PR c++/84294 - attributes on a function template redeclaration silently discarded
[official-gcc.git] / gcc / cp / parser.c
blobe1acb07d29ef5d84b4540a1037bfb612a1e7030a
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2268 /* Classes [gram.class] */
2270 static tree cp_parser_class_name
2271 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2273 (cp_parser *);
2274 static tree cp_parser_class_head
2275 (cp_parser *, bool *);
2276 static enum tag_types cp_parser_class_key
2277 (cp_parser *);
2278 static void cp_parser_type_parameter_key
2279 (cp_parser* parser);
2280 static void cp_parser_member_specification_opt
2281 (cp_parser *);
2282 static void cp_parser_member_declaration
2283 (cp_parser *);
2284 static tree cp_parser_pure_specifier
2285 (cp_parser *);
2286 static tree cp_parser_constant_initializer
2287 (cp_parser *);
2289 /* Derived classes [gram.class.derived] */
2291 static tree cp_parser_base_clause
2292 (cp_parser *);
2293 static tree cp_parser_base_specifier
2294 (cp_parser *);
2296 /* Special member functions [gram.special] */
2298 static tree cp_parser_conversion_function_id
2299 (cp_parser *);
2300 static tree cp_parser_conversion_type_id
2301 (cp_parser *);
2302 static cp_declarator *cp_parser_conversion_declarator_opt
2303 (cp_parser *);
2304 static void cp_parser_ctor_initializer_opt
2305 (cp_parser *);
2306 static void cp_parser_mem_initializer_list
2307 (cp_parser *);
2308 static tree cp_parser_mem_initializer
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer_id
2311 (cp_parser *);
2313 /* Overloading [gram.over] */
2315 static cp_expr cp_parser_operator_function_id
2316 (cp_parser *);
2317 static cp_expr cp_parser_operator
2318 (cp_parser *);
2320 /* Templates [gram.temp] */
2322 static void cp_parser_template_declaration
2323 (cp_parser *, bool);
2324 static tree cp_parser_template_parameter_list
2325 (cp_parser *);
2326 static tree cp_parser_template_parameter
2327 (cp_parser *, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser *, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser *, bool, bool, enum tag_types, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2334 static tree cp_parser_template_argument_list
2335 (cp_parser *);
2336 static tree cp_parser_template_argument
2337 (cp_parser *);
2338 static void cp_parser_explicit_instantiation
2339 (cp_parser *);
2340 static void cp_parser_explicit_specialization
2341 (cp_parser *);
2343 /* Exception handling [gram.exception] */
2345 static tree cp_parser_try_block
2346 (cp_parser *);
2347 static void cp_parser_function_try_block
2348 (cp_parser *);
2349 static void cp_parser_handler_seq
2350 (cp_parser *);
2351 static void cp_parser_handler
2352 (cp_parser *);
2353 static tree cp_parser_exception_declaration
2354 (cp_parser *);
2355 static tree cp_parser_throw_expression
2356 (cp_parser *);
2357 static tree cp_parser_exception_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_type_id_list
2360 (cp_parser *);
2362 /* GNU Extensions */
2364 static tree cp_parser_asm_specification_opt
2365 (cp_parser *);
2366 static tree cp_parser_asm_operand_list
2367 (cp_parser *);
2368 static tree cp_parser_asm_clobber_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_label_list
2371 (cp_parser *);
2372 static bool cp_next_tokens_can_be_attribute_p
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_std_attribute_p
2377 (cp_parser *);
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser *, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser *, size_t);
2384 static tree cp_parser_attributes_opt
2385 (cp_parser *);
2386 static tree cp_parser_gnu_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attribute_list
2389 (cp_parser *);
2390 static tree cp_parser_std_attribute
2391 (cp_parser *, tree);
2392 static tree cp_parser_std_attribute_spec
2393 (cp_parser *);
2394 static tree cp_parser_std_attribute_spec_seq
2395 (cp_parser *);
2396 static size_t cp_parser_skip_attributes_opt
2397 (cp_parser *, size_t);
2398 static bool cp_parser_extension_opt
2399 (cp_parser *, int *);
2400 static void cp_parser_label_declaration
2401 (cp_parser *);
2403 /* Concept Extensions */
2405 static tree cp_parser_requires_clause
2406 (cp_parser *);
2407 static tree cp_parser_requires_clause_opt
2408 (cp_parser *);
2409 static tree cp_parser_requires_expression
2410 (cp_parser *);
2411 static tree cp_parser_requirement_parameter_list
2412 (cp_parser *);
2413 static tree cp_parser_requirement_body
2414 (cp_parser *);
2415 static tree cp_parser_requirement_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement
2418 (cp_parser *);
2419 static tree cp_parser_simple_requirement
2420 (cp_parser *);
2421 static tree cp_parser_compound_requirement
2422 (cp_parser *);
2423 static tree cp_parser_type_requirement
2424 (cp_parser *);
2425 static tree cp_parser_nested_requirement
2426 (cp_parser *);
2428 /* Transactional Memory Extensions */
2430 static tree cp_parser_transaction
2431 (cp_parser *, cp_token *);
2432 static tree cp_parser_transaction_expression
2433 (cp_parser *, enum rid);
2434 static void cp_parser_function_transaction
2435 (cp_parser *, enum rid);
2436 static tree cp_parser_transaction_cancel
2437 (cp_parser *);
2439 enum pragma_context {
2440 pragma_external,
2441 pragma_member,
2442 pragma_objc_icode,
2443 pragma_stmt,
2444 pragma_compound
2446 static bool cp_parser_pragma
2447 (cp_parser *, enum pragma_context, bool *);
2449 /* Objective-C++ Productions */
2451 static tree cp_parser_objc_message_receiver
2452 (cp_parser *);
2453 static tree cp_parser_objc_message_args
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_expression
2456 (cp_parser *);
2457 static cp_expr cp_parser_objc_encode_expression
2458 (cp_parser *);
2459 static tree cp_parser_objc_defs_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_protocol_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_selector_expression
2464 (cp_parser *);
2465 static cp_expr cp_parser_objc_expression
2466 (cp_parser *);
2467 static bool cp_parser_objc_selector_p
2468 (enum cpp_ttype);
2469 static tree cp_parser_objc_selector
2470 (cp_parser *);
2471 static tree cp_parser_objc_protocol_refs_opt
2472 (cp_parser *);
2473 static void cp_parser_objc_declaration
2474 (cp_parser *, tree);
2475 static tree cp_parser_objc_statement
2476 (cp_parser *);
2477 static bool cp_parser_objc_valid_prefix_attributes
2478 (cp_parser *, tree *);
2479 static void cp_parser_objc_at_property_declaration
2480 (cp_parser *) ;
2481 static void cp_parser_objc_at_synthesize_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_dynamic_declaration
2484 (cp_parser *) ;
2485 static tree cp_parser_objc_struct_declaration
2486 (cp_parser *) ;
2488 /* Utility Routines */
2490 static cp_expr cp_parser_lookup_name
2491 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2492 static tree cp_parser_lookup_name_simple
2493 (cp_parser *, tree, location_t);
2494 static tree cp_parser_maybe_treat_template_as_class
2495 (tree, bool);
2496 static bool cp_parser_check_declarator_template_parameters
2497 (cp_parser *, cp_declarator *, location_t);
2498 static bool cp_parser_check_template_parameters
2499 (cp_parser *, unsigned, location_t, cp_declarator *);
2500 static cp_expr cp_parser_simple_cast_expression
2501 (cp_parser *);
2502 static tree cp_parser_global_scope_opt
2503 (cp_parser *, bool);
2504 static bool cp_parser_constructor_declarator_p
2505 (cp_parser *, bool);
2506 static tree cp_parser_function_definition_from_specifiers_and_declarator
2507 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2508 static tree cp_parser_function_definition_after_declarator
2509 (cp_parser *, bool);
2510 static bool cp_parser_template_declaration_after_export
2511 (cp_parser *, bool);
2512 static void cp_parser_perform_template_parameter_access_checks
2513 (vec<deferred_access_check, va_gc> *);
2514 static tree cp_parser_single_declaration
2515 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2516 static cp_expr cp_parser_functional_cast
2517 (cp_parser *, tree);
2518 static tree cp_parser_save_member_function_body
2519 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2520 static tree cp_parser_save_nsdmi
2521 (cp_parser *);
2522 static tree cp_parser_enclosed_template_argument_list
2523 (cp_parser *);
2524 static void cp_parser_save_default_args
2525 (cp_parser *, tree);
2526 static void cp_parser_late_parsing_for_member
2527 (cp_parser *, tree);
2528 static tree cp_parser_late_parse_one_default_arg
2529 (cp_parser *, tree, tree, tree);
2530 static void cp_parser_late_parsing_nsdmi
2531 (cp_parser *, tree);
2532 static void cp_parser_late_parsing_default_args
2533 (cp_parser *, tree);
2534 static tree cp_parser_sizeof_operand
2535 (cp_parser *, enum rid);
2536 static cp_expr cp_parser_trait_expr
2537 (cp_parser *, enum rid);
2538 static bool cp_parser_declares_only_class_p
2539 (cp_parser *);
2540 static void cp_parser_set_storage_class
2541 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2542 static void cp_parser_set_decl_spec_type
2543 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2544 static void set_and_check_decl_spec_loc
2545 (cp_decl_specifier_seq *decl_specs,
2546 cp_decl_spec ds, cp_token *);
2547 static bool cp_parser_friend_p
2548 (const cp_decl_specifier_seq *);
2549 static void cp_parser_required_error
2550 (cp_parser *, required_token, bool, location_t);
2551 static cp_token *cp_parser_require
2552 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2553 static cp_token *cp_parser_require_keyword
2554 (cp_parser *, enum rid, required_token);
2555 static bool cp_parser_token_starts_function_definition_p
2556 (cp_token *);
2557 static bool cp_parser_next_token_starts_class_definition_p
2558 (cp_parser *);
2559 static bool cp_parser_next_token_ends_template_argument_p
2560 (cp_parser *);
2561 static bool cp_parser_nth_token_starts_template_argument_list_p
2562 (cp_parser *, size_t);
2563 static enum tag_types cp_parser_token_is_class_key
2564 (cp_token *);
2565 static enum tag_types cp_parser_token_is_type_parameter_key
2566 (cp_token *);
2567 static void cp_parser_check_class_key
2568 (enum tag_types, tree type);
2569 static void cp_parser_check_access_in_redeclaration
2570 (tree type, location_t location);
2571 static bool cp_parser_optional_template_keyword
2572 (cp_parser *);
2573 static void cp_parser_pre_parsed_nested_name_specifier
2574 (cp_parser *);
2575 static bool cp_parser_cache_group
2576 (cp_parser *, enum cpp_ttype, unsigned);
2577 static tree cp_parser_cache_defarg
2578 (cp_parser *parser, bool nsdmi);
2579 static void cp_parser_parse_tentatively
2580 (cp_parser *);
2581 static void cp_parser_commit_to_tentative_parse
2582 (cp_parser *);
2583 static void cp_parser_commit_to_topmost_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_abort_tentative_parse
2586 (cp_parser *);
2587 static bool cp_parser_parse_definitely
2588 (cp_parser *);
2589 static inline bool cp_parser_parsing_tentatively
2590 (cp_parser *);
2591 static bool cp_parser_uncommitted_to_tentative_parse_p
2592 (cp_parser *);
2593 static void cp_parser_error
2594 (cp_parser *, const char *);
2595 static void cp_parser_name_lookup_error
2596 (cp_parser *, tree, tree, name_lookup_error, location_t);
2597 static bool cp_parser_simulate_error
2598 (cp_parser *);
2599 static bool cp_parser_check_type_definition
2600 (cp_parser *);
2601 static void cp_parser_check_for_definition_in_return_type
2602 (cp_declarator *, tree, location_t type_location);
2603 static void cp_parser_check_for_invalid_template_id
2604 (cp_parser *, tree, enum tag_types, location_t location);
2605 static bool cp_parser_non_integral_constant_expression
2606 (cp_parser *, non_integral_constant);
2607 static void cp_parser_diagnose_invalid_type_name
2608 (cp_parser *, tree, location_t);
2609 static bool cp_parser_parse_and_diagnose_invalid_type_name
2610 (cp_parser *);
2611 static int cp_parser_skip_to_closing_parenthesis
2612 (cp_parser *, bool, bool, bool);
2613 static void cp_parser_skip_to_end_of_statement
2614 (cp_parser *);
2615 static void cp_parser_consume_semicolon_at_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_skip_to_end_of_block_or_statement
2618 (cp_parser *);
2619 static bool cp_parser_skip_to_closing_brace
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_template_parameter_list
2622 (cp_parser *);
2623 static void cp_parser_skip_to_pragma_eol
2624 (cp_parser*, cp_token *);
2625 static bool cp_parser_error_occurred
2626 (cp_parser *);
2627 static bool cp_parser_allow_gnu_extensions_p
2628 (cp_parser *);
2629 static bool cp_parser_is_pure_string_literal
2630 (cp_token *);
2631 static bool cp_parser_is_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_keyword
2634 (cp_token *, enum rid);
2635 static tree cp_parser_make_typename_type
2636 (cp_parser *, tree, location_t location);
2637 static cp_declarator * cp_parser_make_indirect_declarator
2638 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2639 static bool cp_parser_compound_literal_p
2640 (cp_parser *);
2641 static bool cp_parser_array_designator_p
2642 (cp_parser *);
2643 static bool cp_parser_init_statement_p
2644 (cp_parser *);
2645 static bool cp_parser_skip_to_closing_square_bracket
2646 (cp_parser *);
2648 /* Concept-related syntactic transformations */
2650 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2651 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2653 // -------------------------------------------------------------------------- //
2654 // Unevaluated Operand Guard
2656 // Implementation of an RAII helper for unevaluated operand parsing.
2657 cp_unevaluated::cp_unevaluated ()
2659 ++cp_unevaluated_operand;
2660 ++c_inhibit_evaluation_warnings;
2663 cp_unevaluated::~cp_unevaluated ()
2665 --c_inhibit_evaluation_warnings;
2666 --cp_unevaluated_operand;
2669 // -------------------------------------------------------------------------- //
2670 // Tentative Parsing
2672 /* Returns nonzero if we are parsing tentatively. */
2674 static inline bool
2675 cp_parser_parsing_tentatively (cp_parser* parser)
2677 return parser->context->next != NULL;
2680 /* Returns nonzero if TOKEN is a string literal. */
2682 static bool
2683 cp_parser_is_pure_string_literal (cp_token* token)
2685 return (token->type == CPP_STRING ||
2686 token->type == CPP_STRING16 ||
2687 token->type == CPP_STRING32 ||
2688 token->type == CPP_WSTRING ||
2689 token->type == CPP_UTF8STRING);
2692 /* Returns nonzero if TOKEN is a string literal
2693 of a user-defined string literal. */
2695 static bool
2696 cp_parser_is_string_literal (cp_token* token)
2698 return (cp_parser_is_pure_string_literal (token) ||
2699 token->type == CPP_STRING_USERDEF ||
2700 token->type == CPP_STRING16_USERDEF ||
2701 token->type == CPP_STRING32_USERDEF ||
2702 token->type == CPP_WSTRING_USERDEF ||
2703 token->type == CPP_UTF8STRING_USERDEF);
2706 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2708 static bool
2709 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2711 return token->keyword == keyword;
2714 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2715 PRAGMA_NONE. */
2717 static enum pragma_kind
2718 cp_parser_pragma_kind (cp_token *token)
2720 if (token->type != CPP_PRAGMA)
2721 return PRAGMA_NONE;
2722 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2723 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2726 /* Helper function for cp_parser_error.
2727 Having peeked a token of kind TOK1_KIND that might signify
2728 a conflict marker, peek successor tokens to determine
2729 if we actually do have a conflict marker.
2730 Specifically, we consider a run of 7 '<', '=' or '>' characters
2731 at the start of a line as a conflict marker.
2732 These come through the lexer as three pairs and a single,
2733 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2734 If it returns true, *OUT_LOC is written to with the location/range
2735 of the marker. */
2737 static bool
2738 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2739 location_t *out_loc)
2741 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2742 if (token2->type != tok1_kind)
2743 return false;
2744 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2745 if (token3->type != tok1_kind)
2746 return false;
2747 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2748 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2749 return false;
2751 /* It must be at the start of the line. */
2752 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2753 if (LOCATION_COLUMN (start_loc) != 1)
2754 return false;
2756 /* We have a conflict marker. Construct a location of the form:
2757 <<<<<<<
2758 ^~~~~~~
2759 with start == caret, finishing at the end of the marker. */
2760 location_t finish_loc = get_finish (token4->location);
2761 *out_loc = make_location (start_loc, start_loc, finish_loc);
2763 return true;
2766 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2767 RT_CLOSE_PAREN. */
2769 static const char *
2770 get_matching_symbol (required_token token_desc)
2772 switch (token_desc)
2774 default:
2775 gcc_unreachable ();
2776 return "";
2777 case RT_CLOSE_BRACE:
2778 return "{";
2779 case RT_CLOSE_PAREN:
2780 return "(";
2784 /* Attempt to convert TOKEN_DESC from a required_token to an
2785 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2787 static enum cpp_ttype
2788 get_required_cpp_ttype (required_token token_desc)
2790 switch (token_desc)
2792 case RT_SEMICOLON:
2793 return CPP_SEMICOLON;
2794 case RT_OPEN_PAREN:
2795 return CPP_OPEN_PAREN;
2796 case RT_CLOSE_BRACE:
2797 return CPP_CLOSE_BRACE;
2798 case RT_OPEN_BRACE:
2799 return CPP_OPEN_BRACE;
2800 case RT_CLOSE_SQUARE:
2801 return CPP_CLOSE_SQUARE;
2802 case RT_OPEN_SQUARE:
2803 return CPP_OPEN_SQUARE;
2804 case RT_COMMA:
2805 return CPP_COMMA;
2806 case RT_COLON:
2807 return CPP_COLON;
2808 case RT_CLOSE_PAREN:
2809 return CPP_CLOSE_PAREN;
2811 default:
2812 /* Use CPP_EOF as a "no completions possible" code. */
2813 return CPP_EOF;
2818 /* Subroutine of cp_parser_error and cp_parser_required_error.
2820 Issue a diagnostic of the form
2821 FILE:LINE: MESSAGE before TOKEN
2822 where TOKEN is the next token in the input stream. MESSAGE
2823 (specified by the caller) is usually of the form "expected
2824 OTHER-TOKEN".
2826 This bypasses the check for tentative passing, and potentially
2827 adds material needed by cp_parser_required_error.
2829 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2830 suggesting insertion of the missing token.
2832 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2833 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2834 location. */
2836 static void
2837 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2838 required_token missing_token_desc,
2839 location_t matching_location)
2841 cp_token *token = cp_lexer_peek_token (parser->lexer);
2842 /* This diagnostic makes more sense if it is tagged to the line
2843 of the token we just peeked at. */
2844 cp_lexer_set_source_position_from_token (token);
2846 if (token->type == CPP_PRAGMA)
2848 error_at (token->location,
2849 "%<#pragma%> is not allowed here");
2850 cp_parser_skip_to_pragma_eol (parser, token);
2851 return;
2854 /* If this is actually a conflict marker, report it as such. */
2855 if (token->type == CPP_LSHIFT
2856 || token->type == CPP_RSHIFT
2857 || token->type == CPP_EQ_EQ)
2859 location_t loc;
2860 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2862 error_at (loc, "version control conflict marker in file");
2863 return;
2867 gcc_rich_location richloc (input_location);
2869 bool added_matching_location = false;
2871 if (missing_token_desc != RT_NONE)
2873 /* Potentially supply a fix-it hint, suggesting to add the
2874 missing token immediately after the *previous* token.
2875 This may move the primary location within richloc. */
2876 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2877 location_t prev_token_loc
2878 = cp_lexer_previous_token (parser->lexer)->location;
2879 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2881 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2882 Attempt to consolidate diagnostics by printing it as a
2883 secondary range within the main diagnostic. */
2884 if (matching_location != UNKNOWN_LOCATION)
2885 added_matching_location
2886 = richloc.add_location_if_nearby (matching_location);
2889 /* Actually emit the error. */
2890 c_parse_error (gmsgid,
2891 /* Because c_parser_error does not understand
2892 CPP_KEYWORD, keywords are treated like
2893 identifiers. */
2894 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2895 token->u.value, token->flags, &richloc);
2897 if (missing_token_desc != RT_NONE)
2899 /* If we weren't able to consolidate matching_location, then
2900 print it as a secondary diagnostic. */
2901 if (matching_location != UNKNOWN_LOCATION
2902 && !added_matching_location)
2903 inform (matching_location, "to match this %qs",
2904 get_matching_symbol (missing_token_desc));
2908 /* If not parsing tentatively, issue a diagnostic of the form
2909 FILE:LINE: MESSAGE before TOKEN
2910 where TOKEN is the next token in the input stream. MESSAGE
2911 (specified by the caller) is usually of the form "expected
2912 OTHER-TOKEN". */
2914 static void
2915 cp_parser_error (cp_parser* parser, const char* gmsgid)
2917 if (!cp_parser_simulate_error (parser))
2918 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2921 /* Issue an error about name-lookup failing. NAME is the
2922 IDENTIFIER_NODE DECL is the result of
2923 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2924 the thing that we hoped to find. */
2926 static void
2927 cp_parser_name_lookup_error (cp_parser* parser,
2928 tree name,
2929 tree decl,
2930 name_lookup_error desired,
2931 location_t location)
2933 /* If name lookup completely failed, tell the user that NAME was not
2934 declared. */
2935 if (decl == error_mark_node)
2937 if (parser->scope && parser->scope != global_namespace)
2938 error_at (location, "%<%E::%E%> has not been declared",
2939 parser->scope, name);
2940 else if (parser->scope == global_namespace)
2941 error_at (location, "%<::%E%> has not been declared", name);
2942 else if (parser->object_scope
2943 && !CLASS_TYPE_P (parser->object_scope))
2944 error_at (location, "request for member %qE in non-class type %qT",
2945 name, parser->object_scope);
2946 else if (parser->object_scope)
2947 error_at (location, "%<%T::%E%> has not been declared",
2948 parser->object_scope, name);
2949 else
2950 error_at (location, "%qE has not been declared", name);
2952 else if (parser->scope && parser->scope != global_namespace)
2954 switch (desired)
2956 case NLE_TYPE:
2957 error_at (location, "%<%E::%E%> is not a type",
2958 parser->scope, name);
2959 break;
2960 case NLE_CXX98:
2961 error_at (location, "%<%E::%E%> is not a class or namespace",
2962 parser->scope, name);
2963 break;
2964 case NLE_NOT_CXX98:
2965 error_at (location,
2966 "%<%E::%E%> is not a class, namespace, or enumeration",
2967 parser->scope, name);
2968 break;
2969 default:
2970 gcc_unreachable ();
2974 else if (parser->scope == global_namespace)
2976 switch (desired)
2978 case NLE_TYPE:
2979 error_at (location, "%<::%E%> is not a type", name);
2980 break;
2981 case NLE_CXX98:
2982 error_at (location, "%<::%E%> is not a class or namespace", name);
2983 break;
2984 case NLE_NOT_CXX98:
2985 error_at (location,
2986 "%<::%E%> is not a class, namespace, or enumeration",
2987 name);
2988 break;
2989 default:
2990 gcc_unreachable ();
2993 else
2995 switch (desired)
2997 case NLE_TYPE:
2998 error_at (location, "%qE is not a type", name);
2999 break;
3000 case NLE_CXX98:
3001 error_at (location, "%qE is not a class or namespace", name);
3002 break;
3003 case NLE_NOT_CXX98:
3004 error_at (location,
3005 "%qE is not a class, namespace, or enumeration", name);
3006 break;
3007 default:
3008 gcc_unreachable ();
3013 /* If we are parsing tentatively, remember that an error has occurred
3014 during this tentative parse. Returns true if the error was
3015 simulated; false if a message should be issued by the caller. */
3017 static bool
3018 cp_parser_simulate_error (cp_parser* parser)
3020 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3022 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3023 return true;
3025 return false;
3028 /* This function is called when a type is defined. If type
3029 definitions are forbidden at this point, an error message is
3030 issued. */
3032 static bool
3033 cp_parser_check_type_definition (cp_parser* parser)
3035 /* If types are forbidden here, issue a message. */
3036 if (parser->type_definition_forbidden_message)
3038 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3039 in the message need to be interpreted. */
3040 error (parser->type_definition_forbidden_message);
3041 return false;
3043 return true;
3046 /* This function is called when the DECLARATOR is processed. The TYPE
3047 was a type defined in the decl-specifiers. If it is invalid to
3048 define a type in the decl-specifiers for DECLARATOR, an error is
3049 issued. TYPE_LOCATION is the location of TYPE and is used
3050 for error reporting. */
3052 static void
3053 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3054 tree type, location_t type_location)
3056 /* [dcl.fct] forbids type definitions in return types.
3057 Unfortunately, it's not easy to know whether or not we are
3058 processing a return type until after the fact. */
3059 while (declarator
3060 && (declarator->kind == cdk_pointer
3061 || declarator->kind == cdk_reference
3062 || declarator->kind == cdk_ptrmem))
3063 declarator = declarator->declarator;
3064 if (declarator
3065 && declarator->kind == cdk_function)
3067 error_at (type_location,
3068 "new types may not be defined in a return type");
3069 inform (type_location,
3070 "(perhaps a semicolon is missing after the definition of %qT)",
3071 type);
3075 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3076 "<" in any valid C++ program. If the next token is indeed "<",
3077 issue a message warning the user about what appears to be an
3078 invalid attempt to form a template-id. LOCATION is the location
3079 of the type-specifier (TYPE) */
3081 static void
3082 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3083 tree type,
3084 enum tag_types tag_type,
3085 location_t location)
3087 cp_token_position start = 0;
3089 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3091 if (TREE_CODE (type) == TYPE_DECL)
3092 type = TREE_TYPE (type);
3093 if (TYPE_P (type) && !template_placeholder_p (type))
3094 error_at (location, "%qT is not a template", type);
3095 else if (identifier_p (type))
3097 if (tag_type != none_type)
3098 error_at (location, "%qE is not a class template", type);
3099 else
3100 error_at (location, "%qE is not a template", type);
3102 else
3103 error_at (location, "invalid template-id");
3104 /* Remember the location of the invalid "<". */
3105 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3106 start = cp_lexer_token_position (parser->lexer, true);
3107 /* Consume the "<". */
3108 cp_lexer_consume_token (parser->lexer);
3109 /* Parse the template arguments. */
3110 cp_parser_enclosed_template_argument_list (parser);
3111 /* Permanently remove the invalid template arguments so that
3112 this error message is not issued again. */
3113 if (start)
3114 cp_lexer_purge_tokens_after (parser->lexer, start);
3118 /* If parsing an integral constant-expression, issue an error message
3119 about the fact that THING appeared and return true. Otherwise,
3120 return false. In either case, set
3121 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3123 static bool
3124 cp_parser_non_integral_constant_expression (cp_parser *parser,
3125 non_integral_constant thing)
3127 parser->non_integral_constant_expression_p = true;
3128 if (parser->integral_constant_expression_p)
3130 if (!parser->allow_non_integral_constant_expression_p)
3132 const char *msg = NULL;
3133 switch (thing)
3135 case NIC_FLOAT:
3136 pedwarn (input_location, OPT_Wpedantic,
3137 "ISO C++ forbids using a floating-point literal "
3138 "in a constant-expression");
3139 return true;
3140 case NIC_CAST:
3141 error ("a cast to a type other than an integral or "
3142 "enumeration type cannot appear in a "
3143 "constant-expression");
3144 return true;
3145 case NIC_TYPEID:
3146 error ("%<typeid%> operator "
3147 "cannot appear in a constant-expression");
3148 return true;
3149 case NIC_NCC:
3150 error ("non-constant compound literals "
3151 "cannot appear in a constant-expression");
3152 return true;
3153 case NIC_FUNC_CALL:
3154 error ("a function call "
3155 "cannot appear in a constant-expression");
3156 return true;
3157 case NIC_INC:
3158 error ("an increment "
3159 "cannot appear in a constant-expression");
3160 return true;
3161 case NIC_DEC:
3162 error ("an decrement "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_ARRAY_REF:
3166 error ("an array reference "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_ADDR_LABEL:
3170 error ("the address of a label "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_OVERLOADED:
3174 error ("calls to overloaded operators "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_ASSIGNMENT:
3178 error ("an assignment cannot appear in a constant-expression");
3179 return true;
3180 case NIC_COMMA:
3181 error ("a comma operator "
3182 "cannot appear in a constant-expression");
3183 return true;
3184 case NIC_CONSTRUCTOR:
3185 error ("a call to a constructor "
3186 "cannot appear in a constant-expression");
3187 return true;
3188 case NIC_TRANSACTION:
3189 error ("a transaction expression "
3190 "cannot appear in a constant-expression");
3191 return true;
3192 case NIC_THIS:
3193 msg = "this";
3194 break;
3195 case NIC_FUNC_NAME:
3196 msg = "__FUNCTION__";
3197 break;
3198 case NIC_PRETTY_FUNC:
3199 msg = "__PRETTY_FUNCTION__";
3200 break;
3201 case NIC_C99_FUNC:
3202 msg = "__func__";
3203 break;
3204 case NIC_VA_ARG:
3205 msg = "va_arg";
3206 break;
3207 case NIC_ARROW:
3208 msg = "->";
3209 break;
3210 case NIC_POINT:
3211 msg = ".";
3212 break;
3213 case NIC_STAR:
3214 msg = "*";
3215 break;
3216 case NIC_ADDR:
3217 msg = "&";
3218 break;
3219 case NIC_PREINCREMENT:
3220 msg = "++";
3221 break;
3222 case NIC_PREDECREMENT:
3223 msg = "--";
3224 break;
3225 case NIC_NEW:
3226 msg = "new";
3227 break;
3228 case NIC_DEL:
3229 msg = "delete";
3230 break;
3231 default:
3232 gcc_unreachable ();
3234 if (msg)
3235 error ("%qs cannot appear in a constant-expression", msg);
3236 return true;
3239 return false;
3242 /* Emit a diagnostic for an invalid type name. This function commits
3243 to the current active tentative parse, if any. (Otherwise, the
3244 problematic construct might be encountered again later, resulting
3245 in duplicate error messages.) LOCATION is the location of ID. */
3247 static void
3248 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3249 location_t location)
3251 tree decl, ambiguous_decls;
3252 cp_parser_commit_to_tentative_parse (parser);
3253 /* Try to lookup the identifier. */
3254 decl = cp_parser_lookup_name (parser, id, none_type,
3255 /*is_template=*/false,
3256 /*is_namespace=*/false,
3257 /*check_dependency=*/true,
3258 &ambiguous_decls, location);
3259 if (ambiguous_decls)
3260 /* If the lookup was ambiguous, an error will already have
3261 been issued. */
3262 return;
3263 /* If the lookup found a template-name, it means that the user forgot
3264 to specify an argument list. Emit a useful error message. */
3265 if (DECL_TYPE_TEMPLATE_P (decl))
3267 error_at (location,
3268 "invalid use of template-name %qE without an argument list",
3269 decl);
3270 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3271 inform (location, "class template argument deduction is only available "
3272 "with -std=c++17 or -std=gnu++17");
3273 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3275 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3276 error_at (location, "invalid use of destructor %qD as a type", id);
3277 else if (TREE_CODE (decl) == TYPE_DECL)
3278 /* Something like 'unsigned A a;' */
3279 error_at (location, "invalid combination of multiple type-specifiers");
3280 else if (!parser->scope)
3282 /* Issue an error message. */
3283 name_hint hint;
3284 if (TREE_CODE (id) == IDENTIFIER_NODE)
3285 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3286 if (hint)
3288 gcc_rich_location richloc (location);
3289 richloc.add_fixit_replace (hint.suggestion ());
3290 error_at (&richloc,
3291 "%qE does not name a type; did you mean %qs?",
3292 id, hint.suggestion ());
3294 else
3295 error_at (location, "%qE does not name a type", id);
3296 /* If we're in a template class, it's possible that the user was
3297 referring to a type from a base class. For example:
3299 template <typename T> struct A { typedef T X; };
3300 template <typename T> struct B : public A<T> { X x; };
3302 The user should have said "typename A<T>::X". */
3303 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3304 inform (location, "C++11 %<constexpr%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3307 inform (location, "C++11 %<noexcept%> only available with "
3308 "-std=c++11 or -std=gnu++11");
3309 else if (cxx_dialect < cxx11
3310 && TREE_CODE (id) == IDENTIFIER_NODE
3311 && id_equal (id, "thread_local"))
3312 inform (location, "C++11 %<thread_local%> only available with "
3313 "-std=c++11 or -std=gnu++11");
3314 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3315 inform (location, "%<concept%> only available with -fconcepts");
3316 else if (processing_template_decl && current_class_type
3317 && TYPE_BINFO (current_class_type))
3319 tree b;
3321 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3323 b = TREE_CHAIN (b))
3325 tree base_type = BINFO_TYPE (b);
3326 if (CLASS_TYPE_P (base_type)
3327 && dependent_type_p (base_type))
3329 tree field;
3330 /* Go from a particular instantiation of the
3331 template (which will have an empty TYPE_FIELDs),
3332 to the main version. */
3333 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3334 for (field = TYPE_FIELDS (base_type);
3335 field;
3336 field = DECL_CHAIN (field))
3337 if (TREE_CODE (field) == TYPE_DECL
3338 && DECL_NAME (field) == id)
3340 inform (location,
3341 "(perhaps %<typename %T::%E%> was intended)",
3342 BINFO_TYPE (b), id);
3343 break;
3345 if (field)
3346 break;
3351 /* Here we diagnose qualified-ids where the scope is actually correct,
3352 but the identifier does not resolve to a valid type name. */
3353 else if (parser->scope != error_mark_node)
3355 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3357 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3358 error_at (location_of (id),
3359 "%qE in namespace %qE does not name a template type",
3360 id, parser->scope);
3361 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3362 error_at (location_of (id),
3363 "%qE in namespace %qE does not name a template type",
3364 TREE_OPERAND (id, 0), parser->scope);
3365 else
3366 error_at (location_of (id),
3367 "%qE in namespace %qE does not name a type",
3368 id, parser->scope);
3369 if (DECL_P (decl))
3370 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3371 else if (decl == error_mark_node)
3372 suggest_alternative_in_explicit_scope (location, id,
3373 parser->scope);
3375 else if (CLASS_TYPE_P (parser->scope)
3376 && constructor_name_p (id, parser->scope))
3378 /* A<T>::A<T>() */
3379 error_at (location, "%<%T::%E%> names the constructor, not"
3380 " the type", parser->scope, id);
3381 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3382 error_at (location, "and %qT has no template constructors",
3383 parser->scope);
3385 else if (TYPE_P (parser->scope)
3386 && dependent_scope_p (parser->scope))
3388 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3389 error_at (location,
3390 "need %<typename%> before %<%T::%D::%E%> because "
3391 "%<%T::%D%> is a dependent scope",
3392 TYPE_CONTEXT (parser->scope),
3393 TYPENAME_TYPE_FULLNAME (parser->scope),
3395 TYPE_CONTEXT (parser->scope),
3396 TYPENAME_TYPE_FULLNAME (parser->scope));
3397 else
3398 error_at (location, "need %<typename%> before %<%T::%E%> because "
3399 "%qT is a dependent scope",
3400 parser->scope, id, parser->scope);
3402 else if (TYPE_P (parser->scope))
3404 if (!COMPLETE_TYPE_P (parser->scope))
3405 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3406 parser->scope);
3407 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3408 error_at (location_of (id),
3409 "%qE in %q#T does not name a template type",
3410 id, parser->scope);
3411 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3412 error_at (location_of (id),
3413 "%qE in %q#T does not name a template type",
3414 TREE_OPERAND (id, 0), parser->scope);
3415 else
3416 error_at (location_of (id),
3417 "%qE in %q#T does not name a type",
3418 id, parser->scope);
3419 if (DECL_P (decl))
3420 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3422 else
3423 gcc_unreachable ();
3427 /* Check for a common situation where a type-name should be present,
3428 but is not, and issue a sensible error message. Returns true if an
3429 invalid type-name was detected.
3431 The situation handled by this function are variable declarations of the
3432 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3433 Usually, `ID' should name a type, but if we got here it means that it
3434 does not. We try to emit the best possible error message depending on
3435 how exactly the id-expression looks like. */
3437 static bool
3438 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3440 tree id;
3441 cp_token *token = cp_lexer_peek_token (parser->lexer);
3443 /* Avoid duplicate error about ambiguous lookup. */
3444 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3446 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3447 if (next->type == CPP_NAME && next->error_reported)
3448 goto out;
3451 cp_parser_parse_tentatively (parser);
3452 id = cp_parser_id_expression (parser,
3453 /*template_keyword_p=*/false,
3454 /*check_dependency_p=*/true,
3455 /*template_p=*/NULL,
3456 /*declarator_p=*/true,
3457 /*optional_p=*/false);
3458 /* If the next token is a (, this is a function with no explicit return
3459 type, i.e. constructor, destructor or conversion op. */
3460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3461 || TREE_CODE (id) == TYPE_DECL)
3463 cp_parser_abort_tentative_parse (parser);
3464 return false;
3466 if (!cp_parser_parse_definitely (parser))
3467 return false;
3469 /* Emit a diagnostic for the invalid type. */
3470 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3471 out:
3472 /* If we aren't in the middle of a declarator (i.e. in a
3473 parameter-declaration-clause), skip to the end of the declaration;
3474 there's no point in trying to process it. */
3475 if (!parser->in_declarator_p)
3476 cp_parser_skip_to_end_of_block_or_statement (parser);
3477 return true;
3480 /* Consume tokens up to, and including, the next non-nested closing `)'.
3481 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3482 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3483 found an unnested token of that type. */
3485 static int
3486 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3487 bool recovering,
3488 cpp_ttype or_ttype,
3489 bool consume_paren)
3491 unsigned paren_depth = 0;
3492 unsigned brace_depth = 0;
3493 unsigned square_depth = 0;
3495 if (recovering && or_ttype == CPP_EOF
3496 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3497 return 0;
3499 while (true)
3501 cp_token * token = cp_lexer_peek_token (parser->lexer);
3503 /* Have we found what we're looking for before the closing paren? */
3504 if (token->type == or_ttype && or_ttype != CPP_EOF
3505 && !brace_depth && !paren_depth && !square_depth)
3506 return -1;
3508 switch (token->type)
3510 case CPP_EOF:
3511 case CPP_PRAGMA_EOL:
3512 /* If we've run out of tokens, then there is no closing `)'. */
3513 return 0;
3515 /* This is good for lambda expression capture-lists. */
3516 case CPP_OPEN_SQUARE:
3517 ++square_depth;
3518 break;
3519 case CPP_CLOSE_SQUARE:
3520 if (!square_depth--)
3521 return 0;
3522 break;
3524 case CPP_SEMICOLON:
3525 /* This matches the processing in skip_to_end_of_statement. */
3526 if (!brace_depth)
3527 return 0;
3528 break;
3530 case CPP_OPEN_BRACE:
3531 ++brace_depth;
3532 break;
3533 case CPP_CLOSE_BRACE:
3534 if (!brace_depth--)
3535 return 0;
3536 break;
3538 case CPP_OPEN_PAREN:
3539 if (!brace_depth)
3540 ++paren_depth;
3541 break;
3543 case CPP_CLOSE_PAREN:
3544 if (!brace_depth && !paren_depth--)
3546 if (consume_paren)
3547 cp_lexer_consume_token (parser->lexer);
3548 return 1;
3550 break;
3552 default:
3553 break;
3556 /* Consume the token. */
3557 cp_lexer_consume_token (parser->lexer);
3561 /* Consume tokens up to, and including, the next non-nested closing `)'.
3562 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3563 are doing error recovery. Returns -1 if OR_COMMA is true and we
3564 found an unnested token of that type. */
3566 static int
3567 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3568 bool recovering,
3569 bool or_comma,
3570 bool consume_paren)
3572 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3573 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3574 ttype, consume_paren);
3577 /* Consume tokens until we reach the end of the current statement.
3578 Normally, that will be just before consuming a `;'. However, if a
3579 non-nested `}' comes first, then we stop before consuming that. */
3581 static void
3582 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3584 unsigned nesting_depth = 0;
3586 /* Unwind generic function template scope if necessary. */
3587 if (parser->fully_implicit_function_template_p)
3588 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3590 while (true)
3592 cp_token *token = cp_lexer_peek_token (parser->lexer);
3594 switch (token->type)
3596 case CPP_EOF:
3597 case CPP_PRAGMA_EOL:
3598 /* If we've run out of tokens, stop. */
3599 return;
3601 case CPP_SEMICOLON:
3602 /* If the next token is a `;', we have reached the end of the
3603 statement. */
3604 if (!nesting_depth)
3605 return;
3606 break;
3608 case CPP_CLOSE_BRACE:
3609 /* If this is a non-nested '}', stop before consuming it.
3610 That way, when confronted with something like:
3612 { 3 + }
3614 we stop before consuming the closing '}', even though we
3615 have not yet reached a `;'. */
3616 if (nesting_depth == 0)
3617 return;
3619 /* If it is the closing '}' for a block that we have
3620 scanned, stop -- but only after consuming the token.
3621 That way given:
3623 void f g () { ... }
3624 typedef int I;
3626 we will stop after the body of the erroneously declared
3627 function, but before consuming the following `typedef'
3628 declaration. */
3629 if (--nesting_depth == 0)
3631 cp_lexer_consume_token (parser->lexer);
3632 return;
3634 break;
3636 case CPP_OPEN_BRACE:
3637 ++nesting_depth;
3638 break;
3640 default:
3641 break;
3644 /* Consume the token. */
3645 cp_lexer_consume_token (parser->lexer);
3649 /* This function is called at the end of a statement or declaration.
3650 If the next token is a semicolon, it is consumed; otherwise, error
3651 recovery is attempted. */
3653 static void
3654 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3656 /* Look for the trailing `;'. */
3657 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3659 /* If there is additional (erroneous) input, skip to the end of
3660 the statement. */
3661 cp_parser_skip_to_end_of_statement (parser);
3662 /* If the next token is now a `;', consume it. */
3663 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3664 cp_lexer_consume_token (parser->lexer);
3668 /* Skip tokens until we have consumed an entire block, or until we
3669 have consumed a non-nested `;'. */
3671 static void
3672 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3674 int nesting_depth = 0;
3676 /* Unwind generic function template scope if necessary. */
3677 if (parser->fully_implicit_function_template_p)
3678 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3680 while (nesting_depth >= 0)
3682 cp_token *token = cp_lexer_peek_token (parser->lexer);
3684 switch (token->type)
3686 case CPP_EOF:
3687 case CPP_PRAGMA_EOL:
3688 /* If we've run out of tokens, stop. */
3689 return;
3691 case CPP_SEMICOLON:
3692 /* Stop if this is an unnested ';'. */
3693 if (!nesting_depth)
3694 nesting_depth = -1;
3695 break;
3697 case CPP_CLOSE_BRACE:
3698 /* Stop if this is an unnested '}', or closes the outermost
3699 nesting level. */
3700 nesting_depth--;
3701 if (nesting_depth < 0)
3702 return;
3703 if (!nesting_depth)
3704 nesting_depth = -1;
3705 break;
3707 case CPP_OPEN_BRACE:
3708 /* Nest. */
3709 nesting_depth++;
3710 break;
3712 default:
3713 break;
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser->lexer);
3721 /* Skip tokens until a non-nested closing curly brace is the next
3722 token, or there are no more tokens. Return true in the first case,
3723 false otherwise. */
3725 static bool
3726 cp_parser_skip_to_closing_brace (cp_parser *parser)
3728 unsigned nesting_depth = 0;
3730 while (true)
3732 cp_token *token = cp_lexer_peek_token (parser->lexer);
3734 switch (token->type)
3736 case CPP_EOF:
3737 case CPP_PRAGMA_EOL:
3738 /* If we've run out of tokens, stop. */
3739 return false;
3741 case CPP_CLOSE_BRACE:
3742 /* If the next token is a non-nested `}', then we have reached
3743 the end of the current block. */
3744 if (nesting_depth-- == 0)
3745 return true;
3746 break;
3748 case CPP_OPEN_BRACE:
3749 /* If it the next token is a `{', then we are entering a new
3750 block. Consume the entire block. */
3751 ++nesting_depth;
3752 break;
3754 default:
3755 break;
3758 /* Consume the token. */
3759 cp_lexer_consume_token (parser->lexer);
3763 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3764 parameter is the PRAGMA token, allowing us to purge the entire pragma
3765 sequence. */
3767 static void
3768 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3770 cp_token *token;
3772 parser->lexer->in_pragma = false;
3775 token = cp_lexer_consume_token (parser->lexer);
3776 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3778 /* Ensure that the pragma is not parsed again. */
3779 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3782 /* Require pragma end of line, resyncing with it as necessary. The
3783 arguments are as for cp_parser_skip_to_pragma_eol. */
3785 static void
3786 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3788 parser->lexer->in_pragma = false;
3789 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3790 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3793 /* This is a simple wrapper around make_typename_type. When the id is
3794 an unresolved identifier node, we can provide a superior diagnostic
3795 using cp_parser_diagnose_invalid_type_name. */
3797 static tree
3798 cp_parser_make_typename_type (cp_parser *parser, tree id,
3799 location_t id_location)
3801 tree result;
3802 if (identifier_p (id))
3804 result = make_typename_type (parser->scope, id, typename_type,
3805 /*complain=*/tf_none);
3806 if (result == error_mark_node)
3807 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3808 return result;
3810 return make_typename_type (parser->scope, id, typename_type, tf_error);
3813 /* This is a wrapper around the
3814 make_{pointer,ptrmem,reference}_declarator functions that decides
3815 which one to call based on the CODE and CLASS_TYPE arguments. The
3816 CODE argument should be one of the values returned by
3817 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3818 appertain to the pointer or reference. */
3820 static cp_declarator *
3821 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3822 cp_cv_quals cv_qualifiers,
3823 cp_declarator *target,
3824 tree attributes)
3826 if (code == ERROR_MARK)
3827 return cp_error_declarator;
3829 if (code == INDIRECT_REF)
3830 if (class_type == NULL_TREE)
3831 return make_pointer_declarator (cv_qualifiers, target, attributes);
3832 else
3833 return make_ptrmem_declarator (cv_qualifiers, class_type,
3834 target, attributes);
3835 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3836 return make_reference_declarator (cv_qualifiers, target,
3837 false, attributes);
3838 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3839 return make_reference_declarator (cv_qualifiers, target,
3840 true, attributes);
3841 gcc_unreachable ();
3844 /* Create a new C++ parser. */
3846 static cp_parser *
3847 cp_parser_new (void)
3849 cp_parser *parser;
3850 cp_lexer *lexer;
3851 unsigned i;
3853 /* cp_lexer_new_main is called before doing GC allocation because
3854 cp_lexer_new_main might load a PCH file. */
3855 lexer = cp_lexer_new_main ();
3857 /* Initialize the binops_by_token so that we can get the tree
3858 directly from the token. */
3859 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3860 binops_by_token[binops[i].token_type] = binops[i];
3862 parser = ggc_cleared_alloc<cp_parser> ();
3863 parser->lexer = lexer;
3864 parser->context = cp_parser_context_new (NULL);
3866 /* For now, we always accept GNU extensions. */
3867 parser->allow_gnu_extensions_p = 1;
3869 /* The `>' token is a greater-than operator, not the end of a
3870 template-id. */
3871 parser->greater_than_is_operator_p = true;
3873 parser->default_arg_ok_p = true;
3875 /* We are not parsing a constant-expression. */
3876 parser->integral_constant_expression_p = false;
3877 parser->allow_non_integral_constant_expression_p = false;
3878 parser->non_integral_constant_expression_p = false;
3880 /* Local variable names are not forbidden. */
3881 parser->local_variables_forbidden_p = false;
3883 /* We are not processing an `extern "C"' declaration. */
3884 parser->in_unbraced_linkage_specification_p = false;
3886 /* We are not processing a declarator. */
3887 parser->in_declarator_p = false;
3889 /* We are not processing a template-argument-list. */
3890 parser->in_template_argument_list_p = false;
3892 /* We are not in an iteration statement. */
3893 parser->in_statement = 0;
3895 /* We are not in a switch statement. */
3896 parser->in_switch_statement_p = false;
3898 /* We are not parsing a type-id inside an expression. */
3899 parser->in_type_id_in_expr_p = false;
3901 /* Declarations aren't implicitly extern "C". */
3902 parser->implicit_extern_c = false;
3904 /* String literals should be translated to the execution character set. */
3905 parser->translate_strings_p = true;
3907 /* We are not parsing a function body. */
3908 parser->in_function_body = false;
3910 /* We can correct until told otherwise. */
3911 parser->colon_corrects_to_scope_p = true;
3913 /* The unparsed function queue is empty. */
3914 push_unparsed_function_queues (parser);
3916 /* There are no classes being defined. */
3917 parser->num_classes_being_defined = 0;
3919 /* No template parameters apply. */
3920 parser->num_template_parameter_lists = 0;
3922 /* Special parsing data structures. */
3923 parser->omp_declare_simd = NULL;
3924 parser->oacc_routine = NULL;
3926 /* Not declaring an implicit function template. */
3927 parser->auto_is_implicit_function_template_parm_p = false;
3928 parser->fully_implicit_function_template_p = false;
3929 parser->implicit_template_parms = 0;
3930 parser->implicit_template_scope = 0;
3932 /* Allow constrained-type-specifiers. */
3933 parser->prevent_constrained_type_specifiers = 0;
3935 /* We haven't yet seen an 'extern "C"'. */
3936 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3938 return parser;
3941 /* Create a cp_lexer structure which will emit the tokens in CACHE
3942 and push it onto the parser's lexer stack. This is used for delayed
3943 parsing of in-class method bodies and default arguments, and should
3944 not be confused with tentative parsing. */
3945 static void
3946 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3948 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3949 lexer->next = parser->lexer;
3950 parser->lexer = lexer;
3952 /* Move the current source position to that of the first token in the
3953 new lexer. */
3954 cp_lexer_set_source_position_from_token (lexer->next_token);
3957 /* Pop the top lexer off the parser stack. This is never used for the
3958 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3959 static void
3960 cp_parser_pop_lexer (cp_parser *parser)
3962 cp_lexer *lexer = parser->lexer;
3963 parser->lexer = lexer->next;
3964 cp_lexer_destroy (lexer);
3966 /* Put the current source position back where it was before this
3967 lexer was pushed. */
3968 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3971 /* Lexical conventions [gram.lex] */
3973 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3974 identifier. */
3976 static cp_expr
3977 cp_parser_identifier (cp_parser* parser)
3979 cp_token *token;
3981 /* Look for the identifier. */
3982 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3983 /* Return the value. */
3984 if (token)
3985 return cp_expr (token->u.value, token->location);
3986 else
3987 return error_mark_node;
3990 /* Parse a sequence of adjacent string constants. Returns a
3991 TREE_STRING representing the combined, nul-terminated string
3992 constant. If TRANSLATE is true, translate the string to the
3993 execution character set. If WIDE_OK is true, a wide string is
3994 invalid here.
3996 C++98 [lex.string] says that if a narrow string literal token is
3997 adjacent to a wide string literal token, the behavior is undefined.
3998 However, C99 6.4.5p4 says that this results in a wide string literal.
3999 We follow C99 here, for consistency with the C front end.
4001 This code is largely lifted from lex_string() in c-lex.c.
4003 FUTURE: ObjC++ will need to handle @-strings here. */
4004 static cp_expr
4005 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4006 bool lookup_udlit = true)
4008 tree value;
4009 size_t count;
4010 struct obstack str_ob;
4011 cpp_string str, istr, *strs;
4012 cp_token *tok;
4013 enum cpp_ttype type, curr_type;
4014 int have_suffix_p = 0;
4015 tree string_tree;
4016 tree suffix_id = NULL_TREE;
4017 bool curr_tok_is_userdef_p = false;
4019 tok = cp_lexer_peek_token (parser->lexer);
4020 if (!cp_parser_is_string_literal (tok))
4022 cp_parser_error (parser, "expected string-literal");
4023 return error_mark_node;
4026 location_t loc = tok->location;
4028 if (cpp_userdef_string_p (tok->type))
4030 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4031 curr_type = cpp_userdef_string_remove_type (tok->type);
4032 curr_tok_is_userdef_p = true;
4034 else
4036 string_tree = tok->u.value;
4037 curr_type = tok->type;
4039 type = curr_type;
4041 /* Try to avoid the overhead of creating and destroying an obstack
4042 for the common case of just one string. */
4043 if (!cp_parser_is_string_literal
4044 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4046 cp_lexer_consume_token (parser->lexer);
4048 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4049 str.len = TREE_STRING_LENGTH (string_tree);
4050 count = 1;
4052 if (curr_tok_is_userdef_p)
4054 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4055 have_suffix_p = 1;
4056 curr_type = cpp_userdef_string_remove_type (tok->type);
4058 else
4059 curr_type = tok->type;
4061 strs = &str;
4063 else
4065 location_t last_tok_loc = tok->location;
4066 gcc_obstack_init (&str_ob);
4067 count = 0;
4071 cp_lexer_consume_token (parser->lexer);
4072 count++;
4073 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4074 str.len = TREE_STRING_LENGTH (string_tree);
4076 if (curr_tok_is_userdef_p)
4078 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4079 if (have_suffix_p == 0)
4081 suffix_id = curr_suffix_id;
4082 have_suffix_p = 1;
4084 else if (have_suffix_p == 1
4085 && curr_suffix_id != suffix_id)
4087 error ("inconsistent user-defined literal suffixes"
4088 " %qD and %qD in string literal",
4089 suffix_id, curr_suffix_id);
4090 have_suffix_p = -1;
4092 curr_type = cpp_userdef_string_remove_type (tok->type);
4094 else
4095 curr_type = tok->type;
4097 if (type != curr_type)
4099 if (type == CPP_STRING)
4100 type = curr_type;
4101 else if (curr_type != CPP_STRING)
4103 rich_location rich_loc (line_table, tok->location);
4104 rich_loc.add_range (last_tok_loc, false);
4105 error_at (&rich_loc,
4106 "unsupported non-standard concatenation "
4107 "of string literals");
4111 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4113 last_tok_loc = tok->location;
4115 tok = cp_lexer_peek_token (parser->lexer);
4116 if (cpp_userdef_string_p (tok->type))
4118 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4119 curr_type = cpp_userdef_string_remove_type (tok->type);
4120 curr_tok_is_userdef_p = true;
4122 else
4124 string_tree = tok->u.value;
4125 curr_type = tok->type;
4126 curr_tok_is_userdef_p = false;
4129 while (cp_parser_is_string_literal (tok));
4131 /* A string literal built by concatenation has its caret=start at
4132 the start of the initial string, and its finish at the finish of
4133 the final string literal. */
4134 loc = make_location (loc, loc, get_finish (last_tok_loc));
4136 strs = (cpp_string *) obstack_finish (&str_ob);
4139 if (type != CPP_STRING && !wide_ok)
4141 cp_parser_error (parser, "a wide string is invalid in this context");
4142 type = CPP_STRING;
4145 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4146 (parse_in, strs, count, &istr, type))
4148 value = build_string (istr.len, (const char *)istr.text);
4149 free (CONST_CAST (unsigned char *, istr.text));
4151 switch (type)
4153 default:
4154 case CPP_STRING:
4155 case CPP_UTF8STRING:
4156 TREE_TYPE (value) = char_array_type_node;
4157 break;
4158 case CPP_STRING16:
4159 TREE_TYPE (value) = char16_array_type_node;
4160 break;
4161 case CPP_STRING32:
4162 TREE_TYPE (value) = char32_array_type_node;
4163 break;
4164 case CPP_WSTRING:
4165 TREE_TYPE (value) = wchar_array_type_node;
4166 break;
4169 value = fix_string_type (value);
4171 if (have_suffix_p)
4173 tree literal = build_userdef_literal (suffix_id, value,
4174 OT_NONE, NULL_TREE);
4175 if (lookup_udlit)
4176 value = cp_parser_userdef_string_literal (literal);
4177 else
4178 value = literal;
4181 else
4182 /* cpp_interpret_string has issued an error. */
4183 value = error_mark_node;
4185 if (count > 1)
4186 obstack_free (&str_ob, 0);
4188 return cp_expr (value, loc);
4191 /* Look up a literal operator with the name and the exact arguments. */
4193 static tree
4194 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4196 tree decl;
4197 decl = lookup_name (name);
4198 if (!decl || !is_overloaded_fn (decl))
4199 return error_mark_node;
4201 for (lkp_iterator iter (decl); iter; ++iter)
4203 unsigned int ix;
4204 bool found = true;
4205 tree fn = *iter;
4206 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4207 if (parmtypes != NULL_TREE)
4209 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4210 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4212 tree tparm = TREE_VALUE (parmtypes);
4213 tree targ = TREE_TYPE ((*args)[ix]);
4214 bool ptr = TYPE_PTR_P (tparm);
4215 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4216 if ((ptr || arr || !same_type_p (tparm, targ))
4217 && (!ptr || !arr
4218 || !same_type_p (TREE_TYPE (tparm),
4219 TREE_TYPE (targ))))
4220 found = false;
4222 if (found
4223 && ix == vec_safe_length (args)
4224 /* May be this should be sufficient_parms_p instead,
4225 depending on how exactly should user-defined literals
4226 work in presence of default arguments on the literal
4227 operator parameters. */
4228 && parmtypes == void_list_node)
4229 return decl;
4233 return error_mark_node;
4236 /* Parse a user-defined char constant. Returns a call to a user-defined
4237 literal operator taking the character as an argument. */
4239 static cp_expr
4240 cp_parser_userdef_char_literal (cp_parser *parser)
4242 cp_token *token = cp_lexer_consume_token (parser->lexer);
4243 tree literal = token->u.value;
4244 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4245 tree value = USERDEF_LITERAL_VALUE (literal);
4246 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4247 tree decl, result;
4249 /* Build up a call to the user-defined operator */
4250 /* Lookup the name we got back from the id-expression. */
4251 vec<tree, va_gc> *args = make_tree_vector ();
4252 vec_safe_push (args, value);
4253 decl = lookup_literal_operator (name, args);
4254 if (!decl || decl == error_mark_node)
4256 error ("unable to find character literal operator %qD with %qT argument",
4257 name, TREE_TYPE (value));
4258 release_tree_vector (args);
4259 return error_mark_node;
4261 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4262 release_tree_vector (args);
4263 return result;
4266 /* A subroutine of cp_parser_userdef_numeric_literal to
4267 create a char... template parameter pack from a string node. */
4269 static tree
4270 make_char_string_pack (tree value)
4272 tree charvec;
4273 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4274 const char *str = TREE_STRING_POINTER (value);
4275 int i, len = TREE_STRING_LENGTH (value) - 1;
4276 tree argvec = make_tree_vec (1);
4278 /* Fill in CHARVEC with all of the parameters. */
4279 charvec = make_tree_vec (len);
4280 for (i = 0; i < len; ++i)
4281 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4283 /* Build the argument packs. */
4284 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4286 TREE_VEC_ELT (argvec, 0) = argpack;
4288 return argvec;
4291 /* A subroutine of cp_parser_userdef_numeric_literal to
4292 create a char... template parameter pack from a string node. */
4294 static tree
4295 make_string_pack (tree value)
4297 tree charvec;
4298 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4299 const unsigned char *str
4300 = (const unsigned char *) TREE_STRING_POINTER (value);
4301 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4302 int len = TREE_STRING_LENGTH (value) / sz - 1;
4303 tree argvec = make_tree_vec (2);
4305 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4306 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4308 /* First template parm is character type. */
4309 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4311 /* Fill in CHARVEC with all of the parameters. */
4312 charvec = make_tree_vec (len);
4313 for (int i = 0; i < len; ++i)
4314 TREE_VEC_ELT (charvec, i)
4315 = double_int_to_tree (str_char_type_node,
4316 double_int::from_buffer (str + i * sz, sz));
4318 /* Build the argument packs. */
4319 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4321 TREE_VEC_ELT (argvec, 1) = argpack;
4323 return argvec;
4326 /* Parse a user-defined numeric constant. returns a call to a user-defined
4327 literal operator. */
4329 static cp_expr
4330 cp_parser_userdef_numeric_literal (cp_parser *parser)
4332 cp_token *token = cp_lexer_consume_token (parser->lexer);
4333 tree literal = token->u.value;
4334 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4335 tree value = USERDEF_LITERAL_VALUE (literal);
4336 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4337 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4338 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4339 tree decl, result;
4340 vec<tree, va_gc> *args;
4342 /* Look for a literal operator taking the exact type of numeric argument
4343 as the literal value. */
4344 args = make_tree_vector ();
4345 vec_safe_push (args, value);
4346 decl = lookup_literal_operator (name, args);
4347 if (decl && decl != error_mark_node)
4349 result = finish_call_expr (decl, &args, false, true,
4350 tf_warning_or_error);
4352 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4354 warning_at (token->location, OPT_Woverflow,
4355 "integer literal exceeds range of %qT type",
4356 long_long_unsigned_type_node);
4358 else
4360 if (overflow > 0)
4361 warning_at (token->location, OPT_Woverflow,
4362 "floating literal exceeds range of %qT type",
4363 long_double_type_node);
4364 else if (overflow < 0)
4365 warning_at (token->location, OPT_Woverflow,
4366 "floating literal truncated to zero");
4369 release_tree_vector (args);
4370 return result;
4372 release_tree_vector (args);
4374 /* If the numeric argument didn't work, look for a raw literal
4375 operator taking a const char* argument consisting of the number
4376 in string format. */
4377 args = make_tree_vector ();
4378 vec_safe_push (args, num_string);
4379 decl = lookup_literal_operator (name, args);
4380 if (decl && decl != error_mark_node)
4382 result = finish_call_expr (decl, &args, false, true,
4383 tf_warning_or_error);
4384 release_tree_vector (args);
4385 return result;
4387 release_tree_vector (args);
4389 /* If the raw literal didn't work, look for a non-type template
4390 function with parameter pack char.... Call the function with
4391 template parameter characters representing the number. */
4392 args = make_tree_vector ();
4393 decl = lookup_literal_operator (name, args);
4394 if (decl && decl != error_mark_node)
4396 tree tmpl_args = make_char_string_pack (num_string);
4397 decl = lookup_template_function (decl, tmpl_args);
4398 result = finish_call_expr (decl, &args, false, true,
4399 tf_warning_or_error);
4400 release_tree_vector (args);
4401 return result;
4404 release_tree_vector (args);
4406 /* In C++14 the standard library defines complex number suffixes that
4407 conflict with GNU extensions. Prefer them if <complex> is #included. */
4408 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4409 bool i14 = (cxx_dialect > cxx11
4410 && (id_equal (suffix_id, "i")
4411 || id_equal (suffix_id, "if")
4412 || id_equal (suffix_id, "il")));
4413 diagnostic_t kind = DK_ERROR;
4414 int opt = 0;
4416 if (i14 && ext)
4418 tree cxlit = lookup_qualified_name (std_node,
4419 get_identifier ("complex_literals"),
4420 0, false, false);
4421 if (cxlit == error_mark_node)
4423 /* No <complex>, so pedwarn and use GNU semantics. */
4424 kind = DK_PEDWARN;
4425 opt = OPT_Wpedantic;
4429 bool complained
4430 = emit_diagnostic (kind, input_location, opt,
4431 "unable to find numeric literal operator %qD", name);
4433 if (!complained)
4434 /* Don't inform either. */;
4435 else if (i14)
4437 inform (token->location, "add %<using namespace std::complex_literals%> "
4438 "(from <complex>) to enable the C++14 user-defined literal "
4439 "suffixes");
4440 if (ext)
4441 inform (token->location, "or use %<j%> instead of %<i%> for the "
4442 "GNU built-in suffix");
4444 else if (!ext)
4445 inform (token->location, "use -fext-numeric-literals "
4446 "to enable more built-in suffixes");
4448 if (kind == DK_ERROR)
4449 value = error_mark_node;
4450 else
4452 /* Use the built-in semantics. */
4453 tree type;
4454 if (id_equal (suffix_id, "i"))
4456 if (TREE_CODE (value) == INTEGER_CST)
4457 type = integer_type_node;
4458 else
4459 type = double_type_node;
4461 else if (id_equal (suffix_id, "if"))
4462 type = float_type_node;
4463 else /* if (id_equal (suffix_id, "il")) */
4464 type = long_double_type_node;
4466 value = build_complex (build_complex_type (type),
4467 fold_convert (type, integer_zero_node),
4468 fold_convert (type, value));
4471 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4472 /* Avoid repeated diagnostics. */
4473 token->u.value = value;
4474 return value;
4477 /* Parse a user-defined string constant. Returns a call to a user-defined
4478 literal operator taking a character pointer and the length of the string
4479 as arguments. */
4481 static tree
4482 cp_parser_userdef_string_literal (tree literal)
4484 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4485 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4486 tree value = USERDEF_LITERAL_VALUE (literal);
4487 int len = TREE_STRING_LENGTH (value)
4488 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4489 tree decl, result;
4490 vec<tree, va_gc> *args;
4492 /* Build up a call to the user-defined operator. */
4493 /* Lookup the name we got back from the id-expression. */
4494 args = make_tree_vector ();
4495 vec_safe_push (args, value);
4496 vec_safe_push (args, build_int_cst (size_type_node, len));
4497 decl = lookup_literal_operator (name, args);
4499 if (decl && decl != error_mark_node)
4501 result = finish_call_expr (decl, &args, false, true,
4502 tf_warning_or_error);
4503 release_tree_vector (args);
4504 return result;
4506 release_tree_vector (args);
4508 /* Look for a template function with typename parameter CharT
4509 and parameter pack CharT... Call the function with
4510 template parameter characters representing the string. */
4511 args = make_tree_vector ();
4512 decl = lookup_literal_operator (name, args);
4513 if (decl && decl != error_mark_node)
4515 tree tmpl_args = make_string_pack (value);
4516 decl = lookup_template_function (decl, tmpl_args);
4517 result = finish_call_expr (decl, &args, false, true,
4518 tf_warning_or_error);
4519 release_tree_vector (args);
4520 return result;
4522 release_tree_vector (args);
4524 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4525 name, TREE_TYPE (value), size_type_node);
4526 return error_mark_node;
4530 /* Basic concepts [gram.basic] */
4532 /* Parse a translation-unit.
4534 translation-unit:
4535 declaration-seq [opt]
4537 Returns TRUE if all went well. */
4539 static bool
4540 cp_parser_translation_unit (cp_parser* parser)
4542 /* The address of the first non-permanent object on the declarator
4543 obstack. */
4544 static void *declarator_obstack_base;
4546 bool success;
4548 /* Create the declarator obstack, if necessary. */
4549 if (!cp_error_declarator)
4551 gcc_obstack_init (&declarator_obstack);
4552 /* Create the error declarator. */
4553 cp_error_declarator = make_declarator (cdk_error);
4554 /* Create the empty parameter list. */
4555 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4556 UNKNOWN_LOCATION);
4557 /* Remember where the base of the declarator obstack lies. */
4558 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4561 cp_parser_declaration_seq_opt (parser);
4563 /* If there are no tokens left then all went well. */
4564 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4566 /* Get rid of the token array; we don't need it any more. */
4567 cp_lexer_destroy (parser->lexer);
4568 parser->lexer = NULL;
4570 /* This file might have been a context that's implicitly extern
4571 "C". If so, pop the lang context. (Only relevant for PCH.) */
4572 if (parser->implicit_extern_c)
4574 pop_lang_context ();
4575 parser->implicit_extern_c = false;
4578 /* Finish up. */
4579 finish_translation_unit ();
4581 success = true;
4583 else
4585 cp_parser_error (parser, "expected declaration");
4586 success = false;
4589 /* Make sure the declarator obstack was fully cleaned up. */
4590 gcc_assert (obstack_next_free (&declarator_obstack)
4591 == declarator_obstack_base);
4593 /* All went well. */
4594 return success;
4597 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4598 decltype context. */
4600 static inline tsubst_flags_t
4601 complain_flags (bool decltype_p)
4603 tsubst_flags_t complain = tf_warning_or_error;
4604 if (decltype_p)
4605 complain |= tf_decltype;
4606 return complain;
4609 /* We're about to parse a collection of statements. If we're currently
4610 parsing tentatively, set up a firewall so that any nested
4611 cp_parser_commit_to_tentative_parse won't affect the current context. */
4613 static cp_token_position
4614 cp_parser_start_tentative_firewall (cp_parser *parser)
4616 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4617 return 0;
4619 cp_parser_parse_tentatively (parser);
4620 cp_parser_commit_to_topmost_tentative_parse (parser);
4621 return cp_lexer_token_position (parser->lexer, false);
4624 /* We've finished parsing the collection of statements. Wrap up the
4625 firewall and replace the relevant tokens with the parsed form. */
4627 static void
4628 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4629 tree expr)
4631 if (!start)
4632 return;
4634 /* Finish the firewall level. */
4635 cp_parser_parse_definitely (parser);
4636 /* And remember the result of the parse for when we try again. */
4637 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4638 token->type = CPP_PREPARSED_EXPR;
4639 token->u.value = expr;
4640 token->keyword = RID_MAX;
4641 cp_lexer_purge_tokens_after (parser->lexer, start);
4644 /* Like the above functions, but let the user modify the tokens. Used by
4645 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4646 later parses, so it makes sense to localize the effects of
4647 cp_parser_commit_to_tentative_parse. */
4649 struct tentative_firewall
4651 cp_parser *parser;
4652 bool set;
4654 tentative_firewall (cp_parser *p): parser(p)
4656 /* If we're currently parsing tentatively, start a committed level as a
4657 firewall and then an inner tentative parse. */
4658 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4660 cp_parser_parse_tentatively (parser);
4661 cp_parser_commit_to_topmost_tentative_parse (parser);
4662 cp_parser_parse_tentatively (parser);
4666 ~tentative_firewall()
4668 if (set)
4670 /* Finish the inner tentative parse and the firewall, propagating any
4671 uncommitted error state to the outer tentative parse. */
4672 bool err = cp_parser_error_occurred (parser);
4673 cp_parser_parse_definitely (parser);
4674 cp_parser_parse_definitely (parser);
4675 if (err)
4676 cp_parser_simulate_error (parser);
4681 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4682 This class is for tracking such a matching pair of symbols.
4683 In particular, it tracks the location of the first token,
4684 so that if the second token is missing, we can highlight the
4685 location of the first token when notifying the user about the
4686 problem. */
4688 template <typename traits_t>
4689 class token_pair
4691 public:
4692 /* token_pair's ctor. */
4693 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4695 /* If the next token is the opening symbol for this pair, consume it and
4696 return true.
4697 Otherwise, issue an error and return false.
4698 In either case, record the location of the opening token. */
4700 bool require_open (cp_parser *parser)
4702 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4703 return cp_parser_require (parser, traits_t::open_token_type,
4704 traits_t::required_token_open);
4707 /* Consume the next token from PARSER, recording its location as
4708 that of the opening token within the pair. */
4710 cp_token * consume_open (cp_parser *parser)
4712 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4713 gcc_assert (tok->type == traits_t::open_token_type);
4714 m_open_loc = tok->location;
4715 return tok;
4718 /* If the next token is the closing symbol for this pair, consume it
4719 and return it.
4720 Otherwise, issue an error, highlighting the location of the
4721 corresponding opening token, and return NULL. */
4723 cp_token *require_close (cp_parser *parser) const
4725 return cp_parser_require (parser, traits_t::close_token_type,
4726 traits_t::required_token_close,
4727 m_open_loc);
4730 private:
4731 location_t m_open_loc;
4734 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4736 struct matching_paren_traits
4738 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4739 static const enum required_token required_token_open = RT_OPEN_PAREN;
4740 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4741 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4744 /* "matching_parens" is a token_pair<T> class for tracking matching
4745 pairs of parentheses. */
4747 typedef token_pair<matching_paren_traits> matching_parens;
4749 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4751 struct matching_brace_traits
4753 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4754 static const enum required_token required_token_open = RT_OPEN_BRACE;
4755 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4756 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4759 /* "matching_braces" is a token_pair<T> class for tracking matching
4760 pairs of braces. */
4762 typedef token_pair<matching_brace_traits> matching_braces;
4765 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4766 enclosing parentheses. */
4768 static cp_expr
4769 cp_parser_statement_expr (cp_parser *parser)
4771 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4773 /* Consume the '('. */
4774 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4775 matching_parens parens;
4776 parens.consume_open (parser);
4777 /* Start the statement-expression. */
4778 tree expr = begin_stmt_expr ();
4779 /* Parse the compound-statement. */
4780 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4781 /* Finish up. */
4782 expr = finish_stmt_expr (expr, false);
4783 /* Consume the ')'. */
4784 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4785 if (!parens.require_close (parser))
4786 cp_parser_skip_to_end_of_statement (parser);
4788 cp_parser_end_tentative_firewall (parser, start, expr);
4789 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4790 return cp_expr (expr, combined_loc);
4793 /* Expressions [gram.expr] */
4795 /* Parse a fold-operator.
4797 fold-operator:
4798 - * / % ^ & | = < > << >>
4799 = -= *= /= %= ^= &= |= <<= >>=
4800 == != <= >= && || , .* ->*
4802 This returns the tree code corresponding to the matched operator
4803 as an int. When the current token matches a compound assignment
4804 opertor, the resulting tree code is the negative value of the
4805 non-assignment operator. */
4807 static int
4808 cp_parser_fold_operator (cp_token *token)
4810 switch (token->type)
4812 case CPP_PLUS: return PLUS_EXPR;
4813 case CPP_MINUS: return MINUS_EXPR;
4814 case CPP_MULT: return MULT_EXPR;
4815 case CPP_DIV: return TRUNC_DIV_EXPR;
4816 case CPP_MOD: return TRUNC_MOD_EXPR;
4817 case CPP_XOR: return BIT_XOR_EXPR;
4818 case CPP_AND: return BIT_AND_EXPR;
4819 case CPP_OR: return BIT_IOR_EXPR;
4820 case CPP_LSHIFT: return LSHIFT_EXPR;
4821 case CPP_RSHIFT: return RSHIFT_EXPR;
4823 case CPP_EQ: return -NOP_EXPR;
4824 case CPP_PLUS_EQ: return -PLUS_EXPR;
4825 case CPP_MINUS_EQ: return -MINUS_EXPR;
4826 case CPP_MULT_EQ: return -MULT_EXPR;
4827 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4828 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4829 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4830 case CPP_AND_EQ: return -BIT_AND_EXPR;
4831 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4832 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4833 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4835 case CPP_EQ_EQ: return EQ_EXPR;
4836 case CPP_NOT_EQ: return NE_EXPR;
4837 case CPP_LESS: return LT_EXPR;
4838 case CPP_GREATER: return GT_EXPR;
4839 case CPP_LESS_EQ: return LE_EXPR;
4840 case CPP_GREATER_EQ: return GE_EXPR;
4842 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4843 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4845 case CPP_COMMA: return COMPOUND_EXPR;
4847 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4848 case CPP_DEREF_STAR: return MEMBER_REF;
4850 default: return ERROR_MARK;
4854 /* Returns true if CODE indicates a binary expression, which is not allowed in
4855 the LHS of a fold-expression. More codes will need to be added to use this
4856 function in other contexts. */
4858 static bool
4859 is_binary_op (tree_code code)
4861 switch (code)
4863 case PLUS_EXPR:
4864 case POINTER_PLUS_EXPR:
4865 case MINUS_EXPR:
4866 case MULT_EXPR:
4867 case TRUNC_DIV_EXPR:
4868 case TRUNC_MOD_EXPR:
4869 case BIT_XOR_EXPR:
4870 case BIT_AND_EXPR:
4871 case BIT_IOR_EXPR:
4872 case LSHIFT_EXPR:
4873 case RSHIFT_EXPR:
4875 case MODOP_EXPR:
4877 case EQ_EXPR:
4878 case NE_EXPR:
4879 case LE_EXPR:
4880 case GE_EXPR:
4881 case LT_EXPR:
4882 case GT_EXPR:
4884 case TRUTH_ANDIF_EXPR:
4885 case TRUTH_ORIF_EXPR:
4887 case COMPOUND_EXPR:
4889 case DOTSTAR_EXPR:
4890 case MEMBER_REF:
4891 return true;
4893 default:
4894 return false;
4898 /* If the next token is a suitable fold operator, consume it and return as
4899 the function above. */
4901 static int
4902 cp_parser_fold_operator (cp_parser *parser)
4904 cp_token* token = cp_lexer_peek_token (parser->lexer);
4905 int code = cp_parser_fold_operator (token);
4906 if (code != ERROR_MARK)
4907 cp_lexer_consume_token (parser->lexer);
4908 return code;
4911 /* Parse a fold-expression.
4913 fold-expression:
4914 ( ... folding-operator cast-expression)
4915 ( cast-expression folding-operator ... )
4916 ( cast-expression folding operator ... folding-operator cast-expression)
4918 Note that the '(' and ')' are matched in primary expression. */
4920 static cp_expr
4921 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4923 cp_id_kind pidk;
4925 // Left fold.
4926 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4928 cp_lexer_consume_token (parser->lexer);
4929 int op = cp_parser_fold_operator (parser);
4930 if (op == ERROR_MARK)
4932 cp_parser_error (parser, "expected binary operator");
4933 return error_mark_node;
4936 tree expr = cp_parser_cast_expression (parser, false, false,
4937 false, &pidk);
4938 if (expr == error_mark_node)
4939 return error_mark_node;
4940 return finish_left_unary_fold_expr (expr, op);
4943 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4944 int op = cp_parser_fold_operator (parser);
4945 if (op == ERROR_MARK)
4947 cp_parser_error (parser, "expected binary operator");
4948 return error_mark_node;
4951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4953 cp_parser_error (parser, "expected ...");
4954 return error_mark_node;
4956 cp_lexer_consume_token (parser->lexer);
4958 /* The operands of a fold-expression are cast-expressions, so binary or
4959 conditional expressions are not allowed. We check this here to avoid
4960 tentative parsing. */
4961 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4962 /* OK, the expression was parenthesized. */;
4963 else if (is_binary_op (TREE_CODE (expr1)))
4964 error_at (location_of (expr1),
4965 "binary expression in operand of fold-expression");
4966 else if (TREE_CODE (expr1) == COND_EXPR)
4967 error_at (location_of (expr1),
4968 "conditional expression in operand of fold-expression");
4970 // Right fold.
4971 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4972 return finish_right_unary_fold_expr (expr1, op);
4974 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4976 cp_parser_error (parser, "mismatched operator in fold-expression");
4977 return error_mark_node;
4979 cp_lexer_consume_token (parser->lexer);
4981 // Binary left or right fold.
4982 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4983 if (expr2 == error_mark_node)
4984 return error_mark_node;
4985 return finish_binary_fold_expr (expr1, expr2, op);
4988 /* Parse a primary-expression.
4990 primary-expression:
4991 literal
4992 this
4993 ( expression )
4994 id-expression
4995 lambda-expression (C++11)
4997 GNU Extensions:
4999 primary-expression:
5000 ( compound-statement )
5001 __builtin_va_arg ( assignment-expression , type-id )
5002 __builtin_offsetof ( type-id , offsetof-expression )
5004 C++ Extensions:
5005 __has_nothrow_assign ( type-id )
5006 __has_nothrow_constructor ( type-id )
5007 __has_nothrow_copy ( type-id )
5008 __has_trivial_assign ( type-id )
5009 __has_trivial_constructor ( type-id )
5010 __has_trivial_copy ( type-id )
5011 __has_trivial_destructor ( type-id )
5012 __has_virtual_destructor ( type-id )
5013 __is_abstract ( type-id )
5014 __is_base_of ( type-id , type-id )
5015 __is_class ( type-id )
5016 __is_empty ( type-id )
5017 __is_enum ( type-id )
5018 __is_final ( type-id )
5019 __is_literal_type ( type-id )
5020 __is_pod ( type-id )
5021 __is_polymorphic ( type-id )
5022 __is_std_layout ( type-id )
5023 __is_trivial ( type-id )
5024 __is_union ( type-id )
5026 Objective-C++ Extension:
5028 primary-expression:
5029 objc-expression
5031 literal:
5032 __null
5034 ADDRESS_P is true iff this expression was immediately preceded by
5035 "&" and therefore might denote a pointer-to-member. CAST_P is true
5036 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5037 true iff this expression is a template argument.
5039 Returns a representation of the expression. Upon return, *IDK
5040 indicates what kind of id-expression (if any) was present. */
5042 static cp_expr
5043 cp_parser_primary_expression (cp_parser *parser,
5044 bool address_p,
5045 bool cast_p,
5046 bool template_arg_p,
5047 bool decltype_p,
5048 cp_id_kind *idk)
5050 cp_token *token = NULL;
5052 /* Assume the primary expression is not an id-expression. */
5053 *idk = CP_ID_KIND_NONE;
5055 /* Peek at the next token. */
5056 token = cp_lexer_peek_token (parser->lexer);
5057 switch ((int) token->type)
5059 /* literal:
5060 integer-literal
5061 character-literal
5062 floating-literal
5063 string-literal
5064 boolean-literal
5065 pointer-literal
5066 user-defined-literal */
5067 case CPP_CHAR:
5068 case CPP_CHAR16:
5069 case CPP_CHAR32:
5070 case CPP_WCHAR:
5071 case CPP_UTF8CHAR:
5072 case CPP_NUMBER:
5073 case CPP_PREPARSED_EXPR:
5074 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5075 return cp_parser_userdef_numeric_literal (parser);
5076 token = cp_lexer_consume_token (parser->lexer);
5077 if (TREE_CODE (token->u.value) == FIXED_CST)
5079 error_at (token->location,
5080 "fixed-point types not supported in C++");
5081 return error_mark_node;
5083 /* Floating-point literals are only allowed in an integral
5084 constant expression if they are cast to an integral or
5085 enumeration type. */
5086 if (TREE_CODE (token->u.value) == REAL_CST
5087 && parser->integral_constant_expression_p
5088 && pedantic)
5090 /* CAST_P will be set even in invalid code like "int(2.7 +
5091 ...)". Therefore, we have to check that the next token
5092 is sure to end the cast. */
5093 if (cast_p)
5095 cp_token *next_token;
5097 next_token = cp_lexer_peek_token (parser->lexer);
5098 if (/* The comma at the end of an
5099 enumerator-definition. */
5100 next_token->type != CPP_COMMA
5101 /* The curly brace at the end of an enum-specifier. */
5102 && next_token->type != CPP_CLOSE_BRACE
5103 /* The end of a statement. */
5104 && next_token->type != CPP_SEMICOLON
5105 /* The end of the cast-expression. */
5106 && next_token->type != CPP_CLOSE_PAREN
5107 /* The end of an array bound. */
5108 && next_token->type != CPP_CLOSE_SQUARE
5109 /* The closing ">" in a template-argument-list. */
5110 && (next_token->type != CPP_GREATER
5111 || parser->greater_than_is_operator_p)
5112 /* C++0x only: A ">>" treated like two ">" tokens,
5113 in a template-argument-list. */
5114 && (next_token->type != CPP_RSHIFT
5115 || (cxx_dialect == cxx98)
5116 || parser->greater_than_is_operator_p))
5117 cast_p = false;
5120 /* If we are within a cast, then the constraint that the
5121 cast is to an integral or enumeration type will be
5122 checked at that point. If we are not within a cast, then
5123 this code is invalid. */
5124 if (!cast_p)
5125 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5127 return cp_expr (token->u.value, token->location);
5129 case CPP_CHAR_USERDEF:
5130 case CPP_CHAR16_USERDEF:
5131 case CPP_CHAR32_USERDEF:
5132 case CPP_WCHAR_USERDEF:
5133 case CPP_UTF8CHAR_USERDEF:
5134 return cp_parser_userdef_char_literal (parser);
5136 case CPP_STRING:
5137 case CPP_STRING16:
5138 case CPP_STRING32:
5139 case CPP_WSTRING:
5140 case CPP_UTF8STRING:
5141 case CPP_STRING_USERDEF:
5142 case CPP_STRING16_USERDEF:
5143 case CPP_STRING32_USERDEF:
5144 case CPP_WSTRING_USERDEF:
5145 case CPP_UTF8STRING_USERDEF:
5146 /* ??? Should wide strings be allowed when parser->translate_strings_p
5147 is false (i.e. in attributes)? If not, we can kill the third
5148 argument to cp_parser_string_literal. */
5149 return cp_parser_string_literal (parser,
5150 parser->translate_strings_p,
5151 true);
5153 case CPP_OPEN_PAREN:
5154 /* If we see `( { ' then we are looking at the beginning of
5155 a GNU statement-expression. */
5156 if (cp_parser_allow_gnu_extensions_p (parser)
5157 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5159 /* Statement-expressions are not allowed by the standard. */
5160 pedwarn (token->location, OPT_Wpedantic,
5161 "ISO C++ forbids braced-groups within expressions");
5163 /* And they're not allowed outside of a function-body; you
5164 cannot, for example, write:
5166 int i = ({ int j = 3; j + 1; });
5168 at class or namespace scope. */
5169 if (!parser->in_function_body
5170 || parser->in_template_argument_list_p)
5172 error_at (token->location,
5173 "statement-expressions are not allowed outside "
5174 "functions nor in template-argument lists");
5175 cp_parser_skip_to_end_of_block_or_statement (parser);
5176 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5177 cp_lexer_consume_token (parser->lexer);
5178 return error_mark_node;
5180 else
5181 return cp_parser_statement_expr (parser);
5183 /* Otherwise it's a normal parenthesized expression. */
5185 cp_expr expr;
5186 bool saved_greater_than_is_operator_p;
5188 location_t open_paren_loc = token->location;
5190 /* Consume the `('. */
5191 matching_parens parens;
5192 parens.consume_open (parser);
5193 /* Within a parenthesized expression, a `>' token is always
5194 the greater-than operator. */
5195 saved_greater_than_is_operator_p
5196 = parser->greater_than_is_operator_p;
5197 parser->greater_than_is_operator_p = true;
5199 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5200 /* Left fold expression. */
5201 expr = NULL_TREE;
5202 else
5203 /* Parse the parenthesized expression. */
5204 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5206 token = cp_lexer_peek_token (parser->lexer);
5207 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5209 expr = cp_parser_fold_expression (parser, expr);
5210 if (expr != error_mark_node
5211 && cxx_dialect < cxx17
5212 && !in_system_header_at (input_location))
5213 pedwarn (input_location, 0, "fold-expressions only available "
5214 "with -std=c++17 or -std=gnu++17");
5216 else
5217 /* Let the front end know that this expression was
5218 enclosed in parentheses. This matters in case, for
5219 example, the expression is of the form `A::B', since
5220 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5221 not. */
5222 expr = finish_parenthesized_expr (expr);
5224 /* DR 705: Wrapping an unqualified name in parentheses
5225 suppresses arg-dependent lookup. We want to pass back
5226 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5227 (c++/37862), but none of the others. */
5228 if (*idk != CP_ID_KIND_QUALIFIED)
5229 *idk = CP_ID_KIND_NONE;
5231 /* The `>' token might be the end of a template-id or
5232 template-parameter-list now. */
5233 parser->greater_than_is_operator_p
5234 = saved_greater_than_is_operator_p;
5236 /* Consume the `)'. */
5237 token = cp_lexer_peek_token (parser->lexer);
5238 location_t close_paren_loc = token->location;
5239 expr.set_range (open_paren_loc, close_paren_loc);
5240 if (!parens.require_close (parser)
5241 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5242 cp_parser_skip_to_end_of_statement (parser);
5244 return expr;
5247 case CPP_OPEN_SQUARE:
5249 if (c_dialect_objc ())
5251 /* We might have an Objective-C++ message. */
5252 cp_parser_parse_tentatively (parser);
5253 tree msg = cp_parser_objc_message_expression (parser);
5254 /* If that works out, we're done ... */
5255 if (cp_parser_parse_definitely (parser))
5256 return msg;
5257 /* ... else, fall though to see if it's a lambda. */
5259 cp_expr lam = cp_parser_lambda_expression (parser);
5260 /* Don't warn about a failed tentative parse. */
5261 if (cp_parser_error_occurred (parser))
5262 return error_mark_node;
5263 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5264 return lam;
5267 case CPP_OBJC_STRING:
5268 if (c_dialect_objc ())
5269 /* We have an Objective-C++ string literal. */
5270 return cp_parser_objc_expression (parser);
5271 cp_parser_error (parser, "expected primary-expression");
5272 return error_mark_node;
5274 case CPP_KEYWORD:
5275 switch (token->keyword)
5277 /* These two are the boolean literals. */
5278 case RID_TRUE:
5279 cp_lexer_consume_token (parser->lexer);
5280 return cp_expr (boolean_true_node, token->location);
5281 case RID_FALSE:
5282 cp_lexer_consume_token (parser->lexer);
5283 return cp_expr (boolean_false_node, token->location);
5285 /* The `__null' literal. */
5286 case RID_NULL:
5287 cp_lexer_consume_token (parser->lexer);
5288 return cp_expr (null_node, token->location);
5290 /* The `nullptr' literal. */
5291 case RID_NULLPTR:
5292 cp_lexer_consume_token (parser->lexer);
5293 return cp_expr (nullptr_node, token->location);
5295 /* Recognize the `this' keyword. */
5296 case RID_THIS:
5297 cp_lexer_consume_token (parser->lexer);
5298 if (parser->local_variables_forbidden_p)
5300 error_at (token->location,
5301 "%<this%> may not be used in this context");
5302 return error_mark_node;
5304 /* Pointers cannot appear in constant-expressions. */
5305 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5306 return error_mark_node;
5307 return cp_expr (finish_this_expr (), token->location);
5309 /* The `operator' keyword can be the beginning of an
5310 id-expression. */
5311 case RID_OPERATOR:
5312 goto id_expression;
5314 case RID_FUNCTION_NAME:
5315 case RID_PRETTY_FUNCTION_NAME:
5316 case RID_C99_FUNCTION_NAME:
5318 non_integral_constant name;
5320 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5321 __func__ are the names of variables -- but they are
5322 treated specially. Therefore, they are handled here,
5323 rather than relying on the generic id-expression logic
5324 below. Grammatically, these names are id-expressions.
5326 Consume the token. */
5327 token = cp_lexer_consume_token (parser->lexer);
5329 switch (token->keyword)
5331 case RID_FUNCTION_NAME:
5332 name = NIC_FUNC_NAME;
5333 break;
5334 case RID_PRETTY_FUNCTION_NAME:
5335 name = NIC_PRETTY_FUNC;
5336 break;
5337 case RID_C99_FUNCTION_NAME:
5338 name = NIC_C99_FUNC;
5339 break;
5340 default:
5341 gcc_unreachable ();
5344 if (cp_parser_non_integral_constant_expression (parser, name))
5345 return error_mark_node;
5347 /* Look up the name. */
5348 return finish_fname (token->u.value);
5351 case RID_VA_ARG:
5353 tree expression;
5354 tree type;
5355 source_location type_location;
5356 location_t start_loc
5357 = cp_lexer_peek_token (parser->lexer)->location;
5358 /* The `__builtin_va_arg' construct is used to handle
5359 `va_arg'. Consume the `__builtin_va_arg' token. */
5360 cp_lexer_consume_token (parser->lexer);
5361 /* Look for the opening `('. */
5362 matching_parens parens;
5363 parens.require_open (parser);
5364 /* Now, parse the assignment-expression. */
5365 expression = cp_parser_assignment_expression (parser);
5366 /* Look for the `,'. */
5367 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5368 type_location = cp_lexer_peek_token (parser->lexer)->location;
5369 /* Parse the type-id. */
5371 type_id_in_expr_sentinel s (parser);
5372 type = cp_parser_type_id (parser);
5374 /* Look for the closing `)'. */
5375 location_t finish_loc
5376 = cp_lexer_peek_token (parser->lexer)->location;
5377 parens.require_close (parser);
5378 /* Using `va_arg' in a constant-expression is not
5379 allowed. */
5380 if (cp_parser_non_integral_constant_expression (parser,
5381 NIC_VA_ARG))
5382 return error_mark_node;
5383 /* Construct a location of the form:
5384 __builtin_va_arg (v, int)
5385 ~~~~~~~~~~~~~~~~~~~~~^~~~
5386 with the caret at the type, ranging from the start of the
5387 "__builtin_va_arg" token to the close paren. */
5388 location_t combined_loc
5389 = make_location (type_location, start_loc, finish_loc);
5390 return build_x_va_arg (combined_loc, expression, type);
5393 case RID_OFFSETOF:
5394 return cp_parser_builtin_offsetof (parser);
5396 case RID_HAS_NOTHROW_ASSIGN:
5397 case RID_HAS_NOTHROW_CONSTRUCTOR:
5398 case RID_HAS_NOTHROW_COPY:
5399 case RID_HAS_TRIVIAL_ASSIGN:
5400 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5401 case RID_HAS_TRIVIAL_COPY:
5402 case RID_HAS_TRIVIAL_DESTRUCTOR:
5403 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5404 case RID_HAS_VIRTUAL_DESTRUCTOR:
5405 case RID_IS_ABSTRACT:
5406 case RID_IS_AGGREGATE:
5407 case RID_IS_BASE_OF:
5408 case RID_IS_CLASS:
5409 case RID_IS_EMPTY:
5410 case RID_IS_ENUM:
5411 case RID_IS_FINAL:
5412 case RID_IS_LITERAL_TYPE:
5413 case RID_IS_POD:
5414 case RID_IS_POLYMORPHIC:
5415 case RID_IS_SAME_AS:
5416 case RID_IS_STD_LAYOUT:
5417 case RID_IS_TRIVIAL:
5418 case RID_IS_TRIVIALLY_ASSIGNABLE:
5419 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5420 case RID_IS_TRIVIALLY_COPYABLE:
5421 case RID_IS_UNION:
5422 case RID_IS_ASSIGNABLE:
5423 case RID_IS_CONSTRUCTIBLE:
5424 return cp_parser_trait_expr (parser, token->keyword);
5426 // C++ concepts
5427 case RID_REQUIRES:
5428 return cp_parser_requires_expression (parser);
5430 /* Objective-C++ expressions. */
5431 case RID_AT_ENCODE:
5432 case RID_AT_PROTOCOL:
5433 case RID_AT_SELECTOR:
5434 return cp_parser_objc_expression (parser);
5436 case RID_TEMPLATE:
5437 if (parser->in_function_body
5438 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5439 == CPP_LESS))
5441 error_at (token->location,
5442 "a template declaration cannot appear at block scope");
5443 cp_parser_skip_to_end_of_block_or_statement (parser);
5444 return error_mark_node;
5446 /* FALLTHRU */
5447 default:
5448 cp_parser_error (parser, "expected primary-expression");
5449 return error_mark_node;
5452 /* An id-expression can start with either an identifier, a
5453 `::' as the beginning of a qualified-id, or the "operator"
5454 keyword. */
5455 case CPP_NAME:
5456 case CPP_SCOPE:
5457 case CPP_TEMPLATE_ID:
5458 case CPP_NESTED_NAME_SPECIFIER:
5460 id_expression:
5461 cp_expr id_expression;
5462 cp_expr decl;
5463 const char *error_msg;
5464 bool template_p;
5465 bool done;
5466 cp_token *id_expr_token;
5468 /* Parse the id-expression. */
5469 id_expression
5470 = cp_parser_id_expression (parser,
5471 /*template_keyword_p=*/false,
5472 /*check_dependency_p=*/true,
5473 &template_p,
5474 /*declarator_p=*/false,
5475 /*optional_p=*/false);
5476 if (id_expression == error_mark_node)
5477 return error_mark_node;
5478 id_expr_token = token;
5479 token = cp_lexer_peek_token (parser->lexer);
5480 done = (token->type != CPP_OPEN_SQUARE
5481 && token->type != CPP_OPEN_PAREN
5482 && token->type != CPP_DOT
5483 && token->type != CPP_DEREF
5484 && token->type != CPP_PLUS_PLUS
5485 && token->type != CPP_MINUS_MINUS);
5486 /* If we have a template-id, then no further lookup is
5487 required. If the template-id was for a template-class, we
5488 will sometimes have a TYPE_DECL at this point. */
5489 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5490 || TREE_CODE (id_expression) == TYPE_DECL)
5491 decl = id_expression;
5492 /* Look up the name. */
5493 else
5495 tree ambiguous_decls;
5497 /* If we already know that this lookup is ambiguous, then
5498 we've already issued an error message; there's no reason
5499 to check again. */
5500 if (id_expr_token->type == CPP_NAME
5501 && id_expr_token->error_reported)
5503 cp_parser_simulate_error (parser);
5504 return error_mark_node;
5507 decl = cp_parser_lookup_name (parser, id_expression,
5508 none_type,
5509 template_p,
5510 /*is_namespace=*/false,
5511 /*check_dependency=*/true,
5512 &ambiguous_decls,
5513 id_expr_token->location);
5514 /* If the lookup was ambiguous, an error will already have
5515 been issued. */
5516 if (ambiguous_decls)
5517 return error_mark_node;
5519 /* In Objective-C++, we may have an Objective-C 2.0
5520 dot-syntax for classes here. */
5521 if (c_dialect_objc ()
5522 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5523 && TREE_CODE (decl) == TYPE_DECL
5524 && objc_is_class_name (decl))
5526 tree component;
5527 cp_lexer_consume_token (parser->lexer);
5528 component = cp_parser_identifier (parser);
5529 if (component == error_mark_node)
5530 return error_mark_node;
5532 tree result = objc_build_class_component_ref (id_expression,
5533 component);
5534 /* Build a location of the form:
5535 expr.component
5536 ~~~~~^~~~~~~~~
5537 with caret at the start of the component name (at
5538 input_location), ranging from the start of the id_expression
5539 to the end of the component name. */
5540 location_t combined_loc
5541 = make_location (input_location, id_expression.get_start (),
5542 get_finish (input_location));
5543 protected_set_expr_location (result, combined_loc);
5544 return result;
5547 /* In Objective-C++, an instance variable (ivar) may be preferred
5548 to whatever cp_parser_lookup_name() found.
5549 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5550 rest of c-family, we have to do a little extra work to preserve
5551 any location information in cp_expr "decl". Given that
5552 objc_lookup_ivar is implemented in "c-family" and "objc", we
5553 have a trip through the pure "tree" type, rather than cp_expr.
5554 Naively copying it back to "decl" would implicitly give the
5555 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5556 store an EXPR_LOCATION. Hence we only update "decl" (and
5557 hence its location_t) if we get back a different tree node. */
5558 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5559 id_expression);
5560 if (decl_tree != decl.get_value ())
5561 decl = cp_expr (decl_tree);
5563 /* If name lookup gives us a SCOPE_REF, then the
5564 qualifying scope was dependent. */
5565 if (TREE_CODE (decl) == SCOPE_REF)
5567 /* At this point, we do not know if DECL is a valid
5568 integral constant expression. We assume that it is
5569 in fact such an expression, so that code like:
5571 template <int N> struct A {
5572 int a[B<N>::i];
5575 is accepted. At template-instantiation time, we
5576 will check that B<N>::i is actually a constant. */
5577 return decl;
5579 /* Check to see if DECL is a local variable in a context
5580 where that is forbidden. */
5581 if (parser->local_variables_forbidden_p
5582 && local_variable_p (decl))
5584 /* It might be that we only found DECL because we are
5585 trying to be generous with pre-ISO scoping rules.
5586 For example, consider:
5588 int i;
5589 void g() {
5590 for (int i = 0; i < 10; ++i) {}
5591 extern void f(int j = i);
5594 Here, name look up will originally find the out
5595 of scope `i'. We need to issue a warning message,
5596 but then use the global `i'. */
5597 decl = check_for_out_of_scope_variable (decl);
5598 if (local_variable_p (decl))
5600 error_at (id_expr_token->location,
5601 "local variable %qD may not appear in this context",
5602 decl.get_value ());
5603 return error_mark_node;
5608 decl = (finish_id_expression
5609 (id_expression, decl, parser->scope,
5610 idk,
5611 parser->integral_constant_expression_p,
5612 parser->allow_non_integral_constant_expression_p,
5613 &parser->non_integral_constant_expression_p,
5614 template_p, done, address_p,
5615 template_arg_p,
5616 &error_msg,
5617 id_expression.get_location ()));
5618 if (error_msg)
5619 cp_parser_error (parser, error_msg);
5620 decl.set_location (id_expr_token->location);
5621 return decl;
5624 /* Anything else is an error. */
5625 default:
5626 cp_parser_error (parser, "expected primary-expression");
5627 return error_mark_node;
5631 static inline cp_expr
5632 cp_parser_primary_expression (cp_parser *parser,
5633 bool address_p,
5634 bool cast_p,
5635 bool template_arg_p,
5636 cp_id_kind *idk)
5638 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5639 /*decltype*/false, idk);
5642 /* Parse an id-expression.
5644 id-expression:
5645 unqualified-id
5646 qualified-id
5648 qualified-id:
5649 :: [opt] nested-name-specifier template [opt] unqualified-id
5650 :: identifier
5651 :: operator-function-id
5652 :: template-id
5654 Return a representation of the unqualified portion of the
5655 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5656 a `::' or nested-name-specifier.
5658 Often, if the id-expression was a qualified-id, the caller will
5659 want to make a SCOPE_REF to represent the qualified-id. This
5660 function does not do this in order to avoid wastefully creating
5661 SCOPE_REFs when they are not required.
5663 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5664 `template' keyword.
5666 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5667 uninstantiated templates.
5669 If *TEMPLATE_P is non-NULL, it is set to true iff the
5670 `template' keyword is used to explicitly indicate that the entity
5671 named is a template.
5673 If DECLARATOR_P is true, the id-expression is appearing as part of
5674 a declarator, rather than as part of an expression. */
5676 static cp_expr
5677 cp_parser_id_expression (cp_parser *parser,
5678 bool template_keyword_p,
5679 bool check_dependency_p,
5680 bool *template_p,
5681 bool declarator_p,
5682 bool optional_p)
5684 bool global_scope_p;
5685 bool nested_name_specifier_p;
5687 /* Assume the `template' keyword was not used. */
5688 if (template_p)
5689 *template_p = template_keyword_p;
5691 /* Look for the optional `::' operator. */
5692 global_scope_p
5693 = (!template_keyword_p
5694 && (cp_parser_global_scope_opt (parser,
5695 /*current_scope_valid_p=*/false)
5696 != NULL_TREE));
5698 /* Look for the optional nested-name-specifier. */
5699 nested_name_specifier_p
5700 = (cp_parser_nested_name_specifier_opt (parser,
5701 /*typename_keyword_p=*/false,
5702 check_dependency_p,
5703 /*type_p=*/false,
5704 declarator_p,
5705 template_keyword_p)
5706 != NULL_TREE);
5708 /* If there is a nested-name-specifier, then we are looking at
5709 the first qualified-id production. */
5710 if (nested_name_specifier_p)
5712 tree saved_scope;
5713 tree saved_object_scope;
5714 tree saved_qualifying_scope;
5715 cp_expr unqualified_id;
5716 bool is_template;
5718 /* See if the next token is the `template' keyword. */
5719 if (!template_p)
5720 template_p = &is_template;
5721 *template_p = cp_parser_optional_template_keyword (parser);
5722 /* Name lookup we do during the processing of the
5723 unqualified-id might obliterate SCOPE. */
5724 saved_scope = parser->scope;
5725 saved_object_scope = parser->object_scope;
5726 saved_qualifying_scope = parser->qualifying_scope;
5727 /* Process the final unqualified-id. */
5728 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5729 check_dependency_p,
5730 declarator_p,
5731 /*optional_p=*/false);
5732 /* Restore the SAVED_SCOPE for our caller. */
5733 parser->scope = saved_scope;
5734 parser->object_scope = saved_object_scope;
5735 parser->qualifying_scope = saved_qualifying_scope;
5737 return unqualified_id;
5739 /* Otherwise, if we are in global scope, then we are looking at one
5740 of the other qualified-id productions. */
5741 else if (global_scope_p)
5743 cp_token *token;
5744 tree id;
5746 /* Peek at the next token. */
5747 token = cp_lexer_peek_token (parser->lexer);
5749 /* If it's an identifier, and the next token is not a "<", then
5750 we can avoid the template-id case. This is an optimization
5751 for this common case. */
5752 if (token->type == CPP_NAME
5753 && !cp_parser_nth_token_starts_template_argument_list_p
5754 (parser, 2))
5755 return cp_parser_identifier (parser);
5757 cp_parser_parse_tentatively (parser);
5758 /* Try a template-id. */
5759 id = cp_parser_template_id (parser,
5760 /*template_keyword_p=*/false,
5761 /*check_dependency_p=*/true,
5762 none_type,
5763 declarator_p);
5764 /* If that worked, we're done. */
5765 if (cp_parser_parse_definitely (parser))
5766 return id;
5768 /* Peek at the next token. (Changes in the token buffer may
5769 have invalidated the pointer obtained above.) */
5770 token = cp_lexer_peek_token (parser->lexer);
5772 switch (token->type)
5774 case CPP_NAME:
5775 return cp_parser_identifier (parser);
5777 case CPP_KEYWORD:
5778 if (token->keyword == RID_OPERATOR)
5779 return cp_parser_operator_function_id (parser);
5780 /* Fall through. */
5782 default:
5783 cp_parser_error (parser, "expected id-expression");
5784 return error_mark_node;
5787 else
5788 return cp_parser_unqualified_id (parser, template_keyword_p,
5789 /*check_dependency_p=*/true,
5790 declarator_p,
5791 optional_p);
5794 /* Parse an unqualified-id.
5796 unqualified-id:
5797 identifier
5798 operator-function-id
5799 conversion-function-id
5800 ~ class-name
5801 template-id
5803 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5804 keyword, in a construct like `A::template ...'.
5806 Returns a representation of unqualified-id. For the `identifier'
5807 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5808 production a BIT_NOT_EXPR is returned; the operand of the
5809 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5810 other productions, see the documentation accompanying the
5811 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5812 names are looked up in uninstantiated templates. If DECLARATOR_P
5813 is true, the unqualified-id is appearing as part of a declarator,
5814 rather than as part of an expression. */
5816 static cp_expr
5817 cp_parser_unqualified_id (cp_parser* parser,
5818 bool template_keyword_p,
5819 bool check_dependency_p,
5820 bool declarator_p,
5821 bool optional_p)
5823 cp_token *token;
5825 /* Peek at the next token. */
5826 token = cp_lexer_peek_token (parser->lexer);
5828 switch ((int) token->type)
5830 case CPP_NAME:
5832 tree id;
5834 /* We don't know yet whether or not this will be a
5835 template-id. */
5836 cp_parser_parse_tentatively (parser);
5837 /* Try a template-id. */
5838 id = cp_parser_template_id (parser, template_keyword_p,
5839 check_dependency_p,
5840 none_type,
5841 declarator_p);
5842 /* If it worked, we're done. */
5843 if (cp_parser_parse_definitely (parser))
5844 return id;
5845 /* Otherwise, it's an ordinary identifier. */
5846 return cp_parser_identifier (parser);
5849 case CPP_TEMPLATE_ID:
5850 return cp_parser_template_id (parser, template_keyword_p,
5851 check_dependency_p,
5852 none_type,
5853 declarator_p);
5855 case CPP_COMPL:
5857 tree type_decl;
5858 tree qualifying_scope;
5859 tree object_scope;
5860 tree scope;
5861 bool done;
5863 /* Consume the `~' token. */
5864 cp_lexer_consume_token (parser->lexer);
5865 /* Parse the class-name. The standard, as written, seems to
5866 say that:
5868 template <typename T> struct S { ~S (); };
5869 template <typename T> S<T>::~S() {}
5871 is invalid, since `~' must be followed by a class-name, but
5872 `S<T>' is dependent, and so not known to be a class.
5873 That's not right; we need to look in uninstantiated
5874 templates. A further complication arises from:
5876 template <typename T> void f(T t) {
5877 t.T::~T();
5880 Here, it is not possible to look up `T' in the scope of `T'
5881 itself. We must look in both the current scope, and the
5882 scope of the containing complete expression.
5884 Yet another issue is:
5886 struct S {
5887 int S;
5888 ~S();
5891 S::~S() {}
5893 The standard does not seem to say that the `S' in `~S'
5894 should refer to the type `S' and not the data member
5895 `S::S'. */
5897 /* DR 244 says that we look up the name after the "~" in the
5898 same scope as we looked up the qualifying name. That idea
5899 isn't fully worked out; it's more complicated than that. */
5900 scope = parser->scope;
5901 object_scope = parser->object_scope;
5902 qualifying_scope = parser->qualifying_scope;
5904 /* Check for invalid scopes. */
5905 if (scope == error_mark_node)
5907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5908 cp_lexer_consume_token (parser->lexer);
5909 return error_mark_node;
5911 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5913 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5914 error_at (token->location,
5915 "scope %qT before %<~%> is not a class-name",
5916 scope);
5917 cp_parser_simulate_error (parser);
5918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5919 cp_lexer_consume_token (parser->lexer);
5920 return error_mark_node;
5922 gcc_assert (!scope || TYPE_P (scope));
5924 /* If the name is of the form "X::~X" it's OK even if X is a
5925 typedef. */
5926 token = cp_lexer_peek_token (parser->lexer);
5927 if (scope
5928 && token->type == CPP_NAME
5929 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5930 != CPP_LESS)
5931 && (token->u.value == TYPE_IDENTIFIER (scope)
5932 || (CLASS_TYPE_P (scope)
5933 && constructor_name_p (token->u.value, scope))))
5935 cp_lexer_consume_token (parser->lexer);
5936 return build_nt (BIT_NOT_EXPR, scope);
5939 /* ~auto means the destructor of whatever the object is. */
5940 if (cp_parser_is_keyword (token, RID_AUTO))
5942 if (cxx_dialect < cxx14)
5943 pedwarn (input_location, 0,
5944 "%<~auto%> only available with "
5945 "-std=c++14 or -std=gnu++14");
5946 cp_lexer_consume_token (parser->lexer);
5947 return build_nt (BIT_NOT_EXPR, make_auto ());
5950 /* If there was an explicit qualification (S::~T), first look
5951 in the scope given by the qualification (i.e., S).
5953 Note: in the calls to cp_parser_class_name below we pass
5954 typename_type so that lookup finds the injected-class-name
5955 rather than the constructor. */
5956 done = false;
5957 type_decl = NULL_TREE;
5958 if (scope)
5960 cp_parser_parse_tentatively (parser);
5961 type_decl = cp_parser_class_name (parser,
5962 /*typename_keyword_p=*/false,
5963 /*template_keyword_p=*/false,
5964 typename_type,
5965 /*check_dependency=*/false,
5966 /*class_head_p=*/false,
5967 declarator_p);
5968 if (cp_parser_parse_definitely (parser))
5969 done = true;
5971 /* In "N::S::~S", look in "N" as well. */
5972 if (!done && scope && qualifying_scope)
5974 cp_parser_parse_tentatively (parser);
5975 parser->scope = qualifying_scope;
5976 parser->object_scope = NULL_TREE;
5977 parser->qualifying_scope = NULL_TREE;
5978 type_decl
5979 = cp_parser_class_name (parser,
5980 /*typename_keyword_p=*/false,
5981 /*template_keyword_p=*/false,
5982 typename_type,
5983 /*check_dependency=*/false,
5984 /*class_head_p=*/false,
5985 declarator_p);
5986 if (cp_parser_parse_definitely (parser))
5987 done = true;
5989 /* In "p->S::~T", look in the scope given by "*p" as well. */
5990 else if (!done && object_scope)
5992 cp_parser_parse_tentatively (parser);
5993 parser->scope = object_scope;
5994 parser->object_scope = NULL_TREE;
5995 parser->qualifying_scope = NULL_TREE;
5996 type_decl
5997 = cp_parser_class_name (parser,
5998 /*typename_keyword_p=*/false,
5999 /*template_keyword_p=*/false,
6000 typename_type,
6001 /*check_dependency=*/false,
6002 /*class_head_p=*/false,
6003 declarator_p);
6004 if (cp_parser_parse_definitely (parser))
6005 done = true;
6007 /* Look in the surrounding context. */
6008 if (!done)
6010 parser->scope = NULL_TREE;
6011 parser->object_scope = NULL_TREE;
6012 parser->qualifying_scope = NULL_TREE;
6013 if (processing_template_decl)
6014 cp_parser_parse_tentatively (parser);
6015 type_decl
6016 = cp_parser_class_name (parser,
6017 /*typename_keyword_p=*/false,
6018 /*template_keyword_p=*/false,
6019 typename_type,
6020 /*check_dependency=*/false,
6021 /*class_head_p=*/false,
6022 declarator_p);
6023 if (processing_template_decl
6024 && ! cp_parser_parse_definitely (parser))
6026 /* We couldn't find a type with this name. If we're parsing
6027 tentatively, fail and try something else. */
6028 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6030 cp_parser_simulate_error (parser);
6031 return error_mark_node;
6033 /* Otherwise, accept it and check for a match at instantiation
6034 time. */
6035 type_decl = cp_parser_identifier (parser);
6036 if (type_decl != error_mark_node)
6037 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6038 return type_decl;
6041 /* If an error occurred, assume that the name of the
6042 destructor is the same as the name of the qualifying
6043 class. That allows us to keep parsing after running
6044 into ill-formed destructor names. */
6045 if (type_decl == error_mark_node && scope)
6046 return build_nt (BIT_NOT_EXPR, scope);
6047 else if (type_decl == error_mark_node)
6048 return error_mark_node;
6050 /* Check that destructor name and scope match. */
6051 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6054 error_at (token->location,
6055 "declaration of %<~%T%> as member of %qT",
6056 type_decl, scope);
6057 cp_parser_simulate_error (parser);
6058 return error_mark_node;
6061 /* [class.dtor]
6063 A typedef-name that names a class shall not be used as the
6064 identifier in the declarator for a destructor declaration. */
6065 if (declarator_p
6066 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6067 && !DECL_SELF_REFERENCE_P (type_decl)
6068 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6069 error_at (token->location,
6070 "typedef-name %qD used as destructor declarator",
6071 type_decl);
6073 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6076 case CPP_KEYWORD:
6077 if (token->keyword == RID_OPERATOR)
6079 cp_expr id;
6081 /* This could be a template-id, so we try that first. */
6082 cp_parser_parse_tentatively (parser);
6083 /* Try a template-id. */
6084 id = cp_parser_template_id (parser, template_keyword_p,
6085 /*check_dependency_p=*/true,
6086 none_type,
6087 declarator_p);
6088 /* If that worked, we're done. */
6089 if (cp_parser_parse_definitely (parser))
6090 return id;
6091 /* We still don't know whether we're looking at an
6092 operator-function-id or a conversion-function-id. */
6093 cp_parser_parse_tentatively (parser);
6094 /* Try an operator-function-id. */
6095 id = cp_parser_operator_function_id (parser);
6096 /* If that didn't work, try a conversion-function-id. */
6097 if (!cp_parser_parse_definitely (parser))
6098 id = cp_parser_conversion_function_id (parser);
6099 else if (UDLIT_OPER_P (id))
6101 /* 17.6.3.3.5 */
6102 const char *name = UDLIT_OP_SUFFIX (id);
6103 if (name[0] != '_' && !in_system_header_at (input_location)
6104 && declarator_p)
6105 warning (OPT_Wliteral_suffix,
6106 "literal operator suffixes not preceded by %<_%>"
6107 " are reserved for future standardization");
6110 return id;
6112 /* Fall through. */
6114 default:
6115 if (optional_p)
6116 return NULL_TREE;
6117 cp_parser_error (parser, "expected unqualified-id");
6118 return error_mark_node;
6122 /* Parse an (optional) nested-name-specifier.
6124 nested-name-specifier: [C++98]
6125 class-or-namespace-name :: nested-name-specifier [opt]
6126 class-or-namespace-name :: template nested-name-specifier [opt]
6128 nested-name-specifier: [C++0x]
6129 type-name ::
6130 namespace-name ::
6131 nested-name-specifier identifier ::
6132 nested-name-specifier template [opt] simple-template-id ::
6134 PARSER->SCOPE should be set appropriately before this function is
6135 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6136 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6137 in name lookups.
6139 Sets PARSER->SCOPE to the class (TYPE) or namespace
6140 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6141 it unchanged if there is no nested-name-specifier. Returns the new
6142 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6144 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6145 part of a declaration and/or decl-specifier. */
6147 static tree
6148 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6149 bool typename_keyword_p,
6150 bool check_dependency_p,
6151 bool type_p,
6152 bool is_declaration,
6153 bool template_keyword_p /* = false */)
6155 bool success = false;
6156 cp_token_position start = 0;
6157 cp_token *token;
6159 /* Remember where the nested-name-specifier starts. */
6160 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6162 start = cp_lexer_token_position (parser->lexer, false);
6163 push_deferring_access_checks (dk_deferred);
6166 while (true)
6168 tree new_scope;
6169 tree old_scope;
6170 tree saved_qualifying_scope;
6172 /* Spot cases that cannot be the beginning of a
6173 nested-name-specifier. */
6174 token = cp_lexer_peek_token (parser->lexer);
6176 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6177 the already parsed nested-name-specifier. */
6178 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6180 /* Grab the nested-name-specifier and continue the loop. */
6181 cp_parser_pre_parsed_nested_name_specifier (parser);
6182 /* If we originally encountered this nested-name-specifier
6183 with IS_DECLARATION set to false, we will not have
6184 resolved TYPENAME_TYPEs, so we must do so here. */
6185 if (is_declaration
6186 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6188 new_scope = resolve_typename_type (parser->scope,
6189 /*only_current_p=*/false);
6190 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6191 parser->scope = new_scope;
6193 success = true;
6194 continue;
6197 /* Spot cases that cannot be the beginning of a
6198 nested-name-specifier. On the second and subsequent times
6199 through the loop, we look for the `template' keyword. */
6200 if (success && token->keyword == RID_TEMPLATE)
6202 /* A template-id can start a nested-name-specifier. */
6203 else if (token->type == CPP_TEMPLATE_ID)
6205 /* DR 743: decltype can be used in a nested-name-specifier. */
6206 else if (token_is_decltype (token))
6208 else
6210 /* If the next token is not an identifier, then it is
6211 definitely not a type-name or namespace-name. */
6212 if (token->type != CPP_NAME)
6213 break;
6214 /* If the following token is neither a `<' (to begin a
6215 template-id), nor a `::', then we are not looking at a
6216 nested-name-specifier. */
6217 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6219 if (token->type == CPP_COLON
6220 && parser->colon_corrects_to_scope_p
6221 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6223 gcc_rich_location richloc (token->location);
6224 richloc.add_fixit_replace ("::");
6225 error_at (&richloc,
6226 "found %<:%> in nested-name-specifier, "
6227 "expected %<::%>");
6228 token->type = CPP_SCOPE;
6231 if (token->type != CPP_SCOPE
6232 && !cp_parser_nth_token_starts_template_argument_list_p
6233 (parser, 2))
6234 break;
6237 /* The nested-name-specifier is optional, so we parse
6238 tentatively. */
6239 cp_parser_parse_tentatively (parser);
6241 /* Look for the optional `template' keyword, if this isn't the
6242 first time through the loop. */
6243 if (success)
6244 template_keyword_p = cp_parser_optional_template_keyword (parser);
6246 /* Save the old scope since the name lookup we are about to do
6247 might destroy it. */
6248 old_scope = parser->scope;
6249 saved_qualifying_scope = parser->qualifying_scope;
6250 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6251 look up names in "X<T>::I" in order to determine that "Y" is
6252 a template. So, if we have a typename at this point, we make
6253 an effort to look through it. */
6254 if (is_declaration
6255 && !typename_keyword_p
6256 && parser->scope
6257 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6258 parser->scope = resolve_typename_type (parser->scope,
6259 /*only_current_p=*/false);
6260 /* Parse the qualifying entity. */
6261 new_scope
6262 = cp_parser_qualifying_entity (parser,
6263 typename_keyword_p,
6264 template_keyword_p,
6265 check_dependency_p,
6266 type_p,
6267 is_declaration);
6268 /* Look for the `::' token. */
6269 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6271 /* If we found what we wanted, we keep going; otherwise, we're
6272 done. */
6273 if (!cp_parser_parse_definitely (parser))
6275 bool error_p = false;
6277 /* Restore the OLD_SCOPE since it was valid before the
6278 failed attempt at finding the last
6279 class-or-namespace-name. */
6280 parser->scope = old_scope;
6281 parser->qualifying_scope = saved_qualifying_scope;
6283 /* If the next token is a decltype, and the one after that is a
6284 `::', then the decltype has failed to resolve to a class or
6285 enumeration type. Give this error even when parsing
6286 tentatively since it can't possibly be valid--and we're going
6287 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6288 won't get another chance.*/
6289 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6290 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6291 == CPP_SCOPE))
6293 token = cp_lexer_consume_token (parser->lexer);
6294 error_at (token->location, "decltype evaluates to %qT, "
6295 "which is not a class or enumeration type",
6296 token->u.tree_check_value->value);
6297 parser->scope = error_mark_node;
6298 error_p = true;
6299 /* As below. */
6300 success = true;
6301 cp_lexer_consume_token (parser->lexer);
6304 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6305 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6307 /* If we have a non-type template-id followed by ::, it can't
6308 possibly be valid. */
6309 token = cp_lexer_peek_token (parser->lexer);
6310 tree tid = token->u.tree_check_value->value;
6311 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6312 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6314 tree tmpl = NULL_TREE;
6315 if (is_overloaded_fn (tid))
6317 tree fns = get_fns (tid);
6318 if (OVL_SINGLE_P (fns))
6319 tmpl = OVL_FIRST (fns);
6320 error_at (token->location, "function template-id %qD "
6321 "in nested-name-specifier", tid);
6323 else
6325 /* Variable template. */
6326 tmpl = TREE_OPERAND (tid, 0);
6327 gcc_assert (variable_template_p (tmpl));
6328 error_at (token->location, "variable template-id %qD "
6329 "in nested-name-specifier", tid);
6331 if (tmpl)
6332 inform (DECL_SOURCE_LOCATION (tmpl),
6333 "%qD declared here", tmpl);
6335 parser->scope = error_mark_node;
6336 error_p = true;
6337 /* As below. */
6338 success = true;
6339 cp_lexer_consume_token (parser->lexer);
6340 cp_lexer_consume_token (parser->lexer);
6344 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6345 break;
6346 /* If the next token is an identifier, and the one after
6347 that is a `::', then any valid interpretation would have
6348 found a class-or-namespace-name. */
6349 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6350 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6351 == CPP_SCOPE)
6352 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6353 != CPP_COMPL))
6355 token = cp_lexer_consume_token (parser->lexer);
6356 if (!error_p)
6358 if (!token->error_reported)
6360 tree decl;
6361 tree ambiguous_decls;
6363 decl = cp_parser_lookup_name (parser, token->u.value,
6364 none_type,
6365 /*is_template=*/false,
6366 /*is_namespace=*/false,
6367 /*check_dependency=*/true,
6368 &ambiguous_decls,
6369 token->location);
6370 if (TREE_CODE (decl) == TEMPLATE_DECL)
6371 error_at (token->location,
6372 "%qD used without template parameters",
6373 decl);
6374 else if (ambiguous_decls)
6376 // cp_parser_lookup_name has the same diagnostic,
6377 // thus make sure to emit it at most once.
6378 if (cp_parser_uncommitted_to_tentative_parse_p
6379 (parser))
6381 error_at (token->location,
6382 "reference to %qD is ambiguous",
6383 token->u.value);
6384 print_candidates (ambiguous_decls);
6386 decl = error_mark_node;
6388 else
6390 if (cxx_dialect != cxx98)
6391 cp_parser_name_lookup_error
6392 (parser, token->u.value, decl, NLE_NOT_CXX98,
6393 token->location);
6394 else
6395 cp_parser_name_lookup_error
6396 (parser, token->u.value, decl, NLE_CXX98,
6397 token->location);
6400 parser->scope = error_mark_node;
6401 error_p = true;
6402 /* Treat this as a successful nested-name-specifier
6403 due to:
6405 [basic.lookup.qual]
6407 If the name found is not a class-name (clause
6408 _class_) or namespace-name (_namespace.def_), the
6409 program is ill-formed. */
6410 success = true;
6412 cp_lexer_consume_token (parser->lexer);
6414 break;
6416 /* We've found one valid nested-name-specifier. */
6417 success = true;
6418 /* Name lookup always gives us a DECL. */
6419 if (TREE_CODE (new_scope) == TYPE_DECL)
6420 new_scope = TREE_TYPE (new_scope);
6421 /* Uses of "template" must be followed by actual templates. */
6422 if (template_keyword_p
6423 && !(CLASS_TYPE_P (new_scope)
6424 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6425 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6426 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6427 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6428 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6429 == TEMPLATE_ID_EXPR)))
6430 permerror (input_location, TYPE_P (new_scope)
6431 ? G_("%qT is not a template")
6432 : G_("%qD is not a template"),
6433 new_scope);
6434 /* If it is a class scope, try to complete it; we are about to
6435 be looking up names inside the class. */
6436 if (TYPE_P (new_scope)
6437 /* Since checking types for dependency can be expensive,
6438 avoid doing it if the type is already complete. */
6439 && !COMPLETE_TYPE_P (new_scope)
6440 /* Do not try to complete dependent types. */
6441 && !dependent_type_p (new_scope))
6443 new_scope = complete_type (new_scope);
6444 /* If it is a typedef to current class, use the current
6445 class instead, as the typedef won't have any names inside
6446 it yet. */
6447 if (!COMPLETE_TYPE_P (new_scope)
6448 && currently_open_class (new_scope))
6449 new_scope = TYPE_MAIN_VARIANT (new_scope);
6451 /* Make sure we look in the right scope the next time through
6452 the loop. */
6453 parser->scope = new_scope;
6456 /* If parsing tentatively, replace the sequence of tokens that makes
6457 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6458 token. That way, should we re-parse the token stream, we will
6459 not have to repeat the effort required to do the parse, nor will
6460 we issue duplicate error messages. */
6461 if (success && start)
6463 cp_token *token;
6465 token = cp_lexer_token_at (parser->lexer, start);
6466 /* Reset the contents of the START token. */
6467 token->type = CPP_NESTED_NAME_SPECIFIER;
6468 /* Retrieve any deferred checks. Do not pop this access checks yet
6469 so the memory will not be reclaimed during token replacing below. */
6470 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6471 token->u.tree_check_value->value = parser->scope;
6472 token->u.tree_check_value->checks = get_deferred_access_checks ();
6473 token->u.tree_check_value->qualifying_scope =
6474 parser->qualifying_scope;
6475 token->keyword = RID_MAX;
6477 /* Purge all subsequent tokens. */
6478 cp_lexer_purge_tokens_after (parser->lexer, start);
6481 if (start)
6482 pop_to_parent_deferring_access_checks ();
6484 return success ? parser->scope : NULL_TREE;
6487 /* Parse a nested-name-specifier. See
6488 cp_parser_nested_name_specifier_opt for details. This function
6489 behaves identically, except that it will an issue an error if no
6490 nested-name-specifier is present. */
6492 static tree
6493 cp_parser_nested_name_specifier (cp_parser *parser,
6494 bool typename_keyword_p,
6495 bool check_dependency_p,
6496 bool type_p,
6497 bool is_declaration)
6499 tree scope;
6501 /* Look for the nested-name-specifier. */
6502 scope = cp_parser_nested_name_specifier_opt (parser,
6503 typename_keyword_p,
6504 check_dependency_p,
6505 type_p,
6506 is_declaration);
6507 /* If it was not present, issue an error message. */
6508 if (!scope)
6510 cp_parser_error (parser, "expected nested-name-specifier");
6511 parser->scope = NULL_TREE;
6514 return scope;
6517 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6518 this is either a class-name or a namespace-name (which corresponds
6519 to the class-or-namespace-name production in the grammar). For
6520 C++0x, it can also be a type-name that refers to an enumeration
6521 type or a simple-template-id.
6523 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6524 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6525 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6526 TYPE_P is TRUE iff the next name should be taken as a class-name,
6527 even the same name is declared to be another entity in the same
6528 scope.
6530 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6531 specified by the class-or-namespace-name. If neither is found the
6532 ERROR_MARK_NODE is returned. */
6534 static tree
6535 cp_parser_qualifying_entity (cp_parser *parser,
6536 bool typename_keyword_p,
6537 bool template_keyword_p,
6538 bool check_dependency_p,
6539 bool type_p,
6540 bool is_declaration)
6542 tree saved_scope;
6543 tree saved_qualifying_scope;
6544 tree saved_object_scope;
6545 tree scope;
6546 bool only_class_p;
6547 bool successful_parse_p;
6549 /* DR 743: decltype can appear in a nested-name-specifier. */
6550 if (cp_lexer_next_token_is_decltype (parser->lexer))
6552 scope = cp_parser_decltype (parser);
6553 if (TREE_CODE (scope) != ENUMERAL_TYPE
6554 && !MAYBE_CLASS_TYPE_P (scope))
6556 cp_parser_simulate_error (parser);
6557 return error_mark_node;
6559 if (TYPE_NAME (scope))
6560 scope = TYPE_NAME (scope);
6561 return scope;
6564 /* Before we try to parse the class-name, we must save away the
6565 current PARSER->SCOPE since cp_parser_class_name will destroy
6566 it. */
6567 saved_scope = parser->scope;
6568 saved_qualifying_scope = parser->qualifying_scope;
6569 saved_object_scope = parser->object_scope;
6570 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6571 there is no need to look for a namespace-name. */
6572 only_class_p = template_keyword_p
6573 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6574 if (!only_class_p)
6575 cp_parser_parse_tentatively (parser);
6576 scope = cp_parser_class_name (parser,
6577 typename_keyword_p,
6578 template_keyword_p,
6579 type_p ? class_type : none_type,
6580 check_dependency_p,
6581 /*class_head_p=*/false,
6582 is_declaration,
6583 /*enum_ok=*/cxx_dialect > cxx98);
6584 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6585 /* If that didn't work, try for a namespace-name. */
6586 if (!only_class_p && !successful_parse_p)
6588 /* Restore the saved scope. */
6589 parser->scope = saved_scope;
6590 parser->qualifying_scope = saved_qualifying_scope;
6591 parser->object_scope = saved_object_scope;
6592 /* If we are not looking at an identifier followed by the scope
6593 resolution operator, then this is not part of a
6594 nested-name-specifier. (Note that this function is only used
6595 to parse the components of a nested-name-specifier.) */
6596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6597 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6598 return error_mark_node;
6599 scope = cp_parser_namespace_name (parser);
6602 return scope;
6605 /* Return true if we are looking at a compound-literal, false otherwise. */
6607 static bool
6608 cp_parser_compound_literal_p (cp_parser *parser)
6610 cp_lexer_save_tokens (parser->lexer);
6612 /* Skip tokens until the next token is a closing parenthesis.
6613 If we find the closing `)', and the next token is a `{', then
6614 we are looking at a compound-literal. */
6615 bool compound_literal_p
6616 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6617 /*consume_paren=*/true)
6618 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6620 /* Roll back the tokens we skipped. */
6621 cp_lexer_rollback_tokens (parser->lexer);
6623 return compound_literal_p;
6626 /* Return true if EXPR is the integer constant zero or a complex constant
6627 of zero, without any folding, but ignoring location wrappers. */
6629 static bool
6630 literal_integer_zerop (const_tree expr)
6632 STRIP_ANY_LOCATION_WRAPPER (expr);
6633 return integer_zerop (expr);
6636 /* Parse a postfix-expression.
6638 postfix-expression:
6639 primary-expression
6640 postfix-expression [ expression ]
6641 postfix-expression ( expression-list [opt] )
6642 simple-type-specifier ( expression-list [opt] )
6643 typename :: [opt] nested-name-specifier identifier
6644 ( expression-list [opt] )
6645 typename :: [opt] nested-name-specifier template [opt] template-id
6646 ( expression-list [opt] )
6647 postfix-expression . template [opt] id-expression
6648 postfix-expression -> template [opt] id-expression
6649 postfix-expression . pseudo-destructor-name
6650 postfix-expression -> pseudo-destructor-name
6651 postfix-expression ++
6652 postfix-expression --
6653 dynamic_cast < type-id > ( expression )
6654 static_cast < type-id > ( expression )
6655 reinterpret_cast < type-id > ( expression )
6656 const_cast < type-id > ( expression )
6657 typeid ( expression )
6658 typeid ( type-id )
6660 GNU Extension:
6662 postfix-expression:
6663 ( type-id ) { initializer-list , [opt] }
6665 This extension is a GNU version of the C99 compound-literal
6666 construct. (The C99 grammar uses `type-name' instead of `type-id',
6667 but they are essentially the same concept.)
6669 If ADDRESS_P is true, the postfix expression is the operand of the
6670 `&' operator. CAST_P is true if this expression is the target of a
6671 cast.
6673 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6674 class member access expressions [expr.ref].
6676 Returns a representation of the expression. */
6678 static cp_expr
6679 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6680 bool member_access_only_p, bool decltype_p,
6681 cp_id_kind * pidk_return)
6683 cp_token *token;
6684 location_t loc;
6685 enum rid keyword;
6686 cp_id_kind idk = CP_ID_KIND_NONE;
6687 cp_expr postfix_expression = NULL_TREE;
6688 bool is_member_access = false;
6690 /* Peek at the next token. */
6691 token = cp_lexer_peek_token (parser->lexer);
6692 loc = token->location;
6693 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6695 /* Some of the productions are determined by keywords. */
6696 keyword = token->keyword;
6697 switch (keyword)
6699 case RID_DYNCAST:
6700 case RID_STATCAST:
6701 case RID_REINTCAST:
6702 case RID_CONSTCAST:
6704 tree type;
6705 cp_expr expression;
6706 const char *saved_message;
6707 bool saved_in_type_id_in_expr_p;
6709 /* All of these can be handled in the same way from the point
6710 of view of parsing. Begin by consuming the token
6711 identifying the cast. */
6712 cp_lexer_consume_token (parser->lexer);
6714 /* New types cannot be defined in the cast. */
6715 saved_message = parser->type_definition_forbidden_message;
6716 parser->type_definition_forbidden_message
6717 = G_("types may not be defined in casts");
6719 /* Look for the opening `<'. */
6720 cp_parser_require (parser, CPP_LESS, RT_LESS);
6721 /* Parse the type to which we are casting. */
6722 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6723 parser->in_type_id_in_expr_p = true;
6724 type = cp_parser_type_id (parser);
6725 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6726 /* Look for the closing `>'. */
6727 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6728 /* Restore the old message. */
6729 parser->type_definition_forbidden_message = saved_message;
6731 bool saved_greater_than_is_operator_p
6732 = parser->greater_than_is_operator_p;
6733 parser->greater_than_is_operator_p = true;
6735 /* And the expression which is being cast. */
6736 matching_parens parens;
6737 parens.require_open (parser);
6738 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6739 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6740 RT_CLOSE_PAREN);
6741 location_t end_loc = close_paren ?
6742 close_paren->location : UNKNOWN_LOCATION;
6744 parser->greater_than_is_operator_p
6745 = saved_greater_than_is_operator_p;
6747 /* Only type conversions to integral or enumeration types
6748 can be used in constant-expressions. */
6749 if (!cast_valid_in_integral_constant_expression_p (type)
6750 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6752 postfix_expression = error_mark_node;
6753 break;
6756 switch (keyword)
6758 case RID_DYNCAST:
6759 postfix_expression
6760 = build_dynamic_cast (type, expression, tf_warning_or_error);
6761 break;
6762 case RID_STATCAST:
6763 postfix_expression
6764 = build_static_cast (type, expression, tf_warning_or_error);
6765 break;
6766 case RID_REINTCAST:
6767 postfix_expression
6768 = build_reinterpret_cast (type, expression,
6769 tf_warning_or_error);
6770 break;
6771 case RID_CONSTCAST:
6772 postfix_expression
6773 = build_const_cast (type, expression, tf_warning_or_error);
6774 break;
6775 default:
6776 gcc_unreachable ();
6779 /* Construct a location e.g. :
6780 reinterpret_cast <int *> (expr)
6781 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6782 ranging from the start of the "*_cast" token to the final closing
6783 paren, with the caret at the start. */
6784 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6785 postfix_expression.set_location (cp_cast_loc);
6787 break;
6789 case RID_TYPEID:
6791 tree type;
6792 const char *saved_message;
6793 bool saved_in_type_id_in_expr_p;
6795 /* Consume the `typeid' token. */
6796 cp_lexer_consume_token (parser->lexer);
6797 /* Look for the `(' token. */
6798 matching_parens parens;
6799 parens.require_open (parser);
6800 /* Types cannot be defined in a `typeid' expression. */
6801 saved_message = parser->type_definition_forbidden_message;
6802 parser->type_definition_forbidden_message
6803 = G_("types may not be defined in a %<typeid%> expression");
6804 /* We can't be sure yet whether we're looking at a type-id or an
6805 expression. */
6806 cp_parser_parse_tentatively (parser);
6807 /* Try a type-id first. */
6808 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6809 parser->in_type_id_in_expr_p = true;
6810 type = cp_parser_type_id (parser);
6811 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6812 /* Look for the `)' token. Otherwise, we can't be sure that
6813 we're not looking at an expression: consider `typeid (int
6814 (3))', for example. */
6815 cp_token *close_paren = parens.require_close (parser);
6816 /* If all went well, simply lookup the type-id. */
6817 if (cp_parser_parse_definitely (parser))
6818 postfix_expression = get_typeid (type, tf_warning_or_error);
6819 /* Otherwise, fall back to the expression variant. */
6820 else
6822 tree expression;
6824 /* Look for an expression. */
6825 expression = cp_parser_expression (parser, & idk);
6826 /* Compute its typeid. */
6827 postfix_expression = build_typeid (expression, tf_warning_or_error);
6828 /* Look for the `)' token. */
6829 close_paren = parens.require_close (parser);
6831 /* Restore the saved message. */
6832 parser->type_definition_forbidden_message = saved_message;
6833 /* `typeid' may not appear in an integral constant expression. */
6834 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6835 postfix_expression = error_mark_node;
6837 /* Construct a location e.g. :
6838 typeid (expr)
6839 ^~~~~~~~~~~~~
6840 ranging from the start of the "typeid" token to the final closing
6841 paren, with the caret at the start. */
6842 if (close_paren)
6844 location_t typeid_loc
6845 = make_location (start_loc, start_loc, close_paren->location);
6846 postfix_expression.set_location (typeid_loc);
6847 postfix_expression.maybe_add_location_wrapper ();
6850 break;
6852 case RID_TYPENAME:
6854 tree type;
6855 /* The syntax permitted here is the same permitted for an
6856 elaborated-type-specifier. */
6857 ++parser->prevent_constrained_type_specifiers;
6858 type = cp_parser_elaborated_type_specifier (parser,
6859 /*is_friend=*/false,
6860 /*is_declaration=*/false);
6861 --parser->prevent_constrained_type_specifiers;
6862 postfix_expression = cp_parser_functional_cast (parser, type);
6864 break;
6866 case RID_ADDRESSOF:
6867 case RID_BUILTIN_SHUFFLE:
6868 case RID_BUILTIN_LAUNDER:
6870 vec<tree, va_gc> *vec;
6871 unsigned int i;
6872 tree p;
6874 cp_lexer_consume_token (parser->lexer);
6875 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6876 /*cast_p=*/false, /*allow_expansion_p=*/true,
6877 /*non_constant_p=*/NULL);
6878 if (vec == NULL)
6880 postfix_expression = error_mark_node;
6881 break;
6884 FOR_EACH_VEC_ELT (*vec, i, p)
6885 mark_exp_read (p);
6887 switch (keyword)
6889 case RID_ADDRESSOF:
6890 if (vec->length () == 1)
6891 postfix_expression
6892 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6893 else
6895 error_at (loc, "wrong number of arguments to "
6896 "%<__builtin_addressof%>");
6897 postfix_expression = error_mark_node;
6899 break;
6901 case RID_BUILTIN_LAUNDER:
6902 if (vec->length () == 1)
6903 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6904 tf_warning_or_error);
6905 else
6907 error_at (loc, "wrong number of arguments to "
6908 "%<__builtin_launder%>");
6909 postfix_expression = error_mark_node;
6911 break;
6913 case RID_BUILTIN_SHUFFLE:
6914 if (vec->length () == 2)
6915 postfix_expression
6916 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6917 (*vec)[1], tf_warning_or_error);
6918 else if (vec->length () == 3)
6919 postfix_expression
6920 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6921 (*vec)[2], tf_warning_or_error);
6922 else
6924 error_at (loc, "wrong number of arguments to "
6925 "%<__builtin_shuffle%>");
6926 postfix_expression = error_mark_node;
6928 break;
6930 default:
6931 gcc_unreachable ();
6933 break;
6936 default:
6938 tree type;
6940 /* If the next thing is a simple-type-specifier, we may be
6941 looking at a functional cast. We could also be looking at
6942 an id-expression. So, we try the functional cast, and if
6943 that doesn't work we fall back to the primary-expression. */
6944 cp_parser_parse_tentatively (parser);
6945 /* Look for the simple-type-specifier. */
6946 ++parser->prevent_constrained_type_specifiers;
6947 type = cp_parser_simple_type_specifier (parser,
6948 /*decl_specs=*/NULL,
6949 CP_PARSER_FLAGS_NONE);
6950 --parser->prevent_constrained_type_specifiers;
6951 /* Parse the cast itself. */
6952 if (!cp_parser_error_occurred (parser))
6953 postfix_expression
6954 = cp_parser_functional_cast (parser, type);
6955 /* If that worked, we're done. */
6956 if (cp_parser_parse_definitely (parser))
6957 break;
6959 /* If the functional-cast didn't work out, try a
6960 compound-literal. */
6961 if (cp_parser_allow_gnu_extensions_p (parser)
6962 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6964 cp_expr initializer = NULL_TREE;
6966 cp_parser_parse_tentatively (parser);
6968 matching_parens parens;
6969 parens.consume_open (parser);
6971 /* Avoid calling cp_parser_type_id pointlessly, see comment
6972 in cp_parser_cast_expression about c++/29234. */
6973 if (!cp_parser_compound_literal_p (parser))
6974 cp_parser_simulate_error (parser);
6975 else
6977 /* Parse the type. */
6978 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6979 parser->in_type_id_in_expr_p = true;
6980 type = cp_parser_type_id (parser);
6981 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6982 parens.require_close (parser);
6985 /* If things aren't going well, there's no need to
6986 keep going. */
6987 if (!cp_parser_error_occurred (parser))
6989 bool non_constant_p;
6990 /* Parse the brace-enclosed initializer list. */
6991 initializer = cp_parser_braced_list (parser,
6992 &non_constant_p);
6994 /* If that worked, we're definitely looking at a
6995 compound-literal expression. */
6996 if (cp_parser_parse_definitely (parser))
6998 /* Warn the user that a compound literal is not
6999 allowed in standard C++. */
7000 pedwarn (input_location, OPT_Wpedantic,
7001 "ISO C++ forbids compound-literals");
7002 /* For simplicity, we disallow compound literals in
7003 constant-expressions. We could
7004 allow compound literals of integer type, whose
7005 initializer was a constant, in constant
7006 expressions. Permitting that usage, as a further
7007 extension, would not change the meaning of any
7008 currently accepted programs. (Of course, as
7009 compound literals are not part of ISO C++, the
7010 standard has nothing to say.) */
7011 if (cp_parser_non_integral_constant_expression (parser,
7012 NIC_NCC))
7014 postfix_expression = error_mark_node;
7015 break;
7017 /* Form the representation of the compound-literal. */
7018 postfix_expression
7019 = finish_compound_literal (type, initializer,
7020 tf_warning_or_error, fcl_c99);
7021 postfix_expression.set_location (initializer.get_location ());
7022 break;
7026 /* It must be a primary-expression. */
7027 postfix_expression
7028 = cp_parser_primary_expression (parser, address_p, cast_p,
7029 /*template_arg_p=*/false,
7030 decltype_p,
7031 &idk);
7033 break;
7036 /* Note that we don't need to worry about calling build_cplus_new on a
7037 class-valued CALL_EXPR in decltype when it isn't the end of the
7038 postfix-expression; unary_complex_lvalue will take care of that for
7039 all these cases. */
7041 /* Keep looping until the postfix-expression is complete. */
7042 while (true)
7044 if (idk == CP_ID_KIND_UNQUALIFIED
7045 && identifier_p (postfix_expression)
7046 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7047 /* It is not a Koenig lookup function call. */
7048 postfix_expression
7049 = unqualified_name_lookup_error (postfix_expression);
7051 /* Peek at the next token. */
7052 token = cp_lexer_peek_token (parser->lexer);
7054 switch (token->type)
7056 case CPP_OPEN_SQUARE:
7057 if (cp_next_tokens_can_be_std_attribute_p (parser))
7059 cp_parser_error (parser,
7060 "two consecutive %<[%> shall "
7061 "only introduce an attribute");
7062 return error_mark_node;
7064 postfix_expression
7065 = cp_parser_postfix_open_square_expression (parser,
7066 postfix_expression,
7067 false,
7068 decltype_p);
7069 postfix_expression.set_range (start_loc,
7070 postfix_expression.get_location ());
7072 idk = CP_ID_KIND_NONE;
7073 is_member_access = false;
7074 break;
7076 case CPP_OPEN_PAREN:
7077 /* postfix-expression ( expression-list [opt] ) */
7079 bool koenig_p;
7080 bool is_builtin_constant_p;
7081 bool saved_integral_constant_expression_p = false;
7082 bool saved_non_integral_constant_expression_p = false;
7083 tsubst_flags_t complain = complain_flags (decltype_p);
7084 vec<tree, va_gc> *args;
7085 location_t close_paren_loc = UNKNOWN_LOCATION;
7087 is_member_access = false;
7089 is_builtin_constant_p
7090 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7091 if (is_builtin_constant_p)
7093 /* The whole point of __builtin_constant_p is to allow
7094 non-constant expressions to appear as arguments. */
7095 saved_integral_constant_expression_p
7096 = parser->integral_constant_expression_p;
7097 saved_non_integral_constant_expression_p
7098 = parser->non_integral_constant_expression_p;
7099 parser->integral_constant_expression_p = false;
7101 args = (cp_parser_parenthesized_expression_list
7102 (parser, non_attr,
7103 /*cast_p=*/false, /*allow_expansion_p=*/true,
7104 /*non_constant_p=*/NULL,
7105 /*close_paren_loc=*/&close_paren_loc,
7106 /*wrap_locations_p=*/true));
7107 if (is_builtin_constant_p)
7109 parser->integral_constant_expression_p
7110 = saved_integral_constant_expression_p;
7111 parser->non_integral_constant_expression_p
7112 = saved_non_integral_constant_expression_p;
7115 if (args == NULL)
7117 postfix_expression = error_mark_node;
7118 break;
7121 /* Function calls are not permitted in
7122 constant-expressions. */
7123 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7124 && cp_parser_non_integral_constant_expression (parser,
7125 NIC_FUNC_CALL))
7127 postfix_expression = error_mark_node;
7128 release_tree_vector (args);
7129 break;
7132 koenig_p = false;
7133 if (idk == CP_ID_KIND_UNQUALIFIED
7134 || idk == CP_ID_KIND_TEMPLATE_ID)
7136 if (identifier_p (postfix_expression))
7138 if (!args->is_empty ())
7140 koenig_p = true;
7141 if (!any_type_dependent_arguments_p (args))
7142 postfix_expression
7143 = perform_koenig_lookup (postfix_expression, args,
7144 complain);
7146 else
7147 postfix_expression
7148 = unqualified_fn_lookup_error (postfix_expression);
7150 /* We do not perform argument-dependent lookup if
7151 normal lookup finds a non-function, in accordance
7152 with the expected resolution of DR 218. */
7153 else if (!args->is_empty ()
7154 && is_overloaded_fn (postfix_expression))
7156 tree fn = get_first_fn (postfix_expression);
7157 fn = STRIP_TEMPLATE (fn);
7159 /* Do not do argument dependent lookup if regular
7160 lookup finds a member function or a block-scope
7161 function declaration. [basic.lookup.argdep]/3 */
7162 if (!DECL_FUNCTION_MEMBER_P (fn)
7163 && !DECL_LOCAL_FUNCTION_P (fn))
7165 koenig_p = true;
7166 if (!any_type_dependent_arguments_p (args))
7167 postfix_expression
7168 = perform_koenig_lookup (postfix_expression, args,
7169 complain);
7174 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7175 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7176 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7177 && vec_safe_length (args) == 3)
7179 tree arg0 = (*args)[0];
7180 tree arg1 = (*args)[1];
7181 tree arg2 = (*args)[2];
7182 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7183 | (literal_integer_zerop (arg2) << 2));
7184 warn_for_memset (input_location, arg0, arg2, literal_mask);
7187 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7189 tree instance = TREE_OPERAND (postfix_expression, 0);
7190 tree fn = TREE_OPERAND (postfix_expression, 1);
7192 if (processing_template_decl
7193 && (type_dependent_object_expression_p (instance)
7194 || (!BASELINK_P (fn)
7195 && TREE_CODE (fn) != FIELD_DECL)
7196 || type_dependent_expression_p (fn)
7197 || any_type_dependent_arguments_p (args)))
7199 maybe_generic_this_capture (instance, fn);
7200 postfix_expression
7201 = build_min_nt_call_vec (postfix_expression, args);
7202 release_tree_vector (args);
7203 break;
7206 if (BASELINK_P (fn))
7208 postfix_expression
7209 = (build_new_method_call
7210 (instance, fn, &args, NULL_TREE,
7211 (idk == CP_ID_KIND_QUALIFIED
7212 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7213 : LOOKUP_NORMAL),
7214 /*fn_p=*/NULL,
7215 complain));
7217 else
7218 postfix_expression
7219 = finish_call_expr (postfix_expression, &args,
7220 /*disallow_virtual=*/false,
7221 /*koenig_p=*/false,
7222 complain);
7224 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7225 || TREE_CODE (postfix_expression) == MEMBER_REF
7226 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7227 postfix_expression = (build_offset_ref_call_from_tree
7228 (postfix_expression, &args,
7229 complain));
7230 else if (idk == CP_ID_KIND_QUALIFIED)
7231 /* A call to a static class member, or a namespace-scope
7232 function. */
7233 postfix_expression
7234 = finish_call_expr (postfix_expression, &args,
7235 /*disallow_virtual=*/true,
7236 koenig_p,
7237 complain);
7238 else
7239 /* All other function calls. */
7240 postfix_expression
7241 = finish_call_expr (postfix_expression, &args,
7242 /*disallow_virtual=*/false,
7243 koenig_p,
7244 complain);
7246 if (close_paren_loc != UNKNOWN_LOCATION)
7248 location_t combined_loc = make_location (token->location,
7249 start_loc,
7250 close_paren_loc);
7251 postfix_expression.set_location (combined_loc);
7254 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7255 idk = CP_ID_KIND_NONE;
7257 release_tree_vector (args);
7259 break;
7261 case CPP_DOT:
7262 case CPP_DEREF:
7263 /* postfix-expression . template [opt] id-expression
7264 postfix-expression . pseudo-destructor-name
7265 postfix-expression -> template [opt] id-expression
7266 postfix-expression -> pseudo-destructor-name */
7268 /* Consume the `.' or `->' operator. */
7269 cp_lexer_consume_token (parser->lexer);
7271 postfix_expression
7272 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7273 postfix_expression,
7274 false, &idk, loc);
7276 is_member_access = true;
7277 break;
7279 case CPP_PLUS_PLUS:
7280 /* postfix-expression ++ */
7281 /* Consume the `++' token. */
7282 cp_lexer_consume_token (parser->lexer);
7283 /* Generate a representation for the complete expression. */
7284 postfix_expression
7285 = finish_increment_expr (postfix_expression,
7286 POSTINCREMENT_EXPR);
7287 /* Increments may not appear in constant-expressions. */
7288 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7289 postfix_expression = error_mark_node;
7290 idk = CP_ID_KIND_NONE;
7291 is_member_access = false;
7292 break;
7294 case CPP_MINUS_MINUS:
7295 /* postfix-expression -- */
7296 /* Consume the `--' token. */
7297 cp_lexer_consume_token (parser->lexer);
7298 /* Generate a representation for the complete expression. */
7299 postfix_expression
7300 = finish_increment_expr (postfix_expression,
7301 POSTDECREMENT_EXPR);
7302 /* Decrements may not appear in constant-expressions. */
7303 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7304 postfix_expression = error_mark_node;
7305 idk = CP_ID_KIND_NONE;
7306 is_member_access = false;
7307 break;
7309 default:
7310 if (pidk_return != NULL)
7311 * pidk_return = idk;
7312 if (member_access_only_p)
7313 return is_member_access
7314 ? postfix_expression
7315 : cp_expr (error_mark_node);
7316 else
7317 return postfix_expression;
7321 /* We should never get here. */
7322 gcc_unreachable ();
7323 return error_mark_node;
7326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7327 by cp_parser_builtin_offsetof. We're looking for
7329 postfix-expression [ expression ]
7330 postfix-expression [ braced-init-list ] (C++11)
7332 FOR_OFFSETOF is set if we're being called in that context, which
7333 changes how we deal with integer constant expressions. */
7335 static tree
7336 cp_parser_postfix_open_square_expression (cp_parser *parser,
7337 tree postfix_expression,
7338 bool for_offsetof,
7339 bool decltype_p)
7341 tree index = NULL_TREE;
7342 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7343 bool saved_greater_than_is_operator_p;
7345 /* Consume the `[' token. */
7346 cp_lexer_consume_token (parser->lexer);
7348 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7349 parser->greater_than_is_operator_p = true;
7351 /* Parse the index expression. */
7352 /* ??? For offsetof, there is a question of what to allow here. If
7353 offsetof is not being used in an integral constant expression context,
7354 then we *could* get the right answer by computing the value at runtime.
7355 If we are in an integral constant expression context, then we might
7356 could accept any constant expression; hard to say without analysis.
7357 Rather than open the barn door too wide right away, allow only integer
7358 constant expressions here. */
7359 if (for_offsetof)
7360 index = cp_parser_constant_expression (parser);
7361 else
7363 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7365 bool expr_nonconst_p;
7366 cp_lexer_set_source_position (parser->lexer);
7367 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7368 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7370 else
7371 index = cp_parser_expression (parser);
7374 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7376 /* Look for the closing `]'. */
7377 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7379 /* Build the ARRAY_REF. */
7380 postfix_expression = grok_array_decl (loc, postfix_expression,
7381 index, decltype_p);
7383 /* When not doing offsetof, array references are not permitted in
7384 constant-expressions. */
7385 if (!for_offsetof
7386 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7387 postfix_expression = error_mark_node;
7389 return postfix_expression;
7392 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7393 dereference of incomplete type, returns true if error_mark_node should
7394 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7395 and *DEPENDENT_P. */
7397 bool
7398 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7399 bool *dependent_p)
7401 /* In a template, be permissive by treating an object expression
7402 of incomplete type as dependent (after a pedwarn). */
7403 diagnostic_t kind = (processing_template_decl
7404 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7406 switch (TREE_CODE (*postfix_expression))
7408 case CAST_EXPR:
7409 case REINTERPRET_CAST_EXPR:
7410 case CONST_CAST_EXPR:
7411 case STATIC_CAST_EXPR:
7412 case DYNAMIC_CAST_EXPR:
7413 case IMPLICIT_CONV_EXPR:
7414 case VIEW_CONVERT_EXPR:
7415 case NON_LVALUE_EXPR:
7416 kind = DK_ERROR;
7417 break;
7418 case OVERLOAD:
7419 /* Don't emit any diagnostic for OVERLOADs. */
7420 kind = DK_IGNORED;
7421 break;
7422 default:
7423 /* Avoid clobbering e.g. DECLs. */
7424 if (!EXPR_P (*postfix_expression))
7425 kind = DK_ERROR;
7426 break;
7429 if (kind == DK_IGNORED)
7430 return false;
7432 location_t exploc = location_of (*postfix_expression);
7433 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7434 if (!MAYBE_CLASS_TYPE_P (*scope))
7435 return true;
7436 if (kind == DK_ERROR)
7437 *scope = *postfix_expression = error_mark_node;
7438 else if (processing_template_decl)
7440 *dependent_p = true;
7441 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7443 return false;
7446 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7447 by cp_parser_builtin_offsetof. We're looking for
7449 postfix-expression . template [opt] id-expression
7450 postfix-expression . pseudo-destructor-name
7451 postfix-expression -> template [opt] id-expression
7452 postfix-expression -> pseudo-destructor-name
7454 FOR_OFFSETOF is set if we're being called in that context. That sorta
7455 limits what of the above we'll actually accept, but nevermind.
7456 TOKEN_TYPE is the "." or "->" token, which will already have been
7457 removed from the stream. */
7459 static tree
7460 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7461 enum cpp_ttype token_type,
7462 cp_expr postfix_expression,
7463 bool for_offsetof, cp_id_kind *idk,
7464 location_t location)
7466 tree name;
7467 bool dependent_p;
7468 bool pseudo_destructor_p;
7469 tree scope = NULL_TREE;
7470 location_t start_loc = postfix_expression.get_start ();
7472 /* If this is a `->' operator, dereference the pointer. */
7473 if (token_type == CPP_DEREF)
7474 postfix_expression = build_x_arrow (location, postfix_expression,
7475 tf_warning_or_error);
7476 /* Check to see whether or not the expression is type-dependent and
7477 not the current instantiation. */
7478 dependent_p = type_dependent_object_expression_p (postfix_expression);
7479 /* The identifier following the `->' or `.' is not qualified. */
7480 parser->scope = NULL_TREE;
7481 parser->qualifying_scope = NULL_TREE;
7482 parser->object_scope = NULL_TREE;
7483 *idk = CP_ID_KIND_NONE;
7485 /* Enter the scope corresponding to the type of the object
7486 given by the POSTFIX_EXPRESSION. */
7487 if (!dependent_p)
7489 scope = TREE_TYPE (postfix_expression);
7490 /* According to the standard, no expression should ever have
7491 reference type. Unfortunately, we do not currently match
7492 the standard in this respect in that our internal representation
7493 of an expression may have reference type even when the standard
7494 says it does not. Therefore, we have to manually obtain the
7495 underlying type here. */
7496 scope = non_reference (scope);
7497 /* The type of the POSTFIX_EXPRESSION must be complete. */
7498 /* Unlike the object expression in other contexts, *this is not
7499 required to be of complete type for purposes of class member
7500 access (5.2.5) outside the member function body. */
7501 if (postfix_expression != current_class_ref
7502 && scope != error_mark_node
7503 && !(processing_template_decl
7504 && current_class_type
7505 && (same_type_ignoring_top_level_qualifiers_p
7506 (scope, current_class_type))))
7508 scope = complete_type (scope);
7509 if (!COMPLETE_TYPE_P (scope)
7510 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7511 &dependent_p))
7512 return error_mark_node;
7515 if (!dependent_p)
7517 /* Let the name lookup machinery know that we are processing a
7518 class member access expression. */
7519 parser->context->object_type = scope;
7520 /* If something went wrong, we want to be able to discern that case,
7521 as opposed to the case where there was no SCOPE due to the type
7522 of expression being dependent. */
7523 if (!scope)
7524 scope = error_mark_node;
7525 /* If the SCOPE was erroneous, make the various semantic analysis
7526 functions exit quickly -- and without issuing additional error
7527 messages. */
7528 if (scope == error_mark_node)
7529 postfix_expression = error_mark_node;
7533 if (dependent_p)
7534 /* Tell cp_parser_lookup_name that there was an object, even though it's
7535 type-dependent. */
7536 parser->context->object_type = unknown_type_node;
7538 /* Assume this expression is not a pseudo-destructor access. */
7539 pseudo_destructor_p = false;
7541 /* If the SCOPE is a scalar type, then, if this is a valid program,
7542 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7543 is type dependent, it can be pseudo-destructor-name or something else.
7544 Try to parse it as pseudo-destructor-name first. */
7545 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7547 tree s;
7548 tree type;
7550 cp_parser_parse_tentatively (parser);
7551 /* Parse the pseudo-destructor-name. */
7552 s = NULL_TREE;
7553 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7554 &s, &type);
7555 if (dependent_p
7556 && (cp_parser_error_occurred (parser)
7557 || !SCALAR_TYPE_P (type)))
7558 cp_parser_abort_tentative_parse (parser);
7559 else if (cp_parser_parse_definitely (parser))
7561 pseudo_destructor_p = true;
7562 postfix_expression
7563 = finish_pseudo_destructor_expr (postfix_expression,
7564 s, type, location);
7568 if (!pseudo_destructor_p)
7570 /* If the SCOPE is not a scalar type, we are looking at an
7571 ordinary class member access expression, rather than a
7572 pseudo-destructor-name. */
7573 bool template_p;
7574 cp_token *token = cp_lexer_peek_token (parser->lexer);
7575 /* Parse the id-expression. */
7576 name = (cp_parser_id_expression
7577 (parser,
7578 cp_parser_optional_template_keyword (parser),
7579 /*check_dependency_p=*/true,
7580 &template_p,
7581 /*declarator_p=*/false,
7582 /*optional_p=*/false));
7583 /* In general, build a SCOPE_REF if the member name is qualified.
7584 However, if the name was not dependent and has already been
7585 resolved; there is no need to build the SCOPE_REF. For example;
7587 struct X { void f(); };
7588 template <typename T> void f(T* t) { t->X::f(); }
7590 Even though "t" is dependent, "X::f" is not and has been resolved
7591 to a BASELINK; there is no need to include scope information. */
7593 /* But we do need to remember that there was an explicit scope for
7594 virtual function calls. */
7595 if (parser->scope)
7596 *idk = CP_ID_KIND_QUALIFIED;
7598 /* If the name is a template-id that names a type, we will get a
7599 TYPE_DECL here. That is invalid code. */
7600 if (TREE_CODE (name) == TYPE_DECL)
7602 error_at (token->location, "invalid use of %qD", name);
7603 postfix_expression = error_mark_node;
7605 else
7607 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7609 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7611 error_at (token->location, "%<%D::%D%> is not a class member",
7612 parser->scope, name);
7613 postfix_expression = error_mark_node;
7615 else
7616 name = build_qualified_name (/*type=*/NULL_TREE,
7617 parser->scope,
7618 name,
7619 template_p);
7620 parser->scope = NULL_TREE;
7621 parser->qualifying_scope = NULL_TREE;
7622 parser->object_scope = NULL_TREE;
7624 if (parser->scope && name && BASELINK_P (name))
7625 adjust_result_of_qualified_name_lookup
7626 (name, parser->scope, scope);
7627 postfix_expression
7628 = finish_class_member_access_expr (postfix_expression, name,
7629 template_p,
7630 tf_warning_or_error);
7631 /* Build a location e.g.:
7632 ptr->access_expr
7633 ~~~^~~~~~~~~~~~~
7634 where the caret is at the deref token, ranging from
7635 the start of postfix_expression to the end of the access expr. */
7636 location_t end_loc
7637 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7638 location_t combined_loc
7639 = make_location (input_location, start_loc, end_loc);
7640 protected_set_expr_location (postfix_expression, combined_loc);
7644 /* We no longer need to look up names in the scope of the object on
7645 the left-hand side of the `.' or `->' operator. */
7646 parser->context->object_type = NULL_TREE;
7648 /* Outside of offsetof, these operators may not appear in
7649 constant-expressions. */
7650 if (!for_offsetof
7651 && (cp_parser_non_integral_constant_expression
7652 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7653 postfix_expression = error_mark_node;
7655 return postfix_expression;
7658 /* Parse a parenthesized expression-list.
7660 expression-list:
7661 assignment-expression
7662 expression-list, assignment-expression
7664 attribute-list:
7665 expression-list
7666 identifier
7667 identifier, expression-list
7669 CAST_P is true if this expression is the target of a cast.
7671 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7672 argument pack.
7674 WRAP_LOCATIONS_P is true if expressions within this list for which
7675 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7676 their source locations.
7678 Returns a vector of trees. Each element is a representation of an
7679 assignment-expression. NULL is returned if the ( and or ) are
7680 missing. An empty, but allocated, vector is returned on no
7681 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7682 if we are parsing an attribute list for an attribute that wants a
7683 plain identifier argument, normal_attr for an attribute that wants
7684 an expression, or non_attr if we aren't parsing an attribute list. If
7685 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7686 not all of the expressions in the list were constant.
7687 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7688 will be written to with the location of the closing parenthesis. If
7689 an error occurs, it may or may not be written to. */
7691 static vec<tree, va_gc> *
7692 cp_parser_parenthesized_expression_list (cp_parser* parser,
7693 int is_attribute_list,
7694 bool cast_p,
7695 bool allow_expansion_p,
7696 bool *non_constant_p,
7697 location_t *close_paren_loc,
7698 bool wrap_locations_p)
7700 vec<tree, va_gc> *expression_list;
7701 bool fold_expr_p = is_attribute_list != non_attr;
7702 tree identifier = NULL_TREE;
7703 bool saved_greater_than_is_operator_p;
7705 /* Assume all the expressions will be constant. */
7706 if (non_constant_p)
7707 *non_constant_p = false;
7709 matching_parens parens;
7710 if (!parens.require_open (parser))
7711 return NULL;
7713 expression_list = make_tree_vector ();
7715 /* Within a parenthesized expression, a `>' token is always
7716 the greater-than operator. */
7717 saved_greater_than_is_operator_p
7718 = parser->greater_than_is_operator_p;
7719 parser->greater_than_is_operator_p = true;
7721 cp_expr expr (NULL_TREE);
7723 /* Consume expressions until there are no more. */
7724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7725 while (true)
7727 /* At the beginning of attribute lists, check to see if the
7728 next token is an identifier. */
7729 if (is_attribute_list == id_attr
7730 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7732 cp_token *token;
7734 /* Consume the identifier. */
7735 token = cp_lexer_consume_token (parser->lexer);
7736 /* Save the identifier. */
7737 identifier = token->u.value;
7739 else
7741 bool expr_non_constant_p;
7743 /* Parse the next assignment-expression. */
7744 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7746 /* A braced-init-list. */
7747 cp_lexer_set_source_position (parser->lexer);
7748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7749 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7750 if (non_constant_p && expr_non_constant_p)
7751 *non_constant_p = true;
7753 else if (non_constant_p)
7755 expr = (cp_parser_constant_expression
7756 (parser, /*allow_non_constant_p=*/true,
7757 &expr_non_constant_p));
7758 if (expr_non_constant_p)
7759 *non_constant_p = true;
7761 else
7762 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7763 cast_p);
7765 if (fold_expr_p)
7766 expr = instantiate_non_dependent_expr (expr);
7768 /* If we have an ellipsis, then this is an expression
7769 expansion. */
7770 if (allow_expansion_p
7771 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7773 /* Consume the `...'. */
7774 cp_lexer_consume_token (parser->lexer);
7776 /* Build the argument pack. */
7777 expr = make_pack_expansion (expr);
7780 if (wrap_locations_p)
7781 expr.maybe_add_location_wrapper ();
7783 /* Add it to the list. We add error_mark_node
7784 expressions to the list, so that we can still tell if
7785 the correct form for a parenthesized expression-list
7786 is found. That gives better errors. */
7787 vec_safe_push (expression_list, expr.get_value ());
7789 if (expr == error_mark_node)
7790 goto skip_comma;
7793 /* After the first item, attribute lists look the same as
7794 expression lists. */
7795 is_attribute_list = non_attr;
7797 get_comma:;
7798 /* If the next token isn't a `,', then we are done. */
7799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7800 break;
7802 /* Otherwise, consume the `,' and keep going. */
7803 cp_lexer_consume_token (parser->lexer);
7806 if (close_paren_loc)
7807 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7809 if (!parens.require_close (parser))
7811 int ending;
7813 skip_comma:;
7814 /* We try and resync to an unnested comma, as that will give the
7815 user better diagnostics. */
7816 ending = cp_parser_skip_to_closing_parenthesis (parser,
7817 /*recovering=*/true,
7818 /*or_comma=*/true,
7819 /*consume_paren=*/true);
7820 if (ending < 0)
7821 goto get_comma;
7822 if (!ending)
7824 parser->greater_than_is_operator_p
7825 = saved_greater_than_is_operator_p;
7826 return NULL;
7830 parser->greater_than_is_operator_p
7831 = saved_greater_than_is_operator_p;
7833 if (identifier)
7834 vec_safe_insert (expression_list, 0, identifier);
7836 return expression_list;
7839 /* Parse a pseudo-destructor-name.
7841 pseudo-destructor-name:
7842 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7843 :: [opt] nested-name-specifier template template-id :: ~ type-name
7844 :: [opt] nested-name-specifier [opt] ~ type-name
7846 If either of the first two productions is used, sets *SCOPE to the
7847 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7848 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7849 or ERROR_MARK_NODE if the parse fails. */
7851 static void
7852 cp_parser_pseudo_destructor_name (cp_parser* parser,
7853 tree object,
7854 tree* scope,
7855 tree* type)
7857 bool nested_name_specifier_p;
7859 /* Handle ~auto. */
7860 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7861 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7862 && !type_dependent_expression_p (object))
7864 if (cxx_dialect < cxx14)
7865 pedwarn (input_location, 0,
7866 "%<~auto%> only available with "
7867 "-std=c++14 or -std=gnu++14");
7868 cp_lexer_consume_token (parser->lexer);
7869 cp_lexer_consume_token (parser->lexer);
7870 *scope = NULL_TREE;
7871 *type = TREE_TYPE (object);
7872 return;
7875 /* Assume that things will not work out. */
7876 *type = error_mark_node;
7878 /* Look for the optional `::' operator. */
7879 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7880 /* Look for the optional nested-name-specifier. */
7881 nested_name_specifier_p
7882 = (cp_parser_nested_name_specifier_opt (parser,
7883 /*typename_keyword_p=*/false,
7884 /*check_dependency_p=*/true,
7885 /*type_p=*/false,
7886 /*is_declaration=*/false)
7887 != NULL_TREE);
7888 /* Now, if we saw a nested-name-specifier, we might be doing the
7889 second production. */
7890 if (nested_name_specifier_p
7891 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7893 /* Consume the `template' keyword. */
7894 cp_lexer_consume_token (parser->lexer);
7895 /* Parse the template-id. */
7896 cp_parser_template_id (parser,
7897 /*template_keyword_p=*/true,
7898 /*check_dependency_p=*/false,
7899 class_type,
7900 /*is_declaration=*/true);
7901 /* Look for the `::' token. */
7902 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7904 /* If the next token is not a `~', then there might be some
7905 additional qualification. */
7906 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7908 /* At this point, we're looking for "type-name :: ~". The type-name
7909 must not be a class-name, since this is a pseudo-destructor. So,
7910 it must be either an enum-name, or a typedef-name -- both of which
7911 are just identifiers. So, we peek ahead to check that the "::"
7912 and "~" tokens are present; if they are not, then we can avoid
7913 calling type_name. */
7914 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7915 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7916 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7918 cp_parser_error (parser, "non-scalar type");
7919 return;
7922 /* Look for the type-name. */
7923 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7924 if (*scope == error_mark_node)
7925 return;
7927 /* Look for the `::' token. */
7928 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7930 else
7931 *scope = NULL_TREE;
7933 /* Look for the `~'. */
7934 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7936 /* Once we see the ~, this has to be a pseudo-destructor. */
7937 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7938 cp_parser_commit_to_topmost_tentative_parse (parser);
7940 /* Look for the type-name again. We are not responsible for
7941 checking that it matches the first type-name. */
7942 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7945 /* Parse a unary-expression.
7947 unary-expression:
7948 postfix-expression
7949 ++ cast-expression
7950 -- cast-expression
7951 unary-operator cast-expression
7952 sizeof unary-expression
7953 sizeof ( type-id )
7954 alignof ( type-id ) [C++0x]
7955 new-expression
7956 delete-expression
7958 GNU Extensions:
7960 unary-expression:
7961 __extension__ cast-expression
7962 __alignof__ unary-expression
7963 __alignof__ ( type-id )
7964 alignof unary-expression [C++0x]
7965 __real__ cast-expression
7966 __imag__ cast-expression
7967 && identifier
7968 sizeof ( type-id ) { initializer-list , [opt] }
7969 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7970 __alignof__ ( type-id ) { initializer-list , [opt] }
7972 ADDRESS_P is true iff the unary-expression is appearing as the
7973 operand of the `&' operator. CAST_P is true if this expression is
7974 the target of a cast.
7976 Returns a representation of the expression. */
7978 static cp_expr
7979 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7980 bool address_p, bool cast_p, bool decltype_p)
7982 cp_token *token;
7983 enum tree_code unary_operator;
7985 /* Peek at the next token. */
7986 token = cp_lexer_peek_token (parser->lexer);
7987 /* Some keywords give away the kind of expression. */
7988 if (token->type == CPP_KEYWORD)
7990 enum rid keyword = token->keyword;
7992 switch (keyword)
7994 case RID_ALIGNOF:
7995 case RID_SIZEOF:
7997 tree operand, ret;
7998 enum tree_code op;
7999 location_t start_loc = token->location;
8001 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8002 /* Consume the token. */
8003 cp_lexer_consume_token (parser->lexer);
8004 /* Parse the operand. */
8005 operand = cp_parser_sizeof_operand (parser, keyword);
8007 if (TYPE_P (operand))
8008 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8009 else
8011 /* ISO C++ defines alignof only with types, not with
8012 expressions. So pedwarn if alignof is used with a non-
8013 type expression. However, __alignof__ is ok. */
8014 if (id_equal (token->u.value, "alignof"))
8015 pedwarn (token->location, OPT_Wpedantic,
8016 "ISO C++ does not allow %<alignof%> "
8017 "with a non-type");
8019 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8021 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8022 SIZEOF_EXPR with the original operand. */
8023 if (op == SIZEOF_EXPR && ret != error_mark_node)
8025 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8027 if (!processing_template_decl && TYPE_P (operand))
8029 ret = build_min (SIZEOF_EXPR, size_type_node,
8030 build1 (NOP_EXPR, operand,
8031 error_mark_node));
8032 SIZEOF_EXPR_TYPE_P (ret) = 1;
8034 else
8035 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8036 TREE_SIDE_EFFECTS (ret) = 0;
8037 TREE_READONLY (ret) = 1;
8041 /* Construct a location e.g. :
8042 alignof (expr)
8043 ^~~~~~~~~~~~~~
8044 with start == caret at the start of the "alignof"/"sizeof"
8045 token, with the endpoint at the final closing paren. */
8046 location_t finish_loc
8047 = cp_lexer_previous_token (parser->lexer)->location;
8048 location_t compound_loc
8049 = make_location (start_loc, start_loc, finish_loc);
8051 cp_expr ret_expr (ret);
8052 ret_expr.set_location (compound_loc);
8053 ret_expr = ret_expr.maybe_add_location_wrapper ();
8054 return ret_expr;
8057 case RID_NEW:
8058 return cp_parser_new_expression (parser);
8060 case RID_DELETE:
8061 return cp_parser_delete_expression (parser);
8063 case RID_EXTENSION:
8065 /* The saved value of the PEDANTIC flag. */
8066 int saved_pedantic;
8067 tree expr;
8069 /* Save away the PEDANTIC flag. */
8070 cp_parser_extension_opt (parser, &saved_pedantic);
8071 /* Parse the cast-expression. */
8072 expr = cp_parser_simple_cast_expression (parser);
8073 /* Restore the PEDANTIC flag. */
8074 pedantic = saved_pedantic;
8076 return expr;
8079 case RID_REALPART:
8080 case RID_IMAGPART:
8082 tree expression;
8084 /* Consume the `__real__' or `__imag__' token. */
8085 cp_lexer_consume_token (parser->lexer);
8086 /* Parse the cast-expression. */
8087 expression = cp_parser_simple_cast_expression (parser);
8088 /* Create the complete representation. */
8089 return build_x_unary_op (token->location,
8090 (keyword == RID_REALPART
8091 ? REALPART_EXPR : IMAGPART_EXPR),
8092 expression,
8093 tf_warning_or_error);
8095 break;
8097 case RID_TRANSACTION_ATOMIC:
8098 case RID_TRANSACTION_RELAXED:
8099 return cp_parser_transaction_expression (parser, keyword);
8101 case RID_NOEXCEPT:
8103 tree expr;
8104 const char *saved_message;
8105 bool saved_integral_constant_expression_p;
8106 bool saved_non_integral_constant_expression_p;
8107 bool saved_greater_than_is_operator_p;
8109 location_t start_loc = token->location;
8111 cp_lexer_consume_token (parser->lexer);
8112 matching_parens parens;
8113 parens.require_open (parser);
8115 saved_message = parser->type_definition_forbidden_message;
8116 parser->type_definition_forbidden_message
8117 = G_("types may not be defined in %<noexcept%> expressions");
8119 saved_integral_constant_expression_p
8120 = parser->integral_constant_expression_p;
8121 saved_non_integral_constant_expression_p
8122 = parser->non_integral_constant_expression_p;
8123 parser->integral_constant_expression_p = false;
8125 saved_greater_than_is_operator_p
8126 = parser->greater_than_is_operator_p;
8127 parser->greater_than_is_operator_p = true;
8129 ++cp_unevaluated_operand;
8130 ++c_inhibit_evaluation_warnings;
8131 ++cp_noexcept_operand;
8132 expr = cp_parser_expression (parser);
8133 --cp_noexcept_operand;
8134 --c_inhibit_evaluation_warnings;
8135 --cp_unevaluated_operand;
8137 parser->greater_than_is_operator_p
8138 = saved_greater_than_is_operator_p;
8140 parser->integral_constant_expression_p
8141 = saved_integral_constant_expression_p;
8142 parser->non_integral_constant_expression_p
8143 = saved_non_integral_constant_expression_p;
8145 parser->type_definition_forbidden_message = saved_message;
8147 location_t finish_loc
8148 = cp_lexer_peek_token (parser->lexer)->location;
8149 parens.require_close (parser);
8151 /* Construct a location of the form:
8152 noexcept (expr)
8153 ^~~~~~~~~~~~~~~
8154 with start == caret, finishing at the close-paren. */
8155 location_t noexcept_loc
8156 = make_location (start_loc, start_loc, finish_loc);
8158 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8159 noexcept_loc);
8162 default:
8163 break;
8167 /* Look for the `:: new' and `:: delete', which also signal the
8168 beginning of a new-expression, or delete-expression,
8169 respectively. If the next token is `::', then it might be one of
8170 these. */
8171 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8173 enum rid keyword;
8175 /* See if the token after the `::' is one of the keywords in
8176 which we're interested. */
8177 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8178 /* If it's `new', we have a new-expression. */
8179 if (keyword == RID_NEW)
8180 return cp_parser_new_expression (parser);
8181 /* Similarly, for `delete'. */
8182 else if (keyword == RID_DELETE)
8183 return cp_parser_delete_expression (parser);
8186 /* Look for a unary operator. */
8187 unary_operator = cp_parser_unary_operator (token);
8188 /* The `++' and `--' operators can be handled similarly, even though
8189 they are not technically unary-operators in the grammar. */
8190 if (unary_operator == ERROR_MARK)
8192 if (token->type == CPP_PLUS_PLUS)
8193 unary_operator = PREINCREMENT_EXPR;
8194 else if (token->type == CPP_MINUS_MINUS)
8195 unary_operator = PREDECREMENT_EXPR;
8196 /* Handle the GNU address-of-label extension. */
8197 else if (cp_parser_allow_gnu_extensions_p (parser)
8198 && token->type == CPP_AND_AND)
8200 tree identifier;
8201 tree expression;
8202 location_t start_loc = token->location;
8204 /* Consume the '&&' token. */
8205 cp_lexer_consume_token (parser->lexer);
8206 /* Look for the identifier. */
8207 location_t finish_loc
8208 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8209 identifier = cp_parser_identifier (parser);
8210 /* Construct a location of the form:
8211 &&label
8212 ^~~~~~~
8213 with caret==start at the "&&", finish at the end of the label. */
8214 location_t combined_loc
8215 = make_location (start_loc, start_loc, finish_loc);
8216 /* Create an expression representing the address. */
8217 expression = finish_label_address_expr (identifier, combined_loc);
8218 if (cp_parser_non_integral_constant_expression (parser,
8219 NIC_ADDR_LABEL))
8220 expression = error_mark_node;
8221 return expression;
8224 if (unary_operator != ERROR_MARK)
8226 cp_expr cast_expression;
8227 cp_expr expression = error_mark_node;
8228 non_integral_constant non_constant_p = NIC_NONE;
8229 location_t loc = token->location;
8230 tsubst_flags_t complain = complain_flags (decltype_p);
8232 /* Consume the operator token. */
8233 token = cp_lexer_consume_token (parser->lexer);
8234 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8236 /* Parse the cast-expression. */
8237 cast_expression
8238 = cp_parser_cast_expression (parser,
8239 unary_operator == ADDR_EXPR,
8240 /*cast_p=*/false,
8241 /*decltype*/false,
8242 pidk);
8244 /* Make a location:
8245 OP_TOKEN CAST_EXPRESSION
8246 ^~~~~~~~~~~~~~~~~~~~~~~~~
8247 with start==caret at the operator token, and
8248 extending to the end of the cast_expression. */
8249 loc = make_location (loc, loc, cast_expression.get_finish ());
8251 /* Now, build an appropriate representation. */
8252 switch (unary_operator)
8254 case INDIRECT_REF:
8255 non_constant_p = NIC_STAR;
8256 expression = build_x_indirect_ref (loc, cast_expression,
8257 RO_UNARY_STAR,
8258 complain);
8259 /* TODO: build_x_indirect_ref does not always honor the
8260 location, so ensure it is set. */
8261 expression.set_location (loc);
8262 break;
8264 case ADDR_EXPR:
8265 non_constant_p = NIC_ADDR;
8266 /* Fall through. */
8267 case BIT_NOT_EXPR:
8268 expression = build_x_unary_op (loc, unary_operator,
8269 cast_expression,
8270 complain);
8271 /* TODO: build_x_unary_op does not always honor the location,
8272 so ensure it is set. */
8273 expression.set_location (loc);
8274 break;
8276 case PREINCREMENT_EXPR:
8277 case PREDECREMENT_EXPR:
8278 non_constant_p = unary_operator == PREINCREMENT_EXPR
8279 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8280 /* Fall through. */
8281 case NEGATE_EXPR:
8282 /* Immediately fold negation of a constant, unless the constant is 0
8283 (since -0 == 0) or it would overflow. */
8284 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8285 && CONSTANT_CLASS_P (cast_expression)
8286 && !integer_zerop (cast_expression)
8287 && !TREE_OVERFLOW (cast_expression))
8289 tree folded = fold_build1 (unary_operator,
8290 TREE_TYPE (cast_expression),
8291 cast_expression);
8292 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8294 expression = cp_expr (folded, loc);
8295 break;
8298 /* Fall through. */
8299 case UNARY_PLUS_EXPR:
8300 case TRUTH_NOT_EXPR:
8301 expression = finish_unary_op_expr (loc, unary_operator,
8302 cast_expression, complain);
8303 break;
8305 default:
8306 gcc_unreachable ();
8309 if (non_constant_p != NIC_NONE
8310 && cp_parser_non_integral_constant_expression (parser,
8311 non_constant_p))
8312 expression = error_mark_node;
8314 return expression;
8317 return cp_parser_postfix_expression (parser, address_p, cast_p,
8318 /*member_access_only_p=*/false,
8319 decltype_p,
8320 pidk);
8323 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8324 unary-operator, the corresponding tree code is returned. */
8326 static enum tree_code
8327 cp_parser_unary_operator (cp_token* token)
8329 switch (token->type)
8331 case CPP_MULT:
8332 return INDIRECT_REF;
8334 case CPP_AND:
8335 return ADDR_EXPR;
8337 case CPP_PLUS:
8338 return UNARY_PLUS_EXPR;
8340 case CPP_MINUS:
8341 return NEGATE_EXPR;
8343 case CPP_NOT:
8344 return TRUTH_NOT_EXPR;
8346 case CPP_COMPL:
8347 return BIT_NOT_EXPR;
8349 default:
8350 return ERROR_MARK;
8354 /* Parse a new-expression.
8356 new-expression:
8357 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8358 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8360 Returns a representation of the expression. */
8362 static tree
8363 cp_parser_new_expression (cp_parser* parser)
8365 bool global_scope_p;
8366 vec<tree, va_gc> *placement;
8367 tree type;
8368 vec<tree, va_gc> *initializer;
8369 tree nelts = NULL_TREE;
8370 tree ret;
8372 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8374 /* Look for the optional `::' operator. */
8375 global_scope_p
8376 = (cp_parser_global_scope_opt (parser,
8377 /*current_scope_valid_p=*/false)
8378 != NULL_TREE);
8379 /* Look for the `new' operator. */
8380 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8381 /* There's no easy way to tell a new-placement from the
8382 `( type-id )' construct. */
8383 cp_parser_parse_tentatively (parser);
8384 /* Look for a new-placement. */
8385 placement = cp_parser_new_placement (parser);
8386 /* If that didn't work out, there's no new-placement. */
8387 if (!cp_parser_parse_definitely (parser))
8389 if (placement != NULL)
8390 release_tree_vector (placement);
8391 placement = NULL;
8394 /* If the next token is a `(', then we have a parenthesized
8395 type-id. */
8396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8398 cp_token *token;
8399 const char *saved_message = parser->type_definition_forbidden_message;
8401 /* Consume the `('. */
8402 matching_parens parens;
8403 parens.consume_open (parser);
8405 /* Parse the type-id. */
8406 parser->type_definition_forbidden_message
8407 = G_("types may not be defined in a new-expression");
8409 type_id_in_expr_sentinel s (parser);
8410 type = cp_parser_type_id (parser);
8412 parser->type_definition_forbidden_message = saved_message;
8414 /* Look for the closing `)'. */
8415 parens.require_close (parser);
8416 token = cp_lexer_peek_token (parser->lexer);
8417 /* There should not be a direct-new-declarator in this production,
8418 but GCC used to allowed this, so we check and emit a sensible error
8419 message for this case. */
8420 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8422 error_at (token->location,
8423 "array bound forbidden after parenthesized type-id");
8424 inform (token->location,
8425 "try removing the parentheses around the type-id");
8426 cp_parser_direct_new_declarator (parser);
8429 /* Otherwise, there must be a new-type-id. */
8430 else
8431 type = cp_parser_new_type_id (parser, &nelts);
8433 /* If the next token is a `(' or '{', then we have a new-initializer. */
8434 cp_token *token = cp_lexer_peek_token (parser->lexer);
8435 if (token->type == CPP_OPEN_PAREN
8436 || token->type == CPP_OPEN_BRACE)
8437 initializer = cp_parser_new_initializer (parser);
8438 else
8439 initializer = NULL;
8441 /* A new-expression may not appear in an integral constant
8442 expression. */
8443 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8444 ret = error_mark_node;
8445 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8446 of a new-type-id or type-id of a new-expression, the new-expression shall
8447 contain a new-initializer of the form ( assignment-expression )".
8448 Additionally, consistently with the spirit of DR 1467, we want to accept
8449 'new auto { 2 }' too. */
8450 else if ((ret = type_uses_auto (type))
8451 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8452 && (vec_safe_length (initializer) != 1
8453 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8454 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8456 error_at (token->location,
8457 "initialization of new-expression for type %<auto%> "
8458 "requires exactly one element");
8459 ret = error_mark_node;
8461 else
8463 /* Construct a location e.g.:
8464 ptr = new int[100]
8465 ^~~~~~~~~~~~
8466 with caret == start at the start of the "new" token, and the end
8467 at the end of the final token we consumed. */
8468 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8469 location_t end_loc = get_finish (end_tok->location);
8470 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8472 /* Create a representation of the new-expression. */
8473 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8474 tf_warning_or_error);
8475 protected_set_expr_location (ret, combined_loc);
8478 if (placement != NULL)
8479 release_tree_vector (placement);
8480 if (initializer != NULL)
8481 release_tree_vector (initializer);
8483 return ret;
8486 /* Parse a new-placement.
8488 new-placement:
8489 ( expression-list )
8491 Returns the same representation as for an expression-list. */
8493 static vec<tree, va_gc> *
8494 cp_parser_new_placement (cp_parser* parser)
8496 vec<tree, va_gc> *expression_list;
8498 /* Parse the expression-list. */
8499 expression_list = (cp_parser_parenthesized_expression_list
8500 (parser, non_attr, /*cast_p=*/false,
8501 /*allow_expansion_p=*/true,
8502 /*non_constant_p=*/NULL));
8504 if (expression_list && expression_list->is_empty ())
8505 error ("expected expression-list or type-id");
8507 return expression_list;
8510 /* Parse a new-type-id.
8512 new-type-id:
8513 type-specifier-seq new-declarator [opt]
8515 Returns the TYPE allocated. If the new-type-id indicates an array
8516 type, *NELTS is set to the number of elements in the last array
8517 bound; the TYPE will not include the last array bound. */
8519 static tree
8520 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8522 cp_decl_specifier_seq type_specifier_seq;
8523 cp_declarator *new_declarator;
8524 cp_declarator *declarator;
8525 cp_declarator *outer_declarator;
8526 const char *saved_message;
8528 /* The type-specifier sequence must not contain type definitions.
8529 (It cannot contain declarations of new types either, but if they
8530 are not definitions we will catch that because they are not
8531 complete.) */
8532 saved_message = parser->type_definition_forbidden_message;
8533 parser->type_definition_forbidden_message
8534 = G_("types may not be defined in a new-type-id");
8535 /* Parse the type-specifier-seq. */
8536 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8537 /*is_trailing_return=*/false,
8538 &type_specifier_seq);
8539 /* Restore the old message. */
8540 parser->type_definition_forbidden_message = saved_message;
8542 if (type_specifier_seq.type == error_mark_node)
8543 return error_mark_node;
8545 /* Parse the new-declarator. */
8546 new_declarator = cp_parser_new_declarator_opt (parser);
8548 /* Determine the number of elements in the last array dimension, if
8549 any. */
8550 *nelts = NULL_TREE;
8551 /* Skip down to the last array dimension. */
8552 declarator = new_declarator;
8553 outer_declarator = NULL;
8554 while (declarator && (declarator->kind == cdk_pointer
8555 || declarator->kind == cdk_ptrmem))
8557 outer_declarator = declarator;
8558 declarator = declarator->declarator;
8560 while (declarator
8561 && declarator->kind == cdk_array
8562 && declarator->declarator
8563 && declarator->declarator->kind == cdk_array)
8565 outer_declarator = declarator;
8566 declarator = declarator->declarator;
8569 if (declarator && declarator->kind == cdk_array)
8571 *nelts = declarator->u.array.bounds;
8572 if (*nelts == error_mark_node)
8573 *nelts = integer_one_node;
8575 if (outer_declarator)
8576 outer_declarator->declarator = declarator->declarator;
8577 else
8578 new_declarator = NULL;
8581 return groktypename (&type_specifier_seq, new_declarator, false);
8584 /* Parse an (optional) new-declarator.
8586 new-declarator:
8587 ptr-operator new-declarator [opt]
8588 direct-new-declarator
8590 Returns the declarator. */
8592 static cp_declarator *
8593 cp_parser_new_declarator_opt (cp_parser* parser)
8595 enum tree_code code;
8596 tree type, std_attributes = NULL_TREE;
8597 cp_cv_quals cv_quals;
8599 /* We don't know if there's a ptr-operator next, or not. */
8600 cp_parser_parse_tentatively (parser);
8601 /* Look for a ptr-operator. */
8602 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8603 /* If that worked, look for more new-declarators. */
8604 if (cp_parser_parse_definitely (parser))
8606 cp_declarator *declarator;
8608 /* Parse another optional declarator. */
8609 declarator = cp_parser_new_declarator_opt (parser);
8611 declarator = cp_parser_make_indirect_declarator
8612 (code, type, cv_quals, declarator, std_attributes);
8614 return declarator;
8617 /* If the next token is a `[', there is a direct-new-declarator. */
8618 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8619 return cp_parser_direct_new_declarator (parser);
8621 return NULL;
8624 /* Parse a direct-new-declarator.
8626 direct-new-declarator:
8627 [ expression ]
8628 direct-new-declarator [constant-expression]
8632 static cp_declarator *
8633 cp_parser_direct_new_declarator (cp_parser* parser)
8635 cp_declarator *declarator = NULL;
8637 while (true)
8639 tree expression;
8640 cp_token *token;
8642 /* Look for the opening `['. */
8643 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8645 token = cp_lexer_peek_token (parser->lexer);
8646 expression = cp_parser_expression (parser);
8647 /* The standard requires that the expression have integral
8648 type. DR 74 adds enumeration types. We believe that the
8649 real intent is that these expressions be handled like the
8650 expression in a `switch' condition, which also allows
8651 classes with a single conversion to integral or
8652 enumeration type. */
8653 if (!processing_template_decl)
8655 expression
8656 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8657 expression,
8658 /*complain=*/true);
8659 if (!expression)
8661 error_at (token->location,
8662 "expression in new-declarator must have integral "
8663 "or enumeration type");
8664 expression = error_mark_node;
8668 /* Look for the closing `]'. */
8669 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8671 /* Add this bound to the declarator. */
8672 declarator = make_array_declarator (declarator, expression);
8674 /* If the next token is not a `[', then there are no more
8675 bounds. */
8676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8677 break;
8680 return declarator;
8683 /* Parse a new-initializer.
8685 new-initializer:
8686 ( expression-list [opt] )
8687 braced-init-list
8689 Returns a representation of the expression-list. */
8691 static vec<tree, va_gc> *
8692 cp_parser_new_initializer (cp_parser* parser)
8694 vec<tree, va_gc> *expression_list;
8696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8698 tree t;
8699 bool expr_non_constant_p;
8700 cp_lexer_set_source_position (parser->lexer);
8701 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8702 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8703 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8704 expression_list = make_tree_vector_single (t);
8706 else
8707 expression_list = (cp_parser_parenthesized_expression_list
8708 (parser, non_attr, /*cast_p=*/false,
8709 /*allow_expansion_p=*/true,
8710 /*non_constant_p=*/NULL));
8712 return expression_list;
8715 /* Parse a delete-expression.
8717 delete-expression:
8718 :: [opt] delete cast-expression
8719 :: [opt] delete [ ] cast-expression
8721 Returns a representation of the expression. */
8723 static tree
8724 cp_parser_delete_expression (cp_parser* parser)
8726 bool global_scope_p;
8727 bool array_p;
8728 tree expression;
8730 /* Look for the optional `::' operator. */
8731 global_scope_p
8732 = (cp_parser_global_scope_opt (parser,
8733 /*current_scope_valid_p=*/false)
8734 != NULL_TREE);
8735 /* Look for the `delete' keyword. */
8736 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8737 /* See if the array syntax is in use. */
8738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8740 /* Consume the `[' token. */
8741 cp_lexer_consume_token (parser->lexer);
8742 /* Look for the `]' token. */
8743 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8744 /* Remember that this is the `[]' construct. */
8745 array_p = true;
8747 else
8748 array_p = false;
8750 /* Parse the cast-expression. */
8751 expression = cp_parser_simple_cast_expression (parser);
8753 /* A delete-expression may not appear in an integral constant
8754 expression. */
8755 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8756 return error_mark_node;
8758 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8759 tf_warning_or_error);
8762 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8763 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8764 0 otherwise. */
8766 static int
8767 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8769 cp_token *token = cp_lexer_peek_token (parser->lexer);
8770 switch (token->type)
8772 case CPP_COMMA:
8773 case CPP_SEMICOLON:
8774 case CPP_QUERY:
8775 case CPP_COLON:
8776 case CPP_CLOSE_SQUARE:
8777 case CPP_CLOSE_PAREN:
8778 case CPP_CLOSE_BRACE:
8779 case CPP_OPEN_BRACE:
8780 case CPP_DOT:
8781 case CPP_DOT_STAR:
8782 case CPP_DEREF:
8783 case CPP_DEREF_STAR:
8784 case CPP_DIV:
8785 case CPP_MOD:
8786 case CPP_LSHIFT:
8787 case CPP_RSHIFT:
8788 case CPP_LESS:
8789 case CPP_GREATER:
8790 case CPP_LESS_EQ:
8791 case CPP_GREATER_EQ:
8792 case CPP_EQ_EQ:
8793 case CPP_NOT_EQ:
8794 case CPP_EQ:
8795 case CPP_MULT_EQ:
8796 case CPP_DIV_EQ:
8797 case CPP_MOD_EQ:
8798 case CPP_PLUS_EQ:
8799 case CPP_MINUS_EQ:
8800 case CPP_RSHIFT_EQ:
8801 case CPP_LSHIFT_EQ:
8802 case CPP_AND_EQ:
8803 case CPP_XOR_EQ:
8804 case CPP_OR_EQ:
8805 case CPP_XOR:
8806 case CPP_OR:
8807 case CPP_OR_OR:
8808 case CPP_EOF:
8809 case CPP_ELLIPSIS:
8810 return 0;
8812 case CPP_OPEN_PAREN:
8813 /* In ((type ()) () the last () isn't a valid cast-expression,
8814 so the whole must be parsed as postfix-expression. */
8815 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8816 != CPP_CLOSE_PAREN;
8818 case CPP_OPEN_SQUARE:
8819 /* '[' may start a primary-expression in obj-c++ and in C++11,
8820 as a lambda-expression, eg, '(void)[]{}'. */
8821 if (cxx_dialect >= cxx11)
8822 return -1;
8823 return c_dialect_objc ();
8825 case CPP_PLUS_PLUS:
8826 case CPP_MINUS_MINUS:
8827 /* '++' and '--' may or may not start a cast-expression:
8829 struct T { void operator++(int); };
8830 void f() { (T())++; }
8834 int a;
8835 (int)++a; */
8836 return -1;
8838 default:
8839 return 1;
8843 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8844 in the order: const_cast, static_cast, reinterpret_cast.
8846 Don't suggest dynamic_cast.
8848 Return the first legal cast kind found, or NULL otherwise. */
8850 static const char *
8851 get_cast_suggestion (tree dst_type, tree orig_expr)
8853 tree trial;
8855 /* Reuse the parser logic by attempting to build the various kinds of
8856 cast, with "complain" disabled.
8857 Identify the first such cast that is valid. */
8859 /* Don't attempt to run such logic within template processing. */
8860 if (processing_template_decl)
8861 return NULL;
8863 /* First try const_cast. */
8864 trial = build_const_cast (dst_type, orig_expr, tf_none);
8865 if (trial != error_mark_node)
8866 return "const_cast";
8868 /* If that fails, try static_cast. */
8869 trial = build_static_cast (dst_type, orig_expr, tf_none);
8870 if (trial != error_mark_node)
8871 return "static_cast";
8873 /* Finally, try reinterpret_cast. */
8874 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8875 if (trial != error_mark_node)
8876 return "reinterpret_cast";
8878 /* No such cast possible. */
8879 return NULL;
8882 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8883 suggesting how to convert a C-style cast of the form:
8885 (DST_TYPE)ORIG_EXPR
8887 to a C++-style cast.
8889 The primary range of RICHLOC is asssumed to be that of the original
8890 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8891 of the parens in the C-style cast. */
8893 static void
8894 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8895 location_t close_paren_loc, tree orig_expr,
8896 tree dst_type)
8898 /* This function is non-trivial, so bail out now if the warning isn't
8899 going to be emitted. */
8900 if (!warn_old_style_cast)
8901 return;
8903 /* Try to find a legal C++ cast, trying them in order:
8904 const_cast, static_cast, reinterpret_cast. */
8905 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8906 if (!cast_suggestion)
8907 return;
8909 /* Replace the open paren with "CAST_SUGGESTION<". */
8910 pretty_printer pp;
8911 pp_printf (&pp, "%s<", cast_suggestion);
8912 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8914 /* Replace the close paren with "> (". */
8915 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8917 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8918 rich_loc->add_fixit_insert_after (")");
8922 /* Parse a cast-expression.
8924 cast-expression:
8925 unary-expression
8926 ( type-id ) cast-expression
8928 ADDRESS_P is true iff the unary-expression is appearing as the
8929 operand of the `&' operator. CAST_P is true if this expression is
8930 the target of a cast.
8932 Returns a representation of the expression. */
8934 static cp_expr
8935 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8936 bool decltype_p, cp_id_kind * pidk)
8938 /* If it's a `(', then we might be looking at a cast. */
8939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8941 tree type = NULL_TREE;
8942 cp_expr expr (NULL_TREE);
8943 int cast_expression = 0;
8944 const char *saved_message;
8946 /* There's no way to know yet whether or not this is a cast.
8947 For example, `(int (3))' is a unary-expression, while `(int)
8948 3' is a cast. So, we resort to parsing tentatively. */
8949 cp_parser_parse_tentatively (parser);
8950 /* Types may not be defined in a cast. */
8951 saved_message = parser->type_definition_forbidden_message;
8952 parser->type_definition_forbidden_message
8953 = G_("types may not be defined in casts");
8954 /* Consume the `('. */
8955 matching_parens parens;
8956 cp_token *open_paren = parens.consume_open (parser);
8957 location_t open_paren_loc = open_paren->location;
8958 location_t close_paren_loc = UNKNOWN_LOCATION;
8960 /* A very tricky bit is that `(struct S) { 3 }' is a
8961 compound-literal (which we permit in C++ as an extension).
8962 But, that construct is not a cast-expression -- it is a
8963 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8964 is legal; if the compound-literal were a cast-expression,
8965 you'd need an extra set of parentheses.) But, if we parse
8966 the type-id, and it happens to be a class-specifier, then we
8967 will commit to the parse at that point, because we cannot
8968 undo the action that is done when creating a new class. So,
8969 then we cannot back up and do a postfix-expression.
8971 Another tricky case is the following (c++/29234):
8973 struct S { void operator () (); };
8975 void foo ()
8977 ( S()() );
8980 As a type-id we parse the parenthesized S()() as a function
8981 returning a function, groktypename complains and we cannot
8982 back up in this case either.
8984 Therefore, we scan ahead to the closing `)', and check to see
8985 if the tokens after the `)' can start a cast-expression. Otherwise
8986 we are dealing with an unary-expression, a postfix-expression
8987 or something else.
8989 Yet another tricky case, in C++11, is the following (c++/54891):
8991 (void)[]{};
8993 The issue is that usually, besides the case of lambda-expressions,
8994 the parenthesized type-id cannot be followed by '[', and, eg, we
8995 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8996 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8997 we don't commit, we try a cast-expression, then an unary-expression.
8999 Save tokens so that we can put them back. */
9000 cp_lexer_save_tokens (parser->lexer);
9002 /* We may be looking at a cast-expression. */
9003 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9004 /*consume_paren=*/true))
9005 cast_expression
9006 = cp_parser_tokens_start_cast_expression (parser);
9008 /* Roll back the tokens we skipped. */
9009 cp_lexer_rollback_tokens (parser->lexer);
9010 /* If we aren't looking at a cast-expression, simulate an error so
9011 that the call to cp_parser_error_occurred below returns true. */
9012 if (!cast_expression)
9013 cp_parser_simulate_error (parser);
9014 else
9016 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9017 parser->in_type_id_in_expr_p = true;
9018 /* Look for the type-id. */
9019 type = cp_parser_type_id (parser);
9020 /* Look for the closing `)'. */
9021 cp_token *close_paren = parens.require_close (parser);
9022 if (close_paren)
9023 close_paren_loc = close_paren->location;
9024 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9027 /* Restore the saved message. */
9028 parser->type_definition_forbidden_message = saved_message;
9030 /* At this point this can only be either a cast or a
9031 parenthesized ctor such as `(T ())' that looks like a cast to
9032 function returning T. */
9033 if (!cp_parser_error_occurred (parser))
9035 /* Only commit if the cast-expression doesn't start with
9036 '++', '--', or '[' in C++11. */
9037 if (cast_expression > 0)
9038 cp_parser_commit_to_topmost_tentative_parse (parser);
9040 expr = cp_parser_cast_expression (parser,
9041 /*address_p=*/false,
9042 /*cast_p=*/true,
9043 /*decltype_p=*/false,
9044 pidk);
9046 if (cp_parser_parse_definitely (parser))
9048 /* Warn about old-style casts, if so requested. */
9049 if (warn_old_style_cast
9050 && !in_system_header_at (input_location)
9051 && !VOID_TYPE_P (type)
9052 && current_lang_name != lang_name_c)
9054 gcc_rich_location rich_loc (input_location);
9055 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9056 expr, type);
9057 warning_at (&rich_loc, OPT_Wold_style_cast,
9058 "use of old-style cast to %q#T", type);
9061 /* Only type conversions to integral or enumeration types
9062 can be used in constant-expressions. */
9063 if (!cast_valid_in_integral_constant_expression_p (type)
9064 && cp_parser_non_integral_constant_expression (parser,
9065 NIC_CAST))
9066 return error_mark_node;
9068 /* Perform the cast. */
9069 /* Make a location:
9070 (TYPE) EXPR
9071 ^~~~~~~~~~~
9072 with start==caret at the open paren, extending to the
9073 end of "expr". */
9074 location_t cast_loc = make_location (open_paren_loc,
9075 open_paren_loc,
9076 expr.get_finish ());
9077 expr = build_c_cast (cast_loc, type, expr);
9078 return expr;
9081 else
9082 cp_parser_abort_tentative_parse (parser);
9085 /* If we get here, then it's not a cast, so it must be a
9086 unary-expression. */
9087 return cp_parser_unary_expression (parser, pidk, address_p,
9088 cast_p, decltype_p);
9091 /* Parse a binary expression of the general form:
9093 pm-expression:
9094 cast-expression
9095 pm-expression .* cast-expression
9096 pm-expression ->* cast-expression
9098 multiplicative-expression:
9099 pm-expression
9100 multiplicative-expression * pm-expression
9101 multiplicative-expression / pm-expression
9102 multiplicative-expression % pm-expression
9104 additive-expression:
9105 multiplicative-expression
9106 additive-expression + multiplicative-expression
9107 additive-expression - multiplicative-expression
9109 shift-expression:
9110 additive-expression
9111 shift-expression << additive-expression
9112 shift-expression >> additive-expression
9114 relational-expression:
9115 shift-expression
9116 relational-expression < shift-expression
9117 relational-expression > shift-expression
9118 relational-expression <= shift-expression
9119 relational-expression >= shift-expression
9121 GNU Extension:
9123 relational-expression:
9124 relational-expression <? shift-expression
9125 relational-expression >? shift-expression
9127 equality-expression:
9128 relational-expression
9129 equality-expression == relational-expression
9130 equality-expression != relational-expression
9132 and-expression:
9133 equality-expression
9134 and-expression & equality-expression
9136 exclusive-or-expression:
9137 and-expression
9138 exclusive-or-expression ^ and-expression
9140 inclusive-or-expression:
9141 exclusive-or-expression
9142 inclusive-or-expression | exclusive-or-expression
9144 logical-and-expression:
9145 inclusive-or-expression
9146 logical-and-expression && inclusive-or-expression
9148 logical-or-expression:
9149 logical-and-expression
9150 logical-or-expression || logical-and-expression
9152 All these are implemented with a single function like:
9154 binary-expression:
9155 simple-cast-expression
9156 binary-expression <token> binary-expression
9158 CAST_P is true if this expression is the target of a cast.
9160 The binops_by_token map is used to get the tree codes for each <token> type.
9161 binary-expressions are associated according to a precedence table. */
9163 #define TOKEN_PRECEDENCE(token) \
9164 (((token->type == CPP_GREATER \
9165 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9166 && !parser->greater_than_is_operator_p) \
9167 ? PREC_NOT_OPERATOR \
9168 : binops_by_token[token->type].prec)
9170 static cp_expr
9171 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9172 bool no_toplevel_fold_p,
9173 bool decltype_p,
9174 enum cp_parser_prec prec,
9175 cp_id_kind * pidk)
9177 cp_parser_expression_stack stack;
9178 cp_parser_expression_stack_entry *sp = &stack[0];
9179 cp_parser_expression_stack_entry current;
9180 cp_expr rhs;
9181 cp_token *token;
9182 enum tree_code rhs_type;
9183 enum cp_parser_prec new_prec, lookahead_prec;
9184 tree overload;
9186 /* Parse the first expression. */
9187 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9188 ? TRUTH_NOT_EXPR : ERROR_MARK);
9189 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9190 cast_p, decltype_p, pidk);
9191 current.prec = prec;
9193 if (cp_parser_error_occurred (parser))
9194 return error_mark_node;
9196 for (;;)
9198 /* Get an operator token. */
9199 token = cp_lexer_peek_token (parser->lexer);
9201 if (warn_cxx11_compat
9202 && token->type == CPP_RSHIFT
9203 && !parser->greater_than_is_operator_p)
9205 if (warning_at (token->location, OPT_Wc__11_compat,
9206 "%<>>%> operator is treated"
9207 " as two right angle brackets in C++11"))
9208 inform (token->location,
9209 "suggest parentheses around %<>>%> expression");
9212 new_prec = TOKEN_PRECEDENCE (token);
9213 if (new_prec != PREC_NOT_OPERATOR
9214 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9215 /* This is a fold-expression; handle it later. */
9216 new_prec = PREC_NOT_OPERATOR;
9218 /* Popping an entry off the stack means we completed a subexpression:
9219 - either we found a token which is not an operator (`>' where it is not
9220 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9221 will happen repeatedly;
9222 - or, we found an operator which has lower priority. This is the case
9223 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9224 parsing `3 * 4'. */
9225 if (new_prec <= current.prec)
9227 if (sp == stack)
9228 break;
9229 else
9230 goto pop;
9233 get_rhs:
9234 current.tree_type = binops_by_token[token->type].tree_type;
9235 current.loc = token->location;
9237 /* We used the operator token. */
9238 cp_lexer_consume_token (parser->lexer);
9240 /* For "false && x" or "true || x", x will never be executed;
9241 disable warnings while evaluating it. */
9242 if (current.tree_type == TRUTH_ANDIF_EXPR)
9243 c_inhibit_evaluation_warnings +=
9244 cp_fully_fold (current.lhs) == truthvalue_false_node;
9245 else if (current.tree_type == TRUTH_ORIF_EXPR)
9246 c_inhibit_evaluation_warnings +=
9247 cp_fully_fold (current.lhs) == truthvalue_true_node;
9249 /* Extract another operand. It may be the RHS of this expression
9250 or the LHS of a new, higher priority expression. */
9251 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9252 ? TRUTH_NOT_EXPR : ERROR_MARK);
9253 rhs = cp_parser_simple_cast_expression (parser);
9255 /* Get another operator token. Look up its precedence to avoid
9256 building a useless (immediately popped) stack entry for common
9257 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9258 token = cp_lexer_peek_token (parser->lexer);
9259 lookahead_prec = TOKEN_PRECEDENCE (token);
9260 if (lookahead_prec != PREC_NOT_OPERATOR
9261 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9262 lookahead_prec = PREC_NOT_OPERATOR;
9263 if (lookahead_prec > new_prec)
9265 /* ... and prepare to parse the RHS of the new, higher priority
9266 expression. Since precedence levels on the stack are
9267 monotonically increasing, we do not have to care about
9268 stack overflows. */
9269 *sp = current;
9270 ++sp;
9271 current.lhs = rhs;
9272 current.lhs_type = rhs_type;
9273 current.prec = new_prec;
9274 new_prec = lookahead_prec;
9275 goto get_rhs;
9277 pop:
9278 lookahead_prec = new_prec;
9279 /* If the stack is not empty, we have parsed into LHS the right side
9280 (`4' in the example above) of an expression we had suspended.
9281 We can use the information on the stack to recover the LHS (`3')
9282 from the stack together with the tree code (`MULT_EXPR'), and
9283 the precedence of the higher level subexpression
9284 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9285 which will be used to actually build the additive expression. */
9286 rhs = current.lhs;
9287 rhs_type = current.lhs_type;
9288 --sp;
9289 current = *sp;
9292 /* Undo the disabling of warnings done above. */
9293 if (current.tree_type == TRUTH_ANDIF_EXPR)
9294 c_inhibit_evaluation_warnings -=
9295 cp_fully_fold (current.lhs) == truthvalue_false_node;
9296 else if (current.tree_type == TRUTH_ORIF_EXPR)
9297 c_inhibit_evaluation_warnings -=
9298 cp_fully_fold (current.lhs) == truthvalue_true_node;
9300 if (warn_logical_not_paren
9301 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9302 && current.lhs_type == TRUTH_NOT_EXPR
9303 /* Avoid warning for !!x == y. */
9304 && (TREE_CODE (current.lhs) != NE_EXPR
9305 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9306 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9307 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9308 /* Avoid warning for !b == y where b is boolean. */
9309 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9310 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9311 != BOOLEAN_TYPE))))
9312 /* Avoid warning for !!b == y where b is boolean. */
9313 && (!DECL_P (current.lhs)
9314 || TREE_TYPE (current.lhs) == NULL_TREE
9315 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9316 warn_logical_not_parentheses (current.loc, current.tree_type,
9317 current.lhs, maybe_constant_value (rhs));
9319 overload = NULL;
9321 location_t combined_loc = make_location (current.loc,
9322 current.lhs.get_start (),
9323 rhs.get_finish ());
9325 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9326 ERROR_MARK for everything that is not a binary expression.
9327 This makes warn_about_parentheses miss some warnings that
9328 involve unary operators. For unary expressions we should
9329 pass the correct tree_code unless the unary expression was
9330 surrounded by parentheses.
9332 if (no_toplevel_fold_p
9333 && lookahead_prec <= current.prec
9334 && sp == stack)
9336 if (current.lhs == error_mark_node || rhs == error_mark_node)
9337 current.lhs = error_mark_node;
9338 else
9340 current.lhs
9341 = build_min (current.tree_type,
9342 TREE_CODE_CLASS (current.tree_type)
9343 == tcc_comparison
9344 ? boolean_type_node : TREE_TYPE (current.lhs),
9345 current.lhs.get_value (), rhs.get_value ());
9346 SET_EXPR_LOCATION (current.lhs, combined_loc);
9349 else
9351 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9352 current.lhs, current.lhs_type,
9353 rhs, rhs_type, &overload,
9354 complain_flags (decltype_p));
9355 /* TODO: build_x_binary_op doesn't always honor the location. */
9356 current.lhs.set_location (combined_loc);
9358 current.lhs_type = current.tree_type;
9360 /* If the binary operator required the use of an overloaded operator,
9361 then this expression cannot be an integral constant-expression.
9362 An overloaded operator can be used even if both operands are
9363 otherwise permissible in an integral constant-expression if at
9364 least one of the operands is of enumeration type. */
9366 if (overload
9367 && cp_parser_non_integral_constant_expression (parser,
9368 NIC_OVERLOADED))
9369 return error_mark_node;
9372 return current.lhs;
9375 static cp_expr
9376 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9377 bool no_toplevel_fold_p,
9378 enum cp_parser_prec prec,
9379 cp_id_kind * pidk)
9381 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9382 /*decltype*/false, prec, pidk);
9385 /* Parse the `? expression : assignment-expression' part of a
9386 conditional-expression. The LOGICAL_OR_EXPR is the
9387 logical-or-expression that started the conditional-expression.
9388 Returns a representation of the entire conditional-expression.
9390 This routine is used by cp_parser_assignment_expression.
9392 ? expression : assignment-expression
9394 GNU Extensions:
9396 ? : assignment-expression */
9398 static tree
9399 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9401 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9402 cp_expr assignment_expr;
9403 struct cp_token *token;
9404 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9406 /* Consume the `?' token. */
9407 cp_lexer_consume_token (parser->lexer);
9408 token = cp_lexer_peek_token (parser->lexer);
9409 if (cp_parser_allow_gnu_extensions_p (parser)
9410 && token->type == CPP_COLON)
9412 pedwarn (token->location, OPT_Wpedantic,
9413 "ISO C++ does not allow ?: with omitted middle operand");
9414 /* Implicit true clause. */
9415 expr = NULL_TREE;
9416 c_inhibit_evaluation_warnings +=
9417 folded_logical_or_expr == truthvalue_true_node;
9418 warn_for_omitted_condop (token->location, logical_or_expr);
9420 else
9422 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9423 parser->colon_corrects_to_scope_p = false;
9424 /* Parse the expression. */
9425 c_inhibit_evaluation_warnings +=
9426 folded_logical_or_expr == truthvalue_false_node;
9427 expr = cp_parser_expression (parser);
9428 c_inhibit_evaluation_warnings +=
9429 ((folded_logical_or_expr == truthvalue_true_node)
9430 - (folded_logical_or_expr == truthvalue_false_node));
9431 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9434 /* The next token should be a `:'. */
9435 cp_parser_require (parser, CPP_COLON, RT_COLON);
9436 /* Parse the assignment-expression. */
9437 assignment_expr = cp_parser_assignment_expression (parser);
9438 c_inhibit_evaluation_warnings -=
9439 folded_logical_or_expr == truthvalue_true_node;
9441 /* Make a location:
9442 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9443 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9444 with the caret at the "?", ranging from the start of
9445 the logical_or_expr to the end of the assignment_expr. */
9446 loc = make_location (loc,
9447 logical_or_expr.get_start (),
9448 assignment_expr.get_finish ());
9450 /* Build the conditional-expression. */
9451 return build_x_conditional_expr (loc, logical_or_expr,
9452 expr,
9453 assignment_expr,
9454 tf_warning_or_error);
9457 /* Parse an assignment-expression.
9459 assignment-expression:
9460 conditional-expression
9461 logical-or-expression assignment-operator assignment_expression
9462 throw-expression
9464 CAST_P is true if this expression is the target of a cast.
9465 DECLTYPE_P is true if this expression is the operand of decltype.
9467 Returns a representation for the expression. */
9469 static cp_expr
9470 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9471 bool cast_p, bool decltype_p)
9473 cp_expr expr;
9475 /* If the next token is the `throw' keyword, then we're looking at
9476 a throw-expression. */
9477 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9478 expr = cp_parser_throw_expression (parser);
9479 /* Otherwise, it must be that we are looking at a
9480 logical-or-expression. */
9481 else
9483 /* Parse the binary expressions (logical-or-expression). */
9484 expr = cp_parser_binary_expression (parser, cast_p, false,
9485 decltype_p,
9486 PREC_NOT_OPERATOR, pidk);
9487 /* If the next token is a `?' then we're actually looking at a
9488 conditional-expression. */
9489 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9490 return cp_parser_question_colon_clause (parser, expr);
9491 else
9493 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9495 /* If it's an assignment-operator, we're using the second
9496 production. */
9497 enum tree_code assignment_operator
9498 = cp_parser_assignment_operator_opt (parser);
9499 if (assignment_operator != ERROR_MARK)
9501 bool non_constant_p;
9503 /* Parse the right-hand side of the assignment. */
9504 cp_expr rhs = cp_parser_initializer_clause (parser,
9505 &non_constant_p);
9507 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9508 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9510 /* An assignment may not appear in a
9511 constant-expression. */
9512 if (cp_parser_non_integral_constant_expression (parser,
9513 NIC_ASSIGNMENT))
9514 return error_mark_node;
9515 /* Build the assignment expression. Its default
9516 location:
9517 LHS = RHS
9518 ~~~~^~~~~
9519 is the location of the '=' token as the
9520 caret, ranging from the start of the lhs to the
9521 end of the rhs. */
9522 loc = make_location (loc,
9523 expr.get_start (),
9524 rhs.get_finish ());
9525 expr = build_x_modify_expr (loc, expr,
9526 assignment_operator,
9527 rhs,
9528 complain_flags (decltype_p));
9529 /* TODO: build_x_modify_expr doesn't honor the location,
9530 so we must set it here. */
9531 expr.set_location (loc);
9536 return expr;
9539 /* Parse an (optional) assignment-operator.
9541 assignment-operator: one of
9542 = *= /= %= += -= >>= <<= &= ^= |=
9544 GNU Extension:
9546 assignment-operator: one of
9547 <?= >?=
9549 If the next token is an assignment operator, the corresponding tree
9550 code is returned, and the token is consumed. For example, for
9551 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9552 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9553 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9554 operator, ERROR_MARK is returned. */
9556 static enum tree_code
9557 cp_parser_assignment_operator_opt (cp_parser* parser)
9559 enum tree_code op;
9560 cp_token *token;
9562 /* Peek at the next token. */
9563 token = cp_lexer_peek_token (parser->lexer);
9565 switch (token->type)
9567 case CPP_EQ:
9568 op = NOP_EXPR;
9569 break;
9571 case CPP_MULT_EQ:
9572 op = MULT_EXPR;
9573 break;
9575 case CPP_DIV_EQ:
9576 op = TRUNC_DIV_EXPR;
9577 break;
9579 case CPP_MOD_EQ:
9580 op = TRUNC_MOD_EXPR;
9581 break;
9583 case CPP_PLUS_EQ:
9584 op = PLUS_EXPR;
9585 break;
9587 case CPP_MINUS_EQ:
9588 op = MINUS_EXPR;
9589 break;
9591 case CPP_RSHIFT_EQ:
9592 op = RSHIFT_EXPR;
9593 break;
9595 case CPP_LSHIFT_EQ:
9596 op = LSHIFT_EXPR;
9597 break;
9599 case CPP_AND_EQ:
9600 op = BIT_AND_EXPR;
9601 break;
9603 case CPP_XOR_EQ:
9604 op = BIT_XOR_EXPR;
9605 break;
9607 case CPP_OR_EQ:
9608 op = BIT_IOR_EXPR;
9609 break;
9611 default:
9612 /* Nothing else is an assignment operator. */
9613 op = ERROR_MARK;
9616 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9617 if (op != ERROR_MARK
9618 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9619 op = ERROR_MARK;
9621 /* If it was an assignment operator, consume it. */
9622 if (op != ERROR_MARK)
9623 cp_lexer_consume_token (parser->lexer);
9625 return op;
9628 /* Parse an expression.
9630 expression:
9631 assignment-expression
9632 expression , assignment-expression
9634 CAST_P is true if this expression is the target of a cast.
9635 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9636 except possibly parenthesized or on the RHS of a comma (N3276).
9638 Returns a representation of the expression. */
9640 static cp_expr
9641 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9642 bool cast_p, bool decltype_p)
9644 cp_expr expression = NULL_TREE;
9645 location_t loc = UNKNOWN_LOCATION;
9647 while (true)
9649 cp_expr assignment_expression;
9651 /* Parse the next assignment-expression. */
9652 assignment_expression
9653 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9655 /* We don't create a temporary for a call that is the immediate operand
9656 of decltype or on the RHS of a comma. But when we see a comma, we
9657 need to create a temporary for a call on the LHS. */
9658 if (decltype_p && !processing_template_decl
9659 && TREE_CODE (assignment_expression) == CALL_EXPR
9660 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9661 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9662 assignment_expression
9663 = build_cplus_new (TREE_TYPE (assignment_expression),
9664 assignment_expression, tf_warning_or_error);
9666 /* If this is the first assignment-expression, we can just
9667 save it away. */
9668 if (!expression)
9669 expression = assignment_expression;
9670 else
9672 /* Create a location with caret at the comma, ranging
9673 from the start of the LHS to the end of the RHS. */
9674 loc = make_location (loc,
9675 expression.get_start (),
9676 assignment_expression.get_finish ());
9677 expression = build_x_compound_expr (loc, expression,
9678 assignment_expression,
9679 complain_flags (decltype_p));
9680 expression.set_location (loc);
9682 /* If the next token is not a comma, or we're in a fold-expression, then
9683 we are done with the expression. */
9684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9685 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9686 break;
9687 /* Consume the `,'. */
9688 loc = cp_lexer_peek_token (parser->lexer)->location;
9689 cp_lexer_consume_token (parser->lexer);
9690 /* A comma operator cannot appear in a constant-expression. */
9691 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9692 expression = error_mark_node;
9695 return expression;
9698 /* Parse a constant-expression.
9700 constant-expression:
9701 conditional-expression
9703 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9704 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9705 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9706 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9707 only parse a conditional-expression, otherwise parse an
9708 assignment-expression. See below for rationale. */
9710 static cp_expr
9711 cp_parser_constant_expression (cp_parser* parser,
9712 bool allow_non_constant_p,
9713 bool *non_constant_p,
9714 bool strict_p)
9716 bool saved_integral_constant_expression_p;
9717 bool saved_allow_non_integral_constant_expression_p;
9718 bool saved_non_integral_constant_expression_p;
9719 cp_expr expression;
9721 /* It might seem that we could simply parse the
9722 conditional-expression, and then check to see if it were
9723 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9724 one that the compiler can figure out is constant, possibly after
9725 doing some simplifications or optimizations. The standard has a
9726 precise definition of constant-expression, and we must honor
9727 that, even though it is somewhat more restrictive.
9729 For example:
9731 int i[(2, 3)];
9733 is not a legal declaration, because `(2, 3)' is not a
9734 constant-expression. The `,' operator is forbidden in a
9735 constant-expression. However, GCC's constant-folding machinery
9736 will fold this operation to an INTEGER_CST for `3'. */
9738 /* Save the old settings. */
9739 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9740 saved_allow_non_integral_constant_expression_p
9741 = parser->allow_non_integral_constant_expression_p;
9742 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9743 /* We are now parsing a constant-expression. */
9744 parser->integral_constant_expression_p = true;
9745 parser->allow_non_integral_constant_expression_p
9746 = (allow_non_constant_p || cxx_dialect >= cxx11);
9747 parser->non_integral_constant_expression_p = false;
9748 /* Although the grammar says "conditional-expression", when not STRICT_P,
9749 we parse an "assignment-expression", which also permits
9750 "throw-expression" and the use of assignment operators. In the case
9751 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9752 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9753 actually essential that we look for an assignment-expression.
9754 For example, cp_parser_initializer_clauses uses this function to
9755 determine whether a particular assignment-expression is in fact
9756 constant. */
9757 if (strict_p)
9759 /* Parse the binary expressions (logical-or-expression). */
9760 expression = cp_parser_binary_expression (parser, false, false, false,
9761 PREC_NOT_OPERATOR, NULL);
9762 /* If the next token is a `?' then we're actually looking at
9763 a conditional-expression; otherwise we're done. */
9764 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9765 expression = cp_parser_question_colon_clause (parser, expression);
9767 else
9768 expression = cp_parser_assignment_expression (parser);
9769 /* Restore the old settings. */
9770 parser->integral_constant_expression_p
9771 = saved_integral_constant_expression_p;
9772 parser->allow_non_integral_constant_expression_p
9773 = saved_allow_non_integral_constant_expression_p;
9774 if (cxx_dialect >= cxx11)
9776 /* Require an rvalue constant expression here; that's what our
9777 callers expect. Reference constant expressions are handled
9778 separately in e.g. cp_parser_template_argument. */
9779 tree decay = expression;
9780 if (TREE_TYPE (expression)
9781 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9782 decay = build_address (expression);
9783 bool is_const = potential_rvalue_constant_expression (decay);
9784 parser->non_integral_constant_expression_p = !is_const;
9785 if (!is_const && !allow_non_constant_p)
9786 require_potential_rvalue_constant_expression (decay);
9788 if (allow_non_constant_p)
9789 *non_constant_p = parser->non_integral_constant_expression_p;
9790 parser->non_integral_constant_expression_p
9791 = saved_non_integral_constant_expression_p;
9793 return expression;
9796 /* Parse __builtin_offsetof.
9798 offsetof-expression:
9799 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9801 offsetof-member-designator:
9802 id-expression
9803 | offsetof-member-designator "." id-expression
9804 | offsetof-member-designator "[" expression "]"
9805 | offsetof-member-designator "->" id-expression */
9807 static cp_expr
9808 cp_parser_builtin_offsetof (cp_parser *parser)
9810 int save_ice_p, save_non_ice_p;
9811 tree type;
9812 cp_expr expr;
9813 cp_id_kind dummy;
9814 cp_token *token;
9815 location_t finish_loc;
9817 /* We're about to accept non-integral-constant things, but will
9818 definitely yield an integral constant expression. Save and
9819 restore these values around our local parsing. */
9820 save_ice_p = parser->integral_constant_expression_p;
9821 save_non_ice_p = parser->non_integral_constant_expression_p;
9823 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9825 /* Consume the "__builtin_offsetof" token. */
9826 cp_lexer_consume_token (parser->lexer);
9827 /* Consume the opening `('. */
9828 matching_parens parens;
9829 parens.require_open (parser);
9830 /* Parse the type-id. */
9831 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9832 type = cp_parser_type_id (parser);
9833 /* Look for the `,'. */
9834 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9835 token = cp_lexer_peek_token (parser->lexer);
9837 /* Build the (type *)null that begins the traditional offsetof macro. */
9838 tree object_ptr
9839 = build_static_cast (build_pointer_type (type), null_pointer_node,
9840 tf_warning_or_error);
9842 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9843 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9844 true, &dummy, token->location);
9845 while (true)
9847 token = cp_lexer_peek_token (parser->lexer);
9848 switch (token->type)
9850 case CPP_OPEN_SQUARE:
9851 /* offsetof-member-designator "[" expression "]" */
9852 expr = cp_parser_postfix_open_square_expression (parser, expr,
9853 true, false);
9854 break;
9856 case CPP_DEREF:
9857 /* offsetof-member-designator "->" identifier */
9858 expr = grok_array_decl (token->location, expr,
9859 integer_zero_node, false);
9860 /* FALLTHRU */
9862 case CPP_DOT:
9863 /* offsetof-member-designator "." identifier */
9864 cp_lexer_consume_token (parser->lexer);
9865 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9866 expr, true, &dummy,
9867 token->location);
9868 break;
9870 case CPP_CLOSE_PAREN:
9871 /* Consume the ")" token. */
9872 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9873 cp_lexer_consume_token (parser->lexer);
9874 goto success;
9876 default:
9877 /* Error. We know the following require will fail, but
9878 that gives the proper error message. */
9879 parens.require_close (parser);
9880 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9881 expr = error_mark_node;
9882 goto failure;
9886 success:
9887 /* Make a location of the form:
9888 __builtin_offsetof (struct s, f)
9889 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9890 with caret at the type-id, ranging from the start of the
9891 "_builtin_offsetof" token to the close paren. */
9892 loc = make_location (loc, start_loc, finish_loc);
9893 /* The result will be an INTEGER_CST, so we need to explicitly
9894 preserve the location. */
9895 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9897 failure:
9898 parser->integral_constant_expression_p = save_ice_p;
9899 parser->non_integral_constant_expression_p = save_non_ice_p;
9901 expr = expr.maybe_add_location_wrapper ();
9902 return expr;
9905 /* Parse a trait expression.
9907 Returns a representation of the expression, the underlying type
9908 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9910 static cp_expr
9911 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9913 cp_trait_kind kind;
9914 tree type1, type2 = NULL_TREE;
9915 bool binary = false;
9916 bool variadic = false;
9918 switch (keyword)
9920 case RID_HAS_NOTHROW_ASSIGN:
9921 kind = CPTK_HAS_NOTHROW_ASSIGN;
9922 break;
9923 case RID_HAS_NOTHROW_CONSTRUCTOR:
9924 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9925 break;
9926 case RID_HAS_NOTHROW_COPY:
9927 kind = CPTK_HAS_NOTHROW_COPY;
9928 break;
9929 case RID_HAS_TRIVIAL_ASSIGN:
9930 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9931 break;
9932 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9933 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9934 break;
9935 case RID_HAS_TRIVIAL_COPY:
9936 kind = CPTK_HAS_TRIVIAL_COPY;
9937 break;
9938 case RID_HAS_TRIVIAL_DESTRUCTOR:
9939 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9940 break;
9941 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9942 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9943 break;
9944 case RID_HAS_VIRTUAL_DESTRUCTOR:
9945 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9946 break;
9947 case RID_IS_ABSTRACT:
9948 kind = CPTK_IS_ABSTRACT;
9949 break;
9950 case RID_IS_AGGREGATE:
9951 kind = CPTK_IS_AGGREGATE;
9952 break;
9953 case RID_IS_BASE_OF:
9954 kind = CPTK_IS_BASE_OF;
9955 binary = true;
9956 break;
9957 case RID_IS_CLASS:
9958 kind = CPTK_IS_CLASS;
9959 break;
9960 case RID_IS_EMPTY:
9961 kind = CPTK_IS_EMPTY;
9962 break;
9963 case RID_IS_ENUM:
9964 kind = CPTK_IS_ENUM;
9965 break;
9966 case RID_IS_FINAL:
9967 kind = CPTK_IS_FINAL;
9968 break;
9969 case RID_IS_LITERAL_TYPE:
9970 kind = CPTK_IS_LITERAL_TYPE;
9971 break;
9972 case RID_IS_POD:
9973 kind = CPTK_IS_POD;
9974 break;
9975 case RID_IS_POLYMORPHIC:
9976 kind = CPTK_IS_POLYMORPHIC;
9977 break;
9978 case RID_IS_SAME_AS:
9979 kind = CPTK_IS_SAME_AS;
9980 binary = true;
9981 break;
9982 case RID_IS_STD_LAYOUT:
9983 kind = CPTK_IS_STD_LAYOUT;
9984 break;
9985 case RID_IS_TRIVIAL:
9986 kind = CPTK_IS_TRIVIAL;
9987 break;
9988 case RID_IS_TRIVIALLY_ASSIGNABLE:
9989 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9990 binary = true;
9991 break;
9992 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9993 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9994 variadic = true;
9995 break;
9996 case RID_IS_TRIVIALLY_COPYABLE:
9997 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9998 break;
9999 case RID_IS_UNION:
10000 kind = CPTK_IS_UNION;
10001 break;
10002 case RID_UNDERLYING_TYPE:
10003 kind = CPTK_UNDERLYING_TYPE;
10004 break;
10005 case RID_BASES:
10006 kind = CPTK_BASES;
10007 break;
10008 case RID_DIRECT_BASES:
10009 kind = CPTK_DIRECT_BASES;
10010 break;
10011 case RID_IS_ASSIGNABLE:
10012 kind = CPTK_IS_ASSIGNABLE;
10013 binary = true;
10014 break;
10015 case RID_IS_CONSTRUCTIBLE:
10016 kind = CPTK_IS_CONSTRUCTIBLE;
10017 variadic = true;
10018 break;
10019 default:
10020 gcc_unreachable ();
10023 /* Get location of initial token. */
10024 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10026 /* Consume the token. */
10027 cp_lexer_consume_token (parser->lexer);
10029 matching_parens parens;
10030 parens.require_open (parser);
10033 type_id_in_expr_sentinel s (parser);
10034 type1 = cp_parser_type_id (parser);
10037 if (type1 == error_mark_node)
10038 return error_mark_node;
10040 if (binary)
10042 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10045 type_id_in_expr_sentinel s (parser);
10046 type2 = cp_parser_type_id (parser);
10049 if (type2 == error_mark_node)
10050 return error_mark_node;
10052 else if (variadic)
10054 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10056 cp_lexer_consume_token (parser->lexer);
10057 tree elt = cp_parser_type_id (parser);
10058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10060 cp_lexer_consume_token (parser->lexer);
10061 elt = make_pack_expansion (elt);
10063 if (elt == error_mark_node)
10064 return error_mark_node;
10065 type2 = tree_cons (NULL_TREE, elt, type2);
10069 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10070 parens.require_close (parser);
10072 /* Construct a location of the form:
10073 __is_trivially_copyable(_Tp)
10074 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10075 with start == caret, finishing at the close-paren. */
10076 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10078 /* Complete the trait expression, which may mean either processing
10079 the trait expr now or saving it for template instantiation. */
10080 switch (kind)
10082 case CPTK_UNDERLYING_TYPE:
10083 return cp_expr (finish_underlying_type (type1), trait_loc);
10084 case CPTK_BASES:
10085 return cp_expr (finish_bases (type1, false), trait_loc);
10086 case CPTK_DIRECT_BASES:
10087 return cp_expr (finish_bases (type1, true), trait_loc);
10088 default:
10089 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10093 /* Parse a lambda expression.
10095 lambda-expression:
10096 lambda-introducer lambda-declarator [opt] compound-statement
10098 Returns a representation of the expression. */
10100 static cp_expr
10101 cp_parser_lambda_expression (cp_parser* parser)
10103 tree lambda_expr = build_lambda_expr ();
10104 tree type;
10105 bool ok = true;
10106 cp_token *token = cp_lexer_peek_token (parser->lexer);
10107 cp_token_position start = 0;
10109 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10111 if (cp_unevaluated_operand)
10113 if (!token->error_reported)
10115 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10116 "lambda-expression in unevaluated context");
10117 token->error_reported = true;
10119 ok = false;
10121 else if (parser->in_template_argument_list_p)
10123 if (!token->error_reported)
10125 error_at (token->location, "lambda-expression in template-argument");
10126 token->error_reported = true;
10128 ok = false;
10131 /* We may be in the middle of deferred access check. Disable
10132 it now. */
10133 push_deferring_access_checks (dk_no_deferred);
10135 cp_parser_lambda_introducer (parser, lambda_expr);
10137 type = begin_lambda_type (lambda_expr);
10138 if (type == error_mark_node)
10139 return error_mark_node;
10141 record_lambda_scope (lambda_expr);
10143 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10144 determine_visibility (TYPE_NAME (type));
10146 /* Now that we've started the type, add the capture fields for any
10147 explicit captures. */
10148 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10151 /* Inside the class, surrounding template-parameter-lists do not apply. */
10152 unsigned int saved_num_template_parameter_lists
10153 = parser->num_template_parameter_lists;
10154 unsigned char in_statement = parser->in_statement;
10155 bool in_switch_statement_p = parser->in_switch_statement_p;
10156 bool fully_implicit_function_template_p
10157 = parser->fully_implicit_function_template_p;
10158 tree implicit_template_parms = parser->implicit_template_parms;
10159 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10160 bool auto_is_implicit_function_template_parm_p
10161 = parser->auto_is_implicit_function_template_parm_p;
10163 parser->num_template_parameter_lists = 0;
10164 parser->in_statement = 0;
10165 parser->in_switch_statement_p = false;
10166 parser->fully_implicit_function_template_p = false;
10167 parser->implicit_template_parms = 0;
10168 parser->implicit_template_scope = 0;
10169 parser->auto_is_implicit_function_template_parm_p = false;
10171 /* By virtue of defining a local class, a lambda expression has access to
10172 the private variables of enclosing classes. */
10174 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10176 if (ok && cp_parser_error_occurred (parser))
10177 ok = false;
10179 if (ok)
10181 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10182 && cp_parser_start_tentative_firewall (parser))
10183 start = token;
10184 cp_parser_lambda_body (parser, lambda_expr);
10186 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10188 if (cp_parser_skip_to_closing_brace (parser))
10189 cp_lexer_consume_token (parser->lexer);
10192 /* The capture list was built up in reverse order; fix that now. */
10193 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10194 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10196 if (ok)
10197 maybe_add_lambda_conv_op (type);
10199 type = finish_struct (type, /*attributes=*/NULL_TREE);
10201 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10202 parser->in_statement = in_statement;
10203 parser->in_switch_statement_p = in_switch_statement_p;
10204 parser->fully_implicit_function_template_p
10205 = fully_implicit_function_template_p;
10206 parser->implicit_template_parms = implicit_template_parms;
10207 parser->implicit_template_scope = implicit_template_scope;
10208 parser->auto_is_implicit_function_template_parm_p
10209 = auto_is_implicit_function_template_parm_p;
10212 /* This field is only used during parsing of the lambda. */
10213 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10215 /* This lambda shouldn't have any proxies left at this point. */
10216 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10217 /* And now that we're done, push proxies for an enclosing lambda. */
10218 insert_pending_capture_proxies ();
10220 if (ok)
10221 lambda_expr = build_lambda_object (lambda_expr);
10222 else
10223 lambda_expr = error_mark_node;
10225 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10227 pop_deferring_access_checks ();
10229 return lambda_expr;
10232 /* Parse the beginning of a lambda expression.
10234 lambda-introducer:
10235 [ lambda-capture [opt] ]
10237 LAMBDA_EXPR is the current representation of the lambda expression. */
10239 static void
10240 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10242 /* Need commas after the first capture. */
10243 bool first = true;
10245 /* Eat the leading `['. */
10246 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10248 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10249 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10250 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10251 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10252 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10253 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10257 cp_lexer_consume_token (parser->lexer);
10258 first = false;
10261 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10263 cp_token* capture_token;
10264 tree capture_id;
10265 tree capture_init_expr;
10266 cp_id_kind idk = CP_ID_KIND_NONE;
10267 bool explicit_init_p = false;
10269 enum capture_kind_type
10271 BY_COPY,
10272 BY_REFERENCE
10274 enum capture_kind_type capture_kind = BY_COPY;
10276 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10278 error ("expected end of capture-list");
10279 return;
10282 if (first)
10283 first = false;
10284 else
10285 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10287 /* Possibly capture `this'. */
10288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10290 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10291 if (cxx_dialect < cxx2a
10292 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10293 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10294 "with by-copy capture default");
10295 cp_lexer_consume_token (parser->lexer);
10296 add_capture (lambda_expr,
10297 /*id=*/this_identifier,
10298 /*initializer=*/finish_this_expr (),
10299 /*by_reference_p=*/true,
10300 explicit_init_p);
10301 continue;
10304 /* Possibly capture `*this'. */
10305 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10306 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10308 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10309 if (cxx_dialect < cxx17)
10310 pedwarn (loc, 0, "%<*this%> capture only available with "
10311 "-std=c++17 or -std=gnu++17");
10312 cp_lexer_consume_token (parser->lexer);
10313 cp_lexer_consume_token (parser->lexer);
10314 add_capture (lambda_expr,
10315 /*id=*/this_identifier,
10316 /*initializer=*/finish_this_expr (),
10317 /*by_reference_p=*/false,
10318 explicit_init_p);
10319 continue;
10322 /* Remember whether we want to capture as a reference or not. */
10323 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10325 capture_kind = BY_REFERENCE;
10326 cp_lexer_consume_token (parser->lexer);
10329 /* Get the identifier. */
10330 capture_token = cp_lexer_peek_token (parser->lexer);
10331 capture_id = cp_parser_identifier (parser);
10333 if (capture_id == error_mark_node)
10334 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10335 delimiters, but I modified this to stop on unnested ']' as well. It
10336 was already changed to stop on unnested '}', so the
10337 "closing_parenthesis" name is no more misleading with my change. */
10339 cp_parser_skip_to_closing_parenthesis (parser,
10340 /*recovering=*/true,
10341 /*or_comma=*/true,
10342 /*consume_paren=*/true);
10343 break;
10346 /* Find the initializer for this capture. */
10347 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10348 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10349 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10351 bool direct, non_constant;
10352 /* An explicit initializer exists. */
10353 if (cxx_dialect < cxx14)
10354 pedwarn (input_location, 0,
10355 "lambda capture initializers "
10356 "only available with -std=c++14 or -std=gnu++14");
10357 capture_init_expr = cp_parser_initializer (parser, &direct,
10358 &non_constant);
10359 explicit_init_p = true;
10360 if (capture_init_expr == NULL_TREE)
10362 error ("empty initializer for lambda init-capture");
10363 capture_init_expr = error_mark_node;
10366 else
10368 const char* error_msg;
10370 /* Turn the identifier into an id-expression. */
10371 capture_init_expr
10372 = cp_parser_lookup_name_simple (parser, capture_id,
10373 capture_token->location);
10375 if (capture_init_expr == error_mark_node)
10377 unqualified_name_lookup_error (capture_id);
10378 continue;
10380 else if (DECL_P (capture_init_expr)
10381 && (!VAR_P (capture_init_expr)
10382 && TREE_CODE (capture_init_expr) != PARM_DECL))
10384 error_at (capture_token->location,
10385 "capture of non-variable %qD ",
10386 capture_init_expr);
10387 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10388 "%q#D declared here", capture_init_expr);
10389 continue;
10391 if (VAR_P (capture_init_expr)
10392 && decl_storage_duration (capture_init_expr) != dk_auto)
10394 if (pedwarn (capture_token->location, 0, "capture of variable "
10395 "%qD with non-automatic storage duration",
10396 capture_init_expr))
10397 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10398 "%q#D declared here", capture_init_expr);
10399 continue;
10402 capture_init_expr
10403 = finish_id_expression
10404 (capture_id,
10405 capture_init_expr,
10406 parser->scope,
10407 &idk,
10408 /*integral_constant_expression_p=*/false,
10409 /*allow_non_integral_constant_expression_p=*/false,
10410 /*non_integral_constant_expression_p=*/NULL,
10411 /*template_p=*/false,
10412 /*done=*/true,
10413 /*address_p=*/false,
10414 /*template_arg_p=*/false,
10415 &error_msg,
10416 capture_token->location);
10418 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10420 cp_lexer_consume_token (parser->lexer);
10421 capture_init_expr = make_pack_expansion (capture_init_expr);
10425 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10426 && !explicit_init_p)
10428 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10429 && capture_kind == BY_COPY)
10430 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10431 "of %qD redundant with by-copy capture default",
10432 capture_id);
10433 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10434 && capture_kind == BY_REFERENCE)
10435 pedwarn (capture_token->location, 0, "explicit by-reference "
10436 "capture of %qD redundant with by-reference capture "
10437 "default", capture_id);
10440 add_capture (lambda_expr,
10441 capture_id,
10442 capture_init_expr,
10443 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10444 explicit_init_p);
10446 /* If there is any qualification still in effect, clear it
10447 now; we will be starting fresh with the next capture. */
10448 parser->scope = NULL_TREE;
10449 parser->qualifying_scope = NULL_TREE;
10450 parser->object_scope = NULL_TREE;
10453 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10456 /* Parse the (optional) middle of a lambda expression.
10458 lambda-declarator:
10459 < template-parameter-list [opt] >
10460 ( parameter-declaration-clause [opt] )
10461 attribute-specifier [opt]
10462 decl-specifier-seq [opt]
10463 exception-specification [opt]
10464 lambda-return-type-clause [opt]
10466 LAMBDA_EXPR is the current representation of the lambda expression. */
10468 static bool
10469 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10471 /* 5.1.1.4 of the standard says:
10472 If a lambda-expression does not include a lambda-declarator, it is as if
10473 the lambda-declarator were ().
10474 This means an empty parameter list, no attributes, and no exception
10475 specification. */
10476 tree param_list = void_list_node;
10477 tree attributes = NULL_TREE;
10478 tree exception_spec = NULL_TREE;
10479 tree template_param_list = NULL_TREE;
10480 tree tx_qual = NULL_TREE;
10481 tree return_type = NULL_TREE;
10482 cp_decl_specifier_seq lambda_specs;
10483 clear_decl_specs (&lambda_specs);
10485 /* The template-parameter-list is optional, but must begin with
10486 an opening angle if present. */
10487 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10489 if (cxx_dialect < cxx14)
10490 pedwarn (parser->lexer->next_token->location, 0,
10491 "lambda templates are only available with "
10492 "-std=c++14 or -std=gnu++14");
10493 else if (cxx_dialect < cxx2a)
10494 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10495 "lambda templates are only available with "
10496 "-std=c++2a or -std=gnu++2a");
10498 cp_lexer_consume_token (parser->lexer);
10500 template_param_list = cp_parser_template_parameter_list (parser);
10502 cp_parser_skip_to_end_of_template_parameter_list (parser);
10504 /* We just processed one more parameter list. */
10505 ++parser->num_template_parameter_lists;
10508 /* The parameter-declaration-clause is optional (unless
10509 template-parameter-list was given), but must begin with an
10510 opening parenthesis if present. */
10511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10513 matching_parens parens;
10514 parens.consume_open (parser);
10516 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10518 /* Parse parameters. */
10519 param_list = cp_parser_parameter_declaration_clause (parser);
10521 /* Default arguments shall not be specified in the
10522 parameter-declaration-clause of a lambda-declarator. */
10523 if (cxx_dialect < cxx14)
10524 for (tree t = param_list; t; t = TREE_CHAIN (t))
10525 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10526 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10527 "default argument specified for lambda parameter");
10529 parens.require_close (parser);
10531 attributes = cp_parser_attributes_opt (parser);
10533 /* In the decl-specifier-seq of the lambda-declarator, each
10534 decl-specifier shall either be mutable or constexpr. */
10535 int declares_class_or_enum;
10536 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10537 cp_parser_decl_specifier_seq (parser,
10538 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10539 &lambda_specs, &declares_class_or_enum);
10540 if (lambda_specs.storage_class == sc_mutable)
10542 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10543 if (lambda_specs.conflicting_specifiers_p)
10544 error_at (lambda_specs.locations[ds_storage_class],
10545 "duplicate %<mutable%>");
10548 tx_qual = cp_parser_tx_qualifier_opt (parser);
10550 /* Parse optional exception specification. */
10551 exception_spec = cp_parser_exception_specification_opt (parser);
10553 /* Parse optional trailing return type. */
10554 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10556 cp_lexer_consume_token (parser->lexer);
10557 return_type = cp_parser_trailing_type_id (parser);
10560 /* The function parameters must be in scope all the way until after the
10561 trailing-return-type in case of decltype. */
10562 pop_bindings_and_leave_scope ();
10564 else if (template_param_list != NULL_TREE) // generate diagnostic
10565 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10567 /* Create the function call operator.
10569 Messing with declarators like this is no uglier than building up the
10570 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10571 other code. */
10573 cp_decl_specifier_seq return_type_specs;
10574 cp_declarator* declarator;
10575 tree fco;
10576 int quals;
10577 void *p;
10579 clear_decl_specs (&return_type_specs);
10580 if (return_type)
10581 return_type_specs.type = return_type;
10582 else
10583 /* Maybe we will deduce the return type later. */
10584 return_type_specs.type = make_auto ();
10586 if (lambda_specs.locations[ds_constexpr])
10588 if (cxx_dialect >= cxx17)
10589 return_type_specs.locations[ds_constexpr]
10590 = lambda_specs.locations[ds_constexpr];
10591 else
10592 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10593 "lambda only available with -std=c++17 or -std=gnu++17");
10596 p = obstack_alloc (&declarator_obstack, 0);
10598 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10600 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10601 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10602 declarator = make_call_declarator (declarator, param_list, quals,
10603 VIRT_SPEC_UNSPECIFIED,
10604 REF_QUAL_NONE,
10605 tx_qual,
10606 exception_spec,
10607 /*late_return_type=*/NULL_TREE,
10608 /*requires_clause*/NULL_TREE);
10609 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10611 fco = grokmethod (&return_type_specs,
10612 declarator,
10613 attributes);
10614 if (fco != error_mark_node)
10616 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10617 DECL_ARTIFICIAL (fco) = 1;
10618 /* Give the object parameter a different name. */
10619 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10620 if (return_type)
10621 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10623 if (template_param_list)
10625 fco = finish_member_template_decl (fco);
10626 finish_template_decl (template_param_list);
10627 --parser->num_template_parameter_lists;
10629 else if (parser->fully_implicit_function_template_p)
10630 fco = finish_fully_implicit_template (parser, fco);
10632 finish_member_declaration (fco);
10634 obstack_free (&declarator_obstack, p);
10636 return (fco != error_mark_node);
10640 /* Parse the body of a lambda expression, which is simply
10642 compound-statement
10644 but which requires special handling.
10645 LAMBDA_EXPR is the current representation of the lambda expression. */
10647 static void
10648 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10650 bool nested = (current_function_decl != NULL_TREE);
10651 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10652 bool in_function_body = parser->in_function_body;
10654 if (nested)
10655 push_function_context ();
10656 else
10657 /* Still increment function_depth so that we don't GC in the
10658 middle of an expression. */
10659 ++function_depth;
10661 vec<tree> omp_privatization_save;
10662 save_omp_privatization_clauses (omp_privatization_save);
10663 /* Clear this in case we're in the middle of a default argument. */
10664 parser->local_variables_forbidden_p = false;
10665 parser->in_function_body = true;
10668 local_specialization_stack s (lss_copy);
10669 tree fco = lambda_function (lambda_expr);
10670 tree body = start_lambda_function (fco, lambda_expr);
10671 matching_braces braces;
10673 if (braces.require_open (parser))
10675 tree compound_stmt = begin_compound_stmt (0);
10677 /* Originally C++11 required us to peek for 'return expr'; and
10678 process it specially here to deduce the return type. N3638
10679 removed the need for that. */
10681 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10682 cp_parser_label_declaration (parser);
10683 cp_parser_statement_seq_opt (parser, NULL_TREE);
10684 braces.require_close (parser);
10686 finish_compound_stmt (compound_stmt);
10689 finish_lambda_function (body);
10692 restore_omp_privatization_clauses (omp_privatization_save);
10693 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10694 parser->in_function_body = in_function_body;
10695 if (nested)
10696 pop_function_context();
10697 else
10698 --function_depth;
10701 /* Statements [gram.stmt.stmt] */
10703 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10705 static void
10706 add_debug_begin_stmt (location_t loc)
10708 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10709 return;
10710 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10711 /* A concept is never expanded normally. */
10712 return;
10714 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10715 SET_EXPR_LOCATION (stmt, loc);
10716 add_stmt (stmt);
10719 /* Parse a statement.
10721 statement:
10722 labeled-statement
10723 expression-statement
10724 compound-statement
10725 selection-statement
10726 iteration-statement
10727 jump-statement
10728 declaration-statement
10729 try-block
10731 C++11:
10733 statement:
10734 labeled-statement
10735 attribute-specifier-seq (opt) expression-statement
10736 attribute-specifier-seq (opt) compound-statement
10737 attribute-specifier-seq (opt) selection-statement
10738 attribute-specifier-seq (opt) iteration-statement
10739 attribute-specifier-seq (opt) jump-statement
10740 declaration-statement
10741 attribute-specifier-seq (opt) try-block
10743 init-statement:
10744 expression-statement
10745 simple-declaration
10747 TM Extension:
10749 statement:
10750 atomic-statement
10752 IN_COMPOUND is true when the statement is nested inside a
10753 cp_parser_compound_statement; this matters for certain pragmas.
10755 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10756 is a (possibly labeled) if statement which is not enclosed in braces
10757 and has an else clause. This is used to implement -Wparentheses.
10759 CHAIN is a vector of if-else-if conditions. */
10761 static void
10762 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10763 bool in_compound, bool *if_p, vec<tree> *chain,
10764 location_t *loc_after_labels)
10766 tree statement, std_attrs = NULL_TREE;
10767 cp_token *token;
10768 location_t statement_location, attrs_location;
10770 restart:
10771 if (if_p != NULL)
10772 *if_p = false;
10773 /* There is no statement yet. */
10774 statement = NULL_TREE;
10776 saved_token_sentinel saved_tokens (parser->lexer);
10777 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10778 if (c_dialect_objc ())
10779 /* In obj-c++, seeing '[[' might be the either the beginning of
10780 c++11 attributes, or a nested objc-message-expression. So
10781 let's parse the c++11 attributes tentatively. */
10782 cp_parser_parse_tentatively (parser);
10783 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10784 if (c_dialect_objc ())
10786 if (!cp_parser_parse_definitely (parser))
10787 std_attrs = NULL_TREE;
10790 /* Peek at the next token. */
10791 token = cp_lexer_peek_token (parser->lexer);
10792 /* Remember the location of the first token in the statement. */
10793 statement_location = token->location;
10794 add_debug_begin_stmt (statement_location);
10795 /* If this is a keyword, then that will often determine what kind of
10796 statement we have. */
10797 if (token->type == CPP_KEYWORD)
10799 enum rid keyword = token->keyword;
10801 switch (keyword)
10803 case RID_CASE:
10804 case RID_DEFAULT:
10805 /* Looks like a labeled-statement with a case label.
10806 Parse the label, and then use tail recursion to parse
10807 the statement. */
10808 cp_parser_label_for_labeled_statement (parser, std_attrs);
10809 in_compound = false;
10810 goto restart;
10812 case RID_IF:
10813 case RID_SWITCH:
10814 statement = cp_parser_selection_statement (parser, if_p, chain);
10815 break;
10817 case RID_WHILE:
10818 case RID_DO:
10819 case RID_FOR:
10820 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10821 break;
10823 case RID_BREAK:
10824 case RID_CONTINUE:
10825 case RID_RETURN:
10826 case RID_GOTO:
10827 statement = cp_parser_jump_statement (parser);
10828 break;
10830 /* Objective-C++ exception-handling constructs. */
10831 case RID_AT_TRY:
10832 case RID_AT_CATCH:
10833 case RID_AT_FINALLY:
10834 case RID_AT_SYNCHRONIZED:
10835 case RID_AT_THROW:
10836 statement = cp_parser_objc_statement (parser);
10837 break;
10839 case RID_TRY:
10840 statement = cp_parser_try_block (parser);
10841 break;
10843 case RID_NAMESPACE:
10844 /* This must be a namespace alias definition. */
10845 cp_parser_declaration_statement (parser);
10846 return;
10848 case RID_TRANSACTION_ATOMIC:
10849 case RID_TRANSACTION_RELAXED:
10850 case RID_SYNCHRONIZED:
10851 case RID_ATOMIC_NOEXCEPT:
10852 case RID_ATOMIC_CANCEL:
10853 statement = cp_parser_transaction (parser, token);
10854 break;
10855 case RID_TRANSACTION_CANCEL:
10856 statement = cp_parser_transaction_cancel (parser);
10857 break;
10859 default:
10860 /* It might be a keyword like `int' that can start a
10861 declaration-statement. */
10862 break;
10865 else if (token->type == CPP_NAME)
10867 /* If the next token is a `:', then we are looking at a
10868 labeled-statement. */
10869 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10870 if (token->type == CPP_COLON)
10872 /* Looks like a labeled-statement with an ordinary label.
10873 Parse the label, and then use tail recursion to parse
10874 the statement. */
10876 cp_parser_label_for_labeled_statement (parser, std_attrs);
10877 in_compound = false;
10878 goto restart;
10881 /* Anything that starts with a `{' must be a compound-statement. */
10882 else if (token->type == CPP_OPEN_BRACE)
10883 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10884 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10885 a statement all its own. */
10886 else if (token->type == CPP_PRAGMA)
10888 /* Only certain OpenMP pragmas are attached to statements, and thus
10889 are considered statements themselves. All others are not. In
10890 the context of a compound, accept the pragma as a "statement" and
10891 return so that we can check for a close brace. Otherwise we
10892 require a real statement and must go back and read one. */
10893 if (in_compound)
10894 cp_parser_pragma (parser, pragma_compound, if_p);
10895 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10896 goto restart;
10897 return;
10899 else if (token->type == CPP_EOF)
10901 cp_parser_error (parser, "expected statement");
10902 return;
10905 /* Everything else must be a declaration-statement or an
10906 expression-statement. Try for the declaration-statement
10907 first, unless we are looking at a `;', in which case we know that
10908 we have an expression-statement. */
10909 if (!statement)
10911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10913 if (std_attrs != NULL_TREE)
10915 /* Attributes should be parsed as part of the the
10916 declaration, so let's un-parse them. */
10917 saved_tokens.rollback();
10918 std_attrs = NULL_TREE;
10921 cp_parser_parse_tentatively (parser);
10922 /* Try to parse the declaration-statement. */
10923 cp_parser_declaration_statement (parser);
10924 /* If that worked, we're done. */
10925 if (cp_parser_parse_definitely (parser))
10926 return;
10928 /* All preceding labels have been parsed at this point. */
10929 if (loc_after_labels != NULL)
10930 *loc_after_labels = statement_location;
10932 /* Look for an expression-statement instead. */
10933 statement = cp_parser_expression_statement (parser, in_statement_expr);
10935 /* Handle [[fallthrough]];. */
10936 if (attribute_fallthrough_p (std_attrs))
10938 /* The next token after the fallthrough attribute is ';'. */
10939 if (statement == NULL_TREE)
10941 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10942 statement = build_call_expr_internal_loc (statement_location,
10943 IFN_FALLTHROUGH,
10944 void_type_node, 0);
10945 finish_expr_stmt (statement);
10947 else
10948 warning_at (statement_location, OPT_Wattributes,
10949 "%<fallthrough%> attribute not followed by %<;%>");
10950 std_attrs = NULL_TREE;
10954 /* Set the line number for the statement. */
10955 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10956 SET_EXPR_LOCATION (statement, statement_location);
10958 /* Allow "[[fallthrough]];", but warn otherwise. */
10959 if (std_attrs != NULL_TREE)
10960 warning_at (attrs_location,
10961 OPT_Wattributes,
10962 "attributes at the beginning of statement are ignored");
10965 /* Append ATTR to attribute list ATTRS. */
10967 static tree
10968 attr_chainon (tree attrs, tree attr)
10970 if (attrs == error_mark_node)
10971 return error_mark_node;
10972 if (attr == error_mark_node)
10973 return error_mark_node;
10974 return chainon (attrs, attr);
10977 /* Parse the label for a labeled-statement, i.e.
10979 identifier :
10980 case constant-expression :
10981 default :
10983 GNU Extension:
10984 case constant-expression ... constant-expression : statement
10986 When a label is parsed without errors, the label is added to the
10987 parse tree by the finish_* functions, so this function doesn't
10988 have to return the label. */
10990 static void
10991 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10993 cp_token *token;
10994 tree label = NULL_TREE;
10995 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10997 /* The next token should be an identifier. */
10998 token = cp_lexer_peek_token (parser->lexer);
10999 if (token->type != CPP_NAME
11000 && token->type != CPP_KEYWORD)
11002 cp_parser_error (parser, "expected labeled-statement");
11003 return;
11006 /* Remember whether this case or a user-defined label is allowed to fall
11007 through to. */
11008 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11010 parser->colon_corrects_to_scope_p = false;
11011 switch (token->keyword)
11013 case RID_CASE:
11015 tree expr, expr_hi;
11016 cp_token *ellipsis;
11018 /* Consume the `case' token. */
11019 cp_lexer_consume_token (parser->lexer);
11020 /* Parse the constant-expression. */
11021 expr = cp_parser_constant_expression (parser);
11022 if (check_for_bare_parameter_packs (expr))
11023 expr = error_mark_node;
11025 ellipsis = cp_lexer_peek_token (parser->lexer);
11026 if (ellipsis->type == CPP_ELLIPSIS)
11028 /* Consume the `...' token. */
11029 cp_lexer_consume_token (parser->lexer);
11030 expr_hi = cp_parser_constant_expression (parser);
11031 if (check_for_bare_parameter_packs (expr_hi))
11032 expr_hi = error_mark_node;
11034 /* We don't need to emit warnings here, as the common code
11035 will do this for us. */
11037 else
11038 expr_hi = NULL_TREE;
11040 if (parser->in_switch_statement_p)
11042 tree l = finish_case_label (token->location, expr, expr_hi);
11043 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11044 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11046 else
11047 error_at (token->location,
11048 "case label %qE not within a switch statement",
11049 expr);
11051 break;
11053 case RID_DEFAULT:
11054 /* Consume the `default' token. */
11055 cp_lexer_consume_token (parser->lexer);
11057 if (parser->in_switch_statement_p)
11059 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11060 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11061 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11063 else
11064 error_at (token->location, "case label not within a switch statement");
11065 break;
11067 default:
11068 /* Anything else must be an ordinary label. */
11069 label = finish_label_stmt (cp_parser_identifier (parser));
11070 if (label && TREE_CODE (label) == LABEL_DECL)
11071 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11072 break;
11075 /* Require the `:' token. */
11076 cp_parser_require (parser, CPP_COLON, RT_COLON);
11078 /* An ordinary label may optionally be followed by attributes.
11079 However, this is only permitted if the attributes are then
11080 followed by a semicolon. This is because, for backward
11081 compatibility, when parsing
11082 lab: __attribute__ ((unused)) int i;
11083 we want the attribute to attach to "i", not "lab". */
11084 if (label != NULL_TREE
11085 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11087 tree attrs;
11088 cp_parser_parse_tentatively (parser);
11089 attrs = cp_parser_gnu_attributes_opt (parser);
11090 if (attrs == NULL_TREE
11091 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11092 cp_parser_abort_tentative_parse (parser);
11093 else if (!cp_parser_parse_definitely (parser))
11095 else
11096 attributes = attr_chainon (attributes, attrs);
11099 if (attributes != NULL_TREE)
11100 cplus_decl_attributes (&label, attributes, 0);
11102 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11105 /* Parse an expression-statement.
11107 expression-statement:
11108 expression [opt] ;
11110 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11111 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11112 indicates whether this expression-statement is part of an
11113 expression statement. */
11115 static tree
11116 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11118 tree statement = NULL_TREE;
11119 cp_token *token = cp_lexer_peek_token (parser->lexer);
11120 location_t loc = token->location;
11122 /* There might be attribute fallthrough. */
11123 tree attr = cp_parser_gnu_attributes_opt (parser);
11125 /* If the next token is a ';', then there is no expression
11126 statement. */
11127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11129 statement = cp_parser_expression (parser);
11130 if (statement == error_mark_node
11131 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11133 cp_parser_skip_to_end_of_block_or_statement (parser);
11134 return error_mark_node;
11138 /* Handle [[fallthrough]];. */
11139 if (attribute_fallthrough_p (attr))
11141 /* The next token after the fallthrough attribute is ';'. */
11142 if (statement == NULL_TREE)
11143 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11144 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11145 void_type_node, 0);
11146 else
11147 warning_at (loc, OPT_Wattributes,
11148 "%<fallthrough%> attribute not followed by %<;%>");
11149 attr = NULL_TREE;
11152 /* Allow "[[fallthrough]];", but warn otherwise. */
11153 if (attr != NULL_TREE)
11154 warning_at (loc, OPT_Wattributes,
11155 "attributes at the beginning of statement are ignored");
11157 /* Give a helpful message for "A<T>::type t;" and the like. */
11158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11159 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11161 if (TREE_CODE (statement) == SCOPE_REF)
11162 error_at (token->location, "need %<typename%> before %qE because "
11163 "%qT is a dependent scope",
11164 statement, TREE_OPERAND (statement, 0));
11165 else if (is_overloaded_fn (statement)
11166 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11168 /* A::A a; */
11169 tree fn = get_first_fn (statement);
11170 error_at (token->location,
11171 "%<%T::%D%> names the constructor, not the type",
11172 DECL_CONTEXT (fn), DECL_NAME (fn));
11176 /* Consume the final `;'. */
11177 cp_parser_consume_semicolon_at_end_of_statement (parser);
11179 if (in_statement_expr
11180 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11181 /* This is the final expression statement of a statement
11182 expression. */
11183 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11184 else if (statement)
11185 statement = finish_expr_stmt (statement);
11187 return statement;
11190 /* Parse a compound-statement.
11192 compound-statement:
11193 { statement-seq [opt] }
11195 GNU extension:
11197 compound-statement:
11198 { label-declaration-seq [opt] statement-seq [opt] }
11200 label-declaration-seq:
11201 label-declaration
11202 label-declaration-seq label-declaration
11204 Returns a tree representing the statement. */
11206 static tree
11207 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11208 int bcs_flags, bool function_body)
11210 tree compound_stmt;
11211 matching_braces braces;
11213 /* Consume the `{'. */
11214 if (!braces.require_open (parser))
11215 return error_mark_node;
11216 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11217 && !function_body && cxx_dialect < cxx14)
11218 pedwarn (input_location, OPT_Wpedantic,
11219 "compound-statement in %<constexpr%> function");
11220 /* Begin the compound-statement. */
11221 compound_stmt = begin_compound_stmt (bcs_flags);
11222 /* If the next keyword is `__label__' we have a label declaration. */
11223 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11224 cp_parser_label_declaration (parser);
11225 /* Parse an (optional) statement-seq. */
11226 cp_parser_statement_seq_opt (parser, in_statement_expr);
11227 /* Finish the compound-statement. */
11228 finish_compound_stmt (compound_stmt);
11229 /* Consume the `}'. */
11230 braces.require_close (parser);
11232 return compound_stmt;
11235 /* Parse an (optional) statement-seq.
11237 statement-seq:
11238 statement
11239 statement-seq [opt] statement */
11241 static void
11242 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11244 /* Scan statements until there aren't any more. */
11245 while (true)
11247 cp_token *token = cp_lexer_peek_token (parser->lexer);
11249 /* If we are looking at a `}', then we have run out of
11250 statements; the same is true if we have reached the end
11251 of file, or have stumbled upon a stray '@end'. */
11252 if (token->type == CPP_CLOSE_BRACE
11253 || token->type == CPP_EOF
11254 || token->type == CPP_PRAGMA_EOL
11255 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11256 break;
11258 /* If we are in a compound statement and find 'else' then
11259 something went wrong. */
11260 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11262 if (parser->in_statement & IN_IF_STMT)
11263 break;
11264 else
11266 token = cp_lexer_consume_token (parser->lexer);
11267 error_at (token->location, "%<else%> without a previous %<if%>");
11271 /* Parse the statement. */
11272 cp_parser_statement (parser, in_statement_expr, true, NULL);
11276 /* Return true if we're looking at (init; cond), false otherwise. */
11278 static bool
11279 cp_parser_init_statement_p (cp_parser *parser)
11281 /* Save tokens so that we can put them back. */
11282 cp_lexer_save_tokens (parser->lexer);
11284 /* Look for ';' that is not nested in () or {}. */
11285 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11286 /*recovering=*/false,
11287 CPP_SEMICOLON,
11288 /*consume_paren=*/false);
11290 /* Roll back the tokens we skipped. */
11291 cp_lexer_rollback_tokens (parser->lexer);
11293 return ret == -1;
11296 /* Parse a selection-statement.
11298 selection-statement:
11299 if ( init-statement [opt] condition ) statement
11300 if ( init-statement [opt] condition ) statement else statement
11301 switch ( init-statement [opt] condition ) statement
11303 Returns the new IF_STMT or SWITCH_STMT.
11305 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11306 is a (possibly labeled) if statement which is not enclosed in
11307 braces and has an else clause. This is used to implement
11308 -Wparentheses.
11310 CHAIN is a vector of if-else-if conditions. This is used to implement
11311 -Wduplicated-cond. */
11313 static tree
11314 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11315 vec<tree> *chain)
11317 cp_token *token;
11318 enum rid keyword;
11319 token_indent_info guard_tinfo;
11321 if (if_p != NULL)
11322 *if_p = false;
11324 /* Peek at the next token. */
11325 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11326 guard_tinfo = get_token_indent_info (token);
11328 /* See what kind of keyword it is. */
11329 keyword = token->keyword;
11330 switch (keyword)
11332 case RID_IF:
11333 case RID_SWITCH:
11335 tree statement;
11336 tree condition;
11338 bool cx = false;
11339 if (keyword == RID_IF
11340 && cp_lexer_next_token_is_keyword (parser->lexer,
11341 RID_CONSTEXPR))
11343 cx = true;
11344 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11345 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11346 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11347 "with -std=c++17 or -std=gnu++17");
11350 /* Look for the `('. */
11351 matching_parens parens;
11352 if (!parens.require_open (parser))
11354 cp_parser_skip_to_end_of_statement (parser);
11355 return error_mark_node;
11358 /* Begin the selection-statement. */
11359 if (keyword == RID_IF)
11361 statement = begin_if_stmt ();
11362 IF_STMT_CONSTEXPR_P (statement) = cx;
11364 else
11365 statement = begin_switch_stmt ();
11367 /* Parse the optional init-statement. */
11368 if (cp_parser_init_statement_p (parser))
11370 tree decl;
11371 if (cxx_dialect < cxx17)
11372 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11373 "init-statement in selection statements only available "
11374 "with -std=c++17 or -std=gnu++17");
11375 cp_parser_init_statement (parser, &decl);
11378 /* Parse the condition. */
11379 condition = cp_parser_condition (parser);
11380 /* Look for the `)'. */
11381 if (!parens.require_close (parser))
11382 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11383 /*consume_paren=*/true);
11385 if (keyword == RID_IF)
11387 bool nested_if;
11388 unsigned char in_statement;
11390 /* Add the condition. */
11391 condition = finish_if_stmt_cond (condition, statement);
11393 if (warn_duplicated_cond)
11394 warn_duplicated_cond_add_or_warn (token->location, condition,
11395 &chain);
11397 /* Parse the then-clause. */
11398 in_statement = parser->in_statement;
11399 parser->in_statement |= IN_IF_STMT;
11401 /* Outside a template, the non-selected branch of a constexpr
11402 if is a 'discarded statement', i.e. unevaluated. */
11403 bool was_discarded = in_discarded_stmt;
11404 bool discard_then = (cx && !processing_template_decl
11405 && integer_zerop (condition));
11406 if (discard_then)
11408 in_discarded_stmt = true;
11409 ++c_inhibit_evaluation_warnings;
11412 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11413 guard_tinfo);
11415 parser->in_statement = in_statement;
11417 finish_then_clause (statement);
11419 if (discard_then)
11421 THEN_CLAUSE (statement) = NULL_TREE;
11422 in_discarded_stmt = was_discarded;
11423 --c_inhibit_evaluation_warnings;
11426 /* If the next token is `else', parse the else-clause. */
11427 if (cp_lexer_next_token_is_keyword (parser->lexer,
11428 RID_ELSE))
11430 bool discard_else = (cx && !processing_template_decl
11431 && integer_nonzerop (condition));
11432 if (discard_else)
11434 in_discarded_stmt = true;
11435 ++c_inhibit_evaluation_warnings;
11438 guard_tinfo
11439 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11440 /* Consume the `else' keyword. */
11441 cp_lexer_consume_token (parser->lexer);
11442 if (warn_duplicated_cond)
11444 if (cp_lexer_next_token_is_keyword (parser->lexer,
11445 RID_IF)
11446 && chain == NULL)
11448 /* We've got "if (COND) else if (COND2)". Start
11449 the condition chain and add COND as the first
11450 element. */
11451 chain = new vec<tree> ();
11452 if (!CONSTANT_CLASS_P (condition)
11453 && !TREE_SIDE_EFFECTS (condition))
11455 /* Wrap it in a NOP_EXPR so that we can set the
11456 location of the condition. */
11457 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11458 condition);
11459 SET_EXPR_LOCATION (e, token->location);
11460 chain->safe_push (e);
11463 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11464 RID_IF))
11466 /* This is if-else without subsequent if. Zap the
11467 condition chain; we would have already warned at
11468 this point. */
11469 delete chain;
11470 chain = NULL;
11473 begin_else_clause (statement);
11474 /* Parse the else-clause. */
11475 cp_parser_implicitly_scoped_statement (parser, NULL,
11476 guard_tinfo, chain);
11478 finish_else_clause (statement);
11480 /* If we are currently parsing a then-clause, then
11481 IF_P will not be NULL. We set it to true to
11482 indicate that this if statement has an else clause.
11483 This may trigger the Wparentheses warning below
11484 when we get back up to the parent if statement. */
11485 if (if_p != NULL)
11486 *if_p = true;
11488 if (discard_else)
11490 ELSE_CLAUSE (statement) = NULL_TREE;
11491 in_discarded_stmt = was_discarded;
11492 --c_inhibit_evaluation_warnings;
11495 else
11497 /* This if statement does not have an else clause. If
11498 NESTED_IF is true, then the then-clause has an if
11499 statement which does have an else clause. We warn
11500 about the potential ambiguity. */
11501 if (nested_if)
11502 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11503 "suggest explicit braces to avoid ambiguous"
11504 " %<else%>");
11505 if (warn_duplicated_cond)
11507 /* We don't need the condition chain anymore. */
11508 delete chain;
11509 chain = NULL;
11513 /* Now we're all done with the if-statement. */
11514 finish_if_stmt (statement);
11516 else
11518 bool in_switch_statement_p;
11519 unsigned char in_statement;
11521 /* Add the condition. */
11522 finish_switch_cond (condition, statement);
11524 /* Parse the body of the switch-statement. */
11525 in_switch_statement_p = parser->in_switch_statement_p;
11526 in_statement = parser->in_statement;
11527 parser->in_switch_statement_p = true;
11528 parser->in_statement |= IN_SWITCH_STMT;
11529 cp_parser_implicitly_scoped_statement (parser, if_p,
11530 guard_tinfo);
11531 parser->in_switch_statement_p = in_switch_statement_p;
11532 parser->in_statement = in_statement;
11534 /* Now we're all done with the switch-statement. */
11535 finish_switch_stmt (statement);
11538 return statement;
11540 break;
11542 default:
11543 cp_parser_error (parser, "expected selection-statement");
11544 return error_mark_node;
11548 /* Parse a condition.
11550 condition:
11551 expression
11552 type-specifier-seq declarator = initializer-clause
11553 type-specifier-seq declarator braced-init-list
11555 GNU Extension:
11557 condition:
11558 type-specifier-seq declarator asm-specification [opt]
11559 attributes [opt] = assignment-expression
11561 Returns the expression that should be tested. */
11563 static tree
11564 cp_parser_condition (cp_parser* parser)
11566 cp_decl_specifier_seq type_specifiers;
11567 const char *saved_message;
11568 int declares_class_or_enum;
11570 /* Try the declaration first. */
11571 cp_parser_parse_tentatively (parser);
11572 /* New types are not allowed in the type-specifier-seq for a
11573 condition. */
11574 saved_message = parser->type_definition_forbidden_message;
11575 parser->type_definition_forbidden_message
11576 = G_("types may not be defined in conditions");
11577 /* Parse the type-specifier-seq. */
11578 cp_parser_decl_specifier_seq (parser,
11579 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11580 &type_specifiers,
11581 &declares_class_or_enum);
11582 /* Restore the saved message. */
11583 parser->type_definition_forbidden_message = saved_message;
11584 /* If all is well, we might be looking at a declaration. */
11585 if (!cp_parser_error_occurred (parser))
11587 tree decl;
11588 tree asm_specification;
11589 tree attributes;
11590 cp_declarator *declarator;
11591 tree initializer = NULL_TREE;
11593 /* Parse the declarator. */
11594 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11595 /*ctor_dtor_or_conv_p=*/NULL,
11596 /*parenthesized_p=*/NULL,
11597 /*member_p=*/false,
11598 /*friend_p=*/false);
11599 /* Parse the attributes. */
11600 attributes = cp_parser_attributes_opt (parser);
11601 /* Parse the asm-specification. */
11602 asm_specification = cp_parser_asm_specification_opt (parser);
11603 /* If the next token is not an `=' or '{', then we might still be
11604 looking at an expression. For example:
11606 if (A(a).x)
11608 looks like a decl-specifier-seq and a declarator -- but then
11609 there is no `=', so this is an expression. */
11610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11611 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11612 cp_parser_simulate_error (parser);
11614 /* If we did see an `=' or '{', then we are looking at a declaration
11615 for sure. */
11616 if (cp_parser_parse_definitely (parser))
11618 tree pushed_scope;
11619 bool non_constant_p;
11620 int flags = LOOKUP_ONLYCONVERTING;
11622 /* Create the declaration. */
11623 decl = start_decl (declarator, &type_specifiers,
11624 /*initialized_p=*/true,
11625 attributes, /*prefix_attributes=*/NULL_TREE,
11626 &pushed_scope);
11628 /* Parse the initializer. */
11629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11631 initializer = cp_parser_braced_list (parser, &non_constant_p);
11632 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11633 flags = 0;
11635 else
11637 /* Consume the `='. */
11638 cp_parser_require (parser, CPP_EQ, RT_EQ);
11639 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11641 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11644 /* Process the initializer. */
11645 cp_finish_decl (decl,
11646 initializer, !non_constant_p,
11647 asm_specification,
11648 flags);
11650 if (pushed_scope)
11651 pop_scope (pushed_scope);
11653 return convert_from_reference (decl);
11656 /* If we didn't even get past the declarator successfully, we are
11657 definitely not looking at a declaration. */
11658 else
11659 cp_parser_abort_tentative_parse (parser);
11661 /* Otherwise, we are looking at an expression. */
11662 return cp_parser_expression (parser);
11665 /* Parses a for-statement or range-for-statement until the closing ')',
11666 not included. */
11668 static tree
11669 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11671 tree init, scope, decl;
11672 bool is_range_for;
11674 /* Begin the for-statement. */
11675 scope = begin_for_scope (&init);
11677 /* Parse the initialization. */
11678 is_range_for = cp_parser_init_statement (parser, &decl);
11680 if (is_range_for)
11681 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11682 else
11683 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11686 static tree
11687 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11688 unsigned short unroll)
11690 /* Normal for loop */
11691 tree condition = NULL_TREE;
11692 tree expression = NULL_TREE;
11693 tree stmt;
11695 stmt = begin_for_stmt (scope, init);
11696 /* The init-statement has already been parsed in
11697 cp_parser_init_statement, so no work is needed here. */
11698 finish_init_stmt (stmt);
11700 /* If there's a condition, process it. */
11701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11702 condition = cp_parser_condition (parser);
11703 else if (ivdep)
11705 cp_parser_error (parser, "missing loop condition in loop with "
11706 "%<GCC ivdep%> pragma");
11707 condition = error_mark_node;
11709 else if (unroll)
11711 cp_parser_error (parser, "missing loop condition in loop with "
11712 "%<GCC unroll%> pragma");
11713 condition = error_mark_node;
11715 finish_for_cond (condition, stmt, ivdep, unroll);
11716 /* Look for the `;'. */
11717 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11719 /* If there's an expression, process it. */
11720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11721 expression = cp_parser_expression (parser);
11722 finish_for_expr (expression, stmt);
11724 return stmt;
11727 /* Tries to parse a range-based for-statement:
11729 range-based-for:
11730 decl-specifier-seq declarator : expression
11732 The decl-specifier-seq declarator and the `:' are already parsed by
11733 cp_parser_init_statement. If processing_template_decl it returns a
11734 newly created RANGE_FOR_STMT; if not, it is converted to a
11735 regular FOR_STMT. */
11737 static tree
11738 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11739 bool ivdep, unsigned short unroll)
11741 tree stmt, range_expr;
11742 auto_vec <cxx_binding *, 16> bindings;
11743 auto_vec <tree, 16> names;
11744 tree decomp_first_name = NULL_TREE;
11745 unsigned int decomp_cnt = 0;
11747 /* Get the range declaration momentarily out of the way so that
11748 the range expression doesn't clash with it. */
11749 if (range_decl != error_mark_node)
11751 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11753 tree v = DECL_VALUE_EXPR (range_decl);
11754 /* For decomposition declaration get all of the corresponding
11755 declarations out of the way. */
11756 if (TREE_CODE (v) == ARRAY_REF
11757 && VAR_P (TREE_OPERAND (v, 0))
11758 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11760 tree d = range_decl;
11761 range_decl = TREE_OPERAND (v, 0);
11762 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11763 decomp_first_name = d;
11764 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11766 tree name = DECL_NAME (d);
11767 names.safe_push (name);
11768 bindings.safe_push (IDENTIFIER_BINDING (name));
11769 IDENTIFIER_BINDING (name)
11770 = IDENTIFIER_BINDING (name)->previous;
11774 if (names.is_empty ())
11776 tree name = DECL_NAME (range_decl);
11777 names.safe_push (name);
11778 bindings.safe_push (IDENTIFIER_BINDING (name));
11779 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11785 bool expr_non_constant_p;
11786 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11788 else
11789 range_expr = cp_parser_expression (parser);
11791 /* Put the range declaration(s) back into scope. */
11792 for (unsigned int i = 0; i < names.length (); i++)
11794 cxx_binding *binding = bindings[i];
11795 binding->previous = IDENTIFIER_BINDING (names[i]);
11796 IDENTIFIER_BINDING (names[i]) = binding;
11799 /* If in template, STMT is converted to a normal for-statement
11800 at instantiation. If not, it is done just ahead. */
11801 if (processing_template_decl)
11803 if (check_for_bare_parameter_packs (range_expr))
11804 range_expr = error_mark_node;
11805 stmt = begin_range_for_stmt (scope, init);
11806 if (ivdep)
11807 RANGE_FOR_IVDEP (stmt) = 1;
11808 if (unroll)
11809 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11810 finish_range_for_decl (stmt, range_decl, range_expr);
11811 if (!type_dependent_expression_p (range_expr)
11812 /* do_auto_deduction doesn't mess with template init-lists. */
11813 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11814 do_range_for_auto_deduction (range_decl, range_expr);
11816 else
11818 stmt = begin_for_stmt (scope, init);
11819 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11820 decomp_first_name, decomp_cnt, ivdep,
11821 unroll);
11823 return stmt;
11826 /* Subroutine of cp_convert_range_for: given the initializer expression,
11827 builds up the range temporary. */
11829 static tree
11830 build_range_temp (tree range_expr)
11832 tree range_type, range_temp;
11834 /* Find out the type deduced by the declaration
11835 `auto &&__range = range_expr'. */
11836 range_type = cp_build_reference_type (make_auto (), true);
11837 range_type = do_auto_deduction (range_type, range_expr,
11838 type_uses_auto (range_type));
11840 /* Create the __range variable. */
11841 range_temp = build_decl (input_location, VAR_DECL,
11842 get_identifier ("__for_range"), range_type);
11843 TREE_USED (range_temp) = 1;
11844 DECL_ARTIFICIAL (range_temp) = 1;
11846 return range_temp;
11849 /* Used by cp_parser_range_for in template context: we aren't going to
11850 do a full conversion yet, but we still need to resolve auto in the
11851 type of the for-range-declaration if present. This is basically
11852 a shortcut version of cp_convert_range_for. */
11854 static void
11855 do_range_for_auto_deduction (tree decl, tree range_expr)
11857 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11858 if (auto_node)
11860 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11861 range_temp = convert_from_reference (build_range_temp (range_expr));
11862 iter_type = (cp_parser_perform_range_for_lookup
11863 (range_temp, &begin_dummy, &end_dummy));
11864 if (iter_type)
11866 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11867 iter_type);
11868 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11869 RO_UNARY_STAR,
11870 tf_warning_or_error);
11871 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11872 iter_decl, auto_node);
11877 /* Converts a range-based for-statement into a normal
11878 for-statement, as per the definition.
11880 for (RANGE_DECL : RANGE_EXPR)
11881 BLOCK
11883 should be equivalent to:
11886 auto &&__range = RANGE_EXPR;
11887 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11888 __begin != __end;
11889 ++__begin)
11891 RANGE_DECL = *__begin;
11892 BLOCK
11896 If RANGE_EXPR is an array:
11897 BEGIN_EXPR = __range
11898 END_EXPR = __range + ARRAY_SIZE(__range)
11899 Else if RANGE_EXPR has a member 'begin' or 'end':
11900 BEGIN_EXPR = __range.begin()
11901 END_EXPR = __range.end()
11902 Else:
11903 BEGIN_EXPR = begin(__range)
11904 END_EXPR = end(__range);
11906 If __range has a member 'begin' but not 'end', or vice versa, we must
11907 still use the second alternative (it will surely fail, however).
11908 When calling begin()/end() in the third alternative we must use
11909 argument dependent lookup, but always considering 'std' as an associated
11910 namespace. */
11912 tree
11913 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11914 tree decomp_first_name, unsigned int decomp_cnt,
11915 bool ivdep, unsigned short unroll)
11917 tree begin, end;
11918 tree iter_type, begin_expr, end_expr;
11919 tree condition, expression;
11921 range_expr = mark_lvalue_use (range_expr);
11923 if (range_decl == error_mark_node || range_expr == error_mark_node)
11924 /* If an error happened previously do nothing or else a lot of
11925 unhelpful errors would be issued. */
11926 begin_expr = end_expr = iter_type = error_mark_node;
11927 else
11929 tree range_temp;
11931 if (VAR_P (range_expr)
11932 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11933 /* Can't bind a reference to an array of runtime bound. */
11934 range_temp = range_expr;
11935 else
11937 range_temp = build_range_temp (range_expr);
11938 pushdecl (range_temp);
11939 cp_finish_decl (range_temp, range_expr,
11940 /*is_constant_init*/false, NULL_TREE,
11941 LOOKUP_ONLYCONVERTING);
11942 range_temp = convert_from_reference (range_temp);
11944 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11945 &begin_expr, &end_expr);
11948 /* The new for initialization statement. */
11949 begin = build_decl (input_location, VAR_DECL,
11950 get_identifier ("__for_begin"), iter_type);
11951 TREE_USED (begin) = 1;
11952 DECL_ARTIFICIAL (begin) = 1;
11953 pushdecl (begin);
11954 cp_finish_decl (begin, begin_expr,
11955 /*is_constant_init*/false, NULL_TREE,
11956 LOOKUP_ONLYCONVERTING);
11958 if (cxx_dialect >= cxx17)
11959 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11960 end = build_decl (input_location, VAR_DECL,
11961 get_identifier ("__for_end"), iter_type);
11962 TREE_USED (end) = 1;
11963 DECL_ARTIFICIAL (end) = 1;
11964 pushdecl (end);
11965 cp_finish_decl (end, end_expr,
11966 /*is_constant_init*/false, NULL_TREE,
11967 LOOKUP_ONLYCONVERTING);
11969 finish_init_stmt (statement);
11971 /* The new for condition. */
11972 condition = build_x_binary_op (input_location, NE_EXPR,
11973 begin, ERROR_MARK,
11974 end, ERROR_MARK,
11975 NULL, tf_warning_or_error);
11976 finish_for_cond (condition, statement, ivdep, unroll);
11978 /* The new increment expression. */
11979 expression = finish_unary_op_expr (input_location,
11980 PREINCREMENT_EXPR, begin,
11981 tf_warning_or_error);
11982 finish_for_expr (expression, statement);
11984 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11985 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11987 /* The declaration is initialized with *__begin inside the loop body. */
11988 cp_finish_decl (range_decl,
11989 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11990 tf_warning_or_error),
11991 /*is_constant_init*/false, NULL_TREE,
11992 LOOKUP_ONLYCONVERTING);
11993 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11994 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11996 return statement;
11999 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12000 We need to solve both at the same time because the method used
12001 depends on the existence of members begin or end.
12002 Returns the type deduced for the iterator expression. */
12004 static tree
12005 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12007 if (error_operand_p (range))
12009 *begin = *end = error_mark_node;
12010 return error_mark_node;
12013 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12015 error ("range-based %<for%> expression of type %qT "
12016 "has incomplete type", TREE_TYPE (range));
12017 *begin = *end = error_mark_node;
12018 return error_mark_node;
12020 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12022 /* If RANGE is an array, we will use pointer arithmetic. */
12023 *begin = decay_conversion (range, tf_warning_or_error);
12024 *end = build_binary_op (input_location, PLUS_EXPR,
12025 range,
12026 array_type_nelts_top (TREE_TYPE (range)),
12027 false);
12028 return TREE_TYPE (*begin);
12030 else
12032 /* If it is not an array, we must do a bit of magic. */
12033 tree id_begin, id_end;
12034 tree member_begin, member_end;
12036 *begin = *end = error_mark_node;
12038 id_begin = get_identifier ("begin");
12039 id_end = get_identifier ("end");
12040 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12041 /*protect=*/2, /*want_type=*/false,
12042 tf_warning_or_error);
12043 member_end = lookup_member (TREE_TYPE (range), id_end,
12044 /*protect=*/2, /*want_type=*/false,
12045 tf_warning_or_error);
12047 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12049 /* Use the member functions. */
12050 if (member_begin != NULL_TREE)
12051 *begin = cp_parser_range_for_member_function (range, id_begin);
12052 else
12053 error ("range-based %<for%> expression of type %qT has an "
12054 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12056 if (member_end != NULL_TREE)
12057 *end = cp_parser_range_for_member_function (range, id_end);
12058 else
12059 error ("range-based %<for%> expression of type %qT has a "
12060 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12062 else
12064 /* Use global functions with ADL. */
12065 vec<tree, va_gc> *vec;
12066 vec = make_tree_vector ();
12068 vec_safe_push (vec, range);
12070 member_begin = perform_koenig_lookup (id_begin, vec,
12071 tf_warning_or_error);
12072 *begin = finish_call_expr (member_begin, &vec, false, true,
12073 tf_warning_or_error);
12074 member_end = perform_koenig_lookup (id_end, vec,
12075 tf_warning_or_error);
12076 *end = finish_call_expr (member_end, &vec, false, true,
12077 tf_warning_or_error);
12079 release_tree_vector (vec);
12082 /* Last common checks. */
12083 if (*begin == error_mark_node || *end == error_mark_node)
12085 /* If one of the expressions is an error do no more checks. */
12086 *begin = *end = error_mark_node;
12087 return error_mark_node;
12089 else if (type_dependent_expression_p (*begin)
12090 || type_dependent_expression_p (*end))
12091 /* Can happen, when, eg, in a template context, Koenig lookup
12092 can't resolve begin/end (c++/58503). */
12093 return NULL_TREE;
12094 else
12096 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12097 /* The unqualified type of the __begin and __end temporaries should
12098 be the same, as required by the multiple auto declaration. */
12099 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12101 if (cxx_dialect >= cxx17
12102 && (build_x_binary_op (input_location, NE_EXPR,
12103 *begin, ERROR_MARK,
12104 *end, ERROR_MARK,
12105 NULL, tf_none)
12106 != error_mark_node))
12107 /* P0184R0 allows __begin and __end to have different types,
12108 but make sure they are comparable so we can give a better
12109 diagnostic. */;
12110 else
12111 error ("inconsistent begin/end types in range-based %<for%> "
12112 "statement: %qT and %qT",
12113 TREE_TYPE (*begin), TREE_TYPE (*end));
12115 return iter_type;
12120 /* Helper function for cp_parser_perform_range_for_lookup.
12121 Builds a tree for RANGE.IDENTIFIER(). */
12123 static tree
12124 cp_parser_range_for_member_function (tree range, tree identifier)
12126 tree member, res;
12127 vec<tree, va_gc> *vec;
12129 member = finish_class_member_access_expr (range, identifier,
12130 false, tf_warning_or_error);
12131 if (member == error_mark_node)
12132 return error_mark_node;
12134 vec = make_tree_vector ();
12135 res = finish_call_expr (member, &vec,
12136 /*disallow_virtual=*/false,
12137 /*koenig_p=*/false,
12138 tf_warning_or_error);
12139 release_tree_vector (vec);
12140 return res;
12143 /* Parse an iteration-statement.
12145 iteration-statement:
12146 while ( condition ) statement
12147 do statement while ( expression ) ;
12148 for ( init-statement condition [opt] ; expression [opt] )
12149 statement
12151 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12153 static tree
12154 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12155 unsigned short unroll)
12157 cp_token *token;
12158 enum rid keyword;
12159 tree statement;
12160 unsigned char in_statement;
12161 token_indent_info guard_tinfo;
12163 /* Peek at the next token. */
12164 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12165 if (!token)
12166 return error_mark_node;
12168 guard_tinfo = get_token_indent_info (token);
12170 /* Remember whether or not we are already within an iteration
12171 statement. */
12172 in_statement = parser->in_statement;
12174 /* See what kind of keyword it is. */
12175 keyword = token->keyword;
12176 switch (keyword)
12178 case RID_WHILE:
12180 tree condition;
12182 /* Begin the while-statement. */
12183 statement = begin_while_stmt ();
12184 /* Look for the `('. */
12185 matching_parens parens;
12186 parens.require_open (parser);
12187 /* Parse the condition. */
12188 condition = cp_parser_condition (parser);
12189 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12190 /* Look for the `)'. */
12191 parens.require_close (parser);
12192 /* Parse the dependent statement. */
12193 parser->in_statement = IN_ITERATION_STMT;
12194 bool prev = note_iteration_stmt_body_start ();
12195 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12196 note_iteration_stmt_body_end (prev);
12197 parser->in_statement = in_statement;
12198 /* We're done with the while-statement. */
12199 finish_while_stmt (statement);
12201 break;
12203 case RID_DO:
12205 tree expression;
12207 /* Begin the do-statement. */
12208 statement = begin_do_stmt ();
12209 /* Parse the body of the do-statement. */
12210 parser->in_statement = IN_ITERATION_STMT;
12211 bool prev = note_iteration_stmt_body_start ();
12212 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12213 note_iteration_stmt_body_end (prev);
12214 parser->in_statement = in_statement;
12215 finish_do_body (statement);
12216 /* Look for the `while' keyword. */
12217 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12218 /* Look for the `('. */
12219 matching_parens parens;
12220 parens.require_open (parser);
12221 /* Parse the expression. */
12222 expression = cp_parser_expression (parser);
12223 /* We're done with the do-statement. */
12224 finish_do_stmt (expression, statement, ivdep, unroll);
12225 /* Look for the `)'. */
12226 parens.require_close (parser);
12227 /* Look for the `;'. */
12228 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12230 break;
12232 case RID_FOR:
12234 /* Look for the `('. */
12235 matching_parens parens;
12236 parens.require_open (parser);
12238 statement = cp_parser_for (parser, ivdep, unroll);
12240 /* Look for the `)'. */
12241 parens.require_close (parser);
12243 /* Parse the body of the for-statement. */
12244 parser->in_statement = IN_ITERATION_STMT;
12245 bool prev = note_iteration_stmt_body_start ();
12246 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12247 note_iteration_stmt_body_end (prev);
12248 parser->in_statement = in_statement;
12250 /* We're done with the for-statement. */
12251 finish_for_stmt (statement);
12253 break;
12255 default:
12256 cp_parser_error (parser, "expected iteration-statement");
12257 statement = error_mark_node;
12258 break;
12261 return statement;
12264 /* Parse a init-statement or the declarator of a range-based-for.
12265 Returns true if a range-based-for declaration is seen.
12267 init-statement:
12268 expression-statement
12269 simple-declaration */
12271 static bool
12272 cp_parser_init_statement (cp_parser* parser, tree *decl)
12274 /* If the next token is a `;', then we have an empty
12275 expression-statement. Grammatically, this is also a
12276 simple-declaration, but an invalid one, because it does not
12277 declare anything. Therefore, if we did not handle this case
12278 specially, we would issue an error message about an invalid
12279 declaration. */
12280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12282 bool is_range_for = false;
12283 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12285 /* A colon is used in range-based for. */
12286 parser->colon_corrects_to_scope_p = false;
12288 /* We're going to speculatively look for a declaration, falling back
12289 to an expression, if necessary. */
12290 cp_parser_parse_tentatively (parser);
12291 /* Parse the declaration. */
12292 cp_parser_simple_declaration (parser,
12293 /*function_definition_allowed_p=*/false,
12294 decl);
12295 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12296 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12298 /* It is a range-for, consume the ':' */
12299 cp_lexer_consume_token (parser->lexer);
12300 is_range_for = true;
12301 if (cxx_dialect < cxx11)
12303 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12304 "range-based %<for%> loops only available with "
12305 "-std=c++11 or -std=gnu++11");
12306 *decl = error_mark_node;
12309 else
12310 /* The ';' is not consumed yet because we told
12311 cp_parser_simple_declaration not to. */
12312 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12314 if (cp_parser_parse_definitely (parser))
12315 return is_range_for;
12316 /* If the tentative parse failed, then we shall need to look for an
12317 expression-statement. */
12319 /* If we are here, it is an expression-statement. */
12320 cp_parser_expression_statement (parser, NULL_TREE);
12321 return false;
12324 /* Parse a jump-statement.
12326 jump-statement:
12327 break ;
12328 continue ;
12329 return expression [opt] ;
12330 return braced-init-list ;
12331 goto identifier ;
12333 GNU extension:
12335 jump-statement:
12336 goto * expression ;
12338 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12340 static tree
12341 cp_parser_jump_statement (cp_parser* parser)
12343 tree statement = error_mark_node;
12344 cp_token *token;
12345 enum rid keyword;
12346 unsigned char in_statement;
12348 /* Peek at the next token. */
12349 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12350 if (!token)
12351 return error_mark_node;
12353 /* See what kind of keyword it is. */
12354 keyword = token->keyword;
12355 switch (keyword)
12357 case RID_BREAK:
12358 in_statement = parser->in_statement & ~IN_IF_STMT;
12359 switch (in_statement)
12361 case 0:
12362 error_at (token->location, "break statement not within loop or switch");
12363 break;
12364 default:
12365 gcc_assert ((in_statement & IN_SWITCH_STMT)
12366 || in_statement == IN_ITERATION_STMT);
12367 statement = finish_break_stmt ();
12368 if (in_statement == IN_ITERATION_STMT)
12369 break_maybe_infinite_loop ();
12370 break;
12371 case IN_OMP_BLOCK:
12372 error_at (token->location, "invalid exit from OpenMP structured block");
12373 break;
12374 case IN_OMP_FOR:
12375 error_at (token->location, "break statement used with OpenMP for loop");
12376 break;
12378 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12379 break;
12381 case RID_CONTINUE:
12382 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12384 case 0:
12385 error_at (token->location, "continue statement not within a loop");
12386 break;
12387 /* Fall through. */
12388 case IN_ITERATION_STMT:
12389 case IN_OMP_FOR:
12390 statement = finish_continue_stmt ();
12391 break;
12392 case IN_OMP_BLOCK:
12393 error_at (token->location, "invalid exit from OpenMP structured block");
12394 break;
12395 default:
12396 gcc_unreachable ();
12398 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12399 break;
12401 case RID_RETURN:
12403 tree expr;
12404 bool expr_non_constant_p;
12406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12408 cp_lexer_set_source_position (parser->lexer);
12409 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12410 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12412 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12413 expr = cp_parser_expression (parser);
12414 else
12415 /* If the next token is a `;', then there is no
12416 expression. */
12417 expr = NULL_TREE;
12418 /* Build the return-statement. */
12419 if (current_function_auto_return_pattern && in_discarded_stmt)
12420 /* Don't deduce from a discarded return statement. */;
12421 else
12422 statement = finish_return_stmt (expr);
12423 /* Look for the final `;'. */
12424 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12426 break;
12428 case RID_GOTO:
12429 if (parser->in_function_body
12430 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12432 error ("%<goto%> in %<constexpr%> function");
12433 cp_function_chain->invalid_constexpr = true;
12436 /* Create the goto-statement. */
12437 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12439 /* Issue a warning about this use of a GNU extension. */
12440 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12441 /* Consume the '*' token. */
12442 cp_lexer_consume_token (parser->lexer);
12443 /* Parse the dependent expression. */
12444 finish_goto_stmt (cp_parser_expression (parser));
12446 else
12447 finish_goto_stmt (cp_parser_identifier (parser));
12448 /* Look for the final `;'. */
12449 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12450 break;
12452 default:
12453 cp_parser_error (parser, "expected jump-statement");
12454 break;
12457 return statement;
12460 /* Parse a declaration-statement.
12462 declaration-statement:
12463 block-declaration */
12465 static void
12466 cp_parser_declaration_statement (cp_parser* parser)
12468 void *p;
12470 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12471 p = obstack_alloc (&declarator_obstack, 0);
12473 /* Parse the block-declaration. */
12474 cp_parser_block_declaration (parser, /*statement_p=*/true);
12476 /* Free any declarators allocated. */
12477 obstack_free (&declarator_obstack, p);
12480 /* Some dependent statements (like `if (cond) statement'), are
12481 implicitly in their own scope. In other words, if the statement is
12482 a single statement (as opposed to a compound-statement), it is
12483 none-the-less treated as if it were enclosed in braces. Any
12484 declarations appearing in the dependent statement are out of scope
12485 after control passes that point. This function parses a statement,
12486 but ensures that is in its own scope, even if it is not a
12487 compound-statement.
12489 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12490 is a (possibly labeled) if statement which is not enclosed in
12491 braces and has an else clause. This is used to implement
12492 -Wparentheses.
12494 CHAIN is a vector of if-else-if conditions. This is used to implement
12495 -Wduplicated-cond.
12497 Returns the new statement. */
12499 static tree
12500 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12501 const token_indent_info &guard_tinfo,
12502 vec<tree> *chain)
12504 tree statement;
12505 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12506 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12507 token_indent_info body_tinfo
12508 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12510 if (if_p != NULL)
12511 *if_p = false;
12513 /* Mark if () ; with a special NOP_EXPR. */
12514 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12516 cp_lexer_consume_token (parser->lexer);
12517 statement = add_stmt (build_empty_stmt (body_loc));
12519 if (guard_tinfo.keyword == RID_IF
12520 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12521 warning_at (body_loc, OPT_Wempty_body,
12522 "suggest braces around empty body in an %<if%> statement");
12523 else if (guard_tinfo.keyword == RID_ELSE)
12524 warning_at (body_loc, OPT_Wempty_body,
12525 "suggest braces around empty body in an %<else%> statement");
12527 /* if a compound is opened, we simply parse the statement directly. */
12528 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12529 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12530 /* If the token is not a `{', then we must take special action. */
12531 else
12533 /* Create a compound-statement. */
12534 statement = begin_compound_stmt (0);
12535 /* Parse the dependent-statement. */
12536 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12537 &body_loc_after_labels);
12538 /* Finish the dummy compound-statement. */
12539 finish_compound_stmt (statement);
12542 token_indent_info next_tinfo
12543 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12544 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12546 if (body_loc_after_labels != UNKNOWN_LOCATION
12547 && next_tinfo.type != CPP_SEMICOLON)
12548 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12549 guard_tinfo.location, guard_tinfo.keyword);
12551 /* Return the statement. */
12552 return statement;
12555 /* For some dependent statements (like `while (cond) statement'), we
12556 have already created a scope. Therefore, even if the dependent
12557 statement is a compound-statement, we do not want to create another
12558 scope. */
12560 static void
12561 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12562 const token_indent_info &guard_tinfo)
12564 /* If the token is a `{', then we must take special action. */
12565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12567 token_indent_info body_tinfo
12568 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12569 location_t loc_after_labels = UNKNOWN_LOCATION;
12571 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12572 &loc_after_labels);
12573 token_indent_info next_tinfo
12574 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12575 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12577 if (loc_after_labels != UNKNOWN_LOCATION
12578 && next_tinfo.type != CPP_SEMICOLON)
12579 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12580 guard_tinfo.location,
12581 guard_tinfo.keyword);
12583 else
12585 /* Avoid calling cp_parser_compound_statement, so that we
12586 don't create a new scope. Do everything else by hand. */
12587 matching_braces braces;
12588 braces.require_open (parser);
12589 /* If the next keyword is `__label__' we have a label declaration. */
12590 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12591 cp_parser_label_declaration (parser);
12592 /* Parse an (optional) statement-seq. */
12593 cp_parser_statement_seq_opt (parser, NULL_TREE);
12594 braces.require_close (parser);
12598 /* Declarations [gram.dcl.dcl] */
12600 /* Parse an optional declaration-sequence.
12602 declaration-seq:
12603 declaration
12604 declaration-seq declaration */
12606 static void
12607 cp_parser_declaration_seq_opt (cp_parser* parser)
12609 while (true)
12611 cp_token *token;
12613 token = cp_lexer_peek_token (parser->lexer);
12615 if (token->type == CPP_CLOSE_BRACE
12616 || token->type == CPP_EOF
12617 || token->type == CPP_PRAGMA_EOL)
12618 break;
12620 if (token->type == CPP_SEMICOLON)
12622 /* A declaration consisting of a single semicolon is
12623 invalid. Allow it unless we're being pedantic. */
12624 cp_lexer_consume_token (parser->lexer);
12625 if (!in_system_header_at (input_location))
12626 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12627 continue;
12630 /* If we're entering or exiting a region that's implicitly
12631 extern "C", modify the lang context appropriately. */
12632 if (!parser->implicit_extern_c && token->implicit_extern_c)
12634 push_lang_context (lang_name_c);
12635 parser->implicit_extern_c = true;
12637 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12639 pop_lang_context ();
12640 parser->implicit_extern_c = false;
12643 if (token->type == CPP_PRAGMA)
12645 /* A top-level declaration can consist solely of a #pragma.
12646 A nested declaration cannot, so this is done here and not
12647 in cp_parser_declaration. (A #pragma at block scope is
12648 handled in cp_parser_statement.) */
12649 cp_parser_pragma (parser, pragma_external, NULL);
12650 continue;
12653 /* Parse the declaration itself. */
12654 cp_parser_declaration (parser);
12658 /* Parse a declaration.
12660 declaration:
12661 block-declaration
12662 function-definition
12663 template-declaration
12664 explicit-instantiation
12665 explicit-specialization
12666 linkage-specification
12667 namespace-definition
12669 C++17:
12670 deduction-guide
12672 GNU extension:
12674 declaration:
12675 __extension__ declaration */
12677 static void
12678 cp_parser_declaration (cp_parser* parser)
12680 cp_token token1;
12681 cp_token token2;
12682 int saved_pedantic;
12683 void *p;
12684 tree attributes = NULL_TREE;
12686 /* Check for the `__extension__' keyword. */
12687 if (cp_parser_extension_opt (parser, &saved_pedantic))
12689 /* Parse the qualified declaration. */
12690 cp_parser_declaration (parser);
12691 /* Restore the PEDANTIC flag. */
12692 pedantic = saved_pedantic;
12694 return;
12697 /* Try to figure out what kind of declaration is present. */
12698 token1 = *cp_lexer_peek_token (parser->lexer);
12700 if (token1.type != CPP_EOF)
12701 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12702 else
12704 token2.type = CPP_EOF;
12705 token2.keyword = RID_MAX;
12708 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12709 p = obstack_alloc (&declarator_obstack, 0);
12711 /* If the next token is `extern' and the following token is a string
12712 literal, then we have a linkage specification. */
12713 if (token1.keyword == RID_EXTERN
12714 && cp_parser_is_pure_string_literal (&token2))
12715 cp_parser_linkage_specification (parser);
12716 /* If the next token is `template', then we have either a template
12717 declaration, an explicit instantiation, or an explicit
12718 specialization. */
12719 else if (token1.keyword == RID_TEMPLATE)
12721 /* `template <>' indicates a template specialization. */
12722 if (token2.type == CPP_LESS
12723 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12724 cp_parser_explicit_specialization (parser);
12725 /* `template <' indicates a template declaration. */
12726 else if (token2.type == CPP_LESS)
12727 cp_parser_template_declaration (parser, /*member_p=*/false);
12728 /* Anything else must be an explicit instantiation. */
12729 else
12730 cp_parser_explicit_instantiation (parser);
12732 /* If the next token is `export', then we have a template
12733 declaration. */
12734 else if (token1.keyword == RID_EXPORT)
12735 cp_parser_template_declaration (parser, /*member_p=*/false);
12736 /* If the next token is `extern', 'static' or 'inline' and the one
12737 after that is `template', we have a GNU extended explicit
12738 instantiation directive. */
12739 else if (cp_parser_allow_gnu_extensions_p (parser)
12740 && (token1.keyword == RID_EXTERN
12741 || token1.keyword == RID_STATIC
12742 || token1.keyword == RID_INLINE)
12743 && token2.keyword == RID_TEMPLATE)
12744 cp_parser_explicit_instantiation (parser);
12745 /* If the next token is `namespace', check for a named or unnamed
12746 namespace definition. */
12747 else if (token1.keyword == RID_NAMESPACE
12748 && (/* A named namespace definition. */
12749 (token2.type == CPP_NAME
12750 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12751 != CPP_EQ))
12752 || (token2.type == CPP_OPEN_SQUARE
12753 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12754 == CPP_OPEN_SQUARE)
12755 /* An unnamed namespace definition. */
12756 || token2.type == CPP_OPEN_BRACE
12757 || token2.keyword == RID_ATTRIBUTE))
12758 cp_parser_namespace_definition (parser);
12759 /* An inline (associated) namespace definition. */
12760 else if (token1.keyword == RID_INLINE
12761 && token2.keyword == RID_NAMESPACE)
12762 cp_parser_namespace_definition (parser);
12763 /* Objective-C++ declaration/definition. */
12764 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12765 cp_parser_objc_declaration (parser, NULL_TREE);
12766 else if (c_dialect_objc ()
12767 && token1.keyword == RID_ATTRIBUTE
12768 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12769 cp_parser_objc_declaration (parser, attributes);
12770 /* At this point we may have a template declared by a concept
12771 introduction. */
12772 else if (flag_concepts
12773 && cp_parser_template_declaration_after_export (parser,
12774 /*member_p=*/false))
12775 /* We did. */;
12776 else
12777 /* Try to parse a block-declaration, or a function-definition. */
12778 cp_parser_block_declaration (parser, /*statement_p=*/false);
12780 /* Free any declarators allocated. */
12781 obstack_free (&declarator_obstack, p);
12784 /* Parse a block-declaration.
12786 block-declaration:
12787 simple-declaration
12788 asm-definition
12789 namespace-alias-definition
12790 using-declaration
12791 using-directive
12793 GNU Extension:
12795 block-declaration:
12796 __extension__ block-declaration
12798 C++0x Extension:
12800 block-declaration:
12801 static_assert-declaration
12803 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12804 part of a declaration-statement. */
12806 static void
12807 cp_parser_block_declaration (cp_parser *parser,
12808 bool statement_p)
12810 cp_token *token1;
12811 int saved_pedantic;
12813 /* Check for the `__extension__' keyword. */
12814 if (cp_parser_extension_opt (parser, &saved_pedantic))
12816 /* Parse the qualified declaration. */
12817 cp_parser_block_declaration (parser, statement_p);
12818 /* Restore the PEDANTIC flag. */
12819 pedantic = saved_pedantic;
12821 return;
12824 /* Peek at the next token to figure out which kind of declaration is
12825 present. */
12826 token1 = cp_lexer_peek_token (parser->lexer);
12828 /* If the next keyword is `asm', we have an asm-definition. */
12829 if (token1->keyword == RID_ASM)
12831 if (statement_p)
12832 cp_parser_commit_to_tentative_parse (parser);
12833 cp_parser_asm_definition (parser);
12835 /* If the next keyword is `namespace', we have a
12836 namespace-alias-definition. */
12837 else if (token1->keyword == RID_NAMESPACE)
12838 cp_parser_namespace_alias_definition (parser);
12839 /* If the next keyword is `using', we have a
12840 using-declaration, a using-directive, or an alias-declaration. */
12841 else if (token1->keyword == RID_USING)
12843 cp_token *token2;
12845 if (statement_p)
12846 cp_parser_commit_to_tentative_parse (parser);
12847 /* If the token after `using' is `namespace', then we have a
12848 using-directive. */
12849 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12850 if (token2->keyword == RID_NAMESPACE)
12851 cp_parser_using_directive (parser);
12852 /* If the second token after 'using' is '=', then we have an
12853 alias-declaration. */
12854 else if (cxx_dialect >= cxx11
12855 && token2->type == CPP_NAME
12856 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12857 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12858 cp_parser_alias_declaration (parser);
12859 /* Otherwise, it's a using-declaration. */
12860 else
12861 cp_parser_using_declaration (parser,
12862 /*access_declaration_p=*/false);
12864 /* If the next keyword is `__label__' we have a misplaced label
12865 declaration. */
12866 else if (token1->keyword == RID_LABEL)
12868 cp_lexer_consume_token (parser->lexer);
12869 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12870 cp_parser_skip_to_end_of_statement (parser);
12871 /* If the next token is now a `;', consume it. */
12872 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12873 cp_lexer_consume_token (parser->lexer);
12875 /* If the next token is `static_assert' we have a static assertion. */
12876 else if (token1->keyword == RID_STATIC_ASSERT)
12877 cp_parser_static_assert (parser, /*member_p=*/false);
12878 /* Anything else must be a simple-declaration. */
12879 else
12880 cp_parser_simple_declaration (parser, !statement_p,
12881 /*maybe_range_for_decl*/NULL);
12884 /* Parse a simple-declaration.
12886 simple-declaration:
12887 decl-specifier-seq [opt] init-declarator-list [opt] ;
12888 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12889 brace-or-equal-initializer ;
12891 init-declarator-list:
12892 init-declarator
12893 init-declarator-list , init-declarator
12895 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12896 function-definition as a simple-declaration.
12898 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12899 parsed declaration if it is an uninitialized single declarator not followed
12900 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12901 if present, will not be consumed. */
12903 static void
12904 cp_parser_simple_declaration (cp_parser* parser,
12905 bool function_definition_allowed_p,
12906 tree *maybe_range_for_decl)
12908 cp_decl_specifier_seq decl_specifiers;
12909 int declares_class_or_enum;
12910 bool saw_declarator;
12911 location_t comma_loc = UNKNOWN_LOCATION;
12912 location_t init_loc = UNKNOWN_LOCATION;
12914 if (maybe_range_for_decl)
12915 *maybe_range_for_decl = NULL_TREE;
12917 /* Defer access checks until we know what is being declared; the
12918 checks for names appearing in the decl-specifier-seq should be
12919 done as if we were in the scope of the thing being declared. */
12920 push_deferring_access_checks (dk_deferred);
12922 /* Parse the decl-specifier-seq. We have to keep track of whether
12923 or not the decl-specifier-seq declares a named class or
12924 enumeration type, since that is the only case in which the
12925 init-declarator-list is allowed to be empty.
12927 [dcl.dcl]
12929 In a simple-declaration, the optional init-declarator-list can be
12930 omitted only when declaring a class or enumeration, that is when
12931 the decl-specifier-seq contains either a class-specifier, an
12932 elaborated-type-specifier, or an enum-specifier. */
12933 cp_parser_decl_specifier_seq (parser,
12934 CP_PARSER_FLAGS_OPTIONAL,
12935 &decl_specifiers,
12936 &declares_class_or_enum);
12937 /* We no longer need to defer access checks. */
12938 stop_deferring_access_checks ();
12940 /* In a block scope, a valid declaration must always have a
12941 decl-specifier-seq. By not trying to parse declarators, we can
12942 resolve the declaration/expression ambiguity more quickly. */
12943 if (!function_definition_allowed_p
12944 && !decl_specifiers.any_specifiers_p)
12946 cp_parser_error (parser, "expected declaration");
12947 goto done;
12950 /* If the next two tokens are both identifiers, the code is
12951 erroneous. The usual cause of this situation is code like:
12953 T t;
12955 where "T" should name a type -- but does not. */
12956 if (!decl_specifiers.any_type_specifiers_p
12957 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12959 /* If parsing tentatively, we should commit; we really are
12960 looking at a declaration. */
12961 cp_parser_commit_to_tentative_parse (parser);
12962 /* Give up. */
12963 goto done;
12966 /* If we have seen at least one decl-specifier, and the next token
12967 is not a parenthesis, then we must be looking at a declaration.
12968 (After "int (" we might be looking at a functional cast.) */
12969 if (decl_specifiers.any_specifiers_p
12970 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12971 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12972 && !cp_parser_error_occurred (parser))
12973 cp_parser_commit_to_tentative_parse (parser);
12975 /* Look for C++17 decomposition declaration. */
12976 for (size_t n = 1; ; n++)
12977 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12978 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12979 continue;
12980 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12981 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12982 && decl_specifiers.any_specifiers_p)
12984 tree decl
12985 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12986 maybe_range_for_decl,
12987 &init_loc);
12989 /* The next token should be either a `,' or a `;'. */
12990 cp_token *token = cp_lexer_peek_token (parser->lexer);
12991 /* If it's a `;', we are done. */
12992 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12993 goto finish;
12994 /* Anything else is an error. */
12995 else
12997 /* If we have already issued an error message we don't need
12998 to issue another one. */
12999 if ((decl != error_mark_node
13000 && DECL_INITIAL (decl) != error_mark_node)
13001 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13002 cp_parser_error (parser, "expected %<,%> or %<;%>");
13003 /* Skip tokens until we reach the end of the statement. */
13004 cp_parser_skip_to_end_of_statement (parser);
13005 /* If the next token is now a `;', consume it. */
13006 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13007 cp_lexer_consume_token (parser->lexer);
13008 goto done;
13011 else
13012 break;
13014 tree last_type;
13015 bool auto_specifier_p;
13016 /* NULL_TREE if both variable and function declaration are allowed,
13017 error_mark_node if function declaration are not allowed and
13018 a FUNCTION_DECL that should be diagnosed if it is followed by
13019 variable declarations. */
13020 tree auto_function_declaration;
13022 last_type = NULL_TREE;
13023 auto_specifier_p
13024 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13025 auto_function_declaration = NULL_TREE;
13027 /* Keep going until we hit the `;' at the end of the simple
13028 declaration. */
13029 saw_declarator = false;
13030 while (cp_lexer_next_token_is_not (parser->lexer,
13031 CPP_SEMICOLON))
13033 cp_token *token;
13034 bool function_definition_p;
13035 tree decl;
13036 tree auto_result = NULL_TREE;
13038 if (saw_declarator)
13040 /* If we are processing next declarator, comma is expected */
13041 token = cp_lexer_peek_token (parser->lexer);
13042 gcc_assert (token->type == CPP_COMMA);
13043 cp_lexer_consume_token (parser->lexer);
13044 if (maybe_range_for_decl)
13046 *maybe_range_for_decl = error_mark_node;
13047 if (comma_loc == UNKNOWN_LOCATION)
13048 comma_loc = token->location;
13051 else
13052 saw_declarator = true;
13054 /* Parse the init-declarator. */
13055 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13056 /*checks=*/NULL,
13057 function_definition_allowed_p,
13058 /*member_p=*/false,
13059 declares_class_or_enum,
13060 &function_definition_p,
13061 maybe_range_for_decl,
13062 &init_loc,
13063 &auto_result);
13064 /* If an error occurred while parsing tentatively, exit quickly.
13065 (That usually happens when in the body of a function; each
13066 statement is treated as a declaration-statement until proven
13067 otherwise.) */
13068 if (cp_parser_error_occurred (parser))
13069 goto done;
13071 if (auto_specifier_p && cxx_dialect >= cxx14)
13073 /* If the init-declarator-list contains more than one
13074 init-declarator, they shall all form declarations of
13075 variables. */
13076 if (auto_function_declaration == NULL_TREE)
13077 auto_function_declaration
13078 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13079 else if (TREE_CODE (decl) == FUNCTION_DECL
13080 || auto_function_declaration != error_mark_node)
13082 error_at (decl_specifiers.locations[ds_type_spec],
13083 "non-variable %qD in declaration with more than one "
13084 "declarator with placeholder type",
13085 TREE_CODE (decl) == FUNCTION_DECL
13086 ? decl : auto_function_declaration);
13087 auto_function_declaration = error_mark_node;
13091 if (auto_result
13092 && (!processing_template_decl || !type_uses_auto (auto_result)))
13094 if (last_type
13095 && last_type != error_mark_node
13096 && !same_type_p (auto_result, last_type))
13098 /* If the list of declarators contains more than one declarator,
13099 the type of each declared variable is determined as described
13100 above. If the type deduced for the template parameter U is not
13101 the same in each deduction, the program is ill-formed. */
13102 error_at (decl_specifiers.locations[ds_type_spec],
13103 "inconsistent deduction for %qT: %qT and then %qT",
13104 decl_specifiers.type, last_type, auto_result);
13105 last_type = error_mark_node;
13107 else
13108 last_type = auto_result;
13111 /* Handle function definitions specially. */
13112 if (function_definition_p)
13114 /* If the next token is a `,', then we are probably
13115 processing something like:
13117 void f() {}, *p;
13119 which is erroneous. */
13120 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13122 cp_token *token = cp_lexer_peek_token (parser->lexer);
13123 error_at (token->location,
13124 "mixing"
13125 " declarations and function-definitions is forbidden");
13127 /* Otherwise, we're done with the list of declarators. */
13128 else
13130 pop_deferring_access_checks ();
13131 return;
13134 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13135 *maybe_range_for_decl = decl;
13136 /* The next token should be either a `,' or a `;'. */
13137 token = cp_lexer_peek_token (parser->lexer);
13138 /* If it's a `,', there are more declarators to come. */
13139 if (token->type == CPP_COMMA)
13140 /* will be consumed next time around */;
13141 /* If it's a `;', we are done. */
13142 else if (token->type == CPP_SEMICOLON)
13143 break;
13144 else if (maybe_range_for_decl)
13146 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13147 permerror (decl_specifiers.locations[ds_type_spec],
13148 "types may not be defined in a for-range-declaration");
13149 break;
13151 /* Anything else is an error. */
13152 else
13154 /* If we have already issued an error message we don't need
13155 to issue another one. */
13156 if ((decl != error_mark_node
13157 && DECL_INITIAL (decl) != error_mark_node)
13158 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13159 cp_parser_error (parser, "expected %<,%> or %<;%>");
13160 /* Skip tokens until we reach the end of the statement. */
13161 cp_parser_skip_to_end_of_statement (parser);
13162 /* If the next token is now a `;', consume it. */
13163 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13164 cp_lexer_consume_token (parser->lexer);
13165 goto done;
13167 /* After the first time around, a function-definition is not
13168 allowed -- even if it was OK at first. For example:
13170 int i, f() {}
13172 is not valid. */
13173 function_definition_allowed_p = false;
13176 /* Issue an error message if no declarators are present, and the
13177 decl-specifier-seq does not itself declare a class or
13178 enumeration: [dcl.dcl]/3. */
13179 if (!saw_declarator)
13181 if (cp_parser_declares_only_class_p (parser))
13183 if (!declares_class_or_enum
13184 && decl_specifiers.type
13185 && OVERLOAD_TYPE_P (decl_specifiers.type))
13186 /* Ensure an error is issued anyway when finish_decltype_type,
13187 called via cp_parser_decl_specifier_seq, returns a class or
13188 an enumeration (c++/51786). */
13189 decl_specifiers.type = NULL_TREE;
13190 shadow_tag (&decl_specifiers);
13192 /* Perform any deferred access checks. */
13193 perform_deferred_access_checks (tf_warning_or_error);
13196 /* Consume the `;'. */
13197 finish:
13198 if (!maybe_range_for_decl)
13199 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13200 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13202 if (init_loc != UNKNOWN_LOCATION)
13203 error_at (init_loc, "initializer in range-based %<for%> loop");
13204 if (comma_loc != UNKNOWN_LOCATION)
13205 error_at (comma_loc,
13206 "multiple declarations in range-based %<for%> loop");
13209 done:
13210 pop_deferring_access_checks ();
13213 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13214 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13215 initializer ; */
13217 static tree
13218 cp_parser_decomposition_declaration (cp_parser *parser,
13219 cp_decl_specifier_seq *decl_specifiers,
13220 tree *maybe_range_for_decl,
13221 location_t *init_loc)
13223 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13224 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13225 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13227 /* Parse the identifier-list. */
13228 auto_vec<cp_expr, 10> v;
13229 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13230 while (true)
13232 cp_expr e = cp_parser_identifier (parser);
13233 if (e.get_value () == error_mark_node)
13234 break;
13235 v.safe_push (e);
13236 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13237 break;
13238 cp_lexer_consume_token (parser->lexer);
13241 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13242 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13244 end_loc = UNKNOWN_LOCATION;
13245 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13246 false);
13247 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13248 cp_lexer_consume_token (parser->lexer);
13249 else
13251 cp_parser_skip_to_end_of_statement (parser);
13252 return error_mark_node;
13256 if (cxx_dialect < cxx17)
13257 pedwarn (loc, 0, "structured bindings only available with "
13258 "-std=c++17 or -std=gnu++17");
13260 tree pushed_scope;
13261 cp_declarator *declarator = make_declarator (cdk_decomp);
13262 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13263 declarator->id_loc = loc;
13264 if (ref_qual != REF_QUAL_NONE)
13265 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13266 ref_qual == REF_QUAL_RVALUE,
13267 NULL_TREE);
13268 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13269 NULL_TREE, decl_specifiers->attributes,
13270 &pushed_scope);
13271 tree orig_decl = decl;
13273 unsigned int i;
13274 cp_expr e;
13275 cp_decl_specifier_seq decl_specs;
13276 clear_decl_specs (&decl_specs);
13277 decl_specs.type = make_auto ();
13278 tree prev = decl;
13279 FOR_EACH_VEC_ELT (v, i, e)
13281 if (i == 0)
13282 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13283 else
13284 declarator->u.id.unqualified_name = e.get_value ();
13285 declarator->id_loc = e.get_location ();
13286 tree elt_pushed_scope;
13287 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13288 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13289 if (decl2 == error_mark_node)
13290 decl = error_mark_node;
13291 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13293 /* Ensure we've diagnosed redeclaration if we aren't creating
13294 a new VAR_DECL. */
13295 gcc_assert (errorcount);
13296 decl = error_mark_node;
13298 else
13299 prev = decl2;
13300 if (elt_pushed_scope)
13301 pop_scope (elt_pushed_scope);
13304 if (v.is_empty ())
13306 error_at (loc, "empty structured binding declaration");
13307 decl = error_mark_node;
13310 if (maybe_range_for_decl == NULL
13311 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13313 bool non_constant_p = false, is_direct_init = false;
13314 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13315 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13316 &non_constant_p);
13317 if (initializer == NULL_TREE
13318 || (TREE_CODE (initializer) == TREE_LIST
13319 && TREE_CHAIN (initializer))
13320 || (is_direct_init
13321 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13322 && CONSTRUCTOR_NELTS (initializer) != 1))
13324 error_at (loc, "invalid initializer for structured binding "
13325 "declaration");
13326 initializer = error_mark_node;
13329 if (decl != error_mark_node)
13331 cp_maybe_mangle_decomp (decl, prev, v.length ());
13332 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13333 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13334 cp_finish_decomp (decl, prev, v.length ());
13337 else if (decl != error_mark_node)
13339 *maybe_range_for_decl = prev;
13340 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13341 the underlying DECL. */
13342 cp_finish_decomp (decl, prev, v.length ());
13345 if (pushed_scope)
13346 pop_scope (pushed_scope);
13348 if (decl == error_mark_node && DECL_P (orig_decl))
13350 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13351 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13354 return decl;
13357 /* Parse a decl-specifier-seq.
13359 decl-specifier-seq:
13360 decl-specifier-seq [opt] decl-specifier
13361 decl-specifier attribute-specifier-seq [opt] (C++11)
13363 decl-specifier:
13364 storage-class-specifier
13365 type-specifier
13366 function-specifier
13367 friend
13368 typedef
13370 GNU Extension:
13372 decl-specifier:
13373 attributes
13375 Concepts Extension:
13377 decl-specifier:
13378 concept
13380 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13382 The parser flags FLAGS is used to control type-specifier parsing.
13384 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13385 flags:
13387 1: one of the decl-specifiers is an elaborated-type-specifier
13388 (i.e., a type declaration)
13389 2: one of the decl-specifiers is an enum-specifier or a
13390 class-specifier (i.e., a type definition)
13394 static void
13395 cp_parser_decl_specifier_seq (cp_parser* parser,
13396 cp_parser_flags flags,
13397 cp_decl_specifier_seq *decl_specs,
13398 int* declares_class_or_enum)
13400 bool constructor_possible_p = !parser->in_declarator_p;
13401 bool found_decl_spec = false;
13402 cp_token *start_token = NULL;
13403 cp_decl_spec ds;
13405 /* Clear DECL_SPECS. */
13406 clear_decl_specs (decl_specs);
13408 /* Assume no class or enumeration type is declared. */
13409 *declares_class_or_enum = 0;
13411 /* Keep reading specifiers until there are no more to read. */
13412 while (true)
13414 bool constructor_p;
13415 cp_token *token;
13416 ds = ds_last;
13418 /* Peek at the next token. */
13419 token = cp_lexer_peek_token (parser->lexer);
13421 /* Save the first token of the decl spec list for error
13422 reporting. */
13423 if (!start_token)
13424 start_token = token;
13425 /* Handle attributes. */
13426 if (cp_next_tokens_can_be_attribute_p (parser))
13428 /* Parse the attributes. */
13429 tree attrs = cp_parser_attributes_opt (parser);
13431 /* In a sequence of declaration specifiers, c++11 attributes
13432 appertain to the type that precede them. In that case
13433 [dcl.spec]/1 says:
13435 The attribute-specifier-seq affects the type only for
13436 the declaration it appears in, not other declarations
13437 involving the same type.
13439 But for now let's force the user to position the
13440 attribute either at the beginning of the declaration or
13441 after the declarator-id, which would clearly mean that it
13442 applies to the declarator. */
13443 if (cxx11_attribute_p (attrs))
13445 if (!found_decl_spec)
13446 /* The c++11 attribute is at the beginning of the
13447 declaration. It appertains to the entity being
13448 declared. */;
13449 else
13451 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13453 /* This is an attribute following a
13454 class-specifier. */
13455 if (decl_specs->type_definition_p)
13456 warn_misplaced_attr_for_class_type (token->location,
13457 decl_specs->type);
13458 attrs = NULL_TREE;
13460 else
13462 decl_specs->std_attributes
13463 = attr_chainon (decl_specs->std_attributes, attrs);
13464 if (decl_specs->locations[ds_std_attribute] == 0)
13465 decl_specs->locations[ds_std_attribute] = token->location;
13467 continue;
13471 decl_specs->attributes
13472 = attr_chainon (decl_specs->attributes, attrs);
13473 if (decl_specs->locations[ds_attribute] == 0)
13474 decl_specs->locations[ds_attribute] = token->location;
13475 continue;
13477 /* Assume we will find a decl-specifier keyword. */
13478 found_decl_spec = true;
13479 /* If the next token is an appropriate keyword, we can simply
13480 add it to the list. */
13481 switch (token->keyword)
13483 /* decl-specifier:
13484 friend
13485 constexpr */
13486 case RID_FRIEND:
13487 if (!at_class_scope_p ())
13489 gcc_rich_location richloc (token->location);
13490 richloc.add_fixit_remove ();
13491 error_at (&richloc, "%<friend%> used outside of class");
13492 cp_lexer_purge_token (parser->lexer);
13494 else
13496 ds = ds_friend;
13497 /* Consume the token. */
13498 cp_lexer_consume_token (parser->lexer);
13500 break;
13502 case RID_CONSTEXPR:
13503 ds = ds_constexpr;
13504 cp_lexer_consume_token (parser->lexer);
13505 break;
13507 case RID_CONCEPT:
13508 ds = ds_concept;
13509 cp_lexer_consume_token (parser->lexer);
13510 break;
13512 /* function-specifier:
13513 inline
13514 virtual
13515 explicit */
13516 case RID_INLINE:
13517 case RID_VIRTUAL:
13518 case RID_EXPLICIT:
13519 cp_parser_function_specifier_opt (parser, decl_specs);
13520 break;
13522 /* decl-specifier:
13523 typedef */
13524 case RID_TYPEDEF:
13525 ds = ds_typedef;
13526 /* Consume the token. */
13527 cp_lexer_consume_token (parser->lexer);
13528 /* A constructor declarator cannot appear in a typedef. */
13529 constructor_possible_p = false;
13530 /* The "typedef" keyword can only occur in a declaration; we
13531 may as well commit at this point. */
13532 cp_parser_commit_to_tentative_parse (parser);
13534 if (decl_specs->storage_class != sc_none)
13535 decl_specs->conflicting_specifiers_p = true;
13536 break;
13538 /* storage-class-specifier:
13539 auto
13540 register
13541 static
13542 extern
13543 mutable
13545 GNU Extension:
13546 thread */
13547 case RID_AUTO:
13548 if (cxx_dialect == cxx98)
13550 /* Consume the token. */
13551 cp_lexer_consume_token (parser->lexer);
13553 /* Complain about `auto' as a storage specifier, if
13554 we're complaining about C++0x compatibility. */
13555 gcc_rich_location richloc (token->location);
13556 richloc.add_fixit_remove ();
13557 warning_at (&richloc, OPT_Wc__11_compat,
13558 "%<auto%> changes meaning in C++11; "
13559 "please remove it");
13561 /* Set the storage class anyway. */
13562 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13563 token);
13565 else
13566 /* C++0x auto type-specifier. */
13567 found_decl_spec = false;
13568 break;
13570 case RID_REGISTER:
13571 case RID_STATIC:
13572 case RID_EXTERN:
13573 case RID_MUTABLE:
13574 /* Consume the token. */
13575 cp_lexer_consume_token (parser->lexer);
13576 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13577 token);
13578 break;
13579 case RID_THREAD:
13580 /* Consume the token. */
13581 ds = ds_thread;
13582 cp_lexer_consume_token (parser->lexer);
13583 break;
13585 default:
13586 /* We did not yet find a decl-specifier yet. */
13587 found_decl_spec = false;
13588 break;
13591 if (found_decl_spec
13592 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13593 && token->keyword != RID_CONSTEXPR)
13594 error ("decl-specifier invalid in condition");
13596 if (found_decl_spec
13597 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13598 && token->keyword != RID_MUTABLE
13599 && token->keyword != RID_CONSTEXPR)
13600 error_at (token->location, "%qD invalid in lambda",
13601 ridpointers[token->keyword]);
13603 if (ds != ds_last)
13604 set_and_check_decl_spec_loc (decl_specs, ds, token);
13606 /* Constructors are a special case. The `S' in `S()' is not a
13607 decl-specifier; it is the beginning of the declarator. */
13608 constructor_p
13609 = (!found_decl_spec
13610 && constructor_possible_p
13611 && (cp_parser_constructor_declarator_p
13612 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13614 /* If we don't have a DECL_SPEC yet, then we must be looking at
13615 a type-specifier. */
13616 if (!found_decl_spec && !constructor_p)
13618 int decl_spec_declares_class_or_enum;
13619 bool is_cv_qualifier;
13620 tree type_spec;
13622 type_spec
13623 = cp_parser_type_specifier (parser, flags,
13624 decl_specs,
13625 /*is_declaration=*/true,
13626 &decl_spec_declares_class_or_enum,
13627 &is_cv_qualifier);
13628 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13630 /* If this type-specifier referenced a user-defined type
13631 (a typedef, class-name, etc.), then we can't allow any
13632 more such type-specifiers henceforth.
13634 [dcl.spec]
13636 The longest sequence of decl-specifiers that could
13637 possibly be a type name is taken as the
13638 decl-specifier-seq of a declaration. The sequence shall
13639 be self-consistent as described below.
13641 [dcl.type]
13643 As a general rule, at most one type-specifier is allowed
13644 in the complete decl-specifier-seq of a declaration. The
13645 only exceptions are the following:
13647 -- const or volatile can be combined with any other
13648 type-specifier.
13650 -- signed or unsigned can be combined with char, long,
13651 short, or int.
13653 -- ..
13655 Example:
13657 typedef char* Pc;
13658 void g (const int Pc);
13660 Here, Pc is *not* part of the decl-specifier seq; it's
13661 the declarator. Therefore, once we see a type-specifier
13662 (other than a cv-qualifier), we forbid any additional
13663 user-defined types. We *do* still allow things like `int
13664 int' to be considered a decl-specifier-seq, and issue the
13665 error message later. */
13666 if (type_spec && !is_cv_qualifier)
13667 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13668 /* A constructor declarator cannot follow a type-specifier. */
13669 if (type_spec)
13671 constructor_possible_p = false;
13672 found_decl_spec = true;
13673 if (!is_cv_qualifier)
13674 decl_specs->any_type_specifiers_p = true;
13678 /* If we still do not have a DECL_SPEC, then there are no more
13679 decl-specifiers. */
13680 if (!found_decl_spec)
13681 break;
13683 decl_specs->any_specifiers_p = true;
13684 /* After we see one decl-specifier, further decl-specifiers are
13685 always optional. */
13686 flags |= CP_PARSER_FLAGS_OPTIONAL;
13689 /* Don't allow a friend specifier with a class definition. */
13690 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13691 && (*declares_class_or_enum & 2))
13692 error_at (decl_specs->locations[ds_friend],
13693 "class definition may not be declared a friend");
13696 /* Parse an (optional) storage-class-specifier.
13698 storage-class-specifier:
13699 auto
13700 register
13701 static
13702 extern
13703 mutable
13705 GNU Extension:
13707 storage-class-specifier:
13708 thread
13710 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13712 static tree
13713 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13715 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13717 case RID_AUTO:
13718 if (cxx_dialect != cxx98)
13719 return NULL_TREE;
13720 /* Fall through for C++98. */
13721 gcc_fallthrough ();
13723 case RID_REGISTER:
13724 case RID_STATIC:
13725 case RID_EXTERN:
13726 case RID_MUTABLE:
13727 case RID_THREAD:
13728 /* Consume the token. */
13729 return cp_lexer_consume_token (parser->lexer)->u.value;
13731 default:
13732 return NULL_TREE;
13736 /* Parse an (optional) function-specifier.
13738 function-specifier:
13739 inline
13740 virtual
13741 explicit
13743 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13744 Updates DECL_SPECS, if it is non-NULL. */
13746 static tree
13747 cp_parser_function_specifier_opt (cp_parser* parser,
13748 cp_decl_specifier_seq *decl_specs)
13750 cp_token *token = cp_lexer_peek_token (parser->lexer);
13751 switch (token->keyword)
13753 case RID_INLINE:
13754 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13755 break;
13757 case RID_VIRTUAL:
13758 /* 14.5.2.3 [temp.mem]
13760 A member function template shall not be virtual. */
13761 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13762 && current_class_type)
13763 error_at (token->location, "templates may not be %<virtual%>");
13764 else
13765 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13766 break;
13768 case RID_EXPLICIT:
13769 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13770 break;
13772 default:
13773 return NULL_TREE;
13776 /* Consume the token. */
13777 return cp_lexer_consume_token (parser->lexer)->u.value;
13780 /* Parse a linkage-specification.
13782 linkage-specification:
13783 extern string-literal { declaration-seq [opt] }
13784 extern string-literal declaration */
13786 static void
13787 cp_parser_linkage_specification (cp_parser* parser)
13789 tree linkage;
13791 /* Look for the `extern' keyword. */
13792 cp_token *extern_token
13793 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13795 /* Look for the string-literal. */
13796 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13797 linkage = cp_parser_string_literal (parser, false, false);
13799 /* Transform the literal into an identifier. If the literal is a
13800 wide-character string, or contains embedded NULs, then we can't
13801 handle it as the user wants. */
13802 if (strlen (TREE_STRING_POINTER (linkage))
13803 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13805 cp_parser_error (parser, "invalid linkage-specification");
13806 /* Assume C++ linkage. */
13807 linkage = lang_name_cplusplus;
13809 else
13810 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13812 /* We're now using the new linkage. */
13813 push_lang_context (linkage);
13815 /* Preserve the location of the the innermost linkage specification,
13816 tracking the locations of nested specifications via a local. */
13817 location_t saved_location
13818 = parser->innermost_linkage_specification_location;
13819 /* Construct a location ranging from the start of the "extern" to
13820 the end of the string-literal, with the caret at the start, e.g.:
13821 extern "C" {
13822 ^~~~~~~~~~
13824 parser->innermost_linkage_specification_location
13825 = make_location (extern_token->location,
13826 extern_token->location,
13827 get_finish (string_token->location));
13829 /* If the next token is a `{', then we're using the first
13830 production. */
13831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13833 cp_ensure_no_omp_declare_simd (parser);
13834 cp_ensure_no_oacc_routine (parser);
13836 /* Consume the `{' token. */
13837 matching_braces braces;
13838 braces.consume_open (parser)->location;
13839 /* Parse the declarations. */
13840 cp_parser_declaration_seq_opt (parser);
13841 /* Look for the closing `}'. */
13842 braces.require_close (parser);
13844 /* Otherwise, there's just one declaration. */
13845 else
13847 bool saved_in_unbraced_linkage_specification_p;
13849 saved_in_unbraced_linkage_specification_p
13850 = parser->in_unbraced_linkage_specification_p;
13851 parser->in_unbraced_linkage_specification_p = true;
13852 cp_parser_declaration (parser);
13853 parser->in_unbraced_linkage_specification_p
13854 = saved_in_unbraced_linkage_specification_p;
13857 /* We're done with the linkage-specification. */
13858 pop_lang_context ();
13860 /* Restore location of parent linkage specification, if any. */
13861 parser->innermost_linkage_specification_location = saved_location;
13864 /* Parse a static_assert-declaration.
13866 static_assert-declaration:
13867 static_assert ( constant-expression , string-literal ) ;
13868 static_assert ( constant-expression ) ; (C++17)
13870 If MEMBER_P, this static_assert is a class member. */
13872 static void
13873 cp_parser_static_assert(cp_parser *parser, bool member_p)
13875 cp_expr condition;
13876 location_t token_loc;
13877 tree message;
13878 bool dummy;
13880 /* Peek at the `static_assert' token so we can keep track of exactly
13881 where the static assertion started. */
13882 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13884 /* Look for the `static_assert' keyword. */
13885 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13886 RT_STATIC_ASSERT))
13887 return;
13889 /* We know we are in a static assertion; commit to any tentative
13890 parse. */
13891 if (cp_parser_parsing_tentatively (parser))
13892 cp_parser_commit_to_tentative_parse (parser);
13894 /* Parse the `(' starting the static assertion condition. */
13895 matching_parens parens;
13896 parens.require_open (parser);
13898 /* Parse the constant-expression. Allow a non-constant expression
13899 here in order to give better diagnostics in finish_static_assert. */
13900 condition =
13901 cp_parser_constant_expression (parser,
13902 /*allow_non_constant_p=*/true,
13903 /*non_constant_p=*/&dummy);
13905 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13907 if (cxx_dialect < cxx17)
13908 pedwarn (input_location, OPT_Wpedantic,
13909 "static_assert without a message "
13910 "only available with -std=c++17 or -std=gnu++17");
13911 /* Eat the ')' */
13912 cp_lexer_consume_token (parser->lexer);
13913 message = build_string (1, "");
13914 TREE_TYPE (message) = char_array_type_node;
13915 fix_string_type (message);
13917 else
13919 /* Parse the separating `,'. */
13920 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13922 /* Parse the string-literal message. */
13923 message = cp_parser_string_literal (parser,
13924 /*translate=*/false,
13925 /*wide_ok=*/true);
13927 /* A `)' completes the static assertion. */
13928 if (!parens.require_close (parser))
13929 cp_parser_skip_to_closing_parenthesis (parser,
13930 /*recovering=*/true,
13931 /*or_comma=*/false,
13932 /*consume_paren=*/true);
13935 /* A semicolon terminates the declaration. */
13936 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13938 /* Get the location for the static assertion. Use that of the
13939 condition if available, otherwise, use that of the "static_assert"
13940 token. */
13941 location_t assert_loc = condition.get_location ();
13942 if (assert_loc == UNKNOWN_LOCATION)
13943 assert_loc = token_loc;
13945 /* Complete the static assertion, which may mean either processing
13946 the static assert now or saving it for template instantiation. */
13947 finish_static_assert (condition, message, assert_loc, member_p);
13950 /* Parse the expression in decltype ( expression ). */
13952 static tree
13953 cp_parser_decltype_expr (cp_parser *parser,
13954 bool &id_expression_or_member_access_p)
13956 cp_token *id_expr_start_token;
13957 tree expr;
13959 /* Since we're going to preserve any side-effects from this parse, set up a
13960 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13961 in the expression. */
13962 tentative_firewall firewall (parser);
13964 /* First, try parsing an id-expression. */
13965 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13966 cp_parser_parse_tentatively (parser);
13967 expr = cp_parser_id_expression (parser,
13968 /*template_keyword_p=*/false,
13969 /*check_dependency_p=*/true,
13970 /*template_p=*/NULL,
13971 /*declarator_p=*/false,
13972 /*optional_p=*/false);
13974 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13976 bool non_integral_constant_expression_p = false;
13977 tree id_expression = expr;
13978 cp_id_kind idk;
13979 const char *error_msg;
13981 if (identifier_p (expr))
13982 /* Lookup the name we got back from the id-expression. */
13983 expr = cp_parser_lookup_name_simple (parser, expr,
13984 id_expr_start_token->location);
13986 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13987 /* A template without args is not a complete id-expression. */
13988 expr = error_mark_node;
13990 if (expr
13991 && expr != error_mark_node
13992 && TREE_CODE (expr) != TYPE_DECL
13993 && (TREE_CODE (expr) != BIT_NOT_EXPR
13994 || !TYPE_P (TREE_OPERAND (expr, 0)))
13995 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13997 /* Complete lookup of the id-expression. */
13998 expr = (finish_id_expression
13999 (id_expression, expr, parser->scope, &idk,
14000 /*integral_constant_expression_p=*/false,
14001 /*allow_non_integral_constant_expression_p=*/true,
14002 &non_integral_constant_expression_p,
14003 /*template_p=*/false,
14004 /*done=*/true,
14005 /*address_p=*/false,
14006 /*template_arg_p=*/false,
14007 &error_msg,
14008 id_expr_start_token->location));
14010 if (expr == error_mark_node)
14011 /* We found an id-expression, but it was something that we
14012 should not have found. This is an error, not something
14013 we can recover from, so note that we found an
14014 id-expression and we'll recover as gracefully as
14015 possible. */
14016 id_expression_or_member_access_p = true;
14019 if (expr
14020 && expr != error_mark_node
14021 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14022 /* We have an id-expression. */
14023 id_expression_or_member_access_p = true;
14026 if (!id_expression_or_member_access_p)
14028 /* Abort the id-expression parse. */
14029 cp_parser_abort_tentative_parse (parser);
14031 /* Parsing tentatively, again. */
14032 cp_parser_parse_tentatively (parser);
14034 /* Parse a class member access. */
14035 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14036 /*cast_p=*/false, /*decltype*/true,
14037 /*member_access_only_p=*/true, NULL);
14039 if (expr
14040 && expr != error_mark_node
14041 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14042 /* We have an id-expression. */
14043 id_expression_or_member_access_p = true;
14046 if (id_expression_or_member_access_p)
14047 /* We have parsed the complete id-expression or member access. */
14048 cp_parser_parse_definitely (parser);
14049 else
14051 /* Abort our attempt to parse an id-expression or member access
14052 expression. */
14053 cp_parser_abort_tentative_parse (parser);
14055 /* Commit to the tentative_firewall so we get syntax errors. */
14056 cp_parser_commit_to_tentative_parse (parser);
14058 /* Parse a full expression. */
14059 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14060 /*decltype_p=*/true);
14063 return expr;
14066 /* Parse a `decltype' type. Returns the type.
14068 simple-type-specifier:
14069 decltype ( expression )
14070 C++14 proposal:
14071 decltype ( auto ) */
14073 static tree
14074 cp_parser_decltype (cp_parser *parser)
14076 bool id_expression_or_member_access_p = false;
14077 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14079 if (start_token->type == CPP_DECLTYPE)
14081 /* Already parsed. */
14082 cp_lexer_consume_token (parser->lexer);
14083 return saved_checks_value (start_token->u.tree_check_value);
14086 /* Look for the `decltype' token. */
14087 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14088 return error_mark_node;
14090 /* Parse the opening `('. */
14091 matching_parens parens;
14092 if (!parens.require_open (parser))
14093 return error_mark_node;
14095 push_deferring_access_checks (dk_deferred);
14097 tree expr = NULL_TREE;
14099 if (cxx_dialect >= cxx14
14100 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14101 /* decltype (auto) */
14102 cp_lexer_consume_token (parser->lexer);
14103 else
14105 /* decltype (expression) */
14107 /* Types cannot be defined in a `decltype' expression. Save away the
14108 old message and set the new one. */
14109 const char *saved_message = parser->type_definition_forbidden_message;
14110 parser->type_definition_forbidden_message
14111 = G_("types may not be defined in %<decltype%> expressions");
14113 /* The restrictions on constant-expressions do not apply inside
14114 decltype expressions. */
14115 bool saved_integral_constant_expression_p
14116 = parser->integral_constant_expression_p;
14117 bool saved_non_integral_constant_expression_p
14118 = parser->non_integral_constant_expression_p;
14119 parser->integral_constant_expression_p = false;
14121 /* Within a parenthesized expression, a `>' token is always
14122 the greater-than operator. */
14123 bool saved_greater_than_is_operator_p
14124 = parser->greater_than_is_operator_p;
14125 parser->greater_than_is_operator_p = true;
14127 /* Do not actually evaluate the expression. */
14128 ++cp_unevaluated_operand;
14130 /* Do not warn about problems with the expression. */
14131 ++c_inhibit_evaluation_warnings;
14133 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14135 /* Go back to evaluating expressions. */
14136 --cp_unevaluated_operand;
14137 --c_inhibit_evaluation_warnings;
14139 /* The `>' token might be the end of a template-id or
14140 template-parameter-list now. */
14141 parser->greater_than_is_operator_p
14142 = saved_greater_than_is_operator_p;
14144 /* Restore the old message and the integral constant expression
14145 flags. */
14146 parser->type_definition_forbidden_message = saved_message;
14147 parser->integral_constant_expression_p
14148 = saved_integral_constant_expression_p;
14149 parser->non_integral_constant_expression_p
14150 = saved_non_integral_constant_expression_p;
14153 /* Parse to the closing `)'. */
14154 if (!parens.require_close (parser))
14156 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14157 /*consume_paren=*/true);
14158 pop_deferring_access_checks ();
14159 return error_mark_node;
14162 if (!expr)
14164 /* Build auto. */
14165 expr = make_decltype_auto ();
14166 AUTO_IS_DECLTYPE (expr) = true;
14168 else
14169 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14170 tf_warning_or_error);
14172 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14173 it again. */
14174 start_token->type = CPP_DECLTYPE;
14175 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14176 start_token->u.tree_check_value->value = expr;
14177 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14178 start_token->keyword = RID_MAX;
14179 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14181 pop_to_parent_deferring_access_checks ();
14183 return expr;
14186 /* Special member functions [gram.special] */
14188 /* Parse a conversion-function-id.
14190 conversion-function-id:
14191 operator conversion-type-id
14193 Returns an IDENTIFIER_NODE representing the operator. */
14195 static tree
14196 cp_parser_conversion_function_id (cp_parser* parser)
14198 tree type;
14199 tree saved_scope;
14200 tree saved_qualifying_scope;
14201 tree saved_object_scope;
14202 tree pushed_scope = NULL_TREE;
14204 /* Look for the `operator' token. */
14205 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14206 return error_mark_node;
14207 /* When we parse the conversion-type-id, the current scope will be
14208 reset. However, we need that information in able to look up the
14209 conversion function later, so we save it here. */
14210 saved_scope = parser->scope;
14211 saved_qualifying_scope = parser->qualifying_scope;
14212 saved_object_scope = parser->object_scope;
14213 /* We must enter the scope of the class so that the names of
14214 entities declared within the class are available in the
14215 conversion-type-id. For example, consider:
14217 struct S {
14218 typedef int I;
14219 operator I();
14222 S::operator I() { ... }
14224 In order to see that `I' is a type-name in the definition, we
14225 must be in the scope of `S'. */
14226 if (saved_scope)
14227 pushed_scope = push_scope (saved_scope);
14228 /* Parse the conversion-type-id. */
14229 type = cp_parser_conversion_type_id (parser);
14230 /* Leave the scope of the class, if any. */
14231 if (pushed_scope)
14232 pop_scope (pushed_scope);
14233 /* Restore the saved scope. */
14234 parser->scope = saved_scope;
14235 parser->qualifying_scope = saved_qualifying_scope;
14236 parser->object_scope = saved_object_scope;
14237 /* If the TYPE is invalid, indicate failure. */
14238 if (type == error_mark_node)
14239 return error_mark_node;
14240 return make_conv_op_name (type);
14243 /* Parse a conversion-type-id:
14245 conversion-type-id:
14246 type-specifier-seq conversion-declarator [opt]
14248 Returns the TYPE specified. */
14250 static tree
14251 cp_parser_conversion_type_id (cp_parser* parser)
14253 tree attributes;
14254 cp_decl_specifier_seq type_specifiers;
14255 cp_declarator *declarator;
14256 tree type_specified;
14257 const char *saved_message;
14259 /* Parse the attributes. */
14260 attributes = cp_parser_attributes_opt (parser);
14262 saved_message = parser->type_definition_forbidden_message;
14263 parser->type_definition_forbidden_message
14264 = G_("types may not be defined in a conversion-type-id");
14266 /* Parse the type-specifiers. */
14267 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14268 /*is_trailing_return=*/false,
14269 &type_specifiers);
14271 parser->type_definition_forbidden_message = saved_message;
14273 /* If that didn't work, stop. */
14274 if (type_specifiers.type == error_mark_node)
14275 return error_mark_node;
14276 /* Parse the conversion-declarator. */
14277 declarator = cp_parser_conversion_declarator_opt (parser);
14279 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14280 /*initialized=*/0, &attributes);
14281 if (attributes)
14282 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14284 /* Don't give this error when parsing tentatively. This happens to
14285 work because we always parse this definitively once. */
14286 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14287 && type_uses_auto (type_specified))
14289 if (cxx_dialect < cxx14)
14291 error ("invalid use of %<auto%> in conversion operator");
14292 return error_mark_node;
14294 else if (template_parm_scope_p ())
14295 warning (0, "use of %<auto%> in member template "
14296 "conversion operator can never be deduced");
14299 return type_specified;
14302 /* Parse an (optional) conversion-declarator.
14304 conversion-declarator:
14305 ptr-operator conversion-declarator [opt]
14309 static cp_declarator *
14310 cp_parser_conversion_declarator_opt (cp_parser* parser)
14312 enum tree_code code;
14313 tree class_type, std_attributes = NULL_TREE;
14314 cp_cv_quals cv_quals;
14316 /* We don't know if there's a ptr-operator next, or not. */
14317 cp_parser_parse_tentatively (parser);
14318 /* Try the ptr-operator. */
14319 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14320 &std_attributes);
14321 /* If it worked, look for more conversion-declarators. */
14322 if (cp_parser_parse_definitely (parser))
14324 cp_declarator *declarator;
14326 /* Parse another optional declarator. */
14327 declarator = cp_parser_conversion_declarator_opt (parser);
14329 declarator = cp_parser_make_indirect_declarator
14330 (code, class_type, cv_quals, declarator, std_attributes);
14332 return declarator;
14335 return NULL;
14338 /* Parse an (optional) ctor-initializer.
14340 ctor-initializer:
14341 : mem-initializer-list */
14343 static void
14344 cp_parser_ctor_initializer_opt (cp_parser* parser)
14346 /* If the next token is not a `:', then there is no
14347 ctor-initializer. */
14348 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14350 /* Do default initialization of any bases and members. */
14351 if (DECL_CONSTRUCTOR_P (current_function_decl))
14352 finish_mem_initializers (NULL_TREE);
14353 return;
14356 /* Consume the `:' token. */
14357 cp_lexer_consume_token (parser->lexer);
14358 /* And the mem-initializer-list. */
14359 cp_parser_mem_initializer_list (parser);
14362 /* Parse a mem-initializer-list.
14364 mem-initializer-list:
14365 mem-initializer ... [opt]
14366 mem-initializer ... [opt] , mem-initializer-list */
14368 static void
14369 cp_parser_mem_initializer_list (cp_parser* parser)
14371 tree mem_initializer_list = NULL_TREE;
14372 tree target_ctor = error_mark_node;
14373 cp_token *token = cp_lexer_peek_token (parser->lexer);
14375 /* Let the semantic analysis code know that we are starting the
14376 mem-initializer-list. */
14377 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14378 error_at (token->location,
14379 "only constructors take member initializers");
14381 /* Loop through the list. */
14382 while (true)
14384 tree mem_initializer;
14386 token = cp_lexer_peek_token (parser->lexer);
14387 /* Parse the mem-initializer. */
14388 mem_initializer = cp_parser_mem_initializer (parser);
14389 /* If the next token is a `...', we're expanding member initializers. */
14390 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14392 /* Consume the `...'. */
14393 cp_lexer_consume_token (parser->lexer);
14395 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14396 can be expanded but members cannot. */
14397 if (mem_initializer != error_mark_node
14398 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14400 error_at (token->location,
14401 "cannot expand initializer for member %qD",
14402 TREE_PURPOSE (mem_initializer));
14403 mem_initializer = error_mark_node;
14406 /* Construct the pack expansion type. */
14407 if (mem_initializer != error_mark_node)
14408 mem_initializer = make_pack_expansion (mem_initializer);
14410 if (target_ctor != error_mark_node
14411 && mem_initializer != error_mark_node)
14413 error ("mem-initializer for %qD follows constructor delegation",
14414 TREE_PURPOSE (mem_initializer));
14415 mem_initializer = error_mark_node;
14417 /* Look for a target constructor. */
14418 if (mem_initializer != error_mark_node
14419 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14420 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14422 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14423 if (mem_initializer_list)
14425 error ("constructor delegation follows mem-initializer for %qD",
14426 TREE_PURPOSE (mem_initializer_list));
14427 mem_initializer = error_mark_node;
14429 target_ctor = mem_initializer;
14431 /* Add it to the list, unless it was erroneous. */
14432 if (mem_initializer != error_mark_node)
14434 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14435 mem_initializer_list = mem_initializer;
14437 /* If the next token is not a `,', we're done. */
14438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14439 break;
14440 /* Consume the `,' token. */
14441 cp_lexer_consume_token (parser->lexer);
14444 /* Perform semantic analysis. */
14445 if (DECL_CONSTRUCTOR_P (current_function_decl))
14446 finish_mem_initializers (mem_initializer_list);
14449 /* Parse a mem-initializer.
14451 mem-initializer:
14452 mem-initializer-id ( expression-list [opt] )
14453 mem-initializer-id braced-init-list
14455 GNU extension:
14457 mem-initializer:
14458 ( expression-list [opt] )
14460 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14461 class) or FIELD_DECL (for a non-static data member) to initialize;
14462 the TREE_VALUE is the expression-list. An empty initialization
14463 list is represented by void_list_node. */
14465 static tree
14466 cp_parser_mem_initializer (cp_parser* parser)
14468 tree mem_initializer_id;
14469 tree expression_list;
14470 tree member;
14471 cp_token *token = cp_lexer_peek_token (parser->lexer);
14473 /* Find out what is being initialized. */
14474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14476 permerror (token->location,
14477 "anachronistic old-style base class initializer");
14478 mem_initializer_id = NULL_TREE;
14480 else
14482 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14483 if (mem_initializer_id == error_mark_node)
14484 return mem_initializer_id;
14486 member = expand_member_init (mem_initializer_id);
14487 if (member && !DECL_P (member))
14488 in_base_initializer = 1;
14490 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14492 bool expr_non_constant_p;
14493 cp_lexer_set_source_position (parser->lexer);
14494 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14495 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14496 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14497 expression_list = build_tree_list (NULL_TREE, expression_list);
14499 else
14501 vec<tree, va_gc> *vec;
14502 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14503 /*cast_p=*/false,
14504 /*allow_expansion_p=*/true,
14505 /*non_constant_p=*/NULL);
14506 if (vec == NULL)
14507 return error_mark_node;
14508 expression_list = build_tree_list_vec (vec);
14509 release_tree_vector (vec);
14512 if (expression_list == error_mark_node)
14513 return error_mark_node;
14514 if (!expression_list)
14515 expression_list = void_type_node;
14517 in_base_initializer = 0;
14519 return member ? build_tree_list (member, expression_list) : error_mark_node;
14522 /* Parse a mem-initializer-id.
14524 mem-initializer-id:
14525 :: [opt] nested-name-specifier [opt] class-name
14526 decltype-specifier (C++11)
14527 identifier
14529 Returns a TYPE indicating the class to be initialized for the first
14530 production (and the second in C++11). Returns an IDENTIFIER_NODE
14531 indicating the data member to be initialized for the last production. */
14533 static tree
14534 cp_parser_mem_initializer_id (cp_parser* parser)
14536 bool global_scope_p;
14537 bool nested_name_specifier_p;
14538 bool template_p = false;
14539 tree id;
14541 cp_token *token = cp_lexer_peek_token (parser->lexer);
14543 /* `typename' is not allowed in this context ([temp.res]). */
14544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14546 error_at (token->location,
14547 "keyword %<typename%> not allowed in this context (a qualified "
14548 "member initializer is implicitly a type)");
14549 cp_lexer_consume_token (parser->lexer);
14551 /* Look for the optional `::' operator. */
14552 global_scope_p
14553 = (cp_parser_global_scope_opt (parser,
14554 /*current_scope_valid_p=*/false)
14555 != NULL_TREE);
14556 /* Look for the optional nested-name-specifier. The simplest way to
14557 implement:
14559 [temp.res]
14561 The keyword `typename' is not permitted in a base-specifier or
14562 mem-initializer; in these contexts a qualified name that
14563 depends on a template-parameter is implicitly assumed to be a
14564 type name.
14566 is to assume that we have seen the `typename' keyword at this
14567 point. */
14568 nested_name_specifier_p
14569 = (cp_parser_nested_name_specifier_opt (parser,
14570 /*typename_keyword_p=*/true,
14571 /*check_dependency_p=*/true,
14572 /*type_p=*/true,
14573 /*is_declaration=*/true)
14574 != NULL_TREE);
14575 if (nested_name_specifier_p)
14576 template_p = cp_parser_optional_template_keyword (parser);
14577 /* If there is a `::' operator or a nested-name-specifier, then we
14578 are definitely looking for a class-name. */
14579 if (global_scope_p || nested_name_specifier_p)
14580 return cp_parser_class_name (parser,
14581 /*typename_keyword_p=*/true,
14582 /*template_keyword_p=*/template_p,
14583 typename_type,
14584 /*check_dependency_p=*/true,
14585 /*class_head_p=*/false,
14586 /*is_declaration=*/true);
14587 /* Otherwise, we could also be looking for an ordinary identifier. */
14588 cp_parser_parse_tentatively (parser);
14589 if (cp_lexer_next_token_is_decltype (parser->lexer))
14590 /* Try a decltype-specifier. */
14591 id = cp_parser_decltype (parser);
14592 else
14593 /* Otherwise, try a class-name. */
14594 id = cp_parser_class_name (parser,
14595 /*typename_keyword_p=*/true,
14596 /*template_keyword_p=*/false,
14597 none_type,
14598 /*check_dependency_p=*/true,
14599 /*class_head_p=*/false,
14600 /*is_declaration=*/true);
14601 /* If we found one, we're done. */
14602 if (cp_parser_parse_definitely (parser))
14603 return id;
14604 /* Otherwise, look for an ordinary identifier. */
14605 return cp_parser_identifier (parser);
14608 /* Overloading [gram.over] */
14610 /* Parse an operator-function-id.
14612 operator-function-id:
14613 operator operator
14615 Returns an IDENTIFIER_NODE for the operator which is a
14616 human-readable spelling of the identifier, e.g., `operator +'. */
14618 static cp_expr
14619 cp_parser_operator_function_id (cp_parser* parser)
14621 /* Look for the `operator' keyword. */
14622 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14623 return error_mark_node;
14624 /* And then the name of the operator itself. */
14625 return cp_parser_operator (parser);
14628 /* Return an identifier node for a user-defined literal operator.
14629 The suffix identifier is chained to the operator name identifier. */
14631 tree
14632 cp_literal_operator_id (const char* name)
14634 tree identifier;
14635 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14636 + strlen (name) + 10);
14637 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14638 identifier = get_identifier (buffer);
14640 return identifier;
14643 /* Parse an operator.
14645 operator:
14646 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14647 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14648 || ++ -- , ->* -> () []
14650 GNU Extensions:
14652 operator:
14653 <? >? <?= >?=
14655 Returns an IDENTIFIER_NODE for the operator which is a
14656 human-readable spelling of the identifier, e.g., `operator +'. */
14658 static cp_expr
14659 cp_parser_operator (cp_parser* parser)
14661 tree id = NULL_TREE;
14662 cp_token *token;
14663 bool utf8 = false;
14665 /* Peek at the next token. */
14666 token = cp_lexer_peek_token (parser->lexer);
14668 location_t start_loc = token->location;
14670 /* Figure out which operator we have. */
14671 enum tree_code op = ERROR_MARK;
14672 bool assop = false;
14673 bool consumed = false;
14674 switch (token->type)
14676 case CPP_KEYWORD:
14678 /* The keyword should be either `new' or `delete'. */
14679 if (token->keyword == RID_NEW)
14680 op = NEW_EXPR;
14681 else if (token->keyword == RID_DELETE)
14682 op = DELETE_EXPR;
14683 else
14684 break;
14686 /* Consume the `new' or `delete' token. */
14687 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14689 /* Peek at the next token. */
14690 token = cp_lexer_peek_token (parser->lexer);
14691 /* If it's a `[' token then this is the array variant of the
14692 operator. */
14693 if (token->type == CPP_OPEN_SQUARE)
14695 /* Consume the `[' token. */
14696 cp_lexer_consume_token (parser->lexer);
14697 /* Look for the `]' token. */
14698 if (cp_token *close_token
14699 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14700 end_loc = close_token->location;
14701 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14703 start_loc = make_location (start_loc, start_loc, end_loc);
14704 consumed = true;
14705 break;
14708 case CPP_PLUS:
14709 op = PLUS_EXPR;
14710 break;
14712 case CPP_MINUS:
14713 op = MINUS_EXPR;
14714 break;
14716 case CPP_MULT:
14717 op = MULT_EXPR;
14718 break;
14720 case CPP_DIV:
14721 op = TRUNC_DIV_EXPR;
14722 break;
14724 case CPP_MOD:
14725 op = TRUNC_MOD_EXPR;
14726 break;
14728 case CPP_XOR:
14729 op = BIT_XOR_EXPR;
14730 break;
14732 case CPP_AND:
14733 op = BIT_AND_EXPR;
14734 break;
14736 case CPP_OR:
14737 op = BIT_IOR_EXPR;
14738 break;
14740 case CPP_COMPL:
14741 op = BIT_NOT_EXPR;
14742 break;
14744 case CPP_NOT:
14745 op = TRUTH_NOT_EXPR;
14746 break;
14748 case CPP_EQ:
14749 assop = true;
14750 op = NOP_EXPR;
14751 break;
14753 case CPP_LESS:
14754 op = LT_EXPR;
14755 break;
14757 case CPP_GREATER:
14758 op = GT_EXPR;
14759 break;
14761 case CPP_PLUS_EQ:
14762 assop = true;
14763 op = PLUS_EXPR;
14764 break;
14766 case CPP_MINUS_EQ:
14767 assop = true;
14768 op = MINUS_EXPR;
14769 break;
14771 case CPP_MULT_EQ:
14772 assop = true;
14773 op = MULT_EXPR;
14774 break;
14776 case CPP_DIV_EQ:
14777 assop = true;
14778 op = TRUNC_DIV_EXPR;
14779 break;
14781 case CPP_MOD_EQ:
14782 assop = true;
14783 op = TRUNC_MOD_EXPR;
14784 break;
14786 case CPP_XOR_EQ:
14787 assop = true;
14788 op = BIT_XOR_EXPR;
14789 break;
14791 case CPP_AND_EQ:
14792 assop = true;
14793 op = BIT_AND_EXPR;
14794 break;
14796 case CPP_OR_EQ:
14797 assop = true;
14798 op = BIT_IOR_EXPR;
14799 break;
14801 case CPP_LSHIFT:
14802 op = LSHIFT_EXPR;
14803 break;
14805 case CPP_RSHIFT:
14806 op = RSHIFT_EXPR;
14807 break;
14809 case CPP_LSHIFT_EQ:
14810 assop = true;
14811 op = LSHIFT_EXPR;
14812 break;
14814 case CPP_RSHIFT_EQ:
14815 assop = true;
14816 op = RSHIFT_EXPR;
14817 break;
14819 case CPP_EQ_EQ:
14820 op = EQ_EXPR;
14821 break;
14823 case CPP_NOT_EQ:
14824 op = NE_EXPR;
14825 break;
14827 case CPP_LESS_EQ:
14828 op = LE_EXPR;
14829 break;
14831 case CPP_GREATER_EQ:
14832 op = GE_EXPR;
14833 break;
14835 case CPP_AND_AND:
14836 op = TRUTH_ANDIF_EXPR;
14837 break;
14839 case CPP_OR_OR:
14840 op = TRUTH_ORIF_EXPR;
14841 break;
14843 case CPP_PLUS_PLUS:
14844 op = POSTINCREMENT_EXPR;
14845 break;
14847 case CPP_MINUS_MINUS:
14848 op = PREDECREMENT_EXPR;
14849 break;
14851 case CPP_COMMA:
14852 op = COMPOUND_EXPR;
14853 break;
14855 case CPP_DEREF_STAR:
14856 op = MEMBER_REF;
14857 break;
14859 case CPP_DEREF:
14860 op = COMPONENT_REF;
14861 break;
14863 case CPP_OPEN_PAREN:
14865 /* Consume the `('. */
14866 matching_parens parens;
14867 parens.consume_open (parser);
14868 /* Look for the matching `)'. */
14869 parens.require_close (parser);
14870 op = CALL_EXPR;
14871 consumed = true;
14872 break;
14875 case CPP_OPEN_SQUARE:
14876 /* Consume the `['. */
14877 cp_lexer_consume_token (parser->lexer);
14878 /* Look for the matching `]'. */
14879 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14880 op = ARRAY_REF;
14881 consumed = true;
14882 break;
14884 case CPP_UTF8STRING:
14885 case CPP_UTF8STRING_USERDEF:
14886 utf8 = true;
14887 /* FALLTHRU */
14888 case CPP_STRING:
14889 case CPP_WSTRING:
14890 case CPP_STRING16:
14891 case CPP_STRING32:
14892 case CPP_STRING_USERDEF:
14893 case CPP_WSTRING_USERDEF:
14894 case CPP_STRING16_USERDEF:
14895 case CPP_STRING32_USERDEF:
14897 tree str, string_tree;
14898 int sz, len;
14900 if (cxx_dialect == cxx98)
14901 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14903 /* Consume the string. */
14904 str = cp_parser_string_literal (parser, /*translate=*/true,
14905 /*wide_ok=*/true, /*lookup_udlit=*/false);
14906 if (str == error_mark_node)
14907 return error_mark_node;
14908 else if (TREE_CODE (str) == USERDEF_LITERAL)
14910 string_tree = USERDEF_LITERAL_VALUE (str);
14911 id = USERDEF_LITERAL_SUFFIX_ID (str);
14913 else
14915 string_tree = str;
14916 /* Look for the suffix identifier. */
14917 token = cp_lexer_peek_token (parser->lexer);
14918 if (token->type == CPP_NAME)
14919 id = cp_parser_identifier (parser);
14920 else if (token->type == CPP_KEYWORD)
14922 error ("unexpected keyword;"
14923 " remove space between quotes and suffix identifier");
14924 return error_mark_node;
14926 else
14928 error ("expected suffix identifier");
14929 return error_mark_node;
14932 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14933 (TREE_TYPE (TREE_TYPE (string_tree))));
14934 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14935 if (len != 0)
14937 error ("expected empty string after %<operator%> keyword");
14938 return error_mark_node;
14940 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14941 != char_type_node)
14943 error ("invalid encoding prefix in literal operator");
14944 return error_mark_node;
14946 if (id != error_mark_node)
14948 const char *name = IDENTIFIER_POINTER (id);
14949 id = cp_literal_operator_id (name);
14951 return id;
14954 default:
14955 /* Anything else is an error. */
14956 break;
14959 /* If we have selected an identifier, we need to consume the
14960 operator token. */
14961 if (op != ERROR_MARK)
14963 id = ovl_op_identifier (assop, op);
14964 if (!consumed)
14965 cp_lexer_consume_token (parser->lexer);
14967 /* Otherwise, no valid operator name was present. */
14968 else
14970 cp_parser_error (parser, "expected operator");
14971 id = error_mark_node;
14974 return cp_expr (id, start_loc);
14977 /* Parse a template-declaration.
14979 template-declaration:
14980 export [opt] template < template-parameter-list > declaration
14982 If MEMBER_P is TRUE, this template-declaration occurs within a
14983 class-specifier.
14985 The grammar rule given by the standard isn't correct. What
14986 is really meant is:
14988 template-declaration:
14989 export [opt] template-parameter-list-seq
14990 decl-specifier-seq [opt] init-declarator [opt] ;
14991 export [opt] template-parameter-list-seq
14992 function-definition
14994 template-parameter-list-seq:
14995 template-parameter-list-seq [opt]
14996 template < template-parameter-list >
14998 Concept Extensions:
15000 template-parameter-list-seq:
15001 template < template-parameter-list > requires-clause [opt]
15003 requires-clause:
15004 requires logical-or-expression */
15006 static void
15007 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15009 /* Check for `export'. */
15010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15012 /* Consume the `export' token. */
15013 cp_lexer_consume_token (parser->lexer);
15014 /* Warn that we do not support `export'. */
15015 warning (0, "keyword %<export%> not implemented, and will be ignored");
15018 cp_parser_template_declaration_after_export (parser, member_p);
15021 /* Parse a template-parameter-list.
15023 template-parameter-list:
15024 template-parameter
15025 template-parameter-list , template-parameter
15027 Returns a TREE_LIST. Each node represents a template parameter.
15028 The nodes are connected via their TREE_CHAINs. */
15030 static tree
15031 cp_parser_template_parameter_list (cp_parser* parser)
15033 tree parameter_list = NULL_TREE;
15035 begin_template_parm_list ();
15037 /* The loop below parses the template parms. We first need to know
15038 the total number of template parms to be able to compute proper
15039 canonical types of each dependent type. So after the loop, when
15040 we know the total number of template parms,
15041 end_template_parm_list computes the proper canonical types and
15042 fixes up the dependent types accordingly. */
15043 while (true)
15045 tree parameter;
15046 bool is_non_type;
15047 bool is_parameter_pack;
15048 location_t parm_loc;
15050 /* Parse the template-parameter. */
15051 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15052 parameter = cp_parser_template_parameter (parser,
15053 &is_non_type,
15054 &is_parameter_pack);
15055 /* Add it to the list. */
15056 if (parameter != error_mark_node)
15057 parameter_list = process_template_parm (parameter_list,
15058 parm_loc,
15059 parameter,
15060 is_non_type,
15061 is_parameter_pack);
15062 else
15064 tree err_parm = build_tree_list (parameter, parameter);
15065 parameter_list = chainon (parameter_list, err_parm);
15068 /* If the next token is not a `,', we're done. */
15069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15070 break;
15071 /* Otherwise, consume the `,' token. */
15072 cp_lexer_consume_token (parser->lexer);
15075 return end_template_parm_list (parameter_list);
15078 /* Parse a introduction-list.
15080 introduction-list:
15081 introduced-parameter
15082 introduction-list , introduced-parameter
15084 introduced-parameter:
15085 ...[opt] identifier
15087 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15088 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15089 WILDCARD_DECL will also have DECL_NAME set and token location in
15090 DECL_SOURCE_LOCATION. */
15092 static tree
15093 cp_parser_introduction_list (cp_parser *parser)
15095 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15097 while (true)
15099 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15100 if (is_pack)
15101 cp_lexer_consume_token (parser->lexer);
15103 /* Build placeholder. */
15104 tree parm = build_nt (WILDCARD_DECL);
15105 DECL_SOURCE_LOCATION (parm)
15106 = cp_lexer_peek_token (parser->lexer)->location;
15107 DECL_NAME (parm) = cp_parser_identifier (parser);
15108 WILDCARD_PACK_P (parm) = is_pack;
15109 vec_safe_push (introduction_vec, parm);
15111 /* If the next token is not a `,', we're done. */
15112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15113 break;
15114 /* Otherwise, consume the `,' token. */
15115 cp_lexer_consume_token (parser->lexer);
15118 /* Convert the vec into a TREE_VEC. */
15119 tree introduction_list = make_tree_vec (introduction_vec->length ());
15120 unsigned int n;
15121 tree parm;
15122 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15123 TREE_VEC_ELT (introduction_list, n) = parm;
15125 release_tree_vector (introduction_vec);
15126 return introduction_list;
15129 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15130 is an abstract declarator. */
15132 static inline cp_declarator*
15133 get_id_declarator (cp_declarator *declarator)
15135 cp_declarator *d = declarator;
15136 while (d && d->kind != cdk_id)
15137 d = d->declarator;
15138 return d;
15141 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15142 is an abstract declarator. */
15144 static inline tree
15145 get_unqualified_id (cp_declarator *declarator)
15147 declarator = get_id_declarator (declarator);
15148 if (declarator)
15149 return declarator->u.id.unqualified_name;
15150 else
15151 return NULL_TREE;
15154 /* Returns true if DECL represents a constrained-parameter. */
15156 static inline bool
15157 is_constrained_parameter (tree decl)
15159 return (decl
15160 && TREE_CODE (decl) == TYPE_DECL
15161 && CONSTRAINED_PARM_CONCEPT (decl)
15162 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15165 /* Returns true if PARM declares a constrained-parameter. */
15167 static inline bool
15168 is_constrained_parameter (cp_parameter_declarator *parm)
15170 return is_constrained_parameter (parm->decl_specifiers.type);
15173 /* Check that the type parameter is only a declarator-id, and that its
15174 type is not cv-qualified. */
15176 bool
15177 cp_parser_check_constrained_type_parm (cp_parser *parser,
15178 cp_parameter_declarator *parm)
15180 if (!parm->declarator)
15181 return true;
15183 if (parm->declarator->kind != cdk_id)
15185 cp_parser_error (parser, "invalid constrained type parameter");
15186 return false;
15189 /* Don't allow cv-qualified type parameters. */
15190 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15191 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15193 cp_parser_error (parser, "cv-qualified type parameter");
15194 return false;
15197 return true;
15200 /* Finish parsing/processing a template type parameter and checking
15201 various restrictions. */
15203 static inline tree
15204 cp_parser_constrained_type_template_parm (cp_parser *parser,
15205 tree id,
15206 cp_parameter_declarator* parmdecl)
15208 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15209 return finish_template_type_parm (class_type_node, id);
15210 else
15211 return error_mark_node;
15214 static tree
15215 finish_constrained_template_template_parm (tree proto, tree id)
15217 /* FIXME: This should probably be copied, and we may need to adjust
15218 the template parameter depths. */
15219 tree saved_parms = current_template_parms;
15220 begin_template_parm_list ();
15221 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15222 end_template_parm_list ();
15224 tree parm = finish_template_template_parm (class_type_node, id);
15225 current_template_parms = saved_parms;
15227 return parm;
15230 /* Finish parsing/processing a template template parameter by borrowing
15231 the template parameter list from the prototype parameter. */
15233 static tree
15234 cp_parser_constrained_template_template_parm (cp_parser *parser,
15235 tree proto,
15236 tree id,
15237 cp_parameter_declarator *parmdecl)
15239 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15240 return error_mark_node;
15241 return finish_constrained_template_template_parm (proto, id);
15244 /* Create a new non-type template parameter from the given PARM
15245 declarator. */
15247 static tree
15248 constrained_non_type_template_parm (bool *is_non_type,
15249 cp_parameter_declarator *parm)
15251 *is_non_type = true;
15252 cp_declarator *decl = parm->declarator;
15253 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15254 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15255 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15258 /* Build a constrained template parameter based on the PARMDECL
15259 declarator. The type of PARMDECL is the constrained type, which
15260 refers to the prototype template parameter that ultimately
15261 specifies the type of the declared parameter. */
15263 static tree
15264 finish_constrained_parameter (cp_parser *parser,
15265 cp_parameter_declarator *parmdecl,
15266 bool *is_non_type,
15267 bool *is_parameter_pack)
15269 tree decl = parmdecl->decl_specifiers.type;
15270 tree id = get_unqualified_id (parmdecl->declarator);
15271 tree def = parmdecl->default_argument;
15272 tree proto = DECL_INITIAL (decl);
15274 /* A template parameter constrained by a variadic concept shall also
15275 be declared as a template parameter pack. */
15276 bool is_variadic = template_parameter_pack_p (proto);
15277 if (is_variadic && !*is_parameter_pack)
15278 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15280 /* Build the parameter. Return an error if the declarator was invalid. */
15281 tree parm;
15282 if (TREE_CODE (proto) == TYPE_DECL)
15283 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15284 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15285 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15286 parmdecl);
15287 else
15288 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15289 if (parm == error_mark_node)
15290 return error_mark_node;
15292 /* Finish the parameter decl and create a node attaching the
15293 default argument and constraint. */
15294 parm = build_tree_list (def, parm);
15295 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15297 return parm;
15300 /* Returns true if the parsed type actually represents the declaration
15301 of a type template-parameter. */
15303 static inline bool
15304 declares_constrained_type_template_parameter (tree type)
15306 return (is_constrained_parameter (type)
15307 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15311 /* Returns true if the parsed type actually represents the declaration of
15312 a template template-parameter. */
15314 static bool
15315 declares_constrained_template_template_parameter (tree type)
15317 return (is_constrained_parameter (type)
15318 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15321 /* Parse a default argument for a type template-parameter.
15322 Note that diagnostics are handled in cp_parser_template_parameter. */
15324 static tree
15325 cp_parser_default_type_template_argument (cp_parser *parser)
15327 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15329 /* Consume the `=' token. */
15330 cp_lexer_consume_token (parser->lexer);
15332 cp_token *token = cp_lexer_peek_token (parser->lexer);
15334 /* Parse the default-argument. */
15335 push_deferring_access_checks (dk_no_deferred);
15336 tree default_argument = cp_parser_type_id (parser);
15337 pop_deferring_access_checks ();
15339 if (flag_concepts && type_uses_auto (default_argument))
15341 error_at (token->location,
15342 "invalid use of %<auto%> in default template argument");
15343 return error_mark_node;
15346 return default_argument;
15349 /* Parse a default argument for a template template-parameter. */
15351 static tree
15352 cp_parser_default_template_template_argument (cp_parser *parser)
15354 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15356 bool is_template;
15358 /* Consume the `='. */
15359 cp_lexer_consume_token (parser->lexer);
15360 /* Parse the id-expression. */
15361 push_deferring_access_checks (dk_no_deferred);
15362 /* save token before parsing the id-expression, for error
15363 reporting */
15364 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15365 tree default_argument
15366 = cp_parser_id_expression (parser,
15367 /*template_keyword_p=*/false,
15368 /*check_dependency_p=*/true,
15369 /*template_p=*/&is_template,
15370 /*declarator_p=*/false,
15371 /*optional_p=*/false);
15372 if (TREE_CODE (default_argument) == TYPE_DECL)
15373 /* If the id-expression was a template-id that refers to
15374 a template-class, we already have the declaration here,
15375 so no further lookup is needed. */
15377 else
15378 /* Look up the name. */
15379 default_argument
15380 = cp_parser_lookup_name (parser, default_argument,
15381 none_type,
15382 /*is_template=*/is_template,
15383 /*is_namespace=*/false,
15384 /*check_dependency=*/true,
15385 /*ambiguous_decls=*/NULL,
15386 token->location);
15387 /* See if the default argument is valid. */
15388 default_argument = check_template_template_default_arg (default_argument);
15389 pop_deferring_access_checks ();
15390 return default_argument;
15393 /* Parse a template-parameter.
15395 template-parameter:
15396 type-parameter
15397 parameter-declaration
15399 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15400 the parameter. The TREE_PURPOSE is the default value, if any.
15401 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15402 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15403 set to true iff this parameter is a parameter pack. */
15405 static tree
15406 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15407 bool *is_parameter_pack)
15409 cp_token *token;
15410 cp_parameter_declarator *parameter_declarator;
15411 tree parm;
15413 /* Assume it is a type parameter or a template parameter. */
15414 *is_non_type = false;
15415 /* Assume it not a parameter pack. */
15416 *is_parameter_pack = false;
15417 /* Peek at the next token. */
15418 token = cp_lexer_peek_token (parser->lexer);
15419 /* If it is `template', we have a type-parameter. */
15420 if (token->keyword == RID_TEMPLATE)
15421 return cp_parser_type_parameter (parser, is_parameter_pack);
15422 /* If it is `class' or `typename' we do not know yet whether it is a
15423 type parameter or a non-type parameter. Consider:
15425 template <typename T, typename T::X X> ...
15429 template <class C, class D*> ...
15431 Here, the first parameter is a type parameter, and the second is
15432 a non-type parameter. We can tell by looking at the token after
15433 the identifier -- if it is a `,', `=', or `>' then we have a type
15434 parameter. */
15435 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15437 /* Peek at the token after `class' or `typename'. */
15438 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15439 /* If it's an ellipsis, we have a template type parameter
15440 pack. */
15441 if (token->type == CPP_ELLIPSIS)
15442 return cp_parser_type_parameter (parser, is_parameter_pack);
15443 /* If it's an identifier, skip it. */
15444 if (token->type == CPP_NAME)
15445 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15446 /* Now, see if the token looks like the end of a template
15447 parameter. */
15448 if (token->type == CPP_COMMA
15449 || token->type == CPP_EQ
15450 || token->type == CPP_GREATER)
15451 return cp_parser_type_parameter (parser, is_parameter_pack);
15454 /* Otherwise, it is a non-type parameter or a constrained parameter.
15456 [temp.param]
15458 When parsing a default template-argument for a non-type
15459 template-parameter, the first non-nested `>' is taken as the end
15460 of the template parameter-list rather than a greater-than
15461 operator. */
15462 parameter_declarator
15463 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15464 /*parenthesized_p=*/NULL);
15466 if (!parameter_declarator)
15467 return error_mark_node;
15469 /* If the parameter declaration is marked as a parameter pack, set
15470 *IS_PARAMETER_PACK to notify the caller. */
15471 if (parameter_declarator->template_parameter_pack_p)
15472 *is_parameter_pack = true;
15474 if (parameter_declarator->default_argument)
15476 /* Can happen in some cases of erroneous input (c++/34892). */
15477 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15478 /* Consume the `...' for better error recovery. */
15479 cp_lexer_consume_token (parser->lexer);
15482 // The parameter may have been constrained.
15483 if (is_constrained_parameter (parameter_declarator))
15484 return finish_constrained_parameter (parser,
15485 parameter_declarator,
15486 is_non_type,
15487 is_parameter_pack);
15489 // Now we're sure that the parameter is a non-type parameter.
15490 *is_non_type = true;
15492 parm = grokdeclarator (parameter_declarator->declarator,
15493 &parameter_declarator->decl_specifiers,
15494 TPARM, /*initialized=*/0,
15495 /*attrlist=*/NULL);
15496 if (parm == error_mark_node)
15497 return error_mark_node;
15499 return build_tree_list (parameter_declarator->default_argument, parm);
15502 /* Parse a type-parameter.
15504 type-parameter:
15505 class identifier [opt]
15506 class identifier [opt] = type-id
15507 typename identifier [opt]
15508 typename identifier [opt] = type-id
15509 template < template-parameter-list > class identifier [opt]
15510 template < template-parameter-list > class identifier [opt]
15511 = id-expression
15513 GNU Extension (variadic templates):
15515 type-parameter:
15516 class ... identifier [opt]
15517 typename ... identifier [opt]
15519 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15520 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15521 the declaration of the parameter.
15523 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15525 static tree
15526 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15528 cp_token *token;
15529 tree parameter;
15531 /* Look for a keyword to tell us what kind of parameter this is. */
15532 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15533 if (!token)
15534 return error_mark_node;
15536 switch (token->keyword)
15538 case RID_CLASS:
15539 case RID_TYPENAME:
15541 tree identifier;
15542 tree default_argument;
15544 /* If the next token is an ellipsis, we have a template
15545 argument pack. */
15546 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15548 /* Consume the `...' token. */
15549 cp_lexer_consume_token (parser->lexer);
15550 maybe_warn_variadic_templates ();
15552 *is_parameter_pack = true;
15555 /* If the next token is an identifier, then it names the
15556 parameter. */
15557 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15558 identifier = cp_parser_identifier (parser);
15559 else
15560 identifier = NULL_TREE;
15562 /* Create the parameter. */
15563 parameter = finish_template_type_parm (class_type_node, identifier);
15565 /* If the next token is an `=', we have a default argument. */
15566 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15568 default_argument
15569 = cp_parser_default_type_template_argument (parser);
15571 /* Template parameter packs cannot have default
15572 arguments. */
15573 if (*is_parameter_pack)
15575 if (identifier)
15576 error_at (token->location,
15577 "template parameter pack %qD cannot have a "
15578 "default argument", identifier);
15579 else
15580 error_at (token->location,
15581 "template parameter packs cannot have "
15582 "default arguments");
15583 default_argument = NULL_TREE;
15585 else if (check_for_bare_parameter_packs (default_argument))
15586 default_argument = error_mark_node;
15588 else
15589 default_argument = NULL_TREE;
15591 /* Create the combined representation of the parameter and the
15592 default argument. */
15593 parameter = build_tree_list (default_argument, parameter);
15595 break;
15597 case RID_TEMPLATE:
15599 tree identifier;
15600 tree default_argument;
15602 /* Look for the `<'. */
15603 cp_parser_require (parser, CPP_LESS, RT_LESS);
15604 /* Parse the template-parameter-list. */
15605 cp_parser_template_parameter_list (parser);
15606 /* Look for the `>'. */
15607 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15609 // If template requirements are present, parse them.
15610 if (flag_concepts)
15612 tree reqs = get_shorthand_constraints (current_template_parms);
15613 if (tree r = cp_parser_requires_clause_opt (parser))
15614 reqs = conjoin_constraints (reqs, normalize_expression (r));
15615 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15618 /* Look for the `class' or 'typename' keywords. */
15619 cp_parser_type_parameter_key (parser);
15620 /* If the next token is an ellipsis, we have a template
15621 argument pack. */
15622 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15624 /* Consume the `...' token. */
15625 cp_lexer_consume_token (parser->lexer);
15626 maybe_warn_variadic_templates ();
15628 *is_parameter_pack = true;
15630 /* If the next token is an `=', then there is a
15631 default-argument. If the next token is a `>', we are at
15632 the end of the parameter-list. If the next token is a `,',
15633 then we are at the end of this parameter. */
15634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15635 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15636 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15638 identifier = cp_parser_identifier (parser);
15639 /* Treat invalid names as if the parameter were nameless. */
15640 if (identifier == error_mark_node)
15641 identifier = NULL_TREE;
15643 else
15644 identifier = NULL_TREE;
15646 /* Create the template parameter. */
15647 parameter = finish_template_template_parm (class_type_node,
15648 identifier);
15650 /* If the next token is an `=', then there is a
15651 default-argument. */
15652 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15654 default_argument
15655 = cp_parser_default_template_template_argument (parser);
15657 /* Template parameter packs cannot have default
15658 arguments. */
15659 if (*is_parameter_pack)
15661 if (identifier)
15662 error_at (token->location,
15663 "template parameter pack %qD cannot "
15664 "have a default argument",
15665 identifier);
15666 else
15667 error_at (token->location, "template parameter packs cannot "
15668 "have default arguments");
15669 default_argument = NULL_TREE;
15672 else
15673 default_argument = NULL_TREE;
15675 /* Create the combined representation of the parameter and the
15676 default argument. */
15677 parameter = build_tree_list (default_argument, parameter);
15679 break;
15681 default:
15682 gcc_unreachable ();
15683 break;
15686 return parameter;
15689 /* Parse a template-id.
15691 template-id:
15692 template-name < template-argument-list [opt] >
15694 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15695 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15696 returned. Otherwise, if the template-name names a function, or set
15697 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15698 names a class, returns a TYPE_DECL for the specialization.
15700 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15701 uninstantiated templates. */
15703 static tree
15704 cp_parser_template_id (cp_parser *parser,
15705 bool template_keyword_p,
15706 bool check_dependency_p,
15707 enum tag_types tag_type,
15708 bool is_declaration)
15710 tree templ;
15711 tree arguments;
15712 tree template_id;
15713 cp_token_position start_of_id = 0;
15714 cp_token *next_token = NULL, *next_token_2 = NULL;
15715 bool is_identifier;
15717 /* If the next token corresponds to a template-id, there is no need
15718 to reparse it. */
15719 cp_token *token = cp_lexer_peek_token (parser->lexer);
15720 if (token->type == CPP_TEMPLATE_ID)
15722 cp_lexer_consume_token (parser->lexer);
15723 return saved_checks_value (token->u.tree_check_value);
15726 /* Avoid performing name lookup if there is no possibility of
15727 finding a template-id. */
15728 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15729 || (token->type == CPP_NAME
15730 && !cp_parser_nth_token_starts_template_argument_list_p
15731 (parser, 2)))
15733 cp_parser_error (parser, "expected template-id");
15734 return error_mark_node;
15737 /* Remember where the template-id starts. */
15738 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15739 start_of_id = cp_lexer_token_position (parser->lexer, false);
15741 push_deferring_access_checks (dk_deferred);
15743 /* Parse the template-name. */
15744 is_identifier = false;
15745 templ = cp_parser_template_name (parser, template_keyword_p,
15746 check_dependency_p,
15747 is_declaration,
15748 tag_type,
15749 &is_identifier);
15750 if (templ == error_mark_node || is_identifier)
15752 pop_deferring_access_checks ();
15753 return templ;
15756 /* Since we're going to preserve any side-effects from this parse, set up a
15757 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15758 in the template arguments. */
15759 tentative_firewall firewall (parser);
15761 /* If we find the sequence `[:' after a template-name, it's probably
15762 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15763 parse correctly the argument list. */
15764 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15765 == CPP_OPEN_SQUARE)
15766 && next_token->flags & DIGRAPH
15767 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15768 == CPP_COLON)
15769 && !(next_token_2->flags & PREV_WHITE))
15771 cp_parser_parse_tentatively (parser);
15772 /* Change `:' into `::'. */
15773 next_token_2->type = CPP_SCOPE;
15774 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15775 CPP_LESS. */
15776 cp_lexer_consume_token (parser->lexer);
15778 /* Parse the arguments. */
15779 arguments = cp_parser_enclosed_template_argument_list (parser);
15780 if (!cp_parser_parse_definitely (parser))
15782 /* If we couldn't parse an argument list, then we revert our changes
15783 and return simply an error. Maybe this is not a template-id
15784 after all. */
15785 next_token_2->type = CPP_COLON;
15786 cp_parser_error (parser, "expected %<<%>");
15787 pop_deferring_access_checks ();
15788 return error_mark_node;
15790 /* Otherwise, emit an error about the invalid digraph, but continue
15791 parsing because we got our argument list. */
15792 if (permerror (next_token->location,
15793 "%<<::%> cannot begin a template-argument list"))
15795 static bool hint = false;
15796 inform (next_token->location,
15797 "%<<:%> is an alternate spelling for %<[%>."
15798 " Insert whitespace between %<<%> and %<::%>");
15799 if (!hint && !flag_permissive)
15801 inform (next_token->location, "(if you use %<-fpermissive%> "
15802 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15803 "accept your code)");
15804 hint = true;
15808 else
15810 /* Look for the `<' that starts the template-argument-list. */
15811 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15813 pop_deferring_access_checks ();
15814 return error_mark_node;
15816 /* Parse the arguments. */
15817 arguments = cp_parser_enclosed_template_argument_list (parser);
15820 /* Set the location to be of the form:
15821 template-name < template-argument-list [opt] >
15822 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15823 with caret == start at the start of the template-name,
15824 ranging until the closing '>'. */
15825 location_t finish_loc
15826 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15827 location_t combined_loc
15828 = make_location (token->location, token->location, finish_loc);
15830 /* Build a representation of the specialization. */
15831 if (identifier_p (templ))
15832 template_id = build_min_nt_loc (combined_loc,
15833 TEMPLATE_ID_EXPR,
15834 templ, arguments);
15835 else if (DECL_TYPE_TEMPLATE_P (templ)
15836 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15838 bool entering_scope;
15839 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15840 template (rather than some instantiation thereof) only if
15841 is not nested within some other construct. For example, in
15842 "template <typename T> void f(T) { A<T>::", A<T> is just an
15843 instantiation of A. */
15844 entering_scope = (template_parm_scope_p ()
15845 && cp_lexer_next_token_is (parser->lexer,
15846 CPP_SCOPE));
15847 template_id
15848 = finish_template_type (templ, arguments, entering_scope);
15850 /* A template-like identifier may be a partial concept id. */
15851 else if (flag_concepts
15852 && (template_id = (cp_parser_maybe_partial_concept_id
15853 (parser, templ, arguments))))
15854 return template_id;
15855 else if (variable_template_p (templ))
15857 template_id = lookup_template_variable (templ, arguments);
15858 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15859 SET_EXPR_LOCATION (template_id, combined_loc);
15861 else
15863 /* If it's not a class-template or a template-template, it should be
15864 a function-template. */
15865 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15866 || TREE_CODE (templ) == OVERLOAD
15867 || BASELINK_P (templ)));
15869 template_id = lookup_template_function (templ, arguments);
15870 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15871 SET_EXPR_LOCATION (template_id, combined_loc);
15874 /* If parsing tentatively, replace the sequence of tokens that makes
15875 up the template-id with a CPP_TEMPLATE_ID token. That way,
15876 should we re-parse the token stream, we will not have to repeat
15877 the effort required to do the parse, nor will we issue duplicate
15878 error messages about problems during instantiation of the
15879 template. */
15880 if (start_of_id
15881 /* Don't do this if we had a parse error in a declarator; re-parsing
15882 might succeed if a name changes meaning (60361). */
15883 && !(cp_parser_error_occurred (parser)
15884 && cp_parser_parsing_tentatively (parser)
15885 && parser->in_declarator_p))
15887 /* Reset the contents of the START_OF_ID token. */
15888 token->type = CPP_TEMPLATE_ID;
15889 token->location = combined_loc;
15891 /* We must mark the lookup as kept, so we don't throw it away on
15892 the first parse. */
15893 if (is_overloaded_fn (template_id))
15894 lookup_keep (get_fns (template_id), true);
15896 /* Retrieve any deferred checks. Do not pop this access checks yet
15897 so the memory will not be reclaimed during token replacing below. */
15898 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15899 token->u.tree_check_value->value = template_id;
15900 token->u.tree_check_value->checks = get_deferred_access_checks ();
15901 token->keyword = RID_MAX;
15903 /* Purge all subsequent tokens. */
15904 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15906 /* ??? Can we actually assume that, if template_id ==
15907 error_mark_node, we will have issued a diagnostic to the
15908 user, as opposed to simply marking the tentative parse as
15909 failed? */
15910 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15911 error_at (token->location, "parse error in template argument list");
15914 pop_to_parent_deferring_access_checks ();
15915 return template_id;
15918 /* Parse a template-name.
15920 template-name:
15921 identifier
15923 The standard should actually say:
15925 template-name:
15926 identifier
15927 operator-function-id
15929 A defect report has been filed about this issue.
15931 A conversion-function-id cannot be a template name because they cannot
15932 be part of a template-id. In fact, looking at this code:
15934 a.operator K<int>()
15936 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15937 It is impossible to call a templated conversion-function-id with an
15938 explicit argument list, since the only allowed template parameter is
15939 the type to which it is converting.
15941 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15942 `template' keyword, in a construction like:
15944 T::template f<3>()
15946 In that case `f' is taken to be a template-name, even though there
15947 is no way of knowing for sure.
15949 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15950 name refers to a set of overloaded functions, at least one of which
15951 is a template, or an IDENTIFIER_NODE with the name of the template,
15952 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15953 names are looked up inside uninstantiated templates. */
15955 static tree
15956 cp_parser_template_name (cp_parser* parser,
15957 bool template_keyword_p,
15958 bool check_dependency_p,
15959 bool is_declaration,
15960 enum tag_types tag_type,
15961 bool *is_identifier)
15963 tree identifier;
15964 tree decl;
15965 cp_token *token = cp_lexer_peek_token (parser->lexer);
15967 /* If the next token is `operator', then we have either an
15968 operator-function-id or a conversion-function-id. */
15969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15971 /* We don't know whether we're looking at an
15972 operator-function-id or a conversion-function-id. */
15973 cp_parser_parse_tentatively (parser);
15974 /* Try an operator-function-id. */
15975 identifier = cp_parser_operator_function_id (parser);
15976 /* If that didn't work, try a conversion-function-id. */
15977 if (!cp_parser_parse_definitely (parser))
15979 cp_parser_error (parser, "expected template-name");
15980 return error_mark_node;
15983 /* Look for the identifier. */
15984 else
15985 identifier = cp_parser_identifier (parser);
15987 /* If we didn't find an identifier, we don't have a template-id. */
15988 if (identifier == error_mark_node)
15989 return error_mark_node;
15991 /* If the name immediately followed the `template' keyword, then it
15992 is a template-name. However, if the next token is not `<', then
15993 we do not treat it as a template-name, since it is not being used
15994 as part of a template-id. This enables us to handle constructs
15995 like:
15997 template <typename T> struct S { S(); };
15998 template <typename T> S<T>::S();
16000 correctly. We would treat `S' as a template -- if it were `S<T>'
16001 -- but we do not if there is no `<'. */
16003 if (processing_template_decl
16004 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16006 /* In a declaration, in a dependent context, we pretend that the
16007 "template" keyword was present in order to improve error
16008 recovery. For example, given:
16010 template <typename T> void f(T::X<int>);
16012 we want to treat "X<int>" as a template-id. */
16013 if (is_declaration
16014 && !template_keyword_p
16015 && parser->scope && TYPE_P (parser->scope)
16016 && check_dependency_p
16017 && dependent_scope_p (parser->scope)
16018 /* Do not do this for dtors (or ctors), since they never
16019 need the template keyword before their name. */
16020 && !constructor_name_p (identifier, parser->scope))
16022 cp_token_position start = 0;
16024 /* Explain what went wrong. */
16025 error_at (token->location, "non-template %qD used as template",
16026 identifier);
16027 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16028 parser->scope, identifier);
16029 /* If parsing tentatively, find the location of the "<" token. */
16030 if (cp_parser_simulate_error (parser))
16031 start = cp_lexer_token_position (parser->lexer, true);
16032 /* Parse the template arguments so that we can issue error
16033 messages about them. */
16034 cp_lexer_consume_token (parser->lexer);
16035 cp_parser_enclosed_template_argument_list (parser);
16036 /* Skip tokens until we find a good place from which to
16037 continue parsing. */
16038 cp_parser_skip_to_closing_parenthesis (parser,
16039 /*recovering=*/true,
16040 /*or_comma=*/true,
16041 /*consume_paren=*/false);
16042 /* If parsing tentatively, permanently remove the
16043 template argument list. That will prevent duplicate
16044 error messages from being issued about the missing
16045 "template" keyword. */
16046 if (start)
16047 cp_lexer_purge_tokens_after (parser->lexer, start);
16048 if (is_identifier)
16049 *is_identifier = true;
16050 parser->context->object_type = NULL_TREE;
16051 return identifier;
16054 /* If the "template" keyword is present, then there is generally
16055 no point in doing name-lookup, so we just return IDENTIFIER.
16056 But, if the qualifying scope is non-dependent then we can
16057 (and must) do name-lookup normally. */
16058 if (template_keyword_p)
16060 tree scope = (parser->scope ? parser->scope
16061 : parser->context->object_type);
16062 if (scope && TYPE_P (scope)
16063 && (!CLASS_TYPE_P (scope)
16064 || (check_dependency_p && dependent_type_p (scope))))
16066 /* We're optimizing away the call to cp_parser_lookup_name, but
16067 we still need to do this. */
16068 parser->context->object_type = NULL_TREE;
16069 return identifier;
16074 /* Look up the name. */
16075 decl = cp_parser_lookup_name (parser, identifier,
16076 tag_type,
16077 /*is_template=*/true,
16078 /*is_namespace=*/false,
16079 check_dependency_p,
16080 /*ambiguous_decls=*/NULL,
16081 token->location);
16083 decl = strip_using_decl (decl);
16085 /* If DECL is a template, then the name was a template-name. */
16086 if (TREE_CODE (decl) == TEMPLATE_DECL)
16088 if (TREE_DEPRECATED (decl)
16089 && deprecated_state != DEPRECATED_SUPPRESS)
16090 warn_deprecated_use (decl, NULL_TREE);
16092 else
16094 /* The standard does not explicitly indicate whether a name that
16095 names a set of overloaded declarations, some of which are
16096 templates, is a template-name. However, such a name should
16097 be a template-name; otherwise, there is no way to form a
16098 template-id for the overloaded templates. */
16099 bool found = false;
16101 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16102 !found && iter; ++iter)
16103 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16104 found = true;
16106 if (!found)
16108 /* The name does not name a template. */
16109 cp_parser_error (parser, "expected template-name");
16110 return error_mark_node;
16114 /* If DECL is dependent, and refers to a function, then just return
16115 its name; we will look it up again during template instantiation. */
16116 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16118 tree scope = ovl_scope (decl);
16119 if (TYPE_P (scope) && dependent_type_p (scope))
16120 return identifier;
16123 return decl;
16126 /* Parse a template-argument-list.
16128 template-argument-list:
16129 template-argument ... [opt]
16130 template-argument-list , template-argument ... [opt]
16132 Returns a TREE_VEC containing the arguments. */
16134 static tree
16135 cp_parser_template_argument_list (cp_parser* parser)
16137 tree fixed_args[10];
16138 unsigned n_args = 0;
16139 unsigned alloced = 10;
16140 tree *arg_ary = fixed_args;
16141 tree vec;
16142 bool saved_in_template_argument_list_p;
16143 bool saved_ice_p;
16144 bool saved_non_ice_p;
16146 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16147 parser->in_template_argument_list_p = true;
16148 /* Even if the template-id appears in an integral
16149 constant-expression, the contents of the argument list do
16150 not. */
16151 saved_ice_p = parser->integral_constant_expression_p;
16152 parser->integral_constant_expression_p = false;
16153 saved_non_ice_p = parser->non_integral_constant_expression_p;
16154 parser->non_integral_constant_expression_p = false;
16156 /* Parse the arguments. */
16159 tree argument;
16161 if (n_args)
16162 /* Consume the comma. */
16163 cp_lexer_consume_token (parser->lexer);
16165 /* Parse the template-argument. */
16166 argument = cp_parser_template_argument (parser);
16168 /* If the next token is an ellipsis, we're expanding a template
16169 argument pack. */
16170 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16172 if (argument == error_mark_node)
16174 cp_token *token = cp_lexer_peek_token (parser->lexer);
16175 error_at (token->location,
16176 "expected parameter pack before %<...%>");
16178 /* Consume the `...' token. */
16179 cp_lexer_consume_token (parser->lexer);
16181 /* Make the argument into a TYPE_PACK_EXPANSION or
16182 EXPR_PACK_EXPANSION. */
16183 argument = make_pack_expansion (argument);
16186 if (n_args == alloced)
16188 alloced *= 2;
16190 if (arg_ary == fixed_args)
16192 arg_ary = XNEWVEC (tree, alloced);
16193 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16195 else
16196 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16198 arg_ary[n_args++] = argument;
16200 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16202 vec = make_tree_vec (n_args);
16204 while (n_args--)
16205 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16207 if (arg_ary != fixed_args)
16208 free (arg_ary);
16209 parser->non_integral_constant_expression_p = saved_non_ice_p;
16210 parser->integral_constant_expression_p = saved_ice_p;
16211 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16212 if (CHECKING_P)
16213 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16214 return vec;
16217 /* Parse a template-argument.
16219 template-argument:
16220 assignment-expression
16221 type-id
16222 id-expression
16224 The representation is that of an assignment-expression, type-id, or
16225 id-expression -- except that the qualified id-expression is
16226 evaluated, so that the value returned is either a DECL or an
16227 OVERLOAD.
16229 Although the standard says "assignment-expression", it forbids
16230 throw-expressions or assignments in the template argument.
16231 Therefore, we use "conditional-expression" instead. */
16233 static tree
16234 cp_parser_template_argument (cp_parser* parser)
16236 tree argument;
16237 bool template_p;
16238 bool address_p;
16239 bool maybe_type_id = false;
16240 cp_token *token = NULL, *argument_start_token = NULL;
16241 location_t loc = 0;
16242 cp_id_kind idk;
16244 /* There's really no way to know what we're looking at, so we just
16245 try each alternative in order.
16247 [temp.arg]
16249 In a template-argument, an ambiguity between a type-id and an
16250 expression is resolved to a type-id, regardless of the form of
16251 the corresponding template-parameter.
16253 Therefore, we try a type-id first. */
16254 cp_parser_parse_tentatively (parser);
16255 argument = cp_parser_template_type_arg (parser);
16256 /* If there was no error parsing the type-id but the next token is a
16257 '>>', our behavior depends on which dialect of C++ we're
16258 parsing. In C++98, we probably found a typo for '> >'. But there
16259 are type-id which are also valid expressions. For instance:
16261 struct X { int operator >> (int); };
16262 template <int V> struct Foo {};
16263 Foo<X () >> 5> r;
16265 Here 'X()' is a valid type-id of a function type, but the user just
16266 wanted to write the expression "X() >> 5". Thus, we remember that we
16267 found a valid type-id, but we still try to parse the argument as an
16268 expression to see what happens.
16270 In C++0x, the '>>' will be considered two separate '>'
16271 tokens. */
16272 if (!cp_parser_error_occurred (parser)
16273 && cxx_dialect == cxx98
16274 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16276 maybe_type_id = true;
16277 cp_parser_abort_tentative_parse (parser);
16279 else
16281 /* If the next token isn't a `,' or a `>', then this argument wasn't
16282 really finished. This means that the argument is not a valid
16283 type-id. */
16284 if (!cp_parser_next_token_ends_template_argument_p (parser))
16285 cp_parser_error (parser, "expected template-argument");
16286 /* If that worked, we're done. */
16287 if (cp_parser_parse_definitely (parser))
16288 return argument;
16290 /* We're still not sure what the argument will be. */
16291 cp_parser_parse_tentatively (parser);
16292 /* Try a template. */
16293 argument_start_token = cp_lexer_peek_token (parser->lexer);
16294 argument = cp_parser_id_expression (parser,
16295 /*template_keyword_p=*/false,
16296 /*check_dependency_p=*/true,
16297 &template_p,
16298 /*declarator_p=*/false,
16299 /*optional_p=*/false);
16300 /* If the next token isn't a `,' or a `>', then this argument wasn't
16301 really finished. */
16302 if (!cp_parser_next_token_ends_template_argument_p (parser))
16303 cp_parser_error (parser, "expected template-argument");
16304 if (!cp_parser_error_occurred (parser))
16306 /* Figure out what is being referred to. If the id-expression
16307 was for a class template specialization, then we will have a
16308 TYPE_DECL at this point. There is no need to do name lookup
16309 at this point in that case. */
16310 if (TREE_CODE (argument) != TYPE_DECL)
16311 argument = cp_parser_lookup_name (parser, argument,
16312 none_type,
16313 /*is_template=*/template_p,
16314 /*is_namespace=*/false,
16315 /*check_dependency=*/true,
16316 /*ambiguous_decls=*/NULL,
16317 argument_start_token->location);
16318 /* Handle a constrained-type-specifier for a non-type template
16319 parameter. */
16320 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16321 argument = decl;
16322 else if (TREE_CODE (argument) != TEMPLATE_DECL
16323 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16324 cp_parser_error (parser, "expected template-name");
16326 if (cp_parser_parse_definitely (parser))
16328 if (TREE_DEPRECATED (argument))
16329 warn_deprecated_use (argument, NULL_TREE);
16330 return argument;
16332 /* It must be a non-type argument. In C++17 any constant-expression is
16333 allowed. */
16334 if (cxx_dialect > cxx14)
16335 goto general_expr;
16337 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16339 -- an integral constant-expression of integral or enumeration
16340 type; or
16342 -- the name of a non-type template-parameter; or
16344 -- the name of an object or function with external linkage...
16346 -- the address of an object or function with external linkage...
16348 -- a pointer to member... */
16349 /* Look for a non-type template parameter. */
16350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16352 cp_parser_parse_tentatively (parser);
16353 argument = cp_parser_primary_expression (parser,
16354 /*address_p=*/false,
16355 /*cast_p=*/false,
16356 /*template_arg_p=*/true,
16357 &idk);
16358 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16359 || !cp_parser_next_token_ends_template_argument_p (parser))
16360 cp_parser_simulate_error (parser);
16361 if (cp_parser_parse_definitely (parser))
16362 return argument;
16365 /* If the next token is "&", the argument must be the address of an
16366 object or function with external linkage. */
16367 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16368 if (address_p)
16370 loc = cp_lexer_peek_token (parser->lexer)->location;
16371 cp_lexer_consume_token (parser->lexer);
16373 /* See if we might have an id-expression. */
16374 token = cp_lexer_peek_token (parser->lexer);
16375 if (token->type == CPP_NAME
16376 || token->keyword == RID_OPERATOR
16377 || token->type == CPP_SCOPE
16378 || token->type == CPP_TEMPLATE_ID
16379 || token->type == CPP_NESTED_NAME_SPECIFIER)
16381 cp_parser_parse_tentatively (parser);
16382 argument = cp_parser_primary_expression (parser,
16383 address_p,
16384 /*cast_p=*/false,
16385 /*template_arg_p=*/true,
16386 &idk);
16387 if (cp_parser_error_occurred (parser)
16388 || !cp_parser_next_token_ends_template_argument_p (parser))
16389 cp_parser_abort_tentative_parse (parser);
16390 else
16392 tree probe;
16394 if (INDIRECT_REF_P (argument))
16396 /* Strip the dereference temporarily. */
16397 gcc_assert (REFERENCE_REF_P (argument));
16398 argument = TREE_OPERAND (argument, 0);
16401 /* If we're in a template, we represent a qualified-id referring
16402 to a static data member as a SCOPE_REF even if the scope isn't
16403 dependent so that we can check access control later. */
16404 probe = argument;
16405 if (TREE_CODE (probe) == SCOPE_REF)
16406 probe = TREE_OPERAND (probe, 1);
16407 if (VAR_P (probe))
16409 /* A variable without external linkage might still be a
16410 valid constant-expression, so no error is issued here
16411 if the external-linkage check fails. */
16412 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16413 cp_parser_simulate_error (parser);
16415 else if (is_overloaded_fn (argument))
16416 /* All overloaded functions are allowed; if the external
16417 linkage test does not pass, an error will be issued
16418 later. */
16420 else if (address_p
16421 && (TREE_CODE (argument) == OFFSET_REF
16422 || TREE_CODE (argument) == SCOPE_REF))
16423 /* A pointer-to-member. */
16425 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16427 else
16428 cp_parser_simulate_error (parser);
16430 if (cp_parser_parse_definitely (parser))
16432 if (address_p)
16433 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16434 tf_warning_or_error);
16435 else
16436 argument = convert_from_reference (argument);
16437 return argument;
16441 /* If the argument started with "&", there are no other valid
16442 alternatives at this point. */
16443 if (address_p)
16445 cp_parser_error (parser, "invalid non-type template argument");
16446 return error_mark_node;
16449 general_expr:
16450 /* If the argument wasn't successfully parsed as a type-id followed
16451 by '>>', the argument can only be a constant expression now.
16452 Otherwise, we try parsing the constant-expression tentatively,
16453 because the argument could really be a type-id. */
16454 if (maybe_type_id)
16455 cp_parser_parse_tentatively (parser);
16457 if (cxx_dialect <= cxx14)
16458 argument = cp_parser_constant_expression (parser);
16459 else
16461 /* With C++17 generalized non-type template arguments we need to handle
16462 lvalue constant expressions, too. */
16463 argument = cp_parser_assignment_expression (parser);
16464 require_potential_constant_expression (argument);
16467 if (!maybe_type_id)
16468 return argument;
16469 if (!cp_parser_next_token_ends_template_argument_p (parser))
16470 cp_parser_error (parser, "expected template-argument");
16471 if (cp_parser_parse_definitely (parser))
16472 return argument;
16473 /* We did our best to parse the argument as a non type-id, but that
16474 was the only alternative that matched (albeit with a '>' after
16475 it). We can assume it's just a typo from the user, and a
16476 diagnostic will then be issued. */
16477 return cp_parser_template_type_arg (parser);
16480 /* Parse an explicit-instantiation.
16482 explicit-instantiation:
16483 template declaration
16485 Although the standard says `declaration', what it really means is:
16487 explicit-instantiation:
16488 template decl-specifier-seq [opt] declarator [opt] ;
16490 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16491 supposed to be allowed. A defect report has been filed about this
16492 issue.
16494 GNU Extension:
16496 explicit-instantiation:
16497 storage-class-specifier template
16498 decl-specifier-seq [opt] declarator [opt] ;
16499 function-specifier template
16500 decl-specifier-seq [opt] declarator [opt] ; */
16502 static void
16503 cp_parser_explicit_instantiation (cp_parser* parser)
16505 int declares_class_or_enum;
16506 cp_decl_specifier_seq decl_specifiers;
16507 tree extension_specifier = NULL_TREE;
16509 timevar_push (TV_TEMPLATE_INST);
16511 /* Look for an (optional) storage-class-specifier or
16512 function-specifier. */
16513 if (cp_parser_allow_gnu_extensions_p (parser))
16515 extension_specifier
16516 = cp_parser_storage_class_specifier_opt (parser);
16517 if (!extension_specifier)
16518 extension_specifier
16519 = cp_parser_function_specifier_opt (parser,
16520 /*decl_specs=*/NULL);
16523 /* Look for the `template' keyword. */
16524 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16525 /* Let the front end know that we are processing an explicit
16526 instantiation. */
16527 begin_explicit_instantiation ();
16528 /* [temp.explicit] says that we are supposed to ignore access
16529 control while processing explicit instantiation directives. */
16530 push_deferring_access_checks (dk_no_check);
16531 /* Parse a decl-specifier-seq. */
16532 cp_parser_decl_specifier_seq (parser,
16533 CP_PARSER_FLAGS_OPTIONAL,
16534 &decl_specifiers,
16535 &declares_class_or_enum);
16536 /* If there was exactly one decl-specifier, and it declared a class,
16537 and there's no declarator, then we have an explicit type
16538 instantiation. */
16539 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16541 tree type;
16543 type = check_tag_decl (&decl_specifiers,
16544 /*explicit_type_instantiation_p=*/true);
16545 /* Turn access control back on for names used during
16546 template instantiation. */
16547 pop_deferring_access_checks ();
16548 if (type)
16549 do_type_instantiation (type, extension_specifier,
16550 /*complain=*/tf_error);
16552 else
16554 cp_declarator *declarator;
16555 tree decl;
16557 /* Parse the declarator. */
16558 declarator
16559 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16560 /*ctor_dtor_or_conv_p=*/NULL,
16561 /*parenthesized_p=*/NULL,
16562 /*member_p=*/false,
16563 /*friend_p=*/false);
16564 if (declares_class_or_enum & 2)
16565 cp_parser_check_for_definition_in_return_type (declarator,
16566 decl_specifiers.type,
16567 decl_specifiers.locations[ds_type_spec]);
16568 if (declarator != cp_error_declarator)
16570 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16571 permerror (decl_specifiers.locations[ds_inline],
16572 "explicit instantiation shall not use"
16573 " %<inline%> specifier");
16574 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16575 permerror (decl_specifiers.locations[ds_constexpr],
16576 "explicit instantiation shall not use"
16577 " %<constexpr%> specifier");
16579 decl = grokdeclarator (declarator, &decl_specifiers,
16580 NORMAL, 0, &decl_specifiers.attributes);
16581 /* Turn access control back on for names used during
16582 template instantiation. */
16583 pop_deferring_access_checks ();
16584 /* Do the explicit instantiation. */
16585 do_decl_instantiation (decl, extension_specifier);
16587 else
16589 pop_deferring_access_checks ();
16590 /* Skip the body of the explicit instantiation. */
16591 cp_parser_skip_to_end_of_statement (parser);
16594 /* We're done with the instantiation. */
16595 end_explicit_instantiation ();
16597 cp_parser_consume_semicolon_at_end_of_statement (parser);
16599 timevar_pop (TV_TEMPLATE_INST);
16602 /* Parse an explicit-specialization.
16604 explicit-specialization:
16605 template < > declaration
16607 Although the standard says `declaration', what it really means is:
16609 explicit-specialization:
16610 template <> decl-specifier [opt] init-declarator [opt] ;
16611 template <> function-definition
16612 template <> explicit-specialization
16613 template <> template-declaration */
16615 static void
16616 cp_parser_explicit_specialization (cp_parser* parser)
16618 bool need_lang_pop;
16619 cp_token *token = cp_lexer_peek_token (parser->lexer);
16621 /* Look for the `template' keyword. */
16622 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16623 /* Look for the `<'. */
16624 cp_parser_require (parser, CPP_LESS, RT_LESS);
16625 /* Look for the `>'. */
16626 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16627 /* We have processed another parameter list. */
16628 ++parser->num_template_parameter_lists;
16629 /* [temp]
16631 A template ... explicit specialization ... shall not have C
16632 linkage. */
16633 if (current_lang_name == lang_name_c)
16635 error_at (token->location, "template specialization with C linkage");
16636 maybe_show_extern_c_location ();
16637 /* Give it C++ linkage to avoid confusing other parts of the
16638 front end. */
16639 push_lang_context (lang_name_cplusplus);
16640 need_lang_pop = true;
16642 else
16643 need_lang_pop = false;
16644 /* Let the front end know that we are beginning a specialization. */
16645 if (!begin_specialization ())
16647 end_specialization ();
16648 return;
16651 /* If the next keyword is `template', we need to figure out whether
16652 or not we're looking a template-declaration. */
16653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16655 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16656 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16657 cp_parser_template_declaration_after_export (parser,
16658 /*member_p=*/false);
16659 else
16660 cp_parser_explicit_specialization (parser);
16662 else
16663 /* Parse the dependent declaration. */
16664 cp_parser_single_declaration (parser,
16665 /*checks=*/NULL,
16666 /*member_p=*/false,
16667 /*explicit_specialization_p=*/true,
16668 /*friend_p=*/NULL);
16669 /* We're done with the specialization. */
16670 end_specialization ();
16671 /* For the erroneous case of a template with C linkage, we pushed an
16672 implicit C++ linkage scope; exit that scope now. */
16673 if (need_lang_pop)
16674 pop_lang_context ();
16675 /* We're done with this parameter list. */
16676 --parser->num_template_parameter_lists;
16679 /* Parse a type-specifier.
16681 type-specifier:
16682 simple-type-specifier
16683 class-specifier
16684 enum-specifier
16685 elaborated-type-specifier
16686 cv-qualifier
16688 GNU Extension:
16690 type-specifier:
16691 __complex__
16693 Returns a representation of the type-specifier. For a
16694 class-specifier, enum-specifier, or elaborated-type-specifier, a
16695 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16697 The parser flags FLAGS is used to control type-specifier parsing.
16699 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16700 in a decl-specifier-seq.
16702 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16703 class-specifier, enum-specifier, or elaborated-type-specifier, then
16704 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16705 if a type is declared; 2 if it is defined. Otherwise, it is set to
16706 zero.
16708 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16709 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16710 is set to FALSE. */
16712 static tree
16713 cp_parser_type_specifier (cp_parser* parser,
16714 cp_parser_flags flags,
16715 cp_decl_specifier_seq *decl_specs,
16716 bool is_declaration,
16717 int* declares_class_or_enum,
16718 bool* is_cv_qualifier)
16720 tree type_spec = NULL_TREE;
16721 cp_token *token;
16722 enum rid keyword;
16723 cp_decl_spec ds = ds_last;
16725 /* Assume this type-specifier does not declare a new type. */
16726 if (declares_class_or_enum)
16727 *declares_class_or_enum = 0;
16728 /* And that it does not specify a cv-qualifier. */
16729 if (is_cv_qualifier)
16730 *is_cv_qualifier = false;
16731 /* Peek at the next token. */
16732 token = cp_lexer_peek_token (parser->lexer);
16734 /* If we're looking at a keyword, we can use that to guide the
16735 production we choose. */
16736 keyword = token->keyword;
16737 switch (keyword)
16739 case RID_ENUM:
16740 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16741 goto elaborated_type_specifier;
16743 /* Look for the enum-specifier. */
16744 type_spec = cp_parser_enum_specifier (parser);
16745 /* If that worked, we're done. */
16746 if (type_spec)
16748 if (declares_class_or_enum)
16749 *declares_class_or_enum = 2;
16750 if (decl_specs)
16751 cp_parser_set_decl_spec_type (decl_specs,
16752 type_spec,
16753 token,
16754 /*type_definition_p=*/true);
16755 return type_spec;
16757 else
16758 goto elaborated_type_specifier;
16760 /* Any of these indicate either a class-specifier, or an
16761 elaborated-type-specifier. */
16762 case RID_CLASS:
16763 case RID_STRUCT:
16764 case RID_UNION:
16765 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16766 goto elaborated_type_specifier;
16768 /* Parse tentatively so that we can back up if we don't find a
16769 class-specifier. */
16770 cp_parser_parse_tentatively (parser);
16771 /* Look for the class-specifier. */
16772 type_spec = cp_parser_class_specifier (parser);
16773 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16774 /* If that worked, we're done. */
16775 if (cp_parser_parse_definitely (parser))
16777 if (declares_class_or_enum)
16778 *declares_class_or_enum = 2;
16779 if (decl_specs)
16780 cp_parser_set_decl_spec_type (decl_specs,
16781 type_spec,
16782 token,
16783 /*type_definition_p=*/true);
16784 return type_spec;
16787 /* Fall through. */
16788 elaborated_type_specifier:
16789 /* We're declaring (not defining) a class or enum. */
16790 if (declares_class_or_enum)
16791 *declares_class_or_enum = 1;
16793 /* Fall through. */
16794 case RID_TYPENAME:
16795 /* Look for an elaborated-type-specifier. */
16796 type_spec
16797 = (cp_parser_elaborated_type_specifier
16798 (parser,
16799 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16800 is_declaration));
16801 if (decl_specs)
16802 cp_parser_set_decl_spec_type (decl_specs,
16803 type_spec,
16804 token,
16805 /*type_definition_p=*/false);
16806 return type_spec;
16808 case RID_CONST:
16809 ds = ds_const;
16810 if (is_cv_qualifier)
16811 *is_cv_qualifier = true;
16812 break;
16814 case RID_VOLATILE:
16815 ds = ds_volatile;
16816 if (is_cv_qualifier)
16817 *is_cv_qualifier = true;
16818 break;
16820 case RID_RESTRICT:
16821 ds = ds_restrict;
16822 if (is_cv_qualifier)
16823 *is_cv_qualifier = true;
16824 break;
16826 case RID_COMPLEX:
16827 /* The `__complex__' keyword is a GNU extension. */
16828 ds = ds_complex;
16829 break;
16831 default:
16832 break;
16835 /* Handle simple keywords. */
16836 if (ds != ds_last)
16838 if (decl_specs)
16840 set_and_check_decl_spec_loc (decl_specs, ds, token);
16841 decl_specs->any_specifiers_p = true;
16843 return cp_lexer_consume_token (parser->lexer)->u.value;
16846 /* If we do not already have a type-specifier, assume we are looking
16847 at a simple-type-specifier. */
16848 type_spec = cp_parser_simple_type_specifier (parser,
16849 decl_specs,
16850 flags);
16852 /* If we didn't find a type-specifier, and a type-specifier was not
16853 optional in this context, issue an error message. */
16854 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16856 cp_parser_error (parser, "expected type specifier");
16857 return error_mark_node;
16860 return type_spec;
16863 /* Parse a simple-type-specifier.
16865 simple-type-specifier:
16866 :: [opt] nested-name-specifier [opt] type-name
16867 :: [opt] nested-name-specifier template template-id
16868 char
16869 wchar_t
16870 bool
16871 short
16873 long
16874 signed
16875 unsigned
16876 float
16877 double
16878 void
16880 C++11 Extension:
16882 simple-type-specifier:
16883 auto
16884 decltype ( expression )
16885 char16_t
16886 char32_t
16887 __underlying_type ( type-id )
16889 C++17 extension:
16891 nested-name-specifier(opt) template-name
16893 GNU Extension:
16895 simple-type-specifier:
16896 __int128
16897 __typeof__ unary-expression
16898 __typeof__ ( type-id )
16899 __typeof__ ( type-id ) { initializer-list , [opt] }
16901 Concepts Extension:
16903 simple-type-specifier:
16904 constrained-type-specifier
16906 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16907 appropriately updated. */
16909 static tree
16910 cp_parser_simple_type_specifier (cp_parser* parser,
16911 cp_decl_specifier_seq *decl_specs,
16912 cp_parser_flags flags)
16914 tree type = NULL_TREE;
16915 cp_token *token;
16916 int idx;
16918 /* Peek at the next token. */
16919 token = cp_lexer_peek_token (parser->lexer);
16921 /* If we're looking at a keyword, things are easy. */
16922 switch (token->keyword)
16924 case RID_CHAR:
16925 if (decl_specs)
16926 decl_specs->explicit_char_p = true;
16927 type = char_type_node;
16928 break;
16929 case RID_CHAR16:
16930 type = char16_type_node;
16931 break;
16932 case RID_CHAR32:
16933 type = char32_type_node;
16934 break;
16935 case RID_WCHAR:
16936 type = wchar_type_node;
16937 break;
16938 case RID_BOOL:
16939 type = boolean_type_node;
16940 break;
16941 case RID_SHORT:
16942 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16943 type = short_integer_type_node;
16944 break;
16945 case RID_INT:
16946 if (decl_specs)
16947 decl_specs->explicit_int_p = true;
16948 type = integer_type_node;
16949 break;
16950 case RID_INT_N_0:
16951 case RID_INT_N_1:
16952 case RID_INT_N_2:
16953 case RID_INT_N_3:
16954 idx = token->keyword - RID_INT_N_0;
16955 if (! int_n_enabled_p [idx])
16956 break;
16957 if (decl_specs)
16959 decl_specs->explicit_intN_p = true;
16960 decl_specs->int_n_idx = idx;
16962 type = int_n_trees [idx].signed_type;
16963 break;
16964 case RID_LONG:
16965 if (decl_specs)
16966 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16967 type = long_integer_type_node;
16968 break;
16969 case RID_SIGNED:
16970 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16971 type = integer_type_node;
16972 break;
16973 case RID_UNSIGNED:
16974 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16975 type = unsigned_type_node;
16976 break;
16977 case RID_FLOAT:
16978 type = float_type_node;
16979 break;
16980 case RID_DOUBLE:
16981 type = double_type_node;
16982 break;
16983 case RID_VOID:
16984 type = void_type_node;
16985 break;
16987 case RID_AUTO:
16988 maybe_warn_cpp0x (CPP0X_AUTO);
16989 if (parser->auto_is_implicit_function_template_parm_p)
16991 /* The 'auto' might be the placeholder return type for a function decl
16992 with trailing return type. */
16993 bool have_trailing_return_fn_decl = false;
16995 cp_parser_parse_tentatively (parser);
16996 cp_lexer_consume_token (parser->lexer);
16997 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16998 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16999 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17000 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17002 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17004 cp_lexer_consume_token (parser->lexer);
17005 cp_parser_skip_to_closing_parenthesis (parser,
17006 /*recovering*/false,
17007 /*or_comma*/false,
17008 /*consume_paren*/true);
17009 continue;
17012 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17014 have_trailing_return_fn_decl = true;
17015 break;
17018 cp_lexer_consume_token (parser->lexer);
17020 cp_parser_abort_tentative_parse (parser);
17022 if (have_trailing_return_fn_decl)
17024 type = make_auto ();
17025 break;
17028 if (cxx_dialect >= cxx14)
17030 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17031 type = TREE_TYPE (type);
17033 else
17034 type = error_mark_node;
17036 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17038 if (cxx_dialect < cxx14)
17039 error_at (token->location,
17040 "use of %<auto%> in lambda parameter declaration "
17041 "only available with "
17042 "-std=c++14 or -std=gnu++14");
17044 else if (cxx_dialect < cxx14)
17045 error_at (token->location,
17046 "use of %<auto%> in parameter declaration "
17047 "only available with "
17048 "-std=c++14 or -std=gnu++14");
17049 else if (!flag_concepts)
17050 pedwarn (token->location, OPT_Wpedantic,
17051 "ISO C++ forbids use of %<auto%> in parameter "
17052 "declaration");
17054 else
17055 type = make_auto ();
17056 break;
17058 case RID_DECLTYPE:
17059 /* Since DR 743, decltype can either be a simple-type-specifier by
17060 itself or begin a nested-name-specifier. Parsing it will replace
17061 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17062 handling below decide what to do. */
17063 cp_parser_decltype (parser);
17064 cp_lexer_set_token_position (parser->lexer, token);
17065 break;
17067 case RID_TYPEOF:
17068 /* Consume the `typeof' token. */
17069 cp_lexer_consume_token (parser->lexer);
17070 /* Parse the operand to `typeof'. */
17071 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17072 /* If it is not already a TYPE, take its type. */
17073 if (!TYPE_P (type))
17074 type = finish_typeof (type);
17076 if (decl_specs)
17077 cp_parser_set_decl_spec_type (decl_specs, type,
17078 token,
17079 /*type_definition_p=*/false);
17081 return type;
17083 case RID_UNDERLYING_TYPE:
17084 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17085 if (decl_specs)
17086 cp_parser_set_decl_spec_type (decl_specs, type,
17087 token,
17088 /*type_definition_p=*/false);
17090 return type;
17092 case RID_BASES:
17093 case RID_DIRECT_BASES:
17094 type = cp_parser_trait_expr (parser, token->keyword);
17095 if (decl_specs)
17096 cp_parser_set_decl_spec_type (decl_specs, type,
17097 token,
17098 /*type_definition_p=*/false);
17099 return type;
17100 default:
17101 break;
17104 /* If token is an already-parsed decltype not followed by ::,
17105 it's a simple-type-specifier. */
17106 if (token->type == CPP_DECLTYPE
17107 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17109 type = saved_checks_value (token->u.tree_check_value);
17110 if (decl_specs)
17112 cp_parser_set_decl_spec_type (decl_specs, type,
17113 token,
17114 /*type_definition_p=*/false);
17115 /* Remember that we are handling a decltype in order to
17116 implement the resolution of DR 1510 when the argument
17117 isn't instantiation dependent. */
17118 decl_specs->decltype_p = true;
17120 cp_lexer_consume_token (parser->lexer);
17121 return type;
17124 /* If the type-specifier was for a built-in type, we're done. */
17125 if (type)
17127 /* Record the type. */
17128 if (decl_specs
17129 && (token->keyword != RID_SIGNED
17130 && token->keyword != RID_UNSIGNED
17131 && token->keyword != RID_SHORT
17132 && token->keyword != RID_LONG))
17133 cp_parser_set_decl_spec_type (decl_specs,
17134 type,
17135 token,
17136 /*type_definition_p=*/false);
17137 if (decl_specs)
17138 decl_specs->any_specifiers_p = true;
17140 /* Consume the token. */
17141 cp_lexer_consume_token (parser->lexer);
17143 if (type == error_mark_node)
17144 return error_mark_node;
17146 /* There is no valid C++ program where a non-template type is
17147 followed by a "<". That usually indicates that the user thought
17148 that the type was a template. */
17149 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17150 token->location);
17152 return TYPE_NAME (type);
17155 /* The type-specifier must be a user-defined type. */
17156 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17158 bool qualified_p;
17159 bool global_p;
17161 /* Don't gobble tokens or issue error messages if this is an
17162 optional type-specifier. */
17163 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17164 cp_parser_parse_tentatively (parser);
17166 token = cp_lexer_peek_token (parser->lexer);
17168 /* Look for the optional `::' operator. */
17169 global_p
17170 = (cp_parser_global_scope_opt (parser,
17171 /*current_scope_valid_p=*/false)
17172 != NULL_TREE);
17173 /* Look for the nested-name specifier. */
17174 qualified_p
17175 = (cp_parser_nested_name_specifier_opt (parser,
17176 /*typename_keyword_p=*/false,
17177 /*check_dependency_p=*/true,
17178 /*type_p=*/false,
17179 /*is_declaration=*/false)
17180 != NULL_TREE);
17181 /* If we have seen a nested-name-specifier, and the next token
17182 is `template', then we are using the template-id production. */
17183 if (parser->scope
17184 && cp_parser_optional_template_keyword (parser))
17186 /* Look for the template-id. */
17187 type = cp_parser_template_id (parser,
17188 /*template_keyword_p=*/true,
17189 /*check_dependency_p=*/true,
17190 none_type,
17191 /*is_declaration=*/false);
17192 /* If the template-id did not name a type, we are out of
17193 luck. */
17194 if (TREE_CODE (type) != TYPE_DECL)
17196 cp_parser_error (parser, "expected template-id for type");
17197 type = NULL_TREE;
17200 /* Otherwise, look for a type-name. */
17201 else
17202 type = cp_parser_type_name (parser);
17203 /* Keep track of all name-lookups performed in class scopes. */
17204 if (type
17205 && !global_p
17206 && !qualified_p
17207 && TREE_CODE (type) == TYPE_DECL
17208 && identifier_p (DECL_NAME (type)))
17209 maybe_note_name_used_in_class (DECL_NAME (type), type);
17210 /* If it didn't work out, we don't have a TYPE. */
17211 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17212 && !cp_parser_parse_definitely (parser))
17213 type = NULL_TREE;
17214 if (!type && cxx_dialect >= cxx17)
17216 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17217 cp_parser_parse_tentatively (parser);
17219 cp_parser_global_scope_opt (parser,
17220 /*current_scope_valid_p=*/false);
17221 cp_parser_nested_name_specifier_opt (parser,
17222 /*typename_keyword_p=*/false,
17223 /*check_dependency_p=*/true,
17224 /*type_p=*/false,
17225 /*is_declaration=*/false);
17226 tree name = cp_parser_identifier (parser);
17227 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17228 && parser->scope != error_mark_node)
17230 tree tmpl = cp_parser_lookup_name (parser, name,
17231 none_type,
17232 /*is_template=*/false,
17233 /*is_namespace=*/false,
17234 /*check_dependency=*/true,
17235 /*ambiguous_decls=*/NULL,
17236 token->location);
17237 if (tmpl && tmpl != error_mark_node
17238 && (DECL_CLASS_TEMPLATE_P (tmpl)
17239 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17240 type = make_template_placeholder (tmpl);
17241 else
17243 type = error_mark_node;
17244 if (!cp_parser_simulate_error (parser))
17245 cp_parser_name_lookup_error (parser, name, tmpl,
17246 NLE_TYPE, token->location);
17249 else
17250 type = error_mark_node;
17252 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17253 && !cp_parser_parse_definitely (parser))
17254 type = NULL_TREE;
17256 if (type && decl_specs)
17257 cp_parser_set_decl_spec_type (decl_specs, type,
17258 token,
17259 /*type_definition_p=*/false);
17262 /* If we didn't get a type-name, issue an error message. */
17263 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17265 cp_parser_error (parser, "expected type-name");
17266 return error_mark_node;
17269 if (type && type != error_mark_node)
17271 /* See if TYPE is an Objective-C type, and if so, parse and
17272 accept any protocol references following it. Do this before
17273 the cp_parser_check_for_invalid_template_id() call, because
17274 Objective-C types can be followed by '<...>' which would
17275 enclose protocol names rather than template arguments, and so
17276 everything is fine. */
17277 if (c_dialect_objc () && !parser->scope
17278 && (objc_is_id (type) || objc_is_class_name (type)))
17280 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17281 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17283 /* Clobber the "unqualified" type previously entered into
17284 DECL_SPECS with the new, improved protocol-qualified version. */
17285 if (decl_specs)
17286 decl_specs->type = qual_type;
17288 return qual_type;
17291 /* There is no valid C++ program where a non-template type is
17292 followed by a "<". That usually indicates that the user
17293 thought that the type was a template. */
17294 cp_parser_check_for_invalid_template_id (parser, type,
17295 none_type,
17296 token->location);
17299 return type;
17302 /* Parse a type-name.
17304 type-name:
17305 class-name
17306 enum-name
17307 typedef-name
17308 simple-template-id [in c++0x]
17310 enum-name:
17311 identifier
17313 typedef-name:
17314 identifier
17316 Concepts:
17318 type-name:
17319 concept-name
17320 partial-concept-id
17322 concept-name:
17323 identifier
17325 Returns a TYPE_DECL for the type. */
17327 static tree
17328 cp_parser_type_name (cp_parser* parser)
17330 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17333 /* See above. */
17334 static tree
17335 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17337 tree type_decl;
17339 /* We can't know yet whether it is a class-name or not. */
17340 cp_parser_parse_tentatively (parser);
17341 /* Try a class-name. */
17342 type_decl = cp_parser_class_name (parser,
17343 typename_keyword_p,
17344 /*template_keyword_p=*/false,
17345 none_type,
17346 /*check_dependency_p=*/true,
17347 /*class_head_p=*/false,
17348 /*is_declaration=*/false);
17349 /* If it's not a class-name, keep looking. */
17350 if (!cp_parser_parse_definitely (parser))
17352 if (cxx_dialect < cxx11)
17353 /* It must be a typedef-name or an enum-name. */
17354 return cp_parser_nonclass_name (parser);
17356 cp_parser_parse_tentatively (parser);
17357 /* It is either a simple-template-id representing an
17358 instantiation of an alias template... */
17359 type_decl = cp_parser_template_id (parser,
17360 /*template_keyword_p=*/false,
17361 /*check_dependency_p=*/true,
17362 none_type,
17363 /*is_declaration=*/false);
17364 /* Note that this must be an instantiation of an alias template
17365 because [temp.names]/6 says:
17367 A template-id that names an alias template specialization
17368 is a type-name.
17370 Whereas [temp.names]/7 says:
17372 A simple-template-id that names a class template
17373 specialization is a class-name.
17375 With concepts, this could also be a partial-concept-id that
17376 declares a non-type template parameter. */
17377 if (type_decl != NULL_TREE
17378 && TREE_CODE (type_decl) == TYPE_DECL
17379 && TYPE_DECL_ALIAS_P (type_decl))
17380 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17381 else if (is_constrained_parameter (type_decl))
17382 /* Don't do anything. */ ;
17383 else
17384 cp_parser_simulate_error (parser);
17386 if (!cp_parser_parse_definitely (parser))
17387 /* ... Or a typedef-name or an enum-name. */
17388 return cp_parser_nonclass_name (parser);
17391 return type_decl;
17394 /* Check if DECL and ARGS can form a constrained-type-specifier.
17395 If ARGS is non-null, we try to form a concept check of the
17396 form DECL<?, ARGS> where ? is a wildcard that matches any
17397 kind of template argument. If ARGS is NULL, then we try to
17398 form a concept check of the form DECL<?>. */
17400 static tree
17401 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17402 tree decl, tree args)
17404 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17406 /* If we a constrained-type-specifier cannot be deduced. */
17407 if (parser->prevent_constrained_type_specifiers)
17408 return NULL_TREE;
17410 /* A constrained type specifier can only be found in an
17411 overload set or as a reference to a template declaration.
17413 FIXME: This might be masking a bug. It's possible that
17414 that the deduction below is causing template specializations
17415 to be formed with the wildcard as an argument. */
17416 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17417 return NULL_TREE;
17419 /* Try to build a call expression that evaluates the
17420 concept. This can fail if the overload set refers
17421 only to non-templates. */
17422 tree placeholder = build_nt (WILDCARD_DECL);
17423 tree check = build_concept_check (decl, placeholder, args);
17424 if (check == error_mark_node)
17425 return NULL_TREE;
17427 /* Deduce the checked constraint and the prototype parameter.
17429 FIXME: In certain cases, failure to deduce should be a
17430 diagnosable error. */
17431 tree conc;
17432 tree proto;
17433 if (!deduce_constrained_parameter (check, conc, proto))
17434 return NULL_TREE;
17436 /* In template parameter scope, this results in a constrained
17437 parameter. Return a descriptor of that parm. */
17438 if (processing_template_parmlist)
17439 return build_constrained_parameter (conc, proto, args);
17441 /* In a parameter-declaration-clause, constrained-type
17442 specifiers result in invented template parameters. */
17443 if (parser->auto_is_implicit_function_template_parm_p)
17445 tree x = build_constrained_parameter (conc, proto, args);
17446 return synthesize_implicit_template_parm (parser, x);
17448 else
17450 /* Otherwise, we're in a context where the constrained
17451 type name is deduced and the constraint applies
17452 after deduction. */
17453 return make_constrained_auto (conc, args);
17456 return NULL_TREE;
17459 /* If DECL refers to a concept, return a TYPE_DECL representing
17460 the result of using the constrained type specifier in the
17461 current context. DECL refers to a concept if
17463 - it is an overload set containing a function concept taking a single
17464 type argument, or
17466 - it is a variable concept taking a single type argument. */
17468 static tree
17469 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17471 if (flag_concepts
17472 && (TREE_CODE (decl) == OVERLOAD
17473 || BASELINK_P (decl)
17474 || variable_concept_p (decl)))
17475 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17476 else
17477 return NULL_TREE;
17480 /* Check if DECL and ARGS form a partial-concept-id. If so,
17481 assign ID to the resulting constrained placeholder.
17483 Returns true if the partial-concept-id designates a placeholder
17484 and false otherwise. Note that *id is set to NULL_TREE in
17485 this case. */
17487 static tree
17488 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17490 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17493 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17494 or a concept-name.
17496 enum-name:
17497 identifier
17499 typedef-name:
17500 identifier
17502 concept-name:
17503 identifier
17505 Returns a TYPE_DECL for the type. */
17507 static tree
17508 cp_parser_nonclass_name (cp_parser* parser)
17510 tree type_decl;
17511 tree identifier;
17513 cp_token *token = cp_lexer_peek_token (parser->lexer);
17514 identifier = cp_parser_identifier (parser);
17515 if (identifier == error_mark_node)
17516 return error_mark_node;
17518 /* Look up the type-name. */
17519 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17521 type_decl = strip_using_decl (type_decl);
17523 /* If we found an overload set, then it may refer to a concept-name. */
17524 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17525 type_decl = decl;
17527 if (TREE_CODE (type_decl) != TYPE_DECL
17528 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17530 /* See if this is an Objective-C type. */
17531 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17532 tree type = objc_get_protocol_qualified_type (identifier, protos);
17533 if (type)
17534 type_decl = TYPE_NAME (type);
17537 /* Issue an error if we did not find a type-name. */
17538 if (TREE_CODE (type_decl) != TYPE_DECL
17539 /* In Objective-C, we have the complication that class names are
17540 normally type names and start declarations (eg, the
17541 "NSObject" in "NSObject *object;"), but can be used in an
17542 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17543 is an expression. So, a classname followed by a dot is not a
17544 valid type-name. */
17545 || (objc_is_class_name (TREE_TYPE (type_decl))
17546 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17548 if (!cp_parser_simulate_error (parser))
17549 cp_parser_name_lookup_error (parser, identifier, type_decl,
17550 NLE_TYPE, token->location);
17551 return error_mark_node;
17553 /* Remember that the name was used in the definition of the
17554 current class so that we can check later to see if the
17555 meaning would have been different after the class was
17556 entirely defined. */
17557 else if (type_decl != error_mark_node
17558 && !parser->scope)
17559 maybe_note_name_used_in_class (identifier, type_decl);
17561 return type_decl;
17564 /* Parse an elaborated-type-specifier. Note that the grammar given
17565 here incorporates the resolution to DR68.
17567 elaborated-type-specifier:
17568 class-key :: [opt] nested-name-specifier [opt] identifier
17569 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17570 enum-key :: [opt] nested-name-specifier [opt] identifier
17571 typename :: [opt] nested-name-specifier identifier
17572 typename :: [opt] nested-name-specifier template [opt]
17573 template-id
17575 GNU extension:
17577 elaborated-type-specifier:
17578 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17579 class-key attributes :: [opt] nested-name-specifier [opt]
17580 template [opt] template-id
17581 enum attributes :: [opt] nested-name-specifier [opt] identifier
17583 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17584 declared `friend'. If IS_DECLARATION is TRUE, then this
17585 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17586 something is being declared.
17588 Returns the TYPE specified. */
17590 static tree
17591 cp_parser_elaborated_type_specifier (cp_parser* parser,
17592 bool is_friend,
17593 bool is_declaration)
17595 enum tag_types tag_type;
17596 tree identifier;
17597 tree type = NULL_TREE;
17598 tree attributes = NULL_TREE;
17599 tree globalscope;
17600 cp_token *token = NULL;
17602 /* See if we're looking at the `enum' keyword. */
17603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17605 /* Consume the `enum' token. */
17606 cp_lexer_consume_token (parser->lexer);
17607 /* Remember that it's an enumeration type. */
17608 tag_type = enum_type;
17609 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17610 enums) is used here. */
17611 cp_token *token = cp_lexer_peek_token (parser->lexer);
17612 if (cp_parser_is_keyword (token, RID_CLASS)
17613 || cp_parser_is_keyword (token, RID_STRUCT))
17615 gcc_rich_location richloc (token->location);
17616 richloc.add_range (input_location, false);
17617 richloc.add_fixit_remove ();
17618 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17619 "a scoped enum must not use the %qD keyword",
17620 token->u.value);
17621 /* Consume the `struct' or `class' and parse it anyway. */
17622 cp_lexer_consume_token (parser->lexer);
17624 /* Parse the attributes. */
17625 attributes = cp_parser_attributes_opt (parser);
17627 /* Or, it might be `typename'. */
17628 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17629 RID_TYPENAME))
17631 /* Consume the `typename' token. */
17632 cp_lexer_consume_token (parser->lexer);
17633 /* Remember that it's a `typename' type. */
17634 tag_type = typename_type;
17636 /* Otherwise it must be a class-key. */
17637 else
17639 tag_type = cp_parser_class_key (parser);
17640 if (tag_type == none_type)
17641 return error_mark_node;
17642 /* Parse the attributes. */
17643 attributes = cp_parser_attributes_opt (parser);
17646 /* Look for the `::' operator. */
17647 globalscope = cp_parser_global_scope_opt (parser,
17648 /*current_scope_valid_p=*/false);
17649 /* Look for the nested-name-specifier. */
17650 tree nested_name_specifier;
17651 if (tag_type == typename_type && !globalscope)
17653 nested_name_specifier
17654 = cp_parser_nested_name_specifier (parser,
17655 /*typename_keyword_p=*/true,
17656 /*check_dependency_p=*/true,
17657 /*type_p=*/true,
17658 is_declaration);
17659 if (!nested_name_specifier)
17660 return error_mark_node;
17662 else
17663 /* Even though `typename' is not present, the proposed resolution
17664 to Core Issue 180 says that in `class A<T>::B', `B' should be
17665 considered a type-name, even if `A<T>' is dependent. */
17666 nested_name_specifier
17667 = cp_parser_nested_name_specifier_opt (parser,
17668 /*typename_keyword_p=*/true,
17669 /*check_dependency_p=*/true,
17670 /*type_p=*/true,
17671 is_declaration);
17672 /* For everything but enumeration types, consider a template-id.
17673 For an enumeration type, consider only a plain identifier. */
17674 if (tag_type != enum_type)
17676 bool template_p = false;
17677 tree decl;
17679 /* Allow the `template' keyword. */
17680 template_p = cp_parser_optional_template_keyword (parser);
17681 /* If we didn't see `template', we don't know if there's a
17682 template-id or not. */
17683 if (!template_p)
17684 cp_parser_parse_tentatively (parser);
17685 /* Parse the template-id. */
17686 token = cp_lexer_peek_token (parser->lexer);
17687 decl = cp_parser_template_id (parser, template_p,
17688 /*check_dependency_p=*/true,
17689 tag_type,
17690 is_declaration);
17691 /* If we didn't find a template-id, look for an ordinary
17692 identifier. */
17693 if (!template_p && !cp_parser_parse_definitely (parser))
17695 /* We can get here when cp_parser_template_id, called by
17696 cp_parser_class_name with tag_type == none_type, succeeds
17697 and caches a BASELINK. Then, when called again here,
17698 instead of failing and returning an error_mark_node
17699 returns it (see template/typename17.C in C++11).
17700 ??? Could we diagnose this earlier? */
17701 else if (tag_type == typename_type && BASELINK_P (decl))
17703 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17704 type = error_mark_node;
17706 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17707 in effect, then we must assume that, upon instantiation, the
17708 template will correspond to a class. */
17709 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17710 && tag_type == typename_type)
17711 type = make_typename_type (parser->scope, decl,
17712 typename_type,
17713 /*complain=*/tf_error);
17714 /* If the `typename' keyword is in effect and DECL is not a type
17715 decl, then type is non existent. */
17716 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17718 else if (TREE_CODE (decl) == TYPE_DECL)
17720 type = check_elaborated_type_specifier (tag_type, decl,
17721 /*allow_template_p=*/true);
17723 /* If the next token is a semicolon, this must be a specialization,
17724 instantiation, or friend declaration. Check the scope while we
17725 still know whether or not we had a nested-name-specifier. */
17726 if (type != error_mark_node
17727 && !nested_name_specifier && !is_friend
17728 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17729 check_unqualified_spec_or_inst (type, token->location);
17731 else if (decl == error_mark_node)
17732 type = error_mark_node;
17735 if (!type)
17737 token = cp_lexer_peek_token (parser->lexer);
17738 identifier = cp_parser_identifier (parser);
17740 if (identifier == error_mark_node)
17742 parser->scope = NULL_TREE;
17743 return error_mark_node;
17746 /* For a `typename', we needn't call xref_tag. */
17747 if (tag_type == typename_type
17748 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17749 return cp_parser_make_typename_type (parser, identifier,
17750 token->location);
17752 /* Template parameter lists apply only if we are not within a
17753 function parameter list. */
17754 bool template_parm_lists_apply
17755 = parser->num_template_parameter_lists;
17756 if (template_parm_lists_apply)
17757 for (cp_binding_level *s = current_binding_level;
17758 s && s->kind != sk_template_parms;
17759 s = s->level_chain)
17760 if (s->kind == sk_function_parms)
17761 template_parm_lists_apply = false;
17763 /* Look up a qualified name in the usual way. */
17764 if (parser->scope)
17766 tree decl;
17767 tree ambiguous_decls;
17769 decl = cp_parser_lookup_name (parser, identifier,
17770 tag_type,
17771 /*is_template=*/false,
17772 /*is_namespace=*/false,
17773 /*check_dependency=*/true,
17774 &ambiguous_decls,
17775 token->location);
17777 /* If the lookup was ambiguous, an error will already have been
17778 issued. */
17779 if (ambiguous_decls)
17780 return error_mark_node;
17782 /* If we are parsing friend declaration, DECL may be a
17783 TEMPLATE_DECL tree node here. However, we need to check
17784 whether this TEMPLATE_DECL results in valid code. Consider
17785 the following example:
17787 namespace N {
17788 template <class T> class C {};
17790 class X {
17791 template <class T> friend class N::C; // #1, valid code
17793 template <class T> class Y {
17794 friend class N::C; // #2, invalid code
17797 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17798 name lookup of `N::C'. We see that friend declaration must
17799 be template for the code to be valid. Note that
17800 processing_template_decl does not work here since it is
17801 always 1 for the above two cases. */
17803 decl = (cp_parser_maybe_treat_template_as_class
17804 (decl, /*tag_name_p=*/is_friend
17805 && template_parm_lists_apply));
17807 if (TREE_CODE (decl) != TYPE_DECL)
17809 cp_parser_diagnose_invalid_type_name (parser,
17810 identifier,
17811 token->location);
17812 return error_mark_node;
17815 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17817 bool allow_template = (template_parm_lists_apply
17818 || DECL_SELF_REFERENCE_P (decl));
17819 type = check_elaborated_type_specifier (tag_type, decl,
17820 allow_template);
17822 if (type == error_mark_node)
17823 return error_mark_node;
17826 /* Forward declarations of nested types, such as
17828 class C1::C2;
17829 class C1::C2::C3;
17831 are invalid unless all components preceding the final '::'
17832 are complete. If all enclosing types are complete, these
17833 declarations become merely pointless.
17835 Invalid forward declarations of nested types are errors
17836 caught elsewhere in parsing. Those that are pointless arrive
17837 here. */
17839 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17840 && !is_friend && !processing_explicit_instantiation)
17841 warning (0, "declaration %qD does not declare anything", decl);
17843 type = TREE_TYPE (decl);
17845 else
17847 /* An elaborated-type-specifier sometimes introduces a new type and
17848 sometimes names an existing type. Normally, the rule is that it
17849 introduces a new type only if there is not an existing type of
17850 the same name already in scope. For example, given:
17852 struct S {};
17853 void f() { struct S s; }
17855 the `struct S' in the body of `f' is the same `struct S' as in
17856 the global scope; the existing definition is used. However, if
17857 there were no global declaration, this would introduce a new
17858 local class named `S'.
17860 An exception to this rule applies to the following code:
17862 namespace N { struct S; }
17864 Here, the elaborated-type-specifier names a new type
17865 unconditionally; even if there is already an `S' in the
17866 containing scope this declaration names a new type.
17867 This exception only applies if the elaborated-type-specifier
17868 forms the complete declaration:
17870 [class.name]
17872 A declaration consisting solely of `class-key identifier ;' is
17873 either a redeclaration of the name in the current scope or a
17874 forward declaration of the identifier as a class name. It
17875 introduces the name into the current scope.
17877 We are in this situation precisely when the next token is a `;'.
17879 An exception to the exception is that a `friend' declaration does
17880 *not* name a new type; i.e., given:
17882 struct S { friend struct T; };
17884 `T' is not a new type in the scope of `S'.
17886 Also, `new struct S' or `sizeof (struct S)' never results in the
17887 definition of a new type; a new type can only be declared in a
17888 declaration context. */
17890 tag_scope ts;
17891 bool template_p;
17893 if (is_friend)
17894 /* Friends have special name lookup rules. */
17895 ts = ts_within_enclosing_non_class;
17896 else if (is_declaration
17897 && cp_lexer_next_token_is (parser->lexer,
17898 CPP_SEMICOLON))
17899 /* This is a `class-key identifier ;' */
17900 ts = ts_current;
17901 else
17902 ts = ts_global;
17904 template_p =
17905 (template_parm_lists_apply
17906 && (cp_parser_next_token_starts_class_definition_p (parser)
17907 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17908 /* An unqualified name was used to reference this type, so
17909 there were no qualifying templates. */
17910 if (template_parm_lists_apply
17911 && !cp_parser_check_template_parameters (parser,
17912 /*num_templates=*/0,
17913 token->location,
17914 /*declarator=*/NULL))
17915 return error_mark_node;
17916 type = xref_tag (tag_type, identifier, ts, template_p);
17920 if (type == error_mark_node)
17921 return error_mark_node;
17923 /* Allow attributes on forward declarations of classes. */
17924 if (attributes)
17926 if (TREE_CODE (type) == TYPENAME_TYPE)
17927 warning (OPT_Wattributes,
17928 "attributes ignored on uninstantiated type");
17929 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17930 && ! processing_explicit_instantiation)
17931 warning (OPT_Wattributes,
17932 "attributes ignored on template instantiation");
17933 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17934 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17935 else
17936 warning (OPT_Wattributes,
17937 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17940 if (tag_type != enum_type)
17942 /* Indicate whether this class was declared as a `class' or as a
17943 `struct'. */
17944 if (CLASS_TYPE_P (type))
17945 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17946 cp_parser_check_class_key (tag_type, type);
17949 /* A "<" cannot follow an elaborated type specifier. If that
17950 happens, the user was probably trying to form a template-id. */
17951 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17952 token->location);
17954 return type;
17957 /* Parse an enum-specifier.
17959 enum-specifier:
17960 enum-head { enumerator-list [opt] }
17961 enum-head { enumerator-list , } [C++0x]
17963 enum-head:
17964 enum-key identifier [opt] enum-base [opt]
17965 enum-key nested-name-specifier identifier enum-base [opt]
17967 enum-key:
17968 enum
17969 enum class [C++0x]
17970 enum struct [C++0x]
17972 enum-base: [C++0x]
17973 : type-specifier-seq
17975 opaque-enum-specifier:
17976 enum-key identifier enum-base [opt] ;
17978 GNU Extensions:
17979 enum-key attributes[opt] identifier [opt] enum-base [opt]
17980 { enumerator-list [opt] }attributes[opt]
17981 enum-key attributes[opt] identifier [opt] enum-base [opt]
17982 { enumerator-list, }attributes[opt] [C++0x]
17984 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17985 if the token stream isn't an enum-specifier after all. */
17987 static tree
17988 cp_parser_enum_specifier (cp_parser* parser)
17990 tree identifier;
17991 tree type = NULL_TREE;
17992 tree prev_scope;
17993 tree nested_name_specifier = NULL_TREE;
17994 tree attributes;
17995 bool scoped_enum_p = false;
17996 bool has_underlying_type = false;
17997 bool nested_being_defined = false;
17998 bool new_value_list = false;
17999 bool is_new_type = false;
18000 bool is_unnamed = false;
18001 tree underlying_type = NULL_TREE;
18002 cp_token *type_start_token = NULL;
18003 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18005 parser->colon_corrects_to_scope_p = false;
18007 /* Parse tentatively so that we can back up if we don't find a
18008 enum-specifier. */
18009 cp_parser_parse_tentatively (parser);
18011 /* Caller guarantees that the current token is 'enum', an identifier
18012 possibly follows, and the token after that is an opening brace.
18013 If we don't have an identifier, fabricate an anonymous name for
18014 the enumeration being defined. */
18015 cp_lexer_consume_token (parser->lexer);
18017 /* Parse the "class" or "struct", which indicates a scoped
18018 enumeration type in C++0x. */
18019 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18020 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18022 if (cxx_dialect < cxx11)
18023 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18025 /* Consume the `struct' or `class' token. */
18026 cp_lexer_consume_token (parser->lexer);
18028 scoped_enum_p = true;
18031 attributes = cp_parser_attributes_opt (parser);
18033 /* Clear the qualification. */
18034 parser->scope = NULL_TREE;
18035 parser->qualifying_scope = NULL_TREE;
18036 parser->object_scope = NULL_TREE;
18038 /* Figure out in what scope the declaration is being placed. */
18039 prev_scope = current_scope ();
18041 type_start_token = cp_lexer_peek_token (parser->lexer);
18043 push_deferring_access_checks (dk_no_check);
18044 nested_name_specifier
18045 = cp_parser_nested_name_specifier_opt (parser,
18046 /*typename_keyword_p=*/true,
18047 /*check_dependency_p=*/false,
18048 /*type_p=*/false,
18049 /*is_declaration=*/false);
18051 if (nested_name_specifier)
18053 tree name;
18055 identifier = cp_parser_identifier (parser);
18056 name = cp_parser_lookup_name (parser, identifier,
18057 enum_type,
18058 /*is_template=*/false,
18059 /*is_namespace=*/false,
18060 /*check_dependency=*/true,
18061 /*ambiguous_decls=*/NULL,
18062 input_location);
18063 if (name && name != error_mark_node)
18065 type = TREE_TYPE (name);
18066 if (TREE_CODE (type) == TYPENAME_TYPE)
18068 /* Are template enums allowed in ISO? */
18069 if (template_parm_scope_p ())
18070 pedwarn (type_start_token->location, OPT_Wpedantic,
18071 "%qD is an enumeration template", name);
18072 /* ignore a typename reference, for it will be solved by name
18073 in start_enum. */
18074 type = NULL_TREE;
18077 else if (nested_name_specifier == error_mark_node)
18078 /* We already issued an error. */;
18079 else
18081 error_at (type_start_token->location,
18082 "%qD does not name an enumeration in %qT",
18083 identifier, nested_name_specifier);
18084 nested_name_specifier = error_mark_node;
18087 else
18089 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18090 identifier = cp_parser_identifier (parser);
18091 else
18093 identifier = make_anon_name ();
18094 is_unnamed = true;
18095 if (scoped_enum_p)
18096 error_at (type_start_token->location,
18097 "unnamed scoped enum is not allowed");
18100 pop_deferring_access_checks ();
18102 /* Check for the `:' that denotes a specified underlying type in C++0x.
18103 Note that a ':' could also indicate a bitfield width, however. */
18104 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18106 cp_decl_specifier_seq type_specifiers;
18108 /* Consume the `:'. */
18109 cp_lexer_consume_token (parser->lexer);
18111 /* Parse the type-specifier-seq. */
18112 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18113 /*is_trailing_return=*/false,
18114 &type_specifiers);
18116 /* At this point this is surely not elaborated type specifier. */
18117 if (!cp_parser_parse_definitely (parser))
18118 return NULL_TREE;
18120 if (cxx_dialect < cxx11)
18121 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18123 has_underlying_type = true;
18125 /* If that didn't work, stop. */
18126 if (type_specifiers.type != error_mark_node)
18128 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18129 /*initialized=*/0, NULL);
18130 if (underlying_type == error_mark_node
18131 || check_for_bare_parameter_packs (underlying_type))
18132 underlying_type = NULL_TREE;
18136 /* Look for the `{' but don't consume it yet. */
18137 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18139 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18141 cp_parser_error (parser, "expected %<{%>");
18142 if (has_underlying_type)
18144 type = NULL_TREE;
18145 goto out;
18148 /* An opaque-enum-specifier must have a ';' here. */
18149 if ((scoped_enum_p || underlying_type)
18150 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18152 cp_parser_error (parser, "expected %<;%> or %<{%>");
18153 if (has_underlying_type)
18155 type = NULL_TREE;
18156 goto out;
18161 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18162 return NULL_TREE;
18164 if (nested_name_specifier)
18166 if (CLASS_TYPE_P (nested_name_specifier))
18168 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18169 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18170 push_scope (nested_name_specifier);
18172 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18174 push_nested_namespace (nested_name_specifier);
18178 /* Issue an error message if type-definitions are forbidden here. */
18179 if (!cp_parser_check_type_definition (parser))
18180 type = error_mark_node;
18181 else
18182 /* Create the new type. We do this before consuming the opening
18183 brace so the enum will be recorded as being on the line of its
18184 tag (or the 'enum' keyword, if there is no tag). */
18185 type = start_enum (identifier, type, underlying_type,
18186 attributes, scoped_enum_p, &is_new_type);
18188 /* If the next token is not '{' it is an opaque-enum-specifier or an
18189 elaborated-type-specifier. */
18190 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18192 timevar_push (TV_PARSE_ENUM);
18193 if (nested_name_specifier
18194 && nested_name_specifier != error_mark_node)
18196 /* The following catches invalid code such as:
18197 enum class S<int>::E { A, B, C }; */
18198 if (!processing_specialization
18199 && CLASS_TYPE_P (nested_name_specifier)
18200 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18201 error_at (type_start_token->location, "cannot add an enumerator "
18202 "list to a template instantiation");
18204 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18206 error_at (type_start_token->location,
18207 "%<%T::%E%> has not been declared",
18208 TYPE_CONTEXT (nested_name_specifier),
18209 nested_name_specifier);
18210 type = error_mark_node;
18212 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18213 && !CLASS_TYPE_P (nested_name_specifier))
18215 error_at (type_start_token->location, "nested name specifier "
18216 "%qT for enum declaration does not name a class "
18217 "or namespace", nested_name_specifier);
18218 type = error_mark_node;
18220 /* If that scope does not contain the scope in which the
18221 class was originally declared, the program is invalid. */
18222 else if (prev_scope && !is_ancestor (prev_scope,
18223 nested_name_specifier))
18225 if (at_namespace_scope_p ())
18226 error_at (type_start_token->location,
18227 "declaration of %qD in namespace %qD which does not "
18228 "enclose %qD",
18229 type, prev_scope, nested_name_specifier);
18230 else
18231 error_at (type_start_token->location,
18232 "declaration of %qD in %qD which does not "
18233 "enclose %qD",
18234 type, prev_scope, nested_name_specifier);
18235 type = error_mark_node;
18237 /* If that scope is the scope where the declaration is being placed
18238 the program is invalid. */
18239 else if (CLASS_TYPE_P (nested_name_specifier)
18240 && CLASS_TYPE_P (prev_scope)
18241 && same_type_p (nested_name_specifier, prev_scope))
18243 permerror (type_start_token->location,
18244 "extra qualification not allowed");
18245 nested_name_specifier = NULL_TREE;
18249 if (scoped_enum_p)
18250 begin_scope (sk_scoped_enum, type);
18252 /* Consume the opening brace. */
18253 matching_braces braces;
18254 braces.consume_open (parser);
18256 if (type == error_mark_node)
18257 ; /* Nothing to add */
18258 else if (OPAQUE_ENUM_P (type)
18259 || (cxx_dialect > cxx98 && processing_specialization))
18261 new_value_list = true;
18262 SET_OPAQUE_ENUM_P (type, false);
18263 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18265 else
18267 error_at (type_start_token->location,
18268 "multiple definition of %q#T", type);
18269 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18270 "previous definition here");
18271 type = error_mark_node;
18274 if (type == error_mark_node)
18275 cp_parser_skip_to_end_of_block_or_statement (parser);
18276 /* If the next token is not '}', then there are some enumerators. */
18277 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18279 if (is_unnamed && !scoped_enum_p)
18280 pedwarn (type_start_token->location, OPT_Wpedantic,
18281 "ISO C++ forbids empty unnamed enum");
18283 else
18284 cp_parser_enumerator_list (parser, type);
18286 /* Consume the final '}'. */
18287 braces.require_close (parser);
18289 if (scoped_enum_p)
18290 finish_scope ();
18291 timevar_pop (TV_PARSE_ENUM);
18293 else
18295 /* If a ';' follows, then it is an opaque-enum-specifier
18296 and additional restrictions apply. */
18297 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18299 if (is_unnamed)
18300 error_at (type_start_token->location,
18301 "opaque-enum-specifier without name");
18302 else if (nested_name_specifier)
18303 error_at (type_start_token->location,
18304 "opaque-enum-specifier must use a simple identifier");
18308 /* Look for trailing attributes to apply to this enumeration, and
18309 apply them if appropriate. */
18310 if (cp_parser_allow_gnu_extensions_p (parser))
18312 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18313 cplus_decl_attributes (&type,
18314 trailing_attr,
18315 (int) ATTR_FLAG_TYPE_IN_PLACE);
18318 /* Finish up the enumeration. */
18319 if (type != error_mark_node)
18321 if (new_value_list)
18322 finish_enum_value_list (type);
18323 if (is_new_type)
18324 finish_enum (type);
18327 if (nested_name_specifier)
18329 if (CLASS_TYPE_P (nested_name_specifier))
18331 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18332 pop_scope (nested_name_specifier);
18334 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18336 pop_nested_namespace (nested_name_specifier);
18339 out:
18340 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18341 return type;
18344 /* Parse an enumerator-list. The enumerators all have the indicated
18345 TYPE.
18347 enumerator-list:
18348 enumerator-definition
18349 enumerator-list , enumerator-definition */
18351 static void
18352 cp_parser_enumerator_list (cp_parser* parser, tree type)
18354 while (true)
18356 /* Parse an enumerator-definition. */
18357 cp_parser_enumerator_definition (parser, type);
18359 /* If the next token is not a ',', we've reached the end of
18360 the list. */
18361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18362 break;
18363 /* Otherwise, consume the `,' and keep going. */
18364 cp_lexer_consume_token (parser->lexer);
18365 /* If the next token is a `}', there is a trailing comma. */
18366 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18368 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18369 pedwarn (input_location, OPT_Wpedantic,
18370 "comma at end of enumerator list");
18371 break;
18376 /* Parse an enumerator-definition. The enumerator has the indicated
18377 TYPE.
18379 enumerator-definition:
18380 enumerator
18381 enumerator = constant-expression
18383 enumerator:
18384 identifier
18386 GNU Extensions:
18388 enumerator-definition:
18389 enumerator attributes [opt]
18390 enumerator attributes [opt] = constant-expression */
18392 static void
18393 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18395 tree identifier;
18396 tree value;
18397 location_t loc;
18399 /* Save the input location because we are interested in the location
18400 of the identifier and not the location of the explicit value. */
18401 loc = cp_lexer_peek_token (parser->lexer)->location;
18403 /* Look for the identifier. */
18404 identifier = cp_parser_identifier (parser);
18405 if (identifier == error_mark_node)
18406 return;
18408 /* Parse any specified attributes. */
18409 tree attrs = cp_parser_attributes_opt (parser);
18411 /* If the next token is an '=', then there is an explicit value. */
18412 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18414 /* Consume the `=' token. */
18415 cp_lexer_consume_token (parser->lexer);
18416 /* Parse the value. */
18417 value = cp_parser_constant_expression (parser);
18419 else
18420 value = NULL_TREE;
18422 /* If we are processing a template, make sure the initializer of the
18423 enumerator doesn't contain any bare template parameter pack. */
18424 if (check_for_bare_parameter_packs (value))
18425 value = error_mark_node;
18427 /* Create the enumerator. */
18428 build_enumerator (identifier, value, type, attrs, loc);
18431 /* Parse a namespace-name.
18433 namespace-name:
18434 original-namespace-name
18435 namespace-alias
18437 Returns the NAMESPACE_DECL for the namespace. */
18439 static tree
18440 cp_parser_namespace_name (cp_parser* parser)
18442 tree identifier;
18443 tree namespace_decl;
18445 cp_token *token = cp_lexer_peek_token (parser->lexer);
18447 /* Get the name of the namespace. */
18448 identifier = cp_parser_identifier (parser);
18449 if (identifier == error_mark_node)
18450 return error_mark_node;
18452 /* Look up the identifier in the currently active scope. Look only
18453 for namespaces, due to:
18455 [basic.lookup.udir]
18457 When looking up a namespace-name in a using-directive or alias
18458 definition, only namespace names are considered.
18460 And:
18462 [basic.lookup.qual]
18464 During the lookup of a name preceding the :: scope resolution
18465 operator, object, function, and enumerator names are ignored.
18467 (Note that cp_parser_qualifying_entity only calls this
18468 function if the token after the name is the scope resolution
18469 operator.) */
18470 namespace_decl = cp_parser_lookup_name (parser, identifier,
18471 none_type,
18472 /*is_template=*/false,
18473 /*is_namespace=*/true,
18474 /*check_dependency=*/true,
18475 /*ambiguous_decls=*/NULL,
18476 token->location);
18477 /* If it's not a namespace, issue an error. */
18478 if (namespace_decl == error_mark_node
18479 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18481 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18483 error_at (token->location, "%qD is not a namespace-name", identifier);
18484 if (namespace_decl == error_mark_node
18485 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18486 suggest_alternative_in_explicit_scope (token->location, identifier,
18487 parser->scope);
18489 cp_parser_error (parser, "expected namespace-name");
18490 namespace_decl = error_mark_node;
18493 return namespace_decl;
18496 /* Parse a namespace-definition.
18498 namespace-definition:
18499 named-namespace-definition
18500 unnamed-namespace-definition
18502 named-namespace-definition:
18503 original-namespace-definition
18504 extension-namespace-definition
18506 original-namespace-definition:
18507 namespace identifier { namespace-body }
18509 extension-namespace-definition:
18510 namespace original-namespace-name { namespace-body }
18512 unnamed-namespace-definition:
18513 namespace { namespace-body } */
18515 static void
18516 cp_parser_namespace_definition (cp_parser* parser)
18518 tree identifier;
18519 int nested_definition_count = 0;
18521 cp_ensure_no_omp_declare_simd (parser);
18522 cp_ensure_no_oacc_routine (parser);
18524 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18526 if (is_inline)
18528 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18529 cp_lexer_consume_token (parser->lexer);
18532 /* Look for the `namespace' keyword. */
18533 cp_token* token
18534 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18536 /* Parse any specified attributes before the identifier. */
18537 tree attribs = cp_parser_attributes_opt (parser);
18539 for (;;)
18541 identifier = NULL_TREE;
18543 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18545 identifier = cp_parser_identifier (parser);
18547 /* Parse any attributes specified after the identifier. */
18548 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18552 break;
18554 if (!nested_definition_count && cxx_dialect < cxx17)
18555 pedwarn (input_location, OPT_Wpedantic,
18556 "nested namespace definitions only available with "
18557 "-std=c++17 or -std=gnu++17");
18559 /* Nested namespace names can create new namespaces (unlike
18560 other qualified-ids). */
18561 if (int count = identifier ? push_namespace (identifier) : 0)
18562 nested_definition_count += count;
18563 else
18564 cp_parser_error (parser, "nested namespace name required");
18565 cp_lexer_consume_token (parser->lexer);
18568 if (nested_definition_count && !identifier)
18569 cp_parser_error (parser, "namespace name required");
18571 if (nested_definition_count && attribs)
18572 error_at (token->location,
18573 "a nested namespace definition cannot have attributes");
18574 if (nested_definition_count && is_inline)
18575 error_at (token->location,
18576 "a nested namespace definition cannot be inline");
18578 /* Start the namespace. */
18579 nested_definition_count += push_namespace (identifier, is_inline);
18581 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18583 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18585 /* Look for the `{' to validate starting the namespace. */
18586 matching_braces braces;
18587 if (braces.require_open (parser))
18589 /* Parse the body of the namespace. */
18590 cp_parser_namespace_body (parser);
18592 /* Look for the final `}'. */
18593 braces.require_close (parser);
18596 if (has_visibility)
18597 pop_visibility (1);
18599 /* Pop the nested namespace definitions. */
18600 while (nested_definition_count--)
18601 pop_namespace ();
18604 /* Parse a namespace-body.
18606 namespace-body:
18607 declaration-seq [opt] */
18609 static void
18610 cp_parser_namespace_body (cp_parser* parser)
18612 cp_parser_declaration_seq_opt (parser);
18615 /* Parse a namespace-alias-definition.
18617 namespace-alias-definition:
18618 namespace identifier = qualified-namespace-specifier ; */
18620 static void
18621 cp_parser_namespace_alias_definition (cp_parser* parser)
18623 tree identifier;
18624 tree namespace_specifier;
18626 cp_token *token = cp_lexer_peek_token (parser->lexer);
18628 /* Look for the `namespace' keyword. */
18629 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18630 /* Look for the identifier. */
18631 identifier = cp_parser_identifier (parser);
18632 if (identifier == error_mark_node)
18633 return;
18634 /* Look for the `=' token. */
18635 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18636 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18638 error_at (token->location, "%<namespace%> definition is not allowed here");
18639 /* Skip the definition. */
18640 cp_lexer_consume_token (parser->lexer);
18641 if (cp_parser_skip_to_closing_brace (parser))
18642 cp_lexer_consume_token (parser->lexer);
18643 return;
18645 cp_parser_require (parser, CPP_EQ, RT_EQ);
18646 /* Look for the qualified-namespace-specifier. */
18647 namespace_specifier
18648 = cp_parser_qualified_namespace_specifier (parser);
18649 /* Look for the `;' token. */
18650 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18652 /* Register the alias in the symbol table. */
18653 do_namespace_alias (identifier, namespace_specifier);
18656 /* Parse a qualified-namespace-specifier.
18658 qualified-namespace-specifier:
18659 :: [opt] nested-name-specifier [opt] namespace-name
18661 Returns a NAMESPACE_DECL corresponding to the specified
18662 namespace. */
18664 static tree
18665 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18667 /* Look for the optional `::'. */
18668 cp_parser_global_scope_opt (parser,
18669 /*current_scope_valid_p=*/false);
18671 /* Look for the optional nested-name-specifier. */
18672 cp_parser_nested_name_specifier_opt (parser,
18673 /*typename_keyword_p=*/false,
18674 /*check_dependency_p=*/true,
18675 /*type_p=*/false,
18676 /*is_declaration=*/true);
18678 return cp_parser_namespace_name (parser);
18681 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18682 access declaration.
18684 using-declaration:
18685 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18686 using :: unqualified-id ;
18688 access-declaration:
18689 qualified-id ;
18693 static bool
18694 cp_parser_using_declaration (cp_parser* parser,
18695 bool access_declaration_p)
18697 cp_token *token;
18698 bool typename_p = false;
18699 bool global_scope_p;
18700 tree decl;
18701 tree identifier;
18702 tree qscope;
18703 int oldcount = errorcount;
18704 cp_token *diag_token = NULL;
18706 if (access_declaration_p)
18708 diag_token = cp_lexer_peek_token (parser->lexer);
18709 cp_parser_parse_tentatively (parser);
18711 else
18713 /* Look for the `using' keyword. */
18714 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18716 again:
18717 /* Peek at the next token. */
18718 token = cp_lexer_peek_token (parser->lexer);
18719 /* See if it's `typename'. */
18720 if (token->keyword == RID_TYPENAME)
18722 /* Remember that we've seen it. */
18723 typename_p = true;
18724 /* Consume the `typename' token. */
18725 cp_lexer_consume_token (parser->lexer);
18729 /* Look for the optional global scope qualification. */
18730 global_scope_p
18731 = (cp_parser_global_scope_opt (parser,
18732 /*current_scope_valid_p=*/false)
18733 != NULL_TREE);
18735 /* If we saw `typename', or didn't see `::', then there must be a
18736 nested-name-specifier present. */
18737 if (typename_p || !global_scope_p)
18739 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18740 /*check_dependency_p=*/true,
18741 /*type_p=*/false,
18742 /*is_declaration=*/true);
18743 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18745 cp_parser_skip_to_end_of_block_or_statement (parser);
18746 return false;
18749 /* Otherwise, we could be in either of the two productions. In that
18750 case, treat the nested-name-specifier as optional. */
18751 else
18752 qscope = cp_parser_nested_name_specifier_opt (parser,
18753 /*typename_keyword_p=*/false,
18754 /*check_dependency_p=*/true,
18755 /*type_p=*/false,
18756 /*is_declaration=*/true);
18757 if (!qscope)
18758 qscope = global_namespace;
18759 else if (UNSCOPED_ENUM_P (qscope))
18760 qscope = CP_TYPE_CONTEXT (qscope);
18762 if (access_declaration_p && cp_parser_error_occurred (parser))
18763 /* Something has already gone wrong; there's no need to parse
18764 further. Since an error has occurred, the return value of
18765 cp_parser_parse_definitely will be false, as required. */
18766 return cp_parser_parse_definitely (parser);
18768 token = cp_lexer_peek_token (parser->lexer);
18769 /* Parse the unqualified-id. */
18770 identifier = cp_parser_unqualified_id (parser,
18771 /*template_keyword_p=*/false,
18772 /*check_dependency_p=*/true,
18773 /*declarator_p=*/true,
18774 /*optional_p=*/false);
18776 if (access_declaration_p)
18778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18779 cp_parser_simulate_error (parser);
18780 if (!cp_parser_parse_definitely (parser))
18781 return false;
18783 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18785 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18786 if (cxx_dialect < cxx17
18787 && !in_system_header_at (ell->location))
18788 pedwarn (ell->location, 0,
18789 "pack expansion in using-declaration only available "
18790 "with -std=c++17 or -std=gnu++17");
18791 qscope = make_pack_expansion (qscope);
18794 /* The function we call to handle a using-declaration is different
18795 depending on what scope we are in. */
18796 if (qscope == error_mark_node || identifier == error_mark_node)
18798 else if (!identifier_p (identifier)
18799 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18800 /* [namespace.udecl]
18802 A using declaration shall not name a template-id. */
18803 error_at (token->location,
18804 "a template-id may not appear in a using-declaration");
18805 else
18807 if (at_class_scope_p ())
18809 /* Create the USING_DECL. */
18810 decl = do_class_using_decl (qscope, identifier);
18812 if (decl && typename_p)
18813 USING_DECL_TYPENAME_P (decl) = 1;
18815 if (check_for_bare_parameter_packs (decl))
18817 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18818 return false;
18820 else
18821 /* Add it to the list of members in this class. */
18822 finish_member_declaration (decl);
18824 else
18826 decl = cp_parser_lookup_name_simple (parser,
18827 identifier,
18828 token->location);
18829 if (decl == error_mark_node)
18830 cp_parser_name_lookup_error (parser, identifier,
18831 decl, NLE_NULL,
18832 token->location);
18833 else if (check_for_bare_parameter_packs (decl))
18835 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18836 return false;
18838 else if (!at_namespace_scope_p ())
18839 finish_local_using_decl (decl, qscope, identifier);
18840 else
18841 finish_namespace_using_decl (decl, qscope, identifier);
18845 if (!access_declaration_p
18846 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18848 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18849 if (cxx_dialect < cxx17)
18850 pedwarn (comma->location, 0,
18851 "comma-separated list in using-declaration only available "
18852 "with -std=c++17 or -std=gnu++17");
18853 goto again;
18856 /* Look for the final `;'. */
18857 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18859 if (access_declaration_p && errorcount == oldcount)
18860 warning_at (diag_token->location, OPT_Wdeprecated,
18861 "access declarations are deprecated "
18862 "in favour of using-declarations; "
18863 "suggestion: add the %<using%> keyword");
18865 return true;
18868 /* Parse an alias-declaration.
18870 alias-declaration:
18871 using identifier attribute-specifier-seq [opt] = type-id */
18873 static tree
18874 cp_parser_alias_declaration (cp_parser* parser)
18876 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18877 location_t id_location;
18878 cp_declarator *declarator;
18879 cp_decl_specifier_seq decl_specs;
18880 bool member_p;
18881 const char *saved_message = NULL;
18883 /* Look for the `using' keyword. */
18884 cp_token *using_token
18885 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18886 if (using_token == NULL)
18887 return error_mark_node;
18889 id_location = cp_lexer_peek_token (parser->lexer)->location;
18890 id = cp_parser_identifier (parser);
18891 if (id == error_mark_node)
18892 return error_mark_node;
18894 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18895 attributes = cp_parser_attributes_opt (parser);
18896 if (attributes == error_mark_node)
18897 return error_mark_node;
18899 cp_parser_require (parser, CPP_EQ, RT_EQ);
18901 if (cp_parser_error_occurred (parser))
18902 return error_mark_node;
18904 cp_parser_commit_to_tentative_parse (parser);
18906 /* Now we are going to parse the type-id of the declaration. */
18909 [dcl.type]/3 says:
18911 "A type-specifier-seq shall not define a class or enumeration
18912 unless it appears in the type-id of an alias-declaration (7.1.3) that
18913 is not the declaration of a template-declaration."
18915 In other words, if we currently are in an alias template, the
18916 type-id should not define a type.
18918 So let's set parser->type_definition_forbidden_message in that
18919 case; cp_parser_check_type_definition (called by
18920 cp_parser_class_specifier) will then emit an error if a type is
18921 defined in the type-id. */
18922 if (parser->num_template_parameter_lists)
18924 saved_message = parser->type_definition_forbidden_message;
18925 parser->type_definition_forbidden_message =
18926 G_("types may not be defined in alias template declarations");
18929 type = cp_parser_type_id (parser);
18931 /* Restore the error message if need be. */
18932 if (parser->num_template_parameter_lists)
18933 parser->type_definition_forbidden_message = saved_message;
18935 if (type == error_mark_node
18936 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18938 cp_parser_skip_to_end_of_block_or_statement (parser);
18939 return error_mark_node;
18942 /* A typedef-name can also be introduced by an alias-declaration. The
18943 identifier following the using keyword becomes a typedef-name. It has
18944 the same semantics as if it were introduced by the typedef
18945 specifier. In particular, it does not define a new type and it shall
18946 not appear in the type-id. */
18948 clear_decl_specs (&decl_specs);
18949 decl_specs.type = type;
18950 if (attributes != NULL_TREE)
18952 decl_specs.attributes = attributes;
18953 set_and_check_decl_spec_loc (&decl_specs,
18954 ds_attribute,
18955 attrs_token);
18957 set_and_check_decl_spec_loc (&decl_specs,
18958 ds_typedef,
18959 using_token);
18960 set_and_check_decl_spec_loc (&decl_specs,
18961 ds_alias,
18962 using_token);
18964 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18965 declarator->id_loc = id_location;
18967 member_p = at_class_scope_p ();
18968 if (member_p)
18969 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18970 NULL_TREE, attributes);
18971 else
18972 decl = start_decl (declarator, &decl_specs, 0,
18973 attributes, NULL_TREE, &pushed_scope);
18974 if (decl == error_mark_node)
18975 return decl;
18977 // Attach constraints to the alias declaration.
18978 if (flag_concepts && current_template_parms)
18980 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18981 tree constr = build_constraints (reqs, NULL_TREE);
18982 set_constraints (decl, constr);
18985 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18987 if (pushed_scope)
18988 pop_scope (pushed_scope);
18990 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18991 added into the symbol table; otherwise, return the TYPE_DECL. */
18992 if (DECL_LANG_SPECIFIC (decl)
18993 && DECL_TEMPLATE_INFO (decl)
18994 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18996 decl = DECL_TI_TEMPLATE (decl);
18997 if (member_p)
18998 check_member_template (decl);
19001 return decl;
19004 /* Parse a using-directive.
19006 using-directive:
19007 using namespace :: [opt] nested-name-specifier [opt]
19008 namespace-name ; */
19010 static void
19011 cp_parser_using_directive (cp_parser* parser)
19013 tree namespace_decl;
19014 tree attribs;
19016 /* Look for the `using' keyword. */
19017 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19018 /* And the `namespace' keyword. */
19019 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19020 /* Look for the optional `::' operator. */
19021 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19022 /* And the optional nested-name-specifier. */
19023 cp_parser_nested_name_specifier_opt (parser,
19024 /*typename_keyword_p=*/false,
19025 /*check_dependency_p=*/true,
19026 /*type_p=*/false,
19027 /*is_declaration=*/true);
19028 /* Get the namespace being used. */
19029 namespace_decl = cp_parser_namespace_name (parser);
19030 /* And any specified attributes. */
19031 attribs = cp_parser_attributes_opt (parser);
19033 /* Update the symbol table. */
19034 if (namespace_bindings_p ())
19035 finish_namespace_using_directive (namespace_decl, attribs);
19036 else
19037 finish_local_using_directive (namespace_decl, attribs);
19039 /* Look for the final `;'. */
19040 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19043 /* Parse an asm-definition.
19045 asm-definition:
19046 asm ( string-literal ) ;
19048 GNU Extension:
19050 asm-definition:
19051 asm volatile [opt] ( string-literal ) ;
19052 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19053 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19054 : asm-operand-list [opt] ) ;
19055 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19056 : asm-operand-list [opt]
19057 : asm-clobber-list [opt] ) ;
19058 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19059 : asm-clobber-list [opt]
19060 : asm-goto-list ) ; */
19062 static void
19063 cp_parser_asm_definition (cp_parser* parser)
19065 tree string;
19066 tree outputs = NULL_TREE;
19067 tree inputs = NULL_TREE;
19068 tree clobbers = NULL_TREE;
19069 tree labels = NULL_TREE;
19070 tree asm_stmt;
19071 bool volatile_p = false;
19072 bool extended_p = false;
19073 bool invalid_inputs_p = false;
19074 bool invalid_outputs_p = false;
19075 bool goto_p = false;
19076 required_token missing = RT_NONE;
19078 /* Look for the `asm' keyword. */
19079 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19081 if (parser->in_function_body
19082 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19084 error ("%<asm%> in %<constexpr%> function");
19085 cp_function_chain->invalid_constexpr = true;
19088 /* See if the next token is `volatile'. */
19089 if (cp_parser_allow_gnu_extensions_p (parser)
19090 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19092 /* Remember that we saw the `volatile' keyword. */
19093 volatile_p = true;
19094 /* Consume the token. */
19095 cp_lexer_consume_token (parser->lexer);
19097 if (cp_parser_allow_gnu_extensions_p (parser)
19098 && parser->in_function_body
19099 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19101 /* Remember that we saw the `goto' keyword. */
19102 goto_p = true;
19103 /* Consume the token. */
19104 cp_lexer_consume_token (parser->lexer);
19106 /* Look for the opening `('. */
19107 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19108 return;
19109 /* Look for the string. */
19110 string = cp_parser_string_literal (parser, false, false);
19111 if (string == error_mark_node)
19113 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19114 /*consume_paren=*/true);
19115 return;
19118 /* If we're allowing GNU extensions, check for the extended assembly
19119 syntax. Unfortunately, the `:' tokens need not be separated by
19120 a space in C, and so, for compatibility, we tolerate that here
19121 too. Doing that means that we have to treat the `::' operator as
19122 two `:' tokens. */
19123 if (cp_parser_allow_gnu_extensions_p (parser)
19124 && parser->in_function_body
19125 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19126 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19128 bool inputs_p = false;
19129 bool clobbers_p = false;
19130 bool labels_p = false;
19132 /* The extended syntax was used. */
19133 extended_p = true;
19135 /* Look for outputs. */
19136 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19138 /* Consume the `:'. */
19139 cp_lexer_consume_token (parser->lexer);
19140 /* Parse the output-operands. */
19141 if (cp_lexer_next_token_is_not (parser->lexer,
19142 CPP_COLON)
19143 && cp_lexer_next_token_is_not (parser->lexer,
19144 CPP_SCOPE)
19145 && cp_lexer_next_token_is_not (parser->lexer,
19146 CPP_CLOSE_PAREN)
19147 && !goto_p)
19149 outputs = cp_parser_asm_operand_list (parser);
19150 if (outputs == error_mark_node)
19151 invalid_outputs_p = true;
19154 /* If the next token is `::', there are no outputs, and the
19155 next token is the beginning of the inputs. */
19156 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19157 /* The inputs are coming next. */
19158 inputs_p = true;
19160 /* Look for inputs. */
19161 if (inputs_p
19162 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19164 /* Consume the `:' or `::'. */
19165 cp_lexer_consume_token (parser->lexer);
19166 /* Parse the output-operands. */
19167 if (cp_lexer_next_token_is_not (parser->lexer,
19168 CPP_COLON)
19169 && cp_lexer_next_token_is_not (parser->lexer,
19170 CPP_SCOPE)
19171 && cp_lexer_next_token_is_not (parser->lexer,
19172 CPP_CLOSE_PAREN))
19174 inputs = cp_parser_asm_operand_list (parser);
19175 if (inputs == error_mark_node)
19176 invalid_inputs_p = true;
19179 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19180 /* The clobbers are coming next. */
19181 clobbers_p = true;
19183 /* Look for clobbers. */
19184 if (clobbers_p
19185 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19187 clobbers_p = true;
19188 /* Consume the `:' or `::'. */
19189 cp_lexer_consume_token (parser->lexer);
19190 /* Parse the clobbers. */
19191 if (cp_lexer_next_token_is_not (parser->lexer,
19192 CPP_COLON)
19193 && cp_lexer_next_token_is_not (parser->lexer,
19194 CPP_CLOSE_PAREN))
19195 clobbers = cp_parser_asm_clobber_list (parser);
19197 else if (goto_p
19198 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19199 /* The labels are coming next. */
19200 labels_p = true;
19202 /* Look for labels. */
19203 if (labels_p
19204 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19206 labels_p = true;
19207 /* Consume the `:' or `::'. */
19208 cp_lexer_consume_token (parser->lexer);
19209 /* Parse the labels. */
19210 labels = cp_parser_asm_label_list (parser);
19213 if (goto_p && !labels_p)
19214 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19216 else if (goto_p)
19217 missing = RT_COLON_SCOPE;
19219 /* Look for the closing `)'. */
19220 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19221 missing ? missing : RT_CLOSE_PAREN))
19222 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19223 /*consume_paren=*/true);
19224 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19226 if (!invalid_inputs_p && !invalid_outputs_p)
19228 /* Create the ASM_EXPR. */
19229 if (parser->in_function_body)
19231 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19232 inputs, clobbers, labels);
19233 /* If the extended syntax was not used, mark the ASM_EXPR. */
19234 if (!extended_p)
19236 tree temp = asm_stmt;
19237 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19238 temp = TREE_OPERAND (temp, 0);
19240 ASM_INPUT_P (temp) = 1;
19243 else
19244 symtab->finalize_toplevel_asm (string);
19248 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19249 type that comes from the decl-specifier-seq. */
19251 static tree
19252 strip_declarator_types (tree type, cp_declarator *declarator)
19254 for (cp_declarator *d = declarator; d;)
19255 switch (d->kind)
19257 case cdk_id:
19258 case cdk_decomp:
19259 case cdk_error:
19260 d = NULL;
19261 break;
19263 default:
19264 if (TYPE_PTRMEMFUNC_P (type))
19265 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19266 type = TREE_TYPE (type);
19267 d = d->declarator;
19268 break;
19271 return type;
19274 /* Declarators [gram.dcl.decl] */
19276 /* Parse an init-declarator.
19278 init-declarator:
19279 declarator initializer [opt]
19281 GNU Extension:
19283 init-declarator:
19284 declarator asm-specification [opt] attributes [opt] initializer [opt]
19286 function-definition:
19287 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19288 function-body
19289 decl-specifier-seq [opt] declarator function-try-block
19291 GNU Extension:
19293 function-definition:
19294 __extension__ function-definition
19296 TM Extension:
19298 function-definition:
19299 decl-specifier-seq [opt] declarator function-transaction-block
19301 The DECL_SPECIFIERS apply to this declarator. Returns a
19302 representation of the entity declared. If MEMBER_P is TRUE, then
19303 this declarator appears in a class scope. The new DECL created by
19304 this declarator is returned.
19306 The CHECKS are access checks that should be performed once we know
19307 what entity is being declared (and, therefore, what classes have
19308 befriended it).
19310 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19311 for a function-definition here as well. If the declarator is a
19312 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19313 be TRUE upon return. By that point, the function-definition will
19314 have been completely parsed.
19316 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19317 is FALSE.
19319 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19320 parsed declaration if it is an uninitialized single declarator not followed
19321 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19322 if present, will not be consumed. If returned, this declarator will be
19323 created with SD_INITIALIZED but will not call cp_finish_decl.
19325 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19326 and there is an initializer, the pointed location_t is set to the
19327 location of the '=' or `(', or '{' in C++11 token introducing the
19328 initializer. */
19330 static tree
19331 cp_parser_init_declarator (cp_parser* parser,
19332 cp_decl_specifier_seq *decl_specifiers,
19333 vec<deferred_access_check, va_gc> *checks,
19334 bool function_definition_allowed_p,
19335 bool member_p,
19336 int declares_class_or_enum,
19337 bool* function_definition_p,
19338 tree* maybe_range_for_decl,
19339 location_t* init_loc,
19340 tree* auto_result)
19342 cp_token *token = NULL, *asm_spec_start_token = NULL,
19343 *attributes_start_token = NULL;
19344 cp_declarator *declarator;
19345 tree prefix_attributes;
19346 tree attributes = NULL;
19347 tree asm_specification;
19348 tree initializer;
19349 tree decl = NULL_TREE;
19350 tree scope;
19351 int is_initialized;
19352 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19353 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19354 "(...)". */
19355 enum cpp_ttype initialization_kind;
19356 bool is_direct_init = false;
19357 bool is_non_constant_init;
19358 int ctor_dtor_or_conv_p;
19359 bool friend_p = cp_parser_friend_p (decl_specifiers);
19360 tree pushed_scope = NULL_TREE;
19361 bool range_for_decl_p = false;
19362 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19363 location_t tmp_init_loc = UNKNOWN_LOCATION;
19365 /* Gather the attributes that were provided with the
19366 decl-specifiers. */
19367 prefix_attributes = decl_specifiers->attributes;
19369 /* Assume that this is not the declarator for a function
19370 definition. */
19371 if (function_definition_p)
19372 *function_definition_p = false;
19374 /* Default arguments are only permitted for function parameters. */
19375 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19376 parser->default_arg_ok_p = false;
19378 /* Defer access checks while parsing the declarator; we cannot know
19379 what names are accessible until we know what is being
19380 declared. */
19381 resume_deferring_access_checks ();
19383 token = cp_lexer_peek_token (parser->lexer);
19385 /* Parse the declarator. */
19386 declarator
19387 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19388 &ctor_dtor_or_conv_p,
19389 /*parenthesized_p=*/NULL,
19390 member_p, friend_p);
19391 /* Gather up the deferred checks. */
19392 stop_deferring_access_checks ();
19394 parser->default_arg_ok_p = saved_default_arg_ok_p;
19396 /* If the DECLARATOR was erroneous, there's no need to go
19397 further. */
19398 if (declarator == cp_error_declarator)
19399 return error_mark_node;
19401 /* Check that the number of template-parameter-lists is OK. */
19402 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19403 token->location))
19404 return error_mark_node;
19406 if (declares_class_or_enum & 2)
19407 cp_parser_check_for_definition_in_return_type (declarator,
19408 decl_specifiers->type,
19409 decl_specifiers->locations[ds_type_spec]);
19411 /* Figure out what scope the entity declared by the DECLARATOR is
19412 located in. `grokdeclarator' sometimes changes the scope, so
19413 we compute it now. */
19414 scope = get_scope_of_declarator (declarator);
19416 /* Perform any lookups in the declared type which were thought to be
19417 dependent, but are not in the scope of the declarator. */
19418 decl_specifiers->type
19419 = maybe_update_decl_type (decl_specifiers->type, scope);
19421 /* If we're allowing GNU extensions, look for an
19422 asm-specification. */
19423 if (cp_parser_allow_gnu_extensions_p (parser))
19425 /* Look for an asm-specification. */
19426 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19427 asm_specification = cp_parser_asm_specification_opt (parser);
19429 else
19430 asm_specification = NULL_TREE;
19432 /* Look for attributes. */
19433 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19434 attributes = cp_parser_attributes_opt (parser);
19436 /* Peek at the next token. */
19437 token = cp_lexer_peek_token (parser->lexer);
19439 bool bogus_implicit_tmpl = false;
19441 if (function_declarator_p (declarator))
19443 /* Handle C++17 deduction guides. */
19444 if (!decl_specifiers->type
19445 && ctor_dtor_or_conv_p <= 0
19446 && cxx_dialect >= cxx17)
19448 cp_declarator *id = get_id_declarator (declarator);
19449 tree name = id->u.id.unqualified_name;
19450 parser->scope = id->u.id.qualifying_scope;
19451 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19452 if (tmpl
19453 && (DECL_CLASS_TEMPLATE_P (tmpl)
19454 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19456 id->u.id.unqualified_name = dguide_name (tmpl);
19457 id->u.id.sfk = sfk_deduction_guide;
19458 ctor_dtor_or_conv_p = 1;
19462 /* Check to see if the token indicates the start of a
19463 function-definition. */
19464 if (cp_parser_token_starts_function_definition_p (token))
19466 if (!function_definition_allowed_p)
19468 /* If a function-definition should not appear here, issue an
19469 error message. */
19470 cp_parser_error (parser,
19471 "a function-definition is not allowed here");
19472 return error_mark_node;
19475 location_t func_brace_location
19476 = cp_lexer_peek_token (parser->lexer)->location;
19478 /* Neither attributes nor an asm-specification are allowed
19479 on a function-definition. */
19480 if (asm_specification)
19481 error_at (asm_spec_start_token->location,
19482 "an asm-specification is not allowed "
19483 "on a function-definition");
19484 if (attributes)
19485 error_at (attributes_start_token->location,
19486 "attributes are not allowed "
19487 "on a function-definition");
19488 /* This is a function-definition. */
19489 *function_definition_p = true;
19491 /* Parse the function definition. */
19492 if (member_p)
19493 decl = cp_parser_save_member_function_body (parser,
19494 decl_specifiers,
19495 declarator,
19496 prefix_attributes);
19497 else
19498 decl =
19499 (cp_parser_function_definition_from_specifiers_and_declarator
19500 (parser, decl_specifiers, prefix_attributes, declarator));
19502 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19504 /* This is where the prologue starts... */
19505 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19506 = func_brace_location;
19509 return decl;
19512 else if (parser->fully_implicit_function_template_p)
19514 /* A non-template declaration involving a function parameter list
19515 containing an implicit template parameter will be made into a
19516 template. If the resulting declaration is not going to be an
19517 actual function then finish the template scope here to prevent it.
19518 An error message will be issued once we have a decl to talk about.
19520 FIXME probably we should do type deduction rather than create an
19521 implicit template, but the standard currently doesn't allow it. */
19522 bogus_implicit_tmpl = true;
19523 finish_fully_implicit_template (parser, NULL_TREE);
19526 /* [dcl.dcl]
19528 Only in function declarations for constructors, destructors, type
19529 conversions, and deduction guides can the decl-specifier-seq be omitted.
19531 We explicitly postpone this check past the point where we handle
19532 function-definitions because we tolerate function-definitions
19533 that are missing their return types in some modes. */
19534 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19536 cp_parser_error (parser,
19537 "expected constructor, destructor, or type conversion");
19538 return error_mark_node;
19541 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19542 if (token->type == CPP_EQ
19543 || token->type == CPP_OPEN_PAREN
19544 || token->type == CPP_OPEN_BRACE)
19546 is_initialized = SD_INITIALIZED;
19547 initialization_kind = token->type;
19548 if (maybe_range_for_decl)
19549 *maybe_range_for_decl = error_mark_node;
19550 tmp_init_loc = token->location;
19551 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19552 *init_loc = tmp_init_loc;
19554 if (token->type == CPP_EQ
19555 && function_declarator_p (declarator))
19557 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19558 if (t2->keyword == RID_DEFAULT)
19559 is_initialized = SD_DEFAULTED;
19560 else if (t2->keyword == RID_DELETE)
19561 is_initialized = SD_DELETED;
19564 else
19566 /* If the init-declarator isn't initialized and isn't followed by a
19567 `,' or `;', it's not a valid init-declarator. */
19568 if (token->type != CPP_COMMA
19569 && token->type != CPP_SEMICOLON)
19571 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19572 range_for_decl_p = true;
19573 else
19575 if (!maybe_range_for_decl)
19576 cp_parser_error (parser, "expected initializer");
19577 return error_mark_node;
19580 is_initialized = SD_UNINITIALIZED;
19581 initialization_kind = CPP_EOF;
19584 /* Because start_decl has side-effects, we should only call it if we
19585 know we're going ahead. By this point, we know that we cannot
19586 possibly be looking at any other construct. */
19587 cp_parser_commit_to_tentative_parse (parser);
19589 /* Enter the newly declared entry in the symbol table. If we're
19590 processing a declaration in a class-specifier, we wait until
19591 after processing the initializer. */
19592 if (!member_p)
19594 if (parser->in_unbraced_linkage_specification_p)
19595 decl_specifiers->storage_class = sc_extern;
19596 decl = start_decl (declarator, decl_specifiers,
19597 range_for_decl_p? SD_INITIALIZED : is_initialized,
19598 attributes, prefix_attributes, &pushed_scope);
19599 cp_finalize_omp_declare_simd (parser, decl);
19600 cp_finalize_oacc_routine (parser, decl, false);
19601 /* Adjust location of decl if declarator->id_loc is more appropriate:
19602 set, and decl wasn't merged with another decl, in which case its
19603 location would be different from input_location, and more accurate. */
19604 if (DECL_P (decl)
19605 && declarator->id_loc != UNKNOWN_LOCATION
19606 && DECL_SOURCE_LOCATION (decl) == input_location)
19607 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19609 else if (scope)
19610 /* Enter the SCOPE. That way unqualified names appearing in the
19611 initializer will be looked up in SCOPE. */
19612 pushed_scope = push_scope (scope);
19614 /* Perform deferred access control checks, now that we know in which
19615 SCOPE the declared entity resides. */
19616 if (!member_p && decl)
19618 tree saved_current_function_decl = NULL_TREE;
19620 /* If the entity being declared is a function, pretend that we
19621 are in its scope. If it is a `friend', it may have access to
19622 things that would not otherwise be accessible. */
19623 if (TREE_CODE (decl) == FUNCTION_DECL)
19625 saved_current_function_decl = current_function_decl;
19626 current_function_decl = decl;
19629 /* Perform access checks for template parameters. */
19630 cp_parser_perform_template_parameter_access_checks (checks);
19632 /* Perform the access control checks for the declarator and the
19633 decl-specifiers. */
19634 perform_deferred_access_checks (tf_warning_or_error);
19636 /* Restore the saved value. */
19637 if (TREE_CODE (decl) == FUNCTION_DECL)
19638 current_function_decl = saved_current_function_decl;
19641 /* Parse the initializer. */
19642 initializer = NULL_TREE;
19643 is_direct_init = false;
19644 is_non_constant_init = true;
19645 if (is_initialized)
19647 if (function_declarator_p (declarator))
19649 if (initialization_kind == CPP_EQ)
19650 initializer = cp_parser_pure_specifier (parser);
19651 else
19653 /* If the declaration was erroneous, we don't really
19654 know what the user intended, so just silently
19655 consume the initializer. */
19656 if (decl != error_mark_node)
19657 error_at (tmp_init_loc, "initializer provided for function");
19658 cp_parser_skip_to_closing_parenthesis (parser,
19659 /*recovering=*/true,
19660 /*or_comma=*/false,
19661 /*consume_paren=*/true);
19664 else
19666 /* We want to record the extra mangling scope for in-class
19667 initializers of class members and initializers of static data
19668 member templates. The former involves deferring
19669 parsing of the initializer until end of class as with default
19670 arguments. So right here we only handle the latter. */
19671 if (!member_p && processing_template_decl && decl != error_mark_node)
19672 start_lambda_scope (decl);
19673 initializer = cp_parser_initializer (parser,
19674 &is_direct_init,
19675 &is_non_constant_init);
19676 if (!member_p && processing_template_decl && decl != error_mark_node)
19677 finish_lambda_scope ();
19678 if (initializer == error_mark_node)
19679 cp_parser_skip_to_end_of_statement (parser);
19683 /* The old parser allows attributes to appear after a parenthesized
19684 initializer. Mark Mitchell proposed removing this functionality
19685 on the GCC mailing lists on 2002-08-13. This parser accepts the
19686 attributes -- but ignores them. */
19687 if (cp_parser_allow_gnu_extensions_p (parser)
19688 && initialization_kind == CPP_OPEN_PAREN)
19689 if (cp_parser_attributes_opt (parser))
19690 warning (OPT_Wattributes,
19691 "attributes after parenthesized initializer ignored");
19693 /* And now complain about a non-function implicit template. */
19694 if (bogus_implicit_tmpl && decl != error_mark_node)
19695 error_at (DECL_SOURCE_LOCATION (decl),
19696 "non-function %qD declared as implicit template", decl);
19698 /* For an in-class declaration, use `grokfield' to create the
19699 declaration. */
19700 if (member_p)
19702 if (pushed_scope)
19704 pop_scope (pushed_scope);
19705 pushed_scope = NULL_TREE;
19707 decl = grokfield (declarator, decl_specifiers,
19708 initializer, !is_non_constant_init,
19709 /*asmspec=*/NULL_TREE,
19710 attr_chainon (attributes, prefix_attributes));
19711 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19712 cp_parser_save_default_args (parser, decl);
19713 cp_finalize_omp_declare_simd (parser, decl);
19714 cp_finalize_oacc_routine (parser, decl, false);
19717 /* Finish processing the declaration. But, skip member
19718 declarations. */
19719 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19721 cp_finish_decl (decl,
19722 initializer, !is_non_constant_init,
19723 asm_specification,
19724 /* If the initializer is in parentheses, then this is
19725 a direct-initialization, which means that an
19726 `explicit' constructor is OK. Otherwise, an
19727 `explicit' constructor cannot be used. */
19728 ((is_direct_init || !is_initialized)
19729 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19731 else if ((cxx_dialect != cxx98) && friend_p
19732 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19733 /* Core issue #226 (C++0x only): A default template-argument
19734 shall not be specified in a friend class template
19735 declaration. */
19736 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19737 /*is_partial=*/false, /*is_friend_decl=*/1);
19739 if (!friend_p && pushed_scope)
19740 pop_scope (pushed_scope);
19742 if (function_declarator_p (declarator)
19743 && parser->fully_implicit_function_template_p)
19745 if (member_p)
19746 decl = finish_fully_implicit_template (parser, decl);
19747 else
19748 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19751 if (auto_result && is_initialized && decl_specifiers->type
19752 && type_uses_auto (decl_specifiers->type))
19753 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19755 return decl;
19758 /* Parse a declarator.
19760 declarator:
19761 direct-declarator
19762 ptr-operator declarator
19764 abstract-declarator:
19765 ptr-operator abstract-declarator [opt]
19766 direct-abstract-declarator
19768 GNU Extensions:
19770 declarator:
19771 attributes [opt] direct-declarator
19772 attributes [opt] ptr-operator declarator
19774 abstract-declarator:
19775 attributes [opt] ptr-operator abstract-declarator [opt]
19776 attributes [opt] direct-abstract-declarator
19778 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19779 detect constructors, destructors, deduction guides, or conversion operators.
19780 It is set to -1 if the declarator is a name, and +1 if it is a
19781 function. Otherwise it is set to zero. Usually you just want to
19782 test for >0, but internally the negative value is used.
19784 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19785 a decl-specifier-seq unless it declares a constructor, destructor,
19786 or conversion. It might seem that we could check this condition in
19787 semantic analysis, rather than parsing, but that makes it difficult
19788 to handle something like `f()'. We want to notice that there are
19789 no decl-specifiers, and therefore realize that this is an
19790 expression, not a declaration.)
19792 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19793 the declarator is a direct-declarator of the form "(...)".
19795 MEMBER_P is true iff this declarator is a member-declarator.
19797 FRIEND_P is true iff this declarator is a friend. */
19799 static cp_declarator *
19800 cp_parser_declarator (cp_parser* parser,
19801 cp_parser_declarator_kind dcl_kind,
19802 int* ctor_dtor_or_conv_p,
19803 bool* parenthesized_p,
19804 bool member_p, bool friend_p)
19806 cp_declarator *declarator;
19807 enum tree_code code;
19808 cp_cv_quals cv_quals;
19809 tree class_type;
19810 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19812 /* Assume this is not a constructor, destructor, or type-conversion
19813 operator. */
19814 if (ctor_dtor_or_conv_p)
19815 *ctor_dtor_or_conv_p = 0;
19817 if (cp_parser_allow_gnu_extensions_p (parser))
19818 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19820 /* Check for the ptr-operator production. */
19821 cp_parser_parse_tentatively (parser);
19822 /* Parse the ptr-operator. */
19823 code = cp_parser_ptr_operator (parser,
19824 &class_type,
19825 &cv_quals,
19826 &std_attributes);
19828 /* If that worked, then we have a ptr-operator. */
19829 if (cp_parser_parse_definitely (parser))
19831 /* If a ptr-operator was found, then this declarator was not
19832 parenthesized. */
19833 if (parenthesized_p)
19834 *parenthesized_p = true;
19835 /* The dependent declarator is optional if we are parsing an
19836 abstract-declarator. */
19837 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19838 cp_parser_parse_tentatively (parser);
19840 /* Parse the dependent declarator. */
19841 declarator = cp_parser_declarator (parser, dcl_kind,
19842 /*ctor_dtor_or_conv_p=*/NULL,
19843 /*parenthesized_p=*/NULL,
19844 /*member_p=*/false,
19845 friend_p);
19847 /* If we are parsing an abstract-declarator, we must handle the
19848 case where the dependent declarator is absent. */
19849 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19850 && !cp_parser_parse_definitely (parser))
19851 declarator = NULL;
19853 declarator = cp_parser_make_indirect_declarator
19854 (code, class_type, cv_quals, declarator, std_attributes);
19856 /* Everything else is a direct-declarator. */
19857 else
19859 if (parenthesized_p)
19860 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19861 CPP_OPEN_PAREN);
19862 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19863 ctor_dtor_or_conv_p,
19864 member_p, friend_p);
19867 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19868 declarator->attributes = gnu_attributes;
19869 return declarator;
19872 /* Parse a direct-declarator or direct-abstract-declarator.
19874 direct-declarator:
19875 declarator-id
19876 direct-declarator ( parameter-declaration-clause )
19877 cv-qualifier-seq [opt]
19878 ref-qualifier [opt]
19879 exception-specification [opt]
19880 direct-declarator [ constant-expression [opt] ]
19881 ( declarator )
19883 direct-abstract-declarator:
19884 direct-abstract-declarator [opt]
19885 ( parameter-declaration-clause )
19886 cv-qualifier-seq [opt]
19887 ref-qualifier [opt]
19888 exception-specification [opt]
19889 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19890 ( abstract-declarator )
19892 Returns a representation of the declarator. DCL_KIND is
19893 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19894 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19895 we are parsing a direct-declarator. It is
19896 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19897 of ambiguity we prefer an abstract declarator, as per
19898 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19899 as for cp_parser_declarator. */
19901 static cp_declarator *
19902 cp_parser_direct_declarator (cp_parser* parser,
19903 cp_parser_declarator_kind dcl_kind,
19904 int* ctor_dtor_or_conv_p,
19905 bool member_p, bool friend_p)
19907 cp_token *token;
19908 cp_declarator *declarator = NULL;
19909 tree scope = NULL_TREE;
19910 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19911 bool saved_in_declarator_p = parser->in_declarator_p;
19912 bool first = true;
19913 tree pushed_scope = NULL_TREE;
19914 cp_token *open_paren = NULL, *close_paren = NULL;
19916 while (true)
19918 /* Peek at the next token. */
19919 token = cp_lexer_peek_token (parser->lexer);
19920 if (token->type == CPP_OPEN_PAREN)
19922 /* This is either a parameter-declaration-clause, or a
19923 parenthesized declarator. When we know we are parsing a
19924 named declarator, it must be a parenthesized declarator
19925 if FIRST is true. For instance, `(int)' is a
19926 parameter-declaration-clause, with an omitted
19927 direct-abstract-declarator. But `((*))', is a
19928 parenthesized abstract declarator. Finally, when T is a
19929 template parameter `(T)' is a
19930 parameter-declaration-clause, and not a parenthesized
19931 named declarator.
19933 We first try and parse a parameter-declaration-clause,
19934 and then try a nested declarator (if FIRST is true).
19936 It is not an error for it not to be a
19937 parameter-declaration-clause, even when FIRST is
19938 false. Consider,
19940 int i (int);
19941 int i (3);
19943 The first is the declaration of a function while the
19944 second is the definition of a variable, including its
19945 initializer.
19947 Having seen only the parenthesis, we cannot know which of
19948 these two alternatives should be selected. Even more
19949 complex are examples like:
19951 int i (int (a));
19952 int i (int (3));
19954 The former is a function-declaration; the latter is a
19955 variable initialization.
19957 Thus again, we try a parameter-declaration-clause, and if
19958 that fails, we back out and return. */
19960 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19962 tree params;
19963 bool is_declarator = false;
19965 open_paren = NULL;
19967 /* In a member-declarator, the only valid interpretation
19968 of a parenthesis is the start of a
19969 parameter-declaration-clause. (It is invalid to
19970 initialize a static data member with a parenthesized
19971 initializer; only the "=" form of initialization is
19972 permitted.) */
19973 if (!member_p)
19974 cp_parser_parse_tentatively (parser);
19976 /* Consume the `('. */
19977 matching_parens parens;
19978 parens.consume_open (parser);
19979 if (first)
19981 /* If this is going to be an abstract declarator, we're
19982 in a declarator and we can't have default args. */
19983 parser->default_arg_ok_p = false;
19984 parser->in_declarator_p = true;
19987 begin_scope (sk_function_parms, NULL_TREE);
19989 /* Parse the parameter-declaration-clause. */
19990 params = cp_parser_parameter_declaration_clause (parser);
19992 /* Consume the `)'. */
19993 parens.require_close (parser);
19995 /* If all went well, parse the cv-qualifier-seq,
19996 ref-qualifier and the exception-specification. */
19997 if (member_p || cp_parser_parse_definitely (parser))
19999 cp_cv_quals cv_quals;
20000 cp_virt_specifiers virt_specifiers;
20001 cp_ref_qualifier ref_qual;
20002 tree exception_specification;
20003 tree late_return;
20004 tree attrs;
20005 bool memfn = (member_p || (pushed_scope
20006 && CLASS_TYPE_P (pushed_scope)));
20008 is_declarator = true;
20010 if (ctor_dtor_or_conv_p)
20011 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20012 first = false;
20014 /* Parse the cv-qualifier-seq. */
20015 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20016 /* Parse the ref-qualifier. */
20017 ref_qual = cp_parser_ref_qualifier_opt (parser);
20018 /* Parse the tx-qualifier. */
20019 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20020 /* And the exception-specification. */
20021 exception_specification
20022 = cp_parser_exception_specification_opt (parser);
20024 attrs = cp_parser_std_attribute_spec_seq (parser);
20026 /* In here, we handle cases where attribute is used after
20027 the function declaration. For example:
20028 void func (int x) __attribute__((vector(..))); */
20029 tree gnu_attrs = NULL_TREE;
20030 tree requires_clause = NULL_TREE;
20031 late_return = (cp_parser_late_return_type_opt
20032 (parser, declarator, requires_clause,
20033 memfn ? cv_quals : -1));
20035 /* Parse the virt-specifier-seq. */
20036 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20038 /* Create the function-declarator. */
20039 declarator = make_call_declarator (declarator,
20040 params,
20041 cv_quals,
20042 virt_specifiers,
20043 ref_qual,
20044 tx_qual,
20045 exception_specification,
20046 late_return,
20047 requires_clause);
20048 declarator->std_attributes = attrs;
20049 declarator->attributes = gnu_attrs;
20050 /* Any subsequent parameter lists are to do with
20051 return type, so are not those of the declared
20052 function. */
20053 parser->default_arg_ok_p = false;
20056 /* Remove the function parms from scope. */
20057 pop_bindings_and_leave_scope ();
20059 if (is_declarator)
20060 /* Repeat the main loop. */
20061 continue;
20064 /* If this is the first, we can try a parenthesized
20065 declarator. */
20066 if (first)
20068 bool saved_in_type_id_in_expr_p;
20070 parser->default_arg_ok_p = saved_default_arg_ok_p;
20071 parser->in_declarator_p = saved_in_declarator_p;
20073 open_paren = token;
20074 /* Consume the `('. */
20075 matching_parens parens;
20076 parens.consume_open (parser);
20077 /* Parse the nested declarator. */
20078 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20079 parser->in_type_id_in_expr_p = true;
20080 declarator
20081 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20082 /*parenthesized_p=*/NULL,
20083 member_p, friend_p);
20084 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20085 first = false;
20086 /* Expect a `)'. */
20087 close_paren = cp_lexer_peek_token (parser->lexer);
20088 if (!parens.require_close (parser))
20089 declarator = cp_error_declarator;
20090 if (declarator == cp_error_declarator)
20091 break;
20093 goto handle_declarator;
20095 /* Otherwise, we must be done. */
20096 else
20097 break;
20099 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20100 && token->type == CPP_OPEN_SQUARE
20101 && !cp_next_tokens_can_be_attribute_p (parser))
20103 /* Parse an array-declarator. */
20104 tree bounds, attrs;
20106 if (ctor_dtor_or_conv_p)
20107 *ctor_dtor_or_conv_p = 0;
20109 open_paren = NULL;
20110 first = false;
20111 parser->default_arg_ok_p = false;
20112 parser->in_declarator_p = true;
20113 /* Consume the `['. */
20114 cp_lexer_consume_token (parser->lexer);
20115 /* Peek at the next token. */
20116 token = cp_lexer_peek_token (parser->lexer);
20117 /* If the next token is `]', then there is no
20118 constant-expression. */
20119 if (token->type != CPP_CLOSE_SQUARE)
20121 bool non_constant_p;
20122 bounds
20123 = cp_parser_constant_expression (parser,
20124 /*allow_non_constant=*/true,
20125 &non_constant_p);
20126 if (!non_constant_p)
20127 /* OK */;
20128 else if (error_operand_p (bounds))
20129 /* Already gave an error. */;
20130 else if (!parser->in_function_body
20131 || current_binding_level->kind == sk_function_parms)
20133 /* Normally, the array bound must be an integral constant
20134 expression. However, as an extension, we allow VLAs
20135 in function scopes as long as they aren't part of a
20136 parameter declaration. */
20137 cp_parser_error (parser,
20138 "array bound is not an integer constant");
20139 bounds = error_mark_node;
20141 else if (processing_template_decl
20142 && !type_dependent_expression_p (bounds))
20144 /* Remember this wasn't a constant-expression. */
20145 bounds = build_nop (TREE_TYPE (bounds), bounds);
20146 TREE_SIDE_EFFECTS (bounds) = 1;
20149 else
20150 bounds = NULL_TREE;
20151 /* Look for the closing `]'. */
20152 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20154 declarator = cp_error_declarator;
20155 break;
20158 attrs = cp_parser_std_attribute_spec_seq (parser);
20159 declarator = make_array_declarator (declarator, bounds);
20160 declarator->std_attributes = attrs;
20162 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20165 tree qualifying_scope;
20166 tree unqualified_name;
20167 tree attrs;
20168 special_function_kind sfk;
20169 bool abstract_ok;
20170 bool pack_expansion_p = false;
20171 cp_token *declarator_id_start_token;
20173 /* Parse a declarator-id */
20174 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20175 if (abstract_ok)
20177 cp_parser_parse_tentatively (parser);
20179 /* If we see an ellipsis, we should be looking at a
20180 parameter pack. */
20181 if (token->type == CPP_ELLIPSIS)
20183 /* Consume the `...' */
20184 cp_lexer_consume_token (parser->lexer);
20186 pack_expansion_p = true;
20190 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20191 unqualified_name
20192 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20193 qualifying_scope = parser->scope;
20194 if (abstract_ok)
20196 bool okay = false;
20198 if (!unqualified_name && pack_expansion_p)
20200 /* Check whether an error occurred. */
20201 okay = !cp_parser_error_occurred (parser);
20203 /* We already consumed the ellipsis to mark a
20204 parameter pack, but we have no way to report it,
20205 so abort the tentative parse. We will be exiting
20206 immediately anyway. */
20207 cp_parser_abort_tentative_parse (parser);
20209 else
20210 okay = cp_parser_parse_definitely (parser);
20212 if (!okay)
20213 unqualified_name = error_mark_node;
20214 else if (unqualified_name
20215 && (qualifying_scope
20216 || (!identifier_p (unqualified_name))))
20218 cp_parser_error (parser, "expected unqualified-id");
20219 unqualified_name = error_mark_node;
20223 if (!unqualified_name)
20224 return NULL;
20225 if (unqualified_name == error_mark_node)
20227 declarator = cp_error_declarator;
20228 pack_expansion_p = false;
20229 declarator->parameter_pack_p = false;
20230 break;
20233 attrs = cp_parser_std_attribute_spec_seq (parser);
20235 if (qualifying_scope && at_namespace_scope_p ()
20236 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20238 /* In the declaration of a member of a template class
20239 outside of the class itself, the SCOPE will sometimes
20240 be a TYPENAME_TYPE. For example, given:
20242 template <typename T>
20243 int S<T>::R::i = 3;
20245 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20246 this context, we must resolve S<T>::R to an ordinary
20247 type, rather than a typename type.
20249 The reason we normally avoid resolving TYPENAME_TYPEs
20250 is that a specialization of `S' might render
20251 `S<T>::R' not a type. However, if `S' is
20252 specialized, then this `i' will not be used, so there
20253 is no harm in resolving the types here. */
20254 tree type;
20256 /* Resolve the TYPENAME_TYPE. */
20257 type = resolve_typename_type (qualifying_scope,
20258 /*only_current_p=*/false);
20259 /* If that failed, the declarator is invalid. */
20260 if (TREE_CODE (type) == TYPENAME_TYPE)
20262 if (typedef_variant_p (type))
20263 error_at (declarator_id_start_token->location,
20264 "cannot define member of dependent typedef "
20265 "%qT", type);
20266 else
20267 error_at (declarator_id_start_token->location,
20268 "%<%T::%E%> is not a type",
20269 TYPE_CONTEXT (qualifying_scope),
20270 TYPE_IDENTIFIER (qualifying_scope));
20272 qualifying_scope = type;
20275 sfk = sfk_none;
20277 if (unqualified_name)
20279 tree class_type;
20281 if (qualifying_scope
20282 && CLASS_TYPE_P (qualifying_scope))
20283 class_type = qualifying_scope;
20284 else
20285 class_type = current_class_type;
20287 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20289 tree name_type = TREE_TYPE (unqualified_name);
20291 if (!class_type || !same_type_p (name_type, class_type))
20293 /* We do not attempt to print the declarator
20294 here because we do not have enough
20295 information about its original syntactic
20296 form. */
20297 cp_parser_error (parser, "invalid declarator");
20298 declarator = cp_error_declarator;
20299 break;
20301 else if (qualifying_scope
20302 && CLASSTYPE_USE_TEMPLATE (name_type))
20304 error_at (declarator_id_start_token->location,
20305 "invalid use of constructor as a template");
20306 inform (declarator_id_start_token->location,
20307 "use %<%T::%D%> instead of %<%T::%D%> to "
20308 "name the constructor in a qualified name",
20309 class_type,
20310 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20311 class_type, name_type);
20312 declarator = cp_error_declarator;
20313 break;
20315 unqualified_name = constructor_name (class_type);
20318 if (class_type)
20320 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20321 sfk = sfk_destructor;
20322 else if (identifier_p (unqualified_name)
20323 && IDENTIFIER_CONV_OP_P (unqualified_name))
20324 sfk = sfk_conversion;
20325 else if (/* There's no way to declare a constructor
20326 for an unnamed type, even if the type
20327 got a name for linkage purposes. */
20328 !TYPE_WAS_UNNAMED (class_type)
20329 /* Handle correctly (c++/19200):
20331 struct S {
20332 struct T{};
20333 friend void S(T);
20336 and also:
20338 namespace N {
20339 void S();
20342 struct S {
20343 friend void N::S();
20344 }; */
20345 && (!friend_p || class_type == qualifying_scope)
20346 && constructor_name_p (unqualified_name,
20347 class_type))
20348 sfk = sfk_constructor;
20349 else if (is_overloaded_fn (unqualified_name)
20350 && DECL_CONSTRUCTOR_P (get_first_fn
20351 (unqualified_name)))
20352 sfk = sfk_constructor;
20354 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20355 *ctor_dtor_or_conv_p = -1;
20358 declarator = make_id_declarator (qualifying_scope,
20359 unqualified_name,
20360 sfk);
20361 declarator->std_attributes = attrs;
20362 declarator->id_loc = token->location;
20363 declarator->parameter_pack_p = pack_expansion_p;
20365 if (pack_expansion_p)
20366 maybe_warn_variadic_templates ();
20369 handle_declarator:;
20370 scope = get_scope_of_declarator (declarator);
20371 if (scope)
20373 /* Any names that appear after the declarator-id for a
20374 member are looked up in the containing scope. */
20375 if (at_function_scope_p ())
20377 /* But declarations with qualified-ids can't appear in a
20378 function. */
20379 cp_parser_error (parser, "qualified-id in declaration");
20380 declarator = cp_error_declarator;
20381 break;
20383 pushed_scope = push_scope (scope);
20385 parser->in_declarator_p = true;
20386 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20387 || (declarator && declarator->kind == cdk_id))
20388 /* Default args are only allowed on function
20389 declarations. */
20390 parser->default_arg_ok_p = saved_default_arg_ok_p;
20391 else
20392 parser->default_arg_ok_p = false;
20394 first = false;
20396 /* We're done. */
20397 else
20398 break;
20401 /* For an abstract declarator, we might wind up with nothing at this
20402 point. That's an error; the declarator is not optional. */
20403 if (!declarator)
20404 cp_parser_error (parser, "expected declarator");
20405 else if (open_paren)
20407 /* Record overly parenthesized declarator so we can give a
20408 diagnostic about confusing decl/expr disambiguation. */
20409 if (declarator->kind == cdk_array)
20411 /* If the open and close parens are on different lines, this
20412 is probably a formatting thing, so ignore. */
20413 expanded_location open = expand_location (open_paren->location);
20414 expanded_location close = expand_location (close_paren->location);
20415 if (open.line != close.line || open.file != close.file)
20416 open_paren = NULL;
20418 if (open_paren)
20419 declarator->parenthesized = open_paren->location;
20422 /* If we entered a scope, we must exit it now. */
20423 if (pushed_scope)
20424 pop_scope (pushed_scope);
20426 parser->default_arg_ok_p = saved_default_arg_ok_p;
20427 parser->in_declarator_p = saved_in_declarator_p;
20429 return declarator;
20432 /* Parse a ptr-operator.
20434 ptr-operator:
20435 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20436 * cv-qualifier-seq [opt]
20438 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20439 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20441 GNU Extension:
20443 ptr-operator:
20444 & cv-qualifier-seq [opt]
20446 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20447 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20448 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20449 filled in with the TYPE containing the member. *CV_QUALS is
20450 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20451 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20452 Note that the tree codes returned by this function have nothing
20453 to do with the types of trees that will be eventually be created
20454 to represent the pointer or reference type being parsed. They are
20455 just constants with suggestive names. */
20456 static enum tree_code
20457 cp_parser_ptr_operator (cp_parser* parser,
20458 tree* type,
20459 cp_cv_quals *cv_quals,
20460 tree *attributes)
20462 enum tree_code code = ERROR_MARK;
20463 cp_token *token;
20464 tree attrs = NULL_TREE;
20466 /* Assume that it's not a pointer-to-member. */
20467 *type = NULL_TREE;
20468 /* And that there are no cv-qualifiers. */
20469 *cv_quals = TYPE_UNQUALIFIED;
20471 /* Peek at the next token. */
20472 token = cp_lexer_peek_token (parser->lexer);
20474 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20475 if (token->type == CPP_MULT)
20476 code = INDIRECT_REF;
20477 else if (token->type == CPP_AND)
20478 code = ADDR_EXPR;
20479 else if ((cxx_dialect != cxx98) &&
20480 token->type == CPP_AND_AND) /* C++0x only */
20481 code = NON_LVALUE_EXPR;
20483 if (code != ERROR_MARK)
20485 /* Consume the `*', `&' or `&&'. */
20486 cp_lexer_consume_token (parser->lexer);
20488 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20489 `&', if we are allowing GNU extensions. (The only qualifier
20490 that can legally appear after `&' is `restrict', but that is
20491 enforced during semantic analysis. */
20492 if (code == INDIRECT_REF
20493 || cp_parser_allow_gnu_extensions_p (parser))
20494 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20496 attrs = cp_parser_std_attribute_spec_seq (parser);
20497 if (attributes != NULL)
20498 *attributes = attrs;
20500 else
20502 /* Try the pointer-to-member case. */
20503 cp_parser_parse_tentatively (parser);
20504 /* Look for the optional `::' operator. */
20505 cp_parser_global_scope_opt (parser,
20506 /*current_scope_valid_p=*/false);
20507 /* Look for the nested-name specifier. */
20508 token = cp_lexer_peek_token (parser->lexer);
20509 cp_parser_nested_name_specifier (parser,
20510 /*typename_keyword_p=*/false,
20511 /*check_dependency_p=*/true,
20512 /*type_p=*/false,
20513 /*is_declaration=*/false);
20514 /* If we found it, and the next token is a `*', then we are
20515 indeed looking at a pointer-to-member operator. */
20516 if (!cp_parser_error_occurred (parser)
20517 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20519 /* Indicate that the `*' operator was used. */
20520 code = INDIRECT_REF;
20522 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20523 error_at (token->location, "%qD is a namespace", parser->scope);
20524 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20525 error_at (token->location, "cannot form pointer to member of "
20526 "non-class %q#T", parser->scope);
20527 else
20529 /* The type of which the member is a member is given by the
20530 current SCOPE. */
20531 *type = parser->scope;
20532 /* The next name will not be qualified. */
20533 parser->scope = NULL_TREE;
20534 parser->qualifying_scope = NULL_TREE;
20535 parser->object_scope = NULL_TREE;
20536 /* Look for optional c++11 attributes. */
20537 attrs = cp_parser_std_attribute_spec_seq (parser);
20538 if (attributes != NULL)
20539 *attributes = attrs;
20540 /* Look for the optional cv-qualifier-seq. */
20541 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20544 /* If that didn't work we don't have a ptr-operator. */
20545 if (!cp_parser_parse_definitely (parser))
20546 cp_parser_error (parser, "expected ptr-operator");
20549 return code;
20552 /* Parse an (optional) cv-qualifier-seq.
20554 cv-qualifier-seq:
20555 cv-qualifier cv-qualifier-seq [opt]
20557 cv-qualifier:
20558 const
20559 volatile
20561 GNU Extension:
20563 cv-qualifier:
20564 __restrict__
20566 Returns a bitmask representing the cv-qualifiers. */
20568 static cp_cv_quals
20569 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20571 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20573 while (true)
20575 cp_token *token;
20576 cp_cv_quals cv_qualifier;
20578 /* Peek at the next token. */
20579 token = cp_lexer_peek_token (parser->lexer);
20580 /* See if it's a cv-qualifier. */
20581 switch (token->keyword)
20583 case RID_CONST:
20584 cv_qualifier = TYPE_QUAL_CONST;
20585 break;
20587 case RID_VOLATILE:
20588 cv_qualifier = TYPE_QUAL_VOLATILE;
20589 break;
20591 case RID_RESTRICT:
20592 cv_qualifier = TYPE_QUAL_RESTRICT;
20593 break;
20595 default:
20596 cv_qualifier = TYPE_UNQUALIFIED;
20597 break;
20600 if (!cv_qualifier)
20601 break;
20603 if (cv_quals & cv_qualifier)
20605 gcc_rich_location richloc (token->location);
20606 richloc.add_fixit_remove ();
20607 error_at (&richloc, "duplicate cv-qualifier");
20608 cp_lexer_purge_token (parser->lexer);
20610 else
20612 cp_lexer_consume_token (parser->lexer);
20613 cv_quals |= cv_qualifier;
20617 return cv_quals;
20620 /* Parse an (optional) ref-qualifier
20622 ref-qualifier:
20626 Returns cp_ref_qualifier representing ref-qualifier. */
20628 static cp_ref_qualifier
20629 cp_parser_ref_qualifier_opt (cp_parser* parser)
20631 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20633 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20634 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20635 return ref_qual;
20637 while (true)
20639 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20640 cp_token *token = cp_lexer_peek_token (parser->lexer);
20642 switch (token->type)
20644 case CPP_AND:
20645 curr_ref_qual = REF_QUAL_LVALUE;
20646 break;
20648 case CPP_AND_AND:
20649 curr_ref_qual = REF_QUAL_RVALUE;
20650 break;
20652 default:
20653 curr_ref_qual = REF_QUAL_NONE;
20654 break;
20657 if (!curr_ref_qual)
20658 break;
20659 else if (ref_qual)
20661 error_at (token->location, "multiple ref-qualifiers");
20662 cp_lexer_purge_token (parser->lexer);
20664 else
20666 ref_qual = curr_ref_qual;
20667 cp_lexer_consume_token (parser->lexer);
20671 return ref_qual;
20674 /* Parse an optional tx-qualifier.
20676 tx-qualifier:
20677 transaction_safe
20678 transaction_safe_dynamic */
20680 static tree
20681 cp_parser_tx_qualifier_opt (cp_parser *parser)
20683 cp_token *token = cp_lexer_peek_token (parser->lexer);
20684 if (token->type == CPP_NAME)
20686 tree name = token->u.value;
20687 const char *p = IDENTIFIER_POINTER (name);
20688 const int len = strlen ("transaction_safe");
20689 if (!strncmp (p, "transaction_safe", len))
20691 p += len;
20692 if (*p == '\0'
20693 || !strcmp (p, "_dynamic"))
20695 cp_lexer_consume_token (parser->lexer);
20696 if (!flag_tm)
20698 error ("%qE requires %<-fgnu-tm%>", name);
20699 return NULL_TREE;
20701 else
20702 return name;
20706 return NULL_TREE;
20709 /* Parse an (optional) virt-specifier-seq.
20711 virt-specifier-seq:
20712 virt-specifier virt-specifier-seq [opt]
20714 virt-specifier:
20715 override
20716 final
20718 Returns a bitmask representing the virt-specifiers. */
20720 static cp_virt_specifiers
20721 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20723 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20725 while (true)
20727 cp_token *token;
20728 cp_virt_specifiers virt_specifier;
20730 /* Peek at the next token. */
20731 token = cp_lexer_peek_token (parser->lexer);
20732 /* See if it's a virt-specifier-qualifier. */
20733 if (token->type != CPP_NAME)
20734 break;
20735 if (id_equal (token->u.value, "override"))
20737 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20738 virt_specifier = VIRT_SPEC_OVERRIDE;
20740 else if (id_equal (token->u.value, "final"))
20742 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20743 virt_specifier = VIRT_SPEC_FINAL;
20745 else if (id_equal (token->u.value, "__final"))
20747 virt_specifier = VIRT_SPEC_FINAL;
20749 else
20750 break;
20752 if (virt_specifiers & virt_specifier)
20754 gcc_rich_location richloc (token->location);
20755 richloc.add_fixit_remove ();
20756 error_at (&richloc, "duplicate virt-specifier");
20757 cp_lexer_purge_token (parser->lexer);
20759 else
20761 cp_lexer_consume_token (parser->lexer);
20762 virt_specifiers |= virt_specifier;
20765 return virt_specifiers;
20768 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20769 is in scope even though it isn't real. */
20771 void
20772 inject_this_parameter (tree ctype, cp_cv_quals quals)
20774 tree this_parm;
20776 if (current_class_ptr)
20778 /* We don't clear this between NSDMIs. Is it already what we want? */
20779 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20780 if (DECL_P (current_class_ptr)
20781 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20782 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20783 && cp_type_quals (type) == quals)
20784 return;
20787 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20788 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20789 current_class_ptr = NULL_TREE;
20790 current_class_ref
20791 = cp_build_fold_indirect_ref (this_parm);
20792 current_class_ptr = this_parm;
20795 /* Return true iff our current scope is a non-static data member
20796 initializer. */
20798 bool
20799 parsing_nsdmi (void)
20801 /* We recognize NSDMI context by the context-less 'this' pointer set up
20802 by the function above. */
20803 if (current_class_ptr
20804 && TREE_CODE (current_class_ptr) == PARM_DECL
20805 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20806 return true;
20807 return false;
20810 /* Parse a late-specified return type, if any. This is not a separate
20811 non-terminal, but part of a function declarator, which looks like
20813 -> trailing-type-specifier-seq abstract-declarator(opt)
20815 Returns the type indicated by the type-id.
20817 In addition to this, parse any queued up #pragma omp declare simd
20818 clauses, and #pragma acc routine clauses.
20820 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20821 function. */
20823 static tree
20824 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20825 tree& requires_clause, cp_cv_quals quals)
20827 cp_token *token;
20828 tree type = NULL_TREE;
20829 bool declare_simd_p = (parser->omp_declare_simd
20830 && declarator
20831 && declarator->kind == cdk_id);
20833 bool oacc_routine_p = (parser->oacc_routine
20834 && declarator
20835 && declarator->kind == cdk_id);
20837 /* Peek at the next token. */
20838 token = cp_lexer_peek_token (parser->lexer);
20839 /* A late-specified return type is indicated by an initial '->'. */
20840 if (token->type != CPP_DEREF
20841 && token->keyword != RID_REQUIRES
20842 && !(token->type == CPP_NAME
20843 && token->u.value == ridpointers[RID_REQUIRES])
20844 && !(declare_simd_p || oacc_routine_p))
20845 return NULL_TREE;
20847 tree save_ccp = current_class_ptr;
20848 tree save_ccr = current_class_ref;
20849 if (quals >= 0)
20851 /* DR 1207: 'this' is in scope in the trailing return type. */
20852 inject_this_parameter (current_class_type, quals);
20855 if (token->type == CPP_DEREF)
20857 /* Consume the ->. */
20858 cp_lexer_consume_token (parser->lexer);
20860 type = cp_parser_trailing_type_id (parser);
20863 /* Function declarations may be followed by a trailing
20864 requires-clause. */
20865 requires_clause = cp_parser_requires_clause_opt (parser);
20867 if (declare_simd_p)
20868 declarator->attributes
20869 = cp_parser_late_parsing_omp_declare_simd (parser,
20870 declarator->attributes);
20871 if (oacc_routine_p)
20872 declarator->attributes
20873 = cp_parser_late_parsing_oacc_routine (parser,
20874 declarator->attributes);
20876 if (quals >= 0)
20878 current_class_ptr = save_ccp;
20879 current_class_ref = save_ccr;
20882 return type;
20885 /* Parse a declarator-id.
20887 declarator-id:
20888 id-expression
20889 :: [opt] nested-name-specifier [opt] type-name
20891 In the `id-expression' case, the value returned is as for
20892 cp_parser_id_expression if the id-expression was an unqualified-id.
20893 If the id-expression was a qualified-id, then a SCOPE_REF is
20894 returned. The first operand is the scope (either a NAMESPACE_DECL
20895 or TREE_TYPE), but the second is still just a representation of an
20896 unqualified-id. */
20898 static tree
20899 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20901 tree id;
20902 /* The expression must be an id-expression. Assume that qualified
20903 names are the names of types so that:
20905 template <class T>
20906 int S<T>::R::i = 3;
20908 will work; we must treat `S<T>::R' as the name of a type.
20909 Similarly, assume that qualified names are templates, where
20910 required, so that:
20912 template <class T>
20913 int S<T>::R<T>::i = 3;
20915 will work, too. */
20916 id = cp_parser_id_expression (parser,
20917 /*template_keyword_p=*/false,
20918 /*check_dependency_p=*/false,
20919 /*template_p=*/NULL,
20920 /*declarator_p=*/true,
20921 optional_p);
20922 if (id && BASELINK_P (id))
20923 id = BASELINK_FUNCTIONS (id);
20924 return id;
20927 /* Parse a type-id.
20929 type-id:
20930 type-specifier-seq abstract-declarator [opt]
20932 Returns the TYPE specified. */
20934 static tree
20935 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20936 bool is_trailing_return)
20938 cp_decl_specifier_seq type_specifier_seq;
20939 cp_declarator *abstract_declarator;
20941 /* Parse the type-specifier-seq. */
20942 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20943 is_trailing_return,
20944 &type_specifier_seq);
20945 if (is_template_arg && type_specifier_seq.type
20946 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20947 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20948 /* A bare template name as a template argument is a template template
20949 argument, not a placeholder, so fail parsing it as a type argument. */
20951 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20952 cp_parser_simulate_error (parser);
20953 return error_mark_node;
20955 if (type_specifier_seq.type == error_mark_node)
20956 return error_mark_node;
20958 /* There might or might not be an abstract declarator. */
20959 cp_parser_parse_tentatively (parser);
20960 /* Look for the declarator. */
20961 abstract_declarator
20962 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20963 /*parenthesized_p=*/NULL,
20964 /*member_p=*/false,
20965 /*friend_p=*/false);
20966 /* Check to see if there really was a declarator. */
20967 if (!cp_parser_parse_definitely (parser))
20968 abstract_declarator = NULL;
20970 if (type_specifier_seq.type
20971 /* The concepts TS allows 'auto' as a type-id. */
20972 && (!flag_concepts || parser->in_type_id_in_expr_p)
20973 /* None of the valid uses of 'auto' in C++14 involve the type-id
20974 nonterminal, but it is valid in a trailing-return-type. */
20975 && !(cxx_dialect >= cxx14 && is_trailing_return))
20976 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20978 /* A type-id with type 'auto' is only ok if the abstract declarator
20979 is a function declarator with a late-specified return type.
20981 A type-id with 'auto' is also valid in a trailing-return-type
20982 in a compound-requirement. */
20983 if (abstract_declarator
20984 && abstract_declarator->kind == cdk_function
20985 && abstract_declarator->u.function.late_return_type)
20986 /* OK */;
20987 else if (parser->in_result_type_constraint_p)
20988 /* OK */;
20989 else
20991 location_t loc = type_specifier_seq.locations[ds_type_spec];
20992 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20994 error_at (loc, "missing template arguments after %qT",
20995 auto_node);
20996 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20997 tmpl);
20999 else
21000 error_at (loc, "invalid use of %qT", auto_node);
21001 return error_mark_node;
21005 return groktypename (&type_specifier_seq, abstract_declarator,
21006 is_template_arg);
21009 static tree
21010 cp_parser_type_id (cp_parser *parser)
21012 return cp_parser_type_id_1 (parser, false, false);
21015 static tree
21016 cp_parser_template_type_arg (cp_parser *parser)
21018 tree r;
21019 const char *saved_message = parser->type_definition_forbidden_message;
21020 parser->type_definition_forbidden_message
21021 = G_("types may not be defined in template arguments");
21022 r = cp_parser_type_id_1 (parser, true, false);
21023 parser->type_definition_forbidden_message = saved_message;
21024 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21026 error ("invalid use of %<auto%> in template argument");
21027 r = error_mark_node;
21029 return r;
21032 static tree
21033 cp_parser_trailing_type_id (cp_parser *parser)
21035 return cp_parser_type_id_1 (parser, false, true);
21038 /* Parse a type-specifier-seq.
21040 type-specifier-seq:
21041 type-specifier type-specifier-seq [opt]
21043 GNU extension:
21045 type-specifier-seq:
21046 attributes type-specifier-seq [opt]
21048 If IS_DECLARATION is true, we are at the start of a "condition" or
21049 exception-declaration, so we might be followed by a declarator-id.
21051 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21052 i.e. we've just seen "->".
21054 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21056 static void
21057 cp_parser_type_specifier_seq (cp_parser* parser,
21058 bool is_declaration,
21059 bool is_trailing_return,
21060 cp_decl_specifier_seq *type_specifier_seq)
21062 bool seen_type_specifier = false;
21063 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21064 cp_token *start_token = NULL;
21066 /* Clear the TYPE_SPECIFIER_SEQ. */
21067 clear_decl_specs (type_specifier_seq);
21069 /* In the context of a trailing return type, enum E { } is an
21070 elaborated-type-specifier followed by a function-body, not an
21071 enum-specifier. */
21072 if (is_trailing_return)
21073 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21075 /* Parse the type-specifiers and attributes. */
21076 while (true)
21078 tree type_specifier;
21079 bool is_cv_qualifier;
21081 /* Check for attributes first. */
21082 if (cp_next_tokens_can_be_attribute_p (parser))
21084 type_specifier_seq->attributes
21085 = attr_chainon (type_specifier_seq->attributes,
21086 cp_parser_attributes_opt (parser));
21087 continue;
21090 /* record the token of the beginning of the type specifier seq,
21091 for error reporting purposes*/
21092 if (!start_token)
21093 start_token = cp_lexer_peek_token (parser->lexer);
21095 /* Look for the type-specifier. */
21096 type_specifier = cp_parser_type_specifier (parser,
21097 flags,
21098 type_specifier_seq,
21099 /*is_declaration=*/false,
21100 NULL,
21101 &is_cv_qualifier);
21102 if (!type_specifier)
21104 /* If the first type-specifier could not be found, this is not a
21105 type-specifier-seq at all. */
21106 if (!seen_type_specifier)
21108 /* Set in_declarator_p to avoid skipping to the semicolon. */
21109 int in_decl = parser->in_declarator_p;
21110 parser->in_declarator_p = true;
21112 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21113 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21114 cp_parser_error (parser, "expected type-specifier");
21116 parser->in_declarator_p = in_decl;
21118 type_specifier_seq->type = error_mark_node;
21119 return;
21121 /* If subsequent type-specifiers could not be found, the
21122 type-specifier-seq is complete. */
21123 break;
21126 seen_type_specifier = true;
21127 /* The standard says that a condition can be:
21129 type-specifier-seq declarator = assignment-expression
21131 However, given:
21133 struct S {};
21134 if (int S = ...)
21136 we should treat the "S" as a declarator, not as a
21137 type-specifier. The standard doesn't say that explicitly for
21138 type-specifier-seq, but it does say that for
21139 decl-specifier-seq in an ordinary declaration. Perhaps it
21140 would be clearer just to allow a decl-specifier-seq here, and
21141 then add a semantic restriction that if any decl-specifiers
21142 that are not type-specifiers appear, the program is invalid. */
21143 if (is_declaration && !is_cv_qualifier)
21144 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21148 /* Return whether the function currently being declared has an associated
21149 template parameter list. */
21151 static bool
21152 function_being_declared_is_template_p (cp_parser* parser)
21154 if (!current_template_parms || processing_template_parmlist)
21155 return false;
21157 if (parser->implicit_template_scope)
21158 return true;
21160 if (at_class_scope_p ()
21161 && TYPE_BEING_DEFINED (current_class_type))
21162 return parser->num_template_parameter_lists != 0;
21164 return ((int) parser->num_template_parameter_lists > template_class_depth
21165 (current_class_type));
21168 /* Parse a parameter-declaration-clause.
21170 parameter-declaration-clause:
21171 parameter-declaration-list [opt] ... [opt]
21172 parameter-declaration-list , ...
21174 Returns a representation for the parameter declarations. A return
21175 value of NULL indicates a parameter-declaration-clause consisting
21176 only of an ellipsis. */
21178 static tree
21179 cp_parser_parameter_declaration_clause (cp_parser* parser)
21181 tree parameters;
21182 cp_token *token;
21183 bool ellipsis_p;
21184 bool is_error;
21186 struct cleanup {
21187 cp_parser* parser;
21188 int auto_is_implicit_function_template_parm_p;
21189 ~cleanup() {
21190 parser->auto_is_implicit_function_template_parm_p
21191 = auto_is_implicit_function_template_parm_p;
21193 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21195 (void) cleanup;
21197 if (!processing_specialization
21198 && !processing_template_parmlist
21199 && !processing_explicit_instantiation)
21200 if (!current_function_decl
21201 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21202 parser->auto_is_implicit_function_template_parm_p = true;
21204 /* Peek at the next token. */
21205 token = cp_lexer_peek_token (parser->lexer);
21206 /* Check for trivial parameter-declaration-clauses. */
21207 if (token->type == CPP_ELLIPSIS)
21209 /* Consume the `...' token. */
21210 cp_lexer_consume_token (parser->lexer);
21211 return NULL_TREE;
21213 else if (token->type == CPP_CLOSE_PAREN)
21214 /* There are no parameters. */
21216 #ifndef NO_IMPLICIT_EXTERN_C
21217 if (in_system_header_at (input_location)
21218 && current_class_type == NULL
21219 && current_lang_name == lang_name_c)
21220 return NULL_TREE;
21221 else
21222 #endif
21223 return void_list_node;
21225 /* Check for `(void)', too, which is a special case. */
21226 else if (token->keyword == RID_VOID
21227 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21228 == CPP_CLOSE_PAREN))
21230 /* Consume the `void' token. */
21231 cp_lexer_consume_token (parser->lexer);
21232 /* There are no parameters. */
21233 return void_list_node;
21236 /* Parse the parameter-declaration-list. */
21237 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21238 /* If a parse error occurred while parsing the
21239 parameter-declaration-list, then the entire
21240 parameter-declaration-clause is erroneous. */
21241 if (is_error)
21242 return NULL;
21244 /* Peek at the next token. */
21245 token = cp_lexer_peek_token (parser->lexer);
21246 /* If it's a `,', the clause should terminate with an ellipsis. */
21247 if (token->type == CPP_COMMA)
21249 /* Consume the `,'. */
21250 cp_lexer_consume_token (parser->lexer);
21251 /* Expect an ellipsis. */
21252 ellipsis_p
21253 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21255 /* It might also be `...' if the optional trailing `,' was
21256 omitted. */
21257 else if (token->type == CPP_ELLIPSIS)
21259 /* Consume the `...' token. */
21260 cp_lexer_consume_token (parser->lexer);
21261 /* And remember that we saw it. */
21262 ellipsis_p = true;
21264 else
21265 ellipsis_p = false;
21267 /* Finish the parameter list. */
21268 if (!ellipsis_p)
21269 parameters = chainon (parameters, void_list_node);
21271 return parameters;
21274 /* Parse a parameter-declaration-list.
21276 parameter-declaration-list:
21277 parameter-declaration
21278 parameter-declaration-list , parameter-declaration
21280 Returns a representation of the parameter-declaration-list, as for
21281 cp_parser_parameter_declaration_clause. However, the
21282 `void_list_node' is never appended to the list. Upon return,
21283 *IS_ERROR will be true iff an error occurred. */
21285 static tree
21286 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21288 tree parameters = NULL_TREE;
21289 tree *tail = &parameters;
21290 bool saved_in_unbraced_linkage_specification_p;
21291 int index = 0;
21293 /* Assume all will go well. */
21294 *is_error = false;
21295 /* The special considerations that apply to a function within an
21296 unbraced linkage specifications do not apply to the parameters
21297 to the function. */
21298 saved_in_unbraced_linkage_specification_p
21299 = parser->in_unbraced_linkage_specification_p;
21300 parser->in_unbraced_linkage_specification_p = false;
21302 /* Look for more parameters. */
21303 while (true)
21305 cp_parameter_declarator *parameter;
21306 tree decl = error_mark_node;
21307 bool parenthesized_p = false;
21308 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21309 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21310 (current_template_parms)) : 0);
21312 /* Parse the parameter. */
21313 parameter
21314 = cp_parser_parameter_declaration (parser,
21315 /*template_parm_p=*/false,
21316 &parenthesized_p);
21318 /* We don't know yet if the enclosing context is deprecated, so wait
21319 and warn in grokparms if appropriate. */
21320 deprecated_state = DEPRECATED_SUPPRESS;
21322 if (parameter)
21324 /* If a function parameter pack was specified and an implicit template
21325 parameter was introduced during cp_parser_parameter_declaration,
21326 change any implicit parameters introduced into packs. */
21327 if (parser->implicit_template_parms
21328 && parameter->declarator
21329 && parameter->declarator->parameter_pack_p)
21331 int latest_template_parm_idx = TREE_VEC_LENGTH
21332 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21334 if (latest_template_parm_idx != template_parm_idx)
21335 parameter->decl_specifiers.type = convert_generic_types_to_packs
21336 (parameter->decl_specifiers.type,
21337 template_parm_idx, latest_template_parm_idx);
21340 decl = grokdeclarator (parameter->declarator,
21341 &parameter->decl_specifiers,
21342 PARM,
21343 parameter->default_argument != NULL_TREE,
21344 &parameter->decl_specifiers.attributes);
21345 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21346 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21349 deprecated_state = DEPRECATED_NORMAL;
21351 /* If a parse error occurred parsing the parameter declaration,
21352 then the entire parameter-declaration-list is erroneous. */
21353 if (decl == error_mark_node)
21355 *is_error = true;
21356 parameters = error_mark_node;
21357 break;
21360 if (parameter->decl_specifiers.attributes)
21361 cplus_decl_attributes (&decl,
21362 parameter->decl_specifiers.attributes,
21364 if (DECL_NAME (decl))
21365 decl = pushdecl (decl);
21367 if (decl != error_mark_node)
21369 retrofit_lang_decl (decl);
21370 DECL_PARM_INDEX (decl) = ++index;
21371 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21374 /* Add the new parameter to the list. */
21375 *tail = build_tree_list (parameter->default_argument, decl);
21376 tail = &TREE_CHAIN (*tail);
21378 /* Peek at the next token. */
21379 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21380 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21381 /* These are for Objective-C++ */
21382 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21383 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21384 /* The parameter-declaration-list is complete. */
21385 break;
21386 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21388 cp_token *token;
21390 /* Peek at the next token. */
21391 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21392 /* If it's an ellipsis, then the list is complete. */
21393 if (token->type == CPP_ELLIPSIS)
21394 break;
21395 /* Otherwise, there must be more parameters. Consume the
21396 `,'. */
21397 cp_lexer_consume_token (parser->lexer);
21398 /* When parsing something like:
21400 int i(float f, double d)
21402 we can tell after seeing the declaration for "f" that we
21403 are not looking at an initialization of a variable "i",
21404 but rather at the declaration of a function "i".
21406 Due to the fact that the parsing of template arguments
21407 (as specified to a template-id) requires backtracking we
21408 cannot use this technique when inside a template argument
21409 list. */
21410 if (!parser->in_template_argument_list_p
21411 && !parser->in_type_id_in_expr_p
21412 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21413 /* However, a parameter-declaration of the form
21414 "float(f)" (which is a valid declaration of a
21415 parameter "f") can also be interpreted as an
21416 expression (the conversion of "f" to "float"). */
21417 && !parenthesized_p)
21418 cp_parser_commit_to_tentative_parse (parser);
21420 else
21422 cp_parser_error (parser, "expected %<,%> or %<...%>");
21423 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21424 cp_parser_skip_to_closing_parenthesis (parser,
21425 /*recovering=*/true,
21426 /*or_comma=*/false,
21427 /*consume_paren=*/false);
21428 break;
21432 parser->in_unbraced_linkage_specification_p
21433 = saved_in_unbraced_linkage_specification_p;
21435 /* Reset implicit_template_scope if we are about to leave the function
21436 parameter list that introduced it. Note that for out-of-line member
21437 definitions, there will be one or more class scopes before we get to
21438 the template parameter scope. */
21440 if (cp_binding_level *its = parser->implicit_template_scope)
21441 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21443 while (maybe_its->kind == sk_class)
21444 maybe_its = maybe_its->level_chain;
21445 if (maybe_its == its)
21447 parser->implicit_template_parms = 0;
21448 parser->implicit_template_scope = 0;
21452 return parameters;
21455 /* Parse a parameter declaration.
21457 parameter-declaration:
21458 decl-specifier-seq ... [opt] declarator
21459 decl-specifier-seq declarator = assignment-expression
21460 decl-specifier-seq ... [opt] abstract-declarator [opt]
21461 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21463 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21464 declares a template parameter. (In that case, a non-nested `>'
21465 token encountered during the parsing of the assignment-expression
21466 is not interpreted as a greater-than operator.)
21468 Returns a representation of the parameter, or NULL if an error
21469 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21470 true iff the declarator is of the form "(p)". */
21472 static cp_parameter_declarator *
21473 cp_parser_parameter_declaration (cp_parser *parser,
21474 bool template_parm_p,
21475 bool *parenthesized_p)
21477 int declares_class_or_enum;
21478 cp_decl_specifier_seq decl_specifiers;
21479 cp_declarator *declarator;
21480 tree default_argument;
21481 cp_token *token = NULL, *declarator_token_start = NULL;
21482 const char *saved_message;
21483 bool template_parameter_pack_p = false;
21485 /* In a template parameter, `>' is not an operator.
21487 [temp.param]
21489 When parsing a default template-argument for a non-type
21490 template-parameter, the first non-nested `>' is taken as the end
21491 of the template parameter-list rather than a greater-than
21492 operator. */
21494 /* Type definitions may not appear in parameter types. */
21495 saved_message = parser->type_definition_forbidden_message;
21496 parser->type_definition_forbidden_message
21497 = G_("types may not be defined in parameter types");
21499 /* Parse the declaration-specifiers. */
21500 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21501 cp_parser_decl_specifier_seq (parser,
21502 CP_PARSER_FLAGS_NONE,
21503 &decl_specifiers,
21504 &declares_class_or_enum);
21506 /* Complain about missing 'typename' or other invalid type names. */
21507 if (!decl_specifiers.any_type_specifiers_p
21508 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21509 decl_specifiers.type = error_mark_node;
21511 /* If an error occurred, there's no reason to attempt to parse the
21512 rest of the declaration. */
21513 if (cp_parser_error_occurred (parser))
21515 parser->type_definition_forbidden_message = saved_message;
21516 return NULL;
21519 /* Peek at the next token. */
21520 token = cp_lexer_peek_token (parser->lexer);
21522 /* If the next token is a `)', `,', `=', `>', or `...', then there
21523 is no declarator. However, when variadic templates are enabled,
21524 there may be a declarator following `...'. */
21525 if (token->type == CPP_CLOSE_PAREN
21526 || token->type == CPP_COMMA
21527 || token->type == CPP_EQ
21528 || token->type == CPP_GREATER)
21530 declarator = NULL;
21531 if (parenthesized_p)
21532 *parenthesized_p = false;
21534 /* Otherwise, there should be a declarator. */
21535 else
21537 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21538 parser->default_arg_ok_p = false;
21540 /* After seeing a decl-specifier-seq, if the next token is not a
21541 "(", there is no possibility that the code is a valid
21542 expression. Therefore, if parsing tentatively, we commit at
21543 this point. */
21544 if (!parser->in_template_argument_list_p
21545 /* In an expression context, having seen:
21547 (int((char ...
21549 we cannot be sure whether we are looking at a
21550 function-type (taking a "char" as a parameter) or a cast
21551 of some object of type "char" to "int". */
21552 && !parser->in_type_id_in_expr_p
21553 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21554 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21555 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21556 cp_parser_commit_to_tentative_parse (parser);
21557 /* Parse the declarator. */
21558 declarator_token_start = token;
21559 declarator = cp_parser_declarator (parser,
21560 CP_PARSER_DECLARATOR_EITHER,
21561 /*ctor_dtor_or_conv_p=*/NULL,
21562 parenthesized_p,
21563 /*member_p=*/false,
21564 /*friend_p=*/false);
21565 parser->default_arg_ok_p = saved_default_arg_ok_p;
21566 /* After the declarator, allow more attributes. */
21567 decl_specifiers.attributes
21568 = attr_chainon (decl_specifiers.attributes,
21569 cp_parser_attributes_opt (parser));
21571 /* If the declarator is a template parameter pack, remember that and
21572 clear the flag in the declarator itself so we don't get errors
21573 from grokdeclarator. */
21574 if (template_parm_p && declarator && declarator->parameter_pack_p)
21576 declarator->parameter_pack_p = false;
21577 template_parameter_pack_p = true;
21581 /* If the next token is an ellipsis, and we have not seen a declarator
21582 name, and if either the type of the declarator contains parameter
21583 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21584 for, eg, abbreviated integral type names), then we actually have a
21585 parameter pack expansion expression. Otherwise, leave the ellipsis
21586 for a C-style variadic function. */
21587 token = cp_lexer_peek_token (parser->lexer);
21588 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21590 tree type = decl_specifiers.type;
21592 if (type && DECL_P (type))
21593 type = TREE_TYPE (type);
21595 if (((type
21596 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21597 && (template_parm_p || uses_parameter_packs (type)))
21598 || (!type && template_parm_p))
21599 && declarator_can_be_parameter_pack (declarator))
21601 /* Consume the `...'. */
21602 cp_lexer_consume_token (parser->lexer);
21603 maybe_warn_variadic_templates ();
21605 /* Build a pack expansion type */
21606 if (template_parm_p)
21607 template_parameter_pack_p = true;
21608 else if (declarator)
21609 declarator->parameter_pack_p = true;
21610 else
21611 decl_specifiers.type = make_pack_expansion (type);
21615 /* The restriction on defining new types applies only to the type
21616 of the parameter, not to the default argument. */
21617 parser->type_definition_forbidden_message = saved_message;
21619 /* If the next token is `=', then process a default argument. */
21620 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21622 tree type = decl_specifiers.type;
21623 token = cp_lexer_peek_token (parser->lexer);
21624 /* If we are defining a class, then the tokens that make up the
21625 default argument must be saved and processed later. */
21626 if (!template_parm_p && at_class_scope_p ()
21627 && TYPE_BEING_DEFINED (current_class_type)
21628 && !LAMBDA_TYPE_P (current_class_type))
21629 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21631 // A constrained-type-specifier may declare a type template-parameter.
21632 else if (declares_constrained_type_template_parameter (type))
21633 default_argument
21634 = cp_parser_default_type_template_argument (parser);
21636 // A constrained-type-specifier may declare a template-template-parameter.
21637 else if (declares_constrained_template_template_parameter (type))
21638 default_argument
21639 = cp_parser_default_template_template_argument (parser);
21641 /* Outside of a class definition, we can just parse the
21642 assignment-expression. */
21643 else
21644 default_argument
21645 = cp_parser_default_argument (parser, template_parm_p);
21647 if (!parser->default_arg_ok_p)
21649 permerror (token->location,
21650 "default arguments are only "
21651 "permitted for function parameters");
21653 else if ((declarator && declarator->parameter_pack_p)
21654 || template_parameter_pack_p
21655 || (decl_specifiers.type
21656 && PACK_EXPANSION_P (decl_specifiers.type)))
21658 /* Find the name of the parameter pack. */
21659 cp_declarator *id_declarator = declarator;
21660 while (id_declarator && id_declarator->kind != cdk_id)
21661 id_declarator = id_declarator->declarator;
21663 if (id_declarator && id_declarator->kind == cdk_id)
21664 error_at (declarator_token_start->location,
21665 template_parm_p
21666 ? G_("template parameter pack %qD "
21667 "cannot have a default argument")
21668 : G_("parameter pack %qD cannot have "
21669 "a default argument"),
21670 id_declarator->u.id.unqualified_name);
21671 else
21672 error_at (declarator_token_start->location,
21673 template_parm_p
21674 ? G_("template parameter pack cannot have "
21675 "a default argument")
21676 : G_("parameter pack cannot have a "
21677 "default argument"));
21679 default_argument = NULL_TREE;
21682 else
21683 default_argument = NULL_TREE;
21685 /* Generate a location for the parameter, ranging from the start of the
21686 initial token to the end of the final token (using input_location for
21687 the latter, set up by cp_lexer_set_source_position_from_token when
21688 consuming tokens).
21690 If we have a identifier, then use it for the caret location, e.g.
21692 extern int callee (int one, int (*two)(int, int), float three);
21693 ~~~~~~^~~~~~~~~~~~~~
21695 otherwise, reuse the start location for the caret location e.g.:
21697 extern int callee (int one, int (*)(int, int), float three);
21698 ^~~~~~~~~~~~~~~~~
21701 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21702 ? declarator->id_loc
21703 : decl_spec_token_start->location);
21704 location_t param_loc = make_location (caret_loc,
21705 decl_spec_token_start->location,
21706 input_location);
21708 return make_parameter_declarator (&decl_specifiers,
21709 declarator,
21710 default_argument,
21711 param_loc,
21712 template_parameter_pack_p);
21715 /* Parse a default argument and return it.
21717 TEMPLATE_PARM_P is true if this is a default argument for a
21718 non-type template parameter. */
21719 static tree
21720 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21722 tree default_argument = NULL_TREE;
21723 bool saved_greater_than_is_operator_p;
21724 bool saved_local_variables_forbidden_p;
21725 bool non_constant_p, is_direct_init;
21727 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21728 set correctly. */
21729 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21730 parser->greater_than_is_operator_p = !template_parm_p;
21731 /* Local variable names (and the `this' keyword) may not
21732 appear in a default argument. */
21733 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21734 parser->local_variables_forbidden_p = true;
21735 /* Parse the assignment-expression. */
21736 if (template_parm_p)
21737 push_deferring_access_checks (dk_no_deferred);
21738 tree saved_class_ptr = NULL_TREE;
21739 tree saved_class_ref = NULL_TREE;
21740 /* The "this" pointer is not valid in a default argument. */
21741 if (cfun)
21743 saved_class_ptr = current_class_ptr;
21744 cp_function_chain->x_current_class_ptr = NULL_TREE;
21745 saved_class_ref = current_class_ref;
21746 cp_function_chain->x_current_class_ref = NULL_TREE;
21748 default_argument
21749 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21750 /* Restore the "this" pointer. */
21751 if (cfun)
21753 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21754 cp_function_chain->x_current_class_ref = saved_class_ref;
21756 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21758 if (template_parm_p)
21759 pop_deferring_access_checks ();
21760 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21761 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21763 return default_argument;
21766 /* Parse a function-body.
21768 function-body:
21769 compound_statement */
21771 static void
21772 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21774 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21775 ? BCS_TRY_BLOCK : BCS_NORMAL),
21776 true);
21779 /* Parse a ctor-initializer-opt followed by a function-body. Return
21780 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21781 is true we are parsing a function-try-block. */
21783 static void
21784 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21785 bool in_function_try_block)
21787 tree body, list;
21788 const bool check_body_p =
21789 DECL_CONSTRUCTOR_P (current_function_decl)
21790 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21791 tree last = NULL;
21793 /* Begin the function body. */
21794 body = begin_function_body ();
21795 /* Parse the optional ctor-initializer. */
21796 cp_parser_ctor_initializer_opt (parser);
21798 /* If we're parsing a constexpr constructor definition, we need
21799 to check that the constructor body is indeed empty. However,
21800 before we get to cp_parser_function_body lot of junk has been
21801 generated, so we can't just check that we have an empty block.
21802 Rather we take a snapshot of the outermost block, and check whether
21803 cp_parser_function_body changed its state. */
21804 if (check_body_p)
21806 list = cur_stmt_list;
21807 if (STATEMENT_LIST_TAIL (list))
21808 last = STATEMENT_LIST_TAIL (list)->stmt;
21810 /* Parse the function-body. */
21811 cp_parser_function_body (parser, in_function_try_block);
21812 if (check_body_p)
21813 check_constexpr_ctor_body (last, list, /*complain=*/true);
21814 /* Finish the function body. */
21815 finish_function_body (body);
21818 /* Parse an initializer.
21820 initializer:
21821 = initializer-clause
21822 ( expression-list )
21824 Returns an expression representing the initializer. If no
21825 initializer is present, NULL_TREE is returned.
21827 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21828 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21829 set to TRUE if there is no initializer present. If there is an
21830 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21831 is set to true; otherwise it is set to false. */
21833 static tree
21834 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21835 bool* non_constant_p)
21837 cp_token *token;
21838 tree init;
21840 /* Peek at the next token. */
21841 token = cp_lexer_peek_token (parser->lexer);
21843 /* Let our caller know whether or not this initializer was
21844 parenthesized. */
21845 *is_direct_init = (token->type != CPP_EQ);
21846 /* Assume that the initializer is constant. */
21847 *non_constant_p = false;
21849 if (token->type == CPP_EQ)
21851 /* Consume the `='. */
21852 cp_lexer_consume_token (parser->lexer);
21853 /* Parse the initializer-clause. */
21854 init = cp_parser_initializer_clause (parser, non_constant_p);
21856 else if (token->type == CPP_OPEN_PAREN)
21858 vec<tree, va_gc> *vec;
21859 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21860 /*cast_p=*/false,
21861 /*allow_expansion_p=*/true,
21862 non_constant_p);
21863 if (vec == NULL)
21864 return error_mark_node;
21865 init = build_tree_list_vec (vec);
21866 release_tree_vector (vec);
21868 else if (token->type == CPP_OPEN_BRACE)
21870 cp_lexer_set_source_position (parser->lexer);
21871 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21872 init = cp_parser_braced_list (parser, non_constant_p);
21873 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21875 else
21877 /* Anything else is an error. */
21878 cp_parser_error (parser, "expected initializer");
21879 init = error_mark_node;
21882 if (check_for_bare_parameter_packs (init))
21883 init = error_mark_node;
21885 return init;
21888 /* Parse an initializer-clause.
21890 initializer-clause:
21891 assignment-expression
21892 braced-init-list
21894 Returns an expression representing the initializer.
21896 If the `assignment-expression' production is used the value
21897 returned is simply a representation for the expression.
21899 Otherwise, calls cp_parser_braced_list. */
21901 static cp_expr
21902 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21904 cp_expr initializer;
21906 /* Assume the expression is constant. */
21907 *non_constant_p = false;
21909 /* If it is not a `{', then we are looking at an
21910 assignment-expression. */
21911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21913 initializer
21914 = cp_parser_constant_expression (parser,
21915 /*allow_non_constant_p=*/true,
21916 non_constant_p);
21918 else
21919 initializer = cp_parser_braced_list (parser, non_constant_p);
21921 return initializer;
21924 /* Parse a brace-enclosed initializer list.
21926 braced-init-list:
21927 { initializer-list , [opt] }
21928 { designated-initializer-list , [opt] }
21931 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21932 the elements of the initializer-list (or NULL, if the last
21933 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21934 NULL_TREE. There is no way to detect whether or not the optional
21935 trailing `,' was provided. NON_CONSTANT_P is as for
21936 cp_parser_initializer. */
21938 static cp_expr
21939 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21941 tree initializer;
21942 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21944 /* Consume the `{' token. */
21945 matching_braces braces;
21946 braces.require_open (parser);
21947 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21948 initializer = make_node (CONSTRUCTOR);
21949 /* If it's not a `}', then there is a non-trivial initializer. */
21950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21952 /* Parse the initializer list. */
21953 CONSTRUCTOR_ELTS (initializer)
21954 = cp_parser_initializer_list (parser, non_constant_p);
21955 /* A trailing `,' token is allowed. */
21956 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21957 cp_lexer_consume_token (parser->lexer);
21959 else
21960 *non_constant_p = false;
21961 /* Now, there should be a trailing `}'. */
21962 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21963 braces.require_close (parser);
21964 TREE_TYPE (initializer) = init_list_type_node;
21966 cp_expr result (initializer);
21967 /* Build a location of the form:
21968 { ... }
21969 ^~~~~~~
21970 with caret==start at the open brace, finish at the close brace. */
21971 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21972 result.set_location (combined_loc);
21973 return result;
21976 /* Consume tokens up to, and including, the next non-nested closing `]'.
21977 Returns true iff we found a closing `]'. */
21979 static bool
21980 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21982 unsigned square_depth = 0;
21984 while (true)
21986 cp_token * token = cp_lexer_peek_token (parser->lexer);
21988 switch (token->type)
21990 case CPP_EOF:
21991 case CPP_PRAGMA_EOL:
21992 /* If we've run out of tokens, then there is no closing `]'. */
21993 return false;
21995 case CPP_OPEN_SQUARE:
21996 ++square_depth;
21997 break;
21999 case CPP_CLOSE_SQUARE:
22000 if (!square_depth--)
22002 cp_lexer_consume_token (parser->lexer);
22003 return true;
22005 break;
22007 default:
22008 break;
22011 /* Consume the token. */
22012 cp_lexer_consume_token (parser->lexer);
22016 /* Return true if we are looking at an array-designator, false otherwise. */
22018 static bool
22019 cp_parser_array_designator_p (cp_parser *parser)
22021 /* Consume the `['. */
22022 cp_lexer_consume_token (parser->lexer);
22024 cp_lexer_save_tokens (parser->lexer);
22026 /* Skip tokens until the next token is a closing square bracket.
22027 If we find the closing `]', and the next token is a `=', then
22028 we are looking at an array designator. */
22029 bool array_designator_p
22030 = (cp_parser_skip_to_closing_square_bracket (parser)
22031 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22033 /* Roll back the tokens we skipped. */
22034 cp_lexer_rollback_tokens (parser->lexer);
22036 return array_designator_p;
22039 /* Parse an initializer-list.
22041 initializer-list:
22042 initializer-clause ... [opt]
22043 initializer-list , initializer-clause ... [opt]
22045 C++2A Extension:
22047 designated-initializer-list:
22048 designated-initializer-clause
22049 designated-initializer-list , designated-initializer-clause
22051 designated-initializer-clause:
22052 designator brace-or-equal-initializer
22054 designator:
22055 . identifier
22057 GNU Extension:
22059 initializer-list:
22060 designation initializer-clause ...[opt]
22061 initializer-list , designation initializer-clause ...[opt]
22063 designation:
22064 . identifier =
22065 identifier :
22066 [ constant-expression ] =
22068 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22069 for the initializer. If the INDEX of the elt is non-NULL, it is the
22070 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22071 as for cp_parser_initializer. */
22073 static vec<constructor_elt, va_gc> *
22074 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22076 vec<constructor_elt, va_gc> *v = NULL;
22077 bool first_p = true;
22078 tree first_designator = NULL_TREE;
22080 /* Assume all of the expressions are constant. */
22081 *non_constant_p = false;
22083 /* Parse the rest of the list. */
22084 while (true)
22086 cp_token *token;
22087 tree designator;
22088 tree initializer;
22089 bool clause_non_constant_p;
22090 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22092 /* Handle the C++2A syntax, '. id ='. */
22093 if ((cxx_dialect >= cxx2a
22094 || cp_parser_allow_gnu_extensions_p (parser))
22095 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22096 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22097 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22098 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22099 == CPP_OPEN_BRACE)))
22101 if (cxx_dialect < cxx2a)
22102 pedwarn (loc, OPT_Wpedantic,
22103 "C++ designated initializers only available with "
22104 "-std=c++2a or -std=gnu++2a");
22105 /* Consume the `.'. */
22106 cp_lexer_consume_token (parser->lexer);
22107 /* Consume the identifier. */
22108 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22109 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22110 /* Consume the `='. */
22111 cp_lexer_consume_token (parser->lexer);
22113 /* Also, if the next token is an identifier and the following one is a
22114 colon, we are looking at the GNU designated-initializer
22115 syntax. */
22116 else if (cp_parser_allow_gnu_extensions_p (parser)
22117 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22118 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22119 == CPP_COLON))
22121 /* Warn the user that they are using an extension. */
22122 pedwarn (loc, OPT_Wpedantic,
22123 "ISO C++ does not allow GNU designated initializers");
22124 /* Consume the identifier. */
22125 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22126 /* Consume the `:'. */
22127 cp_lexer_consume_token (parser->lexer);
22129 /* Also handle C99 array designators, '[ const ] ='. */
22130 else if (cp_parser_allow_gnu_extensions_p (parser)
22131 && !c_dialect_objc ()
22132 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22134 /* In C++11, [ could start a lambda-introducer. */
22135 bool non_const = false;
22137 cp_parser_parse_tentatively (parser);
22139 if (!cp_parser_array_designator_p (parser))
22141 cp_parser_simulate_error (parser);
22142 designator = NULL_TREE;
22144 else
22146 designator = cp_parser_constant_expression (parser, true,
22147 &non_const);
22148 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22149 cp_parser_require (parser, CPP_EQ, RT_EQ);
22152 if (!cp_parser_parse_definitely (parser))
22153 designator = NULL_TREE;
22154 else if (non_const
22155 && (!require_potential_rvalue_constant_expression
22156 (designator)))
22157 designator = NULL_TREE;
22158 if (designator)
22159 /* Warn the user that they are using an extension. */
22160 pedwarn (loc, OPT_Wpedantic,
22161 "ISO C++ does not allow C99 designated initializers");
22163 else
22164 designator = NULL_TREE;
22166 if (first_p)
22168 first_designator = designator;
22169 first_p = false;
22171 else if (cxx_dialect >= cxx2a
22172 && first_designator != error_mark_node
22173 && (!first_designator != !designator))
22175 error_at (loc, "either all initializer clauses should be designated "
22176 "or none of them should be");
22177 first_designator = error_mark_node;
22179 else if (cxx_dialect < cxx2a && !first_designator)
22180 first_designator = designator;
22182 /* Parse the initializer. */
22183 initializer = cp_parser_initializer_clause (parser,
22184 &clause_non_constant_p);
22185 /* If any clause is non-constant, so is the entire initializer. */
22186 if (clause_non_constant_p)
22187 *non_constant_p = true;
22189 /* If we have an ellipsis, this is an initializer pack
22190 expansion. */
22191 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22193 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22195 /* Consume the `...'. */
22196 cp_lexer_consume_token (parser->lexer);
22198 if (designator && cxx_dialect >= cxx2a)
22199 error_at (loc,
22200 "%<...%> not allowed in designated initializer list");
22202 /* Turn the initializer into an initializer expansion. */
22203 initializer = make_pack_expansion (initializer);
22206 /* Add it to the vector. */
22207 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22209 /* If the next token is not a comma, we have reached the end of
22210 the list. */
22211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22212 break;
22214 /* Peek at the next token. */
22215 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22216 /* If the next token is a `}', then we're still done. An
22217 initializer-clause can have a trailing `,' after the
22218 initializer-list and before the closing `}'. */
22219 if (token->type == CPP_CLOSE_BRACE)
22220 break;
22222 /* Consume the `,' token. */
22223 cp_lexer_consume_token (parser->lexer);
22226 /* The same identifier shall not appear in multiple designators
22227 of a designated-initializer-list. */
22228 if (first_designator)
22230 unsigned int i;
22231 tree designator, val;
22232 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22233 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22235 if (IDENTIFIER_MARKED (designator))
22237 error_at (EXPR_LOC_OR_LOC (val, input_location),
22238 "%<.%s%> designator used multiple times in "
22239 "the same initializer list",
22240 IDENTIFIER_POINTER (designator));
22241 (*v)[i].index = NULL_TREE;
22243 else
22244 IDENTIFIER_MARKED (designator) = 1;
22246 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22247 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22248 IDENTIFIER_MARKED (designator) = 0;
22251 return v;
22254 /* Classes [gram.class] */
22256 /* Parse a class-name.
22258 class-name:
22259 identifier
22260 template-id
22262 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22263 to indicate that names looked up in dependent types should be
22264 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22265 keyword has been used to indicate that the name that appears next
22266 is a template. TAG_TYPE indicates the explicit tag given before
22267 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22268 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22269 is the class being defined in a class-head. If ENUM_OK is TRUE,
22270 enum-names are also accepted.
22272 Returns the TYPE_DECL representing the class. */
22274 static tree
22275 cp_parser_class_name (cp_parser *parser,
22276 bool typename_keyword_p,
22277 bool template_keyword_p,
22278 enum tag_types tag_type,
22279 bool check_dependency_p,
22280 bool class_head_p,
22281 bool is_declaration,
22282 bool enum_ok)
22284 tree decl;
22285 tree scope;
22286 bool typename_p;
22287 cp_token *token;
22288 tree identifier = NULL_TREE;
22290 /* All class-names start with an identifier. */
22291 token = cp_lexer_peek_token (parser->lexer);
22292 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22294 cp_parser_error (parser, "expected class-name");
22295 return error_mark_node;
22298 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22299 to a template-id, so we save it here. */
22300 scope = parser->scope;
22301 if (scope == error_mark_node)
22302 return error_mark_node;
22304 /* Any name names a type if we're following the `typename' keyword
22305 in a qualified name where the enclosing scope is type-dependent. */
22306 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22307 && dependent_type_p (scope));
22308 /* Handle the common case (an identifier, but not a template-id)
22309 efficiently. */
22310 if (token->type == CPP_NAME
22311 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22313 cp_token *identifier_token;
22314 bool ambiguous_p;
22316 /* Look for the identifier. */
22317 identifier_token = cp_lexer_peek_token (parser->lexer);
22318 ambiguous_p = identifier_token->error_reported;
22319 identifier = cp_parser_identifier (parser);
22320 /* If the next token isn't an identifier, we are certainly not
22321 looking at a class-name. */
22322 if (identifier == error_mark_node)
22323 decl = error_mark_node;
22324 /* If we know this is a type-name, there's no need to look it
22325 up. */
22326 else if (typename_p)
22327 decl = identifier;
22328 else
22330 tree ambiguous_decls;
22331 /* If we already know that this lookup is ambiguous, then
22332 we've already issued an error message; there's no reason
22333 to check again. */
22334 if (ambiguous_p)
22336 cp_parser_simulate_error (parser);
22337 return error_mark_node;
22339 /* If the next token is a `::', then the name must be a type
22340 name.
22342 [basic.lookup.qual]
22344 During the lookup for a name preceding the :: scope
22345 resolution operator, object, function, and enumerator
22346 names are ignored. */
22347 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22348 tag_type = scope_type;
22349 /* Look up the name. */
22350 decl = cp_parser_lookup_name (parser, identifier,
22351 tag_type,
22352 /*is_template=*/false,
22353 /*is_namespace=*/false,
22354 check_dependency_p,
22355 &ambiguous_decls,
22356 identifier_token->location);
22357 if (ambiguous_decls)
22359 if (cp_parser_parsing_tentatively (parser))
22360 cp_parser_simulate_error (parser);
22361 return error_mark_node;
22365 else
22367 /* Try a template-id. */
22368 decl = cp_parser_template_id (parser, template_keyword_p,
22369 check_dependency_p,
22370 tag_type,
22371 is_declaration);
22372 if (decl == error_mark_node)
22373 return error_mark_node;
22376 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22378 /* If this is a typename, create a TYPENAME_TYPE. */
22379 if (typename_p && decl != error_mark_node)
22381 decl = make_typename_type (scope, decl, typename_type,
22382 /*complain=*/tf_error);
22383 if (decl != error_mark_node)
22384 decl = TYPE_NAME (decl);
22387 decl = strip_using_decl (decl);
22389 /* Check to see that it is really the name of a class. */
22390 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22391 && identifier_p (TREE_OPERAND (decl, 0))
22392 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22393 /* Situations like this:
22395 template <typename T> struct A {
22396 typename T::template X<int>::I i;
22399 are problematic. Is `T::template X<int>' a class-name? The
22400 standard does not seem to be definitive, but there is no other
22401 valid interpretation of the following `::'. Therefore, those
22402 names are considered class-names. */
22404 decl = make_typename_type (scope, decl, tag_type, tf_error);
22405 if (decl != error_mark_node)
22406 decl = TYPE_NAME (decl);
22408 else if (TREE_CODE (decl) != TYPE_DECL
22409 || TREE_TYPE (decl) == error_mark_node
22410 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22411 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22412 /* In Objective-C 2.0, a classname followed by '.' starts a
22413 dot-syntax expression, and it's not a type-name. */
22414 || (c_dialect_objc ()
22415 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22416 && objc_is_class_name (decl)))
22417 decl = error_mark_node;
22419 if (decl == error_mark_node)
22420 cp_parser_error (parser, "expected class-name");
22421 else if (identifier && !parser->scope)
22422 maybe_note_name_used_in_class (identifier, decl);
22424 return decl;
22427 /* Parse a class-specifier.
22429 class-specifier:
22430 class-head { member-specification [opt] }
22432 Returns the TREE_TYPE representing the class. */
22434 static tree
22435 cp_parser_class_specifier_1 (cp_parser* parser)
22437 tree type;
22438 tree attributes = NULL_TREE;
22439 bool nested_name_specifier_p;
22440 unsigned saved_num_template_parameter_lists;
22441 bool saved_in_function_body;
22442 unsigned char in_statement;
22443 bool in_switch_statement_p;
22444 bool saved_in_unbraced_linkage_specification_p;
22445 tree old_scope = NULL_TREE;
22446 tree scope = NULL_TREE;
22447 cp_token *closing_brace;
22449 push_deferring_access_checks (dk_no_deferred);
22451 /* Parse the class-head. */
22452 type = cp_parser_class_head (parser,
22453 &nested_name_specifier_p);
22454 /* If the class-head was a semantic disaster, skip the entire body
22455 of the class. */
22456 if (!type)
22458 cp_parser_skip_to_end_of_block_or_statement (parser);
22459 pop_deferring_access_checks ();
22460 return error_mark_node;
22463 /* Look for the `{'. */
22464 matching_braces braces;
22465 if (!braces.require_open (parser))
22467 pop_deferring_access_checks ();
22468 return error_mark_node;
22471 cp_ensure_no_omp_declare_simd (parser);
22472 cp_ensure_no_oacc_routine (parser);
22474 /* Issue an error message if type-definitions are forbidden here. */
22475 cp_parser_check_type_definition (parser);
22476 /* Remember that we are defining one more class. */
22477 ++parser->num_classes_being_defined;
22478 /* Inside the class, surrounding template-parameter-lists do not
22479 apply. */
22480 saved_num_template_parameter_lists
22481 = parser->num_template_parameter_lists;
22482 parser->num_template_parameter_lists = 0;
22483 /* We are not in a function body. */
22484 saved_in_function_body = parser->in_function_body;
22485 parser->in_function_body = false;
22486 /* Or in a loop. */
22487 in_statement = parser->in_statement;
22488 parser->in_statement = 0;
22489 /* Or in a switch. */
22490 in_switch_statement_p = parser->in_switch_statement_p;
22491 parser->in_switch_statement_p = false;
22492 /* We are not immediately inside an extern "lang" block. */
22493 saved_in_unbraced_linkage_specification_p
22494 = parser->in_unbraced_linkage_specification_p;
22495 parser->in_unbraced_linkage_specification_p = false;
22497 // Associate constraints with the type.
22498 if (flag_concepts)
22499 type = associate_classtype_constraints (type);
22501 /* Start the class. */
22502 if (nested_name_specifier_p)
22504 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22505 old_scope = push_inner_scope (scope);
22507 type = begin_class_definition (type);
22509 if (type == error_mark_node)
22510 /* If the type is erroneous, skip the entire body of the class. */
22511 cp_parser_skip_to_closing_brace (parser);
22512 else
22513 /* Parse the member-specification. */
22514 cp_parser_member_specification_opt (parser);
22516 /* Look for the trailing `}'. */
22517 closing_brace = braces.require_close (parser);
22518 /* Look for trailing attributes to apply to this class. */
22519 if (cp_parser_allow_gnu_extensions_p (parser))
22520 attributes = cp_parser_gnu_attributes_opt (parser);
22521 if (type != error_mark_node)
22522 type = finish_struct (type, attributes);
22523 if (nested_name_specifier_p)
22524 pop_inner_scope (old_scope, scope);
22526 /* We've finished a type definition. Check for the common syntax
22527 error of forgetting a semicolon after the definition. We need to
22528 be careful, as we can't just check for not-a-semicolon and be done
22529 with it; the user might have typed:
22531 class X { } c = ...;
22532 class X { } *p = ...;
22534 and so forth. Instead, enumerate all the possible tokens that
22535 might follow this production; if we don't see one of them, then
22536 complain and silently insert the semicolon. */
22538 cp_token *token = cp_lexer_peek_token (parser->lexer);
22539 bool want_semicolon = true;
22541 if (cp_next_tokens_can_be_std_attribute_p (parser))
22542 /* Don't try to parse c++11 attributes here. As per the
22543 grammar, that should be a task for
22544 cp_parser_decl_specifier_seq. */
22545 want_semicolon = false;
22547 switch (token->type)
22549 case CPP_NAME:
22550 case CPP_SEMICOLON:
22551 case CPP_MULT:
22552 case CPP_AND:
22553 case CPP_OPEN_PAREN:
22554 case CPP_CLOSE_PAREN:
22555 case CPP_COMMA:
22556 want_semicolon = false;
22557 break;
22559 /* While it's legal for type qualifiers and storage class
22560 specifiers to follow type definitions in the grammar, only
22561 compiler testsuites contain code like that. Assume that if
22562 we see such code, then what we're really seeing is a case
22563 like:
22565 class X { }
22566 const <type> var = ...;
22570 class Y { }
22571 static <type> func (...) ...
22573 i.e. the qualifier or specifier applies to the next
22574 declaration. To do so, however, we need to look ahead one
22575 more token to see if *that* token is a type specifier.
22577 This code could be improved to handle:
22579 class Z { }
22580 static const <type> var = ...; */
22581 case CPP_KEYWORD:
22582 if (keyword_is_decl_specifier (token->keyword))
22584 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22586 /* Handling user-defined types here would be nice, but very
22587 tricky. */
22588 want_semicolon
22589 = (lookahead->type == CPP_KEYWORD
22590 && keyword_begins_type_specifier (lookahead->keyword));
22592 break;
22593 default:
22594 break;
22597 /* If we don't have a type, then something is very wrong and we
22598 shouldn't try to do anything clever. Likewise for not seeing the
22599 closing brace. */
22600 if (closing_brace && TYPE_P (type) && want_semicolon)
22602 /* Locate the closing brace. */
22603 cp_token_position prev
22604 = cp_lexer_previous_token_position (parser->lexer);
22605 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22606 location_t loc = prev_token->location;
22608 /* We want to suggest insertion of a ';' immediately *after* the
22609 closing brace, so, if we can, offset the location by 1 column. */
22610 location_t next_loc = loc;
22611 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22612 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22614 rich_location richloc (line_table, next_loc);
22616 /* If we successfully offset the location, suggest the fix-it. */
22617 if (next_loc != loc)
22618 richloc.add_fixit_insert_before (next_loc, ";");
22620 if (CLASSTYPE_DECLARED_CLASS (type))
22621 error_at (&richloc,
22622 "expected %<;%> after class definition");
22623 else if (TREE_CODE (type) == RECORD_TYPE)
22624 error_at (&richloc,
22625 "expected %<;%> after struct definition");
22626 else if (TREE_CODE (type) == UNION_TYPE)
22627 error_at (&richloc,
22628 "expected %<;%> after union definition");
22629 else
22630 gcc_unreachable ();
22632 /* Unget one token and smash it to look as though we encountered
22633 a semicolon in the input stream. */
22634 cp_lexer_set_token_position (parser->lexer, prev);
22635 token = cp_lexer_peek_token (parser->lexer);
22636 token->type = CPP_SEMICOLON;
22637 token->keyword = RID_MAX;
22641 /* If this class is not itself within the scope of another class,
22642 then we need to parse the bodies of all of the queued function
22643 definitions. Note that the queued functions defined in a class
22644 are not always processed immediately following the
22645 class-specifier for that class. Consider:
22647 struct A {
22648 struct B { void f() { sizeof (A); } };
22651 If `f' were processed before the processing of `A' were
22652 completed, there would be no way to compute the size of `A'.
22653 Note that the nesting we are interested in here is lexical --
22654 not the semantic nesting given by TYPE_CONTEXT. In particular,
22655 for:
22657 struct A { struct B; };
22658 struct A::B { void f() { } };
22660 there is no need to delay the parsing of `A::B::f'. */
22661 if (--parser->num_classes_being_defined == 0)
22663 tree decl;
22664 tree class_type = NULL_TREE;
22665 tree pushed_scope = NULL_TREE;
22666 unsigned ix;
22667 cp_default_arg_entry *e;
22668 tree save_ccp, save_ccr;
22670 /* In a first pass, parse default arguments to the functions.
22671 Then, in a second pass, parse the bodies of the functions.
22672 This two-phased approach handles cases like:
22674 struct S {
22675 void f() { g(); }
22676 void g(int i = 3);
22680 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22682 decl = e->decl;
22683 /* If there are default arguments that have not yet been processed,
22684 take care of them now. */
22685 if (class_type != e->class_type)
22687 if (pushed_scope)
22688 pop_scope (pushed_scope);
22689 class_type = e->class_type;
22690 pushed_scope = push_scope (class_type);
22692 /* Make sure that any template parameters are in scope. */
22693 maybe_begin_member_template_processing (decl);
22694 /* Parse the default argument expressions. */
22695 cp_parser_late_parsing_default_args (parser, decl);
22696 /* Remove any template parameters from the symbol table. */
22697 maybe_end_member_template_processing ();
22699 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22700 /* Now parse any NSDMIs. */
22701 save_ccp = current_class_ptr;
22702 save_ccr = current_class_ref;
22703 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22705 if (class_type != DECL_CONTEXT (decl))
22707 if (pushed_scope)
22708 pop_scope (pushed_scope);
22709 class_type = DECL_CONTEXT (decl);
22710 pushed_scope = push_scope (class_type);
22712 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22713 cp_parser_late_parsing_nsdmi (parser, decl);
22715 vec_safe_truncate (unparsed_nsdmis, 0);
22716 current_class_ptr = save_ccp;
22717 current_class_ref = save_ccr;
22718 if (pushed_scope)
22719 pop_scope (pushed_scope);
22721 /* Now do some post-NSDMI bookkeeping. */
22722 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22723 after_nsdmi_defaulted_late_checks (class_type);
22724 vec_safe_truncate (unparsed_classes, 0);
22725 after_nsdmi_defaulted_late_checks (type);
22727 /* Now parse the body of the functions. */
22728 if (flag_openmp)
22730 /* OpenMP UDRs need to be parsed before all other functions. */
22731 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22732 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22733 cp_parser_late_parsing_for_member (parser, decl);
22734 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22735 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22736 cp_parser_late_parsing_for_member (parser, decl);
22738 else
22739 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22740 cp_parser_late_parsing_for_member (parser, decl);
22741 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22743 else
22744 vec_safe_push (unparsed_classes, type);
22746 /* Put back any saved access checks. */
22747 pop_deferring_access_checks ();
22749 /* Restore saved state. */
22750 parser->in_switch_statement_p = in_switch_statement_p;
22751 parser->in_statement = in_statement;
22752 parser->in_function_body = saved_in_function_body;
22753 parser->num_template_parameter_lists
22754 = saved_num_template_parameter_lists;
22755 parser->in_unbraced_linkage_specification_p
22756 = saved_in_unbraced_linkage_specification_p;
22758 return type;
22761 static tree
22762 cp_parser_class_specifier (cp_parser* parser)
22764 tree ret;
22765 timevar_push (TV_PARSE_STRUCT);
22766 ret = cp_parser_class_specifier_1 (parser);
22767 timevar_pop (TV_PARSE_STRUCT);
22768 return ret;
22771 /* Parse a class-head.
22773 class-head:
22774 class-key identifier [opt] base-clause [opt]
22775 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22776 class-key nested-name-specifier [opt] template-id
22777 base-clause [opt]
22779 class-virt-specifier:
22780 final
22782 GNU Extensions:
22783 class-key attributes identifier [opt] base-clause [opt]
22784 class-key attributes nested-name-specifier identifier base-clause [opt]
22785 class-key attributes nested-name-specifier [opt] template-id
22786 base-clause [opt]
22788 Upon return BASES is initialized to the list of base classes (or
22789 NULL, if there are none) in the same form returned by
22790 cp_parser_base_clause.
22792 Returns the TYPE of the indicated class. Sets
22793 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22794 involving a nested-name-specifier was used, and FALSE otherwise.
22796 Returns error_mark_node if this is not a class-head.
22798 Returns NULL_TREE if the class-head is syntactically valid, but
22799 semantically invalid in a way that means we should skip the entire
22800 body of the class. */
22802 static tree
22803 cp_parser_class_head (cp_parser* parser,
22804 bool* nested_name_specifier_p)
22806 tree nested_name_specifier;
22807 enum tag_types class_key;
22808 tree id = NULL_TREE;
22809 tree type = NULL_TREE;
22810 tree attributes;
22811 tree bases;
22812 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22813 bool template_id_p = false;
22814 bool qualified_p = false;
22815 bool invalid_nested_name_p = false;
22816 bool invalid_explicit_specialization_p = false;
22817 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22818 tree pushed_scope = NULL_TREE;
22819 unsigned num_templates;
22820 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22821 /* Assume no nested-name-specifier will be present. */
22822 *nested_name_specifier_p = false;
22823 /* Assume no template parameter lists will be used in defining the
22824 type. */
22825 num_templates = 0;
22826 parser->colon_corrects_to_scope_p = false;
22828 /* Look for the class-key. */
22829 class_key = cp_parser_class_key (parser);
22830 if (class_key == none_type)
22831 return error_mark_node;
22833 location_t class_head_start_location = input_location;
22835 /* Parse the attributes. */
22836 attributes = cp_parser_attributes_opt (parser);
22838 /* If the next token is `::', that is invalid -- but sometimes
22839 people do try to write:
22841 struct ::S {};
22843 Handle this gracefully by accepting the extra qualifier, and then
22844 issuing an error about it later if this really is a
22845 class-head. If it turns out just to be an elaborated type
22846 specifier, remain silent. */
22847 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22848 qualified_p = true;
22850 push_deferring_access_checks (dk_no_check);
22852 /* Determine the name of the class. Begin by looking for an
22853 optional nested-name-specifier. */
22854 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22855 nested_name_specifier
22856 = cp_parser_nested_name_specifier_opt (parser,
22857 /*typename_keyword_p=*/false,
22858 /*check_dependency_p=*/false,
22859 /*type_p=*/true,
22860 /*is_declaration=*/false);
22861 /* If there was a nested-name-specifier, then there *must* be an
22862 identifier. */
22864 cp_token *bad_template_keyword = NULL;
22866 if (nested_name_specifier)
22868 type_start_token = cp_lexer_peek_token (parser->lexer);
22869 /* Although the grammar says `identifier', it really means
22870 `class-name' or `template-name'. You are only allowed to
22871 define a class that has already been declared with this
22872 syntax.
22874 The proposed resolution for Core Issue 180 says that wherever
22875 you see `class T::X' you should treat `X' as a type-name.
22877 It is OK to define an inaccessible class; for example:
22879 class A { class B; };
22880 class A::B {};
22882 We do not know if we will see a class-name, or a
22883 template-name. We look for a class-name first, in case the
22884 class-name is a template-id; if we looked for the
22885 template-name first we would stop after the template-name. */
22886 cp_parser_parse_tentatively (parser);
22887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22888 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22889 type = cp_parser_class_name (parser,
22890 /*typename_keyword_p=*/false,
22891 /*template_keyword_p=*/false,
22892 class_type,
22893 /*check_dependency_p=*/false,
22894 /*class_head_p=*/true,
22895 /*is_declaration=*/false);
22896 /* If that didn't work, ignore the nested-name-specifier. */
22897 if (!cp_parser_parse_definitely (parser))
22899 invalid_nested_name_p = true;
22900 type_start_token = cp_lexer_peek_token (parser->lexer);
22901 id = cp_parser_identifier (parser);
22902 if (id == error_mark_node)
22903 id = NULL_TREE;
22905 /* If we could not find a corresponding TYPE, treat this
22906 declaration like an unqualified declaration. */
22907 if (type == error_mark_node)
22908 nested_name_specifier = NULL_TREE;
22909 /* Otherwise, count the number of templates used in TYPE and its
22910 containing scopes. */
22911 else
22913 tree scope;
22915 for (scope = TREE_TYPE (type);
22916 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22917 scope = get_containing_scope (scope))
22918 if (TYPE_P (scope)
22919 && CLASS_TYPE_P (scope)
22920 && CLASSTYPE_TEMPLATE_INFO (scope)
22921 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22922 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22923 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22924 ++num_templates;
22927 /* Otherwise, the identifier is optional. */
22928 else
22930 /* We don't know whether what comes next is a template-id,
22931 an identifier, or nothing at all. */
22932 cp_parser_parse_tentatively (parser);
22933 /* Check for a template-id. */
22934 type_start_token = cp_lexer_peek_token (parser->lexer);
22935 id = cp_parser_template_id (parser,
22936 /*template_keyword_p=*/false,
22937 /*check_dependency_p=*/true,
22938 class_key,
22939 /*is_declaration=*/true);
22940 /* If that didn't work, it could still be an identifier. */
22941 if (!cp_parser_parse_definitely (parser))
22943 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22945 type_start_token = cp_lexer_peek_token (parser->lexer);
22946 id = cp_parser_identifier (parser);
22948 else
22949 id = NULL_TREE;
22951 else
22953 template_id_p = true;
22954 ++num_templates;
22958 pop_deferring_access_checks ();
22960 if (id)
22962 cp_parser_check_for_invalid_template_id (parser, id,
22963 class_key,
22964 type_start_token->location);
22966 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22968 /* If it's not a `:' or a `{' then we can't really be looking at a
22969 class-head, since a class-head only appears as part of a
22970 class-specifier. We have to detect this situation before calling
22971 xref_tag, since that has irreversible side-effects. */
22972 if (!cp_parser_next_token_starts_class_definition_p (parser))
22974 cp_parser_error (parser, "expected %<{%> or %<:%>");
22975 type = error_mark_node;
22976 goto out;
22979 /* At this point, we're going ahead with the class-specifier, even
22980 if some other problem occurs. */
22981 cp_parser_commit_to_tentative_parse (parser);
22982 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22984 cp_parser_error (parser,
22985 "cannot specify %<override%> for a class");
22986 type = error_mark_node;
22987 goto out;
22989 /* Issue the error about the overly-qualified name now. */
22990 if (qualified_p)
22992 cp_parser_error (parser,
22993 "global qualification of class name is invalid");
22994 type = error_mark_node;
22995 goto out;
22997 else if (invalid_nested_name_p)
22999 cp_parser_error (parser,
23000 "qualified name does not name a class");
23001 type = error_mark_node;
23002 goto out;
23004 else if (nested_name_specifier)
23006 tree scope;
23008 if (bad_template_keyword)
23009 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23010 keyword template shall not appear at the top level. */
23011 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23012 "keyword %<template%> not allowed in class-head-name");
23014 /* Reject typedef-names in class heads. */
23015 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23017 error_at (type_start_token->location,
23018 "invalid class name in declaration of %qD",
23019 type);
23020 type = NULL_TREE;
23021 goto done;
23024 /* Figure out in what scope the declaration is being placed. */
23025 scope = current_scope ();
23026 /* If that scope does not contain the scope in which the
23027 class was originally declared, the program is invalid. */
23028 if (scope && !is_ancestor (scope, nested_name_specifier))
23030 if (at_namespace_scope_p ())
23031 error_at (type_start_token->location,
23032 "declaration of %qD in namespace %qD which does not "
23033 "enclose %qD",
23034 type, scope, nested_name_specifier);
23035 else
23036 error_at (type_start_token->location,
23037 "declaration of %qD in %qD which does not enclose %qD",
23038 type, scope, nested_name_specifier);
23039 type = NULL_TREE;
23040 goto done;
23042 /* [dcl.meaning]
23044 A declarator-id shall not be qualified except for the
23045 definition of a ... nested class outside of its class
23046 ... [or] the definition or explicit instantiation of a
23047 class member of a namespace outside of its namespace. */
23048 if (scope == nested_name_specifier)
23050 permerror (nested_name_specifier_token_start->location,
23051 "extra qualification not allowed");
23052 nested_name_specifier = NULL_TREE;
23053 num_templates = 0;
23056 /* An explicit-specialization must be preceded by "template <>". If
23057 it is not, try to recover gracefully. */
23058 if (at_namespace_scope_p ()
23059 && parser->num_template_parameter_lists == 0
23060 && !processing_template_parmlist
23061 && template_id_p)
23063 /* Build a location of this form:
23064 struct typename <ARGS>
23065 ^~~~~~~~~~~~~~~~~~~~~~
23066 with caret==start at the start token, and
23067 finishing at the end of the type. */
23068 location_t reported_loc
23069 = make_location (class_head_start_location,
23070 class_head_start_location,
23071 get_finish (type_start_token->location));
23072 rich_location richloc (line_table, reported_loc);
23073 richloc.add_fixit_insert_before (class_head_start_location,
23074 "template <> ");
23075 error_at (&richloc,
23076 "an explicit specialization must be preceded by"
23077 " %<template <>%>");
23078 invalid_explicit_specialization_p = true;
23079 /* Take the same action that would have been taken by
23080 cp_parser_explicit_specialization. */
23081 ++parser->num_template_parameter_lists;
23082 begin_specialization ();
23084 /* There must be no "return" statements between this point and the
23085 end of this function; set "type "to the correct return value and
23086 use "goto done;" to return. */
23087 /* Make sure that the right number of template parameters were
23088 present. */
23089 if (!cp_parser_check_template_parameters (parser, num_templates,
23090 type_start_token->location,
23091 /*declarator=*/NULL))
23093 /* If something went wrong, there is no point in even trying to
23094 process the class-definition. */
23095 type = NULL_TREE;
23096 goto done;
23099 /* Look up the type. */
23100 if (template_id_p)
23102 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23103 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23104 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23106 error_at (type_start_token->location,
23107 "function template %qD redeclared as a class template", id);
23108 type = error_mark_node;
23110 else
23112 type = TREE_TYPE (id);
23113 type = maybe_process_partial_specialization (type);
23115 /* Check the scope while we still know whether or not we had a
23116 nested-name-specifier. */
23117 if (type != error_mark_node)
23118 check_unqualified_spec_or_inst (type, type_start_token->location);
23120 if (nested_name_specifier)
23121 pushed_scope = push_scope (nested_name_specifier);
23123 else if (nested_name_specifier)
23125 tree class_type;
23127 /* Given:
23129 template <typename T> struct S { struct T };
23130 template <typename T> struct S<T>::T { };
23132 we will get a TYPENAME_TYPE when processing the definition of
23133 `S::T'. We need to resolve it to the actual type before we
23134 try to define it. */
23135 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23137 class_type = resolve_typename_type (TREE_TYPE (type),
23138 /*only_current_p=*/false);
23139 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23140 type = TYPE_NAME (class_type);
23141 else
23143 cp_parser_error (parser, "could not resolve typename type");
23144 type = error_mark_node;
23148 if (maybe_process_partial_specialization (TREE_TYPE (type))
23149 == error_mark_node)
23151 type = NULL_TREE;
23152 goto done;
23155 class_type = current_class_type;
23156 /* Enter the scope indicated by the nested-name-specifier. */
23157 pushed_scope = push_scope (nested_name_specifier);
23158 /* Get the canonical version of this type. */
23159 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23160 /* Call push_template_decl if it seems like we should be defining a
23161 template either from the template headers or the type we're
23162 defining, so that we diagnose both extra and missing headers. */
23163 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23164 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23165 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23167 type = push_template_decl (type);
23168 if (type == error_mark_node)
23170 type = NULL_TREE;
23171 goto done;
23175 type = TREE_TYPE (type);
23176 *nested_name_specifier_p = true;
23178 else /* The name is not a nested name. */
23180 /* If the class was unnamed, create a dummy name. */
23181 if (!id)
23182 id = make_anon_name ();
23183 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23184 ? ts_within_enclosing_non_class
23185 : ts_current);
23186 type = xref_tag (class_key, id, tag_scope,
23187 parser->num_template_parameter_lists);
23190 /* Indicate whether this class was declared as a `class' or as a
23191 `struct'. */
23192 if (TREE_CODE (type) == RECORD_TYPE)
23193 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23194 cp_parser_check_class_key (class_key, type);
23196 /* If this type was already complete, and we see another definition,
23197 that's an error. */
23198 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23200 error_at (type_start_token->location, "redefinition of %q#T",
23201 type);
23202 inform (location_of (type), "previous definition of %q#T",
23203 type);
23204 type = NULL_TREE;
23205 goto done;
23207 else if (type == error_mark_node)
23208 type = NULL_TREE;
23210 if (type)
23212 /* Apply attributes now, before any use of the class as a template
23213 argument in its base list. */
23214 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23215 fixup_attribute_variants (type);
23218 /* We will have entered the scope containing the class; the names of
23219 base classes should be looked up in that context. For example:
23221 struct A { struct B {}; struct C; };
23222 struct A::C : B {};
23224 is valid. */
23226 /* Get the list of base-classes, if there is one. */
23227 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23229 /* PR59482: enter the class scope so that base-specifiers are looked
23230 up correctly. */
23231 if (type)
23232 pushclass (type);
23233 bases = cp_parser_base_clause (parser);
23234 /* PR59482: get out of the previously pushed class scope so that the
23235 subsequent pops pop the right thing. */
23236 if (type)
23237 popclass ();
23239 else
23240 bases = NULL_TREE;
23242 /* If we're really defining a class, process the base classes.
23243 If they're invalid, fail. */
23244 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23245 xref_basetypes (type, bases);
23247 done:
23248 /* Leave the scope given by the nested-name-specifier. We will
23249 enter the class scope itself while processing the members. */
23250 if (pushed_scope)
23251 pop_scope (pushed_scope);
23253 if (invalid_explicit_specialization_p)
23255 end_specialization ();
23256 --parser->num_template_parameter_lists;
23259 if (type)
23260 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23261 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23262 CLASSTYPE_FINAL (type) = 1;
23263 out:
23264 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23265 return type;
23268 /* Parse a class-key.
23270 class-key:
23271 class
23272 struct
23273 union
23275 Returns the kind of class-key specified, or none_type to indicate
23276 error. */
23278 static enum tag_types
23279 cp_parser_class_key (cp_parser* parser)
23281 cp_token *token;
23282 enum tag_types tag_type;
23284 /* Look for the class-key. */
23285 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23286 if (!token)
23287 return none_type;
23289 /* Check to see if the TOKEN is a class-key. */
23290 tag_type = cp_parser_token_is_class_key (token);
23291 if (!tag_type)
23292 cp_parser_error (parser, "expected class-key");
23293 return tag_type;
23296 /* Parse a type-parameter-key.
23298 type-parameter-key:
23299 class
23300 typename
23303 static void
23304 cp_parser_type_parameter_key (cp_parser* parser)
23306 /* Look for the type-parameter-key. */
23307 enum tag_types tag_type = none_type;
23308 cp_token *token = cp_lexer_peek_token (parser->lexer);
23309 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23311 cp_lexer_consume_token (parser->lexer);
23312 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23313 /* typename is not allowed in a template template parameter
23314 by the standard until C++17. */
23315 pedwarn (token->location, OPT_Wpedantic,
23316 "ISO C++ forbids typename key in template template parameter;"
23317 " use -std=c++17 or -std=gnu++17");
23319 else
23320 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23322 return;
23325 /* Parse an (optional) member-specification.
23327 member-specification:
23328 member-declaration member-specification [opt]
23329 access-specifier : member-specification [opt] */
23331 static void
23332 cp_parser_member_specification_opt (cp_parser* parser)
23334 while (true)
23336 cp_token *token;
23337 enum rid keyword;
23339 /* Peek at the next token. */
23340 token = cp_lexer_peek_token (parser->lexer);
23341 /* If it's a `}', or EOF then we've seen all the members. */
23342 if (token->type == CPP_CLOSE_BRACE
23343 || token->type == CPP_EOF
23344 || token->type == CPP_PRAGMA_EOL)
23345 break;
23347 /* See if this token is a keyword. */
23348 keyword = token->keyword;
23349 switch (keyword)
23351 case RID_PUBLIC:
23352 case RID_PROTECTED:
23353 case RID_PRIVATE:
23354 /* Consume the access-specifier. */
23355 cp_lexer_consume_token (parser->lexer);
23356 /* Remember which access-specifier is active. */
23357 current_access_specifier = token->u.value;
23358 /* Look for the `:'. */
23359 cp_parser_require (parser, CPP_COLON, RT_COLON);
23360 break;
23362 default:
23363 /* Accept #pragmas at class scope. */
23364 if (token->type == CPP_PRAGMA)
23366 cp_parser_pragma (parser, pragma_member, NULL);
23367 break;
23370 /* Otherwise, the next construction must be a
23371 member-declaration. */
23372 cp_parser_member_declaration (parser);
23377 /* Parse a member-declaration.
23379 member-declaration:
23380 decl-specifier-seq [opt] member-declarator-list [opt] ;
23381 function-definition ; [opt]
23382 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23383 using-declaration
23384 template-declaration
23385 alias-declaration
23387 member-declarator-list:
23388 member-declarator
23389 member-declarator-list , member-declarator
23391 member-declarator:
23392 declarator pure-specifier [opt]
23393 declarator constant-initializer [opt]
23394 identifier [opt] : constant-expression
23396 GNU Extensions:
23398 member-declaration:
23399 __extension__ member-declaration
23401 member-declarator:
23402 declarator attributes [opt] pure-specifier [opt]
23403 declarator attributes [opt] constant-initializer [opt]
23404 identifier [opt] attributes [opt] : constant-expression
23406 C++0x Extensions:
23408 member-declaration:
23409 static_assert-declaration */
23411 static void
23412 cp_parser_member_declaration (cp_parser* parser)
23414 cp_decl_specifier_seq decl_specifiers;
23415 tree prefix_attributes;
23416 tree decl;
23417 int declares_class_or_enum;
23418 bool friend_p;
23419 cp_token *token = NULL;
23420 cp_token *decl_spec_token_start = NULL;
23421 cp_token *initializer_token_start = NULL;
23422 int saved_pedantic;
23423 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23425 /* Check for the `__extension__' keyword. */
23426 if (cp_parser_extension_opt (parser, &saved_pedantic))
23428 /* Recurse. */
23429 cp_parser_member_declaration (parser);
23430 /* Restore the old value of the PEDANTIC flag. */
23431 pedantic = saved_pedantic;
23433 return;
23436 /* Check for a template-declaration. */
23437 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23439 /* An explicit specialization here is an error condition, and we
23440 expect the specialization handler to detect and report this. */
23441 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23442 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23443 cp_parser_explicit_specialization (parser);
23444 else
23445 cp_parser_template_declaration (parser, /*member_p=*/true);
23447 return;
23449 /* Check for a template introduction. */
23450 else if (cp_parser_template_declaration_after_export (parser, true))
23451 return;
23453 /* Check for a using-declaration. */
23454 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23456 if (cxx_dialect < cxx11)
23458 /* Parse the using-declaration. */
23459 cp_parser_using_declaration (parser,
23460 /*access_declaration_p=*/false);
23461 return;
23463 else
23465 tree decl;
23466 bool alias_decl_expected;
23467 cp_parser_parse_tentatively (parser);
23468 decl = cp_parser_alias_declaration (parser);
23469 /* Note that if we actually see the '=' token after the
23470 identifier, cp_parser_alias_declaration commits the
23471 tentative parse. In that case, we really expect an
23472 alias-declaration. Otherwise, we expect a using
23473 declaration. */
23474 alias_decl_expected =
23475 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23476 cp_parser_parse_definitely (parser);
23478 if (alias_decl_expected)
23479 finish_member_declaration (decl);
23480 else
23481 cp_parser_using_declaration (parser,
23482 /*access_declaration_p=*/false);
23483 return;
23487 /* Check for @defs. */
23488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23490 tree ivar, member;
23491 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23492 ivar = ivar_chains;
23493 while (ivar)
23495 member = ivar;
23496 ivar = TREE_CHAIN (member);
23497 TREE_CHAIN (member) = NULL_TREE;
23498 finish_member_declaration (member);
23500 return;
23503 /* If the next token is `static_assert' we have a static assertion. */
23504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23506 cp_parser_static_assert (parser, /*member_p=*/true);
23507 return;
23510 parser->colon_corrects_to_scope_p = false;
23512 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23513 goto out;
23515 /* Parse the decl-specifier-seq. */
23516 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23517 cp_parser_decl_specifier_seq (parser,
23518 CP_PARSER_FLAGS_OPTIONAL,
23519 &decl_specifiers,
23520 &declares_class_or_enum);
23521 /* Check for an invalid type-name. */
23522 if (!decl_specifiers.any_type_specifiers_p
23523 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23524 goto out;
23525 /* If there is no declarator, then the decl-specifier-seq should
23526 specify a type. */
23527 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23529 /* If there was no decl-specifier-seq, and the next token is a
23530 `;', then we have something like:
23532 struct S { ; };
23534 [class.mem]
23536 Each member-declaration shall declare at least one member
23537 name of the class. */
23538 if (!decl_specifiers.any_specifiers_p)
23540 cp_token *token = cp_lexer_peek_token (parser->lexer);
23541 if (!in_system_header_at (token->location))
23543 gcc_rich_location richloc (token->location);
23544 richloc.add_fixit_remove ();
23545 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23548 else
23550 tree type;
23552 /* See if this declaration is a friend. */
23553 friend_p = cp_parser_friend_p (&decl_specifiers);
23554 /* If there were decl-specifiers, check to see if there was
23555 a class-declaration. */
23556 type = check_tag_decl (&decl_specifiers,
23557 /*explicit_type_instantiation_p=*/false);
23558 /* Nested classes have already been added to the class, but
23559 a `friend' needs to be explicitly registered. */
23560 if (friend_p)
23562 /* If the `friend' keyword was present, the friend must
23563 be introduced with a class-key. */
23564 if (!declares_class_or_enum && cxx_dialect < cxx11)
23565 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23566 "in C++03 a class-key must be used "
23567 "when declaring a friend");
23568 /* In this case:
23570 template <typename T> struct A {
23571 friend struct A<T>::B;
23574 A<T>::B will be represented by a TYPENAME_TYPE, and
23575 therefore not recognized by check_tag_decl. */
23576 if (!type)
23578 type = decl_specifiers.type;
23579 if (type && TREE_CODE (type) == TYPE_DECL)
23580 type = TREE_TYPE (type);
23582 if (!type || !TYPE_P (type))
23583 error_at (decl_spec_token_start->location,
23584 "friend declaration does not name a class or "
23585 "function");
23586 else
23587 make_friend_class (current_class_type, type,
23588 /*complain=*/true);
23590 /* If there is no TYPE, an error message will already have
23591 been issued. */
23592 else if (!type || type == error_mark_node)
23594 /* An anonymous aggregate has to be handled specially; such
23595 a declaration really declares a data member (with a
23596 particular type), as opposed to a nested class. */
23597 else if (ANON_AGGR_TYPE_P (type))
23599 /* C++11 9.5/6. */
23600 if (decl_specifiers.storage_class != sc_none)
23601 error_at (decl_spec_token_start->location,
23602 "a storage class on an anonymous aggregate "
23603 "in class scope is not allowed");
23605 /* Remove constructors and such from TYPE, now that we
23606 know it is an anonymous aggregate. */
23607 fixup_anonymous_aggr (type);
23608 /* And make the corresponding data member. */
23609 decl = build_decl (decl_spec_token_start->location,
23610 FIELD_DECL, NULL_TREE, type);
23611 /* Add it to the class. */
23612 finish_member_declaration (decl);
23614 else
23615 cp_parser_check_access_in_redeclaration
23616 (TYPE_NAME (type),
23617 decl_spec_token_start->location);
23620 else
23622 bool assume_semicolon = false;
23624 /* Clear attributes from the decl_specifiers but keep them
23625 around as prefix attributes that apply them to the entity
23626 being declared. */
23627 prefix_attributes = decl_specifiers.attributes;
23628 decl_specifiers.attributes = NULL_TREE;
23630 /* See if these declarations will be friends. */
23631 friend_p = cp_parser_friend_p (&decl_specifiers);
23633 /* Keep going until we hit the `;' at the end of the
23634 declaration. */
23635 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23637 tree attributes = NULL_TREE;
23638 tree first_attribute;
23639 tree initializer;
23640 bool named_bitfld = false;
23642 /* Peek at the next token. */
23643 token = cp_lexer_peek_token (parser->lexer);
23645 /* The following code wants to know early if it is a bit-field
23646 or some other declaration. Attributes can appear before
23647 the `:' token. Skip over them without consuming any tokens
23648 to peek if they are followed by `:'. */
23649 if (cp_next_tokens_can_be_attribute_p (parser)
23650 || (token->type == CPP_NAME
23651 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23652 && (named_bitfld = true)))
23654 size_t n
23655 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23656 token = cp_lexer_peek_nth_token (parser->lexer, n);
23659 /* Check for a bitfield declaration. */
23660 if (token->type == CPP_COLON
23661 || (token->type == CPP_NAME
23662 && token == cp_lexer_peek_token (parser->lexer)
23663 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23664 && (named_bitfld = true)))
23666 tree identifier;
23667 tree width;
23668 tree late_attributes = NULL_TREE;
23670 if (named_bitfld)
23671 identifier = cp_parser_identifier (parser);
23672 else
23673 identifier = NULL_TREE;
23675 /* Look for attributes that apply to the bitfield. */
23676 attributes = cp_parser_attributes_opt (parser);
23678 /* Consume the `:' token. */
23679 cp_lexer_consume_token (parser->lexer);
23681 /* Get the width of the bitfield. */
23682 width = cp_parser_constant_expression (parser, false, NULL,
23683 cxx_dialect >= cxx11);
23685 /* In C++2A and as extension for C++11 and above we allow
23686 default member initializers for bit-fields. */
23687 initializer = NULL_TREE;
23688 if (cxx_dialect >= cxx11
23689 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23690 || cp_lexer_next_token_is (parser->lexer,
23691 CPP_OPEN_BRACE)))
23693 location_t loc
23694 = cp_lexer_peek_token (parser->lexer)->location;
23695 if (cxx_dialect < cxx2a
23696 && !in_system_header_at (loc)
23697 && identifier != NULL_TREE)
23698 pedwarn (loc, 0,
23699 "default member initializers for bit-fields "
23700 "only available with -std=c++2a or "
23701 "-std=gnu++2a");
23703 initializer = cp_parser_save_nsdmi (parser);
23704 if (identifier == NULL_TREE)
23706 error_at (loc, "default member initializer for "
23707 "unnamed bit-field");
23708 initializer = NULL_TREE;
23711 else
23713 /* Look for attributes that apply to the bitfield after
23714 the `:' token and width. This is where GCC used to
23715 parse attributes in the past, pedwarn if there is
23716 a std attribute. */
23717 if (cp_next_tokens_can_be_std_attribute_p (parser))
23718 pedwarn (input_location, OPT_Wpedantic,
23719 "ISO C++ allows bit-field attributes only "
23720 "before the %<:%> token");
23722 late_attributes = cp_parser_attributes_opt (parser);
23725 attributes = attr_chainon (attributes, late_attributes);
23727 /* Remember which attributes are prefix attributes and
23728 which are not. */
23729 first_attribute = attributes;
23730 /* Combine the attributes. */
23731 attributes = attr_chainon (prefix_attributes, attributes);
23733 /* Create the bitfield declaration. */
23734 decl = grokbitfield (identifier
23735 ? make_id_declarator (NULL_TREE,
23736 identifier,
23737 sfk_none)
23738 : NULL,
23739 &decl_specifiers,
23740 width, initializer,
23741 attributes);
23743 else
23745 cp_declarator *declarator;
23746 tree asm_specification;
23747 int ctor_dtor_or_conv_p;
23749 /* Parse the declarator. */
23750 declarator
23751 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23752 &ctor_dtor_or_conv_p,
23753 /*parenthesized_p=*/NULL,
23754 /*member_p=*/true,
23755 friend_p);
23757 /* If something went wrong parsing the declarator, make sure
23758 that we at least consume some tokens. */
23759 if (declarator == cp_error_declarator)
23761 /* Skip to the end of the statement. */
23762 cp_parser_skip_to_end_of_statement (parser);
23763 /* If the next token is not a semicolon, that is
23764 probably because we just skipped over the body of
23765 a function. So, we consume a semicolon if
23766 present, but do not issue an error message if it
23767 is not present. */
23768 if (cp_lexer_next_token_is (parser->lexer,
23769 CPP_SEMICOLON))
23770 cp_lexer_consume_token (parser->lexer);
23771 goto out;
23774 if (declares_class_or_enum & 2)
23775 cp_parser_check_for_definition_in_return_type
23776 (declarator, decl_specifiers.type,
23777 decl_specifiers.locations[ds_type_spec]);
23779 /* Look for an asm-specification. */
23780 asm_specification = cp_parser_asm_specification_opt (parser);
23781 /* Look for attributes that apply to the declaration. */
23782 attributes = cp_parser_attributes_opt (parser);
23783 /* Remember which attributes are prefix attributes and
23784 which are not. */
23785 first_attribute = attributes;
23786 /* Combine the attributes. */
23787 attributes = attr_chainon (prefix_attributes, attributes);
23789 /* If it's an `=', then we have a constant-initializer or a
23790 pure-specifier. It is not correct to parse the
23791 initializer before registering the member declaration
23792 since the member declaration should be in scope while
23793 its initializer is processed. However, the rest of the
23794 front end does not yet provide an interface that allows
23795 us to handle this correctly. */
23796 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23798 /* In [class.mem]:
23800 A pure-specifier shall be used only in the declaration of
23801 a virtual function.
23803 A member-declarator can contain a constant-initializer
23804 only if it declares a static member of integral or
23805 enumeration type.
23807 Therefore, if the DECLARATOR is for a function, we look
23808 for a pure-specifier; otherwise, we look for a
23809 constant-initializer. When we call `grokfield', it will
23810 perform more stringent semantics checks. */
23811 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23812 if (function_declarator_p (declarator)
23813 || (decl_specifiers.type
23814 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23815 && declarator->kind == cdk_id
23816 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23817 == FUNCTION_TYPE)))
23818 initializer = cp_parser_pure_specifier (parser);
23819 else if (decl_specifiers.storage_class != sc_static)
23820 initializer = cp_parser_save_nsdmi (parser);
23821 else if (cxx_dialect >= cxx11)
23823 bool nonconst;
23824 /* Don't require a constant rvalue in C++11, since we
23825 might want a reference constant. We'll enforce
23826 constancy later. */
23827 cp_lexer_consume_token (parser->lexer);
23828 /* Parse the initializer. */
23829 initializer = cp_parser_initializer_clause (parser,
23830 &nonconst);
23832 else
23833 /* Parse the initializer. */
23834 initializer = cp_parser_constant_initializer (parser);
23836 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23837 && !function_declarator_p (declarator))
23839 bool x;
23840 if (decl_specifiers.storage_class != sc_static)
23841 initializer = cp_parser_save_nsdmi (parser);
23842 else
23843 initializer = cp_parser_initializer (parser, &x, &x);
23845 /* Otherwise, there is no initializer. */
23846 else
23847 initializer = NULL_TREE;
23849 /* See if we are probably looking at a function
23850 definition. We are certainly not looking at a
23851 member-declarator. Calling `grokfield' has
23852 side-effects, so we must not do it unless we are sure
23853 that we are looking at a member-declarator. */
23854 if (cp_parser_token_starts_function_definition_p
23855 (cp_lexer_peek_token (parser->lexer)))
23857 /* The grammar does not allow a pure-specifier to be
23858 used when a member function is defined. (It is
23859 possible that this fact is an oversight in the
23860 standard, since a pure function may be defined
23861 outside of the class-specifier. */
23862 if (initializer && initializer_token_start)
23863 error_at (initializer_token_start->location,
23864 "pure-specifier on function-definition");
23865 decl = cp_parser_save_member_function_body (parser,
23866 &decl_specifiers,
23867 declarator,
23868 attributes);
23869 if (parser->fully_implicit_function_template_p)
23870 decl = finish_fully_implicit_template (parser, decl);
23871 /* If the member was not a friend, declare it here. */
23872 if (!friend_p)
23873 finish_member_declaration (decl);
23874 /* Peek at the next token. */
23875 token = cp_lexer_peek_token (parser->lexer);
23876 /* If the next token is a semicolon, consume it. */
23877 if (token->type == CPP_SEMICOLON)
23879 location_t semicolon_loc
23880 = cp_lexer_consume_token (parser->lexer)->location;
23881 gcc_rich_location richloc (semicolon_loc);
23882 richloc.add_fixit_remove ();
23883 warning_at (&richloc, OPT_Wextra_semi,
23884 "extra %<;%> after in-class "
23885 "function definition");
23887 goto out;
23889 else
23890 if (declarator->kind == cdk_function)
23891 declarator->id_loc = token->location;
23892 /* Create the declaration. */
23893 decl = grokfield (declarator, &decl_specifiers,
23894 initializer, /*init_const_expr_p=*/true,
23895 asm_specification, attributes);
23896 if (parser->fully_implicit_function_template_p)
23898 if (friend_p)
23899 finish_fully_implicit_template (parser, 0);
23900 else
23901 decl = finish_fully_implicit_template (parser, decl);
23905 cp_finalize_omp_declare_simd (parser, decl);
23906 cp_finalize_oacc_routine (parser, decl, false);
23908 /* Reset PREFIX_ATTRIBUTES. */
23909 if (attributes != error_mark_node)
23911 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23912 attributes = TREE_CHAIN (attributes);
23913 if (attributes)
23914 TREE_CHAIN (attributes) = NULL_TREE;
23917 /* If there is any qualification still in effect, clear it
23918 now; we will be starting fresh with the next declarator. */
23919 parser->scope = NULL_TREE;
23920 parser->qualifying_scope = NULL_TREE;
23921 parser->object_scope = NULL_TREE;
23922 /* If it's a `,', then there are more declarators. */
23923 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23925 cp_lexer_consume_token (parser->lexer);
23926 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23928 cp_token *token = cp_lexer_previous_token (parser->lexer);
23929 gcc_rich_location richloc (token->location);
23930 richloc.add_fixit_remove ();
23931 error_at (&richloc, "stray %<,%> at end of "
23932 "member declaration");
23935 /* If the next token isn't a `;', then we have a parse error. */
23936 else if (cp_lexer_next_token_is_not (parser->lexer,
23937 CPP_SEMICOLON))
23939 /* The next token might be a ways away from where the
23940 actual semicolon is missing. Find the previous token
23941 and use that for our error position. */
23942 cp_token *token = cp_lexer_previous_token (parser->lexer);
23943 gcc_rich_location richloc (token->location);
23944 richloc.add_fixit_insert_after (";");
23945 error_at (&richloc, "expected %<;%> at end of "
23946 "member declaration");
23948 /* Assume that the user meant to provide a semicolon. If
23949 we were to cp_parser_skip_to_end_of_statement, we might
23950 skip to a semicolon inside a member function definition
23951 and issue nonsensical error messages. */
23952 assume_semicolon = true;
23955 if (decl)
23957 /* Add DECL to the list of members. */
23958 if (!friend_p
23959 /* Explicitly include, eg, NSDMIs, for better error
23960 recovery (c++/58650). */
23961 || !DECL_DECLARES_FUNCTION_P (decl))
23962 finish_member_declaration (decl);
23964 if (TREE_CODE (decl) == FUNCTION_DECL)
23965 cp_parser_save_default_args (parser, decl);
23966 else if (TREE_CODE (decl) == FIELD_DECL
23967 && DECL_INITIAL (decl))
23968 /* Add DECL to the queue of NSDMI to be parsed later. */
23969 vec_safe_push (unparsed_nsdmis, decl);
23972 if (assume_semicolon)
23973 goto out;
23977 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23978 out:
23979 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23982 /* Parse a pure-specifier.
23984 pure-specifier:
23987 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23988 Otherwise, ERROR_MARK_NODE is returned. */
23990 static tree
23991 cp_parser_pure_specifier (cp_parser* parser)
23993 cp_token *token;
23995 /* Look for the `=' token. */
23996 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23997 return error_mark_node;
23998 /* Look for the `0' token. */
23999 token = cp_lexer_peek_token (parser->lexer);
24001 if (token->type == CPP_EOF
24002 || token->type == CPP_PRAGMA_EOL)
24003 return error_mark_node;
24005 cp_lexer_consume_token (parser->lexer);
24007 /* Accept = default or = delete in c++0x mode. */
24008 if (token->keyword == RID_DEFAULT
24009 || token->keyword == RID_DELETE)
24011 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24012 return token->u.value;
24015 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24016 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24018 cp_parser_error (parser,
24019 "invalid pure specifier (only %<= 0%> is allowed)");
24020 cp_parser_skip_to_end_of_statement (parser);
24021 return error_mark_node;
24023 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24025 error_at (token->location, "templates may not be %<virtual%>");
24026 return error_mark_node;
24029 return integer_zero_node;
24032 /* Parse a constant-initializer.
24034 constant-initializer:
24035 = constant-expression
24037 Returns a representation of the constant-expression. */
24039 static tree
24040 cp_parser_constant_initializer (cp_parser* parser)
24042 /* Look for the `=' token. */
24043 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24044 return error_mark_node;
24046 /* It is invalid to write:
24048 struct S { static const int i = { 7 }; };
24051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24053 cp_parser_error (parser,
24054 "a brace-enclosed initializer is not allowed here");
24055 /* Consume the opening brace. */
24056 matching_braces braces;
24057 braces.consume_open (parser);
24058 /* Skip the initializer. */
24059 cp_parser_skip_to_closing_brace (parser);
24060 /* Look for the trailing `}'. */
24061 braces.require_close (parser);
24063 return error_mark_node;
24066 return cp_parser_constant_expression (parser);
24069 /* Derived classes [gram.class.derived] */
24071 /* Parse a base-clause.
24073 base-clause:
24074 : base-specifier-list
24076 base-specifier-list:
24077 base-specifier ... [opt]
24078 base-specifier-list , base-specifier ... [opt]
24080 Returns a TREE_LIST representing the base-classes, in the order in
24081 which they were declared. The representation of each node is as
24082 described by cp_parser_base_specifier.
24084 In the case that no bases are specified, this function will return
24085 NULL_TREE, not ERROR_MARK_NODE. */
24087 static tree
24088 cp_parser_base_clause (cp_parser* parser)
24090 tree bases = NULL_TREE;
24092 /* Look for the `:' that begins the list. */
24093 cp_parser_require (parser, CPP_COLON, RT_COLON);
24095 /* Scan the base-specifier-list. */
24096 while (true)
24098 cp_token *token;
24099 tree base;
24100 bool pack_expansion_p = false;
24102 /* Look for the base-specifier. */
24103 base = cp_parser_base_specifier (parser);
24104 /* Look for the (optional) ellipsis. */
24105 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24107 /* Consume the `...'. */
24108 cp_lexer_consume_token (parser->lexer);
24110 pack_expansion_p = true;
24113 /* Add BASE to the front of the list. */
24114 if (base && base != error_mark_node)
24116 if (pack_expansion_p)
24117 /* Make this a pack expansion type. */
24118 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24120 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24122 TREE_CHAIN (base) = bases;
24123 bases = base;
24126 /* Peek at the next token. */
24127 token = cp_lexer_peek_token (parser->lexer);
24128 /* If it's not a comma, then the list is complete. */
24129 if (token->type != CPP_COMMA)
24130 break;
24131 /* Consume the `,'. */
24132 cp_lexer_consume_token (parser->lexer);
24135 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24136 base class had a qualified name. However, the next name that
24137 appears is certainly not qualified. */
24138 parser->scope = NULL_TREE;
24139 parser->qualifying_scope = NULL_TREE;
24140 parser->object_scope = NULL_TREE;
24142 return nreverse (bases);
24145 /* Parse a base-specifier.
24147 base-specifier:
24148 :: [opt] nested-name-specifier [opt] class-name
24149 virtual access-specifier [opt] :: [opt] nested-name-specifier
24150 [opt] class-name
24151 access-specifier virtual [opt] :: [opt] nested-name-specifier
24152 [opt] class-name
24154 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24155 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24156 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24157 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24159 static tree
24160 cp_parser_base_specifier (cp_parser* parser)
24162 cp_token *token;
24163 bool done = false;
24164 bool virtual_p = false;
24165 bool duplicate_virtual_error_issued_p = false;
24166 bool duplicate_access_error_issued_p = false;
24167 bool class_scope_p, template_p;
24168 tree access = access_default_node;
24169 tree type;
24171 /* Process the optional `virtual' and `access-specifier'. */
24172 while (!done)
24174 /* Peek at the next token. */
24175 token = cp_lexer_peek_token (parser->lexer);
24176 /* Process `virtual'. */
24177 switch (token->keyword)
24179 case RID_VIRTUAL:
24180 /* If `virtual' appears more than once, issue an error. */
24181 if (virtual_p && !duplicate_virtual_error_issued_p)
24183 cp_parser_error (parser,
24184 "%<virtual%> specified more than once in base-specifier");
24185 duplicate_virtual_error_issued_p = true;
24188 virtual_p = true;
24190 /* Consume the `virtual' token. */
24191 cp_lexer_consume_token (parser->lexer);
24193 break;
24195 case RID_PUBLIC:
24196 case RID_PROTECTED:
24197 case RID_PRIVATE:
24198 /* If more than one access specifier appears, issue an
24199 error. */
24200 if (access != access_default_node
24201 && !duplicate_access_error_issued_p)
24203 cp_parser_error (parser,
24204 "more than one access specifier in base-specifier");
24205 duplicate_access_error_issued_p = true;
24208 access = ridpointers[(int) token->keyword];
24210 /* Consume the access-specifier. */
24211 cp_lexer_consume_token (parser->lexer);
24213 break;
24215 default:
24216 done = true;
24217 break;
24220 /* It is not uncommon to see programs mechanically, erroneously, use
24221 the 'typename' keyword to denote (dependent) qualified types
24222 as base classes. */
24223 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24225 token = cp_lexer_peek_token (parser->lexer);
24226 if (!processing_template_decl)
24227 error_at (token->location,
24228 "keyword %<typename%> not allowed outside of templates");
24229 else
24230 error_at (token->location,
24231 "keyword %<typename%> not allowed in this context "
24232 "(the base class is implicitly a type)");
24233 cp_lexer_consume_token (parser->lexer);
24236 /* Look for the optional `::' operator. */
24237 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24238 /* Look for the nested-name-specifier. The simplest way to
24239 implement:
24241 [temp.res]
24243 The keyword `typename' is not permitted in a base-specifier or
24244 mem-initializer; in these contexts a qualified name that
24245 depends on a template-parameter is implicitly assumed to be a
24246 type name.
24248 is to pretend that we have seen the `typename' keyword at this
24249 point. */
24250 cp_parser_nested_name_specifier_opt (parser,
24251 /*typename_keyword_p=*/true,
24252 /*check_dependency_p=*/true,
24253 /*type_p=*/true,
24254 /*is_declaration=*/true);
24255 /* If the base class is given by a qualified name, assume that names
24256 we see are type names or templates, as appropriate. */
24257 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24258 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24260 if (!parser->scope
24261 && cp_lexer_next_token_is_decltype (parser->lexer))
24262 /* DR 950 allows decltype as a base-specifier. */
24263 type = cp_parser_decltype (parser);
24264 else
24266 /* Otherwise, look for the class-name. */
24267 type = cp_parser_class_name (parser,
24268 class_scope_p,
24269 template_p,
24270 typename_type,
24271 /*check_dependency_p=*/true,
24272 /*class_head_p=*/false,
24273 /*is_declaration=*/true);
24274 type = TREE_TYPE (type);
24277 if (type == error_mark_node)
24278 return error_mark_node;
24280 return finish_base_specifier (type, access, virtual_p);
24283 /* Exception handling [gram.exception] */
24285 /* Parse an (optional) noexcept-specification.
24287 noexcept-specification:
24288 noexcept ( constant-expression ) [opt]
24290 If no noexcept-specification is present, returns NULL_TREE.
24291 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24292 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24293 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24294 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24295 in which case a boolean condition is returned instead. */
24297 static tree
24298 cp_parser_noexcept_specification_opt (cp_parser* parser,
24299 bool require_constexpr,
24300 bool* consumed_expr,
24301 bool return_cond)
24303 cp_token *token;
24304 const char *saved_message;
24306 /* Peek at the next token. */
24307 token = cp_lexer_peek_token (parser->lexer);
24309 /* Is it a noexcept-specification? */
24310 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24312 tree expr;
24313 cp_lexer_consume_token (parser->lexer);
24315 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24317 matching_parens parens;
24318 parens.consume_open (parser);
24320 if (require_constexpr)
24322 /* Types may not be defined in an exception-specification. */
24323 saved_message = parser->type_definition_forbidden_message;
24324 parser->type_definition_forbidden_message
24325 = G_("types may not be defined in an exception-specification");
24327 expr = cp_parser_constant_expression (parser);
24329 /* Restore the saved message. */
24330 parser->type_definition_forbidden_message = saved_message;
24332 else
24334 expr = cp_parser_expression (parser);
24335 *consumed_expr = true;
24338 parens.require_close (parser);
24340 else
24342 expr = boolean_true_node;
24343 if (!require_constexpr)
24344 *consumed_expr = false;
24347 /* We cannot build a noexcept-spec right away because this will check
24348 that expr is a constexpr. */
24349 if (!return_cond)
24350 return build_noexcept_spec (expr, tf_warning_or_error);
24351 else
24352 return expr;
24354 else
24355 return NULL_TREE;
24358 /* Parse an (optional) exception-specification.
24360 exception-specification:
24361 throw ( type-id-list [opt] )
24363 Returns a TREE_LIST representing the exception-specification. The
24364 TREE_VALUE of each node is a type. */
24366 static tree
24367 cp_parser_exception_specification_opt (cp_parser* parser)
24369 cp_token *token;
24370 tree type_id_list;
24371 const char *saved_message;
24373 /* Peek at the next token. */
24374 token = cp_lexer_peek_token (parser->lexer);
24376 /* Is it a noexcept-specification? */
24377 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24378 false);
24379 if (type_id_list != NULL_TREE)
24380 return type_id_list;
24382 /* If it's not `throw', then there's no exception-specification. */
24383 if (!cp_parser_is_keyword (token, RID_THROW))
24384 return NULL_TREE;
24386 location_t loc = token->location;
24388 /* Consume the `throw'. */
24389 cp_lexer_consume_token (parser->lexer);
24391 /* Look for the `('. */
24392 matching_parens parens;
24393 parens.require_open (parser);
24395 /* Peek at the next token. */
24396 token = cp_lexer_peek_token (parser->lexer);
24397 /* If it's not a `)', then there is a type-id-list. */
24398 if (token->type != CPP_CLOSE_PAREN)
24400 /* Types may not be defined in an exception-specification. */
24401 saved_message = parser->type_definition_forbidden_message;
24402 parser->type_definition_forbidden_message
24403 = G_("types may not be defined in an exception-specification");
24404 /* Parse the type-id-list. */
24405 type_id_list = cp_parser_type_id_list (parser);
24406 /* Restore the saved message. */
24407 parser->type_definition_forbidden_message = saved_message;
24409 if (cxx_dialect >= cxx17)
24411 error_at (loc, "ISO C++17 does not allow dynamic exception "
24412 "specifications");
24413 type_id_list = NULL_TREE;
24415 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24416 warning_at (loc, OPT_Wdeprecated,
24417 "dynamic exception specifications are deprecated in "
24418 "C++11");
24420 /* In C++17, throw() is equivalent to noexcept (true). throw()
24421 is deprecated in C++11 and above as well, but is still widely used,
24422 so don't warn about it yet. */
24423 else if (cxx_dialect >= cxx17)
24424 type_id_list = noexcept_true_spec;
24425 else
24426 type_id_list = empty_except_spec;
24428 /* Look for the `)'. */
24429 parens.require_close (parser);
24431 return type_id_list;
24434 /* Parse an (optional) type-id-list.
24436 type-id-list:
24437 type-id ... [opt]
24438 type-id-list , type-id ... [opt]
24440 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24441 in the order that the types were presented. */
24443 static tree
24444 cp_parser_type_id_list (cp_parser* parser)
24446 tree types = NULL_TREE;
24448 while (true)
24450 cp_token *token;
24451 tree type;
24453 token = cp_lexer_peek_token (parser->lexer);
24455 /* Get the next type-id. */
24456 type = cp_parser_type_id (parser);
24457 /* Check for invalid 'auto'. */
24458 if (flag_concepts && type_uses_auto (type))
24460 error_at (token->location,
24461 "invalid use of %<auto%> in exception-specification");
24462 type = error_mark_node;
24464 /* Parse the optional ellipsis. */
24465 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24467 /* Consume the `...'. */
24468 cp_lexer_consume_token (parser->lexer);
24470 /* Turn the type into a pack expansion expression. */
24471 type = make_pack_expansion (type);
24473 /* Add it to the list. */
24474 types = add_exception_specifier (types, type, /*complain=*/1);
24475 /* Peek at the next token. */
24476 token = cp_lexer_peek_token (parser->lexer);
24477 /* If it is not a `,', we are done. */
24478 if (token->type != CPP_COMMA)
24479 break;
24480 /* Consume the `,'. */
24481 cp_lexer_consume_token (parser->lexer);
24484 return nreverse (types);
24487 /* Parse a try-block.
24489 try-block:
24490 try compound-statement handler-seq */
24492 static tree
24493 cp_parser_try_block (cp_parser* parser)
24495 tree try_block;
24497 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24498 if (parser->in_function_body
24499 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24500 error ("%<try%> in %<constexpr%> function");
24502 try_block = begin_try_block ();
24503 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24504 finish_try_block (try_block);
24505 cp_parser_handler_seq (parser);
24506 finish_handler_sequence (try_block);
24508 return try_block;
24511 /* Parse a function-try-block.
24513 function-try-block:
24514 try ctor-initializer [opt] function-body handler-seq */
24516 static void
24517 cp_parser_function_try_block (cp_parser* parser)
24519 tree compound_stmt;
24520 tree try_block;
24522 /* Look for the `try' keyword. */
24523 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24524 return;
24525 /* Let the rest of the front end know where we are. */
24526 try_block = begin_function_try_block (&compound_stmt);
24527 /* Parse the function-body. */
24528 cp_parser_ctor_initializer_opt_and_function_body
24529 (parser, /*in_function_try_block=*/true);
24530 /* We're done with the `try' part. */
24531 finish_function_try_block (try_block);
24532 /* Parse the handlers. */
24533 cp_parser_handler_seq (parser);
24534 /* We're done with the handlers. */
24535 finish_function_handler_sequence (try_block, compound_stmt);
24538 /* Parse a handler-seq.
24540 handler-seq:
24541 handler handler-seq [opt] */
24543 static void
24544 cp_parser_handler_seq (cp_parser* parser)
24546 while (true)
24548 cp_token *token;
24550 /* Parse the handler. */
24551 cp_parser_handler (parser);
24552 /* Peek at the next token. */
24553 token = cp_lexer_peek_token (parser->lexer);
24554 /* If it's not `catch' then there are no more handlers. */
24555 if (!cp_parser_is_keyword (token, RID_CATCH))
24556 break;
24560 /* Parse a handler.
24562 handler:
24563 catch ( exception-declaration ) compound-statement */
24565 static void
24566 cp_parser_handler (cp_parser* parser)
24568 tree handler;
24569 tree declaration;
24571 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24572 handler = begin_handler ();
24573 matching_parens parens;
24574 parens.require_open (parser);
24575 declaration = cp_parser_exception_declaration (parser);
24576 finish_handler_parms (declaration, handler);
24577 parens.require_close (parser);
24578 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24579 finish_handler (handler);
24582 /* Parse an exception-declaration.
24584 exception-declaration:
24585 type-specifier-seq declarator
24586 type-specifier-seq abstract-declarator
24587 type-specifier-seq
24590 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24591 ellipsis variant is used. */
24593 static tree
24594 cp_parser_exception_declaration (cp_parser* parser)
24596 cp_decl_specifier_seq type_specifiers;
24597 cp_declarator *declarator;
24598 const char *saved_message;
24600 /* If it's an ellipsis, it's easy to handle. */
24601 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24603 /* Consume the `...' token. */
24604 cp_lexer_consume_token (parser->lexer);
24605 return NULL_TREE;
24608 /* Types may not be defined in exception-declarations. */
24609 saved_message = parser->type_definition_forbidden_message;
24610 parser->type_definition_forbidden_message
24611 = G_("types may not be defined in exception-declarations");
24613 /* Parse the type-specifier-seq. */
24614 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24615 /*is_trailing_return=*/false,
24616 &type_specifiers);
24617 /* If it's a `)', then there is no declarator. */
24618 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24619 declarator = NULL;
24620 else
24621 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24622 /*ctor_dtor_or_conv_p=*/NULL,
24623 /*parenthesized_p=*/NULL,
24624 /*member_p=*/false,
24625 /*friend_p=*/false);
24627 /* Restore the saved message. */
24628 parser->type_definition_forbidden_message = saved_message;
24630 if (!type_specifiers.any_specifiers_p)
24631 return error_mark_node;
24633 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24636 /* Parse a throw-expression.
24638 throw-expression:
24639 throw assignment-expression [opt]
24641 Returns a THROW_EXPR representing the throw-expression. */
24643 static tree
24644 cp_parser_throw_expression (cp_parser* parser)
24646 tree expression;
24647 cp_token* token;
24649 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24650 token = cp_lexer_peek_token (parser->lexer);
24651 /* Figure out whether or not there is an assignment-expression
24652 following the "throw" keyword. */
24653 if (token->type == CPP_COMMA
24654 || token->type == CPP_SEMICOLON
24655 || token->type == CPP_CLOSE_PAREN
24656 || token->type == CPP_CLOSE_SQUARE
24657 || token->type == CPP_CLOSE_BRACE
24658 || token->type == CPP_COLON)
24659 expression = NULL_TREE;
24660 else
24661 expression = cp_parser_assignment_expression (parser);
24663 return build_throw (expression);
24666 /* GNU Extensions */
24668 /* Parse an (optional) asm-specification.
24670 asm-specification:
24671 asm ( string-literal )
24673 If the asm-specification is present, returns a STRING_CST
24674 corresponding to the string-literal. Otherwise, returns
24675 NULL_TREE. */
24677 static tree
24678 cp_parser_asm_specification_opt (cp_parser* parser)
24680 cp_token *token;
24681 tree asm_specification;
24683 /* Peek at the next token. */
24684 token = cp_lexer_peek_token (parser->lexer);
24685 /* If the next token isn't the `asm' keyword, then there's no
24686 asm-specification. */
24687 if (!cp_parser_is_keyword (token, RID_ASM))
24688 return NULL_TREE;
24690 /* Consume the `asm' token. */
24691 cp_lexer_consume_token (parser->lexer);
24692 /* Look for the `('. */
24693 matching_parens parens;
24694 parens.require_open (parser);
24696 /* Look for the string-literal. */
24697 asm_specification = cp_parser_string_literal (parser, false, false);
24699 /* Look for the `)'. */
24700 parens.require_close (parser);
24702 return asm_specification;
24705 /* Parse an asm-operand-list.
24707 asm-operand-list:
24708 asm-operand
24709 asm-operand-list , asm-operand
24711 asm-operand:
24712 string-literal ( expression )
24713 [ string-literal ] string-literal ( expression )
24715 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24716 each node is the expression. The TREE_PURPOSE is itself a
24717 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24718 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24719 is a STRING_CST for the string literal before the parenthesis. Returns
24720 ERROR_MARK_NODE if any of the operands are invalid. */
24722 static tree
24723 cp_parser_asm_operand_list (cp_parser* parser)
24725 tree asm_operands = NULL_TREE;
24726 bool invalid_operands = false;
24728 while (true)
24730 tree string_literal;
24731 tree expression;
24732 tree name;
24734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24736 /* Consume the `[' token. */
24737 cp_lexer_consume_token (parser->lexer);
24738 /* Read the operand name. */
24739 name = cp_parser_identifier (parser);
24740 if (name != error_mark_node)
24741 name = build_string (IDENTIFIER_LENGTH (name),
24742 IDENTIFIER_POINTER (name));
24743 /* Look for the closing `]'. */
24744 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24746 else
24747 name = NULL_TREE;
24748 /* Look for the string-literal. */
24749 string_literal = cp_parser_string_literal (parser, false, false);
24751 /* Look for the `('. */
24752 matching_parens parens;
24753 parens.require_open (parser);
24754 /* Parse the expression. */
24755 expression = cp_parser_expression (parser);
24756 /* Look for the `)'. */
24757 parens.require_close (parser);
24759 if (name == error_mark_node
24760 || string_literal == error_mark_node
24761 || expression == error_mark_node)
24762 invalid_operands = true;
24764 /* Add this operand to the list. */
24765 asm_operands = tree_cons (build_tree_list (name, string_literal),
24766 expression,
24767 asm_operands);
24768 /* If the next token is not a `,', there are no more
24769 operands. */
24770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24771 break;
24772 /* Consume the `,'. */
24773 cp_lexer_consume_token (parser->lexer);
24776 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24779 /* Parse an asm-clobber-list.
24781 asm-clobber-list:
24782 string-literal
24783 asm-clobber-list , string-literal
24785 Returns a TREE_LIST, indicating the clobbers in the order that they
24786 appeared. The TREE_VALUE of each node is a STRING_CST. */
24788 static tree
24789 cp_parser_asm_clobber_list (cp_parser* parser)
24791 tree clobbers = NULL_TREE;
24793 while (true)
24795 tree string_literal;
24797 /* Look for the string literal. */
24798 string_literal = cp_parser_string_literal (parser, false, false);
24799 /* Add it to the list. */
24800 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24801 /* If the next token is not a `,', then the list is
24802 complete. */
24803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24804 break;
24805 /* Consume the `,' token. */
24806 cp_lexer_consume_token (parser->lexer);
24809 return clobbers;
24812 /* Parse an asm-label-list.
24814 asm-label-list:
24815 identifier
24816 asm-label-list , identifier
24818 Returns a TREE_LIST, indicating the labels in the order that they
24819 appeared. The TREE_VALUE of each node is a label. */
24821 static tree
24822 cp_parser_asm_label_list (cp_parser* parser)
24824 tree labels = NULL_TREE;
24826 while (true)
24828 tree identifier, label, name;
24830 /* Look for the identifier. */
24831 identifier = cp_parser_identifier (parser);
24832 if (!error_operand_p (identifier))
24834 label = lookup_label (identifier);
24835 if (TREE_CODE (label) == LABEL_DECL)
24837 TREE_USED (label) = 1;
24838 check_goto (label);
24839 name = build_string (IDENTIFIER_LENGTH (identifier),
24840 IDENTIFIER_POINTER (identifier));
24841 labels = tree_cons (name, label, labels);
24844 /* If the next token is not a `,', then the list is
24845 complete. */
24846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24847 break;
24848 /* Consume the `,' token. */
24849 cp_lexer_consume_token (parser->lexer);
24852 return nreverse (labels);
24855 /* Return TRUE iff the next tokens in the stream are possibly the
24856 beginning of a GNU extension attribute. */
24858 static bool
24859 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24861 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24864 /* Return TRUE iff the next tokens in the stream are possibly the
24865 beginning of a standard C++-11 attribute specifier. */
24867 static bool
24868 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24870 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24873 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24874 beginning of a standard C++-11 attribute specifier. */
24876 static bool
24877 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24879 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24881 return (cxx_dialect >= cxx11
24882 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24883 || (token->type == CPP_OPEN_SQUARE
24884 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24885 && token->type == CPP_OPEN_SQUARE)));
24888 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24889 beginning of a GNU extension attribute. */
24891 static bool
24892 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24894 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24896 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24899 /* Return true iff the next tokens can be the beginning of either a
24900 GNU attribute list, or a standard C++11 attribute sequence. */
24902 static bool
24903 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24905 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24906 || cp_next_tokens_can_be_std_attribute_p (parser));
24909 /* Return true iff the next Nth tokens can be the beginning of either
24910 a GNU attribute list, or a standard C++11 attribute sequence. */
24912 static bool
24913 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24915 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24916 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24919 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24920 of GNU attributes, or return NULL. */
24922 static tree
24923 cp_parser_attributes_opt (cp_parser *parser)
24925 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24926 return cp_parser_gnu_attributes_opt (parser);
24927 return cp_parser_std_attribute_spec_seq (parser);
24930 /* Parse an (optional) series of attributes.
24932 attributes:
24933 attributes attribute
24935 attribute:
24936 __attribute__ (( attribute-list [opt] ))
24938 The return value is as for cp_parser_gnu_attribute_list. */
24940 static tree
24941 cp_parser_gnu_attributes_opt (cp_parser* parser)
24943 tree attributes = NULL_TREE;
24945 while (true)
24947 cp_token *token;
24948 tree attribute_list;
24949 bool ok = true;
24951 /* Peek at the next token. */
24952 token = cp_lexer_peek_token (parser->lexer);
24953 /* If it's not `__attribute__', then we're done. */
24954 if (token->keyword != RID_ATTRIBUTE)
24955 break;
24957 /* Consume the `__attribute__' keyword. */
24958 cp_lexer_consume_token (parser->lexer);
24959 /* Look for the two `(' tokens. */
24960 matching_parens outer_parens;
24961 outer_parens.require_open (parser);
24962 matching_parens inner_parens;
24963 inner_parens.require_open (parser);
24965 /* Peek at the next token. */
24966 token = cp_lexer_peek_token (parser->lexer);
24967 if (token->type != CPP_CLOSE_PAREN)
24968 /* Parse the attribute-list. */
24969 attribute_list = cp_parser_gnu_attribute_list (parser);
24970 else
24971 /* If the next token is a `)', then there is no attribute
24972 list. */
24973 attribute_list = NULL;
24975 /* Look for the two `)' tokens. */
24976 if (!inner_parens.require_close (parser))
24977 ok = false;
24978 if (!outer_parens.require_close (parser))
24979 ok = false;
24980 if (!ok)
24981 cp_parser_skip_to_end_of_statement (parser);
24983 /* Add these new attributes to the list. */
24984 attributes = attr_chainon (attributes, attribute_list);
24987 return attributes;
24990 /* Parse a GNU attribute-list.
24992 attribute-list:
24993 attribute
24994 attribute-list , attribute
24996 attribute:
24997 identifier
24998 identifier ( identifier )
24999 identifier ( identifier , expression-list )
25000 identifier ( expression-list )
25002 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25003 to an attribute. The TREE_PURPOSE of each node is the identifier
25004 indicating which attribute is in use. The TREE_VALUE represents
25005 the arguments, if any. */
25007 static tree
25008 cp_parser_gnu_attribute_list (cp_parser* parser)
25010 tree attribute_list = NULL_TREE;
25011 bool save_translate_strings_p = parser->translate_strings_p;
25013 parser->translate_strings_p = false;
25014 while (true)
25016 cp_token *token;
25017 tree identifier;
25018 tree attribute;
25020 /* Look for the identifier. We also allow keywords here; for
25021 example `__attribute__ ((const))' is legal. */
25022 token = cp_lexer_peek_token (parser->lexer);
25023 if (token->type == CPP_NAME
25024 || token->type == CPP_KEYWORD)
25026 tree arguments = NULL_TREE;
25028 /* Consume the token, but save it since we need it for the
25029 SIMD enabled function parsing. */
25030 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25032 /* Save away the identifier that indicates which attribute
25033 this is. */
25034 identifier = (token->type == CPP_KEYWORD)
25035 /* For keywords, use the canonical spelling, not the
25036 parsed identifier. */
25037 ? ridpointers[(int) token->keyword]
25038 : id_token->u.value;
25040 identifier = canonicalize_attr_name (identifier);
25041 attribute = build_tree_list (identifier, NULL_TREE);
25043 /* Peek at the next token. */
25044 token = cp_lexer_peek_token (parser->lexer);
25045 /* If it's an `(', then parse the attribute arguments. */
25046 if (token->type == CPP_OPEN_PAREN)
25048 vec<tree, va_gc> *vec;
25049 int attr_flag = (attribute_takes_identifier_p (identifier)
25050 ? id_attr : normal_attr);
25051 vec = cp_parser_parenthesized_expression_list
25052 (parser, attr_flag, /*cast_p=*/false,
25053 /*allow_expansion_p=*/false,
25054 /*non_constant_p=*/NULL);
25055 if (vec == NULL)
25056 arguments = error_mark_node;
25057 else
25059 arguments = build_tree_list_vec (vec);
25060 release_tree_vector (vec);
25062 /* Save the arguments away. */
25063 TREE_VALUE (attribute) = arguments;
25066 if (arguments != error_mark_node)
25068 /* Add this attribute to the list. */
25069 TREE_CHAIN (attribute) = attribute_list;
25070 attribute_list = attribute;
25073 token = cp_lexer_peek_token (parser->lexer);
25075 /* Now, look for more attributes. If the next token isn't a
25076 `,', we're done. */
25077 if (token->type != CPP_COMMA)
25078 break;
25080 /* Consume the comma and keep going. */
25081 cp_lexer_consume_token (parser->lexer);
25083 parser->translate_strings_p = save_translate_strings_p;
25085 /* We built up the list in reverse order. */
25086 return nreverse (attribute_list);
25089 /* Parse a standard C++11 attribute.
25091 The returned representation is a TREE_LIST which TREE_PURPOSE is
25092 the scoped name of the attribute, and the TREE_VALUE is its
25093 arguments list.
25095 Note that the scoped name of the attribute is itself a TREE_LIST
25096 which TREE_PURPOSE is the namespace of the attribute, and
25097 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25098 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25099 and which TREE_PURPOSE is directly the attribute name.
25101 Clients of the attribute code should use get_attribute_namespace
25102 and get_attribute_name to get the actual namespace and name of
25103 attributes, regardless of their being GNU or C++11 attributes.
25105 attribute:
25106 attribute-token attribute-argument-clause [opt]
25108 attribute-token:
25109 identifier
25110 attribute-scoped-token
25112 attribute-scoped-token:
25113 attribute-namespace :: identifier
25115 attribute-namespace:
25116 identifier
25118 attribute-argument-clause:
25119 ( balanced-token-seq )
25121 balanced-token-seq:
25122 balanced-token [opt]
25123 balanced-token-seq balanced-token
25125 balanced-token:
25126 ( balanced-token-seq )
25127 [ balanced-token-seq ]
25128 { balanced-token-seq }. */
25130 static tree
25131 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25133 tree attribute, attr_id = NULL_TREE, arguments;
25134 cp_token *token;
25136 /* First, parse name of the attribute, a.k.a attribute-token. */
25138 token = cp_lexer_peek_token (parser->lexer);
25139 if (token->type == CPP_NAME)
25140 attr_id = token->u.value;
25141 else if (token->type == CPP_KEYWORD)
25142 attr_id = ridpointers[(int) token->keyword];
25143 else if (token->flags & NAMED_OP)
25144 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25146 if (attr_id == NULL_TREE)
25147 return NULL_TREE;
25149 cp_lexer_consume_token (parser->lexer);
25151 token = cp_lexer_peek_token (parser->lexer);
25152 if (token->type == CPP_SCOPE)
25154 /* We are seeing a scoped attribute token. */
25156 cp_lexer_consume_token (parser->lexer);
25157 if (attr_ns)
25158 error_at (token->location, "attribute using prefix used together "
25159 "with scoped attribute token");
25160 attr_ns = attr_id;
25162 token = cp_lexer_consume_token (parser->lexer);
25163 if (token->type == CPP_NAME)
25164 attr_id = token->u.value;
25165 else if (token->type == CPP_KEYWORD)
25166 attr_id = ridpointers[(int) token->keyword];
25167 else if (token->flags & NAMED_OP)
25168 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25169 else
25171 error_at (token->location,
25172 "expected an identifier for the attribute name");
25173 return error_mark_node;
25176 attr_id = canonicalize_attr_name (attr_id);
25177 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25178 NULL_TREE);
25179 token = cp_lexer_peek_token (parser->lexer);
25181 else if (attr_ns)
25182 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25183 NULL_TREE);
25184 else
25186 attr_id = canonicalize_attr_name (attr_id);
25187 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25188 NULL_TREE);
25189 /* C++11 noreturn attribute is equivalent to GNU's. */
25190 if (is_attribute_p ("noreturn", attr_id))
25191 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25192 /* C++14 deprecated attribute is equivalent to GNU's. */
25193 else if (is_attribute_p ("deprecated", attr_id))
25194 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25195 /* C++17 fallthrough attribute is equivalent to GNU's. */
25196 else if (is_attribute_p ("fallthrough", attr_id))
25197 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25198 /* Transactional Memory TS optimize_for_synchronized attribute is
25199 equivalent to GNU transaction_callable. */
25200 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25201 TREE_PURPOSE (attribute)
25202 = get_identifier ("transaction_callable");
25203 /* Transactional Memory attributes are GNU attributes. */
25204 else if (tm_attr_to_mask (attr_id))
25205 TREE_PURPOSE (attribute) = attr_id;
25208 /* Now parse the optional argument clause of the attribute. */
25210 if (token->type != CPP_OPEN_PAREN)
25211 return attribute;
25214 vec<tree, va_gc> *vec;
25215 int attr_flag = normal_attr;
25217 if (attr_ns == get_identifier ("gnu")
25218 && attribute_takes_identifier_p (attr_id))
25219 /* A GNU attribute that takes an identifier in parameter. */
25220 attr_flag = id_attr;
25222 vec = cp_parser_parenthesized_expression_list
25223 (parser, attr_flag, /*cast_p=*/false,
25224 /*allow_expansion_p=*/true,
25225 /*non_constant_p=*/NULL);
25226 if (vec == NULL)
25227 arguments = error_mark_node;
25228 else
25230 arguments = build_tree_list_vec (vec);
25231 release_tree_vector (vec);
25234 if (arguments == error_mark_node)
25235 attribute = error_mark_node;
25236 else
25237 TREE_VALUE (attribute) = arguments;
25240 return attribute;
25243 /* Check that the attribute ATTRIBUTE appears at most once in the
25244 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25245 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25246 isn't implemented yet in GCC. */
25248 static void
25249 cp_parser_check_std_attribute (tree attributes, tree attribute)
25251 if (attributes)
25253 tree name = get_attribute_name (attribute);
25254 if (is_attribute_p ("noreturn", name)
25255 && lookup_attribute ("noreturn", attributes))
25256 error ("attribute %<noreturn%> can appear at most once "
25257 "in an attribute-list");
25258 else if (is_attribute_p ("deprecated", name)
25259 && lookup_attribute ("deprecated", attributes))
25260 error ("attribute %<deprecated%> can appear at most once "
25261 "in an attribute-list");
25265 /* Parse a list of standard C++-11 attributes.
25267 attribute-list:
25268 attribute [opt]
25269 attribute-list , attribute[opt]
25270 attribute ...
25271 attribute-list , attribute ...
25274 static tree
25275 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25277 tree attributes = NULL_TREE, attribute = NULL_TREE;
25278 cp_token *token = NULL;
25280 while (true)
25282 attribute = cp_parser_std_attribute (parser, attr_ns);
25283 if (attribute == error_mark_node)
25284 break;
25285 if (attribute != NULL_TREE)
25287 cp_parser_check_std_attribute (attributes, attribute);
25288 TREE_CHAIN (attribute) = attributes;
25289 attributes = attribute;
25291 token = cp_lexer_peek_token (parser->lexer);
25292 if (token->type == CPP_ELLIPSIS)
25294 cp_lexer_consume_token (parser->lexer);
25295 if (attribute == NULL_TREE)
25296 error_at (token->location,
25297 "expected attribute before %<...%>");
25298 else
25300 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25301 if (pack == error_mark_node)
25302 return error_mark_node;
25303 TREE_VALUE (attribute) = pack;
25305 token = cp_lexer_peek_token (parser->lexer);
25307 if (token->type != CPP_COMMA)
25308 break;
25309 cp_lexer_consume_token (parser->lexer);
25311 attributes = nreverse (attributes);
25312 return attributes;
25315 /* Parse a standard C++-11 attribute specifier.
25317 attribute-specifier:
25318 [ [ attribute-using-prefix [opt] attribute-list ] ]
25319 alignment-specifier
25321 attribute-using-prefix:
25322 using attribute-namespace :
25324 alignment-specifier:
25325 alignas ( type-id ... [opt] )
25326 alignas ( alignment-expression ... [opt] ). */
25328 static tree
25329 cp_parser_std_attribute_spec (cp_parser *parser)
25331 tree attributes = NULL_TREE;
25332 cp_token *token = cp_lexer_peek_token (parser->lexer);
25334 if (token->type == CPP_OPEN_SQUARE
25335 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25337 tree attr_ns = NULL_TREE;
25339 cp_lexer_consume_token (parser->lexer);
25340 cp_lexer_consume_token (parser->lexer);
25342 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25344 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25345 if (token->type == CPP_NAME)
25346 attr_ns = token->u.value;
25347 else if (token->type == CPP_KEYWORD)
25348 attr_ns = ridpointers[(int) token->keyword];
25349 else if (token->flags & NAMED_OP)
25350 attr_ns = get_identifier (cpp_type2name (token->type,
25351 token->flags));
25352 if (attr_ns
25353 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25355 if (cxx_dialect < cxx17
25356 && !in_system_header_at (input_location))
25357 pedwarn (input_location, 0,
25358 "attribute using prefix only available "
25359 "with -std=c++17 or -std=gnu++17");
25361 cp_lexer_consume_token (parser->lexer);
25362 cp_lexer_consume_token (parser->lexer);
25363 cp_lexer_consume_token (parser->lexer);
25365 else
25366 attr_ns = NULL_TREE;
25369 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25371 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25372 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25373 cp_parser_skip_to_end_of_statement (parser);
25374 else
25375 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25376 when we are sure that we have actually parsed them. */
25377 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25379 else
25381 tree alignas_expr;
25383 /* Look for an alignment-specifier. */
25385 token = cp_lexer_peek_token (parser->lexer);
25387 if (token->type != CPP_KEYWORD
25388 || token->keyword != RID_ALIGNAS)
25389 return NULL_TREE;
25391 cp_lexer_consume_token (parser->lexer);
25392 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25394 matching_parens parens;
25395 if (!parens.require_open (parser))
25396 return error_mark_node;
25398 cp_parser_parse_tentatively (parser);
25399 alignas_expr = cp_parser_type_id (parser);
25401 if (!cp_parser_parse_definitely (parser))
25403 alignas_expr = cp_parser_assignment_expression (parser);
25404 if (alignas_expr == error_mark_node)
25405 cp_parser_skip_to_end_of_statement (parser);
25406 if (alignas_expr == NULL_TREE
25407 || alignas_expr == error_mark_node)
25408 return alignas_expr;
25411 alignas_expr = cxx_alignas_expr (alignas_expr);
25412 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25414 /* Handle alignas (pack...). */
25415 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25417 cp_lexer_consume_token (parser->lexer);
25418 alignas_expr = make_pack_expansion (alignas_expr);
25421 /* Something went wrong, so don't build the attribute. */
25422 if (alignas_expr == error_mark_node)
25423 return error_mark_node;
25425 if (!parens.require_close (parser))
25426 return error_mark_node;
25428 /* Build the C++-11 representation of an 'aligned'
25429 attribute. */
25430 attributes =
25431 build_tree_list (build_tree_list (get_identifier ("gnu"),
25432 get_identifier ("aligned")),
25433 alignas_expr);
25436 return attributes;
25439 /* Parse a standard C++-11 attribute-specifier-seq.
25441 attribute-specifier-seq:
25442 attribute-specifier-seq [opt] attribute-specifier
25445 static tree
25446 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25448 tree attr_specs = NULL_TREE;
25449 tree attr_last = NULL_TREE;
25451 while (true)
25453 tree attr_spec = cp_parser_std_attribute_spec (parser);
25454 if (attr_spec == NULL_TREE)
25455 break;
25456 if (attr_spec == error_mark_node)
25457 return error_mark_node;
25459 if (attr_last)
25460 TREE_CHAIN (attr_last) = attr_spec;
25461 else
25462 attr_specs = attr_last = attr_spec;
25463 attr_last = tree_last (attr_last);
25466 return attr_specs;
25469 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25470 return index of the first token after balanced-token, or N on failure. */
25472 static size_t
25473 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25475 size_t orig_n = n;
25476 int nparens = 0, nbraces = 0, nsquares = 0;
25478 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25480 case CPP_EOF:
25481 case CPP_PRAGMA_EOL:
25482 /* Ran out of tokens. */
25483 return orig_n;
25484 case CPP_OPEN_PAREN:
25485 ++nparens;
25486 break;
25487 case CPP_OPEN_BRACE:
25488 ++nbraces;
25489 break;
25490 case CPP_OPEN_SQUARE:
25491 ++nsquares;
25492 break;
25493 case CPP_CLOSE_PAREN:
25494 --nparens;
25495 break;
25496 case CPP_CLOSE_BRACE:
25497 --nbraces;
25498 break;
25499 case CPP_CLOSE_SQUARE:
25500 --nsquares;
25501 break;
25502 default:
25503 break;
25505 while (nparens || nbraces || nsquares);
25506 return n;
25509 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25510 return index of the first token after the GNU attribute tokens, or N on
25511 failure. */
25513 static size_t
25514 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25516 while (true)
25518 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25519 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25520 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25521 break;
25523 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25524 if (n2 == n + 2)
25525 break;
25526 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25527 break;
25528 n = n2 + 1;
25530 return n;
25533 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25534 next token), return index of the first token after the standard C++11
25535 attribute tokens, or N on failure. */
25537 static size_t
25538 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25540 while (true)
25542 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25543 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25545 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25546 if (n2 == n + 1)
25547 break;
25548 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25549 break;
25550 n = n2 + 1;
25552 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25553 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25555 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25556 if (n2 == n + 1)
25557 break;
25558 n = n2;
25560 else
25561 break;
25563 return n;
25566 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25567 as the next token), return index of the first token after the attribute
25568 tokens, or N on failure. */
25570 static size_t
25571 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25573 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25574 return cp_parser_skip_gnu_attributes_opt (parser, n);
25575 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25578 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25579 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25580 current value of the PEDANTIC flag, regardless of whether or not
25581 the `__extension__' keyword is present. The caller is responsible
25582 for restoring the value of the PEDANTIC flag. */
25584 static bool
25585 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25587 /* Save the old value of the PEDANTIC flag. */
25588 *saved_pedantic = pedantic;
25590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25592 /* Consume the `__extension__' token. */
25593 cp_lexer_consume_token (parser->lexer);
25594 /* We're not being pedantic while the `__extension__' keyword is
25595 in effect. */
25596 pedantic = 0;
25598 return true;
25601 return false;
25604 /* Parse a label declaration.
25606 label-declaration:
25607 __label__ label-declarator-seq ;
25609 label-declarator-seq:
25610 identifier , label-declarator-seq
25611 identifier */
25613 static void
25614 cp_parser_label_declaration (cp_parser* parser)
25616 /* Look for the `__label__' keyword. */
25617 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25619 while (true)
25621 tree identifier;
25623 /* Look for an identifier. */
25624 identifier = cp_parser_identifier (parser);
25625 /* If we failed, stop. */
25626 if (identifier == error_mark_node)
25627 break;
25628 /* Declare it as a label. */
25629 finish_label_decl (identifier);
25630 /* If the next token is a `;', stop. */
25631 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25632 break;
25633 /* Look for the `,' separating the label declarations. */
25634 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25637 /* Look for the final `;'. */
25638 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25641 // -------------------------------------------------------------------------- //
25642 // Requires Clause
25644 // Parse a requires clause.
25646 // requires-clause:
25647 // 'requires' logical-or-expression
25649 // The required logical-or-expression must be a constant expression. Note
25650 // that we don't check that the expression is constepxr here. We defer until
25651 // we analyze constraints and then, we only check atomic constraints.
25652 static tree
25653 cp_parser_requires_clause (cp_parser *parser)
25655 // Parse the requires clause so that it is not automatically folded.
25656 ++processing_template_decl;
25657 tree expr = cp_parser_binary_expression (parser, false, false,
25658 PREC_NOT_OPERATOR, NULL);
25659 if (check_for_bare_parameter_packs (expr))
25660 expr = error_mark_node;
25661 --processing_template_decl;
25662 return expr;
25665 // Optionally parse a requires clause:
25666 static tree
25667 cp_parser_requires_clause_opt (cp_parser *parser)
25669 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25670 if (tok->keyword != RID_REQUIRES)
25672 if (!flag_concepts && tok->type == CPP_NAME
25673 && tok->u.value == ridpointers[RID_REQUIRES])
25675 error_at (cp_lexer_peek_token (parser->lexer)->location,
25676 "%<requires%> only available with -fconcepts");
25677 /* Parse and discard the requires-clause. */
25678 cp_lexer_consume_token (parser->lexer);
25679 cp_parser_requires_clause (parser);
25681 return NULL_TREE;
25683 cp_lexer_consume_token (parser->lexer);
25684 return cp_parser_requires_clause (parser);
25688 /*---------------------------------------------------------------------------
25689 Requires expressions
25690 ---------------------------------------------------------------------------*/
25692 /* Parse a requires expression
25694 requirement-expression:
25695 'requires' requirement-parameter-list [opt] requirement-body */
25696 static tree
25697 cp_parser_requires_expression (cp_parser *parser)
25699 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25700 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25702 /* A requires-expression shall appear only within a concept
25703 definition or a requires-clause.
25705 TODO: Implement this diagnostic correctly. */
25706 if (!processing_template_decl)
25708 error_at (loc, "a requires expression cannot appear outside a template");
25709 cp_parser_skip_to_end_of_statement (parser);
25710 return error_mark_node;
25713 tree parms, reqs;
25715 /* Local parameters are delared as variables within the scope
25716 of the expression. They are not visible past the end of
25717 the expression. Expressions within the requires-expression
25718 are unevaluated. */
25719 struct scope_sentinel
25721 scope_sentinel ()
25723 ++cp_unevaluated_operand;
25724 begin_scope (sk_block, NULL_TREE);
25727 ~scope_sentinel ()
25729 pop_bindings_and_leave_scope ();
25730 --cp_unevaluated_operand;
25732 } s;
25734 /* Parse the optional parameter list. */
25735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25737 parms = cp_parser_requirement_parameter_list (parser);
25738 if (parms == error_mark_node)
25739 return error_mark_node;
25741 else
25742 parms = NULL_TREE;
25744 /* Parse the requirement body. */
25745 reqs = cp_parser_requirement_body (parser);
25746 if (reqs == error_mark_node)
25747 return error_mark_node;
25750 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25751 the parm chain. */
25752 grokparms (parms, &parms);
25753 return finish_requires_expr (parms, reqs);
25756 /* Parse a parameterized requirement.
25758 requirement-parameter-list:
25759 '(' parameter-declaration-clause ')' */
25760 static tree
25761 cp_parser_requirement_parameter_list (cp_parser *parser)
25763 matching_parens parens;
25764 if (!parens.require_open (parser))
25765 return error_mark_node;
25767 tree parms = cp_parser_parameter_declaration_clause (parser);
25769 if (!parens.require_close (parser))
25770 return error_mark_node;
25772 return parms;
25775 /* Parse the body of a requirement.
25777 requirement-body:
25778 '{' requirement-list '}' */
25779 static tree
25780 cp_parser_requirement_body (cp_parser *parser)
25782 matching_braces braces;
25783 if (!braces.require_open (parser))
25784 return error_mark_node;
25786 tree reqs = cp_parser_requirement_list (parser);
25788 if (!braces.require_close (parser))
25789 return error_mark_node;
25791 return reqs;
25794 /* Parse a list of requirements.
25796 requirement-list:
25797 requirement
25798 requirement-list ';' requirement[opt] */
25799 static tree
25800 cp_parser_requirement_list (cp_parser *parser)
25802 tree result = NULL_TREE;
25803 while (true)
25805 tree req = cp_parser_requirement (parser);
25806 if (req == error_mark_node)
25807 return error_mark_node;
25809 result = tree_cons (NULL_TREE, req, result);
25811 /* If we see a semi-colon, consume it. */
25812 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25813 cp_lexer_consume_token (parser->lexer);
25815 /* Stop processing at the end of the list. */
25816 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25817 break;
25820 /* Reverse the order of requirements so they are analyzed in
25821 declaration order. */
25822 return nreverse (result);
25825 /* Parse a syntactic requirement or type requirement.
25827 requirement:
25828 simple-requirement
25829 compound-requirement
25830 type-requirement
25831 nested-requirement */
25832 static tree
25833 cp_parser_requirement (cp_parser *parser)
25835 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25836 return cp_parser_compound_requirement (parser);
25837 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25838 return cp_parser_type_requirement (parser);
25839 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25840 return cp_parser_nested_requirement (parser);
25841 else
25842 return cp_parser_simple_requirement (parser);
25845 /* Parse a simple requirement.
25847 simple-requirement:
25848 expression ';' */
25849 static tree
25850 cp_parser_simple_requirement (cp_parser *parser)
25852 tree expr = cp_parser_expression (parser, NULL, false, false);
25853 if (!expr || expr == error_mark_node)
25854 return error_mark_node;
25856 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25857 return error_mark_node;
25859 return finish_simple_requirement (expr);
25862 /* Parse a type requirement
25864 type-requirement
25865 nested-name-specifier [opt] required-type-name ';'
25867 required-type-name:
25868 type-name
25869 'template' [opt] simple-template-id */
25870 static tree
25871 cp_parser_type_requirement (cp_parser *parser)
25873 cp_lexer_consume_token (parser->lexer);
25875 // Save the scope before parsing name specifiers.
25876 tree saved_scope = parser->scope;
25877 tree saved_object_scope = parser->object_scope;
25878 tree saved_qualifying_scope = parser->qualifying_scope;
25879 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25880 cp_parser_nested_name_specifier_opt (parser,
25881 /*typename_keyword_p=*/true,
25882 /*check_dependency_p=*/false,
25883 /*type_p=*/true,
25884 /*is_declaration=*/false);
25886 tree type;
25887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25889 cp_lexer_consume_token (parser->lexer);
25890 type = cp_parser_template_id (parser,
25891 /*template_keyword_p=*/true,
25892 /*check_dependency=*/false,
25893 /*tag_type=*/none_type,
25894 /*is_declaration=*/false);
25895 type = make_typename_type (parser->scope, type, typename_type,
25896 /*complain=*/tf_error);
25898 else
25899 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25901 if (TREE_CODE (type) == TYPE_DECL)
25902 type = TREE_TYPE (type);
25904 parser->scope = saved_scope;
25905 parser->object_scope = saved_object_scope;
25906 parser->qualifying_scope = saved_qualifying_scope;
25908 if (type == error_mark_node)
25909 cp_parser_skip_to_end_of_statement (parser);
25911 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25912 return error_mark_node;
25913 if (type == error_mark_node)
25914 return error_mark_node;
25916 return finish_type_requirement (type);
25919 /* Parse a compound requirement
25921 compound-requirement:
25922 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25923 static tree
25924 cp_parser_compound_requirement (cp_parser *parser)
25926 /* Parse an expression enclosed in '{ }'s. */
25927 matching_braces braces;
25928 if (!braces.require_open (parser))
25929 return error_mark_node;
25931 tree expr = cp_parser_expression (parser, NULL, false, false);
25932 if (!expr || expr == error_mark_node)
25933 return error_mark_node;
25935 if (!braces.require_close (parser))
25936 return error_mark_node;
25938 /* Parse the optional noexcept. */
25939 bool noexcept_p = false;
25940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25942 cp_lexer_consume_token (parser->lexer);
25943 noexcept_p = true;
25946 /* Parse the optional trailing return type. */
25947 tree type = NULL_TREE;
25948 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25950 cp_lexer_consume_token (parser->lexer);
25951 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25952 parser->in_result_type_constraint_p = true;
25953 type = cp_parser_trailing_type_id (parser);
25954 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25955 if (type == error_mark_node)
25956 return error_mark_node;
25959 return finish_compound_requirement (expr, type, noexcept_p);
25962 /* Parse a nested requirement. This is the same as a requires clause.
25964 nested-requirement:
25965 requires-clause */
25966 static tree
25967 cp_parser_nested_requirement (cp_parser *parser)
25969 cp_lexer_consume_token (parser->lexer);
25970 tree req = cp_parser_requires_clause (parser);
25971 if (req == error_mark_node)
25972 return error_mark_node;
25973 return finish_nested_requirement (req);
25976 /* Support Functions */
25978 /* Return the appropriate prefer_type argument for lookup_name_real based on
25979 tag_type and template_mem_access. */
25981 static inline int
25982 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25984 /* DR 141: When looking in the current enclosing context for a template-name
25985 after -> or ., only consider class templates. */
25986 if (template_mem_access)
25987 return 2;
25988 switch (tag_type)
25990 case none_type: return 0; // No preference.
25991 case scope_type: return 1; // Type or namespace.
25992 default: return 2; // Type only.
25996 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25997 NAME should have one of the representations used for an
25998 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25999 is returned. If PARSER->SCOPE is a dependent type, then a
26000 SCOPE_REF is returned.
26002 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26003 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26004 was formed. Abstractly, such entities should not be passed to this
26005 function, because they do not need to be looked up, but it is
26006 simpler to check for this special case here, rather than at the
26007 call-sites.
26009 In cases not explicitly covered above, this function returns a
26010 DECL, OVERLOAD, or baselink representing the result of the lookup.
26011 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26012 is returned.
26014 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26015 (e.g., "struct") that was used. In that case bindings that do not
26016 refer to types are ignored.
26018 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26019 ignored.
26021 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26022 are ignored.
26024 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26025 types.
26027 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26028 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26029 NULL_TREE otherwise. */
26031 static cp_expr
26032 cp_parser_lookup_name (cp_parser *parser, tree name,
26033 enum tag_types tag_type,
26034 bool is_template,
26035 bool is_namespace,
26036 bool check_dependency,
26037 tree *ambiguous_decls,
26038 location_t name_location)
26040 tree decl;
26041 tree object_type = parser->context->object_type;
26043 /* Assume that the lookup will be unambiguous. */
26044 if (ambiguous_decls)
26045 *ambiguous_decls = NULL_TREE;
26047 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26048 no longer valid. Note that if we are parsing tentatively, and
26049 the parse fails, OBJECT_TYPE will be automatically restored. */
26050 parser->context->object_type = NULL_TREE;
26052 if (name == error_mark_node)
26053 return error_mark_node;
26055 /* A template-id has already been resolved; there is no lookup to
26056 do. */
26057 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26058 return name;
26059 if (BASELINK_P (name))
26061 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26062 == TEMPLATE_ID_EXPR);
26063 return name;
26066 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26067 it should already have been checked to make sure that the name
26068 used matches the type being destroyed. */
26069 if (TREE_CODE (name) == BIT_NOT_EXPR)
26071 tree type;
26073 /* Figure out to which type this destructor applies. */
26074 if (parser->scope)
26075 type = parser->scope;
26076 else if (object_type)
26077 type = object_type;
26078 else
26079 type = current_class_type;
26080 /* If that's not a class type, there is no destructor. */
26081 if (!type || !CLASS_TYPE_P (type))
26082 return error_mark_node;
26084 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26085 lazily_declare_fn (sfk_destructor, type);
26087 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26088 return dtor;
26090 return error_mark_node;
26093 /* By this point, the NAME should be an ordinary identifier. If
26094 the id-expression was a qualified name, the qualifying scope is
26095 stored in PARSER->SCOPE at this point. */
26096 gcc_assert (identifier_p (name));
26098 /* Perform the lookup. */
26099 if (parser->scope)
26101 bool dependent_p;
26103 if (parser->scope == error_mark_node)
26104 return error_mark_node;
26106 /* If the SCOPE is dependent, the lookup must be deferred until
26107 the template is instantiated -- unless we are explicitly
26108 looking up names in uninstantiated templates. Even then, we
26109 cannot look up the name if the scope is not a class type; it
26110 might, for example, be a template type parameter. */
26111 dependent_p = (TYPE_P (parser->scope)
26112 && dependent_scope_p (parser->scope));
26113 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26114 && dependent_p)
26115 /* Defer lookup. */
26116 decl = error_mark_node;
26117 else
26119 tree pushed_scope = NULL_TREE;
26121 /* If PARSER->SCOPE is a dependent type, then it must be a
26122 class type, and we must not be checking dependencies;
26123 otherwise, we would have processed this lookup above. So
26124 that PARSER->SCOPE is not considered a dependent base by
26125 lookup_member, we must enter the scope here. */
26126 if (dependent_p)
26127 pushed_scope = push_scope (parser->scope);
26129 /* If the PARSER->SCOPE is a template specialization, it
26130 may be instantiated during name lookup. In that case,
26131 errors may be issued. Even if we rollback the current
26132 tentative parse, those errors are valid. */
26133 decl = lookup_qualified_name (parser->scope, name,
26134 prefer_type_arg (tag_type),
26135 /*complain=*/true);
26137 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26138 lookup result and the nested-name-specifier nominates a class C:
26139 * if the name specified after the nested-name-specifier, when
26140 looked up in C, is the injected-class-name of C (Clause 9), or
26141 * if the name specified after the nested-name-specifier is the
26142 same as the identifier or the simple-template-id's template-
26143 name in the last component of the nested-name-specifier,
26144 the name is instead considered to name the constructor of
26145 class C. [ Note: for example, the constructor is not an
26146 acceptable lookup result in an elaborated-type-specifier so
26147 the constructor would not be used in place of the
26148 injected-class-name. --end note ] Such a constructor name
26149 shall be used only in the declarator-id of a declaration that
26150 names a constructor or in a using-declaration. */
26151 if (tag_type == none_type
26152 && DECL_SELF_REFERENCE_P (decl)
26153 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26154 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26155 prefer_type_arg (tag_type),
26156 /*complain=*/true);
26158 /* If we have a single function from a using decl, pull it out. */
26159 if (TREE_CODE (decl) == OVERLOAD
26160 && !really_overloaded_fn (decl))
26161 decl = OVL_FUNCTION (decl);
26163 if (pushed_scope)
26164 pop_scope (pushed_scope);
26167 /* If the scope is a dependent type and either we deferred lookup or
26168 we did lookup but didn't find the name, rememeber the name. */
26169 if (decl == error_mark_node && TYPE_P (parser->scope)
26170 && dependent_type_p (parser->scope))
26172 if (tag_type)
26174 tree type;
26176 /* The resolution to Core Issue 180 says that `struct
26177 A::B' should be considered a type-name, even if `A'
26178 is dependent. */
26179 type = make_typename_type (parser->scope, name, tag_type,
26180 /*complain=*/tf_error);
26181 if (type != error_mark_node)
26182 decl = TYPE_NAME (type);
26184 else if (is_template
26185 && (cp_parser_next_token_ends_template_argument_p (parser)
26186 || cp_lexer_next_token_is (parser->lexer,
26187 CPP_CLOSE_PAREN)))
26188 decl = make_unbound_class_template (parser->scope,
26189 name, NULL_TREE,
26190 /*complain=*/tf_error);
26191 else
26192 decl = build_qualified_name (/*type=*/NULL_TREE,
26193 parser->scope, name,
26194 is_template);
26196 parser->qualifying_scope = parser->scope;
26197 parser->object_scope = NULL_TREE;
26199 else if (object_type)
26201 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26202 OBJECT_TYPE is not a class. */
26203 if (CLASS_TYPE_P (object_type))
26204 /* If the OBJECT_TYPE is a template specialization, it may
26205 be instantiated during name lookup. In that case, errors
26206 may be issued. Even if we rollback the current tentative
26207 parse, those errors are valid. */
26208 decl = lookup_member (object_type,
26209 name,
26210 /*protect=*/0,
26211 prefer_type_arg (tag_type),
26212 tf_warning_or_error);
26213 else
26214 decl = NULL_TREE;
26216 if (!decl)
26217 /* Look it up in the enclosing context. DR 141: When looking for a
26218 template-name after -> or ., only consider class templates. */
26219 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26220 /*nonclass=*/0,
26221 /*block_p=*/true, is_namespace, 0);
26222 if (object_type == unknown_type_node)
26223 /* The object is type-dependent, so we can't look anything up; we used
26224 this to get the DR 141 behavior. */
26225 object_type = NULL_TREE;
26226 parser->object_scope = object_type;
26227 parser->qualifying_scope = NULL_TREE;
26229 else
26231 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26232 /*nonclass=*/0,
26233 /*block_p=*/true, is_namespace, 0);
26234 parser->qualifying_scope = NULL_TREE;
26235 parser->object_scope = NULL_TREE;
26238 /* If the lookup failed, let our caller know. */
26239 if (!decl || decl == error_mark_node)
26240 return error_mark_node;
26242 /* Pull out the template from an injected-class-name (or multiple). */
26243 if (is_template)
26244 decl = maybe_get_template_decl_from_type_decl (decl);
26246 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26247 if (TREE_CODE (decl) == TREE_LIST)
26249 if (ambiguous_decls)
26250 *ambiguous_decls = decl;
26251 /* The error message we have to print is too complicated for
26252 cp_parser_error, so we incorporate its actions directly. */
26253 if (!cp_parser_simulate_error (parser))
26255 error_at (name_location, "reference to %qD is ambiguous",
26256 name);
26257 print_candidates (decl);
26259 return error_mark_node;
26262 gcc_assert (DECL_P (decl)
26263 || TREE_CODE (decl) == OVERLOAD
26264 || TREE_CODE (decl) == SCOPE_REF
26265 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26266 || BASELINK_P (decl));
26268 /* If we have resolved the name of a member declaration, check to
26269 see if the declaration is accessible. When the name resolves to
26270 set of overloaded functions, accessibility is checked when
26271 overload resolution is done.
26273 During an explicit instantiation, access is not checked at all,
26274 as per [temp.explicit]. */
26275 if (DECL_P (decl))
26276 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26278 maybe_record_typedef_use (decl);
26280 return cp_expr (decl, name_location);
26283 /* Like cp_parser_lookup_name, but for use in the typical case where
26284 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26285 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26287 static tree
26288 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26290 return cp_parser_lookup_name (parser, name,
26291 none_type,
26292 /*is_template=*/false,
26293 /*is_namespace=*/false,
26294 /*check_dependency=*/true,
26295 /*ambiguous_decls=*/NULL,
26296 location);
26299 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26300 the current context, return the TYPE_DECL. If TAG_NAME_P is
26301 true, the DECL indicates the class being defined in a class-head,
26302 or declared in an elaborated-type-specifier.
26304 Otherwise, return DECL. */
26306 static tree
26307 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26309 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26310 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26312 struct A {
26313 template <typename T> struct B;
26316 template <typename T> struct A::B {};
26318 Similarly, in an elaborated-type-specifier:
26320 namespace N { struct X{}; }
26322 struct A {
26323 template <typename T> friend struct N::X;
26326 However, if the DECL refers to a class type, and we are in
26327 the scope of the class, then the name lookup automatically
26328 finds the TYPE_DECL created by build_self_reference rather
26329 than a TEMPLATE_DECL. For example, in:
26331 template <class T> struct S {
26332 S s;
26335 there is no need to handle such case. */
26337 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26338 return DECL_TEMPLATE_RESULT (decl);
26340 return decl;
26343 /* If too many, or too few, template-parameter lists apply to the
26344 declarator, issue an error message. Returns TRUE if all went well,
26345 and FALSE otherwise. */
26347 static bool
26348 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26349 cp_declarator *declarator,
26350 location_t declarator_location)
26352 switch (declarator->kind)
26354 case cdk_id:
26356 unsigned num_templates = 0;
26357 tree scope = declarator->u.id.qualifying_scope;
26359 if (scope)
26360 num_templates = num_template_headers_for_class (scope);
26361 else if (TREE_CODE (declarator->u.id.unqualified_name)
26362 == TEMPLATE_ID_EXPR)
26363 /* If the DECLARATOR has the form `X<y>' then it uses one
26364 additional level of template parameters. */
26365 ++num_templates;
26367 return cp_parser_check_template_parameters
26368 (parser, num_templates, declarator_location, declarator);
26371 case cdk_function:
26372 case cdk_array:
26373 case cdk_pointer:
26374 case cdk_reference:
26375 case cdk_ptrmem:
26376 return (cp_parser_check_declarator_template_parameters
26377 (parser, declarator->declarator, declarator_location));
26379 case cdk_decomp:
26380 case cdk_error:
26381 return true;
26383 default:
26384 gcc_unreachable ();
26386 return false;
26389 /* NUM_TEMPLATES were used in the current declaration. If that is
26390 invalid, return FALSE and issue an error messages. Otherwise,
26391 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26392 declarator and we can print more accurate diagnostics. */
26394 static bool
26395 cp_parser_check_template_parameters (cp_parser* parser,
26396 unsigned num_templates,
26397 location_t location,
26398 cp_declarator *declarator)
26400 /* If there are the same number of template classes and parameter
26401 lists, that's OK. */
26402 if (parser->num_template_parameter_lists == num_templates)
26403 return true;
26404 /* If there are more, but only one more, then we are referring to a
26405 member template. That's OK too. */
26406 if (parser->num_template_parameter_lists == num_templates + 1)
26407 return true;
26408 /* If there are more template classes than parameter lists, we have
26409 something like:
26411 template <class T> void S<T>::R<T>::f (); */
26412 if (parser->num_template_parameter_lists < num_templates)
26414 if (declarator && !current_function_decl)
26415 error_at (location, "specializing member %<%T::%E%> "
26416 "requires %<template<>%> syntax",
26417 declarator->u.id.qualifying_scope,
26418 declarator->u.id.unqualified_name);
26419 else if (declarator)
26420 error_at (location, "invalid declaration of %<%T::%E%>",
26421 declarator->u.id.qualifying_scope,
26422 declarator->u.id.unqualified_name);
26423 else
26424 error_at (location, "too few template-parameter-lists");
26425 return false;
26427 /* Otherwise, there are too many template parameter lists. We have
26428 something like:
26430 template <class T> template <class U> void S::f(); */
26431 error_at (location, "too many template-parameter-lists");
26432 return false;
26435 /* Parse an optional `::' token indicating that the following name is
26436 from the global namespace. If so, PARSER->SCOPE is set to the
26437 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26438 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26439 Returns the new value of PARSER->SCOPE, if the `::' token is
26440 present, and NULL_TREE otherwise. */
26442 static tree
26443 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26445 cp_token *token;
26447 /* Peek at the next token. */
26448 token = cp_lexer_peek_token (parser->lexer);
26449 /* If we're looking at a `::' token then we're starting from the
26450 global namespace, not our current location. */
26451 if (token->type == CPP_SCOPE)
26453 /* Consume the `::' token. */
26454 cp_lexer_consume_token (parser->lexer);
26455 /* Set the SCOPE so that we know where to start the lookup. */
26456 parser->scope = global_namespace;
26457 parser->qualifying_scope = global_namespace;
26458 parser->object_scope = NULL_TREE;
26460 return parser->scope;
26462 else if (!current_scope_valid_p)
26464 parser->scope = NULL_TREE;
26465 parser->qualifying_scope = NULL_TREE;
26466 parser->object_scope = NULL_TREE;
26469 return NULL_TREE;
26472 /* Returns TRUE if the upcoming token sequence is the start of a
26473 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26474 declarator is preceded by the `friend' specifier. */
26476 static bool
26477 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26479 bool constructor_p;
26480 bool outside_class_specifier_p;
26481 tree nested_name_specifier;
26482 cp_token *next_token;
26484 /* The common case is that this is not a constructor declarator, so
26485 try to avoid doing lots of work if at all possible. It's not
26486 valid declare a constructor at function scope. */
26487 if (parser->in_function_body)
26488 return false;
26489 /* And only certain tokens can begin a constructor declarator. */
26490 next_token = cp_lexer_peek_token (parser->lexer);
26491 if (next_token->type != CPP_NAME
26492 && next_token->type != CPP_SCOPE
26493 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26494 && next_token->type != CPP_TEMPLATE_ID)
26495 return false;
26497 /* Parse tentatively; we are going to roll back all of the tokens
26498 consumed here. */
26499 cp_parser_parse_tentatively (parser);
26500 /* Assume that we are looking at a constructor declarator. */
26501 constructor_p = true;
26503 /* Look for the optional `::' operator. */
26504 cp_parser_global_scope_opt (parser,
26505 /*current_scope_valid_p=*/false);
26506 /* Look for the nested-name-specifier. */
26507 nested_name_specifier
26508 = (cp_parser_nested_name_specifier_opt (parser,
26509 /*typename_keyword_p=*/false,
26510 /*check_dependency_p=*/false,
26511 /*type_p=*/false,
26512 /*is_declaration=*/false));
26514 outside_class_specifier_p = (!at_class_scope_p ()
26515 || !TYPE_BEING_DEFINED (current_class_type)
26516 || friend_p);
26518 /* Outside of a class-specifier, there must be a
26519 nested-name-specifier. Except in C++17 mode, where we
26520 might be declaring a guiding declaration. */
26521 if (!nested_name_specifier && outside_class_specifier_p
26522 && cxx_dialect < cxx17)
26523 constructor_p = false;
26524 else if (nested_name_specifier == error_mark_node)
26525 constructor_p = false;
26527 /* If we have a class scope, this is easy; DR 147 says that S::S always
26528 names the constructor, and no other qualified name could. */
26529 if (constructor_p && nested_name_specifier
26530 && CLASS_TYPE_P (nested_name_specifier))
26532 tree id = cp_parser_unqualified_id (parser,
26533 /*template_keyword_p=*/false,
26534 /*check_dependency_p=*/false,
26535 /*declarator_p=*/true,
26536 /*optional_p=*/false);
26537 if (is_overloaded_fn (id))
26538 id = DECL_NAME (get_first_fn (id));
26539 if (!constructor_name_p (id, nested_name_specifier))
26540 constructor_p = false;
26542 /* If we still think that this might be a constructor-declarator,
26543 look for a class-name. */
26544 else if (constructor_p)
26546 /* If we have:
26548 template <typename T> struct S {
26549 S();
26552 we must recognize that the nested `S' names a class. */
26553 if (cxx_dialect >= cxx17)
26554 cp_parser_parse_tentatively (parser);
26556 tree type_decl;
26557 type_decl = cp_parser_class_name (parser,
26558 /*typename_keyword_p=*/false,
26559 /*template_keyword_p=*/false,
26560 none_type,
26561 /*check_dependency_p=*/false,
26562 /*class_head_p=*/false,
26563 /*is_declaration=*/false);
26565 if (cxx_dialect >= cxx17
26566 && !cp_parser_parse_definitely (parser))
26568 type_decl = NULL_TREE;
26569 tree tmpl = cp_parser_template_name (parser,
26570 /*template_keyword*/false,
26571 /*check_dependency_p*/false,
26572 /*is_declaration*/false,
26573 none_type,
26574 /*is_identifier*/NULL);
26575 if (DECL_CLASS_TEMPLATE_P (tmpl)
26576 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26577 /* It's a deduction guide, return true. */;
26578 else
26579 cp_parser_simulate_error (parser);
26582 /* If there was no class-name, then this is not a constructor.
26583 Otherwise, if we are in a class-specifier and we aren't
26584 handling a friend declaration, check that its type matches
26585 current_class_type (c++/38313). Note: error_mark_node
26586 is left alone for error recovery purposes. */
26587 constructor_p = (!cp_parser_error_occurred (parser)
26588 && (outside_class_specifier_p
26589 || type_decl == NULL_TREE
26590 || type_decl == error_mark_node
26591 || same_type_p (current_class_type,
26592 TREE_TYPE (type_decl))));
26594 /* If we're still considering a constructor, we have to see a `(',
26595 to begin the parameter-declaration-clause, followed by either a
26596 `)', an `...', or a decl-specifier. We need to check for a
26597 type-specifier to avoid being fooled into thinking that:
26599 S (f) (int);
26601 is a constructor. (It is actually a function named `f' that
26602 takes one parameter (of type `int') and returns a value of type
26603 `S'. */
26604 if (constructor_p
26605 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26606 constructor_p = false;
26608 if (constructor_p
26609 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26610 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26611 /* A parameter declaration begins with a decl-specifier,
26612 which is either the "attribute" keyword, a storage class
26613 specifier, or (usually) a type-specifier. */
26614 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26616 tree type;
26617 tree pushed_scope = NULL_TREE;
26618 unsigned saved_num_template_parameter_lists;
26620 /* Names appearing in the type-specifier should be looked up
26621 in the scope of the class. */
26622 if (current_class_type)
26623 type = NULL_TREE;
26624 else if (type_decl)
26626 type = TREE_TYPE (type_decl);
26627 if (TREE_CODE (type) == TYPENAME_TYPE)
26629 type = resolve_typename_type (type,
26630 /*only_current_p=*/false);
26631 if (TREE_CODE (type) == TYPENAME_TYPE)
26633 cp_parser_abort_tentative_parse (parser);
26634 return false;
26637 pushed_scope = push_scope (type);
26640 /* Inside the constructor parameter list, surrounding
26641 template-parameter-lists do not apply. */
26642 saved_num_template_parameter_lists
26643 = parser->num_template_parameter_lists;
26644 parser->num_template_parameter_lists = 0;
26646 /* Look for the type-specifier. */
26647 cp_parser_type_specifier (parser,
26648 CP_PARSER_FLAGS_NONE,
26649 /*decl_specs=*/NULL,
26650 /*is_declarator=*/true,
26651 /*declares_class_or_enum=*/NULL,
26652 /*is_cv_qualifier=*/NULL);
26654 parser->num_template_parameter_lists
26655 = saved_num_template_parameter_lists;
26657 /* Leave the scope of the class. */
26658 if (pushed_scope)
26659 pop_scope (pushed_scope);
26661 constructor_p = !cp_parser_error_occurred (parser);
26665 /* We did not really want to consume any tokens. */
26666 cp_parser_abort_tentative_parse (parser);
26668 return constructor_p;
26671 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26672 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26673 they must be performed once we are in the scope of the function.
26675 Returns the function defined. */
26677 static tree
26678 cp_parser_function_definition_from_specifiers_and_declarator
26679 (cp_parser* parser,
26680 cp_decl_specifier_seq *decl_specifiers,
26681 tree attributes,
26682 const cp_declarator *declarator)
26684 tree fn;
26685 bool success_p;
26687 /* Begin the function-definition. */
26688 success_p = start_function (decl_specifiers, declarator, attributes);
26690 /* The things we're about to see are not directly qualified by any
26691 template headers we've seen thus far. */
26692 reset_specialization ();
26694 /* If there were names looked up in the decl-specifier-seq that we
26695 did not check, check them now. We must wait until we are in the
26696 scope of the function to perform the checks, since the function
26697 might be a friend. */
26698 perform_deferred_access_checks (tf_warning_or_error);
26700 if (success_p)
26702 cp_finalize_omp_declare_simd (parser, current_function_decl);
26703 parser->omp_declare_simd = NULL;
26704 cp_finalize_oacc_routine (parser, current_function_decl, true);
26705 parser->oacc_routine = NULL;
26708 if (!success_p)
26710 /* Skip the entire function. */
26711 cp_parser_skip_to_end_of_block_or_statement (parser);
26712 fn = error_mark_node;
26714 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26716 /* Seen already, skip it. An error message has already been output. */
26717 cp_parser_skip_to_end_of_block_or_statement (parser);
26718 fn = current_function_decl;
26719 current_function_decl = NULL_TREE;
26720 /* If this is a function from a class, pop the nested class. */
26721 if (current_class_name)
26722 pop_nested_class ();
26724 else
26726 timevar_id_t tv;
26727 if (DECL_DECLARED_INLINE_P (current_function_decl))
26728 tv = TV_PARSE_INLINE;
26729 else
26730 tv = TV_PARSE_FUNC;
26731 timevar_push (tv);
26732 fn = cp_parser_function_definition_after_declarator (parser,
26733 /*inline_p=*/false);
26734 timevar_pop (tv);
26737 return fn;
26740 /* Parse the part of a function-definition that follows the
26741 declarator. INLINE_P is TRUE iff this function is an inline
26742 function defined within a class-specifier.
26744 Returns the function defined. */
26746 static tree
26747 cp_parser_function_definition_after_declarator (cp_parser* parser,
26748 bool inline_p)
26750 tree fn;
26751 bool saved_in_unbraced_linkage_specification_p;
26752 bool saved_in_function_body;
26753 unsigned saved_num_template_parameter_lists;
26754 cp_token *token;
26755 bool fully_implicit_function_template_p
26756 = parser->fully_implicit_function_template_p;
26757 parser->fully_implicit_function_template_p = false;
26758 tree implicit_template_parms
26759 = parser->implicit_template_parms;
26760 parser->implicit_template_parms = 0;
26761 cp_binding_level* implicit_template_scope
26762 = parser->implicit_template_scope;
26763 parser->implicit_template_scope = 0;
26765 saved_in_function_body = parser->in_function_body;
26766 parser->in_function_body = true;
26767 /* If the next token is `return', then the code may be trying to
26768 make use of the "named return value" extension that G++ used to
26769 support. */
26770 token = cp_lexer_peek_token (parser->lexer);
26771 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26773 /* Consume the `return' keyword. */
26774 cp_lexer_consume_token (parser->lexer);
26775 /* Look for the identifier that indicates what value is to be
26776 returned. */
26777 cp_parser_identifier (parser);
26778 /* Issue an error message. */
26779 error_at (token->location,
26780 "named return values are no longer supported");
26781 /* Skip tokens until we reach the start of the function body. */
26782 while (true)
26784 cp_token *token = cp_lexer_peek_token (parser->lexer);
26785 if (token->type == CPP_OPEN_BRACE
26786 || token->type == CPP_EOF
26787 || token->type == CPP_PRAGMA_EOL)
26788 break;
26789 cp_lexer_consume_token (parser->lexer);
26792 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26793 anything declared inside `f'. */
26794 saved_in_unbraced_linkage_specification_p
26795 = parser->in_unbraced_linkage_specification_p;
26796 parser->in_unbraced_linkage_specification_p = false;
26797 /* Inside the function, surrounding template-parameter-lists do not
26798 apply. */
26799 saved_num_template_parameter_lists
26800 = parser->num_template_parameter_lists;
26801 parser->num_template_parameter_lists = 0;
26803 /* If the next token is `try', `__transaction_atomic', or
26804 `__transaction_relaxed`, then we are looking at either function-try-block
26805 or function-transaction-block. Note that all of these include the
26806 function-body. */
26807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26808 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26809 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26810 RID_TRANSACTION_RELAXED))
26811 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26812 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26813 cp_parser_function_try_block (parser);
26814 else
26815 cp_parser_ctor_initializer_opt_and_function_body
26816 (parser, /*in_function_try_block=*/false);
26818 /* Finish the function. */
26819 fn = finish_function (inline_p);
26820 /* Generate code for it, if necessary. */
26821 expand_or_defer_fn (fn);
26822 /* Restore the saved values. */
26823 parser->in_unbraced_linkage_specification_p
26824 = saved_in_unbraced_linkage_specification_p;
26825 parser->num_template_parameter_lists
26826 = saved_num_template_parameter_lists;
26827 parser->in_function_body = saved_in_function_body;
26829 parser->fully_implicit_function_template_p
26830 = fully_implicit_function_template_p;
26831 parser->implicit_template_parms
26832 = implicit_template_parms;
26833 parser->implicit_template_scope
26834 = implicit_template_scope;
26836 if (parser->fully_implicit_function_template_p)
26837 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26839 return fn;
26842 /* Parse a template-declaration body (following argument list). */
26844 static void
26845 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26846 tree parameter_list,
26847 bool member_p)
26849 tree decl = NULL_TREE;
26850 bool friend_p = false;
26852 /* We just processed one more parameter list. */
26853 ++parser->num_template_parameter_lists;
26855 /* Get the deferred access checks from the parameter list. These
26856 will be checked once we know what is being declared, as for a
26857 member template the checks must be performed in the scope of the
26858 class containing the member. */
26859 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26861 /* Tentatively parse for a new template parameter list, which can either be
26862 the template keyword or a template introduction. */
26863 if (cp_parser_template_declaration_after_export (parser, member_p))
26864 /* OK */;
26865 else if (cxx_dialect >= cxx11
26866 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26867 decl = cp_parser_alias_declaration (parser);
26868 else
26870 /* There are no access checks when parsing a template, as we do not
26871 know if a specialization will be a friend. */
26872 push_deferring_access_checks (dk_no_check);
26873 cp_token *token = cp_lexer_peek_token (parser->lexer);
26874 decl = cp_parser_single_declaration (parser,
26875 checks,
26876 member_p,
26877 /*explicit_specialization_p=*/false,
26878 &friend_p);
26879 pop_deferring_access_checks ();
26881 /* If this is a member template declaration, let the front
26882 end know. */
26883 if (member_p && !friend_p && decl)
26885 if (TREE_CODE (decl) == TYPE_DECL)
26886 cp_parser_check_access_in_redeclaration (decl, token->location);
26888 decl = finish_member_template_decl (decl);
26890 else if (friend_p && decl
26891 && DECL_DECLARES_TYPE_P (decl))
26892 make_friend_class (current_class_type, TREE_TYPE (decl),
26893 /*complain=*/true);
26895 /* We are done with the current parameter list. */
26896 --parser->num_template_parameter_lists;
26898 pop_deferring_access_checks ();
26900 /* Finish up. */
26901 finish_template_decl (parameter_list);
26903 /* Check the template arguments for a literal operator template. */
26904 if (decl
26905 && DECL_DECLARES_FUNCTION_P (decl)
26906 && UDLIT_OPER_P (DECL_NAME (decl)))
26908 bool ok = true;
26909 if (parameter_list == NULL_TREE)
26910 ok = false;
26911 else
26913 int num_parms = TREE_VEC_LENGTH (parameter_list);
26914 if (num_parms == 1)
26916 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26917 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26918 if (TREE_TYPE (parm) != char_type_node
26919 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26920 ok = false;
26922 else if (num_parms == 2 && cxx_dialect >= cxx14)
26924 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26925 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26926 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26927 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26928 if (parm == error_mark_node
26929 || TREE_TYPE (parm) != TREE_TYPE (type)
26930 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26931 ok = false;
26933 else
26934 ok = false;
26936 if (!ok)
26938 if (cxx_dialect >= cxx14)
26939 error ("literal operator template %qD has invalid parameter list."
26940 " Expected non-type template argument pack <char...>"
26941 " or <typename CharT, CharT...>",
26942 decl);
26943 else
26944 error ("literal operator template %qD has invalid parameter list."
26945 " Expected non-type template argument pack <char...>",
26946 decl);
26950 /* Register member declarations. */
26951 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26952 finish_member_declaration (decl);
26953 /* If DECL is a function template, we must return to parse it later.
26954 (Even though there is no definition, there might be default
26955 arguments that need handling.) */
26956 if (member_p && decl
26957 && DECL_DECLARES_FUNCTION_P (decl))
26958 vec_safe_push (unparsed_funs_with_definitions, decl);
26961 /* Parse a template introduction header for a template-declaration. Returns
26962 false if tentative parse fails. */
26964 static bool
26965 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26967 cp_parser_parse_tentatively (parser);
26969 tree saved_scope = parser->scope;
26970 tree saved_object_scope = parser->object_scope;
26971 tree saved_qualifying_scope = parser->qualifying_scope;
26973 /* Look for the optional `::' operator. */
26974 cp_parser_global_scope_opt (parser,
26975 /*current_scope_valid_p=*/false);
26976 /* Look for the nested-name-specifier. */
26977 cp_parser_nested_name_specifier_opt (parser,
26978 /*typename_keyword_p=*/false,
26979 /*check_dependency_p=*/true,
26980 /*type_p=*/false,
26981 /*is_declaration=*/false);
26983 cp_token *token = cp_lexer_peek_token (parser->lexer);
26984 tree concept_name = cp_parser_identifier (parser);
26986 /* Look up the concept for which we will be matching
26987 template parameters. */
26988 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26989 token->location);
26990 parser->scope = saved_scope;
26991 parser->object_scope = saved_object_scope;
26992 parser->qualifying_scope = saved_qualifying_scope;
26994 if (concept_name == error_mark_node)
26995 cp_parser_simulate_error (parser);
26997 /* Look for opening brace for introduction. */
26998 matching_braces braces;
26999 braces.require_open (parser);
27001 if (!cp_parser_parse_definitely (parser))
27002 return false;
27004 push_deferring_access_checks (dk_deferred);
27006 /* Build vector of placeholder parameters and grab
27007 matching identifiers. */
27008 tree introduction_list = cp_parser_introduction_list (parser);
27010 /* The introduction-list shall not be empty. */
27011 int nargs = TREE_VEC_LENGTH (introduction_list);
27012 if (nargs == 0)
27014 error ("empty introduction-list");
27015 return true;
27018 /* Look for closing brace for introduction. */
27019 if (!braces.require_close (parser))
27020 return true;
27022 if (tmpl_decl == error_mark_node)
27024 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27025 token->location);
27026 return true;
27029 /* Build and associate the constraint. */
27030 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27031 if (parms && parms != error_mark_node)
27033 cp_parser_template_declaration_after_parameters (parser, parms,
27034 member_p);
27035 return true;
27038 error_at (token->location, "no matching concept for template-introduction");
27039 return true;
27042 /* Parse a normal template-declaration following the template keyword. */
27044 static void
27045 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27047 tree parameter_list;
27048 bool need_lang_pop;
27049 location_t location = input_location;
27051 /* Look for the `<' token. */
27052 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27053 return;
27054 if (at_class_scope_p () && current_function_decl)
27056 /* 14.5.2.2 [temp.mem]
27058 A local class shall not have member templates. */
27059 error_at (location,
27060 "invalid declaration of member template in local class");
27061 cp_parser_skip_to_end_of_block_or_statement (parser);
27062 return;
27064 /* [temp]
27066 A template ... shall not have C linkage. */
27067 if (current_lang_name == lang_name_c)
27069 error_at (location, "template with C linkage");
27070 maybe_show_extern_c_location ();
27071 /* Give it C++ linkage to avoid confusing other parts of the
27072 front end. */
27073 push_lang_context (lang_name_cplusplus);
27074 need_lang_pop = true;
27076 else
27077 need_lang_pop = false;
27079 /* We cannot perform access checks on the template parameter
27080 declarations until we know what is being declared, just as we
27081 cannot check the decl-specifier list. */
27082 push_deferring_access_checks (dk_deferred);
27084 /* If the next token is `>', then we have an invalid
27085 specialization. Rather than complain about an invalid template
27086 parameter, issue an error message here. */
27087 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27089 cp_parser_error (parser, "invalid explicit specialization");
27090 begin_specialization ();
27091 parameter_list = NULL_TREE;
27093 else
27095 /* Parse the template parameters. */
27096 parameter_list = cp_parser_template_parameter_list (parser);
27099 /* Look for the `>'. */
27100 cp_parser_skip_to_end_of_template_parameter_list (parser);
27102 /* Manage template requirements */
27103 if (flag_concepts)
27105 tree reqs = get_shorthand_constraints (current_template_parms);
27106 if (tree r = cp_parser_requires_clause_opt (parser))
27107 reqs = conjoin_constraints (reqs, normalize_expression (r));
27108 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27111 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27112 member_p);
27114 /* For the erroneous case of a template with C linkage, we pushed an
27115 implicit C++ linkage scope; exit that scope now. */
27116 if (need_lang_pop)
27117 pop_lang_context ();
27120 /* Parse a template-declaration, assuming that the `export' (and
27121 `extern') keywords, if present, has already been scanned. MEMBER_P
27122 is as for cp_parser_template_declaration. */
27124 static bool
27125 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27127 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27129 cp_lexer_consume_token (parser->lexer);
27130 cp_parser_explicit_template_declaration (parser, member_p);
27131 return true;
27133 else if (flag_concepts)
27134 return cp_parser_template_introduction (parser, member_p);
27136 return false;
27139 /* Perform the deferred access checks from a template-parameter-list.
27140 CHECKS is a TREE_LIST of access checks, as returned by
27141 get_deferred_access_checks. */
27143 static void
27144 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27146 ++processing_template_parmlist;
27147 perform_access_checks (checks, tf_warning_or_error);
27148 --processing_template_parmlist;
27151 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27152 `function-definition' sequence that follows a template header.
27153 If MEMBER_P is true, this declaration appears in a class scope.
27155 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27156 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27158 static tree
27159 cp_parser_single_declaration (cp_parser* parser,
27160 vec<deferred_access_check, va_gc> *checks,
27161 bool member_p,
27162 bool explicit_specialization_p,
27163 bool* friend_p)
27165 int declares_class_or_enum;
27166 tree decl = NULL_TREE;
27167 cp_decl_specifier_seq decl_specifiers;
27168 bool function_definition_p = false;
27169 cp_token *decl_spec_token_start;
27171 /* This function is only used when processing a template
27172 declaration. */
27173 gcc_assert (innermost_scope_kind () == sk_template_parms
27174 || innermost_scope_kind () == sk_template_spec);
27176 /* Defer access checks until we know what is being declared. */
27177 push_deferring_access_checks (dk_deferred);
27179 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27180 alternative. */
27181 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27182 cp_parser_decl_specifier_seq (parser,
27183 CP_PARSER_FLAGS_OPTIONAL,
27184 &decl_specifiers,
27185 &declares_class_or_enum);
27186 if (friend_p)
27187 *friend_p = cp_parser_friend_p (&decl_specifiers);
27189 /* There are no template typedefs. */
27190 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27192 error_at (decl_spec_token_start->location,
27193 "template declaration of %<typedef%>");
27194 decl = error_mark_node;
27197 /* Gather up the access checks that occurred the
27198 decl-specifier-seq. */
27199 stop_deferring_access_checks ();
27201 /* Check for the declaration of a template class. */
27202 if (declares_class_or_enum)
27204 if (cp_parser_declares_only_class_p (parser)
27205 || (declares_class_or_enum & 2))
27207 // If this is a declaration, but not a definition, associate
27208 // any constraints with the type declaration. Constraints
27209 // are associated with definitions in cp_parser_class_specifier.
27210 if (declares_class_or_enum == 1)
27211 associate_classtype_constraints (decl_specifiers.type);
27213 decl = shadow_tag (&decl_specifiers);
27215 /* In this case:
27217 struct C {
27218 friend template <typename T> struct A<T>::B;
27221 A<T>::B will be represented by a TYPENAME_TYPE, and
27222 therefore not recognized by shadow_tag. */
27223 if (friend_p && *friend_p
27224 && !decl
27225 && decl_specifiers.type
27226 && TYPE_P (decl_specifiers.type))
27227 decl = decl_specifiers.type;
27229 if (decl && decl != error_mark_node)
27230 decl = TYPE_NAME (decl);
27231 else
27232 decl = error_mark_node;
27234 /* Perform access checks for template parameters. */
27235 cp_parser_perform_template_parameter_access_checks (checks);
27237 /* Give a helpful diagnostic for
27238 template <class T> struct A { } a;
27239 if we aren't already recovering from an error. */
27240 if (!cp_parser_declares_only_class_p (parser)
27241 && !seen_error ())
27243 error_at (cp_lexer_peek_token (parser->lexer)->location,
27244 "a class template declaration must not declare "
27245 "anything else");
27246 cp_parser_skip_to_end_of_block_or_statement (parser);
27247 goto out;
27252 /* Complain about missing 'typename' or other invalid type names. */
27253 if (!decl_specifiers.any_type_specifiers_p
27254 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27256 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27257 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27258 the rest of this declaration. */
27259 decl = error_mark_node;
27260 goto out;
27263 /* If it's not a template class, try for a template function. If
27264 the next token is a `;', then this declaration does not declare
27265 anything. But, if there were errors in the decl-specifiers, then
27266 the error might well have come from an attempted class-specifier.
27267 In that case, there's no need to warn about a missing declarator. */
27268 if (!decl
27269 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27270 || decl_specifiers.type != error_mark_node))
27272 decl = cp_parser_init_declarator (parser,
27273 &decl_specifiers,
27274 checks,
27275 /*function_definition_allowed_p=*/true,
27276 member_p,
27277 declares_class_or_enum,
27278 &function_definition_p,
27279 NULL, NULL, NULL);
27281 /* 7.1.1-1 [dcl.stc]
27283 A storage-class-specifier shall not be specified in an explicit
27284 specialization... */
27285 if (decl
27286 && explicit_specialization_p
27287 && decl_specifiers.storage_class != sc_none)
27289 error_at (decl_spec_token_start->location,
27290 "explicit template specialization cannot have a storage class");
27291 decl = error_mark_node;
27294 if (decl && VAR_P (decl))
27295 check_template_variable (decl);
27298 /* Look for a trailing `;' after the declaration. */
27299 if (!function_definition_p
27300 && (decl == error_mark_node
27301 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27302 cp_parser_skip_to_end_of_block_or_statement (parser);
27304 out:
27305 pop_deferring_access_checks ();
27307 /* Clear any current qualification; whatever comes next is the start
27308 of something new. */
27309 parser->scope = NULL_TREE;
27310 parser->qualifying_scope = NULL_TREE;
27311 parser->object_scope = NULL_TREE;
27313 return decl;
27316 /* Parse a cast-expression that is not the operand of a unary "&". */
27318 static cp_expr
27319 cp_parser_simple_cast_expression (cp_parser *parser)
27321 return cp_parser_cast_expression (parser, /*address_p=*/false,
27322 /*cast_p=*/false, /*decltype*/false, NULL);
27325 /* Parse a functional cast to TYPE. Returns an expression
27326 representing the cast. */
27328 static cp_expr
27329 cp_parser_functional_cast (cp_parser* parser, tree type)
27331 vec<tree, va_gc> *vec;
27332 tree expression_list;
27333 cp_expr cast;
27334 bool nonconst_p;
27336 location_t start_loc = input_location;
27338 if (!type)
27339 type = error_mark_node;
27341 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27343 cp_lexer_set_source_position (parser->lexer);
27344 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27345 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27346 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27347 if (TREE_CODE (type) == TYPE_DECL)
27348 type = TREE_TYPE (type);
27350 cast = finish_compound_literal (type, expression_list,
27351 tf_warning_or_error, fcl_functional);
27352 /* Create a location of the form:
27353 type_name{i, f}
27354 ^~~~~~~~~~~~~~~
27355 with caret == start at the start of the type name,
27356 finishing at the closing brace. */
27357 location_t finish_loc
27358 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27359 location_t combined_loc = make_location (start_loc, start_loc,
27360 finish_loc);
27361 cast.set_location (combined_loc);
27362 return cast;
27366 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27367 /*cast_p=*/true,
27368 /*allow_expansion_p=*/true,
27369 /*non_constant_p=*/NULL);
27370 if (vec == NULL)
27371 expression_list = error_mark_node;
27372 else
27374 expression_list = build_tree_list_vec (vec);
27375 release_tree_vector (vec);
27378 cast = build_functional_cast (type, expression_list,
27379 tf_warning_or_error);
27380 /* [expr.const]/1: In an integral constant expression "only type
27381 conversions to integral or enumeration type can be used". */
27382 if (TREE_CODE (type) == TYPE_DECL)
27383 type = TREE_TYPE (type);
27384 if (cast != error_mark_node
27385 && !cast_valid_in_integral_constant_expression_p (type)
27386 && cp_parser_non_integral_constant_expression (parser,
27387 NIC_CONSTRUCTOR))
27388 return error_mark_node;
27390 /* Create a location of the form:
27391 float(i)
27392 ^~~~~~~~
27393 with caret == start at the start of the type name,
27394 finishing at the closing paren. */
27395 location_t finish_loc
27396 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27397 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27398 cast.set_location (combined_loc);
27399 return cast;
27402 /* Save the tokens that make up the body of a member function defined
27403 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27404 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27405 specifiers applied to the declaration. Returns the FUNCTION_DECL
27406 for the member function. */
27408 static tree
27409 cp_parser_save_member_function_body (cp_parser* parser,
27410 cp_decl_specifier_seq *decl_specifiers,
27411 cp_declarator *declarator,
27412 tree attributes)
27414 cp_token *first;
27415 cp_token *last;
27416 tree fn;
27417 bool function_try_block = false;
27419 /* Create the FUNCTION_DECL. */
27420 fn = grokmethod (decl_specifiers, declarator, attributes);
27421 cp_finalize_omp_declare_simd (parser, fn);
27422 cp_finalize_oacc_routine (parser, fn, true);
27423 /* If something went badly wrong, bail out now. */
27424 if (fn == error_mark_node)
27426 /* If there's a function-body, skip it. */
27427 if (cp_parser_token_starts_function_definition_p
27428 (cp_lexer_peek_token (parser->lexer)))
27429 cp_parser_skip_to_end_of_block_or_statement (parser);
27430 return error_mark_node;
27433 /* Remember it, if there default args to post process. */
27434 cp_parser_save_default_args (parser, fn);
27436 /* Save away the tokens that make up the body of the
27437 function. */
27438 first = parser->lexer->next_token;
27440 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27441 cp_lexer_consume_token (parser->lexer);
27442 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27443 RID_TRANSACTION_ATOMIC))
27445 cp_lexer_consume_token (parser->lexer);
27446 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27448 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27449 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27450 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27451 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27452 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27454 cp_lexer_consume_token (parser->lexer);
27455 cp_lexer_consume_token (parser->lexer);
27456 cp_lexer_consume_token (parser->lexer);
27457 cp_lexer_consume_token (parser->lexer);
27458 cp_lexer_consume_token (parser->lexer);
27460 else
27461 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27462 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27464 cp_lexer_consume_token (parser->lexer);
27465 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27466 break;
27470 /* Handle function try blocks. */
27471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27473 cp_lexer_consume_token (parser->lexer);
27474 function_try_block = true;
27476 /* We can have braced-init-list mem-initializers before the fn body. */
27477 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27479 cp_lexer_consume_token (parser->lexer);
27480 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27482 /* cache_group will stop after an un-nested { } pair, too. */
27483 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27484 break;
27486 /* variadic mem-inits have ... after the ')'. */
27487 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27488 cp_lexer_consume_token (parser->lexer);
27491 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27492 /* Handle function try blocks. */
27493 if (function_try_block)
27494 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27495 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27496 last = parser->lexer->next_token;
27498 /* Save away the inline definition; we will process it when the
27499 class is complete. */
27500 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27501 DECL_PENDING_INLINE_P (fn) = 1;
27503 /* We need to know that this was defined in the class, so that
27504 friend templates are handled correctly. */
27505 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27507 /* Add FN to the queue of functions to be parsed later. */
27508 vec_safe_push (unparsed_funs_with_definitions, fn);
27510 return fn;
27513 /* Save the tokens that make up the in-class initializer for a non-static
27514 data member. Returns a DEFAULT_ARG. */
27516 static tree
27517 cp_parser_save_nsdmi (cp_parser* parser)
27519 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27522 /* Parse a template-argument-list, as well as the trailing ">" (but
27523 not the opening "<"). See cp_parser_template_argument_list for the
27524 return value. */
27526 static tree
27527 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27529 tree arguments;
27530 tree saved_scope;
27531 tree saved_qualifying_scope;
27532 tree saved_object_scope;
27533 bool saved_greater_than_is_operator_p;
27534 int saved_unevaluated_operand;
27535 int saved_inhibit_evaluation_warnings;
27537 /* [temp.names]
27539 When parsing a template-id, the first non-nested `>' is taken as
27540 the end of the template-argument-list rather than a greater-than
27541 operator. */
27542 saved_greater_than_is_operator_p
27543 = parser->greater_than_is_operator_p;
27544 parser->greater_than_is_operator_p = false;
27545 /* Parsing the argument list may modify SCOPE, so we save it
27546 here. */
27547 saved_scope = parser->scope;
27548 saved_qualifying_scope = parser->qualifying_scope;
27549 saved_object_scope = parser->object_scope;
27550 /* We need to evaluate the template arguments, even though this
27551 template-id may be nested within a "sizeof". */
27552 saved_unevaluated_operand = cp_unevaluated_operand;
27553 cp_unevaluated_operand = 0;
27554 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27555 c_inhibit_evaluation_warnings = 0;
27556 /* Parse the template-argument-list itself. */
27557 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27558 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27559 arguments = NULL_TREE;
27560 else
27561 arguments = cp_parser_template_argument_list (parser);
27562 /* Look for the `>' that ends the template-argument-list. If we find
27563 a '>>' instead, it's probably just a typo. */
27564 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27566 if (cxx_dialect != cxx98)
27568 /* In C++0x, a `>>' in a template argument list or cast
27569 expression is considered to be two separate `>'
27570 tokens. So, change the current token to a `>', but don't
27571 consume it: it will be consumed later when the outer
27572 template argument list (or cast expression) is parsed.
27573 Note that this replacement of `>' for `>>' is necessary
27574 even if we are parsing tentatively: in the tentative
27575 case, after calling
27576 cp_parser_enclosed_template_argument_list we will always
27577 throw away all of the template arguments and the first
27578 closing `>', either because the template argument list
27579 was erroneous or because we are replacing those tokens
27580 with a CPP_TEMPLATE_ID token. The second `>' (which will
27581 not have been thrown away) is needed either to close an
27582 outer template argument list or to complete a new-style
27583 cast. */
27584 cp_token *token = cp_lexer_peek_token (parser->lexer);
27585 token->type = CPP_GREATER;
27587 else if (!saved_greater_than_is_operator_p)
27589 /* If we're in a nested template argument list, the '>>' has
27590 to be a typo for '> >'. We emit the error message, but we
27591 continue parsing and we push a '>' as next token, so that
27592 the argument list will be parsed correctly. Note that the
27593 global source location is still on the token before the
27594 '>>', so we need to say explicitly where we want it. */
27595 cp_token *token = cp_lexer_peek_token (parser->lexer);
27596 gcc_rich_location richloc (token->location);
27597 richloc.add_fixit_replace ("> >");
27598 error_at (&richloc, "%<>>%> should be %<> >%> "
27599 "within a nested template argument list");
27601 token->type = CPP_GREATER;
27603 else
27605 /* If this is not a nested template argument list, the '>>'
27606 is a typo for '>'. Emit an error message and continue.
27607 Same deal about the token location, but here we can get it
27608 right by consuming the '>>' before issuing the diagnostic. */
27609 cp_token *token = cp_lexer_consume_token (parser->lexer);
27610 error_at (token->location,
27611 "spurious %<>>%>, use %<>%> to terminate "
27612 "a template argument list");
27615 else
27616 cp_parser_skip_to_end_of_template_parameter_list (parser);
27617 /* The `>' token might be a greater-than operator again now. */
27618 parser->greater_than_is_operator_p
27619 = saved_greater_than_is_operator_p;
27620 /* Restore the SAVED_SCOPE. */
27621 parser->scope = saved_scope;
27622 parser->qualifying_scope = saved_qualifying_scope;
27623 parser->object_scope = saved_object_scope;
27624 cp_unevaluated_operand = saved_unevaluated_operand;
27625 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27627 return arguments;
27630 /* MEMBER_FUNCTION is a member function, or a friend. If default
27631 arguments, or the body of the function have not yet been parsed,
27632 parse them now. */
27634 static void
27635 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27637 timevar_push (TV_PARSE_INMETH);
27638 /* If this member is a template, get the underlying
27639 FUNCTION_DECL. */
27640 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27641 member_function = DECL_TEMPLATE_RESULT (member_function);
27643 /* There should not be any class definitions in progress at this
27644 point; the bodies of members are only parsed outside of all class
27645 definitions. */
27646 gcc_assert (parser->num_classes_being_defined == 0);
27647 /* While we're parsing the member functions we might encounter more
27648 classes. We want to handle them right away, but we don't want
27649 them getting mixed up with functions that are currently in the
27650 queue. */
27651 push_unparsed_function_queues (parser);
27653 /* Make sure that any template parameters are in scope. */
27654 maybe_begin_member_template_processing (member_function);
27656 /* If the body of the function has not yet been parsed, parse it
27657 now. */
27658 if (DECL_PENDING_INLINE_P (member_function))
27660 tree function_scope;
27661 cp_token_cache *tokens;
27663 /* The function is no longer pending; we are processing it. */
27664 tokens = DECL_PENDING_INLINE_INFO (member_function);
27665 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27666 DECL_PENDING_INLINE_P (member_function) = 0;
27668 /* If this is a local class, enter the scope of the containing
27669 function. */
27670 function_scope = current_function_decl;
27671 if (function_scope)
27672 push_function_context ();
27674 /* Push the body of the function onto the lexer stack. */
27675 cp_parser_push_lexer_for_tokens (parser, tokens);
27677 /* Let the front end know that we going to be defining this
27678 function. */
27679 start_preparsed_function (member_function, NULL_TREE,
27680 SF_PRE_PARSED | SF_INCLASS_INLINE);
27682 /* Don't do access checking if it is a templated function. */
27683 if (processing_template_decl)
27684 push_deferring_access_checks (dk_no_check);
27686 /* #pragma omp declare reduction needs special parsing. */
27687 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27689 parser->lexer->in_pragma = true;
27690 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27691 finish_function (/*inline_p=*/true);
27692 cp_check_omp_declare_reduction (member_function);
27694 else
27695 /* Now, parse the body of the function. */
27696 cp_parser_function_definition_after_declarator (parser,
27697 /*inline_p=*/true);
27699 if (processing_template_decl)
27700 pop_deferring_access_checks ();
27702 /* Leave the scope of the containing function. */
27703 if (function_scope)
27704 pop_function_context ();
27705 cp_parser_pop_lexer (parser);
27708 /* Remove any template parameters from the symbol table. */
27709 maybe_end_member_template_processing ();
27711 /* Restore the queue. */
27712 pop_unparsed_function_queues (parser);
27713 timevar_pop (TV_PARSE_INMETH);
27716 /* If DECL contains any default args, remember it on the unparsed
27717 functions queue. */
27719 static void
27720 cp_parser_save_default_args (cp_parser* parser, tree decl)
27722 tree probe;
27724 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27725 probe;
27726 probe = TREE_CHAIN (probe))
27727 if (TREE_PURPOSE (probe))
27729 cp_default_arg_entry entry = {current_class_type, decl};
27730 vec_safe_push (unparsed_funs_with_default_args, entry);
27731 break;
27735 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27736 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27737 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27738 from the parameter-type-list. */
27740 static tree
27741 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27742 tree default_arg, tree parmtype)
27744 cp_token_cache *tokens;
27745 tree parsed_arg;
27746 bool dummy;
27748 if (default_arg == error_mark_node)
27749 return error_mark_node;
27751 /* Push the saved tokens for the default argument onto the parser's
27752 lexer stack. */
27753 tokens = DEFARG_TOKENS (default_arg);
27754 cp_parser_push_lexer_for_tokens (parser, tokens);
27756 start_lambda_scope (decl);
27758 /* Parse the default argument. */
27759 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27760 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27761 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27763 finish_lambda_scope ();
27765 if (parsed_arg == error_mark_node)
27766 cp_parser_skip_to_end_of_statement (parser);
27768 if (!processing_template_decl)
27770 /* In a non-template class, check conversions now. In a template,
27771 we'll wait and instantiate these as needed. */
27772 if (TREE_CODE (decl) == PARM_DECL)
27773 parsed_arg = check_default_argument (parmtype, parsed_arg,
27774 tf_warning_or_error);
27775 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27776 parsed_arg = error_mark_node;
27777 else
27778 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27781 /* If the token stream has not been completely used up, then
27782 there was extra junk after the end of the default
27783 argument. */
27784 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27786 if (TREE_CODE (decl) == PARM_DECL)
27787 cp_parser_error (parser, "expected %<,%>");
27788 else
27789 cp_parser_error (parser, "expected %<;%>");
27792 /* Revert to the main lexer. */
27793 cp_parser_pop_lexer (parser);
27795 return parsed_arg;
27798 /* FIELD is a non-static data member with an initializer which we saved for
27799 later; parse it now. */
27801 static void
27802 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27804 tree def;
27806 maybe_begin_member_template_processing (field);
27808 push_unparsed_function_queues (parser);
27809 def = cp_parser_late_parse_one_default_arg (parser, field,
27810 DECL_INITIAL (field),
27811 NULL_TREE);
27812 pop_unparsed_function_queues (parser);
27814 maybe_end_member_template_processing ();
27816 DECL_INITIAL (field) = def;
27819 /* FN is a FUNCTION_DECL which may contains a parameter with an
27820 unparsed DEFAULT_ARG. Parse the default args now. This function
27821 assumes that the current scope is the scope in which the default
27822 argument should be processed. */
27824 static void
27825 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27827 bool saved_local_variables_forbidden_p;
27828 tree parm, parmdecl;
27830 /* While we're parsing the default args, we might (due to the
27831 statement expression extension) encounter more classes. We want
27832 to handle them right away, but we don't want them getting mixed
27833 up with default args that are currently in the queue. */
27834 push_unparsed_function_queues (parser);
27836 /* Local variable names (and the `this' keyword) may not appear
27837 in a default argument. */
27838 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27839 parser->local_variables_forbidden_p = true;
27841 push_defarg_context (fn);
27843 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27844 parmdecl = DECL_ARGUMENTS (fn);
27845 parm && parm != void_list_node;
27846 parm = TREE_CHAIN (parm),
27847 parmdecl = DECL_CHAIN (parmdecl))
27849 tree default_arg = TREE_PURPOSE (parm);
27850 tree parsed_arg;
27851 vec<tree, va_gc> *insts;
27852 tree copy;
27853 unsigned ix;
27855 if (!default_arg)
27856 continue;
27858 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27859 /* This can happen for a friend declaration for a function
27860 already declared with default arguments. */
27861 continue;
27863 parsed_arg
27864 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27865 default_arg,
27866 TREE_VALUE (parm));
27867 TREE_PURPOSE (parm) = parsed_arg;
27869 /* Update any instantiations we've already created. */
27870 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27871 vec_safe_iterate (insts, ix, &copy); ix++)
27872 TREE_PURPOSE (copy) = parsed_arg;
27875 pop_defarg_context ();
27877 /* Make sure no default arg is missing. */
27878 check_default_args (fn);
27880 /* Restore the state of local_variables_forbidden_p. */
27881 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27883 /* Restore the queue. */
27884 pop_unparsed_function_queues (parser);
27887 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27889 sizeof ... ( identifier )
27891 where the 'sizeof' token has already been consumed. */
27893 static tree
27894 cp_parser_sizeof_pack (cp_parser *parser)
27896 /* Consume the `...'. */
27897 cp_lexer_consume_token (parser->lexer);
27898 maybe_warn_variadic_templates ();
27900 matching_parens parens;
27901 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27902 if (paren)
27903 parens.consume_open (parser);
27904 else
27905 permerror (cp_lexer_peek_token (parser->lexer)->location,
27906 "%<sizeof...%> argument must be surrounded by parentheses");
27908 cp_token *token = cp_lexer_peek_token (parser->lexer);
27909 tree name = cp_parser_identifier (parser);
27910 if (name == error_mark_node)
27911 return error_mark_node;
27912 /* The name is not qualified. */
27913 parser->scope = NULL_TREE;
27914 parser->qualifying_scope = NULL_TREE;
27915 parser->object_scope = NULL_TREE;
27916 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27917 if (expr == error_mark_node)
27918 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27919 token->location);
27920 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27921 expr = TREE_TYPE (expr);
27922 else if (TREE_CODE (expr) == CONST_DECL)
27923 expr = DECL_INITIAL (expr);
27924 expr = make_pack_expansion (expr);
27925 PACK_EXPANSION_SIZEOF_P (expr) = true;
27927 if (paren)
27928 parens.require_close (parser);
27930 return expr;
27933 /* Parse the operand of `sizeof' (or a similar operator). Returns
27934 either a TYPE or an expression, depending on the form of the
27935 input. The KEYWORD indicates which kind of expression we have
27936 encountered. */
27938 static tree
27939 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27941 tree expr = NULL_TREE;
27942 const char *saved_message;
27943 char *tmp;
27944 bool saved_integral_constant_expression_p;
27945 bool saved_non_integral_constant_expression_p;
27947 /* If it's a `...', then we are computing the length of a parameter
27948 pack. */
27949 if (keyword == RID_SIZEOF
27950 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27951 return cp_parser_sizeof_pack (parser);
27953 /* Types cannot be defined in a `sizeof' expression. Save away the
27954 old message. */
27955 saved_message = parser->type_definition_forbidden_message;
27956 /* And create the new one. */
27957 tmp = concat ("types may not be defined in %<",
27958 IDENTIFIER_POINTER (ridpointers[keyword]),
27959 "%> expressions", NULL);
27960 parser->type_definition_forbidden_message = tmp;
27962 /* The restrictions on constant-expressions do not apply inside
27963 sizeof expressions. */
27964 saved_integral_constant_expression_p
27965 = parser->integral_constant_expression_p;
27966 saved_non_integral_constant_expression_p
27967 = parser->non_integral_constant_expression_p;
27968 parser->integral_constant_expression_p = false;
27970 /* Do not actually evaluate the expression. */
27971 ++cp_unevaluated_operand;
27972 ++c_inhibit_evaluation_warnings;
27973 /* If it's a `(', then we might be looking at the type-id
27974 construction. */
27975 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27977 tree type = NULL_TREE;
27979 /* We can't be sure yet whether we're looking at a type-id or an
27980 expression. */
27981 cp_parser_parse_tentatively (parser);
27983 matching_parens parens;
27984 parens.consume_open (parser);
27986 /* Note: as a GNU Extension, compound literals are considered
27987 postfix-expressions as they are in C99, so they are valid
27988 arguments to sizeof. See comment in cp_parser_cast_expression
27989 for details. */
27990 if (cp_parser_compound_literal_p (parser))
27991 cp_parser_simulate_error (parser);
27992 else
27994 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27995 parser->in_type_id_in_expr_p = true;
27996 /* Look for the type-id. */
27997 type = cp_parser_type_id (parser);
27998 /* Look for the closing `)'. */
27999 parens.require_close (parser);
28000 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28003 /* If all went well, then we're done. */
28004 if (cp_parser_parse_definitely (parser))
28006 cp_decl_specifier_seq decl_specs;
28008 /* Build a trivial decl-specifier-seq. */
28009 clear_decl_specs (&decl_specs);
28010 decl_specs.type = type;
28012 /* Call grokdeclarator to figure out what type this is. */
28013 expr = grokdeclarator (NULL,
28014 &decl_specs,
28015 TYPENAME,
28016 /*initialized=*/0,
28017 /*attrlist=*/NULL);
28021 /* If the type-id production did not work out, then we must be
28022 looking at the unary-expression production. */
28023 if (!expr)
28024 expr = cp_parser_unary_expression (parser);
28026 /* Go back to evaluating expressions. */
28027 --cp_unevaluated_operand;
28028 --c_inhibit_evaluation_warnings;
28030 /* Free the message we created. */
28031 free (tmp);
28032 /* And restore the old one. */
28033 parser->type_definition_forbidden_message = saved_message;
28034 parser->integral_constant_expression_p
28035 = saved_integral_constant_expression_p;
28036 parser->non_integral_constant_expression_p
28037 = saved_non_integral_constant_expression_p;
28039 return expr;
28042 /* If the current declaration has no declarator, return true. */
28044 static bool
28045 cp_parser_declares_only_class_p (cp_parser *parser)
28047 /* If the next token is a `;' or a `,' then there is no
28048 declarator. */
28049 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28050 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28053 /* Update the DECL_SPECS to reflect the storage class indicated by
28054 KEYWORD. */
28056 static void
28057 cp_parser_set_storage_class (cp_parser *parser,
28058 cp_decl_specifier_seq *decl_specs,
28059 enum rid keyword,
28060 cp_token *token)
28062 cp_storage_class storage_class;
28064 if (parser->in_unbraced_linkage_specification_p)
28066 error_at (token->location, "invalid use of %qD in linkage specification",
28067 ridpointers[keyword]);
28068 return;
28070 else if (decl_specs->storage_class != sc_none)
28072 decl_specs->conflicting_specifiers_p = true;
28073 return;
28076 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28077 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28078 && decl_specs->gnu_thread_keyword_p)
28080 pedwarn (decl_specs->locations[ds_thread], 0,
28081 "%<__thread%> before %qD", ridpointers[keyword]);
28084 switch (keyword)
28086 case RID_AUTO:
28087 storage_class = sc_auto;
28088 break;
28089 case RID_REGISTER:
28090 storage_class = sc_register;
28091 break;
28092 case RID_STATIC:
28093 storage_class = sc_static;
28094 break;
28095 case RID_EXTERN:
28096 storage_class = sc_extern;
28097 break;
28098 case RID_MUTABLE:
28099 storage_class = sc_mutable;
28100 break;
28101 default:
28102 gcc_unreachable ();
28104 decl_specs->storage_class = storage_class;
28105 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28107 /* A storage class specifier cannot be applied alongside a typedef
28108 specifier. If there is a typedef specifier present then set
28109 conflicting_specifiers_p which will trigger an error later
28110 on in grokdeclarator. */
28111 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28112 decl_specs->conflicting_specifiers_p = true;
28115 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28116 is true, the type is a class or enum definition. */
28118 static void
28119 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28120 tree type_spec,
28121 cp_token *token,
28122 bool type_definition_p)
28124 decl_specs->any_specifiers_p = true;
28126 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28127 (with, for example, in "typedef int wchar_t;") we remember that
28128 this is what happened. In system headers, we ignore these
28129 declarations so that G++ can work with system headers that are not
28130 C++-safe. */
28131 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28132 && !type_definition_p
28133 && (type_spec == boolean_type_node
28134 || type_spec == char16_type_node
28135 || type_spec == char32_type_node
28136 || type_spec == wchar_type_node)
28137 && (decl_specs->type
28138 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28139 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28140 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28141 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28143 decl_specs->redefined_builtin_type = type_spec;
28144 set_and_check_decl_spec_loc (decl_specs,
28145 ds_redefined_builtin_type_spec,
28146 token);
28147 if (!decl_specs->type)
28149 decl_specs->type = type_spec;
28150 decl_specs->type_definition_p = false;
28151 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28154 else if (decl_specs->type)
28155 decl_specs->multiple_types_p = true;
28156 else
28158 decl_specs->type = type_spec;
28159 decl_specs->type_definition_p = type_definition_p;
28160 decl_specs->redefined_builtin_type = NULL_TREE;
28161 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28165 /* True iff TOKEN is the GNU keyword __thread. */
28167 static bool
28168 token_is__thread (cp_token *token)
28170 gcc_assert (token->keyword == RID_THREAD);
28171 return id_equal (token->u.value, "__thread");
28174 /* Set the location for a declarator specifier and check if it is
28175 duplicated.
28177 DECL_SPECS is the sequence of declarator specifiers onto which to
28178 set the location.
28180 DS is the single declarator specifier to set which location is to
28181 be set onto the existing sequence of declarators.
28183 LOCATION is the location for the declarator specifier to
28184 consider. */
28186 static void
28187 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28188 cp_decl_spec ds, cp_token *token)
28190 gcc_assert (ds < ds_last);
28192 if (decl_specs == NULL)
28193 return;
28195 source_location location = token->location;
28197 if (decl_specs->locations[ds] == 0)
28199 decl_specs->locations[ds] = location;
28200 if (ds == ds_thread)
28201 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28203 else
28205 if (ds == ds_long)
28207 if (decl_specs->locations[ds_long_long] != 0)
28208 error_at (location,
28209 "%<long long long%> is too long for GCC");
28210 else
28212 decl_specs->locations[ds_long_long] = location;
28213 pedwarn_cxx98 (location,
28214 OPT_Wlong_long,
28215 "ISO C++ 1998 does not support %<long long%>");
28218 else if (ds == ds_thread)
28220 bool gnu = token_is__thread (token);
28221 if (gnu != decl_specs->gnu_thread_keyword_p)
28222 error_at (location,
28223 "both %<__thread%> and %<thread_local%> specified");
28224 else
28226 gcc_rich_location richloc (location);
28227 richloc.add_fixit_remove ();
28228 error_at (&richloc, "duplicate %qD", token->u.value);
28231 else
28233 static const char *const decl_spec_names[] = {
28234 "signed",
28235 "unsigned",
28236 "short",
28237 "long",
28238 "const",
28239 "volatile",
28240 "restrict",
28241 "inline",
28242 "virtual",
28243 "explicit",
28244 "friend",
28245 "typedef",
28246 "using",
28247 "constexpr",
28248 "__complex"
28250 gcc_rich_location richloc (location);
28251 richloc.add_fixit_remove ();
28252 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28257 /* Return true iff the declarator specifier DS is present in the
28258 sequence of declarator specifiers DECL_SPECS. */
28260 bool
28261 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28262 cp_decl_spec ds)
28264 gcc_assert (ds < ds_last);
28266 if (decl_specs == NULL)
28267 return false;
28269 return decl_specs->locations[ds] != 0;
28272 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28273 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28275 static bool
28276 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28278 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28281 /* Issue an error message indicating that TOKEN_DESC was expected.
28282 If KEYWORD is true, it indicated this function is called by
28283 cp_parser_require_keword and the required token can only be
28284 a indicated keyword.
28286 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28287 within any error as the location of an "opening" token matching
28288 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28289 RT_CLOSE_PAREN). */
28291 static void
28292 cp_parser_required_error (cp_parser *parser,
28293 required_token token_desc,
28294 bool keyword,
28295 location_t matching_location)
28297 if (cp_parser_simulate_error (parser))
28298 return;
28300 const char *gmsgid = NULL;
28301 switch (token_desc)
28303 case RT_NEW:
28304 gmsgid = G_("expected %<new%>");
28305 break;
28306 case RT_DELETE:
28307 gmsgid = G_("expected %<delete%>");
28308 break;
28309 case RT_RETURN:
28310 gmsgid = G_("expected %<return%>");
28311 break;
28312 case RT_WHILE:
28313 gmsgid = G_("expected %<while%>");
28314 break;
28315 case RT_EXTERN:
28316 gmsgid = G_("expected %<extern%>");
28317 break;
28318 case RT_STATIC_ASSERT:
28319 gmsgid = G_("expected %<static_assert%>");
28320 break;
28321 case RT_DECLTYPE:
28322 gmsgid = G_("expected %<decltype%>");
28323 break;
28324 case RT_OPERATOR:
28325 gmsgid = G_("expected %<operator%>");
28326 break;
28327 case RT_CLASS:
28328 gmsgid = G_("expected %<class%>");
28329 break;
28330 case RT_TEMPLATE:
28331 gmsgid = G_("expected %<template%>");
28332 break;
28333 case RT_NAMESPACE:
28334 gmsgid = G_("expected %<namespace%>");
28335 break;
28336 case RT_USING:
28337 gmsgid = G_("expected %<using%>");
28338 break;
28339 case RT_ASM:
28340 gmsgid = G_("expected %<asm%>");
28341 break;
28342 case RT_TRY:
28343 gmsgid = G_("expected %<try%>");
28344 break;
28345 case RT_CATCH:
28346 gmsgid = G_("expected %<catch%>");
28347 break;
28348 case RT_THROW:
28349 gmsgid = G_("expected %<throw%>");
28350 break;
28351 case RT_LABEL:
28352 gmsgid = G_("expected %<__label__%>");
28353 break;
28354 case RT_AT_TRY:
28355 gmsgid = G_("expected %<@try%>");
28356 break;
28357 case RT_AT_SYNCHRONIZED:
28358 gmsgid = G_("expected %<@synchronized%>");
28359 break;
28360 case RT_AT_THROW:
28361 gmsgid = G_("expected %<@throw%>");
28362 break;
28363 case RT_TRANSACTION_ATOMIC:
28364 gmsgid = G_("expected %<__transaction_atomic%>");
28365 break;
28366 case RT_TRANSACTION_RELAXED:
28367 gmsgid = G_("expected %<__transaction_relaxed%>");
28368 break;
28369 default:
28370 break;
28373 if (!gmsgid && !keyword)
28375 switch (token_desc)
28377 case RT_SEMICOLON:
28378 gmsgid = G_("expected %<;%>");
28379 break;
28380 case RT_OPEN_PAREN:
28381 gmsgid = G_("expected %<(%>");
28382 break;
28383 case RT_CLOSE_BRACE:
28384 gmsgid = G_("expected %<}%>");
28385 break;
28386 case RT_OPEN_BRACE:
28387 gmsgid = G_("expected %<{%>");
28388 break;
28389 case RT_CLOSE_SQUARE:
28390 gmsgid = G_("expected %<]%>");
28391 break;
28392 case RT_OPEN_SQUARE:
28393 gmsgid = G_("expected %<[%>");
28394 break;
28395 case RT_COMMA:
28396 gmsgid = G_("expected %<,%>");
28397 break;
28398 case RT_SCOPE:
28399 gmsgid = G_("expected %<::%>");
28400 break;
28401 case RT_LESS:
28402 gmsgid = G_("expected %<<%>");
28403 break;
28404 case RT_GREATER:
28405 gmsgid = G_("expected %<>%>");
28406 break;
28407 case RT_EQ:
28408 gmsgid = G_("expected %<=%>");
28409 break;
28410 case RT_ELLIPSIS:
28411 gmsgid = G_("expected %<...%>");
28412 break;
28413 case RT_MULT:
28414 gmsgid = G_("expected %<*%>");
28415 break;
28416 case RT_COMPL:
28417 gmsgid = G_("expected %<~%>");
28418 break;
28419 case RT_COLON:
28420 gmsgid = G_("expected %<:%>");
28421 break;
28422 case RT_COLON_SCOPE:
28423 gmsgid = G_("expected %<:%> or %<::%>");
28424 break;
28425 case RT_CLOSE_PAREN:
28426 gmsgid = G_("expected %<)%>");
28427 break;
28428 case RT_COMMA_CLOSE_PAREN:
28429 gmsgid = G_("expected %<,%> or %<)%>");
28430 break;
28431 case RT_PRAGMA_EOL:
28432 gmsgid = G_("expected end of line");
28433 break;
28434 case RT_NAME:
28435 gmsgid = G_("expected identifier");
28436 break;
28437 case RT_SELECT:
28438 gmsgid = G_("expected selection-statement");
28439 break;
28440 case RT_ITERATION:
28441 gmsgid = G_("expected iteration-statement");
28442 break;
28443 case RT_JUMP:
28444 gmsgid = G_("expected jump-statement");
28445 break;
28446 case RT_CLASS_KEY:
28447 gmsgid = G_("expected class-key");
28448 break;
28449 case RT_CLASS_TYPENAME_TEMPLATE:
28450 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28451 break;
28452 default:
28453 gcc_unreachable ();
28457 if (gmsgid)
28458 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28462 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28463 issue an error message indicating that TOKEN_DESC was expected.
28465 Returns the token consumed, if the token had the appropriate type.
28466 Otherwise, returns NULL.
28468 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28469 within any error as the location of an "opening" token matching
28470 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28471 RT_CLOSE_PAREN). */
28473 static cp_token *
28474 cp_parser_require (cp_parser* parser,
28475 enum cpp_ttype type,
28476 required_token token_desc,
28477 location_t matching_location)
28479 if (cp_lexer_next_token_is (parser->lexer, type))
28480 return cp_lexer_consume_token (parser->lexer);
28481 else
28483 /* Output the MESSAGE -- unless we're parsing tentatively. */
28484 if (!cp_parser_simulate_error (parser))
28485 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28486 matching_location);
28487 return NULL;
28491 /* An error message is produced if the next token is not '>'.
28492 All further tokens are skipped until the desired token is
28493 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28495 static void
28496 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28498 /* Current level of '< ... >'. */
28499 unsigned level = 0;
28500 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28501 unsigned nesting_depth = 0;
28503 /* Are we ready, yet? If not, issue error message. */
28504 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28505 return;
28507 /* Skip tokens until the desired token is found. */
28508 while (true)
28510 /* Peek at the next token. */
28511 switch (cp_lexer_peek_token (parser->lexer)->type)
28513 case CPP_LESS:
28514 if (!nesting_depth)
28515 ++level;
28516 break;
28518 case CPP_RSHIFT:
28519 if (cxx_dialect == cxx98)
28520 /* C++0x views the `>>' operator as two `>' tokens, but
28521 C++98 does not. */
28522 break;
28523 else if (!nesting_depth && level-- == 0)
28525 /* We've hit a `>>' where the first `>' closes the
28526 template argument list, and the second `>' is
28527 spurious. Just consume the `>>' and stop; we've
28528 already produced at least one error. */
28529 cp_lexer_consume_token (parser->lexer);
28530 return;
28532 /* Fall through for C++0x, so we handle the second `>' in
28533 the `>>'. */
28534 gcc_fallthrough ();
28536 case CPP_GREATER:
28537 if (!nesting_depth && level-- == 0)
28539 /* We've reached the token we want, consume it and stop. */
28540 cp_lexer_consume_token (parser->lexer);
28541 return;
28543 break;
28545 case CPP_OPEN_PAREN:
28546 case CPP_OPEN_SQUARE:
28547 ++nesting_depth;
28548 break;
28550 case CPP_CLOSE_PAREN:
28551 case CPP_CLOSE_SQUARE:
28552 if (nesting_depth-- == 0)
28553 return;
28554 break;
28556 case CPP_EOF:
28557 case CPP_PRAGMA_EOL:
28558 case CPP_SEMICOLON:
28559 case CPP_OPEN_BRACE:
28560 case CPP_CLOSE_BRACE:
28561 /* The '>' was probably forgotten, don't look further. */
28562 return;
28564 default:
28565 break;
28568 /* Consume this token. */
28569 cp_lexer_consume_token (parser->lexer);
28573 /* If the next token is the indicated keyword, consume it. Otherwise,
28574 issue an error message indicating that TOKEN_DESC was expected.
28576 Returns the token consumed, if the token had the appropriate type.
28577 Otherwise, returns NULL. */
28579 static cp_token *
28580 cp_parser_require_keyword (cp_parser* parser,
28581 enum rid keyword,
28582 required_token token_desc)
28584 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28586 if (token && token->keyword != keyword)
28588 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28589 UNKNOWN_LOCATION);
28590 return NULL;
28593 return token;
28596 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28597 function-definition. */
28599 static bool
28600 cp_parser_token_starts_function_definition_p (cp_token* token)
28602 return (/* An ordinary function-body begins with an `{'. */
28603 token->type == CPP_OPEN_BRACE
28604 /* A ctor-initializer begins with a `:'. */
28605 || token->type == CPP_COLON
28606 /* A function-try-block begins with `try'. */
28607 || token->keyword == RID_TRY
28608 /* A function-transaction-block begins with `__transaction_atomic'
28609 or `__transaction_relaxed'. */
28610 || token->keyword == RID_TRANSACTION_ATOMIC
28611 || token->keyword == RID_TRANSACTION_RELAXED
28612 /* The named return value extension begins with `return'. */
28613 || token->keyword == RID_RETURN);
28616 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28617 definition. */
28619 static bool
28620 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28622 cp_token *token;
28624 token = cp_lexer_peek_token (parser->lexer);
28625 return (token->type == CPP_OPEN_BRACE
28626 || (token->type == CPP_COLON
28627 && !parser->colon_doesnt_start_class_def_p));
28630 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28631 C++0x) ending a template-argument. */
28633 static bool
28634 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28636 cp_token *token;
28638 token = cp_lexer_peek_token (parser->lexer);
28639 return (token->type == CPP_COMMA
28640 || token->type == CPP_GREATER
28641 || token->type == CPP_ELLIPSIS
28642 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28645 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28646 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28648 static bool
28649 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28650 size_t n)
28652 cp_token *token;
28654 token = cp_lexer_peek_nth_token (parser->lexer, n);
28655 if (token->type == CPP_LESS)
28656 return true;
28657 /* Check for the sequence `<::' in the original code. It would be lexed as
28658 `[:', where `[' is a digraph, and there is no whitespace before
28659 `:'. */
28660 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28662 cp_token *token2;
28663 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28664 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28665 return true;
28667 return false;
28670 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28671 or none_type otherwise. */
28673 static enum tag_types
28674 cp_parser_token_is_class_key (cp_token* token)
28676 switch (token->keyword)
28678 case RID_CLASS:
28679 return class_type;
28680 case RID_STRUCT:
28681 return record_type;
28682 case RID_UNION:
28683 return union_type;
28685 default:
28686 return none_type;
28690 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28691 or none_type otherwise or if the token is null. */
28693 static enum tag_types
28694 cp_parser_token_is_type_parameter_key (cp_token* token)
28696 if (!token)
28697 return none_type;
28699 switch (token->keyword)
28701 case RID_CLASS:
28702 return class_type;
28703 case RID_TYPENAME:
28704 return typename_type;
28706 default:
28707 return none_type;
28711 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28713 static void
28714 cp_parser_check_class_key (enum tag_types class_key, tree type)
28716 if (type == error_mark_node)
28717 return;
28718 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28720 if (permerror (input_location, "%qs tag used in naming %q#T",
28721 class_key == union_type ? "union"
28722 : class_key == record_type ? "struct" : "class",
28723 type))
28724 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28725 "%q#T was previously declared here", type);
28729 /* Issue an error message if DECL is redeclared with different
28730 access than its original declaration [class.access.spec/3].
28731 This applies to nested classes, nested class templates and
28732 enumerations [class.mem/1]. */
28734 static void
28735 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28737 if (!decl
28738 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28739 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28740 return;
28742 if ((TREE_PRIVATE (decl)
28743 != (current_access_specifier == access_private_node))
28744 || (TREE_PROTECTED (decl)
28745 != (current_access_specifier == access_protected_node)))
28746 error_at (location, "%qD redeclared with different access", decl);
28749 /* Look for the `template' keyword, as a syntactic disambiguator.
28750 Return TRUE iff it is present, in which case it will be
28751 consumed. */
28753 static bool
28754 cp_parser_optional_template_keyword (cp_parser *parser)
28756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28758 /* In C++98 the `template' keyword can only be used within templates;
28759 outside templates the parser can always figure out what is a
28760 template and what is not. In C++11, per the resolution of DR 468,
28761 `template' is allowed in cases where it is not strictly necessary. */
28762 if (!processing_template_decl
28763 && pedantic && cxx_dialect == cxx98)
28765 cp_token *token = cp_lexer_peek_token (parser->lexer);
28766 pedwarn (token->location, OPT_Wpedantic,
28767 "in C++98 %<template%> (as a disambiguator) is only "
28768 "allowed within templates");
28769 /* If this part of the token stream is rescanned, the same
28770 error message would be generated. So, we purge the token
28771 from the stream. */
28772 cp_lexer_purge_token (parser->lexer);
28773 return false;
28775 else
28777 /* Consume the `template' keyword. */
28778 cp_lexer_consume_token (parser->lexer);
28779 return true;
28782 return false;
28785 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28786 set PARSER->SCOPE, and perform other related actions. */
28788 static void
28789 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28791 struct tree_check *check_value;
28793 /* Get the stored value. */
28794 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28795 /* Set the scope from the stored value. */
28796 parser->scope = saved_checks_value (check_value);
28797 parser->qualifying_scope = check_value->qualifying_scope;
28798 parser->object_scope = NULL_TREE;
28801 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28802 encounter the end of a block before what we were looking for. */
28804 static bool
28805 cp_parser_cache_group (cp_parser *parser,
28806 enum cpp_ttype end,
28807 unsigned depth)
28809 while (true)
28811 cp_token *token = cp_lexer_peek_token (parser->lexer);
28813 /* Abort a parenthesized expression if we encounter a semicolon. */
28814 if ((end == CPP_CLOSE_PAREN || depth == 0)
28815 && token->type == CPP_SEMICOLON)
28816 return true;
28817 /* If we've reached the end of the file, stop. */
28818 if (token->type == CPP_EOF
28819 || (end != CPP_PRAGMA_EOL
28820 && token->type == CPP_PRAGMA_EOL))
28821 return true;
28822 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28823 /* We've hit the end of an enclosing block, so there's been some
28824 kind of syntax error. */
28825 return true;
28827 /* Consume the token. */
28828 cp_lexer_consume_token (parser->lexer);
28829 /* See if it starts a new group. */
28830 if (token->type == CPP_OPEN_BRACE)
28832 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28833 /* In theory this should probably check end == '}', but
28834 cp_parser_save_member_function_body needs it to exit
28835 after either '}' or ')' when called with ')'. */
28836 if (depth == 0)
28837 return false;
28839 else if (token->type == CPP_OPEN_PAREN)
28841 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28842 if (depth == 0 && end == CPP_CLOSE_PAREN)
28843 return false;
28845 else if (token->type == CPP_PRAGMA)
28846 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28847 else if (token->type == end)
28848 return false;
28852 /* Like above, for caching a default argument or NSDMI. Both of these are
28853 terminated by a non-nested comma, but it can be unclear whether or not a
28854 comma is nested in a template argument list unless we do more parsing.
28855 In order to handle this ambiguity, when we encounter a ',' after a '<'
28856 we try to parse what follows as a parameter-declaration-list (in the
28857 case of a default argument) or a member-declarator (in the case of an
28858 NSDMI). If that succeeds, then we stop caching. */
28860 static tree
28861 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28863 unsigned depth = 0;
28864 int maybe_template_id = 0;
28865 cp_token *first_token;
28866 cp_token *token;
28867 tree default_argument;
28869 /* Add tokens until we have processed the entire default
28870 argument. We add the range [first_token, token). */
28871 first_token = cp_lexer_peek_token (parser->lexer);
28872 if (first_token->type == CPP_OPEN_BRACE)
28874 /* For list-initialization, this is straightforward. */
28875 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28876 token = cp_lexer_peek_token (parser->lexer);
28878 else while (true)
28880 bool done = false;
28882 /* Peek at the next token. */
28883 token = cp_lexer_peek_token (parser->lexer);
28884 /* What we do depends on what token we have. */
28885 switch (token->type)
28887 /* In valid code, a default argument must be
28888 immediately followed by a `,' `)', or `...'. */
28889 case CPP_COMMA:
28890 if (depth == 0 && maybe_template_id)
28892 /* If we've seen a '<', we might be in a
28893 template-argument-list. Until Core issue 325 is
28894 resolved, we don't know how this situation ought
28895 to be handled, so try to DTRT. We check whether
28896 what comes after the comma is a valid parameter
28897 declaration list. If it is, then the comma ends
28898 the default argument; otherwise the default
28899 argument continues. */
28900 bool error = false;
28901 cp_token *peek;
28903 /* Set ITALP so cp_parser_parameter_declaration_list
28904 doesn't decide to commit to this parse. */
28905 bool saved_italp = parser->in_template_argument_list_p;
28906 parser->in_template_argument_list_p = true;
28908 cp_parser_parse_tentatively (parser);
28910 if (nsdmi)
28912 /* Parse declarators until we reach a non-comma or
28913 somthing that cannot be an initializer.
28914 Just checking whether we're looking at a single
28915 declarator is insufficient. Consider:
28916 int var = tuple<T,U>::x;
28917 The template parameter 'U' looks exactly like a
28918 declarator. */
28921 int ctor_dtor_or_conv_p;
28922 cp_lexer_consume_token (parser->lexer);
28923 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28924 &ctor_dtor_or_conv_p,
28925 /*parenthesized_p=*/NULL,
28926 /*member_p=*/true,
28927 /*friend_p=*/false);
28928 peek = cp_lexer_peek_token (parser->lexer);
28929 if (cp_parser_error_occurred (parser))
28930 break;
28932 while (peek->type == CPP_COMMA);
28933 /* If we met an '=' or ';' then the original comma
28934 was the end of the NSDMI. Otherwise assume
28935 we're still in the NSDMI. */
28936 error = (peek->type != CPP_EQ
28937 && peek->type != CPP_SEMICOLON);
28939 else
28941 cp_lexer_consume_token (parser->lexer);
28942 begin_scope (sk_function_parms, NULL_TREE);
28943 cp_parser_parameter_declaration_list (parser, &error);
28944 pop_bindings_and_leave_scope ();
28946 if (!cp_parser_error_occurred (parser) && !error)
28947 done = true;
28948 cp_parser_abort_tentative_parse (parser);
28950 parser->in_template_argument_list_p = saved_italp;
28951 break;
28953 /* FALLTHRU */
28954 case CPP_CLOSE_PAREN:
28955 case CPP_ELLIPSIS:
28956 /* If we run into a non-nested `;', `}', or `]',
28957 then the code is invalid -- but the default
28958 argument is certainly over. */
28959 case CPP_SEMICOLON:
28960 case CPP_CLOSE_BRACE:
28961 case CPP_CLOSE_SQUARE:
28962 if (depth == 0
28963 /* Handle correctly int n = sizeof ... ( p ); */
28964 && token->type != CPP_ELLIPSIS)
28965 done = true;
28966 /* Update DEPTH, if necessary. */
28967 else if (token->type == CPP_CLOSE_PAREN
28968 || token->type == CPP_CLOSE_BRACE
28969 || token->type == CPP_CLOSE_SQUARE)
28970 --depth;
28971 break;
28973 case CPP_OPEN_PAREN:
28974 case CPP_OPEN_SQUARE:
28975 case CPP_OPEN_BRACE:
28976 ++depth;
28977 break;
28979 case CPP_LESS:
28980 if (depth == 0)
28981 /* This might be the comparison operator, or it might
28982 start a template argument list. */
28983 ++maybe_template_id;
28984 break;
28986 case CPP_RSHIFT:
28987 if (cxx_dialect == cxx98)
28988 break;
28989 /* Fall through for C++0x, which treats the `>>'
28990 operator like two `>' tokens in certain
28991 cases. */
28992 gcc_fallthrough ();
28994 case CPP_GREATER:
28995 if (depth == 0)
28997 /* This might be an operator, or it might close a
28998 template argument list. But if a previous '<'
28999 started a template argument list, this will have
29000 closed it, so we can't be in one anymore. */
29001 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29002 if (maybe_template_id < 0)
29003 maybe_template_id = 0;
29005 break;
29007 /* If we run out of tokens, issue an error message. */
29008 case CPP_EOF:
29009 case CPP_PRAGMA_EOL:
29010 error_at (token->location, "file ends in default argument");
29011 return error_mark_node;
29013 case CPP_NAME:
29014 case CPP_SCOPE:
29015 /* In these cases, we should look for template-ids.
29016 For example, if the default argument is
29017 `X<int, double>()', we need to do name lookup to
29018 figure out whether or not `X' is a template; if
29019 so, the `,' does not end the default argument.
29021 That is not yet done. */
29022 break;
29024 default:
29025 break;
29028 /* If we've reached the end, stop. */
29029 if (done)
29030 break;
29032 /* Add the token to the token block. */
29033 token = cp_lexer_consume_token (parser->lexer);
29036 /* Create a DEFAULT_ARG to represent the unparsed default
29037 argument. */
29038 default_argument = make_node (DEFAULT_ARG);
29039 DEFARG_TOKENS (default_argument)
29040 = cp_token_cache_new (first_token, token);
29041 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29043 return default_argument;
29046 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29048 location_t
29049 defarg_location (tree default_argument)
29051 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29052 location_t start = tokens->first->location;
29053 location_t end = tokens->last->location;
29054 return make_location (start, start, end);
29057 /* Begin parsing tentatively. We always save tokens while parsing
29058 tentatively so that if the tentative parsing fails we can restore the
29059 tokens. */
29061 static void
29062 cp_parser_parse_tentatively (cp_parser* parser)
29064 /* Enter a new parsing context. */
29065 parser->context = cp_parser_context_new (parser->context);
29066 /* Begin saving tokens. */
29067 cp_lexer_save_tokens (parser->lexer);
29068 /* In order to avoid repetitive access control error messages,
29069 access checks are queued up until we are no longer parsing
29070 tentatively. */
29071 push_deferring_access_checks (dk_deferred);
29074 /* Commit to the currently active tentative parse. */
29076 static void
29077 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29079 cp_parser_context *context;
29080 cp_lexer *lexer;
29082 /* Mark all of the levels as committed. */
29083 lexer = parser->lexer;
29084 for (context = parser->context; context->next; context = context->next)
29086 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29087 break;
29088 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29089 while (!cp_lexer_saving_tokens (lexer))
29090 lexer = lexer->next;
29091 cp_lexer_commit_tokens (lexer);
29095 /* Commit to the topmost currently active tentative parse.
29097 Note that this function shouldn't be called when there are
29098 irreversible side-effects while in a tentative state. For
29099 example, we shouldn't create a permanent entry in the symbol
29100 table, or issue an error message that might not apply if the
29101 tentative parse is aborted. */
29103 static void
29104 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29106 cp_parser_context *context = parser->context;
29107 cp_lexer *lexer = parser->lexer;
29109 if (context)
29111 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29112 return;
29113 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29115 while (!cp_lexer_saving_tokens (lexer))
29116 lexer = lexer->next;
29117 cp_lexer_commit_tokens (lexer);
29121 /* Abort the currently active tentative parse. All consumed tokens
29122 will be rolled back, and no diagnostics will be issued. */
29124 static void
29125 cp_parser_abort_tentative_parse (cp_parser* parser)
29127 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29128 || errorcount > 0);
29129 cp_parser_simulate_error (parser);
29130 /* Now, pretend that we want to see if the construct was
29131 successfully parsed. */
29132 cp_parser_parse_definitely (parser);
29135 /* Stop parsing tentatively. If a parse error has occurred, restore the
29136 token stream. Otherwise, commit to the tokens we have consumed.
29137 Returns true if no error occurred; false otherwise. */
29139 static bool
29140 cp_parser_parse_definitely (cp_parser* parser)
29142 bool error_occurred;
29143 cp_parser_context *context;
29145 /* Remember whether or not an error occurred, since we are about to
29146 destroy that information. */
29147 error_occurred = cp_parser_error_occurred (parser);
29148 /* Remove the topmost context from the stack. */
29149 context = parser->context;
29150 parser->context = context->next;
29151 /* If no parse errors occurred, commit to the tentative parse. */
29152 if (!error_occurred)
29154 /* Commit to the tokens read tentatively, unless that was
29155 already done. */
29156 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29157 cp_lexer_commit_tokens (parser->lexer);
29159 pop_to_parent_deferring_access_checks ();
29161 /* Otherwise, if errors occurred, roll back our state so that things
29162 are just as they were before we began the tentative parse. */
29163 else
29165 cp_lexer_rollback_tokens (parser->lexer);
29166 pop_deferring_access_checks ();
29168 /* Add the context to the front of the free list. */
29169 context->next = cp_parser_context_free_list;
29170 cp_parser_context_free_list = context;
29172 return !error_occurred;
29175 /* Returns true if we are parsing tentatively and are not committed to
29176 this tentative parse. */
29178 static bool
29179 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29181 return (cp_parser_parsing_tentatively (parser)
29182 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29185 /* Returns nonzero iff an error has occurred during the most recent
29186 tentative parse. */
29188 static bool
29189 cp_parser_error_occurred (cp_parser* parser)
29191 return (cp_parser_parsing_tentatively (parser)
29192 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29195 /* Returns nonzero if GNU extensions are allowed. */
29197 static bool
29198 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29200 return parser->allow_gnu_extensions_p;
29203 /* Objective-C++ Productions */
29206 /* Parse an Objective-C expression, which feeds into a primary-expression
29207 above.
29209 objc-expression:
29210 objc-message-expression
29211 objc-string-literal
29212 objc-encode-expression
29213 objc-protocol-expression
29214 objc-selector-expression
29216 Returns a tree representation of the expression. */
29218 static cp_expr
29219 cp_parser_objc_expression (cp_parser* parser)
29221 /* Try to figure out what kind of declaration is present. */
29222 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29224 switch (kwd->type)
29226 case CPP_OPEN_SQUARE:
29227 return cp_parser_objc_message_expression (parser);
29229 case CPP_OBJC_STRING:
29230 kwd = cp_lexer_consume_token (parser->lexer);
29231 return objc_build_string_object (kwd->u.value);
29233 case CPP_KEYWORD:
29234 switch (kwd->keyword)
29236 case RID_AT_ENCODE:
29237 return cp_parser_objc_encode_expression (parser);
29239 case RID_AT_PROTOCOL:
29240 return cp_parser_objc_protocol_expression (parser);
29242 case RID_AT_SELECTOR:
29243 return cp_parser_objc_selector_expression (parser);
29245 default:
29246 break;
29248 /* FALLTHRU */
29249 default:
29250 error_at (kwd->location,
29251 "misplaced %<@%D%> Objective-C++ construct",
29252 kwd->u.value);
29253 cp_parser_skip_to_end_of_block_or_statement (parser);
29256 return error_mark_node;
29259 /* Parse an Objective-C message expression.
29261 objc-message-expression:
29262 [ objc-message-receiver objc-message-args ]
29264 Returns a representation of an Objective-C message. */
29266 static tree
29267 cp_parser_objc_message_expression (cp_parser* parser)
29269 tree receiver, messageargs;
29271 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29272 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29273 receiver = cp_parser_objc_message_receiver (parser);
29274 messageargs = cp_parser_objc_message_args (parser);
29275 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29276 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29278 tree result = objc_build_message_expr (receiver, messageargs);
29280 /* Construct a location e.g.
29281 [self func1:5]
29282 ^~~~~~~~~~~~~~
29283 ranging from the '[' to the ']', with the caret at the start. */
29284 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29285 protected_set_expr_location (result, combined_loc);
29287 return result;
29290 /* Parse an objc-message-receiver.
29292 objc-message-receiver:
29293 expression
29294 simple-type-specifier
29296 Returns a representation of the type or expression. */
29298 static tree
29299 cp_parser_objc_message_receiver (cp_parser* parser)
29301 tree rcv;
29303 /* An Objective-C message receiver may be either (1) a type
29304 or (2) an expression. */
29305 cp_parser_parse_tentatively (parser);
29306 rcv = cp_parser_expression (parser);
29308 /* If that worked out, fine. */
29309 if (cp_parser_parse_definitely (parser))
29310 return rcv;
29312 cp_parser_parse_tentatively (parser);
29313 rcv = cp_parser_simple_type_specifier (parser,
29314 /*decl_specs=*/NULL,
29315 CP_PARSER_FLAGS_NONE);
29317 if (cp_parser_parse_definitely (parser))
29318 return objc_get_class_reference (rcv);
29320 cp_parser_error (parser, "objective-c++ message receiver expected");
29321 return error_mark_node;
29324 /* Parse the arguments and selectors comprising an Objective-C message.
29326 objc-message-args:
29327 objc-selector
29328 objc-selector-args
29329 objc-selector-args , objc-comma-args
29331 objc-selector-args:
29332 objc-selector [opt] : assignment-expression
29333 objc-selector-args objc-selector [opt] : assignment-expression
29335 objc-comma-args:
29336 assignment-expression
29337 objc-comma-args , assignment-expression
29339 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29340 selector arguments and TREE_VALUE containing a list of comma
29341 arguments. */
29343 static tree
29344 cp_parser_objc_message_args (cp_parser* parser)
29346 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29347 bool maybe_unary_selector_p = true;
29348 cp_token *token = cp_lexer_peek_token (parser->lexer);
29350 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29352 tree selector = NULL_TREE, arg;
29354 if (token->type != CPP_COLON)
29355 selector = cp_parser_objc_selector (parser);
29357 /* Detect if we have a unary selector. */
29358 if (maybe_unary_selector_p
29359 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29360 return build_tree_list (selector, NULL_TREE);
29362 maybe_unary_selector_p = false;
29363 cp_parser_require (parser, CPP_COLON, RT_COLON);
29364 arg = cp_parser_assignment_expression (parser);
29366 sel_args
29367 = chainon (sel_args,
29368 build_tree_list (selector, arg));
29370 token = cp_lexer_peek_token (parser->lexer);
29373 /* Handle non-selector arguments, if any. */
29374 while (token->type == CPP_COMMA)
29376 tree arg;
29378 cp_lexer_consume_token (parser->lexer);
29379 arg = cp_parser_assignment_expression (parser);
29381 addl_args
29382 = chainon (addl_args,
29383 build_tree_list (NULL_TREE, arg));
29385 token = cp_lexer_peek_token (parser->lexer);
29388 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29390 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29391 return build_tree_list (error_mark_node, error_mark_node);
29394 return build_tree_list (sel_args, addl_args);
29397 /* Parse an Objective-C encode expression.
29399 objc-encode-expression:
29400 @encode objc-typename
29402 Returns an encoded representation of the type argument. */
29404 static cp_expr
29405 cp_parser_objc_encode_expression (cp_parser* parser)
29407 tree type;
29408 cp_token *token;
29409 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29411 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29412 matching_parens parens;
29413 parens.require_open (parser);
29414 token = cp_lexer_peek_token (parser->lexer);
29415 type = complete_type (cp_parser_type_id (parser));
29416 parens.require_close (parser);
29418 if (!type)
29420 error_at (token->location,
29421 "%<@encode%> must specify a type as an argument");
29422 return error_mark_node;
29425 /* This happens if we find @encode(T) (where T is a template
29426 typename or something dependent on a template typename) when
29427 parsing a template. In that case, we can't compile it
29428 immediately, but we rather create an AT_ENCODE_EXPR which will
29429 need to be instantiated when the template is used.
29431 if (dependent_type_p (type))
29433 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29434 TREE_READONLY (value) = 1;
29435 return value;
29439 /* Build a location of the form:
29440 @encode(int)
29441 ^~~~~~~~~~~~
29442 with caret==start at the @ token, finishing at the close paren. */
29443 location_t combined_loc
29444 = make_location (start_loc, start_loc,
29445 cp_lexer_previous_token (parser->lexer)->location);
29447 return cp_expr (objc_build_encode_expr (type), combined_loc);
29450 /* Parse an Objective-C @defs expression. */
29452 static tree
29453 cp_parser_objc_defs_expression (cp_parser *parser)
29455 tree name;
29457 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29458 matching_parens parens;
29459 parens.require_open (parser);
29460 name = cp_parser_identifier (parser);
29461 parens.require_close (parser);
29463 return objc_get_class_ivars (name);
29466 /* Parse an Objective-C protocol expression.
29468 objc-protocol-expression:
29469 @protocol ( identifier )
29471 Returns a representation of the protocol expression. */
29473 static tree
29474 cp_parser_objc_protocol_expression (cp_parser* parser)
29476 tree proto;
29477 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29479 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29480 matching_parens parens;
29481 parens.require_open (parser);
29482 proto = cp_parser_identifier (parser);
29483 parens.require_close (parser);
29485 /* Build a location of the form:
29486 @protocol(prot)
29487 ^~~~~~~~~~~~~~~
29488 with caret==start at the @ token, finishing at the close paren. */
29489 location_t combined_loc
29490 = make_location (start_loc, start_loc,
29491 cp_lexer_previous_token (parser->lexer)->location);
29492 tree result = objc_build_protocol_expr (proto);
29493 protected_set_expr_location (result, combined_loc);
29494 return result;
29497 /* Parse an Objective-C selector expression.
29499 objc-selector-expression:
29500 @selector ( objc-method-signature )
29502 objc-method-signature:
29503 objc-selector
29504 objc-selector-seq
29506 objc-selector-seq:
29507 objc-selector :
29508 objc-selector-seq objc-selector :
29510 Returns a representation of the method selector. */
29512 static tree
29513 cp_parser_objc_selector_expression (cp_parser* parser)
29515 tree sel_seq = NULL_TREE;
29516 bool maybe_unary_selector_p = true;
29517 cp_token *token;
29518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29520 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29521 matching_parens parens;
29522 parens.require_open (parser);
29523 token = cp_lexer_peek_token (parser->lexer);
29525 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29526 || token->type == CPP_SCOPE)
29528 tree selector = NULL_TREE;
29530 if (token->type != CPP_COLON
29531 || token->type == CPP_SCOPE)
29532 selector = cp_parser_objc_selector (parser);
29534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29535 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29537 /* Detect if we have a unary selector. */
29538 if (maybe_unary_selector_p)
29540 sel_seq = selector;
29541 goto finish_selector;
29543 else
29545 cp_parser_error (parser, "expected %<:%>");
29548 maybe_unary_selector_p = false;
29549 token = cp_lexer_consume_token (parser->lexer);
29551 if (token->type == CPP_SCOPE)
29553 sel_seq
29554 = chainon (sel_seq,
29555 build_tree_list (selector, NULL_TREE));
29556 sel_seq
29557 = chainon (sel_seq,
29558 build_tree_list (NULL_TREE, NULL_TREE));
29560 else
29561 sel_seq
29562 = chainon (sel_seq,
29563 build_tree_list (selector, NULL_TREE));
29565 token = cp_lexer_peek_token (parser->lexer);
29568 finish_selector:
29569 parens.require_close (parser);
29572 /* Build a location of the form:
29573 @selector(func)
29574 ^~~~~~~~~~~~~~~
29575 with caret==start at the @ token, finishing at the close paren. */
29576 location_t combined_loc
29577 = make_location (loc, loc,
29578 cp_lexer_previous_token (parser->lexer)->location);
29579 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29580 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29581 protected_set_expr_location (result, combined_loc);
29582 return result;
29585 /* Parse a list of identifiers.
29587 objc-identifier-list:
29588 identifier
29589 objc-identifier-list , identifier
29591 Returns a TREE_LIST of identifier nodes. */
29593 static tree
29594 cp_parser_objc_identifier_list (cp_parser* parser)
29596 tree identifier;
29597 tree list;
29598 cp_token *sep;
29600 identifier = cp_parser_identifier (parser);
29601 if (identifier == error_mark_node)
29602 return error_mark_node;
29604 list = build_tree_list (NULL_TREE, identifier);
29605 sep = cp_lexer_peek_token (parser->lexer);
29607 while (sep->type == CPP_COMMA)
29609 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29610 identifier = cp_parser_identifier (parser);
29611 if (identifier == error_mark_node)
29612 return list;
29614 list = chainon (list, build_tree_list (NULL_TREE,
29615 identifier));
29616 sep = cp_lexer_peek_token (parser->lexer);
29619 return list;
29622 /* Parse an Objective-C alias declaration.
29624 objc-alias-declaration:
29625 @compatibility_alias identifier identifier ;
29627 This function registers the alias mapping with the Objective-C front end.
29628 It returns nothing. */
29630 static void
29631 cp_parser_objc_alias_declaration (cp_parser* parser)
29633 tree alias, orig;
29635 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29636 alias = cp_parser_identifier (parser);
29637 orig = cp_parser_identifier (parser);
29638 objc_declare_alias (alias, orig);
29639 cp_parser_consume_semicolon_at_end_of_statement (parser);
29642 /* Parse an Objective-C class forward-declaration.
29644 objc-class-declaration:
29645 @class objc-identifier-list ;
29647 The function registers the forward declarations with the Objective-C
29648 front end. It returns nothing. */
29650 static void
29651 cp_parser_objc_class_declaration (cp_parser* parser)
29653 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29654 while (true)
29656 tree id;
29658 id = cp_parser_identifier (parser);
29659 if (id == error_mark_node)
29660 break;
29662 objc_declare_class (id);
29664 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29665 cp_lexer_consume_token (parser->lexer);
29666 else
29667 break;
29669 cp_parser_consume_semicolon_at_end_of_statement (parser);
29672 /* Parse a list of Objective-C protocol references.
29674 objc-protocol-refs-opt:
29675 objc-protocol-refs [opt]
29677 objc-protocol-refs:
29678 < objc-identifier-list >
29680 Returns a TREE_LIST of identifiers, if any. */
29682 static tree
29683 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29685 tree protorefs = NULL_TREE;
29687 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29689 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29690 protorefs = cp_parser_objc_identifier_list (parser);
29691 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29694 return protorefs;
29697 /* Parse a Objective-C visibility specification. */
29699 static void
29700 cp_parser_objc_visibility_spec (cp_parser* parser)
29702 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29704 switch (vis->keyword)
29706 case RID_AT_PRIVATE:
29707 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29708 break;
29709 case RID_AT_PROTECTED:
29710 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29711 break;
29712 case RID_AT_PUBLIC:
29713 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29714 break;
29715 case RID_AT_PACKAGE:
29716 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29717 break;
29718 default:
29719 return;
29722 /* Eat '@private'/'@protected'/'@public'. */
29723 cp_lexer_consume_token (parser->lexer);
29726 /* Parse an Objective-C method type. Return 'true' if it is a class
29727 (+) method, and 'false' if it is an instance (-) method. */
29729 static inline bool
29730 cp_parser_objc_method_type (cp_parser* parser)
29732 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29733 return true;
29734 else
29735 return false;
29738 /* Parse an Objective-C protocol qualifier. */
29740 static tree
29741 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29743 tree quals = NULL_TREE, node;
29744 cp_token *token = cp_lexer_peek_token (parser->lexer);
29746 node = token->u.value;
29748 while (node && identifier_p (node)
29749 && (node == ridpointers [(int) RID_IN]
29750 || node == ridpointers [(int) RID_OUT]
29751 || node == ridpointers [(int) RID_INOUT]
29752 || node == ridpointers [(int) RID_BYCOPY]
29753 || node == ridpointers [(int) RID_BYREF]
29754 || node == ridpointers [(int) RID_ONEWAY]))
29756 quals = tree_cons (NULL_TREE, node, quals);
29757 cp_lexer_consume_token (parser->lexer);
29758 token = cp_lexer_peek_token (parser->lexer);
29759 node = token->u.value;
29762 return quals;
29765 /* Parse an Objective-C typename. */
29767 static tree
29768 cp_parser_objc_typename (cp_parser* parser)
29770 tree type_name = NULL_TREE;
29772 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29774 tree proto_quals, cp_type = NULL_TREE;
29776 matching_parens parens;
29777 parens.consume_open (parser); /* Eat '('. */
29778 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29780 /* An ObjC type name may consist of just protocol qualifiers, in which
29781 case the type shall default to 'id'. */
29782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29784 cp_type = cp_parser_type_id (parser);
29786 /* If the type could not be parsed, an error has already
29787 been produced. For error recovery, behave as if it had
29788 not been specified, which will use the default type
29789 'id'. */
29790 if (cp_type == error_mark_node)
29792 cp_type = NULL_TREE;
29793 /* We need to skip to the closing parenthesis as
29794 cp_parser_type_id() does not seem to do it for
29795 us. */
29796 cp_parser_skip_to_closing_parenthesis (parser,
29797 /*recovering=*/true,
29798 /*or_comma=*/false,
29799 /*consume_paren=*/false);
29803 parens.require_close (parser);
29804 type_name = build_tree_list (proto_quals, cp_type);
29807 return type_name;
29810 /* Check to see if TYPE refers to an Objective-C selector name. */
29812 static bool
29813 cp_parser_objc_selector_p (enum cpp_ttype type)
29815 return (type == CPP_NAME || type == CPP_KEYWORD
29816 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29817 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29818 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29819 || type == CPP_XOR || type == CPP_XOR_EQ);
29822 /* Parse an Objective-C selector. */
29824 static tree
29825 cp_parser_objc_selector (cp_parser* parser)
29827 cp_token *token = cp_lexer_consume_token (parser->lexer);
29829 if (!cp_parser_objc_selector_p (token->type))
29831 error_at (token->location, "invalid Objective-C++ selector name");
29832 return error_mark_node;
29835 /* C++ operator names are allowed to appear in ObjC selectors. */
29836 switch (token->type)
29838 case CPP_AND_AND: return get_identifier ("and");
29839 case CPP_AND_EQ: return get_identifier ("and_eq");
29840 case CPP_AND: return get_identifier ("bitand");
29841 case CPP_OR: return get_identifier ("bitor");
29842 case CPP_COMPL: return get_identifier ("compl");
29843 case CPP_NOT: return get_identifier ("not");
29844 case CPP_NOT_EQ: return get_identifier ("not_eq");
29845 case CPP_OR_OR: return get_identifier ("or");
29846 case CPP_OR_EQ: return get_identifier ("or_eq");
29847 case CPP_XOR: return get_identifier ("xor");
29848 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29849 default: return token->u.value;
29853 /* Parse an Objective-C params list. */
29855 static tree
29856 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29858 tree params = NULL_TREE;
29859 bool maybe_unary_selector_p = true;
29860 cp_token *token = cp_lexer_peek_token (parser->lexer);
29862 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29864 tree selector = NULL_TREE, type_name, identifier;
29865 tree parm_attr = NULL_TREE;
29867 if (token->keyword == RID_ATTRIBUTE)
29868 break;
29870 if (token->type != CPP_COLON)
29871 selector = cp_parser_objc_selector (parser);
29873 /* Detect if we have a unary selector. */
29874 if (maybe_unary_selector_p
29875 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29877 params = selector; /* Might be followed by attributes. */
29878 break;
29881 maybe_unary_selector_p = false;
29882 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29884 /* Something went quite wrong. There should be a colon
29885 here, but there is not. Stop parsing parameters. */
29886 break;
29888 type_name = cp_parser_objc_typename (parser);
29889 /* New ObjC allows attributes on parameters too. */
29890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29891 parm_attr = cp_parser_attributes_opt (parser);
29892 identifier = cp_parser_identifier (parser);
29894 params
29895 = chainon (params,
29896 objc_build_keyword_decl (selector,
29897 type_name,
29898 identifier,
29899 parm_attr));
29901 token = cp_lexer_peek_token (parser->lexer);
29904 if (params == NULL_TREE)
29906 cp_parser_error (parser, "objective-c++ method declaration is expected");
29907 return error_mark_node;
29910 /* We allow tail attributes for the method. */
29911 if (token->keyword == RID_ATTRIBUTE)
29913 *attributes = cp_parser_attributes_opt (parser);
29914 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29915 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29916 return params;
29917 cp_parser_error (parser,
29918 "method attributes must be specified at the end");
29919 return error_mark_node;
29922 if (params == NULL_TREE)
29924 cp_parser_error (parser, "objective-c++ method declaration is expected");
29925 return error_mark_node;
29927 return params;
29930 /* Parse the non-keyword Objective-C params. */
29932 static tree
29933 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29934 tree* attributes)
29936 tree params = make_node (TREE_LIST);
29937 cp_token *token = cp_lexer_peek_token (parser->lexer);
29938 *ellipsisp = false; /* Initially, assume no ellipsis. */
29940 while (token->type == CPP_COMMA)
29942 cp_parameter_declarator *parmdecl;
29943 tree parm;
29945 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29946 token = cp_lexer_peek_token (parser->lexer);
29948 if (token->type == CPP_ELLIPSIS)
29950 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29951 *ellipsisp = true;
29952 token = cp_lexer_peek_token (parser->lexer);
29953 break;
29956 /* TODO: parse attributes for tail parameters. */
29957 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29958 parm = grokdeclarator (parmdecl->declarator,
29959 &parmdecl->decl_specifiers,
29960 PARM, /*initialized=*/0,
29961 /*attrlist=*/NULL);
29963 chainon (params, build_tree_list (NULL_TREE, parm));
29964 token = cp_lexer_peek_token (parser->lexer);
29967 /* We allow tail attributes for the method. */
29968 if (token->keyword == RID_ATTRIBUTE)
29970 if (*attributes == NULL_TREE)
29972 *attributes = cp_parser_attributes_opt (parser);
29973 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29974 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29975 return params;
29977 else
29978 /* We have an error, but parse the attributes, so that we can
29979 carry on. */
29980 *attributes = cp_parser_attributes_opt (parser);
29982 cp_parser_error (parser,
29983 "method attributes must be specified at the end");
29984 return error_mark_node;
29987 return params;
29990 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29992 static void
29993 cp_parser_objc_interstitial_code (cp_parser* parser)
29995 cp_token *token = cp_lexer_peek_token (parser->lexer);
29997 /* If the next token is `extern' and the following token is a string
29998 literal, then we have a linkage specification. */
29999 if (token->keyword == RID_EXTERN
30000 && cp_parser_is_pure_string_literal
30001 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30002 cp_parser_linkage_specification (parser);
30003 /* Handle #pragma, if any. */
30004 else if (token->type == CPP_PRAGMA)
30005 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30006 /* Allow stray semicolons. */
30007 else if (token->type == CPP_SEMICOLON)
30008 cp_lexer_consume_token (parser->lexer);
30009 /* Mark methods as optional or required, when building protocols. */
30010 else if (token->keyword == RID_AT_OPTIONAL)
30012 cp_lexer_consume_token (parser->lexer);
30013 objc_set_method_opt (true);
30015 else if (token->keyword == RID_AT_REQUIRED)
30017 cp_lexer_consume_token (parser->lexer);
30018 objc_set_method_opt (false);
30020 else if (token->keyword == RID_NAMESPACE)
30021 cp_parser_namespace_definition (parser);
30022 /* Other stray characters must generate errors. */
30023 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30025 cp_lexer_consume_token (parser->lexer);
30026 error ("stray %qs between Objective-C++ methods",
30027 token->type == CPP_OPEN_BRACE ? "{" : "}");
30029 /* Finally, try to parse a block-declaration, or a function-definition. */
30030 else
30031 cp_parser_block_declaration (parser, /*statement_p=*/false);
30034 /* Parse a method signature. */
30036 static tree
30037 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30039 tree rettype, kwdparms, optparms;
30040 bool ellipsis = false;
30041 bool is_class_method;
30043 is_class_method = cp_parser_objc_method_type (parser);
30044 rettype = cp_parser_objc_typename (parser);
30045 *attributes = NULL_TREE;
30046 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30047 if (kwdparms == error_mark_node)
30048 return error_mark_node;
30049 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30050 if (optparms == error_mark_node)
30051 return error_mark_node;
30053 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30056 static bool
30057 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30059 tree tattr;
30060 cp_lexer_save_tokens (parser->lexer);
30061 tattr = cp_parser_attributes_opt (parser);
30062 gcc_assert (tattr) ;
30064 /* If the attributes are followed by a method introducer, this is not allowed.
30065 Dump the attributes and flag the situation. */
30066 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30067 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30068 return true;
30070 /* Otherwise, the attributes introduce some interstitial code, possibly so
30071 rewind to allow that check. */
30072 cp_lexer_rollback_tokens (parser->lexer);
30073 return false;
30076 /* Parse an Objective-C method prototype list. */
30078 static void
30079 cp_parser_objc_method_prototype_list (cp_parser* parser)
30081 cp_token *token = cp_lexer_peek_token (parser->lexer);
30083 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30085 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30087 tree attributes, sig;
30088 bool is_class_method;
30089 if (token->type == CPP_PLUS)
30090 is_class_method = true;
30091 else
30092 is_class_method = false;
30093 sig = cp_parser_objc_method_signature (parser, &attributes);
30094 if (sig == error_mark_node)
30096 cp_parser_skip_to_end_of_block_or_statement (parser);
30097 token = cp_lexer_peek_token (parser->lexer);
30098 continue;
30100 objc_add_method_declaration (is_class_method, sig, attributes);
30101 cp_parser_consume_semicolon_at_end_of_statement (parser);
30103 else if (token->keyword == RID_AT_PROPERTY)
30104 cp_parser_objc_at_property_declaration (parser);
30105 else if (token->keyword == RID_ATTRIBUTE
30106 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30107 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30108 OPT_Wattributes,
30109 "prefix attributes are ignored for methods");
30110 else
30111 /* Allow for interspersed non-ObjC++ code. */
30112 cp_parser_objc_interstitial_code (parser);
30114 token = cp_lexer_peek_token (parser->lexer);
30117 if (token->type != CPP_EOF)
30118 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30119 else
30120 cp_parser_error (parser, "expected %<@end%>");
30122 objc_finish_interface ();
30125 /* Parse an Objective-C method definition list. */
30127 static void
30128 cp_parser_objc_method_definition_list (cp_parser* parser)
30130 cp_token *token = cp_lexer_peek_token (parser->lexer);
30132 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30134 tree meth;
30136 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30138 cp_token *ptk;
30139 tree sig, attribute;
30140 bool is_class_method;
30141 if (token->type == CPP_PLUS)
30142 is_class_method = true;
30143 else
30144 is_class_method = false;
30145 push_deferring_access_checks (dk_deferred);
30146 sig = cp_parser_objc_method_signature (parser, &attribute);
30147 if (sig == error_mark_node)
30149 cp_parser_skip_to_end_of_block_or_statement (parser);
30150 token = cp_lexer_peek_token (parser->lexer);
30151 continue;
30153 objc_start_method_definition (is_class_method, sig, attribute,
30154 NULL_TREE);
30156 /* For historical reasons, we accept an optional semicolon. */
30157 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30158 cp_lexer_consume_token (parser->lexer);
30160 ptk = cp_lexer_peek_token (parser->lexer);
30161 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30162 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30164 perform_deferred_access_checks (tf_warning_or_error);
30165 stop_deferring_access_checks ();
30166 meth = cp_parser_function_definition_after_declarator (parser,
30167 false);
30168 pop_deferring_access_checks ();
30169 objc_finish_method_definition (meth);
30172 /* The following case will be removed once @synthesize is
30173 completely implemented. */
30174 else if (token->keyword == RID_AT_PROPERTY)
30175 cp_parser_objc_at_property_declaration (parser);
30176 else if (token->keyword == RID_AT_SYNTHESIZE)
30177 cp_parser_objc_at_synthesize_declaration (parser);
30178 else if (token->keyword == RID_AT_DYNAMIC)
30179 cp_parser_objc_at_dynamic_declaration (parser);
30180 else if (token->keyword == RID_ATTRIBUTE
30181 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30182 warning_at (token->location, OPT_Wattributes,
30183 "prefix attributes are ignored for methods");
30184 else
30185 /* Allow for interspersed non-ObjC++ code. */
30186 cp_parser_objc_interstitial_code (parser);
30188 token = cp_lexer_peek_token (parser->lexer);
30191 if (token->type != CPP_EOF)
30192 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30193 else
30194 cp_parser_error (parser, "expected %<@end%>");
30196 objc_finish_implementation ();
30199 /* Parse Objective-C ivars. */
30201 static void
30202 cp_parser_objc_class_ivars (cp_parser* parser)
30204 cp_token *token = cp_lexer_peek_token (parser->lexer);
30206 if (token->type != CPP_OPEN_BRACE)
30207 return; /* No ivars specified. */
30209 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30210 token = cp_lexer_peek_token (parser->lexer);
30212 while (token->type != CPP_CLOSE_BRACE
30213 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30215 cp_decl_specifier_seq declspecs;
30216 int decl_class_or_enum_p;
30217 tree prefix_attributes;
30219 cp_parser_objc_visibility_spec (parser);
30221 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30222 break;
30224 cp_parser_decl_specifier_seq (parser,
30225 CP_PARSER_FLAGS_OPTIONAL,
30226 &declspecs,
30227 &decl_class_or_enum_p);
30229 /* auto, register, static, extern, mutable. */
30230 if (declspecs.storage_class != sc_none)
30232 cp_parser_error (parser, "invalid type for instance variable");
30233 declspecs.storage_class = sc_none;
30236 /* thread_local. */
30237 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30239 cp_parser_error (parser, "invalid type for instance variable");
30240 declspecs.locations[ds_thread] = 0;
30243 /* typedef. */
30244 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30246 cp_parser_error (parser, "invalid type for instance variable");
30247 declspecs.locations[ds_typedef] = 0;
30250 prefix_attributes = declspecs.attributes;
30251 declspecs.attributes = NULL_TREE;
30253 /* Keep going until we hit the `;' at the end of the
30254 declaration. */
30255 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30257 tree width = NULL_TREE, attributes, first_attribute, decl;
30258 cp_declarator *declarator = NULL;
30259 int ctor_dtor_or_conv_p;
30261 /* Check for a (possibly unnamed) bitfield declaration. */
30262 token = cp_lexer_peek_token (parser->lexer);
30263 if (token->type == CPP_COLON)
30264 goto eat_colon;
30266 if (token->type == CPP_NAME
30267 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30268 == CPP_COLON))
30270 /* Get the name of the bitfield. */
30271 declarator = make_id_declarator (NULL_TREE,
30272 cp_parser_identifier (parser),
30273 sfk_none);
30275 eat_colon:
30276 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30277 /* Get the width of the bitfield. */
30278 width
30279 = cp_parser_constant_expression (parser);
30281 else
30283 /* Parse the declarator. */
30284 declarator
30285 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30286 &ctor_dtor_or_conv_p,
30287 /*parenthesized_p=*/NULL,
30288 /*member_p=*/false,
30289 /*friend_p=*/false);
30292 /* Look for attributes that apply to the ivar. */
30293 attributes = cp_parser_attributes_opt (parser);
30294 /* Remember which attributes are prefix attributes and
30295 which are not. */
30296 first_attribute = attributes;
30297 /* Combine the attributes. */
30298 attributes = attr_chainon (prefix_attributes, attributes);
30300 if (width)
30301 /* Create the bitfield declaration. */
30302 decl = grokbitfield (declarator, &declspecs,
30303 width, NULL_TREE, attributes);
30304 else
30305 decl = grokfield (declarator, &declspecs,
30306 NULL_TREE, /*init_const_expr_p=*/false,
30307 NULL_TREE, attributes);
30309 /* Add the instance variable. */
30310 if (decl != error_mark_node && decl != NULL_TREE)
30311 objc_add_instance_variable (decl);
30313 /* Reset PREFIX_ATTRIBUTES. */
30314 if (attributes != error_mark_node)
30316 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30317 attributes = TREE_CHAIN (attributes);
30318 if (attributes)
30319 TREE_CHAIN (attributes) = NULL_TREE;
30322 token = cp_lexer_peek_token (parser->lexer);
30324 if (token->type == CPP_COMMA)
30326 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30327 continue;
30329 break;
30332 cp_parser_consume_semicolon_at_end_of_statement (parser);
30333 token = cp_lexer_peek_token (parser->lexer);
30336 if (token->keyword == RID_AT_END)
30337 cp_parser_error (parser, "expected %<}%>");
30339 /* Do not consume the RID_AT_END, so it will be read again as terminating
30340 the @interface of @implementation. */
30341 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30342 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30344 /* For historical reasons, we accept an optional semicolon. */
30345 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30346 cp_lexer_consume_token (parser->lexer);
30349 /* Parse an Objective-C protocol declaration. */
30351 static void
30352 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30354 tree proto, protorefs;
30355 cp_token *tok;
30357 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30360 tok = cp_lexer_peek_token (parser->lexer);
30361 error_at (tok->location, "identifier expected after %<@protocol%>");
30362 cp_parser_consume_semicolon_at_end_of_statement (parser);
30363 return;
30366 /* See if we have a forward declaration or a definition. */
30367 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30369 /* Try a forward declaration first. */
30370 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30372 while (true)
30374 tree id;
30376 id = cp_parser_identifier (parser);
30377 if (id == error_mark_node)
30378 break;
30380 objc_declare_protocol (id, attributes);
30382 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30383 cp_lexer_consume_token (parser->lexer);
30384 else
30385 break;
30387 cp_parser_consume_semicolon_at_end_of_statement (parser);
30390 /* Ok, we got a full-fledged definition (or at least should). */
30391 else
30393 proto = cp_parser_identifier (parser);
30394 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30395 objc_start_protocol (proto, protorefs, attributes);
30396 cp_parser_objc_method_prototype_list (parser);
30400 /* Parse an Objective-C superclass or category. */
30402 static void
30403 cp_parser_objc_superclass_or_category (cp_parser *parser,
30404 bool iface_p,
30405 tree *super,
30406 tree *categ, bool *is_class_extension)
30408 cp_token *next = cp_lexer_peek_token (parser->lexer);
30410 *super = *categ = NULL_TREE;
30411 *is_class_extension = false;
30412 if (next->type == CPP_COLON)
30414 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30415 *super = cp_parser_identifier (parser);
30417 else if (next->type == CPP_OPEN_PAREN)
30419 matching_parens parens;
30420 parens.consume_open (parser); /* Eat '('. */
30422 /* If there is no category name, and this is an @interface, we
30423 have a class extension. */
30424 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30426 *categ = NULL_TREE;
30427 *is_class_extension = true;
30429 else
30430 *categ = cp_parser_identifier (parser);
30432 parens.require_close (parser);
30436 /* Parse an Objective-C class interface. */
30438 static void
30439 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30441 tree name, super, categ, protos;
30442 bool is_class_extension;
30444 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30445 name = cp_parser_identifier (parser);
30446 if (name == error_mark_node)
30448 /* It's hard to recover because even if valid @interface stuff
30449 is to follow, we can't compile it (or validate it) if we
30450 don't even know which class it refers to. Let's assume this
30451 was a stray '@interface' token in the stream and skip it.
30453 return;
30455 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30456 &is_class_extension);
30457 protos = cp_parser_objc_protocol_refs_opt (parser);
30459 /* We have either a class or a category on our hands. */
30460 if (categ || is_class_extension)
30461 objc_start_category_interface (name, categ, protos, attributes);
30462 else
30464 objc_start_class_interface (name, super, protos, attributes);
30465 /* Handle instance variable declarations, if any. */
30466 cp_parser_objc_class_ivars (parser);
30467 objc_continue_interface ();
30470 cp_parser_objc_method_prototype_list (parser);
30473 /* Parse an Objective-C class implementation. */
30475 static void
30476 cp_parser_objc_class_implementation (cp_parser* parser)
30478 tree name, super, categ;
30479 bool is_class_extension;
30481 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30482 name = cp_parser_identifier (parser);
30483 if (name == error_mark_node)
30485 /* It's hard to recover because even if valid @implementation
30486 stuff is to follow, we can't compile it (or validate it) if
30487 we don't even know which class it refers to. Let's assume
30488 this was a stray '@implementation' token in the stream and
30489 skip it.
30491 return;
30493 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30494 &is_class_extension);
30496 /* We have either a class or a category on our hands. */
30497 if (categ)
30498 objc_start_category_implementation (name, categ);
30499 else
30501 objc_start_class_implementation (name, super);
30502 /* Handle instance variable declarations, if any. */
30503 cp_parser_objc_class_ivars (parser);
30504 objc_continue_implementation ();
30507 cp_parser_objc_method_definition_list (parser);
30510 /* Consume the @end token and finish off the implementation. */
30512 static void
30513 cp_parser_objc_end_implementation (cp_parser* parser)
30515 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30516 objc_finish_implementation ();
30519 /* Parse an Objective-C declaration. */
30521 static void
30522 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30524 /* Try to figure out what kind of declaration is present. */
30525 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30527 if (attributes)
30528 switch (kwd->keyword)
30530 case RID_AT_ALIAS:
30531 case RID_AT_CLASS:
30532 case RID_AT_END:
30533 error_at (kwd->location, "attributes may not be specified before"
30534 " the %<@%D%> Objective-C++ keyword",
30535 kwd->u.value);
30536 attributes = NULL;
30537 break;
30538 case RID_AT_IMPLEMENTATION:
30539 warning_at (kwd->location, OPT_Wattributes,
30540 "prefix attributes are ignored before %<@%D%>",
30541 kwd->u.value);
30542 attributes = NULL;
30543 default:
30544 break;
30547 switch (kwd->keyword)
30549 case RID_AT_ALIAS:
30550 cp_parser_objc_alias_declaration (parser);
30551 break;
30552 case RID_AT_CLASS:
30553 cp_parser_objc_class_declaration (parser);
30554 break;
30555 case RID_AT_PROTOCOL:
30556 cp_parser_objc_protocol_declaration (parser, attributes);
30557 break;
30558 case RID_AT_INTERFACE:
30559 cp_parser_objc_class_interface (parser, attributes);
30560 break;
30561 case RID_AT_IMPLEMENTATION:
30562 cp_parser_objc_class_implementation (parser);
30563 break;
30564 case RID_AT_END:
30565 cp_parser_objc_end_implementation (parser);
30566 break;
30567 default:
30568 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30569 kwd->u.value);
30570 cp_parser_skip_to_end_of_block_or_statement (parser);
30574 /* Parse an Objective-C try-catch-finally statement.
30576 objc-try-catch-finally-stmt:
30577 @try compound-statement objc-catch-clause-seq [opt]
30578 objc-finally-clause [opt]
30580 objc-catch-clause-seq:
30581 objc-catch-clause objc-catch-clause-seq [opt]
30583 objc-catch-clause:
30584 @catch ( objc-exception-declaration ) compound-statement
30586 objc-finally-clause:
30587 @finally compound-statement
30589 objc-exception-declaration:
30590 parameter-declaration
30591 '...'
30593 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30595 Returns NULL_TREE.
30597 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30598 for C. Keep them in sync. */
30600 static tree
30601 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30603 location_t location;
30604 tree stmt;
30606 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30607 location = cp_lexer_peek_token (parser->lexer)->location;
30608 objc_maybe_warn_exceptions (location);
30609 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30610 node, lest it get absorbed into the surrounding block. */
30611 stmt = push_stmt_list ();
30612 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30613 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30615 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30617 cp_parameter_declarator *parm;
30618 tree parameter_declaration = error_mark_node;
30619 bool seen_open_paren = false;
30620 matching_parens parens;
30622 cp_lexer_consume_token (parser->lexer);
30623 if (parens.require_open (parser))
30624 seen_open_paren = true;
30625 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30627 /* We have "@catch (...)" (where the '...' are literally
30628 what is in the code). Skip the '...'.
30629 parameter_declaration is set to NULL_TREE, and
30630 objc_being_catch_clauses() knows that that means
30631 '...'. */
30632 cp_lexer_consume_token (parser->lexer);
30633 parameter_declaration = NULL_TREE;
30635 else
30637 /* We have "@catch (NSException *exception)" or something
30638 like that. Parse the parameter declaration. */
30639 parm = cp_parser_parameter_declaration (parser, false, NULL);
30640 if (parm == NULL)
30641 parameter_declaration = error_mark_node;
30642 else
30643 parameter_declaration = grokdeclarator (parm->declarator,
30644 &parm->decl_specifiers,
30645 PARM, /*initialized=*/0,
30646 /*attrlist=*/NULL);
30648 if (seen_open_paren)
30649 parens.require_close (parser);
30650 else
30652 /* If there was no open parenthesis, we are recovering from
30653 an error, and we are trying to figure out what mistake
30654 the user has made. */
30656 /* If there is an immediate closing parenthesis, the user
30657 probably forgot the opening one (ie, they typed "@catch
30658 NSException *e)". Parse the closing parenthesis and keep
30659 going. */
30660 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30661 cp_lexer_consume_token (parser->lexer);
30663 /* If these is no immediate closing parenthesis, the user
30664 probably doesn't know that parenthesis are required at
30665 all (ie, they typed "@catch NSException *e"). So, just
30666 forget about the closing parenthesis and keep going. */
30668 objc_begin_catch_clause (parameter_declaration);
30669 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30670 objc_finish_catch_clause ();
30672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30674 cp_lexer_consume_token (parser->lexer);
30675 location = cp_lexer_peek_token (parser->lexer)->location;
30676 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30677 node, lest it get absorbed into the surrounding block. */
30678 stmt = push_stmt_list ();
30679 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30680 objc_build_finally_clause (location, pop_stmt_list (stmt));
30683 return objc_finish_try_stmt ();
30686 /* Parse an Objective-C synchronized statement.
30688 objc-synchronized-stmt:
30689 @synchronized ( expression ) compound-statement
30691 Returns NULL_TREE. */
30693 static tree
30694 cp_parser_objc_synchronized_statement (cp_parser *parser)
30696 location_t location;
30697 tree lock, stmt;
30699 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30701 location = cp_lexer_peek_token (parser->lexer)->location;
30702 objc_maybe_warn_exceptions (location);
30703 matching_parens parens;
30704 parens.require_open (parser);
30705 lock = cp_parser_expression (parser);
30706 parens.require_close (parser);
30708 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30709 node, lest it get absorbed into the surrounding block. */
30710 stmt = push_stmt_list ();
30711 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30713 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30716 /* Parse an Objective-C throw statement.
30718 objc-throw-stmt:
30719 @throw assignment-expression [opt] ;
30721 Returns a constructed '@throw' statement. */
30723 static tree
30724 cp_parser_objc_throw_statement (cp_parser *parser)
30726 tree expr = NULL_TREE;
30727 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30729 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30732 expr = cp_parser_expression (parser);
30734 cp_parser_consume_semicolon_at_end_of_statement (parser);
30736 return objc_build_throw_stmt (loc, expr);
30739 /* Parse an Objective-C statement. */
30741 static tree
30742 cp_parser_objc_statement (cp_parser * parser)
30744 /* Try to figure out what kind of declaration is present. */
30745 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30747 switch (kwd->keyword)
30749 case RID_AT_TRY:
30750 return cp_parser_objc_try_catch_finally_statement (parser);
30751 case RID_AT_SYNCHRONIZED:
30752 return cp_parser_objc_synchronized_statement (parser);
30753 case RID_AT_THROW:
30754 return cp_parser_objc_throw_statement (parser);
30755 default:
30756 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30757 kwd->u.value);
30758 cp_parser_skip_to_end_of_block_or_statement (parser);
30761 return error_mark_node;
30764 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30765 look ahead to see if an objc keyword follows the attributes. This
30766 is to detect the use of prefix attributes on ObjC @interface and
30767 @protocol. */
30769 static bool
30770 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30772 cp_lexer_save_tokens (parser->lexer);
30773 *attrib = cp_parser_attributes_opt (parser);
30774 gcc_assert (*attrib);
30775 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30777 cp_lexer_commit_tokens (parser->lexer);
30778 return true;
30780 cp_lexer_rollback_tokens (parser->lexer);
30781 return false;
30784 /* This routine is a minimal replacement for
30785 c_parser_struct_declaration () used when parsing the list of
30786 types/names or ObjC++ properties. For example, when parsing the
30787 code
30789 @property (readonly) int a, b, c;
30791 this function is responsible for parsing "int a, int b, int c" and
30792 returning the declarations as CHAIN of DECLs.
30794 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30795 similar parsing. */
30796 static tree
30797 cp_parser_objc_struct_declaration (cp_parser *parser)
30799 tree decls = NULL_TREE;
30800 cp_decl_specifier_seq declspecs;
30801 int decl_class_or_enum_p;
30802 tree prefix_attributes;
30804 cp_parser_decl_specifier_seq (parser,
30805 CP_PARSER_FLAGS_NONE,
30806 &declspecs,
30807 &decl_class_or_enum_p);
30809 if (declspecs.type == error_mark_node)
30810 return error_mark_node;
30812 /* auto, register, static, extern, mutable. */
30813 if (declspecs.storage_class != sc_none)
30815 cp_parser_error (parser, "invalid type for property");
30816 declspecs.storage_class = sc_none;
30819 /* thread_local. */
30820 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30822 cp_parser_error (parser, "invalid type for property");
30823 declspecs.locations[ds_thread] = 0;
30826 /* typedef. */
30827 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30829 cp_parser_error (parser, "invalid type for property");
30830 declspecs.locations[ds_typedef] = 0;
30833 prefix_attributes = declspecs.attributes;
30834 declspecs.attributes = NULL_TREE;
30836 /* Keep going until we hit the `;' at the end of the declaration. */
30837 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30839 tree attributes, first_attribute, decl;
30840 cp_declarator *declarator;
30841 cp_token *token;
30843 /* Parse the declarator. */
30844 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30845 NULL, NULL, false, false);
30847 /* Look for attributes that apply to the ivar. */
30848 attributes = cp_parser_attributes_opt (parser);
30849 /* Remember which attributes are prefix attributes and
30850 which are not. */
30851 first_attribute = attributes;
30852 /* Combine the attributes. */
30853 attributes = attr_chainon (prefix_attributes, attributes);
30855 decl = grokfield (declarator, &declspecs,
30856 NULL_TREE, /*init_const_expr_p=*/false,
30857 NULL_TREE, attributes);
30859 if (decl == error_mark_node || decl == NULL_TREE)
30860 return error_mark_node;
30862 /* Reset PREFIX_ATTRIBUTES. */
30863 if (attributes != error_mark_node)
30865 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30866 attributes = TREE_CHAIN (attributes);
30867 if (attributes)
30868 TREE_CHAIN (attributes) = NULL_TREE;
30871 DECL_CHAIN (decl) = decls;
30872 decls = decl;
30874 token = cp_lexer_peek_token (parser->lexer);
30875 if (token->type == CPP_COMMA)
30877 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30878 continue;
30880 else
30881 break;
30883 return decls;
30886 /* Parse an Objective-C @property declaration. The syntax is:
30888 objc-property-declaration:
30889 '@property' objc-property-attributes[opt] struct-declaration ;
30891 objc-property-attributes:
30892 '(' objc-property-attribute-list ')'
30894 objc-property-attribute-list:
30895 objc-property-attribute
30896 objc-property-attribute-list, objc-property-attribute
30898 objc-property-attribute
30899 'getter' = identifier
30900 'setter' = identifier
30901 'readonly'
30902 'readwrite'
30903 'assign'
30904 'retain'
30905 'copy'
30906 'nonatomic'
30908 For example:
30909 @property NSString *name;
30910 @property (readonly) id object;
30911 @property (retain, nonatomic, getter=getTheName) id name;
30912 @property int a, b, c;
30914 PS: This function is identical to
30915 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30916 static void
30917 cp_parser_objc_at_property_declaration (cp_parser *parser)
30919 /* The following variables hold the attributes of the properties as
30920 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30921 seen. When we see an attribute, we set them to 'true' (if they
30922 are boolean properties) or to the identifier (if they have an
30923 argument, ie, for getter and setter). Note that here we only
30924 parse the list of attributes, check the syntax and accumulate the
30925 attributes that we find. objc_add_property_declaration() will
30926 then process the information. */
30927 bool property_assign = false;
30928 bool property_copy = false;
30929 tree property_getter_ident = NULL_TREE;
30930 bool property_nonatomic = false;
30931 bool property_readonly = false;
30932 bool property_readwrite = false;
30933 bool property_retain = false;
30934 tree property_setter_ident = NULL_TREE;
30936 /* 'properties' is the list of properties that we read. Usually a
30937 single one, but maybe more (eg, in "@property int a, b, c;" there
30938 are three). */
30939 tree properties;
30940 location_t loc;
30942 loc = cp_lexer_peek_token (parser->lexer)->location;
30944 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30946 /* Parse the optional attribute list... */
30947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30949 /* Eat the '('. */
30950 matching_parens parens;
30951 parens.consume_open (parser);
30953 while (true)
30955 bool syntax_error = false;
30956 cp_token *token = cp_lexer_peek_token (parser->lexer);
30957 enum rid keyword;
30959 if (token->type != CPP_NAME)
30961 cp_parser_error (parser, "expected identifier");
30962 break;
30964 keyword = C_RID_CODE (token->u.value);
30965 cp_lexer_consume_token (parser->lexer);
30966 switch (keyword)
30968 case RID_ASSIGN: property_assign = true; break;
30969 case RID_COPY: property_copy = true; break;
30970 case RID_NONATOMIC: property_nonatomic = true; break;
30971 case RID_READONLY: property_readonly = true; break;
30972 case RID_READWRITE: property_readwrite = true; break;
30973 case RID_RETAIN: property_retain = true; break;
30975 case RID_GETTER:
30976 case RID_SETTER:
30977 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30979 if (keyword == RID_GETTER)
30980 cp_parser_error (parser,
30981 "missing %<=%> (after %<getter%> attribute)");
30982 else
30983 cp_parser_error (parser,
30984 "missing %<=%> (after %<setter%> attribute)");
30985 syntax_error = true;
30986 break;
30988 cp_lexer_consume_token (parser->lexer); /* eat the = */
30989 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30991 cp_parser_error (parser, "expected identifier");
30992 syntax_error = true;
30993 break;
30995 if (keyword == RID_SETTER)
30997 if (property_setter_ident != NULL_TREE)
30999 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31000 cp_lexer_consume_token (parser->lexer);
31002 else
31003 property_setter_ident = cp_parser_objc_selector (parser);
31004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31005 cp_parser_error (parser, "setter name must terminate with %<:%>");
31006 else
31007 cp_lexer_consume_token (parser->lexer);
31009 else
31011 if (property_getter_ident != NULL_TREE)
31013 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31014 cp_lexer_consume_token (parser->lexer);
31016 else
31017 property_getter_ident = cp_parser_objc_selector (parser);
31019 break;
31020 default:
31021 cp_parser_error (parser, "unknown property attribute");
31022 syntax_error = true;
31023 break;
31026 if (syntax_error)
31027 break;
31029 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31030 cp_lexer_consume_token (parser->lexer);
31031 else
31032 break;
31035 /* FIXME: "@property (setter, assign);" will generate a spurious
31036 "error: expected ‘)’ before ‘,’ token". This is because
31037 cp_parser_require, unlike the C counterpart, will produce an
31038 error even if we are in error recovery. */
31039 if (!parens.require_close (parser))
31041 cp_parser_skip_to_closing_parenthesis (parser,
31042 /*recovering=*/true,
31043 /*or_comma=*/false,
31044 /*consume_paren=*/true);
31048 /* ... and the property declaration(s). */
31049 properties = cp_parser_objc_struct_declaration (parser);
31051 if (properties == error_mark_node)
31053 cp_parser_skip_to_end_of_statement (parser);
31054 /* If the next token is now a `;', consume it. */
31055 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31056 cp_lexer_consume_token (parser->lexer);
31057 return;
31060 if (properties == NULL_TREE)
31061 cp_parser_error (parser, "expected identifier");
31062 else
31064 /* Comma-separated properties are chained together in
31065 reverse order; add them one by one. */
31066 properties = nreverse (properties);
31068 for (; properties; properties = TREE_CHAIN (properties))
31069 objc_add_property_declaration (loc, copy_node (properties),
31070 property_readonly, property_readwrite,
31071 property_assign, property_retain,
31072 property_copy, property_nonatomic,
31073 property_getter_ident, property_setter_ident);
31076 cp_parser_consume_semicolon_at_end_of_statement (parser);
31079 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31081 objc-synthesize-declaration:
31082 @synthesize objc-synthesize-identifier-list ;
31084 objc-synthesize-identifier-list:
31085 objc-synthesize-identifier
31086 objc-synthesize-identifier-list, objc-synthesize-identifier
31088 objc-synthesize-identifier
31089 identifier
31090 identifier = identifier
31092 For example:
31093 @synthesize MyProperty;
31094 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31096 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31097 for C. Keep them in sync.
31099 static void
31100 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31102 tree list = NULL_TREE;
31103 location_t loc;
31104 loc = cp_lexer_peek_token (parser->lexer)->location;
31106 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31107 while (true)
31109 tree property, ivar;
31110 property = cp_parser_identifier (parser);
31111 if (property == error_mark_node)
31113 cp_parser_consume_semicolon_at_end_of_statement (parser);
31114 return;
31116 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31118 cp_lexer_consume_token (parser->lexer);
31119 ivar = cp_parser_identifier (parser);
31120 if (ivar == error_mark_node)
31122 cp_parser_consume_semicolon_at_end_of_statement (parser);
31123 return;
31126 else
31127 ivar = NULL_TREE;
31128 list = chainon (list, build_tree_list (ivar, property));
31129 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31130 cp_lexer_consume_token (parser->lexer);
31131 else
31132 break;
31134 cp_parser_consume_semicolon_at_end_of_statement (parser);
31135 objc_add_synthesize_declaration (loc, list);
31138 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31140 objc-dynamic-declaration:
31141 @dynamic identifier-list ;
31143 For example:
31144 @dynamic MyProperty;
31145 @dynamic MyProperty, AnotherProperty;
31147 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31148 for C. Keep them in sync.
31150 static void
31151 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31153 tree list = NULL_TREE;
31154 location_t loc;
31155 loc = cp_lexer_peek_token (parser->lexer)->location;
31157 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31158 while (true)
31160 tree property;
31161 property = cp_parser_identifier (parser);
31162 if (property == error_mark_node)
31164 cp_parser_consume_semicolon_at_end_of_statement (parser);
31165 return;
31167 list = chainon (list, build_tree_list (NULL, property));
31168 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31169 cp_lexer_consume_token (parser->lexer);
31170 else
31171 break;
31173 cp_parser_consume_semicolon_at_end_of_statement (parser);
31174 objc_add_dynamic_declaration (loc, list);
31178 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31180 /* Returns name of the next clause.
31181 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31182 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31183 returned and the token is consumed. */
31185 static pragma_omp_clause
31186 cp_parser_omp_clause_name (cp_parser *parser)
31188 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31191 result = PRAGMA_OACC_CLAUSE_AUTO;
31192 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31193 result = PRAGMA_OMP_CLAUSE_IF;
31194 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31195 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31196 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31197 result = PRAGMA_OACC_CLAUSE_DELETE;
31198 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31199 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31200 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31201 result = PRAGMA_OMP_CLAUSE_FOR;
31202 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31204 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31205 const char *p = IDENTIFIER_POINTER (id);
31207 switch (p[0])
31209 case 'a':
31210 if (!strcmp ("aligned", p))
31211 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31212 else if (!strcmp ("async", p))
31213 result = PRAGMA_OACC_CLAUSE_ASYNC;
31214 break;
31215 case 'c':
31216 if (!strcmp ("collapse", p))
31217 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31218 else if (!strcmp ("copy", p))
31219 result = PRAGMA_OACC_CLAUSE_COPY;
31220 else if (!strcmp ("copyin", p))
31221 result = PRAGMA_OMP_CLAUSE_COPYIN;
31222 else if (!strcmp ("copyout", p))
31223 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31224 else if (!strcmp ("copyprivate", p))
31225 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31226 else if (!strcmp ("create", p))
31227 result = PRAGMA_OACC_CLAUSE_CREATE;
31228 break;
31229 case 'd':
31230 if (!strcmp ("defaultmap", p))
31231 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31232 else if (!strcmp ("depend", p))
31233 result = PRAGMA_OMP_CLAUSE_DEPEND;
31234 else if (!strcmp ("device", p))
31235 result = PRAGMA_OMP_CLAUSE_DEVICE;
31236 else if (!strcmp ("deviceptr", p))
31237 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31238 else if (!strcmp ("device_resident", p))
31239 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31240 else if (!strcmp ("dist_schedule", p))
31241 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31242 break;
31243 case 'f':
31244 if (!strcmp ("final", p))
31245 result = PRAGMA_OMP_CLAUSE_FINAL;
31246 else if (!strcmp ("firstprivate", p))
31247 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31248 else if (!strcmp ("from", p))
31249 result = PRAGMA_OMP_CLAUSE_FROM;
31250 break;
31251 case 'g':
31252 if (!strcmp ("gang", p))
31253 result = PRAGMA_OACC_CLAUSE_GANG;
31254 else if (!strcmp ("grainsize", p))
31255 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31256 break;
31257 case 'h':
31258 if (!strcmp ("hint", p))
31259 result = PRAGMA_OMP_CLAUSE_HINT;
31260 else if (!strcmp ("host", p))
31261 result = PRAGMA_OACC_CLAUSE_HOST;
31262 break;
31263 case 'i':
31264 if (!strcmp ("inbranch", p))
31265 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31266 else if (!strcmp ("independent", p))
31267 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31268 else if (!strcmp ("is_device_ptr", p))
31269 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31270 break;
31271 case 'l':
31272 if (!strcmp ("lastprivate", p))
31273 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31274 else if (!strcmp ("linear", p))
31275 result = PRAGMA_OMP_CLAUSE_LINEAR;
31276 else if (!strcmp ("link", p))
31277 result = PRAGMA_OMP_CLAUSE_LINK;
31278 break;
31279 case 'm':
31280 if (!strcmp ("map", p))
31281 result = PRAGMA_OMP_CLAUSE_MAP;
31282 else if (!strcmp ("mergeable", p))
31283 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31284 break;
31285 case 'n':
31286 if (!strcmp ("nogroup", p))
31287 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31288 else if (!strcmp ("notinbranch", p))
31289 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31290 else if (!strcmp ("nowait", p))
31291 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31292 else if (!strcmp ("num_gangs", p))
31293 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31294 else if (!strcmp ("num_tasks", p))
31295 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31296 else if (!strcmp ("num_teams", p))
31297 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31298 else if (!strcmp ("num_threads", p))
31299 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31300 else if (!strcmp ("num_workers", p))
31301 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31302 break;
31303 case 'o':
31304 if (!strcmp ("ordered", p))
31305 result = PRAGMA_OMP_CLAUSE_ORDERED;
31306 break;
31307 case 'p':
31308 if (!strcmp ("parallel", p))
31309 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31310 else if (!strcmp ("present", p))
31311 result = PRAGMA_OACC_CLAUSE_PRESENT;
31312 else if (!strcmp ("present_or_copy", p)
31313 || !strcmp ("pcopy", p))
31314 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31315 else if (!strcmp ("present_or_copyin", p)
31316 || !strcmp ("pcopyin", p))
31317 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31318 else if (!strcmp ("present_or_copyout", p)
31319 || !strcmp ("pcopyout", p))
31320 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31321 else if (!strcmp ("present_or_create", p)
31322 || !strcmp ("pcreate", p))
31323 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31324 else if (!strcmp ("priority", p))
31325 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31326 else if (!strcmp ("proc_bind", p))
31327 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31328 break;
31329 case 'r':
31330 if (!strcmp ("reduction", p))
31331 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31332 break;
31333 case 's':
31334 if (!strcmp ("safelen", p))
31335 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31336 else if (!strcmp ("schedule", p))
31337 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31338 else if (!strcmp ("sections", p))
31339 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31340 else if (!strcmp ("self", p))
31341 result = PRAGMA_OACC_CLAUSE_SELF;
31342 else if (!strcmp ("seq", p))
31343 result = PRAGMA_OACC_CLAUSE_SEQ;
31344 else if (!strcmp ("shared", p))
31345 result = PRAGMA_OMP_CLAUSE_SHARED;
31346 else if (!strcmp ("simd", p))
31347 result = PRAGMA_OMP_CLAUSE_SIMD;
31348 else if (!strcmp ("simdlen", p))
31349 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31350 break;
31351 case 't':
31352 if (!strcmp ("taskgroup", p))
31353 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31354 else if (!strcmp ("thread_limit", p))
31355 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31356 else if (!strcmp ("threads", p))
31357 result = PRAGMA_OMP_CLAUSE_THREADS;
31358 else if (!strcmp ("tile", p))
31359 result = PRAGMA_OACC_CLAUSE_TILE;
31360 else if (!strcmp ("to", p))
31361 result = PRAGMA_OMP_CLAUSE_TO;
31362 break;
31363 case 'u':
31364 if (!strcmp ("uniform", p))
31365 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31366 else if (!strcmp ("untied", p))
31367 result = PRAGMA_OMP_CLAUSE_UNTIED;
31368 else if (!strcmp ("use_device", p))
31369 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31370 else if (!strcmp ("use_device_ptr", p))
31371 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31372 break;
31373 case 'v':
31374 if (!strcmp ("vector", p))
31375 result = PRAGMA_OACC_CLAUSE_VECTOR;
31376 else if (!strcmp ("vector_length", p))
31377 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31378 break;
31379 case 'w':
31380 if (!strcmp ("wait", p))
31381 result = PRAGMA_OACC_CLAUSE_WAIT;
31382 else if (!strcmp ("worker", p))
31383 result = PRAGMA_OACC_CLAUSE_WORKER;
31384 break;
31388 if (result != PRAGMA_OMP_CLAUSE_NONE)
31389 cp_lexer_consume_token (parser->lexer);
31391 return result;
31394 /* Validate that a clause of the given type does not already exist. */
31396 static void
31397 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31398 const char *name, location_t location)
31400 tree c;
31402 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31403 if (OMP_CLAUSE_CODE (c) == code)
31405 error_at (location, "too many %qs clauses", name);
31406 break;
31410 /* OpenMP 2.5:
31411 variable-list:
31412 identifier
31413 variable-list , identifier
31415 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31416 colon). An opening parenthesis will have been consumed by the caller.
31418 If KIND is nonzero, create the appropriate node and install the decl
31419 in OMP_CLAUSE_DECL and add the node to the head of the list.
31421 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31422 return the list created.
31424 COLON can be NULL if only closing parenthesis should end the list,
31425 or pointer to bool which will receive false if the list is terminated
31426 by closing parenthesis or true if the list is terminated by colon. */
31428 static tree
31429 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31430 tree list, bool *colon)
31432 cp_token *token;
31433 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31434 if (colon)
31436 parser->colon_corrects_to_scope_p = false;
31437 *colon = false;
31439 while (1)
31441 tree name, decl;
31443 token = cp_lexer_peek_token (parser->lexer);
31444 if (kind != 0
31445 && current_class_ptr
31446 && cp_parser_is_keyword (token, RID_THIS))
31448 decl = finish_this_expr ();
31449 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31450 || CONVERT_EXPR_P (decl))
31451 decl = TREE_OPERAND (decl, 0);
31452 cp_lexer_consume_token (parser->lexer);
31454 else
31456 name = cp_parser_id_expression (parser, /*template_p=*/false,
31457 /*check_dependency_p=*/true,
31458 /*template_p=*/NULL,
31459 /*declarator_p=*/false,
31460 /*optional_p=*/false);
31461 if (name == error_mark_node)
31462 goto skip_comma;
31464 if (identifier_p (name))
31465 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31466 else
31467 decl = name;
31468 if (decl == error_mark_node)
31469 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31470 token->location);
31472 if (decl == error_mark_node)
31474 else if (kind != 0)
31476 switch (kind)
31478 case OMP_CLAUSE__CACHE_:
31479 /* The OpenACC cache directive explicitly only allows "array
31480 elements or subarrays". */
31481 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31483 error_at (token->location, "expected %<[%>");
31484 decl = error_mark_node;
31485 break;
31487 /* FALLTHROUGH. */
31488 case OMP_CLAUSE_MAP:
31489 case OMP_CLAUSE_FROM:
31490 case OMP_CLAUSE_TO:
31491 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31493 location_t loc
31494 = cp_lexer_peek_token (parser->lexer)->location;
31495 cp_id_kind idk = CP_ID_KIND_NONE;
31496 cp_lexer_consume_token (parser->lexer);
31497 decl = convert_from_reference (decl);
31498 decl
31499 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31500 decl, false,
31501 &idk, loc);
31503 /* FALLTHROUGH. */
31504 case OMP_CLAUSE_DEPEND:
31505 case OMP_CLAUSE_REDUCTION:
31506 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31508 tree low_bound = NULL_TREE, length = NULL_TREE;
31510 parser->colon_corrects_to_scope_p = false;
31511 cp_lexer_consume_token (parser->lexer);
31512 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31513 low_bound = cp_parser_expression (parser);
31514 if (!colon)
31515 parser->colon_corrects_to_scope_p
31516 = saved_colon_corrects_to_scope_p;
31517 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31518 length = integer_one_node;
31519 else
31521 /* Look for `:'. */
31522 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31523 goto skip_comma;
31524 if (!cp_lexer_next_token_is (parser->lexer,
31525 CPP_CLOSE_SQUARE))
31526 length = cp_parser_expression (parser);
31528 /* Look for the closing `]'. */
31529 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31530 RT_CLOSE_SQUARE))
31531 goto skip_comma;
31533 decl = tree_cons (low_bound, length, decl);
31535 break;
31536 default:
31537 break;
31540 tree u = build_omp_clause (token->location, kind);
31541 OMP_CLAUSE_DECL (u) = decl;
31542 OMP_CLAUSE_CHAIN (u) = list;
31543 list = u;
31545 else
31546 list = tree_cons (decl, NULL_TREE, list);
31548 get_comma:
31549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31550 break;
31551 cp_lexer_consume_token (parser->lexer);
31554 if (colon)
31555 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31557 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31559 *colon = true;
31560 cp_parser_require (parser, CPP_COLON, RT_COLON);
31561 return list;
31564 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31566 int ending;
31568 /* Try to resync to an unnested comma. Copied from
31569 cp_parser_parenthesized_expression_list. */
31570 skip_comma:
31571 if (colon)
31572 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31573 ending = cp_parser_skip_to_closing_parenthesis (parser,
31574 /*recovering=*/true,
31575 /*or_comma=*/true,
31576 /*consume_paren=*/true);
31577 if (ending < 0)
31578 goto get_comma;
31581 return list;
31584 /* Similarly, but expect leading and trailing parenthesis. This is a very
31585 common case for omp clauses. */
31587 static tree
31588 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31590 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31591 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31592 return list;
31595 /* OpenACC 2.0:
31596 copy ( variable-list )
31597 copyin ( variable-list )
31598 copyout ( variable-list )
31599 create ( variable-list )
31600 delete ( variable-list )
31601 present ( variable-list )
31602 present_or_copy ( variable-list )
31603 pcopy ( variable-list )
31604 present_or_copyin ( variable-list )
31605 pcopyin ( variable-list )
31606 present_or_copyout ( variable-list )
31607 pcopyout ( variable-list )
31608 present_or_create ( variable-list )
31609 pcreate ( variable-list ) */
31611 static tree
31612 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31613 tree list)
31615 enum gomp_map_kind kind;
31616 switch (c_kind)
31618 case PRAGMA_OACC_CLAUSE_COPY:
31619 kind = GOMP_MAP_FORCE_TOFROM;
31620 break;
31621 case PRAGMA_OACC_CLAUSE_COPYIN:
31622 kind = GOMP_MAP_FORCE_TO;
31623 break;
31624 case PRAGMA_OACC_CLAUSE_COPYOUT:
31625 kind = GOMP_MAP_FORCE_FROM;
31626 break;
31627 case PRAGMA_OACC_CLAUSE_CREATE:
31628 kind = GOMP_MAP_FORCE_ALLOC;
31629 break;
31630 case PRAGMA_OACC_CLAUSE_DELETE:
31631 kind = GOMP_MAP_DELETE;
31632 break;
31633 case PRAGMA_OACC_CLAUSE_DEVICE:
31634 kind = GOMP_MAP_FORCE_TO;
31635 break;
31636 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31637 kind = GOMP_MAP_DEVICE_RESIDENT;
31638 break;
31639 case PRAGMA_OACC_CLAUSE_HOST:
31640 case PRAGMA_OACC_CLAUSE_SELF:
31641 kind = GOMP_MAP_FORCE_FROM;
31642 break;
31643 case PRAGMA_OACC_CLAUSE_LINK:
31644 kind = GOMP_MAP_LINK;
31645 break;
31646 case PRAGMA_OACC_CLAUSE_PRESENT:
31647 kind = GOMP_MAP_FORCE_PRESENT;
31648 break;
31649 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31650 kind = GOMP_MAP_TOFROM;
31651 break;
31652 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31653 kind = GOMP_MAP_TO;
31654 break;
31655 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31656 kind = GOMP_MAP_FROM;
31657 break;
31658 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31659 kind = GOMP_MAP_ALLOC;
31660 break;
31661 default:
31662 gcc_unreachable ();
31664 tree nl, c;
31665 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31667 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31668 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31670 return nl;
31673 /* OpenACC 2.0:
31674 deviceptr ( variable-list ) */
31676 static tree
31677 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31679 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31680 tree vars, t;
31682 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31683 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31684 variable-list must only allow for pointer variables. */
31685 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31686 for (t = vars; t; t = TREE_CHAIN (t))
31688 tree v = TREE_PURPOSE (t);
31689 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31690 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31691 OMP_CLAUSE_DECL (u) = v;
31692 OMP_CLAUSE_CHAIN (u) = list;
31693 list = u;
31696 return list;
31699 /* OpenACC 2.0:
31700 auto
31701 independent
31702 nohost
31703 seq */
31705 static tree
31706 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31707 enum omp_clause_code code,
31708 tree list, location_t location)
31710 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31711 tree c = build_omp_clause (location, code);
31712 OMP_CLAUSE_CHAIN (c) = list;
31713 return c;
31716 /* OpenACC:
31717 num_gangs ( expression )
31718 num_workers ( expression )
31719 vector_length ( expression ) */
31721 static tree
31722 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31723 const char *str, tree list)
31725 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31727 matching_parens parens;
31728 if (!parens.require_open (parser))
31729 return list;
31731 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31733 if (t == error_mark_node
31734 || !parens.require_close (parser))
31736 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31737 /*or_comma=*/false,
31738 /*consume_paren=*/true);
31739 return list;
31742 check_no_duplicate_clause (list, code, str, loc);
31744 tree c = build_omp_clause (loc, code);
31745 OMP_CLAUSE_OPERAND (c, 0) = t;
31746 OMP_CLAUSE_CHAIN (c) = list;
31747 return c;
31750 /* OpenACC:
31752 gang [( gang-arg-list )]
31753 worker [( [num:] int-expr )]
31754 vector [( [length:] int-expr )]
31756 where gang-arg is one of:
31758 [num:] int-expr
31759 static: size-expr
31761 and size-expr may be:
31764 int-expr
31767 static tree
31768 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31769 const char *str, tree list)
31771 const char *id = "num";
31772 cp_lexer *lexer = parser->lexer;
31773 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31774 location_t loc = cp_lexer_peek_token (lexer)->location;
31776 if (kind == OMP_CLAUSE_VECTOR)
31777 id = "length";
31779 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31781 matching_parens parens;
31782 parens.consume_open (parser);
31786 cp_token *next = cp_lexer_peek_token (lexer);
31787 int idx = 0;
31789 /* Gang static argument. */
31790 if (kind == OMP_CLAUSE_GANG
31791 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31793 cp_lexer_consume_token (lexer);
31795 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31796 goto cleanup_error;
31798 idx = 1;
31799 if (ops[idx] != NULL)
31801 cp_parser_error (parser, "too many %<static%> arguments");
31802 goto cleanup_error;
31805 /* Check for the '*' argument. */
31806 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31807 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31808 || cp_lexer_nth_token_is (parser->lexer, 2,
31809 CPP_CLOSE_PAREN)))
31811 cp_lexer_consume_token (lexer);
31812 ops[idx] = integer_minus_one_node;
31814 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31816 cp_lexer_consume_token (lexer);
31817 continue;
31819 else break;
31822 /* Worker num: argument and vector length: arguments. */
31823 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31824 && id_equal (next->u.value, id)
31825 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31827 cp_lexer_consume_token (lexer); /* id */
31828 cp_lexer_consume_token (lexer); /* ':' */
31831 /* Now collect the actual argument. */
31832 if (ops[idx] != NULL_TREE)
31834 cp_parser_error (parser, "unexpected argument");
31835 goto cleanup_error;
31838 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31839 false);
31840 if (expr == error_mark_node)
31841 goto cleanup_error;
31843 mark_exp_read (expr);
31844 ops[idx] = expr;
31846 if (kind == OMP_CLAUSE_GANG
31847 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31849 cp_lexer_consume_token (lexer);
31850 continue;
31852 break;
31854 while (1);
31856 if (!parens.require_close (parser))
31857 goto cleanup_error;
31860 check_no_duplicate_clause (list, kind, str, loc);
31862 c = build_omp_clause (loc, kind);
31864 if (ops[1])
31865 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31867 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31868 OMP_CLAUSE_CHAIN (c) = list;
31870 return c;
31872 cleanup_error:
31873 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31874 return list;
31877 /* OpenACC 2.0:
31878 tile ( size-expr-list ) */
31880 static tree
31881 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31883 tree c, expr = error_mark_node;
31884 tree tile = NULL_TREE;
31886 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31887 so, but the spec authors never considered such a case and have
31888 differing opinions on what it might mean, including 'not
31889 allowed'.) */
31890 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31891 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31892 clause_loc);
31894 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31895 return list;
31899 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31900 return list;
31902 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31903 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31904 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31906 cp_lexer_consume_token (parser->lexer);
31907 expr = integer_zero_node;
31909 else
31910 expr = cp_parser_constant_expression (parser);
31912 tile = tree_cons (NULL_TREE, expr, tile);
31914 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31916 /* Consume the trailing ')'. */
31917 cp_lexer_consume_token (parser->lexer);
31919 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31920 tile = nreverse (tile);
31921 OMP_CLAUSE_TILE_LIST (c) = tile;
31922 OMP_CLAUSE_CHAIN (c) = list;
31923 return c;
31926 /* OpenACC 2.0
31927 Parse wait clause or directive parameters. */
31929 static tree
31930 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31932 vec<tree, va_gc> *args;
31933 tree t, args_tree;
31935 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31936 /*cast_p=*/false,
31937 /*allow_expansion_p=*/true,
31938 /*non_constant_p=*/NULL);
31940 if (args == NULL || args->length () == 0)
31942 cp_parser_error (parser, "expected integer expression before ')'");
31943 if (args != NULL)
31944 release_tree_vector (args);
31945 return list;
31948 args_tree = build_tree_list_vec (args);
31950 release_tree_vector (args);
31952 for (t = args_tree; t; t = TREE_CHAIN (t))
31954 tree targ = TREE_VALUE (t);
31956 if (targ != error_mark_node)
31958 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31959 error ("%<wait%> expression must be integral");
31960 else
31962 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31964 targ = mark_rvalue_use (targ);
31965 OMP_CLAUSE_DECL (c) = targ;
31966 OMP_CLAUSE_CHAIN (c) = list;
31967 list = c;
31972 return list;
31975 /* OpenACC:
31976 wait ( int-expr-list ) */
31978 static tree
31979 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31981 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31983 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31984 return list;
31986 list = cp_parser_oacc_wait_list (parser, location, list);
31988 return list;
31991 /* OpenMP 3.0:
31992 collapse ( constant-expression ) */
31994 static tree
31995 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31997 tree c, num;
31998 location_t loc;
31999 HOST_WIDE_INT n;
32001 loc = cp_lexer_peek_token (parser->lexer)->location;
32002 matching_parens parens;
32003 if (!parens.require_open (parser))
32004 return list;
32006 num = cp_parser_constant_expression (parser);
32008 if (!parens.require_close (parser))
32009 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32010 /*or_comma=*/false,
32011 /*consume_paren=*/true);
32013 if (num == error_mark_node)
32014 return list;
32015 num = fold_non_dependent_expr (num);
32016 if (!tree_fits_shwi_p (num)
32017 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32018 || (n = tree_to_shwi (num)) <= 0
32019 || (int) n != n)
32021 error_at (loc, "collapse argument needs positive constant integer expression");
32022 return list;
32025 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32026 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32027 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32028 OMP_CLAUSE_CHAIN (c) = list;
32029 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32031 return c;
32034 /* OpenMP 2.5:
32035 default ( none | shared )
32037 OpenACC:
32038 default ( none | present ) */
32040 static tree
32041 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32042 location_t location, bool is_oacc)
32044 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32045 tree c;
32047 matching_parens parens;
32048 if (!parens.require_open (parser))
32049 return list;
32050 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32052 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32053 const char *p = IDENTIFIER_POINTER (id);
32055 switch (p[0])
32057 case 'n':
32058 if (strcmp ("none", p) != 0)
32059 goto invalid_kind;
32060 kind = OMP_CLAUSE_DEFAULT_NONE;
32061 break;
32063 case 'p':
32064 if (strcmp ("present", p) != 0 || !is_oacc)
32065 goto invalid_kind;
32066 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32067 break;
32069 case 's':
32070 if (strcmp ("shared", p) != 0 || is_oacc)
32071 goto invalid_kind;
32072 kind = OMP_CLAUSE_DEFAULT_SHARED;
32073 break;
32075 default:
32076 goto invalid_kind;
32079 cp_lexer_consume_token (parser->lexer);
32081 else
32083 invalid_kind:
32084 if (is_oacc)
32085 cp_parser_error (parser, "expected %<none%> or %<present%>");
32086 else
32087 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32090 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32091 || !parens.require_close (parser))
32092 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32093 /*or_comma=*/false,
32094 /*consume_paren=*/true);
32096 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32097 return list;
32099 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32100 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32101 OMP_CLAUSE_CHAIN (c) = list;
32102 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32104 return c;
32107 /* OpenMP 3.1:
32108 final ( expression ) */
32110 static tree
32111 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32113 tree t, c;
32115 matching_parens parens;
32116 if (!parens.require_open (parser))
32117 return list;
32119 t = cp_parser_condition (parser);
32121 if (t == error_mark_node
32122 || !parens.require_close (parser))
32123 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32124 /*or_comma=*/false,
32125 /*consume_paren=*/true);
32127 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32129 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32130 OMP_CLAUSE_FINAL_EXPR (c) = t;
32131 OMP_CLAUSE_CHAIN (c) = list;
32133 return c;
32136 /* OpenMP 2.5:
32137 if ( expression )
32139 OpenMP 4.5:
32140 if ( directive-name-modifier : expression )
32142 directive-name-modifier:
32143 parallel | task | taskloop | target data | target | target update
32144 | target enter data | target exit data */
32146 static tree
32147 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32148 bool is_omp)
32150 tree t, c;
32151 enum tree_code if_modifier = ERROR_MARK;
32153 matching_parens parens;
32154 if (!parens.require_open (parser))
32155 return list;
32157 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32159 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32160 const char *p = IDENTIFIER_POINTER (id);
32161 int n = 2;
32163 if (strcmp ("parallel", p) == 0)
32164 if_modifier = OMP_PARALLEL;
32165 else if (strcmp ("task", p) == 0)
32166 if_modifier = OMP_TASK;
32167 else if (strcmp ("taskloop", p) == 0)
32168 if_modifier = OMP_TASKLOOP;
32169 else if (strcmp ("target", p) == 0)
32171 if_modifier = OMP_TARGET;
32172 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32174 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32175 p = IDENTIFIER_POINTER (id);
32176 if (strcmp ("data", p) == 0)
32177 if_modifier = OMP_TARGET_DATA;
32178 else if (strcmp ("update", p) == 0)
32179 if_modifier = OMP_TARGET_UPDATE;
32180 else if (strcmp ("enter", p) == 0)
32181 if_modifier = OMP_TARGET_ENTER_DATA;
32182 else if (strcmp ("exit", p) == 0)
32183 if_modifier = OMP_TARGET_EXIT_DATA;
32184 if (if_modifier != OMP_TARGET)
32185 n = 3;
32186 else
32188 location_t loc
32189 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32190 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32191 "or %<exit%>");
32192 if_modifier = ERROR_MARK;
32194 if (if_modifier == OMP_TARGET_ENTER_DATA
32195 || if_modifier == OMP_TARGET_EXIT_DATA)
32197 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32199 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32200 p = IDENTIFIER_POINTER (id);
32201 if (strcmp ("data", p) == 0)
32202 n = 4;
32204 if (n != 4)
32206 location_t loc
32207 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32208 error_at (loc, "expected %<data%>");
32209 if_modifier = ERROR_MARK;
32214 if (if_modifier != ERROR_MARK)
32216 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32218 while (n-- > 0)
32219 cp_lexer_consume_token (parser->lexer);
32221 else
32223 if (n > 2)
32225 location_t loc
32226 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32227 error_at (loc, "expected %<:%>");
32229 if_modifier = ERROR_MARK;
32234 t = cp_parser_condition (parser);
32236 if (t == error_mark_node
32237 || !parens.require_close (parser))
32238 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32239 /*or_comma=*/false,
32240 /*consume_paren=*/true);
32242 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32243 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32245 if (if_modifier != ERROR_MARK
32246 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32248 const char *p = NULL;
32249 switch (if_modifier)
32251 case OMP_PARALLEL: p = "parallel"; break;
32252 case OMP_TASK: p = "task"; break;
32253 case OMP_TASKLOOP: p = "taskloop"; break;
32254 case OMP_TARGET_DATA: p = "target data"; break;
32255 case OMP_TARGET: p = "target"; break;
32256 case OMP_TARGET_UPDATE: p = "target update"; break;
32257 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32258 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32259 default: gcc_unreachable ();
32261 error_at (location, "too many %<if%> clauses with %qs modifier",
32263 return list;
32265 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32267 if (!is_omp)
32268 error_at (location, "too many %<if%> clauses");
32269 else
32270 error_at (location, "too many %<if%> clauses without modifier");
32271 return list;
32273 else if (if_modifier == ERROR_MARK
32274 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32276 error_at (location, "if any %<if%> clause has modifier, then all "
32277 "%<if%> clauses have to use modifier");
32278 return list;
32282 c = build_omp_clause (location, OMP_CLAUSE_IF);
32283 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32284 OMP_CLAUSE_IF_EXPR (c) = t;
32285 OMP_CLAUSE_CHAIN (c) = list;
32287 return c;
32290 /* OpenMP 3.1:
32291 mergeable */
32293 static tree
32294 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32295 tree list, location_t location)
32297 tree c;
32299 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32300 location);
32302 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32303 OMP_CLAUSE_CHAIN (c) = list;
32304 return c;
32307 /* OpenMP 2.5:
32308 nowait */
32310 static tree
32311 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32312 tree list, location_t location)
32314 tree c;
32316 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32318 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32319 OMP_CLAUSE_CHAIN (c) = list;
32320 return c;
32323 /* OpenMP 2.5:
32324 num_threads ( expression ) */
32326 static tree
32327 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32328 location_t location)
32330 tree t, c;
32332 matching_parens parens;
32333 if (!parens.require_open (parser))
32334 return list;
32336 t = cp_parser_expression (parser);
32338 if (t == error_mark_node
32339 || !parens.require_close (parser))
32340 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32341 /*or_comma=*/false,
32342 /*consume_paren=*/true);
32344 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32345 "num_threads", location);
32347 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32348 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32349 OMP_CLAUSE_CHAIN (c) = list;
32351 return c;
32354 /* OpenMP 4.5:
32355 num_tasks ( expression ) */
32357 static tree
32358 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32359 location_t location)
32361 tree t, c;
32363 matching_parens parens;
32364 if (!parens.require_open (parser))
32365 return list;
32367 t = cp_parser_expression (parser);
32369 if (t == error_mark_node
32370 || !parens.require_close (parser))
32371 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32372 /*or_comma=*/false,
32373 /*consume_paren=*/true);
32375 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32376 "num_tasks", location);
32378 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32379 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32380 OMP_CLAUSE_CHAIN (c) = list;
32382 return c;
32385 /* OpenMP 4.5:
32386 grainsize ( expression ) */
32388 static tree
32389 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32390 location_t location)
32392 tree t, c;
32394 matching_parens parens;
32395 if (!parens.require_open (parser))
32396 return list;
32398 t = cp_parser_expression (parser);
32400 if (t == error_mark_node
32401 || !parens.require_close (parser))
32402 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32403 /*or_comma=*/false,
32404 /*consume_paren=*/true);
32406 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32407 "grainsize", location);
32409 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32410 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32411 OMP_CLAUSE_CHAIN (c) = list;
32413 return c;
32416 /* OpenMP 4.5:
32417 priority ( expression ) */
32419 static tree
32420 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32421 location_t location)
32423 tree t, c;
32425 matching_parens parens;
32426 if (!parens.require_open (parser))
32427 return list;
32429 t = cp_parser_expression (parser);
32431 if (t == error_mark_node
32432 || !parens.require_close (parser))
32433 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32434 /*or_comma=*/false,
32435 /*consume_paren=*/true);
32437 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32438 "priority", location);
32440 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32441 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32442 OMP_CLAUSE_CHAIN (c) = list;
32444 return c;
32447 /* OpenMP 4.5:
32448 hint ( expression ) */
32450 static tree
32451 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32452 location_t location)
32454 tree t, c;
32456 matching_parens parens;
32457 if (!parens.require_open (parser))
32458 return list;
32460 t = cp_parser_expression (parser);
32462 if (t == error_mark_node
32463 || !parens.require_close (parser))
32464 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32465 /*or_comma=*/false,
32466 /*consume_paren=*/true);
32468 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32470 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32471 OMP_CLAUSE_HINT_EXPR (c) = t;
32472 OMP_CLAUSE_CHAIN (c) = list;
32474 return c;
32477 /* OpenMP 4.5:
32478 defaultmap ( tofrom : scalar ) */
32480 static tree
32481 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32482 location_t location)
32484 tree c, id;
32485 const char *p;
32487 matching_parens parens;
32488 if (!parens.require_open (parser))
32489 return list;
32491 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32493 cp_parser_error (parser, "expected %<tofrom%>");
32494 goto out_err;
32496 id = cp_lexer_peek_token (parser->lexer)->u.value;
32497 p = IDENTIFIER_POINTER (id);
32498 if (strcmp (p, "tofrom") != 0)
32500 cp_parser_error (parser, "expected %<tofrom%>");
32501 goto out_err;
32503 cp_lexer_consume_token (parser->lexer);
32504 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32505 goto out_err;
32507 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32509 cp_parser_error (parser, "expected %<scalar%>");
32510 goto out_err;
32512 id = cp_lexer_peek_token (parser->lexer)->u.value;
32513 p = IDENTIFIER_POINTER (id);
32514 if (strcmp (p, "scalar") != 0)
32516 cp_parser_error (parser, "expected %<scalar%>");
32517 goto out_err;
32519 cp_lexer_consume_token (parser->lexer);
32520 if (!parens.require_close (parser))
32521 goto out_err;
32523 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32524 location);
32526 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32527 OMP_CLAUSE_CHAIN (c) = list;
32528 return c;
32530 out_err:
32531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32532 /*or_comma=*/false,
32533 /*consume_paren=*/true);
32534 return list;
32537 /* OpenMP 2.5:
32538 ordered
32540 OpenMP 4.5:
32541 ordered ( constant-expression ) */
32543 static tree
32544 cp_parser_omp_clause_ordered (cp_parser *parser,
32545 tree list, location_t location)
32547 tree c, num = NULL_TREE;
32548 HOST_WIDE_INT n;
32550 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32551 "ordered", location);
32553 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32555 matching_parens parens;
32556 parens.consume_open (parser);
32558 num = cp_parser_constant_expression (parser);
32560 if (!parens.require_close (parser))
32561 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32562 /*or_comma=*/false,
32563 /*consume_paren=*/true);
32565 if (num == error_mark_node)
32566 return list;
32567 num = fold_non_dependent_expr (num);
32568 if (!tree_fits_shwi_p (num)
32569 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32570 || (n = tree_to_shwi (num)) <= 0
32571 || (int) n != n)
32573 error_at (location,
32574 "ordered argument needs positive constant integer "
32575 "expression");
32576 return list;
32580 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32581 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32582 OMP_CLAUSE_CHAIN (c) = list;
32583 return c;
32586 /* OpenMP 2.5:
32587 reduction ( reduction-operator : variable-list )
32589 reduction-operator:
32590 One of: + * - & ^ | && ||
32592 OpenMP 3.1:
32594 reduction-operator:
32595 One of: + * - & ^ | && || min max
32597 OpenMP 4.0:
32599 reduction-operator:
32600 One of: + * - & ^ | && ||
32601 id-expression */
32603 static tree
32604 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32606 enum tree_code code = ERROR_MARK;
32607 tree nlist, c, id = NULL_TREE;
32609 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32610 return list;
32612 switch (cp_lexer_peek_token (parser->lexer)->type)
32614 case CPP_PLUS: code = PLUS_EXPR; break;
32615 case CPP_MULT: code = MULT_EXPR; break;
32616 case CPP_MINUS: code = MINUS_EXPR; break;
32617 case CPP_AND: code = BIT_AND_EXPR; break;
32618 case CPP_XOR: code = BIT_XOR_EXPR; break;
32619 case CPP_OR: code = BIT_IOR_EXPR; break;
32620 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32621 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32622 default: break;
32625 if (code != ERROR_MARK)
32626 cp_lexer_consume_token (parser->lexer);
32627 else
32629 bool saved_colon_corrects_to_scope_p;
32630 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32631 parser->colon_corrects_to_scope_p = false;
32632 id = cp_parser_id_expression (parser, /*template_p=*/false,
32633 /*check_dependency_p=*/true,
32634 /*template_p=*/NULL,
32635 /*declarator_p=*/false,
32636 /*optional_p=*/false);
32637 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32638 if (identifier_p (id))
32640 const char *p = IDENTIFIER_POINTER (id);
32642 if (strcmp (p, "min") == 0)
32643 code = MIN_EXPR;
32644 else if (strcmp (p, "max") == 0)
32645 code = MAX_EXPR;
32646 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32647 code = PLUS_EXPR;
32648 else if (id == ovl_op_identifier (false, MULT_EXPR))
32649 code = MULT_EXPR;
32650 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32651 code = MINUS_EXPR;
32652 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32653 code = BIT_AND_EXPR;
32654 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32655 code = BIT_IOR_EXPR;
32656 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32657 code = BIT_XOR_EXPR;
32658 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32659 code = TRUTH_ANDIF_EXPR;
32660 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32661 code = TRUTH_ORIF_EXPR;
32662 id = omp_reduction_id (code, id, NULL_TREE);
32663 tree scope = parser->scope;
32664 if (scope)
32665 id = build_qualified_name (NULL_TREE, scope, id, false);
32666 parser->scope = NULL_TREE;
32667 parser->qualifying_scope = NULL_TREE;
32668 parser->object_scope = NULL_TREE;
32670 else
32672 error ("invalid reduction-identifier");
32673 resync_fail:
32674 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32675 /*or_comma=*/false,
32676 /*consume_paren=*/true);
32677 return list;
32681 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32682 goto resync_fail;
32684 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32685 NULL);
32686 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32688 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32689 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32692 return nlist;
32695 /* OpenMP 2.5:
32696 schedule ( schedule-kind )
32697 schedule ( schedule-kind , expression )
32699 schedule-kind:
32700 static | dynamic | guided | runtime | auto
32702 OpenMP 4.5:
32703 schedule ( schedule-modifier : schedule-kind )
32704 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32706 schedule-modifier:
32707 simd
32708 monotonic
32709 nonmonotonic */
32711 static tree
32712 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32714 tree c, t;
32715 int modifiers = 0, nmodifiers = 0;
32717 matching_parens parens;
32718 if (!parens.require_open (parser))
32719 return list;
32721 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32723 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32725 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32726 const char *p = IDENTIFIER_POINTER (id);
32727 if (strcmp ("simd", p) == 0)
32728 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32729 else if (strcmp ("monotonic", p) == 0)
32730 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32731 else if (strcmp ("nonmonotonic", p) == 0)
32732 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32733 else
32734 break;
32735 cp_lexer_consume_token (parser->lexer);
32736 if (nmodifiers++ == 0
32737 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32738 cp_lexer_consume_token (parser->lexer);
32739 else
32741 cp_parser_require (parser, CPP_COLON, RT_COLON);
32742 break;
32746 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32748 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32749 const char *p = IDENTIFIER_POINTER (id);
32751 switch (p[0])
32753 case 'd':
32754 if (strcmp ("dynamic", p) != 0)
32755 goto invalid_kind;
32756 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32757 break;
32759 case 'g':
32760 if (strcmp ("guided", p) != 0)
32761 goto invalid_kind;
32762 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32763 break;
32765 case 'r':
32766 if (strcmp ("runtime", p) != 0)
32767 goto invalid_kind;
32768 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32769 break;
32771 default:
32772 goto invalid_kind;
32775 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32776 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32777 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32778 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32779 else
32780 goto invalid_kind;
32781 cp_lexer_consume_token (parser->lexer);
32783 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32784 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32785 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32786 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32788 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32789 "specified");
32790 modifiers = 0;
32793 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32795 cp_token *token;
32796 cp_lexer_consume_token (parser->lexer);
32798 token = cp_lexer_peek_token (parser->lexer);
32799 t = cp_parser_assignment_expression (parser);
32801 if (t == error_mark_node)
32802 goto resync_fail;
32803 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32804 error_at (token->location, "schedule %<runtime%> does not take "
32805 "a %<chunk_size%> parameter");
32806 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32807 error_at (token->location, "schedule %<auto%> does not take "
32808 "a %<chunk_size%> parameter");
32809 else
32810 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32812 if (!parens.require_close (parser))
32813 goto resync_fail;
32815 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32816 goto resync_fail;
32818 OMP_CLAUSE_SCHEDULE_KIND (c)
32819 = (enum omp_clause_schedule_kind)
32820 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32822 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32823 OMP_CLAUSE_CHAIN (c) = list;
32824 return c;
32826 invalid_kind:
32827 cp_parser_error (parser, "invalid schedule kind");
32828 resync_fail:
32829 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32830 /*or_comma=*/false,
32831 /*consume_paren=*/true);
32832 return list;
32835 /* OpenMP 3.0:
32836 untied */
32838 static tree
32839 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32840 tree list, location_t location)
32842 tree c;
32844 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32846 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32847 OMP_CLAUSE_CHAIN (c) = list;
32848 return c;
32851 /* OpenMP 4.0:
32852 inbranch
32853 notinbranch */
32855 static tree
32856 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32857 tree list, location_t location)
32859 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32860 tree c = build_omp_clause (location, code);
32861 OMP_CLAUSE_CHAIN (c) = list;
32862 return c;
32865 /* OpenMP 4.0:
32866 parallel
32868 sections
32869 taskgroup */
32871 static tree
32872 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32873 enum omp_clause_code code,
32874 tree list, location_t location)
32876 tree c = build_omp_clause (location, code);
32877 OMP_CLAUSE_CHAIN (c) = list;
32878 return c;
32881 /* OpenMP 4.5:
32882 nogroup */
32884 static tree
32885 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32886 tree list, location_t location)
32888 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32889 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32890 OMP_CLAUSE_CHAIN (c) = list;
32891 return c;
32894 /* OpenMP 4.5:
32895 simd
32896 threads */
32898 static tree
32899 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32900 enum omp_clause_code code,
32901 tree list, location_t location)
32903 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32904 tree c = build_omp_clause (location, code);
32905 OMP_CLAUSE_CHAIN (c) = list;
32906 return c;
32909 /* OpenMP 4.0:
32910 num_teams ( expression ) */
32912 static tree
32913 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32914 location_t location)
32916 tree t, c;
32918 matching_parens parens;
32919 if (!parens.require_open (parser))
32920 return list;
32922 t = cp_parser_expression (parser);
32924 if (t == error_mark_node
32925 || !parens.require_close (parser))
32926 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32927 /*or_comma=*/false,
32928 /*consume_paren=*/true);
32930 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32931 "num_teams", location);
32933 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32934 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32935 OMP_CLAUSE_CHAIN (c) = list;
32937 return c;
32940 /* OpenMP 4.0:
32941 thread_limit ( expression ) */
32943 static tree
32944 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32945 location_t location)
32947 tree t, c;
32949 matching_parens parens;
32950 if (!parens.require_open (parser))
32951 return list;
32953 t = cp_parser_expression (parser);
32955 if (t == error_mark_node
32956 || !parens.require_close (parser))
32957 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32958 /*or_comma=*/false,
32959 /*consume_paren=*/true);
32961 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32962 "thread_limit", location);
32964 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32965 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32966 OMP_CLAUSE_CHAIN (c) = list;
32968 return c;
32971 /* OpenMP 4.0:
32972 aligned ( variable-list )
32973 aligned ( variable-list : constant-expression ) */
32975 static tree
32976 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32978 tree nlist, c, alignment = NULL_TREE;
32979 bool colon;
32981 matching_parens parens;
32982 if (!parens.require_open (parser))
32983 return list;
32985 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32986 &colon);
32988 if (colon)
32990 alignment = cp_parser_constant_expression (parser);
32992 if (!parens.require_close (parser))
32993 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32994 /*or_comma=*/false,
32995 /*consume_paren=*/true);
32997 if (alignment == error_mark_node)
32998 alignment = NULL_TREE;
33001 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33002 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33004 return nlist;
33007 /* OpenMP 4.0:
33008 linear ( variable-list )
33009 linear ( variable-list : expression )
33011 OpenMP 4.5:
33012 linear ( modifier ( variable-list ) )
33013 linear ( modifier ( variable-list ) : expression ) */
33015 static tree
33016 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33017 bool declare_simd)
33019 tree nlist, c, step = integer_one_node;
33020 bool colon;
33021 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33023 matching_parens parens;
33024 if (!parens.require_open (parser))
33025 return list;
33027 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33029 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33030 const char *p = IDENTIFIER_POINTER (id);
33032 if (strcmp ("ref", p) == 0)
33033 kind = OMP_CLAUSE_LINEAR_REF;
33034 else if (strcmp ("val", p) == 0)
33035 kind = OMP_CLAUSE_LINEAR_VAL;
33036 else if (strcmp ("uval", p) == 0)
33037 kind = OMP_CLAUSE_LINEAR_UVAL;
33038 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33039 cp_lexer_consume_token (parser->lexer);
33040 else
33041 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33044 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33045 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33046 &colon);
33047 else
33049 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33050 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33051 if (colon)
33052 cp_parser_require (parser, CPP_COLON, RT_COLON);
33053 else if (!parens.require_close (parser))
33054 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33055 /*or_comma=*/false,
33056 /*consume_paren=*/true);
33059 if (colon)
33061 step = NULL_TREE;
33062 if (declare_simd
33063 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33064 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33066 cp_token *token = cp_lexer_peek_token (parser->lexer);
33067 cp_parser_parse_tentatively (parser);
33068 step = cp_parser_id_expression (parser, /*template_p=*/false,
33069 /*check_dependency_p=*/true,
33070 /*template_p=*/NULL,
33071 /*declarator_p=*/false,
33072 /*optional_p=*/false);
33073 if (step != error_mark_node)
33074 step = cp_parser_lookup_name_simple (parser, step, token->location);
33075 if (step == error_mark_node)
33077 step = NULL_TREE;
33078 cp_parser_abort_tentative_parse (parser);
33080 else if (!cp_parser_parse_definitely (parser))
33081 step = NULL_TREE;
33083 if (!step)
33084 step = cp_parser_expression (parser);
33086 if (!parens.require_close (parser))
33087 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33088 /*or_comma=*/false,
33089 /*consume_paren=*/true);
33091 if (step == error_mark_node)
33092 return list;
33095 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33097 OMP_CLAUSE_LINEAR_STEP (c) = step;
33098 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33101 return nlist;
33104 /* OpenMP 4.0:
33105 safelen ( constant-expression ) */
33107 static tree
33108 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33109 location_t location)
33111 tree t, c;
33113 matching_parens parens;
33114 if (!parens.require_open (parser))
33115 return list;
33117 t = cp_parser_constant_expression (parser);
33119 if (t == error_mark_node
33120 || !parens.require_close (parser))
33121 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33122 /*or_comma=*/false,
33123 /*consume_paren=*/true);
33125 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33127 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33128 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33129 OMP_CLAUSE_CHAIN (c) = list;
33131 return c;
33134 /* OpenMP 4.0:
33135 simdlen ( constant-expression ) */
33137 static tree
33138 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33139 location_t location)
33141 tree t, c;
33143 matching_parens parens;
33144 if (!parens.require_open (parser))
33145 return list;
33147 t = cp_parser_constant_expression (parser);
33149 if (t == error_mark_node
33150 || !parens.require_close (parser))
33151 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33152 /*or_comma=*/false,
33153 /*consume_paren=*/true);
33155 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33157 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33158 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33159 OMP_CLAUSE_CHAIN (c) = list;
33161 return c;
33164 /* OpenMP 4.5:
33165 vec:
33166 identifier [+/- integer]
33167 vec , identifier [+/- integer]
33170 static tree
33171 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33172 tree list)
33174 tree vec = NULL;
33176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33178 cp_parser_error (parser, "expected identifier");
33179 return list;
33182 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33184 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33185 tree t, identifier = cp_parser_identifier (parser);
33186 tree addend = NULL;
33188 if (identifier == error_mark_node)
33189 t = error_mark_node;
33190 else
33192 t = cp_parser_lookup_name_simple
33193 (parser, identifier,
33194 cp_lexer_peek_token (parser->lexer)->location);
33195 if (t == error_mark_node)
33196 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33197 id_loc);
33200 bool neg = false;
33201 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33202 neg = true;
33203 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33205 addend = integer_zero_node;
33206 goto add_to_vector;
33208 cp_lexer_consume_token (parser->lexer);
33210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33212 cp_parser_error (parser, "expected integer");
33213 return list;
33216 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33217 if (TREE_CODE (addend) != INTEGER_CST)
33219 cp_parser_error (parser, "expected integer");
33220 return list;
33222 cp_lexer_consume_token (parser->lexer);
33224 add_to_vector:
33225 if (t != error_mark_node)
33227 vec = tree_cons (addend, t, vec);
33228 if (neg)
33229 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33233 break;
33235 cp_lexer_consume_token (parser->lexer);
33238 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33240 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33241 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33242 OMP_CLAUSE_DECL (u) = nreverse (vec);
33243 OMP_CLAUSE_CHAIN (u) = list;
33244 return u;
33246 return list;
33249 /* OpenMP 4.0:
33250 depend ( depend-kind : variable-list )
33252 depend-kind:
33253 in | out | inout
33255 OpenMP 4.5:
33256 depend ( source )
33258 depend ( sink : vec ) */
33260 static tree
33261 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33263 tree nlist, c;
33264 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33266 matching_parens parens;
33267 if (!parens.require_open (parser))
33268 return list;
33270 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33272 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33273 const char *p = IDENTIFIER_POINTER (id);
33275 if (strcmp ("in", p) == 0)
33276 kind = OMP_CLAUSE_DEPEND_IN;
33277 else if (strcmp ("inout", p) == 0)
33278 kind = OMP_CLAUSE_DEPEND_INOUT;
33279 else if (strcmp ("out", p) == 0)
33280 kind = OMP_CLAUSE_DEPEND_OUT;
33281 else if (strcmp ("source", p) == 0)
33282 kind = OMP_CLAUSE_DEPEND_SOURCE;
33283 else if (strcmp ("sink", p) == 0)
33284 kind = OMP_CLAUSE_DEPEND_SINK;
33285 else
33286 goto invalid_kind;
33288 else
33289 goto invalid_kind;
33291 cp_lexer_consume_token (parser->lexer);
33293 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33295 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33296 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33297 OMP_CLAUSE_DECL (c) = NULL_TREE;
33298 OMP_CLAUSE_CHAIN (c) = list;
33299 if (!parens.require_close (parser))
33300 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33301 /*or_comma=*/false,
33302 /*consume_paren=*/true);
33303 return c;
33306 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33307 goto resync_fail;
33309 if (kind == OMP_CLAUSE_DEPEND_SINK)
33310 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33311 else
33313 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33314 list, NULL);
33316 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33317 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33319 return nlist;
33321 invalid_kind:
33322 cp_parser_error (parser, "invalid depend kind");
33323 resync_fail:
33324 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33325 /*or_comma=*/false,
33326 /*consume_paren=*/true);
33327 return list;
33330 /* OpenMP 4.0:
33331 map ( map-kind : variable-list )
33332 map ( variable-list )
33334 map-kind:
33335 alloc | to | from | tofrom
33337 OpenMP 4.5:
33338 map-kind:
33339 alloc | to | from | tofrom | release | delete
33341 map ( always [,] map-kind: variable-list ) */
33343 static tree
33344 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33346 tree nlist, c;
33347 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33348 bool always = false;
33350 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33351 return list;
33353 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33355 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33356 const char *p = IDENTIFIER_POINTER (id);
33358 if (strcmp ("always", p) == 0)
33360 int nth = 2;
33361 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33362 nth++;
33363 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33364 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33365 == RID_DELETE))
33366 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33367 == CPP_COLON))
33369 always = true;
33370 cp_lexer_consume_token (parser->lexer);
33371 if (nth == 3)
33372 cp_lexer_consume_token (parser->lexer);
33377 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33378 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33380 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33381 const char *p = IDENTIFIER_POINTER (id);
33383 if (strcmp ("alloc", p) == 0)
33384 kind = GOMP_MAP_ALLOC;
33385 else if (strcmp ("to", p) == 0)
33386 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33387 else if (strcmp ("from", p) == 0)
33388 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33389 else if (strcmp ("tofrom", p) == 0)
33390 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33391 else if (strcmp ("release", p) == 0)
33392 kind = GOMP_MAP_RELEASE;
33393 else
33395 cp_parser_error (parser, "invalid map kind");
33396 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33397 /*or_comma=*/false,
33398 /*consume_paren=*/true);
33399 return list;
33401 cp_lexer_consume_token (parser->lexer);
33402 cp_lexer_consume_token (parser->lexer);
33404 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33405 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33407 kind = GOMP_MAP_DELETE;
33408 cp_lexer_consume_token (parser->lexer);
33409 cp_lexer_consume_token (parser->lexer);
33412 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33413 NULL);
33415 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33416 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33418 return nlist;
33421 /* OpenMP 4.0:
33422 device ( expression ) */
33424 static tree
33425 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33426 location_t location)
33428 tree t, c;
33430 matching_parens parens;
33431 if (!parens.require_open (parser))
33432 return list;
33434 t = cp_parser_expression (parser);
33436 if (t == error_mark_node
33437 || !parens.require_close (parser))
33438 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33439 /*or_comma=*/false,
33440 /*consume_paren=*/true);
33442 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33443 "device", location);
33445 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33446 OMP_CLAUSE_DEVICE_ID (c) = t;
33447 OMP_CLAUSE_CHAIN (c) = list;
33449 return c;
33452 /* OpenMP 4.0:
33453 dist_schedule ( static )
33454 dist_schedule ( static , expression ) */
33456 static tree
33457 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33458 location_t location)
33460 tree c, t;
33462 matching_parens parens;
33463 if (!parens.require_open (parser))
33464 return list;
33466 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33468 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33469 goto invalid_kind;
33470 cp_lexer_consume_token (parser->lexer);
33472 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33474 cp_lexer_consume_token (parser->lexer);
33476 t = cp_parser_assignment_expression (parser);
33478 if (t == error_mark_node)
33479 goto resync_fail;
33480 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33482 if (!parens.require_close (parser))
33483 goto resync_fail;
33485 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33486 goto resync_fail;
33488 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33489 location);
33490 OMP_CLAUSE_CHAIN (c) = list;
33491 return c;
33493 invalid_kind:
33494 cp_parser_error (parser, "invalid dist_schedule kind");
33495 resync_fail:
33496 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33497 /*or_comma=*/false,
33498 /*consume_paren=*/true);
33499 return list;
33502 /* OpenMP 4.0:
33503 proc_bind ( proc-bind-kind )
33505 proc-bind-kind:
33506 master | close | spread */
33508 static tree
33509 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33510 location_t location)
33512 tree c;
33513 enum omp_clause_proc_bind_kind kind;
33515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33516 return list;
33518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33520 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33521 const char *p = IDENTIFIER_POINTER (id);
33523 if (strcmp ("master", p) == 0)
33524 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33525 else if (strcmp ("close", p) == 0)
33526 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33527 else if (strcmp ("spread", p) == 0)
33528 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33529 else
33530 goto invalid_kind;
33532 else
33533 goto invalid_kind;
33535 cp_lexer_consume_token (parser->lexer);
33536 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33537 goto resync_fail;
33539 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33540 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33541 location);
33542 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33543 OMP_CLAUSE_CHAIN (c) = list;
33544 return c;
33546 invalid_kind:
33547 cp_parser_error (parser, "invalid depend kind");
33548 resync_fail:
33549 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33550 /*or_comma=*/false,
33551 /*consume_paren=*/true);
33552 return list;
33555 /* OpenACC:
33556 async [( int-expr )] */
33558 static tree
33559 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33561 tree c, t;
33562 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33564 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33566 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33568 matching_parens parens;
33569 parens.consume_open (parser);
33571 t = cp_parser_expression (parser);
33572 if (t == error_mark_node
33573 || !parens.require_close (parser))
33574 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33575 /*or_comma=*/false,
33576 /*consume_paren=*/true);
33579 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33581 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33582 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33583 OMP_CLAUSE_CHAIN (c) = list;
33584 list = c;
33586 return list;
33589 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33590 is a bitmask in MASK. Return the list of clauses found. */
33592 static tree
33593 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33594 const char *where, cp_token *pragma_tok,
33595 bool finish_p = true)
33597 tree clauses = NULL;
33598 bool first = true;
33600 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33602 location_t here;
33603 pragma_omp_clause c_kind;
33604 omp_clause_code code;
33605 const char *c_name;
33606 tree prev = clauses;
33608 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33609 cp_lexer_consume_token (parser->lexer);
33611 here = cp_lexer_peek_token (parser->lexer)->location;
33612 c_kind = cp_parser_omp_clause_name (parser);
33614 switch (c_kind)
33616 case PRAGMA_OACC_CLAUSE_ASYNC:
33617 clauses = cp_parser_oacc_clause_async (parser, clauses);
33618 c_name = "async";
33619 break;
33620 case PRAGMA_OACC_CLAUSE_AUTO:
33621 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33622 clauses, here);
33623 c_name = "auto";
33624 break;
33625 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33626 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33627 c_name = "collapse";
33628 break;
33629 case PRAGMA_OACC_CLAUSE_COPY:
33630 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33631 c_name = "copy";
33632 break;
33633 case PRAGMA_OACC_CLAUSE_COPYIN:
33634 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33635 c_name = "copyin";
33636 break;
33637 case PRAGMA_OACC_CLAUSE_COPYOUT:
33638 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33639 c_name = "copyout";
33640 break;
33641 case PRAGMA_OACC_CLAUSE_CREATE:
33642 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33643 c_name = "create";
33644 break;
33645 case PRAGMA_OACC_CLAUSE_DELETE:
33646 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33647 c_name = "delete";
33648 break;
33649 case PRAGMA_OMP_CLAUSE_DEFAULT:
33650 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33651 c_name = "default";
33652 break;
33653 case PRAGMA_OACC_CLAUSE_DEVICE:
33654 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33655 c_name = "device";
33656 break;
33657 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33658 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33659 c_name = "deviceptr";
33660 break;
33661 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33662 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33663 c_name = "device_resident";
33664 break;
33665 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33666 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33667 clauses);
33668 c_name = "firstprivate";
33669 break;
33670 case PRAGMA_OACC_CLAUSE_GANG:
33671 c_name = "gang";
33672 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33673 c_name, clauses);
33674 break;
33675 case PRAGMA_OACC_CLAUSE_HOST:
33676 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33677 c_name = "host";
33678 break;
33679 case PRAGMA_OACC_CLAUSE_IF:
33680 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33681 c_name = "if";
33682 break;
33683 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33684 clauses = cp_parser_oacc_simple_clause (parser,
33685 OMP_CLAUSE_INDEPENDENT,
33686 clauses, here);
33687 c_name = "independent";
33688 break;
33689 case PRAGMA_OACC_CLAUSE_LINK:
33690 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33691 c_name = "link";
33692 break;
33693 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33694 code = OMP_CLAUSE_NUM_GANGS;
33695 c_name = "num_gangs";
33696 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33697 clauses);
33698 break;
33699 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33700 c_name = "num_workers";
33701 code = OMP_CLAUSE_NUM_WORKERS;
33702 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33703 clauses);
33704 break;
33705 case PRAGMA_OACC_CLAUSE_PRESENT:
33706 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33707 c_name = "present";
33708 break;
33709 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33710 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33711 c_name = "present_or_copy";
33712 break;
33713 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33714 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33715 c_name = "present_or_copyin";
33716 break;
33717 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33718 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33719 c_name = "present_or_copyout";
33720 break;
33721 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33722 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33723 c_name = "present_or_create";
33724 break;
33725 case PRAGMA_OACC_CLAUSE_PRIVATE:
33726 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33727 clauses);
33728 c_name = "private";
33729 break;
33730 case PRAGMA_OACC_CLAUSE_REDUCTION:
33731 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33732 c_name = "reduction";
33733 break;
33734 case PRAGMA_OACC_CLAUSE_SELF:
33735 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33736 c_name = "self";
33737 break;
33738 case PRAGMA_OACC_CLAUSE_SEQ:
33739 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33740 clauses, here);
33741 c_name = "seq";
33742 break;
33743 case PRAGMA_OACC_CLAUSE_TILE:
33744 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33745 c_name = "tile";
33746 break;
33747 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33748 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33749 clauses);
33750 c_name = "use_device";
33751 break;
33752 case PRAGMA_OACC_CLAUSE_VECTOR:
33753 c_name = "vector";
33754 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33755 c_name, clauses);
33756 break;
33757 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33758 c_name = "vector_length";
33759 code = OMP_CLAUSE_VECTOR_LENGTH;
33760 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33761 clauses);
33762 break;
33763 case PRAGMA_OACC_CLAUSE_WAIT:
33764 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33765 c_name = "wait";
33766 break;
33767 case PRAGMA_OACC_CLAUSE_WORKER:
33768 c_name = "worker";
33769 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33770 c_name, clauses);
33771 break;
33772 default:
33773 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33774 goto saw_error;
33777 first = false;
33779 if (((mask >> c_kind) & 1) == 0)
33781 /* Remove the invalid clause(s) from the list to avoid
33782 confusing the rest of the compiler. */
33783 clauses = prev;
33784 error_at (here, "%qs is not valid for %qs", c_name, where);
33788 saw_error:
33789 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33791 if (finish_p)
33792 return finish_omp_clauses (clauses, C_ORT_ACC);
33794 return clauses;
33797 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33798 is a bitmask in MASK. Return the list of clauses found; the result
33799 of clause default goes in *pdefault. */
33801 static tree
33802 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33803 const char *where, cp_token *pragma_tok,
33804 bool finish_p = true)
33806 tree clauses = NULL;
33807 bool first = true;
33808 cp_token *token = NULL;
33810 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33812 pragma_omp_clause c_kind;
33813 const char *c_name;
33814 tree prev = clauses;
33816 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33817 cp_lexer_consume_token (parser->lexer);
33819 token = cp_lexer_peek_token (parser->lexer);
33820 c_kind = cp_parser_omp_clause_name (parser);
33822 switch (c_kind)
33824 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33825 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33826 token->location);
33827 c_name = "collapse";
33828 break;
33829 case PRAGMA_OMP_CLAUSE_COPYIN:
33830 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33831 c_name = "copyin";
33832 break;
33833 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33834 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33835 clauses);
33836 c_name = "copyprivate";
33837 break;
33838 case PRAGMA_OMP_CLAUSE_DEFAULT:
33839 clauses = cp_parser_omp_clause_default (parser, clauses,
33840 token->location, false);
33841 c_name = "default";
33842 break;
33843 case PRAGMA_OMP_CLAUSE_FINAL:
33844 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33845 c_name = "final";
33846 break;
33847 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33848 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33849 clauses);
33850 c_name = "firstprivate";
33851 break;
33852 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33853 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33854 token->location);
33855 c_name = "grainsize";
33856 break;
33857 case PRAGMA_OMP_CLAUSE_HINT:
33858 clauses = cp_parser_omp_clause_hint (parser, clauses,
33859 token->location);
33860 c_name = "hint";
33861 break;
33862 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33863 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33864 token->location);
33865 c_name = "defaultmap";
33866 break;
33867 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33868 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33869 clauses);
33870 c_name = "use_device_ptr";
33871 break;
33872 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33873 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33874 clauses);
33875 c_name = "is_device_ptr";
33876 break;
33877 case PRAGMA_OMP_CLAUSE_IF:
33878 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33879 true);
33880 c_name = "if";
33881 break;
33882 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33883 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33884 clauses);
33885 c_name = "lastprivate";
33886 break;
33887 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33888 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33889 token->location);
33890 c_name = "mergeable";
33891 break;
33892 case PRAGMA_OMP_CLAUSE_NOWAIT:
33893 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33894 c_name = "nowait";
33895 break;
33896 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33897 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33898 token->location);
33899 c_name = "num_tasks";
33900 break;
33901 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33902 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33903 token->location);
33904 c_name = "num_threads";
33905 break;
33906 case PRAGMA_OMP_CLAUSE_ORDERED:
33907 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33908 token->location);
33909 c_name = "ordered";
33910 break;
33911 case PRAGMA_OMP_CLAUSE_PRIORITY:
33912 clauses = cp_parser_omp_clause_priority (parser, clauses,
33913 token->location);
33914 c_name = "priority";
33915 break;
33916 case PRAGMA_OMP_CLAUSE_PRIVATE:
33917 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33918 clauses);
33919 c_name = "private";
33920 break;
33921 case PRAGMA_OMP_CLAUSE_REDUCTION:
33922 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33923 c_name = "reduction";
33924 break;
33925 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33926 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33927 token->location);
33928 c_name = "schedule";
33929 break;
33930 case PRAGMA_OMP_CLAUSE_SHARED:
33931 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33932 clauses);
33933 c_name = "shared";
33934 break;
33935 case PRAGMA_OMP_CLAUSE_UNTIED:
33936 clauses = cp_parser_omp_clause_untied (parser, clauses,
33937 token->location);
33938 c_name = "untied";
33939 break;
33940 case PRAGMA_OMP_CLAUSE_INBRANCH:
33941 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33942 clauses, token->location);
33943 c_name = "inbranch";
33944 break;
33945 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33946 clauses = cp_parser_omp_clause_branch (parser,
33947 OMP_CLAUSE_NOTINBRANCH,
33948 clauses, token->location);
33949 c_name = "notinbranch";
33950 break;
33951 case PRAGMA_OMP_CLAUSE_PARALLEL:
33952 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33953 clauses, token->location);
33954 c_name = "parallel";
33955 if (!first)
33957 clause_not_first:
33958 error_at (token->location, "%qs must be the first clause of %qs",
33959 c_name, where);
33960 clauses = prev;
33962 break;
33963 case PRAGMA_OMP_CLAUSE_FOR:
33964 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33965 clauses, token->location);
33966 c_name = "for";
33967 if (!first)
33968 goto clause_not_first;
33969 break;
33970 case PRAGMA_OMP_CLAUSE_SECTIONS:
33971 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33972 clauses, token->location);
33973 c_name = "sections";
33974 if (!first)
33975 goto clause_not_first;
33976 break;
33977 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33978 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33979 clauses, token->location);
33980 c_name = "taskgroup";
33981 if (!first)
33982 goto clause_not_first;
33983 break;
33984 case PRAGMA_OMP_CLAUSE_LINK:
33985 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33986 c_name = "to";
33987 break;
33988 case PRAGMA_OMP_CLAUSE_TO:
33989 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33990 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33991 clauses);
33992 else
33993 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33994 c_name = "to";
33995 break;
33996 case PRAGMA_OMP_CLAUSE_FROM:
33997 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33998 c_name = "from";
33999 break;
34000 case PRAGMA_OMP_CLAUSE_UNIFORM:
34001 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34002 clauses);
34003 c_name = "uniform";
34004 break;
34005 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34006 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34007 token->location);
34008 c_name = "num_teams";
34009 break;
34010 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34011 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34012 token->location);
34013 c_name = "thread_limit";
34014 break;
34015 case PRAGMA_OMP_CLAUSE_ALIGNED:
34016 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34017 c_name = "aligned";
34018 break;
34019 case PRAGMA_OMP_CLAUSE_LINEAR:
34021 bool declare_simd = false;
34022 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34023 declare_simd = true;
34024 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34026 c_name = "linear";
34027 break;
34028 case PRAGMA_OMP_CLAUSE_DEPEND:
34029 clauses = cp_parser_omp_clause_depend (parser, clauses,
34030 token->location);
34031 c_name = "depend";
34032 break;
34033 case PRAGMA_OMP_CLAUSE_MAP:
34034 clauses = cp_parser_omp_clause_map (parser, clauses);
34035 c_name = "map";
34036 break;
34037 case PRAGMA_OMP_CLAUSE_DEVICE:
34038 clauses = cp_parser_omp_clause_device (parser, clauses,
34039 token->location);
34040 c_name = "device";
34041 break;
34042 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34043 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34044 token->location);
34045 c_name = "dist_schedule";
34046 break;
34047 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34048 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34049 token->location);
34050 c_name = "proc_bind";
34051 break;
34052 case PRAGMA_OMP_CLAUSE_SAFELEN:
34053 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34054 token->location);
34055 c_name = "safelen";
34056 break;
34057 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34058 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34059 token->location);
34060 c_name = "simdlen";
34061 break;
34062 case PRAGMA_OMP_CLAUSE_NOGROUP:
34063 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34064 token->location);
34065 c_name = "nogroup";
34066 break;
34067 case PRAGMA_OMP_CLAUSE_THREADS:
34068 clauses
34069 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34070 clauses, token->location);
34071 c_name = "threads";
34072 break;
34073 case PRAGMA_OMP_CLAUSE_SIMD:
34074 clauses
34075 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34076 clauses, token->location);
34077 c_name = "simd";
34078 break;
34079 default:
34080 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34081 goto saw_error;
34084 first = false;
34086 if (((mask >> c_kind) & 1) == 0)
34088 /* Remove the invalid clause(s) from the list to avoid
34089 confusing the rest of the compiler. */
34090 clauses = prev;
34091 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34094 saw_error:
34095 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34096 if (finish_p)
34098 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34099 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34100 else
34101 return finish_omp_clauses (clauses, C_ORT_OMP);
34103 return clauses;
34106 /* OpenMP 2.5:
34107 structured-block:
34108 statement
34110 In practice, we're also interested in adding the statement to an
34111 outer node. So it is convenient if we work around the fact that
34112 cp_parser_statement calls add_stmt. */
34114 static unsigned
34115 cp_parser_begin_omp_structured_block (cp_parser *parser)
34117 unsigned save = parser->in_statement;
34119 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34120 This preserves the "not within loop or switch" style error messages
34121 for nonsense cases like
34122 void foo() {
34123 #pragma omp single
34124 break;
34127 if (parser->in_statement)
34128 parser->in_statement = IN_OMP_BLOCK;
34130 return save;
34133 static void
34134 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34136 parser->in_statement = save;
34139 static tree
34140 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34142 tree stmt = begin_omp_structured_block ();
34143 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34145 cp_parser_statement (parser, NULL_TREE, false, if_p);
34147 cp_parser_end_omp_structured_block (parser, save);
34148 return finish_omp_structured_block (stmt);
34151 /* OpenMP 2.5:
34152 # pragma omp atomic new-line
34153 expression-stmt
34155 expression-stmt:
34156 x binop= expr | x++ | ++x | x-- | --x
34157 binop:
34158 +, *, -, /, &, ^, |, <<, >>
34160 where x is an lvalue expression with scalar type.
34162 OpenMP 3.1:
34163 # pragma omp atomic new-line
34164 update-stmt
34166 # pragma omp atomic read new-line
34167 read-stmt
34169 # pragma omp atomic write new-line
34170 write-stmt
34172 # pragma omp atomic update new-line
34173 update-stmt
34175 # pragma omp atomic capture new-line
34176 capture-stmt
34178 # pragma omp atomic capture new-line
34179 capture-block
34181 read-stmt:
34182 v = x
34183 write-stmt:
34184 x = expr
34185 update-stmt:
34186 expression-stmt | x = x binop expr
34187 capture-stmt:
34188 v = expression-stmt
34189 capture-block:
34190 { v = x; update-stmt; } | { update-stmt; v = x; }
34192 OpenMP 4.0:
34193 update-stmt:
34194 expression-stmt | x = x binop expr | x = expr binop x
34195 capture-stmt:
34196 v = update-stmt
34197 capture-block:
34198 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34200 where x and v are lvalue expressions with scalar type. */
34202 static void
34203 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34205 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34206 tree rhs1 = NULL_TREE, orig_lhs;
34207 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34208 bool structured_block = false;
34209 bool seq_cst = false;
34211 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34213 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34214 const char *p = IDENTIFIER_POINTER (id);
34216 if (!strcmp (p, "seq_cst"))
34218 seq_cst = true;
34219 cp_lexer_consume_token (parser->lexer);
34220 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34221 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34222 cp_lexer_consume_token (parser->lexer);
34225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34228 const char *p = IDENTIFIER_POINTER (id);
34230 if (!strcmp (p, "read"))
34231 code = OMP_ATOMIC_READ;
34232 else if (!strcmp (p, "write"))
34233 code = NOP_EXPR;
34234 else if (!strcmp (p, "update"))
34235 code = OMP_ATOMIC;
34236 else if (!strcmp (p, "capture"))
34237 code = OMP_ATOMIC_CAPTURE_NEW;
34238 else
34239 p = NULL;
34240 if (p)
34241 cp_lexer_consume_token (parser->lexer);
34243 if (!seq_cst)
34245 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34246 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34247 cp_lexer_consume_token (parser->lexer);
34249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34252 const char *p = IDENTIFIER_POINTER (id);
34254 if (!strcmp (p, "seq_cst"))
34256 seq_cst = true;
34257 cp_lexer_consume_token (parser->lexer);
34261 cp_parser_require_pragma_eol (parser, pragma_tok);
34263 switch (code)
34265 case OMP_ATOMIC_READ:
34266 case NOP_EXPR: /* atomic write */
34267 v = cp_parser_unary_expression (parser);
34268 if (v == error_mark_node)
34269 goto saw_error;
34270 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34271 goto saw_error;
34272 if (code == NOP_EXPR)
34273 lhs = cp_parser_expression (parser);
34274 else
34275 lhs = cp_parser_unary_expression (parser);
34276 if (lhs == error_mark_node)
34277 goto saw_error;
34278 if (code == NOP_EXPR)
34280 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34281 opcode. */
34282 code = OMP_ATOMIC;
34283 rhs = lhs;
34284 lhs = v;
34285 v = NULL_TREE;
34287 goto done;
34288 case OMP_ATOMIC_CAPTURE_NEW:
34289 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34291 cp_lexer_consume_token (parser->lexer);
34292 structured_block = true;
34294 else
34296 v = cp_parser_unary_expression (parser);
34297 if (v == error_mark_node)
34298 goto saw_error;
34299 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34300 goto saw_error;
34302 default:
34303 break;
34306 restart:
34307 lhs = cp_parser_unary_expression (parser);
34308 orig_lhs = lhs;
34309 switch (TREE_CODE (lhs))
34311 case ERROR_MARK:
34312 goto saw_error;
34314 case POSTINCREMENT_EXPR:
34315 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34316 code = OMP_ATOMIC_CAPTURE_OLD;
34317 /* FALLTHROUGH */
34318 case PREINCREMENT_EXPR:
34319 lhs = TREE_OPERAND (lhs, 0);
34320 opcode = PLUS_EXPR;
34321 rhs = integer_one_node;
34322 break;
34324 case POSTDECREMENT_EXPR:
34325 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34326 code = OMP_ATOMIC_CAPTURE_OLD;
34327 /* FALLTHROUGH */
34328 case PREDECREMENT_EXPR:
34329 lhs = TREE_OPERAND (lhs, 0);
34330 opcode = MINUS_EXPR;
34331 rhs = integer_one_node;
34332 break;
34334 case COMPOUND_EXPR:
34335 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34336 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34337 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34338 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34339 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34340 (TREE_OPERAND (lhs, 1), 0), 0)))
34341 == BOOLEAN_TYPE)
34342 /* Undo effects of boolean_increment for post {in,de}crement. */
34343 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34344 /* FALLTHRU */
34345 case MODIFY_EXPR:
34346 if (TREE_CODE (lhs) == MODIFY_EXPR
34347 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34349 /* Undo effects of boolean_increment. */
34350 if (integer_onep (TREE_OPERAND (lhs, 1)))
34352 /* This is pre or post increment. */
34353 rhs = TREE_OPERAND (lhs, 1);
34354 lhs = TREE_OPERAND (lhs, 0);
34355 opcode = NOP_EXPR;
34356 if (code == OMP_ATOMIC_CAPTURE_NEW
34357 && !structured_block
34358 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34359 code = OMP_ATOMIC_CAPTURE_OLD;
34360 break;
34363 /* FALLTHRU */
34364 default:
34365 switch (cp_lexer_peek_token (parser->lexer)->type)
34367 case CPP_MULT_EQ:
34368 opcode = MULT_EXPR;
34369 break;
34370 case CPP_DIV_EQ:
34371 opcode = TRUNC_DIV_EXPR;
34372 break;
34373 case CPP_PLUS_EQ:
34374 opcode = PLUS_EXPR;
34375 break;
34376 case CPP_MINUS_EQ:
34377 opcode = MINUS_EXPR;
34378 break;
34379 case CPP_LSHIFT_EQ:
34380 opcode = LSHIFT_EXPR;
34381 break;
34382 case CPP_RSHIFT_EQ:
34383 opcode = RSHIFT_EXPR;
34384 break;
34385 case CPP_AND_EQ:
34386 opcode = BIT_AND_EXPR;
34387 break;
34388 case CPP_OR_EQ:
34389 opcode = BIT_IOR_EXPR;
34390 break;
34391 case CPP_XOR_EQ:
34392 opcode = BIT_XOR_EXPR;
34393 break;
34394 case CPP_EQ:
34395 enum cp_parser_prec oprec;
34396 cp_token *token;
34397 cp_lexer_consume_token (parser->lexer);
34398 cp_parser_parse_tentatively (parser);
34399 rhs1 = cp_parser_simple_cast_expression (parser);
34400 if (rhs1 == error_mark_node)
34402 cp_parser_abort_tentative_parse (parser);
34403 cp_parser_simple_cast_expression (parser);
34404 goto saw_error;
34406 token = cp_lexer_peek_token (parser->lexer);
34407 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34409 cp_parser_abort_tentative_parse (parser);
34410 cp_parser_parse_tentatively (parser);
34411 rhs = cp_parser_binary_expression (parser, false, true,
34412 PREC_NOT_OPERATOR, NULL);
34413 if (rhs == error_mark_node)
34415 cp_parser_abort_tentative_parse (parser);
34416 cp_parser_binary_expression (parser, false, true,
34417 PREC_NOT_OPERATOR, NULL);
34418 goto saw_error;
34420 switch (TREE_CODE (rhs))
34422 case MULT_EXPR:
34423 case TRUNC_DIV_EXPR:
34424 case RDIV_EXPR:
34425 case PLUS_EXPR:
34426 case MINUS_EXPR:
34427 case LSHIFT_EXPR:
34428 case RSHIFT_EXPR:
34429 case BIT_AND_EXPR:
34430 case BIT_IOR_EXPR:
34431 case BIT_XOR_EXPR:
34432 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34434 if (cp_parser_parse_definitely (parser))
34436 opcode = TREE_CODE (rhs);
34437 rhs1 = TREE_OPERAND (rhs, 0);
34438 rhs = TREE_OPERAND (rhs, 1);
34439 goto stmt_done;
34441 else
34442 goto saw_error;
34444 break;
34445 default:
34446 break;
34448 cp_parser_abort_tentative_parse (parser);
34449 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34451 rhs = cp_parser_expression (parser);
34452 if (rhs == error_mark_node)
34453 goto saw_error;
34454 opcode = NOP_EXPR;
34455 rhs1 = NULL_TREE;
34456 goto stmt_done;
34458 cp_parser_error (parser,
34459 "invalid form of %<#pragma omp atomic%>");
34460 goto saw_error;
34462 if (!cp_parser_parse_definitely (parser))
34463 goto saw_error;
34464 switch (token->type)
34466 case CPP_SEMICOLON:
34467 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34469 code = OMP_ATOMIC_CAPTURE_OLD;
34470 v = lhs;
34471 lhs = NULL_TREE;
34472 lhs1 = rhs1;
34473 rhs1 = NULL_TREE;
34474 cp_lexer_consume_token (parser->lexer);
34475 goto restart;
34477 else if (structured_block)
34479 opcode = NOP_EXPR;
34480 rhs = rhs1;
34481 rhs1 = NULL_TREE;
34482 goto stmt_done;
34484 cp_parser_error (parser,
34485 "invalid form of %<#pragma omp atomic%>");
34486 goto saw_error;
34487 case CPP_MULT:
34488 opcode = MULT_EXPR;
34489 break;
34490 case CPP_DIV:
34491 opcode = TRUNC_DIV_EXPR;
34492 break;
34493 case CPP_PLUS:
34494 opcode = PLUS_EXPR;
34495 break;
34496 case CPP_MINUS:
34497 opcode = MINUS_EXPR;
34498 break;
34499 case CPP_LSHIFT:
34500 opcode = LSHIFT_EXPR;
34501 break;
34502 case CPP_RSHIFT:
34503 opcode = RSHIFT_EXPR;
34504 break;
34505 case CPP_AND:
34506 opcode = BIT_AND_EXPR;
34507 break;
34508 case CPP_OR:
34509 opcode = BIT_IOR_EXPR;
34510 break;
34511 case CPP_XOR:
34512 opcode = BIT_XOR_EXPR;
34513 break;
34514 default:
34515 cp_parser_error (parser,
34516 "invalid operator for %<#pragma omp atomic%>");
34517 goto saw_error;
34519 oprec = TOKEN_PRECEDENCE (token);
34520 gcc_assert (oprec != PREC_NOT_OPERATOR);
34521 if (commutative_tree_code (opcode))
34522 oprec = (enum cp_parser_prec) (oprec - 1);
34523 cp_lexer_consume_token (parser->lexer);
34524 rhs = cp_parser_binary_expression (parser, false, false,
34525 oprec, NULL);
34526 if (rhs == error_mark_node)
34527 goto saw_error;
34528 goto stmt_done;
34529 /* FALLTHROUGH */
34530 default:
34531 cp_parser_error (parser,
34532 "invalid operator for %<#pragma omp atomic%>");
34533 goto saw_error;
34535 cp_lexer_consume_token (parser->lexer);
34537 rhs = cp_parser_expression (parser);
34538 if (rhs == error_mark_node)
34539 goto saw_error;
34540 break;
34542 stmt_done:
34543 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34545 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34546 goto saw_error;
34547 v = cp_parser_unary_expression (parser);
34548 if (v == error_mark_node)
34549 goto saw_error;
34550 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34551 goto saw_error;
34552 lhs1 = cp_parser_unary_expression (parser);
34553 if (lhs1 == error_mark_node)
34554 goto saw_error;
34556 if (structured_block)
34558 cp_parser_consume_semicolon_at_end_of_statement (parser);
34559 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34561 done:
34562 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34563 if (!structured_block)
34564 cp_parser_consume_semicolon_at_end_of_statement (parser);
34565 return;
34567 saw_error:
34568 cp_parser_skip_to_end_of_block_or_statement (parser);
34569 if (structured_block)
34571 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34572 cp_lexer_consume_token (parser->lexer);
34573 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34575 cp_parser_skip_to_end_of_block_or_statement (parser);
34576 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34577 cp_lexer_consume_token (parser->lexer);
34583 /* OpenMP 2.5:
34584 # pragma omp barrier new-line */
34586 static void
34587 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34589 cp_parser_require_pragma_eol (parser, pragma_tok);
34590 finish_omp_barrier ();
34593 /* OpenMP 2.5:
34594 # pragma omp critical [(name)] new-line
34595 structured-block
34597 OpenMP 4.5:
34598 # pragma omp critical [(name) [hint(expression)]] new-line
34599 structured-block */
34601 #define OMP_CRITICAL_CLAUSE_MASK \
34602 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34604 static tree
34605 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34607 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34609 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34611 matching_parens parens;
34612 parens.consume_open (parser);
34614 name = cp_parser_identifier (parser);
34616 if (name == error_mark_node
34617 || !parens.require_close (parser))
34618 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34619 /*or_comma=*/false,
34620 /*consume_paren=*/true);
34621 if (name == error_mark_node)
34622 name = NULL;
34624 clauses = cp_parser_omp_all_clauses (parser,
34625 OMP_CRITICAL_CLAUSE_MASK,
34626 "#pragma omp critical", pragma_tok);
34628 else
34629 cp_parser_require_pragma_eol (parser, pragma_tok);
34631 stmt = cp_parser_omp_structured_block (parser, if_p);
34632 return c_finish_omp_critical (input_location, stmt, name, clauses);
34635 /* OpenMP 2.5:
34636 # pragma omp flush flush-vars[opt] new-line
34638 flush-vars:
34639 ( variable-list ) */
34641 static void
34642 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34644 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34645 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34646 cp_parser_require_pragma_eol (parser, pragma_tok);
34648 finish_omp_flush ();
34651 /* Helper function, to parse omp for increment expression. */
34653 static tree
34654 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34656 tree cond = cp_parser_binary_expression (parser, false, true,
34657 PREC_NOT_OPERATOR, NULL);
34658 if (cond == error_mark_node
34659 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34661 cp_parser_skip_to_end_of_statement (parser);
34662 return error_mark_node;
34665 switch (TREE_CODE (cond))
34667 case GT_EXPR:
34668 case GE_EXPR:
34669 case LT_EXPR:
34670 case LE_EXPR:
34671 break;
34672 case NE_EXPR:
34673 /* Fall through: OpenMP disallows NE_EXPR. */
34674 gcc_fallthrough ();
34675 default:
34676 return error_mark_node;
34679 /* If decl is an iterator, preserve LHS and RHS of the relational
34680 expr until finish_omp_for. */
34681 if (decl
34682 && (type_dependent_expression_p (decl)
34683 || CLASS_TYPE_P (TREE_TYPE (decl))))
34684 return cond;
34686 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34687 TREE_CODE (cond),
34688 TREE_OPERAND (cond, 0), ERROR_MARK,
34689 TREE_OPERAND (cond, 1), ERROR_MARK,
34690 /*overload=*/NULL, tf_warning_or_error);
34693 /* Helper function, to parse omp for increment expression. */
34695 static tree
34696 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34698 cp_token *token = cp_lexer_peek_token (parser->lexer);
34699 enum tree_code op;
34700 tree lhs, rhs;
34701 cp_id_kind idk;
34702 bool decl_first;
34704 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34706 op = (token->type == CPP_PLUS_PLUS
34707 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34708 cp_lexer_consume_token (parser->lexer);
34709 lhs = cp_parser_simple_cast_expression (parser);
34710 if (lhs != decl
34711 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34712 return error_mark_node;
34713 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34716 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34717 if (lhs != decl
34718 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34719 return error_mark_node;
34721 token = cp_lexer_peek_token (parser->lexer);
34722 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34724 op = (token->type == CPP_PLUS_PLUS
34725 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34726 cp_lexer_consume_token (parser->lexer);
34727 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34730 op = cp_parser_assignment_operator_opt (parser);
34731 if (op == ERROR_MARK)
34732 return error_mark_node;
34734 if (op != NOP_EXPR)
34736 rhs = cp_parser_assignment_expression (parser);
34737 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34738 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34741 lhs = cp_parser_binary_expression (parser, false, false,
34742 PREC_ADDITIVE_EXPRESSION, NULL);
34743 token = cp_lexer_peek_token (parser->lexer);
34744 decl_first = (lhs == decl
34745 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34746 if (decl_first)
34747 lhs = NULL_TREE;
34748 if (token->type != CPP_PLUS
34749 && token->type != CPP_MINUS)
34750 return error_mark_node;
34754 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34755 cp_lexer_consume_token (parser->lexer);
34756 rhs = cp_parser_binary_expression (parser, false, false,
34757 PREC_ADDITIVE_EXPRESSION, NULL);
34758 token = cp_lexer_peek_token (parser->lexer);
34759 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34761 if (lhs == NULL_TREE)
34763 if (op == PLUS_EXPR)
34764 lhs = rhs;
34765 else
34766 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34767 tf_warning_or_error);
34769 else
34770 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34771 ERROR_MARK, NULL, tf_warning_or_error);
34774 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34776 if (!decl_first)
34778 if ((rhs != decl
34779 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34780 || op == MINUS_EXPR)
34781 return error_mark_node;
34782 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34784 else
34785 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34787 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34790 /* Parse the initialization statement of an OpenMP for loop.
34792 Return true if the resulting construct should have an
34793 OMP_CLAUSE_PRIVATE added to it. */
34795 static tree
34796 cp_parser_omp_for_loop_init (cp_parser *parser,
34797 tree &this_pre_body,
34798 vec<tree, va_gc> *for_block,
34799 tree &init,
34800 tree &orig_init,
34801 tree &decl,
34802 tree &real_decl)
34804 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34805 return NULL_TREE;
34807 tree add_private_clause = NULL_TREE;
34809 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34811 init-expr:
34812 var = lb
34813 integer-type var = lb
34814 random-access-iterator-type var = lb
34815 pointer-type var = lb
34817 cp_decl_specifier_seq type_specifiers;
34819 /* First, try to parse as an initialized declaration. See
34820 cp_parser_condition, from whence the bulk of this is copied. */
34822 cp_parser_parse_tentatively (parser);
34823 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34824 /*is_trailing_return=*/false,
34825 &type_specifiers);
34826 if (cp_parser_parse_definitely (parser))
34828 /* If parsing a type specifier seq succeeded, then this
34829 MUST be a initialized declaration. */
34830 tree asm_specification, attributes;
34831 cp_declarator *declarator;
34833 declarator = cp_parser_declarator (parser,
34834 CP_PARSER_DECLARATOR_NAMED,
34835 /*ctor_dtor_or_conv_p=*/NULL,
34836 /*parenthesized_p=*/NULL,
34837 /*member_p=*/false,
34838 /*friend_p=*/false);
34839 attributes = cp_parser_attributes_opt (parser);
34840 asm_specification = cp_parser_asm_specification_opt (parser);
34842 if (declarator == cp_error_declarator)
34843 cp_parser_skip_to_end_of_statement (parser);
34845 else
34847 tree pushed_scope, auto_node;
34849 decl = start_decl (declarator, &type_specifiers,
34850 SD_INITIALIZED, attributes,
34851 /*prefix_attributes=*/NULL_TREE,
34852 &pushed_scope);
34854 auto_node = type_uses_auto (TREE_TYPE (decl));
34855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34857 if (cp_lexer_next_token_is (parser->lexer,
34858 CPP_OPEN_PAREN))
34859 error ("parenthesized initialization is not allowed in "
34860 "OpenMP %<for%> loop");
34861 else
34862 /* Trigger an error. */
34863 cp_parser_require (parser, CPP_EQ, RT_EQ);
34865 init = error_mark_node;
34866 cp_parser_skip_to_end_of_statement (parser);
34868 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34869 || type_dependent_expression_p (decl)
34870 || auto_node)
34872 bool is_direct_init, is_non_constant_init;
34874 init = cp_parser_initializer (parser,
34875 &is_direct_init,
34876 &is_non_constant_init);
34878 if (auto_node)
34880 TREE_TYPE (decl)
34881 = do_auto_deduction (TREE_TYPE (decl), init,
34882 auto_node);
34884 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34885 && !type_dependent_expression_p (decl))
34886 goto non_class;
34889 cp_finish_decl (decl, init, !is_non_constant_init,
34890 asm_specification,
34891 LOOKUP_ONLYCONVERTING);
34892 orig_init = init;
34893 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34895 vec_safe_push (for_block, this_pre_body);
34896 init = NULL_TREE;
34898 else
34900 init = pop_stmt_list (this_pre_body);
34901 if (init && TREE_CODE (init) == STATEMENT_LIST)
34903 tree_stmt_iterator i = tsi_start (init);
34904 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34905 while (!tsi_end_p (i))
34907 tree t = tsi_stmt (i);
34908 if (TREE_CODE (t) == DECL_EXPR
34909 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34911 tsi_delink (&i);
34912 vec_safe_push (for_block, t);
34913 continue;
34915 break;
34917 if (tsi_one_before_end_p (i))
34919 tree t = tsi_stmt (i);
34920 tsi_delink (&i);
34921 free_stmt_list (init);
34922 init = t;
34926 this_pre_body = NULL_TREE;
34928 else
34930 /* Consume '='. */
34931 cp_lexer_consume_token (parser->lexer);
34932 init = cp_parser_assignment_expression (parser);
34934 non_class:
34935 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34936 init = error_mark_node;
34937 else
34938 cp_finish_decl (decl, NULL_TREE,
34939 /*init_const_expr_p=*/false,
34940 asm_specification,
34941 LOOKUP_ONLYCONVERTING);
34944 if (pushed_scope)
34945 pop_scope (pushed_scope);
34948 else
34950 cp_id_kind idk;
34951 /* If parsing a type specifier sequence failed, then
34952 this MUST be a simple expression. */
34953 cp_parser_parse_tentatively (parser);
34954 decl = cp_parser_primary_expression (parser, false, false,
34955 false, &idk);
34956 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34957 if (!cp_parser_error_occurred (parser)
34958 && decl
34959 && (TREE_CODE (decl) == COMPONENT_REF
34960 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34962 cp_parser_abort_tentative_parse (parser);
34963 cp_parser_parse_tentatively (parser);
34964 cp_token *token = cp_lexer_peek_token (parser->lexer);
34965 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34966 /*check_dependency_p=*/true,
34967 /*template_p=*/NULL,
34968 /*declarator_p=*/false,
34969 /*optional_p=*/false);
34970 if (name != error_mark_node
34971 && last_tok == cp_lexer_peek_token (parser->lexer))
34973 decl = cp_parser_lookup_name_simple (parser, name,
34974 token->location);
34975 if (TREE_CODE (decl) == FIELD_DECL)
34976 add_private_clause = omp_privatize_field (decl, false);
34978 cp_parser_abort_tentative_parse (parser);
34979 cp_parser_parse_tentatively (parser);
34980 decl = cp_parser_primary_expression (parser, false, false,
34981 false, &idk);
34983 if (!cp_parser_error_occurred (parser)
34984 && decl
34985 && DECL_P (decl)
34986 && CLASS_TYPE_P (TREE_TYPE (decl)))
34988 tree rhs;
34990 cp_parser_parse_definitely (parser);
34991 cp_parser_require (parser, CPP_EQ, RT_EQ);
34992 rhs = cp_parser_assignment_expression (parser);
34993 orig_init = rhs;
34994 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34995 decl, NOP_EXPR,
34996 rhs,
34997 tf_warning_or_error));
34998 if (!add_private_clause)
34999 add_private_clause = decl;
35001 else
35003 decl = NULL;
35004 cp_parser_abort_tentative_parse (parser);
35005 init = cp_parser_expression (parser);
35006 if (init)
35008 if (TREE_CODE (init) == MODIFY_EXPR
35009 || TREE_CODE (init) == MODOP_EXPR)
35010 real_decl = TREE_OPERAND (init, 0);
35014 return add_private_clause;
35017 /* Parse the restricted form of the for statement allowed by OpenMP. */
35019 static tree
35020 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35021 tree *cclauses, bool *if_p)
35023 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35024 tree real_decl, initv, condv, incrv, declv;
35025 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35026 location_t loc_first;
35027 bool collapse_err = false;
35028 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35029 vec<tree, va_gc> *for_block = make_tree_vector ();
35030 auto_vec<tree, 4> orig_inits;
35031 bool tiling = false;
35033 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35034 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35035 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35036 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35038 tiling = true;
35039 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35041 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35042 && OMP_CLAUSE_ORDERED_EXPR (cl))
35044 ordered_cl = cl;
35045 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35048 if (ordered && ordered < collapse)
35050 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35051 "%<ordered%> clause parameter is less than %<collapse%>");
35052 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35053 = build_int_cst (NULL_TREE, collapse);
35054 ordered = collapse;
35056 if (ordered)
35058 for (tree *pc = &clauses; *pc; )
35059 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35061 error_at (OMP_CLAUSE_LOCATION (*pc),
35062 "%<linear%> clause may not be specified together "
35063 "with %<ordered%> clause with a parameter");
35064 *pc = OMP_CLAUSE_CHAIN (*pc);
35066 else
35067 pc = &OMP_CLAUSE_CHAIN (*pc);
35070 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35071 count = ordered ? ordered : collapse;
35073 declv = make_tree_vec (count);
35074 initv = make_tree_vec (count);
35075 condv = make_tree_vec (count);
35076 incrv = make_tree_vec (count);
35078 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35080 for (i = 0; i < count; i++)
35082 int bracecount = 0;
35083 tree add_private_clause = NULL_TREE;
35084 location_t loc;
35086 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35088 if (!collapse_err)
35089 cp_parser_error (parser, "for statement expected");
35090 return NULL;
35092 loc = cp_lexer_consume_token (parser->lexer)->location;
35094 matching_parens parens;
35095 if (!parens.require_open (parser))
35096 return NULL;
35098 init = orig_init = decl = real_decl = NULL;
35099 this_pre_body = push_stmt_list ();
35101 add_private_clause
35102 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35103 init, orig_init, decl, real_decl);
35105 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35106 if (this_pre_body)
35108 this_pre_body = pop_stmt_list (this_pre_body);
35109 if (pre_body)
35111 tree t = pre_body;
35112 pre_body = push_stmt_list ();
35113 add_stmt (t);
35114 add_stmt (this_pre_body);
35115 pre_body = pop_stmt_list (pre_body);
35117 else
35118 pre_body = this_pre_body;
35121 if (decl)
35122 real_decl = decl;
35123 if (cclauses != NULL
35124 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35125 && real_decl != NULL_TREE)
35127 tree *c;
35128 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35129 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35130 && OMP_CLAUSE_DECL (*c) == real_decl)
35132 error_at (loc, "iteration variable %qD"
35133 " should not be firstprivate", real_decl);
35134 *c = OMP_CLAUSE_CHAIN (*c);
35136 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35137 && OMP_CLAUSE_DECL (*c) == real_decl)
35139 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35140 tree l = *c;
35141 *c = OMP_CLAUSE_CHAIN (*c);
35142 if (code == OMP_SIMD)
35144 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35145 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35147 else
35149 OMP_CLAUSE_CHAIN (l) = clauses;
35150 clauses = l;
35152 add_private_clause = NULL_TREE;
35154 else
35156 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35157 && OMP_CLAUSE_DECL (*c) == real_decl)
35158 add_private_clause = NULL_TREE;
35159 c = &OMP_CLAUSE_CHAIN (*c);
35163 if (add_private_clause)
35165 tree c;
35166 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35168 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35169 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35170 && OMP_CLAUSE_DECL (c) == decl)
35171 break;
35172 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35173 && OMP_CLAUSE_DECL (c) == decl)
35174 error_at (loc, "iteration variable %qD "
35175 "should not be firstprivate",
35176 decl);
35177 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35178 && OMP_CLAUSE_DECL (c) == decl)
35179 error_at (loc, "iteration variable %qD should not be reduction",
35180 decl);
35182 if (c == NULL)
35184 if (code != OMP_SIMD)
35185 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35186 else if (collapse == 1)
35187 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35188 else
35189 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35190 OMP_CLAUSE_DECL (c) = add_private_clause;
35191 c = finish_omp_clauses (c, C_ORT_OMP);
35192 if (c)
35194 OMP_CLAUSE_CHAIN (c) = clauses;
35195 clauses = c;
35196 /* For linear, signal that we need to fill up
35197 the so far unknown linear step. */
35198 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35199 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35204 cond = NULL;
35205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35206 cond = cp_parser_omp_for_cond (parser, decl);
35207 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35209 incr = NULL;
35210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35212 /* If decl is an iterator, preserve the operator on decl
35213 until finish_omp_for. */
35214 if (real_decl
35215 && ((processing_template_decl
35216 && (TREE_TYPE (real_decl) == NULL_TREE
35217 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35218 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35219 incr = cp_parser_omp_for_incr (parser, real_decl);
35220 else
35221 incr = cp_parser_expression (parser);
35222 if (!EXPR_HAS_LOCATION (incr))
35223 protected_set_expr_location (incr, input_location);
35226 if (!parens.require_close (parser))
35227 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35228 /*or_comma=*/false,
35229 /*consume_paren=*/true);
35231 TREE_VEC_ELT (declv, i) = decl;
35232 TREE_VEC_ELT (initv, i) = init;
35233 TREE_VEC_ELT (condv, i) = cond;
35234 TREE_VEC_ELT (incrv, i) = incr;
35235 if (orig_init)
35237 orig_inits.safe_grow_cleared (i + 1);
35238 orig_inits[i] = orig_init;
35241 if (i == count - 1)
35242 break;
35244 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35245 in between the collapsed for loops to be still considered perfectly
35246 nested. Hopefully the final version clarifies this.
35247 For now handle (multiple) {'s and empty statements. */
35248 cp_parser_parse_tentatively (parser);
35249 for (;;)
35251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35252 break;
35253 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35255 cp_lexer_consume_token (parser->lexer);
35256 bracecount++;
35258 else if (bracecount
35259 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35260 cp_lexer_consume_token (parser->lexer);
35261 else
35263 loc = cp_lexer_peek_token (parser->lexer)->location;
35264 error_at (loc, "not enough for loops to collapse");
35265 collapse_err = true;
35266 cp_parser_abort_tentative_parse (parser);
35267 declv = NULL_TREE;
35268 break;
35272 if (declv)
35274 cp_parser_parse_definitely (parser);
35275 nbraces += bracecount;
35279 if (nbraces)
35280 if_p = NULL;
35282 /* Note that we saved the original contents of this flag when we entered
35283 the structured block, and so we don't need to re-save it here. */
35284 parser->in_statement = IN_OMP_FOR;
35286 /* Note that the grammar doesn't call for a structured block here,
35287 though the loop as a whole is a structured block. */
35288 body = push_stmt_list ();
35289 cp_parser_statement (parser, NULL_TREE, false, if_p);
35290 body = pop_stmt_list (body);
35292 if (declv == NULL_TREE)
35293 ret = NULL_TREE;
35294 else
35295 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35296 body, pre_body, &orig_inits, clauses);
35298 while (nbraces)
35300 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35302 cp_lexer_consume_token (parser->lexer);
35303 nbraces--;
35305 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35306 cp_lexer_consume_token (parser->lexer);
35307 else
35309 if (!collapse_err)
35311 error_at (cp_lexer_peek_token (parser->lexer)->location,
35312 "collapsed loops not perfectly nested");
35314 collapse_err = true;
35315 cp_parser_statement_seq_opt (parser, NULL);
35316 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35317 break;
35321 while (!for_block->is_empty ())
35323 tree t = for_block->pop ();
35324 if (TREE_CODE (t) == STATEMENT_LIST)
35325 add_stmt (pop_stmt_list (t));
35326 else
35327 add_stmt (t);
35329 release_tree_vector (for_block);
35331 return ret;
35334 /* Helper function for OpenMP parsing, split clauses and call
35335 finish_omp_clauses on each of the set of clauses afterwards. */
35337 static void
35338 cp_omp_split_clauses (location_t loc, enum tree_code code,
35339 omp_clause_mask mask, tree clauses, tree *cclauses)
35341 int i;
35342 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35343 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35344 if (cclauses[i])
35345 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35348 /* OpenMP 4.0:
35349 #pragma omp simd simd-clause[optseq] new-line
35350 for-loop */
35352 #define OMP_SIMD_CLAUSE_MASK \
35353 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35362 static tree
35363 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35364 char *p_name, omp_clause_mask mask, tree *cclauses,
35365 bool *if_p)
35367 tree clauses, sb, ret;
35368 unsigned int save;
35369 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35371 strcat (p_name, " simd");
35372 mask |= OMP_SIMD_CLAUSE_MASK;
35374 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35375 cclauses == NULL);
35376 if (cclauses)
35378 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35379 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35380 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35381 OMP_CLAUSE_ORDERED);
35382 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35384 error_at (OMP_CLAUSE_LOCATION (c),
35385 "%<ordered%> clause with parameter may not be specified "
35386 "on %qs construct", p_name);
35387 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35391 sb = begin_omp_structured_block ();
35392 save = cp_parser_begin_omp_structured_block (parser);
35394 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35396 cp_parser_end_omp_structured_block (parser, save);
35397 add_stmt (finish_omp_structured_block (sb));
35399 return ret;
35402 /* OpenMP 2.5:
35403 #pragma omp for for-clause[optseq] new-line
35404 for-loop
35406 OpenMP 4.0:
35407 #pragma omp for simd for-simd-clause[optseq] new-line
35408 for-loop */
35410 #define OMP_FOR_CLAUSE_MASK \
35411 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35421 static tree
35422 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35423 char *p_name, omp_clause_mask mask, tree *cclauses,
35424 bool *if_p)
35426 tree clauses, sb, ret;
35427 unsigned int save;
35428 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35430 strcat (p_name, " for");
35431 mask |= OMP_FOR_CLAUSE_MASK;
35432 /* parallel for{, simd} disallows nowait clause, but for
35433 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35434 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35435 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35436 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35437 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35438 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35440 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35442 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35443 const char *p = IDENTIFIER_POINTER (id);
35445 if (strcmp (p, "simd") == 0)
35447 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35448 if (cclauses == NULL)
35449 cclauses = cclauses_buf;
35451 cp_lexer_consume_token (parser->lexer);
35452 if (!flag_openmp) /* flag_openmp_simd */
35453 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35454 cclauses, if_p);
35455 sb = begin_omp_structured_block ();
35456 save = cp_parser_begin_omp_structured_block (parser);
35457 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35458 cclauses, if_p);
35459 cp_parser_end_omp_structured_block (parser, save);
35460 tree body = finish_omp_structured_block (sb);
35461 if (ret == NULL)
35462 return ret;
35463 ret = make_node (OMP_FOR);
35464 TREE_TYPE (ret) = void_type_node;
35465 OMP_FOR_BODY (ret) = body;
35466 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35467 SET_EXPR_LOCATION (ret, loc);
35468 add_stmt (ret);
35469 return ret;
35472 if (!flag_openmp) /* flag_openmp_simd */
35474 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35475 return NULL_TREE;
35478 /* Composite distribute parallel for disallows linear clause. */
35479 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35480 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35482 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35483 cclauses == NULL);
35484 if (cclauses)
35486 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35487 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35490 sb = begin_omp_structured_block ();
35491 save = cp_parser_begin_omp_structured_block (parser);
35493 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35495 cp_parser_end_omp_structured_block (parser, save);
35496 add_stmt (finish_omp_structured_block (sb));
35498 return ret;
35501 /* OpenMP 2.5:
35502 # pragma omp master new-line
35503 structured-block */
35505 static tree
35506 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35508 cp_parser_require_pragma_eol (parser, pragma_tok);
35509 return c_finish_omp_master (input_location,
35510 cp_parser_omp_structured_block (parser, if_p));
35513 /* OpenMP 2.5:
35514 # pragma omp ordered new-line
35515 structured-block
35517 OpenMP 4.5:
35518 # pragma omp ordered ordered-clauses new-line
35519 structured-block */
35521 #define OMP_ORDERED_CLAUSE_MASK \
35522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35525 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35526 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35528 static bool
35529 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35530 enum pragma_context context, bool *if_p)
35532 location_t loc = pragma_tok->location;
35534 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35536 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35537 const char *p = IDENTIFIER_POINTER (id);
35539 if (strcmp (p, "depend") == 0)
35541 if (!flag_openmp) /* flag_openmp_simd */
35543 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35544 return false;
35546 if (context == pragma_stmt)
35548 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35549 "%<depend%> clause may only be used in compound "
35550 "statements");
35551 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35552 return false;
35554 tree clauses
35555 = cp_parser_omp_all_clauses (parser,
35556 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35557 "#pragma omp ordered", pragma_tok);
35558 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35559 return false;
35563 tree clauses
35564 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35565 "#pragma omp ordered", pragma_tok);
35567 if (!flag_openmp /* flag_openmp_simd */
35568 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35569 return false;
35571 c_finish_omp_ordered (loc, clauses,
35572 cp_parser_omp_structured_block (parser, if_p));
35573 return true;
35576 /* OpenMP 2.5:
35578 section-scope:
35579 { section-sequence }
35581 section-sequence:
35582 section-directive[opt] structured-block
35583 section-sequence section-directive structured-block */
35585 static tree
35586 cp_parser_omp_sections_scope (cp_parser *parser)
35588 tree stmt, substmt;
35589 bool error_suppress = false;
35590 cp_token *tok;
35592 matching_braces braces;
35593 if (!braces.require_open (parser))
35594 return NULL_TREE;
35596 stmt = push_stmt_list ();
35598 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35599 != PRAGMA_OMP_SECTION)
35601 substmt = cp_parser_omp_structured_block (parser, NULL);
35602 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35603 add_stmt (substmt);
35606 while (1)
35608 tok = cp_lexer_peek_token (parser->lexer);
35609 if (tok->type == CPP_CLOSE_BRACE)
35610 break;
35611 if (tok->type == CPP_EOF)
35612 break;
35614 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35616 cp_lexer_consume_token (parser->lexer);
35617 cp_parser_require_pragma_eol (parser, tok);
35618 error_suppress = false;
35620 else if (!error_suppress)
35622 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35623 error_suppress = true;
35626 substmt = cp_parser_omp_structured_block (parser, NULL);
35627 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35628 add_stmt (substmt);
35630 braces.require_close (parser);
35632 substmt = pop_stmt_list (stmt);
35634 stmt = make_node (OMP_SECTIONS);
35635 TREE_TYPE (stmt) = void_type_node;
35636 OMP_SECTIONS_BODY (stmt) = substmt;
35638 add_stmt (stmt);
35639 return stmt;
35642 /* OpenMP 2.5:
35643 # pragma omp sections sections-clause[optseq] newline
35644 sections-scope */
35646 #define OMP_SECTIONS_CLAUSE_MASK \
35647 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35653 static tree
35654 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35655 char *p_name, omp_clause_mask mask, tree *cclauses)
35657 tree clauses, ret;
35658 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35660 strcat (p_name, " sections");
35661 mask |= OMP_SECTIONS_CLAUSE_MASK;
35662 if (cclauses)
35663 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35665 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35666 cclauses == NULL);
35667 if (cclauses)
35669 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35670 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35673 ret = cp_parser_omp_sections_scope (parser);
35674 if (ret)
35675 OMP_SECTIONS_CLAUSES (ret) = clauses;
35677 return ret;
35680 /* OpenMP 2.5:
35681 # pragma omp parallel parallel-clause[optseq] new-line
35682 structured-block
35683 # pragma omp parallel for parallel-for-clause[optseq] new-line
35684 structured-block
35685 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35686 structured-block
35688 OpenMP 4.0:
35689 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35690 structured-block */
35692 #define OMP_PARALLEL_CLAUSE_MASK \
35693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35703 static tree
35704 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35705 char *p_name, omp_clause_mask mask, tree *cclauses,
35706 bool *if_p)
35708 tree stmt, clauses, block;
35709 unsigned int save;
35710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35712 strcat (p_name, " parallel");
35713 mask |= OMP_PARALLEL_CLAUSE_MASK;
35714 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35715 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35716 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35717 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35721 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35722 if (cclauses == NULL)
35723 cclauses = cclauses_buf;
35725 cp_lexer_consume_token (parser->lexer);
35726 if (!flag_openmp) /* flag_openmp_simd */
35727 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35728 if_p);
35729 block = begin_omp_parallel ();
35730 save = cp_parser_begin_omp_structured_block (parser);
35731 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35732 if_p);
35733 cp_parser_end_omp_structured_block (parser, save);
35734 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35735 block);
35736 if (ret == NULL_TREE)
35737 return ret;
35738 OMP_PARALLEL_COMBINED (stmt) = 1;
35739 return stmt;
35741 /* When combined with distribute, parallel has to be followed by for.
35742 #pragma omp target parallel is allowed though. */
35743 else if (cclauses
35744 && (mask & (OMP_CLAUSE_MASK_1
35745 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35747 error_at (loc, "expected %<for%> after %qs", p_name);
35748 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35749 return NULL_TREE;
35751 else if (!flag_openmp) /* flag_openmp_simd */
35753 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35754 return NULL_TREE;
35756 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35758 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35759 const char *p = IDENTIFIER_POINTER (id);
35760 if (strcmp (p, "sections") == 0)
35762 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35763 cclauses = cclauses_buf;
35765 cp_lexer_consume_token (parser->lexer);
35766 block = begin_omp_parallel ();
35767 save = cp_parser_begin_omp_structured_block (parser);
35768 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35769 cp_parser_end_omp_structured_block (parser, save);
35770 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35771 block);
35772 OMP_PARALLEL_COMBINED (stmt) = 1;
35773 return stmt;
35777 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35778 cclauses == NULL);
35779 if (cclauses)
35781 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35782 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35785 block = begin_omp_parallel ();
35786 save = cp_parser_begin_omp_structured_block (parser);
35787 cp_parser_statement (parser, NULL_TREE, false, if_p);
35788 cp_parser_end_omp_structured_block (parser, save);
35789 stmt = finish_omp_parallel (clauses, block);
35790 return stmt;
35793 /* OpenMP 2.5:
35794 # pragma omp single single-clause[optseq] new-line
35795 structured-block */
35797 #define OMP_SINGLE_CLAUSE_MASK \
35798 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35803 static tree
35804 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35806 tree stmt = make_node (OMP_SINGLE);
35807 TREE_TYPE (stmt) = void_type_node;
35809 OMP_SINGLE_CLAUSES (stmt)
35810 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35811 "#pragma omp single", pragma_tok);
35812 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35814 return add_stmt (stmt);
35817 /* OpenMP 3.0:
35818 # pragma omp task task-clause[optseq] new-line
35819 structured-block */
35821 #define OMP_TASK_CLAUSE_MASK \
35822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35833 static tree
35834 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35836 tree clauses, block;
35837 unsigned int save;
35839 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35840 "#pragma omp task", pragma_tok);
35841 block = begin_omp_task ();
35842 save = cp_parser_begin_omp_structured_block (parser);
35843 cp_parser_statement (parser, NULL_TREE, false, if_p);
35844 cp_parser_end_omp_structured_block (parser, save);
35845 return finish_omp_task (clauses, block);
35848 /* OpenMP 3.0:
35849 # pragma omp taskwait new-line */
35851 static void
35852 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35854 cp_parser_require_pragma_eol (parser, pragma_tok);
35855 finish_omp_taskwait ();
35858 /* OpenMP 3.1:
35859 # pragma omp taskyield new-line */
35861 static void
35862 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35864 cp_parser_require_pragma_eol (parser, pragma_tok);
35865 finish_omp_taskyield ();
35868 /* OpenMP 4.0:
35869 # pragma omp taskgroup new-line
35870 structured-block */
35872 static tree
35873 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35875 cp_parser_require_pragma_eol (parser, pragma_tok);
35876 return c_finish_omp_taskgroup (input_location,
35877 cp_parser_omp_structured_block (parser,
35878 if_p));
35882 /* OpenMP 2.5:
35883 # pragma omp threadprivate (variable-list) */
35885 static void
35886 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35888 tree vars;
35890 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35891 cp_parser_require_pragma_eol (parser, pragma_tok);
35893 finish_omp_threadprivate (vars);
35896 /* OpenMP 4.0:
35897 # pragma omp cancel cancel-clause[optseq] new-line */
35899 #define OMP_CANCEL_CLAUSE_MASK \
35900 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35906 static void
35907 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35909 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35910 "#pragma omp cancel", pragma_tok);
35911 finish_omp_cancel (clauses);
35914 /* OpenMP 4.0:
35915 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35917 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35918 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35923 static void
35924 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35925 enum pragma_context context)
35927 tree clauses;
35928 bool point_seen = false;
35930 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35932 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35933 const char *p = IDENTIFIER_POINTER (id);
35935 if (strcmp (p, "point") == 0)
35937 cp_lexer_consume_token (parser->lexer);
35938 point_seen = true;
35941 if (!point_seen)
35943 cp_parser_error (parser, "expected %<point%>");
35944 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35945 return;
35948 if (context != pragma_compound)
35950 if (context == pragma_stmt)
35951 error_at (pragma_tok->location,
35952 "%<#pragma %s%> may only be used in compound statements",
35953 "omp cancellation point");
35954 else
35955 cp_parser_error (parser, "expected declaration specifiers");
35956 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35957 return;
35960 clauses = cp_parser_omp_all_clauses (parser,
35961 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35962 "#pragma omp cancellation point",
35963 pragma_tok);
35964 finish_omp_cancellation_point (clauses);
35967 /* OpenMP 4.0:
35968 #pragma omp distribute distribute-clause[optseq] new-line
35969 for-loop */
35971 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35978 static tree
35979 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35980 char *p_name, omp_clause_mask mask, tree *cclauses,
35981 bool *if_p)
35983 tree clauses, sb, ret;
35984 unsigned int save;
35985 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35987 strcat (p_name, " distribute");
35988 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35992 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35993 const char *p = IDENTIFIER_POINTER (id);
35994 bool simd = false;
35995 bool parallel = false;
35997 if (strcmp (p, "simd") == 0)
35998 simd = true;
35999 else
36000 parallel = strcmp (p, "parallel") == 0;
36001 if (parallel || simd)
36003 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36004 if (cclauses == NULL)
36005 cclauses = cclauses_buf;
36006 cp_lexer_consume_token (parser->lexer);
36007 if (!flag_openmp) /* flag_openmp_simd */
36009 if (simd)
36010 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36011 cclauses, if_p);
36012 else
36013 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36014 cclauses, if_p);
36016 sb = begin_omp_structured_block ();
36017 save = cp_parser_begin_omp_structured_block (parser);
36018 if (simd)
36019 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36020 cclauses, if_p);
36021 else
36022 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36023 cclauses, if_p);
36024 cp_parser_end_omp_structured_block (parser, save);
36025 tree body = finish_omp_structured_block (sb);
36026 if (ret == NULL)
36027 return ret;
36028 ret = make_node (OMP_DISTRIBUTE);
36029 TREE_TYPE (ret) = void_type_node;
36030 OMP_FOR_BODY (ret) = body;
36031 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36032 SET_EXPR_LOCATION (ret, loc);
36033 add_stmt (ret);
36034 return ret;
36037 if (!flag_openmp) /* flag_openmp_simd */
36039 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36040 return NULL_TREE;
36043 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36044 cclauses == NULL);
36045 if (cclauses)
36047 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36048 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36051 sb = begin_omp_structured_block ();
36052 save = cp_parser_begin_omp_structured_block (parser);
36054 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36056 cp_parser_end_omp_structured_block (parser, save);
36057 add_stmt (finish_omp_structured_block (sb));
36059 return ret;
36062 /* OpenMP 4.0:
36063 # pragma omp teams teams-clause[optseq] new-line
36064 structured-block */
36066 #define OMP_TEAMS_CLAUSE_MASK \
36067 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36075 static tree
36076 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36077 char *p_name, omp_clause_mask mask, tree *cclauses,
36078 bool *if_p)
36080 tree clauses, sb, ret;
36081 unsigned int save;
36082 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36084 strcat (p_name, " teams");
36085 mask |= OMP_TEAMS_CLAUSE_MASK;
36087 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36089 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36090 const char *p = IDENTIFIER_POINTER (id);
36091 if (strcmp (p, "distribute") == 0)
36093 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36094 if (cclauses == NULL)
36095 cclauses = cclauses_buf;
36097 cp_lexer_consume_token (parser->lexer);
36098 if (!flag_openmp) /* flag_openmp_simd */
36099 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36100 cclauses, if_p);
36101 sb = begin_omp_structured_block ();
36102 save = cp_parser_begin_omp_structured_block (parser);
36103 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36104 cclauses, if_p);
36105 cp_parser_end_omp_structured_block (parser, save);
36106 tree body = finish_omp_structured_block (sb);
36107 if (ret == NULL)
36108 return ret;
36109 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36110 ret = make_node (OMP_TEAMS);
36111 TREE_TYPE (ret) = void_type_node;
36112 OMP_TEAMS_CLAUSES (ret) = clauses;
36113 OMP_TEAMS_BODY (ret) = body;
36114 OMP_TEAMS_COMBINED (ret) = 1;
36115 SET_EXPR_LOCATION (ret, loc);
36116 return add_stmt (ret);
36119 if (!flag_openmp) /* flag_openmp_simd */
36121 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36122 return NULL_TREE;
36125 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36126 cclauses == NULL);
36127 if (cclauses)
36129 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36130 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36133 tree stmt = make_node (OMP_TEAMS);
36134 TREE_TYPE (stmt) = void_type_node;
36135 OMP_TEAMS_CLAUSES (stmt) = clauses;
36136 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36137 SET_EXPR_LOCATION (stmt, loc);
36139 return add_stmt (stmt);
36142 /* OpenMP 4.0:
36143 # pragma omp target data target-data-clause[optseq] new-line
36144 structured-block */
36146 #define OMP_TARGET_DATA_CLAUSE_MASK \
36147 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36152 static tree
36153 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36155 tree clauses
36156 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36157 "#pragma omp target data", pragma_tok);
36158 int map_seen = 0;
36159 for (tree *pc = &clauses; *pc;)
36161 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36162 switch (OMP_CLAUSE_MAP_KIND (*pc))
36164 case GOMP_MAP_TO:
36165 case GOMP_MAP_ALWAYS_TO:
36166 case GOMP_MAP_FROM:
36167 case GOMP_MAP_ALWAYS_FROM:
36168 case GOMP_MAP_TOFROM:
36169 case GOMP_MAP_ALWAYS_TOFROM:
36170 case GOMP_MAP_ALLOC:
36171 map_seen = 3;
36172 break;
36173 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36174 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36175 case GOMP_MAP_ALWAYS_POINTER:
36176 break;
36177 default:
36178 map_seen |= 1;
36179 error_at (OMP_CLAUSE_LOCATION (*pc),
36180 "%<#pragma omp target data%> with map-type other "
36181 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36182 "on %<map%> clause");
36183 *pc = OMP_CLAUSE_CHAIN (*pc);
36184 continue;
36186 pc = &OMP_CLAUSE_CHAIN (*pc);
36189 if (map_seen != 3)
36191 if (map_seen == 0)
36192 error_at (pragma_tok->location,
36193 "%<#pragma omp target data%> must contain at least "
36194 "one %<map%> clause");
36195 return NULL_TREE;
36198 tree stmt = make_node (OMP_TARGET_DATA);
36199 TREE_TYPE (stmt) = void_type_node;
36200 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36202 keep_next_level (true);
36203 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36205 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36206 return add_stmt (stmt);
36209 /* OpenMP 4.5:
36210 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36211 structured-block */
36213 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36214 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36220 static tree
36221 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36222 enum pragma_context context)
36224 bool data_seen = false;
36225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36228 const char *p = IDENTIFIER_POINTER (id);
36230 if (strcmp (p, "data") == 0)
36232 cp_lexer_consume_token (parser->lexer);
36233 data_seen = true;
36236 if (!data_seen)
36238 cp_parser_error (parser, "expected %<data%>");
36239 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36240 return NULL_TREE;
36243 if (context == pragma_stmt)
36245 error_at (pragma_tok->location,
36246 "%<#pragma %s%> may only be used in compound statements",
36247 "omp target enter data");
36248 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36249 return NULL_TREE;
36252 tree clauses
36253 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36254 "#pragma omp target enter data", pragma_tok);
36255 int map_seen = 0;
36256 for (tree *pc = &clauses; *pc;)
36258 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36259 switch (OMP_CLAUSE_MAP_KIND (*pc))
36261 case GOMP_MAP_TO:
36262 case GOMP_MAP_ALWAYS_TO:
36263 case GOMP_MAP_ALLOC:
36264 map_seen = 3;
36265 break;
36266 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36267 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36268 case GOMP_MAP_ALWAYS_POINTER:
36269 break;
36270 default:
36271 map_seen |= 1;
36272 error_at (OMP_CLAUSE_LOCATION (*pc),
36273 "%<#pragma omp target enter data%> with map-type other "
36274 "than %<to%> or %<alloc%> on %<map%> clause");
36275 *pc = OMP_CLAUSE_CHAIN (*pc);
36276 continue;
36278 pc = &OMP_CLAUSE_CHAIN (*pc);
36281 if (map_seen != 3)
36283 if (map_seen == 0)
36284 error_at (pragma_tok->location,
36285 "%<#pragma omp target enter data%> must contain at least "
36286 "one %<map%> clause");
36287 return NULL_TREE;
36290 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36291 TREE_TYPE (stmt) = void_type_node;
36292 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36293 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36294 return add_stmt (stmt);
36297 /* OpenMP 4.5:
36298 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36299 structured-block */
36301 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36302 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36308 static tree
36309 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36310 enum pragma_context context)
36312 bool data_seen = false;
36313 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36315 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36316 const char *p = IDENTIFIER_POINTER (id);
36318 if (strcmp (p, "data") == 0)
36320 cp_lexer_consume_token (parser->lexer);
36321 data_seen = true;
36324 if (!data_seen)
36326 cp_parser_error (parser, "expected %<data%>");
36327 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36328 return NULL_TREE;
36331 if (context == pragma_stmt)
36333 error_at (pragma_tok->location,
36334 "%<#pragma %s%> may only be used in compound statements",
36335 "omp target exit data");
36336 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36337 return NULL_TREE;
36340 tree clauses
36341 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36342 "#pragma omp target exit data", pragma_tok);
36343 int map_seen = 0;
36344 for (tree *pc = &clauses; *pc;)
36346 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36347 switch (OMP_CLAUSE_MAP_KIND (*pc))
36349 case GOMP_MAP_FROM:
36350 case GOMP_MAP_ALWAYS_FROM:
36351 case GOMP_MAP_RELEASE:
36352 case GOMP_MAP_DELETE:
36353 map_seen = 3;
36354 break;
36355 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36356 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36357 case GOMP_MAP_ALWAYS_POINTER:
36358 break;
36359 default:
36360 map_seen |= 1;
36361 error_at (OMP_CLAUSE_LOCATION (*pc),
36362 "%<#pragma omp target exit data%> with map-type other "
36363 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36364 " clause");
36365 *pc = OMP_CLAUSE_CHAIN (*pc);
36366 continue;
36368 pc = &OMP_CLAUSE_CHAIN (*pc);
36371 if (map_seen != 3)
36373 if (map_seen == 0)
36374 error_at (pragma_tok->location,
36375 "%<#pragma omp target exit data%> must contain at least "
36376 "one %<map%> clause");
36377 return NULL_TREE;
36380 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36381 TREE_TYPE (stmt) = void_type_node;
36382 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36383 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36384 return add_stmt (stmt);
36387 /* OpenMP 4.0:
36388 # pragma omp target update target-update-clause[optseq] new-line */
36390 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36398 static bool
36399 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36400 enum pragma_context context)
36402 if (context == pragma_stmt)
36404 error_at (pragma_tok->location,
36405 "%<#pragma %s%> may only be used in compound statements",
36406 "omp target update");
36407 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36408 return false;
36411 tree clauses
36412 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36413 "#pragma omp target update", pragma_tok);
36414 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36415 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36417 error_at (pragma_tok->location,
36418 "%<#pragma omp target update%> must contain at least one "
36419 "%<from%> or %<to%> clauses");
36420 return false;
36423 tree stmt = make_node (OMP_TARGET_UPDATE);
36424 TREE_TYPE (stmt) = void_type_node;
36425 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36426 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36427 add_stmt (stmt);
36428 return false;
36431 /* OpenMP 4.0:
36432 # pragma omp target target-clause[optseq] new-line
36433 structured-block */
36435 #define OMP_TARGET_CLAUSE_MASK \
36436 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36446 static bool
36447 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36448 enum pragma_context context, bool *if_p)
36450 tree *pc = NULL, stmt;
36452 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36454 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36455 const char *p = IDENTIFIER_POINTER (id);
36456 enum tree_code ccode = ERROR_MARK;
36458 if (strcmp (p, "teams") == 0)
36459 ccode = OMP_TEAMS;
36460 else if (strcmp (p, "parallel") == 0)
36461 ccode = OMP_PARALLEL;
36462 else if (strcmp (p, "simd") == 0)
36463 ccode = OMP_SIMD;
36464 if (ccode != ERROR_MARK)
36466 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36467 char p_name[sizeof ("#pragma omp target teams distribute "
36468 "parallel for simd")];
36470 cp_lexer_consume_token (parser->lexer);
36471 strcpy (p_name, "#pragma omp target");
36472 if (!flag_openmp) /* flag_openmp_simd */
36474 tree stmt;
36475 switch (ccode)
36477 case OMP_TEAMS:
36478 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36479 OMP_TARGET_CLAUSE_MASK,
36480 cclauses, if_p);
36481 break;
36482 case OMP_PARALLEL:
36483 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36484 OMP_TARGET_CLAUSE_MASK,
36485 cclauses, if_p);
36486 break;
36487 case OMP_SIMD:
36488 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36489 OMP_TARGET_CLAUSE_MASK,
36490 cclauses, if_p);
36491 break;
36492 default:
36493 gcc_unreachable ();
36495 return stmt != NULL_TREE;
36497 keep_next_level (true);
36498 tree sb = begin_omp_structured_block (), ret;
36499 unsigned save = cp_parser_begin_omp_structured_block (parser);
36500 switch (ccode)
36502 case OMP_TEAMS:
36503 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36504 OMP_TARGET_CLAUSE_MASK, cclauses,
36505 if_p);
36506 break;
36507 case OMP_PARALLEL:
36508 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36509 OMP_TARGET_CLAUSE_MASK, cclauses,
36510 if_p);
36511 break;
36512 case OMP_SIMD:
36513 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36514 OMP_TARGET_CLAUSE_MASK, cclauses,
36515 if_p);
36516 break;
36517 default:
36518 gcc_unreachable ();
36520 cp_parser_end_omp_structured_block (parser, save);
36521 tree body = finish_omp_structured_block (sb);
36522 if (ret == NULL_TREE)
36523 return false;
36524 if (ccode == OMP_TEAMS && !processing_template_decl)
36526 /* For combined target teams, ensure the num_teams and
36527 thread_limit clause expressions are evaluated on the host,
36528 before entering the target construct. */
36529 tree c;
36530 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36531 c; c = OMP_CLAUSE_CHAIN (c))
36532 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36533 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36534 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36536 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36537 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36538 if (expr == error_mark_node)
36539 continue;
36540 tree tmp = TARGET_EXPR_SLOT (expr);
36541 add_stmt (expr);
36542 OMP_CLAUSE_OPERAND (c, 0) = expr;
36543 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36544 OMP_CLAUSE_FIRSTPRIVATE);
36545 OMP_CLAUSE_DECL (tc) = tmp;
36546 OMP_CLAUSE_CHAIN (tc)
36547 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36548 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36551 tree stmt = make_node (OMP_TARGET);
36552 TREE_TYPE (stmt) = void_type_node;
36553 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36554 OMP_TARGET_BODY (stmt) = body;
36555 OMP_TARGET_COMBINED (stmt) = 1;
36556 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36557 add_stmt (stmt);
36558 pc = &OMP_TARGET_CLAUSES (stmt);
36559 goto check_clauses;
36561 else if (!flag_openmp) /* flag_openmp_simd */
36563 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36564 return false;
36566 else if (strcmp (p, "data") == 0)
36568 cp_lexer_consume_token (parser->lexer);
36569 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36570 return true;
36572 else if (strcmp (p, "enter") == 0)
36574 cp_lexer_consume_token (parser->lexer);
36575 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36576 return false;
36578 else if (strcmp (p, "exit") == 0)
36580 cp_lexer_consume_token (parser->lexer);
36581 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36582 return false;
36584 else if (strcmp (p, "update") == 0)
36586 cp_lexer_consume_token (parser->lexer);
36587 return cp_parser_omp_target_update (parser, pragma_tok, context);
36590 if (!flag_openmp) /* flag_openmp_simd */
36592 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36593 return false;
36596 stmt = make_node (OMP_TARGET);
36597 TREE_TYPE (stmt) = void_type_node;
36599 OMP_TARGET_CLAUSES (stmt)
36600 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36601 "#pragma omp target", pragma_tok);
36602 pc = &OMP_TARGET_CLAUSES (stmt);
36603 keep_next_level (true);
36604 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36606 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36607 add_stmt (stmt);
36609 check_clauses:
36610 while (*pc)
36612 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36613 switch (OMP_CLAUSE_MAP_KIND (*pc))
36615 case GOMP_MAP_TO:
36616 case GOMP_MAP_ALWAYS_TO:
36617 case GOMP_MAP_FROM:
36618 case GOMP_MAP_ALWAYS_FROM:
36619 case GOMP_MAP_TOFROM:
36620 case GOMP_MAP_ALWAYS_TOFROM:
36621 case GOMP_MAP_ALLOC:
36622 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36623 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36624 case GOMP_MAP_ALWAYS_POINTER:
36625 break;
36626 default:
36627 error_at (OMP_CLAUSE_LOCATION (*pc),
36628 "%<#pragma omp target%> with map-type other "
36629 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36630 "on %<map%> clause");
36631 *pc = OMP_CLAUSE_CHAIN (*pc);
36632 continue;
36634 pc = &OMP_CLAUSE_CHAIN (*pc);
36636 return true;
36639 /* OpenACC 2.0:
36640 # pragma acc cache (variable-list) new-line
36643 static tree
36644 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36646 tree stmt, clauses;
36648 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36649 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36651 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36653 stmt = make_node (OACC_CACHE);
36654 TREE_TYPE (stmt) = void_type_node;
36655 OACC_CACHE_CLAUSES (stmt) = clauses;
36656 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36657 add_stmt (stmt);
36659 return stmt;
36662 /* OpenACC 2.0:
36663 # pragma acc data oacc-data-clause[optseq] new-line
36664 structured-block */
36666 #define OACC_DATA_CLAUSE_MASK \
36667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36679 static tree
36680 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36682 tree stmt, clauses, block;
36683 unsigned int save;
36685 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36686 "#pragma acc data", pragma_tok);
36688 block = begin_omp_parallel ();
36689 save = cp_parser_begin_omp_structured_block (parser);
36690 cp_parser_statement (parser, NULL_TREE, false, if_p);
36691 cp_parser_end_omp_structured_block (parser, save);
36692 stmt = finish_oacc_data (clauses, block);
36693 return stmt;
36696 /* OpenACC 2.0:
36697 # pragma acc host_data <clauses> new-line
36698 structured-block */
36700 #define OACC_HOST_DATA_CLAUSE_MASK \
36701 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36703 static tree
36704 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36706 tree stmt, clauses, block;
36707 unsigned int save;
36709 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36710 "#pragma acc host_data", pragma_tok);
36712 block = begin_omp_parallel ();
36713 save = cp_parser_begin_omp_structured_block (parser);
36714 cp_parser_statement (parser, NULL_TREE, false, if_p);
36715 cp_parser_end_omp_structured_block (parser, save);
36716 stmt = finish_oacc_host_data (clauses, block);
36717 return stmt;
36720 /* OpenACC 2.0:
36721 # pragma acc declare oacc-data-clause[optseq] new-line
36724 #define OACC_DECLARE_CLAUSE_MASK \
36725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36738 static tree
36739 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36741 tree clauses, stmt;
36742 bool error = false;
36744 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36745 "#pragma acc declare", pragma_tok, true);
36748 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36750 error_at (pragma_tok->location,
36751 "no valid clauses specified in %<#pragma acc declare%>");
36752 return NULL_TREE;
36755 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36757 location_t loc = OMP_CLAUSE_LOCATION (t);
36758 tree decl = OMP_CLAUSE_DECL (t);
36759 if (!DECL_P (decl))
36761 error_at (loc, "array section in %<#pragma acc declare%>");
36762 error = true;
36763 continue;
36765 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36766 switch (OMP_CLAUSE_MAP_KIND (t))
36768 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36769 case GOMP_MAP_FORCE_ALLOC:
36770 case GOMP_MAP_FORCE_TO:
36771 case GOMP_MAP_FORCE_DEVICEPTR:
36772 case GOMP_MAP_DEVICE_RESIDENT:
36773 break;
36775 case GOMP_MAP_LINK:
36776 if (!global_bindings_p ()
36777 && (TREE_STATIC (decl)
36778 || !DECL_EXTERNAL (decl)))
36780 error_at (loc,
36781 "%qD must be a global variable in "
36782 "%<#pragma acc declare link%>",
36783 decl);
36784 error = true;
36785 continue;
36787 break;
36789 default:
36790 if (global_bindings_p ())
36792 error_at (loc, "invalid OpenACC clause at file scope");
36793 error = true;
36794 continue;
36796 if (DECL_EXTERNAL (decl))
36798 error_at (loc,
36799 "invalid use of %<extern%> variable %qD "
36800 "in %<#pragma acc declare%>", decl);
36801 error = true;
36802 continue;
36804 else if (TREE_PUBLIC (decl))
36806 error_at (loc,
36807 "invalid use of %<global%> variable %qD "
36808 "in %<#pragma acc declare%>", decl);
36809 error = true;
36810 continue;
36812 break;
36815 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36816 || lookup_attribute ("omp declare target link",
36817 DECL_ATTRIBUTES (decl)))
36819 error_at (loc, "variable %qD used more than once with "
36820 "%<#pragma acc declare%>", decl);
36821 error = true;
36822 continue;
36825 if (!error)
36827 tree id;
36829 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36830 id = get_identifier ("omp declare target link");
36831 else
36832 id = get_identifier ("omp declare target");
36834 DECL_ATTRIBUTES (decl)
36835 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36836 if (global_bindings_p ())
36838 symtab_node *node = symtab_node::get (decl);
36839 if (node != NULL)
36841 node->offloadable = 1;
36842 if (ENABLE_OFFLOADING)
36844 g->have_offload = true;
36845 if (is_a <varpool_node *> (node))
36846 vec_safe_push (offload_vars, decl);
36853 if (error || global_bindings_p ())
36854 return NULL_TREE;
36856 stmt = make_node (OACC_DECLARE);
36857 TREE_TYPE (stmt) = void_type_node;
36858 OACC_DECLARE_CLAUSES (stmt) = clauses;
36859 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36861 add_stmt (stmt);
36863 return NULL_TREE;
36866 /* OpenACC 2.0:
36867 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36871 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36873 LOC is the location of the #pragma token.
36876 #define OACC_ENTER_DATA_CLAUSE_MASK \
36877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36885 #define OACC_EXIT_DATA_CLAUSE_MASK \
36886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36892 static tree
36893 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36894 bool enter)
36896 location_t loc = pragma_tok->location;
36897 tree stmt, clauses;
36898 const char *p = "";
36900 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36901 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36903 if (strcmp (p, "data") != 0)
36905 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36906 enter ? "enter" : "exit");
36907 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36908 return NULL_TREE;
36911 cp_lexer_consume_token (parser->lexer);
36913 if (enter)
36914 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36915 "#pragma acc enter data", pragma_tok);
36916 else
36917 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36918 "#pragma acc exit data", pragma_tok);
36920 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36922 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36923 enter ? "enter" : "exit");
36924 return NULL_TREE;
36927 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36928 TREE_TYPE (stmt) = void_type_node;
36929 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36930 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36931 add_stmt (stmt);
36932 return stmt;
36935 /* OpenACC 2.0:
36936 # pragma acc loop oacc-loop-clause[optseq] new-line
36937 structured-block */
36939 #define OACC_LOOP_CLAUSE_MASK \
36940 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36951 static tree
36952 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36953 omp_clause_mask mask, tree *cclauses, bool *if_p)
36955 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36957 strcat (p_name, " loop");
36958 mask |= OACC_LOOP_CLAUSE_MASK;
36960 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36961 cclauses == NULL);
36962 if (cclauses)
36964 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36965 if (*cclauses)
36966 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36967 if (clauses)
36968 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36971 tree block = begin_omp_structured_block ();
36972 int save = cp_parser_begin_omp_structured_block (parser);
36973 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36974 cp_parser_end_omp_structured_block (parser, save);
36975 add_stmt (finish_omp_structured_block (block));
36977 return stmt;
36980 /* OpenACC 2.0:
36981 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36982 structured-block
36986 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36987 structured-block
36990 #define OACC_KERNELS_CLAUSE_MASK \
36991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37009 #define OACC_PARALLEL_CLAUSE_MASK \
37010 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37031 static tree
37032 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37033 char *p_name, bool *if_p)
37035 omp_clause_mask mask;
37036 enum tree_code code;
37037 switch (cp_parser_pragma_kind (pragma_tok))
37039 case PRAGMA_OACC_KERNELS:
37040 strcat (p_name, " kernels");
37041 mask = OACC_KERNELS_CLAUSE_MASK;
37042 code = OACC_KERNELS;
37043 break;
37044 case PRAGMA_OACC_PARALLEL:
37045 strcat (p_name, " parallel");
37046 mask = OACC_PARALLEL_CLAUSE_MASK;
37047 code = OACC_PARALLEL;
37048 break;
37049 default:
37050 gcc_unreachable ();
37053 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37055 const char *p
37056 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37057 if (strcmp (p, "loop") == 0)
37059 cp_lexer_consume_token (parser->lexer);
37060 tree block = begin_omp_parallel ();
37061 tree clauses;
37062 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37063 if_p);
37064 return finish_omp_construct (code, block, clauses);
37068 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37070 tree block = begin_omp_parallel ();
37071 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37072 cp_parser_statement (parser, NULL_TREE, false, if_p);
37073 cp_parser_end_omp_structured_block (parser, save);
37074 return finish_omp_construct (code, block, clauses);
37077 /* OpenACC 2.0:
37078 # pragma acc update oacc-update-clause[optseq] new-line
37081 #define OACC_UPDATE_CLAUSE_MASK \
37082 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37089 static tree
37090 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37092 tree stmt, clauses;
37094 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37095 "#pragma acc update", pragma_tok);
37097 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37099 error_at (pragma_tok->location,
37100 "%<#pragma acc update%> must contain at least one "
37101 "%<device%> or %<host%> or %<self%> clause");
37102 return NULL_TREE;
37105 stmt = make_node (OACC_UPDATE);
37106 TREE_TYPE (stmt) = void_type_node;
37107 OACC_UPDATE_CLAUSES (stmt) = clauses;
37108 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37109 add_stmt (stmt);
37110 return stmt;
37113 /* OpenACC 2.0:
37114 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37116 LOC is the location of the #pragma token.
37119 #define OACC_WAIT_CLAUSE_MASK \
37120 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37122 static tree
37123 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37125 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37126 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37128 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37129 list = cp_parser_oacc_wait_list (parser, loc, list);
37131 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37132 "#pragma acc wait", pragma_tok);
37134 stmt = c_finish_oacc_wait (loc, list, clauses);
37135 stmt = finish_expr_stmt (stmt);
37137 return stmt;
37140 /* OpenMP 4.0:
37141 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37143 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37151 static void
37152 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37153 enum pragma_context context)
37155 bool first_p = parser->omp_declare_simd == NULL;
37156 cp_omp_declare_simd_data data;
37157 if (first_p)
37159 data.error_seen = false;
37160 data.fndecl_seen = false;
37161 data.tokens = vNULL;
37162 data.clauses = NULL_TREE;
37163 /* It is safe to take the address of a local variable; it will only be
37164 used while this scope is live. */
37165 parser->omp_declare_simd = &data;
37168 /* Store away all pragma tokens. */
37169 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37170 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37171 cp_lexer_consume_token (parser->lexer);
37172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37173 parser->omp_declare_simd->error_seen = true;
37174 cp_parser_require_pragma_eol (parser, pragma_tok);
37175 struct cp_token_cache *cp
37176 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37177 parser->omp_declare_simd->tokens.safe_push (cp);
37179 if (first_p)
37181 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37182 cp_parser_pragma (parser, context, NULL);
37183 switch (context)
37185 case pragma_external:
37186 cp_parser_declaration (parser);
37187 break;
37188 case pragma_member:
37189 cp_parser_member_declaration (parser);
37190 break;
37191 case pragma_objc_icode:
37192 cp_parser_block_declaration (parser, /*statement_p=*/false);
37193 break;
37194 default:
37195 cp_parser_declaration_statement (parser);
37196 break;
37198 if (parser->omp_declare_simd
37199 && !parser->omp_declare_simd->error_seen
37200 && !parser->omp_declare_simd->fndecl_seen)
37201 error_at (pragma_tok->location,
37202 "%<#pragma omp declare simd%> not immediately followed by "
37203 "function declaration or definition");
37204 data.tokens.release ();
37205 parser->omp_declare_simd = NULL;
37209 /* Finalize #pragma omp declare simd clauses after direct declarator has
37210 been parsed, and put that into "omp declare simd" attribute. */
37212 static tree
37213 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37215 struct cp_token_cache *ce;
37216 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37217 int i;
37219 if (!data->error_seen && data->fndecl_seen)
37221 error ("%<#pragma omp declare simd%> not immediately followed by "
37222 "a single function declaration or definition");
37223 data->error_seen = true;
37225 if (data->error_seen)
37226 return attrs;
37228 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37230 tree c, cl;
37232 cp_parser_push_lexer_for_tokens (parser, ce);
37233 parser->lexer->in_pragma = true;
37234 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37235 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37236 cp_lexer_consume_token (parser->lexer);
37237 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37238 "#pragma omp declare simd", pragma_tok);
37239 cp_parser_pop_lexer (parser);
37240 if (cl)
37241 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37242 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37243 TREE_CHAIN (c) = attrs;
37244 if (processing_template_decl)
37245 ATTR_IS_DEPENDENT (c) = 1;
37246 attrs = c;
37249 data->fndecl_seen = true;
37250 return attrs;
37254 /* OpenMP 4.0:
37255 # pragma omp declare target new-line
37256 declarations and definitions
37257 # pragma omp end declare target new-line
37259 OpenMP 4.5:
37260 # pragma omp declare target ( extended-list ) new-line
37262 # pragma omp declare target declare-target-clauses[seq] new-line */
37264 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37265 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37268 static void
37269 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37271 tree clauses = NULL_TREE;
37272 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37273 clauses
37274 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37275 "#pragma omp declare target", pragma_tok);
37276 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37278 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37279 clauses);
37280 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37281 cp_parser_require_pragma_eol (parser, pragma_tok);
37283 else
37285 cp_parser_require_pragma_eol (parser, pragma_tok);
37286 scope_chain->omp_declare_target_attribute++;
37287 return;
37289 if (scope_chain->omp_declare_target_attribute)
37290 error_at (pragma_tok->location,
37291 "%<#pragma omp declare target%> with clauses in between "
37292 "%<#pragma omp declare target%> without clauses and "
37293 "%<#pragma omp end declare target%>");
37294 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37296 tree t = OMP_CLAUSE_DECL (c), id;
37297 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37298 tree at2 = lookup_attribute ("omp declare target link",
37299 DECL_ATTRIBUTES (t));
37300 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37302 id = get_identifier ("omp declare target link");
37303 std::swap (at1, at2);
37305 else
37306 id = get_identifier ("omp declare target");
37307 if (at2)
37309 error_at (OMP_CLAUSE_LOCATION (c),
37310 "%qD specified both in declare target %<link%> and %<to%>"
37311 " clauses", t);
37312 continue;
37314 if (!at1)
37316 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37317 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37318 continue;
37320 symtab_node *node = symtab_node::get (t);
37321 if (node != NULL)
37323 node->offloadable = 1;
37324 if (ENABLE_OFFLOADING)
37326 g->have_offload = true;
37327 if (is_a <varpool_node *> (node))
37328 vec_safe_push (offload_vars, t);
37335 static void
37336 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37338 const char *p = "";
37339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37341 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37342 p = IDENTIFIER_POINTER (id);
37344 if (strcmp (p, "declare") == 0)
37346 cp_lexer_consume_token (parser->lexer);
37347 p = "";
37348 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37350 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37351 p = IDENTIFIER_POINTER (id);
37353 if (strcmp (p, "target") == 0)
37354 cp_lexer_consume_token (parser->lexer);
37355 else
37357 cp_parser_error (parser, "expected %<target%>");
37358 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37359 return;
37362 else
37364 cp_parser_error (parser, "expected %<declare%>");
37365 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37366 return;
37368 cp_parser_require_pragma_eol (parser, pragma_tok);
37369 if (!scope_chain->omp_declare_target_attribute)
37370 error_at (pragma_tok->location,
37371 "%<#pragma omp end declare target%> without corresponding "
37372 "%<#pragma omp declare target%>");
37373 else
37374 scope_chain->omp_declare_target_attribute--;
37377 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37378 expression and optional initializer clause of
37379 #pragma omp declare reduction. We store the expression(s) as
37380 either 3, 6 or 7 special statements inside of the artificial function's
37381 body. The first two statements are DECL_EXPRs for the artificial
37382 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37383 expression that uses those variables.
37384 If there was any INITIALIZER clause, this is followed by further statements,
37385 the fourth and fifth statements are DECL_EXPRs for the artificial
37386 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37387 constructor variant (first token after open paren is not omp_priv),
37388 then the sixth statement is a statement with the function call expression
37389 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37390 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37391 to initialize the OMP_PRIV artificial variable and there is seventh
37392 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37394 static bool
37395 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37397 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37398 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37399 type = TREE_TYPE (type);
37400 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37401 DECL_ARTIFICIAL (omp_out) = 1;
37402 pushdecl (omp_out);
37403 add_decl_expr (omp_out);
37404 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37405 DECL_ARTIFICIAL (omp_in) = 1;
37406 pushdecl (omp_in);
37407 add_decl_expr (omp_in);
37408 tree combiner;
37409 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37411 keep_next_level (true);
37412 tree block = begin_omp_structured_block ();
37413 combiner = cp_parser_expression (parser);
37414 finish_expr_stmt (combiner);
37415 block = finish_omp_structured_block (block);
37416 add_stmt (block);
37418 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37419 return false;
37421 const char *p = "";
37422 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37424 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37425 p = IDENTIFIER_POINTER (id);
37428 if (strcmp (p, "initializer") == 0)
37430 cp_lexer_consume_token (parser->lexer);
37431 matching_parens parens;
37432 if (!parens.require_open (parser))
37433 return false;
37435 p = "";
37436 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37438 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37439 p = IDENTIFIER_POINTER (id);
37442 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37443 DECL_ARTIFICIAL (omp_priv) = 1;
37444 pushdecl (omp_priv);
37445 add_decl_expr (omp_priv);
37446 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37447 DECL_ARTIFICIAL (omp_orig) = 1;
37448 pushdecl (omp_orig);
37449 add_decl_expr (omp_orig);
37451 keep_next_level (true);
37452 block = begin_omp_structured_block ();
37454 bool ctor = false;
37455 if (strcmp (p, "omp_priv") == 0)
37457 bool is_direct_init, is_non_constant_init;
37458 ctor = true;
37459 cp_lexer_consume_token (parser->lexer);
37460 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37461 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37462 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37463 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37464 == CPP_CLOSE_PAREN
37465 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37466 == CPP_CLOSE_PAREN))
37468 finish_omp_structured_block (block);
37469 error ("invalid initializer clause");
37470 return false;
37472 initializer = cp_parser_initializer (parser, &is_direct_init,
37473 &is_non_constant_init);
37474 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37475 NULL_TREE, LOOKUP_ONLYCONVERTING);
37477 else
37479 cp_parser_parse_tentatively (parser);
37480 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37481 /*check_dependency_p=*/true,
37482 /*template_p=*/NULL,
37483 /*declarator_p=*/false,
37484 /*optional_p=*/false);
37485 vec<tree, va_gc> *args;
37486 if (fn_name == error_mark_node
37487 || cp_parser_error_occurred (parser)
37488 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37489 || ((args = cp_parser_parenthesized_expression_list
37490 (parser, non_attr, /*cast_p=*/false,
37491 /*allow_expansion_p=*/true,
37492 /*non_constant_p=*/NULL)),
37493 cp_parser_error_occurred (parser)))
37495 finish_omp_structured_block (block);
37496 cp_parser_abort_tentative_parse (parser);
37497 cp_parser_error (parser, "expected id-expression (arguments)");
37498 return false;
37500 unsigned int i;
37501 tree arg;
37502 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37503 if (arg == omp_priv
37504 || (TREE_CODE (arg) == ADDR_EXPR
37505 && TREE_OPERAND (arg, 0) == omp_priv))
37506 break;
37507 cp_parser_abort_tentative_parse (parser);
37508 if (arg == NULL_TREE)
37509 error ("one of the initializer call arguments should be %<omp_priv%>"
37510 " or %<&omp_priv%>");
37511 initializer = cp_parser_postfix_expression (parser, false, false, false,
37512 false, NULL);
37513 finish_expr_stmt (initializer);
37516 block = finish_omp_structured_block (block);
37517 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37518 add_stmt (block);
37520 if (ctor)
37521 add_decl_expr (omp_orig);
37523 if (!parens.require_close (parser))
37524 return false;
37527 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37528 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37529 UNKNOWN_LOCATION);
37531 return true;
37534 /* OpenMP 4.0
37535 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37536 initializer-clause[opt] new-line
37538 initializer-clause:
37539 initializer (omp_priv initializer)
37540 initializer (function-name (argument-list)) */
37542 static void
37543 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37544 enum pragma_context)
37546 auto_vec<tree> types;
37547 enum tree_code reduc_code = ERROR_MARK;
37548 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37549 unsigned int i;
37550 cp_token *first_token;
37551 cp_token_cache *cp;
37552 int errs;
37553 void *p;
37555 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37556 p = obstack_alloc (&declarator_obstack, 0);
37558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37559 goto fail;
37561 switch (cp_lexer_peek_token (parser->lexer)->type)
37563 case CPP_PLUS:
37564 reduc_code = PLUS_EXPR;
37565 break;
37566 case CPP_MULT:
37567 reduc_code = MULT_EXPR;
37568 break;
37569 case CPP_MINUS:
37570 reduc_code = MINUS_EXPR;
37571 break;
37572 case CPP_AND:
37573 reduc_code = BIT_AND_EXPR;
37574 break;
37575 case CPP_XOR:
37576 reduc_code = BIT_XOR_EXPR;
37577 break;
37578 case CPP_OR:
37579 reduc_code = BIT_IOR_EXPR;
37580 break;
37581 case CPP_AND_AND:
37582 reduc_code = TRUTH_ANDIF_EXPR;
37583 break;
37584 case CPP_OR_OR:
37585 reduc_code = TRUTH_ORIF_EXPR;
37586 break;
37587 case CPP_NAME:
37588 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37589 break;
37590 default:
37591 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37592 "%<|%>, %<&&%>, %<||%> or identifier");
37593 goto fail;
37596 if (reduc_code != ERROR_MARK)
37597 cp_lexer_consume_token (parser->lexer);
37599 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37600 if (reduc_id == error_mark_node)
37601 goto fail;
37603 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37604 goto fail;
37606 /* Types may not be defined in declare reduction type list. */
37607 const char *saved_message;
37608 saved_message = parser->type_definition_forbidden_message;
37609 parser->type_definition_forbidden_message
37610 = G_("types may not be defined in declare reduction type list");
37611 bool saved_colon_corrects_to_scope_p;
37612 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37613 parser->colon_corrects_to_scope_p = false;
37614 bool saved_colon_doesnt_start_class_def_p;
37615 saved_colon_doesnt_start_class_def_p
37616 = parser->colon_doesnt_start_class_def_p;
37617 parser->colon_doesnt_start_class_def_p = true;
37619 while (true)
37621 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37622 type = cp_parser_type_id (parser);
37623 if (type == error_mark_node)
37625 else if (ARITHMETIC_TYPE_P (type)
37626 && (orig_reduc_id == NULL_TREE
37627 || (TREE_CODE (type) != COMPLEX_TYPE
37628 && (id_equal (orig_reduc_id, "min")
37629 || id_equal (orig_reduc_id, "max")))))
37630 error_at (loc, "predeclared arithmetic type %qT in "
37631 "%<#pragma omp declare reduction%>", type);
37632 else if (TREE_CODE (type) == FUNCTION_TYPE
37633 || TREE_CODE (type) == METHOD_TYPE
37634 || TREE_CODE (type) == ARRAY_TYPE)
37635 error_at (loc, "function or array type %qT in "
37636 "%<#pragma omp declare reduction%>", type);
37637 else if (TREE_CODE (type) == REFERENCE_TYPE)
37638 error_at (loc, "reference type %qT in "
37639 "%<#pragma omp declare reduction%>", type);
37640 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37641 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37642 "%<#pragma omp declare reduction%>", type);
37643 else
37644 types.safe_push (type);
37646 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37647 cp_lexer_consume_token (parser->lexer);
37648 else
37649 break;
37652 /* Restore the saved message. */
37653 parser->type_definition_forbidden_message = saved_message;
37654 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37655 parser->colon_doesnt_start_class_def_p
37656 = saved_colon_doesnt_start_class_def_p;
37658 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37659 || types.is_empty ())
37661 fail:
37662 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37663 goto done;
37666 first_token = cp_lexer_peek_token (parser->lexer);
37667 cp = NULL;
37668 errs = errorcount;
37669 FOR_EACH_VEC_ELT (types, i, type)
37671 tree fntype
37672 = build_function_type_list (void_type_node,
37673 cp_build_reference_type (type, false),
37674 NULL_TREE);
37675 tree this_reduc_id = reduc_id;
37676 if (!dependent_type_p (type))
37677 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37678 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37679 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37680 DECL_ARTIFICIAL (fndecl) = 1;
37681 DECL_EXTERNAL (fndecl) = 1;
37682 DECL_DECLARED_INLINE_P (fndecl) = 1;
37683 DECL_IGNORED_P (fndecl) = 1;
37684 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37685 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37686 DECL_ATTRIBUTES (fndecl)
37687 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37688 DECL_ATTRIBUTES (fndecl));
37689 if (processing_template_decl)
37690 fndecl = push_template_decl (fndecl);
37691 bool block_scope = false;
37692 tree block = NULL_TREE;
37693 if (current_function_decl)
37695 block_scope = true;
37696 DECL_CONTEXT (fndecl) = global_namespace;
37697 if (!processing_template_decl)
37698 pushdecl (fndecl);
37700 else if (current_class_type)
37702 if (cp == NULL)
37704 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37705 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37706 cp_lexer_consume_token (parser->lexer);
37707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37708 goto fail;
37709 cp = cp_token_cache_new (first_token,
37710 cp_lexer_peek_nth_token (parser->lexer,
37711 2));
37713 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37714 finish_member_declaration (fndecl);
37715 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37716 DECL_PENDING_INLINE_P (fndecl) = 1;
37717 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37718 continue;
37720 else
37722 DECL_CONTEXT (fndecl) = current_namespace;
37723 pushdecl (fndecl);
37725 if (!block_scope)
37726 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37727 else
37728 block = begin_omp_structured_block ();
37729 if (cp)
37731 cp_parser_push_lexer_for_tokens (parser, cp);
37732 parser->lexer->in_pragma = true;
37734 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37736 if (!block_scope)
37737 finish_function (/*inline_p=*/false);
37738 else
37739 DECL_CONTEXT (fndecl) = current_function_decl;
37740 if (cp)
37741 cp_parser_pop_lexer (parser);
37742 goto fail;
37744 if (cp)
37745 cp_parser_pop_lexer (parser);
37746 if (!block_scope)
37747 finish_function (/*inline_p=*/false);
37748 else
37750 DECL_CONTEXT (fndecl) = current_function_decl;
37751 block = finish_omp_structured_block (block);
37752 if (TREE_CODE (block) == BIND_EXPR)
37753 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37754 else if (TREE_CODE (block) == STATEMENT_LIST)
37755 DECL_SAVED_TREE (fndecl) = block;
37756 if (processing_template_decl)
37757 add_decl_expr (fndecl);
37759 cp_check_omp_declare_reduction (fndecl);
37760 if (cp == NULL && types.length () > 1)
37761 cp = cp_token_cache_new (first_token,
37762 cp_lexer_peek_nth_token (parser->lexer, 2));
37763 if (errs != errorcount)
37764 break;
37767 cp_parser_require_pragma_eol (parser, pragma_tok);
37769 done:
37770 /* Free any declarators allocated. */
37771 obstack_free (&declarator_obstack, p);
37774 /* OpenMP 4.0
37775 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37776 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37777 initializer-clause[opt] new-line
37778 #pragma omp declare target new-line */
37780 static bool
37781 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37782 enum pragma_context context)
37784 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37786 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37787 const char *p = IDENTIFIER_POINTER (id);
37789 if (strcmp (p, "simd") == 0)
37791 cp_lexer_consume_token (parser->lexer);
37792 cp_parser_omp_declare_simd (parser, pragma_tok,
37793 context);
37794 return true;
37796 cp_ensure_no_omp_declare_simd (parser);
37797 if (strcmp (p, "reduction") == 0)
37799 cp_lexer_consume_token (parser->lexer);
37800 cp_parser_omp_declare_reduction (parser, pragma_tok,
37801 context);
37802 return false;
37804 if (!flag_openmp) /* flag_openmp_simd */
37806 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37807 return false;
37809 if (strcmp (p, "target") == 0)
37811 cp_lexer_consume_token (parser->lexer);
37812 cp_parser_omp_declare_target (parser, pragma_tok);
37813 return false;
37816 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37817 "or %<target%>");
37818 cp_parser_require_pragma_eol (parser, pragma_tok);
37819 return false;
37822 /* OpenMP 4.5:
37823 #pragma omp taskloop taskloop-clause[optseq] new-line
37824 for-loop
37826 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37827 for-loop */
37829 #define OMP_TASKLOOP_CLAUSE_MASK \
37830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37845 static tree
37846 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37847 char *p_name, omp_clause_mask mask, tree *cclauses,
37848 bool *if_p)
37850 tree clauses, sb, ret;
37851 unsigned int save;
37852 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37854 strcat (p_name, " taskloop");
37855 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37857 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37859 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37860 const char *p = IDENTIFIER_POINTER (id);
37862 if (strcmp (p, "simd") == 0)
37864 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37865 if (cclauses == NULL)
37866 cclauses = cclauses_buf;
37868 cp_lexer_consume_token (parser->lexer);
37869 if (!flag_openmp) /* flag_openmp_simd */
37870 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37871 cclauses, if_p);
37872 sb = begin_omp_structured_block ();
37873 save = cp_parser_begin_omp_structured_block (parser);
37874 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37875 cclauses, if_p);
37876 cp_parser_end_omp_structured_block (parser, save);
37877 tree body = finish_omp_structured_block (sb);
37878 if (ret == NULL)
37879 return ret;
37880 ret = make_node (OMP_TASKLOOP);
37881 TREE_TYPE (ret) = void_type_node;
37882 OMP_FOR_BODY (ret) = body;
37883 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37884 SET_EXPR_LOCATION (ret, loc);
37885 add_stmt (ret);
37886 return ret;
37889 if (!flag_openmp) /* flag_openmp_simd */
37891 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37892 return NULL_TREE;
37895 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37896 cclauses == NULL);
37897 if (cclauses)
37899 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37900 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37903 sb = begin_omp_structured_block ();
37904 save = cp_parser_begin_omp_structured_block (parser);
37906 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37907 if_p);
37909 cp_parser_end_omp_structured_block (parser, save);
37910 add_stmt (finish_omp_structured_block (sb));
37912 return ret;
37916 /* OpenACC 2.0:
37917 # pragma acc routine oacc-routine-clause[optseq] new-line
37918 function-definition
37920 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37923 #define OACC_ROUTINE_CLAUSE_MASK \
37924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37930 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37931 component, which must resolve to a declared namespace-scope
37932 function. The clauses are either processed directly (for a named
37933 function), or defered until the immediatley following declaration
37934 is parsed. */
37936 static void
37937 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37938 enum pragma_context context)
37940 gcc_checking_assert (context == pragma_external);
37941 /* The checking for "another pragma following this one" in the "no optional
37942 '( name )'" case makes sure that we dont re-enter. */
37943 gcc_checking_assert (parser->oacc_routine == NULL);
37945 cp_oacc_routine_data data;
37946 data.error_seen = false;
37947 data.fndecl_seen = false;
37948 data.tokens = vNULL;
37949 data.clauses = NULL_TREE;
37950 data.loc = pragma_tok->location;
37951 /* It is safe to take the address of a local variable; it will only be
37952 used while this scope is live. */
37953 parser->oacc_routine = &data;
37955 /* Look for optional '( name )'. */
37956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37958 matching_parens parens;
37959 parens.consume_open (parser); /* '(' */
37961 /* We parse the name as an id-expression. If it resolves to
37962 anything other than a non-overloaded function at namespace
37963 scope, it's an error. */
37964 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37965 tree name = cp_parser_id_expression (parser,
37966 /*template_keyword_p=*/false,
37967 /*check_dependency_p=*/false,
37968 /*template_p=*/NULL,
37969 /*declarator_p=*/false,
37970 /*optional_p=*/false);
37971 tree decl = (identifier_p (name)
37972 ? cp_parser_lookup_name_simple (parser, name, name_loc)
37973 : name);
37974 if (name != error_mark_node && decl == error_mark_node)
37975 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37977 if (decl == error_mark_node
37978 || !parens.require_close (parser))
37980 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37981 parser->oacc_routine = NULL;
37982 return;
37985 data.clauses
37986 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37987 "#pragma acc routine",
37988 cp_lexer_peek_token (parser->lexer));
37990 if (decl && is_overloaded_fn (decl)
37991 && (TREE_CODE (decl) != FUNCTION_DECL
37992 || DECL_FUNCTION_TEMPLATE_P (decl)))
37994 error_at (name_loc,
37995 "%<#pragma acc routine%> names a set of overloads");
37996 parser->oacc_routine = NULL;
37997 return;
38000 /* Perhaps we should use the same rule as declarations in different
38001 namespaces? */
38002 if (!DECL_NAMESPACE_SCOPE_P (decl))
38004 error_at (name_loc,
38005 "%qD does not refer to a namespace scope function", decl);
38006 parser->oacc_routine = NULL;
38007 return;
38010 if (TREE_CODE (decl) != FUNCTION_DECL)
38012 error_at (name_loc, "%qD does not refer to a function", decl);
38013 parser->oacc_routine = NULL;
38014 return;
38017 cp_finalize_oacc_routine (parser, decl, false);
38018 parser->oacc_routine = NULL;
38020 else /* No optional '( name )'. */
38022 /* Store away all pragma tokens. */
38023 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38024 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38025 cp_lexer_consume_token (parser->lexer);
38026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38027 parser->oacc_routine->error_seen = true;
38028 cp_parser_require_pragma_eol (parser, pragma_tok);
38029 struct cp_token_cache *cp
38030 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38031 parser->oacc_routine->tokens.safe_push (cp);
38033 /* Emit a helpful diagnostic if there's another pragma following this
38034 one. */
38035 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38037 cp_ensure_no_oacc_routine (parser);
38038 data.tokens.release ();
38039 /* ..., and then just keep going. */
38040 return;
38043 /* We only have to consider the pragma_external case here. */
38044 cp_parser_declaration (parser);
38045 if (parser->oacc_routine
38046 && !parser->oacc_routine->fndecl_seen)
38047 cp_ensure_no_oacc_routine (parser);
38048 else
38049 parser->oacc_routine = NULL;
38050 data.tokens.release ();
38054 /* Finalize #pragma acc routine clauses after direct declarator has
38055 been parsed. */
38057 static tree
38058 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38060 struct cp_token_cache *ce;
38061 cp_oacc_routine_data *data = parser->oacc_routine;
38063 if (!data->error_seen && data->fndecl_seen)
38065 error_at (data->loc,
38066 "%<#pragma acc routine%> not immediately followed by "
38067 "a single function declaration or definition");
38068 data->error_seen = true;
38070 if (data->error_seen)
38071 return attrs;
38073 gcc_checking_assert (data->tokens.length () == 1);
38074 ce = data->tokens[0];
38076 cp_parser_push_lexer_for_tokens (parser, ce);
38077 parser->lexer->in_pragma = true;
38078 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38080 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38081 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38082 parser->oacc_routine->clauses
38083 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38084 "#pragma acc routine", pragma_tok);
38085 cp_parser_pop_lexer (parser);
38086 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38087 fndecl_seen. */
38089 return attrs;
38092 /* Apply any saved OpenACC routine clauses to a just-parsed
38093 declaration. */
38095 static void
38096 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38098 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38100 /* Keep going if we're in error reporting mode. */
38101 if (parser->oacc_routine->error_seen
38102 || fndecl == error_mark_node)
38103 return;
38105 if (parser->oacc_routine->fndecl_seen)
38107 error_at (parser->oacc_routine->loc,
38108 "%<#pragma acc routine%> not immediately followed by"
38109 " a single function declaration or definition");
38110 parser->oacc_routine = NULL;
38111 return;
38113 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38115 cp_ensure_no_oacc_routine (parser);
38116 return;
38119 if (oacc_get_fn_attrib (fndecl))
38121 error_at (parser->oacc_routine->loc,
38122 "%<#pragma acc routine%> already applied to %qD", fndecl);
38123 parser->oacc_routine = NULL;
38124 return;
38127 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38129 error_at (parser->oacc_routine->loc,
38130 TREE_USED (fndecl)
38131 ? G_("%<#pragma acc routine%> must be applied before use")
38132 : G_("%<#pragma acc routine%> must be applied before "
38133 "definition"));
38134 parser->oacc_routine = NULL;
38135 return;
38138 /* Process the routine's dimension clauses. */
38139 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38140 oacc_replace_fn_attrib (fndecl, dims);
38142 /* Add an "omp declare target" attribute. */
38143 DECL_ATTRIBUTES (fndecl)
38144 = tree_cons (get_identifier ("omp declare target"),
38145 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38147 /* Don't unset parser->oacc_routine here: we may still need it to
38148 diagnose wrong usage. But, remember that we've used this "#pragma acc
38149 routine". */
38150 parser->oacc_routine->fndecl_seen = true;
38154 /* Main entry point to OpenMP statement pragmas. */
38156 static void
38157 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38159 tree stmt;
38160 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38161 omp_clause_mask mask (0);
38163 switch (cp_parser_pragma_kind (pragma_tok))
38165 case PRAGMA_OACC_ATOMIC:
38166 cp_parser_omp_atomic (parser, pragma_tok);
38167 return;
38168 case PRAGMA_OACC_CACHE:
38169 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38170 break;
38171 case PRAGMA_OACC_DATA:
38172 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38173 break;
38174 case PRAGMA_OACC_ENTER_DATA:
38175 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38176 break;
38177 case PRAGMA_OACC_EXIT_DATA:
38178 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38179 break;
38180 case PRAGMA_OACC_HOST_DATA:
38181 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38182 break;
38183 case PRAGMA_OACC_KERNELS:
38184 case PRAGMA_OACC_PARALLEL:
38185 strcpy (p_name, "#pragma acc");
38186 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38187 if_p);
38188 break;
38189 case PRAGMA_OACC_LOOP:
38190 strcpy (p_name, "#pragma acc");
38191 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38192 if_p);
38193 break;
38194 case PRAGMA_OACC_UPDATE:
38195 stmt = cp_parser_oacc_update (parser, pragma_tok);
38196 break;
38197 case PRAGMA_OACC_WAIT:
38198 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38199 break;
38200 case PRAGMA_OMP_ATOMIC:
38201 cp_parser_omp_atomic (parser, pragma_tok);
38202 return;
38203 case PRAGMA_OMP_CRITICAL:
38204 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38205 break;
38206 case PRAGMA_OMP_DISTRIBUTE:
38207 strcpy (p_name, "#pragma omp");
38208 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38209 if_p);
38210 break;
38211 case PRAGMA_OMP_FOR:
38212 strcpy (p_name, "#pragma omp");
38213 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38214 if_p);
38215 break;
38216 case PRAGMA_OMP_MASTER:
38217 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38218 break;
38219 case PRAGMA_OMP_PARALLEL:
38220 strcpy (p_name, "#pragma omp");
38221 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38222 if_p);
38223 break;
38224 case PRAGMA_OMP_SECTIONS:
38225 strcpy (p_name, "#pragma omp");
38226 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38227 break;
38228 case PRAGMA_OMP_SIMD:
38229 strcpy (p_name, "#pragma omp");
38230 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38231 if_p);
38232 break;
38233 case PRAGMA_OMP_SINGLE:
38234 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38235 break;
38236 case PRAGMA_OMP_TASK:
38237 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38238 break;
38239 case PRAGMA_OMP_TASKGROUP:
38240 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38241 break;
38242 case PRAGMA_OMP_TASKLOOP:
38243 strcpy (p_name, "#pragma omp");
38244 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38245 if_p);
38246 break;
38247 case PRAGMA_OMP_TEAMS:
38248 strcpy (p_name, "#pragma omp");
38249 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38250 if_p);
38251 break;
38252 default:
38253 gcc_unreachable ();
38256 protected_set_expr_location (stmt, pragma_tok->location);
38259 /* Transactional Memory parsing routines. */
38261 /* Parse a transaction attribute.
38263 txn-attribute:
38264 attribute
38265 [ [ identifier ] ]
38267 We use this instead of cp_parser_attributes_opt for transactions to avoid
38268 the pedwarn in C++98 mode. */
38270 static tree
38271 cp_parser_txn_attribute_opt (cp_parser *parser)
38273 cp_token *token;
38274 tree attr_name, attr = NULL;
38276 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38277 return cp_parser_attributes_opt (parser);
38279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38280 return NULL_TREE;
38281 cp_lexer_consume_token (parser->lexer);
38282 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38283 goto error1;
38285 token = cp_lexer_peek_token (parser->lexer);
38286 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38288 token = cp_lexer_consume_token (parser->lexer);
38290 attr_name = (token->type == CPP_KEYWORD
38291 /* For keywords, use the canonical spelling,
38292 not the parsed identifier. */
38293 ? ridpointers[(int) token->keyword]
38294 : token->u.value);
38295 attr = build_tree_list (attr_name, NULL_TREE);
38297 else
38298 cp_parser_error (parser, "expected identifier");
38300 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38301 error1:
38302 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38303 return attr;
38306 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38308 transaction-statement:
38309 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38310 compound-statement
38311 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38314 static tree
38315 cp_parser_transaction (cp_parser *parser, cp_token *token)
38317 unsigned char old_in = parser->in_transaction;
38318 unsigned char this_in = 1, new_in;
38319 enum rid keyword = token->keyword;
38320 tree stmt, attrs, noex;
38322 cp_lexer_consume_token (parser->lexer);
38324 if (keyword == RID_TRANSACTION_RELAXED
38325 || keyword == RID_SYNCHRONIZED)
38326 this_in |= TM_STMT_ATTR_RELAXED;
38327 else
38329 attrs = cp_parser_txn_attribute_opt (parser);
38330 if (attrs)
38331 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38334 /* Parse a noexcept specification. */
38335 if (keyword == RID_ATOMIC_NOEXCEPT)
38336 noex = boolean_true_node;
38337 else if (keyword == RID_ATOMIC_CANCEL)
38339 /* cancel-and-throw is unimplemented. */
38340 sorry ("atomic_cancel");
38341 noex = NULL_TREE;
38343 else
38344 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38346 /* Keep track if we're in the lexical scope of an outer transaction. */
38347 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38349 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38351 parser->in_transaction = new_in;
38352 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38353 parser->in_transaction = old_in;
38355 finish_transaction_stmt (stmt, NULL, this_in, noex);
38357 return stmt;
38360 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38362 transaction-expression:
38363 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38364 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38367 static tree
38368 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38370 unsigned char old_in = parser->in_transaction;
38371 unsigned char this_in = 1;
38372 cp_token *token;
38373 tree expr, noex;
38374 bool noex_expr;
38375 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38377 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38378 || keyword == RID_TRANSACTION_RELAXED);
38380 if (!flag_tm)
38381 error_at (loc,
38382 keyword == RID_TRANSACTION_RELAXED
38383 ? G_("%<__transaction_relaxed%> without transactional memory "
38384 "support enabled")
38385 : G_("%<__transaction_atomic%> without transactional memory "
38386 "support enabled"));
38388 token = cp_parser_require_keyword (parser, keyword,
38389 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38390 : RT_TRANSACTION_RELAXED));
38391 gcc_assert (token != NULL);
38393 if (keyword == RID_TRANSACTION_RELAXED)
38394 this_in |= TM_STMT_ATTR_RELAXED;
38396 /* Set this early. This might mean that we allow transaction_cancel in
38397 an expression that we find out later actually has to be a constexpr.
38398 However, we expect that cxx_constant_value will be able to deal with
38399 this; also, if the noexcept has no constexpr, then what we parse next
38400 really is a transaction's body. */
38401 parser->in_transaction = this_in;
38403 /* Parse a noexcept specification. */
38404 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38405 true);
38407 if (!noex || !noex_expr
38408 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38410 matching_parens parens;
38411 parens.require_open (parser);
38413 expr = cp_parser_expression (parser);
38414 expr = finish_parenthesized_expr (expr);
38416 parens.require_close (parser);
38418 else
38420 /* The only expression that is available got parsed for the noexcept
38421 already. noexcept is true then. */
38422 expr = noex;
38423 noex = boolean_true_node;
38426 expr = build_transaction_expr (token->location, expr, this_in, noex);
38427 parser->in_transaction = old_in;
38429 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38430 return error_mark_node;
38432 return (flag_tm ? expr : error_mark_node);
38435 /* Parse a function-transaction-block.
38437 function-transaction-block:
38438 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38439 function-body
38440 __transaction_atomic txn-attribute[opt] function-try-block
38441 __transaction_relaxed ctor-initializer[opt] function-body
38442 __transaction_relaxed function-try-block
38445 static void
38446 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38448 unsigned char old_in = parser->in_transaction;
38449 unsigned char new_in = 1;
38450 tree compound_stmt, stmt, attrs;
38451 cp_token *token;
38453 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38454 || keyword == RID_TRANSACTION_RELAXED);
38455 token = cp_parser_require_keyword (parser, keyword,
38456 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38457 : RT_TRANSACTION_RELAXED));
38458 gcc_assert (token != NULL);
38460 if (keyword == RID_TRANSACTION_RELAXED)
38461 new_in |= TM_STMT_ATTR_RELAXED;
38462 else
38464 attrs = cp_parser_txn_attribute_opt (parser);
38465 if (attrs)
38466 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38469 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38471 parser->in_transaction = new_in;
38473 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38474 cp_parser_function_try_block (parser);
38475 else
38476 cp_parser_ctor_initializer_opt_and_function_body
38477 (parser, /*in_function_try_block=*/false);
38479 parser->in_transaction = old_in;
38481 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38484 /* Parse a __transaction_cancel statement.
38486 cancel-statement:
38487 __transaction_cancel txn-attribute[opt] ;
38488 __transaction_cancel txn-attribute[opt] throw-expression ;
38490 ??? Cancel and throw is not yet implemented. */
38492 static tree
38493 cp_parser_transaction_cancel (cp_parser *parser)
38495 cp_token *token;
38496 bool is_outer = false;
38497 tree stmt, attrs;
38499 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38500 RT_TRANSACTION_CANCEL);
38501 gcc_assert (token != NULL);
38503 attrs = cp_parser_txn_attribute_opt (parser);
38504 if (attrs)
38505 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38507 /* ??? Parse cancel-and-throw here. */
38509 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38511 if (!flag_tm)
38513 error_at (token->location, "%<__transaction_cancel%> without "
38514 "transactional memory support enabled");
38515 return error_mark_node;
38517 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38519 error_at (token->location, "%<__transaction_cancel%> within a "
38520 "%<__transaction_relaxed%>");
38521 return error_mark_node;
38523 else if (is_outer)
38525 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38526 && !is_tm_may_cancel_outer (current_function_decl))
38528 error_at (token->location, "outer %<__transaction_cancel%> not "
38529 "within outer %<__transaction_atomic%>");
38530 error_at (token->location,
38531 " or a %<transaction_may_cancel_outer%> function");
38532 return error_mark_node;
38535 else if (parser->in_transaction == 0)
38537 error_at (token->location, "%<__transaction_cancel%> not within "
38538 "%<__transaction_atomic%>");
38539 return error_mark_node;
38542 stmt = build_tm_abort_call (token->location, is_outer);
38543 add_stmt (stmt);
38545 return stmt;
38548 /* The parser. */
38550 static GTY (()) cp_parser *the_parser;
38553 /* Special handling for the first token or line in the file. The first
38554 thing in the file might be #pragma GCC pch_preprocess, which loads a
38555 PCH file, which is a GC collection point. So we need to handle this
38556 first pragma without benefit of an existing lexer structure.
38558 Always returns one token to the caller in *FIRST_TOKEN. This is
38559 either the true first token of the file, or the first token after
38560 the initial pragma. */
38562 static void
38563 cp_parser_initial_pragma (cp_token *first_token)
38565 tree name = NULL;
38567 cp_lexer_get_preprocessor_token (NULL, first_token);
38568 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38569 return;
38571 cp_lexer_get_preprocessor_token (NULL, first_token);
38572 if (first_token->type == CPP_STRING)
38574 name = first_token->u.value;
38576 cp_lexer_get_preprocessor_token (NULL, first_token);
38577 if (first_token->type != CPP_PRAGMA_EOL)
38578 error_at (first_token->location,
38579 "junk at end of %<#pragma GCC pch_preprocess%>");
38581 else
38582 error_at (first_token->location, "expected string literal");
38584 /* Skip to the end of the pragma. */
38585 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38586 cp_lexer_get_preprocessor_token (NULL, first_token);
38588 /* Now actually load the PCH file. */
38589 if (name)
38590 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38592 /* Read one more token to return to our caller. We have to do this
38593 after reading the PCH file in, since its pointers have to be
38594 live. */
38595 cp_lexer_get_preprocessor_token (NULL, first_token);
38598 /* Parse a pragma GCC ivdep. */
38600 static bool
38601 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38603 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38604 return true;
38607 /* Parse a pragma GCC unroll. */
38609 static unsigned short
38610 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38612 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38613 tree expr = cp_parser_constant_expression (parser);
38614 unsigned short unroll;
38615 expr = maybe_constant_value (expr);
38616 HOST_WIDE_INT lunroll = 0;
38617 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38618 || TREE_CODE (expr) != INTEGER_CST
38619 || (lunroll = tree_to_shwi (expr)) < 0
38620 || lunroll >= USHRT_MAX)
38622 error_at (location, "%<#pragma GCC unroll%> requires an"
38623 " assignment-expression that evaluates to a non-negative"
38624 " integral constant less than %u", USHRT_MAX);
38625 unroll = 0;
38627 else
38629 unroll = (unsigned short)lunroll;
38630 if (unroll == 0)
38631 unroll = 1;
38633 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38634 return unroll;
38637 /* Normal parsing of a pragma token. Here we can (and must) use the
38638 regular lexer. */
38640 static bool
38641 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38643 cp_token *pragma_tok;
38644 unsigned int id;
38645 tree stmt;
38646 bool ret;
38648 pragma_tok = cp_lexer_consume_token (parser->lexer);
38649 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38650 parser->lexer->in_pragma = true;
38652 id = cp_parser_pragma_kind (pragma_tok);
38653 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38654 cp_ensure_no_omp_declare_simd (parser);
38655 switch (id)
38657 case PRAGMA_GCC_PCH_PREPROCESS:
38658 error_at (pragma_tok->location,
38659 "%<#pragma GCC pch_preprocess%> must be first");
38660 break;
38662 case PRAGMA_OMP_BARRIER:
38663 switch (context)
38665 case pragma_compound:
38666 cp_parser_omp_barrier (parser, pragma_tok);
38667 return false;
38668 case pragma_stmt:
38669 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38670 "used in compound statements", "omp barrier");
38671 break;
38672 default:
38673 goto bad_stmt;
38675 break;
38677 case PRAGMA_OMP_FLUSH:
38678 switch (context)
38680 case pragma_compound:
38681 cp_parser_omp_flush (parser, pragma_tok);
38682 return false;
38683 case pragma_stmt:
38684 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38685 "used in compound statements", "omp flush");
38686 break;
38687 default:
38688 goto bad_stmt;
38690 break;
38692 case PRAGMA_OMP_TASKWAIT:
38693 switch (context)
38695 case pragma_compound:
38696 cp_parser_omp_taskwait (parser, pragma_tok);
38697 return false;
38698 case pragma_stmt:
38699 error_at (pragma_tok->location,
38700 "%<#pragma %s%> may only be used in compound statements",
38701 "omp taskwait");
38702 break;
38703 default:
38704 goto bad_stmt;
38706 break;
38708 case PRAGMA_OMP_TASKYIELD:
38709 switch (context)
38711 case pragma_compound:
38712 cp_parser_omp_taskyield (parser, pragma_tok);
38713 return false;
38714 case pragma_stmt:
38715 error_at (pragma_tok->location,
38716 "%<#pragma %s%> may only be used in compound statements",
38717 "omp taskyield");
38718 break;
38719 default:
38720 goto bad_stmt;
38722 break;
38724 case PRAGMA_OMP_CANCEL:
38725 switch (context)
38727 case pragma_compound:
38728 cp_parser_omp_cancel (parser, pragma_tok);
38729 return false;
38730 case pragma_stmt:
38731 error_at (pragma_tok->location,
38732 "%<#pragma %s%> may only be used in compound statements",
38733 "omp cancel");
38734 break;
38735 default:
38736 goto bad_stmt;
38738 break;
38740 case PRAGMA_OMP_CANCELLATION_POINT:
38741 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38742 return false;
38744 case PRAGMA_OMP_THREADPRIVATE:
38745 cp_parser_omp_threadprivate (parser, pragma_tok);
38746 return false;
38748 case PRAGMA_OMP_DECLARE:
38749 return cp_parser_omp_declare (parser, pragma_tok, context);
38751 case PRAGMA_OACC_DECLARE:
38752 cp_parser_oacc_declare (parser, pragma_tok);
38753 return false;
38755 case PRAGMA_OACC_ENTER_DATA:
38756 if (context == pragma_stmt)
38758 error_at (pragma_tok->location,
38759 "%<#pragma %s%> may only be used in compound statements",
38760 "acc enter data");
38761 break;
38763 else if (context != pragma_compound)
38764 goto bad_stmt;
38765 cp_parser_omp_construct (parser, pragma_tok, if_p);
38766 return true;
38768 case PRAGMA_OACC_EXIT_DATA:
38769 if (context == pragma_stmt)
38771 error_at (pragma_tok->location,
38772 "%<#pragma %s%> may only be used in compound statements",
38773 "acc exit data");
38774 break;
38776 else if (context != pragma_compound)
38777 goto bad_stmt;
38778 cp_parser_omp_construct (parser, pragma_tok, if_p);
38779 return true;
38781 case PRAGMA_OACC_ROUTINE:
38782 if (context != pragma_external)
38784 error_at (pragma_tok->location,
38785 "%<#pragma acc routine%> must be at file scope");
38786 break;
38788 cp_parser_oacc_routine (parser, pragma_tok, context);
38789 return false;
38791 case PRAGMA_OACC_UPDATE:
38792 if (context == pragma_stmt)
38794 error_at (pragma_tok->location,
38795 "%<#pragma %s%> may only be used in compound statements",
38796 "acc update");
38797 break;
38799 else if (context != pragma_compound)
38800 goto bad_stmt;
38801 cp_parser_omp_construct (parser, pragma_tok, if_p);
38802 return true;
38804 case PRAGMA_OACC_WAIT:
38805 if (context == pragma_stmt)
38807 error_at (pragma_tok->location,
38808 "%<#pragma %s%> may only be used in compound statements",
38809 "acc wait");
38810 break;
38812 else if (context != pragma_compound)
38813 goto bad_stmt;
38814 cp_parser_omp_construct (parser, pragma_tok, if_p);
38815 return true;
38817 case PRAGMA_OACC_ATOMIC:
38818 case PRAGMA_OACC_CACHE:
38819 case PRAGMA_OACC_DATA:
38820 case PRAGMA_OACC_HOST_DATA:
38821 case PRAGMA_OACC_KERNELS:
38822 case PRAGMA_OACC_PARALLEL:
38823 case PRAGMA_OACC_LOOP:
38824 case PRAGMA_OMP_ATOMIC:
38825 case PRAGMA_OMP_CRITICAL:
38826 case PRAGMA_OMP_DISTRIBUTE:
38827 case PRAGMA_OMP_FOR:
38828 case PRAGMA_OMP_MASTER:
38829 case PRAGMA_OMP_PARALLEL:
38830 case PRAGMA_OMP_SECTIONS:
38831 case PRAGMA_OMP_SIMD:
38832 case PRAGMA_OMP_SINGLE:
38833 case PRAGMA_OMP_TASK:
38834 case PRAGMA_OMP_TASKGROUP:
38835 case PRAGMA_OMP_TASKLOOP:
38836 case PRAGMA_OMP_TEAMS:
38837 if (context != pragma_stmt && context != pragma_compound)
38838 goto bad_stmt;
38839 stmt = push_omp_privatization_clauses (false);
38840 cp_parser_omp_construct (parser, pragma_tok, if_p);
38841 pop_omp_privatization_clauses (stmt);
38842 return true;
38844 case PRAGMA_OMP_ORDERED:
38845 if (context != pragma_stmt && context != pragma_compound)
38846 goto bad_stmt;
38847 stmt = push_omp_privatization_clauses (false);
38848 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38849 pop_omp_privatization_clauses (stmt);
38850 return ret;
38852 case PRAGMA_OMP_TARGET:
38853 if (context != pragma_stmt && context != pragma_compound)
38854 goto bad_stmt;
38855 stmt = push_omp_privatization_clauses (false);
38856 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38857 pop_omp_privatization_clauses (stmt);
38858 return ret;
38860 case PRAGMA_OMP_END_DECLARE_TARGET:
38861 cp_parser_omp_end_declare_target (parser, pragma_tok);
38862 return false;
38864 case PRAGMA_OMP_SECTION:
38865 error_at (pragma_tok->location,
38866 "%<#pragma omp section%> may only be used in "
38867 "%<#pragma omp sections%> construct");
38868 break;
38870 case PRAGMA_IVDEP:
38872 if (context == pragma_external)
38874 error_at (pragma_tok->location,
38875 "%<#pragma GCC ivdep%> must be inside a function");
38876 break;
38878 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38879 unsigned short unroll;
38880 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38881 if (tok->type == CPP_PRAGMA
38882 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38884 tok = cp_lexer_consume_token (parser->lexer);
38885 unroll = cp_parser_pragma_unroll (parser, tok);
38886 tok = cp_lexer_peek_token (the_parser->lexer);
38888 else
38889 unroll = 0;
38890 if (tok->type != CPP_KEYWORD
38891 || (tok->keyword != RID_FOR
38892 && tok->keyword != RID_WHILE
38893 && tok->keyword != RID_DO))
38895 cp_parser_error (parser, "for, while or do statement expected");
38896 return false;
38898 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38899 return true;
38902 case PRAGMA_UNROLL:
38904 if (context == pragma_external)
38906 error_at (pragma_tok->location,
38907 "%<#pragma GCC unroll%> must be inside a function");
38908 break;
38910 const unsigned short unroll
38911 = cp_parser_pragma_unroll (parser, pragma_tok);
38912 bool ivdep;
38913 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38914 if (tok->type == CPP_PRAGMA
38915 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38917 tok = cp_lexer_consume_token (parser->lexer);
38918 ivdep = cp_parser_pragma_ivdep (parser, tok);
38919 tok = cp_lexer_peek_token (the_parser->lexer);
38921 else
38922 ivdep = false;
38923 if (tok->type != CPP_KEYWORD
38924 || (tok->keyword != RID_FOR
38925 && tok->keyword != RID_WHILE
38926 && tok->keyword != RID_DO))
38928 cp_parser_error (parser, "for, while or do statement expected");
38929 return false;
38931 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38932 return true;
38935 default:
38936 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38937 c_invoke_pragma_handler (id);
38938 break;
38940 bad_stmt:
38941 cp_parser_error (parser, "expected declaration specifiers");
38942 break;
38945 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38946 return false;
38949 /* The interface the pragma parsers have to the lexer. */
38951 enum cpp_ttype
38952 pragma_lex (tree *value, location_t *loc)
38954 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38955 enum cpp_ttype ret = tok->type;
38957 *value = tok->u.value;
38958 if (loc)
38959 *loc = tok->location;
38961 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38962 ret = CPP_EOF;
38963 else if (ret == CPP_STRING)
38964 *value = cp_parser_string_literal (the_parser, false, false);
38965 else
38967 if (ret == CPP_KEYWORD)
38968 ret = CPP_NAME;
38969 cp_lexer_consume_token (the_parser->lexer);
38972 return ret;
38976 /* External interface. */
38978 /* Parse one entire translation unit. */
38980 void
38981 c_parse_file (void)
38983 static bool already_called = false;
38985 if (already_called)
38986 fatal_error (input_location,
38987 "inter-module optimizations not implemented for C++");
38988 already_called = true;
38990 the_parser = cp_parser_new ();
38991 push_deferring_access_checks (flag_access_control
38992 ? dk_no_deferred : dk_no_check);
38993 cp_parser_translation_unit (the_parser);
38994 the_parser = NULL;
38997 /* Create an identifier for a generic parameter type (a synthesized
38998 template parameter implied by `auto' or a concept identifier). */
39000 static GTY(()) int generic_parm_count;
39001 static tree
39002 make_generic_type_name ()
39004 char buf[32];
39005 sprintf (buf, "auto:%d", ++generic_parm_count);
39006 return get_identifier (buf);
39009 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39010 (creating a new template parameter list if necessary). Returns the newly
39011 created template type parm. */
39013 static tree
39014 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39016 gcc_assert (current_binding_level->kind == sk_function_parms);
39018 /* Before committing to modifying any scope, if we're in an
39019 implicit template scope, and we're trying to synthesize a
39020 constrained parameter, try to find a previous parameter with
39021 the same name. This is the same-type rule for abbreviated
39022 function templates.
39024 NOTE: We can generate implicit parameters when tentatively
39025 parsing a nested name specifier, only to reject that parse
39026 later. However, matching the same template-id as part of a
39027 direct-declarator should generate an identical template
39028 parameter, so this rule will merge them. */
39029 if (parser->implicit_template_scope && constr)
39031 tree t = parser->implicit_template_parms;
39032 while (t)
39034 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39036 tree d = TREE_VALUE (t);
39037 if (TREE_CODE (d) == PARM_DECL)
39038 /* Return the TEMPLATE_PARM_INDEX. */
39039 d = DECL_INITIAL (d);
39040 return d;
39042 t = TREE_CHAIN (t);
39046 /* We are either continuing a function template that already contains implicit
39047 template parameters, creating a new fully-implicit function template, or
39048 extending an existing explicit function template with implicit template
39049 parameters. */
39051 cp_binding_level *const entry_scope = current_binding_level;
39053 bool become_template = false;
39054 cp_binding_level *parent_scope = 0;
39056 if (parser->implicit_template_scope)
39058 gcc_assert (parser->implicit_template_parms);
39060 current_binding_level = parser->implicit_template_scope;
39062 else
39064 /* Roll back to the existing template parameter scope (in the case of
39065 extending an explicit function template) or introduce a new template
39066 parameter scope ahead of the function parameter scope (or class scope
39067 in the case of out-of-line member definitions). The function scope is
39068 added back after template parameter synthesis below. */
39070 cp_binding_level *scope = entry_scope;
39072 while (scope->kind == sk_function_parms)
39074 parent_scope = scope;
39075 scope = scope->level_chain;
39077 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39079 /* If not defining a class, then any class scope is a scope level in
39080 an out-of-line member definition. In this case simply wind back
39081 beyond the first such scope to inject the template parameter list.
39082 Otherwise wind back to the class being defined. The latter can
39083 occur in class member friend declarations such as:
39085 class A {
39086 void foo (auto);
39088 class B {
39089 friend void A::foo (auto);
39092 The template parameter list synthesized for the friend declaration
39093 must be injected in the scope of 'B'. This can also occur in
39094 erroneous cases such as:
39096 struct A {
39097 struct B {
39098 void foo (auto);
39100 void B::foo (auto) {}
39103 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39104 but, nevertheless, the template parameter list synthesized for the
39105 declarator should be injected into the scope of 'A' as if the
39106 ill-formed template was specified explicitly. */
39108 while (scope->kind == sk_class && !scope->defining_class_p)
39110 parent_scope = scope;
39111 scope = scope->level_chain;
39115 current_binding_level = scope;
39117 if (scope->kind != sk_template_parms
39118 || !function_being_declared_is_template_p (parser))
39120 /* Introduce a new template parameter list for implicit template
39121 parameters. */
39123 become_template = true;
39125 parser->implicit_template_scope
39126 = begin_scope (sk_template_parms, NULL);
39128 ++processing_template_decl;
39130 parser->fully_implicit_function_template_p = true;
39131 ++parser->num_template_parameter_lists;
39133 else
39135 /* Synthesize implicit template parameters at the end of the explicit
39136 template parameter list. */
39138 gcc_assert (current_template_parms);
39140 parser->implicit_template_scope = scope;
39142 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39143 parser->implicit_template_parms
39144 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39148 /* Synthesize a new template parameter and track the current template
39149 parameter chain with implicit_template_parms. */
39151 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39152 tree synth_id = make_generic_type_name ();
39153 tree synth_tmpl_parm;
39154 bool non_type = false;
39156 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39157 synth_tmpl_parm
39158 = finish_template_type_parm (class_type_node, synth_id);
39159 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39160 synth_tmpl_parm
39161 = finish_constrained_template_template_parm (proto, synth_id);
39162 else
39164 synth_tmpl_parm = copy_decl (proto);
39165 DECL_NAME (synth_tmpl_parm) = synth_id;
39166 non_type = true;
39169 // Attach the constraint to the parm before processing.
39170 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39171 TREE_TYPE (node) = constr;
39172 tree new_parm
39173 = process_template_parm (parser->implicit_template_parms,
39174 input_location,
39175 node,
39176 /*non_type=*/non_type,
39177 /*param_pack=*/false);
39179 // Chain the new parameter to the list of implicit parameters.
39180 if (parser->implicit_template_parms)
39181 parser->implicit_template_parms
39182 = TREE_CHAIN (parser->implicit_template_parms);
39183 else
39184 parser->implicit_template_parms = new_parm;
39186 tree new_decl = get_local_decls ();
39187 if (non_type)
39188 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39189 new_decl = DECL_INITIAL (new_decl);
39191 /* If creating a fully implicit function template, start the new implicit
39192 template parameter list with this synthesized type, otherwise grow the
39193 current template parameter list. */
39195 if (become_template)
39197 parent_scope->level_chain = current_binding_level;
39199 tree new_parms = make_tree_vec (1);
39200 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39201 current_template_parms = tree_cons (size_int (processing_template_decl),
39202 new_parms, current_template_parms);
39204 else
39206 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39207 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39208 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39209 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39212 // If the new parameter was constrained, we need to add that to the
39213 // constraints in the template parameter list.
39214 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39216 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39217 reqs = conjoin_constraints (reqs, req);
39218 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39221 current_binding_level = entry_scope;
39223 return new_decl;
39226 /* Finish the declaration of a fully implicit function template. Such a
39227 template has no explicit template parameter list so has not been through the
39228 normal template head and tail processing. synthesize_implicit_template_parm
39229 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39230 provided if the declaration is a class member such that its template
39231 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39232 form is returned. Otherwise NULL_TREE is returned. */
39234 static tree
39235 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39237 gcc_assert (parser->fully_implicit_function_template_p);
39239 if (member_decl_opt && member_decl_opt != error_mark_node
39240 && DECL_VIRTUAL_P (member_decl_opt))
39242 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39243 "implicit templates may not be %<virtual%>");
39244 DECL_VIRTUAL_P (member_decl_opt) = false;
39247 if (member_decl_opt)
39248 member_decl_opt = finish_member_template_decl (member_decl_opt);
39249 end_template_decl ();
39251 parser->fully_implicit_function_template_p = false;
39252 --parser->num_template_parameter_lists;
39254 return member_decl_opt;
39257 /* Helper function for diagnostics that have complained about things
39258 being used with 'extern "C"' linkage.
39260 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39262 void
39263 maybe_show_extern_c_location (void)
39265 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39266 inform (the_parser->innermost_linkage_specification_location,
39267 "%<extern \"C\"%> linkage started here");
39270 #include "gt-cp-parser.h"