PR inline-asm/84742
[official-gcc.git] / gcc / cp / parser.c
bloba19bbe1e1d06d7a95c52d04ef87b91fd2faa9b5b
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2268 /* Classes [gram.class] */
2270 static tree cp_parser_class_name
2271 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2273 (cp_parser *);
2274 static tree cp_parser_class_head
2275 (cp_parser *, bool *);
2276 static enum tag_types cp_parser_class_key
2277 (cp_parser *);
2278 static void cp_parser_type_parameter_key
2279 (cp_parser* parser);
2280 static void cp_parser_member_specification_opt
2281 (cp_parser *);
2282 static void cp_parser_member_declaration
2283 (cp_parser *);
2284 static tree cp_parser_pure_specifier
2285 (cp_parser *);
2286 static tree cp_parser_constant_initializer
2287 (cp_parser *);
2289 /* Derived classes [gram.class.derived] */
2291 static tree cp_parser_base_clause
2292 (cp_parser *);
2293 static tree cp_parser_base_specifier
2294 (cp_parser *);
2296 /* Special member functions [gram.special] */
2298 static tree cp_parser_conversion_function_id
2299 (cp_parser *);
2300 static tree cp_parser_conversion_type_id
2301 (cp_parser *);
2302 static cp_declarator *cp_parser_conversion_declarator_opt
2303 (cp_parser *);
2304 static void cp_parser_ctor_initializer_opt
2305 (cp_parser *);
2306 static void cp_parser_mem_initializer_list
2307 (cp_parser *);
2308 static tree cp_parser_mem_initializer
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer_id
2311 (cp_parser *);
2313 /* Overloading [gram.over] */
2315 static cp_expr cp_parser_operator_function_id
2316 (cp_parser *);
2317 static cp_expr cp_parser_operator
2318 (cp_parser *);
2320 /* Templates [gram.temp] */
2322 static void cp_parser_template_declaration
2323 (cp_parser *, bool);
2324 static tree cp_parser_template_parameter_list
2325 (cp_parser *);
2326 static tree cp_parser_template_parameter
2327 (cp_parser *, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser *, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser *, bool, bool, enum tag_types, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2334 static tree cp_parser_template_argument_list
2335 (cp_parser *);
2336 static tree cp_parser_template_argument
2337 (cp_parser *);
2338 static void cp_parser_explicit_instantiation
2339 (cp_parser *);
2340 static void cp_parser_explicit_specialization
2341 (cp_parser *);
2343 /* Exception handling [gram.exception] */
2345 static tree cp_parser_try_block
2346 (cp_parser *);
2347 static void cp_parser_function_try_block
2348 (cp_parser *);
2349 static void cp_parser_handler_seq
2350 (cp_parser *);
2351 static void cp_parser_handler
2352 (cp_parser *);
2353 static tree cp_parser_exception_declaration
2354 (cp_parser *);
2355 static tree cp_parser_throw_expression
2356 (cp_parser *);
2357 static tree cp_parser_exception_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_type_id_list
2360 (cp_parser *);
2362 /* GNU Extensions */
2364 static tree cp_parser_asm_specification_opt
2365 (cp_parser *);
2366 static tree cp_parser_asm_operand_list
2367 (cp_parser *);
2368 static tree cp_parser_asm_clobber_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_label_list
2371 (cp_parser *);
2372 static bool cp_next_tokens_can_be_attribute_p
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_std_attribute_p
2377 (cp_parser *);
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser *, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser *, size_t);
2384 static tree cp_parser_attributes_opt
2385 (cp_parser *);
2386 static tree cp_parser_gnu_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attribute_list
2389 (cp_parser *);
2390 static tree cp_parser_std_attribute
2391 (cp_parser *, tree);
2392 static tree cp_parser_std_attribute_spec
2393 (cp_parser *);
2394 static tree cp_parser_std_attribute_spec_seq
2395 (cp_parser *);
2396 static size_t cp_parser_skip_attributes_opt
2397 (cp_parser *, size_t);
2398 static bool cp_parser_extension_opt
2399 (cp_parser *, int *);
2400 static void cp_parser_label_declaration
2401 (cp_parser *);
2403 /* Concept Extensions */
2405 static tree cp_parser_requires_clause
2406 (cp_parser *);
2407 static tree cp_parser_requires_clause_opt
2408 (cp_parser *);
2409 static tree cp_parser_requires_expression
2410 (cp_parser *);
2411 static tree cp_parser_requirement_parameter_list
2412 (cp_parser *);
2413 static tree cp_parser_requirement_body
2414 (cp_parser *);
2415 static tree cp_parser_requirement_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement
2418 (cp_parser *);
2419 static tree cp_parser_simple_requirement
2420 (cp_parser *);
2421 static tree cp_parser_compound_requirement
2422 (cp_parser *);
2423 static tree cp_parser_type_requirement
2424 (cp_parser *);
2425 static tree cp_parser_nested_requirement
2426 (cp_parser *);
2428 /* Transactional Memory Extensions */
2430 static tree cp_parser_transaction
2431 (cp_parser *, cp_token *);
2432 static tree cp_parser_transaction_expression
2433 (cp_parser *, enum rid);
2434 static void cp_parser_function_transaction
2435 (cp_parser *, enum rid);
2436 static tree cp_parser_transaction_cancel
2437 (cp_parser *);
2439 enum pragma_context {
2440 pragma_external,
2441 pragma_member,
2442 pragma_objc_icode,
2443 pragma_stmt,
2444 pragma_compound
2446 static bool cp_parser_pragma
2447 (cp_parser *, enum pragma_context, bool *);
2449 /* Objective-C++ Productions */
2451 static tree cp_parser_objc_message_receiver
2452 (cp_parser *);
2453 static tree cp_parser_objc_message_args
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_expression
2456 (cp_parser *);
2457 static cp_expr cp_parser_objc_encode_expression
2458 (cp_parser *);
2459 static tree cp_parser_objc_defs_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_protocol_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_selector_expression
2464 (cp_parser *);
2465 static cp_expr cp_parser_objc_expression
2466 (cp_parser *);
2467 static bool cp_parser_objc_selector_p
2468 (enum cpp_ttype);
2469 static tree cp_parser_objc_selector
2470 (cp_parser *);
2471 static tree cp_parser_objc_protocol_refs_opt
2472 (cp_parser *);
2473 static void cp_parser_objc_declaration
2474 (cp_parser *, tree);
2475 static tree cp_parser_objc_statement
2476 (cp_parser *);
2477 static bool cp_parser_objc_valid_prefix_attributes
2478 (cp_parser *, tree *);
2479 static void cp_parser_objc_at_property_declaration
2480 (cp_parser *) ;
2481 static void cp_parser_objc_at_synthesize_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_dynamic_declaration
2484 (cp_parser *) ;
2485 static tree cp_parser_objc_struct_declaration
2486 (cp_parser *) ;
2488 /* Utility Routines */
2490 static cp_expr cp_parser_lookup_name
2491 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2492 static tree cp_parser_lookup_name_simple
2493 (cp_parser *, tree, location_t);
2494 static tree cp_parser_maybe_treat_template_as_class
2495 (tree, bool);
2496 static bool cp_parser_check_declarator_template_parameters
2497 (cp_parser *, cp_declarator *, location_t);
2498 static bool cp_parser_check_template_parameters
2499 (cp_parser *, unsigned, location_t, cp_declarator *);
2500 static cp_expr cp_parser_simple_cast_expression
2501 (cp_parser *);
2502 static tree cp_parser_global_scope_opt
2503 (cp_parser *, bool);
2504 static bool cp_parser_constructor_declarator_p
2505 (cp_parser *, bool);
2506 static tree cp_parser_function_definition_from_specifiers_and_declarator
2507 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2508 static tree cp_parser_function_definition_after_declarator
2509 (cp_parser *, bool);
2510 static bool cp_parser_template_declaration_after_export
2511 (cp_parser *, bool);
2512 static void cp_parser_perform_template_parameter_access_checks
2513 (vec<deferred_access_check, va_gc> *);
2514 static tree cp_parser_single_declaration
2515 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2516 static cp_expr cp_parser_functional_cast
2517 (cp_parser *, tree);
2518 static tree cp_parser_save_member_function_body
2519 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2520 static tree cp_parser_save_nsdmi
2521 (cp_parser *);
2522 static tree cp_parser_enclosed_template_argument_list
2523 (cp_parser *);
2524 static void cp_parser_save_default_args
2525 (cp_parser *, tree);
2526 static void cp_parser_late_parsing_for_member
2527 (cp_parser *, tree);
2528 static tree cp_parser_late_parse_one_default_arg
2529 (cp_parser *, tree, tree, tree);
2530 static void cp_parser_late_parsing_nsdmi
2531 (cp_parser *, tree);
2532 static void cp_parser_late_parsing_default_args
2533 (cp_parser *, tree);
2534 static tree cp_parser_sizeof_operand
2535 (cp_parser *, enum rid);
2536 static cp_expr cp_parser_trait_expr
2537 (cp_parser *, enum rid);
2538 static bool cp_parser_declares_only_class_p
2539 (cp_parser *);
2540 static void cp_parser_set_storage_class
2541 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2542 static void cp_parser_set_decl_spec_type
2543 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2544 static void set_and_check_decl_spec_loc
2545 (cp_decl_specifier_seq *decl_specs,
2546 cp_decl_spec ds, cp_token *);
2547 static bool cp_parser_friend_p
2548 (const cp_decl_specifier_seq *);
2549 static void cp_parser_required_error
2550 (cp_parser *, required_token, bool, location_t);
2551 static cp_token *cp_parser_require
2552 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2553 static cp_token *cp_parser_require_keyword
2554 (cp_parser *, enum rid, required_token);
2555 static bool cp_parser_token_starts_function_definition_p
2556 (cp_token *);
2557 static bool cp_parser_next_token_starts_class_definition_p
2558 (cp_parser *);
2559 static bool cp_parser_next_token_ends_template_argument_p
2560 (cp_parser *);
2561 static bool cp_parser_nth_token_starts_template_argument_list_p
2562 (cp_parser *, size_t);
2563 static enum tag_types cp_parser_token_is_class_key
2564 (cp_token *);
2565 static enum tag_types cp_parser_token_is_type_parameter_key
2566 (cp_token *);
2567 static void cp_parser_check_class_key
2568 (enum tag_types, tree type);
2569 static void cp_parser_check_access_in_redeclaration
2570 (tree type, location_t location);
2571 static bool cp_parser_optional_template_keyword
2572 (cp_parser *);
2573 static void cp_parser_pre_parsed_nested_name_specifier
2574 (cp_parser *);
2575 static bool cp_parser_cache_group
2576 (cp_parser *, enum cpp_ttype, unsigned);
2577 static tree cp_parser_cache_defarg
2578 (cp_parser *parser, bool nsdmi);
2579 static void cp_parser_parse_tentatively
2580 (cp_parser *);
2581 static void cp_parser_commit_to_tentative_parse
2582 (cp_parser *);
2583 static void cp_parser_commit_to_topmost_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_abort_tentative_parse
2586 (cp_parser *);
2587 static bool cp_parser_parse_definitely
2588 (cp_parser *);
2589 static inline bool cp_parser_parsing_tentatively
2590 (cp_parser *);
2591 static bool cp_parser_uncommitted_to_tentative_parse_p
2592 (cp_parser *);
2593 static void cp_parser_error
2594 (cp_parser *, const char *);
2595 static void cp_parser_name_lookup_error
2596 (cp_parser *, tree, tree, name_lookup_error, location_t);
2597 static bool cp_parser_simulate_error
2598 (cp_parser *);
2599 static bool cp_parser_check_type_definition
2600 (cp_parser *);
2601 static void cp_parser_check_for_definition_in_return_type
2602 (cp_declarator *, tree, location_t type_location);
2603 static void cp_parser_check_for_invalid_template_id
2604 (cp_parser *, tree, enum tag_types, location_t location);
2605 static bool cp_parser_non_integral_constant_expression
2606 (cp_parser *, non_integral_constant);
2607 static void cp_parser_diagnose_invalid_type_name
2608 (cp_parser *, tree, location_t);
2609 static bool cp_parser_parse_and_diagnose_invalid_type_name
2610 (cp_parser *);
2611 static int cp_parser_skip_to_closing_parenthesis
2612 (cp_parser *, bool, bool, bool);
2613 static void cp_parser_skip_to_end_of_statement
2614 (cp_parser *);
2615 static void cp_parser_consume_semicolon_at_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_skip_to_end_of_block_or_statement
2618 (cp_parser *);
2619 static bool cp_parser_skip_to_closing_brace
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_template_parameter_list
2622 (cp_parser *);
2623 static void cp_parser_skip_to_pragma_eol
2624 (cp_parser*, cp_token *);
2625 static bool cp_parser_error_occurred
2626 (cp_parser *);
2627 static bool cp_parser_allow_gnu_extensions_p
2628 (cp_parser *);
2629 static bool cp_parser_is_pure_string_literal
2630 (cp_token *);
2631 static bool cp_parser_is_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_keyword
2634 (cp_token *, enum rid);
2635 static tree cp_parser_make_typename_type
2636 (cp_parser *, tree, location_t location);
2637 static cp_declarator * cp_parser_make_indirect_declarator
2638 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2639 static bool cp_parser_compound_literal_p
2640 (cp_parser *);
2641 static bool cp_parser_array_designator_p
2642 (cp_parser *);
2643 static bool cp_parser_init_statement_p
2644 (cp_parser *);
2645 static bool cp_parser_skip_to_closing_square_bracket
2646 (cp_parser *);
2648 /* Concept-related syntactic transformations */
2650 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2651 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2653 // -------------------------------------------------------------------------- //
2654 // Unevaluated Operand Guard
2656 // Implementation of an RAII helper for unevaluated operand parsing.
2657 cp_unevaluated::cp_unevaluated ()
2659 ++cp_unevaluated_operand;
2660 ++c_inhibit_evaluation_warnings;
2663 cp_unevaluated::~cp_unevaluated ()
2665 --c_inhibit_evaluation_warnings;
2666 --cp_unevaluated_operand;
2669 // -------------------------------------------------------------------------- //
2670 // Tentative Parsing
2672 /* Returns nonzero if we are parsing tentatively. */
2674 static inline bool
2675 cp_parser_parsing_tentatively (cp_parser* parser)
2677 return parser->context->next != NULL;
2680 /* Returns nonzero if TOKEN is a string literal. */
2682 static bool
2683 cp_parser_is_pure_string_literal (cp_token* token)
2685 return (token->type == CPP_STRING ||
2686 token->type == CPP_STRING16 ||
2687 token->type == CPP_STRING32 ||
2688 token->type == CPP_WSTRING ||
2689 token->type == CPP_UTF8STRING);
2692 /* Returns nonzero if TOKEN is a string literal
2693 of a user-defined string literal. */
2695 static bool
2696 cp_parser_is_string_literal (cp_token* token)
2698 return (cp_parser_is_pure_string_literal (token) ||
2699 token->type == CPP_STRING_USERDEF ||
2700 token->type == CPP_STRING16_USERDEF ||
2701 token->type == CPP_STRING32_USERDEF ||
2702 token->type == CPP_WSTRING_USERDEF ||
2703 token->type == CPP_UTF8STRING_USERDEF);
2706 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2708 static bool
2709 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2711 return token->keyword == keyword;
2714 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2715 PRAGMA_NONE. */
2717 static enum pragma_kind
2718 cp_parser_pragma_kind (cp_token *token)
2720 if (token->type != CPP_PRAGMA)
2721 return PRAGMA_NONE;
2722 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2723 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2726 /* Helper function for cp_parser_error.
2727 Having peeked a token of kind TOK1_KIND that might signify
2728 a conflict marker, peek successor tokens to determine
2729 if we actually do have a conflict marker.
2730 Specifically, we consider a run of 7 '<', '=' or '>' characters
2731 at the start of a line as a conflict marker.
2732 These come through the lexer as three pairs and a single,
2733 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2734 If it returns true, *OUT_LOC is written to with the location/range
2735 of the marker. */
2737 static bool
2738 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2739 location_t *out_loc)
2741 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2742 if (token2->type != tok1_kind)
2743 return false;
2744 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2745 if (token3->type != tok1_kind)
2746 return false;
2747 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2748 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2749 return false;
2751 /* It must be at the start of the line. */
2752 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2753 if (LOCATION_COLUMN (start_loc) != 1)
2754 return false;
2756 /* We have a conflict marker. Construct a location of the form:
2757 <<<<<<<
2758 ^~~~~~~
2759 with start == caret, finishing at the end of the marker. */
2760 location_t finish_loc = get_finish (token4->location);
2761 *out_loc = make_location (start_loc, start_loc, finish_loc);
2763 return true;
2766 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2767 RT_CLOSE_PAREN. */
2769 static const char *
2770 get_matching_symbol (required_token token_desc)
2772 switch (token_desc)
2774 default:
2775 gcc_unreachable ();
2776 return "";
2777 case RT_CLOSE_BRACE:
2778 return "{";
2779 case RT_CLOSE_PAREN:
2780 return "(";
2784 /* Attempt to convert TOKEN_DESC from a required_token to an
2785 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2787 static enum cpp_ttype
2788 get_required_cpp_ttype (required_token token_desc)
2790 switch (token_desc)
2792 case RT_SEMICOLON:
2793 return CPP_SEMICOLON;
2794 case RT_OPEN_PAREN:
2795 return CPP_OPEN_PAREN;
2796 case RT_CLOSE_BRACE:
2797 return CPP_CLOSE_BRACE;
2798 case RT_OPEN_BRACE:
2799 return CPP_OPEN_BRACE;
2800 case RT_CLOSE_SQUARE:
2801 return CPP_CLOSE_SQUARE;
2802 case RT_OPEN_SQUARE:
2803 return CPP_OPEN_SQUARE;
2804 case RT_COMMA:
2805 return CPP_COMMA;
2806 case RT_COLON:
2807 return CPP_COLON;
2808 case RT_CLOSE_PAREN:
2809 return CPP_CLOSE_PAREN;
2811 default:
2812 /* Use CPP_EOF as a "no completions possible" code. */
2813 return CPP_EOF;
2818 /* Subroutine of cp_parser_error and cp_parser_required_error.
2820 Issue a diagnostic of the form
2821 FILE:LINE: MESSAGE before TOKEN
2822 where TOKEN is the next token in the input stream. MESSAGE
2823 (specified by the caller) is usually of the form "expected
2824 OTHER-TOKEN".
2826 This bypasses the check for tentative passing, and potentially
2827 adds material needed by cp_parser_required_error.
2829 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2830 suggesting insertion of the missing token.
2832 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2833 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2834 location. */
2836 static void
2837 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2838 required_token missing_token_desc,
2839 location_t matching_location)
2841 cp_token *token = cp_lexer_peek_token (parser->lexer);
2842 /* This diagnostic makes more sense if it is tagged to the line
2843 of the token we just peeked at. */
2844 cp_lexer_set_source_position_from_token (token);
2846 if (token->type == CPP_PRAGMA)
2848 error_at (token->location,
2849 "%<#pragma%> is not allowed here");
2850 cp_parser_skip_to_pragma_eol (parser, token);
2851 return;
2854 /* If this is actually a conflict marker, report it as such. */
2855 if (token->type == CPP_LSHIFT
2856 || token->type == CPP_RSHIFT
2857 || token->type == CPP_EQ_EQ)
2859 location_t loc;
2860 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2862 error_at (loc, "version control conflict marker in file");
2863 return;
2867 gcc_rich_location richloc (input_location);
2869 bool added_matching_location = false;
2871 if (missing_token_desc != RT_NONE)
2873 /* Potentially supply a fix-it hint, suggesting to add the
2874 missing token immediately after the *previous* token.
2875 This may move the primary location within richloc. */
2876 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2877 location_t prev_token_loc
2878 = cp_lexer_previous_token (parser->lexer)->location;
2879 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2881 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2882 Attempt to consolidate diagnostics by printing it as a
2883 secondary range within the main diagnostic. */
2884 if (matching_location != UNKNOWN_LOCATION)
2885 added_matching_location
2886 = richloc.add_location_if_nearby (matching_location);
2889 /* Actually emit the error. */
2890 c_parse_error (gmsgid,
2891 /* Because c_parser_error does not understand
2892 CPP_KEYWORD, keywords are treated like
2893 identifiers. */
2894 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2895 token->u.value, token->flags, &richloc);
2897 if (missing_token_desc != RT_NONE)
2899 /* If we weren't able to consolidate matching_location, then
2900 print it as a secondary diagnostic. */
2901 if (matching_location != UNKNOWN_LOCATION
2902 && !added_matching_location)
2903 inform (matching_location, "to match this %qs",
2904 get_matching_symbol (missing_token_desc));
2908 /* If not parsing tentatively, issue a diagnostic of the form
2909 FILE:LINE: MESSAGE before TOKEN
2910 where TOKEN is the next token in the input stream. MESSAGE
2911 (specified by the caller) is usually of the form "expected
2912 OTHER-TOKEN". */
2914 static void
2915 cp_parser_error (cp_parser* parser, const char* gmsgid)
2917 if (!cp_parser_simulate_error (parser))
2918 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2921 /* Issue an error about name-lookup failing. NAME is the
2922 IDENTIFIER_NODE DECL is the result of
2923 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2924 the thing that we hoped to find. */
2926 static void
2927 cp_parser_name_lookup_error (cp_parser* parser,
2928 tree name,
2929 tree decl,
2930 name_lookup_error desired,
2931 location_t location)
2933 /* If name lookup completely failed, tell the user that NAME was not
2934 declared. */
2935 if (decl == error_mark_node)
2937 if (parser->scope && parser->scope != global_namespace)
2938 error_at (location, "%<%E::%E%> has not been declared",
2939 parser->scope, name);
2940 else if (parser->scope == global_namespace)
2941 error_at (location, "%<::%E%> has not been declared", name);
2942 else if (parser->object_scope
2943 && !CLASS_TYPE_P (parser->object_scope))
2944 error_at (location, "request for member %qE in non-class type %qT",
2945 name, parser->object_scope);
2946 else if (parser->object_scope)
2947 error_at (location, "%<%T::%E%> has not been declared",
2948 parser->object_scope, name);
2949 else
2950 error_at (location, "%qE has not been declared", name);
2952 else if (parser->scope && parser->scope != global_namespace)
2954 switch (desired)
2956 case NLE_TYPE:
2957 error_at (location, "%<%E::%E%> is not a type",
2958 parser->scope, name);
2959 break;
2960 case NLE_CXX98:
2961 error_at (location, "%<%E::%E%> is not a class or namespace",
2962 parser->scope, name);
2963 break;
2964 case NLE_NOT_CXX98:
2965 error_at (location,
2966 "%<%E::%E%> is not a class, namespace, or enumeration",
2967 parser->scope, name);
2968 break;
2969 default:
2970 gcc_unreachable ();
2974 else if (parser->scope == global_namespace)
2976 switch (desired)
2978 case NLE_TYPE:
2979 error_at (location, "%<::%E%> is not a type", name);
2980 break;
2981 case NLE_CXX98:
2982 error_at (location, "%<::%E%> is not a class or namespace", name);
2983 break;
2984 case NLE_NOT_CXX98:
2985 error_at (location,
2986 "%<::%E%> is not a class, namespace, or enumeration",
2987 name);
2988 break;
2989 default:
2990 gcc_unreachable ();
2993 else
2995 switch (desired)
2997 case NLE_TYPE:
2998 error_at (location, "%qE is not a type", name);
2999 break;
3000 case NLE_CXX98:
3001 error_at (location, "%qE is not a class or namespace", name);
3002 break;
3003 case NLE_NOT_CXX98:
3004 error_at (location,
3005 "%qE is not a class, namespace, or enumeration", name);
3006 break;
3007 default:
3008 gcc_unreachable ();
3013 /* If we are parsing tentatively, remember that an error has occurred
3014 during this tentative parse. Returns true if the error was
3015 simulated; false if a message should be issued by the caller. */
3017 static bool
3018 cp_parser_simulate_error (cp_parser* parser)
3020 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3022 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3023 return true;
3025 return false;
3028 /* This function is called when a type is defined. If type
3029 definitions are forbidden at this point, an error message is
3030 issued. */
3032 static bool
3033 cp_parser_check_type_definition (cp_parser* parser)
3035 /* If types are forbidden here, issue a message. */
3036 if (parser->type_definition_forbidden_message)
3038 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3039 in the message need to be interpreted. */
3040 error (parser->type_definition_forbidden_message);
3041 return false;
3043 return true;
3046 /* This function is called when the DECLARATOR is processed. The TYPE
3047 was a type defined in the decl-specifiers. If it is invalid to
3048 define a type in the decl-specifiers for DECLARATOR, an error is
3049 issued. TYPE_LOCATION is the location of TYPE and is used
3050 for error reporting. */
3052 static void
3053 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3054 tree type, location_t type_location)
3056 /* [dcl.fct] forbids type definitions in return types.
3057 Unfortunately, it's not easy to know whether or not we are
3058 processing a return type until after the fact. */
3059 while (declarator
3060 && (declarator->kind == cdk_pointer
3061 || declarator->kind == cdk_reference
3062 || declarator->kind == cdk_ptrmem))
3063 declarator = declarator->declarator;
3064 if (declarator
3065 && declarator->kind == cdk_function)
3067 error_at (type_location,
3068 "new types may not be defined in a return type");
3069 inform (type_location,
3070 "(perhaps a semicolon is missing after the definition of %qT)",
3071 type);
3075 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3076 "<" in any valid C++ program. If the next token is indeed "<",
3077 issue a message warning the user about what appears to be an
3078 invalid attempt to form a template-id. LOCATION is the location
3079 of the type-specifier (TYPE) */
3081 static void
3082 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3083 tree type,
3084 enum tag_types tag_type,
3085 location_t location)
3087 cp_token_position start = 0;
3089 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3091 if (TREE_CODE (type) == TYPE_DECL)
3092 type = TREE_TYPE (type);
3093 if (TYPE_P (type) && !template_placeholder_p (type))
3094 error_at (location, "%qT is not a template", type);
3095 else if (identifier_p (type))
3097 if (tag_type != none_type)
3098 error_at (location, "%qE is not a class template", type);
3099 else
3100 error_at (location, "%qE is not a template", type);
3102 else
3103 error_at (location, "invalid template-id");
3104 /* Remember the location of the invalid "<". */
3105 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3106 start = cp_lexer_token_position (parser->lexer, true);
3107 /* Consume the "<". */
3108 cp_lexer_consume_token (parser->lexer);
3109 /* Parse the template arguments. */
3110 cp_parser_enclosed_template_argument_list (parser);
3111 /* Permanently remove the invalid template arguments so that
3112 this error message is not issued again. */
3113 if (start)
3114 cp_lexer_purge_tokens_after (parser->lexer, start);
3118 /* If parsing an integral constant-expression, issue an error message
3119 about the fact that THING appeared and return true. Otherwise,
3120 return false. In either case, set
3121 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3123 static bool
3124 cp_parser_non_integral_constant_expression (cp_parser *parser,
3125 non_integral_constant thing)
3127 parser->non_integral_constant_expression_p = true;
3128 if (parser->integral_constant_expression_p)
3130 if (!parser->allow_non_integral_constant_expression_p)
3132 const char *msg = NULL;
3133 switch (thing)
3135 case NIC_FLOAT:
3136 pedwarn (input_location, OPT_Wpedantic,
3137 "ISO C++ forbids using a floating-point literal "
3138 "in a constant-expression");
3139 return true;
3140 case NIC_CAST:
3141 error ("a cast to a type other than an integral or "
3142 "enumeration type cannot appear in a "
3143 "constant-expression");
3144 return true;
3145 case NIC_TYPEID:
3146 error ("%<typeid%> operator "
3147 "cannot appear in a constant-expression");
3148 return true;
3149 case NIC_NCC:
3150 error ("non-constant compound literals "
3151 "cannot appear in a constant-expression");
3152 return true;
3153 case NIC_FUNC_CALL:
3154 error ("a function call "
3155 "cannot appear in a constant-expression");
3156 return true;
3157 case NIC_INC:
3158 error ("an increment "
3159 "cannot appear in a constant-expression");
3160 return true;
3161 case NIC_DEC:
3162 error ("an decrement "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_ARRAY_REF:
3166 error ("an array reference "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_ADDR_LABEL:
3170 error ("the address of a label "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_OVERLOADED:
3174 error ("calls to overloaded operators "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_ASSIGNMENT:
3178 error ("an assignment cannot appear in a constant-expression");
3179 return true;
3180 case NIC_COMMA:
3181 error ("a comma operator "
3182 "cannot appear in a constant-expression");
3183 return true;
3184 case NIC_CONSTRUCTOR:
3185 error ("a call to a constructor "
3186 "cannot appear in a constant-expression");
3187 return true;
3188 case NIC_TRANSACTION:
3189 error ("a transaction expression "
3190 "cannot appear in a constant-expression");
3191 return true;
3192 case NIC_THIS:
3193 msg = "this";
3194 break;
3195 case NIC_FUNC_NAME:
3196 msg = "__FUNCTION__";
3197 break;
3198 case NIC_PRETTY_FUNC:
3199 msg = "__PRETTY_FUNCTION__";
3200 break;
3201 case NIC_C99_FUNC:
3202 msg = "__func__";
3203 break;
3204 case NIC_VA_ARG:
3205 msg = "va_arg";
3206 break;
3207 case NIC_ARROW:
3208 msg = "->";
3209 break;
3210 case NIC_POINT:
3211 msg = ".";
3212 break;
3213 case NIC_STAR:
3214 msg = "*";
3215 break;
3216 case NIC_ADDR:
3217 msg = "&";
3218 break;
3219 case NIC_PREINCREMENT:
3220 msg = "++";
3221 break;
3222 case NIC_PREDECREMENT:
3223 msg = "--";
3224 break;
3225 case NIC_NEW:
3226 msg = "new";
3227 break;
3228 case NIC_DEL:
3229 msg = "delete";
3230 break;
3231 default:
3232 gcc_unreachable ();
3234 if (msg)
3235 error ("%qs cannot appear in a constant-expression", msg);
3236 return true;
3239 return false;
3242 /* Emit a diagnostic for an invalid type name. This function commits
3243 to the current active tentative parse, if any. (Otherwise, the
3244 problematic construct might be encountered again later, resulting
3245 in duplicate error messages.) LOCATION is the location of ID. */
3247 static void
3248 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3249 location_t location)
3251 tree decl, ambiguous_decls;
3252 cp_parser_commit_to_tentative_parse (parser);
3253 /* Try to lookup the identifier. */
3254 decl = cp_parser_lookup_name (parser, id, none_type,
3255 /*is_template=*/false,
3256 /*is_namespace=*/false,
3257 /*check_dependency=*/true,
3258 &ambiguous_decls, location);
3259 if (ambiguous_decls)
3260 /* If the lookup was ambiguous, an error will already have
3261 been issued. */
3262 return;
3263 /* If the lookup found a template-name, it means that the user forgot
3264 to specify an argument list. Emit a useful error message. */
3265 if (DECL_TYPE_TEMPLATE_P (decl))
3267 error_at (location,
3268 "invalid use of template-name %qE without an argument list",
3269 decl);
3270 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3271 inform (location, "class template argument deduction is only available "
3272 "with -std=c++17 or -std=gnu++17");
3273 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3275 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3276 error_at (location, "invalid use of destructor %qD as a type", id);
3277 else if (TREE_CODE (decl) == TYPE_DECL)
3278 /* Something like 'unsigned A a;' */
3279 error_at (location, "invalid combination of multiple type-specifiers");
3280 else if (!parser->scope)
3282 /* Issue an error message. */
3283 name_hint hint;
3284 if (TREE_CODE (id) == IDENTIFIER_NODE)
3285 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3286 if (hint)
3288 gcc_rich_location richloc (location);
3289 richloc.add_fixit_replace (hint.suggestion ());
3290 error_at (&richloc,
3291 "%qE does not name a type; did you mean %qs?",
3292 id, hint.suggestion ());
3294 else
3295 error_at (location, "%qE does not name a type", id);
3296 /* If we're in a template class, it's possible that the user was
3297 referring to a type from a base class. For example:
3299 template <typename T> struct A { typedef T X; };
3300 template <typename T> struct B : public A<T> { X x; };
3302 The user should have said "typename A<T>::X". */
3303 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3304 inform (location, "C++11 %<constexpr%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3307 inform (location, "C++11 %<noexcept%> only available with "
3308 "-std=c++11 or -std=gnu++11");
3309 else if (cxx_dialect < cxx11
3310 && TREE_CODE (id) == IDENTIFIER_NODE
3311 && id_equal (id, "thread_local"))
3312 inform (location, "C++11 %<thread_local%> only available with "
3313 "-std=c++11 or -std=gnu++11");
3314 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3315 inform (location, "%<concept%> only available with -fconcepts");
3316 else if (processing_template_decl && current_class_type
3317 && TYPE_BINFO (current_class_type))
3319 tree b;
3321 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3323 b = TREE_CHAIN (b))
3325 tree base_type = BINFO_TYPE (b);
3326 if (CLASS_TYPE_P (base_type)
3327 && dependent_type_p (base_type))
3329 tree field;
3330 /* Go from a particular instantiation of the
3331 template (which will have an empty TYPE_FIELDs),
3332 to the main version. */
3333 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3334 for (field = TYPE_FIELDS (base_type);
3335 field;
3336 field = DECL_CHAIN (field))
3337 if (TREE_CODE (field) == TYPE_DECL
3338 && DECL_NAME (field) == id)
3340 inform (location,
3341 "(perhaps %<typename %T::%E%> was intended)",
3342 BINFO_TYPE (b), id);
3343 break;
3345 if (field)
3346 break;
3351 /* Here we diagnose qualified-ids where the scope is actually correct,
3352 but the identifier does not resolve to a valid type name. */
3353 else if (parser->scope != error_mark_node)
3355 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3357 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3358 error_at (location_of (id),
3359 "%qE in namespace %qE does not name a template type",
3360 id, parser->scope);
3361 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3362 error_at (location_of (id),
3363 "%qE in namespace %qE does not name a template type",
3364 TREE_OPERAND (id, 0), parser->scope);
3365 else
3366 error_at (location_of (id),
3367 "%qE in namespace %qE does not name a type",
3368 id, parser->scope);
3369 if (DECL_P (decl))
3370 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3371 else if (decl == error_mark_node)
3372 suggest_alternative_in_explicit_scope (location, id,
3373 parser->scope);
3375 else if (CLASS_TYPE_P (parser->scope)
3376 && constructor_name_p (id, parser->scope))
3378 /* A<T>::A<T>() */
3379 error_at (location, "%<%T::%E%> names the constructor, not"
3380 " the type", parser->scope, id);
3381 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3382 error_at (location, "and %qT has no template constructors",
3383 parser->scope);
3385 else if (TYPE_P (parser->scope)
3386 && dependent_scope_p (parser->scope))
3388 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3389 error_at (location,
3390 "need %<typename%> before %<%T::%D::%E%> because "
3391 "%<%T::%D%> is a dependent scope",
3392 TYPE_CONTEXT (parser->scope),
3393 TYPENAME_TYPE_FULLNAME (parser->scope),
3395 TYPE_CONTEXT (parser->scope),
3396 TYPENAME_TYPE_FULLNAME (parser->scope));
3397 else
3398 error_at (location, "need %<typename%> before %<%T::%E%> because "
3399 "%qT is a dependent scope",
3400 parser->scope, id, parser->scope);
3402 else if (TYPE_P (parser->scope))
3404 if (!COMPLETE_TYPE_P (parser->scope))
3405 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3406 parser->scope);
3407 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3408 error_at (location_of (id),
3409 "%qE in %q#T does not name a template type",
3410 id, parser->scope);
3411 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3412 error_at (location_of (id),
3413 "%qE in %q#T does not name a template type",
3414 TREE_OPERAND (id, 0), parser->scope);
3415 else
3416 error_at (location_of (id),
3417 "%qE in %q#T does not name a type",
3418 id, parser->scope);
3419 if (DECL_P (decl))
3420 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3422 else
3423 gcc_unreachable ();
3427 /* Check for a common situation where a type-name should be present,
3428 but is not, and issue a sensible error message. Returns true if an
3429 invalid type-name was detected.
3431 The situation handled by this function are variable declarations of the
3432 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3433 Usually, `ID' should name a type, but if we got here it means that it
3434 does not. We try to emit the best possible error message depending on
3435 how exactly the id-expression looks like. */
3437 static bool
3438 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3440 tree id;
3441 cp_token *token = cp_lexer_peek_token (parser->lexer);
3443 /* Avoid duplicate error about ambiguous lookup. */
3444 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3446 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3447 if (next->type == CPP_NAME && next->error_reported)
3448 goto out;
3451 cp_parser_parse_tentatively (parser);
3452 id = cp_parser_id_expression (parser,
3453 /*template_keyword_p=*/false,
3454 /*check_dependency_p=*/true,
3455 /*template_p=*/NULL,
3456 /*declarator_p=*/true,
3457 /*optional_p=*/false);
3458 /* If the next token is a (, this is a function with no explicit return
3459 type, i.e. constructor, destructor or conversion op. */
3460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3461 || TREE_CODE (id) == TYPE_DECL)
3463 cp_parser_abort_tentative_parse (parser);
3464 return false;
3466 if (!cp_parser_parse_definitely (parser))
3467 return false;
3469 /* Emit a diagnostic for the invalid type. */
3470 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3471 out:
3472 /* If we aren't in the middle of a declarator (i.e. in a
3473 parameter-declaration-clause), skip to the end of the declaration;
3474 there's no point in trying to process it. */
3475 if (!parser->in_declarator_p)
3476 cp_parser_skip_to_end_of_block_or_statement (parser);
3477 return true;
3480 /* Consume tokens up to, and including, the next non-nested closing `)'.
3481 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3482 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3483 found an unnested token of that type. */
3485 static int
3486 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3487 bool recovering,
3488 cpp_ttype or_ttype,
3489 bool consume_paren)
3491 unsigned paren_depth = 0;
3492 unsigned brace_depth = 0;
3493 unsigned square_depth = 0;
3495 if (recovering && or_ttype == CPP_EOF
3496 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3497 return 0;
3499 while (true)
3501 cp_token * token = cp_lexer_peek_token (parser->lexer);
3503 /* Have we found what we're looking for before the closing paren? */
3504 if (token->type == or_ttype && or_ttype != CPP_EOF
3505 && !brace_depth && !paren_depth && !square_depth)
3506 return -1;
3508 switch (token->type)
3510 case CPP_EOF:
3511 case CPP_PRAGMA_EOL:
3512 /* If we've run out of tokens, then there is no closing `)'. */
3513 return 0;
3515 /* This is good for lambda expression capture-lists. */
3516 case CPP_OPEN_SQUARE:
3517 ++square_depth;
3518 break;
3519 case CPP_CLOSE_SQUARE:
3520 if (!square_depth--)
3521 return 0;
3522 break;
3524 case CPP_SEMICOLON:
3525 /* This matches the processing in skip_to_end_of_statement. */
3526 if (!brace_depth)
3527 return 0;
3528 break;
3530 case CPP_OPEN_BRACE:
3531 ++brace_depth;
3532 break;
3533 case CPP_CLOSE_BRACE:
3534 if (!brace_depth--)
3535 return 0;
3536 break;
3538 case CPP_OPEN_PAREN:
3539 if (!brace_depth)
3540 ++paren_depth;
3541 break;
3543 case CPP_CLOSE_PAREN:
3544 if (!brace_depth && !paren_depth--)
3546 if (consume_paren)
3547 cp_lexer_consume_token (parser->lexer);
3548 return 1;
3550 break;
3552 default:
3553 break;
3556 /* Consume the token. */
3557 cp_lexer_consume_token (parser->lexer);
3561 /* Consume tokens up to, and including, the next non-nested closing `)'.
3562 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3563 are doing error recovery. Returns -1 if OR_COMMA is true and we
3564 found an unnested token of that type. */
3566 static int
3567 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3568 bool recovering,
3569 bool or_comma,
3570 bool consume_paren)
3572 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3573 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3574 ttype, consume_paren);
3577 /* Consume tokens until we reach the end of the current statement.
3578 Normally, that will be just before consuming a `;'. However, if a
3579 non-nested `}' comes first, then we stop before consuming that. */
3581 static void
3582 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3584 unsigned nesting_depth = 0;
3586 /* Unwind generic function template scope if necessary. */
3587 if (parser->fully_implicit_function_template_p)
3588 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3590 while (true)
3592 cp_token *token = cp_lexer_peek_token (parser->lexer);
3594 switch (token->type)
3596 case CPP_EOF:
3597 case CPP_PRAGMA_EOL:
3598 /* If we've run out of tokens, stop. */
3599 return;
3601 case CPP_SEMICOLON:
3602 /* If the next token is a `;', we have reached the end of the
3603 statement. */
3604 if (!nesting_depth)
3605 return;
3606 break;
3608 case CPP_CLOSE_BRACE:
3609 /* If this is a non-nested '}', stop before consuming it.
3610 That way, when confronted with something like:
3612 { 3 + }
3614 we stop before consuming the closing '}', even though we
3615 have not yet reached a `;'. */
3616 if (nesting_depth == 0)
3617 return;
3619 /* If it is the closing '}' for a block that we have
3620 scanned, stop -- but only after consuming the token.
3621 That way given:
3623 void f g () { ... }
3624 typedef int I;
3626 we will stop after the body of the erroneously declared
3627 function, but before consuming the following `typedef'
3628 declaration. */
3629 if (--nesting_depth == 0)
3631 cp_lexer_consume_token (parser->lexer);
3632 return;
3634 break;
3636 case CPP_OPEN_BRACE:
3637 ++nesting_depth;
3638 break;
3640 default:
3641 break;
3644 /* Consume the token. */
3645 cp_lexer_consume_token (parser->lexer);
3649 /* This function is called at the end of a statement or declaration.
3650 If the next token is a semicolon, it is consumed; otherwise, error
3651 recovery is attempted. */
3653 static void
3654 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3656 /* Look for the trailing `;'. */
3657 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3659 /* If there is additional (erroneous) input, skip to the end of
3660 the statement. */
3661 cp_parser_skip_to_end_of_statement (parser);
3662 /* If the next token is now a `;', consume it. */
3663 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3664 cp_lexer_consume_token (parser->lexer);
3668 /* Skip tokens until we have consumed an entire block, or until we
3669 have consumed a non-nested `;'. */
3671 static void
3672 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3674 int nesting_depth = 0;
3676 /* Unwind generic function template scope if necessary. */
3677 if (parser->fully_implicit_function_template_p)
3678 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3680 while (nesting_depth >= 0)
3682 cp_token *token = cp_lexer_peek_token (parser->lexer);
3684 switch (token->type)
3686 case CPP_EOF:
3687 case CPP_PRAGMA_EOL:
3688 /* If we've run out of tokens, stop. */
3689 return;
3691 case CPP_SEMICOLON:
3692 /* Stop if this is an unnested ';'. */
3693 if (!nesting_depth)
3694 nesting_depth = -1;
3695 break;
3697 case CPP_CLOSE_BRACE:
3698 /* Stop if this is an unnested '}', or closes the outermost
3699 nesting level. */
3700 nesting_depth--;
3701 if (nesting_depth < 0)
3702 return;
3703 if (!nesting_depth)
3704 nesting_depth = -1;
3705 break;
3707 case CPP_OPEN_BRACE:
3708 /* Nest. */
3709 nesting_depth++;
3710 break;
3712 default:
3713 break;
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser->lexer);
3721 /* Skip tokens until a non-nested closing curly brace is the next
3722 token, or there are no more tokens. Return true in the first case,
3723 false otherwise. */
3725 static bool
3726 cp_parser_skip_to_closing_brace (cp_parser *parser)
3728 unsigned nesting_depth = 0;
3730 while (true)
3732 cp_token *token = cp_lexer_peek_token (parser->lexer);
3734 switch (token->type)
3736 case CPP_EOF:
3737 case CPP_PRAGMA_EOL:
3738 /* If we've run out of tokens, stop. */
3739 return false;
3741 case CPP_CLOSE_BRACE:
3742 /* If the next token is a non-nested `}', then we have reached
3743 the end of the current block. */
3744 if (nesting_depth-- == 0)
3745 return true;
3746 break;
3748 case CPP_OPEN_BRACE:
3749 /* If it the next token is a `{', then we are entering a new
3750 block. Consume the entire block. */
3751 ++nesting_depth;
3752 break;
3754 default:
3755 break;
3758 /* Consume the token. */
3759 cp_lexer_consume_token (parser->lexer);
3763 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3764 parameter is the PRAGMA token, allowing us to purge the entire pragma
3765 sequence. */
3767 static void
3768 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3770 cp_token *token;
3772 parser->lexer->in_pragma = false;
3775 token = cp_lexer_consume_token (parser->lexer);
3776 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3778 /* Ensure that the pragma is not parsed again. */
3779 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3782 /* Require pragma end of line, resyncing with it as necessary. The
3783 arguments are as for cp_parser_skip_to_pragma_eol. */
3785 static void
3786 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3788 parser->lexer->in_pragma = false;
3789 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3790 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3793 /* This is a simple wrapper around make_typename_type. When the id is
3794 an unresolved identifier node, we can provide a superior diagnostic
3795 using cp_parser_diagnose_invalid_type_name. */
3797 static tree
3798 cp_parser_make_typename_type (cp_parser *parser, tree id,
3799 location_t id_location)
3801 tree result;
3802 if (identifier_p (id))
3804 result = make_typename_type (parser->scope, id, typename_type,
3805 /*complain=*/tf_none);
3806 if (result == error_mark_node)
3807 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3808 return result;
3810 return make_typename_type (parser->scope, id, typename_type, tf_error);
3813 /* This is a wrapper around the
3814 make_{pointer,ptrmem,reference}_declarator functions that decides
3815 which one to call based on the CODE and CLASS_TYPE arguments. The
3816 CODE argument should be one of the values returned by
3817 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3818 appertain to the pointer or reference. */
3820 static cp_declarator *
3821 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3822 cp_cv_quals cv_qualifiers,
3823 cp_declarator *target,
3824 tree attributes)
3826 if (code == ERROR_MARK)
3827 return cp_error_declarator;
3829 if (code == INDIRECT_REF)
3830 if (class_type == NULL_TREE)
3831 return make_pointer_declarator (cv_qualifiers, target, attributes);
3832 else
3833 return make_ptrmem_declarator (cv_qualifiers, class_type,
3834 target, attributes);
3835 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3836 return make_reference_declarator (cv_qualifiers, target,
3837 false, attributes);
3838 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3839 return make_reference_declarator (cv_qualifiers, target,
3840 true, attributes);
3841 gcc_unreachable ();
3844 /* Create a new C++ parser. */
3846 static cp_parser *
3847 cp_parser_new (void)
3849 cp_parser *parser;
3850 cp_lexer *lexer;
3851 unsigned i;
3853 /* cp_lexer_new_main is called before doing GC allocation because
3854 cp_lexer_new_main might load a PCH file. */
3855 lexer = cp_lexer_new_main ();
3857 /* Initialize the binops_by_token so that we can get the tree
3858 directly from the token. */
3859 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3860 binops_by_token[binops[i].token_type] = binops[i];
3862 parser = ggc_cleared_alloc<cp_parser> ();
3863 parser->lexer = lexer;
3864 parser->context = cp_parser_context_new (NULL);
3866 /* For now, we always accept GNU extensions. */
3867 parser->allow_gnu_extensions_p = 1;
3869 /* The `>' token is a greater-than operator, not the end of a
3870 template-id. */
3871 parser->greater_than_is_operator_p = true;
3873 parser->default_arg_ok_p = true;
3875 /* We are not parsing a constant-expression. */
3876 parser->integral_constant_expression_p = false;
3877 parser->allow_non_integral_constant_expression_p = false;
3878 parser->non_integral_constant_expression_p = false;
3880 /* Local variable names are not forbidden. */
3881 parser->local_variables_forbidden_p = false;
3883 /* We are not processing an `extern "C"' declaration. */
3884 parser->in_unbraced_linkage_specification_p = false;
3886 /* We are not processing a declarator. */
3887 parser->in_declarator_p = false;
3889 /* We are not processing a template-argument-list. */
3890 parser->in_template_argument_list_p = false;
3892 /* We are not in an iteration statement. */
3893 parser->in_statement = 0;
3895 /* We are not in a switch statement. */
3896 parser->in_switch_statement_p = false;
3898 /* We are not parsing a type-id inside an expression. */
3899 parser->in_type_id_in_expr_p = false;
3901 /* Declarations aren't implicitly extern "C". */
3902 parser->implicit_extern_c = false;
3904 /* String literals should be translated to the execution character set. */
3905 parser->translate_strings_p = true;
3907 /* We are not parsing a function body. */
3908 parser->in_function_body = false;
3910 /* We can correct until told otherwise. */
3911 parser->colon_corrects_to_scope_p = true;
3913 /* The unparsed function queue is empty. */
3914 push_unparsed_function_queues (parser);
3916 /* There are no classes being defined. */
3917 parser->num_classes_being_defined = 0;
3919 /* No template parameters apply. */
3920 parser->num_template_parameter_lists = 0;
3922 /* Special parsing data structures. */
3923 parser->omp_declare_simd = NULL;
3924 parser->oacc_routine = NULL;
3926 /* Not declaring an implicit function template. */
3927 parser->auto_is_implicit_function_template_parm_p = false;
3928 parser->fully_implicit_function_template_p = false;
3929 parser->implicit_template_parms = 0;
3930 parser->implicit_template_scope = 0;
3932 /* Allow constrained-type-specifiers. */
3933 parser->prevent_constrained_type_specifiers = 0;
3935 /* We haven't yet seen an 'extern "C"'. */
3936 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3938 return parser;
3941 /* Create a cp_lexer structure which will emit the tokens in CACHE
3942 and push it onto the parser's lexer stack. This is used for delayed
3943 parsing of in-class method bodies and default arguments, and should
3944 not be confused with tentative parsing. */
3945 static void
3946 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3948 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3949 lexer->next = parser->lexer;
3950 parser->lexer = lexer;
3952 /* Move the current source position to that of the first token in the
3953 new lexer. */
3954 cp_lexer_set_source_position_from_token (lexer->next_token);
3957 /* Pop the top lexer off the parser stack. This is never used for the
3958 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3959 static void
3960 cp_parser_pop_lexer (cp_parser *parser)
3962 cp_lexer *lexer = parser->lexer;
3963 parser->lexer = lexer->next;
3964 cp_lexer_destroy (lexer);
3966 /* Put the current source position back where it was before this
3967 lexer was pushed. */
3968 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3971 /* Lexical conventions [gram.lex] */
3973 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3974 identifier. */
3976 static cp_expr
3977 cp_parser_identifier (cp_parser* parser)
3979 cp_token *token;
3981 /* Look for the identifier. */
3982 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3983 /* Return the value. */
3984 if (token)
3985 return cp_expr (token->u.value, token->location);
3986 else
3987 return error_mark_node;
3990 /* Parse a sequence of adjacent string constants. Returns a
3991 TREE_STRING representing the combined, nul-terminated string
3992 constant. If TRANSLATE is true, translate the string to the
3993 execution character set. If WIDE_OK is true, a wide string is
3994 invalid here.
3996 C++98 [lex.string] says that if a narrow string literal token is
3997 adjacent to a wide string literal token, the behavior is undefined.
3998 However, C99 6.4.5p4 says that this results in a wide string literal.
3999 We follow C99 here, for consistency with the C front end.
4001 This code is largely lifted from lex_string() in c-lex.c.
4003 FUTURE: ObjC++ will need to handle @-strings here. */
4004 static cp_expr
4005 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4006 bool lookup_udlit = true)
4008 tree value;
4009 size_t count;
4010 struct obstack str_ob;
4011 cpp_string str, istr, *strs;
4012 cp_token *tok;
4013 enum cpp_ttype type, curr_type;
4014 int have_suffix_p = 0;
4015 tree string_tree;
4016 tree suffix_id = NULL_TREE;
4017 bool curr_tok_is_userdef_p = false;
4019 tok = cp_lexer_peek_token (parser->lexer);
4020 if (!cp_parser_is_string_literal (tok))
4022 cp_parser_error (parser, "expected string-literal");
4023 return error_mark_node;
4026 location_t loc = tok->location;
4028 if (cpp_userdef_string_p (tok->type))
4030 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4031 curr_type = cpp_userdef_string_remove_type (tok->type);
4032 curr_tok_is_userdef_p = true;
4034 else
4036 string_tree = tok->u.value;
4037 curr_type = tok->type;
4039 type = curr_type;
4041 /* Try to avoid the overhead of creating and destroying an obstack
4042 for the common case of just one string. */
4043 if (!cp_parser_is_string_literal
4044 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4046 cp_lexer_consume_token (parser->lexer);
4048 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4049 str.len = TREE_STRING_LENGTH (string_tree);
4050 count = 1;
4052 if (curr_tok_is_userdef_p)
4054 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4055 have_suffix_p = 1;
4056 curr_type = cpp_userdef_string_remove_type (tok->type);
4058 else
4059 curr_type = tok->type;
4061 strs = &str;
4063 else
4065 location_t last_tok_loc = tok->location;
4066 gcc_obstack_init (&str_ob);
4067 count = 0;
4071 cp_lexer_consume_token (parser->lexer);
4072 count++;
4073 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4074 str.len = TREE_STRING_LENGTH (string_tree);
4076 if (curr_tok_is_userdef_p)
4078 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4079 if (have_suffix_p == 0)
4081 suffix_id = curr_suffix_id;
4082 have_suffix_p = 1;
4084 else if (have_suffix_p == 1
4085 && curr_suffix_id != suffix_id)
4087 error ("inconsistent user-defined literal suffixes"
4088 " %qD and %qD in string literal",
4089 suffix_id, curr_suffix_id);
4090 have_suffix_p = -1;
4092 curr_type = cpp_userdef_string_remove_type (tok->type);
4094 else
4095 curr_type = tok->type;
4097 if (type != curr_type)
4099 if (type == CPP_STRING)
4100 type = curr_type;
4101 else if (curr_type != CPP_STRING)
4103 rich_location rich_loc (line_table, tok->location);
4104 rich_loc.add_range (last_tok_loc, false);
4105 error_at (&rich_loc,
4106 "unsupported non-standard concatenation "
4107 "of string literals");
4111 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4113 last_tok_loc = tok->location;
4115 tok = cp_lexer_peek_token (parser->lexer);
4116 if (cpp_userdef_string_p (tok->type))
4118 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4119 curr_type = cpp_userdef_string_remove_type (tok->type);
4120 curr_tok_is_userdef_p = true;
4122 else
4124 string_tree = tok->u.value;
4125 curr_type = tok->type;
4126 curr_tok_is_userdef_p = false;
4129 while (cp_parser_is_string_literal (tok));
4131 /* A string literal built by concatenation has its caret=start at
4132 the start of the initial string, and its finish at the finish of
4133 the final string literal. */
4134 loc = make_location (loc, loc, get_finish (last_tok_loc));
4136 strs = (cpp_string *) obstack_finish (&str_ob);
4139 if (type != CPP_STRING && !wide_ok)
4141 cp_parser_error (parser, "a wide string is invalid in this context");
4142 type = CPP_STRING;
4145 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4146 (parse_in, strs, count, &istr, type))
4148 value = build_string (istr.len, (const char *)istr.text);
4149 free (CONST_CAST (unsigned char *, istr.text));
4151 switch (type)
4153 default:
4154 case CPP_STRING:
4155 case CPP_UTF8STRING:
4156 TREE_TYPE (value) = char_array_type_node;
4157 break;
4158 case CPP_STRING16:
4159 TREE_TYPE (value) = char16_array_type_node;
4160 break;
4161 case CPP_STRING32:
4162 TREE_TYPE (value) = char32_array_type_node;
4163 break;
4164 case CPP_WSTRING:
4165 TREE_TYPE (value) = wchar_array_type_node;
4166 break;
4169 value = fix_string_type (value);
4171 if (have_suffix_p)
4173 tree literal = build_userdef_literal (suffix_id, value,
4174 OT_NONE, NULL_TREE);
4175 if (lookup_udlit)
4176 value = cp_parser_userdef_string_literal (literal);
4177 else
4178 value = literal;
4181 else
4182 /* cpp_interpret_string has issued an error. */
4183 value = error_mark_node;
4185 if (count > 1)
4186 obstack_free (&str_ob, 0);
4188 return cp_expr (value, loc);
4191 /* Look up a literal operator with the name and the exact arguments. */
4193 static tree
4194 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4196 tree decl;
4197 decl = lookup_name (name);
4198 if (!decl || !is_overloaded_fn (decl))
4199 return error_mark_node;
4201 for (lkp_iterator iter (decl); iter; ++iter)
4203 unsigned int ix;
4204 bool found = true;
4205 tree fn = *iter;
4206 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4207 if (parmtypes != NULL_TREE)
4209 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4210 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4212 tree tparm = TREE_VALUE (parmtypes);
4213 tree targ = TREE_TYPE ((*args)[ix]);
4214 bool ptr = TYPE_PTR_P (tparm);
4215 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4216 if ((ptr || arr || !same_type_p (tparm, targ))
4217 && (!ptr || !arr
4218 || !same_type_p (TREE_TYPE (tparm),
4219 TREE_TYPE (targ))))
4220 found = false;
4222 if (found
4223 && ix == vec_safe_length (args)
4224 /* May be this should be sufficient_parms_p instead,
4225 depending on how exactly should user-defined literals
4226 work in presence of default arguments on the literal
4227 operator parameters. */
4228 && parmtypes == void_list_node)
4229 return decl;
4233 return error_mark_node;
4236 /* Parse a user-defined char constant. Returns a call to a user-defined
4237 literal operator taking the character as an argument. */
4239 static cp_expr
4240 cp_parser_userdef_char_literal (cp_parser *parser)
4242 cp_token *token = cp_lexer_consume_token (parser->lexer);
4243 tree literal = token->u.value;
4244 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4245 tree value = USERDEF_LITERAL_VALUE (literal);
4246 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4247 tree decl, result;
4249 /* Build up a call to the user-defined operator */
4250 /* Lookup the name we got back from the id-expression. */
4251 vec<tree, va_gc> *args = make_tree_vector ();
4252 vec_safe_push (args, value);
4253 decl = lookup_literal_operator (name, args);
4254 if (!decl || decl == error_mark_node)
4256 error ("unable to find character literal operator %qD with %qT argument",
4257 name, TREE_TYPE (value));
4258 release_tree_vector (args);
4259 return error_mark_node;
4261 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4262 release_tree_vector (args);
4263 return result;
4266 /* A subroutine of cp_parser_userdef_numeric_literal to
4267 create a char... template parameter pack from a string node. */
4269 static tree
4270 make_char_string_pack (tree value)
4272 tree charvec;
4273 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4274 const char *str = TREE_STRING_POINTER (value);
4275 int i, len = TREE_STRING_LENGTH (value) - 1;
4276 tree argvec = make_tree_vec (1);
4278 /* Fill in CHARVEC with all of the parameters. */
4279 charvec = make_tree_vec (len);
4280 for (i = 0; i < len; ++i)
4281 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4283 /* Build the argument packs. */
4284 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4286 TREE_VEC_ELT (argvec, 0) = argpack;
4288 return argvec;
4291 /* A subroutine of cp_parser_userdef_numeric_literal to
4292 create a char... template parameter pack from a string node. */
4294 static tree
4295 make_string_pack (tree value)
4297 tree charvec;
4298 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4299 const unsigned char *str
4300 = (const unsigned char *) TREE_STRING_POINTER (value);
4301 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4302 int len = TREE_STRING_LENGTH (value) / sz - 1;
4303 tree argvec = make_tree_vec (2);
4305 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4306 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4308 /* First template parm is character type. */
4309 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4311 /* Fill in CHARVEC with all of the parameters. */
4312 charvec = make_tree_vec (len);
4313 for (int i = 0; i < len; ++i)
4314 TREE_VEC_ELT (charvec, i)
4315 = double_int_to_tree (str_char_type_node,
4316 double_int::from_buffer (str + i * sz, sz));
4318 /* Build the argument packs. */
4319 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4321 TREE_VEC_ELT (argvec, 1) = argpack;
4323 return argvec;
4326 /* Parse a user-defined numeric constant. returns a call to a user-defined
4327 literal operator. */
4329 static cp_expr
4330 cp_parser_userdef_numeric_literal (cp_parser *parser)
4332 cp_token *token = cp_lexer_consume_token (parser->lexer);
4333 tree literal = token->u.value;
4334 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4335 tree value = USERDEF_LITERAL_VALUE (literal);
4336 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4337 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4338 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4339 tree decl, result;
4340 vec<tree, va_gc> *args;
4342 /* Look for a literal operator taking the exact type of numeric argument
4343 as the literal value. */
4344 args = make_tree_vector ();
4345 vec_safe_push (args, value);
4346 decl = lookup_literal_operator (name, args);
4347 if (decl && decl != error_mark_node)
4349 result = finish_call_expr (decl, &args, false, true,
4350 tf_warning_or_error);
4352 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4354 warning_at (token->location, OPT_Woverflow,
4355 "integer literal exceeds range of %qT type",
4356 long_long_unsigned_type_node);
4358 else
4360 if (overflow > 0)
4361 warning_at (token->location, OPT_Woverflow,
4362 "floating literal exceeds range of %qT type",
4363 long_double_type_node);
4364 else if (overflow < 0)
4365 warning_at (token->location, OPT_Woverflow,
4366 "floating literal truncated to zero");
4369 release_tree_vector (args);
4370 return result;
4372 release_tree_vector (args);
4374 /* If the numeric argument didn't work, look for a raw literal
4375 operator taking a const char* argument consisting of the number
4376 in string format. */
4377 args = make_tree_vector ();
4378 vec_safe_push (args, num_string);
4379 decl = lookup_literal_operator (name, args);
4380 if (decl && decl != error_mark_node)
4382 result = finish_call_expr (decl, &args, false, true,
4383 tf_warning_or_error);
4384 release_tree_vector (args);
4385 return result;
4387 release_tree_vector (args);
4389 /* If the raw literal didn't work, look for a non-type template
4390 function with parameter pack char.... Call the function with
4391 template parameter characters representing the number. */
4392 args = make_tree_vector ();
4393 decl = lookup_literal_operator (name, args);
4394 if (decl && decl != error_mark_node)
4396 tree tmpl_args = make_char_string_pack (num_string);
4397 decl = lookup_template_function (decl, tmpl_args);
4398 result = finish_call_expr (decl, &args, false, true,
4399 tf_warning_or_error);
4400 release_tree_vector (args);
4401 return result;
4404 release_tree_vector (args);
4406 /* In C++14 the standard library defines complex number suffixes that
4407 conflict with GNU extensions. Prefer them if <complex> is #included. */
4408 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4409 bool i14 = (cxx_dialect > cxx11
4410 && (id_equal (suffix_id, "i")
4411 || id_equal (suffix_id, "if")
4412 || id_equal (suffix_id, "il")));
4413 diagnostic_t kind = DK_ERROR;
4414 int opt = 0;
4416 if (i14 && ext)
4418 tree cxlit = lookup_qualified_name (std_node,
4419 get_identifier ("complex_literals"),
4420 0, false, false);
4421 if (cxlit == error_mark_node)
4423 /* No <complex>, so pedwarn and use GNU semantics. */
4424 kind = DK_PEDWARN;
4425 opt = OPT_Wpedantic;
4429 bool complained
4430 = emit_diagnostic (kind, input_location, opt,
4431 "unable to find numeric literal operator %qD", name);
4433 if (!complained)
4434 /* Don't inform either. */;
4435 else if (i14)
4437 inform (token->location, "add %<using namespace std::complex_literals%> "
4438 "(from <complex>) to enable the C++14 user-defined literal "
4439 "suffixes");
4440 if (ext)
4441 inform (token->location, "or use %<j%> instead of %<i%> for the "
4442 "GNU built-in suffix");
4444 else if (!ext)
4445 inform (token->location, "use -fext-numeric-literals "
4446 "to enable more built-in suffixes");
4448 if (kind == DK_ERROR)
4449 value = error_mark_node;
4450 else
4452 /* Use the built-in semantics. */
4453 tree type;
4454 if (id_equal (suffix_id, "i"))
4456 if (TREE_CODE (value) == INTEGER_CST)
4457 type = integer_type_node;
4458 else
4459 type = double_type_node;
4461 else if (id_equal (suffix_id, "if"))
4462 type = float_type_node;
4463 else /* if (id_equal (suffix_id, "il")) */
4464 type = long_double_type_node;
4466 value = build_complex (build_complex_type (type),
4467 fold_convert (type, integer_zero_node),
4468 fold_convert (type, value));
4471 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4472 /* Avoid repeated diagnostics. */
4473 token->u.value = value;
4474 return value;
4477 /* Parse a user-defined string constant. Returns a call to a user-defined
4478 literal operator taking a character pointer and the length of the string
4479 as arguments. */
4481 static tree
4482 cp_parser_userdef_string_literal (tree literal)
4484 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4485 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4486 tree value = USERDEF_LITERAL_VALUE (literal);
4487 int len = TREE_STRING_LENGTH (value)
4488 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4489 tree decl, result;
4490 vec<tree, va_gc> *args;
4492 /* Build up a call to the user-defined operator. */
4493 /* Lookup the name we got back from the id-expression. */
4494 args = make_tree_vector ();
4495 vec_safe_push (args, value);
4496 vec_safe_push (args, build_int_cst (size_type_node, len));
4497 decl = lookup_literal_operator (name, args);
4499 if (decl && decl != error_mark_node)
4501 result = finish_call_expr (decl, &args, false, true,
4502 tf_warning_or_error);
4503 release_tree_vector (args);
4504 return result;
4506 release_tree_vector (args);
4508 /* Look for a template function with typename parameter CharT
4509 and parameter pack CharT... Call the function with
4510 template parameter characters representing the string. */
4511 args = make_tree_vector ();
4512 decl = lookup_literal_operator (name, args);
4513 if (decl && decl != error_mark_node)
4515 tree tmpl_args = make_string_pack (value);
4516 decl = lookup_template_function (decl, tmpl_args);
4517 result = finish_call_expr (decl, &args, false, true,
4518 tf_warning_or_error);
4519 release_tree_vector (args);
4520 return result;
4522 release_tree_vector (args);
4524 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4525 name, TREE_TYPE (value), size_type_node);
4526 return error_mark_node;
4530 /* Basic concepts [gram.basic] */
4532 /* Parse a translation-unit.
4534 translation-unit:
4535 declaration-seq [opt]
4537 Returns TRUE if all went well. */
4539 static bool
4540 cp_parser_translation_unit (cp_parser* parser)
4542 /* The address of the first non-permanent object on the declarator
4543 obstack. */
4544 static void *declarator_obstack_base;
4546 bool success;
4548 /* Create the declarator obstack, if necessary. */
4549 if (!cp_error_declarator)
4551 gcc_obstack_init (&declarator_obstack);
4552 /* Create the error declarator. */
4553 cp_error_declarator = make_declarator (cdk_error);
4554 /* Create the empty parameter list. */
4555 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4556 UNKNOWN_LOCATION);
4557 /* Remember where the base of the declarator obstack lies. */
4558 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4561 cp_parser_declaration_seq_opt (parser);
4563 /* If there are no tokens left then all went well. */
4564 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4566 /* Get rid of the token array; we don't need it any more. */
4567 cp_lexer_destroy (parser->lexer);
4568 parser->lexer = NULL;
4570 /* This file might have been a context that's implicitly extern
4571 "C". If so, pop the lang context. (Only relevant for PCH.) */
4572 if (parser->implicit_extern_c)
4574 pop_lang_context ();
4575 parser->implicit_extern_c = false;
4578 /* Finish up. */
4579 finish_translation_unit ();
4581 success = true;
4583 else
4585 cp_parser_error (parser, "expected declaration");
4586 success = false;
4589 /* Make sure the declarator obstack was fully cleaned up. */
4590 gcc_assert (obstack_next_free (&declarator_obstack)
4591 == declarator_obstack_base);
4593 /* All went well. */
4594 return success;
4597 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4598 decltype context. */
4600 static inline tsubst_flags_t
4601 complain_flags (bool decltype_p)
4603 tsubst_flags_t complain = tf_warning_or_error;
4604 if (decltype_p)
4605 complain |= tf_decltype;
4606 return complain;
4609 /* We're about to parse a collection of statements. If we're currently
4610 parsing tentatively, set up a firewall so that any nested
4611 cp_parser_commit_to_tentative_parse won't affect the current context. */
4613 static cp_token_position
4614 cp_parser_start_tentative_firewall (cp_parser *parser)
4616 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4617 return 0;
4619 cp_parser_parse_tentatively (parser);
4620 cp_parser_commit_to_topmost_tentative_parse (parser);
4621 return cp_lexer_token_position (parser->lexer, false);
4624 /* We've finished parsing the collection of statements. Wrap up the
4625 firewall and replace the relevant tokens with the parsed form. */
4627 static void
4628 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4629 tree expr)
4631 if (!start)
4632 return;
4634 /* Finish the firewall level. */
4635 cp_parser_parse_definitely (parser);
4636 /* And remember the result of the parse for when we try again. */
4637 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4638 token->type = CPP_PREPARSED_EXPR;
4639 token->u.value = expr;
4640 token->keyword = RID_MAX;
4641 cp_lexer_purge_tokens_after (parser->lexer, start);
4644 /* Like the above functions, but let the user modify the tokens. Used by
4645 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4646 later parses, so it makes sense to localize the effects of
4647 cp_parser_commit_to_tentative_parse. */
4649 struct tentative_firewall
4651 cp_parser *parser;
4652 bool set;
4654 tentative_firewall (cp_parser *p): parser(p)
4656 /* If we're currently parsing tentatively, start a committed level as a
4657 firewall and then an inner tentative parse. */
4658 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4660 cp_parser_parse_tentatively (parser);
4661 cp_parser_commit_to_topmost_tentative_parse (parser);
4662 cp_parser_parse_tentatively (parser);
4666 ~tentative_firewall()
4668 if (set)
4670 /* Finish the inner tentative parse and the firewall, propagating any
4671 uncommitted error state to the outer tentative parse. */
4672 bool err = cp_parser_error_occurred (parser);
4673 cp_parser_parse_definitely (parser);
4674 cp_parser_parse_definitely (parser);
4675 if (err)
4676 cp_parser_simulate_error (parser);
4681 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4682 This class is for tracking such a matching pair of symbols.
4683 In particular, it tracks the location of the first token,
4684 so that if the second token is missing, we can highlight the
4685 location of the first token when notifying the user about the
4686 problem. */
4688 template <typename traits_t>
4689 class token_pair
4691 public:
4692 /* token_pair's ctor. */
4693 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4695 /* If the next token is the opening symbol for this pair, consume it and
4696 return true.
4697 Otherwise, issue an error and return false.
4698 In either case, record the location of the opening token. */
4700 bool require_open (cp_parser *parser)
4702 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4703 return cp_parser_require (parser, traits_t::open_token_type,
4704 traits_t::required_token_open);
4707 /* Consume the next token from PARSER, recording its location as
4708 that of the opening token within the pair. */
4710 cp_token * consume_open (cp_parser *parser)
4712 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4713 gcc_assert (tok->type == traits_t::open_token_type);
4714 m_open_loc = tok->location;
4715 return tok;
4718 /* If the next token is the closing symbol for this pair, consume it
4719 and return it.
4720 Otherwise, issue an error, highlighting the location of the
4721 corresponding opening token, and return NULL. */
4723 cp_token *require_close (cp_parser *parser) const
4725 return cp_parser_require (parser, traits_t::close_token_type,
4726 traits_t::required_token_close,
4727 m_open_loc);
4730 private:
4731 location_t m_open_loc;
4734 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4736 struct matching_paren_traits
4738 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4739 static const enum required_token required_token_open = RT_OPEN_PAREN;
4740 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4741 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4744 /* "matching_parens" is a token_pair<T> class for tracking matching
4745 pairs of parentheses. */
4747 typedef token_pair<matching_paren_traits> matching_parens;
4749 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4751 struct matching_brace_traits
4753 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4754 static const enum required_token required_token_open = RT_OPEN_BRACE;
4755 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4756 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4759 /* "matching_braces" is a token_pair<T> class for tracking matching
4760 pairs of braces. */
4762 typedef token_pair<matching_brace_traits> matching_braces;
4765 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4766 enclosing parentheses. */
4768 static cp_expr
4769 cp_parser_statement_expr (cp_parser *parser)
4771 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4773 /* Consume the '('. */
4774 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4775 matching_parens parens;
4776 parens.consume_open (parser);
4777 /* Start the statement-expression. */
4778 tree expr = begin_stmt_expr ();
4779 /* Parse the compound-statement. */
4780 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4781 /* Finish up. */
4782 expr = finish_stmt_expr (expr, false);
4783 /* Consume the ')'. */
4784 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4785 if (!parens.require_close (parser))
4786 cp_parser_skip_to_end_of_statement (parser);
4788 cp_parser_end_tentative_firewall (parser, start, expr);
4789 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4790 return cp_expr (expr, combined_loc);
4793 /* Expressions [gram.expr] */
4795 /* Parse a fold-operator.
4797 fold-operator:
4798 - * / % ^ & | = < > << >>
4799 = -= *= /= %= ^= &= |= <<= >>=
4800 == != <= >= && || , .* ->*
4802 This returns the tree code corresponding to the matched operator
4803 as an int. When the current token matches a compound assignment
4804 opertor, the resulting tree code is the negative value of the
4805 non-assignment operator. */
4807 static int
4808 cp_parser_fold_operator (cp_token *token)
4810 switch (token->type)
4812 case CPP_PLUS: return PLUS_EXPR;
4813 case CPP_MINUS: return MINUS_EXPR;
4814 case CPP_MULT: return MULT_EXPR;
4815 case CPP_DIV: return TRUNC_DIV_EXPR;
4816 case CPP_MOD: return TRUNC_MOD_EXPR;
4817 case CPP_XOR: return BIT_XOR_EXPR;
4818 case CPP_AND: return BIT_AND_EXPR;
4819 case CPP_OR: return BIT_IOR_EXPR;
4820 case CPP_LSHIFT: return LSHIFT_EXPR;
4821 case CPP_RSHIFT: return RSHIFT_EXPR;
4823 case CPP_EQ: return -NOP_EXPR;
4824 case CPP_PLUS_EQ: return -PLUS_EXPR;
4825 case CPP_MINUS_EQ: return -MINUS_EXPR;
4826 case CPP_MULT_EQ: return -MULT_EXPR;
4827 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4828 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4829 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4830 case CPP_AND_EQ: return -BIT_AND_EXPR;
4831 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4832 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4833 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4835 case CPP_EQ_EQ: return EQ_EXPR;
4836 case CPP_NOT_EQ: return NE_EXPR;
4837 case CPP_LESS: return LT_EXPR;
4838 case CPP_GREATER: return GT_EXPR;
4839 case CPP_LESS_EQ: return LE_EXPR;
4840 case CPP_GREATER_EQ: return GE_EXPR;
4842 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4843 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4845 case CPP_COMMA: return COMPOUND_EXPR;
4847 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4848 case CPP_DEREF_STAR: return MEMBER_REF;
4850 default: return ERROR_MARK;
4854 /* Returns true if CODE indicates a binary expression, which is not allowed in
4855 the LHS of a fold-expression. More codes will need to be added to use this
4856 function in other contexts. */
4858 static bool
4859 is_binary_op (tree_code code)
4861 switch (code)
4863 case PLUS_EXPR:
4864 case POINTER_PLUS_EXPR:
4865 case MINUS_EXPR:
4866 case MULT_EXPR:
4867 case TRUNC_DIV_EXPR:
4868 case TRUNC_MOD_EXPR:
4869 case BIT_XOR_EXPR:
4870 case BIT_AND_EXPR:
4871 case BIT_IOR_EXPR:
4872 case LSHIFT_EXPR:
4873 case RSHIFT_EXPR:
4875 case MODOP_EXPR:
4877 case EQ_EXPR:
4878 case NE_EXPR:
4879 case LE_EXPR:
4880 case GE_EXPR:
4881 case LT_EXPR:
4882 case GT_EXPR:
4884 case TRUTH_ANDIF_EXPR:
4885 case TRUTH_ORIF_EXPR:
4887 case COMPOUND_EXPR:
4889 case DOTSTAR_EXPR:
4890 case MEMBER_REF:
4891 return true;
4893 default:
4894 return false;
4898 /* If the next token is a suitable fold operator, consume it and return as
4899 the function above. */
4901 static int
4902 cp_parser_fold_operator (cp_parser *parser)
4904 cp_token* token = cp_lexer_peek_token (parser->lexer);
4905 int code = cp_parser_fold_operator (token);
4906 if (code != ERROR_MARK)
4907 cp_lexer_consume_token (parser->lexer);
4908 return code;
4911 /* Parse a fold-expression.
4913 fold-expression:
4914 ( ... folding-operator cast-expression)
4915 ( cast-expression folding-operator ... )
4916 ( cast-expression folding operator ... folding-operator cast-expression)
4918 Note that the '(' and ')' are matched in primary expression. */
4920 static cp_expr
4921 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4923 cp_id_kind pidk;
4925 // Left fold.
4926 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4928 cp_lexer_consume_token (parser->lexer);
4929 int op = cp_parser_fold_operator (parser);
4930 if (op == ERROR_MARK)
4932 cp_parser_error (parser, "expected binary operator");
4933 return error_mark_node;
4936 tree expr = cp_parser_cast_expression (parser, false, false,
4937 false, &pidk);
4938 if (expr == error_mark_node)
4939 return error_mark_node;
4940 return finish_left_unary_fold_expr (expr, op);
4943 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4944 int op = cp_parser_fold_operator (parser);
4945 if (op == ERROR_MARK)
4947 cp_parser_error (parser, "expected binary operator");
4948 return error_mark_node;
4951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4953 cp_parser_error (parser, "expected ...");
4954 return error_mark_node;
4956 cp_lexer_consume_token (parser->lexer);
4958 /* The operands of a fold-expression are cast-expressions, so binary or
4959 conditional expressions are not allowed. We check this here to avoid
4960 tentative parsing. */
4961 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4962 /* OK, the expression was parenthesized. */;
4963 else if (is_binary_op (TREE_CODE (expr1)))
4964 error_at (location_of (expr1),
4965 "binary expression in operand of fold-expression");
4966 else if (TREE_CODE (expr1) == COND_EXPR
4967 || (REFERENCE_REF_P (expr1)
4968 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4969 error_at (location_of (expr1),
4970 "conditional expression in operand of fold-expression");
4972 // Right fold.
4973 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4974 return finish_right_unary_fold_expr (expr1, op);
4976 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4978 cp_parser_error (parser, "mismatched operator in fold-expression");
4979 return error_mark_node;
4981 cp_lexer_consume_token (parser->lexer);
4983 // Binary left or right fold.
4984 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4985 if (expr2 == error_mark_node)
4986 return error_mark_node;
4987 return finish_binary_fold_expr (expr1, expr2, op);
4990 /* Parse a primary-expression.
4992 primary-expression:
4993 literal
4994 this
4995 ( expression )
4996 id-expression
4997 lambda-expression (C++11)
4999 GNU Extensions:
5001 primary-expression:
5002 ( compound-statement )
5003 __builtin_va_arg ( assignment-expression , type-id )
5004 __builtin_offsetof ( type-id , offsetof-expression )
5006 C++ Extensions:
5007 __has_nothrow_assign ( type-id )
5008 __has_nothrow_constructor ( type-id )
5009 __has_nothrow_copy ( type-id )
5010 __has_trivial_assign ( type-id )
5011 __has_trivial_constructor ( type-id )
5012 __has_trivial_copy ( type-id )
5013 __has_trivial_destructor ( type-id )
5014 __has_virtual_destructor ( type-id )
5015 __is_abstract ( type-id )
5016 __is_base_of ( type-id , type-id )
5017 __is_class ( type-id )
5018 __is_empty ( type-id )
5019 __is_enum ( type-id )
5020 __is_final ( type-id )
5021 __is_literal_type ( type-id )
5022 __is_pod ( type-id )
5023 __is_polymorphic ( type-id )
5024 __is_std_layout ( type-id )
5025 __is_trivial ( type-id )
5026 __is_union ( type-id )
5028 Objective-C++ Extension:
5030 primary-expression:
5031 objc-expression
5033 literal:
5034 __null
5036 ADDRESS_P is true iff this expression was immediately preceded by
5037 "&" and therefore might denote a pointer-to-member. CAST_P is true
5038 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5039 true iff this expression is a template argument.
5041 Returns a representation of the expression. Upon return, *IDK
5042 indicates what kind of id-expression (if any) was present. */
5044 static cp_expr
5045 cp_parser_primary_expression (cp_parser *parser,
5046 bool address_p,
5047 bool cast_p,
5048 bool template_arg_p,
5049 bool decltype_p,
5050 cp_id_kind *idk)
5052 cp_token *token = NULL;
5054 /* Assume the primary expression is not an id-expression. */
5055 *idk = CP_ID_KIND_NONE;
5057 /* Peek at the next token. */
5058 token = cp_lexer_peek_token (parser->lexer);
5059 switch ((int) token->type)
5061 /* literal:
5062 integer-literal
5063 character-literal
5064 floating-literal
5065 string-literal
5066 boolean-literal
5067 pointer-literal
5068 user-defined-literal */
5069 case CPP_CHAR:
5070 case CPP_CHAR16:
5071 case CPP_CHAR32:
5072 case CPP_WCHAR:
5073 case CPP_UTF8CHAR:
5074 case CPP_NUMBER:
5075 case CPP_PREPARSED_EXPR:
5076 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5077 return cp_parser_userdef_numeric_literal (parser);
5078 token = cp_lexer_consume_token (parser->lexer);
5079 if (TREE_CODE (token->u.value) == FIXED_CST)
5081 error_at (token->location,
5082 "fixed-point types not supported in C++");
5083 return error_mark_node;
5085 /* Floating-point literals are only allowed in an integral
5086 constant expression if they are cast to an integral or
5087 enumeration type. */
5088 if (TREE_CODE (token->u.value) == REAL_CST
5089 && parser->integral_constant_expression_p
5090 && pedantic)
5092 /* CAST_P will be set even in invalid code like "int(2.7 +
5093 ...)". Therefore, we have to check that the next token
5094 is sure to end the cast. */
5095 if (cast_p)
5097 cp_token *next_token;
5099 next_token = cp_lexer_peek_token (parser->lexer);
5100 if (/* The comma at the end of an
5101 enumerator-definition. */
5102 next_token->type != CPP_COMMA
5103 /* The curly brace at the end of an enum-specifier. */
5104 && next_token->type != CPP_CLOSE_BRACE
5105 /* The end of a statement. */
5106 && next_token->type != CPP_SEMICOLON
5107 /* The end of the cast-expression. */
5108 && next_token->type != CPP_CLOSE_PAREN
5109 /* The end of an array bound. */
5110 && next_token->type != CPP_CLOSE_SQUARE
5111 /* The closing ">" in a template-argument-list. */
5112 && (next_token->type != CPP_GREATER
5113 || parser->greater_than_is_operator_p)
5114 /* C++0x only: A ">>" treated like two ">" tokens,
5115 in a template-argument-list. */
5116 && (next_token->type != CPP_RSHIFT
5117 || (cxx_dialect == cxx98)
5118 || parser->greater_than_is_operator_p))
5119 cast_p = false;
5122 /* If we are within a cast, then the constraint that the
5123 cast is to an integral or enumeration type will be
5124 checked at that point. If we are not within a cast, then
5125 this code is invalid. */
5126 if (!cast_p)
5127 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5129 return cp_expr (token->u.value, token->location);
5131 case CPP_CHAR_USERDEF:
5132 case CPP_CHAR16_USERDEF:
5133 case CPP_CHAR32_USERDEF:
5134 case CPP_WCHAR_USERDEF:
5135 case CPP_UTF8CHAR_USERDEF:
5136 return cp_parser_userdef_char_literal (parser);
5138 case CPP_STRING:
5139 case CPP_STRING16:
5140 case CPP_STRING32:
5141 case CPP_WSTRING:
5142 case CPP_UTF8STRING:
5143 case CPP_STRING_USERDEF:
5144 case CPP_STRING16_USERDEF:
5145 case CPP_STRING32_USERDEF:
5146 case CPP_WSTRING_USERDEF:
5147 case CPP_UTF8STRING_USERDEF:
5148 /* ??? Should wide strings be allowed when parser->translate_strings_p
5149 is false (i.e. in attributes)? If not, we can kill the third
5150 argument to cp_parser_string_literal. */
5151 return cp_parser_string_literal (parser,
5152 parser->translate_strings_p,
5153 true);
5155 case CPP_OPEN_PAREN:
5156 /* If we see `( { ' then we are looking at the beginning of
5157 a GNU statement-expression. */
5158 if (cp_parser_allow_gnu_extensions_p (parser)
5159 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5161 /* Statement-expressions are not allowed by the standard. */
5162 pedwarn (token->location, OPT_Wpedantic,
5163 "ISO C++ forbids braced-groups within expressions");
5165 /* And they're not allowed outside of a function-body; you
5166 cannot, for example, write:
5168 int i = ({ int j = 3; j + 1; });
5170 at class or namespace scope. */
5171 if (!parser->in_function_body
5172 || parser->in_template_argument_list_p)
5174 error_at (token->location,
5175 "statement-expressions are not allowed outside "
5176 "functions nor in template-argument lists");
5177 cp_parser_skip_to_end_of_block_or_statement (parser);
5178 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5179 cp_lexer_consume_token (parser->lexer);
5180 return error_mark_node;
5182 else
5183 return cp_parser_statement_expr (parser);
5185 /* Otherwise it's a normal parenthesized expression. */
5187 cp_expr expr;
5188 bool saved_greater_than_is_operator_p;
5190 location_t open_paren_loc = token->location;
5192 /* Consume the `('. */
5193 matching_parens parens;
5194 parens.consume_open (parser);
5195 /* Within a parenthesized expression, a `>' token is always
5196 the greater-than operator. */
5197 saved_greater_than_is_operator_p
5198 = parser->greater_than_is_operator_p;
5199 parser->greater_than_is_operator_p = true;
5201 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5202 /* Left fold expression. */
5203 expr = NULL_TREE;
5204 else
5205 /* Parse the parenthesized expression. */
5206 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5208 token = cp_lexer_peek_token (parser->lexer);
5209 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5211 expr = cp_parser_fold_expression (parser, expr);
5212 if (expr != error_mark_node
5213 && cxx_dialect < cxx17
5214 && !in_system_header_at (input_location))
5215 pedwarn (input_location, 0, "fold-expressions only available "
5216 "with -std=c++17 or -std=gnu++17");
5218 else
5219 /* Let the front end know that this expression was
5220 enclosed in parentheses. This matters in case, for
5221 example, the expression is of the form `A::B', since
5222 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5223 not. */
5224 expr = finish_parenthesized_expr (expr);
5226 /* DR 705: Wrapping an unqualified name in parentheses
5227 suppresses arg-dependent lookup. We want to pass back
5228 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5229 (c++/37862), but none of the others. */
5230 if (*idk != CP_ID_KIND_QUALIFIED)
5231 *idk = CP_ID_KIND_NONE;
5233 /* The `>' token might be the end of a template-id or
5234 template-parameter-list now. */
5235 parser->greater_than_is_operator_p
5236 = saved_greater_than_is_operator_p;
5238 /* Consume the `)'. */
5239 token = cp_lexer_peek_token (parser->lexer);
5240 location_t close_paren_loc = token->location;
5241 expr.set_range (open_paren_loc, close_paren_loc);
5242 if (!parens.require_close (parser)
5243 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5244 cp_parser_skip_to_end_of_statement (parser);
5246 return expr;
5249 case CPP_OPEN_SQUARE:
5251 if (c_dialect_objc ())
5253 /* We might have an Objective-C++ message. */
5254 cp_parser_parse_tentatively (parser);
5255 tree msg = cp_parser_objc_message_expression (parser);
5256 /* If that works out, we're done ... */
5257 if (cp_parser_parse_definitely (parser))
5258 return msg;
5259 /* ... else, fall though to see if it's a lambda. */
5261 cp_expr lam = cp_parser_lambda_expression (parser);
5262 /* Don't warn about a failed tentative parse. */
5263 if (cp_parser_error_occurred (parser))
5264 return error_mark_node;
5265 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5266 return lam;
5269 case CPP_OBJC_STRING:
5270 if (c_dialect_objc ())
5271 /* We have an Objective-C++ string literal. */
5272 return cp_parser_objc_expression (parser);
5273 cp_parser_error (parser, "expected primary-expression");
5274 return error_mark_node;
5276 case CPP_KEYWORD:
5277 switch (token->keyword)
5279 /* These two are the boolean literals. */
5280 case RID_TRUE:
5281 cp_lexer_consume_token (parser->lexer);
5282 return cp_expr (boolean_true_node, token->location);
5283 case RID_FALSE:
5284 cp_lexer_consume_token (parser->lexer);
5285 return cp_expr (boolean_false_node, token->location);
5287 /* The `__null' literal. */
5288 case RID_NULL:
5289 cp_lexer_consume_token (parser->lexer);
5290 return cp_expr (null_node, token->location);
5292 /* The `nullptr' literal. */
5293 case RID_NULLPTR:
5294 cp_lexer_consume_token (parser->lexer);
5295 return cp_expr (nullptr_node, token->location);
5297 /* Recognize the `this' keyword. */
5298 case RID_THIS:
5299 cp_lexer_consume_token (parser->lexer);
5300 if (parser->local_variables_forbidden_p)
5302 error_at (token->location,
5303 "%<this%> may not be used in this context");
5304 return error_mark_node;
5306 /* Pointers cannot appear in constant-expressions. */
5307 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5308 return error_mark_node;
5309 return cp_expr (finish_this_expr (), token->location);
5311 /* The `operator' keyword can be the beginning of an
5312 id-expression. */
5313 case RID_OPERATOR:
5314 goto id_expression;
5316 case RID_FUNCTION_NAME:
5317 case RID_PRETTY_FUNCTION_NAME:
5318 case RID_C99_FUNCTION_NAME:
5320 non_integral_constant name;
5322 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5323 __func__ are the names of variables -- but they are
5324 treated specially. Therefore, they are handled here,
5325 rather than relying on the generic id-expression logic
5326 below. Grammatically, these names are id-expressions.
5328 Consume the token. */
5329 token = cp_lexer_consume_token (parser->lexer);
5331 switch (token->keyword)
5333 case RID_FUNCTION_NAME:
5334 name = NIC_FUNC_NAME;
5335 break;
5336 case RID_PRETTY_FUNCTION_NAME:
5337 name = NIC_PRETTY_FUNC;
5338 break;
5339 case RID_C99_FUNCTION_NAME:
5340 name = NIC_C99_FUNC;
5341 break;
5342 default:
5343 gcc_unreachable ();
5346 if (cp_parser_non_integral_constant_expression (parser, name))
5347 return error_mark_node;
5349 /* Look up the name. */
5350 return finish_fname (token->u.value);
5353 case RID_VA_ARG:
5355 tree expression;
5356 tree type;
5357 source_location type_location;
5358 location_t start_loc
5359 = cp_lexer_peek_token (parser->lexer)->location;
5360 /* The `__builtin_va_arg' construct is used to handle
5361 `va_arg'. Consume the `__builtin_va_arg' token. */
5362 cp_lexer_consume_token (parser->lexer);
5363 /* Look for the opening `('. */
5364 matching_parens parens;
5365 parens.require_open (parser);
5366 /* Now, parse the assignment-expression. */
5367 expression = cp_parser_assignment_expression (parser);
5368 /* Look for the `,'. */
5369 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5370 type_location = cp_lexer_peek_token (parser->lexer)->location;
5371 /* Parse the type-id. */
5373 type_id_in_expr_sentinel s (parser);
5374 type = cp_parser_type_id (parser);
5376 /* Look for the closing `)'. */
5377 location_t finish_loc
5378 = cp_lexer_peek_token (parser->lexer)->location;
5379 parens.require_close (parser);
5380 /* Using `va_arg' in a constant-expression is not
5381 allowed. */
5382 if (cp_parser_non_integral_constant_expression (parser,
5383 NIC_VA_ARG))
5384 return error_mark_node;
5385 /* Construct a location of the form:
5386 __builtin_va_arg (v, int)
5387 ~~~~~~~~~~~~~~~~~~~~~^~~~
5388 with the caret at the type, ranging from the start of the
5389 "__builtin_va_arg" token to the close paren. */
5390 location_t combined_loc
5391 = make_location (type_location, start_loc, finish_loc);
5392 return build_x_va_arg (combined_loc, expression, type);
5395 case RID_OFFSETOF:
5396 return cp_parser_builtin_offsetof (parser);
5398 case RID_HAS_NOTHROW_ASSIGN:
5399 case RID_HAS_NOTHROW_CONSTRUCTOR:
5400 case RID_HAS_NOTHROW_COPY:
5401 case RID_HAS_TRIVIAL_ASSIGN:
5402 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5403 case RID_HAS_TRIVIAL_COPY:
5404 case RID_HAS_TRIVIAL_DESTRUCTOR:
5405 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5406 case RID_HAS_VIRTUAL_DESTRUCTOR:
5407 case RID_IS_ABSTRACT:
5408 case RID_IS_AGGREGATE:
5409 case RID_IS_BASE_OF:
5410 case RID_IS_CLASS:
5411 case RID_IS_EMPTY:
5412 case RID_IS_ENUM:
5413 case RID_IS_FINAL:
5414 case RID_IS_LITERAL_TYPE:
5415 case RID_IS_POD:
5416 case RID_IS_POLYMORPHIC:
5417 case RID_IS_SAME_AS:
5418 case RID_IS_STD_LAYOUT:
5419 case RID_IS_TRIVIAL:
5420 case RID_IS_TRIVIALLY_ASSIGNABLE:
5421 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5422 case RID_IS_TRIVIALLY_COPYABLE:
5423 case RID_IS_UNION:
5424 case RID_IS_ASSIGNABLE:
5425 case RID_IS_CONSTRUCTIBLE:
5426 return cp_parser_trait_expr (parser, token->keyword);
5428 // C++ concepts
5429 case RID_REQUIRES:
5430 return cp_parser_requires_expression (parser);
5432 /* Objective-C++ expressions. */
5433 case RID_AT_ENCODE:
5434 case RID_AT_PROTOCOL:
5435 case RID_AT_SELECTOR:
5436 return cp_parser_objc_expression (parser);
5438 case RID_TEMPLATE:
5439 if (parser->in_function_body
5440 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5441 == CPP_LESS))
5443 error_at (token->location,
5444 "a template declaration cannot appear at block scope");
5445 cp_parser_skip_to_end_of_block_or_statement (parser);
5446 return error_mark_node;
5448 /* FALLTHRU */
5449 default:
5450 cp_parser_error (parser, "expected primary-expression");
5451 return error_mark_node;
5454 /* An id-expression can start with either an identifier, a
5455 `::' as the beginning of a qualified-id, or the "operator"
5456 keyword. */
5457 case CPP_NAME:
5458 case CPP_SCOPE:
5459 case CPP_TEMPLATE_ID:
5460 case CPP_NESTED_NAME_SPECIFIER:
5462 id_expression:
5463 cp_expr id_expression;
5464 cp_expr decl;
5465 const char *error_msg;
5466 bool template_p;
5467 bool done;
5468 cp_token *id_expr_token;
5470 /* Parse the id-expression. */
5471 id_expression
5472 = cp_parser_id_expression (parser,
5473 /*template_keyword_p=*/false,
5474 /*check_dependency_p=*/true,
5475 &template_p,
5476 /*declarator_p=*/false,
5477 /*optional_p=*/false);
5478 if (id_expression == error_mark_node)
5479 return error_mark_node;
5480 id_expr_token = token;
5481 token = cp_lexer_peek_token (parser->lexer);
5482 done = (token->type != CPP_OPEN_SQUARE
5483 && token->type != CPP_OPEN_PAREN
5484 && token->type != CPP_DOT
5485 && token->type != CPP_DEREF
5486 && token->type != CPP_PLUS_PLUS
5487 && token->type != CPP_MINUS_MINUS);
5488 /* If we have a template-id, then no further lookup is
5489 required. If the template-id was for a template-class, we
5490 will sometimes have a TYPE_DECL at this point. */
5491 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5492 || TREE_CODE (id_expression) == TYPE_DECL)
5493 decl = id_expression;
5494 /* Look up the name. */
5495 else
5497 tree ambiguous_decls;
5499 /* If we already know that this lookup is ambiguous, then
5500 we've already issued an error message; there's no reason
5501 to check again. */
5502 if (id_expr_token->type == CPP_NAME
5503 && id_expr_token->error_reported)
5505 cp_parser_simulate_error (parser);
5506 return error_mark_node;
5509 decl = cp_parser_lookup_name (parser, id_expression,
5510 none_type,
5511 template_p,
5512 /*is_namespace=*/false,
5513 /*check_dependency=*/true,
5514 &ambiguous_decls,
5515 id_expr_token->location);
5516 /* If the lookup was ambiguous, an error will already have
5517 been issued. */
5518 if (ambiguous_decls)
5519 return error_mark_node;
5521 /* In Objective-C++, we may have an Objective-C 2.0
5522 dot-syntax for classes here. */
5523 if (c_dialect_objc ()
5524 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5525 && TREE_CODE (decl) == TYPE_DECL
5526 && objc_is_class_name (decl))
5528 tree component;
5529 cp_lexer_consume_token (parser->lexer);
5530 component = cp_parser_identifier (parser);
5531 if (component == error_mark_node)
5532 return error_mark_node;
5534 tree result = objc_build_class_component_ref (id_expression,
5535 component);
5536 /* Build a location of the form:
5537 expr.component
5538 ~~~~~^~~~~~~~~
5539 with caret at the start of the component name (at
5540 input_location), ranging from the start of the id_expression
5541 to the end of the component name. */
5542 location_t combined_loc
5543 = make_location (input_location, id_expression.get_start (),
5544 get_finish (input_location));
5545 protected_set_expr_location (result, combined_loc);
5546 return result;
5549 /* In Objective-C++, an instance variable (ivar) may be preferred
5550 to whatever cp_parser_lookup_name() found.
5551 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5552 rest of c-family, we have to do a little extra work to preserve
5553 any location information in cp_expr "decl". Given that
5554 objc_lookup_ivar is implemented in "c-family" and "objc", we
5555 have a trip through the pure "tree" type, rather than cp_expr.
5556 Naively copying it back to "decl" would implicitly give the
5557 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5558 store an EXPR_LOCATION. Hence we only update "decl" (and
5559 hence its location_t) if we get back a different tree node. */
5560 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5561 id_expression);
5562 if (decl_tree != decl.get_value ())
5563 decl = cp_expr (decl_tree);
5565 /* If name lookup gives us a SCOPE_REF, then the
5566 qualifying scope was dependent. */
5567 if (TREE_CODE (decl) == SCOPE_REF)
5569 /* At this point, we do not know if DECL is a valid
5570 integral constant expression. We assume that it is
5571 in fact such an expression, so that code like:
5573 template <int N> struct A {
5574 int a[B<N>::i];
5577 is accepted. At template-instantiation time, we
5578 will check that B<N>::i is actually a constant. */
5579 return decl;
5581 /* Check to see if DECL is a local variable in a context
5582 where that is forbidden. */
5583 if (parser->local_variables_forbidden_p
5584 && local_variable_p (decl))
5586 /* It might be that we only found DECL because we are
5587 trying to be generous with pre-ISO scoping rules.
5588 For example, consider:
5590 int i;
5591 void g() {
5592 for (int i = 0; i < 10; ++i) {}
5593 extern void f(int j = i);
5596 Here, name look up will originally find the out
5597 of scope `i'. We need to issue a warning message,
5598 but then use the global `i'. */
5599 decl = check_for_out_of_scope_variable (decl);
5600 if (local_variable_p (decl))
5602 error_at (id_expr_token->location,
5603 "local variable %qD may not appear in this context",
5604 decl.get_value ());
5605 return error_mark_node;
5610 decl = (finish_id_expression
5611 (id_expression, decl, parser->scope,
5612 idk,
5613 parser->integral_constant_expression_p,
5614 parser->allow_non_integral_constant_expression_p,
5615 &parser->non_integral_constant_expression_p,
5616 template_p, done, address_p,
5617 template_arg_p,
5618 &error_msg,
5619 id_expression.get_location ()));
5620 if (error_msg)
5621 cp_parser_error (parser, error_msg);
5622 decl.set_location (id_expr_token->location);
5623 return decl;
5626 /* Anything else is an error. */
5627 default:
5628 cp_parser_error (parser, "expected primary-expression");
5629 return error_mark_node;
5633 static inline cp_expr
5634 cp_parser_primary_expression (cp_parser *parser,
5635 bool address_p,
5636 bool cast_p,
5637 bool template_arg_p,
5638 cp_id_kind *idk)
5640 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5641 /*decltype*/false, idk);
5644 /* Parse an id-expression.
5646 id-expression:
5647 unqualified-id
5648 qualified-id
5650 qualified-id:
5651 :: [opt] nested-name-specifier template [opt] unqualified-id
5652 :: identifier
5653 :: operator-function-id
5654 :: template-id
5656 Return a representation of the unqualified portion of the
5657 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5658 a `::' or nested-name-specifier.
5660 Often, if the id-expression was a qualified-id, the caller will
5661 want to make a SCOPE_REF to represent the qualified-id. This
5662 function does not do this in order to avoid wastefully creating
5663 SCOPE_REFs when they are not required.
5665 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5666 `template' keyword.
5668 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5669 uninstantiated templates.
5671 If *TEMPLATE_P is non-NULL, it is set to true iff the
5672 `template' keyword is used to explicitly indicate that the entity
5673 named is a template.
5675 If DECLARATOR_P is true, the id-expression is appearing as part of
5676 a declarator, rather than as part of an expression. */
5678 static cp_expr
5679 cp_parser_id_expression (cp_parser *parser,
5680 bool template_keyword_p,
5681 bool check_dependency_p,
5682 bool *template_p,
5683 bool declarator_p,
5684 bool optional_p)
5686 bool global_scope_p;
5687 bool nested_name_specifier_p;
5689 /* Assume the `template' keyword was not used. */
5690 if (template_p)
5691 *template_p = template_keyword_p;
5693 /* Look for the optional `::' operator. */
5694 global_scope_p
5695 = (!template_keyword_p
5696 && (cp_parser_global_scope_opt (parser,
5697 /*current_scope_valid_p=*/false)
5698 != NULL_TREE));
5700 /* Look for the optional nested-name-specifier. */
5701 nested_name_specifier_p
5702 = (cp_parser_nested_name_specifier_opt (parser,
5703 /*typename_keyword_p=*/false,
5704 check_dependency_p,
5705 /*type_p=*/false,
5706 declarator_p,
5707 template_keyword_p)
5708 != NULL_TREE);
5710 /* If there is a nested-name-specifier, then we are looking at
5711 the first qualified-id production. */
5712 if (nested_name_specifier_p)
5714 tree saved_scope;
5715 tree saved_object_scope;
5716 tree saved_qualifying_scope;
5717 cp_expr unqualified_id;
5718 bool is_template;
5720 /* See if the next token is the `template' keyword. */
5721 if (!template_p)
5722 template_p = &is_template;
5723 *template_p = cp_parser_optional_template_keyword (parser);
5724 /* Name lookup we do during the processing of the
5725 unqualified-id might obliterate SCOPE. */
5726 saved_scope = parser->scope;
5727 saved_object_scope = parser->object_scope;
5728 saved_qualifying_scope = parser->qualifying_scope;
5729 /* Process the final unqualified-id. */
5730 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5731 check_dependency_p,
5732 declarator_p,
5733 /*optional_p=*/false);
5734 /* Restore the SAVED_SCOPE for our caller. */
5735 parser->scope = saved_scope;
5736 parser->object_scope = saved_object_scope;
5737 parser->qualifying_scope = saved_qualifying_scope;
5739 return unqualified_id;
5741 /* Otherwise, if we are in global scope, then we are looking at one
5742 of the other qualified-id productions. */
5743 else if (global_scope_p)
5745 cp_token *token;
5746 tree id;
5748 /* Peek at the next token. */
5749 token = cp_lexer_peek_token (parser->lexer);
5751 /* If it's an identifier, and the next token is not a "<", then
5752 we can avoid the template-id case. This is an optimization
5753 for this common case. */
5754 if (token->type == CPP_NAME
5755 && !cp_parser_nth_token_starts_template_argument_list_p
5756 (parser, 2))
5757 return cp_parser_identifier (parser);
5759 cp_parser_parse_tentatively (parser);
5760 /* Try a template-id. */
5761 id = cp_parser_template_id (parser,
5762 /*template_keyword_p=*/false,
5763 /*check_dependency_p=*/true,
5764 none_type,
5765 declarator_p);
5766 /* If that worked, we're done. */
5767 if (cp_parser_parse_definitely (parser))
5768 return id;
5770 /* Peek at the next token. (Changes in the token buffer may
5771 have invalidated the pointer obtained above.) */
5772 token = cp_lexer_peek_token (parser->lexer);
5774 switch (token->type)
5776 case CPP_NAME:
5777 return cp_parser_identifier (parser);
5779 case CPP_KEYWORD:
5780 if (token->keyword == RID_OPERATOR)
5781 return cp_parser_operator_function_id (parser);
5782 /* Fall through. */
5784 default:
5785 cp_parser_error (parser, "expected id-expression");
5786 return error_mark_node;
5789 else
5790 return cp_parser_unqualified_id (parser, template_keyword_p,
5791 /*check_dependency_p=*/true,
5792 declarator_p,
5793 optional_p);
5796 /* Parse an unqualified-id.
5798 unqualified-id:
5799 identifier
5800 operator-function-id
5801 conversion-function-id
5802 ~ class-name
5803 template-id
5805 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5806 keyword, in a construct like `A::template ...'.
5808 Returns a representation of unqualified-id. For the `identifier'
5809 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5810 production a BIT_NOT_EXPR is returned; the operand of the
5811 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5812 other productions, see the documentation accompanying the
5813 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5814 names are looked up in uninstantiated templates. If DECLARATOR_P
5815 is true, the unqualified-id is appearing as part of a declarator,
5816 rather than as part of an expression. */
5818 static cp_expr
5819 cp_parser_unqualified_id (cp_parser* parser,
5820 bool template_keyword_p,
5821 bool check_dependency_p,
5822 bool declarator_p,
5823 bool optional_p)
5825 cp_token *token;
5827 /* Peek at the next token. */
5828 token = cp_lexer_peek_token (parser->lexer);
5830 switch ((int) token->type)
5832 case CPP_NAME:
5834 tree id;
5836 /* We don't know yet whether or not this will be a
5837 template-id. */
5838 cp_parser_parse_tentatively (parser);
5839 /* Try a template-id. */
5840 id = cp_parser_template_id (parser, template_keyword_p,
5841 check_dependency_p,
5842 none_type,
5843 declarator_p);
5844 /* If it worked, we're done. */
5845 if (cp_parser_parse_definitely (parser))
5846 return id;
5847 /* Otherwise, it's an ordinary identifier. */
5848 return cp_parser_identifier (parser);
5851 case CPP_TEMPLATE_ID:
5852 return cp_parser_template_id (parser, template_keyword_p,
5853 check_dependency_p,
5854 none_type,
5855 declarator_p);
5857 case CPP_COMPL:
5859 tree type_decl;
5860 tree qualifying_scope;
5861 tree object_scope;
5862 tree scope;
5863 bool done;
5865 /* Consume the `~' token. */
5866 cp_lexer_consume_token (parser->lexer);
5867 /* Parse the class-name. The standard, as written, seems to
5868 say that:
5870 template <typename T> struct S { ~S (); };
5871 template <typename T> S<T>::~S() {}
5873 is invalid, since `~' must be followed by a class-name, but
5874 `S<T>' is dependent, and so not known to be a class.
5875 That's not right; we need to look in uninstantiated
5876 templates. A further complication arises from:
5878 template <typename T> void f(T t) {
5879 t.T::~T();
5882 Here, it is not possible to look up `T' in the scope of `T'
5883 itself. We must look in both the current scope, and the
5884 scope of the containing complete expression.
5886 Yet another issue is:
5888 struct S {
5889 int S;
5890 ~S();
5893 S::~S() {}
5895 The standard does not seem to say that the `S' in `~S'
5896 should refer to the type `S' and not the data member
5897 `S::S'. */
5899 /* DR 244 says that we look up the name after the "~" in the
5900 same scope as we looked up the qualifying name. That idea
5901 isn't fully worked out; it's more complicated than that. */
5902 scope = parser->scope;
5903 object_scope = parser->object_scope;
5904 qualifying_scope = parser->qualifying_scope;
5906 /* Check for invalid scopes. */
5907 if (scope == error_mark_node)
5909 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5910 cp_lexer_consume_token (parser->lexer);
5911 return error_mark_node;
5913 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5915 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5916 error_at (token->location,
5917 "scope %qT before %<~%> is not a class-name",
5918 scope);
5919 cp_parser_simulate_error (parser);
5920 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5921 cp_lexer_consume_token (parser->lexer);
5922 return error_mark_node;
5924 gcc_assert (!scope || TYPE_P (scope));
5926 /* If the name is of the form "X::~X" it's OK even if X is a
5927 typedef. */
5928 token = cp_lexer_peek_token (parser->lexer);
5929 if (scope
5930 && token->type == CPP_NAME
5931 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5932 != CPP_LESS)
5933 && (token->u.value == TYPE_IDENTIFIER (scope)
5934 || (CLASS_TYPE_P (scope)
5935 && constructor_name_p (token->u.value, scope))))
5937 cp_lexer_consume_token (parser->lexer);
5938 return build_nt (BIT_NOT_EXPR, scope);
5941 /* ~auto means the destructor of whatever the object is. */
5942 if (cp_parser_is_keyword (token, RID_AUTO))
5944 if (cxx_dialect < cxx14)
5945 pedwarn (input_location, 0,
5946 "%<~auto%> only available with "
5947 "-std=c++14 or -std=gnu++14");
5948 cp_lexer_consume_token (parser->lexer);
5949 return build_nt (BIT_NOT_EXPR, make_auto ());
5952 /* If there was an explicit qualification (S::~T), first look
5953 in the scope given by the qualification (i.e., S).
5955 Note: in the calls to cp_parser_class_name below we pass
5956 typename_type so that lookup finds the injected-class-name
5957 rather than the constructor. */
5958 done = false;
5959 type_decl = NULL_TREE;
5960 if (scope)
5962 cp_parser_parse_tentatively (parser);
5963 type_decl = cp_parser_class_name (parser,
5964 /*typename_keyword_p=*/false,
5965 /*template_keyword_p=*/false,
5966 typename_type,
5967 /*check_dependency=*/false,
5968 /*class_head_p=*/false,
5969 declarator_p);
5970 if (cp_parser_parse_definitely (parser))
5971 done = true;
5973 /* In "N::S::~S", look in "N" as well. */
5974 if (!done && scope && qualifying_scope)
5976 cp_parser_parse_tentatively (parser);
5977 parser->scope = qualifying_scope;
5978 parser->object_scope = NULL_TREE;
5979 parser->qualifying_scope = NULL_TREE;
5980 type_decl
5981 = cp_parser_class_name (parser,
5982 /*typename_keyword_p=*/false,
5983 /*template_keyword_p=*/false,
5984 typename_type,
5985 /*check_dependency=*/false,
5986 /*class_head_p=*/false,
5987 declarator_p);
5988 if (cp_parser_parse_definitely (parser))
5989 done = true;
5991 /* In "p->S::~T", look in the scope given by "*p" as well. */
5992 else if (!done && object_scope)
5994 cp_parser_parse_tentatively (parser);
5995 parser->scope = object_scope;
5996 parser->object_scope = NULL_TREE;
5997 parser->qualifying_scope = NULL_TREE;
5998 type_decl
5999 = cp_parser_class_name (parser,
6000 /*typename_keyword_p=*/false,
6001 /*template_keyword_p=*/false,
6002 typename_type,
6003 /*check_dependency=*/false,
6004 /*class_head_p=*/false,
6005 declarator_p);
6006 if (cp_parser_parse_definitely (parser))
6007 done = true;
6009 /* Look in the surrounding context. */
6010 if (!done)
6012 parser->scope = NULL_TREE;
6013 parser->object_scope = NULL_TREE;
6014 parser->qualifying_scope = NULL_TREE;
6015 if (processing_template_decl)
6016 cp_parser_parse_tentatively (parser);
6017 type_decl
6018 = cp_parser_class_name (parser,
6019 /*typename_keyword_p=*/false,
6020 /*template_keyword_p=*/false,
6021 typename_type,
6022 /*check_dependency=*/false,
6023 /*class_head_p=*/false,
6024 declarator_p);
6025 if (processing_template_decl
6026 && ! cp_parser_parse_definitely (parser))
6028 /* We couldn't find a type with this name. If we're parsing
6029 tentatively, fail and try something else. */
6030 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6032 cp_parser_simulate_error (parser);
6033 return error_mark_node;
6035 /* Otherwise, accept it and check for a match at instantiation
6036 time. */
6037 type_decl = cp_parser_identifier (parser);
6038 if (type_decl != error_mark_node)
6039 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6040 return type_decl;
6043 /* If an error occurred, assume that the name of the
6044 destructor is the same as the name of the qualifying
6045 class. That allows us to keep parsing after running
6046 into ill-formed destructor names. */
6047 if (type_decl == error_mark_node && scope)
6048 return build_nt (BIT_NOT_EXPR, scope);
6049 else if (type_decl == error_mark_node)
6050 return error_mark_node;
6052 /* Check that destructor name and scope match. */
6053 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6055 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6056 error_at (token->location,
6057 "declaration of %<~%T%> as member of %qT",
6058 type_decl, scope);
6059 cp_parser_simulate_error (parser);
6060 return error_mark_node;
6063 /* [class.dtor]
6065 A typedef-name that names a class shall not be used as the
6066 identifier in the declarator for a destructor declaration. */
6067 if (declarator_p
6068 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6069 && !DECL_SELF_REFERENCE_P (type_decl)
6070 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6071 error_at (token->location,
6072 "typedef-name %qD used as destructor declarator",
6073 type_decl);
6075 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6078 case CPP_KEYWORD:
6079 if (token->keyword == RID_OPERATOR)
6081 cp_expr id;
6083 /* This could be a template-id, so we try that first. */
6084 cp_parser_parse_tentatively (parser);
6085 /* Try a template-id. */
6086 id = cp_parser_template_id (parser, template_keyword_p,
6087 /*check_dependency_p=*/true,
6088 none_type,
6089 declarator_p);
6090 /* If that worked, we're done. */
6091 if (cp_parser_parse_definitely (parser))
6092 return id;
6093 /* We still don't know whether we're looking at an
6094 operator-function-id or a conversion-function-id. */
6095 cp_parser_parse_tentatively (parser);
6096 /* Try an operator-function-id. */
6097 id = cp_parser_operator_function_id (parser);
6098 /* If that didn't work, try a conversion-function-id. */
6099 if (!cp_parser_parse_definitely (parser))
6100 id = cp_parser_conversion_function_id (parser);
6101 else if (UDLIT_OPER_P (id))
6103 /* 17.6.3.3.5 */
6104 const char *name = UDLIT_OP_SUFFIX (id);
6105 if (name[0] != '_' && !in_system_header_at (input_location)
6106 && declarator_p)
6107 warning (OPT_Wliteral_suffix,
6108 "literal operator suffixes not preceded by %<_%>"
6109 " are reserved for future standardization");
6112 return id;
6114 /* Fall through. */
6116 default:
6117 if (optional_p)
6118 return NULL_TREE;
6119 cp_parser_error (parser, "expected unqualified-id");
6120 return error_mark_node;
6124 /* Parse an (optional) nested-name-specifier.
6126 nested-name-specifier: [C++98]
6127 class-or-namespace-name :: nested-name-specifier [opt]
6128 class-or-namespace-name :: template nested-name-specifier [opt]
6130 nested-name-specifier: [C++0x]
6131 type-name ::
6132 namespace-name ::
6133 nested-name-specifier identifier ::
6134 nested-name-specifier template [opt] simple-template-id ::
6136 PARSER->SCOPE should be set appropriately before this function is
6137 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6138 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6139 in name lookups.
6141 Sets PARSER->SCOPE to the class (TYPE) or namespace
6142 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6143 it unchanged if there is no nested-name-specifier. Returns the new
6144 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6146 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6147 part of a declaration and/or decl-specifier. */
6149 static tree
6150 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6151 bool typename_keyword_p,
6152 bool check_dependency_p,
6153 bool type_p,
6154 bool is_declaration,
6155 bool template_keyword_p /* = false */)
6157 bool success = false;
6158 cp_token_position start = 0;
6159 cp_token *token;
6161 /* Remember where the nested-name-specifier starts. */
6162 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6164 start = cp_lexer_token_position (parser->lexer, false);
6165 push_deferring_access_checks (dk_deferred);
6168 while (true)
6170 tree new_scope;
6171 tree old_scope;
6172 tree saved_qualifying_scope;
6174 /* Spot cases that cannot be the beginning of a
6175 nested-name-specifier. */
6176 token = cp_lexer_peek_token (parser->lexer);
6178 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6179 the already parsed nested-name-specifier. */
6180 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6182 /* Grab the nested-name-specifier and continue the loop. */
6183 cp_parser_pre_parsed_nested_name_specifier (parser);
6184 /* If we originally encountered this nested-name-specifier
6185 with IS_DECLARATION set to false, we will not have
6186 resolved TYPENAME_TYPEs, so we must do so here. */
6187 if (is_declaration
6188 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6190 new_scope = resolve_typename_type (parser->scope,
6191 /*only_current_p=*/false);
6192 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6193 parser->scope = new_scope;
6195 success = true;
6196 continue;
6199 /* Spot cases that cannot be the beginning of a
6200 nested-name-specifier. On the second and subsequent times
6201 through the loop, we look for the `template' keyword. */
6202 if (success && token->keyword == RID_TEMPLATE)
6204 /* A template-id can start a nested-name-specifier. */
6205 else if (token->type == CPP_TEMPLATE_ID)
6207 /* DR 743: decltype can be used in a nested-name-specifier. */
6208 else if (token_is_decltype (token))
6210 else
6212 /* If the next token is not an identifier, then it is
6213 definitely not a type-name or namespace-name. */
6214 if (token->type != CPP_NAME)
6215 break;
6216 /* If the following token is neither a `<' (to begin a
6217 template-id), nor a `::', then we are not looking at a
6218 nested-name-specifier. */
6219 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6221 if (token->type == CPP_COLON
6222 && parser->colon_corrects_to_scope_p
6223 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6225 gcc_rich_location richloc (token->location);
6226 richloc.add_fixit_replace ("::");
6227 error_at (&richloc,
6228 "found %<:%> in nested-name-specifier, "
6229 "expected %<::%>");
6230 token->type = CPP_SCOPE;
6233 if (token->type != CPP_SCOPE
6234 && !cp_parser_nth_token_starts_template_argument_list_p
6235 (parser, 2))
6236 break;
6239 /* The nested-name-specifier is optional, so we parse
6240 tentatively. */
6241 cp_parser_parse_tentatively (parser);
6243 /* Look for the optional `template' keyword, if this isn't the
6244 first time through the loop. */
6245 if (success)
6246 template_keyword_p = cp_parser_optional_template_keyword (parser);
6248 /* Save the old scope since the name lookup we are about to do
6249 might destroy it. */
6250 old_scope = parser->scope;
6251 saved_qualifying_scope = parser->qualifying_scope;
6252 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6253 look up names in "X<T>::I" in order to determine that "Y" is
6254 a template. So, if we have a typename at this point, we make
6255 an effort to look through it. */
6256 if (is_declaration
6257 && !typename_keyword_p
6258 && parser->scope
6259 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6260 parser->scope = resolve_typename_type (parser->scope,
6261 /*only_current_p=*/false);
6262 /* Parse the qualifying entity. */
6263 new_scope
6264 = cp_parser_qualifying_entity (parser,
6265 typename_keyword_p,
6266 template_keyword_p,
6267 check_dependency_p,
6268 type_p,
6269 is_declaration);
6270 /* Look for the `::' token. */
6271 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6273 /* If we found what we wanted, we keep going; otherwise, we're
6274 done. */
6275 if (!cp_parser_parse_definitely (parser))
6277 bool error_p = false;
6279 /* Restore the OLD_SCOPE since it was valid before the
6280 failed attempt at finding the last
6281 class-or-namespace-name. */
6282 parser->scope = old_scope;
6283 parser->qualifying_scope = saved_qualifying_scope;
6285 /* If the next token is a decltype, and the one after that is a
6286 `::', then the decltype has failed to resolve to a class or
6287 enumeration type. Give this error even when parsing
6288 tentatively since it can't possibly be valid--and we're going
6289 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6290 won't get another chance.*/
6291 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6292 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6293 == CPP_SCOPE))
6295 token = cp_lexer_consume_token (parser->lexer);
6296 error_at (token->location, "decltype evaluates to %qT, "
6297 "which is not a class or enumeration type",
6298 token->u.tree_check_value->value);
6299 parser->scope = error_mark_node;
6300 error_p = true;
6301 /* As below. */
6302 success = true;
6303 cp_lexer_consume_token (parser->lexer);
6306 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6307 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6309 /* If we have a non-type template-id followed by ::, it can't
6310 possibly be valid. */
6311 token = cp_lexer_peek_token (parser->lexer);
6312 tree tid = token->u.tree_check_value->value;
6313 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6314 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6316 tree tmpl = NULL_TREE;
6317 if (is_overloaded_fn (tid))
6319 tree fns = get_fns (tid);
6320 if (OVL_SINGLE_P (fns))
6321 tmpl = OVL_FIRST (fns);
6322 error_at (token->location, "function template-id %qD "
6323 "in nested-name-specifier", tid);
6325 else
6327 /* Variable template. */
6328 tmpl = TREE_OPERAND (tid, 0);
6329 gcc_assert (variable_template_p (tmpl));
6330 error_at (token->location, "variable template-id %qD "
6331 "in nested-name-specifier", tid);
6333 if (tmpl)
6334 inform (DECL_SOURCE_LOCATION (tmpl),
6335 "%qD declared here", tmpl);
6337 parser->scope = error_mark_node;
6338 error_p = true;
6339 /* As below. */
6340 success = true;
6341 cp_lexer_consume_token (parser->lexer);
6342 cp_lexer_consume_token (parser->lexer);
6346 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6347 break;
6348 /* If the next token is an identifier, and the one after
6349 that is a `::', then any valid interpretation would have
6350 found a class-or-namespace-name. */
6351 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6352 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6353 == CPP_SCOPE)
6354 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6355 != CPP_COMPL))
6357 token = cp_lexer_consume_token (parser->lexer);
6358 if (!error_p)
6360 if (!token->error_reported)
6362 tree decl;
6363 tree ambiguous_decls;
6365 decl = cp_parser_lookup_name (parser, token->u.value,
6366 none_type,
6367 /*is_template=*/false,
6368 /*is_namespace=*/false,
6369 /*check_dependency=*/true,
6370 &ambiguous_decls,
6371 token->location);
6372 if (TREE_CODE (decl) == TEMPLATE_DECL)
6373 error_at (token->location,
6374 "%qD used without template parameters",
6375 decl);
6376 else if (ambiguous_decls)
6378 // cp_parser_lookup_name has the same diagnostic,
6379 // thus make sure to emit it at most once.
6380 if (cp_parser_uncommitted_to_tentative_parse_p
6381 (parser))
6383 error_at (token->location,
6384 "reference to %qD is ambiguous",
6385 token->u.value);
6386 print_candidates (ambiguous_decls);
6388 decl = error_mark_node;
6390 else
6392 if (cxx_dialect != cxx98)
6393 cp_parser_name_lookup_error
6394 (parser, token->u.value, decl, NLE_NOT_CXX98,
6395 token->location);
6396 else
6397 cp_parser_name_lookup_error
6398 (parser, token->u.value, decl, NLE_CXX98,
6399 token->location);
6402 parser->scope = error_mark_node;
6403 error_p = true;
6404 /* Treat this as a successful nested-name-specifier
6405 due to:
6407 [basic.lookup.qual]
6409 If the name found is not a class-name (clause
6410 _class_) or namespace-name (_namespace.def_), the
6411 program is ill-formed. */
6412 success = true;
6414 cp_lexer_consume_token (parser->lexer);
6416 break;
6418 /* We've found one valid nested-name-specifier. */
6419 success = true;
6420 /* Name lookup always gives us a DECL. */
6421 if (TREE_CODE (new_scope) == TYPE_DECL)
6422 new_scope = TREE_TYPE (new_scope);
6423 /* Uses of "template" must be followed by actual templates. */
6424 if (template_keyword_p
6425 && !(CLASS_TYPE_P (new_scope)
6426 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6427 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6428 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6429 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6430 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6431 == TEMPLATE_ID_EXPR)))
6432 permerror (input_location, TYPE_P (new_scope)
6433 ? G_("%qT is not a template")
6434 : G_("%qD is not a template"),
6435 new_scope);
6436 /* If it is a class scope, try to complete it; we are about to
6437 be looking up names inside the class. */
6438 if (TYPE_P (new_scope)
6439 /* Since checking types for dependency can be expensive,
6440 avoid doing it if the type is already complete. */
6441 && !COMPLETE_TYPE_P (new_scope)
6442 /* Do not try to complete dependent types. */
6443 && !dependent_type_p (new_scope))
6445 new_scope = complete_type (new_scope);
6446 /* If it is a typedef to current class, use the current
6447 class instead, as the typedef won't have any names inside
6448 it yet. */
6449 if (!COMPLETE_TYPE_P (new_scope)
6450 && currently_open_class (new_scope))
6451 new_scope = TYPE_MAIN_VARIANT (new_scope);
6453 /* Make sure we look in the right scope the next time through
6454 the loop. */
6455 parser->scope = new_scope;
6458 /* If parsing tentatively, replace the sequence of tokens that makes
6459 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6460 token. That way, should we re-parse the token stream, we will
6461 not have to repeat the effort required to do the parse, nor will
6462 we issue duplicate error messages. */
6463 if (success && start)
6465 cp_token *token;
6467 token = cp_lexer_token_at (parser->lexer, start);
6468 /* Reset the contents of the START token. */
6469 token->type = CPP_NESTED_NAME_SPECIFIER;
6470 /* Retrieve any deferred checks. Do not pop this access checks yet
6471 so the memory will not be reclaimed during token replacing below. */
6472 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6473 token->u.tree_check_value->value = parser->scope;
6474 token->u.tree_check_value->checks = get_deferred_access_checks ();
6475 token->u.tree_check_value->qualifying_scope =
6476 parser->qualifying_scope;
6477 token->keyword = RID_MAX;
6479 /* Purge all subsequent tokens. */
6480 cp_lexer_purge_tokens_after (parser->lexer, start);
6483 if (start)
6484 pop_to_parent_deferring_access_checks ();
6486 return success ? parser->scope : NULL_TREE;
6489 /* Parse a nested-name-specifier. See
6490 cp_parser_nested_name_specifier_opt for details. This function
6491 behaves identically, except that it will an issue an error if no
6492 nested-name-specifier is present. */
6494 static tree
6495 cp_parser_nested_name_specifier (cp_parser *parser,
6496 bool typename_keyword_p,
6497 bool check_dependency_p,
6498 bool type_p,
6499 bool is_declaration)
6501 tree scope;
6503 /* Look for the nested-name-specifier. */
6504 scope = cp_parser_nested_name_specifier_opt (parser,
6505 typename_keyword_p,
6506 check_dependency_p,
6507 type_p,
6508 is_declaration);
6509 /* If it was not present, issue an error message. */
6510 if (!scope)
6512 cp_parser_error (parser, "expected nested-name-specifier");
6513 parser->scope = NULL_TREE;
6516 return scope;
6519 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6520 this is either a class-name or a namespace-name (which corresponds
6521 to the class-or-namespace-name production in the grammar). For
6522 C++0x, it can also be a type-name that refers to an enumeration
6523 type or a simple-template-id.
6525 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6526 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6527 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6528 TYPE_P is TRUE iff the next name should be taken as a class-name,
6529 even the same name is declared to be another entity in the same
6530 scope.
6532 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6533 specified by the class-or-namespace-name. If neither is found the
6534 ERROR_MARK_NODE is returned. */
6536 static tree
6537 cp_parser_qualifying_entity (cp_parser *parser,
6538 bool typename_keyword_p,
6539 bool template_keyword_p,
6540 bool check_dependency_p,
6541 bool type_p,
6542 bool is_declaration)
6544 tree saved_scope;
6545 tree saved_qualifying_scope;
6546 tree saved_object_scope;
6547 tree scope;
6548 bool only_class_p;
6549 bool successful_parse_p;
6551 /* DR 743: decltype can appear in a nested-name-specifier. */
6552 if (cp_lexer_next_token_is_decltype (parser->lexer))
6554 scope = cp_parser_decltype (parser);
6555 if (TREE_CODE (scope) != ENUMERAL_TYPE
6556 && !MAYBE_CLASS_TYPE_P (scope))
6558 cp_parser_simulate_error (parser);
6559 return error_mark_node;
6561 if (TYPE_NAME (scope))
6562 scope = TYPE_NAME (scope);
6563 return scope;
6566 /* Before we try to parse the class-name, we must save away the
6567 current PARSER->SCOPE since cp_parser_class_name will destroy
6568 it. */
6569 saved_scope = parser->scope;
6570 saved_qualifying_scope = parser->qualifying_scope;
6571 saved_object_scope = parser->object_scope;
6572 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6573 there is no need to look for a namespace-name. */
6574 only_class_p = template_keyword_p
6575 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6576 if (!only_class_p)
6577 cp_parser_parse_tentatively (parser);
6578 scope = cp_parser_class_name (parser,
6579 typename_keyword_p,
6580 template_keyword_p,
6581 type_p ? class_type : none_type,
6582 check_dependency_p,
6583 /*class_head_p=*/false,
6584 is_declaration,
6585 /*enum_ok=*/cxx_dialect > cxx98);
6586 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6587 /* If that didn't work, try for a namespace-name. */
6588 if (!only_class_p && !successful_parse_p)
6590 /* Restore the saved scope. */
6591 parser->scope = saved_scope;
6592 parser->qualifying_scope = saved_qualifying_scope;
6593 parser->object_scope = saved_object_scope;
6594 /* If we are not looking at an identifier followed by the scope
6595 resolution operator, then this is not part of a
6596 nested-name-specifier. (Note that this function is only used
6597 to parse the components of a nested-name-specifier.) */
6598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6599 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6600 return error_mark_node;
6601 scope = cp_parser_namespace_name (parser);
6604 return scope;
6607 /* Return true if we are looking at a compound-literal, false otherwise. */
6609 static bool
6610 cp_parser_compound_literal_p (cp_parser *parser)
6612 cp_lexer_save_tokens (parser->lexer);
6614 /* Skip tokens until the next token is a closing parenthesis.
6615 If we find the closing `)', and the next token is a `{', then
6616 we are looking at a compound-literal. */
6617 bool compound_literal_p
6618 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6619 /*consume_paren=*/true)
6620 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6622 /* Roll back the tokens we skipped. */
6623 cp_lexer_rollback_tokens (parser->lexer);
6625 return compound_literal_p;
6628 /* Return true if EXPR is the integer constant zero or a complex constant
6629 of zero, without any folding, but ignoring location wrappers. */
6631 static bool
6632 literal_integer_zerop (const_tree expr)
6634 STRIP_ANY_LOCATION_WRAPPER (expr);
6635 return integer_zerop (expr);
6638 /* Parse a postfix-expression.
6640 postfix-expression:
6641 primary-expression
6642 postfix-expression [ expression ]
6643 postfix-expression ( expression-list [opt] )
6644 simple-type-specifier ( expression-list [opt] )
6645 typename :: [opt] nested-name-specifier identifier
6646 ( expression-list [opt] )
6647 typename :: [opt] nested-name-specifier template [opt] template-id
6648 ( expression-list [opt] )
6649 postfix-expression . template [opt] id-expression
6650 postfix-expression -> template [opt] id-expression
6651 postfix-expression . pseudo-destructor-name
6652 postfix-expression -> pseudo-destructor-name
6653 postfix-expression ++
6654 postfix-expression --
6655 dynamic_cast < type-id > ( expression )
6656 static_cast < type-id > ( expression )
6657 reinterpret_cast < type-id > ( expression )
6658 const_cast < type-id > ( expression )
6659 typeid ( expression )
6660 typeid ( type-id )
6662 GNU Extension:
6664 postfix-expression:
6665 ( type-id ) { initializer-list , [opt] }
6667 This extension is a GNU version of the C99 compound-literal
6668 construct. (The C99 grammar uses `type-name' instead of `type-id',
6669 but they are essentially the same concept.)
6671 If ADDRESS_P is true, the postfix expression is the operand of the
6672 `&' operator. CAST_P is true if this expression is the target of a
6673 cast.
6675 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6676 class member access expressions [expr.ref].
6678 Returns a representation of the expression. */
6680 static cp_expr
6681 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6682 bool member_access_only_p, bool decltype_p,
6683 cp_id_kind * pidk_return)
6685 cp_token *token;
6686 location_t loc;
6687 enum rid keyword;
6688 cp_id_kind idk = CP_ID_KIND_NONE;
6689 cp_expr postfix_expression = NULL_TREE;
6690 bool is_member_access = false;
6692 /* Peek at the next token. */
6693 token = cp_lexer_peek_token (parser->lexer);
6694 loc = token->location;
6695 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6697 /* Some of the productions are determined by keywords. */
6698 keyword = token->keyword;
6699 switch (keyword)
6701 case RID_DYNCAST:
6702 case RID_STATCAST:
6703 case RID_REINTCAST:
6704 case RID_CONSTCAST:
6706 tree type;
6707 cp_expr expression;
6708 const char *saved_message;
6709 bool saved_in_type_id_in_expr_p;
6711 /* All of these can be handled in the same way from the point
6712 of view of parsing. Begin by consuming the token
6713 identifying the cast. */
6714 cp_lexer_consume_token (parser->lexer);
6716 /* New types cannot be defined in the cast. */
6717 saved_message = parser->type_definition_forbidden_message;
6718 parser->type_definition_forbidden_message
6719 = G_("types may not be defined in casts");
6721 /* Look for the opening `<'. */
6722 cp_parser_require (parser, CPP_LESS, RT_LESS);
6723 /* Parse the type to which we are casting. */
6724 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6725 parser->in_type_id_in_expr_p = true;
6726 type = cp_parser_type_id (parser);
6727 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6728 /* Look for the closing `>'. */
6729 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6730 /* Restore the old message. */
6731 parser->type_definition_forbidden_message = saved_message;
6733 bool saved_greater_than_is_operator_p
6734 = parser->greater_than_is_operator_p;
6735 parser->greater_than_is_operator_p = true;
6737 /* And the expression which is being cast. */
6738 matching_parens parens;
6739 parens.require_open (parser);
6740 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6741 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6742 RT_CLOSE_PAREN);
6743 location_t end_loc = close_paren ?
6744 close_paren->location : UNKNOWN_LOCATION;
6746 parser->greater_than_is_operator_p
6747 = saved_greater_than_is_operator_p;
6749 /* Only type conversions to integral or enumeration types
6750 can be used in constant-expressions. */
6751 if (!cast_valid_in_integral_constant_expression_p (type)
6752 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6754 postfix_expression = error_mark_node;
6755 break;
6758 switch (keyword)
6760 case RID_DYNCAST:
6761 postfix_expression
6762 = build_dynamic_cast (type, expression, tf_warning_or_error);
6763 break;
6764 case RID_STATCAST:
6765 postfix_expression
6766 = build_static_cast (type, expression, tf_warning_or_error);
6767 break;
6768 case RID_REINTCAST:
6769 postfix_expression
6770 = build_reinterpret_cast (type, expression,
6771 tf_warning_or_error);
6772 break;
6773 case RID_CONSTCAST:
6774 postfix_expression
6775 = build_const_cast (type, expression, tf_warning_or_error);
6776 break;
6777 default:
6778 gcc_unreachable ();
6781 /* Construct a location e.g. :
6782 reinterpret_cast <int *> (expr)
6783 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6784 ranging from the start of the "*_cast" token to the final closing
6785 paren, with the caret at the start. */
6786 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6787 postfix_expression.set_location (cp_cast_loc);
6789 break;
6791 case RID_TYPEID:
6793 tree type;
6794 const char *saved_message;
6795 bool saved_in_type_id_in_expr_p;
6797 /* Consume the `typeid' token. */
6798 cp_lexer_consume_token (parser->lexer);
6799 /* Look for the `(' token. */
6800 matching_parens parens;
6801 parens.require_open (parser);
6802 /* Types cannot be defined in a `typeid' expression. */
6803 saved_message = parser->type_definition_forbidden_message;
6804 parser->type_definition_forbidden_message
6805 = G_("types may not be defined in a %<typeid%> expression");
6806 /* We can't be sure yet whether we're looking at a type-id or an
6807 expression. */
6808 cp_parser_parse_tentatively (parser);
6809 /* Try a type-id first. */
6810 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6811 parser->in_type_id_in_expr_p = true;
6812 type = cp_parser_type_id (parser);
6813 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6814 /* Look for the `)' token. Otherwise, we can't be sure that
6815 we're not looking at an expression: consider `typeid (int
6816 (3))', for example. */
6817 cp_token *close_paren = parens.require_close (parser);
6818 /* If all went well, simply lookup the type-id. */
6819 if (cp_parser_parse_definitely (parser))
6820 postfix_expression = get_typeid (type, tf_warning_or_error);
6821 /* Otherwise, fall back to the expression variant. */
6822 else
6824 tree expression;
6826 /* Look for an expression. */
6827 expression = cp_parser_expression (parser, & idk);
6828 /* Compute its typeid. */
6829 postfix_expression = build_typeid (expression, tf_warning_or_error);
6830 /* Look for the `)' token. */
6831 close_paren = parens.require_close (parser);
6833 /* Restore the saved message. */
6834 parser->type_definition_forbidden_message = saved_message;
6835 /* `typeid' may not appear in an integral constant expression. */
6836 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6837 postfix_expression = error_mark_node;
6839 /* Construct a location e.g. :
6840 typeid (expr)
6841 ^~~~~~~~~~~~~
6842 ranging from the start of the "typeid" token to the final closing
6843 paren, with the caret at the start. */
6844 if (close_paren)
6846 location_t typeid_loc
6847 = make_location (start_loc, start_loc, close_paren->location);
6848 postfix_expression.set_location (typeid_loc);
6849 postfix_expression.maybe_add_location_wrapper ();
6852 break;
6854 case RID_TYPENAME:
6856 tree type;
6857 /* The syntax permitted here is the same permitted for an
6858 elaborated-type-specifier. */
6859 ++parser->prevent_constrained_type_specifiers;
6860 type = cp_parser_elaborated_type_specifier (parser,
6861 /*is_friend=*/false,
6862 /*is_declaration=*/false);
6863 --parser->prevent_constrained_type_specifiers;
6864 postfix_expression = cp_parser_functional_cast (parser, type);
6866 break;
6868 case RID_ADDRESSOF:
6869 case RID_BUILTIN_SHUFFLE:
6870 case RID_BUILTIN_LAUNDER:
6872 vec<tree, va_gc> *vec;
6873 unsigned int i;
6874 tree p;
6876 cp_lexer_consume_token (parser->lexer);
6877 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6878 /*cast_p=*/false, /*allow_expansion_p=*/true,
6879 /*non_constant_p=*/NULL);
6880 if (vec == NULL)
6882 postfix_expression = error_mark_node;
6883 break;
6886 FOR_EACH_VEC_ELT (*vec, i, p)
6887 mark_exp_read (p);
6889 switch (keyword)
6891 case RID_ADDRESSOF:
6892 if (vec->length () == 1)
6893 postfix_expression
6894 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6895 else
6897 error_at (loc, "wrong number of arguments to "
6898 "%<__builtin_addressof%>");
6899 postfix_expression = error_mark_node;
6901 break;
6903 case RID_BUILTIN_LAUNDER:
6904 if (vec->length () == 1)
6905 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6906 tf_warning_or_error);
6907 else
6909 error_at (loc, "wrong number of arguments to "
6910 "%<__builtin_launder%>");
6911 postfix_expression = error_mark_node;
6913 break;
6915 case RID_BUILTIN_SHUFFLE:
6916 if (vec->length () == 2)
6917 postfix_expression
6918 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6919 (*vec)[1], tf_warning_or_error);
6920 else if (vec->length () == 3)
6921 postfix_expression
6922 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6923 (*vec)[2], tf_warning_or_error);
6924 else
6926 error_at (loc, "wrong number of arguments to "
6927 "%<__builtin_shuffle%>");
6928 postfix_expression = error_mark_node;
6930 break;
6932 default:
6933 gcc_unreachable ();
6935 break;
6938 default:
6940 tree type;
6942 /* If the next thing is a simple-type-specifier, we may be
6943 looking at a functional cast. We could also be looking at
6944 an id-expression. So, we try the functional cast, and if
6945 that doesn't work we fall back to the primary-expression. */
6946 cp_parser_parse_tentatively (parser);
6947 /* Look for the simple-type-specifier. */
6948 ++parser->prevent_constrained_type_specifiers;
6949 type = cp_parser_simple_type_specifier (parser,
6950 /*decl_specs=*/NULL,
6951 CP_PARSER_FLAGS_NONE);
6952 --parser->prevent_constrained_type_specifiers;
6953 /* Parse the cast itself. */
6954 if (!cp_parser_error_occurred (parser))
6955 postfix_expression
6956 = cp_parser_functional_cast (parser, type);
6957 /* If that worked, we're done. */
6958 if (cp_parser_parse_definitely (parser))
6959 break;
6961 /* If the functional-cast didn't work out, try a
6962 compound-literal. */
6963 if (cp_parser_allow_gnu_extensions_p (parser)
6964 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6966 cp_expr initializer = NULL_TREE;
6968 cp_parser_parse_tentatively (parser);
6970 matching_parens parens;
6971 parens.consume_open (parser);
6973 /* Avoid calling cp_parser_type_id pointlessly, see comment
6974 in cp_parser_cast_expression about c++/29234. */
6975 if (!cp_parser_compound_literal_p (parser))
6976 cp_parser_simulate_error (parser);
6977 else
6979 /* Parse the type. */
6980 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6981 parser->in_type_id_in_expr_p = true;
6982 type = cp_parser_type_id (parser);
6983 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6984 parens.require_close (parser);
6987 /* If things aren't going well, there's no need to
6988 keep going. */
6989 if (!cp_parser_error_occurred (parser))
6991 bool non_constant_p;
6992 /* Parse the brace-enclosed initializer list. */
6993 initializer = cp_parser_braced_list (parser,
6994 &non_constant_p);
6996 /* If that worked, we're definitely looking at a
6997 compound-literal expression. */
6998 if (cp_parser_parse_definitely (parser))
7000 /* Warn the user that a compound literal is not
7001 allowed in standard C++. */
7002 pedwarn (input_location, OPT_Wpedantic,
7003 "ISO C++ forbids compound-literals");
7004 /* For simplicity, we disallow compound literals in
7005 constant-expressions. We could
7006 allow compound literals of integer type, whose
7007 initializer was a constant, in constant
7008 expressions. Permitting that usage, as a further
7009 extension, would not change the meaning of any
7010 currently accepted programs. (Of course, as
7011 compound literals are not part of ISO C++, the
7012 standard has nothing to say.) */
7013 if (cp_parser_non_integral_constant_expression (parser,
7014 NIC_NCC))
7016 postfix_expression = error_mark_node;
7017 break;
7019 /* Form the representation of the compound-literal. */
7020 postfix_expression
7021 = finish_compound_literal (type, initializer,
7022 tf_warning_or_error, fcl_c99);
7023 postfix_expression.set_location (initializer.get_location ());
7024 break;
7028 /* It must be a primary-expression. */
7029 postfix_expression
7030 = cp_parser_primary_expression (parser, address_p, cast_p,
7031 /*template_arg_p=*/false,
7032 decltype_p,
7033 &idk);
7035 break;
7038 /* Note that we don't need to worry about calling build_cplus_new on a
7039 class-valued CALL_EXPR in decltype when it isn't the end of the
7040 postfix-expression; unary_complex_lvalue will take care of that for
7041 all these cases. */
7043 /* Keep looping until the postfix-expression is complete. */
7044 while (true)
7046 if (idk == CP_ID_KIND_UNQUALIFIED
7047 && identifier_p (postfix_expression)
7048 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7049 /* It is not a Koenig lookup function call. */
7050 postfix_expression
7051 = unqualified_name_lookup_error (postfix_expression);
7053 /* Peek at the next token. */
7054 token = cp_lexer_peek_token (parser->lexer);
7056 switch (token->type)
7058 case CPP_OPEN_SQUARE:
7059 if (cp_next_tokens_can_be_std_attribute_p (parser))
7061 cp_parser_error (parser,
7062 "two consecutive %<[%> shall "
7063 "only introduce an attribute");
7064 return error_mark_node;
7066 postfix_expression
7067 = cp_parser_postfix_open_square_expression (parser,
7068 postfix_expression,
7069 false,
7070 decltype_p);
7071 postfix_expression.set_range (start_loc,
7072 postfix_expression.get_location ());
7074 idk = CP_ID_KIND_NONE;
7075 is_member_access = false;
7076 break;
7078 case CPP_OPEN_PAREN:
7079 /* postfix-expression ( expression-list [opt] ) */
7081 bool koenig_p;
7082 bool is_builtin_constant_p;
7083 bool saved_integral_constant_expression_p = false;
7084 bool saved_non_integral_constant_expression_p = false;
7085 tsubst_flags_t complain = complain_flags (decltype_p);
7086 vec<tree, va_gc> *args;
7087 location_t close_paren_loc = UNKNOWN_LOCATION;
7089 is_member_access = false;
7091 is_builtin_constant_p
7092 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7093 if (is_builtin_constant_p)
7095 /* The whole point of __builtin_constant_p is to allow
7096 non-constant expressions to appear as arguments. */
7097 saved_integral_constant_expression_p
7098 = parser->integral_constant_expression_p;
7099 saved_non_integral_constant_expression_p
7100 = parser->non_integral_constant_expression_p;
7101 parser->integral_constant_expression_p = false;
7103 args = (cp_parser_parenthesized_expression_list
7104 (parser, non_attr,
7105 /*cast_p=*/false, /*allow_expansion_p=*/true,
7106 /*non_constant_p=*/NULL,
7107 /*close_paren_loc=*/&close_paren_loc,
7108 /*wrap_locations_p=*/true));
7109 if (is_builtin_constant_p)
7111 parser->integral_constant_expression_p
7112 = saved_integral_constant_expression_p;
7113 parser->non_integral_constant_expression_p
7114 = saved_non_integral_constant_expression_p;
7117 if (args == NULL)
7119 postfix_expression = error_mark_node;
7120 break;
7123 /* Function calls are not permitted in
7124 constant-expressions. */
7125 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7126 && cp_parser_non_integral_constant_expression (parser,
7127 NIC_FUNC_CALL))
7129 postfix_expression = error_mark_node;
7130 release_tree_vector (args);
7131 break;
7134 koenig_p = false;
7135 if (idk == CP_ID_KIND_UNQUALIFIED
7136 || idk == CP_ID_KIND_TEMPLATE_ID)
7138 if (identifier_p (postfix_expression))
7140 if (!args->is_empty ())
7142 koenig_p = true;
7143 if (!any_type_dependent_arguments_p (args))
7144 postfix_expression
7145 = perform_koenig_lookup (postfix_expression, args,
7146 complain);
7148 else
7149 postfix_expression
7150 = unqualified_fn_lookup_error (postfix_expression);
7152 /* We do not perform argument-dependent lookup if
7153 normal lookup finds a non-function, in accordance
7154 with the expected resolution of DR 218. */
7155 else if (!args->is_empty ()
7156 && is_overloaded_fn (postfix_expression))
7158 tree fn = get_first_fn (postfix_expression);
7159 fn = STRIP_TEMPLATE (fn);
7161 /* Do not do argument dependent lookup if regular
7162 lookup finds a member function or a block-scope
7163 function declaration. [basic.lookup.argdep]/3 */
7164 if (!DECL_FUNCTION_MEMBER_P (fn)
7165 && !DECL_LOCAL_FUNCTION_P (fn))
7167 koenig_p = true;
7168 if (!any_type_dependent_arguments_p (args))
7169 postfix_expression
7170 = perform_koenig_lookup (postfix_expression, args,
7171 complain);
7176 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7177 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7178 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7179 && vec_safe_length (args) == 3)
7181 tree arg0 = (*args)[0];
7182 tree arg1 = (*args)[1];
7183 tree arg2 = (*args)[2];
7184 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7185 | (literal_integer_zerop (arg2) << 2));
7186 warn_for_memset (input_location, arg0, arg2, literal_mask);
7189 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7191 tree instance = TREE_OPERAND (postfix_expression, 0);
7192 tree fn = TREE_OPERAND (postfix_expression, 1);
7194 if (processing_template_decl
7195 && (type_dependent_object_expression_p (instance)
7196 || (!BASELINK_P (fn)
7197 && TREE_CODE (fn) != FIELD_DECL)
7198 || type_dependent_expression_p (fn)
7199 || any_type_dependent_arguments_p (args)))
7201 maybe_generic_this_capture (instance, fn);
7202 postfix_expression
7203 = build_min_nt_call_vec (postfix_expression, args);
7204 release_tree_vector (args);
7205 break;
7208 if (BASELINK_P (fn))
7210 postfix_expression
7211 = (build_new_method_call
7212 (instance, fn, &args, NULL_TREE,
7213 (idk == CP_ID_KIND_QUALIFIED
7214 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7215 : LOOKUP_NORMAL),
7216 /*fn_p=*/NULL,
7217 complain));
7219 else
7220 postfix_expression
7221 = finish_call_expr (postfix_expression, &args,
7222 /*disallow_virtual=*/false,
7223 /*koenig_p=*/false,
7224 complain);
7226 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7227 || TREE_CODE (postfix_expression) == MEMBER_REF
7228 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7229 postfix_expression = (build_offset_ref_call_from_tree
7230 (postfix_expression, &args,
7231 complain));
7232 else if (idk == CP_ID_KIND_QUALIFIED)
7233 /* A call to a static class member, or a namespace-scope
7234 function. */
7235 postfix_expression
7236 = finish_call_expr (postfix_expression, &args,
7237 /*disallow_virtual=*/true,
7238 koenig_p,
7239 complain);
7240 else
7241 /* All other function calls. */
7242 postfix_expression
7243 = finish_call_expr (postfix_expression, &args,
7244 /*disallow_virtual=*/false,
7245 koenig_p,
7246 complain);
7248 if (close_paren_loc != UNKNOWN_LOCATION)
7250 location_t combined_loc = make_location (token->location,
7251 start_loc,
7252 close_paren_loc);
7253 postfix_expression.set_location (combined_loc);
7256 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7257 idk = CP_ID_KIND_NONE;
7259 release_tree_vector (args);
7261 break;
7263 case CPP_DOT:
7264 case CPP_DEREF:
7265 /* postfix-expression . template [opt] id-expression
7266 postfix-expression . pseudo-destructor-name
7267 postfix-expression -> template [opt] id-expression
7268 postfix-expression -> pseudo-destructor-name */
7270 /* Consume the `.' or `->' operator. */
7271 cp_lexer_consume_token (parser->lexer);
7273 postfix_expression
7274 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7275 postfix_expression,
7276 false, &idk, loc);
7278 is_member_access = true;
7279 break;
7281 case CPP_PLUS_PLUS:
7282 /* postfix-expression ++ */
7283 /* Consume the `++' token. */
7284 cp_lexer_consume_token (parser->lexer);
7285 /* Generate a representation for the complete expression. */
7286 postfix_expression
7287 = finish_increment_expr (postfix_expression,
7288 POSTINCREMENT_EXPR);
7289 /* Increments may not appear in constant-expressions. */
7290 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7291 postfix_expression = error_mark_node;
7292 idk = CP_ID_KIND_NONE;
7293 is_member_access = false;
7294 break;
7296 case CPP_MINUS_MINUS:
7297 /* postfix-expression -- */
7298 /* Consume the `--' token. */
7299 cp_lexer_consume_token (parser->lexer);
7300 /* Generate a representation for the complete expression. */
7301 postfix_expression
7302 = finish_increment_expr (postfix_expression,
7303 POSTDECREMENT_EXPR);
7304 /* Decrements may not appear in constant-expressions. */
7305 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7306 postfix_expression = error_mark_node;
7307 idk = CP_ID_KIND_NONE;
7308 is_member_access = false;
7309 break;
7311 default:
7312 if (pidk_return != NULL)
7313 * pidk_return = idk;
7314 if (member_access_only_p)
7315 return is_member_access
7316 ? postfix_expression
7317 : cp_expr (error_mark_node);
7318 else
7319 return postfix_expression;
7323 /* We should never get here. */
7324 gcc_unreachable ();
7325 return error_mark_node;
7328 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7329 by cp_parser_builtin_offsetof. We're looking for
7331 postfix-expression [ expression ]
7332 postfix-expression [ braced-init-list ] (C++11)
7334 FOR_OFFSETOF is set if we're being called in that context, which
7335 changes how we deal with integer constant expressions. */
7337 static tree
7338 cp_parser_postfix_open_square_expression (cp_parser *parser,
7339 tree postfix_expression,
7340 bool for_offsetof,
7341 bool decltype_p)
7343 tree index = NULL_TREE;
7344 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7345 bool saved_greater_than_is_operator_p;
7347 /* Consume the `[' token. */
7348 cp_lexer_consume_token (parser->lexer);
7350 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7351 parser->greater_than_is_operator_p = true;
7353 /* Parse the index expression. */
7354 /* ??? For offsetof, there is a question of what to allow here. If
7355 offsetof is not being used in an integral constant expression context,
7356 then we *could* get the right answer by computing the value at runtime.
7357 If we are in an integral constant expression context, then we might
7358 could accept any constant expression; hard to say without analysis.
7359 Rather than open the barn door too wide right away, allow only integer
7360 constant expressions here. */
7361 if (for_offsetof)
7362 index = cp_parser_constant_expression (parser);
7363 else
7365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7367 bool expr_nonconst_p;
7368 cp_lexer_set_source_position (parser->lexer);
7369 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7370 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7372 else
7373 index = cp_parser_expression (parser);
7376 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7378 /* Look for the closing `]'. */
7379 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7381 /* Build the ARRAY_REF. */
7382 postfix_expression = grok_array_decl (loc, postfix_expression,
7383 index, decltype_p);
7385 /* When not doing offsetof, array references are not permitted in
7386 constant-expressions. */
7387 if (!for_offsetof
7388 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7389 postfix_expression = error_mark_node;
7391 return postfix_expression;
7394 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7395 dereference of incomplete type, returns true if error_mark_node should
7396 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7397 and *DEPENDENT_P. */
7399 bool
7400 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7401 bool *dependent_p)
7403 /* In a template, be permissive by treating an object expression
7404 of incomplete type as dependent (after a pedwarn). */
7405 diagnostic_t kind = (processing_template_decl
7406 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7408 switch (TREE_CODE (*postfix_expression))
7410 case CAST_EXPR:
7411 case REINTERPRET_CAST_EXPR:
7412 case CONST_CAST_EXPR:
7413 case STATIC_CAST_EXPR:
7414 case DYNAMIC_CAST_EXPR:
7415 case IMPLICIT_CONV_EXPR:
7416 case VIEW_CONVERT_EXPR:
7417 case NON_LVALUE_EXPR:
7418 kind = DK_ERROR;
7419 break;
7420 case OVERLOAD:
7421 /* Don't emit any diagnostic for OVERLOADs. */
7422 kind = DK_IGNORED;
7423 break;
7424 default:
7425 /* Avoid clobbering e.g. DECLs. */
7426 if (!EXPR_P (*postfix_expression))
7427 kind = DK_ERROR;
7428 break;
7431 if (kind == DK_IGNORED)
7432 return false;
7434 location_t exploc = location_of (*postfix_expression);
7435 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7436 if (!MAYBE_CLASS_TYPE_P (*scope))
7437 return true;
7438 if (kind == DK_ERROR)
7439 *scope = *postfix_expression = error_mark_node;
7440 else if (processing_template_decl)
7442 *dependent_p = true;
7443 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7445 return false;
7448 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7449 by cp_parser_builtin_offsetof. We're looking for
7451 postfix-expression . template [opt] id-expression
7452 postfix-expression . pseudo-destructor-name
7453 postfix-expression -> template [opt] id-expression
7454 postfix-expression -> pseudo-destructor-name
7456 FOR_OFFSETOF is set if we're being called in that context. That sorta
7457 limits what of the above we'll actually accept, but nevermind.
7458 TOKEN_TYPE is the "." or "->" token, which will already have been
7459 removed from the stream. */
7461 static tree
7462 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7463 enum cpp_ttype token_type,
7464 cp_expr postfix_expression,
7465 bool for_offsetof, cp_id_kind *idk,
7466 location_t location)
7468 tree name;
7469 bool dependent_p;
7470 bool pseudo_destructor_p;
7471 tree scope = NULL_TREE;
7472 location_t start_loc = postfix_expression.get_start ();
7474 /* If this is a `->' operator, dereference the pointer. */
7475 if (token_type == CPP_DEREF)
7476 postfix_expression = build_x_arrow (location, postfix_expression,
7477 tf_warning_or_error);
7478 /* Check to see whether or not the expression is type-dependent and
7479 not the current instantiation. */
7480 dependent_p = type_dependent_object_expression_p (postfix_expression);
7481 /* The identifier following the `->' or `.' is not qualified. */
7482 parser->scope = NULL_TREE;
7483 parser->qualifying_scope = NULL_TREE;
7484 parser->object_scope = NULL_TREE;
7485 *idk = CP_ID_KIND_NONE;
7487 /* Enter the scope corresponding to the type of the object
7488 given by the POSTFIX_EXPRESSION. */
7489 if (!dependent_p)
7491 scope = TREE_TYPE (postfix_expression);
7492 /* According to the standard, no expression should ever have
7493 reference type. Unfortunately, we do not currently match
7494 the standard in this respect in that our internal representation
7495 of an expression may have reference type even when the standard
7496 says it does not. Therefore, we have to manually obtain the
7497 underlying type here. */
7498 scope = non_reference (scope);
7499 /* The type of the POSTFIX_EXPRESSION must be complete. */
7500 /* Unlike the object expression in other contexts, *this is not
7501 required to be of complete type for purposes of class member
7502 access (5.2.5) outside the member function body. */
7503 if (postfix_expression != current_class_ref
7504 && scope != error_mark_node
7505 && !(processing_template_decl
7506 && current_class_type
7507 && (same_type_ignoring_top_level_qualifiers_p
7508 (scope, current_class_type))))
7510 scope = complete_type (scope);
7511 if (!COMPLETE_TYPE_P (scope)
7512 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7513 &dependent_p))
7514 return error_mark_node;
7517 if (!dependent_p)
7519 /* Let the name lookup machinery know that we are processing a
7520 class member access expression. */
7521 parser->context->object_type = scope;
7522 /* If something went wrong, we want to be able to discern that case,
7523 as opposed to the case where there was no SCOPE due to the type
7524 of expression being dependent. */
7525 if (!scope)
7526 scope = error_mark_node;
7527 /* If the SCOPE was erroneous, make the various semantic analysis
7528 functions exit quickly -- and without issuing additional error
7529 messages. */
7530 if (scope == error_mark_node)
7531 postfix_expression = error_mark_node;
7535 if (dependent_p)
7536 /* Tell cp_parser_lookup_name that there was an object, even though it's
7537 type-dependent. */
7538 parser->context->object_type = unknown_type_node;
7540 /* Assume this expression is not a pseudo-destructor access. */
7541 pseudo_destructor_p = false;
7543 /* If the SCOPE is a scalar type, then, if this is a valid program,
7544 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7545 is type dependent, it can be pseudo-destructor-name or something else.
7546 Try to parse it as pseudo-destructor-name first. */
7547 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7549 tree s;
7550 tree type;
7552 cp_parser_parse_tentatively (parser);
7553 /* Parse the pseudo-destructor-name. */
7554 s = NULL_TREE;
7555 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7556 &s, &type);
7557 if (dependent_p
7558 && (cp_parser_error_occurred (parser)
7559 || !SCALAR_TYPE_P (type)))
7560 cp_parser_abort_tentative_parse (parser);
7561 else if (cp_parser_parse_definitely (parser))
7563 pseudo_destructor_p = true;
7564 postfix_expression
7565 = finish_pseudo_destructor_expr (postfix_expression,
7566 s, type, location);
7570 if (!pseudo_destructor_p)
7572 /* If the SCOPE is not a scalar type, we are looking at an
7573 ordinary class member access expression, rather than a
7574 pseudo-destructor-name. */
7575 bool template_p;
7576 cp_token *token = cp_lexer_peek_token (parser->lexer);
7577 /* Parse the id-expression. */
7578 name = (cp_parser_id_expression
7579 (parser,
7580 cp_parser_optional_template_keyword (parser),
7581 /*check_dependency_p=*/true,
7582 &template_p,
7583 /*declarator_p=*/false,
7584 /*optional_p=*/false));
7585 /* In general, build a SCOPE_REF if the member name is qualified.
7586 However, if the name was not dependent and has already been
7587 resolved; there is no need to build the SCOPE_REF. For example;
7589 struct X { void f(); };
7590 template <typename T> void f(T* t) { t->X::f(); }
7592 Even though "t" is dependent, "X::f" is not and has been resolved
7593 to a BASELINK; there is no need to include scope information. */
7595 /* But we do need to remember that there was an explicit scope for
7596 virtual function calls. */
7597 if (parser->scope)
7598 *idk = CP_ID_KIND_QUALIFIED;
7600 /* If the name is a template-id that names a type, we will get a
7601 TYPE_DECL here. That is invalid code. */
7602 if (TREE_CODE (name) == TYPE_DECL)
7604 error_at (token->location, "invalid use of %qD", name);
7605 postfix_expression = error_mark_node;
7607 else
7609 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7611 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7613 error_at (token->location, "%<%D::%D%> is not a class member",
7614 parser->scope, name);
7615 postfix_expression = error_mark_node;
7617 else
7618 name = build_qualified_name (/*type=*/NULL_TREE,
7619 parser->scope,
7620 name,
7621 template_p);
7622 parser->scope = NULL_TREE;
7623 parser->qualifying_scope = NULL_TREE;
7624 parser->object_scope = NULL_TREE;
7626 if (parser->scope && name && BASELINK_P (name))
7627 adjust_result_of_qualified_name_lookup
7628 (name, parser->scope, scope);
7629 postfix_expression
7630 = finish_class_member_access_expr (postfix_expression, name,
7631 template_p,
7632 tf_warning_or_error);
7633 /* Build a location e.g.:
7634 ptr->access_expr
7635 ~~~^~~~~~~~~~~~~
7636 where the caret is at the deref token, ranging from
7637 the start of postfix_expression to the end of the access expr. */
7638 location_t end_loc
7639 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7640 location_t combined_loc
7641 = make_location (input_location, start_loc, end_loc);
7642 protected_set_expr_location (postfix_expression, combined_loc);
7646 /* We no longer need to look up names in the scope of the object on
7647 the left-hand side of the `.' or `->' operator. */
7648 parser->context->object_type = NULL_TREE;
7650 /* Outside of offsetof, these operators may not appear in
7651 constant-expressions. */
7652 if (!for_offsetof
7653 && (cp_parser_non_integral_constant_expression
7654 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7655 postfix_expression = error_mark_node;
7657 return postfix_expression;
7660 /* Parse a parenthesized expression-list.
7662 expression-list:
7663 assignment-expression
7664 expression-list, assignment-expression
7666 attribute-list:
7667 expression-list
7668 identifier
7669 identifier, expression-list
7671 CAST_P is true if this expression is the target of a cast.
7673 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7674 argument pack.
7676 WRAP_LOCATIONS_P is true if expressions within this list for which
7677 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7678 their source locations.
7680 Returns a vector of trees. Each element is a representation of an
7681 assignment-expression. NULL is returned if the ( and or ) are
7682 missing. An empty, but allocated, vector is returned on no
7683 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7684 if we are parsing an attribute list for an attribute that wants a
7685 plain identifier argument, normal_attr for an attribute that wants
7686 an expression, or non_attr if we aren't parsing an attribute list. If
7687 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7688 not all of the expressions in the list were constant.
7689 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7690 will be written to with the location of the closing parenthesis. If
7691 an error occurs, it may or may not be written to. */
7693 static vec<tree, va_gc> *
7694 cp_parser_parenthesized_expression_list (cp_parser* parser,
7695 int is_attribute_list,
7696 bool cast_p,
7697 bool allow_expansion_p,
7698 bool *non_constant_p,
7699 location_t *close_paren_loc,
7700 bool wrap_locations_p)
7702 vec<tree, va_gc> *expression_list;
7703 bool fold_expr_p = is_attribute_list != non_attr;
7704 tree identifier = NULL_TREE;
7705 bool saved_greater_than_is_operator_p;
7707 /* Assume all the expressions will be constant. */
7708 if (non_constant_p)
7709 *non_constant_p = false;
7711 matching_parens parens;
7712 if (!parens.require_open (parser))
7713 return NULL;
7715 expression_list = make_tree_vector ();
7717 /* Within a parenthesized expression, a `>' token is always
7718 the greater-than operator. */
7719 saved_greater_than_is_operator_p
7720 = parser->greater_than_is_operator_p;
7721 parser->greater_than_is_operator_p = true;
7723 cp_expr expr (NULL_TREE);
7725 /* Consume expressions until there are no more. */
7726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7727 while (true)
7729 /* At the beginning of attribute lists, check to see if the
7730 next token is an identifier. */
7731 if (is_attribute_list == id_attr
7732 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7734 cp_token *token;
7736 /* Consume the identifier. */
7737 token = cp_lexer_consume_token (parser->lexer);
7738 /* Save the identifier. */
7739 identifier = token->u.value;
7741 else
7743 bool expr_non_constant_p;
7745 /* Parse the next assignment-expression. */
7746 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7748 /* A braced-init-list. */
7749 cp_lexer_set_source_position (parser->lexer);
7750 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7751 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7752 if (non_constant_p && expr_non_constant_p)
7753 *non_constant_p = true;
7755 else if (non_constant_p)
7757 expr = (cp_parser_constant_expression
7758 (parser, /*allow_non_constant_p=*/true,
7759 &expr_non_constant_p));
7760 if (expr_non_constant_p)
7761 *non_constant_p = true;
7763 else
7764 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7765 cast_p);
7767 if (fold_expr_p)
7768 expr = instantiate_non_dependent_expr (expr);
7770 /* If we have an ellipsis, then this is an expression
7771 expansion. */
7772 if (allow_expansion_p
7773 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7775 /* Consume the `...'. */
7776 cp_lexer_consume_token (parser->lexer);
7778 /* Build the argument pack. */
7779 expr = make_pack_expansion (expr);
7782 if (wrap_locations_p)
7783 expr.maybe_add_location_wrapper ();
7785 /* Add it to the list. We add error_mark_node
7786 expressions to the list, so that we can still tell if
7787 the correct form for a parenthesized expression-list
7788 is found. That gives better errors. */
7789 vec_safe_push (expression_list, expr.get_value ());
7791 if (expr == error_mark_node)
7792 goto skip_comma;
7795 /* After the first item, attribute lists look the same as
7796 expression lists. */
7797 is_attribute_list = non_attr;
7799 get_comma:;
7800 /* If the next token isn't a `,', then we are done. */
7801 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7802 break;
7804 /* Otherwise, consume the `,' and keep going. */
7805 cp_lexer_consume_token (parser->lexer);
7808 if (close_paren_loc)
7809 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7811 if (!parens.require_close (parser))
7813 int ending;
7815 skip_comma:;
7816 /* We try and resync to an unnested comma, as that will give the
7817 user better diagnostics. */
7818 ending = cp_parser_skip_to_closing_parenthesis (parser,
7819 /*recovering=*/true,
7820 /*or_comma=*/true,
7821 /*consume_paren=*/true);
7822 if (ending < 0)
7823 goto get_comma;
7824 if (!ending)
7826 parser->greater_than_is_operator_p
7827 = saved_greater_than_is_operator_p;
7828 return NULL;
7832 parser->greater_than_is_operator_p
7833 = saved_greater_than_is_operator_p;
7835 if (identifier)
7836 vec_safe_insert (expression_list, 0, identifier);
7838 return expression_list;
7841 /* Parse a pseudo-destructor-name.
7843 pseudo-destructor-name:
7844 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7845 :: [opt] nested-name-specifier template template-id :: ~ type-name
7846 :: [opt] nested-name-specifier [opt] ~ type-name
7848 If either of the first two productions is used, sets *SCOPE to the
7849 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7850 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7851 or ERROR_MARK_NODE if the parse fails. */
7853 static void
7854 cp_parser_pseudo_destructor_name (cp_parser* parser,
7855 tree object,
7856 tree* scope,
7857 tree* type)
7859 bool nested_name_specifier_p;
7861 /* Handle ~auto. */
7862 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7863 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7864 && !type_dependent_expression_p (object))
7866 if (cxx_dialect < cxx14)
7867 pedwarn (input_location, 0,
7868 "%<~auto%> only available with "
7869 "-std=c++14 or -std=gnu++14");
7870 cp_lexer_consume_token (parser->lexer);
7871 cp_lexer_consume_token (parser->lexer);
7872 *scope = NULL_TREE;
7873 *type = TREE_TYPE (object);
7874 return;
7877 /* Assume that things will not work out. */
7878 *type = error_mark_node;
7880 /* Look for the optional `::' operator. */
7881 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7882 /* Look for the optional nested-name-specifier. */
7883 nested_name_specifier_p
7884 = (cp_parser_nested_name_specifier_opt (parser,
7885 /*typename_keyword_p=*/false,
7886 /*check_dependency_p=*/true,
7887 /*type_p=*/false,
7888 /*is_declaration=*/false)
7889 != NULL_TREE);
7890 /* Now, if we saw a nested-name-specifier, we might be doing the
7891 second production. */
7892 if (nested_name_specifier_p
7893 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7895 /* Consume the `template' keyword. */
7896 cp_lexer_consume_token (parser->lexer);
7897 /* Parse the template-id. */
7898 cp_parser_template_id (parser,
7899 /*template_keyword_p=*/true,
7900 /*check_dependency_p=*/false,
7901 class_type,
7902 /*is_declaration=*/true);
7903 /* Look for the `::' token. */
7904 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7906 /* If the next token is not a `~', then there might be some
7907 additional qualification. */
7908 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7910 /* At this point, we're looking for "type-name :: ~". The type-name
7911 must not be a class-name, since this is a pseudo-destructor. So,
7912 it must be either an enum-name, or a typedef-name -- both of which
7913 are just identifiers. So, we peek ahead to check that the "::"
7914 and "~" tokens are present; if they are not, then we can avoid
7915 calling type_name. */
7916 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7917 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7918 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7920 cp_parser_error (parser, "non-scalar type");
7921 return;
7924 /* Look for the type-name. */
7925 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7926 if (*scope == error_mark_node)
7927 return;
7929 /* Look for the `::' token. */
7930 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7932 else
7933 *scope = NULL_TREE;
7935 /* Look for the `~'. */
7936 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7938 /* Once we see the ~, this has to be a pseudo-destructor. */
7939 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7940 cp_parser_commit_to_topmost_tentative_parse (parser);
7942 /* Look for the type-name again. We are not responsible for
7943 checking that it matches the first type-name. */
7944 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7947 /* Parse a unary-expression.
7949 unary-expression:
7950 postfix-expression
7951 ++ cast-expression
7952 -- cast-expression
7953 unary-operator cast-expression
7954 sizeof unary-expression
7955 sizeof ( type-id )
7956 alignof ( type-id ) [C++0x]
7957 new-expression
7958 delete-expression
7960 GNU Extensions:
7962 unary-expression:
7963 __extension__ cast-expression
7964 __alignof__ unary-expression
7965 __alignof__ ( type-id )
7966 alignof unary-expression [C++0x]
7967 __real__ cast-expression
7968 __imag__ cast-expression
7969 && identifier
7970 sizeof ( type-id ) { initializer-list , [opt] }
7971 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7972 __alignof__ ( type-id ) { initializer-list , [opt] }
7974 ADDRESS_P is true iff the unary-expression is appearing as the
7975 operand of the `&' operator. CAST_P is true if this expression is
7976 the target of a cast.
7978 Returns a representation of the expression. */
7980 static cp_expr
7981 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7982 bool address_p, bool cast_p, bool decltype_p)
7984 cp_token *token;
7985 enum tree_code unary_operator;
7987 /* Peek at the next token. */
7988 token = cp_lexer_peek_token (parser->lexer);
7989 /* Some keywords give away the kind of expression. */
7990 if (token->type == CPP_KEYWORD)
7992 enum rid keyword = token->keyword;
7994 switch (keyword)
7996 case RID_ALIGNOF:
7997 case RID_SIZEOF:
7999 tree operand, ret;
8000 enum tree_code op;
8001 location_t start_loc = token->location;
8003 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8004 /* Consume the token. */
8005 cp_lexer_consume_token (parser->lexer);
8006 /* Parse the operand. */
8007 operand = cp_parser_sizeof_operand (parser, keyword);
8009 if (TYPE_P (operand))
8010 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8011 else
8013 /* ISO C++ defines alignof only with types, not with
8014 expressions. So pedwarn if alignof is used with a non-
8015 type expression. However, __alignof__ is ok. */
8016 if (id_equal (token->u.value, "alignof"))
8017 pedwarn (token->location, OPT_Wpedantic,
8018 "ISO C++ does not allow %<alignof%> "
8019 "with a non-type");
8021 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8023 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8024 SIZEOF_EXPR with the original operand. */
8025 if (op == SIZEOF_EXPR && ret != error_mark_node)
8027 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8029 if (!processing_template_decl && TYPE_P (operand))
8031 ret = build_min (SIZEOF_EXPR, size_type_node,
8032 build1 (NOP_EXPR, operand,
8033 error_mark_node));
8034 SIZEOF_EXPR_TYPE_P (ret) = 1;
8036 else
8037 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8038 TREE_SIDE_EFFECTS (ret) = 0;
8039 TREE_READONLY (ret) = 1;
8043 /* Construct a location e.g. :
8044 alignof (expr)
8045 ^~~~~~~~~~~~~~
8046 with start == caret at the start of the "alignof"/"sizeof"
8047 token, with the endpoint at the final closing paren. */
8048 location_t finish_loc
8049 = cp_lexer_previous_token (parser->lexer)->location;
8050 location_t compound_loc
8051 = make_location (start_loc, start_loc, finish_loc);
8053 cp_expr ret_expr (ret);
8054 ret_expr.set_location (compound_loc);
8055 ret_expr = ret_expr.maybe_add_location_wrapper ();
8056 return ret_expr;
8059 case RID_NEW:
8060 return cp_parser_new_expression (parser);
8062 case RID_DELETE:
8063 return cp_parser_delete_expression (parser);
8065 case RID_EXTENSION:
8067 /* The saved value of the PEDANTIC flag. */
8068 int saved_pedantic;
8069 tree expr;
8071 /* Save away the PEDANTIC flag. */
8072 cp_parser_extension_opt (parser, &saved_pedantic);
8073 /* Parse the cast-expression. */
8074 expr = cp_parser_simple_cast_expression (parser);
8075 /* Restore the PEDANTIC flag. */
8076 pedantic = saved_pedantic;
8078 return expr;
8081 case RID_REALPART:
8082 case RID_IMAGPART:
8084 tree expression;
8086 /* Consume the `__real__' or `__imag__' token. */
8087 cp_lexer_consume_token (parser->lexer);
8088 /* Parse the cast-expression. */
8089 expression = cp_parser_simple_cast_expression (parser);
8090 /* Create the complete representation. */
8091 return build_x_unary_op (token->location,
8092 (keyword == RID_REALPART
8093 ? REALPART_EXPR : IMAGPART_EXPR),
8094 expression,
8095 tf_warning_or_error);
8097 break;
8099 case RID_TRANSACTION_ATOMIC:
8100 case RID_TRANSACTION_RELAXED:
8101 return cp_parser_transaction_expression (parser, keyword);
8103 case RID_NOEXCEPT:
8105 tree expr;
8106 const char *saved_message;
8107 bool saved_integral_constant_expression_p;
8108 bool saved_non_integral_constant_expression_p;
8109 bool saved_greater_than_is_operator_p;
8111 location_t start_loc = token->location;
8113 cp_lexer_consume_token (parser->lexer);
8114 matching_parens parens;
8115 parens.require_open (parser);
8117 saved_message = parser->type_definition_forbidden_message;
8118 parser->type_definition_forbidden_message
8119 = G_("types may not be defined in %<noexcept%> expressions");
8121 saved_integral_constant_expression_p
8122 = parser->integral_constant_expression_p;
8123 saved_non_integral_constant_expression_p
8124 = parser->non_integral_constant_expression_p;
8125 parser->integral_constant_expression_p = false;
8127 saved_greater_than_is_operator_p
8128 = parser->greater_than_is_operator_p;
8129 parser->greater_than_is_operator_p = true;
8131 ++cp_unevaluated_operand;
8132 ++c_inhibit_evaluation_warnings;
8133 ++cp_noexcept_operand;
8134 expr = cp_parser_expression (parser);
8135 --cp_noexcept_operand;
8136 --c_inhibit_evaluation_warnings;
8137 --cp_unevaluated_operand;
8139 parser->greater_than_is_operator_p
8140 = saved_greater_than_is_operator_p;
8142 parser->integral_constant_expression_p
8143 = saved_integral_constant_expression_p;
8144 parser->non_integral_constant_expression_p
8145 = saved_non_integral_constant_expression_p;
8147 parser->type_definition_forbidden_message = saved_message;
8149 location_t finish_loc
8150 = cp_lexer_peek_token (parser->lexer)->location;
8151 parens.require_close (parser);
8153 /* Construct a location of the form:
8154 noexcept (expr)
8155 ^~~~~~~~~~~~~~~
8156 with start == caret, finishing at the close-paren. */
8157 location_t noexcept_loc
8158 = make_location (start_loc, start_loc, finish_loc);
8160 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8161 noexcept_loc);
8164 default:
8165 break;
8169 /* Look for the `:: new' and `:: delete', which also signal the
8170 beginning of a new-expression, or delete-expression,
8171 respectively. If the next token is `::', then it might be one of
8172 these. */
8173 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8175 enum rid keyword;
8177 /* See if the token after the `::' is one of the keywords in
8178 which we're interested. */
8179 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8180 /* If it's `new', we have a new-expression. */
8181 if (keyword == RID_NEW)
8182 return cp_parser_new_expression (parser);
8183 /* Similarly, for `delete'. */
8184 else if (keyword == RID_DELETE)
8185 return cp_parser_delete_expression (parser);
8188 /* Look for a unary operator. */
8189 unary_operator = cp_parser_unary_operator (token);
8190 /* The `++' and `--' operators can be handled similarly, even though
8191 they are not technically unary-operators in the grammar. */
8192 if (unary_operator == ERROR_MARK)
8194 if (token->type == CPP_PLUS_PLUS)
8195 unary_operator = PREINCREMENT_EXPR;
8196 else if (token->type == CPP_MINUS_MINUS)
8197 unary_operator = PREDECREMENT_EXPR;
8198 /* Handle the GNU address-of-label extension. */
8199 else if (cp_parser_allow_gnu_extensions_p (parser)
8200 && token->type == CPP_AND_AND)
8202 tree identifier;
8203 tree expression;
8204 location_t start_loc = token->location;
8206 /* Consume the '&&' token. */
8207 cp_lexer_consume_token (parser->lexer);
8208 /* Look for the identifier. */
8209 location_t finish_loc
8210 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8211 identifier = cp_parser_identifier (parser);
8212 /* Construct a location of the form:
8213 &&label
8214 ^~~~~~~
8215 with caret==start at the "&&", finish at the end of the label. */
8216 location_t combined_loc
8217 = make_location (start_loc, start_loc, finish_loc);
8218 /* Create an expression representing the address. */
8219 expression = finish_label_address_expr (identifier, combined_loc);
8220 if (cp_parser_non_integral_constant_expression (parser,
8221 NIC_ADDR_LABEL))
8222 expression = error_mark_node;
8223 return expression;
8226 if (unary_operator != ERROR_MARK)
8228 cp_expr cast_expression;
8229 cp_expr expression = error_mark_node;
8230 non_integral_constant non_constant_p = NIC_NONE;
8231 location_t loc = token->location;
8232 tsubst_flags_t complain = complain_flags (decltype_p);
8234 /* Consume the operator token. */
8235 token = cp_lexer_consume_token (parser->lexer);
8236 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8238 /* Parse the cast-expression. */
8239 cast_expression
8240 = cp_parser_cast_expression (parser,
8241 unary_operator == ADDR_EXPR,
8242 /*cast_p=*/false,
8243 /*decltype*/false,
8244 pidk);
8246 /* Make a location:
8247 OP_TOKEN CAST_EXPRESSION
8248 ^~~~~~~~~~~~~~~~~~~~~~~~~
8249 with start==caret at the operator token, and
8250 extending to the end of the cast_expression. */
8251 loc = make_location (loc, loc, cast_expression.get_finish ());
8253 /* Now, build an appropriate representation. */
8254 switch (unary_operator)
8256 case INDIRECT_REF:
8257 non_constant_p = NIC_STAR;
8258 expression = build_x_indirect_ref (loc, cast_expression,
8259 RO_UNARY_STAR,
8260 complain);
8261 /* TODO: build_x_indirect_ref does not always honor the
8262 location, so ensure it is set. */
8263 expression.set_location (loc);
8264 break;
8266 case ADDR_EXPR:
8267 non_constant_p = NIC_ADDR;
8268 /* Fall through. */
8269 case BIT_NOT_EXPR:
8270 expression = build_x_unary_op (loc, unary_operator,
8271 cast_expression,
8272 complain);
8273 /* TODO: build_x_unary_op does not always honor the location,
8274 so ensure it is set. */
8275 expression.set_location (loc);
8276 break;
8278 case PREINCREMENT_EXPR:
8279 case PREDECREMENT_EXPR:
8280 non_constant_p = unary_operator == PREINCREMENT_EXPR
8281 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8282 /* Fall through. */
8283 case NEGATE_EXPR:
8284 /* Immediately fold negation of a constant, unless the constant is 0
8285 (since -0 == 0) or it would overflow. */
8286 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8287 && CONSTANT_CLASS_P (cast_expression)
8288 && !integer_zerop (cast_expression)
8289 && !TREE_OVERFLOW (cast_expression))
8291 tree folded = fold_build1 (unary_operator,
8292 TREE_TYPE (cast_expression),
8293 cast_expression);
8294 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8296 expression = cp_expr (folded, loc);
8297 break;
8300 /* Fall through. */
8301 case UNARY_PLUS_EXPR:
8302 case TRUTH_NOT_EXPR:
8303 expression = finish_unary_op_expr (loc, unary_operator,
8304 cast_expression, complain);
8305 break;
8307 default:
8308 gcc_unreachable ();
8311 if (non_constant_p != NIC_NONE
8312 && cp_parser_non_integral_constant_expression (parser,
8313 non_constant_p))
8314 expression = error_mark_node;
8316 return expression;
8319 return cp_parser_postfix_expression (parser, address_p, cast_p,
8320 /*member_access_only_p=*/false,
8321 decltype_p,
8322 pidk);
8325 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8326 unary-operator, the corresponding tree code is returned. */
8328 static enum tree_code
8329 cp_parser_unary_operator (cp_token* token)
8331 switch (token->type)
8333 case CPP_MULT:
8334 return INDIRECT_REF;
8336 case CPP_AND:
8337 return ADDR_EXPR;
8339 case CPP_PLUS:
8340 return UNARY_PLUS_EXPR;
8342 case CPP_MINUS:
8343 return NEGATE_EXPR;
8345 case CPP_NOT:
8346 return TRUTH_NOT_EXPR;
8348 case CPP_COMPL:
8349 return BIT_NOT_EXPR;
8351 default:
8352 return ERROR_MARK;
8356 /* Parse a new-expression.
8358 new-expression:
8359 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8360 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8362 Returns a representation of the expression. */
8364 static tree
8365 cp_parser_new_expression (cp_parser* parser)
8367 bool global_scope_p;
8368 vec<tree, va_gc> *placement;
8369 tree type;
8370 vec<tree, va_gc> *initializer;
8371 tree nelts = NULL_TREE;
8372 tree ret;
8374 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8376 /* Look for the optional `::' operator. */
8377 global_scope_p
8378 = (cp_parser_global_scope_opt (parser,
8379 /*current_scope_valid_p=*/false)
8380 != NULL_TREE);
8381 /* Look for the `new' operator. */
8382 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8383 /* There's no easy way to tell a new-placement from the
8384 `( type-id )' construct. */
8385 cp_parser_parse_tentatively (parser);
8386 /* Look for a new-placement. */
8387 placement = cp_parser_new_placement (parser);
8388 /* If that didn't work out, there's no new-placement. */
8389 if (!cp_parser_parse_definitely (parser))
8391 if (placement != NULL)
8392 release_tree_vector (placement);
8393 placement = NULL;
8396 /* If the next token is a `(', then we have a parenthesized
8397 type-id. */
8398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8400 cp_token *token;
8401 const char *saved_message = parser->type_definition_forbidden_message;
8403 /* Consume the `('. */
8404 matching_parens parens;
8405 parens.consume_open (parser);
8407 /* Parse the type-id. */
8408 parser->type_definition_forbidden_message
8409 = G_("types may not be defined in a new-expression");
8411 type_id_in_expr_sentinel s (parser);
8412 type = cp_parser_type_id (parser);
8414 parser->type_definition_forbidden_message = saved_message;
8416 /* Look for the closing `)'. */
8417 parens.require_close (parser);
8418 token = cp_lexer_peek_token (parser->lexer);
8419 /* There should not be a direct-new-declarator in this production,
8420 but GCC used to allowed this, so we check and emit a sensible error
8421 message for this case. */
8422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8424 error_at (token->location,
8425 "array bound forbidden after parenthesized type-id");
8426 inform (token->location,
8427 "try removing the parentheses around the type-id");
8428 cp_parser_direct_new_declarator (parser);
8431 /* Otherwise, there must be a new-type-id. */
8432 else
8433 type = cp_parser_new_type_id (parser, &nelts);
8435 /* If the next token is a `(' or '{', then we have a new-initializer. */
8436 cp_token *token = cp_lexer_peek_token (parser->lexer);
8437 if (token->type == CPP_OPEN_PAREN
8438 || token->type == CPP_OPEN_BRACE)
8439 initializer = cp_parser_new_initializer (parser);
8440 else
8441 initializer = NULL;
8443 /* A new-expression may not appear in an integral constant
8444 expression. */
8445 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8446 ret = error_mark_node;
8447 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8448 of a new-type-id or type-id of a new-expression, the new-expression shall
8449 contain a new-initializer of the form ( assignment-expression )".
8450 Additionally, consistently with the spirit of DR 1467, we want to accept
8451 'new auto { 2 }' too. */
8452 else if ((ret = type_uses_auto (type))
8453 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8454 && (vec_safe_length (initializer) != 1
8455 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8456 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8458 error_at (token->location,
8459 "initialization of new-expression for type %<auto%> "
8460 "requires exactly one element");
8461 ret = error_mark_node;
8463 else
8465 /* Construct a location e.g.:
8466 ptr = new int[100]
8467 ^~~~~~~~~~~~
8468 with caret == start at the start of the "new" token, and the end
8469 at the end of the final token we consumed. */
8470 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8471 location_t end_loc = get_finish (end_tok->location);
8472 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8474 /* Create a representation of the new-expression. */
8475 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8476 tf_warning_or_error);
8477 protected_set_expr_location (ret, combined_loc);
8480 if (placement != NULL)
8481 release_tree_vector (placement);
8482 if (initializer != NULL)
8483 release_tree_vector (initializer);
8485 return ret;
8488 /* Parse a new-placement.
8490 new-placement:
8491 ( expression-list )
8493 Returns the same representation as for an expression-list. */
8495 static vec<tree, va_gc> *
8496 cp_parser_new_placement (cp_parser* parser)
8498 vec<tree, va_gc> *expression_list;
8500 /* Parse the expression-list. */
8501 expression_list = (cp_parser_parenthesized_expression_list
8502 (parser, non_attr, /*cast_p=*/false,
8503 /*allow_expansion_p=*/true,
8504 /*non_constant_p=*/NULL));
8506 if (expression_list && expression_list->is_empty ())
8507 error ("expected expression-list or type-id");
8509 return expression_list;
8512 /* Parse a new-type-id.
8514 new-type-id:
8515 type-specifier-seq new-declarator [opt]
8517 Returns the TYPE allocated. If the new-type-id indicates an array
8518 type, *NELTS is set to the number of elements in the last array
8519 bound; the TYPE will not include the last array bound. */
8521 static tree
8522 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8524 cp_decl_specifier_seq type_specifier_seq;
8525 cp_declarator *new_declarator;
8526 cp_declarator *declarator;
8527 cp_declarator *outer_declarator;
8528 const char *saved_message;
8530 /* The type-specifier sequence must not contain type definitions.
8531 (It cannot contain declarations of new types either, but if they
8532 are not definitions we will catch that because they are not
8533 complete.) */
8534 saved_message = parser->type_definition_forbidden_message;
8535 parser->type_definition_forbidden_message
8536 = G_("types may not be defined in a new-type-id");
8537 /* Parse the type-specifier-seq. */
8538 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8539 /*is_trailing_return=*/false,
8540 &type_specifier_seq);
8541 /* Restore the old message. */
8542 parser->type_definition_forbidden_message = saved_message;
8544 if (type_specifier_seq.type == error_mark_node)
8545 return error_mark_node;
8547 /* Parse the new-declarator. */
8548 new_declarator = cp_parser_new_declarator_opt (parser);
8550 /* Determine the number of elements in the last array dimension, if
8551 any. */
8552 *nelts = NULL_TREE;
8553 /* Skip down to the last array dimension. */
8554 declarator = new_declarator;
8555 outer_declarator = NULL;
8556 while (declarator && (declarator->kind == cdk_pointer
8557 || declarator->kind == cdk_ptrmem))
8559 outer_declarator = declarator;
8560 declarator = declarator->declarator;
8562 while (declarator
8563 && declarator->kind == cdk_array
8564 && declarator->declarator
8565 && declarator->declarator->kind == cdk_array)
8567 outer_declarator = declarator;
8568 declarator = declarator->declarator;
8571 if (declarator && declarator->kind == cdk_array)
8573 *nelts = declarator->u.array.bounds;
8574 if (*nelts == error_mark_node)
8575 *nelts = integer_one_node;
8577 if (outer_declarator)
8578 outer_declarator->declarator = declarator->declarator;
8579 else
8580 new_declarator = NULL;
8583 return groktypename (&type_specifier_seq, new_declarator, false);
8586 /* Parse an (optional) new-declarator.
8588 new-declarator:
8589 ptr-operator new-declarator [opt]
8590 direct-new-declarator
8592 Returns the declarator. */
8594 static cp_declarator *
8595 cp_parser_new_declarator_opt (cp_parser* parser)
8597 enum tree_code code;
8598 tree type, std_attributes = NULL_TREE;
8599 cp_cv_quals cv_quals;
8601 /* We don't know if there's a ptr-operator next, or not. */
8602 cp_parser_parse_tentatively (parser);
8603 /* Look for a ptr-operator. */
8604 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8605 /* If that worked, look for more new-declarators. */
8606 if (cp_parser_parse_definitely (parser))
8608 cp_declarator *declarator;
8610 /* Parse another optional declarator. */
8611 declarator = cp_parser_new_declarator_opt (parser);
8613 declarator = cp_parser_make_indirect_declarator
8614 (code, type, cv_quals, declarator, std_attributes);
8616 return declarator;
8619 /* If the next token is a `[', there is a direct-new-declarator. */
8620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8621 return cp_parser_direct_new_declarator (parser);
8623 return NULL;
8626 /* Parse a direct-new-declarator.
8628 direct-new-declarator:
8629 [ expression ]
8630 direct-new-declarator [constant-expression]
8634 static cp_declarator *
8635 cp_parser_direct_new_declarator (cp_parser* parser)
8637 cp_declarator *declarator = NULL;
8639 while (true)
8641 tree expression;
8642 cp_token *token;
8644 /* Look for the opening `['. */
8645 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8647 token = cp_lexer_peek_token (parser->lexer);
8648 expression = cp_parser_expression (parser);
8649 /* The standard requires that the expression have integral
8650 type. DR 74 adds enumeration types. We believe that the
8651 real intent is that these expressions be handled like the
8652 expression in a `switch' condition, which also allows
8653 classes with a single conversion to integral or
8654 enumeration type. */
8655 if (!processing_template_decl)
8657 expression
8658 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8659 expression,
8660 /*complain=*/true);
8661 if (!expression)
8663 error_at (token->location,
8664 "expression in new-declarator must have integral "
8665 "or enumeration type");
8666 expression = error_mark_node;
8670 /* Look for the closing `]'. */
8671 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8673 /* Add this bound to the declarator. */
8674 declarator = make_array_declarator (declarator, expression);
8676 /* If the next token is not a `[', then there are no more
8677 bounds. */
8678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8679 break;
8682 return declarator;
8685 /* Parse a new-initializer.
8687 new-initializer:
8688 ( expression-list [opt] )
8689 braced-init-list
8691 Returns a representation of the expression-list. */
8693 static vec<tree, va_gc> *
8694 cp_parser_new_initializer (cp_parser* parser)
8696 vec<tree, va_gc> *expression_list;
8698 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8700 tree t;
8701 bool expr_non_constant_p;
8702 cp_lexer_set_source_position (parser->lexer);
8703 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8704 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8705 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8706 expression_list = make_tree_vector_single (t);
8708 else
8709 expression_list = (cp_parser_parenthesized_expression_list
8710 (parser, non_attr, /*cast_p=*/false,
8711 /*allow_expansion_p=*/true,
8712 /*non_constant_p=*/NULL));
8714 return expression_list;
8717 /* Parse a delete-expression.
8719 delete-expression:
8720 :: [opt] delete cast-expression
8721 :: [opt] delete [ ] cast-expression
8723 Returns a representation of the expression. */
8725 static tree
8726 cp_parser_delete_expression (cp_parser* parser)
8728 bool global_scope_p;
8729 bool array_p;
8730 tree expression;
8732 /* Look for the optional `::' operator. */
8733 global_scope_p
8734 = (cp_parser_global_scope_opt (parser,
8735 /*current_scope_valid_p=*/false)
8736 != NULL_TREE);
8737 /* Look for the `delete' keyword. */
8738 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8739 /* See if the array syntax is in use. */
8740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8742 /* Consume the `[' token. */
8743 cp_lexer_consume_token (parser->lexer);
8744 /* Look for the `]' token. */
8745 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8746 /* Remember that this is the `[]' construct. */
8747 array_p = true;
8749 else
8750 array_p = false;
8752 /* Parse the cast-expression. */
8753 expression = cp_parser_simple_cast_expression (parser);
8755 /* A delete-expression may not appear in an integral constant
8756 expression. */
8757 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8758 return error_mark_node;
8760 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8761 tf_warning_or_error);
8764 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8765 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8766 0 otherwise. */
8768 static int
8769 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8771 cp_token *token = cp_lexer_peek_token (parser->lexer);
8772 switch (token->type)
8774 case CPP_COMMA:
8775 case CPP_SEMICOLON:
8776 case CPP_QUERY:
8777 case CPP_COLON:
8778 case CPP_CLOSE_SQUARE:
8779 case CPP_CLOSE_PAREN:
8780 case CPP_CLOSE_BRACE:
8781 case CPP_OPEN_BRACE:
8782 case CPP_DOT:
8783 case CPP_DOT_STAR:
8784 case CPP_DEREF:
8785 case CPP_DEREF_STAR:
8786 case CPP_DIV:
8787 case CPP_MOD:
8788 case CPP_LSHIFT:
8789 case CPP_RSHIFT:
8790 case CPP_LESS:
8791 case CPP_GREATER:
8792 case CPP_LESS_EQ:
8793 case CPP_GREATER_EQ:
8794 case CPP_EQ_EQ:
8795 case CPP_NOT_EQ:
8796 case CPP_EQ:
8797 case CPP_MULT_EQ:
8798 case CPP_DIV_EQ:
8799 case CPP_MOD_EQ:
8800 case CPP_PLUS_EQ:
8801 case CPP_MINUS_EQ:
8802 case CPP_RSHIFT_EQ:
8803 case CPP_LSHIFT_EQ:
8804 case CPP_AND_EQ:
8805 case CPP_XOR_EQ:
8806 case CPP_OR_EQ:
8807 case CPP_XOR:
8808 case CPP_OR:
8809 case CPP_OR_OR:
8810 case CPP_EOF:
8811 case CPP_ELLIPSIS:
8812 return 0;
8814 case CPP_OPEN_PAREN:
8815 /* In ((type ()) () the last () isn't a valid cast-expression,
8816 so the whole must be parsed as postfix-expression. */
8817 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8818 != CPP_CLOSE_PAREN;
8820 case CPP_OPEN_SQUARE:
8821 /* '[' may start a primary-expression in obj-c++ and in C++11,
8822 as a lambda-expression, eg, '(void)[]{}'. */
8823 if (cxx_dialect >= cxx11)
8824 return -1;
8825 return c_dialect_objc ();
8827 case CPP_PLUS_PLUS:
8828 case CPP_MINUS_MINUS:
8829 /* '++' and '--' may or may not start a cast-expression:
8831 struct T { void operator++(int); };
8832 void f() { (T())++; }
8836 int a;
8837 (int)++a; */
8838 return -1;
8840 default:
8841 return 1;
8845 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8846 in the order: const_cast, static_cast, reinterpret_cast.
8848 Don't suggest dynamic_cast.
8850 Return the first legal cast kind found, or NULL otherwise. */
8852 static const char *
8853 get_cast_suggestion (tree dst_type, tree orig_expr)
8855 tree trial;
8857 /* Reuse the parser logic by attempting to build the various kinds of
8858 cast, with "complain" disabled.
8859 Identify the first such cast that is valid. */
8861 /* Don't attempt to run such logic within template processing. */
8862 if (processing_template_decl)
8863 return NULL;
8865 /* First try const_cast. */
8866 trial = build_const_cast (dst_type, orig_expr, tf_none);
8867 if (trial != error_mark_node)
8868 return "const_cast";
8870 /* If that fails, try static_cast. */
8871 trial = build_static_cast (dst_type, orig_expr, tf_none);
8872 if (trial != error_mark_node)
8873 return "static_cast";
8875 /* Finally, try reinterpret_cast. */
8876 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8877 if (trial != error_mark_node)
8878 return "reinterpret_cast";
8880 /* No such cast possible. */
8881 return NULL;
8884 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8885 suggesting how to convert a C-style cast of the form:
8887 (DST_TYPE)ORIG_EXPR
8889 to a C++-style cast.
8891 The primary range of RICHLOC is asssumed to be that of the original
8892 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8893 of the parens in the C-style cast. */
8895 static void
8896 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8897 location_t close_paren_loc, tree orig_expr,
8898 tree dst_type)
8900 /* This function is non-trivial, so bail out now if the warning isn't
8901 going to be emitted. */
8902 if (!warn_old_style_cast)
8903 return;
8905 /* Try to find a legal C++ cast, trying them in order:
8906 const_cast, static_cast, reinterpret_cast. */
8907 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8908 if (!cast_suggestion)
8909 return;
8911 /* Replace the open paren with "CAST_SUGGESTION<". */
8912 pretty_printer pp;
8913 pp_printf (&pp, "%s<", cast_suggestion);
8914 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8916 /* Replace the close paren with "> (". */
8917 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8919 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8920 rich_loc->add_fixit_insert_after (")");
8924 /* Parse a cast-expression.
8926 cast-expression:
8927 unary-expression
8928 ( type-id ) cast-expression
8930 ADDRESS_P is true iff the unary-expression is appearing as the
8931 operand of the `&' operator. CAST_P is true if this expression is
8932 the target of a cast.
8934 Returns a representation of the expression. */
8936 static cp_expr
8937 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8938 bool decltype_p, cp_id_kind * pidk)
8940 /* If it's a `(', then we might be looking at a cast. */
8941 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8943 tree type = NULL_TREE;
8944 cp_expr expr (NULL_TREE);
8945 int cast_expression = 0;
8946 const char *saved_message;
8948 /* There's no way to know yet whether or not this is a cast.
8949 For example, `(int (3))' is a unary-expression, while `(int)
8950 3' is a cast. So, we resort to parsing tentatively. */
8951 cp_parser_parse_tentatively (parser);
8952 /* Types may not be defined in a cast. */
8953 saved_message = parser->type_definition_forbidden_message;
8954 parser->type_definition_forbidden_message
8955 = G_("types may not be defined in casts");
8956 /* Consume the `('. */
8957 matching_parens parens;
8958 cp_token *open_paren = parens.consume_open (parser);
8959 location_t open_paren_loc = open_paren->location;
8960 location_t close_paren_loc = UNKNOWN_LOCATION;
8962 /* A very tricky bit is that `(struct S) { 3 }' is a
8963 compound-literal (which we permit in C++ as an extension).
8964 But, that construct is not a cast-expression -- it is a
8965 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8966 is legal; if the compound-literal were a cast-expression,
8967 you'd need an extra set of parentheses.) But, if we parse
8968 the type-id, and it happens to be a class-specifier, then we
8969 will commit to the parse at that point, because we cannot
8970 undo the action that is done when creating a new class. So,
8971 then we cannot back up and do a postfix-expression.
8973 Another tricky case is the following (c++/29234):
8975 struct S { void operator () (); };
8977 void foo ()
8979 ( S()() );
8982 As a type-id we parse the parenthesized S()() as a function
8983 returning a function, groktypename complains and we cannot
8984 back up in this case either.
8986 Therefore, we scan ahead to the closing `)', and check to see
8987 if the tokens after the `)' can start a cast-expression. Otherwise
8988 we are dealing with an unary-expression, a postfix-expression
8989 or something else.
8991 Yet another tricky case, in C++11, is the following (c++/54891):
8993 (void)[]{};
8995 The issue is that usually, besides the case of lambda-expressions,
8996 the parenthesized type-id cannot be followed by '[', and, eg, we
8997 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8998 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8999 we don't commit, we try a cast-expression, then an unary-expression.
9001 Save tokens so that we can put them back. */
9002 cp_lexer_save_tokens (parser->lexer);
9004 /* We may be looking at a cast-expression. */
9005 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9006 /*consume_paren=*/true))
9007 cast_expression
9008 = cp_parser_tokens_start_cast_expression (parser);
9010 /* Roll back the tokens we skipped. */
9011 cp_lexer_rollback_tokens (parser->lexer);
9012 /* If we aren't looking at a cast-expression, simulate an error so
9013 that the call to cp_parser_error_occurred below returns true. */
9014 if (!cast_expression)
9015 cp_parser_simulate_error (parser);
9016 else
9018 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9019 parser->in_type_id_in_expr_p = true;
9020 /* Look for the type-id. */
9021 type = cp_parser_type_id (parser);
9022 /* Look for the closing `)'. */
9023 cp_token *close_paren = parens.require_close (parser);
9024 if (close_paren)
9025 close_paren_loc = close_paren->location;
9026 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9029 /* Restore the saved message. */
9030 parser->type_definition_forbidden_message = saved_message;
9032 /* At this point this can only be either a cast or a
9033 parenthesized ctor such as `(T ())' that looks like a cast to
9034 function returning T. */
9035 if (!cp_parser_error_occurred (parser))
9037 /* Only commit if the cast-expression doesn't start with
9038 '++', '--', or '[' in C++11. */
9039 if (cast_expression > 0)
9040 cp_parser_commit_to_topmost_tentative_parse (parser);
9042 expr = cp_parser_cast_expression (parser,
9043 /*address_p=*/false,
9044 /*cast_p=*/true,
9045 /*decltype_p=*/false,
9046 pidk);
9048 if (cp_parser_parse_definitely (parser))
9050 /* Warn about old-style casts, if so requested. */
9051 if (warn_old_style_cast
9052 && !in_system_header_at (input_location)
9053 && !VOID_TYPE_P (type)
9054 && current_lang_name != lang_name_c)
9056 gcc_rich_location rich_loc (input_location);
9057 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9058 expr, type);
9059 warning_at (&rich_loc, OPT_Wold_style_cast,
9060 "use of old-style cast to %q#T", type);
9063 /* Only type conversions to integral or enumeration types
9064 can be used in constant-expressions. */
9065 if (!cast_valid_in_integral_constant_expression_p (type)
9066 && cp_parser_non_integral_constant_expression (parser,
9067 NIC_CAST))
9068 return error_mark_node;
9070 /* Perform the cast. */
9071 /* Make a location:
9072 (TYPE) EXPR
9073 ^~~~~~~~~~~
9074 with start==caret at the open paren, extending to the
9075 end of "expr". */
9076 location_t cast_loc = make_location (open_paren_loc,
9077 open_paren_loc,
9078 expr.get_finish ());
9079 expr = build_c_cast (cast_loc, type, expr);
9080 return expr;
9083 else
9084 cp_parser_abort_tentative_parse (parser);
9087 /* If we get here, then it's not a cast, so it must be a
9088 unary-expression. */
9089 return cp_parser_unary_expression (parser, pidk, address_p,
9090 cast_p, decltype_p);
9093 /* Parse a binary expression of the general form:
9095 pm-expression:
9096 cast-expression
9097 pm-expression .* cast-expression
9098 pm-expression ->* cast-expression
9100 multiplicative-expression:
9101 pm-expression
9102 multiplicative-expression * pm-expression
9103 multiplicative-expression / pm-expression
9104 multiplicative-expression % pm-expression
9106 additive-expression:
9107 multiplicative-expression
9108 additive-expression + multiplicative-expression
9109 additive-expression - multiplicative-expression
9111 shift-expression:
9112 additive-expression
9113 shift-expression << additive-expression
9114 shift-expression >> additive-expression
9116 relational-expression:
9117 shift-expression
9118 relational-expression < shift-expression
9119 relational-expression > shift-expression
9120 relational-expression <= shift-expression
9121 relational-expression >= shift-expression
9123 GNU Extension:
9125 relational-expression:
9126 relational-expression <? shift-expression
9127 relational-expression >? shift-expression
9129 equality-expression:
9130 relational-expression
9131 equality-expression == relational-expression
9132 equality-expression != relational-expression
9134 and-expression:
9135 equality-expression
9136 and-expression & equality-expression
9138 exclusive-or-expression:
9139 and-expression
9140 exclusive-or-expression ^ and-expression
9142 inclusive-or-expression:
9143 exclusive-or-expression
9144 inclusive-or-expression | exclusive-or-expression
9146 logical-and-expression:
9147 inclusive-or-expression
9148 logical-and-expression && inclusive-or-expression
9150 logical-or-expression:
9151 logical-and-expression
9152 logical-or-expression || logical-and-expression
9154 All these are implemented with a single function like:
9156 binary-expression:
9157 simple-cast-expression
9158 binary-expression <token> binary-expression
9160 CAST_P is true if this expression is the target of a cast.
9162 The binops_by_token map is used to get the tree codes for each <token> type.
9163 binary-expressions are associated according to a precedence table. */
9165 #define TOKEN_PRECEDENCE(token) \
9166 (((token->type == CPP_GREATER \
9167 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9168 && !parser->greater_than_is_operator_p) \
9169 ? PREC_NOT_OPERATOR \
9170 : binops_by_token[token->type].prec)
9172 static cp_expr
9173 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9174 bool no_toplevel_fold_p,
9175 bool decltype_p,
9176 enum cp_parser_prec prec,
9177 cp_id_kind * pidk)
9179 cp_parser_expression_stack stack;
9180 cp_parser_expression_stack_entry *sp = &stack[0];
9181 cp_parser_expression_stack_entry current;
9182 cp_expr rhs;
9183 cp_token *token;
9184 enum tree_code rhs_type;
9185 enum cp_parser_prec new_prec, lookahead_prec;
9186 tree overload;
9188 /* Parse the first expression. */
9189 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9190 ? TRUTH_NOT_EXPR : ERROR_MARK);
9191 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9192 cast_p, decltype_p, pidk);
9193 current.prec = prec;
9195 if (cp_parser_error_occurred (parser))
9196 return error_mark_node;
9198 for (;;)
9200 /* Get an operator token. */
9201 token = cp_lexer_peek_token (parser->lexer);
9203 if (warn_cxx11_compat
9204 && token->type == CPP_RSHIFT
9205 && !parser->greater_than_is_operator_p)
9207 if (warning_at (token->location, OPT_Wc__11_compat,
9208 "%<>>%> operator is treated"
9209 " as two right angle brackets in C++11"))
9210 inform (token->location,
9211 "suggest parentheses around %<>>%> expression");
9214 new_prec = TOKEN_PRECEDENCE (token);
9215 if (new_prec != PREC_NOT_OPERATOR
9216 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9217 /* This is a fold-expression; handle it later. */
9218 new_prec = PREC_NOT_OPERATOR;
9220 /* Popping an entry off the stack means we completed a subexpression:
9221 - either we found a token which is not an operator (`>' where it is not
9222 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9223 will happen repeatedly;
9224 - or, we found an operator which has lower priority. This is the case
9225 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9226 parsing `3 * 4'. */
9227 if (new_prec <= current.prec)
9229 if (sp == stack)
9230 break;
9231 else
9232 goto pop;
9235 get_rhs:
9236 current.tree_type = binops_by_token[token->type].tree_type;
9237 current.loc = token->location;
9239 /* We used the operator token. */
9240 cp_lexer_consume_token (parser->lexer);
9242 /* For "false && x" or "true || x", x will never be executed;
9243 disable warnings while evaluating it. */
9244 if (current.tree_type == TRUTH_ANDIF_EXPR)
9245 c_inhibit_evaluation_warnings +=
9246 cp_fully_fold (current.lhs) == truthvalue_false_node;
9247 else if (current.tree_type == TRUTH_ORIF_EXPR)
9248 c_inhibit_evaluation_warnings +=
9249 cp_fully_fold (current.lhs) == truthvalue_true_node;
9251 /* Extract another operand. It may be the RHS of this expression
9252 or the LHS of a new, higher priority expression. */
9253 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9254 ? TRUTH_NOT_EXPR : ERROR_MARK);
9255 rhs = cp_parser_simple_cast_expression (parser);
9257 /* Get another operator token. Look up its precedence to avoid
9258 building a useless (immediately popped) stack entry for common
9259 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9260 token = cp_lexer_peek_token (parser->lexer);
9261 lookahead_prec = TOKEN_PRECEDENCE (token);
9262 if (lookahead_prec != PREC_NOT_OPERATOR
9263 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9264 lookahead_prec = PREC_NOT_OPERATOR;
9265 if (lookahead_prec > new_prec)
9267 /* ... and prepare to parse the RHS of the new, higher priority
9268 expression. Since precedence levels on the stack are
9269 monotonically increasing, we do not have to care about
9270 stack overflows. */
9271 *sp = current;
9272 ++sp;
9273 current.lhs = rhs;
9274 current.lhs_type = rhs_type;
9275 current.prec = new_prec;
9276 new_prec = lookahead_prec;
9277 goto get_rhs;
9279 pop:
9280 lookahead_prec = new_prec;
9281 /* If the stack is not empty, we have parsed into LHS the right side
9282 (`4' in the example above) of an expression we had suspended.
9283 We can use the information on the stack to recover the LHS (`3')
9284 from the stack together with the tree code (`MULT_EXPR'), and
9285 the precedence of the higher level subexpression
9286 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9287 which will be used to actually build the additive expression. */
9288 rhs = current.lhs;
9289 rhs_type = current.lhs_type;
9290 --sp;
9291 current = *sp;
9294 /* Undo the disabling of warnings done above. */
9295 if (current.tree_type == TRUTH_ANDIF_EXPR)
9296 c_inhibit_evaluation_warnings -=
9297 cp_fully_fold (current.lhs) == truthvalue_false_node;
9298 else if (current.tree_type == TRUTH_ORIF_EXPR)
9299 c_inhibit_evaluation_warnings -=
9300 cp_fully_fold (current.lhs) == truthvalue_true_node;
9302 if (warn_logical_not_paren
9303 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9304 && current.lhs_type == TRUTH_NOT_EXPR
9305 /* Avoid warning for !!x == y. */
9306 && (TREE_CODE (current.lhs) != NE_EXPR
9307 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9308 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9309 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9310 /* Avoid warning for !b == y where b is boolean. */
9311 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9312 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9313 != BOOLEAN_TYPE))))
9314 /* Avoid warning for !!b == y where b is boolean. */
9315 && (!DECL_P (current.lhs)
9316 || TREE_TYPE (current.lhs) == NULL_TREE
9317 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9318 warn_logical_not_parentheses (current.loc, current.tree_type,
9319 current.lhs, maybe_constant_value (rhs));
9321 overload = NULL;
9323 location_t combined_loc = make_location (current.loc,
9324 current.lhs.get_start (),
9325 rhs.get_finish ());
9327 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9328 ERROR_MARK for everything that is not a binary expression.
9329 This makes warn_about_parentheses miss some warnings that
9330 involve unary operators. For unary expressions we should
9331 pass the correct tree_code unless the unary expression was
9332 surrounded by parentheses.
9334 if (no_toplevel_fold_p
9335 && lookahead_prec <= current.prec
9336 && sp == stack)
9338 if (current.lhs == error_mark_node || rhs == error_mark_node)
9339 current.lhs = error_mark_node;
9340 else
9342 current.lhs
9343 = build_min (current.tree_type,
9344 TREE_CODE_CLASS (current.tree_type)
9345 == tcc_comparison
9346 ? boolean_type_node : TREE_TYPE (current.lhs),
9347 current.lhs.get_value (), rhs.get_value ());
9348 SET_EXPR_LOCATION (current.lhs, combined_loc);
9351 else
9353 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9354 current.lhs, current.lhs_type,
9355 rhs, rhs_type, &overload,
9356 complain_flags (decltype_p));
9357 /* TODO: build_x_binary_op doesn't always honor the location. */
9358 current.lhs.set_location (combined_loc);
9360 current.lhs_type = current.tree_type;
9362 /* If the binary operator required the use of an overloaded operator,
9363 then this expression cannot be an integral constant-expression.
9364 An overloaded operator can be used even if both operands are
9365 otherwise permissible in an integral constant-expression if at
9366 least one of the operands is of enumeration type. */
9368 if (overload
9369 && cp_parser_non_integral_constant_expression (parser,
9370 NIC_OVERLOADED))
9371 return error_mark_node;
9374 return current.lhs;
9377 static cp_expr
9378 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9379 bool no_toplevel_fold_p,
9380 enum cp_parser_prec prec,
9381 cp_id_kind * pidk)
9383 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9384 /*decltype*/false, prec, pidk);
9387 /* Parse the `? expression : assignment-expression' part of a
9388 conditional-expression. The LOGICAL_OR_EXPR is the
9389 logical-or-expression that started the conditional-expression.
9390 Returns a representation of the entire conditional-expression.
9392 This routine is used by cp_parser_assignment_expression.
9394 ? expression : assignment-expression
9396 GNU Extensions:
9398 ? : assignment-expression */
9400 static tree
9401 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9403 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9404 cp_expr assignment_expr;
9405 struct cp_token *token;
9406 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9408 /* Consume the `?' token. */
9409 cp_lexer_consume_token (parser->lexer);
9410 token = cp_lexer_peek_token (parser->lexer);
9411 if (cp_parser_allow_gnu_extensions_p (parser)
9412 && token->type == CPP_COLON)
9414 pedwarn (token->location, OPT_Wpedantic,
9415 "ISO C++ does not allow ?: with omitted middle operand");
9416 /* Implicit true clause. */
9417 expr = NULL_TREE;
9418 c_inhibit_evaluation_warnings +=
9419 folded_logical_or_expr == truthvalue_true_node;
9420 warn_for_omitted_condop (token->location, logical_or_expr);
9422 else
9424 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9425 parser->colon_corrects_to_scope_p = false;
9426 /* Parse the expression. */
9427 c_inhibit_evaluation_warnings +=
9428 folded_logical_or_expr == truthvalue_false_node;
9429 expr = cp_parser_expression (parser);
9430 c_inhibit_evaluation_warnings +=
9431 ((folded_logical_or_expr == truthvalue_true_node)
9432 - (folded_logical_or_expr == truthvalue_false_node));
9433 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9436 /* The next token should be a `:'. */
9437 cp_parser_require (parser, CPP_COLON, RT_COLON);
9438 /* Parse the assignment-expression. */
9439 assignment_expr = cp_parser_assignment_expression (parser);
9440 c_inhibit_evaluation_warnings -=
9441 folded_logical_or_expr == truthvalue_true_node;
9443 /* Make a location:
9444 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9445 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9446 with the caret at the "?", ranging from the start of
9447 the logical_or_expr to the end of the assignment_expr. */
9448 loc = make_location (loc,
9449 logical_or_expr.get_start (),
9450 assignment_expr.get_finish ());
9452 /* Build the conditional-expression. */
9453 return build_x_conditional_expr (loc, logical_or_expr,
9454 expr,
9455 assignment_expr,
9456 tf_warning_or_error);
9459 /* Parse an assignment-expression.
9461 assignment-expression:
9462 conditional-expression
9463 logical-or-expression assignment-operator assignment_expression
9464 throw-expression
9466 CAST_P is true if this expression is the target of a cast.
9467 DECLTYPE_P is true if this expression is the operand of decltype.
9469 Returns a representation for the expression. */
9471 static cp_expr
9472 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9473 bool cast_p, bool decltype_p)
9475 cp_expr expr;
9477 /* If the next token is the `throw' keyword, then we're looking at
9478 a throw-expression. */
9479 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9480 expr = cp_parser_throw_expression (parser);
9481 /* Otherwise, it must be that we are looking at a
9482 logical-or-expression. */
9483 else
9485 /* Parse the binary expressions (logical-or-expression). */
9486 expr = cp_parser_binary_expression (parser, cast_p, false,
9487 decltype_p,
9488 PREC_NOT_OPERATOR, pidk);
9489 /* If the next token is a `?' then we're actually looking at a
9490 conditional-expression. */
9491 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9492 return cp_parser_question_colon_clause (parser, expr);
9493 else
9495 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9497 /* If it's an assignment-operator, we're using the second
9498 production. */
9499 enum tree_code assignment_operator
9500 = cp_parser_assignment_operator_opt (parser);
9501 if (assignment_operator != ERROR_MARK)
9503 bool non_constant_p;
9505 /* Parse the right-hand side of the assignment. */
9506 cp_expr rhs = cp_parser_initializer_clause (parser,
9507 &non_constant_p);
9509 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9510 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9512 /* An assignment may not appear in a
9513 constant-expression. */
9514 if (cp_parser_non_integral_constant_expression (parser,
9515 NIC_ASSIGNMENT))
9516 return error_mark_node;
9517 /* Build the assignment expression. Its default
9518 location:
9519 LHS = RHS
9520 ~~~~^~~~~
9521 is the location of the '=' token as the
9522 caret, ranging from the start of the lhs to the
9523 end of the rhs. */
9524 loc = make_location (loc,
9525 expr.get_start (),
9526 rhs.get_finish ());
9527 expr = build_x_modify_expr (loc, expr,
9528 assignment_operator,
9529 rhs,
9530 complain_flags (decltype_p));
9531 /* TODO: build_x_modify_expr doesn't honor the location,
9532 so we must set it here. */
9533 expr.set_location (loc);
9538 return expr;
9541 /* Parse an (optional) assignment-operator.
9543 assignment-operator: one of
9544 = *= /= %= += -= >>= <<= &= ^= |=
9546 GNU Extension:
9548 assignment-operator: one of
9549 <?= >?=
9551 If the next token is an assignment operator, the corresponding tree
9552 code is returned, and the token is consumed. For example, for
9553 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9554 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9555 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9556 operator, ERROR_MARK is returned. */
9558 static enum tree_code
9559 cp_parser_assignment_operator_opt (cp_parser* parser)
9561 enum tree_code op;
9562 cp_token *token;
9564 /* Peek at the next token. */
9565 token = cp_lexer_peek_token (parser->lexer);
9567 switch (token->type)
9569 case CPP_EQ:
9570 op = NOP_EXPR;
9571 break;
9573 case CPP_MULT_EQ:
9574 op = MULT_EXPR;
9575 break;
9577 case CPP_DIV_EQ:
9578 op = TRUNC_DIV_EXPR;
9579 break;
9581 case CPP_MOD_EQ:
9582 op = TRUNC_MOD_EXPR;
9583 break;
9585 case CPP_PLUS_EQ:
9586 op = PLUS_EXPR;
9587 break;
9589 case CPP_MINUS_EQ:
9590 op = MINUS_EXPR;
9591 break;
9593 case CPP_RSHIFT_EQ:
9594 op = RSHIFT_EXPR;
9595 break;
9597 case CPP_LSHIFT_EQ:
9598 op = LSHIFT_EXPR;
9599 break;
9601 case CPP_AND_EQ:
9602 op = BIT_AND_EXPR;
9603 break;
9605 case CPP_XOR_EQ:
9606 op = BIT_XOR_EXPR;
9607 break;
9609 case CPP_OR_EQ:
9610 op = BIT_IOR_EXPR;
9611 break;
9613 default:
9614 /* Nothing else is an assignment operator. */
9615 op = ERROR_MARK;
9618 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9619 if (op != ERROR_MARK
9620 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9621 op = ERROR_MARK;
9623 /* If it was an assignment operator, consume it. */
9624 if (op != ERROR_MARK)
9625 cp_lexer_consume_token (parser->lexer);
9627 return op;
9630 /* Parse an expression.
9632 expression:
9633 assignment-expression
9634 expression , assignment-expression
9636 CAST_P is true if this expression is the target of a cast.
9637 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9638 except possibly parenthesized or on the RHS of a comma (N3276).
9640 Returns a representation of the expression. */
9642 static cp_expr
9643 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9644 bool cast_p, bool decltype_p)
9646 cp_expr expression = NULL_TREE;
9647 location_t loc = UNKNOWN_LOCATION;
9649 while (true)
9651 cp_expr assignment_expression;
9653 /* Parse the next assignment-expression. */
9654 assignment_expression
9655 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9657 /* We don't create a temporary for a call that is the immediate operand
9658 of decltype or on the RHS of a comma. But when we see a comma, we
9659 need to create a temporary for a call on the LHS. */
9660 if (decltype_p && !processing_template_decl
9661 && TREE_CODE (assignment_expression) == CALL_EXPR
9662 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9663 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9664 assignment_expression
9665 = build_cplus_new (TREE_TYPE (assignment_expression),
9666 assignment_expression, tf_warning_or_error);
9668 /* If this is the first assignment-expression, we can just
9669 save it away. */
9670 if (!expression)
9671 expression = assignment_expression;
9672 else
9674 /* Create a location with caret at the comma, ranging
9675 from the start of the LHS to the end of the RHS. */
9676 loc = make_location (loc,
9677 expression.get_start (),
9678 assignment_expression.get_finish ());
9679 expression = build_x_compound_expr (loc, expression,
9680 assignment_expression,
9681 complain_flags (decltype_p));
9682 expression.set_location (loc);
9684 /* If the next token is not a comma, or we're in a fold-expression, then
9685 we are done with the expression. */
9686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9687 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9688 break;
9689 /* Consume the `,'. */
9690 loc = cp_lexer_peek_token (parser->lexer)->location;
9691 cp_lexer_consume_token (parser->lexer);
9692 /* A comma operator cannot appear in a constant-expression. */
9693 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9694 expression = error_mark_node;
9697 return expression;
9700 /* Parse a constant-expression.
9702 constant-expression:
9703 conditional-expression
9705 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9706 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9707 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9708 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9709 only parse a conditional-expression, otherwise parse an
9710 assignment-expression. See below for rationale. */
9712 static cp_expr
9713 cp_parser_constant_expression (cp_parser* parser,
9714 bool allow_non_constant_p,
9715 bool *non_constant_p,
9716 bool strict_p)
9718 bool saved_integral_constant_expression_p;
9719 bool saved_allow_non_integral_constant_expression_p;
9720 bool saved_non_integral_constant_expression_p;
9721 cp_expr expression;
9723 /* It might seem that we could simply parse the
9724 conditional-expression, and then check to see if it were
9725 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9726 one that the compiler can figure out is constant, possibly after
9727 doing some simplifications or optimizations. The standard has a
9728 precise definition of constant-expression, and we must honor
9729 that, even though it is somewhat more restrictive.
9731 For example:
9733 int i[(2, 3)];
9735 is not a legal declaration, because `(2, 3)' is not a
9736 constant-expression. The `,' operator is forbidden in a
9737 constant-expression. However, GCC's constant-folding machinery
9738 will fold this operation to an INTEGER_CST for `3'. */
9740 /* Save the old settings. */
9741 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9742 saved_allow_non_integral_constant_expression_p
9743 = parser->allow_non_integral_constant_expression_p;
9744 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9745 /* We are now parsing a constant-expression. */
9746 parser->integral_constant_expression_p = true;
9747 parser->allow_non_integral_constant_expression_p
9748 = (allow_non_constant_p || cxx_dialect >= cxx11);
9749 parser->non_integral_constant_expression_p = false;
9750 /* Although the grammar says "conditional-expression", when not STRICT_P,
9751 we parse an "assignment-expression", which also permits
9752 "throw-expression" and the use of assignment operators. In the case
9753 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9754 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9755 actually essential that we look for an assignment-expression.
9756 For example, cp_parser_initializer_clauses uses this function to
9757 determine whether a particular assignment-expression is in fact
9758 constant. */
9759 if (strict_p)
9761 /* Parse the binary expressions (logical-or-expression). */
9762 expression = cp_parser_binary_expression (parser, false, false, false,
9763 PREC_NOT_OPERATOR, NULL);
9764 /* If the next token is a `?' then we're actually looking at
9765 a conditional-expression; otherwise we're done. */
9766 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9767 expression = cp_parser_question_colon_clause (parser, expression);
9769 else
9770 expression = cp_parser_assignment_expression (parser);
9771 /* Restore the old settings. */
9772 parser->integral_constant_expression_p
9773 = saved_integral_constant_expression_p;
9774 parser->allow_non_integral_constant_expression_p
9775 = saved_allow_non_integral_constant_expression_p;
9776 if (cxx_dialect >= cxx11)
9778 /* Require an rvalue constant expression here; that's what our
9779 callers expect. Reference constant expressions are handled
9780 separately in e.g. cp_parser_template_argument. */
9781 tree decay = expression;
9782 if (TREE_TYPE (expression)
9783 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9784 decay = build_address (expression);
9785 bool is_const = potential_rvalue_constant_expression (decay);
9786 parser->non_integral_constant_expression_p = !is_const;
9787 if (!is_const && !allow_non_constant_p)
9788 require_potential_rvalue_constant_expression (decay);
9790 if (allow_non_constant_p)
9791 *non_constant_p = parser->non_integral_constant_expression_p;
9792 parser->non_integral_constant_expression_p
9793 = saved_non_integral_constant_expression_p;
9795 return expression;
9798 /* Parse __builtin_offsetof.
9800 offsetof-expression:
9801 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9803 offsetof-member-designator:
9804 id-expression
9805 | offsetof-member-designator "." id-expression
9806 | offsetof-member-designator "[" expression "]"
9807 | offsetof-member-designator "->" id-expression */
9809 static cp_expr
9810 cp_parser_builtin_offsetof (cp_parser *parser)
9812 int save_ice_p, save_non_ice_p;
9813 tree type;
9814 cp_expr expr;
9815 cp_id_kind dummy;
9816 cp_token *token;
9817 location_t finish_loc;
9819 /* We're about to accept non-integral-constant things, but will
9820 definitely yield an integral constant expression. Save and
9821 restore these values around our local parsing. */
9822 save_ice_p = parser->integral_constant_expression_p;
9823 save_non_ice_p = parser->non_integral_constant_expression_p;
9825 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9827 /* Consume the "__builtin_offsetof" token. */
9828 cp_lexer_consume_token (parser->lexer);
9829 /* Consume the opening `('. */
9830 matching_parens parens;
9831 parens.require_open (parser);
9832 /* Parse the type-id. */
9833 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9834 type = cp_parser_type_id (parser);
9835 /* Look for the `,'. */
9836 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9837 token = cp_lexer_peek_token (parser->lexer);
9839 /* Build the (type *)null that begins the traditional offsetof macro. */
9840 tree object_ptr
9841 = build_static_cast (build_pointer_type (type), null_pointer_node,
9842 tf_warning_or_error);
9844 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9845 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9846 true, &dummy, token->location);
9847 while (true)
9849 token = cp_lexer_peek_token (parser->lexer);
9850 switch (token->type)
9852 case CPP_OPEN_SQUARE:
9853 /* offsetof-member-designator "[" expression "]" */
9854 expr = cp_parser_postfix_open_square_expression (parser, expr,
9855 true, false);
9856 break;
9858 case CPP_DEREF:
9859 /* offsetof-member-designator "->" identifier */
9860 expr = grok_array_decl (token->location, expr,
9861 integer_zero_node, false);
9862 /* FALLTHRU */
9864 case CPP_DOT:
9865 /* offsetof-member-designator "." identifier */
9866 cp_lexer_consume_token (parser->lexer);
9867 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9868 expr, true, &dummy,
9869 token->location);
9870 break;
9872 case CPP_CLOSE_PAREN:
9873 /* Consume the ")" token. */
9874 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9875 cp_lexer_consume_token (parser->lexer);
9876 goto success;
9878 default:
9879 /* Error. We know the following require will fail, but
9880 that gives the proper error message. */
9881 parens.require_close (parser);
9882 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9883 expr = error_mark_node;
9884 goto failure;
9888 success:
9889 /* Make a location of the form:
9890 __builtin_offsetof (struct s, f)
9891 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9892 with caret at the type-id, ranging from the start of the
9893 "_builtin_offsetof" token to the close paren. */
9894 loc = make_location (loc, start_loc, finish_loc);
9895 /* The result will be an INTEGER_CST, so we need to explicitly
9896 preserve the location. */
9897 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9899 failure:
9900 parser->integral_constant_expression_p = save_ice_p;
9901 parser->non_integral_constant_expression_p = save_non_ice_p;
9903 expr = expr.maybe_add_location_wrapper ();
9904 return expr;
9907 /* Parse a trait expression.
9909 Returns a representation of the expression, the underlying type
9910 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9912 static cp_expr
9913 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9915 cp_trait_kind kind;
9916 tree type1, type2 = NULL_TREE;
9917 bool binary = false;
9918 bool variadic = false;
9920 switch (keyword)
9922 case RID_HAS_NOTHROW_ASSIGN:
9923 kind = CPTK_HAS_NOTHROW_ASSIGN;
9924 break;
9925 case RID_HAS_NOTHROW_CONSTRUCTOR:
9926 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9927 break;
9928 case RID_HAS_NOTHROW_COPY:
9929 kind = CPTK_HAS_NOTHROW_COPY;
9930 break;
9931 case RID_HAS_TRIVIAL_ASSIGN:
9932 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9933 break;
9934 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9935 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9936 break;
9937 case RID_HAS_TRIVIAL_COPY:
9938 kind = CPTK_HAS_TRIVIAL_COPY;
9939 break;
9940 case RID_HAS_TRIVIAL_DESTRUCTOR:
9941 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9942 break;
9943 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9944 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9945 break;
9946 case RID_HAS_VIRTUAL_DESTRUCTOR:
9947 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9948 break;
9949 case RID_IS_ABSTRACT:
9950 kind = CPTK_IS_ABSTRACT;
9951 break;
9952 case RID_IS_AGGREGATE:
9953 kind = CPTK_IS_AGGREGATE;
9954 break;
9955 case RID_IS_BASE_OF:
9956 kind = CPTK_IS_BASE_OF;
9957 binary = true;
9958 break;
9959 case RID_IS_CLASS:
9960 kind = CPTK_IS_CLASS;
9961 break;
9962 case RID_IS_EMPTY:
9963 kind = CPTK_IS_EMPTY;
9964 break;
9965 case RID_IS_ENUM:
9966 kind = CPTK_IS_ENUM;
9967 break;
9968 case RID_IS_FINAL:
9969 kind = CPTK_IS_FINAL;
9970 break;
9971 case RID_IS_LITERAL_TYPE:
9972 kind = CPTK_IS_LITERAL_TYPE;
9973 break;
9974 case RID_IS_POD:
9975 kind = CPTK_IS_POD;
9976 break;
9977 case RID_IS_POLYMORPHIC:
9978 kind = CPTK_IS_POLYMORPHIC;
9979 break;
9980 case RID_IS_SAME_AS:
9981 kind = CPTK_IS_SAME_AS;
9982 binary = true;
9983 break;
9984 case RID_IS_STD_LAYOUT:
9985 kind = CPTK_IS_STD_LAYOUT;
9986 break;
9987 case RID_IS_TRIVIAL:
9988 kind = CPTK_IS_TRIVIAL;
9989 break;
9990 case RID_IS_TRIVIALLY_ASSIGNABLE:
9991 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9992 binary = true;
9993 break;
9994 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9995 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9996 variadic = true;
9997 break;
9998 case RID_IS_TRIVIALLY_COPYABLE:
9999 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10000 break;
10001 case RID_IS_UNION:
10002 kind = CPTK_IS_UNION;
10003 break;
10004 case RID_UNDERLYING_TYPE:
10005 kind = CPTK_UNDERLYING_TYPE;
10006 break;
10007 case RID_BASES:
10008 kind = CPTK_BASES;
10009 break;
10010 case RID_DIRECT_BASES:
10011 kind = CPTK_DIRECT_BASES;
10012 break;
10013 case RID_IS_ASSIGNABLE:
10014 kind = CPTK_IS_ASSIGNABLE;
10015 binary = true;
10016 break;
10017 case RID_IS_CONSTRUCTIBLE:
10018 kind = CPTK_IS_CONSTRUCTIBLE;
10019 variadic = true;
10020 break;
10021 default:
10022 gcc_unreachable ();
10025 /* Get location of initial token. */
10026 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10028 /* Consume the token. */
10029 cp_lexer_consume_token (parser->lexer);
10031 matching_parens parens;
10032 parens.require_open (parser);
10035 type_id_in_expr_sentinel s (parser);
10036 type1 = cp_parser_type_id (parser);
10039 if (type1 == error_mark_node)
10040 return error_mark_node;
10042 if (binary)
10044 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10047 type_id_in_expr_sentinel s (parser);
10048 type2 = cp_parser_type_id (parser);
10051 if (type2 == error_mark_node)
10052 return error_mark_node;
10054 else if (variadic)
10056 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10058 cp_lexer_consume_token (parser->lexer);
10059 tree elt = cp_parser_type_id (parser);
10060 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10062 cp_lexer_consume_token (parser->lexer);
10063 elt = make_pack_expansion (elt);
10065 if (elt == error_mark_node)
10066 return error_mark_node;
10067 type2 = tree_cons (NULL_TREE, elt, type2);
10071 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10072 parens.require_close (parser);
10074 /* Construct a location of the form:
10075 __is_trivially_copyable(_Tp)
10076 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10077 with start == caret, finishing at the close-paren. */
10078 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10080 /* Complete the trait expression, which may mean either processing
10081 the trait expr now or saving it for template instantiation. */
10082 switch (kind)
10084 case CPTK_UNDERLYING_TYPE:
10085 return cp_expr (finish_underlying_type (type1), trait_loc);
10086 case CPTK_BASES:
10087 return cp_expr (finish_bases (type1, false), trait_loc);
10088 case CPTK_DIRECT_BASES:
10089 return cp_expr (finish_bases (type1, true), trait_loc);
10090 default:
10091 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10095 /* Parse a lambda expression.
10097 lambda-expression:
10098 lambda-introducer lambda-declarator [opt] compound-statement
10100 Returns a representation of the expression. */
10102 static cp_expr
10103 cp_parser_lambda_expression (cp_parser* parser)
10105 tree lambda_expr = build_lambda_expr ();
10106 tree type;
10107 bool ok = true;
10108 cp_token *token = cp_lexer_peek_token (parser->lexer);
10109 cp_token_position start = 0;
10111 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10113 if (cp_unevaluated_operand)
10115 if (!token->error_reported)
10117 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10118 "lambda-expression in unevaluated context");
10119 token->error_reported = true;
10121 ok = false;
10123 else if (parser->in_template_argument_list_p)
10125 if (!token->error_reported)
10127 error_at (token->location, "lambda-expression in template-argument");
10128 token->error_reported = true;
10130 ok = false;
10133 /* We may be in the middle of deferred access check. Disable
10134 it now. */
10135 push_deferring_access_checks (dk_no_deferred);
10137 cp_parser_lambda_introducer (parser, lambda_expr);
10139 type = begin_lambda_type (lambda_expr);
10140 if (type == error_mark_node)
10141 return error_mark_node;
10143 record_lambda_scope (lambda_expr);
10145 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10146 determine_visibility (TYPE_NAME (type));
10148 /* Now that we've started the type, add the capture fields for any
10149 explicit captures. */
10150 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10153 /* Inside the class, surrounding template-parameter-lists do not apply. */
10154 unsigned int saved_num_template_parameter_lists
10155 = parser->num_template_parameter_lists;
10156 unsigned char in_statement = parser->in_statement;
10157 bool in_switch_statement_p = parser->in_switch_statement_p;
10158 bool fully_implicit_function_template_p
10159 = parser->fully_implicit_function_template_p;
10160 tree implicit_template_parms = parser->implicit_template_parms;
10161 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10162 bool auto_is_implicit_function_template_parm_p
10163 = parser->auto_is_implicit_function_template_parm_p;
10165 parser->num_template_parameter_lists = 0;
10166 parser->in_statement = 0;
10167 parser->in_switch_statement_p = false;
10168 parser->fully_implicit_function_template_p = false;
10169 parser->implicit_template_parms = 0;
10170 parser->implicit_template_scope = 0;
10171 parser->auto_is_implicit_function_template_parm_p = false;
10173 /* By virtue of defining a local class, a lambda expression has access to
10174 the private variables of enclosing classes. */
10176 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10178 if (ok && cp_parser_error_occurred (parser))
10179 ok = false;
10181 if (ok)
10183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10184 && cp_parser_start_tentative_firewall (parser))
10185 start = token;
10186 cp_parser_lambda_body (parser, lambda_expr);
10188 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10190 if (cp_parser_skip_to_closing_brace (parser))
10191 cp_lexer_consume_token (parser->lexer);
10194 /* The capture list was built up in reverse order; fix that now. */
10195 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10196 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10198 if (ok)
10199 maybe_add_lambda_conv_op (type);
10201 type = finish_struct (type, /*attributes=*/NULL_TREE);
10203 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10204 parser->in_statement = in_statement;
10205 parser->in_switch_statement_p = in_switch_statement_p;
10206 parser->fully_implicit_function_template_p
10207 = fully_implicit_function_template_p;
10208 parser->implicit_template_parms = implicit_template_parms;
10209 parser->implicit_template_scope = implicit_template_scope;
10210 parser->auto_is_implicit_function_template_parm_p
10211 = auto_is_implicit_function_template_parm_p;
10214 /* This field is only used during parsing of the lambda. */
10215 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10217 /* This lambda shouldn't have any proxies left at this point. */
10218 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10219 /* And now that we're done, push proxies for an enclosing lambda. */
10220 insert_pending_capture_proxies ();
10222 if (ok)
10223 lambda_expr = build_lambda_object (lambda_expr);
10224 else
10225 lambda_expr = error_mark_node;
10227 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10229 pop_deferring_access_checks ();
10231 return lambda_expr;
10234 /* Parse the beginning of a lambda expression.
10236 lambda-introducer:
10237 [ lambda-capture [opt] ]
10239 LAMBDA_EXPR is the current representation of the lambda expression. */
10241 static void
10242 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10244 /* Need commas after the first capture. */
10245 bool first = true;
10247 /* Eat the leading `['. */
10248 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10250 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10251 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10252 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10253 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10254 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10255 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10257 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10259 cp_lexer_consume_token (parser->lexer);
10260 first = false;
10263 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10265 cp_token* capture_token;
10266 tree capture_id;
10267 tree capture_init_expr;
10268 cp_id_kind idk = CP_ID_KIND_NONE;
10269 bool explicit_init_p = false;
10271 enum capture_kind_type
10273 BY_COPY,
10274 BY_REFERENCE
10276 enum capture_kind_type capture_kind = BY_COPY;
10278 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10280 error ("expected end of capture-list");
10281 return;
10284 if (first)
10285 first = false;
10286 else
10287 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10289 /* Possibly capture `this'. */
10290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10292 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10293 if (cxx_dialect < cxx2a
10294 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10295 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10296 "with by-copy capture default");
10297 cp_lexer_consume_token (parser->lexer);
10298 add_capture (lambda_expr,
10299 /*id=*/this_identifier,
10300 /*initializer=*/finish_this_expr (),
10301 /*by_reference_p=*/true,
10302 explicit_init_p);
10303 continue;
10306 /* Possibly capture `*this'. */
10307 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10308 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10310 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10311 if (cxx_dialect < cxx17)
10312 pedwarn (loc, 0, "%<*this%> capture only available with "
10313 "-std=c++17 or -std=gnu++17");
10314 cp_lexer_consume_token (parser->lexer);
10315 cp_lexer_consume_token (parser->lexer);
10316 add_capture (lambda_expr,
10317 /*id=*/this_identifier,
10318 /*initializer=*/finish_this_expr (),
10319 /*by_reference_p=*/false,
10320 explicit_init_p);
10321 continue;
10324 /* Remember whether we want to capture as a reference or not. */
10325 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10327 capture_kind = BY_REFERENCE;
10328 cp_lexer_consume_token (parser->lexer);
10331 /* Get the identifier. */
10332 capture_token = cp_lexer_peek_token (parser->lexer);
10333 capture_id = cp_parser_identifier (parser);
10335 if (capture_id == error_mark_node)
10336 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10337 delimiters, but I modified this to stop on unnested ']' as well. It
10338 was already changed to stop on unnested '}', so the
10339 "closing_parenthesis" name is no more misleading with my change. */
10341 cp_parser_skip_to_closing_parenthesis (parser,
10342 /*recovering=*/true,
10343 /*or_comma=*/true,
10344 /*consume_paren=*/true);
10345 break;
10348 /* Find the initializer for this capture. */
10349 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10350 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10351 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10353 bool direct, non_constant;
10354 /* An explicit initializer exists. */
10355 if (cxx_dialect < cxx14)
10356 pedwarn (input_location, 0,
10357 "lambda capture initializers "
10358 "only available with -std=c++14 or -std=gnu++14");
10359 capture_init_expr = cp_parser_initializer (parser, &direct,
10360 &non_constant);
10361 explicit_init_p = true;
10362 if (capture_init_expr == NULL_TREE)
10364 error ("empty initializer for lambda init-capture");
10365 capture_init_expr = error_mark_node;
10368 else
10370 const char* error_msg;
10372 /* Turn the identifier into an id-expression. */
10373 capture_init_expr
10374 = cp_parser_lookup_name_simple (parser, capture_id,
10375 capture_token->location);
10377 if (capture_init_expr == error_mark_node)
10379 unqualified_name_lookup_error (capture_id);
10380 continue;
10382 else if (!VAR_P (capture_init_expr)
10383 && TREE_CODE (capture_init_expr) != PARM_DECL)
10385 error_at (capture_token->location,
10386 "capture of non-variable %qE ",
10387 capture_init_expr);
10388 if (DECL_P (capture_init_expr))
10389 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10390 "%q#D declared here", capture_init_expr);
10391 continue;
10393 if (VAR_P (capture_init_expr)
10394 && decl_storage_duration (capture_init_expr) != dk_auto)
10396 if (pedwarn (capture_token->location, 0, "capture of variable "
10397 "%qD with non-automatic storage duration",
10398 capture_init_expr))
10399 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10400 "%q#D declared here", capture_init_expr);
10401 continue;
10404 capture_init_expr
10405 = finish_id_expression
10406 (capture_id,
10407 capture_init_expr,
10408 parser->scope,
10409 &idk,
10410 /*integral_constant_expression_p=*/false,
10411 /*allow_non_integral_constant_expression_p=*/false,
10412 /*non_integral_constant_expression_p=*/NULL,
10413 /*template_p=*/false,
10414 /*done=*/true,
10415 /*address_p=*/false,
10416 /*template_arg_p=*/false,
10417 &error_msg,
10418 capture_token->location);
10420 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10422 cp_lexer_consume_token (parser->lexer);
10423 capture_init_expr = make_pack_expansion (capture_init_expr);
10427 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10428 && !explicit_init_p)
10430 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10431 && capture_kind == BY_COPY)
10432 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10433 "of %qD redundant with by-copy capture default",
10434 capture_id);
10435 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10436 && capture_kind == BY_REFERENCE)
10437 pedwarn (capture_token->location, 0, "explicit by-reference "
10438 "capture of %qD redundant with by-reference capture "
10439 "default", capture_id);
10442 add_capture (lambda_expr,
10443 capture_id,
10444 capture_init_expr,
10445 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10446 explicit_init_p);
10448 /* If there is any qualification still in effect, clear it
10449 now; we will be starting fresh with the next capture. */
10450 parser->scope = NULL_TREE;
10451 parser->qualifying_scope = NULL_TREE;
10452 parser->object_scope = NULL_TREE;
10455 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10458 /* Parse the (optional) middle of a lambda expression.
10460 lambda-declarator:
10461 < template-parameter-list [opt] >
10462 ( parameter-declaration-clause [opt] )
10463 attribute-specifier [opt]
10464 decl-specifier-seq [opt]
10465 exception-specification [opt]
10466 lambda-return-type-clause [opt]
10468 LAMBDA_EXPR is the current representation of the lambda expression. */
10470 static bool
10471 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10473 /* 5.1.1.4 of the standard says:
10474 If a lambda-expression does not include a lambda-declarator, it is as if
10475 the lambda-declarator were ().
10476 This means an empty parameter list, no attributes, and no exception
10477 specification. */
10478 tree param_list = void_list_node;
10479 tree attributes = NULL_TREE;
10480 tree exception_spec = NULL_TREE;
10481 tree template_param_list = NULL_TREE;
10482 tree tx_qual = NULL_TREE;
10483 tree return_type = NULL_TREE;
10484 cp_decl_specifier_seq lambda_specs;
10485 clear_decl_specs (&lambda_specs);
10487 /* The template-parameter-list is optional, but must begin with
10488 an opening angle if present. */
10489 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10491 if (cxx_dialect < cxx14)
10492 pedwarn (parser->lexer->next_token->location, 0,
10493 "lambda templates are only available with "
10494 "-std=c++14 or -std=gnu++14");
10495 else if (cxx_dialect < cxx2a)
10496 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10497 "lambda templates are only available with "
10498 "-std=c++2a or -std=gnu++2a");
10500 cp_lexer_consume_token (parser->lexer);
10502 template_param_list = cp_parser_template_parameter_list (parser);
10504 cp_parser_skip_to_end_of_template_parameter_list (parser);
10506 /* We just processed one more parameter list. */
10507 ++parser->num_template_parameter_lists;
10510 /* The parameter-declaration-clause is optional (unless
10511 template-parameter-list was given), but must begin with an
10512 opening parenthesis if present. */
10513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10515 matching_parens parens;
10516 parens.consume_open (parser);
10518 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10520 /* Parse parameters. */
10521 param_list = cp_parser_parameter_declaration_clause (parser);
10523 /* Default arguments shall not be specified in the
10524 parameter-declaration-clause of a lambda-declarator. */
10525 if (cxx_dialect < cxx14)
10526 for (tree t = param_list; t; t = TREE_CHAIN (t))
10527 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10528 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10529 "default argument specified for lambda parameter");
10531 parens.require_close (parser);
10533 attributes = cp_parser_attributes_opt (parser);
10535 /* In the decl-specifier-seq of the lambda-declarator, each
10536 decl-specifier shall either be mutable or constexpr. */
10537 int declares_class_or_enum;
10538 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10539 cp_parser_decl_specifier_seq (parser,
10540 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10541 &lambda_specs, &declares_class_or_enum);
10542 if (lambda_specs.storage_class == sc_mutable)
10544 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10545 if (lambda_specs.conflicting_specifiers_p)
10546 error_at (lambda_specs.locations[ds_storage_class],
10547 "duplicate %<mutable%>");
10550 tx_qual = cp_parser_tx_qualifier_opt (parser);
10552 /* Parse optional exception specification. */
10553 exception_spec = cp_parser_exception_specification_opt (parser);
10555 /* Parse optional trailing return type. */
10556 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10558 cp_lexer_consume_token (parser->lexer);
10559 return_type = cp_parser_trailing_type_id (parser);
10562 /* The function parameters must be in scope all the way until after the
10563 trailing-return-type in case of decltype. */
10564 pop_bindings_and_leave_scope ();
10566 else if (template_param_list != NULL_TREE) // generate diagnostic
10567 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10569 /* Create the function call operator.
10571 Messing with declarators like this is no uglier than building up the
10572 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10573 other code. */
10575 cp_decl_specifier_seq return_type_specs;
10576 cp_declarator* declarator;
10577 tree fco;
10578 int quals;
10579 void *p;
10581 clear_decl_specs (&return_type_specs);
10582 if (return_type)
10583 return_type_specs.type = return_type;
10584 else
10585 /* Maybe we will deduce the return type later. */
10586 return_type_specs.type = make_auto ();
10588 if (lambda_specs.locations[ds_constexpr])
10590 if (cxx_dialect >= cxx17)
10591 return_type_specs.locations[ds_constexpr]
10592 = lambda_specs.locations[ds_constexpr];
10593 else
10594 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10595 "lambda only available with -std=c++17 or -std=gnu++17");
10598 p = obstack_alloc (&declarator_obstack, 0);
10600 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10602 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10603 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10604 declarator = make_call_declarator (declarator, param_list, quals,
10605 VIRT_SPEC_UNSPECIFIED,
10606 REF_QUAL_NONE,
10607 tx_qual,
10608 exception_spec,
10609 /*late_return_type=*/NULL_TREE,
10610 /*requires_clause*/NULL_TREE);
10611 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10613 fco = grokmethod (&return_type_specs,
10614 declarator,
10615 attributes);
10616 if (fco != error_mark_node)
10618 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10619 DECL_ARTIFICIAL (fco) = 1;
10620 /* Give the object parameter a different name. */
10621 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10622 if (return_type)
10623 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10625 if (template_param_list)
10627 fco = finish_member_template_decl (fco);
10628 finish_template_decl (template_param_list);
10629 --parser->num_template_parameter_lists;
10631 else if (parser->fully_implicit_function_template_p)
10632 fco = finish_fully_implicit_template (parser, fco);
10634 finish_member_declaration (fco);
10636 obstack_free (&declarator_obstack, p);
10638 return (fco != error_mark_node);
10642 /* Parse the body of a lambda expression, which is simply
10644 compound-statement
10646 but which requires special handling.
10647 LAMBDA_EXPR is the current representation of the lambda expression. */
10649 static void
10650 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10652 bool nested = (current_function_decl != NULL_TREE);
10653 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10654 bool in_function_body = parser->in_function_body;
10656 if (nested)
10657 push_function_context ();
10658 else
10659 /* Still increment function_depth so that we don't GC in the
10660 middle of an expression. */
10661 ++function_depth;
10663 vec<tree> omp_privatization_save;
10664 save_omp_privatization_clauses (omp_privatization_save);
10665 /* Clear this in case we're in the middle of a default argument. */
10666 parser->local_variables_forbidden_p = false;
10667 parser->in_function_body = true;
10670 local_specialization_stack s (lss_copy);
10671 tree fco = lambda_function (lambda_expr);
10672 tree body = start_lambda_function (fco, lambda_expr);
10673 matching_braces braces;
10675 if (braces.require_open (parser))
10677 tree compound_stmt = begin_compound_stmt (0);
10679 /* Originally C++11 required us to peek for 'return expr'; and
10680 process it specially here to deduce the return type. N3638
10681 removed the need for that. */
10683 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10684 cp_parser_label_declaration (parser);
10685 cp_parser_statement_seq_opt (parser, NULL_TREE);
10686 braces.require_close (parser);
10688 finish_compound_stmt (compound_stmt);
10691 finish_lambda_function (body);
10694 restore_omp_privatization_clauses (omp_privatization_save);
10695 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10696 parser->in_function_body = in_function_body;
10697 if (nested)
10698 pop_function_context();
10699 else
10700 --function_depth;
10703 /* Statements [gram.stmt.stmt] */
10705 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10707 static void
10708 add_debug_begin_stmt (location_t loc)
10710 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10711 return;
10712 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10713 /* A concept is never expanded normally. */
10714 return;
10716 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10717 SET_EXPR_LOCATION (stmt, loc);
10718 add_stmt (stmt);
10721 /* Parse a statement.
10723 statement:
10724 labeled-statement
10725 expression-statement
10726 compound-statement
10727 selection-statement
10728 iteration-statement
10729 jump-statement
10730 declaration-statement
10731 try-block
10733 C++11:
10735 statement:
10736 labeled-statement
10737 attribute-specifier-seq (opt) expression-statement
10738 attribute-specifier-seq (opt) compound-statement
10739 attribute-specifier-seq (opt) selection-statement
10740 attribute-specifier-seq (opt) iteration-statement
10741 attribute-specifier-seq (opt) jump-statement
10742 declaration-statement
10743 attribute-specifier-seq (opt) try-block
10745 init-statement:
10746 expression-statement
10747 simple-declaration
10749 TM Extension:
10751 statement:
10752 atomic-statement
10754 IN_COMPOUND is true when the statement is nested inside a
10755 cp_parser_compound_statement; this matters for certain pragmas.
10757 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10758 is a (possibly labeled) if statement which is not enclosed in braces
10759 and has an else clause. This is used to implement -Wparentheses.
10761 CHAIN is a vector of if-else-if conditions. */
10763 static void
10764 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10765 bool in_compound, bool *if_p, vec<tree> *chain,
10766 location_t *loc_after_labels)
10768 tree statement, std_attrs = NULL_TREE;
10769 cp_token *token;
10770 location_t statement_location, attrs_location;
10772 restart:
10773 if (if_p != NULL)
10774 *if_p = false;
10775 /* There is no statement yet. */
10776 statement = NULL_TREE;
10778 saved_token_sentinel saved_tokens (parser->lexer);
10779 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10780 if (c_dialect_objc ())
10781 /* In obj-c++, seeing '[[' might be the either the beginning of
10782 c++11 attributes, or a nested objc-message-expression. So
10783 let's parse the c++11 attributes tentatively. */
10784 cp_parser_parse_tentatively (parser);
10785 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10786 if (c_dialect_objc ())
10788 if (!cp_parser_parse_definitely (parser))
10789 std_attrs = NULL_TREE;
10792 /* Peek at the next token. */
10793 token = cp_lexer_peek_token (parser->lexer);
10794 /* Remember the location of the first token in the statement. */
10795 statement_location = token->location;
10796 add_debug_begin_stmt (statement_location);
10797 /* If this is a keyword, then that will often determine what kind of
10798 statement we have. */
10799 if (token->type == CPP_KEYWORD)
10801 enum rid keyword = token->keyword;
10803 switch (keyword)
10805 case RID_CASE:
10806 case RID_DEFAULT:
10807 /* Looks like a labeled-statement with a case label.
10808 Parse the label, and then use tail recursion to parse
10809 the statement. */
10810 cp_parser_label_for_labeled_statement (parser, std_attrs);
10811 in_compound = false;
10812 goto restart;
10814 case RID_IF:
10815 case RID_SWITCH:
10816 statement = cp_parser_selection_statement (parser, if_p, chain);
10817 break;
10819 case RID_WHILE:
10820 case RID_DO:
10821 case RID_FOR:
10822 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10823 break;
10825 case RID_BREAK:
10826 case RID_CONTINUE:
10827 case RID_RETURN:
10828 case RID_GOTO:
10829 statement = cp_parser_jump_statement (parser);
10830 break;
10832 /* Objective-C++ exception-handling constructs. */
10833 case RID_AT_TRY:
10834 case RID_AT_CATCH:
10835 case RID_AT_FINALLY:
10836 case RID_AT_SYNCHRONIZED:
10837 case RID_AT_THROW:
10838 statement = cp_parser_objc_statement (parser);
10839 break;
10841 case RID_TRY:
10842 statement = cp_parser_try_block (parser);
10843 break;
10845 case RID_NAMESPACE:
10846 /* This must be a namespace alias definition. */
10847 cp_parser_declaration_statement (parser);
10848 return;
10850 case RID_TRANSACTION_ATOMIC:
10851 case RID_TRANSACTION_RELAXED:
10852 case RID_SYNCHRONIZED:
10853 case RID_ATOMIC_NOEXCEPT:
10854 case RID_ATOMIC_CANCEL:
10855 statement = cp_parser_transaction (parser, token);
10856 break;
10857 case RID_TRANSACTION_CANCEL:
10858 statement = cp_parser_transaction_cancel (parser);
10859 break;
10861 default:
10862 /* It might be a keyword like `int' that can start a
10863 declaration-statement. */
10864 break;
10867 else if (token->type == CPP_NAME)
10869 /* If the next token is a `:', then we are looking at a
10870 labeled-statement. */
10871 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10872 if (token->type == CPP_COLON)
10874 /* Looks like a labeled-statement with an ordinary label.
10875 Parse the label, and then use tail recursion to parse
10876 the statement. */
10878 cp_parser_label_for_labeled_statement (parser, std_attrs);
10879 in_compound = false;
10880 goto restart;
10883 /* Anything that starts with a `{' must be a compound-statement. */
10884 else if (token->type == CPP_OPEN_BRACE)
10885 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10886 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10887 a statement all its own. */
10888 else if (token->type == CPP_PRAGMA)
10890 /* Only certain OpenMP pragmas are attached to statements, and thus
10891 are considered statements themselves. All others are not. In
10892 the context of a compound, accept the pragma as a "statement" and
10893 return so that we can check for a close brace. Otherwise we
10894 require a real statement and must go back and read one. */
10895 if (in_compound)
10896 cp_parser_pragma (parser, pragma_compound, if_p);
10897 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10898 goto restart;
10899 return;
10901 else if (token->type == CPP_EOF)
10903 cp_parser_error (parser, "expected statement");
10904 return;
10907 /* Everything else must be a declaration-statement or an
10908 expression-statement. Try for the declaration-statement
10909 first, unless we are looking at a `;', in which case we know that
10910 we have an expression-statement. */
10911 if (!statement)
10913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10915 if (std_attrs != NULL_TREE)
10917 /* Attributes should be parsed as part of the the
10918 declaration, so let's un-parse them. */
10919 saved_tokens.rollback();
10920 std_attrs = NULL_TREE;
10923 cp_parser_parse_tentatively (parser);
10924 /* Try to parse the declaration-statement. */
10925 cp_parser_declaration_statement (parser);
10926 /* If that worked, we're done. */
10927 if (cp_parser_parse_definitely (parser))
10928 return;
10930 /* All preceding labels have been parsed at this point. */
10931 if (loc_after_labels != NULL)
10932 *loc_after_labels = statement_location;
10934 /* Look for an expression-statement instead. */
10935 statement = cp_parser_expression_statement (parser, in_statement_expr);
10937 /* Handle [[fallthrough]];. */
10938 if (attribute_fallthrough_p (std_attrs))
10940 /* The next token after the fallthrough attribute is ';'. */
10941 if (statement == NULL_TREE)
10943 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10944 statement = build_call_expr_internal_loc (statement_location,
10945 IFN_FALLTHROUGH,
10946 void_type_node, 0);
10947 finish_expr_stmt (statement);
10949 else
10950 warning_at (statement_location, OPT_Wattributes,
10951 "%<fallthrough%> attribute not followed by %<;%>");
10952 std_attrs = NULL_TREE;
10956 /* Set the line number for the statement. */
10957 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10958 SET_EXPR_LOCATION (statement, statement_location);
10960 /* Allow "[[fallthrough]];", but warn otherwise. */
10961 if (std_attrs != NULL_TREE)
10962 warning_at (attrs_location,
10963 OPT_Wattributes,
10964 "attributes at the beginning of statement are ignored");
10967 /* Append ATTR to attribute list ATTRS. */
10969 static tree
10970 attr_chainon (tree attrs, tree attr)
10972 if (attrs == error_mark_node)
10973 return error_mark_node;
10974 if (attr == error_mark_node)
10975 return error_mark_node;
10976 return chainon (attrs, attr);
10979 /* Parse the label for a labeled-statement, i.e.
10981 identifier :
10982 case constant-expression :
10983 default :
10985 GNU Extension:
10986 case constant-expression ... constant-expression : statement
10988 When a label is parsed without errors, the label is added to the
10989 parse tree by the finish_* functions, so this function doesn't
10990 have to return the label. */
10992 static void
10993 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10995 cp_token *token;
10996 tree label = NULL_TREE;
10997 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10999 /* The next token should be an identifier. */
11000 token = cp_lexer_peek_token (parser->lexer);
11001 if (token->type != CPP_NAME
11002 && token->type != CPP_KEYWORD)
11004 cp_parser_error (parser, "expected labeled-statement");
11005 return;
11008 /* Remember whether this case or a user-defined label is allowed to fall
11009 through to. */
11010 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11012 parser->colon_corrects_to_scope_p = false;
11013 switch (token->keyword)
11015 case RID_CASE:
11017 tree expr, expr_hi;
11018 cp_token *ellipsis;
11020 /* Consume the `case' token. */
11021 cp_lexer_consume_token (parser->lexer);
11022 /* Parse the constant-expression. */
11023 expr = cp_parser_constant_expression (parser);
11024 if (check_for_bare_parameter_packs (expr))
11025 expr = error_mark_node;
11027 ellipsis = cp_lexer_peek_token (parser->lexer);
11028 if (ellipsis->type == CPP_ELLIPSIS)
11030 /* Consume the `...' token. */
11031 cp_lexer_consume_token (parser->lexer);
11032 expr_hi = cp_parser_constant_expression (parser);
11033 if (check_for_bare_parameter_packs (expr_hi))
11034 expr_hi = error_mark_node;
11036 /* We don't need to emit warnings here, as the common code
11037 will do this for us. */
11039 else
11040 expr_hi = NULL_TREE;
11042 if (parser->in_switch_statement_p)
11044 tree l = finish_case_label (token->location, expr, expr_hi);
11045 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11046 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11048 else
11049 error_at (token->location,
11050 "case label %qE not within a switch statement",
11051 expr);
11053 break;
11055 case RID_DEFAULT:
11056 /* Consume the `default' token. */
11057 cp_lexer_consume_token (parser->lexer);
11059 if (parser->in_switch_statement_p)
11061 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11062 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11063 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11065 else
11066 error_at (token->location, "case label not within a switch statement");
11067 break;
11069 default:
11070 /* Anything else must be an ordinary label. */
11071 label = finish_label_stmt (cp_parser_identifier (parser));
11072 if (label && TREE_CODE (label) == LABEL_DECL)
11073 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11074 break;
11077 /* Require the `:' token. */
11078 cp_parser_require (parser, CPP_COLON, RT_COLON);
11080 /* An ordinary label may optionally be followed by attributes.
11081 However, this is only permitted if the attributes are then
11082 followed by a semicolon. This is because, for backward
11083 compatibility, when parsing
11084 lab: __attribute__ ((unused)) int i;
11085 we want the attribute to attach to "i", not "lab". */
11086 if (label != NULL_TREE
11087 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11089 tree attrs;
11090 cp_parser_parse_tentatively (parser);
11091 attrs = cp_parser_gnu_attributes_opt (parser);
11092 if (attrs == NULL_TREE
11093 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11094 cp_parser_abort_tentative_parse (parser);
11095 else if (!cp_parser_parse_definitely (parser))
11097 else
11098 attributes = attr_chainon (attributes, attrs);
11101 if (attributes != NULL_TREE)
11102 cplus_decl_attributes (&label, attributes, 0);
11104 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11107 /* Parse an expression-statement.
11109 expression-statement:
11110 expression [opt] ;
11112 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11113 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11114 indicates whether this expression-statement is part of an
11115 expression statement. */
11117 static tree
11118 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11120 tree statement = NULL_TREE;
11121 cp_token *token = cp_lexer_peek_token (parser->lexer);
11122 location_t loc = token->location;
11124 /* There might be attribute fallthrough. */
11125 tree attr = cp_parser_gnu_attributes_opt (parser);
11127 /* If the next token is a ';', then there is no expression
11128 statement. */
11129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11131 statement = cp_parser_expression (parser);
11132 if (statement == error_mark_node
11133 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11135 cp_parser_skip_to_end_of_block_or_statement (parser);
11136 return error_mark_node;
11140 /* Handle [[fallthrough]];. */
11141 if (attribute_fallthrough_p (attr))
11143 /* The next token after the fallthrough attribute is ';'. */
11144 if (statement == NULL_TREE)
11145 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11146 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11147 void_type_node, 0);
11148 else
11149 warning_at (loc, OPT_Wattributes,
11150 "%<fallthrough%> attribute not followed by %<;%>");
11151 attr = NULL_TREE;
11154 /* Allow "[[fallthrough]];", but warn otherwise. */
11155 if (attr != NULL_TREE)
11156 warning_at (loc, OPT_Wattributes,
11157 "attributes at the beginning of statement are ignored");
11159 /* Give a helpful message for "A<T>::type t;" and the like. */
11160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11161 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11163 if (TREE_CODE (statement) == SCOPE_REF)
11164 error_at (token->location, "need %<typename%> before %qE because "
11165 "%qT is a dependent scope",
11166 statement, TREE_OPERAND (statement, 0));
11167 else if (is_overloaded_fn (statement)
11168 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11170 /* A::A a; */
11171 tree fn = get_first_fn (statement);
11172 error_at (token->location,
11173 "%<%T::%D%> names the constructor, not the type",
11174 DECL_CONTEXT (fn), DECL_NAME (fn));
11178 /* Consume the final `;'. */
11179 cp_parser_consume_semicolon_at_end_of_statement (parser);
11181 if (in_statement_expr
11182 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11183 /* This is the final expression statement of a statement
11184 expression. */
11185 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11186 else if (statement)
11187 statement = finish_expr_stmt (statement);
11189 return statement;
11192 /* Parse a compound-statement.
11194 compound-statement:
11195 { statement-seq [opt] }
11197 GNU extension:
11199 compound-statement:
11200 { label-declaration-seq [opt] statement-seq [opt] }
11202 label-declaration-seq:
11203 label-declaration
11204 label-declaration-seq label-declaration
11206 Returns a tree representing the statement. */
11208 static tree
11209 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11210 int bcs_flags, bool function_body)
11212 tree compound_stmt;
11213 matching_braces braces;
11215 /* Consume the `{'. */
11216 if (!braces.require_open (parser))
11217 return error_mark_node;
11218 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11219 && !function_body && cxx_dialect < cxx14)
11220 pedwarn (input_location, OPT_Wpedantic,
11221 "compound-statement in %<constexpr%> function");
11222 /* Begin the compound-statement. */
11223 compound_stmt = begin_compound_stmt (bcs_flags);
11224 /* If the next keyword is `__label__' we have a label declaration. */
11225 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11226 cp_parser_label_declaration (parser);
11227 /* Parse an (optional) statement-seq. */
11228 cp_parser_statement_seq_opt (parser, in_statement_expr);
11229 /* Finish the compound-statement. */
11230 finish_compound_stmt (compound_stmt);
11231 /* Consume the `}'. */
11232 braces.require_close (parser);
11234 return compound_stmt;
11237 /* Parse an (optional) statement-seq.
11239 statement-seq:
11240 statement
11241 statement-seq [opt] statement */
11243 static void
11244 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11246 /* Scan statements until there aren't any more. */
11247 while (true)
11249 cp_token *token = cp_lexer_peek_token (parser->lexer);
11251 /* If we are looking at a `}', then we have run out of
11252 statements; the same is true if we have reached the end
11253 of file, or have stumbled upon a stray '@end'. */
11254 if (token->type == CPP_CLOSE_BRACE
11255 || token->type == CPP_EOF
11256 || token->type == CPP_PRAGMA_EOL
11257 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11258 break;
11260 /* If we are in a compound statement and find 'else' then
11261 something went wrong. */
11262 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11264 if (parser->in_statement & IN_IF_STMT)
11265 break;
11266 else
11268 token = cp_lexer_consume_token (parser->lexer);
11269 error_at (token->location, "%<else%> without a previous %<if%>");
11273 /* Parse the statement. */
11274 cp_parser_statement (parser, in_statement_expr, true, NULL);
11278 /* Return true if we're looking at (init; cond), false otherwise. */
11280 static bool
11281 cp_parser_init_statement_p (cp_parser *parser)
11283 /* Save tokens so that we can put them back. */
11284 cp_lexer_save_tokens (parser->lexer);
11286 /* Look for ';' that is not nested in () or {}. */
11287 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11288 /*recovering=*/false,
11289 CPP_SEMICOLON,
11290 /*consume_paren=*/false);
11292 /* Roll back the tokens we skipped. */
11293 cp_lexer_rollback_tokens (parser->lexer);
11295 return ret == -1;
11298 /* Parse a selection-statement.
11300 selection-statement:
11301 if ( init-statement [opt] condition ) statement
11302 if ( init-statement [opt] condition ) statement else statement
11303 switch ( init-statement [opt] condition ) statement
11305 Returns the new IF_STMT or SWITCH_STMT.
11307 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11308 is a (possibly labeled) if statement which is not enclosed in
11309 braces and has an else clause. This is used to implement
11310 -Wparentheses.
11312 CHAIN is a vector of if-else-if conditions. This is used to implement
11313 -Wduplicated-cond. */
11315 static tree
11316 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11317 vec<tree> *chain)
11319 cp_token *token;
11320 enum rid keyword;
11321 token_indent_info guard_tinfo;
11323 if (if_p != NULL)
11324 *if_p = false;
11326 /* Peek at the next token. */
11327 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11328 guard_tinfo = get_token_indent_info (token);
11330 /* See what kind of keyword it is. */
11331 keyword = token->keyword;
11332 switch (keyword)
11334 case RID_IF:
11335 case RID_SWITCH:
11337 tree statement;
11338 tree condition;
11340 bool cx = false;
11341 if (keyword == RID_IF
11342 && cp_lexer_next_token_is_keyword (parser->lexer,
11343 RID_CONSTEXPR))
11345 cx = true;
11346 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11347 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11348 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11349 "with -std=c++17 or -std=gnu++17");
11352 /* Look for the `('. */
11353 matching_parens parens;
11354 if (!parens.require_open (parser))
11356 cp_parser_skip_to_end_of_statement (parser);
11357 return error_mark_node;
11360 /* Begin the selection-statement. */
11361 if (keyword == RID_IF)
11363 statement = begin_if_stmt ();
11364 IF_STMT_CONSTEXPR_P (statement) = cx;
11366 else
11367 statement = begin_switch_stmt ();
11369 /* Parse the optional init-statement. */
11370 if (cp_parser_init_statement_p (parser))
11372 tree decl;
11373 if (cxx_dialect < cxx17)
11374 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11375 "init-statement in selection statements only available "
11376 "with -std=c++17 or -std=gnu++17");
11377 cp_parser_init_statement (parser, &decl);
11380 /* Parse the condition. */
11381 condition = cp_parser_condition (parser);
11382 /* Look for the `)'. */
11383 if (!parens.require_close (parser))
11384 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11385 /*consume_paren=*/true);
11387 if (keyword == RID_IF)
11389 bool nested_if;
11390 unsigned char in_statement;
11392 /* Add the condition. */
11393 condition = finish_if_stmt_cond (condition, statement);
11395 if (warn_duplicated_cond)
11396 warn_duplicated_cond_add_or_warn (token->location, condition,
11397 &chain);
11399 /* Parse the then-clause. */
11400 in_statement = parser->in_statement;
11401 parser->in_statement |= IN_IF_STMT;
11403 /* Outside a template, the non-selected branch of a constexpr
11404 if is a 'discarded statement', i.e. unevaluated. */
11405 bool was_discarded = in_discarded_stmt;
11406 bool discard_then = (cx && !processing_template_decl
11407 && integer_zerop (condition));
11408 if (discard_then)
11410 in_discarded_stmt = true;
11411 ++c_inhibit_evaluation_warnings;
11414 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11415 guard_tinfo);
11417 parser->in_statement = in_statement;
11419 finish_then_clause (statement);
11421 if (discard_then)
11423 THEN_CLAUSE (statement) = NULL_TREE;
11424 in_discarded_stmt = was_discarded;
11425 --c_inhibit_evaluation_warnings;
11428 /* If the next token is `else', parse the else-clause. */
11429 if (cp_lexer_next_token_is_keyword (parser->lexer,
11430 RID_ELSE))
11432 bool discard_else = (cx && !processing_template_decl
11433 && integer_nonzerop (condition));
11434 if (discard_else)
11436 in_discarded_stmt = true;
11437 ++c_inhibit_evaluation_warnings;
11440 guard_tinfo
11441 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11442 /* Consume the `else' keyword. */
11443 cp_lexer_consume_token (parser->lexer);
11444 if (warn_duplicated_cond)
11446 if (cp_lexer_next_token_is_keyword (parser->lexer,
11447 RID_IF)
11448 && chain == NULL)
11450 /* We've got "if (COND) else if (COND2)". Start
11451 the condition chain and add COND as the first
11452 element. */
11453 chain = new vec<tree> ();
11454 if (!CONSTANT_CLASS_P (condition)
11455 && !TREE_SIDE_EFFECTS (condition))
11457 /* Wrap it in a NOP_EXPR so that we can set the
11458 location of the condition. */
11459 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11460 condition);
11461 SET_EXPR_LOCATION (e, token->location);
11462 chain->safe_push (e);
11465 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11466 RID_IF))
11468 /* This is if-else without subsequent if. Zap the
11469 condition chain; we would have already warned at
11470 this point. */
11471 delete chain;
11472 chain = NULL;
11475 begin_else_clause (statement);
11476 /* Parse the else-clause. */
11477 cp_parser_implicitly_scoped_statement (parser, NULL,
11478 guard_tinfo, chain);
11480 finish_else_clause (statement);
11482 /* If we are currently parsing a then-clause, then
11483 IF_P will not be NULL. We set it to true to
11484 indicate that this if statement has an else clause.
11485 This may trigger the Wparentheses warning below
11486 when we get back up to the parent if statement. */
11487 if (if_p != NULL)
11488 *if_p = true;
11490 if (discard_else)
11492 ELSE_CLAUSE (statement) = NULL_TREE;
11493 in_discarded_stmt = was_discarded;
11494 --c_inhibit_evaluation_warnings;
11497 else
11499 /* This if statement does not have an else clause. If
11500 NESTED_IF is true, then the then-clause has an if
11501 statement which does have an else clause. We warn
11502 about the potential ambiguity. */
11503 if (nested_if)
11504 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11505 "suggest explicit braces to avoid ambiguous"
11506 " %<else%>");
11507 if (warn_duplicated_cond)
11509 /* We don't need the condition chain anymore. */
11510 delete chain;
11511 chain = NULL;
11515 /* Now we're all done with the if-statement. */
11516 finish_if_stmt (statement);
11518 else
11520 bool in_switch_statement_p;
11521 unsigned char in_statement;
11523 /* Add the condition. */
11524 finish_switch_cond (condition, statement);
11526 /* Parse the body of the switch-statement. */
11527 in_switch_statement_p = parser->in_switch_statement_p;
11528 in_statement = parser->in_statement;
11529 parser->in_switch_statement_p = true;
11530 parser->in_statement |= IN_SWITCH_STMT;
11531 cp_parser_implicitly_scoped_statement (parser, if_p,
11532 guard_tinfo);
11533 parser->in_switch_statement_p = in_switch_statement_p;
11534 parser->in_statement = in_statement;
11536 /* Now we're all done with the switch-statement. */
11537 finish_switch_stmt (statement);
11540 return statement;
11542 break;
11544 default:
11545 cp_parser_error (parser, "expected selection-statement");
11546 return error_mark_node;
11550 /* Parse a condition.
11552 condition:
11553 expression
11554 type-specifier-seq declarator = initializer-clause
11555 type-specifier-seq declarator braced-init-list
11557 GNU Extension:
11559 condition:
11560 type-specifier-seq declarator asm-specification [opt]
11561 attributes [opt] = assignment-expression
11563 Returns the expression that should be tested. */
11565 static tree
11566 cp_parser_condition (cp_parser* parser)
11568 cp_decl_specifier_seq type_specifiers;
11569 const char *saved_message;
11570 int declares_class_or_enum;
11572 /* Try the declaration first. */
11573 cp_parser_parse_tentatively (parser);
11574 /* New types are not allowed in the type-specifier-seq for a
11575 condition. */
11576 saved_message = parser->type_definition_forbidden_message;
11577 parser->type_definition_forbidden_message
11578 = G_("types may not be defined in conditions");
11579 /* Parse the type-specifier-seq. */
11580 cp_parser_decl_specifier_seq (parser,
11581 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11582 &type_specifiers,
11583 &declares_class_or_enum);
11584 /* Restore the saved message. */
11585 parser->type_definition_forbidden_message = saved_message;
11586 /* If all is well, we might be looking at a declaration. */
11587 if (!cp_parser_error_occurred (parser))
11589 tree decl;
11590 tree asm_specification;
11591 tree attributes;
11592 cp_declarator *declarator;
11593 tree initializer = NULL_TREE;
11595 /* Parse the declarator. */
11596 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11597 /*ctor_dtor_or_conv_p=*/NULL,
11598 /*parenthesized_p=*/NULL,
11599 /*member_p=*/false,
11600 /*friend_p=*/false);
11601 /* Parse the attributes. */
11602 attributes = cp_parser_attributes_opt (parser);
11603 /* Parse the asm-specification. */
11604 asm_specification = cp_parser_asm_specification_opt (parser);
11605 /* If the next token is not an `=' or '{', then we might still be
11606 looking at an expression. For example:
11608 if (A(a).x)
11610 looks like a decl-specifier-seq and a declarator -- but then
11611 there is no `=', so this is an expression. */
11612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11613 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11614 cp_parser_simulate_error (parser);
11616 /* If we did see an `=' or '{', then we are looking at a declaration
11617 for sure. */
11618 if (cp_parser_parse_definitely (parser))
11620 tree pushed_scope;
11621 bool non_constant_p;
11622 int flags = LOOKUP_ONLYCONVERTING;
11624 /* Create the declaration. */
11625 decl = start_decl (declarator, &type_specifiers,
11626 /*initialized_p=*/true,
11627 attributes, /*prefix_attributes=*/NULL_TREE,
11628 &pushed_scope);
11630 /* Parse the initializer. */
11631 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11633 initializer = cp_parser_braced_list (parser, &non_constant_p);
11634 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11635 flags = 0;
11637 else
11639 /* Consume the `='. */
11640 cp_parser_require (parser, CPP_EQ, RT_EQ);
11641 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11643 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11644 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11646 /* Process the initializer. */
11647 cp_finish_decl (decl,
11648 initializer, !non_constant_p,
11649 asm_specification,
11650 flags);
11652 if (pushed_scope)
11653 pop_scope (pushed_scope);
11655 return convert_from_reference (decl);
11658 /* If we didn't even get past the declarator successfully, we are
11659 definitely not looking at a declaration. */
11660 else
11661 cp_parser_abort_tentative_parse (parser);
11663 /* Otherwise, we are looking at an expression. */
11664 return cp_parser_expression (parser);
11667 /* Parses a for-statement or range-for-statement until the closing ')',
11668 not included. */
11670 static tree
11671 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11673 tree init, scope, decl;
11674 bool is_range_for;
11676 /* Begin the for-statement. */
11677 scope = begin_for_scope (&init);
11679 /* Parse the initialization. */
11680 is_range_for = cp_parser_init_statement (parser, &decl);
11682 if (is_range_for)
11683 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11684 else
11685 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11688 static tree
11689 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11690 unsigned short unroll)
11692 /* Normal for loop */
11693 tree condition = NULL_TREE;
11694 tree expression = NULL_TREE;
11695 tree stmt;
11697 stmt = begin_for_stmt (scope, init);
11698 /* The init-statement has already been parsed in
11699 cp_parser_init_statement, so no work is needed here. */
11700 finish_init_stmt (stmt);
11702 /* If there's a condition, process it. */
11703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11704 condition = cp_parser_condition (parser);
11705 else if (ivdep)
11707 cp_parser_error (parser, "missing loop condition in loop with "
11708 "%<GCC ivdep%> pragma");
11709 condition = error_mark_node;
11711 else if (unroll)
11713 cp_parser_error (parser, "missing loop condition in loop with "
11714 "%<GCC unroll%> pragma");
11715 condition = error_mark_node;
11717 finish_for_cond (condition, stmt, ivdep, unroll);
11718 /* Look for the `;'. */
11719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11721 /* If there's an expression, process it. */
11722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11723 expression = cp_parser_expression (parser);
11724 finish_for_expr (expression, stmt);
11726 return stmt;
11729 /* Tries to parse a range-based for-statement:
11731 range-based-for:
11732 decl-specifier-seq declarator : expression
11734 The decl-specifier-seq declarator and the `:' are already parsed by
11735 cp_parser_init_statement. If processing_template_decl it returns a
11736 newly created RANGE_FOR_STMT; if not, it is converted to a
11737 regular FOR_STMT. */
11739 static tree
11740 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11741 bool ivdep, unsigned short unroll)
11743 tree stmt, range_expr;
11744 auto_vec <cxx_binding *, 16> bindings;
11745 auto_vec <tree, 16> names;
11746 tree decomp_first_name = NULL_TREE;
11747 unsigned int decomp_cnt = 0;
11749 /* Get the range declaration momentarily out of the way so that
11750 the range expression doesn't clash with it. */
11751 if (range_decl != error_mark_node)
11753 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11755 tree v = DECL_VALUE_EXPR (range_decl);
11756 /* For decomposition declaration get all of the corresponding
11757 declarations out of the way. */
11758 if (TREE_CODE (v) == ARRAY_REF
11759 && VAR_P (TREE_OPERAND (v, 0))
11760 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11762 tree d = range_decl;
11763 range_decl = TREE_OPERAND (v, 0);
11764 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11765 decomp_first_name = d;
11766 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11768 tree name = DECL_NAME (d);
11769 names.safe_push (name);
11770 bindings.safe_push (IDENTIFIER_BINDING (name));
11771 IDENTIFIER_BINDING (name)
11772 = IDENTIFIER_BINDING (name)->previous;
11776 if (names.is_empty ())
11778 tree name = DECL_NAME (range_decl);
11779 names.safe_push (name);
11780 bindings.safe_push (IDENTIFIER_BINDING (name));
11781 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11785 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11787 bool expr_non_constant_p;
11788 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11790 else
11791 range_expr = cp_parser_expression (parser);
11793 /* Put the range declaration(s) back into scope. */
11794 for (unsigned int i = 0; i < names.length (); i++)
11796 cxx_binding *binding = bindings[i];
11797 binding->previous = IDENTIFIER_BINDING (names[i]);
11798 IDENTIFIER_BINDING (names[i]) = binding;
11801 /* If in template, STMT is converted to a normal for-statement
11802 at instantiation. If not, it is done just ahead. */
11803 if (processing_template_decl)
11805 if (check_for_bare_parameter_packs (range_expr))
11806 range_expr = error_mark_node;
11807 stmt = begin_range_for_stmt (scope, init);
11808 if (ivdep)
11809 RANGE_FOR_IVDEP (stmt) = 1;
11810 if (unroll)
11811 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11812 finish_range_for_decl (stmt, range_decl, range_expr);
11813 if (!type_dependent_expression_p (range_expr)
11814 /* do_auto_deduction doesn't mess with template init-lists. */
11815 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11816 do_range_for_auto_deduction (range_decl, range_expr);
11818 else
11820 stmt = begin_for_stmt (scope, init);
11821 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11822 decomp_first_name, decomp_cnt, ivdep,
11823 unroll);
11825 return stmt;
11828 /* Subroutine of cp_convert_range_for: given the initializer expression,
11829 builds up the range temporary. */
11831 static tree
11832 build_range_temp (tree range_expr)
11834 tree range_type, range_temp;
11836 /* Find out the type deduced by the declaration
11837 `auto &&__range = range_expr'. */
11838 range_type = cp_build_reference_type (make_auto (), true);
11839 range_type = do_auto_deduction (range_type, range_expr,
11840 type_uses_auto (range_type));
11842 /* Create the __range variable. */
11843 range_temp = build_decl (input_location, VAR_DECL,
11844 get_identifier ("__for_range"), range_type);
11845 TREE_USED (range_temp) = 1;
11846 DECL_ARTIFICIAL (range_temp) = 1;
11848 return range_temp;
11851 /* Used by cp_parser_range_for in template context: we aren't going to
11852 do a full conversion yet, but we still need to resolve auto in the
11853 type of the for-range-declaration if present. This is basically
11854 a shortcut version of cp_convert_range_for. */
11856 static void
11857 do_range_for_auto_deduction (tree decl, tree range_expr)
11859 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11860 if (auto_node)
11862 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11863 range_temp = convert_from_reference (build_range_temp (range_expr));
11864 iter_type = (cp_parser_perform_range_for_lookup
11865 (range_temp, &begin_dummy, &end_dummy));
11866 if (iter_type)
11868 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11869 iter_type);
11870 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11871 RO_UNARY_STAR,
11872 tf_warning_or_error);
11873 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11874 iter_decl, auto_node);
11879 /* Converts a range-based for-statement into a normal
11880 for-statement, as per the definition.
11882 for (RANGE_DECL : RANGE_EXPR)
11883 BLOCK
11885 should be equivalent to:
11888 auto &&__range = RANGE_EXPR;
11889 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11890 __begin != __end;
11891 ++__begin)
11893 RANGE_DECL = *__begin;
11894 BLOCK
11898 If RANGE_EXPR is an array:
11899 BEGIN_EXPR = __range
11900 END_EXPR = __range + ARRAY_SIZE(__range)
11901 Else if RANGE_EXPR has a member 'begin' or 'end':
11902 BEGIN_EXPR = __range.begin()
11903 END_EXPR = __range.end()
11904 Else:
11905 BEGIN_EXPR = begin(__range)
11906 END_EXPR = end(__range);
11908 If __range has a member 'begin' but not 'end', or vice versa, we must
11909 still use the second alternative (it will surely fail, however).
11910 When calling begin()/end() in the third alternative we must use
11911 argument dependent lookup, but always considering 'std' as an associated
11912 namespace. */
11914 tree
11915 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11916 tree decomp_first_name, unsigned int decomp_cnt,
11917 bool ivdep, unsigned short unroll)
11919 tree begin, end;
11920 tree iter_type, begin_expr, end_expr;
11921 tree condition, expression;
11923 range_expr = mark_lvalue_use (range_expr);
11925 if (range_decl == error_mark_node || range_expr == error_mark_node)
11926 /* If an error happened previously do nothing or else a lot of
11927 unhelpful errors would be issued. */
11928 begin_expr = end_expr = iter_type = error_mark_node;
11929 else
11931 tree range_temp;
11933 if (VAR_P (range_expr)
11934 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11935 /* Can't bind a reference to an array of runtime bound. */
11936 range_temp = range_expr;
11937 else
11939 range_temp = build_range_temp (range_expr);
11940 pushdecl (range_temp);
11941 cp_finish_decl (range_temp, range_expr,
11942 /*is_constant_init*/false, NULL_TREE,
11943 LOOKUP_ONLYCONVERTING);
11944 range_temp = convert_from_reference (range_temp);
11946 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11947 &begin_expr, &end_expr);
11950 /* The new for initialization statement. */
11951 begin = build_decl (input_location, VAR_DECL,
11952 get_identifier ("__for_begin"), iter_type);
11953 TREE_USED (begin) = 1;
11954 DECL_ARTIFICIAL (begin) = 1;
11955 pushdecl (begin);
11956 cp_finish_decl (begin, begin_expr,
11957 /*is_constant_init*/false, NULL_TREE,
11958 LOOKUP_ONLYCONVERTING);
11960 if (cxx_dialect >= cxx17)
11961 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11962 end = build_decl (input_location, VAR_DECL,
11963 get_identifier ("__for_end"), iter_type);
11964 TREE_USED (end) = 1;
11965 DECL_ARTIFICIAL (end) = 1;
11966 pushdecl (end);
11967 cp_finish_decl (end, end_expr,
11968 /*is_constant_init*/false, NULL_TREE,
11969 LOOKUP_ONLYCONVERTING);
11971 finish_init_stmt (statement);
11973 /* The new for condition. */
11974 condition = build_x_binary_op (input_location, NE_EXPR,
11975 begin, ERROR_MARK,
11976 end, ERROR_MARK,
11977 NULL, tf_warning_or_error);
11978 finish_for_cond (condition, statement, ivdep, unroll);
11980 /* The new increment expression. */
11981 expression = finish_unary_op_expr (input_location,
11982 PREINCREMENT_EXPR, begin,
11983 tf_warning_or_error);
11984 finish_for_expr (expression, statement);
11986 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11987 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11989 /* The declaration is initialized with *__begin inside the loop body. */
11990 cp_finish_decl (range_decl,
11991 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11992 tf_warning_or_error),
11993 /*is_constant_init*/false, NULL_TREE,
11994 LOOKUP_ONLYCONVERTING);
11995 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11996 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11998 return statement;
12001 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12002 We need to solve both at the same time because the method used
12003 depends on the existence of members begin or end.
12004 Returns the type deduced for the iterator expression. */
12006 static tree
12007 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12009 if (error_operand_p (range))
12011 *begin = *end = error_mark_node;
12012 return error_mark_node;
12015 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12017 error ("range-based %<for%> expression of type %qT "
12018 "has incomplete type", TREE_TYPE (range));
12019 *begin = *end = error_mark_node;
12020 return error_mark_node;
12022 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12024 /* If RANGE is an array, we will use pointer arithmetic. */
12025 *begin = decay_conversion (range, tf_warning_or_error);
12026 *end = build_binary_op (input_location, PLUS_EXPR,
12027 range,
12028 array_type_nelts_top (TREE_TYPE (range)),
12029 false);
12030 return TREE_TYPE (*begin);
12032 else
12034 /* If it is not an array, we must do a bit of magic. */
12035 tree id_begin, id_end;
12036 tree member_begin, member_end;
12038 *begin = *end = error_mark_node;
12040 id_begin = get_identifier ("begin");
12041 id_end = get_identifier ("end");
12042 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12043 /*protect=*/2, /*want_type=*/false,
12044 tf_warning_or_error);
12045 member_end = lookup_member (TREE_TYPE (range), id_end,
12046 /*protect=*/2, /*want_type=*/false,
12047 tf_warning_or_error);
12049 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12051 /* Use the member functions. */
12052 if (member_begin != NULL_TREE)
12053 *begin = cp_parser_range_for_member_function (range, id_begin);
12054 else
12055 error ("range-based %<for%> expression of type %qT has an "
12056 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12058 if (member_end != NULL_TREE)
12059 *end = cp_parser_range_for_member_function (range, id_end);
12060 else
12061 error ("range-based %<for%> expression of type %qT has a "
12062 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12064 else
12066 /* Use global functions with ADL. */
12067 vec<tree, va_gc> *vec;
12068 vec = make_tree_vector ();
12070 vec_safe_push (vec, range);
12072 member_begin = perform_koenig_lookup (id_begin, vec,
12073 tf_warning_or_error);
12074 *begin = finish_call_expr (member_begin, &vec, false, true,
12075 tf_warning_or_error);
12076 member_end = perform_koenig_lookup (id_end, vec,
12077 tf_warning_or_error);
12078 *end = finish_call_expr (member_end, &vec, false, true,
12079 tf_warning_or_error);
12081 release_tree_vector (vec);
12084 /* Last common checks. */
12085 if (*begin == error_mark_node || *end == error_mark_node)
12087 /* If one of the expressions is an error do no more checks. */
12088 *begin = *end = error_mark_node;
12089 return error_mark_node;
12091 else if (type_dependent_expression_p (*begin)
12092 || type_dependent_expression_p (*end))
12093 /* Can happen, when, eg, in a template context, Koenig lookup
12094 can't resolve begin/end (c++/58503). */
12095 return NULL_TREE;
12096 else
12098 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12099 /* The unqualified type of the __begin and __end temporaries should
12100 be the same, as required by the multiple auto declaration. */
12101 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12103 if (cxx_dialect >= cxx17
12104 && (build_x_binary_op (input_location, NE_EXPR,
12105 *begin, ERROR_MARK,
12106 *end, ERROR_MARK,
12107 NULL, tf_none)
12108 != error_mark_node))
12109 /* P0184R0 allows __begin and __end to have different types,
12110 but make sure they are comparable so we can give a better
12111 diagnostic. */;
12112 else
12113 error ("inconsistent begin/end types in range-based %<for%> "
12114 "statement: %qT and %qT",
12115 TREE_TYPE (*begin), TREE_TYPE (*end));
12117 return iter_type;
12122 /* Helper function for cp_parser_perform_range_for_lookup.
12123 Builds a tree for RANGE.IDENTIFIER(). */
12125 static tree
12126 cp_parser_range_for_member_function (tree range, tree identifier)
12128 tree member, res;
12129 vec<tree, va_gc> *vec;
12131 member = finish_class_member_access_expr (range, identifier,
12132 false, tf_warning_or_error);
12133 if (member == error_mark_node)
12134 return error_mark_node;
12136 vec = make_tree_vector ();
12137 res = finish_call_expr (member, &vec,
12138 /*disallow_virtual=*/false,
12139 /*koenig_p=*/false,
12140 tf_warning_or_error);
12141 release_tree_vector (vec);
12142 return res;
12145 /* Parse an iteration-statement.
12147 iteration-statement:
12148 while ( condition ) statement
12149 do statement while ( expression ) ;
12150 for ( init-statement condition [opt] ; expression [opt] )
12151 statement
12153 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12155 static tree
12156 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12157 unsigned short unroll)
12159 cp_token *token;
12160 enum rid keyword;
12161 tree statement;
12162 unsigned char in_statement;
12163 token_indent_info guard_tinfo;
12165 /* Peek at the next token. */
12166 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12167 if (!token)
12168 return error_mark_node;
12170 guard_tinfo = get_token_indent_info (token);
12172 /* Remember whether or not we are already within an iteration
12173 statement. */
12174 in_statement = parser->in_statement;
12176 /* See what kind of keyword it is. */
12177 keyword = token->keyword;
12178 switch (keyword)
12180 case RID_WHILE:
12182 tree condition;
12184 /* Begin the while-statement. */
12185 statement = begin_while_stmt ();
12186 /* Look for the `('. */
12187 matching_parens parens;
12188 parens.require_open (parser);
12189 /* Parse the condition. */
12190 condition = cp_parser_condition (parser);
12191 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12192 /* Look for the `)'. */
12193 parens.require_close (parser);
12194 /* Parse the dependent statement. */
12195 parser->in_statement = IN_ITERATION_STMT;
12196 bool prev = note_iteration_stmt_body_start ();
12197 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12198 note_iteration_stmt_body_end (prev);
12199 parser->in_statement = in_statement;
12200 /* We're done with the while-statement. */
12201 finish_while_stmt (statement);
12203 break;
12205 case RID_DO:
12207 tree expression;
12209 /* Begin the do-statement. */
12210 statement = begin_do_stmt ();
12211 /* Parse the body of the do-statement. */
12212 parser->in_statement = IN_ITERATION_STMT;
12213 bool prev = note_iteration_stmt_body_start ();
12214 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12215 note_iteration_stmt_body_end (prev);
12216 parser->in_statement = in_statement;
12217 finish_do_body (statement);
12218 /* Look for the `while' keyword. */
12219 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12220 /* Look for the `('. */
12221 matching_parens parens;
12222 parens.require_open (parser);
12223 /* Parse the expression. */
12224 expression = cp_parser_expression (parser);
12225 /* We're done with the do-statement. */
12226 finish_do_stmt (expression, statement, ivdep, unroll);
12227 /* Look for the `)'. */
12228 parens.require_close (parser);
12229 /* Look for the `;'. */
12230 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12232 break;
12234 case RID_FOR:
12236 /* Look for the `('. */
12237 matching_parens parens;
12238 parens.require_open (parser);
12240 statement = cp_parser_for (parser, ivdep, unroll);
12242 /* Look for the `)'. */
12243 parens.require_close (parser);
12245 /* Parse the body of the for-statement. */
12246 parser->in_statement = IN_ITERATION_STMT;
12247 bool prev = note_iteration_stmt_body_start ();
12248 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12249 note_iteration_stmt_body_end (prev);
12250 parser->in_statement = in_statement;
12252 /* We're done with the for-statement. */
12253 finish_for_stmt (statement);
12255 break;
12257 default:
12258 cp_parser_error (parser, "expected iteration-statement");
12259 statement = error_mark_node;
12260 break;
12263 return statement;
12266 /* Parse a init-statement or the declarator of a range-based-for.
12267 Returns true if a range-based-for declaration is seen.
12269 init-statement:
12270 expression-statement
12271 simple-declaration */
12273 static bool
12274 cp_parser_init_statement (cp_parser* parser, tree *decl)
12276 /* If the next token is a `;', then we have an empty
12277 expression-statement. Grammatically, this is also a
12278 simple-declaration, but an invalid one, because it does not
12279 declare anything. Therefore, if we did not handle this case
12280 specially, we would issue an error message about an invalid
12281 declaration. */
12282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12284 bool is_range_for = false;
12285 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12287 /* A colon is used in range-based for. */
12288 parser->colon_corrects_to_scope_p = false;
12290 /* We're going to speculatively look for a declaration, falling back
12291 to an expression, if necessary. */
12292 cp_parser_parse_tentatively (parser);
12293 /* Parse the declaration. */
12294 cp_parser_simple_declaration (parser,
12295 /*function_definition_allowed_p=*/false,
12296 decl);
12297 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12298 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12300 /* It is a range-for, consume the ':' */
12301 cp_lexer_consume_token (parser->lexer);
12302 is_range_for = true;
12303 if (cxx_dialect < cxx11)
12305 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12306 "range-based %<for%> loops only available with "
12307 "-std=c++11 or -std=gnu++11");
12308 *decl = error_mark_node;
12311 else
12312 /* The ';' is not consumed yet because we told
12313 cp_parser_simple_declaration not to. */
12314 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12316 if (cp_parser_parse_definitely (parser))
12317 return is_range_for;
12318 /* If the tentative parse failed, then we shall need to look for an
12319 expression-statement. */
12321 /* If we are here, it is an expression-statement. */
12322 cp_parser_expression_statement (parser, NULL_TREE);
12323 return false;
12326 /* Parse a jump-statement.
12328 jump-statement:
12329 break ;
12330 continue ;
12331 return expression [opt] ;
12332 return braced-init-list ;
12333 goto identifier ;
12335 GNU extension:
12337 jump-statement:
12338 goto * expression ;
12340 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12342 static tree
12343 cp_parser_jump_statement (cp_parser* parser)
12345 tree statement = error_mark_node;
12346 cp_token *token;
12347 enum rid keyword;
12348 unsigned char in_statement;
12350 /* Peek at the next token. */
12351 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12352 if (!token)
12353 return error_mark_node;
12355 /* See what kind of keyword it is. */
12356 keyword = token->keyword;
12357 switch (keyword)
12359 case RID_BREAK:
12360 in_statement = parser->in_statement & ~IN_IF_STMT;
12361 switch (in_statement)
12363 case 0:
12364 error_at (token->location, "break statement not within loop or switch");
12365 break;
12366 default:
12367 gcc_assert ((in_statement & IN_SWITCH_STMT)
12368 || in_statement == IN_ITERATION_STMT);
12369 statement = finish_break_stmt ();
12370 if (in_statement == IN_ITERATION_STMT)
12371 break_maybe_infinite_loop ();
12372 break;
12373 case IN_OMP_BLOCK:
12374 error_at (token->location, "invalid exit from OpenMP structured block");
12375 break;
12376 case IN_OMP_FOR:
12377 error_at (token->location, "break statement used with OpenMP for loop");
12378 break;
12380 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12381 break;
12383 case RID_CONTINUE:
12384 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12386 case 0:
12387 error_at (token->location, "continue statement not within a loop");
12388 break;
12389 /* Fall through. */
12390 case IN_ITERATION_STMT:
12391 case IN_OMP_FOR:
12392 statement = finish_continue_stmt ();
12393 break;
12394 case IN_OMP_BLOCK:
12395 error_at (token->location, "invalid exit from OpenMP structured block");
12396 break;
12397 default:
12398 gcc_unreachable ();
12400 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12401 break;
12403 case RID_RETURN:
12405 tree expr;
12406 bool expr_non_constant_p;
12408 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12410 cp_lexer_set_source_position (parser->lexer);
12411 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12412 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12414 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12415 expr = cp_parser_expression (parser);
12416 else
12417 /* If the next token is a `;', then there is no
12418 expression. */
12419 expr = NULL_TREE;
12420 /* Build the return-statement. */
12421 if (current_function_auto_return_pattern && in_discarded_stmt)
12422 /* Don't deduce from a discarded return statement. */;
12423 else
12424 statement = finish_return_stmt (expr);
12425 /* Look for the final `;'. */
12426 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12428 break;
12430 case RID_GOTO:
12431 if (parser->in_function_body
12432 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12434 error ("%<goto%> in %<constexpr%> function");
12435 cp_function_chain->invalid_constexpr = true;
12438 /* Create the goto-statement. */
12439 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12441 /* Issue a warning about this use of a GNU extension. */
12442 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12443 /* Consume the '*' token. */
12444 cp_lexer_consume_token (parser->lexer);
12445 /* Parse the dependent expression. */
12446 finish_goto_stmt (cp_parser_expression (parser));
12448 else
12449 finish_goto_stmt (cp_parser_identifier (parser));
12450 /* Look for the final `;'. */
12451 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12452 break;
12454 default:
12455 cp_parser_error (parser, "expected jump-statement");
12456 break;
12459 return statement;
12462 /* Parse a declaration-statement.
12464 declaration-statement:
12465 block-declaration */
12467 static void
12468 cp_parser_declaration_statement (cp_parser* parser)
12470 void *p;
12472 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12473 p = obstack_alloc (&declarator_obstack, 0);
12475 /* Parse the block-declaration. */
12476 cp_parser_block_declaration (parser, /*statement_p=*/true);
12478 /* Free any declarators allocated. */
12479 obstack_free (&declarator_obstack, p);
12482 /* Some dependent statements (like `if (cond) statement'), are
12483 implicitly in their own scope. In other words, if the statement is
12484 a single statement (as opposed to a compound-statement), it is
12485 none-the-less treated as if it were enclosed in braces. Any
12486 declarations appearing in the dependent statement are out of scope
12487 after control passes that point. This function parses a statement,
12488 but ensures that is in its own scope, even if it is not a
12489 compound-statement.
12491 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12492 is a (possibly labeled) if statement which is not enclosed in
12493 braces and has an else clause. This is used to implement
12494 -Wparentheses.
12496 CHAIN is a vector of if-else-if conditions. This is used to implement
12497 -Wduplicated-cond.
12499 Returns the new statement. */
12501 static tree
12502 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12503 const token_indent_info &guard_tinfo,
12504 vec<tree> *chain)
12506 tree statement;
12507 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12508 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12509 token_indent_info body_tinfo
12510 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12512 if (if_p != NULL)
12513 *if_p = false;
12515 /* Mark if () ; with a special NOP_EXPR. */
12516 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12518 cp_lexer_consume_token (parser->lexer);
12519 statement = add_stmt (build_empty_stmt (body_loc));
12521 if (guard_tinfo.keyword == RID_IF
12522 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12523 warning_at (body_loc, OPT_Wempty_body,
12524 "suggest braces around empty body in an %<if%> statement");
12525 else if (guard_tinfo.keyword == RID_ELSE)
12526 warning_at (body_loc, OPT_Wempty_body,
12527 "suggest braces around empty body in an %<else%> statement");
12529 /* if a compound is opened, we simply parse the statement directly. */
12530 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12531 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12532 /* If the token is not a `{', then we must take special action. */
12533 else
12535 /* Create a compound-statement. */
12536 statement = begin_compound_stmt (0);
12537 /* Parse the dependent-statement. */
12538 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12539 &body_loc_after_labels);
12540 /* Finish the dummy compound-statement. */
12541 finish_compound_stmt (statement);
12544 token_indent_info next_tinfo
12545 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12546 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12548 if (body_loc_after_labels != UNKNOWN_LOCATION
12549 && next_tinfo.type != CPP_SEMICOLON)
12550 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12551 guard_tinfo.location, guard_tinfo.keyword);
12553 /* Return the statement. */
12554 return statement;
12557 /* For some dependent statements (like `while (cond) statement'), we
12558 have already created a scope. Therefore, even if the dependent
12559 statement is a compound-statement, we do not want to create another
12560 scope. */
12562 static void
12563 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12564 const token_indent_info &guard_tinfo)
12566 /* If the token is a `{', then we must take special action. */
12567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12569 token_indent_info body_tinfo
12570 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12571 location_t loc_after_labels = UNKNOWN_LOCATION;
12573 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12574 &loc_after_labels);
12575 token_indent_info next_tinfo
12576 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12577 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12579 if (loc_after_labels != UNKNOWN_LOCATION
12580 && next_tinfo.type != CPP_SEMICOLON)
12581 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12582 guard_tinfo.location,
12583 guard_tinfo.keyword);
12585 else
12587 /* Avoid calling cp_parser_compound_statement, so that we
12588 don't create a new scope. Do everything else by hand. */
12589 matching_braces braces;
12590 braces.require_open (parser);
12591 /* If the next keyword is `__label__' we have a label declaration. */
12592 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12593 cp_parser_label_declaration (parser);
12594 /* Parse an (optional) statement-seq. */
12595 cp_parser_statement_seq_opt (parser, NULL_TREE);
12596 braces.require_close (parser);
12600 /* Declarations [gram.dcl.dcl] */
12602 /* Parse an optional declaration-sequence.
12604 declaration-seq:
12605 declaration
12606 declaration-seq declaration */
12608 static void
12609 cp_parser_declaration_seq_opt (cp_parser* parser)
12611 while (true)
12613 cp_token *token;
12615 token = cp_lexer_peek_token (parser->lexer);
12617 if (token->type == CPP_CLOSE_BRACE
12618 || token->type == CPP_EOF
12619 || token->type == CPP_PRAGMA_EOL)
12620 break;
12622 if (token->type == CPP_SEMICOLON)
12624 /* A declaration consisting of a single semicolon is
12625 invalid. Allow it unless we're being pedantic. */
12626 cp_lexer_consume_token (parser->lexer);
12627 if (!in_system_header_at (input_location))
12628 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12629 continue;
12632 /* If we're entering or exiting a region that's implicitly
12633 extern "C", modify the lang context appropriately. */
12634 if (!parser->implicit_extern_c && token->implicit_extern_c)
12636 push_lang_context (lang_name_c);
12637 parser->implicit_extern_c = true;
12639 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12641 pop_lang_context ();
12642 parser->implicit_extern_c = false;
12645 if (token->type == CPP_PRAGMA)
12647 /* A top-level declaration can consist solely of a #pragma.
12648 A nested declaration cannot, so this is done here and not
12649 in cp_parser_declaration. (A #pragma at block scope is
12650 handled in cp_parser_statement.) */
12651 cp_parser_pragma (parser, pragma_external, NULL);
12652 continue;
12655 /* Parse the declaration itself. */
12656 cp_parser_declaration (parser);
12660 /* Parse a declaration.
12662 declaration:
12663 block-declaration
12664 function-definition
12665 template-declaration
12666 explicit-instantiation
12667 explicit-specialization
12668 linkage-specification
12669 namespace-definition
12671 C++17:
12672 deduction-guide
12674 GNU extension:
12676 declaration:
12677 __extension__ declaration */
12679 static void
12680 cp_parser_declaration (cp_parser* parser)
12682 cp_token token1;
12683 cp_token token2;
12684 int saved_pedantic;
12685 void *p;
12686 tree attributes = NULL_TREE;
12688 /* Check for the `__extension__' keyword. */
12689 if (cp_parser_extension_opt (parser, &saved_pedantic))
12691 /* Parse the qualified declaration. */
12692 cp_parser_declaration (parser);
12693 /* Restore the PEDANTIC flag. */
12694 pedantic = saved_pedantic;
12696 return;
12699 /* Try to figure out what kind of declaration is present. */
12700 token1 = *cp_lexer_peek_token (parser->lexer);
12702 if (token1.type != CPP_EOF)
12703 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12704 else
12706 token2.type = CPP_EOF;
12707 token2.keyword = RID_MAX;
12710 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12711 p = obstack_alloc (&declarator_obstack, 0);
12713 /* If the next token is `extern' and the following token is a string
12714 literal, then we have a linkage specification. */
12715 if (token1.keyword == RID_EXTERN
12716 && cp_parser_is_pure_string_literal (&token2))
12717 cp_parser_linkage_specification (parser);
12718 /* If the next token is `template', then we have either a template
12719 declaration, an explicit instantiation, or an explicit
12720 specialization. */
12721 else if (token1.keyword == RID_TEMPLATE)
12723 /* `template <>' indicates a template specialization. */
12724 if (token2.type == CPP_LESS
12725 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12726 cp_parser_explicit_specialization (parser);
12727 /* `template <' indicates a template declaration. */
12728 else if (token2.type == CPP_LESS)
12729 cp_parser_template_declaration (parser, /*member_p=*/false);
12730 /* Anything else must be an explicit instantiation. */
12731 else
12732 cp_parser_explicit_instantiation (parser);
12734 /* If the next token is `export', then we have a template
12735 declaration. */
12736 else if (token1.keyword == RID_EXPORT)
12737 cp_parser_template_declaration (parser, /*member_p=*/false);
12738 /* If the next token is `extern', 'static' or 'inline' and the one
12739 after that is `template', we have a GNU extended explicit
12740 instantiation directive. */
12741 else if (cp_parser_allow_gnu_extensions_p (parser)
12742 && (token1.keyword == RID_EXTERN
12743 || token1.keyword == RID_STATIC
12744 || token1.keyword == RID_INLINE)
12745 && token2.keyword == RID_TEMPLATE)
12746 cp_parser_explicit_instantiation (parser);
12747 /* If the next token is `namespace', check for a named or unnamed
12748 namespace definition. */
12749 else if (token1.keyword == RID_NAMESPACE
12750 && (/* A named namespace definition. */
12751 (token2.type == CPP_NAME
12752 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12753 != CPP_EQ))
12754 || (token2.type == CPP_OPEN_SQUARE
12755 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12756 == CPP_OPEN_SQUARE)
12757 /* An unnamed namespace definition. */
12758 || token2.type == CPP_OPEN_BRACE
12759 || token2.keyword == RID_ATTRIBUTE))
12760 cp_parser_namespace_definition (parser);
12761 /* An inline (associated) namespace definition. */
12762 else if (token1.keyword == RID_INLINE
12763 && token2.keyword == RID_NAMESPACE)
12764 cp_parser_namespace_definition (parser);
12765 /* Objective-C++ declaration/definition. */
12766 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12767 cp_parser_objc_declaration (parser, NULL_TREE);
12768 else if (c_dialect_objc ()
12769 && token1.keyword == RID_ATTRIBUTE
12770 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12771 cp_parser_objc_declaration (parser, attributes);
12772 /* At this point we may have a template declared by a concept
12773 introduction. */
12774 else if (flag_concepts
12775 && cp_parser_template_declaration_after_export (parser,
12776 /*member_p=*/false))
12777 /* We did. */;
12778 else
12779 /* Try to parse a block-declaration, or a function-definition. */
12780 cp_parser_block_declaration (parser, /*statement_p=*/false);
12782 /* Free any declarators allocated. */
12783 obstack_free (&declarator_obstack, p);
12786 /* Parse a block-declaration.
12788 block-declaration:
12789 simple-declaration
12790 asm-definition
12791 namespace-alias-definition
12792 using-declaration
12793 using-directive
12795 GNU Extension:
12797 block-declaration:
12798 __extension__ block-declaration
12800 C++0x Extension:
12802 block-declaration:
12803 static_assert-declaration
12805 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12806 part of a declaration-statement. */
12808 static void
12809 cp_parser_block_declaration (cp_parser *parser,
12810 bool statement_p)
12812 cp_token *token1;
12813 int saved_pedantic;
12815 /* Check for the `__extension__' keyword. */
12816 if (cp_parser_extension_opt (parser, &saved_pedantic))
12818 /* Parse the qualified declaration. */
12819 cp_parser_block_declaration (parser, statement_p);
12820 /* Restore the PEDANTIC flag. */
12821 pedantic = saved_pedantic;
12823 return;
12826 /* Peek at the next token to figure out which kind of declaration is
12827 present. */
12828 token1 = cp_lexer_peek_token (parser->lexer);
12830 /* If the next keyword is `asm', we have an asm-definition. */
12831 if (token1->keyword == RID_ASM)
12833 if (statement_p)
12834 cp_parser_commit_to_tentative_parse (parser);
12835 cp_parser_asm_definition (parser);
12837 /* If the next keyword is `namespace', we have a
12838 namespace-alias-definition. */
12839 else if (token1->keyword == RID_NAMESPACE)
12840 cp_parser_namespace_alias_definition (parser);
12841 /* If the next keyword is `using', we have a
12842 using-declaration, a using-directive, or an alias-declaration. */
12843 else if (token1->keyword == RID_USING)
12845 cp_token *token2;
12847 if (statement_p)
12848 cp_parser_commit_to_tentative_parse (parser);
12849 /* If the token after `using' is `namespace', then we have a
12850 using-directive. */
12851 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12852 if (token2->keyword == RID_NAMESPACE)
12853 cp_parser_using_directive (parser);
12854 /* If the second token after 'using' is '=', then we have an
12855 alias-declaration. */
12856 else if (cxx_dialect >= cxx11
12857 && token2->type == CPP_NAME
12858 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12859 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12860 cp_parser_alias_declaration (parser);
12861 /* Otherwise, it's a using-declaration. */
12862 else
12863 cp_parser_using_declaration (parser,
12864 /*access_declaration_p=*/false);
12866 /* If the next keyword is `__label__' we have a misplaced label
12867 declaration. */
12868 else if (token1->keyword == RID_LABEL)
12870 cp_lexer_consume_token (parser->lexer);
12871 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12872 cp_parser_skip_to_end_of_statement (parser);
12873 /* If the next token is now a `;', consume it. */
12874 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12875 cp_lexer_consume_token (parser->lexer);
12877 /* If the next token is `static_assert' we have a static assertion. */
12878 else if (token1->keyword == RID_STATIC_ASSERT)
12879 cp_parser_static_assert (parser, /*member_p=*/false);
12880 /* Anything else must be a simple-declaration. */
12881 else
12882 cp_parser_simple_declaration (parser, !statement_p,
12883 /*maybe_range_for_decl*/NULL);
12886 /* Parse a simple-declaration.
12888 simple-declaration:
12889 decl-specifier-seq [opt] init-declarator-list [opt] ;
12890 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12891 brace-or-equal-initializer ;
12893 init-declarator-list:
12894 init-declarator
12895 init-declarator-list , init-declarator
12897 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12898 function-definition as a simple-declaration.
12900 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12901 parsed declaration if it is an uninitialized single declarator not followed
12902 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12903 if present, will not be consumed. */
12905 static void
12906 cp_parser_simple_declaration (cp_parser* parser,
12907 bool function_definition_allowed_p,
12908 tree *maybe_range_for_decl)
12910 cp_decl_specifier_seq decl_specifiers;
12911 int declares_class_or_enum;
12912 bool saw_declarator;
12913 location_t comma_loc = UNKNOWN_LOCATION;
12914 location_t init_loc = UNKNOWN_LOCATION;
12916 if (maybe_range_for_decl)
12917 *maybe_range_for_decl = NULL_TREE;
12919 /* Defer access checks until we know what is being declared; the
12920 checks for names appearing in the decl-specifier-seq should be
12921 done as if we were in the scope of the thing being declared. */
12922 push_deferring_access_checks (dk_deferred);
12924 /* Parse the decl-specifier-seq. We have to keep track of whether
12925 or not the decl-specifier-seq declares a named class or
12926 enumeration type, since that is the only case in which the
12927 init-declarator-list is allowed to be empty.
12929 [dcl.dcl]
12931 In a simple-declaration, the optional init-declarator-list can be
12932 omitted only when declaring a class or enumeration, that is when
12933 the decl-specifier-seq contains either a class-specifier, an
12934 elaborated-type-specifier, or an enum-specifier. */
12935 cp_parser_decl_specifier_seq (parser,
12936 CP_PARSER_FLAGS_OPTIONAL,
12937 &decl_specifiers,
12938 &declares_class_or_enum);
12939 /* We no longer need to defer access checks. */
12940 stop_deferring_access_checks ();
12942 /* In a block scope, a valid declaration must always have a
12943 decl-specifier-seq. By not trying to parse declarators, we can
12944 resolve the declaration/expression ambiguity more quickly. */
12945 if (!function_definition_allowed_p
12946 && !decl_specifiers.any_specifiers_p)
12948 cp_parser_error (parser, "expected declaration");
12949 goto done;
12952 /* If the next two tokens are both identifiers, the code is
12953 erroneous. The usual cause of this situation is code like:
12955 T t;
12957 where "T" should name a type -- but does not. */
12958 if (!decl_specifiers.any_type_specifiers_p
12959 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12961 /* If parsing tentatively, we should commit; we really are
12962 looking at a declaration. */
12963 cp_parser_commit_to_tentative_parse (parser);
12964 /* Give up. */
12965 goto done;
12968 /* If we have seen at least one decl-specifier, and the next token
12969 is not a parenthesis, then we must be looking at a declaration.
12970 (After "int (" we might be looking at a functional cast.) */
12971 if (decl_specifiers.any_specifiers_p
12972 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12973 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12974 && !cp_parser_error_occurred (parser))
12975 cp_parser_commit_to_tentative_parse (parser);
12977 /* Look for C++17 decomposition declaration. */
12978 for (size_t n = 1; ; n++)
12979 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12980 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12981 continue;
12982 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12983 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12984 && decl_specifiers.any_specifiers_p)
12986 tree decl
12987 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12988 maybe_range_for_decl,
12989 &init_loc);
12991 /* The next token should be either a `,' or a `;'. */
12992 cp_token *token = cp_lexer_peek_token (parser->lexer);
12993 /* If it's a `;', we are done. */
12994 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12995 goto finish;
12996 /* Anything else is an error. */
12997 else
12999 /* If we have already issued an error message we don't need
13000 to issue another one. */
13001 if ((decl != error_mark_node
13002 && DECL_INITIAL (decl) != error_mark_node)
13003 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13004 cp_parser_error (parser, "expected %<,%> or %<;%>");
13005 /* Skip tokens until we reach the end of the statement. */
13006 cp_parser_skip_to_end_of_statement (parser);
13007 /* If the next token is now a `;', consume it. */
13008 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13009 cp_lexer_consume_token (parser->lexer);
13010 goto done;
13013 else
13014 break;
13016 tree last_type;
13017 bool auto_specifier_p;
13018 /* NULL_TREE if both variable and function declaration are allowed,
13019 error_mark_node if function declaration are not allowed and
13020 a FUNCTION_DECL that should be diagnosed if it is followed by
13021 variable declarations. */
13022 tree auto_function_declaration;
13024 last_type = NULL_TREE;
13025 auto_specifier_p
13026 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13027 auto_function_declaration = NULL_TREE;
13029 /* Keep going until we hit the `;' at the end of the simple
13030 declaration. */
13031 saw_declarator = false;
13032 while (cp_lexer_next_token_is_not (parser->lexer,
13033 CPP_SEMICOLON))
13035 cp_token *token;
13036 bool function_definition_p;
13037 tree decl;
13038 tree auto_result = NULL_TREE;
13040 if (saw_declarator)
13042 /* If we are processing next declarator, comma is expected */
13043 token = cp_lexer_peek_token (parser->lexer);
13044 gcc_assert (token->type == CPP_COMMA);
13045 cp_lexer_consume_token (parser->lexer);
13046 if (maybe_range_for_decl)
13048 *maybe_range_for_decl = error_mark_node;
13049 if (comma_loc == UNKNOWN_LOCATION)
13050 comma_loc = token->location;
13053 else
13054 saw_declarator = true;
13056 /* Parse the init-declarator. */
13057 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13058 /*checks=*/NULL,
13059 function_definition_allowed_p,
13060 /*member_p=*/false,
13061 declares_class_or_enum,
13062 &function_definition_p,
13063 maybe_range_for_decl,
13064 &init_loc,
13065 &auto_result);
13066 /* If an error occurred while parsing tentatively, exit quickly.
13067 (That usually happens when in the body of a function; each
13068 statement is treated as a declaration-statement until proven
13069 otherwise.) */
13070 if (cp_parser_error_occurred (parser))
13071 goto done;
13073 if (auto_specifier_p && cxx_dialect >= cxx14)
13075 /* If the init-declarator-list contains more than one
13076 init-declarator, they shall all form declarations of
13077 variables. */
13078 if (auto_function_declaration == NULL_TREE)
13079 auto_function_declaration
13080 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13081 else if (TREE_CODE (decl) == FUNCTION_DECL
13082 || auto_function_declaration != error_mark_node)
13084 error_at (decl_specifiers.locations[ds_type_spec],
13085 "non-variable %qD in declaration with more than one "
13086 "declarator with placeholder type",
13087 TREE_CODE (decl) == FUNCTION_DECL
13088 ? decl : auto_function_declaration);
13089 auto_function_declaration = error_mark_node;
13093 if (auto_result
13094 && (!processing_template_decl || !type_uses_auto (auto_result)))
13096 if (last_type
13097 && last_type != error_mark_node
13098 && !same_type_p (auto_result, last_type))
13100 /* If the list of declarators contains more than one declarator,
13101 the type of each declared variable is determined as described
13102 above. If the type deduced for the template parameter U is not
13103 the same in each deduction, the program is ill-formed. */
13104 error_at (decl_specifiers.locations[ds_type_spec],
13105 "inconsistent deduction for %qT: %qT and then %qT",
13106 decl_specifiers.type, last_type, auto_result);
13107 last_type = error_mark_node;
13109 else
13110 last_type = auto_result;
13113 /* Handle function definitions specially. */
13114 if (function_definition_p)
13116 /* If the next token is a `,', then we are probably
13117 processing something like:
13119 void f() {}, *p;
13121 which is erroneous. */
13122 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13124 cp_token *token = cp_lexer_peek_token (parser->lexer);
13125 error_at (token->location,
13126 "mixing"
13127 " declarations and function-definitions is forbidden");
13129 /* Otherwise, we're done with the list of declarators. */
13130 else
13132 pop_deferring_access_checks ();
13133 return;
13136 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13137 *maybe_range_for_decl = decl;
13138 /* The next token should be either a `,' or a `;'. */
13139 token = cp_lexer_peek_token (parser->lexer);
13140 /* If it's a `,', there are more declarators to come. */
13141 if (token->type == CPP_COMMA)
13142 /* will be consumed next time around */;
13143 /* If it's a `;', we are done. */
13144 else if (token->type == CPP_SEMICOLON)
13145 break;
13146 else if (maybe_range_for_decl)
13148 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13149 permerror (decl_specifiers.locations[ds_type_spec],
13150 "types may not be defined in a for-range-declaration");
13151 break;
13153 /* Anything else is an error. */
13154 else
13156 /* If we have already issued an error message we don't need
13157 to issue another one. */
13158 if ((decl != error_mark_node
13159 && DECL_INITIAL (decl) != error_mark_node)
13160 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13161 cp_parser_error (parser, "expected %<,%> or %<;%>");
13162 /* Skip tokens until we reach the end of the statement. */
13163 cp_parser_skip_to_end_of_statement (parser);
13164 /* If the next token is now a `;', consume it. */
13165 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13166 cp_lexer_consume_token (parser->lexer);
13167 goto done;
13169 /* After the first time around, a function-definition is not
13170 allowed -- even if it was OK at first. For example:
13172 int i, f() {}
13174 is not valid. */
13175 function_definition_allowed_p = false;
13178 /* Issue an error message if no declarators are present, and the
13179 decl-specifier-seq does not itself declare a class or
13180 enumeration: [dcl.dcl]/3. */
13181 if (!saw_declarator)
13183 if (cp_parser_declares_only_class_p (parser))
13185 if (!declares_class_or_enum
13186 && decl_specifiers.type
13187 && OVERLOAD_TYPE_P (decl_specifiers.type))
13188 /* Ensure an error is issued anyway when finish_decltype_type,
13189 called via cp_parser_decl_specifier_seq, returns a class or
13190 an enumeration (c++/51786). */
13191 decl_specifiers.type = NULL_TREE;
13192 shadow_tag (&decl_specifiers);
13194 /* Perform any deferred access checks. */
13195 perform_deferred_access_checks (tf_warning_or_error);
13198 /* Consume the `;'. */
13199 finish:
13200 if (!maybe_range_for_decl)
13201 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13202 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13204 if (init_loc != UNKNOWN_LOCATION)
13205 error_at (init_loc, "initializer in range-based %<for%> loop");
13206 if (comma_loc != UNKNOWN_LOCATION)
13207 error_at (comma_loc,
13208 "multiple declarations in range-based %<for%> loop");
13211 done:
13212 pop_deferring_access_checks ();
13215 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13216 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13217 initializer ; */
13219 static tree
13220 cp_parser_decomposition_declaration (cp_parser *parser,
13221 cp_decl_specifier_seq *decl_specifiers,
13222 tree *maybe_range_for_decl,
13223 location_t *init_loc)
13225 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13226 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13227 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13229 /* Parse the identifier-list. */
13230 auto_vec<cp_expr, 10> v;
13231 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13232 while (true)
13234 cp_expr e = cp_parser_identifier (parser);
13235 if (e.get_value () == error_mark_node)
13236 break;
13237 v.safe_push (e);
13238 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13239 break;
13240 cp_lexer_consume_token (parser->lexer);
13243 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13244 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13246 end_loc = UNKNOWN_LOCATION;
13247 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13248 false);
13249 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13250 cp_lexer_consume_token (parser->lexer);
13251 else
13253 cp_parser_skip_to_end_of_statement (parser);
13254 return error_mark_node;
13258 if (cxx_dialect < cxx17)
13259 pedwarn (loc, 0, "structured bindings only available with "
13260 "-std=c++17 or -std=gnu++17");
13262 tree pushed_scope;
13263 cp_declarator *declarator = make_declarator (cdk_decomp);
13264 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13265 declarator->id_loc = loc;
13266 if (ref_qual != REF_QUAL_NONE)
13267 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13268 ref_qual == REF_QUAL_RVALUE,
13269 NULL_TREE);
13270 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13271 NULL_TREE, decl_specifiers->attributes,
13272 &pushed_scope);
13273 tree orig_decl = decl;
13275 unsigned int i;
13276 cp_expr e;
13277 cp_decl_specifier_seq decl_specs;
13278 clear_decl_specs (&decl_specs);
13279 decl_specs.type = make_auto ();
13280 tree prev = decl;
13281 FOR_EACH_VEC_ELT (v, i, e)
13283 if (i == 0)
13284 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13285 else
13286 declarator->u.id.unqualified_name = e.get_value ();
13287 declarator->id_loc = e.get_location ();
13288 tree elt_pushed_scope;
13289 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13290 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13291 if (decl2 == error_mark_node)
13292 decl = error_mark_node;
13293 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13295 /* Ensure we've diagnosed redeclaration if we aren't creating
13296 a new VAR_DECL. */
13297 gcc_assert (errorcount);
13298 decl = error_mark_node;
13300 else
13301 prev = decl2;
13302 if (elt_pushed_scope)
13303 pop_scope (elt_pushed_scope);
13306 if (v.is_empty ())
13308 error_at (loc, "empty structured binding declaration");
13309 decl = error_mark_node;
13312 if (maybe_range_for_decl == NULL
13313 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13315 bool non_constant_p = false, is_direct_init = false;
13316 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13317 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13318 &non_constant_p);
13319 if (initializer == NULL_TREE
13320 || (TREE_CODE (initializer) == TREE_LIST
13321 && TREE_CHAIN (initializer))
13322 || (is_direct_init
13323 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13324 && CONSTRUCTOR_NELTS (initializer) != 1))
13326 error_at (loc, "invalid initializer for structured binding "
13327 "declaration");
13328 initializer = error_mark_node;
13331 if (decl != error_mark_node)
13333 cp_maybe_mangle_decomp (decl, prev, v.length ());
13334 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13335 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13336 cp_finish_decomp (decl, prev, v.length ());
13339 else if (decl != error_mark_node)
13341 *maybe_range_for_decl = prev;
13342 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13343 the underlying DECL. */
13344 cp_finish_decomp (decl, prev, v.length ());
13347 if (pushed_scope)
13348 pop_scope (pushed_scope);
13350 if (decl == error_mark_node && DECL_P (orig_decl))
13352 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13353 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13356 return decl;
13359 /* Parse a decl-specifier-seq.
13361 decl-specifier-seq:
13362 decl-specifier-seq [opt] decl-specifier
13363 decl-specifier attribute-specifier-seq [opt] (C++11)
13365 decl-specifier:
13366 storage-class-specifier
13367 type-specifier
13368 function-specifier
13369 friend
13370 typedef
13372 GNU Extension:
13374 decl-specifier:
13375 attributes
13377 Concepts Extension:
13379 decl-specifier:
13380 concept
13382 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13384 The parser flags FLAGS is used to control type-specifier parsing.
13386 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13387 flags:
13389 1: one of the decl-specifiers is an elaborated-type-specifier
13390 (i.e., a type declaration)
13391 2: one of the decl-specifiers is an enum-specifier or a
13392 class-specifier (i.e., a type definition)
13396 static void
13397 cp_parser_decl_specifier_seq (cp_parser* parser,
13398 cp_parser_flags flags,
13399 cp_decl_specifier_seq *decl_specs,
13400 int* declares_class_or_enum)
13402 bool constructor_possible_p = !parser->in_declarator_p;
13403 bool found_decl_spec = false;
13404 cp_token *start_token = NULL;
13405 cp_decl_spec ds;
13407 /* Clear DECL_SPECS. */
13408 clear_decl_specs (decl_specs);
13410 /* Assume no class or enumeration type is declared. */
13411 *declares_class_or_enum = 0;
13413 /* Keep reading specifiers until there are no more to read. */
13414 while (true)
13416 bool constructor_p;
13417 cp_token *token;
13418 ds = ds_last;
13420 /* Peek at the next token. */
13421 token = cp_lexer_peek_token (parser->lexer);
13423 /* Save the first token of the decl spec list for error
13424 reporting. */
13425 if (!start_token)
13426 start_token = token;
13427 /* Handle attributes. */
13428 if (cp_next_tokens_can_be_attribute_p (parser))
13430 /* Parse the attributes. */
13431 tree attrs = cp_parser_attributes_opt (parser);
13433 /* In a sequence of declaration specifiers, c++11 attributes
13434 appertain to the type that precede them. In that case
13435 [dcl.spec]/1 says:
13437 The attribute-specifier-seq affects the type only for
13438 the declaration it appears in, not other declarations
13439 involving the same type.
13441 But for now let's force the user to position the
13442 attribute either at the beginning of the declaration or
13443 after the declarator-id, which would clearly mean that it
13444 applies to the declarator. */
13445 if (cxx11_attribute_p (attrs))
13447 if (!found_decl_spec)
13448 /* The c++11 attribute is at the beginning of the
13449 declaration. It appertains to the entity being
13450 declared. */;
13451 else
13453 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13455 /* This is an attribute following a
13456 class-specifier. */
13457 if (decl_specs->type_definition_p)
13458 warn_misplaced_attr_for_class_type (token->location,
13459 decl_specs->type);
13460 attrs = NULL_TREE;
13462 else
13464 decl_specs->std_attributes
13465 = attr_chainon (decl_specs->std_attributes, attrs);
13466 if (decl_specs->locations[ds_std_attribute] == 0)
13467 decl_specs->locations[ds_std_attribute] = token->location;
13469 continue;
13473 decl_specs->attributes
13474 = attr_chainon (decl_specs->attributes, attrs);
13475 if (decl_specs->locations[ds_attribute] == 0)
13476 decl_specs->locations[ds_attribute] = token->location;
13477 continue;
13479 /* Assume we will find a decl-specifier keyword. */
13480 found_decl_spec = true;
13481 /* If the next token is an appropriate keyword, we can simply
13482 add it to the list. */
13483 switch (token->keyword)
13485 /* decl-specifier:
13486 friend
13487 constexpr */
13488 case RID_FRIEND:
13489 if (!at_class_scope_p ())
13491 gcc_rich_location richloc (token->location);
13492 richloc.add_fixit_remove ();
13493 error_at (&richloc, "%<friend%> used outside of class");
13494 cp_lexer_purge_token (parser->lexer);
13496 else
13498 ds = ds_friend;
13499 /* Consume the token. */
13500 cp_lexer_consume_token (parser->lexer);
13502 break;
13504 case RID_CONSTEXPR:
13505 ds = ds_constexpr;
13506 cp_lexer_consume_token (parser->lexer);
13507 break;
13509 case RID_CONCEPT:
13510 ds = ds_concept;
13511 cp_lexer_consume_token (parser->lexer);
13512 break;
13514 /* function-specifier:
13515 inline
13516 virtual
13517 explicit */
13518 case RID_INLINE:
13519 case RID_VIRTUAL:
13520 case RID_EXPLICIT:
13521 cp_parser_function_specifier_opt (parser, decl_specs);
13522 break;
13524 /* decl-specifier:
13525 typedef */
13526 case RID_TYPEDEF:
13527 ds = ds_typedef;
13528 /* Consume the token. */
13529 cp_lexer_consume_token (parser->lexer);
13530 /* A constructor declarator cannot appear in a typedef. */
13531 constructor_possible_p = false;
13532 /* The "typedef" keyword can only occur in a declaration; we
13533 may as well commit at this point. */
13534 cp_parser_commit_to_tentative_parse (parser);
13536 if (decl_specs->storage_class != sc_none)
13537 decl_specs->conflicting_specifiers_p = true;
13538 break;
13540 /* storage-class-specifier:
13541 auto
13542 register
13543 static
13544 extern
13545 mutable
13547 GNU Extension:
13548 thread */
13549 case RID_AUTO:
13550 if (cxx_dialect == cxx98)
13552 /* Consume the token. */
13553 cp_lexer_consume_token (parser->lexer);
13555 /* Complain about `auto' as a storage specifier, if
13556 we're complaining about C++0x compatibility. */
13557 gcc_rich_location richloc (token->location);
13558 richloc.add_fixit_remove ();
13559 warning_at (&richloc, OPT_Wc__11_compat,
13560 "%<auto%> changes meaning in C++11; "
13561 "please remove it");
13563 /* Set the storage class anyway. */
13564 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13565 token);
13567 else
13568 /* C++0x auto type-specifier. */
13569 found_decl_spec = false;
13570 break;
13572 case RID_REGISTER:
13573 case RID_STATIC:
13574 case RID_EXTERN:
13575 case RID_MUTABLE:
13576 /* Consume the token. */
13577 cp_lexer_consume_token (parser->lexer);
13578 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13579 token);
13580 break;
13581 case RID_THREAD:
13582 /* Consume the token. */
13583 ds = ds_thread;
13584 cp_lexer_consume_token (parser->lexer);
13585 break;
13587 default:
13588 /* We did not yet find a decl-specifier yet. */
13589 found_decl_spec = false;
13590 break;
13593 if (found_decl_spec
13594 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13595 && token->keyword != RID_CONSTEXPR)
13596 error ("decl-specifier invalid in condition");
13598 if (found_decl_spec
13599 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13600 && token->keyword != RID_MUTABLE
13601 && token->keyword != RID_CONSTEXPR)
13602 error_at (token->location, "%qD invalid in lambda",
13603 ridpointers[token->keyword]);
13605 if (ds != ds_last)
13606 set_and_check_decl_spec_loc (decl_specs, ds, token);
13608 /* Constructors are a special case. The `S' in `S()' is not a
13609 decl-specifier; it is the beginning of the declarator. */
13610 constructor_p
13611 = (!found_decl_spec
13612 && constructor_possible_p
13613 && (cp_parser_constructor_declarator_p
13614 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13616 /* If we don't have a DECL_SPEC yet, then we must be looking at
13617 a type-specifier. */
13618 if (!found_decl_spec && !constructor_p)
13620 int decl_spec_declares_class_or_enum;
13621 bool is_cv_qualifier;
13622 tree type_spec;
13624 type_spec
13625 = cp_parser_type_specifier (parser, flags,
13626 decl_specs,
13627 /*is_declaration=*/true,
13628 &decl_spec_declares_class_or_enum,
13629 &is_cv_qualifier);
13630 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13632 /* If this type-specifier referenced a user-defined type
13633 (a typedef, class-name, etc.), then we can't allow any
13634 more such type-specifiers henceforth.
13636 [dcl.spec]
13638 The longest sequence of decl-specifiers that could
13639 possibly be a type name is taken as the
13640 decl-specifier-seq of a declaration. The sequence shall
13641 be self-consistent as described below.
13643 [dcl.type]
13645 As a general rule, at most one type-specifier is allowed
13646 in the complete decl-specifier-seq of a declaration. The
13647 only exceptions are the following:
13649 -- const or volatile can be combined with any other
13650 type-specifier.
13652 -- signed or unsigned can be combined with char, long,
13653 short, or int.
13655 -- ..
13657 Example:
13659 typedef char* Pc;
13660 void g (const int Pc);
13662 Here, Pc is *not* part of the decl-specifier seq; it's
13663 the declarator. Therefore, once we see a type-specifier
13664 (other than a cv-qualifier), we forbid any additional
13665 user-defined types. We *do* still allow things like `int
13666 int' to be considered a decl-specifier-seq, and issue the
13667 error message later. */
13668 if (type_spec && !is_cv_qualifier)
13669 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13670 /* A constructor declarator cannot follow a type-specifier. */
13671 if (type_spec)
13673 constructor_possible_p = false;
13674 found_decl_spec = true;
13675 if (!is_cv_qualifier)
13676 decl_specs->any_type_specifiers_p = true;
13680 /* If we still do not have a DECL_SPEC, then there are no more
13681 decl-specifiers. */
13682 if (!found_decl_spec)
13683 break;
13685 decl_specs->any_specifiers_p = true;
13686 /* After we see one decl-specifier, further decl-specifiers are
13687 always optional. */
13688 flags |= CP_PARSER_FLAGS_OPTIONAL;
13691 /* Don't allow a friend specifier with a class definition. */
13692 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13693 && (*declares_class_or_enum & 2))
13694 error_at (decl_specs->locations[ds_friend],
13695 "class definition may not be declared a friend");
13698 /* Parse an (optional) storage-class-specifier.
13700 storage-class-specifier:
13701 auto
13702 register
13703 static
13704 extern
13705 mutable
13707 GNU Extension:
13709 storage-class-specifier:
13710 thread
13712 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13714 static tree
13715 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13717 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13719 case RID_AUTO:
13720 if (cxx_dialect != cxx98)
13721 return NULL_TREE;
13722 /* Fall through for C++98. */
13723 gcc_fallthrough ();
13725 case RID_REGISTER:
13726 case RID_STATIC:
13727 case RID_EXTERN:
13728 case RID_MUTABLE:
13729 case RID_THREAD:
13730 /* Consume the token. */
13731 return cp_lexer_consume_token (parser->lexer)->u.value;
13733 default:
13734 return NULL_TREE;
13738 /* Parse an (optional) function-specifier.
13740 function-specifier:
13741 inline
13742 virtual
13743 explicit
13745 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13746 Updates DECL_SPECS, if it is non-NULL. */
13748 static tree
13749 cp_parser_function_specifier_opt (cp_parser* parser,
13750 cp_decl_specifier_seq *decl_specs)
13752 cp_token *token = cp_lexer_peek_token (parser->lexer);
13753 switch (token->keyword)
13755 case RID_INLINE:
13756 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13757 break;
13759 case RID_VIRTUAL:
13760 /* 14.5.2.3 [temp.mem]
13762 A member function template shall not be virtual. */
13763 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13764 && current_class_type)
13765 error_at (token->location, "templates may not be %<virtual%>");
13766 else
13767 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13768 break;
13770 case RID_EXPLICIT:
13771 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13772 break;
13774 default:
13775 return NULL_TREE;
13778 /* Consume the token. */
13779 return cp_lexer_consume_token (parser->lexer)->u.value;
13782 /* Parse a linkage-specification.
13784 linkage-specification:
13785 extern string-literal { declaration-seq [opt] }
13786 extern string-literal declaration */
13788 static void
13789 cp_parser_linkage_specification (cp_parser* parser)
13791 tree linkage;
13793 /* Look for the `extern' keyword. */
13794 cp_token *extern_token
13795 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13797 /* Look for the string-literal. */
13798 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13799 linkage = cp_parser_string_literal (parser, false, false);
13801 /* Transform the literal into an identifier. If the literal is a
13802 wide-character string, or contains embedded NULs, then we can't
13803 handle it as the user wants. */
13804 if (strlen (TREE_STRING_POINTER (linkage))
13805 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13807 cp_parser_error (parser, "invalid linkage-specification");
13808 /* Assume C++ linkage. */
13809 linkage = lang_name_cplusplus;
13811 else
13812 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13814 /* We're now using the new linkage. */
13815 push_lang_context (linkage);
13817 /* Preserve the location of the the innermost linkage specification,
13818 tracking the locations of nested specifications via a local. */
13819 location_t saved_location
13820 = parser->innermost_linkage_specification_location;
13821 /* Construct a location ranging from the start of the "extern" to
13822 the end of the string-literal, with the caret at the start, e.g.:
13823 extern "C" {
13824 ^~~~~~~~~~
13826 parser->innermost_linkage_specification_location
13827 = make_location (extern_token->location,
13828 extern_token->location,
13829 get_finish (string_token->location));
13831 /* If the next token is a `{', then we're using the first
13832 production. */
13833 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13835 cp_ensure_no_omp_declare_simd (parser);
13836 cp_ensure_no_oacc_routine (parser);
13838 /* Consume the `{' token. */
13839 matching_braces braces;
13840 braces.consume_open (parser)->location;
13841 /* Parse the declarations. */
13842 cp_parser_declaration_seq_opt (parser);
13843 /* Look for the closing `}'. */
13844 braces.require_close (parser);
13846 /* Otherwise, there's just one declaration. */
13847 else
13849 bool saved_in_unbraced_linkage_specification_p;
13851 saved_in_unbraced_linkage_specification_p
13852 = parser->in_unbraced_linkage_specification_p;
13853 parser->in_unbraced_linkage_specification_p = true;
13854 cp_parser_declaration (parser);
13855 parser->in_unbraced_linkage_specification_p
13856 = saved_in_unbraced_linkage_specification_p;
13859 /* We're done with the linkage-specification. */
13860 pop_lang_context ();
13862 /* Restore location of parent linkage specification, if any. */
13863 parser->innermost_linkage_specification_location = saved_location;
13866 /* Parse a static_assert-declaration.
13868 static_assert-declaration:
13869 static_assert ( constant-expression , string-literal ) ;
13870 static_assert ( constant-expression ) ; (C++17)
13872 If MEMBER_P, this static_assert is a class member. */
13874 static void
13875 cp_parser_static_assert(cp_parser *parser, bool member_p)
13877 cp_expr condition;
13878 location_t token_loc;
13879 tree message;
13880 bool dummy;
13882 /* Peek at the `static_assert' token so we can keep track of exactly
13883 where the static assertion started. */
13884 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13886 /* Look for the `static_assert' keyword. */
13887 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13888 RT_STATIC_ASSERT))
13889 return;
13891 /* We know we are in a static assertion; commit to any tentative
13892 parse. */
13893 if (cp_parser_parsing_tentatively (parser))
13894 cp_parser_commit_to_tentative_parse (parser);
13896 /* Parse the `(' starting the static assertion condition. */
13897 matching_parens parens;
13898 parens.require_open (parser);
13900 /* Parse the constant-expression. Allow a non-constant expression
13901 here in order to give better diagnostics in finish_static_assert. */
13902 condition =
13903 cp_parser_constant_expression (parser,
13904 /*allow_non_constant_p=*/true,
13905 /*non_constant_p=*/&dummy);
13907 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13909 if (cxx_dialect < cxx17)
13910 pedwarn (input_location, OPT_Wpedantic,
13911 "static_assert without a message "
13912 "only available with -std=c++17 or -std=gnu++17");
13913 /* Eat the ')' */
13914 cp_lexer_consume_token (parser->lexer);
13915 message = build_string (1, "");
13916 TREE_TYPE (message) = char_array_type_node;
13917 fix_string_type (message);
13919 else
13921 /* Parse the separating `,'. */
13922 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13924 /* Parse the string-literal message. */
13925 message = cp_parser_string_literal (parser,
13926 /*translate=*/false,
13927 /*wide_ok=*/true);
13929 /* A `)' completes the static assertion. */
13930 if (!parens.require_close (parser))
13931 cp_parser_skip_to_closing_parenthesis (parser,
13932 /*recovering=*/true,
13933 /*or_comma=*/false,
13934 /*consume_paren=*/true);
13937 /* A semicolon terminates the declaration. */
13938 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13940 /* Get the location for the static assertion. Use that of the
13941 condition if available, otherwise, use that of the "static_assert"
13942 token. */
13943 location_t assert_loc = condition.get_location ();
13944 if (assert_loc == UNKNOWN_LOCATION)
13945 assert_loc = token_loc;
13947 /* Complete the static assertion, which may mean either processing
13948 the static assert now or saving it for template instantiation. */
13949 finish_static_assert (condition, message, assert_loc, member_p);
13952 /* Parse the expression in decltype ( expression ). */
13954 static tree
13955 cp_parser_decltype_expr (cp_parser *parser,
13956 bool &id_expression_or_member_access_p)
13958 cp_token *id_expr_start_token;
13959 tree expr;
13961 /* Since we're going to preserve any side-effects from this parse, set up a
13962 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13963 in the expression. */
13964 tentative_firewall firewall (parser);
13966 /* First, try parsing an id-expression. */
13967 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13968 cp_parser_parse_tentatively (parser);
13969 expr = cp_parser_id_expression (parser,
13970 /*template_keyword_p=*/false,
13971 /*check_dependency_p=*/true,
13972 /*template_p=*/NULL,
13973 /*declarator_p=*/false,
13974 /*optional_p=*/false);
13976 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13978 bool non_integral_constant_expression_p = false;
13979 tree id_expression = expr;
13980 cp_id_kind idk;
13981 const char *error_msg;
13983 if (identifier_p (expr))
13984 /* Lookup the name we got back from the id-expression. */
13985 expr = cp_parser_lookup_name_simple (parser, expr,
13986 id_expr_start_token->location);
13988 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13989 /* A template without args is not a complete id-expression. */
13990 expr = error_mark_node;
13992 if (expr
13993 && expr != error_mark_node
13994 && TREE_CODE (expr) != TYPE_DECL
13995 && (TREE_CODE (expr) != BIT_NOT_EXPR
13996 || !TYPE_P (TREE_OPERAND (expr, 0)))
13997 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13999 /* Complete lookup of the id-expression. */
14000 expr = (finish_id_expression
14001 (id_expression, expr, parser->scope, &idk,
14002 /*integral_constant_expression_p=*/false,
14003 /*allow_non_integral_constant_expression_p=*/true,
14004 &non_integral_constant_expression_p,
14005 /*template_p=*/false,
14006 /*done=*/true,
14007 /*address_p=*/false,
14008 /*template_arg_p=*/false,
14009 &error_msg,
14010 id_expr_start_token->location));
14012 if (expr == error_mark_node)
14013 /* We found an id-expression, but it was something that we
14014 should not have found. This is an error, not something
14015 we can recover from, so note that we found an
14016 id-expression and we'll recover as gracefully as
14017 possible. */
14018 id_expression_or_member_access_p = true;
14021 if (expr
14022 && expr != error_mark_node
14023 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14024 /* We have an id-expression. */
14025 id_expression_or_member_access_p = true;
14028 if (!id_expression_or_member_access_p)
14030 /* Abort the id-expression parse. */
14031 cp_parser_abort_tentative_parse (parser);
14033 /* Parsing tentatively, again. */
14034 cp_parser_parse_tentatively (parser);
14036 /* Parse a class member access. */
14037 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14038 /*cast_p=*/false, /*decltype*/true,
14039 /*member_access_only_p=*/true, NULL);
14041 if (expr
14042 && expr != error_mark_node
14043 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14044 /* We have an id-expression. */
14045 id_expression_or_member_access_p = true;
14048 if (id_expression_or_member_access_p)
14049 /* We have parsed the complete id-expression or member access. */
14050 cp_parser_parse_definitely (parser);
14051 else
14053 /* Abort our attempt to parse an id-expression or member access
14054 expression. */
14055 cp_parser_abort_tentative_parse (parser);
14057 /* Commit to the tentative_firewall so we get syntax errors. */
14058 cp_parser_commit_to_tentative_parse (parser);
14060 /* Parse a full expression. */
14061 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14062 /*decltype_p=*/true);
14065 return expr;
14068 /* Parse a `decltype' type. Returns the type.
14070 simple-type-specifier:
14071 decltype ( expression )
14072 C++14 proposal:
14073 decltype ( auto ) */
14075 static tree
14076 cp_parser_decltype (cp_parser *parser)
14078 bool id_expression_or_member_access_p = false;
14079 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14081 if (start_token->type == CPP_DECLTYPE)
14083 /* Already parsed. */
14084 cp_lexer_consume_token (parser->lexer);
14085 return saved_checks_value (start_token->u.tree_check_value);
14088 /* Look for the `decltype' token. */
14089 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14090 return error_mark_node;
14092 /* Parse the opening `('. */
14093 matching_parens parens;
14094 if (!parens.require_open (parser))
14095 return error_mark_node;
14097 push_deferring_access_checks (dk_deferred);
14099 tree expr = NULL_TREE;
14101 if (cxx_dialect >= cxx14
14102 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14103 /* decltype (auto) */
14104 cp_lexer_consume_token (parser->lexer);
14105 else
14107 /* decltype (expression) */
14109 /* Types cannot be defined in a `decltype' expression. Save away the
14110 old message and set the new one. */
14111 const char *saved_message = parser->type_definition_forbidden_message;
14112 parser->type_definition_forbidden_message
14113 = G_("types may not be defined in %<decltype%> expressions");
14115 /* The restrictions on constant-expressions do not apply inside
14116 decltype expressions. */
14117 bool saved_integral_constant_expression_p
14118 = parser->integral_constant_expression_p;
14119 bool saved_non_integral_constant_expression_p
14120 = parser->non_integral_constant_expression_p;
14121 parser->integral_constant_expression_p = false;
14123 /* Within a parenthesized expression, a `>' token is always
14124 the greater-than operator. */
14125 bool saved_greater_than_is_operator_p
14126 = parser->greater_than_is_operator_p;
14127 parser->greater_than_is_operator_p = true;
14129 /* Do not actually evaluate the expression. */
14130 ++cp_unevaluated_operand;
14132 /* Do not warn about problems with the expression. */
14133 ++c_inhibit_evaluation_warnings;
14135 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14137 /* Go back to evaluating expressions. */
14138 --cp_unevaluated_operand;
14139 --c_inhibit_evaluation_warnings;
14141 /* The `>' token might be the end of a template-id or
14142 template-parameter-list now. */
14143 parser->greater_than_is_operator_p
14144 = saved_greater_than_is_operator_p;
14146 /* Restore the old message and the integral constant expression
14147 flags. */
14148 parser->type_definition_forbidden_message = saved_message;
14149 parser->integral_constant_expression_p
14150 = saved_integral_constant_expression_p;
14151 parser->non_integral_constant_expression_p
14152 = saved_non_integral_constant_expression_p;
14155 /* Parse to the closing `)'. */
14156 if (!parens.require_close (parser))
14158 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14159 /*consume_paren=*/true);
14160 pop_deferring_access_checks ();
14161 return error_mark_node;
14164 if (!expr)
14166 /* Build auto. */
14167 expr = make_decltype_auto ();
14168 AUTO_IS_DECLTYPE (expr) = true;
14170 else
14171 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14172 tf_warning_or_error);
14174 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14175 it again. */
14176 start_token->type = CPP_DECLTYPE;
14177 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14178 start_token->u.tree_check_value->value = expr;
14179 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14180 start_token->keyword = RID_MAX;
14181 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14183 pop_to_parent_deferring_access_checks ();
14185 return expr;
14188 /* Special member functions [gram.special] */
14190 /* Parse a conversion-function-id.
14192 conversion-function-id:
14193 operator conversion-type-id
14195 Returns an IDENTIFIER_NODE representing the operator. */
14197 static tree
14198 cp_parser_conversion_function_id (cp_parser* parser)
14200 tree type;
14201 tree saved_scope;
14202 tree saved_qualifying_scope;
14203 tree saved_object_scope;
14204 tree pushed_scope = NULL_TREE;
14206 /* Look for the `operator' token. */
14207 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14208 return error_mark_node;
14209 /* When we parse the conversion-type-id, the current scope will be
14210 reset. However, we need that information in able to look up the
14211 conversion function later, so we save it here. */
14212 saved_scope = parser->scope;
14213 saved_qualifying_scope = parser->qualifying_scope;
14214 saved_object_scope = parser->object_scope;
14215 /* We must enter the scope of the class so that the names of
14216 entities declared within the class are available in the
14217 conversion-type-id. For example, consider:
14219 struct S {
14220 typedef int I;
14221 operator I();
14224 S::operator I() { ... }
14226 In order to see that `I' is a type-name in the definition, we
14227 must be in the scope of `S'. */
14228 if (saved_scope)
14229 pushed_scope = push_scope (saved_scope);
14230 /* Parse the conversion-type-id. */
14231 type = cp_parser_conversion_type_id (parser);
14232 /* Leave the scope of the class, if any. */
14233 if (pushed_scope)
14234 pop_scope (pushed_scope);
14235 /* Restore the saved scope. */
14236 parser->scope = saved_scope;
14237 parser->qualifying_scope = saved_qualifying_scope;
14238 parser->object_scope = saved_object_scope;
14239 /* If the TYPE is invalid, indicate failure. */
14240 if (type == error_mark_node)
14241 return error_mark_node;
14242 return make_conv_op_name (type);
14245 /* Parse a conversion-type-id:
14247 conversion-type-id:
14248 type-specifier-seq conversion-declarator [opt]
14250 Returns the TYPE specified. */
14252 static tree
14253 cp_parser_conversion_type_id (cp_parser* parser)
14255 tree attributes;
14256 cp_decl_specifier_seq type_specifiers;
14257 cp_declarator *declarator;
14258 tree type_specified;
14259 const char *saved_message;
14261 /* Parse the attributes. */
14262 attributes = cp_parser_attributes_opt (parser);
14264 saved_message = parser->type_definition_forbidden_message;
14265 parser->type_definition_forbidden_message
14266 = G_("types may not be defined in a conversion-type-id");
14268 /* Parse the type-specifiers. */
14269 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14270 /*is_trailing_return=*/false,
14271 &type_specifiers);
14273 parser->type_definition_forbidden_message = saved_message;
14275 /* If that didn't work, stop. */
14276 if (type_specifiers.type == error_mark_node)
14277 return error_mark_node;
14278 /* Parse the conversion-declarator. */
14279 declarator = cp_parser_conversion_declarator_opt (parser);
14281 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14282 /*initialized=*/0, &attributes);
14283 if (attributes)
14284 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14286 /* Don't give this error when parsing tentatively. This happens to
14287 work because we always parse this definitively once. */
14288 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14289 && type_uses_auto (type_specified))
14291 if (cxx_dialect < cxx14)
14293 error ("invalid use of %<auto%> in conversion operator");
14294 return error_mark_node;
14296 else if (template_parm_scope_p ())
14297 warning (0, "use of %<auto%> in member template "
14298 "conversion operator can never be deduced");
14301 return type_specified;
14304 /* Parse an (optional) conversion-declarator.
14306 conversion-declarator:
14307 ptr-operator conversion-declarator [opt]
14311 static cp_declarator *
14312 cp_parser_conversion_declarator_opt (cp_parser* parser)
14314 enum tree_code code;
14315 tree class_type, std_attributes = NULL_TREE;
14316 cp_cv_quals cv_quals;
14318 /* We don't know if there's a ptr-operator next, or not. */
14319 cp_parser_parse_tentatively (parser);
14320 /* Try the ptr-operator. */
14321 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14322 &std_attributes);
14323 /* If it worked, look for more conversion-declarators. */
14324 if (cp_parser_parse_definitely (parser))
14326 cp_declarator *declarator;
14328 /* Parse another optional declarator. */
14329 declarator = cp_parser_conversion_declarator_opt (parser);
14331 declarator = cp_parser_make_indirect_declarator
14332 (code, class_type, cv_quals, declarator, std_attributes);
14334 return declarator;
14337 return NULL;
14340 /* Parse an (optional) ctor-initializer.
14342 ctor-initializer:
14343 : mem-initializer-list */
14345 static void
14346 cp_parser_ctor_initializer_opt (cp_parser* parser)
14348 /* If the next token is not a `:', then there is no
14349 ctor-initializer. */
14350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14352 /* Do default initialization of any bases and members. */
14353 if (DECL_CONSTRUCTOR_P (current_function_decl))
14354 finish_mem_initializers (NULL_TREE);
14355 return;
14358 /* Consume the `:' token. */
14359 cp_lexer_consume_token (parser->lexer);
14360 /* And the mem-initializer-list. */
14361 cp_parser_mem_initializer_list (parser);
14364 /* Parse a mem-initializer-list.
14366 mem-initializer-list:
14367 mem-initializer ... [opt]
14368 mem-initializer ... [opt] , mem-initializer-list */
14370 static void
14371 cp_parser_mem_initializer_list (cp_parser* parser)
14373 tree mem_initializer_list = NULL_TREE;
14374 tree target_ctor = error_mark_node;
14375 cp_token *token = cp_lexer_peek_token (parser->lexer);
14377 /* Let the semantic analysis code know that we are starting the
14378 mem-initializer-list. */
14379 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14380 error_at (token->location,
14381 "only constructors take member initializers");
14383 /* Loop through the list. */
14384 while (true)
14386 tree mem_initializer;
14388 token = cp_lexer_peek_token (parser->lexer);
14389 /* Parse the mem-initializer. */
14390 mem_initializer = cp_parser_mem_initializer (parser);
14391 /* If the next token is a `...', we're expanding member initializers. */
14392 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14394 /* Consume the `...'. */
14395 cp_lexer_consume_token (parser->lexer);
14397 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14398 can be expanded but members cannot. */
14399 if (mem_initializer != error_mark_node
14400 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14402 error_at (token->location,
14403 "cannot expand initializer for member %qD",
14404 TREE_PURPOSE (mem_initializer));
14405 mem_initializer = error_mark_node;
14408 /* Construct the pack expansion type. */
14409 if (mem_initializer != error_mark_node)
14410 mem_initializer = make_pack_expansion (mem_initializer);
14412 if (target_ctor != error_mark_node
14413 && mem_initializer != error_mark_node)
14415 error ("mem-initializer for %qD follows constructor delegation",
14416 TREE_PURPOSE (mem_initializer));
14417 mem_initializer = error_mark_node;
14419 /* Look for a target constructor. */
14420 if (mem_initializer != error_mark_node
14421 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14422 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14424 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14425 if (mem_initializer_list)
14427 error ("constructor delegation follows mem-initializer for %qD",
14428 TREE_PURPOSE (mem_initializer_list));
14429 mem_initializer = error_mark_node;
14431 target_ctor = mem_initializer;
14433 /* Add it to the list, unless it was erroneous. */
14434 if (mem_initializer != error_mark_node)
14436 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14437 mem_initializer_list = mem_initializer;
14439 /* If the next token is not a `,', we're done. */
14440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14441 break;
14442 /* Consume the `,' token. */
14443 cp_lexer_consume_token (parser->lexer);
14446 /* Perform semantic analysis. */
14447 if (DECL_CONSTRUCTOR_P (current_function_decl))
14448 finish_mem_initializers (mem_initializer_list);
14451 /* Parse a mem-initializer.
14453 mem-initializer:
14454 mem-initializer-id ( expression-list [opt] )
14455 mem-initializer-id braced-init-list
14457 GNU extension:
14459 mem-initializer:
14460 ( expression-list [opt] )
14462 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14463 class) or FIELD_DECL (for a non-static data member) to initialize;
14464 the TREE_VALUE is the expression-list. An empty initialization
14465 list is represented by void_list_node. */
14467 static tree
14468 cp_parser_mem_initializer (cp_parser* parser)
14470 tree mem_initializer_id;
14471 tree expression_list;
14472 tree member;
14473 cp_token *token = cp_lexer_peek_token (parser->lexer);
14475 /* Find out what is being initialized. */
14476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14478 permerror (token->location,
14479 "anachronistic old-style base class initializer");
14480 mem_initializer_id = NULL_TREE;
14482 else
14484 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14485 if (mem_initializer_id == error_mark_node)
14486 return mem_initializer_id;
14488 member = expand_member_init (mem_initializer_id);
14489 if (member && !DECL_P (member))
14490 in_base_initializer = 1;
14492 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14494 bool expr_non_constant_p;
14495 cp_lexer_set_source_position (parser->lexer);
14496 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14497 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14498 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14499 expression_list = build_tree_list (NULL_TREE, expression_list);
14501 else
14503 vec<tree, va_gc> *vec;
14504 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14505 /*cast_p=*/false,
14506 /*allow_expansion_p=*/true,
14507 /*non_constant_p=*/NULL);
14508 if (vec == NULL)
14509 return error_mark_node;
14510 expression_list = build_tree_list_vec (vec);
14511 release_tree_vector (vec);
14514 if (expression_list == error_mark_node)
14515 return error_mark_node;
14516 if (!expression_list)
14517 expression_list = void_type_node;
14519 in_base_initializer = 0;
14521 return member ? build_tree_list (member, expression_list) : error_mark_node;
14524 /* Parse a mem-initializer-id.
14526 mem-initializer-id:
14527 :: [opt] nested-name-specifier [opt] class-name
14528 decltype-specifier (C++11)
14529 identifier
14531 Returns a TYPE indicating the class to be initialized for the first
14532 production (and the second in C++11). Returns an IDENTIFIER_NODE
14533 indicating the data member to be initialized for the last production. */
14535 static tree
14536 cp_parser_mem_initializer_id (cp_parser* parser)
14538 bool global_scope_p;
14539 bool nested_name_specifier_p;
14540 bool template_p = false;
14541 tree id;
14543 cp_token *token = cp_lexer_peek_token (parser->lexer);
14545 /* `typename' is not allowed in this context ([temp.res]). */
14546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14548 error_at (token->location,
14549 "keyword %<typename%> not allowed in this context (a qualified "
14550 "member initializer is implicitly a type)");
14551 cp_lexer_consume_token (parser->lexer);
14553 /* Look for the optional `::' operator. */
14554 global_scope_p
14555 = (cp_parser_global_scope_opt (parser,
14556 /*current_scope_valid_p=*/false)
14557 != NULL_TREE);
14558 /* Look for the optional nested-name-specifier. The simplest way to
14559 implement:
14561 [temp.res]
14563 The keyword `typename' is not permitted in a base-specifier or
14564 mem-initializer; in these contexts a qualified name that
14565 depends on a template-parameter is implicitly assumed to be a
14566 type name.
14568 is to assume that we have seen the `typename' keyword at this
14569 point. */
14570 nested_name_specifier_p
14571 = (cp_parser_nested_name_specifier_opt (parser,
14572 /*typename_keyword_p=*/true,
14573 /*check_dependency_p=*/true,
14574 /*type_p=*/true,
14575 /*is_declaration=*/true)
14576 != NULL_TREE);
14577 if (nested_name_specifier_p)
14578 template_p = cp_parser_optional_template_keyword (parser);
14579 /* If there is a `::' operator or a nested-name-specifier, then we
14580 are definitely looking for a class-name. */
14581 if (global_scope_p || nested_name_specifier_p)
14582 return cp_parser_class_name (parser,
14583 /*typename_keyword_p=*/true,
14584 /*template_keyword_p=*/template_p,
14585 typename_type,
14586 /*check_dependency_p=*/true,
14587 /*class_head_p=*/false,
14588 /*is_declaration=*/true);
14589 /* Otherwise, we could also be looking for an ordinary identifier. */
14590 cp_parser_parse_tentatively (parser);
14591 if (cp_lexer_next_token_is_decltype (parser->lexer))
14592 /* Try a decltype-specifier. */
14593 id = cp_parser_decltype (parser);
14594 else
14595 /* Otherwise, try a class-name. */
14596 id = cp_parser_class_name (parser,
14597 /*typename_keyword_p=*/true,
14598 /*template_keyword_p=*/false,
14599 none_type,
14600 /*check_dependency_p=*/true,
14601 /*class_head_p=*/false,
14602 /*is_declaration=*/true);
14603 /* If we found one, we're done. */
14604 if (cp_parser_parse_definitely (parser))
14605 return id;
14606 /* Otherwise, look for an ordinary identifier. */
14607 return cp_parser_identifier (parser);
14610 /* Overloading [gram.over] */
14612 /* Parse an operator-function-id.
14614 operator-function-id:
14615 operator operator
14617 Returns an IDENTIFIER_NODE for the operator which is a
14618 human-readable spelling of the identifier, e.g., `operator +'. */
14620 static cp_expr
14621 cp_parser_operator_function_id (cp_parser* parser)
14623 /* Look for the `operator' keyword. */
14624 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14625 return error_mark_node;
14626 /* And then the name of the operator itself. */
14627 return cp_parser_operator (parser);
14630 /* Return an identifier node for a user-defined literal operator.
14631 The suffix identifier is chained to the operator name identifier. */
14633 tree
14634 cp_literal_operator_id (const char* name)
14636 tree identifier;
14637 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14638 + strlen (name) + 10);
14639 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14640 identifier = get_identifier (buffer);
14642 return identifier;
14645 /* Parse an operator.
14647 operator:
14648 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14649 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14650 || ++ -- , ->* -> () []
14652 GNU Extensions:
14654 operator:
14655 <? >? <?= >?=
14657 Returns an IDENTIFIER_NODE for the operator which is a
14658 human-readable spelling of the identifier, e.g., `operator +'. */
14660 static cp_expr
14661 cp_parser_operator (cp_parser* parser)
14663 tree id = NULL_TREE;
14664 cp_token *token;
14665 bool utf8 = false;
14667 /* Peek at the next token. */
14668 token = cp_lexer_peek_token (parser->lexer);
14670 location_t start_loc = token->location;
14672 /* Figure out which operator we have. */
14673 enum tree_code op = ERROR_MARK;
14674 bool assop = false;
14675 bool consumed = false;
14676 switch (token->type)
14678 case CPP_KEYWORD:
14680 /* The keyword should be either `new' or `delete'. */
14681 if (token->keyword == RID_NEW)
14682 op = NEW_EXPR;
14683 else if (token->keyword == RID_DELETE)
14684 op = DELETE_EXPR;
14685 else
14686 break;
14688 /* Consume the `new' or `delete' token. */
14689 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14691 /* Peek at the next token. */
14692 token = cp_lexer_peek_token (parser->lexer);
14693 /* If it's a `[' token then this is the array variant of the
14694 operator. */
14695 if (token->type == CPP_OPEN_SQUARE)
14697 /* Consume the `[' token. */
14698 cp_lexer_consume_token (parser->lexer);
14699 /* Look for the `]' token. */
14700 if (cp_token *close_token
14701 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14702 end_loc = close_token->location;
14703 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14705 start_loc = make_location (start_loc, start_loc, end_loc);
14706 consumed = true;
14707 break;
14710 case CPP_PLUS:
14711 op = PLUS_EXPR;
14712 break;
14714 case CPP_MINUS:
14715 op = MINUS_EXPR;
14716 break;
14718 case CPP_MULT:
14719 op = MULT_EXPR;
14720 break;
14722 case CPP_DIV:
14723 op = TRUNC_DIV_EXPR;
14724 break;
14726 case CPP_MOD:
14727 op = TRUNC_MOD_EXPR;
14728 break;
14730 case CPP_XOR:
14731 op = BIT_XOR_EXPR;
14732 break;
14734 case CPP_AND:
14735 op = BIT_AND_EXPR;
14736 break;
14738 case CPP_OR:
14739 op = BIT_IOR_EXPR;
14740 break;
14742 case CPP_COMPL:
14743 op = BIT_NOT_EXPR;
14744 break;
14746 case CPP_NOT:
14747 op = TRUTH_NOT_EXPR;
14748 break;
14750 case CPP_EQ:
14751 assop = true;
14752 op = NOP_EXPR;
14753 break;
14755 case CPP_LESS:
14756 op = LT_EXPR;
14757 break;
14759 case CPP_GREATER:
14760 op = GT_EXPR;
14761 break;
14763 case CPP_PLUS_EQ:
14764 assop = true;
14765 op = PLUS_EXPR;
14766 break;
14768 case CPP_MINUS_EQ:
14769 assop = true;
14770 op = MINUS_EXPR;
14771 break;
14773 case CPP_MULT_EQ:
14774 assop = true;
14775 op = MULT_EXPR;
14776 break;
14778 case CPP_DIV_EQ:
14779 assop = true;
14780 op = TRUNC_DIV_EXPR;
14781 break;
14783 case CPP_MOD_EQ:
14784 assop = true;
14785 op = TRUNC_MOD_EXPR;
14786 break;
14788 case CPP_XOR_EQ:
14789 assop = true;
14790 op = BIT_XOR_EXPR;
14791 break;
14793 case CPP_AND_EQ:
14794 assop = true;
14795 op = BIT_AND_EXPR;
14796 break;
14798 case CPP_OR_EQ:
14799 assop = true;
14800 op = BIT_IOR_EXPR;
14801 break;
14803 case CPP_LSHIFT:
14804 op = LSHIFT_EXPR;
14805 break;
14807 case CPP_RSHIFT:
14808 op = RSHIFT_EXPR;
14809 break;
14811 case CPP_LSHIFT_EQ:
14812 assop = true;
14813 op = LSHIFT_EXPR;
14814 break;
14816 case CPP_RSHIFT_EQ:
14817 assop = true;
14818 op = RSHIFT_EXPR;
14819 break;
14821 case CPP_EQ_EQ:
14822 op = EQ_EXPR;
14823 break;
14825 case CPP_NOT_EQ:
14826 op = NE_EXPR;
14827 break;
14829 case CPP_LESS_EQ:
14830 op = LE_EXPR;
14831 break;
14833 case CPP_GREATER_EQ:
14834 op = GE_EXPR;
14835 break;
14837 case CPP_AND_AND:
14838 op = TRUTH_ANDIF_EXPR;
14839 break;
14841 case CPP_OR_OR:
14842 op = TRUTH_ORIF_EXPR;
14843 break;
14845 case CPP_PLUS_PLUS:
14846 op = POSTINCREMENT_EXPR;
14847 break;
14849 case CPP_MINUS_MINUS:
14850 op = PREDECREMENT_EXPR;
14851 break;
14853 case CPP_COMMA:
14854 op = COMPOUND_EXPR;
14855 break;
14857 case CPP_DEREF_STAR:
14858 op = MEMBER_REF;
14859 break;
14861 case CPP_DEREF:
14862 op = COMPONENT_REF;
14863 break;
14865 case CPP_OPEN_PAREN:
14867 /* Consume the `('. */
14868 matching_parens parens;
14869 parens.consume_open (parser);
14870 /* Look for the matching `)'. */
14871 parens.require_close (parser);
14872 op = CALL_EXPR;
14873 consumed = true;
14874 break;
14877 case CPP_OPEN_SQUARE:
14878 /* Consume the `['. */
14879 cp_lexer_consume_token (parser->lexer);
14880 /* Look for the matching `]'. */
14881 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14882 op = ARRAY_REF;
14883 consumed = true;
14884 break;
14886 case CPP_UTF8STRING:
14887 case CPP_UTF8STRING_USERDEF:
14888 utf8 = true;
14889 /* FALLTHRU */
14890 case CPP_STRING:
14891 case CPP_WSTRING:
14892 case CPP_STRING16:
14893 case CPP_STRING32:
14894 case CPP_STRING_USERDEF:
14895 case CPP_WSTRING_USERDEF:
14896 case CPP_STRING16_USERDEF:
14897 case CPP_STRING32_USERDEF:
14899 tree str, string_tree;
14900 int sz, len;
14902 if (cxx_dialect == cxx98)
14903 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14905 /* Consume the string. */
14906 str = cp_parser_string_literal (parser, /*translate=*/true,
14907 /*wide_ok=*/true, /*lookup_udlit=*/false);
14908 if (str == error_mark_node)
14909 return error_mark_node;
14910 else if (TREE_CODE (str) == USERDEF_LITERAL)
14912 string_tree = USERDEF_LITERAL_VALUE (str);
14913 id = USERDEF_LITERAL_SUFFIX_ID (str);
14915 else
14917 string_tree = str;
14918 /* Look for the suffix identifier. */
14919 token = cp_lexer_peek_token (parser->lexer);
14920 if (token->type == CPP_NAME)
14921 id = cp_parser_identifier (parser);
14922 else if (token->type == CPP_KEYWORD)
14924 error ("unexpected keyword;"
14925 " remove space between quotes and suffix identifier");
14926 return error_mark_node;
14928 else
14930 error ("expected suffix identifier");
14931 return error_mark_node;
14934 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14935 (TREE_TYPE (TREE_TYPE (string_tree))));
14936 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14937 if (len != 0)
14939 error ("expected empty string after %<operator%> keyword");
14940 return error_mark_node;
14942 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14943 != char_type_node)
14945 error ("invalid encoding prefix in literal operator");
14946 return error_mark_node;
14948 if (id != error_mark_node)
14950 const char *name = IDENTIFIER_POINTER (id);
14951 id = cp_literal_operator_id (name);
14953 return id;
14956 default:
14957 /* Anything else is an error. */
14958 break;
14961 /* If we have selected an identifier, we need to consume the
14962 operator token. */
14963 if (op != ERROR_MARK)
14965 id = ovl_op_identifier (assop, op);
14966 if (!consumed)
14967 cp_lexer_consume_token (parser->lexer);
14969 /* Otherwise, no valid operator name was present. */
14970 else
14972 cp_parser_error (parser, "expected operator");
14973 id = error_mark_node;
14976 return cp_expr (id, start_loc);
14979 /* Parse a template-declaration.
14981 template-declaration:
14982 export [opt] template < template-parameter-list > declaration
14984 If MEMBER_P is TRUE, this template-declaration occurs within a
14985 class-specifier.
14987 The grammar rule given by the standard isn't correct. What
14988 is really meant is:
14990 template-declaration:
14991 export [opt] template-parameter-list-seq
14992 decl-specifier-seq [opt] init-declarator [opt] ;
14993 export [opt] template-parameter-list-seq
14994 function-definition
14996 template-parameter-list-seq:
14997 template-parameter-list-seq [opt]
14998 template < template-parameter-list >
15000 Concept Extensions:
15002 template-parameter-list-seq:
15003 template < template-parameter-list > requires-clause [opt]
15005 requires-clause:
15006 requires logical-or-expression */
15008 static void
15009 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15011 /* Check for `export'. */
15012 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15014 /* Consume the `export' token. */
15015 cp_lexer_consume_token (parser->lexer);
15016 /* Warn that we do not support `export'. */
15017 warning (0, "keyword %<export%> not implemented, and will be ignored");
15020 cp_parser_template_declaration_after_export (parser, member_p);
15023 /* Parse a template-parameter-list.
15025 template-parameter-list:
15026 template-parameter
15027 template-parameter-list , template-parameter
15029 Returns a TREE_LIST. Each node represents a template parameter.
15030 The nodes are connected via their TREE_CHAINs. */
15032 static tree
15033 cp_parser_template_parameter_list (cp_parser* parser)
15035 tree parameter_list = NULL_TREE;
15037 begin_template_parm_list ();
15039 /* The loop below parses the template parms. We first need to know
15040 the total number of template parms to be able to compute proper
15041 canonical types of each dependent type. So after the loop, when
15042 we know the total number of template parms,
15043 end_template_parm_list computes the proper canonical types and
15044 fixes up the dependent types accordingly. */
15045 while (true)
15047 tree parameter;
15048 bool is_non_type;
15049 bool is_parameter_pack;
15050 location_t parm_loc;
15052 /* Parse the template-parameter. */
15053 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15054 parameter = cp_parser_template_parameter (parser,
15055 &is_non_type,
15056 &is_parameter_pack);
15057 /* Add it to the list. */
15058 if (parameter != error_mark_node)
15059 parameter_list = process_template_parm (parameter_list,
15060 parm_loc,
15061 parameter,
15062 is_non_type,
15063 is_parameter_pack);
15064 else
15066 tree err_parm = build_tree_list (parameter, parameter);
15067 parameter_list = chainon (parameter_list, err_parm);
15070 /* If the next token is not a `,', we're done. */
15071 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15072 break;
15073 /* Otherwise, consume the `,' token. */
15074 cp_lexer_consume_token (parser->lexer);
15077 return end_template_parm_list (parameter_list);
15080 /* Parse a introduction-list.
15082 introduction-list:
15083 introduced-parameter
15084 introduction-list , introduced-parameter
15086 introduced-parameter:
15087 ...[opt] identifier
15089 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15090 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15091 WILDCARD_DECL will also have DECL_NAME set and token location in
15092 DECL_SOURCE_LOCATION. */
15094 static tree
15095 cp_parser_introduction_list (cp_parser *parser)
15097 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15099 while (true)
15101 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15102 if (is_pack)
15103 cp_lexer_consume_token (parser->lexer);
15105 /* Build placeholder. */
15106 tree parm = build_nt (WILDCARD_DECL);
15107 DECL_SOURCE_LOCATION (parm)
15108 = cp_lexer_peek_token (parser->lexer)->location;
15109 DECL_NAME (parm) = cp_parser_identifier (parser);
15110 WILDCARD_PACK_P (parm) = is_pack;
15111 vec_safe_push (introduction_vec, parm);
15113 /* If the next token is not a `,', we're done. */
15114 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15115 break;
15116 /* Otherwise, consume the `,' token. */
15117 cp_lexer_consume_token (parser->lexer);
15120 /* Convert the vec into a TREE_VEC. */
15121 tree introduction_list = make_tree_vec (introduction_vec->length ());
15122 unsigned int n;
15123 tree parm;
15124 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15125 TREE_VEC_ELT (introduction_list, n) = parm;
15127 release_tree_vector (introduction_vec);
15128 return introduction_list;
15131 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15132 is an abstract declarator. */
15134 static inline cp_declarator*
15135 get_id_declarator (cp_declarator *declarator)
15137 cp_declarator *d = declarator;
15138 while (d && d->kind != cdk_id)
15139 d = d->declarator;
15140 return d;
15143 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15144 is an abstract declarator. */
15146 static inline tree
15147 get_unqualified_id (cp_declarator *declarator)
15149 declarator = get_id_declarator (declarator);
15150 if (declarator)
15151 return declarator->u.id.unqualified_name;
15152 else
15153 return NULL_TREE;
15156 /* Returns true if DECL represents a constrained-parameter. */
15158 static inline bool
15159 is_constrained_parameter (tree decl)
15161 return (decl
15162 && TREE_CODE (decl) == TYPE_DECL
15163 && CONSTRAINED_PARM_CONCEPT (decl)
15164 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15167 /* Returns true if PARM declares a constrained-parameter. */
15169 static inline bool
15170 is_constrained_parameter (cp_parameter_declarator *parm)
15172 return is_constrained_parameter (parm->decl_specifiers.type);
15175 /* Check that the type parameter is only a declarator-id, and that its
15176 type is not cv-qualified. */
15178 bool
15179 cp_parser_check_constrained_type_parm (cp_parser *parser,
15180 cp_parameter_declarator *parm)
15182 if (!parm->declarator)
15183 return true;
15185 if (parm->declarator->kind != cdk_id)
15187 cp_parser_error (parser, "invalid constrained type parameter");
15188 return false;
15191 /* Don't allow cv-qualified type parameters. */
15192 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15193 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15195 cp_parser_error (parser, "cv-qualified type parameter");
15196 return false;
15199 return true;
15202 /* Finish parsing/processing a template type parameter and checking
15203 various restrictions. */
15205 static inline tree
15206 cp_parser_constrained_type_template_parm (cp_parser *parser,
15207 tree id,
15208 cp_parameter_declarator* parmdecl)
15210 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15211 return finish_template_type_parm (class_type_node, id);
15212 else
15213 return error_mark_node;
15216 static tree
15217 finish_constrained_template_template_parm (tree proto, tree id)
15219 /* FIXME: This should probably be copied, and we may need to adjust
15220 the template parameter depths. */
15221 tree saved_parms = current_template_parms;
15222 begin_template_parm_list ();
15223 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15224 end_template_parm_list ();
15226 tree parm = finish_template_template_parm (class_type_node, id);
15227 current_template_parms = saved_parms;
15229 return parm;
15232 /* Finish parsing/processing a template template parameter by borrowing
15233 the template parameter list from the prototype parameter. */
15235 static tree
15236 cp_parser_constrained_template_template_parm (cp_parser *parser,
15237 tree proto,
15238 tree id,
15239 cp_parameter_declarator *parmdecl)
15241 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15242 return error_mark_node;
15243 return finish_constrained_template_template_parm (proto, id);
15246 /* Create a new non-type template parameter from the given PARM
15247 declarator. */
15249 static tree
15250 constrained_non_type_template_parm (bool *is_non_type,
15251 cp_parameter_declarator *parm)
15253 *is_non_type = true;
15254 cp_declarator *decl = parm->declarator;
15255 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15256 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15257 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15260 /* Build a constrained template parameter based on the PARMDECL
15261 declarator. The type of PARMDECL is the constrained type, which
15262 refers to the prototype template parameter that ultimately
15263 specifies the type of the declared parameter. */
15265 static tree
15266 finish_constrained_parameter (cp_parser *parser,
15267 cp_parameter_declarator *parmdecl,
15268 bool *is_non_type,
15269 bool *is_parameter_pack)
15271 tree decl = parmdecl->decl_specifiers.type;
15272 tree id = get_unqualified_id (parmdecl->declarator);
15273 tree def = parmdecl->default_argument;
15274 tree proto = DECL_INITIAL (decl);
15276 /* A template parameter constrained by a variadic concept shall also
15277 be declared as a template parameter pack. */
15278 bool is_variadic = template_parameter_pack_p (proto);
15279 if (is_variadic && !*is_parameter_pack)
15280 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15282 /* Build the parameter. Return an error if the declarator was invalid. */
15283 tree parm;
15284 if (TREE_CODE (proto) == TYPE_DECL)
15285 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15286 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15287 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15288 parmdecl);
15289 else
15290 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15291 if (parm == error_mark_node)
15292 return error_mark_node;
15294 /* Finish the parameter decl and create a node attaching the
15295 default argument and constraint. */
15296 parm = build_tree_list (def, parm);
15297 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15299 return parm;
15302 /* Returns true if the parsed type actually represents the declaration
15303 of a type template-parameter. */
15305 static inline bool
15306 declares_constrained_type_template_parameter (tree type)
15308 return (is_constrained_parameter (type)
15309 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15313 /* Returns true if the parsed type actually represents the declaration of
15314 a template template-parameter. */
15316 static bool
15317 declares_constrained_template_template_parameter (tree type)
15319 return (is_constrained_parameter (type)
15320 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15323 /* Parse a default argument for a type template-parameter.
15324 Note that diagnostics are handled in cp_parser_template_parameter. */
15326 static tree
15327 cp_parser_default_type_template_argument (cp_parser *parser)
15329 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15331 /* Consume the `=' token. */
15332 cp_lexer_consume_token (parser->lexer);
15334 cp_token *token = cp_lexer_peek_token (parser->lexer);
15336 /* Parse the default-argument. */
15337 push_deferring_access_checks (dk_no_deferred);
15338 tree default_argument = cp_parser_type_id (parser);
15339 pop_deferring_access_checks ();
15341 if (flag_concepts && type_uses_auto (default_argument))
15343 error_at (token->location,
15344 "invalid use of %<auto%> in default template argument");
15345 return error_mark_node;
15348 return default_argument;
15351 /* Parse a default argument for a template template-parameter. */
15353 static tree
15354 cp_parser_default_template_template_argument (cp_parser *parser)
15356 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15358 bool is_template;
15360 /* Consume the `='. */
15361 cp_lexer_consume_token (parser->lexer);
15362 /* Parse the id-expression. */
15363 push_deferring_access_checks (dk_no_deferred);
15364 /* save token before parsing the id-expression, for error
15365 reporting */
15366 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15367 tree default_argument
15368 = cp_parser_id_expression (parser,
15369 /*template_keyword_p=*/false,
15370 /*check_dependency_p=*/true,
15371 /*template_p=*/&is_template,
15372 /*declarator_p=*/false,
15373 /*optional_p=*/false);
15374 if (TREE_CODE (default_argument) == TYPE_DECL)
15375 /* If the id-expression was a template-id that refers to
15376 a template-class, we already have the declaration here,
15377 so no further lookup is needed. */
15379 else
15380 /* Look up the name. */
15381 default_argument
15382 = cp_parser_lookup_name (parser, default_argument,
15383 none_type,
15384 /*is_template=*/is_template,
15385 /*is_namespace=*/false,
15386 /*check_dependency=*/true,
15387 /*ambiguous_decls=*/NULL,
15388 token->location);
15389 /* See if the default argument is valid. */
15390 default_argument = check_template_template_default_arg (default_argument);
15391 pop_deferring_access_checks ();
15392 return default_argument;
15395 /* Parse a template-parameter.
15397 template-parameter:
15398 type-parameter
15399 parameter-declaration
15401 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15402 the parameter. The TREE_PURPOSE is the default value, if any.
15403 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15404 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15405 set to true iff this parameter is a parameter pack. */
15407 static tree
15408 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15409 bool *is_parameter_pack)
15411 cp_token *token;
15412 cp_parameter_declarator *parameter_declarator;
15413 tree parm;
15415 /* Assume it is a type parameter or a template parameter. */
15416 *is_non_type = false;
15417 /* Assume it not a parameter pack. */
15418 *is_parameter_pack = false;
15419 /* Peek at the next token. */
15420 token = cp_lexer_peek_token (parser->lexer);
15421 /* If it is `template', we have a type-parameter. */
15422 if (token->keyword == RID_TEMPLATE)
15423 return cp_parser_type_parameter (parser, is_parameter_pack);
15424 /* If it is `class' or `typename' we do not know yet whether it is a
15425 type parameter or a non-type parameter. Consider:
15427 template <typename T, typename T::X X> ...
15431 template <class C, class D*> ...
15433 Here, the first parameter is a type parameter, and the second is
15434 a non-type parameter. We can tell by looking at the token after
15435 the identifier -- if it is a `,', `=', or `>' then we have a type
15436 parameter. */
15437 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15439 /* Peek at the token after `class' or `typename'. */
15440 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15441 /* If it's an ellipsis, we have a template type parameter
15442 pack. */
15443 if (token->type == CPP_ELLIPSIS)
15444 return cp_parser_type_parameter (parser, is_parameter_pack);
15445 /* If it's an identifier, skip it. */
15446 if (token->type == CPP_NAME)
15447 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15448 /* Now, see if the token looks like the end of a template
15449 parameter. */
15450 if (token->type == CPP_COMMA
15451 || token->type == CPP_EQ
15452 || token->type == CPP_GREATER)
15453 return cp_parser_type_parameter (parser, is_parameter_pack);
15456 /* Otherwise, it is a non-type parameter or a constrained parameter.
15458 [temp.param]
15460 When parsing a default template-argument for a non-type
15461 template-parameter, the first non-nested `>' is taken as the end
15462 of the template parameter-list rather than a greater-than
15463 operator. */
15464 parameter_declarator
15465 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15466 /*parenthesized_p=*/NULL);
15468 if (!parameter_declarator)
15469 return error_mark_node;
15471 /* If the parameter declaration is marked as a parameter pack, set
15472 *IS_PARAMETER_PACK to notify the caller. */
15473 if (parameter_declarator->template_parameter_pack_p)
15474 *is_parameter_pack = true;
15476 if (parameter_declarator->default_argument)
15478 /* Can happen in some cases of erroneous input (c++/34892). */
15479 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15480 /* Consume the `...' for better error recovery. */
15481 cp_lexer_consume_token (parser->lexer);
15484 // The parameter may have been constrained.
15485 if (is_constrained_parameter (parameter_declarator))
15486 return finish_constrained_parameter (parser,
15487 parameter_declarator,
15488 is_non_type,
15489 is_parameter_pack);
15491 // Now we're sure that the parameter is a non-type parameter.
15492 *is_non_type = true;
15494 parm = grokdeclarator (parameter_declarator->declarator,
15495 &parameter_declarator->decl_specifiers,
15496 TPARM, /*initialized=*/0,
15497 /*attrlist=*/NULL);
15498 if (parm == error_mark_node)
15499 return error_mark_node;
15501 return build_tree_list (parameter_declarator->default_argument, parm);
15504 /* Parse a type-parameter.
15506 type-parameter:
15507 class identifier [opt]
15508 class identifier [opt] = type-id
15509 typename identifier [opt]
15510 typename identifier [opt] = type-id
15511 template < template-parameter-list > class identifier [opt]
15512 template < template-parameter-list > class identifier [opt]
15513 = id-expression
15515 GNU Extension (variadic templates):
15517 type-parameter:
15518 class ... identifier [opt]
15519 typename ... identifier [opt]
15521 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15522 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15523 the declaration of the parameter.
15525 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15527 static tree
15528 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15530 cp_token *token;
15531 tree parameter;
15533 /* Look for a keyword to tell us what kind of parameter this is. */
15534 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15535 if (!token)
15536 return error_mark_node;
15538 switch (token->keyword)
15540 case RID_CLASS:
15541 case RID_TYPENAME:
15543 tree identifier;
15544 tree default_argument;
15546 /* If the next token is an ellipsis, we have a template
15547 argument pack. */
15548 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15550 /* Consume the `...' token. */
15551 cp_lexer_consume_token (parser->lexer);
15552 maybe_warn_variadic_templates ();
15554 *is_parameter_pack = true;
15557 /* If the next token is an identifier, then it names the
15558 parameter. */
15559 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15560 identifier = cp_parser_identifier (parser);
15561 else
15562 identifier = NULL_TREE;
15564 /* Create the parameter. */
15565 parameter = finish_template_type_parm (class_type_node, identifier);
15567 /* If the next token is an `=', we have a default argument. */
15568 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15570 default_argument
15571 = cp_parser_default_type_template_argument (parser);
15573 /* Template parameter packs cannot have default
15574 arguments. */
15575 if (*is_parameter_pack)
15577 if (identifier)
15578 error_at (token->location,
15579 "template parameter pack %qD cannot have a "
15580 "default argument", identifier);
15581 else
15582 error_at (token->location,
15583 "template parameter packs cannot have "
15584 "default arguments");
15585 default_argument = NULL_TREE;
15587 else if (check_for_bare_parameter_packs (default_argument))
15588 default_argument = error_mark_node;
15590 else
15591 default_argument = NULL_TREE;
15593 /* Create the combined representation of the parameter and the
15594 default argument. */
15595 parameter = build_tree_list (default_argument, parameter);
15597 break;
15599 case RID_TEMPLATE:
15601 tree identifier;
15602 tree default_argument;
15604 /* Look for the `<'. */
15605 cp_parser_require (parser, CPP_LESS, RT_LESS);
15606 /* Parse the template-parameter-list. */
15607 cp_parser_template_parameter_list (parser);
15608 /* Look for the `>'. */
15609 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15611 // If template requirements are present, parse them.
15612 if (flag_concepts)
15614 tree reqs = get_shorthand_constraints (current_template_parms);
15615 if (tree r = cp_parser_requires_clause_opt (parser))
15616 reqs = conjoin_constraints (reqs, normalize_expression (r));
15617 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15620 /* Look for the `class' or 'typename' keywords. */
15621 cp_parser_type_parameter_key (parser);
15622 /* If the next token is an ellipsis, we have a template
15623 argument pack. */
15624 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15626 /* Consume the `...' token. */
15627 cp_lexer_consume_token (parser->lexer);
15628 maybe_warn_variadic_templates ();
15630 *is_parameter_pack = true;
15632 /* If the next token is an `=', then there is a
15633 default-argument. If the next token is a `>', we are at
15634 the end of the parameter-list. If the next token is a `,',
15635 then we are at the end of this parameter. */
15636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15637 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15638 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15640 identifier = cp_parser_identifier (parser);
15641 /* Treat invalid names as if the parameter were nameless. */
15642 if (identifier == error_mark_node)
15643 identifier = NULL_TREE;
15645 else
15646 identifier = NULL_TREE;
15648 /* Create the template parameter. */
15649 parameter = finish_template_template_parm (class_type_node,
15650 identifier);
15652 /* If the next token is an `=', then there is a
15653 default-argument. */
15654 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15656 default_argument
15657 = cp_parser_default_template_template_argument (parser);
15659 /* Template parameter packs cannot have default
15660 arguments. */
15661 if (*is_parameter_pack)
15663 if (identifier)
15664 error_at (token->location,
15665 "template parameter pack %qD cannot "
15666 "have a default argument",
15667 identifier);
15668 else
15669 error_at (token->location, "template parameter packs cannot "
15670 "have default arguments");
15671 default_argument = NULL_TREE;
15674 else
15675 default_argument = NULL_TREE;
15677 /* Create the combined representation of the parameter and the
15678 default argument. */
15679 parameter = build_tree_list (default_argument, parameter);
15681 break;
15683 default:
15684 gcc_unreachable ();
15685 break;
15688 return parameter;
15691 /* Parse a template-id.
15693 template-id:
15694 template-name < template-argument-list [opt] >
15696 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15697 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15698 returned. Otherwise, if the template-name names a function, or set
15699 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15700 names a class, returns a TYPE_DECL for the specialization.
15702 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15703 uninstantiated templates. */
15705 static tree
15706 cp_parser_template_id (cp_parser *parser,
15707 bool template_keyword_p,
15708 bool check_dependency_p,
15709 enum tag_types tag_type,
15710 bool is_declaration)
15712 tree templ;
15713 tree arguments;
15714 tree template_id;
15715 cp_token_position start_of_id = 0;
15716 cp_token *next_token = NULL, *next_token_2 = NULL;
15717 bool is_identifier;
15719 /* If the next token corresponds to a template-id, there is no need
15720 to reparse it. */
15721 cp_token *token = cp_lexer_peek_token (parser->lexer);
15722 if (token->type == CPP_TEMPLATE_ID)
15724 cp_lexer_consume_token (parser->lexer);
15725 return saved_checks_value (token->u.tree_check_value);
15728 /* Avoid performing name lookup if there is no possibility of
15729 finding a template-id. */
15730 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15731 || (token->type == CPP_NAME
15732 && !cp_parser_nth_token_starts_template_argument_list_p
15733 (parser, 2)))
15735 cp_parser_error (parser, "expected template-id");
15736 return error_mark_node;
15739 /* Remember where the template-id starts. */
15740 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15741 start_of_id = cp_lexer_token_position (parser->lexer, false);
15743 push_deferring_access_checks (dk_deferred);
15745 /* Parse the template-name. */
15746 is_identifier = false;
15747 templ = cp_parser_template_name (parser, template_keyword_p,
15748 check_dependency_p,
15749 is_declaration,
15750 tag_type,
15751 &is_identifier);
15752 if (templ == error_mark_node || is_identifier)
15754 pop_deferring_access_checks ();
15755 return templ;
15758 /* Since we're going to preserve any side-effects from this parse, set up a
15759 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15760 in the template arguments. */
15761 tentative_firewall firewall (parser);
15763 /* If we find the sequence `[:' after a template-name, it's probably
15764 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15765 parse correctly the argument list. */
15766 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15767 == CPP_OPEN_SQUARE)
15768 && next_token->flags & DIGRAPH
15769 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15770 == CPP_COLON)
15771 && !(next_token_2->flags & PREV_WHITE))
15773 cp_parser_parse_tentatively (parser);
15774 /* Change `:' into `::'. */
15775 next_token_2->type = CPP_SCOPE;
15776 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15777 CPP_LESS. */
15778 cp_lexer_consume_token (parser->lexer);
15780 /* Parse the arguments. */
15781 arguments = cp_parser_enclosed_template_argument_list (parser);
15782 if (!cp_parser_parse_definitely (parser))
15784 /* If we couldn't parse an argument list, then we revert our changes
15785 and return simply an error. Maybe this is not a template-id
15786 after all. */
15787 next_token_2->type = CPP_COLON;
15788 cp_parser_error (parser, "expected %<<%>");
15789 pop_deferring_access_checks ();
15790 return error_mark_node;
15792 /* Otherwise, emit an error about the invalid digraph, but continue
15793 parsing because we got our argument list. */
15794 if (permerror (next_token->location,
15795 "%<<::%> cannot begin a template-argument list"))
15797 static bool hint = false;
15798 inform (next_token->location,
15799 "%<<:%> is an alternate spelling for %<[%>."
15800 " Insert whitespace between %<<%> and %<::%>");
15801 if (!hint && !flag_permissive)
15803 inform (next_token->location, "(if you use %<-fpermissive%> "
15804 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15805 "accept your code)");
15806 hint = true;
15810 else
15812 /* Look for the `<' that starts the template-argument-list. */
15813 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15815 pop_deferring_access_checks ();
15816 return error_mark_node;
15818 /* Parse the arguments. */
15819 arguments = cp_parser_enclosed_template_argument_list (parser);
15822 /* Set the location to be of the form:
15823 template-name < template-argument-list [opt] >
15824 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15825 with caret == start at the start of the template-name,
15826 ranging until the closing '>'. */
15827 location_t finish_loc
15828 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15829 location_t combined_loc
15830 = make_location (token->location, token->location, finish_loc);
15832 /* Build a representation of the specialization. */
15833 if (identifier_p (templ))
15834 template_id = build_min_nt_loc (combined_loc,
15835 TEMPLATE_ID_EXPR,
15836 templ, arguments);
15837 else if (DECL_TYPE_TEMPLATE_P (templ)
15838 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15840 bool entering_scope;
15841 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15842 template (rather than some instantiation thereof) only if
15843 is not nested within some other construct. For example, in
15844 "template <typename T> void f(T) { A<T>::", A<T> is just an
15845 instantiation of A. */
15846 entering_scope = (template_parm_scope_p ()
15847 && cp_lexer_next_token_is (parser->lexer,
15848 CPP_SCOPE));
15849 template_id
15850 = finish_template_type (templ, arguments, entering_scope);
15852 /* A template-like identifier may be a partial concept id. */
15853 else if (flag_concepts
15854 && (template_id = (cp_parser_maybe_partial_concept_id
15855 (parser, templ, arguments))))
15856 return template_id;
15857 else if (variable_template_p (templ))
15859 template_id = lookup_template_variable (templ, arguments);
15860 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15861 SET_EXPR_LOCATION (template_id, combined_loc);
15863 else
15865 /* If it's not a class-template or a template-template, it should be
15866 a function-template. */
15867 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15868 || TREE_CODE (templ) == OVERLOAD
15869 || BASELINK_P (templ)));
15871 template_id = lookup_template_function (templ, arguments);
15872 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15873 SET_EXPR_LOCATION (template_id, combined_loc);
15876 /* If parsing tentatively, replace the sequence of tokens that makes
15877 up the template-id with a CPP_TEMPLATE_ID token. That way,
15878 should we re-parse the token stream, we will not have to repeat
15879 the effort required to do the parse, nor will we issue duplicate
15880 error messages about problems during instantiation of the
15881 template. */
15882 if (start_of_id
15883 /* Don't do this if we had a parse error in a declarator; re-parsing
15884 might succeed if a name changes meaning (60361). */
15885 && !(cp_parser_error_occurred (parser)
15886 && cp_parser_parsing_tentatively (parser)
15887 && parser->in_declarator_p))
15889 /* Reset the contents of the START_OF_ID token. */
15890 token->type = CPP_TEMPLATE_ID;
15891 token->location = combined_loc;
15893 /* We must mark the lookup as kept, so we don't throw it away on
15894 the first parse. */
15895 if (is_overloaded_fn (template_id))
15896 lookup_keep (get_fns (template_id), true);
15898 /* Retrieve any deferred checks. Do not pop this access checks yet
15899 so the memory will not be reclaimed during token replacing below. */
15900 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15901 token->u.tree_check_value->value = template_id;
15902 token->u.tree_check_value->checks = get_deferred_access_checks ();
15903 token->keyword = RID_MAX;
15905 /* Purge all subsequent tokens. */
15906 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15908 /* ??? Can we actually assume that, if template_id ==
15909 error_mark_node, we will have issued a diagnostic to the
15910 user, as opposed to simply marking the tentative parse as
15911 failed? */
15912 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15913 error_at (token->location, "parse error in template argument list");
15916 pop_to_parent_deferring_access_checks ();
15917 return template_id;
15920 /* Parse a template-name.
15922 template-name:
15923 identifier
15925 The standard should actually say:
15927 template-name:
15928 identifier
15929 operator-function-id
15931 A defect report has been filed about this issue.
15933 A conversion-function-id cannot be a template name because they cannot
15934 be part of a template-id. In fact, looking at this code:
15936 a.operator K<int>()
15938 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15939 It is impossible to call a templated conversion-function-id with an
15940 explicit argument list, since the only allowed template parameter is
15941 the type to which it is converting.
15943 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15944 `template' keyword, in a construction like:
15946 T::template f<3>()
15948 In that case `f' is taken to be a template-name, even though there
15949 is no way of knowing for sure.
15951 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15952 name refers to a set of overloaded functions, at least one of which
15953 is a template, or an IDENTIFIER_NODE with the name of the template,
15954 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15955 names are looked up inside uninstantiated templates. */
15957 static tree
15958 cp_parser_template_name (cp_parser* parser,
15959 bool template_keyword_p,
15960 bool check_dependency_p,
15961 bool is_declaration,
15962 enum tag_types tag_type,
15963 bool *is_identifier)
15965 tree identifier;
15966 tree decl;
15967 cp_token *token = cp_lexer_peek_token (parser->lexer);
15969 /* If the next token is `operator', then we have either an
15970 operator-function-id or a conversion-function-id. */
15971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15973 /* We don't know whether we're looking at an
15974 operator-function-id or a conversion-function-id. */
15975 cp_parser_parse_tentatively (parser);
15976 /* Try an operator-function-id. */
15977 identifier = cp_parser_operator_function_id (parser);
15978 /* If that didn't work, try a conversion-function-id. */
15979 if (!cp_parser_parse_definitely (parser))
15981 cp_parser_error (parser, "expected template-name");
15982 return error_mark_node;
15985 /* Look for the identifier. */
15986 else
15987 identifier = cp_parser_identifier (parser);
15989 /* If we didn't find an identifier, we don't have a template-id. */
15990 if (identifier == error_mark_node)
15991 return error_mark_node;
15993 /* If the name immediately followed the `template' keyword, then it
15994 is a template-name. However, if the next token is not `<', then
15995 we do not treat it as a template-name, since it is not being used
15996 as part of a template-id. This enables us to handle constructs
15997 like:
15999 template <typename T> struct S { S(); };
16000 template <typename T> S<T>::S();
16002 correctly. We would treat `S' as a template -- if it were `S<T>'
16003 -- but we do not if there is no `<'. */
16005 if (processing_template_decl
16006 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16008 /* In a declaration, in a dependent context, we pretend that the
16009 "template" keyword was present in order to improve error
16010 recovery. For example, given:
16012 template <typename T> void f(T::X<int>);
16014 we want to treat "X<int>" as a template-id. */
16015 if (is_declaration
16016 && !template_keyword_p
16017 && parser->scope && TYPE_P (parser->scope)
16018 && check_dependency_p
16019 && dependent_scope_p (parser->scope)
16020 /* Do not do this for dtors (or ctors), since they never
16021 need the template keyword before their name. */
16022 && !constructor_name_p (identifier, parser->scope))
16024 cp_token_position start = 0;
16026 /* Explain what went wrong. */
16027 error_at (token->location, "non-template %qD used as template",
16028 identifier);
16029 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16030 parser->scope, identifier);
16031 /* If parsing tentatively, find the location of the "<" token. */
16032 if (cp_parser_simulate_error (parser))
16033 start = cp_lexer_token_position (parser->lexer, true);
16034 /* Parse the template arguments so that we can issue error
16035 messages about them. */
16036 cp_lexer_consume_token (parser->lexer);
16037 cp_parser_enclosed_template_argument_list (parser);
16038 /* Skip tokens until we find a good place from which to
16039 continue parsing. */
16040 cp_parser_skip_to_closing_parenthesis (parser,
16041 /*recovering=*/true,
16042 /*or_comma=*/true,
16043 /*consume_paren=*/false);
16044 /* If parsing tentatively, permanently remove the
16045 template argument list. That will prevent duplicate
16046 error messages from being issued about the missing
16047 "template" keyword. */
16048 if (start)
16049 cp_lexer_purge_tokens_after (parser->lexer, start);
16050 if (is_identifier)
16051 *is_identifier = true;
16052 parser->context->object_type = NULL_TREE;
16053 return identifier;
16056 /* If the "template" keyword is present, then there is generally
16057 no point in doing name-lookup, so we just return IDENTIFIER.
16058 But, if the qualifying scope is non-dependent then we can
16059 (and must) do name-lookup normally. */
16060 if (template_keyword_p)
16062 tree scope = (parser->scope ? parser->scope
16063 : parser->context->object_type);
16064 if (scope && TYPE_P (scope)
16065 && (!CLASS_TYPE_P (scope)
16066 || (check_dependency_p && dependent_type_p (scope))))
16068 /* We're optimizing away the call to cp_parser_lookup_name, but
16069 we still need to do this. */
16070 parser->context->object_type = NULL_TREE;
16071 return identifier;
16076 /* Look up the name. */
16077 decl = cp_parser_lookup_name (parser, identifier,
16078 tag_type,
16079 /*is_template=*/true,
16080 /*is_namespace=*/false,
16081 check_dependency_p,
16082 /*ambiguous_decls=*/NULL,
16083 token->location);
16085 decl = strip_using_decl (decl);
16087 /* If DECL is a template, then the name was a template-name. */
16088 if (TREE_CODE (decl) == TEMPLATE_DECL)
16090 if (TREE_DEPRECATED (decl)
16091 && deprecated_state != DEPRECATED_SUPPRESS)
16092 warn_deprecated_use (decl, NULL_TREE);
16094 else
16096 /* The standard does not explicitly indicate whether a name that
16097 names a set of overloaded declarations, some of which are
16098 templates, is a template-name. However, such a name should
16099 be a template-name; otherwise, there is no way to form a
16100 template-id for the overloaded templates. */
16101 bool found = false;
16103 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16104 !found && iter; ++iter)
16105 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16106 found = true;
16108 if (!found)
16110 /* The name does not name a template. */
16111 cp_parser_error (parser, "expected template-name");
16112 return error_mark_node;
16116 /* If DECL is dependent, and refers to a function, then just return
16117 its name; we will look it up again during template instantiation. */
16118 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16120 tree scope = ovl_scope (decl);
16121 if (TYPE_P (scope) && dependent_type_p (scope))
16122 return identifier;
16125 return decl;
16128 /* Parse a template-argument-list.
16130 template-argument-list:
16131 template-argument ... [opt]
16132 template-argument-list , template-argument ... [opt]
16134 Returns a TREE_VEC containing the arguments. */
16136 static tree
16137 cp_parser_template_argument_list (cp_parser* parser)
16139 tree fixed_args[10];
16140 unsigned n_args = 0;
16141 unsigned alloced = 10;
16142 tree *arg_ary = fixed_args;
16143 tree vec;
16144 bool saved_in_template_argument_list_p;
16145 bool saved_ice_p;
16146 bool saved_non_ice_p;
16148 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16149 parser->in_template_argument_list_p = true;
16150 /* Even if the template-id appears in an integral
16151 constant-expression, the contents of the argument list do
16152 not. */
16153 saved_ice_p = parser->integral_constant_expression_p;
16154 parser->integral_constant_expression_p = false;
16155 saved_non_ice_p = parser->non_integral_constant_expression_p;
16156 parser->non_integral_constant_expression_p = false;
16158 /* Parse the arguments. */
16161 tree argument;
16163 if (n_args)
16164 /* Consume the comma. */
16165 cp_lexer_consume_token (parser->lexer);
16167 /* Parse the template-argument. */
16168 argument = cp_parser_template_argument (parser);
16170 /* If the next token is an ellipsis, we're expanding a template
16171 argument pack. */
16172 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16174 if (argument == error_mark_node)
16176 cp_token *token = cp_lexer_peek_token (parser->lexer);
16177 error_at (token->location,
16178 "expected parameter pack before %<...%>");
16180 /* Consume the `...' token. */
16181 cp_lexer_consume_token (parser->lexer);
16183 /* Make the argument into a TYPE_PACK_EXPANSION or
16184 EXPR_PACK_EXPANSION. */
16185 argument = make_pack_expansion (argument);
16188 if (n_args == alloced)
16190 alloced *= 2;
16192 if (arg_ary == fixed_args)
16194 arg_ary = XNEWVEC (tree, alloced);
16195 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16197 else
16198 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16200 arg_ary[n_args++] = argument;
16202 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16204 vec = make_tree_vec (n_args);
16206 while (n_args--)
16207 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16209 if (arg_ary != fixed_args)
16210 free (arg_ary);
16211 parser->non_integral_constant_expression_p = saved_non_ice_p;
16212 parser->integral_constant_expression_p = saved_ice_p;
16213 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16214 if (CHECKING_P)
16215 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16216 return vec;
16219 /* Parse a template-argument.
16221 template-argument:
16222 assignment-expression
16223 type-id
16224 id-expression
16226 The representation is that of an assignment-expression, type-id, or
16227 id-expression -- except that the qualified id-expression is
16228 evaluated, so that the value returned is either a DECL or an
16229 OVERLOAD.
16231 Although the standard says "assignment-expression", it forbids
16232 throw-expressions or assignments in the template argument.
16233 Therefore, we use "conditional-expression" instead. */
16235 static tree
16236 cp_parser_template_argument (cp_parser* parser)
16238 tree argument;
16239 bool template_p;
16240 bool address_p;
16241 bool maybe_type_id = false;
16242 cp_token *token = NULL, *argument_start_token = NULL;
16243 location_t loc = 0;
16244 cp_id_kind idk;
16246 /* There's really no way to know what we're looking at, so we just
16247 try each alternative in order.
16249 [temp.arg]
16251 In a template-argument, an ambiguity between a type-id and an
16252 expression is resolved to a type-id, regardless of the form of
16253 the corresponding template-parameter.
16255 Therefore, we try a type-id first. */
16256 cp_parser_parse_tentatively (parser);
16257 argument = cp_parser_template_type_arg (parser);
16258 /* If there was no error parsing the type-id but the next token is a
16259 '>>', our behavior depends on which dialect of C++ we're
16260 parsing. In C++98, we probably found a typo for '> >'. But there
16261 are type-id which are also valid expressions. For instance:
16263 struct X { int operator >> (int); };
16264 template <int V> struct Foo {};
16265 Foo<X () >> 5> r;
16267 Here 'X()' is a valid type-id of a function type, but the user just
16268 wanted to write the expression "X() >> 5". Thus, we remember that we
16269 found a valid type-id, but we still try to parse the argument as an
16270 expression to see what happens.
16272 In C++0x, the '>>' will be considered two separate '>'
16273 tokens. */
16274 if (!cp_parser_error_occurred (parser)
16275 && cxx_dialect == cxx98
16276 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16278 maybe_type_id = true;
16279 cp_parser_abort_tentative_parse (parser);
16281 else
16283 /* If the next token isn't a `,' or a `>', then this argument wasn't
16284 really finished. This means that the argument is not a valid
16285 type-id. */
16286 if (!cp_parser_next_token_ends_template_argument_p (parser))
16287 cp_parser_error (parser, "expected template-argument");
16288 /* If that worked, we're done. */
16289 if (cp_parser_parse_definitely (parser))
16290 return argument;
16292 /* We're still not sure what the argument will be. */
16293 cp_parser_parse_tentatively (parser);
16294 /* Try a template. */
16295 argument_start_token = cp_lexer_peek_token (parser->lexer);
16296 argument = cp_parser_id_expression (parser,
16297 /*template_keyword_p=*/false,
16298 /*check_dependency_p=*/true,
16299 &template_p,
16300 /*declarator_p=*/false,
16301 /*optional_p=*/false);
16302 /* If the next token isn't a `,' or a `>', then this argument wasn't
16303 really finished. */
16304 if (!cp_parser_next_token_ends_template_argument_p (parser))
16305 cp_parser_error (parser, "expected template-argument");
16306 if (!cp_parser_error_occurred (parser))
16308 /* Figure out what is being referred to. If the id-expression
16309 was for a class template specialization, then we will have a
16310 TYPE_DECL at this point. There is no need to do name lookup
16311 at this point in that case. */
16312 if (TREE_CODE (argument) != TYPE_DECL)
16313 argument = cp_parser_lookup_name (parser, argument,
16314 none_type,
16315 /*is_template=*/template_p,
16316 /*is_namespace=*/false,
16317 /*check_dependency=*/true,
16318 /*ambiguous_decls=*/NULL,
16319 argument_start_token->location);
16320 /* Handle a constrained-type-specifier for a non-type template
16321 parameter. */
16322 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16323 argument = decl;
16324 else if (TREE_CODE (argument) != TEMPLATE_DECL
16325 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16326 cp_parser_error (parser, "expected template-name");
16328 if (cp_parser_parse_definitely (parser))
16330 if (TREE_DEPRECATED (argument))
16331 warn_deprecated_use (argument, NULL_TREE);
16332 return argument;
16334 /* It must be a non-type argument. In C++17 any constant-expression is
16335 allowed. */
16336 if (cxx_dialect > cxx14)
16337 goto general_expr;
16339 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16341 -- an integral constant-expression of integral or enumeration
16342 type; or
16344 -- the name of a non-type template-parameter; or
16346 -- the name of an object or function with external linkage...
16348 -- the address of an object or function with external linkage...
16350 -- a pointer to member... */
16351 /* Look for a non-type template parameter. */
16352 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16354 cp_parser_parse_tentatively (parser);
16355 argument = cp_parser_primary_expression (parser,
16356 /*address_p=*/false,
16357 /*cast_p=*/false,
16358 /*template_arg_p=*/true,
16359 &idk);
16360 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16361 || !cp_parser_next_token_ends_template_argument_p (parser))
16362 cp_parser_simulate_error (parser);
16363 if (cp_parser_parse_definitely (parser))
16364 return argument;
16367 /* If the next token is "&", the argument must be the address of an
16368 object or function with external linkage. */
16369 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16370 if (address_p)
16372 loc = cp_lexer_peek_token (parser->lexer)->location;
16373 cp_lexer_consume_token (parser->lexer);
16375 /* See if we might have an id-expression. */
16376 token = cp_lexer_peek_token (parser->lexer);
16377 if (token->type == CPP_NAME
16378 || token->keyword == RID_OPERATOR
16379 || token->type == CPP_SCOPE
16380 || token->type == CPP_TEMPLATE_ID
16381 || token->type == CPP_NESTED_NAME_SPECIFIER)
16383 cp_parser_parse_tentatively (parser);
16384 argument = cp_parser_primary_expression (parser,
16385 address_p,
16386 /*cast_p=*/false,
16387 /*template_arg_p=*/true,
16388 &idk);
16389 if (cp_parser_error_occurred (parser)
16390 || !cp_parser_next_token_ends_template_argument_p (parser))
16391 cp_parser_abort_tentative_parse (parser);
16392 else
16394 tree probe;
16396 if (INDIRECT_REF_P (argument))
16398 /* Strip the dereference temporarily. */
16399 gcc_assert (REFERENCE_REF_P (argument));
16400 argument = TREE_OPERAND (argument, 0);
16403 /* If we're in a template, we represent a qualified-id referring
16404 to a static data member as a SCOPE_REF even if the scope isn't
16405 dependent so that we can check access control later. */
16406 probe = argument;
16407 if (TREE_CODE (probe) == SCOPE_REF)
16408 probe = TREE_OPERAND (probe, 1);
16409 if (VAR_P (probe))
16411 /* A variable without external linkage might still be a
16412 valid constant-expression, so no error is issued here
16413 if the external-linkage check fails. */
16414 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16415 cp_parser_simulate_error (parser);
16417 else if (is_overloaded_fn (argument))
16418 /* All overloaded functions are allowed; if the external
16419 linkage test does not pass, an error will be issued
16420 later. */
16422 else if (address_p
16423 && (TREE_CODE (argument) == OFFSET_REF
16424 || TREE_CODE (argument) == SCOPE_REF))
16425 /* A pointer-to-member. */
16427 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16429 else
16430 cp_parser_simulate_error (parser);
16432 if (cp_parser_parse_definitely (parser))
16434 if (address_p)
16435 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16436 tf_warning_or_error);
16437 else
16438 argument = convert_from_reference (argument);
16439 return argument;
16443 /* If the argument started with "&", there are no other valid
16444 alternatives at this point. */
16445 if (address_p)
16447 cp_parser_error (parser, "invalid non-type template argument");
16448 return error_mark_node;
16451 general_expr:
16452 /* If the argument wasn't successfully parsed as a type-id followed
16453 by '>>', the argument can only be a constant expression now.
16454 Otherwise, we try parsing the constant-expression tentatively,
16455 because the argument could really be a type-id. */
16456 if (maybe_type_id)
16457 cp_parser_parse_tentatively (parser);
16459 if (cxx_dialect <= cxx14)
16460 argument = cp_parser_constant_expression (parser);
16461 else
16463 /* With C++17 generalized non-type template arguments we need to handle
16464 lvalue constant expressions, too. */
16465 argument = cp_parser_assignment_expression (parser);
16466 require_potential_constant_expression (argument);
16469 if (!maybe_type_id)
16470 return argument;
16471 if (!cp_parser_next_token_ends_template_argument_p (parser))
16472 cp_parser_error (parser, "expected template-argument");
16473 if (cp_parser_parse_definitely (parser))
16474 return argument;
16475 /* We did our best to parse the argument as a non type-id, but that
16476 was the only alternative that matched (albeit with a '>' after
16477 it). We can assume it's just a typo from the user, and a
16478 diagnostic will then be issued. */
16479 return cp_parser_template_type_arg (parser);
16482 /* Parse an explicit-instantiation.
16484 explicit-instantiation:
16485 template declaration
16487 Although the standard says `declaration', what it really means is:
16489 explicit-instantiation:
16490 template decl-specifier-seq [opt] declarator [opt] ;
16492 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16493 supposed to be allowed. A defect report has been filed about this
16494 issue.
16496 GNU Extension:
16498 explicit-instantiation:
16499 storage-class-specifier template
16500 decl-specifier-seq [opt] declarator [opt] ;
16501 function-specifier template
16502 decl-specifier-seq [opt] declarator [opt] ; */
16504 static void
16505 cp_parser_explicit_instantiation (cp_parser* parser)
16507 int declares_class_or_enum;
16508 cp_decl_specifier_seq decl_specifiers;
16509 tree extension_specifier = NULL_TREE;
16511 timevar_push (TV_TEMPLATE_INST);
16513 /* Look for an (optional) storage-class-specifier or
16514 function-specifier. */
16515 if (cp_parser_allow_gnu_extensions_p (parser))
16517 extension_specifier
16518 = cp_parser_storage_class_specifier_opt (parser);
16519 if (!extension_specifier)
16520 extension_specifier
16521 = cp_parser_function_specifier_opt (parser,
16522 /*decl_specs=*/NULL);
16525 /* Look for the `template' keyword. */
16526 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16527 /* Let the front end know that we are processing an explicit
16528 instantiation. */
16529 begin_explicit_instantiation ();
16530 /* [temp.explicit] says that we are supposed to ignore access
16531 control while processing explicit instantiation directives. */
16532 push_deferring_access_checks (dk_no_check);
16533 /* Parse a decl-specifier-seq. */
16534 cp_parser_decl_specifier_seq (parser,
16535 CP_PARSER_FLAGS_OPTIONAL,
16536 &decl_specifiers,
16537 &declares_class_or_enum);
16538 /* If there was exactly one decl-specifier, and it declared a class,
16539 and there's no declarator, then we have an explicit type
16540 instantiation. */
16541 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16543 tree type;
16545 type = check_tag_decl (&decl_specifiers,
16546 /*explicit_type_instantiation_p=*/true);
16547 /* Turn access control back on for names used during
16548 template instantiation. */
16549 pop_deferring_access_checks ();
16550 if (type)
16551 do_type_instantiation (type, extension_specifier,
16552 /*complain=*/tf_error);
16554 else
16556 cp_declarator *declarator;
16557 tree decl;
16559 /* Parse the declarator. */
16560 declarator
16561 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16562 /*ctor_dtor_or_conv_p=*/NULL,
16563 /*parenthesized_p=*/NULL,
16564 /*member_p=*/false,
16565 /*friend_p=*/false);
16566 if (declares_class_or_enum & 2)
16567 cp_parser_check_for_definition_in_return_type (declarator,
16568 decl_specifiers.type,
16569 decl_specifiers.locations[ds_type_spec]);
16570 if (declarator != cp_error_declarator)
16572 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16573 permerror (decl_specifiers.locations[ds_inline],
16574 "explicit instantiation shall not use"
16575 " %<inline%> specifier");
16576 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16577 permerror (decl_specifiers.locations[ds_constexpr],
16578 "explicit instantiation shall not use"
16579 " %<constexpr%> specifier");
16581 decl = grokdeclarator (declarator, &decl_specifiers,
16582 NORMAL, 0, &decl_specifiers.attributes);
16583 /* Turn access control back on for names used during
16584 template instantiation. */
16585 pop_deferring_access_checks ();
16586 /* Do the explicit instantiation. */
16587 do_decl_instantiation (decl, extension_specifier);
16589 else
16591 pop_deferring_access_checks ();
16592 /* Skip the body of the explicit instantiation. */
16593 cp_parser_skip_to_end_of_statement (parser);
16596 /* We're done with the instantiation. */
16597 end_explicit_instantiation ();
16599 cp_parser_consume_semicolon_at_end_of_statement (parser);
16601 timevar_pop (TV_TEMPLATE_INST);
16604 /* Parse an explicit-specialization.
16606 explicit-specialization:
16607 template < > declaration
16609 Although the standard says `declaration', what it really means is:
16611 explicit-specialization:
16612 template <> decl-specifier [opt] init-declarator [opt] ;
16613 template <> function-definition
16614 template <> explicit-specialization
16615 template <> template-declaration */
16617 static void
16618 cp_parser_explicit_specialization (cp_parser* parser)
16620 bool need_lang_pop;
16621 cp_token *token = cp_lexer_peek_token (parser->lexer);
16623 /* Look for the `template' keyword. */
16624 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16625 /* Look for the `<'. */
16626 cp_parser_require (parser, CPP_LESS, RT_LESS);
16627 /* Look for the `>'. */
16628 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16629 /* We have processed another parameter list. */
16630 ++parser->num_template_parameter_lists;
16631 /* [temp]
16633 A template ... explicit specialization ... shall not have C
16634 linkage. */
16635 if (current_lang_name == lang_name_c)
16637 error_at (token->location, "template specialization with C linkage");
16638 maybe_show_extern_c_location ();
16639 /* Give it C++ linkage to avoid confusing other parts of the
16640 front end. */
16641 push_lang_context (lang_name_cplusplus);
16642 need_lang_pop = true;
16644 else
16645 need_lang_pop = false;
16646 /* Let the front end know that we are beginning a specialization. */
16647 if (!begin_specialization ())
16649 end_specialization ();
16650 return;
16653 /* If the next keyword is `template', we need to figure out whether
16654 or not we're looking a template-declaration. */
16655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16657 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16658 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16659 cp_parser_template_declaration_after_export (parser,
16660 /*member_p=*/false);
16661 else
16662 cp_parser_explicit_specialization (parser);
16664 else
16665 /* Parse the dependent declaration. */
16666 cp_parser_single_declaration (parser,
16667 /*checks=*/NULL,
16668 /*member_p=*/false,
16669 /*explicit_specialization_p=*/true,
16670 /*friend_p=*/NULL);
16671 /* We're done with the specialization. */
16672 end_specialization ();
16673 /* For the erroneous case of a template with C linkage, we pushed an
16674 implicit C++ linkage scope; exit that scope now. */
16675 if (need_lang_pop)
16676 pop_lang_context ();
16677 /* We're done with this parameter list. */
16678 --parser->num_template_parameter_lists;
16681 /* Parse a type-specifier.
16683 type-specifier:
16684 simple-type-specifier
16685 class-specifier
16686 enum-specifier
16687 elaborated-type-specifier
16688 cv-qualifier
16690 GNU Extension:
16692 type-specifier:
16693 __complex__
16695 Returns a representation of the type-specifier. For a
16696 class-specifier, enum-specifier, or elaborated-type-specifier, a
16697 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16699 The parser flags FLAGS is used to control type-specifier parsing.
16701 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16702 in a decl-specifier-seq.
16704 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16705 class-specifier, enum-specifier, or elaborated-type-specifier, then
16706 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16707 if a type is declared; 2 if it is defined. Otherwise, it is set to
16708 zero.
16710 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16711 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16712 is set to FALSE. */
16714 static tree
16715 cp_parser_type_specifier (cp_parser* parser,
16716 cp_parser_flags flags,
16717 cp_decl_specifier_seq *decl_specs,
16718 bool is_declaration,
16719 int* declares_class_or_enum,
16720 bool* is_cv_qualifier)
16722 tree type_spec = NULL_TREE;
16723 cp_token *token;
16724 enum rid keyword;
16725 cp_decl_spec ds = ds_last;
16727 /* Assume this type-specifier does not declare a new type. */
16728 if (declares_class_or_enum)
16729 *declares_class_or_enum = 0;
16730 /* And that it does not specify a cv-qualifier. */
16731 if (is_cv_qualifier)
16732 *is_cv_qualifier = false;
16733 /* Peek at the next token. */
16734 token = cp_lexer_peek_token (parser->lexer);
16736 /* If we're looking at a keyword, we can use that to guide the
16737 production we choose. */
16738 keyword = token->keyword;
16739 switch (keyword)
16741 case RID_ENUM:
16742 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16743 goto elaborated_type_specifier;
16745 /* Look for the enum-specifier. */
16746 type_spec = cp_parser_enum_specifier (parser);
16747 /* If that worked, we're done. */
16748 if (type_spec)
16750 if (declares_class_or_enum)
16751 *declares_class_or_enum = 2;
16752 if (decl_specs)
16753 cp_parser_set_decl_spec_type (decl_specs,
16754 type_spec,
16755 token,
16756 /*type_definition_p=*/true);
16757 return type_spec;
16759 else
16760 goto elaborated_type_specifier;
16762 /* Any of these indicate either a class-specifier, or an
16763 elaborated-type-specifier. */
16764 case RID_CLASS:
16765 case RID_STRUCT:
16766 case RID_UNION:
16767 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16768 goto elaborated_type_specifier;
16770 /* Parse tentatively so that we can back up if we don't find a
16771 class-specifier. */
16772 cp_parser_parse_tentatively (parser);
16773 /* Look for the class-specifier. */
16774 type_spec = cp_parser_class_specifier (parser);
16775 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16776 /* If that worked, we're done. */
16777 if (cp_parser_parse_definitely (parser))
16779 if (declares_class_or_enum)
16780 *declares_class_or_enum = 2;
16781 if (decl_specs)
16782 cp_parser_set_decl_spec_type (decl_specs,
16783 type_spec,
16784 token,
16785 /*type_definition_p=*/true);
16786 return type_spec;
16789 /* Fall through. */
16790 elaborated_type_specifier:
16791 /* We're declaring (not defining) a class or enum. */
16792 if (declares_class_or_enum)
16793 *declares_class_or_enum = 1;
16795 /* Fall through. */
16796 case RID_TYPENAME:
16797 /* Look for an elaborated-type-specifier. */
16798 type_spec
16799 = (cp_parser_elaborated_type_specifier
16800 (parser,
16801 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16802 is_declaration));
16803 if (decl_specs)
16804 cp_parser_set_decl_spec_type (decl_specs,
16805 type_spec,
16806 token,
16807 /*type_definition_p=*/false);
16808 return type_spec;
16810 case RID_CONST:
16811 ds = ds_const;
16812 if (is_cv_qualifier)
16813 *is_cv_qualifier = true;
16814 break;
16816 case RID_VOLATILE:
16817 ds = ds_volatile;
16818 if (is_cv_qualifier)
16819 *is_cv_qualifier = true;
16820 break;
16822 case RID_RESTRICT:
16823 ds = ds_restrict;
16824 if (is_cv_qualifier)
16825 *is_cv_qualifier = true;
16826 break;
16828 case RID_COMPLEX:
16829 /* The `__complex__' keyword is a GNU extension. */
16830 ds = ds_complex;
16831 break;
16833 default:
16834 break;
16837 /* Handle simple keywords. */
16838 if (ds != ds_last)
16840 if (decl_specs)
16842 set_and_check_decl_spec_loc (decl_specs, ds, token);
16843 decl_specs->any_specifiers_p = true;
16845 return cp_lexer_consume_token (parser->lexer)->u.value;
16848 /* If we do not already have a type-specifier, assume we are looking
16849 at a simple-type-specifier. */
16850 type_spec = cp_parser_simple_type_specifier (parser,
16851 decl_specs,
16852 flags);
16854 /* If we didn't find a type-specifier, and a type-specifier was not
16855 optional in this context, issue an error message. */
16856 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16858 cp_parser_error (parser, "expected type specifier");
16859 return error_mark_node;
16862 return type_spec;
16865 /* Parse a simple-type-specifier.
16867 simple-type-specifier:
16868 :: [opt] nested-name-specifier [opt] type-name
16869 :: [opt] nested-name-specifier template template-id
16870 char
16871 wchar_t
16872 bool
16873 short
16875 long
16876 signed
16877 unsigned
16878 float
16879 double
16880 void
16882 C++11 Extension:
16884 simple-type-specifier:
16885 auto
16886 decltype ( expression )
16887 char16_t
16888 char32_t
16889 __underlying_type ( type-id )
16891 C++17 extension:
16893 nested-name-specifier(opt) template-name
16895 GNU Extension:
16897 simple-type-specifier:
16898 __int128
16899 __typeof__ unary-expression
16900 __typeof__ ( type-id )
16901 __typeof__ ( type-id ) { initializer-list , [opt] }
16903 Concepts Extension:
16905 simple-type-specifier:
16906 constrained-type-specifier
16908 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16909 appropriately updated. */
16911 static tree
16912 cp_parser_simple_type_specifier (cp_parser* parser,
16913 cp_decl_specifier_seq *decl_specs,
16914 cp_parser_flags flags)
16916 tree type = NULL_TREE;
16917 cp_token *token;
16918 int idx;
16920 /* Peek at the next token. */
16921 token = cp_lexer_peek_token (parser->lexer);
16923 /* If we're looking at a keyword, things are easy. */
16924 switch (token->keyword)
16926 case RID_CHAR:
16927 if (decl_specs)
16928 decl_specs->explicit_char_p = true;
16929 type = char_type_node;
16930 break;
16931 case RID_CHAR16:
16932 type = char16_type_node;
16933 break;
16934 case RID_CHAR32:
16935 type = char32_type_node;
16936 break;
16937 case RID_WCHAR:
16938 type = wchar_type_node;
16939 break;
16940 case RID_BOOL:
16941 type = boolean_type_node;
16942 break;
16943 case RID_SHORT:
16944 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16945 type = short_integer_type_node;
16946 break;
16947 case RID_INT:
16948 if (decl_specs)
16949 decl_specs->explicit_int_p = true;
16950 type = integer_type_node;
16951 break;
16952 case RID_INT_N_0:
16953 case RID_INT_N_1:
16954 case RID_INT_N_2:
16955 case RID_INT_N_3:
16956 idx = token->keyword - RID_INT_N_0;
16957 if (! int_n_enabled_p [idx])
16958 break;
16959 if (decl_specs)
16961 decl_specs->explicit_intN_p = true;
16962 decl_specs->int_n_idx = idx;
16964 type = int_n_trees [idx].signed_type;
16965 break;
16966 case RID_LONG:
16967 if (decl_specs)
16968 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16969 type = long_integer_type_node;
16970 break;
16971 case RID_SIGNED:
16972 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16973 type = integer_type_node;
16974 break;
16975 case RID_UNSIGNED:
16976 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16977 type = unsigned_type_node;
16978 break;
16979 case RID_FLOAT:
16980 type = float_type_node;
16981 break;
16982 case RID_DOUBLE:
16983 type = double_type_node;
16984 break;
16985 case RID_VOID:
16986 type = void_type_node;
16987 break;
16989 case RID_AUTO:
16990 maybe_warn_cpp0x (CPP0X_AUTO);
16991 if (parser->auto_is_implicit_function_template_parm_p)
16993 /* The 'auto' might be the placeholder return type for a function decl
16994 with trailing return type. */
16995 bool have_trailing_return_fn_decl = false;
16997 cp_parser_parse_tentatively (parser);
16998 cp_lexer_consume_token (parser->lexer);
16999 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17000 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17001 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17002 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17004 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17006 cp_lexer_consume_token (parser->lexer);
17007 cp_parser_skip_to_closing_parenthesis (parser,
17008 /*recovering*/false,
17009 /*or_comma*/false,
17010 /*consume_paren*/true);
17011 continue;
17014 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17016 have_trailing_return_fn_decl = true;
17017 break;
17020 cp_lexer_consume_token (parser->lexer);
17022 cp_parser_abort_tentative_parse (parser);
17024 if (have_trailing_return_fn_decl)
17026 type = make_auto ();
17027 break;
17030 if (cxx_dialect >= cxx14)
17032 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17033 type = TREE_TYPE (type);
17035 else
17036 type = error_mark_node;
17038 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17040 if (cxx_dialect < cxx14)
17041 error_at (token->location,
17042 "use of %<auto%> in lambda parameter declaration "
17043 "only available with "
17044 "-std=c++14 or -std=gnu++14");
17046 else if (cxx_dialect < cxx14)
17047 error_at (token->location,
17048 "use of %<auto%> in parameter declaration "
17049 "only available with "
17050 "-std=c++14 or -std=gnu++14");
17051 else if (!flag_concepts)
17052 pedwarn (token->location, OPT_Wpedantic,
17053 "ISO C++ forbids use of %<auto%> in parameter "
17054 "declaration");
17056 else
17057 type = make_auto ();
17058 break;
17060 case RID_DECLTYPE:
17061 /* Since DR 743, decltype can either be a simple-type-specifier by
17062 itself or begin a nested-name-specifier. Parsing it will replace
17063 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17064 handling below decide what to do. */
17065 cp_parser_decltype (parser);
17066 cp_lexer_set_token_position (parser->lexer, token);
17067 break;
17069 case RID_TYPEOF:
17070 /* Consume the `typeof' token. */
17071 cp_lexer_consume_token (parser->lexer);
17072 /* Parse the operand to `typeof'. */
17073 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17074 /* If it is not already a TYPE, take its type. */
17075 if (!TYPE_P (type))
17076 type = finish_typeof (type);
17078 if (decl_specs)
17079 cp_parser_set_decl_spec_type (decl_specs, type,
17080 token,
17081 /*type_definition_p=*/false);
17083 return type;
17085 case RID_UNDERLYING_TYPE:
17086 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17087 if (decl_specs)
17088 cp_parser_set_decl_spec_type (decl_specs, type,
17089 token,
17090 /*type_definition_p=*/false);
17092 return type;
17094 case RID_BASES:
17095 case RID_DIRECT_BASES:
17096 type = cp_parser_trait_expr (parser, token->keyword);
17097 if (decl_specs)
17098 cp_parser_set_decl_spec_type (decl_specs, type,
17099 token,
17100 /*type_definition_p=*/false);
17101 return type;
17102 default:
17103 break;
17106 /* If token is an already-parsed decltype not followed by ::,
17107 it's a simple-type-specifier. */
17108 if (token->type == CPP_DECLTYPE
17109 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17111 type = saved_checks_value (token->u.tree_check_value);
17112 if (decl_specs)
17114 cp_parser_set_decl_spec_type (decl_specs, type,
17115 token,
17116 /*type_definition_p=*/false);
17117 /* Remember that we are handling a decltype in order to
17118 implement the resolution of DR 1510 when the argument
17119 isn't instantiation dependent. */
17120 decl_specs->decltype_p = true;
17122 cp_lexer_consume_token (parser->lexer);
17123 return type;
17126 /* If the type-specifier was for a built-in type, we're done. */
17127 if (type)
17129 /* Record the type. */
17130 if (decl_specs
17131 && (token->keyword != RID_SIGNED
17132 && token->keyword != RID_UNSIGNED
17133 && token->keyword != RID_SHORT
17134 && token->keyword != RID_LONG))
17135 cp_parser_set_decl_spec_type (decl_specs,
17136 type,
17137 token,
17138 /*type_definition_p=*/false);
17139 if (decl_specs)
17140 decl_specs->any_specifiers_p = true;
17142 /* Consume the token. */
17143 cp_lexer_consume_token (parser->lexer);
17145 if (type == error_mark_node)
17146 return error_mark_node;
17148 /* There is no valid C++ program where a non-template type is
17149 followed by a "<". That usually indicates that the user thought
17150 that the type was a template. */
17151 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17152 token->location);
17154 return TYPE_NAME (type);
17157 /* The type-specifier must be a user-defined type. */
17158 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17160 bool qualified_p;
17161 bool global_p;
17163 /* Don't gobble tokens or issue error messages if this is an
17164 optional type-specifier. */
17165 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17166 cp_parser_parse_tentatively (parser);
17168 token = cp_lexer_peek_token (parser->lexer);
17170 /* Look for the optional `::' operator. */
17171 global_p
17172 = (cp_parser_global_scope_opt (parser,
17173 /*current_scope_valid_p=*/false)
17174 != NULL_TREE);
17175 /* Look for the nested-name specifier. */
17176 qualified_p
17177 = (cp_parser_nested_name_specifier_opt (parser,
17178 /*typename_keyword_p=*/false,
17179 /*check_dependency_p=*/true,
17180 /*type_p=*/false,
17181 /*is_declaration=*/false)
17182 != NULL_TREE);
17183 /* If we have seen a nested-name-specifier, and the next token
17184 is `template', then we are using the template-id production. */
17185 if (parser->scope
17186 && cp_parser_optional_template_keyword (parser))
17188 /* Look for the template-id. */
17189 type = cp_parser_template_id (parser,
17190 /*template_keyword_p=*/true,
17191 /*check_dependency_p=*/true,
17192 none_type,
17193 /*is_declaration=*/false);
17194 /* If the template-id did not name a type, we are out of
17195 luck. */
17196 if (TREE_CODE (type) != TYPE_DECL)
17198 cp_parser_error (parser, "expected template-id for type");
17199 type = NULL_TREE;
17202 /* Otherwise, look for a type-name. */
17203 else
17204 type = cp_parser_type_name (parser);
17205 /* Keep track of all name-lookups performed in class scopes. */
17206 if (type
17207 && !global_p
17208 && !qualified_p
17209 && TREE_CODE (type) == TYPE_DECL
17210 && identifier_p (DECL_NAME (type)))
17211 maybe_note_name_used_in_class (DECL_NAME (type), type);
17212 /* If it didn't work out, we don't have a TYPE. */
17213 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17214 && !cp_parser_parse_definitely (parser))
17215 type = NULL_TREE;
17216 if (!type && cxx_dialect >= cxx17)
17218 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17219 cp_parser_parse_tentatively (parser);
17221 cp_parser_global_scope_opt (parser,
17222 /*current_scope_valid_p=*/false);
17223 cp_parser_nested_name_specifier_opt (parser,
17224 /*typename_keyword_p=*/false,
17225 /*check_dependency_p=*/true,
17226 /*type_p=*/false,
17227 /*is_declaration=*/false);
17228 tree name = cp_parser_identifier (parser);
17229 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17230 && parser->scope != error_mark_node)
17232 tree tmpl = cp_parser_lookup_name (parser, name,
17233 none_type,
17234 /*is_template=*/false,
17235 /*is_namespace=*/false,
17236 /*check_dependency=*/true,
17237 /*ambiguous_decls=*/NULL,
17238 token->location);
17239 if (tmpl && tmpl != error_mark_node
17240 && (DECL_CLASS_TEMPLATE_P (tmpl)
17241 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17242 type = make_template_placeholder (tmpl);
17243 else
17245 type = error_mark_node;
17246 if (!cp_parser_simulate_error (parser))
17247 cp_parser_name_lookup_error (parser, name, tmpl,
17248 NLE_TYPE, token->location);
17251 else
17252 type = error_mark_node;
17254 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17255 && !cp_parser_parse_definitely (parser))
17256 type = NULL_TREE;
17258 if (type && decl_specs)
17259 cp_parser_set_decl_spec_type (decl_specs, type,
17260 token,
17261 /*type_definition_p=*/false);
17264 /* If we didn't get a type-name, issue an error message. */
17265 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17267 cp_parser_error (parser, "expected type-name");
17268 return error_mark_node;
17271 if (type && type != error_mark_node)
17273 /* See if TYPE is an Objective-C type, and if so, parse and
17274 accept any protocol references following it. Do this before
17275 the cp_parser_check_for_invalid_template_id() call, because
17276 Objective-C types can be followed by '<...>' which would
17277 enclose protocol names rather than template arguments, and so
17278 everything is fine. */
17279 if (c_dialect_objc () && !parser->scope
17280 && (objc_is_id (type) || objc_is_class_name (type)))
17282 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17283 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17285 /* Clobber the "unqualified" type previously entered into
17286 DECL_SPECS with the new, improved protocol-qualified version. */
17287 if (decl_specs)
17288 decl_specs->type = qual_type;
17290 return qual_type;
17293 /* There is no valid C++ program where a non-template type is
17294 followed by a "<". That usually indicates that the user
17295 thought that the type was a template. */
17296 cp_parser_check_for_invalid_template_id (parser, type,
17297 none_type,
17298 token->location);
17301 return type;
17304 /* Parse a type-name.
17306 type-name:
17307 class-name
17308 enum-name
17309 typedef-name
17310 simple-template-id [in c++0x]
17312 enum-name:
17313 identifier
17315 typedef-name:
17316 identifier
17318 Concepts:
17320 type-name:
17321 concept-name
17322 partial-concept-id
17324 concept-name:
17325 identifier
17327 Returns a TYPE_DECL for the type. */
17329 static tree
17330 cp_parser_type_name (cp_parser* parser)
17332 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17335 /* See above. */
17336 static tree
17337 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17339 tree type_decl;
17341 /* We can't know yet whether it is a class-name or not. */
17342 cp_parser_parse_tentatively (parser);
17343 /* Try a class-name. */
17344 type_decl = cp_parser_class_name (parser,
17345 typename_keyword_p,
17346 /*template_keyword_p=*/false,
17347 none_type,
17348 /*check_dependency_p=*/true,
17349 /*class_head_p=*/false,
17350 /*is_declaration=*/false);
17351 /* If it's not a class-name, keep looking. */
17352 if (!cp_parser_parse_definitely (parser))
17354 if (cxx_dialect < cxx11)
17355 /* It must be a typedef-name or an enum-name. */
17356 return cp_parser_nonclass_name (parser);
17358 cp_parser_parse_tentatively (parser);
17359 /* It is either a simple-template-id representing an
17360 instantiation of an alias template... */
17361 type_decl = cp_parser_template_id (parser,
17362 /*template_keyword_p=*/false,
17363 /*check_dependency_p=*/true,
17364 none_type,
17365 /*is_declaration=*/false);
17366 /* Note that this must be an instantiation of an alias template
17367 because [temp.names]/6 says:
17369 A template-id that names an alias template specialization
17370 is a type-name.
17372 Whereas [temp.names]/7 says:
17374 A simple-template-id that names a class template
17375 specialization is a class-name.
17377 With concepts, this could also be a partial-concept-id that
17378 declares a non-type template parameter. */
17379 if (type_decl != NULL_TREE
17380 && TREE_CODE (type_decl) == TYPE_DECL
17381 && TYPE_DECL_ALIAS_P (type_decl))
17382 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17383 else if (is_constrained_parameter (type_decl))
17384 /* Don't do anything. */ ;
17385 else
17386 cp_parser_simulate_error (parser);
17388 if (!cp_parser_parse_definitely (parser))
17389 /* ... Or a typedef-name or an enum-name. */
17390 return cp_parser_nonclass_name (parser);
17393 return type_decl;
17396 /* Check if DECL and ARGS can form a constrained-type-specifier.
17397 If ARGS is non-null, we try to form a concept check of the
17398 form DECL<?, ARGS> where ? is a wildcard that matches any
17399 kind of template argument. If ARGS is NULL, then we try to
17400 form a concept check of the form DECL<?>. */
17402 static tree
17403 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17404 tree decl, tree args)
17406 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17408 /* If we a constrained-type-specifier cannot be deduced. */
17409 if (parser->prevent_constrained_type_specifiers)
17410 return NULL_TREE;
17412 /* A constrained type specifier can only be found in an
17413 overload set or as a reference to a template declaration.
17415 FIXME: This might be masking a bug. It's possible that
17416 that the deduction below is causing template specializations
17417 to be formed with the wildcard as an argument. */
17418 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17419 return NULL_TREE;
17421 /* Try to build a call expression that evaluates the
17422 concept. This can fail if the overload set refers
17423 only to non-templates. */
17424 tree placeholder = build_nt (WILDCARD_DECL);
17425 tree check = build_concept_check (decl, placeholder, args);
17426 if (check == error_mark_node)
17427 return NULL_TREE;
17429 /* Deduce the checked constraint and the prototype parameter.
17431 FIXME: In certain cases, failure to deduce should be a
17432 diagnosable error. */
17433 tree conc;
17434 tree proto;
17435 if (!deduce_constrained_parameter (check, conc, proto))
17436 return NULL_TREE;
17438 /* In template parameter scope, this results in a constrained
17439 parameter. Return a descriptor of that parm. */
17440 if (processing_template_parmlist)
17441 return build_constrained_parameter (conc, proto, args);
17443 /* In a parameter-declaration-clause, constrained-type
17444 specifiers result in invented template parameters. */
17445 if (parser->auto_is_implicit_function_template_parm_p)
17447 tree x = build_constrained_parameter (conc, proto, args);
17448 return synthesize_implicit_template_parm (parser, x);
17450 else
17452 /* Otherwise, we're in a context where the constrained
17453 type name is deduced and the constraint applies
17454 after deduction. */
17455 return make_constrained_auto (conc, args);
17458 return NULL_TREE;
17461 /* If DECL refers to a concept, return a TYPE_DECL representing
17462 the result of using the constrained type specifier in the
17463 current context. DECL refers to a concept if
17465 - it is an overload set containing a function concept taking a single
17466 type argument, or
17468 - it is a variable concept taking a single type argument. */
17470 static tree
17471 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17473 if (flag_concepts
17474 && (TREE_CODE (decl) == OVERLOAD
17475 || BASELINK_P (decl)
17476 || variable_concept_p (decl)))
17477 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17478 else
17479 return NULL_TREE;
17482 /* Check if DECL and ARGS form a partial-concept-id. If so,
17483 assign ID to the resulting constrained placeholder.
17485 Returns true if the partial-concept-id designates a placeholder
17486 and false otherwise. Note that *id is set to NULL_TREE in
17487 this case. */
17489 static tree
17490 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17492 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17495 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17496 or a concept-name.
17498 enum-name:
17499 identifier
17501 typedef-name:
17502 identifier
17504 concept-name:
17505 identifier
17507 Returns a TYPE_DECL for the type. */
17509 static tree
17510 cp_parser_nonclass_name (cp_parser* parser)
17512 tree type_decl;
17513 tree identifier;
17515 cp_token *token = cp_lexer_peek_token (parser->lexer);
17516 identifier = cp_parser_identifier (parser);
17517 if (identifier == error_mark_node)
17518 return error_mark_node;
17520 /* Look up the type-name. */
17521 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17523 type_decl = strip_using_decl (type_decl);
17525 /* If we found an overload set, then it may refer to a concept-name. */
17526 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17527 type_decl = decl;
17529 if (TREE_CODE (type_decl) != TYPE_DECL
17530 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17532 /* See if this is an Objective-C type. */
17533 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17534 tree type = objc_get_protocol_qualified_type (identifier, protos);
17535 if (type)
17536 type_decl = TYPE_NAME (type);
17539 /* Issue an error if we did not find a type-name. */
17540 if (TREE_CODE (type_decl) != TYPE_DECL
17541 /* In Objective-C, we have the complication that class names are
17542 normally type names and start declarations (eg, the
17543 "NSObject" in "NSObject *object;"), but can be used in an
17544 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17545 is an expression. So, a classname followed by a dot is not a
17546 valid type-name. */
17547 || (objc_is_class_name (TREE_TYPE (type_decl))
17548 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17550 if (!cp_parser_simulate_error (parser))
17551 cp_parser_name_lookup_error (parser, identifier, type_decl,
17552 NLE_TYPE, token->location);
17553 return error_mark_node;
17555 /* Remember that the name was used in the definition of the
17556 current class so that we can check later to see if the
17557 meaning would have been different after the class was
17558 entirely defined. */
17559 else if (type_decl != error_mark_node
17560 && !parser->scope)
17561 maybe_note_name_used_in_class (identifier, type_decl);
17563 return type_decl;
17566 /* Parse an elaborated-type-specifier. Note that the grammar given
17567 here incorporates the resolution to DR68.
17569 elaborated-type-specifier:
17570 class-key :: [opt] nested-name-specifier [opt] identifier
17571 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17572 enum-key :: [opt] nested-name-specifier [opt] identifier
17573 typename :: [opt] nested-name-specifier identifier
17574 typename :: [opt] nested-name-specifier template [opt]
17575 template-id
17577 GNU extension:
17579 elaborated-type-specifier:
17580 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17581 class-key attributes :: [opt] nested-name-specifier [opt]
17582 template [opt] template-id
17583 enum attributes :: [opt] nested-name-specifier [opt] identifier
17585 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17586 declared `friend'. If IS_DECLARATION is TRUE, then this
17587 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17588 something is being declared.
17590 Returns the TYPE specified. */
17592 static tree
17593 cp_parser_elaborated_type_specifier (cp_parser* parser,
17594 bool is_friend,
17595 bool is_declaration)
17597 enum tag_types tag_type;
17598 tree identifier;
17599 tree type = NULL_TREE;
17600 tree attributes = NULL_TREE;
17601 tree globalscope;
17602 cp_token *token = NULL;
17604 /* See if we're looking at the `enum' keyword. */
17605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17607 /* Consume the `enum' token. */
17608 cp_lexer_consume_token (parser->lexer);
17609 /* Remember that it's an enumeration type. */
17610 tag_type = enum_type;
17611 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17612 enums) is used here. */
17613 cp_token *token = cp_lexer_peek_token (parser->lexer);
17614 if (cp_parser_is_keyword (token, RID_CLASS)
17615 || cp_parser_is_keyword (token, RID_STRUCT))
17617 gcc_rich_location richloc (token->location);
17618 richloc.add_range (input_location, false);
17619 richloc.add_fixit_remove ();
17620 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17621 "a scoped enum must not use the %qD keyword",
17622 token->u.value);
17623 /* Consume the `struct' or `class' and parse it anyway. */
17624 cp_lexer_consume_token (parser->lexer);
17626 /* Parse the attributes. */
17627 attributes = cp_parser_attributes_opt (parser);
17629 /* Or, it might be `typename'. */
17630 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17631 RID_TYPENAME))
17633 /* Consume the `typename' token. */
17634 cp_lexer_consume_token (parser->lexer);
17635 /* Remember that it's a `typename' type. */
17636 tag_type = typename_type;
17638 /* Otherwise it must be a class-key. */
17639 else
17641 tag_type = cp_parser_class_key (parser);
17642 if (tag_type == none_type)
17643 return error_mark_node;
17644 /* Parse the attributes. */
17645 attributes = cp_parser_attributes_opt (parser);
17648 /* Look for the `::' operator. */
17649 globalscope = cp_parser_global_scope_opt (parser,
17650 /*current_scope_valid_p=*/false);
17651 /* Look for the nested-name-specifier. */
17652 tree nested_name_specifier;
17653 if (tag_type == typename_type && !globalscope)
17655 nested_name_specifier
17656 = cp_parser_nested_name_specifier (parser,
17657 /*typename_keyword_p=*/true,
17658 /*check_dependency_p=*/true,
17659 /*type_p=*/true,
17660 is_declaration);
17661 if (!nested_name_specifier)
17662 return error_mark_node;
17664 else
17665 /* Even though `typename' is not present, the proposed resolution
17666 to Core Issue 180 says that in `class A<T>::B', `B' should be
17667 considered a type-name, even if `A<T>' is dependent. */
17668 nested_name_specifier
17669 = cp_parser_nested_name_specifier_opt (parser,
17670 /*typename_keyword_p=*/true,
17671 /*check_dependency_p=*/true,
17672 /*type_p=*/true,
17673 is_declaration);
17674 /* For everything but enumeration types, consider a template-id.
17675 For an enumeration type, consider only a plain identifier. */
17676 if (tag_type != enum_type)
17678 bool template_p = false;
17679 tree decl;
17681 /* Allow the `template' keyword. */
17682 template_p = cp_parser_optional_template_keyword (parser);
17683 /* If we didn't see `template', we don't know if there's a
17684 template-id or not. */
17685 if (!template_p)
17686 cp_parser_parse_tentatively (parser);
17687 /* Parse the template-id. */
17688 token = cp_lexer_peek_token (parser->lexer);
17689 decl = cp_parser_template_id (parser, template_p,
17690 /*check_dependency_p=*/true,
17691 tag_type,
17692 is_declaration);
17693 /* If we didn't find a template-id, look for an ordinary
17694 identifier. */
17695 if (!template_p && !cp_parser_parse_definitely (parser))
17697 /* We can get here when cp_parser_template_id, called by
17698 cp_parser_class_name with tag_type == none_type, succeeds
17699 and caches a BASELINK. Then, when called again here,
17700 instead of failing and returning an error_mark_node
17701 returns it (see template/typename17.C in C++11).
17702 ??? Could we diagnose this earlier? */
17703 else if (tag_type == typename_type && BASELINK_P (decl))
17705 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17706 type = error_mark_node;
17708 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17709 in effect, then we must assume that, upon instantiation, the
17710 template will correspond to a class. */
17711 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17712 && tag_type == typename_type)
17713 type = make_typename_type (parser->scope, decl,
17714 typename_type,
17715 /*complain=*/tf_error);
17716 /* If the `typename' keyword is in effect and DECL is not a type
17717 decl, then type is non existent. */
17718 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17720 else if (TREE_CODE (decl) == TYPE_DECL)
17722 type = check_elaborated_type_specifier (tag_type, decl,
17723 /*allow_template_p=*/true);
17725 /* If the next token is a semicolon, this must be a specialization,
17726 instantiation, or friend declaration. Check the scope while we
17727 still know whether or not we had a nested-name-specifier. */
17728 if (type != error_mark_node
17729 && !nested_name_specifier && !is_friend
17730 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17731 check_unqualified_spec_or_inst (type, token->location);
17733 else if (decl == error_mark_node)
17734 type = error_mark_node;
17737 if (!type)
17739 token = cp_lexer_peek_token (parser->lexer);
17740 identifier = cp_parser_identifier (parser);
17742 if (identifier == error_mark_node)
17744 parser->scope = NULL_TREE;
17745 return error_mark_node;
17748 /* For a `typename', we needn't call xref_tag. */
17749 if (tag_type == typename_type
17750 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17751 return cp_parser_make_typename_type (parser, identifier,
17752 token->location);
17754 /* Template parameter lists apply only if we are not within a
17755 function parameter list. */
17756 bool template_parm_lists_apply
17757 = parser->num_template_parameter_lists;
17758 if (template_parm_lists_apply)
17759 for (cp_binding_level *s = current_binding_level;
17760 s && s->kind != sk_template_parms;
17761 s = s->level_chain)
17762 if (s->kind == sk_function_parms)
17763 template_parm_lists_apply = false;
17765 /* Look up a qualified name in the usual way. */
17766 if (parser->scope)
17768 tree decl;
17769 tree ambiguous_decls;
17771 decl = cp_parser_lookup_name (parser, identifier,
17772 tag_type,
17773 /*is_template=*/false,
17774 /*is_namespace=*/false,
17775 /*check_dependency=*/true,
17776 &ambiguous_decls,
17777 token->location);
17779 /* If the lookup was ambiguous, an error will already have been
17780 issued. */
17781 if (ambiguous_decls)
17782 return error_mark_node;
17784 /* If we are parsing friend declaration, DECL may be a
17785 TEMPLATE_DECL tree node here. However, we need to check
17786 whether this TEMPLATE_DECL results in valid code. Consider
17787 the following example:
17789 namespace N {
17790 template <class T> class C {};
17792 class X {
17793 template <class T> friend class N::C; // #1, valid code
17795 template <class T> class Y {
17796 friend class N::C; // #2, invalid code
17799 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17800 name lookup of `N::C'. We see that friend declaration must
17801 be template for the code to be valid. Note that
17802 processing_template_decl does not work here since it is
17803 always 1 for the above two cases. */
17805 decl = (cp_parser_maybe_treat_template_as_class
17806 (decl, /*tag_name_p=*/is_friend
17807 && template_parm_lists_apply));
17809 if (TREE_CODE (decl) != TYPE_DECL)
17811 cp_parser_diagnose_invalid_type_name (parser,
17812 identifier,
17813 token->location);
17814 return error_mark_node;
17817 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17819 bool allow_template = (template_parm_lists_apply
17820 || DECL_SELF_REFERENCE_P (decl));
17821 type = check_elaborated_type_specifier (tag_type, decl,
17822 allow_template);
17824 if (type == error_mark_node)
17825 return error_mark_node;
17828 /* Forward declarations of nested types, such as
17830 class C1::C2;
17831 class C1::C2::C3;
17833 are invalid unless all components preceding the final '::'
17834 are complete. If all enclosing types are complete, these
17835 declarations become merely pointless.
17837 Invalid forward declarations of nested types are errors
17838 caught elsewhere in parsing. Those that are pointless arrive
17839 here. */
17841 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17842 && !is_friend && !processing_explicit_instantiation)
17843 warning (0, "declaration %qD does not declare anything", decl);
17845 type = TREE_TYPE (decl);
17847 else
17849 /* An elaborated-type-specifier sometimes introduces a new type and
17850 sometimes names an existing type. Normally, the rule is that it
17851 introduces a new type only if there is not an existing type of
17852 the same name already in scope. For example, given:
17854 struct S {};
17855 void f() { struct S s; }
17857 the `struct S' in the body of `f' is the same `struct S' as in
17858 the global scope; the existing definition is used. However, if
17859 there were no global declaration, this would introduce a new
17860 local class named `S'.
17862 An exception to this rule applies to the following code:
17864 namespace N { struct S; }
17866 Here, the elaborated-type-specifier names a new type
17867 unconditionally; even if there is already an `S' in the
17868 containing scope this declaration names a new type.
17869 This exception only applies if the elaborated-type-specifier
17870 forms the complete declaration:
17872 [class.name]
17874 A declaration consisting solely of `class-key identifier ;' is
17875 either a redeclaration of the name in the current scope or a
17876 forward declaration of the identifier as a class name. It
17877 introduces the name into the current scope.
17879 We are in this situation precisely when the next token is a `;'.
17881 An exception to the exception is that a `friend' declaration does
17882 *not* name a new type; i.e., given:
17884 struct S { friend struct T; };
17886 `T' is not a new type in the scope of `S'.
17888 Also, `new struct S' or `sizeof (struct S)' never results in the
17889 definition of a new type; a new type can only be declared in a
17890 declaration context. */
17892 tag_scope ts;
17893 bool template_p;
17895 if (is_friend)
17896 /* Friends have special name lookup rules. */
17897 ts = ts_within_enclosing_non_class;
17898 else if (is_declaration
17899 && cp_lexer_next_token_is (parser->lexer,
17900 CPP_SEMICOLON))
17901 /* This is a `class-key identifier ;' */
17902 ts = ts_current;
17903 else
17904 ts = ts_global;
17906 template_p =
17907 (template_parm_lists_apply
17908 && (cp_parser_next_token_starts_class_definition_p (parser)
17909 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17910 /* An unqualified name was used to reference this type, so
17911 there were no qualifying templates. */
17912 if (template_parm_lists_apply
17913 && !cp_parser_check_template_parameters (parser,
17914 /*num_templates=*/0,
17915 token->location,
17916 /*declarator=*/NULL))
17917 return error_mark_node;
17918 type = xref_tag (tag_type, identifier, ts, template_p);
17922 if (type == error_mark_node)
17923 return error_mark_node;
17925 /* Allow attributes on forward declarations of classes. */
17926 if (attributes)
17928 if (TREE_CODE (type) == TYPENAME_TYPE)
17929 warning (OPT_Wattributes,
17930 "attributes ignored on uninstantiated type");
17931 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17932 && ! processing_explicit_instantiation)
17933 warning (OPT_Wattributes,
17934 "attributes ignored on template instantiation");
17935 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17936 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17937 else
17938 warning (OPT_Wattributes,
17939 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17942 if (tag_type != enum_type)
17944 /* Indicate whether this class was declared as a `class' or as a
17945 `struct'. */
17946 if (CLASS_TYPE_P (type))
17947 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17948 cp_parser_check_class_key (tag_type, type);
17951 /* A "<" cannot follow an elaborated type specifier. If that
17952 happens, the user was probably trying to form a template-id. */
17953 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17954 token->location);
17956 return type;
17959 /* Parse an enum-specifier.
17961 enum-specifier:
17962 enum-head { enumerator-list [opt] }
17963 enum-head { enumerator-list , } [C++0x]
17965 enum-head:
17966 enum-key identifier [opt] enum-base [opt]
17967 enum-key nested-name-specifier identifier enum-base [opt]
17969 enum-key:
17970 enum
17971 enum class [C++0x]
17972 enum struct [C++0x]
17974 enum-base: [C++0x]
17975 : type-specifier-seq
17977 opaque-enum-specifier:
17978 enum-key identifier enum-base [opt] ;
17980 GNU Extensions:
17981 enum-key attributes[opt] identifier [opt] enum-base [opt]
17982 { enumerator-list [opt] }attributes[opt]
17983 enum-key attributes[opt] identifier [opt] enum-base [opt]
17984 { enumerator-list, }attributes[opt] [C++0x]
17986 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17987 if the token stream isn't an enum-specifier after all. */
17989 static tree
17990 cp_parser_enum_specifier (cp_parser* parser)
17992 tree identifier;
17993 tree type = NULL_TREE;
17994 tree prev_scope;
17995 tree nested_name_specifier = NULL_TREE;
17996 tree attributes;
17997 bool scoped_enum_p = false;
17998 bool has_underlying_type = false;
17999 bool nested_being_defined = false;
18000 bool new_value_list = false;
18001 bool is_new_type = false;
18002 bool is_unnamed = false;
18003 tree underlying_type = NULL_TREE;
18004 cp_token *type_start_token = NULL;
18005 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18007 parser->colon_corrects_to_scope_p = false;
18009 /* Parse tentatively so that we can back up if we don't find a
18010 enum-specifier. */
18011 cp_parser_parse_tentatively (parser);
18013 /* Caller guarantees that the current token is 'enum', an identifier
18014 possibly follows, and the token after that is an opening brace.
18015 If we don't have an identifier, fabricate an anonymous name for
18016 the enumeration being defined. */
18017 cp_lexer_consume_token (parser->lexer);
18019 /* Parse the "class" or "struct", which indicates a scoped
18020 enumeration type in C++0x. */
18021 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18022 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18024 if (cxx_dialect < cxx11)
18025 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18027 /* Consume the `struct' or `class' token. */
18028 cp_lexer_consume_token (parser->lexer);
18030 scoped_enum_p = true;
18033 attributes = cp_parser_attributes_opt (parser);
18035 /* Clear the qualification. */
18036 parser->scope = NULL_TREE;
18037 parser->qualifying_scope = NULL_TREE;
18038 parser->object_scope = NULL_TREE;
18040 /* Figure out in what scope the declaration is being placed. */
18041 prev_scope = current_scope ();
18043 type_start_token = cp_lexer_peek_token (parser->lexer);
18045 push_deferring_access_checks (dk_no_check);
18046 nested_name_specifier
18047 = cp_parser_nested_name_specifier_opt (parser,
18048 /*typename_keyword_p=*/true,
18049 /*check_dependency_p=*/false,
18050 /*type_p=*/false,
18051 /*is_declaration=*/false);
18053 if (nested_name_specifier)
18055 tree name;
18057 identifier = cp_parser_identifier (parser);
18058 name = cp_parser_lookup_name (parser, identifier,
18059 enum_type,
18060 /*is_template=*/false,
18061 /*is_namespace=*/false,
18062 /*check_dependency=*/true,
18063 /*ambiguous_decls=*/NULL,
18064 input_location);
18065 if (name && name != error_mark_node)
18067 type = TREE_TYPE (name);
18068 if (TREE_CODE (type) == TYPENAME_TYPE)
18070 /* Are template enums allowed in ISO? */
18071 if (template_parm_scope_p ())
18072 pedwarn (type_start_token->location, OPT_Wpedantic,
18073 "%qD is an enumeration template", name);
18074 /* ignore a typename reference, for it will be solved by name
18075 in start_enum. */
18076 type = NULL_TREE;
18079 else if (nested_name_specifier == error_mark_node)
18080 /* We already issued an error. */;
18081 else
18083 error_at (type_start_token->location,
18084 "%qD does not name an enumeration in %qT",
18085 identifier, nested_name_specifier);
18086 nested_name_specifier = error_mark_node;
18089 else
18091 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18092 identifier = cp_parser_identifier (parser);
18093 else
18095 identifier = make_anon_name ();
18096 is_unnamed = true;
18097 if (scoped_enum_p)
18098 error_at (type_start_token->location,
18099 "unnamed scoped enum is not allowed");
18102 pop_deferring_access_checks ();
18104 /* Check for the `:' that denotes a specified underlying type in C++0x.
18105 Note that a ':' could also indicate a bitfield width, however. */
18106 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18108 cp_decl_specifier_seq type_specifiers;
18110 /* Consume the `:'. */
18111 cp_lexer_consume_token (parser->lexer);
18113 /* Parse the type-specifier-seq. */
18114 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18115 /*is_trailing_return=*/false,
18116 &type_specifiers);
18118 /* At this point this is surely not elaborated type specifier. */
18119 if (!cp_parser_parse_definitely (parser))
18120 return NULL_TREE;
18122 if (cxx_dialect < cxx11)
18123 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18125 has_underlying_type = true;
18127 /* If that didn't work, stop. */
18128 if (type_specifiers.type != error_mark_node)
18130 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18131 /*initialized=*/0, NULL);
18132 if (underlying_type == error_mark_node
18133 || check_for_bare_parameter_packs (underlying_type))
18134 underlying_type = NULL_TREE;
18138 /* Look for the `{' but don't consume it yet. */
18139 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18141 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18143 cp_parser_error (parser, "expected %<{%>");
18144 if (has_underlying_type)
18146 type = NULL_TREE;
18147 goto out;
18150 /* An opaque-enum-specifier must have a ';' here. */
18151 if ((scoped_enum_p || underlying_type)
18152 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18154 cp_parser_error (parser, "expected %<;%> or %<{%>");
18155 if (has_underlying_type)
18157 type = NULL_TREE;
18158 goto out;
18163 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18164 return NULL_TREE;
18166 if (nested_name_specifier)
18168 if (CLASS_TYPE_P (nested_name_specifier))
18170 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18171 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18172 push_scope (nested_name_specifier);
18174 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18176 push_nested_namespace (nested_name_specifier);
18180 /* Issue an error message if type-definitions are forbidden here. */
18181 if (!cp_parser_check_type_definition (parser))
18182 type = error_mark_node;
18183 else
18184 /* Create the new type. We do this before consuming the opening
18185 brace so the enum will be recorded as being on the line of its
18186 tag (or the 'enum' keyword, if there is no tag). */
18187 type = start_enum (identifier, type, underlying_type,
18188 attributes, scoped_enum_p, &is_new_type);
18190 /* If the next token is not '{' it is an opaque-enum-specifier or an
18191 elaborated-type-specifier. */
18192 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18194 timevar_push (TV_PARSE_ENUM);
18195 if (nested_name_specifier
18196 && nested_name_specifier != error_mark_node)
18198 /* The following catches invalid code such as:
18199 enum class S<int>::E { A, B, C }; */
18200 if (!processing_specialization
18201 && CLASS_TYPE_P (nested_name_specifier)
18202 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18203 error_at (type_start_token->location, "cannot add an enumerator "
18204 "list to a template instantiation");
18206 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18208 error_at (type_start_token->location,
18209 "%<%T::%E%> has not been declared",
18210 TYPE_CONTEXT (nested_name_specifier),
18211 nested_name_specifier);
18212 type = error_mark_node;
18214 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18215 && !CLASS_TYPE_P (nested_name_specifier))
18217 error_at (type_start_token->location, "nested name specifier "
18218 "%qT for enum declaration does not name a class "
18219 "or namespace", nested_name_specifier);
18220 type = error_mark_node;
18222 /* If that scope does not contain the scope in which the
18223 class was originally declared, the program is invalid. */
18224 else if (prev_scope && !is_ancestor (prev_scope,
18225 nested_name_specifier))
18227 if (at_namespace_scope_p ())
18228 error_at (type_start_token->location,
18229 "declaration of %qD in namespace %qD which does not "
18230 "enclose %qD",
18231 type, prev_scope, nested_name_specifier);
18232 else
18233 error_at (type_start_token->location,
18234 "declaration of %qD in %qD which does not "
18235 "enclose %qD",
18236 type, prev_scope, nested_name_specifier);
18237 type = error_mark_node;
18239 /* If that scope is the scope where the declaration is being placed
18240 the program is invalid. */
18241 else if (CLASS_TYPE_P (nested_name_specifier)
18242 && CLASS_TYPE_P (prev_scope)
18243 && same_type_p (nested_name_specifier, prev_scope))
18245 permerror (type_start_token->location,
18246 "extra qualification not allowed");
18247 nested_name_specifier = NULL_TREE;
18251 if (scoped_enum_p)
18252 begin_scope (sk_scoped_enum, type);
18254 /* Consume the opening brace. */
18255 matching_braces braces;
18256 braces.consume_open (parser);
18258 if (type == error_mark_node)
18259 ; /* Nothing to add */
18260 else if (OPAQUE_ENUM_P (type)
18261 || (cxx_dialect > cxx98 && processing_specialization))
18263 new_value_list = true;
18264 SET_OPAQUE_ENUM_P (type, false);
18265 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18267 else
18269 error_at (type_start_token->location,
18270 "multiple definition of %q#T", type);
18271 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18272 "previous definition here");
18273 type = error_mark_node;
18276 if (type == error_mark_node)
18277 cp_parser_skip_to_end_of_block_or_statement (parser);
18278 /* If the next token is not '}', then there are some enumerators. */
18279 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18281 if (is_unnamed && !scoped_enum_p)
18282 pedwarn (type_start_token->location, OPT_Wpedantic,
18283 "ISO C++ forbids empty unnamed enum");
18285 else
18286 cp_parser_enumerator_list (parser, type);
18288 /* Consume the final '}'. */
18289 braces.require_close (parser);
18291 if (scoped_enum_p)
18292 finish_scope ();
18293 timevar_pop (TV_PARSE_ENUM);
18295 else
18297 /* If a ';' follows, then it is an opaque-enum-specifier
18298 and additional restrictions apply. */
18299 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18301 if (is_unnamed)
18302 error_at (type_start_token->location,
18303 "opaque-enum-specifier without name");
18304 else if (nested_name_specifier)
18305 error_at (type_start_token->location,
18306 "opaque-enum-specifier must use a simple identifier");
18310 /* Look for trailing attributes to apply to this enumeration, and
18311 apply them if appropriate. */
18312 if (cp_parser_allow_gnu_extensions_p (parser))
18314 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18315 cplus_decl_attributes (&type,
18316 trailing_attr,
18317 (int) ATTR_FLAG_TYPE_IN_PLACE);
18320 /* Finish up the enumeration. */
18321 if (type != error_mark_node)
18323 if (new_value_list)
18324 finish_enum_value_list (type);
18325 if (is_new_type)
18326 finish_enum (type);
18329 if (nested_name_specifier)
18331 if (CLASS_TYPE_P (nested_name_specifier))
18333 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18334 pop_scope (nested_name_specifier);
18336 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18338 pop_nested_namespace (nested_name_specifier);
18341 out:
18342 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18343 return type;
18346 /* Parse an enumerator-list. The enumerators all have the indicated
18347 TYPE.
18349 enumerator-list:
18350 enumerator-definition
18351 enumerator-list , enumerator-definition */
18353 static void
18354 cp_parser_enumerator_list (cp_parser* parser, tree type)
18356 while (true)
18358 /* Parse an enumerator-definition. */
18359 cp_parser_enumerator_definition (parser, type);
18361 /* If the next token is not a ',', we've reached the end of
18362 the list. */
18363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18364 break;
18365 /* Otherwise, consume the `,' and keep going. */
18366 cp_lexer_consume_token (parser->lexer);
18367 /* If the next token is a `}', there is a trailing comma. */
18368 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18370 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18371 pedwarn (input_location, OPT_Wpedantic,
18372 "comma at end of enumerator list");
18373 break;
18378 /* Parse an enumerator-definition. The enumerator has the indicated
18379 TYPE.
18381 enumerator-definition:
18382 enumerator
18383 enumerator = constant-expression
18385 enumerator:
18386 identifier
18388 GNU Extensions:
18390 enumerator-definition:
18391 enumerator attributes [opt]
18392 enumerator attributes [opt] = constant-expression */
18394 static void
18395 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18397 tree identifier;
18398 tree value;
18399 location_t loc;
18401 /* Save the input location because we are interested in the location
18402 of the identifier and not the location of the explicit value. */
18403 loc = cp_lexer_peek_token (parser->lexer)->location;
18405 /* Look for the identifier. */
18406 identifier = cp_parser_identifier (parser);
18407 if (identifier == error_mark_node)
18408 return;
18410 /* Parse any specified attributes. */
18411 tree attrs = cp_parser_attributes_opt (parser);
18413 /* If the next token is an '=', then there is an explicit value. */
18414 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18416 /* Consume the `=' token. */
18417 cp_lexer_consume_token (parser->lexer);
18418 /* Parse the value. */
18419 value = cp_parser_constant_expression (parser);
18421 else
18422 value = NULL_TREE;
18424 /* If we are processing a template, make sure the initializer of the
18425 enumerator doesn't contain any bare template parameter pack. */
18426 if (check_for_bare_parameter_packs (value))
18427 value = error_mark_node;
18429 /* Create the enumerator. */
18430 build_enumerator (identifier, value, type, attrs, loc);
18433 /* Parse a namespace-name.
18435 namespace-name:
18436 original-namespace-name
18437 namespace-alias
18439 Returns the NAMESPACE_DECL for the namespace. */
18441 static tree
18442 cp_parser_namespace_name (cp_parser* parser)
18444 tree identifier;
18445 tree namespace_decl;
18447 cp_token *token = cp_lexer_peek_token (parser->lexer);
18449 /* Get the name of the namespace. */
18450 identifier = cp_parser_identifier (parser);
18451 if (identifier == error_mark_node)
18452 return error_mark_node;
18454 /* Look up the identifier in the currently active scope. Look only
18455 for namespaces, due to:
18457 [basic.lookup.udir]
18459 When looking up a namespace-name in a using-directive or alias
18460 definition, only namespace names are considered.
18462 And:
18464 [basic.lookup.qual]
18466 During the lookup of a name preceding the :: scope resolution
18467 operator, object, function, and enumerator names are ignored.
18469 (Note that cp_parser_qualifying_entity only calls this
18470 function if the token after the name is the scope resolution
18471 operator.) */
18472 namespace_decl = cp_parser_lookup_name (parser, identifier,
18473 none_type,
18474 /*is_template=*/false,
18475 /*is_namespace=*/true,
18476 /*check_dependency=*/true,
18477 /*ambiguous_decls=*/NULL,
18478 token->location);
18479 /* If it's not a namespace, issue an error. */
18480 if (namespace_decl == error_mark_node
18481 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18483 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18485 error_at (token->location, "%qD is not a namespace-name", identifier);
18486 if (namespace_decl == error_mark_node
18487 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18488 suggest_alternative_in_explicit_scope (token->location, identifier,
18489 parser->scope);
18491 cp_parser_error (parser, "expected namespace-name");
18492 namespace_decl = error_mark_node;
18495 return namespace_decl;
18498 /* Parse a namespace-definition.
18500 namespace-definition:
18501 named-namespace-definition
18502 unnamed-namespace-definition
18504 named-namespace-definition:
18505 original-namespace-definition
18506 extension-namespace-definition
18508 original-namespace-definition:
18509 namespace identifier { namespace-body }
18511 extension-namespace-definition:
18512 namespace original-namespace-name { namespace-body }
18514 unnamed-namespace-definition:
18515 namespace { namespace-body } */
18517 static void
18518 cp_parser_namespace_definition (cp_parser* parser)
18520 tree identifier;
18521 int nested_definition_count = 0;
18523 cp_ensure_no_omp_declare_simd (parser);
18524 cp_ensure_no_oacc_routine (parser);
18526 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18528 if (is_inline)
18530 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18531 cp_lexer_consume_token (parser->lexer);
18534 /* Look for the `namespace' keyword. */
18535 cp_token* token
18536 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18538 /* Parse any specified attributes before the identifier. */
18539 tree attribs = cp_parser_attributes_opt (parser);
18541 for (;;)
18543 identifier = NULL_TREE;
18545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18547 identifier = cp_parser_identifier (parser);
18549 /* Parse any attributes specified after the identifier. */
18550 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18553 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18554 break;
18556 if (!nested_definition_count && cxx_dialect < cxx17)
18557 pedwarn (input_location, OPT_Wpedantic,
18558 "nested namespace definitions only available with "
18559 "-std=c++17 or -std=gnu++17");
18561 /* Nested namespace names can create new namespaces (unlike
18562 other qualified-ids). */
18563 if (int count = identifier ? push_namespace (identifier) : 0)
18564 nested_definition_count += count;
18565 else
18566 cp_parser_error (parser, "nested namespace name required");
18567 cp_lexer_consume_token (parser->lexer);
18570 if (nested_definition_count && !identifier)
18571 cp_parser_error (parser, "namespace name required");
18573 if (nested_definition_count && attribs)
18574 error_at (token->location,
18575 "a nested namespace definition cannot have attributes");
18576 if (nested_definition_count && is_inline)
18577 error_at (token->location,
18578 "a nested namespace definition cannot be inline");
18580 /* Start the namespace. */
18581 nested_definition_count += push_namespace (identifier, is_inline);
18583 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18585 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18587 /* Look for the `{' to validate starting the namespace. */
18588 matching_braces braces;
18589 if (braces.require_open (parser))
18591 /* Parse the body of the namespace. */
18592 cp_parser_namespace_body (parser);
18594 /* Look for the final `}'. */
18595 braces.require_close (parser);
18598 if (has_visibility)
18599 pop_visibility (1);
18601 /* Pop the nested namespace definitions. */
18602 while (nested_definition_count--)
18603 pop_namespace ();
18606 /* Parse a namespace-body.
18608 namespace-body:
18609 declaration-seq [opt] */
18611 static void
18612 cp_parser_namespace_body (cp_parser* parser)
18614 cp_parser_declaration_seq_opt (parser);
18617 /* Parse a namespace-alias-definition.
18619 namespace-alias-definition:
18620 namespace identifier = qualified-namespace-specifier ; */
18622 static void
18623 cp_parser_namespace_alias_definition (cp_parser* parser)
18625 tree identifier;
18626 tree namespace_specifier;
18628 cp_token *token = cp_lexer_peek_token (parser->lexer);
18630 /* Look for the `namespace' keyword. */
18631 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18632 /* Look for the identifier. */
18633 identifier = cp_parser_identifier (parser);
18634 if (identifier == error_mark_node)
18635 return;
18636 /* Look for the `=' token. */
18637 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18638 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18640 error_at (token->location, "%<namespace%> definition is not allowed here");
18641 /* Skip the definition. */
18642 cp_lexer_consume_token (parser->lexer);
18643 if (cp_parser_skip_to_closing_brace (parser))
18644 cp_lexer_consume_token (parser->lexer);
18645 return;
18647 cp_parser_require (parser, CPP_EQ, RT_EQ);
18648 /* Look for the qualified-namespace-specifier. */
18649 namespace_specifier
18650 = cp_parser_qualified_namespace_specifier (parser);
18651 /* Look for the `;' token. */
18652 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18654 /* Register the alias in the symbol table. */
18655 do_namespace_alias (identifier, namespace_specifier);
18658 /* Parse a qualified-namespace-specifier.
18660 qualified-namespace-specifier:
18661 :: [opt] nested-name-specifier [opt] namespace-name
18663 Returns a NAMESPACE_DECL corresponding to the specified
18664 namespace. */
18666 static tree
18667 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18669 /* Look for the optional `::'. */
18670 cp_parser_global_scope_opt (parser,
18671 /*current_scope_valid_p=*/false);
18673 /* Look for the optional nested-name-specifier. */
18674 cp_parser_nested_name_specifier_opt (parser,
18675 /*typename_keyword_p=*/false,
18676 /*check_dependency_p=*/true,
18677 /*type_p=*/false,
18678 /*is_declaration=*/true);
18680 return cp_parser_namespace_name (parser);
18683 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18684 access declaration.
18686 using-declaration:
18687 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18688 using :: unqualified-id ;
18690 access-declaration:
18691 qualified-id ;
18695 static bool
18696 cp_parser_using_declaration (cp_parser* parser,
18697 bool access_declaration_p)
18699 cp_token *token;
18700 bool typename_p = false;
18701 bool global_scope_p;
18702 tree decl;
18703 tree identifier;
18704 tree qscope;
18705 int oldcount = errorcount;
18706 cp_token *diag_token = NULL;
18708 if (access_declaration_p)
18710 diag_token = cp_lexer_peek_token (parser->lexer);
18711 cp_parser_parse_tentatively (parser);
18713 else
18715 /* Look for the `using' keyword. */
18716 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18718 again:
18719 /* Peek at the next token. */
18720 token = cp_lexer_peek_token (parser->lexer);
18721 /* See if it's `typename'. */
18722 if (token->keyword == RID_TYPENAME)
18724 /* Remember that we've seen it. */
18725 typename_p = true;
18726 /* Consume the `typename' token. */
18727 cp_lexer_consume_token (parser->lexer);
18731 /* Look for the optional global scope qualification. */
18732 global_scope_p
18733 = (cp_parser_global_scope_opt (parser,
18734 /*current_scope_valid_p=*/false)
18735 != NULL_TREE);
18737 /* If we saw `typename', or didn't see `::', then there must be a
18738 nested-name-specifier present. */
18739 if (typename_p || !global_scope_p)
18741 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18742 /*check_dependency_p=*/true,
18743 /*type_p=*/false,
18744 /*is_declaration=*/true);
18745 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18747 cp_parser_skip_to_end_of_block_or_statement (parser);
18748 return false;
18751 /* Otherwise, we could be in either of the two productions. In that
18752 case, treat the nested-name-specifier as optional. */
18753 else
18754 qscope = cp_parser_nested_name_specifier_opt (parser,
18755 /*typename_keyword_p=*/false,
18756 /*check_dependency_p=*/true,
18757 /*type_p=*/false,
18758 /*is_declaration=*/true);
18759 if (!qscope)
18760 qscope = global_namespace;
18761 else if (UNSCOPED_ENUM_P (qscope))
18762 qscope = CP_TYPE_CONTEXT (qscope);
18764 if (access_declaration_p && cp_parser_error_occurred (parser))
18765 /* Something has already gone wrong; there's no need to parse
18766 further. Since an error has occurred, the return value of
18767 cp_parser_parse_definitely will be false, as required. */
18768 return cp_parser_parse_definitely (parser);
18770 token = cp_lexer_peek_token (parser->lexer);
18771 /* Parse the unqualified-id. */
18772 identifier = cp_parser_unqualified_id (parser,
18773 /*template_keyword_p=*/false,
18774 /*check_dependency_p=*/true,
18775 /*declarator_p=*/true,
18776 /*optional_p=*/false);
18778 if (access_declaration_p)
18780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18781 cp_parser_simulate_error (parser);
18782 if (!cp_parser_parse_definitely (parser))
18783 return false;
18785 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18787 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18788 if (cxx_dialect < cxx17
18789 && !in_system_header_at (ell->location))
18790 pedwarn (ell->location, 0,
18791 "pack expansion in using-declaration only available "
18792 "with -std=c++17 or -std=gnu++17");
18793 qscope = make_pack_expansion (qscope);
18796 /* The function we call to handle a using-declaration is different
18797 depending on what scope we are in. */
18798 if (qscope == error_mark_node || identifier == error_mark_node)
18800 else if (!identifier_p (identifier)
18801 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18802 /* [namespace.udecl]
18804 A using declaration shall not name a template-id. */
18805 error_at (token->location,
18806 "a template-id may not appear in a using-declaration");
18807 else
18809 if (at_class_scope_p ())
18811 /* Create the USING_DECL. */
18812 decl = do_class_using_decl (qscope, identifier);
18814 if (decl && typename_p)
18815 USING_DECL_TYPENAME_P (decl) = 1;
18817 if (check_for_bare_parameter_packs (decl))
18819 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18820 return false;
18822 else
18823 /* Add it to the list of members in this class. */
18824 finish_member_declaration (decl);
18826 else
18828 decl = cp_parser_lookup_name_simple (parser,
18829 identifier,
18830 token->location);
18831 if (decl == error_mark_node)
18832 cp_parser_name_lookup_error (parser, identifier,
18833 decl, NLE_NULL,
18834 token->location);
18835 else if (check_for_bare_parameter_packs (decl))
18837 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18838 return false;
18840 else if (!at_namespace_scope_p ())
18841 finish_local_using_decl (decl, qscope, identifier);
18842 else
18843 finish_namespace_using_decl (decl, qscope, identifier);
18847 if (!access_declaration_p
18848 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18850 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18851 if (cxx_dialect < cxx17)
18852 pedwarn (comma->location, 0,
18853 "comma-separated list in using-declaration only available "
18854 "with -std=c++17 or -std=gnu++17");
18855 goto again;
18858 /* Look for the final `;'. */
18859 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18861 if (access_declaration_p && errorcount == oldcount)
18862 warning_at (diag_token->location, OPT_Wdeprecated,
18863 "access declarations are deprecated "
18864 "in favour of using-declarations; "
18865 "suggestion: add the %<using%> keyword");
18867 return true;
18870 /* Parse an alias-declaration.
18872 alias-declaration:
18873 using identifier attribute-specifier-seq [opt] = type-id */
18875 static tree
18876 cp_parser_alias_declaration (cp_parser* parser)
18878 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18879 location_t id_location;
18880 cp_declarator *declarator;
18881 cp_decl_specifier_seq decl_specs;
18882 bool member_p;
18883 const char *saved_message = NULL;
18885 /* Look for the `using' keyword. */
18886 cp_token *using_token
18887 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18888 if (using_token == NULL)
18889 return error_mark_node;
18891 id_location = cp_lexer_peek_token (parser->lexer)->location;
18892 id = cp_parser_identifier (parser);
18893 if (id == error_mark_node)
18894 return error_mark_node;
18896 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18897 attributes = cp_parser_attributes_opt (parser);
18898 if (attributes == error_mark_node)
18899 return error_mark_node;
18901 cp_parser_require (parser, CPP_EQ, RT_EQ);
18903 if (cp_parser_error_occurred (parser))
18904 return error_mark_node;
18906 cp_parser_commit_to_tentative_parse (parser);
18908 /* Now we are going to parse the type-id of the declaration. */
18911 [dcl.type]/3 says:
18913 "A type-specifier-seq shall not define a class or enumeration
18914 unless it appears in the type-id of an alias-declaration (7.1.3) that
18915 is not the declaration of a template-declaration."
18917 In other words, if we currently are in an alias template, the
18918 type-id should not define a type.
18920 So let's set parser->type_definition_forbidden_message in that
18921 case; cp_parser_check_type_definition (called by
18922 cp_parser_class_specifier) will then emit an error if a type is
18923 defined in the type-id. */
18924 if (parser->num_template_parameter_lists)
18926 saved_message = parser->type_definition_forbidden_message;
18927 parser->type_definition_forbidden_message =
18928 G_("types may not be defined in alias template declarations");
18931 type = cp_parser_type_id (parser);
18933 /* Restore the error message if need be. */
18934 if (parser->num_template_parameter_lists)
18935 parser->type_definition_forbidden_message = saved_message;
18937 if (type == error_mark_node
18938 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18940 cp_parser_skip_to_end_of_block_or_statement (parser);
18941 return error_mark_node;
18944 /* A typedef-name can also be introduced by an alias-declaration. The
18945 identifier following the using keyword becomes a typedef-name. It has
18946 the same semantics as if it were introduced by the typedef
18947 specifier. In particular, it does not define a new type and it shall
18948 not appear in the type-id. */
18950 clear_decl_specs (&decl_specs);
18951 decl_specs.type = type;
18952 if (attributes != NULL_TREE)
18954 decl_specs.attributes = attributes;
18955 set_and_check_decl_spec_loc (&decl_specs,
18956 ds_attribute,
18957 attrs_token);
18959 set_and_check_decl_spec_loc (&decl_specs,
18960 ds_typedef,
18961 using_token);
18962 set_and_check_decl_spec_loc (&decl_specs,
18963 ds_alias,
18964 using_token);
18966 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18967 declarator->id_loc = id_location;
18969 member_p = at_class_scope_p ();
18970 if (member_p)
18971 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18972 NULL_TREE, attributes);
18973 else
18974 decl = start_decl (declarator, &decl_specs, 0,
18975 attributes, NULL_TREE, &pushed_scope);
18976 if (decl == error_mark_node)
18977 return decl;
18979 // Attach constraints to the alias declaration.
18980 if (flag_concepts && current_template_parms)
18982 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18983 tree constr = build_constraints (reqs, NULL_TREE);
18984 set_constraints (decl, constr);
18987 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18989 if (pushed_scope)
18990 pop_scope (pushed_scope);
18992 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18993 added into the symbol table; otherwise, return the TYPE_DECL. */
18994 if (DECL_LANG_SPECIFIC (decl)
18995 && DECL_TEMPLATE_INFO (decl)
18996 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18998 decl = DECL_TI_TEMPLATE (decl);
18999 if (member_p)
19000 check_member_template (decl);
19003 return decl;
19006 /* Parse a using-directive.
19008 using-directive:
19009 using namespace :: [opt] nested-name-specifier [opt]
19010 namespace-name ; */
19012 static void
19013 cp_parser_using_directive (cp_parser* parser)
19015 tree namespace_decl;
19016 tree attribs;
19018 /* Look for the `using' keyword. */
19019 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19020 /* And the `namespace' keyword. */
19021 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19022 /* Look for the optional `::' operator. */
19023 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19024 /* And the optional nested-name-specifier. */
19025 cp_parser_nested_name_specifier_opt (parser,
19026 /*typename_keyword_p=*/false,
19027 /*check_dependency_p=*/true,
19028 /*type_p=*/false,
19029 /*is_declaration=*/true);
19030 /* Get the namespace being used. */
19031 namespace_decl = cp_parser_namespace_name (parser);
19032 /* And any specified attributes. */
19033 attribs = cp_parser_attributes_opt (parser);
19035 /* Update the symbol table. */
19036 if (namespace_bindings_p ())
19037 finish_namespace_using_directive (namespace_decl, attribs);
19038 else
19039 finish_local_using_directive (namespace_decl, attribs);
19041 /* Look for the final `;'. */
19042 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19045 /* Parse an asm-definition.
19047 asm-definition:
19048 asm ( string-literal ) ;
19050 GNU Extension:
19052 asm-definition:
19053 asm volatile [opt] ( string-literal ) ;
19054 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19055 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19056 : asm-operand-list [opt] ) ;
19057 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19058 : asm-operand-list [opt]
19059 : asm-clobber-list [opt] ) ;
19060 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19061 : asm-clobber-list [opt]
19062 : asm-goto-list ) ; */
19064 static void
19065 cp_parser_asm_definition (cp_parser* parser)
19067 tree string;
19068 tree outputs = NULL_TREE;
19069 tree inputs = NULL_TREE;
19070 tree clobbers = NULL_TREE;
19071 tree labels = NULL_TREE;
19072 tree asm_stmt;
19073 bool volatile_p = false;
19074 bool extended_p = false;
19075 bool invalid_inputs_p = false;
19076 bool invalid_outputs_p = false;
19077 bool goto_p = false;
19078 required_token missing = RT_NONE;
19080 /* Look for the `asm' keyword. */
19081 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19083 if (parser->in_function_body
19084 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19086 error ("%<asm%> in %<constexpr%> function");
19087 cp_function_chain->invalid_constexpr = true;
19090 /* See if the next token is `volatile'. */
19091 if (cp_parser_allow_gnu_extensions_p (parser)
19092 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19094 /* Remember that we saw the `volatile' keyword. */
19095 volatile_p = true;
19096 /* Consume the token. */
19097 cp_lexer_consume_token (parser->lexer);
19099 if (cp_parser_allow_gnu_extensions_p (parser)
19100 && parser->in_function_body
19101 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19103 /* Remember that we saw the `goto' keyword. */
19104 goto_p = true;
19105 /* Consume the token. */
19106 cp_lexer_consume_token (parser->lexer);
19108 /* Look for the opening `('. */
19109 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19110 return;
19111 /* Look for the string. */
19112 string = cp_parser_string_literal (parser, false, false);
19113 if (string == error_mark_node)
19115 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19116 /*consume_paren=*/true);
19117 return;
19120 /* If we're allowing GNU extensions, check for the extended assembly
19121 syntax. Unfortunately, the `:' tokens need not be separated by
19122 a space in C, and so, for compatibility, we tolerate that here
19123 too. Doing that means that we have to treat the `::' operator as
19124 two `:' tokens. */
19125 if (cp_parser_allow_gnu_extensions_p (parser)
19126 && parser->in_function_body
19127 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19128 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19130 bool inputs_p = false;
19131 bool clobbers_p = false;
19132 bool labels_p = false;
19134 /* The extended syntax was used. */
19135 extended_p = true;
19137 /* Look for outputs. */
19138 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19140 /* Consume the `:'. */
19141 cp_lexer_consume_token (parser->lexer);
19142 /* Parse the output-operands. */
19143 if (cp_lexer_next_token_is_not (parser->lexer,
19144 CPP_COLON)
19145 && cp_lexer_next_token_is_not (parser->lexer,
19146 CPP_SCOPE)
19147 && cp_lexer_next_token_is_not (parser->lexer,
19148 CPP_CLOSE_PAREN)
19149 && !goto_p)
19151 outputs = cp_parser_asm_operand_list (parser);
19152 if (outputs == error_mark_node)
19153 invalid_outputs_p = true;
19156 /* If the next token is `::', there are no outputs, and the
19157 next token is the beginning of the inputs. */
19158 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19159 /* The inputs are coming next. */
19160 inputs_p = true;
19162 /* Look for inputs. */
19163 if (inputs_p
19164 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19166 /* Consume the `:' or `::'. */
19167 cp_lexer_consume_token (parser->lexer);
19168 /* Parse the output-operands. */
19169 if (cp_lexer_next_token_is_not (parser->lexer,
19170 CPP_COLON)
19171 && cp_lexer_next_token_is_not (parser->lexer,
19172 CPP_SCOPE)
19173 && cp_lexer_next_token_is_not (parser->lexer,
19174 CPP_CLOSE_PAREN))
19176 inputs = cp_parser_asm_operand_list (parser);
19177 if (inputs == error_mark_node)
19178 invalid_inputs_p = true;
19181 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19182 /* The clobbers are coming next. */
19183 clobbers_p = true;
19185 /* Look for clobbers. */
19186 if (clobbers_p
19187 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19189 clobbers_p = true;
19190 /* Consume the `:' or `::'. */
19191 cp_lexer_consume_token (parser->lexer);
19192 /* Parse the clobbers. */
19193 if (cp_lexer_next_token_is_not (parser->lexer,
19194 CPP_COLON)
19195 && cp_lexer_next_token_is_not (parser->lexer,
19196 CPP_CLOSE_PAREN))
19197 clobbers = cp_parser_asm_clobber_list (parser);
19199 else if (goto_p
19200 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19201 /* The labels are coming next. */
19202 labels_p = true;
19204 /* Look for labels. */
19205 if (labels_p
19206 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19208 labels_p = true;
19209 /* Consume the `:' or `::'. */
19210 cp_lexer_consume_token (parser->lexer);
19211 /* Parse the labels. */
19212 labels = cp_parser_asm_label_list (parser);
19215 if (goto_p && !labels_p)
19216 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19218 else if (goto_p)
19219 missing = RT_COLON_SCOPE;
19221 /* Look for the closing `)'. */
19222 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19223 missing ? missing : RT_CLOSE_PAREN))
19224 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19225 /*consume_paren=*/true);
19226 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19228 if (!invalid_inputs_p && !invalid_outputs_p)
19230 /* Create the ASM_EXPR. */
19231 if (parser->in_function_body)
19233 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19234 inputs, clobbers, labels);
19235 /* If the extended syntax was not used, mark the ASM_EXPR. */
19236 if (!extended_p)
19238 tree temp = asm_stmt;
19239 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19240 temp = TREE_OPERAND (temp, 0);
19242 ASM_INPUT_P (temp) = 1;
19245 else
19246 symtab->finalize_toplevel_asm (string);
19250 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19251 type that comes from the decl-specifier-seq. */
19253 static tree
19254 strip_declarator_types (tree type, cp_declarator *declarator)
19256 for (cp_declarator *d = declarator; d;)
19257 switch (d->kind)
19259 case cdk_id:
19260 case cdk_decomp:
19261 case cdk_error:
19262 d = NULL;
19263 break;
19265 default:
19266 if (TYPE_PTRMEMFUNC_P (type))
19267 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19268 type = TREE_TYPE (type);
19269 d = d->declarator;
19270 break;
19273 return type;
19276 /* Declarators [gram.dcl.decl] */
19278 /* Parse an init-declarator.
19280 init-declarator:
19281 declarator initializer [opt]
19283 GNU Extension:
19285 init-declarator:
19286 declarator asm-specification [opt] attributes [opt] initializer [opt]
19288 function-definition:
19289 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19290 function-body
19291 decl-specifier-seq [opt] declarator function-try-block
19293 GNU Extension:
19295 function-definition:
19296 __extension__ function-definition
19298 TM Extension:
19300 function-definition:
19301 decl-specifier-seq [opt] declarator function-transaction-block
19303 The DECL_SPECIFIERS apply to this declarator. Returns a
19304 representation of the entity declared. If MEMBER_P is TRUE, then
19305 this declarator appears in a class scope. The new DECL created by
19306 this declarator is returned.
19308 The CHECKS are access checks that should be performed once we know
19309 what entity is being declared (and, therefore, what classes have
19310 befriended it).
19312 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19313 for a function-definition here as well. If the declarator is a
19314 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19315 be TRUE upon return. By that point, the function-definition will
19316 have been completely parsed.
19318 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19319 is FALSE.
19321 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19322 parsed declaration if it is an uninitialized single declarator not followed
19323 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19324 if present, will not be consumed. If returned, this declarator will be
19325 created with SD_INITIALIZED but will not call cp_finish_decl.
19327 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19328 and there is an initializer, the pointed location_t is set to the
19329 location of the '=' or `(', or '{' in C++11 token introducing the
19330 initializer. */
19332 static tree
19333 cp_parser_init_declarator (cp_parser* parser,
19334 cp_decl_specifier_seq *decl_specifiers,
19335 vec<deferred_access_check, va_gc> *checks,
19336 bool function_definition_allowed_p,
19337 bool member_p,
19338 int declares_class_or_enum,
19339 bool* function_definition_p,
19340 tree* maybe_range_for_decl,
19341 location_t* init_loc,
19342 tree* auto_result)
19344 cp_token *token = NULL, *asm_spec_start_token = NULL,
19345 *attributes_start_token = NULL;
19346 cp_declarator *declarator;
19347 tree prefix_attributes;
19348 tree attributes = NULL;
19349 tree asm_specification;
19350 tree initializer;
19351 tree decl = NULL_TREE;
19352 tree scope;
19353 int is_initialized;
19354 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19355 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19356 "(...)". */
19357 enum cpp_ttype initialization_kind;
19358 bool is_direct_init = false;
19359 bool is_non_constant_init;
19360 int ctor_dtor_or_conv_p;
19361 bool friend_p = cp_parser_friend_p (decl_specifiers);
19362 tree pushed_scope = NULL_TREE;
19363 bool range_for_decl_p = false;
19364 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19365 location_t tmp_init_loc = UNKNOWN_LOCATION;
19367 /* Gather the attributes that were provided with the
19368 decl-specifiers. */
19369 prefix_attributes = decl_specifiers->attributes;
19371 /* Assume that this is not the declarator for a function
19372 definition. */
19373 if (function_definition_p)
19374 *function_definition_p = false;
19376 /* Default arguments are only permitted for function parameters. */
19377 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19378 parser->default_arg_ok_p = false;
19380 /* Defer access checks while parsing the declarator; we cannot know
19381 what names are accessible until we know what is being
19382 declared. */
19383 resume_deferring_access_checks ();
19385 token = cp_lexer_peek_token (parser->lexer);
19387 /* Parse the declarator. */
19388 declarator
19389 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19390 &ctor_dtor_or_conv_p,
19391 /*parenthesized_p=*/NULL,
19392 member_p, friend_p);
19393 /* Gather up the deferred checks. */
19394 stop_deferring_access_checks ();
19396 parser->default_arg_ok_p = saved_default_arg_ok_p;
19398 /* If the DECLARATOR was erroneous, there's no need to go
19399 further. */
19400 if (declarator == cp_error_declarator)
19401 return error_mark_node;
19403 /* Check that the number of template-parameter-lists is OK. */
19404 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19405 token->location))
19406 return error_mark_node;
19408 if (declares_class_or_enum & 2)
19409 cp_parser_check_for_definition_in_return_type (declarator,
19410 decl_specifiers->type,
19411 decl_specifiers->locations[ds_type_spec]);
19413 /* Figure out what scope the entity declared by the DECLARATOR is
19414 located in. `grokdeclarator' sometimes changes the scope, so
19415 we compute it now. */
19416 scope = get_scope_of_declarator (declarator);
19418 /* Perform any lookups in the declared type which were thought to be
19419 dependent, but are not in the scope of the declarator. */
19420 decl_specifiers->type
19421 = maybe_update_decl_type (decl_specifiers->type, scope);
19423 /* If we're allowing GNU extensions, look for an
19424 asm-specification. */
19425 if (cp_parser_allow_gnu_extensions_p (parser))
19427 /* Look for an asm-specification. */
19428 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19429 asm_specification = cp_parser_asm_specification_opt (parser);
19431 else
19432 asm_specification = NULL_TREE;
19434 /* Look for attributes. */
19435 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19436 attributes = cp_parser_attributes_opt (parser);
19438 /* Peek at the next token. */
19439 token = cp_lexer_peek_token (parser->lexer);
19441 bool bogus_implicit_tmpl = false;
19443 if (function_declarator_p (declarator))
19445 /* Handle C++17 deduction guides. */
19446 if (!decl_specifiers->type
19447 && ctor_dtor_or_conv_p <= 0
19448 && cxx_dialect >= cxx17)
19450 cp_declarator *id = get_id_declarator (declarator);
19451 tree name = id->u.id.unqualified_name;
19452 parser->scope = id->u.id.qualifying_scope;
19453 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19454 if (tmpl
19455 && (DECL_CLASS_TEMPLATE_P (tmpl)
19456 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19458 id->u.id.unqualified_name = dguide_name (tmpl);
19459 id->u.id.sfk = sfk_deduction_guide;
19460 ctor_dtor_or_conv_p = 1;
19464 /* Check to see if the token indicates the start of a
19465 function-definition. */
19466 if (cp_parser_token_starts_function_definition_p (token))
19468 if (!function_definition_allowed_p)
19470 /* If a function-definition should not appear here, issue an
19471 error message. */
19472 cp_parser_error (parser,
19473 "a function-definition is not allowed here");
19474 return error_mark_node;
19477 location_t func_brace_location
19478 = cp_lexer_peek_token (parser->lexer)->location;
19480 /* Neither attributes nor an asm-specification are allowed
19481 on a function-definition. */
19482 if (asm_specification)
19483 error_at (asm_spec_start_token->location,
19484 "an asm-specification is not allowed "
19485 "on a function-definition");
19486 if (attributes)
19487 error_at (attributes_start_token->location,
19488 "attributes are not allowed "
19489 "on a function-definition");
19490 /* This is a function-definition. */
19491 *function_definition_p = true;
19493 /* Parse the function definition. */
19494 if (member_p)
19495 decl = cp_parser_save_member_function_body (parser,
19496 decl_specifiers,
19497 declarator,
19498 prefix_attributes);
19499 else
19500 decl =
19501 (cp_parser_function_definition_from_specifiers_and_declarator
19502 (parser, decl_specifiers, prefix_attributes, declarator));
19504 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19506 /* This is where the prologue starts... */
19507 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19508 = func_brace_location;
19511 return decl;
19514 else if (parser->fully_implicit_function_template_p)
19516 /* A non-template declaration involving a function parameter list
19517 containing an implicit template parameter will be made into a
19518 template. If the resulting declaration is not going to be an
19519 actual function then finish the template scope here to prevent it.
19520 An error message will be issued once we have a decl to talk about.
19522 FIXME probably we should do type deduction rather than create an
19523 implicit template, but the standard currently doesn't allow it. */
19524 bogus_implicit_tmpl = true;
19525 finish_fully_implicit_template (parser, NULL_TREE);
19528 /* [dcl.dcl]
19530 Only in function declarations for constructors, destructors, type
19531 conversions, and deduction guides can the decl-specifier-seq be omitted.
19533 We explicitly postpone this check past the point where we handle
19534 function-definitions because we tolerate function-definitions
19535 that are missing their return types in some modes. */
19536 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19538 cp_parser_error (parser,
19539 "expected constructor, destructor, or type conversion");
19540 return error_mark_node;
19543 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19544 if (token->type == CPP_EQ
19545 || token->type == CPP_OPEN_PAREN
19546 || token->type == CPP_OPEN_BRACE)
19548 is_initialized = SD_INITIALIZED;
19549 initialization_kind = token->type;
19550 if (maybe_range_for_decl)
19551 *maybe_range_for_decl = error_mark_node;
19552 tmp_init_loc = token->location;
19553 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19554 *init_loc = tmp_init_loc;
19556 if (token->type == CPP_EQ
19557 && function_declarator_p (declarator))
19559 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19560 if (t2->keyword == RID_DEFAULT)
19561 is_initialized = SD_DEFAULTED;
19562 else if (t2->keyword == RID_DELETE)
19563 is_initialized = SD_DELETED;
19566 else
19568 /* If the init-declarator isn't initialized and isn't followed by a
19569 `,' or `;', it's not a valid init-declarator. */
19570 if (token->type != CPP_COMMA
19571 && token->type != CPP_SEMICOLON)
19573 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19574 range_for_decl_p = true;
19575 else
19577 if (!maybe_range_for_decl)
19578 cp_parser_error (parser, "expected initializer");
19579 return error_mark_node;
19582 is_initialized = SD_UNINITIALIZED;
19583 initialization_kind = CPP_EOF;
19586 /* Because start_decl has side-effects, we should only call it if we
19587 know we're going ahead. By this point, we know that we cannot
19588 possibly be looking at any other construct. */
19589 cp_parser_commit_to_tentative_parse (parser);
19591 /* Enter the newly declared entry in the symbol table. If we're
19592 processing a declaration in a class-specifier, we wait until
19593 after processing the initializer. */
19594 if (!member_p)
19596 if (parser->in_unbraced_linkage_specification_p)
19597 decl_specifiers->storage_class = sc_extern;
19598 decl = start_decl (declarator, decl_specifiers,
19599 range_for_decl_p? SD_INITIALIZED : is_initialized,
19600 attributes, prefix_attributes, &pushed_scope);
19601 cp_finalize_omp_declare_simd (parser, decl);
19602 cp_finalize_oacc_routine (parser, decl, false);
19603 /* Adjust location of decl if declarator->id_loc is more appropriate:
19604 set, and decl wasn't merged with another decl, in which case its
19605 location would be different from input_location, and more accurate. */
19606 if (DECL_P (decl)
19607 && declarator->id_loc != UNKNOWN_LOCATION
19608 && DECL_SOURCE_LOCATION (decl) == input_location)
19609 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19611 else if (scope)
19612 /* Enter the SCOPE. That way unqualified names appearing in the
19613 initializer will be looked up in SCOPE. */
19614 pushed_scope = push_scope (scope);
19616 /* Perform deferred access control checks, now that we know in which
19617 SCOPE the declared entity resides. */
19618 if (!member_p && decl)
19620 tree saved_current_function_decl = NULL_TREE;
19622 /* If the entity being declared is a function, pretend that we
19623 are in its scope. If it is a `friend', it may have access to
19624 things that would not otherwise be accessible. */
19625 if (TREE_CODE (decl) == FUNCTION_DECL)
19627 saved_current_function_decl = current_function_decl;
19628 current_function_decl = decl;
19631 /* Perform access checks for template parameters. */
19632 cp_parser_perform_template_parameter_access_checks (checks);
19634 /* Perform the access control checks for the declarator and the
19635 decl-specifiers. */
19636 perform_deferred_access_checks (tf_warning_or_error);
19638 /* Restore the saved value. */
19639 if (TREE_CODE (decl) == FUNCTION_DECL)
19640 current_function_decl = saved_current_function_decl;
19643 /* Parse the initializer. */
19644 initializer = NULL_TREE;
19645 is_direct_init = false;
19646 is_non_constant_init = true;
19647 if (is_initialized)
19649 if (function_declarator_p (declarator))
19651 if (initialization_kind == CPP_EQ)
19652 initializer = cp_parser_pure_specifier (parser);
19653 else
19655 /* If the declaration was erroneous, we don't really
19656 know what the user intended, so just silently
19657 consume the initializer. */
19658 if (decl != error_mark_node)
19659 error_at (tmp_init_loc, "initializer provided for function");
19660 cp_parser_skip_to_closing_parenthesis (parser,
19661 /*recovering=*/true,
19662 /*or_comma=*/false,
19663 /*consume_paren=*/true);
19666 else
19668 /* We want to record the extra mangling scope for in-class
19669 initializers of class members and initializers of static data
19670 member templates. The former involves deferring
19671 parsing of the initializer until end of class as with default
19672 arguments. So right here we only handle the latter. */
19673 if (!member_p && processing_template_decl && decl != error_mark_node)
19674 start_lambda_scope (decl);
19675 initializer = cp_parser_initializer (parser,
19676 &is_direct_init,
19677 &is_non_constant_init);
19678 if (!member_p && processing_template_decl && decl != error_mark_node)
19679 finish_lambda_scope ();
19680 if (initializer == error_mark_node)
19681 cp_parser_skip_to_end_of_statement (parser);
19685 /* The old parser allows attributes to appear after a parenthesized
19686 initializer. Mark Mitchell proposed removing this functionality
19687 on the GCC mailing lists on 2002-08-13. This parser accepts the
19688 attributes -- but ignores them. */
19689 if (cp_parser_allow_gnu_extensions_p (parser)
19690 && initialization_kind == CPP_OPEN_PAREN)
19691 if (cp_parser_attributes_opt (parser))
19692 warning (OPT_Wattributes,
19693 "attributes after parenthesized initializer ignored");
19695 /* And now complain about a non-function implicit template. */
19696 if (bogus_implicit_tmpl && decl != error_mark_node)
19697 error_at (DECL_SOURCE_LOCATION (decl),
19698 "non-function %qD declared as implicit template", decl);
19700 /* For an in-class declaration, use `grokfield' to create the
19701 declaration. */
19702 if (member_p)
19704 if (pushed_scope)
19706 pop_scope (pushed_scope);
19707 pushed_scope = NULL_TREE;
19709 decl = grokfield (declarator, decl_specifiers,
19710 initializer, !is_non_constant_init,
19711 /*asmspec=*/NULL_TREE,
19712 attr_chainon (attributes, prefix_attributes));
19713 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19714 cp_parser_save_default_args (parser, decl);
19715 cp_finalize_omp_declare_simd (parser, decl);
19716 cp_finalize_oacc_routine (parser, decl, false);
19719 /* Finish processing the declaration. But, skip member
19720 declarations. */
19721 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19723 cp_finish_decl (decl,
19724 initializer, !is_non_constant_init,
19725 asm_specification,
19726 /* If the initializer is in parentheses, then this is
19727 a direct-initialization, which means that an
19728 `explicit' constructor is OK. Otherwise, an
19729 `explicit' constructor cannot be used. */
19730 ((is_direct_init || !is_initialized)
19731 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19733 else if ((cxx_dialect != cxx98) && friend_p
19734 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19735 /* Core issue #226 (C++0x only): A default template-argument
19736 shall not be specified in a friend class template
19737 declaration. */
19738 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19739 /*is_partial=*/false, /*is_friend_decl=*/1);
19741 if (!friend_p && pushed_scope)
19742 pop_scope (pushed_scope);
19744 if (function_declarator_p (declarator)
19745 && parser->fully_implicit_function_template_p)
19747 if (member_p)
19748 decl = finish_fully_implicit_template (parser, decl);
19749 else
19750 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19753 if (auto_result && is_initialized && decl_specifiers->type
19754 && type_uses_auto (decl_specifiers->type))
19755 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19757 return decl;
19760 /* Parse a declarator.
19762 declarator:
19763 direct-declarator
19764 ptr-operator declarator
19766 abstract-declarator:
19767 ptr-operator abstract-declarator [opt]
19768 direct-abstract-declarator
19770 GNU Extensions:
19772 declarator:
19773 attributes [opt] direct-declarator
19774 attributes [opt] ptr-operator declarator
19776 abstract-declarator:
19777 attributes [opt] ptr-operator abstract-declarator [opt]
19778 attributes [opt] direct-abstract-declarator
19780 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19781 detect constructors, destructors, deduction guides, or conversion operators.
19782 It is set to -1 if the declarator is a name, and +1 if it is a
19783 function. Otherwise it is set to zero. Usually you just want to
19784 test for >0, but internally the negative value is used.
19786 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19787 a decl-specifier-seq unless it declares a constructor, destructor,
19788 or conversion. It might seem that we could check this condition in
19789 semantic analysis, rather than parsing, but that makes it difficult
19790 to handle something like `f()'. We want to notice that there are
19791 no decl-specifiers, and therefore realize that this is an
19792 expression, not a declaration.)
19794 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19795 the declarator is a direct-declarator of the form "(...)".
19797 MEMBER_P is true iff this declarator is a member-declarator.
19799 FRIEND_P is true iff this declarator is a friend. */
19801 static cp_declarator *
19802 cp_parser_declarator (cp_parser* parser,
19803 cp_parser_declarator_kind dcl_kind,
19804 int* ctor_dtor_or_conv_p,
19805 bool* parenthesized_p,
19806 bool member_p, bool friend_p)
19808 cp_declarator *declarator;
19809 enum tree_code code;
19810 cp_cv_quals cv_quals;
19811 tree class_type;
19812 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19814 /* Assume this is not a constructor, destructor, or type-conversion
19815 operator. */
19816 if (ctor_dtor_or_conv_p)
19817 *ctor_dtor_or_conv_p = 0;
19819 if (cp_parser_allow_gnu_extensions_p (parser))
19820 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19822 /* Check for the ptr-operator production. */
19823 cp_parser_parse_tentatively (parser);
19824 /* Parse the ptr-operator. */
19825 code = cp_parser_ptr_operator (parser,
19826 &class_type,
19827 &cv_quals,
19828 &std_attributes);
19830 /* If that worked, then we have a ptr-operator. */
19831 if (cp_parser_parse_definitely (parser))
19833 /* If a ptr-operator was found, then this declarator was not
19834 parenthesized. */
19835 if (parenthesized_p)
19836 *parenthesized_p = true;
19837 /* The dependent declarator is optional if we are parsing an
19838 abstract-declarator. */
19839 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19840 cp_parser_parse_tentatively (parser);
19842 /* Parse the dependent declarator. */
19843 declarator = cp_parser_declarator (parser, dcl_kind,
19844 /*ctor_dtor_or_conv_p=*/NULL,
19845 /*parenthesized_p=*/NULL,
19846 /*member_p=*/false,
19847 friend_p);
19849 /* If we are parsing an abstract-declarator, we must handle the
19850 case where the dependent declarator is absent. */
19851 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19852 && !cp_parser_parse_definitely (parser))
19853 declarator = NULL;
19855 declarator = cp_parser_make_indirect_declarator
19856 (code, class_type, cv_quals, declarator, std_attributes);
19858 /* Everything else is a direct-declarator. */
19859 else
19861 if (parenthesized_p)
19862 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19863 CPP_OPEN_PAREN);
19864 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19865 ctor_dtor_or_conv_p,
19866 member_p, friend_p);
19869 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19870 declarator->attributes = gnu_attributes;
19871 return declarator;
19874 /* Parse a direct-declarator or direct-abstract-declarator.
19876 direct-declarator:
19877 declarator-id
19878 direct-declarator ( parameter-declaration-clause )
19879 cv-qualifier-seq [opt]
19880 ref-qualifier [opt]
19881 exception-specification [opt]
19882 direct-declarator [ constant-expression [opt] ]
19883 ( declarator )
19885 direct-abstract-declarator:
19886 direct-abstract-declarator [opt]
19887 ( parameter-declaration-clause )
19888 cv-qualifier-seq [opt]
19889 ref-qualifier [opt]
19890 exception-specification [opt]
19891 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19892 ( abstract-declarator )
19894 Returns a representation of the declarator. DCL_KIND is
19895 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19896 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19897 we are parsing a direct-declarator. It is
19898 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19899 of ambiguity we prefer an abstract declarator, as per
19900 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19901 as for cp_parser_declarator. */
19903 static cp_declarator *
19904 cp_parser_direct_declarator (cp_parser* parser,
19905 cp_parser_declarator_kind dcl_kind,
19906 int* ctor_dtor_or_conv_p,
19907 bool member_p, bool friend_p)
19909 cp_token *token;
19910 cp_declarator *declarator = NULL;
19911 tree scope = NULL_TREE;
19912 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19913 bool saved_in_declarator_p = parser->in_declarator_p;
19914 bool first = true;
19915 tree pushed_scope = NULL_TREE;
19916 cp_token *open_paren = NULL, *close_paren = NULL;
19918 while (true)
19920 /* Peek at the next token. */
19921 token = cp_lexer_peek_token (parser->lexer);
19922 if (token->type == CPP_OPEN_PAREN)
19924 /* This is either a parameter-declaration-clause, or a
19925 parenthesized declarator. When we know we are parsing a
19926 named declarator, it must be a parenthesized declarator
19927 if FIRST is true. For instance, `(int)' is a
19928 parameter-declaration-clause, with an omitted
19929 direct-abstract-declarator. But `((*))', is a
19930 parenthesized abstract declarator. Finally, when T is a
19931 template parameter `(T)' is a
19932 parameter-declaration-clause, and not a parenthesized
19933 named declarator.
19935 We first try and parse a parameter-declaration-clause,
19936 and then try a nested declarator (if FIRST is true).
19938 It is not an error for it not to be a
19939 parameter-declaration-clause, even when FIRST is
19940 false. Consider,
19942 int i (int);
19943 int i (3);
19945 The first is the declaration of a function while the
19946 second is the definition of a variable, including its
19947 initializer.
19949 Having seen only the parenthesis, we cannot know which of
19950 these two alternatives should be selected. Even more
19951 complex are examples like:
19953 int i (int (a));
19954 int i (int (3));
19956 The former is a function-declaration; the latter is a
19957 variable initialization.
19959 Thus again, we try a parameter-declaration-clause, and if
19960 that fails, we back out and return. */
19962 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19964 tree params;
19965 bool is_declarator = false;
19967 open_paren = NULL;
19969 /* In a member-declarator, the only valid interpretation
19970 of a parenthesis is the start of a
19971 parameter-declaration-clause. (It is invalid to
19972 initialize a static data member with a parenthesized
19973 initializer; only the "=" form of initialization is
19974 permitted.) */
19975 if (!member_p)
19976 cp_parser_parse_tentatively (parser);
19978 /* Consume the `('. */
19979 matching_parens parens;
19980 parens.consume_open (parser);
19981 if (first)
19983 /* If this is going to be an abstract declarator, we're
19984 in a declarator and we can't have default args. */
19985 parser->default_arg_ok_p = false;
19986 parser->in_declarator_p = true;
19989 begin_scope (sk_function_parms, NULL_TREE);
19991 /* Parse the parameter-declaration-clause. */
19992 params = cp_parser_parameter_declaration_clause (parser);
19994 /* Consume the `)'. */
19995 parens.require_close (parser);
19997 /* If all went well, parse the cv-qualifier-seq,
19998 ref-qualifier and the exception-specification. */
19999 if (member_p || cp_parser_parse_definitely (parser))
20001 cp_cv_quals cv_quals;
20002 cp_virt_specifiers virt_specifiers;
20003 cp_ref_qualifier ref_qual;
20004 tree exception_specification;
20005 tree late_return;
20006 tree attrs;
20007 bool memfn = (member_p || (pushed_scope
20008 && CLASS_TYPE_P (pushed_scope)));
20010 is_declarator = true;
20012 if (ctor_dtor_or_conv_p)
20013 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20014 first = false;
20016 /* Parse the cv-qualifier-seq. */
20017 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20018 /* Parse the ref-qualifier. */
20019 ref_qual = cp_parser_ref_qualifier_opt (parser);
20020 /* Parse the tx-qualifier. */
20021 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20022 /* And the exception-specification. */
20023 exception_specification
20024 = cp_parser_exception_specification_opt (parser);
20026 attrs = cp_parser_std_attribute_spec_seq (parser);
20028 /* In here, we handle cases where attribute is used after
20029 the function declaration. For example:
20030 void func (int x) __attribute__((vector(..))); */
20031 tree gnu_attrs = NULL_TREE;
20032 tree requires_clause = NULL_TREE;
20033 late_return = (cp_parser_late_return_type_opt
20034 (parser, declarator, requires_clause,
20035 memfn ? cv_quals : -1));
20037 /* Parse the virt-specifier-seq. */
20038 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20040 /* Create the function-declarator. */
20041 declarator = make_call_declarator (declarator,
20042 params,
20043 cv_quals,
20044 virt_specifiers,
20045 ref_qual,
20046 tx_qual,
20047 exception_specification,
20048 late_return,
20049 requires_clause);
20050 declarator->std_attributes = attrs;
20051 declarator->attributes = gnu_attrs;
20052 /* Any subsequent parameter lists are to do with
20053 return type, so are not those of the declared
20054 function. */
20055 parser->default_arg_ok_p = false;
20058 /* Remove the function parms from scope. */
20059 pop_bindings_and_leave_scope ();
20061 if (is_declarator)
20062 /* Repeat the main loop. */
20063 continue;
20066 /* If this is the first, we can try a parenthesized
20067 declarator. */
20068 if (first)
20070 bool saved_in_type_id_in_expr_p;
20072 parser->default_arg_ok_p = saved_default_arg_ok_p;
20073 parser->in_declarator_p = saved_in_declarator_p;
20075 open_paren = token;
20076 /* Consume the `('. */
20077 matching_parens parens;
20078 parens.consume_open (parser);
20079 /* Parse the nested declarator. */
20080 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20081 parser->in_type_id_in_expr_p = true;
20082 declarator
20083 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20084 /*parenthesized_p=*/NULL,
20085 member_p, friend_p);
20086 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20087 first = false;
20088 /* Expect a `)'. */
20089 close_paren = cp_lexer_peek_token (parser->lexer);
20090 if (!parens.require_close (parser))
20091 declarator = cp_error_declarator;
20092 if (declarator == cp_error_declarator)
20093 break;
20095 goto handle_declarator;
20097 /* Otherwise, we must be done. */
20098 else
20099 break;
20101 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20102 && token->type == CPP_OPEN_SQUARE
20103 && !cp_next_tokens_can_be_attribute_p (parser))
20105 /* Parse an array-declarator. */
20106 tree bounds, attrs;
20108 if (ctor_dtor_or_conv_p)
20109 *ctor_dtor_or_conv_p = 0;
20111 open_paren = NULL;
20112 first = false;
20113 parser->default_arg_ok_p = false;
20114 parser->in_declarator_p = true;
20115 /* Consume the `['. */
20116 cp_lexer_consume_token (parser->lexer);
20117 /* Peek at the next token. */
20118 token = cp_lexer_peek_token (parser->lexer);
20119 /* If the next token is `]', then there is no
20120 constant-expression. */
20121 if (token->type != CPP_CLOSE_SQUARE)
20123 bool non_constant_p;
20124 bounds
20125 = cp_parser_constant_expression (parser,
20126 /*allow_non_constant=*/true,
20127 &non_constant_p);
20128 if (!non_constant_p)
20129 /* OK */;
20130 else if (error_operand_p (bounds))
20131 /* Already gave an error. */;
20132 else if (!parser->in_function_body
20133 || current_binding_level->kind == sk_function_parms)
20135 /* Normally, the array bound must be an integral constant
20136 expression. However, as an extension, we allow VLAs
20137 in function scopes as long as they aren't part of a
20138 parameter declaration. */
20139 cp_parser_error (parser,
20140 "array bound is not an integer constant");
20141 bounds = error_mark_node;
20143 else if (processing_template_decl
20144 && !type_dependent_expression_p (bounds))
20146 /* Remember this wasn't a constant-expression. */
20147 bounds = build_nop (TREE_TYPE (bounds), bounds);
20148 TREE_SIDE_EFFECTS (bounds) = 1;
20151 else
20152 bounds = NULL_TREE;
20153 /* Look for the closing `]'. */
20154 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20156 declarator = cp_error_declarator;
20157 break;
20160 attrs = cp_parser_std_attribute_spec_seq (parser);
20161 declarator = make_array_declarator (declarator, bounds);
20162 declarator->std_attributes = attrs;
20164 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20167 tree qualifying_scope;
20168 tree unqualified_name;
20169 tree attrs;
20170 special_function_kind sfk;
20171 bool abstract_ok;
20172 bool pack_expansion_p = false;
20173 cp_token *declarator_id_start_token;
20175 /* Parse a declarator-id */
20176 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20177 if (abstract_ok)
20179 cp_parser_parse_tentatively (parser);
20181 /* If we see an ellipsis, we should be looking at a
20182 parameter pack. */
20183 if (token->type == CPP_ELLIPSIS)
20185 /* Consume the `...' */
20186 cp_lexer_consume_token (parser->lexer);
20188 pack_expansion_p = true;
20192 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20193 unqualified_name
20194 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20195 qualifying_scope = parser->scope;
20196 if (abstract_ok)
20198 bool okay = false;
20200 if (!unqualified_name && pack_expansion_p)
20202 /* Check whether an error occurred. */
20203 okay = !cp_parser_error_occurred (parser);
20205 /* We already consumed the ellipsis to mark a
20206 parameter pack, but we have no way to report it,
20207 so abort the tentative parse. We will be exiting
20208 immediately anyway. */
20209 cp_parser_abort_tentative_parse (parser);
20211 else
20212 okay = cp_parser_parse_definitely (parser);
20214 if (!okay)
20215 unqualified_name = error_mark_node;
20216 else if (unqualified_name
20217 && (qualifying_scope
20218 || (!identifier_p (unqualified_name))))
20220 cp_parser_error (parser, "expected unqualified-id");
20221 unqualified_name = error_mark_node;
20225 if (!unqualified_name)
20226 return NULL;
20227 if (unqualified_name == error_mark_node)
20229 declarator = cp_error_declarator;
20230 pack_expansion_p = false;
20231 declarator->parameter_pack_p = false;
20232 break;
20235 attrs = cp_parser_std_attribute_spec_seq (parser);
20237 if (qualifying_scope && at_namespace_scope_p ()
20238 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20240 /* In the declaration of a member of a template class
20241 outside of the class itself, the SCOPE will sometimes
20242 be a TYPENAME_TYPE. For example, given:
20244 template <typename T>
20245 int S<T>::R::i = 3;
20247 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20248 this context, we must resolve S<T>::R to an ordinary
20249 type, rather than a typename type.
20251 The reason we normally avoid resolving TYPENAME_TYPEs
20252 is that a specialization of `S' might render
20253 `S<T>::R' not a type. However, if `S' is
20254 specialized, then this `i' will not be used, so there
20255 is no harm in resolving the types here. */
20256 tree type;
20258 /* Resolve the TYPENAME_TYPE. */
20259 type = resolve_typename_type (qualifying_scope,
20260 /*only_current_p=*/false);
20261 /* If that failed, the declarator is invalid. */
20262 if (TREE_CODE (type) == TYPENAME_TYPE)
20264 if (typedef_variant_p (type))
20265 error_at (declarator_id_start_token->location,
20266 "cannot define member of dependent typedef "
20267 "%qT", type);
20268 else
20269 error_at (declarator_id_start_token->location,
20270 "%<%T::%E%> is not a type",
20271 TYPE_CONTEXT (qualifying_scope),
20272 TYPE_IDENTIFIER (qualifying_scope));
20274 qualifying_scope = type;
20277 sfk = sfk_none;
20279 if (unqualified_name)
20281 tree class_type;
20283 if (qualifying_scope
20284 && CLASS_TYPE_P (qualifying_scope))
20285 class_type = qualifying_scope;
20286 else
20287 class_type = current_class_type;
20289 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20291 tree name_type = TREE_TYPE (unqualified_name);
20293 if (!class_type || !same_type_p (name_type, class_type))
20295 /* We do not attempt to print the declarator
20296 here because we do not have enough
20297 information about its original syntactic
20298 form. */
20299 cp_parser_error (parser, "invalid declarator");
20300 declarator = cp_error_declarator;
20301 break;
20303 else if (qualifying_scope
20304 && CLASSTYPE_USE_TEMPLATE (name_type))
20306 error_at (declarator_id_start_token->location,
20307 "invalid use of constructor as a template");
20308 inform (declarator_id_start_token->location,
20309 "use %<%T::%D%> instead of %<%T::%D%> to "
20310 "name the constructor in a qualified name",
20311 class_type,
20312 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20313 class_type, name_type);
20314 declarator = cp_error_declarator;
20315 break;
20317 unqualified_name = constructor_name (class_type);
20320 if (class_type)
20322 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20323 sfk = sfk_destructor;
20324 else if (identifier_p (unqualified_name)
20325 && IDENTIFIER_CONV_OP_P (unqualified_name))
20326 sfk = sfk_conversion;
20327 else if (/* There's no way to declare a constructor
20328 for an unnamed type, even if the type
20329 got a name for linkage purposes. */
20330 !TYPE_WAS_UNNAMED (class_type)
20331 /* Handle correctly (c++/19200):
20333 struct S {
20334 struct T{};
20335 friend void S(T);
20338 and also:
20340 namespace N {
20341 void S();
20344 struct S {
20345 friend void N::S();
20346 }; */
20347 && (!friend_p || class_type == qualifying_scope)
20348 && constructor_name_p (unqualified_name,
20349 class_type))
20350 sfk = sfk_constructor;
20351 else if (is_overloaded_fn (unqualified_name)
20352 && DECL_CONSTRUCTOR_P (get_first_fn
20353 (unqualified_name)))
20354 sfk = sfk_constructor;
20356 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20357 *ctor_dtor_or_conv_p = -1;
20360 declarator = make_id_declarator (qualifying_scope,
20361 unqualified_name,
20362 sfk);
20363 declarator->std_attributes = attrs;
20364 declarator->id_loc = token->location;
20365 declarator->parameter_pack_p = pack_expansion_p;
20367 if (pack_expansion_p)
20368 maybe_warn_variadic_templates ();
20371 handle_declarator:;
20372 scope = get_scope_of_declarator (declarator);
20373 if (scope)
20375 /* Any names that appear after the declarator-id for a
20376 member are looked up in the containing scope. */
20377 if (at_function_scope_p ())
20379 /* But declarations with qualified-ids can't appear in a
20380 function. */
20381 cp_parser_error (parser, "qualified-id in declaration");
20382 declarator = cp_error_declarator;
20383 break;
20385 pushed_scope = push_scope (scope);
20387 parser->in_declarator_p = true;
20388 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20389 || (declarator && declarator->kind == cdk_id))
20390 /* Default args are only allowed on function
20391 declarations. */
20392 parser->default_arg_ok_p = saved_default_arg_ok_p;
20393 else
20394 parser->default_arg_ok_p = false;
20396 first = false;
20398 /* We're done. */
20399 else
20400 break;
20403 /* For an abstract declarator, we might wind up with nothing at this
20404 point. That's an error; the declarator is not optional. */
20405 if (!declarator)
20406 cp_parser_error (parser, "expected declarator");
20407 else if (open_paren)
20409 /* Record overly parenthesized declarator so we can give a
20410 diagnostic about confusing decl/expr disambiguation. */
20411 if (declarator->kind == cdk_array)
20413 /* If the open and close parens are on different lines, this
20414 is probably a formatting thing, so ignore. */
20415 expanded_location open = expand_location (open_paren->location);
20416 expanded_location close = expand_location (close_paren->location);
20417 if (open.line != close.line || open.file != close.file)
20418 open_paren = NULL;
20420 if (open_paren)
20421 declarator->parenthesized = open_paren->location;
20424 /* If we entered a scope, we must exit it now. */
20425 if (pushed_scope)
20426 pop_scope (pushed_scope);
20428 parser->default_arg_ok_p = saved_default_arg_ok_p;
20429 parser->in_declarator_p = saved_in_declarator_p;
20431 return declarator;
20434 /* Parse a ptr-operator.
20436 ptr-operator:
20437 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20438 * cv-qualifier-seq [opt]
20440 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20441 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20443 GNU Extension:
20445 ptr-operator:
20446 & cv-qualifier-seq [opt]
20448 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20449 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20450 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20451 filled in with the TYPE containing the member. *CV_QUALS is
20452 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20453 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20454 Note that the tree codes returned by this function have nothing
20455 to do with the types of trees that will be eventually be created
20456 to represent the pointer or reference type being parsed. They are
20457 just constants with suggestive names. */
20458 static enum tree_code
20459 cp_parser_ptr_operator (cp_parser* parser,
20460 tree* type,
20461 cp_cv_quals *cv_quals,
20462 tree *attributes)
20464 enum tree_code code = ERROR_MARK;
20465 cp_token *token;
20466 tree attrs = NULL_TREE;
20468 /* Assume that it's not a pointer-to-member. */
20469 *type = NULL_TREE;
20470 /* And that there are no cv-qualifiers. */
20471 *cv_quals = TYPE_UNQUALIFIED;
20473 /* Peek at the next token. */
20474 token = cp_lexer_peek_token (parser->lexer);
20476 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20477 if (token->type == CPP_MULT)
20478 code = INDIRECT_REF;
20479 else if (token->type == CPP_AND)
20480 code = ADDR_EXPR;
20481 else if ((cxx_dialect != cxx98) &&
20482 token->type == CPP_AND_AND) /* C++0x only */
20483 code = NON_LVALUE_EXPR;
20485 if (code != ERROR_MARK)
20487 /* Consume the `*', `&' or `&&'. */
20488 cp_lexer_consume_token (parser->lexer);
20490 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20491 `&', if we are allowing GNU extensions. (The only qualifier
20492 that can legally appear after `&' is `restrict', but that is
20493 enforced during semantic analysis. */
20494 if (code == INDIRECT_REF
20495 || cp_parser_allow_gnu_extensions_p (parser))
20496 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20498 attrs = cp_parser_std_attribute_spec_seq (parser);
20499 if (attributes != NULL)
20500 *attributes = attrs;
20502 else
20504 /* Try the pointer-to-member case. */
20505 cp_parser_parse_tentatively (parser);
20506 /* Look for the optional `::' operator. */
20507 cp_parser_global_scope_opt (parser,
20508 /*current_scope_valid_p=*/false);
20509 /* Look for the nested-name specifier. */
20510 token = cp_lexer_peek_token (parser->lexer);
20511 cp_parser_nested_name_specifier (parser,
20512 /*typename_keyword_p=*/false,
20513 /*check_dependency_p=*/true,
20514 /*type_p=*/false,
20515 /*is_declaration=*/false);
20516 /* If we found it, and the next token is a `*', then we are
20517 indeed looking at a pointer-to-member operator. */
20518 if (!cp_parser_error_occurred (parser)
20519 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20521 /* Indicate that the `*' operator was used. */
20522 code = INDIRECT_REF;
20524 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20525 error_at (token->location, "%qD is a namespace", parser->scope);
20526 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20527 error_at (token->location, "cannot form pointer to member of "
20528 "non-class %q#T", parser->scope);
20529 else
20531 /* The type of which the member is a member is given by the
20532 current SCOPE. */
20533 *type = parser->scope;
20534 /* The next name will not be qualified. */
20535 parser->scope = NULL_TREE;
20536 parser->qualifying_scope = NULL_TREE;
20537 parser->object_scope = NULL_TREE;
20538 /* Look for optional c++11 attributes. */
20539 attrs = cp_parser_std_attribute_spec_seq (parser);
20540 if (attributes != NULL)
20541 *attributes = attrs;
20542 /* Look for the optional cv-qualifier-seq. */
20543 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20546 /* If that didn't work we don't have a ptr-operator. */
20547 if (!cp_parser_parse_definitely (parser))
20548 cp_parser_error (parser, "expected ptr-operator");
20551 return code;
20554 /* Parse an (optional) cv-qualifier-seq.
20556 cv-qualifier-seq:
20557 cv-qualifier cv-qualifier-seq [opt]
20559 cv-qualifier:
20560 const
20561 volatile
20563 GNU Extension:
20565 cv-qualifier:
20566 __restrict__
20568 Returns a bitmask representing the cv-qualifiers. */
20570 static cp_cv_quals
20571 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20573 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20575 while (true)
20577 cp_token *token;
20578 cp_cv_quals cv_qualifier;
20580 /* Peek at the next token. */
20581 token = cp_lexer_peek_token (parser->lexer);
20582 /* See if it's a cv-qualifier. */
20583 switch (token->keyword)
20585 case RID_CONST:
20586 cv_qualifier = TYPE_QUAL_CONST;
20587 break;
20589 case RID_VOLATILE:
20590 cv_qualifier = TYPE_QUAL_VOLATILE;
20591 break;
20593 case RID_RESTRICT:
20594 cv_qualifier = TYPE_QUAL_RESTRICT;
20595 break;
20597 default:
20598 cv_qualifier = TYPE_UNQUALIFIED;
20599 break;
20602 if (!cv_qualifier)
20603 break;
20605 if (cv_quals & cv_qualifier)
20607 gcc_rich_location richloc (token->location);
20608 richloc.add_fixit_remove ();
20609 error_at (&richloc, "duplicate cv-qualifier");
20610 cp_lexer_purge_token (parser->lexer);
20612 else
20614 cp_lexer_consume_token (parser->lexer);
20615 cv_quals |= cv_qualifier;
20619 return cv_quals;
20622 /* Parse an (optional) ref-qualifier
20624 ref-qualifier:
20628 Returns cp_ref_qualifier representing ref-qualifier. */
20630 static cp_ref_qualifier
20631 cp_parser_ref_qualifier_opt (cp_parser* parser)
20633 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20635 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20636 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20637 return ref_qual;
20639 while (true)
20641 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20642 cp_token *token = cp_lexer_peek_token (parser->lexer);
20644 switch (token->type)
20646 case CPP_AND:
20647 curr_ref_qual = REF_QUAL_LVALUE;
20648 break;
20650 case CPP_AND_AND:
20651 curr_ref_qual = REF_QUAL_RVALUE;
20652 break;
20654 default:
20655 curr_ref_qual = REF_QUAL_NONE;
20656 break;
20659 if (!curr_ref_qual)
20660 break;
20661 else if (ref_qual)
20663 error_at (token->location, "multiple ref-qualifiers");
20664 cp_lexer_purge_token (parser->lexer);
20666 else
20668 ref_qual = curr_ref_qual;
20669 cp_lexer_consume_token (parser->lexer);
20673 return ref_qual;
20676 /* Parse an optional tx-qualifier.
20678 tx-qualifier:
20679 transaction_safe
20680 transaction_safe_dynamic */
20682 static tree
20683 cp_parser_tx_qualifier_opt (cp_parser *parser)
20685 cp_token *token = cp_lexer_peek_token (parser->lexer);
20686 if (token->type == CPP_NAME)
20688 tree name = token->u.value;
20689 const char *p = IDENTIFIER_POINTER (name);
20690 const int len = strlen ("transaction_safe");
20691 if (!strncmp (p, "transaction_safe", len))
20693 p += len;
20694 if (*p == '\0'
20695 || !strcmp (p, "_dynamic"))
20697 cp_lexer_consume_token (parser->lexer);
20698 if (!flag_tm)
20700 error ("%qE requires %<-fgnu-tm%>", name);
20701 return NULL_TREE;
20703 else
20704 return name;
20708 return NULL_TREE;
20711 /* Parse an (optional) virt-specifier-seq.
20713 virt-specifier-seq:
20714 virt-specifier virt-specifier-seq [opt]
20716 virt-specifier:
20717 override
20718 final
20720 Returns a bitmask representing the virt-specifiers. */
20722 static cp_virt_specifiers
20723 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20725 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20727 while (true)
20729 cp_token *token;
20730 cp_virt_specifiers virt_specifier;
20732 /* Peek at the next token. */
20733 token = cp_lexer_peek_token (parser->lexer);
20734 /* See if it's a virt-specifier-qualifier. */
20735 if (token->type != CPP_NAME)
20736 break;
20737 if (id_equal (token->u.value, "override"))
20739 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20740 virt_specifier = VIRT_SPEC_OVERRIDE;
20742 else if (id_equal (token->u.value, "final"))
20744 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20745 virt_specifier = VIRT_SPEC_FINAL;
20747 else if (id_equal (token->u.value, "__final"))
20749 virt_specifier = VIRT_SPEC_FINAL;
20751 else
20752 break;
20754 if (virt_specifiers & virt_specifier)
20756 gcc_rich_location richloc (token->location);
20757 richloc.add_fixit_remove ();
20758 error_at (&richloc, "duplicate virt-specifier");
20759 cp_lexer_purge_token (parser->lexer);
20761 else
20763 cp_lexer_consume_token (parser->lexer);
20764 virt_specifiers |= virt_specifier;
20767 return virt_specifiers;
20770 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20771 is in scope even though it isn't real. */
20773 void
20774 inject_this_parameter (tree ctype, cp_cv_quals quals)
20776 tree this_parm;
20778 if (current_class_ptr)
20780 /* We don't clear this between NSDMIs. Is it already what we want? */
20781 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20782 if (DECL_P (current_class_ptr)
20783 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20784 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20785 && cp_type_quals (type) == quals)
20786 return;
20789 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20790 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20791 current_class_ptr = NULL_TREE;
20792 current_class_ref
20793 = cp_build_fold_indirect_ref (this_parm);
20794 current_class_ptr = this_parm;
20797 /* Return true iff our current scope is a non-static data member
20798 initializer. */
20800 bool
20801 parsing_nsdmi (void)
20803 /* We recognize NSDMI context by the context-less 'this' pointer set up
20804 by the function above. */
20805 if (current_class_ptr
20806 && TREE_CODE (current_class_ptr) == PARM_DECL
20807 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20808 return true;
20809 return false;
20812 /* Parse a late-specified return type, if any. This is not a separate
20813 non-terminal, but part of a function declarator, which looks like
20815 -> trailing-type-specifier-seq abstract-declarator(opt)
20817 Returns the type indicated by the type-id.
20819 In addition to this, parse any queued up #pragma omp declare simd
20820 clauses, and #pragma acc routine clauses.
20822 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20823 function. */
20825 static tree
20826 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20827 tree& requires_clause, cp_cv_quals quals)
20829 cp_token *token;
20830 tree type = NULL_TREE;
20831 bool declare_simd_p = (parser->omp_declare_simd
20832 && declarator
20833 && declarator->kind == cdk_id);
20835 bool oacc_routine_p = (parser->oacc_routine
20836 && declarator
20837 && declarator->kind == cdk_id);
20839 /* Peek at the next token. */
20840 token = cp_lexer_peek_token (parser->lexer);
20841 /* A late-specified return type is indicated by an initial '->'. */
20842 if (token->type != CPP_DEREF
20843 && token->keyword != RID_REQUIRES
20844 && !(token->type == CPP_NAME
20845 && token->u.value == ridpointers[RID_REQUIRES])
20846 && !(declare_simd_p || oacc_routine_p))
20847 return NULL_TREE;
20849 tree save_ccp = current_class_ptr;
20850 tree save_ccr = current_class_ref;
20851 if (quals >= 0)
20853 /* DR 1207: 'this' is in scope in the trailing return type. */
20854 inject_this_parameter (current_class_type, quals);
20857 if (token->type == CPP_DEREF)
20859 /* Consume the ->. */
20860 cp_lexer_consume_token (parser->lexer);
20862 type = cp_parser_trailing_type_id (parser);
20865 /* Function declarations may be followed by a trailing
20866 requires-clause. */
20867 requires_clause = cp_parser_requires_clause_opt (parser);
20869 if (declare_simd_p)
20870 declarator->attributes
20871 = cp_parser_late_parsing_omp_declare_simd (parser,
20872 declarator->attributes);
20873 if (oacc_routine_p)
20874 declarator->attributes
20875 = cp_parser_late_parsing_oacc_routine (parser,
20876 declarator->attributes);
20878 if (quals >= 0)
20880 current_class_ptr = save_ccp;
20881 current_class_ref = save_ccr;
20884 return type;
20887 /* Parse a declarator-id.
20889 declarator-id:
20890 id-expression
20891 :: [opt] nested-name-specifier [opt] type-name
20893 In the `id-expression' case, the value returned is as for
20894 cp_parser_id_expression if the id-expression was an unqualified-id.
20895 If the id-expression was a qualified-id, then a SCOPE_REF is
20896 returned. The first operand is the scope (either a NAMESPACE_DECL
20897 or TREE_TYPE), but the second is still just a representation of an
20898 unqualified-id. */
20900 static tree
20901 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20903 tree id;
20904 /* The expression must be an id-expression. Assume that qualified
20905 names are the names of types so that:
20907 template <class T>
20908 int S<T>::R::i = 3;
20910 will work; we must treat `S<T>::R' as the name of a type.
20911 Similarly, assume that qualified names are templates, where
20912 required, so that:
20914 template <class T>
20915 int S<T>::R<T>::i = 3;
20917 will work, too. */
20918 id = cp_parser_id_expression (parser,
20919 /*template_keyword_p=*/false,
20920 /*check_dependency_p=*/false,
20921 /*template_p=*/NULL,
20922 /*declarator_p=*/true,
20923 optional_p);
20924 if (id && BASELINK_P (id))
20925 id = BASELINK_FUNCTIONS (id);
20926 return id;
20929 /* Parse a type-id.
20931 type-id:
20932 type-specifier-seq abstract-declarator [opt]
20934 Returns the TYPE specified. */
20936 static tree
20937 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20938 bool is_trailing_return)
20940 cp_decl_specifier_seq type_specifier_seq;
20941 cp_declarator *abstract_declarator;
20943 /* Parse the type-specifier-seq. */
20944 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20945 is_trailing_return,
20946 &type_specifier_seq);
20947 if (is_template_arg && type_specifier_seq.type
20948 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20949 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20950 /* A bare template name as a template argument is a template template
20951 argument, not a placeholder, so fail parsing it as a type argument. */
20953 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20954 cp_parser_simulate_error (parser);
20955 return error_mark_node;
20957 if (type_specifier_seq.type == error_mark_node)
20958 return error_mark_node;
20960 /* There might or might not be an abstract declarator. */
20961 cp_parser_parse_tentatively (parser);
20962 /* Look for the declarator. */
20963 abstract_declarator
20964 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20965 /*parenthesized_p=*/NULL,
20966 /*member_p=*/false,
20967 /*friend_p=*/false);
20968 /* Check to see if there really was a declarator. */
20969 if (!cp_parser_parse_definitely (parser))
20970 abstract_declarator = NULL;
20972 if (type_specifier_seq.type
20973 /* The concepts TS allows 'auto' as a type-id. */
20974 && (!flag_concepts || parser->in_type_id_in_expr_p)
20975 /* None of the valid uses of 'auto' in C++14 involve the type-id
20976 nonterminal, but it is valid in a trailing-return-type. */
20977 && !(cxx_dialect >= cxx14 && is_trailing_return))
20978 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20980 /* A type-id with type 'auto' is only ok if the abstract declarator
20981 is a function declarator with a late-specified return type.
20983 A type-id with 'auto' is also valid in a trailing-return-type
20984 in a compound-requirement. */
20985 if (abstract_declarator
20986 && abstract_declarator->kind == cdk_function
20987 && abstract_declarator->u.function.late_return_type)
20988 /* OK */;
20989 else if (parser->in_result_type_constraint_p)
20990 /* OK */;
20991 else
20993 location_t loc = type_specifier_seq.locations[ds_type_spec];
20994 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20996 error_at (loc, "missing template arguments after %qT",
20997 auto_node);
20998 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20999 tmpl);
21001 else
21002 error_at (loc, "invalid use of %qT", auto_node);
21003 return error_mark_node;
21007 return groktypename (&type_specifier_seq, abstract_declarator,
21008 is_template_arg);
21011 static tree
21012 cp_parser_type_id (cp_parser *parser)
21014 return cp_parser_type_id_1 (parser, false, false);
21017 static tree
21018 cp_parser_template_type_arg (cp_parser *parser)
21020 tree r;
21021 const char *saved_message = parser->type_definition_forbidden_message;
21022 parser->type_definition_forbidden_message
21023 = G_("types may not be defined in template arguments");
21024 r = cp_parser_type_id_1 (parser, true, false);
21025 parser->type_definition_forbidden_message = saved_message;
21026 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21028 error ("invalid use of %<auto%> in template argument");
21029 r = error_mark_node;
21031 return r;
21034 static tree
21035 cp_parser_trailing_type_id (cp_parser *parser)
21037 return cp_parser_type_id_1 (parser, false, true);
21040 /* Parse a type-specifier-seq.
21042 type-specifier-seq:
21043 type-specifier type-specifier-seq [opt]
21045 GNU extension:
21047 type-specifier-seq:
21048 attributes type-specifier-seq [opt]
21050 If IS_DECLARATION is true, we are at the start of a "condition" or
21051 exception-declaration, so we might be followed by a declarator-id.
21053 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21054 i.e. we've just seen "->".
21056 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21058 static void
21059 cp_parser_type_specifier_seq (cp_parser* parser,
21060 bool is_declaration,
21061 bool is_trailing_return,
21062 cp_decl_specifier_seq *type_specifier_seq)
21064 bool seen_type_specifier = false;
21065 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21066 cp_token *start_token = NULL;
21068 /* Clear the TYPE_SPECIFIER_SEQ. */
21069 clear_decl_specs (type_specifier_seq);
21071 /* In the context of a trailing return type, enum E { } is an
21072 elaborated-type-specifier followed by a function-body, not an
21073 enum-specifier. */
21074 if (is_trailing_return)
21075 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21077 /* Parse the type-specifiers and attributes. */
21078 while (true)
21080 tree type_specifier;
21081 bool is_cv_qualifier;
21083 /* Check for attributes first. */
21084 if (cp_next_tokens_can_be_attribute_p (parser))
21086 type_specifier_seq->attributes
21087 = attr_chainon (type_specifier_seq->attributes,
21088 cp_parser_attributes_opt (parser));
21089 continue;
21092 /* record the token of the beginning of the type specifier seq,
21093 for error reporting purposes*/
21094 if (!start_token)
21095 start_token = cp_lexer_peek_token (parser->lexer);
21097 /* Look for the type-specifier. */
21098 type_specifier = cp_parser_type_specifier (parser,
21099 flags,
21100 type_specifier_seq,
21101 /*is_declaration=*/false,
21102 NULL,
21103 &is_cv_qualifier);
21104 if (!type_specifier)
21106 /* If the first type-specifier could not be found, this is not a
21107 type-specifier-seq at all. */
21108 if (!seen_type_specifier)
21110 /* Set in_declarator_p to avoid skipping to the semicolon. */
21111 int in_decl = parser->in_declarator_p;
21112 parser->in_declarator_p = true;
21114 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21115 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21116 cp_parser_error (parser, "expected type-specifier");
21118 parser->in_declarator_p = in_decl;
21120 type_specifier_seq->type = error_mark_node;
21121 return;
21123 /* If subsequent type-specifiers could not be found, the
21124 type-specifier-seq is complete. */
21125 break;
21128 seen_type_specifier = true;
21129 /* The standard says that a condition can be:
21131 type-specifier-seq declarator = assignment-expression
21133 However, given:
21135 struct S {};
21136 if (int S = ...)
21138 we should treat the "S" as a declarator, not as a
21139 type-specifier. The standard doesn't say that explicitly for
21140 type-specifier-seq, but it does say that for
21141 decl-specifier-seq in an ordinary declaration. Perhaps it
21142 would be clearer just to allow a decl-specifier-seq here, and
21143 then add a semantic restriction that if any decl-specifiers
21144 that are not type-specifiers appear, the program is invalid. */
21145 if (is_declaration && !is_cv_qualifier)
21146 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21150 /* Return whether the function currently being declared has an associated
21151 template parameter list. */
21153 static bool
21154 function_being_declared_is_template_p (cp_parser* parser)
21156 if (!current_template_parms || processing_template_parmlist)
21157 return false;
21159 if (parser->implicit_template_scope)
21160 return true;
21162 if (at_class_scope_p ()
21163 && TYPE_BEING_DEFINED (current_class_type))
21164 return parser->num_template_parameter_lists != 0;
21166 return ((int) parser->num_template_parameter_lists > template_class_depth
21167 (current_class_type));
21170 /* Parse a parameter-declaration-clause.
21172 parameter-declaration-clause:
21173 parameter-declaration-list [opt] ... [opt]
21174 parameter-declaration-list , ...
21176 Returns a representation for the parameter declarations. A return
21177 value of NULL indicates a parameter-declaration-clause consisting
21178 only of an ellipsis. */
21180 static tree
21181 cp_parser_parameter_declaration_clause (cp_parser* parser)
21183 tree parameters;
21184 cp_token *token;
21185 bool ellipsis_p;
21186 bool is_error;
21188 struct cleanup {
21189 cp_parser* parser;
21190 int auto_is_implicit_function_template_parm_p;
21191 ~cleanup() {
21192 parser->auto_is_implicit_function_template_parm_p
21193 = auto_is_implicit_function_template_parm_p;
21195 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21197 (void) cleanup;
21199 if (!processing_specialization
21200 && !processing_template_parmlist
21201 && !processing_explicit_instantiation)
21202 if (!current_function_decl
21203 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21204 parser->auto_is_implicit_function_template_parm_p = true;
21206 /* Peek at the next token. */
21207 token = cp_lexer_peek_token (parser->lexer);
21208 /* Check for trivial parameter-declaration-clauses. */
21209 if (token->type == CPP_ELLIPSIS)
21211 /* Consume the `...' token. */
21212 cp_lexer_consume_token (parser->lexer);
21213 return NULL_TREE;
21215 else if (token->type == CPP_CLOSE_PAREN)
21216 /* There are no parameters. */
21218 #ifndef NO_IMPLICIT_EXTERN_C
21219 if (in_system_header_at (input_location)
21220 && current_class_type == NULL
21221 && current_lang_name == lang_name_c)
21222 return NULL_TREE;
21223 else
21224 #endif
21225 return void_list_node;
21227 /* Check for `(void)', too, which is a special case. */
21228 else if (token->keyword == RID_VOID
21229 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21230 == CPP_CLOSE_PAREN))
21232 /* Consume the `void' token. */
21233 cp_lexer_consume_token (parser->lexer);
21234 /* There are no parameters. */
21235 return void_list_node;
21238 /* Parse the parameter-declaration-list. */
21239 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21240 /* If a parse error occurred while parsing the
21241 parameter-declaration-list, then the entire
21242 parameter-declaration-clause is erroneous. */
21243 if (is_error)
21244 return NULL;
21246 /* Peek at the next token. */
21247 token = cp_lexer_peek_token (parser->lexer);
21248 /* If it's a `,', the clause should terminate with an ellipsis. */
21249 if (token->type == CPP_COMMA)
21251 /* Consume the `,'. */
21252 cp_lexer_consume_token (parser->lexer);
21253 /* Expect an ellipsis. */
21254 ellipsis_p
21255 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21257 /* It might also be `...' if the optional trailing `,' was
21258 omitted. */
21259 else if (token->type == CPP_ELLIPSIS)
21261 /* Consume the `...' token. */
21262 cp_lexer_consume_token (parser->lexer);
21263 /* And remember that we saw it. */
21264 ellipsis_p = true;
21266 else
21267 ellipsis_p = false;
21269 /* Finish the parameter list. */
21270 if (!ellipsis_p)
21271 parameters = chainon (parameters, void_list_node);
21273 return parameters;
21276 /* Parse a parameter-declaration-list.
21278 parameter-declaration-list:
21279 parameter-declaration
21280 parameter-declaration-list , parameter-declaration
21282 Returns a representation of the parameter-declaration-list, as for
21283 cp_parser_parameter_declaration_clause. However, the
21284 `void_list_node' is never appended to the list. Upon return,
21285 *IS_ERROR will be true iff an error occurred. */
21287 static tree
21288 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21290 tree parameters = NULL_TREE;
21291 tree *tail = &parameters;
21292 bool saved_in_unbraced_linkage_specification_p;
21293 int index = 0;
21295 /* Assume all will go well. */
21296 *is_error = false;
21297 /* The special considerations that apply to a function within an
21298 unbraced linkage specifications do not apply to the parameters
21299 to the function. */
21300 saved_in_unbraced_linkage_specification_p
21301 = parser->in_unbraced_linkage_specification_p;
21302 parser->in_unbraced_linkage_specification_p = false;
21304 /* Look for more parameters. */
21305 while (true)
21307 cp_parameter_declarator *parameter;
21308 tree decl = error_mark_node;
21309 bool parenthesized_p = false;
21310 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21311 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21312 (current_template_parms)) : 0);
21314 /* Parse the parameter. */
21315 parameter
21316 = cp_parser_parameter_declaration (parser,
21317 /*template_parm_p=*/false,
21318 &parenthesized_p);
21320 /* We don't know yet if the enclosing context is deprecated, so wait
21321 and warn in grokparms if appropriate. */
21322 deprecated_state = DEPRECATED_SUPPRESS;
21324 if (parameter)
21326 /* If a function parameter pack was specified and an implicit template
21327 parameter was introduced during cp_parser_parameter_declaration,
21328 change any implicit parameters introduced into packs. */
21329 if (parser->implicit_template_parms
21330 && parameter->declarator
21331 && parameter->declarator->parameter_pack_p)
21333 int latest_template_parm_idx = TREE_VEC_LENGTH
21334 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21336 if (latest_template_parm_idx != template_parm_idx)
21337 parameter->decl_specifiers.type = convert_generic_types_to_packs
21338 (parameter->decl_specifiers.type,
21339 template_parm_idx, latest_template_parm_idx);
21342 decl = grokdeclarator (parameter->declarator,
21343 &parameter->decl_specifiers,
21344 PARM,
21345 parameter->default_argument != NULL_TREE,
21346 &parameter->decl_specifiers.attributes);
21347 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21348 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21351 deprecated_state = DEPRECATED_NORMAL;
21353 /* If a parse error occurred parsing the parameter declaration,
21354 then the entire parameter-declaration-list is erroneous. */
21355 if (decl == error_mark_node)
21357 *is_error = true;
21358 parameters = error_mark_node;
21359 break;
21362 if (parameter->decl_specifiers.attributes)
21363 cplus_decl_attributes (&decl,
21364 parameter->decl_specifiers.attributes,
21366 if (DECL_NAME (decl))
21367 decl = pushdecl (decl);
21369 if (decl != error_mark_node)
21371 retrofit_lang_decl (decl);
21372 DECL_PARM_INDEX (decl) = ++index;
21373 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21376 /* Add the new parameter to the list. */
21377 *tail = build_tree_list (parameter->default_argument, decl);
21378 tail = &TREE_CHAIN (*tail);
21380 /* Peek at the next token. */
21381 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21382 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21383 /* These are for Objective-C++ */
21384 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21385 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21386 /* The parameter-declaration-list is complete. */
21387 break;
21388 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21390 cp_token *token;
21392 /* Peek at the next token. */
21393 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21394 /* If it's an ellipsis, then the list is complete. */
21395 if (token->type == CPP_ELLIPSIS)
21396 break;
21397 /* Otherwise, there must be more parameters. Consume the
21398 `,'. */
21399 cp_lexer_consume_token (parser->lexer);
21400 /* When parsing something like:
21402 int i(float f, double d)
21404 we can tell after seeing the declaration for "f" that we
21405 are not looking at an initialization of a variable "i",
21406 but rather at the declaration of a function "i".
21408 Due to the fact that the parsing of template arguments
21409 (as specified to a template-id) requires backtracking we
21410 cannot use this technique when inside a template argument
21411 list. */
21412 if (!parser->in_template_argument_list_p
21413 && !parser->in_type_id_in_expr_p
21414 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21415 /* However, a parameter-declaration of the form
21416 "float(f)" (which is a valid declaration of a
21417 parameter "f") can also be interpreted as an
21418 expression (the conversion of "f" to "float"). */
21419 && !parenthesized_p)
21420 cp_parser_commit_to_tentative_parse (parser);
21422 else
21424 cp_parser_error (parser, "expected %<,%> or %<...%>");
21425 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21426 cp_parser_skip_to_closing_parenthesis (parser,
21427 /*recovering=*/true,
21428 /*or_comma=*/false,
21429 /*consume_paren=*/false);
21430 break;
21434 parser->in_unbraced_linkage_specification_p
21435 = saved_in_unbraced_linkage_specification_p;
21437 /* Reset implicit_template_scope if we are about to leave the function
21438 parameter list that introduced it. Note that for out-of-line member
21439 definitions, there will be one or more class scopes before we get to
21440 the template parameter scope. */
21442 if (cp_binding_level *its = parser->implicit_template_scope)
21443 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21445 while (maybe_its->kind == sk_class)
21446 maybe_its = maybe_its->level_chain;
21447 if (maybe_its == its)
21449 parser->implicit_template_parms = 0;
21450 parser->implicit_template_scope = 0;
21454 return parameters;
21457 /* Parse a parameter declaration.
21459 parameter-declaration:
21460 decl-specifier-seq ... [opt] declarator
21461 decl-specifier-seq declarator = assignment-expression
21462 decl-specifier-seq ... [opt] abstract-declarator [opt]
21463 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21465 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21466 declares a template parameter. (In that case, a non-nested `>'
21467 token encountered during the parsing of the assignment-expression
21468 is not interpreted as a greater-than operator.)
21470 Returns a representation of the parameter, or NULL if an error
21471 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21472 true iff the declarator is of the form "(p)". */
21474 static cp_parameter_declarator *
21475 cp_parser_parameter_declaration (cp_parser *parser,
21476 bool template_parm_p,
21477 bool *parenthesized_p)
21479 int declares_class_or_enum;
21480 cp_decl_specifier_seq decl_specifiers;
21481 cp_declarator *declarator;
21482 tree default_argument;
21483 cp_token *token = NULL, *declarator_token_start = NULL;
21484 const char *saved_message;
21485 bool template_parameter_pack_p = false;
21487 /* In a template parameter, `>' is not an operator.
21489 [temp.param]
21491 When parsing a default template-argument for a non-type
21492 template-parameter, the first non-nested `>' is taken as the end
21493 of the template parameter-list rather than a greater-than
21494 operator. */
21496 /* Type definitions may not appear in parameter types. */
21497 saved_message = parser->type_definition_forbidden_message;
21498 parser->type_definition_forbidden_message
21499 = G_("types may not be defined in parameter types");
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);
21590 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21592 tree type = decl_specifiers.type;
21594 if (type && DECL_P (type))
21595 type = TREE_TYPE (type);
21597 if (((type
21598 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21599 && (template_parm_p || uses_parameter_packs (type)))
21600 || (!type && template_parm_p))
21601 && declarator_can_be_parameter_pack (declarator))
21603 /* Consume the `...'. */
21604 cp_lexer_consume_token (parser->lexer);
21605 maybe_warn_variadic_templates ();
21607 /* Build a pack expansion type */
21608 if (template_parm_p)
21609 template_parameter_pack_p = true;
21610 else if (declarator)
21611 declarator->parameter_pack_p = true;
21612 else
21613 decl_specifiers.type = make_pack_expansion (type);
21617 /* The restriction on defining new types applies only to the type
21618 of the parameter, not to the default argument. */
21619 parser->type_definition_forbidden_message = saved_message;
21621 /* If the next token is `=', then process a default argument. */
21622 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21624 tree type = decl_specifiers.type;
21625 token = cp_lexer_peek_token (parser->lexer);
21626 /* If we are defining a class, then the tokens that make up the
21627 default argument must be saved and processed later. */
21628 if (!template_parm_p && at_class_scope_p ()
21629 && TYPE_BEING_DEFINED (current_class_type)
21630 && !LAMBDA_TYPE_P (current_class_type))
21631 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21633 // A constrained-type-specifier may declare a type template-parameter.
21634 else if (declares_constrained_type_template_parameter (type))
21635 default_argument
21636 = cp_parser_default_type_template_argument (parser);
21638 // A constrained-type-specifier may declare a template-template-parameter.
21639 else if (declares_constrained_template_template_parameter (type))
21640 default_argument
21641 = cp_parser_default_template_template_argument (parser);
21643 /* Outside of a class definition, we can just parse the
21644 assignment-expression. */
21645 else
21646 default_argument
21647 = cp_parser_default_argument (parser, template_parm_p);
21649 if (!parser->default_arg_ok_p)
21651 permerror (token->location,
21652 "default arguments are only "
21653 "permitted for function parameters");
21655 else if ((declarator && declarator->parameter_pack_p)
21656 || template_parameter_pack_p
21657 || (decl_specifiers.type
21658 && PACK_EXPANSION_P (decl_specifiers.type)))
21660 /* Find the name of the parameter pack. */
21661 cp_declarator *id_declarator = declarator;
21662 while (id_declarator && id_declarator->kind != cdk_id)
21663 id_declarator = id_declarator->declarator;
21665 if (id_declarator && id_declarator->kind == cdk_id)
21666 error_at (declarator_token_start->location,
21667 template_parm_p
21668 ? G_("template parameter pack %qD "
21669 "cannot have a default argument")
21670 : G_("parameter pack %qD cannot have "
21671 "a default argument"),
21672 id_declarator->u.id.unqualified_name);
21673 else
21674 error_at (declarator_token_start->location,
21675 template_parm_p
21676 ? G_("template parameter pack cannot have "
21677 "a default argument")
21678 : G_("parameter pack cannot have a "
21679 "default argument"));
21681 default_argument = NULL_TREE;
21684 else
21685 default_argument = NULL_TREE;
21687 /* Generate a location for the parameter, ranging from the start of the
21688 initial token to the end of the final token (using input_location for
21689 the latter, set up by cp_lexer_set_source_position_from_token when
21690 consuming tokens).
21692 If we have a identifier, then use it for the caret location, e.g.
21694 extern int callee (int one, int (*two)(int, int), float three);
21695 ~~~~~~^~~~~~~~~~~~~~
21697 otherwise, reuse the start location for the caret location e.g.:
21699 extern int callee (int one, int (*)(int, int), float three);
21700 ^~~~~~~~~~~~~~~~~
21703 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21704 ? declarator->id_loc
21705 : decl_spec_token_start->location);
21706 location_t param_loc = make_location (caret_loc,
21707 decl_spec_token_start->location,
21708 input_location);
21710 return make_parameter_declarator (&decl_specifiers,
21711 declarator,
21712 default_argument,
21713 param_loc,
21714 template_parameter_pack_p);
21717 /* Parse a default argument and return it.
21719 TEMPLATE_PARM_P is true if this is a default argument for a
21720 non-type template parameter. */
21721 static tree
21722 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21724 tree default_argument = NULL_TREE;
21725 bool saved_greater_than_is_operator_p;
21726 bool saved_local_variables_forbidden_p;
21727 bool non_constant_p, is_direct_init;
21729 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21730 set correctly. */
21731 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21732 parser->greater_than_is_operator_p = !template_parm_p;
21733 /* Local variable names (and the `this' keyword) may not
21734 appear in a default argument. */
21735 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21736 parser->local_variables_forbidden_p = true;
21737 /* Parse the assignment-expression. */
21738 if (template_parm_p)
21739 push_deferring_access_checks (dk_no_deferred);
21740 tree saved_class_ptr = NULL_TREE;
21741 tree saved_class_ref = NULL_TREE;
21742 /* The "this" pointer is not valid in a default argument. */
21743 if (cfun)
21745 saved_class_ptr = current_class_ptr;
21746 cp_function_chain->x_current_class_ptr = NULL_TREE;
21747 saved_class_ref = current_class_ref;
21748 cp_function_chain->x_current_class_ref = NULL_TREE;
21750 default_argument
21751 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21752 /* Restore the "this" pointer. */
21753 if (cfun)
21755 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21756 cp_function_chain->x_current_class_ref = saved_class_ref;
21758 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21759 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21760 if (template_parm_p)
21761 pop_deferring_access_checks ();
21762 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21763 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21765 return default_argument;
21768 /* Parse a function-body.
21770 function-body:
21771 compound_statement */
21773 static void
21774 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21776 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21777 ? BCS_TRY_BLOCK : BCS_NORMAL),
21778 true);
21781 /* Parse a ctor-initializer-opt followed by a function-body. Return
21782 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21783 is true we are parsing a function-try-block. */
21785 static void
21786 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21787 bool in_function_try_block)
21789 tree body, list;
21790 const bool check_body_p =
21791 DECL_CONSTRUCTOR_P (current_function_decl)
21792 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21793 tree last = NULL;
21795 /* Begin the function body. */
21796 body = begin_function_body ();
21797 /* Parse the optional ctor-initializer. */
21798 cp_parser_ctor_initializer_opt (parser);
21800 /* If we're parsing a constexpr constructor definition, we need
21801 to check that the constructor body is indeed empty. However,
21802 before we get to cp_parser_function_body lot of junk has been
21803 generated, so we can't just check that we have an empty block.
21804 Rather we take a snapshot of the outermost block, and check whether
21805 cp_parser_function_body changed its state. */
21806 if (check_body_p)
21808 list = cur_stmt_list;
21809 if (STATEMENT_LIST_TAIL (list))
21810 last = STATEMENT_LIST_TAIL (list)->stmt;
21812 /* Parse the function-body. */
21813 cp_parser_function_body (parser, in_function_try_block);
21814 if (check_body_p)
21815 check_constexpr_ctor_body (last, list, /*complain=*/true);
21816 /* Finish the function body. */
21817 finish_function_body (body);
21820 /* Parse an initializer.
21822 initializer:
21823 = initializer-clause
21824 ( expression-list )
21826 Returns an expression representing the initializer. If no
21827 initializer is present, NULL_TREE is returned.
21829 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21830 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21831 set to TRUE if there is no initializer present. If there is an
21832 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21833 is set to true; otherwise it is set to false. */
21835 static tree
21836 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21837 bool* non_constant_p)
21839 cp_token *token;
21840 tree init;
21842 /* Peek at the next token. */
21843 token = cp_lexer_peek_token (parser->lexer);
21845 /* Let our caller know whether or not this initializer was
21846 parenthesized. */
21847 *is_direct_init = (token->type != CPP_EQ);
21848 /* Assume that the initializer is constant. */
21849 *non_constant_p = false;
21851 if (token->type == CPP_EQ)
21853 /* Consume the `='. */
21854 cp_lexer_consume_token (parser->lexer);
21855 /* Parse the initializer-clause. */
21856 init = cp_parser_initializer_clause (parser, non_constant_p);
21858 else if (token->type == CPP_OPEN_PAREN)
21860 vec<tree, va_gc> *vec;
21861 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21862 /*cast_p=*/false,
21863 /*allow_expansion_p=*/true,
21864 non_constant_p);
21865 if (vec == NULL)
21866 return error_mark_node;
21867 init = build_tree_list_vec (vec);
21868 release_tree_vector (vec);
21870 else if (token->type == CPP_OPEN_BRACE)
21872 cp_lexer_set_source_position (parser->lexer);
21873 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21874 init = cp_parser_braced_list (parser, non_constant_p);
21875 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21877 else
21879 /* Anything else is an error. */
21880 cp_parser_error (parser, "expected initializer");
21881 init = error_mark_node;
21884 if (check_for_bare_parameter_packs (init))
21885 init = error_mark_node;
21887 return init;
21890 /* Parse an initializer-clause.
21892 initializer-clause:
21893 assignment-expression
21894 braced-init-list
21896 Returns an expression representing the initializer.
21898 If the `assignment-expression' production is used the value
21899 returned is simply a representation for the expression.
21901 Otherwise, calls cp_parser_braced_list. */
21903 static cp_expr
21904 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21906 cp_expr initializer;
21908 /* Assume the expression is constant. */
21909 *non_constant_p = false;
21911 /* If it is not a `{', then we are looking at an
21912 assignment-expression. */
21913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21915 initializer
21916 = cp_parser_constant_expression (parser,
21917 /*allow_non_constant_p=*/true,
21918 non_constant_p);
21920 else
21921 initializer = cp_parser_braced_list (parser, non_constant_p);
21923 return initializer;
21926 /* Parse a brace-enclosed initializer list.
21928 braced-init-list:
21929 { initializer-list , [opt] }
21930 { designated-initializer-list , [opt] }
21933 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21934 the elements of the initializer-list (or NULL, if the last
21935 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21936 NULL_TREE. There is no way to detect whether or not the optional
21937 trailing `,' was provided. NON_CONSTANT_P is as for
21938 cp_parser_initializer. */
21940 static cp_expr
21941 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21943 tree initializer;
21944 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21946 /* Consume the `{' token. */
21947 matching_braces braces;
21948 braces.require_open (parser);
21949 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21950 initializer = make_node (CONSTRUCTOR);
21951 /* If it's not a `}', then there is a non-trivial initializer. */
21952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21954 /* Parse the initializer list. */
21955 CONSTRUCTOR_ELTS (initializer)
21956 = cp_parser_initializer_list (parser, non_constant_p);
21957 /* A trailing `,' token is allowed. */
21958 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21959 cp_lexer_consume_token (parser->lexer);
21961 else
21962 *non_constant_p = false;
21963 /* Now, there should be a trailing `}'. */
21964 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21965 braces.require_close (parser);
21966 TREE_TYPE (initializer) = init_list_type_node;
21968 cp_expr result (initializer);
21969 /* Build a location of the form:
21970 { ... }
21971 ^~~~~~~
21972 with caret==start at the open brace, finish at the close brace. */
21973 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21974 result.set_location (combined_loc);
21975 return result;
21978 /* Consume tokens up to, and including, the next non-nested closing `]'.
21979 Returns true iff we found a closing `]'. */
21981 static bool
21982 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21984 unsigned square_depth = 0;
21986 while (true)
21988 cp_token * token = cp_lexer_peek_token (parser->lexer);
21990 switch (token->type)
21992 case CPP_EOF:
21993 case CPP_PRAGMA_EOL:
21994 /* If we've run out of tokens, then there is no closing `]'. */
21995 return false;
21997 case CPP_OPEN_SQUARE:
21998 ++square_depth;
21999 break;
22001 case CPP_CLOSE_SQUARE:
22002 if (!square_depth--)
22004 cp_lexer_consume_token (parser->lexer);
22005 return true;
22007 break;
22009 default:
22010 break;
22013 /* Consume the token. */
22014 cp_lexer_consume_token (parser->lexer);
22018 /* Return true if we are looking at an array-designator, false otherwise. */
22020 static bool
22021 cp_parser_array_designator_p (cp_parser *parser)
22023 /* Consume the `['. */
22024 cp_lexer_consume_token (parser->lexer);
22026 cp_lexer_save_tokens (parser->lexer);
22028 /* Skip tokens until the next token is a closing square bracket.
22029 If we find the closing `]', and the next token is a `=', then
22030 we are looking at an array designator. */
22031 bool array_designator_p
22032 = (cp_parser_skip_to_closing_square_bracket (parser)
22033 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22035 /* Roll back the tokens we skipped. */
22036 cp_lexer_rollback_tokens (parser->lexer);
22038 return array_designator_p;
22041 /* Parse an initializer-list.
22043 initializer-list:
22044 initializer-clause ... [opt]
22045 initializer-list , initializer-clause ... [opt]
22047 C++2A Extension:
22049 designated-initializer-list:
22050 designated-initializer-clause
22051 designated-initializer-list , designated-initializer-clause
22053 designated-initializer-clause:
22054 designator brace-or-equal-initializer
22056 designator:
22057 . identifier
22059 GNU Extension:
22061 initializer-list:
22062 designation initializer-clause ...[opt]
22063 initializer-list , designation initializer-clause ...[opt]
22065 designation:
22066 . identifier =
22067 identifier :
22068 [ constant-expression ] =
22070 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22071 for the initializer. If the INDEX of the elt is non-NULL, it is the
22072 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22073 as for cp_parser_initializer. */
22075 static vec<constructor_elt, va_gc> *
22076 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22078 vec<constructor_elt, va_gc> *v = NULL;
22079 bool first_p = true;
22080 tree first_designator = NULL_TREE;
22082 /* Assume all of the expressions are constant. */
22083 *non_constant_p = false;
22085 /* Parse the rest of the list. */
22086 while (true)
22088 cp_token *token;
22089 tree designator;
22090 tree initializer;
22091 bool clause_non_constant_p;
22092 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22094 /* Handle the C++2A syntax, '. id ='. */
22095 if ((cxx_dialect >= cxx2a
22096 || cp_parser_allow_gnu_extensions_p (parser))
22097 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22098 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22099 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22100 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22101 == CPP_OPEN_BRACE)))
22103 if (cxx_dialect < cxx2a)
22104 pedwarn (loc, OPT_Wpedantic,
22105 "C++ designated initializers only available with "
22106 "-std=c++2a or -std=gnu++2a");
22107 /* Consume the `.'. */
22108 cp_lexer_consume_token (parser->lexer);
22109 /* Consume the identifier. */
22110 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22111 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22112 /* Consume the `='. */
22113 cp_lexer_consume_token (parser->lexer);
22115 /* Also, if the next token is an identifier and the following one is a
22116 colon, we are looking at the GNU designated-initializer
22117 syntax. */
22118 else if (cp_parser_allow_gnu_extensions_p (parser)
22119 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22120 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22121 == CPP_COLON))
22123 /* Warn the user that they are using an extension. */
22124 pedwarn (loc, OPT_Wpedantic,
22125 "ISO C++ does not allow GNU designated initializers");
22126 /* Consume the identifier. */
22127 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22128 /* Consume the `:'. */
22129 cp_lexer_consume_token (parser->lexer);
22131 /* Also handle C99 array designators, '[ const ] ='. */
22132 else if (cp_parser_allow_gnu_extensions_p (parser)
22133 && !c_dialect_objc ()
22134 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22136 /* In C++11, [ could start a lambda-introducer. */
22137 bool non_const = false;
22139 cp_parser_parse_tentatively (parser);
22141 if (!cp_parser_array_designator_p (parser))
22143 cp_parser_simulate_error (parser);
22144 designator = NULL_TREE;
22146 else
22148 designator = cp_parser_constant_expression (parser, true,
22149 &non_const);
22150 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22151 cp_parser_require (parser, CPP_EQ, RT_EQ);
22154 if (!cp_parser_parse_definitely (parser))
22155 designator = NULL_TREE;
22156 else if (non_const
22157 && (!require_potential_rvalue_constant_expression
22158 (designator)))
22159 designator = NULL_TREE;
22160 if (designator)
22161 /* Warn the user that they are using an extension. */
22162 pedwarn (loc, OPT_Wpedantic,
22163 "ISO C++ does not allow C99 designated initializers");
22165 else
22166 designator = NULL_TREE;
22168 if (first_p)
22170 first_designator = designator;
22171 first_p = false;
22173 else if (cxx_dialect >= cxx2a
22174 && first_designator != error_mark_node
22175 && (!first_designator != !designator))
22177 error_at (loc, "either all initializer clauses should be designated "
22178 "or none of them should be");
22179 first_designator = error_mark_node;
22181 else if (cxx_dialect < cxx2a && !first_designator)
22182 first_designator = designator;
22184 /* Parse the initializer. */
22185 initializer = cp_parser_initializer_clause (parser,
22186 &clause_non_constant_p);
22187 /* If any clause is non-constant, so is the entire initializer. */
22188 if (clause_non_constant_p)
22189 *non_constant_p = true;
22191 /* If we have an ellipsis, this is an initializer pack
22192 expansion. */
22193 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22195 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22197 /* Consume the `...'. */
22198 cp_lexer_consume_token (parser->lexer);
22200 if (designator && cxx_dialect >= cxx2a)
22201 error_at (loc,
22202 "%<...%> not allowed in designated initializer list");
22204 /* Turn the initializer into an initializer expansion. */
22205 initializer = make_pack_expansion (initializer);
22208 /* Add it to the vector. */
22209 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22211 /* If the next token is not a comma, we have reached the end of
22212 the list. */
22213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22214 break;
22216 /* Peek at the next token. */
22217 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22218 /* If the next token is a `}', then we're still done. An
22219 initializer-clause can have a trailing `,' after the
22220 initializer-list and before the closing `}'. */
22221 if (token->type == CPP_CLOSE_BRACE)
22222 break;
22224 /* Consume the `,' token. */
22225 cp_lexer_consume_token (parser->lexer);
22228 /* The same identifier shall not appear in multiple designators
22229 of a designated-initializer-list. */
22230 if (first_designator)
22232 unsigned int i;
22233 tree designator, val;
22234 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22235 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22237 if (IDENTIFIER_MARKED (designator))
22239 error_at (EXPR_LOC_OR_LOC (val, input_location),
22240 "%<.%s%> designator used multiple times in "
22241 "the same initializer list",
22242 IDENTIFIER_POINTER (designator));
22243 (*v)[i].index = NULL_TREE;
22245 else
22246 IDENTIFIER_MARKED (designator) = 1;
22248 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22249 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22250 IDENTIFIER_MARKED (designator) = 0;
22253 return v;
22256 /* Classes [gram.class] */
22258 /* Parse a class-name.
22260 class-name:
22261 identifier
22262 template-id
22264 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22265 to indicate that names looked up in dependent types should be
22266 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22267 keyword has been used to indicate that the name that appears next
22268 is a template. TAG_TYPE indicates the explicit tag given before
22269 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22270 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22271 is the class being defined in a class-head. If ENUM_OK is TRUE,
22272 enum-names are also accepted.
22274 Returns the TYPE_DECL representing the class. */
22276 static tree
22277 cp_parser_class_name (cp_parser *parser,
22278 bool typename_keyword_p,
22279 bool template_keyword_p,
22280 enum tag_types tag_type,
22281 bool check_dependency_p,
22282 bool class_head_p,
22283 bool is_declaration,
22284 bool enum_ok)
22286 tree decl;
22287 tree scope;
22288 bool typename_p;
22289 cp_token *token;
22290 tree identifier = NULL_TREE;
22292 /* All class-names start with an identifier. */
22293 token = cp_lexer_peek_token (parser->lexer);
22294 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22296 cp_parser_error (parser, "expected class-name");
22297 return error_mark_node;
22300 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22301 to a template-id, so we save it here. */
22302 scope = parser->scope;
22303 if (scope == error_mark_node)
22304 return error_mark_node;
22306 /* Any name names a type if we're following the `typename' keyword
22307 in a qualified name where the enclosing scope is type-dependent. */
22308 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22309 && dependent_type_p (scope));
22310 /* Handle the common case (an identifier, but not a template-id)
22311 efficiently. */
22312 if (token->type == CPP_NAME
22313 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22315 cp_token *identifier_token;
22316 bool ambiguous_p;
22318 /* Look for the identifier. */
22319 identifier_token = cp_lexer_peek_token (parser->lexer);
22320 ambiguous_p = identifier_token->error_reported;
22321 identifier = cp_parser_identifier (parser);
22322 /* If the next token isn't an identifier, we are certainly not
22323 looking at a class-name. */
22324 if (identifier == error_mark_node)
22325 decl = error_mark_node;
22326 /* If we know this is a type-name, there's no need to look it
22327 up. */
22328 else if (typename_p)
22329 decl = identifier;
22330 else
22332 tree ambiguous_decls;
22333 /* If we already know that this lookup is ambiguous, then
22334 we've already issued an error message; there's no reason
22335 to check again. */
22336 if (ambiguous_p)
22338 cp_parser_simulate_error (parser);
22339 return error_mark_node;
22341 /* If the next token is a `::', then the name must be a type
22342 name.
22344 [basic.lookup.qual]
22346 During the lookup for a name preceding the :: scope
22347 resolution operator, object, function, and enumerator
22348 names are ignored. */
22349 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22350 tag_type = scope_type;
22351 /* Look up the name. */
22352 decl = cp_parser_lookup_name (parser, identifier,
22353 tag_type,
22354 /*is_template=*/false,
22355 /*is_namespace=*/false,
22356 check_dependency_p,
22357 &ambiguous_decls,
22358 identifier_token->location);
22359 if (ambiguous_decls)
22361 if (cp_parser_parsing_tentatively (parser))
22362 cp_parser_simulate_error (parser);
22363 return error_mark_node;
22367 else
22369 /* Try a template-id. */
22370 decl = cp_parser_template_id (parser, template_keyword_p,
22371 check_dependency_p,
22372 tag_type,
22373 is_declaration);
22374 if (decl == error_mark_node)
22375 return error_mark_node;
22378 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22380 /* If this is a typename, create a TYPENAME_TYPE. */
22381 if (typename_p && decl != error_mark_node)
22383 decl = make_typename_type (scope, decl, typename_type,
22384 /*complain=*/tf_error);
22385 if (decl != error_mark_node)
22386 decl = TYPE_NAME (decl);
22389 decl = strip_using_decl (decl);
22391 /* Check to see that it is really the name of a class. */
22392 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22393 && identifier_p (TREE_OPERAND (decl, 0))
22394 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22395 /* Situations like this:
22397 template <typename T> struct A {
22398 typename T::template X<int>::I i;
22401 are problematic. Is `T::template X<int>' a class-name? The
22402 standard does not seem to be definitive, but there is no other
22403 valid interpretation of the following `::'. Therefore, those
22404 names are considered class-names. */
22406 decl = make_typename_type (scope, decl, tag_type, tf_error);
22407 if (decl != error_mark_node)
22408 decl = TYPE_NAME (decl);
22410 else if (TREE_CODE (decl) != TYPE_DECL
22411 || TREE_TYPE (decl) == error_mark_node
22412 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22413 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22414 /* In Objective-C 2.0, a classname followed by '.' starts a
22415 dot-syntax expression, and it's not a type-name. */
22416 || (c_dialect_objc ()
22417 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22418 && objc_is_class_name (decl)))
22419 decl = error_mark_node;
22421 if (decl == error_mark_node)
22422 cp_parser_error (parser, "expected class-name");
22423 else if (identifier && !parser->scope)
22424 maybe_note_name_used_in_class (identifier, decl);
22426 return decl;
22429 /* Parse a class-specifier.
22431 class-specifier:
22432 class-head { member-specification [opt] }
22434 Returns the TREE_TYPE representing the class. */
22436 static tree
22437 cp_parser_class_specifier_1 (cp_parser* parser)
22439 tree type;
22440 tree attributes = NULL_TREE;
22441 bool nested_name_specifier_p;
22442 unsigned saved_num_template_parameter_lists;
22443 bool saved_in_function_body;
22444 unsigned char in_statement;
22445 bool in_switch_statement_p;
22446 bool saved_in_unbraced_linkage_specification_p;
22447 tree old_scope = NULL_TREE;
22448 tree scope = NULL_TREE;
22449 cp_token *closing_brace;
22451 push_deferring_access_checks (dk_no_deferred);
22453 /* Parse the class-head. */
22454 type = cp_parser_class_head (parser,
22455 &nested_name_specifier_p);
22456 /* If the class-head was a semantic disaster, skip the entire body
22457 of the class. */
22458 if (!type)
22460 cp_parser_skip_to_end_of_block_or_statement (parser);
22461 pop_deferring_access_checks ();
22462 return error_mark_node;
22465 /* Look for the `{'. */
22466 matching_braces braces;
22467 if (!braces.require_open (parser))
22469 pop_deferring_access_checks ();
22470 return error_mark_node;
22473 cp_ensure_no_omp_declare_simd (parser);
22474 cp_ensure_no_oacc_routine (parser);
22476 /* Issue an error message if type-definitions are forbidden here. */
22477 cp_parser_check_type_definition (parser);
22478 /* Remember that we are defining one more class. */
22479 ++parser->num_classes_being_defined;
22480 /* Inside the class, surrounding template-parameter-lists do not
22481 apply. */
22482 saved_num_template_parameter_lists
22483 = parser->num_template_parameter_lists;
22484 parser->num_template_parameter_lists = 0;
22485 /* We are not in a function body. */
22486 saved_in_function_body = parser->in_function_body;
22487 parser->in_function_body = false;
22488 /* Or in a loop. */
22489 in_statement = parser->in_statement;
22490 parser->in_statement = 0;
22491 /* Or in a switch. */
22492 in_switch_statement_p = parser->in_switch_statement_p;
22493 parser->in_switch_statement_p = false;
22494 /* We are not immediately inside an extern "lang" block. */
22495 saved_in_unbraced_linkage_specification_p
22496 = parser->in_unbraced_linkage_specification_p;
22497 parser->in_unbraced_linkage_specification_p = false;
22499 // Associate constraints with the type.
22500 if (flag_concepts)
22501 type = associate_classtype_constraints (type);
22503 /* Start the class. */
22504 if (nested_name_specifier_p)
22506 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22507 old_scope = push_inner_scope (scope);
22509 type = begin_class_definition (type);
22511 if (type == error_mark_node)
22512 /* If the type is erroneous, skip the entire body of the class. */
22513 cp_parser_skip_to_closing_brace (parser);
22514 else
22515 /* Parse the member-specification. */
22516 cp_parser_member_specification_opt (parser);
22518 /* Look for the trailing `}'. */
22519 closing_brace = braces.require_close (parser);
22520 /* Look for trailing attributes to apply to this class. */
22521 if (cp_parser_allow_gnu_extensions_p (parser))
22522 attributes = cp_parser_gnu_attributes_opt (parser);
22523 if (type != error_mark_node)
22524 type = finish_struct (type, attributes);
22525 if (nested_name_specifier_p)
22526 pop_inner_scope (old_scope, scope);
22528 /* We've finished a type definition. Check for the common syntax
22529 error of forgetting a semicolon after the definition. We need to
22530 be careful, as we can't just check for not-a-semicolon and be done
22531 with it; the user might have typed:
22533 class X { } c = ...;
22534 class X { } *p = ...;
22536 and so forth. Instead, enumerate all the possible tokens that
22537 might follow this production; if we don't see one of them, then
22538 complain and silently insert the semicolon. */
22540 cp_token *token = cp_lexer_peek_token (parser->lexer);
22541 bool want_semicolon = true;
22543 if (cp_next_tokens_can_be_std_attribute_p (parser))
22544 /* Don't try to parse c++11 attributes here. As per the
22545 grammar, that should be a task for
22546 cp_parser_decl_specifier_seq. */
22547 want_semicolon = false;
22549 switch (token->type)
22551 case CPP_NAME:
22552 case CPP_SEMICOLON:
22553 case CPP_MULT:
22554 case CPP_AND:
22555 case CPP_OPEN_PAREN:
22556 case CPP_CLOSE_PAREN:
22557 case CPP_COMMA:
22558 want_semicolon = false;
22559 break;
22561 /* While it's legal for type qualifiers and storage class
22562 specifiers to follow type definitions in the grammar, only
22563 compiler testsuites contain code like that. Assume that if
22564 we see such code, then what we're really seeing is a case
22565 like:
22567 class X { }
22568 const <type> var = ...;
22572 class Y { }
22573 static <type> func (...) ...
22575 i.e. the qualifier or specifier applies to the next
22576 declaration. To do so, however, we need to look ahead one
22577 more token to see if *that* token is a type specifier.
22579 This code could be improved to handle:
22581 class Z { }
22582 static const <type> var = ...; */
22583 case CPP_KEYWORD:
22584 if (keyword_is_decl_specifier (token->keyword))
22586 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22588 /* Handling user-defined types here would be nice, but very
22589 tricky. */
22590 want_semicolon
22591 = (lookahead->type == CPP_KEYWORD
22592 && keyword_begins_type_specifier (lookahead->keyword));
22594 break;
22595 default:
22596 break;
22599 /* If we don't have a type, then something is very wrong and we
22600 shouldn't try to do anything clever. Likewise for not seeing the
22601 closing brace. */
22602 if (closing_brace && TYPE_P (type) && want_semicolon)
22604 /* Locate the closing brace. */
22605 cp_token_position prev
22606 = cp_lexer_previous_token_position (parser->lexer);
22607 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22608 location_t loc = prev_token->location;
22610 /* We want to suggest insertion of a ';' immediately *after* the
22611 closing brace, so, if we can, offset the location by 1 column. */
22612 location_t next_loc = loc;
22613 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22614 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22616 rich_location richloc (line_table, next_loc);
22618 /* If we successfully offset the location, suggest the fix-it. */
22619 if (next_loc != loc)
22620 richloc.add_fixit_insert_before (next_loc, ";");
22622 if (CLASSTYPE_DECLARED_CLASS (type))
22623 error_at (&richloc,
22624 "expected %<;%> after class definition");
22625 else if (TREE_CODE (type) == RECORD_TYPE)
22626 error_at (&richloc,
22627 "expected %<;%> after struct definition");
22628 else if (TREE_CODE (type) == UNION_TYPE)
22629 error_at (&richloc,
22630 "expected %<;%> after union definition");
22631 else
22632 gcc_unreachable ();
22634 /* Unget one token and smash it to look as though we encountered
22635 a semicolon in the input stream. */
22636 cp_lexer_set_token_position (parser->lexer, prev);
22637 token = cp_lexer_peek_token (parser->lexer);
22638 token->type = CPP_SEMICOLON;
22639 token->keyword = RID_MAX;
22643 /* If this class is not itself within the scope of another class,
22644 then we need to parse the bodies of all of the queued function
22645 definitions. Note that the queued functions defined in a class
22646 are not always processed immediately following the
22647 class-specifier for that class. Consider:
22649 struct A {
22650 struct B { void f() { sizeof (A); } };
22653 If `f' were processed before the processing of `A' were
22654 completed, there would be no way to compute the size of `A'.
22655 Note that the nesting we are interested in here is lexical --
22656 not the semantic nesting given by TYPE_CONTEXT. In particular,
22657 for:
22659 struct A { struct B; };
22660 struct A::B { void f() { } };
22662 there is no need to delay the parsing of `A::B::f'. */
22663 if (--parser->num_classes_being_defined == 0)
22665 tree decl;
22666 tree class_type = NULL_TREE;
22667 tree pushed_scope = NULL_TREE;
22668 unsigned ix;
22669 cp_default_arg_entry *e;
22670 tree save_ccp, save_ccr;
22672 /* In a first pass, parse default arguments to the functions.
22673 Then, in a second pass, parse the bodies of the functions.
22674 This two-phased approach handles cases like:
22676 struct S {
22677 void f() { g(); }
22678 void g(int i = 3);
22682 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22684 decl = e->decl;
22685 /* If there are default arguments that have not yet been processed,
22686 take care of them now. */
22687 if (class_type != e->class_type)
22689 if (pushed_scope)
22690 pop_scope (pushed_scope);
22691 class_type = e->class_type;
22692 pushed_scope = push_scope (class_type);
22694 /* Make sure that any template parameters are in scope. */
22695 maybe_begin_member_template_processing (decl);
22696 /* Parse the default argument expressions. */
22697 cp_parser_late_parsing_default_args (parser, decl);
22698 /* Remove any template parameters from the symbol table. */
22699 maybe_end_member_template_processing ();
22701 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22702 /* Now parse any NSDMIs. */
22703 save_ccp = current_class_ptr;
22704 save_ccr = current_class_ref;
22705 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22707 if (class_type != DECL_CONTEXT (decl))
22709 if (pushed_scope)
22710 pop_scope (pushed_scope);
22711 class_type = DECL_CONTEXT (decl);
22712 pushed_scope = push_scope (class_type);
22714 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22715 cp_parser_late_parsing_nsdmi (parser, decl);
22717 vec_safe_truncate (unparsed_nsdmis, 0);
22718 current_class_ptr = save_ccp;
22719 current_class_ref = save_ccr;
22720 if (pushed_scope)
22721 pop_scope (pushed_scope);
22723 /* Now do some post-NSDMI bookkeeping. */
22724 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22725 after_nsdmi_defaulted_late_checks (class_type);
22726 vec_safe_truncate (unparsed_classes, 0);
22727 after_nsdmi_defaulted_late_checks (type);
22729 /* Now parse the body of the functions. */
22730 if (flag_openmp)
22732 /* OpenMP UDRs need to be parsed before all other functions. */
22733 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22734 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22735 cp_parser_late_parsing_for_member (parser, decl);
22736 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22737 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22738 cp_parser_late_parsing_for_member (parser, decl);
22740 else
22741 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22742 cp_parser_late_parsing_for_member (parser, decl);
22743 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22745 else
22746 vec_safe_push (unparsed_classes, type);
22748 /* Put back any saved access checks. */
22749 pop_deferring_access_checks ();
22751 /* Restore saved state. */
22752 parser->in_switch_statement_p = in_switch_statement_p;
22753 parser->in_statement = in_statement;
22754 parser->in_function_body = saved_in_function_body;
22755 parser->num_template_parameter_lists
22756 = saved_num_template_parameter_lists;
22757 parser->in_unbraced_linkage_specification_p
22758 = saved_in_unbraced_linkage_specification_p;
22760 return type;
22763 static tree
22764 cp_parser_class_specifier (cp_parser* parser)
22766 tree ret;
22767 timevar_push (TV_PARSE_STRUCT);
22768 ret = cp_parser_class_specifier_1 (parser);
22769 timevar_pop (TV_PARSE_STRUCT);
22770 return ret;
22773 /* Parse a class-head.
22775 class-head:
22776 class-key identifier [opt] base-clause [opt]
22777 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22778 class-key nested-name-specifier [opt] template-id
22779 base-clause [opt]
22781 class-virt-specifier:
22782 final
22784 GNU Extensions:
22785 class-key attributes identifier [opt] base-clause [opt]
22786 class-key attributes nested-name-specifier identifier base-clause [opt]
22787 class-key attributes nested-name-specifier [opt] template-id
22788 base-clause [opt]
22790 Upon return BASES is initialized to the list of base classes (or
22791 NULL, if there are none) in the same form returned by
22792 cp_parser_base_clause.
22794 Returns the TYPE of the indicated class. Sets
22795 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22796 involving a nested-name-specifier was used, and FALSE otherwise.
22798 Returns error_mark_node if this is not a class-head.
22800 Returns NULL_TREE if the class-head is syntactically valid, but
22801 semantically invalid in a way that means we should skip the entire
22802 body of the class. */
22804 static tree
22805 cp_parser_class_head (cp_parser* parser,
22806 bool* nested_name_specifier_p)
22808 tree nested_name_specifier;
22809 enum tag_types class_key;
22810 tree id = NULL_TREE;
22811 tree type = NULL_TREE;
22812 tree attributes;
22813 tree bases;
22814 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22815 bool template_id_p = false;
22816 bool qualified_p = false;
22817 bool invalid_nested_name_p = false;
22818 bool invalid_explicit_specialization_p = false;
22819 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22820 tree pushed_scope = NULL_TREE;
22821 unsigned num_templates;
22822 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22823 /* Assume no nested-name-specifier will be present. */
22824 *nested_name_specifier_p = false;
22825 /* Assume no template parameter lists will be used in defining the
22826 type. */
22827 num_templates = 0;
22828 parser->colon_corrects_to_scope_p = false;
22830 /* Look for the class-key. */
22831 class_key = cp_parser_class_key (parser);
22832 if (class_key == none_type)
22833 return error_mark_node;
22835 location_t class_head_start_location = input_location;
22837 /* Parse the attributes. */
22838 attributes = cp_parser_attributes_opt (parser);
22840 /* If the next token is `::', that is invalid -- but sometimes
22841 people do try to write:
22843 struct ::S {};
22845 Handle this gracefully by accepting the extra qualifier, and then
22846 issuing an error about it later if this really is a
22847 class-head. If it turns out just to be an elaborated type
22848 specifier, remain silent. */
22849 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22850 qualified_p = true;
22852 push_deferring_access_checks (dk_no_check);
22854 /* Determine the name of the class. Begin by looking for an
22855 optional nested-name-specifier. */
22856 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22857 nested_name_specifier
22858 = cp_parser_nested_name_specifier_opt (parser,
22859 /*typename_keyword_p=*/false,
22860 /*check_dependency_p=*/false,
22861 /*type_p=*/true,
22862 /*is_declaration=*/false);
22863 /* If there was a nested-name-specifier, then there *must* be an
22864 identifier. */
22866 cp_token *bad_template_keyword = NULL;
22868 if (nested_name_specifier)
22870 type_start_token = cp_lexer_peek_token (parser->lexer);
22871 /* Although the grammar says `identifier', it really means
22872 `class-name' or `template-name'. You are only allowed to
22873 define a class that has already been declared with this
22874 syntax.
22876 The proposed resolution for Core Issue 180 says that wherever
22877 you see `class T::X' you should treat `X' as a type-name.
22879 It is OK to define an inaccessible class; for example:
22881 class A { class B; };
22882 class A::B {};
22884 We do not know if we will see a class-name, or a
22885 template-name. We look for a class-name first, in case the
22886 class-name is a template-id; if we looked for the
22887 template-name first we would stop after the template-name. */
22888 cp_parser_parse_tentatively (parser);
22889 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22890 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22891 type = cp_parser_class_name (parser,
22892 /*typename_keyword_p=*/false,
22893 /*template_keyword_p=*/false,
22894 class_type,
22895 /*check_dependency_p=*/false,
22896 /*class_head_p=*/true,
22897 /*is_declaration=*/false);
22898 /* If that didn't work, ignore the nested-name-specifier. */
22899 if (!cp_parser_parse_definitely (parser))
22901 invalid_nested_name_p = true;
22902 type_start_token = cp_lexer_peek_token (parser->lexer);
22903 id = cp_parser_identifier (parser);
22904 if (id == error_mark_node)
22905 id = NULL_TREE;
22907 /* If we could not find a corresponding TYPE, treat this
22908 declaration like an unqualified declaration. */
22909 if (type == error_mark_node)
22910 nested_name_specifier = NULL_TREE;
22911 /* Otherwise, count the number of templates used in TYPE and its
22912 containing scopes. */
22913 else
22915 tree scope;
22917 for (scope = TREE_TYPE (type);
22918 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22919 scope = get_containing_scope (scope))
22920 if (TYPE_P (scope)
22921 && CLASS_TYPE_P (scope)
22922 && CLASSTYPE_TEMPLATE_INFO (scope)
22923 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22924 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22925 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22926 ++num_templates;
22929 /* Otherwise, the identifier is optional. */
22930 else
22932 /* We don't know whether what comes next is a template-id,
22933 an identifier, or nothing at all. */
22934 cp_parser_parse_tentatively (parser);
22935 /* Check for a template-id. */
22936 type_start_token = cp_lexer_peek_token (parser->lexer);
22937 id = cp_parser_template_id (parser,
22938 /*template_keyword_p=*/false,
22939 /*check_dependency_p=*/true,
22940 class_key,
22941 /*is_declaration=*/true);
22942 /* If that didn't work, it could still be an identifier. */
22943 if (!cp_parser_parse_definitely (parser))
22945 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22947 type_start_token = cp_lexer_peek_token (parser->lexer);
22948 id = cp_parser_identifier (parser);
22950 else
22951 id = NULL_TREE;
22953 else
22955 template_id_p = true;
22956 ++num_templates;
22960 pop_deferring_access_checks ();
22962 if (id)
22964 cp_parser_check_for_invalid_template_id (parser, id,
22965 class_key,
22966 type_start_token->location);
22968 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22970 /* If it's not a `:' or a `{' then we can't really be looking at a
22971 class-head, since a class-head only appears as part of a
22972 class-specifier. We have to detect this situation before calling
22973 xref_tag, since that has irreversible side-effects. */
22974 if (!cp_parser_next_token_starts_class_definition_p (parser))
22976 cp_parser_error (parser, "expected %<{%> or %<:%>");
22977 type = error_mark_node;
22978 goto out;
22981 /* At this point, we're going ahead with the class-specifier, even
22982 if some other problem occurs. */
22983 cp_parser_commit_to_tentative_parse (parser);
22984 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22986 cp_parser_error (parser,
22987 "cannot specify %<override%> for a class");
22988 type = error_mark_node;
22989 goto out;
22991 /* Issue the error about the overly-qualified name now. */
22992 if (qualified_p)
22994 cp_parser_error (parser,
22995 "global qualification of class name is invalid");
22996 type = error_mark_node;
22997 goto out;
22999 else if (invalid_nested_name_p)
23001 cp_parser_error (parser,
23002 "qualified name does not name a class");
23003 type = error_mark_node;
23004 goto out;
23006 else if (nested_name_specifier)
23008 tree scope;
23010 if (bad_template_keyword)
23011 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23012 keyword template shall not appear at the top level. */
23013 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23014 "keyword %<template%> not allowed in class-head-name");
23016 /* Reject typedef-names in class heads. */
23017 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23019 error_at (type_start_token->location,
23020 "invalid class name in declaration of %qD",
23021 type);
23022 type = NULL_TREE;
23023 goto done;
23026 /* Figure out in what scope the declaration is being placed. */
23027 scope = current_scope ();
23028 /* If that scope does not contain the scope in which the
23029 class was originally declared, the program is invalid. */
23030 if (scope && !is_ancestor (scope, nested_name_specifier))
23032 if (at_namespace_scope_p ())
23033 error_at (type_start_token->location,
23034 "declaration of %qD in namespace %qD which does not "
23035 "enclose %qD",
23036 type, scope, nested_name_specifier);
23037 else
23038 error_at (type_start_token->location,
23039 "declaration of %qD in %qD which does not enclose %qD",
23040 type, scope, nested_name_specifier);
23041 type = NULL_TREE;
23042 goto done;
23044 /* [dcl.meaning]
23046 A declarator-id shall not be qualified except for the
23047 definition of a ... nested class outside of its class
23048 ... [or] the definition or explicit instantiation of a
23049 class member of a namespace outside of its namespace. */
23050 if (scope == nested_name_specifier)
23052 permerror (nested_name_specifier_token_start->location,
23053 "extra qualification not allowed");
23054 nested_name_specifier = NULL_TREE;
23055 num_templates = 0;
23058 /* An explicit-specialization must be preceded by "template <>". If
23059 it is not, try to recover gracefully. */
23060 if (at_namespace_scope_p ()
23061 && parser->num_template_parameter_lists == 0
23062 && !processing_template_parmlist
23063 && template_id_p)
23065 /* Build a location of this form:
23066 struct typename <ARGS>
23067 ^~~~~~~~~~~~~~~~~~~~~~
23068 with caret==start at the start token, and
23069 finishing at the end of the type. */
23070 location_t reported_loc
23071 = make_location (class_head_start_location,
23072 class_head_start_location,
23073 get_finish (type_start_token->location));
23074 rich_location richloc (line_table, reported_loc);
23075 richloc.add_fixit_insert_before (class_head_start_location,
23076 "template <> ");
23077 error_at (&richloc,
23078 "an explicit specialization must be preceded by"
23079 " %<template <>%>");
23080 invalid_explicit_specialization_p = true;
23081 /* Take the same action that would have been taken by
23082 cp_parser_explicit_specialization. */
23083 ++parser->num_template_parameter_lists;
23084 begin_specialization ();
23086 /* There must be no "return" statements between this point and the
23087 end of this function; set "type "to the correct return value and
23088 use "goto done;" to return. */
23089 /* Make sure that the right number of template parameters were
23090 present. */
23091 if (!cp_parser_check_template_parameters (parser, num_templates,
23092 type_start_token->location,
23093 /*declarator=*/NULL))
23095 /* If something went wrong, there is no point in even trying to
23096 process the class-definition. */
23097 type = NULL_TREE;
23098 goto done;
23101 /* Look up the type. */
23102 if (template_id_p)
23104 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23105 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23106 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23108 error_at (type_start_token->location,
23109 "function template %qD redeclared as a class template", id);
23110 type = error_mark_node;
23112 else
23114 type = TREE_TYPE (id);
23115 type = maybe_process_partial_specialization (type);
23117 /* Check the scope while we still know whether or not we had a
23118 nested-name-specifier. */
23119 if (type != error_mark_node)
23120 check_unqualified_spec_or_inst (type, type_start_token->location);
23122 if (nested_name_specifier)
23123 pushed_scope = push_scope (nested_name_specifier);
23125 else if (nested_name_specifier)
23127 tree class_type;
23129 /* Given:
23131 template <typename T> struct S { struct T };
23132 template <typename T> struct S<T>::T { };
23134 we will get a TYPENAME_TYPE when processing the definition of
23135 `S::T'. We need to resolve it to the actual type before we
23136 try to define it. */
23137 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23139 class_type = resolve_typename_type (TREE_TYPE (type),
23140 /*only_current_p=*/false);
23141 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23142 type = TYPE_NAME (class_type);
23143 else
23145 cp_parser_error (parser, "could not resolve typename type");
23146 type = error_mark_node;
23150 if (maybe_process_partial_specialization (TREE_TYPE (type))
23151 == error_mark_node)
23153 type = NULL_TREE;
23154 goto done;
23157 class_type = current_class_type;
23158 /* Enter the scope indicated by the nested-name-specifier. */
23159 pushed_scope = push_scope (nested_name_specifier);
23160 /* Get the canonical version of this type. */
23161 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23162 /* Call push_template_decl if it seems like we should be defining a
23163 template either from the template headers or the type we're
23164 defining, so that we diagnose both extra and missing headers. */
23165 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23166 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23167 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23169 type = push_template_decl (type);
23170 if (type == error_mark_node)
23172 type = NULL_TREE;
23173 goto done;
23177 type = TREE_TYPE (type);
23178 *nested_name_specifier_p = true;
23180 else /* The name is not a nested name. */
23182 /* If the class was unnamed, create a dummy name. */
23183 if (!id)
23184 id = make_anon_name ();
23185 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23186 ? ts_within_enclosing_non_class
23187 : ts_current);
23188 type = xref_tag (class_key, id, tag_scope,
23189 parser->num_template_parameter_lists);
23192 /* Indicate whether this class was declared as a `class' or as a
23193 `struct'. */
23194 if (TREE_CODE (type) == RECORD_TYPE)
23195 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23196 cp_parser_check_class_key (class_key, type);
23198 /* If this type was already complete, and we see another definition,
23199 that's an error. */
23200 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23202 error_at (type_start_token->location, "redefinition of %q#T",
23203 type);
23204 inform (location_of (type), "previous definition of %q#T",
23205 type);
23206 type = NULL_TREE;
23207 goto done;
23209 else if (type == error_mark_node)
23210 type = NULL_TREE;
23212 if (type)
23214 /* Apply attributes now, before any use of the class as a template
23215 argument in its base list. */
23216 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23217 fixup_attribute_variants (type);
23220 /* We will have entered the scope containing the class; the names of
23221 base classes should be looked up in that context. For example:
23223 struct A { struct B {}; struct C; };
23224 struct A::C : B {};
23226 is valid. */
23228 /* Get the list of base-classes, if there is one. */
23229 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23231 /* PR59482: enter the class scope so that base-specifiers are looked
23232 up correctly. */
23233 if (type)
23234 pushclass (type);
23235 bases = cp_parser_base_clause (parser);
23236 /* PR59482: get out of the previously pushed class scope so that the
23237 subsequent pops pop the right thing. */
23238 if (type)
23239 popclass ();
23241 else
23242 bases = NULL_TREE;
23244 /* If we're really defining a class, process the base classes.
23245 If they're invalid, fail. */
23246 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23247 xref_basetypes (type, bases);
23249 done:
23250 /* Leave the scope given by the nested-name-specifier. We will
23251 enter the class scope itself while processing the members. */
23252 if (pushed_scope)
23253 pop_scope (pushed_scope);
23255 if (invalid_explicit_specialization_p)
23257 end_specialization ();
23258 --parser->num_template_parameter_lists;
23261 if (type)
23262 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23263 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23264 CLASSTYPE_FINAL (type) = 1;
23265 out:
23266 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23267 return type;
23270 /* Parse a class-key.
23272 class-key:
23273 class
23274 struct
23275 union
23277 Returns the kind of class-key specified, or none_type to indicate
23278 error. */
23280 static enum tag_types
23281 cp_parser_class_key (cp_parser* parser)
23283 cp_token *token;
23284 enum tag_types tag_type;
23286 /* Look for the class-key. */
23287 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23288 if (!token)
23289 return none_type;
23291 /* Check to see if the TOKEN is a class-key. */
23292 tag_type = cp_parser_token_is_class_key (token);
23293 if (!tag_type)
23294 cp_parser_error (parser, "expected class-key");
23295 return tag_type;
23298 /* Parse a type-parameter-key.
23300 type-parameter-key:
23301 class
23302 typename
23305 static void
23306 cp_parser_type_parameter_key (cp_parser* parser)
23308 /* Look for the type-parameter-key. */
23309 enum tag_types tag_type = none_type;
23310 cp_token *token = cp_lexer_peek_token (parser->lexer);
23311 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23313 cp_lexer_consume_token (parser->lexer);
23314 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23315 /* typename is not allowed in a template template parameter
23316 by the standard until C++17. */
23317 pedwarn (token->location, OPT_Wpedantic,
23318 "ISO C++ forbids typename key in template template parameter;"
23319 " use -std=c++17 or -std=gnu++17");
23321 else
23322 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23324 return;
23327 /* Parse an (optional) member-specification.
23329 member-specification:
23330 member-declaration member-specification [opt]
23331 access-specifier : member-specification [opt] */
23333 static void
23334 cp_parser_member_specification_opt (cp_parser* parser)
23336 while (true)
23338 cp_token *token;
23339 enum rid keyword;
23341 /* Peek at the next token. */
23342 token = cp_lexer_peek_token (parser->lexer);
23343 /* If it's a `}', or EOF then we've seen all the members. */
23344 if (token->type == CPP_CLOSE_BRACE
23345 || token->type == CPP_EOF
23346 || token->type == CPP_PRAGMA_EOL)
23347 break;
23349 /* See if this token is a keyword. */
23350 keyword = token->keyword;
23351 switch (keyword)
23353 case RID_PUBLIC:
23354 case RID_PROTECTED:
23355 case RID_PRIVATE:
23356 /* Consume the access-specifier. */
23357 cp_lexer_consume_token (parser->lexer);
23358 /* Remember which access-specifier is active. */
23359 current_access_specifier = token->u.value;
23360 /* Look for the `:'. */
23361 cp_parser_require (parser, CPP_COLON, RT_COLON);
23362 break;
23364 default:
23365 /* Accept #pragmas at class scope. */
23366 if (token->type == CPP_PRAGMA)
23368 cp_parser_pragma (parser, pragma_member, NULL);
23369 break;
23372 /* Otherwise, the next construction must be a
23373 member-declaration. */
23374 cp_parser_member_declaration (parser);
23379 /* Parse a member-declaration.
23381 member-declaration:
23382 decl-specifier-seq [opt] member-declarator-list [opt] ;
23383 function-definition ; [opt]
23384 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23385 using-declaration
23386 template-declaration
23387 alias-declaration
23389 member-declarator-list:
23390 member-declarator
23391 member-declarator-list , member-declarator
23393 member-declarator:
23394 declarator pure-specifier [opt]
23395 declarator constant-initializer [opt]
23396 identifier [opt] : constant-expression
23398 GNU Extensions:
23400 member-declaration:
23401 __extension__ member-declaration
23403 member-declarator:
23404 declarator attributes [opt] pure-specifier [opt]
23405 declarator attributes [opt] constant-initializer [opt]
23406 identifier [opt] attributes [opt] : constant-expression
23408 C++0x Extensions:
23410 member-declaration:
23411 static_assert-declaration */
23413 static void
23414 cp_parser_member_declaration (cp_parser* parser)
23416 cp_decl_specifier_seq decl_specifiers;
23417 tree prefix_attributes;
23418 tree decl;
23419 int declares_class_or_enum;
23420 bool friend_p;
23421 cp_token *token = NULL;
23422 cp_token *decl_spec_token_start = NULL;
23423 cp_token *initializer_token_start = NULL;
23424 int saved_pedantic;
23425 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23427 /* Check for the `__extension__' keyword. */
23428 if (cp_parser_extension_opt (parser, &saved_pedantic))
23430 /* Recurse. */
23431 cp_parser_member_declaration (parser);
23432 /* Restore the old value of the PEDANTIC flag. */
23433 pedantic = saved_pedantic;
23435 return;
23438 /* Check for a template-declaration. */
23439 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23441 /* An explicit specialization here is an error condition, and we
23442 expect the specialization handler to detect and report this. */
23443 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23444 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23445 cp_parser_explicit_specialization (parser);
23446 else
23447 cp_parser_template_declaration (parser, /*member_p=*/true);
23449 return;
23451 /* Check for a template introduction. */
23452 else if (cp_parser_template_declaration_after_export (parser, true))
23453 return;
23455 /* Check for a using-declaration. */
23456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23458 if (cxx_dialect < cxx11)
23460 /* Parse the using-declaration. */
23461 cp_parser_using_declaration (parser,
23462 /*access_declaration_p=*/false);
23463 return;
23465 else
23467 tree decl;
23468 bool alias_decl_expected;
23469 cp_parser_parse_tentatively (parser);
23470 decl = cp_parser_alias_declaration (parser);
23471 /* Note that if we actually see the '=' token after the
23472 identifier, cp_parser_alias_declaration commits the
23473 tentative parse. In that case, we really expect an
23474 alias-declaration. Otherwise, we expect a using
23475 declaration. */
23476 alias_decl_expected =
23477 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23478 cp_parser_parse_definitely (parser);
23480 if (alias_decl_expected)
23481 finish_member_declaration (decl);
23482 else
23483 cp_parser_using_declaration (parser,
23484 /*access_declaration_p=*/false);
23485 return;
23489 /* Check for @defs. */
23490 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23492 tree ivar, member;
23493 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23494 ivar = ivar_chains;
23495 while (ivar)
23497 member = ivar;
23498 ivar = TREE_CHAIN (member);
23499 TREE_CHAIN (member) = NULL_TREE;
23500 finish_member_declaration (member);
23502 return;
23505 /* If the next token is `static_assert' we have a static assertion. */
23506 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23508 cp_parser_static_assert (parser, /*member_p=*/true);
23509 return;
23512 parser->colon_corrects_to_scope_p = false;
23514 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23515 goto out;
23517 /* Parse the decl-specifier-seq. */
23518 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23519 cp_parser_decl_specifier_seq (parser,
23520 CP_PARSER_FLAGS_OPTIONAL,
23521 &decl_specifiers,
23522 &declares_class_or_enum);
23523 /* Check for an invalid type-name. */
23524 if (!decl_specifiers.any_type_specifiers_p
23525 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23526 goto out;
23527 /* If there is no declarator, then the decl-specifier-seq should
23528 specify a type. */
23529 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23531 /* If there was no decl-specifier-seq, and the next token is a
23532 `;', then we have something like:
23534 struct S { ; };
23536 [class.mem]
23538 Each member-declaration shall declare at least one member
23539 name of the class. */
23540 if (!decl_specifiers.any_specifiers_p)
23542 cp_token *token = cp_lexer_peek_token (parser->lexer);
23543 if (!in_system_header_at (token->location))
23545 gcc_rich_location richloc (token->location);
23546 richloc.add_fixit_remove ();
23547 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23550 else
23552 tree type;
23554 /* See if this declaration is a friend. */
23555 friend_p = cp_parser_friend_p (&decl_specifiers);
23556 /* If there were decl-specifiers, check to see if there was
23557 a class-declaration. */
23558 type = check_tag_decl (&decl_specifiers,
23559 /*explicit_type_instantiation_p=*/false);
23560 /* Nested classes have already been added to the class, but
23561 a `friend' needs to be explicitly registered. */
23562 if (friend_p)
23564 /* If the `friend' keyword was present, the friend must
23565 be introduced with a class-key. */
23566 if (!declares_class_or_enum && cxx_dialect < cxx11)
23567 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23568 "in C++03 a class-key must be used "
23569 "when declaring a friend");
23570 /* In this case:
23572 template <typename T> struct A {
23573 friend struct A<T>::B;
23576 A<T>::B will be represented by a TYPENAME_TYPE, and
23577 therefore not recognized by check_tag_decl. */
23578 if (!type)
23580 type = decl_specifiers.type;
23581 if (type && TREE_CODE (type) == TYPE_DECL)
23582 type = TREE_TYPE (type);
23584 if (!type || !TYPE_P (type))
23585 error_at (decl_spec_token_start->location,
23586 "friend declaration does not name a class or "
23587 "function");
23588 else
23589 make_friend_class (current_class_type, type,
23590 /*complain=*/true);
23592 /* If there is no TYPE, an error message will already have
23593 been issued. */
23594 else if (!type || type == error_mark_node)
23596 /* An anonymous aggregate has to be handled specially; such
23597 a declaration really declares a data member (with a
23598 particular type), as opposed to a nested class. */
23599 else if (ANON_AGGR_TYPE_P (type))
23601 /* C++11 9.5/6. */
23602 if (decl_specifiers.storage_class != sc_none)
23603 error_at (decl_spec_token_start->location,
23604 "a storage class on an anonymous aggregate "
23605 "in class scope is not allowed");
23607 /* Remove constructors and such from TYPE, now that we
23608 know it is an anonymous aggregate. */
23609 fixup_anonymous_aggr (type);
23610 /* And make the corresponding data member. */
23611 decl = build_decl (decl_spec_token_start->location,
23612 FIELD_DECL, NULL_TREE, type);
23613 /* Add it to the class. */
23614 finish_member_declaration (decl);
23616 else
23617 cp_parser_check_access_in_redeclaration
23618 (TYPE_NAME (type),
23619 decl_spec_token_start->location);
23622 else
23624 bool assume_semicolon = false;
23626 /* Clear attributes from the decl_specifiers but keep them
23627 around as prefix attributes that apply them to the entity
23628 being declared. */
23629 prefix_attributes = decl_specifiers.attributes;
23630 decl_specifiers.attributes = NULL_TREE;
23632 /* See if these declarations will be friends. */
23633 friend_p = cp_parser_friend_p (&decl_specifiers);
23635 /* Keep going until we hit the `;' at the end of the
23636 declaration. */
23637 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23639 tree attributes = NULL_TREE;
23640 tree first_attribute;
23641 tree initializer;
23642 bool named_bitfld = false;
23644 /* Peek at the next token. */
23645 token = cp_lexer_peek_token (parser->lexer);
23647 /* The following code wants to know early if it is a bit-field
23648 or some other declaration. Attributes can appear before
23649 the `:' token. Skip over them without consuming any tokens
23650 to peek if they are followed by `:'. */
23651 if (cp_next_tokens_can_be_attribute_p (parser)
23652 || (token->type == CPP_NAME
23653 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23654 && (named_bitfld = true)))
23656 size_t n
23657 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23658 token = cp_lexer_peek_nth_token (parser->lexer, n);
23661 /* Check for a bitfield declaration. */
23662 if (token->type == CPP_COLON
23663 || (token->type == CPP_NAME
23664 && token == cp_lexer_peek_token (parser->lexer)
23665 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23666 && (named_bitfld = true)))
23668 tree identifier;
23669 tree width;
23670 tree late_attributes = NULL_TREE;
23672 if (named_bitfld)
23673 identifier = cp_parser_identifier (parser);
23674 else
23675 identifier = NULL_TREE;
23677 /* Look for attributes that apply to the bitfield. */
23678 attributes = cp_parser_attributes_opt (parser);
23680 /* Consume the `:' token. */
23681 cp_lexer_consume_token (parser->lexer);
23683 /* Get the width of the bitfield. */
23684 width = cp_parser_constant_expression (parser, false, NULL,
23685 cxx_dialect >= cxx11);
23687 /* In C++2A and as extension for C++11 and above we allow
23688 default member initializers for bit-fields. */
23689 initializer = NULL_TREE;
23690 if (cxx_dialect >= cxx11
23691 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23692 || cp_lexer_next_token_is (parser->lexer,
23693 CPP_OPEN_BRACE)))
23695 location_t loc
23696 = cp_lexer_peek_token (parser->lexer)->location;
23697 if (cxx_dialect < cxx2a
23698 && !in_system_header_at (loc)
23699 && identifier != NULL_TREE)
23700 pedwarn (loc, 0,
23701 "default member initializers for bit-fields "
23702 "only available with -std=c++2a or "
23703 "-std=gnu++2a");
23705 initializer = cp_parser_save_nsdmi (parser);
23706 if (identifier == NULL_TREE)
23708 error_at (loc, "default member initializer for "
23709 "unnamed bit-field");
23710 initializer = NULL_TREE;
23713 else
23715 /* Look for attributes that apply to the bitfield after
23716 the `:' token and width. This is where GCC used to
23717 parse attributes in the past, pedwarn if there is
23718 a std attribute. */
23719 if (cp_next_tokens_can_be_std_attribute_p (parser))
23720 pedwarn (input_location, OPT_Wpedantic,
23721 "ISO C++ allows bit-field attributes only "
23722 "before the %<:%> token");
23724 late_attributes = cp_parser_attributes_opt (parser);
23727 attributes = attr_chainon (attributes, late_attributes);
23729 /* Remember which attributes are prefix attributes and
23730 which are not. */
23731 first_attribute = attributes;
23732 /* Combine the attributes. */
23733 attributes = attr_chainon (prefix_attributes, attributes);
23735 /* Create the bitfield declaration. */
23736 decl = grokbitfield (identifier
23737 ? make_id_declarator (NULL_TREE,
23738 identifier,
23739 sfk_none)
23740 : NULL,
23741 &decl_specifiers,
23742 width, initializer,
23743 attributes);
23745 else
23747 cp_declarator *declarator;
23748 tree asm_specification;
23749 int ctor_dtor_or_conv_p;
23751 /* Parse the declarator. */
23752 declarator
23753 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23754 &ctor_dtor_or_conv_p,
23755 /*parenthesized_p=*/NULL,
23756 /*member_p=*/true,
23757 friend_p);
23759 /* If something went wrong parsing the declarator, make sure
23760 that we at least consume some tokens. */
23761 if (declarator == cp_error_declarator)
23763 /* Skip to the end of the statement. */
23764 cp_parser_skip_to_end_of_statement (parser);
23765 /* If the next token is not a semicolon, that is
23766 probably because we just skipped over the body of
23767 a function. So, we consume a semicolon if
23768 present, but do not issue an error message if it
23769 is not present. */
23770 if (cp_lexer_next_token_is (parser->lexer,
23771 CPP_SEMICOLON))
23772 cp_lexer_consume_token (parser->lexer);
23773 goto out;
23776 if (declares_class_or_enum & 2)
23777 cp_parser_check_for_definition_in_return_type
23778 (declarator, decl_specifiers.type,
23779 decl_specifiers.locations[ds_type_spec]);
23781 /* Look for an asm-specification. */
23782 asm_specification = cp_parser_asm_specification_opt (parser);
23783 /* Look for attributes that apply to the declaration. */
23784 attributes = cp_parser_attributes_opt (parser);
23785 /* Remember which attributes are prefix attributes and
23786 which are not. */
23787 first_attribute = attributes;
23788 /* Combine the attributes. */
23789 attributes = attr_chainon (prefix_attributes, attributes);
23791 /* If it's an `=', then we have a constant-initializer or a
23792 pure-specifier. It is not correct to parse the
23793 initializer before registering the member declaration
23794 since the member declaration should be in scope while
23795 its initializer is processed. However, the rest of the
23796 front end does not yet provide an interface that allows
23797 us to handle this correctly. */
23798 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23800 /* In [class.mem]:
23802 A pure-specifier shall be used only in the declaration of
23803 a virtual function.
23805 A member-declarator can contain a constant-initializer
23806 only if it declares a static member of integral or
23807 enumeration type.
23809 Therefore, if the DECLARATOR is for a function, we look
23810 for a pure-specifier; otherwise, we look for a
23811 constant-initializer. When we call `grokfield', it will
23812 perform more stringent semantics checks. */
23813 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23814 if (function_declarator_p (declarator)
23815 || (decl_specifiers.type
23816 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23817 && declarator->kind == cdk_id
23818 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23819 == FUNCTION_TYPE)))
23820 initializer = cp_parser_pure_specifier (parser);
23821 else if (decl_specifiers.storage_class != sc_static)
23822 initializer = cp_parser_save_nsdmi (parser);
23823 else if (cxx_dialect >= cxx11)
23825 bool nonconst;
23826 /* Don't require a constant rvalue in C++11, since we
23827 might want a reference constant. We'll enforce
23828 constancy later. */
23829 cp_lexer_consume_token (parser->lexer);
23830 /* Parse the initializer. */
23831 initializer = cp_parser_initializer_clause (parser,
23832 &nonconst);
23834 else
23835 /* Parse the initializer. */
23836 initializer = cp_parser_constant_initializer (parser);
23838 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23839 && !function_declarator_p (declarator))
23841 bool x;
23842 if (decl_specifiers.storage_class != sc_static)
23843 initializer = cp_parser_save_nsdmi (parser);
23844 else
23845 initializer = cp_parser_initializer (parser, &x, &x);
23847 /* Otherwise, there is no initializer. */
23848 else
23849 initializer = NULL_TREE;
23851 /* See if we are probably looking at a function
23852 definition. We are certainly not looking at a
23853 member-declarator. Calling `grokfield' has
23854 side-effects, so we must not do it unless we are sure
23855 that we are looking at a member-declarator. */
23856 if (cp_parser_token_starts_function_definition_p
23857 (cp_lexer_peek_token (parser->lexer)))
23859 /* The grammar does not allow a pure-specifier to be
23860 used when a member function is defined. (It is
23861 possible that this fact is an oversight in the
23862 standard, since a pure function may be defined
23863 outside of the class-specifier. */
23864 if (initializer && initializer_token_start)
23865 error_at (initializer_token_start->location,
23866 "pure-specifier on function-definition");
23867 decl = cp_parser_save_member_function_body (parser,
23868 &decl_specifiers,
23869 declarator,
23870 attributes);
23871 if (parser->fully_implicit_function_template_p)
23872 decl = finish_fully_implicit_template (parser, decl);
23873 /* If the member was not a friend, declare it here. */
23874 if (!friend_p)
23875 finish_member_declaration (decl);
23876 /* Peek at the next token. */
23877 token = cp_lexer_peek_token (parser->lexer);
23878 /* If the next token is a semicolon, consume it. */
23879 if (token->type == CPP_SEMICOLON)
23881 location_t semicolon_loc
23882 = cp_lexer_consume_token (parser->lexer)->location;
23883 gcc_rich_location richloc (semicolon_loc);
23884 richloc.add_fixit_remove ();
23885 warning_at (&richloc, OPT_Wextra_semi,
23886 "extra %<;%> after in-class "
23887 "function definition");
23889 goto out;
23891 else
23892 if (declarator->kind == cdk_function)
23893 declarator->id_loc = token->location;
23894 /* Create the declaration. */
23895 decl = grokfield (declarator, &decl_specifiers,
23896 initializer, /*init_const_expr_p=*/true,
23897 asm_specification, attributes);
23898 if (parser->fully_implicit_function_template_p)
23900 if (friend_p)
23901 finish_fully_implicit_template (parser, 0);
23902 else
23903 decl = finish_fully_implicit_template (parser, decl);
23907 cp_finalize_omp_declare_simd (parser, decl);
23908 cp_finalize_oacc_routine (parser, decl, false);
23910 /* Reset PREFIX_ATTRIBUTES. */
23911 if (attributes != error_mark_node)
23913 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23914 attributes = TREE_CHAIN (attributes);
23915 if (attributes)
23916 TREE_CHAIN (attributes) = NULL_TREE;
23919 /* If there is any qualification still in effect, clear it
23920 now; we will be starting fresh with the next declarator. */
23921 parser->scope = NULL_TREE;
23922 parser->qualifying_scope = NULL_TREE;
23923 parser->object_scope = NULL_TREE;
23924 /* If it's a `,', then there are more declarators. */
23925 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23927 cp_lexer_consume_token (parser->lexer);
23928 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23930 cp_token *token = cp_lexer_previous_token (parser->lexer);
23931 gcc_rich_location richloc (token->location);
23932 richloc.add_fixit_remove ();
23933 error_at (&richloc, "stray %<,%> at end of "
23934 "member declaration");
23937 /* If the next token isn't a `;', then we have a parse error. */
23938 else if (cp_lexer_next_token_is_not (parser->lexer,
23939 CPP_SEMICOLON))
23941 /* The next token might be a ways away from where the
23942 actual semicolon is missing. Find the previous token
23943 and use that for our error position. */
23944 cp_token *token = cp_lexer_previous_token (parser->lexer);
23945 gcc_rich_location richloc (token->location);
23946 richloc.add_fixit_insert_after (";");
23947 error_at (&richloc, "expected %<;%> at end of "
23948 "member declaration");
23950 /* Assume that the user meant to provide a semicolon. If
23951 we were to cp_parser_skip_to_end_of_statement, we might
23952 skip to a semicolon inside a member function definition
23953 and issue nonsensical error messages. */
23954 assume_semicolon = true;
23957 if (decl)
23959 /* Add DECL to the list of members. */
23960 if (!friend_p
23961 /* Explicitly include, eg, NSDMIs, for better error
23962 recovery (c++/58650). */
23963 || !DECL_DECLARES_FUNCTION_P (decl))
23964 finish_member_declaration (decl);
23966 if (TREE_CODE (decl) == FUNCTION_DECL)
23967 cp_parser_save_default_args (parser, decl);
23968 else if (TREE_CODE (decl) == FIELD_DECL
23969 && DECL_INITIAL (decl))
23970 /* Add DECL to the queue of NSDMI to be parsed later. */
23971 vec_safe_push (unparsed_nsdmis, decl);
23974 if (assume_semicolon)
23975 goto out;
23979 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23980 out:
23981 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23984 /* Parse a pure-specifier.
23986 pure-specifier:
23989 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23990 Otherwise, ERROR_MARK_NODE is returned. */
23992 static tree
23993 cp_parser_pure_specifier (cp_parser* parser)
23995 cp_token *token;
23997 /* Look for the `=' token. */
23998 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23999 return error_mark_node;
24000 /* Look for the `0' token. */
24001 token = cp_lexer_peek_token (parser->lexer);
24003 if (token->type == CPP_EOF
24004 || token->type == CPP_PRAGMA_EOL)
24005 return error_mark_node;
24007 cp_lexer_consume_token (parser->lexer);
24009 /* Accept = default or = delete in c++0x mode. */
24010 if (token->keyword == RID_DEFAULT
24011 || token->keyword == RID_DELETE)
24013 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24014 return token->u.value;
24017 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24018 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24020 cp_parser_error (parser,
24021 "invalid pure specifier (only %<= 0%> is allowed)");
24022 cp_parser_skip_to_end_of_statement (parser);
24023 return error_mark_node;
24025 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24027 error_at (token->location, "templates may not be %<virtual%>");
24028 return error_mark_node;
24031 return integer_zero_node;
24034 /* Parse a constant-initializer.
24036 constant-initializer:
24037 = constant-expression
24039 Returns a representation of the constant-expression. */
24041 static tree
24042 cp_parser_constant_initializer (cp_parser* parser)
24044 /* Look for the `=' token. */
24045 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24046 return error_mark_node;
24048 /* It is invalid to write:
24050 struct S { static const int i = { 7 }; };
24053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24055 cp_parser_error (parser,
24056 "a brace-enclosed initializer is not allowed here");
24057 /* Consume the opening brace. */
24058 matching_braces braces;
24059 braces.consume_open (parser);
24060 /* Skip the initializer. */
24061 cp_parser_skip_to_closing_brace (parser);
24062 /* Look for the trailing `}'. */
24063 braces.require_close (parser);
24065 return error_mark_node;
24068 return cp_parser_constant_expression (parser);
24071 /* Derived classes [gram.class.derived] */
24073 /* Parse a base-clause.
24075 base-clause:
24076 : base-specifier-list
24078 base-specifier-list:
24079 base-specifier ... [opt]
24080 base-specifier-list , base-specifier ... [opt]
24082 Returns a TREE_LIST representing the base-classes, in the order in
24083 which they were declared. The representation of each node is as
24084 described by cp_parser_base_specifier.
24086 In the case that no bases are specified, this function will return
24087 NULL_TREE, not ERROR_MARK_NODE. */
24089 static tree
24090 cp_parser_base_clause (cp_parser* parser)
24092 tree bases = NULL_TREE;
24094 /* Look for the `:' that begins the list. */
24095 cp_parser_require (parser, CPP_COLON, RT_COLON);
24097 /* Scan the base-specifier-list. */
24098 while (true)
24100 cp_token *token;
24101 tree base;
24102 bool pack_expansion_p = false;
24104 /* Look for the base-specifier. */
24105 base = cp_parser_base_specifier (parser);
24106 /* Look for the (optional) ellipsis. */
24107 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24109 /* Consume the `...'. */
24110 cp_lexer_consume_token (parser->lexer);
24112 pack_expansion_p = true;
24115 /* Add BASE to the front of the list. */
24116 if (base && base != error_mark_node)
24118 if (pack_expansion_p)
24119 /* Make this a pack expansion type. */
24120 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24122 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24124 TREE_CHAIN (base) = bases;
24125 bases = base;
24128 /* Peek at the next token. */
24129 token = cp_lexer_peek_token (parser->lexer);
24130 /* If it's not a comma, then the list is complete. */
24131 if (token->type != CPP_COMMA)
24132 break;
24133 /* Consume the `,'. */
24134 cp_lexer_consume_token (parser->lexer);
24137 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24138 base class had a qualified name. However, the next name that
24139 appears is certainly not qualified. */
24140 parser->scope = NULL_TREE;
24141 parser->qualifying_scope = NULL_TREE;
24142 parser->object_scope = NULL_TREE;
24144 return nreverse (bases);
24147 /* Parse a base-specifier.
24149 base-specifier:
24150 :: [opt] nested-name-specifier [opt] class-name
24151 virtual access-specifier [opt] :: [opt] nested-name-specifier
24152 [opt] class-name
24153 access-specifier virtual [opt] :: [opt] nested-name-specifier
24154 [opt] class-name
24156 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24157 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24158 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24159 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24161 static tree
24162 cp_parser_base_specifier (cp_parser* parser)
24164 cp_token *token;
24165 bool done = false;
24166 bool virtual_p = false;
24167 bool duplicate_virtual_error_issued_p = false;
24168 bool duplicate_access_error_issued_p = false;
24169 bool class_scope_p, template_p;
24170 tree access = access_default_node;
24171 tree type;
24173 /* Process the optional `virtual' and `access-specifier'. */
24174 while (!done)
24176 /* Peek at the next token. */
24177 token = cp_lexer_peek_token (parser->lexer);
24178 /* Process `virtual'. */
24179 switch (token->keyword)
24181 case RID_VIRTUAL:
24182 /* If `virtual' appears more than once, issue an error. */
24183 if (virtual_p && !duplicate_virtual_error_issued_p)
24185 cp_parser_error (parser,
24186 "%<virtual%> specified more than once in base-specifier");
24187 duplicate_virtual_error_issued_p = true;
24190 virtual_p = true;
24192 /* Consume the `virtual' token. */
24193 cp_lexer_consume_token (parser->lexer);
24195 break;
24197 case RID_PUBLIC:
24198 case RID_PROTECTED:
24199 case RID_PRIVATE:
24200 /* If more than one access specifier appears, issue an
24201 error. */
24202 if (access != access_default_node
24203 && !duplicate_access_error_issued_p)
24205 cp_parser_error (parser,
24206 "more than one access specifier in base-specifier");
24207 duplicate_access_error_issued_p = true;
24210 access = ridpointers[(int) token->keyword];
24212 /* Consume the access-specifier. */
24213 cp_lexer_consume_token (parser->lexer);
24215 break;
24217 default:
24218 done = true;
24219 break;
24222 /* It is not uncommon to see programs mechanically, erroneously, use
24223 the 'typename' keyword to denote (dependent) qualified types
24224 as base classes. */
24225 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24227 token = cp_lexer_peek_token (parser->lexer);
24228 if (!processing_template_decl)
24229 error_at (token->location,
24230 "keyword %<typename%> not allowed outside of templates");
24231 else
24232 error_at (token->location,
24233 "keyword %<typename%> not allowed in this context "
24234 "(the base class is implicitly a type)");
24235 cp_lexer_consume_token (parser->lexer);
24238 /* Look for the optional `::' operator. */
24239 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24240 /* Look for the nested-name-specifier. The simplest way to
24241 implement:
24243 [temp.res]
24245 The keyword `typename' is not permitted in a base-specifier or
24246 mem-initializer; in these contexts a qualified name that
24247 depends on a template-parameter is implicitly assumed to be a
24248 type name.
24250 is to pretend that we have seen the `typename' keyword at this
24251 point. */
24252 cp_parser_nested_name_specifier_opt (parser,
24253 /*typename_keyword_p=*/true,
24254 /*check_dependency_p=*/true,
24255 /*type_p=*/true,
24256 /*is_declaration=*/true);
24257 /* If the base class is given by a qualified name, assume that names
24258 we see are type names or templates, as appropriate. */
24259 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24260 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24262 if (!parser->scope
24263 && cp_lexer_next_token_is_decltype (parser->lexer))
24264 /* DR 950 allows decltype as a base-specifier. */
24265 type = cp_parser_decltype (parser);
24266 else
24268 /* Otherwise, look for the class-name. */
24269 type = cp_parser_class_name (parser,
24270 class_scope_p,
24271 template_p,
24272 typename_type,
24273 /*check_dependency_p=*/true,
24274 /*class_head_p=*/false,
24275 /*is_declaration=*/true);
24276 type = TREE_TYPE (type);
24279 if (type == error_mark_node)
24280 return error_mark_node;
24282 return finish_base_specifier (type, access, virtual_p);
24285 /* Exception handling [gram.exception] */
24287 /* Parse an (optional) noexcept-specification.
24289 noexcept-specification:
24290 noexcept ( constant-expression ) [opt]
24292 If no noexcept-specification is present, returns NULL_TREE.
24293 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24294 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24295 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24296 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24297 in which case a boolean condition is returned instead. */
24299 static tree
24300 cp_parser_noexcept_specification_opt (cp_parser* parser,
24301 bool require_constexpr,
24302 bool* consumed_expr,
24303 bool return_cond)
24305 cp_token *token;
24306 const char *saved_message;
24308 /* Peek at the next token. */
24309 token = cp_lexer_peek_token (parser->lexer);
24311 /* Is it a noexcept-specification? */
24312 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24314 tree expr;
24315 cp_lexer_consume_token (parser->lexer);
24317 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24319 matching_parens parens;
24320 parens.consume_open (parser);
24322 if (require_constexpr)
24324 /* Types may not be defined in an exception-specification. */
24325 saved_message = parser->type_definition_forbidden_message;
24326 parser->type_definition_forbidden_message
24327 = G_("types may not be defined in an exception-specification");
24329 expr = cp_parser_constant_expression (parser);
24331 /* Restore the saved message. */
24332 parser->type_definition_forbidden_message = saved_message;
24334 else
24336 expr = cp_parser_expression (parser);
24337 *consumed_expr = true;
24340 parens.require_close (parser);
24342 else
24344 expr = boolean_true_node;
24345 if (!require_constexpr)
24346 *consumed_expr = false;
24349 /* We cannot build a noexcept-spec right away because this will check
24350 that expr is a constexpr. */
24351 if (!return_cond)
24352 return build_noexcept_spec (expr, tf_warning_or_error);
24353 else
24354 return expr;
24356 else
24357 return NULL_TREE;
24360 /* Parse an (optional) exception-specification.
24362 exception-specification:
24363 throw ( type-id-list [opt] )
24365 Returns a TREE_LIST representing the exception-specification. The
24366 TREE_VALUE of each node is a type. */
24368 static tree
24369 cp_parser_exception_specification_opt (cp_parser* parser)
24371 cp_token *token;
24372 tree type_id_list;
24373 const char *saved_message;
24375 /* Peek at the next token. */
24376 token = cp_lexer_peek_token (parser->lexer);
24378 /* Is it a noexcept-specification? */
24379 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24380 false);
24381 if (type_id_list != NULL_TREE)
24382 return type_id_list;
24384 /* If it's not `throw', then there's no exception-specification. */
24385 if (!cp_parser_is_keyword (token, RID_THROW))
24386 return NULL_TREE;
24388 location_t loc = token->location;
24390 /* Consume the `throw'. */
24391 cp_lexer_consume_token (parser->lexer);
24393 /* Look for the `('. */
24394 matching_parens parens;
24395 parens.require_open (parser);
24397 /* Peek at the next token. */
24398 token = cp_lexer_peek_token (parser->lexer);
24399 /* If it's not a `)', then there is a type-id-list. */
24400 if (token->type != CPP_CLOSE_PAREN)
24402 /* Types may not be defined in an exception-specification. */
24403 saved_message = parser->type_definition_forbidden_message;
24404 parser->type_definition_forbidden_message
24405 = G_("types may not be defined in an exception-specification");
24406 /* Parse the type-id-list. */
24407 type_id_list = cp_parser_type_id_list (parser);
24408 /* Restore the saved message. */
24409 parser->type_definition_forbidden_message = saved_message;
24411 if (cxx_dialect >= cxx17)
24413 error_at (loc, "ISO C++17 does not allow dynamic exception "
24414 "specifications");
24415 type_id_list = NULL_TREE;
24417 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24418 warning_at (loc, OPT_Wdeprecated,
24419 "dynamic exception specifications are deprecated in "
24420 "C++11");
24422 /* In C++17, throw() is equivalent to noexcept (true). throw()
24423 is deprecated in C++11 and above as well, but is still widely used,
24424 so don't warn about it yet. */
24425 else if (cxx_dialect >= cxx17)
24426 type_id_list = noexcept_true_spec;
24427 else
24428 type_id_list = empty_except_spec;
24430 /* Look for the `)'. */
24431 parens.require_close (parser);
24433 return type_id_list;
24436 /* Parse an (optional) type-id-list.
24438 type-id-list:
24439 type-id ... [opt]
24440 type-id-list , type-id ... [opt]
24442 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24443 in the order that the types were presented. */
24445 static tree
24446 cp_parser_type_id_list (cp_parser* parser)
24448 tree types = NULL_TREE;
24450 while (true)
24452 cp_token *token;
24453 tree type;
24455 token = cp_lexer_peek_token (parser->lexer);
24457 /* Get the next type-id. */
24458 type = cp_parser_type_id (parser);
24459 /* Check for invalid 'auto'. */
24460 if (flag_concepts && type_uses_auto (type))
24462 error_at (token->location,
24463 "invalid use of %<auto%> in exception-specification");
24464 type = error_mark_node;
24466 /* Parse the optional ellipsis. */
24467 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24469 /* Consume the `...'. */
24470 cp_lexer_consume_token (parser->lexer);
24472 /* Turn the type into a pack expansion expression. */
24473 type = make_pack_expansion (type);
24475 /* Add it to the list. */
24476 types = add_exception_specifier (types, type, /*complain=*/1);
24477 /* Peek at the next token. */
24478 token = cp_lexer_peek_token (parser->lexer);
24479 /* If it is not a `,', we are done. */
24480 if (token->type != CPP_COMMA)
24481 break;
24482 /* Consume the `,'. */
24483 cp_lexer_consume_token (parser->lexer);
24486 return nreverse (types);
24489 /* Parse a try-block.
24491 try-block:
24492 try compound-statement handler-seq */
24494 static tree
24495 cp_parser_try_block (cp_parser* parser)
24497 tree try_block;
24499 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24500 if (parser->in_function_body
24501 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24502 error ("%<try%> in %<constexpr%> function");
24504 try_block = begin_try_block ();
24505 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24506 finish_try_block (try_block);
24507 cp_parser_handler_seq (parser);
24508 finish_handler_sequence (try_block);
24510 return try_block;
24513 /* Parse a function-try-block.
24515 function-try-block:
24516 try ctor-initializer [opt] function-body handler-seq */
24518 static void
24519 cp_parser_function_try_block (cp_parser* parser)
24521 tree compound_stmt;
24522 tree try_block;
24524 /* Look for the `try' keyword. */
24525 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24526 return;
24527 /* Let the rest of the front end know where we are. */
24528 try_block = begin_function_try_block (&compound_stmt);
24529 /* Parse the function-body. */
24530 cp_parser_ctor_initializer_opt_and_function_body
24531 (parser, /*in_function_try_block=*/true);
24532 /* We're done with the `try' part. */
24533 finish_function_try_block (try_block);
24534 /* Parse the handlers. */
24535 cp_parser_handler_seq (parser);
24536 /* We're done with the handlers. */
24537 finish_function_handler_sequence (try_block, compound_stmt);
24540 /* Parse a handler-seq.
24542 handler-seq:
24543 handler handler-seq [opt] */
24545 static void
24546 cp_parser_handler_seq (cp_parser* parser)
24548 while (true)
24550 cp_token *token;
24552 /* Parse the handler. */
24553 cp_parser_handler (parser);
24554 /* Peek at the next token. */
24555 token = cp_lexer_peek_token (parser->lexer);
24556 /* If it's not `catch' then there are no more handlers. */
24557 if (!cp_parser_is_keyword (token, RID_CATCH))
24558 break;
24562 /* Parse a handler.
24564 handler:
24565 catch ( exception-declaration ) compound-statement */
24567 static void
24568 cp_parser_handler (cp_parser* parser)
24570 tree handler;
24571 tree declaration;
24573 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24574 handler = begin_handler ();
24575 matching_parens parens;
24576 parens.require_open (parser);
24577 declaration = cp_parser_exception_declaration (parser);
24578 finish_handler_parms (declaration, handler);
24579 parens.require_close (parser);
24580 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24581 finish_handler (handler);
24584 /* Parse an exception-declaration.
24586 exception-declaration:
24587 type-specifier-seq declarator
24588 type-specifier-seq abstract-declarator
24589 type-specifier-seq
24592 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24593 ellipsis variant is used. */
24595 static tree
24596 cp_parser_exception_declaration (cp_parser* parser)
24598 cp_decl_specifier_seq type_specifiers;
24599 cp_declarator *declarator;
24600 const char *saved_message;
24602 /* If it's an ellipsis, it's easy to handle. */
24603 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24605 /* Consume the `...' token. */
24606 cp_lexer_consume_token (parser->lexer);
24607 return NULL_TREE;
24610 /* Types may not be defined in exception-declarations. */
24611 saved_message = parser->type_definition_forbidden_message;
24612 parser->type_definition_forbidden_message
24613 = G_("types may not be defined in exception-declarations");
24615 /* Parse the type-specifier-seq. */
24616 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24617 /*is_trailing_return=*/false,
24618 &type_specifiers);
24619 /* If it's a `)', then there is no declarator. */
24620 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24621 declarator = NULL;
24622 else
24623 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24624 /*ctor_dtor_or_conv_p=*/NULL,
24625 /*parenthesized_p=*/NULL,
24626 /*member_p=*/false,
24627 /*friend_p=*/false);
24629 /* Restore the saved message. */
24630 parser->type_definition_forbidden_message = saved_message;
24632 if (!type_specifiers.any_specifiers_p)
24633 return error_mark_node;
24635 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24638 /* Parse a throw-expression.
24640 throw-expression:
24641 throw assignment-expression [opt]
24643 Returns a THROW_EXPR representing the throw-expression. */
24645 static tree
24646 cp_parser_throw_expression (cp_parser* parser)
24648 tree expression;
24649 cp_token* token;
24651 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24652 token = cp_lexer_peek_token (parser->lexer);
24653 /* Figure out whether or not there is an assignment-expression
24654 following the "throw" keyword. */
24655 if (token->type == CPP_COMMA
24656 || token->type == CPP_SEMICOLON
24657 || token->type == CPP_CLOSE_PAREN
24658 || token->type == CPP_CLOSE_SQUARE
24659 || token->type == CPP_CLOSE_BRACE
24660 || token->type == CPP_COLON)
24661 expression = NULL_TREE;
24662 else
24663 expression = cp_parser_assignment_expression (parser);
24665 return build_throw (expression);
24668 /* GNU Extensions */
24670 /* Parse an (optional) asm-specification.
24672 asm-specification:
24673 asm ( string-literal )
24675 If the asm-specification is present, returns a STRING_CST
24676 corresponding to the string-literal. Otherwise, returns
24677 NULL_TREE. */
24679 static tree
24680 cp_parser_asm_specification_opt (cp_parser* parser)
24682 cp_token *token;
24683 tree asm_specification;
24685 /* Peek at the next token. */
24686 token = cp_lexer_peek_token (parser->lexer);
24687 /* If the next token isn't the `asm' keyword, then there's no
24688 asm-specification. */
24689 if (!cp_parser_is_keyword (token, RID_ASM))
24690 return NULL_TREE;
24692 /* Consume the `asm' token. */
24693 cp_lexer_consume_token (parser->lexer);
24694 /* Look for the `('. */
24695 matching_parens parens;
24696 parens.require_open (parser);
24698 /* Look for the string-literal. */
24699 asm_specification = cp_parser_string_literal (parser, false, false);
24701 /* Look for the `)'. */
24702 parens.require_close (parser);
24704 return asm_specification;
24707 /* Parse an asm-operand-list.
24709 asm-operand-list:
24710 asm-operand
24711 asm-operand-list , asm-operand
24713 asm-operand:
24714 string-literal ( expression )
24715 [ string-literal ] string-literal ( expression )
24717 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24718 each node is the expression. The TREE_PURPOSE is itself a
24719 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24720 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24721 is a STRING_CST for the string literal before the parenthesis. Returns
24722 ERROR_MARK_NODE if any of the operands are invalid. */
24724 static tree
24725 cp_parser_asm_operand_list (cp_parser* parser)
24727 tree asm_operands = NULL_TREE;
24728 bool invalid_operands = false;
24730 while (true)
24732 tree string_literal;
24733 tree expression;
24734 tree name;
24736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24738 /* Consume the `[' token. */
24739 cp_lexer_consume_token (parser->lexer);
24740 /* Read the operand name. */
24741 name = cp_parser_identifier (parser);
24742 if (name != error_mark_node)
24743 name = build_string (IDENTIFIER_LENGTH (name),
24744 IDENTIFIER_POINTER (name));
24745 /* Look for the closing `]'. */
24746 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24748 else
24749 name = NULL_TREE;
24750 /* Look for the string-literal. */
24751 string_literal = cp_parser_string_literal (parser, false, false);
24753 /* Look for the `('. */
24754 matching_parens parens;
24755 parens.require_open (parser);
24756 /* Parse the expression. */
24757 expression = cp_parser_expression (parser);
24758 /* Look for the `)'. */
24759 parens.require_close (parser);
24761 if (name == error_mark_node
24762 || string_literal == error_mark_node
24763 || expression == error_mark_node)
24764 invalid_operands = true;
24766 /* Add this operand to the list. */
24767 asm_operands = tree_cons (build_tree_list (name, string_literal),
24768 expression,
24769 asm_operands);
24770 /* If the next token is not a `,', there are no more
24771 operands. */
24772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24773 break;
24774 /* Consume the `,'. */
24775 cp_lexer_consume_token (parser->lexer);
24778 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24781 /* Parse an asm-clobber-list.
24783 asm-clobber-list:
24784 string-literal
24785 asm-clobber-list , string-literal
24787 Returns a TREE_LIST, indicating the clobbers in the order that they
24788 appeared. The TREE_VALUE of each node is a STRING_CST. */
24790 static tree
24791 cp_parser_asm_clobber_list (cp_parser* parser)
24793 tree clobbers = NULL_TREE;
24795 while (true)
24797 tree string_literal;
24799 /* Look for the string literal. */
24800 string_literal = cp_parser_string_literal (parser, false, false);
24801 /* Add it to the list. */
24802 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24803 /* If the next token is not a `,', then the list is
24804 complete. */
24805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24806 break;
24807 /* Consume the `,' token. */
24808 cp_lexer_consume_token (parser->lexer);
24811 return clobbers;
24814 /* Parse an asm-label-list.
24816 asm-label-list:
24817 identifier
24818 asm-label-list , identifier
24820 Returns a TREE_LIST, indicating the labels in the order that they
24821 appeared. The TREE_VALUE of each node is a label. */
24823 static tree
24824 cp_parser_asm_label_list (cp_parser* parser)
24826 tree labels = NULL_TREE;
24828 while (true)
24830 tree identifier, label, name;
24832 /* Look for the identifier. */
24833 identifier = cp_parser_identifier (parser);
24834 if (!error_operand_p (identifier))
24836 label = lookup_label (identifier);
24837 if (TREE_CODE (label) == LABEL_DECL)
24839 TREE_USED (label) = 1;
24840 check_goto (label);
24841 name = build_string (IDENTIFIER_LENGTH (identifier),
24842 IDENTIFIER_POINTER (identifier));
24843 labels = tree_cons (name, label, labels);
24846 /* If the next token is not a `,', then the list is
24847 complete. */
24848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24849 break;
24850 /* Consume the `,' token. */
24851 cp_lexer_consume_token (parser->lexer);
24854 return nreverse (labels);
24857 /* Return TRUE iff the next tokens in the stream are possibly the
24858 beginning of a GNU extension attribute. */
24860 static bool
24861 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24863 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24866 /* Return TRUE iff the next tokens in the stream are possibly the
24867 beginning of a standard C++-11 attribute specifier. */
24869 static bool
24870 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24872 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24875 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24876 beginning of a standard C++-11 attribute specifier. */
24878 static bool
24879 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24881 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24883 return (cxx_dialect >= cxx11
24884 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24885 || (token->type == CPP_OPEN_SQUARE
24886 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24887 && token->type == CPP_OPEN_SQUARE)));
24890 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24891 beginning of a GNU extension attribute. */
24893 static bool
24894 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24896 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24898 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24901 /* Return true iff the next tokens can be the beginning of either a
24902 GNU attribute list, or a standard C++11 attribute sequence. */
24904 static bool
24905 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24907 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24908 || cp_next_tokens_can_be_std_attribute_p (parser));
24911 /* Return true iff the next Nth tokens can be the beginning of either
24912 a GNU attribute list, or a standard C++11 attribute sequence. */
24914 static bool
24915 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24917 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24918 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24921 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24922 of GNU attributes, or return NULL. */
24924 static tree
24925 cp_parser_attributes_opt (cp_parser *parser)
24927 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24928 return cp_parser_gnu_attributes_opt (parser);
24929 return cp_parser_std_attribute_spec_seq (parser);
24932 /* Parse an (optional) series of attributes.
24934 attributes:
24935 attributes attribute
24937 attribute:
24938 __attribute__ (( attribute-list [opt] ))
24940 The return value is as for cp_parser_gnu_attribute_list. */
24942 static tree
24943 cp_parser_gnu_attributes_opt (cp_parser* parser)
24945 tree attributes = NULL_TREE;
24947 while (true)
24949 cp_token *token;
24950 tree attribute_list;
24951 bool ok = true;
24953 /* Peek at the next token. */
24954 token = cp_lexer_peek_token (parser->lexer);
24955 /* If it's not `__attribute__', then we're done. */
24956 if (token->keyword != RID_ATTRIBUTE)
24957 break;
24959 /* Consume the `__attribute__' keyword. */
24960 cp_lexer_consume_token (parser->lexer);
24961 /* Look for the two `(' tokens. */
24962 matching_parens outer_parens;
24963 outer_parens.require_open (parser);
24964 matching_parens inner_parens;
24965 inner_parens.require_open (parser);
24967 /* Peek at the next token. */
24968 token = cp_lexer_peek_token (parser->lexer);
24969 if (token->type != CPP_CLOSE_PAREN)
24970 /* Parse the attribute-list. */
24971 attribute_list = cp_parser_gnu_attribute_list (parser);
24972 else
24973 /* If the next token is a `)', then there is no attribute
24974 list. */
24975 attribute_list = NULL;
24977 /* Look for the two `)' tokens. */
24978 if (!inner_parens.require_close (parser))
24979 ok = false;
24980 if (!outer_parens.require_close (parser))
24981 ok = false;
24982 if (!ok)
24983 cp_parser_skip_to_end_of_statement (parser);
24985 /* Add these new attributes to the list. */
24986 attributes = attr_chainon (attributes, attribute_list);
24989 return attributes;
24992 /* Parse a GNU attribute-list.
24994 attribute-list:
24995 attribute
24996 attribute-list , attribute
24998 attribute:
24999 identifier
25000 identifier ( identifier )
25001 identifier ( identifier , expression-list )
25002 identifier ( expression-list )
25004 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25005 to an attribute. The TREE_PURPOSE of each node is the identifier
25006 indicating which attribute is in use. The TREE_VALUE represents
25007 the arguments, if any. */
25009 static tree
25010 cp_parser_gnu_attribute_list (cp_parser* parser)
25012 tree attribute_list = NULL_TREE;
25013 bool save_translate_strings_p = parser->translate_strings_p;
25015 parser->translate_strings_p = false;
25016 while (true)
25018 cp_token *token;
25019 tree identifier;
25020 tree attribute;
25022 /* Look for the identifier. We also allow keywords here; for
25023 example `__attribute__ ((const))' is legal. */
25024 token = cp_lexer_peek_token (parser->lexer);
25025 if (token->type == CPP_NAME
25026 || token->type == CPP_KEYWORD)
25028 tree arguments = NULL_TREE;
25030 /* Consume the token, but save it since we need it for the
25031 SIMD enabled function parsing. */
25032 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25034 /* Save away the identifier that indicates which attribute
25035 this is. */
25036 identifier = (token->type == CPP_KEYWORD)
25037 /* For keywords, use the canonical spelling, not the
25038 parsed identifier. */
25039 ? ridpointers[(int) token->keyword]
25040 : id_token->u.value;
25042 identifier = canonicalize_attr_name (identifier);
25043 attribute = build_tree_list (identifier, NULL_TREE);
25045 /* Peek at the next token. */
25046 token = cp_lexer_peek_token (parser->lexer);
25047 /* If it's an `(', then parse the attribute arguments. */
25048 if (token->type == CPP_OPEN_PAREN)
25050 vec<tree, va_gc> *vec;
25051 int attr_flag = (attribute_takes_identifier_p (identifier)
25052 ? id_attr : normal_attr);
25053 vec = cp_parser_parenthesized_expression_list
25054 (parser, attr_flag, /*cast_p=*/false,
25055 /*allow_expansion_p=*/false,
25056 /*non_constant_p=*/NULL);
25057 if (vec == NULL)
25058 arguments = error_mark_node;
25059 else
25061 arguments = build_tree_list_vec (vec);
25062 release_tree_vector (vec);
25064 /* Save the arguments away. */
25065 TREE_VALUE (attribute) = arguments;
25068 if (arguments != error_mark_node)
25070 /* Add this attribute to the list. */
25071 TREE_CHAIN (attribute) = attribute_list;
25072 attribute_list = attribute;
25075 token = cp_lexer_peek_token (parser->lexer);
25077 /* Now, look for more attributes. If the next token isn't a
25078 `,', we're done. */
25079 if (token->type != CPP_COMMA)
25080 break;
25082 /* Consume the comma and keep going. */
25083 cp_lexer_consume_token (parser->lexer);
25085 parser->translate_strings_p = save_translate_strings_p;
25087 /* We built up the list in reverse order. */
25088 return nreverse (attribute_list);
25091 /* Parse a standard C++11 attribute.
25093 The returned representation is a TREE_LIST which TREE_PURPOSE is
25094 the scoped name of the attribute, and the TREE_VALUE is its
25095 arguments list.
25097 Note that the scoped name of the attribute is itself a TREE_LIST
25098 which TREE_PURPOSE is the namespace of the attribute, and
25099 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25100 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25101 and which TREE_PURPOSE is directly the attribute name.
25103 Clients of the attribute code should use get_attribute_namespace
25104 and get_attribute_name to get the actual namespace and name of
25105 attributes, regardless of their being GNU or C++11 attributes.
25107 attribute:
25108 attribute-token attribute-argument-clause [opt]
25110 attribute-token:
25111 identifier
25112 attribute-scoped-token
25114 attribute-scoped-token:
25115 attribute-namespace :: identifier
25117 attribute-namespace:
25118 identifier
25120 attribute-argument-clause:
25121 ( balanced-token-seq )
25123 balanced-token-seq:
25124 balanced-token [opt]
25125 balanced-token-seq balanced-token
25127 balanced-token:
25128 ( balanced-token-seq )
25129 [ balanced-token-seq ]
25130 { balanced-token-seq }. */
25132 static tree
25133 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25135 tree attribute, attr_id = NULL_TREE, arguments;
25136 cp_token *token;
25138 /* First, parse name of the attribute, a.k.a attribute-token. */
25140 token = cp_lexer_peek_token (parser->lexer);
25141 if (token->type == CPP_NAME)
25142 attr_id = token->u.value;
25143 else if (token->type == CPP_KEYWORD)
25144 attr_id = ridpointers[(int) token->keyword];
25145 else if (token->flags & NAMED_OP)
25146 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25148 if (attr_id == NULL_TREE)
25149 return NULL_TREE;
25151 cp_lexer_consume_token (parser->lexer);
25153 token = cp_lexer_peek_token (parser->lexer);
25154 if (token->type == CPP_SCOPE)
25156 /* We are seeing a scoped attribute token. */
25158 cp_lexer_consume_token (parser->lexer);
25159 if (attr_ns)
25160 error_at (token->location, "attribute using prefix used together "
25161 "with scoped attribute token");
25162 attr_ns = attr_id;
25164 token = cp_lexer_consume_token (parser->lexer);
25165 if (token->type == CPP_NAME)
25166 attr_id = token->u.value;
25167 else if (token->type == CPP_KEYWORD)
25168 attr_id = ridpointers[(int) token->keyword];
25169 else if (token->flags & NAMED_OP)
25170 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25171 else
25173 error_at (token->location,
25174 "expected an identifier for the attribute name");
25175 return error_mark_node;
25178 attr_id = canonicalize_attr_name (attr_id);
25179 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25180 NULL_TREE);
25181 token = cp_lexer_peek_token (parser->lexer);
25183 else if (attr_ns)
25184 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25185 NULL_TREE);
25186 else
25188 attr_id = canonicalize_attr_name (attr_id);
25189 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25190 NULL_TREE);
25191 /* C++11 noreturn attribute is equivalent to GNU's. */
25192 if (is_attribute_p ("noreturn", attr_id))
25193 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25194 /* C++14 deprecated attribute is equivalent to GNU's. */
25195 else if (is_attribute_p ("deprecated", attr_id))
25196 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25197 /* C++17 fallthrough attribute is equivalent to GNU's. */
25198 else if (is_attribute_p ("fallthrough", attr_id))
25199 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25200 /* Transactional Memory TS optimize_for_synchronized attribute is
25201 equivalent to GNU transaction_callable. */
25202 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25203 TREE_PURPOSE (attribute)
25204 = get_identifier ("transaction_callable");
25205 /* Transactional Memory attributes are GNU attributes. */
25206 else if (tm_attr_to_mask (attr_id))
25207 TREE_PURPOSE (attribute) = attr_id;
25210 /* Now parse the optional argument clause of the attribute. */
25212 if (token->type != CPP_OPEN_PAREN)
25213 return attribute;
25216 vec<tree, va_gc> *vec;
25217 int attr_flag = normal_attr;
25219 if (attr_ns == get_identifier ("gnu")
25220 && attribute_takes_identifier_p (attr_id))
25221 /* A GNU attribute that takes an identifier in parameter. */
25222 attr_flag = id_attr;
25224 vec = cp_parser_parenthesized_expression_list
25225 (parser, attr_flag, /*cast_p=*/false,
25226 /*allow_expansion_p=*/true,
25227 /*non_constant_p=*/NULL);
25228 if (vec == NULL)
25229 arguments = error_mark_node;
25230 else
25232 arguments = build_tree_list_vec (vec);
25233 release_tree_vector (vec);
25236 if (arguments == error_mark_node)
25237 attribute = error_mark_node;
25238 else
25239 TREE_VALUE (attribute) = arguments;
25242 return attribute;
25245 /* Check that the attribute ATTRIBUTE appears at most once in the
25246 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25247 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25248 isn't implemented yet in GCC. */
25250 static void
25251 cp_parser_check_std_attribute (tree attributes, tree attribute)
25253 if (attributes)
25255 tree name = get_attribute_name (attribute);
25256 if (is_attribute_p ("noreturn", name)
25257 && lookup_attribute ("noreturn", attributes))
25258 error ("attribute %<noreturn%> can appear at most once "
25259 "in an attribute-list");
25260 else if (is_attribute_p ("deprecated", name)
25261 && lookup_attribute ("deprecated", attributes))
25262 error ("attribute %<deprecated%> can appear at most once "
25263 "in an attribute-list");
25267 /* Parse a list of standard C++-11 attributes.
25269 attribute-list:
25270 attribute [opt]
25271 attribute-list , attribute[opt]
25272 attribute ...
25273 attribute-list , attribute ...
25276 static tree
25277 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25279 tree attributes = NULL_TREE, attribute = NULL_TREE;
25280 cp_token *token = NULL;
25282 while (true)
25284 attribute = cp_parser_std_attribute (parser, attr_ns);
25285 if (attribute == error_mark_node)
25286 break;
25287 if (attribute != NULL_TREE)
25289 cp_parser_check_std_attribute (attributes, attribute);
25290 TREE_CHAIN (attribute) = attributes;
25291 attributes = attribute;
25293 token = cp_lexer_peek_token (parser->lexer);
25294 if (token->type == CPP_ELLIPSIS)
25296 cp_lexer_consume_token (parser->lexer);
25297 if (attribute == NULL_TREE)
25298 error_at (token->location,
25299 "expected attribute before %<...%>");
25300 else
25302 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25303 if (pack == error_mark_node)
25304 return error_mark_node;
25305 TREE_VALUE (attribute) = pack;
25307 token = cp_lexer_peek_token (parser->lexer);
25309 if (token->type != CPP_COMMA)
25310 break;
25311 cp_lexer_consume_token (parser->lexer);
25313 attributes = nreverse (attributes);
25314 return attributes;
25317 /* Parse a standard C++-11 attribute specifier.
25319 attribute-specifier:
25320 [ [ attribute-using-prefix [opt] attribute-list ] ]
25321 alignment-specifier
25323 attribute-using-prefix:
25324 using attribute-namespace :
25326 alignment-specifier:
25327 alignas ( type-id ... [opt] )
25328 alignas ( alignment-expression ... [opt] ). */
25330 static tree
25331 cp_parser_std_attribute_spec (cp_parser *parser)
25333 tree attributes = NULL_TREE;
25334 cp_token *token = cp_lexer_peek_token (parser->lexer);
25336 if (token->type == CPP_OPEN_SQUARE
25337 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25339 tree attr_ns = NULL_TREE;
25341 cp_lexer_consume_token (parser->lexer);
25342 cp_lexer_consume_token (parser->lexer);
25344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25346 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25347 if (token->type == CPP_NAME)
25348 attr_ns = token->u.value;
25349 else if (token->type == CPP_KEYWORD)
25350 attr_ns = ridpointers[(int) token->keyword];
25351 else if (token->flags & NAMED_OP)
25352 attr_ns = get_identifier (cpp_type2name (token->type,
25353 token->flags));
25354 if (attr_ns
25355 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25357 if (cxx_dialect < cxx17
25358 && !in_system_header_at (input_location))
25359 pedwarn (input_location, 0,
25360 "attribute using prefix only available "
25361 "with -std=c++17 or -std=gnu++17");
25363 cp_lexer_consume_token (parser->lexer);
25364 cp_lexer_consume_token (parser->lexer);
25365 cp_lexer_consume_token (parser->lexer);
25367 else
25368 attr_ns = NULL_TREE;
25371 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25373 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25374 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25375 cp_parser_skip_to_end_of_statement (parser);
25376 else
25377 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25378 when we are sure that we have actually parsed them. */
25379 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25381 else
25383 tree alignas_expr;
25385 /* Look for an alignment-specifier. */
25387 token = cp_lexer_peek_token (parser->lexer);
25389 if (token->type != CPP_KEYWORD
25390 || token->keyword != RID_ALIGNAS)
25391 return NULL_TREE;
25393 cp_lexer_consume_token (parser->lexer);
25394 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25396 matching_parens parens;
25397 if (!parens.require_open (parser))
25398 return error_mark_node;
25400 cp_parser_parse_tentatively (parser);
25401 alignas_expr = cp_parser_type_id (parser);
25403 if (!cp_parser_parse_definitely (parser))
25405 alignas_expr = cp_parser_assignment_expression (parser);
25406 if (alignas_expr == error_mark_node)
25407 cp_parser_skip_to_end_of_statement (parser);
25408 if (alignas_expr == NULL_TREE
25409 || alignas_expr == error_mark_node)
25410 return alignas_expr;
25413 alignas_expr = cxx_alignas_expr (alignas_expr);
25414 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25416 /* Handle alignas (pack...). */
25417 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25419 cp_lexer_consume_token (parser->lexer);
25420 alignas_expr = make_pack_expansion (alignas_expr);
25423 /* Something went wrong, so don't build the attribute. */
25424 if (alignas_expr == error_mark_node)
25425 return error_mark_node;
25427 if (!parens.require_close (parser))
25428 return error_mark_node;
25430 /* Build the C++-11 representation of an 'aligned'
25431 attribute. */
25432 attributes =
25433 build_tree_list (build_tree_list (get_identifier ("gnu"),
25434 get_identifier ("aligned")),
25435 alignas_expr);
25438 return attributes;
25441 /* Parse a standard C++-11 attribute-specifier-seq.
25443 attribute-specifier-seq:
25444 attribute-specifier-seq [opt] attribute-specifier
25447 static tree
25448 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25450 tree attr_specs = NULL_TREE;
25451 tree attr_last = NULL_TREE;
25453 while (true)
25455 tree attr_spec = cp_parser_std_attribute_spec (parser);
25456 if (attr_spec == NULL_TREE)
25457 break;
25458 if (attr_spec == error_mark_node)
25459 return error_mark_node;
25461 if (attr_last)
25462 TREE_CHAIN (attr_last) = attr_spec;
25463 else
25464 attr_specs = attr_last = attr_spec;
25465 attr_last = tree_last (attr_last);
25468 return attr_specs;
25471 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25472 return index of the first token after balanced-token, or N on failure. */
25474 static size_t
25475 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25477 size_t orig_n = n;
25478 int nparens = 0, nbraces = 0, nsquares = 0;
25480 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25482 case CPP_EOF:
25483 case CPP_PRAGMA_EOL:
25484 /* Ran out of tokens. */
25485 return orig_n;
25486 case CPP_OPEN_PAREN:
25487 ++nparens;
25488 break;
25489 case CPP_OPEN_BRACE:
25490 ++nbraces;
25491 break;
25492 case CPP_OPEN_SQUARE:
25493 ++nsquares;
25494 break;
25495 case CPP_CLOSE_PAREN:
25496 --nparens;
25497 break;
25498 case CPP_CLOSE_BRACE:
25499 --nbraces;
25500 break;
25501 case CPP_CLOSE_SQUARE:
25502 --nsquares;
25503 break;
25504 default:
25505 break;
25507 while (nparens || nbraces || nsquares);
25508 return n;
25511 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25512 return index of the first token after the GNU attribute tokens, or N on
25513 failure. */
25515 static size_t
25516 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25518 while (true)
25520 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25521 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25522 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25523 break;
25525 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25526 if (n2 == n + 2)
25527 break;
25528 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25529 break;
25530 n = n2 + 1;
25532 return n;
25535 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25536 next token), return index of the first token after the standard C++11
25537 attribute tokens, or N on failure. */
25539 static size_t
25540 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25542 while (true)
25544 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25545 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25547 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25548 if (n2 == n + 1)
25549 break;
25550 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25551 break;
25552 n = n2 + 1;
25554 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25555 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25557 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25558 if (n2 == n + 1)
25559 break;
25560 n = n2;
25562 else
25563 break;
25565 return n;
25568 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25569 as the next token), return index of the first token after the attribute
25570 tokens, or N on failure. */
25572 static size_t
25573 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25575 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25576 return cp_parser_skip_gnu_attributes_opt (parser, n);
25577 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25580 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25581 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25582 current value of the PEDANTIC flag, regardless of whether or not
25583 the `__extension__' keyword is present. The caller is responsible
25584 for restoring the value of the PEDANTIC flag. */
25586 static bool
25587 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25589 /* Save the old value of the PEDANTIC flag. */
25590 *saved_pedantic = pedantic;
25592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25594 /* Consume the `__extension__' token. */
25595 cp_lexer_consume_token (parser->lexer);
25596 /* We're not being pedantic while the `__extension__' keyword is
25597 in effect. */
25598 pedantic = 0;
25600 return true;
25603 return false;
25606 /* Parse a label declaration.
25608 label-declaration:
25609 __label__ label-declarator-seq ;
25611 label-declarator-seq:
25612 identifier , label-declarator-seq
25613 identifier */
25615 static void
25616 cp_parser_label_declaration (cp_parser* parser)
25618 /* Look for the `__label__' keyword. */
25619 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25621 while (true)
25623 tree identifier;
25625 /* Look for an identifier. */
25626 identifier = cp_parser_identifier (parser);
25627 /* If we failed, stop. */
25628 if (identifier == error_mark_node)
25629 break;
25630 /* Declare it as a label. */
25631 finish_label_decl (identifier);
25632 /* If the next token is a `;', stop. */
25633 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25634 break;
25635 /* Look for the `,' separating the label declarations. */
25636 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25639 /* Look for the final `;'. */
25640 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25643 // -------------------------------------------------------------------------- //
25644 // Requires Clause
25646 // Parse a requires clause.
25648 // requires-clause:
25649 // 'requires' logical-or-expression
25651 // The required logical-or-expression must be a constant expression. Note
25652 // that we don't check that the expression is constepxr here. We defer until
25653 // we analyze constraints and then, we only check atomic constraints.
25654 static tree
25655 cp_parser_requires_clause (cp_parser *parser)
25657 // Parse the requires clause so that it is not automatically folded.
25658 ++processing_template_decl;
25659 tree expr = cp_parser_binary_expression (parser, false, false,
25660 PREC_NOT_OPERATOR, NULL);
25661 if (check_for_bare_parameter_packs (expr))
25662 expr = error_mark_node;
25663 --processing_template_decl;
25664 return expr;
25667 // Optionally parse a requires clause:
25668 static tree
25669 cp_parser_requires_clause_opt (cp_parser *parser)
25671 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25672 if (tok->keyword != RID_REQUIRES)
25674 if (!flag_concepts && tok->type == CPP_NAME
25675 && tok->u.value == ridpointers[RID_REQUIRES])
25677 error_at (cp_lexer_peek_token (parser->lexer)->location,
25678 "%<requires%> only available with -fconcepts");
25679 /* Parse and discard the requires-clause. */
25680 cp_lexer_consume_token (parser->lexer);
25681 cp_parser_requires_clause (parser);
25683 return NULL_TREE;
25685 cp_lexer_consume_token (parser->lexer);
25686 return cp_parser_requires_clause (parser);
25690 /*---------------------------------------------------------------------------
25691 Requires expressions
25692 ---------------------------------------------------------------------------*/
25694 /* Parse a requires expression
25696 requirement-expression:
25697 'requires' requirement-parameter-list [opt] requirement-body */
25698 static tree
25699 cp_parser_requires_expression (cp_parser *parser)
25701 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25702 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25704 /* A requires-expression shall appear only within a concept
25705 definition or a requires-clause.
25707 TODO: Implement this diagnostic correctly. */
25708 if (!processing_template_decl)
25710 error_at (loc, "a requires expression cannot appear outside a template");
25711 cp_parser_skip_to_end_of_statement (parser);
25712 return error_mark_node;
25715 tree parms, reqs;
25717 /* Local parameters are delared as variables within the scope
25718 of the expression. They are not visible past the end of
25719 the expression. Expressions within the requires-expression
25720 are unevaluated. */
25721 struct scope_sentinel
25723 scope_sentinel ()
25725 ++cp_unevaluated_operand;
25726 begin_scope (sk_block, NULL_TREE);
25729 ~scope_sentinel ()
25731 pop_bindings_and_leave_scope ();
25732 --cp_unevaluated_operand;
25734 } s;
25736 /* Parse the optional parameter list. */
25737 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25739 parms = cp_parser_requirement_parameter_list (parser);
25740 if (parms == error_mark_node)
25741 return error_mark_node;
25743 else
25744 parms = NULL_TREE;
25746 /* Parse the requirement body. */
25747 reqs = cp_parser_requirement_body (parser);
25748 if (reqs == error_mark_node)
25749 return error_mark_node;
25752 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25753 the parm chain. */
25754 grokparms (parms, &parms);
25755 return finish_requires_expr (parms, reqs);
25758 /* Parse a parameterized requirement.
25760 requirement-parameter-list:
25761 '(' parameter-declaration-clause ')' */
25762 static tree
25763 cp_parser_requirement_parameter_list (cp_parser *parser)
25765 matching_parens parens;
25766 if (!parens.require_open (parser))
25767 return error_mark_node;
25769 tree parms = cp_parser_parameter_declaration_clause (parser);
25771 if (!parens.require_close (parser))
25772 return error_mark_node;
25774 return parms;
25777 /* Parse the body of a requirement.
25779 requirement-body:
25780 '{' requirement-list '}' */
25781 static tree
25782 cp_parser_requirement_body (cp_parser *parser)
25784 matching_braces braces;
25785 if (!braces.require_open (parser))
25786 return error_mark_node;
25788 tree reqs = cp_parser_requirement_list (parser);
25790 if (!braces.require_close (parser))
25791 return error_mark_node;
25793 return reqs;
25796 /* Parse a list of requirements.
25798 requirement-list:
25799 requirement
25800 requirement-list ';' requirement[opt] */
25801 static tree
25802 cp_parser_requirement_list (cp_parser *parser)
25804 tree result = NULL_TREE;
25805 while (true)
25807 tree req = cp_parser_requirement (parser);
25808 if (req == error_mark_node)
25809 return error_mark_node;
25811 result = tree_cons (NULL_TREE, req, result);
25813 /* If we see a semi-colon, consume it. */
25814 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25815 cp_lexer_consume_token (parser->lexer);
25817 /* Stop processing at the end of the list. */
25818 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25819 break;
25822 /* Reverse the order of requirements so they are analyzed in
25823 declaration order. */
25824 return nreverse (result);
25827 /* Parse a syntactic requirement or type requirement.
25829 requirement:
25830 simple-requirement
25831 compound-requirement
25832 type-requirement
25833 nested-requirement */
25834 static tree
25835 cp_parser_requirement (cp_parser *parser)
25837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25838 return cp_parser_compound_requirement (parser);
25839 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25840 return cp_parser_type_requirement (parser);
25841 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25842 return cp_parser_nested_requirement (parser);
25843 else
25844 return cp_parser_simple_requirement (parser);
25847 /* Parse a simple requirement.
25849 simple-requirement:
25850 expression ';' */
25851 static tree
25852 cp_parser_simple_requirement (cp_parser *parser)
25854 tree expr = cp_parser_expression (parser, NULL, false, false);
25855 if (!expr || expr == error_mark_node)
25856 return error_mark_node;
25858 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25859 return error_mark_node;
25861 return finish_simple_requirement (expr);
25864 /* Parse a type requirement
25866 type-requirement
25867 nested-name-specifier [opt] required-type-name ';'
25869 required-type-name:
25870 type-name
25871 'template' [opt] simple-template-id */
25872 static tree
25873 cp_parser_type_requirement (cp_parser *parser)
25875 cp_lexer_consume_token (parser->lexer);
25877 // Save the scope before parsing name specifiers.
25878 tree saved_scope = parser->scope;
25879 tree saved_object_scope = parser->object_scope;
25880 tree saved_qualifying_scope = parser->qualifying_scope;
25881 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25882 cp_parser_nested_name_specifier_opt (parser,
25883 /*typename_keyword_p=*/true,
25884 /*check_dependency_p=*/false,
25885 /*type_p=*/true,
25886 /*is_declaration=*/false);
25888 tree type;
25889 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25891 cp_lexer_consume_token (parser->lexer);
25892 type = cp_parser_template_id (parser,
25893 /*template_keyword_p=*/true,
25894 /*check_dependency=*/false,
25895 /*tag_type=*/none_type,
25896 /*is_declaration=*/false);
25897 type = make_typename_type (parser->scope, type, typename_type,
25898 /*complain=*/tf_error);
25900 else
25901 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25903 if (TREE_CODE (type) == TYPE_DECL)
25904 type = TREE_TYPE (type);
25906 parser->scope = saved_scope;
25907 parser->object_scope = saved_object_scope;
25908 parser->qualifying_scope = saved_qualifying_scope;
25910 if (type == error_mark_node)
25911 cp_parser_skip_to_end_of_statement (parser);
25913 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25914 return error_mark_node;
25915 if (type == error_mark_node)
25916 return error_mark_node;
25918 return finish_type_requirement (type);
25921 /* Parse a compound requirement
25923 compound-requirement:
25924 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25925 static tree
25926 cp_parser_compound_requirement (cp_parser *parser)
25928 /* Parse an expression enclosed in '{ }'s. */
25929 matching_braces braces;
25930 if (!braces.require_open (parser))
25931 return error_mark_node;
25933 tree expr = cp_parser_expression (parser, NULL, false, false);
25934 if (!expr || expr == error_mark_node)
25935 return error_mark_node;
25937 if (!braces.require_close (parser))
25938 return error_mark_node;
25940 /* Parse the optional noexcept. */
25941 bool noexcept_p = false;
25942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25944 cp_lexer_consume_token (parser->lexer);
25945 noexcept_p = true;
25948 /* Parse the optional trailing return type. */
25949 tree type = NULL_TREE;
25950 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25952 cp_lexer_consume_token (parser->lexer);
25953 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25954 parser->in_result_type_constraint_p = true;
25955 type = cp_parser_trailing_type_id (parser);
25956 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25957 if (type == error_mark_node)
25958 return error_mark_node;
25961 return finish_compound_requirement (expr, type, noexcept_p);
25964 /* Parse a nested requirement. This is the same as a requires clause.
25966 nested-requirement:
25967 requires-clause */
25968 static tree
25969 cp_parser_nested_requirement (cp_parser *parser)
25971 cp_lexer_consume_token (parser->lexer);
25972 tree req = cp_parser_requires_clause (parser);
25973 if (req == error_mark_node)
25974 return error_mark_node;
25975 return finish_nested_requirement (req);
25978 /* Support Functions */
25980 /* Return the appropriate prefer_type argument for lookup_name_real based on
25981 tag_type and template_mem_access. */
25983 static inline int
25984 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25986 /* DR 141: When looking in the current enclosing context for a template-name
25987 after -> or ., only consider class templates. */
25988 if (template_mem_access)
25989 return 2;
25990 switch (tag_type)
25992 case none_type: return 0; // No preference.
25993 case scope_type: return 1; // Type or namespace.
25994 default: return 2; // Type only.
25998 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25999 NAME should have one of the representations used for an
26000 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26001 is returned. If PARSER->SCOPE is a dependent type, then a
26002 SCOPE_REF is returned.
26004 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26005 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26006 was formed. Abstractly, such entities should not be passed to this
26007 function, because they do not need to be looked up, but it is
26008 simpler to check for this special case here, rather than at the
26009 call-sites.
26011 In cases not explicitly covered above, this function returns a
26012 DECL, OVERLOAD, or baselink representing the result of the lookup.
26013 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26014 is returned.
26016 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26017 (e.g., "struct") that was used. In that case bindings that do not
26018 refer to types are ignored.
26020 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26021 ignored.
26023 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26024 are ignored.
26026 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26027 types.
26029 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26030 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26031 NULL_TREE otherwise. */
26033 static cp_expr
26034 cp_parser_lookup_name (cp_parser *parser, tree name,
26035 enum tag_types tag_type,
26036 bool is_template,
26037 bool is_namespace,
26038 bool check_dependency,
26039 tree *ambiguous_decls,
26040 location_t name_location)
26042 tree decl;
26043 tree object_type = parser->context->object_type;
26045 /* Assume that the lookup will be unambiguous. */
26046 if (ambiguous_decls)
26047 *ambiguous_decls = NULL_TREE;
26049 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26050 no longer valid. Note that if we are parsing tentatively, and
26051 the parse fails, OBJECT_TYPE will be automatically restored. */
26052 parser->context->object_type = NULL_TREE;
26054 if (name == error_mark_node)
26055 return error_mark_node;
26057 /* A template-id has already been resolved; there is no lookup to
26058 do. */
26059 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26060 return name;
26061 if (BASELINK_P (name))
26063 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26064 == TEMPLATE_ID_EXPR);
26065 return name;
26068 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26069 it should already have been checked to make sure that the name
26070 used matches the type being destroyed. */
26071 if (TREE_CODE (name) == BIT_NOT_EXPR)
26073 tree type;
26075 /* Figure out to which type this destructor applies. */
26076 if (parser->scope)
26077 type = parser->scope;
26078 else if (object_type)
26079 type = object_type;
26080 else
26081 type = current_class_type;
26082 /* If that's not a class type, there is no destructor. */
26083 if (!type || !CLASS_TYPE_P (type))
26084 return error_mark_node;
26086 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26087 lazily_declare_fn (sfk_destructor, type);
26089 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26090 return dtor;
26092 return error_mark_node;
26095 /* By this point, the NAME should be an ordinary identifier. If
26096 the id-expression was a qualified name, the qualifying scope is
26097 stored in PARSER->SCOPE at this point. */
26098 gcc_assert (identifier_p (name));
26100 /* Perform the lookup. */
26101 if (parser->scope)
26103 bool dependent_p;
26105 if (parser->scope == error_mark_node)
26106 return error_mark_node;
26108 /* If the SCOPE is dependent, the lookup must be deferred until
26109 the template is instantiated -- unless we are explicitly
26110 looking up names in uninstantiated templates. Even then, we
26111 cannot look up the name if the scope is not a class type; it
26112 might, for example, be a template type parameter. */
26113 dependent_p = (TYPE_P (parser->scope)
26114 && dependent_scope_p (parser->scope));
26115 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26116 && dependent_p)
26117 /* Defer lookup. */
26118 decl = error_mark_node;
26119 else
26121 tree pushed_scope = NULL_TREE;
26123 /* If PARSER->SCOPE is a dependent type, then it must be a
26124 class type, and we must not be checking dependencies;
26125 otherwise, we would have processed this lookup above. So
26126 that PARSER->SCOPE is not considered a dependent base by
26127 lookup_member, we must enter the scope here. */
26128 if (dependent_p)
26129 pushed_scope = push_scope (parser->scope);
26131 /* If the PARSER->SCOPE is a template specialization, it
26132 may be instantiated during name lookup. In that case,
26133 errors may be issued. Even if we rollback the current
26134 tentative parse, those errors are valid. */
26135 decl = lookup_qualified_name (parser->scope, name,
26136 prefer_type_arg (tag_type),
26137 /*complain=*/true);
26139 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26140 lookup result and the nested-name-specifier nominates a class C:
26141 * if the name specified after the nested-name-specifier, when
26142 looked up in C, is the injected-class-name of C (Clause 9), or
26143 * if the name specified after the nested-name-specifier is the
26144 same as the identifier or the simple-template-id's template-
26145 name in the last component of the nested-name-specifier,
26146 the name is instead considered to name the constructor of
26147 class C. [ Note: for example, the constructor is not an
26148 acceptable lookup result in an elaborated-type-specifier so
26149 the constructor would not be used in place of the
26150 injected-class-name. --end note ] Such a constructor name
26151 shall be used only in the declarator-id of a declaration that
26152 names a constructor or in a using-declaration. */
26153 if (tag_type == none_type
26154 && DECL_SELF_REFERENCE_P (decl)
26155 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26156 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26157 prefer_type_arg (tag_type),
26158 /*complain=*/true);
26160 /* If we have a single function from a using decl, pull it out. */
26161 if (TREE_CODE (decl) == OVERLOAD
26162 && !really_overloaded_fn (decl))
26163 decl = OVL_FUNCTION (decl);
26165 if (pushed_scope)
26166 pop_scope (pushed_scope);
26169 /* If the scope is a dependent type and either we deferred lookup or
26170 we did lookup but didn't find the name, rememeber the name. */
26171 if (decl == error_mark_node && TYPE_P (parser->scope)
26172 && dependent_type_p (parser->scope))
26174 if (tag_type)
26176 tree type;
26178 /* The resolution to Core Issue 180 says that `struct
26179 A::B' should be considered a type-name, even if `A'
26180 is dependent. */
26181 type = make_typename_type (parser->scope, name, tag_type,
26182 /*complain=*/tf_error);
26183 if (type != error_mark_node)
26184 decl = TYPE_NAME (type);
26186 else if (is_template
26187 && (cp_parser_next_token_ends_template_argument_p (parser)
26188 || cp_lexer_next_token_is (parser->lexer,
26189 CPP_CLOSE_PAREN)))
26190 decl = make_unbound_class_template (parser->scope,
26191 name, NULL_TREE,
26192 /*complain=*/tf_error);
26193 else
26194 decl = build_qualified_name (/*type=*/NULL_TREE,
26195 parser->scope, name,
26196 is_template);
26198 parser->qualifying_scope = parser->scope;
26199 parser->object_scope = NULL_TREE;
26201 else if (object_type)
26203 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26204 OBJECT_TYPE is not a class. */
26205 if (CLASS_TYPE_P (object_type))
26206 /* If the OBJECT_TYPE is a template specialization, it may
26207 be instantiated during name lookup. In that case, errors
26208 may be issued. Even if we rollback the current tentative
26209 parse, those errors are valid. */
26210 decl = lookup_member (object_type,
26211 name,
26212 /*protect=*/0,
26213 prefer_type_arg (tag_type),
26214 tf_warning_or_error);
26215 else
26216 decl = NULL_TREE;
26218 if (!decl)
26219 /* Look it up in the enclosing context. DR 141: When looking for a
26220 template-name after -> or ., only consider class templates. */
26221 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26222 /*nonclass=*/0,
26223 /*block_p=*/true, is_namespace, 0);
26224 if (object_type == unknown_type_node)
26225 /* The object is type-dependent, so we can't look anything up; we used
26226 this to get the DR 141 behavior. */
26227 object_type = NULL_TREE;
26228 parser->object_scope = object_type;
26229 parser->qualifying_scope = NULL_TREE;
26231 else
26233 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26234 /*nonclass=*/0,
26235 /*block_p=*/true, is_namespace, 0);
26236 parser->qualifying_scope = NULL_TREE;
26237 parser->object_scope = NULL_TREE;
26240 /* If the lookup failed, let our caller know. */
26241 if (!decl || decl == error_mark_node)
26242 return error_mark_node;
26244 /* Pull out the template from an injected-class-name (or multiple). */
26245 if (is_template)
26246 decl = maybe_get_template_decl_from_type_decl (decl);
26248 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26249 if (TREE_CODE (decl) == TREE_LIST)
26251 if (ambiguous_decls)
26252 *ambiguous_decls = decl;
26253 /* The error message we have to print is too complicated for
26254 cp_parser_error, so we incorporate its actions directly. */
26255 if (!cp_parser_simulate_error (parser))
26257 error_at (name_location, "reference to %qD is ambiguous",
26258 name);
26259 print_candidates (decl);
26261 return error_mark_node;
26264 gcc_assert (DECL_P (decl)
26265 || TREE_CODE (decl) == OVERLOAD
26266 || TREE_CODE (decl) == SCOPE_REF
26267 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26268 || BASELINK_P (decl));
26270 /* If we have resolved the name of a member declaration, check to
26271 see if the declaration is accessible. When the name resolves to
26272 set of overloaded functions, accessibility is checked when
26273 overload resolution is done.
26275 During an explicit instantiation, access is not checked at all,
26276 as per [temp.explicit]. */
26277 if (DECL_P (decl))
26278 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26280 maybe_record_typedef_use (decl);
26282 return cp_expr (decl, name_location);
26285 /* Like cp_parser_lookup_name, but for use in the typical case where
26286 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26287 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26289 static tree
26290 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26292 return cp_parser_lookup_name (parser, name,
26293 none_type,
26294 /*is_template=*/false,
26295 /*is_namespace=*/false,
26296 /*check_dependency=*/true,
26297 /*ambiguous_decls=*/NULL,
26298 location);
26301 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26302 the current context, return the TYPE_DECL. If TAG_NAME_P is
26303 true, the DECL indicates the class being defined in a class-head,
26304 or declared in an elaborated-type-specifier.
26306 Otherwise, return DECL. */
26308 static tree
26309 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26311 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26312 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26314 struct A {
26315 template <typename T> struct B;
26318 template <typename T> struct A::B {};
26320 Similarly, in an elaborated-type-specifier:
26322 namespace N { struct X{}; }
26324 struct A {
26325 template <typename T> friend struct N::X;
26328 However, if the DECL refers to a class type, and we are in
26329 the scope of the class, then the name lookup automatically
26330 finds the TYPE_DECL created by build_self_reference rather
26331 than a TEMPLATE_DECL. For example, in:
26333 template <class T> struct S {
26334 S s;
26337 there is no need to handle such case. */
26339 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26340 return DECL_TEMPLATE_RESULT (decl);
26342 return decl;
26345 /* If too many, or too few, template-parameter lists apply to the
26346 declarator, issue an error message. Returns TRUE if all went well,
26347 and FALSE otherwise. */
26349 static bool
26350 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26351 cp_declarator *declarator,
26352 location_t declarator_location)
26354 switch (declarator->kind)
26356 case cdk_id:
26358 unsigned num_templates = 0;
26359 tree scope = declarator->u.id.qualifying_scope;
26361 if (scope)
26362 num_templates = num_template_headers_for_class (scope);
26363 else if (TREE_CODE (declarator->u.id.unqualified_name)
26364 == TEMPLATE_ID_EXPR)
26365 /* If the DECLARATOR has the form `X<y>' then it uses one
26366 additional level of template parameters. */
26367 ++num_templates;
26369 return cp_parser_check_template_parameters
26370 (parser, num_templates, declarator_location, declarator);
26373 case cdk_function:
26374 case cdk_array:
26375 case cdk_pointer:
26376 case cdk_reference:
26377 case cdk_ptrmem:
26378 return (cp_parser_check_declarator_template_parameters
26379 (parser, declarator->declarator, declarator_location));
26381 case cdk_decomp:
26382 case cdk_error:
26383 return true;
26385 default:
26386 gcc_unreachable ();
26388 return false;
26391 /* NUM_TEMPLATES were used in the current declaration. If that is
26392 invalid, return FALSE and issue an error messages. Otherwise,
26393 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26394 declarator and we can print more accurate diagnostics. */
26396 static bool
26397 cp_parser_check_template_parameters (cp_parser* parser,
26398 unsigned num_templates,
26399 location_t location,
26400 cp_declarator *declarator)
26402 /* If there are the same number of template classes and parameter
26403 lists, that's OK. */
26404 if (parser->num_template_parameter_lists == num_templates)
26405 return true;
26406 /* If there are more, but only one more, then we are referring to a
26407 member template. That's OK too. */
26408 if (parser->num_template_parameter_lists == num_templates + 1)
26409 return true;
26410 /* If there are more template classes than parameter lists, we have
26411 something like:
26413 template <class T> void S<T>::R<T>::f (); */
26414 if (parser->num_template_parameter_lists < num_templates)
26416 if (declarator && !current_function_decl)
26417 error_at (location, "specializing member %<%T::%E%> "
26418 "requires %<template<>%> syntax",
26419 declarator->u.id.qualifying_scope,
26420 declarator->u.id.unqualified_name);
26421 else if (declarator)
26422 error_at (location, "invalid declaration of %<%T::%E%>",
26423 declarator->u.id.qualifying_scope,
26424 declarator->u.id.unqualified_name);
26425 else
26426 error_at (location, "too few template-parameter-lists");
26427 return false;
26429 /* Otherwise, there are too many template parameter lists. We have
26430 something like:
26432 template <class T> template <class U> void S::f(); */
26433 error_at (location, "too many template-parameter-lists");
26434 return false;
26437 /* Parse an optional `::' token indicating that the following name is
26438 from the global namespace. If so, PARSER->SCOPE is set to the
26439 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26440 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26441 Returns the new value of PARSER->SCOPE, if the `::' token is
26442 present, and NULL_TREE otherwise. */
26444 static tree
26445 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26447 cp_token *token;
26449 /* Peek at the next token. */
26450 token = cp_lexer_peek_token (parser->lexer);
26451 /* If we're looking at a `::' token then we're starting from the
26452 global namespace, not our current location. */
26453 if (token->type == CPP_SCOPE)
26455 /* Consume the `::' token. */
26456 cp_lexer_consume_token (parser->lexer);
26457 /* Set the SCOPE so that we know where to start the lookup. */
26458 parser->scope = global_namespace;
26459 parser->qualifying_scope = global_namespace;
26460 parser->object_scope = NULL_TREE;
26462 return parser->scope;
26464 else if (!current_scope_valid_p)
26466 parser->scope = NULL_TREE;
26467 parser->qualifying_scope = NULL_TREE;
26468 parser->object_scope = NULL_TREE;
26471 return NULL_TREE;
26474 /* Returns TRUE if the upcoming token sequence is the start of a
26475 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26476 declarator is preceded by the `friend' specifier. */
26478 static bool
26479 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26481 bool constructor_p;
26482 bool outside_class_specifier_p;
26483 tree nested_name_specifier;
26484 cp_token *next_token;
26486 /* The common case is that this is not a constructor declarator, so
26487 try to avoid doing lots of work if at all possible. It's not
26488 valid declare a constructor at function scope. */
26489 if (parser->in_function_body)
26490 return false;
26491 /* And only certain tokens can begin a constructor declarator. */
26492 next_token = cp_lexer_peek_token (parser->lexer);
26493 if (next_token->type != CPP_NAME
26494 && next_token->type != CPP_SCOPE
26495 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26496 && next_token->type != CPP_TEMPLATE_ID)
26497 return false;
26499 /* Parse tentatively; we are going to roll back all of the tokens
26500 consumed here. */
26501 cp_parser_parse_tentatively (parser);
26502 /* Assume that we are looking at a constructor declarator. */
26503 constructor_p = true;
26505 /* Look for the optional `::' operator. */
26506 cp_parser_global_scope_opt (parser,
26507 /*current_scope_valid_p=*/false);
26508 /* Look for the nested-name-specifier. */
26509 nested_name_specifier
26510 = (cp_parser_nested_name_specifier_opt (parser,
26511 /*typename_keyword_p=*/false,
26512 /*check_dependency_p=*/false,
26513 /*type_p=*/false,
26514 /*is_declaration=*/false));
26516 outside_class_specifier_p = (!at_class_scope_p ()
26517 || !TYPE_BEING_DEFINED (current_class_type)
26518 || friend_p);
26520 /* Outside of a class-specifier, there must be a
26521 nested-name-specifier. Except in C++17 mode, where we
26522 might be declaring a guiding declaration. */
26523 if (!nested_name_specifier && outside_class_specifier_p
26524 && cxx_dialect < cxx17)
26525 constructor_p = false;
26526 else if (nested_name_specifier == error_mark_node)
26527 constructor_p = false;
26529 /* If we have a class scope, this is easy; DR 147 says that S::S always
26530 names the constructor, and no other qualified name could. */
26531 if (constructor_p && nested_name_specifier
26532 && CLASS_TYPE_P (nested_name_specifier))
26534 tree id = cp_parser_unqualified_id (parser,
26535 /*template_keyword_p=*/false,
26536 /*check_dependency_p=*/false,
26537 /*declarator_p=*/true,
26538 /*optional_p=*/false);
26539 if (is_overloaded_fn (id))
26540 id = DECL_NAME (get_first_fn (id));
26541 if (!constructor_name_p (id, nested_name_specifier))
26542 constructor_p = false;
26544 /* If we still think that this might be a constructor-declarator,
26545 look for a class-name. */
26546 else if (constructor_p)
26548 /* If we have:
26550 template <typename T> struct S {
26551 S();
26554 we must recognize that the nested `S' names a class. */
26555 if (cxx_dialect >= cxx17)
26556 cp_parser_parse_tentatively (parser);
26558 tree type_decl;
26559 type_decl = cp_parser_class_name (parser,
26560 /*typename_keyword_p=*/false,
26561 /*template_keyword_p=*/false,
26562 none_type,
26563 /*check_dependency_p=*/false,
26564 /*class_head_p=*/false,
26565 /*is_declaration=*/false);
26567 if (cxx_dialect >= cxx17
26568 && !cp_parser_parse_definitely (parser))
26570 type_decl = NULL_TREE;
26571 tree tmpl = cp_parser_template_name (parser,
26572 /*template_keyword*/false,
26573 /*check_dependency_p*/false,
26574 /*is_declaration*/false,
26575 none_type,
26576 /*is_identifier*/NULL);
26577 if (DECL_CLASS_TEMPLATE_P (tmpl)
26578 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26579 /* It's a deduction guide, return true. */;
26580 else
26581 cp_parser_simulate_error (parser);
26584 /* If there was no class-name, then this is not a constructor.
26585 Otherwise, if we are in a class-specifier and we aren't
26586 handling a friend declaration, check that its type matches
26587 current_class_type (c++/38313). Note: error_mark_node
26588 is left alone for error recovery purposes. */
26589 constructor_p = (!cp_parser_error_occurred (parser)
26590 && (outside_class_specifier_p
26591 || type_decl == NULL_TREE
26592 || type_decl == error_mark_node
26593 || same_type_p (current_class_type,
26594 TREE_TYPE (type_decl))));
26596 /* If we're still considering a constructor, we have to see a `(',
26597 to begin the parameter-declaration-clause, followed by either a
26598 `)', an `...', or a decl-specifier. We need to check for a
26599 type-specifier to avoid being fooled into thinking that:
26601 S (f) (int);
26603 is a constructor. (It is actually a function named `f' that
26604 takes one parameter (of type `int') and returns a value of type
26605 `S'. */
26606 if (constructor_p
26607 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26608 constructor_p = false;
26610 if (constructor_p
26611 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26612 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26613 /* A parameter declaration begins with a decl-specifier,
26614 which is either the "attribute" keyword, a storage class
26615 specifier, or (usually) a type-specifier. */
26616 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26618 tree type;
26619 tree pushed_scope = NULL_TREE;
26620 unsigned saved_num_template_parameter_lists;
26622 /* Names appearing in the type-specifier should be looked up
26623 in the scope of the class. */
26624 if (current_class_type)
26625 type = NULL_TREE;
26626 else if (type_decl)
26628 type = TREE_TYPE (type_decl);
26629 if (TREE_CODE (type) == TYPENAME_TYPE)
26631 type = resolve_typename_type (type,
26632 /*only_current_p=*/false);
26633 if (TREE_CODE (type) == TYPENAME_TYPE)
26635 cp_parser_abort_tentative_parse (parser);
26636 return false;
26639 pushed_scope = push_scope (type);
26642 /* Inside the constructor parameter list, surrounding
26643 template-parameter-lists do not apply. */
26644 saved_num_template_parameter_lists
26645 = parser->num_template_parameter_lists;
26646 parser->num_template_parameter_lists = 0;
26648 /* Look for the type-specifier. */
26649 cp_parser_type_specifier (parser,
26650 CP_PARSER_FLAGS_NONE,
26651 /*decl_specs=*/NULL,
26652 /*is_declarator=*/true,
26653 /*declares_class_or_enum=*/NULL,
26654 /*is_cv_qualifier=*/NULL);
26656 parser->num_template_parameter_lists
26657 = saved_num_template_parameter_lists;
26659 /* Leave the scope of the class. */
26660 if (pushed_scope)
26661 pop_scope (pushed_scope);
26663 constructor_p = !cp_parser_error_occurred (parser);
26667 /* We did not really want to consume any tokens. */
26668 cp_parser_abort_tentative_parse (parser);
26670 return constructor_p;
26673 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26674 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26675 they must be performed once we are in the scope of the function.
26677 Returns the function defined. */
26679 static tree
26680 cp_parser_function_definition_from_specifiers_and_declarator
26681 (cp_parser* parser,
26682 cp_decl_specifier_seq *decl_specifiers,
26683 tree attributes,
26684 const cp_declarator *declarator)
26686 tree fn;
26687 bool success_p;
26689 /* Begin the function-definition. */
26690 success_p = start_function (decl_specifiers, declarator, attributes);
26692 /* The things we're about to see are not directly qualified by any
26693 template headers we've seen thus far. */
26694 reset_specialization ();
26696 /* If there were names looked up in the decl-specifier-seq that we
26697 did not check, check them now. We must wait until we are in the
26698 scope of the function to perform the checks, since the function
26699 might be a friend. */
26700 perform_deferred_access_checks (tf_warning_or_error);
26702 if (success_p)
26704 cp_finalize_omp_declare_simd (parser, current_function_decl);
26705 parser->omp_declare_simd = NULL;
26706 cp_finalize_oacc_routine (parser, current_function_decl, true);
26707 parser->oacc_routine = NULL;
26710 if (!success_p)
26712 /* Skip the entire function. */
26713 cp_parser_skip_to_end_of_block_or_statement (parser);
26714 fn = error_mark_node;
26716 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26718 /* Seen already, skip it. An error message has already been output. */
26719 cp_parser_skip_to_end_of_block_or_statement (parser);
26720 fn = current_function_decl;
26721 current_function_decl = NULL_TREE;
26722 /* If this is a function from a class, pop the nested class. */
26723 if (current_class_name)
26724 pop_nested_class ();
26726 else
26728 timevar_id_t tv;
26729 if (DECL_DECLARED_INLINE_P (current_function_decl))
26730 tv = TV_PARSE_INLINE;
26731 else
26732 tv = TV_PARSE_FUNC;
26733 timevar_push (tv);
26734 fn = cp_parser_function_definition_after_declarator (parser,
26735 /*inline_p=*/false);
26736 timevar_pop (tv);
26739 return fn;
26742 /* Parse the part of a function-definition that follows the
26743 declarator. INLINE_P is TRUE iff this function is an inline
26744 function defined within a class-specifier.
26746 Returns the function defined. */
26748 static tree
26749 cp_parser_function_definition_after_declarator (cp_parser* parser,
26750 bool inline_p)
26752 tree fn;
26753 bool saved_in_unbraced_linkage_specification_p;
26754 bool saved_in_function_body;
26755 unsigned saved_num_template_parameter_lists;
26756 cp_token *token;
26757 bool fully_implicit_function_template_p
26758 = parser->fully_implicit_function_template_p;
26759 parser->fully_implicit_function_template_p = false;
26760 tree implicit_template_parms
26761 = parser->implicit_template_parms;
26762 parser->implicit_template_parms = 0;
26763 cp_binding_level* implicit_template_scope
26764 = parser->implicit_template_scope;
26765 parser->implicit_template_scope = 0;
26767 saved_in_function_body = parser->in_function_body;
26768 parser->in_function_body = true;
26769 /* If the next token is `return', then the code may be trying to
26770 make use of the "named return value" extension that G++ used to
26771 support. */
26772 token = cp_lexer_peek_token (parser->lexer);
26773 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26775 /* Consume the `return' keyword. */
26776 cp_lexer_consume_token (parser->lexer);
26777 /* Look for the identifier that indicates what value is to be
26778 returned. */
26779 cp_parser_identifier (parser);
26780 /* Issue an error message. */
26781 error_at (token->location,
26782 "named return values are no longer supported");
26783 /* Skip tokens until we reach the start of the function body. */
26784 while (true)
26786 cp_token *token = cp_lexer_peek_token (parser->lexer);
26787 if (token->type == CPP_OPEN_BRACE
26788 || token->type == CPP_EOF
26789 || token->type == CPP_PRAGMA_EOL)
26790 break;
26791 cp_lexer_consume_token (parser->lexer);
26794 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26795 anything declared inside `f'. */
26796 saved_in_unbraced_linkage_specification_p
26797 = parser->in_unbraced_linkage_specification_p;
26798 parser->in_unbraced_linkage_specification_p = false;
26799 /* Inside the function, surrounding template-parameter-lists do not
26800 apply. */
26801 saved_num_template_parameter_lists
26802 = parser->num_template_parameter_lists;
26803 parser->num_template_parameter_lists = 0;
26805 /* If the next token is `try', `__transaction_atomic', or
26806 `__transaction_relaxed`, then we are looking at either function-try-block
26807 or function-transaction-block. Note that all of these include the
26808 function-body. */
26809 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26810 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26811 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26812 RID_TRANSACTION_RELAXED))
26813 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26814 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26815 cp_parser_function_try_block (parser);
26816 else
26817 cp_parser_ctor_initializer_opt_and_function_body
26818 (parser, /*in_function_try_block=*/false);
26820 /* Finish the function. */
26821 fn = finish_function (inline_p);
26822 /* Generate code for it, if necessary. */
26823 expand_or_defer_fn (fn);
26824 /* Restore the saved values. */
26825 parser->in_unbraced_linkage_specification_p
26826 = saved_in_unbraced_linkage_specification_p;
26827 parser->num_template_parameter_lists
26828 = saved_num_template_parameter_lists;
26829 parser->in_function_body = saved_in_function_body;
26831 parser->fully_implicit_function_template_p
26832 = fully_implicit_function_template_p;
26833 parser->implicit_template_parms
26834 = implicit_template_parms;
26835 parser->implicit_template_scope
26836 = implicit_template_scope;
26838 if (parser->fully_implicit_function_template_p)
26839 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26841 return fn;
26844 /* Parse a template-declaration body (following argument list). */
26846 static void
26847 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26848 tree parameter_list,
26849 bool member_p)
26851 tree decl = NULL_TREE;
26852 bool friend_p = false;
26854 /* We just processed one more parameter list. */
26855 ++parser->num_template_parameter_lists;
26857 /* Get the deferred access checks from the parameter list. These
26858 will be checked once we know what is being declared, as for a
26859 member template the checks must be performed in the scope of the
26860 class containing the member. */
26861 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26863 /* Tentatively parse for a new template parameter list, which can either be
26864 the template keyword or a template introduction. */
26865 if (cp_parser_template_declaration_after_export (parser, member_p))
26866 /* OK */;
26867 else if (cxx_dialect >= cxx11
26868 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26869 decl = cp_parser_alias_declaration (parser);
26870 else
26872 /* There are no access checks when parsing a template, as we do not
26873 know if a specialization will be a friend. */
26874 push_deferring_access_checks (dk_no_check);
26875 cp_token *token = cp_lexer_peek_token (parser->lexer);
26876 decl = cp_parser_single_declaration (parser,
26877 checks,
26878 member_p,
26879 /*explicit_specialization_p=*/false,
26880 &friend_p);
26881 pop_deferring_access_checks ();
26883 /* If this is a member template declaration, let the front
26884 end know. */
26885 if (member_p && !friend_p && decl)
26887 if (TREE_CODE (decl) == TYPE_DECL)
26888 cp_parser_check_access_in_redeclaration (decl, token->location);
26890 decl = finish_member_template_decl (decl);
26892 else if (friend_p && decl
26893 && DECL_DECLARES_TYPE_P (decl))
26894 make_friend_class (current_class_type, TREE_TYPE (decl),
26895 /*complain=*/true);
26897 /* We are done with the current parameter list. */
26898 --parser->num_template_parameter_lists;
26900 pop_deferring_access_checks ();
26902 /* Finish up. */
26903 finish_template_decl (parameter_list);
26905 /* Check the template arguments for a literal operator template. */
26906 if (decl
26907 && DECL_DECLARES_FUNCTION_P (decl)
26908 && UDLIT_OPER_P (DECL_NAME (decl)))
26910 bool ok = true;
26911 if (parameter_list == NULL_TREE)
26912 ok = false;
26913 else
26915 int num_parms = TREE_VEC_LENGTH (parameter_list);
26916 if (num_parms == 1)
26918 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26919 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26920 if (TREE_TYPE (parm) != char_type_node
26921 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26922 ok = false;
26924 else if (num_parms == 2 && cxx_dialect >= cxx14)
26926 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26927 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26928 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26929 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26930 if (parm == error_mark_node
26931 || TREE_TYPE (parm) != TREE_TYPE (type)
26932 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26933 ok = false;
26935 else
26936 ok = false;
26938 if (!ok)
26940 if (cxx_dialect >= cxx14)
26941 error ("literal operator template %qD has invalid parameter list."
26942 " Expected non-type template argument pack <char...>"
26943 " or <typename CharT, CharT...>",
26944 decl);
26945 else
26946 error ("literal operator template %qD has invalid parameter list."
26947 " Expected non-type template argument pack <char...>",
26948 decl);
26952 /* Register member declarations. */
26953 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26954 finish_member_declaration (decl);
26955 /* If DECL is a function template, we must return to parse it later.
26956 (Even though there is no definition, there might be default
26957 arguments that need handling.) */
26958 if (member_p && decl
26959 && DECL_DECLARES_FUNCTION_P (decl))
26960 vec_safe_push (unparsed_funs_with_definitions, decl);
26963 /* Parse a template introduction header for a template-declaration. Returns
26964 false if tentative parse fails. */
26966 static bool
26967 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26969 cp_parser_parse_tentatively (parser);
26971 tree saved_scope = parser->scope;
26972 tree saved_object_scope = parser->object_scope;
26973 tree saved_qualifying_scope = parser->qualifying_scope;
26975 /* Look for the optional `::' operator. */
26976 cp_parser_global_scope_opt (parser,
26977 /*current_scope_valid_p=*/false);
26978 /* Look for the nested-name-specifier. */
26979 cp_parser_nested_name_specifier_opt (parser,
26980 /*typename_keyword_p=*/false,
26981 /*check_dependency_p=*/true,
26982 /*type_p=*/false,
26983 /*is_declaration=*/false);
26985 cp_token *token = cp_lexer_peek_token (parser->lexer);
26986 tree concept_name = cp_parser_identifier (parser);
26988 /* Look up the concept for which we will be matching
26989 template parameters. */
26990 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26991 token->location);
26992 parser->scope = saved_scope;
26993 parser->object_scope = saved_object_scope;
26994 parser->qualifying_scope = saved_qualifying_scope;
26996 if (concept_name == error_mark_node)
26997 cp_parser_simulate_error (parser);
26999 /* Look for opening brace for introduction. */
27000 matching_braces braces;
27001 braces.require_open (parser);
27003 if (!cp_parser_parse_definitely (parser))
27004 return false;
27006 push_deferring_access_checks (dk_deferred);
27008 /* Build vector of placeholder parameters and grab
27009 matching identifiers. */
27010 tree introduction_list = cp_parser_introduction_list (parser);
27012 /* The introduction-list shall not be empty. */
27013 int nargs = TREE_VEC_LENGTH (introduction_list);
27014 if (nargs == 0)
27016 error ("empty introduction-list");
27017 return true;
27020 /* Look for closing brace for introduction. */
27021 if (!braces.require_close (parser))
27022 return true;
27024 if (tmpl_decl == error_mark_node)
27026 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27027 token->location);
27028 return true;
27031 /* Build and associate the constraint. */
27032 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27033 if (parms && parms != error_mark_node)
27035 cp_parser_template_declaration_after_parameters (parser, parms,
27036 member_p);
27037 return true;
27040 error_at (token->location, "no matching concept for template-introduction");
27041 return true;
27044 /* Parse a normal template-declaration following the template keyword. */
27046 static void
27047 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27049 tree parameter_list;
27050 bool need_lang_pop;
27051 location_t location = input_location;
27053 /* Look for the `<' token. */
27054 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27055 return;
27056 if (at_class_scope_p () && current_function_decl)
27058 /* 14.5.2.2 [temp.mem]
27060 A local class shall not have member templates. */
27061 error_at (location,
27062 "invalid declaration of member template in local class");
27063 cp_parser_skip_to_end_of_block_or_statement (parser);
27064 return;
27066 /* [temp]
27068 A template ... shall not have C linkage. */
27069 if (current_lang_name == lang_name_c)
27071 error_at (location, "template with C linkage");
27072 maybe_show_extern_c_location ();
27073 /* Give it C++ linkage to avoid confusing other parts of the
27074 front end. */
27075 push_lang_context (lang_name_cplusplus);
27076 need_lang_pop = true;
27078 else
27079 need_lang_pop = false;
27081 /* We cannot perform access checks on the template parameter
27082 declarations until we know what is being declared, just as we
27083 cannot check the decl-specifier list. */
27084 push_deferring_access_checks (dk_deferred);
27086 /* If the next token is `>', then we have an invalid
27087 specialization. Rather than complain about an invalid template
27088 parameter, issue an error message here. */
27089 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27091 cp_parser_error (parser, "invalid explicit specialization");
27092 begin_specialization ();
27093 parameter_list = NULL_TREE;
27095 else
27097 /* Parse the template parameters. */
27098 parameter_list = cp_parser_template_parameter_list (parser);
27101 /* Look for the `>'. */
27102 cp_parser_skip_to_end_of_template_parameter_list (parser);
27104 /* Manage template requirements */
27105 if (flag_concepts)
27107 tree reqs = get_shorthand_constraints (current_template_parms);
27108 if (tree r = cp_parser_requires_clause_opt (parser))
27109 reqs = conjoin_constraints (reqs, normalize_expression (r));
27110 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27113 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27114 member_p);
27116 /* For the erroneous case of a template with C linkage, we pushed an
27117 implicit C++ linkage scope; exit that scope now. */
27118 if (need_lang_pop)
27119 pop_lang_context ();
27122 /* Parse a template-declaration, assuming that the `export' (and
27123 `extern') keywords, if present, has already been scanned. MEMBER_P
27124 is as for cp_parser_template_declaration. */
27126 static bool
27127 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27129 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27131 cp_lexer_consume_token (parser->lexer);
27132 cp_parser_explicit_template_declaration (parser, member_p);
27133 return true;
27135 else if (flag_concepts)
27136 return cp_parser_template_introduction (parser, member_p);
27138 return false;
27141 /* Perform the deferred access checks from a template-parameter-list.
27142 CHECKS is a TREE_LIST of access checks, as returned by
27143 get_deferred_access_checks. */
27145 static void
27146 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27148 ++processing_template_parmlist;
27149 perform_access_checks (checks, tf_warning_or_error);
27150 --processing_template_parmlist;
27153 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27154 `function-definition' sequence that follows a template header.
27155 If MEMBER_P is true, this declaration appears in a class scope.
27157 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27158 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27160 static tree
27161 cp_parser_single_declaration (cp_parser* parser,
27162 vec<deferred_access_check, va_gc> *checks,
27163 bool member_p,
27164 bool explicit_specialization_p,
27165 bool* friend_p)
27167 int declares_class_or_enum;
27168 tree decl = NULL_TREE;
27169 cp_decl_specifier_seq decl_specifiers;
27170 bool function_definition_p = false;
27171 cp_token *decl_spec_token_start;
27173 /* This function is only used when processing a template
27174 declaration. */
27175 gcc_assert (innermost_scope_kind () == sk_template_parms
27176 || innermost_scope_kind () == sk_template_spec);
27178 /* Defer access checks until we know what is being declared. */
27179 push_deferring_access_checks (dk_deferred);
27181 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27182 alternative. */
27183 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27184 cp_parser_decl_specifier_seq (parser,
27185 CP_PARSER_FLAGS_OPTIONAL,
27186 &decl_specifiers,
27187 &declares_class_or_enum);
27188 if (friend_p)
27189 *friend_p = cp_parser_friend_p (&decl_specifiers);
27191 /* There are no template typedefs. */
27192 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27194 error_at (decl_spec_token_start->location,
27195 "template declaration of %<typedef%>");
27196 decl = error_mark_node;
27199 /* Gather up the access checks that occurred the
27200 decl-specifier-seq. */
27201 stop_deferring_access_checks ();
27203 /* Check for the declaration of a template class. */
27204 if (declares_class_or_enum)
27206 if (cp_parser_declares_only_class_p (parser)
27207 || (declares_class_or_enum & 2))
27209 // If this is a declaration, but not a definition, associate
27210 // any constraints with the type declaration. Constraints
27211 // are associated with definitions in cp_parser_class_specifier.
27212 if (declares_class_or_enum == 1)
27213 associate_classtype_constraints (decl_specifiers.type);
27215 decl = shadow_tag (&decl_specifiers);
27217 /* In this case:
27219 struct C {
27220 friend template <typename T> struct A<T>::B;
27223 A<T>::B will be represented by a TYPENAME_TYPE, and
27224 therefore not recognized by shadow_tag. */
27225 if (friend_p && *friend_p
27226 && !decl
27227 && decl_specifiers.type
27228 && TYPE_P (decl_specifiers.type))
27229 decl = decl_specifiers.type;
27231 if (decl && decl != error_mark_node)
27232 decl = TYPE_NAME (decl);
27233 else
27234 decl = error_mark_node;
27236 /* Perform access checks for template parameters. */
27237 cp_parser_perform_template_parameter_access_checks (checks);
27239 /* Give a helpful diagnostic for
27240 template <class T> struct A { } a;
27241 if we aren't already recovering from an error. */
27242 if (!cp_parser_declares_only_class_p (parser)
27243 && !seen_error ())
27245 error_at (cp_lexer_peek_token (parser->lexer)->location,
27246 "a class template declaration must not declare "
27247 "anything else");
27248 cp_parser_skip_to_end_of_block_or_statement (parser);
27249 goto out;
27254 /* Complain about missing 'typename' or other invalid type names. */
27255 if (!decl_specifiers.any_type_specifiers_p
27256 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27258 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27259 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27260 the rest of this declaration. */
27261 decl = error_mark_node;
27262 goto out;
27265 /* If it's not a template class, try for a template function. If
27266 the next token is a `;', then this declaration does not declare
27267 anything. But, if there were errors in the decl-specifiers, then
27268 the error might well have come from an attempted class-specifier.
27269 In that case, there's no need to warn about a missing declarator. */
27270 if (!decl
27271 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27272 || decl_specifiers.type != error_mark_node))
27274 decl = cp_parser_init_declarator (parser,
27275 &decl_specifiers,
27276 checks,
27277 /*function_definition_allowed_p=*/true,
27278 member_p,
27279 declares_class_or_enum,
27280 &function_definition_p,
27281 NULL, NULL, NULL);
27283 /* 7.1.1-1 [dcl.stc]
27285 A storage-class-specifier shall not be specified in an explicit
27286 specialization... */
27287 if (decl
27288 && explicit_specialization_p
27289 && decl_specifiers.storage_class != sc_none)
27291 error_at (decl_spec_token_start->location,
27292 "explicit template specialization cannot have a storage class");
27293 decl = error_mark_node;
27296 if (decl && VAR_P (decl))
27297 check_template_variable (decl);
27300 /* Look for a trailing `;' after the declaration. */
27301 if (!function_definition_p
27302 && (decl == error_mark_node
27303 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27304 cp_parser_skip_to_end_of_block_or_statement (parser);
27306 out:
27307 pop_deferring_access_checks ();
27309 /* Clear any current qualification; whatever comes next is the start
27310 of something new. */
27311 parser->scope = NULL_TREE;
27312 parser->qualifying_scope = NULL_TREE;
27313 parser->object_scope = NULL_TREE;
27315 return decl;
27318 /* Parse a cast-expression that is not the operand of a unary "&". */
27320 static cp_expr
27321 cp_parser_simple_cast_expression (cp_parser *parser)
27323 return cp_parser_cast_expression (parser, /*address_p=*/false,
27324 /*cast_p=*/false, /*decltype*/false, NULL);
27327 /* Parse a functional cast to TYPE. Returns an expression
27328 representing the cast. */
27330 static cp_expr
27331 cp_parser_functional_cast (cp_parser* parser, tree type)
27333 vec<tree, va_gc> *vec;
27334 tree expression_list;
27335 cp_expr cast;
27336 bool nonconst_p;
27338 location_t start_loc = input_location;
27340 if (!type)
27341 type = error_mark_node;
27343 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27345 cp_lexer_set_source_position (parser->lexer);
27346 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27347 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27348 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27349 if (TREE_CODE (type) == TYPE_DECL)
27350 type = TREE_TYPE (type);
27352 cast = finish_compound_literal (type, expression_list,
27353 tf_warning_or_error, fcl_functional);
27354 /* Create a location of the form:
27355 type_name{i, f}
27356 ^~~~~~~~~~~~~~~
27357 with caret == start at the start of the type name,
27358 finishing at the closing brace. */
27359 location_t finish_loc
27360 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27361 location_t combined_loc = make_location (start_loc, start_loc,
27362 finish_loc);
27363 cast.set_location (combined_loc);
27364 return cast;
27368 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27369 /*cast_p=*/true,
27370 /*allow_expansion_p=*/true,
27371 /*non_constant_p=*/NULL);
27372 if (vec == NULL)
27373 expression_list = error_mark_node;
27374 else
27376 expression_list = build_tree_list_vec (vec);
27377 release_tree_vector (vec);
27380 cast = build_functional_cast (type, expression_list,
27381 tf_warning_or_error);
27382 /* [expr.const]/1: In an integral constant expression "only type
27383 conversions to integral or enumeration type can be used". */
27384 if (TREE_CODE (type) == TYPE_DECL)
27385 type = TREE_TYPE (type);
27386 if (cast != error_mark_node
27387 && !cast_valid_in_integral_constant_expression_p (type)
27388 && cp_parser_non_integral_constant_expression (parser,
27389 NIC_CONSTRUCTOR))
27390 return error_mark_node;
27392 /* Create a location of the form:
27393 float(i)
27394 ^~~~~~~~
27395 with caret == start at the start of the type name,
27396 finishing at the closing paren. */
27397 location_t finish_loc
27398 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27399 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27400 cast.set_location (combined_loc);
27401 return cast;
27404 /* Save the tokens that make up the body of a member function defined
27405 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27406 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27407 specifiers applied to the declaration. Returns the FUNCTION_DECL
27408 for the member function. */
27410 static tree
27411 cp_parser_save_member_function_body (cp_parser* parser,
27412 cp_decl_specifier_seq *decl_specifiers,
27413 cp_declarator *declarator,
27414 tree attributes)
27416 cp_token *first;
27417 cp_token *last;
27418 tree fn;
27419 bool function_try_block = false;
27421 /* Create the FUNCTION_DECL. */
27422 fn = grokmethod (decl_specifiers, declarator, attributes);
27423 cp_finalize_omp_declare_simd (parser, fn);
27424 cp_finalize_oacc_routine (parser, fn, true);
27425 /* If something went badly wrong, bail out now. */
27426 if (fn == error_mark_node)
27428 /* If there's a function-body, skip it. */
27429 if (cp_parser_token_starts_function_definition_p
27430 (cp_lexer_peek_token (parser->lexer)))
27431 cp_parser_skip_to_end_of_block_or_statement (parser);
27432 return error_mark_node;
27435 /* Remember it, if there default args to post process. */
27436 cp_parser_save_default_args (parser, fn);
27438 /* Save away the tokens that make up the body of the
27439 function. */
27440 first = parser->lexer->next_token;
27442 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27443 cp_lexer_consume_token (parser->lexer);
27444 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27445 RID_TRANSACTION_ATOMIC))
27447 cp_lexer_consume_token (parser->lexer);
27448 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27449 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27450 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27451 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27452 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27453 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27454 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27456 cp_lexer_consume_token (parser->lexer);
27457 cp_lexer_consume_token (parser->lexer);
27458 cp_lexer_consume_token (parser->lexer);
27459 cp_lexer_consume_token (parser->lexer);
27460 cp_lexer_consume_token (parser->lexer);
27462 else
27463 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27464 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27466 cp_lexer_consume_token (parser->lexer);
27467 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27468 break;
27472 /* Handle function try blocks. */
27473 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27475 cp_lexer_consume_token (parser->lexer);
27476 function_try_block = true;
27478 /* We can have braced-init-list mem-initializers before the fn body. */
27479 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27481 cp_lexer_consume_token (parser->lexer);
27482 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27484 /* cache_group will stop after an un-nested { } pair, too. */
27485 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27486 break;
27488 /* variadic mem-inits have ... after the ')'. */
27489 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27490 cp_lexer_consume_token (parser->lexer);
27493 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27494 /* Handle function try blocks. */
27495 if (function_try_block)
27496 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27497 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27498 last = parser->lexer->next_token;
27500 /* Save away the inline definition; we will process it when the
27501 class is complete. */
27502 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27503 DECL_PENDING_INLINE_P (fn) = 1;
27505 /* We need to know that this was defined in the class, so that
27506 friend templates are handled correctly. */
27507 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27509 /* Add FN to the queue of functions to be parsed later. */
27510 vec_safe_push (unparsed_funs_with_definitions, fn);
27512 return fn;
27515 /* Save the tokens that make up the in-class initializer for a non-static
27516 data member. Returns a DEFAULT_ARG. */
27518 static tree
27519 cp_parser_save_nsdmi (cp_parser* parser)
27521 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27524 /* Parse a template-argument-list, as well as the trailing ">" (but
27525 not the opening "<"). See cp_parser_template_argument_list for the
27526 return value. */
27528 static tree
27529 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27531 tree arguments;
27532 tree saved_scope;
27533 tree saved_qualifying_scope;
27534 tree saved_object_scope;
27535 bool saved_greater_than_is_operator_p;
27536 int saved_unevaluated_operand;
27537 int saved_inhibit_evaluation_warnings;
27539 /* [temp.names]
27541 When parsing a template-id, the first non-nested `>' is taken as
27542 the end of the template-argument-list rather than a greater-than
27543 operator. */
27544 saved_greater_than_is_operator_p
27545 = parser->greater_than_is_operator_p;
27546 parser->greater_than_is_operator_p = false;
27547 /* Parsing the argument list may modify SCOPE, so we save it
27548 here. */
27549 saved_scope = parser->scope;
27550 saved_qualifying_scope = parser->qualifying_scope;
27551 saved_object_scope = parser->object_scope;
27552 /* We need to evaluate the template arguments, even though this
27553 template-id may be nested within a "sizeof". */
27554 saved_unevaluated_operand = cp_unevaluated_operand;
27555 cp_unevaluated_operand = 0;
27556 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27557 c_inhibit_evaluation_warnings = 0;
27558 /* Parse the template-argument-list itself. */
27559 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27560 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27561 arguments = NULL_TREE;
27562 else
27563 arguments = cp_parser_template_argument_list (parser);
27564 /* Look for the `>' that ends the template-argument-list. If we find
27565 a '>>' instead, it's probably just a typo. */
27566 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27568 if (cxx_dialect != cxx98)
27570 /* In C++0x, a `>>' in a template argument list or cast
27571 expression is considered to be two separate `>'
27572 tokens. So, change the current token to a `>', but don't
27573 consume it: it will be consumed later when the outer
27574 template argument list (or cast expression) is parsed.
27575 Note that this replacement of `>' for `>>' is necessary
27576 even if we are parsing tentatively: in the tentative
27577 case, after calling
27578 cp_parser_enclosed_template_argument_list we will always
27579 throw away all of the template arguments and the first
27580 closing `>', either because the template argument list
27581 was erroneous or because we are replacing those tokens
27582 with a CPP_TEMPLATE_ID token. The second `>' (which will
27583 not have been thrown away) is needed either to close an
27584 outer template argument list or to complete a new-style
27585 cast. */
27586 cp_token *token = cp_lexer_peek_token (parser->lexer);
27587 token->type = CPP_GREATER;
27589 else if (!saved_greater_than_is_operator_p)
27591 /* If we're in a nested template argument list, the '>>' has
27592 to be a typo for '> >'. We emit the error message, but we
27593 continue parsing and we push a '>' as next token, so that
27594 the argument list will be parsed correctly. Note that the
27595 global source location is still on the token before the
27596 '>>', so we need to say explicitly where we want it. */
27597 cp_token *token = cp_lexer_peek_token (parser->lexer);
27598 gcc_rich_location richloc (token->location);
27599 richloc.add_fixit_replace ("> >");
27600 error_at (&richloc, "%<>>%> should be %<> >%> "
27601 "within a nested template argument list");
27603 token->type = CPP_GREATER;
27605 else
27607 /* If this is not a nested template argument list, the '>>'
27608 is a typo for '>'. Emit an error message and continue.
27609 Same deal about the token location, but here we can get it
27610 right by consuming the '>>' before issuing the diagnostic. */
27611 cp_token *token = cp_lexer_consume_token (parser->lexer);
27612 error_at (token->location,
27613 "spurious %<>>%>, use %<>%> to terminate "
27614 "a template argument list");
27617 else
27618 cp_parser_skip_to_end_of_template_parameter_list (parser);
27619 /* The `>' token might be a greater-than operator again now. */
27620 parser->greater_than_is_operator_p
27621 = saved_greater_than_is_operator_p;
27622 /* Restore the SAVED_SCOPE. */
27623 parser->scope = saved_scope;
27624 parser->qualifying_scope = saved_qualifying_scope;
27625 parser->object_scope = saved_object_scope;
27626 cp_unevaluated_operand = saved_unevaluated_operand;
27627 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27629 return arguments;
27632 /* MEMBER_FUNCTION is a member function, or a friend. If default
27633 arguments, or the body of the function have not yet been parsed,
27634 parse them now. */
27636 static void
27637 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27639 timevar_push (TV_PARSE_INMETH);
27640 /* If this member is a template, get the underlying
27641 FUNCTION_DECL. */
27642 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27643 member_function = DECL_TEMPLATE_RESULT (member_function);
27645 /* There should not be any class definitions in progress at this
27646 point; the bodies of members are only parsed outside of all class
27647 definitions. */
27648 gcc_assert (parser->num_classes_being_defined == 0);
27649 /* While we're parsing the member functions we might encounter more
27650 classes. We want to handle them right away, but we don't want
27651 them getting mixed up with functions that are currently in the
27652 queue. */
27653 push_unparsed_function_queues (parser);
27655 /* Make sure that any template parameters are in scope. */
27656 maybe_begin_member_template_processing (member_function);
27658 /* If the body of the function has not yet been parsed, parse it
27659 now. */
27660 if (DECL_PENDING_INLINE_P (member_function))
27662 tree function_scope;
27663 cp_token_cache *tokens;
27665 /* The function is no longer pending; we are processing it. */
27666 tokens = DECL_PENDING_INLINE_INFO (member_function);
27667 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27668 DECL_PENDING_INLINE_P (member_function) = 0;
27670 /* If this is a local class, enter the scope of the containing
27671 function. */
27672 function_scope = current_function_decl;
27673 if (function_scope)
27674 push_function_context ();
27676 /* Push the body of the function onto the lexer stack. */
27677 cp_parser_push_lexer_for_tokens (parser, tokens);
27679 /* Let the front end know that we going to be defining this
27680 function. */
27681 start_preparsed_function (member_function, NULL_TREE,
27682 SF_PRE_PARSED | SF_INCLASS_INLINE);
27684 /* Don't do access checking if it is a templated function. */
27685 if (processing_template_decl)
27686 push_deferring_access_checks (dk_no_check);
27688 /* #pragma omp declare reduction needs special parsing. */
27689 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27691 parser->lexer->in_pragma = true;
27692 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27693 finish_function (/*inline_p=*/true);
27694 cp_check_omp_declare_reduction (member_function);
27696 else
27697 /* Now, parse the body of the function. */
27698 cp_parser_function_definition_after_declarator (parser,
27699 /*inline_p=*/true);
27701 if (processing_template_decl)
27702 pop_deferring_access_checks ();
27704 /* Leave the scope of the containing function. */
27705 if (function_scope)
27706 pop_function_context ();
27707 cp_parser_pop_lexer (parser);
27710 /* Remove any template parameters from the symbol table. */
27711 maybe_end_member_template_processing ();
27713 /* Restore the queue. */
27714 pop_unparsed_function_queues (parser);
27715 timevar_pop (TV_PARSE_INMETH);
27718 /* If DECL contains any default args, remember it on the unparsed
27719 functions queue. */
27721 static void
27722 cp_parser_save_default_args (cp_parser* parser, tree decl)
27724 tree probe;
27726 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27727 probe;
27728 probe = TREE_CHAIN (probe))
27729 if (TREE_PURPOSE (probe))
27731 cp_default_arg_entry entry = {current_class_type, decl};
27732 vec_safe_push (unparsed_funs_with_default_args, entry);
27733 break;
27737 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27738 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27739 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27740 from the parameter-type-list. */
27742 static tree
27743 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27744 tree default_arg, tree parmtype)
27746 cp_token_cache *tokens;
27747 tree parsed_arg;
27748 bool dummy;
27750 if (default_arg == error_mark_node)
27751 return error_mark_node;
27753 /* Push the saved tokens for the default argument onto the parser's
27754 lexer stack. */
27755 tokens = DEFARG_TOKENS (default_arg);
27756 cp_parser_push_lexer_for_tokens (parser, tokens);
27758 start_lambda_scope (decl);
27760 /* Parse the default argument. */
27761 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27762 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27763 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27765 finish_lambda_scope ();
27767 if (parsed_arg == error_mark_node)
27768 cp_parser_skip_to_end_of_statement (parser);
27770 if (!processing_template_decl)
27772 /* In a non-template class, check conversions now. In a template,
27773 we'll wait and instantiate these as needed. */
27774 if (TREE_CODE (decl) == PARM_DECL)
27775 parsed_arg = check_default_argument (parmtype, parsed_arg,
27776 tf_warning_or_error);
27777 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27778 parsed_arg = error_mark_node;
27779 else
27780 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27783 /* If the token stream has not been completely used up, then
27784 there was extra junk after the end of the default
27785 argument. */
27786 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27788 if (TREE_CODE (decl) == PARM_DECL)
27789 cp_parser_error (parser, "expected %<,%>");
27790 else
27791 cp_parser_error (parser, "expected %<;%>");
27794 /* Revert to the main lexer. */
27795 cp_parser_pop_lexer (parser);
27797 return parsed_arg;
27800 /* FIELD is a non-static data member with an initializer which we saved for
27801 later; parse it now. */
27803 static void
27804 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27806 tree def;
27808 maybe_begin_member_template_processing (field);
27810 push_unparsed_function_queues (parser);
27811 def = cp_parser_late_parse_one_default_arg (parser, field,
27812 DECL_INITIAL (field),
27813 NULL_TREE);
27814 pop_unparsed_function_queues (parser);
27816 maybe_end_member_template_processing ();
27818 DECL_INITIAL (field) = def;
27821 /* FN is a FUNCTION_DECL which may contains a parameter with an
27822 unparsed DEFAULT_ARG. Parse the default args now. This function
27823 assumes that the current scope is the scope in which the default
27824 argument should be processed. */
27826 static void
27827 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27829 bool saved_local_variables_forbidden_p;
27830 tree parm, parmdecl;
27832 /* While we're parsing the default args, we might (due to the
27833 statement expression extension) encounter more classes. We want
27834 to handle them right away, but we don't want them getting mixed
27835 up with default args that are currently in the queue. */
27836 push_unparsed_function_queues (parser);
27838 /* Local variable names (and the `this' keyword) may not appear
27839 in a default argument. */
27840 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27841 parser->local_variables_forbidden_p = true;
27843 push_defarg_context (fn);
27845 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27846 parmdecl = DECL_ARGUMENTS (fn);
27847 parm && parm != void_list_node;
27848 parm = TREE_CHAIN (parm),
27849 parmdecl = DECL_CHAIN (parmdecl))
27851 tree default_arg = TREE_PURPOSE (parm);
27852 tree parsed_arg;
27853 vec<tree, va_gc> *insts;
27854 tree copy;
27855 unsigned ix;
27857 if (!default_arg)
27858 continue;
27860 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27861 /* This can happen for a friend declaration for a function
27862 already declared with default arguments. */
27863 continue;
27865 parsed_arg
27866 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27867 default_arg,
27868 TREE_VALUE (parm));
27869 TREE_PURPOSE (parm) = parsed_arg;
27871 /* Update any instantiations we've already created. */
27872 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27873 vec_safe_iterate (insts, ix, &copy); ix++)
27874 TREE_PURPOSE (copy) = parsed_arg;
27877 pop_defarg_context ();
27879 /* Make sure no default arg is missing. */
27880 check_default_args (fn);
27882 /* Restore the state of local_variables_forbidden_p. */
27883 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27885 /* Restore the queue. */
27886 pop_unparsed_function_queues (parser);
27889 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27891 sizeof ... ( identifier )
27893 where the 'sizeof' token has already been consumed. */
27895 static tree
27896 cp_parser_sizeof_pack (cp_parser *parser)
27898 /* Consume the `...'. */
27899 cp_lexer_consume_token (parser->lexer);
27900 maybe_warn_variadic_templates ();
27902 matching_parens parens;
27903 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27904 if (paren)
27905 parens.consume_open (parser);
27906 else
27907 permerror (cp_lexer_peek_token (parser->lexer)->location,
27908 "%<sizeof...%> argument must be surrounded by parentheses");
27910 cp_token *token = cp_lexer_peek_token (parser->lexer);
27911 tree name = cp_parser_identifier (parser);
27912 if (name == error_mark_node)
27913 return error_mark_node;
27914 /* The name is not qualified. */
27915 parser->scope = NULL_TREE;
27916 parser->qualifying_scope = NULL_TREE;
27917 parser->object_scope = NULL_TREE;
27918 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27919 if (expr == error_mark_node)
27920 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27921 token->location);
27922 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27923 expr = TREE_TYPE (expr);
27924 else if (TREE_CODE (expr) == CONST_DECL)
27925 expr = DECL_INITIAL (expr);
27926 expr = make_pack_expansion (expr);
27927 PACK_EXPANSION_SIZEOF_P (expr) = true;
27929 if (paren)
27930 parens.require_close (parser);
27932 return expr;
27935 /* Parse the operand of `sizeof' (or a similar operator). Returns
27936 either a TYPE or an expression, depending on the form of the
27937 input. The KEYWORD indicates which kind of expression we have
27938 encountered. */
27940 static tree
27941 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27943 tree expr = NULL_TREE;
27944 const char *saved_message;
27945 char *tmp;
27946 bool saved_integral_constant_expression_p;
27947 bool saved_non_integral_constant_expression_p;
27949 /* If it's a `...', then we are computing the length of a parameter
27950 pack. */
27951 if (keyword == RID_SIZEOF
27952 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27953 return cp_parser_sizeof_pack (parser);
27955 /* Types cannot be defined in a `sizeof' expression. Save away the
27956 old message. */
27957 saved_message = parser->type_definition_forbidden_message;
27958 /* And create the new one. */
27959 tmp = concat ("types may not be defined in %<",
27960 IDENTIFIER_POINTER (ridpointers[keyword]),
27961 "%> expressions", NULL);
27962 parser->type_definition_forbidden_message = tmp;
27964 /* The restrictions on constant-expressions do not apply inside
27965 sizeof expressions. */
27966 saved_integral_constant_expression_p
27967 = parser->integral_constant_expression_p;
27968 saved_non_integral_constant_expression_p
27969 = parser->non_integral_constant_expression_p;
27970 parser->integral_constant_expression_p = false;
27972 /* Do not actually evaluate the expression. */
27973 ++cp_unevaluated_operand;
27974 ++c_inhibit_evaluation_warnings;
27975 /* If it's a `(', then we might be looking at the type-id
27976 construction. */
27977 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27979 tree type = NULL_TREE;
27981 /* We can't be sure yet whether we're looking at a type-id or an
27982 expression. */
27983 cp_parser_parse_tentatively (parser);
27985 matching_parens parens;
27986 parens.consume_open (parser);
27988 /* Note: as a GNU Extension, compound literals are considered
27989 postfix-expressions as they are in C99, so they are valid
27990 arguments to sizeof. See comment in cp_parser_cast_expression
27991 for details. */
27992 if (cp_parser_compound_literal_p (parser))
27993 cp_parser_simulate_error (parser);
27994 else
27996 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27997 parser->in_type_id_in_expr_p = true;
27998 /* Look for the type-id. */
27999 type = cp_parser_type_id (parser);
28000 /* Look for the closing `)'. */
28001 parens.require_close (parser);
28002 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28005 /* If all went well, then we're done. */
28006 if (cp_parser_parse_definitely (parser))
28008 cp_decl_specifier_seq decl_specs;
28010 /* Build a trivial decl-specifier-seq. */
28011 clear_decl_specs (&decl_specs);
28012 decl_specs.type = type;
28014 /* Call grokdeclarator to figure out what type this is. */
28015 expr = grokdeclarator (NULL,
28016 &decl_specs,
28017 TYPENAME,
28018 /*initialized=*/0,
28019 /*attrlist=*/NULL);
28023 /* If the type-id production did not work out, then we must be
28024 looking at the unary-expression production. */
28025 if (!expr)
28026 expr = cp_parser_unary_expression (parser);
28028 /* Go back to evaluating expressions. */
28029 --cp_unevaluated_operand;
28030 --c_inhibit_evaluation_warnings;
28032 /* Free the message we created. */
28033 free (tmp);
28034 /* And restore the old one. */
28035 parser->type_definition_forbidden_message = saved_message;
28036 parser->integral_constant_expression_p
28037 = saved_integral_constant_expression_p;
28038 parser->non_integral_constant_expression_p
28039 = saved_non_integral_constant_expression_p;
28041 return expr;
28044 /* If the current declaration has no declarator, return true. */
28046 static bool
28047 cp_parser_declares_only_class_p (cp_parser *parser)
28049 /* If the next token is a `;' or a `,' then there is no
28050 declarator. */
28051 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28052 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28055 /* Update the DECL_SPECS to reflect the storage class indicated by
28056 KEYWORD. */
28058 static void
28059 cp_parser_set_storage_class (cp_parser *parser,
28060 cp_decl_specifier_seq *decl_specs,
28061 enum rid keyword,
28062 cp_token *token)
28064 cp_storage_class storage_class;
28066 if (parser->in_unbraced_linkage_specification_p)
28068 error_at (token->location, "invalid use of %qD in linkage specification",
28069 ridpointers[keyword]);
28070 return;
28072 else if (decl_specs->storage_class != sc_none)
28074 decl_specs->conflicting_specifiers_p = true;
28075 return;
28078 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28079 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28080 && decl_specs->gnu_thread_keyword_p)
28082 pedwarn (decl_specs->locations[ds_thread], 0,
28083 "%<__thread%> before %qD", ridpointers[keyword]);
28086 switch (keyword)
28088 case RID_AUTO:
28089 storage_class = sc_auto;
28090 break;
28091 case RID_REGISTER:
28092 storage_class = sc_register;
28093 break;
28094 case RID_STATIC:
28095 storage_class = sc_static;
28096 break;
28097 case RID_EXTERN:
28098 storage_class = sc_extern;
28099 break;
28100 case RID_MUTABLE:
28101 storage_class = sc_mutable;
28102 break;
28103 default:
28104 gcc_unreachable ();
28106 decl_specs->storage_class = storage_class;
28107 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28109 /* A storage class specifier cannot be applied alongside a typedef
28110 specifier. If there is a typedef specifier present then set
28111 conflicting_specifiers_p which will trigger an error later
28112 on in grokdeclarator. */
28113 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28114 decl_specs->conflicting_specifiers_p = true;
28117 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28118 is true, the type is a class or enum definition. */
28120 static void
28121 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28122 tree type_spec,
28123 cp_token *token,
28124 bool type_definition_p)
28126 decl_specs->any_specifiers_p = true;
28128 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28129 (with, for example, in "typedef int wchar_t;") we remember that
28130 this is what happened. In system headers, we ignore these
28131 declarations so that G++ can work with system headers that are not
28132 C++-safe. */
28133 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28134 && !type_definition_p
28135 && (type_spec == boolean_type_node
28136 || type_spec == char16_type_node
28137 || type_spec == char32_type_node
28138 || type_spec == wchar_type_node)
28139 && (decl_specs->type
28140 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28141 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28142 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28143 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28145 decl_specs->redefined_builtin_type = type_spec;
28146 set_and_check_decl_spec_loc (decl_specs,
28147 ds_redefined_builtin_type_spec,
28148 token);
28149 if (!decl_specs->type)
28151 decl_specs->type = type_spec;
28152 decl_specs->type_definition_p = false;
28153 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28156 else if (decl_specs->type)
28157 decl_specs->multiple_types_p = true;
28158 else
28160 decl_specs->type = type_spec;
28161 decl_specs->type_definition_p = type_definition_p;
28162 decl_specs->redefined_builtin_type = NULL_TREE;
28163 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28167 /* True iff TOKEN is the GNU keyword __thread. */
28169 static bool
28170 token_is__thread (cp_token *token)
28172 gcc_assert (token->keyword == RID_THREAD);
28173 return id_equal (token->u.value, "__thread");
28176 /* Set the location for a declarator specifier and check if it is
28177 duplicated.
28179 DECL_SPECS is the sequence of declarator specifiers onto which to
28180 set the location.
28182 DS is the single declarator specifier to set which location is to
28183 be set onto the existing sequence of declarators.
28185 LOCATION is the location for the declarator specifier to
28186 consider. */
28188 static void
28189 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28190 cp_decl_spec ds, cp_token *token)
28192 gcc_assert (ds < ds_last);
28194 if (decl_specs == NULL)
28195 return;
28197 source_location location = token->location;
28199 if (decl_specs->locations[ds] == 0)
28201 decl_specs->locations[ds] = location;
28202 if (ds == ds_thread)
28203 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28205 else
28207 if (ds == ds_long)
28209 if (decl_specs->locations[ds_long_long] != 0)
28210 error_at (location,
28211 "%<long long long%> is too long for GCC");
28212 else
28214 decl_specs->locations[ds_long_long] = location;
28215 pedwarn_cxx98 (location,
28216 OPT_Wlong_long,
28217 "ISO C++ 1998 does not support %<long long%>");
28220 else if (ds == ds_thread)
28222 bool gnu = token_is__thread (token);
28223 if (gnu != decl_specs->gnu_thread_keyword_p)
28224 error_at (location,
28225 "both %<__thread%> and %<thread_local%> specified");
28226 else
28228 gcc_rich_location richloc (location);
28229 richloc.add_fixit_remove ();
28230 error_at (&richloc, "duplicate %qD", token->u.value);
28233 else
28235 static const char *const decl_spec_names[] = {
28236 "signed",
28237 "unsigned",
28238 "short",
28239 "long",
28240 "const",
28241 "volatile",
28242 "restrict",
28243 "inline",
28244 "virtual",
28245 "explicit",
28246 "friend",
28247 "typedef",
28248 "using",
28249 "constexpr",
28250 "__complex"
28252 gcc_rich_location richloc (location);
28253 richloc.add_fixit_remove ();
28254 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28259 /* Return true iff the declarator specifier DS is present in the
28260 sequence of declarator specifiers DECL_SPECS. */
28262 bool
28263 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28264 cp_decl_spec ds)
28266 gcc_assert (ds < ds_last);
28268 if (decl_specs == NULL)
28269 return false;
28271 return decl_specs->locations[ds] != 0;
28274 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28275 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28277 static bool
28278 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28280 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28283 /* Issue an error message indicating that TOKEN_DESC was expected.
28284 If KEYWORD is true, it indicated this function is called by
28285 cp_parser_require_keword and the required token can only be
28286 a indicated keyword.
28288 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28289 within any error as the location of an "opening" token matching
28290 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28291 RT_CLOSE_PAREN). */
28293 static void
28294 cp_parser_required_error (cp_parser *parser,
28295 required_token token_desc,
28296 bool keyword,
28297 location_t matching_location)
28299 if (cp_parser_simulate_error (parser))
28300 return;
28302 const char *gmsgid = NULL;
28303 switch (token_desc)
28305 case RT_NEW:
28306 gmsgid = G_("expected %<new%>");
28307 break;
28308 case RT_DELETE:
28309 gmsgid = G_("expected %<delete%>");
28310 break;
28311 case RT_RETURN:
28312 gmsgid = G_("expected %<return%>");
28313 break;
28314 case RT_WHILE:
28315 gmsgid = G_("expected %<while%>");
28316 break;
28317 case RT_EXTERN:
28318 gmsgid = G_("expected %<extern%>");
28319 break;
28320 case RT_STATIC_ASSERT:
28321 gmsgid = G_("expected %<static_assert%>");
28322 break;
28323 case RT_DECLTYPE:
28324 gmsgid = G_("expected %<decltype%>");
28325 break;
28326 case RT_OPERATOR:
28327 gmsgid = G_("expected %<operator%>");
28328 break;
28329 case RT_CLASS:
28330 gmsgid = G_("expected %<class%>");
28331 break;
28332 case RT_TEMPLATE:
28333 gmsgid = G_("expected %<template%>");
28334 break;
28335 case RT_NAMESPACE:
28336 gmsgid = G_("expected %<namespace%>");
28337 break;
28338 case RT_USING:
28339 gmsgid = G_("expected %<using%>");
28340 break;
28341 case RT_ASM:
28342 gmsgid = G_("expected %<asm%>");
28343 break;
28344 case RT_TRY:
28345 gmsgid = G_("expected %<try%>");
28346 break;
28347 case RT_CATCH:
28348 gmsgid = G_("expected %<catch%>");
28349 break;
28350 case RT_THROW:
28351 gmsgid = G_("expected %<throw%>");
28352 break;
28353 case RT_LABEL:
28354 gmsgid = G_("expected %<__label__%>");
28355 break;
28356 case RT_AT_TRY:
28357 gmsgid = G_("expected %<@try%>");
28358 break;
28359 case RT_AT_SYNCHRONIZED:
28360 gmsgid = G_("expected %<@synchronized%>");
28361 break;
28362 case RT_AT_THROW:
28363 gmsgid = G_("expected %<@throw%>");
28364 break;
28365 case RT_TRANSACTION_ATOMIC:
28366 gmsgid = G_("expected %<__transaction_atomic%>");
28367 break;
28368 case RT_TRANSACTION_RELAXED:
28369 gmsgid = G_("expected %<__transaction_relaxed%>");
28370 break;
28371 default:
28372 break;
28375 if (!gmsgid && !keyword)
28377 switch (token_desc)
28379 case RT_SEMICOLON:
28380 gmsgid = G_("expected %<;%>");
28381 break;
28382 case RT_OPEN_PAREN:
28383 gmsgid = G_("expected %<(%>");
28384 break;
28385 case RT_CLOSE_BRACE:
28386 gmsgid = G_("expected %<}%>");
28387 break;
28388 case RT_OPEN_BRACE:
28389 gmsgid = G_("expected %<{%>");
28390 break;
28391 case RT_CLOSE_SQUARE:
28392 gmsgid = G_("expected %<]%>");
28393 break;
28394 case RT_OPEN_SQUARE:
28395 gmsgid = G_("expected %<[%>");
28396 break;
28397 case RT_COMMA:
28398 gmsgid = G_("expected %<,%>");
28399 break;
28400 case RT_SCOPE:
28401 gmsgid = G_("expected %<::%>");
28402 break;
28403 case RT_LESS:
28404 gmsgid = G_("expected %<<%>");
28405 break;
28406 case RT_GREATER:
28407 gmsgid = G_("expected %<>%>");
28408 break;
28409 case RT_EQ:
28410 gmsgid = G_("expected %<=%>");
28411 break;
28412 case RT_ELLIPSIS:
28413 gmsgid = G_("expected %<...%>");
28414 break;
28415 case RT_MULT:
28416 gmsgid = G_("expected %<*%>");
28417 break;
28418 case RT_COMPL:
28419 gmsgid = G_("expected %<~%>");
28420 break;
28421 case RT_COLON:
28422 gmsgid = G_("expected %<:%>");
28423 break;
28424 case RT_COLON_SCOPE:
28425 gmsgid = G_("expected %<:%> or %<::%>");
28426 break;
28427 case RT_CLOSE_PAREN:
28428 gmsgid = G_("expected %<)%>");
28429 break;
28430 case RT_COMMA_CLOSE_PAREN:
28431 gmsgid = G_("expected %<,%> or %<)%>");
28432 break;
28433 case RT_PRAGMA_EOL:
28434 gmsgid = G_("expected end of line");
28435 break;
28436 case RT_NAME:
28437 gmsgid = G_("expected identifier");
28438 break;
28439 case RT_SELECT:
28440 gmsgid = G_("expected selection-statement");
28441 break;
28442 case RT_ITERATION:
28443 gmsgid = G_("expected iteration-statement");
28444 break;
28445 case RT_JUMP:
28446 gmsgid = G_("expected jump-statement");
28447 break;
28448 case RT_CLASS_KEY:
28449 gmsgid = G_("expected class-key");
28450 break;
28451 case RT_CLASS_TYPENAME_TEMPLATE:
28452 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28453 break;
28454 default:
28455 gcc_unreachable ();
28459 if (gmsgid)
28460 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28464 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28465 issue an error message indicating that TOKEN_DESC was expected.
28467 Returns the token consumed, if the token had the appropriate type.
28468 Otherwise, returns NULL.
28470 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28471 within any error as the location of an "opening" token matching
28472 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28473 RT_CLOSE_PAREN). */
28475 static cp_token *
28476 cp_parser_require (cp_parser* parser,
28477 enum cpp_ttype type,
28478 required_token token_desc,
28479 location_t matching_location)
28481 if (cp_lexer_next_token_is (parser->lexer, type))
28482 return cp_lexer_consume_token (parser->lexer);
28483 else
28485 /* Output the MESSAGE -- unless we're parsing tentatively. */
28486 if (!cp_parser_simulate_error (parser))
28487 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28488 matching_location);
28489 return NULL;
28493 /* An error message is produced if the next token is not '>'.
28494 All further tokens are skipped until the desired token is
28495 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28497 static void
28498 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28500 /* Current level of '< ... >'. */
28501 unsigned level = 0;
28502 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28503 unsigned nesting_depth = 0;
28505 /* Are we ready, yet? If not, issue error message. */
28506 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28507 return;
28509 /* Skip tokens until the desired token is found. */
28510 while (true)
28512 /* Peek at the next token. */
28513 switch (cp_lexer_peek_token (parser->lexer)->type)
28515 case CPP_LESS:
28516 if (!nesting_depth)
28517 ++level;
28518 break;
28520 case CPP_RSHIFT:
28521 if (cxx_dialect == cxx98)
28522 /* C++0x views the `>>' operator as two `>' tokens, but
28523 C++98 does not. */
28524 break;
28525 else if (!nesting_depth && level-- == 0)
28527 /* We've hit a `>>' where the first `>' closes the
28528 template argument list, and the second `>' is
28529 spurious. Just consume the `>>' and stop; we've
28530 already produced at least one error. */
28531 cp_lexer_consume_token (parser->lexer);
28532 return;
28534 /* Fall through for C++0x, so we handle the second `>' in
28535 the `>>'. */
28536 gcc_fallthrough ();
28538 case CPP_GREATER:
28539 if (!nesting_depth && level-- == 0)
28541 /* We've reached the token we want, consume it and stop. */
28542 cp_lexer_consume_token (parser->lexer);
28543 return;
28545 break;
28547 case CPP_OPEN_PAREN:
28548 case CPP_OPEN_SQUARE:
28549 ++nesting_depth;
28550 break;
28552 case CPP_CLOSE_PAREN:
28553 case CPP_CLOSE_SQUARE:
28554 if (nesting_depth-- == 0)
28555 return;
28556 break;
28558 case CPP_EOF:
28559 case CPP_PRAGMA_EOL:
28560 case CPP_SEMICOLON:
28561 case CPP_OPEN_BRACE:
28562 case CPP_CLOSE_BRACE:
28563 /* The '>' was probably forgotten, don't look further. */
28564 return;
28566 default:
28567 break;
28570 /* Consume this token. */
28571 cp_lexer_consume_token (parser->lexer);
28575 /* If the next token is the indicated keyword, consume it. Otherwise,
28576 issue an error message indicating that TOKEN_DESC was expected.
28578 Returns the token consumed, if the token had the appropriate type.
28579 Otherwise, returns NULL. */
28581 static cp_token *
28582 cp_parser_require_keyword (cp_parser* parser,
28583 enum rid keyword,
28584 required_token token_desc)
28586 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28588 if (token && token->keyword != keyword)
28590 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28591 UNKNOWN_LOCATION);
28592 return NULL;
28595 return token;
28598 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28599 function-definition. */
28601 static bool
28602 cp_parser_token_starts_function_definition_p (cp_token* token)
28604 return (/* An ordinary function-body begins with an `{'. */
28605 token->type == CPP_OPEN_BRACE
28606 /* A ctor-initializer begins with a `:'. */
28607 || token->type == CPP_COLON
28608 /* A function-try-block begins with `try'. */
28609 || token->keyword == RID_TRY
28610 /* A function-transaction-block begins with `__transaction_atomic'
28611 or `__transaction_relaxed'. */
28612 || token->keyword == RID_TRANSACTION_ATOMIC
28613 || token->keyword == RID_TRANSACTION_RELAXED
28614 /* The named return value extension begins with `return'. */
28615 || token->keyword == RID_RETURN);
28618 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28619 definition. */
28621 static bool
28622 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28624 cp_token *token;
28626 token = cp_lexer_peek_token (parser->lexer);
28627 return (token->type == CPP_OPEN_BRACE
28628 || (token->type == CPP_COLON
28629 && !parser->colon_doesnt_start_class_def_p));
28632 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28633 C++0x) ending a template-argument. */
28635 static bool
28636 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28638 cp_token *token;
28640 token = cp_lexer_peek_token (parser->lexer);
28641 return (token->type == CPP_COMMA
28642 || token->type == CPP_GREATER
28643 || token->type == CPP_ELLIPSIS
28644 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28647 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28648 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28650 static bool
28651 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28652 size_t n)
28654 cp_token *token;
28656 token = cp_lexer_peek_nth_token (parser->lexer, n);
28657 if (token->type == CPP_LESS)
28658 return true;
28659 /* Check for the sequence `<::' in the original code. It would be lexed as
28660 `[:', where `[' is a digraph, and there is no whitespace before
28661 `:'. */
28662 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28664 cp_token *token2;
28665 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28666 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28667 return true;
28669 return false;
28672 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28673 or none_type otherwise. */
28675 static enum tag_types
28676 cp_parser_token_is_class_key (cp_token* token)
28678 switch (token->keyword)
28680 case RID_CLASS:
28681 return class_type;
28682 case RID_STRUCT:
28683 return record_type;
28684 case RID_UNION:
28685 return union_type;
28687 default:
28688 return none_type;
28692 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28693 or none_type otherwise or if the token is null. */
28695 static enum tag_types
28696 cp_parser_token_is_type_parameter_key (cp_token* token)
28698 if (!token)
28699 return none_type;
28701 switch (token->keyword)
28703 case RID_CLASS:
28704 return class_type;
28705 case RID_TYPENAME:
28706 return typename_type;
28708 default:
28709 return none_type;
28713 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28715 static void
28716 cp_parser_check_class_key (enum tag_types class_key, tree type)
28718 if (type == error_mark_node)
28719 return;
28720 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28722 if (permerror (input_location, "%qs tag used in naming %q#T",
28723 class_key == union_type ? "union"
28724 : class_key == record_type ? "struct" : "class",
28725 type))
28726 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28727 "%q#T was previously declared here", type);
28731 /* Issue an error message if DECL is redeclared with different
28732 access than its original declaration [class.access.spec/3].
28733 This applies to nested classes, nested class templates and
28734 enumerations [class.mem/1]. */
28736 static void
28737 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28739 if (!decl
28740 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28741 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28742 return;
28744 if ((TREE_PRIVATE (decl)
28745 != (current_access_specifier == access_private_node))
28746 || (TREE_PROTECTED (decl)
28747 != (current_access_specifier == access_protected_node)))
28748 error_at (location, "%qD redeclared with different access", decl);
28751 /* Look for the `template' keyword, as a syntactic disambiguator.
28752 Return TRUE iff it is present, in which case it will be
28753 consumed. */
28755 static bool
28756 cp_parser_optional_template_keyword (cp_parser *parser)
28758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28760 /* In C++98 the `template' keyword can only be used within templates;
28761 outside templates the parser can always figure out what is a
28762 template and what is not. In C++11, per the resolution of DR 468,
28763 `template' is allowed in cases where it is not strictly necessary. */
28764 if (!processing_template_decl
28765 && pedantic && cxx_dialect == cxx98)
28767 cp_token *token = cp_lexer_peek_token (parser->lexer);
28768 pedwarn (token->location, OPT_Wpedantic,
28769 "in C++98 %<template%> (as a disambiguator) is only "
28770 "allowed within templates");
28771 /* If this part of the token stream is rescanned, the same
28772 error message would be generated. So, we purge the token
28773 from the stream. */
28774 cp_lexer_purge_token (parser->lexer);
28775 return false;
28777 else
28779 /* Consume the `template' keyword. */
28780 cp_lexer_consume_token (parser->lexer);
28781 return true;
28784 return false;
28787 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28788 set PARSER->SCOPE, and perform other related actions. */
28790 static void
28791 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28793 struct tree_check *check_value;
28795 /* Get the stored value. */
28796 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28797 /* Set the scope from the stored value. */
28798 parser->scope = saved_checks_value (check_value);
28799 parser->qualifying_scope = check_value->qualifying_scope;
28800 parser->object_scope = NULL_TREE;
28803 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28804 encounter the end of a block before what we were looking for. */
28806 static bool
28807 cp_parser_cache_group (cp_parser *parser,
28808 enum cpp_ttype end,
28809 unsigned depth)
28811 while (true)
28813 cp_token *token = cp_lexer_peek_token (parser->lexer);
28815 /* Abort a parenthesized expression if we encounter a semicolon. */
28816 if ((end == CPP_CLOSE_PAREN || depth == 0)
28817 && token->type == CPP_SEMICOLON)
28818 return true;
28819 /* If we've reached the end of the file, stop. */
28820 if (token->type == CPP_EOF
28821 || (end != CPP_PRAGMA_EOL
28822 && token->type == CPP_PRAGMA_EOL))
28823 return true;
28824 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28825 /* We've hit the end of an enclosing block, so there's been some
28826 kind of syntax error. */
28827 return true;
28829 /* Consume the token. */
28830 cp_lexer_consume_token (parser->lexer);
28831 /* See if it starts a new group. */
28832 if (token->type == CPP_OPEN_BRACE)
28834 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28835 /* In theory this should probably check end == '}', but
28836 cp_parser_save_member_function_body needs it to exit
28837 after either '}' or ')' when called with ')'. */
28838 if (depth == 0)
28839 return false;
28841 else if (token->type == CPP_OPEN_PAREN)
28843 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28844 if (depth == 0 && end == CPP_CLOSE_PAREN)
28845 return false;
28847 else if (token->type == CPP_PRAGMA)
28848 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28849 else if (token->type == end)
28850 return false;
28854 /* Like above, for caching a default argument or NSDMI. Both of these are
28855 terminated by a non-nested comma, but it can be unclear whether or not a
28856 comma is nested in a template argument list unless we do more parsing.
28857 In order to handle this ambiguity, when we encounter a ',' after a '<'
28858 we try to parse what follows as a parameter-declaration-list (in the
28859 case of a default argument) or a member-declarator (in the case of an
28860 NSDMI). If that succeeds, then we stop caching. */
28862 static tree
28863 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28865 unsigned depth = 0;
28866 int maybe_template_id = 0;
28867 cp_token *first_token;
28868 cp_token *token;
28869 tree default_argument;
28871 /* Add tokens until we have processed the entire default
28872 argument. We add the range [first_token, token). */
28873 first_token = cp_lexer_peek_token (parser->lexer);
28874 if (first_token->type == CPP_OPEN_BRACE)
28876 /* For list-initialization, this is straightforward. */
28877 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28878 token = cp_lexer_peek_token (parser->lexer);
28880 else while (true)
28882 bool done = false;
28884 /* Peek at the next token. */
28885 token = cp_lexer_peek_token (parser->lexer);
28886 /* What we do depends on what token we have. */
28887 switch (token->type)
28889 /* In valid code, a default argument must be
28890 immediately followed by a `,' `)', or `...'. */
28891 case CPP_COMMA:
28892 if (depth == 0 && maybe_template_id)
28894 /* If we've seen a '<', we might be in a
28895 template-argument-list. Until Core issue 325 is
28896 resolved, we don't know how this situation ought
28897 to be handled, so try to DTRT. We check whether
28898 what comes after the comma is a valid parameter
28899 declaration list. If it is, then the comma ends
28900 the default argument; otherwise the default
28901 argument continues. */
28902 bool error = false;
28903 cp_token *peek;
28905 /* Set ITALP so cp_parser_parameter_declaration_list
28906 doesn't decide to commit to this parse. */
28907 bool saved_italp = parser->in_template_argument_list_p;
28908 parser->in_template_argument_list_p = true;
28910 cp_parser_parse_tentatively (parser);
28912 if (nsdmi)
28914 /* Parse declarators until we reach a non-comma or
28915 somthing that cannot be an initializer.
28916 Just checking whether we're looking at a single
28917 declarator is insufficient. Consider:
28918 int var = tuple<T,U>::x;
28919 The template parameter 'U' looks exactly like a
28920 declarator. */
28923 int ctor_dtor_or_conv_p;
28924 cp_lexer_consume_token (parser->lexer);
28925 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28926 &ctor_dtor_or_conv_p,
28927 /*parenthesized_p=*/NULL,
28928 /*member_p=*/true,
28929 /*friend_p=*/false);
28930 peek = cp_lexer_peek_token (parser->lexer);
28931 if (cp_parser_error_occurred (parser))
28932 break;
28934 while (peek->type == CPP_COMMA);
28935 /* If we met an '=' or ';' then the original comma
28936 was the end of the NSDMI. Otherwise assume
28937 we're still in the NSDMI. */
28938 error = (peek->type != CPP_EQ
28939 && peek->type != CPP_SEMICOLON);
28941 else
28943 cp_lexer_consume_token (parser->lexer);
28944 begin_scope (sk_function_parms, NULL_TREE);
28945 cp_parser_parameter_declaration_list (parser, &error);
28946 pop_bindings_and_leave_scope ();
28948 if (!cp_parser_error_occurred (parser) && !error)
28949 done = true;
28950 cp_parser_abort_tentative_parse (parser);
28952 parser->in_template_argument_list_p = saved_italp;
28953 break;
28955 /* FALLTHRU */
28956 case CPP_CLOSE_PAREN:
28957 case CPP_ELLIPSIS:
28958 /* If we run into a non-nested `;', `}', or `]',
28959 then the code is invalid -- but the default
28960 argument is certainly over. */
28961 case CPP_SEMICOLON:
28962 case CPP_CLOSE_BRACE:
28963 case CPP_CLOSE_SQUARE:
28964 if (depth == 0
28965 /* Handle correctly int n = sizeof ... ( p ); */
28966 && token->type != CPP_ELLIPSIS)
28967 done = true;
28968 /* Update DEPTH, if necessary. */
28969 else if (token->type == CPP_CLOSE_PAREN
28970 || token->type == CPP_CLOSE_BRACE
28971 || token->type == CPP_CLOSE_SQUARE)
28972 --depth;
28973 break;
28975 case CPP_OPEN_PAREN:
28976 case CPP_OPEN_SQUARE:
28977 case CPP_OPEN_BRACE:
28978 ++depth;
28979 break;
28981 case CPP_LESS:
28982 if (depth == 0)
28983 /* This might be the comparison operator, or it might
28984 start a template argument list. */
28985 ++maybe_template_id;
28986 break;
28988 case CPP_RSHIFT:
28989 if (cxx_dialect == cxx98)
28990 break;
28991 /* Fall through for C++0x, which treats the `>>'
28992 operator like two `>' tokens in certain
28993 cases. */
28994 gcc_fallthrough ();
28996 case CPP_GREATER:
28997 if (depth == 0)
28999 /* This might be an operator, or it might close a
29000 template argument list. But if a previous '<'
29001 started a template argument list, this will have
29002 closed it, so we can't be in one anymore. */
29003 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29004 if (maybe_template_id < 0)
29005 maybe_template_id = 0;
29007 break;
29009 /* If we run out of tokens, issue an error message. */
29010 case CPP_EOF:
29011 case CPP_PRAGMA_EOL:
29012 error_at (token->location, "file ends in default argument");
29013 return error_mark_node;
29015 case CPP_NAME:
29016 case CPP_SCOPE:
29017 /* In these cases, we should look for template-ids.
29018 For example, if the default argument is
29019 `X<int, double>()', we need to do name lookup to
29020 figure out whether or not `X' is a template; if
29021 so, the `,' does not end the default argument.
29023 That is not yet done. */
29024 break;
29026 default:
29027 break;
29030 /* If we've reached the end, stop. */
29031 if (done)
29032 break;
29034 /* Add the token to the token block. */
29035 token = cp_lexer_consume_token (parser->lexer);
29038 /* Create a DEFAULT_ARG to represent the unparsed default
29039 argument. */
29040 default_argument = make_node (DEFAULT_ARG);
29041 DEFARG_TOKENS (default_argument)
29042 = cp_token_cache_new (first_token, token);
29043 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29045 return default_argument;
29048 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29050 location_t
29051 defarg_location (tree default_argument)
29053 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29054 location_t start = tokens->first->location;
29055 location_t end = tokens->last->location;
29056 return make_location (start, start, end);
29059 /* Begin parsing tentatively. We always save tokens while parsing
29060 tentatively so that if the tentative parsing fails we can restore the
29061 tokens. */
29063 static void
29064 cp_parser_parse_tentatively (cp_parser* parser)
29066 /* Enter a new parsing context. */
29067 parser->context = cp_parser_context_new (parser->context);
29068 /* Begin saving tokens. */
29069 cp_lexer_save_tokens (parser->lexer);
29070 /* In order to avoid repetitive access control error messages,
29071 access checks are queued up until we are no longer parsing
29072 tentatively. */
29073 push_deferring_access_checks (dk_deferred);
29076 /* Commit to the currently active tentative parse. */
29078 static void
29079 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29081 cp_parser_context *context;
29082 cp_lexer *lexer;
29084 /* Mark all of the levels as committed. */
29085 lexer = parser->lexer;
29086 for (context = parser->context; context->next; context = context->next)
29088 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29089 break;
29090 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29091 while (!cp_lexer_saving_tokens (lexer))
29092 lexer = lexer->next;
29093 cp_lexer_commit_tokens (lexer);
29097 /* Commit to the topmost currently active tentative parse.
29099 Note that this function shouldn't be called when there are
29100 irreversible side-effects while in a tentative state. For
29101 example, we shouldn't create a permanent entry in the symbol
29102 table, or issue an error message that might not apply if the
29103 tentative parse is aborted. */
29105 static void
29106 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29108 cp_parser_context *context = parser->context;
29109 cp_lexer *lexer = parser->lexer;
29111 if (context)
29113 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29114 return;
29115 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29117 while (!cp_lexer_saving_tokens (lexer))
29118 lexer = lexer->next;
29119 cp_lexer_commit_tokens (lexer);
29123 /* Abort the currently active tentative parse. All consumed tokens
29124 will be rolled back, and no diagnostics will be issued. */
29126 static void
29127 cp_parser_abort_tentative_parse (cp_parser* parser)
29129 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29130 || errorcount > 0);
29131 cp_parser_simulate_error (parser);
29132 /* Now, pretend that we want to see if the construct was
29133 successfully parsed. */
29134 cp_parser_parse_definitely (parser);
29137 /* Stop parsing tentatively. If a parse error has occurred, restore the
29138 token stream. Otherwise, commit to the tokens we have consumed.
29139 Returns true if no error occurred; false otherwise. */
29141 static bool
29142 cp_parser_parse_definitely (cp_parser* parser)
29144 bool error_occurred;
29145 cp_parser_context *context;
29147 /* Remember whether or not an error occurred, since we are about to
29148 destroy that information. */
29149 error_occurred = cp_parser_error_occurred (parser);
29150 /* Remove the topmost context from the stack. */
29151 context = parser->context;
29152 parser->context = context->next;
29153 /* If no parse errors occurred, commit to the tentative parse. */
29154 if (!error_occurred)
29156 /* Commit to the tokens read tentatively, unless that was
29157 already done. */
29158 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29159 cp_lexer_commit_tokens (parser->lexer);
29161 pop_to_parent_deferring_access_checks ();
29163 /* Otherwise, if errors occurred, roll back our state so that things
29164 are just as they were before we began the tentative parse. */
29165 else
29167 cp_lexer_rollback_tokens (parser->lexer);
29168 pop_deferring_access_checks ();
29170 /* Add the context to the front of the free list. */
29171 context->next = cp_parser_context_free_list;
29172 cp_parser_context_free_list = context;
29174 return !error_occurred;
29177 /* Returns true if we are parsing tentatively and are not committed to
29178 this tentative parse. */
29180 static bool
29181 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29183 return (cp_parser_parsing_tentatively (parser)
29184 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29187 /* Returns nonzero iff an error has occurred during the most recent
29188 tentative parse. */
29190 static bool
29191 cp_parser_error_occurred (cp_parser* parser)
29193 return (cp_parser_parsing_tentatively (parser)
29194 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29197 /* Returns nonzero if GNU extensions are allowed. */
29199 static bool
29200 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29202 return parser->allow_gnu_extensions_p;
29205 /* Objective-C++ Productions */
29208 /* Parse an Objective-C expression, which feeds into a primary-expression
29209 above.
29211 objc-expression:
29212 objc-message-expression
29213 objc-string-literal
29214 objc-encode-expression
29215 objc-protocol-expression
29216 objc-selector-expression
29218 Returns a tree representation of the expression. */
29220 static cp_expr
29221 cp_parser_objc_expression (cp_parser* parser)
29223 /* Try to figure out what kind of declaration is present. */
29224 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29226 switch (kwd->type)
29228 case CPP_OPEN_SQUARE:
29229 return cp_parser_objc_message_expression (parser);
29231 case CPP_OBJC_STRING:
29232 kwd = cp_lexer_consume_token (parser->lexer);
29233 return objc_build_string_object (kwd->u.value);
29235 case CPP_KEYWORD:
29236 switch (kwd->keyword)
29238 case RID_AT_ENCODE:
29239 return cp_parser_objc_encode_expression (parser);
29241 case RID_AT_PROTOCOL:
29242 return cp_parser_objc_protocol_expression (parser);
29244 case RID_AT_SELECTOR:
29245 return cp_parser_objc_selector_expression (parser);
29247 default:
29248 break;
29250 /* FALLTHRU */
29251 default:
29252 error_at (kwd->location,
29253 "misplaced %<@%D%> Objective-C++ construct",
29254 kwd->u.value);
29255 cp_parser_skip_to_end_of_block_or_statement (parser);
29258 return error_mark_node;
29261 /* Parse an Objective-C message expression.
29263 objc-message-expression:
29264 [ objc-message-receiver objc-message-args ]
29266 Returns a representation of an Objective-C message. */
29268 static tree
29269 cp_parser_objc_message_expression (cp_parser* parser)
29271 tree receiver, messageargs;
29273 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29274 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29275 receiver = cp_parser_objc_message_receiver (parser);
29276 messageargs = cp_parser_objc_message_args (parser);
29277 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29278 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29280 tree result = objc_build_message_expr (receiver, messageargs);
29282 /* Construct a location e.g.
29283 [self func1:5]
29284 ^~~~~~~~~~~~~~
29285 ranging from the '[' to the ']', with the caret at the start. */
29286 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29287 protected_set_expr_location (result, combined_loc);
29289 return result;
29292 /* Parse an objc-message-receiver.
29294 objc-message-receiver:
29295 expression
29296 simple-type-specifier
29298 Returns a representation of the type or expression. */
29300 static tree
29301 cp_parser_objc_message_receiver (cp_parser* parser)
29303 tree rcv;
29305 /* An Objective-C message receiver may be either (1) a type
29306 or (2) an expression. */
29307 cp_parser_parse_tentatively (parser);
29308 rcv = cp_parser_expression (parser);
29310 /* If that worked out, fine. */
29311 if (cp_parser_parse_definitely (parser))
29312 return rcv;
29314 cp_parser_parse_tentatively (parser);
29315 rcv = cp_parser_simple_type_specifier (parser,
29316 /*decl_specs=*/NULL,
29317 CP_PARSER_FLAGS_NONE);
29319 if (cp_parser_parse_definitely (parser))
29320 return objc_get_class_reference (rcv);
29322 cp_parser_error (parser, "objective-c++ message receiver expected");
29323 return error_mark_node;
29326 /* Parse the arguments and selectors comprising an Objective-C message.
29328 objc-message-args:
29329 objc-selector
29330 objc-selector-args
29331 objc-selector-args , objc-comma-args
29333 objc-selector-args:
29334 objc-selector [opt] : assignment-expression
29335 objc-selector-args objc-selector [opt] : assignment-expression
29337 objc-comma-args:
29338 assignment-expression
29339 objc-comma-args , assignment-expression
29341 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29342 selector arguments and TREE_VALUE containing a list of comma
29343 arguments. */
29345 static tree
29346 cp_parser_objc_message_args (cp_parser* parser)
29348 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29349 bool maybe_unary_selector_p = true;
29350 cp_token *token = cp_lexer_peek_token (parser->lexer);
29352 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29354 tree selector = NULL_TREE, arg;
29356 if (token->type != CPP_COLON)
29357 selector = cp_parser_objc_selector (parser);
29359 /* Detect if we have a unary selector. */
29360 if (maybe_unary_selector_p
29361 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29362 return build_tree_list (selector, NULL_TREE);
29364 maybe_unary_selector_p = false;
29365 cp_parser_require (parser, CPP_COLON, RT_COLON);
29366 arg = cp_parser_assignment_expression (parser);
29368 sel_args
29369 = chainon (sel_args,
29370 build_tree_list (selector, arg));
29372 token = cp_lexer_peek_token (parser->lexer);
29375 /* Handle non-selector arguments, if any. */
29376 while (token->type == CPP_COMMA)
29378 tree arg;
29380 cp_lexer_consume_token (parser->lexer);
29381 arg = cp_parser_assignment_expression (parser);
29383 addl_args
29384 = chainon (addl_args,
29385 build_tree_list (NULL_TREE, arg));
29387 token = cp_lexer_peek_token (parser->lexer);
29390 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29392 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29393 return build_tree_list (error_mark_node, error_mark_node);
29396 return build_tree_list (sel_args, addl_args);
29399 /* Parse an Objective-C encode expression.
29401 objc-encode-expression:
29402 @encode objc-typename
29404 Returns an encoded representation of the type argument. */
29406 static cp_expr
29407 cp_parser_objc_encode_expression (cp_parser* parser)
29409 tree type;
29410 cp_token *token;
29411 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29413 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29414 matching_parens parens;
29415 parens.require_open (parser);
29416 token = cp_lexer_peek_token (parser->lexer);
29417 type = complete_type (cp_parser_type_id (parser));
29418 parens.require_close (parser);
29420 if (!type)
29422 error_at (token->location,
29423 "%<@encode%> must specify a type as an argument");
29424 return error_mark_node;
29427 /* This happens if we find @encode(T) (where T is a template
29428 typename or something dependent on a template typename) when
29429 parsing a template. In that case, we can't compile it
29430 immediately, but we rather create an AT_ENCODE_EXPR which will
29431 need to be instantiated when the template is used.
29433 if (dependent_type_p (type))
29435 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29436 TREE_READONLY (value) = 1;
29437 return value;
29441 /* Build a location of the form:
29442 @encode(int)
29443 ^~~~~~~~~~~~
29444 with caret==start at the @ token, finishing at the close paren. */
29445 location_t combined_loc
29446 = make_location (start_loc, start_loc,
29447 cp_lexer_previous_token (parser->lexer)->location);
29449 return cp_expr (objc_build_encode_expr (type), combined_loc);
29452 /* Parse an Objective-C @defs expression. */
29454 static tree
29455 cp_parser_objc_defs_expression (cp_parser *parser)
29457 tree name;
29459 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29460 matching_parens parens;
29461 parens.require_open (parser);
29462 name = cp_parser_identifier (parser);
29463 parens.require_close (parser);
29465 return objc_get_class_ivars (name);
29468 /* Parse an Objective-C protocol expression.
29470 objc-protocol-expression:
29471 @protocol ( identifier )
29473 Returns a representation of the protocol expression. */
29475 static tree
29476 cp_parser_objc_protocol_expression (cp_parser* parser)
29478 tree proto;
29479 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29481 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29482 matching_parens parens;
29483 parens.require_open (parser);
29484 proto = cp_parser_identifier (parser);
29485 parens.require_close (parser);
29487 /* Build a location of the form:
29488 @protocol(prot)
29489 ^~~~~~~~~~~~~~~
29490 with caret==start at the @ token, finishing at the close paren. */
29491 location_t combined_loc
29492 = make_location (start_loc, start_loc,
29493 cp_lexer_previous_token (parser->lexer)->location);
29494 tree result = objc_build_protocol_expr (proto);
29495 protected_set_expr_location (result, combined_loc);
29496 return result;
29499 /* Parse an Objective-C selector expression.
29501 objc-selector-expression:
29502 @selector ( objc-method-signature )
29504 objc-method-signature:
29505 objc-selector
29506 objc-selector-seq
29508 objc-selector-seq:
29509 objc-selector :
29510 objc-selector-seq objc-selector :
29512 Returns a representation of the method selector. */
29514 static tree
29515 cp_parser_objc_selector_expression (cp_parser* parser)
29517 tree sel_seq = NULL_TREE;
29518 bool maybe_unary_selector_p = true;
29519 cp_token *token;
29520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29522 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29523 matching_parens parens;
29524 parens.require_open (parser);
29525 token = cp_lexer_peek_token (parser->lexer);
29527 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29528 || token->type == CPP_SCOPE)
29530 tree selector = NULL_TREE;
29532 if (token->type != CPP_COLON
29533 || token->type == CPP_SCOPE)
29534 selector = cp_parser_objc_selector (parser);
29536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29537 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29539 /* Detect if we have a unary selector. */
29540 if (maybe_unary_selector_p)
29542 sel_seq = selector;
29543 goto finish_selector;
29545 else
29547 cp_parser_error (parser, "expected %<:%>");
29550 maybe_unary_selector_p = false;
29551 token = cp_lexer_consume_token (parser->lexer);
29553 if (token->type == CPP_SCOPE)
29555 sel_seq
29556 = chainon (sel_seq,
29557 build_tree_list (selector, NULL_TREE));
29558 sel_seq
29559 = chainon (sel_seq,
29560 build_tree_list (NULL_TREE, NULL_TREE));
29562 else
29563 sel_seq
29564 = chainon (sel_seq,
29565 build_tree_list (selector, NULL_TREE));
29567 token = cp_lexer_peek_token (parser->lexer);
29570 finish_selector:
29571 parens.require_close (parser);
29574 /* Build a location of the form:
29575 @selector(func)
29576 ^~~~~~~~~~~~~~~
29577 with caret==start at the @ token, finishing at the close paren. */
29578 location_t combined_loc
29579 = make_location (loc, loc,
29580 cp_lexer_previous_token (parser->lexer)->location);
29581 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29582 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29583 protected_set_expr_location (result, combined_loc);
29584 return result;
29587 /* Parse a list of identifiers.
29589 objc-identifier-list:
29590 identifier
29591 objc-identifier-list , identifier
29593 Returns a TREE_LIST of identifier nodes. */
29595 static tree
29596 cp_parser_objc_identifier_list (cp_parser* parser)
29598 tree identifier;
29599 tree list;
29600 cp_token *sep;
29602 identifier = cp_parser_identifier (parser);
29603 if (identifier == error_mark_node)
29604 return error_mark_node;
29606 list = build_tree_list (NULL_TREE, identifier);
29607 sep = cp_lexer_peek_token (parser->lexer);
29609 while (sep->type == CPP_COMMA)
29611 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29612 identifier = cp_parser_identifier (parser);
29613 if (identifier == error_mark_node)
29614 return list;
29616 list = chainon (list, build_tree_list (NULL_TREE,
29617 identifier));
29618 sep = cp_lexer_peek_token (parser->lexer);
29621 return list;
29624 /* Parse an Objective-C alias declaration.
29626 objc-alias-declaration:
29627 @compatibility_alias identifier identifier ;
29629 This function registers the alias mapping with the Objective-C front end.
29630 It returns nothing. */
29632 static void
29633 cp_parser_objc_alias_declaration (cp_parser* parser)
29635 tree alias, orig;
29637 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29638 alias = cp_parser_identifier (parser);
29639 orig = cp_parser_identifier (parser);
29640 objc_declare_alias (alias, orig);
29641 cp_parser_consume_semicolon_at_end_of_statement (parser);
29644 /* Parse an Objective-C class forward-declaration.
29646 objc-class-declaration:
29647 @class objc-identifier-list ;
29649 The function registers the forward declarations with the Objective-C
29650 front end. It returns nothing. */
29652 static void
29653 cp_parser_objc_class_declaration (cp_parser* parser)
29655 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29656 while (true)
29658 tree id;
29660 id = cp_parser_identifier (parser);
29661 if (id == error_mark_node)
29662 break;
29664 objc_declare_class (id);
29666 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29667 cp_lexer_consume_token (parser->lexer);
29668 else
29669 break;
29671 cp_parser_consume_semicolon_at_end_of_statement (parser);
29674 /* Parse a list of Objective-C protocol references.
29676 objc-protocol-refs-opt:
29677 objc-protocol-refs [opt]
29679 objc-protocol-refs:
29680 < objc-identifier-list >
29682 Returns a TREE_LIST of identifiers, if any. */
29684 static tree
29685 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29687 tree protorefs = NULL_TREE;
29689 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29691 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29692 protorefs = cp_parser_objc_identifier_list (parser);
29693 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29696 return protorefs;
29699 /* Parse a Objective-C visibility specification. */
29701 static void
29702 cp_parser_objc_visibility_spec (cp_parser* parser)
29704 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29706 switch (vis->keyword)
29708 case RID_AT_PRIVATE:
29709 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29710 break;
29711 case RID_AT_PROTECTED:
29712 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29713 break;
29714 case RID_AT_PUBLIC:
29715 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29716 break;
29717 case RID_AT_PACKAGE:
29718 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29719 break;
29720 default:
29721 return;
29724 /* Eat '@private'/'@protected'/'@public'. */
29725 cp_lexer_consume_token (parser->lexer);
29728 /* Parse an Objective-C method type. Return 'true' if it is a class
29729 (+) method, and 'false' if it is an instance (-) method. */
29731 static inline bool
29732 cp_parser_objc_method_type (cp_parser* parser)
29734 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29735 return true;
29736 else
29737 return false;
29740 /* Parse an Objective-C protocol qualifier. */
29742 static tree
29743 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29745 tree quals = NULL_TREE, node;
29746 cp_token *token = cp_lexer_peek_token (parser->lexer);
29748 node = token->u.value;
29750 while (node && identifier_p (node)
29751 && (node == ridpointers [(int) RID_IN]
29752 || node == ridpointers [(int) RID_OUT]
29753 || node == ridpointers [(int) RID_INOUT]
29754 || node == ridpointers [(int) RID_BYCOPY]
29755 || node == ridpointers [(int) RID_BYREF]
29756 || node == ridpointers [(int) RID_ONEWAY]))
29758 quals = tree_cons (NULL_TREE, node, quals);
29759 cp_lexer_consume_token (parser->lexer);
29760 token = cp_lexer_peek_token (parser->lexer);
29761 node = token->u.value;
29764 return quals;
29767 /* Parse an Objective-C typename. */
29769 static tree
29770 cp_parser_objc_typename (cp_parser* parser)
29772 tree type_name = NULL_TREE;
29774 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29776 tree proto_quals, cp_type = NULL_TREE;
29778 matching_parens parens;
29779 parens.consume_open (parser); /* Eat '('. */
29780 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29782 /* An ObjC type name may consist of just protocol qualifiers, in which
29783 case the type shall default to 'id'. */
29784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29786 cp_type = cp_parser_type_id (parser);
29788 /* If the type could not be parsed, an error has already
29789 been produced. For error recovery, behave as if it had
29790 not been specified, which will use the default type
29791 'id'. */
29792 if (cp_type == error_mark_node)
29794 cp_type = NULL_TREE;
29795 /* We need to skip to the closing parenthesis as
29796 cp_parser_type_id() does not seem to do it for
29797 us. */
29798 cp_parser_skip_to_closing_parenthesis (parser,
29799 /*recovering=*/true,
29800 /*or_comma=*/false,
29801 /*consume_paren=*/false);
29805 parens.require_close (parser);
29806 type_name = build_tree_list (proto_quals, cp_type);
29809 return type_name;
29812 /* Check to see if TYPE refers to an Objective-C selector name. */
29814 static bool
29815 cp_parser_objc_selector_p (enum cpp_ttype type)
29817 return (type == CPP_NAME || type == CPP_KEYWORD
29818 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29819 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29820 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29821 || type == CPP_XOR || type == CPP_XOR_EQ);
29824 /* Parse an Objective-C selector. */
29826 static tree
29827 cp_parser_objc_selector (cp_parser* parser)
29829 cp_token *token = cp_lexer_consume_token (parser->lexer);
29831 if (!cp_parser_objc_selector_p (token->type))
29833 error_at (token->location, "invalid Objective-C++ selector name");
29834 return error_mark_node;
29837 /* C++ operator names are allowed to appear in ObjC selectors. */
29838 switch (token->type)
29840 case CPP_AND_AND: return get_identifier ("and");
29841 case CPP_AND_EQ: return get_identifier ("and_eq");
29842 case CPP_AND: return get_identifier ("bitand");
29843 case CPP_OR: return get_identifier ("bitor");
29844 case CPP_COMPL: return get_identifier ("compl");
29845 case CPP_NOT: return get_identifier ("not");
29846 case CPP_NOT_EQ: return get_identifier ("not_eq");
29847 case CPP_OR_OR: return get_identifier ("or");
29848 case CPP_OR_EQ: return get_identifier ("or_eq");
29849 case CPP_XOR: return get_identifier ("xor");
29850 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29851 default: return token->u.value;
29855 /* Parse an Objective-C params list. */
29857 static tree
29858 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29860 tree params = NULL_TREE;
29861 bool maybe_unary_selector_p = true;
29862 cp_token *token = cp_lexer_peek_token (parser->lexer);
29864 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29866 tree selector = NULL_TREE, type_name, identifier;
29867 tree parm_attr = NULL_TREE;
29869 if (token->keyword == RID_ATTRIBUTE)
29870 break;
29872 if (token->type != CPP_COLON)
29873 selector = cp_parser_objc_selector (parser);
29875 /* Detect if we have a unary selector. */
29876 if (maybe_unary_selector_p
29877 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29879 params = selector; /* Might be followed by attributes. */
29880 break;
29883 maybe_unary_selector_p = false;
29884 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29886 /* Something went quite wrong. There should be a colon
29887 here, but there is not. Stop parsing parameters. */
29888 break;
29890 type_name = cp_parser_objc_typename (parser);
29891 /* New ObjC allows attributes on parameters too. */
29892 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29893 parm_attr = cp_parser_attributes_opt (parser);
29894 identifier = cp_parser_identifier (parser);
29896 params
29897 = chainon (params,
29898 objc_build_keyword_decl (selector,
29899 type_name,
29900 identifier,
29901 parm_attr));
29903 token = cp_lexer_peek_token (parser->lexer);
29906 if (params == NULL_TREE)
29908 cp_parser_error (parser, "objective-c++ method declaration is expected");
29909 return error_mark_node;
29912 /* We allow tail attributes for the method. */
29913 if (token->keyword == RID_ATTRIBUTE)
29915 *attributes = cp_parser_attributes_opt (parser);
29916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29917 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29918 return params;
29919 cp_parser_error (parser,
29920 "method attributes must be specified at the end");
29921 return error_mark_node;
29924 if (params == NULL_TREE)
29926 cp_parser_error (parser, "objective-c++ method declaration is expected");
29927 return error_mark_node;
29929 return params;
29932 /* Parse the non-keyword Objective-C params. */
29934 static tree
29935 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29936 tree* attributes)
29938 tree params = make_node (TREE_LIST);
29939 cp_token *token = cp_lexer_peek_token (parser->lexer);
29940 *ellipsisp = false; /* Initially, assume no ellipsis. */
29942 while (token->type == CPP_COMMA)
29944 cp_parameter_declarator *parmdecl;
29945 tree parm;
29947 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29948 token = cp_lexer_peek_token (parser->lexer);
29950 if (token->type == CPP_ELLIPSIS)
29952 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29953 *ellipsisp = true;
29954 token = cp_lexer_peek_token (parser->lexer);
29955 break;
29958 /* TODO: parse attributes for tail parameters. */
29959 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29960 parm = grokdeclarator (parmdecl->declarator,
29961 &parmdecl->decl_specifiers,
29962 PARM, /*initialized=*/0,
29963 /*attrlist=*/NULL);
29965 chainon (params, build_tree_list (NULL_TREE, parm));
29966 token = cp_lexer_peek_token (parser->lexer);
29969 /* We allow tail attributes for the method. */
29970 if (token->keyword == RID_ATTRIBUTE)
29972 if (*attributes == NULL_TREE)
29974 *attributes = cp_parser_attributes_opt (parser);
29975 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29976 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29977 return params;
29979 else
29980 /* We have an error, but parse the attributes, so that we can
29981 carry on. */
29982 *attributes = cp_parser_attributes_opt (parser);
29984 cp_parser_error (parser,
29985 "method attributes must be specified at the end");
29986 return error_mark_node;
29989 return params;
29992 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29994 static void
29995 cp_parser_objc_interstitial_code (cp_parser* parser)
29997 cp_token *token = cp_lexer_peek_token (parser->lexer);
29999 /* If the next token is `extern' and the following token is a string
30000 literal, then we have a linkage specification. */
30001 if (token->keyword == RID_EXTERN
30002 && cp_parser_is_pure_string_literal
30003 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30004 cp_parser_linkage_specification (parser);
30005 /* Handle #pragma, if any. */
30006 else if (token->type == CPP_PRAGMA)
30007 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30008 /* Allow stray semicolons. */
30009 else if (token->type == CPP_SEMICOLON)
30010 cp_lexer_consume_token (parser->lexer);
30011 /* Mark methods as optional or required, when building protocols. */
30012 else if (token->keyword == RID_AT_OPTIONAL)
30014 cp_lexer_consume_token (parser->lexer);
30015 objc_set_method_opt (true);
30017 else if (token->keyword == RID_AT_REQUIRED)
30019 cp_lexer_consume_token (parser->lexer);
30020 objc_set_method_opt (false);
30022 else if (token->keyword == RID_NAMESPACE)
30023 cp_parser_namespace_definition (parser);
30024 /* Other stray characters must generate errors. */
30025 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30027 cp_lexer_consume_token (parser->lexer);
30028 error ("stray %qs between Objective-C++ methods",
30029 token->type == CPP_OPEN_BRACE ? "{" : "}");
30031 /* Finally, try to parse a block-declaration, or a function-definition. */
30032 else
30033 cp_parser_block_declaration (parser, /*statement_p=*/false);
30036 /* Parse a method signature. */
30038 static tree
30039 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30041 tree rettype, kwdparms, optparms;
30042 bool ellipsis = false;
30043 bool is_class_method;
30045 is_class_method = cp_parser_objc_method_type (parser);
30046 rettype = cp_parser_objc_typename (parser);
30047 *attributes = NULL_TREE;
30048 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30049 if (kwdparms == error_mark_node)
30050 return error_mark_node;
30051 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30052 if (optparms == error_mark_node)
30053 return error_mark_node;
30055 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30058 static bool
30059 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30061 tree tattr;
30062 cp_lexer_save_tokens (parser->lexer);
30063 tattr = cp_parser_attributes_opt (parser);
30064 gcc_assert (tattr) ;
30066 /* If the attributes are followed by a method introducer, this is not allowed.
30067 Dump the attributes and flag the situation. */
30068 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30069 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30070 return true;
30072 /* Otherwise, the attributes introduce some interstitial code, possibly so
30073 rewind to allow that check. */
30074 cp_lexer_rollback_tokens (parser->lexer);
30075 return false;
30078 /* Parse an Objective-C method prototype list. */
30080 static void
30081 cp_parser_objc_method_prototype_list (cp_parser* parser)
30083 cp_token *token = cp_lexer_peek_token (parser->lexer);
30085 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30087 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30089 tree attributes, sig;
30090 bool is_class_method;
30091 if (token->type == CPP_PLUS)
30092 is_class_method = true;
30093 else
30094 is_class_method = false;
30095 sig = cp_parser_objc_method_signature (parser, &attributes);
30096 if (sig == error_mark_node)
30098 cp_parser_skip_to_end_of_block_or_statement (parser);
30099 token = cp_lexer_peek_token (parser->lexer);
30100 continue;
30102 objc_add_method_declaration (is_class_method, sig, attributes);
30103 cp_parser_consume_semicolon_at_end_of_statement (parser);
30105 else if (token->keyword == RID_AT_PROPERTY)
30106 cp_parser_objc_at_property_declaration (parser);
30107 else if (token->keyword == RID_ATTRIBUTE
30108 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30109 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30110 OPT_Wattributes,
30111 "prefix attributes are ignored for methods");
30112 else
30113 /* Allow for interspersed non-ObjC++ code. */
30114 cp_parser_objc_interstitial_code (parser);
30116 token = cp_lexer_peek_token (parser->lexer);
30119 if (token->type != CPP_EOF)
30120 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30121 else
30122 cp_parser_error (parser, "expected %<@end%>");
30124 objc_finish_interface ();
30127 /* Parse an Objective-C method definition list. */
30129 static void
30130 cp_parser_objc_method_definition_list (cp_parser* parser)
30132 cp_token *token = cp_lexer_peek_token (parser->lexer);
30134 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30136 tree meth;
30138 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30140 cp_token *ptk;
30141 tree sig, attribute;
30142 bool is_class_method;
30143 if (token->type == CPP_PLUS)
30144 is_class_method = true;
30145 else
30146 is_class_method = false;
30147 push_deferring_access_checks (dk_deferred);
30148 sig = cp_parser_objc_method_signature (parser, &attribute);
30149 if (sig == error_mark_node)
30151 cp_parser_skip_to_end_of_block_or_statement (parser);
30152 token = cp_lexer_peek_token (parser->lexer);
30153 continue;
30155 objc_start_method_definition (is_class_method, sig, attribute,
30156 NULL_TREE);
30158 /* For historical reasons, we accept an optional semicolon. */
30159 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30160 cp_lexer_consume_token (parser->lexer);
30162 ptk = cp_lexer_peek_token (parser->lexer);
30163 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30164 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30166 perform_deferred_access_checks (tf_warning_or_error);
30167 stop_deferring_access_checks ();
30168 meth = cp_parser_function_definition_after_declarator (parser,
30169 false);
30170 pop_deferring_access_checks ();
30171 objc_finish_method_definition (meth);
30174 /* The following case will be removed once @synthesize is
30175 completely implemented. */
30176 else if (token->keyword == RID_AT_PROPERTY)
30177 cp_parser_objc_at_property_declaration (parser);
30178 else if (token->keyword == RID_AT_SYNTHESIZE)
30179 cp_parser_objc_at_synthesize_declaration (parser);
30180 else if (token->keyword == RID_AT_DYNAMIC)
30181 cp_parser_objc_at_dynamic_declaration (parser);
30182 else if (token->keyword == RID_ATTRIBUTE
30183 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30184 warning_at (token->location, OPT_Wattributes,
30185 "prefix attributes are ignored for methods");
30186 else
30187 /* Allow for interspersed non-ObjC++ code. */
30188 cp_parser_objc_interstitial_code (parser);
30190 token = cp_lexer_peek_token (parser->lexer);
30193 if (token->type != CPP_EOF)
30194 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30195 else
30196 cp_parser_error (parser, "expected %<@end%>");
30198 objc_finish_implementation ();
30201 /* Parse Objective-C ivars. */
30203 static void
30204 cp_parser_objc_class_ivars (cp_parser* parser)
30206 cp_token *token = cp_lexer_peek_token (parser->lexer);
30208 if (token->type != CPP_OPEN_BRACE)
30209 return; /* No ivars specified. */
30211 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30212 token = cp_lexer_peek_token (parser->lexer);
30214 while (token->type != CPP_CLOSE_BRACE
30215 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30217 cp_decl_specifier_seq declspecs;
30218 int decl_class_or_enum_p;
30219 tree prefix_attributes;
30221 cp_parser_objc_visibility_spec (parser);
30223 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30224 break;
30226 cp_parser_decl_specifier_seq (parser,
30227 CP_PARSER_FLAGS_OPTIONAL,
30228 &declspecs,
30229 &decl_class_or_enum_p);
30231 /* auto, register, static, extern, mutable. */
30232 if (declspecs.storage_class != sc_none)
30234 cp_parser_error (parser, "invalid type for instance variable");
30235 declspecs.storage_class = sc_none;
30238 /* thread_local. */
30239 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30241 cp_parser_error (parser, "invalid type for instance variable");
30242 declspecs.locations[ds_thread] = 0;
30245 /* typedef. */
30246 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30248 cp_parser_error (parser, "invalid type for instance variable");
30249 declspecs.locations[ds_typedef] = 0;
30252 prefix_attributes = declspecs.attributes;
30253 declspecs.attributes = NULL_TREE;
30255 /* Keep going until we hit the `;' at the end of the
30256 declaration. */
30257 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30259 tree width = NULL_TREE, attributes, first_attribute, decl;
30260 cp_declarator *declarator = NULL;
30261 int ctor_dtor_or_conv_p;
30263 /* Check for a (possibly unnamed) bitfield declaration. */
30264 token = cp_lexer_peek_token (parser->lexer);
30265 if (token->type == CPP_COLON)
30266 goto eat_colon;
30268 if (token->type == CPP_NAME
30269 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30270 == CPP_COLON))
30272 /* Get the name of the bitfield. */
30273 declarator = make_id_declarator (NULL_TREE,
30274 cp_parser_identifier (parser),
30275 sfk_none);
30277 eat_colon:
30278 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30279 /* Get the width of the bitfield. */
30280 width
30281 = cp_parser_constant_expression (parser);
30283 else
30285 /* Parse the declarator. */
30286 declarator
30287 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30288 &ctor_dtor_or_conv_p,
30289 /*parenthesized_p=*/NULL,
30290 /*member_p=*/false,
30291 /*friend_p=*/false);
30294 /* Look for attributes that apply to the ivar. */
30295 attributes = cp_parser_attributes_opt (parser);
30296 /* Remember which attributes are prefix attributes and
30297 which are not. */
30298 first_attribute = attributes;
30299 /* Combine the attributes. */
30300 attributes = attr_chainon (prefix_attributes, attributes);
30302 if (width)
30303 /* Create the bitfield declaration. */
30304 decl = grokbitfield (declarator, &declspecs,
30305 width, NULL_TREE, attributes);
30306 else
30307 decl = grokfield (declarator, &declspecs,
30308 NULL_TREE, /*init_const_expr_p=*/false,
30309 NULL_TREE, attributes);
30311 /* Add the instance variable. */
30312 if (decl != error_mark_node && decl != NULL_TREE)
30313 objc_add_instance_variable (decl);
30315 /* Reset PREFIX_ATTRIBUTES. */
30316 if (attributes != error_mark_node)
30318 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30319 attributes = TREE_CHAIN (attributes);
30320 if (attributes)
30321 TREE_CHAIN (attributes) = NULL_TREE;
30324 token = cp_lexer_peek_token (parser->lexer);
30326 if (token->type == CPP_COMMA)
30328 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30329 continue;
30331 break;
30334 cp_parser_consume_semicolon_at_end_of_statement (parser);
30335 token = cp_lexer_peek_token (parser->lexer);
30338 if (token->keyword == RID_AT_END)
30339 cp_parser_error (parser, "expected %<}%>");
30341 /* Do not consume the RID_AT_END, so it will be read again as terminating
30342 the @interface of @implementation. */
30343 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30344 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30346 /* For historical reasons, we accept an optional semicolon. */
30347 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30348 cp_lexer_consume_token (parser->lexer);
30351 /* Parse an Objective-C protocol declaration. */
30353 static void
30354 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30356 tree proto, protorefs;
30357 cp_token *tok;
30359 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30362 tok = cp_lexer_peek_token (parser->lexer);
30363 error_at (tok->location, "identifier expected after %<@protocol%>");
30364 cp_parser_consume_semicolon_at_end_of_statement (parser);
30365 return;
30368 /* See if we have a forward declaration or a definition. */
30369 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30371 /* Try a forward declaration first. */
30372 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30374 while (true)
30376 tree id;
30378 id = cp_parser_identifier (parser);
30379 if (id == error_mark_node)
30380 break;
30382 objc_declare_protocol (id, attributes);
30384 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30385 cp_lexer_consume_token (parser->lexer);
30386 else
30387 break;
30389 cp_parser_consume_semicolon_at_end_of_statement (parser);
30392 /* Ok, we got a full-fledged definition (or at least should). */
30393 else
30395 proto = cp_parser_identifier (parser);
30396 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30397 objc_start_protocol (proto, protorefs, attributes);
30398 cp_parser_objc_method_prototype_list (parser);
30402 /* Parse an Objective-C superclass or category. */
30404 static void
30405 cp_parser_objc_superclass_or_category (cp_parser *parser,
30406 bool iface_p,
30407 tree *super,
30408 tree *categ, bool *is_class_extension)
30410 cp_token *next = cp_lexer_peek_token (parser->lexer);
30412 *super = *categ = NULL_TREE;
30413 *is_class_extension = false;
30414 if (next->type == CPP_COLON)
30416 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30417 *super = cp_parser_identifier (parser);
30419 else if (next->type == CPP_OPEN_PAREN)
30421 matching_parens parens;
30422 parens.consume_open (parser); /* Eat '('. */
30424 /* If there is no category name, and this is an @interface, we
30425 have a class extension. */
30426 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30428 *categ = NULL_TREE;
30429 *is_class_extension = true;
30431 else
30432 *categ = cp_parser_identifier (parser);
30434 parens.require_close (parser);
30438 /* Parse an Objective-C class interface. */
30440 static void
30441 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30443 tree name, super, categ, protos;
30444 bool is_class_extension;
30446 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30447 name = cp_parser_identifier (parser);
30448 if (name == error_mark_node)
30450 /* It's hard to recover because even if valid @interface stuff
30451 is to follow, we can't compile it (or validate it) if we
30452 don't even know which class it refers to. Let's assume this
30453 was a stray '@interface' token in the stream and skip it.
30455 return;
30457 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30458 &is_class_extension);
30459 protos = cp_parser_objc_protocol_refs_opt (parser);
30461 /* We have either a class or a category on our hands. */
30462 if (categ || is_class_extension)
30463 objc_start_category_interface (name, categ, protos, attributes);
30464 else
30466 objc_start_class_interface (name, super, protos, attributes);
30467 /* Handle instance variable declarations, if any. */
30468 cp_parser_objc_class_ivars (parser);
30469 objc_continue_interface ();
30472 cp_parser_objc_method_prototype_list (parser);
30475 /* Parse an Objective-C class implementation. */
30477 static void
30478 cp_parser_objc_class_implementation (cp_parser* parser)
30480 tree name, super, categ;
30481 bool is_class_extension;
30483 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30484 name = cp_parser_identifier (parser);
30485 if (name == error_mark_node)
30487 /* It's hard to recover because even if valid @implementation
30488 stuff is to follow, we can't compile it (or validate it) if
30489 we don't even know which class it refers to. Let's assume
30490 this was a stray '@implementation' token in the stream and
30491 skip it.
30493 return;
30495 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30496 &is_class_extension);
30498 /* We have either a class or a category on our hands. */
30499 if (categ)
30500 objc_start_category_implementation (name, categ);
30501 else
30503 objc_start_class_implementation (name, super);
30504 /* Handle instance variable declarations, if any. */
30505 cp_parser_objc_class_ivars (parser);
30506 objc_continue_implementation ();
30509 cp_parser_objc_method_definition_list (parser);
30512 /* Consume the @end token and finish off the implementation. */
30514 static void
30515 cp_parser_objc_end_implementation (cp_parser* parser)
30517 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30518 objc_finish_implementation ();
30521 /* Parse an Objective-C declaration. */
30523 static void
30524 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30526 /* Try to figure out what kind of declaration is present. */
30527 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30529 if (attributes)
30530 switch (kwd->keyword)
30532 case RID_AT_ALIAS:
30533 case RID_AT_CLASS:
30534 case RID_AT_END:
30535 error_at (kwd->location, "attributes may not be specified before"
30536 " the %<@%D%> Objective-C++ keyword",
30537 kwd->u.value);
30538 attributes = NULL;
30539 break;
30540 case RID_AT_IMPLEMENTATION:
30541 warning_at (kwd->location, OPT_Wattributes,
30542 "prefix attributes are ignored before %<@%D%>",
30543 kwd->u.value);
30544 attributes = NULL;
30545 default:
30546 break;
30549 switch (kwd->keyword)
30551 case RID_AT_ALIAS:
30552 cp_parser_objc_alias_declaration (parser);
30553 break;
30554 case RID_AT_CLASS:
30555 cp_parser_objc_class_declaration (parser);
30556 break;
30557 case RID_AT_PROTOCOL:
30558 cp_parser_objc_protocol_declaration (parser, attributes);
30559 break;
30560 case RID_AT_INTERFACE:
30561 cp_parser_objc_class_interface (parser, attributes);
30562 break;
30563 case RID_AT_IMPLEMENTATION:
30564 cp_parser_objc_class_implementation (parser);
30565 break;
30566 case RID_AT_END:
30567 cp_parser_objc_end_implementation (parser);
30568 break;
30569 default:
30570 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30571 kwd->u.value);
30572 cp_parser_skip_to_end_of_block_or_statement (parser);
30576 /* Parse an Objective-C try-catch-finally statement.
30578 objc-try-catch-finally-stmt:
30579 @try compound-statement objc-catch-clause-seq [opt]
30580 objc-finally-clause [opt]
30582 objc-catch-clause-seq:
30583 objc-catch-clause objc-catch-clause-seq [opt]
30585 objc-catch-clause:
30586 @catch ( objc-exception-declaration ) compound-statement
30588 objc-finally-clause:
30589 @finally compound-statement
30591 objc-exception-declaration:
30592 parameter-declaration
30593 '...'
30595 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30597 Returns NULL_TREE.
30599 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30600 for C. Keep them in sync. */
30602 static tree
30603 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30605 location_t location;
30606 tree stmt;
30608 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30609 location = cp_lexer_peek_token (parser->lexer)->location;
30610 objc_maybe_warn_exceptions (location);
30611 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30612 node, lest it get absorbed into the surrounding block. */
30613 stmt = push_stmt_list ();
30614 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30615 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30617 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30619 cp_parameter_declarator *parm;
30620 tree parameter_declaration = error_mark_node;
30621 bool seen_open_paren = false;
30622 matching_parens parens;
30624 cp_lexer_consume_token (parser->lexer);
30625 if (parens.require_open (parser))
30626 seen_open_paren = true;
30627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30629 /* We have "@catch (...)" (where the '...' are literally
30630 what is in the code). Skip the '...'.
30631 parameter_declaration is set to NULL_TREE, and
30632 objc_being_catch_clauses() knows that that means
30633 '...'. */
30634 cp_lexer_consume_token (parser->lexer);
30635 parameter_declaration = NULL_TREE;
30637 else
30639 /* We have "@catch (NSException *exception)" or something
30640 like that. Parse the parameter declaration. */
30641 parm = cp_parser_parameter_declaration (parser, false, NULL);
30642 if (parm == NULL)
30643 parameter_declaration = error_mark_node;
30644 else
30645 parameter_declaration = grokdeclarator (parm->declarator,
30646 &parm->decl_specifiers,
30647 PARM, /*initialized=*/0,
30648 /*attrlist=*/NULL);
30650 if (seen_open_paren)
30651 parens.require_close (parser);
30652 else
30654 /* If there was no open parenthesis, we are recovering from
30655 an error, and we are trying to figure out what mistake
30656 the user has made. */
30658 /* If there is an immediate closing parenthesis, the user
30659 probably forgot the opening one (ie, they typed "@catch
30660 NSException *e)". Parse the closing parenthesis and keep
30661 going. */
30662 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30663 cp_lexer_consume_token (parser->lexer);
30665 /* If these is no immediate closing parenthesis, the user
30666 probably doesn't know that parenthesis are required at
30667 all (ie, they typed "@catch NSException *e"). So, just
30668 forget about the closing parenthesis and keep going. */
30670 objc_begin_catch_clause (parameter_declaration);
30671 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30672 objc_finish_catch_clause ();
30674 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30676 cp_lexer_consume_token (parser->lexer);
30677 location = cp_lexer_peek_token (parser->lexer)->location;
30678 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30679 node, lest it get absorbed into the surrounding block. */
30680 stmt = push_stmt_list ();
30681 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30682 objc_build_finally_clause (location, pop_stmt_list (stmt));
30685 return objc_finish_try_stmt ();
30688 /* Parse an Objective-C synchronized statement.
30690 objc-synchronized-stmt:
30691 @synchronized ( expression ) compound-statement
30693 Returns NULL_TREE. */
30695 static tree
30696 cp_parser_objc_synchronized_statement (cp_parser *parser)
30698 location_t location;
30699 tree lock, stmt;
30701 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30703 location = cp_lexer_peek_token (parser->lexer)->location;
30704 objc_maybe_warn_exceptions (location);
30705 matching_parens parens;
30706 parens.require_open (parser);
30707 lock = cp_parser_expression (parser);
30708 parens.require_close (parser);
30710 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30711 node, lest it get absorbed into the surrounding block. */
30712 stmt = push_stmt_list ();
30713 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30715 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30718 /* Parse an Objective-C throw statement.
30720 objc-throw-stmt:
30721 @throw assignment-expression [opt] ;
30723 Returns a constructed '@throw' statement. */
30725 static tree
30726 cp_parser_objc_throw_statement (cp_parser *parser)
30728 tree expr = NULL_TREE;
30729 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30731 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30734 expr = cp_parser_expression (parser);
30736 cp_parser_consume_semicolon_at_end_of_statement (parser);
30738 return objc_build_throw_stmt (loc, expr);
30741 /* Parse an Objective-C statement. */
30743 static tree
30744 cp_parser_objc_statement (cp_parser * parser)
30746 /* Try to figure out what kind of declaration is present. */
30747 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30749 switch (kwd->keyword)
30751 case RID_AT_TRY:
30752 return cp_parser_objc_try_catch_finally_statement (parser);
30753 case RID_AT_SYNCHRONIZED:
30754 return cp_parser_objc_synchronized_statement (parser);
30755 case RID_AT_THROW:
30756 return cp_parser_objc_throw_statement (parser);
30757 default:
30758 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30759 kwd->u.value);
30760 cp_parser_skip_to_end_of_block_or_statement (parser);
30763 return error_mark_node;
30766 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30767 look ahead to see if an objc keyword follows the attributes. This
30768 is to detect the use of prefix attributes on ObjC @interface and
30769 @protocol. */
30771 static bool
30772 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30774 cp_lexer_save_tokens (parser->lexer);
30775 *attrib = cp_parser_attributes_opt (parser);
30776 gcc_assert (*attrib);
30777 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30779 cp_lexer_commit_tokens (parser->lexer);
30780 return true;
30782 cp_lexer_rollback_tokens (parser->lexer);
30783 return false;
30786 /* This routine is a minimal replacement for
30787 c_parser_struct_declaration () used when parsing the list of
30788 types/names or ObjC++ properties. For example, when parsing the
30789 code
30791 @property (readonly) int a, b, c;
30793 this function is responsible for parsing "int a, int b, int c" and
30794 returning the declarations as CHAIN of DECLs.
30796 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30797 similar parsing. */
30798 static tree
30799 cp_parser_objc_struct_declaration (cp_parser *parser)
30801 tree decls = NULL_TREE;
30802 cp_decl_specifier_seq declspecs;
30803 int decl_class_or_enum_p;
30804 tree prefix_attributes;
30806 cp_parser_decl_specifier_seq (parser,
30807 CP_PARSER_FLAGS_NONE,
30808 &declspecs,
30809 &decl_class_or_enum_p);
30811 if (declspecs.type == error_mark_node)
30812 return error_mark_node;
30814 /* auto, register, static, extern, mutable. */
30815 if (declspecs.storage_class != sc_none)
30817 cp_parser_error (parser, "invalid type for property");
30818 declspecs.storage_class = sc_none;
30821 /* thread_local. */
30822 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30824 cp_parser_error (parser, "invalid type for property");
30825 declspecs.locations[ds_thread] = 0;
30828 /* typedef. */
30829 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30831 cp_parser_error (parser, "invalid type for property");
30832 declspecs.locations[ds_typedef] = 0;
30835 prefix_attributes = declspecs.attributes;
30836 declspecs.attributes = NULL_TREE;
30838 /* Keep going until we hit the `;' at the end of the declaration. */
30839 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30841 tree attributes, first_attribute, decl;
30842 cp_declarator *declarator;
30843 cp_token *token;
30845 /* Parse the declarator. */
30846 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30847 NULL, NULL, false, false);
30849 /* Look for attributes that apply to the ivar. */
30850 attributes = cp_parser_attributes_opt (parser);
30851 /* Remember which attributes are prefix attributes and
30852 which are not. */
30853 first_attribute = attributes;
30854 /* Combine the attributes. */
30855 attributes = attr_chainon (prefix_attributes, attributes);
30857 decl = grokfield (declarator, &declspecs,
30858 NULL_TREE, /*init_const_expr_p=*/false,
30859 NULL_TREE, attributes);
30861 if (decl == error_mark_node || decl == NULL_TREE)
30862 return error_mark_node;
30864 /* Reset PREFIX_ATTRIBUTES. */
30865 if (attributes != error_mark_node)
30867 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30868 attributes = TREE_CHAIN (attributes);
30869 if (attributes)
30870 TREE_CHAIN (attributes) = NULL_TREE;
30873 DECL_CHAIN (decl) = decls;
30874 decls = decl;
30876 token = cp_lexer_peek_token (parser->lexer);
30877 if (token->type == CPP_COMMA)
30879 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30880 continue;
30882 else
30883 break;
30885 return decls;
30888 /* Parse an Objective-C @property declaration. The syntax is:
30890 objc-property-declaration:
30891 '@property' objc-property-attributes[opt] struct-declaration ;
30893 objc-property-attributes:
30894 '(' objc-property-attribute-list ')'
30896 objc-property-attribute-list:
30897 objc-property-attribute
30898 objc-property-attribute-list, objc-property-attribute
30900 objc-property-attribute
30901 'getter' = identifier
30902 'setter' = identifier
30903 'readonly'
30904 'readwrite'
30905 'assign'
30906 'retain'
30907 'copy'
30908 'nonatomic'
30910 For example:
30911 @property NSString *name;
30912 @property (readonly) id object;
30913 @property (retain, nonatomic, getter=getTheName) id name;
30914 @property int a, b, c;
30916 PS: This function is identical to
30917 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30918 static void
30919 cp_parser_objc_at_property_declaration (cp_parser *parser)
30921 /* The following variables hold the attributes of the properties as
30922 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30923 seen. When we see an attribute, we set them to 'true' (if they
30924 are boolean properties) or to the identifier (if they have an
30925 argument, ie, for getter and setter). Note that here we only
30926 parse the list of attributes, check the syntax and accumulate the
30927 attributes that we find. objc_add_property_declaration() will
30928 then process the information. */
30929 bool property_assign = false;
30930 bool property_copy = false;
30931 tree property_getter_ident = NULL_TREE;
30932 bool property_nonatomic = false;
30933 bool property_readonly = false;
30934 bool property_readwrite = false;
30935 bool property_retain = false;
30936 tree property_setter_ident = NULL_TREE;
30938 /* 'properties' is the list of properties that we read. Usually a
30939 single one, but maybe more (eg, in "@property int a, b, c;" there
30940 are three). */
30941 tree properties;
30942 location_t loc;
30944 loc = cp_lexer_peek_token (parser->lexer)->location;
30946 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30948 /* Parse the optional attribute list... */
30949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30951 /* Eat the '('. */
30952 matching_parens parens;
30953 parens.consume_open (parser);
30955 while (true)
30957 bool syntax_error = false;
30958 cp_token *token = cp_lexer_peek_token (parser->lexer);
30959 enum rid keyword;
30961 if (token->type != CPP_NAME)
30963 cp_parser_error (parser, "expected identifier");
30964 break;
30966 keyword = C_RID_CODE (token->u.value);
30967 cp_lexer_consume_token (parser->lexer);
30968 switch (keyword)
30970 case RID_ASSIGN: property_assign = true; break;
30971 case RID_COPY: property_copy = true; break;
30972 case RID_NONATOMIC: property_nonatomic = true; break;
30973 case RID_READONLY: property_readonly = true; break;
30974 case RID_READWRITE: property_readwrite = true; break;
30975 case RID_RETAIN: property_retain = true; break;
30977 case RID_GETTER:
30978 case RID_SETTER:
30979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30981 if (keyword == RID_GETTER)
30982 cp_parser_error (parser,
30983 "missing %<=%> (after %<getter%> attribute)");
30984 else
30985 cp_parser_error (parser,
30986 "missing %<=%> (after %<setter%> attribute)");
30987 syntax_error = true;
30988 break;
30990 cp_lexer_consume_token (parser->lexer); /* eat the = */
30991 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30993 cp_parser_error (parser, "expected identifier");
30994 syntax_error = true;
30995 break;
30997 if (keyword == RID_SETTER)
30999 if (property_setter_ident != NULL_TREE)
31001 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31002 cp_lexer_consume_token (parser->lexer);
31004 else
31005 property_setter_ident = cp_parser_objc_selector (parser);
31006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31007 cp_parser_error (parser, "setter name must terminate with %<:%>");
31008 else
31009 cp_lexer_consume_token (parser->lexer);
31011 else
31013 if (property_getter_ident != NULL_TREE)
31015 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31016 cp_lexer_consume_token (parser->lexer);
31018 else
31019 property_getter_ident = cp_parser_objc_selector (parser);
31021 break;
31022 default:
31023 cp_parser_error (parser, "unknown property attribute");
31024 syntax_error = true;
31025 break;
31028 if (syntax_error)
31029 break;
31031 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31032 cp_lexer_consume_token (parser->lexer);
31033 else
31034 break;
31037 /* FIXME: "@property (setter, assign);" will generate a spurious
31038 "error: expected ‘)’ before ‘,’ token". This is because
31039 cp_parser_require, unlike the C counterpart, will produce an
31040 error even if we are in error recovery. */
31041 if (!parens.require_close (parser))
31043 cp_parser_skip_to_closing_parenthesis (parser,
31044 /*recovering=*/true,
31045 /*or_comma=*/false,
31046 /*consume_paren=*/true);
31050 /* ... and the property declaration(s). */
31051 properties = cp_parser_objc_struct_declaration (parser);
31053 if (properties == error_mark_node)
31055 cp_parser_skip_to_end_of_statement (parser);
31056 /* If the next token is now a `;', consume it. */
31057 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31058 cp_lexer_consume_token (parser->lexer);
31059 return;
31062 if (properties == NULL_TREE)
31063 cp_parser_error (parser, "expected identifier");
31064 else
31066 /* Comma-separated properties are chained together in
31067 reverse order; add them one by one. */
31068 properties = nreverse (properties);
31070 for (; properties; properties = TREE_CHAIN (properties))
31071 objc_add_property_declaration (loc, copy_node (properties),
31072 property_readonly, property_readwrite,
31073 property_assign, property_retain,
31074 property_copy, property_nonatomic,
31075 property_getter_ident, property_setter_ident);
31078 cp_parser_consume_semicolon_at_end_of_statement (parser);
31081 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31083 objc-synthesize-declaration:
31084 @synthesize objc-synthesize-identifier-list ;
31086 objc-synthesize-identifier-list:
31087 objc-synthesize-identifier
31088 objc-synthesize-identifier-list, objc-synthesize-identifier
31090 objc-synthesize-identifier
31091 identifier
31092 identifier = identifier
31094 For example:
31095 @synthesize MyProperty;
31096 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31098 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31099 for C. Keep them in sync.
31101 static void
31102 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31104 tree list = NULL_TREE;
31105 location_t loc;
31106 loc = cp_lexer_peek_token (parser->lexer)->location;
31108 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31109 while (true)
31111 tree property, ivar;
31112 property = cp_parser_identifier (parser);
31113 if (property == error_mark_node)
31115 cp_parser_consume_semicolon_at_end_of_statement (parser);
31116 return;
31118 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31120 cp_lexer_consume_token (parser->lexer);
31121 ivar = cp_parser_identifier (parser);
31122 if (ivar == error_mark_node)
31124 cp_parser_consume_semicolon_at_end_of_statement (parser);
31125 return;
31128 else
31129 ivar = NULL_TREE;
31130 list = chainon (list, build_tree_list (ivar, property));
31131 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31132 cp_lexer_consume_token (parser->lexer);
31133 else
31134 break;
31136 cp_parser_consume_semicolon_at_end_of_statement (parser);
31137 objc_add_synthesize_declaration (loc, list);
31140 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31142 objc-dynamic-declaration:
31143 @dynamic identifier-list ;
31145 For example:
31146 @dynamic MyProperty;
31147 @dynamic MyProperty, AnotherProperty;
31149 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31150 for C. Keep them in sync.
31152 static void
31153 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31155 tree list = NULL_TREE;
31156 location_t loc;
31157 loc = cp_lexer_peek_token (parser->lexer)->location;
31159 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31160 while (true)
31162 tree property;
31163 property = cp_parser_identifier (parser);
31164 if (property == error_mark_node)
31166 cp_parser_consume_semicolon_at_end_of_statement (parser);
31167 return;
31169 list = chainon (list, build_tree_list (NULL, property));
31170 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31171 cp_lexer_consume_token (parser->lexer);
31172 else
31173 break;
31175 cp_parser_consume_semicolon_at_end_of_statement (parser);
31176 objc_add_dynamic_declaration (loc, list);
31180 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31182 /* Returns name of the next clause.
31183 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31184 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31185 returned and the token is consumed. */
31187 static pragma_omp_clause
31188 cp_parser_omp_clause_name (cp_parser *parser)
31190 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31193 result = PRAGMA_OACC_CLAUSE_AUTO;
31194 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31195 result = PRAGMA_OMP_CLAUSE_IF;
31196 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31197 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31198 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31199 result = PRAGMA_OACC_CLAUSE_DELETE;
31200 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31201 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31202 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31203 result = PRAGMA_OMP_CLAUSE_FOR;
31204 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31206 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31207 const char *p = IDENTIFIER_POINTER (id);
31209 switch (p[0])
31211 case 'a':
31212 if (!strcmp ("aligned", p))
31213 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31214 else if (!strcmp ("async", p))
31215 result = PRAGMA_OACC_CLAUSE_ASYNC;
31216 break;
31217 case 'c':
31218 if (!strcmp ("collapse", p))
31219 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31220 else if (!strcmp ("copy", p))
31221 result = PRAGMA_OACC_CLAUSE_COPY;
31222 else if (!strcmp ("copyin", p))
31223 result = PRAGMA_OMP_CLAUSE_COPYIN;
31224 else if (!strcmp ("copyout", p))
31225 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31226 else if (!strcmp ("copyprivate", p))
31227 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31228 else if (!strcmp ("create", p))
31229 result = PRAGMA_OACC_CLAUSE_CREATE;
31230 break;
31231 case 'd':
31232 if (!strcmp ("defaultmap", p))
31233 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31234 else if (!strcmp ("depend", p))
31235 result = PRAGMA_OMP_CLAUSE_DEPEND;
31236 else if (!strcmp ("device", p))
31237 result = PRAGMA_OMP_CLAUSE_DEVICE;
31238 else if (!strcmp ("deviceptr", p))
31239 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31240 else if (!strcmp ("device_resident", p))
31241 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31242 else if (!strcmp ("dist_schedule", p))
31243 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31244 break;
31245 case 'f':
31246 if (!strcmp ("final", p))
31247 result = PRAGMA_OMP_CLAUSE_FINAL;
31248 else if (!strcmp ("firstprivate", p))
31249 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31250 else if (!strcmp ("from", p))
31251 result = PRAGMA_OMP_CLAUSE_FROM;
31252 break;
31253 case 'g':
31254 if (!strcmp ("gang", p))
31255 result = PRAGMA_OACC_CLAUSE_GANG;
31256 else if (!strcmp ("grainsize", p))
31257 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31258 break;
31259 case 'h':
31260 if (!strcmp ("hint", p))
31261 result = PRAGMA_OMP_CLAUSE_HINT;
31262 else if (!strcmp ("host", p))
31263 result = PRAGMA_OACC_CLAUSE_HOST;
31264 break;
31265 case 'i':
31266 if (!strcmp ("inbranch", p))
31267 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31268 else if (!strcmp ("independent", p))
31269 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31270 else if (!strcmp ("is_device_ptr", p))
31271 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31272 break;
31273 case 'l':
31274 if (!strcmp ("lastprivate", p))
31275 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31276 else if (!strcmp ("linear", p))
31277 result = PRAGMA_OMP_CLAUSE_LINEAR;
31278 else if (!strcmp ("link", p))
31279 result = PRAGMA_OMP_CLAUSE_LINK;
31280 break;
31281 case 'm':
31282 if (!strcmp ("map", p))
31283 result = PRAGMA_OMP_CLAUSE_MAP;
31284 else if (!strcmp ("mergeable", p))
31285 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31286 break;
31287 case 'n':
31288 if (!strcmp ("nogroup", p))
31289 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31290 else if (!strcmp ("notinbranch", p))
31291 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31292 else if (!strcmp ("nowait", p))
31293 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31294 else if (!strcmp ("num_gangs", p))
31295 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31296 else if (!strcmp ("num_tasks", p))
31297 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31298 else if (!strcmp ("num_teams", p))
31299 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31300 else if (!strcmp ("num_threads", p))
31301 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31302 else if (!strcmp ("num_workers", p))
31303 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31304 break;
31305 case 'o':
31306 if (!strcmp ("ordered", p))
31307 result = PRAGMA_OMP_CLAUSE_ORDERED;
31308 break;
31309 case 'p':
31310 if (!strcmp ("parallel", p))
31311 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31312 else if (!strcmp ("present", p))
31313 result = PRAGMA_OACC_CLAUSE_PRESENT;
31314 else if (!strcmp ("present_or_copy", p)
31315 || !strcmp ("pcopy", p))
31316 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31317 else if (!strcmp ("present_or_copyin", p)
31318 || !strcmp ("pcopyin", p))
31319 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31320 else if (!strcmp ("present_or_copyout", p)
31321 || !strcmp ("pcopyout", p))
31322 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31323 else if (!strcmp ("present_or_create", p)
31324 || !strcmp ("pcreate", p))
31325 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31326 else if (!strcmp ("priority", p))
31327 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31328 else if (!strcmp ("proc_bind", p))
31329 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31330 break;
31331 case 'r':
31332 if (!strcmp ("reduction", p))
31333 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31334 break;
31335 case 's':
31336 if (!strcmp ("safelen", p))
31337 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31338 else if (!strcmp ("schedule", p))
31339 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31340 else if (!strcmp ("sections", p))
31341 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31342 else if (!strcmp ("self", p))
31343 result = PRAGMA_OACC_CLAUSE_SELF;
31344 else if (!strcmp ("seq", p))
31345 result = PRAGMA_OACC_CLAUSE_SEQ;
31346 else if (!strcmp ("shared", p))
31347 result = PRAGMA_OMP_CLAUSE_SHARED;
31348 else if (!strcmp ("simd", p))
31349 result = PRAGMA_OMP_CLAUSE_SIMD;
31350 else if (!strcmp ("simdlen", p))
31351 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31352 break;
31353 case 't':
31354 if (!strcmp ("taskgroup", p))
31355 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31356 else if (!strcmp ("thread_limit", p))
31357 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31358 else if (!strcmp ("threads", p))
31359 result = PRAGMA_OMP_CLAUSE_THREADS;
31360 else if (!strcmp ("tile", p))
31361 result = PRAGMA_OACC_CLAUSE_TILE;
31362 else if (!strcmp ("to", p))
31363 result = PRAGMA_OMP_CLAUSE_TO;
31364 break;
31365 case 'u':
31366 if (!strcmp ("uniform", p))
31367 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31368 else if (!strcmp ("untied", p))
31369 result = PRAGMA_OMP_CLAUSE_UNTIED;
31370 else if (!strcmp ("use_device", p))
31371 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31372 else if (!strcmp ("use_device_ptr", p))
31373 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31374 break;
31375 case 'v':
31376 if (!strcmp ("vector", p))
31377 result = PRAGMA_OACC_CLAUSE_VECTOR;
31378 else if (!strcmp ("vector_length", p))
31379 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31380 break;
31381 case 'w':
31382 if (!strcmp ("wait", p))
31383 result = PRAGMA_OACC_CLAUSE_WAIT;
31384 else if (!strcmp ("worker", p))
31385 result = PRAGMA_OACC_CLAUSE_WORKER;
31386 break;
31390 if (result != PRAGMA_OMP_CLAUSE_NONE)
31391 cp_lexer_consume_token (parser->lexer);
31393 return result;
31396 /* Validate that a clause of the given type does not already exist. */
31398 static void
31399 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31400 const char *name, location_t location)
31402 tree c;
31404 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31405 if (OMP_CLAUSE_CODE (c) == code)
31407 error_at (location, "too many %qs clauses", name);
31408 break;
31412 /* OpenMP 2.5:
31413 variable-list:
31414 identifier
31415 variable-list , identifier
31417 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31418 colon). An opening parenthesis will have been consumed by the caller.
31420 If KIND is nonzero, create the appropriate node and install the decl
31421 in OMP_CLAUSE_DECL and add the node to the head of the list.
31423 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31424 return the list created.
31426 COLON can be NULL if only closing parenthesis should end the list,
31427 or pointer to bool which will receive false if the list is terminated
31428 by closing parenthesis or true if the list is terminated by colon. */
31430 static tree
31431 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31432 tree list, bool *colon)
31434 cp_token *token;
31435 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31436 if (colon)
31438 parser->colon_corrects_to_scope_p = false;
31439 *colon = false;
31441 while (1)
31443 tree name, decl;
31445 token = cp_lexer_peek_token (parser->lexer);
31446 if (kind != 0
31447 && current_class_ptr
31448 && cp_parser_is_keyword (token, RID_THIS))
31450 decl = finish_this_expr ();
31451 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31452 || CONVERT_EXPR_P (decl))
31453 decl = TREE_OPERAND (decl, 0);
31454 cp_lexer_consume_token (parser->lexer);
31456 else
31458 name = cp_parser_id_expression (parser, /*template_p=*/false,
31459 /*check_dependency_p=*/true,
31460 /*template_p=*/NULL,
31461 /*declarator_p=*/false,
31462 /*optional_p=*/false);
31463 if (name == error_mark_node)
31464 goto skip_comma;
31466 if (identifier_p (name))
31467 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31468 else
31469 decl = name;
31470 if (decl == error_mark_node)
31471 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31472 token->location);
31474 if (decl == error_mark_node)
31476 else if (kind != 0)
31478 switch (kind)
31480 case OMP_CLAUSE__CACHE_:
31481 /* The OpenACC cache directive explicitly only allows "array
31482 elements or subarrays". */
31483 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31485 error_at (token->location, "expected %<[%>");
31486 decl = error_mark_node;
31487 break;
31489 /* FALLTHROUGH. */
31490 case OMP_CLAUSE_MAP:
31491 case OMP_CLAUSE_FROM:
31492 case OMP_CLAUSE_TO:
31493 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31495 location_t loc
31496 = cp_lexer_peek_token (parser->lexer)->location;
31497 cp_id_kind idk = CP_ID_KIND_NONE;
31498 cp_lexer_consume_token (parser->lexer);
31499 decl = convert_from_reference (decl);
31500 decl
31501 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31502 decl, false,
31503 &idk, loc);
31505 /* FALLTHROUGH. */
31506 case OMP_CLAUSE_DEPEND:
31507 case OMP_CLAUSE_REDUCTION:
31508 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31510 tree low_bound = NULL_TREE, length = NULL_TREE;
31512 parser->colon_corrects_to_scope_p = false;
31513 cp_lexer_consume_token (parser->lexer);
31514 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31515 low_bound = cp_parser_expression (parser);
31516 if (!colon)
31517 parser->colon_corrects_to_scope_p
31518 = saved_colon_corrects_to_scope_p;
31519 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31520 length = integer_one_node;
31521 else
31523 /* Look for `:'. */
31524 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31525 goto skip_comma;
31526 if (!cp_lexer_next_token_is (parser->lexer,
31527 CPP_CLOSE_SQUARE))
31528 length = cp_parser_expression (parser);
31530 /* Look for the closing `]'. */
31531 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31532 RT_CLOSE_SQUARE))
31533 goto skip_comma;
31535 decl = tree_cons (low_bound, length, decl);
31537 break;
31538 default:
31539 break;
31542 tree u = build_omp_clause (token->location, kind);
31543 OMP_CLAUSE_DECL (u) = decl;
31544 OMP_CLAUSE_CHAIN (u) = list;
31545 list = u;
31547 else
31548 list = tree_cons (decl, NULL_TREE, list);
31550 get_comma:
31551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31552 break;
31553 cp_lexer_consume_token (parser->lexer);
31556 if (colon)
31557 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31559 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31561 *colon = true;
31562 cp_parser_require (parser, CPP_COLON, RT_COLON);
31563 return list;
31566 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31568 int ending;
31570 /* Try to resync to an unnested comma. Copied from
31571 cp_parser_parenthesized_expression_list. */
31572 skip_comma:
31573 if (colon)
31574 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31575 ending = cp_parser_skip_to_closing_parenthesis (parser,
31576 /*recovering=*/true,
31577 /*or_comma=*/true,
31578 /*consume_paren=*/true);
31579 if (ending < 0)
31580 goto get_comma;
31583 return list;
31586 /* Similarly, but expect leading and trailing parenthesis. This is a very
31587 common case for omp clauses. */
31589 static tree
31590 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31592 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31593 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31594 return list;
31597 /* OpenACC 2.0:
31598 copy ( variable-list )
31599 copyin ( variable-list )
31600 copyout ( variable-list )
31601 create ( variable-list )
31602 delete ( variable-list )
31603 present ( variable-list )
31604 present_or_copy ( variable-list )
31605 pcopy ( variable-list )
31606 present_or_copyin ( variable-list )
31607 pcopyin ( variable-list )
31608 present_or_copyout ( variable-list )
31609 pcopyout ( variable-list )
31610 present_or_create ( variable-list )
31611 pcreate ( variable-list ) */
31613 static tree
31614 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31615 tree list)
31617 enum gomp_map_kind kind;
31618 switch (c_kind)
31620 case PRAGMA_OACC_CLAUSE_COPY:
31621 kind = GOMP_MAP_FORCE_TOFROM;
31622 break;
31623 case PRAGMA_OACC_CLAUSE_COPYIN:
31624 kind = GOMP_MAP_FORCE_TO;
31625 break;
31626 case PRAGMA_OACC_CLAUSE_COPYOUT:
31627 kind = GOMP_MAP_FORCE_FROM;
31628 break;
31629 case PRAGMA_OACC_CLAUSE_CREATE:
31630 kind = GOMP_MAP_FORCE_ALLOC;
31631 break;
31632 case PRAGMA_OACC_CLAUSE_DELETE:
31633 kind = GOMP_MAP_DELETE;
31634 break;
31635 case PRAGMA_OACC_CLAUSE_DEVICE:
31636 kind = GOMP_MAP_FORCE_TO;
31637 break;
31638 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31639 kind = GOMP_MAP_DEVICE_RESIDENT;
31640 break;
31641 case PRAGMA_OACC_CLAUSE_HOST:
31642 case PRAGMA_OACC_CLAUSE_SELF:
31643 kind = GOMP_MAP_FORCE_FROM;
31644 break;
31645 case PRAGMA_OACC_CLAUSE_LINK:
31646 kind = GOMP_MAP_LINK;
31647 break;
31648 case PRAGMA_OACC_CLAUSE_PRESENT:
31649 kind = GOMP_MAP_FORCE_PRESENT;
31650 break;
31651 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31652 kind = GOMP_MAP_TOFROM;
31653 break;
31654 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31655 kind = GOMP_MAP_TO;
31656 break;
31657 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31658 kind = GOMP_MAP_FROM;
31659 break;
31660 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31661 kind = GOMP_MAP_ALLOC;
31662 break;
31663 default:
31664 gcc_unreachable ();
31666 tree nl, c;
31667 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31669 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31670 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31672 return nl;
31675 /* OpenACC 2.0:
31676 deviceptr ( variable-list ) */
31678 static tree
31679 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31681 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31682 tree vars, t;
31684 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31685 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31686 variable-list must only allow for pointer variables. */
31687 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31688 for (t = vars; t; t = TREE_CHAIN (t))
31690 tree v = TREE_PURPOSE (t);
31691 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31692 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31693 OMP_CLAUSE_DECL (u) = v;
31694 OMP_CLAUSE_CHAIN (u) = list;
31695 list = u;
31698 return list;
31701 /* OpenACC 2.0:
31702 auto
31703 independent
31704 nohost
31705 seq */
31707 static tree
31708 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31709 enum omp_clause_code code,
31710 tree list, location_t location)
31712 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31713 tree c = build_omp_clause (location, code);
31714 OMP_CLAUSE_CHAIN (c) = list;
31715 return c;
31718 /* OpenACC:
31719 num_gangs ( expression )
31720 num_workers ( expression )
31721 vector_length ( expression ) */
31723 static tree
31724 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31725 const char *str, tree list)
31727 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31729 matching_parens parens;
31730 if (!parens.require_open (parser))
31731 return list;
31733 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31735 if (t == error_mark_node
31736 || !parens.require_close (parser))
31738 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31739 /*or_comma=*/false,
31740 /*consume_paren=*/true);
31741 return list;
31744 check_no_duplicate_clause (list, code, str, loc);
31746 tree c = build_omp_clause (loc, code);
31747 OMP_CLAUSE_OPERAND (c, 0) = t;
31748 OMP_CLAUSE_CHAIN (c) = list;
31749 return c;
31752 /* OpenACC:
31754 gang [( gang-arg-list )]
31755 worker [( [num:] int-expr )]
31756 vector [( [length:] int-expr )]
31758 where gang-arg is one of:
31760 [num:] int-expr
31761 static: size-expr
31763 and size-expr may be:
31766 int-expr
31769 static tree
31770 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31771 const char *str, tree list)
31773 const char *id = "num";
31774 cp_lexer *lexer = parser->lexer;
31775 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31776 location_t loc = cp_lexer_peek_token (lexer)->location;
31778 if (kind == OMP_CLAUSE_VECTOR)
31779 id = "length";
31781 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31783 matching_parens parens;
31784 parens.consume_open (parser);
31788 cp_token *next = cp_lexer_peek_token (lexer);
31789 int idx = 0;
31791 /* Gang static argument. */
31792 if (kind == OMP_CLAUSE_GANG
31793 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31795 cp_lexer_consume_token (lexer);
31797 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31798 goto cleanup_error;
31800 idx = 1;
31801 if (ops[idx] != NULL)
31803 cp_parser_error (parser, "too many %<static%> arguments");
31804 goto cleanup_error;
31807 /* Check for the '*' argument. */
31808 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31809 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31810 || cp_lexer_nth_token_is (parser->lexer, 2,
31811 CPP_CLOSE_PAREN)))
31813 cp_lexer_consume_token (lexer);
31814 ops[idx] = integer_minus_one_node;
31816 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31818 cp_lexer_consume_token (lexer);
31819 continue;
31821 else break;
31824 /* Worker num: argument and vector length: arguments. */
31825 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31826 && id_equal (next->u.value, id)
31827 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31829 cp_lexer_consume_token (lexer); /* id */
31830 cp_lexer_consume_token (lexer); /* ':' */
31833 /* Now collect the actual argument. */
31834 if (ops[idx] != NULL_TREE)
31836 cp_parser_error (parser, "unexpected argument");
31837 goto cleanup_error;
31840 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31841 false);
31842 if (expr == error_mark_node)
31843 goto cleanup_error;
31845 mark_exp_read (expr);
31846 ops[idx] = expr;
31848 if (kind == OMP_CLAUSE_GANG
31849 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31851 cp_lexer_consume_token (lexer);
31852 continue;
31854 break;
31856 while (1);
31858 if (!parens.require_close (parser))
31859 goto cleanup_error;
31862 check_no_duplicate_clause (list, kind, str, loc);
31864 c = build_omp_clause (loc, kind);
31866 if (ops[1])
31867 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31869 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31870 OMP_CLAUSE_CHAIN (c) = list;
31872 return c;
31874 cleanup_error:
31875 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31876 return list;
31879 /* OpenACC 2.0:
31880 tile ( size-expr-list ) */
31882 static tree
31883 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31885 tree c, expr = error_mark_node;
31886 tree tile = NULL_TREE;
31888 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31889 so, but the spec authors never considered such a case and have
31890 differing opinions on what it might mean, including 'not
31891 allowed'.) */
31892 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31893 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31894 clause_loc);
31896 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31897 return list;
31901 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31902 return list;
31904 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31905 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31906 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31908 cp_lexer_consume_token (parser->lexer);
31909 expr = integer_zero_node;
31911 else
31912 expr = cp_parser_constant_expression (parser);
31914 tile = tree_cons (NULL_TREE, expr, tile);
31916 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31918 /* Consume the trailing ')'. */
31919 cp_lexer_consume_token (parser->lexer);
31921 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31922 tile = nreverse (tile);
31923 OMP_CLAUSE_TILE_LIST (c) = tile;
31924 OMP_CLAUSE_CHAIN (c) = list;
31925 return c;
31928 /* OpenACC 2.0
31929 Parse wait clause or directive parameters. */
31931 static tree
31932 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31934 vec<tree, va_gc> *args;
31935 tree t, args_tree;
31937 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31938 /*cast_p=*/false,
31939 /*allow_expansion_p=*/true,
31940 /*non_constant_p=*/NULL);
31942 if (args == NULL || args->length () == 0)
31944 cp_parser_error (parser, "expected integer expression before ')'");
31945 if (args != NULL)
31946 release_tree_vector (args);
31947 return list;
31950 args_tree = build_tree_list_vec (args);
31952 release_tree_vector (args);
31954 for (t = args_tree; t; t = TREE_CHAIN (t))
31956 tree targ = TREE_VALUE (t);
31958 if (targ != error_mark_node)
31960 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31961 error ("%<wait%> expression must be integral");
31962 else
31964 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31966 targ = mark_rvalue_use (targ);
31967 OMP_CLAUSE_DECL (c) = targ;
31968 OMP_CLAUSE_CHAIN (c) = list;
31969 list = c;
31974 return list;
31977 /* OpenACC:
31978 wait ( int-expr-list ) */
31980 static tree
31981 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31983 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31985 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31986 return list;
31988 list = cp_parser_oacc_wait_list (parser, location, list);
31990 return list;
31993 /* OpenMP 3.0:
31994 collapse ( constant-expression ) */
31996 static tree
31997 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31999 tree c, num;
32000 location_t loc;
32001 HOST_WIDE_INT n;
32003 loc = cp_lexer_peek_token (parser->lexer)->location;
32004 matching_parens parens;
32005 if (!parens.require_open (parser))
32006 return list;
32008 num = cp_parser_constant_expression (parser);
32010 if (!parens.require_close (parser))
32011 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32012 /*or_comma=*/false,
32013 /*consume_paren=*/true);
32015 if (num == error_mark_node)
32016 return list;
32017 num = fold_non_dependent_expr (num);
32018 if (!tree_fits_shwi_p (num)
32019 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32020 || (n = tree_to_shwi (num)) <= 0
32021 || (int) n != n)
32023 error_at (loc, "collapse argument needs positive constant integer expression");
32024 return list;
32027 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32028 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32029 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32030 OMP_CLAUSE_CHAIN (c) = list;
32031 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32033 return c;
32036 /* OpenMP 2.5:
32037 default ( none | shared )
32039 OpenACC:
32040 default ( none | present ) */
32042 static tree
32043 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32044 location_t location, bool is_oacc)
32046 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32047 tree c;
32049 matching_parens parens;
32050 if (!parens.require_open (parser))
32051 return list;
32052 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32054 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32055 const char *p = IDENTIFIER_POINTER (id);
32057 switch (p[0])
32059 case 'n':
32060 if (strcmp ("none", p) != 0)
32061 goto invalid_kind;
32062 kind = OMP_CLAUSE_DEFAULT_NONE;
32063 break;
32065 case 'p':
32066 if (strcmp ("present", p) != 0 || !is_oacc)
32067 goto invalid_kind;
32068 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32069 break;
32071 case 's':
32072 if (strcmp ("shared", p) != 0 || is_oacc)
32073 goto invalid_kind;
32074 kind = OMP_CLAUSE_DEFAULT_SHARED;
32075 break;
32077 default:
32078 goto invalid_kind;
32081 cp_lexer_consume_token (parser->lexer);
32083 else
32085 invalid_kind:
32086 if (is_oacc)
32087 cp_parser_error (parser, "expected %<none%> or %<present%>");
32088 else
32089 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32092 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32093 || !parens.require_close (parser))
32094 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32095 /*or_comma=*/false,
32096 /*consume_paren=*/true);
32098 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32099 return list;
32101 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32102 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32103 OMP_CLAUSE_CHAIN (c) = list;
32104 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32106 return c;
32109 /* OpenMP 3.1:
32110 final ( expression ) */
32112 static tree
32113 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32115 tree t, c;
32117 matching_parens parens;
32118 if (!parens.require_open (parser))
32119 return list;
32121 t = cp_parser_condition (parser);
32123 if (t == error_mark_node
32124 || !parens.require_close (parser))
32125 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32126 /*or_comma=*/false,
32127 /*consume_paren=*/true);
32129 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32131 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32132 OMP_CLAUSE_FINAL_EXPR (c) = t;
32133 OMP_CLAUSE_CHAIN (c) = list;
32135 return c;
32138 /* OpenMP 2.5:
32139 if ( expression )
32141 OpenMP 4.5:
32142 if ( directive-name-modifier : expression )
32144 directive-name-modifier:
32145 parallel | task | taskloop | target data | target | target update
32146 | target enter data | target exit data */
32148 static tree
32149 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32150 bool is_omp)
32152 tree t, c;
32153 enum tree_code if_modifier = ERROR_MARK;
32155 matching_parens parens;
32156 if (!parens.require_open (parser))
32157 return list;
32159 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32161 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32162 const char *p = IDENTIFIER_POINTER (id);
32163 int n = 2;
32165 if (strcmp ("parallel", p) == 0)
32166 if_modifier = OMP_PARALLEL;
32167 else if (strcmp ("task", p) == 0)
32168 if_modifier = OMP_TASK;
32169 else if (strcmp ("taskloop", p) == 0)
32170 if_modifier = OMP_TASKLOOP;
32171 else if (strcmp ("target", p) == 0)
32173 if_modifier = OMP_TARGET;
32174 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32176 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32177 p = IDENTIFIER_POINTER (id);
32178 if (strcmp ("data", p) == 0)
32179 if_modifier = OMP_TARGET_DATA;
32180 else if (strcmp ("update", p) == 0)
32181 if_modifier = OMP_TARGET_UPDATE;
32182 else if (strcmp ("enter", p) == 0)
32183 if_modifier = OMP_TARGET_ENTER_DATA;
32184 else if (strcmp ("exit", p) == 0)
32185 if_modifier = OMP_TARGET_EXIT_DATA;
32186 if (if_modifier != OMP_TARGET)
32187 n = 3;
32188 else
32190 location_t loc
32191 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32192 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32193 "or %<exit%>");
32194 if_modifier = ERROR_MARK;
32196 if (if_modifier == OMP_TARGET_ENTER_DATA
32197 || if_modifier == OMP_TARGET_EXIT_DATA)
32199 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32201 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32202 p = IDENTIFIER_POINTER (id);
32203 if (strcmp ("data", p) == 0)
32204 n = 4;
32206 if (n != 4)
32208 location_t loc
32209 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32210 error_at (loc, "expected %<data%>");
32211 if_modifier = ERROR_MARK;
32216 if (if_modifier != ERROR_MARK)
32218 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32220 while (n-- > 0)
32221 cp_lexer_consume_token (parser->lexer);
32223 else
32225 if (n > 2)
32227 location_t loc
32228 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32229 error_at (loc, "expected %<:%>");
32231 if_modifier = ERROR_MARK;
32236 t = cp_parser_condition (parser);
32238 if (t == error_mark_node
32239 || !parens.require_close (parser))
32240 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32241 /*or_comma=*/false,
32242 /*consume_paren=*/true);
32244 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32245 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32247 if (if_modifier != ERROR_MARK
32248 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32250 const char *p = NULL;
32251 switch (if_modifier)
32253 case OMP_PARALLEL: p = "parallel"; break;
32254 case OMP_TASK: p = "task"; break;
32255 case OMP_TASKLOOP: p = "taskloop"; break;
32256 case OMP_TARGET_DATA: p = "target data"; break;
32257 case OMP_TARGET: p = "target"; break;
32258 case OMP_TARGET_UPDATE: p = "target update"; break;
32259 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32260 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32261 default: gcc_unreachable ();
32263 error_at (location, "too many %<if%> clauses with %qs modifier",
32265 return list;
32267 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32269 if (!is_omp)
32270 error_at (location, "too many %<if%> clauses");
32271 else
32272 error_at (location, "too many %<if%> clauses without modifier");
32273 return list;
32275 else if (if_modifier == ERROR_MARK
32276 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32278 error_at (location, "if any %<if%> clause has modifier, then all "
32279 "%<if%> clauses have to use modifier");
32280 return list;
32284 c = build_omp_clause (location, OMP_CLAUSE_IF);
32285 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32286 OMP_CLAUSE_IF_EXPR (c) = t;
32287 OMP_CLAUSE_CHAIN (c) = list;
32289 return c;
32292 /* OpenMP 3.1:
32293 mergeable */
32295 static tree
32296 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32297 tree list, location_t location)
32299 tree c;
32301 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32302 location);
32304 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32305 OMP_CLAUSE_CHAIN (c) = list;
32306 return c;
32309 /* OpenMP 2.5:
32310 nowait */
32312 static tree
32313 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32314 tree list, location_t location)
32316 tree c;
32318 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32320 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32321 OMP_CLAUSE_CHAIN (c) = list;
32322 return c;
32325 /* OpenMP 2.5:
32326 num_threads ( expression ) */
32328 static tree
32329 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32330 location_t location)
32332 tree t, c;
32334 matching_parens parens;
32335 if (!parens.require_open (parser))
32336 return list;
32338 t = cp_parser_expression (parser);
32340 if (t == error_mark_node
32341 || !parens.require_close (parser))
32342 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32343 /*or_comma=*/false,
32344 /*consume_paren=*/true);
32346 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32347 "num_threads", location);
32349 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32350 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32351 OMP_CLAUSE_CHAIN (c) = list;
32353 return c;
32356 /* OpenMP 4.5:
32357 num_tasks ( expression ) */
32359 static tree
32360 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32361 location_t location)
32363 tree t, c;
32365 matching_parens parens;
32366 if (!parens.require_open (parser))
32367 return list;
32369 t = cp_parser_expression (parser);
32371 if (t == error_mark_node
32372 || !parens.require_close (parser))
32373 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32374 /*or_comma=*/false,
32375 /*consume_paren=*/true);
32377 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32378 "num_tasks", location);
32380 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32381 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32382 OMP_CLAUSE_CHAIN (c) = list;
32384 return c;
32387 /* OpenMP 4.5:
32388 grainsize ( expression ) */
32390 static tree
32391 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32392 location_t location)
32394 tree t, c;
32396 matching_parens parens;
32397 if (!parens.require_open (parser))
32398 return list;
32400 t = cp_parser_expression (parser);
32402 if (t == error_mark_node
32403 || !parens.require_close (parser))
32404 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32405 /*or_comma=*/false,
32406 /*consume_paren=*/true);
32408 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32409 "grainsize", location);
32411 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32412 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32413 OMP_CLAUSE_CHAIN (c) = list;
32415 return c;
32418 /* OpenMP 4.5:
32419 priority ( expression ) */
32421 static tree
32422 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32423 location_t location)
32425 tree t, c;
32427 matching_parens parens;
32428 if (!parens.require_open (parser))
32429 return list;
32431 t = cp_parser_expression (parser);
32433 if (t == error_mark_node
32434 || !parens.require_close (parser))
32435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32436 /*or_comma=*/false,
32437 /*consume_paren=*/true);
32439 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32440 "priority", location);
32442 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32443 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32444 OMP_CLAUSE_CHAIN (c) = list;
32446 return c;
32449 /* OpenMP 4.5:
32450 hint ( expression ) */
32452 static tree
32453 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32454 location_t location)
32456 tree t, c;
32458 matching_parens parens;
32459 if (!parens.require_open (parser))
32460 return list;
32462 t = cp_parser_expression (parser);
32464 if (t == error_mark_node
32465 || !parens.require_close (parser))
32466 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32467 /*or_comma=*/false,
32468 /*consume_paren=*/true);
32470 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32472 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32473 OMP_CLAUSE_HINT_EXPR (c) = t;
32474 OMP_CLAUSE_CHAIN (c) = list;
32476 return c;
32479 /* OpenMP 4.5:
32480 defaultmap ( tofrom : scalar ) */
32482 static tree
32483 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32484 location_t location)
32486 tree c, id;
32487 const char *p;
32489 matching_parens parens;
32490 if (!parens.require_open (parser))
32491 return list;
32493 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32495 cp_parser_error (parser, "expected %<tofrom%>");
32496 goto out_err;
32498 id = cp_lexer_peek_token (parser->lexer)->u.value;
32499 p = IDENTIFIER_POINTER (id);
32500 if (strcmp (p, "tofrom") != 0)
32502 cp_parser_error (parser, "expected %<tofrom%>");
32503 goto out_err;
32505 cp_lexer_consume_token (parser->lexer);
32506 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32507 goto out_err;
32509 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32511 cp_parser_error (parser, "expected %<scalar%>");
32512 goto out_err;
32514 id = cp_lexer_peek_token (parser->lexer)->u.value;
32515 p = IDENTIFIER_POINTER (id);
32516 if (strcmp (p, "scalar") != 0)
32518 cp_parser_error (parser, "expected %<scalar%>");
32519 goto out_err;
32521 cp_lexer_consume_token (parser->lexer);
32522 if (!parens.require_close (parser))
32523 goto out_err;
32525 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32526 location);
32528 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32529 OMP_CLAUSE_CHAIN (c) = list;
32530 return c;
32532 out_err:
32533 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32534 /*or_comma=*/false,
32535 /*consume_paren=*/true);
32536 return list;
32539 /* OpenMP 2.5:
32540 ordered
32542 OpenMP 4.5:
32543 ordered ( constant-expression ) */
32545 static tree
32546 cp_parser_omp_clause_ordered (cp_parser *parser,
32547 tree list, location_t location)
32549 tree c, num = NULL_TREE;
32550 HOST_WIDE_INT n;
32552 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32553 "ordered", location);
32555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32557 matching_parens parens;
32558 parens.consume_open (parser);
32560 num = cp_parser_constant_expression (parser);
32562 if (!parens.require_close (parser))
32563 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32564 /*or_comma=*/false,
32565 /*consume_paren=*/true);
32567 if (num == error_mark_node)
32568 return list;
32569 num = fold_non_dependent_expr (num);
32570 if (!tree_fits_shwi_p (num)
32571 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32572 || (n = tree_to_shwi (num)) <= 0
32573 || (int) n != n)
32575 error_at (location,
32576 "ordered argument needs positive constant integer "
32577 "expression");
32578 return list;
32582 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32583 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32584 OMP_CLAUSE_CHAIN (c) = list;
32585 return c;
32588 /* OpenMP 2.5:
32589 reduction ( reduction-operator : variable-list )
32591 reduction-operator:
32592 One of: + * - & ^ | && ||
32594 OpenMP 3.1:
32596 reduction-operator:
32597 One of: + * - & ^ | && || min max
32599 OpenMP 4.0:
32601 reduction-operator:
32602 One of: + * - & ^ | && ||
32603 id-expression */
32605 static tree
32606 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32608 enum tree_code code = ERROR_MARK;
32609 tree nlist, c, id = NULL_TREE;
32611 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32612 return list;
32614 switch (cp_lexer_peek_token (parser->lexer)->type)
32616 case CPP_PLUS: code = PLUS_EXPR; break;
32617 case CPP_MULT: code = MULT_EXPR; break;
32618 case CPP_MINUS: code = MINUS_EXPR; break;
32619 case CPP_AND: code = BIT_AND_EXPR; break;
32620 case CPP_XOR: code = BIT_XOR_EXPR; break;
32621 case CPP_OR: code = BIT_IOR_EXPR; break;
32622 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32623 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32624 default: break;
32627 if (code != ERROR_MARK)
32628 cp_lexer_consume_token (parser->lexer);
32629 else
32631 bool saved_colon_corrects_to_scope_p;
32632 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32633 parser->colon_corrects_to_scope_p = false;
32634 id = cp_parser_id_expression (parser, /*template_p=*/false,
32635 /*check_dependency_p=*/true,
32636 /*template_p=*/NULL,
32637 /*declarator_p=*/false,
32638 /*optional_p=*/false);
32639 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32640 if (identifier_p (id))
32642 const char *p = IDENTIFIER_POINTER (id);
32644 if (strcmp (p, "min") == 0)
32645 code = MIN_EXPR;
32646 else if (strcmp (p, "max") == 0)
32647 code = MAX_EXPR;
32648 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32649 code = PLUS_EXPR;
32650 else if (id == ovl_op_identifier (false, MULT_EXPR))
32651 code = MULT_EXPR;
32652 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32653 code = MINUS_EXPR;
32654 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32655 code = BIT_AND_EXPR;
32656 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32657 code = BIT_IOR_EXPR;
32658 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32659 code = BIT_XOR_EXPR;
32660 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32661 code = TRUTH_ANDIF_EXPR;
32662 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32663 code = TRUTH_ORIF_EXPR;
32664 id = omp_reduction_id (code, id, NULL_TREE);
32665 tree scope = parser->scope;
32666 if (scope)
32667 id = build_qualified_name (NULL_TREE, scope, id, false);
32668 parser->scope = NULL_TREE;
32669 parser->qualifying_scope = NULL_TREE;
32670 parser->object_scope = NULL_TREE;
32672 else
32674 error ("invalid reduction-identifier");
32675 resync_fail:
32676 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32677 /*or_comma=*/false,
32678 /*consume_paren=*/true);
32679 return list;
32683 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32684 goto resync_fail;
32686 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32687 NULL);
32688 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32690 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32691 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32694 return nlist;
32697 /* OpenMP 2.5:
32698 schedule ( schedule-kind )
32699 schedule ( schedule-kind , expression )
32701 schedule-kind:
32702 static | dynamic | guided | runtime | auto
32704 OpenMP 4.5:
32705 schedule ( schedule-modifier : schedule-kind )
32706 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32708 schedule-modifier:
32709 simd
32710 monotonic
32711 nonmonotonic */
32713 static tree
32714 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32716 tree c, t;
32717 int modifiers = 0, nmodifiers = 0;
32719 matching_parens parens;
32720 if (!parens.require_open (parser))
32721 return list;
32723 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32725 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32727 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32728 const char *p = IDENTIFIER_POINTER (id);
32729 if (strcmp ("simd", p) == 0)
32730 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32731 else if (strcmp ("monotonic", p) == 0)
32732 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32733 else if (strcmp ("nonmonotonic", p) == 0)
32734 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32735 else
32736 break;
32737 cp_lexer_consume_token (parser->lexer);
32738 if (nmodifiers++ == 0
32739 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32740 cp_lexer_consume_token (parser->lexer);
32741 else
32743 cp_parser_require (parser, CPP_COLON, RT_COLON);
32744 break;
32748 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32750 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32751 const char *p = IDENTIFIER_POINTER (id);
32753 switch (p[0])
32755 case 'd':
32756 if (strcmp ("dynamic", p) != 0)
32757 goto invalid_kind;
32758 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32759 break;
32761 case 'g':
32762 if (strcmp ("guided", p) != 0)
32763 goto invalid_kind;
32764 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32765 break;
32767 case 'r':
32768 if (strcmp ("runtime", p) != 0)
32769 goto invalid_kind;
32770 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32771 break;
32773 default:
32774 goto invalid_kind;
32777 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32778 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32779 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32780 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32781 else
32782 goto invalid_kind;
32783 cp_lexer_consume_token (parser->lexer);
32785 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32786 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32787 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32788 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32790 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32791 "specified");
32792 modifiers = 0;
32795 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32797 cp_token *token;
32798 cp_lexer_consume_token (parser->lexer);
32800 token = cp_lexer_peek_token (parser->lexer);
32801 t = cp_parser_assignment_expression (parser);
32803 if (t == error_mark_node)
32804 goto resync_fail;
32805 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32806 error_at (token->location, "schedule %<runtime%> does not take "
32807 "a %<chunk_size%> parameter");
32808 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32809 error_at (token->location, "schedule %<auto%> does not take "
32810 "a %<chunk_size%> parameter");
32811 else
32812 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32814 if (!parens.require_close (parser))
32815 goto resync_fail;
32817 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32818 goto resync_fail;
32820 OMP_CLAUSE_SCHEDULE_KIND (c)
32821 = (enum omp_clause_schedule_kind)
32822 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32824 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32825 OMP_CLAUSE_CHAIN (c) = list;
32826 return c;
32828 invalid_kind:
32829 cp_parser_error (parser, "invalid schedule kind");
32830 resync_fail:
32831 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32832 /*or_comma=*/false,
32833 /*consume_paren=*/true);
32834 return list;
32837 /* OpenMP 3.0:
32838 untied */
32840 static tree
32841 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32842 tree list, location_t location)
32844 tree c;
32846 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32848 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32849 OMP_CLAUSE_CHAIN (c) = list;
32850 return c;
32853 /* OpenMP 4.0:
32854 inbranch
32855 notinbranch */
32857 static tree
32858 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32859 tree list, location_t location)
32861 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32862 tree c = build_omp_clause (location, code);
32863 OMP_CLAUSE_CHAIN (c) = list;
32864 return c;
32867 /* OpenMP 4.0:
32868 parallel
32870 sections
32871 taskgroup */
32873 static tree
32874 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32875 enum omp_clause_code code,
32876 tree list, location_t location)
32878 tree c = build_omp_clause (location, code);
32879 OMP_CLAUSE_CHAIN (c) = list;
32880 return c;
32883 /* OpenMP 4.5:
32884 nogroup */
32886 static tree
32887 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32888 tree list, location_t location)
32890 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32891 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32892 OMP_CLAUSE_CHAIN (c) = list;
32893 return c;
32896 /* OpenMP 4.5:
32897 simd
32898 threads */
32900 static tree
32901 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32902 enum omp_clause_code code,
32903 tree list, location_t location)
32905 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32906 tree c = build_omp_clause (location, code);
32907 OMP_CLAUSE_CHAIN (c) = list;
32908 return c;
32911 /* OpenMP 4.0:
32912 num_teams ( expression ) */
32914 static tree
32915 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32916 location_t location)
32918 tree t, c;
32920 matching_parens parens;
32921 if (!parens.require_open (parser))
32922 return list;
32924 t = cp_parser_expression (parser);
32926 if (t == error_mark_node
32927 || !parens.require_close (parser))
32928 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32929 /*or_comma=*/false,
32930 /*consume_paren=*/true);
32932 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32933 "num_teams", location);
32935 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32936 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32937 OMP_CLAUSE_CHAIN (c) = list;
32939 return c;
32942 /* OpenMP 4.0:
32943 thread_limit ( expression ) */
32945 static tree
32946 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32947 location_t location)
32949 tree t, c;
32951 matching_parens parens;
32952 if (!parens.require_open (parser))
32953 return list;
32955 t = cp_parser_expression (parser);
32957 if (t == error_mark_node
32958 || !parens.require_close (parser))
32959 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32960 /*or_comma=*/false,
32961 /*consume_paren=*/true);
32963 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32964 "thread_limit", location);
32966 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32967 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32968 OMP_CLAUSE_CHAIN (c) = list;
32970 return c;
32973 /* OpenMP 4.0:
32974 aligned ( variable-list )
32975 aligned ( variable-list : constant-expression ) */
32977 static tree
32978 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32980 tree nlist, c, alignment = NULL_TREE;
32981 bool colon;
32983 matching_parens parens;
32984 if (!parens.require_open (parser))
32985 return list;
32987 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32988 &colon);
32990 if (colon)
32992 alignment = cp_parser_constant_expression (parser);
32994 if (!parens.require_close (parser))
32995 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32996 /*or_comma=*/false,
32997 /*consume_paren=*/true);
32999 if (alignment == error_mark_node)
33000 alignment = NULL_TREE;
33003 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33004 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33006 return nlist;
33009 /* OpenMP 4.0:
33010 linear ( variable-list )
33011 linear ( variable-list : expression )
33013 OpenMP 4.5:
33014 linear ( modifier ( variable-list ) )
33015 linear ( modifier ( variable-list ) : expression ) */
33017 static tree
33018 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33019 bool declare_simd)
33021 tree nlist, c, step = integer_one_node;
33022 bool colon;
33023 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33025 matching_parens parens;
33026 if (!parens.require_open (parser))
33027 return list;
33029 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33031 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33032 const char *p = IDENTIFIER_POINTER (id);
33034 if (strcmp ("ref", p) == 0)
33035 kind = OMP_CLAUSE_LINEAR_REF;
33036 else if (strcmp ("val", p) == 0)
33037 kind = OMP_CLAUSE_LINEAR_VAL;
33038 else if (strcmp ("uval", p) == 0)
33039 kind = OMP_CLAUSE_LINEAR_UVAL;
33040 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33041 cp_lexer_consume_token (parser->lexer);
33042 else
33043 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33046 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33047 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33048 &colon);
33049 else
33051 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33052 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33053 if (colon)
33054 cp_parser_require (parser, CPP_COLON, RT_COLON);
33055 else if (!parens.require_close (parser))
33056 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33057 /*or_comma=*/false,
33058 /*consume_paren=*/true);
33061 if (colon)
33063 step = NULL_TREE;
33064 if (declare_simd
33065 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33066 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33068 cp_token *token = cp_lexer_peek_token (parser->lexer);
33069 cp_parser_parse_tentatively (parser);
33070 step = cp_parser_id_expression (parser, /*template_p=*/false,
33071 /*check_dependency_p=*/true,
33072 /*template_p=*/NULL,
33073 /*declarator_p=*/false,
33074 /*optional_p=*/false);
33075 if (step != error_mark_node)
33076 step = cp_parser_lookup_name_simple (parser, step, token->location);
33077 if (step == error_mark_node)
33079 step = NULL_TREE;
33080 cp_parser_abort_tentative_parse (parser);
33082 else if (!cp_parser_parse_definitely (parser))
33083 step = NULL_TREE;
33085 if (!step)
33086 step = cp_parser_expression (parser);
33088 if (!parens.require_close (parser))
33089 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33090 /*or_comma=*/false,
33091 /*consume_paren=*/true);
33093 if (step == error_mark_node)
33094 return list;
33097 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33099 OMP_CLAUSE_LINEAR_STEP (c) = step;
33100 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33103 return nlist;
33106 /* OpenMP 4.0:
33107 safelen ( constant-expression ) */
33109 static tree
33110 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33111 location_t location)
33113 tree t, c;
33115 matching_parens parens;
33116 if (!parens.require_open (parser))
33117 return list;
33119 t = cp_parser_constant_expression (parser);
33121 if (t == error_mark_node
33122 || !parens.require_close (parser))
33123 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33124 /*or_comma=*/false,
33125 /*consume_paren=*/true);
33127 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33129 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33130 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33131 OMP_CLAUSE_CHAIN (c) = list;
33133 return c;
33136 /* OpenMP 4.0:
33137 simdlen ( constant-expression ) */
33139 static tree
33140 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33141 location_t location)
33143 tree t, c;
33145 matching_parens parens;
33146 if (!parens.require_open (parser))
33147 return list;
33149 t = cp_parser_constant_expression (parser);
33151 if (t == error_mark_node
33152 || !parens.require_close (parser))
33153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33154 /*or_comma=*/false,
33155 /*consume_paren=*/true);
33157 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33159 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33160 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33161 OMP_CLAUSE_CHAIN (c) = list;
33163 return c;
33166 /* OpenMP 4.5:
33167 vec:
33168 identifier [+/- integer]
33169 vec , identifier [+/- integer]
33172 static tree
33173 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33174 tree list)
33176 tree vec = NULL;
33178 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33180 cp_parser_error (parser, "expected identifier");
33181 return list;
33184 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33186 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33187 tree t, identifier = cp_parser_identifier (parser);
33188 tree addend = NULL;
33190 if (identifier == error_mark_node)
33191 t = error_mark_node;
33192 else
33194 t = cp_parser_lookup_name_simple
33195 (parser, identifier,
33196 cp_lexer_peek_token (parser->lexer)->location);
33197 if (t == error_mark_node)
33198 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33199 id_loc);
33202 bool neg = false;
33203 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33204 neg = true;
33205 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33207 addend = integer_zero_node;
33208 goto add_to_vector;
33210 cp_lexer_consume_token (parser->lexer);
33212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33214 cp_parser_error (parser, "expected integer");
33215 return list;
33218 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33219 if (TREE_CODE (addend) != INTEGER_CST)
33221 cp_parser_error (parser, "expected integer");
33222 return list;
33224 cp_lexer_consume_token (parser->lexer);
33226 add_to_vector:
33227 if (t != error_mark_node)
33229 vec = tree_cons (addend, t, vec);
33230 if (neg)
33231 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33235 break;
33237 cp_lexer_consume_token (parser->lexer);
33240 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33242 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33243 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33244 OMP_CLAUSE_DECL (u) = nreverse (vec);
33245 OMP_CLAUSE_CHAIN (u) = list;
33246 return u;
33248 return list;
33251 /* OpenMP 4.0:
33252 depend ( depend-kind : variable-list )
33254 depend-kind:
33255 in | out | inout
33257 OpenMP 4.5:
33258 depend ( source )
33260 depend ( sink : vec ) */
33262 static tree
33263 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33265 tree nlist, c;
33266 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33268 matching_parens parens;
33269 if (!parens.require_open (parser))
33270 return list;
33272 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33274 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33275 const char *p = IDENTIFIER_POINTER (id);
33277 if (strcmp ("in", p) == 0)
33278 kind = OMP_CLAUSE_DEPEND_IN;
33279 else if (strcmp ("inout", p) == 0)
33280 kind = OMP_CLAUSE_DEPEND_INOUT;
33281 else if (strcmp ("out", p) == 0)
33282 kind = OMP_CLAUSE_DEPEND_OUT;
33283 else if (strcmp ("source", p) == 0)
33284 kind = OMP_CLAUSE_DEPEND_SOURCE;
33285 else if (strcmp ("sink", p) == 0)
33286 kind = OMP_CLAUSE_DEPEND_SINK;
33287 else
33288 goto invalid_kind;
33290 else
33291 goto invalid_kind;
33293 cp_lexer_consume_token (parser->lexer);
33295 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33297 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33298 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33299 OMP_CLAUSE_DECL (c) = NULL_TREE;
33300 OMP_CLAUSE_CHAIN (c) = list;
33301 if (!parens.require_close (parser))
33302 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33303 /*or_comma=*/false,
33304 /*consume_paren=*/true);
33305 return c;
33308 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33309 goto resync_fail;
33311 if (kind == OMP_CLAUSE_DEPEND_SINK)
33312 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33313 else
33315 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33316 list, NULL);
33318 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33319 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33321 return nlist;
33323 invalid_kind:
33324 cp_parser_error (parser, "invalid depend kind");
33325 resync_fail:
33326 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33327 /*or_comma=*/false,
33328 /*consume_paren=*/true);
33329 return list;
33332 /* OpenMP 4.0:
33333 map ( map-kind : variable-list )
33334 map ( variable-list )
33336 map-kind:
33337 alloc | to | from | tofrom
33339 OpenMP 4.5:
33340 map-kind:
33341 alloc | to | from | tofrom | release | delete
33343 map ( always [,] map-kind: variable-list ) */
33345 static tree
33346 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33348 tree nlist, c;
33349 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33350 bool always = false;
33352 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33353 return list;
33355 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33357 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33358 const char *p = IDENTIFIER_POINTER (id);
33360 if (strcmp ("always", p) == 0)
33362 int nth = 2;
33363 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33364 nth++;
33365 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33366 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33367 == RID_DELETE))
33368 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33369 == CPP_COLON))
33371 always = true;
33372 cp_lexer_consume_token (parser->lexer);
33373 if (nth == 3)
33374 cp_lexer_consume_token (parser->lexer);
33379 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33380 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33382 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33383 const char *p = IDENTIFIER_POINTER (id);
33385 if (strcmp ("alloc", p) == 0)
33386 kind = GOMP_MAP_ALLOC;
33387 else if (strcmp ("to", p) == 0)
33388 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33389 else if (strcmp ("from", p) == 0)
33390 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33391 else if (strcmp ("tofrom", p) == 0)
33392 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33393 else if (strcmp ("release", p) == 0)
33394 kind = GOMP_MAP_RELEASE;
33395 else
33397 cp_parser_error (parser, "invalid map kind");
33398 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33399 /*or_comma=*/false,
33400 /*consume_paren=*/true);
33401 return list;
33403 cp_lexer_consume_token (parser->lexer);
33404 cp_lexer_consume_token (parser->lexer);
33406 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33407 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33409 kind = GOMP_MAP_DELETE;
33410 cp_lexer_consume_token (parser->lexer);
33411 cp_lexer_consume_token (parser->lexer);
33414 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33415 NULL);
33417 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33418 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33420 return nlist;
33423 /* OpenMP 4.0:
33424 device ( expression ) */
33426 static tree
33427 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33428 location_t location)
33430 tree t, c;
33432 matching_parens parens;
33433 if (!parens.require_open (parser))
33434 return list;
33436 t = cp_parser_expression (parser);
33438 if (t == error_mark_node
33439 || !parens.require_close (parser))
33440 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33441 /*or_comma=*/false,
33442 /*consume_paren=*/true);
33444 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33445 "device", location);
33447 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33448 OMP_CLAUSE_DEVICE_ID (c) = t;
33449 OMP_CLAUSE_CHAIN (c) = list;
33451 return c;
33454 /* OpenMP 4.0:
33455 dist_schedule ( static )
33456 dist_schedule ( static , expression ) */
33458 static tree
33459 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33460 location_t location)
33462 tree c, t;
33464 matching_parens parens;
33465 if (!parens.require_open (parser))
33466 return list;
33468 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33470 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33471 goto invalid_kind;
33472 cp_lexer_consume_token (parser->lexer);
33474 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33476 cp_lexer_consume_token (parser->lexer);
33478 t = cp_parser_assignment_expression (parser);
33480 if (t == error_mark_node)
33481 goto resync_fail;
33482 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33484 if (!parens.require_close (parser))
33485 goto resync_fail;
33487 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33488 goto resync_fail;
33490 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33491 location);
33492 OMP_CLAUSE_CHAIN (c) = list;
33493 return c;
33495 invalid_kind:
33496 cp_parser_error (parser, "invalid dist_schedule kind");
33497 resync_fail:
33498 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33499 /*or_comma=*/false,
33500 /*consume_paren=*/true);
33501 return list;
33504 /* OpenMP 4.0:
33505 proc_bind ( proc-bind-kind )
33507 proc-bind-kind:
33508 master | close | spread */
33510 static tree
33511 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33512 location_t location)
33514 tree c;
33515 enum omp_clause_proc_bind_kind kind;
33517 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33518 return list;
33520 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33522 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33523 const char *p = IDENTIFIER_POINTER (id);
33525 if (strcmp ("master", p) == 0)
33526 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33527 else if (strcmp ("close", p) == 0)
33528 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33529 else if (strcmp ("spread", p) == 0)
33530 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33531 else
33532 goto invalid_kind;
33534 else
33535 goto invalid_kind;
33537 cp_lexer_consume_token (parser->lexer);
33538 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33539 goto resync_fail;
33541 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33542 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33543 location);
33544 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33545 OMP_CLAUSE_CHAIN (c) = list;
33546 return c;
33548 invalid_kind:
33549 cp_parser_error (parser, "invalid depend kind");
33550 resync_fail:
33551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33552 /*or_comma=*/false,
33553 /*consume_paren=*/true);
33554 return list;
33557 /* OpenACC:
33558 async [( int-expr )] */
33560 static tree
33561 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33563 tree c, t;
33564 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33566 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33568 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33570 matching_parens parens;
33571 parens.consume_open (parser);
33573 t = cp_parser_expression (parser);
33574 if (t == error_mark_node
33575 || !parens.require_close (parser))
33576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33577 /*or_comma=*/false,
33578 /*consume_paren=*/true);
33581 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33583 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33584 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33585 OMP_CLAUSE_CHAIN (c) = list;
33586 list = c;
33588 return list;
33591 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33592 is a bitmask in MASK. Return the list of clauses found. */
33594 static tree
33595 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33596 const char *where, cp_token *pragma_tok,
33597 bool finish_p = true)
33599 tree clauses = NULL;
33600 bool first = true;
33602 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33604 location_t here;
33605 pragma_omp_clause c_kind;
33606 omp_clause_code code;
33607 const char *c_name;
33608 tree prev = clauses;
33610 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33611 cp_lexer_consume_token (parser->lexer);
33613 here = cp_lexer_peek_token (parser->lexer)->location;
33614 c_kind = cp_parser_omp_clause_name (parser);
33616 switch (c_kind)
33618 case PRAGMA_OACC_CLAUSE_ASYNC:
33619 clauses = cp_parser_oacc_clause_async (parser, clauses);
33620 c_name = "async";
33621 break;
33622 case PRAGMA_OACC_CLAUSE_AUTO:
33623 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33624 clauses, here);
33625 c_name = "auto";
33626 break;
33627 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33628 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33629 c_name = "collapse";
33630 break;
33631 case PRAGMA_OACC_CLAUSE_COPY:
33632 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33633 c_name = "copy";
33634 break;
33635 case PRAGMA_OACC_CLAUSE_COPYIN:
33636 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33637 c_name = "copyin";
33638 break;
33639 case PRAGMA_OACC_CLAUSE_COPYOUT:
33640 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33641 c_name = "copyout";
33642 break;
33643 case PRAGMA_OACC_CLAUSE_CREATE:
33644 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33645 c_name = "create";
33646 break;
33647 case PRAGMA_OACC_CLAUSE_DELETE:
33648 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33649 c_name = "delete";
33650 break;
33651 case PRAGMA_OMP_CLAUSE_DEFAULT:
33652 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33653 c_name = "default";
33654 break;
33655 case PRAGMA_OACC_CLAUSE_DEVICE:
33656 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33657 c_name = "device";
33658 break;
33659 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33660 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33661 c_name = "deviceptr";
33662 break;
33663 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33664 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33665 c_name = "device_resident";
33666 break;
33667 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33668 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33669 clauses);
33670 c_name = "firstprivate";
33671 break;
33672 case PRAGMA_OACC_CLAUSE_GANG:
33673 c_name = "gang";
33674 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33675 c_name, clauses);
33676 break;
33677 case PRAGMA_OACC_CLAUSE_HOST:
33678 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33679 c_name = "host";
33680 break;
33681 case PRAGMA_OACC_CLAUSE_IF:
33682 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33683 c_name = "if";
33684 break;
33685 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33686 clauses = cp_parser_oacc_simple_clause (parser,
33687 OMP_CLAUSE_INDEPENDENT,
33688 clauses, here);
33689 c_name = "independent";
33690 break;
33691 case PRAGMA_OACC_CLAUSE_LINK:
33692 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33693 c_name = "link";
33694 break;
33695 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33696 code = OMP_CLAUSE_NUM_GANGS;
33697 c_name = "num_gangs";
33698 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33699 clauses);
33700 break;
33701 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33702 c_name = "num_workers";
33703 code = OMP_CLAUSE_NUM_WORKERS;
33704 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33705 clauses);
33706 break;
33707 case PRAGMA_OACC_CLAUSE_PRESENT:
33708 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33709 c_name = "present";
33710 break;
33711 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33712 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33713 c_name = "present_or_copy";
33714 break;
33715 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33716 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33717 c_name = "present_or_copyin";
33718 break;
33719 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33720 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33721 c_name = "present_or_copyout";
33722 break;
33723 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33724 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33725 c_name = "present_or_create";
33726 break;
33727 case PRAGMA_OACC_CLAUSE_PRIVATE:
33728 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33729 clauses);
33730 c_name = "private";
33731 break;
33732 case PRAGMA_OACC_CLAUSE_REDUCTION:
33733 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33734 c_name = "reduction";
33735 break;
33736 case PRAGMA_OACC_CLAUSE_SELF:
33737 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33738 c_name = "self";
33739 break;
33740 case PRAGMA_OACC_CLAUSE_SEQ:
33741 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33742 clauses, here);
33743 c_name = "seq";
33744 break;
33745 case PRAGMA_OACC_CLAUSE_TILE:
33746 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33747 c_name = "tile";
33748 break;
33749 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33750 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33751 clauses);
33752 c_name = "use_device";
33753 break;
33754 case PRAGMA_OACC_CLAUSE_VECTOR:
33755 c_name = "vector";
33756 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33757 c_name, clauses);
33758 break;
33759 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33760 c_name = "vector_length";
33761 code = OMP_CLAUSE_VECTOR_LENGTH;
33762 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33763 clauses);
33764 break;
33765 case PRAGMA_OACC_CLAUSE_WAIT:
33766 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33767 c_name = "wait";
33768 break;
33769 case PRAGMA_OACC_CLAUSE_WORKER:
33770 c_name = "worker";
33771 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33772 c_name, clauses);
33773 break;
33774 default:
33775 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33776 goto saw_error;
33779 first = false;
33781 if (((mask >> c_kind) & 1) == 0)
33783 /* Remove the invalid clause(s) from the list to avoid
33784 confusing the rest of the compiler. */
33785 clauses = prev;
33786 error_at (here, "%qs is not valid for %qs", c_name, where);
33790 saw_error:
33791 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33793 if (finish_p)
33794 return finish_omp_clauses (clauses, C_ORT_ACC);
33796 return clauses;
33799 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33800 is a bitmask in MASK. Return the list of clauses found; the result
33801 of clause default goes in *pdefault. */
33803 static tree
33804 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33805 const char *where, cp_token *pragma_tok,
33806 bool finish_p = true)
33808 tree clauses = NULL;
33809 bool first = true;
33810 cp_token *token = NULL;
33812 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33814 pragma_omp_clause c_kind;
33815 const char *c_name;
33816 tree prev = clauses;
33818 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33819 cp_lexer_consume_token (parser->lexer);
33821 token = cp_lexer_peek_token (parser->lexer);
33822 c_kind = cp_parser_omp_clause_name (parser);
33824 switch (c_kind)
33826 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33827 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33828 token->location);
33829 c_name = "collapse";
33830 break;
33831 case PRAGMA_OMP_CLAUSE_COPYIN:
33832 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33833 c_name = "copyin";
33834 break;
33835 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33836 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33837 clauses);
33838 c_name = "copyprivate";
33839 break;
33840 case PRAGMA_OMP_CLAUSE_DEFAULT:
33841 clauses = cp_parser_omp_clause_default (parser, clauses,
33842 token->location, false);
33843 c_name = "default";
33844 break;
33845 case PRAGMA_OMP_CLAUSE_FINAL:
33846 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33847 c_name = "final";
33848 break;
33849 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33850 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33851 clauses);
33852 c_name = "firstprivate";
33853 break;
33854 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33855 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33856 token->location);
33857 c_name = "grainsize";
33858 break;
33859 case PRAGMA_OMP_CLAUSE_HINT:
33860 clauses = cp_parser_omp_clause_hint (parser, clauses,
33861 token->location);
33862 c_name = "hint";
33863 break;
33864 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33865 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33866 token->location);
33867 c_name = "defaultmap";
33868 break;
33869 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33870 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33871 clauses);
33872 c_name = "use_device_ptr";
33873 break;
33874 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33875 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33876 clauses);
33877 c_name = "is_device_ptr";
33878 break;
33879 case PRAGMA_OMP_CLAUSE_IF:
33880 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33881 true);
33882 c_name = "if";
33883 break;
33884 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33885 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33886 clauses);
33887 c_name = "lastprivate";
33888 break;
33889 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33890 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33891 token->location);
33892 c_name = "mergeable";
33893 break;
33894 case PRAGMA_OMP_CLAUSE_NOWAIT:
33895 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33896 c_name = "nowait";
33897 break;
33898 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33899 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33900 token->location);
33901 c_name = "num_tasks";
33902 break;
33903 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33904 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33905 token->location);
33906 c_name = "num_threads";
33907 break;
33908 case PRAGMA_OMP_CLAUSE_ORDERED:
33909 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33910 token->location);
33911 c_name = "ordered";
33912 break;
33913 case PRAGMA_OMP_CLAUSE_PRIORITY:
33914 clauses = cp_parser_omp_clause_priority (parser, clauses,
33915 token->location);
33916 c_name = "priority";
33917 break;
33918 case PRAGMA_OMP_CLAUSE_PRIVATE:
33919 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33920 clauses);
33921 c_name = "private";
33922 break;
33923 case PRAGMA_OMP_CLAUSE_REDUCTION:
33924 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33925 c_name = "reduction";
33926 break;
33927 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33928 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33929 token->location);
33930 c_name = "schedule";
33931 break;
33932 case PRAGMA_OMP_CLAUSE_SHARED:
33933 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33934 clauses);
33935 c_name = "shared";
33936 break;
33937 case PRAGMA_OMP_CLAUSE_UNTIED:
33938 clauses = cp_parser_omp_clause_untied (parser, clauses,
33939 token->location);
33940 c_name = "untied";
33941 break;
33942 case PRAGMA_OMP_CLAUSE_INBRANCH:
33943 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33944 clauses, token->location);
33945 c_name = "inbranch";
33946 break;
33947 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33948 clauses = cp_parser_omp_clause_branch (parser,
33949 OMP_CLAUSE_NOTINBRANCH,
33950 clauses, token->location);
33951 c_name = "notinbranch";
33952 break;
33953 case PRAGMA_OMP_CLAUSE_PARALLEL:
33954 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33955 clauses, token->location);
33956 c_name = "parallel";
33957 if (!first)
33959 clause_not_first:
33960 error_at (token->location, "%qs must be the first clause of %qs",
33961 c_name, where);
33962 clauses = prev;
33964 break;
33965 case PRAGMA_OMP_CLAUSE_FOR:
33966 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33967 clauses, token->location);
33968 c_name = "for";
33969 if (!first)
33970 goto clause_not_first;
33971 break;
33972 case PRAGMA_OMP_CLAUSE_SECTIONS:
33973 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33974 clauses, token->location);
33975 c_name = "sections";
33976 if (!first)
33977 goto clause_not_first;
33978 break;
33979 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33980 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33981 clauses, token->location);
33982 c_name = "taskgroup";
33983 if (!first)
33984 goto clause_not_first;
33985 break;
33986 case PRAGMA_OMP_CLAUSE_LINK:
33987 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33988 c_name = "to";
33989 break;
33990 case PRAGMA_OMP_CLAUSE_TO:
33991 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33992 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33993 clauses);
33994 else
33995 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33996 c_name = "to";
33997 break;
33998 case PRAGMA_OMP_CLAUSE_FROM:
33999 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34000 c_name = "from";
34001 break;
34002 case PRAGMA_OMP_CLAUSE_UNIFORM:
34003 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34004 clauses);
34005 c_name = "uniform";
34006 break;
34007 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34008 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34009 token->location);
34010 c_name = "num_teams";
34011 break;
34012 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34013 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34014 token->location);
34015 c_name = "thread_limit";
34016 break;
34017 case PRAGMA_OMP_CLAUSE_ALIGNED:
34018 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34019 c_name = "aligned";
34020 break;
34021 case PRAGMA_OMP_CLAUSE_LINEAR:
34023 bool declare_simd = false;
34024 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34025 declare_simd = true;
34026 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34028 c_name = "linear";
34029 break;
34030 case PRAGMA_OMP_CLAUSE_DEPEND:
34031 clauses = cp_parser_omp_clause_depend (parser, clauses,
34032 token->location);
34033 c_name = "depend";
34034 break;
34035 case PRAGMA_OMP_CLAUSE_MAP:
34036 clauses = cp_parser_omp_clause_map (parser, clauses);
34037 c_name = "map";
34038 break;
34039 case PRAGMA_OMP_CLAUSE_DEVICE:
34040 clauses = cp_parser_omp_clause_device (parser, clauses,
34041 token->location);
34042 c_name = "device";
34043 break;
34044 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34045 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34046 token->location);
34047 c_name = "dist_schedule";
34048 break;
34049 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34050 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34051 token->location);
34052 c_name = "proc_bind";
34053 break;
34054 case PRAGMA_OMP_CLAUSE_SAFELEN:
34055 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34056 token->location);
34057 c_name = "safelen";
34058 break;
34059 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34060 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34061 token->location);
34062 c_name = "simdlen";
34063 break;
34064 case PRAGMA_OMP_CLAUSE_NOGROUP:
34065 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34066 token->location);
34067 c_name = "nogroup";
34068 break;
34069 case PRAGMA_OMP_CLAUSE_THREADS:
34070 clauses
34071 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34072 clauses, token->location);
34073 c_name = "threads";
34074 break;
34075 case PRAGMA_OMP_CLAUSE_SIMD:
34076 clauses
34077 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34078 clauses, token->location);
34079 c_name = "simd";
34080 break;
34081 default:
34082 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34083 goto saw_error;
34086 first = false;
34088 if (((mask >> c_kind) & 1) == 0)
34090 /* Remove the invalid clause(s) from the list to avoid
34091 confusing the rest of the compiler. */
34092 clauses = prev;
34093 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34096 saw_error:
34097 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34098 if (finish_p)
34100 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34101 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34102 else
34103 return finish_omp_clauses (clauses, C_ORT_OMP);
34105 return clauses;
34108 /* OpenMP 2.5:
34109 structured-block:
34110 statement
34112 In practice, we're also interested in adding the statement to an
34113 outer node. So it is convenient if we work around the fact that
34114 cp_parser_statement calls add_stmt. */
34116 static unsigned
34117 cp_parser_begin_omp_structured_block (cp_parser *parser)
34119 unsigned save = parser->in_statement;
34121 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34122 This preserves the "not within loop or switch" style error messages
34123 for nonsense cases like
34124 void foo() {
34125 #pragma omp single
34126 break;
34129 if (parser->in_statement)
34130 parser->in_statement = IN_OMP_BLOCK;
34132 return save;
34135 static void
34136 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34138 parser->in_statement = save;
34141 static tree
34142 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34144 tree stmt = begin_omp_structured_block ();
34145 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34147 cp_parser_statement (parser, NULL_TREE, false, if_p);
34149 cp_parser_end_omp_structured_block (parser, save);
34150 return finish_omp_structured_block (stmt);
34153 /* OpenMP 2.5:
34154 # pragma omp atomic new-line
34155 expression-stmt
34157 expression-stmt:
34158 x binop= expr | x++ | ++x | x-- | --x
34159 binop:
34160 +, *, -, /, &, ^, |, <<, >>
34162 where x is an lvalue expression with scalar type.
34164 OpenMP 3.1:
34165 # pragma omp atomic new-line
34166 update-stmt
34168 # pragma omp atomic read new-line
34169 read-stmt
34171 # pragma omp atomic write new-line
34172 write-stmt
34174 # pragma omp atomic update new-line
34175 update-stmt
34177 # pragma omp atomic capture new-line
34178 capture-stmt
34180 # pragma omp atomic capture new-line
34181 capture-block
34183 read-stmt:
34184 v = x
34185 write-stmt:
34186 x = expr
34187 update-stmt:
34188 expression-stmt | x = x binop expr
34189 capture-stmt:
34190 v = expression-stmt
34191 capture-block:
34192 { v = x; update-stmt; } | { update-stmt; v = x; }
34194 OpenMP 4.0:
34195 update-stmt:
34196 expression-stmt | x = x binop expr | x = expr binop x
34197 capture-stmt:
34198 v = update-stmt
34199 capture-block:
34200 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34202 where x and v are lvalue expressions with scalar type. */
34204 static void
34205 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34207 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34208 tree rhs1 = NULL_TREE, orig_lhs;
34209 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34210 bool structured_block = false;
34211 bool seq_cst = false;
34213 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34215 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34216 const char *p = IDENTIFIER_POINTER (id);
34218 if (!strcmp (p, "seq_cst"))
34220 seq_cst = true;
34221 cp_lexer_consume_token (parser->lexer);
34222 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34223 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34224 cp_lexer_consume_token (parser->lexer);
34227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34230 const char *p = IDENTIFIER_POINTER (id);
34232 if (!strcmp (p, "read"))
34233 code = OMP_ATOMIC_READ;
34234 else if (!strcmp (p, "write"))
34235 code = NOP_EXPR;
34236 else if (!strcmp (p, "update"))
34237 code = OMP_ATOMIC;
34238 else if (!strcmp (p, "capture"))
34239 code = OMP_ATOMIC_CAPTURE_NEW;
34240 else
34241 p = NULL;
34242 if (p)
34243 cp_lexer_consume_token (parser->lexer);
34245 if (!seq_cst)
34247 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34248 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34249 cp_lexer_consume_token (parser->lexer);
34251 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34253 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34254 const char *p = IDENTIFIER_POINTER (id);
34256 if (!strcmp (p, "seq_cst"))
34258 seq_cst = true;
34259 cp_lexer_consume_token (parser->lexer);
34263 cp_parser_require_pragma_eol (parser, pragma_tok);
34265 switch (code)
34267 case OMP_ATOMIC_READ:
34268 case NOP_EXPR: /* atomic write */
34269 v = cp_parser_unary_expression (parser);
34270 if (v == error_mark_node)
34271 goto saw_error;
34272 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34273 goto saw_error;
34274 if (code == NOP_EXPR)
34275 lhs = cp_parser_expression (parser);
34276 else
34277 lhs = cp_parser_unary_expression (parser);
34278 if (lhs == error_mark_node)
34279 goto saw_error;
34280 if (code == NOP_EXPR)
34282 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34283 opcode. */
34284 code = OMP_ATOMIC;
34285 rhs = lhs;
34286 lhs = v;
34287 v = NULL_TREE;
34289 goto done;
34290 case OMP_ATOMIC_CAPTURE_NEW:
34291 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34293 cp_lexer_consume_token (parser->lexer);
34294 structured_block = true;
34296 else
34298 v = cp_parser_unary_expression (parser);
34299 if (v == error_mark_node)
34300 goto saw_error;
34301 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34302 goto saw_error;
34304 default:
34305 break;
34308 restart:
34309 lhs = cp_parser_unary_expression (parser);
34310 orig_lhs = lhs;
34311 switch (TREE_CODE (lhs))
34313 case ERROR_MARK:
34314 goto saw_error;
34316 case POSTINCREMENT_EXPR:
34317 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34318 code = OMP_ATOMIC_CAPTURE_OLD;
34319 /* FALLTHROUGH */
34320 case PREINCREMENT_EXPR:
34321 lhs = TREE_OPERAND (lhs, 0);
34322 opcode = PLUS_EXPR;
34323 rhs = integer_one_node;
34324 break;
34326 case POSTDECREMENT_EXPR:
34327 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34328 code = OMP_ATOMIC_CAPTURE_OLD;
34329 /* FALLTHROUGH */
34330 case PREDECREMENT_EXPR:
34331 lhs = TREE_OPERAND (lhs, 0);
34332 opcode = MINUS_EXPR;
34333 rhs = integer_one_node;
34334 break;
34336 case COMPOUND_EXPR:
34337 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34338 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34339 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34340 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34341 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34342 (TREE_OPERAND (lhs, 1), 0), 0)))
34343 == BOOLEAN_TYPE)
34344 /* Undo effects of boolean_increment for post {in,de}crement. */
34345 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34346 /* FALLTHRU */
34347 case MODIFY_EXPR:
34348 if (TREE_CODE (lhs) == MODIFY_EXPR
34349 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34351 /* Undo effects of boolean_increment. */
34352 if (integer_onep (TREE_OPERAND (lhs, 1)))
34354 /* This is pre or post increment. */
34355 rhs = TREE_OPERAND (lhs, 1);
34356 lhs = TREE_OPERAND (lhs, 0);
34357 opcode = NOP_EXPR;
34358 if (code == OMP_ATOMIC_CAPTURE_NEW
34359 && !structured_block
34360 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34361 code = OMP_ATOMIC_CAPTURE_OLD;
34362 break;
34365 /* FALLTHRU */
34366 default:
34367 switch (cp_lexer_peek_token (parser->lexer)->type)
34369 case CPP_MULT_EQ:
34370 opcode = MULT_EXPR;
34371 break;
34372 case CPP_DIV_EQ:
34373 opcode = TRUNC_DIV_EXPR;
34374 break;
34375 case CPP_PLUS_EQ:
34376 opcode = PLUS_EXPR;
34377 break;
34378 case CPP_MINUS_EQ:
34379 opcode = MINUS_EXPR;
34380 break;
34381 case CPP_LSHIFT_EQ:
34382 opcode = LSHIFT_EXPR;
34383 break;
34384 case CPP_RSHIFT_EQ:
34385 opcode = RSHIFT_EXPR;
34386 break;
34387 case CPP_AND_EQ:
34388 opcode = BIT_AND_EXPR;
34389 break;
34390 case CPP_OR_EQ:
34391 opcode = BIT_IOR_EXPR;
34392 break;
34393 case CPP_XOR_EQ:
34394 opcode = BIT_XOR_EXPR;
34395 break;
34396 case CPP_EQ:
34397 enum cp_parser_prec oprec;
34398 cp_token *token;
34399 cp_lexer_consume_token (parser->lexer);
34400 cp_parser_parse_tentatively (parser);
34401 rhs1 = cp_parser_simple_cast_expression (parser);
34402 if (rhs1 == error_mark_node)
34404 cp_parser_abort_tentative_parse (parser);
34405 cp_parser_simple_cast_expression (parser);
34406 goto saw_error;
34408 token = cp_lexer_peek_token (parser->lexer);
34409 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34411 cp_parser_abort_tentative_parse (parser);
34412 cp_parser_parse_tentatively (parser);
34413 rhs = cp_parser_binary_expression (parser, false, true,
34414 PREC_NOT_OPERATOR, NULL);
34415 if (rhs == error_mark_node)
34417 cp_parser_abort_tentative_parse (parser);
34418 cp_parser_binary_expression (parser, false, true,
34419 PREC_NOT_OPERATOR, NULL);
34420 goto saw_error;
34422 switch (TREE_CODE (rhs))
34424 case MULT_EXPR:
34425 case TRUNC_DIV_EXPR:
34426 case RDIV_EXPR:
34427 case PLUS_EXPR:
34428 case MINUS_EXPR:
34429 case LSHIFT_EXPR:
34430 case RSHIFT_EXPR:
34431 case BIT_AND_EXPR:
34432 case BIT_IOR_EXPR:
34433 case BIT_XOR_EXPR:
34434 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34436 if (cp_parser_parse_definitely (parser))
34438 opcode = TREE_CODE (rhs);
34439 rhs1 = TREE_OPERAND (rhs, 0);
34440 rhs = TREE_OPERAND (rhs, 1);
34441 goto stmt_done;
34443 else
34444 goto saw_error;
34446 break;
34447 default:
34448 break;
34450 cp_parser_abort_tentative_parse (parser);
34451 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34453 rhs = cp_parser_expression (parser);
34454 if (rhs == error_mark_node)
34455 goto saw_error;
34456 opcode = NOP_EXPR;
34457 rhs1 = NULL_TREE;
34458 goto stmt_done;
34460 cp_parser_error (parser,
34461 "invalid form of %<#pragma omp atomic%>");
34462 goto saw_error;
34464 if (!cp_parser_parse_definitely (parser))
34465 goto saw_error;
34466 switch (token->type)
34468 case CPP_SEMICOLON:
34469 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34471 code = OMP_ATOMIC_CAPTURE_OLD;
34472 v = lhs;
34473 lhs = NULL_TREE;
34474 lhs1 = rhs1;
34475 rhs1 = NULL_TREE;
34476 cp_lexer_consume_token (parser->lexer);
34477 goto restart;
34479 else if (structured_block)
34481 opcode = NOP_EXPR;
34482 rhs = rhs1;
34483 rhs1 = NULL_TREE;
34484 goto stmt_done;
34486 cp_parser_error (parser,
34487 "invalid form of %<#pragma omp atomic%>");
34488 goto saw_error;
34489 case CPP_MULT:
34490 opcode = MULT_EXPR;
34491 break;
34492 case CPP_DIV:
34493 opcode = TRUNC_DIV_EXPR;
34494 break;
34495 case CPP_PLUS:
34496 opcode = PLUS_EXPR;
34497 break;
34498 case CPP_MINUS:
34499 opcode = MINUS_EXPR;
34500 break;
34501 case CPP_LSHIFT:
34502 opcode = LSHIFT_EXPR;
34503 break;
34504 case CPP_RSHIFT:
34505 opcode = RSHIFT_EXPR;
34506 break;
34507 case CPP_AND:
34508 opcode = BIT_AND_EXPR;
34509 break;
34510 case CPP_OR:
34511 opcode = BIT_IOR_EXPR;
34512 break;
34513 case CPP_XOR:
34514 opcode = BIT_XOR_EXPR;
34515 break;
34516 default:
34517 cp_parser_error (parser,
34518 "invalid operator for %<#pragma omp atomic%>");
34519 goto saw_error;
34521 oprec = TOKEN_PRECEDENCE (token);
34522 gcc_assert (oprec != PREC_NOT_OPERATOR);
34523 if (commutative_tree_code (opcode))
34524 oprec = (enum cp_parser_prec) (oprec - 1);
34525 cp_lexer_consume_token (parser->lexer);
34526 rhs = cp_parser_binary_expression (parser, false, false,
34527 oprec, NULL);
34528 if (rhs == error_mark_node)
34529 goto saw_error;
34530 goto stmt_done;
34531 /* FALLTHROUGH */
34532 default:
34533 cp_parser_error (parser,
34534 "invalid operator for %<#pragma omp atomic%>");
34535 goto saw_error;
34537 cp_lexer_consume_token (parser->lexer);
34539 rhs = cp_parser_expression (parser);
34540 if (rhs == error_mark_node)
34541 goto saw_error;
34542 break;
34544 stmt_done:
34545 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34547 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34548 goto saw_error;
34549 v = cp_parser_unary_expression (parser);
34550 if (v == error_mark_node)
34551 goto saw_error;
34552 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34553 goto saw_error;
34554 lhs1 = cp_parser_unary_expression (parser);
34555 if (lhs1 == error_mark_node)
34556 goto saw_error;
34558 if (structured_block)
34560 cp_parser_consume_semicolon_at_end_of_statement (parser);
34561 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34563 done:
34564 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34565 if (!structured_block)
34566 cp_parser_consume_semicolon_at_end_of_statement (parser);
34567 return;
34569 saw_error:
34570 cp_parser_skip_to_end_of_block_or_statement (parser);
34571 if (structured_block)
34573 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34574 cp_lexer_consume_token (parser->lexer);
34575 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34577 cp_parser_skip_to_end_of_block_or_statement (parser);
34578 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34579 cp_lexer_consume_token (parser->lexer);
34585 /* OpenMP 2.5:
34586 # pragma omp barrier new-line */
34588 static void
34589 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34591 cp_parser_require_pragma_eol (parser, pragma_tok);
34592 finish_omp_barrier ();
34595 /* OpenMP 2.5:
34596 # pragma omp critical [(name)] new-line
34597 structured-block
34599 OpenMP 4.5:
34600 # pragma omp critical [(name) [hint(expression)]] new-line
34601 structured-block */
34603 #define OMP_CRITICAL_CLAUSE_MASK \
34604 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34606 static tree
34607 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34609 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34611 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34613 matching_parens parens;
34614 parens.consume_open (parser);
34616 name = cp_parser_identifier (parser);
34618 if (name == error_mark_node
34619 || !parens.require_close (parser))
34620 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34621 /*or_comma=*/false,
34622 /*consume_paren=*/true);
34623 if (name == error_mark_node)
34624 name = NULL;
34626 clauses = cp_parser_omp_all_clauses (parser,
34627 OMP_CRITICAL_CLAUSE_MASK,
34628 "#pragma omp critical", pragma_tok);
34630 else
34631 cp_parser_require_pragma_eol (parser, pragma_tok);
34633 stmt = cp_parser_omp_structured_block (parser, if_p);
34634 return c_finish_omp_critical (input_location, stmt, name, clauses);
34637 /* OpenMP 2.5:
34638 # pragma omp flush flush-vars[opt] new-line
34640 flush-vars:
34641 ( variable-list ) */
34643 static void
34644 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34647 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34648 cp_parser_require_pragma_eol (parser, pragma_tok);
34650 finish_omp_flush ();
34653 /* Helper function, to parse omp for increment expression. */
34655 static tree
34656 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34658 tree cond = cp_parser_binary_expression (parser, false, true,
34659 PREC_NOT_OPERATOR, NULL);
34660 if (cond == error_mark_node
34661 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34663 cp_parser_skip_to_end_of_statement (parser);
34664 return error_mark_node;
34667 switch (TREE_CODE (cond))
34669 case GT_EXPR:
34670 case GE_EXPR:
34671 case LT_EXPR:
34672 case LE_EXPR:
34673 break;
34674 case NE_EXPR:
34675 /* Fall through: OpenMP disallows NE_EXPR. */
34676 gcc_fallthrough ();
34677 default:
34678 return error_mark_node;
34681 /* If decl is an iterator, preserve LHS and RHS of the relational
34682 expr until finish_omp_for. */
34683 if (decl
34684 && (type_dependent_expression_p (decl)
34685 || CLASS_TYPE_P (TREE_TYPE (decl))))
34686 return cond;
34688 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34689 TREE_CODE (cond),
34690 TREE_OPERAND (cond, 0), ERROR_MARK,
34691 TREE_OPERAND (cond, 1), ERROR_MARK,
34692 /*overload=*/NULL, tf_warning_or_error);
34695 /* Helper function, to parse omp for increment expression. */
34697 static tree
34698 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34700 cp_token *token = cp_lexer_peek_token (parser->lexer);
34701 enum tree_code op;
34702 tree lhs, rhs;
34703 cp_id_kind idk;
34704 bool decl_first;
34706 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34708 op = (token->type == CPP_PLUS_PLUS
34709 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34710 cp_lexer_consume_token (parser->lexer);
34711 lhs = cp_parser_simple_cast_expression (parser);
34712 if (lhs != decl
34713 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34714 return error_mark_node;
34715 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34718 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34719 if (lhs != decl
34720 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34721 return error_mark_node;
34723 token = cp_lexer_peek_token (parser->lexer);
34724 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34726 op = (token->type == CPP_PLUS_PLUS
34727 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34728 cp_lexer_consume_token (parser->lexer);
34729 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34732 op = cp_parser_assignment_operator_opt (parser);
34733 if (op == ERROR_MARK)
34734 return error_mark_node;
34736 if (op != NOP_EXPR)
34738 rhs = cp_parser_assignment_expression (parser);
34739 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34740 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34743 lhs = cp_parser_binary_expression (parser, false, false,
34744 PREC_ADDITIVE_EXPRESSION, NULL);
34745 token = cp_lexer_peek_token (parser->lexer);
34746 decl_first = (lhs == decl
34747 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34748 if (decl_first)
34749 lhs = NULL_TREE;
34750 if (token->type != CPP_PLUS
34751 && token->type != CPP_MINUS)
34752 return error_mark_node;
34756 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34757 cp_lexer_consume_token (parser->lexer);
34758 rhs = cp_parser_binary_expression (parser, false, false,
34759 PREC_ADDITIVE_EXPRESSION, NULL);
34760 token = cp_lexer_peek_token (parser->lexer);
34761 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34763 if (lhs == NULL_TREE)
34765 if (op == PLUS_EXPR)
34766 lhs = rhs;
34767 else
34768 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34769 tf_warning_or_error);
34771 else
34772 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34773 ERROR_MARK, NULL, tf_warning_or_error);
34776 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34778 if (!decl_first)
34780 if ((rhs != decl
34781 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34782 || op == MINUS_EXPR)
34783 return error_mark_node;
34784 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34786 else
34787 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34789 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34792 /* Parse the initialization statement of an OpenMP for loop.
34794 Return true if the resulting construct should have an
34795 OMP_CLAUSE_PRIVATE added to it. */
34797 static tree
34798 cp_parser_omp_for_loop_init (cp_parser *parser,
34799 tree &this_pre_body,
34800 vec<tree, va_gc> *for_block,
34801 tree &init,
34802 tree &orig_init,
34803 tree &decl,
34804 tree &real_decl)
34806 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34807 return NULL_TREE;
34809 tree add_private_clause = NULL_TREE;
34811 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34813 init-expr:
34814 var = lb
34815 integer-type var = lb
34816 random-access-iterator-type var = lb
34817 pointer-type var = lb
34819 cp_decl_specifier_seq type_specifiers;
34821 /* First, try to parse as an initialized declaration. See
34822 cp_parser_condition, from whence the bulk of this is copied. */
34824 cp_parser_parse_tentatively (parser);
34825 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34826 /*is_trailing_return=*/false,
34827 &type_specifiers);
34828 if (cp_parser_parse_definitely (parser))
34830 /* If parsing a type specifier seq succeeded, then this
34831 MUST be a initialized declaration. */
34832 tree asm_specification, attributes;
34833 cp_declarator *declarator;
34835 declarator = cp_parser_declarator (parser,
34836 CP_PARSER_DECLARATOR_NAMED,
34837 /*ctor_dtor_or_conv_p=*/NULL,
34838 /*parenthesized_p=*/NULL,
34839 /*member_p=*/false,
34840 /*friend_p=*/false);
34841 attributes = cp_parser_attributes_opt (parser);
34842 asm_specification = cp_parser_asm_specification_opt (parser);
34844 if (declarator == cp_error_declarator)
34845 cp_parser_skip_to_end_of_statement (parser);
34847 else
34849 tree pushed_scope, auto_node;
34851 decl = start_decl (declarator, &type_specifiers,
34852 SD_INITIALIZED, attributes,
34853 /*prefix_attributes=*/NULL_TREE,
34854 &pushed_scope);
34856 auto_node = type_uses_auto (TREE_TYPE (decl));
34857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34859 if (cp_lexer_next_token_is (parser->lexer,
34860 CPP_OPEN_PAREN))
34861 error ("parenthesized initialization is not allowed in "
34862 "OpenMP %<for%> loop");
34863 else
34864 /* Trigger an error. */
34865 cp_parser_require (parser, CPP_EQ, RT_EQ);
34867 init = error_mark_node;
34868 cp_parser_skip_to_end_of_statement (parser);
34870 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34871 || type_dependent_expression_p (decl)
34872 || auto_node)
34874 bool is_direct_init, is_non_constant_init;
34876 init = cp_parser_initializer (parser,
34877 &is_direct_init,
34878 &is_non_constant_init);
34880 if (auto_node)
34882 TREE_TYPE (decl)
34883 = do_auto_deduction (TREE_TYPE (decl), init,
34884 auto_node);
34886 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34887 && !type_dependent_expression_p (decl))
34888 goto non_class;
34891 cp_finish_decl (decl, init, !is_non_constant_init,
34892 asm_specification,
34893 LOOKUP_ONLYCONVERTING);
34894 orig_init = init;
34895 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34897 vec_safe_push (for_block, this_pre_body);
34898 init = NULL_TREE;
34900 else
34902 init = pop_stmt_list (this_pre_body);
34903 if (init && TREE_CODE (init) == STATEMENT_LIST)
34905 tree_stmt_iterator i = tsi_start (init);
34906 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34907 while (!tsi_end_p (i))
34909 tree t = tsi_stmt (i);
34910 if (TREE_CODE (t) == DECL_EXPR
34911 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34913 tsi_delink (&i);
34914 vec_safe_push (for_block, t);
34915 continue;
34917 break;
34919 if (tsi_one_before_end_p (i))
34921 tree t = tsi_stmt (i);
34922 tsi_delink (&i);
34923 free_stmt_list (init);
34924 init = t;
34928 this_pre_body = NULL_TREE;
34930 else
34932 /* Consume '='. */
34933 cp_lexer_consume_token (parser->lexer);
34934 init = cp_parser_assignment_expression (parser);
34936 non_class:
34937 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34938 init = error_mark_node;
34939 else
34940 cp_finish_decl (decl, NULL_TREE,
34941 /*init_const_expr_p=*/false,
34942 asm_specification,
34943 LOOKUP_ONLYCONVERTING);
34946 if (pushed_scope)
34947 pop_scope (pushed_scope);
34950 else
34952 cp_id_kind idk;
34953 /* If parsing a type specifier sequence failed, then
34954 this MUST be a simple expression. */
34955 cp_parser_parse_tentatively (parser);
34956 decl = cp_parser_primary_expression (parser, false, false,
34957 false, &idk);
34958 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34959 if (!cp_parser_error_occurred (parser)
34960 && decl
34961 && (TREE_CODE (decl) == COMPONENT_REF
34962 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34964 cp_parser_abort_tentative_parse (parser);
34965 cp_parser_parse_tentatively (parser);
34966 cp_token *token = cp_lexer_peek_token (parser->lexer);
34967 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34968 /*check_dependency_p=*/true,
34969 /*template_p=*/NULL,
34970 /*declarator_p=*/false,
34971 /*optional_p=*/false);
34972 if (name != error_mark_node
34973 && last_tok == cp_lexer_peek_token (parser->lexer))
34975 decl = cp_parser_lookup_name_simple (parser, name,
34976 token->location);
34977 if (TREE_CODE (decl) == FIELD_DECL)
34978 add_private_clause = omp_privatize_field (decl, false);
34980 cp_parser_abort_tentative_parse (parser);
34981 cp_parser_parse_tentatively (parser);
34982 decl = cp_parser_primary_expression (parser, false, false,
34983 false, &idk);
34985 if (!cp_parser_error_occurred (parser)
34986 && decl
34987 && DECL_P (decl)
34988 && CLASS_TYPE_P (TREE_TYPE (decl)))
34990 tree rhs;
34992 cp_parser_parse_definitely (parser);
34993 cp_parser_require (parser, CPP_EQ, RT_EQ);
34994 rhs = cp_parser_assignment_expression (parser);
34995 orig_init = rhs;
34996 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34997 decl, NOP_EXPR,
34998 rhs,
34999 tf_warning_or_error));
35000 if (!add_private_clause)
35001 add_private_clause = decl;
35003 else
35005 decl = NULL;
35006 cp_parser_abort_tentative_parse (parser);
35007 init = cp_parser_expression (parser);
35008 if (init)
35010 if (TREE_CODE (init) == MODIFY_EXPR
35011 || TREE_CODE (init) == MODOP_EXPR)
35012 real_decl = TREE_OPERAND (init, 0);
35016 return add_private_clause;
35019 /* Parse the restricted form of the for statement allowed by OpenMP. */
35021 static tree
35022 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35023 tree *cclauses, bool *if_p)
35025 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35026 tree real_decl, initv, condv, incrv, declv;
35027 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35028 location_t loc_first;
35029 bool collapse_err = false;
35030 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35031 vec<tree, va_gc> *for_block = make_tree_vector ();
35032 auto_vec<tree, 4> orig_inits;
35033 bool tiling = false;
35035 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35036 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35037 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35038 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35040 tiling = true;
35041 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35043 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35044 && OMP_CLAUSE_ORDERED_EXPR (cl))
35046 ordered_cl = cl;
35047 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35050 if (ordered && ordered < collapse)
35052 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35053 "%<ordered%> clause parameter is less than %<collapse%>");
35054 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35055 = build_int_cst (NULL_TREE, collapse);
35056 ordered = collapse;
35058 if (ordered)
35060 for (tree *pc = &clauses; *pc; )
35061 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35063 error_at (OMP_CLAUSE_LOCATION (*pc),
35064 "%<linear%> clause may not be specified together "
35065 "with %<ordered%> clause with a parameter");
35066 *pc = OMP_CLAUSE_CHAIN (*pc);
35068 else
35069 pc = &OMP_CLAUSE_CHAIN (*pc);
35072 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35073 count = ordered ? ordered : collapse;
35075 declv = make_tree_vec (count);
35076 initv = make_tree_vec (count);
35077 condv = make_tree_vec (count);
35078 incrv = make_tree_vec (count);
35080 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35082 for (i = 0; i < count; i++)
35084 int bracecount = 0;
35085 tree add_private_clause = NULL_TREE;
35086 location_t loc;
35088 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35090 if (!collapse_err)
35091 cp_parser_error (parser, "for statement expected");
35092 return NULL;
35094 loc = cp_lexer_consume_token (parser->lexer)->location;
35096 matching_parens parens;
35097 if (!parens.require_open (parser))
35098 return NULL;
35100 init = orig_init = decl = real_decl = NULL;
35101 this_pre_body = push_stmt_list ();
35103 add_private_clause
35104 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35105 init, orig_init, decl, real_decl);
35107 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35108 if (this_pre_body)
35110 this_pre_body = pop_stmt_list (this_pre_body);
35111 if (pre_body)
35113 tree t = pre_body;
35114 pre_body = push_stmt_list ();
35115 add_stmt (t);
35116 add_stmt (this_pre_body);
35117 pre_body = pop_stmt_list (pre_body);
35119 else
35120 pre_body = this_pre_body;
35123 if (decl)
35124 real_decl = decl;
35125 if (cclauses != NULL
35126 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35127 && real_decl != NULL_TREE)
35129 tree *c;
35130 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35131 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35132 && OMP_CLAUSE_DECL (*c) == real_decl)
35134 error_at (loc, "iteration variable %qD"
35135 " should not be firstprivate", real_decl);
35136 *c = OMP_CLAUSE_CHAIN (*c);
35138 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35139 && OMP_CLAUSE_DECL (*c) == real_decl)
35141 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35142 tree l = *c;
35143 *c = OMP_CLAUSE_CHAIN (*c);
35144 if (code == OMP_SIMD)
35146 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35147 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35149 else
35151 OMP_CLAUSE_CHAIN (l) = clauses;
35152 clauses = l;
35154 add_private_clause = NULL_TREE;
35156 else
35158 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35159 && OMP_CLAUSE_DECL (*c) == real_decl)
35160 add_private_clause = NULL_TREE;
35161 c = &OMP_CLAUSE_CHAIN (*c);
35165 if (add_private_clause)
35167 tree c;
35168 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35170 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35171 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35172 && OMP_CLAUSE_DECL (c) == decl)
35173 break;
35174 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35175 && OMP_CLAUSE_DECL (c) == decl)
35176 error_at (loc, "iteration variable %qD "
35177 "should not be firstprivate",
35178 decl);
35179 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35180 && OMP_CLAUSE_DECL (c) == decl)
35181 error_at (loc, "iteration variable %qD should not be reduction",
35182 decl);
35184 if (c == NULL)
35186 if (code != OMP_SIMD)
35187 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35188 else if (collapse == 1)
35189 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35190 else
35191 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35192 OMP_CLAUSE_DECL (c) = add_private_clause;
35193 c = finish_omp_clauses (c, C_ORT_OMP);
35194 if (c)
35196 OMP_CLAUSE_CHAIN (c) = clauses;
35197 clauses = c;
35198 /* For linear, signal that we need to fill up
35199 the so far unknown linear step. */
35200 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35201 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35206 cond = NULL;
35207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35208 cond = cp_parser_omp_for_cond (parser, decl);
35209 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35211 incr = NULL;
35212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35214 /* If decl is an iterator, preserve the operator on decl
35215 until finish_omp_for. */
35216 if (real_decl
35217 && ((processing_template_decl
35218 && (TREE_TYPE (real_decl) == NULL_TREE
35219 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35220 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35221 incr = cp_parser_omp_for_incr (parser, real_decl);
35222 else
35223 incr = cp_parser_expression (parser);
35224 if (!EXPR_HAS_LOCATION (incr))
35225 protected_set_expr_location (incr, input_location);
35228 if (!parens.require_close (parser))
35229 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35230 /*or_comma=*/false,
35231 /*consume_paren=*/true);
35233 TREE_VEC_ELT (declv, i) = decl;
35234 TREE_VEC_ELT (initv, i) = init;
35235 TREE_VEC_ELT (condv, i) = cond;
35236 TREE_VEC_ELT (incrv, i) = incr;
35237 if (orig_init)
35239 orig_inits.safe_grow_cleared (i + 1);
35240 orig_inits[i] = orig_init;
35243 if (i == count - 1)
35244 break;
35246 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35247 in between the collapsed for loops to be still considered perfectly
35248 nested. Hopefully the final version clarifies this.
35249 For now handle (multiple) {'s and empty statements. */
35250 cp_parser_parse_tentatively (parser);
35251 for (;;)
35253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35254 break;
35255 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35257 cp_lexer_consume_token (parser->lexer);
35258 bracecount++;
35260 else if (bracecount
35261 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35262 cp_lexer_consume_token (parser->lexer);
35263 else
35265 loc = cp_lexer_peek_token (parser->lexer)->location;
35266 error_at (loc, "not enough for loops to collapse");
35267 collapse_err = true;
35268 cp_parser_abort_tentative_parse (parser);
35269 declv = NULL_TREE;
35270 break;
35274 if (declv)
35276 cp_parser_parse_definitely (parser);
35277 nbraces += bracecount;
35281 if (nbraces)
35282 if_p = NULL;
35284 /* Note that we saved the original contents of this flag when we entered
35285 the structured block, and so we don't need to re-save it here. */
35286 parser->in_statement = IN_OMP_FOR;
35288 /* Note that the grammar doesn't call for a structured block here,
35289 though the loop as a whole is a structured block. */
35290 body = push_stmt_list ();
35291 cp_parser_statement (parser, NULL_TREE, false, if_p);
35292 body = pop_stmt_list (body);
35294 if (declv == NULL_TREE)
35295 ret = NULL_TREE;
35296 else
35297 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35298 body, pre_body, &orig_inits, clauses);
35300 while (nbraces)
35302 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35304 cp_lexer_consume_token (parser->lexer);
35305 nbraces--;
35307 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35308 cp_lexer_consume_token (parser->lexer);
35309 else
35311 if (!collapse_err)
35313 error_at (cp_lexer_peek_token (parser->lexer)->location,
35314 "collapsed loops not perfectly nested");
35316 collapse_err = true;
35317 cp_parser_statement_seq_opt (parser, NULL);
35318 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35319 break;
35323 while (!for_block->is_empty ())
35325 tree t = for_block->pop ();
35326 if (TREE_CODE (t) == STATEMENT_LIST)
35327 add_stmt (pop_stmt_list (t));
35328 else
35329 add_stmt (t);
35331 release_tree_vector (for_block);
35333 return ret;
35336 /* Helper function for OpenMP parsing, split clauses and call
35337 finish_omp_clauses on each of the set of clauses afterwards. */
35339 static void
35340 cp_omp_split_clauses (location_t loc, enum tree_code code,
35341 omp_clause_mask mask, tree clauses, tree *cclauses)
35343 int i;
35344 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35345 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35346 if (cclauses[i])
35347 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35350 /* OpenMP 4.0:
35351 #pragma omp simd simd-clause[optseq] new-line
35352 for-loop */
35354 #define OMP_SIMD_CLAUSE_MASK \
35355 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35364 static tree
35365 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35366 char *p_name, omp_clause_mask mask, tree *cclauses,
35367 bool *if_p)
35369 tree clauses, sb, ret;
35370 unsigned int save;
35371 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35373 strcat (p_name, " simd");
35374 mask |= OMP_SIMD_CLAUSE_MASK;
35376 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35377 cclauses == NULL);
35378 if (cclauses)
35380 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35381 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35382 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35383 OMP_CLAUSE_ORDERED);
35384 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35386 error_at (OMP_CLAUSE_LOCATION (c),
35387 "%<ordered%> clause with parameter may not be specified "
35388 "on %qs construct", p_name);
35389 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35393 sb = begin_omp_structured_block ();
35394 save = cp_parser_begin_omp_structured_block (parser);
35396 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35398 cp_parser_end_omp_structured_block (parser, save);
35399 add_stmt (finish_omp_structured_block (sb));
35401 return ret;
35404 /* OpenMP 2.5:
35405 #pragma omp for for-clause[optseq] new-line
35406 for-loop
35408 OpenMP 4.0:
35409 #pragma omp for simd for-simd-clause[optseq] new-line
35410 for-loop */
35412 #define OMP_FOR_CLAUSE_MASK \
35413 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35423 static tree
35424 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35425 char *p_name, omp_clause_mask mask, tree *cclauses,
35426 bool *if_p)
35428 tree clauses, sb, ret;
35429 unsigned int save;
35430 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35432 strcat (p_name, " for");
35433 mask |= OMP_FOR_CLAUSE_MASK;
35434 /* parallel for{, simd} disallows nowait clause, but for
35435 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35436 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35437 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35438 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35439 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35440 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35442 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35444 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35445 const char *p = IDENTIFIER_POINTER (id);
35447 if (strcmp (p, "simd") == 0)
35449 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35450 if (cclauses == NULL)
35451 cclauses = cclauses_buf;
35453 cp_lexer_consume_token (parser->lexer);
35454 if (!flag_openmp) /* flag_openmp_simd */
35455 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35456 cclauses, if_p);
35457 sb = begin_omp_structured_block ();
35458 save = cp_parser_begin_omp_structured_block (parser);
35459 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35460 cclauses, if_p);
35461 cp_parser_end_omp_structured_block (parser, save);
35462 tree body = finish_omp_structured_block (sb);
35463 if (ret == NULL)
35464 return ret;
35465 ret = make_node (OMP_FOR);
35466 TREE_TYPE (ret) = void_type_node;
35467 OMP_FOR_BODY (ret) = body;
35468 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35469 SET_EXPR_LOCATION (ret, loc);
35470 add_stmt (ret);
35471 return ret;
35474 if (!flag_openmp) /* flag_openmp_simd */
35476 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35477 return NULL_TREE;
35480 /* Composite distribute parallel for disallows linear clause. */
35481 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35482 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35484 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35485 cclauses == NULL);
35486 if (cclauses)
35488 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35489 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35492 sb = begin_omp_structured_block ();
35493 save = cp_parser_begin_omp_structured_block (parser);
35495 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35497 cp_parser_end_omp_structured_block (parser, save);
35498 add_stmt (finish_omp_structured_block (sb));
35500 return ret;
35503 /* OpenMP 2.5:
35504 # pragma omp master new-line
35505 structured-block */
35507 static tree
35508 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35510 cp_parser_require_pragma_eol (parser, pragma_tok);
35511 return c_finish_omp_master (input_location,
35512 cp_parser_omp_structured_block (parser, if_p));
35515 /* OpenMP 2.5:
35516 # pragma omp ordered new-line
35517 structured-block
35519 OpenMP 4.5:
35520 # pragma omp ordered ordered-clauses new-line
35521 structured-block */
35523 #define OMP_ORDERED_CLAUSE_MASK \
35524 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35527 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35528 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35530 static bool
35531 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35532 enum pragma_context context, bool *if_p)
35534 location_t loc = pragma_tok->location;
35536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35538 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35539 const char *p = IDENTIFIER_POINTER (id);
35541 if (strcmp (p, "depend") == 0)
35543 if (!flag_openmp) /* flag_openmp_simd */
35545 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35546 return false;
35548 if (context == pragma_stmt)
35550 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35551 "%<depend%> clause may only be used in compound "
35552 "statements");
35553 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35554 return false;
35556 tree clauses
35557 = cp_parser_omp_all_clauses (parser,
35558 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35559 "#pragma omp ordered", pragma_tok);
35560 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35561 return false;
35565 tree clauses
35566 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35567 "#pragma omp ordered", pragma_tok);
35569 if (!flag_openmp /* flag_openmp_simd */
35570 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35571 return false;
35573 c_finish_omp_ordered (loc, clauses,
35574 cp_parser_omp_structured_block (parser, if_p));
35575 return true;
35578 /* OpenMP 2.5:
35580 section-scope:
35581 { section-sequence }
35583 section-sequence:
35584 section-directive[opt] structured-block
35585 section-sequence section-directive structured-block */
35587 static tree
35588 cp_parser_omp_sections_scope (cp_parser *parser)
35590 tree stmt, substmt;
35591 bool error_suppress = false;
35592 cp_token *tok;
35594 matching_braces braces;
35595 if (!braces.require_open (parser))
35596 return NULL_TREE;
35598 stmt = push_stmt_list ();
35600 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35601 != PRAGMA_OMP_SECTION)
35603 substmt = cp_parser_omp_structured_block (parser, NULL);
35604 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35605 add_stmt (substmt);
35608 while (1)
35610 tok = cp_lexer_peek_token (parser->lexer);
35611 if (tok->type == CPP_CLOSE_BRACE)
35612 break;
35613 if (tok->type == CPP_EOF)
35614 break;
35616 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35618 cp_lexer_consume_token (parser->lexer);
35619 cp_parser_require_pragma_eol (parser, tok);
35620 error_suppress = false;
35622 else if (!error_suppress)
35624 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35625 error_suppress = true;
35628 substmt = cp_parser_omp_structured_block (parser, NULL);
35629 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35630 add_stmt (substmt);
35632 braces.require_close (parser);
35634 substmt = pop_stmt_list (stmt);
35636 stmt = make_node (OMP_SECTIONS);
35637 TREE_TYPE (stmt) = void_type_node;
35638 OMP_SECTIONS_BODY (stmt) = substmt;
35640 add_stmt (stmt);
35641 return stmt;
35644 /* OpenMP 2.5:
35645 # pragma omp sections sections-clause[optseq] newline
35646 sections-scope */
35648 #define OMP_SECTIONS_CLAUSE_MASK \
35649 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35655 static tree
35656 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35657 char *p_name, omp_clause_mask mask, tree *cclauses)
35659 tree clauses, ret;
35660 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35662 strcat (p_name, " sections");
35663 mask |= OMP_SECTIONS_CLAUSE_MASK;
35664 if (cclauses)
35665 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35667 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35668 cclauses == NULL);
35669 if (cclauses)
35671 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35672 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35675 ret = cp_parser_omp_sections_scope (parser);
35676 if (ret)
35677 OMP_SECTIONS_CLAUSES (ret) = clauses;
35679 return ret;
35682 /* OpenMP 2.5:
35683 # pragma omp parallel parallel-clause[optseq] new-line
35684 structured-block
35685 # pragma omp parallel for parallel-for-clause[optseq] new-line
35686 structured-block
35687 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35688 structured-block
35690 OpenMP 4.0:
35691 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35692 structured-block */
35694 #define OMP_PARALLEL_CLAUSE_MASK \
35695 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35705 static tree
35706 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35707 char *p_name, omp_clause_mask mask, tree *cclauses,
35708 bool *if_p)
35710 tree stmt, clauses, block;
35711 unsigned int save;
35712 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35714 strcat (p_name, " parallel");
35715 mask |= OMP_PARALLEL_CLAUSE_MASK;
35716 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35717 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35718 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35719 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35721 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35723 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35724 if (cclauses == NULL)
35725 cclauses = cclauses_buf;
35727 cp_lexer_consume_token (parser->lexer);
35728 if (!flag_openmp) /* flag_openmp_simd */
35729 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35730 if_p);
35731 block = begin_omp_parallel ();
35732 save = cp_parser_begin_omp_structured_block (parser);
35733 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35734 if_p);
35735 cp_parser_end_omp_structured_block (parser, save);
35736 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35737 block);
35738 if (ret == NULL_TREE)
35739 return ret;
35740 OMP_PARALLEL_COMBINED (stmt) = 1;
35741 return stmt;
35743 /* When combined with distribute, parallel has to be followed by for.
35744 #pragma omp target parallel is allowed though. */
35745 else if (cclauses
35746 && (mask & (OMP_CLAUSE_MASK_1
35747 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35749 error_at (loc, "expected %<for%> after %qs", p_name);
35750 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35751 return NULL_TREE;
35753 else if (!flag_openmp) /* flag_openmp_simd */
35755 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35756 return NULL_TREE;
35758 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35760 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35761 const char *p = IDENTIFIER_POINTER (id);
35762 if (strcmp (p, "sections") == 0)
35764 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35765 cclauses = cclauses_buf;
35767 cp_lexer_consume_token (parser->lexer);
35768 block = begin_omp_parallel ();
35769 save = cp_parser_begin_omp_structured_block (parser);
35770 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35771 cp_parser_end_omp_structured_block (parser, save);
35772 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35773 block);
35774 OMP_PARALLEL_COMBINED (stmt) = 1;
35775 return stmt;
35779 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35780 cclauses == NULL);
35781 if (cclauses)
35783 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35784 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35787 block = begin_omp_parallel ();
35788 save = cp_parser_begin_omp_structured_block (parser);
35789 cp_parser_statement (parser, NULL_TREE, false, if_p);
35790 cp_parser_end_omp_structured_block (parser, save);
35791 stmt = finish_omp_parallel (clauses, block);
35792 return stmt;
35795 /* OpenMP 2.5:
35796 # pragma omp single single-clause[optseq] new-line
35797 structured-block */
35799 #define OMP_SINGLE_CLAUSE_MASK \
35800 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35805 static tree
35806 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35808 tree stmt = make_node (OMP_SINGLE);
35809 TREE_TYPE (stmt) = void_type_node;
35811 OMP_SINGLE_CLAUSES (stmt)
35812 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35813 "#pragma omp single", pragma_tok);
35814 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35816 return add_stmt (stmt);
35819 /* OpenMP 3.0:
35820 # pragma omp task task-clause[optseq] new-line
35821 structured-block */
35823 #define OMP_TASK_CLAUSE_MASK \
35824 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35835 static tree
35836 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35838 tree clauses, block;
35839 unsigned int save;
35841 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35842 "#pragma omp task", pragma_tok);
35843 block = begin_omp_task ();
35844 save = cp_parser_begin_omp_structured_block (parser);
35845 cp_parser_statement (parser, NULL_TREE, false, if_p);
35846 cp_parser_end_omp_structured_block (parser, save);
35847 return finish_omp_task (clauses, block);
35850 /* OpenMP 3.0:
35851 # pragma omp taskwait new-line */
35853 static void
35854 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35856 cp_parser_require_pragma_eol (parser, pragma_tok);
35857 finish_omp_taskwait ();
35860 /* OpenMP 3.1:
35861 # pragma omp taskyield new-line */
35863 static void
35864 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35866 cp_parser_require_pragma_eol (parser, pragma_tok);
35867 finish_omp_taskyield ();
35870 /* OpenMP 4.0:
35871 # pragma omp taskgroup new-line
35872 structured-block */
35874 static tree
35875 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35877 cp_parser_require_pragma_eol (parser, pragma_tok);
35878 return c_finish_omp_taskgroup (input_location,
35879 cp_parser_omp_structured_block (parser,
35880 if_p));
35884 /* OpenMP 2.5:
35885 # pragma omp threadprivate (variable-list) */
35887 static void
35888 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35890 tree vars;
35892 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35893 cp_parser_require_pragma_eol (parser, pragma_tok);
35895 finish_omp_threadprivate (vars);
35898 /* OpenMP 4.0:
35899 # pragma omp cancel cancel-clause[optseq] new-line */
35901 #define OMP_CANCEL_CLAUSE_MASK \
35902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35908 static void
35909 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35911 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35912 "#pragma omp cancel", pragma_tok);
35913 finish_omp_cancel (clauses);
35916 /* OpenMP 4.0:
35917 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35919 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35925 static void
35926 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35927 enum pragma_context context)
35929 tree clauses;
35930 bool point_seen = false;
35932 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35934 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35935 const char *p = IDENTIFIER_POINTER (id);
35937 if (strcmp (p, "point") == 0)
35939 cp_lexer_consume_token (parser->lexer);
35940 point_seen = true;
35943 if (!point_seen)
35945 cp_parser_error (parser, "expected %<point%>");
35946 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35947 return;
35950 if (context != pragma_compound)
35952 if (context == pragma_stmt)
35953 error_at (pragma_tok->location,
35954 "%<#pragma %s%> may only be used in compound statements",
35955 "omp cancellation point");
35956 else
35957 cp_parser_error (parser, "expected declaration specifiers");
35958 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35959 return;
35962 clauses = cp_parser_omp_all_clauses (parser,
35963 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35964 "#pragma omp cancellation point",
35965 pragma_tok);
35966 finish_omp_cancellation_point (clauses);
35969 /* OpenMP 4.0:
35970 #pragma omp distribute distribute-clause[optseq] new-line
35971 for-loop */
35973 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35974 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35980 static tree
35981 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35982 char *p_name, omp_clause_mask mask, tree *cclauses,
35983 bool *if_p)
35985 tree clauses, sb, ret;
35986 unsigned int save;
35987 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35989 strcat (p_name, " distribute");
35990 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35992 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35994 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35995 const char *p = IDENTIFIER_POINTER (id);
35996 bool simd = false;
35997 bool parallel = false;
35999 if (strcmp (p, "simd") == 0)
36000 simd = true;
36001 else
36002 parallel = strcmp (p, "parallel") == 0;
36003 if (parallel || simd)
36005 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36006 if (cclauses == NULL)
36007 cclauses = cclauses_buf;
36008 cp_lexer_consume_token (parser->lexer);
36009 if (!flag_openmp) /* flag_openmp_simd */
36011 if (simd)
36012 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36013 cclauses, if_p);
36014 else
36015 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36016 cclauses, if_p);
36018 sb = begin_omp_structured_block ();
36019 save = cp_parser_begin_omp_structured_block (parser);
36020 if (simd)
36021 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36022 cclauses, if_p);
36023 else
36024 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36025 cclauses, if_p);
36026 cp_parser_end_omp_structured_block (parser, save);
36027 tree body = finish_omp_structured_block (sb);
36028 if (ret == NULL)
36029 return ret;
36030 ret = make_node (OMP_DISTRIBUTE);
36031 TREE_TYPE (ret) = void_type_node;
36032 OMP_FOR_BODY (ret) = body;
36033 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36034 SET_EXPR_LOCATION (ret, loc);
36035 add_stmt (ret);
36036 return ret;
36039 if (!flag_openmp) /* flag_openmp_simd */
36041 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36042 return NULL_TREE;
36045 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36046 cclauses == NULL);
36047 if (cclauses)
36049 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36050 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36053 sb = begin_omp_structured_block ();
36054 save = cp_parser_begin_omp_structured_block (parser);
36056 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36058 cp_parser_end_omp_structured_block (parser, save);
36059 add_stmt (finish_omp_structured_block (sb));
36061 return ret;
36064 /* OpenMP 4.0:
36065 # pragma omp teams teams-clause[optseq] new-line
36066 structured-block */
36068 #define OMP_TEAMS_CLAUSE_MASK \
36069 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36077 static tree
36078 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36079 char *p_name, omp_clause_mask mask, tree *cclauses,
36080 bool *if_p)
36082 tree clauses, sb, ret;
36083 unsigned int save;
36084 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36086 strcat (p_name, " teams");
36087 mask |= OMP_TEAMS_CLAUSE_MASK;
36089 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36091 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36092 const char *p = IDENTIFIER_POINTER (id);
36093 if (strcmp (p, "distribute") == 0)
36095 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36096 if (cclauses == NULL)
36097 cclauses = cclauses_buf;
36099 cp_lexer_consume_token (parser->lexer);
36100 if (!flag_openmp) /* flag_openmp_simd */
36101 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36102 cclauses, if_p);
36103 sb = begin_omp_structured_block ();
36104 save = cp_parser_begin_omp_structured_block (parser);
36105 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36106 cclauses, if_p);
36107 cp_parser_end_omp_structured_block (parser, save);
36108 tree body = finish_omp_structured_block (sb);
36109 if (ret == NULL)
36110 return ret;
36111 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36112 ret = make_node (OMP_TEAMS);
36113 TREE_TYPE (ret) = void_type_node;
36114 OMP_TEAMS_CLAUSES (ret) = clauses;
36115 OMP_TEAMS_BODY (ret) = body;
36116 OMP_TEAMS_COMBINED (ret) = 1;
36117 SET_EXPR_LOCATION (ret, loc);
36118 return add_stmt (ret);
36121 if (!flag_openmp) /* flag_openmp_simd */
36123 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36124 return NULL_TREE;
36127 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36128 cclauses == NULL);
36129 if (cclauses)
36131 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36132 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36135 tree stmt = make_node (OMP_TEAMS);
36136 TREE_TYPE (stmt) = void_type_node;
36137 OMP_TEAMS_CLAUSES (stmt) = clauses;
36138 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36139 SET_EXPR_LOCATION (stmt, loc);
36141 return add_stmt (stmt);
36144 /* OpenMP 4.0:
36145 # pragma omp target data target-data-clause[optseq] new-line
36146 structured-block */
36148 #define OMP_TARGET_DATA_CLAUSE_MASK \
36149 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36154 static tree
36155 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36157 tree clauses
36158 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36159 "#pragma omp target data", pragma_tok);
36160 int map_seen = 0;
36161 for (tree *pc = &clauses; *pc;)
36163 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36164 switch (OMP_CLAUSE_MAP_KIND (*pc))
36166 case GOMP_MAP_TO:
36167 case GOMP_MAP_ALWAYS_TO:
36168 case GOMP_MAP_FROM:
36169 case GOMP_MAP_ALWAYS_FROM:
36170 case GOMP_MAP_TOFROM:
36171 case GOMP_MAP_ALWAYS_TOFROM:
36172 case GOMP_MAP_ALLOC:
36173 map_seen = 3;
36174 break;
36175 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36176 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36177 case GOMP_MAP_ALWAYS_POINTER:
36178 break;
36179 default:
36180 map_seen |= 1;
36181 error_at (OMP_CLAUSE_LOCATION (*pc),
36182 "%<#pragma omp target data%> with map-type other "
36183 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36184 "on %<map%> clause");
36185 *pc = OMP_CLAUSE_CHAIN (*pc);
36186 continue;
36188 pc = &OMP_CLAUSE_CHAIN (*pc);
36191 if (map_seen != 3)
36193 if (map_seen == 0)
36194 error_at (pragma_tok->location,
36195 "%<#pragma omp target data%> must contain at least "
36196 "one %<map%> clause");
36197 return NULL_TREE;
36200 tree stmt = make_node (OMP_TARGET_DATA);
36201 TREE_TYPE (stmt) = void_type_node;
36202 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36204 keep_next_level (true);
36205 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36207 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36208 return add_stmt (stmt);
36211 /* OpenMP 4.5:
36212 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36213 structured-block */
36215 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36216 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36222 static tree
36223 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36224 enum pragma_context context)
36226 bool data_seen = false;
36227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36230 const char *p = IDENTIFIER_POINTER (id);
36232 if (strcmp (p, "data") == 0)
36234 cp_lexer_consume_token (parser->lexer);
36235 data_seen = true;
36238 if (!data_seen)
36240 cp_parser_error (parser, "expected %<data%>");
36241 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36242 return NULL_TREE;
36245 if (context == pragma_stmt)
36247 error_at (pragma_tok->location,
36248 "%<#pragma %s%> may only be used in compound statements",
36249 "omp target enter data");
36250 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36251 return NULL_TREE;
36254 tree clauses
36255 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36256 "#pragma omp target enter data", pragma_tok);
36257 int map_seen = 0;
36258 for (tree *pc = &clauses; *pc;)
36260 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36261 switch (OMP_CLAUSE_MAP_KIND (*pc))
36263 case GOMP_MAP_TO:
36264 case GOMP_MAP_ALWAYS_TO:
36265 case GOMP_MAP_ALLOC:
36266 map_seen = 3;
36267 break;
36268 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36269 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36270 case GOMP_MAP_ALWAYS_POINTER:
36271 break;
36272 default:
36273 map_seen |= 1;
36274 error_at (OMP_CLAUSE_LOCATION (*pc),
36275 "%<#pragma omp target enter data%> with map-type other "
36276 "than %<to%> or %<alloc%> on %<map%> clause");
36277 *pc = OMP_CLAUSE_CHAIN (*pc);
36278 continue;
36280 pc = &OMP_CLAUSE_CHAIN (*pc);
36283 if (map_seen != 3)
36285 if (map_seen == 0)
36286 error_at (pragma_tok->location,
36287 "%<#pragma omp target enter data%> must contain at least "
36288 "one %<map%> clause");
36289 return NULL_TREE;
36292 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36293 TREE_TYPE (stmt) = void_type_node;
36294 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36295 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36296 return add_stmt (stmt);
36299 /* OpenMP 4.5:
36300 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36301 structured-block */
36303 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36304 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36310 static tree
36311 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36312 enum pragma_context context)
36314 bool data_seen = false;
36315 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36317 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36318 const char *p = IDENTIFIER_POINTER (id);
36320 if (strcmp (p, "data") == 0)
36322 cp_lexer_consume_token (parser->lexer);
36323 data_seen = true;
36326 if (!data_seen)
36328 cp_parser_error (parser, "expected %<data%>");
36329 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36330 return NULL_TREE;
36333 if (context == pragma_stmt)
36335 error_at (pragma_tok->location,
36336 "%<#pragma %s%> may only be used in compound statements",
36337 "omp target exit data");
36338 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36339 return NULL_TREE;
36342 tree clauses
36343 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36344 "#pragma omp target exit data", pragma_tok);
36345 int map_seen = 0;
36346 for (tree *pc = &clauses; *pc;)
36348 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36349 switch (OMP_CLAUSE_MAP_KIND (*pc))
36351 case GOMP_MAP_FROM:
36352 case GOMP_MAP_ALWAYS_FROM:
36353 case GOMP_MAP_RELEASE:
36354 case GOMP_MAP_DELETE:
36355 map_seen = 3;
36356 break;
36357 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36358 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36359 case GOMP_MAP_ALWAYS_POINTER:
36360 break;
36361 default:
36362 map_seen |= 1;
36363 error_at (OMP_CLAUSE_LOCATION (*pc),
36364 "%<#pragma omp target exit data%> with map-type other "
36365 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36366 " clause");
36367 *pc = OMP_CLAUSE_CHAIN (*pc);
36368 continue;
36370 pc = &OMP_CLAUSE_CHAIN (*pc);
36373 if (map_seen != 3)
36375 if (map_seen == 0)
36376 error_at (pragma_tok->location,
36377 "%<#pragma omp target exit data%> must contain at least "
36378 "one %<map%> clause");
36379 return NULL_TREE;
36382 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36383 TREE_TYPE (stmt) = void_type_node;
36384 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36385 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36386 return add_stmt (stmt);
36389 /* OpenMP 4.0:
36390 # pragma omp target update target-update-clause[optseq] new-line */
36392 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36393 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36400 static bool
36401 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36402 enum pragma_context context)
36404 if (context == pragma_stmt)
36406 error_at (pragma_tok->location,
36407 "%<#pragma %s%> may only be used in compound statements",
36408 "omp target update");
36409 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36410 return false;
36413 tree clauses
36414 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36415 "#pragma omp target update", pragma_tok);
36416 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36417 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36419 error_at (pragma_tok->location,
36420 "%<#pragma omp target update%> must contain at least one "
36421 "%<from%> or %<to%> clauses");
36422 return false;
36425 tree stmt = make_node (OMP_TARGET_UPDATE);
36426 TREE_TYPE (stmt) = void_type_node;
36427 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36428 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36429 add_stmt (stmt);
36430 return false;
36433 /* OpenMP 4.0:
36434 # pragma omp target target-clause[optseq] new-line
36435 structured-block */
36437 #define OMP_TARGET_CLAUSE_MASK \
36438 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36448 static bool
36449 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36450 enum pragma_context context, bool *if_p)
36452 tree *pc = NULL, stmt;
36454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36456 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36457 const char *p = IDENTIFIER_POINTER (id);
36458 enum tree_code ccode = ERROR_MARK;
36460 if (strcmp (p, "teams") == 0)
36461 ccode = OMP_TEAMS;
36462 else if (strcmp (p, "parallel") == 0)
36463 ccode = OMP_PARALLEL;
36464 else if (strcmp (p, "simd") == 0)
36465 ccode = OMP_SIMD;
36466 if (ccode != ERROR_MARK)
36468 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36469 char p_name[sizeof ("#pragma omp target teams distribute "
36470 "parallel for simd")];
36472 cp_lexer_consume_token (parser->lexer);
36473 strcpy (p_name, "#pragma omp target");
36474 if (!flag_openmp) /* flag_openmp_simd */
36476 tree stmt;
36477 switch (ccode)
36479 case OMP_TEAMS:
36480 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36481 OMP_TARGET_CLAUSE_MASK,
36482 cclauses, if_p);
36483 break;
36484 case OMP_PARALLEL:
36485 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36486 OMP_TARGET_CLAUSE_MASK,
36487 cclauses, if_p);
36488 break;
36489 case OMP_SIMD:
36490 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36491 OMP_TARGET_CLAUSE_MASK,
36492 cclauses, if_p);
36493 break;
36494 default:
36495 gcc_unreachable ();
36497 return stmt != NULL_TREE;
36499 keep_next_level (true);
36500 tree sb = begin_omp_structured_block (), ret;
36501 unsigned save = cp_parser_begin_omp_structured_block (parser);
36502 switch (ccode)
36504 case OMP_TEAMS:
36505 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36506 OMP_TARGET_CLAUSE_MASK, cclauses,
36507 if_p);
36508 break;
36509 case OMP_PARALLEL:
36510 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36511 OMP_TARGET_CLAUSE_MASK, cclauses,
36512 if_p);
36513 break;
36514 case OMP_SIMD:
36515 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36516 OMP_TARGET_CLAUSE_MASK, cclauses,
36517 if_p);
36518 break;
36519 default:
36520 gcc_unreachable ();
36522 cp_parser_end_omp_structured_block (parser, save);
36523 tree body = finish_omp_structured_block (sb);
36524 if (ret == NULL_TREE)
36525 return false;
36526 if (ccode == OMP_TEAMS && !processing_template_decl)
36528 /* For combined target teams, ensure the num_teams and
36529 thread_limit clause expressions are evaluated on the host,
36530 before entering the target construct. */
36531 tree c;
36532 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36533 c; c = OMP_CLAUSE_CHAIN (c))
36534 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36535 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36536 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36538 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36539 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36540 if (expr == error_mark_node)
36541 continue;
36542 tree tmp = TARGET_EXPR_SLOT (expr);
36543 add_stmt (expr);
36544 OMP_CLAUSE_OPERAND (c, 0) = expr;
36545 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36546 OMP_CLAUSE_FIRSTPRIVATE);
36547 OMP_CLAUSE_DECL (tc) = tmp;
36548 OMP_CLAUSE_CHAIN (tc)
36549 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36550 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36553 tree stmt = make_node (OMP_TARGET);
36554 TREE_TYPE (stmt) = void_type_node;
36555 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36556 OMP_TARGET_BODY (stmt) = body;
36557 OMP_TARGET_COMBINED (stmt) = 1;
36558 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36559 add_stmt (stmt);
36560 pc = &OMP_TARGET_CLAUSES (stmt);
36561 goto check_clauses;
36563 else if (!flag_openmp) /* flag_openmp_simd */
36565 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36566 return false;
36568 else if (strcmp (p, "data") == 0)
36570 cp_lexer_consume_token (parser->lexer);
36571 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36572 return true;
36574 else if (strcmp (p, "enter") == 0)
36576 cp_lexer_consume_token (parser->lexer);
36577 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36578 return false;
36580 else if (strcmp (p, "exit") == 0)
36582 cp_lexer_consume_token (parser->lexer);
36583 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36584 return false;
36586 else if (strcmp (p, "update") == 0)
36588 cp_lexer_consume_token (parser->lexer);
36589 return cp_parser_omp_target_update (parser, pragma_tok, context);
36592 if (!flag_openmp) /* flag_openmp_simd */
36594 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36595 return false;
36598 stmt = make_node (OMP_TARGET);
36599 TREE_TYPE (stmt) = void_type_node;
36601 OMP_TARGET_CLAUSES (stmt)
36602 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36603 "#pragma omp target", pragma_tok);
36604 pc = &OMP_TARGET_CLAUSES (stmt);
36605 keep_next_level (true);
36606 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36608 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36609 add_stmt (stmt);
36611 check_clauses:
36612 while (*pc)
36614 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36615 switch (OMP_CLAUSE_MAP_KIND (*pc))
36617 case GOMP_MAP_TO:
36618 case GOMP_MAP_ALWAYS_TO:
36619 case GOMP_MAP_FROM:
36620 case GOMP_MAP_ALWAYS_FROM:
36621 case GOMP_MAP_TOFROM:
36622 case GOMP_MAP_ALWAYS_TOFROM:
36623 case GOMP_MAP_ALLOC:
36624 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36625 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36626 case GOMP_MAP_ALWAYS_POINTER:
36627 break;
36628 default:
36629 error_at (OMP_CLAUSE_LOCATION (*pc),
36630 "%<#pragma omp target%> with map-type other "
36631 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36632 "on %<map%> clause");
36633 *pc = OMP_CLAUSE_CHAIN (*pc);
36634 continue;
36636 pc = &OMP_CLAUSE_CHAIN (*pc);
36638 return true;
36641 /* OpenACC 2.0:
36642 # pragma acc cache (variable-list) new-line
36645 static tree
36646 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36648 tree stmt, clauses;
36650 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36651 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36653 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36655 stmt = make_node (OACC_CACHE);
36656 TREE_TYPE (stmt) = void_type_node;
36657 OACC_CACHE_CLAUSES (stmt) = clauses;
36658 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36659 add_stmt (stmt);
36661 return stmt;
36664 /* OpenACC 2.0:
36665 # pragma acc data oacc-data-clause[optseq] new-line
36666 structured-block */
36668 #define OACC_DATA_CLAUSE_MASK \
36669 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36681 static tree
36682 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36684 tree stmt, clauses, block;
36685 unsigned int save;
36687 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36688 "#pragma acc data", pragma_tok);
36690 block = begin_omp_parallel ();
36691 save = cp_parser_begin_omp_structured_block (parser);
36692 cp_parser_statement (parser, NULL_TREE, false, if_p);
36693 cp_parser_end_omp_structured_block (parser, save);
36694 stmt = finish_oacc_data (clauses, block);
36695 return stmt;
36698 /* OpenACC 2.0:
36699 # pragma acc host_data <clauses> new-line
36700 structured-block */
36702 #define OACC_HOST_DATA_CLAUSE_MASK \
36703 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36705 static tree
36706 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36708 tree stmt, clauses, block;
36709 unsigned int save;
36711 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36712 "#pragma acc host_data", pragma_tok);
36714 block = begin_omp_parallel ();
36715 save = cp_parser_begin_omp_structured_block (parser);
36716 cp_parser_statement (parser, NULL_TREE, false, if_p);
36717 cp_parser_end_omp_structured_block (parser, save);
36718 stmt = finish_oacc_host_data (clauses, block);
36719 return stmt;
36722 /* OpenACC 2.0:
36723 # pragma acc declare oacc-data-clause[optseq] new-line
36726 #define OACC_DECLARE_CLAUSE_MASK \
36727 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36740 static tree
36741 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36743 tree clauses, stmt;
36744 bool error = false;
36746 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36747 "#pragma acc declare", pragma_tok, true);
36750 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36752 error_at (pragma_tok->location,
36753 "no valid clauses specified in %<#pragma acc declare%>");
36754 return NULL_TREE;
36757 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36759 location_t loc = OMP_CLAUSE_LOCATION (t);
36760 tree decl = OMP_CLAUSE_DECL (t);
36761 if (!DECL_P (decl))
36763 error_at (loc, "array section in %<#pragma acc declare%>");
36764 error = true;
36765 continue;
36767 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36768 switch (OMP_CLAUSE_MAP_KIND (t))
36770 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36771 case GOMP_MAP_FORCE_ALLOC:
36772 case GOMP_MAP_FORCE_TO:
36773 case GOMP_MAP_FORCE_DEVICEPTR:
36774 case GOMP_MAP_DEVICE_RESIDENT:
36775 break;
36777 case GOMP_MAP_LINK:
36778 if (!global_bindings_p ()
36779 && (TREE_STATIC (decl)
36780 || !DECL_EXTERNAL (decl)))
36782 error_at (loc,
36783 "%qD must be a global variable in "
36784 "%<#pragma acc declare link%>",
36785 decl);
36786 error = true;
36787 continue;
36789 break;
36791 default:
36792 if (global_bindings_p ())
36794 error_at (loc, "invalid OpenACC clause at file scope");
36795 error = true;
36796 continue;
36798 if (DECL_EXTERNAL (decl))
36800 error_at (loc,
36801 "invalid use of %<extern%> variable %qD "
36802 "in %<#pragma acc declare%>", decl);
36803 error = true;
36804 continue;
36806 else if (TREE_PUBLIC (decl))
36808 error_at (loc,
36809 "invalid use of %<global%> variable %qD "
36810 "in %<#pragma acc declare%>", decl);
36811 error = true;
36812 continue;
36814 break;
36817 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36818 || lookup_attribute ("omp declare target link",
36819 DECL_ATTRIBUTES (decl)))
36821 error_at (loc, "variable %qD used more than once with "
36822 "%<#pragma acc declare%>", decl);
36823 error = true;
36824 continue;
36827 if (!error)
36829 tree id;
36831 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36832 id = get_identifier ("omp declare target link");
36833 else
36834 id = get_identifier ("omp declare target");
36836 DECL_ATTRIBUTES (decl)
36837 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36838 if (global_bindings_p ())
36840 symtab_node *node = symtab_node::get (decl);
36841 if (node != NULL)
36843 node->offloadable = 1;
36844 if (ENABLE_OFFLOADING)
36846 g->have_offload = true;
36847 if (is_a <varpool_node *> (node))
36848 vec_safe_push (offload_vars, decl);
36855 if (error || global_bindings_p ())
36856 return NULL_TREE;
36858 stmt = make_node (OACC_DECLARE);
36859 TREE_TYPE (stmt) = void_type_node;
36860 OACC_DECLARE_CLAUSES (stmt) = clauses;
36861 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36863 add_stmt (stmt);
36865 return NULL_TREE;
36868 /* OpenACC 2.0:
36869 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36873 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36875 LOC is the location of the #pragma token.
36878 #define OACC_ENTER_DATA_CLAUSE_MASK \
36879 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36887 #define OACC_EXIT_DATA_CLAUSE_MASK \
36888 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36894 static tree
36895 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36896 bool enter)
36898 location_t loc = pragma_tok->location;
36899 tree stmt, clauses;
36900 const char *p = "";
36902 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36903 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36905 if (strcmp (p, "data") != 0)
36907 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36908 enter ? "enter" : "exit");
36909 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36910 return NULL_TREE;
36913 cp_lexer_consume_token (parser->lexer);
36915 if (enter)
36916 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36917 "#pragma acc enter data", pragma_tok);
36918 else
36919 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36920 "#pragma acc exit data", pragma_tok);
36922 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36924 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36925 enter ? "enter" : "exit");
36926 return NULL_TREE;
36929 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36930 TREE_TYPE (stmt) = void_type_node;
36931 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36932 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36933 add_stmt (stmt);
36934 return stmt;
36937 /* OpenACC 2.0:
36938 # pragma acc loop oacc-loop-clause[optseq] new-line
36939 structured-block */
36941 #define OACC_LOOP_CLAUSE_MASK \
36942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36953 static tree
36954 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36955 omp_clause_mask mask, tree *cclauses, bool *if_p)
36957 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36959 strcat (p_name, " loop");
36960 mask |= OACC_LOOP_CLAUSE_MASK;
36962 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36963 cclauses == NULL);
36964 if (cclauses)
36966 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36967 if (*cclauses)
36968 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36969 if (clauses)
36970 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36973 tree block = begin_omp_structured_block ();
36974 int save = cp_parser_begin_omp_structured_block (parser);
36975 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36976 cp_parser_end_omp_structured_block (parser, save);
36977 add_stmt (finish_omp_structured_block (block));
36979 return stmt;
36982 /* OpenACC 2.0:
36983 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36984 structured-block
36988 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36989 structured-block
36992 #define OACC_KERNELS_CLAUSE_MASK \
36993 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37011 #define OACC_PARALLEL_CLAUSE_MASK \
37012 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37033 static tree
37034 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37035 char *p_name, bool *if_p)
37037 omp_clause_mask mask;
37038 enum tree_code code;
37039 switch (cp_parser_pragma_kind (pragma_tok))
37041 case PRAGMA_OACC_KERNELS:
37042 strcat (p_name, " kernels");
37043 mask = OACC_KERNELS_CLAUSE_MASK;
37044 code = OACC_KERNELS;
37045 break;
37046 case PRAGMA_OACC_PARALLEL:
37047 strcat (p_name, " parallel");
37048 mask = OACC_PARALLEL_CLAUSE_MASK;
37049 code = OACC_PARALLEL;
37050 break;
37051 default:
37052 gcc_unreachable ();
37055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37057 const char *p
37058 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37059 if (strcmp (p, "loop") == 0)
37061 cp_lexer_consume_token (parser->lexer);
37062 tree block = begin_omp_parallel ();
37063 tree clauses;
37064 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37065 if_p);
37066 return finish_omp_construct (code, block, clauses);
37070 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37072 tree block = begin_omp_parallel ();
37073 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37074 cp_parser_statement (parser, NULL_TREE, false, if_p);
37075 cp_parser_end_omp_structured_block (parser, save);
37076 return finish_omp_construct (code, block, clauses);
37079 /* OpenACC 2.0:
37080 # pragma acc update oacc-update-clause[optseq] new-line
37083 #define OACC_UPDATE_CLAUSE_MASK \
37084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37091 static tree
37092 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37094 tree stmt, clauses;
37096 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37097 "#pragma acc update", pragma_tok);
37099 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37101 error_at (pragma_tok->location,
37102 "%<#pragma acc update%> must contain at least one "
37103 "%<device%> or %<host%> or %<self%> clause");
37104 return NULL_TREE;
37107 stmt = make_node (OACC_UPDATE);
37108 TREE_TYPE (stmt) = void_type_node;
37109 OACC_UPDATE_CLAUSES (stmt) = clauses;
37110 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37111 add_stmt (stmt);
37112 return stmt;
37115 /* OpenACC 2.0:
37116 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37118 LOC is the location of the #pragma token.
37121 #define OACC_WAIT_CLAUSE_MASK \
37122 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37124 static tree
37125 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37127 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37128 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37130 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37131 list = cp_parser_oacc_wait_list (parser, loc, list);
37133 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37134 "#pragma acc wait", pragma_tok);
37136 stmt = c_finish_oacc_wait (loc, list, clauses);
37137 stmt = finish_expr_stmt (stmt);
37139 return stmt;
37142 /* OpenMP 4.0:
37143 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37145 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37146 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37153 static void
37154 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37155 enum pragma_context context)
37157 bool first_p = parser->omp_declare_simd == NULL;
37158 cp_omp_declare_simd_data data;
37159 if (first_p)
37161 data.error_seen = false;
37162 data.fndecl_seen = false;
37163 data.tokens = vNULL;
37164 data.clauses = NULL_TREE;
37165 /* It is safe to take the address of a local variable; it will only be
37166 used while this scope is live. */
37167 parser->omp_declare_simd = &data;
37170 /* Store away all pragma tokens. */
37171 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37172 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37173 cp_lexer_consume_token (parser->lexer);
37174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37175 parser->omp_declare_simd->error_seen = true;
37176 cp_parser_require_pragma_eol (parser, pragma_tok);
37177 struct cp_token_cache *cp
37178 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37179 parser->omp_declare_simd->tokens.safe_push (cp);
37181 if (first_p)
37183 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37184 cp_parser_pragma (parser, context, NULL);
37185 switch (context)
37187 case pragma_external:
37188 cp_parser_declaration (parser);
37189 break;
37190 case pragma_member:
37191 cp_parser_member_declaration (parser);
37192 break;
37193 case pragma_objc_icode:
37194 cp_parser_block_declaration (parser, /*statement_p=*/false);
37195 break;
37196 default:
37197 cp_parser_declaration_statement (parser);
37198 break;
37200 if (parser->omp_declare_simd
37201 && !parser->omp_declare_simd->error_seen
37202 && !parser->omp_declare_simd->fndecl_seen)
37203 error_at (pragma_tok->location,
37204 "%<#pragma omp declare simd%> not immediately followed by "
37205 "function declaration or definition");
37206 data.tokens.release ();
37207 parser->omp_declare_simd = NULL;
37211 /* Finalize #pragma omp declare simd clauses after direct declarator has
37212 been parsed, and put that into "omp declare simd" attribute. */
37214 static tree
37215 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37217 struct cp_token_cache *ce;
37218 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37219 int i;
37221 if (!data->error_seen && data->fndecl_seen)
37223 error ("%<#pragma omp declare simd%> not immediately followed by "
37224 "a single function declaration or definition");
37225 data->error_seen = true;
37227 if (data->error_seen)
37228 return attrs;
37230 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37232 tree c, cl;
37234 cp_parser_push_lexer_for_tokens (parser, ce);
37235 parser->lexer->in_pragma = true;
37236 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37237 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37238 cp_lexer_consume_token (parser->lexer);
37239 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37240 "#pragma omp declare simd", pragma_tok);
37241 cp_parser_pop_lexer (parser);
37242 if (cl)
37243 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37244 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37245 TREE_CHAIN (c) = attrs;
37246 if (processing_template_decl)
37247 ATTR_IS_DEPENDENT (c) = 1;
37248 attrs = c;
37251 data->fndecl_seen = true;
37252 return attrs;
37256 /* OpenMP 4.0:
37257 # pragma omp declare target new-line
37258 declarations and definitions
37259 # pragma omp end declare target new-line
37261 OpenMP 4.5:
37262 # pragma omp declare target ( extended-list ) new-line
37264 # pragma omp declare target declare-target-clauses[seq] new-line */
37266 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37270 static void
37271 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37273 tree clauses = NULL_TREE;
37274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37275 clauses
37276 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37277 "#pragma omp declare target", pragma_tok);
37278 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37280 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37281 clauses);
37282 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37283 cp_parser_require_pragma_eol (parser, pragma_tok);
37285 else
37287 cp_parser_require_pragma_eol (parser, pragma_tok);
37288 scope_chain->omp_declare_target_attribute++;
37289 return;
37291 if (scope_chain->omp_declare_target_attribute)
37292 error_at (pragma_tok->location,
37293 "%<#pragma omp declare target%> with clauses in between "
37294 "%<#pragma omp declare target%> without clauses and "
37295 "%<#pragma omp end declare target%>");
37296 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37298 tree t = OMP_CLAUSE_DECL (c), id;
37299 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37300 tree at2 = lookup_attribute ("omp declare target link",
37301 DECL_ATTRIBUTES (t));
37302 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37304 id = get_identifier ("omp declare target link");
37305 std::swap (at1, at2);
37307 else
37308 id = get_identifier ("omp declare target");
37309 if (at2)
37311 error_at (OMP_CLAUSE_LOCATION (c),
37312 "%qD specified both in declare target %<link%> and %<to%>"
37313 " clauses", t);
37314 continue;
37316 if (!at1)
37318 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37319 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37320 continue;
37322 symtab_node *node = symtab_node::get (t);
37323 if (node != NULL)
37325 node->offloadable = 1;
37326 if (ENABLE_OFFLOADING)
37328 g->have_offload = true;
37329 if (is_a <varpool_node *> (node))
37330 vec_safe_push (offload_vars, t);
37337 static void
37338 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37340 const char *p = "";
37341 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37343 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37344 p = IDENTIFIER_POINTER (id);
37346 if (strcmp (p, "declare") == 0)
37348 cp_lexer_consume_token (parser->lexer);
37349 p = "";
37350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37353 p = IDENTIFIER_POINTER (id);
37355 if (strcmp (p, "target") == 0)
37356 cp_lexer_consume_token (parser->lexer);
37357 else
37359 cp_parser_error (parser, "expected %<target%>");
37360 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37361 return;
37364 else
37366 cp_parser_error (parser, "expected %<declare%>");
37367 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37368 return;
37370 cp_parser_require_pragma_eol (parser, pragma_tok);
37371 if (!scope_chain->omp_declare_target_attribute)
37372 error_at (pragma_tok->location,
37373 "%<#pragma omp end declare target%> without corresponding "
37374 "%<#pragma omp declare target%>");
37375 else
37376 scope_chain->omp_declare_target_attribute--;
37379 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37380 expression and optional initializer clause of
37381 #pragma omp declare reduction. We store the expression(s) as
37382 either 3, 6 or 7 special statements inside of the artificial function's
37383 body. The first two statements are DECL_EXPRs for the artificial
37384 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37385 expression that uses those variables.
37386 If there was any INITIALIZER clause, this is followed by further statements,
37387 the fourth and fifth statements are DECL_EXPRs for the artificial
37388 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37389 constructor variant (first token after open paren is not omp_priv),
37390 then the sixth statement is a statement with the function call expression
37391 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37392 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37393 to initialize the OMP_PRIV artificial variable and there is seventh
37394 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37396 static bool
37397 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37399 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37400 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37401 type = TREE_TYPE (type);
37402 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37403 DECL_ARTIFICIAL (omp_out) = 1;
37404 pushdecl (omp_out);
37405 add_decl_expr (omp_out);
37406 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37407 DECL_ARTIFICIAL (omp_in) = 1;
37408 pushdecl (omp_in);
37409 add_decl_expr (omp_in);
37410 tree combiner;
37411 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37413 keep_next_level (true);
37414 tree block = begin_omp_structured_block ();
37415 combiner = cp_parser_expression (parser);
37416 finish_expr_stmt (combiner);
37417 block = finish_omp_structured_block (block);
37418 add_stmt (block);
37420 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37421 return false;
37423 const char *p = "";
37424 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37426 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37427 p = IDENTIFIER_POINTER (id);
37430 if (strcmp (p, "initializer") == 0)
37432 cp_lexer_consume_token (parser->lexer);
37433 matching_parens parens;
37434 if (!parens.require_open (parser))
37435 return false;
37437 p = "";
37438 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37440 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37441 p = IDENTIFIER_POINTER (id);
37444 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37445 DECL_ARTIFICIAL (omp_priv) = 1;
37446 pushdecl (omp_priv);
37447 add_decl_expr (omp_priv);
37448 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37449 DECL_ARTIFICIAL (omp_orig) = 1;
37450 pushdecl (omp_orig);
37451 add_decl_expr (omp_orig);
37453 keep_next_level (true);
37454 block = begin_omp_structured_block ();
37456 bool ctor = false;
37457 if (strcmp (p, "omp_priv") == 0)
37459 bool is_direct_init, is_non_constant_init;
37460 ctor = true;
37461 cp_lexer_consume_token (parser->lexer);
37462 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37463 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37464 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37465 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37466 == CPP_CLOSE_PAREN
37467 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37468 == CPP_CLOSE_PAREN))
37470 finish_omp_structured_block (block);
37471 error ("invalid initializer clause");
37472 return false;
37474 initializer = cp_parser_initializer (parser, &is_direct_init,
37475 &is_non_constant_init);
37476 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37477 NULL_TREE, LOOKUP_ONLYCONVERTING);
37479 else
37481 cp_parser_parse_tentatively (parser);
37482 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37483 /*check_dependency_p=*/true,
37484 /*template_p=*/NULL,
37485 /*declarator_p=*/false,
37486 /*optional_p=*/false);
37487 vec<tree, va_gc> *args;
37488 if (fn_name == error_mark_node
37489 || cp_parser_error_occurred (parser)
37490 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37491 || ((args = cp_parser_parenthesized_expression_list
37492 (parser, non_attr, /*cast_p=*/false,
37493 /*allow_expansion_p=*/true,
37494 /*non_constant_p=*/NULL)),
37495 cp_parser_error_occurred (parser)))
37497 finish_omp_structured_block (block);
37498 cp_parser_abort_tentative_parse (parser);
37499 cp_parser_error (parser, "expected id-expression (arguments)");
37500 return false;
37502 unsigned int i;
37503 tree arg;
37504 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37505 if (arg == omp_priv
37506 || (TREE_CODE (arg) == ADDR_EXPR
37507 && TREE_OPERAND (arg, 0) == omp_priv))
37508 break;
37509 cp_parser_abort_tentative_parse (parser);
37510 if (arg == NULL_TREE)
37511 error ("one of the initializer call arguments should be %<omp_priv%>"
37512 " or %<&omp_priv%>");
37513 initializer = cp_parser_postfix_expression (parser, false, false, false,
37514 false, NULL);
37515 finish_expr_stmt (initializer);
37518 block = finish_omp_structured_block (block);
37519 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37520 add_stmt (block);
37522 if (ctor)
37523 add_decl_expr (omp_orig);
37525 if (!parens.require_close (parser))
37526 return false;
37529 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37530 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37531 UNKNOWN_LOCATION);
37533 return true;
37536 /* OpenMP 4.0
37537 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37538 initializer-clause[opt] new-line
37540 initializer-clause:
37541 initializer (omp_priv initializer)
37542 initializer (function-name (argument-list)) */
37544 static void
37545 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37546 enum pragma_context)
37548 auto_vec<tree> types;
37549 enum tree_code reduc_code = ERROR_MARK;
37550 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37551 unsigned int i;
37552 cp_token *first_token;
37553 cp_token_cache *cp;
37554 int errs;
37555 void *p;
37557 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37558 p = obstack_alloc (&declarator_obstack, 0);
37560 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37561 goto fail;
37563 switch (cp_lexer_peek_token (parser->lexer)->type)
37565 case CPP_PLUS:
37566 reduc_code = PLUS_EXPR;
37567 break;
37568 case CPP_MULT:
37569 reduc_code = MULT_EXPR;
37570 break;
37571 case CPP_MINUS:
37572 reduc_code = MINUS_EXPR;
37573 break;
37574 case CPP_AND:
37575 reduc_code = BIT_AND_EXPR;
37576 break;
37577 case CPP_XOR:
37578 reduc_code = BIT_XOR_EXPR;
37579 break;
37580 case CPP_OR:
37581 reduc_code = BIT_IOR_EXPR;
37582 break;
37583 case CPP_AND_AND:
37584 reduc_code = TRUTH_ANDIF_EXPR;
37585 break;
37586 case CPP_OR_OR:
37587 reduc_code = TRUTH_ORIF_EXPR;
37588 break;
37589 case CPP_NAME:
37590 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37591 break;
37592 default:
37593 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37594 "%<|%>, %<&&%>, %<||%> or identifier");
37595 goto fail;
37598 if (reduc_code != ERROR_MARK)
37599 cp_lexer_consume_token (parser->lexer);
37601 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37602 if (reduc_id == error_mark_node)
37603 goto fail;
37605 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37606 goto fail;
37608 /* Types may not be defined in declare reduction type list. */
37609 const char *saved_message;
37610 saved_message = parser->type_definition_forbidden_message;
37611 parser->type_definition_forbidden_message
37612 = G_("types may not be defined in declare reduction type list");
37613 bool saved_colon_corrects_to_scope_p;
37614 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37615 parser->colon_corrects_to_scope_p = false;
37616 bool saved_colon_doesnt_start_class_def_p;
37617 saved_colon_doesnt_start_class_def_p
37618 = parser->colon_doesnt_start_class_def_p;
37619 parser->colon_doesnt_start_class_def_p = true;
37621 while (true)
37623 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37624 type = cp_parser_type_id (parser);
37625 if (type == error_mark_node)
37627 else if (ARITHMETIC_TYPE_P (type)
37628 && (orig_reduc_id == NULL_TREE
37629 || (TREE_CODE (type) != COMPLEX_TYPE
37630 && (id_equal (orig_reduc_id, "min")
37631 || id_equal (orig_reduc_id, "max")))))
37632 error_at (loc, "predeclared arithmetic type %qT in "
37633 "%<#pragma omp declare reduction%>", type);
37634 else if (TREE_CODE (type) == FUNCTION_TYPE
37635 || TREE_CODE (type) == METHOD_TYPE
37636 || TREE_CODE (type) == ARRAY_TYPE)
37637 error_at (loc, "function or array type %qT in "
37638 "%<#pragma omp declare reduction%>", type);
37639 else if (TREE_CODE (type) == REFERENCE_TYPE)
37640 error_at (loc, "reference type %qT in "
37641 "%<#pragma omp declare reduction%>", type);
37642 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37643 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37644 "%<#pragma omp declare reduction%>", type);
37645 else
37646 types.safe_push (type);
37648 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37649 cp_lexer_consume_token (parser->lexer);
37650 else
37651 break;
37654 /* Restore the saved message. */
37655 parser->type_definition_forbidden_message = saved_message;
37656 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37657 parser->colon_doesnt_start_class_def_p
37658 = saved_colon_doesnt_start_class_def_p;
37660 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37661 || types.is_empty ())
37663 fail:
37664 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37665 goto done;
37668 first_token = cp_lexer_peek_token (parser->lexer);
37669 cp = NULL;
37670 errs = errorcount;
37671 FOR_EACH_VEC_ELT (types, i, type)
37673 tree fntype
37674 = build_function_type_list (void_type_node,
37675 cp_build_reference_type (type, false),
37676 NULL_TREE);
37677 tree this_reduc_id = reduc_id;
37678 if (!dependent_type_p (type))
37679 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37680 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37681 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37682 DECL_ARTIFICIAL (fndecl) = 1;
37683 DECL_EXTERNAL (fndecl) = 1;
37684 DECL_DECLARED_INLINE_P (fndecl) = 1;
37685 DECL_IGNORED_P (fndecl) = 1;
37686 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37687 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37688 DECL_ATTRIBUTES (fndecl)
37689 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37690 DECL_ATTRIBUTES (fndecl));
37691 if (processing_template_decl)
37692 fndecl = push_template_decl (fndecl);
37693 bool block_scope = false;
37694 tree block = NULL_TREE;
37695 if (current_function_decl)
37697 block_scope = true;
37698 DECL_CONTEXT (fndecl) = global_namespace;
37699 if (!processing_template_decl)
37700 pushdecl (fndecl);
37702 else if (current_class_type)
37704 if (cp == NULL)
37706 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37707 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37708 cp_lexer_consume_token (parser->lexer);
37709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37710 goto fail;
37711 cp = cp_token_cache_new (first_token,
37712 cp_lexer_peek_nth_token (parser->lexer,
37713 2));
37715 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37716 finish_member_declaration (fndecl);
37717 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37718 DECL_PENDING_INLINE_P (fndecl) = 1;
37719 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37720 continue;
37722 else
37724 DECL_CONTEXT (fndecl) = current_namespace;
37725 pushdecl (fndecl);
37727 if (!block_scope)
37728 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37729 else
37730 block = begin_omp_structured_block ();
37731 if (cp)
37733 cp_parser_push_lexer_for_tokens (parser, cp);
37734 parser->lexer->in_pragma = true;
37736 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37738 if (!block_scope)
37739 finish_function (/*inline_p=*/false);
37740 else
37741 DECL_CONTEXT (fndecl) = current_function_decl;
37742 if (cp)
37743 cp_parser_pop_lexer (parser);
37744 goto fail;
37746 if (cp)
37747 cp_parser_pop_lexer (parser);
37748 if (!block_scope)
37749 finish_function (/*inline_p=*/false);
37750 else
37752 DECL_CONTEXT (fndecl) = current_function_decl;
37753 block = finish_omp_structured_block (block);
37754 if (TREE_CODE (block) == BIND_EXPR)
37755 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37756 else if (TREE_CODE (block) == STATEMENT_LIST)
37757 DECL_SAVED_TREE (fndecl) = block;
37758 if (processing_template_decl)
37759 add_decl_expr (fndecl);
37761 cp_check_omp_declare_reduction (fndecl);
37762 if (cp == NULL && types.length () > 1)
37763 cp = cp_token_cache_new (first_token,
37764 cp_lexer_peek_nth_token (parser->lexer, 2));
37765 if (errs != errorcount)
37766 break;
37769 cp_parser_require_pragma_eol (parser, pragma_tok);
37771 done:
37772 /* Free any declarators allocated. */
37773 obstack_free (&declarator_obstack, p);
37776 /* OpenMP 4.0
37777 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37778 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37779 initializer-clause[opt] new-line
37780 #pragma omp declare target new-line */
37782 static bool
37783 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37784 enum pragma_context context)
37786 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37788 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37789 const char *p = IDENTIFIER_POINTER (id);
37791 if (strcmp (p, "simd") == 0)
37793 cp_lexer_consume_token (parser->lexer);
37794 cp_parser_omp_declare_simd (parser, pragma_tok,
37795 context);
37796 return true;
37798 cp_ensure_no_omp_declare_simd (parser);
37799 if (strcmp (p, "reduction") == 0)
37801 cp_lexer_consume_token (parser->lexer);
37802 cp_parser_omp_declare_reduction (parser, pragma_tok,
37803 context);
37804 return false;
37806 if (!flag_openmp) /* flag_openmp_simd */
37808 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37809 return false;
37811 if (strcmp (p, "target") == 0)
37813 cp_lexer_consume_token (parser->lexer);
37814 cp_parser_omp_declare_target (parser, pragma_tok);
37815 return false;
37818 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37819 "or %<target%>");
37820 cp_parser_require_pragma_eol (parser, pragma_tok);
37821 return false;
37824 /* OpenMP 4.5:
37825 #pragma omp taskloop taskloop-clause[optseq] new-line
37826 for-loop
37828 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37829 for-loop */
37831 #define OMP_TASKLOOP_CLAUSE_MASK \
37832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37847 static tree
37848 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37849 char *p_name, omp_clause_mask mask, tree *cclauses,
37850 bool *if_p)
37852 tree clauses, sb, ret;
37853 unsigned int save;
37854 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37856 strcat (p_name, " taskloop");
37857 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37861 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37862 const char *p = IDENTIFIER_POINTER (id);
37864 if (strcmp (p, "simd") == 0)
37866 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37867 if (cclauses == NULL)
37868 cclauses = cclauses_buf;
37870 cp_lexer_consume_token (parser->lexer);
37871 if (!flag_openmp) /* flag_openmp_simd */
37872 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37873 cclauses, if_p);
37874 sb = begin_omp_structured_block ();
37875 save = cp_parser_begin_omp_structured_block (parser);
37876 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37877 cclauses, if_p);
37878 cp_parser_end_omp_structured_block (parser, save);
37879 tree body = finish_omp_structured_block (sb);
37880 if (ret == NULL)
37881 return ret;
37882 ret = make_node (OMP_TASKLOOP);
37883 TREE_TYPE (ret) = void_type_node;
37884 OMP_FOR_BODY (ret) = body;
37885 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37886 SET_EXPR_LOCATION (ret, loc);
37887 add_stmt (ret);
37888 return ret;
37891 if (!flag_openmp) /* flag_openmp_simd */
37893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37894 return NULL_TREE;
37897 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37898 cclauses == NULL);
37899 if (cclauses)
37901 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37902 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37905 sb = begin_omp_structured_block ();
37906 save = cp_parser_begin_omp_structured_block (parser);
37908 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37909 if_p);
37911 cp_parser_end_omp_structured_block (parser, save);
37912 add_stmt (finish_omp_structured_block (sb));
37914 return ret;
37918 /* OpenACC 2.0:
37919 # pragma acc routine oacc-routine-clause[optseq] new-line
37920 function-definition
37922 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37925 #define OACC_ROUTINE_CLAUSE_MASK \
37926 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37932 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37933 component, which must resolve to a declared namespace-scope
37934 function. The clauses are either processed directly (for a named
37935 function), or defered until the immediatley following declaration
37936 is parsed. */
37938 static void
37939 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37940 enum pragma_context context)
37942 gcc_checking_assert (context == pragma_external);
37943 /* The checking for "another pragma following this one" in the "no optional
37944 '( name )'" case makes sure that we dont re-enter. */
37945 gcc_checking_assert (parser->oacc_routine == NULL);
37947 cp_oacc_routine_data data;
37948 data.error_seen = false;
37949 data.fndecl_seen = false;
37950 data.tokens = vNULL;
37951 data.clauses = NULL_TREE;
37952 data.loc = pragma_tok->location;
37953 /* It is safe to take the address of a local variable; it will only be
37954 used while this scope is live. */
37955 parser->oacc_routine = &data;
37957 /* Look for optional '( name )'. */
37958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37960 matching_parens parens;
37961 parens.consume_open (parser); /* '(' */
37963 /* We parse the name as an id-expression. If it resolves to
37964 anything other than a non-overloaded function at namespace
37965 scope, it's an error. */
37966 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37967 tree name = cp_parser_id_expression (parser,
37968 /*template_keyword_p=*/false,
37969 /*check_dependency_p=*/false,
37970 /*template_p=*/NULL,
37971 /*declarator_p=*/false,
37972 /*optional_p=*/false);
37973 tree decl = (identifier_p (name)
37974 ? cp_parser_lookup_name_simple (parser, name, name_loc)
37975 : name);
37976 if (name != error_mark_node && decl == error_mark_node)
37977 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37979 if (decl == error_mark_node
37980 || !parens.require_close (parser))
37982 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37983 parser->oacc_routine = NULL;
37984 return;
37987 data.clauses
37988 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37989 "#pragma acc routine",
37990 cp_lexer_peek_token (parser->lexer));
37992 if (decl && is_overloaded_fn (decl)
37993 && (TREE_CODE (decl) != FUNCTION_DECL
37994 || DECL_FUNCTION_TEMPLATE_P (decl)))
37996 error_at (name_loc,
37997 "%<#pragma acc routine%> names a set of overloads");
37998 parser->oacc_routine = NULL;
37999 return;
38002 /* Perhaps we should use the same rule as declarations in different
38003 namespaces? */
38004 if (!DECL_NAMESPACE_SCOPE_P (decl))
38006 error_at (name_loc,
38007 "%qD does not refer to a namespace scope function", decl);
38008 parser->oacc_routine = NULL;
38009 return;
38012 if (TREE_CODE (decl) != FUNCTION_DECL)
38014 error_at (name_loc, "%qD does not refer to a function", decl);
38015 parser->oacc_routine = NULL;
38016 return;
38019 cp_finalize_oacc_routine (parser, decl, false);
38020 parser->oacc_routine = NULL;
38022 else /* No optional '( name )'. */
38024 /* Store away all pragma tokens. */
38025 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38026 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38027 cp_lexer_consume_token (parser->lexer);
38028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38029 parser->oacc_routine->error_seen = true;
38030 cp_parser_require_pragma_eol (parser, pragma_tok);
38031 struct cp_token_cache *cp
38032 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38033 parser->oacc_routine->tokens.safe_push (cp);
38035 /* Emit a helpful diagnostic if there's another pragma following this
38036 one. */
38037 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38039 cp_ensure_no_oacc_routine (parser);
38040 data.tokens.release ();
38041 /* ..., and then just keep going. */
38042 return;
38045 /* We only have to consider the pragma_external case here. */
38046 cp_parser_declaration (parser);
38047 if (parser->oacc_routine
38048 && !parser->oacc_routine->fndecl_seen)
38049 cp_ensure_no_oacc_routine (parser);
38050 else
38051 parser->oacc_routine = NULL;
38052 data.tokens.release ();
38056 /* Finalize #pragma acc routine clauses after direct declarator has
38057 been parsed. */
38059 static tree
38060 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38062 struct cp_token_cache *ce;
38063 cp_oacc_routine_data *data = parser->oacc_routine;
38065 if (!data->error_seen && data->fndecl_seen)
38067 error_at (data->loc,
38068 "%<#pragma acc routine%> not immediately followed by "
38069 "a single function declaration or definition");
38070 data->error_seen = true;
38072 if (data->error_seen)
38073 return attrs;
38075 gcc_checking_assert (data->tokens.length () == 1);
38076 ce = data->tokens[0];
38078 cp_parser_push_lexer_for_tokens (parser, ce);
38079 parser->lexer->in_pragma = true;
38080 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38082 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38083 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38084 parser->oacc_routine->clauses
38085 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38086 "#pragma acc routine", pragma_tok);
38087 cp_parser_pop_lexer (parser);
38088 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38089 fndecl_seen. */
38091 return attrs;
38094 /* Apply any saved OpenACC routine clauses to a just-parsed
38095 declaration. */
38097 static void
38098 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38100 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38102 /* Keep going if we're in error reporting mode. */
38103 if (parser->oacc_routine->error_seen
38104 || fndecl == error_mark_node)
38105 return;
38107 if (parser->oacc_routine->fndecl_seen)
38109 error_at (parser->oacc_routine->loc,
38110 "%<#pragma acc routine%> not immediately followed by"
38111 " a single function declaration or definition");
38112 parser->oacc_routine = NULL;
38113 return;
38115 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38117 cp_ensure_no_oacc_routine (parser);
38118 return;
38121 if (oacc_get_fn_attrib (fndecl))
38123 error_at (parser->oacc_routine->loc,
38124 "%<#pragma acc routine%> already applied to %qD", fndecl);
38125 parser->oacc_routine = NULL;
38126 return;
38129 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38131 error_at (parser->oacc_routine->loc,
38132 TREE_USED (fndecl)
38133 ? G_("%<#pragma acc routine%> must be applied before use")
38134 : G_("%<#pragma acc routine%> must be applied before "
38135 "definition"));
38136 parser->oacc_routine = NULL;
38137 return;
38140 /* Process the routine's dimension clauses. */
38141 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38142 oacc_replace_fn_attrib (fndecl, dims);
38144 /* Add an "omp declare target" attribute. */
38145 DECL_ATTRIBUTES (fndecl)
38146 = tree_cons (get_identifier ("omp declare target"),
38147 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38149 /* Don't unset parser->oacc_routine here: we may still need it to
38150 diagnose wrong usage. But, remember that we've used this "#pragma acc
38151 routine". */
38152 parser->oacc_routine->fndecl_seen = true;
38156 /* Main entry point to OpenMP statement pragmas. */
38158 static void
38159 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38161 tree stmt;
38162 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38163 omp_clause_mask mask (0);
38165 switch (cp_parser_pragma_kind (pragma_tok))
38167 case PRAGMA_OACC_ATOMIC:
38168 cp_parser_omp_atomic (parser, pragma_tok);
38169 return;
38170 case PRAGMA_OACC_CACHE:
38171 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38172 break;
38173 case PRAGMA_OACC_DATA:
38174 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38175 break;
38176 case PRAGMA_OACC_ENTER_DATA:
38177 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38178 break;
38179 case PRAGMA_OACC_EXIT_DATA:
38180 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38181 break;
38182 case PRAGMA_OACC_HOST_DATA:
38183 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38184 break;
38185 case PRAGMA_OACC_KERNELS:
38186 case PRAGMA_OACC_PARALLEL:
38187 strcpy (p_name, "#pragma acc");
38188 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38189 if_p);
38190 break;
38191 case PRAGMA_OACC_LOOP:
38192 strcpy (p_name, "#pragma acc");
38193 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38194 if_p);
38195 break;
38196 case PRAGMA_OACC_UPDATE:
38197 stmt = cp_parser_oacc_update (parser, pragma_tok);
38198 break;
38199 case PRAGMA_OACC_WAIT:
38200 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38201 break;
38202 case PRAGMA_OMP_ATOMIC:
38203 cp_parser_omp_atomic (parser, pragma_tok);
38204 return;
38205 case PRAGMA_OMP_CRITICAL:
38206 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38207 break;
38208 case PRAGMA_OMP_DISTRIBUTE:
38209 strcpy (p_name, "#pragma omp");
38210 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38211 if_p);
38212 break;
38213 case PRAGMA_OMP_FOR:
38214 strcpy (p_name, "#pragma omp");
38215 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38216 if_p);
38217 break;
38218 case PRAGMA_OMP_MASTER:
38219 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38220 break;
38221 case PRAGMA_OMP_PARALLEL:
38222 strcpy (p_name, "#pragma omp");
38223 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38224 if_p);
38225 break;
38226 case PRAGMA_OMP_SECTIONS:
38227 strcpy (p_name, "#pragma omp");
38228 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38229 break;
38230 case PRAGMA_OMP_SIMD:
38231 strcpy (p_name, "#pragma omp");
38232 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38233 if_p);
38234 break;
38235 case PRAGMA_OMP_SINGLE:
38236 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38237 break;
38238 case PRAGMA_OMP_TASK:
38239 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38240 break;
38241 case PRAGMA_OMP_TASKGROUP:
38242 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38243 break;
38244 case PRAGMA_OMP_TASKLOOP:
38245 strcpy (p_name, "#pragma omp");
38246 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38247 if_p);
38248 break;
38249 case PRAGMA_OMP_TEAMS:
38250 strcpy (p_name, "#pragma omp");
38251 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38252 if_p);
38253 break;
38254 default:
38255 gcc_unreachable ();
38258 protected_set_expr_location (stmt, pragma_tok->location);
38261 /* Transactional Memory parsing routines. */
38263 /* Parse a transaction attribute.
38265 txn-attribute:
38266 attribute
38267 [ [ identifier ] ]
38269 We use this instead of cp_parser_attributes_opt for transactions to avoid
38270 the pedwarn in C++98 mode. */
38272 static tree
38273 cp_parser_txn_attribute_opt (cp_parser *parser)
38275 cp_token *token;
38276 tree attr_name, attr = NULL;
38278 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38279 return cp_parser_attributes_opt (parser);
38281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38282 return NULL_TREE;
38283 cp_lexer_consume_token (parser->lexer);
38284 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38285 goto error1;
38287 token = cp_lexer_peek_token (parser->lexer);
38288 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38290 token = cp_lexer_consume_token (parser->lexer);
38292 attr_name = (token->type == CPP_KEYWORD
38293 /* For keywords, use the canonical spelling,
38294 not the parsed identifier. */
38295 ? ridpointers[(int) token->keyword]
38296 : token->u.value);
38297 attr = build_tree_list (attr_name, NULL_TREE);
38299 else
38300 cp_parser_error (parser, "expected identifier");
38302 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38303 error1:
38304 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38305 return attr;
38308 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38310 transaction-statement:
38311 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38312 compound-statement
38313 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38316 static tree
38317 cp_parser_transaction (cp_parser *parser, cp_token *token)
38319 unsigned char old_in = parser->in_transaction;
38320 unsigned char this_in = 1, new_in;
38321 enum rid keyword = token->keyword;
38322 tree stmt, attrs, noex;
38324 cp_lexer_consume_token (parser->lexer);
38326 if (keyword == RID_TRANSACTION_RELAXED
38327 || keyword == RID_SYNCHRONIZED)
38328 this_in |= TM_STMT_ATTR_RELAXED;
38329 else
38331 attrs = cp_parser_txn_attribute_opt (parser);
38332 if (attrs)
38333 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38336 /* Parse a noexcept specification. */
38337 if (keyword == RID_ATOMIC_NOEXCEPT)
38338 noex = boolean_true_node;
38339 else if (keyword == RID_ATOMIC_CANCEL)
38341 /* cancel-and-throw is unimplemented. */
38342 sorry ("atomic_cancel");
38343 noex = NULL_TREE;
38345 else
38346 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38348 /* Keep track if we're in the lexical scope of an outer transaction. */
38349 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38351 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38353 parser->in_transaction = new_in;
38354 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38355 parser->in_transaction = old_in;
38357 finish_transaction_stmt (stmt, NULL, this_in, noex);
38359 return stmt;
38362 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38364 transaction-expression:
38365 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38366 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38369 static tree
38370 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38372 unsigned char old_in = parser->in_transaction;
38373 unsigned char this_in = 1;
38374 cp_token *token;
38375 tree expr, noex;
38376 bool noex_expr;
38377 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38379 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38380 || keyword == RID_TRANSACTION_RELAXED);
38382 if (!flag_tm)
38383 error_at (loc,
38384 keyword == RID_TRANSACTION_RELAXED
38385 ? G_("%<__transaction_relaxed%> without transactional memory "
38386 "support enabled")
38387 : G_("%<__transaction_atomic%> without transactional memory "
38388 "support enabled"));
38390 token = cp_parser_require_keyword (parser, keyword,
38391 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38392 : RT_TRANSACTION_RELAXED));
38393 gcc_assert (token != NULL);
38395 if (keyword == RID_TRANSACTION_RELAXED)
38396 this_in |= TM_STMT_ATTR_RELAXED;
38398 /* Set this early. This might mean that we allow transaction_cancel in
38399 an expression that we find out later actually has to be a constexpr.
38400 However, we expect that cxx_constant_value will be able to deal with
38401 this; also, if the noexcept has no constexpr, then what we parse next
38402 really is a transaction's body. */
38403 parser->in_transaction = this_in;
38405 /* Parse a noexcept specification. */
38406 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38407 true);
38409 if (!noex || !noex_expr
38410 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38412 matching_parens parens;
38413 parens.require_open (parser);
38415 expr = cp_parser_expression (parser);
38416 expr = finish_parenthesized_expr (expr);
38418 parens.require_close (parser);
38420 else
38422 /* The only expression that is available got parsed for the noexcept
38423 already. noexcept is true then. */
38424 expr = noex;
38425 noex = boolean_true_node;
38428 expr = build_transaction_expr (token->location, expr, this_in, noex);
38429 parser->in_transaction = old_in;
38431 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38432 return error_mark_node;
38434 return (flag_tm ? expr : error_mark_node);
38437 /* Parse a function-transaction-block.
38439 function-transaction-block:
38440 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38441 function-body
38442 __transaction_atomic txn-attribute[opt] function-try-block
38443 __transaction_relaxed ctor-initializer[opt] function-body
38444 __transaction_relaxed function-try-block
38447 static void
38448 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38450 unsigned char old_in = parser->in_transaction;
38451 unsigned char new_in = 1;
38452 tree compound_stmt, stmt, attrs;
38453 cp_token *token;
38455 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38456 || keyword == RID_TRANSACTION_RELAXED);
38457 token = cp_parser_require_keyword (parser, keyword,
38458 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38459 : RT_TRANSACTION_RELAXED));
38460 gcc_assert (token != NULL);
38462 if (keyword == RID_TRANSACTION_RELAXED)
38463 new_in |= TM_STMT_ATTR_RELAXED;
38464 else
38466 attrs = cp_parser_txn_attribute_opt (parser);
38467 if (attrs)
38468 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38471 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38473 parser->in_transaction = new_in;
38475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38476 cp_parser_function_try_block (parser);
38477 else
38478 cp_parser_ctor_initializer_opt_and_function_body
38479 (parser, /*in_function_try_block=*/false);
38481 parser->in_transaction = old_in;
38483 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38486 /* Parse a __transaction_cancel statement.
38488 cancel-statement:
38489 __transaction_cancel txn-attribute[opt] ;
38490 __transaction_cancel txn-attribute[opt] throw-expression ;
38492 ??? Cancel and throw is not yet implemented. */
38494 static tree
38495 cp_parser_transaction_cancel (cp_parser *parser)
38497 cp_token *token;
38498 bool is_outer = false;
38499 tree stmt, attrs;
38501 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38502 RT_TRANSACTION_CANCEL);
38503 gcc_assert (token != NULL);
38505 attrs = cp_parser_txn_attribute_opt (parser);
38506 if (attrs)
38507 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38509 /* ??? Parse cancel-and-throw here. */
38511 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38513 if (!flag_tm)
38515 error_at (token->location, "%<__transaction_cancel%> without "
38516 "transactional memory support enabled");
38517 return error_mark_node;
38519 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38521 error_at (token->location, "%<__transaction_cancel%> within a "
38522 "%<__transaction_relaxed%>");
38523 return error_mark_node;
38525 else if (is_outer)
38527 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38528 && !is_tm_may_cancel_outer (current_function_decl))
38530 error_at (token->location, "outer %<__transaction_cancel%> not "
38531 "within outer %<__transaction_atomic%>");
38532 error_at (token->location,
38533 " or a %<transaction_may_cancel_outer%> function");
38534 return error_mark_node;
38537 else if (parser->in_transaction == 0)
38539 error_at (token->location, "%<__transaction_cancel%> not within "
38540 "%<__transaction_atomic%>");
38541 return error_mark_node;
38544 stmt = build_tm_abort_call (token->location, is_outer);
38545 add_stmt (stmt);
38547 return stmt;
38550 /* The parser. */
38552 static GTY (()) cp_parser *the_parser;
38555 /* Special handling for the first token or line in the file. The first
38556 thing in the file might be #pragma GCC pch_preprocess, which loads a
38557 PCH file, which is a GC collection point. So we need to handle this
38558 first pragma without benefit of an existing lexer structure.
38560 Always returns one token to the caller in *FIRST_TOKEN. This is
38561 either the true first token of the file, or the first token after
38562 the initial pragma. */
38564 static void
38565 cp_parser_initial_pragma (cp_token *first_token)
38567 tree name = NULL;
38569 cp_lexer_get_preprocessor_token (NULL, first_token);
38570 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38571 return;
38573 cp_lexer_get_preprocessor_token (NULL, first_token);
38574 if (first_token->type == CPP_STRING)
38576 name = first_token->u.value;
38578 cp_lexer_get_preprocessor_token (NULL, first_token);
38579 if (first_token->type != CPP_PRAGMA_EOL)
38580 error_at (first_token->location,
38581 "junk at end of %<#pragma GCC pch_preprocess%>");
38583 else
38584 error_at (first_token->location, "expected string literal");
38586 /* Skip to the end of the pragma. */
38587 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38588 cp_lexer_get_preprocessor_token (NULL, first_token);
38590 /* Now actually load the PCH file. */
38591 if (name)
38592 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38594 /* Read one more token to return to our caller. We have to do this
38595 after reading the PCH file in, since its pointers have to be
38596 live. */
38597 cp_lexer_get_preprocessor_token (NULL, first_token);
38600 /* Parse a pragma GCC ivdep. */
38602 static bool
38603 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38605 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38606 return true;
38609 /* Parse a pragma GCC unroll. */
38611 static unsigned short
38612 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38614 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38615 tree expr = cp_parser_constant_expression (parser);
38616 unsigned short unroll;
38617 expr = maybe_constant_value (expr);
38618 HOST_WIDE_INT lunroll = 0;
38619 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38620 || TREE_CODE (expr) != INTEGER_CST
38621 || (lunroll = tree_to_shwi (expr)) < 0
38622 || lunroll >= USHRT_MAX)
38624 error_at (location, "%<#pragma GCC unroll%> requires an"
38625 " assignment-expression that evaluates to a non-negative"
38626 " integral constant less than %u", USHRT_MAX);
38627 unroll = 0;
38629 else
38631 unroll = (unsigned short)lunroll;
38632 if (unroll == 0)
38633 unroll = 1;
38635 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38636 return unroll;
38639 /* Normal parsing of a pragma token. Here we can (and must) use the
38640 regular lexer. */
38642 static bool
38643 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38645 cp_token *pragma_tok;
38646 unsigned int id;
38647 tree stmt;
38648 bool ret;
38650 pragma_tok = cp_lexer_consume_token (parser->lexer);
38651 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38652 parser->lexer->in_pragma = true;
38654 id = cp_parser_pragma_kind (pragma_tok);
38655 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38656 cp_ensure_no_omp_declare_simd (parser);
38657 switch (id)
38659 case PRAGMA_GCC_PCH_PREPROCESS:
38660 error_at (pragma_tok->location,
38661 "%<#pragma GCC pch_preprocess%> must be first");
38662 break;
38664 case PRAGMA_OMP_BARRIER:
38665 switch (context)
38667 case pragma_compound:
38668 cp_parser_omp_barrier (parser, pragma_tok);
38669 return false;
38670 case pragma_stmt:
38671 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38672 "used in compound statements", "omp barrier");
38673 break;
38674 default:
38675 goto bad_stmt;
38677 break;
38679 case PRAGMA_OMP_FLUSH:
38680 switch (context)
38682 case pragma_compound:
38683 cp_parser_omp_flush (parser, pragma_tok);
38684 return false;
38685 case pragma_stmt:
38686 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38687 "used in compound statements", "omp flush");
38688 break;
38689 default:
38690 goto bad_stmt;
38692 break;
38694 case PRAGMA_OMP_TASKWAIT:
38695 switch (context)
38697 case pragma_compound:
38698 cp_parser_omp_taskwait (parser, pragma_tok);
38699 return false;
38700 case pragma_stmt:
38701 error_at (pragma_tok->location,
38702 "%<#pragma %s%> may only be used in compound statements",
38703 "omp taskwait");
38704 break;
38705 default:
38706 goto bad_stmt;
38708 break;
38710 case PRAGMA_OMP_TASKYIELD:
38711 switch (context)
38713 case pragma_compound:
38714 cp_parser_omp_taskyield (parser, pragma_tok);
38715 return false;
38716 case pragma_stmt:
38717 error_at (pragma_tok->location,
38718 "%<#pragma %s%> may only be used in compound statements",
38719 "omp taskyield");
38720 break;
38721 default:
38722 goto bad_stmt;
38724 break;
38726 case PRAGMA_OMP_CANCEL:
38727 switch (context)
38729 case pragma_compound:
38730 cp_parser_omp_cancel (parser, pragma_tok);
38731 return false;
38732 case pragma_stmt:
38733 error_at (pragma_tok->location,
38734 "%<#pragma %s%> may only be used in compound statements",
38735 "omp cancel");
38736 break;
38737 default:
38738 goto bad_stmt;
38740 break;
38742 case PRAGMA_OMP_CANCELLATION_POINT:
38743 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38744 return false;
38746 case PRAGMA_OMP_THREADPRIVATE:
38747 cp_parser_omp_threadprivate (parser, pragma_tok);
38748 return false;
38750 case PRAGMA_OMP_DECLARE:
38751 return cp_parser_omp_declare (parser, pragma_tok, context);
38753 case PRAGMA_OACC_DECLARE:
38754 cp_parser_oacc_declare (parser, pragma_tok);
38755 return false;
38757 case PRAGMA_OACC_ENTER_DATA:
38758 if (context == pragma_stmt)
38760 error_at (pragma_tok->location,
38761 "%<#pragma %s%> may only be used in compound statements",
38762 "acc enter data");
38763 break;
38765 else if (context != pragma_compound)
38766 goto bad_stmt;
38767 cp_parser_omp_construct (parser, pragma_tok, if_p);
38768 return true;
38770 case PRAGMA_OACC_EXIT_DATA:
38771 if (context == pragma_stmt)
38773 error_at (pragma_tok->location,
38774 "%<#pragma %s%> may only be used in compound statements",
38775 "acc exit data");
38776 break;
38778 else if (context != pragma_compound)
38779 goto bad_stmt;
38780 cp_parser_omp_construct (parser, pragma_tok, if_p);
38781 return true;
38783 case PRAGMA_OACC_ROUTINE:
38784 if (context != pragma_external)
38786 error_at (pragma_tok->location,
38787 "%<#pragma acc routine%> must be at file scope");
38788 break;
38790 cp_parser_oacc_routine (parser, pragma_tok, context);
38791 return false;
38793 case PRAGMA_OACC_UPDATE:
38794 if (context == pragma_stmt)
38796 error_at (pragma_tok->location,
38797 "%<#pragma %s%> may only be used in compound statements",
38798 "acc update");
38799 break;
38801 else if (context != pragma_compound)
38802 goto bad_stmt;
38803 cp_parser_omp_construct (parser, pragma_tok, if_p);
38804 return true;
38806 case PRAGMA_OACC_WAIT:
38807 if (context == pragma_stmt)
38809 error_at (pragma_tok->location,
38810 "%<#pragma %s%> may only be used in compound statements",
38811 "acc wait");
38812 break;
38814 else if (context != pragma_compound)
38815 goto bad_stmt;
38816 cp_parser_omp_construct (parser, pragma_tok, if_p);
38817 return true;
38819 case PRAGMA_OACC_ATOMIC:
38820 case PRAGMA_OACC_CACHE:
38821 case PRAGMA_OACC_DATA:
38822 case PRAGMA_OACC_HOST_DATA:
38823 case PRAGMA_OACC_KERNELS:
38824 case PRAGMA_OACC_PARALLEL:
38825 case PRAGMA_OACC_LOOP:
38826 case PRAGMA_OMP_ATOMIC:
38827 case PRAGMA_OMP_CRITICAL:
38828 case PRAGMA_OMP_DISTRIBUTE:
38829 case PRAGMA_OMP_FOR:
38830 case PRAGMA_OMP_MASTER:
38831 case PRAGMA_OMP_PARALLEL:
38832 case PRAGMA_OMP_SECTIONS:
38833 case PRAGMA_OMP_SIMD:
38834 case PRAGMA_OMP_SINGLE:
38835 case PRAGMA_OMP_TASK:
38836 case PRAGMA_OMP_TASKGROUP:
38837 case PRAGMA_OMP_TASKLOOP:
38838 case PRAGMA_OMP_TEAMS:
38839 if (context != pragma_stmt && context != pragma_compound)
38840 goto bad_stmt;
38841 stmt = push_omp_privatization_clauses (false);
38842 cp_parser_omp_construct (parser, pragma_tok, if_p);
38843 pop_omp_privatization_clauses (stmt);
38844 return true;
38846 case PRAGMA_OMP_ORDERED:
38847 if (context != pragma_stmt && context != pragma_compound)
38848 goto bad_stmt;
38849 stmt = push_omp_privatization_clauses (false);
38850 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38851 pop_omp_privatization_clauses (stmt);
38852 return ret;
38854 case PRAGMA_OMP_TARGET:
38855 if (context != pragma_stmt && context != pragma_compound)
38856 goto bad_stmt;
38857 stmt = push_omp_privatization_clauses (false);
38858 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38859 pop_omp_privatization_clauses (stmt);
38860 return ret;
38862 case PRAGMA_OMP_END_DECLARE_TARGET:
38863 cp_parser_omp_end_declare_target (parser, pragma_tok);
38864 return false;
38866 case PRAGMA_OMP_SECTION:
38867 error_at (pragma_tok->location,
38868 "%<#pragma omp section%> may only be used in "
38869 "%<#pragma omp sections%> construct");
38870 break;
38872 case PRAGMA_IVDEP:
38874 if (context == pragma_external)
38876 error_at (pragma_tok->location,
38877 "%<#pragma GCC ivdep%> must be inside a function");
38878 break;
38880 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38881 unsigned short unroll;
38882 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38883 if (tok->type == CPP_PRAGMA
38884 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38886 tok = cp_lexer_consume_token (parser->lexer);
38887 unroll = cp_parser_pragma_unroll (parser, tok);
38888 tok = cp_lexer_peek_token (the_parser->lexer);
38890 else
38891 unroll = 0;
38892 if (tok->type != CPP_KEYWORD
38893 || (tok->keyword != RID_FOR
38894 && tok->keyword != RID_WHILE
38895 && tok->keyword != RID_DO))
38897 cp_parser_error (parser, "for, while or do statement expected");
38898 return false;
38900 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38901 return true;
38904 case PRAGMA_UNROLL:
38906 if (context == pragma_external)
38908 error_at (pragma_tok->location,
38909 "%<#pragma GCC unroll%> must be inside a function");
38910 break;
38912 const unsigned short unroll
38913 = cp_parser_pragma_unroll (parser, pragma_tok);
38914 bool ivdep;
38915 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38916 if (tok->type == CPP_PRAGMA
38917 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38919 tok = cp_lexer_consume_token (parser->lexer);
38920 ivdep = cp_parser_pragma_ivdep (parser, tok);
38921 tok = cp_lexer_peek_token (the_parser->lexer);
38923 else
38924 ivdep = false;
38925 if (tok->type != CPP_KEYWORD
38926 || (tok->keyword != RID_FOR
38927 && tok->keyword != RID_WHILE
38928 && tok->keyword != RID_DO))
38930 cp_parser_error (parser, "for, while or do statement expected");
38931 return false;
38933 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38934 return true;
38937 default:
38938 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38939 c_invoke_pragma_handler (id);
38940 break;
38942 bad_stmt:
38943 cp_parser_error (parser, "expected declaration specifiers");
38944 break;
38947 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38948 return false;
38951 /* The interface the pragma parsers have to the lexer. */
38953 enum cpp_ttype
38954 pragma_lex (tree *value, location_t *loc)
38956 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38957 enum cpp_ttype ret = tok->type;
38959 *value = tok->u.value;
38960 if (loc)
38961 *loc = tok->location;
38963 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38964 ret = CPP_EOF;
38965 else if (ret == CPP_STRING)
38966 *value = cp_parser_string_literal (the_parser, false, false);
38967 else
38969 if (ret == CPP_KEYWORD)
38970 ret = CPP_NAME;
38971 cp_lexer_consume_token (the_parser->lexer);
38974 return ret;
38978 /* External interface. */
38980 /* Parse one entire translation unit. */
38982 void
38983 c_parse_file (void)
38985 static bool already_called = false;
38987 if (already_called)
38988 fatal_error (input_location,
38989 "inter-module optimizations not implemented for C++");
38990 already_called = true;
38992 the_parser = cp_parser_new ();
38993 push_deferring_access_checks (flag_access_control
38994 ? dk_no_deferred : dk_no_check);
38995 cp_parser_translation_unit (the_parser);
38996 the_parser = NULL;
38999 /* Create an identifier for a generic parameter type (a synthesized
39000 template parameter implied by `auto' or a concept identifier). */
39002 static GTY(()) int generic_parm_count;
39003 static tree
39004 make_generic_type_name ()
39006 char buf[32];
39007 sprintf (buf, "auto:%d", ++generic_parm_count);
39008 return get_identifier (buf);
39011 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39012 (creating a new template parameter list if necessary). Returns the newly
39013 created template type parm. */
39015 static tree
39016 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39018 gcc_assert (current_binding_level->kind == sk_function_parms);
39020 /* Before committing to modifying any scope, if we're in an
39021 implicit template scope, and we're trying to synthesize a
39022 constrained parameter, try to find a previous parameter with
39023 the same name. This is the same-type rule for abbreviated
39024 function templates.
39026 NOTE: We can generate implicit parameters when tentatively
39027 parsing a nested name specifier, only to reject that parse
39028 later. However, matching the same template-id as part of a
39029 direct-declarator should generate an identical template
39030 parameter, so this rule will merge them. */
39031 if (parser->implicit_template_scope && constr)
39033 tree t = parser->implicit_template_parms;
39034 while (t)
39036 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39038 tree d = TREE_VALUE (t);
39039 if (TREE_CODE (d) == PARM_DECL)
39040 /* Return the TEMPLATE_PARM_INDEX. */
39041 d = DECL_INITIAL (d);
39042 return d;
39044 t = TREE_CHAIN (t);
39048 /* We are either continuing a function template that already contains implicit
39049 template parameters, creating a new fully-implicit function template, or
39050 extending an existing explicit function template with implicit template
39051 parameters. */
39053 cp_binding_level *const entry_scope = current_binding_level;
39055 bool become_template = false;
39056 cp_binding_level *parent_scope = 0;
39058 if (parser->implicit_template_scope)
39060 gcc_assert (parser->implicit_template_parms);
39062 current_binding_level = parser->implicit_template_scope;
39064 else
39066 /* Roll back to the existing template parameter scope (in the case of
39067 extending an explicit function template) or introduce a new template
39068 parameter scope ahead of the function parameter scope (or class scope
39069 in the case of out-of-line member definitions). The function scope is
39070 added back after template parameter synthesis below. */
39072 cp_binding_level *scope = entry_scope;
39074 while (scope->kind == sk_function_parms)
39076 parent_scope = scope;
39077 scope = scope->level_chain;
39079 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39081 /* If not defining a class, then any class scope is a scope level in
39082 an out-of-line member definition. In this case simply wind back
39083 beyond the first such scope to inject the template parameter list.
39084 Otherwise wind back to the class being defined. The latter can
39085 occur in class member friend declarations such as:
39087 class A {
39088 void foo (auto);
39090 class B {
39091 friend void A::foo (auto);
39094 The template parameter list synthesized for the friend declaration
39095 must be injected in the scope of 'B'. This can also occur in
39096 erroneous cases such as:
39098 struct A {
39099 struct B {
39100 void foo (auto);
39102 void B::foo (auto) {}
39105 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39106 but, nevertheless, the template parameter list synthesized for the
39107 declarator should be injected into the scope of 'A' as if the
39108 ill-formed template was specified explicitly. */
39110 while (scope->kind == sk_class && !scope->defining_class_p)
39112 parent_scope = scope;
39113 scope = scope->level_chain;
39117 current_binding_level = scope;
39119 if (scope->kind != sk_template_parms
39120 || !function_being_declared_is_template_p (parser))
39122 /* Introduce a new template parameter list for implicit template
39123 parameters. */
39125 become_template = true;
39127 parser->implicit_template_scope
39128 = begin_scope (sk_template_parms, NULL);
39130 ++processing_template_decl;
39132 parser->fully_implicit_function_template_p = true;
39133 ++parser->num_template_parameter_lists;
39135 else
39137 /* Synthesize implicit template parameters at the end of the explicit
39138 template parameter list. */
39140 gcc_assert (current_template_parms);
39142 parser->implicit_template_scope = scope;
39144 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39145 parser->implicit_template_parms
39146 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39150 /* Synthesize a new template parameter and track the current template
39151 parameter chain with implicit_template_parms. */
39153 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39154 tree synth_id = make_generic_type_name ();
39155 tree synth_tmpl_parm;
39156 bool non_type = false;
39158 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39159 synth_tmpl_parm
39160 = finish_template_type_parm (class_type_node, synth_id);
39161 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39162 synth_tmpl_parm
39163 = finish_constrained_template_template_parm (proto, synth_id);
39164 else
39166 synth_tmpl_parm = copy_decl (proto);
39167 DECL_NAME (synth_tmpl_parm) = synth_id;
39168 non_type = true;
39171 // Attach the constraint to the parm before processing.
39172 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39173 TREE_TYPE (node) = constr;
39174 tree new_parm
39175 = process_template_parm (parser->implicit_template_parms,
39176 input_location,
39177 node,
39178 /*non_type=*/non_type,
39179 /*param_pack=*/false);
39181 // Chain the new parameter to the list of implicit parameters.
39182 if (parser->implicit_template_parms)
39183 parser->implicit_template_parms
39184 = TREE_CHAIN (parser->implicit_template_parms);
39185 else
39186 parser->implicit_template_parms = new_parm;
39188 tree new_decl = get_local_decls ();
39189 if (non_type)
39190 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39191 new_decl = DECL_INITIAL (new_decl);
39193 /* If creating a fully implicit function template, start the new implicit
39194 template parameter list with this synthesized type, otherwise grow the
39195 current template parameter list. */
39197 if (become_template)
39199 parent_scope->level_chain = current_binding_level;
39201 tree new_parms = make_tree_vec (1);
39202 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39203 current_template_parms = tree_cons (size_int (processing_template_decl),
39204 new_parms, current_template_parms);
39206 else
39208 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39209 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39210 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39211 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39214 // If the new parameter was constrained, we need to add that to the
39215 // constraints in the template parameter list.
39216 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39218 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39219 reqs = conjoin_constraints (reqs, req);
39220 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39223 current_binding_level = entry_scope;
39225 return new_decl;
39228 /* Finish the declaration of a fully implicit function template. Such a
39229 template has no explicit template parameter list so has not been through the
39230 normal template head and tail processing. synthesize_implicit_template_parm
39231 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39232 provided if the declaration is a class member such that its template
39233 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39234 form is returned. Otherwise NULL_TREE is returned. */
39236 static tree
39237 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39239 gcc_assert (parser->fully_implicit_function_template_p);
39241 if (member_decl_opt && member_decl_opt != error_mark_node
39242 && DECL_VIRTUAL_P (member_decl_opt))
39244 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39245 "implicit templates may not be %<virtual%>");
39246 DECL_VIRTUAL_P (member_decl_opt) = false;
39249 if (member_decl_opt)
39250 member_decl_opt = finish_member_template_decl (member_decl_opt);
39251 end_template_decl ();
39253 parser->fully_implicit_function_template_p = false;
39254 --parser->num_template_parameter_lists;
39256 return member_decl_opt;
39259 /* Helper function for diagnostics that have complained about things
39260 being used with 'extern "C"' linkage.
39262 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39264 void
39265 maybe_show_extern_c_location (void)
39267 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39268 inform (the_parser->innermost_linkage_specification_location,
39269 "%<extern \"C\"%> linkage started here");
39272 #include "gt-cp-parser.h"