PR target/85328
[official-gcc.git] / gcc / cp / parser.c
blob8b1b271b53d17205e547299bdc4074fb590354ef
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);
2267 static void abort_fully_implicit_template
2268 (cp_parser *);
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275 (cp_parser *);
2276 static tree cp_parser_class_head
2277 (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279 (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281 (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283 (cp_parser *);
2284 static void cp_parser_member_declaration
2285 (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287 (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289 (cp_parser *);
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2294 (cp_parser *);
2295 static tree cp_parser_base_specifier
2296 (cp_parser *);
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2301 (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303 (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305 (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307 (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313 (cp_parser *);
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2318 (cp_parser *);
2319 static cp_expr cp_parser_operator
2320 (cp_parser *);
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327 (cp_parser *);
2328 static tree cp_parser_template_parameter
2329 (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337 (cp_parser *);
2338 static tree cp_parser_template_argument
2339 (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341 (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343 (cp_parser *);
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2348 (cp_parser *);
2349 static void cp_parser_function_try_block
2350 (cp_parser *);
2351 static void cp_parser_handler_seq
2352 (cp_parser *);
2353 static void cp_parser_handler
2354 (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356 (cp_parser *);
2357 static tree cp_parser_throw_expression
2358 (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_type_id_list
2362 (cp_parser *);
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379 (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391 (cp_parser *);
2392 static tree cp_parser_std_attribute
2393 (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397 (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser *, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static void cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool, location_t);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646 (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648 (cp_parser *);
2650 /* Concept-related syntactic transformations */
2652 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand;
2662 ++c_inhibit_evaluation_warnings;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings;
2668 --cp_unevaluated_operand;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2679 return parser->context->next != NULL;
2682 /* Returns nonzero if TOKEN is a string literal. */
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2687 return (token->type == CPP_STRING ||
2688 token->type == CPP_STRING16 ||
2689 token->type == CPP_STRING32 ||
2690 token->type == CPP_WSTRING ||
2691 token->type == CPP_UTF8STRING);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2700 return (cp_parser_is_pure_string_literal (token) ||
2701 token->type == CPP_STRING_USERDEF ||
2702 token->type == CPP_STRING16_USERDEF ||
2703 token->type == CPP_STRING32_USERDEF ||
2704 token->type == CPP_WSTRING_USERDEF ||
2705 token->type == CPP_UTF8STRING_USERDEF);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2713 return token->keyword == keyword;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 PRAGMA_NONE. */
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2722 if (token->type != CPP_PRAGMA)
2723 return PRAGMA_NONE;
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2737 of the marker. */
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741 location_t *out_loc)
2743 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744 if (token2->type != tok1_kind)
2745 return false;
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2748 return false;
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751 return false;
2753 /* It must be at the start of the line. */
2754 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755 if (LOCATION_COLUMN (start_loc) != 1)
2756 return false;
2758 /* We have a conflict marker. Construct a location of the form:
2759 <<<<<<<
2760 ^~~~~~~
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc = get_finish (token4->location);
2763 *out_loc = make_location (start_loc, start_loc, finish_loc);
2765 return true;
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769 RT_CLOSE_PAREN. */
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2774 switch (token_desc)
2776 default:
2777 gcc_unreachable ();
2778 return "";
2779 case RT_CLOSE_BRACE:
2780 return "{";
2781 case RT_CLOSE_PAREN:
2782 return "(";
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2792 switch (token_desc)
2794 case RT_SEMICOLON:
2795 return CPP_SEMICOLON;
2796 case RT_OPEN_PAREN:
2797 return CPP_OPEN_PAREN;
2798 case RT_CLOSE_BRACE:
2799 return CPP_CLOSE_BRACE;
2800 case RT_OPEN_BRACE:
2801 return CPP_OPEN_BRACE;
2802 case RT_CLOSE_SQUARE:
2803 return CPP_CLOSE_SQUARE;
2804 case RT_OPEN_SQUARE:
2805 return CPP_OPEN_SQUARE;
2806 case RT_COMMA:
2807 return CPP_COMMA;
2808 case RT_COLON:
2809 return CPP_COLON;
2810 case RT_CLOSE_PAREN:
2811 return CPP_CLOSE_PAREN;
2813 default:
2814 /* Use CPP_EOF as a "no completions possible" code. */
2815 return CPP_EOF;
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2826 OTHER-TOKEN".
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836 location. */
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840 required_token missing_token_desc,
2841 location_t matching_location)
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token);
2848 if (token->type == CPP_PRAGMA)
2850 error_at (token->location,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser, token);
2853 return;
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token->type == CPP_LSHIFT
2858 || token->type == CPP_RSHIFT
2859 || token->type == CPP_EQ_EQ)
2861 location_t loc;
2862 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2864 error_at (loc, "version control conflict marker in file");
2865 return;
2869 gcc_rich_location richloc (input_location);
2871 bool added_matching_location = false;
2873 if (missing_token_desc != RT_NONE)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser->lexer)->location;
2881 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location != UNKNOWN_LOCATION)
2887 added_matching_location
2888 = richloc.add_location_if_nearby (matching_location);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2895 identifiers. */
2896 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2897 token->u.value, token->flags, &richloc);
2899 if (missing_token_desc != RT_NONE)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location != UNKNOWN_LOCATION
2904 && !added_matching_location)
2905 inform (matching_location, "to match this %qs",
2906 get_matching_symbol (missing_token_desc));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2914 OTHER-TOKEN". */
2916 static void
2917 cp_parser_error (cp_parser* parser, const char* gmsgid)
2919 if (!cp_parser_simulate_error (parser))
2920 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2928 static void
2929 cp_parser_name_lookup_error (cp_parser* parser,
2930 tree name,
2931 tree decl,
2932 name_lookup_error desired,
2933 location_t location)
2935 /* If name lookup completely failed, tell the user that NAME was not
2936 declared. */
2937 if (decl == error_mark_node)
2939 if (parser->scope && parser->scope != global_namespace)
2940 error_at (location, "%<%E::%E%> has not been declared",
2941 parser->scope, name);
2942 else if (parser->scope == global_namespace)
2943 error_at (location, "%<::%E%> has not been declared", name);
2944 else if (parser->object_scope
2945 && !CLASS_TYPE_P (parser->object_scope))
2946 error_at (location, "request for member %qE in non-class type %qT",
2947 name, parser->object_scope);
2948 else if (parser->object_scope)
2949 error_at (location, "%<%T::%E%> has not been declared",
2950 parser->object_scope, name);
2951 else
2952 error_at (location, "%qE has not been declared", name);
2954 else if (parser->scope && parser->scope != global_namespace)
2956 switch (desired)
2958 case NLE_TYPE:
2959 error_at (location, "%<%E::%E%> is not a type",
2960 parser->scope, name);
2961 break;
2962 case NLE_CXX98:
2963 error_at (location, "%<%E::%E%> is not a class or namespace",
2964 parser->scope, name);
2965 break;
2966 case NLE_NOT_CXX98:
2967 error_at (location,
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser->scope, name);
2970 break;
2971 default:
2972 gcc_unreachable ();
2976 else if (parser->scope == global_namespace)
2978 switch (desired)
2980 case NLE_TYPE:
2981 error_at (location, "%<::%E%> is not a type", name);
2982 break;
2983 case NLE_CXX98:
2984 error_at (location, "%<::%E%> is not a class or namespace", name);
2985 break;
2986 case NLE_NOT_CXX98:
2987 error_at (location,
2988 "%<::%E%> is not a class, namespace, or enumeration",
2989 name);
2990 break;
2991 default:
2992 gcc_unreachable ();
2995 else
2997 switch (desired)
2999 case NLE_TYPE:
3000 error_at (location, "%qE is not a type", name);
3001 break;
3002 case NLE_CXX98:
3003 error_at (location, "%qE is not a class or namespace", name);
3004 break;
3005 case NLE_NOT_CXX98:
3006 error_at (location,
3007 "%qE is not a class, namespace, or enumeration", name);
3008 break;
3009 default:
3010 gcc_unreachable ();
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3019 static bool
3020 cp_parser_simulate_error (cp_parser* parser)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3024 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3025 return true;
3027 return false;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3032 issued. */
3034 static bool
3035 cp_parser_check_type_definition (cp_parser* parser)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser->type_definition_forbidden_message)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser->type_definition_forbidden_message);
3043 return false;
3045 return true;
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3054 static void
3055 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3056 tree type, location_t type_location)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3061 while (declarator
3062 && (declarator->kind == cdk_pointer
3063 || declarator->kind == cdk_reference
3064 || declarator->kind == cdk_ptrmem))
3065 declarator = declarator->declarator;
3066 if (declarator
3067 && declarator->kind == cdk_function)
3069 error_at (type_location,
3070 "new types may not be defined in a return type");
3071 inform (type_location,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3073 type);
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3083 static void
3084 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3085 tree type,
3086 enum tag_types tag_type,
3087 location_t location)
3089 cp_token_position start = 0;
3091 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3093 if (TREE_CODE (type) == TYPE_DECL)
3094 type = TREE_TYPE (type);
3095 if (TYPE_P (type) && !template_placeholder_p (type))
3096 error_at (location, "%qT is not a template", type);
3097 else if (identifier_p (type))
3099 if (tag_type != none_type)
3100 error_at (location, "%qE is not a class template", type);
3101 else
3102 error_at (location, "%qE is not a template", type);
3104 else
3105 error_at (location, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3108 start = cp_lexer_token_position (parser->lexer, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser->lexer);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3115 if (start)
3116 cp_lexer_purge_tokens_after (parser->lexer, start);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3125 static bool
3126 cp_parser_non_integral_constant_expression (cp_parser *parser,
3127 non_integral_constant thing)
3129 parser->non_integral_constant_expression_p = true;
3130 if (parser->integral_constant_expression_p)
3132 if (!parser->allow_non_integral_constant_expression_p)
3134 const char *msg = NULL;
3135 switch (thing)
3137 case NIC_FLOAT:
3138 pedwarn (input_location, OPT_Wpedantic,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3141 return true;
3142 case NIC_CAST:
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3146 return true;
3147 case NIC_TYPEID:
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3150 return true;
3151 case NIC_NCC:
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3154 return true;
3155 case NIC_FUNC_CALL:
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3158 return true;
3159 case NIC_INC:
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3162 return true;
3163 case NIC_DEC:
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_ARRAY_REF:
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_ADDR_LABEL:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_OVERLOADED:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_ASSIGNMENT:
3180 error ("an assignment cannot appear in a constant-expression");
3181 return true;
3182 case NIC_COMMA:
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_CONSTRUCTOR:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_TRANSACTION:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3193 return true;
3194 case NIC_THIS:
3195 msg = "this";
3196 break;
3197 case NIC_FUNC_NAME:
3198 msg = "__FUNCTION__";
3199 break;
3200 case NIC_PRETTY_FUNC:
3201 msg = "__PRETTY_FUNCTION__";
3202 break;
3203 case NIC_C99_FUNC:
3204 msg = "__func__";
3205 break;
3206 case NIC_VA_ARG:
3207 msg = "va_arg";
3208 break;
3209 case NIC_ARROW:
3210 msg = "->";
3211 break;
3212 case NIC_POINT:
3213 msg = ".";
3214 break;
3215 case NIC_STAR:
3216 msg = "*";
3217 break;
3218 case NIC_ADDR:
3219 msg = "&";
3220 break;
3221 case NIC_PREINCREMENT:
3222 msg = "++";
3223 break;
3224 case NIC_PREDECREMENT:
3225 msg = "--";
3226 break;
3227 case NIC_NEW:
3228 msg = "new";
3229 break;
3230 case NIC_DEL:
3231 msg = "delete";
3232 break;
3233 default:
3234 gcc_unreachable ();
3236 if (msg)
3237 error ("%qs cannot appear in a constant-expression", msg);
3238 return true;
3241 return false;
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3249 static void
3250 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3251 location_t location)
3253 tree decl, ambiguous_decls;
3254 cp_parser_commit_to_tentative_parse (parser);
3255 /* Try to lookup the identifier. */
3256 decl = cp_parser_lookup_name (parser, id, none_type,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls, location);
3261 if (ambiguous_decls)
3262 /* If the lookup was ambiguous, an error will already have
3263 been issued. */
3264 return;
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl))
3269 error_at (location,
3270 "invalid use of template-name %qE without an argument list",
3271 decl);
3272 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3273 inform (location, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3277 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3278 error_at (location, "invalid use of destructor %qD as a type", id);
3279 else if (TREE_CODE (decl) == TYPE_DECL)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location, "invalid combination of multiple type-specifiers");
3282 else if (!parser->scope)
3284 /* Issue an error message. */
3285 name_hint hint;
3286 if (TREE_CODE (id) == IDENTIFIER_NODE)
3287 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3288 if (hint)
3290 gcc_rich_location richloc (location);
3291 richloc.add_fixit_replace (hint.suggestion ());
3292 error_at (&richloc,
3293 "%qE does not name a type; did you mean %qs?",
3294 id, hint.suggestion ());
3296 else
3297 error_at (location, "%qE does not name a type", id);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3306 inform (location, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3309 inform (location, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect < cxx11
3312 && TREE_CODE (id) == IDENTIFIER_NODE
3313 && id_equal (id, "thread_local"))
3314 inform (location, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3317 inform (location, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl && current_class_type
3319 && TYPE_BINFO (current_class_type))
3321 tree b;
3323 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3325 b = TREE_CHAIN (b))
3327 tree base_type = BINFO_TYPE (b);
3328 if (CLASS_TYPE_P (base_type)
3329 && dependent_type_p (base_type))
3331 tree field;
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3336 for (field = TYPE_FIELDS (base_type);
3337 field;
3338 field = DECL_CHAIN (field))
3339 if (TREE_CODE (field) == TYPE_DECL
3340 && DECL_NAME (field) == id)
3342 inform (location,
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b), id);
3345 break;
3347 if (field)
3348 break;
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser->scope != error_mark_node)
3357 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3359 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3360 error_at (location_of (id),
3361 "%qE in namespace %qE does not name a template type",
3362 id, parser->scope);
3363 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3364 error_at (location_of (id),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id, 0), parser->scope);
3367 else
3368 error_at (location_of (id),
3369 "%qE in namespace %qE does not name a type",
3370 id, parser->scope);
3371 if (DECL_P (decl))
3372 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3373 else if (decl == error_mark_node)
3374 suggest_alternative_in_explicit_scope (location, id,
3375 parser->scope);
3377 else if (CLASS_TYPE_P (parser->scope)
3378 && constructor_name_p (id, parser->scope))
3380 /* A<T>::A<T>() */
3381 error_at (location, "%<%T::%E%> names the constructor, not"
3382 " the type", parser->scope, id);
3383 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3384 error_at (location, "and %qT has no template constructors",
3385 parser->scope);
3387 else if (TYPE_P (parser->scope)
3388 && dependent_scope_p (parser->scope))
3390 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3391 error_at (location,
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser->scope),
3395 TYPENAME_TYPE_FULLNAME (parser->scope),
3397 TYPE_CONTEXT (parser->scope),
3398 TYPENAME_TYPE_FULLNAME (parser->scope));
3399 else
3400 error_at (location, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser->scope, id, parser->scope);
3404 else if (TYPE_P (parser->scope))
3406 if (!COMPLETE_TYPE_P (parser->scope))
3407 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3408 parser->scope);
3409 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a template type",
3412 id, parser->scope);
3413 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id, 0), parser->scope);
3417 else
3418 error_at (location_of (id),
3419 "%qE in %q#T does not name a type",
3420 id, parser->scope);
3421 if (DECL_P (decl))
3422 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3424 else
3425 gcc_unreachable ();
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3439 static bool
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3442 tree id;
3443 cp_token *token = cp_lexer_peek_token (parser->lexer);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3448 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3449 if (next->type == CPP_NAME && next->error_reported)
3450 goto out;
3453 cp_parser_parse_tentatively (parser);
3454 id = cp_parser_id_expression (parser,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3463 || TREE_CODE (id) == TYPE_DECL)
3465 cp_parser_abort_tentative_parse (parser);
3466 return false;
3468 if (!cp_parser_parse_definitely (parser))
3469 return false;
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3473 out:
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser->in_declarator_p)
3478 cp_parser_skip_to_end_of_block_or_statement (parser);
3479 return true;
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3487 static int
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3489 bool recovering,
3490 cpp_ttype or_ttype,
3491 bool consume_paren)
3493 unsigned paren_depth = 0;
3494 unsigned brace_depth = 0;
3495 unsigned square_depth = 0;
3497 if (recovering && or_ttype == CPP_EOF
3498 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3499 return 0;
3501 while (true)
3503 cp_token * token = cp_lexer_peek_token (parser->lexer);
3505 /* Have we found what we're looking for before the closing paren? */
3506 if (token->type == or_ttype && or_ttype != CPP_EOF
3507 && !brace_depth && !paren_depth && !square_depth)
3508 return -1;
3510 switch (token->type)
3512 case CPP_EOF:
3513 case CPP_PRAGMA_EOL:
3514 /* If we've run out of tokens, then there is no closing `)'. */
3515 return 0;
3517 /* This is good for lambda expression capture-lists. */
3518 case CPP_OPEN_SQUARE:
3519 ++square_depth;
3520 break;
3521 case CPP_CLOSE_SQUARE:
3522 if (!square_depth--)
3523 return 0;
3524 break;
3526 case CPP_SEMICOLON:
3527 /* This matches the processing in skip_to_end_of_statement. */
3528 if (!brace_depth)
3529 return 0;
3530 break;
3532 case CPP_OPEN_BRACE:
3533 ++brace_depth;
3534 break;
3535 case CPP_CLOSE_BRACE:
3536 if (!brace_depth--)
3537 return 0;
3538 break;
3540 case CPP_OPEN_PAREN:
3541 if (!brace_depth)
3542 ++paren_depth;
3543 break;
3545 case CPP_CLOSE_PAREN:
3546 if (!brace_depth && !paren_depth--)
3548 if (consume_paren)
3549 cp_lexer_consume_token (parser->lexer);
3550 return 1;
3552 break;
3554 default:
3555 break;
3558 /* Consume the token. */
3559 cp_lexer_consume_token (parser->lexer);
3563 /* Consume tokens up to, and including, the next non-nested closing `)'.
3564 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3565 are doing error recovery. Returns -1 if OR_COMMA is true and we
3566 found an unnested token of that type. */
3568 static int
3569 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3570 bool recovering,
3571 bool or_comma,
3572 bool consume_paren)
3574 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3575 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3576 ttype, consume_paren);
3579 /* Consume tokens until we reach the end of the current statement.
3580 Normally, that will be just before consuming a `;'. However, if a
3581 non-nested `}' comes first, then we stop before consuming that. */
3583 static void
3584 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3586 unsigned nesting_depth = 0;
3588 /* Unwind generic function template scope if necessary. */
3589 if (parser->fully_implicit_function_template_p)
3590 abort_fully_implicit_template (parser);
3592 while (true)
3594 cp_token *token = cp_lexer_peek_token (parser->lexer);
3596 switch (token->type)
3598 case CPP_EOF:
3599 case CPP_PRAGMA_EOL:
3600 /* If we've run out of tokens, stop. */
3601 return;
3603 case CPP_SEMICOLON:
3604 /* If the next token is a `;', we have reached the end of the
3605 statement. */
3606 if (!nesting_depth)
3607 return;
3608 break;
3610 case CPP_CLOSE_BRACE:
3611 /* If this is a non-nested '}', stop before consuming it.
3612 That way, when confronted with something like:
3614 { 3 + }
3616 we stop before consuming the closing '}', even though we
3617 have not yet reached a `;'. */
3618 if (nesting_depth == 0)
3619 return;
3621 /* If it is the closing '}' for a block that we have
3622 scanned, stop -- but only after consuming the token.
3623 That way given:
3625 void f g () { ... }
3626 typedef int I;
3628 we will stop after the body of the erroneously declared
3629 function, but before consuming the following `typedef'
3630 declaration. */
3631 if (--nesting_depth == 0)
3633 cp_lexer_consume_token (parser->lexer);
3634 return;
3636 break;
3638 case CPP_OPEN_BRACE:
3639 ++nesting_depth;
3640 break;
3642 default:
3643 break;
3646 /* Consume the token. */
3647 cp_lexer_consume_token (parser->lexer);
3651 /* This function is called at the end of a statement or declaration.
3652 If the next token is a semicolon, it is consumed; otherwise, error
3653 recovery is attempted. */
3655 static void
3656 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3658 /* Look for the trailing `;'. */
3659 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3661 /* If there is additional (erroneous) input, skip to the end of
3662 the statement. */
3663 cp_parser_skip_to_end_of_statement (parser);
3664 /* If the next token is now a `;', consume it. */
3665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3666 cp_lexer_consume_token (parser->lexer);
3670 /* Skip tokens until we have consumed an entire block, or until we
3671 have consumed a non-nested `;'. */
3673 static void
3674 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3676 int nesting_depth = 0;
3678 /* Unwind generic function template scope if necessary. */
3679 if (parser->fully_implicit_function_template_p)
3680 abort_fully_implicit_template (parser);
3682 while (nesting_depth >= 0)
3684 cp_token *token = cp_lexer_peek_token (parser->lexer);
3686 switch (token->type)
3688 case CPP_EOF:
3689 case CPP_PRAGMA_EOL:
3690 /* If we've run out of tokens, stop. */
3691 return;
3693 case CPP_SEMICOLON:
3694 /* Stop if this is an unnested ';'. */
3695 if (!nesting_depth)
3696 nesting_depth = -1;
3697 break;
3699 case CPP_CLOSE_BRACE:
3700 /* Stop if this is an unnested '}', or closes the outermost
3701 nesting level. */
3702 nesting_depth--;
3703 if (nesting_depth < 0)
3704 return;
3705 if (!nesting_depth)
3706 nesting_depth = -1;
3707 break;
3709 case CPP_OPEN_BRACE:
3710 /* Nest. */
3711 nesting_depth++;
3712 break;
3714 default:
3715 break;
3718 /* Consume the token. */
3719 cp_lexer_consume_token (parser->lexer);
3723 /* Skip tokens until a non-nested closing curly brace is the next
3724 token, or there are no more tokens. Return true in the first case,
3725 false otherwise. */
3727 static bool
3728 cp_parser_skip_to_closing_brace (cp_parser *parser)
3730 unsigned nesting_depth = 0;
3732 while (true)
3734 cp_token *token = cp_lexer_peek_token (parser->lexer);
3736 switch (token->type)
3738 case CPP_EOF:
3739 case CPP_PRAGMA_EOL:
3740 /* If we've run out of tokens, stop. */
3741 return false;
3743 case CPP_CLOSE_BRACE:
3744 /* If the next token is a non-nested `}', then we have reached
3745 the end of the current block. */
3746 if (nesting_depth-- == 0)
3747 return true;
3748 break;
3750 case CPP_OPEN_BRACE:
3751 /* If it the next token is a `{', then we are entering a new
3752 block. Consume the entire block. */
3753 ++nesting_depth;
3754 break;
3756 default:
3757 break;
3760 /* Consume the token. */
3761 cp_lexer_consume_token (parser->lexer);
3765 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3766 parameter is the PRAGMA token, allowing us to purge the entire pragma
3767 sequence. */
3769 static void
3770 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3772 cp_token *token;
3774 parser->lexer->in_pragma = false;
3777 token = cp_lexer_consume_token (parser->lexer);
3778 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3780 /* Ensure that the pragma is not parsed again. */
3781 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3784 /* Require pragma end of line, resyncing with it as necessary. The
3785 arguments are as for cp_parser_skip_to_pragma_eol. */
3787 static void
3788 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3790 parser->lexer->in_pragma = false;
3791 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3792 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3795 /* This is a simple wrapper around make_typename_type. When the id is
3796 an unresolved identifier node, we can provide a superior diagnostic
3797 using cp_parser_diagnose_invalid_type_name. */
3799 static tree
3800 cp_parser_make_typename_type (cp_parser *parser, tree id,
3801 location_t id_location)
3803 tree result;
3804 if (identifier_p (id))
3806 result = make_typename_type (parser->scope, id, typename_type,
3807 /*complain=*/tf_none);
3808 if (result == error_mark_node)
3809 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3810 return result;
3812 return make_typename_type (parser->scope, id, typename_type, tf_error);
3815 /* This is a wrapper around the
3816 make_{pointer,ptrmem,reference}_declarator functions that decides
3817 which one to call based on the CODE and CLASS_TYPE arguments. The
3818 CODE argument should be one of the values returned by
3819 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3820 appertain to the pointer or reference. */
3822 static cp_declarator *
3823 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3824 cp_cv_quals cv_qualifiers,
3825 cp_declarator *target,
3826 tree attributes)
3828 if (code == ERROR_MARK || target == cp_error_declarator)
3829 return cp_error_declarator;
3831 if (code == INDIRECT_REF)
3832 if (class_type == NULL_TREE)
3833 return make_pointer_declarator (cv_qualifiers, target, attributes);
3834 else
3835 return make_ptrmem_declarator (cv_qualifiers, class_type,
3836 target, attributes);
3837 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3838 return make_reference_declarator (cv_qualifiers, target,
3839 false, attributes);
3840 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3841 return make_reference_declarator (cv_qualifiers, target,
3842 true, attributes);
3843 gcc_unreachable ();
3846 /* Create a new C++ parser. */
3848 static cp_parser *
3849 cp_parser_new (void)
3851 cp_parser *parser;
3852 cp_lexer *lexer;
3853 unsigned i;
3855 /* cp_lexer_new_main is called before doing GC allocation because
3856 cp_lexer_new_main might load a PCH file. */
3857 lexer = cp_lexer_new_main ();
3859 /* Initialize the binops_by_token so that we can get the tree
3860 directly from the token. */
3861 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3862 binops_by_token[binops[i].token_type] = binops[i];
3864 parser = ggc_cleared_alloc<cp_parser> ();
3865 parser->lexer = lexer;
3866 parser->context = cp_parser_context_new (NULL);
3868 /* For now, we always accept GNU extensions. */
3869 parser->allow_gnu_extensions_p = 1;
3871 /* The `>' token is a greater-than operator, not the end of a
3872 template-id. */
3873 parser->greater_than_is_operator_p = true;
3875 parser->default_arg_ok_p = true;
3877 /* We are not parsing a constant-expression. */
3878 parser->integral_constant_expression_p = false;
3879 parser->allow_non_integral_constant_expression_p = false;
3880 parser->non_integral_constant_expression_p = false;
3882 /* Local variable names are not forbidden. */
3883 parser->local_variables_forbidden_p = false;
3885 /* We are not processing an `extern "C"' declaration. */
3886 parser->in_unbraced_linkage_specification_p = false;
3888 /* We are not processing a declarator. */
3889 parser->in_declarator_p = false;
3891 /* We are not processing a template-argument-list. */
3892 parser->in_template_argument_list_p = false;
3894 /* We are not in an iteration statement. */
3895 parser->in_statement = 0;
3897 /* We are not in a switch statement. */
3898 parser->in_switch_statement_p = false;
3900 /* We are not parsing a type-id inside an expression. */
3901 parser->in_type_id_in_expr_p = false;
3903 /* Declarations aren't implicitly extern "C". */
3904 parser->implicit_extern_c = false;
3906 /* String literals should be translated to the execution character set. */
3907 parser->translate_strings_p = true;
3909 /* We are not parsing a function body. */
3910 parser->in_function_body = false;
3912 /* We can correct until told otherwise. */
3913 parser->colon_corrects_to_scope_p = true;
3915 /* The unparsed function queue is empty. */
3916 push_unparsed_function_queues (parser);
3918 /* There are no classes being defined. */
3919 parser->num_classes_being_defined = 0;
3921 /* No template parameters apply. */
3922 parser->num_template_parameter_lists = 0;
3924 /* Special parsing data structures. */
3925 parser->omp_declare_simd = NULL;
3926 parser->oacc_routine = NULL;
3928 /* Not declaring an implicit function template. */
3929 parser->auto_is_implicit_function_template_parm_p = false;
3930 parser->fully_implicit_function_template_p = false;
3931 parser->implicit_template_parms = 0;
3932 parser->implicit_template_scope = 0;
3934 /* Allow constrained-type-specifiers. */
3935 parser->prevent_constrained_type_specifiers = 0;
3937 /* We haven't yet seen an 'extern "C"'. */
3938 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3940 return parser;
3943 /* Create a cp_lexer structure which will emit the tokens in CACHE
3944 and push it onto the parser's lexer stack. This is used for delayed
3945 parsing of in-class method bodies and default arguments, and should
3946 not be confused with tentative parsing. */
3947 static void
3948 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3950 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3951 lexer->next = parser->lexer;
3952 parser->lexer = lexer;
3954 /* Move the current source position to that of the first token in the
3955 new lexer. */
3956 cp_lexer_set_source_position_from_token (lexer->next_token);
3959 /* Pop the top lexer off the parser stack. This is never used for the
3960 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3961 static void
3962 cp_parser_pop_lexer (cp_parser *parser)
3964 cp_lexer *lexer = parser->lexer;
3965 parser->lexer = lexer->next;
3966 cp_lexer_destroy (lexer);
3968 /* Put the current source position back where it was before this
3969 lexer was pushed. */
3970 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3973 /* Lexical conventions [gram.lex] */
3975 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3976 identifier. */
3978 static cp_expr
3979 cp_parser_identifier (cp_parser* parser)
3981 cp_token *token;
3983 /* Look for the identifier. */
3984 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3985 /* Return the value. */
3986 if (token)
3987 return cp_expr (token->u.value, token->location);
3988 else
3989 return error_mark_node;
3992 /* Parse a sequence of adjacent string constants. Returns a
3993 TREE_STRING representing the combined, nul-terminated string
3994 constant. If TRANSLATE is true, translate the string to the
3995 execution character set. If WIDE_OK is true, a wide string is
3996 invalid here.
3998 C++98 [lex.string] says that if a narrow string literal token is
3999 adjacent to a wide string literal token, the behavior is undefined.
4000 However, C99 6.4.5p4 says that this results in a wide string literal.
4001 We follow C99 here, for consistency with the C front end.
4003 This code is largely lifted from lex_string() in c-lex.c.
4005 FUTURE: ObjC++ will need to handle @-strings here. */
4006 static cp_expr
4007 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4008 bool lookup_udlit = true)
4010 tree value;
4011 size_t count;
4012 struct obstack str_ob;
4013 cpp_string str, istr, *strs;
4014 cp_token *tok;
4015 enum cpp_ttype type, curr_type;
4016 int have_suffix_p = 0;
4017 tree string_tree;
4018 tree suffix_id = NULL_TREE;
4019 bool curr_tok_is_userdef_p = false;
4021 tok = cp_lexer_peek_token (parser->lexer);
4022 if (!cp_parser_is_string_literal (tok))
4024 cp_parser_error (parser, "expected string-literal");
4025 return error_mark_node;
4028 location_t loc = tok->location;
4030 if (cpp_userdef_string_p (tok->type))
4032 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4033 curr_type = cpp_userdef_string_remove_type (tok->type);
4034 curr_tok_is_userdef_p = true;
4036 else
4038 string_tree = tok->u.value;
4039 curr_type = tok->type;
4041 type = curr_type;
4043 /* Try to avoid the overhead of creating and destroying an obstack
4044 for the common case of just one string. */
4045 if (!cp_parser_is_string_literal
4046 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4048 cp_lexer_consume_token (parser->lexer);
4050 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4051 str.len = TREE_STRING_LENGTH (string_tree);
4052 count = 1;
4054 if (curr_tok_is_userdef_p)
4056 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4057 have_suffix_p = 1;
4058 curr_type = cpp_userdef_string_remove_type (tok->type);
4060 else
4061 curr_type = tok->type;
4063 strs = &str;
4065 else
4067 location_t last_tok_loc = tok->location;
4068 gcc_obstack_init (&str_ob);
4069 count = 0;
4073 cp_lexer_consume_token (parser->lexer);
4074 count++;
4075 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4076 str.len = TREE_STRING_LENGTH (string_tree);
4078 if (curr_tok_is_userdef_p)
4080 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4081 if (have_suffix_p == 0)
4083 suffix_id = curr_suffix_id;
4084 have_suffix_p = 1;
4086 else if (have_suffix_p == 1
4087 && curr_suffix_id != suffix_id)
4089 error ("inconsistent user-defined literal suffixes"
4090 " %qD and %qD in string literal",
4091 suffix_id, curr_suffix_id);
4092 have_suffix_p = -1;
4094 curr_type = cpp_userdef_string_remove_type (tok->type);
4096 else
4097 curr_type = tok->type;
4099 if (type != curr_type)
4101 if (type == CPP_STRING)
4102 type = curr_type;
4103 else if (curr_type != CPP_STRING)
4105 rich_location rich_loc (line_table, tok->location);
4106 rich_loc.add_range (last_tok_loc, false);
4107 error_at (&rich_loc,
4108 "unsupported non-standard concatenation "
4109 "of string literals");
4113 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4115 last_tok_loc = tok->location;
4117 tok = cp_lexer_peek_token (parser->lexer);
4118 if (cpp_userdef_string_p (tok->type))
4120 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4121 curr_type = cpp_userdef_string_remove_type (tok->type);
4122 curr_tok_is_userdef_p = true;
4124 else
4126 string_tree = tok->u.value;
4127 curr_type = tok->type;
4128 curr_tok_is_userdef_p = false;
4131 while (cp_parser_is_string_literal (tok));
4133 /* A string literal built by concatenation has its caret=start at
4134 the start of the initial string, and its finish at the finish of
4135 the final string literal. */
4136 loc = make_location (loc, loc, get_finish (last_tok_loc));
4138 strs = (cpp_string *) obstack_finish (&str_ob);
4141 if (type != CPP_STRING && !wide_ok)
4143 cp_parser_error (parser, "a wide string is invalid in this context");
4144 type = CPP_STRING;
4147 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4148 (parse_in, strs, count, &istr, type))
4150 value = build_string (istr.len, (const char *)istr.text);
4151 free (CONST_CAST (unsigned char *, istr.text));
4153 switch (type)
4155 default:
4156 case CPP_STRING:
4157 case CPP_UTF8STRING:
4158 TREE_TYPE (value) = char_array_type_node;
4159 break;
4160 case CPP_STRING16:
4161 TREE_TYPE (value) = char16_array_type_node;
4162 break;
4163 case CPP_STRING32:
4164 TREE_TYPE (value) = char32_array_type_node;
4165 break;
4166 case CPP_WSTRING:
4167 TREE_TYPE (value) = wchar_array_type_node;
4168 break;
4171 value = fix_string_type (value);
4173 if (have_suffix_p)
4175 tree literal = build_userdef_literal (suffix_id, value,
4176 OT_NONE, NULL_TREE);
4177 if (lookup_udlit)
4178 value = cp_parser_userdef_string_literal (literal);
4179 else
4180 value = literal;
4183 else
4184 /* cpp_interpret_string has issued an error. */
4185 value = error_mark_node;
4187 if (count > 1)
4188 obstack_free (&str_ob, 0);
4190 return cp_expr (value, loc);
4193 /* Look up a literal operator with the name and the exact arguments. */
4195 static tree
4196 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4198 tree decl;
4199 decl = lookup_name (name);
4200 if (!decl || !is_overloaded_fn (decl))
4201 return error_mark_node;
4203 for (lkp_iterator iter (decl); iter; ++iter)
4205 unsigned int ix;
4206 bool found = true;
4207 tree fn = *iter;
4208 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4209 if (parmtypes != NULL_TREE)
4211 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4212 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4214 tree tparm = TREE_VALUE (parmtypes);
4215 tree targ = TREE_TYPE ((*args)[ix]);
4216 bool ptr = TYPE_PTR_P (tparm);
4217 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4218 if ((ptr || arr || !same_type_p (tparm, targ))
4219 && (!ptr || !arr
4220 || !same_type_p (TREE_TYPE (tparm),
4221 TREE_TYPE (targ))))
4222 found = false;
4224 if (found
4225 && ix == vec_safe_length (args)
4226 /* May be this should be sufficient_parms_p instead,
4227 depending on how exactly should user-defined literals
4228 work in presence of default arguments on the literal
4229 operator parameters. */
4230 && parmtypes == void_list_node)
4231 return decl;
4235 return error_mark_node;
4238 /* Parse a user-defined char constant. Returns a call to a user-defined
4239 literal operator taking the character as an argument. */
4241 static cp_expr
4242 cp_parser_userdef_char_literal (cp_parser *parser)
4244 cp_token *token = cp_lexer_consume_token (parser->lexer);
4245 tree literal = token->u.value;
4246 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4247 tree value = USERDEF_LITERAL_VALUE (literal);
4248 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4249 tree decl, result;
4251 /* Build up a call to the user-defined operator */
4252 /* Lookup the name we got back from the id-expression. */
4253 vec<tree, va_gc> *args = make_tree_vector ();
4254 vec_safe_push (args, value);
4255 decl = lookup_literal_operator (name, args);
4256 if (!decl || decl == error_mark_node)
4258 error ("unable to find character literal operator %qD with %qT argument",
4259 name, TREE_TYPE (value));
4260 release_tree_vector (args);
4261 return error_mark_node;
4263 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4264 release_tree_vector (args);
4265 return result;
4268 /* A subroutine of cp_parser_userdef_numeric_literal to
4269 create a char... template parameter pack from a string node. */
4271 static tree
4272 make_char_string_pack (tree value)
4274 tree charvec;
4275 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4276 const char *str = TREE_STRING_POINTER (value);
4277 int i, len = TREE_STRING_LENGTH (value) - 1;
4278 tree argvec = make_tree_vec (1);
4280 /* Fill in CHARVEC with all of the parameters. */
4281 charvec = make_tree_vec (len);
4282 for (i = 0; i < len; ++i)
4283 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4285 /* Build the argument packs. */
4286 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4288 TREE_VEC_ELT (argvec, 0) = argpack;
4290 return argvec;
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4296 static tree
4297 make_string_pack (tree value)
4299 tree charvec;
4300 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4301 const unsigned char *str
4302 = (const unsigned char *) TREE_STRING_POINTER (value);
4303 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4304 int len = TREE_STRING_LENGTH (value) / sz - 1;
4305 tree argvec = make_tree_vec (2);
4307 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4308 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4310 /* First template parm is character type. */
4311 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4313 /* Fill in CHARVEC with all of the parameters. */
4314 charvec = make_tree_vec (len);
4315 for (int i = 0; i < len; ++i)
4316 TREE_VEC_ELT (charvec, i)
4317 = double_int_to_tree (str_char_type_node,
4318 double_int::from_buffer (str + i * sz, sz));
4320 /* Build the argument packs. */
4321 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4323 TREE_VEC_ELT (argvec, 1) = argpack;
4325 return argvec;
4328 /* Parse a user-defined numeric constant. returns a call to a user-defined
4329 literal operator. */
4331 static cp_expr
4332 cp_parser_userdef_numeric_literal (cp_parser *parser)
4334 cp_token *token = cp_lexer_consume_token (parser->lexer);
4335 tree literal = token->u.value;
4336 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4337 tree value = USERDEF_LITERAL_VALUE (literal);
4338 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4339 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4340 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341 tree decl, result;
4342 vec<tree, va_gc> *args;
4344 /* Look for a literal operator taking the exact type of numeric argument
4345 as the literal value. */
4346 args = make_tree_vector ();
4347 vec_safe_push (args, value);
4348 decl = lookup_literal_operator (name, args);
4349 if (decl && decl != error_mark_node)
4351 result = finish_call_expr (decl, &args, false, true,
4352 tf_warning_or_error);
4354 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4356 warning_at (token->location, OPT_Woverflow,
4357 "integer literal exceeds range of %qT type",
4358 long_long_unsigned_type_node);
4360 else
4362 if (overflow > 0)
4363 warning_at (token->location, OPT_Woverflow,
4364 "floating literal exceeds range of %qT type",
4365 long_double_type_node);
4366 else if (overflow < 0)
4367 warning_at (token->location, OPT_Woverflow,
4368 "floating literal truncated to zero");
4371 release_tree_vector (args);
4372 return result;
4374 release_tree_vector (args);
4376 /* If the numeric argument didn't work, look for a raw literal
4377 operator taking a const char* argument consisting of the number
4378 in string format. */
4379 args = make_tree_vector ();
4380 vec_safe_push (args, num_string);
4381 decl = lookup_literal_operator (name, args);
4382 if (decl && decl != error_mark_node)
4384 result = finish_call_expr (decl, &args, false, true,
4385 tf_warning_or_error);
4386 release_tree_vector (args);
4387 return result;
4389 release_tree_vector (args);
4391 /* If the raw literal didn't work, look for a non-type template
4392 function with parameter pack char.... Call the function with
4393 template parameter characters representing the number. */
4394 args = make_tree_vector ();
4395 decl = lookup_literal_operator (name, args);
4396 if (decl && decl != error_mark_node)
4398 tree tmpl_args = make_char_string_pack (num_string);
4399 decl = lookup_template_function (decl, tmpl_args);
4400 result = finish_call_expr (decl, &args, false, true,
4401 tf_warning_or_error);
4402 release_tree_vector (args);
4403 return result;
4406 release_tree_vector (args);
4408 /* In C++14 the standard library defines complex number suffixes that
4409 conflict with GNU extensions. Prefer them if <complex> is #included. */
4410 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4411 bool i14 = (cxx_dialect > cxx11
4412 && (id_equal (suffix_id, "i")
4413 || id_equal (suffix_id, "if")
4414 || id_equal (suffix_id, "il")));
4415 diagnostic_t kind = DK_ERROR;
4416 int opt = 0;
4418 if (i14 && ext)
4420 tree cxlit = lookup_qualified_name (std_node,
4421 get_identifier ("complex_literals"),
4422 0, false, false);
4423 if (cxlit == error_mark_node)
4425 /* No <complex>, so pedwarn and use GNU semantics. */
4426 kind = DK_PEDWARN;
4427 opt = OPT_Wpedantic;
4431 bool complained
4432 = emit_diagnostic (kind, input_location, opt,
4433 "unable to find numeric literal operator %qD", name);
4435 if (!complained)
4436 /* Don't inform either. */;
4437 else if (i14)
4439 inform (token->location, "add %<using namespace std::complex_literals%> "
4440 "(from <complex>) to enable the C++14 user-defined literal "
4441 "suffixes");
4442 if (ext)
4443 inform (token->location, "or use %<j%> instead of %<i%> for the "
4444 "GNU built-in suffix");
4446 else if (!ext)
4447 inform (token->location, "use -fext-numeric-literals "
4448 "to enable more built-in suffixes");
4450 if (kind == DK_ERROR)
4451 value = error_mark_node;
4452 else
4454 /* Use the built-in semantics. */
4455 tree type;
4456 if (id_equal (suffix_id, "i"))
4458 if (TREE_CODE (value) == INTEGER_CST)
4459 type = integer_type_node;
4460 else
4461 type = double_type_node;
4463 else if (id_equal (suffix_id, "if"))
4464 type = float_type_node;
4465 else /* if (id_equal (suffix_id, "il")) */
4466 type = long_double_type_node;
4468 value = build_complex (build_complex_type (type),
4469 fold_convert (type, integer_zero_node),
4470 fold_convert (type, value));
4473 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4474 /* Avoid repeated diagnostics. */
4475 token->u.value = value;
4476 return value;
4479 /* Parse a user-defined string constant. Returns a call to a user-defined
4480 literal operator taking a character pointer and the length of the string
4481 as arguments. */
4483 static tree
4484 cp_parser_userdef_string_literal (tree literal)
4486 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4487 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4488 tree value = USERDEF_LITERAL_VALUE (literal);
4489 int len = TREE_STRING_LENGTH (value)
4490 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4491 tree decl, result;
4492 vec<tree, va_gc> *args;
4494 /* Build up a call to the user-defined operator. */
4495 /* Lookup the name we got back from the id-expression. */
4496 args = make_tree_vector ();
4497 vec_safe_push (args, value);
4498 vec_safe_push (args, build_int_cst (size_type_node, len));
4499 decl = lookup_literal_operator (name, args);
4501 if (decl && decl != error_mark_node)
4503 result = finish_call_expr (decl, &args, false, true,
4504 tf_warning_or_error);
4505 release_tree_vector (args);
4506 return result;
4508 release_tree_vector (args);
4510 /* Look for a template function with typename parameter CharT
4511 and parameter pack CharT... Call the function with
4512 template parameter characters representing the string. */
4513 args = make_tree_vector ();
4514 decl = lookup_literal_operator (name, args);
4515 if (decl && decl != error_mark_node)
4517 tree tmpl_args = make_string_pack (value);
4518 decl = lookup_template_function (decl, tmpl_args);
4519 result = finish_call_expr (decl, &args, false, true,
4520 tf_warning_or_error);
4521 release_tree_vector (args);
4522 return result;
4524 release_tree_vector (args);
4526 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4527 name, TREE_TYPE (value), size_type_node);
4528 return error_mark_node;
4532 /* Basic concepts [gram.basic] */
4534 /* Parse a translation-unit.
4536 translation-unit:
4537 declaration-seq [opt]
4539 Returns TRUE if all went well. */
4541 static bool
4542 cp_parser_translation_unit (cp_parser* parser)
4544 /* The address of the first non-permanent object on the declarator
4545 obstack. */
4546 static void *declarator_obstack_base;
4548 bool success;
4550 /* Create the declarator obstack, if necessary. */
4551 if (!cp_error_declarator)
4553 gcc_obstack_init (&declarator_obstack);
4554 /* Create the error declarator. */
4555 cp_error_declarator = make_declarator (cdk_error);
4556 /* Create the empty parameter list. */
4557 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4558 UNKNOWN_LOCATION);
4559 /* Remember where the base of the declarator obstack lies. */
4560 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4563 cp_parser_declaration_seq_opt (parser);
4565 /* If there are no tokens left then all went well. */
4566 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4568 /* Get rid of the token array; we don't need it any more. */
4569 cp_lexer_destroy (parser->lexer);
4570 parser->lexer = NULL;
4572 /* This file might have been a context that's implicitly extern
4573 "C". If so, pop the lang context. (Only relevant for PCH.) */
4574 if (parser->implicit_extern_c)
4576 pop_lang_context ();
4577 parser->implicit_extern_c = false;
4580 /* Finish up. */
4581 finish_translation_unit ();
4583 success = true;
4585 else
4587 cp_parser_error (parser, "expected declaration");
4588 success = false;
4591 /* Make sure the declarator obstack was fully cleaned up. */
4592 gcc_assert (obstack_next_free (&declarator_obstack)
4593 == declarator_obstack_base);
4595 /* All went well. */
4596 return success;
4599 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4600 decltype context. */
4602 static inline tsubst_flags_t
4603 complain_flags (bool decltype_p)
4605 tsubst_flags_t complain = tf_warning_or_error;
4606 if (decltype_p)
4607 complain |= tf_decltype;
4608 return complain;
4611 /* We're about to parse a collection of statements. If we're currently
4612 parsing tentatively, set up a firewall so that any nested
4613 cp_parser_commit_to_tentative_parse won't affect the current context. */
4615 static cp_token_position
4616 cp_parser_start_tentative_firewall (cp_parser *parser)
4618 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4619 return 0;
4621 cp_parser_parse_tentatively (parser);
4622 cp_parser_commit_to_topmost_tentative_parse (parser);
4623 return cp_lexer_token_position (parser->lexer, false);
4626 /* We've finished parsing the collection of statements. Wrap up the
4627 firewall and replace the relevant tokens with the parsed form. */
4629 static void
4630 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4631 tree expr)
4633 if (!start)
4634 return;
4636 /* Finish the firewall level. */
4637 cp_parser_parse_definitely (parser);
4638 /* And remember the result of the parse for when we try again. */
4639 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4640 token->type = CPP_PREPARSED_EXPR;
4641 token->u.value = expr;
4642 token->keyword = RID_MAX;
4643 cp_lexer_purge_tokens_after (parser->lexer, start);
4646 /* Like the above functions, but let the user modify the tokens. Used by
4647 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4648 later parses, so it makes sense to localize the effects of
4649 cp_parser_commit_to_tentative_parse. */
4651 struct tentative_firewall
4653 cp_parser *parser;
4654 bool set;
4656 tentative_firewall (cp_parser *p): parser(p)
4658 /* If we're currently parsing tentatively, start a committed level as a
4659 firewall and then an inner tentative parse. */
4660 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4662 cp_parser_parse_tentatively (parser);
4663 cp_parser_commit_to_topmost_tentative_parse (parser);
4664 cp_parser_parse_tentatively (parser);
4668 ~tentative_firewall()
4670 if (set)
4672 /* Finish the inner tentative parse and the firewall, propagating any
4673 uncommitted error state to the outer tentative parse. */
4674 bool err = cp_parser_error_occurred (parser);
4675 cp_parser_parse_definitely (parser);
4676 cp_parser_parse_definitely (parser);
4677 if (err)
4678 cp_parser_simulate_error (parser);
4683 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4684 This class is for tracking such a matching pair of symbols.
4685 In particular, it tracks the location of the first token,
4686 so that if the second token is missing, we can highlight the
4687 location of the first token when notifying the user about the
4688 problem. */
4690 template <typename traits_t>
4691 class token_pair
4693 public:
4694 /* token_pair's ctor. */
4695 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4697 /* If the next token is the opening symbol for this pair, consume it and
4698 return true.
4699 Otherwise, issue an error and return false.
4700 In either case, record the location of the opening token. */
4702 bool require_open (cp_parser *parser)
4704 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4705 return cp_parser_require (parser, traits_t::open_token_type,
4706 traits_t::required_token_open);
4709 /* Consume the next token from PARSER, recording its location as
4710 that of the opening token within the pair. */
4712 cp_token * consume_open (cp_parser *parser)
4714 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4715 gcc_assert (tok->type == traits_t::open_token_type);
4716 m_open_loc = tok->location;
4717 return tok;
4720 /* If the next token is the closing symbol for this pair, consume it
4721 and return it.
4722 Otherwise, issue an error, highlighting the location of the
4723 corresponding opening token, and return NULL. */
4725 cp_token *require_close (cp_parser *parser) const
4727 return cp_parser_require (parser, traits_t::close_token_type,
4728 traits_t::required_token_close,
4729 m_open_loc);
4732 private:
4733 location_t m_open_loc;
4736 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4738 struct matching_paren_traits
4740 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4741 static const enum required_token required_token_open = RT_OPEN_PAREN;
4742 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4743 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4746 /* "matching_parens" is a token_pair<T> class for tracking matching
4747 pairs of parentheses. */
4749 typedef token_pair<matching_paren_traits> matching_parens;
4751 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4753 struct matching_brace_traits
4755 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4756 static const enum required_token required_token_open = RT_OPEN_BRACE;
4757 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4758 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4761 /* "matching_braces" is a token_pair<T> class for tracking matching
4762 pairs of braces. */
4764 typedef token_pair<matching_brace_traits> matching_braces;
4767 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4768 enclosing parentheses. */
4770 static cp_expr
4771 cp_parser_statement_expr (cp_parser *parser)
4773 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4775 /* Consume the '('. */
4776 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4777 matching_parens parens;
4778 parens.consume_open (parser);
4779 /* Start the statement-expression. */
4780 tree expr = begin_stmt_expr ();
4781 /* Parse the compound-statement. */
4782 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4783 /* Finish up. */
4784 expr = finish_stmt_expr (expr, false);
4785 /* Consume the ')'. */
4786 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4787 if (!parens.require_close (parser))
4788 cp_parser_skip_to_end_of_statement (parser);
4790 cp_parser_end_tentative_firewall (parser, start, expr);
4791 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4792 return cp_expr (expr, combined_loc);
4795 /* Expressions [gram.expr] */
4797 /* Parse a fold-operator.
4799 fold-operator:
4800 - * / % ^ & | = < > << >>
4801 = -= *= /= %= ^= &= |= <<= >>=
4802 == != <= >= && || , .* ->*
4804 This returns the tree code corresponding to the matched operator
4805 as an int. When the current token matches a compound assignment
4806 opertor, the resulting tree code is the negative value of the
4807 non-assignment operator. */
4809 static int
4810 cp_parser_fold_operator (cp_token *token)
4812 switch (token->type)
4814 case CPP_PLUS: return PLUS_EXPR;
4815 case CPP_MINUS: return MINUS_EXPR;
4816 case CPP_MULT: return MULT_EXPR;
4817 case CPP_DIV: return TRUNC_DIV_EXPR;
4818 case CPP_MOD: return TRUNC_MOD_EXPR;
4819 case CPP_XOR: return BIT_XOR_EXPR;
4820 case CPP_AND: return BIT_AND_EXPR;
4821 case CPP_OR: return BIT_IOR_EXPR;
4822 case CPP_LSHIFT: return LSHIFT_EXPR;
4823 case CPP_RSHIFT: return RSHIFT_EXPR;
4825 case CPP_EQ: return -NOP_EXPR;
4826 case CPP_PLUS_EQ: return -PLUS_EXPR;
4827 case CPP_MINUS_EQ: return -MINUS_EXPR;
4828 case CPP_MULT_EQ: return -MULT_EXPR;
4829 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4830 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4831 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4832 case CPP_AND_EQ: return -BIT_AND_EXPR;
4833 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4834 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4835 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4837 case CPP_EQ_EQ: return EQ_EXPR;
4838 case CPP_NOT_EQ: return NE_EXPR;
4839 case CPP_LESS: return LT_EXPR;
4840 case CPP_GREATER: return GT_EXPR;
4841 case CPP_LESS_EQ: return LE_EXPR;
4842 case CPP_GREATER_EQ: return GE_EXPR;
4844 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4845 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4847 case CPP_COMMA: return COMPOUND_EXPR;
4849 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4850 case CPP_DEREF_STAR: return MEMBER_REF;
4852 default: return ERROR_MARK;
4856 /* Returns true if CODE indicates a binary expression, which is not allowed in
4857 the LHS of a fold-expression. More codes will need to be added to use this
4858 function in other contexts. */
4860 static bool
4861 is_binary_op (tree_code code)
4863 switch (code)
4865 case PLUS_EXPR:
4866 case POINTER_PLUS_EXPR:
4867 case MINUS_EXPR:
4868 case MULT_EXPR:
4869 case TRUNC_DIV_EXPR:
4870 case TRUNC_MOD_EXPR:
4871 case BIT_XOR_EXPR:
4872 case BIT_AND_EXPR:
4873 case BIT_IOR_EXPR:
4874 case LSHIFT_EXPR:
4875 case RSHIFT_EXPR:
4877 case MODOP_EXPR:
4879 case EQ_EXPR:
4880 case NE_EXPR:
4881 case LE_EXPR:
4882 case GE_EXPR:
4883 case LT_EXPR:
4884 case GT_EXPR:
4886 case TRUTH_ANDIF_EXPR:
4887 case TRUTH_ORIF_EXPR:
4889 case COMPOUND_EXPR:
4891 case DOTSTAR_EXPR:
4892 case MEMBER_REF:
4893 return true;
4895 default:
4896 return false;
4900 /* If the next token is a suitable fold operator, consume it and return as
4901 the function above. */
4903 static int
4904 cp_parser_fold_operator (cp_parser *parser)
4906 cp_token* token = cp_lexer_peek_token (parser->lexer);
4907 int code = cp_parser_fold_operator (token);
4908 if (code != ERROR_MARK)
4909 cp_lexer_consume_token (parser->lexer);
4910 return code;
4913 /* Parse a fold-expression.
4915 fold-expression:
4916 ( ... folding-operator cast-expression)
4917 ( cast-expression folding-operator ... )
4918 ( cast-expression folding operator ... folding-operator cast-expression)
4920 Note that the '(' and ')' are matched in primary expression. */
4922 static cp_expr
4923 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4925 cp_id_kind pidk;
4927 // Left fold.
4928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4930 cp_lexer_consume_token (parser->lexer);
4931 int op = cp_parser_fold_operator (parser);
4932 if (op == ERROR_MARK)
4934 cp_parser_error (parser, "expected binary operator");
4935 return error_mark_node;
4938 tree expr = cp_parser_cast_expression (parser, false, false,
4939 false, &pidk);
4940 if (expr == error_mark_node)
4941 return error_mark_node;
4942 return finish_left_unary_fold_expr (expr, op);
4945 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4946 int op = cp_parser_fold_operator (parser);
4947 if (op == ERROR_MARK)
4949 cp_parser_error (parser, "expected binary operator");
4950 return error_mark_node;
4953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4955 cp_parser_error (parser, "expected ...");
4956 return error_mark_node;
4958 cp_lexer_consume_token (parser->lexer);
4960 /* The operands of a fold-expression are cast-expressions, so binary or
4961 conditional expressions are not allowed. We check this here to avoid
4962 tentative parsing. */
4963 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4964 /* OK, the expression was parenthesized. */;
4965 else if (is_binary_op (TREE_CODE (expr1)))
4966 error_at (location_of (expr1),
4967 "binary expression in operand of fold-expression");
4968 else if (TREE_CODE (expr1) == COND_EXPR
4969 || (REFERENCE_REF_P (expr1)
4970 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4971 error_at (location_of (expr1),
4972 "conditional expression in operand of fold-expression");
4974 // Right fold.
4975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4976 return finish_right_unary_fold_expr (expr1, op);
4978 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4980 cp_parser_error (parser, "mismatched operator in fold-expression");
4981 return error_mark_node;
4983 cp_lexer_consume_token (parser->lexer);
4985 // Binary left or right fold.
4986 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4987 if (expr2 == error_mark_node)
4988 return error_mark_node;
4989 return finish_binary_fold_expr (expr1, expr2, op);
4992 /* Parse a primary-expression.
4994 primary-expression:
4995 literal
4996 this
4997 ( expression )
4998 id-expression
4999 lambda-expression (C++11)
5001 GNU Extensions:
5003 primary-expression:
5004 ( compound-statement )
5005 __builtin_va_arg ( assignment-expression , type-id )
5006 __builtin_offsetof ( type-id , offsetof-expression )
5008 C++ Extensions:
5009 __has_nothrow_assign ( type-id )
5010 __has_nothrow_constructor ( type-id )
5011 __has_nothrow_copy ( type-id )
5012 __has_trivial_assign ( type-id )
5013 __has_trivial_constructor ( type-id )
5014 __has_trivial_copy ( type-id )
5015 __has_trivial_destructor ( type-id )
5016 __has_virtual_destructor ( type-id )
5017 __is_abstract ( type-id )
5018 __is_base_of ( type-id , type-id )
5019 __is_class ( type-id )
5020 __is_empty ( type-id )
5021 __is_enum ( type-id )
5022 __is_final ( type-id )
5023 __is_literal_type ( type-id )
5024 __is_pod ( type-id )
5025 __is_polymorphic ( type-id )
5026 __is_std_layout ( type-id )
5027 __is_trivial ( type-id )
5028 __is_union ( type-id )
5030 Objective-C++ Extension:
5032 primary-expression:
5033 objc-expression
5035 literal:
5036 __null
5038 ADDRESS_P is true iff this expression was immediately preceded by
5039 "&" and therefore might denote a pointer-to-member. CAST_P is true
5040 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5041 true iff this expression is a template argument.
5043 Returns a representation of the expression. Upon return, *IDK
5044 indicates what kind of id-expression (if any) was present. */
5046 static cp_expr
5047 cp_parser_primary_expression (cp_parser *parser,
5048 bool address_p,
5049 bool cast_p,
5050 bool template_arg_p,
5051 bool decltype_p,
5052 cp_id_kind *idk)
5054 cp_token *token = NULL;
5056 /* Assume the primary expression is not an id-expression. */
5057 *idk = CP_ID_KIND_NONE;
5059 /* Peek at the next token. */
5060 token = cp_lexer_peek_token (parser->lexer);
5061 switch ((int) token->type)
5063 /* literal:
5064 integer-literal
5065 character-literal
5066 floating-literal
5067 string-literal
5068 boolean-literal
5069 pointer-literal
5070 user-defined-literal */
5071 case CPP_CHAR:
5072 case CPP_CHAR16:
5073 case CPP_CHAR32:
5074 case CPP_WCHAR:
5075 case CPP_UTF8CHAR:
5076 case CPP_NUMBER:
5077 case CPP_PREPARSED_EXPR:
5078 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5079 return cp_parser_userdef_numeric_literal (parser);
5080 token = cp_lexer_consume_token (parser->lexer);
5081 if (TREE_CODE (token->u.value) == FIXED_CST)
5083 error_at (token->location,
5084 "fixed-point types not supported in C++");
5085 return error_mark_node;
5087 /* Floating-point literals are only allowed in an integral
5088 constant expression if they are cast to an integral or
5089 enumeration type. */
5090 if (TREE_CODE (token->u.value) == REAL_CST
5091 && parser->integral_constant_expression_p
5092 && pedantic)
5094 /* CAST_P will be set even in invalid code like "int(2.7 +
5095 ...)". Therefore, we have to check that the next token
5096 is sure to end the cast. */
5097 if (cast_p)
5099 cp_token *next_token;
5101 next_token = cp_lexer_peek_token (parser->lexer);
5102 if (/* The comma at the end of an
5103 enumerator-definition. */
5104 next_token->type != CPP_COMMA
5105 /* The curly brace at the end of an enum-specifier. */
5106 && next_token->type != CPP_CLOSE_BRACE
5107 /* The end of a statement. */
5108 && next_token->type != CPP_SEMICOLON
5109 /* The end of the cast-expression. */
5110 && next_token->type != CPP_CLOSE_PAREN
5111 /* The end of an array bound. */
5112 && next_token->type != CPP_CLOSE_SQUARE
5113 /* The closing ">" in a template-argument-list. */
5114 && (next_token->type != CPP_GREATER
5115 || parser->greater_than_is_operator_p)
5116 /* C++0x only: A ">>" treated like two ">" tokens,
5117 in a template-argument-list. */
5118 && (next_token->type != CPP_RSHIFT
5119 || (cxx_dialect == cxx98)
5120 || parser->greater_than_is_operator_p))
5121 cast_p = false;
5124 /* If we are within a cast, then the constraint that the
5125 cast is to an integral or enumeration type will be
5126 checked at that point. If we are not within a cast, then
5127 this code is invalid. */
5128 if (!cast_p)
5129 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5131 return cp_expr (token->u.value, token->location);
5133 case CPP_CHAR_USERDEF:
5134 case CPP_CHAR16_USERDEF:
5135 case CPP_CHAR32_USERDEF:
5136 case CPP_WCHAR_USERDEF:
5137 case CPP_UTF8CHAR_USERDEF:
5138 return cp_parser_userdef_char_literal (parser);
5140 case CPP_STRING:
5141 case CPP_STRING16:
5142 case CPP_STRING32:
5143 case CPP_WSTRING:
5144 case CPP_UTF8STRING:
5145 case CPP_STRING_USERDEF:
5146 case CPP_STRING16_USERDEF:
5147 case CPP_STRING32_USERDEF:
5148 case CPP_WSTRING_USERDEF:
5149 case CPP_UTF8STRING_USERDEF:
5150 /* ??? Should wide strings be allowed when parser->translate_strings_p
5151 is false (i.e. in attributes)? If not, we can kill the third
5152 argument to cp_parser_string_literal. */
5153 return cp_parser_string_literal (parser,
5154 parser->translate_strings_p,
5155 true);
5157 case CPP_OPEN_PAREN:
5158 /* If we see `( { ' then we are looking at the beginning of
5159 a GNU statement-expression. */
5160 if (cp_parser_allow_gnu_extensions_p (parser)
5161 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5163 /* Statement-expressions are not allowed by the standard. */
5164 pedwarn (token->location, OPT_Wpedantic,
5165 "ISO C++ forbids braced-groups within expressions");
5167 /* And they're not allowed outside of a function-body; you
5168 cannot, for example, write:
5170 int i = ({ int j = 3; j + 1; });
5172 at class or namespace scope. */
5173 if (!parser->in_function_body
5174 || parser->in_template_argument_list_p)
5176 error_at (token->location,
5177 "statement-expressions are not allowed outside "
5178 "functions nor in template-argument lists");
5179 cp_parser_skip_to_end_of_block_or_statement (parser);
5180 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5181 cp_lexer_consume_token (parser->lexer);
5182 return error_mark_node;
5184 else
5185 return cp_parser_statement_expr (parser);
5187 /* Otherwise it's a normal parenthesized expression. */
5189 cp_expr expr;
5190 bool saved_greater_than_is_operator_p;
5192 location_t open_paren_loc = token->location;
5194 /* Consume the `('. */
5195 matching_parens parens;
5196 parens.consume_open (parser);
5197 /* Within a parenthesized expression, a `>' token is always
5198 the greater-than operator. */
5199 saved_greater_than_is_operator_p
5200 = parser->greater_than_is_operator_p;
5201 parser->greater_than_is_operator_p = true;
5203 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5204 /* Left fold expression. */
5205 expr = NULL_TREE;
5206 else
5207 /* Parse the parenthesized expression. */
5208 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5210 token = cp_lexer_peek_token (parser->lexer);
5211 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5213 expr = cp_parser_fold_expression (parser, expr);
5214 if (expr != error_mark_node
5215 && cxx_dialect < cxx17
5216 && !in_system_header_at (input_location))
5217 pedwarn (input_location, 0, "fold-expressions only available "
5218 "with -std=c++17 or -std=gnu++17");
5220 else
5221 /* Let the front end know that this expression was
5222 enclosed in parentheses. This matters in case, for
5223 example, the expression is of the form `A::B', since
5224 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5225 not. */
5226 expr = finish_parenthesized_expr (expr);
5228 /* DR 705: Wrapping an unqualified name in parentheses
5229 suppresses arg-dependent lookup. We want to pass back
5230 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5231 (c++/37862), but none of the others. */
5232 if (*idk != CP_ID_KIND_QUALIFIED)
5233 *idk = CP_ID_KIND_NONE;
5235 /* The `>' token might be the end of a template-id or
5236 template-parameter-list now. */
5237 parser->greater_than_is_operator_p
5238 = saved_greater_than_is_operator_p;
5240 /* Consume the `)'. */
5241 token = cp_lexer_peek_token (parser->lexer);
5242 location_t close_paren_loc = token->location;
5243 expr.set_range (open_paren_loc, close_paren_loc);
5244 if (!parens.require_close (parser)
5245 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5246 cp_parser_skip_to_end_of_statement (parser);
5248 return expr;
5251 case CPP_OPEN_SQUARE:
5253 if (c_dialect_objc ())
5255 /* We might have an Objective-C++ message. */
5256 cp_parser_parse_tentatively (parser);
5257 tree msg = cp_parser_objc_message_expression (parser);
5258 /* If that works out, we're done ... */
5259 if (cp_parser_parse_definitely (parser))
5260 return msg;
5261 /* ... else, fall though to see if it's a lambda. */
5263 cp_expr lam = cp_parser_lambda_expression (parser);
5264 /* Don't warn about a failed tentative parse. */
5265 if (cp_parser_error_occurred (parser))
5266 return error_mark_node;
5267 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5268 return lam;
5271 case CPP_OBJC_STRING:
5272 if (c_dialect_objc ())
5273 /* We have an Objective-C++ string literal. */
5274 return cp_parser_objc_expression (parser);
5275 cp_parser_error (parser, "expected primary-expression");
5276 return error_mark_node;
5278 case CPP_KEYWORD:
5279 switch (token->keyword)
5281 /* These two are the boolean literals. */
5282 case RID_TRUE:
5283 cp_lexer_consume_token (parser->lexer);
5284 return cp_expr (boolean_true_node, token->location);
5285 case RID_FALSE:
5286 cp_lexer_consume_token (parser->lexer);
5287 return cp_expr (boolean_false_node, token->location);
5289 /* The `__null' literal. */
5290 case RID_NULL:
5291 cp_lexer_consume_token (parser->lexer);
5292 return cp_expr (null_node, token->location);
5294 /* The `nullptr' literal. */
5295 case RID_NULLPTR:
5296 cp_lexer_consume_token (parser->lexer);
5297 return cp_expr (nullptr_node, token->location);
5299 /* Recognize the `this' keyword. */
5300 case RID_THIS:
5301 cp_lexer_consume_token (parser->lexer);
5302 if (parser->local_variables_forbidden_p)
5304 error_at (token->location,
5305 "%<this%> may not be used in this context");
5306 return error_mark_node;
5308 /* Pointers cannot appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5310 return error_mark_node;
5311 return cp_expr (finish_this_expr (), token->location);
5313 /* The `operator' keyword can be the beginning of an
5314 id-expression. */
5315 case RID_OPERATOR:
5316 goto id_expression;
5318 case RID_FUNCTION_NAME:
5319 case RID_PRETTY_FUNCTION_NAME:
5320 case RID_C99_FUNCTION_NAME:
5322 non_integral_constant name;
5324 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5325 __func__ are the names of variables -- but they are
5326 treated specially. Therefore, they are handled here,
5327 rather than relying on the generic id-expression logic
5328 below. Grammatically, these names are id-expressions.
5330 Consume the token. */
5331 token = cp_lexer_consume_token (parser->lexer);
5333 switch (token->keyword)
5335 case RID_FUNCTION_NAME:
5336 name = NIC_FUNC_NAME;
5337 break;
5338 case RID_PRETTY_FUNCTION_NAME:
5339 name = NIC_PRETTY_FUNC;
5340 break;
5341 case RID_C99_FUNCTION_NAME:
5342 name = NIC_C99_FUNC;
5343 break;
5344 default:
5345 gcc_unreachable ();
5348 if (cp_parser_non_integral_constant_expression (parser, name))
5349 return error_mark_node;
5351 /* Look up the name. */
5352 return finish_fname (token->u.value);
5355 case RID_VA_ARG:
5357 tree expression;
5358 tree type;
5359 source_location type_location;
5360 location_t start_loc
5361 = cp_lexer_peek_token (parser->lexer)->location;
5362 /* The `__builtin_va_arg' construct is used to handle
5363 `va_arg'. Consume the `__builtin_va_arg' token. */
5364 cp_lexer_consume_token (parser->lexer);
5365 /* Look for the opening `('. */
5366 matching_parens parens;
5367 parens.require_open (parser);
5368 /* Now, parse the assignment-expression. */
5369 expression = cp_parser_assignment_expression (parser);
5370 /* Look for the `,'. */
5371 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5372 type_location = cp_lexer_peek_token (parser->lexer)->location;
5373 /* Parse the type-id. */
5375 type_id_in_expr_sentinel s (parser);
5376 type = cp_parser_type_id (parser);
5378 /* Look for the closing `)'. */
5379 location_t finish_loc
5380 = cp_lexer_peek_token (parser->lexer)->location;
5381 parens.require_close (parser);
5382 /* Using `va_arg' in a constant-expression is not
5383 allowed. */
5384 if (cp_parser_non_integral_constant_expression (parser,
5385 NIC_VA_ARG))
5386 return error_mark_node;
5387 /* Construct a location of the form:
5388 __builtin_va_arg (v, int)
5389 ~~~~~~~~~~~~~~~~~~~~~^~~~
5390 with the caret at the type, ranging from the start of the
5391 "__builtin_va_arg" token to the close paren. */
5392 location_t combined_loc
5393 = make_location (type_location, start_loc, finish_loc);
5394 return build_x_va_arg (combined_loc, expression, type);
5397 case RID_OFFSETOF:
5398 return cp_parser_builtin_offsetof (parser);
5400 case RID_HAS_NOTHROW_ASSIGN:
5401 case RID_HAS_NOTHROW_CONSTRUCTOR:
5402 case RID_HAS_NOTHROW_COPY:
5403 case RID_HAS_TRIVIAL_ASSIGN:
5404 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5405 case RID_HAS_TRIVIAL_COPY:
5406 case RID_HAS_TRIVIAL_DESTRUCTOR:
5407 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5408 case RID_HAS_VIRTUAL_DESTRUCTOR:
5409 case RID_IS_ABSTRACT:
5410 case RID_IS_AGGREGATE:
5411 case RID_IS_BASE_OF:
5412 case RID_IS_CLASS:
5413 case RID_IS_EMPTY:
5414 case RID_IS_ENUM:
5415 case RID_IS_FINAL:
5416 case RID_IS_LITERAL_TYPE:
5417 case RID_IS_POD:
5418 case RID_IS_POLYMORPHIC:
5419 case RID_IS_SAME_AS:
5420 case RID_IS_STD_LAYOUT:
5421 case RID_IS_TRIVIAL:
5422 case RID_IS_TRIVIALLY_ASSIGNABLE:
5423 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5424 case RID_IS_TRIVIALLY_COPYABLE:
5425 case RID_IS_UNION:
5426 case RID_IS_ASSIGNABLE:
5427 case RID_IS_CONSTRUCTIBLE:
5428 return cp_parser_trait_expr (parser, token->keyword);
5430 // C++ concepts
5431 case RID_REQUIRES:
5432 return cp_parser_requires_expression (parser);
5434 /* Objective-C++ expressions. */
5435 case RID_AT_ENCODE:
5436 case RID_AT_PROTOCOL:
5437 case RID_AT_SELECTOR:
5438 return cp_parser_objc_expression (parser);
5440 case RID_TEMPLATE:
5441 if (parser->in_function_body
5442 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5443 == CPP_LESS))
5445 error_at (token->location,
5446 "a template declaration cannot appear at block scope");
5447 cp_parser_skip_to_end_of_block_or_statement (parser);
5448 return error_mark_node;
5450 /* FALLTHRU */
5451 default:
5452 cp_parser_error (parser, "expected primary-expression");
5453 return error_mark_node;
5456 /* An id-expression can start with either an identifier, a
5457 `::' as the beginning of a qualified-id, or the "operator"
5458 keyword. */
5459 case CPP_NAME:
5460 case CPP_SCOPE:
5461 case CPP_TEMPLATE_ID:
5462 case CPP_NESTED_NAME_SPECIFIER:
5464 id_expression:
5465 cp_expr id_expression;
5466 cp_expr decl;
5467 const char *error_msg;
5468 bool template_p;
5469 bool done;
5470 cp_token *id_expr_token;
5472 /* Parse the id-expression. */
5473 id_expression
5474 = cp_parser_id_expression (parser,
5475 /*template_keyword_p=*/false,
5476 /*check_dependency_p=*/true,
5477 &template_p,
5478 /*declarator_p=*/false,
5479 /*optional_p=*/false);
5480 if (id_expression == error_mark_node)
5481 return error_mark_node;
5482 id_expr_token = token;
5483 token = cp_lexer_peek_token (parser->lexer);
5484 done = (token->type != CPP_OPEN_SQUARE
5485 && token->type != CPP_OPEN_PAREN
5486 && token->type != CPP_DOT
5487 && token->type != CPP_DEREF
5488 && token->type != CPP_PLUS_PLUS
5489 && token->type != CPP_MINUS_MINUS);
5490 /* If we have a template-id, then no further lookup is
5491 required. If the template-id was for a template-class, we
5492 will sometimes have a TYPE_DECL at this point. */
5493 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5494 || TREE_CODE (id_expression) == TYPE_DECL)
5495 decl = id_expression;
5496 /* Look up the name. */
5497 else
5499 tree ambiguous_decls;
5501 /* If we already know that this lookup is ambiguous, then
5502 we've already issued an error message; there's no reason
5503 to check again. */
5504 if (id_expr_token->type == CPP_NAME
5505 && id_expr_token->error_reported)
5507 cp_parser_simulate_error (parser);
5508 return error_mark_node;
5511 decl = cp_parser_lookup_name (parser, id_expression,
5512 none_type,
5513 template_p,
5514 /*is_namespace=*/false,
5515 /*check_dependency=*/true,
5516 &ambiguous_decls,
5517 id_expr_token->location);
5518 /* If the lookup was ambiguous, an error will already have
5519 been issued. */
5520 if (ambiguous_decls)
5521 return error_mark_node;
5523 /* In Objective-C++, we may have an Objective-C 2.0
5524 dot-syntax for classes here. */
5525 if (c_dialect_objc ()
5526 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5527 && TREE_CODE (decl) == TYPE_DECL
5528 && objc_is_class_name (decl))
5530 tree component;
5531 cp_lexer_consume_token (parser->lexer);
5532 component = cp_parser_identifier (parser);
5533 if (component == error_mark_node)
5534 return error_mark_node;
5536 tree result = objc_build_class_component_ref (id_expression,
5537 component);
5538 /* Build a location of the form:
5539 expr.component
5540 ~~~~~^~~~~~~~~
5541 with caret at the start of the component name (at
5542 input_location), ranging from the start of the id_expression
5543 to the end of the component name. */
5544 location_t combined_loc
5545 = make_location (input_location, id_expression.get_start (),
5546 get_finish (input_location));
5547 protected_set_expr_location (result, combined_loc);
5548 return result;
5551 /* In Objective-C++, an instance variable (ivar) may be preferred
5552 to whatever cp_parser_lookup_name() found.
5553 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5554 rest of c-family, we have to do a little extra work to preserve
5555 any location information in cp_expr "decl". Given that
5556 objc_lookup_ivar is implemented in "c-family" and "objc", we
5557 have a trip through the pure "tree" type, rather than cp_expr.
5558 Naively copying it back to "decl" would implicitly give the
5559 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5560 store an EXPR_LOCATION. Hence we only update "decl" (and
5561 hence its location_t) if we get back a different tree node. */
5562 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5563 id_expression);
5564 if (decl_tree != decl.get_value ())
5565 decl = cp_expr (decl_tree);
5567 /* If name lookup gives us a SCOPE_REF, then the
5568 qualifying scope was dependent. */
5569 if (TREE_CODE (decl) == SCOPE_REF)
5571 /* At this point, we do not know if DECL is a valid
5572 integral constant expression. We assume that it is
5573 in fact such an expression, so that code like:
5575 template <int N> struct A {
5576 int a[B<N>::i];
5579 is accepted. At template-instantiation time, we
5580 will check that B<N>::i is actually a constant. */
5581 return decl;
5583 /* Check to see if DECL is a local variable in a context
5584 where that is forbidden. */
5585 if (parser->local_variables_forbidden_p
5586 && local_variable_p (decl))
5588 /* It might be that we only found DECL because we are
5589 trying to be generous with pre-ISO scoping rules.
5590 For example, consider:
5592 int i;
5593 void g() {
5594 for (int i = 0; i < 10; ++i) {}
5595 extern void f(int j = i);
5598 Here, name look up will originally find the out
5599 of scope `i'. We need to issue a warning message,
5600 but then use the global `i'. */
5601 decl = check_for_out_of_scope_variable (decl);
5602 if (local_variable_p (decl))
5604 error_at (id_expr_token->location,
5605 "local variable %qD may not appear in this context",
5606 decl.get_value ());
5607 return error_mark_node;
5612 decl = (finish_id_expression
5613 (id_expression, decl, parser->scope,
5614 idk,
5615 parser->integral_constant_expression_p,
5616 parser->allow_non_integral_constant_expression_p,
5617 &parser->non_integral_constant_expression_p,
5618 template_p, done, address_p,
5619 template_arg_p,
5620 &error_msg,
5621 id_expression.get_location ()));
5622 if (error_msg)
5623 cp_parser_error (parser, error_msg);
5624 decl.set_location (id_expr_token->location);
5625 return decl;
5628 /* Anything else is an error. */
5629 default:
5630 cp_parser_error (parser, "expected primary-expression");
5631 return error_mark_node;
5635 static inline cp_expr
5636 cp_parser_primary_expression (cp_parser *parser,
5637 bool address_p,
5638 bool cast_p,
5639 bool template_arg_p,
5640 cp_id_kind *idk)
5642 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5643 /*decltype*/false, idk);
5646 /* Parse an id-expression.
5648 id-expression:
5649 unqualified-id
5650 qualified-id
5652 qualified-id:
5653 :: [opt] nested-name-specifier template [opt] unqualified-id
5654 :: identifier
5655 :: operator-function-id
5656 :: template-id
5658 Return a representation of the unqualified portion of the
5659 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5660 a `::' or nested-name-specifier.
5662 Often, if the id-expression was a qualified-id, the caller will
5663 want to make a SCOPE_REF to represent the qualified-id. This
5664 function does not do this in order to avoid wastefully creating
5665 SCOPE_REFs when they are not required.
5667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5668 `template' keyword.
5670 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5671 uninstantiated templates.
5673 If *TEMPLATE_P is non-NULL, it is set to true iff the
5674 `template' keyword is used to explicitly indicate that the entity
5675 named is a template.
5677 If DECLARATOR_P is true, the id-expression is appearing as part of
5678 a declarator, rather than as part of an expression. */
5680 static cp_expr
5681 cp_parser_id_expression (cp_parser *parser,
5682 bool template_keyword_p,
5683 bool check_dependency_p,
5684 bool *template_p,
5685 bool declarator_p,
5686 bool optional_p)
5688 bool global_scope_p;
5689 bool nested_name_specifier_p;
5691 /* Assume the `template' keyword was not used. */
5692 if (template_p)
5693 *template_p = template_keyword_p;
5695 /* Look for the optional `::' operator. */
5696 global_scope_p
5697 = (!template_keyword_p
5698 && (cp_parser_global_scope_opt (parser,
5699 /*current_scope_valid_p=*/false)
5700 != NULL_TREE));
5702 /* Look for the optional nested-name-specifier. */
5703 nested_name_specifier_p
5704 = (cp_parser_nested_name_specifier_opt (parser,
5705 /*typename_keyword_p=*/false,
5706 check_dependency_p,
5707 /*type_p=*/false,
5708 declarator_p,
5709 template_keyword_p)
5710 != NULL_TREE);
5712 /* If there is a nested-name-specifier, then we are looking at
5713 the first qualified-id production. */
5714 if (nested_name_specifier_p)
5716 tree saved_scope;
5717 tree saved_object_scope;
5718 tree saved_qualifying_scope;
5719 cp_expr unqualified_id;
5720 bool is_template;
5722 /* See if the next token is the `template' keyword. */
5723 if (!template_p)
5724 template_p = &is_template;
5725 *template_p = cp_parser_optional_template_keyword (parser);
5726 /* Name lookup we do during the processing of the
5727 unqualified-id might obliterate SCOPE. */
5728 saved_scope = parser->scope;
5729 saved_object_scope = parser->object_scope;
5730 saved_qualifying_scope = parser->qualifying_scope;
5731 /* Process the final unqualified-id. */
5732 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5733 check_dependency_p,
5734 declarator_p,
5735 /*optional_p=*/false);
5736 /* Restore the SAVED_SCOPE for our caller. */
5737 parser->scope = saved_scope;
5738 parser->object_scope = saved_object_scope;
5739 parser->qualifying_scope = saved_qualifying_scope;
5741 return unqualified_id;
5743 /* Otherwise, if we are in global scope, then we are looking at one
5744 of the other qualified-id productions. */
5745 else if (global_scope_p)
5747 cp_token *token;
5748 tree id;
5750 /* Peek at the next token. */
5751 token = cp_lexer_peek_token (parser->lexer);
5753 /* If it's an identifier, and the next token is not a "<", then
5754 we can avoid the template-id case. This is an optimization
5755 for this common case. */
5756 if (token->type == CPP_NAME
5757 && !cp_parser_nth_token_starts_template_argument_list_p
5758 (parser, 2))
5759 return cp_parser_identifier (parser);
5761 cp_parser_parse_tentatively (parser);
5762 /* Try a template-id. */
5763 id = cp_parser_template_id (parser,
5764 /*template_keyword_p=*/false,
5765 /*check_dependency_p=*/true,
5766 none_type,
5767 declarator_p);
5768 /* If that worked, we're done. */
5769 if (cp_parser_parse_definitely (parser))
5770 return id;
5772 /* Peek at the next token. (Changes in the token buffer may
5773 have invalidated the pointer obtained above.) */
5774 token = cp_lexer_peek_token (parser->lexer);
5776 switch (token->type)
5778 case CPP_NAME:
5779 return cp_parser_identifier (parser);
5781 case CPP_KEYWORD:
5782 if (token->keyword == RID_OPERATOR)
5783 return cp_parser_operator_function_id (parser);
5784 /* Fall through. */
5786 default:
5787 cp_parser_error (parser, "expected id-expression");
5788 return error_mark_node;
5791 else
5792 return cp_parser_unqualified_id (parser, template_keyword_p,
5793 /*check_dependency_p=*/true,
5794 declarator_p,
5795 optional_p);
5798 /* Parse an unqualified-id.
5800 unqualified-id:
5801 identifier
5802 operator-function-id
5803 conversion-function-id
5804 ~ class-name
5805 template-id
5807 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5808 keyword, in a construct like `A::template ...'.
5810 Returns a representation of unqualified-id. For the `identifier'
5811 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5812 production a BIT_NOT_EXPR is returned; the operand of the
5813 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5814 other productions, see the documentation accompanying the
5815 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5816 names are looked up in uninstantiated templates. If DECLARATOR_P
5817 is true, the unqualified-id is appearing as part of a declarator,
5818 rather than as part of an expression. */
5820 static cp_expr
5821 cp_parser_unqualified_id (cp_parser* parser,
5822 bool template_keyword_p,
5823 bool check_dependency_p,
5824 bool declarator_p,
5825 bool optional_p)
5827 cp_token *token;
5829 /* Peek at the next token. */
5830 token = cp_lexer_peek_token (parser->lexer);
5832 switch ((int) token->type)
5834 case CPP_NAME:
5836 tree id;
5838 /* We don't know yet whether or not this will be a
5839 template-id. */
5840 cp_parser_parse_tentatively (parser);
5841 /* Try a template-id. */
5842 id = cp_parser_template_id (parser, template_keyword_p,
5843 check_dependency_p,
5844 none_type,
5845 declarator_p);
5846 /* If it worked, we're done. */
5847 if (cp_parser_parse_definitely (parser))
5848 return id;
5849 /* Otherwise, it's an ordinary identifier. */
5850 return cp_parser_identifier (parser);
5853 case CPP_TEMPLATE_ID:
5854 return cp_parser_template_id (parser, template_keyword_p,
5855 check_dependency_p,
5856 none_type,
5857 declarator_p);
5859 case CPP_COMPL:
5861 tree type_decl;
5862 tree qualifying_scope;
5863 tree object_scope;
5864 tree scope;
5865 bool done;
5867 /* Consume the `~' token. */
5868 cp_lexer_consume_token (parser->lexer);
5869 /* Parse the class-name. The standard, as written, seems to
5870 say that:
5872 template <typename T> struct S { ~S (); };
5873 template <typename T> S<T>::~S() {}
5875 is invalid, since `~' must be followed by a class-name, but
5876 `S<T>' is dependent, and so not known to be a class.
5877 That's not right; we need to look in uninstantiated
5878 templates. A further complication arises from:
5880 template <typename T> void f(T t) {
5881 t.T::~T();
5884 Here, it is not possible to look up `T' in the scope of `T'
5885 itself. We must look in both the current scope, and the
5886 scope of the containing complete expression.
5888 Yet another issue is:
5890 struct S {
5891 int S;
5892 ~S();
5895 S::~S() {}
5897 The standard does not seem to say that the `S' in `~S'
5898 should refer to the type `S' and not the data member
5899 `S::S'. */
5901 /* DR 244 says that we look up the name after the "~" in the
5902 same scope as we looked up the qualifying name. That idea
5903 isn't fully worked out; it's more complicated than that. */
5904 scope = parser->scope;
5905 object_scope = parser->object_scope;
5906 qualifying_scope = parser->qualifying_scope;
5908 /* Check for invalid scopes. */
5909 if (scope == error_mark_node)
5911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5912 cp_lexer_consume_token (parser->lexer);
5913 return error_mark_node;
5915 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5917 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5918 error_at (token->location,
5919 "scope %qT before %<~%> is not a class-name",
5920 scope);
5921 cp_parser_simulate_error (parser);
5922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5923 cp_lexer_consume_token (parser->lexer);
5924 return error_mark_node;
5926 gcc_assert (!scope || TYPE_P (scope));
5928 /* If the name is of the form "X::~X" it's OK even if X is a
5929 typedef. */
5930 token = cp_lexer_peek_token (parser->lexer);
5931 if (scope
5932 && token->type == CPP_NAME
5933 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5934 != CPP_LESS)
5935 && (token->u.value == TYPE_IDENTIFIER (scope)
5936 || (CLASS_TYPE_P (scope)
5937 && constructor_name_p (token->u.value, scope))))
5939 cp_lexer_consume_token (parser->lexer);
5940 return build_nt (BIT_NOT_EXPR, scope);
5943 /* ~auto means the destructor of whatever the object is. */
5944 if (cp_parser_is_keyword (token, RID_AUTO))
5946 if (cxx_dialect < cxx14)
5947 pedwarn (input_location, 0,
5948 "%<~auto%> only available with "
5949 "-std=c++14 or -std=gnu++14");
5950 cp_lexer_consume_token (parser->lexer);
5951 return build_nt (BIT_NOT_EXPR, make_auto ());
5954 /* If there was an explicit qualification (S::~T), first look
5955 in the scope given by the qualification (i.e., S).
5957 Note: in the calls to cp_parser_class_name below we pass
5958 typename_type so that lookup finds the injected-class-name
5959 rather than the constructor. */
5960 done = false;
5961 type_decl = NULL_TREE;
5962 if (scope)
5964 cp_parser_parse_tentatively (parser);
5965 type_decl = cp_parser_class_name (parser,
5966 /*typename_keyword_p=*/false,
5967 /*template_keyword_p=*/false,
5968 typename_type,
5969 /*check_dependency=*/false,
5970 /*class_head_p=*/false,
5971 declarator_p);
5972 if (cp_parser_parse_definitely (parser))
5973 done = true;
5975 /* In "N::S::~S", look in "N" as well. */
5976 if (!done && scope && qualifying_scope)
5978 cp_parser_parse_tentatively (parser);
5979 parser->scope = qualifying_scope;
5980 parser->object_scope = NULL_TREE;
5981 parser->qualifying_scope = NULL_TREE;
5982 type_decl
5983 = cp_parser_class_name (parser,
5984 /*typename_keyword_p=*/false,
5985 /*template_keyword_p=*/false,
5986 typename_type,
5987 /*check_dependency=*/false,
5988 /*class_head_p=*/false,
5989 declarator_p);
5990 if (cp_parser_parse_definitely (parser))
5991 done = true;
5993 /* In "p->S::~T", look in the scope given by "*p" as well. */
5994 else if (!done && object_scope)
5996 cp_parser_parse_tentatively (parser);
5997 parser->scope = object_scope;
5998 parser->object_scope = NULL_TREE;
5999 parser->qualifying_scope = NULL_TREE;
6000 type_decl
6001 = cp_parser_class_name (parser,
6002 /*typename_keyword_p=*/false,
6003 /*template_keyword_p=*/false,
6004 typename_type,
6005 /*check_dependency=*/false,
6006 /*class_head_p=*/false,
6007 declarator_p);
6008 if (cp_parser_parse_definitely (parser))
6009 done = true;
6011 /* Look in the surrounding context. */
6012 if (!done)
6014 parser->scope = NULL_TREE;
6015 parser->object_scope = NULL_TREE;
6016 parser->qualifying_scope = NULL_TREE;
6017 if (processing_template_decl)
6018 cp_parser_parse_tentatively (parser);
6019 type_decl
6020 = cp_parser_class_name (parser,
6021 /*typename_keyword_p=*/false,
6022 /*template_keyword_p=*/false,
6023 typename_type,
6024 /*check_dependency=*/false,
6025 /*class_head_p=*/false,
6026 declarator_p);
6027 if (processing_template_decl
6028 && ! cp_parser_parse_definitely (parser))
6030 /* We couldn't find a type with this name. If we're parsing
6031 tentatively, fail and try something else. */
6032 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6034 cp_parser_simulate_error (parser);
6035 return error_mark_node;
6037 /* Otherwise, accept it and check for a match at instantiation
6038 time. */
6039 type_decl = cp_parser_identifier (parser);
6040 if (type_decl != error_mark_node)
6041 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6042 return type_decl;
6045 /* If an error occurred, assume that the name of the
6046 destructor is the same as the name of the qualifying
6047 class. That allows us to keep parsing after running
6048 into ill-formed destructor names. */
6049 if (type_decl == error_mark_node && scope)
6050 return build_nt (BIT_NOT_EXPR, scope);
6051 else if (type_decl == error_mark_node)
6052 return error_mark_node;
6054 /* Check that destructor name and scope match. */
6055 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6057 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6058 error_at (token->location,
6059 "declaration of %<~%T%> as member of %qT",
6060 type_decl, scope);
6061 cp_parser_simulate_error (parser);
6062 return error_mark_node;
6065 /* [class.dtor]
6067 A typedef-name that names a class shall not be used as the
6068 identifier in the declarator for a destructor declaration. */
6069 if (declarator_p
6070 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6071 && !DECL_SELF_REFERENCE_P (type_decl)
6072 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6073 error_at (token->location,
6074 "typedef-name %qD used as destructor declarator",
6075 type_decl);
6077 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6080 case CPP_KEYWORD:
6081 if (token->keyword == RID_OPERATOR)
6083 cp_expr id;
6085 /* This could be a template-id, so we try that first. */
6086 cp_parser_parse_tentatively (parser);
6087 /* Try a template-id. */
6088 id = cp_parser_template_id (parser, template_keyword_p,
6089 /*check_dependency_p=*/true,
6090 none_type,
6091 declarator_p);
6092 /* If that worked, we're done. */
6093 if (cp_parser_parse_definitely (parser))
6094 return id;
6095 /* We still don't know whether we're looking at an
6096 operator-function-id or a conversion-function-id. */
6097 cp_parser_parse_tentatively (parser);
6098 /* Try an operator-function-id. */
6099 id = cp_parser_operator_function_id (parser);
6100 /* If that didn't work, try a conversion-function-id. */
6101 if (!cp_parser_parse_definitely (parser))
6102 id = cp_parser_conversion_function_id (parser);
6104 return id;
6106 /* Fall through. */
6108 default:
6109 if (optional_p)
6110 return NULL_TREE;
6111 cp_parser_error (parser, "expected unqualified-id");
6112 return error_mark_node;
6116 /* Parse an (optional) nested-name-specifier.
6118 nested-name-specifier: [C++98]
6119 class-or-namespace-name :: nested-name-specifier [opt]
6120 class-or-namespace-name :: template nested-name-specifier [opt]
6122 nested-name-specifier: [C++0x]
6123 type-name ::
6124 namespace-name ::
6125 nested-name-specifier identifier ::
6126 nested-name-specifier template [opt] simple-template-id ::
6128 PARSER->SCOPE should be set appropriately before this function is
6129 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6130 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6131 in name lookups.
6133 Sets PARSER->SCOPE to the class (TYPE) or namespace
6134 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6135 it unchanged if there is no nested-name-specifier. Returns the new
6136 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6138 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6139 part of a declaration and/or decl-specifier. */
6141 static tree
6142 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6143 bool typename_keyword_p,
6144 bool check_dependency_p,
6145 bool type_p,
6146 bool is_declaration,
6147 bool template_keyword_p /* = false */)
6149 bool success = false;
6150 cp_token_position start = 0;
6151 cp_token *token;
6153 /* Remember where the nested-name-specifier starts. */
6154 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6156 start = cp_lexer_token_position (parser->lexer, false);
6157 push_deferring_access_checks (dk_deferred);
6160 while (true)
6162 tree new_scope;
6163 tree old_scope;
6164 tree saved_qualifying_scope;
6166 /* Spot cases that cannot be the beginning of a
6167 nested-name-specifier. */
6168 token = cp_lexer_peek_token (parser->lexer);
6170 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6171 the already parsed nested-name-specifier. */
6172 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6174 /* Grab the nested-name-specifier and continue the loop. */
6175 cp_parser_pre_parsed_nested_name_specifier (parser);
6176 /* If we originally encountered this nested-name-specifier
6177 with IS_DECLARATION set to false, we will not have
6178 resolved TYPENAME_TYPEs, so we must do so here. */
6179 if (is_declaration
6180 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6182 new_scope = resolve_typename_type (parser->scope,
6183 /*only_current_p=*/false);
6184 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6185 parser->scope = new_scope;
6187 success = true;
6188 continue;
6191 /* Spot cases that cannot be the beginning of a
6192 nested-name-specifier. On the second and subsequent times
6193 through the loop, we look for the `template' keyword. */
6194 if (success && token->keyword == RID_TEMPLATE)
6196 /* A template-id can start a nested-name-specifier. */
6197 else if (token->type == CPP_TEMPLATE_ID)
6199 /* DR 743: decltype can be used in a nested-name-specifier. */
6200 else if (token_is_decltype (token))
6202 else
6204 /* If the next token is not an identifier, then it is
6205 definitely not a type-name or namespace-name. */
6206 if (token->type != CPP_NAME)
6207 break;
6208 /* If the following token is neither a `<' (to begin a
6209 template-id), nor a `::', then we are not looking at a
6210 nested-name-specifier. */
6211 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6213 if (token->type == CPP_COLON
6214 && parser->colon_corrects_to_scope_p
6215 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6217 gcc_rich_location richloc (token->location);
6218 richloc.add_fixit_replace ("::");
6219 error_at (&richloc,
6220 "found %<:%> in nested-name-specifier, "
6221 "expected %<::%>");
6222 token->type = CPP_SCOPE;
6225 if (token->type != CPP_SCOPE
6226 && !cp_parser_nth_token_starts_template_argument_list_p
6227 (parser, 2))
6228 break;
6231 /* The nested-name-specifier is optional, so we parse
6232 tentatively. */
6233 cp_parser_parse_tentatively (parser);
6235 /* Look for the optional `template' keyword, if this isn't the
6236 first time through the loop. */
6237 if (success)
6238 template_keyword_p = cp_parser_optional_template_keyword (parser);
6240 /* Save the old scope since the name lookup we are about to do
6241 might destroy it. */
6242 old_scope = parser->scope;
6243 saved_qualifying_scope = parser->qualifying_scope;
6244 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6245 look up names in "X<T>::I" in order to determine that "Y" is
6246 a template. So, if we have a typename at this point, we make
6247 an effort to look through it. */
6248 if (is_declaration
6249 && !typename_keyword_p
6250 && parser->scope
6251 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6252 parser->scope = resolve_typename_type (parser->scope,
6253 /*only_current_p=*/false);
6254 /* Parse the qualifying entity. */
6255 new_scope
6256 = cp_parser_qualifying_entity (parser,
6257 typename_keyword_p,
6258 template_keyword_p,
6259 check_dependency_p,
6260 type_p,
6261 is_declaration);
6262 /* Look for the `::' token. */
6263 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6265 /* If we found what we wanted, we keep going; otherwise, we're
6266 done. */
6267 if (!cp_parser_parse_definitely (parser))
6269 bool error_p = false;
6271 /* Restore the OLD_SCOPE since it was valid before the
6272 failed attempt at finding the last
6273 class-or-namespace-name. */
6274 parser->scope = old_scope;
6275 parser->qualifying_scope = saved_qualifying_scope;
6277 /* If the next token is a decltype, and the one after that is a
6278 `::', then the decltype has failed to resolve to a class or
6279 enumeration type. Give this error even when parsing
6280 tentatively since it can't possibly be valid--and we're going
6281 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6282 won't get another chance.*/
6283 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6284 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6285 == CPP_SCOPE))
6287 token = cp_lexer_consume_token (parser->lexer);
6288 error_at (token->location, "decltype evaluates to %qT, "
6289 "which is not a class or enumeration type",
6290 token->u.tree_check_value->value);
6291 parser->scope = error_mark_node;
6292 error_p = true;
6293 /* As below. */
6294 success = true;
6295 cp_lexer_consume_token (parser->lexer);
6298 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6299 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6301 /* If we have a non-type template-id followed by ::, it can't
6302 possibly be valid. */
6303 token = cp_lexer_peek_token (parser->lexer);
6304 tree tid = token->u.tree_check_value->value;
6305 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6306 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6308 tree tmpl = NULL_TREE;
6309 if (is_overloaded_fn (tid))
6311 tree fns = get_fns (tid);
6312 if (OVL_SINGLE_P (fns))
6313 tmpl = OVL_FIRST (fns);
6314 error_at (token->location, "function template-id %qD "
6315 "in nested-name-specifier", tid);
6317 else
6319 /* Variable template. */
6320 tmpl = TREE_OPERAND (tid, 0);
6321 gcc_assert (variable_template_p (tmpl));
6322 error_at (token->location, "variable template-id %qD "
6323 "in nested-name-specifier", tid);
6325 if (tmpl)
6326 inform (DECL_SOURCE_LOCATION (tmpl),
6327 "%qD declared here", tmpl);
6329 parser->scope = error_mark_node;
6330 error_p = true;
6331 /* As below. */
6332 success = true;
6333 cp_lexer_consume_token (parser->lexer);
6334 cp_lexer_consume_token (parser->lexer);
6338 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6339 break;
6340 /* If the next token is an identifier, and the one after
6341 that is a `::', then any valid interpretation would have
6342 found a class-or-namespace-name. */
6343 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6344 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6345 == CPP_SCOPE)
6346 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6347 != CPP_COMPL))
6349 token = cp_lexer_consume_token (parser->lexer);
6350 if (!error_p)
6352 if (!token->error_reported)
6354 tree decl;
6355 tree ambiguous_decls;
6357 decl = cp_parser_lookup_name (parser, token->u.value,
6358 none_type,
6359 /*is_template=*/false,
6360 /*is_namespace=*/false,
6361 /*check_dependency=*/true,
6362 &ambiguous_decls,
6363 token->location);
6364 if (TREE_CODE (decl) == TEMPLATE_DECL)
6365 error_at (token->location,
6366 "%qD used without template parameters",
6367 decl);
6368 else if (ambiguous_decls)
6370 // cp_parser_lookup_name has the same diagnostic,
6371 // thus make sure to emit it at most once.
6372 if (cp_parser_uncommitted_to_tentative_parse_p
6373 (parser))
6375 error_at (token->location,
6376 "reference to %qD is ambiguous",
6377 token->u.value);
6378 print_candidates (ambiguous_decls);
6380 decl = error_mark_node;
6382 else
6384 if (cxx_dialect != cxx98)
6385 cp_parser_name_lookup_error
6386 (parser, token->u.value, decl, NLE_NOT_CXX98,
6387 token->location);
6388 else
6389 cp_parser_name_lookup_error
6390 (parser, token->u.value, decl, NLE_CXX98,
6391 token->location);
6394 parser->scope = error_mark_node;
6395 error_p = true;
6396 /* Treat this as a successful nested-name-specifier
6397 due to:
6399 [basic.lookup.qual]
6401 If the name found is not a class-name (clause
6402 _class_) or namespace-name (_namespace.def_), the
6403 program is ill-formed. */
6404 success = true;
6406 cp_lexer_consume_token (parser->lexer);
6408 break;
6410 /* We've found one valid nested-name-specifier. */
6411 success = true;
6412 /* Name lookup always gives us a DECL. */
6413 if (TREE_CODE (new_scope) == TYPE_DECL)
6414 new_scope = TREE_TYPE (new_scope);
6415 /* Uses of "template" must be followed by actual templates. */
6416 if (template_keyword_p
6417 && !(CLASS_TYPE_P (new_scope)
6418 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6419 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6420 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6421 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6422 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6423 == TEMPLATE_ID_EXPR)))
6424 permerror (input_location, TYPE_P (new_scope)
6425 ? G_("%qT is not a template")
6426 : G_("%qD is not a template"),
6427 new_scope);
6428 /* If it is a class scope, try to complete it; we are about to
6429 be looking up names inside the class. */
6430 if (TYPE_P (new_scope)
6431 /* Since checking types for dependency can be expensive,
6432 avoid doing it if the type is already complete. */
6433 && !COMPLETE_TYPE_P (new_scope)
6434 /* Do not try to complete dependent types. */
6435 && !dependent_type_p (new_scope))
6437 new_scope = complete_type (new_scope);
6438 /* If it is a typedef to current class, use the current
6439 class instead, as the typedef won't have any names inside
6440 it yet. */
6441 if (!COMPLETE_TYPE_P (new_scope)
6442 && currently_open_class (new_scope))
6443 new_scope = TYPE_MAIN_VARIANT (new_scope);
6445 /* Make sure we look in the right scope the next time through
6446 the loop. */
6447 parser->scope = new_scope;
6450 /* If parsing tentatively, replace the sequence of tokens that makes
6451 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6452 token. That way, should we re-parse the token stream, we will
6453 not have to repeat the effort required to do the parse, nor will
6454 we issue duplicate error messages. */
6455 if (success && start)
6457 cp_token *token;
6459 token = cp_lexer_token_at (parser->lexer, start);
6460 /* Reset the contents of the START token. */
6461 token->type = CPP_NESTED_NAME_SPECIFIER;
6462 /* Retrieve any deferred checks. Do not pop this access checks yet
6463 so the memory will not be reclaimed during token replacing below. */
6464 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6465 token->u.tree_check_value->value = parser->scope;
6466 token->u.tree_check_value->checks = get_deferred_access_checks ();
6467 token->u.tree_check_value->qualifying_scope =
6468 parser->qualifying_scope;
6469 token->keyword = RID_MAX;
6471 /* Purge all subsequent tokens. */
6472 cp_lexer_purge_tokens_after (parser->lexer, start);
6475 if (start)
6476 pop_to_parent_deferring_access_checks ();
6478 return success ? parser->scope : NULL_TREE;
6481 /* Parse a nested-name-specifier. See
6482 cp_parser_nested_name_specifier_opt for details. This function
6483 behaves identically, except that it will an issue an error if no
6484 nested-name-specifier is present. */
6486 static tree
6487 cp_parser_nested_name_specifier (cp_parser *parser,
6488 bool typename_keyword_p,
6489 bool check_dependency_p,
6490 bool type_p,
6491 bool is_declaration)
6493 tree scope;
6495 /* Look for the nested-name-specifier. */
6496 scope = cp_parser_nested_name_specifier_opt (parser,
6497 typename_keyword_p,
6498 check_dependency_p,
6499 type_p,
6500 is_declaration);
6501 /* If it was not present, issue an error message. */
6502 if (!scope)
6504 cp_parser_error (parser, "expected nested-name-specifier");
6505 parser->scope = NULL_TREE;
6508 return scope;
6511 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6512 this is either a class-name or a namespace-name (which corresponds
6513 to the class-or-namespace-name production in the grammar). For
6514 C++0x, it can also be a type-name that refers to an enumeration
6515 type or a simple-template-id.
6517 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6518 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6519 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6520 TYPE_P is TRUE iff the next name should be taken as a class-name,
6521 even the same name is declared to be another entity in the same
6522 scope.
6524 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6525 specified by the class-or-namespace-name. If neither is found the
6526 ERROR_MARK_NODE is returned. */
6528 static tree
6529 cp_parser_qualifying_entity (cp_parser *parser,
6530 bool typename_keyword_p,
6531 bool template_keyword_p,
6532 bool check_dependency_p,
6533 bool type_p,
6534 bool is_declaration)
6536 tree saved_scope;
6537 tree saved_qualifying_scope;
6538 tree saved_object_scope;
6539 tree scope;
6540 bool only_class_p;
6541 bool successful_parse_p;
6543 /* DR 743: decltype can appear in a nested-name-specifier. */
6544 if (cp_lexer_next_token_is_decltype (parser->lexer))
6546 scope = cp_parser_decltype (parser);
6547 if (TREE_CODE (scope) != ENUMERAL_TYPE
6548 && !MAYBE_CLASS_TYPE_P (scope))
6550 cp_parser_simulate_error (parser);
6551 return error_mark_node;
6553 if (TYPE_NAME (scope))
6554 scope = TYPE_NAME (scope);
6555 return scope;
6558 /* Before we try to parse the class-name, we must save away the
6559 current PARSER->SCOPE since cp_parser_class_name will destroy
6560 it. */
6561 saved_scope = parser->scope;
6562 saved_qualifying_scope = parser->qualifying_scope;
6563 saved_object_scope = parser->object_scope;
6564 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6565 there is no need to look for a namespace-name. */
6566 only_class_p = template_keyword_p
6567 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6568 if (!only_class_p)
6569 cp_parser_parse_tentatively (parser);
6570 scope = cp_parser_class_name (parser,
6571 typename_keyword_p,
6572 template_keyword_p,
6573 type_p ? class_type : none_type,
6574 check_dependency_p,
6575 /*class_head_p=*/false,
6576 is_declaration,
6577 /*enum_ok=*/cxx_dialect > cxx98);
6578 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6579 /* If that didn't work, try for a namespace-name. */
6580 if (!only_class_p && !successful_parse_p)
6582 /* Restore the saved scope. */
6583 parser->scope = saved_scope;
6584 parser->qualifying_scope = saved_qualifying_scope;
6585 parser->object_scope = saved_object_scope;
6586 /* If we are not looking at an identifier followed by the scope
6587 resolution operator, then this is not part of a
6588 nested-name-specifier. (Note that this function is only used
6589 to parse the components of a nested-name-specifier.) */
6590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6591 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6592 return error_mark_node;
6593 scope = cp_parser_namespace_name (parser);
6596 return scope;
6599 /* Return true if we are looking at a compound-literal, false otherwise. */
6601 static bool
6602 cp_parser_compound_literal_p (cp_parser *parser)
6604 cp_lexer_save_tokens (parser->lexer);
6606 /* Skip tokens until the next token is a closing parenthesis.
6607 If we find the closing `)', and the next token is a `{', then
6608 we are looking at a compound-literal. */
6609 bool compound_literal_p
6610 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6611 /*consume_paren=*/true)
6612 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6614 /* Roll back the tokens we skipped. */
6615 cp_lexer_rollback_tokens (parser->lexer);
6617 return compound_literal_p;
6620 /* Return true if EXPR is the integer constant zero or a complex constant
6621 of zero, without any folding, but ignoring location wrappers. */
6623 static bool
6624 literal_integer_zerop (const_tree expr)
6626 STRIP_ANY_LOCATION_WRAPPER (expr);
6627 return integer_zerop (expr);
6630 /* Parse a postfix-expression.
6632 postfix-expression:
6633 primary-expression
6634 postfix-expression [ expression ]
6635 postfix-expression ( expression-list [opt] )
6636 simple-type-specifier ( expression-list [opt] )
6637 typename :: [opt] nested-name-specifier identifier
6638 ( expression-list [opt] )
6639 typename :: [opt] nested-name-specifier template [opt] template-id
6640 ( expression-list [opt] )
6641 postfix-expression . template [opt] id-expression
6642 postfix-expression -> template [opt] id-expression
6643 postfix-expression . pseudo-destructor-name
6644 postfix-expression -> pseudo-destructor-name
6645 postfix-expression ++
6646 postfix-expression --
6647 dynamic_cast < type-id > ( expression )
6648 static_cast < type-id > ( expression )
6649 reinterpret_cast < type-id > ( expression )
6650 const_cast < type-id > ( expression )
6651 typeid ( expression )
6652 typeid ( type-id )
6654 GNU Extension:
6656 postfix-expression:
6657 ( type-id ) { initializer-list , [opt] }
6659 This extension is a GNU version of the C99 compound-literal
6660 construct. (The C99 grammar uses `type-name' instead of `type-id',
6661 but they are essentially the same concept.)
6663 If ADDRESS_P is true, the postfix expression is the operand of the
6664 `&' operator. CAST_P is true if this expression is the target of a
6665 cast.
6667 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6668 class member access expressions [expr.ref].
6670 Returns a representation of the expression. */
6672 static cp_expr
6673 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6674 bool member_access_only_p, bool decltype_p,
6675 cp_id_kind * pidk_return)
6677 cp_token *token;
6678 location_t loc;
6679 enum rid keyword;
6680 cp_id_kind idk = CP_ID_KIND_NONE;
6681 cp_expr postfix_expression = NULL_TREE;
6682 bool is_member_access = false;
6684 /* Peek at the next token. */
6685 token = cp_lexer_peek_token (parser->lexer);
6686 loc = token->location;
6687 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6689 /* Some of the productions are determined by keywords. */
6690 keyword = token->keyword;
6691 switch (keyword)
6693 case RID_DYNCAST:
6694 case RID_STATCAST:
6695 case RID_REINTCAST:
6696 case RID_CONSTCAST:
6698 tree type;
6699 cp_expr expression;
6700 const char *saved_message;
6701 bool saved_in_type_id_in_expr_p;
6703 /* All of these can be handled in the same way from the point
6704 of view of parsing. Begin by consuming the token
6705 identifying the cast. */
6706 cp_lexer_consume_token (parser->lexer);
6708 /* New types cannot be defined in the cast. */
6709 saved_message = parser->type_definition_forbidden_message;
6710 parser->type_definition_forbidden_message
6711 = G_("types may not be defined in casts");
6713 /* Look for the opening `<'. */
6714 cp_parser_require (parser, CPP_LESS, RT_LESS);
6715 /* Parse the type to which we are casting. */
6716 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6717 parser->in_type_id_in_expr_p = true;
6718 type = cp_parser_type_id (parser);
6719 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6720 /* Look for the closing `>'. */
6721 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6722 /* Restore the old message. */
6723 parser->type_definition_forbidden_message = saved_message;
6725 bool saved_greater_than_is_operator_p
6726 = parser->greater_than_is_operator_p;
6727 parser->greater_than_is_operator_p = true;
6729 /* And the expression which is being cast. */
6730 matching_parens parens;
6731 parens.require_open (parser);
6732 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6733 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6734 RT_CLOSE_PAREN);
6735 location_t end_loc = close_paren ?
6736 close_paren->location : UNKNOWN_LOCATION;
6738 parser->greater_than_is_operator_p
6739 = saved_greater_than_is_operator_p;
6741 /* Only type conversions to integral or enumeration types
6742 can be used in constant-expressions. */
6743 if (!cast_valid_in_integral_constant_expression_p (type)
6744 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6746 postfix_expression = error_mark_node;
6747 break;
6750 switch (keyword)
6752 case RID_DYNCAST:
6753 postfix_expression
6754 = build_dynamic_cast (type, expression, tf_warning_or_error);
6755 break;
6756 case RID_STATCAST:
6757 postfix_expression
6758 = build_static_cast (type, expression, tf_warning_or_error);
6759 break;
6760 case RID_REINTCAST:
6761 postfix_expression
6762 = build_reinterpret_cast (type, expression,
6763 tf_warning_or_error);
6764 break;
6765 case RID_CONSTCAST:
6766 postfix_expression
6767 = build_const_cast (type, expression, tf_warning_or_error);
6768 break;
6769 default:
6770 gcc_unreachable ();
6773 /* Construct a location e.g. :
6774 reinterpret_cast <int *> (expr)
6775 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6776 ranging from the start of the "*_cast" token to the final closing
6777 paren, with the caret at the start. */
6778 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6779 postfix_expression.set_location (cp_cast_loc);
6781 break;
6783 case RID_TYPEID:
6785 tree type;
6786 const char *saved_message;
6787 bool saved_in_type_id_in_expr_p;
6789 /* Consume the `typeid' token. */
6790 cp_lexer_consume_token (parser->lexer);
6791 /* Look for the `(' token. */
6792 matching_parens parens;
6793 parens.require_open (parser);
6794 /* Types cannot be defined in a `typeid' expression. */
6795 saved_message = parser->type_definition_forbidden_message;
6796 parser->type_definition_forbidden_message
6797 = G_("types may not be defined in a %<typeid%> expression");
6798 /* We can't be sure yet whether we're looking at a type-id or an
6799 expression. */
6800 cp_parser_parse_tentatively (parser);
6801 /* Try a type-id first. */
6802 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6803 parser->in_type_id_in_expr_p = true;
6804 type = cp_parser_type_id (parser);
6805 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6806 /* Look for the `)' token. Otherwise, we can't be sure that
6807 we're not looking at an expression: consider `typeid (int
6808 (3))', for example. */
6809 cp_token *close_paren = parens.require_close (parser);
6810 /* If all went well, simply lookup the type-id. */
6811 if (cp_parser_parse_definitely (parser))
6812 postfix_expression = get_typeid (type, tf_warning_or_error);
6813 /* Otherwise, fall back to the expression variant. */
6814 else
6816 tree expression;
6818 /* Look for an expression. */
6819 expression = cp_parser_expression (parser, & idk);
6820 /* Compute its typeid. */
6821 postfix_expression = build_typeid (expression, tf_warning_or_error);
6822 /* Look for the `)' token. */
6823 close_paren = parens.require_close (parser);
6825 /* Restore the saved message. */
6826 parser->type_definition_forbidden_message = saved_message;
6827 /* `typeid' may not appear in an integral constant expression. */
6828 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6829 postfix_expression = error_mark_node;
6831 /* Construct a location e.g. :
6832 typeid (expr)
6833 ^~~~~~~~~~~~~
6834 ranging from the start of the "typeid" token to the final closing
6835 paren, with the caret at the start. */
6836 if (close_paren)
6838 location_t typeid_loc
6839 = make_location (start_loc, start_loc, close_paren->location);
6840 postfix_expression.set_location (typeid_loc);
6841 postfix_expression.maybe_add_location_wrapper ();
6844 break;
6846 case RID_TYPENAME:
6848 tree type;
6849 /* The syntax permitted here is the same permitted for an
6850 elaborated-type-specifier. */
6851 ++parser->prevent_constrained_type_specifiers;
6852 type = cp_parser_elaborated_type_specifier (parser,
6853 /*is_friend=*/false,
6854 /*is_declaration=*/false);
6855 --parser->prevent_constrained_type_specifiers;
6856 postfix_expression = cp_parser_functional_cast (parser, type);
6858 break;
6860 case RID_ADDRESSOF:
6861 case RID_BUILTIN_SHUFFLE:
6862 case RID_BUILTIN_LAUNDER:
6864 vec<tree, va_gc> *vec;
6865 unsigned int i;
6866 tree p;
6868 cp_lexer_consume_token (parser->lexer);
6869 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6870 /*cast_p=*/false, /*allow_expansion_p=*/true,
6871 /*non_constant_p=*/NULL);
6872 if (vec == NULL)
6874 postfix_expression = error_mark_node;
6875 break;
6878 FOR_EACH_VEC_ELT (*vec, i, p)
6879 mark_exp_read (p);
6881 switch (keyword)
6883 case RID_ADDRESSOF:
6884 if (vec->length () == 1)
6885 postfix_expression
6886 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6887 else
6889 error_at (loc, "wrong number of arguments to "
6890 "%<__builtin_addressof%>");
6891 postfix_expression = error_mark_node;
6893 break;
6895 case RID_BUILTIN_LAUNDER:
6896 if (vec->length () == 1)
6897 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6898 tf_warning_or_error);
6899 else
6901 error_at (loc, "wrong number of arguments to "
6902 "%<__builtin_launder%>");
6903 postfix_expression = error_mark_node;
6905 break;
6907 case RID_BUILTIN_SHUFFLE:
6908 if (vec->length () == 2)
6909 postfix_expression
6910 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6911 (*vec)[1], tf_warning_or_error);
6912 else if (vec->length () == 3)
6913 postfix_expression
6914 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6915 (*vec)[2], tf_warning_or_error);
6916 else
6918 error_at (loc, "wrong number of arguments to "
6919 "%<__builtin_shuffle%>");
6920 postfix_expression = error_mark_node;
6922 break;
6924 default:
6925 gcc_unreachable ();
6927 break;
6930 default:
6932 tree type;
6934 /* If the next thing is a simple-type-specifier, we may be
6935 looking at a functional cast. We could also be looking at
6936 an id-expression. So, we try the functional cast, and if
6937 that doesn't work we fall back to the primary-expression. */
6938 cp_parser_parse_tentatively (parser);
6939 /* Look for the simple-type-specifier. */
6940 ++parser->prevent_constrained_type_specifiers;
6941 type = cp_parser_simple_type_specifier (parser,
6942 /*decl_specs=*/NULL,
6943 CP_PARSER_FLAGS_NONE);
6944 --parser->prevent_constrained_type_specifiers;
6945 /* Parse the cast itself. */
6946 if (!cp_parser_error_occurred (parser))
6947 postfix_expression
6948 = cp_parser_functional_cast (parser, type);
6949 /* If that worked, we're done. */
6950 if (cp_parser_parse_definitely (parser))
6951 break;
6953 /* If the functional-cast didn't work out, try a
6954 compound-literal. */
6955 if (cp_parser_allow_gnu_extensions_p (parser)
6956 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6958 cp_expr initializer = NULL_TREE;
6960 cp_parser_parse_tentatively (parser);
6962 matching_parens parens;
6963 parens.consume_open (parser);
6965 /* Avoid calling cp_parser_type_id pointlessly, see comment
6966 in cp_parser_cast_expression about c++/29234. */
6967 if (!cp_parser_compound_literal_p (parser))
6968 cp_parser_simulate_error (parser);
6969 else
6971 /* Parse the type. */
6972 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6973 parser->in_type_id_in_expr_p = true;
6974 type = cp_parser_type_id (parser);
6975 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6976 parens.require_close (parser);
6979 /* If things aren't going well, there's no need to
6980 keep going. */
6981 if (!cp_parser_error_occurred (parser))
6983 bool non_constant_p;
6984 /* Parse the brace-enclosed initializer list. */
6985 initializer = cp_parser_braced_list (parser,
6986 &non_constant_p);
6988 /* If that worked, we're definitely looking at a
6989 compound-literal expression. */
6990 if (cp_parser_parse_definitely (parser))
6992 /* Warn the user that a compound literal is not
6993 allowed in standard C++. */
6994 pedwarn (input_location, OPT_Wpedantic,
6995 "ISO C++ forbids compound-literals");
6996 /* For simplicity, we disallow compound literals in
6997 constant-expressions. We could
6998 allow compound literals of integer type, whose
6999 initializer was a constant, in constant
7000 expressions. Permitting that usage, as a further
7001 extension, would not change the meaning of any
7002 currently accepted programs. (Of course, as
7003 compound literals are not part of ISO C++, the
7004 standard has nothing to say.) */
7005 if (cp_parser_non_integral_constant_expression (parser,
7006 NIC_NCC))
7008 postfix_expression = error_mark_node;
7009 break;
7011 /* Form the representation of the compound-literal. */
7012 postfix_expression
7013 = finish_compound_literal (type, initializer,
7014 tf_warning_or_error, fcl_c99);
7015 postfix_expression.set_location (initializer.get_location ());
7016 break;
7020 /* It must be a primary-expression. */
7021 postfix_expression
7022 = cp_parser_primary_expression (parser, address_p, cast_p,
7023 /*template_arg_p=*/false,
7024 decltype_p,
7025 &idk);
7027 break;
7030 /* Note that we don't need to worry about calling build_cplus_new on a
7031 class-valued CALL_EXPR in decltype when it isn't the end of the
7032 postfix-expression; unary_complex_lvalue will take care of that for
7033 all these cases. */
7035 /* Keep looping until the postfix-expression is complete. */
7036 while (true)
7038 if (idk == CP_ID_KIND_UNQUALIFIED
7039 && identifier_p (postfix_expression)
7040 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7041 /* It is not a Koenig lookup function call. */
7042 postfix_expression
7043 = unqualified_name_lookup_error (postfix_expression);
7045 /* Peek at the next token. */
7046 token = cp_lexer_peek_token (parser->lexer);
7048 switch (token->type)
7050 case CPP_OPEN_SQUARE:
7051 if (cp_next_tokens_can_be_std_attribute_p (parser))
7053 cp_parser_error (parser,
7054 "two consecutive %<[%> shall "
7055 "only introduce an attribute");
7056 return error_mark_node;
7058 postfix_expression
7059 = cp_parser_postfix_open_square_expression (parser,
7060 postfix_expression,
7061 false,
7062 decltype_p);
7063 postfix_expression.set_range (start_loc,
7064 postfix_expression.get_location ());
7066 idk = CP_ID_KIND_NONE;
7067 is_member_access = false;
7068 break;
7070 case CPP_OPEN_PAREN:
7071 /* postfix-expression ( expression-list [opt] ) */
7073 bool koenig_p;
7074 bool is_builtin_constant_p;
7075 bool saved_integral_constant_expression_p = false;
7076 bool saved_non_integral_constant_expression_p = false;
7077 tsubst_flags_t complain = complain_flags (decltype_p);
7078 vec<tree, va_gc> *args;
7079 location_t close_paren_loc = UNKNOWN_LOCATION;
7081 is_member_access = false;
7083 is_builtin_constant_p
7084 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7085 if (is_builtin_constant_p)
7087 /* The whole point of __builtin_constant_p is to allow
7088 non-constant expressions to appear as arguments. */
7089 saved_integral_constant_expression_p
7090 = parser->integral_constant_expression_p;
7091 saved_non_integral_constant_expression_p
7092 = parser->non_integral_constant_expression_p;
7093 parser->integral_constant_expression_p = false;
7095 args = (cp_parser_parenthesized_expression_list
7096 (parser, non_attr,
7097 /*cast_p=*/false, /*allow_expansion_p=*/true,
7098 /*non_constant_p=*/NULL,
7099 /*close_paren_loc=*/&close_paren_loc,
7100 /*wrap_locations_p=*/true));
7101 if (is_builtin_constant_p)
7103 parser->integral_constant_expression_p
7104 = saved_integral_constant_expression_p;
7105 parser->non_integral_constant_expression_p
7106 = saved_non_integral_constant_expression_p;
7109 if (args == NULL)
7111 postfix_expression = error_mark_node;
7112 break;
7115 /* Function calls are not permitted in
7116 constant-expressions. */
7117 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7118 && cp_parser_non_integral_constant_expression (parser,
7119 NIC_FUNC_CALL))
7121 postfix_expression = error_mark_node;
7122 release_tree_vector (args);
7123 break;
7126 koenig_p = false;
7127 if (idk == CP_ID_KIND_UNQUALIFIED
7128 || idk == CP_ID_KIND_TEMPLATE_ID)
7130 if (identifier_p (postfix_expression))
7132 if (!args->is_empty ())
7134 koenig_p = true;
7135 if (!any_type_dependent_arguments_p (args))
7136 postfix_expression
7137 = perform_koenig_lookup (postfix_expression, args,
7138 complain);
7140 else
7141 postfix_expression
7142 = unqualified_fn_lookup_error (postfix_expression);
7144 /* We do not perform argument-dependent lookup if
7145 normal lookup finds a non-function, in accordance
7146 with the expected resolution of DR 218. */
7147 else if (!args->is_empty ()
7148 && is_overloaded_fn (postfix_expression))
7150 tree fn = get_first_fn (postfix_expression);
7151 fn = STRIP_TEMPLATE (fn);
7153 /* Do not do argument dependent lookup if regular
7154 lookup finds a member function or a block-scope
7155 function declaration. [basic.lookup.argdep]/3 */
7156 if (!DECL_FUNCTION_MEMBER_P (fn)
7157 && !DECL_LOCAL_FUNCTION_P (fn))
7159 koenig_p = true;
7160 if (!any_type_dependent_arguments_p (args))
7161 postfix_expression
7162 = perform_koenig_lookup (postfix_expression, args,
7163 complain);
7168 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7169 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7170 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7171 && vec_safe_length (args) == 3)
7173 tree arg0 = (*args)[0];
7174 tree arg1 = (*args)[1];
7175 tree arg2 = (*args)[2];
7176 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7177 | (literal_integer_zerop (arg2) << 2));
7178 warn_for_memset (input_location, arg0, arg2, literal_mask);
7181 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7183 tree instance = TREE_OPERAND (postfix_expression, 0);
7184 tree fn = TREE_OPERAND (postfix_expression, 1);
7186 if (processing_template_decl
7187 && (type_dependent_object_expression_p (instance)
7188 || (!BASELINK_P (fn)
7189 && TREE_CODE (fn) != FIELD_DECL)
7190 || type_dependent_expression_p (fn)
7191 || any_type_dependent_arguments_p (args)))
7193 maybe_generic_this_capture (instance, fn);
7194 postfix_expression
7195 = build_min_nt_call_vec (postfix_expression, args);
7196 release_tree_vector (args);
7197 break;
7200 if (BASELINK_P (fn))
7202 postfix_expression
7203 = (build_new_method_call
7204 (instance, fn, &args, NULL_TREE,
7205 (idk == CP_ID_KIND_QUALIFIED
7206 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7207 : LOOKUP_NORMAL),
7208 /*fn_p=*/NULL,
7209 complain));
7211 else
7212 postfix_expression
7213 = finish_call_expr (postfix_expression, &args,
7214 /*disallow_virtual=*/false,
7215 /*koenig_p=*/false,
7216 complain);
7218 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7219 || TREE_CODE (postfix_expression) == MEMBER_REF
7220 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7221 postfix_expression = (build_offset_ref_call_from_tree
7222 (postfix_expression, &args,
7223 complain));
7224 else if (idk == CP_ID_KIND_QUALIFIED)
7225 /* A call to a static class member, or a namespace-scope
7226 function. */
7227 postfix_expression
7228 = finish_call_expr (postfix_expression, &args,
7229 /*disallow_virtual=*/true,
7230 koenig_p,
7231 complain);
7232 else
7233 /* All other function calls. */
7234 postfix_expression
7235 = finish_call_expr (postfix_expression, &args,
7236 /*disallow_virtual=*/false,
7237 koenig_p,
7238 complain);
7240 if (close_paren_loc != UNKNOWN_LOCATION)
7242 location_t combined_loc = make_location (token->location,
7243 start_loc,
7244 close_paren_loc);
7245 postfix_expression.set_location (combined_loc);
7248 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7249 idk = CP_ID_KIND_NONE;
7251 release_tree_vector (args);
7253 break;
7255 case CPP_DOT:
7256 case CPP_DEREF:
7257 /* postfix-expression . template [opt] id-expression
7258 postfix-expression . pseudo-destructor-name
7259 postfix-expression -> template [opt] id-expression
7260 postfix-expression -> pseudo-destructor-name */
7262 /* Consume the `.' or `->' operator. */
7263 cp_lexer_consume_token (parser->lexer);
7265 postfix_expression
7266 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7267 postfix_expression,
7268 false, &idk, loc);
7270 is_member_access = true;
7271 break;
7273 case CPP_PLUS_PLUS:
7274 /* postfix-expression ++ */
7275 /* Consume the `++' token. */
7276 cp_lexer_consume_token (parser->lexer);
7277 /* Generate a representation for the complete expression. */
7278 postfix_expression
7279 = finish_increment_expr (postfix_expression,
7280 POSTINCREMENT_EXPR);
7281 /* Increments may not appear in constant-expressions. */
7282 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7283 postfix_expression = error_mark_node;
7284 idk = CP_ID_KIND_NONE;
7285 is_member_access = false;
7286 break;
7288 case CPP_MINUS_MINUS:
7289 /* postfix-expression -- */
7290 /* Consume the `--' token. */
7291 cp_lexer_consume_token (parser->lexer);
7292 /* Generate a representation for the complete expression. */
7293 postfix_expression
7294 = finish_increment_expr (postfix_expression,
7295 POSTDECREMENT_EXPR);
7296 /* Decrements may not appear in constant-expressions. */
7297 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7298 postfix_expression = error_mark_node;
7299 idk = CP_ID_KIND_NONE;
7300 is_member_access = false;
7301 break;
7303 default:
7304 if (pidk_return != NULL)
7305 * pidk_return = idk;
7306 if (member_access_only_p)
7307 return is_member_access
7308 ? postfix_expression
7309 : cp_expr (error_mark_node);
7310 else
7311 return postfix_expression;
7315 /* We should never get here. */
7316 gcc_unreachable ();
7317 return error_mark_node;
7320 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7321 by cp_parser_builtin_offsetof. We're looking for
7323 postfix-expression [ expression ]
7324 postfix-expression [ braced-init-list ] (C++11)
7326 FOR_OFFSETOF is set if we're being called in that context, which
7327 changes how we deal with integer constant expressions. */
7329 static tree
7330 cp_parser_postfix_open_square_expression (cp_parser *parser,
7331 tree postfix_expression,
7332 bool for_offsetof,
7333 bool decltype_p)
7335 tree index = NULL_TREE;
7336 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7337 bool saved_greater_than_is_operator_p;
7339 /* Consume the `[' token. */
7340 cp_lexer_consume_token (parser->lexer);
7342 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7343 parser->greater_than_is_operator_p = true;
7345 /* Parse the index expression. */
7346 /* ??? For offsetof, there is a question of what to allow here. If
7347 offsetof is not being used in an integral constant expression context,
7348 then we *could* get the right answer by computing the value at runtime.
7349 If we are in an integral constant expression context, then we might
7350 could accept any constant expression; hard to say without analysis.
7351 Rather than open the barn door too wide right away, allow only integer
7352 constant expressions here. */
7353 if (for_offsetof)
7354 index = cp_parser_constant_expression (parser);
7355 else
7357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7359 bool expr_nonconst_p;
7360 cp_lexer_set_source_position (parser->lexer);
7361 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7362 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7364 else
7365 index = cp_parser_expression (parser);
7368 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7370 /* Look for the closing `]'. */
7371 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7373 /* Build the ARRAY_REF. */
7374 postfix_expression = grok_array_decl (loc, postfix_expression,
7375 index, decltype_p);
7377 /* When not doing offsetof, array references are not permitted in
7378 constant-expressions. */
7379 if (!for_offsetof
7380 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7381 postfix_expression = error_mark_node;
7383 return postfix_expression;
7386 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7387 dereference of incomplete type, returns true if error_mark_node should
7388 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7389 and *DEPENDENT_P. */
7391 bool
7392 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7393 bool *dependent_p)
7395 /* In a template, be permissive by treating an object expression
7396 of incomplete type as dependent (after a pedwarn). */
7397 diagnostic_t kind = (processing_template_decl
7398 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7400 switch (TREE_CODE (*postfix_expression))
7402 case CAST_EXPR:
7403 case REINTERPRET_CAST_EXPR:
7404 case CONST_CAST_EXPR:
7405 case STATIC_CAST_EXPR:
7406 case DYNAMIC_CAST_EXPR:
7407 case IMPLICIT_CONV_EXPR:
7408 case VIEW_CONVERT_EXPR:
7409 case NON_LVALUE_EXPR:
7410 kind = DK_ERROR;
7411 break;
7412 case OVERLOAD:
7413 /* Don't emit any diagnostic for OVERLOADs. */
7414 kind = DK_IGNORED;
7415 break;
7416 default:
7417 /* Avoid clobbering e.g. DECLs. */
7418 if (!EXPR_P (*postfix_expression))
7419 kind = DK_ERROR;
7420 break;
7423 if (kind == DK_IGNORED)
7424 return false;
7426 location_t exploc = location_of (*postfix_expression);
7427 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7428 if (!MAYBE_CLASS_TYPE_P (*scope))
7429 return true;
7430 if (kind == DK_ERROR)
7431 *scope = *postfix_expression = error_mark_node;
7432 else if (processing_template_decl)
7434 *dependent_p = true;
7435 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7437 return false;
7440 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7441 by cp_parser_builtin_offsetof. We're looking for
7443 postfix-expression . template [opt] id-expression
7444 postfix-expression . pseudo-destructor-name
7445 postfix-expression -> template [opt] id-expression
7446 postfix-expression -> pseudo-destructor-name
7448 FOR_OFFSETOF is set if we're being called in that context. That sorta
7449 limits what of the above we'll actually accept, but nevermind.
7450 TOKEN_TYPE is the "." or "->" token, which will already have been
7451 removed from the stream. */
7453 static tree
7454 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7455 enum cpp_ttype token_type,
7456 cp_expr postfix_expression,
7457 bool for_offsetof, cp_id_kind *idk,
7458 location_t location)
7460 tree name;
7461 bool dependent_p;
7462 bool pseudo_destructor_p;
7463 tree scope = NULL_TREE;
7464 location_t start_loc = postfix_expression.get_start ();
7466 /* If this is a `->' operator, dereference the pointer. */
7467 if (token_type == CPP_DEREF)
7468 postfix_expression = build_x_arrow (location, postfix_expression,
7469 tf_warning_or_error);
7470 /* Check to see whether or not the expression is type-dependent and
7471 not the current instantiation. */
7472 dependent_p = type_dependent_object_expression_p (postfix_expression);
7473 /* The identifier following the `->' or `.' is not qualified. */
7474 parser->scope = NULL_TREE;
7475 parser->qualifying_scope = NULL_TREE;
7476 parser->object_scope = NULL_TREE;
7477 *idk = CP_ID_KIND_NONE;
7479 /* Enter the scope corresponding to the type of the object
7480 given by the POSTFIX_EXPRESSION. */
7481 if (!dependent_p)
7483 scope = TREE_TYPE (postfix_expression);
7484 /* According to the standard, no expression should ever have
7485 reference type. Unfortunately, we do not currently match
7486 the standard in this respect in that our internal representation
7487 of an expression may have reference type even when the standard
7488 says it does not. Therefore, we have to manually obtain the
7489 underlying type here. */
7490 scope = non_reference (scope);
7491 /* The type of the POSTFIX_EXPRESSION must be complete. */
7492 /* Unlike the object expression in other contexts, *this is not
7493 required to be of complete type for purposes of class member
7494 access (5.2.5) outside the member function body. */
7495 if (postfix_expression != current_class_ref
7496 && scope != error_mark_node
7497 && !(processing_template_decl
7498 && current_class_type
7499 && (same_type_ignoring_top_level_qualifiers_p
7500 (scope, current_class_type))))
7502 scope = complete_type (scope);
7503 if (!COMPLETE_TYPE_P (scope)
7504 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7505 &dependent_p))
7506 return error_mark_node;
7509 if (!dependent_p)
7511 /* Let the name lookup machinery know that we are processing a
7512 class member access expression. */
7513 parser->context->object_type = scope;
7514 /* If something went wrong, we want to be able to discern that case,
7515 as opposed to the case where there was no SCOPE due to the type
7516 of expression being dependent. */
7517 if (!scope)
7518 scope = error_mark_node;
7519 /* If the SCOPE was erroneous, make the various semantic analysis
7520 functions exit quickly -- and without issuing additional error
7521 messages. */
7522 if (scope == error_mark_node)
7523 postfix_expression = error_mark_node;
7527 if (dependent_p)
7528 /* Tell cp_parser_lookup_name that there was an object, even though it's
7529 type-dependent. */
7530 parser->context->object_type = unknown_type_node;
7532 /* Assume this expression is not a pseudo-destructor access. */
7533 pseudo_destructor_p = false;
7535 /* If the SCOPE is a scalar type, then, if this is a valid program,
7536 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7537 is type dependent, it can be pseudo-destructor-name or something else.
7538 Try to parse it as pseudo-destructor-name first. */
7539 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7541 tree s;
7542 tree type;
7544 cp_parser_parse_tentatively (parser);
7545 /* Parse the pseudo-destructor-name. */
7546 s = NULL_TREE;
7547 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7548 &s, &type);
7549 if (dependent_p
7550 && (cp_parser_error_occurred (parser)
7551 || !SCALAR_TYPE_P (type)))
7552 cp_parser_abort_tentative_parse (parser);
7553 else if (cp_parser_parse_definitely (parser))
7555 pseudo_destructor_p = true;
7556 postfix_expression
7557 = finish_pseudo_destructor_expr (postfix_expression,
7558 s, type, location);
7562 if (!pseudo_destructor_p)
7564 /* If the SCOPE is not a scalar type, we are looking at an
7565 ordinary class member access expression, rather than a
7566 pseudo-destructor-name. */
7567 bool template_p;
7568 cp_token *token = cp_lexer_peek_token (parser->lexer);
7569 /* Parse the id-expression. */
7570 name = (cp_parser_id_expression
7571 (parser,
7572 cp_parser_optional_template_keyword (parser),
7573 /*check_dependency_p=*/true,
7574 &template_p,
7575 /*declarator_p=*/false,
7576 /*optional_p=*/false));
7577 /* In general, build a SCOPE_REF if the member name is qualified.
7578 However, if the name was not dependent and has already been
7579 resolved; there is no need to build the SCOPE_REF. For example;
7581 struct X { void f(); };
7582 template <typename T> void f(T* t) { t->X::f(); }
7584 Even though "t" is dependent, "X::f" is not and has been resolved
7585 to a BASELINK; there is no need to include scope information. */
7587 /* But we do need to remember that there was an explicit scope for
7588 virtual function calls. */
7589 if (parser->scope)
7590 *idk = CP_ID_KIND_QUALIFIED;
7592 /* If the name is a template-id that names a type, we will get a
7593 TYPE_DECL here. That is invalid code. */
7594 if (TREE_CODE (name) == TYPE_DECL)
7596 error_at (token->location, "invalid use of %qD", name);
7597 postfix_expression = error_mark_node;
7599 else
7601 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7603 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7605 error_at (token->location, "%<%D::%D%> is not a class member",
7606 parser->scope, name);
7607 postfix_expression = error_mark_node;
7609 else
7610 name = build_qualified_name (/*type=*/NULL_TREE,
7611 parser->scope,
7612 name,
7613 template_p);
7614 parser->scope = NULL_TREE;
7615 parser->qualifying_scope = NULL_TREE;
7616 parser->object_scope = NULL_TREE;
7618 if (parser->scope && name && BASELINK_P (name))
7619 adjust_result_of_qualified_name_lookup
7620 (name, parser->scope, scope);
7621 postfix_expression
7622 = finish_class_member_access_expr (postfix_expression, name,
7623 template_p,
7624 tf_warning_or_error);
7625 /* Build a location e.g.:
7626 ptr->access_expr
7627 ~~~^~~~~~~~~~~~~
7628 where the caret is at the deref token, ranging from
7629 the start of postfix_expression to the end of the access expr. */
7630 location_t end_loc
7631 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7632 location_t combined_loc
7633 = make_location (input_location, start_loc, end_loc);
7634 protected_set_expr_location (postfix_expression, combined_loc);
7638 /* We no longer need to look up names in the scope of the object on
7639 the left-hand side of the `.' or `->' operator. */
7640 parser->context->object_type = NULL_TREE;
7642 /* Outside of offsetof, these operators may not appear in
7643 constant-expressions. */
7644 if (!for_offsetof
7645 && (cp_parser_non_integral_constant_expression
7646 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7647 postfix_expression = error_mark_node;
7649 return postfix_expression;
7652 /* Parse a parenthesized expression-list.
7654 expression-list:
7655 assignment-expression
7656 expression-list, assignment-expression
7658 attribute-list:
7659 expression-list
7660 identifier
7661 identifier, expression-list
7663 CAST_P is true if this expression is the target of a cast.
7665 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7666 argument pack.
7668 WRAP_LOCATIONS_P is true if expressions within this list for which
7669 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7670 their source locations.
7672 Returns a vector of trees. Each element is a representation of an
7673 assignment-expression. NULL is returned if the ( and or ) are
7674 missing. An empty, but allocated, vector is returned on no
7675 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7676 if we are parsing an attribute list for an attribute that wants a
7677 plain identifier argument, normal_attr for an attribute that wants
7678 an expression, or non_attr if we aren't parsing an attribute list. If
7679 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7680 not all of the expressions in the list were constant.
7681 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7682 will be written to with the location of the closing parenthesis. If
7683 an error occurs, it may or may not be written to. */
7685 static vec<tree, va_gc> *
7686 cp_parser_parenthesized_expression_list (cp_parser* parser,
7687 int is_attribute_list,
7688 bool cast_p,
7689 bool allow_expansion_p,
7690 bool *non_constant_p,
7691 location_t *close_paren_loc,
7692 bool wrap_locations_p)
7694 vec<tree, va_gc> *expression_list;
7695 bool fold_expr_p = is_attribute_list != non_attr;
7696 tree identifier = NULL_TREE;
7697 bool saved_greater_than_is_operator_p;
7699 /* Assume all the expressions will be constant. */
7700 if (non_constant_p)
7701 *non_constant_p = false;
7703 matching_parens parens;
7704 if (!parens.require_open (parser))
7705 return NULL;
7707 expression_list = make_tree_vector ();
7709 /* Within a parenthesized expression, a `>' token is always
7710 the greater-than operator. */
7711 saved_greater_than_is_operator_p
7712 = parser->greater_than_is_operator_p;
7713 parser->greater_than_is_operator_p = true;
7715 cp_expr expr (NULL_TREE);
7717 /* Consume expressions until there are no more. */
7718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7719 while (true)
7721 /* At the beginning of attribute lists, check to see if the
7722 next token is an identifier. */
7723 if (is_attribute_list == id_attr
7724 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7726 cp_token *token;
7728 /* Consume the identifier. */
7729 token = cp_lexer_consume_token (parser->lexer);
7730 /* Save the identifier. */
7731 identifier = token->u.value;
7733 else
7735 bool expr_non_constant_p;
7737 /* Parse the next assignment-expression. */
7738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7740 /* A braced-init-list. */
7741 cp_lexer_set_source_position (parser->lexer);
7742 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7743 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7744 if (non_constant_p && expr_non_constant_p)
7745 *non_constant_p = true;
7747 else if (non_constant_p)
7749 expr = (cp_parser_constant_expression
7750 (parser, /*allow_non_constant_p=*/true,
7751 &expr_non_constant_p));
7752 if (expr_non_constant_p)
7753 *non_constant_p = true;
7755 else
7756 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7757 cast_p);
7759 if (fold_expr_p)
7760 expr = instantiate_non_dependent_expr (expr);
7762 /* If we have an ellipsis, then this is an expression
7763 expansion. */
7764 if (allow_expansion_p
7765 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7767 /* Consume the `...'. */
7768 cp_lexer_consume_token (parser->lexer);
7770 /* Build the argument pack. */
7771 expr = make_pack_expansion (expr);
7774 if (wrap_locations_p)
7775 expr.maybe_add_location_wrapper ();
7777 /* Add it to the list. We add error_mark_node
7778 expressions to the list, so that we can still tell if
7779 the correct form for a parenthesized expression-list
7780 is found. That gives better errors. */
7781 vec_safe_push (expression_list, expr.get_value ());
7783 if (expr == error_mark_node)
7784 goto skip_comma;
7787 /* After the first item, attribute lists look the same as
7788 expression lists. */
7789 is_attribute_list = non_attr;
7791 get_comma:;
7792 /* If the next token isn't a `,', then we are done. */
7793 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7794 break;
7796 /* Otherwise, consume the `,' and keep going. */
7797 cp_lexer_consume_token (parser->lexer);
7800 if (close_paren_loc)
7801 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7803 if (!parens.require_close (parser))
7805 int ending;
7807 skip_comma:;
7808 /* We try and resync to an unnested comma, as that will give the
7809 user better diagnostics. */
7810 ending = cp_parser_skip_to_closing_parenthesis (parser,
7811 /*recovering=*/true,
7812 /*or_comma=*/true,
7813 /*consume_paren=*/true);
7814 if (ending < 0)
7815 goto get_comma;
7816 if (!ending)
7818 parser->greater_than_is_operator_p
7819 = saved_greater_than_is_operator_p;
7820 return NULL;
7824 parser->greater_than_is_operator_p
7825 = saved_greater_than_is_operator_p;
7827 if (identifier)
7828 vec_safe_insert (expression_list, 0, identifier);
7830 return expression_list;
7833 /* Parse a pseudo-destructor-name.
7835 pseudo-destructor-name:
7836 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7837 :: [opt] nested-name-specifier template template-id :: ~ type-name
7838 :: [opt] nested-name-specifier [opt] ~ type-name
7840 If either of the first two productions is used, sets *SCOPE to the
7841 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7842 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7843 or ERROR_MARK_NODE if the parse fails. */
7845 static void
7846 cp_parser_pseudo_destructor_name (cp_parser* parser,
7847 tree object,
7848 tree* scope,
7849 tree* type)
7851 bool nested_name_specifier_p;
7853 /* Handle ~auto. */
7854 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7855 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7856 && !type_dependent_expression_p (object))
7858 if (cxx_dialect < cxx14)
7859 pedwarn (input_location, 0,
7860 "%<~auto%> only available with "
7861 "-std=c++14 or -std=gnu++14");
7862 cp_lexer_consume_token (parser->lexer);
7863 cp_lexer_consume_token (parser->lexer);
7864 *scope = NULL_TREE;
7865 *type = TREE_TYPE (object);
7866 return;
7869 /* Assume that things will not work out. */
7870 *type = error_mark_node;
7872 /* Look for the optional `::' operator. */
7873 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7874 /* Look for the optional nested-name-specifier. */
7875 nested_name_specifier_p
7876 = (cp_parser_nested_name_specifier_opt (parser,
7877 /*typename_keyword_p=*/false,
7878 /*check_dependency_p=*/true,
7879 /*type_p=*/false,
7880 /*is_declaration=*/false)
7881 != NULL_TREE);
7882 /* Now, if we saw a nested-name-specifier, we might be doing the
7883 second production. */
7884 if (nested_name_specifier_p
7885 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7887 /* Consume the `template' keyword. */
7888 cp_lexer_consume_token (parser->lexer);
7889 /* Parse the template-id. */
7890 cp_parser_template_id (parser,
7891 /*template_keyword_p=*/true,
7892 /*check_dependency_p=*/false,
7893 class_type,
7894 /*is_declaration=*/true);
7895 /* Look for the `::' token. */
7896 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7898 /* If the next token is not a `~', then there might be some
7899 additional qualification. */
7900 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7902 /* At this point, we're looking for "type-name :: ~". The type-name
7903 must not be a class-name, since this is a pseudo-destructor. So,
7904 it must be either an enum-name, or a typedef-name -- both of which
7905 are just identifiers. So, we peek ahead to check that the "::"
7906 and "~" tokens are present; if they are not, then we can avoid
7907 calling type_name. */
7908 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7909 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7910 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7912 cp_parser_error (parser, "non-scalar type");
7913 return;
7916 /* Look for the type-name. */
7917 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7918 if (*scope == error_mark_node)
7919 return;
7921 /* Look for the `::' token. */
7922 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7924 else
7925 *scope = NULL_TREE;
7927 /* Look for the `~'. */
7928 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7930 /* Once we see the ~, this has to be a pseudo-destructor. */
7931 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7932 cp_parser_commit_to_topmost_tentative_parse (parser);
7934 /* Look for the type-name again. We are not responsible for
7935 checking that it matches the first type-name. */
7936 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7939 /* Parse a unary-expression.
7941 unary-expression:
7942 postfix-expression
7943 ++ cast-expression
7944 -- cast-expression
7945 unary-operator cast-expression
7946 sizeof unary-expression
7947 sizeof ( type-id )
7948 alignof ( type-id ) [C++0x]
7949 new-expression
7950 delete-expression
7952 GNU Extensions:
7954 unary-expression:
7955 __extension__ cast-expression
7956 __alignof__ unary-expression
7957 __alignof__ ( type-id )
7958 alignof unary-expression [C++0x]
7959 __real__ cast-expression
7960 __imag__ cast-expression
7961 && identifier
7962 sizeof ( type-id ) { initializer-list , [opt] }
7963 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7964 __alignof__ ( type-id ) { initializer-list , [opt] }
7966 ADDRESS_P is true iff the unary-expression is appearing as the
7967 operand of the `&' operator. CAST_P is true if this expression is
7968 the target of a cast.
7970 Returns a representation of the expression. */
7972 static cp_expr
7973 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7974 bool address_p, bool cast_p, bool decltype_p)
7976 cp_token *token;
7977 enum tree_code unary_operator;
7979 /* Peek at the next token. */
7980 token = cp_lexer_peek_token (parser->lexer);
7981 /* Some keywords give away the kind of expression. */
7982 if (token->type == CPP_KEYWORD)
7984 enum rid keyword = token->keyword;
7986 switch (keyword)
7988 case RID_ALIGNOF:
7989 case RID_SIZEOF:
7991 tree operand, ret;
7992 enum tree_code op;
7993 location_t start_loc = token->location;
7995 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7996 /* Consume the token. */
7997 cp_lexer_consume_token (parser->lexer);
7998 /* Parse the operand. */
7999 operand = cp_parser_sizeof_operand (parser, keyword);
8001 if (TYPE_P (operand))
8002 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8003 else
8005 /* ISO C++ defines alignof only with types, not with
8006 expressions. So pedwarn if alignof is used with a non-
8007 type expression. However, __alignof__ is ok. */
8008 if (id_equal (token->u.value, "alignof"))
8009 pedwarn (token->location, OPT_Wpedantic,
8010 "ISO C++ does not allow %<alignof%> "
8011 "with a non-type");
8013 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8015 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8016 SIZEOF_EXPR with the original operand. */
8017 if (op == SIZEOF_EXPR && ret != error_mark_node)
8019 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8021 if (!processing_template_decl && TYPE_P (operand))
8023 ret = build_min (SIZEOF_EXPR, size_type_node,
8024 build1 (NOP_EXPR, operand,
8025 error_mark_node));
8026 SIZEOF_EXPR_TYPE_P (ret) = 1;
8028 else
8029 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8030 TREE_SIDE_EFFECTS (ret) = 0;
8031 TREE_READONLY (ret) = 1;
8035 /* Construct a location e.g. :
8036 alignof (expr)
8037 ^~~~~~~~~~~~~~
8038 with start == caret at the start of the "alignof"/"sizeof"
8039 token, with the endpoint at the final closing paren. */
8040 location_t finish_loc
8041 = cp_lexer_previous_token (parser->lexer)->location;
8042 location_t compound_loc
8043 = make_location (start_loc, start_loc, finish_loc);
8045 cp_expr ret_expr (ret);
8046 ret_expr.set_location (compound_loc);
8047 ret_expr = ret_expr.maybe_add_location_wrapper ();
8048 return ret_expr;
8051 case RID_NEW:
8052 return cp_parser_new_expression (parser);
8054 case RID_DELETE:
8055 return cp_parser_delete_expression (parser);
8057 case RID_EXTENSION:
8059 /* The saved value of the PEDANTIC flag. */
8060 int saved_pedantic;
8061 tree expr;
8063 /* Save away the PEDANTIC flag. */
8064 cp_parser_extension_opt (parser, &saved_pedantic);
8065 /* Parse the cast-expression. */
8066 expr = cp_parser_simple_cast_expression (parser);
8067 /* Restore the PEDANTIC flag. */
8068 pedantic = saved_pedantic;
8070 return expr;
8073 case RID_REALPART:
8074 case RID_IMAGPART:
8076 tree expression;
8078 /* Consume the `__real__' or `__imag__' token. */
8079 cp_lexer_consume_token (parser->lexer);
8080 /* Parse the cast-expression. */
8081 expression = cp_parser_simple_cast_expression (parser);
8082 /* Create the complete representation. */
8083 return build_x_unary_op (token->location,
8084 (keyword == RID_REALPART
8085 ? REALPART_EXPR : IMAGPART_EXPR),
8086 expression,
8087 tf_warning_or_error);
8089 break;
8091 case RID_TRANSACTION_ATOMIC:
8092 case RID_TRANSACTION_RELAXED:
8093 return cp_parser_transaction_expression (parser, keyword);
8095 case RID_NOEXCEPT:
8097 tree expr;
8098 const char *saved_message;
8099 bool saved_integral_constant_expression_p;
8100 bool saved_non_integral_constant_expression_p;
8101 bool saved_greater_than_is_operator_p;
8103 location_t start_loc = token->location;
8105 cp_lexer_consume_token (parser->lexer);
8106 matching_parens parens;
8107 parens.require_open (parser);
8109 saved_message = parser->type_definition_forbidden_message;
8110 parser->type_definition_forbidden_message
8111 = G_("types may not be defined in %<noexcept%> expressions");
8113 saved_integral_constant_expression_p
8114 = parser->integral_constant_expression_p;
8115 saved_non_integral_constant_expression_p
8116 = parser->non_integral_constant_expression_p;
8117 parser->integral_constant_expression_p = false;
8119 saved_greater_than_is_operator_p
8120 = parser->greater_than_is_operator_p;
8121 parser->greater_than_is_operator_p = true;
8123 ++cp_unevaluated_operand;
8124 ++c_inhibit_evaluation_warnings;
8125 ++cp_noexcept_operand;
8126 expr = cp_parser_expression (parser);
8127 --cp_noexcept_operand;
8128 --c_inhibit_evaluation_warnings;
8129 --cp_unevaluated_operand;
8131 parser->greater_than_is_operator_p
8132 = saved_greater_than_is_operator_p;
8134 parser->integral_constant_expression_p
8135 = saved_integral_constant_expression_p;
8136 parser->non_integral_constant_expression_p
8137 = saved_non_integral_constant_expression_p;
8139 parser->type_definition_forbidden_message = saved_message;
8141 location_t finish_loc
8142 = cp_lexer_peek_token (parser->lexer)->location;
8143 parens.require_close (parser);
8145 /* Construct a location of the form:
8146 noexcept (expr)
8147 ^~~~~~~~~~~~~~~
8148 with start == caret, finishing at the close-paren. */
8149 location_t noexcept_loc
8150 = make_location (start_loc, start_loc, finish_loc);
8152 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8153 noexcept_loc);
8156 default:
8157 break;
8161 /* Look for the `:: new' and `:: delete', which also signal the
8162 beginning of a new-expression, or delete-expression,
8163 respectively. If the next token is `::', then it might be one of
8164 these. */
8165 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8167 enum rid keyword;
8169 /* See if the token after the `::' is one of the keywords in
8170 which we're interested. */
8171 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8172 /* If it's `new', we have a new-expression. */
8173 if (keyword == RID_NEW)
8174 return cp_parser_new_expression (parser);
8175 /* Similarly, for `delete'. */
8176 else if (keyword == RID_DELETE)
8177 return cp_parser_delete_expression (parser);
8180 /* Look for a unary operator. */
8181 unary_operator = cp_parser_unary_operator (token);
8182 /* The `++' and `--' operators can be handled similarly, even though
8183 they are not technically unary-operators in the grammar. */
8184 if (unary_operator == ERROR_MARK)
8186 if (token->type == CPP_PLUS_PLUS)
8187 unary_operator = PREINCREMENT_EXPR;
8188 else if (token->type == CPP_MINUS_MINUS)
8189 unary_operator = PREDECREMENT_EXPR;
8190 /* Handle the GNU address-of-label extension. */
8191 else if (cp_parser_allow_gnu_extensions_p (parser)
8192 && token->type == CPP_AND_AND)
8194 tree identifier;
8195 tree expression;
8196 location_t start_loc = token->location;
8198 /* Consume the '&&' token. */
8199 cp_lexer_consume_token (parser->lexer);
8200 /* Look for the identifier. */
8201 location_t finish_loc
8202 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8203 identifier = cp_parser_identifier (parser);
8204 /* Construct a location of the form:
8205 &&label
8206 ^~~~~~~
8207 with caret==start at the "&&", finish at the end of the label. */
8208 location_t combined_loc
8209 = make_location (start_loc, start_loc, finish_loc);
8210 /* Create an expression representing the address. */
8211 expression = finish_label_address_expr (identifier, combined_loc);
8212 if (cp_parser_non_integral_constant_expression (parser,
8213 NIC_ADDR_LABEL))
8214 expression = error_mark_node;
8215 return expression;
8218 if (unary_operator != ERROR_MARK)
8220 cp_expr cast_expression;
8221 cp_expr expression = error_mark_node;
8222 non_integral_constant non_constant_p = NIC_NONE;
8223 location_t loc = token->location;
8224 tsubst_flags_t complain = complain_flags (decltype_p);
8226 /* Consume the operator token. */
8227 token = cp_lexer_consume_token (parser->lexer);
8228 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8230 /* Parse the cast-expression. */
8231 cast_expression
8232 = cp_parser_cast_expression (parser,
8233 unary_operator == ADDR_EXPR,
8234 /*cast_p=*/false,
8235 /*decltype*/false,
8236 pidk);
8238 /* Make a location:
8239 OP_TOKEN CAST_EXPRESSION
8240 ^~~~~~~~~~~~~~~~~~~~~~~~~
8241 with start==caret at the operator token, and
8242 extending to the end of the cast_expression. */
8243 loc = make_location (loc, loc, cast_expression.get_finish ());
8245 /* Now, build an appropriate representation. */
8246 switch (unary_operator)
8248 case INDIRECT_REF:
8249 non_constant_p = NIC_STAR;
8250 expression = build_x_indirect_ref (loc, cast_expression,
8251 RO_UNARY_STAR,
8252 complain);
8253 /* TODO: build_x_indirect_ref does not always honor the
8254 location, so ensure it is set. */
8255 expression.set_location (loc);
8256 break;
8258 case ADDR_EXPR:
8259 non_constant_p = NIC_ADDR;
8260 /* Fall through. */
8261 case BIT_NOT_EXPR:
8262 expression = build_x_unary_op (loc, unary_operator,
8263 cast_expression,
8264 complain);
8265 /* TODO: build_x_unary_op does not always honor the location,
8266 so ensure it is set. */
8267 expression.set_location (loc);
8268 break;
8270 case PREINCREMENT_EXPR:
8271 case PREDECREMENT_EXPR:
8272 non_constant_p = unary_operator == PREINCREMENT_EXPR
8273 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8274 /* Fall through. */
8275 case NEGATE_EXPR:
8276 /* Immediately fold negation of a constant, unless the constant is 0
8277 (since -0 == 0) or it would overflow. */
8278 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8279 && CONSTANT_CLASS_P (cast_expression)
8280 && !integer_zerop (cast_expression)
8281 && !TREE_OVERFLOW (cast_expression))
8283 tree folded = fold_build1 (unary_operator,
8284 TREE_TYPE (cast_expression),
8285 cast_expression);
8286 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8288 expression = cp_expr (folded, loc);
8289 break;
8292 /* Fall through. */
8293 case UNARY_PLUS_EXPR:
8294 case TRUTH_NOT_EXPR:
8295 expression = finish_unary_op_expr (loc, unary_operator,
8296 cast_expression, complain);
8297 break;
8299 default:
8300 gcc_unreachable ();
8303 if (non_constant_p != NIC_NONE
8304 && cp_parser_non_integral_constant_expression (parser,
8305 non_constant_p))
8306 expression = error_mark_node;
8308 return expression;
8311 return cp_parser_postfix_expression (parser, address_p, cast_p,
8312 /*member_access_only_p=*/false,
8313 decltype_p,
8314 pidk);
8317 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8318 unary-operator, the corresponding tree code is returned. */
8320 static enum tree_code
8321 cp_parser_unary_operator (cp_token* token)
8323 switch (token->type)
8325 case CPP_MULT:
8326 return INDIRECT_REF;
8328 case CPP_AND:
8329 return ADDR_EXPR;
8331 case CPP_PLUS:
8332 return UNARY_PLUS_EXPR;
8334 case CPP_MINUS:
8335 return NEGATE_EXPR;
8337 case CPP_NOT:
8338 return TRUTH_NOT_EXPR;
8340 case CPP_COMPL:
8341 return BIT_NOT_EXPR;
8343 default:
8344 return ERROR_MARK;
8348 /* Parse a new-expression.
8350 new-expression:
8351 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8352 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8354 Returns a representation of the expression. */
8356 static tree
8357 cp_parser_new_expression (cp_parser* parser)
8359 bool global_scope_p;
8360 vec<tree, va_gc> *placement;
8361 tree type;
8362 vec<tree, va_gc> *initializer;
8363 tree nelts = NULL_TREE;
8364 tree ret;
8366 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8368 /* Look for the optional `::' operator. */
8369 global_scope_p
8370 = (cp_parser_global_scope_opt (parser,
8371 /*current_scope_valid_p=*/false)
8372 != NULL_TREE);
8373 /* Look for the `new' operator. */
8374 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8375 /* There's no easy way to tell a new-placement from the
8376 `( type-id )' construct. */
8377 cp_parser_parse_tentatively (parser);
8378 /* Look for a new-placement. */
8379 placement = cp_parser_new_placement (parser);
8380 /* If that didn't work out, there's no new-placement. */
8381 if (!cp_parser_parse_definitely (parser))
8383 if (placement != NULL)
8384 release_tree_vector (placement);
8385 placement = NULL;
8388 /* If the next token is a `(', then we have a parenthesized
8389 type-id. */
8390 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8392 cp_token *token;
8393 const char *saved_message = parser->type_definition_forbidden_message;
8395 /* Consume the `('. */
8396 matching_parens parens;
8397 parens.consume_open (parser);
8399 /* Parse the type-id. */
8400 parser->type_definition_forbidden_message
8401 = G_("types may not be defined in a new-expression");
8403 type_id_in_expr_sentinel s (parser);
8404 type = cp_parser_type_id (parser);
8406 parser->type_definition_forbidden_message = saved_message;
8408 /* Look for the closing `)'. */
8409 parens.require_close (parser);
8410 token = cp_lexer_peek_token (parser->lexer);
8411 /* There should not be a direct-new-declarator in this production,
8412 but GCC used to allowed this, so we check and emit a sensible error
8413 message for this case. */
8414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8416 error_at (token->location,
8417 "array bound forbidden after parenthesized type-id");
8418 inform (token->location,
8419 "try removing the parentheses around the type-id");
8420 cp_parser_direct_new_declarator (parser);
8423 /* Otherwise, there must be a new-type-id. */
8424 else
8425 type = cp_parser_new_type_id (parser, &nelts);
8427 /* If the next token is a `(' or '{', then we have a new-initializer. */
8428 cp_token *token = cp_lexer_peek_token (parser->lexer);
8429 if (token->type == CPP_OPEN_PAREN
8430 || token->type == CPP_OPEN_BRACE)
8431 initializer = cp_parser_new_initializer (parser);
8432 else
8433 initializer = NULL;
8435 /* A new-expression may not appear in an integral constant
8436 expression. */
8437 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8438 ret = error_mark_node;
8439 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8440 of a new-type-id or type-id of a new-expression, the new-expression shall
8441 contain a new-initializer of the form ( assignment-expression )".
8442 Additionally, consistently with the spirit of DR 1467, we want to accept
8443 'new auto { 2 }' too. */
8444 else if ((ret = type_uses_auto (type))
8445 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8446 && (vec_safe_length (initializer) != 1
8447 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8448 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8450 error_at (token->location,
8451 "initialization of new-expression for type %<auto%> "
8452 "requires exactly one element");
8453 ret = error_mark_node;
8455 else
8457 /* Construct a location e.g.:
8458 ptr = new int[100]
8459 ^~~~~~~~~~~~
8460 with caret == start at the start of the "new" token, and the end
8461 at the end of the final token we consumed. */
8462 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8463 location_t end_loc = get_finish (end_tok->location);
8464 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8466 /* Create a representation of the new-expression. */
8467 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8468 tf_warning_or_error);
8469 protected_set_expr_location (ret, combined_loc);
8472 if (placement != NULL)
8473 release_tree_vector (placement);
8474 if (initializer != NULL)
8475 release_tree_vector (initializer);
8477 return ret;
8480 /* Parse a new-placement.
8482 new-placement:
8483 ( expression-list )
8485 Returns the same representation as for an expression-list. */
8487 static vec<tree, va_gc> *
8488 cp_parser_new_placement (cp_parser* parser)
8490 vec<tree, va_gc> *expression_list;
8492 /* Parse the expression-list. */
8493 expression_list = (cp_parser_parenthesized_expression_list
8494 (parser, non_attr, /*cast_p=*/false,
8495 /*allow_expansion_p=*/true,
8496 /*non_constant_p=*/NULL));
8498 if (expression_list && expression_list->is_empty ())
8499 error ("expected expression-list or type-id");
8501 return expression_list;
8504 /* Parse a new-type-id.
8506 new-type-id:
8507 type-specifier-seq new-declarator [opt]
8509 Returns the TYPE allocated. If the new-type-id indicates an array
8510 type, *NELTS is set to the number of elements in the last array
8511 bound; the TYPE will not include the last array bound. */
8513 static tree
8514 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8516 cp_decl_specifier_seq type_specifier_seq;
8517 cp_declarator *new_declarator;
8518 cp_declarator *declarator;
8519 cp_declarator *outer_declarator;
8520 const char *saved_message;
8522 /* The type-specifier sequence must not contain type definitions.
8523 (It cannot contain declarations of new types either, but if they
8524 are not definitions we will catch that because they are not
8525 complete.) */
8526 saved_message = parser->type_definition_forbidden_message;
8527 parser->type_definition_forbidden_message
8528 = G_("types may not be defined in a new-type-id");
8529 /* Parse the type-specifier-seq. */
8530 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8531 /*is_trailing_return=*/false,
8532 &type_specifier_seq);
8533 /* Restore the old message. */
8534 parser->type_definition_forbidden_message = saved_message;
8536 if (type_specifier_seq.type == error_mark_node)
8537 return error_mark_node;
8539 /* Parse the new-declarator. */
8540 new_declarator = cp_parser_new_declarator_opt (parser);
8542 /* Determine the number of elements in the last array dimension, if
8543 any. */
8544 *nelts = NULL_TREE;
8545 /* Skip down to the last array dimension. */
8546 declarator = new_declarator;
8547 outer_declarator = NULL;
8548 while (declarator && (declarator->kind == cdk_pointer
8549 || declarator->kind == cdk_ptrmem))
8551 outer_declarator = declarator;
8552 declarator = declarator->declarator;
8554 while (declarator
8555 && declarator->kind == cdk_array
8556 && declarator->declarator
8557 && declarator->declarator->kind == cdk_array)
8559 outer_declarator = declarator;
8560 declarator = declarator->declarator;
8563 if (declarator && declarator->kind == cdk_array)
8565 *nelts = declarator->u.array.bounds;
8566 if (*nelts == error_mark_node)
8567 *nelts = integer_one_node;
8569 if (outer_declarator)
8570 outer_declarator->declarator = declarator->declarator;
8571 else
8572 new_declarator = NULL;
8575 return groktypename (&type_specifier_seq, new_declarator, false);
8578 /* Parse an (optional) new-declarator.
8580 new-declarator:
8581 ptr-operator new-declarator [opt]
8582 direct-new-declarator
8584 Returns the declarator. */
8586 static cp_declarator *
8587 cp_parser_new_declarator_opt (cp_parser* parser)
8589 enum tree_code code;
8590 tree type, std_attributes = NULL_TREE;
8591 cp_cv_quals cv_quals;
8593 /* We don't know if there's a ptr-operator next, or not. */
8594 cp_parser_parse_tentatively (parser);
8595 /* Look for a ptr-operator. */
8596 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8597 /* If that worked, look for more new-declarators. */
8598 if (cp_parser_parse_definitely (parser))
8600 cp_declarator *declarator;
8602 /* Parse another optional declarator. */
8603 declarator = cp_parser_new_declarator_opt (parser);
8605 declarator = cp_parser_make_indirect_declarator
8606 (code, type, cv_quals, declarator, std_attributes);
8608 return declarator;
8611 /* If the next token is a `[', there is a direct-new-declarator. */
8612 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8613 return cp_parser_direct_new_declarator (parser);
8615 return NULL;
8618 /* Parse a direct-new-declarator.
8620 direct-new-declarator:
8621 [ expression ]
8622 direct-new-declarator [constant-expression]
8626 static cp_declarator *
8627 cp_parser_direct_new_declarator (cp_parser* parser)
8629 cp_declarator *declarator = NULL;
8631 while (true)
8633 tree expression;
8634 cp_token *token;
8636 /* Look for the opening `['. */
8637 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8639 token = cp_lexer_peek_token (parser->lexer);
8640 expression = cp_parser_expression (parser);
8641 /* The standard requires that the expression have integral
8642 type. DR 74 adds enumeration types. We believe that the
8643 real intent is that these expressions be handled like the
8644 expression in a `switch' condition, which also allows
8645 classes with a single conversion to integral or
8646 enumeration type. */
8647 if (!processing_template_decl)
8649 expression
8650 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8651 expression,
8652 /*complain=*/true);
8653 if (!expression)
8655 error_at (token->location,
8656 "expression in new-declarator must have integral "
8657 "or enumeration type");
8658 expression = error_mark_node;
8662 /* Look for the closing `]'. */
8663 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8665 /* Add this bound to the declarator. */
8666 declarator = make_array_declarator (declarator, expression);
8668 /* If the next token is not a `[', then there are no more
8669 bounds. */
8670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8671 break;
8674 return declarator;
8677 /* Parse a new-initializer.
8679 new-initializer:
8680 ( expression-list [opt] )
8681 braced-init-list
8683 Returns a representation of the expression-list. */
8685 static vec<tree, va_gc> *
8686 cp_parser_new_initializer (cp_parser* parser)
8688 vec<tree, va_gc> *expression_list;
8690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8692 tree t;
8693 bool expr_non_constant_p;
8694 cp_lexer_set_source_position (parser->lexer);
8695 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8696 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8697 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8698 expression_list = make_tree_vector_single (t);
8700 else
8701 expression_list = (cp_parser_parenthesized_expression_list
8702 (parser, non_attr, /*cast_p=*/false,
8703 /*allow_expansion_p=*/true,
8704 /*non_constant_p=*/NULL));
8706 return expression_list;
8709 /* Parse a delete-expression.
8711 delete-expression:
8712 :: [opt] delete cast-expression
8713 :: [opt] delete [ ] cast-expression
8715 Returns a representation of the expression. */
8717 static tree
8718 cp_parser_delete_expression (cp_parser* parser)
8720 bool global_scope_p;
8721 bool array_p;
8722 tree expression;
8724 /* Look for the optional `::' operator. */
8725 global_scope_p
8726 = (cp_parser_global_scope_opt (parser,
8727 /*current_scope_valid_p=*/false)
8728 != NULL_TREE);
8729 /* Look for the `delete' keyword. */
8730 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8731 /* See if the array syntax is in use. */
8732 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8734 /* Consume the `[' token. */
8735 cp_lexer_consume_token (parser->lexer);
8736 /* Look for the `]' token. */
8737 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8738 /* Remember that this is the `[]' construct. */
8739 array_p = true;
8741 else
8742 array_p = false;
8744 /* Parse the cast-expression. */
8745 expression = cp_parser_simple_cast_expression (parser);
8747 /* A delete-expression may not appear in an integral constant
8748 expression. */
8749 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8750 return error_mark_node;
8752 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8753 tf_warning_or_error);
8756 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8757 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8758 0 otherwise. */
8760 static int
8761 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8763 cp_token *token = cp_lexer_peek_token (parser->lexer);
8764 switch (token->type)
8766 case CPP_COMMA:
8767 case CPP_SEMICOLON:
8768 case CPP_QUERY:
8769 case CPP_COLON:
8770 case CPP_CLOSE_SQUARE:
8771 case CPP_CLOSE_PAREN:
8772 case CPP_CLOSE_BRACE:
8773 case CPP_OPEN_BRACE:
8774 case CPP_DOT:
8775 case CPP_DOT_STAR:
8776 case CPP_DEREF:
8777 case CPP_DEREF_STAR:
8778 case CPP_DIV:
8779 case CPP_MOD:
8780 case CPP_LSHIFT:
8781 case CPP_RSHIFT:
8782 case CPP_LESS:
8783 case CPP_GREATER:
8784 case CPP_LESS_EQ:
8785 case CPP_GREATER_EQ:
8786 case CPP_EQ_EQ:
8787 case CPP_NOT_EQ:
8788 case CPP_EQ:
8789 case CPP_MULT_EQ:
8790 case CPP_DIV_EQ:
8791 case CPP_MOD_EQ:
8792 case CPP_PLUS_EQ:
8793 case CPP_MINUS_EQ:
8794 case CPP_RSHIFT_EQ:
8795 case CPP_LSHIFT_EQ:
8796 case CPP_AND_EQ:
8797 case CPP_XOR_EQ:
8798 case CPP_OR_EQ:
8799 case CPP_XOR:
8800 case CPP_OR:
8801 case CPP_OR_OR:
8802 case CPP_EOF:
8803 case CPP_ELLIPSIS:
8804 return 0;
8806 case CPP_OPEN_PAREN:
8807 /* In ((type ()) () the last () isn't a valid cast-expression,
8808 so the whole must be parsed as postfix-expression. */
8809 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8810 != CPP_CLOSE_PAREN;
8812 case CPP_OPEN_SQUARE:
8813 /* '[' may start a primary-expression in obj-c++ and in C++11,
8814 as a lambda-expression, eg, '(void)[]{}'. */
8815 if (cxx_dialect >= cxx11)
8816 return -1;
8817 return c_dialect_objc ();
8819 case CPP_PLUS_PLUS:
8820 case CPP_MINUS_MINUS:
8821 /* '++' and '--' may or may not start a cast-expression:
8823 struct T { void operator++(int); };
8824 void f() { (T())++; }
8828 int a;
8829 (int)++a; */
8830 return -1;
8832 default:
8833 return 1;
8837 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8838 in the order: const_cast, static_cast, reinterpret_cast.
8840 Don't suggest dynamic_cast.
8842 Return the first legal cast kind found, or NULL otherwise. */
8844 static const char *
8845 get_cast_suggestion (tree dst_type, tree orig_expr)
8847 tree trial;
8849 /* Reuse the parser logic by attempting to build the various kinds of
8850 cast, with "complain" disabled.
8851 Identify the first such cast that is valid. */
8853 /* Don't attempt to run such logic within template processing. */
8854 if (processing_template_decl)
8855 return NULL;
8857 /* First try const_cast. */
8858 trial = build_const_cast (dst_type, orig_expr, tf_none);
8859 if (trial != error_mark_node)
8860 return "const_cast";
8862 /* If that fails, try static_cast. */
8863 trial = build_static_cast (dst_type, orig_expr, tf_none);
8864 if (trial != error_mark_node)
8865 return "static_cast";
8867 /* Finally, try reinterpret_cast. */
8868 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8869 if (trial != error_mark_node)
8870 return "reinterpret_cast";
8872 /* No such cast possible. */
8873 return NULL;
8876 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8877 suggesting how to convert a C-style cast of the form:
8879 (DST_TYPE)ORIG_EXPR
8881 to a C++-style cast.
8883 The primary range of RICHLOC is asssumed to be that of the original
8884 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8885 of the parens in the C-style cast. */
8887 static void
8888 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8889 location_t close_paren_loc, tree orig_expr,
8890 tree dst_type)
8892 /* This function is non-trivial, so bail out now if the warning isn't
8893 going to be emitted. */
8894 if (!warn_old_style_cast)
8895 return;
8897 /* Try to find a legal C++ cast, trying them in order:
8898 const_cast, static_cast, reinterpret_cast. */
8899 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8900 if (!cast_suggestion)
8901 return;
8903 /* Replace the open paren with "CAST_SUGGESTION<". */
8904 pretty_printer pp;
8905 pp_printf (&pp, "%s<", cast_suggestion);
8906 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8908 /* Replace the close paren with "> (". */
8909 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8911 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8912 rich_loc->add_fixit_insert_after (")");
8916 /* Parse a cast-expression.
8918 cast-expression:
8919 unary-expression
8920 ( type-id ) cast-expression
8922 ADDRESS_P is true iff the unary-expression is appearing as the
8923 operand of the `&' operator. CAST_P is true if this expression is
8924 the target of a cast.
8926 Returns a representation of the expression. */
8928 static cp_expr
8929 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8930 bool decltype_p, cp_id_kind * pidk)
8932 /* If it's a `(', then we might be looking at a cast. */
8933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8935 tree type = NULL_TREE;
8936 cp_expr expr (NULL_TREE);
8937 int cast_expression = 0;
8938 const char *saved_message;
8940 /* There's no way to know yet whether or not this is a cast.
8941 For example, `(int (3))' is a unary-expression, while `(int)
8942 3' is a cast. So, we resort to parsing tentatively. */
8943 cp_parser_parse_tentatively (parser);
8944 /* Types may not be defined in a cast. */
8945 saved_message = parser->type_definition_forbidden_message;
8946 parser->type_definition_forbidden_message
8947 = G_("types may not be defined in casts");
8948 /* Consume the `('. */
8949 matching_parens parens;
8950 cp_token *open_paren = parens.consume_open (parser);
8951 location_t open_paren_loc = open_paren->location;
8952 location_t close_paren_loc = UNKNOWN_LOCATION;
8954 /* A very tricky bit is that `(struct S) { 3 }' is a
8955 compound-literal (which we permit in C++ as an extension).
8956 But, that construct is not a cast-expression -- it is a
8957 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8958 is legal; if the compound-literal were a cast-expression,
8959 you'd need an extra set of parentheses.) But, if we parse
8960 the type-id, and it happens to be a class-specifier, then we
8961 will commit to the parse at that point, because we cannot
8962 undo the action that is done when creating a new class. So,
8963 then we cannot back up and do a postfix-expression.
8965 Another tricky case is the following (c++/29234):
8967 struct S { void operator () (); };
8969 void foo ()
8971 ( S()() );
8974 As a type-id we parse the parenthesized S()() as a function
8975 returning a function, groktypename complains and we cannot
8976 back up in this case either.
8978 Therefore, we scan ahead to the closing `)', and check to see
8979 if the tokens after the `)' can start a cast-expression. Otherwise
8980 we are dealing with an unary-expression, a postfix-expression
8981 or something else.
8983 Yet another tricky case, in C++11, is the following (c++/54891):
8985 (void)[]{};
8987 The issue is that usually, besides the case of lambda-expressions,
8988 the parenthesized type-id cannot be followed by '[', and, eg, we
8989 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8990 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8991 we don't commit, we try a cast-expression, then an unary-expression.
8993 Save tokens so that we can put them back. */
8994 cp_lexer_save_tokens (parser->lexer);
8996 /* We may be looking at a cast-expression. */
8997 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8998 /*consume_paren=*/true))
8999 cast_expression
9000 = cp_parser_tokens_start_cast_expression (parser);
9002 /* Roll back the tokens we skipped. */
9003 cp_lexer_rollback_tokens (parser->lexer);
9004 /* If we aren't looking at a cast-expression, simulate an error so
9005 that the call to cp_parser_error_occurred below returns true. */
9006 if (!cast_expression)
9007 cp_parser_simulate_error (parser);
9008 else
9010 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9011 parser->in_type_id_in_expr_p = true;
9012 /* Look for the type-id. */
9013 type = cp_parser_type_id (parser);
9014 /* Look for the closing `)'. */
9015 cp_token *close_paren = parens.require_close (parser);
9016 if (close_paren)
9017 close_paren_loc = close_paren->location;
9018 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9021 /* Restore the saved message. */
9022 parser->type_definition_forbidden_message = saved_message;
9024 /* At this point this can only be either a cast or a
9025 parenthesized ctor such as `(T ())' that looks like a cast to
9026 function returning T. */
9027 if (!cp_parser_error_occurred (parser))
9029 /* Only commit if the cast-expression doesn't start with
9030 '++', '--', or '[' in C++11. */
9031 if (cast_expression > 0)
9032 cp_parser_commit_to_topmost_tentative_parse (parser);
9034 expr = cp_parser_cast_expression (parser,
9035 /*address_p=*/false,
9036 /*cast_p=*/true,
9037 /*decltype_p=*/false,
9038 pidk);
9040 if (cp_parser_parse_definitely (parser))
9042 /* Warn about old-style casts, if so requested. */
9043 if (warn_old_style_cast
9044 && !in_system_header_at (input_location)
9045 && !VOID_TYPE_P (type)
9046 && current_lang_name != lang_name_c)
9048 gcc_rich_location rich_loc (input_location);
9049 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9050 expr, type);
9051 warning_at (&rich_loc, OPT_Wold_style_cast,
9052 "use of old-style cast to %q#T", type);
9055 /* Only type conversions to integral or enumeration types
9056 can be used in constant-expressions. */
9057 if (!cast_valid_in_integral_constant_expression_p (type)
9058 && cp_parser_non_integral_constant_expression (parser,
9059 NIC_CAST))
9060 return error_mark_node;
9062 /* Perform the cast. */
9063 /* Make a location:
9064 (TYPE) EXPR
9065 ^~~~~~~~~~~
9066 with start==caret at the open paren, extending to the
9067 end of "expr". */
9068 location_t cast_loc = make_location (open_paren_loc,
9069 open_paren_loc,
9070 expr.get_finish ());
9071 expr = build_c_cast (cast_loc, type, expr);
9072 return expr;
9075 else
9076 cp_parser_abort_tentative_parse (parser);
9079 /* If we get here, then it's not a cast, so it must be a
9080 unary-expression. */
9081 return cp_parser_unary_expression (parser, pidk, address_p,
9082 cast_p, decltype_p);
9085 /* Parse a binary expression of the general form:
9087 pm-expression:
9088 cast-expression
9089 pm-expression .* cast-expression
9090 pm-expression ->* cast-expression
9092 multiplicative-expression:
9093 pm-expression
9094 multiplicative-expression * pm-expression
9095 multiplicative-expression / pm-expression
9096 multiplicative-expression % pm-expression
9098 additive-expression:
9099 multiplicative-expression
9100 additive-expression + multiplicative-expression
9101 additive-expression - multiplicative-expression
9103 shift-expression:
9104 additive-expression
9105 shift-expression << additive-expression
9106 shift-expression >> additive-expression
9108 relational-expression:
9109 shift-expression
9110 relational-expression < shift-expression
9111 relational-expression > shift-expression
9112 relational-expression <= shift-expression
9113 relational-expression >= shift-expression
9115 GNU Extension:
9117 relational-expression:
9118 relational-expression <? shift-expression
9119 relational-expression >? shift-expression
9121 equality-expression:
9122 relational-expression
9123 equality-expression == relational-expression
9124 equality-expression != relational-expression
9126 and-expression:
9127 equality-expression
9128 and-expression & equality-expression
9130 exclusive-or-expression:
9131 and-expression
9132 exclusive-or-expression ^ and-expression
9134 inclusive-or-expression:
9135 exclusive-or-expression
9136 inclusive-or-expression | exclusive-or-expression
9138 logical-and-expression:
9139 inclusive-or-expression
9140 logical-and-expression && inclusive-or-expression
9142 logical-or-expression:
9143 logical-and-expression
9144 logical-or-expression || logical-and-expression
9146 All these are implemented with a single function like:
9148 binary-expression:
9149 simple-cast-expression
9150 binary-expression <token> binary-expression
9152 CAST_P is true if this expression is the target of a cast.
9154 The binops_by_token map is used to get the tree codes for each <token> type.
9155 binary-expressions are associated according to a precedence table. */
9157 #define TOKEN_PRECEDENCE(token) \
9158 (((token->type == CPP_GREATER \
9159 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9160 && !parser->greater_than_is_operator_p) \
9161 ? PREC_NOT_OPERATOR \
9162 : binops_by_token[token->type].prec)
9164 static cp_expr
9165 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9166 bool no_toplevel_fold_p,
9167 bool decltype_p,
9168 enum cp_parser_prec prec,
9169 cp_id_kind * pidk)
9171 cp_parser_expression_stack stack;
9172 cp_parser_expression_stack_entry *sp = &stack[0];
9173 cp_parser_expression_stack_entry current;
9174 cp_expr rhs;
9175 cp_token *token;
9176 enum tree_code rhs_type;
9177 enum cp_parser_prec new_prec, lookahead_prec;
9178 tree overload;
9180 /* Parse the first expression. */
9181 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9182 ? TRUTH_NOT_EXPR : ERROR_MARK);
9183 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9184 cast_p, decltype_p, pidk);
9185 current.prec = prec;
9187 if (cp_parser_error_occurred (parser))
9188 return error_mark_node;
9190 for (;;)
9192 /* Get an operator token. */
9193 token = cp_lexer_peek_token (parser->lexer);
9195 if (warn_cxx11_compat
9196 && token->type == CPP_RSHIFT
9197 && !parser->greater_than_is_operator_p)
9199 if (warning_at (token->location, OPT_Wc__11_compat,
9200 "%<>>%> operator is treated"
9201 " as two right angle brackets in C++11"))
9202 inform (token->location,
9203 "suggest parentheses around %<>>%> expression");
9206 new_prec = TOKEN_PRECEDENCE (token);
9207 if (new_prec != PREC_NOT_OPERATOR
9208 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9209 /* This is a fold-expression; handle it later. */
9210 new_prec = PREC_NOT_OPERATOR;
9212 /* Popping an entry off the stack means we completed a subexpression:
9213 - either we found a token which is not an operator (`>' where it is not
9214 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9215 will happen repeatedly;
9216 - or, we found an operator which has lower priority. This is the case
9217 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9218 parsing `3 * 4'. */
9219 if (new_prec <= current.prec)
9221 if (sp == stack)
9222 break;
9223 else
9224 goto pop;
9227 get_rhs:
9228 current.tree_type = binops_by_token[token->type].tree_type;
9229 current.loc = token->location;
9231 /* We used the operator token. */
9232 cp_lexer_consume_token (parser->lexer);
9234 /* For "false && x" or "true || x", x will never be executed;
9235 disable warnings while evaluating it. */
9236 if (current.tree_type == TRUTH_ANDIF_EXPR)
9237 c_inhibit_evaluation_warnings +=
9238 cp_fully_fold (current.lhs) == truthvalue_false_node;
9239 else if (current.tree_type == TRUTH_ORIF_EXPR)
9240 c_inhibit_evaluation_warnings +=
9241 cp_fully_fold (current.lhs) == truthvalue_true_node;
9243 /* Extract another operand. It may be the RHS of this expression
9244 or the LHS of a new, higher priority expression. */
9245 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9246 ? TRUTH_NOT_EXPR : ERROR_MARK);
9247 rhs = cp_parser_simple_cast_expression (parser);
9249 /* Get another operator token. Look up its precedence to avoid
9250 building a useless (immediately popped) stack entry for common
9251 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9252 token = cp_lexer_peek_token (parser->lexer);
9253 lookahead_prec = TOKEN_PRECEDENCE (token);
9254 if (lookahead_prec != PREC_NOT_OPERATOR
9255 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9256 lookahead_prec = PREC_NOT_OPERATOR;
9257 if (lookahead_prec > new_prec)
9259 /* ... and prepare to parse the RHS of the new, higher priority
9260 expression. Since precedence levels on the stack are
9261 monotonically increasing, we do not have to care about
9262 stack overflows. */
9263 *sp = current;
9264 ++sp;
9265 current.lhs = rhs;
9266 current.lhs_type = rhs_type;
9267 current.prec = new_prec;
9268 new_prec = lookahead_prec;
9269 goto get_rhs;
9271 pop:
9272 lookahead_prec = new_prec;
9273 /* If the stack is not empty, we have parsed into LHS the right side
9274 (`4' in the example above) of an expression we had suspended.
9275 We can use the information on the stack to recover the LHS (`3')
9276 from the stack together with the tree code (`MULT_EXPR'), and
9277 the precedence of the higher level subexpression
9278 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9279 which will be used to actually build the additive expression. */
9280 rhs = current.lhs;
9281 rhs_type = current.lhs_type;
9282 --sp;
9283 current = *sp;
9286 /* Undo the disabling of warnings done above. */
9287 if (current.tree_type == TRUTH_ANDIF_EXPR)
9288 c_inhibit_evaluation_warnings -=
9289 cp_fully_fold (current.lhs) == truthvalue_false_node;
9290 else if (current.tree_type == TRUTH_ORIF_EXPR)
9291 c_inhibit_evaluation_warnings -=
9292 cp_fully_fold (current.lhs) == truthvalue_true_node;
9294 if (warn_logical_not_paren
9295 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9296 && current.lhs_type == TRUTH_NOT_EXPR
9297 /* Avoid warning for !!x == y. */
9298 && (TREE_CODE (current.lhs) != NE_EXPR
9299 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9300 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9301 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9302 /* Avoid warning for !b == y where b is boolean. */
9303 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9304 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9305 != BOOLEAN_TYPE))))
9306 /* Avoid warning for !!b == y where b is boolean. */
9307 && (!DECL_P (current.lhs)
9308 || TREE_TYPE (current.lhs) == NULL_TREE
9309 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9310 warn_logical_not_parentheses (current.loc, current.tree_type,
9311 current.lhs, maybe_constant_value (rhs));
9313 overload = NULL;
9315 location_t combined_loc = make_location (current.loc,
9316 current.lhs.get_start (),
9317 rhs.get_finish ());
9319 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9320 ERROR_MARK for everything that is not a binary expression.
9321 This makes warn_about_parentheses miss some warnings that
9322 involve unary operators. For unary expressions we should
9323 pass the correct tree_code unless the unary expression was
9324 surrounded by parentheses.
9326 if (no_toplevel_fold_p
9327 && lookahead_prec <= current.prec
9328 && sp == stack)
9330 if (current.lhs == error_mark_node || rhs == error_mark_node)
9331 current.lhs = error_mark_node;
9332 else
9334 current.lhs
9335 = build_min (current.tree_type,
9336 TREE_CODE_CLASS (current.tree_type)
9337 == tcc_comparison
9338 ? boolean_type_node : TREE_TYPE (current.lhs),
9339 current.lhs.get_value (), rhs.get_value ());
9340 SET_EXPR_LOCATION (current.lhs, combined_loc);
9343 else
9345 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9346 current.lhs, current.lhs_type,
9347 rhs, rhs_type, &overload,
9348 complain_flags (decltype_p));
9349 /* TODO: build_x_binary_op doesn't always honor the location. */
9350 current.lhs.set_location (combined_loc);
9352 current.lhs_type = current.tree_type;
9354 /* If the binary operator required the use of an overloaded operator,
9355 then this expression cannot be an integral constant-expression.
9356 An overloaded operator can be used even if both operands are
9357 otherwise permissible in an integral constant-expression if at
9358 least one of the operands is of enumeration type. */
9360 if (overload
9361 && cp_parser_non_integral_constant_expression (parser,
9362 NIC_OVERLOADED))
9363 return error_mark_node;
9366 return current.lhs;
9369 static cp_expr
9370 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9371 bool no_toplevel_fold_p,
9372 enum cp_parser_prec prec,
9373 cp_id_kind * pidk)
9375 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9376 /*decltype*/false, prec, pidk);
9379 /* Parse the `? expression : assignment-expression' part of a
9380 conditional-expression. The LOGICAL_OR_EXPR is the
9381 logical-or-expression that started the conditional-expression.
9382 Returns a representation of the entire conditional-expression.
9384 This routine is used by cp_parser_assignment_expression.
9386 ? expression : assignment-expression
9388 GNU Extensions:
9390 ? : assignment-expression */
9392 static tree
9393 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9395 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9396 cp_expr assignment_expr;
9397 struct cp_token *token;
9398 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9400 /* Consume the `?' token. */
9401 cp_lexer_consume_token (parser->lexer);
9402 token = cp_lexer_peek_token (parser->lexer);
9403 if (cp_parser_allow_gnu_extensions_p (parser)
9404 && token->type == CPP_COLON)
9406 pedwarn (token->location, OPT_Wpedantic,
9407 "ISO C++ does not allow ?: with omitted middle operand");
9408 /* Implicit true clause. */
9409 expr = NULL_TREE;
9410 c_inhibit_evaluation_warnings +=
9411 folded_logical_or_expr == truthvalue_true_node;
9412 warn_for_omitted_condop (token->location, logical_or_expr);
9414 else
9416 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9417 parser->colon_corrects_to_scope_p = false;
9418 /* Parse the expression. */
9419 c_inhibit_evaluation_warnings +=
9420 folded_logical_or_expr == truthvalue_false_node;
9421 expr = cp_parser_expression (parser);
9422 c_inhibit_evaluation_warnings +=
9423 ((folded_logical_or_expr == truthvalue_true_node)
9424 - (folded_logical_or_expr == truthvalue_false_node));
9425 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9428 /* The next token should be a `:'. */
9429 cp_parser_require (parser, CPP_COLON, RT_COLON);
9430 /* Parse the assignment-expression. */
9431 assignment_expr = cp_parser_assignment_expression (parser);
9432 c_inhibit_evaluation_warnings -=
9433 folded_logical_or_expr == truthvalue_true_node;
9435 /* Make a location:
9436 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9437 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9438 with the caret at the "?", ranging from the start of
9439 the logical_or_expr to the end of the assignment_expr. */
9440 loc = make_location (loc,
9441 logical_or_expr.get_start (),
9442 assignment_expr.get_finish ());
9444 /* Build the conditional-expression. */
9445 return build_x_conditional_expr (loc, logical_or_expr,
9446 expr,
9447 assignment_expr,
9448 tf_warning_or_error);
9451 /* Parse an assignment-expression.
9453 assignment-expression:
9454 conditional-expression
9455 logical-or-expression assignment-operator assignment_expression
9456 throw-expression
9458 CAST_P is true if this expression is the target of a cast.
9459 DECLTYPE_P is true if this expression is the operand of decltype.
9461 Returns a representation for the expression. */
9463 static cp_expr
9464 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9465 bool cast_p, bool decltype_p)
9467 cp_expr expr;
9469 /* If the next token is the `throw' keyword, then we're looking at
9470 a throw-expression. */
9471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9472 expr = cp_parser_throw_expression (parser);
9473 /* Otherwise, it must be that we are looking at a
9474 logical-or-expression. */
9475 else
9477 /* Parse the binary expressions (logical-or-expression). */
9478 expr = cp_parser_binary_expression (parser, cast_p, false,
9479 decltype_p,
9480 PREC_NOT_OPERATOR, pidk);
9481 /* If the next token is a `?' then we're actually looking at a
9482 conditional-expression. */
9483 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9484 return cp_parser_question_colon_clause (parser, expr);
9485 else
9487 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9489 /* If it's an assignment-operator, we're using the second
9490 production. */
9491 enum tree_code assignment_operator
9492 = cp_parser_assignment_operator_opt (parser);
9493 if (assignment_operator != ERROR_MARK)
9495 bool non_constant_p;
9497 /* Parse the right-hand side of the assignment. */
9498 cp_expr rhs = cp_parser_initializer_clause (parser,
9499 &non_constant_p);
9501 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9502 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9504 /* An assignment may not appear in a
9505 constant-expression. */
9506 if (cp_parser_non_integral_constant_expression (parser,
9507 NIC_ASSIGNMENT))
9508 return error_mark_node;
9509 /* Build the assignment expression. Its default
9510 location:
9511 LHS = RHS
9512 ~~~~^~~~~
9513 is the location of the '=' token as the
9514 caret, ranging from the start of the lhs to the
9515 end of the rhs. */
9516 loc = make_location (loc,
9517 expr.get_start (),
9518 rhs.get_finish ());
9519 expr = build_x_modify_expr (loc, expr,
9520 assignment_operator,
9521 rhs,
9522 complain_flags (decltype_p));
9523 /* TODO: build_x_modify_expr doesn't honor the location,
9524 so we must set it here. */
9525 expr.set_location (loc);
9530 return expr;
9533 /* Parse an (optional) assignment-operator.
9535 assignment-operator: one of
9536 = *= /= %= += -= >>= <<= &= ^= |=
9538 GNU Extension:
9540 assignment-operator: one of
9541 <?= >?=
9543 If the next token is an assignment operator, the corresponding tree
9544 code is returned, and the token is consumed. For example, for
9545 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9546 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9547 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9548 operator, ERROR_MARK is returned. */
9550 static enum tree_code
9551 cp_parser_assignment_operator_opt (cp_parser* parser)
9553 enum tree_code op;
9554 cp_token *token;
9556 /* Peek at the next token. */
9557 token = cp_lexer_peek_token (parser->lexer);
9559 switch (token->type)
9561 case CPP_EQ:
9562 op = NOP_EXPR;
9563 break;
9565 case CPP_MULT_EQ:
9566 op = MULT_EXPR;
9567 break;
9569 case CPP_DIV_EQ:
9570 op = TRUNC_DIV_EXPR;
9571 break;
9573 case CPP_MOD_EQ:
9574 op = TRUNC_MOD_EXPR;
9575 break;
9577 case CPP_PLUS_EQ:
9578 op = PLUS_EXPR;
9579 break;
9581 case CPP_MINUS_EQ:
9582 op = MINUS_EXPR;
9583 break;
9585 case CPP_RSHIFT_EQ:
9586 op = RSHIFT_EXPR;
9587 break;
9589 case CPP_LSHIFT_EQ:
9590 op = LSHIFT_EXPR;
9591 break;
9593 case CPP_AND_EQ:
9594 op = BIT_AND_EXPR;
9595 break;
9597 case CPP_XOR_EQ:
9598 op = BIT_XOR_EXPR;
9599 break;
9601 case CPP_OR_EQ:
9602 op = BIT_IOR_EXPR;
9603 break;
9605 default:
9606 /* Nothing else is an assignment operator. */
9607 op = ERROR_MARK;
9610 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9611 if (op != ERROR_MARK
9612 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9613 op = ERROR_MARK;
9615 /* If it was an assignment operator, consume it. */
9616 if (op != ERROR_MARK)
9617 cp_lexer_consume_token (parser->lexer);
9619 return op;
9622 /* Parse an expression.
9624 expression:
9625 assignment-expression
9626 expression , assignment-expression
9628 CAST_P is true if this expression is the target of a cast.
9629 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9630 except possibly parenthesized or on the RHS of a comma (N3276).
9632 Returns a representation of the expression. */
9634 static cp_expr
9635 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9636 bool cast_p, bool decltype_p)
9638 cp_expr expression = NULL_TREE;
9639 location_t loc = UNKNOWN_LOCATION;
9641 while (true)
9643 cp_expr assignment_expression;
9645 /* Parse the next assignment-expression. */
9646 assignment_expression
9647 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9649 /* We don't create a temporary for a call that is the immediate operand
9650 of decltype or on the RHS of a comma. But when we see a comma, we
9651 need to create a temporary for a call on the LHS. */
9652 if (decltype_p && !processing_template_decl
9653 && TREE_CODE (assignment_expression) == CALL_EXPR
9654 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9655 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9656 assignment_expression
9657 = build_cplus_new (TREE_TYPE (assignment_expression),
9658 assignment_expression, tf_warning_or_error);
9660 /* If this is the first assignment-expression, we can just
9661 save it away. */
9662 if (!expression)
9663 expression = assignment_expression;
9664 else
9666 /* Create a location with caret at the comma, ranging
9667 from the start of the LHS to the end of the RHS. */
9668 loc = make_location (loc,
9669 expression.get_start (),
9670 assignment_expression.get_finish ());
9671 expression = build_x_compound_expr (loc, expression,
9672 assignment_expression,
9673 complain_flags (decltype_p));
9674 expression.set_location (loc);
9676 /* If the next token is not a comma, or we're in a fold-expression, then
9677 we are done with the expression. */
9678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9679 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9680 break;
9681 /* Consume the `,'. */
9682 loc = cp_lexer_peek_token (parser->lexer)->location;
9683 cp_lexer_consume_token (parser->lexer);
9684 /* A comma operator cannot appear in a constant-expression. */
9685 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9686 expression = error_mark_node;
9689 return expression;
9692 /* Parse a constant-expression.
9694 constant-expression:
9695 conditional-expression
9697 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9698 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9699 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9700 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9701 only parse a conditional-expression, otherwise parse an
9702 assignment-expression. See below for rationale. */
9704 static cp_expr
9705 cp_parser_constant_expression (cp_parser* parser,
9706 bool allow_non_constant_p,
9707 bool *non_constant_p,
9708 bool strict_p)
9710 bool saved_integral_constant_expression_p;
9711 bool saved_allow_non_integral_constant_expression_p;
9712 bool saved_non_integral_constant_expression_p;
9713 cp_expr expression;
9715 /* It might seem that we could simply parse the
9716 conditional-expression, and then check to see if it were
9717 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9718 one that the compiler can figure out is constant, possibly after
9719 doing some simplifications or optimizations. The standard has a
9720 precise definition of constant-expression, and we must honor
9721 that, even though it is somewhat more restrictive.
9723 For example:
9725 int i[(2, 3)];
9727 is not a legal declaration, because `(2, 3)' is not a
9728 constant-expression. The `,' operator is forbidden in a
9729 constant-expression. However, GCC's constant-folding machinery
9730 will fold this operation to an INTEGER_CST for `3'. */
9732 /* Save the old settings. */
9733 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9734 saved_allow_non_integral_constant_expression_p
9735 = parser->allow_non_integral_constant_expression_p;
9736 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9737 /* We are now parsing a constant-expression. */
9738 parser->integral_constant_expression_p = true;
9739 parser->allow_non_integral_constant_expression_p
9740 = (allow_non_constant_p || cxx_dialect >= cxx11);
9741 parser->non_integral_constant_expression_p = false;
9742 /* Although the grammar says "conditional-expression", when not STRICT_P,
9743 we parse an "assignment-expression", which also permits
9744 "throw-expression" and the use of assignment operators. In the case
9745 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9746 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9747 actually essential that we look for an assignment-expression.
9748 For example, cp_parser_initializer_clauses uses this function to
9749 determine whether a particular assignment-expression is in fact
9750 constant. */
9751 if (strict_p)
9753 /* Parse the binary expressions (logical-or-expression). */
9754 expression = cp_parser_binary_expression (parser, false, false, false,
9755 PREC_NOT_OPERATOR, NULL);
9756 /* If the next token is a `?' then we're actually looking at
9757 a conditional-expression; otherwise we're done. */
9758 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9759 expression = cp_parser_question_colon_clause (parser, expression);
9761 else
9762 expression = cp_parser_assignment_expression (parser);
9763 /* Restore the old settings. */
9764 parser->integral_constant_expression_p
9765 = saved_integral_constant_expression_p;
9766 parser->allow_non_integral_constant_expression_p
9767 = saved_allow_non_integral_constant_expression_p;
9768 if (cxx_dialect >= cxx11)
9770 /* Require an rvalue constant expression here; that's what our
9771 callers expect. Reference constant expressions are handled
9772 separately in e.g. cp_parser_template_argument. */
9773 tree decay = expression;
9774 if (TREE_TYPE (expression)
9775 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9776 decay = build_address (expression);
9777 bool is_const = potential_rvalue_constant_expression (decay);
9778 parser->non_integral_constant_expression_p = !is_const;
9779 if (!is_const && !allow_non_constant_p)
9780 require_potential_rvalue_constant_expression (decay);
9782 if (allow_non_constant_p)
9783 *non_constant_p = parser->non_integral_constant_expression_p;
9784 parser->non_integral_constant_expression_p
9785 = saved_non_integral_constant_expression_p;
9787 return expression;
9790 /* Parse __builtin_offsetof.
9792 offsetof-expression:
9793 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9795 offsetof-member-designator:
9796 id-expression
9797 | offsetof-member-designator "." id-expression
9798 | offsetof-member-designator "[" expression "]"
9799 | offsetof-member-designator "->" id-expression */
9801 static cp_expr
9802 cp_parser_builtin_offsetof (cp_parser *parser)
9804 int save_ice_p, save_non_ice_p;
9805 tree type;
9806 cp_expr expr;
9807 cp_id_kind dummy;
9808 cp_token *token;
9809 location_t finish_loc;
9811 /* We're about to accept non-integral-constant things, but will
9812 definitely yield an integral constant expression. Save and
9813 restore these values around our local parsing. */
9814 save_ice_p = parser->integral_constant_expression_p;
9815 save_non_ice_p = parser->non_integral_constant_expression_p;
9817 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9819 /* Consume the "__builtin_offsetof" token. */
9820 cp_lexer_consume_token (parser->lexer);
9821 /* Consume the opening `('. */
9822 matching_parens parens;
9823 parens.require_open (parser);
9824 /* Parse the type-id. */
9825 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9826 type = cp_parser_type_id (parser);
9827 /* Look for the `,'. */
9828 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9829 token = cp_lexer_peek_token (parser->lexer);
9831 /* Build the (type *)null that begins the traditional offsetof macro. */
9832 tree object_ptr
9833 = build_static_cast (build_pointer_type (type), null_pointer_node,
9834 tf_warning_or_error);
9836 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9837 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9838 true, &dummy, token->location);
9839 while (true)
9841 token = cp_lexer_peek_token (parser->lexer);
9842 switch (token->type)
9844 case CPP_OPEN_SQUARE:
9845 /* offsetof-member-designator "[" expression "]" */
9846 expr = cp_parser_postfix_open_square_expression (parser, expr,
9847 true, false);
9848 break;
9850 case CPP_DEREF:
9851 /* offsetof-member-designator "->" identifier */
9852 expr = grok_array_decl (token->location, expr,
9853 integer_zero_node, false);
9854 /* FALLTHRU */
9856 case CPP_DOT:
9857 /* offsetof-member-designator "." identifier */
9858 cp_lexer_consume_token (parser->lexer);
9859 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9860 expr, true, &dummy,
9861 token->location);
9862 break;
9864 case CPP_CLOSE_PAREN:
9865 /* Consume the ")" token. */
9866 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9867 cp_lexer_consume_token (parser->lexer);
9868 goto success;
9870 default:
9871 /* Error. We know the following require will fail, but
9872 that gives the proper error message. */
9873 parens.require_close (parser);
9874 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9875 expr = error_mark_node;
9876 goto failure;
9880 success:
9881 /* Make a location of the form:
9882 __builtin_offsetof (struct s, f)
9883 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9884 with caret at the type-id, ranging from the start of the
9885 "_builtin_offsetof" token to the close paren. */
9886 loc = make_location (loc, start_loc, finish_loc);
9887 /* The result will be an INTEGER_CST, so we need to explicitly
9888 preserve the location. */
9889 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9891 failure:
9892 parser->integral_constant_expression_p = save_ice_p;
9893 parser->non_integral_constant_expression_p = save_non_ice_p;
9895 expr = expr.maybe_add_location_wrapper ();
9896 return expr;
9899 /* Parse a trait expression.
9901 Returns a representation of the expression, the underlying type
9902 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9904 static cp_expr
9905 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9907 cp_trait_kind kind;
9908 tree type1, type2 = NULL_TREE;
9909 bool binary = false;
9910 bool variadic = false;
9912 switch (keyword)
9914 case RID_HAS_NOTHROW_ASSIGN:
9915 kind = CPTK_HAS_NOTHROW_ASSIGN;
9916 break;
9917 case RID_HAS_NOTHROW_CONSTRUCTOR:
9918 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9919 break;
9920 case RID_HAS_NOTHROW_COPY:
9921 kind = CPTK_HAS_NOTHROW_COPY;
9922 break;
9923 case RID_HAS_TRIVIAL_ASSIGN:
9924 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9925 break;
9926 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9927 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9928 break;
9929 case RID_HAS_TRIVIAL_COPY:
9930 kind = CPTK_HAS_TRIVIAL_COPY;
9931 break;
9932 case RID_HAS_TRIVIAL_DESTRUCTOR:
9933 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9934 break;
9935 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9936 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9937 break;
9938 case RID_HAS_VIRTUAL_DESTRUCTOR:
9939 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9940 break;
9941 case RID_IS_ABSTRACT:
9942 kind = CPTK_IS_ABSTRACT;
9943 break;
9944 case RID_IS_AGGREGATE:
9945 kind = CPTK_IS_AGGREGATE;
9946 break;
9947 case RID_IS_BASE_OF:
9948 kind = CPTK_IS_BASE_OF;
9949 binary = true;
9950 break;
9951 case RID_IS_CLASS:
9952 kind = CPTK_IS_CLASS;
9953 break;
9954 case RID_IS_EMPTY:
9955 kind = CPTK_IS_EMPTY;
9956 break;
9957 case RID_IS_ENUM:
9958 kind = CPTK_IS_ENUM;
9959 break;
9960 case RID_IS_FINAL:
9961 kind = CPTK_IS_FINAL;
9962 break;
9963 case RID_IS_LITERAL_TYPE:
9964 kind = CPTK_IS_LITERAL_TYPE;
9965 break;
9966 case RID_IS_POD:
9967 kind = CPTK_IS_POD;
9968 break;
9969 case RID_IS_POLYMORPHIC:
9970 kind = CPTK_IS_POLYMORPHIC;
9971 break;
9972 case RID_IS_SAME_AS:
9973 kind = CPTK_IS_SAME_AS;
9974 binary = true;
9975 break;
9976 case RID_IS_STD_LAYOUT:
9977 kind = CPTK_IS_STD_LAYOUT;
9978 break;
9979 case RID_IS_TRIVIAL:
9980 kind = CPTK_IS_TRIVIAL;
9981 break;
9982 case RID_IS_TRIVIALLY_ASSIGNABLE:
9983 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9984 binary = true;
9985 break;
9986 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9987 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9988 variadic = true;
9989 break;
9990 case RID_IS_TRIVIALLY_COPYABLE:
9991 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9992 break;
9993 case RID_IS_UNION:
9994 kind = CPTK_IS_UNION;
9995 break;
9996 case RID_UNDERLYING_TYPE:
9997 kind = CPTK_UNDERLYING_TYPE;
9998 break;
9999 case RID_BASES:
10000 kind = CPTK_BASES;
10001 break;
10002 case RID_DIRECT_BASES:
10003 kind = CPTK_DIRECT_BASES;
10004 break;
10005 case RID_IS_ASSIGNABLE:
10006 kind = CPTK_IS_ASSIGNABLE;
10007 binary = true;
10008 break;
10009 case RID_IS_CONSTRUCTIBLE:
10010 kind = CPTK_IS_CONSTRUCTIBLE;
10011 variadic = true;
10012 break;
10013 default:
10014 gcc_unreachable ();
10017 /* Get location of initial token. */
10018 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10020 /* Consume the token. */
10021 cp_lexer_consume_token (parser->lexer);
10023 matching_parens parens;
10024 parens.require_open (parser);
10027 type_id_in_expr_sentinel s (parser);
10028 type1 = cp_parser_type_id (parser);
10031 if (type1 == error_mark_node)
10032 return error_mark_node;
10034 if (binary)
10036 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10039 type_id_in_expr_sentinel s (parser);
10040 type2 = cp_parser_type_id (parser);
10043 if (type2 == error_mark_node)
10044 return error_mark_node;
10046 else if (variadic)
10048 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10050 cp_lexer_consume_token (parser->lexer);
10051 tree elt = cp_parser_type_id (parser);
10052 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10054 cp_lexer_consume_token (parser->lexer);
10055 elt = make_pack_expansion (elt);
10057 if (elt == error_mark_node)
10058 return error_mark_node;
10059 type2 = tree_cons (NULL_TREE, elt, type2);
10063 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10064 parens.require_close (parser);
10066 /* Construct a location of the form:
10067 __is_trivially_copyable(_Tp)
10068 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10069 with start == caret, finishing at the close-paren. */
10070 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10072 /* Complete the trait expression, which may mean either processing
10073 the trait expr now or saving it for template instantiation. */
10074 switch (kind)
10076 case CPTK_UNDERLYING_TYPE:
10077 return cp_expr (finish_underlying_type (type1), trait_loc);
10078 case CPTK_BASES:
10079 return cp_expr (finish_bases (type1, false), trait_loc);
10080 case CPTK_DIRECT_BASES:
10081 return cp_expr (finish_bases (type1, true), trait_loc);
10082 default:
10083 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10087 /* Parse a lambda expression.
10089 lambda-expression:
10090 lambda-introducer lambda-declarator [opt] compound-statement
10092 Returns a representation of the expression. */
10094 static cp_expr
10095 cp_parser_lambda_expression (cp_parser* parser)
10097 tree lambda_expr = build_lambda_expr ();
10098 tree type;
10099 bool ok = true;
10100 cp_token *token = cp_lexer_peek_token (parser->lexer);
10101 cp_token_position start = 0;
10103 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10105 if (cp_unevaluated_operand)
10107 if (!token->error_reported)
10109 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10110 "lambda-expression in unevaluated context");
10111 token->error_reported = true;
10113 ok = false;
10115 else if (parser->in_template_argument_list_p)
10117 if (!token->error_reported)
10119 error_at (token->location, "lambda-expression in template-argument");
10120 token->error_reported = true;
10122 ok = false;
10125 /* We may be in the middle of deferred access check. Disable
10126 it now. */
10127 push_deferring_access_checks (dk_no_deferred);
10129 cp_parser_lambda_introducer (parser, lambda_expr);
10131 type = begin_lambda_type (lambda_expr);
10132 if (type == error_mark_node)
10133 return error_mark_node;
10135 record_lambda_scope (lambda_expr);
10137 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10138 determine_visibility (TYPE_NAME (type));
10140 /* Now that we've started the type, add the capture fields for any
10141 explicit captures. */
10142 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10145 /* Inside the class, surrounding template-parameter-lists do not apply. */
10146 unsigned int saved_num_template_parameter_lists
10147 = parser->num_template_parameter_lists;
10148 unsigned char in_statement = parser->in_statement;
10149 bool in_switch_statement_p = parser->in_switch_statement_p;
10150 bool fully_implicit_function_template_p
10151 = parser->fully_implicit_function_template_p;
10152 tree implicit_template_parms = parser->implicit_template_parms;
10153 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10154 bool auto_is_implicit_function_template_parm_p
10155 = parser->auto_is_implicit_function_template_parm_p;
10157 parser->num_template_parameter_lists = 0;
10158 parser->in_statement = 0;
10159 parser->in_switch_statement_p = false;
10160 parser->fully_implicit_function_template_p = false;
10161 parser->implicit_template_parms = 0;
10162 parser->implicit_template_scope = 0;
10163 parser->auto_is_implicit_function_template_parm_p = false;
10165 /* By virtue of defining a local class, a lambda expression has access to
10166 the private variables of enclosing classes. */
10168 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10170 if (ok && cp_parser_error_occurred (parser))
10171 ok = false;
10173 if (ok)
10175 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10176 && cp_parser_start_tentative_firewall (parser))
10177 start = token;
10178 cp_parser_lambda_body (parser, lambda_expr);
10180 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10182 if (cp_parser_skip_to_closing_brace (parser))
10183 cp_lexer_consume_token (parser->lexer);
10186 /* The capture list was built up in reverse order; fix that now. */
10187 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10188 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10190 if (ok)
10191 maybe_add_lambda_conv_op (type);
10193 type = finish_struct (type, /*attributes=*/NULL_TREE);
10195 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10196 parser->in_statement = in_statement;
10197 parser->in_switch_statement_p = in_switch_statement_p;
10198 parser->fully_implicit_function_template_p
10199 = fully_implicit_function_template_p;
10200 parser->implicit_template_parms = implicit_template_parms;
10201 parser->implicit_template_scope = implicit_template_scope;
10202 parser->auto_is_implicit_function_template_parm_p
10203 = auto_is_implicit_function_template_parm_p;
10206 /* This field is only used during parsing of the lambda. */
10207 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10209 /* This lambda shouldn't have any proxies left at this point. */
10210 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10211 /* And now that we're done, push proxies for an enclosing lambda. */
10212 insert_pending_capture_proxies ();
10214 if (ok)
10215 lambda_expr = build_lambda_object (lambda_expr);
10216 else
10217 lambda_expr = error_mark_node;
10219 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10221 pop_deferring_access_checks ();
10223 return lambda_expr;
10226 /* Parse the beginning of a lambda expression.
10228 lambda-introducer:
10229 [ lambda-capture [opt] ]
10231 LAMBDA_EXPR is the current representation of the lambda expression. */
10233 static void
10234 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10236 /* Need commas after the first capture. */
10237 bool first = true;
10239 /* Eat the leading `['. */
10240 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10242 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10243 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10244 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10245 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10246 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10247 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10249 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10251 cp_lexer_consume_token (parser->lexer);
10252 first = false;
10255 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10257 cp_token* capture_token;
10258 tree capture_id;
10259 tree capture_init_expr;
10260 cp_id_kind idk = CP_ID_KIND_NONE;
10261 bool explicit_init_p = false;
10263 enum capture_kind_type
10265 BY_COPY,
10266 BY_REFERENCE
10268 enum capture_kind_type capture_kind = BY_COPY;
10270 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10272 error ("expected end of capture-list");
10273 return;
10276 if (first)
10277 first = false;
10278 else
10279 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10281 /* Possibly capture `this'. */
10282 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10284 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10285 if (cxx_dialect < cxx2a
10286 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10287 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10288 "with by-copy capture default");
10289 cp_lexer_consume_token (parser->lexer);
10290 add_capture (lambda_expr,
10291 /*id=*/this_identifier,
10292 /*initializer=*/finish_this_expr (),
10293 /*by_reference_p=*/true,
10294 explicit_init_p);
10295 continue;
10298 /* Possibly capture `*this'. */
10299 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10300 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10302 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10303 if (cxx_dialect < cxx17)
10304 pedwarn (loc, 0, "%<*this%> capture only available with "
10305 "-std=c++17 or -std=gnu++17");
10306 cp_lexer_consume_token (parser->lexer);
10307 cp_lexer_consume_token (parser->lexer);
10308 add_capture (lambda_expr,
10309 /*id=*/this_identifier,
10310 /*initializer=*/finish_this_expr (),
10311 /*by_reference_p=*/false,
10312 explicit_init_p);
10313 continue;
10316 /* Remember whether we want to capture as a reference or not. */
10317 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10319 capture_kind = BY_REFERENCE;
10320 cp_lexer_consume_token (parser->lexer);
10323 /* Get the identifier. */
10324 capture_token = cp_lexer_peek_token (parser->lexer);
10325 capture_id = cp_parser_identifier (parser);
10327 if (capture_id == error_mark_node)
10328 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10329 delimiters, but I modified this to stop on unnested ']' as well. It
10330 was already changed to stop on unnested '}', so the
10331 "closing_parenthesis" name is no more misleading with my change. */
10333 cp_parser_skip_to_closing_parenthesis (parser,
10334 /*recovering=*/true,
10335 /*or_comma=*/true,
10336 /*consume_paren=*/true);
10337 break;
10340 /* Find the initializer for this capture. */
10341 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10342 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10343 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10345 bool direct, non_constant;
10346 /* An explicit initializer exists. */
10347 if (cxx_dialect < cxx14)
10348 pedwarn (input_location, 0,
10349 "lambda capture initializers "
10350 "only available with -std=c++14 or -std=gnu++14");
10351 capture_init_expr = cp_parser_initializer (parser, &direct,
10352 &non_constant);
10353 explicit_init_p = true;
10354 if (capture_init_expr == NULL_TREE)
10356 error ("empty initializer for lambda init-capture");
10357 capture_init_expr = error_mark_node;
10360 else
10362 const char* error_msg;
10364 /* Turn the identifier into an id-expression. */
10365 capture_init_expr
10366 = cp_parser_lookup_name_simple (parser, capture_id,
10367 capture_token->location);
10369 if (capture_init_expr == error_mark_node)
10371 unqualified_name_lookup_error (capture_id);
10372 continue;
10374 else if (!VAR_P (capture_init_expr)
10375 && TREE_CODE (capture_init_expr) != PARM_DECL)
10377 error_at (capture_token->location,
10378 "capture of non-variable %qE",
10379 capture_init_expr);
10380 if (DECL_P (capture_init_expr))
10381 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10382 "%q#D declared here", capture_init_expr);
10383 continue;
10385 if (VAR_P (capture_init_expr)
10386 && decl_storage_duration (capture_init_expr) != dk_auto)
10388 if (pedwarn (capture_token->location, 0, "capture of variable "
10389 "%qD with non-automatic storage duration",
10390 capture_init_expr))
10391 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10392 "%q#D declared here", capture_init_expr);
10393 continue;
10396 capture_init_expr
10397 = finish_id_expression
10398 (capture_id,
10399 capture_init_expr,
10400 parser->scope,
10401 &idk,
10402 /*integral_constant_expression_p=*/false,
10403 /*allow_non_integral_constant_expression_p=*/false,
10404 /*non_integral_constant_expression_p=*/NULL,
10405 /*template_p=*/false,
10406 /*done=*/true,
10407 /*address_p=*/false,
10408 /*template_arg_p=*/false,
10409 &error_msg,
10410 capture_token->location);
10412 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10414 cp_lexer_consume_token (parser->lexer);
10415 capture_init_expr = make_pack_expansion (capture_init_expr);
10419 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10420 && !explicit_init_p)
10422 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10423 && capture_kind == BY_COPY)
10424 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10425 "of %qD redundant with by-copy capture default",
10426 capture_id);
10427 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10428 && capture_kind == BY_REFERENCE)
10429 pedwarn (capture_token->location, 0, "explicit by-reference "
10430 "capture of %qD redundant with by-reference capture "
10431 "default", capture_id);
10434 add_capture (lambda_expr,
10435 capture_id,
10436 capture_init_expr,
10437 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10438 explicit_init_p);
10440 /* If there is any qualification still in effect, clear it
10441 now; we will be starting fresh with the next capture. */
10442 parser->scope = NULL_TREE;
10443 parser->qualifying_scope = NULL_TREE;
10444 parser->object_scope = NULL_TREE;
10447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10450 /* Parse the (optional) middle of a lambda expression.
10452 lambda-declarator:
10453 < template-parameter-list [opt] >
10454 ( parameter-declaration-clause [opt] )
10455 attribute-specifier [opt]
10456 decl-specifier-seq [opt]
10457 exception-specification [opt]
10458 lambda-return-type-clause [opt]
10460 LAMBDA_EXPR is the current representation of the lambda expression. */
10462 static bool
10463 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10465 /* 5.1.1.4 of the standard says:
10466 If a lambda-expression does not include a lambda-declarator, it is as if
10467 the lambda-declarator were ().
10468 This means an empty parameter list, no attributes, and no exception
10469 specification. */
10470 tree param_list = void_list_node;
10471 tree attributes = NULL_TREE;
10472 tree exception_spec = NULL_TREE;
10473 tree template_param_list = NULL_TREE;
10474 tree tx_qual = NULL_TREE;
10475 tree return_type = NULL_TREE;
10476 cp_decl_specifier_seq lambda_specs;
10477 clear_decl_specs (&lambda_specs);
10479 /* The template-parameter-list is optional, but must begin with
10480 an opening angle if present. */
10481 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10483 if (cxx_dialect < cxx14)
10484 pedwarn (parser->lexer->next_token->location, 0,
10485 "lambda templates are only available with "
10486 "-std=c++14 or -std=gnu++14");
10487 else if (cxx_dialect < cxx2a)
10488 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10489 "lambda templates are only available with "
10490 "-std=c++2a or -std=gnu++2a");
10492 cp_lexer_consume_token (parser->lexer);
10494 template_param_list = cp_parser_template_parameter_list (parser);
10496 cp_parser_skip_to_end_of_template_parameter_list (parser);
10498 /* We just processed one more parameter list. */
10499 ++parser->num_template_parameter_lists;
10502 /* The parameter-declaration-clause is optional (unless
10503 template-parameter-list was given), but must begin with an
10504 opening parenthesis if present. */
10505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10507 matching_parens parens;
10508 parens.consume_open (parser);
10510 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10512 /* Parse parameters. */
10513 param_list = cp_parser_parameter_declaration_clause (parser);
10515 /* Default arguments shall not be specified in the
10516 parameter-declaration-clause of a lambda-declarator. */
10517 if (cxx_dialect < cxx14)
10518 for (tree t = param_list; t; t = TREE_CHAIN (t))
10519 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10520 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10521 "default argument specified for lambda parameter");
10523 parens.require_close (parser);
10525 attributes = cp_parser_attributes_opt (parser);
10527 /* In the decl-specifier-seq of the lambda-declarator, each
10528 decl-specifier shall either be mutable or constexpr. */
10529 int declares_class_or_enum;
10530 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10531 cp_parser_decl_specifier_seq (parser,
10532 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10533 &lambda_specs, &declares_class_or_enum);
10534 if (lambda_specs.storage_class == sc_mutable)
10536 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10537 if (lambda_specs.conflicting_specifiers_p)
10538 error_at (lambda_specs.locations[ds_storage_class],
10539 "duplicate %<mutable%>");
10542 tx_qual = cp_parser_tx_qualifier_opt (parser);
10544 /* Parse optional exception specification. */
10545 exception_spec = cp_parser_exception_specification_opt (parser);
10547 /* Parse optional trailing return type. */
10548 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10550 cp_lexer_consume_token (parser->lexer);
10551 return_type = cp_parser_trailing_type_id (parser);
10554 /* The function parameters must be in scope all the way until after the
10555 trailing-return-type in case of decltype. */
10556 pop_bindings_and_leave_scope ();
10558 else if (template_param_list != NULL_TREE) // generate diagnostic
10559 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10561 /* Create the function call operator.
10563 Messing with declarators like this is no uglier than building up the
10564 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10565 other code. */
10567 cp_decl_specifier_seq return_type_specs;
10568 cp_declarator* declarator;
10569 tree fco;
10570 int quals;
10571 void *p;
10573 clear_decl_specs (&return_type_specs);
10574 if (return_type)
10575 return_type_specs.type = return_type;
10576 else
10577 /* Maybe we will deduce the return type later. */
10578 return_type_specs.type = make_auto ();
10580 if (lambda_specs.locations[ds_constexpr])
10582 if (cxx_dialect >= cxx17)
10583 return_type_specs.locations[ds_constexpr]
10584 = lambda_specs.locations[ds_constexpr];
10585 else
10586 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10587 "lambda only available with -std=c++17 or -std=gnu++17");
10590 p = obstack_alloc (&declarator_obstack, 0);
10592 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10594 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10595 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10596 declarator = make_call_declarator (declarator, param_list, quals,
10597 VIRT_SPEC_UNSPECIFIED,
10598 REF_QUAL_NONE,
10599 tx_qual,
10600 exception_spec,
10601 /*late_return_type=*/NULL_TREE,
10602 /*requires_clause*/NULL_TREE);
10603 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10605 fco = grokmethod (&return_type_specs,
10606 declarator,
10607 attributes);
10608 if (fco != error_mark_node)
10610 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10611 DECL_ARTIFICIAL (fco) = 1;
10612 /* Give the object parameter a different name. */
10613 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10614 if (return_type)
10615 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10617 if (template_param_list)
10619 fco = finish_member_template_decl (fco);
10620 finish_template_decl (template_param_list);
10621 --parser->num_template_parameter_lists;
10623 else if (parser->fully_implicit_function_template_p)
10624 fco = finish_fully_implicit_template (parser, fco);
10626 finish_member_declaration (fco);
10628 obstack_free (&declarator_obstack, p);
10630 return (fco != error_mark_node);
10634 /* Parse the body of a lambda expression, which is simply
10636 compound-statement
10638 but which requires special handling.
10639 LAMBDA_EXPR is the current representation of the lambda expression. */
10641 static void
10642 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10644 bool nested = (current_function_decl != NULL_TREE);
10645 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10646 bool in_function_body = parser->in_function_body;
10648 if (nested)
10649 push_function_context ();
10650 else
10651 /* Still increment function_depth so that we don't GC in the
10652 middle of an expression. */
10653 ++function_depth;
10655 vec<tree> omp_privatization_save;
10656 save_omp_privatization_clauses (omp_privatization_save);
10657 /* Clear this in case we're in the middle of a default argument. */
10658 parser->local_variables_forbidden_p = false;
10659 parser->in_function_body = true;
10662 local_specialization_stack s (lss_copy);
10663 tree fco = lambda_function (lambda_expr);
10664 tree body = start_lambda_function (fco, lambda_expr);
10665 matching_braces braces;
10667 if (braces.require_open (parser))
10669 tree compound_stmt = begin_compound_stmt (0);
10671 /* Originally C++11 required us to peek for 'return expr'; and
10672 process it specially here to deduce the return type. N3638
10673 removed the need for that. */
10675 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10676 cp_parser_label_declaration (parser);
10677 cp_parser_statement_seq_opt (parser, NULL_TREE);
10678 braces.require_close (parser);
10680 finish_compound_stmt (compound_stmt);
10683 finish_lambda_function (body);
10686 restore_omp_privatization_clauses (omp_privatization_save);
10687 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10688 parser->in_function_body = in_function_body;
10689 if (nested)
10690 pop_function_context();
10691 else
10692 --function_depth;
10695 /* Statements [gram.stmt.stmt] */
10697 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10699 static void
10700 add_debug_begin_stmt (location_t loc)
10702 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10703 return;
10704 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10705 /* A concept is never expanded normally. */
10706 return;
10708 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10709 SET_EXPR_LOCATION (stmt, loc);
10710 add_stmt (stmt);
10713 /* Parse a statement.
10715 statement:
10716 labeled-statement
10717 expression-statement
10718 compound-statement
10719 selection-statement
10720 iteration-statement
10721 jump-statement
10722 declaration-statement
10723 try-block
10725 C++11:
10727 statement:
10728 labeled-statement
10729 attribute-specifier-seq (opt) expression-statement
10730 attribute-specifier-seq (opt) compound-statement
10731 attribute-specifier-seq (opt) selection-statement
10732 attribute-specifier-seq (opt) iteration-statement
10733 attribute-specifier-seq (opt) jump-statement
10734 declaration-statement
10735 attribute-specifier-seq (opt) try-block
10737 init-statement:
10738 expression-statement
10739 simple-declaration
10741 TM Extension:
10743 statement:
10744 atomic-statement
10746 IN_COMPOUND is true when the statement is nested inside a
10747 cp_parser_compound_statement; this matters for certain pragmas.
10749 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10750 is a (possibly labeled) if statement which is not enclosed in braces
10751 and has an else clause. This is used to implement -Wparentheses.
10753 CHAIN is a vector of if-else-if conditions. */
10755 static void
10756 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10757 bool in_compound, bool *if_p, vec<tree> *chain,
10758 location_t *loc_after_labels)
10760 tree statement, std_attrs = NULL_TREE;
10761 cp_token *token;
10762 location_t statement_location, attrs_location;
10764 restart:
10765 if (if_p != NULL)
10766 *if_p = false;
10767 /* There is no statement yet. */
10768 statement = NULL_TREE;
10770 saved_token_sentinel saved_tokens (parser->lexer);
10771 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10772 if (c_dialect_objc ())
10773 /* In obj-c++, seeing '[[' might be the either the beginning of
10774 c++11 attributes, or a nested objc-message-expression. So
10775 let's parse the c++11 attributes tentatively. */
10776 cp_parser_parse_tentatively (parser);
10777 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10778 if (c_dialect_objc ())
10780 if (!cp_parser_parse_definitely (parser))
10781 std_attrs = NULL_TREE;
10784 /* Peek at the next token. */
10785 token = cp_lexer_peek_token (parser->lexer);
10786 /* Remember the location of the first token in the statement. */
10787 statement_location = token->location;
10788 add_debug_begin_stmt (statement_location);
10789 /* If this is a keyword, then that will often determine what kind of
10790 statement we have. */
10791 if (token->type == CPP_KEYWORD)
10793 enum rid keyword = token->keyword;
10795 switch (keyword)
10797 case RID_CASE:
10798 case RID_DEFAULT:
10799 /* Looks like a labeled-statement with a case label.
10800 Parse the label, and then use tail recursion to parse
10801 the statement. */
10802 cp_parser_label_for_labeled_statement (parser, std_attrs);
10803 in_compound = false;
10804 goto restart;
10806 case RID_IF:
10807 case RID_SWITCH:
10808 statement = cp_parser_selection_statement (parser, if_p, chain);
10809 break;
10811 case RID_WHILE:
10812 case RID_DO:
10813 case RID_FOR:
10814 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10815 break;
10817 case RID_BREAK:
10818 case RID_CONTINUE:
10819 case RID_RETURN:
10820 case RID_GOTO:
10821 statement = cp_parser_jump_statement (parser);
10822 break;
10824 /* Objective-C++ exception-handling constructs. */
10825 case RID_AT_TRY:
10826 case RID_AT_CATCH:
10827 case RID_AT_FINALLY:
10828 case RID_AT_SYNCHRONIZED:
10829 case RID_AT_THROW:
10830 statement = cp_parser_objc_statement (parser);
10831 break;
10833 case RID_TRY:
10834 statement = cp_parser_try_block (parser);
10835 break;
10837 case RID_NAMESPACE:
10838 /* This must be a namespace alias definition. */
10839 cp_parser_declaration_statement (parser);
10840 return;
10842 case RID_TRANSACTION_ATOMIC:
10843 case RID_TRANSACTION_RELAXED:
10844 case RID_SYNCHRONIZED:
10845 case RID_ATOMIC_NOEXCEPT:
10846 case RID_ATOMIC_CANCEL:
10847 statement = cp_parser_transaction (parser, token);
10848 break;
10849 case RID_TRANSACTION_CANCEL:
10850 statement = cp_parser_transaction_cancel (parser);
10851 break;
10853 default:
10854 /* It might be a keyword like `int' that can start a
10855 declaration-statement. */
10856 break;
10859 else if (token->type == CPP_NAME)
10861 /* If the next token is a `:', then we are looking at a
10862 labeled-statement. */
10863 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10864 if (token->type == CPP_COLON)
10866 /* Looks like a labeled-statement with an ordinary label.
10867 Parse the label, and then use tail recursion to parse
10868 the statement. */
10870 cp_parser_label_for_labeled_statement (parser, std_attrs);
10871 in_compound = false;
10872 goto restart;
10875 /* Anything that starts with a `{' must be a compound-statement. */
10876 else if (token->type == CPP_OPEN_BRACE)
10877 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10878 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10879 a statement all its own. */
10880 else if (token->type == CPP_PRAGMA)
10882 /* Only certain OpenMP pragmas are attached to statements, and thus
10883 are considered statements themselves. All others are not. In
10884 the context of a compound, accept the pragma as a "statement" and
10885 return so that we can check for a close brace. Otherwise we
10886 require a real statement and must go back and read one. */
10887 if (in_compound)
10888 cp_parser_pragma (parser, pragma_compound, if_p);
10889 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10890 goto restart;
10891 return;
10893 else if (token->type == CPP_EOF)
10895 cp_parser_error (parser, "expected statement");
10896 return;
10899 /* Everything else must be a declaration-statement or an
10900 expression-statement. Try for the declaration-statement
10901 first, unless we are looking at a `;', in which case we know that
10902 we have an expression-statement. */
10903 if (!statement)
10905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10907 if (std_attrs != NULL_TREE)
10909 /* Attributes should be parsed as part of the the
10910 declaration, so let's un-parse them. */
10911 saved_tokens.rollback();
10912 std_attrs = NULL_TREE;
10915 cp_parser_parse_tentatively (parser);
10916 /* Try to parse the declaration-statement. */
10917 cp_parser_declaration_statement (parser);
10918 /* If that worked, we're done. */
10919 if (cp_parser_parse_definitely (parser))
10920 return;
10922 /* All preceding labels have been parsed at this point. */
10923 if (loc_after_labels != NULL)
10924 *loc_after_labels = statement_location;
10926 /* Look for an expression-statement instead. */
10927 statement = cp_parser_expression_statement (parser, in_statement_expr);
10929 /* Handle [[fallthrough]];. */
10930 if (attribute_fallthrough_p (std_attrs))
10932 /* The next token after the fallthrough attribute is ';'. */
10933 if (statement == NULL_TREE)
10935 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10936 statement = build_call_expr_internal_loc (statement_location,
10937 IFN_FALLTHROUGH,
10938 void_type_node, 0);
10939 finish_expr_stmt (statement);
10941 else
10942 warning_at (statement_location, OPT_Wattributes,
10943 "%<fallthrough%> attribute not followed by %<;%>");
10944 std_attrs = NULL_TREE;
10948 /* Set the line number for the statement. */
10949 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10950 SET_EXPR_LOCATION (statement, statement_location);
10952 /* Allow "[[fallthrough]];", but warn otherwise. */
10953 if (std_attrs != NULL_TREE)
10954 warning_at (attrs_location,
10955 OPT_Wattributes,
10956 "attributes at the beginning of statement are ignored");
10959 /* Append ATTR to attribute list ATTRS. */
10961 static tree
10962 attr_chainon (tree attrs, tree attr)
10964 if (attrs == error_mark_node)
10965 return error_mark_node;
10966 if (attr == error_mark_node)
10967 return error_mark_node;
10968 return chainon (attrs, attr);
10971 /* Parse the label for a labeled-statement, i.e.
10973 identifier :
10974 case constant-expression :
10975 default :
10977 GNU Extension:
10978 case constant-expression ... constant-expression : statement
10980 When a label is parsed without errors, the label is added to the
10981 parse tree by the finish_* functions, so this function doesn't
10982 have to return the label. */
10984 static void
10985 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10987 cp_token *token;
10988 tree label = NULL_TREE;
10989 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10991 /* The next token should be an identifier. */
10992 token = cp_lexer_peek_token (parser->lexer);
10993 if (token->type != CPP_NAME
10994 && token->type != CPP_KEYWORD)
10996 cp_parser_error (parser, "expected labeled-statement");
10997 return;
11000 /* Remember whether this case or a user-defined label is allowed to fall
11001 through to. */
11002 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11004 parser->colon_corrects_to_scope_p = false;
11005 switch (token->keyword)
11007 case RID_CASE:
11009 tree expr, expr_hi;
11010 cp_token *ellipsis;
11012 /* Consume the `case' token. */
11013 cp_lexer_consume_token (parser->lexer);
11014 /* Parse the constant-expression. */
11015 expr = cp_parser_constant_expression (parser);
11016 if (check_for_bare_parameter_packs (expr))
11017 expr = error_mark_node;
11019 ellipsis = cp_lexer_peek_token (parser->lexer);
11020 if (ellipsis->type == CPP_ELLIPSIS)
11022 /* Consume the `...' token. */
11023 cp_lexer_consume_token (parser->lexer);
11024 expr_hi = cp_parser_constant_expression (parser);
11025 if (check_for_bare_parameter_packs (expr_hi))
11026 expr_hi = error_mark_node;
11028 /* We don't need to emit warnings here, as the common code
11029 will do this for us. */
11031 else
11032 expr_hi = NULL_TREE;
11034 if (parser->in_switch_statement_p)
11036 tree l = finish_case_label (token->location, expr, expr_hi);
11037 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11038 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11040 else
11041 error_at (token->location,
11042 "case label %qE not within a switch statement",
11043 expr);
11045 break;
11047 case RID_DEFAULT:
11048 /* Consume the `default' token. */
11049 cp_lexer_consume_token (parser->lexer);
11051 if (parser->in_switch_statement_p)
11053 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11054 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11055 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11057 else
11058 error_at (token->location, "case label not within a switch statement");
11059 break;
11061 default:
11062 /* Anything else must be an ordinary label. */
11063 label = finish_label_stmt (cp_parser_identifier (parser));
11064 if (label && TREE_CODE (label) == LABEL_DECL)
11065 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11066 break;
11069 /* Require the `:' token. */
11070 cp_parser_require (parser, CPP_COLON, RT_COLON);
11072 /* An ordinary label may optionally be followed by attributes.
11073 However, this is only permitted if the attributes are then
11074 followed by a semicolon. This is because, for backward
11075 compatibility, when parsing
11076 lab: __attribute__ ((unused)) int i;
11077 we want the attribute to attach to "i", not "lab". */
11078 if (label != NULL_TREE
11079 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11081 tree attrs;
11082 cp_parser_parse_tentatively (parser);
11083 attrs = cp_parser_gnu_attributes_opt (parser);
11084 if (attrs == NULL_TREE
11085 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11086 cp_parser_abort_tentative_parse (parser);
11087 else if (!cp_parser_parse_definitely (parser))
11089 else
11090 attributes = attr_chainon (attributes, attrs);
11093 if (attributes != NULL_TREE)
11094 cplus_decl_attributes (&label, attributes, 0);
11096 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11099 /* Parse an expression-statement.
11101 expression-statement:
11102 expression [opt] ;
11104 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11105 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11106 indicates whether this expression-statement is part of an
11107 expression statement. */
11109 static tree
11110 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11112 tree statement = NULL_TREE;
11113 cp_token *token = cp_lexer_peek_token (parser->lexer);
11114 location_t loc = token->location;
11116 /* There might be attribute fallthrough. */
11117 tree attr = cp_parser_gnu_attributes_opt (parser);
11119 /* If the next token is a ';', then there is no expression
11120 statement. */
11121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11123 statement = cp_parser_expression (parser);
11124 if (statement == error_mark_node
11125 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11127 cp_parser_skip_to_end_of_block_or_statement (parser);
11128 return error_mark_node;
11132 /* Handle [[fallthrough]];. */
11133 if (attribute_fallthrough_p (attr))
11135 /* The next token after the fallthrough attribute is ';'. */
11136 if (statement == NULL_TREE)
11137 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11138 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11139 void_type_node, 0);
11140 else
11141 warning_at (loc, OPT_Wattributes,
11142 "%<fallthrough%> attribute not followed by %<;%>");
11143 attr = NULL_TREE;
11146 /* Allow "[[fallthrough]];", but warn otherwise. */
11147 if (attr != NULL_TREE)
11148 warning_at (loc, OPT_Wattributes,
11149 "attributes at the beginning of statement are ignored");
11151 /* Give a helpful message for "A<T>::type t;" and the like. */
11152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11153 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11155 if (TREE_CODE (statement) == SCOPE_REF)
11156 error_at (token->location, "need %<typename%> before %qE because "
11157 "%qT is a dependent scope",
11158 statement, TREE_OPERAND (statement, 0));
11159 else if (is_overloaded_fn (statement)
11160 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11162 /* A::A a; */
11163 tree fn = get_first_fn (statement);
11164 error_at (token->location,
11165 "%<%T::%D%> names the constructor, not the type",
11166 DECL_CONTEXT (fn), DECL_NAME (fn));
11170 /* Consume the final `;'. */
11171 cp_parser_consume_semicolon_at_end_of_statement (parser);
11173 if (in_statement_expr
11174 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11175 /* This is the final expression statement of a statement
11176 expression. */
11177 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11178 else if (statement)
11179 statement = finish_expr_stmt (statement);
11181 return statement;
11184 /* Parse a compound-statement.
11186 compound-statement:
11187 { statement-seq [opt] }
11189 GNU extension:
11191 compound-statement:
11192 { label-declaration-seq [opt] statement-seq [opt] }
11194 label-declaration-seq:
11195 label-declaration
11196 label-declaration-seq label-declaration
11198 Returns a tree representing the statement. */
11200 static tree
11201 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11202 int bcs_flags, bool function_body)
11204 tree compound_stmt;
11205 matching_braces braces;
11207 /* Consume the `{'. */
11208 if (!braces.require_open (parser))
11209 return error_mark_node;
11210 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11211 && !function_body && cxx_dialect < cxx14)
11212 pedwarn (input_location, OPT_Wpedantic,
11213 "compound-statement in %<constexpr%> function");
11214 /* Begin the compound-statement. */
11215 compound_stmt = begin_compound_stmt (bcs_flags);
11216 /* If the next keyword is `__label__' we have a label declaration. */
11217 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11218 cp_parser_label_declaration (parser);
11219 /* Parse an (optional) statement-seq. */
11220 cp_parser_statement_seq_opt (parser, in_statement_expr);
11221 /* Finish the compound-statement. */
11222 finish_compound_stmt (compound_stmt);
11223 /* Consume the `}'. */
11224 braces.require_close (parser);
11226 return compound_stmt;
11229 /* Parse an (optional) statement-seq.
11231 statement-seq:
11232 statement
11233 statement-seq [opt] statement */
11235 static void
11236 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11238 /* Scan statements until there aren't any more. */
11239 while (true)
11241 cp_token *token = cp_lexer_peek_token (parser->lexer);
11243 /* If we are looking at a `}', then we have run out of
11244 statements; the same is true if we have reached the end
11245 of file, or have stumbled upon a stray '@end'. */
11246 if (token->type == CPP_CLOSE_BRACE
11247 || token->type == CPP_EOF
11248 || token->type == CPP_PRAGMA_EOL
11249 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11250 break;
11252 /* If we are in a compound statement and find 'else' then
11253 something went wrong. */
11254 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11256 if (parser->in_statement & IN_IF_STMT)
11257 break;
11258 else
11260 token = cp_lexer_consume_token (parser->lexer);
11261 error_at (token->location, "%<else%> without a previous %<if%>");
11265 /* Parse the statement. */
11266 cp_parser_statement (parser, in_statement_expr, true, NULL);
11270 /* Return true if we're looking at (init; cond), false otherwise. */
11272 static bool
11273 cp_parser_init_statement_p (cp_parser *parser)
11275 /* Save tokens so that we can put them back. */
11276 cp_lexer_save_tokens (parser->lexer);
11278 /* Look for ';' that is not nested in () or {}. */
11279 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11280 /*recovering=*/false,
11281 CPP_SEMICOLON,
11282 /*consume_paren=*/false);
11284 /* Roll back the tokens we skipped. */
11285 cp_lexer_rollback_tokens (parser->lexer);
11287 return ret == -1;
11290 /* Parse a selection-statement.
11292 selection-statement:
11293 if ( init-statement [opt] condition ) statement
11294 if ( init-statement [opt] condition ) statement else statement
11295 switch ( init-statement [opt] condition ) statement
11297 Returns the new IF_STMT or SWITCH_STMT.
11299 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11300 is a (possibly labeled) if statement which is not enclosed in
11301 braces and has an else clause. This is used to implement
11302 -Wparentheses.
11304 CHAIN is a vector of if-else-if conditions. This is used to implement
11305 -Wduplicated-cond. */
11307 static tree
11308 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11309 vec<tree> *chain)
11311 cp_token *token;
11312 enum rid keyword;
11313 token_indent_info guard_tinfo;
11315 if (if_p != NULL)
11316 *if_p = false;
11318 /* Peek at the next token. */
11319 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11320 guard_tinfo = get_token_indent_info (token);
11322 /* See what kind of keyword it is. */
11323 keyword = token->keyword;
11324 switch (keyword)
11326 case RID_IF:
11327 case RID_SWITCH:
11329 tree statement;
11330 tree condition;
11332 bool cx = false;
11333 if (keyword == RID_IF
11334 && cp_lexer_next_token_is_keyword (parser->lexer,
11335 RID_CONSTEXPR))
11337 cx = true;
11338 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11339 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11340 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11341 "with -std=c++17 or -std=gnu++17");
11344 /* Look for the `('. */
11345 matching_parens parens;
11346 if (!parens.require_open (parser))
11348 cp_parser_skip_to_end_of_statement (parser);
11349 return error_mark_node;
11352 /* Begin the selection-statement. */
11353 if (keyword == RID_IF)
11355 statement = begin_if_stmt ();
11356 IF_STMT_CONSTEXPR_P (statement) = cx;
11358 else
11359 statement = begin_switch_stmt ();
11361 /* Parse the optional init-statement. */
11362 if (cp_parser_init_statement_p (parser))
11364 tree decl;
11365 if (cxx_dialect < cxx17)
11366 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11367 "init-statement in selection statements only available "
11368 "with -std=c++17 or -std=gnu++17");
11369 cp_parser_init_statement (parser, &decl);
11372 /* Parse the condition. */
11373 condition = cp_parser_condition (parser);
11374 /* Look for the `)'. */
11375 if (!parens.require_close (parser))
11376 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11377 /*consume_paren=*/true);
11379 if (keyword == RID_IF)
11381 bool nested_if;
11382 unsigned char in_statement;
11384 /* Add the condition. */
11385 condition = finish_if_stmt_cond (condition, statement);
11387 if (warn_duplicated_cond)
11388 warn_duplicated_cond_add_or_warn (token->location, condition,
11389 &chain);
11391 /* Parse the then-clause. */
11392 in_statement = parser->in_statement;
11393 parser->in_statement |= IN_IF_STMT;
11395 /* Outside a template, the non-selected branch of a constexpr
11396 if is a 'discarded statement', i.e. unevaluated. */
11397 bool was_discarded = in_discarded_stmt;
11398 bool discard_then = (cx && !processing_template_decl
11399 && integer_zerop (condition));
11400 if (discard_then)
11402 in_discarded_stmt = true;
11403 ++c_inhibit_evaluation_warnings;
11406 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11407 guard_tinfo);
11409 parser->in_statement = in_statement;
11411 finish_then_clause (statement);
11413 if (discard_then)
11415 THEN_CLAUSE (statement) = NULL_TREE;
11416 in_discarded_stmt = was_discarded;
11417 --c_inhibit_evaluation_warnings;
11420 /* If the next token is `else', parse the else-clause. */
11421 if (cp_lexer_next_token_is_keyword (parser->lexer,
11422 RID_ELSE))
11424 bool discard_else = (cx && !processing_template_decl
11425 && integer_nonzerop (condition));
11426 if (discard_else)
11428 in_discarded_stmt = true;
11429 ++c_inhibit_evaluation_warnings;
11432 guard_tinfo
11433 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11434 /* Consume the `else' keyword. */
11435 cp_lexer_consume_token (parser->lexer);
11436 if (warn_duplicated_cond)
11438 if (cp_lexer_next_token_is_keyword (parser->lexer,
11439 RID_IF)
11440 && chain == NULL)
11442 /* We've got "if (COND) else if (COND2)". Start
11443 the condition chain and add COND as the first
11444 element. */
11445 chain = new vec<tree> ();
11446 if (!CONSTANT_CLASS_P (condition)
11447 && !TREE_SIDE_EFFECTS (condition))
11449 /* Wrap it in a NOP_EXPR so that we can set the
11450 location of the condition. */
11451 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11452 condition);
11453 SET_EXPR_LOCATION (e, token->location);
11454 chain->safe_push (e);
11457 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11458 RID_IF))
11460 /* This is if-else without subsequent if. Zap the
11461 condition chain; we would have already warned at
11462 this point. */
11463 delete chain;
11464 chain = NULL;
11467 begin_else_clause (statement);
11468 /* Parse the else-clause. */
11469 cp_parser_implicitly_scoped_statement (parser, NULL,
11470 guard_tinfo, chain);
11472 finish_else_clause (statement);
11474 /* If we are currently parsing a then-clause, then
11475 IF_P will not be NULL. We set it to true to
11476 indicate that this if statement has an else clause.
11477 This may trigger the Wparentheses warning below
11478 when we get back up to the parent if statement. */
11479 if (if_p != NULL)
11480 *if_p = true;
11482 if (discard_else)
11484 ELSE_CLAUSE (statement) = NULL_TREE;
11485 in_discarded_stmt = was_discarded;
11486 --c_inhibit_evaluation_warnings;
11489 else
11491 /* This if statement does not have an else clause. If
11492 NESTED_IF is true, then the then-clause has an if
11493 statement which does have an else clause. We warn
11494 about the potential ambiguity. */
11495 if (nested_if)
11496 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11497 "suggest explicit braces to avoid ambiguous"
11498 " %<else%>");
11499 if (warn_duplicated_cond)
11501 /* We don't need the condition chain anymore. */
11502 delete chain;
11503 chain = NULL;
11507 /* Now we're all done with the if-statement. */
11508 finish_if_stmt (statement);
11510 else
11512 bool in_switch_statement_p;
11513 unsigned char in_statement;
11515 /* Add the condition. */
11516 finish_switch_cond (condition, statement);
11518 /* Parse the body of the switch-statement. */
11519 in_switch_statement_p = parser->in_switch_statement_p;
11520 in_statement = parser->in_statement;
11521 parser->in_switch_statement_p = true;
11522 parser->in_statement |= IN_SWITCH_STMT;
11523 cp_parser_implicitly_scoped_statement (parser, if_p,
11524 guard_tinfo);
11525 parser->in_switch_statement_p = in_switch_statement_p;
11526 parser->in_statement = in_statement;
11528 /* Now we're all done with the switch-statement. */
11529 finish_switch_stmt (statement);
11532 return statement;
11534 break;
11536 default:
11537 cp_parser_error (parser, "expected selection-statement");
11538 return error_mark_node;
11542 /* Parse a condition.
11544 condition:
11545 expression
11546 type-specifier-seq declarator = initializer-clause
11547 type-specifier-seq declarator braced-init-list
11549 GNU Extension:
11551 condition:
11552 type-specifier-seq declarator asm-specification [opt]
11553 attributes [opt] = assignment-expression
11555 Returns the expression that should be tested. */
11557 static tree
11558 cp_parser_condition (cp_parser* parser)
11560 cp_decl_specifier_seq type_specifiers;
11561 const char *saved_message;
11562 int declares_class_or_enum;
11564 /* Try the declaration first. */
11565 cp_parser_parse_tentatively (parser);
11566 /* New types are not allowed in the type-specifier-seq for a
11567 condition. */
11568 saved_message = parser->type_definition_forbidden_message;
11569 parser->type_definition_forbidden_message
11570 = G_("types may not be defined in conditions");
11571 /* Parse the type-specifier-seq. */
11572 cp_parser_decl_specifier_seq (parser,
11573 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11574 &type_specifiers,
11575 &declares_class_or_enum);
11576 /* Restore the saved message. */
11577 parser->type_definition_forbidden_message = saved_message;
11578 /* If all is well, we might be looking at a declaration. */
11579 if (!cp_parser_error_occurred (parser))
11581 tree decl;
11582 tree asm_specification;
11583 tree attributes;
11584 cp_declarator *declarator;
11585 tree initializer = NULL_TREE;
11587 /* Parse the declarator. */
11588 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11589 /*ctor_dtor_or_conv_p=*/NULL,
11590 /*parenthesized_p=*/NULL,
11591 /*member_p=*/false,
11592 /*friend_p=*/false);
11593 /* Parse the attributes. */
11594 attributes = cp_parser_attributes_opt (parser);
11595 /* Parse the asm-specification. */
11596 asm_specification = cp_parser_asm_specification_opt (parser);
11597 /* If the next token is not an `=' or '{', then we might still be
11598 looking at an expression. For example:
11600 if (A(a).x)
11602 looks like a decl-specifier-seq and a declarator -- but then
11603 there is no `=', so this is an expression. */
11604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11605 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11606 cp_parser_simulate_error (parser);
11608 /* If we did see an `=' or '{', then we are looking at a declaration
11609 for sure. */
11610 if (cp_parser_parse_definitely (parser))
11612 tree pushed_scope;
11613 bool non_constant_p;
11614 int flags = LOOKUP_ONLYCONVERTING;
11616 /* Create the declaration. */
11617 decl = start_decl (declarator, &type_specifiers,
11618 /*initialized_p=*/true,
11619 attributes, /*prefix_attributes=*/NULL_TREE,
11620 &pushed_scope);
11622 /* Parse the initializer. */
11623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11625 initializer = cp_parser_braced_list (parser, &non_constant_p);
11626 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11627 flags = 0;
11629 else
11631 /* Consume the `='. */
11632 cp_parser_require (parser, CPP_EQ, RT_EQ);
11633 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11635 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11636 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11638 /* Process the initializer. */
11639 cp_finish_decl (decl,
11640 initializer, !non_constant_p,
11641 asm_specification,
11642 flags);
11644 if (pushed_scope)
11645 pop_scope (pushed_scope);
11647 return convert_from_reference (decl);
11650 /* If we didn't even get past the declarator successfully, we are
11651 definitely not looking at a declaration. */
11652 else
11653 cp_parser_abort_tentative_parse (parser);
11655 /* Otherwise, we are looking at an expression. */
11656 return cp_parser_expression (parser);
11659 /* Parses a for-statement or range-for-statement until the closing ')',
11660 not included. */
11662 static tree
11663 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11665 tree init, scope, decl;
11666 bool is_range_for;
11668 /* Begin the for-statement. */
11669 scope = begin_for_scope (&init);
11671 /* Parse the initialization. */
11672 is_range_for = cp_parser_init_statement (parser, &decl);
11674 if (is_range_for)
11675 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11676 else
11677 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11680 static tree
11681 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11682 unsigned short unroll)
11684 /* Normal for loop */
11685 tree condition = NULL_TREE;
11686 tree expression = NULL_TREE;
11687 tree stmt;
11689 stmt = begin_for_stmt (scope, init);
11690 /* The init-statement has already been parsed in
11691 cp_parser_init_statement, so no work is needed here. */
11692 finish_init_stmt (stmt);
11694 /* If there's a condition, process it. */
11695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11696 condition = cp_parser_condition (parser);
11697 else if (ivdep)
11699 cp_parser_error (parser, "missing loop condition in loop with "
11700 "%<GCC ivdep%> pragma");
11701 condition = error_mark_node;
11703 else if (unroll)
11705 cp_parser_error (parser, "missing loop condition in loop with "
11706 "%<GCC unroll%> pragma");
11707 condition = error_mark_node;
11709 finish_for_cond (condition, stmt, ivdep, unroll);
11710 /* Look for the `;'. */
11711 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11713 /* If there's an expression, process it. */
11714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11715 expression = cp_parser_expression (parser);
11716 finish_for_expr (expression, stmt);
11718 return stmt;
11721 /* Tries to parse a range-based for-statement:
11723 range-based-for:
11724 decl-specifier-seq declarator : expression
11726 The decl-specifier-seq declarator and the `:' are already parsed by
11727 cp_parser_init_statement. If processing_template_decl it returns a
11728 newly created RANGE_FOR_STMT; if not, it is converted to a
11729 regular FOR_STMT. */
11731 static tree
11732 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11733 bool ivdep, unsigned short unroll)
11735 tree stmt, range_expr;
11736 auto_vec <cxx_binding *, 16> bindings;
11737 auto_vec <tree, 16> names;
11738 tree decomp_first_name = NULL_TREE;
11739 unsigned int decomp_cnt = 0;
11741 /* Get the range declaration momentarily out of the way so that
11742 the range expression doesn't clash with it. */
11743 if (range_decl != error_mark_node)
11745 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11747 tree v = DECL_VALUE_EXPR (range_decl);
11748 /* For decomposition declaration get all of the corresponding
11749 declarations out of the way. */
11750 if (TREE_CODE (v) == ARRAY_REF
11751 && VAR_P (TREE_OPERAND (v, 0))
11752 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11754 tree d = range_decl;
11755 range_decl = TREE_OPERAND (v, 0);
11756 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11757 decomp_first_name = d;
11758 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11760 tree name = DECL_NAME (d);
11761 names.safe_push (name);
11762 bindings.safe_push (IDENTIFIER_BINDING (name));
11763 IDENTIFIER_BINDING (name)
11764 = IDENTIFIER_BINDING (name)->previous;
11768 if (names.is_empty ())
11770 tree name = DECL_NAME (range_decl);
11771 names.safe_push (name);
11772 bindings.safe_push (IDENTIFIER_BINDING (name));
11773 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11777 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11779 bool expr_non_constant_p;
11780 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11782 else
11783 range_expr = cp_parser_expression (parser);
11785 /* Put the range declaration(s) back into scope. */
11786 for (unsigned int i = 0; i < names.length (); i++)
11788 cxx_binding *binding = bindings[i];
11789 binding->previous = IDENTIFIER_BINDING (names[i]);
11790 IDENTIFIER_BINDING (names[i]) = binding;
11793 /* If in template, STMT is converted to a normal for-statement
11794 at instantiation. If not, it is done just ahead. */
11795 if (processing_template_decl)
11797 if (check_for_bare_parameter_packs (range_expr))
11798 range_expr = error_mark_node;
11799 stmt = begin_range_for_stmt (scope, init);
11800 if (ivdep)
11801 RANGE_FOR_IVDEP (stmt) = 1;
11802 if (unroll)
11803 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11804 finish_range_for_decl (stmt, range_decl, range_expr);
11805 if (!type_dependent_expression_p (range_expr)
11806 /* do_auto_deduction doesn't mess with template init-lists. */
11807 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11808 do_range_for_auto_deduction (range_decl, range_expr);
11810 else
11812 stmt = begin_for_stmt (scope, init);
11813 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11814 decomp_first_name, decomp_cnt, ivdep,
11815 unroll);
11817 return stmt;
11820 /* Subroutine of cp_convert_range_for: given the initializer expression,
11821 builds up the range temporary. */
11823 static tree
11824 build_range_temp (tree range_expr)
11826 tree range_type, range_temp;
11828 /* Find out the type deduced by the declaration
11829 `auto &&__range = range_expr'. */
11830 range_type = cp_build_reference_type (make_auto (), true);
11831 range_type = do_auto_deduction (range_type, range_expr,
11832 type_uses_auto (range_type));
11834 /* Create the __range variable. */
11835 range_temp = build_decl (input_location, VAR_DECL,
11836 get_identifier ("__for_range"), range_type);
11837 TREE_USED (range_temp) = 1;
11838 DECL_ARTIFICIAL (range_temp) = 1;
11840 return range_temp;
11843 /* Used by cp_parser_range_for in template context: we aren't going to
11844 do a full conversion yet, but we still need to resolve auto in the
11845 type of the for-range-declaration if present. This is basically
11846 a shortcut version of cp_convert_range_for. */
11848 static void
11849 do_range_for_auto_deduction (tree decl, tree range_expr)
11851 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11852 if (auto_node)
11854 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11855 range_temp = convert_from_reference (build_range_temp (range_expr));
11856 iter_type = (cp_parser_perform_range_for_lookup
11857 (range_temp, &begin_dummy, &end_dummy));
11858 if (iter_type)
11860 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11861 iter_type);
11862 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11863 RO_UNARY_STAR,
11864 tf_warning_or_error);
11865 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11866 iter_decl, auto_node);
11871 /* Converts a range-based for-statement into a normal
11872 for-statement, as per the definition.
11874 for (RANGE_DECL : RANGE_EXPR)
11875 BLOCK
11877 should be equivalent to:
11880 auto &&__range = RANGE_EXPR;
11881 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11882 __begin != __end;
11883 ++__begin)
11885 RANGE_DECL = *__begin;
11886 BLOCK
11890 If RANGE_EXPR is an array:
11891 BEGIN_EXPR = __range
11892 END_EXPR = __range + ARRAY_SIZE(__range)
11893 Else if RANGE_EXPR has a member 'begin' or 'end':
11894 BEGIN_EXPR = __range.begin()
11895 END_EXPR = __range.end()
11896 Else:
11897 BEGIN_EXPR = begin(__range)
11898 END_EXPR = end(__range);
11900 If __range has a member 'begin' but not 'end', or vice versa, we must
11901 still use the second alternative (it will surely fail, however).
11902 When calling begin()/end() in the third alternative we must use
11903 argument dependent lookup, but always considering 'std' as an associated
11904 namespace. */
11906 tree
11907 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11908 tree decomp_first_name, unsigned int decomp_cnt,
11909 bool ivdep, unsigned short unroll)
11911 tree begin, end;
11912 tree iter_type, begin_expr, end_expr;
11913 tree condition, expression;
11915 range_expr = mark_lvalue_use (range_expr);
11917 if (range_decl == error_mark_node || range_expr == error_mark_node)
11918 /* If an error happened previously do nothing or else a lot of
11919 unhelpful errors would be issued. */
11920 begin_expr = end_expr = iter_type = error_mark_node;
11921 else
11923 tree range_temp;
11925 if (VAR_P (range_expr)
11926 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11927 /* Can't bind a reference to an array of runtime bound. */
11928 range_temp = range_expr;
11929 else
11931 range_temp = build_range_temp (range_expr);
11932 pushdecl (range_temp);
11933 cp_finish_decl (range_temp, range_expr,
11934 /*is_constant_init*/false, NULL_TREE,
11935 LOOKUP_ONLYCONVERTING);
11936 range_temp = convert_from_reference (range_temp);
11938 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11939 &begin_expr, &end_expr);
11942 /* The new for initialization statement. */
11943 begin = build_decl (input_location, VAR_DECL,
11944 get_identifier ("__for_begin"), iter_type);
11945 TREE_USED (begin) = 1;
11946 DECL_ARTIFICIAL (begin) = 1;
11947 pushdecl (begin);
11948 cp_finish_decl (begin, begin_expr,
11949 /*is_constant_init*/false, NULL_TREE,
11950 LOOKUP_ONLYCONVERTING);
11952 if (cxx_dialect >= cxx17)
11953 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11954 end = build_decl (input_location, VAR_DECL,
11955 get_identifier ("__for_end"), iter_type);
11956 TREE_USED (end) = 1;
11957 DECL_ARTIFICIAL (end) = 1;
11958 pushdecl (end);
11959 cp_finish_decl (end, end_expr,
11960 /*is_constant_init*/false, NULL_TREE,
11961 LOOKUP_ONLYCONVERTING);
11963 finish_init_stmt (statement);
11965 /* The new for condition. */
11966 condition = build_x_binary_op (input_location, NE_EXPR,
11967 begin, ERROR_MARK,
11968 end, ERROR_MARK,
11969 NULL, tf_warning_or_error);
11970 finish_for_cond (condition, statement, ivdep, unroll);
11972 /* The new increment expression. */
11973 expression = finish_unary_op_expr (input_location,
11974 PREINCREMENT_EXPR, begin,
11975 tf_warning_or_error);
11976 finish_for_expr (expression, statement);
11978 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11979 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11981 /* The declaration is initialized with *__begin inside the loop body. */
11982 cp_finish_decl (range_decl,
11983 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11984 tf_warning_or_error),
11985 /*is_constant_init*/false, NULL_TREE,
11986 LOOKUP_ONLYCONVERTING);
11987 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11988 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11990 return statement;
11993 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11994 We need to solve both at the same time because the method used
11995 depends on the existence of members begin or end.
11996 Returns the type deduced for the iterator expression. */
11998 static tree
11999 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12001 if (error_operand_p (range))
12003 *begin = *end = error_mark_node;
12004 return error_mark_node;
12007 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12009 error ("range-based %<for%> expression of type %qT "
12010 "has incomplete type", TREE_TYPE (range));
12011 *begin = *end = error_mark_node;
12012 return error_mark_node;
12014 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12016 /* If RANGE is an array, we will use pointer arithmetic. */
12017 *begin = decay_conversion (range, tf_warning_or_error);
12018 *end = build_binary_op (input_location, PLUS_EXPR,
12019 range,
12020 array_type_nelts_top (TREE_TYPE (range)),
12021 false);
12022 return TREE_TYPE (*begin);
12024 else
12026 /* If it is not an array, we must do a bit of magic. */
12027 tree id_begin, id_end;
12028 tree member_begin, member_end;
12030 *begin = *end = error_mark_node;
12032 id_begin = get_identifier ("begin");
12033 id_end = get_identifier ("end");
12034 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12035 /*protect=*/2, /*want_type=*/false,
12036 tf_warning_or_error);
12037 member_end = lookup_member (TREE_TYPE (range), id_end,
12038 /*protect=*/2, /*want_type=*/false,
12039 tf_warning_or_error);
12041 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12043 /* Use the member functions. */
12044 *begin = cp_parser_range_for_member_function (range, id_begin);
12045 *end = cp_parser_range_for_member_function (range, id_end);
12047 else
12049 /* Use global functions with ADL. */
12050 vec<tree, va_gc> *vec;
12051 vec = make_tree_vector ();
12053 vec_safe_push (vec, range);
12055 member_begin = perform_koenig_lookup (id_begin, vec,
12056 tf_warning_or_error);
12057 *begin = finish_call_expr (member_begin, &vec, false, true,
12058 tf_warning_or_error);
12059 member_end = perform_koenig_lookup (id_end, vec,
12060 tf_warning_or_error);
12061 *end = finish_call_expr (member_end, &vec, false, true,
12062 tf_warning_or_error);
12064 release_tree_vector (vec);
12067 /* Last common checks. */
12068 if (*begin == error_mark_node || *end == error_mark_node)
12070 /* If one of the expressions is an error do no more checks. */
12071 *begin = *end = error_mark_node;
12072 return error_mark_node;
12074 else if (type_dependent_expression_p (*begin)
12075 || type_dependent_expression_p (*end))
12076 /* Can happen, when, eg, in a template context, Koenig lookup
12077 can't resolve begin/end (c++/58503). */
12078 return NULL_TREE;
12079 else
12081 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12082 /* The unqualified type of the __begin and __end temporaries should
12083 be the same, as required by the multiple auto declaration. */
12084 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12086 if (cxx_dialect >= cxx17
12087 && (build_x_binary_op (input_location, NE_EXPR,
12088 *begin, ERROR_MARK,
12089 *end, ERROR_MARK,
12090 NULL, tf_none)
12091 != error_mark_node))
12092 /* P0184R0 allows __begin and __end to have different types,
12093 but make sure they are comparable so we can give a better
12094 diagnostic. */;
12095 else
12096 error ("inconsistent begin/end types in range-based %<for%> "
12097 "statement: %qT and %qT",
12098 TREE_TYPE (*begin), TREE_TYPE (*end));
12100 return iter_type;
12105 /* Helper function for cp_parser_perform_range_for_lookup.
12106 Builds a tree for RANGE.IDENTIFIER(). */
12108 static tree
12109 cp_parser_range_for_member_function (tree range, tree identifier)
12111 tree member, res;
12112 vec<tree, va_gc> *vec;
12114 member = finish_class_member_access_expr (range, identifier,
12115 false, tf_warning_or_error);
12116 if (member == error_mark_node)
12117 return error_mark_node;
12119 vec = make_tree_vector ();
12120 res = finish_call_expr (member, &vec,
12121 /*disallow_virtual=*/false,
12122 /*koenig_p=*/false,
12123 tf_warning_or_error);
12124 release_tree_vector (vec);
12125 return res;
12128 /* Parse an iteration-statement.
12130 iteration-statement:
12131 while ( condition ) statement
12132 do statement while ( expression ) ;
12133 for ( init-statement condition [opt] ; expression [opt] )
12134 statement
12136 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12138 static tree
12139 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12140 unsigned short unroll)
12142 cp_token *token;
12143 enum rid keyword;
12144 tree statement;
12145 unsigned char in_statement;
12146 token_indent_info guard_tinfo;
12148 /* Peek at the next token. */
12149 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12150 if (!token)
12151 return error_mark_node;
12153 guard_tinfo = get_token_indent_info (token);
12155 /* Remember whether or not we are already within an iteration
12156 statement. */
12157 in_statement = parser->in_statement;
12159 /* See what kind of keyword it is. */
12160 keyword = token->keyword;
12161 switch (keyword)
12163 case RID_WHILE:
12165 tree condition;
12167 /* Begin the while-statement. */
12168 statement = begin_while_stmt ();
12169 /* Look for the `('. */
12170 matching_parens parens;
12171 parens.require_open (parser);
12172 /* Parse the condition. */
12173 condition = cp_parser_condition (parser);
12174 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12175 /* Look for the `)'. */
12176 parens.require_close (parser);
12177 /* Parse the dependent statement. */
12178 parser->in_statement = IN_ITERATION_STMT;
12179 bool prev = note_iteration_stmt_body_start ();
12180 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12181 note_iteration_stmt_body_end (prev);
12182 parser->in_statement = in_statement;
12183 /* We're done with the while-statement. */
12184 finish_while_stmt (statement);
12186 break;
12188 case RID_DO:
12190 tree expression;
12192 /* Begin the do-statement. */
12193 statement = begin_do_stmt ();
12194 /* Parse the body of the do-statement. */
12195 parser->in_statement = IN_ITERATION_STMT;
12196 bool prev = note_iteration_stmt_body_start ();
12197 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12198 note_iteration_stmt_body_end (prev);
12199 parser->in_statement = in_statement;
12200 finish_do_body (statement);
12201 /* Look for the `while' keyword. */
12202 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12203 /* Look for the `('. */
12204 matching_parens parens;
12205 parens.require_open (parser);
12206 /* Parse the expression. */
12207 expression = cp_parser_expression (parser);
12208 /* We're done with the do-statement. */
12209 finish_do_stmt (expression, statement, ivdep, unroll);
12210 /* Look for the `)'. */
12211 parens.require_close (parser);
12212 /* Look for the `;'. */
12213 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12215 break;
12217 case RID_FOR:
12219 /* Look for the `('. */
12220 matching_parens parens;
12221 parens.require_open (parser);
12223 statement = cp_parser_for (parser, ivdep, unroll);
12225 /* Look for the `)'. */
12226 parens.require_close (parser);
12228 /* Parse the body of the for-statement. */
12229 parser->in_statement = IN_ITERATION_STMT;
12230 bool prev = note_iteration_stmt_body_start ();
12231 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12232 note_iteration_stmt_body_end (prev);
12233 parser->in_statement = in_statement;
12235 /* We're done with the for-statement. */
12236 finish_for_stmt (statement);
12238 break;
12240 default:
12241 cp_parser_error (parser, "expected iteration-statement");
12242 statement = error_mark_node;
12243 break;
12246 return statement;
12249 /* Parse a init-statement or the declarator of a range-based-for.
12250 Returns true if a range-based-for declaration is seen.
12252 init-statement:
12253 expression-statement
12254 simple-declaration */
12256 static bool
12257 cp_parser_init_statement (cp_parser* parser, tree *decl)
12259 /* If the next token is a `;', then we have an empty
12260 expression-statement. Grammatically, this is also a
12261 simple-declaration, but an invalid one, because it does not
12262 declare anything. Therefore, if we did not handle this case
12263 specially, we would issue an error message about an invalid
12264 declaration. */
12265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12267 bool is_range_for = false;
12268 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12270 /* A colon is used in range-based for. */
12271 parser->colon_corrects_to_scope_p = false;
12273 /* We're going to speculatively look for a declaration, falling back
12274 to an expression, if necessary. */
12275 cp_parser_parse_tentatively (parser);
12276 /* Parse the declaration. */
12277 cp_parser_simple_declaration (parser,
12278 /*function_definition_allowed_p=*/false,
12279 decl);
12280 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12281 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12283 /* It is a range-for, consume the ':' */
12284 cp_lexer_consume_token (parser->lexer);
12285 is_range_for = true;
12286 if (cxx_dialect < cxx11)
12288 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12289 "range-based %<for%> loops only available with "
12290 "-std=c++11 or -std=gnu++11");
12291 *decl = error_mark_node;
12294 else
12295 /* The ';' is not consumed yet because we told
12296 cp_parser_simple_declaration not to. */
12297 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12299 if (cp_parser_parse_definitely (parser))
12300 return is_range_for;
12301 /* If the tentative parse failed, then we shall need to look for an
12302 expression-statement. */
12304 /* If we are here, it is an expression-statement. */
12305 cp_parser_expression_statement (parser, NULL_TREE);
12306 return false;
12309 /* Parse a jump-statement.
12311 jump-statement:
12312 break ;
12313 continue ;
12314 return expression [opt] ;
12315 return braced-init-list ;
12316 goto identifier ;
12318 GNU extension:
12320 jump-statement:
12321 goto * expression ;
12323 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12325 static tree
12326 cp_parser_jump_statement (cp_parser* parser)
12328 tree statement = error_mark_node;
12329 cp_token *token;
12330 enum rid keyword;
12331 unsigned char in_statement;
12333 /* Peek at the next token. */
12334 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12335 if (!token)
12336 return error_mark_node;
12338 /* See what kind of keyword it is. */
12339 keyword = token->keyword;
12340 switch (keyword)
12342 case RID_BREAK:
12343 in_statement = parser->in_statement & ~IN_IF_STMT;
12344 switch (in_statement)
12346 case 0:
12347 error_at (token->location, "break statement not within loop or switch");
12348 break;
12349 default:
12350 gcc_assert ((in_statement & IN_SWITCH_STMT)
12351 || in_statement == IN_ITERATION_STMT);
12352 statement = finish_break_stmt ();
12353 if (in_statement == IN_ITERATION_STMT)
12354 break_maybe_infinite_loop ();
12355 break;
12356 case IN_OMP_BLOCK:
12357 error_at (token->location, "invalid exit from OpenMP structured block");
12358 break;
12359 case IN_OMP_FOR:
12360 error_at (token->location, "break statement used with OpenMP for loop");
12361 break;
12363 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12364 break;
12366 case RID_CONTINUE:
12367 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12369 case 0:
12370 error_at (token->location, "continue statement not within a loop");
12371 break;
12372 /* Fall through. */
12373 case IN_ITERATION_STMT:
12374 case IN_OMP_FOR:
12375 statement = finish_continue_stmt ();
12376 break;
12377 case IN_OMP_BLOCK:
12378 error_at (token->location, "invalid exit from OpenMP structured block");
12379 break;
12380 default:
12381 gcc_unreachable ();
12383 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12384 break;
12386 case RID_RETURN:
12388 tree expr;
12389 bool expr_non_constant_p;
12391 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12393 cp_lexer_set_source_position (parser->lexer);
12394 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12395 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12397 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12398 expr = cp_parser_expression (parser);
12399 else
12400 /* If the next token is a `;', then there is no
12401 expression. */
12402 expr = NULL_TREE;
12403 /* Build the return-statement. */
12404 if (current_function_auto_return_pattern && in_discarded_stmt)
12405 /* Don't deduce from a discarded return statement. */;
12406 else
12407 statement = finish_return_stmt (expr);
12408 /* Look for the final `;'. */
12409 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12411 break;
12413 case RID_GOTO:
12414 if (parser->in_function_body
12415 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12417 error ("%<goto%> in %<constexpr%> function");
12418 cp_function_chain->invalid_constexpr = true;
12421 /* Create the goto-statement. */
12422 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12424 /* Issue a warning about this use of a GNU extension. */
12425 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12426 /* Consume the '*' token. */
12427 cp_lexer_consume_token (parser->lexer);
12428 /* Parse the dependent expression. */
12429 finish_goto_stmt (cp_parser_expression (parser));
12431 else
12432 finish_goto_stmt (cp_parser_identifier (parser));
12433 /* Look for the final `;'. */
12434 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12435 break;
12437 default:
12438 cp_parser_error (parser, "expected jump-statement");
12439 break;
12442 return statement;
12445 /* Parse a declaration-statement.
12447 declaration-statement:
12448 block-declaration */
12450 static void
12451 cp_parser_declaration_statement (cp_parser* parser)
12453 void *p;
12455 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12456 p = obstack_alloc (&declarator_obstack, 0);
12458 /* Parse the block-declaration. */
12459 cp_parser_block_declaration (parser, /*statement_p=*/true);
12461 /* Free any declarators allocated. */
12462 obstack_free (&declarator_obstack, p);
12465 /* Some dependent statements (like `if (cond) statement'), are
12466 implicitly in their own scope. In other words, if the statement is
12467 a single statement (as opposed to a compound-statement), it is
12468 none-the-less treated as if it were enclosed in braces. Any
12469 declarations appearing in the dependent statement are out of scope
12470 after control passes that point. This function parses a statement,
12471 but ensures that is in its own scope, even if it is not a
12472 compound-statement.
12474 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12475 is a (possibly labeled) if statement which is not enclosed in
12476 braces and has an else clause. This is used to implement
12477 -Wparentheses.
12479 CHAIN is a vector of if-else-if conditions. This is used to implement
12480 -Wduplicated-cond.
12482 Returns the new statement. */
12484 static tree
12485 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12486 const token_indent_info &guard_tinfo,
12487 vec<tree> *chain)
12489 tree statement;
12490 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12491 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12492 token_indent_info body_tinfo
12493 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12495 if (if_p != NULL)
12496 *if_p = false;
12498 /* Mark if () ; with a special NOP_EXPR. */
12499 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12501 cp_lexer_consume_token (parser->lexer);
12502 statement = add_stmt (build_empty_stmt (body_loc));
12504 if (guard_tinfo.keyword == RID_IF
12505 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12506 warning_at (body_loc, OPT_Wempty_body,
12507 "suggest braces around empty body in an %<if%> statement");
12508 else if (guard_tinfo.keyword == RID_ELSE)
12509 warning_at (body_loc, OPT_Wempty_body,
12510 "suggest braces around empty body in an %<else%> statement");
12512 /* if a compound is opened, we simply parse the statement directly. */
12513 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12514 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12515 /* If the token is not a `{', then we must take special action. */
12516 else
12518 /* Create a compound-statement. */
12519 statement = begin_compound_stmt (0);
12520 /* Parse the dependent-statement. */
12521 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12522 &body_loc_after_labels);
12523 /* Finish the dummy compound-statement. */
12524 finish_compound_stmt (statement);
12527 token_indent_info next_tinfo
12528 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12529 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12531 if (body_loc_after_labels != UNKNOWN_LOCATION
12532 && next_tinfo.type != CPP_SEMICOLON)
12533 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12534 guard_tinfo.location, guard_tinfo.keyword);
12536 /* Return the statement. */
12537 return statement;
12540 /* For some dependent statements (like `while (cond) statement'), we
12541 have already created a scope. Therefore, even if the dependent
12542 statement is a compound-statement, we do not want to create another
12543 scope. */
12545 static void
12546 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12547 const token_indent_info &guard_tinfo)
12549 /* If the token is a `{', then we must take special action. */
12550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12552 token_indent_info body_tinfo
12553 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12554 location_t loc_after_labels = UNKNOWN_LOCATION;
12556 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12557 &loc_after_labels);
12558 token_indent_info next_tinfo
12559 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12560 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12562 if (loc_after_labels != UNKNOWN_LOCATION
12563 && next_tinfo.type != CPP_SEMICOLON)
12564 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12565 guard_tinfo.location,
12566 guard_tinfo.keyword);
12568 else
12570 /* Avoid calling cp_parser_compound_statement, so that we
12571 don't create a new scope. Do everything else by hand. */
12572 matching_braces braces;
12573 braces.require_open (parser);
12574 /* If the next keyword is `__label__' we have a label declaration. */
12575 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12576 cp_parser_label_declaration (parser);
12577 /* Parse an (optional) statement-seq. */
12578 cp_parser_statement_seq_opt (parser, NULL_TREE);
12579 braces.require_close (parser);
12583 /* Declarations [gram.dcl.dcl] */
12585 /* Parse an optional declaration-sequence.
12587 declaration-seq:
12588 declaration
12589 declaration-seq declaration */
12591 static void
12592 cp_parser_declaration_seq_opt (cp_parser* parser)
12594 while (true)
12596 cp_token *token;
12598 token = cp_lexer_peek_token (parser->lexer);
12600 if (token->type == CPP_CLOSE_BRACE
12601 || token->type == CPP_EOF
12602 || token->type == CPP_PRAGMA_EOL)
12603 break;
12605 if (token->type == CPP_SEMICOLON)
12607 /* A declaration consisting of a single semicolon is
12608 invalid. Allow it unless we're being pedantic. */
12609 cp_lexer_consume_token (parser->lexer);
12610 if (!in_system_header_at (input_location))
12611 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12612 continue;
12615 /* If we're entering or exiting a region that's implicitly
12616 extern "C", modify the lang context appropriately. */
12617 if (!parser->implicit_extern_c && token->implicit_extern_c)
12619 push_lang_context (lang_name_c);
12620 parser->implicit_extern_c = true;
12622 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12624 pop_lang_context ();
12625 parser->implicit_extern_c = false;
12628 if (token->type == CPP_PRAGMA)
12630 /* A top-level declaration can consist solely of a #pragma.
12631 A nested declaration cannot, so this is done here and not
12632 in cp_parser_declaration. (A #pragma at block scope is
12633 handled in cp_parser_statement.) */
12634 cp_parser_pragma (parser, pragma_external, NULL);
12635 continue;
12638 /* Parse the declaration itself. */
12639 cp_parser_declaration (parser);
12643 /* Parse a declaration.
12645 declaration:
12646 block-declaration
12647 function-definition
12648 template-declaration
12649 explicit-instantiation
12650 explicit-specialization
12651 linkage-specification
12652 namespace-definition
12654 C++17:
12655 deduction-guide
12657 GNU extension:
12659 declaration:
12660 __extension__ declaration */
12662 static void
12663 cp_parser_declaration (cp_parser* parser)
12665 cp_token token1;
12666 cp_token token2;
12667 int saved_pedantic;
12668 void *p;
12669 tree attributes = NULL_TREE;
12671 /* Check for the `__extension__' keyword. */
12672 if (cp_parser_extension_opt (parser, &saved_pedantic))
12674 /* Parse the qualified declaration. */
12675 cp_parser_declaration (parser);
12676 /* Restore the PEDANTIC flag. */
12677 pedantic = saved_pedantic;
12679 return;
12682 /* Try to figure out what kind of declaration is present. */
12683 token1 = *cp_lexer_peek_token (parser->lexer);
12685 if (token1.type != CPP_EOF)
12686 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12687 else
12689 token2.type = CPP_EOF;
12690 token2.keyword = RID_MAX;
12693 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12694 p = obstack_alloc (&declarator_obstack, 0);
12696 /* If the next token is `extern' and the following token is a string
12697 literal, then we have a linkage specification. */
12698 if (token1.keyword == RID_EXTERN
12699 && cp_parser_is_pure_string_literal (&token2))
12700 cp_parser_linkage_specification (parser);
12701 /* If the next token is `template', then we have either a template
12702 declaration, an explicit instantiation, or an explicit
12703 specialization. */
12704 else if (token1.keyword == RID_TEMPLATE)
12706 /* `template <>' indicates a template specialization. */
12707 if (token2.type == CPP_LESS
12708 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12709 cp_parser_explicit_specialization (parser);
12710 /* `template <' indicates a template declaration. */
12711 else if (token2.type == CPP_LESS)
12712 cp_parser_template_declaration (parser, /*member_p=*/false);
12713 /* Anything else must be an explicit instantiation. */
12714 else
12715 cp_parser_explicit_instantiation (parser);
12717 /* If the next token is `export', then we have a template
12718 declaration. */
12719 else if (token1.keyword == RID_EXPORT)
12720 cp_parser_template_declaration (parser, /*member_p=*/false);
12721 /* If the next token is `extern', 'static' or 'inline' and the one
12722 after that is `template', we have a GNU extended explicit
12723 instantiation directive. */
12724 else if (cp_parser_allow_gnu_extensions_p (parser)
12725 && (token1.keyword == RID_EXTERN
12726 || token1.keyword == RID_STATIC
12727 || token1.keyword == RID_INLINE)
12728 && token2.keyword == RID_TEMPLATE)
12729 cp_parser_explicit_instantiation (parser);
12730 /* If the next token is `namespace', check for a named or unnamed
12731 namespace definition. */
12732 else if (token1.keyword == RID_NAMESPACE
12733 && (/* A named namespace definition. */
12734 (token2.type == CPP_NAME
12735 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12736 != CPP_EQ))
12737 || (token2.type == CPP_OPEN_SQUARE
12738 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12739 == CPP_OPEN_SQUARE)
12740 /* An unnamed namespace definition. */
12741 || token2.type == CPP_OPEN_BRACE
12742 || token2.keyword == RID_ATTRIBUTE))
12743 cp_parser_namespace_definition (parser);
12744 /* An inline (associated) namespace definition. */
12745 else if (token1.keyword == RID_INLINE
12746 && token2.keyword == RID_NAMESPACE)
12747 cp_parser_namespace_definition (parser);
12748 /* Objective-C++ declaration/definition. */
12749 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12750 cp_parser_objc_declaration (parser, NULL_TREE);
12751 else if (c_dialect_objc ()
12752 && token1.keyword == RID_ATTRIBUTE
12753 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12754 cp_parser_objc_declaration (parser, attributes);
12755 /* At this point we may have a template declared by a concept
12756 introduction. */
12757 else if (flag_concepts
12758 && cp_parser_template_declaration_after_export (parser,
12759 /*member_p=*/false))
12760 /* We did. */;
12761 else
12762 /* Try to parse a block-declaration, or a function-definition. */
12763 cp_parser_block_declaration (parser, /*statement_p=*/false);
12765 /* Free any declarators allocated. */
12766 obstack_free (&declarator_obstack, p);
12769 /* Parse a block-declaration.
12771 block-declaration:
12772 simple-declaration
12773 asm-definition
12774 namespace-alias-definition
12775 using-declaration
12776 using-directive
12778 GNU Extension:
12780 block-declaration:
12781 __extension__ block-declaration
12783 C++0x Extension:
12785 block-declaration:
12786 static_assert-declaration
12788 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12789 part of a declaration-statement. */
12791 static void
12792 cp_parser_block_declaration (cp_parser *parser,
12793 bool statement_p)
12795 cp_token *token1;
12796 int saved_pedantic;
12798 /* Check for the `__extension__' keyword. */
12799 if (cp_parser_extension_opt (parser, &saved_pedantic))
12801 /* Parse the qualified declaration. */
12802 cp_parser_block_declaration (parser, statement_p);
12803 /* Restore the PEDANTIC flag. */
12804 pedantic = saved_pedantic;
12806 return;
12809 /* Peek at the next token to figure out which kind of declaration is
12810 present. */
12811 token1 = cp_lexer_peek_token (parser->lexer);
12813 /* If the next keyword is `asm', we have an asm-definition. */
12814 if (token1->keyword == RID_ASM)
12816 if (statement_p)
12817 cp_parser_commit_to_tentative_parse (parser);
12818 cp_parser_asm_definition (parser);
12820 /* If the next keyword is `namespace', we have a
12821 namespace-alias-definition. */
12822 else if (token1->keyword == RID_NAMESPACE)
12823 cp_parser_namespace_alias_definition (parser);
12824 /* If the next keyword is `using', we have a
12825 using-declaration, a using-directive, or an alias-declaration. */
12826 else if (token1->keyword == RID_USING)
12828 cp_token *token2;
12830 if (statement_p)
12831 cp_parser_commit_to_tentative_parse (parser);
12832 /* If the token after `using' is `namespace', then we have a
12833 using-directive. */
12834 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12835 if (token2->keyword == RID_NAMESPACE)
12836 cp_parser_using_directive (parser);
12837 /* If the second token after 'using' is '=', then we have an
12838 alias-declaration. */
12839 else if (cxx_dialect >= cxx11
12840 && token2->type == CPP_NAME
12841 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12842 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12843 cp_parser_alias_declaration (parser);
12844 /* Otherwise, it's a using-declaration. */
12845 else
12846 cp_parser_using_declaration (parser,
12847 /*access_declaration_p=*/false);
12849 /* If the next keyword is `__label__' we have a misplaced label
12850 declaration. */
12851 else if (token1->keyword == RID_LABEL)
12853 cp_lexer_consume_token (parser->lexer);
12854 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12855 cp_parser_skip_to_end_of_statement (parser);
12856 /* If the next token is now a `;', consume it. */
12857 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12858 cp_lexer_consume_token (parser->lexer);
12860 /* If the next token is `static_assert' we have a static assertion. */
12861 else if (token1->keyword == RID_STATIC_ASSERT)
12862 cp_parser_static_assert (parser, /*member_p=*/false);
12863 /* Anything else must be a simple-declaration. */
12864 else
12865 cp_parser_simple_declaration (parser, !statement_p,
12866 /*maybe_range_for_decl*/NULL);
12869 /* Parse a simple-declaration.
12871 simple-declaration:
12872 decl-specifier-seq [opt] init-declarator-list [opt] ;
12873 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12874 brace-or-equal-initializer ;
12876 init-declarator-list:
12877 init-declarator
12878 init-declarator-list , init-declarator
12880 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12881 function-definition as a simple-declaration.
12883 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12884 parsed declaration if it is an uninitialized single declarator not followed
12885 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12886 if present, will not be consumed. */
12888 static void
12889 cp_parser_simple_declaration (cp_parser* parser,
12890 bool function_definition_allowed_p,
12891 tree *maybe_range_for_decl)
12893 cp_decl_specifier_seq decl_specifiers;
12894 int declares_class_or_enum;
12895 bool saw_declarator;
12896 location_t comma_loc = UNKNOWN_LOCATION;
12897 location_t init_loc = UNKNOWN_LOCATION;
12899 if (maybe_range_for_decl)
12900 *maybe_range_for_decl = NULL_TREE;
12902 /* Defer access checks until we know what is being declared; the
12903 checks for names appearing in the decl-specifier-seq should be
12904 done as if we were in the scope of the thing being declared. */
12905 push_deferring_access_checks (dk_deferred);
12907 /* Parse the decl-specifier-seq. We have to keep track of whether
12908 or not the decl-specifier-seq declares a named class or
12909 enumeration type, since that is the only case in which the
12910 init-declarator-list is allowed to be empty.
12912 [dcl.dcl]
12914 In a simple-declaration, the optional init-declarator-list can be
12915 omitted only when declaring a class or enumeration, that is when
12916 the decl-specifier-seq contains either a class-specifier, an
12917 elaborated-type-specifier, or an enum-specifier. */
12918 cp_parser_decl_specifier_seq (parser,
12919 CP_PARSER_FLAGS_OPTIONAL,
12920 &decl_specifiers,
12921 &declares_class_or_enum);
12922 /* We no longer need to defer access checks. */
12923 stop_deferring_access_checks ();
12925 /* In a block scope, a valid declaration must always have a
12926 decl-specifier-seq. By not trying to parse declarators, we can
12927 resolve the declaration/expression ambiguity more quickly. */
12928 if (!function_definition_allowed_p
12929 && !decl_specifiers.any_specifiers_p)
12931 cp_parser_error (parser, "expected declaration");
12932 goto done;
12935 /* If the next two tokens are both identifiers, the code is
12936 erroneous. The usual cause of this situation is code like:
12938 T t;
12940 where "T" should name a type -- but does not. */
12941 if (!decl_specifiers.any_type_specifiers_p
12942 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12944 /* If parsing tentatively, we should commit; we really are
12945 looking at a declaration. */
12946 cp_parser_commit_to_tentative_parse (parser);
12947 /* Give up. */
12948 goto done;
12951 /* If we have seen at least one decl-specifier, and the next token
12952 is not a parenthesis, then we must be looking at a declaration.
12953 (After "int (" we might be looking at a functional cast.) */
12954 if (decl_specifiers.any_specifiers_p
12955 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12956 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12957 && !cp_parser_error_occurred (parser))
12958 cp_parser_commit_to_tentative_parse (parser);
12960 /* Look for C++17 decomposition declaration. */
12961 for (size_t n = 1; ; n++)
12962 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12963 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12964 continue;
12965 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12966 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12967 && decl_specifiers.any_specifiers_p)
12969 tree decl
12970 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12971 maybe_range_for_decl,
12972 &init_loc);
12974 /* The next token should be either a `,' or a `;'. */
12975 cp_token *token = cp_lexer_peek_token (parser->lexer);
12976 /* If it's a `;', we are done. */
12977 if (token->type == CPP_SEMICOLON)
12978 goto finish;
12979 else if (maybe_range_for_decl)
12981 if (*maybe_range_for_decl == NULL_TREE)
12982 *maybe_range_for_decl = error_mark_node;
12983 goto finish;
12985 /* Anything else is an error. */
12986 else
12988 /* If we have already issued an error message we don't need
12989 to issue another one. */
12990 if ((decl != error_mark_node
12991 && DECL_INITIAL (decl) != error_mark_node)
12992 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12993 cp_parser_error (parser, "expected %<,%> or %<;%>");
12994 /* Skip tokens until we reach the end of the statement. */
12995 cp_parser_skip_to_end_of_statement (parser);
12996 /* If the next token is now a `;', consume it. */
12997 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12998 cp_lexer_consume_token (parser->lexer);
12999 goto done;
13002 else
13003 break;
13005 tree last_type;
13006 bool auto_specifier_p;
13007 /* NULL_TREE if both variable and function declaration are allowed,
13008 error_mark_node if function declaration are not allowed and
13009 a FUNCTION_DECL that should be diagnosed if it is followed by
13010 variable declarations. */
13011 tree auto_function_declaration;
13013 last_type = NULL_TREE;
13014 auto_specifier_p
13015 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13016 auto_function_declaration = NULL_TREE;
13018 /* Keep going until we hit the `;' at the end of the simple
13019 declaration. */
13020 saw_declarator = false;
13021 while (cp_lexer_next_token_is_not (parser->lexer,
13022 CPP_SEMICOLON))
13024 cp_token *token;
13025 bool function_definition_p;
13026 tree decl;
13027 tree auto_result = NULL_TREE;
13029 if (saw_declarator)
13031 /* If we are processing next declarator, comma is expected */
13032 token = cp_lexer_peek_token (parser->lexer);
13033 gcc_assert (token->type == CPP_COMMA);
13034 cp_lexer_consume_token (parser->lexer);
13035 if (maybe_range_for_decl)
13037 *maybe_range_for_decl = error_mark_node;
13038 if (comma_loc == UNKNOWN_LOCATION)
13039 comma_loc = token->location;
13042 else
13043 saw_declarator = true;
13045 /* Parse the init-declarator. */
13046 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13047 /*checks=*/NULL,
13048 function_definition_allowed_p,
13049 /*member_p=*/false,
13050 declares_class_or_enum,
13051 &function_definition_p,
13052 maybe_range_for_decl,
13053 &init_loc,
13054 &auto_result);
13055 /* If an error occurred while parsing tentatively, exit quickly.
13056 (That usually happens when in the body of a function; each
13057 statement is treated as a declaration-statement until proven
13058 otherwise.) */
13059 if (cp_parser_error_occurred (parser))
13060 goto done;
13062 if (auto_specifier_p && cxx_dialect >= cxx14)
13064 /* If the init-declarator-list contains more than one
13065 init-declarator, they shall all form declarations of
13066 variables. */
13067 if (auto_function_declaration == NULL_TREE)
13068 auto_function_declaration
13069 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13070 else if (TREE_CODE (decl) == FUNCTION_DECL
13071 || auto_function_declaration != error_mark_node)
13073 error_at (decl_specifiers.locations[ds_type_spec],
13074 "non-variable %qD in declaration with more than one "
13075 "declarator with placeholder type",
13076 TREE_CODE (decl) == FUNCTION_DECL
13077 ? decl : auto_function_declaration);
13078 auto_function_declaration = error_mark_node;
13082 if (auto_result
13083 && (!processing_template_decl || !type_uses_auto (auto_result)))
13085 if (last_type
13086 && last_type != error_mark_node
13087 && !same_type_p (auto_result, last_type))
13089 /* If the list of declarators contains more than one declarator,
13090 the type of each declared variable is determined as described
13091 above. If the type deduced for the template parameter U is not
13092 the same in each deduction, the program is ill-formed. */
13093 error_at (decl_specifiers.locations[ds_type_spec],
13094 "inconsistent deduction for %qT: %qT and then %qT",
13095 decl_specifiers.type, last_type, auto_result);
13096 last_type = error_mark_node;
13098 else
13099 last_type = auto_result;
13102 /* Handle function definitions specially. */
13103 if (function_definition_p)
13105 /* If the next token is a `,', then we are probably
13106 processing something like:
13108 void f() {}, *p;
13110 which is erroneous. */
13111 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13113 cp_token *token = cp_lexer_peek_token (parser->lexer);
13114 error_at (token->location,
13115 "mixing"
13116 " declarations and function-definitions is forbidden");
13118 /* Otherwise, we're done with the list of declarators. */
13119 else
13121 pop_deferring_access_checks ();
13122 return;
13125 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13126 *maybe_range_for_decl = decl;
13127 /* The next token should be either a `,' or a `;'. */
13128 token = cp_lexer_peek_token (parser->lexer);
13129 /* If it's a `,', there are more declarators to come. */
13130 if (token->type == CPP_COMMA)
13131 /* will be consumed next time around */;
13132 /* If it's a `;', we are done. */
13133 else if (token->type == CPP_SEMICOLON)
13134 break;
13135 else if (maybe_range_for_decl)
13137 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13138 permerror (decl_specifiers.locations[ds_type_spec],
13139 "types may not be defined in a for-range-declaration");
13140 break;
13142 /* Anything else is an error. */
13143 else
13145 /* If we have already issued an error message we don't need
13146 to issue another one. */
13147 if ((decl != error_mark_node
13148 && DECL_INITIAL (decl) != error_mark_node)
13149 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13150 cp_parser_error (parser, "expected %<,%> or %<;%>");
13151 /* Skip tokens until we reach the end of the statement. */
13152 cp_parser_skip_to_end_of_statement (parser);
13153 /* If the next token is now a `;', consume it. */
13154 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13155 cp_lexer_consume_token (parser->lexer);
13156 goto done;
13158 /* After the first time around, a function-definition is not
13159 allowed -- even if it was OK at first. For example:
13161 int i, f() {}
13163 is not valid. */
13164 function_definition_allowed_p = false;
13167 /* Issue an error message if no declarators are present, and the
13168 decl-specifier-seq does not itself declare a class or
13169 enumeration: [dcl.dcl]/3. */
13170 if (!saw_declarator)
13172 if (cp_parser_declares_only_class_p (parser))
13174 if (!declares_class_or_enum
13175 && decl_specifiers.type
13176 && OVERLOAD_TYPE_P (decl_specifiers.type))
13177 /* Ensure an error is issued anyway when finish_decltype_type,
13178 called via cp_parser_decl_specifier_seq, returns a class or
13179 an enumeration (c++/51786). */
13180 decl_specifiers.type = NULL_TREE;
13181 shadow_tag (&decl_specifiers);
13183 /* Perform any deferred access checks. */
13184 perform_deferred_access_checks (tf_warning_or_error);
13187 /* Consume the `;'. */
13188 finish:
13189 if (!maybe_range_for_decl)
13190 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13191 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13193 if (init_loc != UNKNOWN_LOCATION)
13194 error_at (init_loc, "initializer in range-based %<for%> loop");
13195 if (comma_loc != UNKNOWN_LOCATION)
13196 error_at (comma_loc,
13197 "multiple declarations in range-based %<for%> loop");
13200 done:
13201 pop_deferring_access_checks ();
13204 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13205 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13206 initializer ; */
13208 static tree
13209 cp_parser_decomposition_declaration (cp_parser *parser,
13210 cp_decl_specifier_seq *decl_specifiers,
13211 tree *maybe_range_for_decl,
13212 location_t *init_loc)
13214 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13215 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13216 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13218 /* Parse the identifier-list. */
13219 auto_vec<cp_expr, 10> v;
13220 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13221 while (true)
13223 cp_expr e = cp_parser_identifier (parser);
13224 if (e.get_value () == error_mark_node)
13225 break;
13226 v.safe_push (e);
13227 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13228 break;
13229 cp_lexer_consume_token (parser->lexer);
13232 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13233 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13235 end_loc = UNKNOWN_LOCATION;
13236 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13237 false);
13238 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13239 cp_lexer_consume_token (parser->lexer);
13240 else
13242 cp_parser_skip_to_end_of_statement (parser);
13243 return error_mark_node;
13247 if (cxx_dialect < cxx17)
13248 pedwarn (loc, 0, "structured bindings only available with "
13249 "-std=c++17 or -std=gnu++17");
13251 tree pushed_scope;
13252 cp_declarator *declarator = make_declarator (cdk_decomp);
13253 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13254 declarator->id_loc = loc;
13255 if (ref_qual != REF_QUAL_NONE)
13256 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13257 ref_qual == REF_QUAL_RVALUE,
13258 NULL_TREE);
13259 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13260 NULL_TREE, decl_specifiers->attributes,
13261 &pushed_scope);
13262 tree orig_decl = decl;
13264 unsigned int i;
13265 cp_expr e;
13266 cp_decl_specifier_seq decl_specs;
13267 clear_decl_specs (&decl_specs);
13268 decl_specs.type = make_auto ();
13269 tree prev = decl;
13270 FOR_EACH_VEC_ELT (v, i, e)
13272 if (i == 0)
13273 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13274 else
13275 declarator->u.id.unqualified_name = e.get_value ();
13276 declarator->id_loc = e.get_location ();
13277 tree elt_pushed_scope;
13278 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13279 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13280 if (decl2 == error_mark_node)
13281 decl = error_mark_node;
13282 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13284 /* Ensure we've diagnosed redeclaration if we aren't creating
13285 a new VAR_DECL. */
13286 gcc_assert (errorcount);
13287 decl = error_mark_node;
13289 else
13290 prev = decl2;
13291 if (elt_pushed_scope)
13292 pop_scope (elt_pushed_scope);
13295 if (v.is_empty ())
13297 error_at (loc, "empty structured binding declaration");
13298 decl = error_mark_node;
13301 if (maybe_range_for_decl == NULL
13302 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13304 bool non_constant_p = false, is_direct_init = false;
13305 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13306 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13307 &non_constant_p);
13308 if (initializer == NULL_TREE
13309 || (TREE_CODE (initializer) == TREE_LIST
13310 && TREE_CHAIN (initializer))
13311 || (is_direct_init
13312 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13313 && CONSTRUCTOR_NELTS (initializer) != 1))
13315 error_at (loc, "invalid initializer for structured binding "
13316 "declaration");
13317 initializer = error_mark_node;
13320 if (decl != error_mark_node)
13322 cp_maybe_mangle_decomp (decl, prev, v.length ());
13323 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13324 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13325 cp_finish_decomp (decl, prev, v.length ());
13328 else if (decl != error_mark_node)
13330 *maybe_range_for_decl = prev;
13331 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13332 the underlying DECL. */
13333 cp_finish_decomp (decl, prev, v.length ());
13336 if (pushed_scope)
13337 pop_scope (pushed_scope);
13339 if (decl == error_mark_node && DECL_P (orig_decl))
13341 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13342 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13345 return decl;
13348 /* Parse a decl-specifier-seq.
13350 decl-specifier-seq:
13351 decl-specifier-seq [opt] decl-specifier
13352 decl-specifier attribute-specifier-seq [opt] (C++11)
13354 decl-specifier:
13355 storage-class-specifier
13356 type-specifier
13357 function-specifier
13358 friend
13359 typedef
13361 GNU Extension:
13363 decl-specifier:
13364 attributes
13366 Concepts Extension:
13368 decl-specifier:
13369 concept
13371 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13373 The parser flags FLAGS is used to control type-specifier parsing.
13375 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13376 flags:
13378 1: one of the decl-specifiers is an elaborated-type-specifier
13379 (i.e., a type declaration)
13380 2: one of the decl-specifiers is an enum-specifier or a
13381 class-specifier (i.e., a type definition)
13385 static void
13386 cp_parser_decl_specifier_seq (cp_parser* parser,
13387 cp_parser_flags flags,
13388 cp_decl_specifier_seq *decl_specs,
13389 int* declares_class_or_enum)
13391 bool constructor_possible_p = !parser->in_declarator_p;
13392 bool found_decl_spec = false;
13393 cp_token *start_token = NULL;
13394 cp_decl_spec ds;
13396 /* Clear DECL_SPECS. */
13397 clear_decl_specs (decl_specs);
13399 /* Assume no class or enumeration type is declared. */
13400 *declares_class_or_enum = 0;
13402 /* Keep reading specifiers until there are no more to read. */
13403 while (true)
13405 bool constructor_p;
13406 cp_token *token;
13407 ds = ds_last;
13409 /* Peek at the next token. */
13410 token = cp_lexer_peek_token (parser->lexer);
13412 /* Save the first token of the decl spec list for error
13413 reporting. */
13414 if (!start_token)
13415 start_token = token;
13416 /* Handle attributes. */
13417 if (cp_next_tokens_can_be_attribute_p (parser))
13419 /* Parse the attributes. */
13420 tree attrs = cp_parser_attributes_opt (parser);
13422 /* In a sequence of declaration specifiers, c++11 attributes
13423 appertain to the type that precede them. In that case
13424 [dcl.spec]/1 says:
13426 The attribute-specifier-seq affects the type only for
13427 the declaration it appears in, not other declarations
13428 involving the same type.
13430 But for now let's force the user to position the
13431 attribute either at the beginning of the declaration or
13432 after the declarator-id, which would clearly mean that it
13433 applies to the declarator. */
13434 if (cxx11_attribute_p (attrs))
13436 if (!found_decl_spec)
13437 /* The c++11 attribute is at the beginning of the
13438 declaration. It appertains to the entity being
13439 declared. */;
13440 else
13442 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13444 /* This is an attribute following a
13445 class-specifier. */
13446 if (decl_specs->type_definition_p)
13447 warn_misplaced_attr_for_class_type (token->location,
13448 decl_specs->type);
13449 attrs = NULL_TREE;
13451 else
13453 decl_specs->std_attributes
13454 = attr_chainon (decl_specs->std_attributes, attrs);
13455 if (decl_specs->locations[ds_std_attribute] == 0)
13456 decl_specs->locations[ds_std_attribute] = token->location;
13458 continue;
13462 decl_specs->attributes
13463 = attr_chainon (decl_specs->attributes, attrs);
13464 if (decl_specs->locations[ds_attribute] == 0)
13465 decl_specs->locations[ds_attribute] = token->location;
13466 continue;
13468 /* Assume we will find a decl-specifier keyword. */
13469 found_decl_spec = true;
13470 /* If the next token is an appropriate keyword, we can simply
13471 add it to the list. */
13472 switch (token->keyword)
13474 /* decl-specifier:
13475 friend
13476 constexpr */
13477 case RID_FRIEND:
13478 if (!at_class_scope_p ())
13480 gcc_rich_location richloc (token->location);
13481 richloc.add_fixit_remove ();
13482 error_at (&richloc, "%<friend%> used outside of class");
13483 cp_lexer_purge_token (parser->lexer);
13485 else
13487 ds = ds_friend;
13488 /* Consume the token. */
13489 cp_lexer_consume_token (parser->lexer);
13491 break;
13493 case RID_CONSTEXPR:
13494 ds = ds_constexpr;
13495 cp_lexer_consume_token (parser->lexer);
13496 break;
13498 case RID_CONCEPT:
13499 ds = ds_concept;
13500 cp_lexer_consume_token (parser->lexer);
13501 break;
13503 /* function-specifier:
13504 inline
13505 virtual
13506 explicit */
13507 case RID_INLINE:
13508 case RID_VIRTUAL:
13509 case RID_EXPLICIT:
13510 cp_parser_function_specifier_opt (parser, decl_specs);
13511 break;
13513 /* decl-specifier:
13514 typedef */
13515 case RID_TYPEDEF:
13516 ds = ds_typedef;
13517 /* Consume the token. */
13518 cp_lexer_consume_token (parser->lexer);
13519 /* A constructor declarator cannot appear in a typedef. */
13520 constructor_possible_p = false;
13521 /* The "typedef" keyword can only occur in a declaration; we
13522 may as well commit at this point. */
13523 cp_parser_commit_to_tentative_parse (parser);
13525 if (decl_specs->storage_class != sc_none)
13526 decl_specs->conflicting_specifiers_p = true;
13527 break;
13529 /* storage-class-specifier:
13530 auto
13531 register
13532 static
13533 extern
13534 mutable
13536 GNU Extension:
13537 thread */
13538 case RID_AUTO:
13539 if (cxx_dialect == cxx98)
13541 /* Consume the token. */
13542 cp_lexer_consume_token (parser->lexer);
13544 /* Complain about `auto' as a storage specifier, if
13545 we're complaining about C++0x compatibility. */
13546 gcc_rich_location richloc (token->location);
13547 richloc.add_fixit_remove ();
13548 warning_at (&richloc, OPT_Wc__11_compat,
13549 "%<auto%> changes meaning in C++11; "
13550 "please remove it");
13552 /* Set the storage class anyway. */
13553 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13554 token);
13556 else
13557 /* C++0x auto type-specifier. */
13558 found_decl_spec = false;
13559 break;
13561 case RID_REGISTER:
13562 case RID_STATIC:
13563 case RID_EXTERN:
13564 case RID_MUTABLE:
13565 /* Consume the token. */
13566 cp_lexer_consume_token (parser->lexer);
13567 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13568 token);
13569 break;
13570 case RID_THREAD:
13571 /* Consume the token. */
13572 ds = ds_thread;
13573 cp_lexer_consume_token (parser->lexer);
13574 break;
13576 default:
13577 /* We did not yet find a decl-specifier yet. */
13578 found_decl_spec = false;
13579 break;
13582 if (found_decl_spec
13583 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13584 && token->keyword != RID_CONSTEXPR)
13585 error ("decl-specifier invalid in condition");
13587 if (found_decl_spec
13588 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13589 && token->keyword != RID_MUTABLE
13590 && token->keyword != RID_CONSTEXPR)
13591 error_at (token->location, "%qD invalid in lambda",
13592 ridpointers[token->keyword]);
13594 if (ds != ds_last)
13595 set_and_check_decl_spec_loc (decl_specs, ds, token);
13597 /* Constructors are a special case. The `S' in `S()' is not a
13598 decl-specifier; it is the beginning of the declarator. */
13599 constructor_p
13600 = (!found_decl_spec
13601 && constructor_possible_p
13602 && (cp_parser_constructor_declarator_p
13603 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13605 /* If we don't have a DECL_SPEC yet, then we must be looking at
13606 a type-specifier. */
13607 if (!found_decl_spec && !constructor_p)
13609 int decl_spec_declares_class_or_enum;
13610 bool is_cv_qualifier;
13611 tree type_spec;
13613 type_spec
13614 = cp_parser_type_specifier (parser, flags,
13615 decl_specs,
13616 /*is_declaration=*/true,
13617 &decl_spec_declares_class_or_enum,
13618 &is_cv_qualifier);
13619 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13621 /* If this type-specifier referenced a user-defined type
13622 (a typedef, class-name, etc.), then we can't allow any
13623 more such type-specifiers henceforth.
13625 [dcl.spec]
13627 The longest sequence of decl-specifiers that could
13628 possibly be a type name is taken as the
13629 decl-specifier-seq of a declaration. The sequence shall
13630 be self-consistent as described below.
13632 [dcl.type]
13634 As a general rule, at most one type-specifier is allowed
13635 in the complete decl-specifier-seq of a declaration. The
13636 only exceptions are the following:
13638 -- const or volatile can be combined with any other
13639 type-specifier.
13641 -- signed or unsigned can be combined with char, long,
13642 short, or int.
13644 -- ..
13646 Example:
13648 typedef char* Pc;
13649 void g (const int Pc);
13651 Here, Pc is *not* part of the decl-specifier seq; it's
13652 the declarator. Therefore, once we see a type-specifier
13653 (other than a cv-qualifier), we forbid any additional
13654 user-defined types. We *do* still allow things like `int
13655 int' to be considered a decl-specifier-seq, and issue the
13656 error message later. */
13657 if (type_spec && !is_cv_qualifier)
13658 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13659 /* A constructor declarator cannot follow a type-specifier. */
13660 if (type_spec)
13662 constructor_possible_p = false;
13663 found_decl_spec = true;
13664 if (!is_cv_qualifier)
13665 decl_specs->any_type_specifiers_p = true;
13669 /* If we still do not have a DECL_SPEC, then there are no more
13670 decl-specifiers. */
13671 if (!found_decl_spec)
13672 break;
13674 decl_specs->any_specifiers_p = true;
13675 /* After we see one decl-specifier, further decl-specifiers are
13676 always optional. */
13677 flags |= CP_PARSER_FLAGS_OPTIONAL;
13680 /* Don't allow a friend specifier with a class definition. */
13681 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13682 && (*declares_class_or_enum & 2))
13683 error_at (decl_specs->locations[ds_friend],
13684 "class definition may not be declared a friend");
13687 /* Parse an (optional) storage-class-specifier.
13689 storage-class-specifier:
13690 auto
13691 register
13692 static
13693 extern
13694 mutable
13696 GNU Extension:
13698 storage-class-specifier:
13699 thread
13701 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13703 static tree
13704 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13706 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13708 case RID_AUTO:
13709 if (cxx_dialect != cxx98)
13710 return NULL_TREE;
13711 /* Fall through for C++98. */
13712 gcc_fallthrough ();
13714 case RID_REGISTER:
13715 case RID_STATIC:
13716 case RID_EXTERN:
13717 case RID_MUTABLE:
13718 case RID_THREAD:
13719 /* Consume the token. */
13720 return cp_lexer_consume_token (parser->lexer)->u.value;
13722 default:
13723 return NULL_TREE;
13727 /* Parse an (optional) function-specifier.
13729 function-specifier:
13730 inline
13731 virtual
13732 explicit
13734 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13735 Updates DECL_SPECS, if it is non-NULL. */
13737 static tree
13738 cp_parser_function_specifier_opt (cp_parser* parser,
13739 cp_decl_specifier_seq *decl_specs)
13741 cp_token *token = cp_lexer_peek_token (parser->lexer);
13742 switch (token->keyword)
13744 case RID_INLINE:
13745 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13746 break;
13748 case RID_VIRTUAL:
13749 /* 14.5.2.3 [temp.mem]
13751 A member function template shall not be virtual. */
13752 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13753 && current_class_type)
13754 error_at (token->location, "templates may not be %<virtual%>");
13755 else
13756 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13757 break;
13759 case RID_EXPLICIT:
13760 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13761 break;
13763 default:
13764 return NULL_TREE;
13767 /* Consume the token. */
13768 return cp_lexer_consume_token (parser->lexer)->u.value;
13771 /* Parse a linkage-specification.
13773 linkage-specification:
13774 extern string-literal { declaration-seq [opt] }
13775 extern string-literal declaration */
13777 static void
13778 cp_parser_linkage_specification (cp_parser* parser)
13780 tree linkage;
13782 /* Look for the `extern' keyword. */
13783 cp_token *extern_token
13784 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13786 /* Look for the string-literal. */
13787 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13788 linkage = cp_parser_string_literal (parser, false, false);
13790 /* Transform the literal into an identifier. If the literal is a
13791 wide-character string, or contains embedded NULs, then we can't
13792 handle it as the user wants. */
13793 if (strlen (TREE_STRING_POINTER (linkage))
13794 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13796 cp_parser_error (parser, "invalid linkage-specification");
13797 /* Assume C++ linkage. */
13798 linkage = lang_name_cplusplus;
13800 else
13801 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13803 /* We're now using the new linkage. */
13804 push_lang_context (linkage);
13806 /* Preserve the location of the the innermost linkage specification,
13807 tracking the locations of nested specifications via a local. */
13808 location_t saved_location
13809 = parser->innermost_linkage_specification_location;
13810 /* Construct a location ranging from the start of the "extern" to
13811 the end of the string-literal, with the caret at the start, e.g.:
13812 extern "C" {
13813 ^~~~~~~~~~
13815 parser->innermost_linkage_specification_location
13816 = make_location (extern_token->location,
13817 extern_token->location,
13818 get_finish (string_token->location));
13820 /* If the next token is a `{', then we're using the first
13821 production. */
13822 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13824 cp_ensure_no_omp_declare_simd (parser);
13825 cp_ensure_no_oacc_routine (parser);
13827 /* Consume the `{' token. */
13828 matching_braces braces;
13829 braces.consume_open (parser)->location;
13830 /* Parse the declarations. */
13831 cp_parser_declaration_seq_opt (parser);
13832 /* Look for the closing `}'. */
13833 braces.require_close (parser);
13835 /* Otherwise, there's just one declaration. */
13836 else
13838 bool saved_in_unbraced_linkage_specification_p;
13840 saved_in_unbraced_linkage_specification_p
13841 = parser->in_unbraced_linkage_specification_p;
13842 parser->in_unbraced_linkage_specification_p = true;
13843 cp_parser_declaration (parser);
13844 parser->in_unbraced_linkage_specification_p
13845 = saved_in_unbraced_linkage_specification_p;
13848 /* We're done with the linkage-specification. */
13849 pop_lang_context ();
13851 /* Restore location of parent linkage specification, if any. */
13852 parser->innermost_linkage_specification_location = saved_location;
13855 /* Parse a static_assert-declaration.
13857 static_assert-declaration:
13858 static_assert ( constant-expression , string-literal ) ;
13859 static_assert ( constant-expression ) ; (C++17)
13861 If MEMBER_P, this static_assert is a class member. */
13863 static void
13864 cp_parser_static_assert(cp_parser *parser, bool member_p)
13866 cp_expr condition;
13867 location_t token_loc;
13868 tree message;
13869 bool dummy;
13871 /* Peek at the `static_assert' token so we can keep track of exactly
13872 where the static assertion started. */
13873 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13875 /* Look for the `static_assert' keyword. */
13876 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13877 RT_STATIC_ASSERT))
13878 return;
13880 /* We know we are in a static assertion; commit to any tentative
13881 parse. */
13882 if (cp_parser_parsing_tentatively (parser))
13883 cp_parser_commit_to_tentative_parse (parser);
13885 /* Parse the `(' starting the static assertion condition. */
13886 matching_parens parens;
13887 parens.require_open (parser);
13889 /* Parse the constant-expression. Allow a non-constant expression
13890 here in order to give better diagnostics in finish_static_assert. */
13891 condition =
13892 cp_parser_constant_expression (parser,
13893 /*allow_non_constant_p=*/true,
13894 /*non_constant_p=*/&dummy);
13896 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13898 if (cxx_dialect < cxx17)
13899 pedwarn (input_location, OPT_Wpedantic,
13900 "static_assert without a message "
13901 "only available with -std=c++17 or -std=gnu++17");
13902 /* Eat the ')' */
13903 cp_lexer_consume_token (parser->lexer);
13904 message = build_string (1, "");
13905 TREE_TYPE (message) = char_array_type_node;
13906 fix_string_type (message);
13908 else
13910 /* Parse the separating `,'. */
13911 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13913 /* Parse the string-literal message. */
13914 message = cp_parser_string_literal (parser,
13915 /*translate=*/false,
13916 /*wide_ok=*/true);
13918 /* A `)' completes the static assertion. */
13919 if (!parens.require_close (parser))
13920 cp_parser_skip_to_closing_parenthesis (parser,
13921 /*recovering=*/true,
13922 /*or_comma=*/false,
13923 /*consume_paren=*/true);
13926 /* A semicolon terminates the declaration. */
13927 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13929 /* Get the location for the static assertion. Use that of the
13930 condition if available, otherwise, use that of the "static_assert"
13931 token. */
13932 location_t assert_loc = condition.get_location ();
13933 if (assert_loc == UNKNOWN_LOCATION)
13934 assert_loc = token_loc;
13936 /* Complete the static assertion, which may mean either processing
13937 the static assert now or saving it for template instantiation. */
13938 finish_static_assert (condition, message, assert_loc, member_p);
13941 /* Parse the expression in decltype ( expression ). */
13943 static tree
13944 cp_parser_decltype_expr (cp_parser *parser,
13945 bool &id_expression_or_member_access_p)
13947 cp_token *id_expr_start_token;
13948 tree expr;
13950 /* Since we're going to preserve any side-effects from this parse, set up a
13951 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13952 in the expression. */
13953 tentative_firewall firewall (parser);
13955 /* First, try parsing an id-expression. */
13956 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13957 cp_parser_parse_tentatively (parser);
13958 expr = cp_parser_id_expression (parser,
13959 /*template_keyword_p=*/false,
13960 /*check_dependency_p=*/true,
13961 /*template_p=*/NULL,
13962 /*declarator_p=*/false,
13963 /*optional_p=*/false);
13965 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13967 bool non_integral_constant_expression_p = false;
13968 tree id_expression = expr;
13969 cp_id_kind idk;
13970 const char *error_msg;
13972 if (identifier_p (expr))
13973 /* Lookup the name we got back from the id-expression. */
13974 expr = cp_parser_lookup_name_simple (parser, expr,
13975 id_expr_start_token->location);
13977 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13978 /* A template without args is not a complete id-expression. */
13979 expr = error_mark_node;
13981 if (expr
13982 && expr != error_mark_node
13983 && TREE_CODE (expr) != TYPE_DECL
13984 && (TREE_CODE (expr) != BIT_NOT_EXPR
13985 || !TYPE_P (TREE_OPERAND (expr, 0)))
13986 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13988 /* Complete lookup of the id-expression. */
13989 expr = (finish_id_expression
13990 (id_expression, expr, parser->scope, &idk,
13991 /*integral_constant_expression_p=*/false,
13992 /*allow_non_integral_constant_expression_p=*/true,
13993 &non_integral_constant_expression_p,
13994 /*template_p=*/false,
13995 /*done=*/true,
13996 /*address_p=*/false,
13997 /*template_arg_p=*/false,
13998 &error_msg,
13999 id_expr_start_token->location));
14001 if (expr == error_mark_node)
14002 /* We found an id-expression, but it was something that we
14003 should not have found. This is an error, not something
14004 we can recover from, so note that we found an
14005 id-expression and we'll recover as gracefully as
14006 possible. */
14007 id_expression_or_member_access_p = true;
14010 if (expr
14011 && expr != error_mark_node
14012 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14013 /* We have an id-expression. */
14014 id_expression_or_member_access_p = true;
14017 if (!id_expression_or_member_access_p)
14019 /* Abort the id-expression parse. */
14020 cp_parser_abort_tentative_parse (parser);
14022 /* Parsing tentatively, again. */
14023 cp_parser_parse_tentatively (parser);
14025 /* Parse a class member access. */
14026 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14027 /*cast_p=*/false, /*decltype*/true,
14028 /*member_access_only_p=*/true, NULL);
14030 if (expr
14031 && expr != error_mark_node
14032 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14033 /* We have an id-expression. */
14034 id_expression_or_member_access_p = true;
14037 if (id_expression_or_member_access_p)
14038 /* We have parsed the complete id-expression or member access. */
14039 cp_parser_parse_definitely (parser);
14040 else
14042 /* Abort our attempt to parse an id-expression or member access
14043 expression. */
14044 cp_parser_abort_tentative_parse (parser);
14046 /* Commit to the tentative_firewall so we get syntax errors. */
14047 cp_parser_commit_to_tentative_parse (parser);
14049 /* Parse a full expression. */
14050 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14051 /*decltype_p=*/true);
14054 return expr;
14057 /* Parse a `decltype' type. Returns the type.
14059 simple-type-specifier:
14060 decltype ( expression )
14061 C++14 proposal:
14062 decltype ( auto ) */
14064 static tree
14065 cp_parser_decltype (cp_parser *parser)
14067 bool id_expression_or_member_access_p = false;
14068 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14070 if (start_token->type == CPP_DECLTYPE)
14072 /* Already parsed. */
14073 cp_lexer_consume_token (parser->lexer);
14074 return saved_checks_value (start_token->u.tree_check_value);
14077 /* Look for the `decltype' token. */
14078 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14079 return error_mark_node;
14081 /* Parse the opening `('. */
14082 matching_parens parens;
14083 if (!parens.require_open (parser))
14084 return error_mark_node;
14086 push_deferring_access_checks (dk_deferred);
14088 tree expr = NULL_TREE;
14090 if (cxx_dialect >= cxx14
14091 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14092 /* decltype (auto) */
14093 cp_lexer_consume_token (parser->lexer);
14094 else
14096 /* decltype (expression) */
14098 /* Types cannot be defined in a `decltype' expression. Save away the
14099 old message and set the new one. */
14100 const char *saved_message = parser->type_definition_forbidden_message;
14101 parser->type_definition_forbidden_message
14102 = G_("types may not be defined in %<decltype%> expressions");
14104 /* The restrictions on constant-expressions do not apply inside
14105 decltype expressions. */
14106 bool saved_integral_constant_expression_p
14107 = parser->integral_constant_expression_p;
14108 bool saved_non_integral_constant_expression_p
14109 = parser->non_integral_constant_expression_p;
14110 parser->integral_constant_expression_p = false;
14112 /* Within a parenthesized expression, a `>' token is always
14113 the greater-than operator. */
14114 bool saved_greater_than_is_operator_p
14115 = parser->greater_than_is_operator_p;
14116 parser->greater_than_is_operator_p = true;
14118 /* Do not actually evaluate the expression. */
14119 ++cp_unevaluated_operand;
14121 /* Do not warn about problems with the expression. */
14122 ++c_inhibit_evaluation_warnings;
14124 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14126 /* Go back to evaluating expressions. */
14127 --cp_unevaluated_operand;
14128 --c_inhibit_evaluation_warnings;
14130 /* The `>' token might be the end of a template-id or
14131 template-parameter-list now. */
14132 parser->greater_than_is_operator_p
14133 = saved_greater_than_is_operator_p;
14135 /* Restore the old message and the integral constant expression
14136 flags. */
14137 parser->type_definition_forbidden_message = saved_message;
14138 parser->integral_constant_expression_p
14139 = saved_integral_constant_expression_p;
14140 parser->non_integral_constant_expression_p
14141 = saved_non_integral_constant_expression_p;
14144 /* Parse to the closing `)'. */
14145 if (!parens.require_close (parser))
14147 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14148 /*consume_paren=*/true);
14149 pop_deferring_access_checks ();
14150 return error_mark_node;
14153 if (!expr)
14155 /* Build auto. */
14156 expr = make_decltype_auto ();
14157 AUTO_IS_DECLTYPE (expr) = true;
14159 else
14160 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14161 tf_warning_or_error);
14163 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14164 it again. */
14165 start_token->type = CPP_DECLTYPE;
14166 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14167 start_token->u.tree_check_value->value = expr;
14168 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14169 start_token->keyword = RID_MAX;
14170 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14172 pop_to_parent_deferring_access_checks ();
14174 return expr;
14177 /* Special member functions [gram.special] */
14179 /* Parse a conversion-function-id.
14181 conversion-function-id:
14182 operator conversion-type-id
14184 Returns an IDENTIFIER_NODE representing the operator. */
14186 static tree
14187 cp_parser_conversion_function_id (cp_parser* parser)
14189 tree type;
14190 tree saved_scope;
14191 tree saved_qualifying_scope;
14192 tree saved_object_scope;
14193 tree pushed_scope = NULL_TREE;
14195 /* Look for the `operator' token. */
14196 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14197 return error_mark_node;
14198 /* When we parse the conversion-type-id, the current scope will be
14199 reset. However, we need that information in able to look up the
14200 conversion function later, so we save it here. */
14201 saved_scope = parser->scope;
14202 saved_qualifying_scope = parser->qualifying_scope;
14203 saved_object_scope = parser->object_scope;
14204 /* We must enter the scope of the class so that the names of
14205 entities declared within the class are available in the
14206 conversion-type-id. For example, consider:
14208 struct S {
14209 typedef int I;
14210 operator I();
14213 S::operator I() { ... }
14215 In order to see that `I' is a type-name in the definition, we
14216 must be in the scope of `S'. */
14217 if (saved_scope)
14218 pushed_scope = push_scope (saved_scope);
14219 /* Parse the conversion-type-id. */
14220 type = cp_parser_conversion_type_id (parser);
14221 /* Leave the scope of the class, if any. */
14222 if (pushed_scope)
14223 pop_scope (pushed_scope);
14224 /* Restore the saved scope. */
14225 parser->scope = saved_scope;
14226 parser->qualifying_scope = saved_qualifying_scope;
14227 parser->object_scope = saved_object_scope;
14228 /* If the TYPE is invalid, indicate failure. */
14229 if (type == error_mark_node)
14230 return error_mark_node;
14231 return make_conv_op_name (type);
14234 /* Parse a conversion-type-id:
14236 conversion-type-id:
14237 type-specifier-seq conversion-declarator [opt]
14239 Returns the TYPE specified. */
14241 static tree
14242 cp_parser_conversion_type_id (cp_parser* parser)
14244 tree attributes;
14245 cp_decl_specifier_seq type_specifiers;
14246 cp_declarator *declarator;
14247 tree type_specified;
14248 const char *saved_message;
14250 /* Parse the attributes. */
14251 attributes = cp_parser_attributes_opt (parser);
14253 saved_message = parser->type_definition_forbidden_message;
14254 parser->type_definition_forbidden_message
14255 = G_("types may not be defined in a conversion-type-id");
14257 /* Parse the type-specifiers. */
14258 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14259 /*is_trailing_return=*/false,
14260 &type_specifiers);
14262 parser->type_definition_forbidden_message = saved_message;
14264 /* If that didn't work, stop. */
14265 if (type_specifiers.type == error_mark_node)
14266 return error_mark_node;
14267 /* Parse the conversion-declarator. */
14268 declarator = cp_parser_conversion_declarator_opt (parser);
14270 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14271 /*initialized=*/0, &attributes);
14272 if (attributes)
14273 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14275 /* Don't give this error when parsing tentatively. This happens to
14276 work because we always parse this definitively once. */
14277 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14278 && type_uses_auto (type_specified))
14280 if (cxx_dialect < cxx14)
14282 error ("invalid use of %<auto%> in conversion operator");
14283 return error_mark_node;
14285 else if (template_parm_scope_p ())
14286 warning (0, "use of %<auto%> in member template "
14287 "conversion operator can never be deduced");
14290 return type_specified;
14293 /* Parse an (optional) conversion-declarator.
14295 conversion-declarator:
14296 ptr-operator conversion-declarator [opt]
14300 static cp_declarator *
14301 cp_parser_conversion_declarator_opt (cp_parser* parser)
14303 enum tree_code code;
14304 tree class_type, std_attributes = NULL_TREE;
14305 cp_cv_quals cv_quals;
14307 /* We don't know if there's a ptr-operator next, or not. */
14308 cp_parser_parse_tentatively (parser);
14309 /* Try the ptr-operator. */
14310 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14311 &std_attributes);
14312 /* If it worked, look for more conversion-declarators. */
14313 if (cp_parser_parse_definitely (parser))
14315 cp_declarator *declarator;
14317 /* Parse another optional declarator. */
14318 declarator = cp_parser_conversion_declarator_opt (parser);
14320 declarator = cp_parser_make_indirect_declarator
14321 (code, class_type, cv_quals, declarator, std_attributes);
14323 return declarator;
14326 return NULL;
14329 /* Parse an (optional) ctor-initializer.
14331 ctor-initializer:
14332 : mem-initializer-list */
14334 static void
14335 cp_parser_ctor_initializer_opt (cp_parser* parser)
14337 /* If the next token is not a `:', then there is no
14338 ctor-initializer. */
14339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14341 /* Do default initialization of any bases and members. */
14342 if (DECL_CONSTRUCTOR_P (current_function_decl))
14343 finish_mem_initializers (NULL_TREE);
14344 return;
14347 /* Consume the `:' token. */
14348 cp_lexer_consume_token (parser->lexer);
14349 /* And the mem-initializer-list. */
14350 cp_parser_mem_initializer_list (parser);
14353 /* Parse a mem-initializer-list.
14355 mem-initializer-list:
14356 mem-initializer ... [opt]
14357 mem-initializer ... [opt] , mem-initializer-list */
14359 static void
14360 cp_parser_mem_initializer_list (cp_parser* parser)
14362 tree mem_initializer_list = NULL_TREE;
14363 tree target_ctor = error_mark_node;
14364 cp_token *token = cp_lexer_peek_token (parser->lexer);
14366 /* Let the semantic analysis code know that we are starting the
14367 mem-initializer-list. */
14368 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14369 error_at (token->location,
14370 "only constructors take member initializers");
14372 /* Loop through the list. */
14373 while (true)
14375 tree mem_initializer;
14377 token = cp_lexer_peek_token (parser->lexer);
14378 /* Parse the mem-initializer. */
14379 mem_initializer = cp_parser_mem_initializer (parser);
14380 /* If the next token is a `...', we're expanding member initializers. */
14381 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14382 if (ellipsis
14383 || (mem_initializer != error_mark_node
14384 && check_for_bare_parameter_packs (TREE_PURPOSE
14385 (mem_initializer))))
14387 /* Consume the `...'. */
14388 if (ellipsis)
14389 cp_lexer_consume_token (parser->lexer);
14391 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14392 can be expanded but members cannot. */
14393 if (mem_initializer != error_mark_node
14394 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14396 error_at (token->location,
14397 "cannot expand initializer for member %qD",
14398 TREE_PURPOSE (mem_initializer));
14399 mem_initializer = error_mark_node;
14402 /* Construct the pack expansion type. */
14403 if (mem_initializer != error_mark_node)
14404 mem_initializer = make_pack_expansion (mem_initializer);
14406 if (target_ctor != error_mark_node
14407 && mem_initializer != error_mark_node)
14409 error ("mem-initializer for %qD follows constructor delegation",
14410 TREE_PURPOSE (mem_initializer));
14411 mem_initializer = error_mark_node;
14413 /* Look for a target constructor. */
14414 if (mem_initializer != error_mark_node
14415 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14416 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14418 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14419 if (mem_initializer_list)
14421 error ("constructor delegation follows mem-initializer for %qD",
14422 TREE_PURPOSE (mem_initializer_list));
14423 mem_initializer = error_mark_node;
14425 target_ctor = mem_initializer;
14427 /* Add it to the list, unless it was erroneous. */
14428 if (mem_initializer != error_mark_node)
14430 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14431 mem_initializer_list = mem_initializer;
14433 /* If the next token is not a `,', we're done. */
14434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14435 break;
14436 /* Consume the `,' token. */
14437 cp_lexer_consume_token (parser->lexer);
14440 /* Perform semantic analysis. */
14441 if (DECL_CONSTRUCTOR_P (current_function_decl))
14442 finish_mem_initializers (mem_initializer_list);
14445 /* Parse a mem-initializer.
14447 mem-initializer:
14448 mem-initializer-id ( expression-list [opt] )
14449 mem-initializer-id braced-init-list
14451 GNU extension:
14453 mem-initializer:
14454 ( expression-list [opt] )
14456 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14457 class) or FIELD_DECL (for a non-static data member) to initialize;
14458 the TREE_VALUE is the expression-list. An empty initialization
14459 list is represented by void_list_node. */
14461 static tree
14462 cp_parser_mem_initializer (cp_parser* parser)
14464 tree mem_initializer_id;
14465 tree expression_list;
14466 tree member;
14467 cp_token *token = cp_lexer_peek_token (parser->lexer);
14469 /* Find out what is being initialized. */
14470 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14472 permerror (token->location,
14473 "anachronistic old-style base class initializer");
14474 mem_initializer_id = NULL_TREE;
14476 else
14478 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14479 if (mem_initializer_id == error_mark_node)
14480 return mem_initializer_id;
14482 member = expand_member_init (mem_initializer_id);
14483 if (member && !DECL_P (member))
14484 in_base_initializer = 1;
14486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14488 bool expr_non_constant_p;
14489 cp_lexer_set_source_position (parser->lexer);
14490 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14491 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14492 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14493 expression_list = build_tree_list (NULL_TREE, expression_list);
14495 else
14497 vec<tree, va_gc> *vec;
14498 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14499 /*cast_p=*/false,
14500 /*allow_expansion_p=*/true,
14501 /*non_constant_p=*/NULL);
14502 if (vec == NULL)
14503 return error_mark_node;
14504 expression_list = build_tree_list_vec (vec);
14505 release_tree_vector (vec);
14508 if (expression_list == error_mark_node)
14509 return error_mark_node;
14510 if (!expression_list)
14511 expression_list = void_type_node;
14513 in_base_initializer = 0;
14515 return member ? build_tree_list (member, expression_list) : error_mark_node;
14518 /* Parse a mem-initializer-id.
14520 mem-initializer-id:
14521 :: [opt] nested-name-specifier [opt] class-name
14522 decltype-specifier (C++11)
14523 identifier
14525 Returns a TYPE indicating the class to be initialized for the first
14526 production (and the second in C++11). Returns an IDENTIFIER_NODE
14527 indicating the data member to be initialized for the last production. */
14529 static tree
14530 cp_parser_mem_initializer_id (cp_parser* parser)
14532 bool global_scope_p;
14533 bool nested_name_specifier_p;
14534 bool template_p = false;
14535 tree id;
14537 cp_token *token = cp_lexer_peek_token (parser->lexer);
14539 /* `typename' is not allowed in this context ([temp.res]). */
14540 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14542 error_at (token->location,
14543 "keyword %<typename%> not allowed in this context (a qualified "
14544 "member initializer is implicitly a type)");
14545 cp_lexer_consume_token (parser->lexer);
14547 /* Look for the optional `::' operator. */
14548 global_scope_p
14549 = (cp_parser_global_scope_opt (parser,
14550 /*current_scope_valid_p=*/false)
14551 != NULL_TREE);
14552 /* Look for the optional nested-name-specifier. The simplest way to
14553 implement:
14555 [temp.res]
14557 The keyword `typename' is not permitted in a base-specifier or
14558 mem-initializer; in these contexts a qualified name that
14559 depends on a template-parameter is implicitly assumed to be a
14560 type name.
14562 is to assume that we have seen the `typename' keyword at this
14563 point. */
14564 nested_name_specifier_p
14565 = (cp_parser_nested_name_specifier_opt (parser,
14566 /*typename_keyword_p=*/true,
14567 /*check_dependency_p=*/true,
14568 /*type_p=*/true,
14569 /*is_declaration=*/true)
14570 != NULL_TREE);
14571 if (nested_name_specifier_p)
14572 template_p = cp_parser_optional_template_keyword (parser);
14573 /* If there is a `::' operator or a nested-name-specifier, then we
14574 are definitely looking for a class-name. */
14575 if (global_scope_p || nested_name_specifier_p)
14576 return cp_parser_class_name (parser,
14577 /*typename_keyword_p=*/true,
14578 /*template_keyword_p=*/template_p,
14579 typename_type,
14580 /*check_dependency_p=*/true,
14581 /*class_head_p=*/false,
14582 /*is_declaration=*/true);
14583 /* Otherwise, we could also be looking for an ordinary identifier. */
14584 cp_parser_parse_tentatively (parser);
14585 if (cp_lexer_next_token_is_decltype (parser->lexer))
14586 /* Try a decltype-specifier. */
14587 id = cp_parser_decltype (parser);
14588 else
14589 /* Otherwise, try a class-name. */
14590 id = cp_parser_class_name (parser,
14591 /*typename_keyword_p=*/true,
14592 /*template_keyword_p=*/false,
14593 none_type,
14594 /*check_dependency_p=*/true,
14595 /*class_head_p=*/false,
14596 /*is_declaration=*/true);
14597 /* If we found one, we're done. */
14598 if (cp_parser_parse_definitely (parser))
14599 return id;
14600 /* Otherwise, look for an ordinary identifier. */
14601 return cp_parser_identifier (parser);
14604 /* Overloading [gram.over] */
14606 /* Parse an operator-function-id.
14608 operator-function-id:
14609 operator operator
14611 Returns an IDENTIFIER_NODE for the operator which is a
14612 human-readable spelling of the identifier, e.g., `operator +'. */
14614 static cp_expr
14615 cp_parser_operator_function_id (cp_parser* parser)
14617 /* Look for the `operator' keyword. */
14618 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14619 return error_mark_node;
14620 /* And then the name of the operator itself. */
14621 return cp_parser_operator (parser);
14624 /* Return an identifier node for a user-defined literal operator.
14625 The suffix identifier is chained to the operator name identifier. */
14627 tree
14628 cp_literal_operator_id (const char* name)
14630 tree identifier;
14631 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14632 + strlen (name) + 10);
14633 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14634 identifier = get_identifier (buffer);
14636 return identifier;
14639 /* Parse an operator.
14641 operator:
14642 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14643 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14644 || ++ -- , ->* -> () []
14646 GNU Extensions:
14648 operator:
14649 <? >? <?= >?=
14651 Returns an IDENTIFIER_NODE for the operator which is a
14652 human-readable spelling of the identifier, e.g., `operator +'. */
14654 static cp_expr
14655 cp_parser_operator (cp_parser* parser)
14657 tree id = NULL_TREE;
14658 cp_token *token;
14659 bool utf8 = false;
14661 /* Peek at the next token. */
14662 token = cp_lexer_peek_token (parser->lexer);
14664 location_t start_loc = token->location;
14666 /* Figure out which operator we have. */
14667 enum tree_code op = ERROR_MARK;
14668 bool assop = false;
14669 bool consumed = false;
14670 switch (token->type)
14672 case CPP_KEYWORD:
14674 /* The keyword should be either `new' or `delete'. */
14675 if (token->keyword == RID_NEW)
14676 op = NEW_EXPR;
14677 else if (token->keyword == RID_DELETE)
14678 op = DELETE_EXPR;
14679 else
14680 break;
14682 /* Consume the `new' or `delete' token. */
14683 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14685 /* Peek at the next token. */
14686 token = cp_lexer_peek_token (parser->lexer);
14687 /* If it's a `[' token then this is the array variant of the
14688 operator. */
14689 if (token->type == CPP_OPEN_SQUARE)
14691 /* Consume the `[' token. */
14692 cp_lexer_consume_token (parser->lexer);
14693 /* Look for the `]' token. */
14694 if (cp_token *close_token
14695 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14696 end_loc = close_token->location;
14697 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14699 start_loc = make_location (start_loc, start_loc, end_loc);
14700 consumed = true;
14701 break;
14704 case CPP_PLUS:
14705 op = PLUS_EXPR;
14706 break;
14708 case CPP_MINUS:
14709 op = MINUS_EXPR;
14710 break;
14712 case CPP_MULT:
14713 op = MULT_EXPR;
14714 break;
14716 case CPP_DIV:
14717 op = TRUNC_DIV_EXPR;
14718 break;
14720 case CPP_MOD:
14721 op = TRUNC_MOD_EXPR;
14722 break;
14724 case CPP_XOR:
14725 op = BIT_XOR_EXPR;
14726 break;
14728 case CPP_AND:
14729 op = BIT_AND_EXPR;
14730 break;
14732 case CPP_OR:
14733 op = BIT_IOR_EXPR;
14734 break;
14736 case CPP_COMPL:
14737 op = BIT_NOT_EXPR;
14738 break;
14740 case CPP_NOT:
14741 op = TRUTH_NOT_EXPR;
14742 break;
14744 case CPP_EQ:
14745 assop = true;
14746 op = NOP_EXPR;
14747 break;
14749 case CPP_LESS:
14750 op = LT_EXPR;
14751 break;
14753 case CPP_GREATER:
14754 op = GT_EXPR;
14755 break;
14757 case CPP_PLUS_EQ:
14758 assop = true;
14759 op = PLUS_EXPR;
14760 break;
14762 case CPP_MINUS_EQ:
14763 assop = true;
14764 op = MINUS_EXPR;
14765 break;
14767 case CPP_MULT_EQ:
14768 assop = true;
14769 op = MULT_EXPR;
14770 break;
14772 case CPP_DIV_EQ:
14773 assop = true;
14774 op = TRUNC_DIV_EXPR;
14775 break;
14777 case CPP_MOD_EQ:
14778 assop = true;
14779 op = TRUNC_MOD_EXPR;
14780 break;
14782 case CPP_XOR_EQ:
14783 assop = true;
14784 op = BIT_XOR_EXPR;
14785 break;
14787 case CPP_AND_EQ:
14788 assop = true;
14789 op = BIT_AND_EXPR;
14790 break;
14792 case CPP_OR_EQ:
14793 assop = true;
14794 op = BIT_IOR_EXPR;
14795 break;
14797 case CPP_LSHIFT:
14798 op = LSHIFT_EXPR;
14799 break;
14801 case CPP_RSHIFT:
14802 op = RSHIFT_EXPR;
14803 break;
14805 case CPP_LSHIFT_EQ:
14806 assop = true;
14807 op = LSHIFT_EXPR;
14808 break;
14810 case CPP_RSHIFT_EQ:
14811 assop = true;
14812 op = RSHIFT_EXPR;
14813 break;
14815 case CPP_EQ_EQ:
14816 op = EQ_EXPR;
14817 break;
14819 case CPP_NOT_EQ:
14820 op = NE_EXPR;
14821 break;
14823 case CPP_LESS_EQ:
14824 op = LE_EXPR;
14825 break;
14827 case CPP_GREATER_EQ:
14828 op = GE_EXPR;
14829 break;
14831 case CPP_AND_AND:
14832 op = TRUTH_ANDIF_EXPR;
14833 break;
14835 case CPP_OR_OR:
14836 op = TRUTH_ORIF_EXPR;
14837 break;
14839 case CPP_PLUS_PLUS:
14840 op = POSTINCREMENT_EXPR;
14841 break;
14843 case CPP_MINUS_MINUS:
14844 op = PREDECREMENT_EXPR;
14845 break;
14847 case CPP_COMMA:
14848 op = COMPOUND_EXPR;
14849 break;
14851 case CPP_DEREF_STAR:
14852 op = MEMBER_REF;
14853 break;
14855 case CPP_DEREF:
14856 op = COMPONENT_REF;
14857 break;
14859 case CPP_OPEN_PAREN:
14861 /* Consume the `('. */
14862 matching_parens parens;
14863 parens.consume_open (parser);
14864 /* Look for the matching `)'. */
14865 parens.require_close (parser);
14866 op = CALL_EXPR;
14867 consumed = true;
14868 break;
14871 case CPP_OPEN_SQUARE:
14872 /* Consume the `['. */
14873 cp_lexer_consume_token (parser->lexer);
14874 /* Look for the matching `]'. */
14875 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14876 op = ARRAY_REF;
14877 consumed = true;
14878 break;
14880 case CPP_UTF8STRING:
14881 case CPP_UTF8STRING_USERDEF:
14882 utf8 = true;
14883 /* FALLTHRU */
14884 case CPP_STRING:
14885 case CPP_WSTRING:
14886 case CPP_STRING16:
14887 case CPP_STRING32:
14888 case CPP_STRING_USERDEF:
14889 case CPP_WSTRING_USERDEF:
14890 case CPP_STRING16_USERDEF:
14891 case CPP_STRING32_USERDEF:
14893 tree str, string_tree;
14894 int sz, len;
14896 if (cxx_dialect == cxx98)
14897 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14899 /* Consume the string. */
14900 str = cp_parser_string_literal (parser, /*translate=*/true,
14901 /*wide_ok=*/true, /*lookup_udlit=*/false);
14902 if (str == error_mark_node)
14903 return error_mark_node;
14904 else if (TREE_CODE (str) == USERDEF_LITERAL)
14906 string_tree = USERDEF_LITERAL_VALUE (str);
14907 id = USERDEF_LITERAL_SUFFIX_ID (str);
14909 else
14911 string_tree = str;
14912 /* Look for the suffix identifier. */
14913 token = cp_lexer_peek_token (parser->lexer);
14914 if (token->type == CPP_NAME)
14915 id = cp_parser_identifier (parser);
14916 else if (token->type == CPP_KEYWORD)
14918 error ("unexpected keyword;"
14919 " remove space between quotes and suffix identifier");
14920 return error_mark_node;
14922 else
14924 error ("expected suffix identifier");
14925 return error_mark_node;
14928 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14929 (TREE_TYPE (TREE_TYPE (string_tree))));
14930 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14931 if (len != 0)
14933 error ("expected empty string after %<operator%> keyword");
14934 return error_mark_node;
14936 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14937 != char_type_node)
14939 error ("invalid encoding prefix in literal operator");
14940 return error_mark_node;
14942 if (id != error_mark_node)
14944 const char *name = IDENTIFIER_POINTER (id);
14945 id = cp_literal_operator_id (name);
14947 return id;
14950 default:
14951 /* Anything else is an error. */
14952 break;
14955 /* If we have selected an identifier, we need to consume the
14956 operator token. */
14957 if (op != ERROR_MARK)
14959 id = ovl_op_identifier (assop, op);
14960 if (!consumed)
14961 cp_lexer_consume_token (parser->lexer);
14963 /* Otherwise, no valid operator name was present. */
14964 else
14966 cp_parser_error (parser, "expected operator");
14967 id = error_mark_node;
14970 return cp_expr (id, start_loc);
14973 /* Parse a template-declaration.
14975 template-declaration:
14976 export [opt] template < template-parameter-list > declaration
14978 If MEMBER_P is TRUE, this template-declaration occurs within a
14979 class-specifier.
14981 The grammar rule given by the standard isn't correct. What
14982 is really meant is:
14984 template-declaration:
14985 export [opt] template-parameter-list-seq
14986 decl-specifier-seq [opt] init-declarator [opt] ;
14987 export [opt] template-parameter-list-seq
14988 function-definition
14990 template-parameter-list-seq:
14991 template-parameter-list-seq [opt]
14992 template < template-parameter-list >
14994 Concept Extensions:
14996 template-parameter-list-seq:
14997 template < template-parameter-list > requires-clause [opt]
14999 requires-clause:
15000 requires logical-or-expression */
15002 static void
15003 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15005 /* Check for `export'. */
15006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15008 /* Consume the `export' token. */
15009 cp_lexer_consume_token (parser->lexer);
15010 /* Warn that we do not support `export'. */
15011 warning (0, "keyword %<export%> not implemented, and will be ignored");
15014 cp_parser_template_declaration_after_export (parser, member_p);
15017 /* Parse a template-parameter-list.
15019 template-parameter-list:
15020 template-parameter
15021 template-parameter-list , template-parameter
15023 Returns a TREE_LIST. Each node represents a template parameter.
15024 The nodes are connected via their TREE_CHAINs. */
15026 static tree
15027 cp_parser_template_parameter_list (cp_parser* parser)
15029 tree parameter_list = NULL_TREE;
15031 begin_template_parm_list ();
15033 /* The loop below parses the template parms. We first need to know
15034 the total number of template parms to be able to compute proper
15035 canonical types of each dependent type. So after the loop, when
15036 we know the total number of template parms,
15037 end_template_parm_list computes the proper canonical types and
15038 fixes up the dependent types accordingly. */
15039 while (true)
15041 tree parameter;
15042 bool is_non_type;
15043 bool is_parameter_pack;
15044 location_t parm_loc;
15046 /* Parse the template-parameter. */
15047 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15048 parameter = cp_parser_template_parameter (parser,
15049 &is_non_type,
15050 &is_parameter_pack);
15051 /* Add it to the list. */
15052 if (parameter != error_mark_node)
15053 parameter_list = process_template_parm (parameter_list,
15054 parm_loc,
15055 parameter,
15056 is_non_type,
15057 is_parameter_pack);
15058 else
15060 tree err_parm = build_tree_list (parameter, parameter);
15061 parameter_list = chainon (parameter_list, err_parm);
15064 /* If the next token is not a `,', we're done. */
15065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15066 break;
15067 /* Otherwise, consume the `,' token. */
15068 cp_lexer_consume_token (parser->lexer);
15071 return end_template_parm_list (parameter_list);
15074 /* Parse a introduction-list.
15076 introduction-list:
15077 introduced-parameter
15078 introduction-list , introduced-parameter
15080 introduced-parameter:
15081 ...[opt] identifier
15083 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15084 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15085 WILDCARD_DECL will also have DECL_NAME set and token location in
15086 DECL_SOURCE_LOCATION. */
15088 static tree
15089 cp_parser_introduction_list (cp_parser *parser)
15091 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15093 while (true)
15095 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15096 if (is_pack)
15097 cp_lexer_consume_token (parser->lexer);
15099 /* Build placeholder. */
15100 tree parm = build_nt (WILDCARD_DECL);
15101 DECL_SOURCE_LOCATION (parm)
15102 = cp_lexer_peek_token (parser->lexer)->location;
15103 DECL_NAME (parm) = cp_parser_identifier (parser);
15104 WILDCARD_PACK_P (parm) = is_pack;
15105 vec_safe_push (introduction_vec, parm);
15107 /* If the next token is not a `,', we're done. */
15108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15109 break;
15110 /* Otherwise, consume the `,' token. */
15111 cp_lexer_consume_token (parser->lexer);
15114 /* Convert the vec into a TREE_VEC. */
15115 tree introduction_list = make_tree_vec (introduction_vec->length ());
15116 unsigned int n;
15117 tree parm;
15118 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15119 TREE_VEC_ELT (introduction_list, n) = parm;
15121 release_tree_vector (introduction_vec);
15122 return introduction_list;
15125 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15126 is an abstract declarator. */
15128 static inline cp_declarator*
15129 get_id_declarator (cp_declarator *declarator)
15131 cp_declarator *d = declarator;
15132 while (d && d->kind != cdk_id)
15133 d = d->declarator;
15134 return d;
15137 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15138 is an abstract declarator. */
15140 static inline tree
15141 get_unqualified_id (cp_declarator *declarator)
15143 declarator = get_id_declarator (declarator);
15144 if (declarator)
15145 return declarator->u.id.unqualified_name;
15146 else
15147 return NULL_TREE;
15150 /* Returns true if DECL represents a constrained-parameter. */
15152 static inline bool
15153 is_constrained_parameter (tree decl)
15155 return (decl
15156 && TREE_CODE (decl) == TYPE_DECL
15157 && CONSTRAINED_PARM_CONCEPT (decl)
15158 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15161 /* Returns true if PARM declares a constrained-parameter. */
15163 static inline bool
15164 is_constrained_parameter (cp_parameter_declarator *parm)
15166 return is_constrained_parameter (parm->decl_specifiers.type);
15169 /* Check that the type parameter is only a declarator-id, and that its
15170 type is not cv-qualified. */
15172 bool
15173 cp_parser_check_constrained_type_parm (cp_parser *parser,
15174 cp_parameter_declarator *parm)
15176 if (!parm->declarator)
15177 return true;
15179 if (parm->declarator->kind != cdk_id)
15181 cp_parser_error (parser, "invalid constrained type parameter");
15182 return false;
15185 /* Don't allow cv-qualified type parameters. */
15186 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15187 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15189 cp_parser_error (parser, "cv-qualified type parameter");
15190 return false;
15193 return true;
15196 /* Finish parsing/processing a template type parameter and checking
15197 various restrictions. */
15199 static inline tree
15200 cp_parser_constrained_type_template_parm (cp_parser *parser,
15201 tree id,
15202 cp_parameter_declarator* parmdecl)
15204 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15205 return finish_template_type_parm (class_type_node, id);
15206 else
15207 return error_mark_node;
15210 static tree
15211 finish_constrained_template_template_parm (tree proto, tree id)
15213 /* FIXME: This should probably be copied, and we may need to adjust
15214 the template parameter depths. */
15215 tree saved_parms = current_template_parms;
15216 begin_template_parm_list ();
15217 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15218 end_template_parm_list ();
15220 tree parm = finish_template_template_parm (class_type_node, id);
15221 current_template_parms = saved_parms;
15223 return parm;
15226 /* Finish parsing/processing a template template parameter by borrowing
15227 the template parameter list from the prototype parameter. */
15229 static tree
15230 cp_parser_constrained_template_template_parm (cp_parser *parser,
15231 tree proto,
15232 tree id,
15233 cp_parameter_declarator *parmdecl)
15235 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15236 return error_mark_node;
15237 return finish_constrained_template_template_parm (proto, id);
15240 /* Create a new non-type template parameter from the given PARM
15241 declarator. */
15243 static tree
15244 constrained_non_type_template_parm (bool *is_non_type,
15245 cp_parameter_declarator *parm)
15247 *is_non_type = true;
15248 cp_declarator *decl = parm->declarator;
15249 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15250 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15251 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15254 /* Build a constrained template parameter based on the PARMDECL
15255 declarator. The type of PARMDECL is the constrained type, which
15256 refers to the prototype template parameter that ultimately
15257 specifies the type of the declared parameter. */
15259 static tree
15260 finish_constrained_parameter (cp_parser *parser,
15261 cp_parameter_declarator *parmdecl,
15262 bool *is_non_type,
15263 bool *is_parameter_pack)
15265 tree decl = parmdecl->decl_specifiers.type;
15266 tree id = get_unqualified_id (parmdecl->declarator);
15267 tree def = parmdecl->default_argument;
15268 tree proto = DECL_INITIAL (decl);
15270 /* A template parameter constrained by a variadic concept shall also
15271 be declared as a template parameter pack. */
15272 bool is_variadic = template_parameter_pack_p (proto);
15273 if (is_variadic && !*is_parameter_pack)
15274 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15276 /* Build the parameter. Return an error if the declarator was invalid. */
15277 tree parm;
15278 if (TREE_CODE (proto) == TYPE_DECL)
15279 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15280 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15281 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15282 parmdecl);
15283 else
15284 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15285 if (parm == error_mark_node)
15286 return error_mark_node;
15288 /* Finish the parameter decl and create a node attaching the
15289 default argument and constraint. */
15290 parm = build_tree_list (def, parm);
15291 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15293 return parm;
15296 /* Returns true if the parsed type actually represents the declaration
15297 of a type template-parameter. */
15299 static inline bool
15300 declares_constrained_type_template_parameter (tree type)
15302 return (is_constrained_parameter (type)
15303 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15307 /* Returns true if the parsed type actually represents the declaration of
15308 a template template-parameter. */
15310 static bool
15311 declares_constrained_template_template_parameter (tree type)
15313 return (is_constrained_parameter (type)
15314 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15317 /* Parse a default argument for a type template-parameter.
15318 Note that diagnostics are handled in cp_parser_template_parameter. */
15320 static tree
15321 cp_parser_default_type_template_argument (cp_parser *parser)
15323 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15325 /* Consume the `=' token. */
15326 cp_lexer_consume_token (parser->lexer);
15328 cp_token *token = cp_lexer_peek_token (parser->lexer);
15330 /* Parse the default-argument. */
15331 push_deferring_access_checks (dk_no_deferred);
15332 tree default_argument = cp_parser_type_id (parser);
15333 pop_deferring_access_checks ();
15335 if (flag_concepts && type_uses_auto (default_argument))
15337 error_at (token->location,
15338 "invalid use of %<auto%> in default template argument");
15339 return error_mark_node;
15342 return default_argument;
15345 /* Parse a default argument for a template template-parameter. */
15347 static tree
15348 cp_parser_default_template_template_argument (cp_parser *parser)
15350 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15352 bool is_template;
15354 /* Consume the `='. */
15355 cp_lexer_consume_token (parser->lexer);
15356 /* Parse the id-expression. */
15357 push_deferring_access_checks (dk_no_deferred);
15358 /* save token before parsing the id-expression, for error
15359 reporting */
15360 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15361 tree default_argument
15362 = cp_parser_id_expression (parser,
15363 /*template_keyword_p=*/false,
15364 /*check_dependency_p=*/true,
15365 /*template_p=*/&is_template,
15366 /*declarator_p=*/false,
15367 /*optional_p=*/false);
15368 if (TREE_CODE (default_argument) == TYPE_DECL)
15369 /* If the id-expression was a template-id that refers to
15370 a template-class, we already have the declaration here,
15371 so no further lookup is needed. */
15373 else
15374 /* Look up the name. */
15375 default_argument
15376 = cp_parser_lookup_name (parser, default_argument,
15377 none_type,
15378 /*is_template=*/is_template,
15379 /*is_namespace=*/false,
15380 /*check_dependency=*/true,
15381 /*ambiguous_decls=*/NULL,
15382 token->location);
15383 /* See if the default argument is valid. */
15384 default_argument = check_template_template_default_arg (default_argument);
15385 pop_deferring_access_checks ();
15386 return default_argument;
15389 /* Parse a template-parameter.
15391 template-parameter:
15392 type-parameter
15393 parameter-declaration
15395 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15396 the parameter. The TREE_PURPOSE is the default value, if any.
15397 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15398 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15399 set to true iff this parameter is a parameter pack. */
15401 static tree
15402 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15403 bool *is_parameter_pack)
15405 cp_token *token;
15406 cp_parameter_declarator *parameter_declarator;
15407 tree parm;
15409 /* Assume it is a type parameter or a template parameter. */
15410 *is_non_type = false;
15411 /* Assume it not a parameter pack. */
15412 *is_parameter_pack = false;
15413 /* Peek at the next token. */
15414 token = cp_lexer_peek_token (parser->lexer);
15415 /* If it is `template', we have a type-parameter. */
15416 if (token->keyword == RID_TEMPLATE)
15417 return cp_parser_type_parameter (parser, is_parameter_pack);
15418 /* If it is `class' or `typename' we do not know yet whether it is a
15419 type parameter or a non-type parameter. Consider:
15421 template <typename T, typename T::X X> ...
15425 template <class C, class D*> ...
15427 Here, the first parameter is a type parameter, and the second is
15428 a non-type parameter. We can tell by looking at the token after
15429 the identifier -- if it is a `,', `=', or `>' then we have a type
15430 parameter. */
15431 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15433 /* Peek at the token after `class' or `typename'. */
15434 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15435 /* If it's an ellipsis, we have a template type parameter
15436 pack. */
15437 if (token->type == CPP_ELLIPSIS)
15438 return cp_parser_type_parameter (parser, is_parameter_pack);
15439 /* If it's an identifier, skip it. */
15440 if (token->type == CPP_NAME)
15441 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15442 /* Now, see if the token looks like the end of a template
15443 parameter. */
15444 if (token->type == CPP_COMMA
15445 || token->type == CPP_EQ
15446 || token->type == CPP_GREATER)
15447 return cp_parser_type_parameter (parser, is_parameter_pack);
15450 /* Otherwise, it is a non-type parameter or a constrained parameter.
15452 [temp.param]
15454 When parsing a default template-argument for a non-type
15455 template-parameter, the first non-nested `>' is taken as the end
15456 of the template parameter-list rather than a greater-than
15457 operator. */
15458 parameter_declarator
15459 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15460 /*parenthesized_p=*/NULL);
15462 if (!parameter_declarator)
15463 return error_mark_node;
15465 /* If the parameter declaration is marked as a parameter pack, set
15466 *IS_PARAMETER_PACK to notify the caller. */
15467 if (parameter_declarator->template_parameter_pack_p)
15468 *is_parameter_pack = true;
15470 if (parameter_declarator->default_argument)
15472 /* Can happen in some cases of erroneous input (c++/34892). */
15473 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15474 /* Consume the `...' for better error recovery. */
15475 cp_lexer_consume_token (parser->lexer);
15478 // The parameter may have been constrained.
15479 if (is_constrained_parameter (parameter_declarator))
15480 return finish_constrained_parameter (parser,
15481 parameter_declarator,
15482 is_non_type,
15483 is_parameter_pack);
15485 // Now we're sure that the parameter is a non-type parameter.
15486 *is_non_type = true;
15488 parm = grokdeclarator (parameter_declarator->declarator,
15489 &parameter_declarator->decl_specifiers,
15490 TPARM, /*initialized=*/0,
15491 /*attrlist=*/NULL);
15492 if (parm == error_mark_node)
15493 return error_mark_node;
15495 return build_tree_list (parameter_declarator->default_argument, parm);
15498 /* Parse a type-parameter.
15500 type-parameter:
15501 class identifier [opt]
15502 class identifier [opt] = type-id
15503 typename identifier [opt]
15504 typename identifier [opt] = type-id
15505 template < template-parameter-list > class identifier [opt]
15506 template < template-parameter-list > class identifier [opt]
15507 = id-expression
15509 GNU Extension (variadic templates):
15511 type-parameter:
15512 class ... identifier [opt]
15513 typename ... identifier [opt]
15515 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15516 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15517 the declaration of the parameter.
15519 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15521 static tree
15522 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15524 cp_token *token;
15525 tree parameter;
15527 /* Look for a keyword to tell us what kind of parameter this is. */
15528 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15529 if (!token)
15530 return error_mark_node;
15532 switch (token->keyword)
15534 case RID_CLASS:
15535 case RID_TYPENAME:
15537 tree identifier;
15538 tree default_argument;
15540 /* If the next token is an ellipsis, we have a template
15541 argument pack. */
15542 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15544 /* Consume the `...' token. */
15545 cp_lexer_consume_token (parser->lexer);
15546 maybe_warn_variadic_templates ();
15548 *is_parameter_pack = true;
15551 /* If the next token is an identifier, then it names the
15552 parameter. */
15553 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15554 identifier = cp_parser_identifier (parser);
15555 else
15556 identifier = NULL_TREE;
15558 /* Create the parameter. */
15559 parameter = finish_template_type_parm (class_type_node, identifier);
15561 /* If the next token is an `=', we have a default argument. */
15562 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15564 default_argument
15565 = cp_parser_default_type_template_argument (parser);
15567 /* Template parameter packs cannot have default
15568 arguments. */
15569 if (*is_parameter_pack)
15571 if (identifier)
15572 error_at (token->location,
15573 "template parameter pack %qD cannot have a "
15574 "default argument", identifier);
15575 else
15576 error_at (token->location,
15577 "template parameter packs cannot have "
15578 "default arguments");
15579 default_argument = NULL_TREE;
15581 else if (check_for_bare_parameter_packs (default_argument))
15582 default_argument = error_mark_node;
15584 else
15585 default_argument = NULL_TREE;
15587 /* Create the combined representation of the parameter and the
15588 default argument. */
15589 parameter = build_tree_list (default_argument, parameter);
15591 break;
15593 case RID_TEMPLATE:
15595 tree identifier;
15596 tree default_argument;
15598 /* Look for the `<'. */
15599 cp_parser_require (parser, CPP_LESS, RT_LESS);
15600 /* Parse the template-parameter-list. */
15601 cp_parser_template_parameter_list (parser);
15602 /* Look for the `>'. */
15603 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15605 // If template requirements are present, parse them.
15606 if (flag_concepts)
15608 tree reqs = get_shorthand_constraints (current_template_parms);
15609 if (tree r = cp_parser_requires_clause_opt (parser))
15610 reqs = conjoin_constraints (reqs, normalize_expression (r));
15611 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15614 /* Look for the `class' or 'typename' keywords. */
15615 cp_parser_type_parameter_key (parser);
15616 /* If the next token is an ellipsis, we have a template
15617 argument pack. */
15618 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15620 /* Consume the `...' token. */
15621 cp_lexer_consume_token (parser->lexer);
15622 maybe_warn_variadic_templates ();
15624 *is_parameter_pack = true;
15626 /* If the next token is an `=', then there is a
15627 default-argument. If the next token is a `>', we are at
15628 the end of the parameter-list. If the next token is a `,',
15629 then we are at the end of this parameter. */
15630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15631 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15632 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15634 identifier = cp_parser_identifier (parser);
15635 /* Treat invalid names as if the parameter were nameless. */
15636 if (identifier == error_mark_node)
15637 identifier = NULL_TREE;
15639 else
15640 identifier = NULL_TREE;
15642 /* Create the template parameter. */
15643 parameter = finish_template_template_parm (class_type_node,
15644 identifier);
15646 /* If the next token is an `=', then there is a
15647 default-argument. */
15648 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15650 default_argument
15651 = cp_parser_default_template_template_argument (parser);
15653 /* Template parameter packs cannot have default
15654 arguments. */
15655 if (*is_parameter_pack)
15657 if (identifier)
15658 error_at (token->location,
15659 "template parameter pack %qD cannot "
15660 "have a default argument",
15661 identifier);
15662 else
15663 error_at (token->location, "template parameter packs cannot "
15664 "have default arguments");
15665 default_argument = NULL_TREE;
15668 else
15669 default_argument = NULL_TREE;
15671 /* Create the combined representation of the parameter and the
15672 default argument. */
15673 parameter = build_tree_list (default_argument, parameter);
15675 break;
15677 default:
15678 gcc_unreachable ();
15679 break;
15682 return parameter;
15685 /* Parse a template-id.
15687 template-id:
15688 template-name < template-argument-list [opt] >
15690 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15691 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15692 returned. Otherwise, if the template-name names a function, or set
15693 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15694 names a class, returns a TYPE_DECL for the specialization.
15696 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15697 uninstantiated templates. */
15699 static tree
15700 cp_parser_template_id (cp_parser *parser,
15701 bool template_keyword_p,
15702 bool check_dependency_p,
15703 enum tag_types tag_type,
15704 bool is_declaration)
15706 tree templ;
15707 tree arguments;
15708 tree template_id;
15709 cp_token_position start_of_id = 0;
15710 cp_token *next_token = NULL, *next_token_2 = NULL;
15711 bool is_identifier;
15713 /* If the next token corresponds to a template-id, there is no need
15714 to reparse it. */
15715 cp_token *token = cp_lexer_peek_token (parser->lexer);
15716 if (token->type == CPP_TEMPLATE_ID)
15718 cp_lexer_consume_token (parser->lexer);
15719 return saved_checks_value (token->u.tree_check_value);
15722 /* Avoid performing name lookup if there is no possibility of
15723 finding a template-id. */
15724 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15725 || (token->type == CPP_NAME
15726 && !cp_parser_nth_token_starts_template_argument_list_p
15727 (parser, 2)))
15729 cp_parser_error (parser, "expected template-id");
15730 return error_mark_node;
15733 /* Remember where the template-id starts. */
15734 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15735 start_of_id = cp_lexer_token_position (parser->lexer, false);
15737 push_deferring_access_checks (dk_deferred);
15739 /* Parse the template-name. */
15740 is_identifier = false;
15741 templ = cp_parser_template_name (parser, template_keyword_p,
15742 check_dependency_p,
15743 is_declaration,
15744 tag_type,
15745 &is_identifier);
15746 if (templ == error_mark_node || is_identifier)
15748 pop_deferring_access_checks ();
15749 return templ;
15752 /* Since we're going to preserve any side-effects from this parse, set up a
15753 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15754 in the template arguments. */
15755 tentative_firewall firewall (parser);
15757 /* If we find the sequence `[:' after a template-name, it's probably
15758 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15759 parse correctly the argument list. */
15760 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15761 == CPP_OPEN_SQUARE)
15762 && next_token->flags & DIGRAPH
15763 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15764 == CPP_COLON)
15765 && !(next_token_2->flags & PREV_WHITE))
15767 cp_parser_parse_tentatively (parser);
15768 /* Change `:' into `::'. */
15769 next_token_2->type = CPP_SCOPE;
15770 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15771 CPP_LESS. */
15772 cp_lexer_consume_token (parser->lexer);
15774 /* Parse the arguments. */
15775 arguments = cp_parser_enclosed_template_argument_list (parser);
15776 if (!cp_parser_parse_definitely (parser))
15778 /* If we couldn't parse an argument list, then we revert our changes
15779 and return simply an error. Maybe this is not a template-id
15780 after all. */
15781 next_token_2->type = CPP_COLON;
15782 cp_parser_error (parser, "expected %<<%>");
15783 pop_deferring_access_checks ();
15784 return error_mark_node;
15786 /* Otherwise, emit an error about the invalid digraph, but continue
15787 parsing because we got our argument list. */
15788 if (permerror (next_token->location,
15789 "%<<::%> cannot begin a template-argument list"))
15791 static bool hint = false;
15792 inform (next_token->location,
15793 "%<<:%> is an alternate spelling for %<[%>."
15794 " Insert whitespace between %<<%> and %<::%>");
15795 if (!hint && !flag_permissive)
15797 inform (next_token->location, "(if you use %<-fpermissive%> "
15798 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15799 "accept your code)");
15800 hint = true;
15804 else
15806 /* Look for the `<' that starts the template-argument-list. */
15807 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15809 pop_deferring_access_checks ();
15810 return error_mark_node;
15812 /* Parse the arguments. */
15813 arguments = cp_parser_enclosed_template_argument_list (parser);
15816 /* Set the location to be of the form:
15817 template-name < template-argument-list [opt] >
15818 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15819 with caret == start at the start of the template-name,
15820 ranging until the closing '>'. */
15821 location_t finish_loc
15822 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15823 location_t combined_loc
15824 = make_location (token->location, token->location, finish_loc);
15826 /* Check for concepts autos where they don't belong. We could
15827 identify types in some cases of idnetifier TEMPL, looking ahead
15828 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15829 types. We reject them in functions, but if what we have is an
15830 identifier, even with none_type we can't conclude it's NOT a
15831 type, we have to wait for template substitution. */
15832 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15833 template_id = error_mark_node;
15834 /* Build a representation of the specialization. */
15835 else if (identifier_p (templ))
15836 template_id = build_min_nt_loc (combined_loc,
15837 TEMPLATE_ID_EXPR,
15838 templ, arguments);
15839 else if (DECL_TYPE_TEMPLATE_P (templ)
15840 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15842 bool entering_scope;
15843 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15844 template (rather than some instantiation thereof) only if
15845 is not nested within some other construct. For example, in
15846 "template <typename T> void f(T) { A<T>::", A<T> is just an
15847 instantiation of A. */
15848 entering_scope = (template_parm_scope_p ()
15849 && cp_lexer_next_token_is (parser->lexer,
15850 CPP_SCOPE));
15851 template_id
15852 = finish_template_type (templ, arguments, entering_scope);
15854 /* A template-like identifier may be a partial concept id. */
15855 else if (flag_concepts
15856 && (template_id = (cp_parser_maybe_partial_concept_id
15857 (parser, templ, arguments))))
15858 return template_id;
15859 else if (variable_template_p (templ))
15861 template_id = lookup_template_variable (templ, arguments);
15862 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15863 SET_EXPR_LOCATION (template_id, combined_loc);
15865 else
15867 /* If it's not a class-template or a template-template, it should be
15868 a function-template. */
15869 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15870 || TREE_CODE (templ) == OVERLOAD
15871 || BASELINK_P (templ)));
15873 template_id = lookup_template_function (templ, arguments);
15874 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15875 SET_EXPR_LOCATION (template_id, combined_loc);
15878 /* If parsing tentatively, replace the sequence of tokens that makes
15879 up the template-id with a CPP_TEMPLATE_ID token. That way,
15880 should we re-parse the token stream, we will not have to repeat
15881 the effort required to do the parse, nor will we issue duplicate
15882 error messages about problems during instantiation of the
15883 template. */
15884 if (start_of_id
15885 /* Don't do this if we had a parse error in a declarator; re-parsing
15886 might succeed if a name changes meaning (60361). */
15887 && !(cp_parser_error_occurred (parser)
15888 && cp_parser_parsing_tentatively (parser)
15889 && parser->in_declarator_p))
15891 /* Reset the contents of the START_OF_ID token. */
15892 token->type = CPP_TEMPLATE_ID;
15893 token->location = combined_loc;
15895 /* We must mark the lookup as kept, so we don't throw it away on
15896 the first parse. */
15897 if (is_overloaded_fn (template_id))
15898 lookup_keep (get_fns (template_id), true);
15900 /* Retrieve any deferred checks. Do not pop this access checks yet
15901 so the memory will not be reclaimed during token replacing below. */
15902 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15903 token->u.tree_check_value->value = template_id;
15904 token->u.tree_check_value->checks = get_deferred_access_checks ();
15905 token->keyword = RID_MAX;
15907 /* Purge all subsequent tokens. */
15908 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15910 /* ??? Can we actually assume that, if template_id ==
15911 error_mark_node, we will have issued a diagnostic to the
15912 user, as opposed to simply marking the tentative parse as
15913 failed? */
15914 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15915 error_at (token->location, "parse error in template argument list");
15918 pop_to_parent_deferring_access_checks ();
15919 return template_id;
15922 /* Parse a template-name.
15924 template-name:
15925 identifier
15927 The standard should actually say:
15929 template-name:
15930 identifier
15931 operator-function-id
15933 A defect report has been filed about this issue.
15935 A conversion-function-id cannot be a template name because they cannot
15936 be part of a template-id. In fact, looking at this code:
15938 a.operator K<int>()
15940 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15941 It is impossible to call a templated conversion-function-id with an
15942 explicit argument list, since the only allowed template parameter is
15943 the type to which it is converting.
15945 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15946 `template' keyword, in a construction like:
15948 T::template f<3>()
15950 In that case `f' is taken to be a template-name, even though there
15951 is no way of knowing for sure.
15953 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15954 name refers to a set of overloaded functions, at least one of which
15955 is a template, or an IDENTIFIER_NODE with the name of the template,
15956 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15957 names are looked up inside uninstantiated templates. */
15959 static tree
15960 cp_parser_template_name (cp_parser* parser,
15961 bool template_keyword_p,
15962 bool check_dependency_p,
15963 bool is_declaration,
15964 enum tag_types tag_type,
15965 bool *is_identifier)
15967 tree identifier;
15968 tree decl;
15969 cp_token *token = cp_lexer_peek_token (parser->lexer);
15971 /* If the next token is `operator', then we have either an
15972 operator-function-id or a conversion-function-id. */
15973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15975 /* We don't know whether we're looking at an
15976 operator-function-id or a conversion-function-id. */
15977 cp_parser_parse_tentatively (parser);
15978 /* Try an operator-function-id. */
15979 identifier = cp_parser_operator_function_id (parser);
15980 /* If that didn't work, try a conversion-function-id. */
15981 if (!cp_parser_parse_definitely (parser))
15983 cp_parser_error (parser, "expected template-name");
15984 return error_mark_node;
15987 /* Look for the identifier. */
15988 else
15989 identifier = cp_parser_identifier (parser);
15991 /* If we didn't find an identifier, we don't have a template-id. */
15992 if (identifier == error_mark_node)
15993 return error_mark_node;
15995 /* If the name immediately followed the `template' keyword, then it
15996 is a template-name. However, if the next token is not `<', then
15997 we do not treat it as a template-name, since it is not being used
15998 as part of a template-id. This enables us to handle constructs
15999 like:
16001 template <typename T> struct S { S(); };
16002 template <typename T> S<T>::S();
16004 correctly. We would treat `S' as a template -- if it were `S<T>'
16005 -- but we do not if there is no `<'. */
16007 if (processing_template_decl
16008 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16010 /* In a declaration, in a dependent context, we pretend that the
16011 "template" keyword was present in order to improve error
16012 recovery. For example, given:
16014 template <typename T> void f(T::X<int>);
16016 we want to treat "X<int>" as a template-id. */
16017 if (is_declaration
16018 && !template_keyword_p
16019 && parser->scope && TYPE_P (parser->scope)
16020 && check_dependency_p
16021 && dependent_scope_p (parser->scope)
16022 /* Do not do this for dtors (or ctors), since they never
16023 need the template keyword before their name. */
16024 && !constructor_name_p (identifier, parser->scope))
16026 cp_token_position start = 0;
16028 /* Explain what went wrong. */
16029 error_at (token->location, "non-template %qD used as template",
16030 identifier);
16031 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16032 parser->scope, identifier);
16033 /* If parsing tentatively, find the location of the "<" token. */
16034 if (cp_parser_simulate_error (parser))
16035 start = cp_lexer_token_position (parser->lexer, true);
16036 /* Parse the template arguments so that we can issue error
16037 messages about them. */
16038 cp_lexer_consume_token (parser->lexer);
16039 cp_parser_enclosed_template_argument_list (parser);
16040 /* Skip tokens until we find a good place from which to
16041 continue parsing. */
16042 cp_parser_skip_to_closing_parenthesis (parser,
16043 /*recovering=*/true,
16044 /*or_comma=*/true,
16045 /*consume_paren=*/false);
16046 /* If parsing tentatively, permanently remove the
16047 template argument list. That will prevent duplicate
16048 error messages from being issued about the missing
16049 "template" keyword. */
16050 if (start)
16051 cp_lexer_purge_tokens_after (parser->lexer, start);
16052 if (is_identifier)
16053 *is_identifier = true;
16054 parser->context->object_type = NULL_TREE;
16055 return identifier;
16058 /* If the "template" keyword is present, then there is generally
16059 no point in doing name-lookup, so we just return IDENTIFIER.
16060 But, if the qualifying scope is non-dependent then we can
16061 (and must) do name-lookup normally. */
16062 if (template_keyword_p)
16064 tree scope = (parser->scope ? parser->scope
16065 : parser->context->object_type);
16066 if (scope && TYPE_P (scope)
16067 && (!CLASS_TYPE_P (scope)
16068 || (check_dependency_p && dependent_type_p (scope))))
16070 /* We're optimizing away the call to cp_parser_lookup_name, but
16071 we still need to do this. */
16072 parser->context->object_type = NULL_TREE;
16073 return identifier;
16078 /* Look up the name. */
16079 decl = cp_parser_lookup_name (parser, identifier,
16080 tag_type,
16081 /*is_template=*/true,
16082 /*is_namespace=*/false,
16083 check_dependency_p,
16084 /*ambiguous_decls=*/NULL,
16085 token->location);
16087 decl = strip_using_decl (decl);
16089 /* If DECL is a template, then the name was a template-name. */
16090 if (TREE_CODE (decl) == TEMPLATE_DECL)
16092 if (TREE_DEPRECATED (decl)
16093 && deprecated_state != DEPRECATED_SUPPRESS)
16094 warn_deprecated_use (decl, NULL_TREE);
16096 else
16098 /* The standard does not explicitly indicate whether a name that
16099 names a set of overloaded declarations, some of which are
16100 templates, is a template-name. However, such a name should
16101 be a template-name; otherwise, there is no way to form a
16102 template-id for the overloaded templates. */
16103 bool found = false;
16105 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16106 !found && iter; ++iter)
16107 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16108 found = true;
16110 if (!found)
16112 /* The name does not name a template. */
16113 cp_parser_error (parser, "expected template-name");
16114 return error_mark_node;
16118 /* If DECL is dependent, and refers to a function, then just return
16119 its name; we will look it up again during template instantiation. */
16120 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16122 tree scope = ovl_scope (decl);
16123 if (TYPE_P (scope) && dependent_type_p (scope))
16124 return identifier;
16127 return decl;
16130 /* Parse a template-argument-list.
16132 template-argument-list:
16133 template-argument ... [opt]
16134 template-argument-list , template-argument ... [opt]
16136 Returns a TREE_VEC containing the arguments. */
16138 static tree
16139 cp_parser_template_argument_list (cp_parser* parser)
16141 tree fixed_args[10];
16142 unsigned n_args = 0;
16143 unsigned alloced = 10;
16144 tree *arg_ary = fixed_args;
16145 tree vec;
16146 bool saved_in_template_argument_list_p;
16147 bool saved_ice_p;
16148 bool saved_non_ice_p;
16150 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16151 parser->in_template_argument_list_p = true;
16152 /* Even if the template-id appears in an integral
16153 constant-expression, the contents of the argument list do
16154 not. */
16155 saved_ice_p = parser->integral_constant_expression_p;
16156 parser->integral_constant_expression_p = false;
16157 saved_non_ice_p = parser->non_integral_constant_expression_p;
16158 parser->non_integral_constant_expression_p = false;
16160 /* Parse the arguments. */
16163 tree argument;
16165 if (n_args)
16166 /* Consume the comma. */
16167 cp_lexer_consume_token (parser->lexer);
16169 /* Parse the template-argument. */
16170 argument = cp_parser_template_argument (parser);
16172 /* If the next token is an ellipsis, we're expanding a template
16173 argument pack. */
16174 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16176 if (argument == error_mark_node)
16178 cp_token *token = cp_lexer_peek_token (parser->lexer);
16179 error_at (token->location,
16180 "expected parameter pack before %<...%>");
16182 /* Consume the `...' token. */
16183 cp_lexer_consume_token (parser->lexer);
16185 /* Make the argument into a TYPE_PACK_EXPANSION or
16186 EXPR_PACK_EXPANSION. */
16187 argument = make_pack_expansion (argument);
16190 if (n_args == alloced)
16192 alloced *= 2;
16194 if (arg_ary == fixed_args)
16196 arg_ary = XNEWVEC (tree, alloced);
16197 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16199 else
16200 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16202 arg_ary[n_args++] = argument;
16204 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16206 vec = make_tree_vec (n_args);
16208 while (n_args--)
16209 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16211 if (arg_ary != fixed_args)
16212 free (arg_ary);
16213 parser->non_integral_constant_expression_p = saved_non_ice_p;
16214 parser->integral_constant_expression_p = saved_ice_p;
16215 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16216 if (CHECKING_P)
16217 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16218 return vec;
16221 /* Parse a template-argument.
16223 template-argument:
16224 assignment-expression
16225 type-id
16226 id-expression
16228 The representation is that of an assignment-expression, type-id, or
16229 id-expression -- except that the qualified id-expression is
16230 evaluated, so that the value returned is either a DECL or an
16231 OVERLOAD.
16233 Although the standard says "assignment-expression", it forbids
16234 throw-expressions or assignments in the template argument.
16235 Therefore, we use "conditional-expression" instead. */
16237 static tree
16238 cp_parser_template_argument (cp_parser* parser)
16240 tree argument;
16241 bool template_p;
16242 bool address_p;
16243 bool maybe_type_id = false;
16244 cp_token *token = NULL, *argument_start_token = NULL;
16245 location_t loc = 0;
16246 cp_id_kind idk;
16248 /* There's really no way to know what we're looking at, so we just
16249 try each alternative in order.
16251 [temp.arg]
16253 In a template-argument, an ambiguity between a type-id and an
16254 expression is resolved to a type-id, regardless of the form of
16255 the corresponding template-parameter.
16257 Therefore, we try a type-id first. */
16258 cp_parser_parse_tentatively (parser);
16259 argument = cp_parser_template_type_arg (parser);
16260 /* If there was no error parsing the type-id but the next token is a
16261 '>>', our behavior depends on which dialect of C++ we're
16262 parsing. In C++98, we probably found a typo for '> >'. But there
16263 are type-id which are also valid expressions. For instance:
16265 struct X { int operator >> (int); };
16266 template <int V> struct Foo {};
16267 Foo<X () >> 5> r;
16269 Here 'X()' is a valid type-id of a function type, but the user just
16270 wanted to write the expression "X() >> 5". Thus, we remember that we
16271 found a valid type-id, but we still try to parse the argument as an
16272 expression to see what happens.
16274 In C++0x, the '>>' will be considered two separate '>'
16275 tokens. */
16276 if (!cp_parser_error_occurred (parser)
16277 && cxx_dialect == cxx98
16278 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16280 maybe_type_id = true;
16281 cp_parser_abort_tentative_parse (parser);
16283 else
16285 /* If the next token isn't a `,' or a `>', then this argument wasn't
16286 really finished. This means that the argument is not a valid
16287 type-id. */
16288 if (!cp_parser_next_token_ends_template_argument_p (parser))
16289 cp_parser_error (parser, "expected template-argument");
16290 /* If that worked, we're done. */
16291 if (cp_parser_parse_definitely (parser))
16292 return argument;
16294 /* We're still not sure what the argument will be. */
16295 cp_parser_parse_tentatively (parser);
16296 /* Try a template. */
16297 argument_start_token = cp_lexer_peek_token (parser->lexer);
16298 argument = cp_parser_id_expression (parser,
16299 /*template_keyword_p=*/false,
16300 /*check_dependency_p=*/true,
16301 &template_p,
16302 /*declarator_p=*/false,
16303 /*optional_p=*/false);
16304 /* If the next token isn't a `,' or a `>', then this argument wasn't
16305 really finished. */
16306 if (!cp_parser_next_token_ends_template_argument_p (parser))
16307 cp_parser_error (parser, "expected template-argument");
16308 if (!cp_parser_error_occurred (parser))
16310 /* Figure out what is being referred to. If the id-expression
16311 was for a class template specialization, then we will have a
16312 TYPE_DECL at this point. There is no need to do name lookup
16313 at this point in that case. */
16314 if (TREE_CODE (argument) != TYPE_DECL)
16315 argument = cp_parser_lookup_name (parser, argument,
16316 none_type,
16317 /*is_template=*/template_p,
16318 /*is_namespace=*/false,
16319 /*check_dependency=*/true,
16320 /*ambiguous_decls=*/NULL,
16321 argument_start_token->location);
16322 /* Handle a constrained-type-specifier for a non-type template
16323 parameter. */
16324 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16325 argument = decl;
16326 else if (TREE_CODE (argument) != TEMPLATE_DECL
16327 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16328 cp_parser_error (parser, "expected template-name");
16330 if (cp_parser_parse_definitely (parser))
16332 if (TREE_DEPRECATED (argument))
16333 warn_deprecated_use (argument, NULL_TREE);
16334 return argument;
16336 /* It must be a non-type argument. In C++17 any constant-expression is
16337 allowed. */
16338 if (cxx_dialect > cxx14)
16339 goto general_expr;
16341 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16343 -- an integral constant-expression of integral or enumeration
16344 type; or
16346 -- the name of a non-type template-parameter; or
16348 -- the name of an object or function with external linkage...
16350 -- the address of an object or function with external linkage...
16352 -- a pointer to member... */
16353 /* Look for a non-type template parameter. */
16354 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16356 cp_parser_parse_tentatively (parser);
16357 argument = cp_parser_primary_expression (parser,
16358 /*address_p=*/false,
16359 /*cast_p=*/false,
16360 /*template_arg_p=*/true,
16361 &idk);
16362 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16363 || !cp_parser_next_token_ends_template_argument_p (parser))
16364 cp_parser_simulate_error (parser);
16365 if (cp_parser_parse_definitely (parser))
16366 return argument;
16369 /* If the next token is "&", the argument must be the address of an
16370 object or function with external linkage. */
16371 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16372 if (address_p)
16374 loc = cp_lexer_peek_token (parser->lexer)->location;
16375 cp_lexer_consume_token (parser->lexer);
16377 /* See if we might have an id-expression. */
16378 token = cp_lexer_peek_token (parser->lexer);
16379 if (token->type == CPP_NAME
16380 || token->keyword == RID_OPERATOR
16381 || token->type == CPP_SCOPE
16382 || token->type == CPP_TEMPLATE_ID
16383 || token->type == CPP_NESTED_NAME_SPECIFIER)
16385 cp_parser_parse_tentatively (parser);
16386 argument = cp_parser_primary_expression (parser,
16387 address_p,
16388 /*cast_p=*/false,
16389 /*template_arg_p=*/true,
16390 &idk);
16391 if (cp_parser_error_occurred (parser)
16392 || !cp_parser_next_token_ends_template_argument_p (parser))
16393 cp_parser_abort_tentative_parse (parser);
16394 else
16396 tree probe;
16398 if (INDIRECT_REF_P (argument))
16400 /* Strip the dereference temporarily. */
16401 gcc_assert (REFERENCE_REF_P (argument));
16402 argument = TREE_OPERAND (argument, 0);
16405 /* If we're in a template, we represent a qualified-id referring
16406 to a static data member as a SCOPE_REF even if the scope isn't
16407 dependent so that we can check access control later. */
16408 probe = argument;
16409 if (TREE_CODE (probe) == SCOPE_REF)
16410 probe = TREE_OPERAND (probe, 1);
16411 if (VAR_P (probe))
16413 /* A variable without external linkage might still be a
16414 valid constant-expression, so no error is issued here
16415 if the external-linkage check fails. */
16416 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16417 cp_parser_simulate_error (parser);
16419 else if (is_overloaded_fn (argument))
16420 /* All overloaded functions are allowed; if the external
16421 linkage test does not pass, an error will be issued
16422 later. */
16424 else if (address_p
16425 && (TREE_CODE (argument) == OFFSET_REF
16426 || TREE_CODE (argument) == SCOPE_REF))
16427 /* A pointer-to-member. */
16429 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16431 else
16432 cp_parser_simulate_error (parser);
16434 if (cp_parser_parse_definitely (parser))
16436 if (address_p)
16437 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16438 tf_warning_or_error);
16439 else
16440 argument = convert_from_reference (argument);
16441 return argument;
16445 /* If the argument started with "&", there are no other valid
16446 alternatives at this point. */
16447 if (address_p)
16449 cp_parser_error (parser, "invalid non-type template argument");
16450 return error_mark_node;
16453 general_expr:
16454 /* If the argument wasn't successfully parsed as a type-id followed
16455 by '>>', the argument can only be a constant expression now.
16456 Otherwise, we try parsing the constant-expression tentatively,
16457 because the argument could really be a type-id. */
16458 if (maybe_type_id)
16459 cp_parser_parse_tentatively (parser);
16461 if (cxx_dialect <= cxx14)
16462 argument = cp_parser_constant_expression (parser);
16463 else
16465 /* With C++17 generalized non-type template arguments we need to handle
16466 lvalue constant expressions, too. */
16467 argument = cp_parser_assignment_expression (parser);
16468 require_potential_constant_expression (argument);
16471 if (!maybe_type_id)
16472 return argument;
16473 if (!cp_parser_next_token_ends_template_argument_p (parser))
16474 cp_parser_error (parser, "expected template-argument");
16475 if (cp_parser_parse_definitely (parser))
16476 return argument;
16477 /* We did our best to parse the argument as a non type-id, but that
16478 was the only alternative that matched (albeit with a '>' after
16479 it). We can assume it's just a typo from the user, and a
16480 diagnostic will then be issued. */
16481 return cp_parser_template_type_arg (parser);
16484 /* Parse an explicit-instantiation.
16486 explicit-instantiation:
16487 template declaration
16489 Although the standard says `declaration', what it really means is:
16491 explicit-instantiation:
16492 template decl-specifier-seq [opt] declarator [opt] ;
16494 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16495 supposed to be allowed. A defect report has been filed about this
16496 issue.
16498 GNU Extension:
16500 explicit-instantiation:
16501 storage-class-specifier template
16502 decl-specifier-seq [opt] declarator [opt] ;
16503 function-specifier template
16504 decl-specifier-seq [opt] declarator [opt] ; */
16506 static void
16507 cp_parser_explicit_instantiation (cp_parser* parser)
16509 int declares_class_or_enum;
16510 cp_decl_specifier_seq decl_specifiers;
16511 tree extension_specifier = NULL_TREE;
16513 timevar_push (TV_TEMPLATE_INST);
16515 /* Look for an (optional) storage-class-specifier or
16516 function-specifier. */
16517 if (cp_parser_allow_gnu_extensions_p (parser))
16519 extension_specifier
16520 = cp_parser_storage_class_specifier_opt (parser);
16521 if (!extension_specifier)
16522 extension_specifier
16523 = cp_parser_function_specifier_opt (parser,
16524 /*decl_specs=*/NULL);
16527 /* Look for the `template' keyword. */
16528 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16529 /* Let the front end know that we are processing an explicit
16530 instantiation. */
16531 begin_explicit_instantiation ();
16532 /* [temp.explicit] says that we are supposed to ignore access
16533 control while processing explicit instantiation directives. */
16534 push_deferring_access_checks (dk_no_check);
16535 /* Parse a decl-specifier-seq. */
16536 cp_parser_decl_specifier_seq (parser,
16537 CP_PARSER_FLAGS_OPTIONAL,
16538 &decl_specifiers,
16539 &declares_class_or_enum);
16540 /* If there was exactly one decl-specifier, and it declared a class,
16541 and there's no declarator, then we have an explicit type
16542 instantiation. */
16543 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16545 tree type;
16547 type = check_tag_decl (&decl_specifiers,
16548 /*explicit_type_instantiation_p=*/true);
16549 /* Turn access control back on for names used during
16550 template instantiation. */
16551 pop_deferring_access_checks ();
16552 if (type)
16553 do_type_instantiation (type, extension_specifier,
16554 /*complain=*/tf_error);
16556 else
16558 cp_declarator *declarator;
16559 tree decl;
16561 /* Parse the declarator. */
16562 declarator
16563 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16564 /*ctor_dtor_or_conv_p=*/NULL,
16565 /*parenthesized_p=*/NULL,
16566 /*member_p=*/false,
16567 /*friend_p=*/false);
16568 if (declares_class_or_enum & 2)
16569 cp_parser_check_for_definition_in_return_type (declarator,
16570 decl_specifiers.type,
16571 decl_specifiers.locations[ds_type_spec]);
16572 if (declarator != cp_error_declarator)
16574 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16575 permerror (decl_specifiers.locations[ds_inline],
16576 "explicit instantiation shall not use"
16577 " %<inline%> specifier");
16578 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16579 permerror (decl_specifiers.locations[ds_constexpr],
16580 "explicit instantiation shall not use"
16581 " %<constexpr%> specifier");
16583 decl = grokdeclarator (declarator, &decl_specifiers,
16584 NORMAL, 0, &decl_specifiers.attributes);
16585 /* Turn access control back on for names used during
16586 template instantiation. */
16587 pop_deferring_access_checks ();
16588 /* Do the explicit instantiation. */
16589 do_decl_instantiation (decl, extension_specifier);
16591 else
16593 pop_deferring_access_checks ();
16594 /* Skip the body of the explicit instantiation. */
16595 cp_parser_skip_to_end_of_statement (parser);
16598 /* We're done with the instantiation. */
16599 end_explicit_instantiation ();
16601 cp_parser_consume_semicolon_at_end_of_statement (parser);
16603 timevar_pop (TV_TEMPLATE_INST);
16606 /* Parse an explicit-specialization.
16608 explicit-specialization:
16609 template < > declaration
16611 Although the standard says `declaration', what it really means is:
16613 explicit-specialization:
16614 template <> decl-specifier [opt] init-declarator [opt] ;
16615 template <> function-definition
16616 template <> explicit-specialization
16617 template <> template-declaration */
16619 static void
16620 cp_parser_explicit_specialization (cp_parser* parser)
16622 bool need_lang_pop;
16623 cp_token *token = cp_lexer_peek_token (parser->lexer);
16625 /* Look for the `template' keyword. */
16626 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16627 /* Look for the `<'. */
16628 cp_parser_require (parser, CPP_LESS, RT_LESS);
16629 /* Look for the `>'. */
16630 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16631 /* We have processed another parameter list. */
16632 ++parser->num_template_parameter_lists;
16633 /* [temp]
16635 A template ... explicit specialization ... shall not have C
16636 linkage. */
16637 if (current_lang_name == lang_name_c)
16639 error_at (token->location, "template specialization with C linkage");
16640 maybe_show_extern_c_location ();
16641 /* Give it C++ linkage to avoid confusing other parts of the
16642 front end. */
16643 push_lang_context (lang_name_cplusplus);
16644 need_lang_pop = true;
16646 else
16647 need_lang_pop = false;
16648 /* Let the front end know that we are beginning a specialization. */
16649 if (!begin_specialization ())
16651 end_specialization ();
16652 return;
16655 /* If the next keyword is `template', we need to figure out whether
16656 or not we're looking a template-declaration. */
16657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16659 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16660 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16661 cp_parser_template_declaration_after_export (parser,
16662 /*member_p=*/false);
16663 else
16664 cp_parser_explicit_specialization (parser);
16666 else
16667 /* Parse the dependent declaration. */
16668 cp_parser_single_declaration (parser,
16669 /*checks=*/NULL,
16670 /*member_p=*/false,
16671 /*explicit_specialization_p=*/true,
16672 /*friend_p=*/NULL);
16673 /* We're done with the specialization. */
16674 end_specialization ();
16675 /* For the erroneous case of a template with C linkage, we pushed an
16676 implicit C++ linkage scope; exit that scope now. */
16677 if (need_lang_pop)
16678 pop_lang_context ();
16679 /* We're done with this parameter list. */
16680 --parser->num_template_parameter_lists;
16683 /* Parse a type-specifier.
16685 type-specifier:
16686 simple-type-specifier
16687 class-specifier
16688 enum-specifier
16689 elaborated-type-specifier
16690 cv-qualifier
16692 GNU Extension:
16694 type-specifier:
16695 __complex__
16697 Returns a representation of the type-specifier. For a
16698 class-specifier, enum-specifier, or elaborated-type-specifier, a
16699 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16701 The parser flags FLAGS is used to control type-specifier parsing.
16703 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16704 in a decl-specifier-seq.
16706 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16707 class-specifier, enum-specifier, or elaborated-type-specifier, then
16708 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16709 if a type is declared; 2 if it is defined. Otherwise, it is set to
16710 zero.
16712 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16713 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16714 is set to FALSE. */
16716 static tree
16717 cp_parser_type_specifier (cp_parser* parser,
16718 cp_parser_flags flags,
16719 cp_decl_specifier_seq *decl_specs,
16720 bool is_declaration,
16721 int* declares_class_or_enum,
16722 bool* is_cv_qualifier)
16724 tree type_spec = NULL_TREE;
16725 cp_token *token;
16726 enum rid keyword;
16727 cp_decl_spec ds = ds_last;
16729 /* Assume this type-specifier does not declare a new type. */
16730 if (declares_class_or_enum)
16731 *declares_class_or_enum = 0;
16732 /* And that it does not specify a cv-qualifier. */
16733 if (is_cv_qualifier)
16734 *is_cv_qualifier = false;
16735 /* Peek at the next token. */
16736 token = cp_lexer_peek_token (parser->lexer);
16738 /* If we're looking at a keyword, we can use that to guide the
16739 production we choose. */
16740 keyword = token->keyword;
16741 switch (keyword)
16743 case RID_ENUM:
16744 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16745 goto elaborated_type_specifier;
16747 /* Look for the enum-specifier. */
16748 type_spec = cp_parser_enum_specifier (parser);
16749 /* If that worked, we're done. */
16750 if (type_spec)
16752 if (declares_class_or_enum)
16753 *declares_class_or_enum = 2;
16754 if (decl_specs)
16755 cp_parser_set_decl_spec_type (decl_specs,
16756 type_spec,
16757 token,
16758 /*type_definition_p=*/true);
16759 return type_spec;
16761 else
16762 goto elaborated_type_specifier;
16764 /* Any of these indicate either a class-specifier, or an
16765 elaborated-type-specifier. */
16766 case RID_CLASS:
16767 case RID_STRUCT:
16768 case RID_UNION:
16769 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16770 goto elaborated_type_specifier;
16772 /* Parse tentatively so that we can back up if we don't find a
16773 class-specifier. */
16774 cp_parser_parse_tentatively (parser);
16775 /* Look for the class-specifier. */
16776 type_spec = cp_parser_class_specifier (parser);
16777 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16778 /* If that worked, we're done. */
16779 if (cp_parser_parse_definitely (parser))
16781 if (declares_class_or_enum)
16782 *declares_class_or_enum = 2;
16783 if (decl_specs)
16784 cp_parser_set_decl_spec_type (decl_specs,
16785 type_spec,
16786 token,
16787 /*type_definition_p=*/true);
16788 return type_spec;
16791 /* Fall through. */
16792 elaborated_type_specifier:
16793 /* We're declaring (not defining) a class or enum. */
16794 if (declares_class_or_enum)
16795 *declares_class_or_enum = 1;
16797 /* Fall through. */
16798 case RID_TYPENAME:
16799 /* Look for an elaborated-type-specifier. */
16800 type_spec
16801 = (cp_parser_elaborated_type_specifier
16802 (parser,
16803 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16804 is_declaration));
16805 if (decl_specs)
16806 cp_parser_set_decl_spec_type (decl_specs,
16807 type_spec,
16808 token,
16809 /*type_definition_p=*/false);
16810 return type_spec;
16812 case RID_CONST:
16813 ds = ds_const;
16814 if (is_cv_qualifier)
16815 *is_cv_qualifier = true;
16816 break;
16818 case RID_VOLATILE:
16819 ds = ds_volatile;
16820 if (is_cv_qualifier)
16821 *is_cv_qualifier = true;
16822 break;
16824 case RID_RESTRICT:
16825 ds = ds_restrict;
16826 if (is_cv_qualifier)
16827 *is_cv_qualifier = true;
16828 break;
16830 case RID_COMPLEX:
16831 /* The `__complex__' keyword is a GNU extension. */
16832 ds = ds_complex;
16833 break;
16835 default:
16836 break;
16839 /* Handle simple keywords. */
16840 if (ds != ds_last)
16842 if (decl_specs)
16844 set_and_check_decl_spec_loc (decl_specs, ds, token);
16845 decl_specs->any_specifiers_p = true;
16847 return cp_lexer_consume_token (parser->lexer)->u.value;
16850 /* If we do not already have a type-specifier, assume we are looking
16851 at a simple-type-specifier. */
16852 type_spec = cp_parser_simple_type_specifier (parser,
16853 decl_specs,
16854 flags);
16856 /* If we didn't find a type-specifier, and a type-specifier was not
16857 optional in this context, issue an error message. */
16858 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16860 cp_parser_error (parser, "expected type specifier");
16861 return error_mark_node;
16864 return type_spec;
16867 /* Parse a simple-type-specifier.
16869 simple-type-specifier:
16870 :: [opt] nested-name-specifier [opt] type-name
16871 :: [opt] nested-name-specifier template template-id
16872 char
16873 wchar_t
16874 bool
16875 short
16877 long
16878 signed
16879 unsigned
16880 float
16881 double
16882 void
16884 C++11 Extension:
16886 simple-type-specifier:
16887 auto
16888 decltype ( expression )
16889 char16_t
16890 char32_t
16891 __underlying_type ( type-id )
16893 C++17 extension:
16895 nested-name-specifier(opt) template-name
16897 GNU Extension:
16899 simple-type-specifier:
16900 __int128
16901 __typeof__ unary-expression
16902 __typeof__ ( type-id )
16903 __typeof__ ( type-id ) { initializer-list , [opt] }
16905 Concepts Extension:
16907 simple-type-specifier:
16908 constrained-type-specifier
16910 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16911 appropriately updated. */
16913 static tree
16914 cp_parser_simple_type_specifier (cp_parser* parser,
16915 cp_decl_specifier_seq *decl_specs,
16916 cp_parser_flags flags)
16918 tree type = NULL_TREE;
16919 cp_token *token;
16920 int idx;
16922 /* Peek at the next token. */
16923 token = cp_lexer_peek_token (parser->lexer);
16925 /* If we're looking at a keyword, things are easy. */
16926 switch (token->keyword)
16928 case RID_CHAR:
16929 if (decl_specs)
16930 decl_specs->explicit_char_p = true;
16931 type = char_type_node;
16932 break;
16933 case RID_CHAR16:
16934 type = char16_type_node;
16935 break;
16936 case RID_CHAR32:
16937 type = char32_type_node;
16938 break;
16939 case RID_WCHAR:
16940 type = wchar_type_node;
16941 break;
16942 case RID_BOOL:
16943 type = boolean_type_node;
16944 break;
16945 case RID_SHORT:
16946 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16947 type = short_integer_type_node;
16948 break;
16949 case RID_INT:
16950 if (decl_specs)
16951 decl_specs->explicit_int_p = true;
16952 type = integer_type_node;
16953 break;
16954 case RID_INT_N_0:
16955 case RID_INT_N_1:
16956 case RID_INT_N_2:
16957 case RID_INT_N_3:
16958 idx = token->keyword - RID_INT_N_0;
16959 if (! int_n_enabled_p [idx])
16960 break;
16961 if (decl_specs)
16963 decl_specs->explicit_intN_p = true;
16964 decl_specs->int_n_idx = idx;
16966 type = int_n_trees [idx].signed_type;
16967 break;
16968 case RID_LONG:
16969 if (decl_specs)
16970 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16971 type = long_integer_type_node;
16972 break;
16973 case RID_SIGNED:
16974 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16975 type = integer_type_node;
16976 break;
16977 case RID_UNSIGNED:
16978 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16979 type = unsigned_type_node;
16980 break;
16981 case RID_FLOAT:
16982 type = float_type_node;
16983 break;
16984 case RID_DOUBLE:
16985 type = double_type_node;
16986 break;
16987 case RID_VOID:
16988 type = void_type_node;
16989 break;
16991 case RID_AUTO:
16992 maybe_warn_cpp0x (CPP0X_AUTO);
16993 if (parser->auto_is_implicit_function_template_parm_p)
16995 /* The 'auto' might be the placeholder return type for a function decl
16996 with trailing return type. */
16997 bool have_trailing_return_fn_decl = false;
16999 cp_parser_parse_tentatively (parser);
17000 cp_lexer_consume_token (parser->lexer);
17001 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17002 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17003 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17004 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17006 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17008 cp_lexer_consume_token (parser->lexer);
17009 cp_parser_skip_to_closing_parenthesis (parser,
17010 /*recovering*/false,
17011 /*or_comma*/false,
17012 /*consume_paren*/true);
17013 continue;
17016 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17018 have_trailing_return_fn_decl = true;
17019 break;
17022 cp_lexer_consume_token (parser->lexer);
17024 cp_parser_abort_tentative_parse (parser);
17026 if (have_trailing_return_fn_decl)
17028 type = make_auto ();
17029 break;
17032 if (cxx_dialect >= cxx14)
17034 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17035 type = TREE_TYPE (type);
17037 else
17038 type = error_mark_node;
17040 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17042 if (cxx_dialect < cxx14)
17043 error_at (token->location,
17044 "use of %<auto%> in lambda parameter declaration "
17045 "only available with "
17046 "-std=c++14 or -std=gnu++14");
17048 else if (cxx_dialect < cxx14)
17049 error_at (token->location,
17050 "use of %<auto%> in parameter declaration "
17051 "only available with "
17052 "-std=c++14 or -std=gnu++14");
17053 else if (!flag_concepts)
17054 pedwarn (token->location, 0,
17055 "use of %<auto%> in parameter declaration "
17056 "only available with -fconcepts");
17058 else
17059 type = make_auto ();
17060 break;
17062 case RID_DECLTYPE:
17063 /* Since DR 743, decltype can either be a simple-type-specifier by
17064 itself or begin a nested-name-specifier. Parsing it will replace
17065 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17066 handling below decide what to do. */
17067 cp_parser_decltype (parser);
17068 cp_lexer_set_token_position (parser->lexer, token);
17069 break;
17071 case RID_TYPEOF:
17072 /* Consume the `typeof' token. */
17073 cp_lexer_consume_token (parser->lexer);
17074 /* Parse the operand to `typeof'. */
17075 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17076 /* If it is not already a TYPE, take its type. */
17077 if (!TYPE_P (type))
17078 type = finish_typeof (type);
17080 if (decl_specs)
17081 cp_parser_set_decl_spec_type (decl_specs, type,
17082 token,
17083 /*type_definition_p=*/false);
17085 return type;
17087 case RID_UNDERLYING_TYPE:
17088 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17089 if (decl_specs)
17090 cp_parser_set_decl_spec_type (decl_specs, type,
17091 token,
17092 /*type_definition_p=*/false);
17094 return type;
17096 case RID_BASES:
17097 case RID_DIRECT_BASES:
17098 type = cp_parser_trait_expr (parser, token->keyword);
17099 if (decl_specs)
17100 cp_parser_set_decl_spec_type (decl_specs, type,
17101 token,
17102 /*type_definition_p=*/false);
17103 return type;
17104 default:
17105 break;
17108 /* If token is an already-parsed decltype not followed by ::,
17109 it's a simple-type-specifier. */
17110 if (token->type == CPP_DECLTYPE
17111 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17113 type = saved_checks_value (token->u.tree_check_value);
17114 if (decl_specs)
17116 cp_parser_set_decl_spec_type (decl_specs, type,
17117 token,
17118 /*type_definition_p=*/false);
17119 /* Remember that we are handling a decltype in order to
17120 implement the resolution of DR 1510 when the argument
17121 isn't instantiation dependent. */
17122 decl_specs->decltype_p = true;
17124 cp_lexer_consume_token (parser->lexer);
17125 return type;
17128 /* If the type-specifier was for a built-in type, we're done. */
17129 if (type)
17131 /* Record the type. */
17132 if (decl_specs
17133 && (token->keyword != RID_SIGNED
17134 && token->keyword != RID_UNSIGNED
17135 && token->keyword != RID_SHORT
17136 && token->keyword != RID_LONG))
17137 cp_parser_set_decl_spec_type (decl_specs,
17138 type,
17139 token,
17140 /*type_definition_p=*/false);
17141 if (decl_specs)
17142 decl_specs->any_specifiers_p = true;
17144 /* Consume the token. */
17145 cp_lexer_consume_token (parser->lexer);
17147 if (type == error_mark_node)
17148 return error_mark_node;
17150 /* There is no valid C++ program where a non-template type is
17151 followed by a "<". That usually indicates that the user thought
17152 that the type was a template. */
17153 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17154 token->location);
17156 return TYPE_NAME (type);
17159 /* The type-specifier must be a user-defined type. */
17160 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17162 bool qualified_p;
17163 bool global_p;
17165 /* Don't gobble tokens or issue error messages if this is an
17166 optional type-specifier. */
17167 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17168 cp_parser_parse_tentatively (parser);
17170 token = cp_lexer_peek_token (parser->lexer);
17172 /* Look for the optional `::' operator. */
17173 global_p
17174 = (cp_parser_global_scope_opt (parser,
17175 /*current_scope_valid_p=*/false)
17176 != NULL_TREE);
17177 /* Look for the nested-name specifier. */
17178 qualified_p
17179 = (cp_parser_nested_name_specifier_opt (parser,
17180 /*typename_keyword_p=*/false,
17181 /*check_dependency_p=*/true,
17182 /*type_p=*/false,
17183 /*is_declaration=*/false)
17184 != NULL_TREE);
17185 /* If we have seen a nested-name-specifier, and the next token
17186 is `template', then we are using the template-id production. */
17187 if (parser->scope
17188 && cp_parser_optional_template_keyword (parser))
17190 /* Look for the template-id. */
17191 type = cp_parser_template_id (parser,
17192 /*template_keyword_p=*/true,
17193 /*check_dependency_p=*/true,
17194 none_type,
17195 /*is_declaration=*/false);
17196 /* If the template-id did not name a type, we are out of
17197 luck. */
17198 if (TREE_CODE (type) != TYPE_DECL)
17200 cp_parser_error (parser, "expected template-id for type");
17201 type = NULL_TREE;
17204 /* Otherwise, look for a type-name. */
17205 else
17206 type = cp_parser_type_name (parser);
17207 /* Keep track of all name-lookups performed in class scopes. */
17208 if (type
17209 && !global_p
17210 && !qualified_p
17211 && TREE_CODE (type) == TYPE_DECL
17212 && identifier_p (DECL_NAME (type)))
17213 maybe_note_name_used_in_class (DECL_NAME (type), type);
17214 /* If it didn't work out, we don't have a TYPE. */
17215 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17216 && !cp_parser_parse_definitely (parser))
17217 type = NULL_TREE;
17218 if (!type && cxx_dialect >= cxx17)
17220 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17221 cp_parser_parse_tentatively (parser);
17223 cp_parser_global_scope_opt (parser,
17224 /*current_scope_valid_p=*/false);
17225 cp_parser_nested_name_specifier_opt (parser,
17226 /*typename_keyword_p=*/false,
17227 /*check_dependency_p=*/true,
17228 /*type_p=*/false,
17229 /*is_declaration=*/false);
17230 tree name = cp_parser_identifier (parser);
17231 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17232 && parser->scope != error_mark_node)
17234 tree tmpl = cp_parser_lookup_name (parser, name,
17235 none_type,
17236 /*is_template=*/false,
17237 /*is_namespace=*/false,
17238 /*check_dependency=*/true,
17239 /*ambiguous_decls=*/NULL,
17240 token->location);
17241 if (tmpl && tmpl != error_mark_node
17242 && (DECL_CLASS_TEMPLATE_P (tmpl)
17243 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17244 type = make_template_placeholder (tmpl);
17245 else
17247 type = error_mark_node;
17248 if (!cp_parser_simulate_error (parser))
17249 cp_parser_name_lookup_error (parser, name, tmpl,
17250 NLE_TYPE, token->location);
17253 else
17254 type = error_mark_node;
17256 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17257 && !cp_parser_parse_definitely (parser))
17258 type = NULL_TREE;
17260 if (type && decl_specs)
17261 cp_parser_set_decl_spec_type (decl_specs, type,
17262 token,
17263 /*type_definition_p=*/false);
17266 /* If we didn't get a type-name, issue an error message. */
17267 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17269 cp_parser_error (parser, "expected type-name");
17270 return error_mark_node;
17273 if (type && type != error_mark_node)
17275 /* See if TYPE is an Objective-C type, and if so, parse and
17276 accept any protocol references following it. Do this before
17277 the cp_parser_check_for_invalid_template_id() call, because
17278 Objective-C types can be followed by '<...>' which would
17279 enclose protocol names rather than template arguments, and so
17280 everything is fine. */
17281 if (c_dialect_objc () && !parser->scope
17282 && (objc_is_id (type) || objc_is_class_name (type)))
17284 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17285 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17287 /* Clobber the "unqualified" type previously entered into
17288 DECL_SPECS with the new, improved protocol-qualified version. */
17289 if (decl_specs)
17290 decl_specs->type = qual_type;
17292 return qual_type;
17295 /* There is no valid C++ program where a non-template type is
17296 followed by a "<". That usually indicates that the user
17297 thought that the type was a template. */
17298 cp_parser_check_for_invalid_template_id (parser, type,
17299 none_type,
17300 token->location);
17303 return type;
17306 /* Parse a type-name.
17308 type-name:
17309 class-name
17310 enum-name
17311 typedef-name
17312 simple-template-id [in c++0x]
17314 enum-name:
17315 identifier
17317 typedef-name:
17318 identifier
17320 Concepts:
17322 type-name:
17323 concept-name
17324 partial-concept-id
17326 concept-name:
17327 identifier
17329 Returns a TYPE_DECL for the type. */
17331 static tree
17332 cp_parser_type_name (cp_parser* parser)
17334 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17337 /* See above. */
17338 static tree
17339 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17341 tree type_decl;
17343 /* We can't know yet whether it is a class-name or not. */
17344 cp_parser_parse_tentatively (parser);
17345 /* Try a class-name. */
17346 type_decl = cp_parser_class_name (parser,
17347 typename_keyword_p,
17348 /*template_keyword_p=*/false,
17349 none_type,
17350 /*check_dependency_p=*/true,
17351 /*class_head_p=*/false,
17352 /*is_declaration=*/false);
17353 /* If it's not a class-name, keep looking. */
17354 if (!cp_parser_parse_definitely (parser))
17356 if (cxx_dialect < cxx11)
17357 /* It must be a typedef-name or an enum-name. */
17358 return cp_parser_nonclass_name (parser);
17360 cp_parser_parse_tentatively (parser);
17361 /* It is either a simple-template-id representing an
17362 instantiation of an alias template... */
17363 type_decl = cp_parser_template_id (parser,
17364 /*template_keyword_p=*/false,
17365 /*check_dependency_p=*/true,
17366 none_type,
17367 /*is_declaration=*/false);
17368 /* Note that this must be an instantiation of an alias template
17369 because [temp.names]/6 says:
17371 A template-id that names an alias template specialization
17372 is a type-name.
17374 Whereas [temp.names]/7 says:
17376 A simple-template-id that names a class template
17377 specialization is a class-name.
17379 With concepts, this could also be a partial-concept-id that
17380 declares a non-type template parameter. */
17381 if (type_decl != NULL_TREE
17382 && TREE_CODE (type_decl) == TYPE_DECL
17383 && TYPE_DECL_ALIAS_P (type_decl))
17384 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17385 else if (is_constrained_parameter (type_decl))
17386 /* Don't do anything. */ ;
17387 else
17388 cp_parser_simulate_error (parser);
17390 if (!cp_parser_parse_definitely (parser))
17391 /* ... Or a typedef-name or an enum-name. */
17392 return cp_parser_nonclass_name (parser);
17395 return type_decl;
17398 /* Check if DECL and ARGS can form a constrained-type-specifier.
17399 If ARGS is non-null, we try to form a concept check of the
17400 form DECL<?, ARGS> where ? is a wildcard that matches any
17401 kind of template argument. If ARGS is NULL, then we try to
17402 form a concept check of the form DECL<?>. */
17404 static tree
17405 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17406 tree decl, tree args)
17408 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17410 /* If we a constrained-type-specifier cannot be deduced. */
17411 if (parser->prevent_constrained_type_specifiers)
17412 return NULL_TREE;
17414 /* A constrained type specifier can only be found in an
17415 overload set or as a reference to a template declaration.
17417 FIXME: This might be masking a bug. It's possible that
17418 that the deduction below is causing template specializations
17419 to be formed with the wildcard as an argument. */
17420 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17421 return NULL_TREE;
17423 /* Try to build a call expression that evaluates the
17424 concept. This can fail if the overload set refers
17425 only to non-templates. */
17426 tree placeholder = build_nt (WILDCARD_DECL);
17427 tree check = build_concept_check (decl, placeholder, args);
17428 if (check == error_mark_node)
17429 return NULL_TREE;
17431 /* Deduce the checked constraint and the prototype parameter.
17433 FIXME: In certain cases, failure to deduce should be a
17434 diagnosable error. */
17435 tree conc;
17436 tree proto;
17437 if (!deduce_constrained_parameter (check, conc, proto))
17438 return NULL_TREE;
17440 /* In template parameter scope, this results in a constrained
17441 parameter. Return a descriptor of that parm. */
17442 if (processing_template_parmlist)
17443 return build_constrained_parameter (conc, proto, args);
17445 /* In a parameter-declaration-clause, constrained-type
17446 specifiers result in invented template parameters. */
17447 if (parser->auto_is_implicit_function_template_parm_p)
17449 tree x = build_constrained_parameter (conc, proto, args);
17450 return synthesize_implicit_template_parm (parser, x);
17452 else
17454 /* Otherwise, we're in a context where the constrained
17455 type name is deduced and the constraint applies
17456 after deduction. */
17457 return make_constrained_auto (conc, args);
17460 return NULL_TREE;
17463 /* If DECL refers to a concept, return a TYPE_DECL representing
17464 the result of using the constrained type specifier in the
17465 current context. DECL refers to a concept if
17467 - it is an overload set containing a function concept taking a single
17468 type argument, or
17470 - it is a variable concept taking a single type argument. */
17472 static tree
17473 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17475 if (flag_concepts
17476 && (TREE_CODE (decl) == OVERLOAD
17477 || BASELINK_P (decl)
17478 || variable_concept_p (decl)))
17479 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17480 else
17481 return NULL_TREE;
17484 /* Check if DECL and ARGS form a partial-concept-id. If so,
17485 assign ID to the resulting constrained placeholder.
17487 Returns true if the partial-concept-id designates a placeholder
17488 and false otherwise. Note that *id is set to NULL_TREE in
17489 this case. */
17491 static tree
17492 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17494 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17497 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17498 or a concept-name.
17500 enum-name:
17501 identifier
17503 typedef-name:
17504 identifier
17506 concept-name:
17507 identifier
17509 Returns a TYPE_DECL for the type. */
17511 static tree
17512 cp_parser_nonclass_name (cp_parser* parser)
17514 tree type_decl;
17515 tree identifier;
17517 cp_token *token = cp_lexer_peek_token (parser->lexer);
17518 identifier = cp_parser_identifier (parser);
17519 if (identifier == error_mark_node)
17520 return error_mark_node;
17522 /* Look up the type-name. */
17523 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17525 type_decl = strip_using_decl (type_decl);
17527 /* If we found an overload set, then it may refer to a concept-name. */
17528 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17529 type_decl = decl;
17531 if (TREE_CODE (type_decl) != TYPE_DECL
17532 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17534 /* See if this is an Objective-C type. */
17535 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17536 tree type = objc_get_protocol_qualified_type (identifier, protos);
17537 if (type)
17538 type_decl = TYPE_NAME (type);
17541 /* Issue an error if we did not find a type-name. */
17542 if (TREE_CODE (type_decl) != TYPE_DECL
17543 /* In Objective-C, we have the complication that class names are
17544 normally type names and start declarations (eg, the
17545 "NSObject" in "NSObject *object;"), but can be used in an
17546 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17547 is an expression. So, a classname followed by a dot is not a
17548 valid type-name. */
17549 || (objc_is_class_name (TREE_TYPE (type_decl))
17550 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17552 if (!cp_parser_simulate_error (parser))
17553 cp_parser_name_lookup_error (parser, identifier, type_decl,
17554 NLE_TYPE, token->location);
17555 return error_mark_node;
17557 /* Remember that the name was used in the definition of the
17558 current class so that we can check later to see if the
17559 meaning would have been different after the class was
17560 entirely defined. */
17561 else if (type_decl != error_mark_node
17562 && !parser->scope)
17563 maybe_note_name_used_in_class (identifier, type_decl);
17565 return type_decl;
17568 /* Parse an elaborated-type-specifier. Note that the grammar given
17569 here incorporates the resolution to DR68.
17571 elaborated-type-specifier:
17572 class-key :: [opt] nested-name-specifier [opt] identifier
17573 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17574 enum-key :: [opt] nested-name-specifier [opt] identifier
17575 typename :: [opt] nested-name-specifier identifier
17576 typename :: [opt] nested-name-specifier template [opt]
17577 template-id
17579 GNU extension:
17581 elaborated-type-specifier:
17582 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17583 class-key attributes :: [opt] nested-name-specifier [opt]
17584 template [opt] template-id
17585 enum attributes :: [opt] nested-name-specifier [opt] identifier
17587 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17588 declared `friend'. If IS_DECLARATION is TRUE, then this
17589 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17590 something is being declared.
17592 Returns the TYPE specified. */
17594 static tree
17595 cp_parser_elaborated_type_specifier (cp_parser* parser,
17596 bool is_friend,
17597 bool is_declaration)
17599 enum tag_types tag_type;
17600 tree identifier;
17601 tree type = NULL_TREE;
17602 tree attributes = NULL_TREE;
17603 tree globalscope;
17604 cp_token *token = NULL;
17606 /* See if we're looking at the `enum' keyword. */
17607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17609 /* Consume the `enum' token. */
17610 cp_lexer_consume_token (parser->lexer);
17611 /* Remember that it's an enumeration type. */
17612 tag_type = enum_type;
17613 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17614 enums) is used here. */
17615 cp_token *token = cp_lexer_peek_token (parser->lexer);
17616 if (cp_parser_is_keyword (token, RID_CLASS)
17617 || cp_parser_is_keyword (token, RID_STRUCT))
17619 gcc_rich_location richloc (token->location);
17620 richloc.add_range (input_location, false);
17621 richloc.add_fixit_remove ();
17622 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17623 "a scoped enum must not use the %qD keyword",
17624 token->u.value);
17625 /* Consume the `struct' or `class' and parse it anyway. */
17626 cp_lexer_consume_token (parser->lexer);
17628 /* Parse the attributes. */
17629 attributes = cp_parser_attributes_opt (parser);
17631 /* Or, it might be `typename'. */
17632 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17633 RID_TYPENAME))
17635 /* Consume the `typename' token. */
17636 cp_lexer_consume_token (parser->lexer);
17637 /* Remember that it's a `typename' type. */
17638 tag_type = typename_type;
17640 /* Otherwise it must be a class-key. */
17641 else
17643 tag_type = cp_parser_class_key (parser);
17644 if (tag_type == none_type)
17645 return error_mark_node;
17646 /* Parse the attributes. */
17647 attributes = cp_parser_attributes_opt (parser);
17650 /* Look for the `::' operator. */
17651 globalscope = cp_parser_global_scope_opt (parser,
17652 /*current_scope_valid_p=*/false);
17653 /* Look for the nested-name-specifier. */
17654 tree nested_name_specifier;
17655 if (tag_type == typename_type && !globalscope)
17657 nested_name_specifier
17658 = cp_parser_nested_name_specifier (parser,
17659 /*typename_keyword_p=*/true,
17660 /*check_dependency_p=*/true,
17661 /*type_p=*/true,
17662 is_declaration);
17663 if (!nested_name_specifier)
17664 return error_mark_node;
17666 else
17667 /* Even though `typename' is not present, the proposed resolution
17668 to Core Issue 180 says that in `class A<T>::B', `B' should be
17669 considered a type-name, even if `A<T>' is dependent. */
17670 nested_name_specifier
17671 = cp_parser_nested_name_specifier_opt (parser,
17672 /*typename_keyword_p=*/true,
17673 /*check_dependency_p=*/true,
17674 /*type_p=*/true,
17675 is_declaration);
17676 /* For everything but enumeration types, consider a template-id.
17677 For an enumeration type, consider only a plain identifier. */
17678 if (tag_type != enum_type)
17680 bool template_p = false;
17681 tree decl;
17683 /* Allow the `template' keyword. */
17684 template_p = cp_parser_optional_template_keyword (parser);
17685 /* If we didn't see `template', we don't know if there's a
17686 template-id or not. */
17687 if (!template_p)
17688 cp_parser_parse_tentatively (parser);
17689 /* Parse the template-id. */
17690 token = cp_lexer_peek_token (parser->lexer);
17691 decl = cp_parser_template_id (parser, template_p,
17692 /*check_dependency_p=*/true,
17693 tag_type,
17694 is_declaration);
17695 /* If we didn't find a template-id, look for an ordinary
17696 identifier. */
17697 if (!template_p && !cp_parser_parse_definitely (parser))
17699 /* We can get here when cp_parser_template_id, called by
17700 cp_parser_class_name with tag_type == none_type, succeeds
17701 and caches a BASELINK. Then, when called again here,
17702 instead of failing and returning an error_mark_node
17703 returns it (see template/typename17.C in C++11).
17704 ??? Could we diagnose this earlier? */
17705 else if (tag_type == typename_type && BASELINK_P (decl))
17707 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17708 type = error_mark_node;
17710 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17711 in effect, then we must assume that, upon instantiation, the
17712 template will correspond to a class. */
17713 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17714 && tag_type == typename_type)
17715 type = make_typename_type (parser->scope, decl,
17716 typename_type,
17717 /*complain=*/tf_error);
17718 /* If the `typename' keyword is in effect and DECL is not a type
17719 decl, then type is non existent. */
17720 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17722 else if (TREE_CODE (decl) == TYPE_DECL)
17724 type = check_elaborated_type_specifier (tag_type, decl,
17725 /*allow_template_p=*/true);
17727 /* If the next token is a semicolon, this must be a specialization,
17728 instantiation, or friend declaration. Check the scope while we
17729 still know whether or not we had a nested-name-specifier. */
17730 if (type != error_mark_node
17731 && !nested_name_specifier && !is_friend
17732 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17733 check_unqualified_spec_or_inst (type, token->location);
17735 else if (decl == error_mark_node)
17736 type = error_mark_node;
17739 if (!type)
17741 token = cp_lexer_peek_token (parser->lexer);
17742 identifier = cp_parser_identifier (parser);
17744 if (identifier == error_mark_node)
17746 parser->scope = NULL_TREE;
17747 return error_mark_node;
17750 /* For a `typename', we needn't call xref_tag. */
17751 if (tag_type == typename_type
17752 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17753 return cp_parser_make_typename_type (parser, identifier,
17754 token->location);
17756 /* Template parameter lists apply only if we are not within a
17757 function parameter list. */
17758 bool template_parm_lists_apply
17759 = parser->num_template_parameter_lists;
17760 if (template_parm_lists_apply)
17761 for (cp_binding_level *s = current_binding_level;
17762 s && s->kind != sk_template_parms;
17763 s = s->level_chain)
17764 if (s->kind == sk_function_parms)
17765 template_parm_lists_apply = false;
17767 /* Look up a qualified name in the usual way. */
17768 if (parser->scope)
17770 tree decl;
17771 tree ambiguous_decls;
17773 decl = cp_parser_lookup_name (parser, identifier,
17774 tag_type,
17775 /*is_template=*/false,
17776 /*is_namespace=*/false,
17777 /*check_dependency=*/true,
17778 &ambiguous_decls,
17779 token->location);
17781 /* If the lookup was ambiguous, an error will already have been
17782 issued. */
17783 if (ambiguous_decls)
17784 return error_mark_node;
17786 /* If we are parsing friend declaration, DECL may be a
17787 TEMPLATE_DECL tree node here. However, we need to check
17788 whether this TEMPLATE_DECL results in valid code. Consider
17789 the following example:
17791 namespace N {
17792 template <class T> class C {};
17794 class X {
17795 template <class T> friend class N::C; // #1, valid code
17797 template <class T> class Y {
17798 friend class N::C; // #2, invalid code
17801 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17802 name lookup of `N::C'. We see that friend declaration must
17803 be template for the code to be valid. Note that
17804 processing_template_decl does not work here since it is
17805 always 1 for the above two cases. */
17807 decl = (cp_parser_maybe_treat_template_as_class
17808 (decl, /*tag_name_p=*/is_friend
17809 && template_parm_lists_apply));
17811 if (TREE_CODE (decl) != TYPE_DECL)
17813 cp_parser_diagnose_invalid_type_name (parser,
17814 identifier,
17815 token->location);
17816 return error_mark_node;
17819 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17821 bool allow_template = (template_parm_lists_apply
17822 || DECL_SELF_REFERENCE_P (decl));
17823 type = check_elaborated_type_specifier (tag_type, decl,
17824 allow_template);
17826 if (type == error_mark_node)
17827 return error_mark_node;
17830 /* Forward declarations of nested types, such as
17832 class C1::C2;
17833 class C1::C2::C3;
17835 are invalid unless all components preceding the final '::'
17836 are complete. If all enclosing types are complete, these
17837 declarations become merely pointless.
17839 Invalid forward declarations of nested types are errors
17840 caught elsewhere in parsing. Those that are pointless arrive
17841 here. */
17843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17844 && !is_friend && !processing_explicit_instantiation)
17845 warning (0, "declaration %qD does not declare anything", decl);
17847 type = TREE_TYPE (decl);
17849 else
17851 /* An elaborated-type-specifier sometimes introduces a new type and
17852 sometimes names an existing type. Normally, the rule is that it
17853 introduces a new type only if there is not an existing type of
17854 the same name already in scope. For example, given:
17856 struct S {};
17857 void f() { struct S s; }
17859 the `struct S' in the body of `f' is the same `struct S' as in
17860 the global scope; the existing definition is used. However, if
17861 there were no global declaration, this would introduce a new
17862 local class named `S'.
17864 An exception to this rule applies to the following code:
17866 namespace N { struct S; }
17868 Here, the elaborated-type-specifier names a new type
17869 unconditionally; even if there is already an `S' in the
17870 containing scope this declaration names a new type.
17871 This exception only applies if the elaborated-type-specifier
17872 forms the complete declaration:
17874 [class.name]
17876 A declaration consisting solely of `class-key identifier ;' is
17877 either a redeclaration of the name in the current scope or a
17878 forward declaration of the identifier as a class name. It
17879 introduces the name into the current scope.
17881 We are in this situation precisely when the next token is a `;'.
17883 An exception to the exception is that a `friend' declaration does
17884 *not* name a new type; i.e., given:
17886 struct S { friend struct T; };
17888 `T' is not a new type in the scope of `S'.
17890 Also, `new struct S' or `sizeof (struct S)' never results in the
17891 definition of a new type; a new type can only be declared in a
17892 declaration context. */
17894 tag_scope ts;
17895 bool template_p;
17897 if (is_friend)
17898 /* Friends have special name lookup rules. */
17899 ts = ts_within_enclosing_non_class;
17900 else if (is_declaration
17901 && cp_lexer_next_token_is (parser->lexer,
17902 CPP_SEMICOLON))
17903 /* This is a `class-key identifier ;' */
17904 ts = ts_current;
17905 else
17906 ts = ts_global;
17908 template_p =
17909 (template_parm_lists_apply
17910 && (cp_parser_next_token_starts_class_definition_p (parser)
17911 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17912 /* An unqualified name was used to reference this type, so
17913 there were no qualifying templates. */
17914 if (template_parm_lists_apply
17915 && !cp_parser_check_template_parameters (parser,
17916 /*num_templates=*/0,
17917 /*template_id*/false,
17918 token->location,
17919 /*declarator=*/NULL))
17920 return error_mark_node;
17921 type = xref_tag (tag_type, identifier, ts, template_p);
17925 if (type == error_mark_node)
17926 return error_mark_node;
17928 /* Allow attributes on forward declarations of classes. */
17929 if (attributes)
17931 if (TREE_CODE (type) == TYPENAME_TYPE)
17932 warning (OPT_Wattributes,
17933 "attributes ignored on uninstantiated type");
17934 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17935 && ! processing_explicit_instantiation)
17936 warning (OPT_Wattributes,
17937 "attributes ignored on template instantiation");
17938 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17939 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17940 else
17941 warning (OPT_Wattributes,
17942 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17945 if (tag_type != enum_type)
17947 /* Indicate whether this class was declared as a `class' or as a
17948 `struct'. */
17949 if (CLASS_TYPE_P (type))
17950 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17951 cp_parser_check_class_key (tag_type, type);
17954 /* A "<" cannot follow an elaborated type specifier. If that
17955 happens, the user was probably trying to form a template-id. */
17956 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17957 token->location);
17959 return type;
17962 /* Parse an enum-specifier.
17964 enum-specifier:
17965 enum-head { enumerator-list [opt] }
17966 enum-head { enumerator-list , } [C++0x]
17968 enum-head:
17969 enum-key identifier [opt] enum-base [opt]
17970 enum-key nested-name-specifier identifier enum-base [opt]
17972 enum-key:
17973 enum
17974 enum class [C++0x]
17975 enum struct [C++0x]
17977 enum-base: [C++0x]
17978 : type-specifier-seq
17980 opaque-enum-specifier:
17981 enum-key identifier enum-base [opt] ;
17983 GNU Extensions:
17984 enum-key attributes[opt] identifier [opt] enum-base [opt]
17985 { enumerator-list [opt] }attributes[opt]
17986 enum-key attributes[opt] identifier [opt] enum-base [opt]
17987 { enumerator-list, }attributes[opt] [C++0x]
17989 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17990 if the token stream isn't an enum-specifier after all. */
17992 static tree
17993 cp_parser_enum_specifier (cp_parser* parser)
17995 tree identifier;
17996 tree type = NULL_TREE;
17997 tree prev_scope;
17998 tree nested_name_specifier = NULL_TREE;
17999 tree attributes;
18000 bool scoped_enum_p = false;
18001 bool has_underlying_type = false;
18002 bool nested_being_defined = false;
18003 bool new_value_list = false;
18004 bool is_new_type = false;
18005 bool is_unnamed = false;
18006 tree underlying_type = NULL_TREE;
18007 cp_token *type_start_token = NULL;
18008 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18010 parser->colon_corrects_to_scope_p = false;
18012 /* Parse tentatively so that we can back up if we don't find a
18013 enum-specifier. */
18014 cp_parser_parse_tentatively (parser);
18016 /* Caller guarantees that the current token is 'enum', an identifier
18017 possibly follows, and the token after that is an opening brace.
18018 If we don't have an identifier, fabricate an anonymous name for
18019 the enumeration being defined. */
18020 cp_lexer_consume_token (parser->lexer);
18022 /* Parse the "class" or "struct", which indicates a scoped
18023 enumeration type in C++0x. */
18024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18025 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18027 if (cxx_dialect < cxx11)
18028 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18030 /* Consume the `struct' or `class' token. */
18031 cp_lexer_consume_token (parser->lexer);
18033 scoped_enum_p = true;
18036 attributes = cp_parser_attributes_opt (parser);
18038 /* Clear the qualification. */
18039 parser->scope = NULL_TREE;
18040 parser->qualifying_scope = NULL_TREE;
18041 parser->object_scope = NULL_TREE;
18043 /* Figure out in what scope the declaration is being placed. */
18044 prev_scope = current_scope ();
18046 type_start_token = cp_lexer_peek_token (parser->lexer);
18048 push_deferring_access_checks (dk_no_check);
18049 nested_name_specifier
18050 = cp_parser_nested_name_specifier_opt (parser,
18051 /*typename_keyword_p=*/true,
18052 /*check_dependency_p=*/false,
18053 /*type_p=*/false,
18054 /*is_declaration=*/false);
18056 if (nested_name_specifier)
18058 tree name;
18060 identifier = cp_parser_identifier (parser);
18061 name = cp_parser_lookup_name (parser, identifier,
18062 enum_type,
18063 /*is_template=*/false,
18064 /*is_namespace=*/false,
18065 /*check_dependency=*/true,
18066 /*ambiguous_decls=*/NULL,
18067 input_location);
18068 if (name && name != error_mark_node)
18070 type = TREE_TYPE (name);
18071 if (TREE_CODE (type) == TYPENAME_TYPE)
18073 /* Are template enums allowed in ISO? */
18074 if (template_parm_scope_p ())
18075 pedwarn (type_start_token->location, OPT_Wpedantic,
18076 "%qD is an enumeration template", name);
18077 /* ignore a typename reference, for it will be solved by name
18078 in start_enum. */
18079 type = NULL_TREE;
18082 else if (nested_name_specifier == error_mark_node)
18083 /* We already issued an error. */;
18084 else
18086 error_at (type_start_token->location,
18087 "%qD does not name an enumeration in %qT",
18088 identifier, nested_name_specifier);
18089 nested_name_specifier = error_mark_node;
18092 else
18094 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18095 identifier = cp_parser_identifier (parser);
18096 else
18098 identifier = make_anon_name ();
18099 is_unnamed = true;
18100 if (scoped_enum_p)
18101 error_at (type_start_token->location,
18102 "unnamed scoped enum is not allowed");
18105 pop_deferring_access_checks ();
18107 /* Check for the `:' that denotes a specified underlying type in C++0x.
18108 Note that a ':' could also indicate a bitfield width, however. */
18109 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18111 cp_decl_specifier_seq type_specifiers;
18113 /* Consume the `:'. */
18114 cp_lexer_consume_token (parser->lexer);
18116 /* Parse the type-specifier-seq. */
18117 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18118 /*is_trailing_return=*/false,
18119 &type_specifiers);
18121 /* At this point this is surely not elaborated type specifier. */
18122 if (!cp_parser_parse_definitely (parser))
18123 return NULL_TREE;
18125 if (cxx_dialect < cxx11)
18126 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18128 has_underlying_type = true;
18130 /* If that didn't work, stop. */
18131 if (type_specifiers.type != error_mark_node)
18133 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18134 /*initialized=*/0, NULL);
18135 if (underlying_type == error_mark_node
18136 || check_for_bare_parameter_packs (underlying_type))
18137 underlying_type = NULL_TREE;
18141 /* Look for the `{' but don't consume it yet. */
18142 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18144 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18146 cp_parser_error (parser, "expected %<{%>");
18147 if (has_underlying_type)
18149 type = NULL_TREE;
18150 goto out;
18153 /* An opaque-enum-specifier must have a ';' here. */
18154 if ((scoped_enum_p || underlying_type)
18155 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18157 cp_parser_error (parser, "expected %<;%> or %<{%>");
18158 if (has_underlying_type)
18160 type = NULL_TREE;
18161 goto out;
18166 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18167 return NULL_TREE;
18169 if (nested_name_specifier)
18171 if (CLASS_TYPE_P (nested_name_specifier))
18173 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18174 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18175 push_scope (nested_name_specifier);
18177 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18179 push_nested_namespace (nested_name_specifier);
18183 /* Issue an error message if type-definitions are forbidden here. */
18184 if (!cp_parser_check_type_definition (parser))
18185 type = error_mark_node;
18186 else
18187 /* Create the new type. We do this before consuming the opening
18188 brace so the enum will be recorded as being on the line of its
18189 tag (or the 'enum' keyword, if there is no tag). */
18190 type = start_enum (identifier, type, underlying_type,
18191 attributes, scoped_enum_p, &is_new_type);
18193 /* If the next token is not '{' it is an opaque-enum-specifier or an
18194 elaborated-type-specifier. */
18195 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18197 timevar_push (TV_PARSE_ENUM);
18198 if (nested_name_specifier
18199 && nested_name_specifier != error_mark_node)
18201 /* The following catches invalid code such as:
18202 enum class S<int>::E { A, B, C }; */
18203 if (!processing_specialization
18204 && CLASS_TYPE_P (nested_name_specifier)
18205 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18206 error_at (type_start_token->location, "cannot add an enumerator "
18207 "list to a template instantiation");
18209 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18211 error_at (type_start_token->location,
18212 "%<%T::%E%> has not been declared",
18213 TYPE_CONTEXT (nested_name_specifier),
18214 nested_name_specifier);
18215 type = error_mark_node;
18217 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18218 && !CLASS_TYPE_P (nested_name_specifier))
18220 error_at (type_start_token->location, "nested name specifier "
18221 "%qT for enum declaration does not name a class "
18222 "or namespace", nested_name_specifier);
18223 type = error_mark_node;
18225 /* If that scope does not contain the scope in which the
18226 class was originally declared, the program is invalid. */
18227 else if (prev_scope && !is_ancestor (prev_scope,
18228 nested_name_specifier))
18230 if (at_namespace_scope_p ())
18231 error_at (type_start_token->location,
18232 "declaration of %qD in namespace %qD which does not "
18233 "enclose %qD",
18234 type, prev_scope, nested_name_specifier);
18235 else
18236 error_at (type_start_token->location,
18237 "declaration of %qD in %qD which does not "
18238 "enclose %qD",
18239 type, prev_scope, nested_name_specifier);
18240 type = error_mark_node;
18242 /* If that scope is the scope where the declaration is being placed
18243 the program is invalid. */
18244 else if (CLASS_TYPE_P (nested_name_specifier)
18245 && CLASS_TYPE_P (prev_scope)
18246 && same_type_p (nested_name_specifier, prev_scope))
18248 permerror (type_start_token->location,
18249 "extra qualification not allowed");
18250 nested_name_specifier = NULL_TREE;
18254 if (scoped_enum_p)
18255 begin_scope (sk_scoped_enum, type);
18257 /* Consume the opening brace. */
18258 matching_braces braces;
18259 braces.consume_open (parser);
18261 if (type == error_mark_node)
18262 ; /* Nothing to add */
18263 else if (OPAQUE_ENUM_P (type)
18264 || (cxx_dialect > cxx98 && processing_specialization))
18266 new_value_list = true;
18267 SET_OPAQUE_ENUM_P (type, false);
18268 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18270 else
18272 error_at (type_start_token->location,
18273 "multiple definition of %q#T", type);
18274 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18275 "previous definition here");
18276 type = error_mark_node;
18279 if (type == error_mark_node)
18280 cp_parser_skip_to_end_of_block_or_statement (parser);
18281 /* If the next token is not '}', then there are some enumerators. */
18282 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18284 if (is_unnamed && !scoped_enum_p)
18285 pedwarn (type_start_token->location, OPT_Wpedantic,
18286 "ISO C++ forbids empty unnamed enum");
18288 else
18289 cp_parser_enumerator_list (parser, type);
18291 /* Consume the final '}'. */
18292 braces.require_close (parser);
18294 if (scoped_enum_p)
18295 finish_scope ();
18296 timevar_pop (TV_PARSE_ENUM);
18298 else
18300 /* If a ';' follows, then it is an opaque-enum-specifier
18301 and additional restrictions apply. */
18302 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18304 if (is_unnamed)
18305 error_at (type_start_token->location,
18306 "opaque-enum-specifier without name");
18307 else if (nested_name_specifier)
18308 error_at (type_start_token->location,
18309 "opaque-enum-specifier must use a simple identifier");
18313 /* Look for trailing attributes to apply to this enumeration, and
18314 apply them if appropriate. */
18315 if (cp_parser_allow_gnu_extensions_p (parser))
18317 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18318 cplus_decl_attributes (&type,
18319 trailing_attr,
18320 (int) ATTR_FLAG_TYPE_IN_PLACE);
18323 /* Finish up the enumeration. */
18324 if (type != error_mark_node)
18326 if (new_value_list)
18327 finish_enum_value_list (type);
18328 if (is_new_type)
18329 finish_enum (type);
18332 if (nested_name_specifier)
18334 if (CLASS_TYPE_P (nested_name_specifier))
18336 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18337 pop_scope (nested_name_specifier);
18339 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18341 pop_nested_namespace (nested_name_specifier);
18344 out:
18345 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18346 return type;
18349 /* Parse an enumerator-list. The enumerators all have the indicated
18350 TYPE.
18352 enumerator-list:
18353 enumerator-definition
18354 enumerator-list , enumerator-definition */
18356 static void
18357 cp_parser_enumerator_list (cp_parser* parser, tree type)
18359 while (true)
18361 /* Parse an enumerator-definition. */
18362 cp_parser_enumerator_definition (parser, type);
18364 /* If the next token is not a ',', we've reached the end of
18365 the list. */
18366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18367 break;
18368 /* Otherwise, consume the `,' and keep going. */
18369 cp_lexer_consume_token (parser->lexer);
18370 /* If the next token is a `}', there is a trailing comma. */
18371 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18373 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18374 pedwarn (input_location, OPT_Wpedantic,
18375 "comma at end of enumerator list");
18376 break;
18381 /* Parse an enumerator-definition. The enumerator has the indicated
18382 TYPE.
18384 enumerator-definition:
18385 enumerator
18386 enumerator = constant-expression
18388 enumerator:
18389 identifier
18391 GNU Extensions:
18393 enumerator-definition:
18394 enumerator attributes [opt]
18395 enumerator attributes [opt] = constant-expression */
18397 static void
18398 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18400 tree identifier;
18401 tree value;
18402 location_t loc;
18404 /* Save the input location because we are interested in the location
18405 of the identifier and not the location of the explicit value. */
18406 loc = cp_lexer_peek_token (parser->lexer)->location;
18408 /* Look for the identifier. */
18409 identifier = cp_parser_identifier (parser);
18410 if (identifier == error_mark_node)
18411 return;
18413 /* Parse any specified attributes. */
18414 tree attrs = cp_parser_attributes_opt (parser);
18416 /* If the next token is an '=', then there is an explicit value. */
18417 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18419 /* Consume the `=' token. */
18420 cp_lexer_consume_token (parser->lexer);
18421 /* Parse the value. */
18422 value = cp_parser_constant_expression (parser);
18424 else
18425 value = NULL_TREE;
18427 /* If we are processing a template, make sure the initializer of the
18428 enumerator doesn't contain any bare template parameter pack. */
18429 if (check_for_bare_parameter_packs (value))
18430 value = error_mark_node;
18432 /* Create the enumerator. */
18433 build_enumerator (identifier, value, type, attrs, loc);
18436 /* Parse a namespace-name.
18438 namespace-name:
18439 original-namespace-name
18440 namespace-alias
18442 Returns the NAMESPACE_DECL for the namespace. */
18444 static tree
18445 cp_parser_namespace_name (cp_parser* parser)
18447 tree identifier;
18448 tree namespace_decl;
18450 cp_token *token = cp_lexer_peek_token (parser->lexer);
18452 /* Get the name of the namespace. */
18453 identifier = cp_parser_identifier (parser);
18454 if (identifier == error_mark_node)
18455 return error_mark_node;
18457 /* Look up the identifier in the currently active scope. Look only
18458 for namespaces, due to:
18460 [basic.lookup.udir]
18462 When looking up a namespace-name in a using-directive or alias
18463 definition, only namespace names are considered.
18465 And:
18467 [basic.lookup.qual]
18469 During the lookup of a name preceding the :: scope resolution
18470 operator, object, function, and enumerator names are ignored.
18472 (Note that cp_parser_qualifying_entity only calls this
18473 function if the token after the name is the scope resolution
18474 operator.) */
18475 namespace_decl = cp_parser_lookup_name (parser, identifier,
18476 none_type,
18477 /*is_template=*/false,
18478 /*is_namespace=*/true,
18479 /*check_dependency=*/true,
18480 /*ambiguous_decls=*/NULL,
18481 token->location);
18482 /* If it's not a namespace, issue an error. */
18483 if (namespace_decl == error_mark_node
18484 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18486 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18488 error_at (token->location, "%qD is not a namespace-name", identifier);
18489 if (namespace_decl == error_mark_node
18490 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18491 suggest_alternative_in_explicit_scope (token->location, identifier,
18492 parser->scope);
18494 cp_parser_error (parser, "expected namespace-name");
18495 namespace_decl = error_mark_node;
18498 return namespace_decl;
18501 /* Parse a namespace-definition.
18503 namespace-definition:
18504 named-namespace-definition
18505 unnamed-namespace-definition
18507 named-namespace-definition:
18508 original-namespace-definition
18509 extension-namespace-definition
18511 original-namespace-definition:
18512 namespace identifier { namespace-body }
18514 extension-namespace-definition:
18515 namespace original-namespace-name { namespace-body }
18517 unnamed-namespace-definition:
18518 namespace { namespace-body } */
18520 static void
18521 cp_parser_namespace_definition (cp_parser* parser)
18523 tree identifier;
18524 int nested_definition_count = 0;
18526 cp_ensure_no_omp_declare_simd (parser);
18527 cp_ensure_no_oacc_routine (parser);
18529 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18531 if (is_inline)
18533 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18534 cp_lexer_consume_token (parser->lexer);
18537 /* Look for the `namespace' keyword. */
18538 cp_token* token
18539 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18541 /* Parse any specified attributes before the identifier. */
18542 tree attribs = cp_parser_attributes_opt (parser);
18544 for (;;)
18546 identifier = NULL_TREE;
18548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18550 identifier = cp_parser_identifier (parser);
18552 /* Parse any attributes specified after the identifier. */
18553 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18557 break;
18559 if (!nested_definition_count && cxx_dialect < cxx17)
18560 pedwarn (input_location, OPT_Wpedantic,
18561 "nested namespace definitions only available with "
18562 "-std=c++17 or -std=gnu++17");
18564 /* Nested namespace names can create new namespaces (unlike
18565 other qualified-ids). */
18566 if (int count = identifier ? push_namespace (identifier) : 0)
18567 nested_definition_count += count;
18568 else
18569 cp_parser_error (parser, "nested namespace name required");
18570 cp_lexer_consume_token (parser->lexer);
18573 if (nested_definition_count && !identifier)
18574 cp_parser_error (parser, "namespace name required");
18576 if (nested_definition_count && attribs)
18577 error_at (token->location,
18578 "a nested namespace definition cannot have attributes");
18579 if (nested_definition_count && is_inline)
18580 error_at (token->location,
18581 "a nested namespace definition cannot be inline");
18583 /* Start the namespace. */
18584 nested_definition_count += push_namespace (identifier, is_inline);
18586 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18588 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18590 /* Look for the `{' to validate starting the namespace. */
18591 matching_braces braces;
18592 if (braces.require_open (parser))
18594 /* Parse the body of the namespace. */
18595 cp_parser_namespace_body (parser);
18597 /* Look for the final `}'. */
18598 braces.require_close (parser);
18601 if (has_visibility)
18602 pop_visibility (1);
18604 /* Pop the nested namespace definitions. */
18605 while (nested_definition_count--)
18606 pop_namespace ();
18609 /* Parse a namespace-body.
18611 namespace-body:
18612 declaration-seq [opt] */
18614 static void
18615 cp_parser_namespace_body (cp_parser* parser)
18617 cp_parser_declaration_seq_opt (parser);
18620 /* Parse a namespace-alias-definition.
18622 namespace-alias-definition:
18623 namespace identifier = qualified-namespace-specifier ; */
18625 static void
18626 cp_parser_namespace_alias_definition (cp_parser* parser)
18628 tree identifier;
18629 tree namespace_specifier;
18631 cp_token *token = cp_lexer_peek_token (parser->lexer);
18633 /* Look for the `namespace' keyword. */
18634 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18635 /* Look for the identifier. */
18636 identifier = cp_parser_identifier (parser);
18637 if (identifier == error_mark_node)
18638 return;
18639 /* Look for the `=' token. */
18640 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18641 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18643 error_at (token->location, "%<namespace%> definition is not allowed here");
18644 /* Skip the definition. */
18645 cp_lexer_consume_token (parser->lexer);
18646 if (cp_parser_skip_to_closing_brace (parser))
18647 cp_lexer_consume_token (parser->lexer);
18648 return;
18650 cp_parser_require (parser, CPP_EQ, RT_EQ);
18651 /* Look for the qualified-namespace-specifier. */
18652 namespace_specifier
18653 = cp_parser_qualified_namespace_specifier (parser);
18654 /* Look for the `;' token. */
18655 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18657 /* Register the alias in the symbol table. */
18658 do_namespace_alias (identifier, namespace_specifier);
18661 /* Parse a qualified-namespace-specifier.
18663 qualified-namespace-specifier:
18664 :: [opt] nested-name-specifier [opt] namespace-name
18666 Returns a NAMESPACE_DECL corresponding to the specified
18667 namespace. */
18669 static tree
18670 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18672 /* Look for the optional `::'. */
18673 cp_parser_global_scope_opt (parser,
18674 /*current_scope_valid_p=*/false);
18676 /* Look for the optional nested-name-specifier. */
18677 cp_parser_nested_name_specifier_opt (parser,
18678 /*typename_keyword_p=*/false,
18679 /*check_dependency_p=*/true,
18680 /*type_p=*/false,
18681 /*is_declaration=*/true);
18683 return cp_parser_namespace_name (parser);
18686 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18687 access declaration.
18689 using-declaration:
18690 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18691 using :: unqualified-id ;
18693 access-declaration:
18694 qualified-id ;
18698 static bool
18699 cp_parser_using_declaration (cp_parser* parser,
18700 bool access_declaration_p)
18702 cp_token *token;
18703 bool typename_p = false;
18704 bool global_scope_p;
18705 tree decl;
18706 tree identifier;
18707 tree qscope;
18708 int oldcount = errorcount;
18709 cp_token *diag_token = NULL;
18711 if (access_declaration_p)
18713 diag_token = cp_lexer_peek_token (parser->lexer);
18714 cp_parser_parse_tentatively (parser);
18716 else
18718 /* Look for the `using' keyword. */
18719 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18721 again:
18722 /* Peek at the next token. */
18723 token = cp_lexer_peek_token (parser->lexer);
18724 /* See if it's `typename'. */
18725 if (token->keyword == RID_TYPENAME)
18727 /* Remember that we've seen it. */
18728 typename_p = true;
18729 /* Consume the `typename' token. */
18730 cp_lexer_consume_token (parser->lexer);
18734 /* Look for the optional global scope qualification. */
18735 global_scope_p
18736 = (cp_parser_global_scope_opt (parser,
18737 /*current_scope_valid_p=*/false)
18738 != NULL_TREE);
18740 /* If we saw `typename', or didn't see `::', then there must be a
18741 nested-name-specifier present. */
18742 if (typename_p || !global_scope_p)
18744 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18745 /*check_dependency_p=*/true,
18746 /*type_p=*/false,
18747 /*is_declaration=*/true);
18748 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18750 cp_parser_skip_to_end_of_block_or_statement (parser);
18751 return false;
18754 /* Otherwise, we could be in either of the two productions. In that
18755 case, treat the nested-name-specifier as optional. */
18756 else
18757 qscope = cp_parser_nested_name_specifier_opt (parser,
18758 /*typename_keyword_p=*/false,
18759 /*check_dependency_p=*/true,
18760 /*type_p=*/false,
18761 /*is_declaration=*/true);
18762 if (!qscope)
18763 qscope = global_namespace;
18764 else if (UNSCOPED_ENUM_P (qscope))
18765 qscope = CP_TYPE_CONTEXT (qscope);
18767 if (access_declaration_p && cp_parser_error_occurred (parser))
18768 /* Something has already gone wrong; there's no need to parse
18769 further. Since an error has occurred, the return value of
18770 cp_parser_parse_definitely will be false, as required. */
18771 return cp_parser_parse_definitely (parser);
18773 token = cp_lexer_peek_token (parser->lexer);
18774 /* Parse the unqualified-id. */
18775 identifier = cp_parser_unqualified_id (parser,
18776 /*template_keyword_p=*/false,
18777 /*check_dependency_p=*/true,
18778 /*declarator_p=*/true,
18779 /*optional_p=*/false);
18781 if (access_declaration_p)
18783 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18784 cp_parser_simulate_error (parser);
18785 if (!cp_parser_parse_definitely (parser))
18786 return false;
18788 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18790 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18791 if (cxx_dialect < cxx17
18792 && !in_system_header_at (ell->location))
18793 pedwarn (ell->location, 0,
18794 "pack expansion in using-declaration only available "
18795 "with -std=c++17 or -std=gnu++17");
18796 qscope = make_pack_expansion (qscope);
18799 /* The function we call to handle a using-declaration is different
18800 depending on what scope we are in. */
18801 if (qscope == error_mark_node || identifier == error_mark_node)
18803 else if (!identifier_p (identifier)
18804 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18805 /* [namespace.udecl]
18807 A using declaration shall not name a template-id. */
18808 error_at (token->location,
18809 "a template-id may not appear in a using-declaration");
18810 else
18812 if (at_class_scope_p ())
18814 /* Create the USING_DECL. */
18815 decl = do_class_using_decl (qscope, identifier);
18817 if (decl && typename_p)
18818 USING_DECL_TYPENAME_P (decl) = 1;
18820 if (check_for_bare_parameter_packs (decl))
18822 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18823 return false;
18825 else
18826 /* Add it to the list of members in this class. */
18827 finish_member_declaration (decl);
18829 else
18831 decl = cp_parser_lookup_name_simple (parser,
18832 identifier,
18833 token->location);
18834 if (decl == error_mark_node)
18835 cp_parser_name_lookup_error (parser, identifier,
18836 decl, NLE_NULL,
18837 token->location);
18838 else if (check_for_bare_parameter_packs (decl))
18840 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18841 return false;
18843 else if (!at_namespace_scope_p ())
18844 finish_local_using_decl (decl, qscope, identifier);
18845 else
18846 finish_namespace_using_decl (decl, qscope, identifier);
18850 if (!access_declaration_p
18851 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18853 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18854 if (cxx_dialect < cxx17)
18855 pedwarn (comma->location, 0,
18856 "comma-separated list in using-declaration only available "
18857 "with -std=c++17 or -std=gnu++17");
18858 goto again;
18861 /* Look for the final `;'. */
18862 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18864 if (access_declaration_p && errorcount == oldcount)
18865 warning_at (diag_token->location, OPT_Wdeprecated,
18866 "access declarations are deprecated "
18867 "in favour of using-declarations; "
18868 "suggestion: add the %<using%> keyword");
18870 return true;
18873 /* Parse an alias-declaration.
18875 alias-declaration:
18876 using identifier attribute-specifier-seq [opt] = type-id */
18878 static tree
18879 cp_parser_alias_declaration (cp_parser* parser)
18881 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18882 location_t id_location;
18883 cp_declarator *declarator;
18884 cp_decl_specifier_seq decl_specs;
18885 bool member_p;
18886 const char *saved_message = NULL;
18888 /* Look for the `using' keyword. */
18889 cp_token *using_token
18890 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18891 if (using_token == NULL)
18892 return error_mark_node;
18894 id_location = cp_lexer_peek_token (parser->lexer)->location;
18895 id = cp_parser_identifier (parser);
18896 if (id == error_mark_node)
18897 return error_mark_node;
18899 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18900 attributes = cp_parser_attributes_opt (parser);
18901 if (attributes == error_mark_node)
18902 return error_mark_node;
18904 cp_parser_require (parser, CPP_EQ, RT_EQ);
18906 if (cp_parser_error_occurred (parser))
18907 return error_mark_node;
18909 cp_parser_commit_to_tentative_parse (parser);
18911 /* Now we are going to parse the type-id of the declaration. */
18914 [dcl.type]/3 says:
18916 "A type-specifier-seq shall not define a class or enumeration
18917 unless it appears in the type-id of an alias-declaration (7.1.3) that
18918 is not the declaration of a template-declaration."
18920 In other words, if we currently are in an alias template, the
18921 type-id should not define a type.
18923 So let's set parser->type_definition_forbidden_message in that
18924 case; cp_parser_check_type_definition (called by
18925 cp_parser_class_specifier) will then emit an error if a type is
18926 defined in the type-id. */
18927 if (parser->num_template_parameter_lists)
18929 saved_message = parser->type_definition_forbidden_message;
18930 parser->type_definition_forbidden_message =
18931 G_("types may not be defined in alias template declarations");
18934 type = cp_parser_type_id (parser);
18936 /* Restore the error message if need be. */
18937 if (parser->num_template_parameter_lists)
18938 parser->type_definition_forbidden_message = saved_message;
18940 if (type == error_mark_node
18941 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18943 cp_parser_skip_to_end_of_block_or_statement (parser);
18944 return error_mark_node;
18947 /* A typedef-name can also be introduced by an alias-declaration. The
18948 identifier following the using keyword becomes a typedef-name. It has
18949 the same semantics as if it were introduced by the typedef
18950 specifier. In particular, it does not define a new type and it shall
18951 not appear in the type-id. */
18953 clear_decl_specs (&decl_specs);
18954 decl_specs.type = type;
18955 if (attributes != NULL_TREE)
18957 decl_specs.attributes = attributes;
18958 set_and_check_decl_spec_loc (&decl_specs,
18959 ds_attribute,
18960 attrs_token);
18962 set_and_check_decl_spec_loc (&decl_specs,
18963 ds_typedef,
18964 using_token);
18965 set_and_check_decl_spec_loc (&decl_specs,
18966 ds_alias,
18967 using_token);
18969 if (parser->num_template_parameter_lists
18970 && !cp_parser_check_template_parameters (parser,
18971 /*num_templates=*/0,
18972 /*template_id*/false,
18973 id_location,
18974 /*declarator=*/NULL))
18975 return error_mark_node;
18977 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18978 declarator->id_loc = id_location;
18980 member_p = at_class_scope_p ();
18981 if (member_p)
18982 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18983 NULL_TREE, attributes);
18984 else
18985 decl = start_decl (declarator, &decl_specs, 0,
18986 attributes, NULL_TREE, &pushed_scope);
18987 if (decl == error_mark_node)
18988 return decl;
18990 // Attach constraints to the alias declaration.
18991 if (flag_concepts && current_template_parms)
18993 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18994 tree constr = build_constraints (reqs, NULL_TREE);
18995 set_constraints (decl, constr);
18998 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19000 if (pushed_scope)
19001 pop_scope (pushed_scope);
19003 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19004 added into the symbol table; otherwise, return the TYPE_DECL. */
19005 if (DECL_LANG_SPECIFIC (decl)
19006 && DECL_TEMPLATE_INFO (decl)
19007 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19009 decl = DECL_TI_TEMPLATE (decl);
19010 if (member_p)
19011 check_member_template (decl);
19014 return decl;
19017 /* Parse a using-directive.
19019 using-directive:
19020 using namespace :: [opt] nested-name-specifier [opt]
19021 namespace-name ; */
19023 static void
19024 cp_parser_using_directive (cp_parser* parser)
19026 tree namespace_decl;
19027 tree attribs;
19029 /* Look for the `using' keyword. */
19030 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19031 /* And the `namespace' keyword. */
19032 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19033 /* Look for the optional `::' operator. */
19034 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19035 /* And the optional nested-name-specifier. */
19036 cp_parser_nested_name_specifier_opt (parser,
19037 /*typename_keyword_p=*/false,
19038 /*check_dependency_p=*/true,
19039 /*type_p=*/false,
19040 /*is_declaration=*/true);
19041 /* Get the namespace being used. */
19042 namespace_decl = cp_parser_namespace_name (parser);
19043 /* And any specified attributes. */
19044 attribs = cp_parser_attributes_opt (parser);
19046 /* Update the symbol table. */
19047 if (namespace_bindings_p ())
19048 finish_namespace_using_directive (namespace_decl, attribs);
19049 else
19050 finish_local_using_directive (namespace_decl, attribs);
19052 /* Look for the final `;'. */
19053 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19056 /* Parse an asm-definition.
19058 asm-definition:
19059 asm ( string-literal ) ;
19061 GNU Extension:
19063 asm-definition:
19064 asm volatile [opt] ( string-literal ) ;
19065 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19066 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19067 : asm-operand-list [opt] ) ;
19068 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19069 : asm-operand-list [opt]
19070 : asm-clobber-list [opt] ) ;
19071 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19072 : asm-clobber-list [opt]
19073 : asm-goto-list ) ; */
19075 static void
19076 cp_parser_asm_definition (cp_parser* parser)
19078 tree string;
19079 tree outputs = NULL_TREE;
19080 tree inputs = NULL_TREE;
19081 tree clobbers = NULL_TREE;
19082 tree labels = NULL_TREE;
19083 tree asm_stmt;
19084 bool volatile_p = false;
19085 bool extended_p = false;
19086 bool invalid_inputs_p = false;
19087 bool invalid_outputs_p = false;
19088 bool goto_p = false;
19089 required_token missing = RT_NONE;
19091 /* Look for the `asm' keyword. */
19092 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19094 if (parser->in_function_body
19095 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19097 error ("%<asm%> in %<constexpr%> function");
19098 cp_function_chain->invalid_constexpr = true;
19101 /* See if the next token is `volatile'. */
19102 if (cp_parser_allow_gnu_extensions_p (parser)
19103 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19105 /* Remember that we saw the `volatile' keyword. */
19106 volatile_p = true;
19107 /* Consume the token. */
19108 cp_lexer_consume_token (parser->lexer);
19110 if (cp_parser_allow_gnu_extensions_p (parser)
19111 && parser->in_function_body
19112 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19114 /* Remember that we saw the `goto' keyword. */
19115 goto_p = true;
19116 /* Consume the token. */
19117 cp_lexer_consume_token (parser->lexer);
19119 /* Look for the opening `('. */
19120 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19121 return;
19122 /* Look for the string. */
19123 string = cp_parser_string_literal (parser, false, false);
19124 if (string == error_mark_node)
19126 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19127 /*consume_paren=*/true);
19128 return;
19131 /* If we're allowing GNU extensions, check for the extended assembly
19132 syntax. Unfortunately, the `:' tokens need not be separated by
19133 a space in C, and so, for compatibility, we tolerate that here
19134 too. Doing that means that we have to treat the `::' operator as
19135 two `:' tokens. */
19136 if (cp_parser_allow_gnu_extensions_p (parser)
19137 && parser->in_function_body
19138 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19139 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19141 bool inputs_p = false;
19142 bool clobbers_p = false;
19143 bool labels_p = false;
19145 /* The extended syntax was used. */
19146 extended_p = true;
19148 /* Look for outputs. */
19149 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19151 /* Consume the `:'. */
19152 cp_lexer_consume_token (parser->lexer);
19153 /* Parse the output-operands. */
19154 if (cp_lexer_next_token_is_not (parser->lexer,
19155 CPP_COLON)
19156 && cp_lexer_next_token_is_not (parser->lexer,
19157 CPP_SCOPE)
19158 && cp_lexer_next_token_is_not (parser->lexer,
19159 CPP_CLOSE_PAREN)
19160 && !goto_p)
19162 outputs = cp_parser_asm_operand_list (parser);
19163 if (outputs == error_mark_node)
19164 invalid_outputs_p = true;
19167 /* If the next token is `::', there are no outputs, and the
19168 next token is the beginning of the inputs. */
19169 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19170 /* The inputs are coming next. */
19171 inputs_p = true;
19173 /* Look for inputs. */
19174 if (inputs_p
19175 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19177 /* Consume the `:' or `::'. */
19178 cp_lexer_consume_token (parser->lexer);
19179 /* Parse the output-operands. */
19180 if (cp_lexer_next_token_is_not (parser->lexer,
19181 CPP_COLON)
19182 && cp_lexer_next_token_is_not (parser->lexer,
19183 CPP_SCOPE)
19184 && cp_lexer_next_token_is_not (parser->lexer,
19185 CPP_CLOSE_PAREN))
19187 inputs = cp_parser_asm_operand_list (parser);
19188 if (inputs == error_mark_node)
19189 invalid_inputs_p = true;
19192 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19193 /* The clobbers are coming next. */
19194 clobbers_p = true;
19196 /* Look for clobbers. */
19197 if (clobbers_p
19198 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19200 clobbers_p = true;
19201 /* Consume the `:' or `::'. */
19202 cp_lexer_consume_token (parser->lexer);
19203 /* Parse the clobbers. */
19204 if (cp_lexer_next_token_is_not (parser->lexer,
19205 CPP_COLON)
19206 && cp_lexer_next_token_is_not (parser->lexer,
19207 CPP_CLOSE_PAREN))
19208 clobbers = cp_parser_asm_clobber_list (parser);
19210 else if (goto_p
19211 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19212 /* The labels are coming next. */
19213 labels_p = true;
19215 /* Look for labels. */
19216 if (labels_p
19217 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19219 labels_p = true;
19220 /* Consume the `:' or `::'. */
19221 cp_lexer_consume_token (parser->lexer);
19222 /* Parse the labels. */
19223 labels = cp_parser_asm_label_list (parser);
19226 if (goto_p && !labels_p)
19227 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19229 else if (goto_p)
19230 missing = RT_COLON_SCOPE;
19232 /* Look for the closing `)'. */
19233 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19234 missing ? missing : RT_CLOSE_PAREN))
19235 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19236 /*consume_paren=*/true);
19237 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19239 if (!invalid_inputs_p && !invalid_outputs_p)
19241 /* Create the ASM_EXPR. */
19242 if (parser->in_function_body)
19244 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19245 inputs, clobbers, labels);
19246 /* If the extended syntax was not used, mark the ASM_EXPR. */
19247 if (!extended_p)
19249 tree temp = asm_stmt;
19250 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19251 temp = TREE_OPERAND (temp, 0);
19253 ASM_INPUT_P (temp) = 1;
19256 else
19257 symtab->finalize_toplevel_asm (string);
19261 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19262 type that comes from the decl-specifier-seq. */
19264 static tree
19265 strip_declarator_types (tree type, cp_declarator *declarator)
19267 for (cp_declarator *d = declarator; d;)
19268 switch (d->kind)
19270 case cdk_id:
19271 case cdk_decomp:
19272 case cdk_error:
19273 d = NULL;
19274 break;
19276 default:
19277 if (TYPE_PTRMEMFUNC_P (type))
19278 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19279 type = TREE_TYPE (type);
19280 d = d->declarator;
19281 break;
19284 return type;
19287 /* Declarators [gram.dcl.decl] */
19289 /* Parse an init-declarator.
19291 init-declarator:
19292 declarator initializer [opt]
19294 GNU Extension:
19296 init-declarator:
19297 declarator asm-specification [opt] attributes [opt] initializer [opt]
19299 function-definition:
19300 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19301 function-body
19302 decl-specifier-seq [opt] declarator function-try-block
19304 GNU Extension:
19306 function-definition:
19307 __extension__ function-definition
19309 TM Extension:
19311 function-definition:
19312 decl-specifier-seq [opt] declarator function-transaction-block
19314 The DECL_SPECIFIERS apply to this declarator. Returns a
19315 representation of the entity declared. If MEMBER_P is TRUE, then
19316 this declarator appears in a class scope. The new DECL created by
19317 this declarator is returned.
19319 The CHECKS are access checks that should be performed once we know
19320 what entity is being declared (and, therefore, what classes have
19321 befriended it).
19323 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19324 for a function-definition here as well. If the declarator is a
19325 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19326 be TRUE upon return. By that point, the function-definition will
19327 have been completely parsed.
19329 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19330 is FALSE.
19332 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19333 parsed declaration if it is an uninitialized single declarator not followed
19334 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19335 if present, will not be consumed. If returned, this declarator will be
19336 created with SD_INITIALIZED but will not call cp_finish_decl.
19338 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19339 and there is an initializer, the pointed location_t is set to the
19340 location of the '=' or `(', or '{' in C++11 token introducing the
19341 initializer. */
19343 static tree
19344 cp_parser_init_declarator (cp_parser* parser,
19345 cp_decl_specifier_seq *decl_specifiers,
19346 vec<deferred_access_check, va_gc> *checks,
19347 bool function_definition_allowed_p,
19348 bool member_p,
19349 int declares_class_or_enum,
19350 bool* function_definition_p,
19351 tree* maybe_range_for_decl,
19352 location_t* init_loc,
19353 tree* auto_result)
19355 cp_token *token = NULL, *asm_spec_start_token = NULL,
19356 *attributes_start_token = NULL;
19357 cp_declarator *declarator;
19358 tree prefix_attributes;
19359 tree attributes = NULL;
19360 tree asm_specification;
19361 tree initializer;
19362 tree decl = NULL_TREE;
19363 tree scope;
19364 int is_initialized;
19365 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19366 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19367 "(...)". */
19368 enum cpp_ttype initialization_kind;
19369 bool is_direct_init = false;
19370 bool is_non_constant_init;
19371 int ctor_dtor_or_conv_p;
19372 bool friend_p = cp_parser_friend_p (decl_specifiers);
19373 tree pushed_scope = NULL_TREE;
19374 bool range_for_decl_p = false;
19375 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19376 location_t tmp_init_loc = UNKNOWN_LOCATION;
19378 /* Gather the attributes that were provided with the
19379 decl-specifiers. */
19380 prefix_attributes = decl_specifiers->attributes;
19382 /* Assume that this is not the declarator for a function
19383 definition. */
19384 if (function_definition_p)
19385 *function_definition_p = false;
19387 /* Default arguments are only permitted for function parameters. */
19388 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19389 parser->default_arg_ok_p = false;
19391 /* Defer access checks while parsing the declarator; we cannot know
19392 what names are accessible until we know what is being
19393 declared. */
19394 resume_deferring_access_checks ();
19396 token = cp_lexer_peek_token (parser->lexer);
19398 /* Parse the declarator. */
19399 declarator
19400 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19401 &ctor_dtor_or_conv_p,
19402 /*parenthesized_p=*/NULL,
19403 member_p, friend_p);
19404 /* Gather up the deferred checks. */
19405 stop_deferring_access_checks ();
19407 parser->default_arg_ok_p = saved_default_arg_ok_p;
19409 /* If the DECLARATOR was erroneous, there's no need to go
19410 further. */
19411 if (declarator == cp_error_declarator)
19412 return error_mark_node;
19414 /* Check that the number of template-parameter-lists is OK. */
19415 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19416 token->location))
19417 return error_mark_node;
19419 if (declares_class_or_enum & 2)
19420 cp_parser_check_for_definition_in_return_type (declarator,
19421 decl_specifiers->type,
19422 decl_specifiers->locations[ds_type_spec]);
19424 /* Figure out what scope the entity declared by the DECLARATOR is
19425 located in. `grokdeclarator' sometimes changes the scope, so
19426 we compute it now. */
19427 scope = get_scope_of_declarator (declarator);
19429 /* Perform any lookups in the declared type which were thought to be
19430 dependent, but are not in the scope of the declarator. */
19431 decl_specifiers->type
19432 = maybe_update_decl_type (decl_specifiers->type, scope);
19434 /* If we're allowing GNU extensions, look for an
19435 asm-specification. */
19436 if (cp_parser_allow_gnu_extensions_p (parser))
19438 /* Look for an asm-specification. */
19439 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19440 asm_specification = cp_parser_asm_specification_opt (parser);
19442 else
19443 asm_specification = NULL_TREE;
19445 /* Look for attributes. */
19446 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19447 attributes = cp_parser_attributes_opt (parser);
19449 /* Peek at the next token. */
19450 token = cp_lexer_peek_token (parser->lexer);
19452 bool bogus_implicit_tmpl = false;
19454 if (function_declarator_p (declarator))
19456 /* Handle C++17 deduction guides. */
19457 if (!decl_specifiers->type
19458 && ctor_dtor_or_conv_p <= 0
19459 && cxx_dialect >= cxx17)
19461 cp_declarator *id = get_id_declarator (declarator);
19462 tree name = id->u.id.unqualified_name;
19463 parser->scope = id->u.id.qualifying_scope;
19464 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19465 if (tmpl
19466 && (DECL_CLASS_TEMPLATE_P (tmpl)
19467 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19469 id->u.id.unqualified_name = dguide_name (tmpl);
19470 id->u.id.sfk = sfk_deduction_guide;
19471 ctor_dtor_or_conv_p = 1;
19475 /* Check to see if the token indicates the start of a
19476 function-definition. */
19477 if (cp_parser_token_starts_function_definition_p (token))
19479 if (!function_definition_allowed_p)
19481 /* If a function-definition should not appear here, issue an
19482 error message. */
19483 cp_parser_error (parser,
19484 "a function-definition is not allowed here");
19485 return error_mark_node;
19488 location_t func_brace_location
19489 = cp_lexer_peek_token (parser->lexer)->location;
19491 /* Neither attributes nor an asm-specification are allowed
19492 on a function-definition. */
19493 if (asm_specification)
19494 error_at (asm_spec_start_token->location,
19495 "an asm-specification is not allowed "
19496 "on a function-definition");
19497 if (attributes)
19498 error_at (attributes_start_token->location,
19499 "attributes are not allowed "
19500 "on a function-definition");
19501 /* This is a function-definition. */
19502 *function_definition_p = true;
19504 /* Parse the function definition. */
19505 if (member_p)
19506 decl = cp_parser_save_member_function_body (parser,
19507 decl_specifiers,
19508 declarator,
19509 prefix_attributes);
19510 else
19511 decl =
19512 (cp_parser_function_definition_from_specifiers_and_declarator
19513 (parser, decl_specifiers, prefix_attributes, declarator));
19515 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19517 /* This is where the prologue starts... */
19518 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19519 = func_brace_location;
19522 return decl;
19525 else if (parser->fully_implicit_function_template_p)
19527 /* A non-template declaration involving a function parameter list
19528 containing an implicit template parameter will be made into a
19529 template. If the resulting declaration is not going to be an
19530 actual function then finish the template scope here to prevent it.
19531 An error message will be issued once we have a decl to talk about.
19533 FIXME probably we should do type deduction rather than create an
19534 implicit template, but the standard currently doesn't allow it. */
19535 bogus_implicit_tmpl = true;
19536 finish_fully_implicit_template (parser, NULL_TREE);
19539 /* [dcl.dcl]
19541 Only in function declarations for constructors, destructors, type
19542 conversions, and deduction guides can the decl-specifier-seq be omitted.
19544 We explicitly postpone this check past the point where we handle
19545 function-definitions because we tolerate function-definitions
19546 that are missing their return types in some modes. */
19547 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19549 cp_parser_error (parser,
19550 "expected constructor, destructor, or type conversion");
19551 return error_mark_node;
19554 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19555 if (token->type == CPP_EQ
19556 || token->type == CPP_OPEN_PAREN
19557 || token->type == CPP_OPEN_BRACE)
19559 is_initialized = SD_INITIALIZED;
19560 initialization_kind = token->type;
19561 if (maybe_range_for_decl)
19562 *maybe_range_for_decl = error_mark_node;
19563 tmp_init_loc = token->location;
19564 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19565 *init_loc = tmp_init_loc;
19567 if (token->type == CPP_EQ
19568 && function_declarator_p (declarator))
19570 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19571 if (t2->keyword == RID_DEFAULT)
19572 is_initialized = SD_DEFAULTED;
19573 else if (t2->keyword == RID_DELETE)
19574 is_initialized = SD_DELETED;
19577 else
19579 /* If the init-declarator isn't initialized and isn't followed by a
19580 `,' or `;', it's not a valid init-declarator. */
19581 if (token->type != CPP_COMMA
19582 && token->type != CPP_SEMICOLON)
19584 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19585 range_for_decl_p = true;
19586 else
19588 if (!maybe_range_for_decl)
19589 cp_parser_error (parser, "expected initializer");
19590 return error_mark_node;
19593 is_initialized = SD_UNINITIALIZED;
19594 initialization_kind = CPP_EOF;
19597 /* Because start_decl has side-effects, we should only call it if we
19598 know we're going ahead. By this point, we know that we cannot
19599 possibly be looking at any other construct. */
19600 cp_parser_commit_to_tentative_parse (parser);
19602 /* Enter the newly declared entry in the symbol table. If we're
19603 processing a declaration in a class-specifier, we wait until
19604 after processing the initializer. */
19605 if (!member_p)
19607 if (parser->in_unbraced_linkage_specification_p)
19608 decl_specifiers->storage_class = sc_extern;
19609 decl = start_decl (declarator, decl_specifiers,
19610 range_for_decl_p? SD_INITIALIZED : is_initialized,
19611 attributes, prefix_attributes, &pushed_scope);
19612 cp_finalize_omp_declare_simd (parser, decl);
19613 cp_finalize_oacc_routine (parser, decl, false);
19614 /* Adjust location of decl if declarator->id_loc is more appropriate:
19615 set, and decl wasn't merged with another decl, in which case its
19616 location would be different from input_location, and more accurate. */
19617 if (DECL_P (decl)
19618 && declarator->id_loc != UNKNOWN_LOCATION
19619 && DECL_SOURCE_LOCATION (decl) == input_location)
19620 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19622 else if (scope)
19623 /* Enter the SCOPE. That way unqualified names appearing in the
19624 initializer will be looked up in SCOPE. */
19625 pushed_scope = push_scope (scope);
19627 /* Perform deferred access control checks, now that we know in which
19628 SCOPE the declared entity resides. */
19629 if (!member_p && decl)
19631 tree saved_current_function_decl = NULL_TREE;
19633 /* If the entity being declared is a function, pretend that we
19634 are in its scope. If it is a `friend', it may have access to
19635 things that would not otherwise be accessible. */
19636 if (TREE_CODE (decl) == FUNCTION_DECL)
19638 saved_current_function_decl = current_function_decl;
19639 current_function_decl = decl;
19642 /* Perform access checks for template parameters. */
19643 cp_parser_perform_template_parameter_access_checks (checks);
19645 /* Perform the access control checks for the declarator and the
19646 decl-specifiers. */
19647 perform_deferred_access_checks (tf_warning_or_error);
19649 /* Restore the saved value. */
19650 if (TREE_CODE (decl) == FUNCTION_DECL)
19651 current_function_decl = saved_current_function_decl;
19654 /* Parse the initializer. */
19655 initializer = NULL_TREE;
19656 is_direct_init = false;
19657 is_non_constant_init = true;
19658 if (is_initialized)
19660 if (function_declarator_p (declarator))
19662 if (initialization_kind == CPP_EQ)
19663 initializer = cp_parser_pure_specifier (parser);
19664 else
19666 /* If the declaration was erroneous, we don't really
19667 know what the user intended, so just silently
19668 consume the initializer. */
19669 if (decl != error_mark_node)
19670 error_at (tmp_init_loc, "initializer provided for function");
19671 cp_parser_skip_to_closing_parenthesis (parser,
19672 /*recovering=*/true,
19673 /*or_comma=*/false,
19674 /*consume_paren=*/true);
19677 else
19679 /* We want to record the extra mangling scope for in-class
19680 initializers of class members and initializers of static data
19681 member templates. The former involves deferring
19682 parsing of the initializer until end of class as with default
19683 arguments. So right here we only handle the latter. */
19684 if (!member_p && processing_template_decl && decl != error_mark_node)
19685 start_lambda_scope (decl);
19686 initializer = cp_parser_initializer (parser,
19687 &is_direct_init,
19688 &is_non_constant_init);
19689 if (!member_p && processing_template_decl && decl != error_mark_node)
19690 finish_lambda_scope ();
19691 if (initializer == error_mark_node)
19692 cp_parser_skip_to_end_of_statement (parser);
19696 /* The old parser allows attributes to appear after a parenthesized
19697 initializer. Mark Mitchell proposed removing this functionality
19698 on the GCC mailing lists on 2002-08-13. This parser accepts the
19699 attributes -- but ignores them. Made a permerror in GCC 8. */
19700 if (cp_parser_allow_gnu_extensions_p (parser)
19701 && initialization_kind == CPP_OPEN_PAREN
19702 && cp_parser_attributes_opt (parser)
19703 && permerror (input_location,
19704 "attributes after parenthesized initializer ignored"))
19706 static bool hint;
19707 if (flag_permissive && !hint)
19709 hint = true;
19710 inform (input_location,
19711 "this flexibility is deprecated and will be removed");
19715 /* And now complain about a non-function implicit template. */
19716 if (bogus_implicit_tmpl && decl != error_mark_node)
19717 error_at (DECL_SOURCE_LOCATION (decl),
19718 "non-function %qD declared as implicit template", decl);
19720 /* For an in-class declaration, use `grokfield' to create the
19721 declaration. */
19722 if (member_p)
19724 if (pushed_scope)
19726 pop_scope (pushed_scope);
19727 pushed_scope = NULL_TREE;
19729 decl = grokfield (declarator, decl_specifiers,
19730 initializer, !is_non_constant_init,
19731 /*asmspec=*/NULL_TREE,
19732 attr_chainon (attributes, prefix_attributes));
19733 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19734 cp_parser_save_default_args (parser, decl);
19735 cp_finalize_omp_declare_simd (parser, decl);
19736 cp_finalize_oacc_routine (parser, decl, false);
19739 /* Finish processing the declaration. But, skip member
19740 declarations. */
19741 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19743 cp_finish_decl (decl,
19744 initializer, !is_non_constant_init,
19745 asm_specification,
19746 /* If the initializer is in parentheses, then this is
19747 a direct-initialization, which means that an
19748 `explicit' constructor is OK. Otherwise, an
19749 `explicit' constructor cannot be used. */
19750 ((is_direct_init || !is_initialized)
19751 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19753 else if ((cxx_dialect != cxx98) && friend_p
19754 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19755 /* Core issue #226 (C++0x only): A default template-argument
19756 shall not be specified in a friend class template
19757 declaration. */
19758 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19759 /*is_partial=*/false, /*is_friend_decl=*/1);
19761 if (!friend_p && pushed_scope)
19762 pop_scope (pushed_scope);
19764 if (function_declarator_p (declarator)
19765 && parser->fully_implicit_function_template_p)
19767 if (member_p)
19768 decl = finish_fully_implicit_template (parser, decl);
19769 else
19770 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19773 if (auto_result && is_initialized && decl_specifiers->type
19774 && type_uses_auto (decl_specifiers->type))
19775 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19777 return decl;
19780 /* Parse a declarator.
19782 declarator:
19783 direct-declarator
19784 ptr-operator declarator
19786 abstract-declarator:
19787 ptr-operator abstract-declarator [opt]
19788 direct-abstract-declarator
19790 GNU Extensions:
19792 declarator:
19793 attributes [opt] direct-declarator
19794 attributes [opt] ptr-operator declarator
19796 abstract-declarator:
19797 attributes [opt] ptr-operator abstract-declarator [opt]
19798 attributes [opt] direct-abstract-declarator
19800 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19801 detect constructors, destructors, deduction guides, or conversion operators.
19802 It is set to -1 if the declarator is a name, and +1 if it is a
19803 function. Otherwise it is set to zero. Usually you just want to
19804 test for >0, but internally the negative value is used.
19806 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19807 a decl-specifier-seq unless it declares a constructor, destructor,
19808 or conversion. It might seem that we could check this condition in
19809 semantic analysis, rather than parsing, but that makes it difficult
19810 to handle something like `f()'. We want to notice that there are
19811 no decl-specifiers, and therefore realize that this is an
19812 expression, not a declaration.)
19814 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19815 the declarator is a direct-declarator of the form "(...)".
19817 MEMBER_P is true iff this declarator is a member-declarator.
19819 FRIEND_P is true iff this declarator is a friend. */
19821 static cp_declarator *
19822 cp_parser_declarator (cp_parser* parser,
19823 cp_parser_declarator_kind dcl_kind,
19824 int* ctor_dtor_or_conv_p,
19825 bool* parenthesized_p,
19826 bool member_p, bool friend_p)
19828 cp_declarator *declarator;
19829 enum tree_code code;
19830 cp_cv_quals cv_quals;
19831 tree class_type;
19832 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19834 /* Assume this is not a constructor, destructor, or type-conversion
19835 operator. */
19836 if (ctor_dtor_or_conv_p)
19837 *ctor_dtor_or_conv_p = 0;
19839 if (cp_parser_allow_gnu_extensions_p (parser))
19840 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19842 /* Check for the ptr-operator production. */
19843 cp_parser_parse_tentatively (parser);
19844 /* Parse the ptr-operator. */
19845 code = cp_parser_ptr_operator (parser,
19846 &class_type,
19847 &cv_quals,
19848 &std_attributes);
19850 /* If that worked, then we have a ptr-operator. */
19851 if (cp_parser_parse_definitely (parser))
19853 /* If a ptr-operator was found, then this declarator was not
19854 parenthesized. */
19855 if (parenthesized_p)
19856 *parenthesized_p = true;
19857 /* The dependent declarator is optional if we are parsing an
19858 abstract-declarator. */
19859 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19860 cp_parser_parse_tentatively (parser);
19862 /* Parse the dependent declarator. */
19863 declarator = cp_parser_declarator (parser, dcl_kind,
19864 /*ctor_dtor_or_conv_p=*/NULL,
19865 /*parenthesized_p=*/NULL,
19866 /*member_p=*/false,
19867 friend_p);
19869 /* If we are parsing an abstract-declarator, we must handle the
19870 case where the dependent declarator is absent. */
19871 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19872 && !cp_parser_parse_definitely (parser))
19873 declarator = NULL;
19875 declarator = cp_parser_make_indirect_declarator
19876 (code, class_type, cv_quals, declarator, std_attributes);
19878 /* Everything else is a direct-declarator. */
19879 else
19881 if (parenthesized_p)
19882 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19883 CPP_OPEN_PAREN);
19884 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19885 ctor_dtor_or_conv_p,
19886 member_p, friend_p);
19889 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19890 declarator->attributes = gnu_attributes;
19891 return declarator;
19894 /* Parse a direct-declarator or direct-abstract-declarator.
19896 direct-declarator:
19897 declarator-id
19898 direct-declarator ( parameter-declaration-clause )
19899 cv-qualifier-seq [opt]
19900 ref-qualifier [opt]
19901 exception-specification [opt]
19902 direct-declarator [ constant-expression [opt] ]
19903 ( declarator )
19905 direct-abstract-declarator:
19906 direct-abstract-declarator [opt]
19907 ( parameter-declaration-clause )
19908 cv-qualifier-seq [opt]
19909 ref-qualifier [opt]
19910 exception-specification [opt]
19911 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19912 ( abstract-declarator )
19914 Returns a representation of the declarator. DCL_KIND is
19915 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19916 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19917 we are parsing a direct-declarator. It is
19918 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19919 of ambiguity we prefer an abstract declarator, as per
19920 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19921 as for cp_parser_declarator. */
19923 static cp_declarator *
19924 cp_parser_direct_declarator (cp_parser* parser,
19925 cp_parser_declarator_kind dcl_kind,
19926 int* ctor_dtor_or_conv_p,
19927 bool member_p, bool friend_p)
19929 cp_token *token;
19930 cp_declarator *declarator = NULL;
19931 tree scope = NULL_TREE;
19932 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19933 bool saved_in_declarator_p = parser->in_declarator_p;
19934 bool first = true;
19935 tree pushed_scope = NULL_TREE;
19936 cp_token *open_paren = NULL, *close_paren = NULL;
19938 while (true)
19940 /* Peek at the next token. */
19941 token = cp_lexer_peek_token (parser->lexer);
19942 if (token->type == CPP_OPEN_PAREN)
19944 /* This is either a parameter-declaration-clause, or a
19945 parenthesized declarator. When we know we are parsing a
19946 named declarator, it must be a parenthesized declarator
19947 if FIRST is true. For instance, `(int)' is a
19948 parameter-declaration-clause, with an omitted
19949 direct-abstract-declarator. But `((*))', is a
19950 parenthesized abstract declarator. Finally, when T is a
19951 template parameter `(T)' is a
19952 parameter-declaration-clause, and not a parenthesized
19953 named declarator.
19955 We first try and parse a parameter-declaration-clause,
19956 and then try a nested declarator (if FIRST is true).
19958 It is not an error for it not to be a
19959 parameter-declaration-clause, even when FIRST is
19960 false. Consider,
19962 int i (int);
19963 int i (3);
19965 The first is the declaration of a function while the
19966 second is the definition of a variable, including its
19967 initializer.
19969 Having seen only the parenthesis, we cannot know which of
19970 these two alternatives should be selected. Even more
19971 complex are examples like:
19973 int i (int (a));
19974 int i (int (3));
19976 The former is a function-declaration; the latter is a
19977 variable initialization.
19979 Thus again, we try a parameter-declaration-clause, and if
19980 that fails, we back out and return. */
19982 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19984 tree params;
19985 bool is_declarator = false;
19987 open_paren = NULL;
19989 /* In a member-declarator, the only valid interpretation
19990 of a parenthesis is the start of a
19991 parameter-declaration-clause. (It is invalid to
19992 initialize a static data member with a parenthesized
19993 initializer; only the "=" form of initialization is
19994 permitted.) */
19995 if (!member_p)
19996 cp_parser_parse_tentatively (parser);
19998 /* Consume the `('. */
19999 matching_parens parens;
20000 parens.consume_open (parser);
20001 if (first)
20003 /* If this is going to be an abstract declarator, we're
20004 in a declarator and we can't have default args. */
20005 parser->default_arg_ok_p = false;
20006 parser->in_declarator_p = true;
20009 begin_scope (sk_function_parms, NULL_TREE);
20011 /* Parse the parameter-declaration-clause. */
20012 params = cp_parser_parameter_declaration_clause (parser);
20014 /* Consume the `)'. */
20015 parens.require_close (parser);
20017 /* If all went well, parse the cv-qualifier-seq,
20018 ref-qualifier and the exception-specification. */
20019 if (member_p || cp_parser_parse_definitely (parser))
20021 cp_cv_quals cv_quals;
20022 cp_virt_specifiers virt_specifiers;
20023 cp_ref_qualifier ref_qual;
20024 tree exception_specification;
20025 tree late_return;
20026 tree attrs;
20027 bool memfn = (member_p || (pushed_scope
20028 && CLASS_TYPE_P (pushed_scope)));
20030 is_declarator = true;
20032 if (ctor_dtor_or_conv_p)
20033 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20034 first = false;
20036 /* Parse the cv-qualifier-seq. */
20037 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20038 /* Parse the ref-qualifier. */
20039 ref_qual = cp_parser_ref_qualifier_opt (parser);
20040 /* Parse the tx-qualifier. */
20041 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20042 /* And the exception-specification. */
20043 exception_specification
20044 = cp_parser_exception_specification_opt (parser);
20046 attrs = cp_parser_std_attribute_spec_seq (parser);
20048 /* In here, we handle cases where attribute is used after
20049 the function declaration. For example:
20050 void func (int x) __attribute__((vector(..))); */
20051 tree gnu_attrs = NULL_TREE;
20052 tree requires_clause = NULL_TREE;
20053 late_return = (cp_parser_late_return_type_opt
20054 (parser, declarator, requires_clause,
20055 memfn ? cv_quals : -1));
20057 /* Parse the virt-specifier-seq. */
20058 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20060 /* Create the function-declarator. */
20061 declarator = make_call_declarator (declarator,
20062 params,
20063 cv_quals,
20064 virt_specifiers,
20065 ref_qual,
20066 tx_qual,
20067 exception_specification,
20068 late_return,
20069 requires_clause);
20070 declarator->std_attributes = attrs;
20071 declarator->attributes = gnu_attrs;
20072 /* Any subsequent parameter lists are to do with
20073 return type, so are not those of the declared
20074 function. */
20075 parser->default_arg_ok_p = false;
20078 /* Remove the function parms from scope. */
20079 pop_bindings_and_leave_scope ();
20081 if (is_declarator)
20082 /* Repeat the main loop. */
20083 continue;
20086 /* If this is the first, we can try a parenthesized
20087 declarator. */
20088 if (first)
20090 bool saved_in_type_id_in_expr_p;
20092 parser->default_arg_ok_p = saved_default_arg_ok_p;
20093 parser->in_declarator_p = saved_in_declarator_p;
20095 open_paren = token;
20096 /* Consume the `('. */
20097 matching_parens parens;
20098 parens.consume_open (parser);
20099 /* Parse the nested declarator. */
20100 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20101 parser->in_type_id_in_expr_p = true;
20102 declarator
20103 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20104 /*parenthesized_p=*/NULL,
20105 member_p, friend_p);
20106 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20107 first = false;
20108 /* Expect a `)'. */
20109 close_paren = cp_lexer_peek_token (parser->lexer);
20110 if (!parens.require_close (parser))
20111 declarator = cp_error_declarator;
20112 if (declarator == cp_error_declarator)
20113 break;
20115 goto handle_declarator;
20117 /* Otherwise, we must be done. */
20118 else
20119 break;
20121 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20122 && token->type == CPP_OPEN_SQUARE
20123 && !cp_next_tokens_can_be_attribute_p (parser))
20125 /* Parse an array-declarator. */
20126 tree bounds, attrs;
20128 if (ctor_dtor_or_conv_p)
20129 *ctor_dtor_or_conv_p = 0;
20131 open_paren = NULL;
20132 first = false;
20133 parser->default_arg_ok_p = false;
20134 parser->in_declarator_p = true;
20135 /* Consume the `['. */
20136 cp_lexer_consume_token (parser->lexer);
20137 /* Peek at the next token. */
20138 token = cp_lexer_peek_token (parser->lexer);
20139 /* If the next token is `]', then there is no
20140 constant-expression. */
20141 if (token->type != CPP_CLOSE_SQUARE)
20143 bool non_constant_p;
20144 bounds
20145 = cp_parser_constant_expression (parser,
20146 /*allow_non_constant=*/true,
20147 &non_constant_p);
20148 if (!non_constant_p)
20149 /* OK */;
20150 else if (error_operand_p (bounds))
20151 /* Already gave an error. */;
20152 else if (!parser->in_function_body
20153 || current_binding_level->kind == sk_function_parms)
20155 /* Normally, the array bound must be an integral constant
20156 expression. However, as an extension, we allow VLAs
20157 in function scopes as long as they aren't part of a
20158 parameter declaration. */
20159 cp_parser_error (parser,
20160 "array bound is not an integer constant");
20161 bounds = error_mark_node;
20163 else if (processing_template_decl
20164 && !type_dependent_expression_p (bounds))
20166 /* Remember this wasn't a constant-expression. */
20167 bounds = build_nop (TREE_TYPE (bounds), bounds);
20168 TREE_SIDE_EFFECTS (bounds) = 1;
20171 else
20172 bounds = NULL_TREE;
20173 /* Look for the closing `]'. */
20174 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20176 declarator = cp_error_declarator;
20177 break;
20180 attrs = cp_parser_std_attribute_spec_seq (parser);
20181 declarator = make_array_declarator (declarator, bounds);
20182 declarator->std_attributes = attrs;
20184 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20187 tree qualifying_scope;
20188 tree unqualified_name;
20189 tree attrs;
20190 special_function_kind sfk;
20191 bool abstract_ok;
20192 bool pack_expansion_p = false;
20193 cp_token *declarator_id_start_token;
20195 /* Parse a declarator-id */
20196 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20197 if (abstract_ok)
20199 cp_parser_parse_tentatively (parser);
20201 /* If we see an ellipsis, we should be looking at a
20202 parameter pack. */
20203 if (token->type == CPP_ELLIPSIS)
20205 /* Consume the `...' */
20206 cp_lexer_consume_token (parser->lexer);
20208 pack_expansion_p = true;
20212 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20213 unqualified_name
20214 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20215 qualifying_scope = parser->scope;
20216 if (abstract_ok)
20218 bool okay = false;
20220 if (!unqualified_name && pack_expansion_p)
20222 /* Check whether an error occurred. */
20223 okay = !cp_parser_error_occurred (parser);
20225 /* We already consumed the ellipsis to mark a
20226 parameter pack, but we have no way to report it,
20227 so abort the tentative parse. We will be exiting
20228 immediately anyway. */
20229 cp_parser_abort_tentative_parse (parser);
20231 else
20232 okay = cp_parser_parse_definitely (parser);
20234 if (!okay)
20235 unqualified_name = error_mark_node;
20236 else if (unqualified_name
20237 && (qualifying_scope
20238 || (!identifier_p (unqualified_name))))
20240 cp_parser_error (parser, "expected unqualified-id");
20241 unqualified_name = error_mark_node;
20245 if (!unqualified_name)
20246 return NULL;
20247 if (unqualified_name == error_mark_node)
20249 declarator = cp_error_declarator;
20250 pack_expansion_p = false;
20251 declarator->parameter_pack_p = false;
20252 break;
20255 attrs = cp_parser_std_attribute_spec_seq (parser);
20257 if (qualifying_scope && at_namespace_scope_p ()
20258 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20260 /* In the declaration of a member of a template class
20261 outside of the class itself, the SCOPE will sometimes
20262 be a TYPENAME_TYPE. For example, given:
20264 template <typename T>
20265 int S<T>::R::i = 3;
20267 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20268 this context, we must resolve S<T>::R to an ordinary
20269 type, rather than a typename type.
20271 The reason we normally avoid resolving TYPENAME_TYPEs
20272 is that a specialization of `S' might render
20273 `S<T>::R' not a type. However, if `S' is
20274 specialized, then this `i' will not be used, so there
20275 is no harm in resolving the types here. */
20276 tree type;
20278 /* Resolve the TYPENAME_TYPE. */
20279 type = resolve_typename_type (qualifying_scope,
20280 /*only_current_p=*/false);
20281 /* If that failed, the declarator is invalid. */
20282 if (TREE_CODE (type) == TYPENAME_TYPE)
20284 if (typedef_variant_p (type))
20285 error_at (declarator_id_start_token->location,
20286 "cannot define member of dependent typedef "
20287 "%qT", type);
20288 else
20289 error_at (declarator_id_start_token->location,
20290 "%<%T::%E%> is not a type",
20291 TYPE_CONTEXT (qualifying_scope),
20292 TYPE_IDENTIFIER (qualifying_scope));
20294 qualifying_scope = type;
20297 sfk = sfk_none;
20299 if (unqualified_name)
20301 tree class_type;
20303 if (qualifying_scope
20304 && CLASS_TYPE_P (qualifying_scope))
20305 class_type = qualifying_scope;
20306 else
20307 class_type = current_class_type;
20309 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20311 tree name_type = TREE_TYPE (unqualified_name);
20313 if (!class_type || !same_type_p (name_type, class_type))
20315 /* We do not attempt to print the declarator
20316 here because we do not have enough
20317 information about its original syntactic
20318 form. */
20319 cp_parser_error (parser, "invalid declarator");
20320 declarator = cp_error_declarator;
20321 break;
20323 else if (qualifying_scope
20324 && CLASSTYPE_USE_TEMPLATE (name_type))
20326 error_at (declarator_id_start_token->location,
20327 "invalid use of constructor as a template");
20328 inform (declarator_id_start_token->location,
20329 "use %<%T::%D%> instead of %<%T::%D%> to "
20330 "name the constructor in a qualified name",
20331 class_type,
20332 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20333 class_type, name_type);
20334 declarator = cp_error_declarator;
20335 break;
20337 unqualified_name = constructor_name (class_type);
20340 if (class_type)
20342 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20343 sfk = sfk_destructor;
20344 else if (identifier_p (unqualified_name)
20345 && IDENTIFIER_CONV_OP_P (unqualified_name))
20346 sfk = sfk_conversion;
20347 else if (/* There's no way to declare a constructor
20348 for an unnamed type, even if the type
20349 got a name for linkage purposes. */
20350 !TYPE_WAS_UNNAMED (class_type)
20351 /* Handle correctly (c++/19200):
20353 struct S {
20354 struct T{};
20355 friend void S(T);
20358 and also:
20360 namespace N {
20361 void S();
20364 struct S {
20365 friend void N::S();
20366 }; */
20367 && (!friend_p || class_type == qualifying_scope)
20368 && constructor_name_p (unqualified_name,
20369 class_type))
20370 sfk = sfk_constructor;
20371 else if (is_overloaded_fn (unqualified_name)
20372 && DECL_CONSTRUCTOR_P (get_first_fn
20373 (unqualified_name)))
20374 sfk = sfk_constructor;
20376 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20377 *ctor_dtor_or_conv_p = -1;
20380 declarator = make_id_declarator (qualifying_scope,
20381 unqualified_name,
20382 sfk);
20383 declarator->std_attributes = attrs;
20384 declarator->id_loc = token->location;
20385 declarator->parameter_pack_p = pack_expansion_p;
20387 if (pack_expansion_p)
20388 maybe_warn_variadic_templates ();
20391 handle_declarator:;
20392 scope = get_scope_of_declarator (declarator);
20393 if (scope)
20395 /* Any names that appear after the declarator-id for a
20396 member are looked up in the containing scope. */
20397 if (at_function_scope_p ())
20399 /* But declarations with qualified-ids can't appear in a
20400 function. */
20401 cp_parser_error (parser, "qualified-id in declaration");
20402 declarator = cp_error_declarator;
20403 break;
20405 pushed_scope = push_scope (scope);
20407 parser->in_declarator_p = true;
20408 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20409 || (declarator && declarator->kind == cdk_id))
20410 /* Default args are only allowed on function
20411 declarations. */
20412 parser->default_arg_ok_p = saved_default_arg_ok_p;
20413 else
20414 parser->default_arg_ok_p = false;
20416 first = false;
20418 /* We're done. */
20419 else
20420 break;
20423 /* For an abstract declarator, we might wind up with nothing at this
20424 point. That's an error; the declarator is not optional. */
20425 if (!declarator)
20426 cp_parser_error (parser, "expected declarator");
20427 else if (open_paren)
20429 /* Record overly parenthesized declarator so we can give a
20430 diagnostic about confusing decl/expr disambiguation. */
20431 if (declarator->kind == cdk_array)
20433 /* If the open and close parens are on different lines, this
20434 is probably a formatting thing, so ignore. */
20435 expanded_location open = expand_location (open_paren->location);
20436 expanded_location close = expand_location (close_paren->location);
20437 if (open.line != close.line || open.file != close.file)
20438 open_paren = NULL;
20440 if (open_paren)
20441 declarator->parenthesized = open_paren->location;
20444 /* If we entered a scope, we must exit it now. */
20445 if (pushed_scope)
20446 pop_scope (pushed_scope);
20448 parser->default_arg_ok_p = saved_default_arg_ok_p;
20449 parser->in_declarator_p = saved_in_declarator_p;
20451 return declarator;
20454 /* Parse a ptr-operator.
20456 ptr-operator:
20457 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20458 * cv-qualifier-seq [opt]
20460 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20461 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20463 GNU Extension:
20465 ptr-operator:
20466 & cv-qualifier-seq [opt]
20468 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20469 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20470 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20471 filled in with the TYPE containing the member. *CV_QUALS is
20472 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20473 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20474 Note that the tree codes returned by this function have nothing
20475 to do with the types of trees that will be eventually be created
20476 to represent the pointer or reference type being parsed. They are
20477 just constants with suggestive names. */
20478 static enum tree_code
20479 cp_parser_ptr_operator (cp_parser* parser,
20480 tree* type,
20481 cp_cv_quals *cv_quals,
20482 tree *attributes)
20484 enum tree_code code = ERROR_MARK;
20485 cp_token *token;
20486 tree attrs = NULL_TREE;
20488 /* Assume that it's not a pointer-to-member. */
20489 *type = NULL_TREE;
20490 /* And that there are no cv-qualifiers. */
20491 *cv_quals = TYPE_UNQUALIFIED;
20493 /* Peek at the next token. */
20494 token = cp_lexer_peek_token (parser->lexer);
20496 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20497 if (token->type == CPP_MULT)
20498 code = INDIRECT_REF;
20499 else if (token->type == CPP_AND)
20500 code = ADDR_EXPR;
20501 else if ((cxx_dialect != cxx98) &&
20502 token->type == CPP_AND_AND) /* C++0x only */
20503 code = NON_LVALUE_EXPR;
20505 if (code != ERROR_MARK)
20507 /* Consume the `*', `&' or `&&'. */
20508 cp_lexer_consume_token (parser->lexer);
20510 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20511 `&', if we are allowing GNU extensions. (The only qualifier
20512 that can legally appear after `&' is `restrict', but that is
20513 enforced during semantic analysis. */
20514 if (code == INDIRECT_REF
20515 || cp_parser_allow_gnu_extensions_p (parser))
20516 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20518 attrs = cp_parser_std_attribute_spec_seq (parser);
20519 if (attributes != NULL)
20520 *attributes = attrs;
20522 else
20524 /* Try the pointer-to-member case. */
20525 cp_parser_parse_tentatively (parser);
20526 /* Look for the optional `::' operator. */
20527 cp_parser_global_scope_opt (parser,
20528 /*current_scope_valid_p=*/false);
20529 /* Look for the nested-name specifier. */
20530 token = cp_lexer_peek_token (parser->lexer);
20531 cp_parser_nested_name_specifier (parser,
20532 /*typename_keyword_p=*/false,
20533 /*check_dependency_p=*/true,
20534 /*type_p=*/false,
20535 /*is_declaration=*/false);
20536 /* If we found it, and the next token is a `*', then we are
20537 indeed looking at a pointer-to-member operator. */
20538 if (!cp_parser_error_occurred (parser)
20539 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20541 /* Indicate that the `*' operator was used. */
20542 code = INDIRECT_REF;
20544 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20545 error_at (token->location, "%qD is a namespace", parser->scope);
20546 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20547 error_at (token->location, "cannot form pointer to member of "
20548 "non-class %q#T", parser->scope);
20549 else
20551 /* The type of which the member is a member is given by the
20552 current SCOPE. */
20553 *type = parser->scope;
20554 /* The next name will not be qualified. */
20555 parser->scope = NULL_TREE;
20556 parser->qualifying_scope = NULL_TREE;
20557 parser->object_scope = NULL_TREE;
20558 /* Look for optional c++11 attributes. */
20559 attrs = cp_parser_std_attribute_spec_seq (parser);
20560 if (attributes != NULL)
20561 *attributes = attrs;
20562 /* Look for the optional cv-qualifier-seq. */
20563 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20566 /* If that didn't work we don't have a ptr-operator. */
20567 if (!cp_parser_parse_definitely (parser))
20568 cp_parser_error (parser, "expected ptr-operator");
20571 return code;
20574 /* Parse an (optional) cv-qualifier-seq.
20576 cv-qualifier-seq:
20577 cv-qualifier cv-qualifier-seq [opt]
20579 cv-qualifier:
20580 const
20581 volatile
20583 GNU Extension:
20585 cv-qualifier:
20586 __restrict__
20588 Returns a bitmask representing the cv-qualifiers. */
20590 static cp_cv_quals
20591 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20593 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20595 while (true)
20597 cp_token *token;
20598 cp_cv_quals cv_qualifier;
20600 /* Peek at the next token. */
20601 token = cp_lexer_peek_token (parser->lexer);
20602 /* See if it's a cv-qualifier. */
20603 switch (token->keyword)
20605 case RID_CONST:
20606 cv_qualifier = TYPE_QUAL_CONST;
20607 break;
20609 case RID_VOLATILE:
20610 cv_qualifier = TYPE_QUAL_VOLATILE;
20611 break;
20613 case RID_RESTRICT:
20614 cv_qualifier = TYPE_QUAL_RESTRICT;
20615 break;
20617 default:
20618 cv_qualifier = TYPE_UNQUALIFIED;
20619 break;
20622 if (!cv_qualifier)
20623 break;
20625 if (cv_quals & cv_qualifier)
20627 gcc_rich_location richloc (token->location);
20628 richloc.add_fixit_remove ();
20629 error_at (&richloc, "duplicate cv-qualifier");
20630 cp_lexer_purge_token (parser->lexer);
20632 else
20634 cp_lexer_consume_token (parser->lexer);
20635 cv_quals |= cv_qualifier;
20639 return cv_quals;
20642 /* Parse an (optional) ref-qualifier
20644 ref-qualifier:
20648 Returns cp_ref_qualifier representing ref-qualifier. */
20650 static cp_ref_qualifier
20651 cp_parser_ref_qualifier_opt (cp_parser* parser)
20653 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20655 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20656 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20657 return ref_qual;
20659 while (true)
20661 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20662 cp_token *token = cp_lexer_peek_token (parser->lexer);
20664 switch (token->type)
20666 case CPP_AND:
20667 curr_ref_qual = REF_QUAL_LVALUE;
20668 break;
20670 case CPP_AND_AND:
20671 curr_ref_qual = REF_QUAL_RVALUE;
20672 break;
20674 default:
20675 curr_ref_qual = REF_QUAL_NONE;
20676 break;
20679 if (!curr_ref_qual)
20680 break;
20681 else if (ref_qual)
20683 error_at (token->location, "multiple ref-qualifiers");
20684 cp_lexer_purge_token (parser->lexer);
20686 else
20688 ref_qual = curr_ref_qual;
20689 cp_lexer_consume_token (parser->lexer);
20693 return ref_qual;
20696 /* Parse an optional tx-qualifier.
20698 tx-qualifier:
20699 transaction_safe
20700 transaction_safe_dynamic */
20702 static tree
20703 cp_parser_tx_qualifier_opt (cp_parser *parser)
20705 cp_token *token = cp_lexer_peek_token (parser->lexer);
20706 if (token->type == CPP_NAME)
20708 tree name = token->u.value;
20709 const char *p = IDENTIFIER_POINTER (name);
20710 const int len = strlen ("transaction_safe");
20711 if (!strncmp (p, "transaction_safe", len))
20713 p += len;
20714 if (*p == '\0'
20715 || !strcmp (p, "_dynamic"))
20717 cp_lexer_consume_token (parser->lexer);
20718 if (!flag_tm)
20720 error ("%qE requires %<-fgnu-tm%>", name);
20721 return NULL_TREE;
20723 else
20724 return name;
20728 return NULL_TREE;
20731 /* Parse an (optional) virt-specifier-seq.
20733 virt-specifier-seq:
20734 virt-specifier virt-specifier-seq [opt]
20736 virt-specifier:
20737 override
20738 final
20740 Returns a bitmask representing the virt-specifiers. */
20742 static cp_virt_specifiers
20743 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20745 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20747 while (true)
20749 cp_token *token;
20750 cp_virt_specifiers virt_specifier;
20752 /* Peek at the next token. */
20753 token = cp_lexer_peek_token (parser->lexer);
20754 /* See if it's a virt-specifier-qualifier. */
20755 if (token->type != CPP_NAME)
20756 break;
20757 if (id_equal (token->u.value, "override"))
20759 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20760 virt_specifier = VIRT_SPEC_OVERRIDE;
20762 else if (id_equal (token->u.value, "final"))
20764 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20765 virt_specifier = VIRT_SPEC_FINAL;
20767 else if (id_equal (token->u.value, "__final"))
20769 virt_specifier = VIRT_SPEC_FINAL;
20771 else
20772 break;
20774 if (virt_specifiers & virt_specifier)
20776 gcc_rich_location richloc (token->location);
20777 richloc.add_fixit_remove ();
20778 error_at (&richloc, "duplicate virt-specifier");
20779 cp_lexer_purge_token (parser->lexer);
20781 else
20783 cp_lexer_consume_token (parser->lexer);
20784 virt_specifiers |= virt_specifier;
20787 return virt_specifiers;
20790 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20791 is in scope even though it isn't real. */
20793 void
20794 inject_this_parameter (tree ctype, cp_cv_quals quals)
20796 tree this_parm;
20798 if (current_class_ptr)
20800 /* We don't clear this between NSDMIs. Is it already what we want? */
20801 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20802 if (DECL_P (current_class_ptr)
20803 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20804 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20805 && cp_type_quals (type) == quals)
20806 return;
20809 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20810 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20811 current_class_ptr = NULL_TREE;
20812 current_class_ref
20813 = cp_build_fold_indirect_ref (this_parm);
20814 current_class_ptr = this_parm;
20817 /* Return true iff our current scope is a non-static data member
20818 initializer. */
20820 bool
20821 parsing_nsdmi (void)
20823 /* We recognize NSDMI context by the context-less 'this' pointer set up
20824 by the function above. */
20825 if (current_class_ptr
20826 && TREE_CODE (current_class_ptr) == PARM_DECL
20827 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20828 return true;
20829 return false;
20832 /* Parse a late-specified return type, if any. This is not a separate
20833 non-terminal, but part of a function declarator, which looks like
20835 -> trailing-type-specifier-seq abstract-declarator(opt)
20837 Returns the type indicated by the type-id.
20839 In addition to this, parse any queued up #pragma omp declare simd
20840 clauses, and #pragma acc routine clauses.
20842 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20843 function. */
20845 static tree
20846 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20847 tree& requires_clause, cp_cv_quals quals)
20849 cp_token *token;
20850 tree type = NULL_TREE;
20851 bool declare_simd_p = (parser->omp_declare_simd
20852 && declarator
20853 && declarator->kind == cdk_id);
20855 bool oacc_routine_p = (parser->oacc_routine
20856 && declarator
20857 && declarator->kind == cdk_id);
20859 /* Peek at the next token. */
20860 token = cp_lexer_peek_token (parser->lexer);
20861 /* A late-specified return type is indicated by an initial '->'. */
20862 if (token->type != CPP_DEREF
20863 && token->keyword != RID_REQUIRES
20864 && !(token->type == CPP_NAME
20865 && token->u.value == ridpointers[RID_REQUIRES])
20866 && !(declare_simd_p || oacc_routine_p))
20867 return NULL_TREE;
20869 tree save_ccp = current_class_ptr;
20870 tree save_ccr = current_class_ref;
20871 if (quals >= 0)
20873 /* DR 1207: 'this' is in scope in the trailing return type. */
20874 inject_this_parameter (current_class_type, quals);
20877 if (token->type == CPP_DEREF)
20879 /* Consume the ->. */
20880 cp_lexer_consume_token (parser->lexer);
20882 type = cp_parser_trailing_type_id (parser);
20885 /* Function declarations may be followed by a trailing
20886 requires-clause. */
20887 requires_clause = cp_parser_requires_clause_opt (parser);
20889 if (declare_simd_p)
20890 declarator->attributes
20891 = cp_parser_late_parsing_omp_declare_simd (parser,
20892 declarator->attributes);
20893 if (oacc_routine_p)
20894 declarator->attributes
20895 = cp_parser_late_parsing_oacc_routine (parser,
20896 declarator->attributes);
20898 if (quals >= 0)
20900 current_class_ptr = save_ccp;
20901 current_class_ref = save_ccr;
20904 return type;
20907 /* Parse a declarator-id.
20909 declarator-id:
20910 id-expression
20911 :: [opt] nested-name-specifier [opt] type-name
20913 In the `id-expression' case, the value returned is as for
20914 cp_parser_id_expression if the id-expression was an unqualified-id.
20915 If the id-expression was a qualified-id, then a SCOPE_REF is
20916 returned. The first operand is the scope (either a NAMESPACE_DECL
20917 or TREE_TYPE), but the second is still just a representation of an
20918 unqualified-id. */
20920 static tree
20921 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20923 tree id;
20924 /* The expression must be an id-expression. Assume that qualified
20925 names are the names of types so that:
20927 template <class T>
20928 int S<T>::R::i = 3;
20930 will work; we must treat `S<T>::R' as the name of a type.
20931 Similarly, assume that qualified names are templates, where
20932 required, so that:
20934 template <class T>
20935 int S<T>::R<T>::i = 3;
20937 will work, too. */
20938 id = cp_parser_id_expression (parser,
20939 /*template_keyword_p=*/false,
20940 /*check_dependency_p=*/false,
20941 /*template_p=*/NULL,
20942 /*declarator_p=*/true,
20943 optional_p);
20944 if (id && BASELINK_P (id))
20945 id = BASELINK_FUNCTIONS (id);
20946 return id;
20949 /* Parse a type-id.
20951 type-id:
20952 type-specifier-seq abstract-declarator [opt]
20954 Returns the TYPE specified. */
20956 static tree
20957 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20958 bool is_trailing_return)
20960 cp_decl_specifier_seq type_specifier_seq;
20961 cp_declarator *abstract_declarator;
20963 /* Parse the type-specifier-seq. */
20964 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20965 is_trailing_return,
20966 &type_specifier_seq);
20967 if (is_template_arg && type_specifier_seq.type
20968 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20969 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20970 /* A bare template name as a template argument is a template template
20971 argument, not a placeholder, so fail parsing it as a type argument. */
20973 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20974 cp_parser_simulate_error (parser);
20975 return error_mark_node;
20977 if (type_specifier_seq.type == error_mark_node)
20978 return error_mark_node;
20980 /* There might or might not be an abstract declarator. */
20981 cp_parser_parse_tentatively (parser);
20982 /* Look for the declarator. */
20983 abstract_declarator
20984 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20985 /*parenthesized_p=*/NULL,
20986 /*member_p=*/false,
20987 /*friend_p=*/false);
20988 /* Check to see if there really was a declarator. */
20989 if (!cp_parser_parse_definitely (parser))
20990 abstract_declarator = NULL;
20992 if (type_specifier_seq.type
20993 /* The concepts TS allows 'auto' as a type-id. */
20994 && (!flag_concepts || parser->in_type_id_in_expr_p)
20995 /* None of the valid uses of 'auto' in C++14 involve the type-id
20996 nonterminal, but it is valid in a trailing-return-type. */
20997 && !(cxx_dialect >= cxx14 && is_trailing_return))
20998 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21000 /* A type-id with type 'auto' is only ok if the abstract declarator
21001 is a function declarator with a late-specified return type.
21003 A type-id with 'auto' is also valid in a trailing-return-type
21004 in a compound-requirement. */
21005 if (abstract_declarator
21006 && abstract_declarator->kind == cdk_function
21007 && abstract_declarator->u.function.late_return_type)
21008 /* OK */;
21009 else if (parser->in_result_type_constraint_p)
21010 /* OK */;
21011 else
21013 location_t loc = type_specifier_seq.locations[ds_type_spec];
21014 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21016 error_at (loc, "missing template arguments after %qT",
21017 auto_node);
21018 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21019 tmpl);
21021 else
21022 error_at (loc, "invalid use of %qT", auto_node);
21023 return error_mark_node;
21027 return groktypename (&type_specifier_seq, abstract_declarator,
21028 is_template_arg);
21031 static tree
21032 cp_parser_type_id (cp_parser *parser)
21034 return cp_parser_type_id_1 (parser, false, false);
21037 static tree
21038 cp_parser_template_type_arg (cp_parser *parser)
21040 tree r;
21041 const char *saved_message = parser->type_definition_forbidden_message;
21042 parser->type_definition_forbidden_message
21043 = G_("types may not be defined in template arguments");
21044 r = cp_parser_type_id_1 (parser, true, false);
21045 parser->type_definition_forbidden_message = saved_message;
21046 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21048 error ("invalid use of %<auto%> in template argument");
21049 r = error_mark_node;
21051 return r;
21054 static tree
21055 cp_parser_trailing_type_id (cp_parser *parser)
21057 return cp_parser_type_id_1 (parser, false, true);
21060 /* Parse a type-specifier-seq.
21062 type-specifier-seq:
21063 type-specifier type-specifier-seq [opt]
21065 GNU extension:
21067 type-specifier-seq:
21068 attributes type-specifier-seq [opt]
21070 If IS_DECLARATION is true, we are at the start of a "condition" or
21071 exception-declaration, so we might be followed by a declarator-id.
21073 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21074 i.e. we've just seen "->".
21076 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21078 static void
21079 cp_parser_type_specifier_seq (cp_parser* parser,
21080 bool is_declaration,
21081 bool is_trailing_return,
21082 cp_decl_specifier_seq *type_specifier_seq)
21084 bool seen_type_specifier = false;
21085 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21086 cp_token *start_token = NULL;
21088 /* Clear the TYPE_SPECIFIER_SEQ. */
21089 clear_decl_specs (type_specifier_seq);
21091 /* In the context of a trailing return type, enum E { } is an
21092 elaborated-type-specifier followed by a function-body, not an
21093 enum-specifier. */
21094 if (is_trailing_return)
21095 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21097 /* Parse the type-specifiers and attributes. */
21098 while (true)
21100 tree type_specifier;
21101 bool is_cv_qualifier;
21103 /* Check for attributes first. */
21104 if (cp_next_tokens_can_be_attribute_p (parser))
21106 type_specifier_seq->attributes
21107 = attr_chainon (type_specifier_seq->attributes,
21108 cp_parser_attributes_opt (parser));
21109 continue;
21112 /* record the token of the beginning of the type specifier seq,
21113 for error reporting purposes*/
21114 if (!start_token)
21115 start_token = cp_lexer_peek_token (parser->lexer);
21117 /* Look for the type-specifier. */
21118 type_specifier = cp_parser_type_specifier (parser,
21119 flags,
21120 type_specifier_seq,
21121 /*is_declaration=*/false,
21122 NULL,
21123 &is_cv_qualifier);
21124 if (!type_specifier)
21126 /* If the first type-specifier could not be found, this is not a
21127 type-specifier-seq at all. */
21128 if (!seen_type_specifier)
21130 /* Set in_declarator_p to avoid skipping to the semicolon. */
21131 int in_decl = parser->in_declarator_p;
21132 parser->in_declarator_p = true;
21134 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21135 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21136 cp_parser_error (parser, "expected type-specifier");
21138 parser->in_declarator_p = in_decl;
21140 type_specifier_seq->type = error_mark_node;
21141 return;
21143 /* If subsequent type-specifiers could not be found, the
21144 type-specifier-seq is complete. */
21145 break;
21148 seen_type_specifier = true;
21149 /* The standard says that a condition can be:
21151 type-specifier-seq declarator = assignment-expression
21153 However, given:
21155 struct S {};
21156 if (int S = ...)
21158 we should treat the "S" as a declarator, not as a
21159 type-specifier. The standard doesn't say that explicitly for
21160 type-specifier-seq, but it does say that for
21161 decl-specifier-seq in an ordinary declaration. Perhaps it
21162 would be clearer just to allow a decl-specifier-seq here, and
21163 then add a semantic restriction that if any decl-specifiers
21164 that are not type-specifiers appear, the program is invalid. */
21165 if (is_declaration && !is_cv_qualifier)
21166 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21170 /* Return whether the function currently being declared has an associated
21171 template parameter list. */
21173 static bool
21174 function_being_declared_is_template_p (cp_parser* parser)
21176 if (!current_template_parms || processing_template_parmlist)
21177 return false;
21179 if (parser->implicit_template_scope)
21180 return true;
21182 if (at_class_scope_p ()
21183 && TYPE_BEING_DEFINED (current_class_type))
21184 return parser->num_template_parameter_lists != 0;
21186 return ((int) parser->num_template_parameter_lists > template_class_depth
21187 (current_class_type));
21190 /* Parse a parameter-declaration-clause.
21192 parameter-declaration-clause:
21193 parameter-declaration-list [opt] ... [opt]
21194 parameter-declaration-list , ...
21196 Returns a representation for the parameter declarations. A return
21197 value of NULL indicates a parameter-declaration-clause consisting
21198 only of an ellipsis. */
21200 static tree
21201 cp_parser_parameter_declaration_clause (cp_parser* parser)
21203 tree parameters;
21204 cp_token *token;
21205 bool ellipsis_p;
21206 bool is_error;
21208 temp_override<bool> cleanup
21209 (parser->auto_is_implicit_function_template_parm_p);
21211 if (!processing_specialization
21212 && !processing_template_parmlist
21213 && !processing_explicit_instantiation
21214 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21215 actual function or a random abstract declarator. */
21216 && parser->default_arg_ok_p)
21217 if (!current_function_decl
21218 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21219 parser->auto_is_implicit_function_template_parm_p = true;
21221 /* Peek at the next token. */
21222 token = cp_lexer_peek_token (parser->lexer);
21223 /* Check for trivial parameter-declaration-clauses. */
21224 if (token->type == CPP_ELLIPSIS)
21226 /* Consume the `...' token. */
21227 cp_lexer_consume_token (parser->lexer);
21228 return NULL_TREE;
21230 else if (token->type == CPP_CLOSE_PAREN)
21231 /* There are no parameters. */
21233 #ifndef NO_IMPLICIT_EXTERN_C
21234 if (in_system_header_at (input_location)
21235 && current_class_type == NULL
21236 && current_lang_name == lang_name_c)
21237 return NULL_TREE;
21238 else
21239 #endif
21240 return void_list_node;
21242 /* Check for `(void)', too, which is a special case. */
21243 else if (token->keyword == RID_VOID
21244 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21245 == CPP_CLOSE_PAREN))
21247 /* Consume the `void' token. */
21248 cp_lexer_consume_token (parser->lexer);
21249 /* There are no parameters. */
21250 return void_list_node;
21253 /* Parse the parameter-declaration-list. */
21254 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21255 /* If a parse error occurred while parsing the
21256 parameter-declaration-list, then the entire
21257 parameter-declaration-clause is erroneous. */
21258 if (is_error)
21259 return NULL;
21261 /* Peek at the next token. */
21262 token = cp_lexer_peek_token (parser->lexer);
21263 /* If it's a `,', the clause should terminate with an ellipsis. */
21264 if (token->type == CPP_COMMA)
21266 /* Consume the `,'. */
21267 cp_lexer_consume_token (parser->lexer);
21268 /* Expect an ellipsis. */
21269 ellipsis_p
21270 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21272 /* It might also be `...' if the optional trailing `,' was
21273 omitted. */
21274 else if (token->type == CPP_ELLIPSIS)
21276 /* Consume the `...' token. */
21277 cp_lexer_consume_token (parser->lexer);
21278 /* And remember that we saw it. */
21279 ellipsis_p = true;
21281 else
21282 ellipsis_p = false;
21284 /* Finish the parameter list. */
21285 if (!ellipsis_p)
21286 parameters = chainon (parameters, void_list_node);
21288 return parameters;
21291 /* Parse a parameter-declaration-list.
21293 parameter-declaration-list:
21294 parameter-declaration
21295 parameter-declaration-list , parameter-declaration
21297 Returns a representation of the parameter-declaration-list, as for
21298 cp_parser_parameter_declaration_clause. However, the
21299 `void_list_node' is never appended to the list. Upon return,
21300 *IS_ERROR will be true iff an error occurred. */
21302 static tree
21303 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21305 tree parameters = NULL_TREE;
21306 tree *tail = &parameters;
21307 bool saved_in_unbraced_linkage_specification_p;
21308 int index = 0;
21310 /* Assume all will go well. */
21311 *is_error = false;
21312 /* The special considerations that apply to a function within an
21313 unbraced linkage specifications do not apply to the parameters
21314 to the function. */
21315 saved_in_unbraced_linkage_specification_p
21316 = parser->in_unbraced_linkage_specification_p;
21317 parser->in_unbraced_linkage_specification_p = false;
21319 /* Look for more parameters. */
21320 while (true)
21322 cp_parameter_declarator *parameter;
21323 tree decl = error_mark_node;
21324 bool parenthesized_p = false;
21326 /* Parse the parameter. */
21327 parameter
21328 = cp_parser_parameter_declaration (parser,
21329 /*template_parm_p=*/false,
21330 &parenthesized_p);
21332 /* We don't know yet if the enclosing context is deprecated, so wait
21333 and warn in grokparms if appropriate. */
21334 deprecated_state = DEPRECATED_SUPPRESS;
21336 if (parameter)
21338 decl = grokdeclarator (parameter->declarator,
21339 &parameter->decl_specifiers,
21340 PARM,
21341 parameter->default_argument != NULL_TREE,
21342 &parameter->decl_specifiers.attributes);
21343 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21344 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21347 deprecated_state = DEPRECATED_NORMAL;
21349 /* If a parse error occurred parsing the parameter declaration,
21350 then the entire parameter-declaration-list is erroneous. */
21351 if (decl == error_mark_node)
21353 *is_error = true;
21354 parameters = error_mark_node;
21355 break;
21358 if (parameter->decl_specifiers.attributes)
21359 cplus_decl_attributes (&decl,
21360 parameter->decl_specifiers.attributes,
21362 if (DECL_NAME (decl))
21363 decl = pushdecl (decl);
21365 if (decl != error_mark_node)
21367 retrofit_lang_decl (decl);
21368 DECL_PARM_INDEX (decl) = ++index;
21369 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21372 /* Add the new parameter to the list. */
21373 *tail = build_tree_list (parameter->default_argument, decl);
21374 tail = &TREE_CHAIN (*tail);
21376 /* Peek at the next token. */
21377 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21378 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21379 /* These are for Objective-C++ */
21380 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21381 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21382 /* The parameter-declaration-list is complete. */
21383 break;
21384 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21386 cp_token *token;
21388 /* Peek at the next token. */
21389 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21390 /* If it's an ellipsis, then the list is complete. */
21391 if (token->type == CPP_ELLIPSIS)
21392 break;
21393 /* Otherwise, there must be more parameters. Consume the
21394 `,'. */
21395 cp_lexer_consume_token (parser->lexer);
21396 /* When parsing something like:
21398 int i(float f, double d)
21400 we can tell after seeing the declaration for "f" that we
21401 are not looking at an initialization of a variable "i",
21402 but rather at the declaration of a function "i".
21404 Due to the fact that the parsing of template arguments
21405 (as specified to a template-id) requires backtracking we
21406 cannot use this technique when inside a template argument
21407 list. */
21408 if (!parser->in_template_argument_list_p
21409 && !parser->in_type_id_in_expr_p
21410 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21411 /* However, a parameter-declaration of the form
21412 "float(f)" (which is a valid declaration of a
21413 parameter "f") can also be interpreted as an
21414 expression (the conversion of "f" to "float"). */
21415 && !parenthesized_p)
21416 cp_parser_commit_to_tentative_parse (parser);
21418 else
21420 cp_parser_error (parser, "expected %<,%> or %<...%>");
21421 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21422 cp_parser_skip_to_closing_parenthesis (parser,
21423 /*recovering=*/true,
21424 /*or_comma=*/false,
21425 /*consume_paren=*/false);
21426 break;
21430 parser->in_unbraced_linkage_specification_p
21431 = saved_in_unbraced_linkage_specification_p;
21433 /* Reset implicit_template_scope if we are about to leave the function
21434 parameter list that introduced it. Note that for out-of-line member
21435 definitions, there will be one or more class scopes before we get to
21436 the template parameter scope. */
21438 if (cp_binding_level *its = parser->implicit_template_scope)
21439 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21441 while (maybe_its->kind == sk_class)
21442 maybe_its = maybe_its->level_chain;
21443 if (maybe_its == its)
21445 parser->implicit_template_parms = 0;
21446 parser->implicit_template_scope = 0;
21450 return parameters;
21453 /* Parse a parameter declaration.
21455 parameter-declaration:
21456 decl-specifier-seq ... [opt] declarator
21457 decl-specifier-seq declarator = assignment-expression
21458 decl-specifier-seq ... [opt] abstract-declarator [opt]
21459 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21461 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21462 declares a template parameter. (In that case, a non-nested `>'
21463 token encountered during the parsing of the assignment-expression
21464 is not interpreted as a greater-than operator.)
21466 Returns a representation of the parameter, or NULL if an error
21467 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21468 true iff the declarator is of the form "(p)". */
21470 static cp_parameter_declarator *
21471 cp_parser_parameter_declaration (cp_parser *parser,
21472 bool template_parm_p,
21473 bool *parenthesized_p)
21475 int declares_class_or_enum;
21476 cp_decl_specifier_seq decl_specifiers;
21477 cp_declarator *declarator;
21478 tree default_argument;
21479 cp_token *token = NULL, *declarator_token_start = NULL;
21480 const char *saved_message;
21481 bool template_parameter_pack_p = false;
21483 /* In a template parameter, `>' is not an operator.
21485 [temp.param]
21487 When parsing a default template-argument for a non-type
21488 template-parameter, the first non-nested `>' is taken as the end
21489 of the template parameter-list rather than a greater-than
21490 operator. */
21492 /* Type definitions may not appear in parameter types. */
21493 saved_message = parser->type_definition_forbidden_message;
21494 parser->type_definition_forbidden_message
21495 = G_("types may not be defined in parameter types");
21497 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21498 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21499 (current_template_parms)) : 0);
21501 /* Parse the declaration-specifiers. */
21502 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21503 cp_parser_decl_specifier_seq (parser,
21504 CP_PARSER_FLAGS_NONE,
21505 &decl_specifiers,
21506 &declares_class_or_enum);
21508 /* Complain about missing 'typename' or other invalid type names. */
21509 if (!decl_specifiers.any_type_specifiers_p
21510 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21511 decl_specifiers.type = error_mark_node;
21513 /* If an error occurred, there's no reason to attempt to parse the
21514 rest of the declaration. */
21515 if (cp_parser_error_occurred (parser))
21517 parser->type_definition_forbidden_message = saved_message;
21518 return NULL;
21521 /* Peek at the next token. */
21522 token = cp_lexer_peek_token (parser->lexer);
21524 /* If the next token is a `)', `,', `=', `>', or `...', then there
21525 is no declarator. However, when variadic templates are enabled,
21526 there may be a declarator following `...'. */
21527 if (token->type == CPP_CLOSE_PAREN
21528 || token->type == CPP_COMMA
21529 || token->type == CPP_EQ
21530 || token->type == CPP_GREATER)
21532 declarator = NULL;
21533 if (parenthesized_p)
21534 *parenthesized_p = false;
21536 /* Otherwise, there should be a declarator. */
21537 else
21539 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21540 parser->default_arg_ok_p = false;
21542 /* After seeing a decl-specifier-seq, if the next token is not a
21543 "(", there is no possibility that the code is a valid
21544 expression. Therefore, if parsing tentatively, we commit at
21545 this point. */
21546 if (!parser->in_template_argument_list_p
21547 /* In an expression context, having seen:
21549 (int((char ...
21551 we cannot be sure whether we are looking at a
21552 function-type (taking a "char" as a parameter) or a cast
21553 of some object of type "char" to "int". */
21554 && !parser->in_type_id_in_expr_p
21555 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21556 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21557 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21558 cp_parser_commit_to_tentative_parse (parser);
21559 /* Parse the declarator. */
21560 declarator_token_start = token;
21561 declarator = cp_parser_declarator (parser,
21562 CP_PARSER_DECLARATOR_EITHER,
21563 /*ctor_dtor_or_conv_p=*/NULL,
21564 parenthesized_p,
21565 /*member_p=*/false,
21566 /*friend_p=*/false);
21567 parser->default_arg_ok_p = saved_default_arg_ok_p;
21568 /* After the declarator, allow more attributes. */
21569 decl_specifiers.attributes
21570 = attr_chainon (decl_specifiers.attributes,
21571 cp_parser_attributes_opt (parser));
21573 /* If the declarator is a template parameter pack, remember that and
21574 clear the flag in the declarator itself so we don't get errors
21575 from grokdeclarator. */
21576 if (template_parm_p && declarator && declarator->parameter_pack_p)
21578 declarator->parameter_pack_p = false;
21579 template_parameter_pack_p = true;
21583 /* If the next token is an ellipsis, and we have not seen a declarator
21584 name, and if either the type of the declarator contains parameter
21585 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21586 for, eg, abbreviated integral type names), then we actually have a
21587 parameter pack expansion expression. Otherwise, leave the ellipsis
21588 for a C-style variadic function. */
21589 token = cp_lexer_peek_token (parser->lexer);
21591 /* If a function parameter pack was specified and an implicit template
21592 parameter was introduced during cp_parser_parameter_declaration,
21593 change any implicit parameters introduced into packs. */
21594 if (parser->implicit_template_parms
21595 && (token->type == CPP_ELLIPSIS
21596 || (declarator && declarator->parameter_pack_p)))
21598 int latest_template_parm_idx = TREE_VEC_LENGTH
21599 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21601 if (latest_template_parm_idx != template_parm_idx)
21602 decl_specifiers.type = convert_generic_types_to_packs
21603 (decl_specifiers.type,
21604 template_parm_idx, latest_template_parm_idx);
21607 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21609 tree type = decl_specifiers.type;
21611 if (type && DECL_P (type))
21612 type = TREE_TYPE (type);
21614 if (((type
21615 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21616 && (template_parm_p || uses_parameter_packs (type)))
21617 || (!type && template_parm_p))
21618 && declarator_can_be_parameter_pack (declarator))
21620 /* Consume the `...'. */
21621 cp_lexer_consume_token (parser->lexer);
21622 maybe_warn_variadic_templates ();
21624 /* Build a pack expansion type */
21625 if (template_parm_p)
21626 template_parameter_pack_p = true;
21627 else if (declarator)
21628 declarator->parameter_pack_p = true;
21629 else
21630 decl_specifiers.type = make_pack_expansion (type);
21634 /* The restriction on defining new types applies only to the type
21635 of the parameter, not to the default argument. */
21636 parser->type_definition_forbidden_message = saved_message;
21638 /* If the next token is `=', then process a default argument. */
21639 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21641 tree type = decl_specifiers.type;
21642 token = cp_lexer_peek_token (parser->lexer);
21643 /* If we are defining a class, then the tokens that make up the
21644 default argument must be saved and processed later. */
21645 if (!template_parm_p && at_class_scope_p ()
21646 && TYPE_BEING_DEFINED (current_class_type)
21647 && !LAMBDA_TYPE_P (current_class_type))
21648 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21650 // A constrained-type-specifier may declare a type template-parameter.
21651 else if (declares_constrained_type_template_parameter (type))
21652 default_argument
21653 = cp_parser_default_type_template_argument (parser);
21655 // A constrained-type-specifier may declare a template-template-parameter.
21656 else if (declares_constrained_template_template_parameter (type))
21657 default_argument
21658 = cp_parser_default_template_template_argument (parser);
21660 /* Outside of a class definition, we can just parse the
21661 assignment-expression. */
21662 else
21663 default_argument
21664 = cp_parser_default_argument (parser, template_parm_p);
21666 if (!parser->default_arg_ok_p)
21668 permerror (token->location,
21669 "default arguments are only "
21670 "permitted for function parameters");
21672 else if ((declarator && declarator->parameter_pack_p)
21673 || template_parameter_pack_p
21674 || (decl_specifiers.type
21675 && PACK_EXPANSION_P (decl_specifiers.type)))
21677 /* Find the name of the parameter pack. */
21678 cp_declarator *id_declarator = declarator;
21679 while (id_declarator && id_declarator->kind != cdk_id)
21680 id_declarator = id_declarator->declarator;
21682 if (id_declarator && id_declarator->kind == cdk_id)
21683 error_at (declarator_token_start->location,
21684 template_parm_p
21685 ? G_("template parameter pack %qD "
21686 "cannot have a default argument")
21687 : G_("parameter pack %qD cannot have "
21688 "a default argument"),
21689 id_declarator->u.id.unqualified_name);
21690 else
21691 error_at (declarator_token_start->location,
21692 template_parm_p
21693 ? G_("template parameter pack cannot have "
21694 "a default argument")
21695 : G_("parameter pack cannot have a "
21696 "default argument"));
21698 default_argument = NULL_TREE;
21701 else
21702 default_argument = NULL_TREE;
21704 /* Generate a location for the parameter, ranging from the start of the
21705 initial token to the end of the final token (using input_location for
21706 the latter, set up by cp_lexer_set_source_position_from_token when
21707 consuming tokens).
21709 If we have a identifier, then use it for the caret location, e.g.
21711 extern int callee (int one, int (*two)(int, int), float three);
21712 ~~~~~~^~~~~~~~~~~~~~
21714 otherwise, reuse the start location for the caret location e.g.:
21716 extern int callee (int one, int (*)(int, int), float three);
21717 ^~~~~~~~~~~~~~~~~
21720 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21721 ? declarator->id_loc
21722 : decl_spec_token_start->location);
21723 location_t param_loc = make_location (caret_loc,
21724 decl_spec_token_start->location,
21725 input_location);
21727 return make_parameter_declarator (&decl_specifiers,
21728 declarator,
21729 default_argument,
21730 param_loc,
21731 template_parameter_pack_p);
21734 /* Parse a default argument and return it.
21736 TEMPLATE_PARM_P is true if this is a default argument for a
21737 non-type template parameter. */
21738 static tree
21739 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21741 tree default_argument = NULL_TREE;
21742 bool saved_greater_than_is_operator_p;
21743 bool saved_local_variables_forbidden_p;
21744 bool non_constant_p, is_direct_init;
21746 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21747 set correctly. */
21748 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21749 parser->greater_than_is_operator_p = !template_parm_p;
21750 /* Local variable names (and the `this' keyword) may not
21751 appear in a default argument. */
21752 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21753 parser->local_variables_forbidden_p = true;
21754 /* Parse the assignment-expression. */
21755 if (template_parm_p)
21756 push_deferring_access_checks (dk_no_deferred);
21757 tree saved_class_ptr = NULL_TREE;
21758 tree saved_class_ref = NULL_TREE;
21759 /* The "this" pointer is not valid in a default argument. */
21760 if (cfun)
21762 saved_class_ptr = current_class_ptr;
21763 cp_function_chain->x_current_class_ptr = NULL_TREE;
21764 saved_class_ref = current_class_ref;
21765 cp_function_chain->x_current_class_ref = NULL_TREE;
21767 default_argument
21768 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21769 /* Restore the "this" pointer. */
21770 if (cfun)
21772 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21773 cp_function_chain->x_current_class_ref = saved_class_ref;
21775 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21776 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21777 if (template_parm_p)
21778 pop_deferring_access_checks ();
21779 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21780 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21782 return default_argument;
21785 /* Parse a function-body.
21787 function-body:
21788 compound_statement */
21790 static void
21791 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21793 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21794 ? BCS_TRY_BLOCK : BCS_NORMAL),
21795 true);
21798 /* Parse a ctor-initializer-opt followed by a function-body. Return
21799 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21800 is true we are parsing a function-try-block. */
21802 static void
21803 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21804 bool in_function_try_block)
21806 tree body, list;
21807 const bool check_body_p =
21808 DECL_CONSTRUCTOR_P (current_function_decl)
21809 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21810 tree last = NULL;
21812 /* Begin the function body. */
21813 body = begin_function_body ();
21814 /* Parse the optional ctor-initializer. */
21815 cp_parser_ctor_initializer_opt (parser);
21817 /* If we're parsing a constexpr constructor definition, we need
21818 to check that the constructor body is indeed empty. However,
21819 before we get to cp_parser_function_body lot of junk has been
21820 generated, so we can't just check that we have an empty block.
21821 Rather we take a snapshot of the outermost block, and check whether
21822 cp_parser_function_body changed its state. */
21823 if (check_body_p)
21825 list = cur_stmt_list;
21826 if (STATEMENT_LIST_TAIL (list))
21827 last = STATEMENT_LIST_TAIL (list)->stmt;
21829 /* Parse the function-body. */
21830 cp_parser_function_body (parser, in_function_try_block);
21831 if (check_body_p)
21832 check_constexpr_ctor_body (last, list, /*complain=*/true);
21833 /* Finish the function body. */
21834 finish_function_body (body);
21837 /* Parse an initializer.
21839 initializer:
21840 = initializer-clause
21841 ( expression-list )
21843 Returns an expression representing the initializer. If no
21844 initializer is present, NULL_TREE is returned.
21846 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21847 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21848 set to TRUE if there is no initializer present. If there is an
21849 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21850 is set to true; otherwise it is set to false. */
21852 static tree
21853 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21854 bool* non_constant_p)
21856 cp_token *token;
21857 tree init;
21859 /* Peek at the next token. */
21860 token = cp_lexer_peek_token (parser->lexer);
21862 /* Let our caller know whether or not this initializer was
21863 parenthesized. */
21864 *is_direct_init = (token->type != CPP_EQ);
21865 /* Assume that the initializer is constant. */
21866 *non_constant_p = false;
21868 if (token->type == CPP_EQ)
21870 /* Consume the `='. */
21871 cp_lexer_consume_token (parser->lexer);
21872 /* Parse the initializer-clause. */
21873 init = cp_parser_initializer_clause (parser, non_constant_p);
21875 else if (token->type == CPP_OPEN_PAREN)
21877 vec<tree, va_gc> *vec;
21878 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21879 /*cast_p=*/false,
21880 /*allow_expansion_p=*/true,
21881 non_constant_p);
21882 if (vec == NULL)
21883 return error_mark_node;
21884 init = build_tree_list_vec (vec);
21885 release_tree_vector (vec);
21887 else if (token->type == CPP_OPEN_BRACE)
21889 cp_lexer_set_source_position (parser->lexer);
21890 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21891 init = cp_parser_braced_list (parser, non_constant_p);
21892 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21894 else
21896 /* Anything else is an error. */
21897 cp_parser_error (parser, "expected initializer");
21898 init = error_mark_node;
21901 if (check_for_bare_parameter_packs (init))
21902 init = error_mark_node;
21904 return init;
21907 /* Parse an initializer-clause.
21909 initializer-clause:
21910 assignment-expression
21911 braced-init-list
21913 Returns an expression representing the initializer.
21915 If the `assignment-expression' production is used the value
21916 returned is simply a representation for the expression.
21918 Otherwise, calls cp_parser_braced_list. */
21920 static cp_expr
21921 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21923 cp_expr initializer;
21925 /* Assume the expression is constant. */
21926 *non_constant_p = false;
21928 /* If it is not a `{', then we are looking at an
21929 assignment-expression. */
21930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21932 initializer
21933 = cp_parser_constant_expression (parser,
21934 /*allow_non_constant_p=*/true,
21935 non_constant_p);
21937 else
21938 initializer = cp_parser_braced_list (parser, non_constant_p);
21940 return initializer;
21943 /* Parse a brace-enclosed initializer list.
21945 braced-init-list:
21946 { initializer-list , [opt] }
21947 { designated-initializer-list , [opt] }
21950 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21951 the elements of the initializer-list (or NULL, if the last
21952 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21953 NULL_TREE. There is no way to detect whether or not the optional
21954 trailing `,' was provided. NON_CONSTANT_P is as for
21955 cp_parser_initializer. */
21957 static cp_expr
21958 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21960 tree initializer;
21961 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21963 /* Consume the `{' token. */
21964 matching_braces braces;
21965 braces.require_open (parser);
21966 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21967 initializer = make_node (CONSTRUCTOR);
21968 /* If it's not a `}', then there is a non-trivial initializer. */
21969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21971 /* Parse the initializer list. */
21972 CONSTRUCTOR_ELTS (initializer)
21973 = cp_parser_initializer_list (parser, non_constant_p);
21974 /* A trailing `,' token is allowed. */
21975 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21976 cp_lexer_consume_token (parser->lexer);
21978 else
21979 *non_constant_p = false;
21980 /* Now, there should be a trailing `}'. */
21981 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21982 braces.require_close (parser);
21983 TREE_TYPE (initializer) = init_list_type_node;
21985 cp_expr result (initializer);
21986 /* Build a location of the form:
21987 { ... }
21988 ^~~~~~~
21989 with caret==start at the open brace, finish at the close brace. */
21990 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21991 result.set_location (combined_loc);
21992 return result;
21995 /* Consume tokens up to, and including, the next non-nested closing `]'.
21996 Returns true iff we found a closing `]'. */
21998 static bool
21999 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22001 unsigned square_depth = 0;
22003 while (true)
22005 cp_token * token = cp_lexer_peek_token (parser->lexer);
22007 switch (token->type)
22009 case CPP_EOF:
22010 case CPP_PRAGMA_EOL:
22011 /* If we've run out of tokens, then there is no closing `]'. */
22012 return false;
22014 case CPP_OPEN_SQUARE:
22015 ++square_depth;
22016 break;
22018 case CPP_CLOSE_SQUARE:
22019 if (!square_depth--)
22021 cp_lexer_consume_token (parser->lexer);
22022 return true;
22024 break;
22026 default:
22027 break;
22030 /* Consume the token. */
22031 cp_lexer_consume_token (parser->lexer);
22035 /* Return true if we are looking at an array-designator, false otherwise. */
22037 static bool
22038 cp_parser_array_designator_p (cp_parser *parser)
22040 /* Consume the `['. */
22041 cp_lexer_consume_token (parser->lexer);
22043 cp_lexer_save_tokens (parser->lexer);
22045 /* Skip tokens until the next token is a closing square bracket.
22046 If we find the closing `]', and the next token is a `=', then
22047 we are looking at an array designator. */
22048 bool array_designator_p
22049 = (cp_parser_skip_to_closing_square_bracket (parser)
22050 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22052 /* Roll back the tokens we skipped. */
22053 cp_lexer_rollback_tokens (parser->lexer);
22055 return array_designator_p;
22058 /* Parse an initializer-list.
22060 initializer-list:
22061 initializer-clause ... [opt]
22062 initializer-list , initializer-clause ... [opt]
22064 C++2A Extension:
22066 designated-initializer-list:
22067 designated-initializer-clause
22068 designated-initializer-list , designated-initializer-clause
22070 designated-initializer-clause:
22071 designator brace-or-equal-initializer
22073 designator:
22074 . identifier
22076 GNU Extension:
22078 initializer-list:
22079 designation initializer-clause ...[opt]
22080 initializer-list , designation initializer-clause ...[opt]
22082 designation:
22083 . identifier =
22084 identifier :
22085 [ constant-expression ] =
22087 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22088 for the initializer. If the INDEX of the elt is non-NULL, it is the
22089 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22090 as for cp_parser_initializer. */
22092 static vec<constructor_elt, va_gc> *
22093 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22095 vec<constructor_elt, va_gc> *v = NULL;
22096 bool first_p = true;
22097 tree first_designator = NULL_TREE;
22099 /* Assume all of the expressions are constant. */
22100 *non_constant_p = false;
22102 /* Parse the rest of the list. */
22103 while (true)
22105 cp_token *token;
22106 tree designator;
22107 tree initializer;
22108 bool clause_non_constant_p;
22109 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22111 /* Handle the C++2A syntax, '. id ='. */
22112 if ((cxx_dialect >= cxx2a
22113 || cp_parser_allow_gnu_extensions_p (parser))
22114 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22115 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22116 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22117 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22118 == CPP_OPEN_BRACE)))
22120 if (cxx_dialect < cxx2a)
22121 pedwarn (loc, OPT_Wpedantic,
22122 "C++ designated initializers only available with "
22123 "-std=c++2a or -std=gnu++2a");
22124 /* Consume the `.'. */
22125 cp_lexer_consume_token (parser->lexer);
22126 /* Consume the identifier. */
22127 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22128 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22129 /* Consume the `='. */
22130 cp_lexer_consume_token (parser->lexer);
22132 /* Also, if the next token is an identifier and the following one is a
22133 colon, we are looking at the GNU designated-initializer
22134 syntax. */
22135 else if (cp_parser_allow_gnu_extensions_p (parser)
22136 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22137 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22138 == CPP_COLON))
22140 /* Warn the user that they are using an extension. */
22141 pedwarn (loc, OPT_Wpedantic,
22142 "ISO C++ does not allow GNU designated initializers");
22143 /* Consume the identifier. */
22144 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22145 /* Consume the `:'. */
22146 cp_lexer_consume_token (parser->lexer);
22148 /* Also handle C99 array designators, '[ const ] ='. */
22149 else if (cp_parser_allow_gnu_extensions_p (parser)
22150 && !c_dialect_objc ()
22151 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22153 /* In C++11, [ could start a lambda-introducer. */
22154 bool non_const = false;
22156 cp_parser_parse_tentatively (parser);
22158 if (!cp_parser_array_designator_p (parser))
22160 cp_parser_simulate_error (parser);
22161 designator = NULL_TREE;
22163 else
22165 designator = cp_parser_constant_expression (parser, true,
22166 &non_const);
22167 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22168 cp_parser_require (parser, CPP_EQ, RT_EQ);
22171 if (!cp_parser_parse_definitely (parser))
22172 designator = NULL_TREE;
22173 else if (non_const
22174 && (!require_potential_rvalue_constant_expression
22175 (designator)))
22176 designator = NULL_TREE;
22177 if (designator)
22178 /* Warn the user that they are using an extension. */
22179 pedwarn (loc, OPT_Wpedantic,
22180 "ISO C++ does not allow C99 designated initializers");
22182 else
22183 designator = NULL_TREE;
22185 if (first_p)
22187 first_designator = designator;
22188 first_p = false;
22190 else if (cxx_dialect >= cxx2a
22191 && first_designator != error_mark_node
22192 && (!first_designator != !designator))
22194 error_at (loc, "either all initializer clauses should be designated "
22195 "or none of them should be");
22196 first_designator = error_mark_node;
22198 else if (cxx_dialect < cxx2a && !first_designator)
22199 first_designator = designator;
22201 /* Parse the initializer. */
22202 initializer = cp_parser_initializer_clause (parser,
22203 &clause_non_constant_p);
22204 /* If any clause is non-constant, so is the entire initializer. */
22205 if (clause_non_constant_p)
22206 *non_constant_p = true;
22208 /* If we have an ellipsis, this is an initializer pack
22209 expansion. */
22210 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22212 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22214 /* Consume the `...'. */
22215 cp_lexer_consume_token (parser->lexer);
22217 if (designator && cxx_dialect >= cxx2a)
22218 error_at (loc,
22219 "%<...%> not allowed in designated initializer list");
22221 /* Turn the initializer into an initializer expansion. */
22222 initializer = make_pack_expansion (initializer);
22225 /* Add it to the vector. */
22226 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22228 /* If the next token is not a comma, we have reached the end of
22229 the list. */
22230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22231 break;
22233 /* Peek at the next token. */
22234 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22235 /* If the next token is a `}', then we're still done. An
22236 initializer-clause can have a trailing `,' after the
22237 initializer-list and before the closing `}'. */
22238 if (token->type == CPP_CLOSE_BRACE)
22239 break;
22241 /* Consume the `,' token. */
22242 cp_lexer_consume_token (parser->lexer);
22245 /* The same identifier shall not appear in multiple designators
22246 of a designated-initializer-list. */
22247 if (first_designator)
22249 unsigned int i;
22250 tree designator, val;
22251 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22252 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22254 if (IDENTIFIER_MARKED (designator))
22256 error_at (EXPR_LOC_OR_LOC (val, input_location),
22257 "%<.%s%> designator used multiple times in "
22258 "the same initializer list",
22259 IDENTIFIER_POINTER (designator));
22260 (*v)[i].index = NULL_TREE;
22262 else
22263 IDENTIFIER_MARKED (designator) = 1;
22265 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22266 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22267 IDENTIFIER_MARKED (designator) = 0;
22270 return v;
22273 /* Classes [gram.class] */
22275 /* Parse a class-name.
22277 class-name:
22278 identifier
22279 template-id
22281 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22282 to indicate that names looked up in dependent types should be
22283 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22284 keyword has been used to indicate that the name that appears next
22285 is a template. TAG_TYPE indicates the explicit tag given before
22286 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22287 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22288 is the class being defined in a class-head. If ENUM_OK is TRUE,
22289 enum-names are also accepted.
22291 Returns the TYPE_DECL representing the class. */
22293 static tree
22294 cp_parser_class_name (cp_parser *parser,
22295 bool typename_keyword_p,
22296 bool template_keyword_p,
22297 enum tag_types tag_type,
22298 bool check_dependency_p,
22299 bool class_head_p,
22300 bool is_declaration,
22301 bool enum_ok)
22303 tree decl;
22304 tree scope;
22305 bool typename_p;
22306 cp_token *token;
22307 tree identifier = NULL_TREE;
22309 /* All class-names start with an identifier. */
22310 token = cp_lexer_peek_token (parser->lexer);
22311 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22313 cp_parser_error (parser, "expected class-name");
22314 return error_mark_node;
22317 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22318 to a template-id, so we save it here. */
22319 scope = parser->scope;
22320 if (scope == error_mark_node)
22321 return error_mark_node;
22323 /* Any name names a type if we're following the `typename' keyword
22324 in a qualified name where the enclosing scope is type-dependent. */
22325 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22326 && dependent_type_p (scope));
22327 /* Handle the common case (an identifier, but not a template-id)
22328 efficiently. */
22329 if (token->type == CPP_NAME
22330 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22332 cp_token *identifier_token;
22333 bool ambiguous_p;
22335 /* Look for the identifier. */
22336 identifier_token = cp_lexer_peek_token (parser->lexer);
22337 ambiguous_p = identifier_token->error_reported;
22338 identifier = cp_parser_identifier (parser);
22339 /* If the next token isn't an identifier, we are certainly not
22340 looking at a class-name. */
22341 if (identifier == error_mark_node)
22342 decl = error_mark_node;
22343 /* If we know this is a type-name, there's no need to look it
22344 up. */
22345 else if (typename_p)
22346 decl = identifier;
22347 else
22349 tree ambiguous_decls;
22350 /* If we already know that this lookup is ambiguous, then
22351 we've already issued an error message; there's no reason
22352 to check again. */
22353 if (ambiguous_p)
22355 cp_parser_simulate_error (parser);
22356 return error_mark_node;
22358 /* If the next token is a `::', then the name must be a type
22359 name.
22361 [basic.lookup.qual]
22363 During the lookup for a name preceding the :: scope
22364 resolution operator, object, function, and enumerator
22365 names are ignored. */
22366 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22367 tag_type = scope_type;
22368 /* Look up the name. */
22369 decl = cp_parser_lookup_name (parser, identifier,
22370 tag_type,
22371 /*is_template=*/false,
22372 /*is_namespace=*/false,
22373 check_dependency_p,
22374 &ambiguous_decls,
22375 identifier_token->location);
22376 if (ambiguous_decls)
22378 if (cp_parser_parsing_tentatively (parser))
22379 cp_parser_simulate_error (parser);
22380 return error_mark_node;
22384 else
22386 /* Try a template-id. */
22387 decl = cp_parser_template_id (parser, template_keyword_p,
22388 check_dependency_p,
22389 tag_type,
22390 is_declaration);
22391 if (decl == error_mark_node)
22392 return error_mark_node;
22395 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22397 /* If this is a typename, create a TYPENAME_TYPE. */
22398 if (typename_p && decl != error_mark_node)
22400 decl = make_typename_type (scope, decl, typename_type,
22401 /*complain=*/tf_error);
22402 if (decl != error_mark_node)
22403 decl = TYPE_NAME (decl);
22406 decl = strip_using_decl (decl);
22408 /* Check to see that it is really the name of a class. */
22409 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22410 && identifier_p (TREE_OPERAND (decl, 0))
22411 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22412 /* Situations like this:
22414 template <typename T> struct A {
22415 typename T::template X<int>::I i;
22418 are problematic. Is `T::template X<int>' a class-name? The
22419 standard does not seem to be definitive, but there is no other
22420 valid interpretation of the following `::'. Therefore, those
22421 names are considered class-names. */
22423 decl = make_typename_type (scope, decl, tag_type, tf_error);
22424 if (decl != error_mark_node)
22425 decl = TYPE_NAME (decl);
22427 else if (TREE_CODE (decl) != TYPE_DECL
22428 || TREE_TYPE (decl) == error_mark_node
22429 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22430 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22431 /* In Objective-C 2.0, a classname followed by '.' starts a
22432 dot-syntax expression, and it's not a type-name. */
22433 || (c_dialect_objc ()
22434 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22435 && objc_is_class_name (decl)))
22436 decl = error_mark_node;
22438 if (decl == error_mark_node)
22439 cp_parser_error (parser, "expected class-name");
22440 else if (identifier && !parser->scope)
22441 maybe_note_name_used_in_class (identifier, decl);
22443 return decl;
22446 /* Parse a class-specifier.
22448 class-specifier:
22449 class-head { member-specification [opt] }
22451 Returns the TREE_TYPE representing the class. */
22453 static tree
22454 cp_parser_class_specifier_1 (cp_parser* parser)
22456 tree type;
22457 tree attributes = NULL_TREE;
22458 bool nested_name_specifier_p;
22459 unsigned saved_num_template_parameter_lists;
22460 bool saved_in_function_body;
22461 unsigned char in_statement;
22462 bool in_switch_statement_p;
22463 bool saved_in_unbraced_linkage_specification_p;
22464 tree old_scope = NULL_TREE;
22465 tree scope = NULL_TREE;
22466 cp_token *closing_brace;
22468 push_deferring_access_checks (dk_no_deferred);
22470 /* Parse the class-head. */
22471 type = cp_parser_class_head (parser,
22472 &nested_name_specifier_p);
22473 /* If the class-head was a semantic disaster, skip the entire body
22474 of the class. */
22475 if (!type)
22477 cp_parser_skip_to_end_of_block_or_statement (parser);
22478 pop_deferring_access_checks ();
22479 return error_mark_node;
22482 /* Look for the `{'. */
22483 matching_braces braces;
22484 if (!braces.require_open (parser))
22486 pop_deferring_access_checks ();
22487 return error_mark_node;
22490 cp_ensure_no_omp_declare_simd (parser);
22491 cp_ensure_no_oacc_routine (parser);
22493 /* Issue an error message if type-definitions are forbidden here. */
22494 cp_parser_check_type_definition (parser);
22495 /* Remember that we are defining one more class. */
22496 ++parser->num_classes_being_defined;
22497 /* Inside the class, surrounding template-parameter-lists do not
22498 apply. */
22499 saved_num_template_parameter_lists
22500 = parser->num_template_parameter_lists;
22501 parser->num_template_parameter_lists = 0;
22502 /* We are not in a function body. */
22503 saved_in_function_body = parser->in_function_body;
22504 parser->in_function_body = false;
22505 /* Or in a loop. */
22506 in_statement = parser->in_statement;
22507 parser->in_statement = 0;
22508 /* Or in a switch. */
22509 in_switch_statement_p = parser->in_switch_statement_p;
22510 parser->in_switch_statement_p = false;
22511 /* We are not immediately inside an extern "lang" block. */
22512 saved_in_unbraced_linkage_specification_p
22513 = parser->in_unbraced_linkage_specification_p;
22514 parser->in_unbraced_linkage_specification_p = false;
22516 // Associate constraints with the type.
22517 if (flag_concepts)
22518 type = associate_classtype_constraints (type);
22520 /* Start the class. */
22521 if (nested_name_specifier_p)
22523 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22524 old_scope = push_inner_scope (scope);
22526 type = begin_class_definition (type);
22528 if (type == error_mark_node)
22529 /* If the type is erroneous, skip the entire body of the class. */
22530 cp_parser_skip_to_closing_brace (parser);
22531 else
22532 /* Parse the member-specification. */
22533 cp_parser_member_specification_opt (parser);
22535 /* Look for the trailing `}'. */
22536 closing_brace = braces.require_close (parser);
22537 /* Look for trailing attributes to apply to this class. */
22538 if (cp_parser_allow_gnu_extensions_p (parser))
22539 attributes = cp_parser_gnu_attributes_opt (parser);
22540 if (type != error_mark_node)
22541 type = finish_struct (type, attributes);
22542 if (nested_name_specifier_p)
22543 pop_inner_scope (old_scope, scope);
22545 /* We've finished a type definition. Check for the common syntax
22546 error of forgetting a semicolon after the definition. We need to
22547 be careful, as we can't just check for not-a-semicolon and be done
22548 with it; the user might have typed:
22550 class X { } c = ...;
22551 class X { } *p = ...;
22553 and so forth. Instead, enumerate all the possible tokens that
22554 might follow this production; if we don't see one of them, then
22555 complain and silently insert the semicolon. */
22557 cp_token *token = cp_lexer_peek_token (parser->lexer);
22558 bool want_semicolon = true;
22560 if (cp_next_tokens_can_be_std_attribute_p (parser))
22561 /* Don't try to parse c++11 attributes here. As per the
22562 grammar, that should be a task for
22563 cp_parser_decl_specifier_seq. */
22564 want_semicolon = false;
22566 switch (token->type)
22568 case CPP_NAME:
22569 case CPP_SEMICOLON:
22570 case CPP_MULT:
22571 case CPP_AND:
22572 case CPP_OPEN_PAREN:
22573 case CPP_CLOSE_PAREN:
22574 case CPP_COMMA:
22575 want_semicolon = false;
22576 break;
22578 /* While it's legal for type qualifiers and storage class
22579 specifiers to follow type definitions in the grammar, only
22580 compiler testsuites contain code like that. Assume that if
22581 we see such code, then what we're really seeing is a case
22582 like:
22584 class X { }
22585 const <type> var = ...;
22589 class Y { }
22590 static <type> func (...) ...
22592 i.e. the qualifier or specifier applies to the next
22593 declaration. To do so, however, we need to look ahead one
22594 more token to see if *that* token is a type specifier.
22596 This code could be improved to handle:
22598 class Z { }
22599 static const <type> var = ...; */
22600 case CPP_KEYWORD:
22601 if (keyword_is_decl_specifier (token->keyword))
22603 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22605 /* Handling user-defined types here would be nice, but very
22606 tricky. */
22607 want_semicolon
22608 = (lookahead->type == CPP_KEYWORD
22609 && keyword_begins_type_specifier (lookahead->keyword));
22611 break;
22612 default:
22613 break;
22616 /* If we don't have a type, then something is very wrong and we
22617 shouldn't try to do anything clever. Likewise for not seeing the
22618 closing brace. */
22619 if (closing_brace && TYPE_P (type) && want_semicolon)
22621 /* Locate the closing brace. */
22622 cp_token_position prev
22623 = cp_lexer_previous_token_position (parser->lexer);
22624 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22625 location_t loc = prev_token->location;
22627 /* We want to suggest insertion of a ';' immediately *after* the
22628 closing brace, so, if we can, offset the location by 1 column. */
22629 location_t next_loc = loc;
22630 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22631 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22633 rich_location richloc (line_table, next_loc);
22635 /* If we successfully offset the location, suggest the fix-it. */
22636 if (next_loc != loc)
22637 richloc.add_fixit_insert_before (next_loc, ";");
22639 if (CLASSTYPE_DECLARED_CLASS (type))
22640 error_at (&richloc,
22641 "expected %<;%> after class definition");
22642 else if (TREE_CODE (type) == RECORD_TYPE)
22643 error_at (&richloc,
22644 "expected %<;%> after struct definition");
22645 else if (TREE_CODE (type) == UNION_TYPE)
22646 error_at (&richloc,
22647 "expected %<;%> after union definition");
22648 else
22649 gcc_unreachable ();
22651 /* Unget one token and smash it to look as though we encountered
22652 a semicolon in the input stream. */
22653 cp_lexer_set_token_position (parser->lexer, prev);
22654 token = cp_lexer_peek_token (parser->lexer);
22655 token->type = CPP_SEMICOLON;
22656 token->keyword = RID_MAX;
22660 /* If this class is not itself within the scope of another class,
22661 then we need to parse the bodies of all of the queued function
22662 definitions. Note that the queued functions defined in a class
22663 are not always processed immediately following the
22664 class-specifier for that class. Consider:
22666 struct A {
22667 struct B { void f() { sizeof (A); } };
22670 If `f' were processed before the processing of `A' were
22671 completed, there would be no way to compute the size of `A'.
22672 Note that the nesting we are interested in here is lexical --
22673 not the semantic nesting given by TYPE_CONTEXT. In particular,
22674 for:
22676 struct A { struct B; };
22677 struct A::B { void f() { } };
22679 there is no need to delay the parsing of `A::B::f'. */
22680 if (--parser->num_classes_being_defined == 0)
22682 tree decl;
22683 tree class_type = NULL_TREE;
22684 tree pushed_scope = NULL_TREE;
22685 unsigned ix;
22686 cp_default_arg_entry *e;
22687 tree save_ccp, save_ccr;
22689 if (any_erroneous_template_args_p (type))
22691 /* Skip default arguments, NSDMIs, etc, in order to improve
22692 error recovery (c++/71169, c++/71832). */
22693 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22694 vec_safe_truncate (unparsed_nsdmis, 0);
22695 vec_safe_truncate (unparsed_classes, 0);
22696 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22699 /* In a first pass, parse default arguments to the functions.
22700 Then, in a second pass, parse the bodies of the functions.
22701 This two-phased approach handles cases like:
22703 struct S {
22704 void f() { g(); }
22705 void g(int i = 3);
22709 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22711 decl = e->decl;
22712 /* If there are default arguments that have not yet been processed,
22713 take care of them now. */
22714 if (class_type != e->class_type)
22716 if (pushed_scope)
22717 pop_scope (pushed_scope);
22718 class_type = e->class_type;
22719 pushed_scope = push_scope (class_type);
22721 /* Make sure that any template parameters are in scope. */
22722 maybe_begin_member_template_processing (decl);
22723 /* Parse the default argument expressions. */
22724 cp_parser_late_parsing_default_args (parser, decl);
22725 /* Remove any template parameters from the symbol table. */
22726 maybe_end_member_template_processing ();
22728 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22729 /* Now parse any NSDMIs. */
22730 save_ccp = current_class_ptr;
22731 save_ccr = current_class_ref;
22732 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22734 if (class_type != DECL_CONTEXT (decl))
22736 if (pushed_scope)
22737 pop_scope (pushed_scope);
22738 class_type = DECL_CONTEXT (decl);
22739 pushed_scope = push_scope (class_type);
22741 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22742 cp_parser_late_parsing_nsdmi (parser, decl);
22744 vec_safe_truncate (unparsed_nsdmis, 0);
22745 current_class_ptr = save_ccp;
22746 current_class_ref = save_ccr;
22747 if (pushed_scope)
22748 pop_scope (pushed_scope);
22750 /* Now do some post-NSDMI bookkeeping. */
22751 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22752 after_nsdmi_defaulted_late_checks (class_type);
22753 vec_safe_truncate (unparsed_classes, 0);
22754 after_nsdmi_defaulted_late_checks (type);
22756 /* Now parse the body of the functions. */
22757 if (flag_openmp)
22759 /* OpenMP UDRs need to be parsed before all other functions. */
22760 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22761 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22762 cp_parser_late_parsing_for_member (parser, decl);
22763 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22764 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22765 cp_parser_late_parsing_for_member (parser, decl);
22767 else
22768 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22769 cp_parser_late_parsing_for_member (parser, decl);
22770 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22772 else
22773 vec_safe_push (unparsed_classes, type);
22775 /* Put back any saved access checks. */
22776 pop_deferring_access_checks ();
22778 /* Restore saved state. */
22779 parser->in_switch_statement_p = in_switch_statement_p;
22780 parser->in_statement = in_statement;
22781 parser->in_function_body = saved_in_function_body;
22782 parser->num_template_parameter_lists
22783 = saved_num_template_parameter_lists;
22784 parser->in_unbraced_linkage_specification_p
22785 = saved_in_unbraced_linkage_specification_p;
22787 return type;
22790 static tree
22791 cp_parser_class_specifier (cp_parser* parser)
22793 tree ret;
22794 timevar_push (TV_PARSE_STRUCT);
22795 ret = cp_parser_class_specifier_1 (parser);
22796 timevar_pop (TV_PARSE_STRUCT);
22797 return ret;
22800 /* Parse a class-head.
22802 class-head:
22803 class-key identifier [opt] base-clause [opt]
22804 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22805 class-key nested-name-specifier [opt] template-id
22806 base-clause [opt]
22808 class-virt-specifier:
22809 final
22811 GNU Extensions:
22812 class-key attributes identifier [opt] base-clause [opt]
22813 class-key attributes nested-name-specifier identifier base-clause [opt]
22814 class-key attributes nested-name-specifier [opt] template-id
22815 base-clause [opt]
22817 Upon return BASES is initialized to the list of base classes (or
22818 NULL, if there are none) in the same form returned by
22819 cp_parser_base_clause.
22821 Returns the TYPE of the indicated class. Sets
22822 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22823 involving a nested-name-specifier was used, and FALSE otherwise.
22825 Returns error_mark_node if this is not a class-head.
22827 Returns NULL_TREE if the class-head is syntactically valid, but
22828 semantically invalid in a way that means we should skip the entire
22829 body of the class. */
22831 static tree
22832 cp_parser_class_head (cp_parser* parser,
22833 bool* nested_name_specifier_p)
22835 tree nested_name_specifier;
22836 enum tag_types class_key;
22837 tree id = NULL_TREE;
22838 tree type = NULL_TREE;
22839 tree attributes;
22840 tree bases;
22841 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22842 bool template_id_p = false;
22843 bool qualified_p = false;
22844 bool invalid_nested_name_p = false;
22845 bool invalid_explicit_specialization_p = false;
22846 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22847 tree pushed_scope = NULL_TREE;
22848 unsigned num_templates;
22849 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22850 /* Assume no nested-name-specifier will be present. */
22851 *nested_name_specifier_p = false;
22852 /* Assume no template parameter lists will be used in defining the
22853 type. */
22854 num_templates = 0;
22855 parser->colon_corrects_to_scope_p = false;
22857 /* Look for the class-key. */
22858 class_key = cp_parser_class_key (parser);
22859 if (class_key == none_type)
22860 return error_mark_node;
22862 location_t class_head_start_location = input_location;
22864 /* Parse the attributes. */
22865 attributes = cp_parser_attributes_opt (parser);
22867 /* If the next token is `::', that is invalid -- but sometimes
22868 people do try to write:
22870 struct ::S {};
22872 Handle this gracefully by accepting the extra qualifier, and then
22873 issuing an error about it later if this really is a
22874 class-head. If it turns out just to be an elaborated type
22875 specifier, remain silent. */
22876 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22877 qualified_p = true;
22879 push_deferring_access_checks (dk_no_check);
22881 /* Determine the name of the class. Begin by looking for an
22882 optional nested-name-specifier. */
22883 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22884 nested_name_specifier
22885 = cp_parser_nested_name_specifier_opt (parser,
22886 /*typename_keyword_p=*/false,
22887 /*check_dependency_p=*/false,
22888 /*type_p=*/true,
22889 /*is_declaration=*/false);
22890 /* If there was a nested-name-specifier, then there *must* be an
22891 identifier. */
22893 cp_token *bad_template_keyword = NULL;
22895 if (nested_name_specifier)
22897 type_start_token = cp_lexer_peek_token (parser->lexer);
22898 /* Although the grammar says `identifier', it really means
22899 `class-name' or `template-name'. You are only allowed to
22900 define a class that has already been declared with this
22901 syntax.
22903 The proposed resolution for Core Issue 180 says that wherever
22904 you see `class T::X' you should treat `X' as a type-name.
22906 It is OK to define an inaccessible class; for example:
22908 class A { class B; };
22909 class A::B {};
22911 We do not know if we will see a class-name, or a
22912 template-name. We look for a class-name first, in case the
22913 class-name is a template-id; if we looked for the
22914 template-name first we would stop after the template-name. */
22915 cp_parser_parse_tentatively (parser);
22916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22917 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22918 type = cp_parser_class_name (parser,
22919 /*typename_keyword_p=*/false,
22920 /*template_keyword_p=*/false,
22921 class_type,
22922 /*check_dependency_p=*/false,
22923 /*class_head_p=*/true,
22924 /*is_declaration=*/false);
22925 /* If that didn't work, ignore the nested-name-specifier. */
22926 if (!cp_parser_parse_definitely (parser))
22928 invalid_nested_name_p = true;
22929 type_start_token = cp_lexer_peek_token (parser->lexer);
22930 id = cp_parser_identifier (parser);
22931 if (id == error_mark_node)
22932 id = NULL_TREE;
22934 /* If we could not find a corresponding TYPE, treat this
22935 declaration like an unqualified declaration. */
22936 if (type == error_mark_node)
22937 nested_name_specifier = NULL_TREE;
22938 /* Otherwise, count the number of templates used in TYPE and its
22939 containing scopes. */
22940 else
22942 tree scope;
22944 for (scope = TREE_TYPE (type);
22945 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22946 scope = get_containing_scope (scope))
22947 if (TYPE_P (scope)
22948 && CLASS_TYPE_P (scope)
22949 && CLASSTYPE_TEMPLATE_INFO (scope)
22950 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22951 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22952 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22953 ++num_templates;
22956 /* Otherwise, the identifier is optional. */
22957 else
22959 /* We don't know whether what comes next is a template-id,
22960 an identifier, or nothing at all. */
22961 cp_parser_parse_tentatively (parser);
22962 /* Check for a template-id. */
22963 type_start_token = cp_lexer_peek_token (parser->lexer);
22964 id = cp_parser_template_id (parser,
22965 /*template_keyword_p=*/false,
22966 /*check_dependency_p=*/true,
22967 class_key,
22968 /*is_declaration=*/true);
22969 /* If that didn't work, it could still be an identifier. */
22970 if (!cp_parser_parse_definitely (parser))
22972 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22974 type_start_token = cp_lexer_peek_token (parser->lexer);
22975 id = cp_parser_identifier (parser);
22977 else
22978 id = NULL_TREE;
22980 else
22982 template_id_p = true;
22983 ++num_templates;
22987 pop_deferring_access_checks ();
22989 if (id)
22991 cp_parser_check_for_invalid_template_id (parser, id,
22992 class_key,
22993 type_start_token->location);
22995 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22997 /* If it's not a `:' or a `{' then we can't really be looking at a
22998 class-head, since a class-head only appears as part of a
22999 class-specifier. We have to detect this situation before calling
23000 xref_tag, since that has irreversible side-effects. */
23001 if (!cp_parser_next_token_starts_class_definition_p (parser))
23003 cp_parser_error (parser, "expected %<{%> or %<:%>");
23004 type = error_mark_node;
23005 goto out;
23008 /* At this point, we're going ahead with the class-specifier, even
23009 if some other problem occurs. */
23010 cp_parser_commit_to_tentative_parse (parser);
23011 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23013 cp_parser_error (parser,
23014 "cannot specify %<override%> for a class");
23015 type = error_mark_node;
23016 goto out;
23018 /* Issue the error about the overly-qualified name now. */
23019 if (qualified_p)
23021 cp_parser_error (parser,
23022 "global qualification of class name is invalid");
23023 type = error_mark_node;
23024 goto out;
23026 else if (invalid_nested_name_p)
23028 cp_parser_error (parser,
23029 "qualified name does not name a class");
23030 type = error_mark_node;
23031 goto out;
23033 else if (nested_name_specifier)
23035 tree scope;
23037 if (bad_template_keyword)
23038 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23039 keyword template shall not appear at the top level. */
23040 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23041 "keyword %<template%> not allowed in class-head-name");
23043 /* Reject typedef-names in class heads. */
23044 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23046 error_at (type_start_token->location,
23047 "invalid class name in declaration of %qD",
23048 type);
23049 type = NULL_TREE;
23050 goto done;
23053 /* Figure out in what scope the declaration is being placed. */
23054 scope = current_scope ();
23055 /* If that scope does not contain the scope in which the
23056 class was originally declared, the program is invalid. */
23057 if (scope && !is_ancestor (scope, nested_name_specifier))
23059 if (at_namespace_scope_p ())
23060 error_at (type_start_token->location,
23061 "declaration of %qD in namespace %qD which does not "
23062 "enclose %qD",
23063 type, scope, nested_name_specifier);
23064 else
23065 error_at (type_start_token->location,
23066 "declaration of %qD in %qD which does not enclose %qD",
23067 type, scope, nested_name_specifier);
23068 type = NULL_TREE;
23069 goto done;
23071 /* [dcl.meaning]
23073 A declarator-id shall not be qualified except for the
23074 definition of a ... nested class outside of its class
23075 ... [or] the definition or explicit instantiation of a
23076 class member of a namespace outside of its namespace. */
23077 if (scope == nested_name_specifier)
23079 permerror (nested_name_specifier_token_start->location,
23080 "extra qualification not allowed");
23081 nested_name_specifier = NULL_TREE;
23082 num_templates = 0;
23085 /* An explicit-specialization must be preceded by "template <>". If
23086 it is not, try to recover gracefully. */
23087 if (at_namespace_scope_p ()
23088 && parser->num_template_parameter_lists == 0
23089 && !processing_template_parmlist
23090 && template_id_p)
23092 /* Build a location of this form:
23093 struct typename <ARGS>
23094 ^~~~~~~~~~~~~~~~~~~~~~
23095 with caret==start at the start token, and
23096 finishing at the end of the type. */
23097 location_t reported_loc
23098 = make_location (class_head_start_location,
23099 class_head_start_location,
23100 get_finish (type_start_token->location));
23101 rich_location richloc (line_table, reported_loc);
23102 richloc.add_fixit_insert_before (class_head_start_location,
23103 "template <> ");
23104 error_at (&richloc,
23105 "an explicit specialization must be preceded by"
23106 " %<template <>%>");
23107 invalid_explicit_specialization_p = true;
23108 /* Take the same action that would have been taken by
23109 cp_parser_explicit_specialization. */
23110 ++parser->num_template_parameter_lists;
23111 begin_specialization ();
23113 /* There must be no "return" statements between this point and the
23114 end of this function; set "type "to the correct return value and
23115 use "goto done;" to return. */
23116 /* Make sure that the right number of template parameters were
23117 present. */
23118 if (!cp_parser_check_template_parameters (parser, num_templates,
23119 template_id_p,
23120 type_start_token->location,
23121 /*declarator=*/NULL))
23123 /* If something went wrong, there is no point in even trying to
23124 process the class-definition. */
23125 type = NULL_TREE;
23126 goto done;
23129 /* Look up the type. */
23130 if (template_id_p)
23132 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23133 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23134 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23136 error_at (type_start_token->location,
23137 "function template %qD redeclared as a class template", id);
23138 type = error_mark_node;
23140 else
23142 type = TREE_TYPE (id);
23143 type = maybe_process_partial_specialization (type);
23145 /* Check the scope while we still know whether or not we had a
23146 nested-name-specifier. */
23147 if (type != error_mark_node)
23148 check_unqualified_spec_or_inst (type, type_start_token->location);
23150 if (nested_name_specifier)
23151 pushed_scope = push_scope (nested_name_specifier);
23153 else if (nested_name_specifier)
23155 tree class_type;
23157 /* Given:
23159 template <typename T> struct S { struct T };
23160 template <typename T> struct S<T>::T { };
23162 we will get a TYPENAME_TYPE when processing the definition of
23163 `S::T'. We need to resolve it to the actual type before we
23164 try to define it. */
23165 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23167 class_type = resolve_typename_type (TREE_TYPE (type),
23168 /*only_current_p=*/false);
23169 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23170 type = TYPE_NAME (class_type);
23171 else
23173 cp_parser_error (parser, "could not resolve typename type");
23174 type = error_mark_node;
23178 if (maybe_process_partial_specialization (TREE_TYPE (type))
23179 == error_mark_node)
23181 type = NULL_TREE;
23182 goto done;
23185 class_type = current_class_type;
23186 /* Enter the scope indicated by the nested-name-specifier. */
23187 pushed_scope = push_scope (nested_name_specifier);
23188 /* Get the canonical version of this type. */
23189 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23190 /* Call push_template_decl if it seems like we should be defining a
23191 template either from the template headers or the type we're
23192 defining, so that we diagnose both extra and missing headers. */
23193 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23194 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23195 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23197 type = push_template_decl (type);
23198 if (type == error_mark_node)
23200 type = NULL_TREE;
23201 goto done;
23205 type = TREE_TYPE (type);
23206 *nested_name_specifier_p = true;
23208 else /* The name is not a nested name. */
23210 /* If the class was unnamed, create a dummy name. */
23211 if (!id)
23212 id = make_anon_name ();
23213 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23214 ? ts_within_enclosing_non_class
23215 : ts_current);
23216 type = xref_tag (class_key, id, tag_scope,
23217 parser->num_template_parameter_lists);
23220 /* Indicate whether this class was declared as a `class' or as a
23221 `struct'. */
23222 if (TREE_CODE (type) == RECORD_TYPE)
23223 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23224 cp_parser_check_class_key (class_key, type);
23226 /* If this type was already complete, and we see another definition,
23227 that's an error. */
23228 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23230 error_at (type_start_token->location, "redefinition of %q#T",
23231 type);
23232 inform (location_of (type), "previous definition of %q#T",
23233 type);
23234 type = NULL_TREE;
23235 goto done;
23237 else if (type == error_mark_node)
23238 type = NULL_TREE;
23240 if (type)
23242 /* Apply attributes now, before any use of the class as a template
23243 argument in its base list. */
23244 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23245 fixup_attribute_variants (type);
23248 /* We will have entered the scope containing the class; the names of
23249 base classes should be looked up in that context. For example:
23251 struct A { struct B {}; struct C; };
23252 struct A::C : B {};
23254 is valid. */
23256 /* Get the list of base-classes, if there is one. */
23257 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23259 /* PR59482: enter the class scope so that base-specifiers are looked
23260 up correctly. */
23261 if (type)
23262 pushclass (type);
23263 bases = cp_parser_base_clause (parser);
23264 /* PR59482: get out of the previously pushed class scope so that the
23265 subsequent pops pop the right thing. */
23266 if (type)
23267 popclass ();
23269 else
23270 bases = NULL_TREE;
23272 /* If we're really defining a class, process the base classes.
23273 If they're invalid, fail. */
23274 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23275 xref_basetypes (type, bases);
23277 done:
23278 /* Leave the scope given by the nested-name-specifier. We will
23279 enter the class scope itself while processing the members. */
23280 if (pushed_scope)
23281 pop_scope (pushed_scope);
23283 if (invalid_explicit_specialization_p)
23285 end_specialization ();
23286 --parser->num_template_parameter_lists;
23289 if (type)
23290 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23291 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23292 CLASSTYPE_FINAL (type) = 1;
23293 out:
23294 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23295 return type;
23298 /* Parse a class-key.
23300 class-key:
23301 class
23302 struct
23303 union
23305 Returns the kind of class-key specified, or none_type to indicate
23306 error. */
23308 static enum tag_types
23309 cp_parser_class_key (cp_parser* parser)
23311 cp_token *token;
23312 enum tag_types tag_type;
23314 /* Look for the class-key. */
23315 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23316 if (!token)
23317 return none_type;
23319 /* Check to see if the TOKEN is a class-key. */
23320 tag_type = cp_parser_token_is_class_key (token);
23321 if (!tag_type)
23322 cp_parser_error (parser, "expected class-key");
23323 return tag_type;
23326 /* Parse a type-parameter-key.
23328 type-parameter-key:
23329 class
23330 typename
23333 static void
23334 cp_parser_type_parameter_key (cp_parser* parser)
23336 /* Look for the type-parameter-key. */
23337 enum tag_types tag_type = none_type;
23338 cp_token *token = cp_lexer_peek_token (parser->lexer);
23339 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23341 cp_lexer_consume_token (parser->lexer);
23342 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23343 /* typename is not allowed in a template template parameter
23344 by the standard until C++17. */
23345 pedwarn (token->location, OPT_Wpedantic,
23346 "ISO C++ forbids typename key in template template parameter;"
23347 " use -std=c++17 or -std=gnu++17");
23349 else
23350 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23352 return;
23355 /* Parse an (optional) member-specification.
23357 member-specification:
23358 member-declaration member-specification [opt]
23359 access-specifier : member-specification [opt] */
23361 static void
23362 cp_parser_member_specification_opt (cp_parser* parser)
23364 while (true)
23366 cp_token *token;
23367 enum rid keyword;
23369 /* Peek at the next token. */
23370 token = cp_lexer_peek_token (parser->lexer);
23371 /* If it's a `}', or EOF then we've seen all the members. */
23372 if (token->type == CPP_CLOSE_BRACE
23373 || token->type == CPP_EOF
23374 || token->type == CPP_PRAGMA_EOL)
23375 break;
23377 /* See if this token is a keyword. */
23378 keyword = token->keyword;
23379 switch (keyword)
23381 case RID_PUBLIC:
23382 case RID_PROTECTED:
23383 case RID_PRIVATE:
23384 /* Consume the access-specifier. */
23385 cp_lexer_consume_token (parser->lexer);
23386 /* Remember which access-specifier is active. */
23387 current_access_specifier = token->u.value;
23388 /* Look for the `:'. */
23389 cp_parser_require (parser, CPP_COLON, RT_COLON);
23390 break;
23392 default:
23393 /* Accept #pragmas at class scope. */
23394 if (token->type == CPP_PRAGMA)
23396 cp_parser_pragma (parser, pragma_member, NULL);
23397 break;
23400 /* Otherwise, the next construction must be a
23401 member-declaration. */
23402 cp_parser_member_declaration (parser);
23407 /* Parse a member-declaration.
23409 member-declaration:
23410 decl-specifier-seq [opt] member-declarator-list [opt] ;
23411 function-definition ; [opt]
23412 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23413 using-declaration
23414 template-declaration
23415 alias-declaration
23417 member-declarator-list:
23418 member-declarator
23419 member-declarator-list , member-declarator
23421 member-declarator:
23422 declarator pure-specifier [opt]
23423 declarator constant-initializer [opt]
23424 identifier [opt] : constant-expression
23426 GNU Extensions:
23428 member-declaration:
23429 __extension__ member-declaration
23431 member-declarator:
23432 declarator attributes [opt] pure-specifier [opt]
23433 declarator attributes [opt] constant-initializer [opt]
23434 identifier [opt] attributes [opt] : constant-expression
23436 C++0x Extensions:
23438 member-declaration:
23439 static_assert-declaration */
23441 static void
23442 cp_parser_member_declaration (cp_parser* parser)
23444 cp_decl_specifier_seq decl_specifiers;
23445 tree prefix_attributes;
23446 tree decl;
23447 int declares_class_or_enum;
23448 bool friend_p;
23449 cp_token *token = NULL;
23450 cp_token *decl_spec_token_start = NULL;
23451 cp_token *initializer_token_start = NULL;
23452 int saved_pedantic;
23453 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23455 /* Check for the `__extension__' keyword. */
23456 if (cp_parser_extension_opt (parser, &saved_pedantic))
23458 /* Recurse. */
23459 cp_parser_member_declaration (parser);
23460 /* Restore the old value of the PEDANTIC flag. */
23461 pedantic = saved_pedantic;
23463 return;
23466 /* Check for a template-declaration. */
23467 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23469 /* An explicit specialization here is an error condition, and we
23470 expect the specialization handler to detect and report this. */
23471 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23472 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23473 cp_parser_explicit_specialization (parser);
23474 else
23475 cp_parser_template_declaration (parser, /*member_p=*/true);
23477 return;
23479 /* Check for a template introduction. */
23480 else if (cp_parser_template_declaration_after_export (parser, true))
23481 return;
23483 /* Check for a using-declaration. */
23484 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23486 if (cxx_dialect < cxx11)
23488 /* Parse the using-declaration. */
23489 cp_parser_using_declaration (parser,
23490 /*access_declaration_p=*/false);
23491 return;
23493 else
23495 tree decl;
23496 bool alias_decl_expected;
23497 cp_parser_parse_tentatively (parser);
23498 decl = cp_parser_alias_declaration (parser);
23499 /* Note that if we actually see the '=' token after the
23500 identifier, cp_parser_alias_declaration commits the
23501 tentative parse. In that case, we really expect an
23502 alias-declaration. Otherwise, we expect a using
23503 declaration. */
23504 alias_decl_expected =
23505 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23506 cp_parser_parse_definitely (parser);
23508 if (alias_decl_expected)
23509 finish_member_declaration (decl);
23510 else
23511 cp_parser_using_declaration (parser,
23512 /*access_declaration_p=*/false);
23513 return;
23517 /* Check for @defs. */
23518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23520 tree ivar, member;
23521 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23522 ivar = ivar_chains;
23523 while (ivar)
23525 member = ivar;
23526 ivar = TREE_CHAIN (member);
23527 TREE_CHAIN (member) = NULL_TREE;
23528 finish_member_declaration (member);
23530 return;
23533 /* If the next token is `static_assert' we have a static assertion. */
23534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23536 cp_parser_static_assert (parser, /*member_p=*/true);
23537 return;
23540 parser->colon_corrects_to_scope_p = false;
23542 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23543 goto out;
23545 /* Parse the decl-specifier-seq. */
23546 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23547 cp_parser_decl_specifier_seq (parser,
23548 CP_PARSER_FLAGS_OPTIONAL,
23549 &decl_specifiers,
23550 &declares_class_or_enum);
23551 /* Check for an invalid type-name. */
23552 if (!decl_specifiers.any_type_specifiers_p
23553 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23554 goto out;
23555 /* If there is no declarator, then the decl-specifier-seq should
23556 specify a type. */
23557 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23559 /* If there was no decl-specifier-seq, and the next token is a
23560 `;', then we have something like:
23562 struct S { ; };
23564 [class.mem]
23566 Each member-declaration shall declare at least one member
23567 name of the class. */
23568 if (!decl_specifiers.any_specifiers_p)
23570 cp_token *token = cp_lexer_peek_token (parser->lexer);
23571 if (!in_system_header_at (token->location))
23573 gcc_rich_location richloc (token->location);
23574 richloc.add_fixit_remove ();
23575 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23578 else
23580 tree type;
23582 /* See if this declaration is a friend. */
23583 friend_p = cp_parser_friend_p (&decl_specifiers);
23584 /* If there were decl-specifiers, check to see if there was
23585 a class-declaration. */
23586 type = check_tag_decl (&decl_specifiers,
23587 /*explicit_type_instantiation_p=*/false);
23588 /* Nested classes have already been added to the class, but
23589 a `friend' needs to be explicitly registered. */
23590 if (friend_p)
23592 /* If the `friend' keyword was present, the friend must
23593 be introduced with a class-key. */
23594 if (!declares_class_or_enum && cxx_dialect < cxx11)
23595 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23596 "in C++03 a class-key must be used "
23597 "when declaring a friend");
23598 /* In this case:
23600 template <typename T> struct A {
23601 friend struct A<T>::B;
23604 A<T>::B will be represented by a TYPENAME_TYPE, and
23605 therefore not recognized by check_tag_decl. */
23606 if (!type)
23608 type = decl_specifiers.type;
23609 if (type && TREE_CODE (type) == TYPE_DECL)
23610 type = TREE_TYPE (type);
23612 if (!type || !TYPE_P (type))
23613 error_at (decl_spec_token_start->location,
23614 "friend declaration does not name a class or "
23615 "function");
23616 else
23617 make_friend_class (current_class_type, type,
23618 /*complain=*/true);
23620 /* If there is no TYPE, an error message will already have
23621 been issued. */
23622 else if (!type || type == error_mark_node)
23624 /* An anonymous aggregate has to be handled specially; such
23625 a declaration really declares a data member (with a
23626 particular type), as opposed to a nested class. */
23627 else if (ANON_AGGR_TYPE_P (type))
23629 /* C++11 9.5/6. */
23630 if (decl_specifiers.storage_class != sc_none)
23631 error_at (decl_spec_token_start->location,
23632 "a storage class on an anonymous aggregate "
23633 "in class scope is not allowed");
23635 /* Remove constructors and such from TYPE, now that we
23636 know it is an anonymous aggregate. */
23637 fixup_anonymous_aggr (type);
23638 /* And make the corresponding data member. */
23639 decl = build_decl (decl_spec_token_start->location,
23640 FIELD_DECL, NULL_TREE, type);
23641 /* Add it to the class. */
23642 finish_member_declaration (decl);
23644 else
23645 cp_parser_check_access_in_redeclaration
23646 (TYPE_NAME (type),
23647 decl_spec_token_start->location);
23650 else
23652 bool assume_semicolon = false;
23654 /* Clear attributes from the decl_specifiers but keep them
23655 around as prefix attributes that apply them to the entity
23656 being declared. */
23657 prefix_attributes = decl_specifiers.attributes;
23658 decl_specifiers.attributes = NULL_TREE;
23660 /* See if these declarations will be friends. */
23661 friend_p = cp_parser_friend_p (&decl_specifiers);
23663 /* Keep going until we hit the `;' at the end of the
23664 declaration. */
23665 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23667 tree attributes = NULL_TREE;
23668 tree first_attribute;
23669 tree initializer;
23670 bool named_bitfld = false;
23672 /* Peek at the next token. */
23673 token = cp_lexer_peek_token (parser->lexer);
23675 /* The following code wants to know early if it is a bit-field
23676 or some other declaration. Attributes can appear before
23677 the `:' token. Skip over them without consuming any tokens
23678 to peek if they are followed by `:'. */
23679 if (cp_next_tokens_can_be_attribute_p (parser)
23680 || (token->type == CPP_NAME
23681 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23682 && (named_bitfld = true)))
23684 size_t n
23685 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23686 token = cp_lexer_peek_nth_token (parser->lexer, n);
23689 /* Check for a bitfield declaration. */
23690 if (token->type == CPP_COLON
23691 || (token->type == CPP_NAME
23692 && token == cp_lexer_peek_token (parser->lexer)
23693 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23694 && (named_bitfld = true)))
23696 tree identifier;
23697 tree width;
23698 tree late_attributes = NULL_TREE;
23700 if (named_bitfld)
23701 identifier = cp_parser_identifier (parser);
23702 else
23703 identifier = NULL_TREE;
23705 /* Look for attributes that apply to the bitfield. */
23706 attributes = cp_parser_attributes_opt (parser);
23708 /* Consume the `:' token. */
23709 cp_lexer_consume_token (parser->lexer);
23711 /* Get the width of the bitfield. */
23712 width = cp_parser_constant_expression (parser, false, NULL,
23713 cxx_dialect >= cxx11);
23715 /* In C++2A and as extension for C++11 and above we allow
23716 default member initializers for bit-fields. */
23717 initializer = NULL_TREE;
23718 if (cxx_dialect >= cxx11
23719 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23720 || cp_lexer_next_token_is (parser->lexer,
23721 CPP_OPEN_BRACE)))
23723 location_t loc
23724 = cp_lexer_peek_token (parser->lexer)->location;
23725 if (cxx_dialect < cxx2a
23726 && !in_system_header_at (loc)
23727 && identifier != NULL_TREE)
23728 pedwarn (loc, 0,
23729 "default member initializers for bit-fields "
23730 "only available with -std=c++2a or "
23731 "-std=gnu++2a");
23733 initializer = cp_parser_save_nsdmi (parser);
23734 if (identifier == NULL_TREE)
23736 error_at (loc, "default member initializer for "
23737 "unnamed bit-field");
23738 initializer = NULL_TREE;
23741 else
23743 /* Look for attributes that apply to the bitfield after
23744 the `:' token and width. This is where GCC used to
23745 parse attributes in the past, pedwarn if there is
23746 a std attribute. */
23747 if (cp_next_tokens_can_be_std_attribute_p (parser))
23748 pedwarn (input_location, OPT_Wpedantic,
23749 "ISO C++ allows bit-field attributes only "
23750 "before the %<:%> token");
23752 late_attributes = cp_parser_attributes_opt (parser);
23755 attributes = attr_chainon (attributes, late_attributes);
23757 /* Remember which attributes are prefix attributes and
23758 which are not. */
23759 first_attribute = attributes;
23760 /* Combine the attributes. */
23761 attributes = attr_chainon (prefix_attributes, attributes);
23763 /* Create the bitfield declaration. */
23764 decl = grokbitfield (identifier
23765 ? make_id_declarator (NULL_TREE,
23766 identifier,
23767 sfk_none)
23768 : NULL,
23769 &decl_specifiers,
23770 width, initializer,
23771 attributes);
23773 else
23775 cp_declarator *declarator;
23776 tree asm_specification;
23777 int ctor_dtor_or_conv_p;
23779 /* Parse the declarator. */
23780 declarator
23781 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23782 &ctor_dtor_or_conv_p,
23783 /*parenthesized_p=*/NULL,
23784 /*member_p=*/true,
23785 friend_p);
23787 /* If something went wrong parsing the declarator, make sure
23788 that we at least consume some tokens. */
23789 if (declarator == cp_error_declarator)
23791 /* Skip to the end of the statement. */
23792 cp_parser_skip_to_end_of_statement (parser);
23793 /* If the next token is not a semicolon, that is
23794 probably because we just skipped over the body of
23795 a function. So, we consume a semicolon if
23796 present, but do not issue an error message if it
23797 is not present. */
23798 if (cp_lexer_next_token_is (parser->lexer,
23799 CPP_SEMICOLON))
23800 cp_lexer_consume_token (parser->lexer);
23801 goto out;
23804 if (declares_class_or_enum & 2)
23805 cp_parser_check_for_definition_in_return_type
23806 (declarator, decl_specifiers.type,
23807 decl_specifiers.locations[ds_type_spec]);
23809 /* Look for an asm-specification. */
23810 asm_specification = cp_parser_asm_specification_opt (parser);
23811 /* Look for attributes that apply to the declaration. */
23812 attributes = cp_parser_attributes_opt (parser);
23813 /* Remember which attributes are prefix attributes and
23814 which are not. */
23815 first_attribute = attributes;
23816 /* Combine the attributes. */
23817 attributes = attr_chainon (prefix_attributes, attributes);
23819 /* If it's an `=', then we have a constant-initializer or a
23820 pure-specifier. It is not correct to parse the
23821 initializer before registering the member declaration
23822 since the member declaration should be in scope while
23823 its initializer is processed. However, the rest of the
23824 front end does not yet provide an interface that allows
23825 us to handle this correctly. */
23826 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23828 /* In [class.mem]:
23830 A pure-specifier shall be used only in the declaration of
23831 a virtual function.
23833 A member-declarator can contain a constant-initializer
23834 only if it declares a static member of integral or
23835 enumeration type.
23837 Therefore, if the DECLARATOR is for a function, we look
23838 for a pure-specifier; otherwise, we look for a
23839 constant-initializer. When we call `grokfield', it will
23840 perform more stringent semantics checks. */
23841 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23842 if (function_declarator_p (declarator)
23843 || (decl_specifiers.type
23844 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23845 && declarator->kind == cdk_id
23846 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23847 == FUNCTION_TYPE)))
23848 initializer = cp_parser_pure_specifier (parser);
23849 else if (decl_specifiers.storage_class != sc_static)
23850 initializer = cp_parser_save_nsdmi (parser);
23851 else if (cxx_dialect >= cxx11)
23853 bool nonconst;
23854 /* Don't require a constant rvalue in C++11, since we
23855 might want a reference constant. We'll enforce
23856 constancy later. */
23857 cp_lexer_consume_token (parser->lexer);
23858 /* Parse the initializer. */
23859 initializer = cp_parser_initializer_clause (parser,
23860 &nonconst);
23862 else
23863 /* Parse the initializer. */
23864 initializer = cp_parser_constant_initializer (parser);
23866 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23867 && !function_declarator_p (declarator))
23869 bool x;
23870 if (decl_specifiers.storage_class != sc_static)
23871 initializer = cp_parser_save_nsdmi (parser);
23872 else
23873 initializer = cp_parser_initializer (parser, &x, &x);
23875 /* Otherwise, there is no initializer. */
23876 else
23877 initializer = NULL_TREE;
23879 /* See if we are probably looking at a function
23880 definition. We are certainly not looking at a
23881 member-declarator. Calling `grokfield' has
23882 side-effects, so we must not do it unless we are sure
23883 that we are looking at a member-declarator. */
23884 if (cp_parser_token_starts_function_definition_p
23885 (cp_lexer_peek_token (parser->lexer)))
23887 /* The grammar does not allow a pure-specifier to be
23888 used when a member function is defined. (It is
23889 possible that this fact is an oversight in the
23890 standard, since a pure function may be defined
23891 outside of the class-specifier. */
23892 if (initializer && initializer_token_start)
23893 error_at (initializer_token_start->location,
23894 "pure-specifier on function-definition");
23895 decl = cp_parser_save_member_function_body (parser,
23896 &decl_specifiers,
23897 declarator,
23898 attributes);
23899 if (parser->fully_implicit_function_template_p)
23900 decl = finish_fully_implicit_template (parser, decl);
23901 /* If the member was not a friend, declare it here. */
23902 if (!friend_p)
23903 finish_member_declaration (decl);
23904 /* Peek at the next token. */
23905 token = cp_lexer_peek_token (parser->lexer);
23906 /* If the next token is a semicolon, consume it. */
23907 if (token->type == CPP_SEMICOLON)
23909 location_t semicolon_loc
23910 = cp_lexer_consume_token (parser->lexer)->location;
23911 gcc_rich_location richloc (semicolon_loc);
23912 richloc.add_fixit_remove ();
23913 warning_at (&richloc, OPT_Wextra_semi,
23914 "extra %<;%> after in-class "
23915 "function definition");
23917 goto out;
23919 else
23920 if (declarator->kind == cdk_function)
23921 declarator->id_loc = token->location;
23922 /* Create the declaration. */
23923 decl = grokfield (declarator, &decl_specifiers,
23924 initializer, /*init_const_expr_p=*/true,
23925 asm_specification, attributes);
23926 if (parser->fully_implicit_function_template_p)
23928 if (friend_p)
23929 finish_fully_implicit_template (parser, 0);
23930 else
23931 decl = finish_fully_implicit_template (parser, decl);
23935 cp_finalize_omp_declare_simd (parser, decl);
23936 cp_finalize_oacc_routine (parser, decl, false);
23938 /* Reset PREFIX_ATTRIBUTES. */
23939 if (attributes != error_mark_node)
23941 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23942 attributes = TREE_CHAIN (attributes);
23943 if (attributes)
23944 TREE_CHAIN (attributes) = NULL_TREE;
23947 /* If there is any qualification still in effect, clear it
23948 now; we will be starting fresh with the next declarator. */
23949 parser->scope = NULL_TREE;
23950 parser->qualifying_scope = NULL_TREE;
23951 parser->object_scope = NULL_TREE;
23952 /* If it's a `,', then there are more declarators. */
23953 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23955 cp_lexer_consume_token (parser->lexer);
23956 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23958 cp_token *token = cp_lexer_previous_token (parser->lexer);
23959 gcc_rich_location richloc (token->location);
23960 richloc.add_fixit_remove ();
23961 error_at (&richloc, "stray %<,%> at end of "
23962 "member declaration");
23965 /* If the next token isn't a `;', then we have a parse error. */
23966 else if (cp_lexer_next_token_is_not (parser->lexer,
23967 CPP_SEMICOLON))
23969 /* The next token might be a ways away from where the
23970 actual semicolon is missing. Find the previous token
23971 and use that for our error position. */
23972 cp_token *token = cp_lexer_previous_token (parser->lexer);
23973 gcc_rich_location richloc (token->location);
23974 richloc.add_fixit_insert_after (";");
23975 error_at (&richloc, "expected %<;%> at end of "
23976 "member declaration");
23978 /* Assume that the user meant to provide a semicolon. If
23979 we were to cp_parser_skip_to_end_of_statement, we might
23980 skip to a semicolon inside a member function definition
23981 and issue nonsensical error messages. */
23982 assume_semicolon = true;
23985 if (decl)
23987 /* Add DECL to the list of members. */
23988 if (!friend_p
23989 /* Explicitly include, eg, NSDMIs, for better error
23990 recovery (c++/58650). */
23991 || !DECL_DECLARES_FUNCTION_P (decl))
23992 finish_member_declaration (decl);
23994 if (TREE_CODE (decl) == FUNCTION_DECL)
23995 cp_parser_save_default_args (parser, decl);
23996 else if (TREE_CODE (decl) == FIELD_DECL
23997 && DECL_INITIAL (decl))
23998 /* Add DECL to the queue of NSDMI to be parsed later. */
23999 vec_safe_push (unparsed_nsdmis, decl);
24002 if (assume_semicolon)
24003 goto out;
24007 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24008 out:
24009 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24012 /* Parse a pure-specifier.
24014 pure-specifier:
24017 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24018 Otherwise, ERROR_MARK_NODE is returned. */
24020 static tree
24021 cp_parser_pure_specifier (cp_parser* parser)
24023 cp_token *token;
24025 /* Look for the `=' token. */
24026 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24027 return error_mark_node;
24028 /* Look for the `0' token. */
24029 token = cp_lexer_peek_token (parser->lexer);
24031 if (token->type == CPP_EOF
24032 || token->type == CPP_PRAGMA_EOL)
24033 return error_mark_node;
24035 cp_lexer_consume_token (parser->lexer);
24037 /* Accept = default or = delete in c++0x mode. */
24038 if (token->keyword == RID_DEFAULT
24039 || token->keyword == RID_DELETE)
24041 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24042 return token->u.value;
24045 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24046 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24048 cp_parser_error (parser,
24049 "invalid pure specifier (only %<= 0%> is allowed)");
24050 cp_parser_skip_to_end_of_statement (parser);
24051 return error_mark_node;
24053 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24055 error_at (token->location, "templates may not be %<virtual%>");
24056 return error_mark_node;
24059 return integer_zero_node;
24062 /* Parse a constant-initializer.
24064 constant-initializer:
24065 = constant-expression
24067 Returns a representation of the constant-expression. */
24069 static tree
24070 cp_parser_constant_initializer (cp_parser* parser)
24072 /* Look for the `=' token. */
24073 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24074 return error_mark_node;
24076 /* It is invalid to write:
24078 struct S { static const int i = { 7 }; };
24081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24083 cp_parser_error (parser,
24084 "a brace-enclosed initializer is not allowed here");
24085 /* Consume the opening brace. */
24086 matching_braces braces;
24087 braces.consume_open (parser);
24088 /* Skip the initializer. */
24089 cp_parser_skip_to_closing_brace (parser);
24090 /* Look for the trailing `}'. */
24091 braces.require_close (parser);
24093 return error_mark_node;
24096 return cp_parser_constant_expression (parser);
24099 /* Derived classes [gram.class.derived] */
24101 /* Parse a base-clause.
24103 base-clause:
24104 : base-specifier-list
24106 base-specifier-list:
24107 base-specifier ... [opt]
24108 base-specifier-list , base-specifier ... [opt]
24110 Returns a TREE_LIST representing the base-classes, in the order in
24111 which they were declared. The representation of each node is as
24112 described by cp_parser_base_specifier.
24114 In the case that no bases are specified, this function will return
24115 NULL_TREE, not ERROR_MARK_NODE. */
24117 static tree
24118 cp_parser_base_clause (cp_parser* parser)
24120 tree bases = NULL_TREE;
24122 /* Look for the `:' that begins the list. */
24123 cp_parser_require (parser, CPP_COLON, RT_COLON);
24125 /* Scan the base-specifier-list. */
24126 while (true)
24128 cp_token *token;
24129 tree base;
24130 bool pack_expansion_p = false;
24132 /* Look for the base-specifier. */
24133 base = cp_parser_base_specifier (parser);
24134 /* Look for the (optional) ellipsis. */
24135 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24137 /* Consume the `...'. */
24138 cp_lexer_consume_token (parser->lexer);
24140 pack_expansion_p = true;
24143 /* Add BASE to the front of the list. */
24144 if (base && base != error_mark_node)
24146 if (pack_expansion_p)
24147 /* Make this a pack expansion type. */
24148 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24150 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24152 TREE_CHAIN (base) = bases;
24153 bases = base;
24156 /* Peek at the next token. */
24157 token = cp_lexer_peek_token (parser->lexer);
24158 /* If it's not a comma, then the list is complete. */
24159 if (token->type != CPP_COMMA)
24160 break;
24161 /* Consume the `,'. */
24162 cp_lexer_consume_token (parser->lexer);
24165 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24166 base class had a qualified name. However, the next name that
24167 appears is certainly not qualified. */
24168 parser->scope = NULL_TREE;
24169 parser->qualifying_scope = NULL_TREE;
24170 parser->object_scope = NULL_TREE;
24172 return nreverse (bases);
24175 /* Parse a base-specifier.
24177 base-specifier:
24178 :: [opt] nested-name-specifier [opt] class-name
24179 virtual access-specifier [opt] :: [opt] nested-name-specifier
24180 [opt] class-name
24181 access-specifier virtual [opt] :: [opt] nested-name-specifier
24182 [opt] class-name
24184 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24185 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24186 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24187 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24189 static tree
24190 cp_parser_base_specifier (cp_parser* parser)
24192 cp_token *token;
24193 bool done = false;
24194 bool virtual_p = false;
24195 bool duplicate_virtual_error_issued_p = false;
24196 bool duplicate_access_error_issued_p = false;
24197 bool class_scope_p, template_p;
24198 tree access = access_default_node;
24199 tree type;
24201 /* Process the optional `virtual' and `access-specifier'. */
24202 while (!done)
24204 /* Peek at the next token. */
24205 token = cp_lexer_peek_token (parser->lexer);
24206 /* Process `virtual'. */
24207 switch (token->keyword)
24209 case RID_VIRTUAL:
24210 /* If `virtual' appears more than once, issue an error. */
24211 if (virtual_p && !duplicate_virtual_error_issued_p)
24213 cp_parser_error (parser,
24214 "%<virtual%> specified more than once in base-specifier");
24215 duplicate_virtual_error_issued_p = true;
24218 virtual_p = true;
24220 /* Consume the `virtual' token. */
24221 cp_lexer_consume_token (parser->lexer);
24223 break;
24225 case RID_PUBLIC:
24226 case RID_PROTECTED:
24227 case RID_PRIVATE:
24228 /* If more than one access specifier appears, issue an
24229 error. */
24230 if (access != access_default_node
24231 && !duplicate_access_error_issued_p)
24233 cp_parser_error (parser,
24234 "more than one access specifier in base-specifier");
24235 duplicate_access_error_issued_p = true;
24238 access = ridpointers[(int) token->keyword];
24240 /* Consume the access-specifier. */
24241 cp_lexer_consume_token (parser->lexer);
24243 break;
24245 default:
24246 done = true;
24247 break;
24250 /* It is not uncommon to see programs mechanically, erroneously, use
24251 the 'typename' keyword to denote (dependent) qualified types
24252 as base classes. */
24253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24255 token = cp_lexer_peek_token (parser->lexer);
24256 if (!processing_template_decl)
24257 error_at (token->location,
24258 "keyword %<typename%> not allowed outside of templates");
24259 else
24260 error_at (token->location,
24261 "keyword %<typename%> not allowed in this context "
24262 "(the base class is implicitly a type)");
24263 cp_lexer_consume_token (parser->lexer);
24266 /* Look for the optional `::' operator. */
24267 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24268 /* Look for the nested-name-specifier. The simplest way to
24269 implement:
24271 [temp.res]
24273 The keyword `typename' is not permitted in a base-specifier or
24274 mem-initializer; in these contexts a qualified name that
24275 depends on a template-parameter is implicitly assumed to be a
24276 type name.
24278 is to pretend that we have seen the `typename' keyword at this
24279 point. */
24280 cp_parser_nested_name_specifier_opt (parser,
24281 /*typename_keyword_p=*/true,
24282 /*check_dependency_p=*/true,
24283 /*type_p=*/true,
24284 /*is_declaration=*/true);
24285 /* If the base class is given by a qualified name, assume that names
24286 we see are type names or templates, as appropriate. */
24287 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24288 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24290 if (!parser->scope
24291 && cp_lexer_next_token_is_decltype (parser->lexer))
24292 /* DR 950 allows decltype as a base-specifier. */
24293 type = cp_parser_decltype (parser);
24294 else
24296 /* Otherwise, look for the class-name. */
24297 type = cp_parser_class_name (parser,
24298 class_scope_p,
24299 template_p,
24300 typename_type,
24301 /*check_dependency_p=*/true,
24302 /*class_head_p=*/false,
24303 /*is_declaration=*/true);
24304 type = TREE_TYPE (type);
24307 if (type == error_mark_node)
24308 return error_mark_node;
24310 return finish_base_specifier (type, access, virtual_p);
24313 /* Exception handling [gram.exception] */
24315 /* Parse an (optional) noexcept-specification.
24317 noexcept-specification:
24318 noexcept ( constant-expression ) [opt]
24320 If no noexcept-specification is present, returns NULL_TREE.
24321 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24322 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24323 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24324 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24325 in which case a boolean condition is returned instead. */
24327 static tree
24328 cp_parser_noexcept_specification_opt (cp_parser* parser,
24329 bool require_constexpr,
24330 bool* consumed_expr,
24331 bool return_cond)
24333 cp_token *token;
24334 const char *saved_message;
24336 /* Peek at the next token. */
24337 token = cp_lexer_peek_token (parser->lexer);
24339 /* Is it a noexcept-specification? */
24340 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24342 tree expr;
24343 cp_lexer_consume_token (parser->lexer);
24345 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24347 matching_parens parens;
24348 parens.consume_open (parser);
24350 if (require_constexpr)
24352 /* Types may not be defined in an exception-specification. */
24353 saved_message = parser->type_definition_forbidden_message;
24354 parser->type_definition_forbidden_message
24355 = G_("types may not be defined in an exception-specification");
24357 expr = cp_parser_constant_expression (parser);
24359 /* Restore the saved message. */
24360 parser->type_definition_forbidden_message = saved_message;
24362 else
24364 expr = cp_parser_expression (parser);
24365 *consumed_expr = true;
24368 parens.require_close (parser);
24370 else
24372 expr = boolean_true_node;
24373 if (!require_constexpr)
24374 *consumed_expr = false;
24377 /* We cannot build a noexcept-spec right away because this will check
24378 that expr is a constexpr. */
24379 if (!return_cond)
24380 return build_noexcept_spec (expr, tf_warning_or_error);
24381 else
24382 return expr;
24384 else
24385 return NULL_TREE;
24388 /* Parse an (optional) exception-specification.
24390 exception-specification:
24391 throw ( type-id-list [opt] )
24393 Returns a TREE_LIST representing the exception-specification. The
24394 TREE_VALUE of each node is a type. */
24396 static tree
24397 cp_parser_exception_specification_opt (cp_parser* parser)
24399 cp_token *token;
24400 tree type_id_list;
24401 const char *saved_message;
24403 /* Peek at the next token. */
24404 token = cp_lexer_peek_token (parser->lexer);
24406 /* Is it a noexcept-specification? */
24407 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24408 false);
24409 if (type_id_list != NULL_TREE)
24410 return type_id_list;
24412 /* If it's not `throw', then there's no exception-specification. */
24413 if (!cp_parser_is_keyword (token, RID_THROW))
24414 return NULL_TREE;
24416 location_t loc = token->location;
24418 /* Consume the `throw'. */
24419 cp_lexer_consume_token (parser->lexer);
24421 /* Look for the `('. */
24422 matching_parens parens;
24423 parens.require_open (parser);
24425 /* Peek at the next token. */
24426 token = cp_lexer_peek_token (parser->lexer);
24427 /* If it's not a `)', then there is a type-id-list. */
24428 if (token->type != CPP_CLOSE_PAREN)
24430 /* Types may not be defined in an exception-specification. */
24431 saved_message = parser->type_definition_forbidden_message;
24432 parser->type_definition_forbidden_message
24433 = G_("types may not be defined in an exception-specification");
24434 /* Parse the type-id-list. */
24435 type_id_list = cp_parser_type_id_list (parser);
24436 /* Restore the saved message. */
24437 parser->type_definition_forbidden_message = saved_message;
24439 if (cxx_dialect >= cxx17)
24441 error_at (loc, "ISO C++17 does not allow dynamic exception "
24442 "specifications");
24443 type_id_list = NULL_TREE;
24445 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24446 warning_at (loc, OPT_Wdeprecated,
24447 "dynamic exception specifications are deprecated in "
24448 "C++11");
24450 /* In C++17, throw() is equivalent to noexcept (true). throw()
24451 is deprecated in C++11 and above as well, but is still widely used,
24452 so don't warn about it yet. */
24453 else if (cxx_dialect >= cxx17)
24454 type_id_list = noexcept_true_spec;
24455 else
24456 type_id_list = empty_except_spec;
24458 /* Look for the `)'. */
24459 parens.require_close (parser);
24461 return type_id_list;
24464 /* Parse an (optional) type-id-list.
24466 type-id-list:
24467 type-id ... [opt]
24468 type-id-list , type-id ... [opt]
24470 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24471 in the order that the types were presented. */
24473 static tree
24474 cp_parser_type_id_list (cp_parser* parser)
24476 tree types = NULL_TREE;
24478 while (true)
24480 cp_token *token;
24481 tree type;
24483 token = cp_lexer_peek_token (parser->lexer);
24485 /* Get the next type-id. */
24486 type = cp_parser_type_id (parser);
24487 /* Check for invalid 'auto'. */
24488 if (flag_concepts && type_uses_auto (type))
24490 error_at (token->location,
24491 "invalid use of %<auto%> in exception-specification");
24492 type = error_mark_node;
24494 /* Parse the optional ellipsis. */
24495 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24497 /* Consume the `...'. */
24498 cp_lexer_consume_token (parser->lexer);
24500 /* Turn the type into a pack expansion expression. */
24501 type = make_pack_expansion (type);
24503 /* Add it to the list. */
24504 types = add_exception_specifier (types, type, /*complain=*/1);
24505 /* Peek at the next token. */
24506 token = cp_lexer_peek_token (parser->lexer);
24507 /* If it is not a `,', we are done. */
24508 if (token->type != CPP_COMMA)
24509 break;
24510 /* Consume the `,'. */
24511 cp_lexer_consume_token (parser->lexer);
24514 return nreverse (types);
24517 /* Parse a try-block.
24519 try-block:
24520 try compound-statement handler-seq */
24522 static tree
24523 cp_parser_try_block (cp_parser* parser)
24525 tree try_block;
24527 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24528 if (parser->in_function_body
24529 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24530 error ("%<try%> in %<constexpr%> function");
24532 try_block = begin_try_block ();
24533 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24534 finish_try_block (try_block);
24535 cp_parser_handler_seq (parser);
24536 finish_handler_sequence (try_block);
24538 return try_block;
24541 /* Parse a function-try-block.
24543 function-try-block:
24544 try ctor-initializer [opt] function-body handler-seq */
24546 static void
24547 cp_parser_function_try_block (cp_parser* parser)
24549 tree compound_stmt;
24550 tree try_block;
24552 /* Look for the `try' keyword. */
24553 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24554 return;
24555 /* Let the rest of the front end know where we are. */
24556 try_block = begin_function_try_block (&compound_stmt);
24557 /* Parse the function-body. */
24558 cp_parser_ctor_initializer_opt_and_function_body
24559 (parser, /*in_function_try_block=*/true);
24560 /* We're done with the `try' part. */
24561 finish_function_try_block (try_block);
24562 /* Parse the handlers. */
24563 cp_parser_handler_seq (parser);
24564 /* We're done with the handlers. */
24565 finish_function_handler_sequence (try_block, compound_stmt);
24568 /* Parse a handler-seq.
24570 handler-seq:
24571 handler handler-seq [opt] */
24573 static void
24574 cp_parser_handler_seq (cp_parser* parser)
24576 while (true)
24578 cp_token *token;
24580 /* Parse the handler. */
24581 cp_parser_handler (parser);
24582 /* Peek at the next token. */
24583 token = cp_lexer_peek_token (parser->lexer);
24584 /* If it's not `catch' then there are no more handlers. */
24585 if (!cp_parser_is_keyword (token, RID_CATCH))
24586 break;
24590 /* Parse a handler.
24592 handler:
24593 catch ( exception-declaration ) compound-statement */
24595 static void
24596 cp_parser_handler (cp_parser* parser)
24598 tree handler;
24599 tree declaration;
24601 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24602 handler = begin_handler ();
24603 matching_parens parens;
24604 parens.require_open (parser);
24605 declaration = cp_parser_exception_declaration (parser);
24606 finish_handler_parms (declaration, handler);
24607 parens.require_close (parser);
24608 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24609 finish_handler (handler);
24612 /* Parse an exception-declaration.
24614 exception-declaration:
24615 type-specifier-seq declarator
24616 type-specifier-seq abstract-declarator
24617 type-specifier-seq
24620 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24621 ellipsis variant is used. */
24623 static tree
24624 cp_parser_exception_declaration (cp_parser* parser)
24626 cp_decl_specifier_seq type_specifiers;
24627 cp_declarator *declarator;
24628 const char *saved_message;
24630 /* If it's an ellipsis, it's easy to handle. */
24631 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24633 /* Consume the `...' token. */
24634 cp_lexer_consume_token (parser->lexer);
24635 return NULL_TREE;
24638 /* Types may not be defined in exception-declarations. */
24639 saved_message = parser->type_definition_forbidden_message;
24640 parser->type_definition_forbidden_message
24641 = G_("types may not be defined in exception-declarations");
24643 /* Parse the type-specifier-seq. */
24644 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24645 /*is_trailing_return=*/false,
24646 &type_specifiers);
24647 /* If it's a `)', then there is no declarator. */
24648 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24649 declarator = NULL;
24650 else
24651 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24652 /*ctor_dtor_or_conv_p=*/NULL,
24653 /*parenthesized_p=*/NULL,
24654 /*member_p=*/false,
24655 /*friend_p=*/false);
24657 /* Restore the saved message. */
24658 parser->type_definition_forbidden_message = saved_message;
24660 if (!type_specifiers.any_specifiers_p)
24661 return error_mark_node;
24663 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24666 /* Parse a throw-expression.
24668 throw-expression:
24669 throw assignment-expression [opt]
24671 Returns a THROW_EXPR representing the throw-expression. */
24673 static tree
24674 cp_parser_throw_expression (cp_parser* parser)
24676 tree expression;
24677 cp_token* token;
24679 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24680 token = cp_lexer_peek_token (parser->lexer);
24681 /* Figure out whether or not there is an assignment-expression
24682 following the "throw" keyword. */
24683 if (token->type == CPP_COMMA
24684 || token->type == CPP_SEMICOLON
24685 || token->type == CPP_CLOSE_PAREN
24686 || token->type == CPP_CLOSE_SQUARE
24687 || token->type == CPP_CLOSE_BRACE
24688 || token->type == CPP_COLON)
24689 expression = NULL_TREE;
24690 else
24691 expression = cp_parser_assignment_expression (parser);
24693 return build_throw (expression);
24696 /* GNU Extensions */
24698 /* Parse an (optional) asm-specification.
24700 asm-specification:
24701 asm ( string-literal )
24703 If the asm-specification is present, returns a STRING_CST
24704 corresponding to the string-literal. Otherwise, returns
24705 NULL_TREE. */
24707 static tree
24708 cp_parser_asm_specification_opt (cp_parser* parser)
24710 cp_token *token;
24711 tree asm_specification;
24713 /* Peek at the next token. */
24714 token = cp_lexer_peek_token (parser->lexer);
24715 /* If the next token isn't the `asm' keyword, then there's no
24716 asm-specification. */
24717 if (!cp_parser_is_keyword (token, RID_ASM))
24718 return NULL_TREE;
24720 /* Consume the `asm' token. */
24721 cp_lexer_consume_token (parser->lexer);
24722 /* Look for the `('. */
24723 matching_parens parens;
24724 parens.require_open (parser);
24726 /* Look for the string-literal. */
24727 asm_specification = cp_parser_string_literal (parser, false, false);
24729 /* Look for the `)'. */
24730 parens.require_close (parser);
24732 return asm_specification;
24735 /* Parse an asm-operand-list.
24737 asm-operand-list:
24738 asm-operand
24739 asm-operand-list , asm-operand
24741 asm-operand:
24742 string-literal ( expression )
24743 [ string-literal ] string-literal ( expression )
24745 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24746 each node is the expression. The TREE_PURPOSE is itself a
24747 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24748 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24749 is a STRING_CST for the string literal before the parenthesis. Returns
24750 ERROR_MARK_NODE if any of the operands are invalid. */
24752 static tree
24753 cp_parser_asm_operand_list (cp_parser* parser)
24755 tree asm_operands = NULL_TREE;
24756 bool invalid_operands = false;
24758 while (true)
24760 tree string_literal;
24761 tree expression;
24762 tree name;
24764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24766 /* Consume the `[' token. */
24767 cp_lexer_consume_token (parser->lexer);
24768 /* Read the operand name. */
24769 name = cp_parser_identifier (parser);
24770 if (name != error_mark_node)
24771 name = build_string (IDENTIFIER_LENGTH (name),
24772 IDENTIFIER_POINTER (name));
24773 /* Look for the closing `]'. */
24774 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24776 else
24777 name = NULL_TREE;
24778 /* Look for the string-literal. */
24779 string_literal = cp_parser_string_literal (parser, false, false);
24781 /* Look for the `('. */
24782 matching_parens parens;
24783 parens.require_open (parser);
24784 /* Parse the expression. */
24785 expression = cp_parser_expression (parser);
24786 /* Look for the `)'. */
24787 parens.require_close (parser);
24789 if (name == error_mark_node
24790 || string_literal == error_mark_node
24791 || expression == error_mark_node)
24792 invalid_operands = true;
24794 /* Add this operand to the list. */
24795 asm_operands = tree_cons (build_tree_list (name, string_literal),
24796 expression,
24797 asm_operands);
24798 /* If the next token is not a `,', there are no more
24799 operands. */
24800 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24801 break;
24802 /* Consume the `,'. */
24803 cp_lexer_consume_token (parser->lexer);
24806 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24809 /* Parse an asm-clobber-list.
24811 asm-clobber-list:
24812 string-literal
24813 asm-clobber-list , string-literal
24815 Returns a TREE_LIST, indicating the clobbers in the order that they
24816 appeared. The TREE_VALUE of each node is a STRING_CST. */
24818 static tree
24819 cp_parser_asm_clobber_list (cp_parser* parser)
24821 tree clobbers = NULL_TREE;
24823 while (true)
24825 tree string_literal;
24827 /* Look for the string literal. */
24828 string_literal = cp_parser_string_literal (parser, false, false);
24829 /* Add it to the list. */
24830 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24831 /* If the next token is not a `,', then the list is
24832 complete. */
24833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24834 break;
24835 /* Consume the `,' token. */
24836 cp_lexer_consume_token (parser->lexer);
24839 return clobbers;
24842 /* Parse an asm-label-list.
24844 asm-label-list:
24845 identifier
24846 asm-label-list , identifier
24848 Returns a TREE_LIST, indicating the labels in the order that they
24849 appeared. The TREE_VALUE of each node is a label. */
24851 static tree
24852 cp_parser_asm_label_list (cp_parser* parser)
24854 tree labels = NULL_TREE;
24856 while (true)
24858 tree identifier, label, name;
24860 /* Look for the identifier. */
24861 identifier = cp_parser_identifier (parser);
24862 if (!error_operand_p (identifier))
24864 label = lookup_label (identifier);
24865 if (TREE_CODE (label) == LABEL_DECL)
24867 TREE_USED (label) = 1;
24868 check_goto (label);
24869 name = build_string (IDENTIFIER_LENGTH (identifier),
24870 IDENTIFIER_POINTER (identifier));
24871 labels = tree_cons (name, label, labels);
24874 /* If the next token is not a `,', then the list is
24875 complete. */
24876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24877 break;
24878 /* Consume the `,' token. */
24879 cp_lexer_consume_token (parser->lexer);
24882 return nreverse (labels);
24885 /* Return TRUE iff the next tokens in the stream are possibly the
24886 beginning of a GNU extension attribute. */
24888 static bool
24889 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24891 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24894 /* Return TRUE iff the next tokens in the stream are possibly the
24895 beginning of a standard C++-11 attribute specifier. */
24897 static bool
24898 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24900 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24903 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24904 beginning of a standard C++-11 attribute specifier. */
24906 static bool
24907 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24909 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24911 return (cxx_dialect >= cxx11
24912 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24913 || (token->type == CPP_OPEN_SQUARE
24914 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24915 && token->type == CPP_OPEN_SQUARE)));
24918 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24919 beginning of a GNU extension attribute. */
24921 static bool
24922 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24924 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24926 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24929 /* Return true iff the next tokens can be the beginning of either a
24930 GNU attribute list, or a standard C++11 attribute sequence. */
24932 static bool
24933 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24935 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24936 || cp_next_tokens_can_be_std_attribute_p (parser));
24939 /* Return true iff the next Nth tokens can be the beginning of either
24940 a GNU attribute list, or a standard C++11 attribute sequence. */
24942 static bool
24943 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24945 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24946 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24949 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24950 of GNU attributes, or return NULL. */
24952 static tree
24953 cp_parser_attributes_opt (cp_parser *parser)
24955 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24956 return cp_parser_gnu_attributes_opt (parser);
24957 return cp_parser_std_attribute_spec_seq (parser);
24960 /* Parse an (optional) series of attributes.
24962 attributes:
24963 attributes attribute
24965 attribute:
24966 __attribute__ (( attribute-list [opt] ))
24968 The return value is as for cp_parser_gnu_attribute_list. */
24970 static tree
24971 cp_parser_gnu_attributes_opt (cp_parser* parser)
24973 tree attributes = NULL_TREE;
24975 temp_override<bool> cleanup
24976 (parser->auto_is_implicit_function_template_parm_p, false);
24978 while (true)
24980 cp_token *token;
24981 tree attribute_list;
24982 bool ok = true;
24984 /* Peek at the next token. */
24985 token = cp_lexer_peek_token (parser->lexer);
24986 /* If it's not `__attribute__', then we're done. */
24987 if (token->keyword != RID_ATTRIBUTE)
24988 break;
24990 /* Consume the `__attribute__' keyword. */
24991 cp_lexer_consume_token (parser->lexer);
24992 /* Look for the two `(' tokens. */
24993 matching_parens outer_parens;
24994 outer_parens.require_open (parser);
24995 matching_parens inner_parens;
24996 inner_parens.require_open (parser);
24998 /* Peek at the next token. */
24999 token = cp_lexer_peek_token (parser->lexer);
25000 if (token->type != CPP_CLOSE_PAREN)
25001 /* Parse the attribute-list. */
25002 attribute_list = cp_parser_gnu_attribute_list (parser);
25003 else
25004 /* If the next token is a `)', then there is no attribute
25005 list. */
25006 attribute_list = NULL;
25008 /* Look for the two `)' tokens. */
25009 if (!inner_parens.require_close (parser))
25010 ok = false;
25011 if (!outer_parens.require_close (parser))
25012 ok = false;
25013 if (!ok)
25014 cp_parser_skip_to_end_of_statement (parser);
25016 /* Add these new attributes to the list. */
25017 attributes = attr_chainon (attributes, attribute_list);
25020 return attributes;
25023 /* Parse a GNU attribute-list.
25025 attribute-list:
25026 attribute
25027 attribute-list , attribute
25029 attribute:
25030 identifier
25031 identifier ( identifier )
25032 identifier ( identifier , expression-list )
25033 identifier ( expression-list )
25035 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25036 to an attribute. The TREE_PURPOSE of each node is the identifier
25037 indicating which attribute is in use. The TREE_VALUE represents
25038 the arguments, if any. */
25040 static tree
25041 cp_parser_gnu_attribute_list (cp_parser* parser)
25043 tree attribute_list = NULL_TREE;
25044 bool save_translate_strings_p = parser->translate_strings_p;
25046 parser->translate_strings_p = false;
25047 while (true)
25049 cp_token *token;
25050 tree identifier;
25051 tree attribute;
25053 /* Look for the identifier. We also allow keywords here; for
25054 example `__attribute__ ((const))' is legal. */
25055 token = cp_lexer_peek_token (parser->lexer);
25056 if (token->type == CPP_NAME
25057 || token->type == CPP_KEYWORD)
25059 tree arguments = NULL_TREE;
25061 /* Consume the token, but save it since we need it for the
25062 SIMD enabled function parsing. */
25063 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25065 /* Save away the identifier that indicates which attribute
25066 this is. */
25067 identifier = (token->type == CPP_KEYWORD)
25068 /* For keywords, use the canonical spelling, not the
25069 parsed identifier. */
25070 ? ridpointers[(int) token->keyword]
25071 : id_token->u.value;
25073 identifier = canonicalize_attr_name (identifier);
25074 attribute = build_tree_list (identifier, NULL_TREE);
25076 /* Peek at the next token. */
25077 token = cp_lexer_peek_token (parser->lexer);
25078 /* If it's an `(', then parse the attribute arguments. */
25079 if (token->type == CPP_OPEN_PAREN)
25081 vec<tree, va_gc> *vec;
25082 int attr_flag = (attribute_takes_identifier_p (identifier)
25083 ? id_attr : normal_attr);
25084 vec = cp_parser_parenthesized_expression_list
25085 (parser, attr_flag, /*cast_p=*/false,
25086 /*allow_expansion_p=*/false,
25087 /*non_constant_p=*/NULL);
25088 if (vec == NULL)
25089 arguments = error_mark_node;
25090 else
25092 arguments = build_tree_list_vec (vec);
25093 release_tree_vector (vec);
25095 /* Save the arguments away. */
25096 TREE_VALUE (attribute) = arguments;
25099 if (arguments != error_mark_node)
25101 /* Add this attribute to the list. */
25102 TREE_CHAIN (attribute) = attribute_list;
25103 attribute_list = attribute;
25106 token = cp_lexer_peek_token (parser->lexer);
25108 /* Now, look for more attributes. If the next token isn't a
25109 `,', we're done. */
25110 if (token->type != CPP_COMMA)
25111 break;
25113 /* Consume the comma and keep going. */
25114 cp_lexer_consume_token (parser->lexer);
25116 parser->translate_strings_p = save_translate_strings_p;
25118 /* We built up the list in reverse order. */
25119 return nreverse (attribute_list);
25122 /* Parse a standard C++11 attribute.
25124 The returned representation is a TREE_LIST which TREE_PURPOSE is
25125 the scoped name of the attribute, and the TREE_VALUE is its
25126 arguments list.
25128 Note that the scoped name of the attribute is itself a TREE_LIST
25129 which TREE_PURPOSE is the namespace of the attribute, and
25130 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25131 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25132 and which TREE_PURPOSE is directly the attribute name.
25134 Clients of the attribute code should use get_attribute_namespace
25135 and get_attribute_name to get the actual namespace and name of
25136 attributes, regardless of their being GNU or C++11 attributes.
25138 attribute:
25139 attribute-token attribute-argument-clause [opt]
25141 attribute-token:
25142 identifier
25143 attribute-scoped-token
25145 attribute-scoped-token:
25146 attribute-namespace :: identifier
25148 attribute-namespace:
25149 identifier
25151 attribute-argument-clause:
25152 ( balanced-token-seq )
25154 balanced-token-seq:
25155 balanced-token [opt]
25156 balanced-token-seq balanced-token
25158 balanced-token:
25159 ( balanced-token-seq )
25160 [ balanced-token-seq ]
25161 { balanced-token-seq }. */
25163 static tree
25164 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25166 tree attribute, attr_id = NULL_TREE, arguments;
25167 cp_token *token;
25169 temp_override<bool> cleanup
25170 (parser->auto_is_implicit_function_template_parm_p, false);
25172 /* First, parse name of the attribute, a.k.a attribute-token. */
25174 token = cp_lexer_peek_token (parser->lexer);
25175 if (token->type == CPP_NAME)
25176 attr_id = token->u.value;
25177 else if (token->type == CPP_KEYWORD)
25178 attr_id = ridpointers[(int) token->keyword];
25179 else if (token->flags & NAMED_OP)
25180 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25182 if (attr_id == NULL_TREE)
25183 return NULL_TREE;
25185 cp_lexer_consume_token (parser->lexer);
25187 token = cp_lexer_peek_token (parser->lexer);
25188 if (token->type == CPP_SCOPE)
25190 /* We are seeing a scoped attribute token. */
25192 cp_lexer_consume_token (parser->lexer);
25193 if (attr_ns)
25194 error_at (token->location, "attribute using prefix used together "
25195 "with scoped attribute token");
25196 attr_ns = attr_id;
25198 token = cp_lexer_consume_token (parser->lexer);
25199 if (token->type == CPP_NAME)
25200 attr_id = token->u.value;
25201 else if (token->type == CPP_KEYWORD)
25202 attr_id = ridpointers[(int) token->keyword];
25203 else if (token->flags & NAMED_OP)
25204 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25205 else
25207 error_at (token->location,
25208 "expected an identifier for the attribute name");
25209 return error_mark_node;
25212 attr_id = canonicalize_attr_name (attr_id);
25213 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25214 NULL_TREE);
25215 token = cp_lexer_peek_token (parser->lexer);
25217 else if (attr_ns)
25218 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25219 NULL_TREE);
25220 else
25222 attr_id = canonicalize_attr_name (attr_id);
25223 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25224 NULL_TREE);
25225 /* C++11 noreturn attribute is equivalent to GNU's. */
25226 if (is_attribute_p ("noreturn", attr_id))
25227 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25228 /* C++14 deprecated attribute is equivalent to GNU's. */
25229 else if (is_attribute_p ("deprecated", attr_id))
25230 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25231 /* C++17 fallthrough attribute is equivalent to GNU's. */
25232 else if (is_attribute_p ("fallthrough", attr_id))
25233 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25234 /* Transactional Memory TS optimize_for_synchronized attribute is
25235 equivalent to GNU transaction_callable. */
25236 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25237 TREE_PURPOSE (attribute)
25238 = get_identifier ("transaction_callable");
25239 /* Transactional Memory attributes are GNU attributes. */
25240 else if (tm_attr_to_mask (attr_id))
25241 TREE_PURPOSE (attribute) = attr_id;
25244 /* Now parse the optional argument clause of the attribute. */
25246 if (token->type != CPP_OPEN_PAREN)
25247 return attribute;
25250 vec<tree, va_gc> *vec;
25251 int attr_flag = normal_attr;
25253 if (attr_ns == get_identifier ("gnu")
25254 && attribute_takes_identifier_p (attr_id))
25255 /* A GNU attribute that takes an identifier in parameter. */
25256 attr_flag = id_attr;
25258 vec = cp_parser_parenthesized_expression_list
25259 (parser, attr_flag, /*cast_p=*/false,
25260 /*allow_expansion_p=*/true,
25261 /*non_constant_p=*/NULL);
25262 if (vec == NULL)
25263 arguments = error_mark_node;
25264 else
25266 arguments = build_tree_list_vec (vec);
25267 release_tree_vector (vec);
25270 if (arguments == error_mark_node)
25271 attribute = error_mark_node;
25272 else
25273 TREE_VALUE (attribute) = arguments;
25276 return attribute;
25279 /* Check that the attribute ATTRIBUTE appears at most once in the
25280 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25281 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25282 isn't implemented yet in GCC. */
25284 static void
25285 cp_parser_check_std_attribute (tree attributes, tree attribute)
25287 if (attributes)
25289 tree name = get_attribute_name (attribute);
25290 if (is_attribute_p ("noreturn", name)
25291 && lookup_attribute ("noreturn", attributes))
25292 error ("attribute %<noreturn%> can appear at most once "
25293 "in an attribute-list");
25294 else if (is_attribute_p ("deprecated", name)
25295 && lookup_attribute ("deprecated", attributes))
25296 error ("attribute %<deprecated%> can appear at most once "
25297 "in an attribute-list");
25301 /* Parse a list of standard C++-11 attributes.
25303 attribute-list:
25304 attribute [opt]
25305 attribute-list , attribute[opt]
25306 attribute ...
25307 attribute-list , attribute ...
25310 static tree
25311 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25313 tree attributes = NULL_TREE, attribute = NULL_TREE;
25314 cp_token *token = NULL;
25316 while (true)
25318 attribute = cp_parser_std_attribute (parser, attr_ns);
25319 if (attribute == error_mark_node)
25320 break;
25321 if (attribute != NULL_TREE)
25323 cp_parser_check_std_attribute (attributes, attribute);
25324 TREE_CHAIN (attribute) = attributes;
25325 attributes = attribute;
25327 token = cp_lexer_peek_token (parser->lexer);
25328 if (token->type == CPP_ELLIPSIS)
25330 cp_lexer_consume_token (parser->lexer);
25331 if (attribute == NULL_TREE)
25332 error_at (token->location,
25333 "expected attribute before %<...%>");
25334 else
25336 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25337 if (pack == error_mark_node)
25338 return error_mark_node;
25339 TREE_VALUE (attribute) = pack;
25341 token = cp_lexer_peek_token (parser->lexer);
25343 if (token->type != CPP_COMMA)
25344 break;
25345 cp_lexer_consume_token (parser->lexer);
25347 attributes = nreverse (attributes);
25348 return attributes;
25351 /* Parse a standard C++-11 attribute specifier.
25353 attribute-specifier:
25354 [ [ attribute-using-prefix [opt] attribute-list ] ]
25355 alignment-specifier
25357 attribute-using-prefix:
25358 using attribute-namespace :
25360 alignment-specifier:
25361 alignas ( type-id ... [opt] )
25362 alignas ( alignment-expression ... [opt] ). */
25364 static tree
25365 cp_parser_std_attribute_spec (cp_parser *parser)
25367 tree attributes = NULL_TREE;
25368 cp_token *token = cp_lexer_peek_token (parser->lexer);
25370 if (token->type == CPP_OPEN_SQUARE
25371 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25373 tree attr_ns = NULL_TREE;
25375 cp_lexer_consume_token (parser->lexer);
25376 cp_lexer_consume_token (parser->lexer);
25378 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25380 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25381 if (token->type == CPP_NAME)
25382 attr_ns = token->u.value;
25383 else if (token->type == CPP_KEYWORD)
25384 attr_ns = ridpointers[(int) token->keyword];
25385 else if (token->flags & NAMED_OP)
25386 attr_ns = get_identifier (cpp_type2name (token->type,
25387 token->flags));
25388 if (attr_ns
25389 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25391 if (cxx_dialect < cxx17
25392 && !in_system_header_at (input_location))
25393 pedwarn (input_location, 0,
25394 "attribute using prefix only available "
25395 "with -std=c++17 or -std=gnu++17");
25397 cp_lexer_consume_token (parser->lexer);
25398 cp_lexer_consume_token (parser->lexer);
25399 cp_lexer_consume_token (parser->lexer);
25401 else
25402 attr_ns = NULL_TREE;
25405 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25407 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25408 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25409 cp_parser_skip_to_end_of_statement (parser);
25410 else
25411 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25412 when we are sure that we have actually parsed them. */
25413 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25415 else
25417 tree alignas_expr;
25419 /* Look for an alignment-specifier. */
25421 token = cp_lexer_peek_token (parser->lexer);
25423 if (token->type != CPP_KEYWORD
25424 || token->keyword != RID_ALIGNAS)
25425 return NULL_TREE;
25427 cp_lexer_consume_token (parser->lexer);
25428 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25430 matching_parens parens;
25431 if (!parens.require_open (parser))
25432 return error_mark_node;
25434 cp_parser_parse_tentatively (parser);
25435 alignas_expr = cp_parser_type_id (parser);
25437 if (!cp_parser_parse_definitely (parser))
25439 alignas_expr = cp_parser_assignment_expression (parser);
25440 if (alignas_expr == error_mark_node)
25441 cp_parser_skip_to_end_of_statement (parser);
25442 if (alignas_expr == NULL_TREE
25443 || alignas_expr == error_mark_node)
25444 return alignas_expr;
25447 alignas_expr = cxx_alignas_expr (alignas_expr);
25448 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25450 /* Handle alignas (pack...). */
25451 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25453 cp_lexer_consume_token (parser->lexer);
25454 alignas_expr = make_pack_expansion (alignas_expr);
25457 /* Something went wrong, so don't build the attribute. */
25458 if (alignas_expr == error_mark_node)
25459 return error_mark_node;
25461 if (!parens.require_close (parser))
25462 return error_mark_node;
25464 /* Build the C++-11 representation of an 'aligned'
25465 attribute. */
25466 attributes =
25467 build_tree_list (build_tree_list (get_identifier ("gnu"),
25468 get_identifier ("aligned")),
25469 alignas_expr);
25472 return attributes;
25475 /* Parse a standard C++-11 attribute-specifier-seq.
25477 attribute-specifier-seq:
25478 attribute-specifier-seq [opt] attribute-specifier
25481 static tree
25482 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25484 tree attr_specs = NULL_TREE;
25485 tree attr_last = NULL_TREE;
25487 while (true)
25489 tree attr_spec = cp_parser_std_attribute_spec (parser);
25490 if (attr_spec == NULL_TREE)
25491 break;
25492 if (attr_spec == error_mark_node)
25493 return error_mark_node;
25495 if (attr_last)
25496 TREE_CHAIN (attr_last) = attr_spec;
25497 else
25498 attr_specs = attr_last = attr_spec;
25499 attr_last = tree_last (attr_last);
25502 return attr_specs;
25505 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25506 return index of the first token after balanced-token, or N on failure. */
25508 static size_t
25509 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25511 size_t orig_n = n;
25512 int nparens = 0, nbraces = 0, nsquares = 0;
25514 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25516 case CPP_EOF:
25517 case CPP_PRAGMA_EOL:
25518 /* Ran out of tokens. */
25519 return orig_n;
25520 case CPP_OPEN_PAREN:
25521 ++nparens;
25522 break;
25523 case CPP_OPEN_BRACE:
25524 ++nbraces;
25525 break;
25526 case CPP_OPEN_SQUARE:
25527 ++nsquares;
25528 break;
25529 case CPP_CLOSE_PAREN:
25530 --nparens;
25531 break;
25532 case CPP_CLOSE_BRACE:
25533 --nbraces;
25534 break;
25535 case CPP_CLOSE_SQUARE:
25536 --nsquares;
25537 break;
25538 default:
25539 break;
25541 while (nparens || nbraces || nsquares);
25542 return n;
25545 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25546 return index of the first token after the GNU attribute tokens, or N on
25547 failure. */
25549 static size_t
25550 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25552 while (true)
25554 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25555 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25556 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25557 break;
25559 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25560 if (n2 == n + 2)
25561 break;
25562 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25563 break;
25564 n = n2 + 1;
25566 return n;
25569 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25570 next token), return index of the first token after the standard C++11
25571 attribute tokens, or N on failure. */
25573 static size_t
25574 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25576 while (true)
25578 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25579 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25581 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25582 if (n2 == n + 1)
25583 break;
25584 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25585 break;
25586 n = n2 + 1;
25588 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25589 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25591 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25592 if (n2 == n + 1)
25593 break;
25594 n = n2;
25596 else
25597 break;
25599 return n;
25602 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25603 as the next token), return index of the first token after the attribute
25604 tokens, or N on failure. */
25606 static size_t
25607 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25609 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25610 return cp_parser_skip_gnu_attributes_opt (parser, n);
25611 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25614 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25615 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25616 current value of the PEDANTIC flag, regardless of whether or not
25617 the `__extension__' keyword is present. The caller is responsible
25618 for restoring the value of the PEDANTIC flag. */
25620 static bool
25621 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25623 /* Save the old value of the PEDANTIC flag. */
25624 *saved_pedantic = pedantic;
25626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25628 /* Consume the `__extension__' token. */
25629 cp_lexer_consume_token (parser->lexer);
25630 /* We're not being pedantic while the `__extension__' keyword is
25631 in effect. */
25632 pedantic = 0;
25634 return true;
25637 return false;
25640 /* Parse a label declaration.
25642 label-declaration:
25643 __label__ label-declarator-seq ;
25645 label-declarator-seq:
25646 identifier , label-declarator-seq
25647 identifier */
25649 static void
25650 cp_parser_label_declaration (cp_parser* parser)
25652 /* Look for the `__label__' keyword. */
25653 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25655 while (true)
25657 tree identifier;
25659 /* Look for an identifier. */
25660 identifier = cp_parser_identifier (parser);
25661 /* If we failed, stop. */
25662 if (identifier == error_mark_node)
25663 break;
25664 /* Declare it as a label. */
25665 finish_label_decl (identifier);
25666 /* If the next token is a `;', stop. */
25667 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25668 break;
25669 /* Look for the `,' separating the label declarations. */
25670 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25673 /* Look for the final `;'. */
25674 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25677 // -------------------------------------------------------------------------- //
25678 // Requires Clause
25680 // Parse a requires clause.
25682 // requires-clause:
25683 // 'requires' logical-or-expression
25685 // The required logical-or-expression must be a constant expression. Note
25686 // that we don't check that the expression is constepxr here. We defer until
25687 // we analyze constraints and then, we only check atomic constraints.
25688 static tree
25689 cp_parser_requires_clause (cp_parser *parser)
25691 // Parse the requires clause so that it is not automatically folded.
25692 ++processing_template_decl;
25693 tree expr = cp_parser_binary_expression (parser, false, false,
25694 PREC_NOT_OPERATOR, NULL);
25695 if (check_for_bare_parameter_packs (expr))
25696 expr = error_mark_node;
25697 --processing_template_decl;
25698 return expr;
25701 // Optionally parse a requires clause:
25702 static tree
25703 cp_parser_requires_clause_opt (cp_parser *parser)
25705 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25706 if (tok->keyword != RID_REQUIRES)
25708 if (!flag_concepts && tok->type == CPP_NAME
25709 && tok->u.value == ridpointers[RID_REQUIRES])
25711 error_at (cp_lexer_peek_token (parser->lexer)->location,
25712 "%<requires%> only available with -fconcepts");
25713 /* Parse and discard the requires-clause. */
25714 cp_lexer_consume_token (parser->lexer);
25715 cp_parser_requires_clause (parser);
25717 return NULL_TREE;
25719 cp_lexer_consume_token (parser->lexer);
25720 return cp_parser_requires_clause (parser);
25724 /*---------------------------------------------------------------------------
25725 Requires expressions
25726 ---------------------------------------------------------------------------*/
25728 /* Parse a requires expression
25730 requirement-expression:
25731 'requires' requirement-parameter-list [opt] requirement-body */
25732 static tree
25733 cp_parser_requires_expression (cp_parser *parser)
25735 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25736 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25738 /* A requires-expression shall appear only within a concept
25739 definition or a requires-clause.
25741 TODO: Implement this diagnostic correctly. */
25742 if (!processing_template_decl)
25744 error_at (loc, "a requires expression cannot appear outside a template");
25745 cp_parser_skip_to_end_of_statement (parser);
25746 return error_mark_node;
25749 tree parms, reqs;
25751 /* Local parameters are delared as variables within the scope
25752 of the expression. They are not visible past the end of
25753 the expression. Expressions within the requires-expression
25754 are unevaluated. */
25755 struct scope_sentinel
25757 scope_sentinel ()
25759 ++cp_unevaluated_operand;
25760 begin_scope (sk_block, NULL_TREE);
25763 ~scope_sentinel ()
25765 pop_bindings_and_leave_scope ();
25766 --cp_unevaluated_operand;
25768 } s;
25770 /* Parse the optional parameter list. */
25771 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25773 parms = cp_parser_requirement_parameter_list (parser);
25774 if (parms == error_mark_node)
25775 return error_mark_node;
25777 else
25778 parms = NULL_TREE;
25780 /* Parse the requirement body. */
25781 reqs = cp_parser_requirement_body (parser);
25782 if (reqs == error_mark_node)
25783 return error_mark_node;
25786 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25787 the parm chain. */
25788 grokparms (parms, &parms);
25789 return finish_requires_expr (parms, reqs);
25792 /* Parse a parameterized requirement.
25794 requirement-parameter-list:
25795 '(' parameter-declaration-clause ')' */
25796 static tree
25797 cp_parser_requirement_parameter_list (cp_parser *parser)
25799 matching_parens parens;
25800 if (!parens.require_open (parser))
25801 return error_mark_node;
25803 tree parms = cp_parser_parameter_declaration_clause (parser);
25805 if (!parens.require_close (parser))
25806 return error_mark_node;
25808 return parms;
25811 /* Parse the body of a requirement.
25813 requirement-body:
25814 '{' requirement-list '}' */
25815 static tree
25816 cp_parser_requirement_body (cp_parser *parser)
25818 matching_braces braces;
25819 if (!braces.require_open (parser))
25820 return error_mark_node;
25822 tree reqs = cp_parser_requirement_list (parser);
25824 if (!braces.require_close (parser))
25825 return error_mark_node;
25827 return reqs;
25830 /* Parse a list of requirements.
25832 requirement-list:
25833 requirement
25834 requirement-list ';' requirement[opt] */
25835 static tree
25836 cp_parser_requirement_list (cp_parser *parser)
25838 tree result = NULL_TREE;
25839 while (true)
25841 tree req = cp_parser_requirement (parser);
25842 if (req == error_mark_node)
25843 return error_mark_node;
25845 result = tree_cons (NULL_TREE, req, result);
25847 /* If we see a semi-colon, consume it. */
25848 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25849 cp_lexer_consume_token (parser->lexer);
25851 /* Stop processing at the end of the list. */
25852 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25853 break;
25856 /* Reverse the order of requirements so they are analyzed in
25857 declaration order. */
25858 return nreverse (result);
25861 /* Parse a syntactic requirement or type requirement.
25863 requirement:
25864 simple-requirement
25865 compound-requirement
25866 type-requirement
25867 nested-requirement */
25868 static tree
25869 cp_parser_requirement (cp_parser *parser)
25871 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25872 return cp_parser_compound_requirement (parser);
25873 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25874 return cp_parser_type_requirement (parser);
25875 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25876 return cp_parser_nested_requirement (parser);
25877 else
25878 return cp_parser_simple_requirement (parser);
25881 /* Parse a simple requirement.
25883 simple-requirement:
25884 expression ';' */
25885 static tree
25886 cp_parser_simple_requirement (cp_parser *parser)
25888 tree expr = cp_parser_expression (parser, NULL, false, false);
25889 if (!expr || expr == error_mark_node)
25890 return error_mark_node;
25892 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25893 return error_mark_node;
25895 return finish_simple_requirement (expr);
25898 /* Parse a type requirement
25900 type-requirement
25901 nested-name-specifier [opt] required-type-name ';'
25903 required-type-name:
25904 type-name
25905 'template' [opt] simple-template-id */
25906 static tree
25907 cp_parser_type_requirement (cp_parser *parser)
25909 cp_lexer_consume_token (parser->lexer);
25911 // Save the scope before parsing name specifiers.
25912 tree saved_scope = parser->scope;
25913 tree saved_object_scope = parser->object_scope;
25914 tree saved_qualifying_scope = parser->qualifying_scope;
25915 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25916 cp_parser_nested_name_specifier_opt (parser,
25917 /*typename_keyword_p=*/true,
25918 /*check_dependency_p=*/false,
25919 /*type_p=*/true,
25920 /*is_declaration=*/false);
25922 tree type;
25923 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25925 cp_lexer_consume_token (parser->lexer);
25926 type = cp_parser_template_id (parser,
25927 /*template_keyword_p=*/true,
25928 /*check_dependency=*/false,
25929 /*tag_type=*/none_type,
25930 /*is_declaration=*/false);
25931 type = make_typename_type (parser->scope, type, typename_type,
25932 /*complain=*/tf_error);
25934 else
25935 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25937 if (TREE_CODE (type) == TYPE_DECL)
25938 type = TREE_TYPE (type);
25940 parser->scope = saved_scope;
25941 parser->object_scope = saved_object_scope;
25942 parser->qualifying_scope = saved_qualifying_scope;
25944 if (type == error_mark_node)
25945 cp_parser_skip_to_end_of_statement (parser);
25947 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25948 return error_mark_node;
25949 if (type == error_mark_node)
25950 return error_mark_node;
25952 return finish_type_requirement (type);
25955 /* Parse a compound requirement
25957 compound-requirement:
25958 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25959 static tree
25960 cp_parser_compound_requirement (cp_parser *parser)
25962 /* Parse an expression enclosed in '{ }'s. */
25963 matching_braces braces;
25964 if (!braces.require_open (parser))
25965 return error_mark_node;
25967 tree expr = cp_parser_expression (parser, NULL, false, false);
25968 if (!expr || expr == error_mark_node)
25969 return error_mark_node;
25971 if (!braces.require_close (parser))
25972 return error_mark_node;
25974 /* Parse the optional noexcept. */
25975 bool noexcept_p = false;
25976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25978 cp_lexer_consume_token (parser->lexer);
25979 noexcept_p = true;
25982 /* Parse the optional trailing return type. */
25983 tree type = NULL_TREE;
25984 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25986 cp_lexer_consume_token (parser->lexer);
25987 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25988 parser->in_result_type_constraint_p = true;
25989 type = cp_parser_trailing_type_id (parser);
25990 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25991 if (type == error_mark_node)
25992 return error_mark_node;
25995 return finish_compound_requirement (expr, type, noexcept_p);
25998 /* Parse a nested requirement. This is the same as a requires clause.
26000 nested-requirement:
26001 requires-clause */
26002 static tree
26003 cp_parser_nested_requirement (cp_parser *parser)
26005 cp_lexer_consume_token (parser->lexer);
26006 tree req = cp_parser_requires_clause (parser);
26007 if (req == error_mark_node)
26008 return error_mark_node;
26009 return finish_nested_requirement (req);
26012 /* Support Functions */
26014 /* Return the appropriate prefer_type argument for lookup_name_real based on
26015 tag_type and template_mem_access. */
26017 static inline int
26018 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26020 /* DR 141: When looking in the current enclosing context for a template-name
26021 after -> or ., only consider class templates. */
26022 if (template_mem_access)
26023 return 2;
26024 switch (tag_type)
26026 case none_type: return 0; // No preference.
26027 case scope_type: return 1; // Type or namespace.
26028 default: return 2; // Type only.
26032 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26033 NAME should have one of the representations used for an
26034 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26035 is returned. If PARSER->SCOPE is a dependent type, then a
26036 SCOPE_REF is returned.
26038 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26039 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26040 was formed. Abstractly, such entities should not be passed to this
26041 function, because they do not need to be looked up, but it is
26042 simpler to check for this special case here, rather than at the
26043 call-sites.
26045 In cases not explicitly covered above, this function returns a
26046 DECL, OVERLOAD, or baselink representing the result of the lookup.
26047 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26048 is returned.
26050 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26051 (e.g., "struct") that was used. In that case bindings that do not
26052 refer to types are ignored.
26054 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26055 ignored.
26057 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26058 are ignored.
26060 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26061 types.
26063 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26064 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26065 NULL_TREE otherwise. */
26067 static cp_expr
26068 cp_parser_lookup_name (cp_parser *parser, tree name,
26069 enum tag_types tag_type,
26070 bool is_template,
26071 bool is_namespace,
26072 bool check_dependency,
26073 tree *ambiguous_decls,
26074 location_t name_location)
26076 tree decl;
26077 tree object_type = parser->context->object_type;
26079 /* Assume that the lookup will be unambiguous. */
26080 if (ambiguous_decls)
26081 *ambiguous_decls = NULL_TREE;
26083 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26084 no longer valid. Note that if we are parsing tentatively, and
26085 the parse fails, OBJECT_TYPE will be automatically restored. */
26086 parser->context->object_type = NULL_TREE;
26088 if (name == error_mark_node)
26089 return error_mark_node;
26091 /* A template-id has already been resolved; there is no lookup to
26092 do. */
26093 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26094 return name;
26095 if (BASELINK_P (name))
26097 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26098 == TEMPLATE_ID_EXPR);
26099 return name;
26102 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26103 it should already have been checked to make sure that the name
26104 used matches the type being destroyed. */
26105 if (TREE_CODE (name) == BIT_NOT_EXPR)
26107 tree type;
26109 /* Figure out to which type this destructor applies. */
26110 if (parser->scope)
26111 type = parser->scope;
26112 else if (object_type)
26113 type = object_type;
26114 else
26115 type = current_class_type;
26116 /* If that's not a class type, there is no destructor. */
26117 if (!type || !CLASS_TYPE_P (type))
26118 return error_mark_node;
26120 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26121 lazily_declare_fn (sfk_destructor, type);
26123 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26124 return dtor;
26126 return error_mark_node;
26129 /* By this point, the NAME should be an ordinary identifier. If
26130 the id-expression was a qualified name, the qualifying scope is
26131 stored in PARSER->SCOPE at this point. */
26132 gcc_assert (identifier_p (name));
26134 /* Perform the lookup. */
26135 if (parser->scope)
26137 bool dependent_p;
26139 if (parser->scope == error_mark_node)
26140 return error_mark_node;
26142 /* If the SCOPE is dependent, the lookup must be deferred until
26143 the template is instantiated -- unless we are explicitly
26144 looking up names in uninstantiated templates. Even then, we
26145 cannot look up the name if the scope is not a class type; it
26146 might, for example, be a template type parameter. */
26147 dependent_p = (TYPE_P (parser->scope)
26148 && dependent_scope_p (parser->scope));
26149 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26150 && dependent_p)
26151 /* Defer lookup. */
26152 decl = error_mark_node;
26153 else
26155 tree pushed_scope = NULL_TREE;
26157 /* If PARSER->SCOPE is a dependent type, then it must be a
26158 class type, and we must not be checking dependencies;
26159 otherwise, we would have processed this lookup above. So
26160 that PARSER->SCOPE is not considered a dependent base by
26161 lookup_member, we must enter the scope here. */
26162 if (dependent_p)
26163 pushed_scope = push_scope (parser->scope);
26165 /* If the PARSER->SCOPE is a template specialization, it
26166 may be instantiated during name lookup. In that case,
26167 errors may be issued. Even if we rollback the current
26168 tentative parse, those errors are valid. */
26169 decl = lookup_qualified_name (parser->scope, name,
26170 prefer_type_arg (tag_type),
26171 /*complain=*/true);
26173 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26174 lookup result and the nested-name-specifier nominates a class C:
26175 * if the name specified after the nested-name-specifier, when
26176 looked up in C, is the injected-class-name of C (Clause 9), or
26177 * if the name specified after the nested-name-specifier is the
26178 same as the identifier or the simple-template-id's template-
26179 name in the last component of the nested-name-specifier,
26180 the name is instead considered to name the constructor of
26181 class C. [ Note: for example, the constructor is not an
26182 acceptable lookup result in an elaborated-type-specifier so
26183 the constructor would not be used in place of the
26184 injected-class-name. --end note ] Such a constructor name
26185 shall be used only in the declarator-id of a declaration that
26186 names a constructor or in a using-declaration. */
26187 if (tag_type == none_type
26188 && DECL_SELF_REFERENCE_P (decl)
26189 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26190 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26191 prefer_type_arg (tag_type),
26192 /*complain=*/true);
26194 /* If we have a single function from a using decl, pull it out. */
26195 if (TREE_CODE (decl) == OVERLOAD
26196 && !really_overloaded_fn (decl))
26197 decl = OVL_FUNCTION (decl);
26199 if (pushed_scope)
26200 pop_scope (pushed_scope);
26203 /* If the scope is a dependent type and either we deferred lookup or
26204 we did lookup but didn't find the name, rememeber the name. */
26205 if (decl == error_mark_node && TYPE_P (parser->scope)
26206 && dependent_type_p (parser->scope))
26208 if (tag_type)
26210 tree type;
26212 /* The resolution to Core Issue 180 says that `struct
26213 A::B' should be considered a type-name, even if `A'
26214 is dependent. */
26215 type = make_typename_type (parser->scope, name, tag_type,
26216 /*complain=*/tf_error);
26217 if (type != error_mark_node)
26218 decl = TYPE_NAME (type);
26220 else if (is_template
26221 && (cp_parser_next_token_ends_template_argument_p (parser)
26222 || cp_lexer_next_token_is (parser->lexer,
26223 CPP_CLOSE_PAREN)))
26224 decl = make_unbound_class_template (parser->scope,
26225 name, NULL_TREE,
26226 /*complain=*/tf_error);
26227 else
26228 decl = build_qualified_name (/*type=*/NULL_TREE,
26229 parser->scope, name,
26230 is_template);
26232 parser->qualifying_scope = parser->scope;
26233 parser->object_scope = NULL_TREE;
26235 else if (object_type)
26237 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26238 OBJECT_TYPE is not a class. */
26239 if (CLASS_TYPE_P (object_type))
26240 /* If the OBJECT_TYPE is a template specialization, it may
26241 be instantiated during name lookup. In that case, errors
26242 may be issued. Even if we rollback the current tentative
26243 parse, those errors are valid. */
26244 decl = lookup_member (object_type,
26245 name,
26246 /*protect=*/0,
26247 prefer_type_arg (tag_type),
26248 tf_warning_or_error);
26249 else
26250 decl = NULL_TREE;
26252 if (!decl)
26253 /* Look it up in the enclosing context. DR 141: When looking for a
26254 template-name after -> or ., only consider class templates. */
26255 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26256 /*nonclass=*/0,
26257 /*block_p=*/true, is_namespace, 0);
26258 if (object_type == unknown_type_node)
26259 /* The object is type-dependent, so we can't look anything up; we used
26260 this to get the DR 141 behavior. */
26261 object_type = NULL_TREE;
26262 parser->object_scope = object_type;
26263 parser->qualifying_scope = NULL_TREE;
26265 else
26267 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26268 /*nonclass=*/0,
26269 /*block_p=*/true, is_namespace, 0);
26270 parser->qualifying_scope = NULL_TREE;
26271 parser->object_scope = NULL_TREE;
26274 /* If the lookup failed, let our caller know. */
26275 if (!decl || decl == error_mark_node)
26276 return error_mark_node;
26278 /* Pull out the template from an injected-class-name (or multiple). */
26279 if (is_template)
26280 decl = maybe_get_template_decl_from_type_decl (decl);
26282 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26283 if (TREE_CODE (decl) == TREE_LIST)
26285 if (ambiguous_decls)
26286 *ambiguous_decls = decl;
26287 /* The error message we have to print is too complicated for
26288 cp_parser_error, so we incorporate its actions directly. */
26289 if (!cp_parser_simulate_error (parser))
26291 error_at (name_location, "reference to %qD is ambiguous",
26292 name);
26293 print_candidates (decl);
26295 return error_mark_node;
26298 gcc_assert (DECL_P (decl)
26299 || TREE_CODE (decl) == OVERLOAD
26300 || TREE_CODE (decl) == SCOPE_REF
26301 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26302 || BASELINK_P (decl));
26304 /* If we have resolved the name of a member declaration, check to
26305 see if the declaration is accessible. When the name resolves to
26306 set of overloaded functions, accessibility is checked when
26307 overload resolution is done.
26309 During an explicit instantiation, access is not checked at all,
26310 as per [temp.explicit]. */
26311 if (DECL_P (decl))
26312 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26314 maybe_record_typedef_use (decl);
26316 return cp_expr (decl, name_location);
26319 /* Like cp_parser_lookup_name, but for use in the typical case where
26320 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26321 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26323 static tree
26324 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26326 return cp_parser_lookup_name (parser, name,
26327 none_type,
26328 /*is_template=*/false,
26329 /*is_namespace=*/false,
26330 /*check_dependency=*/true,
26331 /*ambiguous_decls=*/NULL,
26332 location);
26335 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26336 the current context, return the TYPE_DECL. If TAG_NAME_P is
26337 true, the DECL indicates the class being defined in a class-head,
26338 or declared in an elaborated-type-specifier.
26340 Otherwise, return DECL. */
26342 static tree
26343 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26345 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26346 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26348 struct A {
26349 template <typename T> struct B;
26352 template <typename T> struct A::B {};
26354 Similarly, in an elaborated-type-specifier:
26356 namespace N { struct X{}; }
26358 struct A {
26359 template <typename T> friend struct N::X;
26362 However, if the DECL refers to a class type, and we are in
26363 the scope of the class, then the name lookup automatically
26364 finds the TYPE_DECL created by build_self_reference rather
26365 than a TEMPLATE_DECL. For example, in:
26367 template <class T> struct S {
26368 S s;
26371 there is no need to handle such case. */
26373 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26374 return DECL_TEMPLATE_RESULT (decl);
26376 return decl;
26379 /* If too many, or too few, template-parameter lists apply to the
26380 declarator, issue an error message. Returns TRUE if all went well,
26381 and FALSE otherwise. */
26383 static bool
26384 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26385 cp_declarator *declarator,
26386 location_t declarator_location)
26388 switch (declarator->kind)
26390 case cdk_id:
26392 unsigned num_templates = 0;
26393 tree scope = declarator->u.id.qualifying_scope;
26394 bool template_id_p = false;
26396 if (scope)
26397 num_templates = num_template_headers_for_class (scope);
26398 else if (TREE_CODE (declarator->u.id.unqualified_name)
26399 == TEMPLATE_ID_EXPR)
26401 /* If the DECLARATOR has the form `X<y>' then it uses one
26402 additional level of template parameters. */
26403 ++num_templates;
26404 template_id_p = true;
26407 return cp_parser_check_template_parameters
26408 (parser, num_templates, template_id_p, declarator_location,
26409 declarator);
26412 case cdk_function:
26413 case cdk_array:
26414 case cdk_pointer:
26415 case cdk_reference:
26416 case cdk_ptrmem:
26417 return (cp_parser_check_declarator_template_parameters
26418 (parser, declarator->declarator, declarator_location));
26420 case cdk_decomp:
26421 case cdk_error:
26422 return true;
26424 default:
26425 gcc_unreachable ();
26427 return false;
26430 /* NUM_TEMPLATES were used in the current declaration. If that is
26431 invalid, return FALSE and issue an error messages. Otherwise,
26432 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26433 declarator and we can print more accurate diagnostics. */
26435 static bool
26436 cp_parser_check_template_parameters (cp_parser* parser,
26437 unsigned num_templates,
26438 bool template_id_p,
26439 location_t location,
26440 cp_declarator *declarator)
26442 /* If there are the same number of template classes and parameter
26443 lists, that's OK. */
26444 if (parser->num_template_parameter_lists == num_templates)
26445 return true;
26446 /* If there are more, but only one more, and the name ends in an identifier,
26447 then we are declaring a primary template. That's OK too. */
26448 if (!template_id_p
26449 && parser->num_template_parameter_lists == num_templates + 1)
26450 return true;
26451 /* If there are more template classes than parameter lists, we have
26452 something like:
26454 template <class T> void S<T>::R<T>::f (); */
26455 if (parser->num_template_parameter_lists < num_templates)
26457 if (declarator && !current_function_decl)
26458 error_at (location, "specializing member %<%T::%E%> "
26459 "requires %<template<>%> syntax",
26460 declarator->u.id.qualifying_scope,
26461 declarator->u.id.unqualified_name);
26462 else if (declarator)
26463 error_at (location, "invalid declaration of %<%T::%E%>",
26464 declarator->u.id.qualifying_scope,
26465 declarator->u.id.unqualified_name);
26466 else
26467 error_at (location, "too few template-parameter-lists");
26468 return false;
26470 /* Otherwise, there are too many template parameter lists. We have
26471 something like:
26473 template <class T> template <class U> void S::f(); */
26474 error_at (location, "too many template-parameter-lists");
26475 return false;
26478 /* Parse an optional `::' token indicating that the following name is
26479 from the global namespace. If so, PARSER->SCOPE is set to the
26480 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26481 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26482 Returns the new value of PARSER->SCOPE, if the `::' token is
26483 present, and NULL_TREE otherwise. */
26485 static tree
26486 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26488 cp_token *token;
26490 /* Peek at the next token. */
26491 token = cp_lexer_peek_token (parser->lexer);
26492 /* If we're looking at a `::' token then we're starting from the
26493 global namespace, not our current location. */
26494 if (token->type == CPP_SCOPE)
26496 /* Consume the `::' token. */
26497 cp_lexer_consume_token (parser->lexer);
26498 /* Set the SCOPE so that we know where to start the lookup. */
26499 parser->scope = global_namespace;
26500 parser->qualifying_scope = global_namespace;
26501 parser->object_scope = NULL_TREE;
26503 return parser->scope;
26505 else if (!current_scope_valid_p)
26507 parser->scope = NULL_TREE;
26508 parser->qualifying_scope = NULL_TREE;
26509 parser->object_scope = NULL_TREE;
26512 return NULL_TREE;
26515 /* Returns TRUE if the upcoming token sequence is the start of a
26516 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26517 declarator is preceded by the `friend' specifier. */
26519 static bool
26520 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26522 bool constructor_p;
26523 bool outside_class_specifier_p;
26524 tree nested_name_specifier;
26525 cp_token *next_token;
26527 /* The common case is that this is not a constructor declarator, so
26528 try to avoid doing lots of work if at all possible. It's not
26529 valid declare a constructor at function scope. */
26530 if (parser->in_function_body)
26531 return false;
26532 /* And only certain tokens can begin a constructor declarator. */
26533 next_token = cp_lexer_peek_token (parser->lexer);
26534 if (next_token->type != CPP_NAME
26535 && next_token->type != CPP_SCOPE
26536 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26537 && next_token->type != CPP_TEMPLATE_ID)
26538 return false;
26540 /* Parse tentatively; we are going to roll back all of the tokens
26541 consumed here. */
26542 cp_parser_parse_tentatively (parser);
26543 /* Assume that we are looking at a constructor declarator. */
26544 constructor_p = true;
26546 /* Look for the optional `::' operator. */
26547 cp_parser_global_scope_opt (parser,
26548 /*current_scope_valid_p=*/false);
26549 /* Look for the nested-name-specifier. */
26550 nested_name_specifier
26551 = (cp_parser_nested_name_specifier_opt (parser,
26552 /*typename_keyword_p=*/false,
26553 /*check_dependency_p=*/false,
26554 /*type_p=*/false,
26555 /*is_declaration=*/false));
26557 outside_class_specifier_p = (!at_class_scope_p ()
26558 || !TYPE_BEING_DEFINED (current_class_type)
26559 || friend_p);
26561 /* Outside of a class-specifier, there must be a
26562 nested-name-specifier. Except in C++17 mode, where we
26563 might be declaring a guiding declaration. */
26564 if (!nested_name_specifier && outside_class_specifier_p
26565 && cxx_dialect < cxx17)
26566 constructor_p = false;
26567 else if (nested_name_specifier == error_mark_node)
26568 constructor_p = false;
26570 /* If we have a class scope, this is easy; DR 147 says that S::S always
26571 names the constructor, and no other qualified name could. */
26572 if (constructor_p && nested_name_specifier
26573 && CLASS_TYPE_P (nested_name_specifier))
26575 tree id = cp_parser_unqualified_id (parser,
26576 /*template_keyword_p=*/false,
26577 /*check_dependency_p=*/false,
26578 /*declarator_p=*/true,
26579 /*optional_p=*/false);
26580 if (is_overloaded_fn (id))
26581 id = DECL_NAME (get_first_fn (id));
26582 if (!constructor_name_p (id, nested_name_specifier))
26583 constructor_p = false;
26585 /* If we still think that this might be a constructor-declarator,
26586 look for a class-name. */
26587 else if (constructor_p)
26589 /* If we have:
26591 template <typename T> struct S {
26592 S();
26595 we must recognize that the nested `S' names a class. */
26596 if (cxx_dialect >= cxx17)
26597 cp_parser_parse_tentatively (parser);
26599 tree type_decl;
26600 type_decl = cp_parser_class_name (parser,
26601 /*typename_keyword_p=*/false,
26602 /*template_keyword_p=*/false,
26603 none_type,
26604 /*check_dependency_p=*/false,
26605 /*class_head_p=*/false,
26606 /*is_declaration=*/false);
26608 if (cxx_dialect >= cxx17
26609 && !cp_parser_parse_definitely (parser))
26611 type_decl = NULL_TREE;
26612 tree tmpl = cp_parser_template_name (parser,
26613 /*template_keyword*/false,
26614 /*check_dependency_p*/false,
26615 /*is_declaration*/false,
26616 none_type,
26617 /*is_identifier*/NULL);
26618 if (DECL_CLASS_TEMPLATE_P (tmpl)
26619 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26620 /* It's a deduction guide, return true. */;
26621 else
26622 cp_parser_simulate_error (parser);
26625 /* If there was no class-name, then this is not a constructor.
26626 Otherwise, if we are in a class-specifier and we aren't
26627 handling a friend declaration, check that its type matches
26628 current_class_type (c++/38313). Note: error_mark_node
26629 is left alone for error recovery purposes. */
26630 constructor_p = (!cp_parser_error_occurred (parser)
26631 && (outside_class_specifier_p
26632 || type_decl == NULL_TREE
26633 || type_decl == error_mark_node
26634 || same_type_p (current_class_type,
26635 TREE_TYPE (type_decl))));
26637 /* If we're still considering a constructor, we have to see a `(',
26638 to begin the parameter-declaration-clause, followed by either a
26639 `)', an `...', or a decl-specifier. We need to check for a
26640 type-specifier to avoid being fooled into thinking that:
26642 S (f) (int);
26644 is a constructor. (It is actually a function named `f' that
26645 takes one parameter (of type `int') and returns a value of type
26646 `S'. */
26647 if (constructor_p
26648 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26649 constructor_p = false;
26651 if (constructor_p
26652 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26653 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26654 /* A parameter declaration begins with a decl-specifier,
26655 which is either the "attribute" keyword, a storage class
26656 specifier, or (usually) a type-specifier. */
26657 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26659 tree type;
26660 tree pushed_scope = NULL_TREE;
26661 unsigned saved_num_template_parameter_lists;
26663 /* Names appearing in the type-specifier should be looked up
26664 in the scope of the class. */
26665 if (current_class_type)
26666 type = NULL_TREE;
26667 else if (type_decl)
26669 type = TREE_TYPE (type_decl);
26670 if (TREE_CODE (type) == TYPENAME_TYPE)
26672 type = resolve_typename_type (type,
26673 /*only_current_p=*/false);
26674 if (TREE_CODE (type) == TYPENAME_TYPE)
26676 cp_parser_abort_tentative_parse (parser);
26677 return false;
26680 pushed_scope = push_scope (type);
26683 /* Inside the constructor parameter list, surrounding
26684 template-parameter-lists do not apply. */
26685 saved_num_template_parameter_lists
26686 = parser->num_template_parameter_lists;
26687 parser->num_template_parameter_lists = 0;
26689 /* Look for the type-specifier. */
26690 cp_parser_type_specifier (parser,
26691 CP_PARSER_FLAGS_NONE,
26692 /*decl_specs=*/NULL,
26693 /*is_declarator=*/true,
26694 /*declares_class_or_enum=*/NULL,
26695 /*is_cv_qualifier=*/NULL);
26697 parser->num_template_parameter_lists
26698 = saved_num_template_parameter_lists;
26700 /* Leave the scope of the class. */
26701 if (pushed_scope)
26702 pop_scope (pushed_scope);
26704 constructor_p = !cp_parser_error_occurred (parser);
26708 /* We did not really want to consume any tokens. */
26709 cp_parser_abort_tentative_parse (parser);
26711 return constructor_p;
26714 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26715 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26716 they must be performed once we are in the scope of the function.
26718 Returns the function defined. */
26720 static tree
26721 cp_parser_function_definition_from_specifiers_and_declarator
26722 (cp_parser* parser,
26723 cp_decl_specifier_seq *decl_specifiers,
26724 tree attributes,
26725 const cp_declarator *declarator)
26727 tree fn;
26728 bool success_p;
26730 /* Begin the function-definition. */
26731 success_p = start_function (decl_specifiers, declarator, attributes);
26733 /* The things we're about to see are not directly qualified by any
26734 template headers we've seen thus far. */
26735 reset_specialization ();
26737 /* If there were names looked up in the decl-specifier-seq that we
26738 did not check, check them now. We must wait until we are in the
26739 scope of the function to perform the checks, since the function
26740 might be a friend. */
26741 perform_deferred_access_checks (tf_warning_or_error);
26743 if (success_p)
26745 cp_finalize_omp_declare_simd (parser, current_function_decl);
26746 parser->omp_declare_simd = NULL;
26747 cp_finalize_oacc_routine (parser, current_function_decl, true);
26748 parser->oacc_routine = NULL;
26751 if (!success_p)
26753 /* Skip the entire function. */
26754 cp_parser_skip_to_end_of_block_or_statement (parser);
26755 fn = error_mark_node;
26757 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26759 /* Seen already, skip it. An error message has already been output. */
26760 cp_parser_skip_to_end_of_block_or_statement (parser);
26761 fn = current_function_decl;
26762 current_function_decl = NULL_TREE;
26763 /* If this is a function from a class, pop the nested class. */
26764 if (current_class_name)
26765 pop_nested_class ();
26767 else
26769 timevar_id_t tv;
26770 if (DECL_DECLARED_INLINE_P (current_function_decl))
26771 tv = TV_PARSE_INLINE;
26772 else
26773 tv = TV_PARSE_FUNC;
26774 timevar_push (tv);
26775 fn = cp_parser_function_definition_after_declarator (parser,
26776 /*inline_p=*/false);
26777 timevar_pop (tv);
26780 return fn;
26783 /* Parse the part of a function-definition that follows the
26784 declarator. INLINE_P is TRUE iff this function is an inline
26785 function defined within a class-specifier.
26787 Returns the function defined. */
26789 static tree
26790 cp_parser_function_definition_after_declarator (cp_parser* parser,
26791 bool inline_p)
26793 tree fn;
26794 bool saved_in_unbraced_linkage_specification_p;
26795 bool saved_in_function_body;
26796 unsigned saved_num_template_parameter_lists;
26797 cp_token *token;
26798 bool fully_implicit_function_template_p
26799 = parser->fully_implicit_function_template_p;
26800 parser->fully_implicit_function_template_p = false;
26801 tree implicit_template_parms
26802 = parser->implicit_template_parms;
26803 parser->implicit_template_parms = 0;
26804 cp_binding_level* implicit_template_scope
26805 = parser->implicit_template_scope;
26806 parser->implicit_template_scope = 0;
26808 saved_in_function_body = parser->in_function_body;
26809 parser->in_function_body = true;
26810 /* If the next token is `return', then the code may be trying to
26811 make use of the "named return value" extension that G++ used to
26812 support. */
26813 token = cp_lexer_peek_token (parser->lexer);
26814 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26816 /* Consume the `return' keyword. */
26817 cp_lexer_consume_token (parser->lexer);
26818 /* Look for the identifier that indicates what value is to be
26819 returned. */
26820 cp_parser_identifier (parser);
26821 /* Issue an error message. */
26822 error_at (token->location,
26823 "named return values are no longer supported");
26824 /* Skip tokens until we reach the start of the function body. */
26825 while (true)
26827 cp_token *token = cp_lexer_peek_token (parser->lexer);
26828 if (token->type == CPP_OPEN_BRACE
26829 || token->type == CPP_EOF
26830 || token->type == CPP_PRAGMA_EOL)
26831 break;
26832 cp_lexer_consume_token (parser->lexer);
26835 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26836 anything declared inside `f'. */
26837 saved_in_unbraced_linkage_specification_p
26838 = parser->in_unbraced_linkage_specification_p;
26839 parser->in_unbraced_linkage_specification_p = false;
26840 /* Inside the function, surrounding template-parameter-lists do not
26841 apply. */
26842 saved_num_template_parameter_lists
26843 = parser->num_template_parameter_lists;
26844 parser->num_template_parameter_lists = 0;
26846 /* If the next token is `try', `__transaction_atomic', or
26847 `__transaction_relaxed`, then we are looking at either function-try-block
26848 or function-transaction-block. Note that all of these include the
26849 function-body. */
26850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26851 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26852 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26853 RID_TRANSACTION_RELAXED))
26854 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26855 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26856 cp_parser_function_try_block (parser);
26857 else
26858 cp_parser_ctor_initializer_opt_and_function_body
26859 (parser, /*in_function_try_block=*/false);
26861 /* Finish the function. */
26862 fn = finish_function (inline_p);
26863 /* Generate code for it, if necessary. */
26864 expand_or_defer_fn (fn);
26865 /* Restore the saved values. */
26866 parser->in_unbraced_linkage_specification_p
26867 = saved_in_unbraced_linkage_specification_p;
26868 parser->num_template_parameter_lists
26869 = saved_num_template_parameter_lists;
26870 parser->in_function_body = saved_in_function_body;
26872 parser->fully_implicit_function_template_p
26873 = fully_implicit_function_template_p;
26874 parser->implicit_template_parms
26875 = implicit_template_parms;
26876 parser->implicit_template_scope
26877 = implicit_template_scope;
26879 if (parser->fully_implicit_function_template_p)
26880 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26882 return fn;
26885 /* Parse a template-declaration body (following argument list). */
26887 static void
26888 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26889 tree parameter_list,
26890 bool member_p)
26892 tree decl = NULL_TREE;
26893 bool friend_p = false;
26895 /* We just processed one more parameter list. */
26896 ++parser->num_template_parameter_lists;
26898 /* Get the deferred access checks from the parameter list. These
26899 will be checked once we know what is being declared, as for a
26900 member template the checks must be performed in the scope of the
26901 class containing the member. */
26902 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26904 /* Tentatively parse for a new template parameter list, which can either be
26905 the template keyword or a template introduction. */
26906 if (cp_parser_template_declaration_after_export (parser, member_p))
26907 /* OK */;
26908 else if (cxx_dialect >= cxx11
26909 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26910 decl = cp_parser_alias_declaration (parser);
26911 else
26913 /* There are no access checks when parsing a template, as we do not
26914 know if a specialization will be a friend. */
26915 push_deferring_access_checks (dk_no_check);
26916 cp_token *token = cp_lexer_peek_token (parser->lexer);
26917 decl = cp_parser_single_declaration (parser,
26918 checks,
26919 member_p,
26920 /*explicit_specialization_p=*/false,
26921 &friend_p);
26922 pop_deferring_access_checks ();
26924 /* If this is a member template declaration, let the front
26925 end know. */
26926 if (member_p && !friend_p && decl)
26928 if (TREE_CODE (decl) == TYPE_DECL)
26929 cp_parser_check_access_in_redeclaration (decl, token->location);
26931 decl = finish_member_template_decl (decl);
26933 else if (friend_p && decl
26934 && DECL_DECLARES_TYPE_P (decl))
26935 make_friend_class (current_class_type, TREE_TYPE (decl),
26936 /*complain=*/true);
26938 /* We are done with the current parameter list. */
26939 --parser->num_template_parameter_lists;
26941 pop_deferring_access_checks ();
26943 /* Finish up. */
26944 finish_template_decl (parameter_list);
26946 /* Check the template arguments for a literal operator template. */
26947 if (decl
26948 && DECL_DECLARES_FUNCTION_P (decl)
26949 && UDLIT_OPER_P (DECL_NAME (decl)))
26951 bool ok = true;
26952 if (parameter_list == NULL_TREE)
26953 ok = false;
26954 else
26956 int num_parms = TREE_VEC_LENGTH (parameter_list);
26957 if (num_parms == 1)
26959 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26960 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26961 if (TREE_TYPE (parm) != char_type_node
26962 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26963 ok = false;
26965 else if (num_parms == 2 && cxx_dialect >= cxx14)
26967 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26968 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26969 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26970 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26971 if (parm == error_mark_node
26972 || TREE_TYPE (parm) != TREE_TYPE (type)
26973 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26974 ok = false;
26976 else
26977 ok = false;
26979 if (!ok)
26981 if (cxx_dialect >= cxx14)
26982 error ("literal operator template %qD has invalid parameter list."
26983 " Expected non-type template argument pack <char...>"
26984 " or <typename CharT, CharT...>",
26985 decl);
26986 else
26987 error ("literal operator template %qD has invalid parameter list."
26988 " Expected non-type template argument pack <char...>",
26989 decl);
26993 /* Register member declarations. */
26994 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26995 finish_member_declaration (decl);
26996 /* If DECL is a function template, we must return to parse it later.
26997 (Even though there is no definition, there might be default
26998 arguments that need handling.) */
26999 if (member_p && decl
27000 && DECL_DECLARES_FUNCTION_P (decl))
27001 vec_safe_push (unparsed_funs_with_definitions, decl);
27004 /* Parse a template introduction header for a template-declaration. Returns
27005 false if tentative parse fails. */
27007 static bool
27008 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27010 cp_parser_parse_tentatively (parser);
27012 tree saved_scope = parser->scope;
27013 tree saved_object_scope = parser->object_scope;
27014 tree saved_qualifying_scope = parser->qualifying_scope;
27016 /* Look for the optional `::' operator. */
27017 cp_parser_global_scope_opt (parser,
27018 /*current_scope_valid_p=*/false);
27019 /* Look for the nested-name-specifier. */
27020 cp_parser_nested_name_specifier_opt (parser,
27021 /*typename_keyword_p=*/false,
27022 /*check_dependency_p=*/true,
27023 /*type_p=*/false,
27024 /*is_declaration=*/false);
27026 cp_token *token = cp_lexer_peek_token (parser->lexer);
27027 tree concept_name = cp_parser_identifier (parser);
27029 /* Look up the concept for which we will be matching
27030 template parameters. */
27031 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27032 token->location);
27033 parser->scope = saved_scope;
27034 parser->object_scope = saved_object_scope;
27035 parser->qualifying_scope = saved_qualifying_scope;
27037 if (concept_name == error_mark_node)
27038 cp_parser_simulate_error (parser);
27040 /* Look for opening brace for introduction. */
27041 matching_braces braces;
27042 braces.require_open (parser);
27044 if (!cp_parser_parse_definitely (parser))
27045 return false;
27047 push_deferring_access_checks (dk_deferred);
27049 /* Build vector of placeholder parameters and grab
27050 matching identifiers. */
27051 tree introduction_list = cp_parser_introduction_list (parser);
27053 /* The introduction-list shall not be empty. */
27054 int nargs = TREE_VEC_LENGTH (introduction_list);
27055 if (nargs == 0)
27057 error ("empty introduction-list");
27058 return true;
27061 /* Look for closing brace for introduction. */
27062 if (!braces.require_close (parser))
27063 return true;
27065 if (tmpl_decl == error_mark_node)
27067 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27068 token->location);
27069 return true;
27072 /* Build and associate the constraint. */
27073 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27074 if (parms && parms != error_mark_node)
27076 cp_parser_template_declaration_after_parameters (parser, parms,
27077 member_p);
27078 return true;
27081 error_at (token->location, "no matching concept for template-introduction");
27082 return true;
27085 /* Parse a normal template-declaration following the template keyword. */
27087 static void
27088 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27090 tree parameter_list;
27091 bool need_lang_pop;
27092 location_t location = input_location;
27094 /* Look for the `<' token. */
27095 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27096 return;
27097 if (at_class_scope_p () && current_function_decl)
27099 /* 14.5.2.2 [temp.mem]
27101 A local class shall not have member templates. */
27102 error_at (location,
27103 "invalid declaration of member template in local class");
27104 cp_parser_skip_to_end_of_block_or_statement (parser);
27105 return;
27107 /* [temp]
27109 A template ... shall not have C linkage. */
27110 if (current_lang_name == lang_name_c)
27112 error_at (location, "template with C linkage");
27113 maybe_show_extern_c_location ();
27114 /* Give it C++ linkage to avoid confusing other parts of the
27115 front end. */
27116 push_lang_context (lang_name_cplusplus);
27117 need_lang_pop = true;
27119 else
27120 need_lang_pop = false;
27122 /* We cannot perform access checks on the template parameter
27123 declarations until we know what is being declared, just as we
27124 cannot check the decl-specifier list. */
27125 push_deferring_access_checks (dk_deferred);
27127 /* If the next token is `>', then we have an invalid
27128 specialization. Rather than complain about an invalid template
27129 parameter, issue an error message here. */
27130 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27132 cp_parser_error (parser, "invalid explicit specialization");
27133 begin_specialization ();
27134 parameter_list = NULL_TREE;
27136 else
27138 /* Parse the template parameters. */
27139 parameter_list = cp_parser_template_parameter_list (parser);
27142 /* Look for the `>'. */
27143 cp_parser_skip_to_end_of_template_parameter_list (parser);
27145 /* Manage template requirements */
27146 if (flag_concepts)
27148 tree reqs = get_shorthand_constraints (current_template_parms);
27149 if (tree r = cp_parser_requires_clause_opt (parser))
27150 reqs = conjoin_constraints (reqs, normalize_expression (r));
27151 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27154 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27155 member_p);
27157 /* For the erroneous case of a template with C linkage, we pushed an
27158 implicit C++ linkage scope; exit that scope now. */
27159 if (need_lang_pop)
27160 pop_lang_context ();
27163 /* Parse a template-declaration, assuming that the `export' (and
27164 `extern') keywords, if present, has already been scanned. MEMBER_P
27165 is as for cp_parser_template_declaration. */
27167 static bool
27168 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27170 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27172 cp_lexer_consume_token (parser->lexer);
27173 cp_parser_explicit_template_declaration (parser, member_p);
27174 return true;
27176 else if (flag_concepts)
27177 return cp_parser_template_introduction (parser, member_p);
27179 return false;
27182 /* Perform the deferred access checks from a template-parameter-list.
27183 CHECKS is a TREE_LIST of access checks, as returned by
27184 get_deferred_access_checks. */
27186 static void
27187 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27189 ++processing_template_parmlist;
27190 perform_access_checks (checks, tf_warning_or_error);
27191 --processing_template_parmlist;
27194 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27195 `function-definition' sequence that follows a template header.
27196 If MEMBER_P is true, this declaration appears in a class scope.
27198 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27199 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27201 static tree
27202 cp_parser_single_declaration (cp_parser* parser,
27203 vec<deferred_access_check, va_gc> *checks,
27204 bool member_p,
27205 bool explicit_specialization_p,
27206 bool* friend_p)
27208 int declares_class_or_enum;
27209 tree decl = NULL_TREE;
27210 cp_decl_specifier_seq decl_specifiers;
27211 bool function_definition_p = false;
27212 cp_token *decl_spec_token_start;
27214 /* This function is only used when processing a template
27215 declaration. */
27216 gcc_assert (innermost_scope_kind () == sk_template_parms
27217 || innermost_scope_kind () == sk_template_spec);
27219 /* Defer access checks until we know what is being declared. */
27220 push_deferring_access_checks (dk_deferred);
27222 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27223 alternative. */
27224 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27225 cp_parser_decl_specifier_seq (parser,
27226 CP_PARSER_FLAGS_OPTIONAL,
27227 &decl_specifiers,
27228 &declares_class_or_enum);
27229 if (friend_p)
27230 *friend_p = cp_parser_friend_p (&decl_specifiers);
27232 /* There are no template typedefs. */
27233 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27235 error_at (decl_spec_token_start->location,
27236 "template declaration of %<typedef%>");
27237 decl = error_mark_node;
27240 /* Gather up the access checks that occurred the
27241 decl-specifier-seq. */
27242 stop_deferring_access_checks ();
27244 /* Check for the declaration of a template class. */
27245 if (declares_class_or_enum)
27247 if (cp_parser_declares_only_class_p (parser)
27248 || (declares_class_or_enum & 2))
27250 // If this is a declaration, but not a definition, associate
27251 // any constraints with the type declaration. Constraints
27252 // are associated with definitions in cp_parser_class_specifier.
27253 if (declares_class_or_enum == 1)
27254 associate_classtype_constraints (decl_specifiers.type);
27256 decl = shadow_tag (&decl_specifiers);
27258 /* In this case:
27260 struct C {
27261 friend template <typename T> struct A<T>::B;
27264 A<T>::B will be represented by a TYPENAME_TYPE, and
27265 therefore not recognized by shadow_tag. */
27266 if (friend_p && *friend_p
27267 && !decl
27268 && decl_specifiers.type
27269 && TYPE_P (decl_specifiers.type))
27270 decl = decl_specifiers.type;
27272 if (decl && decl != error_mark_node)
27273 decl = TYPE_NAME (decl);
27274 else
27275 decl = error_mark_node;
27277 /* Perform access checks for template parameters. */
27278 cp_parser_perform_template_parameter_access_checks (checks);
27280 /* Give a helpful diagnostic for
27281 template <class T> struct A { } a;
27282 if we aren't already recovering from an error. */
27283 if (!cp_parser_declares_only_class_p (parser)
27284 && !seen_error ())
27286 error_at (cp_lexer_peek_token (parser->lexer)->location,
27287 "a class template declaration must not declare "
27288 "anything else");
27289 cp_parser_skip_to_end_of_block_or_statement (parser);
27290 goto out;
27295 /* Complain about missing 'typename' or other invalid type names. */
27296 if (!decl_specifiers.any_type_specifiers_p
27297 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27299 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27300 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27301 the rest of this declaration. */
27302 decl = error_mark_node;
27303 goto out;
27306 /* If it's not a template class, try for a template function. If
27307 the next token is a `;', then this declaration does not declare
27308 anything. But, if there were errors in the decl-specifiers, then
27309 the error might well have come from an attempted class-specifier.
27310 In that case, there's no need to warn about a missing declarator. */
27311 if (!decl
27312 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27313 || decl_specifiers.type != error_mark_node))
27315 decl = cp_parser_init_declarator (parser,
27316 &decl_specifiers,
27317 checks,
27318 /*function_definition_allowed_p=*/true,
27319 member_p,
27320 declares_class_or_enum,
27321 &function_definition_p,
27322 NULL, NULL, NULL);
27324 /* 7.1.1-1 [dcl.stc]
27326 A storage-class-specifier shall not be specified in an explicit
27327 specialization... */
27328 if (decl
27329 && explicit_specialization_p
27330 && decl_specifiers.storage_class != sc_none)
27332 error_at (decl_spec_token_start->location,
27333 "explicit template specialization cannot have a storage class");
27334 decl = error_mark_node;
27337 if (decl && VAR_P (decl))
27338 check_template_variable (decl);
27341 /* Look for a trailing `;' after the declaration. */
27342 if (!function_definition_p
27343 && (decl == error_mark_node
27344 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27345 cp_parser_skip_to_end_of_block_or_statement (parser);
27347 out:
27348 pop_deferring_access_checks ();
27350 /* Clear any current qualification; whatever comes next is the start
27351 of something new. */
27352 parser->scope = NULL_TREE;
27353 parser->qualifying_scope = NULL_TREE;
27354 parser->object_scope = NULL_TREE;
27356 return decl;
27359 /* Parse a cast-expression that is not the operand of a unary "&". */
27361 static cp_expr
27362 cp_parser_simple_cast_expression (cp_parser *parser)
27364 return cp_parser_cast_expression (parser, /*address_p=*/false,
27365 /*cast_p=*/false, /*decltype*/false, NULL);
27368 /* Parse a functional cast to TYPE. Returns an expression
27369 representing the cast. */
27371 static cp_expr
27372 cp_parser_functional_cast (cp_parser* parser, tree type)
27374 vec<tree, va_gc> *vec;
27375 tree expression_list;
27376 cp_expr cast;
27377 bool nonconst_p;
27379 location_t start_loc = input_location;
27381 if (!type)
27382 type = error_mark_node;
27384 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27386 cp_lexer_set_source_position (parser->lexer);
27387 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27388 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27389 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27390 if (TREE_CODE (type) == TYPE_DECL)
27391 type = TREE_TYPE (type);
27393 cast = finish_compound_literal (type, expression_list,
27394 tf_warning_or_error, fcl_functional);
27395 /* Create a location of the form:
27396 type_name{i, f}
27397 ^~~~~~~~~~~~~~~
27398 with caret == start at the start of the type name,
27399 finishing at the closing brace. */
27400 location_t finish_loc
27401 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27402 location_t combined_loc = make_location (start_loc, start_loc,
27403 finish_loc);
27404 cast.set_location (combined_loc);
27405 return cast;
27409 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27410 /*cast_p=*/true,
27411 /*allow_expansion_p=*/true,
27412 /*non_constant_p=*/NULL);
27413 if (vec == NULL)
27414 expression_list = error_mark_node;
27415 else
27417 expression_list = build_tree_list_vec (vec);
27418 release_tree_vector (vec);
27421 cast = build_functional_cast (type, expression_list,
27422 tf_warning_or_error);
27423 /* [expr.const]/1: In an integral constant expression "only type
27424 conversions to integral or enumeration type can be used". */
27425 if (TREE_CODE (type) == TYPE_DECL)
27426 type = TREE_TYPE (type);
27427 if (cast != error_mark_node
27428 && !cast_valid_in_integral_constant_expression_p (type)
27429 && cp_parser_non_integral_constant_expression (parser,
27430 NIC_CONSTRUCTOR))
27431 return error_mark_node;
27433 /* Create a location of the form:
27434 float(i)
27435 ^~~~~~~~
27436 with caret == start at the start of the type name,
27437 finishing at the closing paren. */
27438 location_t finish_loc
27439 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27440 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27441 cast.set_location (combined_loc);
27442 return cast;
27445 /* Save the tokens that make up the body of a member function defined
27446 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27447 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27448 specifiers applied to the declaration. Returns the FUNCTION_DECL
27449 for the member function. */
27451 static tree
27452 cp_parser_save_member_function_body (cp_parser* parser,
27453 cp_decl_specifier_seq *decl_specifiers,
27454 cp_declarator *declarator,
27455 tree attributes)
27457 cp_token *first;
27458 cp_token *last;
27459 tree fn;
27460 bool function_try_block = false;
27462 /* Create the FUNCTION_DECL. */
27463 fn = grokmethod (decl_specifiers, declarator, attributes);
27464 cp_finalize_omp_declare_simd (parser, fn);
27465 cp_finalize_oacc_routine (parser, fn, true);
27466 /* If something went badly wrong, bail out now. */
27467 if (fn == error_mark_node)
27469 /* If there's a function-body, skip it. */
27470 if (cp_parser_token_starts_function_definition_p
27471 (cp_lexer_peek_token (parser->lexer)))
27472 cp_parser_skip_to_end_of_block_or_statement (parser);
27473 return error_mark_node;
27476 /* Remember it, if there default args to post process. */
27477 cp_parser_save_default_args (parser, fn);
27479 /* Save away the tokens that make up the body of the
27480 function. */
27481 first = parser->lexer->next_token;
27483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27484 cp_lexer_consume_token (parser->lexer);
27485 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27486 RID_TRANSACTION_ATOMIC))
27488 cp_lexer_consume_token (parser->lexer);
27489 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27490 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27491 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27492 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27493 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27494 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27495 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27497 cp_lexer_consume_token (parser->lexer);
27498 cp_lexer_consume_token (parser->lexer);
27499 cp_lexer_consume_token (parser->lexer);
27500 cp_lexer_consume_token (parser->lexer);
27501 cp_lexer_consume_token (parser->lexer);
27503 else
27504 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27505 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27507 cp_lexer_consume_token (parser->lexer);
27508 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27509 break;
27513 /* Handle function try blocks. */
27514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27516 cp_lexer_consume_token (parser->lexer);
27517 function_try_block = true;
27519 /* We can have braced-init-list mem-initializers before the fn body. */
27520 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27522 cp_lexer_consume_token (parser->lexer);
27523 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27525 /* cache_group will stop after an un-nested { } pair, too. */
27526 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27527 break;
27529 /* variadic mem-inits have ... after the ')'. */
27530 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27531 cp_lexer_consume_token (parser->lexer);
27534 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27535 /* Handle function try blocks. */
27536 if (function_try_block)
27537 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27538 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27539 last = parser->lexer->next_token;
27541 /* Save away the inline definition; we will process it when the
27542 class is complete. */
27543 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27544 DECL_PENDING_INLINE_P (fn) = 1;
27546 /* We need to know that this was defined in the class, so that
27547 friend templates are handled correctly. */
27548 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27550 /* Add FN to the queue of functions to be parsed later. */
27551 vec_safe_push (unparsed_funs_with_definitions, fn);
27553 return fn;
27556 /* Save the tokens that make up the in-class initializer for a non-static
27557 data member. Returns a DEFAULT_ARG. */
27559 static tree
27560 cp_parser_save_nsdmi (cp_parser* parser)
27562 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27565 /* Parse a template-argument-list, as well as the trailing ">" (but
27566 not the opening "<"). See cp_parser_template_argument_list for the
27567 return value. */
27569 static tree
27570 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27572 tree arguments;
27573 tree saved_scope;
27574 tree saved_qualifying_scope;
27575 tree saved_object_scope;
27576 bool saved_greater_than_is_operator_p;
27577 int saved_unevaluated_operand;
27578 int saved_inhibit_evaluation_warnings;
27580 /* [temp.names]
27582 When parsing a template-id, the first non-nested `>' is taken as
27583 the end of the template-argument-list rather than a greater-than
27584 operator. */
27585 saved_greater_than_is_operator_p
27586 = parser->greater_than_is_operator_p;
27587 parser->greater_than_is_operator_p = false;
27588 /* Parsing the argument list may modify SCOPE, so we save it
27589 here. */
27590 saved_scope = parser->scope;
27591 saved_qualifying_scope = parser->qualifying_scope;
27592 saved_object_scope = parser->object_scope;
27593 /* We need to evaluate the template arguments, even though this
27594 template-id may be nested within a "sizeof". */
27595 saved_unevaluated_operand = cp_unevaluated_operand;
27596 cp_unevaluated_operand = 0;
27597 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27598 c_inhibit_evaluation_warnings = 0;
27599 /* Parse the template-argument-list itself. */
27600 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27601 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27602 arguments = NULL_TREE;
27603 else
27604 arguments = cp_parser_template_argument_list (parser);
27605 /* Look for the `>' that ends the template-argument-list. If we find
27606 a '>>' instead, it's probably just a typo. */
27607 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27609 if (cxx_dialect != cxx98)
27611 /* In C++0x, a `>>' in a template argument list or cast
27612 expression is considered to be two separate `>'
27613 tokens. So, change the current token to a `>', but don't
27614 consume it: it will be consumed later when the outer
27615 template argument list (or cast expression) is parsed.
27616 Note that this replacement of `>' for `>>' is necessary
27617 even if we are parsing tentatively: in the tentative
27618 case, after calling
27619 cp_parser_enclosed_template_argument_list we will always
27620 throw away all of the template arguments and the first
27621 closing `>', either because the template argument list
27622 was erroneous or because we are replacing those tokens
27623 with a CPP_TEMPLATE_ID token. The second `>' (which will
27624 not have been thrown away) is needed either to close an
27625 outer template argument list or to complete a new-style
27626 cast. */
27627 cp_token *token = cp_lexer_peek_token (parser->lexer);
27628 token->type = CPP_GREATER;
27630 else if (!saved_greater_than_is_operator_p)
27632 /* If we're in a nested template argument list, the '>>' has
27633 to be a typo for '> >'. We emit the error message, but we
27634 continue parsing and we push a '>' as next token, so that
27635 the argument list will be parsed correctly. Note that the
27636 global source location is still on the token before the
27637 '>>', so we need to say explicitly where we want it. */
27638 cp_token *token = cp_lexer_peek_token (parser->lexer);
27639 gcc_rich_location richloc (token->location);
27640 richloc.add_fixit_replace ("> >");
27641 error_at (&richloc, "%<>>%> should be %<> >%> "
27642 "within a nested template argument list");
27644 token->type = CPP_GREATER;
27646 else
27648 /* If this is not a nested template argument list, the '>>'
27649 is a typo for '>'. Emit an error message and continue.
27650 Same deal about the token location, but here we can get it
27651 right by consuming the '>>' before issuing the diagnostic. */
27652 cp_token *token = cp_lexer_consume_token (parser->lexer);
27653 error_at (token->location,
27654 "spurious %<>>%>, use %<>%> to terminate "
27655 "a template argument list");
27658 else
27659 cp_parser_skip_to_end_of_template_parameter_list (parser);
27660 /* The `>' token might be a greater-than operator again now. */
27661 parser->greater_than_is_operator_p
27662 = saved_greater_than_is_operator_p;
27663 /* Restore the SAVED_SCOPE. */
27664 parser->scope = saved_scope;
27665 parser->qualifying_scope = saved_qualifying_scope;
27666 parser->object_scope = saved_object_scope;
27667 cp_unevaluated_operand = saved_unevaluated_operand;
27668 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27670 return arguments;
27673 /* MEMBER_FUNCTION is a member function, or a friend. If default
27674 arguments, or the body of the function have not yet been parsed,
27675 parse them now. */
27677 static void
27678 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27680 timevar_push (TV_PARSE_INMETH);
27681 /* If this member is a template, get the underlying
27682 FUNCTION_DECL. */
27683 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27684 member_function = DECL_TEMPLATE_RESULT (member_function);
27686 /* There should not be any class definitions in progress at this
27687 point; the bodies of members are only parsed outside of all class
27688 definitions. */
27689 gcc_assert (parser->num_classes_being_defined == 0);
27690 /* While we're parsing the member functions we might encounter more
27691 classes. We want to handle them right away, but we don't want
27692 them getting mixed up with functions that are currently in the
27693 queue. */
27694 push_unparsed_function_queues (parser);
27696 /* Make sure that any template parameters are in scope. */
27697 maybe_begin_member_template_processing (member_function);
27699 /* If the body of the function has not yet been parsed, parse it
27700 now. */
27701 if (DECL_PENDING_INLINE_P (member_function))
27703 tree function_scope;
27704 cp_token_cache *tokens;
27706 /* The function is no longer pending; we are processing it. */
27707 tokens = DECL_PENDING_INLINE_INFO (member_function);
27708 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27709 DECL_PENDING_INLINE_P (member_function) = 0;
27711 /* If this is a local class, enter the scope of the containing
27712 function. */
27713 function_scope = current_function_decl;
27714 if (function_scope)
27715 push_function_context ();
27717 /* Push the body of the function onto the lexer stack. */
27718 cp_parser_push_lexer_for_tokens (parser, tokens);
27720 /* Let the front end know that we going to be defining this
27721 function. */
27722 start_preparsed_function (member_function, NULL_TREE,
27723 SF_PRE_PARSED | SF_INCLASS_INLINE);
27725 /* Don't do access checking if it is a templated function. */
27726 if (processing_template_decl)
27727 push_deferring_access_checks (dk_no_check);
27729 /* #pragma omp declare reduction needs special parsing. */
27730 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27732 parser->lexer->in_pragma = true;
27733 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27734 finish_function (/*inline_p=*/true);
27735 cp_check_omp_declare_reduction (member_function);
27737 else
27738 /* Now, parse the body of the function. */
27739 cp_parser_function_definition_after_declarator (parser,
27740 /*inline_p=*/true);
27742 if (processing_template_decl)
27743 pop_deferring_access_checks ();
27745 /* Leave the scope of the containing function. */
27746 if (function_scope)
27747 pop_function_context ();
27748 cp_parser_pop_lexer (parser);
27751 /* Remove any template parameters from the symbol table. */
27752 maybe_end_member_template_processing ();
27754 /* Restore the queue. */
27755 pop_unparsed_function_queues (parser);
27756 timevar_pop (TV_PARSE_INMETH);
27759 /* If DECL contains any default args, remember it on the unparsed
27760 functions queue. */
27762 static void
27763 cp_parser_save_default_args (cp_parser* parser, tree decl)
27765 tree probe;
27767 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27768 probe;
27769 probe = TREE_CHAIN (probe))
27770 if (TREE_PURPOSE (probe))
27772 cp_default_arg_entry entry = {current_class_type, decl};
27773 vec_safe_push (unparsed_funs_with_default_args, entry);
27774 break;
27778 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27779 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27780 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27781 from the parameter-type-list. */
27783 static tree
27784 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27785 tree default_arg, tree parmtype)
27787 cp_token_cache *tokens;
27788 tree parsed_arg;
27789 bool dummy;
27791 if (default_arg == error_mark_node)
27792 return error_mark_node;
27794 /* Push the saved tokens for the default argument onto the parser's
27795 lexer stack. */
27796 tokens = DEFARG_TOKENS (default_arg);
27797 cp_parser_push_lexer_for_tokens (parser, tokens);
27799 start_lambda_scope (decl);
27801 /* Parse the default argument. */
27802 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27803 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27804 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27806 finish_lambda_scope ();
27808 if (parsed_arg == error_mark_node)
27809 cp_parser_skip_to_end_of_statement (parser);
27811 if (!processing_template_decl)
27813 /* In a non-template class, check conversions now. In a template,
27814 we'll wait and instantiate these as needed. */
27815 if (TREE_CODE (decl) == PARM_DECL)
27816 parsed_arg = check_default_argument (parmtype, parsed_arg,
27817 tf_warning_or_error);
27818 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27819 parsed_arg = error_mark_node;
27820 else
27821 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27824 /* If the token stream has not been completely used up, then
27825 there was extra junk after the end of the default
27826 argument. */
27827 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27829 if (TREE_CODE (decl) == PARM_DECL)
27830 cp_parser_error (parser, "expected %<,%>");
27831 else
27832 cp_parser_error (parser, "expected %<;%>");
27835 /* Revert to the main lexer. */
27836 cp_parser_pop_lexer (parser);
27838 return parsed_arg;
27841 /* FIELD is a non-static data member with an initializer which we saved for
27842 later; parse it now. */
27844 static void
27845 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27847 tree def;
27849 maybe_begin_member_template_processing (field);
27851 push_unparsed_function_queues (parser);
27852 def = cp_parser_late_parse_one_default_arg (parser, field,
27853 DECL_INITIAL (field),
27854 NULL_TREE);
27855 pop_unparsed_function_queues (parser);
27857 maybe_end_member_template_processing ();
27859 DECL_INITIAL (field) = def;
27862 /* FN is a FUNCTION_DECL which may contains a parameter with an
27863 unparsed DEFAULT_ARG. Parse the default args now. This function
27864 assumes that the current scope is the scope in which the default
27865 argument should be processed. */
27867 static void
27868 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27870 bool saved_local_variables_forbidden_p;
27871 tree parm, parmdecl;
27873 /* While we're parsing the default args, we might (due to the
27874 statement expression extension) encounter more classes. We want
27875 to handle them right away, but we don't want them getting mixed
27876 up with default args that are currently in the queue. */
27877 push_unparsed_function_queues (parser);
27879 /* Local variable names (and the `this' keyword) may not appear
27880 in a default argument. */
27881 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27882 parser->local_variables_forbidden_p = true;
27884 push_defarg_context (fn);
27886 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27887 parmdecl = DECL_ARGUMENTS (fn);
27888 parm && parm != void_list_node;
27889 parm = TREE_CHAIN (parm),
27890 parmdecl = DECL_CHAIN (parmdecl))
27892 tree default_arg = TREE_PURPOSE (parm);
27893 tree parsed_arg;
27894 vec<tree, va_gc> *insts;
27895 tree copy;
27896 unsigned ix;
27898 if (!default_arg)
27899 continue;
27901 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27902 /* This can happen for a friend declaration for a function
27903 already declared with default arguments. */
27904 continue;
27906 parsed_arg
27907 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27908 default_arg,
27909 TREE_VALUE (parm));
27910 TREE_PURPOSE (parm) = parsed_arg;
27912 /* Update any instantiations we've already created. */
27913 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27914 vec_safe_iterate (insts, ix, &copy); ix++)
27915 TREE_PURPOSE (copy) = parsed_arg;
27918 pop_defarg_context ();
27920 /* Make sure no default arg is missing. */
27921 check_default_args (fn);
27923 /* Restore the state of local_variables_forbidden_p. */
27924 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27926 /* Restore the queue. */
27927 pop_unparsed_function_queues (parser);
27930 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27932 sizeof ... ( identifier )
27934 where the 'sizeof' token has already been consumed. */
27936 static tree
27937 cp_parser_sizeof_pack (cp_parser *parser)
27939 /* Consume the `...'. */
27940 cp_lexer_consume_token (parser->lexer);
27941 maybe_warn_variadic_templates ();
27943 matching_parens parens;
27944 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27945 if (paren)
27946 parens.consume_open (parser);
27947 else
27948 permerror (cp_lexer_peek_token (parser->lexer)->location,
27949 "%<sizeof...%> argument must be surrounded by parentheses");
27951 cp_token *token = cp_lexer_peek_token (parser->lexer);
27952 tree name = cp_parser_identifier (parser);
27953 if (name == error_mark_node)
27954 return error_mark_node;
27955 /* The name is not qualified. */
27956 parser->scope = NULL_TREE;
27957 parser->qualifying_scope = NULL_TREE;
27958 parser->object_scope = NULL_TREE;
27959 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27960 if (expr == error_mark_node)
27961 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27962 token->location);
27963 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27964 expr = TREE_TYPE (expr);
27965 else if (TREE_CODE (expr) == CONST_DECL)
27966 expr = DECL_INITIAL (expr);
27967 expr = make_pack_expansion (expr);
27968 PACK_EXPANSION_SIZEOF_P (expr) = true;
27970 if (paren)
27971 parens.require_close (parser);
27973 return expr;
27976 /* Parse the operand of `sizeof' (or a similar operator). Returns
27977 either a TYPE or an expression, depending on the form of the
27978 input. The KEYWORD indicates which kind of expression we have
27979 encountered. */
27981 static tree
27982 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27984 tree expr = NULL_TREE;
27985 const char *saved_message;
27986 char *tmp;
27987 bool saved_integral_constant_expression_p;
27988 bool saved_non_integral_constant_expression_p;
27990 /* If it's a `...', then we are computing the length of a parameter
27991 pack. */
27992 if (keyword == RID_SIZEOF
27993 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27994 return cp_parser_sizeof_pack (parser);
27996 /* Types cannot be defined in a `sizeof' expression. Save away the
27997 old message. */
27998 saved_message = parser->type_definition_forbidden_message;
27999 /* And create the new one. */
28000 tmp = concat ("types may not be defined in %<",
28001 IDENTIFIER_POINTER (ridpointers[keyword]),
28002 "%> expressions", NULL);
28003 parser->type_definition_forbidden_message = tmp;
28005 /* The restrictions on constant-expressions do not apply inside
28006 sizeof expressions. */
28007 saved_integral_constant_expression_p
28008 = parser->integral_constant_expression_p;
28009 saved_non_integral_constant_expression_p
28010 = parser->non_integral_constant_expression_p;
28011 parser->integral_constant_expression_p = false;
28013 /* Do not actually evaluate the expression. */
28014 ++cp_unevaluated_operand;
28015 ++c_inhibit_evaluation_warnings;
28016 /* If it's a `(', then we might be looking at the type-id
28017 construction. */
28018 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28020 tree type = NULL_TREE;
28022 /* We can't be sure yet whether we're looking at a type-id or an
28023 expression. */
28024 cp_parser_parse_tentatively (parser);
28026 matching_parens parens;
28027 parens.consume_open (parser);
28029 /* Note: as a GNU Extension, compound literals are considered
28030 postfix-expressions as they are in C99, so they are valid
28031 arguments to sizeof. See comment in cp_parser_cast_expression
28032 for details. */
28033 if (cp_parser_compound_literal_p (parser))
28034 cp_parser_simulate_error (parser);
28035 else
28037 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28038 parser->in_type_id_in_expr_p = true;
28039 /* Look for the type-id. */
28040 type = cp_parser_type_id (parser);
28041 /* Look for the closing `)'. */
28042 parens.require_close (parser);
28043 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28046 /* If all went well, then we're done. */
28047 if (cp_parser_parse_definitely (parser))
28049 cp_decl_specifier_seq decl_specs;
28051 /* Build a trivial decl-specifier-seq. */
28052 clear_decl_specs (&decl_specs);
28053 decl_specs.type = type;
28055 /* Call grokdeclarator to figure out what type this is. */
28056 expr = grokdeclarator (NULL,
28057 &decl_specs,
28058 TYPENAME,
28059 /*initialized=*/0,
28060 /*attrlist=*/NULL);
28064 /* If the type-id production did not work out, then we must be
28065 looking at the unary-expression production. */
28066 if (!expr)
28067 expr = cp_parser_unary_expression (parser);
28069 /* Go back to evaluating expressions. */
28070 --cp_unevaluated_operand;
28071 --c_inhibit_evaluation_warnings;
28073 /* Free the message we created. */
28074 free (tmp);
28075 /* And restore the old one. */
28076 parser->type_definition_forbidden_message = saved_message;
28077 parser->integral_constant_expression_p
28078 = saved_integral_constant_expression_p;
28079 parser->non_integral_constant_expression_p
28080 = saved_non_integral_constant_expression_p;
28082 return expr;
28085 /* If the current declaration has no declarator, return true. */
28087 static bool
28088 cp_parser_declares_only_class_p (cp_parser *parser)
28090 /* If the next token is a `;' or a `,' then there is no
28091 declarator. */
28092 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28093 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28096 /* Update the DECL_SPECS to reflect the storage class indicated by
28097 KEYWORD. */
28099 static void
28100 cp_parser_set_storage_class (cp_parser *parser,
28101 cp_decl_specifier_seq *decl_specs,
28102 enum rid keyword,
28103 cp_token *token)
28105 cp_storage_class storage_class;
28107 if (parser->in_unbraced_linkage_specification_p)
28109 error_at (token->location, "invalid use of %qD in linkage specification",
28110 ridpointers[keyword]);
28111 return;
28113 else if (decl_specs->storage_class != sc_none)
28115 decl_specs->conflicting_specifiers_p = true;
28116 return;
28119 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28120 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28121 && decl_specs->gnu_thread_keyword_p)
28123 pedwarn (decl_specs->locations[ds_thread], 0,
28124 "%<__thread%> before %qD", ridpointers[keyword]);
28127 switch (keyword)
28129 case RID_AUTO:
28130 storage_class = sc_auto;
28131 break;
28132 case RID_REGISTER:
28133 storage_class = sc_register;
28134 break;
28135 case RID_STATIC:
28136 storage_class = sc_static;
28137 break;
28138 case RID_EXTERN:
28139 storage_class = sc_extern;
28140 break;
28141 case RID_MUTABLE:
28142 storage_class = sc_mutable;
28143 break;
28144 default:
28145 gcc_unreachable ();
28147 decl_specs->storage_class = storage_class;
28148 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28150 /* A storage class specifier cannot be applied alongside a typedef
28151 specifier. If there is a typedef specifier present then set
28152 conflicting_specifiers_p which will trigger an error later
28153 on in grokdeclarator. */
28154 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28155 decl_specs->conflicting_specifiers_p = true;
28158 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28159 is true, the type is a class or enum definition. */
28161 static void
28162 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28163 tree type_spec,
28164 cp_token *token,
28165 bool type_definition_p)
28167 decl_specs->any_specifiers_p = true;
28169 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28170 (with, for example, in "typedef int wchar_t;") we remember that
28171 this is what happened. In system headers, we ignore these
28172 declarations so that G++ can work with system headers that are not
28173 C++-safe. */
28174 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28175 && !type_definition_p
28176 && (type_spec == boolean_type_node
28177 || type_spec == char16_type_node
28178 || type_spec == char32_type_node
28179 || type_spec == wchar_type_node)
28180 && (decl_specs->type
28181 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28182 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28183 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28184 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28186 decl_specs->redefined_builtin_type = type_spec;
28187 set_and_check_decl_spec_loc (decl_specs,
28188 ds_redefined_builtin_type_spec,
28189 token);
28190 if (!decl_specs->type)
28192 decl_specs->type = type_spec;
28193 decl_specs->type_definition_p = false;
28194 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28197 else if (decl_specs->type)
28198 decl_specs->multiple_types_p = true;
28199 else
28201 decl_specs->type = type_spec;
28202 decl_specs->type_definition_p = type_definition_p;
28203 decl_specs->redefined_builtin_type = NULL_TREE;
28204 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28208 /* True iff TOKEN is the GNU keyword __thread. */
28210 static bool
28211 token_is__thread (cp_token *token)
28213 gcc_assert (token->keyword == RID_THREAD);
28214 return id_equal (token->u.value, "__thread");
28217 /* Set the location for a declarator specifier and check if it is
28218 duplicated.
28220 DECL_SPECS is the sequence of declarator specifiers onto which to
28221 set the location.
28223 DS is the single declarator specifier to set which location is to
28224 be set onto the existing sequence of declarators.
28226 LOCATION is the location for the declarator specifier to
28227 consider. */
28229 static void
28230 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28231 cp_decl_spec ds, cp_token *token)
28233 gcc_assert (ds < ds_last);
28235 if (decl_specs == NULL)
28236 return;
28238 source_location location = token->location;
28240 if (decl_specs->locations[ds] == 0)
28242 decl_specs->locations[ds] = location;
28243 if (ds == ds_thread)
28244 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28246 else
28248 if (ds == ds_long)
28250 if (decl_specs->locations[ds_long_long] != 0)
28251 error_at (location,
28252 "%<long long long%> is too long for GCC");
28253 else
28255 decl_specs->locations[ds_long_long] = location;
28256 pedwarn_cxx98 (location,
28257 OPT_Wlong_long,
28258 "ISO C++ 1998 does not support %<long long%>");
28261 else if (ds == ds_thread)
28263 bool gnu = token_is__thread (token);
28264 if (gnu != decl_specs->gnu_thread_keyword_p)
28265 error_at (location,
28266 "both %<__thread%> and %<thread_local%> specified");
28267 else
28269 gcc_rich_location richloc (location);
28270 richloc.add_fixit_remove ();
28271 error_at (&richloc, "duplicate %qD", token->u.value);
28274 else
28276 static const char *const decl_spec_names[] = {
28277 "signed",
28278 "unsigned",
28279 "short",
28280 "long",
28281 "const",
28282 "volatile",
28283 "restrict",
28284 "inline",
28285 "virtual",
28286 "explicit",
28287 "friend",
28288 "typedef",
28289 "using",
28290 "constexpr",
28291 "__complex"
28293 gcc_rich_location richloc (location);
28294 richloc.add_fixit_remove ();
28295 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28300 /* Return true iff the declarator specifier DS is present in the
28301 sequence of declarator specifiers DECL_SPECS. */
28303 bool
28304 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28305 cp_decl_spec ds)
28307 gcc_assert (ds < ds_last);
28309 if (decl_specs == NULL)
28310 return false;
28312 return decl_specs->locations[ds] != 0;
28315 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28316 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28318 static bool
28319 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28321 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28324 /* Issue an error message indicating that TOKEN_DESC was expected.
28325 If KEYWORD is true, it indicated this function is called by
28326 cp_parser_require_keword and the required token can only be
28327 a indicated keyword.
28329 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28330 within any error as the location of an "opening" token matching
28331 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28332 RT_CLOSE_PAREN). */
28334 static void
28335 cp_parser_required_error (cp_parser *parser,
28336 required_token token_desc,
28337 bool keyword,
28338 location_t matching_location)
28340 if (cp_parser_simulate_error (parser))
28341 return;
28343 const char *gmsgid = NULL;
28344 switch (token_desc)
28346 case RT_NEW:
28347 gmsgid = G_("expected %<new%>");
28348 break;
28349 case RT_DELETE:
28350 gmsgid = G_("expected %<delete%>");
28351 break;
28352 case RT_RETURN:
28353 gmsgid = G_("expected %<return%>");
28354 break;
28355 case RT_WHILE:
28356 gmsgid = G_("expected %<while%>");
28357 break;
28358 case RT_EXTERN:
28359 gmsgid = G_("expected %<extern%>");
28360 break;
28361 case RT_STATIC_ASSERT:
28362 gmsgid = G_("expected %<static_assert%>");
28363 break;
28364 case RT_DECLTYPE:
28365 gmsgid = G_("expected %<decltype%>");
28366 break;
28367 case RT_OPERATOR:
28368 gmsgid = G_("expected %<operator%>");
28369 break;
28370 case RT_CLASS:
28371 gmsgid = G_("expected %<class%>");
28372 break;
28373 case RT_TEMPLATE:
28374 gmsgid = G_("expected %<template%>");
28375 break;
28376 case RT_NAMESPACE:
28377 gmsgid = G_("expected %<namespace%>");
28378 break;
28379 case RT_USING:
28380 gmsgid = G_("expected %<using%>");
28381 break;
28382 case RT_ASM:
28383 gmsgid = G_("expected %<asm%>");
28384 break;
28385 case RT_TRY:
28386 gmsgid = G_("expected %<try%>");
28387 break;
28388 case RT_CATCH:
28389 gmsgid = G_("expected %<catch%>");
28390 break;
28391 case RT_THROW:
28392 gmsgid = G_("expected %<throw%>");
28393 break;
28394 case RT_LABEL:
28395 gmsgid = G_("expected %<__label__%>");
28396 break;
28397 case RT_AT_TRY:
28398 gmsgid = G_("expected %<@try%>");
28399 break;
28400 case RT_AT_SYNCHRONIZED:
28401 gmsgid = G_("expected %<@synchronized%>");
28402 break;
28403 case RT_AT_THROW:
28404 gmsgid = G_("expected %<@throw%>");
28405 break;
28406 case RT_TRANSACTION_ATOMIC:
28407 gmsgid = G_("expected %<__transaction_atomic%>");
28408 break;
28409 case RT_TRANSACTION_RELAXED:
28410 gmsgid = G_("expected %<__transaction_relaxed%>");
28411 break;
28412 default:
28413 break;
28416 if (!gmsgid && !keyword)
28418 switch (token_desc)
28420 case RT_SEMICOLON:
28421 gmsgid = G_("expected %<;%>");
28422 break;
28423 case RT_OPEN_PAREN:
28424 gmsgid = G_("expected %<(%>");
28425 break;
28426 case RT_CLOSE_BRACE:
28427 gmsgid = G_("expected %<}%>");
28428 break;
28429 case RT_OPEN_BRACE:
28430 gmsgid = G_("expected %<{%>");
28431 break;
28432 case RT_CLOSE_SQUARE:
28433 gmsgid = G_("expected %<]%>");
28434 break;
28435 case RT_OPEN_SQUARE:
28436 gmsgid = G_("expected %<[%>");
28437 break;
28438 case RT_COMMA:
28439 gmsgid = G_("expected %<,%>");
28440 break;
28441 case RT_SCOPE:
28442 gmsgid = G_("expected %<::%>");
28443 break;
28444 case RT_LESS:
28445 gmsgid = G_("expected %<<%>");
28446 break;
28447 case RT_GREATER:
28448 gmsgid = G_("expected %<>%>");
28449 break;
28450 case RT_EQ:
28451 gmsgid = G_("expected %<=%>");
28452 break;
28453 case RT_ELLIPSIS:
28454 gmsgid = G_("expected %<...%>");
28455 break;
28456 case RT_MULT:
28457 gmsgid = G_("expected %<*%>");
28458 break;
28459 case RT_COMPL:
28460 gmsgid = G_("expected %<~%>");
28461 break;
28462 case RT_COLON:
28463 gmsgid = G_("expected %<:%>");
28464 break;
28465 case RT_COLON_SCOPE:
28466 gmsgid = G_("expected %<:%> or %<::%>");
28467 break;
28468 case RT_CLOSE_PAREN:
28469 gmsgid = G_("expected %<)%>");
28470 break;
28471 case RT_COMMA_CLOSE_PAREN:
28472 gmsgid = G_("expected %<,%> or %<)%>");
28473 break;
28474 case RT_PRAGMA_EOL:
28475 gmsgid = G_("expected end of line");
28476 break;
28477 case RT_NAME:
28478 gmsgid = G_("expected identifier");
28479 break;
28480 case RT_SELECT:
28481 gmsgid = G_("expected selection-statement");
28482 break;
28483 case RT_ITERATION:
28484 gmsgid = G_("expected iteration-statement");
28485 break;
28486 case RT_JUMP:
28487 gmsgid = G_("expected jump-statement");
28488 break;
28489 case RT_CLASS_KEY:
28490 gmsgid = G_("expected class-key");
28491 break;
28492 case RT_CLASS_TYPENAME_TEMPLATE:
28493 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28494 break;
28495 default:
28496 gcc_unreachable ();
28500 if (gmsgid)
28501 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28505 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28506 issue an error message indicating that TOKEN_DESC was expected.
28508 Returns the token consumed, if the token had the appropriate type.
28509 Otherwise, returns NULL.
28511 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28512 within any error as the location of an "opening" token matching
28513 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28514 RT_CLOSE_PAREN). */
28516 static cp_token *
28517 cp_parser_require (cp_parser* parser,
28518 enum cpp_ttype type,
28519 required_token token_desc,
28520 location_t matching_location)
28522 if (cp_lexer_next_token_is (parser->lexer, type))
28523 return cp_lexer_consume_token (parser->lexer);
28524 else
28526 /* Output the MESSAGE -- unless we're parsing tentatively. */
28527 if (!cp_parser_simulate_error (parser))
28528 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28529 matching_location);
28530 return NULL;
28534 /* An error message is produced if the next token is not '>'.
28535 All further tokens are skipped until the desired token is
28536 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28538 static void
28539 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28541 /* Current level of '< ... >'. */
28542 unsigned level = 0;
28543 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28544 unsigned nesting_depth = 0;
28546 /* Are we ready, yet? If not, issue error message. */
28547 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28548 return;
28550 /* Skip tokens until the desired token is found. */
28551 while (true)
28553 /* Peek at the next token. */
28554 switch (cp_lexer_peek_token (parser->lexer)->type)
28556 case CPP_LESS:
28557 if (!nesting_depth)
28558 ++level;
28559 break;
28561 case CPP_RSHIFT:
28562 if (cxx_dialect == cxx98)
28563 /* C++0x views the `>>' operator as two `>' tokens, but
28564 C++98 does not. */
28565 break;
28566 else if (!nesting_depth && level-- == 0)
28568 /* We've hit a `>>' where the first `>' closes the
28569 template argument list, and the second `>' is
28570 spurious. Just consume the `>>' and stop; we've
28571 already produced at least one error. */
28572 cp_lexer_consume_token (parser->lexer);
28573 return;
28575 /* Fall through for C++0x, so we handle the second `>' in
28576 the `>>'. */
28577 gcc_fallthrough ();
28579 case CPP_GREATER:
28580 if (!nesting_depth && level-- == 0)
28582 /* We've reached the token we want, consume it and stop. */
28583 cp_lexer_consume_token (parser->lexer);
28584 return;
28586 break;
28588 case CPP_OPEN_PAREN:
28589 case CPP_OPEN_SQUARE:
28590 ++nesting_depth;
28591 break;
28593 case CPP_CLOSE_PAREN:
28594 case CPP_CLOSE_SQUARE:
28595 if (nesting_depth-- == 0)
28596 return;
28597 break;
28599 case CPP_EOF:
28600 case CPP_PRAGMA_EOL:
28601 case CPP_SEMICOLON:
28602 case CPP_OPEN_BRACE:
28603 case CPP_CLOSE_BRACE:
28604 /* The '>' was probably forgotten, don't look further. */
28605 return;
28607 default:
28608 break;
28611 /* Consume this token. */
28612 cp_lexer_consume_token (parser->lexer);
28616 /* If the next token is the indicated keyword, consume it. Otherwise,
28617 issue an error message indicating that TOKEN_DESC was expected.
28619 Returns the token consumed, if the token had the appropriate type.
28620 Otherwise, returns NULL. */
28622 static cp_token *
28623 cp_parser_require_keyword (cp_parser* parser,
28624 enum rid keyword,
28625 required_token token_desc)
28627 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28629 if (token && token->keyword != keyword)
28631 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28632 UNKNOWN_LOCATION);
28633 return NULL;
28636 return token;
28639 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28640 function-definition. */
28642 static bool
28643 cp_parser_token_starts_function_definition_p (cp_token* token)
28645 return (/* An ordinary function-body begins with an `{'. */
28646 token->type == CPP_OPEN_BRACE
28647 /* A ctor-initializer begins with a `:'. */
28648 || token->type == CPP_COLON
28649 /* A function-try-block begins with `try'. */
28650 || token->keyword == RID_TRY
28651 /* A function-transaction-block begins with `__transaction_atomic'
28652 or `__transaction_relaxed'. */
28653 || token->keyword == RID_TRANSACTION_ATOMIC
28654 || token->keyword == RID_TRANSACTION_RELAXED
28655 /* The named return value extension begins with `return'. */
28656 || token->keyword == RID_RETURN);
28659 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28660 definition. */
28662 static bool
28663 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28665 cp_token *token;
28667 token = cp_lexer_peek_token (parser->lexer);
28668 return (token->type == CPP_OPEN_BRACE
28669 || (token->type == CPP_COLON
28670 && !parser->colon_doesnt_start_class_def_p));
28673 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28674 C++0x) ending a template-argument. */
28676 static bool
28677 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28679 cp_token *token;
28681 token = cp_lexer_peek_token (parser->lexer);
28682 return (token->type == CPP_COMMA
28683 || token->type == CPP_GREATER
28684 || token->type == CPP_ELLIPSIS
28685 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28688 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28689 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28691 static bool
28692 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28693 size_t n)
28695 cp_token *token;
28697 token = cp_lexer_peek_nth_token (parser->lexer, n);
28698 if (token->type == CPP_LESS)
28699 return true;
28700 /* Check for the sequence `<::' in the original code. It would be lexed as
28701 `[:', where `[' is a digraph, and there is no whitespace before
28702 `:'. */
28703 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28705 cp_token *token2;
28706 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28707 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28708 return true;
28710 return false;
28713 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28714 or none_type otherwise. */
28716 static enum tag_types
28717 cp_parser_token_is_class_key (cp_token* token)
28719 switch (token->keyword)
28721 case RID_CLASS:
28722 return class_type;
28723 case RID_STRUCT:
28724 return record_type;
28725 case RID_UNION:
28726 return union_type;
28728 default:
28729 return none_type;
28733 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28734 or none_type otherwise or if the token is null. */
28736 static enum tag_types
28737 cp_parser_token_is_type_parameter_key (cp_token* token)
28739 if (!token)
28740 return none_type;
28742 switch (token->keyword)
28744 case RID_CLASS:
28745 return class_type;
28746 case RID_TYPENAME:
28747 return typename_type;
28749 default:
28750 return none_type;
28754 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28756 static void
28757 cp_parser_check_class_key (enum tag_types class_key, tree type)
28759 if (type == error_mark_node)
28760 return;
28761 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28763 if (permerror (input_location, "%qs tag used in naming %q#T",
28764 class_key == union_type ? "union"
28765 : class_key == record_type ? "struct" : "class",
28766 type))
28767 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28768 "%q#T was previously declared here", type);
28772 /* Issue an error message if DECL is redeclared with different
28773 access than its original declaration [class.access.spec/3].
28774 This applies to nested classes, nested class templates and
28775 enumerations [class.mem/1]. */
28777 static void
28778 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28780 if (!decl
28781 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28782 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28783 return;
28785 if ((TREE_PRIVATE (decl)
28786 != (current_access_specifier == access_private_node))
28787 || (TREE_PROTECTED (decl)
28788 != (current_access_specifier == access_protected_node)))
28789 error_at (location, "%qD redeclared with different access", decl);
28792 /* Look for the `template' keyword, as a syntactic disambiguator.
28793 Return TRUE iff it is present, in which case it will be
28794 consumed. */
28796 static bool
28797 cp_parser_optional_template_keyword (cp_parser *parser)
28799 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28801 /* In C++98 the `template' keyword can only be used within templates;
28802 outside templates the parser can always figure out what is a
28803 template and what is not. In C++11, per the resolution of DR 468,
28804 `template' is allowed in cases where it is not strictly necessary. */
28805 if (!processing_template_decl
28806 && pedantic && cxx_dialect == cxx98)
28808 cp_token *token = cp_lexer_peek_token (parser->lexer);
28809 pedwarn (token->location, OPT_Wpedantic,
28810 "in C++98 %<template%> (as a disambiguator) is only "
28811 "allowed within templates");
28812 /* If this part of the token stream is rescanned, the same
28813 error message would be generated. So, we purge the token
28814 from the stream. */
28815 cp_lexer_purge_token (parser->lexer);
28816 return false;
28818 else
28820 /* Consume the `template' keyword. */
28821 cp_lexer_consume_token (parser->lexer);
28822 return true;
28825 return false;
28828 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28829 set PARSER->SCOPE, and perform other related actions. */
28831 static void
28832 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28834 struct tree_check *check_value;
28836 /* Get the stored value. */
28837 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28838 /* Set the scope from the stored value. */
28839 parser->scope = saved_checks_value (check_value);
28840 parser->qualifying_scope = check_value->qualifying_scope;
28841 parser->object_scope = NULL_TREE;
28844 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28845 encounter the end of a block before what we were looking for. */
28847 static bool
28848 cp_parser_cache_group (cp_parser *parser,
28849 enum cpp_ttype end,
28850 unsigned depth)
28852 while (true)
28854 cp_token *token = cp_lexer_peek_token (parser->lexer);
28856 /* Abort a parenthesized expression if we encounter a semicolon. */
28857 if ((end == CPP_CLOSE_PAREN || depth == 0)
28858 && token->type == CPP_SEMICOLON)
28859 return true;
28860 /* If we've reached the end of the file, stop. */
28861 if (token->type == CPP_EOF
28862 || (end != CPP_PRAGMA_EOL
28863 && token->type == CPP_PRAGMA_EOL))
28864 return true;
28865 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28866 /* We've hit the end of an enclosing block, so there's been some
28867 kind of syntax error. */
28868 return true;
28870 /* Consume the token. */
28871 cp_lexer_consume_token (parser->lexer);
28872 /* See if it starts a new group. */
28873 if (token->type == CPP_OPEN_BRACE)
28875 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28876 /* In theory this should probably check end == '}', but
28877 cp_parser_save_member_function_body needs it to exit
28878 after either '}' or ')' when called with ')'. */
28879 if (depth == 0)
28880 return false;
28882 else if (token->type == CPP_OPEN_PAREN)
28884 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28885 if (depth == 0 && end == CPP_CLOSE_PAREN)
28886 return false;
28888 else if (token->type == CPP_PRAGMA)
28889 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28890 else if (token->type == end)
28891 return false;
28895 /* Like above, for caching a default argument or NSDMI. Both of these are
28896 terminated by a non-nested comma, but it can be unclear whether or not a
28897 comma is nested in a template argument list unless we do more parsing.
28898 In order to handle this ambiguity, when we encounter a ',' after a '<'
28899 we try to parse what follows as a parameter-declaration-list (in the
28900 case of a default argument) or a member-declarator (in the case of an
28901 NSDMI). If that succeeds, then we stop caching. */
28903 static tree
28904 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28906 unsigned depth = 0;
28907 int maybe_template_id = 0;
28908 cp_token *first_token;
28909 cp_token *token;
28910 tree default_argument;
28912 /* Add tokens until we have processed the entire default
28913 argument. We add the range [first_token, token). */
28914 first_token = cp_lexer_peek_token (parser->lexer);
28915 if (first_token->type == CPP_OPEN_BRACE)
28917 /* For list-initialization, this is straightforward. */
28918 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28919 token = cp_lexer_peek_token (parser->lexer);
28921 else while (true)
28923 bool done = false;
28925 /* Peek at the next token. */
28926 token = cp_lexer_peek_token (parser->lexer);
28927 /* What we do depends on what token we have. */
28928 switch (token->type)
28930 /* In valid code, a default argument must be
28931 immediately followed by a `,' `)', or `...'. */
28932 case CPP_COMMA:
28933 if (depth == 0 && maybe_template_id)
28935 /* If we've seen a '<', we might be in a
28936 template-argument-list. Until Core issue 325 is
28937 resolved, we don't know how this situation ought
28938 to be handled, so try to DTRT. We check whether
28939 what comes after the comma is a valid parameter
28940 declaration list. If it is, then the comma ends
28941 the default argument; otherwise the default
28942 argument continues. */
28943 bool error = false;
28944 cp_token *peek;
28946 /* Set ITALP so cp_parser_parameter_declaration_list
28947 doesn't decide to commit to this parse. */
28948 bool saved_italp = parser->in_template_argument_list_p;
28949 parser->in_template_argument_list_p = true;
28951 cp_parser_parse_tentatively (parser);
28953 if (nsdmi)
28955 /* Parse declarators until we reach a non-comma or
28956 somthing that cannot be an initializer.
28957 Just checking whether we're looking at a single
28958 declarator is insufficient. Consider:
28959 int var = tuple<T,U>::x;
28960 The template parameter 'U' looks exactly like a
28961 declarator. */
28964 int ctor_dtor_or_conv_p;
28965 cp_lexer_consume_token (parser->lexer);
28966 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28967 &ctor_dtor_or_conv_p,
28968 /*parenthesized_p=*/NULL,
28969 /*member_p=*/true,
28970 /*friend_p=*/false);
28971 peek = cp_lexer_peek_token (parser->lexer);
28972 if (cp_parser_error_occurred (parser))
28973 break;
28975 while (peek->type == CPP_COMMA);
28976 /* If we met an '=' or ';' then the original comma
28977 was the end of the NSDMI. Otherwise assume
28978 we're still in the NSDMI. */
28979 error = (peek->type != CPP_EQ
28980 && peek->type != CPP_SEMICOLON);
28982 else
28984 cp_lexer_consume_token (parser->lexer);
28985 begin_scope (sk_function_parms, NULL_TREE);
28986 cp_parser_parameter_declaration_list (parser, &error);
28987 pop_bindings_and_leave_scope ();
28989 if (!cp_parser_error_occurred (parser) && !error)
28990 done = true;
28991 cp_parser_abort_tentative_parse (parser);
28993 parser->in_template_argument_list_p = saved_italp;
28994 break;
28996 /* FALLTHRU */
28997 case CPP_CLOSE_PAREN:
28998 case CPP_ELLIPSIS:
28999 /* If we run into a non-nested `;', `}', or `]',
29000 then the code is invalid -- but the default
29001 argument is certainly over. */
29002 case CPP_SEMICOLON:
29003 case CPP_CLOSE_BRACE:
29004 case CPP_CLOSE_SQUARE:
29005 if (depth == 0
29006 /* Handle correctly int n = sizeof ... ( p ); */
29007 && token->type != CPP_ELLIPSIS)
29008 done = true;
29009 /* Update DEPTH, if necessary. */
29010 else if (token->type == CPP_CLOSE_PAREN
29011 || token->type == CPP_CLOSE_BRACE
29012 || token->type == CPP_CLOSE_SQUARE)
29013 --depth;
29014 break;
29016 case CPP_OPEN_PAREN:
29017 case CPP_OPEN_SQUARE:
29018 case CPP_OPEN_BRACE:
29019 ++depth;
29020 break;
29022 case CPP_LESS:
29023 if (depth == 0)
29024 /* This might be the comparison operator, or it might
29025 start a template argument list. */
29026 ++maybe_template_id;
29027 break;
29029 case CPP_RSHIFT:
29030 if (cxx_dialect == cxx98)
29031 break;
29032 /* Fall through for C++0x, which treats the `>>'
29033 operator like two `>' tokens in certain
29034 cases. */
29035 gcc_fallthrough ();
29037 case CPP_GREATER:
29038 if (depth == 0)
29040 /* This might be an operator, or it might close a
29041 template argument list. But if a previous '<'
29042 started a template argument list, this will have
29043 closed it, so we can't be in one anymore. */
29044 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29045 if (maybe_template_id < 0)
29046 maybe_template_id = 0;
29048 break;
29050 /* If we run out of tokens, issue an error message. */
29051 case CPP_EOF:
29052 case CPP_PRAGMA_EOL:
29053 error_at (token->location, "file ends in default argument");
29054 return error_mark_node;
29056 case CPP_NAME:
29057 case CPP_SCOPE:
29058 /* In these cases, we should look for template-ids.
29059 For example, if the default argument is
29060 `X<int, double>()', we need to do name lookup to
29061 figure out whether or not `X' is a template; if
29062 so, the `,' does not end the default argument.
29064 That is not yet done. */
29065 break;
29067 default:
29068 break;
29071 /* If we've reached the end, stop. */
29072 if (done)
29073 break;
29075 /* Add the token to the token block. */
29076 token = cp_lexer_consume_token (parser->lexer);
29079 /* Create a DEFAULT_ARG to represent the unparsed default
29080 argument. */
29081 default_argument = make_node (DEFAULT_ARG);
29082 DEFARG_TOKENS (default_argument)
29083 = cp_token_cache_new (first_token, token);
29084 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29086 return default_argument;
29089 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29091 location_t
29092 defarg_location (tree default_argument)
29094 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29095 location_t start = tokens->first->location;
29096 location_t end = tokens->last->location;
29097 return make_location (start, start, end);
29100 /* Begin parsing tentatively. We always save tokens while parsing
29101 tentatively so that if the tentative parsing fails we can restore the
29102 tokens. */
29104 static void
29105 cp_parser_parse_tentatively (cp_parser* parser)
29107 /* Enter a new parsing context. */
29108 parser->context = cp_parser_context_new (parser->context);
29109 /* Begin saving tokens. */
29110 cp_lexer_save_tokens (parser->lexer);
29111 /* In order to avoid repetitive access control error messages,
29112 access checks are queued up until we are no longer parsing
29113 tentatively. */
29114 push_deferring_access_checks (dk_deferred);
29117 /* Commit to the currently active tentative parse. */
29119 static void
29120 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29122 cp_parser_context *context;
29123 cp_lexer *lexer;
29125 /* Mark all of the levels as committed. */
29126 lexer = parser->lexer;
29127 for (context = parser->context; context->next; context = context->next)
29129 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29130 break;
29131 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29132 while (!cp_lexer_saving_tokens (lexer))
29133 lexer = lexer->next;
29134 cp_lexer_commit_tokens (lexer);
29138 /* Commit to the topmost currently active tentative parse.
29140 Note that this function shouldn't be called when there are
29141 irreversible side-effects while in a tentative state. For
29142 example, we shouldn't create a permanent entry in the symbol
29143 table, or issue an error message that might not apply if the
29144 tentative parse is aborted. */
29146 static void
29147 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29149 cp_parser_context *context = parser->context;
29150 cp_lexer *lexer = parser->lexer;
29152 if (context)
29154 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29155 return;
29156 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29158 while (!cp_lexer_saving_tokens (lexer))
29159 lexer = lexer->next;
29160 cp_lexer_commit_tokens (lexer);
29164 /* Abort the currently active tentative parse. All consumed tokens
29165 will be rolled back, and no diagnostics will be issued. */
29167 static void
29168 cp_parser_abort_tentative_parse (cp_parser* parser)
29170 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29171 || errorcount > 0);
29172 cp_parser_simulate_error (parser);
29173 /* Now, pretend that we want to see if the construct was
29174 successfully parsed. */
29175 cp_parser_parse_definitely (parser);
29178 /* Stop parsing tentatively. If a parse error has occurred, restore the
29179 token stream. Otherwise, commit to the tokens we have consumed.
29180 Returns true if no error occurred; false otherwise. */
29182 static bool
29183 cp_parser_parse_definitely (cp_parser* parser)
29185 bool error_occurred;
29186 cp_parser_context *context;
29188 /* Remember whether or not an error occurred, since we are about to
29189 destroy that information. */
29190 error_occurred = cp_parser_error_occurred (parser);
29191 /* Remove the topmost context from the stack. */
29192 context = parser->context;
29193 parser->context = context->next;
29194 /* If no parse errors occurred, commit to the tentative parse. */
29195 if (!error_occurred)
29197 /* Commit to the tokens read tentatively, unless that was
29198 already done. */
29199 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29200 cp_lexer_commit_tokens (parser->lexer);
29202 pop_to_parent_deferring_access_checks ();
29204 /* Otherwise, if errors occurred, roll back our state so that things
29205 are just as they were before we began the tentative parse. */
29206 else
29208 cp_lexer_rollback_tokens (parser->lexer);
29209 pop_deferring_access_checks ();
29211 /* Add the context to the front of the free list. */
29212 context->next = cp_parser_context_free_list;
29213 cp_parser_context_free_list = context;
29215 return !error_occurred;
29218 /* Returns true if we are parsing tentatively and are not committed to
29219 this tentative parse. */
29221 static bool
29222 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29224 return (cp_parser_parsing_tentatively (parser)
29225 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29228 /* Returns nonzero iff an error has occurred during the most recent
29229 tentative parse. */
29231 static bool
29232 cp_parser_error_occurred (cp_parser* parser)
29234 return (cp_parser_parsing_tentatively (parser)
29235 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29238 /* Returns nonzero if GNU extensions are allowed. */
29240 static bool
29241 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29243 return parser->allow_gnu_extensions_p;
29246 /* Objective-C++ Productions */
29249 /* Parse an Objective-C expression, which feeds into a primary-expression
29250 above.
29252 objc-expression:
29253 objc-message-expression
29254 objc-string-literal
29255 objc-encode-expression
29256 objc-protocol-expression
29257 objc-selector-expression
29259 Returns a tree representation of the expression. */
29261 static cp_expr
29262 cp_parser_objc_expression (cp_parser* parser)
29264 /* Try to figure out what kind of declaration is present. */
29265 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29267 switch (kwd->type)
29269 case CPP_OPEN_SQUARE:
29270 return cp_parser_objc_message_expression (parser);
29272 case CPP_OBJC_STRING:
29273 kwd = cp_lexer_consume_token (parser->lexer);
29274 return objc_build_string_object (kwd->u.value);
29276 case CPP_KEYWORD:
29277 switch (kwd->keyword)
29279 case RID_AT_ENCODE:
29280 return cp_parser_objc_encode_expression (parser);
29282 case RID_AT_PROTOCOL:
29283 return cp_parser_objc_protocol_expression (parser);
29285 case RID_AT_SELECTOR:
29286 return cp_parser_objc_selector_expression (parser);
29288 default:
29289 break;
29291 /* FALLTHRU */
29292 default:
29293 error_at (kwd->location,
29294 "misplaced %<@%D%> Objective-C++ construct",
29295 kwd->u.value);
29296 cp_parser_skip_to_end_of_block_or_statement (parser);
29299 return error_mark_node;
29302 /* Parse an Objective-C message expression.
29304 objc-message-expression:
29305 [ objc-message-receiver objc-message-args ]
29307 Returns a representation of an Objective-C message. */
29309 static tree
29310 cp_parser_objc_message_expression (cp_parser* parser)
29312 tree receiver, messageargs;
29314 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29315 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29316 receiver = cp_parser_objc_message_receiver (parser);
29317 messageargs = cp_parser_objc_message_args (parser);
29318 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29319 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29321 tree result = objc_build_message_expr (receiver, messageargs);
29323 /* Construct a location e.g.
29324 [self func1:5]
29325 ^~~~~~~~~~~~~~
29326 ranging from the '[' to the ']', with the caret at the start. */
29327 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29328 protected_set_expr_location (result, combined_loc);
29330 return result;
29333 /* Parse an objc-message-receiver.
29335 objc-message-receiver:
29336 expression
29337 simple-type-specifier
29339 Returns a representation of the type or expression. */
29341 static tree
29342 cp_parser_objc_message_receiver (cp_parser* parser)
29344 tree rcv;
29346 /* An Objective-C message receiver may be either (1) a type
29347 or (2) an expression. */
29348 cp_parser_parse_tentatively (parser);
29349 rcv = cp_parser_expression (parser);
29351 /* If that worked out, fine. */
29352 if (cp_parser_parse_definitely (parser))
29353 return rcv;
29355 cp_parser_parse_tentatively (parser);
29356 rcv = cp_parser_simple_type_specifier (parser,
29357 /*decl_specs=*/NULL,
29358 CP_PARSER_FLAGS_NONE);
29360 if (cp_parser_parse_definitely (parser))
29361 return objc_get_class_reference (rcv);
29363 cp_parser_error (parser, "objective-c++ message receiver expected");
29364 return error_mark_node;
29367 /* Parse the arguments and selectors comprising an Objective-C message.
29369 objc-message-args:
29370 objc-selector
29371 objc-selector-args
29372 objc-selector-args , objc-comma-args
29374 objc-selector-args:
29375 objc-selector [opt] : assignment-expression
29376 objc-selector-args objc-selector [opt] : assignment-expression
29378 objc-comma-args:
29379 assignment-expression
29380 objc-comma-args , assignment-expression
29382 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29383 selector arguments and TREE_VALUE containing a list of comma
29384 arguments. */
29386 static tree
29387 cp_parser_objc_message_args (cp_parser* parser)
29389 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29390 bool maybe_unary_selector_p = true;
29391 cp_token *token = cp_lexer_peek_token (parser->lexer);
29393 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29395 tree selector = NULL_TREE, arg;
29397 if (token->type != CPP_COLON)
29398 selector = cp_parser_objc_selector (parser);
29400 /* Detect if we have a unary selector. */
29401 if (maybe_unary_selector_p
29402 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29403 return build_tree_list (selector, NULL_TREE);
29405 maybe_unary_selector_p = false;
29406 cp_parser_require (parser, CPP_COLON, RT_COLON);
29407 arg = cp_parser_assignment_expression (parser);
29409 sel_args
29410 = chainon (sel_args,
29411 build_tree_list (selector, arg));
29413 token = cp_lexer_peek_token (parser->lexer);
29416 /* Handle non-selector arguments, if any. */
29417 while (token->type == CPP_COMMA)
29419 tree arg;
29421 cp_lexer_consume_token (parser->lexer);
29422 arg = cp_parser_assignment_expression (parser);
29424 addl_args
29425 = chainon (addl_args,
29426 build_tree_list (NULL_TREE, arg));
29428 token = cp_lexer_peek_token (parser->lexer);
29431 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29433 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29434 return build_tree_list (error_mark_node, error_mark_node);
29437 return build_tree_list (sel_args, addl_args);
29440 /* Parse an Objective-C encode expression.
29442 objc-encode-expression:
29443 @encode objc-typename
29445 Returns an encoded representation of the type argument. */
29447 static cp_expr
29448 cp_parser_objc_encode_expression (cp_parser* parser)
29450 tree type;
29451 cp_token *token;
29452 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29454 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29455 matching_parens parens;
29456 parens.require_open (parser);
29457 token = cp_lexer_peek_token (parser->lexer);
29458 type = complete_type (cp_parser_type_id (parser));
29459 parens.require_close (parser);
29461 if (!type)
29463 error_at (token->location,
29464 "%<@encode%> must specify a type as an argument");
29465 return error_mark_node;
29468 /* This happens if we find @encode(T) (where T is a template
29469 typename or something dependent on a template typename) when
29470 parsing a template. In that case, we can't compile it
29471 immediately, but we rather create an AT_ENCODE_EXPR which will
29472 need to be instantiated when the template is used.
29474 if (dependent_type_p (type))
29476 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29477 TREE_READONLY (value) = 1;
29478 return value;
29482 /* Build a location of the form:
29483 @encode(int)
29484 ^~~~~~~~~~~~
29485 with caret==start at the @ token, finishing at the close paren. */
29486 location_t combined_loc
29487 = make_location (start_loc, start_loc,
29488 cp_lexer_previous_token (parser->lexer)->location);
29490 return cp_expr (objc_build_encode_expr (type), combined_loc);
29493 /* Parse an Objective-C @defs expression. */
29495 static tree
29496 cp_parser_objc_defs_expression (cp_parser *parser)
29498 tree name;
29500 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29501 matching_parens parens;
29502 parens.require_open (parser);
29503 name = cp_parser_identifier (parser);
29504 parens.require_close (parser);
29506 return objc_get_class_ivars (name);
29509 /* Parse an Objective-C protocol expression.
29511 objc-protocol-expression:
29512 @protocol ( identifier )
29514 Returns a representation of the protocol expression. */
29516 static tree
29517 cp_parser_objc_protocol_expression (cp_parser* parser)
29519 tree proto;
29520 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29522 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29523 matching_parens parens;
29524 parens.require_open (parser);
29525 proto = cp_parser_identifier (parser);
29526 parens.require_close (parser);
29528 /* Build a location of the form:
29529 @protocol(prot)
29530 ^~~~~~~~~~~~~~~
29531 with caret==start at the @ token, finishing at the close paren. */
29532 location_t combined_loc
29533 = make_location (start_loc, start_loc,
29534 cp_lexer_previous_token (parser->lexer)->location);
29535 tree result = objc_build_protocol_expr (proto);
29536 protected_set_expr_location (result, combined_loc);
29537 return result;
29540 /* Parse an Objective-C selector expression.
29542 objc-selector-expression:
29543 @selector ( objc-method-signature )
29545 objc-method-signature:
29546 objc-selector
29547 objc-selector-seq
29549 objc-selector-seq:
29550 objc-selector :
29551 objc-selector-seq objc-selector :
29553 Returns a representation of the method selector. */
29555 static tree
29556 cp_parser_objc_selector_expression (cp_parser* parser)
29558 tree sel_seq = NULL_TREE;
29559 bool maybe_unary_selector_p = true;
29560 cp_token *token;
29561 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29563 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29564 matching_parens parens;
29565 parens.require_open (parser);
29566 token = cp_lexer_peek_token (parser->lexer);
29568 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29569 || token->type == CPP_SCOPE)
29571 tree selector = NULL_TREE;
29573 if (token->type != CPP_COLON
29574 || token->type == CPP_SCOPE)
29575 selector = cp_parser_objc_selector (parser);
29577 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29578 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29580 /* Detect if we have a unary selector. */
29581 if (maybe_unary_selector_p)
29583 sel_seq = selector;
29584 goto finish_selector;
29586 else
29588 cp_parser_error (parser, "expected %<:%>");
29591 maybe_unary_selector_p = false;
29592 token = cp_lexer_consume_token (parser->lexer);
29594 if (token->type == CPP_SCOPE)
29596 sel_seq
29597 = chainon (sel_seq,
29598 build_tree_list (selector, NULL_TREE));
29599 sel_seq
29600 = chainon (sel_seq,
29601 build_tree_list (NULL_TREE, NULL_TREE));
29603 else
29604 sel_seq
29605 = chainon (sel_seq,
29606 build_tree_list (selector, NULL_TREE));
29608 token = cp_lexer_peek_token (parser->lexer);
29611 finish_selector:
29612 parens.require_close (parser);
29615 /* Build a location of the form:
29616 @selector(func)
29617 ^~~~~~~~~~~~~~~
29618 with caret==start at the @ token, finishing at the close paren. */
29619 location_t combined_loc
29620 = make_location (loc, loc,
29621 cp_lexer_previous_token (parser->lexer)->location);
29622 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29623 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29624 protected_set_expr_location (result, combined_loc);
29625 return result;
29628 /* Parse a list of identifiers.
29630 objc-identifier-list:
29631 identifier
29632 objc-identifier-list , identifier
29634 Returns a TREE_LIST of identifier nodes. */
29636 static tree
29637 cp_parser_objc_identifier_list (cp_parser* parser)
29639 tree identifier;
29640 tree list;
29641 cp_token *sep;
29643 identifier = cp_parser_identifier (parser);
29644 if (identifier == error_mark_node)
29645 return error_mark_node;
29647 list = build_tree_list (NULL_TREE, identifier);
29648 sep = cp_lexer_peek_token (parser->lexer);
29650 while (sep->type == CPP_COMMA)
29652 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29653 identifier = cp_parser_identifier (parser);
29654 if (identifier == error_mark_node)
29655 return list;
29657 list = chainon (list, build_tree_list (NULL_TREE,
29658 identifier));
29659 sep = cp_lexer_peek_token (parser->lexer);
29662 return list;
29665 /* Parse an Objective-C alias declaration.
29667 objc-alias-declaration:
29668 @compatibility_alias identifier identifier ;
29670 This function registers the alias mapping with the Objective-C front end.
29671 It returns nothing. */
29673 static void
29674 cp_parser_objc_alias_declaration (cp_parser* parser)
29676 tree alias, orig;
29678 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29679 alias = cp_parser_identifier (parser);
29680 orig = cp_parser_identifier (parser);
29681 objc_declare_alias (alias, orig);
29682 cp_parser_consume_semicolon_at_end_of_statement (parser);
29685 /* Parse an Objective-C class forward-declaration.
29687 objc-class-declaration:
29688 @class objc-identifier-list ;
29690 The function registers the forward declarations with the Objective-C
29691 front end. It returns nothing. */
29693 static void
29694 cp_parser_objc_class_declaration (cp_parser* parser)
29696 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29697 while (true)
29699 tree id;
29701 id = cp_parser_identifier (parser);
29702 if (id == error_mark_node)
29703 break;
29705 objc_declare_class (id);
29707 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29708 cp_lexer_consume_token (parser->lexer);
29709 else
29710 break;
29712 cp_parser_consume_semicolon_at_end_of_statement (parser);
29715 /* Parse a list of Objective-C protocol references.
29717 objc-protocol-refs-opt:
29718 objc-protocol-refs [opt]
29720 objc-protocol-refs:
29721 < objc-identifier-list >
29723 Returns a TREE_LIST of identifiers, if any. */
29725 static tree
29726 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29728 tree protorefs = NULL_TREE;
29730 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29732 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29733 protorefs = cp_parser_objc_identifier_list (parser);
29734 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29737 return protorefs;
29740 /* Parse a Objective-C visibility specification. */
29742 static void
29743 cp_parser_objc_visibility_spec (cp_parser* parser)
29745 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29747 switch (vis->keyword)
29749 case RID_AT_PRIVATE:
29750 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29751 break;
29752 case RID_AT_PROTECTED:
29753 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29754 break;
29755 case RID_AT_PUBLIC:
29756 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29757 break;
29758 case RID_AT_PACKAGE:
29759 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29760 break;
29761 default:
29762 return;
29765 /* Eat '@private'/'@protected'/'@public'. */
29766 cp_lexer_consume_token (parser->lexer);
29769 /* Parse an Objective-C method type. Return 'true' if it is a class
29770 (+) method, and 'false' if it is an instance (-) method. */
29772 static inline bool
29773 cp_parser_objc_method_type (cp_parser* parser)
29775 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29776 return true;
29777 else
29778 return false;
29781 /* Parse an Objective-C protocol qualifier. */
29783 static tree
29784 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29786 tree quals = NULL_TREE, node;
29787 cp_token *token = cp_lexer_peek_token (parser->lexer);
29789 node = token->u.value;
29791 while (node && identifier_p (node)
29792 && (node == ridpointers [(int) RID_IN]
29793 || node == ridpointers [(int) RID_OUT]
29794 || node == ridpointers [(int) RID_INOUT]
29795 || node == ridpointers [(int) RID_BYCOPY]
29796 || node == ridpointers [(int) RID_BYREF]
29797 || node == ridpointers [(int) RID_ONEWAY]))
29799 quals = tree_cons (NULL_TREE, node, quals);
29800 cp_lexer_consume_token (parser->lexer);
29801 token = cp_lexer_peek_token (parser->lexer);
29802 node = token->u.value;
29805 return quals;
29808 /* Parse an Objective-C typename. */
29810 static tree
29811 cp_parser_objc_typename (cp_parser* parser)
29813 tree type_name = NULL_TREE;
29815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29817 tree proto_quals, cp_type = NULL_TREE;
29819 matching_parens parens;
29820 parens.consume_open (parser); /* Eat '('. */
29821 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29823 /* An ObjC type name may consist of just protocol qualifiers, in which
29824 case the type shall default to 'id'. */
29825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29827 cp_type = cp_parser_type_id (parser);
29829 /* If the type could not be parsed, an error has already
29830 been produced. For error recovery, behave as if it had
29831 not been specified, which will use the default type
29832 'id'. */
29833 if (cp_type == error_mark_node)
29835 cp_type = NULL_TREE;
29836 /* We need to skip to the closing parenthesis as
29837 cp_parser_type_id() does not seem to do it for
29838 us. */
29839 cp_parser_skip_to_closing_parenthesis (parser,
29840 /*recovering=*/true,
29841 /*or_comma=*/false,
29842 /*consume_paren=*/false);
29846 parens.require_close (parser);
29847 type_name = build_tree_list (proto_quals, cp_type);
29850 return type_name;
29853 /* Check to see if TYPE refers to an Objective-C selector name. */
29855 static bool
29856 cp_parser_objc_selector_p (enum cpp_ttype type)
29858 return (type == CPP_NAME || type == CPP_KEYWORD
29859 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29860 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29861 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29862 || type == CPP_XOR || type == CPP_XOR_EQ);
29865 /* Parse an Objective-C selector. */
29867 static tree
29868 cp_parser_objc_selector (cp_parser* parser)
29870 cp_token *token = cp_lexer_consume_token (parser->lexer);
29872 if (!cp_parser_objc_selector_p (token->type))
29874 error_at (token->location, "invalid Objective-C++ selector name");
29875 return error_mark_node;
29878 /* C++ operator names are allowed to appear in ObjC selectors. */
29879 switch (token->type)
29881 case CPP_AND_AND: return get_identifier ("and");
29882 case CPP_AND_EQ: return get_identifier ("and_eq");
29883 case CPP_AND: return get_identifier ("bitand");
29884 case CPP_OR: return get_identifier ("bitor");
29885 case CPP_COMPL: return get_identifier ("compl");
29886 case CPP_NOT: return get_identifier ("not");
29887 case CPP_NOT_EQ: return get_identifier ("not_eq");
29888 case CPP_OR_OR: return get_identifier ("or");
29889 case CPP_OR_EQ: return get_identifier ("or_eq");
29890 case CPP_XOR: return get_identifier ("xor");
29891 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29892 default: return token->u.value;
29896 /* Parse an Objective-C params list. */
29898 static tree
29899 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29901 tree params = NULL_TREE;
29902 bool maybe_unary_selector_p = true;
29903 cp_token *token = cp_lexer_peek_token (parser->lexer);
29905 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29907 tree selector = NULL_TREE, type_name, identifier;
29908 tree parm_attr = NULL_TREE;
29910 if (token->keyword == RID_ATTRIBUTE)
29911 break;
29913 if (token->type != CPP_COLON)
29914 selector = cp_parser_objc_selector (parser);
29916 /* Detect if we have a unary selector. */
29917 if (maybe_unary_selector_p
29918 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29920 params = selector; /* Might be followed by attributes. */
29921 break;
29924 maybe_unary_selector_p = false;
29925 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29927 /* Something went quite wrong. There should be a colon
29928 here, but there is not. Stop parsing parameters. */
29929 break;
29931 type_name = cp_parser_objc_typename (parser);
29932 /* New ObjC allows attributes on parameters too. */
29933 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29934 parm_attr = cp_parser_attributes_opt (parser);
29935 identifier = cp_parser_identifier (parser);
29937 params
29938 = chainon (params,
29939 objc_build_keyword_decl (selector,
29940 type_name,
29941 identifier,
29942 parm_attr));
29944 token = cp_lexer_peek_token (parser->lexer);
29947 if (params == NULL_TREE)
29949 cp_parser_error (parser, "objective-c++ method declaration is expected");
29950 return error_mark_node;
29953 /* We allow tail attributes for the method. */
29954 if (token->keyword == RID_ATTRIBUTE)
29956 *attributes = cp_parser_attributes_opt (parser);
29957 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29958 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29959 return params;
29960 cp_parser_error (parser,
29961 "method attributes must be specified at the end");
29962 return error_mark_node;
29965 if (params == NULL_TREE)
29967 cp_parser_error (parser, "objective-c++ method declaration is expected");
29968 return error_mark_node;
29970 return params;
29973 /* Parse the non-keyword Objective-C params. */
29975 static tree
29976 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29977 tree* attributes)
29979 tree params = make_node (TREE_LIST);
29980 cp_token *token = cp_lexer_peek_token (parser->lexer);
29981 *ellipsisp = false; /* Initially, assume no ellipsis. */
29983 while (token->type == CPP_COMMA)
29985 cp_parameter_declarator *parmdecl;
29986 tree parm;
29988 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29989 token = cp_lexer_peek_token (parser->lexer);
29991 if (token->type == CPP_ELLIPSIS)
29993 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29994 *ellipsisp = true;
29995 token = cp_lexer_peek_token (parser->lexer);
29996 break;
29999 /* TODO: parse attributes for tail parameters. */
30000 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30001 parm = grokdeclarator (parmdecl->declarator,
30002 &parmdecl->decl_specifiers,
30003 PARM, /*initialized=*/0,
30004 /*attrlist=*/NULL);
30006 chainon (params, build_tree_list (NULL_TREE, parm));
30007 token = cp_lexer_peek_token (parser->lexer);
30010 /* We allow tail attributes for the method. */
30011 if (token->keyword == RID_ATTRIBUTE)
30013 if (*attributes == NULL_TREE)
30015 *attributes = cp_parser_attributes_opt (parser);
30016 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30017 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30018 return params;
30020 else
30021 /* We have an error, but parse the attributes, so that we can
30022 carry on. */
30023 *attributes = cp_parser_attributes_opt (parser);
30025 cp_parser_error (parser,
30026 "method attributes must be specified at the end");
30027 return error_mark_node;
30030 return params;
30033 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30035 static void
30036 cp_parser_objc_interstitial_code (cp_parser* parser)
30038 cp_token *token = cp_lexer_peek_token (parser->lexer);
30040 /* If the next token is `extern' and the following token is a string
30041 literal, then we have a linkage specification. */
30042 if (token->keyword == RID_EXTERN
30043 && cp_parser_is_pure_string_literal
30044 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30045 cp_parser_linkage_specification (parser);
30046 /* Handle #pragma, if any. */
30047 else if (token->type == CPP_PRAGMA)
30048 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30049 /* Allow stray semicolons. */
30050 else if (token->type == CPP_SEMICOLON)
30051 cp_lexer_consume_token (parser->lexer);
30052 /* Mark methods as optional or required, when building protocols. */
30053 else if (token->keyword == RID_AT_OPTIONAL)
30055 cp_lexer_consume_token (parser->lexer);
30056 objc_set_method_opt (true);
30058 else if (token->keyword == RID_AT_REQUIRED)
30060 cp_lexer_consume_token (parser->lexer);
30061 objc_set_method_opt (false);
30063 else if (token->keyword == RID_NAMESPACE)
30064 cp_parser_namespace_definition (parser);
30065 /* Other stray characters must generate errors. */
30066 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30068 cp_lexer_consume_token (parser->lexer);
30069 error ("stray %qs between Objective-C++ methods",
30070 token->type == CPP_OPEN_BRACE ? "{" : "}");
30072 /* Finally, try to parse a block-declaration, or a function-definition. */
30073 else
30074 cp_parser_block_declaration (parser, /*statement_p=*/false);
30077 /* Parse a method signature. */
30079 static tree
30080 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30082 tree rettype, kwdparms, optparms;
30083 bool ellipsis = false;
30084 bool is_class_method;
30086 is_class_method = cp_parser_objc_method_type (parser);
30087 rettype = cp_parser_objc_typename (parser);
30088 *attributes = NULL_TREE;
30089 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30090 if (kwdparms == error_mark_node)
30091 return error_mark_node;
30092 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30093 if (optparms == error_mark_node)
30094 return error_mark_node;
30096 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30099 static bool
30100 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30102 tree tattr;
30103 cp_lexer_save_tokens (parser->lexer);
30104 tattr = cp_parser_attributes_opt (parser);
30105 gcc_assert (tattr) ;
30107 /* If the attributes are followed by a method introducer, this is not allowed.
30108 Dump the attributes and flag the situation. */
30109 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30110 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30111 return true;
30113 /* Otherwise, the attributes introduce some interstitial code, possibly so
30114 rewind to allow that check. */
30115 cp_lexer_rollback_tokens (parser->lexer);
30116 return false;
30119 /* Parse an Objective-C method prototype list. */
30121 static void
30122 cp_parser_objc_method_prototype_list (cp_parser* parser)
30124 cp_token *token = cp_lexer_peek_token (parser->lexer);
30126 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30128 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30130 tree attributes, sig;
30131 bool is_class_method;
30132 if (token->type == CPP_PLUS)
30133 is_class_method = true;
30134 else
30135 is_class_method = false;
30136 sig = cp_parser_objc_method_signature (parser, &attributes);
30137 if (sig == error_mark_node)
30139 cp_parser_skip_to_end_of_block_or_statement (parser);
30140 token = cp_lexer_peek_token (parser->lexer);
30141 continue;
30143 objc_add_method_declaration (is_class_method, sig, attributes);
30144 cp_parser_consume_semicolon_at_end_of_statement (parser);
30146 else if (token->keyword == RID_AT_PROPERTY)
30147 cp_parser_objc_at_property_declaration (parser);
30148 else if (token->keyword == RID_ATTRIBUTE
30149 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30150 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30151 OPT_Wattributes,
30152 "prefix attributes are ignored for methods");
30153 else
30154 /* Allow for interspersed non-ObjC++ code. */
30155 cp_parser_objc_interstitial_code (parser);
30157 token = cp_lexer_peek_token (parser->lexer);
30160 if (token->type != CPP_EOF)
30161 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30162 else
30163 cp_parser_error (parser, "expected %<@end%>");
30165 objc_finish_interface ();
30168 /* Parse an Objective-C method definition list. */
30170 static void
30171 cp_parser_objc_method_definition_list (cp_parser* parser)
30173 cp_token *token = cp_lexer_peek_token (parser->lexer);
30175 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30177 tree meth;
30179 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30181 cp_token *ptk;
30182 tree sig, attribute;
30183 bool is_class_method;
30184 if (token->type == CPP_PLUS)
30185 is_class_method = true;
30186 else
30187 is_class_method = false;
30188 push_deferring_access_checks (dk_deferred);
30189 sig = cp_parser_objc_method_signature (parser, &attribute);
30190 if (sig == error_mark_node)
30192 cp_parser_skip_to_end_of_block_or_statement (parser);
30193 token = cp_lexer_peek_token (parser->lexer);
30194 continue;
30196 objc_start_method_definition (is_class_method, sig, attribute,
30197 NULL_TREE);
30199 /* For historical reasons, we accept an optional semicolon. */
30200 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30201 cp_lexer_consume_token (parser->lexer);
30203 ptk = cp_lexer_peek_token (parser->lexer);
30204 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30205 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30207 perform_deferred_access_checks (tf_warning_or_error);
30208 stop_deferring_access_checks ();
30209 meth = cp_parser_function_definition_after_declarator (parser,
30210 false);
30211 pop_deferring_access_checks ();
30212 objc_finish_method_definition (meth);
30215 /* The following case will be removed once @synthesize is
30216 completely implemented. */
30217 else if (token->keyword == RID_AT_PROPERTY)
30218 cp_parser_objc_at_property_declaration (parser);
30219 else if (token->keyword == RID_AT_SYNTHESIZE)
30220 cp_parser_objc_at_synthesize_declaration (parser);
30221 else if (token->keyword == RID_AT_DYNAMIC)
30222 cp_parser_objc_at_dynamic_declaration (parser);
30223 else if (token->keyword == RID_ATTRIBUTE
30224 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30225 warning_at (token->location, OPT_Wattributes,
30226 "prefix attributes are ignored for methods");
30227 else
30228 /* Allow for interspersed non-ObjC++ code. */
30229 cp_parser_objc_interstitial_code (parser);
30231 token = cp_lexer_peek_token (parser->lexer);
30234 if (token->type != CPP_EOF)
30235 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30236 else
30237 cp_parser_error (parser, "expected %<@end%>");
30239 objc_finish_implementation ();
30242 /* Parse Objective-C ivars. */
30244 static void
30245 cp_parser_objc_class_ivars (cp_parser* parser)
30247 cp_token *token = cp_lexer_peek_token (parser->lexer);
30249 if (token->type != CPP_OPEN_BRACE)
30250 return; /* No ivars specified. */
30252 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30253 token = cp_lexer_peek_token (parser->lexer);
30255 while (token->type != CPP_CLOSE_BRACE
30256 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30258 cp_decl_specifier_seq declspecs;
30259 int decl_class_or_enum_p;
30260 tree prefix_attributes;
30262 cp_parser_objc_visibility_spec (parser);
30264 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30265 break;
30267 cp_parser_decl_specifier_seq (parser,
30268 CP_PARSER_FLAGS_OPTIONAL,
30269 &declspecs,
30270 &decl_class_or_enum_p);
30272 /* auto, register, static, extern, mutable. */
30273 if (declspecs.storage_class != sc_none)
30275 cp_parser_error (parser, "invalid type for instance variable");
30276 declspecs.storage_class = sc_none;
30279 /* thread_local. */
30280 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30282 cp_parser_error (parser, "invalid type for instance variable");
30283 declspecs.locations[ds_thread] = 0;
30286 /* typedef. */
30287 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30289 cp_parser_error (parser, "invalid type for instance variable");
30290 declspecs.locations[ds_typedef] = 0;
30293 prefix_attributes = declspecs.attributes;
30294 declspecs.attributes = NULL_TREE;
30296 /* Keep going until we hit the `;' at the end of the
30297 declaration. */
30298 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30300 tree width = NULL_TREE, attributes, first_attribute, decl;
30301 cp_declarator *declarator = NULL;
30302 int ctor_dtor_or_conv_p;
30304 /* Check for a (possibly unnamed) bitfield declaration. */
30305 token = cp_lexer_peek_token (parser->lexer);
30306 if (token->type == CPP_COLON)
30307 goto eat_colon;
30309 if (token->type == CPP_NAME
30310 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30311 == CPP_COLON))
30313 /* Get the name of the bitfield. */
30314 declarator = make_id_declarator (NULL_TREE,
30315 cp_parser_identifier (parser),
30316 sfk_none);
30318 eat_colon:
30319 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30320 /* Get the width of the bitfield. */
30321 width
30322 = cp_parser_constant_expression (parser);
30324 else
30326 /* Parse the declarator. */
30327 declarator
30328 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30329 &ctor_dtor_or_conv_p,
30330 /*parenthesized_p=*/NULL,
30331 /*member_p=*/false,
30332 /*friend_p=*/false);
30335 /* Look for attributes that apply to the ivar. */
30336 attributes = cp_parser_attributes_opt (parser);
30337 /* Remember which attributes are prefix attributes and
30338 which are not. */
30339 first_attribute = attributes;
30340 /* Combine the attributes. */
30341 attributes = attr_chainon (prefix_attributes, attributes);
30343 if (width)
30344 /* Create the bitfield declaration. */
30345 decl = grokbitfield (declarator, &declspecs,
30346 width, NULL_TREE, attributes);
30347 else
30348 decl = grokfield (declarator, &declspecs,
30349 NULL_TREE, /*init_const_expr_p=*/false,
30350 NULL_TREE, attributes);
30352 /* Add the instance variable. */
30353 if (decl != error_mark_node && decl != NULL_TREE)
30354 objc_add_instance_variable (decl);
30356 /* Reset PREFIX_ATTRIBUTES. */
30357 if (attributes != error_mark_node)
30359 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30360 attributes = TREE_CHAIN (attributes);
30361 if (attributes)
30362 TREE_CHAIN (attributes) = NULL_TREE;
30365 token = cp_lexer_peek_token (parser->lexer);
30367 if (token->type == CPP_COMMA)
30369 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30370 continue;
30372 break;
30375 cp_parser_consume_semicolon_at_end_of_statement (parser);
30376 token = cp_lexer_peek_token (parser->lexer);
30379 if (token->keyword == RID_AT_END)
30380 cp_parser_error (parser, "expected %<}%>");
30382 /* Do not consume the RID_AT_END, so it will be read again as terminating
30383 the @interface of @implementation. */
30384 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30385 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30387 /* For historical reasons, we accept an optional semicolon. */
30388 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30389 cp_lexer_consume_token (parser->lexer);
30392 /* Parse an Objective-C protocol declaration. */
30394 static void
30395 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30397 tree proto, protorefs;
30398 cp_token *tok;
30400 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30403 tok = cp_lexer_peek_token (parser->lexer);
30404 error_at (tok->location, "identifier expected after %<@protocol%>");
30405 cp_parser_consume_semicolon_at_end_of_statement (parser);
30406 return;
30409 /* See if we have a forward declaration or a definition. */
30410 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30412 /* Try a forward declaration first. */
30413 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30415 while (true)
30417 tree id;
30419 id = cp_parser_identifier (parser);
30420 if (id == error_mark_node)
30421 break;
30423 objc_declare_protocol (id, attributes);
30425 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30426 cp_lexer_consume_token (parser->lexer);
30427 else
30428 break;
30430 cp_parser_consume_semicolon_at_end_of_statement (parser);
30433 /* Ok, we got a full-fledged definition (or at least should). */
30434 else
30436 proto = cp_parser_identifier (parser);
30437 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30438 objc_start_protocol (proto, protorefs, attributes);
30439 cp_parser_objc_method_prototype_list (parser);
30443 /* Parse an Objective-C superclass or category. */
30445 static void
30446 cp_parser_objc_superclass_or_category (cp_parser *parser,
30447 bool iface_p,
30448 tree *super,
30449 tree *categ, bool *is_class_extension)
30451 cp_token *next = cp_lexer_peek_token (parser->lexer);
30453 *super = *categ = NULL_TREE;
30454 *is_class_extension = false;
30455 if (next->type == CPP_COLON)
30457 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30458 *super = cp_parser_identifier (parser);
30460 else if (next->type == CPP_OPEN_PAREN)
30462 matching_parens parens;
30463 parens.consume_open (parser); /* Eat '('. */
30465 /* If there is no category name, and this is an @interface, we
30466 have a class extension. */
30467 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30469 *categ = NULL_TREE;
30470 *is_class_extension = true;
30472 else
30473 *categ = cp_parser_identifier (parser);
30475 parens.require_close (parser);
30479 /* Parse an Objective-C class interface. */
30481 static void
30482 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30484 tree name, super, categ, protos;
30485 bool is_class_extension;
30487 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30488 name = cp_parser_identifier (parser);
30489 if (name == error_mark_node)
30491 /* It's hard to recover because even if valid @interface stuff
30492 is to follow, we can't compile it (or validate it) if we
30493 don't even know which class it refers to. Let's assume this
30494 was a stray '@interface' token in the stream and skip it.
30496 return;
30498 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30499 &is_class_extension);
30500 protos = cp_parser_objc_protocol_refs_opt (parser);
30502 /* We have either a class or a category on our hands. */
30503 if (categ || is_class_extension)
30504 objc_start_category_interface (name, categ, protos, attributes);
30505 else
30507 objc_start_class_interface (name, super, protos, attributes);
30508 /* Handle instance variable declarations, if any. */
30509 cp_parser_objc_class_ivars (parser);
30510 objc_continue_interface ();
30513 cp_parser_objc_method_prototype_list (parser);
30516 /* Parse an Objective-C class implementation. */
30518 static void
30519 cp_parser_objc_class_implementation (cp_parser* parser)
30521 tree name, super, categ;
30522 bool is_class_extension;
30524 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30525 name = cp_parser_identifier (parser);
30526 if (name == error_mark_node)
30528 /* It's hard to recover because even if valid @implementation
30529 stuff is to follow, we can't compile it (or validate it) if
30530 we don't even know which class it refers to. Let's assume
30531 this was a stray '@implementation' token in the stream and
30532 skip it.
30534 return;
30536 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30537 &is_class_extension);
30539 /* We have either a class or a category on our hands. */
30540 if (categ)
30541 objc_start_category_implementation (name, categ);
30542 else
30544 objc_start_class_implementation (name, super);
30545 /* Handle instance variable declarations, if any. */
30546 cp_parser_objc_class_ivars (parser);
30547 objc_continue_implementation ();
30550 cp_parser_objc_method_definition_list (parser);
30553 /* Consume the @end token and finish off the implementation. */
30555 static void
30556 cp_parser_objc_end_implementation (cp_parser* parser)
30558 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30559 objc_finish_implementation ();
30562 /* Parse an Objective-C declaration. */
30564 static void
30565 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30567 /* Try to figure out what kind of declaration is present. */
30568 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30570 if (attributes)
30571 switch (kwd->keyword)
30573 case RID_AT_ALIAS:
30574 case RID_AT_CLASS:
30575 case RID_AT_END:
30576 error_at (kwd->location, "attributes may not be specified before"
30577 " the %<@%D%> Objective-C++ keyword",
30578 kwd->u.value);
30579 attributes = NULL;
30580 break;
30581 case RID_AT_IMPLEMENTATION:
30582 warning_at (kwd->location, OPT_Wattributes,
30583 "prefix attributes are ignored before %<@%D%>",
30584 kwd->u.value);
30585 attributes = NULL;
30586 default:
30587 break;
30590 switch (kwd->keyword)
30592 case RID_AT_ALIAS:
30593 cp_parser_objc_alias_declaration (parser);
30594 break;
30595 case RID_AT_CLASS:
30596 cp_parser_objc_class_declaration (parser);
30597 break;
30598 case RID_AT_PROTOCOL:
30599 cp_parser_objc_protocol_declaration (parser, attributes);
30600 break;
30601 case RID_AT_INTERFACE:
30602 cp_parser_objc_class_interface (parser, attributes);
30603 break;
30604 case RID_AT_IMPLEMENTATION:
30605 cp_parser_objc_class_implementation (parser);
30606 break;
30607 case RID_AT_END:
30608 cp_parser_objc_end_implementation (parser);
30609 break;
30610 default:
30611 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30612 kwd->u.value);
30613 cp_parser_skip_to_end_of_block_or_statement (parser);
30617 /* Parse an Objective-C try-catch-finally statement.
30619 objc-try-catch-finally-stmt:
30620 @try compound-statement objc-catch-clause-seq [opt]
30621 objc-finally-clause [opt]
30623 objc-catch-clause-seq:
30624 objc-catch-clause objc-catch-clause-seq [opt]
30626 objc-catch-clause:
30627 @catch ( objc-exception-declaration ) compound-statement
30629 objc-finally-clause:
30630 @finally compound-statement
30632 objc-exception-declaration:
30633 parameter-declaration
30634 '...'
30636 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30638 Returns NULL_TREE.
30640 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30641 for C. Keep them in sync. */
30643 static tree
30644 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30646 location_t location;
30647 tree stmt;
30649 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30650 location = cp_lexer_peek_token (parser->lexer)->location;
30651 objc_maybe_warn_exceptions (location);
30652 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30653 node, lest it get absorbed into the surrounding block. */
30654 stmt = push_stmt_list ();
30655 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30656 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30658 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30660 cp_parameter_declarator *parm;
30661 tree parameter_declaration = error_mark_node;
30662 bool seen_open_paren = false;
30663 matching_parens parens;
30665 cp_lexer_consume_token (parser->lexer);
30666 if (parens.require_open (parser))
30667 seen_open_paren = true;
30668 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30670 /* We have "@catch (...)" (where the '...' are literally
30671 what is in the code). Skip the '...'.
30672 parameter_declaration is set to NULL_TREE, and
30673 objc_being_catch_clauses() knows that that means
30674 '...'. */
30675 cp_lexer_consume_token (parser->lexer);
30676 parameter_declaration = NULL_TREE;
30678 else
30680 /* We have "@catch (NSException *exception)" or something
30681 like that. Parse the parameter declaration. */
30682 parm = cp_parser_parameter_declaration (parser, false, NULL);
30683 if (parm == NULL)
30684 parameter_declaration = error_mark_node;
30685 else
30686 parameter_declaration = grokdeclarator (parm->declarator,
30687 &parm->decl_specifiers,
30688 PARM, /*initialized=*/0,
30689 /*attrlist=*/NULL);
30691 if (seen_open_paren)
30692 parens.require_close (parser);
30693 else
30695 /* If there was no open parenthesis, we are recovering from
30696 an error, and we are trying to figure out what mistake
30697 the user has made. */
30699 /* If there is an immediate closing parenthesis, the user
30700 probably forgot the opening one (ie, they typed "@catch
30701 NSException *e)". Parse the closing parenthesis and keep
30702 going. */
30703 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30704 cp_lexer_consume_token (parser->lexer);
30706 /* If these is no immediate closing parenthesis, the user
30707 probably doesn't know that parenthesis are required at
30708 all (ie, they typed "@catch NSException *e"). So, just
30709 forget about the closing parenthesis and keep going. */
30711 objc_begin_catch_clause (parameter_declaration);
30712 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30713 objc_finish_catch_clause ();
30715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30717 cp_lexer_consume_token (parser->lexer);
30718 location = cp_lexer_peek_token (parser->lexer)->location;
30719 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30720 node, lest it get absorbed into the surrounding block. */
30721 stmt = push_stmt_list ();
30722 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30723 objc_build_finally_clause (location, pop_stmt_list (stmt));
30726 return objc_finish_try_stmt ();
30729 /* Parse an Objective-C synchronized statement.
30731 objc-synchronized-stmt:
30732 @synchronized ( expression ) compound-statement
30734 Returns NULL_TREE. */
30736 static tree
30737 cp_parser_objc_synchronized_statement (cp_parser *parser)
30739 location_t location;
30740 tree lock, stmt;
30742 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30744 location = cp_lexer_peek_token (parser->lexer)->location;
30745 objc_maybe_warn_exceptions (location);
30746 matching_parens parens;
30747 parens.require_open (parser);
30748 lock = cp_parser_expression (parser);
30749 parens.require_close (parser);
30751 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30752 node, lest it get absorbed into the surrounding block. */
30753 stmt = push_stmt_list ();
30754 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30756 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30759 /* Parse an Objective-C throw statement.
30761 objc-throw-stmt:
30762 @throw assignment-expression [opt] ;
30764 Returns a constructed '@throw' statement. */
30766 static tree
30767 cp_parser_objc_throw_statement (cp_parser *parser)
30769 tree expr = NULL_TREE;
30770 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30772 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30775 expr = cp_parser_expression (parser);
30777 cp_parser_consume_semicolon_at_end_of_statement (parser);
30779 return objc_build_throw_stmt (loc, expr);
30782 /* Parse an Objective-C statement. */
30784 static tree
30785 cp_parser_objc_statement (cp_parser * parser)
30787 /* Try to figure out what kind of declaration is present. */
30788 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30790 switch (kwd->keyword)
30792 case RID_AT_TRY:
30793 return cp_parser_objc_try_catch_finally_statement (parser);
30794 case RID_AT_SYNCHRONIZED:
30795 return cp_parser_objc_synchronized_statement (parser);
30796 case RID_AT_THROW:
30797 return cp_parser_objc_throw_statement (parser);
30798 default:
30799 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30800 kwd->u.value);
30801 cp_parser_skip_to_end_of_block_or_statement (parser);
30804 return error_mark_node;
30807 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30808 look ahead to see if an objc keyword follows the attributes. This
30809 is to detect the use of prefix attributes on ObjC @interface and
30810 @protocol. */
30812 static bool
30813 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30815 cp_lexer_save_tokens (parser->lexer);
30816 *attrib = cp_parser_attributes_opt (parser);
30817 gcc_assert (*attrib);
30818 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30820 cp_lexer_commit_tokens (parser->lexer);
30821 return true;
30823 cp_lexer_rollback_tokens (parser->lexer);
30824 return false;
30827 /* This routine is a minimal replacement for
30828 c_parser_struct_declaration () used when parsing the list of
30829 types/names or ObjC++ properties. For example, when parsing the
30830 code
30832 @property (readonly) int a, b, c;
30834 this function is responsible for parsing "int a, int b, int c" and
30835 returning the declarations as CHAIN of DECLs.
30837 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30838 similar parsing. */
30839 static tree
30840 cp_parser_objc_struct_declaration (cp_parser *parser)
30842 tree decls = NULL_TREE;
30843 cp_decl_specifier_seq declspecs;
30844 int decl_class_or_enum_p;
30845 tree prefix_attributes;
30847 cp_parser_decl_specifier_seq (parser,
30848 CP_PARSER_FLAGS_NONE,
30849 &declspecs,
30850 &decl_class_or_enum_p);
30852 if (declspecs.type == error_mark_node)
30853 return error_mark_node;
30855 /* auto, register, static, extern, mutable. */
30856 if (declspecs.storage_class != sc_none)
30858 cp_parser_error (parser, "invalid type for property");
30859 declspecs.storage_class = sc_none;
30862 /* thread_local. */
30863 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30865 cp_parser_error (parser, "invalid type for property");
30866 declspecs.locations[ds_thread] = 0;
30869 /* typedef. */
30870 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30872 cp_parser_error (parser, "invalid type for property");
30873 declspecs.locations[ds_typedef] = 0;
30876 prefix_attributes = declspecs.attributes;
30877 declspecs.attributes = NULL_TREE;
30879 /* Keep going until we hit the `;' at the end of the declaration. */
30880 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30882 tree attributes, first_attribute, decl;
30883 cp_declarator *declarator;
30884 cp_token *token;
30886 /* Parse the declarator. */
30887 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30888 NULL, NULL, false, false);
30890 /* Look for attributes that apply to the ivar. */
30891 attributes = cp_parser_attributes_opt (parser);
30892 /* Remember which attributes are prefix attributes and
30893 which are not. */
30894 first_attribute = attributes;
30895 /* Combine the attributes. */
30896 attributes = attr_chainon (prefix_attributes, attributes);
30898 decl = grokfield (declarator, &declspecs,
30899 NULL_TREE, /*init_const_expr_p=*/false,
30900 NULL_TREE, attributes);
30902 if (decl == error_mark_node || decl == NULL_TREE)
30903 return error_mark_node;
30905 /* Reset PREFIX_ATTRIBUTES. */
30906 if (attributes != error_mark_node)
30908 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30909 attributes = TREE_CHAIN (attributes);
30910 if (attributes)
30911 TREE_CHAIN (attributes) = NULL_TREE;
30914 DECL_CHAIN (decl) = decls;
30915 decls = decl;
30917 token = cp_lexer_peek_token (parser->lexer);
30918 if (token->type == CPP_COMMA)
30920 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30921 continue;
30923 else
30924 break;
30926 return decls;
30929 /* Parse an Objective-C @property declaration. The syntax is:
30931 objc-property-declaration:
30932 '@property' objc-property-attributes[opt] struct-declaration ;
30934 objc-property-attributes:
30935 '(' objc-property-attribute-list ')'
30937 objc-property-attribute-list:
30938 objc-property-attribute
30939 objc-property-attribute-list, objc-property-attribute
30941 objc-property-attribute
30942 'getter' = identifier
30943 'setter' = identifier
30944 'readonly'
30945 'readwrite'
30946 'assign'
30947 'retain'
30948 'copy'
30949 'nonatomic'
30951 For example:
30952 @property NSString *name;
30953 @property (readonly) id object;
30954 @property (retain, nonatomic, getter=getTheName) id name;
30955 @property int a, b, c;
30957 PS: This function is identical to
30958 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30959 static void
30960 cp_parser_objc_at_property_declaration (cp_parser *parser)
30962 /* The following variables hold the attributes of the properties as
30963 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30964 seen. When we see an attribute, we set them to 'true' (if they
30965 are boolean properties) or to the identifier (if they have an
30966 argument, ie, for getter and setter). Note that here we only
30967 parse the list of attributes, check the syntax and accumulate the
30968 attributes that we find. objc_add_property_declaration() will
30969 then process the information. */
30970 bool property_assign = false;
30971 bool property_copy = false;
30972 tree property_getter_ident = NULL_TREE;
30973 bool property_nonatomic = false;
30974 bool property_readonly = false;
30975 bool property_readwrite = false;
30976 bool property_retain = false;
30977 tree property_setter_ident = NULL_TREE;
30979 /* 'properties' is the list of properties that we read. Usually a
30980 single one, but maybe more (eg, in "@property int a, b, c;" there
30981 are three). */
30982 tree properties;
30983 location_t loc;
30985 loc = cp_lexer_peek_token (parser->lexer)->location;
30987 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30989 /* Parse the optional attribute list... */
30990 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30992 /* Eat the '('. */
30993 matching_parens parens;
30994 parens.consume_open (parser);
30996 while (true)
30998 bool syntax_error = false;
30999 cp_token *token = cp_lexer_peek_token (parser->lexer);
31000 enum rid keyword;
31002 if (token->type != CPP_NAME)
31004 cp_parser_error (parser, "expected identifier");
31005 break;
31007 keyword = C_RID_CODE (token->u.value);
31008 cp_lexer_consume_token (parser->lexer);
31009 switch (keyword)
31011 case RID_ASSIGN: property_assign = true; break;
31012 case RID_COPY: property_copy = true; break;
31013 case RID_NONATOMIC: property_nonatomic = true; break;
31014 case RID_READONLY: property_readonly = true; break;
31015 case RID_READWRITE: property_readwrite = true; break;
31016 case RID_RETAIN: property_retain = true; break;
31018 case RID_GETTER:
31019 case RID_SETTER:
31020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31022 if (keyword == RID_GETTER)
31023 cp_parser_error (parser,
31024 "missing %<=%> (after %<getter%> attribute)");
31025 else
31026 cp_parser_error (parser,
31027 "missing %<=%> (after %<setter%> attribute)");
31028 syntax_error = true;
31029 break;
31031 cp_lexer_consume_token (parser->lexer); /* eat the = */
31032 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31034 cp_parser_error (parser, "expected identifier");
31035 syntax_error = true;
31036 break;
31038 if (keyword == RID_SETTER)
31040 if (property_setter_ident != NULL_TREE)
31042 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31043 cp_lexer_consume_token (parser->lexer);
31045 else
31046 property_setter_ident = cp_parser_objc_selector (parser);
31047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31048 cp_parser_error (parser, "setter name must terminate with %<:%>");
31049 else
31050 cp_lexer_consume_token (parser->lexer);
31052 else
31054 if (property_getter_ident != NULL_TREE)
31056 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31057 cp_lexer_consume_token (parser->lexer);
31059 else
31060 property_getter_ident = cp_parser_objc_selector (parser);
31062 break;
31063 default:
31064 cp_parser_error (parser, "unknown property attribute");
31065 syntax_error = true;
31066 break;
31069 if (syntax_error)
31070 break;
31072 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31073 cp_lexer_consume_token (parser->lexer);
31074 else
31075 break;
31078 /* FIXME: "@property (setter, assign);" will generate a spurious
31079 "error: expected ‘)’ before ‘,’ token". This is because
31080 cp_parser_require, unlike the C counterpart, will produce an
31081 error even if we are in error recovery. */
31082 if (!parens.require_close (parser))
31084 cp_parser_skip_to_closing_parenthesis (parser,
31085 /*recovering=*/true,
31086 /*or_comma=*/false,
31087 /*consume_paren=*/true);
31091 /* ... and the property declaration(s). */
31092 properties = cp_parser_objc_struct_declaration (parser);
31094 if (properties == error_mark_node)
31096 cp_parser_skip_to_end_of_statement (parser);
31097 /* If the next token is now a `;', consume it. */
31098 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31099 cp_lexer_consume_token (parser->lexer);
31100 return;
31103 if (properties == NULL_TREE)
31104 cp_parser_error (parser, "expected identifier");
31105 else
31107 /* Comma-separated properties are chained together in
31108 reverse order; add them one by one. */
31109 properties = nreverse (properties);
31111 for (; properties; properties = TREE_CHAIN (properties))
31112 objc_add_property_declaration (loc, copy_node (properties),
31113 property_readonly, property_readwrite,
31114 property_assign, property_retain,
31115 property_copy, property_nonatomic,
31116 property_getter_ident, property_setter_ident);
31119 cp_parser_consume_semicolon_at_end_of_statement (parser);
31122 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31124 objc-synthesize-declaration:
31125 @synthesize objc-synthesize-identifier-list ;
31127 objc-synthesize-identifier-list:
31128 objc-synthesize-identifier
31129 objc-synthesize-identifier-list, objc-synthesize-identifier
31131 objc-synthesize-identifier
31132 identifier
31133 identifier = identifier
31135 For example:
31136 @synthesize MyProperty;
31137 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31139 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31140 for C. Keep them in sync.
31142 static void
31143 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31145 tree list = NULL_TREE;
31146 location_t loc;
31147 loc = cp_lexer_peek_token (parser->lexer)->location;
31149 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31150 while (true)
31152 tree property, ivar;
31153 property = cp_parser_identifier (parser);
31154 if (property == error_mark_node)
31156 cp_parser_consume_semicolon_at_end_of_statement (parser);
31157 return;
31159 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31161 cp_lexer_consume_token (parser->lexer);
31162 ivar = cp_parser_identifier (parser);
31163 if (ivar == error_mark_node)
31165 cp_parser_consume_semicolon_at_end_of_statement (parser);
31166 return;
31169 else
31170 ivar = NULL_TREE;
31171 list = chainon (list, build_tree_list (ivar, property));
31172 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31173 cp_lexer_consume_token (parser->lexer);
31174 else
31175 break;
31177 cp_parser_consume_semicolon_at_end_of_statement (parser);
31178 objc_add_synthesize_declaration (loc, list);
31181 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31183 objc-dynamic-declaration:
31184 @dynamic identifier-list ;
31186 For example:
31187 @dynamic MyProperty;
31188 @dynamic MyProperty, AnotherProperty;
31190 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31191 for C. Keep them in sync.
31193 static void
31194 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31196 tree list = NULL_TREE;
31197 location_t loc;
31198 loc = cp_lexer_peek_token (parser->lexer)->location;
31200 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31201 while (true)
31203 tree property;
31204 property = cp_parser_identifier (parser);
31205 if (property == error_mark_node)
31207 cp_parser_consume_semicolon_at_end_of_statement (parser);
31208 return;
31210 list = chainon (list, build_tree_list (NULL, property));
31211 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31212 cp_lexer_consume_token (parser->lexer);
31213 else
31214 break;
31216 cp_parser_consume_semicolon_at_end_of_statement (parser);
31217 objc_add_dynamic_declaration (loc, list);
31221 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31223 /* Returns name of the next clause.
31224 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31225 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31226 returned and the token is consumed. */
31228 static pragma_omp_clause
31229 cp_parser_omp_clause_name (cp_parser *parser)
31231 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31233 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31234 result = PRAGMA_OACC_CLAUSE_AUTO;
31235 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31236 result = PRAGMA_OMP_CLAUSE_IF;
31237 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31238 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31239 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31240 result = PRAGMA_OACC_CLAUSE_DELETE;
31241 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31242 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31243 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31244 result = PRAGMA_OMP_CLAUSE_FOR;
31245 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31247 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31248 const char *p = IDENTIFIER_POINTER (id);
31250 switch (p[0])
31252 case 'a':
31253 if (!strcmp ("aligned", p))
31254 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31255 else if (!strcmp ("async", p))
31256 result = PRAGMA_OACC_CLAUSE_ASYNC;
31257 break;
31258 case 'c':
31259 if (!strcmp ("collapse", p))
31260 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31261 else if (!strcmp ("copy", p))
31262 result = PRAGMA_OACC_CLAUSE_COPY;
31263 else if (!strcmp ("copyin", p))
31264 result = PRAGMA_OMP_CLAUSE_COPYIN;
31265 else if (!strcmp ("copyout", p))
31266 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31267 else if (!strcmp ("copyprivate", p))
31268 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31269 else if (!strcmp ("create", p))
31270 result = PRAGMA_OACC_CLAUSE_CREATE;
31271 break;
31272 case 'd':
31273 if (!strcmp ("defaultmap", p))
31274 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31275 else if (!strcmp ("depend", p))
31276 result = PRAGMA_OMP_CLAUSE_DEPEND;
31277 else if (!strcmp ("device", p))
31278 result = PRAGMA_OMP_CLAUSE_DEVICE;
31279 else if (!strcmp ("deviceptr", p))
31280 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31281 else if (!strcmp ("device_resident", p))
31282 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31283 else if (!strcmp ("dist_schedule", p))
31284 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31285 break;
31286 case 'f':
31287 if (!strcmp ("final", p))
31288 result = PRAGMA_OMP_CLAUSE_FINAL;
31289 else if (!strcmp ("firstprivate", p))
31290 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31291 else if (!strcmp ("from", p))
31292 result = PRAGMA_OMP_CLAUSE_FROM;
31293 break;
31294 case 'g':
31295 if (!strcmp ("gang", p))
31296 result = PRAGMA_OACC_CLAUSE_GANG;
31297 else if (!strcmp ("grainsize", p))
31298 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31299 break;
31300 case 'h':
31301 if (!strcmp ("hint", p))
31302 result = PRAGMA_OMP_CLAUSE_HINT;
31303 else if (!strcmp ("host", p))
31304 result = PRAGMA_OACC_CLAUSE_HOST;
31305 break;
31306 case 'i':
31307 if (!strcmp ("inbranch", p))
31308 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31309 else if (!strcmp ("independent", p))
31310 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31311 else if (!strcmp ("is_device_ptr", p))
31312 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31313 break;
31314 case 'l':
31315 if (!strcmp ("lastprivate", p))
31316 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31317 else if (!strcmp ("linear", p))
31318 result = PRAGMA_OMP_CLAUSE_LINEAR;
31319 else if (!strcmp ("link", p))
31320 result = PRAGMA_OMP_CLAUSE_LINK;
31321 break;
31322 case 'm':
31323 if (!strcmp ("map", p))
31324 result = PRAGMA_OMP_CLAUSE_MAP;
31325 else if (!strcmp ("mergeable", p))
31326 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31327 break;
31328 case 'n':
31329 if (!strcmp ("nogroup", p))
31330 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31331 else if (!strcmp ("notinbranch", p))
31332 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31333 else if (!strcmp ("nowait", p))
31334 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31335 else if (!strcmp ("num_gangs", p))
31336 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31337 else if (!strcmp ("num_tasks", p))
31338 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31339 else if (!strcmp ("num_teams", p))
31340 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31341 else if (!strcmp ("num_threads", p))
31342 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31343 else if (!strcmp ("num_workers", p))
31344 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31345 break;
31346 case 'o':
31347 if (!strcmp ("ordered", p))
31348 result = PRAGMA_OMP_CLAUSE_ORDERED;
31349 break;
31350 case 'p':
31351 if (!strcmp ("parallel", p))
31352 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31353 else if (!strcmp ("present", p))
31354 result = PRAGMA_OACC_CLAUSE_PRESENT;
31355 else if (!strcmp ("present_or_copy", p)
31356 || !strcmp ("pcopy", p))
31357 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31358 else if (!strcmp ("present_or_copyin", p)
31359 || !strcmp ("pcopyin", p))
31360 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31361 else if (!strcmp ("present_or_copyout", p)
31362 || !strcmp ("pcopyout", p))
31363 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31364 else if (!strcmp ("present_or_create", p)
31365 || !strcmp ("pcreate", p))
31366 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31367 else if (!strcmp ("priority", p))
31368 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31369 else if (!strcmp ("proc_bind", p))
31370 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31371 break;
31372 case 'r':
31373 if (!strcmp ("reduction", p))
31374 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31375 break;
31376 case 's':
31377 if (!strcmp ("safelen", p))
31378 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31379 else if (!strcmp ("schedule", p))
31380 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31381 else if (!strcmp ("sections", p))
31382 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31383 else if (!strcmp ("self", p))
31384 result = PRAGMA_OACC_CLAUSE_SELF;
31385 else if (!strcmp ("seq", p))
31386 result = PRAGMA_OACC_CLAUSE_SEQ;
31387 else if (!strcmp ("shared", p))
31388 result = PRAGMA_OMP_CLAUSE_SHARED;
31389 else if (!strcmp ("simd", p))
31390 result = PRAGMA_OMP_CLAUSE_SIMD;
31391 else if (!strcmp ("simdlen", p))
31392 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31393 break;
31394 case 't':
31395 if (!strcmp ("taskgroup", p))
31396 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31397 else if (!strcmp ("thread_limit", p))
31398 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31399 else if (!strcmp ("threads", p))
31400 result = PRAGMA_OMP_CLAUSE_THREADS;
31401 else if (!strcmp ("tile", p))
31402 result = PRAGMA_OACC_CLAUSE_TILE;
31403 else if (!strcmp ("to", p))
31404 result = PRAGMA_OMP_CLAUSE_TO;
31405 break;
31406 case 'u':
31407 if (!strcmp ("uniform", p))
31408 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31409 else if (!strcmp ("untied", p))
31410 result = PRAGMA_OMP_CLAUSE_UNTIED;
31411 else if (!strcmp ("use_device", p))
31412 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31413 else if (!strcmp ("use_device_ptr", p))
31414 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31415 break;
31416 case 'v':
31417 if (!strcmp ("vector", p))
31418 result = PRAGMA_OACC_CLAUSE_VECTOR;
31419 else if (!strcmp ("vector_length", p))
31420 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31421 break;
31422 case 'w':
31423 if (!strcmp ("wait", p))
31424 result = PRAGMA_OACC_CLAUSE_WAIT;
31425 else if (!strcmp ("worker", p))
31426 result = PRAGMA_OACC_CLAUSE_WORKER;
31427 break;
31431 if (result != PRAGMA_OMP_CLAUSE_NONE)
31432 cp_lexer_consume_token (parser->lexer);
31434 return result;
31437 /* Validate that a clause of the given type does not already exist. */
31439 static void
31440 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31441 const char *name, location_t location)
31443 tree c;
31445 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31446 if (OMP_CLAUSE_CODE (c) == code)
31448 error_at (location, "too many %qs clauses", name);
31449 break;
31453 /* OpenMP 2.5:
31454 variable-list:
31455 identifier
31456 variable-list , identifier
31458 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31459 colon). An opening parenthesis will have been consumed by the caller.
31461 If KIND is nonzero, create the appropriate node and install the decl
31462 in OMP_CLAUSE_DECL and add the node to the head of the list.
31464 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31465 return the list created.
31467 COLON can be NULL if only closing parenthesis should end the list,
31468 or pointer to bool which will receive false if the list is terminated
31469 by closing parenthesis or true if the list is terminated by colon. */
31471 static tree
31472 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31473 tree list, bool *colon)
31475 cp_token *token;
31476 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31477 if (colon)
31479 parser->colon_corrects_to_scope_p = false;
31480 *colon = false;
31482 while (1)
31484 tree name, decl;
31486 token = cp_lexer_peek_token (parser->lexer);
31487 if (kind != 0
31488 && current_class_ptr
31489 && cp_parser_is_keyword (token, RID_THIS))
31491 decl = finish_this_expr ();
31492 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31493 || CONVERT_EXPR_P (decl))
31494 decl = TREE_OPERAND (decl, 0);
31495 cp_lexer_consume_token (parser->lexer);
31497 else
31499 name = cp_parser_id_expression (parser, /*template_p=*/false,
31500 /*check_dependency_p=*/true,
31501 /*template_p=*/NULL,
31502 /*declarator_p=*/false,
31503 /*optional_p=*/false);
31504 if (name == error_mark_node)
31505 goto skip_comma;
31507 if (identifier_p (name))
31508 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31509 else
31510 decl = name;
31511 if (decl == error_mark_node)
31512 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31513 token->location);
31515 if (decl == error_mark_node)
31517 else if (kind != 0)
31519 switch (kind)
31521 case OMP_CLAUSE__CACHE_:
31522 /* The OpenACC cache directive explicitly only allows "array
31523 elements or subarrays". */
31524 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31526 error_at (token->location, "expected %<[%>");
31527 decl = error_mark_node;
31528 break;
31530 /* FALLTHROUGH. */
31531 case OMP_CLAUSE_MAP:
31532 case OMP_CLAUSE_FROM:
31533 case OMP_CLAUSE_TO:
31534 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31536 location_t loc
31537 = cp_lexer_peek_token (parser->lexer)->location;
31538 cp_id_kind idk = CP_ID_KIND_NONE;
31539 cp_lexer_consume_token (parser->lexer);
31540 decl = convert_from_reference (decl);
31541 decl
31542 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31543 decl, false,
31544 &idk, loc);
31546 /* FALLTHROUGH. */
31547 case OMP_CLAUSE_DEPEND:
31548 case OMP_CLAUSE_REDUCTION:
31549 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31551 tree low_bound = NULL_TREE, length = NULL_TREE;
31553 parser->colon_corrects_to_scope_p = false;
31554 cp_lexer_consume_token (parser->lexer);
31555 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31556 low_bound = cp_parser_expression (parser);
31557 if (!colon)
31558 parser->colon_corrects_to_scope_p
31559 = saved_colon_corrects_to_scope_p;
31560 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31561 length = integer_one_node;
31562 else
31564 /* Look for `:'. */
31565 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31566 goto skip_comma;
31567 if (!cp_lexer_next_token_is (parser->lexer,
31568 CPP_CLOSE_SQUARE))
31569 length = cp_parser_expression (parser);
31571 /* Look for the closing `]'. */
31572 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31573 RT_CLOSE_SQUARE))
31574 goto skip_comma;
31576 decl = tree_cons (low_bound, length, decl);
31578 break;
31579 default:
31580 break;
31583 tree u = build_omp_clause (token->location, kind);
31584 OMP_CLAUSE_DECL (u) = decl;
31585 OMP_CLAUSE_CHAIN (u) = list;
31586 list = u;
31588 else
31589 list = tree_cons (decl, NULL_TREE, list);
31591 get_comma:
31592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31593 break;
31594 cp_lexer_consume_token (parser->lexer);
31597 if (colon)
31598 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31600 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31602 *colon = true;
31603 cp_parser_require (parser, CPP_COLON, RT_COLON);
31604 return list;
31607 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31609 int ending;
31611 /* Try to resync to an unnested comma. Copied from
31612 cp_parser_parenthesized_expression_list. */
31613 skip_comma:
31614 if (colon)
31615 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31616 ending = cp_parser_skip_to_closing_parenthesis (parser,
31617 /*recovering=*/true,
31618 /*or_comma=*/true,
31619 /*consume_paren=*/true);
31620 if (ending < 0)
31621 goto get_comma;
31624 return list;
31627 /* Similarly, but expect leading and trailing parenthesis. This is a very
31628 common case for omp clauses. */
31630 static tree
31631 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31633 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31634 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31635 return list;
31638 /* OpenACC 2.0:
31639 copy ( variable-list )
31640 copyin ( variable-list )
31641 copyout ( variable-list )
31642 create ( variable-list )
31643 delete ( variable-list )
31644 present ( variable-list )
31645 present_or_copy ( variable-list )
31646 pcopy ( variable-list )
31647 present_or_copyin ( variable-list )
31648 pcopyin ( variable-list )
31649 present_or_copyout ( variable-list )
31650 pcopyout ( variable-list )
31651 present_or_create ( variable-list )
31652 pcreate ( variable-list ) */
31654 static tree
31655 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31656 tree list)
31658 enum gomp_map_kind kind;
31659 switch (c_kind)
31661 case PRAGMA_OACC_CLAUSE_COPY:
31662 kind = GOMP_MAP_FORCE_TOFROM;
31663 break;
31664 case PRAGMA_OACC_CLAUSE_COPYIN:
31665 kind = GOMP_MAP_FORCE_TO;
31666 break;
31667 case PRAGMA_OACC_CLAUSE_COPYOUT:
31668 kind = GOMP_MAP_FORCE_FROM;
31669 break;
31670 case PRAGMA_OACC_CLAUSE_CREATE:
31671 kind = GOMP_MAP_FORCE_ALLOC;
31672 break;
31673 case PRAGMA_OACC_CLAUSE_DELETE:
31674 kind = GOMP_MAP_DELETE;
31675 break;
31676 case PRAGMA_OACC_CLAUSE_DEVICE:
31677 kind = GOMP_MAP_FORCE_TO;
31678 break;
31679 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31680 kind = GOMP_MAP_DEVICE_RESIDENT;
31681 break;
31682 case PRAGMA_OACC_CLAUSE_HOST:
31683 case PRAGMA_OACC_CLAUSE_SELF:
31684 kind = GOMP_MAP_FORCE_FROM;
31685 break;
31686 case PRAGMA_OACC_CLAUSE_LINK:
31687 kind = GOMP_MAP_LINK;
31688 break;
31689 case PRAGMA_OACC_CLAUSE_PRESENT:
31690 kind = GOMP_MAP_FORCE_PRESENT;
31691 break;
31692 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31693 kind = GOMP_MAP_TOFROM;
31694 break;
31695 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31696 kind = GOMP_MAP_TO;
31697 break;
31698 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31699 kind = GOMP_MAP_FROM;
31700 break;
31701 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31702 kind = GOMP_MAP_ALLOC;
31703 break;
31704 default:
31705 gcc_unreachable ();
31707 tree nl, c;
31708 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31710 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31711 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31713 return nl;
31716 /* OpenACC 2.0:
31717 deviceptr ( variable-list ) */
31719 static tree
31720 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31722 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31723 tree vars, t;
31725 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31726 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31727 variable-list must only allow for pointer variables. */
31728 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31729 for (t = vars; t; t = TREE_CHAIN (t))
31731 tree v = TREE_PURPOSE (t);
31732 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31733 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31734 OMP_CLAUSE_DECL (u) = v;
31735 OMP_CLAUSE_CHAIN (u) = list;
31736 list = u;
31739 return list;
31742 /* OpenACC 2.0:
31743 auto
31744 independent
31745 nohost
31746 seq */
31748 static tree
31749 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31750 enum omp_clause_code code,
31751 tree list, location_t location)
31753 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31754 tree c = build_omp_clause (location, code);
31755 OMP_CLAUSE_CHAIN (c) = list;
31756 return c;
31759 /* OpenACC:
31760 num_gangs ( expression )
31761 num_workers ( expression )
31762 vector_length ( expression ) */
31764 static tree
31765 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31766 const char *str, tree list)
31768 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31770 matching_parens parens;
31771 if (!parens.require_open (parser))
31772 return list;
31774 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31776 if (t == error_mark_node
31777 || !parens.require_close (parser))
31779 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31780 /*or_comma=*/false,
31781 /*consume_paren=*/true);
31782 return list;
31785 check_no_duplicate_clause (list, code, str, loc);
31787 tree c = build_omp_clause (loc, code);
31788 OMP_CLAUSE_OPERAND (c, 0) = t;
31789 OMP_CLAUSE_CHAIN (c) = list;
31790 return c;
31793 /* OpenACC:
31795 gang [( gang-arg-list )]
31796 worker [( [num:] int-expr )]
31797 vector [( [length:] int-expr )]
31799 where gang-arg is one of:
31801 [num:] int-expr
31802 static: size-expr
31804 and size-expr may be:
31807 int-expr
31810 static tree
31811 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31812 const char *str, tree list)
31814 const char *id = "num";
31815 cp_lexer *lexer = parser->lexer;
31816 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31817 location_t loc = cp_lexer_peek_token (lexer)->location;
31819 if (kind == OMP_CLAUSE_VECTOR)
31820 id = "length";
31822 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31824 matching_parens parens;
31825 parens.consume_open (parser);
31829 cp_token *next = cp_lexer_peek_token (lexer);
31830 int idx = 0;
31832 /* Gang static argument. */
31833 if (kind == OMP_CLAUSE_GANG
31834 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31836 cp_lexer_consume_token (lexer);
31838 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31839 goto cleanup_error;
31841 idx = 1;
31842 if (ops[idx] != NULL)
31844 cp_parser_error (parser, "too many %<static%> arguments");
31845 goto cleanup_error;
31848 /* Check for the '*' argument. */
31849 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31850 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31851 || cp_lexer_nth_token_is (parser->lexer, 2,
31852 CPP_CLOSE_PAREN)))
31854 cp_lexer_consume_token (lexer);
31855 ops[idx] = integer_minus_one_node;
31857 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31859 cp_lexer_consume_token (lexer);
31860 continue;
31862 else break;
31865 /* Worker num: argument and vector length: arguments. */
31866 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31867 && id_equal (next->u.value, id)
31868 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31870 cp_lexer_consume_token (lexer); /* id */
31871 cp_lexer_consume_token (lexer); /* ':' */
31874 /* Now collect the actual argument. */
31875 if (ops[idx] != NULL_TREE)
31877 cp_parser_error (parser, "unexpected argument");
31878 goto cleanup_error;
31881 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31882 false);
31883 if (expr == error_mark_node)
31884 goto cleanup_error;
31886 mark_exp_read (expr);
31887 ops[idx] = expr;
31889 if (kind == OMP_CLAUSE_GANG
31890 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31892 cp_lexer_consume_token (lexer);
31893 continue;
31895 break;
31897 while (1);
31899 if (!parens.require_close (parser))
31900 goto cleanup_error;
31903 check_no_duplicate_clause (list, kind, str, loc);
31905 c = build_omp_clause (loc, kind);
31907 if (ops[1])
31908 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31910 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31911 OMP_CLAUSE_CHAIN (c) = list;
31913 return c;
31915 cleanup_error:
31916 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31917 return list;
31920 /* OpenACC 2.0:
31921 tile ( size-expr-list ) */
31923 static tree
31924 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31926 tree c, expr = error_mark_node;
31927 tree tile = NULL_TREE;
31929 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31930 so, but the spec authors never considered such a case and have
31931 differing opinions on what it might mean, including 'not
31932 allowed'.) */
31933 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31934 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31935 clause_loc);
31937 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31938 return list;
31942 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31943 return list;
31945 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31946 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31947 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31949 cp_lexer_consume_token (parser->lexer);
31950 expr = integer_zero_node;
31952 else
31953 expr = cp_parser_constant_expression (parser);
31955 tile = tree_cons (NULL_TREE, expr, tile);
31957 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31959 /* Consume the trailing ')'. */
31960 cp_lexer_consume_token (parser->lexer);
31962 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31963 tile = nreverse (tile);
31964 OMP_CLAUSE_TILE_LIST (c) = tile;
31965 OMP_CLAUSE_CHAIN (c) = list;
31966 return c;
31969 /* OpenACC 2.0
31970 Parse wait clause or directive parameters. */
31972 static tree
31973 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31975 vec<tree, va_gc> *args;
31976 tree t, args_tree;
31978 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31979 /*cast_p=*/false,
31980 /*allow_expansion_p=*/true,
31981 /*non_constant_p=*/NULL);
31983 if (args == NULL || args->length () == 0)
31985 cp_parser_error (parser, "expected integer expression before ')'");
31986 if (args != NULL)
31987 release_tree_vector (args);
31988 return list;
31991 args_tree = build_tree_list_vec (args);
31993 release_tree_vector (args);
31995 for (t = args_tree; t; t = TREE_CHAIN (t))
31997 tree targ = TREE_VALUE (t);
31999 if (targ != error_mark_node)
32001 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32002 error ("%<wait%> expression must be integral");
32003 else
32005 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32007 targ = mark_rvalue_use (targ);
32008 OMP_CLAUSE_DECL (c) = targ;
32009 OMP_CLAUSE_CHAIN (c) = list;
32010 list = c;
32015 return list;
32018 /* OpenACC:
32019 wait ( int-expr-list ) */
32021 static tree
32022 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32024 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32026 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32027 return list;
32029 list = cp_parser_oacc_wait_list (parser, location, list);
32031 return list;
32034 /* OpenMP 3.0:
32035 collapse ( constant-expression ) */
32037 static tree
32038 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32040 tree c, num;
32041 location_t loc;
32042 HOST_WIDE_INT n;
32044 loc = cp_lexer_peek_token (parser->lexer)->location;
32045 matching_parens parens;
32046 if (!parens.require_open (parser))
32047 return list;
32049 num = cp_parser_constant_expression (parser);
32051 if (!parens.require_close (parser))
32052 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32053 /*or_comma=*/false,
32054 /*consume_paren=*/true);
32056 if (num == error_mark_node)
32057 return list;
32058 num = fold_non_dependent_expr (num);
32059 if (!tree_fits_shwi_p (num)
32060 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32061 || (n = tree_to_shwi (num)) <= 0
32062 || (int) n != n)
32064 error_at (loc, "collapse argument needs positive constant integer expression");
32065 return list;
32068 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32069 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32070 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32071 OMP_CLAUSE_CHAIN (c) = list;
32072 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32074 return c;
32077 /* OpenMP 2.5:
32078 default ( none | shared )
32080 OpenACC:
32081 default ( none | present ) */
32083 static tree
32084 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32085 location_t location, bool is_oacc)
32087 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32088 tree c;
32090 matching_parens parens;
32091 if (!parens.require_open (parser))
32092 return list;
32093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32095 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32096 const char *p = IDENTIFIER_POINTER (id);
32098 switch (p[0])
32100 case 'n':
32101 if (strcmp ("none", p) != 0)
32102 goto invalid_kind;
32103 kind = OMP_CLAUSE_DEFAULT_NONE;
32104 break;
32106 case 'p':
32107 if (strcmp ("present", p) != 0 || !is_oacc)
32108 goto invalid_kind;
32109 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32110 break;
32112 case 's':
32113 if (strcmp ("shared", p) != 0 || is_oacc)
32114 goto invalid_kind;
32115 kind = OMP_CLAUSE_DEFAULT_SHARED;
32116 break;
32118 default:
32119 goto invalid_kind;
32122 cp_lexer_consume_token (parser->lexer);
32124 else
32126 invalid_kind:
32127 if (is_oacc)
32128 cp_parser_error (parser, "expected %<none%> or %<present%>");
32129 else
32130 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32133 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32134 || !parens.require_close (parser))
32135 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32136 /*or_comma=*/false,
32137 /*consume_paren=*/true);
32139 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32140 return list;
32142 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32143 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32144 OMP_CLAUSE_CHAIN (c) = list;
32145 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32147 return c;
32150 /* OpenMP 3.1:
32151 final ( expression ) */
32153 static tree
32154 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32156 tree t, c;
32158 matching_parens parens;
32159 if (!parens.require_open (parser))
32160 return list;
32162 t = cp_parser_condition (parser);
32164 if (t == error_mark_node
32165 || !parens.require_close (parser))
32166 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32167 /*or_comma=*/false,
32168 /*consume_paren=*/true);
32170 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32172 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32173 OMP_CLAUSE_FINAL_EXPR (c) = t;
32174 OMP_CLAUSE_CHAIN (c) = list;
32176 return c;
32179 /* OpenMP 2.5:
32180 if ( expression )
32182 OpenMP 4.5:
32183 if ( directive-name-modifier : expression )
32185 directive-name-modifier:
32186 parallel | task | taskloop | target data | target | target update
32187 | target enter data | target exit data */
32189 static tree
32190 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32191 bool is_omp)
32193 tree t, c;
32194 enum tree_code if_modifier = ERROR_MARK;
32196 matching_parens parens;
32197 if (!parens.require_open (parser))
32198 return list;
32200 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32202 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32203 const char *p = IDENTIFIER_POINTER (id);
32204 int n = 2;
32206 if (strcmp ("parallel", p) == 0)
32207 if_modifier = OMP_PARALLEL;
32208 else if (strcmp ("task", p) == 0)
32209 if_modifier = OMP_TASK;
32210 else if (strcmp ("taskloop", p) == 0)
32211 if_modifier = OMP_TASKLOOP;
32212 else if (strcmp ("target", p) == 0)
32214 if_modifier = OMP_TARGET;
32215 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32217 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32218 p = IDENTIFIER_POINTER (id);
32219 if (strcmp ("data", p) == 0)
32220 if_modifier = OMP_TARGET_DATA;
32221 else if (strcmp ("update", p) == 0)
32222 if_modifier = OMP_TARGET_UPDATE;
32223 else if (strcmp ("enter", p) == 0)
32224 if_modifier = OMP_TARGET_ENTER_DATA;
32225 else if (strcmp ("exit", p) == 0)
32226 if_modifier = OMP_TARGET_EXIT_DATA;
32227 if (if_modifier != OMP_TARGET)
32228 n = 3;
32229 else
32231 location_t loc
32232 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32233 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32234 "or %<exit%>");
32235 if_modifier = ERROR_MARK;
32237 if (if_modifier == OMP_TARGET_ENTER_DATA
32238 || if_modifier == OMP_TARGET_EXIT_DATA)
32240 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32242 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32243 p = IDENTIFIER_POINTER (id);
32244 if (strcmp ("data", p) == 0)
32245 n = 4;
32247 if (n != 4)
32249 location_t loc
32250 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32251 error_at (loc, "expected %<data%>");
32252 if_modifier = ERROR_MARK;
32257 if (if_modifier != ERROR_MARK)
32259 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32261 while (n-- > 0)
32262 cp_lexer_consume_token (parser->lexer);
32264 else
32266 if (n > 2)
32268 location_t loc
32269 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32270 error_at (loc, "expected %<:%>");
32272 if_modifier = ERROR_MARK;
32277 t = cp_parser_condition (parser);
32279 if (t == error_mark_node
32280 || !parens.require_close (parser))
32281 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32282 /*or_comma=*/false,
32283 /*consume_paren=*/true);
32285 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32286 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32288 if (if_modifier != ERROR_MARK
32289 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32291 const char *p = NULL;
32292 switch (if_modifier)
32294 case OMP_PARALLEL: p = "parallel"; break;
32295 case OMP_TASK: p = "task"; break;
32296 case OMP_TASKLOOP: p = "taskloop"; break;
32297 case OMP_TARGET_DATA: p = "target data"; break;
32298 case OMP_TARGET: p = "target"; break;
32299 case OMP_TARGET_UPDATE: p = "target update"; break;
32300 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32301 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32302 default: gcc_unreachable ();
32304 error_at (location, "too many %<if%> clauses with %qs modifier",
32306 return list;
32308 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32310 if (!is_omp)
32311 error_at (location, "too many %<if%> clauses");
32312 else
32313 error_at (location, "too many %<if%> clauses without modifier");
32314 return list;
32316 else if (if_modifier == ERROR_MARK
32317 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32319 error_at (location, "if any %<if%> clause has modifier, then all "
32320 "%<if%> clauses have to use modifier");
32321 return list;
32325 c = build_omp_clause (location, OMP_CLAUSE_IF);
32326 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32327 OMP_CLAUSE_IF_EXPR (c) = t;
32328 OMP_CLAUSE_CHAIN (c) = list;
32330 return c;
32333 /* OpenMP 3.1:
32334 mergeable */
32336 static tree
32337 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32338 tree list, location_t location)
32340 tree c;
32342 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32343 location);
32345 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32346 OMP_CLAUSE_CHAIN (c) = list;
32347 return c;
32350 /* OpenMP 2.5:
32351 nowait */
32353 static tree
32354 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32355 tree list, location_t location)
32357 tree c;
32359 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32361 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32362 OMP_CLAUSE_CHAIN (c) = list;
32363 return c;
32366 /* OpenMP 2.5:
32367 num_threads ( expression ) */
32369 static tree
32370 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32371 location_t location)
32373 tree t, c;
32375 matching_parens parens;
32376 if (!parens.require_open (parser))
32377 return list;
32379 t = cp_parser_expression (parser);
32381 if (t == error_mark_node
32382 || !parens.require_close (parser))
32383 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32384 /*or_comma=*/false,
32385 /*consume_paren=*/true);
32387 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32388 "num_threads", location);
32390 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32391 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32392 OMP_CLAUSE_CHAIN (c) = list;
32394 return c;
32397 /* OpenMP 4.5:
32398 num_tasks ( expression ) */
32400 static tree
32401 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32402 location_t location)
32404 tree t, c;
32406 matching_parens parens;
32407 if (!parens.require_open (parser))
32408 return list;
32410 t = cp_parser_expression (parser);
32412 if (t == error_mark_node
32413 || !parens.require_close (parser))
32414 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32415 /*or_comma=*/false,
32416 /*consume_paren=*/true);
32418 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32419 "num_tasks", location);
32421 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32422 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32423 OMP_CLAUSE_CHAIN (c) = list;
32425 return c;
32428 /* OpenMP 4.5:
32429 grainsize ( expression ) */
32431 static tree
32432 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32433 location_t location)
32435 tree t, c;
32437 matching_parens parens;
32438 if (!parens.require_open (parser))
32439 return list;
32441 t = cp_parser_expression (parser);
32443 if (t == error_mark_node
32444 || !parens.require_close (parser))
32445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32446 /*or_comma=*/false,
32447 /*consume_paren=*/true);
32449 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32450 "grainsize", location);
32452 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32453 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32454 OMP_CLAUSE_CHAIN (c) = list;
32456 return c;
32459 /* OpenMP 4.5:
32460 priority ( expression ) */
32462 static tree
32463 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32464 location_t location)
32466 tree t, c;
32468 matching_parens parens;
32469 if (!parens.require_open (parser))
32470 return list;
32472 t = cp_parser_expression (parser);
32474 if (t == error_mark_node
32475 || !parens.require_close (parser))
32476 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32477 /*or_comma=*/false,
32478 /*consume_paren=*/true);
32480 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32481 "priority", location);
32483 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32484 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32485 OMP_CLAUSE_CHAIN (c) = list;
32487 return c;
32490 /* OpenMP 4.5:
32491 hint ( expression ) */
32493 static tree
32494 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32495 location_t location)
32497 tree t, c;
32499 matching_parens parens;
32500 if (!parens.require_open (parser))
32501 return list;
32503 t = cp_parser_expression (parser);
32505 if (t == error_mark_node
32506 || !parens.require_close (parser))
32507 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32508 /*or_comma=*/false,
32509 /*consume_paren=*/true);
32511 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32513 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32514 OMP_CLAUSE_HINT_EXPR (c) = t;
32515 OMP_CLAUSE_CHAIN (c) = list;
32517 return c;
32520 /* OpenMP 4.5:
32521 defaultmap ( tofrom : scalar ) */
32523 static tree
32524 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32525 location_t location)
32527 tree c, id;
32528 const char *p;
32530 matching_parens parens;
32531 if (!parens.require_open (parser))
32532 return list;
32534 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32536 cp_parser_error (parser, "expected %<tofrom%>");
32537 goto out_err;
32539 id = cp_lexer_peek_token (parser->lexer)->u.value;
32540 p = IDENTIFIER_POINTER (id);
32541 if (strcmp (p, "tofrom") != 0)
32543 cp_parser_error (parser, "expected %<tofrom%>");
32544 goto out_err;
32546 cp_lexer_consume_token (parser->lexer);
32547 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32548 goto out_err;
32550 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32552 cp_parser_error (parser, "expected %<scalar%>");
32553 goto out_err;
32555 id = cp_lexer_peek_token (parser->lexer)->u.value;
32556 p = IDENTIFIER_POINTER (id);
32557 if (strcmp (p, "scalar") != 0)
32559 cp_parser_error (parser, "expected %<scalar%>");
32560 goto out_err;
32562 cp_lexer_consume_token (parser->lexer);
32563 if (!parens.require_close (parser))
32564 goto out_err;
32566 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32567 location);
32569 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32570 OMP_CLAUSE_CHAIN (c) = list;
32571 return c;
32573 out_err:
32574 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32575 /*or_comma=*/false,
32576 /*consume_paren=*/true);
32577 return list;
32580 /* OpenMP 2.5:
32581 ordered
32583 OpenMP 4.5:
32584 ordered ( constant-expression ) */
32586 static tree
32587 cp_parser_omp_clause_ordered (cp_parser *parser,
32588 tree list, location_t location)
32590 tree c, num = NULL_TREE;
32591 HOST_WIDE_INT n;
32593 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32594 "ordered", location);
32596 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32598 matching_parens parens;
32599 parens.consume_open (parser);
32601 num = cp_parser_constant_expression (parser);
32603 if (!parens.require_close (parser))
32604 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32605 /*or_comma=*/false,
32606 /*consume_paren=*/true);
32608 if (num == error_mark_node)
32609 return list;
32610 num = fold_non_dependent_expr (num);
32611 if (!tree_fits_shwi_p (num)
32612 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32613 || (n = tree_to_shwi (num)) <= 0
32614 || (int) n != n)
32616 error_at (location,
32617 "ordered argument needs positive constant integer "
32618 "expression");
32619 return list;
32623 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32624 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32625 OMP_CLAUSE_CHAIN (c) = list;
32626 return c;
32629 /* OpenMP 2.5:
32630 reduction ( reduction-operator : variable-list )
32632 reduction-operator:
32633 One of: + * - & ^ | && ||
32635 OpenMP 3.1:
32637 reduction-operator:
32638 One of: + * - & ^ | && || min max
32640 OpenMP 4.0:
32642 reduction-operator:
32643 One of: + * - & ^ | && ||
32644 id-expression */
32646 static tree
32647 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32649 enum tree_code code = ERROR_MARK;
32650 tree nlist, c, id = NULL_TREE;
32652 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32653 return list;
32655 switch (cp_lexer_peek_token (parser->lexer)->type)
32657 case CPP_PLUS: code = PLUS_EXPR; break;
32658 case CPP_MULT: code = MULT_EXPR; break;
32659 case CPP_MINUS: code = MINUS_EXPR; break;
32660 case CPP_AND: code = BIT_AND_EXPR; break;
32661 case CPP_XOR: code = BIT_XOR_EXPR; break;
32662 case CPP_OR: code = BIT_IOR_EXPR; break;
32663 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32664 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32665 default: break;
32668 if (code != ERROR_MARK)
32669 cp_lexer_consume_token (parser->lexer);
32670 else
32672 bool saved_colon_corrects_to_scope_p;
32673 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32674 parser->colon_corrects_to_scope_p = false;
32675 id = cp_parser_id_expression (parser, /*template_p=*/false,
32676 /*check_dependency_p=*/true,
32677 /*template_p=*/NULL,
32678 /*declarator_p=*/false,
32679 /*optional_p=*/false);
32680 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32681 if (identifier_p (id))
32683 const char *p = IDENTIFIER_POINTER (id);
32685 if (strcmp (p, "min") == 0)
32686 code = MIN_EXPR;
32687 else if (strcmp (p, "max") == 0)
32688 code = MAX_EXPR;
32689 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32690 code = PLUS_EXPR;
32691 else if (id == ovl_op_identifier (false, MULT_EXPR))
32692 code = MULT_EXPR;
32693 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32694 code = MINUS_EXPR;
32695 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32696 code = BIT_AND_EXPR;
32697 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32698 code = BIT_IOR_EXPR;
32699 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32700 code = BIT_XOR_EXPR;
32701 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32702 code = TRUTH_ANDIF_EXPR;
32703 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32704 code = TRUTH_ORIF_EXPR;
32705 id = omp_reduction_id (code, id, NULL_TREE);
32706 tree scope = parser->scope;
32707 if (scope)
32708 id = build_qualified_name (NULL_TREE, scope, id, false);
32709 parser->scope = NULL_TREE;
32710 parser->qualifying_scope = NULL_TREE;
32711 parser->object_scope = NULL_TREE;
32713 else
32715 error ("invalid reduction-identifier");
32716 resync_fail:
32717 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32718 /*or_comma=*/false,
32719 /*consume_paren=*/true);
32720 return list;
32724 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32725 goto resync_fail;
32727 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32728 NULL);
32729 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32731 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32732 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32735 return nlist;
32738 /* OpenMP 2.5:
32739 schedule ( schedule-kind )
32740 schedule ( schedule-kind , expression )
32742 schedule-kind:
32743 static | dynamic | guided | runtime | auto
32745 OpenMP 4.5:
32746 schedule ( schedule-modifier : schedule-kind )
32747 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32749 schedule-modifier:
32750 simd
32751 monotonic
32752 nonmonotonic */
32754 static tree
32755 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32757 tree c, t;
32758 int modifiers = 0, nmodifiers = 0;
32760 matching_parens parens;
32761 if (!parens.require_open (parser))
32762 return list;
32764 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32766 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32768 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32769 const char *p = IDENTIFIER_POINTER (id);
32770 if (strcmp ("simd", p) == 0)
32771 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32772 else if (strcmp ("monotonic", p) == 0)
32773 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32774 else if (strcmp ("nonmonotonic", p) == 0)
32775 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32776 else
32777 break;
32778 cp_lexer_consume_token (parser->lexer);
32779 if (nmodifiers++ == 0
32780 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32781 cp_lexer_consume_token (parser->lexer);
32782 else
32784 cp_parser_require (parser, CPP_COLON, RT_COLON);
32785 break;
32789 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32791 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32792 const char *p = IDENTIFIER_POINTER (id);
32794 switch (p[0])
32796 case 'd':
32797 if (strcmp ("dynamic", p) != 0)
32798 goto invalid_kind;
32799 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32800 break;
32802 case 'g':
32803 if (strcmp ("guided", p) != 0)
32804 goto invalid_kind;
32805 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32806 break;
32808 case 'r':
32809 if (strcmp ("runtime", p) != 0)
32810 goto invalid_kind;
32811 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32812 break;
32814 default:
32815 goto invalid_kind;
32818 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32819 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32820 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32821 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32822 else
32823 goto invalid_kind;
32824 cp_lexer_consume_token (parser->lexer);
32826 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32827 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32828 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32829 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32831 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32832 "specified");
32833 modifiers = 0;
32836 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32838 cp_token *token;
32839 cp_lexer_consume_token (parser->lexer);
32841 token = cp_lexer_peek_token (parser->lexer);
32842 t = cp_parser_assignment_expression (parser);
32844 if (t == error_mark_node)
32845 goto resync_fail;
32846 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32847 error_at (token->location, "schedule %<runtime%> does not take "
32848 "a %<chunk_size%> parameter");
32849 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32850 error_at (token->location, "schedule %<auto%> does not take "
32851 "a %<chunk_size%> parameter");
32852 else
32853 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32855 if (!parens.require_close (parser))
32856 goto resync_fail;
32858 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32859 goto resync_fail;
32861 OMP_CLAUSE_SCHEDULE_KIND (c)
32862 = (enum omp_clause_schedule_kind)
32863 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32865 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32866 OMP_CLAUSE_CHAIN (c) = list;
32867 return c;
32869 invalid_kind:
32870 cp_parser_error (parser, "invalid schedule kind");
32871 resync_fail:
32872 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32873 /*or_comma=*/false,
32874 /*consume_paren=*/true);
32875 return list;
32878 /* OpenMP 3.0:
32879 untied */
32881 static tree
32882 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32883 tree list, location_t location)
32885 tree c;
32887 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32889 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32890 OMP_CLAUSE_CHAIN (c) = list;
32891 return c;
32894 /* OpenMP 4.0:
32895 inbranch
32896 notinbranch */
32898 static tree
32899 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32900 tree list, location_t location)
32902 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32903 tree c = build_omp_clause (location, code);
32904 OMP_CLAUSE_CHAIN (c) = list;
32905 return c;
32908 /* OpenMP 4.0:
32909 parallel
32911 sections
32912 taskgroup */
32914 static tree
32915 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32916 enum omp_clause_code code,
32917 tree list, location_t location)
32919 tree c = build_omp_clause (location, code);
32920 OMP_CLAUSE_CHAIN (c) = list;
32921 return c;
32924 /* OpenMP 4.5:
32925 nogroup */
32927 static tree
32928 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32929 tree list, location_t location)
32931 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32932 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32933 OMP_CLAUSE_CHAIN (c) = list;
32934 return c;
32937 /* OpenMP 4.5:
32938 simd
32939 threads */
32941 static tree
32942 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32943 enum omp_clause_code code,
32944 tree list, location_t location)
32946 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32947 tree c = build_omp_clause (location, code);
32948 OMP_CLAUSE_CHAIN (c) = list;
32949 return c;
32952 /* OpenMP 4.0:
32953 num_teams ( expression ) */
32955 static tree
32956 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32957 location_t location)
32959 tree t, c;
32961 matching_parens parens;
32962 if (!parens.require_open (parser))
32963 return list;
32965 t = cp_parser_expression (parser);
32967 if (t == error_mark_node
32968 || !parens.require_close (parser))
32969 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32970 /*or_comma=*/false,
32971 /*consume_paren=*/true);
32973 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32974 "num_teams", location);
32976 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32977 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32978 OMP_CLAUSE_CHAIN (c) = list;
32980 return c;
32983 /* OpenMP 4.0:
32984 thread_limit ( expression ) */
32986 static tree
32987 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32988 location_t location)
32990 tree t, c;
32992 matching_parens parens;
32993 if (!parens.require_open (parser))
32994 return list;
32996 t = cp_parser_expression (parser);
32998 if (t == error_mark_node
32999 || !parens.require_close (parser))
33000 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33001 /*or_comma=*/false,
33002 /*consume_paren=*/true);
33004 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33005 "thread_limit", location);
33007 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33008 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33009 OMP_CLAUSE_CHAIN (c) = list;
33011 return c;
33014 /* OpenMP 4.0:
33015 aligned ( variable-list )
33016 aligned ( variable-list : constant-expression ) */
33018 static tree
33019 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33021 tree nlist, c, alignment = NULL_TREE;
33022 bool colon;
33024 matching_parens parens;
33025 if (!parens.require_open (parser))
33026 return list;
33028 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33029 &colon);
33031 if (colon)
33033 alignment = cp_parser_constant_expression (parser);
33035 if (!parens.require_close (parser))
33036 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33037 /*or_comma=*/false,
33038 /*consume_paren=*/true);
33040 if (alignment == error_mark_node)
33041 alignment = NULL_TREE;
33044 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33045 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33047 return nlist;
33050 /* OpenMP 4.0:
33051 linear ( variable-list )
33052 linear ( variable-list : expression )
33054 OpenMP 4.5:
33055 linear ( modifier ( variable-list ) )
33056 linear ( modifier ( variable-list ) : expression ) */
33058 static tree
33059 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33060 bool declare_simd)
33062 tree nlist, c, step = integer_one_node;
33063 bool colon;
33064 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33066 matching_parens parens;
33067 if (!parens.require_open (parser))
33068 return list;
33070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33072 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33073 const char *p = IDENTIFIER_POINTER (id);
33075 if (strcmp ("ref", p) == 0)
33076 kind = OMP_CLAUSE_LINEAR_REF;
33077 else if (strcmp ("val", p) == 0)
33078 kind = OMP_CLAUSE_LINEAR_VAL;
33079 else if (strcmp ("uval", p) == 0)
33080 kind = OMP_CLAUSE_LINEAR_UVAL;
33081 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33082 cp_lexer_consume_token (parser->lexer);
33083 else
33084 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33087 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33088 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33089 &colon);
33090 else
33092 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33093 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33094 if (colon)
33095 cp_parser_require (parser, CPP_COLON, RT_COLON);
33096 else if (!parens.require_close (parser))
33097 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33098 /*or_comma=*/false,
33099 /*consume_paren=*/true);
33102 if (colon)
33104 step = NULL_TREE;
33105 if (declare_simd
33106 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33107 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33109 cp_token *token = cp_lexer_peek_token (parser->lexer);
33110 cp_parser_parse_tentatively (parser);
33111 step = cp_parser_id_expression (parser, /*template_p=*/false,
33112 /*check_dependency_p=*/true,
33113 /*template_p=*/NULL,
33114 /*declarator_p=*/false,
33115 /*optional_p=*/false);
33116 if (step != error_mark_node)
33117 step = cp_parser_lookup_name_simple (parser, step, token->location);
33118 if (step == error_mark_node)
33120 step = NULL_TREE;
33121 cp_parser_abort_tentative_parse (parser);
33123 else if (!cp_parser_parse_definitely (parser))
33124 step = NULL_TREE;
33126 if (!step)
33127 step = cp_parser_expression (parser);
33129 if (!parens.require_close (parser))
33130 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33131 /*or_comma=*/false,
33132 /*consume_paren=*/true);
33134 if (step == error_mark_node)
33135 return list;
33138 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33140 OMP_CLAUSE_LINEAR_STEP (c) = step;
33141 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33144 return nlist;
33147 /* OpenMP 4.0:
33148 safelen ( constant-expression ) */
33150 static tree
33151 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33152 location_t location)
33154 tree t, c;
33156 matching_parens parens;
33157 if (!parens.require_open (parser))
33158 return list;
33160 t = cp_parser_constant_expression (parser);
33162 if (t == error_mark_node
33163 || !parens.require_close (parser))
33164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33165 /*or_comma=*/false,
33166 /*consume_paren=*/true);
33168 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33170 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33171 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33172 OMP_CLAUSE_CHAIN (c) = list;
33174 return c;
33177 /* OpenMP 4.0:
33178 simdlen ( constant-expression ) */
33180 static tree
33181 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33182 location_t location)
33184 tree t, c;
33186 matching_parens parens;
33187 if (!parens.require_open (parser))
33188 return list;
33190 t = cp_parser_constant_expression (parser);
33192 if (t == error_mark_node
33193 || !parens.require_close (parser))
33194 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33195 /*or_comma=*/false,
33196 /*consume_paren=*/true);
33198 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33200 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33201 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33202 OMP_CLAUSE_CHAIN (c) = list;
33204 return c;
33207 /* OpenMP 4.5:
33208 vec:
33209 identifier [+/- integer]
33210 vec , identifier [+/- integer]
33213 static tree
33214 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33215 tree list)
33217 tree vec = NULL;
33219 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33221 cp_parser_error (parser, "expected identifier");
33222 return list;
33225 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33227 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33228 tree t, identifier = cp_parser_identifier (parser);
33229 tree addend = NULL;
33231 if (identifier == error_mark_node)
33232 t = error_mark_node;
33233 else
33235 t = cp_parser_lookup_name_simple
33236 (parser, identifier,
33237 cp_lexer_peek_token (parser->lexer)->location);
33238 if (t == error_mark_node)
33239 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33240 id_loc);
33243 bool neg = false;
33244 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33245 neg = true;
33246 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33248 addend = integer_zero_node;
33249 goto add_to_vector;
33251 cp_lexer_consume_token (parser->lexer);
33253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33255 cp_parser_error (parser, "expected integer");
33256 return list;
33259 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33260 if (TREE_CODE (addend) != INTEGER_CST)
33262 cp_parser_error (parser, "expected integer");
33263 return list;
33265 cp_lexer_consume_token (parser->lexer);
33267 add_to_vector:
33268 if (t != error_mark_node)
33270 vec = tree_cons (addend, t, vec);
33271 if (neg)
33272 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33276 break;
33278 cp_lexer_consume_token (parser->lexer);
33281 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33283 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33284 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33285 OMP_CLAUSE_DECL (u) = nreverse (vec);
33286 OMP_CLAUSE_CHAIN (u) = list;
33287 return u;
33289 return list;
33292 /* OpenMP 4.0:
33293 depend ( depend-kind : variable-list )
33295 depend-kind:
33296 in | out | inout
33298 OpenMP 4.5:
33299 depend ( source )
33301 depend ( sink : vec ) */
33303 static tree
33304 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33306 tree nlist, c;
33307 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33309 matching_parens parens;
33310 if (!parens.require_open (parser))
33311 return list;
33313 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33315 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33316 const char *p = IDENTIFIER_POINTER (id);
33318 if (strcmp ("in", p) == 0)
33319 kind = OMP_CLAUSE_DEPEND_IN;
33320 else if (strcmp ("inout", p) == 0)
33321 kind = OMP_CLAUSE_DEPEND_INOUT;
33322 else if (strcmp ("out", p) == 0)
33323 kind = OMP_CLAUSE_DEPEND_OUT;
33324 else if (strcmp ("source", p) == 0)
33325 kind = OMP_CLAUSE_DEPEND_SOURCE;
33326 else if (strcmp ("sink", p) == 0)
33327 kind = OMP_CLAUSE_DEPEND_SINK;
33328 else
33329 goto invalid_kind;
33331 else
33332 goto invalid_kind;
33334 cp_lexer_consume_token (parser->lexer);
33336 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33338 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33339 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33340 OMP_CLAUSE_DECL (c) = NULL_TREE;
33341 OMP_CLAUSE_CHAIN (c) = list;
33342 if (!parens.require_close (parser))
33343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33344 /*or_comma=*/false,
33345 /*consume_paren=*/true);
33346 return c;
33349 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33350 goto resync_fail;
33352 if (kind == OMP_CLAUSE_DEPEND_SINK)
33353 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33354 else
33356 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33357 list, NULL);
33359 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33360 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33362 return nlist;
33364 invalid_kind:
33365 cp_parser_error (parser, "invalid depend kind");
33366 resync_fail:
33367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33368 /*or_comma=*/false,
33369 /*consume_paren=*/true);
33370 return list;
33373 /* OpenMP 4.0:
33374 map ( map-kind : variable-list )
33375 map ( variable-list )
33377 map-kind:
33378 alloc | to | from | tofrom
33380 OpenMP 4.5:
33381 map-kind:
33382 alloc | to | from | tofrom | release | delete
33384 map ( always [,] map-kind: variable-list ) */
33386 static tree
33387 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33389 tree nlist, c;
33390 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33391 bool always = false;
33393 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33394 return list;
33396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33398 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33399 const char *p = IDENTIFIER_POINTER (id);
33401 if (strcmp ("always", p) == 0)
33403 int nth = 2;
33404 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33405 nth++;
33406 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33407 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33408 == RID_DELETE))
33409 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33410 == CPP_COLON))
33412 always = true;
33413 cp_lexer_consume_token (parser->lexer);
33414 if (nth == 3)
33415 cp_lexer_consume_token (parser->lexer);
33420 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33421 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33423 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33424 const char *p = IDENTIFIER_POINTER (id);
33426 if (strcmp ("alloc", p) == 0)
33427 kind = GOMP_MAP_ALLOC;
33428 else if (strcmp ("to", p) == 0)
33429 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33430 else if (strcmp ("from", p) == 0)
33431 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33432 else if (strcmp ("tofrom", p) == 0)
33433 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33434 else if (strcmp ("release", p) == 0)
33435 kind = GOMP_MAP_RELEASE;
33436 else
33438 cp_parser_error (parser, "invalid map kind");
33439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33440 /*or_comma=*/false,
33441 /*consume_paren=*/true);
33442 return list;
33444 cp_lexer_consume_token (parser->lexer);
33445 cp_lexer_consume_token (parser->lexer);
33447 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33448 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33450 kind = GOMP_MAP_DELETE;
33451 cp_lexer_consume_token (parser->lexer);
33452 cp_lexer_consume_token (parser->lexer);
33455 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33456 NULL);
33458 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33459 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33461 return nlist;
33464 /* OpenMP 4.0:
33465 device ( expression ) */
33467 static tree
33468 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33469 location_t location)
33471 tree t, c;
33473 matching_parens parens;
33474 if (!parens.require_open (parser))
33475 return list;
33477 t = cp_parser_expression (parser);
33479 if (t == error_mark_node
33480 || !parens.require_close (parser))
33481 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33482 /*or_comma=*/false,
33483 /*consume_paren=*/true);
33485 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33486 "device", location);
33488 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33489 OMP_CLAUSE_DEVICE_ID (c) = t;
33490 OMP_CLAUSE_CHAIN (c) = list;
33492 return c;
33495 /* OpenMP 4.0:
33496 dist_schedule ( static )
33497 dist_schedule ( static , expression ) */
33499 static tree
33500 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33501 location_t location)
33503 tree c, t;
33505 matching_parens parens;
33506 if (!parens.require_open (parser))
33507 return list;
33509 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33511 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33512 goto invalid_kind;
33513 cp_lexer_consume_token (parser->lexer);
33515 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33517 cp_lexer_consume_token (parser->lexer);
33519 t = cp_parser_assignment_expression (parser);
33521 if (t == error_mark_node)
33522 goto resync_fail;
33523 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33525 if (!parens.require_close (parser))
33526 goto resync_fail;
33528 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33529 goto resync_fail;
33531 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33532 location);
33533 OMP_CLAUSE_CHAIN (c) = list;
33534 return c;
33536 invalid_kind:
33537 cp_parser_error (parser, "invalid dist_schedule kind");
33538 resync_fail:
33539 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33540 /*or_comma=*/false,
33541 /*consume_paren=*/true);
33542 return list;
33545 /* OpenMP 4.0:
33546 proc_bind ( proc-bind-kind )
33548 proc-bind-kind:
33549 master | close | spread */
33551 static tree
33552 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33553 location_t location)
33555 tree c;
33556 enum omp_clause_proc_bind_kind kind;
33558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33559 return list;
33561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33563 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33564 const char *p = IDENTIFIER_POINTER (id);
33566 if (strcmp ("master", p) == 0)
33567 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33568 else if (strcmp ("close", p) == 0)
33569 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33570 else if (strcmp ("spread", p) == 0)
33571 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33572 else
33573 goto invalid_kind;
33575 else
33576 goto invalid_kind;
33578 cp_lexer_consume_token (parser->lexer);
33579 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33580 goto resync_fail;
33582 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33583 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33584 location);
33585 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33586 OMP_CLAUSE_CHAIN (c) = list;
33587 return c;
33589 invalid_kind:
33590 cp_parser_error (parser, "invalid depend kind");
33591 resync_fail:
33592 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33593 /*or_comma=*/false,
33594 /*consume_paren=*/true);
33595 return list;
33598 /* OpenACC:
33599 async [( int-expr )] */
33601 static tree
33602 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33604 tree c, t;
33605 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33607 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33609 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33611 matching_parens parens;
33612 parens.consume_open (parser);
33614 t = cp_parser_expression (parser);
33615 if (t == error_mark_node
33616 || !parens.require_close (parser))
33617 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33618 /*or_comma=*/false,
33619 /*consume_paren=*/true);
33622 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33624 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33625 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33626 OMP_CLAUSE_CHAIN (c) = list;
33627 list = c;
33629 return list;
33632 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33633 is a bitmask in MASK. Return the list of clauses found. */
33635 static tree
33636 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33637 const char *where, cp_token *pragma_tok,
33638 bool finish_p = true)
33640 tree clauses = NULL;
33641 bool first = true;
33643 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33645 location_t here;
33646 pragma_omp_clause c_kind;
33647 omp_clause_code code;
33648 const char *c_name;
33649 tree prev = clauses;
33651 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33652 cp_lexer_consume_token (parser->lexer);
33654 here = cp_lexer_peek_token (parser->lexer)->location;
33655 c_kind = cp_parser_omp_clause_name (parser);
33657 switch (c_kind)
33659 case PRAGMA_OACC_CLAUSE_ASYNC:
33660 clauses = cp_parser_oacc_clause_async (parser, clauses);
33661 c_name = "async";
33662 break;
33663 case PRAGMA_OACC_CLAUSE_AUTO:
33664 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33665 clauses, here);
33666 c_name = "auto";
33667 break;
33668 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33669 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33670 c_name = "collapse";
33671 break;
33672 case PRAGMA_OACC_CLAUSE_COPY:
33673 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33674 c_name = "copy";
33675 break;
33676 case PRAGMA_OACC_CLAUSE_COPYIN:
33677 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33678 c_name = "copyin";
33679 break;
33680 case PRAGMA_OACC_CLAUSE_COPYOUT:
33681 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33682 c_name = "copyout";
33683 break;
33684 case PRAGMA_OACC_CLAUSE_CREATE:
33685 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33686 c_name = "create";
33687 break;
33688 case PRAGMA_OACC_CLAUSE_DELETE:
33689 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33690 c_name = "delete";
33691 break;
33692 case PRAGMA_OMP_CLAUSE_DEFAULT:
33693 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33694 c_name = "default";
33695 break;
33696 case PRAGMA_OACC_CLAUSE_DEVICE:
33697 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33698 c_name = "device";
33699 break;
33700 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33701 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33702 c_name = "deviceptr";
33703 break;
33704 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33705 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33706 c_name = "device_resident";
33707 break;
33708 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33709 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33710 clauses);
33711 c_name = "firstprivate";
33712 break;
33713 case PRAGMA_OACC_CLAUSE_GANG:
33714 c_name = "gang";
33715 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33716 c_name, clauses);
33717 break;
33718 case PRAGMA_OACC_CLAUSE_HOST:
33719 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33720 c_name = "host";
33721 break;
33722 case PRAGMA_OACC_CLAUSE_IF:
33723 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33724 c_name = "if";
33725 break;
33726 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33727 clauses = cp_parser_oacc_simple_clause (parser,
33728 OMP_CLAUSE_INDEPENDENT,
33729 clauses, here);
33730 c_name = "independent";
33731 break;
33732 case PRAGMA_OACC_CLAUSE_LINK:
33733 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33734 c_name = "link";
33735 break;
33736 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33737 code = OMP_CLAUSE_NUM_GANGS;
33738 c_name = "num_gangs";
33739 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33740 clauses);
33741 break;
33742 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33743 c_name = "num_workers";
33744 code = OMP_CLAUSE_NUM_WORKERS;
33745 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33746 clauses);
33747 break;
33748 case PRAGMA_OACC_CLAUSE_PRESENT:
33749 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33750 c_name = "present";
33751 break;
33752 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33753 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33754 c_name = "present_or_copy";
33755 break;
33756 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33757 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33758 c_name = "present_or_copyin";
33759 break;
33760 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33761 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33762 c_name = "present_or_copyout";
33763 break;
33764 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33765 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33766 c_name = "present_or_create";
33767 break;
33768 case PRAGMA_OACC_CLAUSE_PRIVATE:
33769 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33770 clauses);
33771 c_name = "private";
33772 break;
33773 case PRAGMA_OACC_CLAUSE_REDUCTION:
33774 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33775 c_name = "reduction";
33776 break;
33777 case PRAGMA_OACC_CLAUSE_SELF:
33778 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33779 c_name = "self";
33780 break;
33781 case PRAGMA_OACC_CLAUSE_SEQ:
33782 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33783 clauses, here);
33784 c_name = "seq";
33785 break;
33786 case PRAGMA_OACC_CLAUSE_TILE:
33787 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33788 c_name = "tile";
33789 break;
33790 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33791 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33792 clauses);
33793 c_name = "use_device";
33794 break;
33795 case PRAGMA_OACC_CLAUSE_VECTOR:
33796 c_name = "vector";
33797 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33798 c_name, clauses);
33799 break;
33800 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33801 c_name = "vector_length";
33802 code = OMP_CLAUSE_VECTOR_LENGTH;
33803 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33804 clauses);
33805 break;
33806 case PRAGMA_OACC_CLAUSE_WAIT:
33807 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33808 c_name = "wait";
33809 break;
33810 case PRAGMA_OACC_CLAUSE_WORKER:
33811 c_name = "worker";
33812 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33813 c_name, clauses);
33814 break;
33815 default:
33816 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33817 goto saw_error;
33820 first = false;
33822 if (((mask >> c_kind) & 1) == 0)
33824 /* Remove the invalid clause(s) from the list to avoid
33825 confusing the rest of the compiler. */
33826 clauses = prev;
33827 error_at (here, "%qs is not valid for %qs", c_name, where);
33831 saw_error:
33832 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33834 if (finish_p)
33835 return finish_omp_clauses (clauses, C_ORT_ACC);
33837 return clauses;
33840 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33841 is a bitmask in MASK. Return the list of clauses found; the result
33842 of clause default goes in *pdefault. */
33844 static tree
33845 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33846 const char *where, cp_token *pragma_tok,
33847 bool finish_p = true)
33849 tree clauses = NULL;
33850 bool first = true;
33851 cp_token *token = NULL;
33853 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33855 pragma_omp_clause c_kind;
33856 const char *c_name;
33857 tree prev = clauses;
33859 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33860 cp_lexer_consume_token (parser->lexer);
33862 token = cp_lexer_peek_token (parser->lexer);
33863 c_kind = cp_parser_omp_clause_name (parser);
33865 switch (c_kind)
33867 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33868 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33869 token->location);
33870 c_name = "collapse";
33871 break;
33872 case PRAGMA_OMP_CLAUSE_COPYIN:
33873 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33874 c_name = "copyin";
33875 break;
33876 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33877 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33878 clauses);
33879 c_name = "copyprivate";
33880 break;
33881 case PRAGMA_OMP_CLAUSE_DEFAULT:
33882 clauses = cp_parser_omp_clause_default (parser, clauses,
33883 token->location, false);
33884 c_name = "default";
33885 break;
33886 case PRAGMA_OMP_CLAUSE_FINAL:
33887 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33888 c_name = "final";
33889 break;
33890 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33891 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33892 clauses);
33893 c_name = "firstprivate";
33894 break;
33895 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33896 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33897 token->location);
33898 c_name = "grainsize";
33899 break;
33900 case PRAGMA_OMP_CLAUSE_HINT:
33901 clauses = cp_parser_omp_clause_hint (parser, clauses,
33902 token->location);
33903 c_name = "hint";
33904 break;
33905 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33906 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33907 token->location);
33908 c_name = "defaultmap";
33909 break;
33910 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33911 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33912 clauses);
33913 c_name = "use_device_ptr";
33914 break;
33915 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33916 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33917 clauses);
33918 c_name = "is_device_ptr";
33919 break;
33920 case PRAGMA_OMP_CLAUSE_IF:
33921 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33922 true);
33923 c_name = "if";
33924 break;
33925 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33926 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33927 clauses);
33928 c_name = "lastprivate";
33929 break;
33930 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33931 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33932 token->location);
33933 c_name = "mergeable";
33934 break;
33935 case PRAGMA_OMP_CLAUSE_NOWAIT:
33936 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33937 c_name = "nowait";
33938 break;
33939 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33940 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33941 token->location);
33942 c_name = "num_tasks";
33943 break;
33944 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33945 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33946 token->location);
33947 c_name = "num_threads";
33948 break;
33949 case PRAGMA_OMP_CLAUSE_ORDERED:
33950 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33951 token->location);
33952 c_name = "ordered";
33953 break;
33954 case PRAGMA_OMP_CLAUSE_PRIORITY:
33955 clauses = cp_parser_omp_clause_priority (parser, clauses,
33956 token->location);
33957 c_name = "priority";
33958 break;
33959 case PRAGMA_OMP_CLAUSE_PRIVATE:
33960 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33961 clauses);
33962 c_name = "private";
33963 break;
33964 case PRAGMA_OMP_CLAUSE_REDUCTION:
33965 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33966 c_name = "reduction";
33967 break;
33968 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33969 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33970 token->location);
33971 c_name = "schedule";
33972 break;
33973 case PRAGMA_OMP_CLAUSE_SHARED:
33974 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33975 clauses);
33976 c_name = "shared";
33977 break;
33978 case PRAGMA_OMP_CLAUSE_UNTIED:
33979 clauses = cp_parser_omp_clause_untied (parser, clauses,
33980 token->location);
33981 c_name = "untied";
33982 break;
33983 case PRAGMA_OMP_CLAUSE_INBRANCH:
33984 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33985 clauses, token->location);
33986 c_name = "inbranch";
33987 break;
33988 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33989 clauses = cp_parser_omp_clause_branch (parser,
33990 OMP_CLAUSE_NOTINBRANCH,
33991 clauses, token->location);
33992 c_name = "notinbranch";
33993 break;
33994 case PRAGMA_OMP_CLAUSE_PARALLEL:
33995 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33996 clauses, token->location);
33997 c_name = "parallel";
33998 if (!first)
34000 clause_not_first:
34001 error_at (token->location, "%qs must be the first clause of %qs",
34002 c_name, where);
34003 clauses = prev;
34005 break;
34006 case PRAGMA_OMP_CLAUSE_FOR:
34007 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34008 clauses, token->location);
34009 c_name = "for";
34010 if (!first)
34011 goto clause_not_first;
34012 break;
34013 case PRAGMA_OMP_CLAUSE_SECTIONS:
34014 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34015 clauses, token->location);
34016 c_name = "sections";
34017 if (!first)
34018 goto clause_not_first;
34019 break;
34020 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34021 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34022 clauses, token->location);
34023 c_name = "taskgroup";
34024 if (!first)
34025 goto clause_not_first;
34026 break;
34027 case PRAGMA_OMP_CLAUSE_LINK:
34028 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34029 c_name = "to";
34030 break;
34031 case PRAGMA_OMP_CLAUSE_TO:
34032 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34033 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34034 clauses);
34035 else
34036 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34037 c_name = "to";
34038 break;
34039 case PRAGMA_OMP_CLAUSE_FROM:
34040 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34041 c_name = "from";
34042 break;
34043 case PRAGMA_OMP_CLAUSE_UNIFORM:
34044 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34045 clauses);
34046 c_name = "uniform";
34047 break;
34048 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34049 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34050 token->location);
34051 c_name = "num_teams";
34052 break;
34053 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34054 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34055 token->location);
34056 c_name = "thread_limit";
34057 break;
34058 case PRAGMA_OMP_CLAUSE_ALIGNED:
34059 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34060 c_name = "aligned";
34061 break;
34062 case PRAGMA_OMP_CLAUSE_LINEAR:
34064 bool declare_simd = false;
34065 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34066 declare_simd = true;
34067 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34069 c_name = "linear";
34070 break;
34071 case PRAGMA_OMP_CLAUSE_DEPEND:
34072 clauses = cp_parser_omp_clause_depend (parser, clauses,
34073 token->location);
34074 c_name = "depend";
34075 break;
34076 case PRAGMA_OMP_CLAUSE_MAP:
34077 clauses = cp_parser_omp_clause_map (parser, clauses);
34078 c_name = "map";
34079 break;
34080 case PRAGMA_OMP_CLAUSE_DEVICE:
34081 clauses = cp_parser_omp_clause_device (parser, clauses,
34082 token->location);
34083 c_name = "device";
34084 break;
34085 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34086 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34087 token->location);
34088 c_name = "dist_schedule";
34089 break;
34090 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34091 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34092 token->location);
34093 c_name = "proc_bind";
34094 break;
34095 case PRAGMA_OMP_CLAUSE_SAFELEN:
34096 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34097 token->location);
34098 c_name = "safelen";
34099 break;
34100 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34101 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34102 token->location);
34103 c_name = "simdlen";
34104 break;
34105 case PRAGMA_OMP_CLAUSE_NOGROUP:
34106 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34107 token->location);
34108 c_name = "nogroup";
34109 break;
34110 case PRAGMA_OMP_CLAUSE_THREADS:
34111 clauses
34112 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34113 clauses, token->location);
34114 c_name = "threads";
34115 break;
34116 case PRAGMA_OMP_CLAUSE_SIMD:
34117 clauses
34118 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34119 clauses, token->location);
34120 c_name = "simd";
34121 break;
34122 default:
34123 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34124 goto saw_error;
34127 first = false;
34129 if (((mask >> c_kind) & 1) == 0)
34131 /* Remove the invalid clause(s) from the list to avoid
34132 confusing the rest of the compiler. */
34133 clauses = prev;
34134 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34137 saw_error:
34138 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34139 if (finish_p)
34141 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34142 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34143 else
34144 return finish_omp_clauses (clauses, C_ORT_OMP);
34146 return clauses;
34149 /* OpenMP 2.5:
34150 structured-block:
34151 statement
34153 In practice, we're also interested in adding the statement to an
34154 outer node. So it is convenient if we work around the fact that
34155 cp_parser_statement calls add_stmt. */
34157 static unsigned
34158 cp_parser_begin_omp_structured_block (cp_parser *parser)
34160 unsigned save = parser->in_statement;
34162 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34163 This preserves the "not within loop or switch" style error messages
34164 for nonsense cases like
34165 void foo() {
34166 #pragma omp single
34167 break;
34170 if (parser->in_statement)
34171 parser->in_statement = IN_OMP_BLOCK;
34173 return save;
34176 static void
34177 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34179 parser->in_statement = save;
34182 static tree
34183 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34185 tree stmt = begin_omp_structured_block ();
34186 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34188 cp_parser_statement (parser, NULL_TREE, false, if_p);
34190 cp_parser_end_omp_structured_block (parser, save);
34191 return finish_omp_structured_block (stmt);
34194 /* OpenMP 2.5:
34195 # pragma omp atomic new-line
34196 expression-stmt
34198 expression-stmt:
34199 x binop= expr | x++ | ++x | x-- | --x
34200 binop:
34201 +, *, -, /, &, ^, |, <<, >>
34203 where x is an lvalue expression with scalar type.
34205 OpenMP 3.1:
34206 # pragma omp atomic new-line
34207 update-stmt
34209 # pragma omp atomic read new-line
34210 read-stmt
34212 # pragma omp atomic write new-line
34213 write-stmt
34215 # pragma omp atomic update new-line
34216 update-stmt
34218 # pragma omp atomic capture new-line
34219 capture-stmt
34221 # pragma omp atomic capture new-line
34222 capture-block
34224 read-stmt:
34225 v = x
34226 write-stmt:
34227 x = expr
34228 update-stmt:
34229 expression-stmt | x = x binop expr
34230 capture-stmt:
34231 v = expression-stmt
34232 capture-block:
34233 { v = x; update-stmt; } | { update-stmt; v = x; }
34235 OpenMP 4.0:
34236 update-stmt:
34237 expression-stmt | x = x binop expr | x = expr binop x
34238 capture-stmt:
34239 v = update-stmt
34240 capture-block:
34241 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34243 where x and v are lvalue expressions with scalar type. */
34245 static void
34246 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34248 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34249 tree rhs1 = NULL_TREE, orig_lhs;
34250 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34251 bool structured_block = false;
34252 bool seq_cst = false;
34254 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34256 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34257 const char *p = IDENTIFIER_POINTER (id);
34259 if (!strcmp (p, "seq_cst"))
34261 seq_cst = true;
34262 cp_lexer_consume_token (parser->lexer);
34263 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34264 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34265 cp_lexer_consume_token (parser->lexer);
34268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34270 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34271 const char *p = IDENTIFIER_POINTER (id);
34273 if (!strcmp (p, "read"))
34274 code = OMP_ATOMIC_READ;
34275 else if (!strcmp (p, "write"))
34276 code = NOP_EXPR;
34277 else if (!strcmp (p, "update"))
34278 code = OMP_ATOMIC;
34279 else if (!strcmp (p, "capture"))
34280 code = OMP_ATOMIC_CAPTURE_NEW;
34281 else
34282 p = NULL;
34283 if (p)
34284 cp_lexer_consume_token (parser->lexer);
34286 if (!seq_cst)
34288 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34289 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34290 cp_lexer_consume_token (parser->lexer);
34292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34294 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34295 const char *p = IDENTIFIER_POINTER (id);
34297 if (!strcmp (p, "seq_cst"))
34299 seq_cst = true;
34300 cp_lexer_consume_token (parser->lexer);
34304 cp_parser_require_pragma_eol (parser, pragma_tok);
34306 switch (code)
34308 case OMP_ATOMIC_READ:
34309 case NOP_EXPR: /* atomic write */
34310 v = cp_parser_unary_expression (parser);
34311 if (v == error_mark_node)
34312 goto saw_error;
34313 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34314 goto saw_error;
34315 if (code == NOP_EXPR)
34316 lhs = cp_parser_expression (parser);
34317 else
34318 lhs = cp_parser_unary_expression (parser);
34319 if (lhs == error_mark_node)
34320 goto saw_error;
34321 if (code == NOP_EXPR)
34323 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34324 opcode. */
34325 code = OMP_ATOMIC;
34326 rhs = lhs;
34327 lhs = v;
34328 v = NULL_TREE;
34330 goto done;
34331 case OMP_ATOMIC_CAPTURE_NEW:
34332 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34334 cp_lexer_consume_token (parser->lexer);
34335 structured_block = true;
34337 else
34339 v = cp_parser_unary_expression (parser);
34340 if (v == error_mark_node)
34341 goto saw_error;
34342 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34343 goto saw_error;
34345 default:
34346 break;
34349 restart:
34350 lhs = cp_parser_unary_expression (parser);
34351 orig_lhs = lhs;
34352 switch (TREE_CODE (lhs))
34354 case ERROR_MARK:
34355 goto saw_error;
34357 case POSTINCREMENT_EXPR:
34358 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34359 code = OMP_ATOMIC_CAPTURE_OLD;
34360 /* FALLTHROUGH */
34361 case PREINCREMENT_EXPR:
34362 lhs = TREE_OPERAND (lhs, 0);
34363 opcode = PLUS_EXPR;
34364 rhs = integer_one_node;
34365 break;
34367 case POSTDECREMENT_EXPR:
34368 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34369 code = OMP_ATOMIC_CAPTURE_OLD;
34370 /* FALLTHROUGH */
34371 case PREDECREMENT_EXPR:
34372 lhs = TREE_OPERAND (lhs, 0);
34373 opcode = MINUS_EXPR;
34374 rhs = integer_one_node;
34375 break;
34377 case COMPOUND_EXPR:
34378 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34379 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34380 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34381 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34382 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34383 (TREE_OPERAND (lhs, 1), 0), 0)))
34384 == BOOLEAN_TYPE)
34385 /* Undo effects of boolean_increment for post {in,de}crement. */
34386 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34387 /* FALLTHRU */
34388 case MODIFY_EXPR:
34389 if (TREE_CODE (lhs) == MODIFY_EXPR
34390 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34392 /* Undo effects of boolean_increment. */
34393 if (integer_onep (TREE_OPERAND (lhs, 1)))
34395 /* This is pre or post increment. */
34396 rhs = TREE_OPERAND (lhs, 1);
34397 lhs = TREE_OPERAND (lhs, 0);
34398 opcode = NOP_EXPR;
34399 if (code == OMP_ATOMIC_CAPTURE_NEW
34400 && !structured_block
34401 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34402 code = OMP_ATOMIC_CAPTURE_OLD;
34403 break;
34406 /* FALLTHRU */
34407 default:
34408 switch (cp_lexer_peek_token (parser->lexer)->type)
34410 case CPP_MULT_EQ:
34411 opcode = MULT_EXPR;
34412 break;
34413 case CPP_DIV_EQ:
34414 opcode = TRUNC_DIV_EXPR;
34415 break;
34416 case CPP_PLUS_EQ:
34417 opcode = PLUS_EXPR;
34418 break;
34419 case CPP_MINUS_EQ:
34420 opcode = MINUS_EXPR;
34421 break;
34422 case CPP_LSHIFT_EQ:
34423 opcode = LSHIFT_EXPR;
34424 break;
34425 case CPP_RSHIFT_EQ:
34426 opcode = RSHIFT_EXPR;
34427 break;
34428 case CPP_AND_EQ:
34429 opcode = BIT_AND_EXPR;
34430 break;
34431 case CPP_OR_EQ:
34432 opcode = BIT_IOR_EXPR;
34433 break;
34434 case CPP_XOR_EQ:
34435 opcode = BIT_XOR_EXPR;
34436 break;
34437 case CPP_EQ:
34438 enum cp_parser_prec oprec;
34439 cp_token *token;
34440 cp_lexer_consume_token (parser->lexer);
34441 cp_parser_parse_tentatively (parser);
34442 rhs1 = cp_parser_simple_cast_expression (parser);
34443 if (rhs1 == error_mark_node)
34445 cp_parser_abort_tentative_parse (parser);
34446 cp_parser_simple_cast_expression (parser);
34447 goto saw_error;
34449 token = cp_lexer_peek_token (parser->lexer);
34450 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34452 cp_parser_abort_tentative_parse (parser);
34453 cp_parser_parse_tentatively (parser);
34454 rhs = cp_parser_binary_expression (parser, false, true,
34455 PREC_NOT_OPERATOR, NULL);
34456 if (rhs == error_mark_node)
34458 cp_parser_abort_tentative_parse (parser);
34459 cp_parser_binary_expression (parser, false, true,
34460 PREC_NOT_OPERATOR, NULL);
34461 goto saw_error;
34463 switch (TREE_CODE (rhs))
34465 case MULT_EXPR:
34466 case TRUNC_DIV_EXPR:
34467 case RDIV_EXPR:
34468 case PLUS_EXPR:
34469 case MINUS_EXPR:
34470 case LSHIFT_EXPR:
34471 case RSHIFT_EXPR:
34472 case BIT_AND_EXPR:
34473 case BIT_IOR_EXPR:
34474 case BIT_XOR_EXPR:
34475 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34477 if (cp_parser_parse_definitely (parser))
34479 opcode = TREE_CODE (rhs);
34480 rhs1 = TREE_OPERAND (rhs, 0);
34481 rhs = TREE_OPERAND (rhs, 1);
34482 goto stmt_done;
34484 else
34485 goto saw_error;
34487 break;
34488 default:
34489 break;
34491 cp_parser_abort_tentative_parse (parser);
34492 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34494 rhs = cp_parser_expression (parser);
34495 if (rhs == error_mark_node)
34496 goto saw_error;
34497 opcode = NOP_EXPR;
34498 rhs1 = NULL_TREE;
34499 goto stmt_done;
34501 cp_parser_error (parser,
34502 "invalid form of %<#pragma omp atomic%>");
34503 goto saw_error;
34505 if (!cp_parser_parse_definitely (parser))
34506 goto saw_error;
34507 switch (token->type)
34509 case CPP_SEMICOLON:
34510 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34512 code = OMP_ATOMIC_CAPTURE_OLD;
34513 v = lhs;
34514 lhs = NULL_TREE;
34515 lhs1 = rhs1;
34516 rhs1 = NULL_TREE;
34517 cp_lexer_consume_token (parser->lexer);
34518 goto restart;
34520 else if (structured_block)
34522 opcode = NOP_EXPR;
34523 rhs = rhs1;
34524 rhs1 = NULL_TREE;
34525 goto stmt_done;
34527 cp_parser_error (parser,
34528 "invalid form of %<#pragma omp atomic%>");
34529 goto saw_error;
34530 case CPP_MULT:
34531 opcode = MULT_EXPR;
34532 break;
34533 case CPP_DIV:
34534 opcode = TRUNC_DIV_EXPR;
34535 break;
34536 case CPP_PLUS:
34537 opcode = PLUS_EXPR;
34538 break;
34539 case CPP_MINUS:
34540 opcode = MINUS_EXPR;
34541 break;
34542 case CPP_LSHIFT:
34543 opcode = LSHIFT_EXPR;
34544 break;
34545 case CPP_RSHIFT:
34546 opcode = RSHIFT_EXPR;
34547 break;
34548 case CPP_AND:
34549 opcode = BIT_AND_EXPR;
34550 break;
34551 case CPP_OR:
34552 opcode = BIT_IOR_EXPR;
34553 break;
34554 case CPP_XOR:
34555 opcode = BIT_XOR_EXPR;
34556 break;
34557 default:
34558 cp_parser_error (parser,
34559 "invalid operator for %<#pragma omp atomic%>");
34560 goto saw_error;
34562 oprec = TOKEN_PRECEDENCE (token);
34563 gcc_assert (oprec != PREC_NOT_OPERATOR);
34564 if (commutative_tree_code (opcode))
34565 oprec = (enum cp_parser_prec) (oprec - 1);
34566 cp_lexer_consume_token (parser->lexer);
34567 rhs = cp_parser_binary_expression (parser, false, false,
34568 oprec, NULL);
34569 if (rhs == error_mark_node)
34570 goto saw_error;
34571 goto stmt_done;
34572 /* FALLTHROUGH */
34573 default:
34574 cp_parser_error (parser,
34575 "invalid operator for %<#pragma omp atomic%>");
34576 goto saw_error;
34578 cp_lexer_consume_token (parser->lexer);
34580 rhs = cp_parser_expression (parser);
34581 if (rhs == error_mark_node)
34582 goto saw_error;
34583 break;
34585 stmt_done:
34586 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34588 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34589 goto saw_error;
34590 v = cp_parser_unary_expression (parser);
34591 if (v == error_mark_node)
34592 goto saw_error;
34593 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34594 goto saw_error;
34595 lhs1 = cp_parser_unary_expression (parser);
34596 if (lhs1 == error_mark_node)
34597 goto saw_error;
34599 if (structured_block)
34601 cp_parser_consume_semicolon_at_end_of_statement (parser);
34602 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34604 done:
34605 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34606 if (!structured_block)
34607 cp_parser_consume_semicolon_at_end_of_statement (parser);
34608 return;
34610 saw_error:
34611 cp_parser_skip_to_end_of_block_or_statement (parser);
34612 if (structured_block)
34614 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34615 cp_lexer_consume_token (parser->lexer);
34616 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34618 cp_parser_skip_to_end_of_block_or_statement (parser);
34619 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34620 cp_lexer_consume_token (parser->lexer);
34626 /* OpenMP 2.5:
34627 # pragma omp barrier new-line */
34629 static void
34630 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34632 cp_parser_require_pragma_eol (parser, pragma_tok);
34633 finish_omp_barrier ();
34636 /* OpenMP 2.5:
34637 # pragma omp critical [(name)] new-line
34638 structured-block
34640 OpenMP 4.5:
34641 # pragma omp critical [(name) [hint(expression)]] new-line
34642 structured-block */
34644 #define OMP_CRITICAL_CLAUSE_MASK \
34645 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34647 static tree
34648 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34650 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34654 matching_parens parens;
34655 parens.consume_open (parser);
34657 name = cp_parser_identifier (parser);
34659 if (name == error_mark_node
34660 || !parens.require_close (parser))
34661 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34662 /*or_comma=*/false,
34663 /*consume_paren=*/true);
34664 if (name == error_mark_node)
34665 name = NULL;
34667 clauses = cp_parser_omp_all_clauses (parser,
34668 OMP_CRITICAL_CLAUSE_MASK,
34669 "#pragma omp critical", pragma_tok);
34671 else
34672 cp_parser_require_pragma_eol (parser, pragma_tok);
34674 stmt = cp_parser_omp_structured_block (parser, if_p);
34675 return c_finish_omp_critical (input_location, stmt, name, clauses);
34678 /* OpenMP 2.5:
34679 # pragma omp flush flush-vars[opt] new-line
34681 flush-vars:
34682 ( variable-list ) */
34684 static void
34685 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34688 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34689 cp_parser_require_pragma_eol (parser, pragma_tok);
34691 finish_omp_flush ();
34694 /* Helper function, to parse omp for increment expression. */
34696 static tree
34697 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34699 tree cond = cp_parser_binary_expression (parser, false, true,
34700 PREC_NOT_OPERATOR, NULL);
34701 if (cond == error_mark_node
34702 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34704 cp_parser_skip_to_end_of_statement (parser);
34705 return error_mark_node;
34708 switch (TREE_CODE (cond))
34710 case GT_EXPR:
34711 case GE_EXPR:
34712 case LT_EXPR:
34713 case LE_EXPR:
34714 break;
34715 case NE_EXPR:
34716 /* Fall through: OpenMP disallows NE_EXPR. */
34717 gcc_fallthrough ();
34718 default:
34719 return error_mark_node;
34722 /* If decl is an iterator, preserve LHS and RHS of the relational
34723 expr until finish_omp_for. */
34724 if (decl
34725 && (type_dependent_expression_p (decl)
34726 || CLASS_TYPE_P (TREE_TYPE (decl))))
34727 return cond;
34729 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34730 TREE_CODE (cond),
34731 TREE_OPERAND (cond, 0), ERROR_MARK,
34732 TREE_OPERAND (cond, 1), ERROR_MARK,
34733 /*overload=*/NULL, tf_warning_or_error);
34736 /* Helper function, to parse omp for increment expression. */
34738 static tree
34739 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34741 cp_token *token = cp_lexer_peek_token (parser->lexer);
34742 enum tree_code op;
34743 tree lhs, rhs;
34744 cp_id_kind idk;
34745 bool decl_first;
34747 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34749 op = (token->type == CPP_PLUS_PLUS
34750 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34751 cp_lexer_consume_token (parser->lexer);
34752 lhs = cp_parser_simple_cast_expression (parser);
34753 if (lhs != decl
34754 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34755 return error_mark_node;
34756 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34759 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34760 if (lhs != decl
34761 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34762 return error_mark_node;
34764 token = cp_lexer_peek_token (parser->lexer);
34765 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34767 op = (token->type == CPP_PLUS_PLUS
34768 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34769 cp_lexer_consume_token (parser->lexer);
34770 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34773 op = cp_parser_assignment_operator_opt (parser);
34774 if (op == ERROR_MARK)
34775 return error_mark_node;
34777 if (op != NOP_EXPR)
34779 rhs = cp_parser_assignment_expression (parser);
34780 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34781 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34784 lhs = cp_parser_binary_expression (parser, false, false,
34785 PREC_ADDITIVE_EXPRESSION, NULL);
34786 token = cp_lexer_peek_token (parser->lexer);
34787 decl_first = (lhs == decl
34788 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34789 if (decl_first)
34790 lhs = NULL_TREE;
34791 if (token->type != CPP_PLUS
34792 && token->type != CPP_MINUS)
34793 return error_mark_node;
34797 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34798 cp_lexer_consume_token (parser->lexer);
34799 rhs = cp_parser_binary_expression (parser, false, false,
34800 PREC_ADDITIVE_EXPRESSION, NULL);
34801 token = cp_lexer_peek_token (parser->lexer);
34802 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34804 if (lhs == NULL_TREE)
34806 if (op == PLUS_EXPR)
34807 lhs = rhs;
34808 else
34809 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34810 tf_warning_or_error);
34812 else
34813 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34814 ERROR_MARK, NULL, tf_warning_or_error);
34817 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34819 if (!decl_first)
34821 if ((rhs != decl
34822 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34823 || op == MINUS_EXPR)
34824 return error_mark_node;
34825 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34827 else
34828 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34830 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34833 /* Parse the initialization statement of an OpenMP for loop.
34835 Return true if the resulting construct should have an
34836 OMP_CLAUSE_PRIVATE added to it. */
34838 static tree
34839 cp_parser_omp_for_loop_init (cp_parser *parser,
34840 tree &this_pre_body,
34841 vec<tree, va_gc> *for_block,
34842 tree &init,
34843 tree &orig_init,
34844 tree &decl,
34845 tree &real_decl)
34847 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34848 return NULL_TREE;
34850 tree add_private_clause = NULL_TREE;
34852 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34854 init-expr:
34855 var = lb
34856 integer-type var = lb
34857 random-access-iterator-type var = lb
34858 pointer-type var = lb
34860 cp_decl_specifier_seq type_specifiers;
34862 /* First, try to parse as an initialized declaration. See
34863 cp_parser_condition, from whence the bulk of this is copied. */
34865 cp_parser_parse_tentatively (parser);
34866 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34867 /*is_trailing_return=*/false,
34868 &type_specifiers);
34869 if (cp_parser_parse_definitely (parser))
34871 /* If parsing a type specifier seq succeeded, then this
34872 MUST be a initialized declaration. */
34873 tree asm_specification, attributes;
34874 cp_declarator *declarator;
34876 declarator = cp_parser_declarator (parser,
34877 CP_PARSER_DECLARATOR_NAMED,
34878 /*ctor_dtor_or_conv_p=*/NULL,
34879 /*parenthesized_p=*/NULL,
34880 /*member_p=*/false,
34881 /*friend_p=*/false);
34882 attributes = cp_parser_attributes_opt (parser);
34883 asm_specification = cp_parser_asm_specification_opt (parser);
34885 if (declarator == cp_error_declarator)
34886 cp_parser_skip_to_end_of_statement (parser);
34888 else
34890 tree pushed_scope, auto_node;
34892 decl = start_decl (declarator, &type_specifiers,
34893 SD_INITIALIZED, attributes,
34894 /*prefix_attributes=*/NULL_TREE,
34895 &pushed_scope);
34897 auto_node = type_uses_auto (TREE_TYPE (decl));
34898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34900 if (cp_lexer_next_token_is (parser->lexer,
34901 CPP_OPEN_PAREN))
34902 error ("parenthesized initialization is not allowed in "
34903 "OpenMP %<for%> loop");
34904 else
34905 /* Trigger an error. */
34906 cp_parser_require (parser, CPP_EQ, RT_EQ);
34908 init = error_mark_node;
34909 cp_parser_skip_to_end_of_statement (parser);
34911 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34912 || type_dependent_expression_p (decl)
34913 || auto_node)
34915 bool is_direct_init, is_non_constant_init;
34917 init = cp_parser_initializer (parser,
34918 &is_direct_init,
34919 &is_non_constant_init);
34921 if (auto_node)
34923 TREE_TYPE (decl)
34924 = do_auto_deduction (TREE_TYPE (decl), init,
34925 auto_node);
34927 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34928 && !type_dependent_expression_p (decl))
34929 goto non_class;
34932 cp_finish_decl (decl, init, !is_non_constant_init,
34933 asm_specification,
34934 LOOKUP_ONLYCONVERTING);
34935 orig_init = init;
34936 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34938 vec_safe_push (for_block, this_pre_body);
34939 init = NULL_TREE;
34941 else
34943 init = pop_stmt_list (this_pre_body);
34944 if (init && TREE_CODE (init) == STATEMENT_LIST)
34946 tree_stmt_iterator i = tsi_start (init);
34947 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34948 while (!tsi_end_p (i))
34950 tree t = tsi_stmt (i);
34951 if (TREE_CODE (t) == DECL_EXPR
34952 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34954 tsi_delink (&i);
34955 vec_safe_push (for_block, t);
34956 continue;
34958 break;
34960 if (tsi_one_before_end_p (i))
34962 tree t = tsi_stmt (i);
34963 tsi_delink (&i);
34964 free_stmt_list (init);
34965 init = t;
34969 this_pre_body = NULL_TREE;
34971 else
34973 /* Consume '='. */
34974 cp_lexer_consume_token (parser->lexer);
34975 init = cp_parser_assignment_expression (parser);
34977 non_class:
34978 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34979 init = error_mark_node;
34980 else
34981 cp_finish_decl (decl, NULL_TREE,
34982 /*init_const_expr_p=*/false,
34983 asm_specification,
34984 LOOKUP_ONLYCONVERTING);
34987 if (pushed_scope)
34988 pop_scope (pushed_scope);
34991 else
34993 cp_id_kind idk;
34994 /* If parsing a type specifier sequence failed, then
34995 this MUST be a simple expression. */
34996 cp_parser_parse_tentatively (parser);
34997 decl = cp_parser_primary_expression (parser, false, false,
34998 false, &idk);
34999 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35000 if (!cp_parser_error_occurred (parser)
35001 && decl
35002 && (TREE_CODE (decl) == COMPONENT_REF
35003 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35005 cp_parser_abort_tentative_parse (parser);
35006 cp_parser_parse_tentatively (parser);
35007 cp_token *token = cp_lexer_peek_token (parser->lexer);
35008 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35009 /*check_dependency_p=*/true,
35010 /*template_p=*/NULL,
35011 /*declarator_p=*/false,
35012 /*optional_p=*/false);
35013 if (name != error_mark_node
35014 && last_tok == cp_lexer_peek_token (parser->lexer))
35016 decl = cp_parser_lookup_name_simple (parser, name,
35017 token->location);
35018 if (TREE_CODE (decl) == FIELD_DECL)
35019 add_private_clause = omp_privatize_field (decl, false);
35021 cp_parser_abort_tentative_parse (parser);
35022 cp_parser_parse_tentatively (parser);
35023 decl = cp_parser_primary_expression (parser, false, false,
35024 false, &idk);
35026 if (!cp_parser_error_occurred (parser)
35027 && decl
35028 && DECL_P (decl)
35029 && CLASS_TYPE_P (TREE_TYPE (decl)))
35031 tree rhs;
35033 cp_parser_parse_definitely (parser);
35034 cp_parser_require (parser, CPP_EQ, RT_EQ);
35035 rhs = cp_parser_assignment_expression (parser);
35036 orig_init = rhs;
35037 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35038 decl, NOP_EXPR,
35039 rhs,
35040 tf_warning_or_error));
35041 if (!add_private_clause)
35042 add_private_clause = decl;
35044 else
35046 decl = NULL;
35047 cp_parser_abort_tentative_parse (parser);
35048 init = cp_parser_expression (parser);
35049 if (init)
35051 if (TREE_CODE (init) == MODIFY_EXPR
35052 || TREE_CODE (init) == MODOP_EXPR)
35053 real_decl = TREE_OPERAND (init, 0);
35057 return add_private_clause;
35060 /* Parse the restricted form of the for statement allowed by OpenMP. */
35062 static tree
35063 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35064 tree *cclauses, bool *if_p)
35066 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35067 tree real_decl, initv, condv, incrv, declv;
35068 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35069 location_t loc_first;
35070 bool collapse_err = false;
35071 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35072 vec<tree, va_gc> *for_block = make_tree_vector ();
35073 auto_vec<tree, 4> orig_inits;
35074 bool tiling = false;
35076 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35077 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35078 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35079 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35081 tiling = true;
35082 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35084 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35085 && OMP_CLAUSE_ORDERED_EXPR (cl))
35087 ordered_cl = cl;
35088 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35091 if (ordered && ordered < collapse)
35093 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35094 "%<ordered%> clause parameter is less than %<collapse%>");
35095 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35096 = build_int_cst (NULL_TREE, collapse);
35097 ordered = collapse;
35099 if (ordered)
35101 for (tree *pc = &clauses; *pc; )
35102 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35104 error_at (OMP_CLAUSE_LOCATION (*pc),
35105 "%<linear%> clause may not be specified together "
35106 "with %<ordered%> clause with a parameter");
35107 *pc = OMP_CLAUSE_CHAIN (*pc);
35109 else
35110 pc = &OMP_CLAUSE_CHAIN (*pc);
35113 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35114 count = ordered ? ordered : collapse;
35116 declv = make_tree_vec (count);
35117 initv = make_tree_vec (count);
35118 condv = make_tree_vec (count);
35119 incrv = make_tree_vec (count);
35121 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35123 for (i = 0; i < count; i++)
35125 int bracecount = 0;
35126 tree add_private_clause = NULL_TREE;
35127 location_t loc;
35129 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35131 if (!collapse_err)
35132 cp_parser_error (parser, "for statement expected");
35133 return NULL;
35135 loc = cp_lexer_consume_token (parser->lexer)->location;
35137 matching_parens parens;
35138 if (!parens.require_open (parser))
35139 return NULL;
35141 init = orig_init = decl = real_decl = NULL;
35142 this_pre_body = push_stmt_list ();
35144 add_private_clause
35145 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35146 init, orig_init, decl, real_decl);
35148 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35149 if (this_pre_body)
35151 this_pre_body = pop_stmt_list (this_pre_body);
35152 if (pre_body)
35154 tree t = pre_body;
35155 pre_body = push_stmt_list ();
35156 add_stmt (t);
35157 add_stmt (this_pre_body);
35158 pre_body = pop_stmt_list (pre_body);
35160 else
35161 pre_body = this_pre_body;
35164 if (decl)
35165 real_decl = decl;
35166 if (cclauses != NULL
35167 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35168 && real_decl != NULL_TREE)
35170 tree *c;
35171 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35172 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35173 && OMP_CLAUSE_DECL (*c) == real_decl)
35175 error_at (loc, "iteration variable %qD"
35176 " should not be firstprivate", real_decl);
35177 *c = OMP_CLAUSE_CHAIN (*c);
35179 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35180 && OMP_CLAUSE_DECL (*c) == real_decl)
35182 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35183 tree l = *c;
35184 *c = OMP_CLAUSE_CHAIN (*c);
35185 if (code == OMP_SIMD)
35187 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35188 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35190 else
35192 OMP_CLAUSE_CHAIN (l) = clauses;
35193 clauses = l;
35195 add_private_clause = NULL_TREE;
35197 else
35199 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35200 && OMP_CLAUSE_DECL (*c) == real_decl)
35201 add_private_clause = NULL_TREE;
35202 c = &OMP_CLAUSE_CHAIN (*c);
35206 if (add_private_clause)
35208 tree c;
35209 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35211 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35212 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35213 && OMP_CLAUSE_DECL (c) == decl)
35214 break;
35215 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35216 && OMP_CLAUSE_DECL (c) == decl)
35217 error_at (loc, "iteration variable %qD "
35218 "should not be firstprivate",
35219 decl);
35220 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35221 && OMP_CLAUSE_DECL (c) == decl)
35222 error_at (loc, "iteration variable %qD should not be reduction",
35223 decl);
35225 if (c == NULL)
35227 if (code != OMP_SIMD)
35228 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35229 else if (collapse == 1)
35230 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35231 else
35232 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35233 OMP_CLAUSE_DECL (c) = add_private_clause;
35234 c = finish_omp_clauses (c, C_ORT_OMP);
35235 if (c)
35237 OMP_CLAUSE_CHAIN (c) = clauses;
35238 clauses = c;
35239 /* For linear, signal that we need to fill up
35240 the so far unknown linear step. */
35241 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35242 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35247 cond = NULL;
35248 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35249 cond = cp_parser_omp_for_cond (parser, decl);
35250 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35252 incr = NULL;
35253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35255 /* If decl is an iterator, preserve the operator on decl
35256 until finish_omp_for. */
35257 if (real_decl
35258 && ((processing_template_decl
35259 && (TREE_TYPE (real_decl) == NULL_TREE
35260 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35261 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35262 incr = cp_parser_omp_for_incr (parser, real_decl);
35263 else
35264 incr = cp_parser_expression (parser);
35265 if (!EXPR_HAS_LOCATION (incr))
35266 protected_set_expr_location (incr, input_location);
35269 if (!parens.require_close (parser))
35270 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35271 /*or_comma=*/false,
35272 /*consume_paren=*/true);
35274 TREE_VEC_ELT (declv, i) = decl;
35275 TREE_VEC_ELT (initv, i) = init;
35276 TREE_VEC_ELT (condv, i) = cond;
35277 TREE_VEC_ELT (incrv, i) = incr;
35278 if (orig_init)
35280 orig_inits.safe_grow_cleared (i + 1);
35281 orig_inits[i] = orig_init;
35284 if (i == count - 1)
35285 break;
35287 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35288 in between the collapsed for loops to be still considered perfectly
35289 nested. Hopefully the final version clarifies this.
35290 For now handle (multiple) {'s and empty statements. */
35291 cp_parser_parse_tentatively (parser);
35292 for (;;)
35294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35295 break;
35296 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35298 cp_lexer_consume_token (parser->lexer);
35299 bracecount++;
35301 else if (bracecount
35302 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35303 cp_lexer_consume_token (parser->lexer);
35304 else
35306 loc = cp_lexer_peek_token (parser->lexer)->location;
35307 error_at (loc, "not enough for loops to collapse");
35308 collapse_err = true;
35309 cp_parser_abort_tentative_parse (parser);
35310 declv = NULL_TREE;
35311 break;
35315 if (declv)
35317 cp_parser_parse_definitely (parser);
35318 nbraces += bracecount;
35322 if (nbraces)
35323 if_p = NULL;
35325 /* Note that we saved the original contents of this flag when we entered
35326 the structured block, and so we don't need to re-save it here. */
35327 parser->in_statement = IN_OMP_FOR;
35329 /* Note that the grammar doesn't call for a structured block here,
35330 though the loop as a whole is a structured block. */
35331 body = push_stmt_list ();
35332 cp_parser_statement (parser, NULL_TREE, false, if_p);
35333 body = pop_stmt_list (body);
35335 if (declv == NULL_TREE)
35336 ret = NULL_TREE;
35337 else
35338 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35339 body, pre_body, &orig_inits, clauses);
35341 while (nbraces)
35343 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35345 cp_lexer_consume_token (parser->lexer);
35346 nbraces--;
35348 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35349 cp_lexer_consume_token (parser->lexer);
35350 else
35352 if (!collapse_err)
35354 error_at (cp_lexer_peek_token (parser->lexer)->location,
35355 "collapsed loops not perfectly nested");
35357 collapse_err = true;
35358 cp_parser_statement_seq_opt (parser, NULL);
35359 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35360 break;
35364 while (!for_block->is_empty ())
35366 tree t = for_block->pop ();
35367 if (TREE_CODE (t) == STATEMENT_LIST)
35368 add_stmt (pop_stmt_list (t));
35369 else
35370 add_stmt (t);
35372 release_tree_vector (for_block);
35374 return ret;
35377 /* Helper function for OpenMP parsing, split clauses and call
35378 finish_omp_clauses on each of the set of clauses afterwards. */
35380 static void
35381 cp_omp_split_clauses (location_t loc, enum tree_code code,
35382 omp_clause_mask mask, tree clauses, tree *cclauses)
35384 int i;
35385 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35386 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35387 if (cclauses[i])
35388 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35391 /* OpenMP 4.0:
35392 #pragma omp simd simd-clause[optseq] new-line
35393 for-loop */
35395 #define OMP_SIMD_CLAUSE_MASK \
35396 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35405 static tree
35406 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35407 char *p_name, omp_clause_mask mask, tree *cclauses,
35408 bool *if_p)
35410 tree clauses, sb, ret;
35411 unsigned int save;
35412 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35414 strcat (p_name, " simd");
35415 mask |= OMP_SIMD_CLAUSE_MASK;
35417 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35418 cclauses == NULL);
35419 if (cclauses)
35421 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35422 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35423 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35424 OMP_CLAUSE_ORDERED);
35425 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35427 error_at (OMP_CLAUSE_LOCATION (c),
35428 "%<ordered%> clause with parameter may not be specified "
35429 "on %qs construct", p_name);
35430 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35434 sb = begin_omp_structured_block ();
35435 save = cp_parser_begin_omp_structured_block (parser);
35437 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35439 cp_parser_end_omp_structured_block (parser, save);
35440 add_stmt (finish_omp_structured_block (sb));
35442 return ret;
35445 /* OpenMP 2.5:
35446 #pragma omp for for-clause[optseq] new-line
35447 for-loop
35449 OpenMP 4.0:
35450 #pragma omp for simd for-simd-clause[optseq] new-line
35451 for-loop */
35453 #define OMP_FOR_CLAUSE_MASK \
35454 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35464 static tree
35465 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35466 char *p_name, omp_clause_mask mask, tree *cclauses,
35467 bool *if_p)
35469 tree clauses, sb, ret;
35470 unsigned int save;
35471 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35473 strcat (p_name, " for");
35474 mask |= OMP_FOR_CLAUSE_MASK;
35475 /* parallel for{, simd} disallows nowait clause, but for
35476 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35477 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35478 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35479 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35480 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35481 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35483 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35485 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35486 const char *p = IDENTIFIER_POINTER (id);
35488 if (strcmp (p, "simd") == 0)
35490 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35491 if (cclauses == NULL)
35492 cclauses = cclauses_buf;
35494 cp_lexer_consume_token (parser->lexer);
35495 if (!flag_openmp) /* flag_openmp_simd */
35496 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35497 cclauses, if_p);
35498 sb = begin_omp_structured_block ();
35499 save = cp_parser_begin_omp_structured_block (parser);
35500 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35501 cclauses, if_p);
35502 cp_parser_end_omp_structured_block (parser, save);
35503 tree body = finish_omp_structured_block (sb);
35504 if (ret == NULL)
35505 return ret;
35506 ret = make_node (OMP_FOR);
35507 TREE_TYPE (ret) = void_type_node;
35508 OMP_FOR_BODY (ret) = body;
35509 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35510 SET_EXPR_LOCATION (ret, loc);
35511 add_stmt (ret);
35512 return ret;
35515 if (!flag_openmp) /* flag_openmp_simd */
35517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35518 return NULL_TREE;
35521 /* Composite distribute parallel for disallows linear clause. */
35522 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35523 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35525 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35526 cclauses == NULL);
35527 if (cclauses)
35529 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35530 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35533 sb = begin_omp_structured_block ();
35534 save = cp_parser_begin_omp_structured_block (parser);
35536 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35538 cp_parser_end_omp_structured_block (parser, save);
35539 add_stmt (finish_omp_structured_block (sb));
35541 return ret;
35544 /* OpenMP 2.5:
35545 # pragma omp master new-line
35546 structured-block */
35548 static tree
35549 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35551 cp_parser_require_pragma_eol (parser, pragma_tok);
35552 return c_finish_omp_master (input_location,
35553 cp_parser_omp_structured_block (parser, if_p));
35556 /* OpenMP 2.5:
35557 # pragma omp ordered new-line
35558 structured-block
35560 OpenMP 4.5:
35561 # pragma omp ordered ordered-clauses new-line
35562 structured-block */
35564 #define OMP_ORDERED_CLAUSE_MASK \
35565 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35568 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35569 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35571 static bool
35572 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35573 enum pragma_context context, bool *if_p)
35575 location_t loc = pragma_tok->location;
35577 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35579 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35580 const char *p = IDENTIFIER_POINTER (id);
35582 if (strcmp (p, "depend") == 0)
35584 if (!flag_openmp) /* flag_openmp_simd */
35586 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35587 return false;
35589 if (context == pragma_stmt)
35591 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35592 "%<depend%> clause may only be used in compound "
35593 "statements");
35594 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35595 return false;
35597 tree clauses
35598 = cp_parser_omp_all_clauses (parser,
35599 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35600 "#pragma omp ordered", pragma_tok);
35601 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35602 return false;
35606 tree clauses
35607 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35608 "#pragma omp ordered", pragma_tok);
35610 if (!flag_openmp /* flag_openmp_simd */
35611 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35612 return false;
35614 c_finish_omp_ordered (loc, clauses,
35615 cp_parser_omp_structured_block (parser, if_p));
35616 return true;
35619 /* OpenMP 2.5:
35621 section-scope:
35622 { section-sequence }
35624 section-sequence:
35625 section-directive[opt] structured-block
35626 section-sequence section-directive structured-block */
35628 static tree
35629 cp_parser_omp_sections_scope (cp_parser *parser)
35631 tree stmt, substmt;
35632 bool error_suppress = false;
35633 cp_token *tok;
35635 matching_braces braces;
35636 if (!braces.require_open (parser))
35637 return NULL_TREE;
35639 stmt = push_stmt_list ();
35641 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35642 != PRAGMA_OMP_SECTION)
35644 substmt = cp_parser_omp_structured_block (parser, NULL);
35645 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35646 add_stmt (substmt);
35649 while (1)
35651 tok = cp_lexer_peek_token (parser->lexer);
35652 if (tok->type == CPP_CLOSE_BRACE)
35653 break;
35654 if (tok->type == CPP_EOF)
35655 break;
35657 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35659 cp_lexer_consume_token (parser->lexer);
35660 cp_parser_require_pragma_eol (parser, tok);
35661 error_suppress = false;
35663 else if (!error_suppress)
35665 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35666 error_suppress = true;
35669 substmt = cp_parser_omp_structured_block (parser, NULL);
35670 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35671 add_stmt (substmt);
35673 braces.require_close (parser);
35675 substmt = pop_stmt_list (stmt);
35677 stmt = make_node (OMP_SECTIONS);
35678 TREE_TYPE (stmt) = void_type_node;
35679 OMP_SECTIONS_BODY (stmt) = substmt;
35681 add_stmt (stmt);
35682 return stmt;
35685 /* OpenMP 2.5:
35686 # pragma omp sections sections-clause[optseq] newline
35687 sections-scope */
35689 #define OMP_SECTIONS_CLAUSE_MASK \
35690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35696 static tree
35697 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35698 char *p_name, omp_clause_mask mask, tree *cclauses)
35700 tree clauses, ret;
35701 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35703 strcat (p_name, " sections");
35704 mask |= OMP_SECTIONS_CLAUSE_MASK;
35705 if (cclauses)
35706 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35708 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35709 cclauses == NULL);
35710 if (cclauses)
35712 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35713 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35716 ret = cp_parser_omp_sections_scope (parser);
35717 if (ret)
35718 OMP_SECTIONS_CLAUSES (ret) = clauses;
35720 return ret;
35723 /* OpenMP 2.5:
35724 # pragma omp parallel parallel-clause[optseq] new-line
35725 structured-block
35726 # pragma omp parallel for parallel-for-clause[optseq] new-line
35727 structured-block
35728 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35729 structured-block
35731 OpenMP 4.0:
35732 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35733 structured-block */
35735 #define OMP_PARALLEL_CLAUSE_MASK \
35736 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35746 static tree
35747 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35748 char *p_name, omp_clause_mask mask, tree *cclauses,
35749 bool *if_p)
35751 tree stmt, clauses, block;
35752 unsigned int save;
35753 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35755 strcat (p_name, " parallel");
35756 mask |= OMP_PARALLEL_CLAUSE_MASK;
35757 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35758 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35759 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35760 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35764 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35765 if (cclauses == NULL)
35766 cclauses = cclauses_buf;
35768 cp_lexer_consume_token (parser->lexer);
35769 if (!flag_openmp) /* flag_openmp_simd */
35770 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35771 if_p);
35772 block = begin_omp_parallel ();
35773 save = cp_parser_begin_omp_structured_block (parser);
35774 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35775 if_p);
35776 cp_parser_end_omp_structured_block (parser, save);
35777 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35778 block);
35779 if (ret == NULL_TREE)
35780 return ret;
35781 OMP_PARALLEL_COMBINED (stmt) = 1;
35782 return stmt;
35784 /* When combined with distribute, parallel has to be followed by for.
35785 #pragma omp target parallel is allowed though. */
35786 else if (cclauses
35787 && (mask & (OMP_CLAUSE_MASK_1
35788 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35790 error_at (loc, "expected %<for%> after %qs", p_name);
35791 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35792 return NULL_TREE;
35794 else if (!flag_openmp) /* flag_openmp_simd */
35796 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35797 return NULL_TREE;
35799 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35801 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35802 const char *p = IDENTIFIER_POINTER (id);
35803 if (strcmp (p, "sections") == 0)
35805 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35806 cclauses = cclauses_buf;
35808 cp_lexer_consume_token (parser->lexer);
35809 block = begin_omp_parallel ();
35810 save = cp_parser_begin_omp_structured_block (parser);
35811 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35812 cp_parser_end_omp_structured_block (parser, save);
35813 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35814 block);
35815 OMP_PARALLEL_COMBINED (stmt) = 1;
35816 return stmt;
35820 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35821 cclauses == NULL);
35822 if (cclauses)
35824 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35825 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35828 block = begin_omp_parallel ();
35829 save = cp_parser_begin_omp_structured_block (parser);
35830 cp_parser_statement (parser, NULL_TREE, false, if_p);
35831 cp_parser_end_omp_structured_block (parser, save);
35832 stmt = finish_omp_parallel (clauses, block);
35833 return stmt;
35836 /* OpenMP 2.5:
35837 # pragma omp single single-clause[optseq] new-line
35838 structured-block */
35840 #define OMP_SINGLE_CLAUSE_MASK \
35841 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35846 static tree
35847 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35849 tree stmt = make_node (OMP_SINGLE);
35850 TREE_TYPE (stmt) = void_type_node;
35852 OMP_SINGLE_CLAUSES (stmt)
35853 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35854 "#pragma omp single", pragma_tok);
35855 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35857 return add_stmt (stmt);
35860 /* OpenMP 3.0:
35861 # pragma omp task task-clause[optseq] new-line
35862 structured-block */
35864 #define OMP_TASK_CLAUSE_MASK \
35865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35876 static tree
35877 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35879 tree clauses, block;
35880 unsigned int save;
35882 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35883 "#pragma omp task", pragma_tok);
35884 block = begin_omp_task ();
35885 save = cp_parser_begin_omp_structured_block (parser);
35886 cp_parser_statement (parser, NULL_TREE, false, if_p);
35887 cp_parser_end_omp_structured_block (parser, save);
35888 return finish_omp_task (clauses, block);
35891 /* OpenMP 3.0:
35892 # pragma omp taskwait new-line */
35894 static void
35895 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35897 cp_parser_require_pragma_eol (parser, pragma_tok);
35898 finish_omp_taskwait ();
35901 /* OpenMP 3.1:
35902 # pragma omp taskyield new-line */
35904 static void
35905 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35907 cp_parser_require_pragma_eol (parser, pragma_tok);
35908 finish_omp_taskyield ();
35911 /* OpenMP 4.0:
35912 # pragma omp taskgroup new-line
35913 structured-block */
35915 static tree
35916 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35918 cp_parser_require_pragma_eol (parser, pragma_tok);
35919 return c_finish_omp_taskgroup (input_location,
35920 cp_parser_omp_structured_block (parser,
35921 if_p));
35925 /* OpenMP 2.5:
35926 # pragma omp threadprivate (variable-list) */
35928 static void
35929 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35931 tree vars;
35933 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35934 cp_parser_require_pragma_eol (parser, pragma_tok);
35936 finish_omp_threadprivate (vars);
35939 /* OpenMP 4.0:
35940 # pragma omp cancel cancel-clause[optseq] new-line */
35942 #define OMP_CANCEL_CLAUSE_MASK \
35943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35949 static void
35950 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35952 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35953 "#pragma omp cancel", pragma_tok);
35954 finish_omp_cancel (clauses);
35957 /* OpenMP 4.0:
35958 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35960 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35966 static void
35967 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35968 enum pragma_context context)
35970 tree clauses;
35971 bool point_seen = false;
35973 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35975 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35976 const char *p = IDENTIFIER_POINTER (id);
35978 if (strcmp (p, "point") == 0)
35980 cp_lexer_consume_token (parser->lexer);
35981 point_seen = true;
35984 if (!point_seen)
35986 cp_parser_error (parser, "expected %<point%>");
35987 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35988 return;
35991 if (context != pragma_compound)
35993 if (context == pragma_stmt)
35994 error_at (pragma_tok->location,
35995 "%<#pragma %s%> may only be used in compound statements",
35996 "omp cancellation point");
35997 else
35998 cp_parser_error (parser, "expected declaration specifiers");
35999 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36000 return;
36003 clauses = cp_parser_omp_all_clauses (parser,
36004 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36005 "#pragma omp cancellation point",
36006 pragma_tok);
36007 finish_omp_cancellation_point (clauses);
36010 /* OpenMP 4.0:
36011 #pragma omp distribute distribute-clause[optseq] new-line
36012 for-loop */
36014 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36021 static tree
36022 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36023 char *p_name, omp_clause_mask mask, tree *cclauses,
36024 bool *if_p)
36026 tree clauses, sb, ret;
36027 unsigned int save;
36028 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36030 strcat (p_name, " distribute");
36031 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36033 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36035 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36036 const char *p = IDENTIFIER_POINTER (id);
36037 bool simd = false;
36038 bool parallel = false;
36040 if (strcmp (p, "simd") == 0)
36041 simd = true;
36042 else
36043 parallel = strcmp (p, "parallel") == 0;
36044 if (parallel || simd)
36046 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36047 if (cclauses == NULL)
36048 cclauses = cclauses_buf;
36049 cp_lexer_consume_token (parser->lexer);
36050 if (!flag_openmp) /* flag_openmp_simd */
36052 if (simd)
36053 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36054 cclauses, if_p);
36055 else
36056 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36057 cclauses, if_p);
36059 sb = begin_omp_structured_block ();
36060 save = cp_parser_begin_omp_structured_block (parser);
36061 if (simd)
36062 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36063 cclauses, if_p);
36064 else
36065 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36066 cclauses, if_p);
36067 cp_parser_end_omp_structured_block (parser, save);
36068 tree body = finish_omp_structured_block (sb);
36069 if (ret == NULL)
36070 return ret;
36071 ret = make_node (OMP_DISTRIBUTE);
36072 TREE_TYPE (ret) = void_type_node;
36073 OMP_FOR_BODY (ret) = body;
36074 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36075 SET_EXPR_LOCATION (ret, loc);
36076 add_stmt (ret);
36077 return ret;
36080 if (!flag_openmp) /* flag_openmp_simd */
36082 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36083 return NULL_TREE;
36086 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36087 cclauses == NULL);
36088 if (cclauses)
36090 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36091 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36094 sb = begin_omp_structured_block ();
36095 save = cp_parser_begin_omp_structured_block (parser);
36097 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36099 cp_parser_end_omp_structured_block (parser, save);
36100 add_stmt (finish_omp_structured_block (sb));
36102 return ret;
36105 /* OpenMP 4.0:
36106 # pragma omp teams teams-clause[optseq] new-line
36107 structured-block */
36109 #define OMP_TEAMS_CLAUSE_MASK \
36110 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36118 static tree
36119 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36120 char *p_name, omp_clause_mask mask, tree *cclauses,
36121 bool *if_p)
36123 tree clauses, sb, ret;
36124 unsigned int save;
36125 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36127 strcat (p_name, " teams");
36128 mask |= OMP_TEAMS_CLAUSE_MASK;
36130 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36132 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36133 const char *p = IDENTIFIER_POINTER (id);
36134 if (strcmp (p, "distribute") == 0)
36136 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36137 if (cclauses == NULL)
36138 cclauses = cclauses_buf;
36140 cp_lexer_consume_token (parser->lexer);
36141 if (!flag_openmp) /* flag_openmp_simd */
36142 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36143 cclauses, if_p);
36144 sb = begin_omp_structured_block ();
36145 save = cp_parser_begin_omp_structured_block (parser);
36146 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36147 cclauses, if_p);
36148 cp_parser_end_omp_structured_block (parser, save);
36149 tree body = finish_omp_structured_block (sb);
36150 if (ret == NULL)
36151 return ret;
36152 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36153 ret = make_node (OMP_TEAMS);
36154 TREE_TYPE (ret) = void_type_node;
36155 OMP_TEAMS_CLAUSES (ret) = clauses;
36156 OMP_TEAMS_BODY (ret) = body;
36157 OMP_TEAMS_COMBINED (ret) = 1;
36158 SET_EXPR_LOCATION (ret, loc);
36159 return add_stmt (ret);
36162 if (!flag_openmp) /* flag_openmp_simd */
36164 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36165 return NULL_TREE;
36168 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36169 cclauses == NULL);
36170 if (cclauses)
36172 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36173 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36176 tree stmt = make_node (OMP_TEAMS);
36177 TREE_TYPE (stmt) = void_type_node;
36178 OMP_TEAMS_CLAUSES (stmt) = clauses;
36179 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36180 SET_EXPR_LOCATION (stmt, loc);
36182 return add_stmt (stmt);
36185 /* OpenMP 4.0:
36186 # pragma omp target data target-data-clause[optseq] new-line
36187 structured-block */
36189 #define OMP_TARGET_DATA_CLAUSE_MASK \
36190 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36195 static tree
36196 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36198 tree clauses
36199 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36200 "#pragma omp target data", pragma_tok);
36201 int map_seen = 0;
36202 for (tree *pc = &clauses; *pc;)
36204 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36205 switch (OMP_CLAUSE_MAP_KIND (*pc))
36207 case GOMP_MAP_TO:
36208 case GOMP_MAP_ALWAYS_TO:
36209 case GOMP_MAP_FROM:
36210 case GOMP_MAP_ALWAYS_FROM:
36211 case GOMP_MAP_TOFROM:
36212 case GOMP_MAP_ALWAYS_TOFROM:
36213 case GOMP_MAP_ALLOC:
36214 map_seen = 3;
36215 break;
36216 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36217 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36218 case GOMP_MAP_ALWAYS_POINTER:
36219 break;
36220 default:
36221 map_seen |= 1;
36222 error_at (OMP_CLAUSE_LOCATION (*pc),
36223 "%<#pragma omp target data%> with map-type other "
36224 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36225 "on %<map%> clause");
36226 *pc = OMP_CLAUSE_CHAIN (*pc);
36227 continue;
36229 pc = &OMP_CLAUSE_CHAIN (*pc);
36232 if (map_seen != 3)
36234 if (map_seen == 0)
36235 error_at (pragma_tok->location,
36236 "%<#pragma omp target data%> must contain at least "
36237 "one %<map%> clause");
36238 return NULL_TREE;
36241 tree stmt = make_node (OMP_TARGET_DATA);
36242 TREE_TYPE (stmt) = void_type_node;
36243 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36245 keep_next_level (true);
36246 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36248 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36249 return add_stmt (stmt);
36252 /* OpenMP 4.5:
36253 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36254 structured-block */
36256 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36257 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36263 static tree
36264 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36265 enum pragma_context context)
36267 bool data_seen = false;
36268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36270 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36271 const char *p = IDENTIFIER_POINTER (id);
36273 if (strcmp (p, "data") == 0)
36275 cp_lexer_consume_token (parser->lexer);
36276 data_seen = true;
36279 if (!data_seen)
36281 cp_parser_error (parser, "expected %<data%>");
36282 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36283 return NULL_TREE;
36286 if (context == pragma_stmt)
36288 error_at (pragma_tok->location,
36289 "%<#pragma %s%> may only be used in compound statements",
36290 "omp target enter data");
36291 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36292 return NULL_TREE;
36295 tree clauses
36296 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36297 "#pragma omp target enter data", pragma_tok);
36298 int map_seen = 0;
36299 for (tree *pc = &clauses; *pc;)
36301 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36302 switch (OMP_CLAUSE_MAP_KIND (*pc))
36304 case GOMP_MAP_TO:
36305 case GOMP_MAP_ALWAYS_TO:
36306 case GOMP_MAP_ALLOC:
36307 map_seen = 3;
36308 break;
36309 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36310 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36311 case GOMP_MAP_ALWAYS_POINTER:
36312 break;
36313 default:
36314 map_seen |= 1;
36315 error_at (OMP_CLAUSE_LOCATION (*pc),
36316 "%<#pragma omp target enter data%> with map-type other "
36317 "than %<to%> or %<alloc%> on %<map%> clause");
36318 *pc = OMP_CLAUSE_CHAIN (*pc);
36319 continue;
36321 pc = &OMP_CLAUSE_CHAIN (*pc);
36324 if (map_seen != 3)
36326 if (map_seen == 0)
36327 error_at (pragma_tok->location,
36328 "%<#pragma omp target enter data%> must contain at least "
36329 "one %<map%> clause");
36330 return NULL_TREE;
36333 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36334 TREE_TYPE (stmt) = void_type_node;
36335 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36336 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36337 return add_stmt (stmt);
36340 /* OpenMP 4.5:
36341 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36342 structured-block */
36344 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36345 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36351 static tree
36352 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36353 enum pragma_context context)
36355 bool data_seen = false;
36356 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36358 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36359 const char *p = IDENTIFIER_POINTER (id);
36361 if (strcmp (p, "data") == 0)
36363 cp_lexer_consume_token (parser->lexer);
36364 data_seen = true;
36367 if (!data_seen)
36369 cp_parser_error (parser, "expected %<data%>");
36370 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36371 return NULL_TREE;
36374 if (context == pragma_stmt)
36376 error_at (pragma_tok->location,
36377 "%<#pragma %s%> may only be used in compound statements",
36378 "omp target exit data");
36379 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36380 return NULL_TREE;
36383 tree clauses
36384 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36385 "#pragma omp target exit data", pragma_tok);
36386 int map_seen = 0;
36387 for (tree *pc = &clauses; *pc;)
36389 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36390 switch (OMP_CLAUSE_MAP_KIND (*pc))
36392 case GOMP_MAP_FROM:
36393 case GOMP_MAP_ALWAYS_FROM:
36394 case GOMP_MAP_RELEASE:
36395 case GOMP_MAP_DELETE:
36396 map_seen = 3;
36397 break;
36398 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36399 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36400 case GOMP_MAP_ALWAYS_POINTER:
36401 break;
36402 default:
36403 map_seen |= 1;
36404 error_at (OMP_CLAUSE_LOCATION (*pc),
36405 "%<#pragma omp target exit data%> with map-type other "
36406 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36407 " clause");
36408 *pc = OMP_CLAUSE_CHAIN (*pc);
36409 continue;
36411 pc = &OMP_CLAUSE_CHAIN (*pc);
36414 if (map_seen != 3)
36416 if (map_seen == 0)
36417 error_at (pragma_tok->location,
36418 "%<#pragma omp target exit data%> must contain at least "
36419 "one %<map%> clause");
36420 return NULL_TREE;
36423 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36424 TREE_TYPE (stmt) = void_type_node;
36425 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36426 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36427 return add_stmt (stmt);
36430 /* OpenMP 4.0:
36431 # pragma omp target update target-update-clause[optseq] new-line */
36433 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36434 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36441 static bool
36442 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36443 enum pragma_context context)
36445 if (context == pragma_stmt)
36447 error_at (pragma_tok->location,
36448 "%<#pragma %s%> may only be used in compound statements",
36449 "omp target update");
36450 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36451 return false;
36454 tree clauses
36455 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36456 "#pragma omp target update", pragma_tok);
36457 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36458 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36460 error_at (pragma_tok->location,
36461 "%<#pragma omp target update%> must contain at least one "
36462 "%<from%> or %<to%> clauses");
36463 return false;
36466 tree stmt = make_node (OMP_TARGET_UPDATE);
36467 TREE_TYPE (stmt) = void_type_node;
36468 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36469 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36470 add_stmt (stmt);
36471 return false;
36474 /* OpenMP 4.0:
36475 # pragma omp target target-clause[optseq] new-line
36476 structured-block */
36478 #define OMP_TARGET_CLAUSE_MASK \
36479 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36489 static bool
36490 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36491 enum pragma_context context, bool *if_p)
36493 tree *pc = NULL, stmt;
36495 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36497 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36498 const char *p = IDENTIFIER_POINTER (id);
36499 enum tree_code ccode = ERROR_MARK;
36501 if (strcmp (p, "teams") == 0)
36502 ccode = OMP_TEAMS;
36503 else if (strcmp (p, "parallel") == 0)
36504 ccode = OMP_PARALLEL;
36505 else if (strcmp (p, "simd") == 0)
36506 ccode = OMP_SIMD;
36507 if (ccode != ERROR_MARK)
36509 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36510 char p_name[sizeof ("#pragma omp target teams distribute "
36511 "parallel for simd")];
36513 cp_lexer_consume_token (parser->lexer);
36514 strcpy (p_name, "#pragma omp target");
36515 if (!flag_openmp) /* flag_openmp_simd */
36517 tree stmt;
36518 switch (ccode)
36520 case OMP_TEAMS:
36521 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36522 OMP_TARGET_CLAUSE_MASK,
36523 cclauses, if_p);
36524 break;
36525 case OMP_PARALLEL:
36526 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36527 OMP_TARGET_CLAUSE_MASK,
36528 cclauses, if_p);
36529 break;
36530 case OMP_SIMD:
36531 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36532 OMP_TARGET_CLAUSE_MASK,
36533 cclauses, if_p);
36534 break;
36535 default:
36536 gcc_unreachable ();
36538 return stmt != NULL_TREE;
36540 keep_next_level (true);
36541 tree sb = begin_omp_structured_block (), ret;
36542 unsigned save = cp_parser_begin_omp_structured_block (parser);
36543 switch (ccode)
36545 case OMP_TEAMS:
36546 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36547 OMP_TARGET_CLAUSE_MASK, cclauses,
36548 if_p);
36549 break;
36550 case OMP_PARALLEL:
36551 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36552 OMP_TARGET_CLAUSE_MASK, cclauses,
36553 if_p);
36554 break;
36555 case OMP_SIMD:
36556 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36557 OMP_TARGET_CLAUSE_MASK, cclauses,
36558 if_p);
36559 break;
36560 default:
36561 gcc_unreachable ();
36563 cp_parser_end_omp_structured_block (parser, save);
36564 tree body = finish_omp_structured_block (sb);
36565 if (ret == NULL_TREE)
36566 return false;
36567 if (ccode == OMP_TEAMS && !processing_template_decl)
36569 /* For combined target teams, ensure the num_teams and
36570 thread_limit clause expressions are evaluated on the host,
36571 before entering the target construct. */
36572 tree c;
36573 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36574 c; c = OMP_CLAUSE_CHAIN (c))
36575 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36576 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36577 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36579 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36580 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36581 if (expr == error_mark_node)
36582 continue;
36583 tree tmp = TARGET_EXPR_SLOT (expr);
36584 add_stmt (expr);
36585 OMP_CLAUSE_OPERAND (c, 0) = expr;
36586 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36587 OMP_CLAUSE_FIRSTPRIVATE);
36588 OMP_CLAUSE_DECL (tc) = tmp;
36589 OMP_CLAUSE_CHAIN (tc)
36590 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36591 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36594 tree stmt = make_node (OMP_TARGET);
36595 TREE_TYPE (stmt) = void_type_node;
36596 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36597 OMP_TARGET_BODY (stmt) = body;
36598 OMP_TARGET_COMBINED (stmt) = 1;
36599 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36600 add_stmt (stmt);
36601 pc = &OMP_TARGET_CLAUSES (stmt);
36602 goto check_clauses;
36604 else if (!flag_openmp) /* flag_openmp_simd */
36606 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36607 return false;
36609 else if (strcmp (p, "data") == 0)
36611 cp_lexer_consume_token (parser->lexer);
36612 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36613 return true;
36615 else if (strcmp (p, "enter") == 0)
36617 cp_lexer_consume_token (parser->lexer);
36618 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36619 return false;
36621 else if (strcmp (p, "exit") == 0)
36623 cp_lexer_consume_token (parser->lexer);
36624 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36625 return false;
36627 else if (strcmp (p, "update") == 0)
36629 cp_lexer_consume_token (parser->lexer);
36630 return cp_parser_omp_target_update (parser, pragma_tok, context);
36633 if (!flag_openmp) /* flag_openmp_simd */
36635 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36636 return false;
36639 stmt = make_node (OMP_TARGET);
36640 TREE_TYPE (stmt) = void_type_node;
36642 OMP_TARGET_CLAUSES (stmt)
36643 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36644 "#pragma omp target", pragma_tok);
36645 pc = &OMP_TARGET_CLAUSES (stmt);
36646 keep_next_level (true);
36647 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36649 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36650 add_stmt (stmt);
36652 check_clauses:
36653 while (*pc)
36655 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36656 switch (OMP_CLAUSE_MAP_KIND (*pc))
36658 case GOMP_MAP_TO:
36659 case GOMP_MAP_ALWAYS_TO:
36660 case GOMP_MAP_FROM:
36661 case GOMP_MAP_ALWAYS_FROM:
36662 case GOMP_MAP_TOFROM:
36663 case GOMP_MAP_ALWAYS_TOFROM:
36664 case GOMP_MAP_ALLOC:
36665 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36666 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36667 case GOMP_MAP_ALWAYS_POINTER:
36668 break;
36669 default:
36670 error_at (OMP_CLAUSE_LOCATION (*pc),
36671 "%<#pragma omp target%> with map-type other "
36672 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36673 "on %<map%> clause");
36674 *pc = OMP_CLAUSE_CHAIN (*pc);
36675 continue;
36677 pc = &OMP_CLAUSE_CHAIN (*pc);
36679 return true;
36682 /* OpenACC 2.0:
36683 # pragma acc cache (variable-list) new-line
36686 static tree
36687 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36689 tree stmt, clauses;
36691 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36692 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36694 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36696 stmt = make_node (OACC_CACHE);
36697 TREE_TYPE (stmt) = void_type_node;
36698 OACC_CACHE_CLAUSES (stmt) = clauses;
36699 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36700 add_stmt (stmt);
36702 return stmt;
36705 /* OpenACC 2.0:
36706 # pragma acc data oacc-data-clause[optseq] new-line
36707 structured-block */
36709 #define OACC_DATA_CLAUSE_MASK \
36710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36722 static tree
36723 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36725 tree stmt, clauses, block;
36726 unsigned int save;
36728 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36729 "#pragma acc data", pragma_tok);
36731 block = begin_omp_parallel ();
36732 save = cp_parser_begin_omp_structured_block (parser);
36733 cp_parser_statement (parser, NULL_TREE, false, if_p);
36734 cp_parser_end_omp_structured_block (parser, save);
36735 stmt = finish_oacc_data (clauses, block);
36736 return stmt;
36739 /* OpenACC 2.0:
36740 # pragma acc host_data <clauses> new-line
36741 structured-block */
36743 #define OACC_HOST_DATA_CLAUSE_MASK \
36744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36746 static tree
36747 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36749 tree stmt, clauses, block;
36750 unsigned int save;
36752 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36753 "#pragma acc host_data", pragma_tok);
36755 block = begin_omp_parallel ();
36756 save = cp_parser_begin_omp_structured_block (parser);
36757 cp_parser_statement (parser, NULL_TREE, false, if_p);
36758 cp_parser_end_omp_structured_block (parser, save);
36759 stmt = finish_oacc_host_data (clauses, block);
36760 return stmt;
36763 /* OpenACC 2.0:
36764 # pragma acc declare oacc-data-clause[optseq] new-line
36767 #define OACC_DECLARE_CLAUSE_MASK \
36768 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36781 static tree
36782 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36784 tree clauses, stmt;
36785 bool error = false;
36787 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36788 "#pragma acc declare", pragma_tok, true);
36791 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36793 error_at (pragma_tok->location,
36794 "no valid clauses specified in %<#pragma acc declare%>");
36795 return NULL_TREE;
36798 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36800 location_t loc = OMP_CLAUSE_LOCATION (t);
36801 tree decl = OMP_CLAUSE_DECL (t);
36802 if (!DECL_P (decl))
36804 error_at (loc, "array section in %<#pragma acc declare%>");
36805 error = true;
36806 continue;
36808 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36809 switch (OMP_CLAUSE_MAP_KIND (t))
36811 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36812 case GOMP_MAP_FORCE_ALLOC:
36813 case GOMP_MAP_FORCE_TO:
36814 case GOMP_MAP_FORCE_DEVICEPTR:
36815 case GOMP_MAP_DEVICE_RESIDENT:
36816 break;
36818 case GOMP_MAP_LINK:
36819 if (!global_bindings_p ()
36820 && (TREE_STATIC (decl)
36821 || !DECL_EXTERNAL (decl)))
36823 error_at (loc,
36824 "%qD must be a global variable in "
36825 "%<#pragma acc declare link%>",
36826 decl);
36827 error = true;
36828 continue;
36830 break;
36832 default:
36833 if (global_bindings_p ())
36835 error_at (loc, "invalid OpenACC clause at file scope");
36836 error = true;
36837 continue;
36839 if (DECL_EXTERNAL (decl))
36841 error_at (loc,
36842 "invalid use of %<extern%> variable %qD "
36843 "in %<#pragma acc declare%>", decl);
36844 error = true;
36845 continue;
36847 else if (TREE_PUBLIC (decl))
36849 error_at (loc,
36850 "invalid use of %<global%> variable %qD "
36851 "in %<#pragma acc declare%>", decl);
36852 error = true;
36853 continue;
36855 break;
36858 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36859 || lookup_attribute ("omp declare target link",
36860 DECL_ATTRIBUTES (decl)))
36862 error_at (loc, "variable %qD used more than once with "
36863 "%<#pragma acc declare%>", decl);
36864 error = true;
36865 continue;
36868 if (!error)
36870 tree id;
36872 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36873 id = get_identifier ("omp declare target link");
36874 else
36875 id = get_identifier ("omp declare target");
36877 DECL_ATTRIBUTES (decl)
36878 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36879 if (global_bindings_p ())
36881 symtab_node *node = symtab_node::get (decl);
36882 if (node != NULL)
36884 node->offloadable = 1;
36885 if (ENABLE_OFFLOADING)
36887 g->have_offload = true;
36888 if (is_a <varpool_node *> (node))
36889 vec_safe_push (offload_vars, decl);
36896 if (error || global_bindings_p ())
36897 return NULL_TREE;
36899 stmt = make_node (OACC_DECLARE);
36900 TREE_TYPE (stmt) = void_type_node;
36901 OACC_DECLARE_CLAUSES (stmt) = clauses;
36902 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36904 add_stmt (stmt);
36906 return NULL_TREE;
36909 /* OpenACC 2.0:
36910 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36914 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36916 LOC is the location of the #pragma token.
36919 #define OACC_ENTER_DATA_CLAUSE_MASK \
36920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36928 #define OACC_EXIT_DATA_CLAUSE_MASK \
36929 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36935 static tree
36936 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36937 bool enter)
36939 location_t loc = pragma_tok->location;
36940 tree stmt, clauses;
36941 const char *p = "";
36943 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36944 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36946 if (strcmp (p, "data") != 0)
36948 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36949 enter ? "enter" : "exit");
36950 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36951 return NULL_TREE;
36954 cp_lexer_consume_token (parser->lexer);
36956 if (enter)
36957 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36958 "#pragma acc enter data", pragma_tok);
36959 else
36960 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36961 "#pragma acc exit data", pragma_tok);
36963 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36965 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36966 enter ? "enter" : "exit");
36967 return NULL_TREE;
36970 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36971 TREE_TYPE (stmt) = void_type_node;
36972 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36973 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36974 add_stmt (stmt);
36975 return stmt;
36978 /* OpenACC 2.0:
36979 # pragma acc loop oacc-loop-clause[optseq] new-line
36980 structured-block */
36982 #define OACC_LOOP_CLAUSE_MASK \
36983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36994 static tree
36995 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36996 omp_clause_mask mask, tree *cclauses, bool *if_p)
36998 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37000 strcat (p_name, " loop");
37001 mask |= OACC_LOOP_CLAUSE_MASK;
37003 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37004 cclauses == NULL);
37005 if (cclauses)
37007 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37008 if (*cclauses)
37009 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37010 if (clauses)
37011 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37014 tree block = begin_omp_structured_block ();
37015 int save = cp_parser_begin_omp_structured_block (parser);
37016 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37017 cp_parser_end_omp_structured_block (parser, save);
37018 add_stmt (finish_omp_structured_block (block));
37020 return stmt;
37023 /* OpenACC 2.0:
37024 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37025 structured-block
37029 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37030 structured-block
37033 #define OACC_KERNELS_CLAUSE_MASK \
37034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37052 #define OACC_PARALLEL_CLAUSE_MASK \
37053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37074 static tree
37075 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37076 char *p_name, bool *if_p)
37078 omp_clause_mask mask;
37079 enum tree_code code;
37080 switch (cp_parser_pragma_kind (pragma_tok))
37082 case PRAGMA_OACC_KERNELS:
37083 strcat (p_name, " kernels");
37084 mask = OACC_KERNELS_CLAUSE_MASK;
37085 code = OACC_KERNELS;
37086 break;
37087 case PRAGMA_OACC_PARALLEL:
37088 strcat (p_name, " parallel");
37089 mask = OACC_PARALLEL_CLAUSE_MASK;
37090 code = OACC_PARALLEL;
37091 break;
37092 default:
37093 gcc_unreachable ();
37096 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37098 const char *p
37099 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37100 if (strcmp (p, "loop") == 0)
37102 cp_lexer_consume_token (parser->lexer);
37103 tree block = begin_omp_parallel ();
37104 tree clauses;
37105 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37106 if_p);
37107 return finish_omp_construct (code, block, clauses);
37111 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37113 tree block = begin_omp_parallel ();
37114 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37115 cp_parser_statement (parser, NULL_TREE, false, if_p);
37116 cp_parser_end_omp_structured_block (parser, save);
37117 return finish_omp_construct (code, block, clauses);
37120 /* OpenACC 2.0:
37121 # pragma acc update oacc-update-clause[optseq] new-line
37124 #define OACC_UPDATE_CLAUSE_MASK \
37125 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37132 static tree
37133 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37135 tree stmt, clauses;
37137 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37138 "#pragma acc update", pragma_tok);
37140 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37142 error_at (pragma_tok->location,
37143 "%<#pragma acc update%> must contain at least one "
37144 "%<device%> or %<host%> or %<self%> clause");
37145 return NULL_TREE;
37148 stmt = make_node (OACC_UPDATE);
37149 TREE_TYPE (stmt) = void_type_node;
37150 OACC_UPDATE_CLAUSES (stmt) = clauses;
37151 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37152 add_stmt (stmt);
37153 return stmt;
37156 /* OpenACC 2.0:
37157 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37159 LOC is the location of the #pragma token.
37162 #define OACC_WAIT_CLAUSE_MASK \
37163 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37165 static tree
37166 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37168 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37169 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37171 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37172 list = cp_parser_oacc_wait_list (parser, loc, list);
37174 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37175 "#pragma acc wait", pragma_tok);
37177 stmt = c_finish_oacc_wait (loc, list, clauses);
37178 stmt = finish_expr_stmt (stmt);
37180 return stmt;
37183 /* OpenMP 4.0:
37184 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37186 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37187 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37194 static void
37195 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37196 enum pragma_context context)
37198 bool first_p = parser->omp_declare_simd == NULL;
37199 cp_omp_declare_simd_data data;
37200 if (first_p)
37202 data.error_seen = false;
37203 data.fndecl_seen = false;
37204 data.tokens = vNULL;
37205 data.clauses = NULL_TREE;
37206 /* It is safe to take the address of a local variable; it will only be
37207 used while this scope is live. */
37208 parser->omp_declare_simd = &data;
37211 /* Store away all pragma tokens. */
37212 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37213 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37214 cp_lexer_consume_token (parser->lexer);
37215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37216 parser->omp_declare_simd->error_seen = true;
37217 cp_parser_require_pragma_eol (parser, pragma_tok);
37218 struct cp_token_cache *cp
37219 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37220 parser->omp_declare_simd->tokens.safe_push (cp);
37222 if (first_p)
37224 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37225 cp_parser_pragma (parser, context, NULL);
37226 switch (context)
37228 case pragma_external:
37229 cp_parser_declaration (parser);
37230 break;
37231 case pragma_member:
37232 cp_parser_member_declaration (parser);
37233 break;
37234 case pragma_objc_icode:
37235 cp_parser_block_declaration (parser, /*statement_p=*/false);
37236 break;
37237 default:
37238 cp_parser_declaration_statement (parser);
37239 break;
37241 if (parser->omp_declare_simd
37242 && !parser->omp_declare_simd->error_seen
37243 && !parser->omp_declare_simd->fndecl_seen)
37244 error_at (pragma_tok->location,
37245 "%<#pragma omp declare simd%> not immediately followed by "
37246 "function declaration or definition");
37247 data.tokens.release ();
37248 parser->omp_declare_simd = NULL;
37252 /* Finalize #pragma omp declare simd clauses after direct declarator has
37253 been parsed, and put that into "omp declare simd" attribute. */
37255 static tree
37256 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37258 struct cp_token_cache *ce;
37259 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37260 int i;
37262 if (!data->error_seen && data->fndecl_seen)
37264 error ("%<#pragma omp declare simd%> not immediately followed by "
37265 "a single function declaration or definition");
37266 data->error_seen = true;
37268 if (data->error_seen)
37269 return attrs;
37271 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37273 tree c, cl;
37275 cp_parser_push_lexer_for_tokens (parser, ce);
37276 parser->lexer->in_pragma = true;
37277 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37278 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37279 cp_lexer_consume_token (parser->lexer);
37280 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37281 "#pragma omp declare simd", pragma_tok);
37282 cp_parser_pop_lexer (parser);
37283 if (cl)
37284 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37285 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37286 TREE_CHAIN (c) = attrs;
37287 if (processing_template_decl)
37288 ATTR_IS_DEPENDENT (c) = 1;
37289 attrs = c;
37292 data->fndecl_seen = true;
37293 return attrs;
37297 /* OpenMP 4.0:
37298 # pragma omp declare target new-line
37299 declarations and definitions
37300 # pragma omp end declare target new-line
37302 OpenMP 4.5:
37303 # pragma omp declare target ( extended-list ) new-line
37305 # pragma omp declare target declare-target-clauses[seq] new-line */
37307 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37308 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37311 static void
37312 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37314 tree clauses = NULL_TREE;
37315 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37316 clauses
37317 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37318 "#pragma omp declare target", pragma_tok);
37319 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37321 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37322 clauses);
37323 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37324 cp_parser_require_pragma_eol (parser, pragma_tok);
37326 else
37328 cp_parser_require_pragma_eol (parser, pragma_tok);
37329 scope_chain->omp_declare_target_attribute++;
37330 return;
37332 if (scope_chain->omp_declare_target_attribute)
37333 error_at (pragma_tok->location,
37334 "%<#pragma omp declare target%> with clauses in between "
37335 "%<#pragma omp declare target%> without clauses and "
37336 "%<#pragma omp end declare target%>");
37337 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37339 tree t = OMP_CLAUSE_DECL (c), id;
37340 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37341 tree at2 = lookup_attribute ("omp declare target link",
37342 DECL_ATTRIBUTES (t));
37343 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37345 id = get_identifier ("omp declare target link");
37346 std::swap (at1, at2);
37348 else
37349 id = get_identifier ("omp declare target");
37350 if (at2)
37352 error_at (OMP_CLAUSE_LOCATION (c),
37353 "%qD specified both in declare target %<link%> and %<to%>"
37354 " clauses", t);
37355 continue;
37357 if (!at1)
37359 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37360 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37361 continue;
37363 symtab_node *node = symtab_node::get (t);
37364 if (node != NULL)
37366 node->offloadable = 1;
37367 if (ENABLE_OFFLOADING)
37369 g->have_offload = true;
37370 if (is_a <varpool_node *> (node))
37371 vec_safe_push (offload_vars, t);
37378 static void
37379 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37381 const char *p = "";
37382 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37384 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37385 p = IDENTIFIER_POINTER (id);
37387 if (strcmp (p, "declare") == 0)
37389 cp_lexer_consume_token (parser->lexer);
37390 p = "";
37391 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37393 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37394 p = IDENTIFIER_POINTER (id);
37396 if (strcmp (p, "target") == 0)
37397 cp_lexer_consume_token (parser->lexer);
37398 else
37400 cp_parser_error (parser, "expected %<target%>");
37401 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37402 return;
37405 else
37407 cp_parser_error (parser, "expected %<declare%>");
37408 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37409 return;
37411 cp_parser_require_pragma_eol (parser, pragma_tok);
37412 if (!scope_chain->omp_declare_target_attribute)
37413 error_at (pragma_tok->location,
37414 "%<#pragma omp end declare target%> without corresponding "
37415 "%<#pragma omp declare target%>");
37416 else
37417 scope_chain->omp_declare_target_attribute--;
37420 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37421 expression and optional initializer clause of
37422 #pragma omp declare reduction. We store the expression(s) as
37423 either 3, 6 or 7 special statements inside of the artificial function's
37424 body. The first two statements are DECL_EXPRs for the artificial
37425 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37426 expression that uses those variables.
37427 If there was any INITIALIZER clause, this is followed by further statements,
37428 the fourth and fifth statements are DECL_EXPRs for the artificial
37429 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37430 constructor variant (first token after open paren is not omp_priv),
37431 then the sixth statement is a statement with the function call expression
37432 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37433 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37434 to initialize the OMP_PRIV artificial variable and there is seventh
37435 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37437 static bool
37438 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37440 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37441 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37442 type = TREE_TYPE (type);
37443 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37444 DECL_ARTIFICIAL (omp_out) = 1;
37445 pushdecl (omp_out);
37446 add_decl_expr (omp_out);
37447 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37448 DECL_ARTIFICIAL (omp_in) = 1;
37449 pushdecl (omp_in);
37450 add_decl_expr (omp_in);
37451 tree combiner;
37452 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37454 keep_next_level (true);
37455 tree block = begin_omp_structured_block ();
37456 combiner = cp_parser_expression (parser);
37457 finish_expr_stmt (combiner);
37458 block = finish_omp_structured_block (block);
37459 add_stmt (block);
37461 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37462 return false;
37464 const char *p = "";
37465 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37467 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37468 p = IDENTIFIER_POINTER (id);
37471 if (strcmp (p, "initializer") == 0)
37473 cp_lexer_consume_token (parser->lexer);
37474 matching_parens parens;
37475 if (!parens.require_open (parser))
37476 return false;
37478 p = "";
37479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37481 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37482 p = IDENTIFIER_POINTER (id);
37485 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37486 DECL_ARTIFICIAL (omp_priv) = 1;
37487 pushdecl (omp_priv);
37488 add_decl_expr (omp_priv);
37489 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37490 DECL_ARTIFICIAL (omp_orig) = 1;
37491 pushdecl (omp_orig);
37492 add_decl_expr (omp_orig);
37494 keep_next_level (true);
37495 block = begin_omp_structured_block ();
37497 bool ctor = false;
37498 if (strcmp (p, "omp_priv") == 0)
37500 bool is_direct_init, is_non_constant_init;
37501 ctor = true;
37502 cp_lexer_consume_token (parser->lexer);
37503 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37504 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37505 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37506 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37507 == CPP_CLOSE_PAREN
37508 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37509 == CPP_CLOSE_PAREN))
37511 finish_omp_structured_block (block);
37512 error ("invalid initializer clause");
37513 return false;
37515 initializer = cp_parser_initializer (parser, &is_direct_init,
37516 &is_non_constant_init);
37517 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37518 NULL_TREE, LOOKUP_ONLYCONVERTING);
37520 else
37522 cp_parser_parse_tentatively (parser);
37523 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37524 /*check_dependency_p=*/true,
37525 /*template_p=*/NULL,
37526 /*declarator_p=*/false,
37527 /*optional_p=*/false);
37528 vec<tree, va_gc> *args;
37529 if (fn_name == error_mark_node
37530 || cp_parser_error_occurred (parser)
37531 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37532 || ((args = cp_parser_parenthesized_expression_list
37533 (parser, non_attr, /*cast_p=*/false,
37534 /*allow_expansion_p=*/true,
37535 /*non_constant_p=*/NULL)),
37536 cp_parser_error_occurred (parser)))
37538 finish_omp_structured_block (block);
37539 cp_parser_abort_tentative_parse (parser);
37540 cp_parser_error (parser, "expected id-expression (arguments)");
37541 return false;
37543 unsigned int i;
37544 tree arg;
37545 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37546 if (arg == omp_priv
37547 || (TREE_CODE (arg) == ADDR_EXPR
37548 && TREE_OPERAND (arg, 0) == omp_priv))
37549 break;
37550 cp_parser_abort_tentative_parse (parser);
37551 if (arg == NULL_TREE)
37552 error ("one of the initializer call arguments should be %<omp_priv%>"
37553 " or %<&omp_priv%>");
37554 initializer = cp_parser_postfix_expression (parser, false, false, false,
37555 false, NULL);
37556 finish_expr_stmt (initializer);
37559 block = finish_omp_structured_block (block);
37560 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37561 add_stmt (block);
37563 if (ctor)
37564 add_decl_expr (omp_orig);
37566 if (!parens.require_close (parser))
37567 return false;
37570 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37571 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37572 UNKNOWN_LOCATION);
37574 return true;
37577 /* OpenMP 4.0
37578 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37579 initializer-clause[opt] new-line
37581 initializer-clause:
37582 initializer (omp_priv initializer)
37583 initializer (function-name (argument-list)) */
37585 static void
37586 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37587 enum pragma_context)
37589 auto_vec<tree> types;
37590 enum tree_code reduc_code = ERROR_MARK;
37591 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37592 unsigned int i;
37593 cp_token *first_token;
37594 cp_token_cache *cp;
37595 int errs;
37596 void *p;
37598 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37599 p = obstack_alloc (&declarator_obstack, 0);
37601 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37602 goto fail;
37604 switch (cp_lexer_peek_token (parser->lexer)->type)
37606 case CPP_PLUS:
37607 reduc_code = PLUS_EXPR;
37608 break;
37609 case CPP_MULT:
37610 reduc_code = MULT_EXPR;
37611 break;
37612 case CPP_MINUS:
37613 reduc_code = MINUS_EXPR;
37614 break;
37615 case CPP_AND:
37616 reduc_code = BIT_AND_EXPR;
37617 break;
37618 case CPP_XOR:
37619 reduc_code = BIT_XOR_EXPR;
37620 break;
37621 case CPP_OR:
37622 reduc_code = BIT_IOR_EXPR;
37623 break;
37624 case CPP_AND_AND:
37625 reduc_code = TRUTH_ANDIF_EXPR;
37626 break;
37627 case CPP_OR_OR:
37628 reduc_code = TRUTH_ORIF_EXPR;
37629 break;
37630 case CPP_NAME:
37631 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37632 break;
37633 default:
37634 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37635 "%<|%>, %<&&%>, %<||%> or identifier");
37636 goto fail;
37639 if (reduc_code != ERROR_MARK)
37640 cp_lexer_consume_token (parser->lexer);
37642 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37643 if (reduc_id == error_mark_node)
37644 goto fail;
37646 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37647 goto fail;
37649 /* Types may not be defined in declare reduction type list. */
37650 const char *saved_message;
37651 saved_message = parser->type_definition_forbidden_message;
37652 parser->type_definition_forbidden_message
37653 = G_("types may not be defined in declare reduction type list");
37654 bool saved_colon_corrects_to_scope_p;
37655 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37656 parser->colon_corrects_to_scope_p = false;
37657 bool saved_colon_doesnt_start_class_def_p;
37658 saved_colon_doesnt_start_class_def_p
37659 = parser->colon_doesnt_start_class_def_p;
37660 parser->colon_doesnt_start_class_def_p = true;
37662 while (true)
37664 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37665 type = cp_parser_type_id (parser);
37666 if (type == error_mark_node)
37668 else if (ARITHMETIC_TYPE_P (type)
37669 && (orig_reduc_id == NULL_TREE
37670 || (TREE_CODE (type) != COMPLEX_TYPE
37671 && (id_equal (orig_reduc_id, "min")
37672 || id_equal (orig_reduc_id, "max")))))
37673 error_at (loc, "predeclared arithmetic type %qT in "
37674 "%<#pragma omp declare reduction%>", type);
37675 else if (TREE_CODE (type) == FUNCTION_TYPE
37676 || TREE_CODE (type) == METHOD_TYPE
37677 || TREE_CODE (type) == ARRAY_TYPE)
37678 error_at (loc, "function or array type %qT in "
37679 "%<#pragma omp declare reduction%>", type);
37680 else if (TREE_CODE (type) == REFERENCE_TYPE)
37681 error_at (loc, "reference type %qT in "
37682 "%<#pragma omp declare reduction%>", type);
37683 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37684 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37685 "%<#pragma omp declare reduction%>", type);
37686 else
37687 types.safe_push (type);
37689 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37690 cp_lexer_consume_token (parser->lexer);
37691 else
37692 break;
37695 /* Restore the saved message. */
37696 parser->type_definition_forbidden_message = saved_message;
37697 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37698 parser->colon_doesnt_start_class_def_p
37699 = saved_colon_doesnt_start_class_def_p;
37701 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37702 || types.is_empty ())
37704 fail:
37705 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37706 goto done;
37709 first_token = cp_lexer_peek_token (parser->lexer);
37710 cp = NULL;
37711 errs = errorcount;
37712 FOR_EACH_VEC_ELT (types, i, type)
37714 tree fntype
37715 = build_function_type_list (void_type_node,
37716 cp_build_reference_type (type, false),
37717 NULL_TREE);
37718 tree this_reduc_id = reduc_id;
37719 if (!dependent_type_p (type))
37720 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37721 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37722 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37723 DECL_ARTIFICIAL (fndecl) = 1;
37724 DECL_EXTERNAL (fndecl) = 1;
37725 DECL_DECLARED_INLINE_P (fndecl) = 1;
37726 DECL_IGNORED_P (fndecl) = 1;
37727 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37728 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37729 DECL_ATTRIBUTES (fndecl)
37730 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37731 DECL_ATTRIBUTES (fndecl));
37732 if (processing_template_decl)
37733 fndecl = push_template_decl (fndecl);
37734 bool block_scope = false;
37735 tree block = NULL_TREE;
37736 if (current_function_decl)
37738 block_scope = true;
37739 DECL_CONTEXT (fndecl) = global_namespace;
37740 if (!processing_template_decl)
37741 pushdecl (fndecl);
37743 else if (current_class_type)
37745 if (cp == NULL)
37747 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37748 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37749 cp_lexer_consume_token (parser->lexer);
37750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37751 goto fail;
37752 cp = cp_token_cache_new (first_token,
37753 cp_lexer_peek_nth_token (parser->lexer,
37754 2));
37756 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37757 finish_member_declaration (fndecl);
37758 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37759 DECL_PENDING_INLINE_P (fndecl) = 1;
37760 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37761 continue;
37763 else
37765 DECL_CONTEXT (fndecl) = current_namespace;
37766 pushdecl (fndecl);
37768 if (!block_scope)
37769 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37770 else
37771 block = begin_omp_structured_block ();
37772 if (cp)
37774 cp_parser_push_lexer_for_tokens (parser, cp);
37775 parser->lexer->in_pragma = true;
37777 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37779 if (!block_scope)
37780 finish_function (/*inline_p=*/false);
37781 else
37782 DECL_CONTEXT (fndecl) = current_function_decl;
37783 if (cp)
37784 cp_parser_pop_lexer (parser);
37785 goto fail;
37787 if (cp)
37788 cp_parser_pop_lexer (parser);
37789 if (!block_scope)
37790 finish_function (/*inline_p=*/false);
37791 else
37793 DECL_CONTEXT (fndecl) = current_function_decl;
37794 block = finish_omp_structured_block (block);
37795 if (TREE_CODE (block) == BIND_EXPR)
37796 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37797 else if (TREE_CODE (block) == STATEMENT_LIST)
37798 DECL_SAVED_TREE (fndecl) = block;
37799 if (processing_template_decl)
37800 add_decl_expr (fndecl);
37802 cp_check_omp_declare_reduction (fndecl);
37803 if (cp == NULL && types.length () > 1)
37804 cp = cp_token_cache_new (first_token,
37805 cp_lexer_peek_nth_token (parser->lexer, 2));
37806 if (errs != errorcount)
37807 break;
37810 cp_parser_require_pragma_eol (parser, pragma_tok);
37812 done:
37813 /* Free any declarators allocated. */
37814 obstack_free (&declarator_obstack, p);
37817 /* OpenMP 4.0
37818 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37819 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37820 initializer-clause[opt] new-line
37821 #pragma omp declare target new-line */
37823 static bool
37824 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37825 enum pragma_context context)
37827 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37829 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37830 const char *p = IDENTIFIER_POINTER (id);
37832 if (strcmp (p, "simd") == 0)
37834 cp_lexer_consume_token (parser->lexer);
37835 cp_parser_omp_declare_simd (parser, pragma_tok,
37836 context);
37837 return true;
37839 cp_ensure_no_omp_declare_simd (parser);
37840 if (strcmp (p, "reduction") == 0)
37842 cp_lexer_consume_token (parser->lexer);
37843 cp_parser_omp_declare_reduction (parser, pragma_tok,
37844 context);
37845 return false;
37847 if (!flag_openmp) /* flag_openmp_simd */
37849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37850 return false;
37852 if (strcmp (p, "target") == 0)
37854 cp_lexer_consume_token (parser->lexer);
37855 cp_parser_omp_declare_target (parser, pragma_tok);
37856 return false;
37859 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37860 "or %<target%>");
37861 cp_parser_require_pragma_eol (parser, pragma_tok);
37862 return false;
37865 /* OpenMP 4.5:
37866 #pragma omp taskloop taskloop-clause[optseq] new-line
37867 for-loop
37869 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37870 for-loop */
37872 #define OMP_TASKLOOP_CLAUSE_MASK \
37873 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37888 static tree
37889 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37890 char *p_name, omp_clause_mask mask, tree *cclauses,
37891 bool *if_p)
37893 tree clauses, sb, ret;
37894 unsigned int save;
37895 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37897 strcat (p_name, " taskloop");
37898 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37900 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37902 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37903 const char *p = IDENTIFIER_POINTER (id);
37905 if (strcmp (p, "simd") == 0)
37907 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37908 if (cclauses == NULL)
37909 cclauses = cclauses_buf;
37911 cp_lexer_consume_token (parser->lexer);
37912 if (!flag_openmp) /* flag_openmp_simd */
37913 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37914 cclauses, if_p);
37915 sb = begin_omp_structured_block ();
37916 save = cp_parser_begin_omp_structured_block (parser);
37917 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37918 cclauses, if_p);
37919 cp_parser_end_omp_structured_block (parser, save);
37920 tree body = finish_omp_structured_block (sb);
37921 if (ret == NULL)
37922 return ret;
37923 ret = make_node (OMP_TASKLOOP);
37924 TREE_TYPE (ret) = void_type_node;
37925 OMP_FOR_BODY (ret) = body;
37926 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37927 SET_EXPR_LOCATION (ret, loc);
37928 add_stmt (ret);
37929 return ret;
37932 if (!flag_openmp) /* flag_openmp_simd */
37934 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37935 return NULL_TREE;
37938 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37939 cclauses == NULL);
37940 if (cclauses)
37942 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37943 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37946 sb = begin_omp_structured_block ();
37947 save = cp_parser_begin_omp_structured_block (parser);
37949 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37950 if_p);
37952 cp_parser_end_omp_structured_block (parser, save);
37953 add_stmt (finish_omp_structured_block (sb));
37955 return ret;
37959 /* OpenACC 2.0:
37960 # pragma acc routine oacc-routine-clause[optseq] new-line
37961 function-definition
37963 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37966 #define OACC_ROUTINE_CLAUSE_MASK \
37967 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37973 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37974 component, which must resolve to a declared namespace-scope
37975 function. The clauses are either processed directly (for a named
37976 function), or defered until the immediatley following declaration
37977 is parsed. */
37979 static void
37980 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37981 enum pragma_context context)
37983 gcc_checking_assert (context == pragma_external);
37984 /* The checking for "another pragma following this one" in the "no optional
37985 '( name )'" case makes sure that we dont re-enter. */
37986 gcc_checking_assert (parser->oacc_routine == NULL);
37988 cp_oacc_routine_data data;
37989 data.error_seen = false;
37990 data.fndecl_seen = false;
37991 data.tokens = vNULL;
37992 data.clauses = NULL_TREE;
37993 data.loc = pragma_tok->location;
37994 /* It is safe to take the address of a local variable; it will only be
37995 used while this scope is live. */
37996 parser->oacc_routine = &data;
37998 /* Look for optional '( name )'. */
37999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38001 matching_parens parens;
38002 parens.consume_open (parser); /* '(' */
38004 /* We parse the name as an id-expression. If it resolves to
38005 anything other than a non-overloaded function at namespace
38006 scope, it's an error. */
38007 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38008 tree name = cp_parser_id_expression (parser,
38009 /*template_keyword_p=*/false,
38010 /*check_dependency_p=*/false,
38011 /*template_p=*/NULL,
38012 /*declarator_p=*/false,
38013 /*optional_p=*/false);
38014 tree decl = (identifier_p (name)
38015 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38016 : name);
38017 if (name != error_mark_node && decl == error_mark_node)
38018 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38020 if (decl == error_mark_node
38021 || !parens.require_close (parser))
38023 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38024 parser->oacc_routine = NULL;
38025 return;
38028 data.clauses
38029 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38030 "#pragma acc routine",
38031 cp_lexer_peek_token (parser->lexer));
38033 if (decl && is_overloaded_fn (decl)
38034 && (TREE_CODE (decl) != FUNCTION_DECL
38035 || DECL_FUNCTION_TEMPLATE_P (decl)))
38037 error_at (name_loc,
38038 "%<#pragma acc routine%> names a set of overloads");
38039 parser->oacc_routine = NULL;
38040 return;
38043 /* Perhaps we should use the same rule as declarations in different
38044 namespaces? */
38045 if (!DECL_NAMESPACE_SCOPE_P (decl))
38047 error_at (name_loc,
38048 "%qD does not refer to a namespace scope function", decl);
38049 parser->oacc_routine = NULL;
38050 return;
38053 if (TREE_CODE (decl) != FUNCTION_DECL)
38055 error_at (name_loc, "%qD does not refer to a function", decl);
38056 parser->oacc_routine = NULL;
38057 return;
38060 cp_finalize_oacc_routine (parser, decl, false);
38061 parser->oacc_routine = NULL;
38063 else /* No optional '( name )'. */
38065 /* Store away all pragma tokens. */
38066 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38067 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38068 cp_lexer_consume_token (parser->lexer);
38069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38070 parser->oacc_routine->error_seen = true;
38071 cp_parser_require_pragma_eol (parser, pragma_tok);
38072 struct cp_token_cache *cp
38073 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38074 parser->oacc_routine->tokens.safe_push (cp);
38076 /* Emit a helpful diagnostic if there's another pragma following this
38077 one. */
38078 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38080 cp_ensure_no_oacc_routine (parser);
38081 data.tokens.release ();
38082 /* ..., and then just keep going. */
38083 return;
38086 /* We only have to consider the pragma_external case here. */
38087 cp_parser_declaration (parser);
38088 if (parser->oacc_routine
38089 && !parser->oacc_routine->fndecl_seen)
38090 cp_ensure_no_oacc_routine (parser);
38091 else
38092 parser->oacc_routine = NULL;
38093 data.tokens.release ();
38097 /* Finalize #pragma acc routine clauses after direct declarator has
38098 been parsed. */
38100 static tree
38101 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38103 struct cp_token_cache *ce;
38104 cp_oacc_routine_data *data = parser->oacc_routine;
38106 if (!data->error_seen && data->fndecl_seen)
38108 error_at (data->loc,
38109 "%<#pragma acc routine%> not immediately followed by "
38110 "a single function declaration or definition");
38111 data->error_seen = true;
38113 if (data->error_seen)
38114 return attrs;
38116 gcc_checking_assert (data->tokens.length () == 1);
38117 ce = data->tokens[0];
38119 cp_parser_push_lexer_for_tokens (parser, ce);
38120 parser->lexer->in_pragma = true;
38121 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38123 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38124 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38125 parser->oacc_routine->clauses
38126 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38127 "#pragma acc routine", pragma_tok);
38128 cp_parser_pop_lexer (parser);
38129 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38130 fndecl_seen. */
38132 return attrs;
38135 /* Apply any saved OpenACC routine clauses to a just-parsed
38136 declaration. */
38138 static void
38139 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38141 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38143 /* Keep going if we're in error reporting mode. */
38144 if (parser->oacc_routine->error_seen
38145 || fndecl == error_mark_node)
38146 return;
38148 if (parser->oacc_routine->fndecl_seen)
38150 error_at (parser->oacc_routine->loc,
38151 "%<#pragma acc routine%> not immediately followed by"
38152 " a single function declaration or definition");
38153 parser->oacc_routine = NULL;
38154 return;
38156 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38158 cp_ensure_no_oacc_routine (parser);
38159 return;
38162 if (oacc_get_fn_attrib (fndecl))
38164 error_at (parser->oacc_routine->loc,
38165 "%<#pragma acc routine%> already applied to %qD", fndecl);
38166 parser->oacc_routine = NULL;
38167 return;
38170 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38172 error_at (parser->oacc_routine->loc,
38173 TREE_USED (fndecl)
38174 ? G_("%<#pragma acc routine%> must be applied before use")
38175 : G_("%<#pragma acc routine%> must be applied before "
38176 "definition"));
38177 parser->oacc_routine = NULL;
38178 return;
38181 /* Process the routine's dimension clauses. */
38182 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38183 oacc_replace_fn_attrib (fndecl, dims);
38185 /* Add an "omp declare target" attribute. */
38186 DECL_ATTRIBUTES (fndecl)
38187 = tree_cons (get_identifier ("omp declare target"),
38188 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38190 /* Don't unset parser->oacc_routine here: we may still need it to
38191 diagnose wrong usage. But, remember that we've used this "#pragma acc
38192 routine". */
38193 parser->oacc_routine->fndecl_seen = true;
38197 /* Main entry point to OpenMP statement pragmas. */
38199 static void
38200 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38202 tree stmt;
38203 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38204 omp_clause_mask mask (0);
38206 switch (cp_parser_pragma_kind (pragma_tok))
38208 case PRAGMA_OACC_ATOMIC:
38209 cp_parser_omp_atomic (parser, pragma_tok);
38210 return;
38211 case PRAGMA_OACC_CACHE:
38212 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38213 break;
38214 case PRAGMA_OACC_DATA:
38215 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38216 break;
38217 case PRAGMA_OACC_ENTER_DATA:
38218 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38219 break;
38220 case PRAGMA_OACC_EXIT_DATA:
38221 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38222 break;
38223 case PRAGMA_OACC_HOST_DATA:
38224 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38225 break;
38226 case PRAGMA_OACC_KERNELS:
38227 case PRAGMA_OACC_PARALLEL:
38228 strcpy (p_name, "#pragma acc");
38229 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38230 if_p);
38231 break;
38232 case PRAGMA_OACC_LOOP:
38233 strcpy (p_name, "#pragma acc");
38234 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38235 if_p);
38236 break;
38237 case PRAGMA_OACC_UPDATE:
38238 stmt = cp_parser_oacc_update (parser, pragma_tok);
38239 break;
38240 case PRAGMA_OACC_WAIT:
38241 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38242 break;
38243 case PRAGMA_OMP_ATOMIC:
38244 cp_parser_omp_atomic (parser, pragma_tok);
38245 return;
38246 case PRAGMA_OMP_CRITICAL:
38247 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38248 break;
38249 case PRAGMA_OMP_DISTRIBUTE:
38250 strcpy (p_name, "#pragma omp");
38251 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38252 if_p);
38253 break;
38254 case PRAGMA_OMP_FOR:
38255 strcpy (p_name, "#pragma omp");
38256 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38257 if_p);
38258 break;
38259 case PRAGMA_OMP_MASTER:
38260 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38261 break;
38262 case PRAGMA_OMP_PARALLEL:
38263 strcpy (p_name, "#pragma omp");
38264 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38265 if_p);
38266 break;
38267 case PRAGMA_OMP_SECTIONS:
38268 strcpy (p_name, "#pragma omp");
38269 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38270 break;
38271 case PRAGMA_OMP_SIMD:
38272 strcpy (p_name, "#pragma omp");
38273 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38274 if_p);
38275 break;
38276 case PRAGMA_OMP_SINGLE:
38277 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38278 break;
38279 case PRAGMA_OMP_TASK:
38280 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38281 break;
38282 case PRAGMA_OMP_TASKGROUP:
38283 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38284 break;
38285 case PRAGMA_OMP_TASKLOOP:
38286 strcpy (p_name, "#pragma omp");
38287 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38288 if_p);
38289 break;
38290 case PRAGMA_OMP_TEAMS:
38291 strcpy (p_name, "#pragma omp");
38292 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38293 if_p);
38294 break;
38295 default:
38296 gcc_unreachable ();
38299 protected_set_expr_location (stmt, pragma_tok->location);
38302 /* Transactional Memory parsing routines. */
38304 /* Parse a transaction attribute.
38306 txn-attribute:
38307 attribute
38308 [ [ identifier ] ]
38310 We use this instead of cp_parser_attributes_opt for transactions to avoid
38311 the pedwarn in C++98 mode. */
38313 static tree
38314 cp_parser_txn_attribute_opt (cp_parser *parser)
38316 cp_token *token;
38317 tree attr_name, attr = NULL;
38319 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38320 return cp_parser_attributes_opt (parser);
38322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38323 return NULL_TREE;
38324 cp_lexer_consume_token (parser->lexer);
38325 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38326 goto error1;
38328 token = cp_lexer_peek_token (parser->lexer);
38329 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38331 token = cp_lexer_consume_token (parser->lexer);
38333 attr_name = (token->type == CPP_KEYWORD
38334 /* For keywords, use the canonical spelling,
38335 not the parsed identifier. */
38336 ? ridpointers[(int) token->keyword]
38337 : token->u.value);
38338 attr = build_tree_list (attr_name, NULL_TREE);
38340 else
38341 cp_parser_error (parser, "expected identifier");
38343 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38344 error1:
38345 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38346 return attr;
38349 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38351 transaction-statement:
38352 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38353 compound-statement
38354 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38357 static tree
38358 cp_parser_transaction (cp_parser *parser, cp_token *token)
38360 unsigned char old_in = parser->in_transaction;
38361 unsigned char this_in = 1, new_in;
38362 enum rid keyword = token->keyword;
38363 tree stmt, attrs, noex;
38365 cp_lexer_consume_token (parser->lexer);
38367 if (keyword == RID_TRANSACTION_RELAXED
38368 || keyword == RID_SYNCHRONIZED)
38369 this_in |= TM_STMT_ATTR_RELAXED;
38370 else
38372 attrs = cp_parser_txn_attribute_opt (parser);
38373 if (attrs)
38374 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38377 /* Parse a noexcept specification. */
38378 if (keyword == RID_ATOMIC_NOEXCEPT)
38379 noex = boolean_true_node;
38380 else if (keyword == RID_ATOMIC_CANCEL)
38382 /* cancel-and-throw is unimplemented. */
38383 sorry ("atomic_cancel");
38384 noex = NULL_TREE;
38386 else
38387 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38389 /* Keep track if we're in the lexical scope of an outer transaction. */
38390 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38392 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38394 parser->in_transaction = new_in;
38395 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38396 parser->in_transaction = old_in;
38398 finish_transaction_stmt (stmt, NULL, this_in, noex);
38400 return stmt;
38403 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38405 transaction-expression:
38406 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38407 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38410 static tree
38411 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38413 unsigned char old_in = parser->in_transaction;
38414 unsigned char this_in = 1;
38415 cp_token *token;
38416 tree expr, noex;
38417 bool noex_expr;
38418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38420 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38421 || keyword == RID_TRANSACTION_RELAXED);
38423 if (!flag_tm)
38424 error_at (loc,
38425 keyword == RID_TRANSACTION_RELAXED
38426 ? G_("%<__transaction_relaxed%> without transactional memory "
38427 "support enabled")
38428 : G_("%<__transaction_atomic%> without transactional memory "
38429 "support enabled"));
38431 token = cp_parser_require_keyword (parser, keyword,
38432 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38433 : RT_TRANSACTION_RELAXED));
38434 gcc_assert (token != NULL);
38436 if (keyword == RID_TRANSACTION_RELAXED)
38437 this_in |= TM_STMT_ATTR_RELAXED;
38439 /* Set this early. This might mean that we allow transaction_cancel in
38440 an expression that we find out later actually has to be a constexpr.
38441 However, we expect that cxx_constant_value will be able to deal with
38442 this; also, if the noexcept has no constexpr, then what we parse next
38443 really is a transaction's body. */
38444 parser->in_transaction = this_in;
38446 /* Parse a noexcept specification. */
38447 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38448 true);
38450 if (!noex || !noex_expr
38451 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38453 matching_parens parens;
38454 parens.require_open (parser);
38456 expr = cp_parser_expression (parser);
38457 expr = finish_parenthesized_expr (expr);
38459 parens.require_close (parser);
38461 else
38463 /* The only expression that is available got parsed for the noexcept
38464 already. noexcept is true then. */
38465 expr = noex;
38466 noex = boolean_true_node;
38469 expr = build_transaction_expr (token->location, expr, this_in, noex);
38470 parser->in_transaction = old_in;
38472 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38473 return error_mark_node;
38475 return (flag_tm ? expr : error_mark_node);
38478 /* Parse a function-transaction-block.
38480 function-transaction-block:
38481 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38482 function-body
38483 __transaction_atomic txn-attribute[opt] function-try-block
38484 __transaction_relaxed ctor-initializer[opt] function-body
38485 __transaction_relaxed function-try-block
38488 static void
38489 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38491 unsigned char old_in = parser->in_transaction;
38492 unsigned char new_in = 1;
38493 tree compound_stmt, stmt, attrs;
38494 cp_token *token;
38496 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38497 || keyword == RID_TRANSACTION_RELAXED);
38498 token = cp_parser_require_keyword (parser, keyword,
38499 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38500 : RT_TRANSACTION_RELAXED));
38501 gcc_assert (token != NULL);
38503 if (keyword == RID_TRANSACTION_RELAXED)
38504 new_in |= TM_STMT_ATTR_RELAXED;
38505 else
38507 attrs = cp_parser_txn_attribute_opt (parser);
38508 if (attrs)
38509 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38512 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38514 parser->in_transaction = new_in;
38516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38517 cp_parser_function_try_block (parser);
38518 else
38519 cp_parser_ctor_initializer_opt_and_function_body
38520 (parser, /*in_function_try_block=*/false);
38522 parser->in_transaction = old_in;
38524 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38527 /* Parse a __transaction_cancel statement.
38529 cancel-statement:
38530 __transaction_cancel txn-attribute[opt] ;
38531 __transaction_cancel txn-attribute[opt] throw-expression ;
38533 ??? Cancel and throw is not yet implemented. */
38535 static tree
38536 cp_parser_transaction_cancel (cp_parser *parser)
38538 cp_token *token;
38539 bool is_outer = false;
38540 tree stmt, attrs;
38542 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38543 RT_TRANSACTION_CANCEL);
38544 gcc_assert (token != NULL);
38546 attrs = cp_parser_txn_attribute_opt (parser);
38547 if (attrs)
38548 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38550 /* ??? Parse cancel-and-throw here. */
38552 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38554 if (!flag_tm)
38556 error_at (token->location, "%<__transaction_cancel%> without "
38557 "transactional memory support enabled");
38558 return error_mark_node;
38560 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38562 error_at (token->location, "%<__transaction_cancel%> within a "
38563 "%<__transaction_relaxed%>");
38564 return error_mark_node;
38566 else if (is_outer)
38568 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38569 && !is_tm_may_cancel_outer (current_function_decl))
38571 error_at (token->location, "outer %<__transaction_cancel%> not "
38572 "within outer %<__transaction_atomic%>");
38573 error_at (token->location,
38574 " or a %<transaction_may_cancel_outer%> function");
38575 return error_mark_node;
38578 else if (parser->in_transaction == 0)
38580 error_at (token->location, "%<__transaction_cancel%> not within "
38581 "%<__transaction_atomic%>");
38582 return error_mark_node;
38585 stmt = build_tm_abort_call (token->location, is_outer);
38586 add_stmt (stmt);
38588 return stmt;
38591 /* The parser. */
38593 static GTY (()) cp_parser *the_parser;
38596 /* Special handling for the first token or line in the file. The first
38597 thing in the file might be #pragma GCC pch_preprocess, which loads a
38598 PCH file, which is a GC collection point. So we need to handle this
38599 first pragma without benefit of an existing lexer structure.
38601 Always returns one token to the caller in *FIRST_TOKEN. This is
38602 either the true first token of the file, or the first token after
38603 the initial pragma. */
38605 static void
38606 cp_parser_initial_pragma (cp_token *first_token)
38608 tree name = NULL;
38610 cp_lexer_get_preprocessor_token (NULL, first_token);
38611 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38612 return;
38614 cp_lexer_get_preprocessor_token (NULL, first_token);
38615 if (first_token->type == CPP_STRING)
38617 name = first_token->u.value;
38619 cp_lexer_get_preprocessor_token (NULL, first_token);
38620 if (first_token->type != CPP_PRAGMA_EOL)
38621 error_at (first_token->location,
38622 "junk at end of %<#pragma GCC pch_preprocess%>");
38624 else
38625 error_at (first_token->location, "expected string literal");
38627 /* Skip to the end of the pragma. */
38628 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38629 cp_lexer_get_preprocessor_token (NULL, first_token);
38631 /* Now actually load the PCH file. */
38632 if (name)
38633 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38635 /* Read one more token to return to our caller. We have to do this
38636 after reading the PCH file in, since its pointers have to be
38637 live. */
38638 cp_lexer_get_preprocessor_token (NULL, first_token);
38641 /* Parse a pragma GCC ivdep. */
38643 static bool
38644 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38646 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38647 return true;
38650 /* Parse a pragma GCC unroll. */
38652 static unsigned short
38653 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38655 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38656 tree expr = cp_parser_constant_expression (parser);
38657 unsigned short unroll;
38658 expr = maybe_constant_value (expr);
38659 HOST_WIDE_INT lunroll = 0;
38660 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38661 || TREE_CODE (expr) != INTEGER_CST
38662 || (lunroll = tree_to_shwi (expr)) < 0
38663 || lunroll >= USHRT_MAX)
38665 error_at (location, "%<#pragma GCC unroll%> requires an"
38666 " assignment-expression that evaluates to a non-negative"
38667 " integral constant less than %u", USHRT_MAX);
38668 unroll = 0;
38670 else
38672 unroll = (unsigned short)lunroll;
38673 if (unroll == 0)
38674 unroll = 1;
38676 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38677 return unroll;
38680 /* Normal parsing of a pragma token. Here we can (and must) use the
38681 regular lexer. */
38683 static bool
38684 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38686 cp_token *pragma_tok;
38687 unsigned int id;
38688 tree stmt;
38689 bool ret;
38691 pragma_tok = cp_lexer_consume_token (parser->lexer);
38692 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38693 parser->lexer->in_pragma = true;
38695 id = cp_parser_pragma_kind (pragma_tok);
38696 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38697 cp_ensure_no_omp_declare_simd (parser);
38698 switch (id)
38700 case PRAGMA_GCC_PCH_PREPROCESS:
38701 error_at (pragma_tok->location,
38702 "%<#pragma GCC pch_preprocess%> must be first");
38703 break;
38705 case PRAGMA_OMP_BARRIER:
38706 switch (context)
38708 case pragma_compound:
38709 cp_parser_omp_barrier (parser, pragma_tok);
38710 return false;
38711 case pragma_stmt:
38712 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38713 "used in compound statements", "omp barrier");
38714 break;
38715 default:
38716 goto bad_stmt;
38718 break;
38720 case PRAGMA_OMP_FLUSH:
38721 switch (context)
38723 case pragma_compound:
38724 cp_parser_omp_flush (parser, pragma_tok);
38725 return false;
38726 case pragma_stmt:
38727 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38728 "used in compound statements", "omp flush");
38729 break;
38730 default:
38731 goto bad_stmt;
38733 break;
38735 case PRAGMA_OMP_TASKWAIT:
38736 switch (context)
38738 case pragma_compound:
38739 cp_parser_omp_taskwait (parser, pragma_tok);
38740 return false;
38741 case pragma_stmt:
38742 error_at (pragma_tok->location,
38743 "%<#pragma %s%> may only be used in compound statements",
38744 "omp taskwait");
38745 break;
38746 default:
38747 goto bad_stmt;
38749 break;
38751 case PRAGMA_OMP_TASKYIELD:
38752 switch (context)
38754 case pragma_compound:
38755 cp_parser_omp_taskyield (parser, pragma_tok);
38756 return false;
38757 case pragma_stmt:
38758 error_at (pragma_tok->location,
38759 "%<#pragma %s%> may only be used in compound statements",
38760 "omp taskyield");
38761 break;
38762 default:
38763 goto bad_stmt;
38765 break;
38767 case PRAGMA_OMP_CANCEL:
38768 switch (context)
38770 case pragma_compound:
38771 cp_parser_omp_cancel (parser, pragma_tok);
38772 return false;
38773 case pragma_stmt:
38774 error_at (pragma_tok->location,
38775 "%<#pragma %s%> may only be used in compound statements",
38776 "omp cancel");
38777 break;
38778 default:
38779 goto bad_stmt;
38781 break;
38783 case PRAGMA_OMP_CANCELLATION_POINT:
38784 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38785 return false;
38787 case PRAGMA_OMP_THREADPRIVATE:
38788 cp_parser_omp_threadprivate (parser, pragma_tok);
38789 return false;
38791 case PRAGMA_OMP_DECLARE:
38792 return cp_parser_omp_declare (parser, pragma_tok, context);
38794 case PRAGMA_OACC_DECLARE:
38795 cp_parser_oacc_declare (parser, pragma_tok);
38796 return false;
38798 case PRAGMA_OACC_ENTER_DATA:
38799 if (context == pragma_stmt)
38801 error_at (pragma_tok->location,
38802 "%<#pragma %s%> may only be used in compound statements",
38803 "acc enter data");
38804 break;
38806 else if (context != pragma_compound)
38807 goto bad_stmt;
38808 cp_parser_omp_construct (parser, pragma_tok, if_p);
38809 return true;
38811 case PRAGMA_OACC_EXIT_DATA:
38812 if (context == pragma_stmt)
38814 error_at (pragma_tok->location,
38815 "%<#pragma %s%> may only be used in compound statements",
38816 "acc exit data");
38817 break;
38819 else if (context != pragma_compound)
38820 goto bad_stmt;
38821 cp_parser_omp_construct (parser, pragma_tok, if_p);
38822 return true;
38824 case PRAGMA_OACC_ROUTINE:
38825 if (context != pragma_external)
38827 error_at (pragma_tok->location,
38828 "%<#pragma acc routine%> must be at file scope");
38829 break;
38831 cp_parser_oacc_routine (parser, pragma_tok, context);
38832 return false;
38834 case PRAGMA_OACC_UPDATE:
38835 if (context == pragma_stmt)
38837 error_at (pragma_tok->location,
38838 "%<#pragma %s%> may only be used in compound statements",
38839 "acc update");
38840 break;
38842 else if (context != pragma_compound)
38843 goto bad_stmt;
38844 cp_parser_omp_construct (parser, pragma_tok, if_p);
38845 return true;
38847 case PRAGMA_OACC_WAIT:
38848 if (context == pragma_stmt)
38850 error_at (pragma_tok->location,
38851 "%<#pragma %s%> may only be used in compound statements",
38852 "acc wait");
38853 break;
38855 else if (context != pragma_compound)
38856 goto bad_stmt;
38857 cp_parser_omp_construct (parser, pragma_tok, if_p);
38858 return true;
38860 case PRAGMA_OACC_ATOMIC:
38861 case PRAGMA_OACC_CACHE:
38862 case PRAGMA_OACC_DATA:
38863 case PRAGMA_OACC_HOST_DATA:
38864 case PRAGMA_OACC_KERNELS:
38865 case PRAGMA_OACC_PARALLEL:
38866 case PRAGMA_OACC_LOOP:
38867 case PRAGMA_OMP_ATOMIC:
38868 case PRAGMA_OMP_CRITICAL:
38869 case PRAGMA_OMP_DISTRIBUTE:
38870 case PRAGMA_OMP_FOR:
38871 case PRAGMA_OMP_MASTER:
38872 case PRAGMA_OMP_PARALLEL:
38873 case PRAGMA_OMP_SECTIONS:
38874 case PRAGMA_OMP_SIMD:
38875 case PRAGMA_OMP_SINGLE:
38876 case PRAGMA_OMP_TASK:
38877 case PRAGMA_OMP_TASKGROUP:
38878 case PRAGMA_OMP_TASKLOOP:
38879 case PRAGMA_OMP_TEAMS:
38880 if (context != pragma_stmt && context != pragma_compound)
38881 goto bad_stmt;
38882 stmt = push_omp_privatization_clauses (false);
38883 cp_parser_omp_construct (parser, pragma_tok, if_p);
38884 pop_omp_privatization_clauses (stmt);
38885 return true;
38887 case PRAGMA_OMP_ORDERED:
38888 if (context != pragma_stmt && context != pragma_compound)
38889 goto bad_stmt;
38890 stmt = push_omp_privatization_clauses (false);
38891 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38892 pop_omp_privatization_clauses (stmt);
38893 return ret;
38895 case PRAGMA_OMP_TARGET:
38896 if (context != pragma_stmt && context != pragma_compound)
38897 goto bad_stmt;
38898 stmt = push_omp_privatization_clauses (false);
38899 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38900 pop_omp_privatization_clauses (stmt);
38901 return ret;
38903 case PRAGMA_OMP_END_DECLARE_TARGET:
38904 cp_parser_omp_end_declare_target (parser, pragma_tok);
38905 return false;
38907 case PRAGMA_OMP_SECTION:
38908 error_at (pragma_tok->location,
38909 "%<#pragma omp section%> may only be used in "
38910 "%<#pragma omp sections%> construct");
38911 break;
38913 case PRAGMA_IVDEP:
38915 if (context == pragma_external)
38917 error_at (pragma_tok->location,
38918 "%<#pragma GCC ivdep%> must be inside a function");
38919 break;
38921 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38922 unsigned short unroll;
38923 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38924 if (tok->type == CPP_PRAGMA
38925 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38927 tok = cp_lexer_consume_token (parser->lexer);
38928 unroll = cp_parser_pragma_unroll (parser, tok);
38929 tok = cp_lexer_peek_token (the_parser->lexer);
38931 else
38932 unroll = 0;
38933 if (tok->type != CPP_KEYWORD
38934 || (tok->keyword != RID_FOR
38935 && tok->keyword != RID_WHILE
38936 && tok->keyword != RID_DO))
38938 cp_parser_error (parser, "for, while or do statement expected");
38939 return false;
38941 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38942 return true;
38945 case PRAGMA_UNROLL:
38947 if (context == pragma_external)
38949 error_at (pragma_tok->location,
38950 "%<#pragma GCC unroll%> must be inside a function");
38951 break;
38953 const unsigned short unroll
38954 = cp_parser_pragma_unroll (parser, pragma_tok);
38955 bool ivdep;
38956 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38957 if (tok->type == CPP_PRAGMA
38958 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38960 tok = cp_lexer_consume_token (parser->lexer);
38961 ivdep = cp_parser_pragma_ivdep (parser, tok);
38962 tok = cp_lexer_peek_token (the_parser->lexer);
38964 else
38965 ivdep = false;
38966 if (tok->type != CPP_KEYWORD
38967 || (tok->keyword != RID_FOR
38968 && tok->keyword != RID_WHILE
38969 && tok->keyword != RID_DO))
38971 cp_parser_error (parser, "for, while or do statement expected");
38972 return false;
38974 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38975 return true;
38978 default:
38979 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38980 c_invoke_pragma_handler (id);
38981 break;
38983 bad_stmt:
38984 cp_parser_error (parser, "expected declaration specifiers");
38985 break;
38988 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38989 return false;
38992 /* The interface the pragma parsers have to the lexer. */
38994 enum cpp_ttype
38995 pragma_lex (tree *value, location_t *loc)
38997 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38998 enum cpp_ttype ret = tok->type;
39000 *value = tok->u.value;
39001 if (loc)
39002 *loc = tok->location;
39004 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39005 ret = CPP_EOF;
39006 else if (ret == CPP_STRING)
39007 *value = cp_parser_string_literal (the_parser, false, false);
39008 else
39010 if (ret == CPP_KEYWORD)
39011 ret = CPP_NAME;
39012 cp_lexer_consume_token (the_parser->lexer);
39015 return ret;
39019 /* External interface. */
39021 /* Parse one entire translation unit. */
39023 void
39024 c_parse_file (void)
39026 static bool already_called = false;
39028 if (already_called)
39029 fatal_error (input_location,
39030 "inter-module optimizations not implemented for C++");
39031 already_called = true;
39033 the_parser = cp_parser_new ();
39034 push_deferring_access_checks (flag_access_control
39035 ? dk_no_deferred : dk_no_check);
39036 cp_parser_translation_unit (the_parser);
39037 the_parser = NULL;
39040 /* Create an identifier for a generic parameter type (a synthesized
39041 template parameter implied by `auto' or a concept identifier). */
39043 static GTY(()) int generic_parm_count;
39044 static tree
39045 make_generic_type_name ()
39047 char buf[32];
39048 sprintf (buf, "auto:%d", ++generic_parm_count);
39049 return get_identifier (buf);
39052 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39053 (creating a new template parameter list if necessary). Returns the newly
39054 created template type parm. */
39056 static tree
39057 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39059 gcc_assert (current_binding_level->kind == sk_function_parms);
39061 /* Before committing to modifying any scope, if we're in an
39062 implicit template scope, and we're trying to synthesize a
39063 constrained parameter, try to find a previous parameter with
39064 the same name. This is the same-type rule for abbreviated
39065 function templates.
39067 NOTE: We can generate implicit parameters when tentatively
39068 parsing a nested name specifier, only to reject that parse
39069 later. However, matching the same template-id as part of a
39070 direct-declarator should generate an identical template
39071 parameter, so this rule will merge them. */
39072 if (parser->implicit_template_scope && constr)
39074 tree t = parser->implicit_template_parms;
39075 while (t)
39077 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39079 tree d = TREE_VALUE (t);
39080 if (TREE_CODE (d) == PARM_DECL)
39081 /* Return the TEMPLATE_PARM_INDEX. */
39082 d = DECL_INITIAL (d);
39083 return d;
39085 t = TREE_CHAIN (t);
39089 /* We are either continuing a function template that already contains implicit
39090 template parameters, creating a new fully-implicit function template, or
39091 extending an existing explicit function template with implicit template
39092 parameters. */
39094 cp_binding_level *const entry_scope = current_binding_level;
39096 bool become_template = false;
39097 cp_binding_level *parent_scope = 0;
39099 if (parser->implicit_template_scope)
39101 gcc_assert (parser->implicit_template_parms);
39103 current_binding_level = parser->implicit_template_scope;
39105 else
39107 /* Roll back to the existing template parameter scope (in the case of
39108 extending an explicit function template) or introduce a new template
39109 parameter scope ahead of the function parameter scope (or class scope
39110 in the case of out-of-line member definitions). The function scope is
39111 added back after template parameter synthesis below. */
39113 cp_binding_level *scope = entry_scope;
39115 while (scope->kind == sk_function_parms)
39117 parent_scope = scope;
39118 scope = scope->level_chain;
39120 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39122 /* If not defining a class, then any class scope is a scope level in
39123 an out-of-line member definition. In this case simply wind back
39124 beyond the first such scope to inject the template parameter list.
39125 Otherwise wind back to the class being defined. The latter can
39126 occur in class member friend declarations such as:
39128 class A {
39129 void foo (auto);
39131 class B {
39132 friend void A::foo (auto);
39135 The template parameter list synthesized for the friend declaration
39136 must be injected in the scope of 'B'. This can also occur in
39137 erroneous cases such as:
39139 struct A {
39140 struct B {
39141 void foo (auto);
39143 void B::foo (auto) {}
39146 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39147 but, nevertheless, the template parameter list synthesized for the
39148 declarator should be injected into the scope of 'A' as if the
39149 ill-formed template was specified explicitly. */
39151 while (scope->kind == sk_class && !scope->defining_class_p)
39153 parent_scope = scope;
39154 scope = scope->level_chain;
39158 current_binding_level = scope;
39160 if (scope->kind != sk_template_parms
39161 || !function_being_declared_is_template_p (parser))
39163 /* Introduce a new template parameter list for implicit template
39164 parameters. */
39166 become_template = true;
39168 parser->implicit_template_scope
39169 = begin_scope (sk_template_parms, NULL);
39171 ++processing_template_decl;
39173 parser->fully_implicit_function_template_p = true;
39174 ++parser->num_template_parameter_lists;
39176 else
39178 /* Synthesize implicit template parameters at the end of the explicit
39179 template parameter list. */
39181 gcc_assert (current_template_parms);
39183 parser->implicit_template_scope = scope;
39185 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39186 parser->implicit_template_parms
39187 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39191 /* Synthesize a new template parameter and track the current template
39192 parameter chain with implicit_template_parms. */
39194 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39195 tree synth_id = make_generic_type_name ();
39196 tree synth_tmpl_parm;
39197 bool non_type = false;
39199 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39200 synth_tmpl_parm
39201 = finish_template_type_parm (class_type_node, synth_id);
39202 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39203 synth_tmpl_parm
39204 = finish_constrained_template_template_parm (proto, synth_id);
39205 else
39207 synth_tmpl_parm = copy_decl (proto);
39208 DECL_NAME (synth_tmpl_parm) = synth_id;
39209 non_type = true;
39212 // Attach the constraint to the parm before processing.
39213 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39214 TREE_TYPE (node) = constr;
39215 tree new_parm
39216 = process_template_parm (parser->implicit_template_parms,
39217 input_location,
39218 node,
39219 /*non_type=*/non_type,
39220 /*param_pack=*/false);
39222 // Chain the new parameter to the list of implicit parameters.
39223 if (parser->implicit_template_parms)
39224 parser->implicit_template_parms
39225 = TREE_CHAIN (parser->implicit_template_parms);
39226 else
39227 parser->implicit_template_parms = new_parm;
39229 tree new_decl = get_local_decls ();
39230 if (non_type)
39231 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39232 new_decl = DECL_INITIAL (new_decl);
39234 /* If creating a fully implicit function template, start the new implicit
39235 template parameter list with this synthesized type, otherwise grow the
39236 current template parameter list. */
39238 if (become_template)
39240 parent_scope->level_chain = current_binding_level;
39242 tree new_parms = make_tree_vec (1);
39243 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39244 current_template_parms = tree_cons (size_int (processing_template_decl),
39245 new_parms, current_template_parms);
39247 else
39249 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39250 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39251 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39252 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39255 // If the new parameter was constrained, we need to add that to the
39256 // constraints in the template parameter list.
39257 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39259 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39260 reqs = conjoin_constraints (reqs, req);
39261 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39264 current_binding_level = entry_scope;
39266 return new_decl;
39269 /* Finish the declaration of a fully implicit function template. Such a
39270 template has no explicit template parameter list so has not been through the
39271 normal template head and tail processing. synthesize_implicit_template_parm
39272 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39273 provided if the declaration is a class member such that its template
39274 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39275 form is returned. Otherwise NULL_TREE is returned. */
39277 static tree
39278 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39280 gcc_assert (parser->fully_implicit_function_template_p);
39282 if (member_decl_opt && member_decl_opt != error_mark_node
39283 && DECL_VIRTUAL_P (member_decl_opt))
39285 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39286 "implicit templates may not be %<virtual%>");
39287 DECL_VIRTUAL_P (member_decl_opt) = false;
39290 if (member_decl_opt)
39291 member_decl_opt = finish_member_template_decl (member_decl_opt);
39292 end_template_decl ();
39294 parser->fully_implicit_function_template_p = false;
39295 parser->implicit_template_parms = 0;
39296 parser->implicit_template_scope = 0;
39297 --parser->num_template_parameter_lists;
39299 return member_decl_opt;
39302 /* Like finish_fully_implicit_template, but to be used in error
39303 recovery, rearranging scopes so that we restore the state we had
39304 before synthesize_implicit_template_parm inserted the implement
39305 template parms scope. */
39307 static void
39308 abort_fully_implicit_template (cp_parser *parser)
39310 cp_binding_level *return_to_scope = current_binding_level;
39312 if (parser->implicit_template_scope
39313 && return_to_scope != parser->implicit_template_scope)
39315 cp_binding_level *child = return_to_scope;
39316 for (cp_binding_level *scope = child->level_chain;
39317 scope != parser->implicit_template_scope;
39318 scope = child->level_chain)
39319 child = scope;
39320 child->level_chain = parser->implicit_template_scope->level_chain;
39321 parser->implicit_template_scope->level_chain = return_to_scope;
39322 current_binding_level = parser->implicit_template_scope;
39324 else
39325 return_to_scope = return_to_scope->level_chain;
39327 finish_fully_implicit_template (parser, NULL);
39329 gcc_assert (current_binding_level == return_to_scope);
39332 /* Helper function for diagnostics that have complained about things
39333 being used with 'extern "C"' linkage.
39335 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39337 void
39338 maybe_show_extern_c_location (void)
39340 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39341 inform (the_parser->innermost_linkage_specification_location,
39342 "%<extern \"C\"%> linkage started here");
39345 #include "gt-cp-parser.h"