Deprecate some C++ extensions
[official-gcc.git] / gcc / cp / parser.c
blob4e3e1dccda0effa112627460d5cd78384c7d96df
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 || target == cp_error_declarator)
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, 0,
17053 "use of %<auto%> in parameter declaration "
17054 "only available with -fconcepts");
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. Made a permerror in GCC 8. */
19689 if (cp_parser_allow_gnu_extensions_p (parser)
19690 && initialization_kind == CPP_OPEN_PAREN
19691 && cp_parser_attributes_opt (parser)
19692 && permerror (input_location,
19693 "attributes after parenthesized initializer ignored"))
19695 static bool hint;
19696 if (flag_permissive && !hint)
19698 hint = true;
19699 inform (input_location,
19700 "this flexibility is deprecated and will be removed");
19704 /* And now complain about a non-function implicit template. */
19705 if (bogus_implicit_tmpl && decl != error_mark_node)
19706 error_at (DECL_SOURCE_LOCATION (decl),
19707 "non-function %qD declared as implicit template", decl);
19709 /* For an in-class declaration, use `grokfield' to create the
19710 declaration. */
19711 if (member_p)
19713 if (pushed_scope)
19715 pop_scope (pushed_scope);
19716 pushed_scope = NULL_TREE;
19718 decl = grokfield (declarator, decl_specifiers,
19719 initializer, !is_non_constant_init,
19720 /*asmspec=*/NULL_TREE,
19721 attr_chainon (attributes, prefix_attributes));
19722 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19723 cp_parser_save_default_args (parser, decl);
19724 cp_finalize_omp_declare_simd (parser, decl);
19725 cp_finalize_oacc_routine (parser, decl, false);
19728 /* Finish processing the declaration. But, skip member
19729 declarations. */
19730 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19732 cp_finish_decl (decl,
19733 initializer, !is_non_constant_init,
19734 asm_specification,
19735 /* If the initializer is in parentheses, then this is
19736 a direct-initialization, which means that an
19737 `explicit' constructor is OK. Otherwise, an
19738 `explicit' constructor cannot be used. */
19739 ((is_direct_init || !is_initialized)
19740 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19742 else if ((cxx_dialect != cxx98) && friend_p
19743 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19744 /* Core issue #226 (C++0x only): A default template-argument
19745 shall not be specified in a friend class template
19746 declaration. */
19747 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19748 /*is_partial=*/false, /*is_friend_decl=*/1);
19750 if (!friend_p && pushed_scope)
19751 pop_scope (pushed_scope);
19753 if (function_declarator_p (declarator)
19754 && parser->fully_implicit_function_template_p)
19756 if (member_p)
19757 decl = finish_fully_implicit_template (parser, decl);
19758 else
19759 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19762 if (auto_result && is_initialized && decl_specifiers->type
19763 && type_uses_auto (decl_specifiers->type))
19764 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19766 return decl;
19769 /* Parse a declarator.
19771 declarator:
19772 direct-declarator
19773 ptr-operator declarator
19775 abstract-declarator:
19776 ptr-operator abstract-declarator [opt]
19777 direct-abstract-declarator
19779 GNU Extensions:
19781 declarator:
19782 attributes [opt] direct-declarator
19783 attributes [opt] ptr-operator declarator
19785 abstract-declarator:
19786 attributes [opt] ptr-operator abstract-declarator [opt]
19787 attributes [opt] direct-abstract-declarator
19789 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19790 detect constructors, destructors, deduction guides, or conversion operators.
19791 It is set to -1 if the declarator is a name, and +1 if it is a
19792 function. Otherwise it is set to zero. Usually you just want to
19793 test for >0, but internally the negative value is used.
19795 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19796 a decl-specifier-seq unless it declares a constructor, destructor,
19797 or conversion. It might seem that we could check this condition in
19798 semantic analysis, rather than parsing, but that makes it difficult
19799 to handle something like `f()'. We want to notice that there are
19800 no decl-specifiers, and therefore realize that this is an
19801 expression, not a declaration.)
19803 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19804 the declarator is a direct-declarator of the form "(...)".
19806 MEMBER_P is true iff this declarator is a member-declarator.
19808 FRIEND_P is true iff this declarator is a friend. */
19810 static cp_declarator *
19811 cp_parser_declarator (cp_parser* parser,
19812 cp_parser_declarator_kind dcl_kind,
19813 int* ctor_dtor_or_conv_p,
19814 bool* parenthesized_p,
19815 bool member_p, bool friend_p)
19817 cp_declarator *declarator;
19818 enum tree_code code;
19819 cp_cv_quals cv_quals;
19820 tree class_type;
19821 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19823 /* Assume this is not a constructor, destructor, or type-conversion
19824 operator. */
19825 if (ctor_dtor_or_conv_p)
19826 *ctor_dtor_or_conv_p = 0;
19828 if (cp_parser_allow_gnu_extensions_p (parser))
19829 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19831 /* Check for the ptr-operator production. */
19832 cp_parser_parse_tentatively (parser);
19833 /* Parse the ptr-operator. */
19834 code = cp_parser_ptr_operator (parser,
19835 &class_type,
19836 &cv_quals,
19837 &std_attributes);
19839 /* If that worked, then we have a ptr-operator. */
19840 if (cp_parser_parse_definitely (parser))
19842 /* If a ptr-operator was found, then this declarator was not
19843 parenthesized. */
19844 if (parenthesized_p)
19845 *parenthesized_p = true;
19846 /* The dependent declarator is optional if we are parsing an
19847 abstract-declarator. */
19848 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19849 cp_parser_parse_tentatively (parser);
19851 /* Parse the dependent declarator. */
19852 declarator = cp_parser_declarator (parser, dcl_kind,
19853 /*ctor_dtor_or_conv_p=*/NULL,
19854 /*parenthesized_p=*/NULL,
19855 /*member_p=*/false,
19856 friend_p);
19858 /* If we are parsing an abstract-declarator, we must handle the
19859 case where the dependent declarator is absent. */
19860 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19861 && !cp_parser_parse_definitely (parser))
19862 declarator = NULL;
19864 declarator = cp_parser_make_indirect_declarator
19865 (code, class_type, cv_quals, declarator, std_attributes);
19867 /* Everything else is a direct-declarator. */
19868 else
19870 if (parenthesized_p)
19871 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19872 CPP_OPEN_PAREN);
19873 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19874 ctor_dtor_or_conv_p,
19875 member_p, friend_p);
19878 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19879 declarator->attributes = gnu_attributes;
19880 return declarator;
19883 /* Parse a direct-declarator or direct-abstract-declarator.
19885 direct-declarator:
19886 declarator-id
19887 direct-declarator ( parameter-declaration-clause )
19888 cv-qualifier-seq [opt]
19889 ref-qualifier [opt]
19890 exception-specification [opt]
19891 direct-declarator [ constant-expression [opt] ]
19892 ( declarator )
19894 direct-abstract-declarator:
19895 direct-abstract-declarator [opt]
19896 ( parameter-declaration-clause )
19897 cv-qualifier-seq [opt]
19898 ref-qualifier [opt]
19899 exception-specification [opt]
19900 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19901 ( abstract-declarator )
19903 Returns a representation of the declarator. DCL_KIND is
19904 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19905 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19906 we are parsing a direct-declarator. It is
19907 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19908 of ambiguity we prefer an abstract declarator, as per
19909 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19910 as for cp_parser_declarator. */
19912 static cp_declarator *
19913 cp_parser_direct_declarator (cp_parser* parser,
19914 cp_parser_declarator_kind dcl_kind,
19915 int* ctor_dtor_or_conv_p,
19916 bool member_p, bool friend_p)
19918 cp_token *token;
19919 cp_declarator *declarator = NULL;
19920 tree scope = NULL_TREE;
19921 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19922 bool saved_in_declarator_p = parser->in_declarator_p;
19923 bool first = true;
19924 tree pushed_scope = NULL_TREE;
19925 cp_token *open_paren = NULL, *close_paren = NULL;
19927 while (true)
19929 /* Peek at the next token. */
19930 token = cp_lexer_peek_token (parser->lexer);
19931 if (token->type == CPP_OPEN_PAREN)
19933 /* This is either a parameter-declaration-clause, or a
19934 parenthesized declarator. When we know we are parsing a
19935 named declarator, it must be a parenthesized declarator
19936 if FIRST is true. For instance, `(int)' is a
19937 parameter-declaration-clause, with an omitted
19938 direct-abstract-declarator. But `((*))', is a
19939 parenthesized abstract declarator. Finally, when T is a
19940 template parameter `(T)' is a
19941 parameter-declaration-clause, and not a parenthesized
19942 named declarator.
19944 We first try and parse a parameter-declaration-clause,
19945 and then try a nested declarator (if FIRST is true).
19947 It is not an error for it not to be a
19948 parameter-declaration-clause, even when FIRST is
19949 false. Consider,
19951 int i (int);
19952 int i (3);
19954 The first is the declaration of a function while the
19955 second is the definition of a variable, including its
19956 initializer.
19958 Having seen only the parenthesis, we cannot know which of
19959 these two alternatives should be selected. Even more
19960 complex are examples like:
19962 int i (int (a));
19963 int i (int (3));
19965 The former is a function-declaration; the latter is a
19966 variable initialization.
19968 Thus again, we try a parameter-declaration-clause, and if
19969 that fails, we back out and return. */
19971 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19973 tree params;
19974 bool is_declarator = false;
19976 open_paren = NULL;
19978 /* In a member-declarator, the only valid interpretation
19979 of a parenthesis is the start of a
19980 parameter-declaration-clause. (It is invalid to
19981 initialize a static data member with a parenthesized
19982 initializer; only the "=" form of initialization is
19983 permitted.) */
19984 if (!member_p)
19985 cp_parser_parse_tentatively (parser);
19987 /* Consume the `('. */
19988 matching_parens parens;
19989 parens.consume_open (parser);
19990 if (first)
19992 /* If this is going to be an abstract declarator, we're
19993 in a declarator and we can't have default args. */
19994 parser->default_arg_ok_p = false;
19995 parser->in_declarator_p = true;
19998 begin_scope (sk_function_parms, NULL_TREE);
20000 /* Parse the parameter-declaration-clause. */
20001 params = cp_parser_parameter_declaration_clause (parser);
20003 /* Consume the `)'. */
20004 parens.require_close (parser);
20006 /* If all went well, parse the cv-qualifier-seq,
20007 ref-qualifier and the exception-specification. */
20008 if (member_p || cp_parser_parse_definitely (parser))
20010 cp_cv_quals cv_quals;
20011 cp_virt_specifiers virt_specifiers;
20012 cp_ref_qualifier ref_qual;
20013 tree exception_specification;
20014 tree late_return;
20015 tree attrs;
20016 bool memfn = (member_p || (pushed_scope
20017 && CLASS_TYPE_P (pushed_scope)));
20019 is_declarator = true;
20021 if (ctor_dtor_or_conv_p)
20022 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20023 first = false;
20025 /* Parse the cv-qualifier-seq. */
20026 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20027 /* Parse the ref-qualifier. */
20028 ref_qual = cp_parser_ref_qualifier_opt (parser);
20029 /* Parse the tx-qualifier. */
20030 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20031 /* And the exception-specification. */
20032 exception_specification
20033 = cp_parser_exception_specification_opt (parser);
20035 attrs = cp_parser_std_attribute_spec_seq (parser);
20037 /* In here, we handle cases where attribute is used after
20038 the function declaration. For example:
20039 void func (int x) __attribute__((vector(..))); */
20040 tree gnu_attrs = NULL_TREE;
20041 tree requires_clause = NULL_TREE;
20042 late_return = (cp_parser_late_return_type_opt
20043 (parser, declarator, requires_clause,
20044 memfn ? cv_quals : -1));
20046 /* Parse the virt-specifier-seq. */
20047 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20049 /* Create the function-declarator. */
20050 declarator = make_call_declarator (declarator,
20051 params,
20052 cv_quals,
20053 virt_specifiers,
20054 ref_qual,
20055 tx_qual,
20056 exception_specification,
20057 late_return,
20058 requires_clause);
20059 declarator->std_attributes = attrs;
20060 declarator->attributes = gnu_attrs;
20061 /* Any subsequent parameter lists are to do with
20062 return type, so are not those of the declared
20063 function. */
20064 parser->default_arg_ok_p = false;
20067 /* Remove the function parms from scope. */
20068 pop_bindings_and_leave_scope ();
20070 if (is_declarator)
20071 /* Repeat the main loop. */
20072 continue;
20075 /* If this is the first, we can try a parenthesized
20076 declarator. */
20077 if (first)
20079 bool saved_in_type_id_in_expr_p;
20081 parser->default_arg_ok_p = saved_default_arg_ok_p;
20082 parser->in_declarator_p = saved_in_declarator_p;
20084 open_paren = token;
20085 /* Consume the `('. */
20086 matching_parens parens;
20087 parens.consume_open (parser);
20088 /* Parse the nested declarator. */
20089 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20090 parser->in_type_id_in_expr_p = true;
20091 declarator
20092 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20093 /*parenthesized_p=*/NULL,
20094 member_p, friend_p);
20095 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20096 first = false;
20097 /* Expect a `)'. */
20098 close_paren = cp_lexer_peek_token (parser->lexer);
20099 if (!parens.require_close (parser))
20100 declarator = cp_error_declarator;
20101 if (declarator == cp_error_declarator)
20102 break;
20104 goto handle_declarator;
20106 /* Otherwise, we must be done. */
20107 else
20108 break;
20110 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20111 && token->type == CPP_OPEN_SQUARE
20112 && !cp_next_tokens_can_be_attribute_p (parser))
20114 /* Parse an array-declarator. */
20115 tree bounds, attrs;
20117 if (ctor_dtor_or_conv_p)
20118 *ctor_dtor_or_conv_p = 0;
20120 open_paren = NULL;
20121 first = false;
20122 parser->default_arg_ok_p = false;
20123 parser->in_declarator_p = true;
20124 /* Consume the `['. */
20125 cp_lexer_consume_token (parser->lexer);
20126 /* Peek at the next token. */
20127 token = cp_lexer_peek_token (parser->lexer);
20128 /* If the next token is `]', then there is no
20129 constant-expression. */
20130 if (token->type != CPP_CLOSE_SQUARE)
20132 bool non_constant_p;
20133 bounds
20134 = cp_parser_constant_expression (parser,
20135 /*allow_non_constant=*/true,
20136 &non_constant_p);
20137 if (!non_constant_p)
20138 /* OK */;
20139 else if (error_operand_p (bounds))
20140 /* Already gave an error. */;
20141 else if (!parser->in_function_body
20142 || current_binding_level->kind == sk_function_parms)
20144 /* Normally, the array bound must be an integral constant
20145 expression. However, as an extension, we allow VLAs
20146 in function scopes as long as they aren't part of a
20147 parameter declaration. */
20148 cp_parser_error (parser,
20149 "array bound is not an integer constant");
20150 bounds = error_mark_node;
20152 else if (processing_template_decl
20153 && !type_dependent_expression_p (bounds))
20155 /* Remember this wasn't a constant-expression. */
20156 bounds = build_nop (TREE_TYPE (bounds), bounds);
20157 TREE_SIDE_EFFECTS (bounds) = 1;
20160 else
20161 bounds = NULL_TREE;
20162 /* Look for the closing `]'. */
20163 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20165 declarator = cp_error_declarator;
20166 break;
20169 attrs = cp_parser_std_attribute_spec_seq (parser);
20170 declarator = make_array_declarator (declarator, bounds);
20171 declarator->std_attributes = attrs;
20173 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20176 tree qualifying_scope;
20177 tree unqualified_name;
20178 tree attrs;
20179 special_function_kind sfk;
20180 bool abstract_ok;
20181 bool pack_expansion_p = false;
20182 cp_token *declarator_id_start_token;
20184 /* Parse a declarator-id */
20185 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20186 if (abstract_ok)
20188 cp_parser_parse_tentatively (parser);
20190 /* If we see an ellipsis, we should be looking at a
20191 parameter pack. */
20192 if (token->type == CPP_ELLIPSIS)
20194 /* Consume the `...' */
20195 cp_lexer_consume_token (parser->lexer);
20197 pack_expansion_p = true;
20201 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20202 unqualified_name
20203 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20204 qualifying_scope = parser->scope;
20205 if (abstract_ok)
20207 bool okay = false;
20209 if (!unqualified_name && pack_expansion_p)
20211 /* Check whether an error occurred. */
20212 okay = !cp_parser_error_occurred (parser);
20214 /* We already consumed the ellipsis to mark a
20215 parameter pack, but we have no way to report it,
20216 so abort the tentative parse. We will be exiting
20217 immediately anyway. */
20218 cp_parser_abort_tentative_parse (parser);
20220 else
20221 okay = cp_parser_parse_definitely (parser);
20223 if (!okay)
20224 unqualified_name = error_mark_node;
20225 else if (unqualified_name
20226 && (qualifying_scope
20227 || (!identifier_p (unqualified_name))))
20229 cp_parser_error (parser, "expected unqualified-id");
20230 unqualified_name = error_mark_node;
20234 if (!unqualified_name)
20235 return NULL;
20236 if (unqualified_name == error_mark_node)
20238 declarator = cp_error_declarator;
20239 pack_expansion_p = false;
20240 declarator->parameter_pack_p = false;
20241 break;
20244 attrs = cp_parser_std_attribute_spec_seq (parser);
20246 if (qualifying_scope && at_namespace_scope_p ()
20247 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20249 /* In the declaration of a member of a template class
20250 outside of the class itself, the SCOPE will sometimes
20251 be a TYPENAME_TYPE. For example, given:
20253 template <typename T>
20254 int S<T>::R::i = 3;
20256 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20257 this context, we must resolve S<T>::R to an ordinary
20258 type, rather than a typename type.
20260 The reason we normally avoid resolving TYPENAME_TYPEs
20261 is that a specialization of `S' might render
20262 `S<T>::R' not a type. However, if `S' is
20263 specialized, then this `i' will not be used, so there
20264 is no harm in resolving the types here. */
20265 tree type;
20267 /* Resolve the TYPENAME_TYPE. */
20268 type = resolve_typename_type (qualifying_scope,
20269 /*only_current_p=*/false);
20270 /* If that failed, the declarator is invalid. */
20271 if (TREE_CODE (type) == TYPENAME_TYPE)
20273 if (typedef_variant_p (type))
20274 error_at (declarator_id_start_token->location,
20275 "cannot define member of dependent typedef "
20276 "%qT", type);
20277 else
20278 error_at (declarator_id_start_token->location,
20279 "%<%T::%E%> is not a type",
20280 TYPE_CONTEXT (qualifying_scope),
20281 TYPE_IDENTIFIER (qualifying_scope));
20283 qualifying_scope = type;
20286 sfk = sfk_none;
20288 if (unqualified_name)
20290 tree class_type;
20292 if (qualifying_scope
20293 && CLASS_TYPE_P (qualifying_scope))
20294 class_type = qualifying_scope;
20295 else
20296 class_type = current_class_type;
20298 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20300 tree name_type = TREE_TYPE (unqualified_name);
20302 if (!class_type || !same_type_p (name_type, class_type))
20304 /* We do not attempt to print the declarator
20305 here because we do not have enough
20306 information about its original syntactic
20307 form. */
20308 cp_parser_error (parser, "invalid declarator");
20309 declarator = cp_error_declarator;
20310 break;
20312 else if (qualifying_scope
20313 && CLASSTYPE_USE_TEMPLATE (name_type))
20315 error_at (declarator_id_start_token->location,
20316 "invalid use of constructor as a template");
20317 inform (declarator_id_start_token->location,
20318 "use %<%T::%D%> instead of %<%T::%D%> to "
20319 "name the constructor in a qualified name",
20320 class_type,
20321 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20322 class_type, name_type);
20323 declarator = cp_error_declarator;
20324 break;
20326 unqualified_name = constructor_name (class_type);
20329 if (class_type)
20331 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20332 sfk = sfk_destructor;
20333 else if (identifier_p (unqualified_name)
20334 && IDENTIFIER_CONV_OP_P (unqualified_name))
20335 sfk = sfk_conversion;
20336 else if (/* There's no way to declare a constructor
20337 for an unnamed type, even if the type
20338 got a name for linkage purposes. */
20339 !TYPE_WAS_UNNAMED (class_type)
20340 /* Handle correctly (c++/19200):
20342 struct S {
20343 struct T{};
20344 friend void S(T);
20347 and also:
20349 namespace N {
20350 void S();
20353 struct S {
20354 friend void N::S();
20355 }; */
20356 && (!friend_p || class_type == qualifying_scope)
20357 && constructor_name_p (unqualified_name,
20358 class_type))
20359 sfk = sfk_constructor;
20360 else if (is_overloaded_fn (unqualified_name)
20361 && DECL_CONSTRUCTOR_P (get_first_fn
20362 (unqualified_name)))
20363 sfk = sfk_constructor;
20365 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20366 *ctor_dtor_or_conv_p = -1;
20369 declarator = make_id_declarator (qualifying_scope,
20370 unqualified_name,
20371 sfk);
20372 declarator->std_attributes = attrs;
20373 declarator->id_loc = token->location;
20374 declarator->parameter_pack_p = pack_expansion_p;
20376 if (pack_expansion_p)
20377 maybe_warn_variadic_templates ();
20380 handle_declarator:;
20381 scope = get_scope_of_declarator (declarator);
20382 if (scope)
20384 /* Any names that appear after the declarator-id for a
20385 member are looked up in the containing scope. */
20386 if (at_function_scope_p ())
20388 /* But declarations with qualified-ids can't appear in a
20389 function. */
20390 cp_parser_error (parser, "qualified-id in declaration");
20391 declarator = cp_error_declarator;
20392 break;
20394 pushed_scope = push_scope (scope);
20396 parser->in_declarator_p = true;
20397 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20398 || (declarator && declarator->kind == cdk_id))
20399 /* Default args are only allowed on function
20400 declarations. */
20401 parser->default_arg_ok_p = saved_default_arg_ok_p;
20402 else
20403 parser->default_arg_ok_p = false;
20405 first = false;
20407 /* We're done. */
20408 else
20409 break;
20412 /* For an abstract declarator, we might wind up with nothing at this
20413 point. That's an error; the declarator is not optional. */
20414 if (!declarator)
20415 cp_parser_error (parser, "expected declarator");
20416 else if (open_paren)
20418 /* Record overly parenthesized declarator so we can give a
20419 diagnostic about confusing decl/expr disambiguation. */
20420 if (declarator->kind == cdk_array)
20422 /* If the open and close parens are on different lines, this
20423 is probably a formatting thing, so ignore. */
20424 expanded_location open = expand_location (open_paren->location);
20425 expanded_location close = expand_location (close_paren->location);
20426 if (open.line != close.line || open.file != close.file)
20427 open_paren = NULL;
20429 if (open_paren)
20430 declarator->parenthesized = open_paren->location;
20433 /* If we entered a scope, we must exit it now. */
20434 if (pushed_scope)
20435 pop_scope (pushed_scope);
20437 parser->default_arg_ok_p = saved_default_arg_ok_p;
20438 parser->in_declarator_p = saved_in_declarator_p;
20440 return declarator;
20443 /* Parse a ptr-operator.
20445 ptr-operator:
20446 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20447 * cv-qualifier-seq [opt]
20449 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20450 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20452 GNU Extension:
20454 ptr-operator:
20455 & cv-qualifier-seq [opt]
20457 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20458 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20459 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20460 filled in with the TYPE containing the member. *CV_QUALS is
20461 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20462 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20463 Note that the tree codes returned by this function have nothing
20464 to do with the types of trees that will be eventually be created
20465 to represent the pointer or reference type being parsed. They are
20466 just constants with suggestive names. */
20467 static enum tree_code
20468 cp_parser_ptr_operator (cp_parser* parser,
20469 tree* type,
20470 cp_cv_quals *cv_quals,
20471 tree *attributes)
20473 enum tree_code code = ERROR_MARK;
20474 cp_token *token;
20475 tree attrs = NULL_TREE;
20477 /* Assume that it's not a pointer-to-member. */
20478 *type = NULL_TREE;
20479 /* And that there are no cv-qualifiers. */
20480 *cv_quals = TYPE_UNQUALIFIED;
20482 /* Peek at the next token. */
20483 token = cp_lexer_peek_token (parser->lexer);
20485 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20486 if (token->type == CPP_MULT)
20487 code = INDIRECT_REF;
20488 else if (token->type == CPP_AND)
20489 code = ADDR_EXPR;
20490 else if ((cxx_dialect != cxx98) &&
20491 token->type == CPP_AND_AND) /* C++0x only */
20492 code = NON_LVALUE_EXPR;
20494 if (code != ERROR_MARK)
20496 /* Consume the `*', `&' or `&&'. */
20497 cp_lexer_consume_token (parser->lexer);
20499 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20500 `&', if we are allowing GNU extensions. (The only qualifier
20501 that can legally appear after `&' is `restrict', but that is
20502 enforced during semantic analysis. */
20503 if (code == INDIRECT_REF
20504 || cp_parser_allow_gnu_extensions_p (parser))
20505 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20507 attrs = cp_parser_std_attribute_spec_seq (parser);
20508 if (attributes != NULL)
20509 *attributes = attrs;
20511 else
20513 /* Try the pointer-to-member case. */
20514 cp_parser_parse_tentatively (parser);
20515 /* Look for the optional `::' operator. */
20516 cp_parser_global_scope_opt (parser,
20517 /*current_scope_valid_p=*/false);
20518 /* Look for the nested-name specifier. */
20519 token = cp_lexer_peek_token (parser->lexer);
20520 cp_parser_nested_name_specifier (parser,
20521 /*typename_keyword_p=*/false,
20522 /*check_dependency_p=*/true,
20523 /*type_p=*/false,
20524 /*is_declaration=*/false);
20525 /* If we found it, and the next token is a `*', then we are
20526 indeed looking at a pointer-to-member operator. */
20527 if (!cp_parser_error_occurred (parser)
20528 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20530 /* Indicate that the `*' operator was used. */
20531 code = INDIRECT_REF;
20533 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20534 error_at (token->location, "%qD is a namespace", parser->scope);
20535 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20536 error_at (token->location, "cannot form pointer to member of "
20537 "non-class %q#T", parser->scope);
20538 else
20540 /* The type of which the member is a member is given by the
20541 current SCOPE. */
20542 *type = parser->scope;
20543 /* The next name will not be qualified. */
20544 parser->scope = NULL_TREE;
20545 parser->qualifying_scope = NULL_TREE;
20546 parser->object_scope = NULL_TREE;
20547 /* Look for optional c++11 attributes. */
20548 attrs = cp_parser_std_attribute_spec_seq (parser);
20549 if (attributes != NULL)
20550 *attributes = attrs;
20551 /* Look for the optional cv-qualifier-seq. */
20552 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20555 /* If that didn't work we don't have a ptr-operator. */
20556 if (!cp_parser_parse_definitely (parser))
20557 cp_parser_error (parser, "expected ptr-operator");
20560 return code;
20563 /* Parse an (optional) cv-qualifier-seq.
20565 cv-qualifier-seq:
20566 cv-qualifier cv-qualifier-seq [opt]
20568 cv-qualifier:
20569 const
20570 volatile
20572 GNU Extension:
20574 cv-qualifier:
20575 __restrict__
20577 Returns a bitmask representing the cv-qualifiers. */
20579 static cp_cv_quals
20580 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20582 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20584 while (true)
20586 cp_token *token;
20587 cp_cv_quals cv_qualifier;
20589 /* Peek at the next token. */
20590 token = cp_lexer_peek_token (parser->lexer);
20591 /* See if it's a cv-qualifier. */
20592 switch (token->keyword)
20594 case RID_CONST:
20595 cv_qualifier = TYPE_QUAL_CONST;
20596 break;
20598 case RID_VOLATILE:
20599 cv_qualifier = TYPE_QUAL_VOLATILE;
20600 break;
20602 case RID_RESTRICT:
20603 cv_qualifier = TYPE_QUAL_RESTRICT;
20604 break;
20606 default:
20607 cv_qualifier = TYPE_UNQUALIFIED;
20608 break;
20611 if (!cv_qualifier)
20612 break;
20614 if (cv_quals & cv_qualifier)
20616 gcc_rich_location richloc (token->location);
20617 richloc.add_fixit_remove ();
20618 error_at (&richloc, "duplicate cv-qualifier");
20619 cp_lexer_purge_token (parser->lexer);
20621 else
20623 cp_lexer_consume_token (parser->lexer);
20624 cv_quals |= cv_qualifier;
20628 return cv_quals;
20631 /* Parse an (optional) ref-qualifier
20633 ref-qualifier:
20637 Returns cp_ref_qualifier representing ref-qualifier. */
20639 static cp_ref_qualifier
20640 cp_parser_ref_qualifier_opt (cp_parser* parser)
20642 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20644 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20645 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20646 return ref_qual;
20648 while (true)
20650 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20651 cp_token *token = cp_lexer_peek_token (parser->lexer);
20653 switch (token->type)
20655 case CPP_AND:
20656 curr_ref_qual = REF_QUAL_LVALUE;
20657 break;
20659 case CPP_AND_AND:
20660 curr_ref_qual = REF_QUAL_RVALUE;
20661 break;
20663 default:
20664 curr_ref_qual = REF_QUAL_NONE;
20665 break;
20668 if (!curr_ref_qual)
20669 break;
20670 else if (ref_qual)
20672 error_at (token->location, "multiple ref-qualifiers");
20673 cp_lexer_purge_token (parser->lexer);
20675 else
20677 ref_qual = curr_ref_qual;
20678 cp_lexer_consume_token (parser->lexer);
20682 return ref_qual;
20685 /* Parse an optional tx-qualifier.
20687 tx-qualifier:
20688 transaction_safe
20689 transaction_safe_dynamic */
20691 static tree
20692 cp_parser_tx_qualifier_opt (cp_parser *parser)
20694 cp_token *token = cp_lexer_peek_token (parser->lexer);
20695 if (token->type == CPP_NAME)
20697 tree name = token->u.value;
20698 const char *p = IDENTIFIER_POINTER (name);
20699 const int len = strlen ("transaction_safe");
20700 if (!strncmp (p, "transaction_safe", len))
20702 p += len;
20703 if (*p == '\0'
20704 || !strcmp (p, "_dynamic"))
20706 cp_lexer_consume_token (parser->lexer);
20707 if (!flag_tm)
20709 error ("%qE requires %<-fgnu-tm%>", name);
20710 return NULL_TREE;
20712 else
20713 return name;
20717 return NULL_TREE;
20720 /* Parse an (optional) virt-specifier-seq.
20722 virt-specifier-seq:
20723 virt-specifier virt-specifier-seq [opt]
20725 virt-specifier:
20726 override
20727 final
20729 Returns a bitmask representing the virt-specifiers. */
20731 static cp_virt_specifiers
20732 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20734 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20736 while (true)
20738 cp_token *token;
20739 cp_virt_specifiers virt_specifier;
20741 /* Peek at the next token. */
20742 token = cp_lexer_peek_token (parser->lexer);
20743 /* See if it's a virt-specifier-qualifier. */
20744 if (token->type != CPP_NAME)
20745 break;
20746 if (id_equal (token->u.value, "override"))
20748 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20749 virt_specifier = VIRT_SPEC_OVERRIDE;
20751 else if (id_equal (token->u.value, "final"))
20753 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20754 virt_specifier = VIRT_SPEC_FINAL;
20756 else if (id_equal (token->u.value, "__final"))
20758 virt_specifier = VIRT_SPEC_FINAL;
20760 else
20761 break;
20763 if (virt_specifiers & virt_specifier)
20765 gcc_rich_location richloc (token->location);
20766 richloc.add_fixit_remove ();
20767 error_at (&richloc, "duplicate virt-specifier");
20768 cp_lexer_purge_token (parser->lexer);
20770 else
20772 cp_lexer_consume_token (parser->lexer);
20773 virt_specifiers |= virt_specifier;
20776 return virt_specifiers;
20779 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20780 is in scope even though it isn't real. */
20782 void
20783 inject_this_parameter (tree ctype, cp_cv_quals quals)
20785 tree this_parm;
20787 if (current_class_ptr)
20789 /* We don't clear this between NSDMIs. Is it already what we want? */
20790 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20791 if (DECL_P (current_class_ptr)
20792 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20793 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20794 && cp_type_quals (type) == quals)
20795 return;
20798 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20799 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20800 current_class_ptr = NULL_TREE;
20801 current_class_ref
20802 = cp_build_fold_indirect_ref (this_parm);
20803 current_class_ptr = this_parm;
20806 /* Return true iff our current scope is a non-static data member
20807 initializer. */
20809 bool
20810 parsing_nsdmi (void)
20812 /* We recognize NSDMI context by the context-less 'this' pointer set up
20813 by the function above. */
20814 if (current_class_ptr
20815 && TREE_CODE (current_class_ptr) == PARM_DECL
20816 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20817 return true;
20818 return false;
20821 /* Parse a late-specified return type, if any. This is not a separate
20822 non-terminal, but part of a function declarator, which looks like
20824 -> trailing-type-specifier-seq abstract-declarator(opt)
20826 Returns the type indicated by the type-id.
20828 In addition to this, parse any queued up #pragma omp declare simd
20829 clauses, and #pragma acc routine clauses.
20831 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20832 function. */
20834 static tree
20835 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20836 tree& requires_clause, cp_cv_quals quals)
20838 cp_token *token;
20839 tree type = NULL_TREE;
20840 bool declare_simd_p = (parser->omp_declare_simd
20841 && declarator
20842 && declarator->kind == cdk_id);
20844 bool oacc_routine_p = (parser->oacc_routine
20845 && declarator
20846 && declarator->kind == cdk_id);
20848 /* Peek at the next token. */
20849 token = cp_lexer_peek_token (parser->lexer);
20850 /* A late-specified return type is indicated by an initial '->'. */
20851 if (token->type != CPP_DEREF
20852 && token->keyword != RID_REQUIRES
20853 && !(token->type == CPP_NAME
20854 && token->u.value == ridpointers[RID_REQUIRES])
20855 && !(declare_simd_p || oacc_routine_p))
20856 return NULL_TREE;
20858 tree save_ccp = current_class_ptr;
20859 tree save_ccr = current_class_ref;
20860 if (quals >= 0)
20862 /* DR 1207: 'this' is in scope in the trailing return type. */
20863 inject_this_parameter (current_class_type, quals);
20866 if (token->type == CPP_DEREF)
20868 /* Consume the ->. */
20869 cp_lexer_consume_token (parser->lexer);
20871 type = cp_parser_trailing_type_id (parser);
20874 /* Function declarations may be followed by a trailing
20875 requires-clause. */
20876 requires_clause = cp_parser_requires_clause_opt (parser);
20878 if (declare_simd_p)
20879 declarator->attributes
20880 = cp_parser_late_parsing_omp_declare_simd (parser,
20881 declarator->attributes);
20882 if (oacc_routine_p)
20883 declarator->attributes
20884 = cp_parser_late_parsing_oacc_routine (parser,
20885 declarator->attributes);
20887 if (quals >= 0)
20889 current_class_ptr = save_ccp;
20890 current_class_ref = save_ccr;
20893 return type;
20896 /* Parse a declarator-id.
20898 declarator-id:
20899 id-expression
20900 :: [opt] nested-name-specifier [opt] type-name
20902 In the `id-expression' case, the value returned is as for
20903 cp_parser_id_expression if the id-expression was an unqualified-id.
20904 If the id-expression was a qualified-id, then a SCOPE_REF is
20905 returned. The first operand is the scope (either a NAMESPACE_DECL
20906 or TREE_TYPE), but the second is still just a representation of an
20907 unqualified-id. */
20909 static tree
20910 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20912 tree id;
20913 /* The expression must be an id-expression. Assume that qualified
20914 names are the names of types so that:
20916 template <class T>
20917 int S<T>::R::i = 3;
20919 will work; we must treat `S<T>::R' as the name of a type.
20920 Similarly, assume that qualified names are templates, where
20921 required, so that:
20923 template <class T>
20924 int S<T>::R<T>::i = 3;
20926 will work, too. */
20927 id = cp_parser_id_expression (parser,
20928 /*template_keyword_p=*/false,
20929 /*check_dependency_p=*/false,
20930 /*template_p=*/NULL,
20931 /*declarator_p=*/true,
20932 optional_p);
20933 if (id && BASELINK_P (id))
20934 id = BASELINK_FUNCTIONS (id);
20935 return id;
20938 /* Parse a type-id.
20940 type-id:
20941 type-specifier-seq abstract-declarator [opt]
20943 Returns the TYPE specified. */
20945 static tree
20946 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20947 bool is_trailing_return)
20949 cp_decl_specifier_seq type_specifier_seq;
20950 cp_declarator *abstract_declarator;
20952 /* Parse the type-specifier-seq. */
20953 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20954 is_trailing_return,
20955 &type_specifier_seq);
20956 if (is_template_arg && type_specifier_seq.type
20957 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20958 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20959 /* A bare template name as a template argument is a template template
20960 argument, not a placeholder, so fail parsing it as a type argument. */
20962 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20963 cp_parser_simulate_error (parser);
20964 return error_mark_node;
20966 if (type_specifier_seq.type == error_mark_node)
20967 return error_mark_node;
20969 /* There might or might not be an abstract declarator. */
20970 cp_parser_parse_tentatively (parser);
20971 /* Look for the declarator. */
20972 abstract_declarator
20973 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20974 /*parenthesized_p=*/NULL,
20975 /*member_p=*/false,
20976 /*friend_p=*/false);
20977 /* Check to see if there really was a declarator. */
20978 if (!cp_parser_parse_definitely (parser))
20979 abstract_declarator = NULL;
20981 if (type_specifier_seq.type
20982 /* The concepts TS allows 'auto' as a type-id. */
20983 && (!flag_concepts || parser->in_type_id_in_expr_p)
20984 /* None of the valid uses of 'auto' in C++14 involve the type-id
20985 nonterminal, but it is valid in a trailing-return-type. */
20986 && !(cxx_dialect >= cxx14 && is_trailing_return))
20987 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20989 /* A type-id with type 'auto' is only ok if the abstract declarator
20990 is a function declarator with a late-specified return type.
20992 A type-id with 'auto' is also valid in a trailing-return-type
20993 in a compound-requirement. */
20994 if (abstract_declarator
20995 && abstract_declarator->kind == cdk_function
20996 && abstract_declarator->u.function.late_return_type)
20997 /* OK */;
20998 else if (parser->in_result_type_constraint_p)
20999 /* OK */;
21000 else
21002 location_t loc = type_specifier_seq.locations[ds_type_spec];
21003 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21005 error_at (loc, "missing template arguments after %qT",
21006 auto_node);
21007 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21008 tmpl);
21010 else
21011 error_at (loc, "invalid use of %qT", auto_node);
21012 return error_mark_node;
21016 return groktypename (&type_specifier_seq, abstract_declarator,
21017 is_template_arg);
21020 static tree
21021 cp_parser_type_id (cp_parser *parser)
21023 return cp_parser_type_id_1 (parser, false, false);
21026 static tree
21027 cp_parser_template_type_arg (cp_parser *parser)
21029 tree r;
21030 const char *saved_message = parser->type_definition_forbidden_message;
21031 parser->type_definition_forbidden_message
21032 = G_("types may not be defined in template arguments");
21033 r = cp_parser_type_id_1 (parser, true, false);
21034 parser->type_definition_forbidden_message = saved_message;
21035 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21037 error ("invalid use of %<auto%> in template argument");
21038 r = error_mark_node;
21040 return r;
21043 static tree
21044 cp_parser_trailing_type_id (cp_parser *parser)
21046 return cp_parser_type_id_1 (parser, false, true);
21049 /* Parse a type-specifier-seq.
21051 type-specifier-seq:
21052 type-specifier type-specifier-seq [opt]
21054 GNU extension:
21056 type-specifier-seq:
21057 attributes type-specifier-seq [opt]
21059 If IS_DECLARATION is true, we are at the start of a "condition" or
21060 exception-declaration, so we might be followed by a declarator-id.
21062 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21063 i.e. we've just seen "->".
21065 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21067 static void
21068 cp_parser_type_specifier_seq (cp_parser* parser,
21069 bool is_declaration,
21070 bool is_trailing_return,
21071 cp_decl_specifier_seq *type_specifier_seq)
21073 bool seen_type_specifier = false;
21074 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21075 cp_token *start_token = NULL;
21077 /* Clear the TYPE_SPECIFIER_SEQ. */
21078 clear_decl_specs (type_specifier_seq);
21080 /* In the context of a trailing return type, enum E { } is an
21081 elaborated-type-specifier followed by a function-body, not an
21082 enum-specifier. */
21083 if (is_trailing_return)
21084 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21086 /* Parse the type-specifiers and attributes. */
21087 while (true)
21089 tree type_specifier;
21090 bool is_cv_qualifier;
21092 /* Check for attributes first. */
21093 if (cp_next_tokens_can_be_attribute_p (parser))
21095 type_specifier_seq->attributes
21096 = attr_chainon (type_specifier_seq->attributes,
21097 cp_parser_attributes_opt (parser));
21098 continue;
21101 /* record the token of the beginning of the type specifier seq,
21102 for error reporting purposes*/
21103 if (!start_token)
21104 start_token = cp_lexer_peek_token (parser->lexer);
21106 /* Look for the type-specifier. */
21107 type_specifier = cp_parser_type_specifier (parser,
21108 flags,
21109 type_specifier_seq,
21110 /*is_declaration=*/false,
21111 NULL,
21112 &is_cv_qualifier);
21113 if (!type_specifier)
21115 /* If the first type-specifier could not be found, this is not a
21116 type-specifier-seq at all. */
21117 if (!seen_type_specifier)
21119 /* Set in_declarator_p to avoid skipping to the semicolon. */
21120 int in_decl = parser->in_declarator_p;
21121 parser->in_declarator_p = true;
21123 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21124 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21125 cp_parser_error (parser, "expected type-specifier");
21127 parser->in_declarator_p = in_decl;
21129 type_specifier_seq->type = error_mark_node;
21130 return;
21132 /* If subsequent type-specifiers could not be found, the
21133 type-specifier-seq is complete. */
21134 break;
21137 seen_type_specifier = true;
21138 /* The standard says that a condition can be:
21140 type-specifier-seq declarator = assignment-expression
21142 However, given:
21144 struct S {};
21145 if (int S = ...)
21147 we should treat the "S" as a declarator, not as a
21148 type-specifier. The standard doesn't say that explicitly for
21149 type-specifier-seq, but it does say that for
21150 decl-specifier-seq in an ordinary declaration. Perhaps it
21151 would be clearer just to allow a decl-specifier-seq here, and
21152 then add a semantic restriction that if any decl-specifiers
21153 that are not type-specifiers appear, the program is invalid. */
21154 if (is_declaration && !is_cv_qualifier)
21155 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21159 /* Return whether the function currently being declared has an associated
21160 template parameter list. */
21162 static bool
21163 function_being_declared_is_template_p (cp_parser* parser)
21165 if (!current_template_parms || processing_template_parmlist)
21166 return false;
21168 if (parser->implicit_template_scope)
21169 return true;
21171 if (at_class_scope_p ()
21172 && TYPE_BEING_DEFINED (current_class_type))
21173 return parser->num_template_parameter_lists != 0;
21175 return ((int) parser->num_template_parameter_lists > template_class_depth
21176 (current_class_type));
21179 /* Parse a parameter-declaration-clause.
21181 parameter-declaration-clause:
21182 parameter-declaration-list [opt] ... [opt]
21183 parameter-declaration-list , ...
21185 Returns a representation for the parameter declarations. A return
21186 value of NULL indicates a parameter-declaration-clause consisting
21187 only of an ellipsis. */
21189 static tree
21190 cp_parser_parameter_declaration_clause (cp_parser* parser)
21192 tree parameters;
21193 cp_token *token;
21194 bool ellipsis_p;
21195 bool is_error;
21197 struct cleanup {
21198 cp_parser* parser;
21199 int auto_is_implicit_function_template_parm_p;
21200 ~cleanup() {
21201 parser->auto_is_implicit_function_template_parm_p
21202 = auto_is_implicit_function_template_parm_p;
21204 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21206 (void) cleanup;
21208 if (!processing_specialization
21209 && !processing_template_parmlist
21210 && !processing_explicit_instantiation
21211 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21212 actual function or a random abstract declarator. */
21213 && parser->default_arg_ok_p)
21214 if (!current_function_decl
21215 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21216 parser->auto_is_implicit_function_template_parm_p = true;
21218 /* Peek at the next token. */
21219 token = cp_lexer_peek_token (parser->lexer);
21220 /* Check for trivial parameter-declaration-clauses. */
21221 if (token->type == CPP_ELLIPSIS)
21223 /* Consume the `...' token. */
21224 cp_lexer_consume_token (parser->lexer);
21225 return NULL_TREE;
21227 else if (token->type == CPP_CLOSE_PAREN)
21228 /* There are no parameters. */
21230 #ifndef NO_IMPLICIT_EXTERN_C
21231 if (in_system_header_at (input_location)
21232 && current_class_type == NULL
21233 && current_lang_name == lang_name_c)
21234 return NULL_TREE;
21235 else
21236 #endif
21237 return void_list_node;
21239 /* Check for `(void)', too, which is a special case. */
21240 else if (token->keyword == RID_VOID
21241 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21242 == CPP_CLOSE_PAREN))
21244 /* Consume the `void' token. */
21245 cp_lexer_consume_token (parser->lexer);
21246 /* There are no parameters. */
21247 return void_list_node;
21250 /* Parse the parameter-declaration-list. */
21251 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21252 /* If a parse error occurred while parsing the
21253 parameter-declaration-list, then the entire
21254 parameter-declaration-clause is erroneous. */
21255 if (is_error)
21256 return NULL;
21258 /* Peek at the next token. */
21259 token = cp_lexer_peek_token (parser->lexer);
21260 /* If it's a `,', the clause should terminate with an ellipsis. */
21261 if (token->type == CPP_COMMA)
21263 /* Consume the `,'. */
21264 cp_lexer_consume_token (parser->lexer);
21265 /* Expect an ellipsis. */
21266 ellipsis_p
21267 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21269 /* It might also be `...' if the optional trailing `,' was
21270 omitted. */
21271 else if (token->type == CPP_ELLIPSIS)
21273 /* Consume the `...' token. */
21274 cp_lexer_consume_token (parser->lexer);
21275 /* And remember that we saw it. */
21276 ellipsis_p = true;
21278 else
21279 ellipsis_p = false;
21281 /* Finish the parameter list. */
21282 if (!ellipsis_p)
21283 parameters = chainon (parameters, void_list_node);
21285 return parameters;
21288 /* Parse a parameter-declaration-list.
21290 parameter-declaration-list:
21291 parameter-declaration
21292 parameter-declaration-list , parameter-declaration
21294 Returns a representation of the parameter-declaration-list, as for
21295 cp_parser_parameter_declaration_clause. However, the
21296 `void_list_node' is never appended to the list. Upon return,
21297 *IS_ERROR will be true iff an error occurred. */
21299 static tree
21300 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21302 tree parameters = NULL_TREE;
21303 tree *tail = &parameters;
21304 bool saved_in_unbraced_linkage_specification_p;
21305 int index = 0;
21307 /* Assume all will go well. */
21308 *is_error = false;
21309 /* The special considerations that apply to a function within an
21310 unbraced linkage specifications do not apply to the parameters
21311 to the function. */
21312 saved_in_unbraced_linkage_specification_p
21313 = parser->in_unbraced_linkage_specification_p;
21314 parser->in_unbraced_linkage_specification_p = false;
21316 /* Look for more parameters. */
21317 while (true)
21319 cp_parameter_declarator *parameter;
21320 tree decl = error_mark_node;
21321 bool parenthesized_p = false;
21322 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21323 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21324 (current_template_parms)) : 0);
21326 /* Parse the parameter. */
21327 parameter
21328 = cp_parser_parameter_declaration (parser,
21329 /*template_parm_p=*/false,
21330 &parenthesized_p);
21332 /* We don't know yet if the enclosing context is deprecated, so wait
21333 and warn in grokparms if appropriate. */
21334 deprecated_state = DEPRECATED_SUPPRESS;
21336 if (parameter)
21338 /* If a function parameter pack was specified and an implicit template
21339 parameter was introduced during cp_parser_parameter_declaration,
21340 change any implicit parameters introduced into packs. */
21341 if (parser->implicit_template_parms
21342 && parameter->declarator
21343 && parameter->declarator->parameter_pack_p)
21345 int latest_template_parm_idx = TREE_VEC_LENGTH
21346 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21348 if (latest_template_parm_idx != template_parm_idx)
21349 parameter->decl_specifiers.type = convert_generic_types_to_packs
21350 (parameter->decl_specifiers.type,
21351 template_parm_idx, latest_template_parm_idx);
21354 decl = grokdeclarator (parameter->declarator,
21355 &parameter->decl_specifiers,
21356 PARM,
21357 parameter->default_argument != NULL_TREE,
21358 &parameter->decl_specifiers.attributes);
21359 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21360 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21363 deprecated_state = DEPRECATED_NORMAL;
21365 /* If a parse error occurred parsing the parameter declaration,
21366 then the entire parameter-declaration-list is erroneous. */
21367 if (decl == error_mark_node)
21369 *is_error = true;
21370 parameters = error_mark_node;
21371 break;
21374 if (parameter->decl_specifiers.attributes)
21375 cplus_decl_attributes (&decl,
21376 parameter->decl_specifiers.attributes,
21378 if (DECL_NAME (decl))
21379 decl = pushdecl (decl);
21381 if (decl != error_mark_node)
21383 retrofit_lang_decl (decl);
21384 DECL_PARM_INDEX (decl) = ++index;
21385 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21388 /* Add the new parameter to the list. */
21389 *tail = build_tree_list (parameter->default_argument, decl);
21390 tail = &TREE_CHAIN (*tail);
21392 /* Peek at the next token. */
21393 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21394 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21395 /* These are for Objective-C++ */
21396 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21397 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21398 /* The parameter-declaration-list is complete. */
21399 break;
21400 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21402 cp_token *token;
21404 /* Peek at the next token. */
21405 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21406 /* If it's an ellipsis, then the list is complete. */
21407 if (token->type == CPP_ELLIPSIS)
21408 break;
21409 /* Otherwise, there must be more parameters. Consume the
21410 `,'. */
21411 cp_lexer_consume_token (parser->lexer);
21412 /* When parsing something like:
21414 int i(float f, double d)
21416 we can tell after seeing the declaration for "f" that we
21417 are not looking at an initialization of a variable "i",
21418 but rather at the declaration of a function "i".
21420 Due to the fact that the parsing of template arguments
21421 (as specified to a template-id) requires backtracking we
21422 cannot use this technique when inside a template argument
21423 list. */
21424 if (!parser->in_template_argument_list_p
21425 && !parser->in_type_id_in_expr_p
21426 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21427 /* However, a parameter-declaration of the form
21428 "float(f)" (which is a valid declaration of a
21429 parameter "f") can also be interpreted as an
21430 expression (the conversion of "f" to "float"). */
21431 && !parenthesized_p)
21432 cp_parser_commit_to_tentative_parse (parser);
21434 else
21436 cp_parser_error (parser, "expected %<,%> or %<...%>");
21437 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21438 cp_parser_skip_to_closing_parenthesis (parser,
21439 /*recovering=*/true,
21440 /*or_comma=*/false,
21441 /*consume_paren=*/false);
21442 break;
21446 parser->in_unbraced_linkage_specification_p
21447 = saved_in_unbraced_linkage_specification_p;
21449 /* Reset implicit_template_scope if we are about to leave the function
21450 parameter list that introduced it. Note that for out-of-line member
21451 definitions, there will be one or more class scopes before we get to
21452 the template parameter scope. */
21454 if (cp_binding_level *its = parser->implicit_template_scope)
21455 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21457 while (maybe_its->kind == sk_class)
21458 maybe_its = maybe_its->level_chain;
21459 if (maybe_its == its)
21461 parser->implicit_template_parms = 0;
21462 parser->implicit_template_scope = 0;
21466 return parameters;
21469 /* Parse a parameter declaration.
21471 parameter-declaration:
21472 decl-specifier-seq ... [opt] declarator
21473 decl-specifier-seq declarator = assignment-expression
21474 decl-specifier-seq ... [opt] abstract-declarator [opt]
21475 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21477 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21478 declares a template parameter. (In that case, a non-nested `>'
21479 token encountered during the parsing of the assignment-expression
21480 is not interpreted as a greater-than operator.)
21482 Returns a representation of the parameter, or NULL if an error
21483 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21484 true iff the declarator is of the form "(p)". */
21486 static cp_parameter_declarator *
21487 cp_parser_parameter_declaration (cp_parser *parser,
21488 bool template_parm_p,
21489 bool *parenthesized_p)
21491 int declares_class_or_enum;
21492 cp_decl_specifier_seq decl_specifiers;
21493 cp_declarator *declarator;
21494 tree default_argument;
21495 cp_token *token = NULL, *declarator_token_start = NULL;
21496 const char *saved_message;
21497 bool template_parameter_pack_p = false;
21499 /* In a template parameter, `>' is not an operator.
21501 [temp.param]
21503 When parsing a default template-argument for a non-type
21504 template-parameter, the first non-nested `>' is taken as the end
21505 of the template parameter-list rather than a greater-than
21506 operator. */
21508 /* Type definitions may not appear in parameter types. */
21509 saved_message = parser->type_definition_forbidden_message;
21510 parser->type_definition_forbidden_message
21511 = G_("types may not be defined in parameter types");
21513 /* Parse the declaration-specifiers. */
21514 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21515 cp_parser_decl_specifier_seq (parser,
21516 CP_PARSER_FLAGS_NONE,
21517 &decl_specifiers,
21518 &declares_class_or_enum);
21520 /* Complain about missing 'typename' or other invalid type names. */
21521 if (!decl_specifiers.any_type_specifiers_p
21522 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21523 decl_specifiers.type = error_mark_node;
21525 /* If an error occurred, there's no reason to attempt to parse the
21526 rest of the declaration. */
21527 if (cp_parser_error_occurred (parser))
21529 parser->type_definition_forbidden_message = saved_message;
21530 return NULL;
21533 /* Peek at the next token. */
21534 token = cp_lexer_peek_token (parser->lexer);
21536 /* If the next token is a `)', `,', `=', `>', or `...', then there
21537 is no declarator. However, when variadic templates are enabled,
21538 there may be a declarator following `...'. */
21539 if (token->type == CPP_CLOSE_PAREN
21540 || token->type == CPP_COMMA
21541 || token->type == CPP_EQ
21542 || token->type == CPP_GREATER)
21544 declarator = NULL;
21545 if (parenthesized_p)
21546 *parenthesized_p = false;
21548 /* Otherwise, there should be a declarator. */
21549 else
21551 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21552 parser->default_arg_ok_p = false;
21554 /* After seeing a decl-specifier-seq, if the next token is not a
21555 "(", there is no possibility that the code is a valid
21556 expression. Therefore, if parsing tentatively, we commit at
21557 this point. */
21558 if (!parser->in_template_argument_list_p
21559 /* In an expression context, having seen:
21561 (int((char ...
21563 we cannot be sure whether we are looking at a
21564 function-type (taking a "char" as a parameter) or a cast
21565 of some object of type "char" to "int". */
21566 && !parser->in_type_id_in_expr_p
21567 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21568 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21569 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21570 cp_parser_commit_to_tentative_parse (parser);
21571 /* Parse the declarator. */
21572 declarator_token_start = token;
21573 declarator = cp_parser_declarator (parser,
21574 CP_PARSER_DECLARATOR_EITHER,
21575 /*ctor_dtor_or_conv_p=*/NULL,
21576 parenthesized_p,
21577 /*member_p=*/false,
21578 /*friend_p=*/false);
21579 parser->default_arg_ok_p = saved_default_arg_ok_p;
21580 /* After the declarator, allow more attributes. */
21581 decl_specifiers.attributes
21582 = attr_chainon (decl_specifiers.attributes,
21583 cp_parser_attributes_opt (parser));
21585 /* If the declarator is a template parameter pack, remember that and
21586 clear the flag in the declarator itself so we don't get errors
21587 from grokdeclarator. */
21588 if (template_parm_p && declarator && declarator->parameter_pack_p)
21590 declarator->parameter_pack_p = false;
21591 template_parameter_pack_p = true;
21595 /* If the next token is an ellipsis, and we have not seen a declarator
21596 name, and if either the type of the declarator contains parameter
21597 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21598 for, eg, abbreviated integral type names), then we actually have a
21599 parameter pack expansion expression. Otherwise, leave the ellipsis
21600 for a C-style variadic function. */
21601 token = cp_lexer_peek_token (parser->lexer);
21602 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21604 tree type = decl_specifiers.type;
21606 if (type && DECL_P (type))
21607 type = TREE_TYPE (type);
21609 if (((type
21610 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21611 && (template_parm_p || uses_parameter_packs (type)))
21612 || (!type && template_parm_p))
21613 && declarator_can_be_parameter_pack (declarator))
21615 /* Consume the `...'. */
21616 cp_lexer_consume_token (parser->lexer);
21617 maybe_warn_variadic_templates ();
21619 /* Build a pack expansion type */
21620 if (template_parm_p)
21621 template_parameter_pack_p = true;
21622 else if (declarator)
21623 declarator->parameter_pack_p = true;
21624 else
21625 decl_specifiers.type = make_pack_expansion (type);
21629 /* The restriction on defining new types applies only to the type
21630 of the parameter, not to the default argument. */
21631 parser->type_definition_forbidden_message = saved_message;
21633 /* If the next token is `=', then process a default argument. */
21634 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21636 tree type = decl_specifiers.type;
21637 token = cp_lexer_peek_token (parser->lexer);
21638 /* If we are defining a class, then the tokens that make up the
21639 default argument must be saved and processed later. */
21640 if (!template_parm_p && at_class_scope_p ()
21641 && TYPE_BEING_DEFINED (current_class_type)
21642 && !LAMBDA_TYPE_P (current_class_type))
21643 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21645 // A constrained-type-specifier may declare a type template-parameter.
21646 else if (declares_constrained_type_template_parameter (type))
21647 default_argument
21648 = cp_parser_default_type_template_argument (parser);
21650 // A constrained-type-specifier may declare a template-template-parameter.
21651 else if (declares_constrained_template_template_parameter (type))
21652 default_argument
21653 = cp_parser_default_template_template_argument (parser);
21655 /* Outside of a class definition, we can just parse the
21656 assignment-expression. */
21657 else
21658 default_argument
21659 = cp_parser_default_argument (parser, template_parm_p);
21661 if (!parser->default_arg_ok_p)
21663 permerror (token->location,
21664 "default arguments are only "
21665 "permitted for function parameters");
21667 else if ((declarator && declarator->parameter_pack_p)
21668 || template_parameter_pack_p
21669 || (decl_specifiers.type
21670 && PACK_EXPANSION_P (decl_specifiers.type)))
21672 /* Find the name of the parameter pack. */
21673 cp_declarator *id_declarator = declarator;
21674 while (id_declarator && id_declarator->kind != cdk_id)
21675 id_declarator = id_declarator->declarator;
21677 if (id_declarator && id_declarator->kind == cdk_id)
21678 error_at (declarator_token_start->location,
21679 template_parm_p
21680 ? G_("template parameter pack %qD "
21681 "cannot have a default argument")
21682 : G_("parameter pack %qD cannot have "
21683 "a default argument"),
21684 id_declarator->u.id.unqualified_name);
21685 else
21686 error_at (declarator_token_start->location,
21687 template_parm_p
21688 ? G_("template parameter pack cannot have "
21689 "a default argument")
21690 : G_("parameter pack cannot have a "
21691 "default argument"));
21693 default_argument = NULL_TREE;
21696 else
21697 default_argument = NULL_TREE;
21699 /* Generate a location for the parameter, ranging from the start of the
21700 initial token to the end of the final token (using input_location for
21701 the latter, set up by cp_lexer_set_source_position_from_token when
21702 consuming tokens).
21704 If we have a identifier, then use it for the caret location, e.g.
21706 extern int callee (int one, int (*two)(int, int), float three);
21707 ~~~~~~^~~~~~~~~~~~~~
21709 otherwise, reuse the start location for the caret location e.g.:
21711 extern int callee (int one, int (*)(int, int), float three);
21712 ^~~~~~~~~~~~~~~~~
21715 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21716 ? declarator->id_loc
21717 : decl_spec_token_start->location);
21718 location_t param_loc = make_location (caret_loc,
21719 decl_spec_token_start->location,
21720 input_location);
21722 return make_parameter_declarator (&decl_specifiers,
21723 declarator,
21724 default_argument,
21725 param_loc,
21726 template_parameter_pack_p);
21729 /* Parse a default argument and return it.
21731 TEMPLATE_PARM_P is true if this is a default argument for a
21732 non-type template parameter. */
21733 static tree
21734 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21736 tree default_argument = NULL_TREE;
21737 bool saved_greater_than_is_operator_p;
21738 bool saved_local_variables_forbidden_p;
21739 bool non_constant_p, is_direct_init;
21741 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21742 set correctly. */
21743 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21744 parser->greater_than_is_operator_p = !template_parm_p;
21745 /* Local variable names (and the `this' keyword) may not
21746 appear in a default argument. */
21747 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21748 parser->local_variables_forbidden_p = true;
21749 /* Parse the assignment-expression. */
21750 if (template_parm_p)
21751 push_deferring_access_checks (dk_no_deferred);
21752 tree saved_class_ptr = NULL_TREE;
21753 tree saved_class_ref = NULL_TREE;
21754 /* The "this" pointer is not valid in a default argument. */
21755 if (cfun)
21757 saved_class_ptr = current_class_ptr;
21758 cp_function_chain->x_current_class_ptr = NULL_TREE;
21759 saved_class_ref = current_class_ref;
21760 cp_function_chain->x_current_class_ref = NULL_TREE;
21762 default_argument
21763 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21764 /* Restore the "this" pointer. */
21765 if (cfun)
21767 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21768 cp_function_chain->x_current_class_ref = saved_class_ref;
21770 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21771 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21772 if (template_parm_p)
21773 pop_deferring_access_checks ();
21774 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21775 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21777 return default_argument;
21780 /* Parse a function-body.
21782 function-body:
21783 compound_statement */
21785 static void
21786 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21788 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21789 ? BCS_TRY_BLOCK : BCS_NORMAL),
21790 true);
21793 /* Parse a ctor-initializer-opt followed by a function-body. Return
21794 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21795 is true we are parsing a function-try-block. */
21797 static void
21798 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21799 bool in_function_try_block)
21801 tree body, list;
21802 const bool check_body_p =
21803 DECL_CONSTRUCTOR_P (current_function_decl)
21804 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21805 tree last = NULL;
21807 /* Begin the function body. */
21808 body = begin_function_body ();
21809 /* Parse the optional ctor-initializer. */
21810 cp_parser_ctor_initializer_opt (parser);
21812 /* If we're parsing a constexpr constructor definition, we need
21813 to check that the constructor body is indeed empty. However,
21814 before we get to cp_parser_function_body lot of junk has been
21815 generated, so we can't just check that we have an empty block.
21816 Rather we take a snapshot of the outermost block, and check whether
21817 cp_parser_function_body changed its state. */
21818 if (check_body_p)
21820 list = cur_stmt_list;
21821 if (STATEMENT_LIST_TAIL (list))
21822 last = STATEMENT_LIST_TAIL (list)->stmt;
21824 /* Parse the function-body. */
21825 cp_parser_function_body (parser, in_function_try_block);
21826 if (check_body_p)
21827 check_constexpr_ctor_body (last, list, /*complain=*/true);
21828 /* Finish the function body. */
21829 finish_function_body (body);
21832 /* Parse an initializer.
21834 initializer:
21835 = initializer-clause
21836 ( expression-list )
21838 Returns an expression representing the initializer. If no
21839 initializer is present, NULL_TREE is returned.
21841 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21842 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21843 set to TRUE if there is no initializer present. If there is an
21844 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21845 is set to true; otherwise it is set to false. */
21847 static tree
21848 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21849 bool* non_constant_p)
21851 cp_token *token;
21852 tree init;
21854 /* Peek at the next token. */
21855 token = cp_lexer_peek_token (parser->lexer);
21857 /* Let our caller know whether or not this initializer was
21858 parenthesized. */
21859 *is_direct_init = (token->type != CPP_EQ);
21860 /* Assume that the initializer is constant. */
21861 *non_constant_p = false;
21863 if (token->type == CPP_EQ)
21865 /* Consume the `='. */
21866 cp_lexer_consume_token (parser->lexer);
21867 /* Parse the initializer-clause. */
21868 init = cp_parser_initializer_clause (parser, non_constant_p);
21870 else if (token->type == CPP_OPEN_PAREN)
21872 vec<tree, va_gc> *vec;
21873 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21874 /*cast_p=*/false,
21875 /*allow_expansion_p=*/true,
21876 non_constant_p);
21877 if (vec == NULL)
21878 return error_mark_node;
21879 init = build_tree_list_vec (vec);
21880 release_tree_vector (vec);
21882 else if (token->type == CPP_OPEN_BRACE)
21884 cp_lexer_set_source_position (parser->lexer);
21885 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21886 init = cp_parser_braced_list (parser, non_constant_p);
21887 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21889 else
21891 /* Anything else is an error. */
21892 cp_parser_error (parser, "expected initializer");
21893 init = error_mark_node;
21896 if (check_for_bare_parameter_packs (init))
21897 init = error_mark_node;
21899 return init;
21902 /* Parse an initializer-clause.
21904 initializer-clause:
21905 assignment-expression
21906 braced-init-list
21908 Returns an expression representing the initializer.
21910 If the `assignment-expression' production is used the value
21911 returned is simply a representation for the expression.
21913 Otherwise, calls cp_parser_braced_list. */
21915 static cp_expr
21916 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21918 cp_expr initializer;
21920 /* Assume the expression is constant. */
21921 *non_constant_p = false;
21923 /* If it is not a `{', then we are looking at an
21924 assignment-expression. */
21925 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21927 initializer
21928 = cp_parser_constant_expression (parser,
21929 /*allow_non_constant_p=*/true,
21930 non_constant_p);
21932 else
21933 initializer = cp_parser_braced_list (parser, non_constant_p);
21935 return initializer;
21938 /* Parse a brace-enclosed initializer list.
21940 braced-init-list:
21941 { initializer-list , [opt] }
21942 { designated-initializer-list , [opt] }
21945 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21946 the elements of the initializer-list (or NULL, if the last
21947 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21948 NULL_TREE. There is no way to detect whether or not the optional
21949 trailing `,' was provided. NON_CONSTANT_P is as for
21950 cp_parser_initializer. */
21952 static cp_expr
21953 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21955 tree initializer;
21956 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21958 /* Consume the `{' token. */
21959 matching_braces braces;
21960 braces.require_open (parser);
21961 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21962 initializer = make_node (CONSTRUCTOR);
21963 /* If it's not a `}', then there is a non-trivial initializer. */
21964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21966 /* Parse the initializer list. */
21967 CONSTRUCTOR_ELTS (initializer)
21968 = cp_parser_initializer_list (parser, non_constant_p);
21969 /* A trailing `,' token is allowed. */
21970 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21971 cp_lexer_consume_token (parser->lexer);
21973 else
21974 *non_constant_p = false;
21975 /* Now, there should be a trailing `}'. */
21976 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21977 braces.require_close (parser);
21978 TREE_TYPE (initializer) = init_list_type_node;
21980 cp_expr result (initializer);
21981 /* Build a location of the form:
21982 { ... }
21983 ^~~~~~~
21984 with caret==start at the open brace, finish at the close brace. */
21985 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21986 result.set_location (combined_loc);
21987 return result;
21990 /* Consume tokens up to, and including, the next non-nested closing `]'.
21991 Returns true iff we found a closing `]'. */
21993 static bool
21994 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21996 unsigned square_depth = 0;
21998 while (true)
22000 cp_token * token = cp_lexer_peek_token (parser->lexer);
22002 switch (token->type)
22004 case CPP_EOF:
22005 case CPP_PRAGMA_EOL:
22006 /* If we've run out of tokens, then there is no closing `]'. */
22007 return false;
22009 case CPP_OPEN_SQUARE:
22010 ++square_depth;
22011 break;
22013 case CPP_CLOSE_SQUARE:
22014 if (!square_depth--)
22016 cp_lexer_consume_token (parser->lexer);
22017 return true;
22019 break;
22021 default:
22022 break;
22025 /* Consume the token. */
22026 cp_lexer_consume_token (parser->lexer);
22030 /* Return true if we are looking at an array-designator, false otherwise. */
22032 static bool
22033 cp_parser_array_designator_p (cp_parser *parser)
22035 /* Consume the `['. */
22036 cp_lexer_consume_token (parser->lexer);
22038 cp_lexer_save_tokens (parser->lexer);
22040 /* Skip tokens until the next token is a closing square bracket.
22041 If we find the closing `]', and the next token is a `=', then
22042 we are looking at an array designator. */
22043 bool array_designator_p
22044 = (cp_parser_skip_to_closing_square_bracket (parser)
22045 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22047 /* Roll back the tokens we skipped. */
22048 cp_lexer_rollback_tokens (parser->lexer);
22050 return array_designator_p;
22053 /* Parse an initializer-list.
22055 initializer-list:
22056 initializer-clause ... [opt]
22057 initializer-list , initializer-clause ... [opt]
22059 C++2A Extension:
22061 designated-initializer-list:
22062 designated-initializer-clause
22063 designated-initializer-list , designated-initializer-clause
22065 designated-initializer-clause:
22066 designator brace-or-equal-initializer
22068 designator:
22069 . identifier
22071 GNU Extension:
22073 initializer-list:
22074 designation initializer-clause ...[opt]
22075 initializer-list , designation initializer-clause ...[opt]
22077 designation:
22078 . identifier =
22079 identifier :
22080 [ constant-expression ] =
22082 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22083 for the initializer. If the INDEX of the elt is non-NULL, it is the
22084 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22085 as for cp_parser_initializer. */
22087 static vec<constructor_elt, va_gc> *
22088 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22090 vec<constructor_elt, va_gc> *v = NULL;
22091 bool first_p = true;
22092 tree first_designator = NULL_TREE;
22094 /* Assume all of the expressions are constant. */
22095 *non_constant_p = false;
22097 /* Parse the rest of the list. */
22098 while (true)
22100 cp_token *token;
22101 tree designator;
22102 tree initializer;
22103 bool clause_non_constant_p;
22104 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22106 /* Handle the C++2A syntax, '. id ='. */
22107 if ((cxx_dialect >= cxx2a
22108 || cp_parser_allow_gnu_extensions_p (parser))
22109 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22110 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22111 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22112 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22113 == CPP_OPEN_BRACE)))
22115 if (cxx_dialect < cxx2a)
22116 pedwarn (loc, OPT_Wpedantic,
22117 "C++ designated initializers only available with "
22118 "-std=c++2a or -std=gnu++2a");
22119 /* Consume the `.'. */
22120 cp_lexer_consume_token (parser->lexer);
22121 /* Consume the identifier. */
22122 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22123 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22124 /* Consume the `='. */
22125 cp_lexer_consume_token (parser->lexer);
22127 /* Also, if the next token is an identifier and the following one is a
22128 colon, we are looking at the GNU designated-initializer
22129 syntax. */
22130 else if (cp_parser_allow_gnu_extensions_p (parser)
22131 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22132 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22133 == CPP_COLON))
22135 /* Warn the user that they are using an extension. */
22136 pedwarn (loc, OPT_Wpedantic,
22137 "ISO C++ does not allow GNU designated initializers");
22138 /* Consume the identifier. */
22139 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22140 /* Consume the `:'. */
22141 cp_lexer_consume_token (parser->lexer);
22143 /* Also handle C99 array designators, '[ const ] ='. */
22144 else if (cp_parser_allow_gnu_extensions_p (parser)
22145 && !c_dialect_objc ()
22146 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22148 /* In C++11, [ could start a lambda-introducer. */
22149 bool non_const = false;
22151 cp_parser_parse_tentatively (parser);
22153 if (!cp_parser_array_designator_p (parser))
22155 cp_parser_simulate_error (parser);
22156 designator = NULL_TREE;
22158 else
22160 designator = cp_parser_constant_expression (parser, true,
22161 &non_const);
22162 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22163 cp_parser_require (parser, CPP_EQ, RT_EQ);
22166 if (!cp_parser_parse_definitely (parser))
22167 designator = NULL_TREE;
22168 else if (non_const
22169 && (!require_potential_rvalue_constant_expression
22170 (designator)))
22171 designator = NULL_TREE;
22172 if (designator)
22173 /* Warn the user that they are using an extension. */
22174 pedwarn (loc, OPT_Wpedantic,
22175 "ISO C++ does not allow C99 designated initializers");
22177 else
22178 designator = NULL_TREE;
22180 if (first_p)
22182 first_designator = designator;
22183 first_p = false;
22185 else if (cxx_dialect >= cxx2a
22186 && first_designator != error_mark_node
22187 && (!first_designator != !designator))
22189 error_at (loc, "either all initializer clauses should be designated "
22190 "or none of them should be");
22191 first_designator = error_mark_node;
22193 else if (cxx_dialect < cxx2a && !first_designator)
22194 first_designator = designator;
22196 /* Parse the initializer. */
22197 initializer = cp_parser_initializer_clause (parser,
22198 &clause_non_constant_p);
22199 /* If any clause is non-constant, so is the entire initializer. */
22200 if (clause_non_constant_p)
22201 *non_constant_p = true;
22203 /* If we have an ellipsis, this is an initializer pack
22204 expansion. */
22205 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22207 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22209 /* Consume the `...'. */
22210 cp_lexer_consume_token (parser->lexer);
22212 if (designator && cxx_dialect >= cxx2a)
22213 error_at (loc,
22214 "%<...%> not allowed in designated initializer list");
22216 /* Turn the initializer into an initializer expansion. */
22217 initializer = make_pack_expansion (initializer);
22220 /* Add it to the vector. */
22221 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22223 /* If the next token is not a comma, we have reached the end of
22224 the list. */
22225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22226 break;
22228 /* Peek at the next token. */
22229 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22230 /* If the next token is a `}', then we're still done. An
22231 initializer-clause can have a trailing `,' after the
22232 initializer-list and before the closing `}'. */
22233 if (token->type == CPP_CLOSE_BRACE)
22234 break;
22236 /* Consume the `,' token. */
22237 cp_lexer_consume_token (parser->lexer);
22240 /* The same identifier shall not appear in multiple designators
22241 of a designated-initializer-list. */
22242 if (first_designator)
22244 unsigned int i;
22245 tree designator, val;
22246 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22247 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22249 if (IDENTIFIER_MARKED (designator))
22251 error_at (EXPR_LOC_OR_LOC (val, input_location),
22252 "%<.%s%> designator used multiple times in "
22253 "the same initializer list",
22254 IDENTIFIER_POINTER (designator));
22255 (*v)[i].index = NULL_TREE;
22257 else
22258 IDENTIFIER_MARKED (designator) = 1;
22260 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22261 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22262 IDENTIFIER_MARKED (designator) = 0;
22265 return v;
22268 /* Classes [gram.class] */
22270 /* Parse a class-name.
22272 class-name:
22273 identifier
22274 template-id
22276 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22277 to indicate that names looked up in dependent types should be
22278 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22279 keyword has been used to indicate that the name that appears next
22280 is a template. TAG_TYPE indicates the explicit tag given before
22281 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22282 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22283 is the class being defined in a class-head. If ENUM_OK is TRUE,
22284 enum-names are also accepted.
22286 Returns the TYPE_DECL representing the class. */
22288 static tree
22289 cp_parser_class_name (cp_parser *parser,
22290 bool typename_keyword_p,
22291 bool template_keyword_p,
22292 enum tag_types tag_type,
22293 bool check_dependency_p,
22294 bool class_head_p,
22295 bool is_declaration,
22296 bool enum_ok)
22298 tree decl;
22299 tree scope;
22300 bool typename_p;
22301 cp_token *token;
22302 tree identifier = NULL_TREE;
22304 /* All class-names start with an identifier. */
22305 token = cp_lexer_peek_token (parser->lexer);
22306 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22308 cp_parser_error (parser, "expected class-name");
22309 return error_mark_node;
22312 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22313 to a template-id, so we save it here. */
22314 scope = parser->scope;
22315 if (scope == error_mark_node)
22316 return error_mark_node;
22318 /* Any name names a type if we're following the `typename' keyword
22319 in a qualified name where the enclosing scope is type-dependent. */
22320 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22321 && dependent_type_p (scope));
22322 /* Handle the common case (an identifier, but not a template-id)
22323 efficiently. */
22324 if (token->type == CPP_NAME
22325 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22327 cp_token *identifier_token;
22328 bool ambiguous_p;
22330 /* Look for the identifier. */
22331 identifier_token = cp_lexer_peek_token (parser->lexer);
22332 ambiguous_p = identifier_token->error_reported;
22333 identifier = cp_parser_identifier (parser);
22334 /* If the next token isn't an identifier, we are certainly not
22335 looking at a class-name. */
22336 if (identifier == error_mark_node)
22337 decl = error_mark_node;
22338 /* If we know this is a type-name, there's no need to look it
22339 up. */
22340 else if (typename_p)
22341 decl = identifier;
22342 else
22344 tree ambiguous_decls;
22345 /* If we already know that this lookup is ambiguous, then
22346 we've already issued an error message; there's no reason
22347 to check again. */
22348 if (ambiguous_p)
22350 cp_parser_simulate_error (parser);
22351 return error_mark_node;
22353 /* If the next token is a `::', then the name must be a type
22354 name.
22356 [basic.lookup.qual]
22358 During the lookup for a name preceding the :: scope
22359 resolution operator, object, function, and enumerator
22360 names are ignored. */
22361 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22362 tag_type = scope_type;
22363 /* Look up the name. */
22364 decl = cp_parser_lookup_name (parser, identifier,
22365 tag_type,
22366 /*is_template=*/false,
22367 /*is_namespace=*/false,
22368 check_dependency_p,
22369 &ambiguous_decls,
22370 identifier_token->location);
22371 if (ambiguous_decls)
22373 if (cp_parser_parsing_tentatively (parser))
22374 cp_parser_simulate_error (parser);
22375 return error_mark_node;
22379 else
22381 /* Try a template-id. */
22382 decl = cp_parser_template_id (parser, template_keyword_p,
22383 check_dependency_p,
22384 tag_type,
22385 is_declaration);
22386 if (decl == error_mark_node)
22387 return error_mark_node;
22390 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22392 /* If this is a typename, create a TYPENAME_TYPE. */
22393 if (typename_p && decl != error_mark_node)
22395 decl = make_typename_type (scope, decl, typename_type,
22396 /*complain=*/tf_error);
22397 if (decl != error_mark_node)
22398 decl = TYPE_NAME (decl);
22401 decl = strip_using_decl (decl);
22403 /* Check to see that it is really the name of a class. */
22404 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22405 && identifier_p (TREE_OPERAND (decl, 0))
22406 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22407 /* Situations like this:
22409 template <typename T> struct A {
22410 typename T::template X<int>::I i;
22413 are problematic. Is `T::template X<int>' a class-name? The
22414 standard does not seem to be definitive, but there is no other
22415 valid interpretation of the following `::'. Therefore, those
22416 names are considered class-names. */
22418 decl = make_typename_type (scope, decl, tag_type, tf_error);
22419 if (decl != error_mark_node)
22420 decl = TYPE_NAME (decl);
22422 else if (TREE_CODE (decl) != TYPE_DECL
22423 || TREE_TYPE (decl) == error_mark_node
22424 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22425 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22426 /* In Objective-C 2.0, a classname followed by '.' starts a
22427 dot-syntax expression, and it's not a type-name. */
22428 || (c_dialect_objc ()
22429 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22430 && objc_is_class_name (decl)))
22431 decl = error_mark_node;
22433 if (decl == error_mark_node)
22434 cp_parser_error (parser, "expected class-name");
22435 else if (identifier && !parser->scope)
22436 maybe_note_name_used_in_class (identifier, decl);
22438 return decl;
22441 /* Parse a class-specifier.
22443 class-specifier:
22444 class-head { member-specification [opt] }
22446 Returns the TREE_TYPE representing the class. */
22448 static tree
22449 cp_parser_class_specifier_1 (cp_parser* parser)
22451 tree type;
22452 tree attributes = NULL_TREE;
22453 bool nested_name_specifier_p;
22454 unsigned saved_num_template_parameter_lists;
22455 bool saved_in_function_body;
22456 unsigned char in_statement;
22457 bool in_switch_statement_p;
22458 bool saved_in_unbraced_linkage_specification_p;
22459 tree old_scope = NULL_TREE;
22460 tree scope = NULL_TREE;
22461 cp_token *closing_brace;
22463 push_deferring_access_checks (dk_no_deferred);
22465 /* Parse the class-head. */
22466 type = cp_parser_class_head (parser,
22467 &nested_name_specifier_p);
22468 /* If the class-head was a semantic disaster, skip the entire body
22469 of the class. */
22470 if (!type)
22472 cp_parser_skip_to_end_of_block_or_statement (parser);
22473 pop_deferring_access_checks ();
22474 return error_mark_node;
22477 /* Look for the `{'. */
22478 matching_braces braces;
22479 if (!braces.require_open (parser))
22481 pop_deferring_access_checks ();
22482 return error_mark_node;
22485 cp_ensure_no_omp_declare_simd (parser);
22486 cp_ensure_no_oacc_routine (parser);
22488 /* Issue an error message if type-definitions are forbidden here. */
22489 cp_parser_check_type_definition (parser);
22490 /* Remember that we are defining one more class. */
22491 ++parser->num_classes_being_defined;
22492 /* Inside the class, surrounding template-parameter-lists do not
22493 apply. */
22494 saved_num_template_parameter_lists
22495 = parser->num_template_parameter_lists;
22496 parser->num_template_parameter_lists = 0;
22497 /* We are not in a function body. */
22498 saved_in_function_body = parser->in_function_body;
22499 parser->in_function_body = false;
22500 /* Or in a loop. */
22501 in_statement = parser->in_statement;
22502 parser->in_statement = 0;
22503 /* Or in a switch. */
22504 in_switch_statement_p = parser->in_switch_statement_p;
22505 parser->in_switch_statement_p = false;
22506 /* We are not immediately inside an extern "lang" block. */
22507 saved_in_unbraced_linkage_specification_p
22508 = parser->in_unbraced_linkage_specification_p;
22509 parser->in_unbraced_linkage_specification_p = false;
22511 // Associate constraints with the type.
22512 if (flag_concepts)
22513 type = associate_classtype_constraints (type);
22515 /* Start the class. */
22516 if (nested_name_specifier_p)
22518 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22519 old_scope = push_inner_scope (scope);
22521 type = begin_class_definition (type);
22523 if (type == error_mark_node)
22524 /* If the type is erroneous, skip the entire body of the class. */
22525 cp_parser_skip_to_closing_brace (parser);
22526 else
22527 /* Parse the member-specification. */
22528 cp_parser_member_specification_opt (parser);
22530 /* Look for the trailing `}'. */
22531 closing_brace = braces.require_close (parser);
22532 /* Look for trailing attributes to apply to this class. */
22533 if (cp_parser_allow_gnu_extensions_p (parser))
22534 attributes = cp_parser_gnu_attributes_opt (parser);
22535 if (type != error_mark_node)
22536 type = finish_struct (type, attributes);
22537 if (nested_name_specifier_p)
22538 pop_inner_scope (old_scope, scope);
22540 /* We've finished a type definition. Check for the common syntax
22541 error of forgetting a semicolon after the definition. We need to
22542 be careful, as we can't just check for not-a-semicolon and be done
22543 with it; the user might have typed:
22545 class X { } c = ...;
22546 class X { } *p = ...;
22548 and so forth. Instead, enumerate all the possible tokens that
22549 might follow this production; if we don't see one of them, then
22550 complain and silently insert the semicolon. */
22552 cp_token *token = cp_lexer_peek_token (parser->lexer);
22553 bool want_semicolon = true;
22555 if (cp_next_tokens_can_be_std_attribute_p (parser))
22556 /* Don't try to parse c++11 attributes here. As per the
22557 grammar, that should be a task for
22558 cp_parser_decl_specifier_seq. */
22559 want_semicolon = false;
22561 switch (token->type)
22563 case CPP_NAME:
22564 case CPP_SEMICOLON:
22565 case CPP_MULT:
22566 case CPP_AND:
22567 case CPP_OPEN_PAREN:
22568 case CPP_CLOSE_PAREN:
22569 case CPP_COMMA:
22570 want_semicolon = false;
22571 break;
22573 /* While it's legal for type qualifiers and storage class
22574 specifiers to follow type definitions in the grammar, only
22575 compiler testsuites contain code like that. Assume that if
22576 we see such code, then what we're really seeing is a case
22577 like:
22579 class X { }
22580 const <type> var = ...;
22584 class Y { }
22585 static <type> func (...) ...
22587 i.e. the qualifier or specifier applies to the next
22588 declaration. To do so, however, we need to look ahead one
22589 more token to see if *that* token is a type specifier.
22591 This code could be improved to handle:
22593 class Z { }
22594 static const <type> var = ...; */
22595 case CPP_KEYWORD:
22596 if (keyword_is_decl_specifier (token->keyword))
22598 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22600 /* Handling user-defined types here would be nice, but very
22601 tricky. */
22602 want_semicolon
22603 = (lookahead->type == CPP_KEYWORD
22604 && keyword_begins_type_specifier (lookahead->keyword));
22606 break;
22607 default:
22608 break;
22611 /* If we don't have a type, then something is very wrong and we
22612 shouldn't try to do anything clever. Likewise for not seeing the
22613 closing brace. */
22614 if (closing_brace && TYPE_P (type) && want_semicolon)
22616 /* Locate the closing brace. */
22617 cp_token_position prev
22618 = cp_lexer_previous_token_position (parser->lexer);
22619 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22620 location_t loc = prev_token->location;
22622 /* We want to suggest insertion of a ';' immediately *after* the
22623 closing brace, so, if we can, offset the location by 1 column. */
22624 location_t next_loc = loc;
22625 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22626 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22628 rich_location richloc (line_table, next_loc);
22630 /* If we successfully offset the location, suggest the fix-it. */
22631 if (next_loc != loc)
22632 richloc.add_fixit_insert_before (next_loc, ";");
22634 if (CLASSTYPE_DECLARED_CLASS (type))
22635 error_at (&richloc,
22636 "expected %<;%> after class definition");
22637 else if (TREE_CODE (type) == RECORD_TYPE)
22638 error_at (&richloc,
22639 "expected %<;%> after struct definition");
22640 else if (TREE_CODE (type) == UNION_TYPE)
22641 error_at (&richloc,
22642 "expected %<;%> after union definition");
22643 else
22644 gcc_unreachable ();
22646 /* Unget one token and smash it to look as though we encountered
22647 a semicolon in the input stream. */
22648 cp_lexer_set_token_position (parser->lexer, prev);
22649 token = cp_lexer_peek_token (parser->lexer);
22650 token->type = CPP_SEMICOLON;
22651 token->keyword = RID_MAX;
22655 /* If this class is not itself within the scope of another class,
22656 then we need to parse the bodies of all of the queued function
22657 definitions. Note that the queued functions defined in a class
22658 are not always processed immediately following the
22659 class-specifier for that class. Consider:
22661 struct A {
22662 struct B { void f() { sizeof (A); } };
22665 If `f' were processed before the processing of `A' were
22666 completed, there would be no way to compute the size of `A'.
22667 Note that the nesting we are interested in here is lexical --
22668 not the semantic nesting given by TYPE_CONTEXT. In particular,
22669 for:
22671 struct A { struct B; };
22672 struct A::B { void f() { } };
22674 there is no need to delay the parsing of `A::B::f'. */
22675 if (--parser->num_classes_being_defined == 0)
22677 tree decl;
22678 tree class_type = NULL_TREE;
22679 tree pushed_scope = NULL_TREE;
22680 unsigned ix;
22681 cp_default_arg_entry *e;
22682 tree save_ccp, save_ccr;
22684 if (any_erroneous_template_args_p (type))
22686 /* Skip default arguments, NSDMIs, etc, in order to improve
22687 error recovery (c++/71169, c++/71832). */
22688 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22689 vec_safe_truncate (unparsed_nsdmis, 0);
22690 vec_safe_truncate (unparsed_classes, 0);
22691 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22694 /* In a first pass, parse default arguments to the functions.
22695 Then, in a second pass, parse the bodies of the functions.
22696 This two-phased approach handles cases like:
22698 struct S {
22699 void f() { g(); }
22700 void g(int i = 3);
22704 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22706 decl = e->decl;
22707 /* If there are default arguments that have not yet been processed,
22708 take care of them now. */
22709 if (class_type != e->class_type)
22711 if (pushed_scope)
22712 pop_scope (pushed_scope);
22713 class_type = e->class_type;
22714 pushed_scope = push_scope (class_type);
22716 /* Make sure that any template parameters are in scope. */
22717 maybe_begin_member_template_processing (decl);
22718 /* Parse the default argument expressions. */
22719 cp_parser_late_parsing_default_args (parser, decl);
22720 /* Remove any template parameters from the symbol table. */
22721 maybe_end_member_template_processing ();
22723 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22724 /* Now parse any NSDMIs. */
22725 save_ccp = current_class_ptr;
22726 save_ccr = current_class_ref;
22727 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22729 if (class_type != DECL_CONTEXT (decl))
22731 if (pushed_scope)
22732 pop_scope (pushed_scope);
22733 class_type = DECL_CONTEXT (decl);
22734 pushed_scope = push_scope (class_type);
22736 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22737 cp_parser_late_parsing_nsdmi (parser, decl);
22739 vec_safe_truncate (unparsed_nsdmis, 0);
22740 current_class_ptr = save_ccp;
22741 current_class_ref = save_ccr;
22742 if (pushed_scope)
22743 pop_scope (pushed_scope);
22745 /* Now do some post-NSDMI bookkeeping. */
22746 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22747 after_nsdmi_defaulted_late_checks (class_type);
22748 vec_safe_truncate (unparsed_classes, 0);
22749 after_nsdmi_defaulted_late_checks (type);
22751 /* Now parse the body of the functions. */
22752 if (flag_openmp)
22754 /* OpenMP UDRs need to be parsed before all other functions. */
22755 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22756 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22757 cp_parser_late_parsing_for_member (parser, decl);
22758 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22759 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22760 cp_parser_late_parsing_for_member (parser, decl);
22762 else
22763 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22764 cp_parser_late_parsing_for_member (parser, decl);
22765 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22767 else
22768 vec_safe_push (unparsed_classes, type);
22770 /* Put back any saved access checks. */
22771 pop_deferring_access_checks ();
22773 /* Restore saved state. */
22774 parser->in_switch_statement_p = in_switch_statement_p;
22775 parser->in_statement = in_statement;
22776 parser->in_function_body = saved_in_function_body;
22777 parser->num_template_parameter_lists
22778 = saved_num_template_parameter_lists;
22779 parser->in_unbraced_linkage_specification_p
22780 = saved_in_unbraced_linkage_specification_p;
22782 return type;
22785 static tree
22786 cp_parser_class_specifier (cp_parser* parser)
22788 tree ret;
22789 timevar_push (TV_PARSE_STRUCT);
22790 ret = cp_parser_class_specifier_1 (parser);
22791 timevar_pop (TV_PARSE_STRUCT);
22792 return ret;
22795 /* Parse a class-head.
22797 class-head:
22798 class-key identifier [opt] base-clause [opt]
22799 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22800 class-key nested-name-specifier [opt] template-id
22801 base-clause [opt]
22803 class-virt-specifier:
22804 final
22806 GNU Extensions:
22807 class-key attributes identifier [opt] base-clause [opt]
22808 class-key attributes nested-name-specifier identifier base-clause [opt]
22809 class-key attributes nested-name-specifier [opt] template-id
22810 base-clause [opt]
22812 Upon return BASES is initialized to the list of base classes (or
22813 NULL, if there are none) in the same form returned by
22814 cp_parser_base_clause.
22816 Returns the TYPE of the indicated class. Sets
22817 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22818 involving a nested-name-specifier was used, and FALSE otherwise.
22820 Returns error_mark_node if this is not a class-head.
22822 Returns NULL_TREE if the class-head is syntactically valid, but
22823 semantically invalid in a way that means we should skip the entire
22824 body of the class. */
22826 static tree
22827 cp_parser_class_head (cp_parser* parser,
22828 bool* nested_name_specifier_p)
22830 tree nested_name_specifier;
22831 enum tag_types class_key;
22832 tree id = NULL_TREE;
22833 tree type = NULL_TREE;
22834 tree attributes;
22835 tree bases;
22836 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22837 bool template_id_p = false;
22838 bool qualified_p = false;
22839 bool invalid_nested_name_p = false;
22840 bool invalid_explicit_specialization_p = false;
22841 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22842 tree pushed_scope = NULL_TREE;
22843 unsigned num_templates;
22844 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22845 /* Assume no nested-name-specifier will be present. */
22846 *nested_name_specifier_p = false;
22847 /* Assume no template parameter lists will be used in defining the
22848 type. */
22849 num_templates = 0;
22850 parser->colon_corrects_to_scope_p = false;
22852 /* Look for the class-key. */
22853 class_key = cp_parser_class_key (parser);
22854 if (class_key == none_type)
22855 return error_mark_node;
22857 location_t class_head_start_location = input_location;
22859 /* Parse the attributes. */
22860 attributes = cp_parser_attributes_opt (parser);
22862 /* If the next token is `::', that is invalid -- but sometimes
22863 people do try to write:
22865 struct ::S {};
22867 Handle this gracefully by accepting the extra qualifier, and then
22868 issuing an error about it later if this really is a
22869 class-head. If it turns out just to be an elaborated type
22870 specifier, remain silent. */
22871 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22872 qualified_p = true;
22874 push_deferring_access_checks (dk_no_check);
22876 /* Determine the name of the class. Begin by looking for an
22877 optional nested-name-specifier. */
22878 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22879 nested_name_specifier
22880 = cp_parser_nested_name_specifier_opt (parser,
22881 /*typename_keyword_p=*/false,
22882 /*check_dependency_p=*/false,
22883 /*type_p=*/true,
22884 /*is_declaration=*/false);
22885 /* If there was a nested-name-specifier, then there *must* be an
22886 identifier. */
22888 cp_token *bad_template_keyword = NULL;
22890 if (nested_name_specifier)
22892 type_start_token = cp_lexer_peek_token (parser->lexer);
22893 /* Although the grammar says `identifier', it really means
22894 `class-name' or `template-name'. You are only allowed to
22895 define a class that has already been declared with this
22896 syntax.
22898 The proposed resolution for Core Issue 180 says that wherever
22899 you see `class T::X' you should treat `X' as a type-name.
22901 It is OK to define an inaccessible class; for example:
22903 class A { class B; };
22904 class A::B {};
22906 We do not know if we will see a class-name, or a
22907 template-name. We look for a class-name first, in case the
22908 class-name is a template-id; if we looked for the
22909 template-name first we would stop after the template-name. */
22910 cp_parser_parse_tentatively (parser);
22911 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22912 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22913 type = cp_parser_class_name (parser,
22914 /*typename_keyword_p=*/false,
22915 /*template_keyword_p=*/false,
22916 class_type,
22917 /*check_dependency_p=*/false,
22918 /*class_head_p=*/true,
22919 /*is_declaration=*/false);
22920 /* If that didn't work, ignore the nested-name-specifier. */
22921 if (!cp_parser_parse_definitely (parser))
22923 invalid_nested_name_p = true;
22924 type_start_token = cp_lexer_peek_token (parser->lexer);
22925 id = cp_parser_identifier (parser);
22926 if (id == error_mark_node)
22927 id = NULL_TREE;
22929 /* If we could not find a corresponding TYPE, treat this
22930 declaration like an unqualified declaration. */
22931 if (type == error_mark_node)
22932 nested_name_specifier = NULL_TREE;
22933 /* Otherwise, count the number of templates used in TYPE and its
22934 containing scopes. */
22935 else
22937 tree scope;
22939 for (scope = TREE_TYPE (type);
22940 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22941 scope = get_containing_scope (scope))
22942 if (TYPE_P (scope)
22943 && CLASS_TYPE_P (scope)
22944 && CLASSTYPE_TEMPLATE_INFO (scope)
22945 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22946 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22947 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22948 ++num_templates;
22951 /* Otherwise, the identifier is optional. */
22952 else
22954 /* We don't know whether what comes next is a template-id,
22955 an identifier, or nothing at all. */
22956 cp_parser_parse_tentatively (parser);
22957 /* Check for a template-id. */
22958 type_start_token = cp_lexer_peek_token (parser->lexer);
22959 id = cp_parser_template_id (parser,
22960 /*template_keyword_p=*/false,
22961 /*check_dependency_p=*/true,
22962 class_key,
22963 /*is_declaration=*/true);
22964 /* If that didn't work, it could still be an identifier. */
22965 if (!cp_parser_parse_definitely (parser))
22967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22969 type_start_token = cp_lexer_peek_token (parser->lexer);
22970 id = cp_parser_identifier (parser);
22972 else
22973 id = NULL_TREE;
22975 else
22977 template_id_p = true;
22978 ++num_templates;
22982 pop_deferring_access_checks ();
22984 if (id)
22986 cp_parser_check_for_invalid_template_id (parser, id,
22987 class_key,
22988 type_start_token->location);
22990 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22992 /* If it's not a `:' or a `{' then we can't really be looking at a
22993 class-head, since a class-head only appears as part of a
22994 class-specifier. We have to detect this situation before calling
22995 xref_tag, since that has irreversible side-effects. */
22996 if (!cp_parser_next_token_starts_class_definition_p (parser))
22998 cp_parser_error (parser, "expected %<{%> or %<:%>");
22999 type = error_mark_node;
23000 goto out;
23003 /* At this point, we're going ahead with the class-specifier, even
23004 if some other problem occurs. */
23005 cp_parser_commit_to_tentative_parse (parser);
23006 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23008 cp_parser_error (parser,
23009 "cannot specify %<override%> for a class");
23010 type = error_mark_node;
23011 goto out;
23013 /* Issue the error about the overly-qualified name now. */
23014 if (qualified_p)
23016 cp_parser_error (parser,
23017 "global qualification of class name is invalid");
23018 type = error_mark_node;
23019 goto out;
23021 else if (invalid_nested_name_p)
23023 cp_parser_error (parser,
23024 "qualified name does not name a class");
23025 type = error_mark_node;
23026 goto out;
23028 else if (nested_name_specifier)
23030 tree scope;
23032 if (bad_template_keyword)
23033 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23034 keyword template shall not appear at the top level. */
23035 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23036 "keyword %<template%> not allowed in class-head-name");
23038 /* Reject typedef-names in class heads. */
23039 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23041 error_at (type_start_token->location,
23042 "invalid class name in declaration of %qD",
23043 type);
23044 type = NULL_TREE;
23045 goto done;
23048 /* Figure out in what scope the declaration is being placed. */
23049 scope = current_scope ();
23050 /* If that scope does not contain the scope in which the
23051 class was originally declared, the program is invalid. */
23052 if (scope && !is_ancestor (scope, nested_name_specifier))
23054 if (at_namespace_scope_p ())
23055 error_at (type_start_token->location,
23056 "declaration of %qD in namespace %qD which does not "
23057 "enclose %qD",
23058 type, scope, nested_name_specifier);
23059 else
23060 error_at (type_start_token->location,
23061 "declaration of %qD in %qD which does not enclose %qD",
23062 type, scope, nested_name_specifier);
23063 type = NULL_TREE;
23064 goto done;
23066 /* [dcl.meaning]
23068 A declarator-id shall not be qualified except for the
23069 definition of a ... nested class outside of its class
23070 ... [or] the definition or explicit instantiation of a
23071 class member of a namespace outside of its namespace. */
23072 if (scope == nested_name_specifier)
23074 permerror (nested_name_specifier_token_start->location,
23075 "extra qualification not allowed");
23076 nested_name_specifier = NULL_TREE;
23077 num_templates = 0;
23080 /* An explicit-specialization must be preceded by "template <>". If
23081 it is not, try to recover gracefully. */
23082 if (at_namespace_scope_p ()
23083 && parser->num_template_parameter_lists == 0
23084 && !processing_template_parmlist
23085 && template_id_p)
23087 /* Build a location of this form:
23088 struct typename <ARGS>
23089 ^~~~~~~~~~~~~~~~~~~~~~
23090 with caret==start at the start token, and
23091 finishing at the end of the type. */
23092 location_t reported_loc
23093 = make_location (class_head_start_location,
23094 class_head_start_location,
23095 get_finish (type_start_token->location));
23096 rich_location richloc (line_table, reported_loc);
23097 richloc.add_fixit_insert_before (class_head_start_location,
23098 "template <> ");
23099 error_at (&richloc,
23100 "an explicit specialization must be preceded by"
23101 " %<template <>%>");
23102 invalid_explicit_specialization_p = true;
23103 /* Take the same action that would have been taken by
23104 cp_parser_explicit_specialization. */
23105 ++parser->num_template_parameter_lists;
23106 begin_specialization ();
23108 /* There must be no "return" statements between this point and the
23109 end of this function; set "type "to the correct return value and
23110 use "goto done;" to return. */
23111 /* Make sure that the right number of template parameters were
23112 present. */
23113 if (!cp_parser_check_template_parameters (parser, num_templates,
23114 type_start_token->location,
23115 /*declarator=*/NULL))
23117 /* If something went wrong, there is no point in even trying to
23118 process the class-definition. */
23119 type = NULL_TREE;
23120 goto done;
23123 /* Look up the type. */
23124 if (template_id_p)
23126 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23127 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23128 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23130 error_at (type_start_token->location,
23131 "function template %qD redeclared as a class template", id);
23132 type = error_mark_node;
23134 else
23136 type = TREE_TYPE (id);
23137 type = maybe_process_partial_specialization (type);
23139 /* Check the scope while we still know whether or not we had a
23140 nested-name-specifier. */
23141 if (type != error_mark_node)
23142 check_unqualified_spec_or_inst (type, type_start_token->location);
23144 if (nested_name_specifier)
23145 pushed_scope = push_scope (nested_name_specifier);
23147 else if (nested_name_specifier)
23149 tree class_type;
23151 /* Given:
23153 template <typename T> struct S { struct T };
23154 template <typename T> struct S<T>::T { };
23156 we will get a TYPENAME_TYPE when processing the definition of
23157 `S::T'. We need to resolve it to the actual type before we
23158 try to define it. */
23159 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23161 class_type = resolve_typename_type (TREE_TYPE (type),
23162 /*only_current_p=*/false);
23163 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23164 type = TYPE_NAME (class_type);
23165 else
23167 cp_parser_error (parser, "could not resolve typename type");
23168 type = error_mark_node;
23172 if (maybe_process_partial_specialization (TREE_TYPE (type))
23173 == error_mark_node)
23175 type = NULL_TREE;
23176 goto done;
23179 class_type = current_class_type;
23180 /* Enter the scope indicated by the nested-name-specifier. */
23181 pushed_scope = push_scope (nested_name_specifier);
23182 /* Get the canonical version of this type. */
23183 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23184 /* Call push_template_decl if it seems like we should be defining a
23185 template either from the template headers or the type we're
23186 defining, so that we diagnose both extra and missing headers. */
23187 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23188 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23189 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23191 type = push_template_decl (type);
23192 if (type == error_mark_node)
23194 type = NULL_TREE;
23195 goto done;
23199 type = TREE_TYPE (type);
23200 *nested_name_specifier_p = true;
23202 else /* The name is not a nested name. */
23204 /* If the class was unnamed, create a dummy name. */
23205 if (!id)
23206 id = make_anon_name ();
23207 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23208 ? ts_within_enclosing_non_class
23209 : ts_current);
23210 type = xref_tag (class_key, id, tag_scope,
23211 parser->num_template_parameter_lists);
23214 /* Indicate whether this class was declared as a `class' or as a
23215 `struct'. */
23216 if (TREE_CODE (type) == RECORD_TYPE)
23217 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23218 cp_parser_check_class_key (class_key, type);
23220 /* If this type was already complete, and we see another definition,
23221 that's an error. */
23222 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23224 error_at (type_start_token->location, "redefinition of %q#T",
23225 type);
23226 inform (location_of (type), "previous definition of %q#T",
23227 type);
23228 type = NULL_TREE;
23229 goto done;
23231 else if (type == error_mark_node)
23232 type = NULL_TREE;
23234 if (type)
23236 /* Apply attributes now, before any use of the class as a template
23237 argument in its base list. */
23238 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23239 fixup_attribute_variants (type);
23242 /* We will have entered the scope containing the class; the names of
23243 base classes should be looked up in that context. For example:
23245 struct A { struct B {}; struct C; };
23246 struct A::C : B {};
23248 is valid. */
23250 /* Get the list of base-classes, if there is one. */
23251 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23253 /* PR59482: enter the class scope so that base-specifiers are looked
23254 up correctly. */
23255 if (type)
23256 pushclass (type);
23257 bases = cp_parser_base_clause (parser);
23258 /* PR59482: get out of the previously pushed class scope so that the
23259 subsequent pops pop the right thing. */
23260 if (type)
23261 popclass ();
23263 else
23264 bases = NULL_TREE;
23266 /* If we're really defining a class, process the base classes.
23267 If they're invalid, fail. */
23268 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23269 xref_basetypes (type, bases);
23271 done:
23272 /* Leave the scope given by the nested-name-specifier. We will
23273 enter the class scope itself while processing the members. */
23274 if (pushed_scope)
23275 pop_scope (pushed_scope);
23277 if (invalid_explicit_specialization_p)
23279 end_specialization ();
23280 --parser->num_template_parameter_lists;
23283 if (type)
23284 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23285 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23286 CLASSTYPE_FINAL (type) = 1;
23287 out:
23288 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23289 return type;
23292 /* Parse a class-key.
23294 class-key:
23295 class
23296 struct
23297 union
23299 Returns the kind of class-key specified, or none_type to indicate
23300 error. */
23302 static enum tag_types
23303 cp_parser_class_key (cp_parser* parser)
23305 cp_token *token;
23306 enum tag_types tag_type;
23308 /* Look for the class-key. */
23309 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23310 if (!token)
23311 return none_type;
23313 /* Check to see if the TOKEN is a class-key. */
23314 tag_type = cp_parser_token_is_class_key (token);
23315 if (!tag_type)
23316 cp_parser_error (parser, "expected class-key");
23317 return tag_type;
23320 /* Parse a type-parameter-key.
23322 type-parameter-key:
23323 class
23324 typename
23327 static void
23328 cp_parser_type_parameter_key (cp_parser* parser)
23330 /* Look for the type-parameter-key. */
23331 enum tag_types tag_type = none_type;
23332 cp_token *token = cp_lexer_peek_token (parser->lexer);
23333 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23335 cp_lexer_consume_token (parser->lexer);
23336 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23337 /* typename is not allowed in a template template parameter
23338 by the standard until C++17. */
23339 pedwarn (token->location, OPT_Wpedantic,
23340 "ISO C++ forbids typename key in template template parameter;"
23341 " use -std=c++17 or -std=gnu++17");
23343 else
23344 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23346 return;
23349 /* Parse an (optional) member-specification.
23351 member-specification:
23352 member-declaration member-specification [opt]
23353 access-specifier : member-specification [opt] */
23355 static void
23356 cp_parser_member_specification_opt (cp_parser* parser)
23358 while (true)
23360 cp_token *token;
23361 enum rid keyword;
23363 /* Peek at the next token. */
23364 token = cp_lexer_peek_token (parser->lexer);
23365 /* If it's a `}', or EOF then we've seen all the members. */
23366 if (token->type == CPP_CLOSE_BRACE
23367 || token->type == CPP_EOF
23368 || token->type == CPP_PRAGMA_EOL)
23369 break;
23371 /* See if this token is a keyword. */
23372 keyword = token->keyword;
23373 switch (keyword)
23375 case RID_PUBLIC:
23376 case RID_PROTECTED:
23377 case RID_PRIVATE:
23378 /* Consume the access-specifier. */
23379 cp_lexer_consume_token (parser->lexer);
23380 /* Remember which access-specifier is active. */
23381 current_access_specifier = token->u.value;
23382 /* Look for the `:'. */
23383 cp_parser_require (parser, CPP_COLON, RT_COLON);
23384 break;
23386 default:
23387 /* Accept #pragmas at class scope. */
23388 if (token->type == CPP_PRAGMA)
23390 cp_parser_pragma (parser, pragma_member, NULL);
23391 break;
23394 /* Otherwise, the next construction must be a
23395 member-declaration. */
23396 cp_parser_member_declaration (parser);
23401 /* Parse a member-declaration.
23403 member-declaration:
23404 decl-specifier-seq [opt] member-declarator-list [opt] ;
23405 function-definition ; [opt]
23406 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23407 using-declaration
23408 template-declaration
23409 alias-declaration
23411 member-declarator-list:
23412 member-declarator
23413 member-declarator-list , member-declarator
23415 member-declarator:
23416 declarator pure-specifier [opt]
23417 declarator constant-initializer [opt]
23418 identifier [opt] : constant-expression
23420 GNU Extensions:
23422 member-declaration:
23423 __extension__ member-declaration
23425 member-declarator:
23426 declarator attributes [opt] pure-specifier [opt]
23427 declarator attributes [opt] constant-initializer [opt]
23428 identifier [opt] attributes [opt] : constant-expression
23430 C++0x Extensions:
23432 member-declaration:
23433 static_assert-declaration */
23435 static void
23436 cp_parser_member_declaration (cp_parser* parser)
23438 cp_decl_specifier_seq decl_specifiers;
23439 tree prefix_attributes;
23440 tree decl;
23441 int declares_class_or_enum;
23442 bool friend_p;
23443 cp_token *token = NULL;
23444 cp_token *decl_spec_token_start = NULL;
23445 cp_token *initializer_token_start = NULL;
23446 int saved_pedantic;
23447 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23449 /* Check for the `__extension__' keyword. */
23450 if (cp_parser_extension_opt (parser, &saved_pedantic))
23452 /* Recurse. */
23453 cp_parser_member_declaration (parser);
23454 /* Restore the old value of the PEDANTIC flag. */
23455 pedantic = saved_pedantic;
23457 return;
23460 /* Check for a template-declaration. */
23461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23463 /* An explicit specialization here is an error condition, and we
23464 expect the specialization handler to detect and report this. */
23465 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23466 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23467 cp_parser_explicit_specialization (parser);
23468 else
23469 cp_parser_template_declaration (parser, /*member_p=*/true);
23471 return;
23473 /* Check for a template introduction. */
23474 else if (cp_parser_template_declaration_after_export (parser, true))
23475 return;
23477 /* Check for a using-declaration. */
23478 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23480 if (cxx_dialect < cxx11)
23482 /* Parse the using-declaration. */
23483 cp_parser_using_declaration (parser,
23484 /*access_declaration_p=*/false);
23485 return;
23487 else
23489 tree decl;
23490 bool alias_decl_expected;
23491 cp_parser_parse_tentatively (parser);
23492 decl = cp_parser_alias_declaration (parser);
23493 /* Note that if we actually see the '=' token after the
23494 identifier, cp_parser_alias_declaration commits the
23495 tentative parse. In that case, we really expect an
23496 alias-declaration. Otherwise, we expect a using
23497 declaration. */
23498 alias_decl_expected =
23499 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23500 cp_parser_parse_definitely (parser);
23502 if (alias_decl_expected)
23503 finish_member_declaration (decl);
23504 else
23505 cp_parser_using_declaration (parser,
23506 /*access_declaration_p=*/false);
23507 return;
23511 /* Check for @defs. */
23512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23514 tree ivar, member;
23515 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23516 ivar = ivar_chains;
23517 while (ivar)
23519 member = ivar;
23520 ivar = TREE_CHAIN (member);
23521 TREE_CHAIN (member) = NULL_TREE;
23522 finish_member_declaration (member);
23524 return;
23527 /* If the next token is `static_assert' we have a static assertion. */
23528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23530 cp_parser_static_assert (parser, /*member_p=*/true);
23531 return;
23534 parser->colon_corrects_to_scope_p = false;
23536 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23537 goto out;
23539 /* Parse the decl-specifier-seq. */
23540 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23541 cp_parser_decl_specifier_seq (parser,
23542 CP_PARSER_FLAGS_OPTIONAL,
23543 &decl_specifiers,
23544 &declares_class_or_enum);
23545 /* Check for an invalid type-name. */
23546 if (!decl_specifiers.any_type_specifiers_p
23547 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23548 goto out;
23549 /* If there is no declarator, then the decl-specifier-seq should
23550 specify a type. */
23551 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23553 /* If there was no decl-specifier-seq, and the next token is a
23554 `;', then we have something like:
23556 struct S { ; };
23558 [class.mem]
23560 Each member-declaration shall declare at least one member
23561 name of the class. */
23562 if (!decl_specifiers.any_specifiers_p)
23564 cp_token *token = cp_lexer_peek_token (parser->lexer);
23565 if (!in_system_header_at (token->location))
23567 gcc_rich_location richloc (token->location);
23568 richloc.add_fixit_remove ();
23569 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23572 else
23574 tree type;
23576 /* See if this declaration is a friend. */
23577 friend_p = cp_parser_friend_p (&decl_specifiers);
23578 /* If there were decl-specifiers, check to see if there was
23579 a class-declaration. */
23580 type = check_tag_decl (&decl_specifiers,
23581 /*explicit_type_instantiation_p=*/false);
23582 /* Nested classes have already been added to the class, but
23583 a `friend' needs to be explicitly registered. */
23584 if (friend_p)
23586 /* If the `friend' keyword was present, the friend must
23587 be introduced with a class-key. */
23588 if (!declares_class_or_enum && cxx_dialect < cxx11)
23589 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23590 "in C++03 a class-key must be used "
23591 "when declaring a friend");
23592 /* In this case:
23594 template <typename T> struct A {
23595 friend struct A<T>::B;
23598 A<T>::B will be represented by a TYPENAME_TYPE, and
23599 therefore not recognized by check_tag_decl. */
23600 if (!type)
23602 type = decl_specifiers.type;
23603 if (type && TREE_CODE (type) == TYPE_DECL)
23604 type = TREE_TYPE (type);
23606 if (!type || !TYPE_P (type))
23607 error_at (decl_spec_token_start->location,
23608 "friend declaration does not name a class or "
23609 "function");
23610 else
23611 make_friend_class (current_class_type, type,
23612 /*complain=*/true);
23614 /* If there is no TYPE, an error message will already have
23615 been issued. */
23616 else if (!type || type == error_mark_node)
23618 /* An anonymous aggregate has to be handled specially; such
23619 a declaration really declares a data member (with a
23620 particular type), as opposed to a nested class. */
23621 else if (ANON_AGGR_TYPE_P (type))
23623 /* C++11 9.5/6. */
23624 if (decl_specifiers.storage_class != sc_none)
23625 error_at (decl_spec_token_start->location,
23626 "a storage class on an anonymous aggregate "
23627 "in class scope is not allowed");
23629 /* Remove constructors and such from TYPE, now that we
23630 know it is an anonymous aggregate. */
23631 fixup_anonymous_aggr (type);
23632 /* And make the corresponding data member. */
23633 decl = build_decl (decl_spec_token_start->location,
23634 FIELD_DECL, NULL_TREE, type);
23635 /* Add it to the class. */
23636 finish_member_declaration (decl);
23638 else
23639 cp_parser_check_access_in_redeclaration
23640 (TYPE_NAME (type),
23641 decl_spec_token_start->location);
23644 else
23646 bool assume_semicolon = false;
23648 /* Clear attributes from the decl_specifiers but keep them
23649 around as prefix attributes that apply them to the entity
23650 being declared. */
23651 prefix_attributes = decl_specifiers.attributes;
23652 decl_specifiers.attributes = NULL_TREE;
23654 /* See if these declarations will be friends. */
23655 friend_p = cp_parser_friend_p (&decl_specifiers);
23657 /* Keep going until we hit the `;' at the end of the
23658 declaration. */
23659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23661 tree attributes = NULL_TREE;
23662 tree first_attribute;
23663 tree initializer;
23664 bool named_bitfld = false;
23666 /* Peek at the next token. */
23667 token = cp_lexer_peek_token (parser->lexer);
23669 /* The following code wants to know early if it is a bit-field
23670 or some other declaration. Attributes can appear before
23671 the `:' token. Skip over them without consuming any tokens
23672 to peek if they are followed by `:'. */
23673 if (cp_next_tokens_can_be_attribute_p (parser)
23674 || (token->type == CPP_NAME
23675 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23676 && (named_bitfld = true)))
23678 size_t n
23679 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23680 token = cp_lexer_peek_nth_token (parser->lexer, n);
23683 /* Check for a bitfield declaration. */
23684 if (token->type == CPP_COLON
23685 || (token->type == CPP_NAME
23686 && token == cp_lexer_peek_token (parser->lexer)
23687 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23688 && (named_bitfld = true)))
23690 tree identifier;
23691 tree width;
23692 tree late_attributes = NULL_TREE;
23694 if (named_bitfld)
23695 identifier = cp_parser_identifier (parser);
23696 else
23697 identifier = NULL_TREE;
23699 /* Look for attributes that apply to the bitfield. */
23700 attributes = cp_parser_attributes_opt (parser);
23702 /* Consume the `:' token. */
23703 cp_lexer_consume_token (parser->lexer);
23705 /* Get the width of the bitfield. */
23706 width = cp_parser_constant_expression (parser, false, NULL,
23707 cxx_dialect >= cxx11);
23709 /* In C++2A and as extension for C++11 and above we allow
23710 default member initializers for bit-fields. */
23711 initializer = NULL_TREE;
23712 if (cxx_dialect >= cxx11
23713 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23714 || cp_lexer_next_token_is (parser->lexer,
23715 CPP_OPEN_BRACE)))
23717 location_t loc
23718 = cp_lexer_peek_token (parser->lexer)->location;
23719 if (cxx_dialect < cxx2a
23720 && !in_system_header_at (loc)
23721 && identifier != NULL_TREE)
23722 pedwarn (loc, 0,
23723 "default member initializers for bit-fields "
23724 "only available with -std=c++2a or "
23725 "-std=gnu++2a");
23727 initializer = cp_parser_save_nsdmi (parser);
23728 if (identifier == NULL_TREE)
23730 error_at (loc, "default member initializer for "
23731 "unnamed bit-field");
23732 initializer = NULL_TREE;
23735 else
23737 /* Look for attributes that apply to the bitfield after
23738 the `:' token and width. This is where GCC used to
23739 parse attributes in the past, pedwarn if there is
23740 a std attribute. */
23741 if (cp_next_tokens_can_be_std_attribute_p (parser))
23742 pedwarn (input_location, OPT_Wpedantic,
23743 "ISO C++ allows bit-field attributes only "
23744 "before the %<:%> token");
23746 late_attributes = cp_parser_attributes_opt (parser);
23749 attributes = attr_chainon (attributes, late_attributes);
23751 /* Remember which attributes are prefix attributes and
23752 which are not. */
23753 first_attribute = attributes;
23754 /* Combine the attributes. */
23755 attributes = attr_chainon (prefix_attributes, attributes);
23757 /* Create the bitfield declaration. */
23758 decl = grokbitfield (identifier
23759 ? make_id_declarator (NULL_TREE,
23760 identifier,
23761 sfk_none)
23762 : NULL,
23763 &decl_specifiers,
23764 width, initializer,
23765 attributes);
23767 else
23769 cp_declarator *declarator;
23770 tree asm_specification;
23771 int ctor_dtor_or_conv_p;
23773 /* Parse the declarator. */
23774 declarator
23775 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23776 &ctor_dtor_or_conv_p,
23777 /*parenthesized_p=*/NULL,
23778 /*member_p=*/true,
23779 friend_p);
23781 /* If something went wrong parsing the declarator, make sure
23782 that we at least consume some tokens. */
23783 if (declarator == cp_error_declarator)
23785 /* Skip to the end of the statement. */
23786 cp_parser_skip_to_end_of_statement (parser);
23787 /* If the next token is not a semicolon, that is
23788 probably because we just skipped over the body of
23789 a function. So, we consume a semicolon if
23790 present, but do not issue an error message if it
23791 is not present. */
23792 if (cp_lexer_next_token_is (parser->lexer,
23793 CPP_SEMICOLON))
23794 cp_lexer_consume_token (parser->lexer);
23795 goto out;
23798 if (declares_class_or_enum & 2)
23799 cp_parser_check_for_definition_in_return_type
23800 (declarator, decl_specifiers.type,
23801 decl_specifiers.locations[ds_type_spec]);
23803 /* Look for an asm-specification. */
23804 asm_specification = cp_parser_asm_specification_opt (parser);
23805 /* Look for attributes that apply to the declaration. */
23806 attributes = cp_parser_attributes_opt (parser);
23807 /* Remember which attributes are prefix attributes and
23808 which are not. */
23809 first_attribute = attributes;
23810 /* Combine the attributes. */
23811 attributes = attr_chainon (prefix_attributes, attributes);
23813 /* If it's an `=', then we have a constant-initializer or a
23814 pure-specifier. It is not correct to parse the
23815 initializer before registering the member declaration
23816 since the member declaration should be in scope while
23817 its initializer is processed. However, the rest of the
23818 front end does not yet provide an interface that allows
23819 us to handle this correctly. */
23820 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23822 /* In [class.mem]:
23824 A pure-specifier shall be used only in the declaration of
23825 a virtual function.
23827 A member-declarator can contain a constant-initializer
23828 only if it declares a static member of integral or
23829 enumeration type.
23831 Therefore, if the DECLARATOR is for a function, we look
23832 for a pure-specifier; otherwise, we look for a
23833 constant-initializer. When we call `grokfield', it will
23834 perform more stringent semantics checks. */
23835 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23836 if (function_declarator_p (declarator)
23837 || (decl_specifiers.type
23838 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23839 && declarator->kind == cdk_id
23840 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23841 == FUNCTION_TYPE)))
23842 initializer = cp_parser_pure_specifier (parser);
23843 else if (decl_specifiers.storage_class != sc_static)
23844 initializer = cp_parser_save_nsdmi (parser);
23845 else if (cxx_dialect >= cxx11)
23847 bool nonconst;
23848 /* Don't require a constant rvalue in C++11, since we
23849 might want a reference constant. We'll enforce
23850 constancy later. */
23851 cp_lexer_consume_token (parser->lexer);
23852 /* Parse the initializer. */
23853 initializer = cp_parser_initializer_clause (parser,
23854 &nonconst);
23856 else
23857 /* Parse the initializer. */
23858 initializer = cp_parser_constant_initializer (parser);
23860 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23861 && !function_declarator_p (declarator))
23863 bool x;
23864 if (decl_specifiers.storage_class != sc_static)
23865 initializer = cp_parser_save_nsdmi (parser);
23866 else
23867 initializer = cp_parser_initializer (parser, &x, &x);
23869 /* Otherwise, there is no initializer. */
23870 else
23871 initializer = NULL_TREE;
23873 /* See if we are probably looking at a function
23874 definition. We are certainly not looking at a
23875 member-declarator. Calling `grokfield' has
23876 side-effects, so we must not do it unless we are sure
23877 that we are looking at a member-declarator. */
23878 if (cp_parser_token_starts_function_definition_p
23879 (cp_lexer_peek_token (parser->lexer)))
23881 /* The grammar does not allow a pure-specifier to be
23882 used when a member function is defined. (It is
23883 possible that this fact is an oversight in the
23884 standard, since a pure function may be defined
23885 outside of the class-specifier. */
23886 if (initializer && initializer_token_start)
23887 error_at (initializer_token_start->location,
23888 "pure-specifier on function-definition");
23889 decl = cp_parser_save_member_function_body (parser,
23890 &decl_specifiers,
23891 declarator,
23892 attributes);
23893 if (parser->fully_implicit_function_template_p)
23894 decl = finish_fully_implicit_template (parser, decl);
23895 /* If the member was not a friend, declare it here. */
23896 if (!friend_p)
23897 finish_member_declaration (decl);
23898 /* Peek at the next token. */
23899 token = cp_lexer_peek_token (parser->lexer);
23900 /* If the next token is a semicolon, consume it. */
23901 if (token->type == CPP_SEMICOLON)
23903 location_t semicolon_loc
23904 = cp_lexer_consume_token (parser->lexer)->location;
23905 gcc_rich_location richloc (semicolon_loc);
23906 richloc.add_fixit_remove ();
23907 warning_at (&richloc, OPT_Wextra_semi,
23908 "extra %<;%> after in-class "
23909 "function definition");
23911 goto out;
23913 else
23914 if (declarator->kind == cdk_function)
23915 declarator->id_loc = token->location;
23916 /* Create the declaration. */
23917 decl = grokfield (declarator, &decl_specifiers,
23918 initializer, /*init_const_expr_p=*/true,
23919 asm_specification, attributes);
23920 if (parser->fully_implicit_function_template_p)
23922 if (friend_p)
23923 finish_fully_implicit_template (parser, 0);
23924 else
23925 decl = finish_fully_implicit_template (parser, decl);
23929 cp_finalize_omp_declare_simd (parser, decl);
23930 cp_finalize_oacc_routine (parser, decl, false);
23932 /* Reset PREFIX_ATTRIBUTES. */
23933 if (attributes != error_mark_node)
23935 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23936 attributes = TREE_CHAIN (attributes);
23937 if (attributes)
23938 TREE_CHAIN (attributes) = NULL_TREE;
23941 /* If there is any qualification still in effect, clear it
23942 now; we will be starting fresh with the next declarator. */
23943 parser->scope = NULL_TREE;
23944 parser->qualifying_scope = NULL_TREE;
23945 parser->object_scope = NULL_TREE;
23946 /* If it's a `,', then there are more declarators. */
23947 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23949 cp_lexer_consume_token (parser->lexer);
23950 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23952 cp_token *token = cp_lexer_previous_token (parser->lexer);
23953 gcc_rich_location richloc (token->location);
23954 richloc.add_fixit_remove ();
23955 error_at (&richloc, "stray %<,%> at end of "
23956 "member declaration");
23959 /* If the next token isn't a `;', then we have a parse error. */
23960 else if (cp_lexer_next_token_is_not (parser->lexer,
23961 CPP_SEMICOLON))
23963 /* The next token might be a ways away from where the
23964 actual semicolon is missing. Find the previous token
23965 and use that for our error position. */
23966 cp_token *token = cp_lexer_previous_token (parser->lexer);
23967 gcc_rich_location richloc (token->location);
23968 richloc.add_fixit_insert_after (";");
23969 error_at (&richloc, "expected %<;%> at end of "
23970 "member declaration");
23972 /* Assume that the user meant to provide a semicolon. If
23973 we were to cp_parser_skip_to_end_of_statement, we might
23974 skip to a semicolon inside a member function definition
23975 and issue nonsensical error messages. */
23976 assume_semicolon = true;
23979 if (decl)
23981 /* Add DECL to the list of members. */
23982 if (!friend_p
23983 /* Explicitly include, eg, NSDMIs, for better error
23984 recovery (c++/58650). */
23985 || !DECL_DECLARES_FUNCTION_P (decl))
23986 finish_member_declaration (decl);
23988 if (TREE_CODE (decl) == FUNCTION_DECL)
23989 cp_parser_save_default_args (parser, decl);
23990 else if (TREE_CODE (decl) == FIELD_DECL
23991 && DECL_INITIAL (decl))
23992 /* Add DECL to the queue of NSDMI to be parsed later. */
23993 vec_safe_push (unparsed_nsdmis, decl);
23996 if (assume_semicolon)
23997 goto out;
24001 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24002 out:
24003 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24006 /* Parse a pure-specifier.
24008 pure-specifier:
24011 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24012 Otherwise, ERROR_MARK_NODE is returned. */
24014 static tree
24015 cp_parser_pure_specifier (cp_parser* parser)
24017 cp_token *token;
24019 /* Look for the `=' token. */
24020 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24021 return error_mark_node;
24022 /* Look for the `0' token. */
24023 token = cp_lexer_peek_token (parser->lexer);
24025 if (token->type == CPP_EOF
24026 || token->type == CPP_PRAGMA_EOL)
24027 return error_mark_node;
24029 cp_lexer_consume_token (parser->lexer);
24031 /* Accept = default or = delete in c++0x mode. */
24032 if (token->keyword == RID_DEFAULT
24033 || token->keyword == RID_DELETE)
24035 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24036 return token->u.value;
24039 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24040 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24042 cp_parser_error (parser,
24043 "invalid pure specifier (only %<= 0%> is allowed)");
24044 cp_parser_skip_to_end_of_statement (parser);
24045 return error_mark_node;
24047 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24049 error_at (token->location, "templates may not be %<virtual%>");
24050 return error_mark_node;
24053 return integer_zero_node;
24056 /* Parse a constant-initializer.
24058 constant-initializer:
24059 = constant-expression
24061 Returns a representation of the constant-expression. */
24063 static tree
24064 cp_parser_constant_initializer (cp_parser* parser)
24066 /* Look for the `=' token. */
24067 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24068 return error_mark_node;
24070 /* It is invalid to write:
24072 struct S { static const int i = { 7 }; };
24075 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24077 cp_parser_error (parser,
24078 "a brace-enclosed initializer is not allowed here");
24079 /* Consume the opening brace. */
24080 matching_braces braces;
24081 braces.consume_open (parser);
24082 /* Skip the initializer. */
24083 cp_parser_skip_to_closing_brace (parser);
24084 /* Look for the trailing `}'. */
24085 braces.require_close (parser);
24087 return error_mark_node;
24090 return cp_parser_constant_expression (parser);
24093 /* Derived classes [gram.class.derived] */
24095 /* Parse a base-clause.
24097 base-clause:
24098 : base-specifier-list
24100 base-specifier-list:
24101 base-specifier ... [opt]
24102 base-specifier-list , base-specifier ... [opt]
24104 Returns a TREE_LIST representing the base-classes, in the order in
24105 which they were declared. The representation of each node is as
24106 described by cp_parser_base_specifier.
24108 In the case that no bases are specified, this function will return
24109 NULL_TREE, not ERROR_MARK_NODE. */
24111 static tree
24112 cp_parser_base_clause (cp_parser* parser)
24114 tree bases = NULL_TREE;
24116 /* Look for the `:' that begins the list. */
24117 cp_parser_require (parser, CPP_COLON, RT_COLON);
24119 /* Scan the base-specifier-list. */
24120 while (true)
24122 cp_token *token;
24123 tree base;
24124 bool pack_expansion_p = false;
24126 /* Look for the base-specifier. */
24127 base = cp_parser_base_specifier (parser);
24128 /* Look for the (optional) ellipsis. */
24129 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24131 /* Consume the `...'. */
24132 cp_lexer_consume_token (parser->lexer);
24134 pack_expansion_p = true;
24137 /* Add BASE to the front of the list. */
24138 if (base && base != error_mark_node)
24140 if (pack_expansion_p)
24141 /* Make this a pack expansion type. */
24142 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24144 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24146 TREE_CHAIN (base) = bases;
24147 bases = base;
24150 /* Peek at the next token. */
24151 token = cp_lexer_peek_token (parser->lexer);
24152 /* If it's not a comma, then the list is complete. */
24153 if (token->type != CPP_COMMA)
24154 break;
24155 /* Consume the `,'. */
24156 cp_lexer_consume_token (parser->lexer);
24159 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24160 base class had a qualified name. However, the next name that
24161 appears is certainly not qualified. */
24162 parser->scope = NULL_TREE;
24163 parser->qualifying_scope = NULL_TREE;
24164 parser->object_scope = NULL_TREE;
24166 return nreverse (bases);
24169 /* Parse a base-specifier.
24171 base-specifier:
24172 :: [opt] nested-name-specifier [opt] class-name
24173 virtual access-specifier [opt] :: [opt] nested-name-specifier
24174 [opt] class-name
24175 access-specifier virtual [opt] :: [opt] nested-name-specifier
24176 [opt] class-name
24178 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24179 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24180 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24181 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24183 static tree
24184 cp_parser_base_specifier (cp_parser* parser)
24186 cp_token *token;
24187 bool done = false;
24188 bool virtual_p = false;
24189 bool duplicate_virtual_error_issued_p = false;
24190 bool duplicate_access_error_issued_p = false;
24191 bool class_scope_p, template_p;
24192 tree access = access_default_node;
24193 tree type;
24195 /* Process the optional `virtual' and `access-specifier'. */
24196 while (!done)
24198 /* Peek at the next token. */
24199 token = cp_lexer_peek_token (parser->lexer);
24200 /* Process `virtual'. */
24201 switch (token->keyword)
24203 case RID_VIRTUAL:
24204 /* If `virtual' appears more than once, issue an error. */
24205 if (virtual_p && !duplicate_virtual_error_issued_p)
24207 cp_parser_error (parser,
24208 "%<virtual%> specified more than once in base-specifier");
24209 duplicate_virtual_error_issued_p = true;
24212 virtual_p = true;
24214 /* Consume the `virtual' token. */
24215 cp_lexer_consume_token (parser->lexer);
24217 break;
24219 case RID_PUBLIC:
24220 case RID_PROTECTED:
24221 case RID_PRIVATE:
24222 /* If more than one access specifier appears, issue an
24223 error. */
24224 if (access != access_default_node
24225 && !duplicate_access_error_issued_p)
24227 cp_parser_error (parser,
24228 "more than one access specifier in base-specifier");
24229 duplicate_access_error_issued_p = true;
24232 access = ridpointers[(int) token->keyword];
24234 /* Consume the access-specifier. */
24235 cp_lexer_consume_token (parser->lexer);
24237 break;
24239 default:
24240 done = true;
24241 break;
24244 /* It is not uncommon to see programs mechanically, erroneously, use
24245 the 'typename' keyword to denote (dependent) qualified types
24246 as base classes. */
24247 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24249 token = cp_lexer_peek_token (parser->lexer);
24250 if (!processing_template_decl)
24251 error_at (token->location,
24252 "keyword %<typename%> not allowed outside of templates");
24253 else
24254 error_at (token->location,
24255 "keyword %<typename%> not allowed in this context "
24256 "(the base class is implicitly a type)");
24257 cp_lexer_consume_token (parser->lexer);
24260 /* Look for the optional `::' operator. */
24261 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24262 /* Look for the nested-name-specifier. The simplest way to
24263 implement:
24265 [temp.res]
24267 The keyword `typename' is not permitted in a base-specifier or
24268 mem-initializer; in these contexts a qualified name that
24269 depends on a template-parameter is implicitly assumed to be a
24270 type name.
24272 is to pretend that we have seen the `typename' keyword at this
24273 point. */
24274 cp_parser_nested_name_specifier_opt (parser,
24275 /*typename_keyword_p=*/true,
24276 /*check_dependency_p=*/true,
24277 /*type_p=*/true,
24278 /*is_declaration=*/true);
24279 /* If the base class is given by a qualified name, assume that names
24280 we see are type names or templates, as appropriate. */
24281 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24282 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24284 if (!parser->scope
24285 && cp_lexer_next_token_is_decltype (parser->lexer))
24286 /* DR 950 allows decltype as a base-specifier. */
24287 type = cp_parser_decltype (parser);
24288 else
24290 /* Otherwise, look for the class-name. */
24291 type = cp_parser_class_name (parser,
24292 class_scope_p,
24293 template_p,
24294 typename_type,
24295 /*check_dependency_p=*/true,
24296 /*class_head_p=*/false,
24297 /*is_declaration=*/true);
24298 type = TREE_TYPE (type);
24301 if (type == error_mark_node)
24302 return error_mark_node;
24304 return finish_base_specifier (type, access, virtual_p);
24307 /* Exception handling [gram.exception] */
24309 /* Parse an (optional) noexcept-specification.
24311 noexcept-specification:
24312 noexcept ( constant-expression ) [opt]
24314 If no noexcept-specification is present, returns NULL_TREE.
24315 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24316 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24317 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24318 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24319 in which case a boolean condition is returned instead. */
24321 static tree
24322 cp_parser_noexcept_specification_opt (cp_parser* parser,
24323 bool require_constexpr,
24324 bool* consumed_expr,
24325 bool return_cond)
24327 cp_token *token;
24328 const char *saved_message;
24330 /* Peek at the next token. */
24331 token = cp_lexer_peek_token (parser->lexer);
24333 /* Is it a noexcept-specification? */
24334 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24336 tree expr;
24337 cp_lexer_consume_token (parser->lexer);
24339 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24341 matching_parens parens;
24342 parens.consume_open (parser);
24344 if (require_constexpr)
24346 /* Types may not be defined in an exception-specification. */
24347 saved_message = parser->type_definition_forbidden_message;
24348 parser->type_definition_forbidden_message
24349 = G_("types may not be defined in an exception-specification");
24351 expr = cp_parser_constant_expression (parser);
24353 /* Restore the saved message. */
24354 parser->type_definition_forbidden_message = saved_message;
24356 else
24358 expr = cp_parser_expression (parser);
24359 *consumed_expr = true;
24362 parens.require_close (parser);
24364 else
24366 expr = boolean_true_node;
24367 if (!require_constexpr)
24368 *consumed_expr = false;
24371 /* We cannot build a noexcept-spec right away because this will check
24372 that expr is a constexpr. */
24373 if (!return_cond)
24374 return build_noexcept_spec (expr, tf_warning_or_error);
24375 else
24376 return expr;
24378 else
24379 return NULL_TREE;
24382 /* Parse an (optional) exception-specification.
24384 exception-specification:
24385 throw ( type-id-list [opt] )
24387 Returns a TREE_LIST representing the exception-specification. The
24388 TREE_VALUE of each node is a type. */
24390 static tree
24391 cp_parser_exception_specification_opt (cp_parser* parser)
24393 cp_token *token;
24394 tree type_id_list;
24395 const char *saved_message;
24397 /* Peek at the next token. */
24398 token = cp_lexer_peek_token (parser->lexer);
24400 /* Is it a noexcept-specification? */
24401 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24402 false);
24403 if (type_id_list != NULL_TREE)
24404 return type_id_list;
24406 /* If it's not `throw', then there's no exception-specification. */
24407 if (!cp_parser_is_keyword (token, RID_THROW))
24408 return NULL_TREE;
24410 location_t loc = token->location;
24412 /* Consume the `throw'. */
24413 cp_lexer_consume_token (parser->lexer);
24415 /* Look for the `('. */
24416 matching_parens parens;
24417 parens.require_open (parser);
24419 /* Peek at the next token. */
24420 token = cp_lexer_peek_token (parser->lexer);
24421 /* If it's not a `)', then there is a type-id-list. */
24422 if (token->type != CPP_CLOSE_PAREN)
24424 /* Types may not be defined in an exception-specification. */
24425 saved_message = parser->type_definition_forbidden_message;
24426 parser->type_definition_forbidden_message
24427 = G_("types may not be defined in an exception-specification");
24428 /* Parse the type-id-list. */
24429 type_id_list = cp_parser_type_id_list (parser);
24430 /* Restore the saved message. */
24431 parser->type_definition_forbidden_message = saved_message;
24433 if (cxx_dialect >= cxx17)
24435 error_at (loc, "ISO C++17 does not allow dynamic exception "
24436 "specifications");
24437 type_id_list = NULL_TREE;
24439 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24440 warning_at (loc, OPT_Wdeprecated,
24441 "dynamic exception specifications are deprecated in "
24442 "C++11");
24444 /* In C++17, throw() is equivalent to noexcept (true). throw()
24445 is deprecated in C++11 and above as well, but is still widely used,
24446 so don't warn about it yet. */
24447 else if (cxx_dialect >= cxx17)
24448 type_id_list = noexcept_true_spec;
24449 else
24450 type_id_list = empty_except_spec;
24452 /* Look for the `)'. */
24453 parens.require_close (parser);
24455 return type_id_list;
24458 /* Parse an (optional) type-id-list.
24460 type-id-list:
24461 type-id ... [opt]
24462 type-id-list , type-id ... [opt]
24464 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24465 in the order that the types were presented. */
24467 static tree
24468 cp_parser_type_id_list (cp_parser* parser)
24470 tree types = NULL_TREE;
24472 while (true)
24474 cp_token *token;
24475 tree type;
24477 token = cp_lexer_peek_token (parser->lexer);
24479 /* Get the next type-id. */
24480 type = cp_parser_type_id (parser);
24481 /* Check for invalid 'auto'. */
24482 if (flag_concepts && type_uses_auto (type))
24484 error_at (token->location,
24485 "invalid use of %<auto%> in exception-specification");
24486 type = error_mark_node;
24488 /* Parse the optional ellipsis. */
24489 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24491 /* Consume the `...'. */
24492 cp_lexer_consume_token (parser->lexer);
24494 /* Turn the type into a pack expansion expression. */
24495 type = make_pack_expansion (type);
24497 /* Add it to the list. */
24498 types = add_exception_specifier (types, type, /*complain=*/1);
24499 /* Peek at the next token. */
24500 token = cp_lexer_peek_token (parser->lexer);
24501 /* If it is not a `,', we are done. */
24502 if (token->type != CPP_COMMA)
24503 break;
24504 /* Consume the `,'. */
24505 cp_lexer_consume_token (parser->lexer);
24508 return nreverse (types);
24511 /* Parse a try-block.
24513 try-block:
24514 try compound-statement handler-seq */
24516 static tree
24517 cp_parser_try_block (cp_parser* parser)
24519 tree try_block;
24521 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24522 if (parser->in_function_body
24523 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24524 error ("%<try%> in %<constexpr%> function");
24526 try_block = begin_try_block ();
24527 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24528 finish_try_block (try_block);
24529 cp_parser_handler_seq (parser);
24530 finish_handler_sequence (try_block);
24532 return try_block;
24535 /* Parse a function-try-block.
24537 function-try-block:
24538 try ctor-initializer [opt] function-body handler-seq */
24540 static void
24541 cp_parser_function_try_block (cp_parser* parser)
24543 tree compound_stmt;
24544 tree try_block;
24546 /* Look for the `try' keyword. */
24547 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24548 return;
24549 /* Let the rest of the front end know where we are. */
24550 try_block = begin_function_try_block (&compound_stmt);
24551 /* Parse the function-body. */
24552 cp_parser_ctor_initializer_opt_and_function_body
24553 (parser, /*in_function_try_block=*/true);
24554 /* We're done with the `try' part. */
24555 finish_function_try_block (try_block);
24556 /* Parse the handlers. */
24557 cp_parser_handler_seq (parser);
24558 /* We're done with the handlers. */
24559 finish_function_handler_sequence (try_block, compound_stmt);
24562 /* Parse a handler-seq.
24564 handler-seq:
24565 handler handler-seq [opt] */
24567 static void
24568 cp_parser_handler_seq (cp_parser* parser)
24570 while (true)
24572 cp_token *token;
24574 /* Parse the handler. */
24575 cp_parser_handler (parser);
24576 /* Peek at the next token. */
24577 token = cp_lexer_peek_token (parser->lexer);
24578 /* If it's not `catch' then there are no more handlers. */
24579 if (!cp_parser_is_keyword (token, RID_CATCH))
24580 break;
24584 /* Parse a handler.
24586 handler:
24587 catch ( exception-declaration ) compound-statement */
24589 static void
24590 cp_parser_handler (cp_parser* parser)
24592 tree handler;
24593 tree declaration;
24595 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24596 handler = begin_handler ();
24597 matching_parens parens;
24598 parens.require_open (parser);
24599 declaration = cp_parser_exception_declaration (parser);
24600 finish_handler_parms (declaration, handler);
24601 parens.require_close (parser);
24602 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24603 finish_handler (handler);
24606 /* Parse an exception-declaration.
24608 exception-declaration:
24609 type-specifier-seq declarator
24610 type-specifier-seq abstract-declarator
24611 type-specifier-seq
24614 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24615 ellipsis variant is used. */
24617 static tree
24618 cp_parser_exception_declaration (cp_parser* parser)
24620 cp_decl_specifier_seq type_specifiers;
24621 cp_declarator *declarator;
24622 const char *saved_message;
24624 /* If it's an ellipsis, it's easy to handle. */
24625 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24627 /* Consume the `...' token. */
24628 cp_lexer_consume_token (parser->lexer);
24629 return NULL_TREE;
24632 /* Types may not be defined in exception-declarations. */
24633 saved_message = parser->type_definition_forbidden_message;
24634 parser->type_definition_forbidden_message
24635 = G_("types may not be defined in exception-declarations");
24637 /* Parse the type-specifier-seq. */
24638 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24639 /*is_trailing_return=*/false,
24640 &type_specifiers);
24641 /* If it's a `)', then there is no declarator. */
24642 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24643 declarator = NULL;
24644 else
24645 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24646 /*ctor_dtor_or_conv_p=*/NULL,
24647 /*parenthesized_p=*/NULL,
24648 /*member_p=*/false,
24649 /*friend_p=*/false);
24651 /* Restore the saved message. */
24652 parser->type_definition_forbidden_message = saved_message;
24654 if (!type_specifiers.any_specifiers_p)
24655 return error_mark_node;
24657 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24660 /* Parse a throw-expression.
24662 throw-expression:
24663 throw assignment-expression [opt]
24665 Returns a THROW_EXPR representing the throw-expression. */
24667 static tree
24668 cp_parser_throw_expression (cp_parser* parser)
24670 tree expression;
24671 cp_token* token;
24673 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24674 token = cp_lexer_peek_token (parser->lexer);
24675 /* Figure out whether or not there is an assignment-expression
24676 following the "throw" keyword. */
24677 if (token->type == CPP_COMMA
24678 || token->type == CPP_SEMICOLON
24679 || token->type == CPP_CLOSE_PAREN
24680 || token->type == CPP_CLOSE_SQUARE
24681 || token->type == CPP_CLOSE_BRACE
24682 || token->type == CPP_COLON)
24683 expression = NULL_TREE;
24684 else
24685 expression = cp_parser_assignment_expression (parser);
24687 return build_throw (expression);
24690 /* GNU Extensions */
24692 /* Parse an (optional) asm-specification.
24694 asm-specification:
24695 asm ( string-literal )
24697 If the asm-specification is present, returns a STRING_CST
24698 corresponding to the string-literal. Otherwise, returns
24699 NULL_TREE. */
24701 static tree
24702 cp_parser_asm_specification_opt (cp_parser* parser)
24704 cp_token *token;
24705 tree asm_specification;
24707 /* Peek at the next token. */
24708 token = cp_lexer_peek_token (parser->lexer);
24709 /* If the next token isn't the `asm' keyword, then there's no
24710 asm-specification. */
24711 if (!cp_parser_is_keyword (token, RID_ASM))
24712 return NULL_TREE;
24714 /* Consume the `asm' token. */
24715 cp_lexer_consume_token (parser->lexer);
24716 /* Look for the `('. */
24717 matching_parens parens;
24718 parens.require_open (parser);
24720 /* Look for the string-literal. */
24721 asm_specification = cp_parser_string_literal (parser, false, false);
24723 /* Look for the `)'. */
24724 parens.require_close (parser);
24726 return asm_specification;
24729 /* Parse an asm-operand-list.
24731 asm-operand-list:
24732 asm-operand
24733 asm-operand-list , asm-operand
24735 asm-operand:
24736 string-literal ( expression )
24737 [ string-literal ] string-literal ( expression )
24739 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24740 each node is the expression. The TREE_PURPOSE is itself a
24741 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24742 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24743 is a STRING_CST for the string literal before the parenthesis. Returns
24744 ERROR_MARK_NODE if any of the operands are invalid. */
24746 static tree
24747 cp_parser_asm_operand_list (cp_parser* parser)
24749 tree asm_operands = NULL_TREE;
24750 bool invalid_operands = false;
24752 while (true)
24754 tree string_literal;
24755 tree expression;
24756 tree name;
24758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24760 /* Consume the `[' token. */
24761 cp_lexer_consume_token (parser->lexer);
24762 /* Read the operand name. */
24763 name = cp_parser_identifier (parser);
24764 if (name != error_mark_node)
24765 name = build_string (IDENTIFIER_LENGTH (name),
24766 IDENTIFIER_POINTER (name));
24767 /* Look for the closing `]'. */
24768 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24770 else
24771 name = NULL_TREE;
24772 /* Look for the string-literal. */
24773 string_literal = cp_parser_string_literal (parser, false, false);
24775 /* Look for the `('. */
24776 matching_parens parens;
24777 parens.require_open (parser);
24778 /* Parse the expression. */
24779 expression = cp_parser_expression (parser);
24780 /* Look for the `)'. */
24781 parens.require_close (parser);
24783 if (name == error_mark_node
24784 || string_literal == error_mark_node
24785 || expression == error_mark_node)
24786 invalid_operands = true;
24788 /* Add this operand to the list. */
24789 asm_operands = tree_cons (build_tree_list (name, string_literal),
24790 expression,
24791 asm_operands);
24792 /* If the next token is not a `,', there are no more
24793 operands. */
24794 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24795 break;
24796 /* Consume the `,'. */
24797 cp_lexer_consume_token (parser->lexer);
24800 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24803 /* Parse an asm-clobber-list.
24805 asm-clobber-list:
24806 string-literal
24807 asm-clobber-list , string-literal
24809 Returns a TREE_LIST, indicating the clobbers in the order that they
24810 appeared. The TREE_VALUE of each node is a STRING_CST. */
24812 static tree
24813 cp_parser_asm_clobber_list (cp_parser* parser)
24815 tree clobbers = NULL_TREE;
24817 while (true)
24819 tree string_literal;
24821 /* Look for the string literal. */
24822 string_literal = cp_parser_string_literal (parser, false, false);
24823 /* Add it to the list. */
24824 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24825 /* If the next token is not a `,', then the list is
24826 complete. */
24827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24828 break;
24829 /* Consume the `,' token. */
24830 cp_lexer_consume_token (parser->lexer);
24833 return clobbers;
24836 /* Parse an asm-label-list.
24838 asm-label-list:
24839 identifier
24840 asm-label-list , identifier
24842 Returns a TREE_LIST, indicating the labels in the order that they
24843 appeared. The TREE_VALUE of each node is a label. */
24845 static tree
24846 cp_parser_asm_label_list (cp_parser* parser)
24848 tree labels = NULL_TREE;
24850 while (true)
24852 tree identifier, label, name;
24854 /* Look for the identifier. */
24855 identifier = cp_parser_identifier (parser);
24856 if (!error_operand_p (identifier))
24858 label = lookup_label (identifier);
24859 if (TREE_CODE (label) == LABEL_DECL)
24861 TREE_USED (label) = 1;
24862 check_goto (label);
24863 name = build_string (IDENTIFIER_LENGTH (identifier),
24864 IDENTIFIER_POINTER (identifier));
24865 labels = tree_cons (name, label, labels);
24868 /* If the next token is not a `,', then the list is
24869 complete. */
24870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24871 break;
24872 /* Consume the `,' token. */
24873 cp_lexer_consume_token (parser->lexer);
24876 return nreverse (labels);
24879 /* Return TRUE iff the next tokens in the stream are possibly the
24880 beginning of a GNU extension attribute. */
24882 static bool
24883 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24885 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24888 /* Return TRUE iff the next tokens in the stream are possibly the
24889 beginning of a standard C++-11 attribute specifier. */
24891 static bool
24892 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24894 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24897 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24898 beginning of a standard C++-11 attribute specifier. */
24900 static bool
24901 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24903 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24905 return (cxx_dialect >= cxx11
24906 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24907 || (token->type == CPP_OPEN_SQUARE
24908 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24909 && token->type == CPP_OPEN_SQUARE)));
24912 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24913 beginning of a GNU extension attribute. */
24915 static bool
24916 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24918 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24920 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24923 /* Return true iff the next tokens can be the beginning of either a
24924 GNU attribute list, or a standard C++11 attribute sequence. */
24926 static bool
24927 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24929 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24930 || cp_next_tokens_can_be_std_attribute_p (parser));
24933 /* Return true iff the next Nth tokens can be the beginning of either
24934 a GNU attribute list, or a standard C++11 attribute sequence. */
24936 static bool
24937 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24939 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24940 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24943 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24944 of GNU attributes, or return NULL. */
24946 static tree
24947 cp_parser_attributes_opt (cp_parser *parser)
24949 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24950 return cp_parser_gnu_attributes_opt (parser);
24951 return cp_parser_std_attribute_spec_seq (parser);
24954 /* Parse an (optional) series of attributes.
24956 attributes:
24957 attributes attribute
24959 attribute:
24960 __attribute__ (( attribute-list [opt] ))
24962 The return value is as for cp_parser_gnu_attribute_list. */
24964 static tree
24965 cp_parser_gnu_attributes_opt (cp_parser* parser)
24967 tree attributes = NULL_TREE;
24969 while (true)
24971 cp_token *token;
24972 tree attribute_list;
24973 bool ok = true;
24975 /* Peek at the next token. */
24976 token = cp_lexer_peek_token (parser->lexer);
24977 /* If it's not `__attribute__', then we're done. */
24978 if (token->keyword != RID_ATTRIBUTE)
24979 break;
24981 /* Consume the `__attribute__' keyword. */
24982 cp_lexer_consume_token (parser->lexer);
24983 /* Look for the two `(' tokens. */
24984 matching_parens outer_parens;
24985 outer_parens.require_open (parser);
24986 matching_parens inner_parens;
24987 inner_parens.require_open (parser);
24989 /* Peek at the next token. */
24990 token = cp_lexer_peek_token (parser->lexer);
24991 if (token->type != CPP_CLOSE_PAREN)
24992 /* Parse the attribute-list. */
24993 attribute_list = cp_parser_gnu_attribute_list (parser);
24994 else
24995 /* If the next token is a `)', then there is no attribute
24996 list. */
24997 attribute_list = NULL;
24999 /* Look for the two `)' tokens. */
25000 if (!inner_parens.require_close (parser))
25001 ok = false;
25002 if (!outer_parens.require_close (parser))
25003 ok = false;
25004 if (!ok)
25005 cp_parser_skip_to_end_of_statement (parser);
25007 /* Add these new attributes to the list. */
25008 attributes = attr_chainon (attributes, attribute_list);
25011 return attributes;
25014 /* Parse a GNU attribute-list.
25016 attribute-list:
25017 attribute
25018 attribute-list , attribute
25020 attribute:
25021 identifier
25022 identifier ( identifier )
25023 identifier ( identifier , expression-list )
25024 identifier ( expression-list )
25026 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25027 to an attribute. The TREE_PURPOSE of each node is the identifier
25028 indicating which attribute is in use. The TREE_VALUE represents
25029 the arguments, if any. */
25031 static tree
25032 cp_parser_gnu_attribute_list (cp_parser* parser)
25034 tree attribute_list = NULL_TREE;
25035 bool save_translate_strings_p = parser->translate_strings_p;
25037 parser->translate_strings_p = false;
25038 while (true)
25040 cp_token *token;
25041 tree identifier;
25042 tree attribute;
25044 /* Look for the identifier. We also allow keywords here; for
25045 example `__attribute__ ((const))' is legal. */
25046 token = cp_lexer_peek_token (parser->lexer);
25047 if (token->type == CPP_NAME
25048 || token->type == CPP_KEYWORD)
25050 tree arguments = NULL_TREE;
25052 /* Consume the token, but save it since we need it for the
25053 SIMD enabled function parsing. */
25054 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25056 /* Save away the identifier that indicates which attribute
25057 this is. */
25058 identifier = (token->type == CPP_KEYWORD)
25059 /* For keywords, use the canonical spelling, not the
25060 parsed identifier. */
25061 ? ridpointers[(int) token->keyword]
25062 : id_token->u.value;
25064 identifier = canonicalize_attr_name (identifier);
25065 attribute = build_tree_list (identifier, NULL_TREE);
25067 /* Peek at the next token. */
25068 token = cp_lexer_peek_token (parser->lexer);
25069 /* If it's an `(', then parse the attribute arguments. */
25070 if (token->type == CPP_OPEN_PAREN)
25072 vec<tree, va_gc> *vec;
25073 int attr_flag = (attribute_takes_identifier_p (identifier)
25074 ? id_attr : normal_attr);
25075 vec = cp_parser_parenthesized_expression_list
25076 (parser, attr_flag, /*cast_p=*/false,
25077 /*allow_expansion_p=*/false,
25078 /*non_constant_p=*/NULL);
25079 if (vec == NULL)
25080 arguments = error_mark_node;
25081 else
25083 arguments = build_tree_list_vec (vec);
25084 release_tree_vector (vec);
25086 /* Save the arguments away. */
25087 TREE_VALUE (attribute) = arguments;
25090 if (arguments != error_mark_node)
25092 /* Add this attribute to the list. */
25093 TREE_CHAIN (attribute) = attribute_list;
25094 attribute_list = attribute;
25097 token = cp_lexer_peek_token (parser->lexer);
25099 /* Now, look for more attributes. If the next token isn't a
25100 `,', we're done. */
25101 if (token->type != CPP_COMMA)
25102 break;
25104 /* Consume the comma and keep going. */
25105 cp_lexer_consume_token (parser->lexer);
25107 parser->translate_strings_p = save_translate_strings_p;
25109 /* We built up the list in reverse order. */
25110 return nreverse (attribute_list);
25113 /* Parse a standard C++11 attribute.
25115 The returned representation is a TREE_LIST which TREE_PURPOSE is
25116 the scoped name of the attribute, and the TREE_VALUE is its
25117 arguments list.
25119 Note that the scoped name of the attribute is itself a TREE_LIST
25120 which TREE_PURPOSE is the namespace of the attribute, and
25121 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25122 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25123 and which TREE_PURPOSE is directly the attribute name.
25125 Clients of the attribute code should use get_attribute_namespace
25126 and get_attribute_name to get the actual namespace and name of
25127 attributes, regardless of their being GNU or C++11 attributes.
25129 attribute:
25130 attribute-token attribute-argument-clause [opt]
25132 attribute-token:
25133 identifier
25134 attribute-scoped-token
25136 attribute-scoped-token:
25137 attribute-namespace :: identifier
25139 attribute-namespace:
25140 identifier
25142 attribute-argument-clause:
25143 ( balanced-token-seq )
25145 balanced-token-seq:
25146 balanced-token [opt]
25147 balanced-token-seq balanced-token
25149 balanced-token:
25150 ( balanced-token-seq )
25151 [ balanced-token-seq ]
25152 { balanced-token-seq }. */
25154 static tree
25155 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25157 tree attribute, attr_id = NULL_TREE, arguments;
25158 cp_token *token;
25160 /* First, parse name of the attribute, a.k.a attribute-token. */
25162 token = cp_lexer_peek_token (parser->lexer);
25163 if (token->type == CPP_NAME)
25164 attr_id = token->u.value;
25165 else if (token->type == CPP_KEYWORD)
25166 attr_id = ridpointers[(int) token->keyword];
25167 else if (token->flags & NAMED_OP)
25168 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25170 if (attr_id == NULL_TREE)
25171 return NULL_TREE;
25173 cp_lexer_consume_token (parser->lexer);
25175 token = cp_lexer_peek_token (parser->lexer);
25176 if (token->type == CPP_SCOPE)
25178 /* We are seeing a scoped attribute token. */
25180 cp_lexer_consume_token (parser->lexer);
25181 if (attr_ns)
25182 error_at (token->location, "attribute using prefix used together "
25183 "with scoped attribute token");
25184 attr_ns = attr_id;
25186 token = cp_lexer_consume_token (parser->lexer);
25187 if (token->type == CPP_NAME)
25188 attr_id = token->u.value;
25189 else if (token->type == CPP_KEYWORD)
25190 attr_id = ridpointers[(int) token->keyword];
25191 else if (token->flags & NAMED_OP)
25192 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25193 else
25195 error_at (token->location,
25196 "expected an identifier for the attribute name");
25197 return error_mark_node;
25200 attr_id = canonicalize_attr_name (attr_id);
25201 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25202 NULL_TREE);
25203 token = cp_lexer_peek_token (parser->lexer);
25205 else if (attr_ns)
25206 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25207 NULL_TREE);
25208 else
25210 attr_id = canonicalize_attr_name (attr_id);
25211 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25212 NULL_TREE);
25213 /* C++11 noreturn attribute is equivalent to GNU's. */
25214 if (is_attribute_p ("noreturn", attr_id))
25215 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25216 /* C++14 deprecated attribute is equivalent to GNU's. */
25217 else if (is_attribute_p ("deprecated", attr_id))
25218 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25219 /* C++17 fallthrough attribute is equivalent to GNU's. */
25220 else if (is_attribute_p ("fallthrough", attr_id))
25221 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25222 /* Transactional Memory TS optimize_for_synchronized attribute is
25223 equivalent to GNU transaction_callable. */
25224 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25225 TREE_PURPOSE (attribute)
25226 = get_identifier ("transaction_callable");
25227 /* Transactional Memory attributes are GNU attributes. */
25228 else if (tm_attr_to_mask (attr_id))
25229 TREE_PURPOSE (attribute) = attr_id;
25232 /* Now parse the optional argument clause of the attribute. */
25234 if (token->type != CPP_OPEN_PAREN)
25235 return attribute;
25238 vec<tree, va_gc> *vec;
25239 int attr_flag = normal_attr;
25241 if (attr_ns == get_identifier ("gnu")
25242 && attribute_takes_identifier_p (attr_id))
25243 /* A GNU attribute that takes an identifier in parameter. */
25244 attr_flag = id_attr;
25246 vec = cp_parser_parenthesized_expression_list
25247 (parser, attr_flag, /*cast_p=*/false,
25248 /*allow_expansion_p=*/true,
25249 /*non_constant_p=*/NULL);
25250 if (vec == NULL)
25251 arguments = error_mark_node;
25252 else
25254 arguments = build_tree_list_vec (vec);
25255 release_tree_vector (vec);
25258 if (arguments == error_mark_node)
25259 attribute = error_mark_node;
25260 else
25261 TREE_VALUE (attribute) = arguments;
25264 return attribute;
25267 /* Check that the attribute ATTRIBUTE appears at most once in the
25268 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25269 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25270 isn't implemented yet in GCC. */
25272 static void
25273 cp_parser_check_std_attribute (tree attributes, tree attribute)
25275 if (attributes)
25277 tree name = get_attribute_name (attribute);
25278 if (is_attribute_p ("noreturn", name)
25279 && lookup_attribute ("noreturn", attributes))
25280 error ("attribute %<noreturn%> can appear at most once "
25281 "in an attribute-list");
25282 else if (is_attribute_p ("deprecated", name)
25283 && lookup_attribute ("deprecated", attributes))
25284 error ("attribute %<deprecated%> can appear at most once "
25285 "in an attribute-list");
25289 /* Parse a list of standard C++-11 attributes.
25291 attribute-list:
25292 attribute [opt]
25293 attribute-list , attribute[opt]
25294 attribute ...
25295 attribute-list , attribute ...
25298 static tree
25299 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25301 tree attributes = NULL_TREE, attribute = NULL_TREE;
25302 cp_token *token = NULL;
25304 while (true)
25306 attribute = cp_parser_std_attribute (parser, attr_ns);
25307 if (attribute == error_mark_node)
25308 break;
25309 if (attribute != NULL_TREE)
25311 cp_parser_check_std_attribute (attributes, attribute);
25312 TREE_CHAIN (attribute) = attributes;
25313 attributes = attribute;
25315 token = cp_lexer_peek_token (parser->lexer);
25316 if (token->type == CPP_ELLIPSIS)
25318 cp_lexer_consume_token (parser->lexer);
25319 if (attribute == NULL_TREE)
25320 error_at (token->location,
25321 "expected attribute before %<...%>");
25322 else
25324 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25325 if (pack == error_mark_node)
25326 return error_mark_node;
25327 TREE_VALUE (attribute) = pack;
25329 token = cp_lexer_peek_token (parser->lexer);
25331 if (token->type != CPP_COMMA)
25332 break;
25333 cp_lexer_consume_token (parser->lexer);
25335 attributes = nreverse (attributes);
25336 return attributes;
25339 /* Parse a standard C++-11 attribute specifier.
25341 attribute-specifier:
25342 [ [ attribute-using-prefix [opt] attribute-list ] ]
25343 alignment-specifier
25345 attribute-using-prefix:
25346 using attribute-namespace :
25348 alignment-specifier:
25349 alignas ( type-id ... [opt] )
25350 alignas ( alignment-expression ... [opt] ). */
25352 static tree
25353 cp_parser_std_attribute_spec (cp_parser *parser)
25355 tree attributes = NULL_TREE;
25356 cp_token *token = cp_lexer_peek_token (parser->lexer);
25358 if (token->type == CPP_OPEN_SQUARE
25359 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25361 tree attr_ns = NULL_TREE;
25363 cp_lexer_consume_token (parser->lexer);
25364 cp_lexer_consume_token (parser->lexer);
25366 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25368 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25369 if (token->type == CPP_NAME)
25370 attr_ns = token->u.value;
25371 else if (token->type == CPP_KEYWORD)
25372 attr_ns = ridpointers[(int) token->keyword];
25373 else if (token->flags & NAMED_OP)
25374 attr_ns = get_identifier (cpp_type2name (token->type,
25375 token->flags));
25376 if (attr_ns
25377 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25379 if (cxx_dialect < cxx17
25380 && !in_system_header_at (input_location))
25381 pedwarn (input_location, 0,
25382 "attribute using prefix only available "
25383 "with -std=c++17 or -std=gnu++17");
25385 cp_lexer_consume_token (parser->lexer);
25386 cp_lexer_consume_token (parser->lexer);
25387 cp_lexer_consume_token (parser->lexer);
25389 else
25390 attr_ns = NULL_TREE;
25393 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25395 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25396 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25397 cp_parser_skip_to_end_of_statement (parser);
25398 else
25399 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25400 when we are sure that we have actually parsed them. */
25401 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25403 else
25405 tree alignas_expr;
25407 /* Look for an alignment-specifier. */
25409 token = cp_lexer_peek_token (parser->lexer);
25411 if (token->type != CPP_KEYWORD
25412 || token->keyword != RID_ALIGNAS)
25413 return NULL_TREE;
25415 cp_lexer_consume_token (parser->lexer);
25416 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25418 matching_parens parens;
25419 if (!parens.require_open (parser))
25420 return error_mark_node;
25422 cp_parser_parse_tentatively (parser);
25423 alignas_expr = cp_parser_type_id (parser);
25425 if (!cp_parser_parse_definitely (parser))
25427 alignas_expr = cp_parser_assignment_expression (parser);
25428 if (alignas_expr == error_mark_node)
25429 cp_parser_skip_to_end_of_statement (parser);
25430 if (alignas_expr == NULL_TREE
25431 || alignas_expr == error_mark_node)
25432 return alignas_expr;
25435 alignas_expr = cxx_alignas_expr (alignas_expr);
25436 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25438 /* Handle alignas (pack...). */
25439 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25441 cp_lexer_consume_token (parser->lexer);
25442 alignas_expr = make_pack_expansion (alignas_expr);
25445 /* Something went wrong, so don't build the attribute. */
25446 if (alignas_expr == error_mark_node)
25447 return error_mark_node;
25449 if (!parens.require_close (parser))
25450 return error_mark_node;
25452 /* Build the C++-11 representation of an 'aligned'
25453 attribute. */
25454 attributes =
25455 build_tree_list (build_tree_list (get_identifier ("gnu"),
25456 get_identifier ("aligned")),
25457 alignas_expr);
25460 return attributes;
25463 /* Parse a standard C++-11 attribute-specifier-seq.
25465 attribute-specifier-seq:
25466 attribute-specifier-seq [opt] attribute-specifier
25469 static tree
25470 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25472 tree attr_specs = NULL_TREE;
25473 tree attr_last = NULL_TREE;
25475 while (true)
25477 tree attr_spec = cp_parser_std_attribute_spec (parser);
25478 if (attr_spec == NULL_TREE)
25479 break;
25480 if (attr_spec == error_mark_node)
25481 return error_mark_node;
25483 if (attr_last)
25484 TREE_CHAIN (attr_last) = attr_spec;
25485 else
25486 attr_specs = attr_last = attr_spec;
25487 attr_last = tree_last (attr_last);
25490 return attr_specs;
25493 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25494 return index of the first token after balanced-token, or N on failure. */
25496 static size_t
25497 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25499 size_t orig_n = n;
25500 int nparens = 0, nbraces = 0, nsquares = 0;
25502 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25504 case CPP_EOF:
25505 case CPP_PRAGMA_EOL:
25506 /* Ran out of tokens. */
25507 return orig_n;
25508 case CPP_OPEN_PAREN:
25509 ++nparens;
25510 break;
25511 case CPP_OPEN_BRACE:
25512 ++nbraces;
25513 break;
25514 case CPP_OPEN_SQUARE:
25515 ++nsquares;
25516 break;
25517 case CPP_CLOSE_PAREN:
25518 --nparens;
25519 break;
25520 case CPP_CLOSE_BRACE:
25521 --nbraces;
25522 break;
25523 case CPP_CLOSE_SQUARE:
25524 --nsquares;
25525 break;
25526 default:
25527 break;
25529 while (nparens || nbraces || nsquares);
25530 return n;
25533 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25534 return index of the first token after the GNU attribute tokens, or N on
25535 failure. */
25537 static size_t
25538 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25540 while (true)
25542 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25543 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25544 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25545 break;
25547 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25548 if (n2 == n + 2)
25549 break;
25550 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25551 break;
25552 n = n2 + 1;
25554 return n;
25557 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25558 next token), return index of the first token after the standard C++11
25559 attribute tokens, or N on failure. */
25561 static size_t
25562 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25564 while (true)
25566 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25567 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25569 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25570 if (n2 == n + 1)
25571 break;
25572 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25573 break;
25574 n = n2 + 1;
25576 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25577 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25579 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25580 if (n2 == n + 1)
25581 break;
25582 n = n2;
25584 else
25585 break;
25587 return n;
25590 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25591 as the next token), return index of the first token after the attribute
25592 tokens, or N on failure. */
25594 static size_t
25595 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25597 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25598 return cp_parser_skip_gnu_attributes_opt (parser, n);
25599 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25602 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25603 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25604 current value of the PEDANTIC flag, regardless of whether or not
25605 the `__extension__' keyword is present. The caller is responsible
25606 for restoring the value of the PEDANTIC flag. */
25608 static bool
25609 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25611 /* Save the old value of the PEDANTIC flag. */
25612 *saved_pedantic = pedantic;
25614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25616 /* Consume the `__extension__' token. */
25617 cp_lexer_consume_token (parser->lexer);
25618 /* We're not being pedantic while the `__extension__' keyword is
25619 in effect. */
25620 pedantic = 0;
25622 return true;
25625 return false;
25628 /* Parse a label declaration.
25630 label-declaration:
25631 __label__ label-declarator-seq ;
25633 label-declarator-seq:
25634 identifier , label-declarator-seq
25635 identifier */
25637 static void
25638 cp_parser_label_declaration (cp_parser* parser)
25640 /* Look for the `__label__' keyword. */
25641 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25643 while (true)
25645 tree identifier;
25647 /* Look for an identifier. */
25648 identifier = cp_parser_identifier (parser);
25649 /* If we failed, stop. */
25650 if (identifier == error_mark_node)
25651 break;
25652 /* Declare it as a label. */
25653 finish_label_decl (identifier);
25654 /* If the next token is a `;', stop. */
25655 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25656 break;
25657 /* Look for the `,' separating the label declarations. */
25658 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25661 /* Look for the final `;'. */
25662 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25665 // -------------------------------------------------------------------------- //
25666 // Requires Clause
25668 // Parse a requires clause.
25670 // requires-clause:
25671 // 'requires' logical-or-expression
25673 // The required logical-or-expression must be a constant expression. Note
25674 // that we don't check that the expression is constepxr here. We defer until
25675 // we analyze constraints and then, we only check atomic constraints.
25676 static tree
25677 cp_parser_requires_clause (cp_parser *parser)
25679 // Parse the requires clause so that it is not automatically folded.
25680 ++processing_template_decl;
25681 tree expr = cp_parser_binary_expression (parser, false, false,
25682 PREC_NOT_OPERATOR, NULL);
25683 if (check_for_bare_parameter_packs (expr))
25684 expr = error_mark_node;
25685 --processing_template_decl;
25686 return expr;
25689 // Optionally parse a requires clause:
25690 static tree
25691 cp_parser_requires_clause_opt (cp_parser *parser)
25693 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25694 if (tok->keyword != RID_REQUIRES)
25696 if (!flag_concepts && tok->type == CPP_NAME
25697 && tok->u.value == ridpointers[RID_REQUIRES])
25699 error_at (cp_lexer_peek_token (parser->lexer)->location,
25700 "%<requires%> only available with -fconcepts");
25701 /* Parse and discard the requires-clause. */
25702 cp_lexer_consume_token (parser->lexer);
25703 cp_parser_requires_clause (parser);
25705 return NULL_TREE;
25707 cp_lexer_consume_token (parser->lexer);
25708 return cp_parser_requires_clause (parser);
25712 /*---------------------------------------------------------------------------
25713 Requires expressions
25714 ---------------------------------------------------------------------------*/
25716 /* Parse a requires expression
25718 requirement-expression:
25719 'requires' requirement-parameter-list [opt] requirement-body */
25720 static tree
25721 cp_parser_requires_expression (cp_parser *parser)
25723 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25724 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25726 /* A requires-expression shall appear only within a concept
25727 definition or a requires-clause.
25729 TODO: Implement this diagnostic correctly. */
25730 if (!processing_template_decl)
25732 error_at (loc, "a requires expression cannot appear outside a template");
25733 cp_parser_skip_to_end_of_statement (parser);
25734 return error_mark_node;
25737 tree parms, reqs;
25739 /* Local parameters are delared as variables within the scope
25740 of the expression. They are not visible past the end of
25741 the expression. Expressions within the requires-expression
25742 are unevaluated. */
25743 struct scope_sentinel
25745 scope_sentinel ()
25747 ++cp_unevaluated_operand;
25748 begin_scope (sk_block, NULL_TREE);
25751 ~scope_sentinel ()
25753 pop_bindings_and_leave_scope ();
25754 --cp_unevaluated_operand;
25756 } s;
25758 /* Parse the optional parameter list. */
25759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25761 parms = cp_parser_requirement_parameter_list (parser);
25762 if (parms == error_mark_node)
25763 return error_mark_node;
25765 else
25766 parms = NULL_TREE;
25768 /* Parse the requirement body. */
25769 reqs = cp_parser_requirement_body (parser);
25770 if (reqs == error_mark_node)
25771 return error_mark_node;
25774 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25775 the parm chain. */
25776 grokparms (parms, &parms);
25777 return finish_requires_expr (parms, reqs);
25780 /* Parse a parameterized requirement.
25782 requirement-parameter-list:
25783 '(' parameter-declaration-clause ')' */
25784 static tree
25785 cp_parser_requirement_parameter_list (cp_parser *parser)
25787 matching_parens parens;
25788 if (!parens.require_open (parser))
25789 return error_mark_node;
25791 tree parms = cp_parser_parameter_declaration_clause (parser);
25793 if (!parens.require_close (parser))
25794 return error_mark_node;
25796 return parms;
25799 /* Parse the body of a requirement.
25801 requirement-body:
25802 '{' requirement-list '}' */
25803 static tree
25804 cp_parser_requirement_body (cp_parser *parser)
25806 matching_braces braces;
25807 if (!braces.require_open (parser))
25808 return error_mark_node;
25810 tree reqs = cp_parser_requirement_list (parser);
25812 if (!braces.require_close (parser))
25813 return error_mark_node;
25815 return reqs;
25818 /* Parse a list of requirements.
25820 requirement-list:
25821 requirement
25822 requirement-list ';' requirement[opt] */
25823 static tree
25824 cp_parser_requirement_list (cp_parser *parser)
25826 tree result = NULL_TREE;
25827 while (true)
25829 tree req = cp_parser_requirement (parser);
25830 if (req == error_mark_node)
25831 return error_mark_node;
25833 result = tree_cons (NULL_TREE, req, result);
25835 /* If we see a semi-colon, consume it. */
25836 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25837 cp_lexer_consume_token (parser->lexer);
25839 /* Stop processing at the end of the list. */
25840 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25841 break;
25844 /* Reverse the order of requirements so they are analyzed in
25845 declaration order. */
25846 return nreverse (result);
25849 /* Parse a syntactic requirement or type requirement.
25851 requirement:
25852 simple-requirement
25853 compound-requirement
25854 type-requirement
25855 nested-requirement */
25856 static tree
25857 cp_parser_requirement (cp_parser *parser)
25859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25860 return cp_parser_compound_requirement (parser);
25861 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25862 return cp_parser_type_requirement (parser);
25863 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25864 return cp_parser_nested_requirement (parser);
25865 else
25866 return cp_parser_simple_requirement (parser);
25869 /* Parse a simple requirement.
25871 simple-requirement:
25872 expression ';' */
25873 static tree
25874 cp_parser_simple_requirement (cp_parser *parser)
25876 tree expr = cp_parser_expression (parser, NULL, false, false);
25877 if (!expr || expr == error_mark_node)
25878 return error_mark_node;
25880 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25881 return error_mark_node;
25883 return finish_simple_requirement (expr);
25886 /* Parse a type requirement
25888 type-requirement
25889 nested-name-specifier [opt] required-type-name ';'
25891 required-type-name:
25892 type-name
25893 'template' [opt] simple-template-id */
25894 static tree
25895 cp_parser_type_requirement (cp_parser *parser)
25897 cp_lexer_consume_token (parser->lexer);
25899 // Save the scope before parsing name specifiers.
25900 tree saved_scope = parser->scope;
25901 tree saved_object_scope = parser->object_scope;
25902 tree saved_qualifying_scope = parser->qualifying_scope;
25903 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25904 cp_parser_nested_name_specifier_opt (parser,
25905 /*typename_keyword_p=*/true,
25906 /*check_dependency_p=*/false,
25907 /*type_p=*/true,
25908 /*is_declaration=*/false);
25910 tree type;
25911 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25913 cp_lexer_consume_token (parser->lexer);
25914 type = cp_parser_template_id (parser,
25915 /*template_keyword_p=*/true,
25916 /*check_dependency=*/false,
25917 /*tag_type=*/none_type,
25918 /*is_declaration=*/false);
25919 type = make_typename_type (parser->scope, type, typename_type,
25920 /*complain=*/tf_error);
25922 else
25923 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25925 if (TREE_CODE (type) == TYPE_DECL)
25926 type = TREE_TYPE (type);
25928 parser->scope = saved_scope;
25929 parser->object_scope = saved_object_scope;
25930 parser->qualifying_scope = saved_qualifying_scope;
25932 if (type == error_mark_node)
25933 cp_parser_skip_to_end_of_statement (parser);
25935 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25936 return error_mark_node;
25937 if (type == error_mark_node)
25938 return error_mark_node;
25940 return finish_type_requirement (type);
25943 /* Parse a compound requirement
25945 compound-requirement:
25946 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25947 static tree
25948 cp_parser_compound_requirement (cp_parser *parser)
25950 /* Parse an expression enclosed in '{ }'s. */
25951 matching_braces braces;
25952 if (!braces.require_open (parser))
25953 return error_mark_node;
25955 tree expr = cp_parser_expression (parser, NULL, false, false);
25956 if (!expr || expr == error_mark_node)
25957 return error_mark_node;
25959 if (!braces.require_close (parser))
25960 return error_mark_node;
25962 /* Parse the optional noexcept. */
25963 bool noexcept_p = false;
25964 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25966 cp_lexer_consume_token (parser->lexer);
25967 noexcept_p = true;
25970 /* Parse the optional trailing return type. */
25971 tree type = NULL_TREE;
25972 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25974 cp_lexer_consume_token (parser->lexer);
25975 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25976 parser->in_result_type_constraint_p = true;
25977 type = cp_parser_trailing_type_id (parser);
25978 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25979 if (type == error_mark_node)
25980 return error_mark_node;
25983 return finish_compound_requirement (expr, type, noexcept_p);
25986 /* Parse a nested requirement. This is the same as a requires clause.
25988 nested-requirement:
25989 requires-clause */
25990 static tree
25991 cp_parser_nested_requirement (cp_parser *parser)
25993 cp_lexer_consume_token (parser->lexer);
25994 tree req = cp_parser_requires_clause (parser);
25995 if (req == error_mark_node)
25996 return error_mark_node;
25997 return finish_nested_requirement (req);
26000 /* Support Functions */
26002 /* Return the appropriate prefer_type argument for lookup_name_real based on
26003 tag_type and template_mem_access. */
26005 static inline int
26006 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26008 /* DR 141: When looking in the current enclosing context for a template-name
26009 after -> or ., only consider class templates. */
26010 if (template_mem_access)
26011 return 2;
26012 switch (tag_type)
26014 case none_type: return 0; // No preference.
26015 case scope_type: return 1; // Type or namespace.
26016 default: return 2; // Type only.
26020 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26021 NAME should have one of the representations used for an
26022 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26023 is returned. If PARSER->SCOPE is a dependent type, then a
26024 SCOPE_REF is returned.
26026 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26027 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26028 was formed. Abstractly, such entities should not be passed to this
26029 function, because they do not need to be looked up, but it is
26030 simpler to check for this special case here, rather than at the
26031 call-sites.
26033 In cases not explicitly covered above, this function returns a
26034 DECL, OVERLOAD, or baselink representing the result of the lookup.
26035 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26036 is returned.
26038 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26039 (e.g., "struct") that was used. In that case bindings that do not
26040 refer to types are ignored.
26042 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26043 ignored.
26045 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26046 are ignored.
26048 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26049 types.
26051 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26052 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26053 NULL_TREE otherwise. */
26055 static cp_expr
26056 cp_parser_lookup_name (cp_parser *parser, tree name,
26057 enum tag_types tag_type,
26058 bool is_template,
26059 bool is_namespace,
26060 bool check_dependency,
26061 tree *ambiguous_decls,
26062 location_t name_location)
26064 tree decl;
26065 tree object_type = parser->context->object_type;
26067 /* Assume that the lookup will be unambiguous. */
26068 if (ambiguous_decls)
26069 *ambiguous_decls = NULL_TREE;
26071 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26072 no longer valid. Note that if we are parsing tentatively, and
26073 the parse fails, OBJECT_TYPE will be automatically restored. */
26074 parser->context->object_type = NULL_TREE;
26076 if (name == error_mark_node)
26077 return error_mark_node;
26079 /* A template-id has already been resolved; there is no lookup to
26080 do. */
26081 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26082 return name;
26083 if (BASELINK_P (name))
26085 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26086 == TEMPLATE_ID_EXPR);
26087 return name;
26090 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26091 it should already have been checked to make sure that the name
26092 used matches the type being destroyed. */
26093 if (TREE_CODE (name) == BIT_NOT_EXPR)
26095 tree type;
26097 /* Figure out to which type this destructor applies. */
26098 if (parser->scope)
26099 type = parser->scope;
26100 else if (object_type)
26101 type = object_type;
26102 else
26103 type = current_class_type;
26104 /* If that's not a class type, there is no destructor. */
26105 if (!type || !CLASS_TYPE_P (type))
26106 return error_mark_node;
26108 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26109 lazily_declare_fn (sfk_destructor, type);
26111 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26112 return dtor;
26114 return error_mark_node;
26117 /* By this point, the NAME should be an ordinary identifier. If
26118 the id-expression was a qualified name, the qualifying scope is
26119 stored in PARSER->SCOPE at this point. */
26120 gcc_assert (identifier_p (name));
26122 /* Perform the lookup. */
26123 if (parser->scope)
26125 bool dependent_p;
26127 if (parser->scope == error_mark_node)
26128 return error_mark_node;
26130 /* If the SCOPE is dependent, the lookup must be deferred until
26131 the template is instantiated -- unless we are explicitly
26132 looking up names in uninstantiated templates. Even then, we
26133 cannot look up the name if the scope is not a class type; it
26134 might, for example, be a template type parameter. */
26135 dependent_p = (TYPE_P (parser->scope)
26136 && dependent_scope_p (parser->scope));
26137 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26138 && dependent_p)
26139 /* Defer lookup. */
26140 decl = error_mark_node;
26141 else
26143 tree pushed_scope = NULL_TREE;
26145 /* If PARSER->SCOPE is a dependent type, then it must be a
26146 class type, and we must not be checking dependencies;
26147 otherwise, we would have processed this lookup above. So
26148 that PARSER->SCOPE is not considered a dependent base by
26149 lookup_member, we must enter the scope here. */
26150 if (dependent_p)
26151 pushed_scope = push_scope (parser->scope);
26153 /* If the PARSER->SCOPE is a template specialization, it
26154 may be instantiated during name lookup. In that case,
26155 errors may be issued. Even if we rollback the current
26156 tentative parse, those errors are valid. */
26157 decl = lookup_qualified_name (parser->scope, name,
26158 prefer_type_arg (tag_type),
26159 /*complain=*/true);
26161 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26162 lookup result and the nested-name-specifier nominates a class C:
26163 * if the name specified after the nested-name-specifier, when
26164 looked up in C, is the injected-class-name of C (Clause 9), or
26165 * if the name specified after the nested-name-specifier is the
26166 same as the identifier or the simple-template-id's template-
26167 name in the last component of the nested-name-specifier,
26168 the name is instead considered to name the constructor of
26169 class C. [ Note: for example, the constructor is not an
26170 acceptable lookup result in an elaborated-type-specifier so
26171 the constructor would not be used in place of the
26172 injected-class-name. --end note ] Such a constructor name
26173 shall be used only in the declarator-id of a declaration that
26174 names a constructor or in a using-declaration. */
26175 if (tag_type == none_type
26176 && DECL_SELF_REFERENCE_P (decl)
26177 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26178 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26179 prefer_type_arg (tag_type),
26180 /*complain=*/true);
26182 /* If we have a single function from a using decl, pull it out. */
26183 if (TREE_CODE (decl) == OVERLOAD
26184 && !really_overloaded_fn (decl))
26185 decl = OVL_FUNCTION (decl);
26187 if (pushed_scope)
26188 pop_scope (pushed_scope);
26191 /* If the scope is a dependent type and either we deferred lookup or
26192 we did lookup but didn't find the name, rememeber the name. */
26193 if (decl == error_mark_node && TYPE_P (parser->scope)
26194 && dependent_type_p (parser->scope))
26196 if (tag_type)
26198 tree type;
26200 /* The resolution to Core Issue 180 says that `struct
26201 A::B' should be considered a type-name, even if `A'
26202 is dependent. */
26203 type = make_typename_type (parser->scope, name, tag_type,
26204 /*complain=*/tf_error);
26205 if (type != error_mark_node)
26206 decl = TYPE_NAME (type);
26208 else if (is_template
26209 && (cp_parser_next_token_ends_template_argument_p (parser)
26210 || cp_lexer_next_token_is (parser->lexer,
26211 CPP_CLOSE_PAREN)))
26212 decl = make_unbound_class_template (parser->scope,
26213 name, NULL_TREE,
26214 /*complain=*/tf_error);
26215 else
26216 decl = build_qualified_name (/*type=*/NULL_TREE,
26217 parser->scope, name,
26218 is_template);
26220 parser->qualifying_scope = parser->scope;
26221 parser->object_scope = NULL_TREE;
26223 else if (object_type)
26225 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26226 OBJECT_TYPE is not a class. */
26227 if (CLASS_TYPE_P (object_type))
26228 /* If the OBJECT_TYPE is a template specialization, it may
26229 be instantiated during name lookup. In that case, errors
26230 may be issued. Even if we rollback the current tentative
26231 parse, those errors are valid. */
26232 decl = lookup_member (object_type,
26233 name,
26234 /*protect=*/0,
26235 prefer_type_arg (tag_type),
26236 tf_warning_or_error);
26237 else
26238 decl = NULL_TREE;
26240 if (!decl)
26241 /* Look it up in the enclosing context. DR 141: When looking for a
26242 template-name after -> or ., only consider class templates. */
26243 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26244 /*nonclass=*/0,
26245 /*block_p=*/true, is_namespace, 0);
26246 if (object_type == unknown_type_node)
26247 /* The object is type-dependent, so we can't look anything up; we used
26248 this to get the DR 141 behavior. */
26249 object_type = NULL_TREE;
26250 parser->object_scope = object_type;
26251 parser->qualifying_scope = NULL_TREE;
26253 else
26255 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26256 /*nonclass=*/0,
26257 /*block_p=*/true, is_namespace, 0);
26258 parser->qualifying_scope = NULL_TREE;
26259 parser->object_scope = NULL_TREE;
26262 /* If the lookup failed, let our caller know. */
26263 if (!decl || decl == error_mark_node)
26264 return error_mark_node;
26266 /* Pull out the template from an injected-class-name (or multiple). */
26267 if (is_template)
26268 decl = maybe_get_template_decl_from_type_decl (decl);
26270 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26271 if (TREE_CODE (decl) == TREE_LIST)
26273 if (ambiguous_decls)
26274 *ambiguous_decls = decl;
26275 /* The error message we have to print is too complicated for
26276 cp_parser_error, so we incorporate its actions directly. */
26277 if (!cp_parser_simulate_error (parser))
26279 error_at (name_location, "reference to %qD is ambiguous",
26280 name);
26281 print_candidates (decl);
26283 return error_mark_node;
26286 gcc_assert (DECL_P (decl)
26287 || TREE_CODE (decl) == OVERLOAD
26288 || TREE_CODE (decl) == SCOPE_REF
26289 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26290 || BASELINK_P (decl));
26292 /* If we have resolved the name of a member declaration, check to
26293 see if the declaration is accessible. When the name resolves to
26294 set of overloaded functions, accessibility is checked when
26295 overload resolution is done.
26297 During an explicit instantiation, access is not checked at all,
26298 as per [temp.explicit]. */
26299 if (DECL_P (decl))
26300 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26302 maybe_record_typedef_use (decl);
26304 return cp_expr (decl, name_location);
26307 /* Like cp_parser_lookup_name, but for use in the typical case where
26308 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26309 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26311 static tree
26312 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26314 return cp_parser_lookup_name (parser, name,
26315 none_type,
26316 /*is_template=*/false,
26317 /*is_namespace=*/false,
26318 /*check_dependency=*/true,
26319 /*ambiguous_decls=*/NULL,
26320 location);
26323 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26324 the current context, return the TYPE_DECL. If TAG_NAME_P is
26325 true, the DECL indicates the class being defined in a class-head,
26326 or declared in an elaborated-type-specifier.
26328 Otherwise, return DECL. */
26330 static tree
26331 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26333 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26334 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26336 struct A {
26337 template <typename T> struct B;
26340 template <typename T> struct A::B {};
26342 Similarly, in an elaborated-type-specifier:
26344 namespace N { struct X{}; }
26346 struct A {
26347 template <typename T> friend struct N::X;
26350 However, if the DECL refers to a class type, and we are in
26351 the scope of the class, then the name lookup automatically
26352 finds the TYPE_DECL created by build_self_reference rather
26353 than a TEMPLATE_DECL. For example, in:
26355 template <class T> struct S {
26356 S s;
26359 there is no need to handle such case. */
26361 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26362 return DECL_TEMPLATE_RESULT (decl);
26364 return decl;
26367 /* If too many, or too few, template-parameter lists apply to the
26368 declarator, issue an error message. Returns TRUE if all went well,
26369 and FALSE otherwise. */
26371 static bool
26372 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26373 cp_declarator *declarator,
26374 location_t declarator_location)
26376 switch (declarator->kind)
26378 case cdk_id:
26380 unsigned num_templates = 0;
26381 tree scope = declarator->u.id.qualifying_scope;
26383 if (scope)
26384 num_templates = num_template_headers_for_class (scope);
26385 else if (TREE_CODE (declarator->u.id.unqualified_name)
26386 == TEMPLATE_ID_EXPR)
26387 /* If the DECLARATOR has the form `X<y>' then it uses one
26388 additional level of template parameters. */
26389 ++num_templates;
26391 return cp_parser_check_template_parameters
26392 (parser, num_templates, declarator_location, declarator);
26395 case cdk_function:
26396 case cdk_array:
26397 case cdk_pointer:
26398 case cdk_reference:
26399 case cdk_ptrmem:
26400 return (cp_parser_check_declarator_template_parameters
26401 (parser, declarator->declarator, declarator_location));
26403 case cdk_decomp:
26404 case cdk_error:
26405 return true;
26407 default:
26408 gcc_unreachable ();
26410 return false;
26413 /* NUM_TEMPLATES were used in the current declaration. If that is
26414 invalid, return FALSE and issue an error messages. Otherwise,
26415 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26416 declarator and we can print more accurate diagnostics. */
26418 static bool
26419 cp_parser_check_template_parameters (cp_parser* parser,
26420 unsigned num_templates,
26421 location_t location,
26422 cp_declarator *declarator)
26424 /* If there are the same number of template classes and parameter
26425 lists, that's OK. */
26426 if (parser->num_template_parameter_lists == num_templates)
26427 return true;
26428 /* If there are more, but only one more, then we are referring to a
26429 member template. That's OK too. */
26430 if (parser->num_template_parameter_lists == num_templates + 1)
26431 return true;
26432 /* If there are more template classes than parameter lists, we have
26433 something like:
26435 template <class T> void S<T>::R<T>::f (); */
26436 if (parser->num_template_parameter_lists < num_templates)
26438 if (declarator && !current_function_decl)
26439 error_at (location, "specializing member %<%T::%E%> "
26440 "requires %<template<>%> syntax",
26441 declarator->u.id.qualifying_scope,
26442 declarator->u.id.unqualified_name);
26443 else if (declarator)
26444 error_at (location, "invalid declaration of %<%T::%E%>",
26445 declarator->u.id.qualifying_scope,
26446 declarator->u.id.unqualified_name);
26447 else
26448 error_at (location, "too few template-parameter-lists");
26449 return false;
26451 /* Otherwise, there are too many template parameter lists. We have
26452 something like:
26454 template <class T> template <class U> void S::f(); */
26455 error_at (location, "too many template-parameter-lists");
26456 return false;
26459 /* Parse an optional `::' token indicating that the following name is
26460 from the global namespace. If so, PARSER->SCOPE is set to the
26461 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26462 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26463 Returns the new value of PARSER->SCOPE, if the `::' token is
26464 present, and NULL_TREE otherwise. */
26466 static tree
26467 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26469 cp_token *token;
26471 /* Peek at the next token. */
26472 token = cp_lexer_peek_token (parser->lexer);
26473 /* If we're looking at a `::' token then we're starting from the
26474 global namespace, not our current location. */
26475 if (token->type == CPP_SCOPE)
26477 /* Consume the `::' token. */
26478 cp_lexer_consume_token (parser->lexer);
26479 /* Set the SCOPE so that we know where to start the lookup. */
26480 parser->scope = global_namespace;
26481 parser->qualifying_scope = global_namespace;
26482 parser->object_scope = NULL_TREE;
26484 return parser->scope;
26486 else if (!current_scope_valid_p)
26488 parser->scope = NULL_TREE;
26489 parser->qualifying_scope = NULL_TREE;
26490 parser->object_scope = NULL_TREE;
26493 return NULL_TREE;
26496 /* Returns TRUE if the upcoming token sequence is the start of a
26497 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26498 declarator is preceded by the `friend' specifier. */
26500 static bool
26501 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26503 bool constructor_p;
26504 bool outside_class_specifier_p;
26505 tree nested_name_specifier;
26506 cp_token *next_token;
26508 /* The common case is that this is not a constructor declarator, so
26509 try to avoid doing lots of work if at all possible. It's not
26510 valid declare a constructor at function scope. */
26511 if (parser->in_function_body)
26512 return false;
26513 /* And only certain tokens can begin a constructor declarator. */
26514 next_token = cp_lexer_peek_token (parser->lexer);
26515 if (next_token->type != CPP_NAME
26516 && next_token->type != CPP_SCOPE
26517 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26518 && next_token->type != CPP_TEMPLATE_ID)
26519 return false;
26521 /* Parse tentatively; we are going to roll back all of the tokens
26522 consumed here. */
26523 cp_parser_parse_tentatively (parser);
26524 /* Assume that we are looking at a constructor declarator. */
26525 constructor_p = true;
26527 /* Look for the optional `::' operator. */
26528 cp_parser_global_scope_opt (parser,
26529 /*current_scope_valid_p=*/false);
26530 /* Look for the nested-name-specifier. */
26531 nested_name_specifier
26532 = (cp_parser_nested_name_specifier_opt (parser,
26533 /*typename_keyword_p=*/false,
26534 /*check_dependency_p=*/false,
26535 /*type_p=*/false,
26536 /*is_declaration=*/false));
26538 outside_class_specifier_p = (!at_class_scope_p ()
26539 || !TYPE_BEING_DEFINED (current_class_type)
26540 || friend_p);
26542 /* Outside of a class-specifier, there must be a
26543 nested-name-specifier. Except in C++17 mode, where we
26544 might be declaring a guiding declaration. */
26545 if (!nested_name_specifier && outside_class_specifier_p
26546 && cxx_dialect < cxx17)
26547 constructor_p = false;
26548 else if (nested_name_specifier == error_mark_node)
26549 constructor_p = false;
26551 /* If we have a class scope, this is easy; DR 147 says that S::S always
26552 names the constructor, and no other qualified name could. */
26553 if (constructor_p && nested_name_specifier
26554 && CLASS_TYPE_P (nested_name_specifier))
26556 tree id = cp_parser_unqualified_id (parser,
26557 /*template_keyword_p=*/false,
26558 /*check_dependency_p=*/false,
26559 /*declarator_p=*/true,
26560 /*optional_p=*/false);
26561 if (is_overloaded_fn (id))
26562 id = DECL_NAME (get_first_fn (id));
26563 if (!constructor_name_p (id, nested_name_specifier))
26564 constructor_p = false;
26566 /* If we still think that this might be a constructor-declarator,
26567 look for a class-name. */
26568 else if (constructor_p)
26570 /* If we have:
26572 template <typename T> struct S {
26573 S();
26576 we must recognize that the nested `S' names a class. */
26577 if (cxx_dialect >= cxx17)
26578 cp_parser_parse_tentatively (parser);
26580 tree type_decl;
26581 type_decl = cp_parser_class_name (parser,
26582 /*typename_keyword_p=*/false,
26583 /*template_keyword_p=*/false,
26584 none_type,
26585 /*check_dependency_p=*/false,
26586 /*class_head_p=*/false,
26587 /*is_declaration=*/false);
26589 if (cxx_dialect >= cxx17
26590 && !cp_parser_parse_definitely (parser))
26592 type_decl = NULL_TREE;
26593 tree tmpl = cp_parser_template_name (parser,
26594 /*template_keyword*/false,
26595 /*check_dependency_p*/false,
26596 /*is_declaration*/false,
26597 none_type,
26598 /*is_identifier*/NULL);
26599 if (DECL_CLASS_TEMPLATE_P (tmpl)
26600 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26601 /* It's a deduction guide, return true. */;
26602 else
26603 cp_parser_simulate_error (parser);
26606 /* If there was no class-name, then this is not a constructor.
26607 Otherwise, if we are in a class-specifier and we aren't
26608 handling a friend declaration, check that its type matches
26609 current_class_type (c++/38313). Note: error_mark_node
26610 is left alone for error recovery purposes. */
26611 constructor_p = (!cp_parser_error_occurred (parser)
26612 && (outside_class_specifier_p
26613 || type_decl == NULL_TREE
26614 || type_decl == error_mark_node
26615 || same_type_p (current_class_type,
26616 TREE_TYPE (type_decl))));
26618 /* If we're still considering a constructor, we have to see a `(',
26619 to begin the parameter-declaration-clause, followed by either a
26620 `)', an `...', or a decl-specifier. We need to check for a
26621 type-specifier to avoid being fooled into thinking that:
26623 S (f) (int);
26625 is a constructor. (It is actually a function named `f' that
26626 takes one parameter (of type `int') and returns a value of type
26627 `S'. */
26628 if (constructor_p
26629 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26630 constructor_p = false;
26632 if (constructor_p
26633 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26634 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26635 /* A parameter declaration begins with a decl-specifier,
26636 which is either the "attribute" keyword, a storage class
26637 specifier, or (usually) a type-specifier. */
26638 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26640 tree type;
26641 tree pushed_scope = NULL_TREE;
26642 unsigned saved_num_template_parameter_lists;
26644 /* Names appearing in the type-specifier should be looked up
26645 in the scope of the class. */
26646 if (current_class_type)
26647 type = NULL_TREE;
26648 else if (type_decl)
26650 type = TREE_TYPE (type_decl);
26651 if (TREE_CODE (type) == TYPENAME_TYPE)
26653 type = resolve_typename_type (type,
26654 /*only_current_p=*/false);
26655 if (TREE_CODE (type) == TYPENAME_TYPE)
26657 cp_parser_abort_tentative_parse (parser);
26658 return false;
26661 pushed_scope = push_scope (type);
26664 /* Inside the constructor parameter list, surrounding
26665 template-parameter-lists do not apply. */
26666 saved_num_template_parameter_lists
26667 = parser->num_template_parameter_lists;
26668 parser->num_template_parameter_lists = 0;
26670 /* Look for the type-specifier. */
26671 cp_parser_type_specifier (parser,
26672 CP_PARSER_FLAGS_NONE,
26673 /*decl_specs=*/NULL,
26674 /*is_declarator=*/true,
26675 /*declares_class_or_enum=*/NULL,
26676 /*is_cv_qualifier=*/NULL);
26678 parser->num_template_parameter_lists
26679 = saved_num_template_parameter_lists;
26681 /* Leave the scope of the class. */
26682 if (pushed_scope)
26683 pop_scope (pushed_scope);
26685 constructor_p = !cp_parser_error_occurred (parser);
26689 /* We did not really want to consume any tokens. */
26690 cp_parser_abort_tentative_parse (parser);
26692 return constructor_p;
26695 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26696 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26697 they must be performed once we are in the scope of the function.
26699 Returns the function defined. */
26701 static tree
26702 cp_parser_function_definition_from_specifiers_and_declarator
26703 (cp_parser* parser,
26704 cp_decl_specifier_seq *decl_specifiers,
26705 tree attributes,
26706 const cp_declarator *declarator)
26708 tree fn;
26709 bool success_p;
26711 /* Begin the function-definition. */
26712 success_p = start_function (decl_specifiers, declarator, attributes);
26714 /* The things we're about to see are not directly qualified by any
26715 template headers we've seen thus far. */
26716 reset_specialization ();
26718 /* If there were names looked up in the decl-specifier-seq that we
26719 did not check, check them now. We must wait until we are in the
26720 scope of the function to perform the checks, since the function
26721 might be a friend. */
26722 perform_deferred_access_checks (tf_warning_or_error);
26724 if (success_p)
26726 cp_finalize_omp_declare_simd (parser, current_function_decl);
26727 parser->omp_declare_simd = NULL;
26728 cp_finalize_oacc_routine (parser, current_function_decl, true);
26729 parser->oacc_routine = NULL;
26732 if (!success_p)
26734 /* Skip the entire function. */
26735 cp_parser_skip_to_end_of_block_or_statement (parser);
26736 fn = error_mark_node;
26738 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26740 /* Seen already, skip it. An error message has already been output. */
26741 cp_parser_skip_to_end_of_block_or_statement (parser);
26742 fn = current_function_decl;
26743 current_function_decl = NULL_TREE;
26744 /* If this is a function from a class, pop the nested class. */
26745 if (current_class_name)
26746 pop_nested_class ();
26748 else
26750 timevar_id_t tv;
26751 if (DECL_DECLARED_INLINE_P (current_function_decl))
26752 tv = TV_PARSE_INLINE;
26753 else
26754 tv = TV_PARSE_FUNC;
26755 timevar_push (tv);
26756 fn = cp_parser_function_definition_after_declarator (parser,
26757 /*inline_p=*/false);
26758 timevar_pop (tv);
26761 return fn;
26764 /* Parse the part of a function-definition that follows the
26765 declarator. INLINE_P is TRUE iff this function is an inline
26766 function defined within a class-specifier.
26768 Returns the function defined. */
26770 static tree
26771 cp_parser_function_definition_after_declarator (cp_parser* parser,
26772 bool inline_p)
26774 tree fn;
26775 bool saved_in_unbraced_linkage_specification_p;
26776 bool saved_in_function_body;
26777 unsigned saved_num_template_parameter_lists;
26778 cp_token *token;
26779 bool fully_implicit_function_template_p
26780 = parser->fully_implicit_function_template_p;
26781 parser->fully_implicit_function_template_p = false;
26782 tree implicit_template_parms
26783 = parser->implicit_template_parms;
26784 parser->implicit_template_parms = 0;
26785 cp_binding_level* implicit_template_scope
26786 = parser->implicit_template_scope;
26787 parser->implicit_template_scope = 0;
26789 saved_in_function_body = parser->in_function_body;
26790 parser->in_function_body = true;
26791 /* If the next token is `return', then the code may be trying to
26792 make use of the "named return value" extension that G++ used to
26793 support. */
26794 token = cp_lexer_peek_token (parser->lexer);
26795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26797 /* Consume the `return' keyword. */
26798 cp_lexer_consume_token (parser->lexer);
26799 /* Look for the identifier that indicates what value is to be
26800 returned. */
26801 cp_parser_identifier (parser);
26802 /* Issue an error message. */
26803 error_at (token->location,
26804 "named return values are no longer supported");
26805 /* Skip tokens until we reach the start of the function body. */
26806 while (true)
26808 cp_token *token = cp_lexer_peek_token (parser->lexer);
26809 if (token->type == CPP_OPEN_BRACE
26810 || token->type == CPP_EOF
26811 || token->type == CPP_PRAGMA_EOL)
26812 break;
26813 cp_lexer_consume_token (parser->lexer);
26816 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26817 anything declared inside `f'. */
26818 saved_in_unbraced_linkage_specification_p
26819 = parser->in_unbraced_linkage_specification_p;
26820 parser->in_unbraced_linkage_specification_p = false;
26821 /* Inside the function, surrounding template-parameter-lists do not
26822 apply. */
26823 saved_num_template_parameter_lists
26824 = parser->num_template_parameter_lists;
26825 parser->num_template_parameter_lists = 0;
26827 /* If the next token is `try', `__transaction_atomic', or
26828 `__transaction_relaxed`, then we are looking at either function-try-block
26829 or function-transaction-block. Note that all of these include the
26830 function-body. */
26831 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26832 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26833 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26834 RID_TRANSACTION_RELAXED))
26835 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26836 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26837 cp_parser_function_try_block (parser);
26838 else
26839 cp_parser_ctor_initializer_opt_and_function_body
26840 (parser, /*in_function_try_block=*/false);
26842 /* Finish the function. */
26843 fn = finish_function (inline_p);
26844 /* Generate code for it, if necessary. */
26845 expand_or_defer_fn (fn);
26846 /* Restore the saved values. */
26847 parser->in_unbraced_linkage_specification_p
26848 = saved_in_unbraced_linkage_specification_p;
26849 parser->num_template_parameter_lists
26850 = saved_num_template_parameter_lists;
26851 parser->in_function_body = saved_in_function_body;
26853 parser->fully_implicit_function_template_p
26854 = fully_implicit_function_template_p;
26855 parser->implicit_template_parms
26856 = implicit_template_parms;
26857 parser->implicit_template_scope
26858 = implicit_template_scope;
26860 if (parser->fully_implicit_function_template_p)
26861 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26863 return fn;
26866 /* Parse a template-declaration body (following argument list). */
26868 static void
26869 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26870 tree parameter_list,
26871 bool member_p)
26873 tree decl = NULL_TREE;
26874 bool friend_p = false;
26876 /* We just processed one more parameter list. */
26877 ++parser->num_template_parameter_lists;
26879 /* Get the deferred access checks from the parameter list. These
26880 will be checked once we know what is being declared, as for a
26881 member template the checks must be performed in the scope of the
26882 class containing the member. */
26883 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26885 /* Tentatively parse for a new template parameter list, which can either be
26886 the template keyword or a template introduction. */
26887 if (cp_parser_template_declaration_after_export (parser, member_p))
26888 /* OK */;
26889 else if (cxx_dialect >= cxx11
26890 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26891 decl = cp_parser_alias_declaration (parser);
26892 else
26894 /* There are no access checks when parsing a template, as we do not
26895 know if a specialization will be a friend. */
26896 push_deferring_access_checks (dk_no_check);
26897 cp_token *token = cp_lexer_peek_token (parser->lexer);
26898 decl = cp_parser_single_declaration (parser,
26899 checks,
26900 member_p,
26901 /*explicit_specialization_p=*/false,
26902 &friend_p);
26903 pop_deferring_access_checks ();
26905 /* If this is a member template declaration, let the front
26906 end know. */
26907 if (member_p && !friend_p && decl)
26909 if (TREE_CODE (decl) == TYPE_DECL)
26910 cp_parser_check_access_in_redeclaration (decl, token->location);
26912 decl = finish_member_template_decl (decl);
26914 else if (friend_p && decl
26915 && DECL_DECLARES_TYPE_P (decl))
26916 make_friend_class (current_class_type, TREE_TYPE (decl),
26917 /*complain=*/true);
26919 /* We are done with the current parameter list. */
26920 --parser->num_template_parameter_lists;
26922 pop_deferring_access_checks ();
26924 /* Finish up. */
26925 finish_template_decl (parameter_list);
26927 /* Check the template arguments for a literal operator template. */
26928 if (decl
26929 && DECL_DECLARES_FUNCTION_P (decl)
26930 && UDLIT_OPER_P (DECL_NAME (decl)))
26932 bool ok = true;
26933 if (parameter_list == NULL_TREE)
26934 ok = false;
26935 else
26937 int num_parms = TREE_VEC_LENGTH (parameter_list);
26938 if (num_parms == 1)
26940 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26941 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26942 if (TREE_TYPE (parm) != char_type_node
26943 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26944 ok = false;
26946 else if (num_parms == 2 && cxx_dialect >= cxx14)
26948 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26949 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26950 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26951 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26952 if (parm == error_mark_node
26953 || TREE_TYPE (parm) != TREE_TYPE (type)
26954 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26955 ok = false;
26957 else
26958 ok = false;
26960 if (!ok)
26962 if (cxx_dialect >= cxx14)
26963 error ("literal operator template %qD has invalid parameter list."
26964 " Expected non-type template argument pack <char...>"
26965 " or <typename CharT, CharT...>",
26966 decl);
26967 else
26968 error ("literal operator template %qD has invalid parameter list."
26969 " Expected non-type template argument pack <char...>",
26970 decl);
26974 /* Register member declarations. */
26975 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26976 finish_member_declaration (decl);
26977 /* If DECL is a function template, we must return to parse it later.
26978 (Even though there is no definition, there might be default
26979 arguments that need handling.) */
26980 if (member_p && decl
26981 && DECL_DECLARES_FUNCTION_P (decl))
26982 vec_safe_push (unparsed_funs_with_definitions, decl);
26985 /* Parse a template introduction header for a template-declaration. Returns
26986 false if tentative parse fails. */
26988 static bool
26989 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26991 cp_parser_parse_tentatively (parser);
26993 tree saved_scope = parser->scope;
26994 tree saved_object_scope = parser->object_scope;
26995 tree saved_qualifying_scope = parser->qualifying_scope;
26997 /* Look for the optional `::' operator. */
26998 cp_parser_global_scope_opt (parser,
26999 /*current_scope_valid_p=*/false);
27000 /* Look for the nested-name-specifier. */
27001 cp_parser_nested_name_specifier_opt (parser,
27002 /*typename_keyword_p=*/false,
27003 /*check_dependency_p=*/true,
27004 /*type_p=*/false,
27005 /*is_declaration=*/false);
27007 cp_token *token = cp_lexer_peek_token (parser->lexer);
27008 tree concept_name = cp_parser_identifier (parser);
27010 /* Look up the concept for which we will be matching
27011 template parameters. */
27012 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27013 token->location);
27014 parser->scope = saved_scope;
27015 parser->object_scope = saved_object_scope;
27016 parser->qualifying_scope = saved_qualifying_scope;
27018 if (concept_name == error_mark_node)
27019 cp_parser_simulate_error (parser);
27021 /* Look for opening brace for introduction. */
27022 matching_braces braces;
27023 braces.require_open (parser);
27025 if (!cp_parser_parse_definitely (parser))
27026 return false;
27028 push_deferring_access_checks (dk_deferred);
27030 /* Build vector of placeholder parameters and grab
27031 matching identifiers. */
27032 tree introduction_list = cp_parser_introduction_list (parser);
27034 /* The introduction-list shall not be empty. */
27035 int nargs = TREE_VEC_LENGTH (introduction_list);
27036 if (nargs == 0)
27038 error ("empty introduction-list");
27039 return true;
27042 /* Look for closing brace for introduction. */
27043 if (!braces.require_close (parser))
27044 return true;
27046 if (tmpl_decl == error_mark_node)
27048 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27049 token->location);
27050 return true;
27053 /* Build and associate the constraint. */
27054 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27055 if (parms && parms != error_mark_node)
27057 cp_parser_template_declaration_after_parameters (parser, parms,
27058 member_p);
27059 return true;
27062 error_at (token->location, "no matching concept for template-introduction");
27063 return true;
27066 /* Parse a normal template-declaration following the template keyword. */
27068 static void
27069 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27071 tree parameter_list;
27072 bool need_lang_pop;
27073 location_t location = input_location;
27075 /* Look for the `<' token. */
27076 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27077 return;
27078 if (at_class_scope_p () && current_function_decl)
27080 /* 14.5.2.2 [temp.mem]
27082 A local class shall not have member templates. */
27083 error_at (location,
27084 "invalid declaration of member template in local class");
27085 cp_parser_skip_to_end_of_block_or_statement (parser);
27086 return;
27088 /* [temp]
27090 A template ... shall not have C linkage. */
27091 if (current_lang_name == lang_name_c)
27093 error_at (location, "template with C linkage");
27094 maybe_show_extern_c_location ();
27095 /* Give it C++ linkage to avoid confusing other parts of the
27096 front end. */
27097 push_lang_context (lang_name_cplusplus);
27098 need_lang_pop = true;
27100 else
27101 need_lang_pop = false;
27103 /* We cannot perform access checks on the template parameter
27104 declarations until we know what is being declared, just as we
27105 cannot check the decl-specifier list. */
27106 push_deferring_access_checks (dk_deferred);
27108 /* If the next token is `>', then we have an invalid
27109 specialization. Rather than complain about an invalid template
27110 parameter, issue an error message here. */
27111 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27113 cp_parser_error (parser, "invalid explicit specialization");
27114 begin_specialization ();
27115 parameter_list = NULL_TREE;
27117 else
27119 /* Parse the template parameters. */
27120 parameter_list = cp_parser_template_parameter_list (parser);
27123 /* Look for the `>'. */
27124 cp_parser_skip_to_end_of_template_parameter_list (parser);
27126 /* Manage template requirements */
27127 if (flag_concepts)
27129 tree reqs = get_shorthand_constraints (current_template_parms);
27130 if (tree r = cp_parser_requires_clause_opt (parser))
27131 reqs = conjoin_constraints (reqs, normalize_expression (r));
27132 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27135 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27136 member_p);
27138 /* For the erroneous case of a template with C linkage, we pushed an
27139 implicit C++ linkage scope; exit that scope now. */
27140 if (need_lang_pop)
27141 pop_lang_context ();
27144 /* Parse a template-declaration, assuming that the `export' (and
27145 `extern') keywords, if present, has already been scanned. MEMBER_P
27146 is as for cp_parser_template_declaration. */
27148 static bool
27149 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27153 cp_lexer_consume_token (parser->lexer);
27154 cp_parser_explicit_template_declaration (parser, member_p);
27155 return true;
27157 else if (flag_concepts)
27158 return cp_parser_template_introduction (parser, member_p);
27160 return false;
27163 /* Perform the deferred access checks from a template-parameter-list.
27164 CHECKS is a TREE_LIST of access checks, as returned by
27165 get_deferred_access_checks. */
27167 static void
27168 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27170 ++processing_template_parmlist;
27171 perform_access_checks (checks, tf_warning_or_error);
27172 --processing_template_parmlist;
27175 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27176 `function-definition' sequence that follows a template header.
27177 If MEMBER_P is true, this declaration appears in a class scope.
27179 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27180 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27182 static tree
27183 cp_parser_single_declaration (cp_parser* parser,
27184 vec<deferred_access_check, va_gc> *checks,
27185 bool member_p,
27186 bool explicit_specialization_p,
27187 bool* friend_p)
27189 int declares_class_or_enum;
27190 tree decl = NULL_TREE;
27191 cp_decl_specifier_seq decl_specifiers;
27192 bool function_definition_p = false;
27193 cp_token *decl_spec_token_start;
27195 /* This function is only used when processing a template
27196 declaration. */
27197 gcc_assert (innermost_scope_kind () == sk_template_parms
27198 || innermost_scope_kind () == sk_template_spec);
27200 /* Defer access checks until we know what is being declared. */
27201 push_deferring_access_checks (dk_deferred);
27203 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27204 alternative. */
27205 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27206 cp_parser_decl_specifier_seq (parser,
27207 CP_PARSER_FLAGS_OPTIONAL,
27208 &decl_specifiers,
27209 &declares_class_or_enum);
27210 if (friend_p)
27211 *friend_p = cp_parser_friend_p (&decl_specifiers);
27213 /* There are no template typedefs. */
27214 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27216 error_at (decl_spec_token_start->location,
27217 "template declaration of %<typedef%>");
27218 decl = error_mark_node;
27221 /* Gather up the access checks that occurred the
27222 decl-specifier-seq. */
27223 stop_deferring_access_checks ();
27225 /* Check for the declaration of a template class. */
27226 if (declares_class_or_enum)
27228 if (cp_parser_declares_only_class_p (parser)
27229 || (declares_class_or_enum & 2))
27231 // If this is a declaration, but not a definition, associate
27232 // any constraints with the type declaration. Constraints
27233 // are associated with definitions in cp_parser_class_specifier.
27234 if (declares_class_or_enum == 1)
27235 associate_classtype_constraints (decl_specifiers.type);
27237 decl = shadow_tag (&decl_specifiers);
27239 /* In this case:
27241 struct C {
27242 friend template <typename T> struct A<T>::B;
27245 A<T>::B will be represented by a TYPENAME_TYPE, and
27246 therefore not recognized by shadow_tag. */
27247 if (friend_p && *friend_p
27248 && !decl
27249 && decl_specifiers.type
27250 && TYPE_P (decl_specifiers.type))
27251 decl = decl_specifiers.type;
27253 if (decl && decl != error_mark_node)
27254 decl = TYPE_NAME (decl);
27255 else
27256 decl = error_mark_node;
27258 /* Perform access checks for template parameters. */
27259 cp_parser_perform_template_parameter_access_checks (checks);
27261 /* Give a helpful diagnostic for
27262 template <class T> struct A { } a;
27263 if we aren't already recovering from an error. */
27264 if (!cp_parser_declares_only_class_p (parser)
27265 && !seen_error ())
27267 error_at (cp_lexer_peek_token (parser->lexer)->location,
27268 "a class template declaration must not declare "
27269 "anything else");
27270 cp_parser_skip_to_end_of_block_or_statement (parser);
27271 goto out;
27276 /* Complain about missing 'typename' or other invalid type names. */
27277 if (!decl_specifiers.any_type_specifiers_p
27278 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27280 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27281 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27282 the rest of this declaration. */
27283 decl = error_mark_node;
27284 goto out;
27287 /* If it's not a template class, try for a template function. If
27288 the next token is a `;', then this declaration does not declare
27289 anything. But, if there were errors in the decl-specifiers, then
27290 the error might well have come from an attempted class-specifier.
27291 In that case, there's no need to warn about a missing declarator. */
27292 if (!decl
27293 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27294 || decl_specifiers.type != error_mark_node))
27296 decl = cp_parser_init_declarator (parser,
27297 &decl_specifiers,
27298 checks,
27299 /*function_definition_allowed_p=*/true,
27300 member_p,
27301 declares_class_or_enum,
27302 &function_definition_p,
27303 NULL, NULL, NULL);
27305 /* 7.1.1-1 [dcl.stc]
27307 A storage-class-specifier shall not be specified in an explicit
27308 specialization... */
27309 if (decl
27310 && explicit_specialization_p
27311 && decl_specifiers.storage_class != sc_none)
27313 error_at (decl_spec_token_start->location,
27314 "explicit template specialization cannot have a storage class");
27315 decl = error_mark_node;
27318 if (decl && VAR_P (decl))
27319 check_template_variable (decl);
27322 /* Look for a trailing `;' after the declaration. */
27323 if (!function_definition_p
27324 && (decl == error_mark_node
27325 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27326 cp_parser_skip_to_end_of_block_or_statement (parser);
27328 out:
27329 pop_deferring_access_checks ();
27331 /* Clear any current qualification; whatever comes next is the start
27332 of something new. */
27333 parser->scope = NULL_TREE;
27334 parser->qualifying_scope = NULL_TREE;
27335 parser->object_scope = NULL_TREE;
27337 return decl;
27340 /* Parse a cast-expression that is not the operand of a unary "&". */
27342 static cp_expr
27343 cp_parser_simple_cast_expression (cp_parser *parser)
27345 return cp_parser_cast_expression (parser, /*address_p=*/false,
27346 /*cast_p=*/false, /*decltype*/false, NULL);
27349 /* Parse a functional cast to TYPE. Returns an expression
27350 representing the cast. */
27352 static cp_expr
27353 cp_parser_functional_cast (cp_parser* parser, tree type)
27355 vec<tree, va_gc> *vec;
27356 tree expression_list;
27357 cp_expr cast;
27358 bool nonconst_p;
27360 location_t start_loc = input_location;
27362 if (!type)
27363 type = error_mark_node;
27365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27367 cp_lexer_set_source_position (parser->lexer);
27368 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27369 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27370 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27371 if (TREE_CODE (type) == TYPE_DECL)
27372 type = TREE_TYPE (type);
27374 cast = finish_compound_literal (type, expression_list,
27375 tf_warning_or_error, fcl_functional);
27376 /* Create a location of the form:
27377 type_name{i, f}
27378 ^~~~~~~~~~~~~~~
27379 with caret == start at the start of the type name,
27380 finishing at the closing brace. */
27381 location_t finish_loc
27382 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27383 location_t combined_loc = make_location (start_loc, start_loc,
27384 finish_loc);
27385 cast.set_location (combined_loc);
27386 return cast;
27390 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27391 /*cast_p=*/true,
27392 /*allow_expansion_p=*/true,
27393 /*non_constant_p=*/NULL);
27394 if (vec == NULL)
27395 expression_list = error_mark_node;
27396 else
27398 expression_list = build_tree_list_vec (vec);
27399 release_tree_vector (vec);
27402 cast = build_functional_cast (type, expression_list,
27403 tf_warning_or_error);
27404 /* [expr.const]/1: In an integral constant expression "only type
27405 conversions to integral or enumeration type can be used". */
27406 if (TREE_CODE (type) == TYPE_DECL)
27407 type = TREE_TYPE (type);
27408 if (cast != error_mark_node
27409 && !cast_valid_in_integral_constant_expression_p (type)
27410 && cp_parser_non_integral_constant_expression (parser,
27411 NIC_CONSTRUCTOR))
27412 return error_mark_node;
27414 /* Create a location of the form:
27415 float(i)
27416 ^~~~~~~~
27417 with caret == start at the start of the type name,
27418 finishing at the closing paren. */
27419 location_t finish_loc
27420 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27421 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27422 cast.set_location (combined_loc);
27423 return cast;
27426 /* Save the tokens that make up the body of a member function defined
27427 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27428 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27429 specifiers applied to the declaration. Returns the FUNCTION_DECL
27430 for the member function. */
27432 static tree
27433 cp_parser_save_member_function_body (cp_parser* parser,
27434 cp_decl_specifier_seq *decl_specifiers,
27435 cp_declarator *declarator,
27436 tree attributes)
27438 cp_token *first;
27439 cp_token *last;
27440 tree fn;
27441 bool function_try_block = false;
27443 /* Create the FUNCTION_DECL. */
27444 fn = grokmethod (decl_specifiers, declarator, attributes);
27445 cp_finalize_omp_declare_simd (parser, fn);
27446 cp_finalize_oacc_routine (parser, fn, true);
27447 /* If something went badly wrong, bail out now. */
27448 if (fn == error_mark_node)
27450 /* If there's a function-body, skip it. */
27451 if (cp_parser_token_starts_function_definition_p
27452 (cp_lexer_peek_token (parser->lexer)))
27453 cp_parser_skip_to_end_of_block_or_statement (parser);
27454 return error_mark_node;
27457 /* Remember it, if there default args to post process. */
27458 cp_parser_save_default_args (parser, fn);
27460 /* Save away the tokens that make up the body of the
27461 function. */
27462 first = parser->lexer->next_token;
27464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27465 cp_lexer_consume_token (parser->lexer);
27466 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27467 RID_TRANSACTION_ATOMIC))
27469 cp_lexer_consume_token (parser->lexer);
27470 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27472 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27473 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27474 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27475 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27476 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27478 cp_lexer_consume_token (parser->lexer);
27479 cp_lexer_consume_token (parser->lexer);
27480 cp_lexer_consume_token (parser->lexer);
27481 cp_lexer_consume_token (parser->lexer);
27482 cp_lexer_consume_token (parser->lexer);
27484 else
27485 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27486 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27488 cp_lexer_consume_token (parser->lexer);
27489 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27490 break;
27494 /* Handle function try blocks. */
27495 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27497 cp_lexer_consume_token (parser->lexer);
27498 function_try_block = true;
27500 /* We can have braced-init-list mem-initializers before the fn body. */
27501 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27503 cp_lexer_consume_token (parser->lexer);
27504 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27506 /* cache_group will stop after an un-nested { } pair, too. */
27507 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27508 break;
27510 /* variadic mem-inits have ... after the ')'. */
27511 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27512 cp_lexer_consume_token (parser->lexer);
27515 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27516 /* Handle function try blocks. */
27517 if (function_try_block)
27518 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27519 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27520 last = parser->lexer->next_token;
27522 /* Save away the inline definition; we will process it when the
27523 class is complete. */
27524 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27525 DECL_PENDING_INLINE_P (fn) = 1;
27527 /* We need to know that this was defined in the class, so that
27528 friend templates are handled correctly. */
27529 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27531 /* Add FN to the queue of functions to be parsed later. */
27532 vec_safe_push (unparsed_funs_with_definitions, fn);
27534 return fn;
27537 /* Save the tokens that make up the in-class initializer for a non-static
27538 data member. Returns a DEFAULT_ARG. */
27540 static tree
27541 cp_parser_save_nsdmi (cp_parser* parser)
27543 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27546 /* Parse a template-argument-list, as well as the trailing ">" (but
27547 not the opening "<"). See cp_parser_template_argument_list for the
27548 return value. */
27550 static tree
27551 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27553 tree arguments;
27554 tree saved_scope;
27555 tree saved_qualifying_scope;
27556 tree saved_object_scope;
27557 bool saved_greater_than_is_operator_p;
27558 int saved_unevaluated_operand;
27559 int saved_inhibit_evaluation_warnings;
27561 /* [temp.names]
27563 When parsing a template-id, the first non-nested `>' is taken as
27564 the end of the template-argument-list rather than a greater-than
27565 operator. */
27566 saved_greater_than_is_operator_p
27567 = parser->greater_than_is_operator_p;
27568 parser->greater_than_is_operator_p = false;
27569 /* Parsing the argument list may modify SCOPE, so we save it
27570 here. */
27571 saved_scope = parser->scope;
27572 saved_qualifying_scope = parser->qualifying_scope;
27573 saved_object_scope = parser->object_scope;
27574 /* We need to evaluate the template arguments, even though this
27575 template-id may be nested within a "sizeof". */
27576 saved_unevaluated_operand = cp_unevaluated_operand;
27577 cp_unevaluated_operand = 0;
27578 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27579 c_inhibit_evaluation_warnings = 0;
27580 /* Parse the template-argument-list itself. */
27581 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27582 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27583 arguments = NULL_TREE;
27584 else
27585 arguments = cp_parser_template_argument_list (parser);
27586 /* Look for the `>' that ends the template-argument-list. If we find
27587 a '>>' instead, it's probably just a typo. */
27588 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27590 if (cxx_dialect != cxx98)
27592 /* In C++0x, a `>>' in a template argument list or cast
27593 expression is considered to be two separate `>'
27594 tokens. So, change the current token to a `>', but don't
27595 consume it: it will be consumed later when the outer
27596 template argument list (or cast expression) is parsed.
27597 Note that this replacement of `>' for `>>' is necessary
27598 even if we are parsing tentatively: in the tentative
27599 case, after calling
27600 cp_parser_enclosed_template_argument_list we will always
27601 throw away all of the template arguments and the first
27602 closing `>', either because the template argument list
27603 was erroneous or because we are replacing those tokens
27604 with a CPP_TEMPLATE_ID token. The second `>' (which will
27605 not have been thrown away) is needed either to close an
27606 outer template argument list or to complete a new-style
27607 cast. */
27608 cp_token *token = cp_lexer_peek_token (parser->lexer);
27609 token->type = CPP_GREATER;
27611 else if (!saved_greater_than_is_operator_p)
27613 /* If we're in a nested template argument list, the '>>' has
27614 to be a typo for '> >'. We emit the error message, but we
27615 continue parsing and we push a '>' as next token, so that
27616 the argument list will be parsed correctly. Note that the
27617 global source location is still on the token before the
27618 '>>', so we need to say explicitly where we want it. */
27619 cp_token *token = cp_lexer_peek_token (parser->lexer);
27620 gcc_rich_location richloc (token->location);
27621 richloc.add_fixit_replace ("> >");
27622 error_at (&richloc, "%<>>%> should be %<> >%> "
27623 "within a nested template argument list");
27625 token->type = CPP_GREATER;
27627 else
27629 /* If this is not a nested template argument list, the '>>'
27630 is a typo for '>'. Emit an error message and continue.
27631 Same deal about the token location, but here we can get it
27632 right by consuming the '>>' before issuing the diagnostic. */
27633 cp_token *token = cp_lexer_consume_token (parser->lexer);
27634 error_at (token->location,
27635 "spurious %<>>%>, use %<>%> to terminate "
27636 "a template argument list");
27639 else
27640 cp_parser_skip_to_end_of_template_parameter_list (parser);
27641 /* The `>' token might be a greater-than operator again now. */
27642 parser->greater_than_is_operator_p
27643 = saved_greater_than_is_operator_p;
27644 /* Restore the SAVED_SCOPE. */
27645 parser->scope = saved_scope;
27646 parser->qualifying_scope = saved_qualifying_scope;
27647 parser->object_scope = saved_object_scope;
27648 cp_unevaluated_operand = saved_unevaluated_operand;
27649 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27651 return arguments;
27654 /* MEMBER_FUNCTION is a member function, or a friend. If default
27655 arguments, or the body of the function have not yet been parsed,
27656 parse them now. */
27658 static void
27659 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27661 timevar_push (TV_PARSE_INMETH);
27662 /* If this member is a template, get the underlying
27663 FUNCTION_DECL. */
27664 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27665 member_function = DECL_TEMPLATE_RESULT (member_function);
27667 /* There should not be any class definitions in progress at this
27668 point; the bodies of members are only parsed outside of all class
27669 definitions. */
27670 gcc_assert (parser->num_classes_being_defined == 0);
27671 /* While we're parsing the member functions we might encounter more
27672 classes. We want to handle them right away, but we don't want
27673 them getting mixed up with functions that are currently in the
27674 queue. */
27675 push_unparsed_function_queues (parser);
27677 /* Make sure that any template parameters are in scope. */
27678 maybe_begin_member_template_processing (member_function);
27680 /* If the body of the function has not yet been parsed, parse it
27681 now. */
27682 if (DECL_PENDING_INLINE_P (member_function))
27684 tree function_scope;
27685 cp_token_cache *tokens;
27687 /* The function is no longer pending; we are processing it. */
27688 tokens = DECL_PENDING_INLINE_INFO (member_function);
27689 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27690 DECL_PENDING_INLINE_P (member_function) = 0;
27692 /* If this is a local class, enter the scope of the containing
27693 function. */
27694 function_scope = current_function_decl;
27695 if (function_scope)
27696 push_function_context ();
27698 /* Push the body of the function onto the lexer stack. */
27699 cp_parser_push_lexer_for_tokens (parser, tokens);
27701 /* Let the front end know that we going to be defining this
27702 function. */
27703 start_preparsed_function (member_function, NULL_TREE,
27704 SF_PRE_PARSED | SF_INCLASS_INLINE);
27706 /* Don't do access checking if it is a templated function. */
27707 if (processing_template_decl)
27708 push_deferring_access_checks (dk_no_check);
27710 /* #pragma omp declare reduction needs special parsing. */
27711 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27713 parser->lexer->in_pragma = true;
27714 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27715 finish_function (/*inline_p=*/true);
27716 cp_check_omp_declare_reduction (member_function);
27718 else
27719 /* Now, parse the body of the function. */
27720 cp_parser_function_definition_after_declarator (parser,
27721 /*inline_p=*/true);
27723 if (processing_template_decl)
27724 pop_deferring_access_checks ();
27726 /* Leave the scope of the containing function. */
27727 if (function_scope)
27728 pop_function_context ();
27729 cp_parser_pop_lexer (parser);
27732 /* Remove any template parameters from the symbol table. */
27733 maybe_end_member_template_processing ();
27735 /* Restore the queue. */
27736 pop_unparsed_function_queues (parser);
27737 timevar_pop (TV_PARSE_INMETH);
27740 /* If DECL contains any default args, remember it on the unparsed
27741 functions queue. */
27743 static void
27744 cp_parser_save_default_args (cp_parser* parser, tree decl)
27746 tree probe;
27748 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27749 probe;
27750 probe = TREE_CHAIN (probe))
27751 if (TREE_PURPOSE (probe))
27753 cp_default_arg_entry entry = {current_class_type, decl};
27754 vec_safe_push (unparsed_funs_with_default_args, entry);
27755 break;
27759 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27760 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27761 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27762 from the parameter-type-list. */
27764 static tree
27765 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27766 tree default_arg, tree parmtype)
27768 cp_token_cache *tokens;
27769 tree parsed_arg;
27770 bool dummy;
27772 if (default_arg == error_mark_node)
27773 return error_mark_node;
27775 /* Push the saved tokens for the default argument onto the parser's
27776 lexer stack. */
27777 tokens = DEFARG_TOKENS (default_arg);
27778 cp_parser_push_lexer_for_tokens (parser, tokens);
27780 start_lambda_scope (decl);
27782 /* Parse the default argument. */
27783 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27784 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27785 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27787 finish_lambda_scope ();
27789 if (parsed_arg == error_mark_node)
27790 cp_parser_skip_to_end_of_statement (parser);
27792 if (!processing_template_decl)
27794 /* In a non-template class, check conversions now. In a template,
27795 we'll wait and instantiate these as needed. */
27796 if (TREE_CODE (decl) == PARM_DECL)
27797 parsed_arg = check_default_argument (parmtype, parsed_arg,
27798 tf_warning_or_error);
27799 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27800 parsed_arg = error_mark_node;
27801 else
27802 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27805 /* If the token stream has not been completely used up, then
27806 there was extra junk after the end of the default
27807 argument. */
27808 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27810 if (TREE_CODE (decl) == PARM_DECL)
27811 cp_parser_error (parser, "expected %<,%>");
27812 else
27813 cp_parser_error (parser, "expected %<;%>");
27816 /* Revert to the main lexer. */
27817 cp_parser_pop_lexer (parser);
27819 return parsed_arg;
27822 /* FIELD is a non-static data member with an initializer which we saved for
27823 later; parse it now. */
27825 static void
27826 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27828 tree def;
27830 maybe_begin_member_template_processing (field);
27832 push_unparsed_function_queues (parser);
27833 def = cp_parser_late_parse_one_default_arg (parser, field,
27834 DECL_INITIAL (field),
27835 NULL_TREE);
27836 pop_unparsed_function_queues (parser);
27838 maybe_end_member_template_processing ();
27840 DECL_INITIAL (field) = def;
27843 /* FN is a FUNCTION_DECL which may contains a parameter with an
27844 unparsed DEFAULT_ARG. Parse the default args now. This function
27845 assumes that the current scope is the scope in which the default
27846 argument should be processed. */
27848 static void
27849 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27851 bool saved_local_variables_forbidden_p;
27852 tree parm, parmdecl;
27854 /* While we're parsing the default args, we might (due to the
27855 statement expression extension) encounter more classes. We want
27856 to handle them right away, but we don't want them getting mixed
27857 up with default args that are currently in the queue. */
27858 push_unparsed_function_queues (parser);
27860 /* Local variable names (and the `this' keyword) may not appear
27861 in a default argument. */
27862 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27863 parser->local_variables_forbidden_p = true;
27865 push_defarg_context (fn);
27867 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27868 parmdecl = DECL_ARGUMENTS (fn);
27869 parm && parm != void_list_node;
27870 parm = TREE_CHAIN (parm),
27871 parmdecl = DECL_CHAIN (parmdecl))
27873 tree default_arg = TREE_PURPOSE (parm);
27874 tree parsed_arg;
27875 vec<tree, va_gc> *insts;
27876 tree copy;
27877 unsigned ix;
27879 if (!default_arg)
27880 continue;
27882 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27883 /* This can happen for a friend declaration for a function
27884 already declared with default arguments. */
27885 continue;
27887 parsed_arg
27888 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27889 default_arg,
27890 TREE_VALUE (parm));
27891 TREE_PURPOSE (parm) = parsed_arg;
27893 /* Update any instantiations we've already created. */
27894 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27895 vec_safe_iterate (insts, ix, &copy); ix++)
27896 TREE_PURPOSE (copy) = parsed_arg;
27899 pop_defarg_context ();
27901 /* Make sure no default arg is missing. */
27902 check_default_args (fn);
27904 /* Restore the state of local_variables_forbidden_p. */
27905 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27907 /* Restore the queue. */
27908 pop_unparsed_function_queues (parser);
27911 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27913 sizeof ... ( identifier )
27915 where the 'sizeof' token has already been consumed. */
27917 static tree
27918 cp_parser_sizeof_pack (cp_parser *parser)
27920 /* Consume the `...'. */
27921 cp_lexer_consume_token (parser->lexer);
27922 maybe_warn_variadic_templates ();
27924 matching_parens parens;
27925 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27926 if (paren)
27927 parens.consume_open (parser);
27928 else
27929 permerror (cp_lexer_peek_token (parser->lexer)->location,
27930 "%<sizeof...%> argument must be surrounded by parentheses");
27932 cp_token *token = cp_lexer_peek_token (parser->lexer);
27933 tree name = cp_parser_identifier (parser);
27934 if (name == error_mark_node)
27935 return error_mark_node;
27936 /* The name is not qualified. */
27937 parser->scope = NULL_TREE;
27938 parser->qualifying_scope = NULL_TREE;
27939 parser->object_scope = NULL_TREE;
27940 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27941 if (expr == error_mark_node)
27942 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27943 token->location);
27944 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27945 expr = TREE_TYPE (expr);
27946 else if (TREE_CODE (expr) == CONST_DECL)
27947 expr = DECL_INITIAL (expr);
27948 expr = make_pack_expansion (expr);
27949 PACK_EXPANSION_SIZEOF_P (expr) = true;
27951 if (paren)
27952 parens.require_close (parser);
27954 return expr;
27957 /* Parse the operand of `sizeof' (or a similar operator). Returns
27958 either a TYPE or an expression, depending on the form of the
27959 input. The KEYWORD indicates which kind of expression we have
27960 encountered. */
27962 static tree
27963 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27965 tree expr = NULL_TREE;
27966 const char *saved_message;
27967 char *tmp;
27968 bool saved_integral_constant_expression_p;
27969 bool saved_non_integral_constant_expression_p;
27971 /* If it's a `...', then we are computing the length of a parameter
27972 pack. */
27973 if (keyword == RID_SIZEOF
27974 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27975 return cp_parser_sizeof_pack (parser);
27977 /* Types cannot be defined in a `sizeof' expression. Save away the
27978 old message. */
27979 saved_message = parser->type_definition_forbidden_message;
27980 /* And create the new one. */
27981 tmp = concat ("types may not be defined in %<",
27982 IDENTIFIER_POINTER (ridpointers[keyword]),
27983 "%> expressions", NULL);
27984 parser->type_definition_forbidden_message = tmp;
27986 /* The restrictions on constant-expressions do not apply inside
27987 sizeof expressions. */
27988 saved_integral_constant_expression_p
27989 = parser->integral_constant_expression_p;
27990 saved_non_integral_constant_expression_p
27991 = parser->non_integral_constant_expression_p;
27992 parser->integral_constant_expression_p = false;
27994 /* Do not actually evaluate the expression. */
27995 ++cp_unevaluated_operand;
27996 ++c_inhibit_evaluation_warnings;
27997 /* If it's a `(', then we might be looking at the type-id
27998 construction. */
27999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28001 tree type = NULL_TREE;
28003 /* We can't be sure yet whether we're looking at a type-id or an
28004 expression. */
28005 cp_parser_parse_tentatively (parser);
28007 matching_parens parens;
28008 parens.consume_open (parser);
28010 /* Note: as a GNU Extension, compound literals are considered
28011 postfix-expressions as they are in C99, so they are valid
28012 arguments to sizeof. See comment in cp_parser_cast_expression
28013 for details. */
28014 if (cp_parser_compound_literal_p (parser))
28015 cp_parser_simulate_error (parser);
28016 else
28018 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28019 parser->in_type_id_in_expr_p = true;
28020 /* Look for the type-id. */
28021 type = cp_parser_type_id (parser);
28022 /* Look for the closing `)'. */
28023 parens.require_close (parser);
28024 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28027 /* If all went well, then we're done. */
28028 if (cp_parser_parse_definitely (parser))
28030 cp_decl_specifier_seq decl_specs;
28032 /* Build a trivial decl-specifier-seq. */
28033 clear_decl_specs (&decl_specs);
28034 decl_specs.type = type;
28036 /* Call grokdeclarator to figure out what type this is. */
28037 expr = grokdeclarator (NULL,
28038 &decl_specs,
28039 TYPENAME,
28040 /*initialized=*/0,
28041 /*attrlist=*/NULL);
28045 /* If the type-id production did not work out, then we must be
28046 looking at the unary-expression production. */
28047 if (!expr)
28048 expr = cp_parser_unary_expression (parser);
28050 /* Go back to evaluating expressions. */
28051 --cp_unevaluated_operand;
28052 --c_inhibit_evaluation_warnings;
28054 /* Free the message we created. */
28055 free (tmp);
28056 /* And restore the old one. */
28057 parser->type_definition_forbidden_message = saved_message;
28058 parser->integral_constant_expression_p
28059 = saved_integral_constant_expression_p;
28060 parser->non_integral_constant_expression_p
28061 = saved_non_integral_constant_expression_p;
28063 return expr;
28066 /* If the current declaration has no declarator, return true. */
28068 static bool
28069 cp_parser_declares_only_class_p (cp_parser *parser)
28071 /* If the next token is a `;' or a `,' then there is no
28072 declarator. */
28073 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28074 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28077 /* Update the DECL_SPECS to reflect the storage class indicated by
28078 KEYWORD. */
28080 static void
28081 cp_parser_set_storage_class (cp_parser *parser,
28082 cp_decl_specifier_seq *decl_specs,
28083 enum rid keyword,
28084 cp_token *token)
28086 cp_storage_class storage_class;
28088 if (parser->in_unbraced_linkage_specification_p)
28090 error_at (token->location, "invalid use of %qD in linkage specification",
28091 ridpointers[keyword]);
28092 return;
28094 else if (decl_specs->storage_class != sc_none)
28096 decl_specs->conflicting_specifiers_p = true;
28097 return;
28100 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28101 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28102 && decl_specs->gnu_thread_keyword_p)
28104 pedwarn (decl_specs->locations[ds_thread], 0,
28105 "%<__thread%> before %qD", ridpointers[keyword]);
28108 switch (keyword)
28110 case RID_AUTO:
28111 storage_class = sc_auto;
28112 break;
28113 case RID_REGISTER:
28114 storage_class = sc_register;
28115 break;
28116 case RID_STATIC:
28117 storage_class = sc_static;
28118 break;
28119 case RID_EXTERN:
28120 storage_class = sc_extern;
28121 break;
28122 case RID_MUTABLE:
28123 storage_class = sc_mutable;
28124 break;
28125 default:
28126 gcc_unreachable ();
28128 decl_specs->storage_class = storage_class;
28129 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28131 /* A storage class specifier cannot be applied alongside a typedef
28132 specifier. If there is a typedef specifier present then set
28133 conflicting_specifiers_p which will trigger an error later
28134 on in grokdeclarator. */
28135 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28136 decl_specs->conflicting_specifiers_p = true;
28139 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28140 is true, the type is a class or enum definition. */
28142 static void
28143 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28144 tree type_spec,
28145 cp_token *token,
28146 bool type_definition_p)
28148 decl_specs->any_specifiers_p = true;
28150 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28151 (with, for example, in "typedef int wchar_t;") we remember that
28152 this is what happened. In system headers, we ignore these
28153 declarations so that G++ can work with system headers that are not
28154 C++-safe. */
28155 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28156 && !type_definition_p
28157 && (type_spec == boolean_type_node
28158 || type_spec == char16_type_node
28159 || type_spec == char32_type_node
28160 || type_spec == wchar_type_node)
28161 && (decl_specs->type
28162 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28163 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28164 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28165 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28167 decl_specs->redefined_builtin_type = type_spec;
28168 set_and_check_decl_spec_loc (decl_specs,
28169 ds_redefined_builtin_type_spec,
28170 token);
28171 if (!decl_specs->type)
28173 decl_specs->type = type_spec;
28174 decl_specs->type_definition_p = false;
28175 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28178 else if (decl_specs->type)
28179 decl_specs->multiple_types_p = true;
28180 else
28182 decl_specs->type = type_spec;
28183 decl_specs->type_definition_p = type_definition_p;
28184 decl_specs->redefined_builtin_type = NULL_TREE;
28185 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28189 /* True iff TOKEN is the GNU keyword __thread. */
28191 static bool
28192 token_is__thread (cp_token *token)
28194 gcc_assert (token->keyword == RID_THREAD);
28195 return id_equal (token->u.value, "__thread");
28198 /* Set the location for a declarator specifier and check if it is
28199 duplicated.
28201 DECL_SPECS is the sequence of declarator specifiers onto which to
28202 set the location.
28204 DS is the single declarator specifier to set which location is to
28205 be set onto the existing sequence of declarators.
28207 LOCATION is the location for the declarator specifier to
28208 consider. */
28210 static void
28211 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28212 cp_decl_spec ds, cp_token *token)
28214 gcc_assert (ds < ds_last);
28216 if (decl_specs == NULL)
28217 return;
28219 source_location location = token->location;
28221 if (decl_specs->locations[ds] == 0)
28223 decl_specs->locations[ds] = location;
28224 if (ds == ds_thread)
28225 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28227 else
28229 if (ds == ds_long)
28231 if (decl_specs->locations[ds_long_long] != 0)
28232 error_at (location,
28233 "%<long long long%> is too long for GCC");
28234 else
28236 decl_specs->locations[ds_long_long] = location;
28237 pedwarn_cxx98 (location,
28238 OPT_Wlong_long,
28239 "ISO C++ 1998 does not support %<long long%>");
28242 else if (ds == ds_thread)
28244 bool gnu = token_is__thread (token);
28245 if (gnu != decl_specs->gnu_thread_keyword_p)
28246 error_at (location,
28247 "both %<__thread%> and %<thread_local%> specified");
28248 else
28250 gcc_rich_location richloc (location);
28251 richloc.add_fixit_remove ();
28252 error_at (&richloc, "duplicate %qD", token->u.value);
28255 else
28257 static const char *const decl_spec_names[] = {
28258 "signed",
28259 "unsigned",
28260 "short",
28261 "long",
28262 "const",
28263 "volatile",
28264 "restrict",
28265 "inline",
28266 "virtual",
28267 "explicit",
28268 "friend",
28269 "typedef",
28270 "using",
28271 "constexpr",
28272 "__complex"
28274 gcc_rich_location richloc (location);
28275 richloc.add_fixit_remove ();
28276 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28281 /* Return true iff the declarator specifier DS is present in the
28282 sequence of declarator specifiers DECL_SPECS. */
28284 bool
28285 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28286 cp_decl_spec ds)
28288 gcc_assert (ds < ds_last);
28290 if (decl_specs == NULL)
28291 return false;
28293 return decl_specs->locations[ds] != 0;
28296 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28297 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28299 static bool
28300 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28302 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28305 /* Issue an error message indicating that TOKEN_DESC was expected.
28306 If KEYWORD is true, it indicated this function is called by
28307 cp_parser_require_keword and the required token can only be
28308 a indicated keyword.
28310 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28311 within any error as the location of an "opening" token matching
28312 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28313 RT_CLOSE_PAREN). */
28315 static void
28316 cp_parser_required_error (cp_parser *parser,
28317 required_token token_desc,
28318 bool keyword,
28319 location_t matching_location)
28321 if (cp_parser_simulate_error (parser))
28322 return;
28324 const char *gmsgid = NULL;
28325 switch (token_desc)
28327 case RT_NEW:
28328 gmsgid = G_("expected %<new%>");
28329 break;
28330 case RT_DELETE:
28331 gmsgid = G_("expected %<delete%>");
28332 break;
28333 case RT_RETURN:
28334 gmsgid = G_("expected %<return%>");
28335 break;
28336 case RT_WHILE:
28337 gmsgid = G_("expected %<while%>");
28338 break;
28339 case RT_EXTERN:
28340 gmsgid = G_("expected %<extern%>");
28341 break;
28342 case RT_STATIC_ASSERT:
28343 gmsgid = G_("expected %<static_assert%>");
28344 break;
28345 case RT_DECLTYPE:
28346 gmsgid = G_("expected %<decltype%>");
28347 break;
28348 case RT_OPERATOR:
28349 gmsgid = G_("expected %<operator%>");
28350 break;
28351 case RT_CLASS:
28352 gmsgid = G_("expected %<class%>");
28353 break;
28354 case RT_TEMPLATE:
28355 gmsgid = G_("expected %<template%>");
28356 break;
28357 case RT_NAMESPACE:
28358 gmsgid = G_("expected %<namespace%>");
28359 break;
28360 case RT_USING:
28361 gmsgid = G_("expected %<using%>");
28362 break;
28363 case RT_ASM:
28364 gmsgid = G_("expected %<asm%>");
28365 break;
28366 case RT_TRY:
28367 gmsgid = G_("expected %<try%>");
28368 break;
28369 case RT_CATCH:
28370 gmsgid = G_("expected %<catch%>");
28371 break;
28372 case RT_THROW:
28373 gmsgid = G_("expected %<throw%>");
28374 break;
28375 case RT_LABEL:
28376 gmsgid = G_("expected %<__label__%>");
28377 break;
28378 case RT_AT_TRY:
28379 gmsgid = G_("expected %<@try%>");
28380 break;
28381 case RT_AT_SYNCHRONIZED:
28382 gmsgid = G_("expected %<@synchronized%>");
28383 break;
28384 case RT_AT_THROW:
28385 gmsgid = G_("expected %<@throw%>");
28386 break;
28387 case RT_TRANSACTION_ATOMIC:
28388 gmsgid = G_("expected %<__transaction_atomic%>");
28389 break;
28390 case RT_TRANSACTION_RELAXED:
28391 gmsgid = G_("expected %<__transaction_relaxed%>");
28392 break;
28393 default:
28394 break;
28397 if (!gmsgid && !keyword)
28399 switch (token_desc)
28401 case RT_SEMICOLON:
28402 gmsgid = G_("expected %<;%>");
28403 break;
28404 case RT_OPEN_PAREN:
28405 gmsgid = G_("expected %<(%>");
28406 break;
28407 case RT_CLOSE_BRACE:
28408 gmsgid = G_("expected %<}%>");
28409 break;
28410 case RT_OPEN_BRACE:
28411 gmsgid = G_("expected %<{%>");
28412 break;
28413 case RT_CLOSE_SQUARE:
28414 gmsgid = G_("expected %<]%>");
28415 break;
28416 case RT_OPEN_SQUARE:
28417 gmsgid = G_("expected %<[%>");
28418 break;
28419 case RT_COMMA:
28420 gmsgid = G_("expected %<,%>");
28421 break;
28422 case RT_SCOPE:
28423 gmsgid = G_("expected %<::%>");
28424 break;
28425 case RT_LESS:
28426 gmsgid = G_("expected %<<%>");
28427 break;
28428 case RT_GREATER:
28429 gmsgid = G_("expected %<>%>");
28430 break;
28431 case RT_EQ:
28432 gmsgid = G_("expected %<=%>");
28433 break;
28434 case RT_ELLIPSIS:
28435 gmsgid = G_("expected %<...%>");
28436 break;
28437 case RT_MULT:
28438 gmsgid = G_("expected %<*%>");
28439 break;
28440 case RT_COMPL:
28441 gmsgid = G_("expected %<~%>");
28442 break;
28443 case RT_COLON:
28444 gmsgid = G_("expected %<:%>");
28445 break;
28446 case RT_COLON_SCOPE:
28447 gmsgid = G_("expected %<:%> or %<::%>");
28448 break;
28449 case RT_CLOSE_PAREN:
28450 gmsgid = G_("expected %<)%>");
28451 break;
28452 case RT_COMMA_CLOSE_PAREN:
28453 gmsgid = G_("expected %<,%> or %<)%>");
28454 break;
28455 case RT_PRAGMA_EOL:
28456 gmsgid = G_("expected end of line");
28457 break;
28458 case RT_NAME:
28459 gmsgid = G_("expected identifier");
28460 break;
28461 case RT_SELECT:
28462 gmsgid = G_("expected selection-statement");
28463 break;
28464 case RT_ITERATION:
28465 gmsgid = G_("expected iteration-statement");
28466 break;
28467 case RT_JUMP:
28468 gmsgid = G_("expected jump-statement");
28469 break;
28470 case RT_CLASS_KEY:
28471 gmsgid = G_("expected class-key");
28472 break;
28473 case RT_CLASS_TYPENAME_TEMPLATE:
28474 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28475 break;
28476 default:
28477 gcc_unreachable ();
28481 if (gmsgid)
28482 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28486 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28487 issue an error message indicating that TOKEN_DESC was expected.
28489 Returns the token consumed, if the token had the appropriate type.
28490 Otherwise, returns NULL.
28492 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28493 within any error as the location of an "opening" token matching
28494 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28495 RT_CLOSE_PAREN). */
28497 static cp_token *
28498 cp_parser_require (cp_parser* parser,
28499 enum cpp_ttype type,
28500 required_token token_desc,
28501 location_t matching_location)
28503 if (cp_lexer_next_token_is (parser->lexer, type))
28504 return cp_lexer_consume_token (parser->lexer);
28505 else
28507 /* Output the MESSAGE -- unless we're parsing tentatively. */
28508 if (!cp_parser_simulate_error (parser))
28509 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28510 matching_location);
28511 return NULL;
28515 /* An error message is produced if the next token is not '>'.
28516 All further tokens are skipped until the desired token is
28517 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28519 static void
28520 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28522 /* Current level of '< ... >'. */
28523 unsigned level = 0;
28524 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28525 unsigned nesting_depth = 0;
28527 /* Are we ready, yet? If not, issue error message. */
28528 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28529 return;
28531 /* Skip tokens until the desired token is found. */
28532 while (true)
28534 /* Peek at the next token. */
28535 switch (cp_lexer_peek_token (parser->lexer)->type)
28537 case CPP_LESS:
28538 if (!nesting_depth)
28539 ++level;
28540 break;
28542 case CPP_RSHIFT:
28543 if (cxx_dialect == cxx98)
28544 /* C++0x views the `>>' operator as two `>' tokens, but
28545 C++98 does not. */
28546 break;
28547 else if (!nesting_depth && level-- == 0)
28549 /* We've hit a `>>' where the first `>' closes the
28550 template argument list, and the second `>' is
28551 spurious. Just consume the `>>' and stop; we've
28552 already produced at least one error. */
28553 cp_lexer_consume_token (parser->lexer);
28554 return;
28556 /* Fall through for C++0x, so we handle the second `>' in
28557 the `>>'. */
28558 gcc_fallthrough ();
28560 case CPP_GREATER:
28561 if (!nesting_depth && level-- == 0)
28563 /* We've reached the token we want, consume it and stop. */
28564 cp_lexer_consume_token (parser->lexer);
28565 return;
28567 break;
28569 case CPP_OPEN_PAREN:
28570 case CPP_OPEN_SQUARE:
28571 ++nesting_depth;
28572 break;
28574 case CPP_CLOSE_PAREN:
28575 case CPP_CLOSE_SQUARE:
28576 if (nesting_depth-- == 0)
28577 return;
28578 break;
28580 case CPP_EOF:
28581 case CPP_PRAGMA_EOL:
28582 case CPP_SEMICOLON:
28583 case CPP_OPEN_BRACE:
28584 case CPP_CLOSE_BRACE:
28585 /* The '>' was probably forgotten, don't look further. */
28586 return;
28588 default:
28589 break;
28592 /* Consume this token. */
28593 cp_lexer_consume_token (parser->lexer);
28597 /* If the next token is the indicated keyword, consume it. Otherwise,
28598 issue an error message indicating that TOKEN_DESC was expected.
28600 Returns the token consumed, if the token had the appropriate type.
28601 Otherwise, returns NULL. */
28603 static cp_token *
28604 cp_parser_require_keyword (cp_parser* parser,
28605 enum rid keyword,
28606 required_token token_desc)
28608 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28610 if (token && token->keyword != keyword)
28612 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28613 UNKNOWN_LOCATION);
28614 return NULL;
28617 return token;
28620 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28621 function-definition. */
28623 static bool
28624 cp_parser_token_starts_function_definition_p (cp_token* token)
28626 return (/* An ordinary function-body begins with an `{'. */
28627 token->type == CPP_OPEN_BRACE
28628 /* A ctor-initializer begins with a `:'. */
28629 || token->type == CPP_COLON
28630 /* A function-try-block begins with `try'. */
28631 || token->keyword == RID_TRY
28632 /* A function-transaction-block begins with `__transaction_atomic'
28633 or `__transaction_relaxed'. */
28634 || token->keyword == RID_TRANSACTION_ATOMIC
28635 || token->keyword == RID_TRANSACTION_RELAXED
28636 /* The named return value extension begins with `return'. */
28637 || token->keyword == RID_RETURN);
28640 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28641 definition. */
28643 static bool
28644 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28646 cp_token *token;
28648 token = cp_lexer_peek_token (parser->lexer);
28649 return (token->type == CPP_OPEN_BRACE
28650 || (token->type == CPP_COLON
28651 && !parser->colon_doesnt_start_class_def_p));
28654 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28655 C++0x) ending a template-argument. */
28657 static bool
28658 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28660 cp_token *token;
28662 token = cp_lexer_peek_token (parser->lexer);
28663 return (token->type == CPP_COMMA
28664 || token->type == CPP_GREATER
28665 || token->type == CPP_ELLIPSIS
28666 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28669 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28670 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28672 static bool
28673 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28674 size_t n)
28676 cp_token *token;
28678 token = cp_lexer_peek_nth_token (parser->lexer, n);
28679 if (token->type == CPP_LESS)
28680 return true;
28681 /* Check for the sequence `<::' in the original code. It would be lexed as
28682 `[:', where `[' is a digraph, and there is no whitespace before
28683 `:'. */
28684 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28686 cp_token *token2;
28687 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28688 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28689 return true;
28691 return false;
28694 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28695 or none_type otherwise. */
28697 static enum tag_types
28698 cp_parser_token_is_class_key (cp_token* token)
28700 switch (token->keyword)
28702 case RID_CLASS:
28703 return class_type;
28704 case RID_STRUCT:
28705 return record_type;
28706 case RID_UNION:
28707 return union_type;
28709 default:
28710 return none_type;
28714 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28715 or none_type otherwise or if the token is null. */
28717 static enum tag_types
28718 cp_parser_token_is_type_parameter_key (cp_token* token)
28720 if (!token)
28721 return none_type;
28723 switch (token->keyword)
28725 case RID_CLASS:
28726 return class_type;
28727 case RID_TYPENAME:
28728 return typename_type;
28730 default:
28731 return none_type;
28735 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28737 static void
28738 cp_parser_check_class_key (enum tag_types class_key, tree type)
28740 if (type == error_mark_node)
28741 return;
28742 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28744 if (permerror (input_location, "%qs tag used in naming %q#T",
28745 class_key == union_type ? "union"
28746 : class_key == record_type ? "struct" : "class",
28747 type))
28748 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28749 "%q#T was previously declared here", type);
28753 /* Issue an error message if DECL is redeclared with different
28754 access than its original declaration [class.access.spec/3].
28755 This applies to nested classes, nested class templates and
28756 enumerations [class.mem/1]. */
28758 static void
28759 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28761 if (!decl
28762 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28763 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28764 return;
28766 if ((TREE_PRIVATE (decl)
28767 != (current_access_specifier == access_private_node))
28768 || (TREE_PROTECTED (decl)
28769 != (current_access_specifier == access_protected_node)))
28770 error_at (location, "%qD redeclared with different access", decl);
28773 /* Look for the `template' keyword, as a syntactic disambiguator.
28774 Return TRUE iff it is present, in which case it will be
28775 consumed. */
28777 static bool
28778 cp_parser_optional_template_keyword (cp_parser *parser)
28780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28782 /* In C++98 the `template' keyword can only be used within templates;
28783 outside templates the parser can always figure out what is a
28784 template and what is not. In C++11, per the resolution of DR 468,
28785 `template' is allowed in cases where it is not strictly necessary. */
28786 if (!processing_template_decl
28787 && pedantic && cxx_dialect == cxx98)
28789 cp_token *token = cp_lexer_peek_token (parser->lexer);
28790 pedwarn (token->location, OPT_Wpedantic,
28791 "in C++98 %<template%> (as a disambiguator) is only "
28792 "allowed within templates");
28793 /* If this part of the token stream is rescanned, the same
28794 error message would be generated. So, we purge the token
28795 from the stream. */
28796 cp_lexer_purge_token (parser->lexer);
28797 return false;
28799 else
28801 /* Consume the `template' keyword. */
28802 cp_lexer_consume_token (parser->lexer);
28803 return true;
28806 return false;
28809 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28810 set PARSER->SCOPE, and perform other related actions. */
28812 static void
28813 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28815 struct tree_check *check_value;
28817 /* Get the stored value. */
28818 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28819 /* Set the scope from the stored value. */
28820 parser->scope = saved_checks_value (check_value);
28821 parser->qualifying_scope = check_value->qualifying_scope;
28822 parser->object_scope = NULL_TREE;
28825 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28826 encounter the end of a block before what we were looking for. */
28828 static bool
28829 cp_parser_cache_group (cp_parser *parser,
28830 enum cpp_ttype end,
28831 unsigned depth)
28833 while (true)
28835 cp_token *token = cp_lexer_peek_token (parser->lexer);
28837 /* Abort a parenthesized expression if we encounter a semicolon. */
28838 if ((end == CPP_CLOSE_PAREN || depth == 0)
28839 && token->type == CPP_SEMICOLON)
28840 return true;
28841 /* If we've reached the end of the file, stop. */
28842 if (token->type == CPP_EOF
28843 || (end != CPP_PRAGMA_EOL
28844 && token->type == CPP_PRAGMA_EOL))
28845 return true;
28846 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28847 /* We've hit the end of an enclosing block, so there's been some
28848 kind of syntax error. */
28849 return true;
28851 /* Consume the token. */
28852 cp_lexer_consume_token (parser->lexer);
28853 /* See if it starts a new group. */
28854 if (token->type == CPP_OPEN_BRACE)
28856 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28857 /* In theory this should probably check end == '}', but
28858 cp_parser_save_member_function_body needs it to exit
28859 after either '}' or ')' when called with ')'. */
28860 if (depth == 0)
28861 return false;
28863 else if (token->type == CPP_OPEN_PAREN)
28865 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28866 if (depth == 0 && end == CPP_CLOSE_PAREN)
28867 return false;
28869 else if (token->type == CPP_PRAGMA)
28870 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28871 else if (token->type == end)
28872 return false;
28876 /* Like above, for caching a default argument or NSDMI. Both of these are
28877 terminated by a non-nested comma, but it can be unclear whether or not a
28878 comma is nested in a template argument list unless we do more parsing.
28879 In order to handle this ambiguity, when we encounter a ',' after a '<'
28880 we try to parse what follows as a parameter-declaration-list (in the
28881 case of a default argument) or a member-declarator (in the case of an
28882 NSDMI). If that succeeds, then we stop caching. */
28884 static tree
28885 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28887 unsigned depth = 0;
28888 int maybe_template_id = 0;
28889 cp_token *first_token;
28890 cp_token *token;
28891 tree default_argument;
28893 /* Add tokens until we have processed the entire default
28894 argument. We add the range [first_token, token). */
28895 first_token = cp_lexer_peek_token (parser->lexer);
28896 if (first_token->type == CPP_OPEN_BRACE)
28898 /* For list-initialization, this is straightforward. */
28899 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28900 token = cp_lexer_peek_token (parser->lexer);
28902 else while (true)
28904 bool done = false;
28906 /* Peek at the next token. */
28907 token = cp_lexer_peek_token (parser->lexer);
28908 /* What we do depends on what token we have. */
28909 switch (token->type)
28911 /* In valid code, a default argument must be
28912 immediately followed by a `,' `)', or `...'. */
28913 case CPP_COMMA:
28914 if (depth == 0 && maybe_template_id)
28916 /* If we've seen a '<', we might be in a
28917 template-argument-list. Until Core issue 325 is
28918 resolved, we don't know how this situation ought
28919 to be handled, so try to DTRT. We check whether
28920 what comes after the comma is a valid parameter
28921 declaration list. If it is, then the comma ends
28922 the default argument; otherwise the default
28923 argument continues. */
28924 bool error = false;
28925 cp_token *peek;
28927 /* Set ITALP so cp_parser_parameter_declaration_list
28928 doesn't decide to commit to this parse. */
28929 bool saved_italp = parser->in_template_argument_list_p;
28930 parser->in_template_argument_list_p = true;
28932 cp_parser_parse_tentatively (parser);
28934 if (nsdmi)
28936 /* Parse declarators until we reach a non-comma or
28937 somthing that cannot be an initializer.
28938 Just checking whether we're looking at a single
28939 declarator is insufficient. Consider:
28940 int var = tuple<T,U>::x;
28941 The template parameter 'U' looks exactly like a
28942 declarator. */
28945 int ctor_dtor_or_conv_p;
28946 cp_lexer_consume_token (parser->lexer);
28947 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28948 &ctor_dtor_or_conv_p,
28949 /*parenthesized_p=*/NULL,
28950 /*member_p=*/true,
28951 /*friend_p=*/false);
28952 peek = cp_lexer_peek_token (parser->lexer);
28953 if (cp_parser_error_occurred (parser))
28954 break;
28956 while (peek->type == CPP_COMMA);
28957 /* If we met an '=' or ';' then the original comma
28958 was the end of the NSDMI. Otherwise assume
28959 we're still in the NSDMI. */
28960 error = (peek->type != CPP_EQ
28961 && peek->type != CPP_SEMICOLON);
28963 else
28965 cp_lexer_consume_token (parser->lexer);
28966 begin_scope (sk_function_parms, NULL_TREE);
28967 cp_parser_parameter_declaration_list (parser, &error);
28968 pop_bindings_and_leave_scope ();
28970 if (!cp_parser_error_occurred (parser) && !error)
28971 done = true;
28972 cp_parser_abort_tentative_parse (parser);
28974 parser->in_template_argument_list_p = saved_italp;
28975 break;
28977 /* FALLTHRU */
28978 case CPP_CLOSE_PAREN:
28979 case CPP_ELLIPSIS:
28980 /* If we run into a non-nested `;', `}', or `]',
28981 then the code is invalid -- but the default
28982 argument is certainly over. */
28983 case CPP_SEMICOLON:
28984 case CPP_CLOSE_BRACE:
28985 case CPP_CLOSE_SQUARE:
28986 if (depth == 0
28987 /* Handle correctly int n = sizeof ... ( p ); */
28988 && token->type != CPP_ELLIPSIS)
28989 done = true;
28990 /* Update DEPTH, if necessary. */
28991 else if (token->type == CPP_CLOSE_PAREN
28992 || token->type == CPP_CLOSE_BRACE
28993 || token->type == CPP_CLOSE_SQUARE)
28994 --depth;
28995 break;
28997 case CPP_OPEN_PAREN:
28998 case CPP_OPEN_SQUARE:
28999 case CPP_OPEN_BRACE:
29000 ++depth;
29001 break;
29003 case CPP_LESS:
29004 if (depth == 0)
29005 /* This might be the comparison operator, or it might
29006 start a template argument list. */
29007 ++maybe_template_id;
29008 break;
29010 case CPP_RSHIFT:
29011 if (cxx_dialect == cxx98)
29012 break;
29013 /* Fall through for C++0x, which treats the `>>'
29014 operator like two `>' tokens in certain
29015 cases. */
29016 gcc_fallthrough ();
29018 case CPP_GREATER:
29019 if (depth == 0)
29021 /* This might be an operator, or it might close a
29022 template argument list. But if a previous '<'
29023 started a template argument list, this will have
29024 closed it, so we can't be in one anymore. */
29025 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29026 if (maybe_template_id < 0)
29027 maybe_template_id = 0;
29029 break;
29031 /* If we run out of tokens, issue an error message. */
29032 case CPP_EOF:
29033 case CPP_PRAGMA_EOL:
29034 error_at (token->location, "file ends in default argument");
29035 return error_mark_node;
29037 case CPP_NAME:
29038 case CPP_SCOPE:
29039 /* In these cases, we should look for template-ids.
29040 For example, if the default argument is
29041 `X<int, double>()', we need to do name lookup to
29042 figure out whether or not `X' is a template; if
29043 so, the `,' does not end the default argument.
29045 That is not yet done. */
29046 break;
29048 default:
29049 break;
29052 /* If we've reached the end, stop. */
29053 if (done)
29054 break;
29056 /* Add the token to the token block. */
29057 token = cp_lexer_consume_token (parser->lexer);
29060 /* Create a DEFAULT_ARG to represent the unparsed default
29061 argument. */
29062 default_argument = make_node (DEFAULT_ARG);
29063 DEFARG_TOKENS (default_argument)
29064 = cp_token_cache_new (first_token, token);
29065 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29067 return default_argument;
29070 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29072 location_t
29073 defarg_location (tree default_argument)
29075 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29076 location_t start = tokens->first->location;
29077 location_t end = tokens->last->location;
29078 return make_location (start, start, end);
29081 /* Begin parsing tentatively. We always save tokens while parsing
29082 tentatively so that if the tentative parsing fails we can restore the
29083 tokens. */
29085 static void
29086 cp_parser_parse_tentatively (cp_parser* parser)
29088 /* Enter a new parsing context. */
29089 parser->context = cp_parser_context_new (parser->context);
29090 /* Begin saving tokens. */
29091 cp_lexer_save_tokens (parser->lexer);
29092 /* In order to avoid repetitive access control error messages,
29093 access checks are queued up until we are no longer parsing
29094 tentatively. */
29095 push_deferring_access_checks (dk_deferred);
29098 /* Commit to the currently active tentative parse. */
29100 static void
29101 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29103 cp_parser_context *context;
29104 cp_lexer *lexer;
29106 /* Mark all of the levels as committed. */
29107 lexer = parser->lexer;
29108 for (context = parser->context; context->next; context = context->next)
29110 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29111 break;
29112 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29113 while (!cp_lexer_saving_tokens (lexer))
29114 lexer = lexer->next;
29115 cp_lexer_commit_tokens (lexer);
29119 /* Commit to the topmost currently active tentative parse.
29121 Note that this function shouldn't be called when there are
29122 irreversible side-effects while in a tentative state. For
29123 example, we shouldn't create a permanent entry in the symbol
29124 table, or issue an error message that might not apply if the
29125 tentative parse is aborted. */
29127 static void
29128 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29130 cp_parser_context *context = parser->context;
29131 cp_lexer *lexer = parser->lexer;
29133 if (context)
29135 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29136 return;
29137 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29139 while (!cp_lexer_saving_tokens (lexer))
29140 lexer = lexer->next;
29141 cp_lexer_commit_tokens (lexer);
29145 /* Abort the currently active tentative parse. All consumed tokens
29146 will be rolled back, and no diagnostics will be issued. */
29148 static void
29149 cp_parser_abort_tentative_parse (cp_parser* parser)
29151 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29152 || errorcount > 0);
29153 cp_parser_simulate_error (parser);
29154 /* Now, pretend that we want to see if the construct was
29155 successfully parsed. */
29156 cp_parser_parse_definitely (parser);
29159 /* Stop parsing tentatively. If a parse error has occurred, restore the
29160 token stream. Otherwise, commit to the tokens we have consumed.
29161 Returns true if no error occurred; false otherwise. */
29163 static bool
29164 cp_parser_parse_definitely (cp_parser* parser)
29166 bool error_occurred;
29167 cp_parser_context *context;
29169 /* Remember whether or not an error occurred, since we are about to
29170 destroy that information. */
29171 error_occurred = cp_parser_error_occurred (parser);
29172 /* Remove the topmost context from the stack. */
29173 context = parser->context;
29174 parser->context = context->next;
29175 /* If no parse errors occurred, commit to the tentative parse. */
29176 if (!error_occurred)
29178 /* Commit to the tokens read tentatively, unless that was
29179 already done. */
29180 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29181 cp_lexer_commit_tokens (parser->lexer);
29183 pop_to_parent_deferring_access_checks ();
29185 /* Otherwise, if errors occurred, roll back our state so that things
29186 are just as they were before we began the tentative parse. */
29187 else
29189 cp_lexer_rollback_tokens (parser->lexer);
29190 pop_deferring_access_checks ();
29192 /* Add the context to the front of the free list. */
29193 context->next = cp_parser_context_free_list;
29194 cp_parser_context_free_list = context;
29196 return !error_occurred;
29199 /* Returns true if we are parsing tentatively and are not committed to
29200 this tentative parse. */
29202 static bool
29203 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29205 return (cp_parser_parsing_tentatively (parser)
29206 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29209 /* Returns nonzero iff an error has occurred during the most recent
29210 tentative parse. */
29212 static bool
29213 cp_parser_error_occurred (cp_parser* parser)
29215 return (cp_parser_parsing_tentatively (parser)
29216 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29219 /* Returns nonzero if GNU extensions are allowed. */
29221 static bool
29222 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29224 return parser->allow_gnu_extensions_p;
29227 /* Objective-C++ Productions */
29230 /* Parse an Objective-C expression, which feeds into a primary-expression
29231 above.
29233 objc-expression:
29234 objc-message-expression
29235 objc-string-literal
29236 objc-encode-expression
29237 objc-protocol-expression
29238 objc-selector-expression
29240 Returns a tree representation of the expression. */
29242 static cp_expr
29243 cp_parser_objc_expression (cp_parser* parser)
29245 /* Try to figure out what kind of declaration is present. */
29246 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29248 switch (kwd->type)
29250 case CPP_OPEN_SQUARE:
29251 return cp_parser_objc_message_expression (parser);
29253 case CPP_OBJC_STRING:
29254 kwd = cp_lexer_consume_token (parser->lexer);
29255 return objc_build_string_object (kwd->u.value);
29257 case CPP_KEYWORD:
29258 switch (kwd->keyword)
29260 case RID_AT_ENCODE:
29261 return cp_parser_objc_encode_expression (parser);
29263 case RID_AT_PROTOCOL:
29264 return cp_parser_objc_protocol_expression (parser);
29266 case RID_AT_SELECTOR:
29267 return cp_parser_objc_selector_expression (parser);
29269 default:
29270 break;
29272 /* FALLTHRU */
29273 default:
29274 error_at (kwd->location,
29275 "misplaced %<@%D%> Objective-C++ construct",
29276 kwd->u.value);
29277 cp_parser_skip_to_end_of_block_or_statement (parser);
29280 return error_mark_node;
29283 /* Parse an Objective-C message expression.
29285 objc-message-expression:
29286 [ objc-message-receiver objc-message-args ]
29288 Returns a representation of an Objective-C message. */
29290 static tree
29291 cp_parser_objc_message_expression (cp_parser* parser)
29293 tree receiver, messageargs;
29295 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29296 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29297 receiver = cp_parser_objc_message_receiver (parser);
29298 messageargs = cp_parser_objc_message_args (parser);
29299 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29300 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29302 tree result = objc_build_message_expr (receiver, messageargs);
29304 /* Construct a location e.g.
29305 [self func1:5]
29306 ^~~~~~~~~~~~~~
29307 ranging from the '[' to the ']', with the caret at the start. */
29308 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29309 protected_set_expr_location (result, combined_loc);
29311 return result;
29314 /* Parse an objc-message-receiver.
29316 objc-message-receiver:
29317 expression
29318 simple-type-specifier
29320 Returns a representation of the type or expression. */
29322 static tree
29323 cp_parser_objc_message_receiver (cp_parser* parser)
29325 tree rcv;
29327 /* An Objective-C message receiver may be either (1) a type
29328 or (2) an expression. */
29329 cp_parser_parse_tentatively (parser);
29330 rcv = cp_parser_expression (parser);
29332 /* If that worked out, fine. */
29333 if (cp_parser_parse_definitely (parser))
29334 return rcv;
29336 cp_parser_parse_tentatively (parser);
29337 rcv = cp_parser_simple_type_specifier (parser,
29338 /*decl_specs=*/NULL,
29339 CP_PARSER_FLAGS_NONE);
29341 if (cp_parser_parse_definitely (parser))
29342 return objc_get_class_reference (rcv);
29344 cp_parser_error (parser, "objective-c++ message receiver expected");
29345 return error_mark_node;
29348 /* Parse the arguments and selectors comprising an Objective-C message.
29350 objc-message-args:
29351 objc-selector
29352 objc-selector-args
29353 objc-selector-args , objc-comma-args
29355 objc-selector-args:
29356 objc-selector [opt] : assignment-expression
29357 objc-selector-args objc-selector [opt] : assignment-expression
29359 objc-comma-args:
29360 assignment-expression
29361 objc-comma-args , assignment-expression
29363 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29364 selector arguments and TREE_VALUE containing a list of comma
29365 arguments. */
29367 static tree
29368 cp_parser_objc_message_args (cp_parser* parser)
29370 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29371 bool maybe_unary_selector_p = true;
29372 cp_token *token = cp_lexer_peek_token (parser->lexer);
29374 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29376 tree selector = NULL_TREE, arg;
29378 if (token->type != CPP_COLON)
29379 selector = cp_parser_objc_selector (parser);
29381 /* Detect if we have a unary selector. */
29382 if (maybe_unary_selector_p
29383 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29384 return build_tree_list (selector, NULL_TREE);
29386 maybe_unary_selector_p = false;
29387 cp_parser_require (parser, CPP_COLON, RT_COLON);
29388 arg = cp_parser_assignment_expression (parser);
29390 sel_args
29391 = chainon (sel_args,
29392 build_tree_list (selector, arg));
29394 token = cp_lexer_peek_token (parser->lexer);
29397 /* Handle non-selector arguments, if any. */
29398 while (token->type == CPP_COMMA)
29400 tree arg;
29402 cp_lexer_consume_token (parser->lexer);
29403 arg = cp_parser_assignment_expression (parser);
29405 addl_args
29406 = chainon (addl_args,
29407 build_tree_list (NULL_TREE, arg));
29409 token = cp_lexer_peek_token (parser->lexer);
29412 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29414 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29415 return build_tree_list (error_mark_node, error_mark_node);
29418 return build_tree_list (sel_args, addl_args);
29421 /* Parse an Objective-C encode expression.
29423 objc-encode-expression:
29424 @encode objc-typename
29426 Returns an encoded representation of the type argument. */
29428 static cp_expr
29429 cp_parser_objc_encode_expression (cp_parser* parser)
29431 tree type;
29432 cp_token *token;
29433 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29435 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29436 matching_parens parens;
29437 parens.require_open (parser);
29438 token = cp_lexer_peek_token (parser->lexer);
29439 type = complete_type (cp_parser_type_id (parser));
29440 parens.require_close (parser);
29442 if (!type)
29444 error_at (token->location,
29445 "%<@encode%> must specify a type as an argument");
29446 return error_mark_node;
29449 /* This happens if we find @encode(T) (where T is a template
29450 typename or something dependent on a template typename) when
29451 parsing a template. In that case, we can't compile it
29452 immediately, but we rather create an AT_ENCODE_EXPR which will
29453 need to be instantiated when the template is used.
29455 if (dependent_type_p (type))
29457 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29458 TREE_READONLY (value) = 1;
29459 return value;
29463 /* Build a location of the form:
29464 @encode(int)
29465 ^~~~~~~~~~~~
29466 with caret==start at the @ token, finishing at the close paren. */
29467 location_t combined_loc
29468 = make_location (start_loc, start_loc,
29469 cp_lexer_previous_token (parser->lexer)->location);
29471 return cp_expr (objc_build_encode_expr (type), combined_loc);
29474 /* Parse an Objective-C @defs expression. */
29476 static tree
29477 cp_parser_objc_defs_expression (cp_parser *parser)
29479 tree name;
29481 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29482 matching_parens parens;
29483 parens.require_open (parser);
29484 name = cp_parser_identifier (parser);
29485 parens.require_close (parser);
29487 return objc_get_class_ivars (name);
29490 /* Parse an Objective-C protocol expression.
29492 objc-protocol-expression:
29493 @protocol ( identifier )
29495 Returns a representation of the protocol expression. */
29497 static tree
29498 cp_parser_objc_protocol_expression (cp_parser* parser)
29500 tree proto;
29501 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29503 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29504 matching_parens parens;
29505 parens.require_open (parser);
29506 proto = cp_parser_identifier (parser);
29507 parens.require_close (parser);
29509 /* Build a location of the form:
29510 @protocol(prot)
29511 ^~~~~~~~~~~~~~~
29512 with caret==start at the @ token, finishing at the close paren. */
29513 location_t combined_loc
29514 = make_location (start_loc, start_loc,
29515 cp_lexer_previous_token (parser->lexer)->location);
29516 tree result = objc_build_protocol_expr (proto);
29517 protected_set_expr_location (result, combined_loc);
29518 return result;
29521 /* Parse an Objective-C selector expression.
29523 objc-selector-expression:
29524 @selector ( objc-method-signature )
29526 objc-method-signature:
29527 objc-selector
29528 objc-selector-seq
29530 objc-selector-seq:
29531 objc-selector :
29532 objc-selector-seq objc-selector :
29534 Returns a representation of the method selector. */
29536 static tree
29537 cp_parser_objc_selector_expression (cp_parser* parser)
29539 tree sel_seq = NULL_TREE;
29540 bool maybe_unary_selector_p = true;
29541 cp_token *token;
29542 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29544 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29545 matching_parens parens;
29546 parens.require_open (parser);
29547 token = cp_lexer_peek_token (parser->lexer);
29549 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29550 || token->type == CPP_SCOPE)
29552 tree selector = NULL_TREE;
29554 if (token->type != CPP_COLON
29555 || token->type == CPP_SCOPE)
29556 selector = cp_parser_objc_selector (parser);
29558 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29559 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29561 /* Detect if we have a unary selector. */
29562 if (maybe_unary_selector_p)
29564 sel_seq = selector;
29565 goto finish_selector;
29567 else
29569 cp_parser_error (parser, "expected %<:%>");
29572 maybe_unary_selector_p = false;
29573 token = cp_lexer_consume_token (parser->lexer);
29575 if (token->type == CPP_SCOPE)
29577 sel_seq
29578 = chainon (sel_seq,
29579 build_tree_list (selector, NULL_TREE));
29580 sel_seq
29581 = chainon (sel_seq,
29582 build_tree_list (NULL_TREE, NULL_TREE));
29584 else
29585 sel_seq
29586 = chainon (sel_seq,
29587 build_tree_list (selector, NULL_TREE));
29589 token = cp_lexer_peek_token (parser->lexer);
29592 finish_selector:
29593 parens.require_close (parser);
29596 /* Build a location of the form:
29597 @selector(func)
29598 ^~~~~~~~~~~~~~~
29599 with caret==start at the @ token, finishing at the close paren. */
29600 location_t combined_loc
29601 = make_location (loc, loc,
29602 cp_lexer_previous_token (parser->lexer)->location);
29603 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29604 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29605 protected_set_expr_location (result, combined_loc);
29606 return result;
29609 /* Parse a list of identifiers.
29611 objc-identifier-list:
29612 identifier
29613 objc-identifier-list , identifier
29615 Returns a TREE_LIST of identifier nodes. */
29617 static tree
29618 cp_parser_objc_identifier_list (cp_parser* parser)
29620 tree identifier;
29621 tree list;
29622 cp_token *sep;
29624 identifier = cp_parser_identifier (parser);
29625 if (identifier == error_mark_node)
29626 return error_mark_node;
29628 list = build_tree_list (NULL_TREE, identifier);
29629 sep = cp_lexer_peek_token (parser->lexer);
29631 while (sep->type == CPP_COMMA)
29633 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29634 identifier = cp_parser_identifier (parser);
29635 if (identifier == error_mark_node)
29636 return list;
29638 list = chainon (list, build_tree_list (NULL_TREE,
29639 identifier));
29640 sep = cp_lexer_peek_token (parser->lexer);
29643 return list;
29646 /* Parse an Objective-C alias declaration.
29648 objc-alias-declaration:
29649 @compatibility_alias identifier identifier ;
29651 This function registers the alias mapping with the Objective-C front end.
29652 It returns nothing. */
29654 static void
29655 cp_parser_objc_alias_declaration (cp_parser* parser)
29657 tree alias, orig;
29659 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29660 alias = cp_parser_identifier (parser);
29661 orig = cp_parser_identifier (parser);
29662 objc_declare_alias (alias, orig);
29663 cp_parser_consume_semicolon_at_end_of_statement (parser);
29666 /* Parse an Objective-C class forward-declaration.
29668 objc-class-declaration:
29669 @class objc-identifier-list ;
29671 The function registers the forward declarations with the Objective-C
29672 front end. It returns nothing. */
29674 static void
29675 cp_parser_objc_class_declaration (cp_parser* parser)
29677 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29678 while (true)
29680 tree id;
29682 id = cp_parser_identifier (parser);
29683 if (id == error_mark_node)
29684 break;
29686 objc_declare_class (id);
29688 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29689 cp_lexer_consume_token (parser->lexer);
29690 else
29691 break;
29693 cp_parser_consume_semicolon_at_end_of_statement (parser);
29696 /* Parse a list of Objective-C protocol references.
29698 objc-protocol-refs-opt:
29699 objc-protocol-refs [opt]
29701 objc-protocol-refs:
29702 < objc-identifier-list >
29704 Returns a TREE_LIST of identifiers, if any. */
29706 static tree
29707 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29709 tree protorefs = NULL_TREE;
29711 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29713 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29714 protorefs = cp_parser_objc_identifier_list (parser);
29715 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29718 return protorefs;
29721 /* Parse a Objective-C visibility specification. */
29723 static void
29724 cp_parser_objc_visibility_spec (cp_parser* parser)
29726 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29728 switch (vis->keyword)
29730 case RID_AT_PRIVATE:
29731 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29732 break;
29733 case RID_AT_PROTECTED:
29734 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29735 break;
29736 case RID_AT_PUBLIC:
29737 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29738 break;
29739 case RID_AT_PACKAGE:
29740 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29741 break;
29742 default:
29743 return;
29746 /* Eat '@private'/'@protected'/'@public'. */
29747 cp_lexer_consume_token (parser->lexer);
29750 /* Parse an Objective-C method type. Return 'true' if it is a class
29751 (+) method, and 'false' if it is an instance (-) method. */
29753 static inline bool
29754 cp_parser_objc_method_type (cp_parser* parser)
29756 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29757 return true;
29758 else
29759 return false;
29762 /* Parse an Objective-C protocol qualifier. */
29764 static tree
29765 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29767 tree quals = NULL_TREE, node;
29768 cp_token *token = cp_lexer_peek_token (parser->lexer);
29770 node = token->u.value;
29772 while (node && identifier_p (node)
29773 && (node == ridpointers [(int) RID_IN]
29774 || node == ridpointers [(int) RID_OUT]
29775 || node == ridpointers [(int) RID_INOUT]
29776 || node == ridpointers [(int) RID_BYCOPY]
29777 || node == ridpointers [(int) RID_BYREF]
29778 || node == ridpointers [(int) RID_ONEWAY]))
29780 quals = tree_cons (NULL_TREE, node, quals);
29781 cp_lexer_consume_token (parser->lexer);
29782 token = cp_lexer_peek_token (parser->lexer);
29783 node = token->u.value;
29786 return quals;
29789 /* Parse an Objective-C typename. */
29791 static tree
29792 cp_parser_objc_typename (cp_parser* parser)
29794 tree type_name = NULL_TREE;
29796 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29798 tree proto_quals, cp_type = NULL_TREE;
29800 matching_parens parens;
29801 parens.consume_open (parser); /* Eat '('. */
29802 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29804 /* An ObjC type name may consist of just protocol qualifiers, in which
29805 case the type shall default to 'id'. */
29806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29808 cp_type = cp_parser_type_id (parser);
29810 /* If the type could not be parsed, an error has already
29811 been produced. For error recovery, behave as if it had
29812 not been specified, which will use the default type
29813 'id'. */
29814 if (cp_type == error_mark_node)
29816 cp_type = NULL_TREE;
29817 /* We need to skip to the closing parenthesis as
29818 cp_parser_type_id() does not seem to do it for
29819 us. */
29820 cp_parser_skip_to_closing_parenthesis (parser,
29821 /*recovering=*/true,
29822 /*or_comma=*/false,
29823 /*consume_paren=*/false);
29827 parens.require_close (parser);
29828 type_name = build_tree_list (proto_quals, cp_type);
29831 return type_name;
29834 /* Check to see if TYPE refers to an Objective-C selector name. */
29836 static bool
29837 cp_parser_objc_selector_p (enum cpp_ttype type)
29839 return (type == CPP_NAME || type == CPP_KEYWORD
29840 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29841 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29842 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29843 || type == CPP_XOR || type == CPP_XOR_EQ);
29846 /* Parse an Objective-C selector. */
29848 static tree
29849 cp_parser_objc_selector (cp_parser* parser)
29851 cp_token *token = cp_lexer_consume_token (parser->lexer);
29853 if (!cp_parser_objc_selector_p (token->type))
29855 error_at (token->location, "invalid Objective-C++ selector name");
29856 return error_mark_node;
29859 /* C++ operator names are allowed to appear in ObjC selectors. */
29860 switch (token->type)
29862 case CPP_AND_AND: return get_identifier ("and");
29863 case CPP_AND_EQ: return get_identifier ("and_eq");
29864 case CPP_AND: return get_identifier ("bitand");
29865 case CPP_OR: return get_identifier ("bitor");
29866 case CPP_COMPL: return get_identifier ("compl");
29867 case CPP_NOT: return get_identifier ("not");
29868 case CPP_NOT_EQ: return get_identifier ("not_eq");
29869 case CPP_OR_OR: return get_identifier ("or");
29870 case CPP_OR_EQ: return get_identifier ("or_eq");
29871 case CPP_XOR: return get_identifier ("xor");
29872 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29873 default: return token->u.value;
29877 /* Parse an Objective-C params list. */
29879 static tree
29880 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29882 tree params = NULL_TREE;
29883 bool maybe_unary_selector_p = true;
29884 cp_token *token = cp_lexer_peek_token (parser->lexer);
29886 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29888 tree selector = NULL_TREE, type_name, identifier;
29889 tree parm_attr = NULL_TREE;
29891 if (token->keyword == RID_ATTRIBUTE)
29892 break;
29894 if (token->type != CPP_COLON)
29895 selector = cp_parser_objc_selector (parser);
29897 /* Detect if we have a unary selector. */
29898 if (maybe_unary_selector_p
29899 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29901 params = selector; /* Might be followed by attributes. */
29902 break;
29905 maybe_unary_selector_p = false;
29906 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29908 /* Something went quite wrong. There should be a colon
29909 here, but there is not. Stop parsing parameters. */
29910 break;
29912 type_name = cp_parser_objc_typename (parser);
29913 /* New ObjC allows attributes on parameters too. */
29914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29915 parm_attr = cp_parser_attributes_opt (parser);
29916 identifier = cp_parser_identifier (parser);
29918 params
29919 = chainon (params,
29920 objc_build_keyword_decl (selector,
29921 type_name,
29922 identifier,
29923 parm_attr));
29925 token = cp_lexer_peek_token (parser->lexer);
29928 if (params == NULL_TREE)
29930 cp_parser_error (parser, "objective-c++ method declaration is expected");
29931 return error_mark_node;
29934 /* We allow tail attributes for the method. */
29935 if (token->keyword == RID_ATTRIBUTE)
29937 *attributes = cp_parser_attributes_opt (parser);
29938 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29939 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29940 return params;
29941 cp_parser_error (parser,
29942 "method attributes must be specified at the end");
29943 return error_mark_node;
29946 if (params == NULL_TREE)
29948 cp_parser_error (parser, "objective-c++ method declaration is expected");
29949 return error_mark_node;
29951 return params;
29954 /* Parse the non-keyword Objective-C params. */
29956 static tree
29957 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29958 tree* attributes)
29960 tree params = make_node (TREE_LIST);
29961 cp_token *token = cp_lexer_peek_token (parser->lexer);
29962 *ellipsisp = false; /* Initially, assume no ellipsis. */
29964 while (token->type == CPP_COMMA)
29966 cp_parameter_declarator *parmdecl;
29967 tree parm;
29969 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29970 token = cp_lexer_peek_token (parser->lexer);
29972 if (token->type == CPP_ELLIPSIS)
29974 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29975 *ellipsisp = true;
29976 token = cp_lexer_peek_token (parser->lexer);
29977 break;
29980 /* TODO: parse attributes for tail parameters. */
29981 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29982 parm = grokdeclarator (parmdecl->declarator,
29983 &parmdecl->decl_specifiers,
29984 PARM, /*initialized=*/0,
29985 /*attrlist=*/NULL);
29987 chainon (params, build_tree_list (NULL_TREE, parm));
29988 token = cp_lexer_peek_token (parser->lexer);
29991 /* We allow tail attributes for the method. */
29992 if (token->keyword == RID_ATTRIBUTE)
29994 if (*attributes == NULL_TREE)
29996 *attributes = cp_parser_attributes_opt (parser);
29997 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29998 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29999 return params;
30001 else
30002 /* We have an error, but parse the attributes, so that we can
30003 carry on. */
30004 *attributes = cp_parser_attributes_opt (parser);
30006 cp_parser_error (parser,
30007 "method attributes must be specified at the end");
30008 return error_mark_node;
30011 return params;
30014 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30016 static void
30017 cp_parser_objc_interstitial_code (cp_parser* parser)
30019 cp_token *token = cp_lexer_peek_token (parser->lexer);
30021 /* If the next token is `extern' and the following token is a string
30022 literal, then we have a linkage specification. */
30023 if (token->keyword == RID_EXTERN
30024 && cp_parser_is_pure_string_literal
30025 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30026 cp_parser_linkage_specification (parser);
30027 /* Handle #pragma, if any. */
30028 else if (token->type == CPP_PRAGMA)
30029 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30030 /* Allow stray semicolons. */
30031 else if (token->type == CPP_SEMICOLON)
30032 cp_lexer_consume_token (parser->lexer);
30033 /* Mark methods as optional or required, when building protocols. */
30034 else if (token->keyword == RID_AT_OPTIONAL)
30036 cp_lexer_consume_token (parser->lexer);
30037 objc_set_method_opt (true);
30039 else if (token->keyword == RID_AT_REQUIRED)
30041 cp_lexer_consume_token (parser->lexer);
30042 objc_set_method_opt (false);
30044 else if (token->keyword == RID_NAMESPACE)
30045 cp_parser_namespace_definition (parser);
30046 /* Other stray characters must generate errors. */
30047 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30049 cp_lexer_consume_token (parser->lexer);
30050 error ("stray %qs between Objective-C++ methods",
30051 token->type == CPP_OPEN_BRACE ? "{" : "}");
30053 /* Finally, try to parse a block-declaration, or a function-definition. */
30054 else
30055 cp_parser_block_declaration (parser, /*statement_p=*/false);
30058 /* Parse a method signature. */
30060 static tree
30061 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30063 tree rettype, kwdparms, optparms;
30064 bool ellipsis = false;
30065 bool is_class_method;
30067 is_class_method = cp_parser_objc_method_type (parser);
30068 rettype = cp_parser_objc_typename (parser);
30069 *attributes = NULL_TREE;
30070 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30071 if (kwdparms == error_mark_node)
30072 return error_mark_node;
30073 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30074 if (optparms == error_mark_node)
30075 return error_mark_node;
30077 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30080 static bool
30081 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30083 tree tattr;
30084 cp_lexer_save_tokens (parser->lexer);
30085 tattr = cp_parser_attributes_opt (parser);
30086 gcc_assert (tattr) ;
30088 /* If the attributes are followed by a method introducer, this is not allowed.
30089 Dump the attributes and flag the situation. */
30090 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30091 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30092 return true;
30094 /* Otherwise, the attributes introduce some interstitial code, possibly so
30095 rewind to allow that check. */
30096 cp_lexer_rollback_tokens (parser->lexer);
30097 return false;
30100 /* Parse an Objective-C method prototype list. */
30102 static void
30103 cp_parser_objc_method_prototype_list (cp_parser* parser)
30105 cp_token *token = cp_lexer_peek_token (parser->lexer);
30107 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30109 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30111 tree attributes, sig;
30112 bool is_class_method;
30113 if (token->type == CPP_PLUS)
30114 is_class_method = true;
30115 else
30116 is_class_method = false;
30117 sig = cp_parser_objc_method_signature (parser, &attributes);
30118 if (sig == error_mark_node)
30120 cp_parser_skip_to_end_of_block_or_statement (parser);
30121 token = cp_lexer_peek_token (parser->lexer);
30122 continue;
30124 objc_add_method_declaration (is_class_method, sig, attributes);
30125 cp_parser_consume_semicolon_at_end_of_statement (parser);
30127 else if (token->keyword == RID_AT_PROPERTY)
30128 cp_parser_objc_at_property_declaration (parser);
30129 else if (token->keyword == RID_ATTRIBUTE
30130 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30131 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30132 OPT_Wattributes,
30133 "prefix attributes are ignored for methods");
30134 else
30135 /* Allow for interspersed non-ObjC++ code. */
30136 cp_parser_objc_interstitial_code (parser);
30138 token = cp_lexer_peek_token (parser->lexer);
30141 if (token->type != CPP_EOF)
30142 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30143 else
30144 cp_parser_error (parser, "expected %<@end%>");
30146 objc_finish_interface ();
30149 /* Parse an Objective-C method definition list. */
30151 static void
30152 cp_parser_objc_method_definition_list (cp_parser* parser)
30154 cp_token *token = cp_lexer_peek_token (parser->lexer);
30156 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30158 tree meth;
30160 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30162 cp_token *ptk;
30163 tree sig, attribute;
30164 bool is_class_method;
30165 if (token->type == CPP_PLUS)
30166 is_class_method = true;
30167 else
30168 is_class_method = false;
30169 push_deferring_access_checks (dk_deferred);
30170 sig = cp_parser_objc_method_signature (parser, &attribute);
30171 if (sig == error_mark_node)
30173 cp_parser_skip_to_end_of_block_or_statement (parser);
30174 token = cp_lexer_peek_token (parser->lexer);
30175 continue;
30177 objc_start_method_definition (is_class_method, sig, attribute,
30178 NULL_TREE);
30180 /* For historical reasons, we accept an optional semicolon. */
30181 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30182 cp_lexer_consume_token (parser->lexer);
30184 ptk = cp_lexer_peek_token (parser->lexer);
30185 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30186 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30188 perform_deferred_access_checks (tf_warning_or_error);
30189 stop_deferring_access_checks ();
30190 meth = cp_parser_function_definition_after_declarator (parser,
30191 false);
30192 pop_deferring_access_checks ();
30193 objc_finish_method_definition (meth);
30196 /* The following case will be removed once @synthesize is
30197 completely implemented. */
30198 else if (token->keyword == RID_AT_PROPERTY)
30199 cp_parser_objc_at_property_declaration (parser);
30200 else if (token->keyword == RID_AT_SYNTHESIZE)
30201 cp_parser_objc_at_synthesize_declaration (parser);
30202 else if (token->keyword == RID_AT_DYNAMIC)
30203 cp_parser_objc_at_dynamic_declaration (parser);
30204 else if (token->keyword == RID_ATTRIBUTE
30205 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30206 warning_at (token->location, OPT_Wattributes,
30207 "prefix attributes are ignored for methods");
30208 else
30209 /* Allow for interspersed non-ObjC++ code. */
30210 cp_parser_objc_interstitial_code (parser);
30212 token = cp_lexer_peek_token (parser->lexer);
30215 if (token->type != CPP_EOF)
30216 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30217 else
30218 cp_parser_error (parser, "expected %<@end%>");
30220 objc_finish_implementation ();
30223 /* Parse Objective-C ivars. */
30225 static void
30226 cp_parser_objc_class_ivars (cp_parser* parser)
30228 cp_token *token = cp_lexer_peek_token (parser->lexer);
30230 if (token->type != CPP_OPEN_BRACE)
30231 return; /* No ivars specified. */
30233 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30234 token = cp_lexer_peek_token (parser->lexer);
30236 while (token->type != CPP_CLOSE_BRACE
30237 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30239 cp_decl_specifier_seq declspecs;
30240 int decl_class_or_enum_p;
30241 tree prefix_attributes;
30243 cp_parser_objc_visibility_spec (parser);
30245 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30246 break;
30248 cp_parser_decl_specifier_seq (parser,
30249 CP_PARSER_FLAGS_OPTIONAL,
30250 &declspecs,
30251 &decl_class_or_enum_p);
30253 /* auto, register, static, extern, mutable. */
30254 if (declspecs.storage_class != sc_none)
30256 cp_parser_error (parser, "invalid type for instance variable");
30257 declspecs.storage_class = sc_none;
30260 /* thread_local. */
30261 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30263 cp_parser_error (parser, "invalid type for instance variable");
30264 declspecs.locations[ds_thread] = 0;
30267 /* typedef. */
30268 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30270 cp_parser_error (parser, "invalid type for instance variable");
30271 declspecs.locations[ds_typedef] = 0;
30274 prefix_attributes = declspecs.attributes;
30275 declspecs.attributes = NULL_TREE;
30277 /* Keep going until we hit the `;' at the end of the
30278 declaration. */
30279 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30281 tree width = NULL_TREE, attributes, first_attribute, decl;
30282 cp_declarator *declarator = NULL;
30283 int ctor_dtor_or_conv_p;
30285 /* Check for a (possibly unnamed) bitfield declaration. */
30286 token = cp_lexer_peek_token (parser->lexer);
30287 if (token->type == CPP_COLON)
30288 goto eat_colon;
30290 if (token->type == CPP_NAME
30291 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30292 == CPP_COLON))
30294 /* Get the name of the bitfield. */
30295 declarator = make_id_declarator (NULL_TREE,
30296 cp_parser_identifier (parser),
30297 sfk_none);
30299 eat_colon:
30300 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30301 /* Get the width of the bitfield. */
30302 width
30303 = cp_parser_constant_expression (parser);
30305 else
30307 /* Parse the declarator. */
30308 declarator
30309 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30310 &ctor_dtor_or_conv_p,
30311 /*parenthesized_p=*/NULL,
30312 /*member_p=*/false,
30313 /*friend_p=*/false);
30316 /* Look for attributes that apply to the ivar. */
30317 attributes = cp_parser_attributes_opt (parser);
30318 /* Remember which attributes are prefix attributes and
30319 which are not. */
30320 first_attribute = attributes;
30321 /* Combine the attributes. */
30322 attributes = attr_chainon (prefix_attributes, attributes);
30324 if (width)
30325 /* Create the bitfield declaration. */
30326 decl = grokbitfield (declarator, &declspecs,
30327 width, NULL_TREE, attributes);
30328 else
30329 decl = grokfield (declarator, &declspecs,
30330 NULL_TREE, /*init_const_expr_p=*/false,
30331 NULL_TREE, attributes);
30333 /* Add the instance variable. */
30334 if (decl != error_mark_node && decl != NULL_TREE)
30335 objc_add_instance_variable (decl);
30337 /* Reset PREFIX_ATTRIBUTES. */
30338 if (attributes != error_mark_node)
30340 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30341 attributes = TREE_CHAIN (attributes);
30342 if (attributes)
30343 TREE_CHAIN (attributes) = NULL_TREE;
30346 token = cp_lexer_peek_token (parser->lexer);
30348 if (token->type == CPP_COMMA)
30350 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30351 continue;
30353 break;
30356 cp_parser_consume_semicolon_at_end_of_statement (parser);
30357 token = cp_lexer_peek_token (parser->lexer);
30360 if (token->keyword == RID_AT_END)
30361 cp_parser_error (parser, "expected %<}%>");
30363 /* Do not consume the RID_AT_END, so it will be read again as terminating
30364 the @interface of @implementation. */
30365 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30366 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30368 /* For historical reasons, we accept an optional semicolon. */
30369 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30370 cp_lexer_consume_token (parser->lexer);
30373 /* Parse an Objective-C protocol declaration. */
30375 static void
30376 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30378 tree proto, protorefs;
30379 cp_token *tok;
30381 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30382 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30384 tok = cp_lexer_peek_token (parser->lexer);
30385 error_at (tok->location, "identifier expected after %<@protocol%>");
30386 cp_parser_consume_semicolon_at_end_of_statement (parser);
30387 return;
30390 /* See if we have a forward declaration or a definition. */
30391 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30393 /* Try a forward declaration first. */
30394 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30396 while (true)
30398 tree id;
30400 id = cp_parser_identifier (parser);
30401 if (id == error_mark_node)
30402 break;
30404 objc_declare_protocol (id, attributes);
30406 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30407 cp_lexer_consume_token (parser->lexer);
30408 else
30409 break;
30411 cp_parser_consume_semicolon_at_end_of_statement (parser);
30414 /* Ok, we got a full-fledged definition (or at least should). */
30415 else
30417 proto = cp_parser_identifier (parser);
30418 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30419 objc_start_protocol (proto, protorefs, attributes);
30420 cp_parser_objc_method_prototype_list (parser);
30424 /* Parse an Objective-C superclass or category. */
30426 static void
30427 cp_parser_objc_superclass_or_category (cp_parser *parser,
30428 bool iface_p,
30429 tree *super,
30430 tree *categ, bool *is_class_extension)
30432 cp_token *next = cp_lexer_peek_token (parser->lexer);
30434 *super = *categ = NULL_TREE;
30435 *is_class_extension = false;
30436 if (next->type == CPP_COLON)
30438 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30439 *super = cp_parser_identifier (parser);
30441 else if (next->type == CPP_OPEN_PAREN)
30443 matching_parens parens;
30444 parens.consume_open (parser); /* Eat '('. */
30446 /* If there is no category name, and this is an @interface, we
30447 have a class extension. */
30448 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30450 *categ = NULL_TREE;
30451 *is_class_extension = true;
30453 else
30454 *categ = cp_parser_identifier (parser);
30456 parens.require_close (parser);
30460 /* Parse an Objective-C class interface. */
30462 static void
30463 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30465 tree name, super, categ, protos;
30466 bool is_class_extension;
30468 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30469 name = cp_parser_identifier (parser);
30470 if (name == error_mark_node)
30472 /* It's hard to recover because even if valid @interface stuff
30473 is to follow, we can't compile it (or validate it) if we
30474 don't even know which class it refers to. Let's assume this
30475 was a stray '@interface' token in the stream and skip it.
30477 return;
30479 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30480 &is_class_extension);
30481 protos = cp_parser_objc_protocol_refs_opt (parser);
30483 /* We have either a class or a category on our hands. */
30484 if (categ || is_class_extension)
30485 objc_start_category_interface (name, categ, protos, attributes);
30486 else
30488 objc_start_class_interface (name, super, protos, attributes);
30489 /* Handle instance variable declarations, if any. */
30490 cp_parser_objc_class_ivars (parser);
30491 objc_continue_interface ();
30494 cp_parser_objc_method_prototype_list (parser);
30497 /* Parse an Objective-C class implementation. */
30499 static void
30500 cp_parser_objc_class_implementation (cp_parser* parser)
30502 tree name, super, categ;
30503 bool is_class_extension;
30505 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30506 name = cp_parser_identifier (parser);
30507 if (name == error_mark_node)
30509 /* It's hard to recover because even if valid @implementation
30510 stuff is to follow, we can't compile it (or validate it) if
30511 we don't even know which class it refers to. Let's assume
30512 this was a stray '@implementation' token in the stream and
30513 skip it.
30515 return;
30517 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30518 &is_class_extension);
30520 /* We have either a class or a category on our hands. */
30521 if (categ)
30522 objc_start_category_implementation (name, categ);
30523 else
30525 objc_start_class_implementation (name, super);
30526 /* Handle instance variable declarations, if any. */
30527 cp_parser_objc_class_ivars (parser);
30528 objc_continue_implementation ();
30531 cp_parser_objc_method_definition_list (parser);
30534 /* Consume the @end token and finish off the implementation. */
30536 static void
30537 cp_parser_objc_end_implementation (cp_parser* parser)
30539 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30540 objc_finish_implementation ();
30543 /* Parse an Objective-C declaration. */
30545 static void
30546 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30548 /* Try to figure out what kind of declaration is present. */
30549 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30551 if (attributes)
30552 switch (kwd->keyword)
30554 case RID_AT_ALIAS:
30555 case RID_AT_CLASS:
30556 case RID_AT_END:
30557 error_at (kwd->location, "attributes may not be specified before"
30558 " the %<@%D%> Objective-C++ keyword",
30559 kwd->u.value);
30560 attributes = NULL;
30561 break;
30562 case RID_AT_IMPLEMENTATION:
30563 warning_at (kwd->location, OPT_Wattributes,
30564 "prefix attributes are ignored before %<@%D%>",
30565 kwd->u.value);
30566 attributes = NULL;
30567 default:
30568 break;
30571 switch (kwd->keyword)
30573 case RID_AT_ALIAS:
30574 cp_parser_objc_alias_declaration (parser);
30575 break;
30576 case RID_AT_CLASS:
30577 cp_parser_objc_class_declaration (parser);
30578 break;
30579 case RID_AT_PROTOCOL:
30580 cp_parser_objc_protocol_declaration (parser, attributes);
30581 break;
30582 case RID_AT_INTERFACE:
30583 cp_parser_objc_class_interface (parser, attributes);
30584 break;
30585 case RID_AT_IMPLEMENTATION:
30586 cp_parser_objc_class_implementation (parser);
30587 break;
30588 case RID_AT_END:
30589 cp_parser_objc_end_implementation (parser);
30590 break;
30591 default:
30592 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30593 kwd->u.value);
30594 cp_parser_skip_to_end_of_block_or_statement (parser);
30598 /* Parse an Objective-C try-catch-finally statement.
30600 objc-try-catch-finally-stmt:
30601 @try compound-statement objc-catch-clause-seq [opt]
30602 objc-finally-clause [opt]
30604 objc-catch-clause-seq:
30605 objc-catch-clause objc-catch-clause-seq [opt]
30607 objc-catch-clause:
30608 @catch ( objc-exception-declaration ) compound-statement
30610 objc-finally-clause:
30611 @finally compound-statement
30613 objc-exception-declaration:
30614 parameter-declaration
30615 '...'
30617 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30619 Returns NULL_TREE.
30621 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30622 for C. Keep them in sync. */
30624 static tree
30625 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30627 location_t location;
30628 tree stmt;
30630 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30631 location = cp_lexer_peek_token (parser->lexer)->location;
30632 objc_maybe_warn_exceptions (location);
30633 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30634 node, lest it get absorbed into the surrounding block. */
30635 stmt = push_stmt_list ();
30636 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30637 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30639 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30641 cp_parameter_declarator *parm;
30642 tree parameter_declaration = error_mark_node;
30643 bool seen_open_paren = false;
30644 matching_parens parens;
30646 cp_lexer_consume_token (parser->lexer);
30647 if (parens.require_open (parser))
30648 seen_open_paren = true;
30649 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30651 /* We have "@catch (...)" (where the '...' are literally
30652 what is in the code). Skip the '...'.
30653 parameter_declaration is set to NULL_TREE, and
30654 objc_being_catch_clauses() knows that that means
30655 '...'. */
30656 cp_lexer_consume_token (parser->lexer);
30657 parameter_declaration = NULL_TREE;
30659 else
30661 /* We have "@catch (NSException *exception)" or something
30662 like that. Parse the parameter declaration. */
30663 parm = cp_parser_parameter_declaration (parser, false, NULL);
30664 if (parm == NULL)
30665 parameter_declaration = error_mark_node;
30666 else
30667 parameter_declaration = grokdeclarator (parm->declarator,
30668 &parm->decl_specifiers,
30669 PARM, /*initialized=*/0,
30670 /*attrlist=*/NULL);
30672 if (seen_open_paren)
30673 parens.require_close (parser);
30674 else
30676 /* If there was no open parenthesis, we are recovering from
30677 an error, and we are trying to figure out what mistake
30678 the user has made. */
30680 /* If there is an immediate closing parenthesis, the user
30681 probably forgot the opening one (ie, they typed "@catch
30682 NSException *e)". Parse the closing parenthesis and keep
30683 going. */
30684 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30685 cp_lexer_consume_token (parser->lexer);
30687 /* If these is no immediate closing parenthesis, the user
30688 probably doesn't know that parenthesis are required at
30689 all (ie, they typed "@catch NSException *e"). So, just
30690 forget about the closing parenthesis and keep going. */
30692 objc_begin_catch_clause (parameter_declaration);
30693 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30694 objc_finish_catch_clause ();
30696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30698 cp_lexer_consume_token (parser->lexer);
30699 location = cp_lexer_peek_token (parser->lexer)->location;
30700 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30701 node, lest it get absorbed into the surrounding block. */
30702 stmt = push_stmt_list ();
30703 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30704 objc_build_finally_clause (location, pop_stmt_list (stmt));
30707 return objc_finish_try_stmt ();
30710 /* Parse an Objective-C synchronized statement.
30712 objc-synchronized-stmt:
30713 @synchronized ( expression ) compound-statement
30715 Returns NULL_TREE. */
30717 static tree
30718 cp_parser_objc_synchronized_statement (cp_parser *parser)
30720 location_t location;
30721 tree lock, stmt;
30723 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30725 location = cp_lexer_peek_token (parser->lexer)->location;
30726 objc_maybe_warn_exceptions (location);
30727 matching_parens parens;
30728 parens.require_open (parser);
30729 lock = cp_parser_expression (parser);
30730 parens.require_close (parser);
30732 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30733 node, lest it get absorbed into the surrounding block. */
30734 stmt = push_stmt_list ();
30735 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30737 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30740 /* Parse an Objective-C throw statement.
30742 objc-throw-stmt:
30743 @throw assignment-expression [opt] ;
30745 Returns a constructed '@throw' statement. */
30747 static tree
30748 cp_parser_objc_throw_statement (cp_parser *parser)
30750 tree expr = NULL_TREE;
30751 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30753 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30755 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30756 expr = cp_parser_expression (parser);
30758 cp_parser_consume_semicolon_at_end_of_statement (parser);
30760 return objc_build_throw_stmt (loc, expr);
30763 /* Parse an Objective-C statement. */
30765 static tree
30766 cp_parser_objc_statement (cp_parser * parser)
30768 /* Try to figure out what kind of declaration is present. */
30769 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30771 switch (kwd->keyword)
30773 case RID_AT_TRY:
30774 return cp_parser_objc_try_catch_finally_statement (parser);
30775 case RID_AT_SYNCHRONIZED:
30776 return cp_parser_objc_synchronized_statement (parser);
30777 case RID_AT_THROW:
30778 return cp_parser_objc_throw_statement (parser);
30779 default:
30780 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30781 kwd->u.value);
30782 cp_parser_skip_to_end_of_block_or_statement (parser);
30785 return error_mark_node;
30788 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30789 look ahead to see if an objc keyword follows the attributes. This
30790 is to detect the use of prefix attributes on ObjC @interface and
30791 @protocol. */
30793 static bool
30794 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30796 cp_lexer_save_tokens (parser->lexer);
30797 *attrib = cp_parser_attributes_opt (parser);
30798 gcc_assert (*attrib);
30799 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30801 cp_lexer_commit_tokens (parser->lexer);
30802 return true;
30804 cp_lexer_rollback_tokens (parser->lexer);
30805 return false;
30808 /* This routine is a minimal replacement for
30809 c_parser_struct_declaration () used when parsing the list of
30810 types/names or ObjC++ properties. For example, when parsing the
30811 code
30813 @property (readonly) int a, b, c;
30815 this function is responsible for parsing "int a, int b, int c" and
30816 returning the declarations as CHAIN of DECLs.
30818 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30819 similar parsing. */
30820 static tree
30821 cp_parser_objc_struct_declaration (cp_parser *parser)
30823 tree decls = NULL_TREE;
30824 cp_decl_specifier_seq declspecs;
30825 int decl_class_or_enum_p;
30826 tree prefix_attributes;
30828 cp_parser_decl_specifier_seq (parser,
30829 CP_PARSER_FLAGS_NONE,
30830 &declspecs,
30831 &decl_class_or_enum_p);
30833 if (declspecs.type == error_mark_node)
30834 return error_mark_node;
30836 /* auto, register, static, extern, mutable. */
30837 if (declspecs.storage_class != sc_none)
30839 cp_parser_error (parser, "invalid type for property");
30840 declspecs.storage_class = sc_none;
30843 /* thread_local. */
30844 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30846 cp_parser_error (parser, "invalid type for property");
30847 declspecs.locations[ds_thread] = 0;
30850 /* typedef. */
30851 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30853 cp_parser_error (parser, "invalid type for property");
30854 declspecs.locations[ds_typedef] = 0;
30857 prefix_attributes = declspecs.attributes;
30858 declspecs.attributes = NULL_TREE;
30860 /* Keep going until we hit the `;' at the end of the declaration. */
30861 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30863 tree attributes, first_attribute, decl;
30864 cp_declarator *declarator;
30865 cp_token *token;
30867 /* Parse the declarator. */
30868 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30869 NULL, NULL, false, false);
30871 /* Look for attributes that apply to the ivar. */
30872 attributes = cp_parser_attributes_opt (parser);
30873 /* Remember which attributes are prefix attributes and
30874 which are not. */
30875 first_attribute = attributes;
30876 /* Combine the attributes. */
30877 attributes = attr_chainon (prefix_attributes, attributes);
30879 decl = grokfield (declarator, &declspecs,
30880 NULL_TREE, /*init_const_expr_p=*/false,
30881 NULL_TREE, attributes);
30883 if (decl == error_mark_node || decl == NULL_TREE)
30884 return error_mark_node;
30886 /* Reset PREFIX_ATTRIBUTES. */
30887 if (attributes != error_mark_node)
30889 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30890 attributes = TREE_CHAIN (attributes);
30891 if (attributes)
30892 TREE_CHAIN (attributes) = NULL_TREE;
30895 DECL_CHAIN (decl) = decls;
30896 decls = decl;
30898 token = cp_lexer_peek_token (parser->lexer);
30899 if (token->type == CPP_COMMA)
30901 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30902 continue;
30904 else
30905 break;
30907 return decls;
30910 /* Parse an Objective-C @property declaration. The syntax is:
30912 objc-property-declaration:
30913 '@property' objc-property-attributes[opt] struct-declaration ;
30915 objc-property-attributes:
30916 '(' objc-property-attribute-list ')'
30918 objc-property-attribute-list:
30919 objc-property-attribute
30920 objc-property-attribute-list, objc-property-attribute
30922 objc-property-attribute
30923 'getter' = identifier
30924 'setter' = identifier
30925 'readonly'
30926 'readwrite'
30927 'assign'
30928 'retain'
30929 'copy'
30930 'nonatomic'
30932 For example:
30933 @property NSString *name;
30934 @property (readonly) id object;
30935 @property (retain, nonatomic, getter=getTheName) id name;
30936 @property int a, b, c;
30938 PS: This function is identical to
30939 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30940 static void
30941 cp_parser_objc_at_property_declaration (cp_parser *parser)
30943 /* The following variables hold the attributes of the properties as
30944 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30945 seen. When we see an attribute, we set them to 'true' (if they
30946 are boolean properties) or to the identifier (if they have an
30947 argument, ie, for getter and setter). Note that here we only
30948 parse the list of attributes, check the syntax and accumulate the
30949 attributes that we find. objc_add_property_declaration() will
30950 then process the information. */
30951 bool property_assign = false;
30952 bool property_copy = false;
30953 tree property_getter_ident = NULL_TREE;
30954 bool property_nonatomic = false;
30955 bool property_readonly = false;
30956 bool property_readwrite = false;
30957 bool property_retain = false;
30958 tree property_setter_ident = NULL_TREE;
30960 /* 'properties' is the list of properties that we read. Usually a
30961 single one, but maybe more (eg, in "@property int a, b, c;" there
30962 are three). */
30963 tree properties;
30964 location_t loc;
30966 loc = cp_lexer_peek_token (parser->lexer)->location;
30968 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30970 /* Parse the optional attribute list... */
30971 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30973 /* Eat the '('. */
30974 matching_parens parens;
30975 parens.consume_open (parser);
30977 while (true)
30979 bool syntax_error = false;
30980 cp_token *token = cp_lexer_peek_token (parser->lexer);
30981 enum rid keyword;
30983 if (token->type != CPP_NAME)
30985 cp_parser_error (parser, "expected identifier");
30986 break;
30988 keyword = C_RID_CODE (token->u.value);
30989 cp_lexer_consume_token (parser->lexer);
30990 switch (keyword)
30992 case RID_ASSIGN: property_assign = true; break;
30993 case RID_COPY: property_copy = true; break;
30994 case RID_NONATOMIC: property_nonatomic = true; break;
30995 case RID_READONLY: property_readonly = true; break;
30996 case RID_READWRITE: property_readwrite = true; break;
30997 case RID_RETAIN: property_retain = true; break;
30999 case RID_GETTER:
31000 case RID_SETTER:
31001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31003 if (keyword == RID_GETTER)
31004 cp_parser_error (parser,
31005 "missing %<=%> (after %<getter%> attribute)");
31006 else
31007 cp_parser_error (parser,
31008 "missing %<=%> (after %<setter%> attribute)");
31009 syntax_error = true;
31010 break;
31012 cp_lexer_consume_token (parser->lexer); /* eat the = */
31013 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31015 cp_parser_error (parser, "expected identifier");
31016 syntax_error = true;
31017 break;
31019 if (keyword == RID_SETTER)
31021 if (property_setter_ident != NULL_TREE)
31023 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31024 cp_lexer_consume_token (parser->lexer);
31026 else
31027 property_setter_ident = cp_parser_objc_selector (parser);
31028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31029 cp_parser_error (parser, "setter name must terminate with %<:%>");
31030 else
31031 cp_lexer_consume_token (parser->lexer);
31033 else
31035 if (property_getter_ident != NULL_TREE)
31037 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31038 cp_lexer_consume_token (parser->lexer);
31040 else
31041 property_getter_ident = cp_parser_objc_selector (parser);
31043 break;
31044 default:
31045 cp_parser_error (parser, "unknown property attribute");
31046 syntax_error = true;
31047 break;
31050 if (syntax_error)
31051 break;
31053 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31054 cp_lexer_consume_token (parser->lexer);
31055 else
31056 break;
31059 /* FIXME: "@property (setter, assign);" will generate a spurious
31060 "error: expected ‘)’ before ‘,’ token". This is because
31061 cp_parser_require, unlike the C counterpart, will produce an
31062 error even if we are in error recovery. */
31063 if (!parens.require_close (parser))
31065 cp_parser_skip_to_closing_parenthesis (parser,
31066 /*recovering=*/true,
31067 /*or_comma=*/false,
31068 /*consume_paren=*/true);
31072 /* ... and the property declaration(s). */
31073 properties = cp_parser_objc_struct_declaration (parser);
31075 if (properties == error_mark_node)
31077 cp_parser_skip_to_end_of_statement (parser);
31078 /* If the next token is now a `;', consume it. */
31079 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31080 cp_lexer_consume_token (parser->lexer);
31081 return;
31084 if (properties == NULL_TREE)
31085 cp_parser_error (parser, "expected identifier");
31086 else
31088 /* Comma-separated properties are chained together in
31089 reverse order; add them one by one. */
31090 properties = nreverse (properties);
31092 for (; properties; properties = TREE_CHAIN (properties))
31093 objc_add_property_declaration (loc, copy_node (properties),
31094 property_readonly, property_readwrite,
31095 property_assign, property_retain,
31096 property_copy, property_nonatomic,
31097 property_getter_ident, property_setter_ident);
31100 cp_parser_consume_semicolon_at_end_of_statement (parser);
31103 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31105 objc-synthesize-declaration:
31106 @synthesize objc-synthesize-identifier-list ;
31108 objc-synthesize-identifier-list:
31109 objc-synthesize-identifier
31110 objc-synthesize-identifier-list, objc-synthesize-identifier
31112 objc-synthesize-identifier
31113 identifier
31114 identifier = identifier
31116 For example:
31117 @synthesize MyProperty;
31118 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31120 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31121 for C. Keep them in sync.
31123 static void
31124 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31126 tree list = NULL_TREE;
31127 location_t loc;
31128 loc = cp_lexer_peek_token (parser->lexer)->location;
31130 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31131 while (true)
31133 tree property, ivar;
31134 property = cp_parser_identifier (parser);
31135 if (property == error_mark_node)
31137 cp_parser_consume_semicolon_at_end_of_statement (parser);
31138 return;
31140 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31142 cp_lexer_consume_token (parser->lexer);
31143 ivar = cp_parser_identifier (parser);
31144 if (ivar == error_mark_node)
31146 cp_parser_consume_semicolon_at_end_of_statement (parser);
31147 return;
31150 else
31151 ivar = NULL_TREE;
31152 list = chainon (list, build_tree_list (ivar, property));
31153 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31154 cp_lexer_consume_token (parser->lexer);
31155 else
31156 break;
31158 cp_parser_consume_semicolon_at_end_of_statement (parser);
31159 objc_add_synthesize_declaration (loc, list);
31162 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31164 objc-dynamic-declaration:
31165 @dynamic identifier-list ;
31167 For example:
31168 @dynamic MyProperty;
31169 @dynamic MyProperty, AnotherProperty;
31171 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31172 for C. Keep them in sync.
31174 static void
31175 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31177 tree list = NULL_TREE;
31178 location_t loc;
31179 loc = cp_lexer_peek_token (parser->lexer)->location;
31181 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31182 while (true)
31184 tree property;
31185 property = cp_parser_identifier (parser);
31186 if (property == error_mark_node)
31188 cp_parser_consume_semicolon_at_end_of_statement (parser);
31189 return;
31191 list = chainon (list, build_tree_list (NULL, property));
31192 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31193 cp_lexer_consume_token (parser->lexer);
31194 else
31195 break;
31197 cp_parser_consume_semicolon_at_end_of_statement (parser);
31198 objc_add_dynamic_declaration (loc, list);
31202 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31204 /* Returns name of the next clause.
31205 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31206 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31207 returned and the token is consumed. */
31209 static pragma_omp_clause
31210 cp_parser_omp_clause_name (cp_parser *parser)
31212 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31215 result = PRAGMA_OACC_CLAUSE_AUTO;
31216 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31217 result = PRAGMA_OMP_CLAUSE_IF;
31218 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31219 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31220 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31221 result = PRAGMA_OACC_CLAUSE_DELETE;
31222 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31223 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31224 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31225 result = PRAGMA_OMP_CLAUSE_FOR;
31226 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31228 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31229 const char *p = IDENTIFIER_POINTER (id);
31231 switch (p[0])
31233 case 'a':
31234 if (!strcmp ("aligned", p))
31235 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31236 else if (!strcmp ("async", p))
31237 result = PRAGMA_OACC_CLAUSE_ASYNC;
31238 break;
31239 case 'c':
31240 if (!strcmp ("collapse", p))
31241 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31242 else if (!strcmp ("copy", p))
31243 result = PRAGMA_OACC_CLAUSE_COPY;
31244 else if (!strcmp ("copyin", p))
31245 result = PRAGMA_OMP_CLAUSE_COPYIN;
31246 else if (!strcmp ("copyout", p))
31247 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31248 else if (!strcmp ("copyprivate", p))
31249 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31250 else if (!strcmp ("create", p))
31251 result = PRAGMA_OACC_CLAUSE_CREATE;
31252 break;
31253 case 'd':
31254 if (!strcmp ("defaultmap", p))
31255 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31256 else if (!strcmp ("depend", p))
31257 result = PRAGMA_OMP_CLAUSE_DEPEND;
31258 else if (!strcmp ("device", p))
31259 result = PRAGMA_OMP_CLAUSE_DEVICE;
31260 else if (!strcmp ("deviceptr", p))
31261 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31262 else if (!strcmp ("device_resident", p))
31263 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31264 else if (!strcmp ("dist_schedule", p))
31265 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31266 break;
31267 case 'f':
31268 if (!strcmp ("final", p))
31269 result = PRAGMA_OMP_CLAUSE_FINAL;
31270 else if (!strcmp ("firstprivate", p))
31271 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31272 else if (!strcmp ("from", p))
31273 result = PRAGMA_OMP_CLAUSE_FROM;
31274 break;
31275 case 'g':
31276 if (!strcmp ("gang", p))
31277 result = PRAGMA_OACC_CLAUSE_GANG;
31278 else if (!strcmp ("grainsize", p))
31279 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31280 break;
31281 case 'h':
31282 if (!strcmp ("hint", p))
31283 result = PRAGMA_OMP_CLAUSE_HINT;
31284 else if (!strcmp ("host", p))
31285 result = PRAGMA_OACC_CLAUSE_HOST;
31286 break;
31287 case 'i':
31288 if (!strcmp ("inbranch", p))
31289 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31290 else if (!strcmp ("independent", p))
31291 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31292 else if (!strcmp ("is_device_ptr", p))
31293 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31294 break;
31295 case 'l':
31296 if (!strcmp ("lastprivate", p))
31297 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31298 else if (!strcmp ("linear", p))
31299 result = PRAGMA_OMP_CLAUSE_LINEAR;
31300 else if (!strcmp ("link", p))
31301 result = PRAGMA_OMP_CLAUSE_LINK;
31302 break;
31303 case 'm':
31304 if (!strcmp ("map", p))
31305 result = PRAGMA_OMP_CLAUSE_MAP;
31306 else if (!strcmp ("mergeable", p))
31307 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31308 break;
31309 case 'n':
31310 if (!strcmp ("nogroup", p))
31311 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31312 else if (!strcmp ("notinbranch", p))
31313 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31314 else if (!strcmp ("nowait", p))
31315 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31316 else if (!strcmp ("num_gangs", p))
31317 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31318 else if (!strcmp ("num_tasks", p))
31319 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31320 else if (!strcmp ("num_teams", p))
31321 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31322 else if (!strcmp ("num_threads", p))
31323 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31324 else if (!strcmp ("num_workers", p))
31325 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31326 break;
31327 case 'o':
31328 if (!strcmp ("ordered", p))
31329 result = PRAGMA_OMP_CLAUSE_ORDERED;
31330 break;
31331 case 'p':
31332 if (!strcmp ("parallel", p))
31333 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31334 else if (!strcmp ("present", p))
31335 result = PRAGMA_OACC_CLAUSE_PRESENT;
31336 else if (!strcmp ("present_or_copy", p)
31337 || !strcmp ("pcopy", p))
31338 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31339 else if (!strcmp ("present_or_copyin", p)
31340 || !strcmp ("pcopyin", p))
31341 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31342 else if (!strcmp ("present_or_copyout", p)
31343 || !strcmp ("pcopyout", p))
31344 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31345 else if (!strcmp ("present_or_create", p)
31346 || !strcmp ("pcreate", p))
31347 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31348 else if (!strcmp ("priority", p))
31349 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31350 else if (!strcmp ("proc_bind", p))
31351 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31352 break;
31353 case 'r':
31354 if (!strcmp ("reduction", p))
31355 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31356 break;
31357 case 's':
31358 if (!strcmp ("safelen", p))
31359 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31360 else if (!strcmp ("schedule", p))
31361 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31362 else if (!strcmp ("sections", p))
31363 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31364 else if (!strcmp ("self", p))
31365 result = PRAGMA_OACC_CLAUSE_SELF;
31366 else if (!strcmp ("seq", p))
31367 result = PRAGMA_OACC_CLAUSE_SEQ;
31368 else if (!strcmp ("shared", p))
31369 result = PRAGMA_OMP_CLAUSE_SHARED;
31370 else if (!strcmp ("simd", p))
31371 result = PRAGMA_OMP_CLAUSE_SIMD;
31372 else if (!strcmp ("simdlen", p))
31373 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31374 break;
31375 case 't':
31376 if (!strcmp ("taskgroup", p))
31377 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31378 else if (!strcmp ("thread_limit", p))
31379 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31380 else if (!strcmp ("threads", p))
31381 result = PRAGMA_OMP_CLAUSE_THREADS;
31382 else if (!strcmp ("tile", p))
31383 result = PRAGMA_OACC_CLAUSE_TILE;
31384 else if (!strcmp ("to", p))
31385 result = PRAGMA_OMP_CLAUSE_TO;
31386 break;
31387 case 'u':
31388 if (!strcmp ("uniform", p))
31389 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31390 else if (!strcmp ("untied", p))
31391 result = PRAGMA_OMP_CLAUSE_UNTIED;
31392 else if (!strcmp ("use_device", p))
31393 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31394 else if (!strcmp ("use_device_ptr", p))
31395 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31396 break;
31397 case 'v':
31398 if (!strcmp ("vector", p))
31399 result = PRAGMA_OACC_CLAUSE_VECTOR;
31400 else if (!strcmp ("vector_length", p))
31401 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31402 break;
31403 case 'w':
31404 if (!strcmp ("wait", p))
31405 result = PRAGMA_OACC_CLAUSE_WAIT;
31406 else if (!strcmp ("worker", p))
31407 result = PRAGMA_OACC_CLAUSE_WORKER;
31408 break;
31412 if (result != PRAGMA_OMP_CLAUSE_NONE)
31413 cp_lexer_consume_token (parser->lexer);
31415 return result;
31418 /* Validate that a clause of the given type does not already exist. */
31420 static void
31421 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31422 const char *name, location_t location)
31424 tree c;
31426 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31427 if (OMP_CLAUSE_CODE (c) == code)
31429 error_at (location, "too many %qs clauses", name);
31430 break;
31434 /* OpenMP 2.5:
31435 variable-list:
31436 identifier
31437 variable-list , identifier
31439 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31440 colon). An opening parenthesis will have been consumed by the caller.
31442 If KIND is nonzero, create the appropriate node and install the decl
31443 in OMP_CLAUSE_DECL and add the node to the head of the list.
31445 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31446 return the list created.
31448 COLON can be NULL if only closing parenthesis should end the list,
31449 or pointer to bool which will receive false if the list is terminated
31450 by closing parenthesis or true if the list is terminated by colon. */
31452 static tree
31453 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31454 tree list, bool *colon)
31456 cp_token *token;
31457 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31458 if (colon)
31460 parser->colon_corrects_to_scope_p = false;
31461 *colon = false;
31463 while (1)
31465 tree name, decl;
31467 token = cp_lexer_peek_token (parser->lexer);
31468 if (kind != 0
31469 && current_class_ptr
31470 && cp_parser_is_keyword (token, RID_THIS))
31472 decl = finish_this_expr ();
31473 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31474 || CONVERT_EXPR_P (decl))
31475 decl = TREE_OPERAND (decl, 0);
31476 cp_lexer_consume_token (parser->lexer);
31478 else
31480 name = cp_parser_id_expression (parser, /*template_p=*/false,
31481 /*check_dependency_p=*/true,
31482 /*template_p=*/NULL,
31483 /*declarator_p=*/false,
31484 /*optional_p=*/false);
31485 if (name == error_mark_node)
31486 goto skip_comma;
31488 if (identifier_p (name))
31489 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31490 else
31491 decl = name;
31492 if (decl == error_mark_node)
31493 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31494 token->location);
31496 if (decl == error_mark_node)
31498 else if (kind != 0)
31500 switch (kind)
31502 case OMP_CLAUSE__CACHE_:
31503 /* The OpenACC cache directive explicitly only allows "array
31504 elements or subarrays". */
31505 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31507 error_at (token->location, "expected %<[%>");
31508 decl = error_mark_node;
31509 break;
31511 /* FALLTHROUGH. */
31512 case OMP_CLAUSE_MAP:
31513 case OMP_CLAUSE_FROM:
31514 case OMP_CLAUSE_TO:
31515 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31517 location_t loc
31518 = cp_lexer_peek_token (parser->lexer)->location;
31519 cp_id_kind idk = CP_ID_KIND_NONE;
31520 cp_lexer_consume_token (parser->lexer);
31521 decl = convert_from_reference (decl);
31522 decl
31523 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31524 decl, false,
31525 &idk, loc);
31527 /* FALLTHROUGH. */
31528 case OMP_CLAUSE_DEPEND:
31529 case OMP_CLAUSE_REDUCTION:
31530 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31532 tree low_bound = NULL_TREE, length = NULL_TREE;
31534 parser->colon_corrects_to_scope_p = false;
31535 cp_lexer_consume_token (parser->lexer);
31536 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31537 low_bound = cp_parser_expression (parser);
31538 if (!colon)
31539 parser->colon_corrects_to_scope_p
31540 = saved_colon_corrects_to_scope_p;
31541 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31542 length = integer_one_node;
31543 else
31545 /* Look for `:'. */
31546 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31547 goto skip_comma;
31548 if (!cp_lexer_next_token_is (parser->lexer,
31549 CPP_CLOSE_SQUARE))
31550 length = cp_parser_expression (parser);
31552 /* Look for the closing `]'. */
31553 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31554 RT_CLOSE_SQUARE))
31555 goto skip_comma;
31557 decl = tree_cons (low_bound, length, decl);
31559 break;
31560 default:
31561 break;
31564 tree u = build_omp_clause (token->location, kind);
31565 OMP_CLAUSE_DECL (u) = decl;
31566 OMP_CLAUSE_CHAIN (u) = list;
31567 list = u;
31569 else
31570 list = tree_cons (decl, NULL_TREE, list);
31572 get_comma:
31573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31574 break;
31575 cp_lexer_consume_token (parser->lexer);
31578 if (colon)
31579 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31581 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31583 *colon = true;
31584 cp_parser_require (parser, CPP_COLON, RT_COLON);
31585 return list;
31588 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31590 int ending;
31592 /* Try to resync to an unnested comma. Copied from
31593 cp_parser_parenthesized_expression_list. */
31594 skip_comma:
31595 if (colon)
31596 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31597 ending = cp_parser_skip_to_closing_parenthesis (parser,
31598 /*recovering=*/true,
31599 /*or_comma=*/true,
31600 /*consume_paren=*/true);
31601 if (ending < 0)
31602 goto get_comma;
31605 return list;
31608 /* Similarly, but expect leading and trailing parenthesis. This is a very
31609 common case for omp clauses. */
31611 static tree
31612 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31614 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31615 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31616 return list;
31619 /* OpenACC 2.0:
31620 copy ( variable-list )
31621 copyin ( variable-list )
31622 copyout ( variable-list )
31623 create ( variable-list )
31624 delete ( variable-list )
31625 present ( variable-list )
31626 present_or_copy ( variable-list )
31627 pcopy ( variable-list )
31628 present_or_copyin ( variable-list )
31629 pcopyin ( variable-list )
31630 present_or_copyout ( variable-list )
31631 pcopyout ( variable-list )
31632 present_or_create ( variable-list )
31633 pcreate ( variable-list ) */
31635 static tree
31636 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31637 tree list)
31639 enum gomp_map_kind kind;
31640 switch (c_kind)
31642 case PRAGMA_OACC_CLAUSE_COPY:
31643 kind = GOMP_MAP_FORCE_TOFROM;
31644 break;
31645 case PRAGMA_OACC_CLAUSE_COPYIN:
31646 kind = GOMP_MAP_FORCE_TO;
31647 break;
31648 case PRAGMA_OACC_CLAUSE_COPYOUT:
31649 kind = GOMP_MAP_FORCE_FROM;
31650 break;
31651 case PRAGMA_OACC_CLAUSE_CREATE:
31652 kind = GOMP_MAP_FORCE_ALLOC;
31653 break;
31654 case PRAGMA_OACC_CLAUSE_DELETE:
31655 kind = GOMP_MAP_DELETE;
31656 break;
31657 case PRAGMA_OACC_CLAUSE_DEVICE:
31658 kind = GOMP_MAP_FORCE_TO;
31659 break;
31660 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31661 kind = GOMP_MAP_DEVICE_RESIDENT;
31662 break;
31663 case PRAGMA_OACC_CLAUSE_HOST:
31664 case PRAGMA_OACC_CLAUSE_SELF:
31665 kind = GOMP_MAP_FORCE_FROM;
31666 break;
31667 case PRAGMA_OACC_CLAUSE_LINK:
31668 kind = GOMP_MAP_LINK;
31669 break;
31670 case PRAGMA_OACC_CLAUSE_PRESENT:
31671 kind = GOMP_MAP_FORCE_PRESENT;
31672 break;
31673 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31674 kind = GOMP_MAP_TOFROM;
31675 break;
31676 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31677 kind = GOMP_MAP_TO;
31678 break;
31679 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31680 kind = GOMP_MAP_FROM;
31681 break;
31682 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31683 kind = GOMP_MAP_ALLOC;
31684 break;
31685 default:
31686 gcc_unreachable ();
31688 tree nl, c;
31689 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31691 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31692 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31694 return nl;
31697 /* OpenACC 2.0:
31698 deviceptr ( variable-list ) */
31700 static tree
31701 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31703 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31704 tree vars, t;
31706 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31707 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31708 variable-list must only allow for pointer variables. */
31709 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31710 for (t = vars; t; t = TREE_CHAIN (t))
31712 tree v = TREE_PURPOSE (t);
31713 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31714 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31715 OMP_CLAUSE_DECL (u) = v;
31716 OMP_CLAUSE_CHAIN (u) = list;
31717 list = u;
31720 return list;
31723 /* OpenACC 2.0:
31724 auto
31725 independent
31726 nohost
31727 seq */
31729 static tree
31730 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31731 enum omp_clause_code code,
31732 tree list, location_t location)
31734 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31735 tree c = build_omp_clause (location, code);
31736 OMP_CLAUSE_CHAIN (c) = list;
31737 return c;
31740 /* OpenACC:
31741 num_gangs ( expression )
31742 num_workers ( expression )
31743 vector_length ( expression ) */
31745 static tree
31746 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31747 const char *str, tree list)
31749 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31751 matching_parens parens;
31752 if (!parens.require_open (parser))
31753 return list;
31755 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31757 if (t == error_mark_node
31758 || !parens.require_close (parser))
31760 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31761 /*or_comma=*/false,
31762 /*consume_paren=*/true);
31763 return list;
31766 check_no_duplicate_clause (list, code, str, loc);
31768 tree c = build_omp_clause (loc, code);
31769 OMP_CLAUSE_OPERAND (c, 0) = t;
31770 OMP_CLAUSE_CHAIN (c) = list;
31771 return c;
31774 /* OpenACC:
31776 gang [( gang-arg-list )]
31777 worker [( [num:] int-expr )]
31778 vector [( [length:] int-expr )]
31780 where gang-arg is one of:
31782 [num:] int-expr
31783 static: size-expr
31785 and size-expr may be:
31788 int-expr
31791 static tree
31792 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31793 const char *str, tree list)
31795 const char *id = "num";
31796 cp_lexer *lexer = parser->lexer;
31797 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31798 location_t loc = cp_lexer_peek_token (lexer)->location;
31800 if (kind == OMP_CLAUSE_VECTOR)
31801 id = "length";
31803 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31805 matching_parens parens;
31806 parens.consume_open (parser);
31810 cp_token *next = cp_lexer_peek_token (lexer);
31811 int idx = 0;
31813 /* Gang static argument. */
31814 if (kind == OMP_CLAUSE_GANG
31815 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31817 cp_lexer_consume_token (lexer);
31819 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31820 goto cleanup_error;
31822 idx = 1;
31823 if (ops[idx] != NULL)
31825 cp_parser_error (parser, "too many %<static%> arguments");
31826 goto cleanup_error;
31829 /* Check for the '*' argument. */
31830 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31831 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31832 || cp_lexer_nth_token_is (parser->lexer, 2,
31833 CPP_CLOSE_PAREN)))
31835 cp_lexer_consume_token (lexer);
31836 ops[idx] = integer_minus_one_node;
31838 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31840 cp_lexer_consume_token (lexer);
31841 continue;
31843 else break;
31846 /* Worker num: argument and vector length: arguments. */
31847 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31848 && id_equal (next->u.value, id)
31849 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31851 cp_lexer_consume_token (lexer); /* id */
31852 cp_lexer_consume_token (lexer); /* ':' */
31855 /* Now collect the actual argument. */
31856 if (ops[idx] != NULL_TREE)
31858 cp_parser_error (parser, "unexpected argument");
31859 goto cleanup_error;
31862 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31863 false);
31864 if (expr == error_mark_node)
31865 goto cleanup_error;
31867 mark_exp_read (expr);
31868 ops[idx] = expr;
31870 if (kind == OMP_CLAUSE_GANG
31871 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31873 cp_lexer_consume_token (lexer);
31874 continue;
31876 break;
31878 while (1);
31880 if (!parens.require_close (parser))
31881 goto cleanup_error;
31884 check_no_duplicate_clause (list, kind, str, loc);
31886 c = build_omp_clause (loc, kind);
31888 if (ops[1])
31889 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31891 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31892 OMP_CLAUSE_CHAIN (c) = list;
31894 return c;
31896 cleanup_error:
31897 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31898 return list;
31901 /* OpenACC 2.0:
31902 tile ( size-expr-list ) */
31904 static tree
31905 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31907 tree c, expr = error_mark_node;
31908 tree tile = NULL_TREE;
31910 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31911 so, but the spec authors never considered such a case and have
31912 differing opinions on what it might mean, including 'not
31913 allowed'.) */
31914 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31915 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31916 clause_loc);
31918 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31919 return list;
31923 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31924 return list;
31926 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31927 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31928 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31930 cp_lexer_consume_token (parser->lexer);
31931 expr = integer_zero_node;
31933 else
31934 expr = cp_parser_constant_expression (parser);
31936 tile = tree_cons (NULL_TREE, expr, tile);
31938 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31940 /* Consume the trailing ')'. */
31941 cp_lexer_consume_token (parser->lexer);
31943 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31944 tile = nreverse (tile);
31945 OMP_CLAUSE_TILE_LIST (c) = tile;
31946 OMP_CLAUSE_CHAIN (c) = list;
31947 return c;
31950 /* OpenACC 2.0
31951 Parse wait clause or directive parameters. */
31953 static tree
31954 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31956 vec<tree, va_gc> *args;
31957 tree t, args_tree;
31959 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31960 /*cast_p=*/false,
31961 /*allow_expansion_p=*/true,
31962 /*non_constant_p=*/NULL);
31964 if (args == NULL || args->length () == 0)
31966 cp_parser_error (parser, "expected integer expression before ')'");
31967 if (args != NULL)
31968 release_tree_vector (args);
31969 return list;
31972 args_tree = build_tree_list_vec (args);
31974 release_tree_vector (args);
31976 for (t = args_tree; t; t = TREE_CHAIN (t))
31978 tree targ = TREE_VALUE (t);
31980 if (targ != error_mark_node)
31982 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31983 error ("%<wait%> expression must be integral");
31984 else
31986 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31988 targ = mark_rvalue_use (targ);
31989 OMP_CLAUSE_DECL (c) = targ;
31990 OMP_CLAUSE_CHAIN (c) = list;
31991 list = c;
31996 return list;
31999 /* OpenACC:
32000 wait ( int-expr-list ) */
32002 static tree
32003 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32005 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32007 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32008 return list;
32010 list = cp_parser_oacc_wait_list (parser, location, list);
32012 return list;
32015 /* OpenMP 3.0:
32016 collapse ( constant-expression ) */
32018 static tree
32019 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32021 tree c, num;
32022 location_t loc;
32023 HOST_WIDE_INT n;
32025 loc = cp_lexer_peek_token (parser->lexer)->location;
32026 matching_parens parens;
32027 if (!parens.require_open (parser))
32028 return list;
32030 num = cp_parser_constant_expression (parser);
32032 if (!parens.require_close (parser))
32033 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32034 /*or_comma=*/false,
32035 /*consume_paren=*/true);
32037 if (num == error_mark_node)
32038 return list;
32039 num = fold_non_dependent_expr (num);
32040 if (!tree_fits_shwi_p (num)
32041 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32042 || (n = tree_to_shwi (num)) <= 0
32043 || (int) n != n)
32045 error_at (loc, "collapse argument needs positive constant integer expression");
32046 return list;
32049 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32050 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32051 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32052 OMP_CLAUSE_CHAIN (c) = list;
32053 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32055 return c;
32058 /* OpenMP 2.5:
32059 default ( none | shared )
32061 OpenACC:
32062 default ( none | present ) */
32064 static tree
32065 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32066 location_t location, bool is_oacc)
32068 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32069 tree c;
32071 matching_parens parens;
32072 if (!parens.require_open (parser))
32073 return list;
32074 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32076 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32077 const char *p = IDENTIFIER_POINTER (id);
32079 switch (p[0])
32081 case 'n':
32082 if (strcmp ("none", p) != 0)
32083 goto invalid_kind;
32084 kind = OMP_CLAUSE_DEFAULT_NONE;
32085 break;
32087 case 'p':
32088 if (strcmp ("present", p) != 0 || !is_oacc)
32089 goto invalid_kind;
32090 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32091 break;
32093 case 's':
32094 if (strcmp ("shared", p) != 0 || is_oacc)
32095 goto invalid_kind;
32096 kind = OMP_CLAUSE_DEFAULT_SHARED;
32097 break;
32099 default:
32100 goto invalid_kind;
32103 cp_lexer_consume_token (parser->lexer);
32105 else
32107 invalid_kind:
32108 if (is_oacc)
32109 cp_parser_error (parser, "expected %<none%> or %<present%>");
32110 else
32111 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32114 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32115 || !parens.require_close (parser))
32116 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32117 /*or_comma=*/false,
32118 /*consume_paren=*/true);
32120 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32121 return list;
32123 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32124 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32125 OMP_CLAUSE_CHAIN (c) = list;
32126 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32128 return c;
32131 /* OpenMP 3.1:
32132 final ( expression ) */
32134 static tree
32135 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32137 tree t, c;
32139 matching_parens parens;
32140 if (!parens.require_open (parser))
32141 return list;
32143 t = cp_parser_condition (parser);
32145 if (t == error_mark_node
32146 || !parens.require_close (parser))
32147 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32148 /*or_comma=*/false,
32149 /*consume_paren=*/true);
32151 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32153 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32154 OMP_CLAUSE_FINAL_EXPR (c) = t;
32155 OMP_CLAUSE_CHAIN (c) = list;
32157 return c;
32160 /* OpenMP 2.5:
32161 if ( expression )
32163 OpenMP 4.5:
32164 if ( directive-name-modifier : expression )
32166 directive-name-modifier:
32167 parallel | task | taskloop | target data | target | target update
32168 | target enter data | target exit data */
32170 static tree
32171 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32172 bool is_omp)
32174 tree t, c;
32175 enum tree_code if_modifier = ERROR_MARK;
32177 matching_parens parens;
32178 if (!parens.require_open (parser))
32179 return list;
32181 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32183 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32184 const char *p = IDENTIFIER_POINTER (id);
32185 int n = 2;
32187 if (strcmp ("parallel", p) == 0)
32188 if_modifier = OMP_PARALLEL;
32189 else if (strcmp ("task", p) == 0)
32190 if_modifier = OMP_TASK;
32191 else if (strcmp ("taskloop", p) == 0)
32192 if_modifier = OMP_TASKLOOP;
32193 else if (strcmp ("target", p) == 0)
32195 if_modifier = OMP_TARGET;
32196 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32198 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32199 p = IDENTIFIER_POINTER (id);
32200 if (strcmp ("data", p) == 0)
32201 if_modifier = OMP_TARGET_DATA;
32202 else if (strcmp ("update", p) == 0)
32203 if_modifier = OMP_TARGET_UPDATE;
32204 else if (strcmp ("enter", p) == 0)
32205 if_modifier = OMP_TARGET_ENTER_DATA;
32206 else if (strcmp ("exit", p) == 0)
32207 if_modifier = OMP_TARGET_EXIT_DATA;
32208 if (if_modifier != OMP_TARGET)
32209 n = 3;
32210 else
32212 location_t loc
32213 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32214 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32215 "or %<exit%>");
32216 if_modifier = ERROR_MARK;
32218 if (if_modifier == OMP_TARGET_ENTER_DATA
32219 || if_modifier == OMP_TARGET_EXIT_DATA)
32221 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32223 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32224 p = IDENTIFIER_POINTER (id);
32225 if (strcmp ("data", p) == 0)
32226 n = 4;
32228 if (n != 4)
32230 location_t loc
32231 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32232 error_at (loc, "expected %<data%>");
32233 if_modifier = ERROR_MARK;
32238 if (if_modifier != ERROR_MARK)
32240 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32242 while (n-- > 0)
32243 cp_lexer_consume_token (parser->lexer);
32245 else
32247 if (n > 2)
32249 location_t loc
32250 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32251 error_at (loc, "expected %<:%>");
32253 if_modifier = ERROR_MARK;
32258 t = cp_parser_condition (parser);
32260 if (t == error_mark_node
32261 || !parens.require_close (parser))
32262 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32263 /*or_comma=*/false,
32264 /*consume_paren=*/true);
32266 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32267 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32269 if (if_modifier != ERROR_MARK
32270 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32272 const char *p = NULL;
32273 switch (if_modifier)
32275 case OMP_PARALLEL: p = "parallel"; break;
32276 case OMP_TASK: p = "task"; break;
32277 case OMP_TASKLOOP: p = "taskloop"; break;
32278 case OMP_TARGET_DATA: p = "target data"; break;
32279 case OMP_TARGET: p = "target"; break;
32280 case OMP_TARGET_UPDATE: p = "target update"; break;
32281 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32282 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32283 default: gcc_unreachable ();
32285 error_at (location, "too many %<if%> clauses with %qs modifier",
32287 return list;
32289 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32291 if (!is_omp)
32292 error_at (location, "too many %<if%> clauses");
32293 else
32294 error_at (location, "too many %<if%> clauses without modifier");
32295 return list;
32297 else if (if_modifier == ERROR_MARK
32298 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32300 error_at (location, "if any %<if%> clause has modifier, then all "
32301 "%<if%> clauses have to use modifier");
32302 return list;
32306 c = build_omp_clause (location, OMP_CLAUSE_IF);
32307 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32308 OMP_CLAUSE_IF_EXPR (c) = t;
32309 OMP_CLAUSE_CHAIN (c) = list;
32311 return c;
32314 /* OpenMP 3.1:
32315 mergeable */
32317 static tree
32318 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32319 tree list, location_t location)
32321 tree c;
32323 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32324 location);
32326 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32327 OMP_CLAUSE_CHAIN (c) = list;
32328 return c;
32331 /* OpenMP 2.5:
32332 nowait */
32334 static tree
32335 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32336 tree list, location_t location)
32338 tree c;
32340 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32342 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32343 OMP_CLAUSE_CHAIN (c) = list;
32344 return c;
32347 /* OpenMP 2.5:
32348 num_threads ( expression ) */
32350 static tree
32351 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32352 location_t location)
32354 tree t, c;
32356 matching_parens parens;
32357 if (!parens.require_open (parser))
32358 return list;
32360 t = cp_parser_expression (parser);
32362 if (t == error_mark_node
32363 || !parens.require_close (parser))
32364 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32365 /*or_comma=*/false,
32366 /*consume_paren=*/true);
32368 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32369 "num_threads", location);
32371 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32372 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32373 OMP_CLAUSE_CHAIN (c) = list;
32375 return c;
32378 /* OpenMP 4.5:
32379 num_tasks ( expression ) */
32381 static tree
32382 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32383 location_t location)
32385 tree t, c;
32387 matching_parens parens;
32388 if (!parens.require_open (parser))
32389 return list;
32391 t = cp_parser_expression (parser);
32393 if (t == error_mark_node
32394 || !parens.require_close (parser))
32395 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32396 /*or_comma=*/false,
32397 /*consume_paren=*/true);
32399 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32400 "num_tasks", location);
32402 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32403 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32404 OMP_CLAUSE_CHAIN (c) = list;
32406 return c;
32409 /* OpenMP 4.5:
32410 grainsize ( expression ) */
32412 static tree
32413 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32414 location_t location)
32416 tree t, c;
32418 matching_parens parens;
32419 if (!parens.require_open (parser))
32420 return list;
32422 t = cp_parser_expression (parser);
32424 if (t == error_mark_node
32425 || !parens.require_close (parser))
32426 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32427 /*or_comma=*/false,
32428 /*consume_paren=*/true);
32430 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32431 "grainsize", location);
32433 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32434 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32435 OMP_CLAUSE_CHAIN (c) = list;
32437 return c;
32440 /* OpenMP 4.5:
32441 priority ( expression ) */
32443 static tree
32444 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32445 location_t location)
32447 tree t, c;
32449 matching_parens parens;
32450 if (!parens.require_open (parser))
32451 return list;
32453 t = cp_parser_expression (parser);
32455 if (t == error_mark_node
32456 || !parens.require_close (parser))
32457 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32458 /*or_comma=*/false,
32459 /*consume_paren=*/true);
32461 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32462 "priority", location);
32464 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32465 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32466 OMP_CLAUSE_CHAIN (c) = list;
32468 return c;
32471 /* OpenMP 4.5:
32472 hint ( expression ) */
32474 static tree
32475 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32476 location_t location)
32478 tree t, c;
32480 matching_parens parens;
32481 if (!parens.require_open (parser))
32482 return list;
32484 t = cp_parser_expression (parser);
32486 if (t == error_mark_node
32487 || !parens.require_close (parser))
32488 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32489 /*or_comma=*/false,
32490 /*consume_paren=*/true);
32492 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32494 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32495 OMP_CLAUSE_HINT_EXPR (c) = t;
32496 OMP_CLAUSE_CHAIN (c) = list;
32498 return c;
32501 /* OpenMP 4.5:
32502 defaultmap ( tofrom : scalar ) */
32504 static tree
32505 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32506 location_t location)
32508 tree c, id;
32509 const char *p;
32511 matching_parens parens;
32512 if (!parens.require_open (parser))
32513 return list;
32515 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32517 cp_parser_error (parser, "expected %<tofrom%>");
32518 goto out_err;
32520 id = cp_lexer_peek_token (parser->lexer)->u.value;
32521 p = IDENTIFIER_POINTER (id);
32522 if (strcmp (p, "tofrom") != 0)
32524 cp_parser_error (parser, "expected %<tofrom%>");
32525 goto out_err;
32527 cp_lexer_consume_token (parser->lexer);
32528 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32529 goto out_err;
32531 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32533 cp_parser_error (parser, "expected %<scalar%>");
32534 goto out_err;
32536 id = cp_lexer_peek_token (parser->lexer)->u.value;
32537 p = IDENTIFIER_POINTER (id);
32538 if (strcmp (p, "scalar") != 0)
32540 cp_parser_error (parser, "expected %<scalar%>");
32541 goto out_err;
32543 cp_lexer_consume_token (parser->lexer);
32544 if (!parens.require_close (parser))
32545 goto out_err;
32547 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32548 location);
32550 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32551 OMP_CLAUSE_CHAIN (c) = list;
32552 return c;
32554 out_err:
32555 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32556 /*or_comma=*/false,
32557 /*consume_paren=*/true);
32558 return list;
32561 /* OpenMP 2.5:
32562 ordered
32564 OpenMP 4.5:
32565 ordered ( constant-expression ) */
32567 static tree
32568 cp_parser_omp_clause_ordered (cp_parser *parser,
32569 tree list, location_t location)
32571 tree c, num = NULL_TREE;
32572 HOST_WIDE_INT n;
32574 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32575 "ordered", location);
32577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32579 matching_parens parens;
32580 parens.consume_open (parser);
32582 num = cp_parser_constant_expression (parser);
32584 if (!parens.require_close (parser))
32585 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32586 /*or_comma=*/false,
32587 /*consume_paren=*/true);
32589 if (num == error_mark_node)
32590 return list;
32591 num = fold_non_dependent_expr (num);
32592 if (!tree_fits_shwi_p (num)
32593 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32594 || (n = tree_to_shwi (num)) <= 0
32595 || (int) n != n)
32597 error_at (location,
32598 "ordered argument needs positive constant integer "
32599 "expression");
32600 return list;
32604 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32605 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32606 OMP_CLAUSE_CHAIN (c) = list;
32607 return c;
32610 /* OpenMP 2.5:
32611 reduction ( reduction-operator : variable-list )
32613 reduction-operator:
32614 One of: + * - & ^ | && ||
32616 OpenMP 3.1:
32618 reduction-operator:
32619 One of: + * - & ^ | && || min max
32621 OpenMP 4.0:
32623 reduction-operator:
32624 One of: + * - & ^ | && ||
32625 id-expression */
32627 static tree
32628 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32630 enum tree_code code = ERROR_MARK;
32631 tree nlist, c, id = NULL_TREE;
32633 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32634 return list;
32636 switch (cp_lexer_peek_token (parser->lexer)->type)
32638 case CPP_PLUS: code = PLUS_EXPR; break;
32639 case CPP_MULT: code = MULT_EXPR; break;
32640 case CPP_MINUS: code = MINUS_EXPR; break;
32641 case CPP_AND: code = BIT_AND_EXPR; break;
32642 case CPP_XOR: code = BIT_XOR_EXPR; break;
32643 case CPP_OR: code = BIT_IOR_EXPR; break;
32644 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32645 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32646 default: break;
32649 if (code != ERROR_MARK)
32650 cp_lexer_consume_token (parser->lexer);
32651 else
32653 bool saved_colon_corrects_to_scope_p;
32654 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32655 parser->colon_corrects_to_scope_p = false;
32656 id = cp_parser_id_expression (parser, /*template_p=*/false,
32657 /*check_dependency_p=*/true,
32658 /*template_p=*/NULL,
32659 /*declarator_p=*/false,
32660 /*optional_p=*/false);
32661 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32662 if (identifier_p (id))
32664 const char *p = IDENTIFIER_POINTER (id);
32666 if (strcmp (p, "min") == 0)
32667 code = MIN_EXPR;
32668 else if (strcmp (p, "max") == 0)
32669 code = MAX_EXPR;
32670 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32671 code = PLUS_EXPR;
32672 else if (id == ovl_op_identifier (false, MULT_EXPR))
32673 code = MULT_EXPR;
32674 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32675 code = MINUS_EXPR;
32676 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32677 code = BIT_AND_EXPR;
32678 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32679 code = BIT_IOR_EXPR;
32680 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32681 code = BIT_XOR_EXPR;
32682 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32683 code = TRUTH_ANDIF_EXPR;
32684 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32685 code = TRUTH_ORIF_EXPR;
32686 id = omp_reduction_id (code, id, NULL_TREE);
32687 tree scope = parser->scope;
32688 if (scope)
32689 id = build_qualified_name (NULL_TREE, scope, id, false);
32690 parser->scope = NULL_TREE;
32691 parser->qualifying_scope = NULL_TREE;
32692 parser->object_scope = NULL_TREE;
32694 else
32696 error ("invalid reduction-identifier");
32697 resync_fail:
32698 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32699 /*or_comma=*/false,
32700 /*consume_paren=*/true);
32701 return list;
32705 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32706 goto resync_fail;
32708 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32709 NULL);
32710 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32712 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32713 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32716 return nlist;
32719 /* OpenMP 2.5:
32720 schedule ( schedule-kind )
32721 schedule ( schedule-kind , expression )
32723 schedule-kind:
32724 static | dynamic | guided | runtime | auto
32726 OpenMP 4.5:
32727 schedule ( schedule-modifier : schedule-kind )
32728 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32730 schedule-modifier:
32731 simd
32732 monotonic
32733 nonmonotonic */
32735 static tree
32736 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32738 tree c, t;
32739 int modifiers = 0, nmodifiers = 0;
32741 matching_parens parens;
32742 if (!parens.require_open (parser))
32743 return list;
32745 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32747 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32749 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32750 const char *p = IDENTIFIER_POINTER (id);
32751 if (strcmp ("simd", p) == 0)
32752 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32753 else if (strcmp ("monotonic", p) == 0)
32754 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32755 else if (strcmp ("nonmonotonic", p) == 0)
32756 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32757 else
32758 break;
32759 cp_lexer_consume_token (parser->lexer);
32760 if (nmodifiers++ == 0
32761 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32762 cp_lexer_consume_token (parser->lexer);
32763 else
32765 cp_parser_require (parser, CPP_COLON, RT_COLON);
32766 break;
32770 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32772 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32773 const char *p = IDENTIFIER_POINTER (id);
32775 switch (p[0])
32777 case 'd':
32778 if (strcmp ("dynamic", p) != 0)
32779 goto invalid_kind;
32780 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32781 break;
32783 case 'g':
32784 if (strcmp ("guided", p) != 0)
32785 goto invalid_kind;
32786 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32787 break;
32789 case 'r':
32790 if (strcmp ("runtime", p) != 0)
32791 goto invalid_kind;
32792 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32793 break;
32795 default:
32796 goto invalid_kind;
32799 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32800 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32801 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32802 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32803 else
32804 goto invalid_kind;
32805 cp_lexer_consume_token (parser->lexer);
32807 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32808 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32809 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32810 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32812 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32813 "specified");
32814 modifiers = 0;
32817 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32819 cp_token *token;
32820 cp_lexer_consume_token (parser->lexer);
32822 token = cp_lexer_peek_token (parser->lexer);
32823 t = cp_parser_assignment_expression (parser);
32825 if (t == error_mark_node)
32826 goto resync_fail;
32827 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32828 error_at (token->location, "schedule %<runtime%> does not take "
32829 "a %<chunk_size%> parameter");
32830 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32831 error_at (token->location, "schedule %<auto%> does not take "
32832 "a %<chunk_size%> parameter");
32833 else
32834 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32836 if (!parens.require_close (parser))
32837 goto resync_fail;
32839 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32840 goto resync_fail;
32842 OMP_CLAUSE_SCHEDULE_KIND (c)
32843 = (enum omp_clause_schedule_kind)
32844 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32846 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32847 OMP_CLAUSE_CHAIN (c) = list;
32848 return c;
32850 invalid_kind:
32851 cp_parser_error (parser, "invalid schedule kind");
32852 resync_fail:
32853 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32854 /*or_comma=*/false,
32855 /*consume_paren=*/true);
32856 return list;
32859 /* OpenMP 3.0:
32860 untied */
32862 static tree
32863 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32864 tree list, location_t location)
32866 tree c;
32868 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32870 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32871 OMP_CLAUSE_CHAIN (c) = list;
32872 return c;
32875 /* OpenMP 4.0:
32876 inbranch
32877 notinbranch */
32879 static tree
32880 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32881 tree list, location_t location)
32883 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32884 tree c = build_omp_clause (location, code);
32885 OMP_CLAUSE_CHAIN (c) = list;
32886 return c;
32889 /* OpenMP 4.0:
32890 parallel
32892 sections
32893 taskgroup */
32895 static tree
32896 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32897 enum omp_clause_code code,
32898 tree list, location_t location)
32900 tree c = build_omp_clause (location, code);
32901 OMP_CLAUSE_CHAIN (c) = list;
32902 return c;
32905 /* OpenMP 4.5:
32906 nogroup */
32908 static tree
32909 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32910 tree list, location_t location)
32912 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32913 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32914 OMP_CLAUSE_CHAIN (c) = list;
32915 return c;
32918 /* OpenMP 4.5:
32919 simd
32920 threads */
32922 static tree
32923 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32924 enum omp_clause_code code,
32925 tree list, location_t location)
32927 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32928 tree c = build_omp_clause (location, code);
32929 OMP_CLAUSE_CHAIN (c) = list;
32930 return c;
32933 /* OpenMP 4.0:
32934 num_teams ( expression ) */
32936 static tree
32937 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32938 location_t location)
32940 tree t, c;
32942 matching_parens parens;
32943 if (!parens.require_open (parser))
32944 return list;
32946 t = cp_parser_expression (parser);
32948 if (t == error_mark_node
32949 || !parens.require_close (parser))
32950 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32951 /*or_comma=*/false,
32952 /*consume_paren=*/true);
32954 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32955 "num_teams", location);
32957 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32958 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32959 OMP_CLAUSE_CHAIN (c) = list;
32961 return c;
32964 /* OpenMP 4.0:
32965 thread_limit ( expression ) */
32967 static tree
32968 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32969 location_t location)
32971 tree t, c;
32973 matching_parens parens;
32974 if (!parens.require_open (parser))
32975 return list;
32977 t = cp_parser_expression (parser);
32979 if (t == error_mark_node
32980 || !parens.require_close (parser))
32981 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32982 /*or_comma=*/false,
32983 /*consume_paren=*/true);
32985 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32986 "thread_limit", location);
32988 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32989 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32990 OMP_CLAUSE_CHAIN (c) = list;
32992 return c;
32995 /* OpenMP 4.0:
32996 aligned ( variable-list )
32997 aligned ( variable-list : constant-expression ) */
32999 static tree
33000 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33002 tree nlist, c, alignment = NULL_TREE;
33003 bool colon;
33005 matching_parens parens;
33006 if (!parens.require_open (parser))
33007 return list;
33009 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33010 &colon);
33012 if (colon)
33014 alignment = cp_parser_constant_expression (parser);
33016 if (!parens.require_close (parser))
33017 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33018 /*or_comma=*/false,
33019 /*consume_paren=*/true);
33021 if (alignment == error_mark_node)
33022 alignment = NULL_TREE;
33025 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33026 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33028 return nlist;
33031 /* OpenMP 4.0:
33032 linear ( variable-list )
33033 linear ( variable-list : expression )
33035 OpenMP 4.5:
33036 linear ( modifier ( variable-list ) )
33037 linear ( modifier ( variable-list ) : expression ) */
33039 static tree
33040 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33041 bool declare_simd)
33043 tree nlist, c, step = integer_one_node;
33044 bool colon;
33045 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33047 matching_parens parens;
33048 if (!parens.require_open (parser))
33049 return list;
33051 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33053 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33054 const char *p = IDENTIFIER_POINTER (id);
33056 if (strcmp ("ref", p) == 0)
33057 kind = OMP_CLAUSE_LINEAR_REF;
33058 else if (strcmp ("val", p) == 0)
33059 kind = OMP_CLAUSE_LINEAR_VAL;
33060 else if (strcmp ("uval", p) == 0)
33061 kind = OMP_CLAUSE_LINEAR_UVAL;
33062 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33063 cp_lexer_consume_token (parser->lexer);
33064 else
33065 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33068 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33069 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33070 &colon);
33071 else
33073 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33074 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33075 if (colon)
33076 cp_parser_require (parser, CPP_COLON, RT_COLON);
33077 else if (!parens.require_close (parser))
33078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33079 /*or_comma=*/false,
33080 /*consume_paren=*/true);
33083 if (colon)
33085 step = NULL_TREE;
33086 if (declare_simd
33087 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33088 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33090 cp_token *token = cp_lexer_peek_token (parser->lexer);
33091 cp_parser_parse_tentatively (parser);
33092 step = cp_parser_id_expression (parser, /*template_p=*/false,
33093 /*check_dependency_p=*/true,
33094 /*template_p=*/NULL,
33095 /*declarator_p=*/false,
33096 /*optional_p=*/false);
33097 if (step != error_mark_node)
33098 step = cp_parser_lookup_name_simple (parser, step, token->location);
33099 if (step == error_mark_node)
33101 step = NULL_TREE;
33102 cp_parser_abort_tentative_parse (parser);
33104 else if (!cp_parser_parse_definitely (parser))
33105 step = NULL_TREE;
33107 if (!step)
33108 step = cp_parser_expression (parser);
33110 if (!parens.require_close (parser))
33111 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33112 /*or_comma=*/false,
33113 /*consume_paren=*/true);
33115 if (step == error_mark_node)
33116 return list;
33119 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33121 OMP_CLAUSE_LINEAR_STEP (c) = step;
33122 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33125 return nlist;
33128 /* OpenMP 4.0:
33129 safelen ( constant-expression ) */
33131 static tree
33132 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33133 location_t location)
33135 tree t, c;
33137 matching_parens parens;
33138 if (!parens.require_open (parser))
33139 return list;
33141 t = cp_parser_constant_expression (parser);
33143 if (t == error_mark_node
33144 || !parens.require_close (parser))
33145 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33146 /*or_comma=*/false,
33147 /*consume_paren=*/true);
33149 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33151 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33152 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33153 OMP_CLAUSE_CHAIN (c) = list;
33155 return c;
33158 /* OpenMP 4.0:
33159 simdlen ( constant-expression ) */
33161 static tree
33162 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33163 location_t location)
33165 tree t, c;
33167 matching_parens parens;
33168 if (!parens.require_open (parser))
33169 return list;
33171 t = cp_parser_constant_expression (parser);
33173 if (t == error_mark_node
33174 || !parens.require_close (parser))
33175 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33176 /*or_comma=*/false,
33177 /*consume_paren=*/true);
33179 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33181 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33182 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33183 OMP_CLAUSE_CHAIN (c) = list;
33185 return c;
33188 /* OpenMP 4.5:
33189 vec:
33190 identifier [+/- integer]
33191 vec , identifier [+/- integer]
33194 static tree
33195 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33196 tree list)
33198 tree vec = NULL;
33200 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33202 cp_parser_error (parser, "expected identifier");
33203 return list;
33206 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33208 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33209 tree t, identifier = cp_parser_identifier (parser);
33210 tree addend = NULL;
33212 if (identifier == error_mark_node)
33213 t = error_mark_node;
33214 else
33216 t = cp_parser_lookup_name_simple
33217 (parser, identifier,
33218 cp_lexer_peek_token (parser->lexer)->location);
33219 if (t == error_mark_node)
33220 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33221 id_loc);
33224 bool neg = false;
33225 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33226 neg = true;
33227 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33229 addend = integer_zero_node;
33230 goto add_to_vector;
33232 cp_lexer_consume_token (parser->lexer);
33234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33236 cp_parser_error (parser, "expected integer");
33237 return list;
33240 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33241 if (TREE_CODE (addend) != INTEGER_CST)
33243 cp_parser_error (parser, "expected integer");
33244 return list;
33246 cp_lexer_consume_token (parser->lexer);
33248 add_to_vector:
33249 if (t != error_mark_node)
33251 vec = tree_cons (addend, t, vec);
33252 if (neg)
33253 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33257 break;
33259 cp_lexer_consume_token (parser->lexer);
33262 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33264 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33265 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33266 OMP_CLAUSE_DECL (u) = nreverse (vec);
33267 OMP_CLAUSE_CHAIN (u) = list;
33268 return u;
33270 return list;
33273 /* OpenMP 4.0:
33274 depend ( depend-kind : variable-list )
33276 depend-kind:
33277 in | out | inout
33279 OpenMP 4.5:
33280 depend ( source )
33282 depend ( sink : vec ) */
33284 static tree
33285 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33287 tree nlist, c;
33288 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33290 matching_parens parens;
33291 if (!parens.require_open (parser))
33292 return list;
33294 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33296 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33297 const char *p = IDENTIFIER_POINTER (id);
33299 if (strcmp ("in", p) == 0)
33300 kind = OMP_CLAUSE_DEPEND_IN;
33301 else if (strcmp ("inout", p) == 0)
33302 kind = OMP_CLAUSE_DEPEND_INOUT;
33303 else if (strcmp ("out", p) == 0)
33304 kind = OMP_CLAUSE_DEPEND_OUT;
33305 else if (strcmp ("source", p) == 0)
33306 kind = OMP_CLAUSE_DEPEND_SOURCE;
33307 else if (strcmp ("sink", p) == 0)
33308 kind = OMP_CLAUSE_DEPEND_SINK;
33309 else
33310 goto invalid_kind;
33312 else
33313 goto invalid_kind;
33315 cp_lexer_consume_token (parser->lexer);
33317 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33319 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33320 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33321 OMP_CLAUSE_DECL (c) = NULL_TREE;
33322 OMP_CLAUSE_CHAIN (c) = list;
33323 if (!parens.require_close (parser))
33324 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33325 /*or_comma=*/false,
33326 /*consume_paren=*/true);
33327 return c;
33330 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33331 goto resync_fail;
33333 if (kind == OMP_CLAUSE_DEPEND_SINK)
33334 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33335 else
33337 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33338 list, NULL);
33340 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33341 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33343 return nlist;
33345 invalid_kind:
33346 cp_parser_error (parser, "invalid depend kind");
33347 resync_fail:
33348 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33349 /*or_comma=*/false,
33350 /*consume_paren=*/true);
33351 return list;
33354 /* OpenMP 4.0:
33355 map ( map-kind : variable-list )
33356 map ( variable-list )
33358 map-kind:
33359 alloc | to | from | tofrom
33361 OpenMP 4.5:
33362 map-kind:
33363 alloc | to | from | tofrom | release | delete
33365 map ( always [,] map-kind: variable-list ) */
33367 static tree
33368 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33370 tree nlist, c;
33371 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33372 bool always = false;
33374 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33375 return list;
33377 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33379 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33380 const char *p = IDENTIFIER_POINTER (id);
33382 if (strcmp ("always", p) == 0)
33384 int nth = 2;
33385 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33386 nth++;
33387 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33388 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33389 == RID_DELETE))
33390 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33391 == CPP_COLON))
33393 always = true;
33394 cp_lexer_consume_token (parser->lexer);
33395 if (nth == 3)
33396 cp_lexer_consume_token (parser->lexer);
33401 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33402 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33404 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33405 const char *p = IDENTIFIER_POINTER (id);
33407 if (strcmp ("alloc", p) == 0)
33408 kind = GOMP_MAP_ALLOC;
33409 else if (strcmp ("to", p) == 0)
33410 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33411 else if (strcmp ("from", p) == 0)
33412 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33413 else if (strcmp ("tofrom", p) == 0)
33414 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33415 else if (strcmp ("release", p) == 0)
33416 kind = GOMP_MAP_RELEASE;
33417 else
33419 cp_parser_error (parser, "invalid map kind");
33420 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33421 /*or_comma=*/false,
33422 /*consume_paren=*/true);
33423 return list;
33425 cp_lexer_consume_token (parser->lexer);
33426 cp_lexer_consume_token (parser->lexer);
33428 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33429 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33431 kind = GOMP_MAP_DELETE;
33432 cp_lexer_consume_token (parser->lexer);
33433 cp_lexer_consume_token (parser->lexer);
33436 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33437 NULL);
33439 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33440 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33442 return nlist;
33445 /* OpenMP 4.0:
33446 device ( expression ) */
33448 static tree
33449 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33450 location_t location)
33452 tree t, c;
33454 matching_parens parens;
33455 if (!parens.require_open (parser))
33456 return list;
33458 t = cp_parser_expression (parser);
33460 if (t == error_mark_node
33461 || !parens.require_close (parser))
33462 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33463 /*or_comma=*/false,
33464 /*consume_paren=*/true);
33466 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33467 "device", location);
33469 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33470 OMP_CLAUSE_DEVICE_ID (c) = t;
33471 OMP_CLAUSE_CHAIN (c) = list;
33473 return c;
33476 /* OpenMP 4.0:
33477 dist_schedule ( static )
33478 dist_schedule ( static , expression ) */
33480 static tree
33481 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33482 location_t location)
33484 tree c, t;
33486 matching_parens parens;
33487 if (!parens.require_open (parser))
33488 return list;
33490 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33492 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33493 goto invalid_kind;
33494 cp_lexer_consume_token (parser->lexer);
33496 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33498 cp_lexer_consume_token (parser->lexer);
33500 t = cp_parser_assignment_expression (parser);
33502 if (t == error_mark_node)
33503 goto resync_fail;
33504 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33506 if (!parens.require_close (parser))
33507 goto resync_fail;
33509 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33510 goto resync_fail;
33512 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33513 location);
33514 OMP_CLAUSE_CHAIN (c) = list;
33515 return c;
33517 invalid_kind:
33518 cp_parser_error (parser, "invalid dist_schedule kind");
33519 resync_fail:
33520 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33521 /*or_comma=*/false,
33522 /*consume_paren=*/true);
33523 return list;
33526 /* OpenMP 4.0:
33527 proc_bind ( proc-bind-kind )
33529 proc-bind-kind:
33530 master | close | spread */
33532 static tree
33533 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33534 location_t location)
33536 tree c;
33537 enum omp_clause_proc_bind_kind kind;
33539 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33540 return list;
33542 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33544 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33545 const char *p = IDENTIFIER_POINTER (id);
33547 if (strcmp ("master", p) == 0)
33548 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33549 else if (strcmp ("close", p) == 0)
33550 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33551 else if (strcmp ("spread", p) == 0)
33552 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33553 else
33554 goto invalid_kind;
33556 else
33557 goto invalid_kind;
33559 cp_lexer_consume_token (parser->lexer);
33560 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33561 goto resync_fail;
33563 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33564 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33565 location);
33566 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33567 OMP_CLAUSE_CHAIN (c) = list;
33568 return c;
33570 invalid_kind:
33571 cp_parser_error (parser, "invalid depend kind");
33572 resync_fail:
33573 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33574 /*or_comma=*/false,
33575 /*consume_paren=*/true);
33576 return list;
33579 /* OpenACC:
33580 async [( int-expr )] */
33582 static tree
33583 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33585 tree c, t;
33586 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33588 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33590 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33592 matching_parens parens;
33593 parens.consume_open (parser);
33595 t = cp_parser_expression (parser);
33596 if (t == error_mark_node
33597 || !parens.require_close (parser))
33598 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33599 /*or_comma=*/false,
33600 /*consume_paren=*/true);
33603 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33605 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33606 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33607 OMP_CLAUSE_CHAIN (c) = list;
33608 list = c;
33610 return list;
33613 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33614 is a bitmask in MASK. Return the list of clauses found. */
33616 static tree
33617 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33618 const char *where, cp_token *pragma_tok,
33619 bool finish_p = true)
33621 tree clauses = NULL;
33622 bool first = true;
33624 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33626 location_t here;
33627 pragma_omp_clause c_kind;
33628 omp_clause_code code;
33629 const char *c_name;
33630 tree prev = clauses;
33632 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33633 cp_lexer_consume_token (parser->lexer);
33635 here = cp_lexer_peek_token (parser->lexer)->location;
33636 c_kind = cp_parser_omp_clause_name (parser);
33638 switch (c_kind)
33640 case PRAGMA_OACC_CLAUSE_ASYNC:
33641 clauses = cp_parser_oacc_clause_async (parser, clauses);
33642 c_name = "async";
33643 break;
33644 case PRAGMA_OACC_CLAUSE_AUTO:
33645 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33646 clauses, here);
33647 c_name = "auto";
33648 break;
33649 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33650 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33651 c_name = "collapse";
33652 break;
33653 case PRAGMA_OACC_CLAUSE_COPY:
33654 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33655 c_name = "copy";
33656 break;
33657 case PRAGMA_OACC_CLAUSE_COPYIN:
33658 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33659 c_name = "copyin";
33660 break;
33661 case PRAGMA_OACC_CLAUSE_COPYOUT:
33662 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33663 c_name = "copyout";
33664 break;
33665 case PRAGMA_OACC_CLAUSE_CREATE:
33666 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33667 c_name = "create";
33668 break;
33669 case PRAGMA_OACC_CLAUSE_DELETE:
33670 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33671 c_name = "delete";
33672 break;
33673 case PRAGMA_OMP_CLAUSE_DEFAULT:
33674 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33675 c_name = "default";
33676 break;
33677 case PRAGMA_OACC_CLAUSE_DEVICE:
33678 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33679 c_name = "device";
33680 break;
33681 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33682 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33683 c_name = "deviceptr";
33684 break;
33685 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33686 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33687 c_name = "device_resident";
33688 break;
33689 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33690 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33691 clauses);
33692 c_name = "firstprivate";
33693 break;
33694 case PRAGMA_OACC_CLAUSE_GANG:
33695 c_name = "gang";
33696 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33697 c_name, clauses);
33698 break;
33699 case PRAGMA_OACC_CLAUSE_HOST:
33700 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33701 c_name = "host";
33702 break;
33703 case PRAGMA_OACC_CLAUSE_IF:
33704 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33705 c_name = "if";
33706 break;
33707 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33708 clauses = cp_parser_oacc_simple_clause (parser,
33709 OMP_CLAUSE_INDEPENDENT,
33710 clauses, here);
33711 c_name = "independent";
33712 break;
33713 case PRAGMA_OACC_CLAUSE_LINK:
33714 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33715 c_name = "link";
33716 break;
33717 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33718 code = OMP_CLAUSE_NUM_GANGS;
33719 c_name = "num_gangs";
33720 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33721 clauses);
33722 break;
33723 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33724 c_name = "num_workers";
33725 code = OMP_CLAUSE_NUM_WORKERS;
33726 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33727 clauses);
33728 break;
33729 case PRAGMA_OACC_CLAUSE_PRESENT:
33730 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33731 c_name = "present";
33732 break;
33733 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33734 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33735 c_name = "present_or_copy";
33736 break;
33737 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33738 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33739 c_name = "present_or_copyin";
33740 break;
33741 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33742 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33743 c_name = "present_or_copyout";
33744 break;
33745 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33746 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33747 c_name = "present_or_create";
33748 break;
33749 case PRAGMA_OACC_CLAUSE_PRIVATE:
33750 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33751 clauses);
33752 c_name = "private";
33753 break;
33754 case PRAGMA_OACC_CLAUSE_REDUCTION:
33755 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33756 c_name = "reduction";
33757 break;
33758 case PRAGMA_OACC_CLAUSE_SELF:
33759 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33760 c_name = "self";
33761 break;
33762 case PRAGMA_OACC_CLAUSE_SEQ:
33763 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33764 clauses, here);
33765 c_name = "seq";
33766 break;
33767 case PRAGMA_OACC_CLAUSE_TILE:
33768 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33769 c_name = "tile";
33770 break;
33771 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33772 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33773 clauses);
33774 c_name = "use_device";
33775 break;
33776 case PRAGMA_OACC_CLAUSE_VECTOR:
33777 c_name = "vector";
33778 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33779 c_name, clauses);
33780 break;
33781 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33782 c_name = "vector_length";
33783 code = OMP_CLAUSE_VECTOR_LENGTH;
33784 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33785 clauses);
33786 break;
33787 case PRAGMA_OACC_CLAUSE_WAIT:
33788 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33789 c_name = "wait";
33790 break;
33791 case PRAGMA_OACC_CLAUSE_WORKER:
33792 c_name = "worker";
33793 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33794 c_name, clauses);
33795 break;
33796 default:
33797 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33798 goto saw_error;
33801 first = false;
33803 if (((mask >> c_kind) & 1) == 0)
33805 /* Remove the invalid clause(s) from the list to avoid
33806 confusing the rest of the compiler. */
33807 clauses = prev;
33808 error_at (here, "%qs is not valid for %qs", c_name, where);
33812 saw_error:
33813 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33815 if (finish_p)
33816 return finish_omp_clauses (clauses, C_ORT_ACC);
33818 return clauses;
33821 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33822 is a bitmask in MASK. Return the list of clauses found; the result
33823 of clause default goes in *pdefault. */
33825 static tree
33826 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33827 const char *where, cp_token *pragma_tok,
33828 bool finish_p = true)
33830 tree clauses = NULL;
33831 bool first = true;
33832 cp_token *token = NULL;
33834 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33836 pragma_omp_clause c_kind;
33837 const char *c_name;
33838 tree prev = clauses;
33840 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33841 cp_lexer_consume_token (parser->lexer);
33843 token = cp_lexer_peek_token (parser->lexer);
33844 c_kind = cp_parser_omp_clause_name (parser);
33846 switch (c_kind)
33848 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33849 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33850 token->location);
33851 c_name = "collapse";
33852 break;
33853 case PRAGMA_OMP_CLAUSE_COPYIN:
33854 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33855 c_name = "copyin";
33856 break;
33857 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33858 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33859 clauses);
33860 c_name = "copyprivate";
33861 break;
33862 case PRAGMA_OMP_CLAUSE_DEFAULT:
33863 clauses = cp_parser_omp_clause_default (parser, clauses,
33864 token->location, false);
33865 c_name = "default";
33866 break;
33867 case PRAGMA_OMP_CLAUSE_FINAL:
33868 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33869 c_name = "final";
33870 break;
33871 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33872 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33873 clauses);
33874 c_name = "firstprivate";
33875 break;
33876 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33877 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33878 token->location);
33879 c_name = "grainsize";
33880 break;
33881 case PRAGMA_OMP_CLAUSE_HINT:
33882 clauses = cp_parser_omp_clause_hint (parser, clauses,
33883 token->location);
33884 c_name = "hint";
33885 break;
33886 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33887 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33888 token->location);
33889 c_name = "defaultmap";
33890 break;
33891 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33892 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33893 clauses);
33894 c_name = "use_device_ptr";
33895 break;
33896 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33897 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33898 clauses);
33899 c_name = "is_device_ptr";
33900 break;
33901 case PRAGMA_OMP_CLAUSE_IF:
33902 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33903 true);
33904 c_name = "if";
33905 break;
33906 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33907 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33908 clauses);
33909 c_name = "lastprivate";
33910 break;
33911 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33912 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33913 token->location);
33914 c_name = "mergeable";
33915 break;
33916 case PRAGMA_OMP_CLAUSE_NOWAIT:
33917 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33918 c_name = "nowait";
33919 break;
33920 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33921 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33922 token->location);
33923 c_name = "num_tasks";
33924 break;
33925 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33926 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33927 token->location);
33928 c_name = "num_threads";
33929 break;
33930 case PRAGMA_OMP_CLAUSE_ORDERED:
33931 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33932 token->location);
33933 c_name = "ordered";
33934 break;
33935 case PRAGMA_OMP_CLAUSE_PRIORITY:
33936 clauses = cp_parser_omp_clause_priority (parser, clauses,
33937 token->location);
33938 c_name = "priority";
33939 break;
33940 case PRAGMA_OMP_CLAUSE_PRIVATE:
33941 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33942 clauses);
33943 c_name = "private";
33944 break;
33945 case PRAGMA_OMP_CLAUSE_REDUCTION:
33946 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33947 c_name = "reduction";
33948 break;
33949 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33950 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33951 token->location);
33952 c_name = "schedule";
33953 break;
33954 case PRAGMA_OMP_CLAUSE_SHARED:
33955 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33956 clauses);
33957 c_name = "shared";
33958 break;
33959 case PRAGMA_OMP_CLAUSE_UNTIED:
33960 clauses = cp_parser_omp_clause_untied (parser, clauses,
33961 token->location);
33962 c_name = "untied";
33963 break;
33964 case PRAGMA_OMP_CLAUSE_INBRANCH:
33965 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33966 clauses, token->location);
33967 c_name = "inbranch";
33968 break;
33969 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33970 clauses = cp_parser_omp_clause_branch (parser,
33971 OMP_CLAUSE_NOTINBRANCH,
33972 clauses, token->location);
33973 c_name = "notinbranch";
33974 break;
33975 case PRAGMA_OMP_CLAUSE_PARALLEL:
33976 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33977 clauses, token->location);
33978 c_name = "parallel";
33979 if (!first)
33981 clause_not_first:
33982 error_at (token->location, "%qs must be the first clause of %qs",
33983 c_name, where);
33984 clauses = prev;
33986 break;
33987 case PRAGMA_OMP_CLAUSE_FOR:
33988 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33989 clauses, token->location);
33990 c_name = "for";
33991 if (!first)
33992 goto clause_not_first;
33993 break;
33994 case PRAGMA_OMP_CLAUSE_SECTIONS:
33995 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33996 clauses, token->location);
33997 c_name = "sections";
33998 if (!first)
33999 goto clause_not_first;
34000 break;
34001 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34002 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34003 clauses, token->location);
34004 c_name = "taskgroup";
34005 if (!first)
34006 goto clause_not_first;
34007 break;
34008 case PRAGMA_OMP_CLAUSE_LINK:
34009 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34010 c_name = "to";
34011 break;
34012 case PRAGMA_OMP_CLAUSE_TO:
34013 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34014 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34015 clauses);
34016 else
34017 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34018 c_name = "to";
34019 break;
34020 case PRAGMA_OMP_CLAUSE_FROM:
34021 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34022 c_name = "from";
34023 break;
34024 case PRAGMA_OMP_CLAUSE_UNIFORM:
34025 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34026 clauses);
34027 c_name = "uniform";
34028 break;
34029 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34030 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34031 token->location);
34032 c_name = "num_teams";
34033 break;
34034 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34035 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34036 token->location);
34037 c_name = "thread_limit";
34038 break;
34039 case PRAGMA_OMP_CLAUSE_ALIGNED:
34040 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34041 c_name = "aligned";
34042 break;
34043 case PRAGMA_OMP_CLAUSE_LINEAR:
34045 bool declare_simd = false;
34046 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34047 declare_simd = true;
34048 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34050 c_name = "linear";
34051 break;
34052 case PRAGMA_OMP_CLAUSE_DEPEND:
34053 clauses = cp_parser_omp_clause_depend (parser, clauses,
34054 token->location);
34055 c_name = "depend";
34056 break;
34057 case PRAGMA_OMP_CLAUSE_MAP:
34058 clauses = cp_parser_omp_clause_map (parser, clauses);
34059 c_name = "map";
34060 break;
34061 case PRAGMA_OMP_CLAUSE_DEVICE:
34062 clauses = cp_parser_omp_clause_device (parser, clauses,
34063 token->location);
34064 c_name = "device";
34065 break;
34066 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34067 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34068 token->location);
34069 c_name = "dist_schedule";
34070 break;
34071 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34072 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34073 token->location);
34074 c_name = "proc_bind";
34075 break;
34076 case PRAGMA_OMP_CLAUSE_SAFELEN:
34077 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34078 token->location);
34079 c_name = "safelen";
34080 break;
34081 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34082 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34083 token->location);
34084 c_name = "simdlen";
34085 break;
34086 case PRAGMA_OMP_CLAUSE_NOGROUP:
34087 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34088 token->location);
34089 c_name = "nogroup";
34090 break;
34091 case PRAGMA_OMP_CLAUSE_THREADS:
34092 clauses
34093 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34094 clauses, token->location);
34095 c_name = "threads";
34096 break;
34097 case PRAGMA_OMP_CLAUSE_SIMD:
34098 clauses
34099 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34100 clauses, token->location);
34101 c_name = "simd";
34102 break;
34103 default:
34104 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34105 goto saw_error;
34108 first = false;
34110 if (((mask >> c_kind) & 1) == 0)
34112 /* Remove the invalid clause(s) from the list to avoid
34113 confusing the rest of the compiler. */
34114 clauses = prev;
34115 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34118 saw_error:
34119 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34120 if (finish_p)
34122 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34123 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34124 else
34125 return finish_omp_clauses (clauses, C_ORT_OMP);
34127 return clauses;
34130 /* OpenMP 2.5:
34131 structured-block:
34132 statement
34134 In practice, we're also interested in adding the statement to an
34135 outer node. So it is convenient if we work around the fact that
34136 cp_parser_statement calls add_stmt. */
34138 static unsigned
34139 cp_parser_begin_omp_structured_block (cp_parser *parser)
34141 unsigned save = parser->in_statement;
34143 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34144 This preserves the "not within loop or switch" style error messages
34145 for nonsense cases like
34146 void foo() {
34147 #pragma omp single
34148 break;
34151 if (parser->in_statement)
34152 parser->in_statement = IN_OMP_BLOCK;
34154 return save;
34157 static void
34158 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34160 parser->in_statement = save;
34163 static tree
34164 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34166 tree stmt = begin_omp_structured_block ();
34167 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34169 cp_parser_statement (parser, NULL_TREE, false, if_p);
34171 cp_parser_end_omp_structured_block (parser, save);
34172 return finish_omp_structured_block (stmt);
34175 /* OpenMP 2.5:
34176 # pragma omp atomic new-line
34177 expression-stmt
34179 expression-stmt:
34180 x binop= expr | x++ | ++x | x-- | --x
34181 binop:
34182 +, *, -, /, &, ^, |, <<, >>
34184 where x is an lvalue expression with scalar type.
34186 OpenMP 3.1:
34187 # pragma omp atomic new-line
34188 update-stmt
34190 # pragma omp atomic read new-line
34191 read-stmt
34193 # pragma omp atomic write new-line
34194 write-stmt
34196 # pragma omp atomic update new-line
34197 update-stmt
34199 # pragma omp atomic capture new-line
34200 capture-stmt
34202 # pragma omp atomic capture new-line
34203 capture-block
34205 read-stmt:
34206 v = x
34207 write-stmt:
34208 x = expr
34209 update-stmt:
34210 expression-stmt | x = x binop expr
34211 capture-stmt:
34212 v = expression-stmt
34213 capture-block:
34214 { v = x; update-stmt; } | { update-stmt; v = x; }
34216 OpenMP 4.0:
34217 update-stmt:
34218 expression-stmt | x = x binop expr | x = expr binop x
34219 capture-stmt:
34220 v = update-stmt
34221 capture-block:
34222 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34224 where x and v are lvalue expressions with scalar type. */
34226 static void
34227 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34229 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34230 tree rhs1 = NULL_TREE, orig_lhs;
34231 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34232 bool structured_block = false;
34233 bool seq_cst = false;
34235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34237 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34238 const char *p = IDENTIFIER_POINTER (id);
34240 if (!strcmp (p, "seq_cst"))
34242 seq_cst = true;
34243 cp_lexer_consume_token (parser->lexer);
34244 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34245 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34246 cp_lexer_consume_token (parser->lexer);
34249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34252 const char *p = IDENTIFIER_POINTER (id);
34254 if (!strcmp (p, "read"))
34255 code = OMP_ATOMIC_READ;
34256 else if (!strcmp (p, "write"))
34257 code = NOP_EXPR;
34258 else if (!strcmp (p, "update"))
34259 code = OMP_ATOMIC;
34260 else if (!strcmp (p, "capture"))
34261 code = OMP_ATOMIC_CAPTURE_NEW;
34262 else
34263 p = NULL;
34264 if (p)
34265 cp_lexer_consume_token (parser->lexer);
34267 if (!seq_cst)
34269 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34270 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34271 cp_lexer_consume_token (parser->lexer);
34273 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34275 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34276 const char *p = IDENTIFIER_POINTER (id);
34278 if (!strcmp (p, "seq_cst"))
34280 seq_cst = true;
34281 cp_lexer_consume_token (parser->lexer);
34285 cp_parser_require_pragma_eol (parser, pragma_tok);
34287 switch (code)
34289 case OMP_ATOMIC_READ:
34290 case NOP_EXPR: /* atomic write */
34291 v = cp_parser_unary_expression (parser);
34292 if (v == error_mark_node)
34293 goto saw_error;
34294 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34295 goto saw_error;
34296 if (code == NOP_EXPR)
34297 lhs = cp_parser_expression (parser);
34298 else
34299 lhs = cp_parser_unary_expression (parser);
34300 if (lhs == error_mark_node)
34301 goto saw_error;
34302 if (code == NOP_EXPR)
34304 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34305 opcode. */
34306 code = OMP_ATOMIC;
34307 rhs = lhs;
34308 lhs = v;
34309 v = NULL_TREE;
34311 goto done;
34312 case OMP_ATOMIC_CAPTURE_NEW:
34313 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34315 cp_lexer_consume_token (parser->lexer);
34316 structured_block = true;
34318 else
34320 v = cp_parser_unary_expression (parser);
34321 if (v == error_mark_node)
34322 goto saw_error;
34323 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34324 goto saw_error;
34326 default:
34327 break;
34330 restart:
34331 lhs = cp_parser_unary_expression (parser);
34332 orig_lhs = lhs;
34333 switch (TREE_CODE (lhs))
34335 case ERROR_MARK:
34336 goto saw_error;
34338 case POSTINCREMENT_EXPR:
34339 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34340 code = OMP_ATOMIC_CAPTURE_OLD;
34341 /* FALLTHROUGH */
34342 case PREINCREMENT_EXPR:
34343 lhs = TREE_OPERAND (lhs, 0);
34344 opcode = PLUS_EXPR;
34345 rhs = integer_one_node;
34346 break;
34348 case POSTDECREMENT_EXPR:
34349 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34350 code = OMP_ATOMIC_CAPTURE_OLD;
34351 /* FALLTHROUGH */
34352 case PREDECREMENT_EXPR:
34353 lhs = TREE_OPERAND (lhs, 0);
34354 opcode = MINUS_EXPR;
34355 rhs = integer_one_node;
34356 break;
34358 case COMPOUND_EXPR:
34359 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34360 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34361 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34362 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34363 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34364 (TREE_OPERAND (lhs, 1), 0), 0)))
34365 == BOOLEAN_TYPE)
34366 /* Undo effects of boolean_increment for post {in,de}crement. */
34367 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34368 /* FALLTHRU */
34369 case MODIFY_EXPR:
34370 if (TREE_CODE (lhs) == MODIFY_EXPR
34371 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34373 /* Undo effects of boolean_increment. */
34374 if (integer_onep (TREE_OPERAND (lhs, 1)))
34376 /* This is pre or post increment. */
34377 rhs = TREE_OPERAND (lhs, 1);
34378 lhs = TREE_OPERAND (lhs, 0);
34379 opcode = NOP_EXPR;
34380 if (code == OMP_ATOMIC_CAPTURE_NEW
34381 && !structured_block
34382 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34383 code = OMP_ATOMIC_CAPTURE_OLD;
34384 break;
34387 /* FALLTHRU */
34388 default:
34389 switch (cp_lexer_peek_token (parser->lexer)->type)
34391 case CPP_MULT_EQ:
34392 opcode = MULT_EXPR;
34393 break;
34394 case CPP_DIV_EQ:
34395 opcode = TRUNC_DIV_EXPR;
34396 break;
34397 case CPP_PLUS_EQ:
34398 opcode = PLUS_EXPR;
34399 break;
34400 case CPP_MINUS_EQ:
34401 opcode = MINUS_EXPR;
34402 break;
34403 case CPP_LSHIFT_EQ:
34404 opcode = LSHIFT_EXPR;
34405 break;
34406 case CPP_RSHIFT_EQ:
34407 opcode = RSHIFT_EXPR;
34408 break;
34409 case CPP_AND_EQ:
34410 opcode = BIT_AND_EXPR;
34411 break;
34412 case CPP_OR_EQ:
34413 opcode = BIT_IOR_EXPR;
34414 break;
34415 case CPP_XOR_EQ:
34416 opcode = BIT_XOR_EXPR;
34417 break;
34418 case CPP_EQ:
34419 enum cp_parser_prec oprec;
34420 cp_token *token;
34421 cp_lexer_consume_token (parser->lexer);
34422 cp_parser_parse_tentatively (parser);
34423 rhs1 = cp_parser_simple_cast_expression (parser);
34424 if (rhs1 == error_mark_node)
34426 cp_parser_abort_tentative_parse (parser);
34427 cp_parser_simple_cast_expression (parser);
34428 goto saw_error;
34430 token = cp_lexer_peek_token (parser->lexer);
34431 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34433 cp_parser_abort_tentative_parse (parser);
34434 cp_parser_parse_tentatively (parser);
34435 rhs = cp_parser_binary_expression (parser, false, true,
34436 PREC_NOT_OPERATOR, NULL);
34437 if (rhs == error_mark_node)
34439 cp_parser_abort_tentative_parse (parser);
34440 cp_parser_binary_expression (parser, false, true,
34441 PREC_NOT_OPERATOR, NULL);
34442 goto saw_error;
34444 switch (TREE_CODE (rhs))
34446 case MULT_EXPR:
34447 case TRUNC_DIV_EXPR:
34448 case RDIV_EXPR:
34449 case PLUS_EXPR:
34450 case MINUS_EXPR:
34451 case LSHIFT_EXPR:
34452 case RSHIFT_EXPR:
34453 case BIT_AND_EXPR:
34454 case BIT_IOR_EXPR:
34455 case BIT_XOR_EXPR:
34456 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34458 if (cp_parser_parse_definitely (parser))
34460 opcode = TREE_CODE (rhs);
34461 rhs1 = TREE_OPERAND (rhs, 0);
34462 rhs = TREE_OPERAND (rhs, 1);
34463 goto stmt_done;
34465 else
34466 goto saw_error;
34468 break;
34469 default:
34470 break;
34472 cp_parser_abort_tentative_parse (parser);
34473 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34475 rhs = cp_parser_expression (parser);
34476 if (rhs == error_mark_node)
34477 goto saw_error;
34478 opcode = NOP_EXPR;
34479 rhs1 = NULL_TREE;
34480 goto stmt_done;
34482 cp_parser_error (parser,
34483 "invalid form of %<#pragma omp atomic%>");
34484 goto saw_error;
34486 if (!cp_parser_parse_definitely (parser))
34487 goto saw_error;
34488 switch (token->type)
34490 case CPP_SEMICOLON:
34491 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34493 code = OMP_ATOMIC_CAPTURE_OLD;
34494 v = lhs;
34495 lhs = NULL_TREE;
34496 lhs1 = rhs1;
34497 rhs1 = NULL_TREE;
34498 cp_lexer_consume_token (parser->lexer);
34499 goto restart;
34501 else if (structured_block)
34503 opcode = NOP_EXPR;
34504 rhs = rhs1;
34505 rhs1 = NULL_TREE;
34506 goto stmt_done;
34508 cp_parser_error (parser,
34509 "invalid form of %<#pragma omp atomic%>");
34510 goto saw_error;
34511 case CPP_MULT:
34512 opcode = MULT_EXPR;
34513 break;
34514 case CPP_DIV:
34515 opcode = TRUNC_DIV_EXPR;
34516 break;
34517 case CPP_PLUS:
34518 opcode = PLUS_EXPR;
34519 break;
34520 case CPP_MINUS:
34521 opcode = MINUS_EXPR;
34522 break;
34523 case CPP_LSHIFT:
34524 opcode = LSHIFT_EXPR;
34525 break;
34526 case CPP_RSHIFT:
34527 opcode = RSHIFT_EXPR;
34528 break;
34529 case CPP_AND:
34530 opcode = BIT_AND_EXPR;
34531 break;
34532 case CPP_OR:
34533 opcode = BIT_IOR_EXPR;
34534 break;
34535 case CPP_XOR:
34536 opcode = BIT_XOR_EXPR;
34537 break;
34538 default:
34539 cp_parser_error (parser,
34540 "invalid operator for %<#pragma omp atomic%>");
34541 goto saw_error;
34543 oprec = TOKEN_PRECEDENCE (token);
34544 gcc_assert (oprec != PREC_NOT_OPERATOR);
34545 if (commutative_tree_code (opcode))
34546 oprec = (enum cp_parser_prec) (oprec - 1);
34547 cp_lexer_consume_token (parser->lexer);
34548 rhs = cp_parser_binary_expression (parser, false, false,
34549 oprec, NULL);
34550 if (rhs == error_mark_node)
34551 goto saw_error;
34552 goto stmt_done;
34553 /* FALLTHROUGH */
34554 default:
34555 cp_parser_error (parser,
34556 "invalid operator for %<#pragma omp atomic%>");
34557 goto saw_error;
34559 cp_lexer_consume_token (parser->lexer);
34561 rhs = cp_parser_expression (parser);
34562 if (rhs == error_mark_node)
34563 goto saw_error;
34564 break;
34566 stmt_done:
34567 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34569 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34570 goto saw_error;
34571 v = cp_parser_unary_expression (parser);
34572 if (v == error_mark_node)
34573 goto saw_error;
34574 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34575 goto saw_error;
34576 lhs1 = cp_parser_unary_expression (parser);
34577 if (lhs1 == error_mark_node)
34578 goto saw_error;
34580 if (structured_block)
34582 cp_parser_consume_semicolon_at_end_of_statement (parser);
34583 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34585 done:
34586 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34587 if (!structured_block)
34588 cp_parser_consume_semicolon_at_end_of_statement (parser);
34589 return;
34591 saw_error:
34592 cp_parser_skip_to_end_of_block_or_statement (parser);
34593 if (structured_block)
34595 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34596 cp_lexer_consume_token (parser->lexer);
34597 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34599 cp_parser_skip_to_end_of_block_or_statement (parser);
34600 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34601 cp_lexer_consume_token (parser->lexer);
34607 /* OpenMP 2.5:
34608 # pragma omp barrier new-line */
34610 static void
34611 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34613 cp_parser_require_pragma_eol (parser, pragma_tok);
34614 finish_omp_barrier ();
34617 /* OpenMP 2.5:
34618 # pragma omp critical [(name)] new-line
34619 structured-block
34621 OpenMP 4.5:
34622 # pragma omp critical [(name) [hint(expression)]] new-line
34623 structured-block */
34625 #define OMP_CRITICAL_CLAUSE_MASK \
34626 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34628 static tree
34629 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34631 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34633 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34635 matching_parens parens;
34636 parens.consume_open (parser);
34638 name = cp_parser_identifier (parser);
34640 if (name == error_mark_node
34641 || !parens.require_close (parser))
34642 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34643 /*or_comma=*/false,
34644 /*consume_paren=*/true);
34645 if (name == error_mark_node)
34646 name = NULL;
34648 clauses = cp_parser_omp_all_clauses (parser,
34649 OMP_CRITICAL_CLAUSE_MASK,
34650 "#pragma omp critical", pragma_tok);
34652 else
34653 cp_parser_require_pragma_eol (parser, pragma_tok);
34655 stmt = cp_parser_omp_structured_block (parser, if_p);
34656 return c_finish_omp_critical (input_location, stmt, name, clauses);
34659 /* OpenMP 2.5:
34660 # pragma omp flush flush-vars[opt] new-line
34662 flush-vars:
34663 ( variable-list ) */
34665 static void
34666 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34668 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34669 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34670 cp_parser_require_pragma_eol (parser, pragma_tok);
34672 finish_omp_flush ();
34675 /* Helper function, to parse omp for increment expression. */
34677 static tree
34678 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34680 tree cond = cp_parser_binary_expression (parser, false, true,
34681 PREC_NOT_OPERATOR, NULL);
34682 if (cond == error_mark_node
34683 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34685 cp_parser_skip_to_end_of_statement (parser);
34686 return error_mark_node;
34689 switch (TREE_CODE (cond))
34691 case GT_EXPR:
34692 case GE_EXPR:
34693 case LT_EXPR:
34694 case LE_EXPR:
34695 break;
34696 case NE_EXPR:
34697 /* Fall through: OpenMP disallows NE_EXPR. */
34698 gcc_fallthrough ();
34699 default:
34700 return error_mark_node;
34703 /* If decl is an iterator, preserve LHS and RHS of the relational
34704 expr until finish_omp_for. */
34705 if (decl
34706 && (type_dependent_expression_p (decl)
34707 || CLASS_TYPE_P (TREE_TYPE (decl))))
34708 return cond;
34710 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34711 TREE_CODE (cond),
34712 TREE_OPERAND (cond, 0), ERROR_MARK,
34713 TREE_OPERAND (cond, 1), ERROR_MARK,
34714 /*overload=*/NULL, tf_warning_or_error);
34717 /* Helper function, to parse omp for increment expression. */
34719 static tree
34720 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34722 cp_token *token = cp_lexer_peek_token (parser->lexer);
34723 enum tree_code op;
34724 tree lhs, rhs;
34725 cp_id_kind idk;
34726 bool decl_first;
34728 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34730 op = (token->type == CPP_PLUS_PLUS
34731 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34732 cp_lexer_consume_token (parser->lexer);
34733 lhs = cp_parser_simple_cast_expression (parser);
34734 if (lhs != decl
34735 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34736 return error_mark_node;
34737 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34740 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34741 if (lhs != decl
34742 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34743 return error_mark_node;
34745 token = cp_lexer_peek_token (parser->lexer);
34746 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34748 op = (token->type == CPP_PLUS_PLUS
34749 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34750 cp_lexer_consume_token (parser->lexer);
34751 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34754 op = cp_parser_assignment_operator_opt (parser);
34755 if (op == ERROR_MARK)
34756 return error_mark_node;
34758 if (op != NOP_EXPR)
34760 rhs = cp_parser_assignment_expression (parser);
34761 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34762 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34765 lhs = cp_parser_binary_expression (parser, false, false,
34766 PREC_ADDITIVE_EXPRESSION, NULL);
34767 token = cp_lexer_peek_token (parser->lexer);
34768 decl_first = (lhs == decl
34769 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34770 if (decl_first)
34771 lhs = NULL_TREE;
34772 if (token->type != CPP_PLUS
34773 && token->type != CPP_MINUS)
34774 return error_mark_node;
34778 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34779 cp_lexer_consume_token (parser->lexer);
34780 rhs = cp_parser_binary_expression (parser, false, false,
34781 PREC_ADDITIVE_EXPRESSION, NULL);
34782 token = cp_lexer_peek_token (parser->lexer);
34783 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34785 if (lhs == NULL_TREE)
34787 if (op == PLUS_EXPR)
34788 lhs = rhs;
34789 else
34790 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34791 tf_warning_or_error);
34793 else
34794 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34795 ERROR_MARK, NULL, tf_warning_or_error);
34798 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34800 if (!decl_first)
34802 if ((rhs != decl
34803 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34804 || op == MINUS_EXPR)
34805 return error_mark_node;
34806 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34808 else
34809 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34811 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34814 /* Parse the initialization statement of an OpenMP for loop.
34816 Return true if the resulting construct should have an
34817 OMP_CLAUSE_PRIVATE added to it. */
34819 static tree
34820 cp_parser_omp_for_loop_init (cp_parser *parser,
34821 tree &this_pre_body,
34822 vec<tree, va_gc> *for_block,
34823 tree &init,
34824 tree &orig_init,
34825 tree &decl,
34826 tree &real_decl)
34828 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34829 return NULL_TREE;
34831 tree add_private_clause = NULL_TREE;
34833 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34835 init-expr:
34836 var = lb
34837 integer-type var = lb
34838 random-access-iterator-type var = lb
34839 pointer-type var = lb
34841 cp_decl_specifier_seq type_specifiers;
34843 /* First, try to parse as an initialized declaration. See
34844 cp_parser_condition, from whence the bulk of this is copied. */
34846 cp_parser_parse_tentatively (parser);
34847 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34848 /*is_trailing_return=*/false,
34849 &type_specifiers);
34850 if (cp_parser_parse_definitely (parser))
34852 /* If parsing a type specifier seq succeeded, then this
34853 MUST be a initialized declaration. */
34854 tree asm_specification, attributes;
34855 cp_declarator *declarator;
34857 declarator = cp_parser_declarator (parser,
34858 CP_PARSER_DECLARATOR_NAMED,
34859 /*ctor_dtor_or_conv_p=*/NULL,
34860 /*parenthesized_p=*/NULL,
34861 /*member_p=*/false,
34862 /*friend_p=*/false);
34863 attributes = cp_parser_attributes_opt (parser);
34864 asm_specification = cp_parser_asm_specification_opt (parser);
34866 if (declarator == cp_error_declarator)
34867 cp_parser_skip_to_end_of_statement (parser);
34869 else
34871 tree pushed_scope, auto_node;
34873 decl = start_decl (declarator, &type_specifiers,
34874 SD_INITIALIZED, attributes,
34875 /*prefix_attributes=*/NULL_TREE,
34876 &pushed_scope);
34878 auto_node = type_uses_auto (TREE_TYPE (decl));
34879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34881 if (cp_lexer_next_token_is (parser->lexer,
34882 CPP_OPEN_PAREN))
34883 error ("parenthesized initialization is not allowed in "
34884 "OpenMP %<for%> loop");
34885 else
34886 /* Trigger an error. */
34887 cp_parser_require (parser, CPP_EQ, RT_EQ);
34889 init = error_mark_node;
34890 cp_parser_skip_to_end_of_statement (parser);
34892 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34893 || type_dependent_expression_p (decl)
34894 || auto_node)
34896 bool is_direct_init, is_non_constant_init;
34898 init = cp_parser_initializer (parser,
34899 &is_direct_init,
34900 &is_non_constant_init);
34902 if (auto_node)
34904 TREE_TYPE (decl)
34905 = do_auto_deduction (TREE_TYPE (decl), init,
34906 auto_node);
34908 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34909 && !type_dependent_expression_p (decl))
34910 goto non_class;
34913 cp_finish_decl (decl, init, !is_non_constant_init,
34914 asm_specification,
34915 LOOKUP_ONLYCONVERTING);
34916 orig_init = init;
34917 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34919 vec_safe_push (for_block, this_pre_body);
34920 init = NULL_TREE;
34922 else
34924 init = pop_stmt_list (this_pre_body);
34925 if (init && TREE_CODE (init) == STATEMENT_LIST)
34927 tree_stmt_iterator i = tsi_start (init);
34928 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34929 while (!tsi_end_p (i))
34931 tree t = tsi_stmt (i);
34932 if (TREE_CODE (t) == DECL_EXPR
34933 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34935 tsi_delink (&i);
34936 vec_safe_push (for_block, t);
34937 continue;
34939 break;
34941 if (tsi_one_before_end_p (i))
34943 tree t = tsi_stmt (i);
34944 tsi_delink (&i);
34945 free_stmt_list (init);
34946 init = t;
34950 this_pre_body = NULL_TREE;
34952 else
34954 /* Consume '='. */
34955 cp_lexer_consume_token (parser->lexer);
34956 init = cp_parser_assignment_expression (parser);
34958 non_class:
34959 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34960 init = error_mark_node;
34961 else
34962 cp_finish_decl (decl, NULL_TREE,
34963 /*init_const_expr_p=*/false,
34964 asm_specification,
34965 LOOKUP_ONLYCONVERTING);
34968 if (pushed_scope)
34969 pop_scope (pushed_scope);
34972 else
34974 cp_id_kind idk;
34975 /* If parsing a type specifier sequence failed, then
34976 this MUST be a simple expression. */
34977 cp_parser_parse_tentatively (parser);
34978 decl = cp_parser_primary_expression (parser, false, false,
34979 false, &idk);
34980 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34981 if (!cp_parser_error_occurred (parser)
34982 && decl
34983 && (TREE_CODE (decl) == COMPONENT_REF
34984 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34986 cp_parser_abort_tentative_parse (parser);
34987 cp_parser_parse_tentatively (parser);
34988 cp_token *token = cp_lexer_peek_token (parser->lexer);
34989 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34990 /*check_dependency_p=*/true,
34991 /*template_p=*/NULL,
34992 /*declarator_p=*/false,
34993 /*optional_p=*/false);
34994 if (name != error_mark_node
34995 && last_tok == cp_lexer_peek_token (parser->lexer))
34997 decl = cp_parser_lookup_name_simple (parser, name,
34998 token->location);
34999 if (TREE_CODE (decl) == FIELD_DECL)
35000 add_private_clause = omp_privatize_field (decl, false);
35002 cp_parser_abort_tentative_parse (parser);
35003 cp_parser_parse_tentatively (parser);
35004 decl = cp_parser_primary_expression (parser, false, false,
35005 false, &idk);
35007 if (!cp_parser_error_occurred (parser)
35008 && decl
35009 && DECL_P (decl)
35010 && CLASS_TYPE_P (TREE_TYPE (decl)))
35012 tree rhs;
35014 cp_parser_parse_definitely (parser);
35015 cp_parser_require (parser, CPP_EQ, RT_EQ);
35016 rhs = cp_parser_assignment_expression (parser);
35017 orig_init = rhs;
35018 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35019 decl, NOP_EXPR,
35020 rhs,
35021 tf_warning_or_error));
35022 if (!add_private_clause)
35023 add_private_clause = decl;
35025 else
35027 decl = NULL;
35028 cp_parser_abort_tentative_parse (parser);
35029 init = cp_parser_expression (parser);
35030 if (init)
35032 if (TREE_CODE (init) == MODIFY_EXPR
35033 || TREE_CODE (init) == MODOP_EXPR)
35034 real_decl = TREE_OPERAND (init, 0);
35038 return add_private_clause;
35041 /* Parse the restricted form of the for statement allowed by OpenMP. */
35043 static tree
35044 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35045 tree *cclauses, bool *if_p)
35047 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35048 tree real_decl, initv, condv, incrv, declv;
35049 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35050 location_t loc_first;
35051 bool collapse_err = false;
35052 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35053 vec<tree, va_gc> *for_block = make_tree_vector ();
35054 auto_vec<tree, 4> orig_inits;
35055 bool tiling = false;
35057 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35058 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35059 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35060 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35062 tiling = true;
35063 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35065 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35066 && OMP_CLAUSE_ORDERED_EXPR (cl))
35068 ordered_cl = cl;
35069 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35072 if (ordered && ordered < collapse)
35074 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35075 "%<ordered%> clause parameter is less than %<collapse%>");
35076 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35077 = build_int_cst (NULL_TREE, collapse);
35078 ordered = collapse;
35080 if (ordered)
35082 for (tree *pc = &clauses; *pc; )
35083 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35085 error_at (OMP_CLAUSE_LOCATION (*pc),
35086 "%<linear%> clause may not be specified together "
35087 "with %<ordered%> clause with a parameter");
35088 *pc = OMP_CLAUSE_CHAIN (*pc);
35090 else
35091 pc = &OMP_CLAUSE_CHAIN (*pc);
35094 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35095 count = ordered ? ordered : collapse;
35097 declv = make_tree_vec (count);
35098 initv = make_tree_vec (count);
35099 condv = make_tree_vec (count);
35100 incrv = make_tree_vec (count);
35102 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35104 for (i = 0; i < count; i++)
35106 int bracecount = 0;
35107 tree add_private_clause = NULL_TREE;
35108 location_t loc;
35110 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35112 if (!collapse_err)
35113 cp_parser_error (parser, "for statement expected");
35114 return NULL;
35116 loc = cp_lexer_consume_token (parser->lexer)->location;
35118 matching_parens parens;
35119 if (!parens.require_open (parser))
35120 return NULL;
35122 init = orig_init = decl = real_decl = NULL;
35123 this_pre_body = push_stmt_list ();
35125 add_private_clause
35126 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35127 init, orig_init, decl, real_decl);
35129 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35130 if (this_pre_body)
35132 this_pre_body = pop_stmt_list (this_pre_body);
35133 if (pre_body)
35135 tree t = pre_body;
35136 pre_body = push_stmt_list ();
35137 add_stmt (t);
35138 add_stmt (this_pre_body);
35139 pre_body = pop_stmt_list (pre_body);
35141 else
35142 pre_body = this_pre_body;
35145 if (decl)
35146 real_decl = decl;
35147 if (cclauses != NULL
35148 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35149 && real_decl != NULL_TREE)
35151 tree *c;
35152 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35153 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35154 && OMP_CLAUSE_DECL (*c) == real_decl)
35156 error_at (loc, "iteration variable %qD"
35157 " should not be firstprivate", real_decl);
35158 *c = OMP_CLAUSE_CHAIN (*c);
35160 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35161 && OMP_CLAUSE_DECL (*c) == real_decl)
35163 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35164 tree l = *c;
35165 *c = OMP_CLAUSE_CHAIN (*c);
35166 if (code == OMP_SIMD)
35168 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35169 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35171 else
35173 OMP_CLAUSE_CHAIN (l) = clauses;
35174 clauses = l;
35176 add_private_clause = NULL_TREE;
35178 else
35180 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35181 && OMP_CLAUSE_DECL (*c) == real_decl)
35182 add_private_clause = NULL_TREE;
35183 c = &OMP_CLAUSE_CHAIN (*c);
35187 if (add_private_clause)
35189 tree c;
35190 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35192 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35193 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35194 && OMP_CLAUSE_DECL (c) == decl)
35195 break;
35196 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35197 && OMP_CLAUSE_DECL (c) == decl)
35198 error_at (loc, "iteration variable %qD "
35199 "should not be firstprivate",
35200 decl);
35201 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35202 && OMP_CLAUSE_DECL (c) == decl)
35203 error_at (loc, "iteration variable %qD should not be reduction",
35204 decl);
35206 if (c == NULL)
35208 if (code != OMP_SIMD)
35209 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35210 else if (collapse == 1)
35211 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35212 else
35213 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35214 OMP_CLAUSE_DECL (c) = add_private_clause;
35215 c = finish_omp_clauses (c, C_ORT_OMP);
35216 if (c)
35218 OMP_CLAUSE_CHAIN (c) = clauses;
35219 clauses = c;
35220 /* For linear, signal that we need to fill up
35221 the so far unknown linear step. */
35222 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35223 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35228 cond = NULL;
35229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35230 cond = cp_parser_omp_for_cond (parser, decl);
35231 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35233 incr = NULL;
35234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35236 /* If decl is an iterator, preserve the operator on decl
35237 until finish_omp_for. */
35238 if (real_decl
35239 && ((processing_template_decl
35240 && (TREE_TYPE (real_decl) == NULL_TREE
35241 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35242 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35243 incr = cp_parser_omp_for_incr (parser, real_decl);
35244 else
35245 incr = cp_parser_expression (parser);
35246 if (!EXPR_HAS_LOCATION (incr))
35247 protected_set_expr_location (incr, input_location);
35250 if (!parens.require_close (parser))
35251 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35252 /*or_comma=*/false,
35253 /*consume_paren=*/true);
35255 TREE_VEC_ELT (declv, i) = decl;
35256 TREE_VEC_ELT (initv, i) = init;
35257 TREE_VEC_ELT (condv, i) = cond;
35258 TREE_VEC_ELT (incrv, i) = incr;
35259 if (orig_init)
35261 orig_inits.safe_grow_cleared (i + 1);
35262 orig_inits[i] = orig_init;
35265 if (i == count - 1)
35266 break;
35268 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35269 in between the collapsed for loops to be still considered perfectly
35270 nested. Hopefully the final version clarifies this.
35271 For now handle (multiple) {'s and empty statements. */
35272 cp_parser_parse_tentatively (parser);
35273 for (;;)
35275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35276 break;
35277 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35279 cp_lexer_consume_token (parser->lexer);
35280 bracecount++;
35282 else if (bracecount
35283 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35284 cp_lexer_consume_token (parser->lexer);
35285 else
35287 loc = cp_lexer_peek_token (parser->lexer)->location;
35288 error_at (loc, "not enough for loops to collapse");
35289 collapse_err = true;
35290 cp_parser_abort_tentative_parse (parser);
35291 declv = NULL_TREE;
35292 break;
35296 if (declv)
35298 cp_parser_parse_definitely (parser);
35299 nbraces += bracecount;
35303 if (nbraces)
35304 if_p = NULL;
35306 /* Note that we saved the original contents of this flag when we entered
35307 the structured block, and so we don't need to re-save it here. */
35308 parser->in_statement = IN_OMP_FOR;
35310 /* Note that the grammar doesn't call for a structured block here,
35311 though the loop as a whole is a structured block. */
35312 body = push_stmt_list ();
35313 cp_parser_statement (parser, NULL_TREE, false, if_p);
35314 body = pop_stmt_list (body);
35316 if (declv == NULL_TREE)
35317 ret = NULL_TREE;
35318 else
35319 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35320 body, pre_body, &orig_inits, clauses);
35322 while (nbraces)
35324 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35326 cp_lexer_consume_token (parser->lexer);
35327 nbraces--;
35329 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35330 cp_lexer_consume_token (parser->lexer);
35331 else
35333 if (!collapse_err)
35335 error_at (cp_lexer_peek_token (parser->lexer)->location,
35336 "collapsed loops not perfectly nested");
35338 collapse_err = true;
35339 cp_parser_statement_seq_opt (parser, NULL);
35340 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35341 break;
35345 while (!for_block->is_empty ())
35347 tree t = for_block->pop ();
35348 if (TREE_CODE (t) == STATEMENT_LIST)
35349 add_stmt (pop_stmt_list (t));
35350 else
35351 add_stmt (t);
35353 release_tree_vector (for_block);
35355 return ret;
35358 /* Helper function for OpenMP parsing, split clauses and call
35359 finish_omp_clauses on each of the set of clauses afterwards. */
35361 static void
35362 cp_omp_split_clauses (location_t loc, enum tree_code code,
35363 omp_clause_mask mask, tree clauses, tree *cclauses)
35365 int i;
35366 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35367 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35368 if (cclauses[i])
35369 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35372 /* OpenMP 4.0:
35373 #pragma omp simd simd-clause[optseq] new-line
35374 for-loop */
35376 #define OMP_SIMD_CLAUSE_MASK \
35377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35386 static tree
35387 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35388 char *p_name, omp_clause_mask mask, tree *cclauses,
35389 bool *if_p)
35391 tree clauses, sb, ret;
35392 unsigned int save;
35393 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35395 strcat (p_name, " simd");
35396 mask |= OMP_SIMD_CLAUSE_MASK;
35398 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35399 cclauses == NULL);
35400 if (cclauses)
35402 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35403 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35404 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35405 OMP_CLAUSE_ORDERED);
35406 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35408 error_at (OMP_CLAUSE_LOCATION (c),
35409 "%<ordered%> clause with parameter may not be specified "
35410 "on %qs construct", p_name);
35411 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35415 sb = begin_omp_structured_block ();
35416 save = cp_parser_begin_omp_structured_block (parser);
35418 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35420 cp_parser_end_omp_structured_block (parser, save);
35421 add_stmt (finish_omp_structured_block (sb));
35423 return ret;
35426 /* OpenMP 2.5:
35427 #pragma omp for for-clause[optseq] new-line
35428 for-loop
35430 OpenMP 4.0:
35431 #pragma omp for simd for-simd-clause[optseq] new-line
35432 for-loop */
35434 #define OMP_FOR_CLAUSE_MASK \
35435 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35445 static tree
35446 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35447 char *p_name, omp_clause_mask mask, tree *cclauses,
35448 bool *if_p)
35450 tree clauses, sb, ret;
35451 unsigned int save;
35452 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35454 strcat (p_name, " for");
35455 mask |= OMP_FOR_CLAUSE_MASK;
35456 /* parallel for{, simd} disallows nowait clause, but for
35457 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35458 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35459 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35460 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35461 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35462 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35464 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35466 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35467 const char *p = IDENTIFIER_POINTER (id);
35469 if (strcmp (p, "simd") == 0)
35471 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35472 if (cclauses == NULL)
35473 cclauses = cclauses_buf;
35475 cp_lexer_consume_token (parser->lexer);
35476 if (!flag_openmp) /* flag_openmp_simd */
35477 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35478 cclauses, if_p);
35479 sb = begin_omp_structured_block ();
35480 save = cp_parser_begin_omp_structured_block (parser);
35481 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35482 cclauses, if_p);
35483 cp_parser_end_omp_structured_block (parser, save);
35484 tree body = finish_omp_structured_block (sb);
35485 if (ret == NULL)
35486 return ret;
35487 ret = make_node (OMP_FOR);
35488 TREE_TYPE (ret) = void_type_node;
35489 OMP_FOR_BODY (ret) = body;
35490 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35491 SET_EXPR_LOCATION (ret, loc);
35492 add_stmt (ret);
35493 return ret;
35496 if (!flag_openmp) /* flag_openmp_simd */
35498 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35499 return NULL_TREE;
35502 /* Composite distribute parallel for disallows linear clause. */
35503 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35504 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35506 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35507 cclauses == NULL);
35508 if (cclauses)
35510 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35511 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35514 sb = begin_omp_structured_block ();
35515 save = cp_parser_begin_omp_structured_block (parser);
35517 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35519 cp_parser_end_omp_structured_block (parser, save);
35520 add_stmt (finish_omp_structured_block (sb));
35522 return ret;
35525 /* OpenMP 2.5:
35526 # pragma omp master new-line
35527 structured-block */
35529 static tree
35530 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35532 cp_parser_require_pragma_eol (parser, pragma_tok);
35533 return c_finish_omp_master (input_location,
35534 cp_parser_omp_structured_block (parser, if_p));
35537 /* OpenMP 2.5:
35538 # pragma omp ordered new-line
35539 structured-block
35541 OpenMP 4.5:
35542 # pragma omp ordered ordered-clauses new-line
35543 structured-block */
35545 #define OMP_ORDERED_CLAUSE_MASK \
35546 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35549 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35550 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35552 static bool
35553 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35554 enum pragma_context context, bool *if_p)
35556 location_t loc = pragma_tok->location;
35558 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35560 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35561 const char *p = IDENTIFIER_POINTER (id);
35563 if (strcmp (p, "depend") == 0)
35565 if (!flag_openmp) /* flag_openmp_simd */
35567 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35568 return false;
35570 if (context == pragma_stmt)
35572 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35573 "%<depend%> clause may only be used in compound "
35574 "statements");
35575 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35576 return false;
35578 tree clauses
35579 = cp_parser_omp_all_clauses (parser,
35580 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35581 "#pragma omp ordered", pragma_tok);
35582 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35583 return false;
35587 tree clauses
35588 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35589 "#pragma omp ordered", pragma_tok);
35591 if (!flag_openmp /* flag_openmp_simd */
35592 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35593 return false;
35595 c_finish_omp_ordered (loc, clauses,
35596 cp_parser_omp_structured_block (parser, if_p));
35597 return true;
35600 /* OpenMP 2.5:
35602 section-scope:
35603 { section-sequence }
35605 section-sequence:
35606 section-directive[opt] structured-block
35607 section-sequence section-directive structured-block */
35609 static tree
35610 cp_parser_omp_sections_scope (cp_parser *parser)
35612 tree stmt, substmt;
35613 bool error_suppress = false;
35614 cp_token *tok;
35616 matching_braces braces;
35617 if (!braces.require_open (parser))
35618 return NULL_TREE;
35620 stmt = push_stmt_list ();
35622 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35623 != PRAGMA_OMP_SECTION)
35625 substmt = cp_parser_omp_structured_block (parser, NULL);
35626 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35627 add_stmt (substmt);
35630 while (1)
35632 tok = cp_lexer_peek_token (parser->lexer);
35633 if (tok->type == CPP_CLOSE_BRACE)
35634 break;
35635 if (tok->type == CPP_EOF)
35636 break;
35638 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35640 cp_lexer_consume_token (parser->lexer);
35641 cp_parser_require_pragma_eol (parser, tok);
35642 error_suppress = false;
35644 else if (!error_suppress)
35646 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35647 error_suppress = true;
35650 substmt = cp_parser_omp_structured_block (parser, NULL);
35651 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35652 add_stmt (substmt);
35654 braces.require_close (parser);
35656 substmt = pop_stmt_list (stmt);
35658 stmt = make_node (OMP_SECTIONS);
35659 TREE_TYPE (stmt) = void_type_node;
35660 OMP_SECTIONS_BODY (stmt) = substmt;
35662 add_stmt (stmt);
35663 return stmt;
35666 /* OpenMP 2.5:
35667 # pragma omp sections sections-clause[optseq] newline
35668 sections-scope */
35670 #define OMP_SECTIONS_CLAUSE_MASK \
35671 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35677 static tree
35678 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35679 char *p_name, omp_clause_mask mask, tree *cclauses)
35681 tree clauses, ret;
35682 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35684 strcat (p_name, " sections");
35685 mask |= OMP_SECTIONS_CLAUSE_MASK;
35686 if (cclauses)
35687 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35689 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35690 cclauses == NULL);
35691 if (cclauses)
35693 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35694 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35697 ret = cp_parser_omp_sections_scope (parser);
35698 if (ret)
35699 OMP_SECTIONS_CLAUSES (ret) = clauses;
35701 return ret;
35704 /* OpenMP 2.5:
35705 # pragma omp parallel parallel-clause[optseq] new-line
35706 structured-block
35707 # pragma omp parallel for parallel-for-clause[optseq] new-line
35708 structured-block
35709 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35710 structured-block
35712 OpenMP 4.0:
35713 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35714 structured-block */
35716 #define OMP_PARALLEL_CLAUSE_MASK \
35717 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35727 static tree
35728 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35729 char *p_name, omp_clause_mask mask, tree *cclauses,
35730 bool *if_p)
35732 tree stmt, clauses, block;
35733 unsigned int save;
35734 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35736 strcat (p_name, " parallel");
35737 mask |= OMP_PARALLEL_CLAUSE_MASK;
35738 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35739 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35740 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35741 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35745 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35746 if (cclauses == NULL)
35747 cclauses = cclauses_buf;
35749 cp_lexer_consume_token (parser->lexer);
35750 if (!flag_openmp) /* flag_openmp_simd */
35751 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35752 if_p);
35753 block = begin_omp_parallel ();
35754 save = cp_parser_begin_omp_structured_block (parser);
35755 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35756 if_p);
35757 cp_parser_end_omp_structured_block (parser, save);
35758 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35759 block);
35760 if (ret == NULL_TREE)
35761 return ret;
35762 OMP_PARALLEL_COMBINED (stmt) = 1;
35763 return stmt;
35765 /* When combined with distribute, parallel has to be followed by for.
35766 #pragma omp target parallel is allowed though. */
35767 else if (cclauses
35768 && (mask & (OMP_CLAUSE_MASK_1
35769 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35771 error_at (loc, "expected %<for%> after %qs", p_name);
35772 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35773 return NULL_TREE;
35775 else if (!flag_openmp) /* flag_openmp_simd */
35777 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35778 return NULL_TREE;
35780 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35782 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35783 const char *p = IDENTIFIER_POINTER (id);
35784 if (strcmp (p, "sections") == 0)
35786 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35787 cclauses = cclauses_buf;
35789 cp_lexer_consume_token (parser->lexer);
35790 block = begin_omp_parallel ();
35791 save = cp_parser_begin_omp_structured_block (parser);
35792 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35793 cp_parser_end_omp_structured_block (parser, save);
35794 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35795 block);
35796 OMP_PARALLEL_COMBINED (stmt) = 1;
35797 return stmt;
35801 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35802 cclauses == NULL);
35803 if (cclauses)
35805 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35806 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35809 block = begin_omp_parallel ();
35810 save = cp_parser_begin_omp_structured_block (parser);
35811 cp_parser_statement (parser, NULL_TREE, false, if_p);
35812 cp_parser_end_omp_structured_block (parser, save);
35813 stmt = finish_omp_parallel (clauses, block);
35814 return stmt;
35817 /* OpenMP 2.5:
35818 # pragma omp single single-clause[optseq] new-line
35819 structured-block */
35821 #define OMP_SINGLE_CLAUSE_MASK \
35822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35827 static tree
35828 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35830 tree stmt = make_node (OMP_SINGLE);
35831 TREE_TYPE (stmt) = void_type_node;
35833 OMP_SINGLE_CLAUSES (stmt)
35834 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35835 "#pragma omp single", pragma_tok);
35836 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35838 return add_stmt (stmt);
35841 /* OpenMP 3.0:
35842 # pragma omp task task-clause[optseq] new-line
35843 structured-block */
35845 #define OMP_TASK_CLAUSE_MASK \
35846 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35857 static tree
35858 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35860 tree clauses, block;
35861 unsigned int save;
35863 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35864 "#pragma omp task", pragma_tok);
35865 block = begin_omp_task ();
35866 save = cp_parser_begin_omp_structured_block (parser);
35867 cp_parser_statement (parser, NULL_TREE, false, if_p);
35868 cp_parser_end_omp_structured_block (parser, save);
35869 return finish_omp_task (clauses, block);
35872 /* OpenMP 3.0:
35873 # pragma omp taskwait new-line */
35875 static void
35876 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35878 cp_parser_require_pragma_eol (parser, pragma_tok);
35879 finish_omp_taskwait ();
35882 /* OpenMP 3.1:
35883 # pragma omp taskyield new-line */
35885 static void
35886 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35888 cp_parser_require_pragma_eol (parser, pragma_tok);
35889 finish_omp_taskyield ();
35892 /* OpenMP 4.0:
35893 # pragma omp taskgroup new-line
35894 structured-block */
35896 static tree
35897 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35899 cp_parser_require_pragma_eol (parser, pragma_tok);
35900 return c_finish_omp_taskgroup (input_location,
35901 cp_parser_omp_structured_block (parser,
35902 if_p));
35906 /* OpenMP 2.5:
35907 # pragma omp threadprivate (variable-list) */
35909 static void
35910 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35912 tree vars;
35914 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35915 cp_parser_require_pragma_eol (parser, pragma_tok);
35917 finish_omp_threadprivate (vars);
35920 /* OpenMP 4.0:
35921 # pragma omp cancel cancel-clause[optseq] new-line */
35923 #define OMP_CANCEL_CLAUSE_MASK \
35924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35930 static void
35931 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35933 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35934 "#pragma omp cancel", pragma_tok);
35935 finish_omp_cancel (clauses);
35938 /* OpenMP 4.0:
35939 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35941 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35947 static void
35948 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35949 enum pragma_context context)
35951 tree clauses;
35952 bool point_seen = false;
35954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35956 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35957 const char *p = IDENTIFIER_POINTER (id);
35959 if (strcmp (p, "point") == 0)
35961 cp_lexer_consume_token (parser->lexer);
35962 point_seen = true;
35965 if (!point_seen)
35967 cp_parser_error (parser, "expected %<point%>");
35968 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35969 return;
35972 if (context != pragma_compound)
35974 if (context == pragma_stmt)
35975 error_at (pragma_tok->location,
35976 "%<#pragma %s%> may only be used in compound statements",
35977 "omp cancellation point");
35978 else
35979 cp_parser_error (parser, "expected declaration specifiers");
35980 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35981 return;
35984 clauses = cp_parser_omp_all_clauses (parser,
35985 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35986 "#pragma omp cancellation point",
35987 pragma_tok);
35988 finish_omp_cancellation_point (clauses);
35991 /* OpenMP 4.0:
35992 #pragma omp distribute distribute-clause[optseq] new-line
35993 for-loop */
35995 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35996 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36002 static tree
36003 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36004 char *p_name, omp_clause_mask mask, tree *cclauses,
36005 bool *if_p)
36007 tree clauses, sb, ret;
36008 unsigned int save;
36009 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36011 strcat (p_name, " distribute");
36012 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36014 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36016 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36017 const char *p = IDENTIFIER_POINTER (id);
36018 bool simd = false;
36019 bool parallel = false;
36021 if (strcmp (p, "simd") == 0)
36022 simd = true;
36023 else
36024 parallel = strcmp (p, "parallel") == 0;
36025 if (parallel || simd)
36027 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36028 if (cclauses == NULL)
36029 cclauses = cclauses_buf;
36030 cp_lexer_consume_token (parser->lexer);
36031 if (!flag_openmp) /* flag_openmp_simd */
36033 if (simd)
36034 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36035 cclauses, if_p);
36036 else
36037 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36038 cclauses, if_p);
36040 sb = begin_omp_structured_block ();
36041 save = cp_parser_begin_omp_structured_block (parser);
36042 if (simd)
36043 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36044 cclauses, if_p);
36045 else
36046 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36047 cclauses, if_p);
36048 cp_parser_end_omp_structured_block (parser, save);
36049 tree body = finish_omp_structured_block (sb);
36050 if (ret == NULL)
36051 return ret;
36052 ret = make_node (OMP_DISTRIBUTE);
36053 TREE_TYPE (ret) = void_type_node;
36054 OMP_FOR_BODY (ret) = body;
36055 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36056 SET_EXPR_LOCATION (ret, loc);
36057 add_stmt (ret);
36058 return ret;
36061 if (!flag_openmp) /* flag_openmp_simd */
36063 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36064 return NULL_TREE;
36067 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36068 cclauses == NULL);
36069 if (cclauses)
36071 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36072 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36075 sb = begin_omp_structured_block ();
36076 save = cp_parser_begin_omp_structured_block (parser);
36078 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36080 cp_parser_end_omp_structured_block (parser, save);
36081 add_stmt (finish_omp_structured_block (sb));
36083 return ret;
36086 /* OpenMP 4.0:
36087 # pragma omp teams teams-clause[optseq] new-line
36088 structured-block */
36090 #define OMP_TEAMS_CLAUSE_MASK \
36091 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36099 static tree
36100 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36101 char *p_name, omp_clause_mask mask, tree *cclauses,
36102 bool *if_p)
36104 tree clauses, sb, ret;
36105 unsigned int save;
36106 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36108 strcat (p_name, " teams");
36109 mask |= OMP_TEAMS_CLAUSE_MASK;
36111 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36113 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36114 const char *p = IDENTIFIER_POINTER (id);
36115 if (strcmp (p, "distribute") == 0)
36117 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36118 if (cclauses == NULL)
36119 cclauses = cclauses_buf;
36121 cp_lexer_consume_token (parser->lexer);
36122 if (!flag_openmp) /* flag_openmp_simd */
36123 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36124 cclauses, if_p);
36125 sb = begin_omp_structured_block ();
36126 save = cp_parser_begin_omp_structured_block (parser);
36127 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36128 cclauses, if_p);
36129 cp_parser_end_omp_structured_block (parser, save);
36130 tree body = finish_omp_structured_block (sb);
36131 if (ret == NULL)
36132 return ret;
36133 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36134 ret = make_node (OMP_TEAMS);
36135 TREE_TYPE (ret) = void_type_node;
36136 OMP_TEAMS_CLAUSES (ret) = clauses;
36137 OMP_TEAMS_BODY (ret) = body;
36138 OMP_TEAMS_COMBINED (ret) = 1;
36139 SET_EXPR_LOCATION (ret, loc);
36140 return add_stmt (ret);
36143 if (!flag_openmp) /* flag_openmp_simd */
36145 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36146 return NULL_TREE;
36149 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36150 cclauses == NULL);
36151 if (cclauses)
36153 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36154 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36157 tree stmt = make_node (OMP_TEAMS);
36158 TREE_TYPE (stmt) = void_type_node;
36159 OMP_TEAMS_CLAUSES (stmt) = clauses;
36160 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36161 SET_EXPR_LOCATION (stmt, loc);
36163 return add_stmt (stmt);
36166 /* OpenMP 4.0:
36167 # pragma omp target data target-data-clause[optseq] new-line
36168 structured-block */
36170 #define OMP_TARGET_DATA_CLAUSE_MASK \
36171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36176 static tree
36177 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36179 tree clauses
36180 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36181 "#pragma omp target data", pragma_tok);
36182 int map_seen = 0;
36183 for (tree *pc = &clauses; *pc;)
36185 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36186 switch (OMP_CLAUSE_MAP_KIND (*pc))
36188 case GOMP_MAP_TO:
36189 case GOMP_MAP_ALWAYS_TO:
36190 case GOMP_MAP_FROM:
36191 case GOMP_MAP_ALWAYS_FROM:
36192 case GOMP_MAP_TOFROM:
36193 case GOMP_MAP_ALWAYS_TOFROM:
36194 case GOMP_MAP_ALLOC:
36195 map_seen = 3;
36196 break;
36197 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36198 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36199 case GOMP_MAP_ALWAYS_POINTER:
36200 break;
36201 default:
36202 map_seen |= 1;
36203 error_at (OMP_CLAUSE_LOCATION (*pc),
36204 "%<#pragma omp target data%> with map-type other "
36205 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36206 "on %<map%> clause");
36207 *pc = OMP_CLAUSE_CHAIN (*pc);
36208 continue;
36210 pc = &OMP_CLAUSE_CHAIN (*pc);
36213 if (map_seen != 3)
36215 if (map_seen == 0)
36216 error_at (pragma_tok->location,
36217 "%<#pragma omp target data%> must contain at least "
36218 "one %<map%> clause");
36219 return NULL_TREE;
36222 tree stmt = make_node (OMP_TARGET_DATA);
36223 TREE_TYPE (stmt) = void_type_node;
36224 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36226 keep_next_level (true);
36227 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36229 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36230 return add_stmt (stmt);
36233 /* OpenMP 4.5:
36234 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36235 structured-block */
36237 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36238 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36244 static tree
36245 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36246 enum pragma_context context)
36248 bool data_seen = false;
36249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36252 const char *p = IDENTIFIER_POINTER (id);
36254 if (strcmp (p, "data") == 0)
36256 cp_lexer_consume_token (parser->lexer);
36257 data_seen = true;
36260 if (!data_seen)
36262 cp_parser_error (parser, "expected %<data%>");
36263 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36264 return NULL_TREE;
36267 if (context == pragma_stmt)
36269 error_at (pragma_tok->location,
36270 "%<#pragma %s%> may only be used in compound statements",
36271 "omp target enter data");
36272 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36273 return NULL_TREE;
36276 tree clauses
36277 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36278 "#pragma omp target enter data", pragma_tok);
36279 int map_seen = 0;
36280 for (tree *pc = &clauses; *pc;)
36282 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36283 switch (OMP_CLAUSE_MAP_KIND (*pc))
36285 case GOMP_MAP_TO:
36286 case GOMP_MAP_ALWAYS_TO:
36287 case GOMP_MAP_ALLOC:
36288 map_seen = 3;
36289 break;
36290 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36291 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36292 case GOMP_MAP_ALWAYS_POINTER:
36293 break;
36294 default:
36295 map_seen |= 1;
36296 error_at (OMP_CLAUSE_LOCATION (*pc),
36297 "%<#pragma omp target enter data%> with map-type other "
36298 "than %<to%> or %<alloc%> on %<map%> clause");
36299 *pc = OMP_CLAUSE_CHAIN (*pc);
36300 continue;
36302 pc = &OMP_CLAUSE_CHAIN (*pc);
36305 if (map_seen != 3)
36307 if (map_seen == 0)
36308 error_at (pragma_tok->location,
36309 "%<#pragma omp target enter data%> must contain at least "
36310 "one %<map%> clause");
36311 return NULL_TREE;
36314 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36315 TREE_TYPE (stmt) = void_type_node;
36316 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36317 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36318 return add_stmt (stmt);
36321 /* OpenMP 4.5:
36322 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36323 structured-block */
36325 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36326 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36332 static tree
36333 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36334 enum pragma_context context)
36336 bool data_seen = false;
36337 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36339 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36340 const char *p = IDENTIFIER_POINTER (id);
36342 if (strcmp (p, "data") == 0)
36344 cp_lexer_consume_token (parser->lexer);
36345 data_seen = true;
36348 if (!data_seen)
36350 cp_parser_error (parser, "expected %<data%>");
36351 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36352 return NULL_TREE;
36355 if (context == pragma_stmt)
36357 error_at (pragma_tok->location,
36358 "%<#pragma %s%> may only be used in compound statements",
36359 "omp target exit data");
36360 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36361 return NULL_TREE;
36364 tree clauses
36365 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36366 "#pragma omp target exit data", pragma_tok);
36367 int map_seen = 0;
36368 for (tree *pc = &clauses; *pc;)
36370 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36371 switch (OMP_CLAUSE_MAP_KIND (*pc))
36373 case GOMP_MAP_FROM:
36374 case GOMP_MAP_ALWAYS_FROM:
36375 case GOMP_MAP_RELEASE:
36376 case GOMP_MAP_DELETE:
36377 map_seen = 3;
36378 break;
36379 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36380 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36381 case GOMP_MAP_ALWAYS_POINTER:
36382 break;
36383 default:
36384 map_seen |= 1;
36385 error_at (OMP_CLAUSE_LOCATION (*pc),
36386 "%<#pragma omp target exit data%> with map-type other "
36387 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36388 " clause");
36389 *pc = OMP_CLAUSE_CHAIN (*pc);
36390 continue;
36392 pc = &OMP_CLAUSE_CHAIN (*pc);
36395 if (map_seen != 3)
36397 if (map_seen == 0)
36398 error_at (pragma_tok->location,
36399 "%<#pragma omp target exit data%> must contain at least "
36400 "one %<map%> clause");
36401 return NULL_TREE;
36404 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36405 TREE_TYPE (stmt) = void_type_node;
36406 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36407 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36408 return add_stmt (stmt);
36411 /* OpenMP 4.0:
36412 # pragma omp target update target-update-clause[optseq] new-line */
36414 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36422 static bool
36423 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36424 enum pragma_context context)
36426 if (context == pragma_stmt)
36428 error_at (pragma_tok->location,
36429 "%<#pragma %s%> may only be used in compound statements",
36430 "omp target update");
36431 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36432 return false;
36435 tree clauses
36436 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36437 "#pragma omp target update", pragma_tok);
36438 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36439 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36441 error_at (pragma_tok->location,
36442 "%<#pragma omp target update%> must contain at least one "
36443 "%<from%> or %<to%> clauses");
36444 return false;
36447 tree stmt = make_node (OMP_TARGET_UPDATE);
36448 TREE_TYPE (stmt) = void_type_node;
36449 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36450 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36451 add_stmt (stmt);
36452 return false;
36455 /* OpenMP 4.0:
36456 # pragma omp target target-clause[optseq] new-line
36457 structured-block */
36459 #define OMP_TARGET_CLAUSE_MASK \
36460 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36470 static bool
36471 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36472 enum pragma_context context, bool *if_p)
36474 tree *pc = NULL, stmt;
36476 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36478 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36479 const char *p = IDENTIFIER_POINTER (id);
36480 enum tree_code ccode = ERROR_MARK;
36482 if (strcmp (p, "teams") == 0)
36483 ccode = OMP_TEAMS;
36484 else if (strcmp (p, "parallel") == 0)
36485 ccode = OMP_PARALLEL;
36486 else if (strcmp (p, "simd") == 0)
36487 ccode = OMP_SIMD;
36488 if (ccode != ERROR_MARK)
36490 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36491 char p_name[sizeof ("#pragma omp target teams distribute "
36492 "parallel for simd")];
36494 cp_lexer_consume_token (parser->lexer);
36495 strcpy (p_name, "#pragma omp target");
36496 if (!flag_openmp) /* flag_openmp_simd */
36498 tree stmt;
36499 switch (ccode)
36501 case OMP_TEAMS:
36502 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36503 OMP_TARGET_CLAUSE_MASK,
36504 cclauses, if_p);
36505 break;
36506 case OMP_PARALLEL:
36507 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36508 OMP_TARGET_CLAUSE_MASK,
36509 cclauses, if_p);
36510 break;
36511 case OMP_SIMD:
36512 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36513 OMP_TARGET_CLAUSE_MASK,
36514 cclauses, if_p);
36515 break;
36516 default:
36517 gcc_unreachable ();
36519 return stmt != NULL_TREE;
36521 keep_next_level (true);
36522 tree sb = begin_omp_structured_block (), ret;
36523 unsigned save = cp_parser_begin_omp_structured_block (parser);
36524 switch (ccode)
36526 case OMP_TEAMS:
36527 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36528 OMP_TARGET_CLAUSE_MASK, cclauses,
36529 if_p);
36530 break;
36531 case OMP_PARALLEL:
36532 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36533 OMP_TARGET_CLAUSE_MASK, cclauses,
36534 if_p);
36535 break;
36536 case OMP_SIMD:
36537 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36538 OMP_TARGET_CLAUSE_MASK, cclauses,
36539 if_p);
36540 break;
36541 default:
36542 gcc_unreachable ();
36544 cp_parser_end_omp_structured_block (parser, save);
36545 tree body = finish_omp_structured_block (sb);
36546 if (ret == NULL_TREE)
36547 return false;
36548 if (ccode == OMP_TEAMS && !processing_template_decl)
36550 /* For combined target teams, ensure the num_teams and
36551 thread_limit clause expressions are evaluated on the host,
36552 before entering the target construct. */
36553 tree c;
36554 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36555 c; c = OMP_CLAUSE_CHAIN (c))
36556 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36557 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36558 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36560 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36561 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36562 if (expr == error_mark_node)
36563 continue;
36564 tree tmp = TARGET_EXPR_SLOT (expr);
36565 add_stmt (expr);
36566 OMP_CLAUSE_OPERAND (c, 0) = expr;
36567 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36568 OMP_CLAUSE_FIRSTPRIVATE);
36569 OMP_CLAUSE_DECL (tc) = tmp;
36570 OMP_CLAUSE_CHAIN (tc)
36571 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36572 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36575 tree stmt = make_node (OMP_TARGET);
36576 TREE_TYPE (stmt) = void_type_node;
36577 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36578 OMP_TARGET_BODY (stmt) = body;
36579 OMP_TARGET_COMBINED (stmt) = 1;
36580 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36581 add_stmt (stmt);
36582 pc = &OMP_TARGET_CLAUSES (stmt);
36583 goto check_clauses;
36585 else if (!flag_openmp) /* flag_openmp_simd */
36587 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36588 return false;
36590 else if (strcmp (p, "data") == 0)
36592 cp_lexer_consume_token (parser->lexer);
36593 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36594 return true;
36596 else if (strcmp (p, "enter") == 0)
36598 cp_lexer_consume_token (parser->lexer);
36599 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36600 return false;
36602 else if (strcmp (p, "exit") == 0)
36604 cp_lexer_consume_token (parser->lexer);
36605 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36606 return false;
36608 else if (strcmp (p, "update") == 0)
36610 cp_lexer_consume_token (parser->lexer);
36611 return cp_parser_omp_target_update (parser, pragma_tok, context);
36614 if (!flag_openmp) /* flag_openmp_simd */
36616 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36617 return false;
36620 stmt = make_node (OMP_TARGET);
36621 TREE_TYPE (stmt) = void_type_node;
36623 OMP_TARGET_CLAUSES (stmt)
36624 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36625 "#pragma omp target", pragma_tok);
36626 pc = &OMP_TARGET_CLAUSES (stmt);
36627 keep_next_level (true);
36628 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36630 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36631 add_stmt (stmt);
36633 check_clauses:
36634 while (*pc)
36636 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36637 switch (OMP_CLAUSE_MAP_KIND (*pc))
36639 case GOMP_MAP_TO:
36640 case GOMP_MAP_ALWAYS_TO:
36641 case GOMP_MAP_FROM:
36642 case GOMP_MAP_ALWAYS_FROM:
36643 case GOMP_MAP_TOFROM:
36644 case GOMP_MAP_ALWAYS_TOFROM:
36645 case GOMP_MAP_ALLOC:
36646 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36647 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36648 case GOMP_MAP_ALWAYS_POINTER:
36649 break;
36650 default:
36651 error_at (OMP_CLAUSE_LOCATION (*pc),
36652 "%<#pragma omp target%> with map-type other "
36653 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36654 "on %<map%> clause");
36655 *pc = OMP_CLAUSE_CHAIN (*pc);
36656 continue;
36658 pc = &OMP_CLAUSE_CHAIN (*pc);
36660 return true;
36663 /* OpenACC 2.0:
36664 # pragma acc cache (variable-list) new-line
36667 static tree
36668 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36670 tree stmt, clauses;
36672 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36673 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36675 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36677 stmt = make_node (OACC_CACHE);
36678 TREE_TYPE (stmt) = void_type_node;
36679 OACC_CACHE_CLAUSES (stmt) = clauses;
36680 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36681 add_stmt (stmt);
36683 return stmt;
36686 /* OpenACC 2.0:
36687 # pragma acc data oacc-data-clause[optseq] new-line
36688 structured-block */
36690 #define OACC_DATA_CLAUSE_MASK \
36691 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36703 static tree
36704 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36706 tree stmt, clauses, block;
36707 unsigned int save;
36709 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36710 "#pragma acc data", pragma_tok);
36712 block = begin_omp_parallel ();
36713 save = cp_parser_begin_omp_structured_block (parser);
36714 cp_parser_statement (parser, NULL_TREE, false, if_p);
36715 cp_parser_end_omp_structured_block (parser, save);
36716 stmt = finish_oacc_data (clauses, block);
36717 return stmt;
36720 /* OpenACC 2.0:
36721 # pragma acc host_data <clauses> new-line
36722 structured-block */
36724 #define OACC_HOST_DATA_CLAUSE_MASK \
36725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36727 static tree
36728 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36730 tree stmt, clauses, block;
36731 unsigned int save;
36733 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36734 "#pragma acc host_data", pragma_tok);
36736 block = begin_omp_parallel ();
36737 save = cp_parser_begin_omp_structured_block (parser);
36738 cp_parser_statement (parser, NULL_TREE, false, if_p);
36739 cp_parser_end_omp_structured_block (parser, save);
36740 stmt = finish_oacc_host_data (clauses, block);
36741 return stmt;
36744 /* OpenACC 2.0:
36745 # pragma acc declare oacc-data-clause[optseq] new-line
36748 #define OACC_DECLARE_CLAUSE_MASK \
36749 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36762 static tree
36763 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36765 tree clauses, stmt;
36766 bool error = false;
36768 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36769 "#pragma acc declare", pragma_tok, true);
36772 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36774 error_at (pragma_tok->location,
36775 "no valid clauses specified in %<#pragma acc declare%>");
36776 return NULL_TREE;
36779 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36781 location_t loc = OMP_CLAUSE_LOCATION (t);
36782 tree decl = OMP_CLAUSE_DECL (t);
36783 if (!DECL_P (decl))
36785 error_at (loc, "array section in %<#pragma acc declare%>");
36786 error = true;
36787 continue;
36789 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36790 switch (OMP_CLAUSE_MAP_KIND (t))
36792 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36793 case GOMP_MAP_FORCE_ALLOC:
36794 case GOMP_MAP_FORCE_TO:
36795 case GOMP_MAP_FORCE_DEVICEPTR:
36796 case GOMP_MAP_DEVICE_RESIDENT:
36797 break;
36799 case GOMP_MAP_LINK:
36800 if (!global_bindings_p ()
36801 && (TREE_STATIC (decl)
36802 || !DECL_EXTERNAL (decl)))
36804 error_at (loc,
36805 "%qD must be a global variable in "
36806 "%<#pragma acc declare link%>",
36807 decl);
36808 error = true;
36809 continue;
36811 break;
36813 default:
36814 if (global_bindings_p ())
36816 error_at (loc, "invalid OpenACC clause at file scope");
36817 error = true;
36818 continue;
36820 if (DECL_EXTERNAL (decl))
36822 error_at (loc,
36823 "invalid use of %<extern%> variable %qD "
36824 "in %<#pragma acc declare%>", decl);
36825 error = true;
36826 continue;
36828 else if (TREE_PUBLIC (decl))
36830 error_at (loc,
36831 "invalid use of %<global%> variable %qD "
36832 "in %<#pragma acc declare%>", decl);
36833 error = true;
36834 continue;
36836 break;
36839 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36840 || lookup_attribute ("omp declare target link",
36841 DECL_ATTRIBUTES (decl)))
36843 error_at (loc, "variable %qD used more than once with "
36844 "%<#pragma acc declare%>", decl);
36845 error = true;
36846 continue;
36849 if (!error)
36851 tree id;
36853 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36854 id = get_identifier ("omp declare target link");
36855 else
36856 id = get_identifier ("omp declare target");
36858 DECL_ATTRIBUTES (decl)
36859 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36860 if (global_bindings_p ())
36862 symtab_node *node = symtab_node::get (decl);
36863 if (node != NULL)
36865 node->offloadable = 1;
36866 if (ENABLE_OFFLOADING)
36868 g->have_offload = true;
36869 if (is_a <varpool_node *> (node))
36870 vec_safe_push (offload_vars, decl);
36877 if (error || global_bindings_p ())
36878 return NULL_TREE;
36880 stmt = make_node (OACC_DECLARE);
36881 TREE_TYPE (stmt) = void_type_node;
36882 OACC_DECLARE_CLAUSES (stmt) = clauses;
36883 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36885 add_stmt (stmt);
36887 return NULL_TREE;
36890 /* OpenACC 2.0:
36891 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36895 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36897 LOC is the location of the #pragma token.
36900 #define OACC_ENTER_DATA_CLAUSE_MASK \
36901 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36909 #define OACC_EXIT_DATA_CLAUSE_MASK \
36910 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36916 static tree
36917 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36918 bool enter)
36920 location_t loc = pragma_tok->location;
36921 tree stmt, clauses;
36922 const char *p = "";
36924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36925 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36927 if (strcmp (p, "data") != 0)
36929 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36930 enter ? "enter" : "exit");
36931 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36932 return NULL_TREE;
36935 cp_lexer_consume_token (parser->lexer);
36937 if (enter)
36938 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36939 "#pragma acc enter data", pragma_tok);
36940 else
36941 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36942 "#pragma acc exit data", pragma_tok);
36944 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36946 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36947 enter ? "enter" : "exit");
36948 return NULL_TREE;
36951 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36952 TREE_TYPE (stmt) = void_type_node;
36953 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36954 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36955 add_stmt (stmt);
36956 return stmt;
36959 /* OpenACC 2.0:
36960 # pragma acc loop oacc-loop-clause[optseq] new-line
36961 structured-block */
36963 #define OACC_LOOP_CLAUSE_MASK \
36964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36975 static tree
36976 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36977 omp_clause_mask mask, tree *cclauses, bool *if_p)
36979 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36981 strcat (p_name, " loop");
36982 mask |= OACC_LOOP_CLAUSE_MASK;
36984 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36985 cclauses == NULL);
36986 if (cclauses)
36988 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36989 if (*cclauses)
36990 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36991 if (clauses)
36992 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36995 tree block = begin_omp_structured_block ();
36996 int save = cp_parser_begin_omp_structured_block (parser);
36997 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36998 cp_parser_end_omp_structured_block (parser, save);
36999 add_stmt (finish_omp_structured_block (block));
37001 return stmt;
37004 /* OpenACC 2.0:
37005 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37006 structured-block
37010 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37011 structured-block
37014 #define OACC_KERNELS_CLAUSE_MASK \
37015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37033 #define OACC_PARALLEL_CLAUSE_MASK \
37034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37055 static tree
37056 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37057 char *p_name, bool *if_p)
37059 omp_clause_mask mask;
37060 enum tree_code code;
37061 switch (cp_parser_pragma_kind (pragma_tok))
37063 case PRAGMA_OACC_KERNELS:
37064 strcat (p_name, " kernels");
37065 mask = OACC_KERNELS_CLAUSE_MASK;
37066 code = OACC_KERNELS;
37067 break;
37068 case PRAGMA_OACC_PARALLEL:
37069 strcat (p_name, " parallel");
37070 mask = OACC_PARALLEL_CLAUSE_MASK;
37071 code = OACC_PARALLEL;
37072 break;
37073 default:
37074 gcc_unreachable ();
37077 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37079 const char *p
37080 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37081 if (strcmp (p, "loop") == 0)
37083 cp_lexer_consume_token (parser->lexer);
37084 tree block = begin_omp_parallel ();
37085 tree clauses;
37086 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37087 if_p);
37088 return finish_omp_construct (code, block, clauses);
37092 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37094 tree block = begin_omp_parallel ();
37095 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37096 cp_parser_statement (parser, NULL_TREE, false, if_p);
37097 cp_parser_end_omp_structured_block (parser, save);
37098 return finish_omp_construct (code, block, clauses);
37101 /* OpenACC 2.0:
37102 # pragma acc update oacc-update-clause[optseq] new-line
37105 #define OACC_UPDATE_CLAUSE_MASK \
37106 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37113 static tree
37114 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37116 tree stmt, clauses;
37118 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37119 "#pragma acc update", pragma_tok);
37121 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37123 error_at (pragma_tok->location,
37124 "%<#pragma acc update%> must contain at least one "
37125 "%<device%> or %<host%> or %<self%> clause");
37126 return NULL_TREE;
37129 stmt = make_node (OACC_UPDATE);
37130 TREE_TYPE (stmt) = void_type_node;
37131 OACC_UPDATE_CLAUSES (stmt) = clauses;
37132 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37133 add_stmt (stmt);
37134 return stmt;
37137 /* OpenACC 2.0:
37138 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37140 LOC is the location of the #pragma token.
37143 #define OACC_WAIT_CLAUSE_MASK \
37144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37146 static tree
37147 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37149 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37150 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37152 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37153 list = cp_parser_oacc_wait_list (parser, loc, list);
37155 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37156 "#pragma acc wait", pragma_tok);
37158 stmt = c_finish_oacc_wait (loc, list, clauses);
37159 stmt = finish_expr_stmt (stmt);
37161 return stmt;
37164 /* OpenMP 4.0:
37165 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37167 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37175 static void
37176 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37177 enum pragma_context context)
37179 bool first_p = parser->omp_declare_simd == NULL;
37180 cp_omp_declare_simd_data data;
37181 if (first_p)
37183 data.error_seen = false;
37184 data.fndecl_seen = false;
37185 data.tokens = vNULL;
37186 data.clauses = NULL_TREE;
37187 /* It is safe to take the address of a local variable; it will only be
37188 used while this scope is live. */
37189 parser->omp_declare_simd = &data;
37192 /* Store away all pragma tokens. */
37193 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37194 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37195 cp_lexer_consume_token (parser->lexer);
37196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37197 parser->omp_declare_simd->error_seen = true;
37198 cp_parser_require_pragma_eol (parser, pragma_tok);
37199 struct cp_token_cache *cp
37200 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37201 parser->omp_declare_simd->tokens.safe_push (cp);
37203 if (first_p)
37205 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37206 cp_parser_pragma (parser, context, NULL);
37207 switch (context)
37209 case pragma_external:
37210 cp_parser_declaration (parser);
37211 break;
37212 case pragma_member:
37213 cp_parser_member_declaration (parser);
37214 break;
37215 case pragma_objc_icode:
37216 cp_parser_block_declaration (parser, /*statement_p=*/false);
37217 break;
37218 default:
37219 cp_parser_declaration_statement (parser);
37220 break;
37222 if (parser->omp_declare_simd
37223 && !parser->omp_declare_simd->error_seen
37224 && !parser->omp_declare_simd->fndecl_seen)
37225 error_at (pragma_tok->location,
37226 "%<#pragma omp declare simd%> not immediately followed by "
37227 "function declaration or definition");
37228 data.tokens.release ();
37229 parser->omp_declare_simd = NULL;
37233 /* Finalize #pragma omp declare simd clauses after direct declarator has
37234 been parsed, and put that into "omp declare simd" attribute. */
37236 static tree
37237 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37239 struct cp_token_cache *ce;
37240 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37241 int i;
37243 if (!data->error_seen && data->fndecl_seen)
37245 error ("%<#pragma omp declare simd%> not immediately followed by "
37246 "a single function declaration or definition");
37247 data->error_seen = true;
37249 if (data->error_seen)
37250 return attrs;
37252 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37254 tree c, cl;
37256 cp_parser_push_lexer_for_tokens (parser, ce);
37257 parser->lexer->in_pragma = true;
37258 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37259 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37260 cp_lexer_consume_token (parser->lexer);
37261 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37262 "#pragma omp declare simd", pragma_tok);
37263 cp_parser_pop_lexer (parser);
37264 if (cl)
37265 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37266 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37267 TREE_CHAIN (c) = attrs;
37268 if (processing_template_decl)
37269 ATTR_IS_DEPENDENT (c) = 1;
37270 attrs = c;
37273 data->fndecl_seen = true;
37274 return attrs;
37278 /* OpenMP 4.0:
37279 # pragma omp declare target new-line
37280 declarations and definitions
37281 # pragma omp end declare target new-line
37283 OpenMP 4.5:
37284 # pragma omp declare target ( extended-list ) new-line
37286 # pragma omp declare target declare-target-clauses[seq] new-line */
37288 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37289 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37292 static void
37293 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37295 tree clauses = NULL_TREE;
37296 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37297 clauses
37298 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37299 "#pragma omp declare target", pragma_tok);
37300 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37302 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37303 clauses);
37304 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37305 cp_parser_require_pragma_eol (parser, pragma_tok);
37307 else
37309 cp_parser_require_pragma_eol (parser, pragma_tok);
37310 scope_chain->omp_declare_target_attribute++;
37311 return;
37313 if (scope_chain->omp_declare_target_attribute)
37314 error_at (pragma_tok->location,
37315 "%<#pragma omp declare target%> with clauses in between "
37316 "%<#pragma omp declare target%> without clauses and "
37317 "%<#pragma omp end declare target%>");
37318 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37320 tree t = OMP_CLAUSE_DECL (c), id;
37321 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37322 tree at2 = lookup_attribute ("omp declare target link",
37323 DECL_ATTRIBUTES (t));
37324 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37326 id = get_identifier ("omp declare target link");
37327 std::swap (at1, at2);
37329 else
37330 id = get_identifier ("omp declare target");
37331 if (at2)
37333 error_at (OMP_CLAUSE_LOCATION (c),
37334 "%qD specified both in declare target %<link%> and %<to%>"
37335 " clauses", t);
37336 continue;
37338 if (!at1)
37340 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37341 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37342 continue;
37344 symtab_node *node = symtab_node::get (t);
37345 if (node != NULL)
37347 node->offloadable = 1;
37348 if (ENABLE_OFFLOADING)
37350 g->have_offload = true;
37351 if (is_a <varpool_node *> (node))
37352 vec_safe_push (offload_vars, t);
37359 static void
37360 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37362 const char *p = "";
37363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37365 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37366 p = IDENTIFIER_POINTER (id);
37368 if (strcmp (p, "declare") == 0)
37370 cp_lexer_consume_token (parser->lexer);
37371 p = "";
37372 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37374 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37375 p = IDENTIFIER_POINTER (id);
37377 if (strcmp (p, "target") == 0)
37378 cp_lexer_consume_token (parser->lexer);
37379 else
37381 cp_parser_error (parser, "expected %<target%>");
37382 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37383 return;
37386 else
37388 cp_parser_error (parser, "expected %<declare%>");
37389 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37390 return;
37392 cp_parser_require_pragma_eol (parser, pragma_tok);
37393 if (!scope_chain->omp_declare_target_attribute)
37394 error_at (pragma_tok->location,
37395 "%<#pragma omp end declare target%> without corresponding "
37396 "%<#pragma omp declare target%>");
37397 else
37398 scope_chain->omp_declare_target_attribute--;
37401 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37402 expression and optional initializer clause of
37403 #pragma omp declare reduction. We store the expression(s) as
37404 either 3, 6 or 7 special statements inside of the artificial function's
37405 body. The first two statements are DECL_EXPRs for the artificial
37406 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37407 expression that uses those variables.
37408 If there was any INITIALIZER clause, this is followed by further statements,
37409 the fourth and fifth statements are DECL_EXPRs for the artificial
37410 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37411 constructor variant (first token after open paren is not omp_priv),
37412 then the sixth statement is a statement with the function call expression
37413 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37414 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37415 to initialize the OMP_PRIV artificial variable and there is seventh
37416 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37418 static bool
37419 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37421 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37422 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37423 type = TREE_TYPE (type);
37424 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37425 DECL_ARTIFICIAL (omp_out) = 1;
37426 pushdecl (omp_out);
37427 add_decl_expr (omp_out);
37428 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37429 DECL_ARTIFICIAL (omp_in) = 1;
37430 pushdecl (omp_in);
37431 add_decl_expr (omp_in);
37432 tree combiner;
37433 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37435 keep_next_level (true);
37436 tree block = begin_omp_structured_block ();
37437 combiner = cp_parser_expression (parser);
37438 finish_expr_stmt (combiner);
37439 block = finish_omp_structured_block (block);
37440 add_stmt (block);
37442 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37443 return false;
37445 const char *p = "";
37446 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37448 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37449 p = IDENTIFIER_POINTER (id);
37452 if (strcmp (p, "initializer") == 0)
37454 cp_lexer_consume_token (parser->lexer);
37455 matching_parens parens;
37456 if (!parens.require_open (parser))
37457 return false;
37459 p = "";
37460 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37462 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37463 p = IDENTIFIER_POINTER (id);
37466 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37467 DECL_ARTIFICIAL (omp_priv) = 1;
37468 pushdecl (omp_priv);
37469 add_decl_expr (omp_priv);
37470 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37471 DECL_ARTIFICIAL (omp_orig) = 1;
37472 pushdecl (omp_orig);
37473 add_decl_expr (omp_orig);
37475 keep_next_level (true);
37476 block = begin_omp_structured_block ();
37478 bool ctor = false;
37479 if (strcmp (p, "omp_priv") == 0)
37481 bool is_direct_init, is_non_constant_init;
37482 ctor = true;
37483 cp_lexer_consume_token (parser->lexer);
37484 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37485 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37486 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37487 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37488 == CPP_CLOSE_PAREN
37489 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37490 == CPP_CLOSE_PAREN))
37492 finish_omp_structured_block (block);
37493 error ("invalid initializer clause");
37494 return false;
37496 initializer = cp_parser_initializer (parser, &is_direct_init,
37497 &is_non_constant_init);
37498 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37499 NULL_TREE, LOOKUP_ONLYCONVERTING);
37501 else
37503 cp_parser_parse_tentatively (parser);
37504 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37505 /*check_dependency_p=*/true,
37506 /*template_p=*/NULL,
37507 /*declarator_p=*/false,
37508 /*optional_p=*/false);
37509 vec<tree, va_gc> *args;
37510 if (fn_name == error_mark_node
37511 || cp_parser_error_occurred (parser)
37512 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37513 || ((args = cp_parser_parenthesized_expression_list
37514 (parser, non_attr, /*cast_p=*/false,
37515 /*allow_expansion_p=*/true,
37516 /*non_constant_p=*/NULL)),
37517 cp_parser_error_occurred (parser)))
37519 finish_omp_structured_block (block);
37520 cp_parser_abort_tentative_parse (parser);
37521 cp_parser_error (parser, "expected id-expression (arguments)");
37522 return false;
37524 unsigned int i;
37525 tree arg;
37526 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37527 if (arg == omp_priv
37528 || (TREE_CODE (arg) == ADDR_EXPR
37529 && TREE_OPERAND (arg, 0) == omp_priv))
37530 break;
37531 cp_parser_abort_tentative_parse (parser);
37532 if (arg == NULL_TREE)
37533 error ("one of the initializer call arguments should be %<omp_priv%>"
37534 " or %<&omp_priv%>");
37535 initializer = cp_parser_postfix_expression (parser, false, false, false,
37536 false, NULL);
37537 finish_expr_stmt (initializer);
37540 block = finish_omp_structured_block (block);
37541 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37542 add_stmt (block);
37544 if (ctor)
37545 add_decl_expr (omp_orig);
37547 if (!parens.require_close (parser))
37548 return false;
37551 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37552 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37553 UNKNOWN_LOCATION);
37555 return true;
37558 /* OpenMP 4.0
37559 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37560 initializer-clause[opt] new-line
37562 initializer-clause:
37563 initializer (omp_priv initializer)
37564 initializer (function-name (argument-list)) */
37566 static void
37567 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37568 enum pragma_context)
37570 auto_vec<tree> types;
37571 enum tree_code reduc_code = ERROR_MARK;
37572 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37573 unsigned int i;
37574 cp_token *first_token;
37575 cp_token_cache *cp;
37576 int errs;
37577 void *p;
37579 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37580 p = obstack_alloc (&declarator_obstack, 0);
37582 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37583 goto fail;
37585 switch (cp_lexer_peek_token (parser->lexer)->type)
37587 case CPP_PLUS:
37588 reduc_code = PLUS_EXPR;
37589 break;
37590 case CPP_MULT:
37591 reduc_code = MULT_EXPR;
37592 break;
37593 case CPP_MINUS:
37594 reduc_code = MINUS_EXPR;
37595 break;
37596 case CPP_AND:
37597 reduc_code = BIT_AND_EXPR;
37598 break;
37599 case CPP_XOR:
37600 reduc_code = BIT_XOR_EXPR;
37601 break;
37602 case CPP_OR:
37603 reduc_code = BIT_IOR_EXPR;
37604 break;
37605 case CPP_AND_AND:
37606 reduc_code = TRUTH_ANDIF_EXPR;
37607 break;
37608 case CPP_OR_OR:
37609 reduc_code = TRUTH_ORIF_EXPR;
37610 break;
37611 case CPP_NAME:
37612 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37613 break;
37614 default:
37615 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37616 "%<|%>, %<&&%>, %<||%> or identifier");
37617 goto fail;
37620 if (reduc_code != ERROR_MARK)
37621 cp_lexer_consume_token (parser->lexer);
37623 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37624 if (reduc_id == error_mark_node)
37625 goto fail;
37627 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37628 goto fail;
37630 /* Types may not be defined in declare reduction type list. */
37631 const char *saved_message;
37632 saved_message = parser->type_definition_forbidden_message;
37633 parser->type_definition_forbidden_message
37634 = G_("types may not be defined in declare reduction type list");
37635 bool saved_colon_corrects_to_scope_p;
37636 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37637 parser->colon_corrects_to_scope_p = false;
37638 bool saved_colon_doesnt_start_class_def_p;
37639 saved_colon_doesnt_start_class_def_p
37640 = parser->colon_doesnt_start_class_def_p;
37641 parser->colon_doesnt_start_class_def_p = true;
37643 while (true)
37645 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37646 type = cp_parser_type_id (parser);
37647 if (type == error_mark_node)
37649 else if (ARITHMETIC_TYPE_P (type)
37650 && (orig_reduc_id == NULL_TREE
37651 || (TREE_CODE (type) != COMPLEX_TYPE
37652 && (id_equal (orig_reduc_id, "min")
37653 || id_equal (orig_reduc_id, "max")))))
37654 error_at (loc, "predeclared arithmetic type %qT in "
37655 "%<#pragma omp declare reduction%>", type);
37656 else if (TREE_CODE (type) == FUNCTION_TYPE
37657 || TREE_CODE (type) == METHOD_TYPE
37658 || TREE_CODE (type) == ARRAY_TYPE)
37659 error_at (loc, "function or array type %qT in "
37660 "%<#pragma omp declare reduction%>", type);
37661 else if (TREE_CODE (type) == REFERENCE_TYPE)
37662 error_at (loc, "reference type %qT in "
37663 "%<#pragma omp declare reduction%>", type);
37664 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37665 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37666 "%<#pragma omp declare reduction%>", type);
37667 else
37668 types.safe_push (type);
37670 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37671 cp_lexer_consume_token (parser->lexer);
37672 else
37673 break;
37676 /* Restore the saved message. */
37677 parser->type_definition_forbidden_message = saved_message;
37678 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37679 parser->colon_doesnt_start_class_def_p
37680 = saved_colon_doesnt_start_class_def_p;
37682 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37683 || types.is_empty ())
37685 fail:
37686 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37687 goto done;
37690 first_token = cp_lexer_peek_token (parser->lexer);
37691 cp = NULL;
37692 errs = errorcount;
37693 FOR_EACH_VEC_ELT (types, i, type)
37695 tree fntype
37696 = build_function_type_list (void_type_node,
37697 cp_build_reference_type (type, false),
37698 NULL_TREE);
37699 tree this_reduc_id = reduc_id;
37700 if (!dependent_type_p (type))
37701 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37702 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37703 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37704 DECL_ARTIFICIAL (fndecl) = 1;
37705 DECL_EXTERNAL (fndecl) = 1;
37706 DECL_DECLARED_INLINE_P (fndecl) = 1;
37707 DECL_IGNORED_P (fndecl) = 1;
37708 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37709 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37710 DECL_ATTRIBUTES (fndecl)
37711 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37712 DECL_ATTRIBUTES (fndecl));
37713 if (processing_template_decl)
37714 fndecl = push_template_decl (fndecl);
37715 bool block_scope = false;
37716 tree block = NULL_TREE;
37717 if (current_function_decl)
37719 block_scope = true;
37720 DECL_CONTEXT (fndecl) = global_namespace;
37721 if (!processing_template_decl)
37722 pushdecl (fndecl);
37724 else if (current_class_type)
37726 if (cp == NULL)
37728 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37729 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37730 cp_lexer_consume_token (parser->lexer);
37731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37732 goto fail;
37733 cp = cp_token_cache_new (first_token,
37734 cp_lexer_peek_nth_token (parser->lexer,
37735 2));
37737 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37738 finish_member_declaration (fndecl);
37739 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37740 DECL_PENDING_INLINE_P (fndecl) = 1;
37741 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37742 continue;
37744 else
37746 DECL_CONTEXT (fndecl) = current_namespace;
37747 pushdecl (fndecl);
37749 if (!block_scope)
37750 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37751 else
37752 block = begin_omp_structured_block ();
37753 if (cp)
37755 cp_parser_push_lexer_for_tokens (parser, cp);
37756 parser->lexer->in_pragma = true;
37758 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37760 if (!block_scope)
37761 finish_function (/*inline_p=*/false);
37762 else
37763 DECL_CONTEXT (fndecl) = current_function_decl;
37764 if (cp)
37765 cp_parser_pop_lexer (parser);
37766 goto fail;
37768 if (cp)
37769 cp_parser_pop_lexer (parser);
37770 if (!block_scope)
37771 finish_function (/*inline_p=*/false);
37772 else
37774 DECL_CONTEXT (fndecl) = current_function_decl;
37775 block = finish_omp_structured_block (block);
37776 if (TREE_CODE (block) == BIND_EXPR)
37777 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37778 else if (TREE_CODE (block) == STATEMENT_LIST)
37779 DECL_SAVED_TREE (fndecl) = block;
37780 if (processing_template_decl)
37781 add_decl_expr (fndecl);
37783 cp_check_omp_declare_reduction (fndecl);
37784 if (cp == NULL && types.length () > 1)
37785 cp = cp_token_cache_new (first_token,
37786 cp_lexer_peek_nth_token (parser->lexer, 2));
37787 if (errs != errorcount)
37788 break;
37791 cp_parser_require_pragma_eol (parser, pragma_tok);
37793 done:
37794 /* Free any declarators allocated. */
37795 obstack_free (&declarator_obstack, p);
37798 /* OpenMP 4.0
37799 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37800 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37801 initializer-clause[opt] new-line
37802 #pragma omp declare target new-line */
37804 static bool
37805 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37806 enum pragma_context context)
37808 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37810 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37811 const char *p = IDENTIFIER_POINTER (id);
37813 if (strcmp (p, "simd") == 0)
37815 cp_lexer_consume_token (parser->lexer);
37816 cp_parser_omp_declare_simd (parser, pragma_tok,
37817 context);
37818 return true;
37820 cp_ensure_no_omp_declare_simd (parser);
37821 if (strcmp (p, "reduction") == 0)
37823 cp_lexer_consume_token (parser->lexer);
37824 cp_parser_omp_declare_reduction (parser, pragma_tok,
37825 context);
37826 return false;
37828 if (!flag_openmp) /* flag_openmp_simd */
37830 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37831 return false;
37833 if (strcmp (p, "target") == 0)
37835 cp_lexer_consume_token (parser->lexer);
37836 cp_parser_omp_declare_target (parser, pragma_tok);
37837 return false;
37840 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37841 "or %<target%>");
37842 cp_parser_require_pragma_eol (parser, pragma_tok);
37843 return false;
37846 /* OpenMP 4.5:
37847 #pragma omp taskloop taskloop-clause[optseq] new-line
37848 for-loop
37850 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37851 for-loop */
37853 #define OMP_TASKLOOP_CLAUSE_MASK \
37854 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37869 static tree
37870 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37871 char *p_name, omp_clause_mask mask, tree *cclauses,
37872 bool *if_p)
37874 tree clauses, sb, ret;
37875 unsigned int save;
37876 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37878 strcat (p_name, " taskloop");
37879 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37881 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37883 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37884 const char *p = IDENTIFIER_POINTER (id);
37886 if (strcmp (p, "simd") == 0)
37888 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37889 if (cclauses == NULL)
37890 cclauses = cclauses_buf;
37892 cp_lexer_consume_token (parser->lexer);
37893 if (!flag_openmp) /* flag_openmp_simd */
37894 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37895 cclauses, if_p);
37896 sb = begin_omp_structured_block ();
37897 save = cp_parser_begin_omp_structured_block (parser);
37898 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37899 cclauses, if_p);
37900 cp_parser_end_omp_structured_block (parser, save);
37901 tree body = finish_omp_structured_block (sb);
37902 if (ret == NULL)
37903 return ret;
37904 ret = make_node (OMP_TASKLOOP);
37905 TREE_TYPE (ret) = void_type_node;
37906 OMP_FOR_BODY (ret) = body;
37907 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37908 SET_EXPR_LOCATION (ret, loc);
37909 add_stmt (ret);
37910 return ret;
37913 if (!flag_openmp) /* flag_openmp_simd */
37915 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37916 return NULL_TREE;
37919 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37920 cclauses == NULL);
37921 if (cclauses)
37923 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37924 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37927 sb = begin_omp_structured_block ();
37928 save = cp_parser_begin_omp_structured_block (parser);
37930 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37931 if_p);
37933 cp_parser_end_omp_structured_block (parser, save);
37934 add_stmt (finish_omp_structured_block (sb));
37936 return ret;
37940 /* OpenACC 2.0:
37941 # pragma acc routine oacc-routine-clause[optseq] new-line
37942 function-definition
37944 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37947 #define OACC_ROUTINE_CLAUSE_MASK \
37948 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37954 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37955 component, which must resolve to a declared namespace-scope
37956 function. The clauses are either processed directly (for a named
37957 function), or defered until the immediatley following declaration
37958 is parsed. */
37960 static void
37961 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37962 enum pragma_context context)
37964 gcc_checking_assert (context == pragma_external);
37965 /* The checking for "another pragma following this one" in the "no optional
37966 '( name )'" case makes sure that we dont re-enter. */
37967 gcc_checking_assert (parser->oacc_routine == NULL);
37969 cp_oacc_routine_data data;
37970 data.error_seen = false;
37971 data.fndecl_seen = false;
37972 data.tokens = vNULL;
37973 data.clauses = NULL_TREE;
37974 data.loc = pragma_tok->location;
37975 /* It is safe to take the address of a local variable; it will only be
37976 used while this scope is live. */
37977 parser->oacc_routine = &data;
37979 /* Look for optional '( name )'. */
37980 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37982 matching_parens parens;
37983 parens.consume_open (parser); /* '(' */
37985 /* We parse the name as an id-expression. If it resolves to
37986 anything other than a non-overloaded function at namespace
37987 scope, it's an error. */
37988 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37989 tree name = cp_parser_id_expression (parser,
37990 /*template_keyword_p=*/false,
37991 /*check_dependency_p=*/false,
37992 /*template_p=*/NULL,
37993 /*declarator_p=*/false,
37994 /*optional_p=*/false);
37995 tree decl = (identifier_p (name)
37996 ? cp_parser_lookup_name_simple (parser, name, name_loc)
37997 : name);
37998 if (name != error_mark_node && decl == error_mark_node)
37999 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38001 if (decl == error_mark_node
38002 || !parens.require_close (parser))
38004 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38005 parser->oacc_routine = NULL;
38006 return;
38009 data.clauses
38010 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38011 "#pragma acc routine",
38012 cp_lexer_peek_token (parser->lexer));
38014 if (decl && is_overloaded_fn (decl)
38015 && (TREE_CODE (decl) != FUNCTION_DECL
38016 || DECL_FUNCTION_TEMPLATE_P (decl)))
38018 error_at (name_loc,
38019 "%<#pragma acc routine%> names a set of overloads");
38020 parser->oacc_routine = NULL;
38021 return;
38024 /* Perhaps we should use the same rule as declarations in different
38025 namespaces? */
38026 if (!DECL_NAMESPACE_SCOPE_P (decl))
38028 error_at (name_loc,
38029 "%qD does not refer to a namespace scope function", decl);
38030 parser->oacc_routine = NULL;
38031 return;
38034 if (TREE_CODE (decl) != FUNCTION_DECL)
38036 error_at (name_loc, "%qD does not refer to a function", decl);
38037 parser->oacc_routine = NULL;
38038 return;
38041 cp_finalize_oacc_routine (parser, decl, false);
38042 parser->oacc_routine = NULL;
38044 else /* No optional '( name )'. */
38046 /* Store away all pragma tokens. */
38047 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38048 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38049 cp_lexer_consume_token (parser->lexer);
38050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38051 parser->oacc_routine->error_seen = true;
38052 cp_parser_require_pragma_eol (parser, pragma_tok);
38053 struct cp_token_cache *cp
38054 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38055 parser->oacc_routine->tokens.safe_push (cp);
38057 /* Emit a helpful diagnostic if there's another pragma following this
38058 one. */
38059 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38061 cp_ensure_no_oacc_routine (parser);
38062 data.tokens.release ();
38063 /* ..., and then just keep going. */
38064 return;
38067 /* We only have to consider the pragma_external case here. */
38068 cp_parser_declaration (parser);
38069 if (parser->oacc_routine
38070 && !parser->oacc_routine->fndecl_seen)
38071 cp_ensure_no_oacc_routine (parser);
38072 else
38073 parser->oacc_routine = NULL;
38074 data.tokens.release ();
38078 /* Finalize #pragma acc routine clauses after direct declarator has
38079 been parsed. */
38081 static tree
38082 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38084 struct cp_token_cache *ce;
38085 cp_oacc_routine_data *data = parser->oacc_routine;
38087 if (!data->error_seen && data->fndecl_seen)
38089 error_at (data->loc,
38090 "%<#pragma acc routine%> not immediately followed by "
38091 "a single function declaration or definition");
38092 data->error_seen = true;
38094 if (data->error_seen)
38095 return attrs;
38097 gcc_checking_assert (data->tokens.length () == 1);
38098 ce = data->tokens[0];
38100 cp_parser_push_lexer_for_tokens (parser, ce);
38101 parser->lexer->in_pragma = true;
38102 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38104 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38105 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38106 parser->oacc_routine->clauses
38107 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38108 "#pragma acc routine", pragma_tok);
38109 cp_parser_pop_lexer (parser);
38110 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38111 fndecl_seen. */
38113 return attrs;
38116 /* Apply any saved OpenACC routine clauses to a just-parsed
38117 declaration. */
38119 static void
38120 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38122 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38124 /* Keep going if we're in error reporting mode. */
38125 if (parser->oacc_routine->error_seen
38126 || fndecl == error_mark_node)
38127 return;
38129 if (parser->oacc_routine->fndecl_seen)
38131 error_at (parser->oacc_routine->loc,
38132 "%<#pragma acc routine%> not immediately followed by"
38133 " a single function declaration or definition");
38134 parser->oacc_routine = NULL;
38135 return;
38137 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38139 cp_ensure_no_oacc_routine (parser);
38140 return;
38143 if (oacc_get_fn_attrib (fndecl))
38145 error_at (parser->oacc_routine->loc,
38146 "%<#pragma acc routine%> already applied to %qD", fndecl);
38147 parser->oacc_routine = NULL;
38148 return;
38151 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38153 error_at (parser->oacc_routine->loc,
38154 TREE_USED (fndecl)
38155 ? G_("%<#pragma acc routine%> must be applied before use")
38156 : G_("%<#pragma acc routine%> must be applied before "
38157 "definition"));
38158 parser->oacc_routine = NULL;
38159 return;
38162 /* Process the routine's dimension clauses. */
38163 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38164 oacc_replace_fn_attrib (fndecl, dims);
38166 /* Add an "omp declare target" attribute. */
38167 DECL_ATTRIBUTES (fndecl)
38168 = tree_cons (get_identifier ("omp declare target"),
38169 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38171 /* Don't unset parser->oacc_routine here: we may still need it to
38172 diagnose wrong usage. But, remember that we've used this "#pragma acc
38173 routine". */
38174 parser->oacc_routine->fndecl_seen = true;
38178 /* Main entry point to OpenMP statement pragmas. */
38180 static void
38181 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38183 tree stmt;
38184 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38185 omp_clause_mask mask (0);
38187 switch (cp_parser_pragma_kind (pragma_tok))
38189 case PRAGMA_OACC_ATOMIC:
38190 cp_parser_omp_atomic (parser, pragma_tok);
38191 return;
38192 case PRAGMA_OACC_CACHE:
38193 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38194 break;
38195 case PRAGMA_OACC_DATA:
38196 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38197 break;
38198 case PRAGMA_OACC_ENTER_DATA:
38199 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38200 break;
38201 case PRAGMA_OACC_EXIT_DATA:
38202 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38203 break;
38204 case PRAGMA_OACC_HOST_DATA:
38205 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38206 break;
38207 case PRAGMA_OACC_KERNELS:
38208 case PRAGMA_OACC_PARALLEL:
38209 strcpy (p_name, "#pragma acc");
38210 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38211 if_p);
38212 break;
38213 case PRAGMA_OACC_LOOP:
38214 strcpy (p_name, "#pragma acc");
38215 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38216 if_p);
38217 break;
38218 case PRAGMA_OACC_UPDATE:
38219 stmt = cp_parser_oacc_update (parser, pragma_tok);
38220 break;
38221 case PRAGMA_OACC_WAIT:
38222 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38223 break;
38224 case PRAGMA_OMP_ATOMIC:
38225 cp_parser_omp_atomic (parser, pragma_tok);
38226 return;
38227 case PRAGMA_OMP_CRITICAL:
38228 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38229 break;
38230 case PRAGMA_OMP_DISTRIBUTE:
38231 strcpy (p_name, "#pragma omp");
38232 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38233 if_p);
38234 break;
38235 case PRAGMA_OMP_FOR:
38236 strcpy (p_name, "#pragma omp");
38237 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38238 if_p);
38239 break;
38240 case PRAGMA_OMP_MASTER:
38241 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38242 break;
38243 case PRAGMA_OMP_PARALLEL:
38244 strcpy (p_name, "#pragma omp");
38245 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38246 if_p);
38247 break;
38248 case PRAGMA_OMP_SECTIONS:
38249 strcpy (p_name, "#pragma omp");
38250 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38251 break;
38252 case PRAGMA_OMP_SIMD:
38253 strcpy (p_name, "#pragma omp");
38254 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38255 if_p);
38256 break;
38257 case PRAGMA_OMP_SINGLE:
38258 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38259 break;
38260 case PRAGMA_OMP_TASK:
38261 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38262 break;
38263 case PRAGMA_OMP_TASKGROUP:
38264 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38265 break;
38266 case PRAGMA_OMP_TASKLOOP:
38267 strcpy (p_name, "#pragma omp");
38268 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38269 if_p);
38270 break;
38271 case PRAGMA_OMP_TEAMS:
38272 strcpy (p_name, "#pragma omp");
38273 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38274 if_p);
38275 break;
38276 default:
38277 gcc_unreachable ();
38280 protected_set_expr_location (stmt, pragma_tok->location);
38283 /* Transactional Memory parsing routines. */
38285 /* Parse a transaction attribute.
38287 txn-attribute:
38288 attribute
38289 [ [ identifier ] ]
38291 We use this instead of cp_parser_attributes_opt for transactions to avoid
38292 the pedwarn in C++98 mode. */
38294 static tree
38295 cp_parser_txn_attribute_opt (cp_parser *parser)
38297 cp_token *token;
38298 tree attr_name, attr = NULL;
38300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38301 return cp_parser_attributes_opt (parser);
38303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38304 return NULL_TREE;
38305 cp_lexer_consume_token (parser->lexer);
38306 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38307 goto error1;
38309 token = cp_lexer_peek_token (parser->lexer);
38310 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38312 token = cp_lexer_consume_token (parser->lexer);
38314 attr_name = (token->type == CPP_KEYWORD
38315 /* For keywords, use the canonical spelling,
38316 not the parsed identifier. */
38317 ? ridpointers[(int) token->keyword]
38318 : token->u.value);
38319 attr = build_tree_list (attr_name, NULL_TREE);
38321 else
38322 cp_parser_error (parser, "expected identifier");
38324 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38325 error1:
38326 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38327 return attr;
38330 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38332 transaction-statement:
38333 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38334 compound-statement
38335 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38338 static tree
38339 cp_parser_transaction (cp_parser *parser, cp_token *token)
38341 unsigned char old_in = parser->in_transaction;
38342 unsigned char this_in = 1, new_in;
38343 enum rid keyword = token->keyword;
38344 tree stmt, attrs, noex;
38346 cp_lexer_consume_token (parser->lexer);
38348 if (keyword == RID_TRANSACTION_RELAXED
38349 || keyword == RID_SYNCHRONIZED)
38350 this_in |= TM_STMT_ATTR_RELAXED;
38351 else
38353 attrs = cp_parser_txn_attribute_opt (parser);
38354 if (attrs)
38355 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38358 /* Parse a noexcept specification. */
38359 if (keyword == RID_ATOMIC_NOEXCEPT)
38360 noex = boolean_true_node;
38361 else if (keyword == RID_ATOMIC_CANCEL)
38363 /* cancel-and-throw is unimplemented. */
38364 sorry ("atomic_cancel");
38365 noex = NULL_TREE;
38367 else
38368 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38370 /* Keep track if we're in the lexical scope of an outer transaction. */
38371 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38373 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38375 parser->in_transaction = new_in;
38376 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38377 parser->in_transaction = old_in;
38379 finish_transaction_stmt (stmt, NULL, this_in, noex);
38381 return stmt;
38384 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38386 transaction-expression:
38387 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38388 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38391 static tree
38392 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38394 unsigned char old_in = parser->in_transaction;
38395 unsigned char this_in = 1;
38396 cp_token *token;
38397 tree expr, noex;
38398 bool noex_expr;
38399 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38401 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38402 || keyword == RID_TRANSACTION_RELAXED);
38404 if (!flag_tm)
38405 error_at (loc,
38406 keyword == RID_TRANSACTION_RELAXED
38407 ? G_("%<__transaction_relaxed%> without transactional memory "
38408 "support enabled")
38409 : G_("%<__transaction_atomic%> without transactional memory "
38410 "support enabled"));
38412 token = cp_parser_require_keyword (parser, keyword,
38413 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38414 : RT_TRANSACTION_RELAXED));
38415 gcc_assert (token != NULL);
38417 if (keyword == RID_TRANSACTION_RELAXED)
38418 this_in |= TM_STMT_ATTR_RELAXED;
38420 /* Set this early. This might mean that we allow transaction_cancel in
38421 an expression that we find out later actually has to be a constexpr.
38422 However, we expect that cxx_constant_value will be able to deal with
38423 this; also, if the noexcept has no constexpr, then what we parse next
38424 really is a transaction's body. */
38425 parser->in_transaction = this_in;
38427 /* Parse a noexcept specification. */
38428 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38429 true);
38431 if (!noex || !noex_expr
38432 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38434 matching_parens parens;
38435 parens.require_open (parser);
38437 expr = cp_parser_expression (parser);
38438 expr = finish_parenthesized_expr (expr);
38440 parens.require_close (parser);
38442 else
38444 /* The only expression that is available got parsed for the noexcept
38445 already. noexcept is true then. */
38446 expr = noex;
38447 noex = boolean_true_node;
38450 expr = build_transaction_expr (token->location, expr, this_in, noex);
38451 parser->in_transaction = old_in;
38453 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38454 return error_mark_node;
38456 return (flag_tm ? expr : error_mark_node);
38459 /* Parse a function-transaction-block.
38461 function-transaction-block:
38462 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38463 function-body
38464 __transaction_atomic txn-attribute[opt] function-try-block
38465 __transaction_relaxed ctor-initializer[opt] function-body
38466 __transaction_relaxed function-try-block
38469 static void
38470 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38472 unsigned char old_in = parser->in_transaction;
38473 unsigned char new_in = 1;
38474 tree compound_stmt, stmt, attrs;
38475 cp_token *token;
38477 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38478 || keyword == RID_TRANSACTION_RELAXED);
38479 token = cp_parser_require_keyword (parser, keyword,
38480 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38481 : RT_TRANSACTION_RELAXED));
38482 gcc_assert (token != NULL);
38484 if (keyword == RID_TRANSACTION_RELAXED)
38485 new_in |= TM_STMT_ATTR_RELAXED;
38486 else
38488 attrs = cp_parser_txn_attribute_opt (parser);
38489 if (attrs)
38490 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38493 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38495 parser->in_transaction = new_in;
38497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38498 cp_parser_function_try_block (parser);
38499 else
38500 cp_parser_ctor_initializer_opt_and_function_body
38501 (parser, /*in_function_try_block=*/false);
38503 parser->in_transaction = old_in;
38505 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38508 /* Parse a __transaction_cancel statement.
38510 cancel-statement:
38511 __transaction_cancel txn-attribute[opt] ;
38512 __transaction_cancel txn-attribute[opt] throw-expression ;
38514 ??? Cancel and throw is not yet implemented. */
38516 static tree
38517 cp_parser_transaction_cancel (cp_parser *parser)
38519 cp_token *token;
38520 bool is_outer = false;
38521 tree stmt, attrs;
38523 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38524 RT_TRANSACTION_CANCEL);
38525 gcc_assert (token != NULL);
38527 attrs = cp_parser_txn_attribute_opt (parser);
38528 if (attrs)
38529 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38531 /* ??? Parse cancel-and-throw here. */
38533 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38535 if (!flag_tm)
38537 error_at (token->location, "%<__transaction_cancel%> without "
38538 "transactional memory support enabled");
38539 return error_mark_node;
38541 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38543 error_at (token->location, "%<__transaction_cancel%> within a "
38544 "%<__transaction_relaxed%>");
38545 return error_mark_node;
38547 else if (is_outer)
38549 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38550 && !is_tm_may_cancel_outer (current_function_decl))
38552 error_at (token->location, "outer %<__transaction_cancel%> not "
38553 "within outer %<__transaction_atomic%>");
38554 error_at (token->location,
38555 " or a %<transaction_may_cancel_outer%> function");
38556 return error_mark_node;
38559 else if (parser->in_transaction == 0)
38561 error_at (token->location, "%<__transaction_cancel%> not within "
38562 "%<__transaction_atomic%>");
38563 return error_mark_node;
38566 stmt = build_tm_abort_call (token->location, is_outer);
38567 add_stmt (stmt);
38569 return stmt;
38572 /* The parser. */
38574 static GTY (()) cp_parser *the_parser;
38577 /* Special handling for the first token or line in the file. The first
38578 thing in the file might be #pragma GCC pch_preprocess, which loads a
38579 PCH file, which is a GC collection point. So we need to handle this
38580 first pragma without benefit of an existing lexer structure.
38582 Always returns one token to the caller in *FIRST_TOKEN. This is
38583 either the true first token of the file, or the first token after
38584 the initial pragma. */
38586 static void
38587 cp_parser_initial_pragma (cp_token *first_token)
38589 tree name = NULL;
38591 cp_lexer_get_preprocessor_token (NULL, first_token);
38592 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38593 return;
38595 cp_lexer_get_preprocessor_token (NULL, first_token);
38596 if (first_token->type == CPP_STRING)
38598 name = first_token->u.value;
38600 cp_lexer_get_preprocessor_token (NULL, first_token);
38601 if (first_token->type != CPP_PRAGMA_EOL)
38602 error_at (first_token->location,
38603 "junk at end of %<#pragma GCC pch_preprocess%>");
38605 else
38606 error_at (first_token->location, "expected string literal");
38608 /* Skip to the end of the pragma. */
38609 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38610 cp_lexer_get_preprocessor_token (NULL, first_token);
38612 /* Now actually load the PCH file. */
38613 if (name)
38614 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38616 /* Read one more token to return to our caller. We have to do this
38617 after reading the PCH file in, since its pointers have to be
38618 live. */
38619 cp_lexer_get_preprocessor_token (NULL, first_token);
38622 /* Parse a pragma GCC ivdep. */
38624 static bool
38625 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38627 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38628 return true;
38631 /* Parse a pragma GCC unroll. */
38633 static unsigned short
38634 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38636 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38637 tree expr = cp_parser_constant_expression (parser);
38638 unsigned short unroll;
38639 expr = maybe_constant_value (expr);
38640 HOST_WIDE_INT lunroll = 0;
38641 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38642 || TREE_CODE (expr) != INTEGER_CST
38643 || (lunroll = tree_to_shwi (expr)) < 0
38644 || lunroll >= USHRT_MAX)
38646 error_at (location, "%<#pragma GCC unroll%> requires an"
38647 " assignment-expression that evaluates to a non-negative"
38648 " integral constant less than %u", USHRT_MAX);
38649 unroll = 0;
38651 else
38653 unroll = (unsigned short)lunroll;
38654 if (unroll == 0)
38655 unroll = 1;
38657 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38658 return unroll;
38661 /* Normal parsing of a pragma token. Here we can (and must) use the
38662 regular lexer. */
38664 static bool
38665 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38667 cp_token *pragma_tok;
38668 unsigned int id;
38669 tree stmt;
38670 bool ret;
38672 pragma_tok = cp_lexer_consume_token (parser->lexer);
38673 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38674 parser->lexer->in_pragma = true;
38676 id = cp_parser_pragma_kind (pragma_tok);
38677 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38678 cp_ensure_no_omp_declare_simd (parser);
38679 switch (id)
38681 case PRAGMA_GCC_PCH_PREPROCESS:
38682 error_at (pragma_tok->location,
38683 "%<#pragma GCC pch_preprocess%> must be first");
38684 break;
38686 case PRAGMA_OMP_BARRIER:
38687 switch (context)
38689 case pragma_compound:
38690 cp_parser_omp_barrier (parser, pragma_tok);
38691 return false;
38692 case pragma_stmt:
38693 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38694 "used in compound statements", "omp barrier");
38695 break;
38696 default:
38697 goto bad_stmt;
38699 break;
38701 case PRAGMA_OMP_FLUSH:
38702 switch (context)
38704 case pragma_compound:
38705 cp_parser_omp_flush (parser, pragma_tok);
38706 return false;
38707 case pragma_stmt:
38708 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38709 "used in compound statements", "omp flush");
38710 break;
38711 default:
38712 goto bad_stmt;
38714 break;
38716 case PRAGMA_OMP_TASKWAIT:
38717 switch (context)
38719 case pragma_compound:
38720 cp_parser_omp_taskwait (parser, pragma_tok);
38721 return false;
38722 case pragma_stmt:
38723 error_at (pragma_tok->location,
38724 "%<#pragma %s%> may only be used in compound statements",
38725 "omp taskwait");
38726 break;
38727 default:
38728 goto bad_stmt;
38730 break;
38732 case PRAGMA_OMP_TASKYIELD:
38733 switch (context)
38735 case pragma_compound:
38736 cp_parser_omp_taskyield (parser, pragma_tok);
38737 return false;
38738 case pragma_stmt:
38739 error_at (pragma_tok->location,
38740 "%<#pragma %s%> may only be used in compound statements",
38741 "omp taskyield");
38742 break;
38743 default:
38744 goto bad_stmt;
38746 break;
38748 case PRAGMA_OMP_CANCEL:
38749 switch (context)
38751 case pragma_compound:
38752 cp_parser_omp_cancel (parser, pragma_tok);
38753 return false;
38754 case pragma_stmt:
38755 error_at (pragma_tok->location,
38756 "%<#pragma %s%> may only be used in compound statements",
38757 "omp cancel");
38758 break;
38759 default:
38760 goto bad_stmt;
38762 break;
38764 case PRAGMA_OMP_CANCELLATION_POINT:
38765 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38766 return false;
38768 case PRAGMA_OMP_THREADPRIVATE:
38769 cp_parser_omp_threadprivate (parser, pragma_tok);
38770 return false;
38772 case PRAGMA_OMP_DECLARE:
38773 return cp_parser_omp_declare (parser, pragma_tok, context);
38775 case PRAGMA_OACC_DECLARE:
38776 cp_parser_oacc_declare (parser, pragma_tok);
38777 return false;
38779 case PRAGMA_OACC_ENTER_DATA:
38780 if (context == pragma_stmt)
38782 error_at (pragma_tok->location,
38783 "%<#pragma %s%> may only be used in compound statements",
38784 "acc enter data");
38785 break;
38787 else if (context != pragma_compound)
38788 goto bad_stmt;
38789 cp_parser_omp_construct (parser, pragma_tok, if_p);
38790 return true;
38792 case PRAGMA_OACC_EXIT_DATA:
38793 if (context == pragma_stmt)
38795 error_at (pragma_tok->location,
38796 "%<#pragma %s%> may only be used in compound statements",
38797 "acc exit data");
38798 break;
38800 else if (context != pragma_compound)
38801 goto bad_stmt;
38802 cp_parser_omp_construct (parser, pragma_tok, if_p);
38803 return true;
38805 case PRAGMA_OACC_ROUTINE:
38806 if (context != pragma_external)
38808 error_at (pragma_tok->location,
38809 "%<#pragma acc routine%> must be at file scope");
38810 break;
38812 cp_parser_oacc_routine (parser, pragma_tok, context);
38813 return false;
38815 case PRAGMA_OACC_UPDATE:
38816 if (context == pragma_stmt)
38818 error_at (pragma_tok->location,
38819 "%<#pragma %s%> may only be used in compound statements",
38820 "acc update");
38821 break;
38823 else if (context != pragma_compound)
38824 goto bad_stmt;
38825 cp_parser_omp_construct (parser, pragma_tok, if_p);
38826 return true;
38828 case PRAGMA_OACC_WAIT:
38829 if (context == pragma_stmt)
38831 error_at (pragma_tok->location,
38832 "%<#pragma %s%> may only be used in compound statements",
38833 "acc wait");
38834 break;
38836 else if (context != pragma_compound)
38837 goto bad_stmt;
38838 cp_parser_omp_construct (parser, pragma_tok, if_p);
38839 return true;
38841 case PRAGMA_OACC_ATOMIC:
38842 case PRAGMA_OACC_CACHE:
38843 case PRAGMA_OACC_DATA:
38844 case PRAGMA_OACC_HOST_DATA:
38845 case PRAGMA_OACC_KERNELS:
38846 case PRAGMA_OACC_PARALLEL:
38847 case PRAGMA_OACC_LOOP:
38848 case PRAGMA_OMP_ATOMIC:
38849 case PRAGMA_OMP_CRITICAL:
38850 case PRAGMA_OMP_DISTRIBUTE:
38851 case PRAGMA_OMP_FOR:
38852 case PRAGMA_OMP_MASTER:
38853 case PRAGMA_OMP_PARALLEL:
38854 case PRAGMA_OMP_SECTIONS:
38855 case PRAGMA_OMP_SIMD:
38856 case PRAGMA_OMP_SINGLE:
38857 case PRAGMA_OMP_TASK:
38858 case PRAGMA_OMP_TASKGROUP:
38859 case PRAGMA_OMP_TASKLOOP:
38860 case PRAGMA_OMP_TEAMS:
38861 if (context != pragma_stmt && context != pragma_compound)
38862 goto bad_stmt;
38863 stmt = push_omp_privatization_clauses (false);
38864 cp_parser_omp_construct (parser, pragma_tok, if_p);
38865 pop_omp_privatization_clauses (stmt);
38866 return true;
38868 case PRAGMA_OMP_ORDERED:
38869 if (context != pragma_stmt && context != pragma_compound)
38870 goto bad_stmt;
38871 stmt = push_omp_privatization_clauses (false);
38872 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38873 pop_omp_privatization_clauses (stmt);
38874 return ret;
38876 case PRAGMA_OMP_TARGET:
38877 if (context != pragma_stmt && context != pragma_compound)
38878 goto bad_stmt;
38879 stmt = push_omp_privatization_clauses (false);
38880 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38881 pop_omp_privatization_clauses (stmt);
38882 return ret;
38884 case PRAGMA_OMP_END_DECLARE_TARGET:
38885 cp_parser_omp_end_declare_target (parser, pragma_tok);
38886 return false;
38888 case PRAGMA_OMP_SECTION:
38889 error_at (pragma_tok->location,
38890 "%<#pragma omp section%> may only be used in "
38891 "%<#pragma omp sections%> construct");
38892 break;
38894 case PRAGMA_IVDEP:
38896 if (context == pragma_external)
38898 error_at (pragma_tok->location,
38899 "%<#pragma GCC ivdep%> must be inside a function");
38900 break;
38902 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38903 unsigned short unroll;
38904 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38905 if (tok->type == CPP_PRAGMA
38906 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38908 tok = cp_lexer_consume_token (parser->lexer);
38909 unroll = cp_parser_pragma_unroll (parser, tok);
38910 tok = cp_lexer_peek_token (the_parser->lexer);
38912 else
38913 unroll = 0;
38914 if (tok->type != CPP_KEYWORD
38915 || (tok->keyword != RID_FOR
38916 && tok->keyword != RID_WHILE
38917 && tok->keyword != RID_DO))
38919 cp_parser_error (parser, "for, while or do statement expected");
38920 return false;
38922 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38923 return true;
38926 case PRAGMA_UNROLL:
38928 if (context == pragma_external)
38930 error_at (pragma_tok->location,
38931 "%<#pragma GCC unroll%> must be inside a function");
38932 break;
38934 const unsigned short unroll
38935 = cp_parser_pragma_unroll (parser, pragma_tok);
38936 bool ivdep;
38937 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38938 if (tok->type == CPP_PRAGMA
38939 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38941 tok = cp_lexer_consume_token (parser->lexer);
38942 ivdep = cp_parser_pragma_ivdep (parser, tok);
38943 tok = cp_lexer_peek_token (the_parser->lexer);
38945 else
38946 ivdep = false;
38947 if (tok->type != CPP_KEYWORD
38948 || (tok->keyword != RID_FOR
38949 && tok->keyword != RID_WHILE
38950 && tok->keyword != RID_DO))
38952 cp_parser_error (parser, "for, while or do statement expected");
38953 return false;
38955 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38956 return true;
38959 default:
38960 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38961 c_invoke_pragma_handler (id);
38962 break;
38964 bad_stmt:
38965 cp_parser_error (parser, "expected declaration specifiers");
38966 break;
38969 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38970 return false;
38973 /* The interface the pragma parsers have to the lexer. */
38975 enum cpp_ttype
38976 pragma_lex (tree *value, location_t *loc)
38978 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38979 enum cpp_ttype ret = tok->type;
38981 *value = tok->u.value;
38982 if (loc)
38983 *loc = tok->location;
38985 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38986 ret = CPP_EOF;
38987 else if (ret == CPP_STRING)
38988 *value = cp_parser_string_literal (the_parser, false, false);
38989 else
38991 if (ret == CPP_KEYWORD)
38992 ret = CPP_NAME;
38993 cp_lexer_consume_token (the_parser->lexer);
38996 return ret;
39000 /* External interface. */
39002 /* Parse one entire translation unit. */
39004 void
39005 c_parse_file (void)
39007 static bool already_called = false;
39009 if (already_called)
39010 fatal_error (input_location,
39011 "inter-module optimizations not implemented for C++");
39012 already_called = true;
39014 the_parser = cp_parser_new ();
39015 push_deferring_access_checks (flag_access_control
39016 ? dk_no_deferred : dk_no_check);
39017 cp_parser_translation_unit (the_parser);
39018 the_parser = NULL;
39021 /* Create an identifier for a generic parameter type (a synthesized
39022 template parameter implied by `auto' or a concept identifier). */
39024 static GTY(()) int generic_parm_count;
39025 static tree
39026 make_generic_type_name ()
39028 char buf[32];
39029 sprintf (buf, "auto:%d", ++generic_parm_count);
39030 return get_identifier (buf);
39033 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39034 (creating a new template parameter list if necessary). Returns the newly
39035 created template type parm. */
39037 static tree
39038 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39040 gcc_assert (current_binding_level->kind == sk_function_parms);
39042 /* Before committing to modifying any scope, if we're in an
39043 implicit template scope, and we're trying to synthesize a
39044 constrained parameter, try to find a previous parameter with
39045 the same name. This is the same-type rule for abbreviated
39046 function templates.
39048 NOTE: We can generate implicit parameters when tentatively
39049 parsing a nested name specifier, only to reject that parse
39050 later. However, matching the same template-id as part of a
39051 direct-declarator should generate an identical template
39052 parameter, so this rule will merge them. */
39053 if (parser->implicit_template_scope && constr)
39055 tree t = parser->implicit_template_parms;
39056 while (t)
39058 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39060 tree d = TREE_VALUE (t);
39061 if (TREE_CODE (d) == PARM_DECL)
39062 /* Return the TEMPLATE_PARM_INDEX. */
39063 d = DECL_INITIAL (d);
39064 return d;
39066 t = TREE_CHAIN (t);
39070 /* We are either continuing a function template that already contains implicit
39071 template parameters, creating a new fully-implicit function template, or
39072 extending an existing explicit function template with implicit template
39073 parameters. */
39075 cp_binding_level *const entry_scope = current_binding_level;
39077 bool become_template = false;
39078 cp_binding_level *parent_scope = 0;
39080 if (parser->implicit_template_scope)
39082 gcc_assert (parser->implicit_template_parms);
39084 current_binding_level = parser->implicit_template_scope;
39086 else
39088 /* Roll back to the existing template parameter scope (in the case of
39089 extending an explicit function template) or introduce a new template
39090 parameter scope ahead of the function parameter scope (or class scope
39091 in the case of out-of-line member definitions). The function scope is
39092 added back after template parameter synthesis below. */
39094 cp_binding_level *scope = entry_scope;
39096 while (scope->kind == sk_function_parms)
39098 parent_scope = scope;
39099 scope = scope->level_chain;
39101 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39103 /* If not defining a class, then any class scope is a scope level in
39104 an out-of-line member definition. In this case simply wind back
39105 beyond the first such scope to inject the template parameter list.
39106 Otherwise wind back to the class being defined. The latter can
39107 occur in class member friend declarations such as:
39109 class A {
39110 void foo (auto);
39112 class B {
39113 friend void A::foo (auto);
39116 The template parameter list synthesized for the friend declaration
39117 must be injected in the scope of 'B'. This can also occur in
39118 erroneous cases such as:
39120 struct A {
39121 struct B {
39122 void foo (auto);
39124 void B::foo (auto) {}
39127 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39128 but, nevertheless, the template parameter list synthesized for the
39129 declarator should be injected into the scope of 'A' as if the
39130 ill-formed template was specified explicitly. */
39132 while (scope->kind == sk_class && !scope->defining_class_p)
39134 parent_scope = scope;
39135 scope = scope->level_chain;
39139 current_binding_level = scope;
39141 if (scope->kind != sk_template_parms
39142 || !function_being_declared_is_template_p (parser))
39144 /* Introduce a new template parameter list for implicit template
39145 parameters. */
39147 become_template = true;
39149 parser->implicit_template_scope
39150 = begin_scope (sk_template_parms, NULL);
39152 ++processing_template_decl;
39154 parser->fully_implicit_function_template_p = true;
39155 ++parser->num_template_parameter_lists;
39157 else
39159 /* Synthesize implicit template parameters at the end of the explicit
39160 template parameter list. */
39162 gcc_assert (current_template_parms);
39164 parser->implicit_template_scope = scope;
39166 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39167 parser->implicit_template_parms
39168 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39172 /* Synthesize a new template parameter and track the current template
39173 parameter chain with implicit_template_parms. */
39175 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39176 tree synth_id = make_generic_type_name ();
39177 tree synth_tmpl_parm;
39178 bool non_type = false;
39180 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39181 synth_tmpl_parm
39182 = finish_template_type_parm (class_type_node, synth_id);
39183 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39184 synth_tmpl_parm
39185 = finish_constrained_template_template_parm (proto, synth_id);
39186 else
39188 synth_tmpl_parm = copy_decl (proto);
39189 DECL_NAME (synth_tmpl_parm) = synth_id;
39190 non_type = true;
39193 // Attach the constraint to the parm before processing.
39194 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39195 TREE_TYPE (node) = constr;
39196 tree new_parm
39197 = process_template_parm (parser->implicit_template_parms,
39198 input_location,
39199 node,
39200 /*non_type=*/non_type,
39201 /*param_pack=*/false);
39203 // Chain the new parameter to the list of implicit parameters.
39204 if (parser->implicit_template_parms)
39205 parser->implicit_template_parms
39206 = TREE_CHAIN (parser->implicit_template_parms);
39207 else
39208 parser->implicit_template_parms = new_parm;
39210 tree new_decl = get_local_decls ();
39211 if (non_type)
39212 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39213 new_decl = DECL_INITIAL (new_decl);
39215 /* If creating a fully implicit function template, start the new implicit
39216 template parameter list with this synthesized type, otherwise grow the
39217 current template parameter list. */
39219 if (become_template)
39221 parent_scope->level_chain = current_binding_level;
39223 tree new_parms = make_tree_vec (1);
39224 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39225 current_template_parms = tree_cons (size_int (processing_template_decl),
39226 new_parms, current_template_parms);
39228 else
39230 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39231 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39232 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39233 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39236 // If the new parameter was constrained, we need to add that to the
39237 // constraints in the template parameter list.
39238 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39240 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39241 reqs = conjoin_constraints (reqs, req);
39242 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39245 current_binding_level = entry_scope;
39247 return new_decl;
39250 /* Finish the declaration of a fully implicit function template. Such a
39251 template has no explicit template parameter list so has not been through the
39252 normal template head and tail processing. synthesize_implicit_template_parm
39253 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39254 provided if the declaration is a class member such that its template
39255 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39256 form is returned. Otherwise NULL_TREE is returned. */
39258 static tree
39259 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39261 gcc_assert (parser->fully_implicit_function_template_p);
39263 if (member_decl_opt && member_decl_opt != error_mark_node
39264 && DECL_VIRTUAL_P (member_decl_opt))
39266 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39267 "implicit templates may not be %<virtual%>");
39268 DECL_VIRTUAL_P (member_decl_opt) = false;
39271 if (member_decl_opt)
39272 member_decl_opt = finish_member_template_decl (member_decl_opt);
39273 end_template_decl ();
39275 parser->fully_implicit_function_template_p = false;
39276 --parser->num_template_parameter_lists;
39278 return member_decl_opt;
39281 /* Helper function for diagnostics that have complained about things
39282 being used with 'extern "C"' linkage.
39284 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39286 void
39287 maybe_show_extern_c_location (void)
39289 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39290 inform (the_parser->innermost_linkage_specification_location,
39291 "%<extern \"C\"%> linkage started here");
39294 #include "gt-cp-parser.h"