PR c++/86728 - C variadic generic lambda.
[official-gcc.git] / gcc / cp / parser.c
blob8cfcd150705f5a79e96fdf55ddfa4cf109294f15
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 *);
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 *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2267 static void abort_fully_implicit_template
2268 (cp_parser *);
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275 (cp_parser *);
2276 static tree cp_parser_class_head
2277 (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279 (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281 (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283 (cp_parser *);
2284 static void cp_parser_member_declaration
2285 (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287 (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289 (cp_parser *);
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2294 (cp_parser *);
2295 static tree cp_parser_base_specifier
2296 (cp_parser *);
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2301 (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303 (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305 (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307 (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313 (cp_parser *);
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2318 (cp_parser *);
2319 static cp_expr cp_parser_operator
2320 (cp_parser *);
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327 (cp_parser *);
2328 static tree cp_parser_template_parameter
2329 (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337 (cp_parser *);
2338 static tree cp_parser_template_argument
2339 (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341 (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343 (cp_parser *);
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2348 (cp_parser *);
2349 static void cp_parser_function_try_block
2350 (cp_parser *);
2351 static void cp_parser_handler_seq
2352 (cp_parser *);
2353 static void cp_parser_handler
2354 (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356 (cp_parser *);
2357 static tree cp_parser_throw_expression
2358 (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_type_id_list
2362 (cp_parser *);
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379 (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391 (cp_parser *);
2392 static tree cp_parser_std_attribute
2393 (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397 (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser *, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static void cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool, location_t);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646 (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648 (cp_parser *);
2650 /* Concept-related syntactic transformations */
2652 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand;
2662 ++c_inhibit_evaluation_warnings;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings;
2668 --cp_unevaluated_operand;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2679 return parser->context->next != NULL;
2682 /* Returns nonzero if TOKEN is a string literal. */
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2687 return (token->type == CPP_STRING ||
2688 token->type == CPP_STRING16 ||
2689 token->type == CPP_STRING32 ||
2690 token->type == CPP_WSTRING ||
2691 token->type == CPP_UTF8STRING);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2700 return (cp_parser_is_pure_string_literal (token) ||
2701 token->type == CPP_STRING_USERDEF ||
2702 token->type == CPP_STRING16_USERDEF ||
2703 token->type == CPP_STRING32_USERDEF ||
2704 token->type == CPP_WSTRING_USERDEF ||
2705 token->type == CPP_UTF8STRING_USERDEF);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2713 return token->keyword == keyword;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 PRAGMA_NONE. */
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2722 if (token->type != CPP_PRAGMA)
2723 return PRAGMA_NONE;
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2737 of the marker. */
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741 location_t *out_loc)
2743 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744 if (token2->type != tok1_kind)
2745 return false;
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2748 return false;
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751 return false;
2753 /* It must be at the start of the line. */
2754 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755 if (LOCATION_COLUMN (start_loc) != 1)
2756 return false;
2758 /* We have a conflict marker. Construct a location of the form:
2759 <<<<<<<
2760 ^~~~~~~
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc = get_finish (token4->location);
2763 *out_loc = make_location (start_loc, start_loc, finish_loc);
2765 return true;
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769 RT_CLOSE_PAREN. */
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2774 switch (token_desc)
2776 default:
2777 gcc_unreachable ();
2778 return "";
2779 case RT_CLOSE_BRACE:
2780 return "{";
2781 case RT_CLOSE_PAREN:
2782 return "(";
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2792 switch (token_desc)
2794 case RT_SEMICOLON:
2795 return CPP_SEMICOLON;
2796 case RT_OPEN_PAREN:
2797 return CPP_OPEN_PAREN;
2798 case RT_CLOSE_BRACE:
2799 return CPP_CLOSE_BRACE;
2800 case RT_OPEN_BRACE:
2801 return CPP_OPEN_BRACE;
2802 case RT_CLOSE_SQUARE:
2803 return CPP_CLOSE_SQUARE;
2804 case RT_OPEN_SQUARE:
2805 return CPP_OPEN_SQUARE;
2806 case RT_COMMA:
2807 return CPP_COMMA;
2808 case RT_COLON:
2809 return CPP_COLON;
2810 case RT_CLOSE_PAREN:
2811 return CPP_CLOSE_PAREN;
2813 default:
2814 /* Use CPP_EOF as a "no completions possible" code. */
2815 return CPP_EOF;
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2826 OTHER-TOKEN".
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836 location. */
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840 required_token missing_token_desc,
2841 location_t matching_location)
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token);
2848 if (token->type == CPP_PRAGMA)
2850 error_at (token->location,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser, token);
2853 return;
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token->type == CPP_LSHIFT
2858 || token->type == CPP_RSHIFT
2859 || token->type == CPP_EQ_EQ)
2861 location_t loc;
2862 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2864 error_at (loc, "version control conflict marker in file");
2865 expanded_location token_exploc = expand_location (token->location);
2866 /* Consume tokens until the end of the source line. */
2867 while (1)
2869 cp_lexer_consume_token (parser->lexer);
2870 cp_token *next = cp_lexer_peek_token (parser->lexer);
2871 if (next == NULL)
2872 break;
2873 expanded_location next_exploc = expand_location (next->location);
2874 if (next_exploc.file != token_exploc.file)
2875 break;
2876 if (next_exploc.line != token_exploc.line)
2877 break;
2879 return;
2883 gcc_rich_location richloc (input_location);
2885 bool added_matching_location = false;
2887 if (missing_token_desc != RT_NONE)
2889 /* Potentially supply a fix-it hint, suggesting to add the
2890 missing token immediately after the *previous* token.
2891 This may move the primary location within richloc. */
2892 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2893 location_t prev_token_loc
2894 = cp_lexer_previous_token (parser->lexer)->location;
2895 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2897 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2898 Attempt to consolidate diagnostics by printing it as a
2899 secondary range within the main diagnostic. */
2900 if (matching_location != UNKNOWN_LOCATION)
2901 added_matching_location
2902 = richloc.add_location_if_nearby (matching_location);
2905 /* Actually emit the error. */
2906 c_parse_error (gmsgid,
2907 /* Because c_parser_error does not understand
2908 CPP_KEYWORD, keywords are treated like
2909 identifiers. */
2910 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2911 token->u.value, token->flags, &richloc);
2913 if (missing_token_desc != RT_NONE)
2915 /* If we weren't able to consolidate matching_location, then
2916 print it as a secondary diagnostic. */
2917 if (matching_location != UNKNOWN_LOCATION
2918 && !added_matching_location)
2919 inform (matching_location, "to match this %qs",
2920 get_matching_symbol (missing_token_desc));
2924 /* If not parsing tentatively, issue a diagnostic of the form
2925 FILE:LINE: MESSAGE before TOKEN
2926 where TOKEN is the next token in the input stream. MESSAGE
2927 (specified by the caller) is usually of the form "expected
2928 OTHER-TOKEN". */
2930 static void
2931 cp_parser_error (cp_parser* parser, const char* gmsgid)
2933 if (!cp_parser_simulate_error (parser))
2934 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2937 /* Issue an error about name-lookup failing. NAME is the
2938 IDENTIFIER_NODE DECL is the result of
2939 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2940 the thing that we hoped to find. */
2942 static void
2943 cp_parser_name_lookup_error (cp_parser* parser,
2944 tree name,
2945 tree decl,
2946 name_lookup_error desired,
2947 location_t location)
2949 /* If name lookup completely failed, tell the user that NAME was not
2950 declared. */
2951 if (decl == error_mark_node)
2953 if (parser->scope && parser->scope != global_namespace)
2954 error_at (location, "%<%E::%E%> has not been declared",
2955 parser->scope, name);
2956 else if (parser->scope == global_namespace)
2957 error_at (location, "%<::%E%> has not been declared", name);
2958 else if (parser->object_scope
2959 && !CLASS_TYPE_P (parser->object_scope))
2960 error_at (location, "request for member %qE in non-class type %qT",
2961 name, parser->object_scope);
2962 else if (parser->object_scope)
2963 error_at (location, "%<%T::%E%> has not been declared",
2964 parser->object_scope, name);
2965 else
2966 error_at (location, "%qE has not been declared", name);
2968 else if (parser->scope && parser->scope != global_namespace)
2970 switch (desired)
2972 case NLE_TYPE:
2973 error_at (location, "%<%E::%E%> is not a type",
2974 parser->scope, name);
2975 break;
2976 case NLE_CXX98:
2977 error_at (location, "%<%E::%E%> is not a class or namespace",
2978 parser->scope, name);
2979 break;
2980 case NLE_NOT_CXX98:
2981 error_at (location,
2982 "%<%E::%E%> is not a class, namespace, or enumeration",
2983 parser->scope, name);
2984 break;
2985 default:
2986 gcc_unreachable ();
2990 else if (parser->scope == global_namespace)
2992 switch (desired)
2994 case NLE_TYPE:
2995 error_at (location, "%<::%E%> is not a type", name);
2996 break;
2997 case NLE_CXX98:
2998 error_at (location, "%<::%E%> is not a class or namespace", name);
2999 break;
3000 case NLE_NOT_CXX98:
3001 error_at (location,
3002 "%<::%E%> is not a class, namespace, or enumeration",
3003 name);
3004 break;
3005 default:
3006 gcc_unreachable ();
3009 else
3011 switch (desired)
3013 case NLE_TYPE:
3014 error_at (location, "%qE is not a type", name);
3015 break;
3016 case NLE_CXX98:
3017 error_at (location, "%qE is not a class or namespace", name);
3018 break;
3019 case NLE_NOT_CXX98:
3020 error_at (location,
3021 "%qE is not a class, namespace, or enumeration", name);
3022 break;
3023 default:
3024 gcc_unreachable ();
3029 /* If we are parsing tentatively, remember that an error has occurred
3030 during this tentative parse. Returns true if the error was
3031 simulated; false if a message should be issued by the caller. */
3033 static bool
3034 cp_parser_simulate_error (cp_parser* parser)
3036 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3038 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3039 return true;
3041 return false;
3044 /* This function is called when a type is defined. If type
3045 definitions are forbidden at this point, an error message is
3046 issued. */
3048 static bool
3049 cp_parser_check_type_definition (cp_parser* parser)
3051 /* If types are forbidden here, issue a message. */
3052 if (parser->type_definition_forbidden_message)
3054 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3055 in the message need to be interpreted. */
3056 error (parser->type_definition_forbidden_message);
3057 return false;
3059 return true;
3062 /* This function is called when the DECLARATOR is processed. The TYPE
3063 was a type defined in the decl-specifiers. If it is invalid to
3064 define a type in the decl-specifiers for DECLARATOR, an error is
3065 issued. TYPE_LOCATION is the location of TYPE and is used
3066 for error reporting. */
3068 static void
3069 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3070 tree type, location_t type_location)
3072 /* [dcl.fct] forbids type definitions in return types.
3073 Unfortunately, it's not easy to know whether or not we are
3074 processing a return type until after the fact. */
3075 while (declarator
3076 && (declarator->kind == cdk_pointer
3077 || declarator->kind == cdk_reference
3078 || declarator->kind == cdk_ptrmem))
3079 declarator = declarator->declarator;
3080 if (declarator
3081 && declarator->kind == cdk_function)
3083 error_at (type_location,
3084 "new types may not be defined in a return type");
3085 inform (type_location,
3086 "(perhaps a semicolon is missing after the definition of %qT)",
3087 type);
3091 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3092 "<" in any valid C++ program. If the next token is indeed "<",
3093 issue a message warning the user about what appears to be an
3094 invalid attempt to form a template-id. LOCATION is the location
3095 of the type-specifier (TYPE) */
3097 static void
3098 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3099 tree type,
3100 enum tag_types tag_type,
3101 location_t location)
3103 cp_token_position start = 0;
3105 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3107 if (TREE_CODE (type) == TYPE_DECL)
3108 type = TREE_TYPE (type);
3109 if (TYPE_P (type) && !template_placeholder_p (type))
3110 error_at (location, "%qT is not a template", type);
3111 else if (identifier_p (type))
3113 if (tag_type != none_type)
3114 error_at (location, "%qE is not a class template", type);
3115 else
3116 error_at (location, "%qE is not a template", type);
3118 else
3119 error_at (location, "invalid template-id");
3120 /* Remember the location of the invalid "<". */
3121 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3122 start = cp_lexer_token_position (parser->lexer, true);
3123 /* Consume the "<". */
3124 cp_lexer_consume_token (parser->lexer);
3125 /* Parse the template arguments. */
3126 cp_parser_enclosed_template_argument_list (parser);
3127 /* Permanently remove the invalid template arguments so that
3128 this error message is not issued again. */
3129 if (start)
3130 cp_lexer_purge_tokens_after (parser->lexer, start);
3134 /* If parsing an integral constant-expression, issue an error message
3135 about the fact that THING appeared and return true. Otherwise,
3136 return false. In either case, set
3137 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3139 static bool
3140 cp_parser_non_integral_constant_expression (cp_parser *parser,
3141 non_integral_constant thing)
3143 parser->non_integral_constant_expression_p = true;
3144 if (parser->integral_constant_expression_p)
3146 if (!parser->allow_non_integral_constant_expression_p)
3148 const char *msg = NULL;
3149 switch (thing)
3151 case NIC_FLOAT:
3152 pedwarn (input_location, OPT_Wpedantic,
3153 "ISO C++ forbids using a floating-point literal "
3154 "in a constant-expression");
3155 return true;
3156 case NIC_CAST:
3157 error ("a cast to a type other than an integral or "
3158 "enumeration type cannot appear in a "
3159 "constant-expression");
3160 return true;
3161 case NIC_TYPEID:
3162 error ("%<typeid%> operator "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_NCC:
3166 error ("non-constant compound literals "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_FUNC_CALL:
3170 error ("a function call "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_INC:
3174 error ("an increment "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_DEC:
3178 error ("an decrement "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_ARRAY_REF:
3182 error ("an array reference "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_ADDR_LABEL:
3186 error ("the address of a label "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_OVERLOADED:
3190 error ("calls to overloaded operators "
3191 "cannot appear in a constant-expression");
3192 return true;
3193 case NIC_ASSIGNMENT:
3194 error ("an assignment cannot appear in a constant-expression");
3195 return true;
3196 case NIC_COMMA:
3197 error ("a comma operator "
3198 "cannot appear in a constant-expression");
3199 return true;
3200 case NIC_CONSTRUCTOR:
3201 error ("a call to a constructor "
3202 "cannot appear in a constant-expression");
3203 return true;
3204 case NIC_TRANSACTION:
3205 error ("a transaction expression "
3206 "cannot appear in a constant-expression");
3207 return true;
3208 case NIC_THIS:
3209 msg = "this";
3210 break;
3211 case NIC_FUNC_NAME:
3212 msg = "__FUNCTION__";
3213 break;
3214 case NIC_PRETTY_FUNC:
3215 msg = "__PRETTY_FUNCTION__";
3216 break;
3217 case NIC_C99_FUNC:
3218 msg = "__func__";
3219 break;
3220 case NIC_VA_ARG:
3221 msg = "va_arg";
3222 break;
3223 case NIC_ARROW:
3224 msg = "->";
3225 break;
3226 case NIC_POINT:
3227 msg = ".";
3228 break;
3229 case NIC_STAR:
3230 msg = "*";
3231 break;
3232 case NIC_ADDR:
3233 msg = "&";
3234 break;
3235 case NIC_PREINCREMENT:
3236 msg = "++";
3237 break;
3238 case NIC_PREDECREMENT:
3239 msg = "--";
3240 break;
3241 case NIC_NEW:
3242 msg = "new";
3243 break;
3244 case NIC_DEL:
3245 msg = "delete";
3246 break;
3247 default:
3248 gcc_unreachable ();
3250 if (msg)
3251 error ("%qs cannot appear in a constant-expression", msg);
3252 return true;
3255 return false;
3258 /* Emit a diagnostic for an invalid type name. This function commits
3259 to the current active tentative parse, if any. (Otherwise, the
3260 problematic construct might be encountered again later, resulting
3261 in duplicate error messages.) LOCATION is the location of ID. */
3263 static void
3264 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3265 location_t location)
3267 tree decl, ambiguous_decls;
3268 cp_parser_commit_to_tentative_parse (parser);
3269 /* Try to lookup the identifier. */
3270 decl = cp_parser_lookup_name (parser, id, none_type,
3271 /*is_template=*/false,
3272 /*is_namespace=*/false,
3273 /*check_dependency=*/true,
3274 &ambiguous_decls, location);
3275 if (ambiguous_decls)
3276 /* If the lookup was ambiguous, an error will already have
3277 been issued. */
3278 return;
3279 /* If the lookup found a template-name, it means that the user forgot
3280 to specify an argument list. Emit a useful error message. */
3281 if (DECL_TYPE_TEMPLATE_P (decl))
3283 error_at (location,
3284 "invalid use of template-name %qE without an argument list",
3285 decl);
3286 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3287 inform (location, "class template argument deduction is only available "
3288 "with -std=c++17 or -std=gnu++17");
3289 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3291 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3292 error_at (location, "invalid use of destructor %qD as a type", id);
3293 else if (TREE_CODE (decl) == TYPE_DECL)
3294 /* Something like 'unsigned A a;' */
3295 error_at (location, "invalid combination of multiple type-specifiers");
3296 else if (!parser->scope)
3298 /* Issue an error message. */
3299 name_hint hint;
3300 if (TREE_CODE (id) == IDENTIFIER_NODE)
3301 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3302 if (hint)
3304 gcc_rich_location richloc (location);
3305 richloc.add_fixit_replace (hint.suggestion ());
3306 error_at (&richloc,
3307 "%qE does not name a type; did you mean %qs?",
3308 id, hint.suggestion ());
3310 else
3311 error_at (location, "%qE does not name a type", id);
3312 /* If we're in a template class, it's possible that the user was
3313 referring to a type from a base class. For example:
3315 template <typename T> struct A { typedef T X; };
3316 template <typename T> struct B : public A<T> { X x; };
3318 The user should have said "typename A<T>::X". */
3319 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3320 inform (location, "C++11 %<constexpr%> only available with "
3321 "-std=c++11 or -std=gnu++11");
3322 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3323 inform (location, "C++11 %<noexcept%> only available with "
3324 "-std=c++11 or -std=gnu++11");
3325 else if (cxx_dialect < cxx11
3326 && TREE_CODE (id) == IDENTIFIER_NODE
3327 && id_equal (id, "thread_local"))
3328 inform (location, "C++11 %<thread_local%> only available with "
3329 "-std=c++11 or -std=gnu++11");
3330 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3331 inform (location, "%<concept%> only available with -fconcepts");
3332 else if (processing_template_decl && current_class_type
3333 && TYPE_BINFO (current_class_type))
3335 tree b;
3337 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3339 b = TREE_CHAIN (b))
3341 tree base_type = BINFO_TYPE (b);
3342 if (CLASS_TYPE_P (base_type)
3343 && dependent_type_p (base_type))
3345 tree field;
3346 /* Go from a particular instantiation of the
3347 template (which will have an empty TYPE_FIELDs),
3348 to the main version. */
3349 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3350 for (field = TYPE_FIELDS (base_type);
3351 field;
3352 field = DECL_CHAIN (field))
3353 if (TREE_CODE (field) == TYPE_DECL
3354 && DECL_NAME (field) == id)
3356 inform (location,
3357 "(perhaps %<typename %T::%E%> was intended)",
3358 BINFO_TYPE (b), id);
3359 break;
3361 if (field)
3362 break;
3367 /* Here we diagnose qualified-ids where the scope is actually correct,
3368 but the identifier does not resolve to a valid type name. */
3369 else if (parser->scope != error_mark_node)
3371 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3373 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3374 error_at (location_of (id),
3375 "%qE in namespace %qE does not name a template type",
3376 id, parser->scope);
3377 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3378 error_at (location_of (id),
3379 "%qE in namespace %qE does not name a template type",
3380 TREE_OPERAND (id, 0), parser->scope);
3381 else
3382 error_at (location_of (id),
3383 "%qE in namespace %qE does not name a type",
3384 id, parser->scope);
3385 if (DECL_P (decl))
3386 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3387 else if (decl == error_mark_node)
3388 suggest_alternative_in_explicit_scope (location, id,
3389 parser->scope);
3391 else if (CLASS_TYPE_P (parser->scope)
3392 && constructor_name_p (id, parser->scope))
3394 /* A<T>::A<T>() */
3395 error_at (location, "%<%T::%E%> names the constructor, not"
3396 " the type", parser->scope, id);
3397 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3398 error_at (location, "and %qT has no template constructors",
3399 parser->scope);
3401 else if (TYPE_P (parser->scope)
3402 && dependent_scope_p (parser->scope))
3404 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3405 error_at (location,
3406 "need %<typename%> before %<%T::%D::%E%> because "
3407 "%<%T::%D%> is a dependent scope",
3408 TYPE_CONTEXT (parser->scope),
3409 TYPENAME_TYPE_FULLNAME (parser->scope),
3411 TYPE_CONTEXT (parser->scope),
3412 TYPENAME_TYPE_FULLNAME (parser->scope));
3413 else
3414 error_at (location, "need %<typename%> before %<%T::%E%> because "
3415 "%qT is a dependent scope",
3416 parser->scope, id, parser->scope);
3418 else if (TYPE_P (parser->scope))
3420 if (!COMPLETE_TYPE_P (parser->scope))
3421 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3422 parser->scope);
3423 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3424 error_at (location_of (id),
3425 "%qE in %q#T does not name a template type",
3426 id, parser->scope);
3427 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3428 error_at (location_of (id),
3429 "%qE in %q#T does not name a template type",
3430 TREE_OPERAND (id, 0), parser->scope);
3431 else
3432 error_at (location_of (id),
3433 "%qE in %q#T does not name a type",
3434 id, parser->scope);
3435 if (DECL_P (decl))
3436 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3438 else
3439 gcc_unreachable ();
3443 /* Check for a common situation where a type-name should be present,
3444 but is not, and issue a sensible error message. Returns true if an
3445 invalid type-name was detected.
3447 The situation handled by this function are variable declarations of the
3448 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3449 Usually, `ID' should name a type, but if we got here it means that it
3450 does not. We try to emit the best possible error message depending on
3451 how exactly the id-expression looks like. */
3453 static bool
3454 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3456 tree id;
3457 cp_token *token = cp_lexer_peek_token (parser->lexer);
3459 /* Avoid duplicate error about ambiguous lookup. */
3460 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3462 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3463 if (next->type == CPP_NAME && next->error_reported)
3464 goto out;
3467 cp_parser_parse_tentatively (parser);
3468 id = cp_parser_id_expression (parser,
3469 /*template_keyword_p=*/false,
3470 /*check_dependency_p=*/true,
3471 /*template_p=*/NULL,
3472 /*declarator_p=*/false,
3473 /*optional_p=*/false);
3474 /* If the next token is a (, this is a function with no explicit return
3475 type, i.e. constructor, destructor or conversion op. */
3476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3477 || TREE_CODE (id) == TYPE_DECL)
3479 cp_parser_abort_tentative_parse (parser);
3480 return false;
3482 if (!cp_parser_parse_definitely (parser))
3483 return false;
3485 /* Emit a diagnostic for the invalid type. */
3486 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3487 out:
3488 /* If we aren't in the middle of a declarator (i.e. in a
3489 parameter-declaration-clause), skip to the end of the declaration;
3490 there's no point in trying to process it. */
3491 if (!parser->in_declarator_p)
3492 cp_parser_skip_to_end_of_block_or_statement (parser);
3493 return true;
3496 /* Consume tokens up to, and including, the next non-nested closing `)'.
3497 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3498 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3499 found an unnested token of that type. */
3501 static int
3502 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3503 bool recovering,
3504 cpp_ttype or_ttype,
3505 bool consume_paren)
3507 unsigned paren_depth = 0;
3508 unsigned brace_depth = 0;
3509 unsigned square_depth = 0;
3510 unsigned condop_depth = 0;
3512 if (recovering && or_ttype == CPP_EOF
3513 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3514 return 0;
3516 while (true)
3518 cp_token * token = cp_lexer_peek_token (parser->lexer);
3520 /* Have we found what we're looking for before the closing paren? */
3521 if (token->type == or_ttype && or_ttype != CPP_EOF
3522 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3523 return -1;
3525 switch (token->type)
3527 case CPP_EOF:
3528 case CPP_PRAGMA_EOL:
3529 /* If we've run out of tokens, then there is no closing `)'. */
3530 return 0;
3532 /* This is good for lambda expression capture-lists. */
3533 case CPP_OPEN_SQUARE:
3534 ++square_depth;
3535 break;
3536 case CPP_CLOSE_SQUARE:
3537 if (!square_depth--)
3538 return 0;
3539 break;
3541 case CPP_SEMICOLON:
3542 /* This matches the processing in skip_to_end_of_statement. */
3543 if (!brace_depth)
3544 return 0;
3545 break;
3547 case CPP_OPEN_BRACE:
3548 ++brace_depth;
3549 break;
3550 case CPP_CLOSE_BRACE:
3551 if (!brace_depth--)
3552 return 0;
3553 break;
3555 case CPP_OPEN_PAREN:
3556 if (!brace_depth)
3557 ++paren_depth;
3558 break;
3560 case CPP_CLOSE_PAREN:
3561 if (!brace_depth && !paren_depth--)
3563 if (consume_paren)
3564 cp_lexer_consume_token (parser->lexer);
3565 return 1;
3567 break;
3569 case CPP_QUERY:
3570 if (!brace_depth && !paren_depth && !square_depth)
3571 ++condop_depth;
3572 break;
3574 case CPP_COLON:
3575 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3576 condop_depth--;
3577 break;
3579 default:
3580 break;
3583 /* Consume the token. */
3584 cp_lexer_consume_token (parser->lexer);
3588 /* Consume tokens up to, and including, the next non-nested closing `)'.
3589 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3590 are doing error recovery. Returns -1 if OR_COMMA is true and we
3591 found an unnested token of that type. */
3593 static int
3594 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3595 bool recovering,
3596 bool or_comma,
3597 bool consume_paren)
3599 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3600 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3601 ttype, consume_paren);
3604 /* Consume tokens until we reach the end of the current statement.
3605 Normally, that will be just before consuming a `;'. However, if a
3606 non-nested `}' comes first, then we stop before consuming that. */
3608 static void
3609 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3611 unsigned nesting_depth = 0;
3613 /* Unwind generic function template scope if necessary. */
3614 if (parser->fully_implicit_function_template_p)
3615 abort_fully_implicit_template (parser);
3617 while (true)
3619 cp_token *token = cp_lexer_peek_token (parser->lexer);
3621 switch (token->type)
3623 case CPP_EOF:
3624 case CPP_PRAGMA_EOL:
3625 /* If we've run out of tokens, stop. */
3626 return;
3628 case CPP_SEMICOLON:
3629 /* If the next token is a `;', we have reached the end of the
3630 statement. */
3631 if (!nesting_depth)
3632 return;
3633 break;
3635 case CPP_CLOSE_BRACE:
3636 /* If this is a non-nested '}', stop before consuming it.
3637 That way, when confronted with something like:
3639 { 3 + }
3641 we stop before consuming the closing '}', even though we
3642 have not yet reached a `;'. */
3643 if (nesting_depth == 0)
3644 return;
3646 /* If it is the closing '}' for a block that we have
3647 scanned, stop -- but only after consuming the token.
3648 That way given:
3650 void f g () { ... }
3651 typedef int I;
3653 we will stop after the body of the erroneously declared
3654 function, but before consuming the following `typedef'
3655 declaration. */
3656 if (--nesting_depth == 0)
3658 cp_lexer_consume_token (parser->lexer);
3659 return;
3661 break;
3663 case CPP_OPEN_BRACE:
3664 ++nesting_depth;
3665 break;
3667 default:
3668 break;
3671 /* Consume the token. */
3672 cp_lexer_consume_token (parser->lexer);
3676 /* This function is called at the end of a statement or declaration.
3677 If the next token is a semicolon, it is consumed; otherwise, error
3678 recovery is attempted. */
3680 static void
3681 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3683 /* Look for the trailing `;'. */
3684 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3686 /* If there is additional (erroneous) input, skip to the end of
3687 the statement. */
3688 cp_parser_skip_to_end_of_statement (parser);
3689 /* If the next token is now a `;', consume it. */
3690 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3691 cp_lexer_consume_token (parser->lexer);
3695 /* Skip tokens until we have consumed an entire block, or until we
3696 have consumed a non-nested `;'. */
3698 static void
3699 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3701 int nesting_depth = 0;
3703 /* Unwind generic function template scope if necessary. */
3704 if (parser->fully_implicit_function_template_p)
3705 abort_fully_implicit_template (parser);
3707 while (nesting_depth >= 0)
3709 cp_token *token = cp_lexer_peek_token (parser->lexer);
3711 switch (token->type)
3713 case CPP_EOF:
3714 case CPP_PRAGMA_EOL:
3715 /* If we've run out of tokens, stop. */
3716 return;
3718 case CPP_SEMICOLON:
3719 /* Stop if this is an unnested ';'. */
3720 if (!nesting_depth)
3721 nesting_depth = -1;
3722 break;
3724 case CPP_CLOSE_BRACE:
3725 /* Stop if this is an unnested '}', or closes the outermost
3726 nesting level. */
3727 nesting_depth--;
3728 if (nesting_depth < 0)
3729 return;
3730 if (!nesting_depth)
3731 nesting_depth = -1;
3732 break;
3734 case CPP_OPEN_BRACE:
3735 /* Nest. */
3736 nesting_depth++;
3737 break;
3739 default:
3740 break;
3743 /* Consume the token. */
3744 cp_lexer_consume_token (parser->lexer);
3748 /* Skip tokens until a non-nested closing curly brace is the next
3749 token, or there are no more tokens. Return true in the first case,
3750 false otherwise. */
3752 static bool
3753 cp_parser_skip_to_closing_brace (cp_parser *parser)
3755 unsigned nesting_depth = 0;
3757 while (true)
3759 cp_token *token = cp_lexer_peek_token (parser->lexer);
3761 switch (token->type)
3763 case CPP_EOF:
3764 case CPP_PRAGMA_EOL:
3765 /* If we've run out of tokens, stop. */
3766 return false;
3768 case CPP_CLOSE_BRACE:
3769 /* If the next token is a non-nested `}', then we have reached
3770 the end of the current block. */
3771 if (nesting_depth-- == 0)
3772 return true;
3773 break;
3775 case CPP_OPEN_BRACE:
3776 /* If it the next token is a `{', then we are entering a new
3777 block. Consume the entire block. */
3778 ++nesting_depth;
3779 break;
3781 default:
3782 break;
3785 /* Consume the token. */
3786 cp_lexer_consume_token (parser->lexer);
3790 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3791 parameter is the PRAGMA token, allowing us to purge the entire pragma
3792 sequence. */
3794 static void
3795 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3797 cp_token *token;
3799 parser->lexer->in_pragma = false;
3802 token = cp_lexer_consume_token (parser->lexer);
3803 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3805 /* Ensure that the pragma is not parsed again. */
3806 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3809 /* Require pragma end of line, resyncing with it as necessary. The
3810 arguments are as for cp_parser_skip_to_pragma_eol. */
3812 static void
3813 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3815 parser->lexer->in_pragma = false;
3816 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3817 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3820 /* This is a simple wrapper around make_typename_type. When the id is
3821 an unresolved identifier node, we can provide a superior diagnostic
3822 using cp_parser_diagnose_invalid_type_name. */
3824 static tree
3825 cp_parser_make_typename_type (cp_parser *parser, tree id,
3826 location_t id_location)
3828 tree result;
3829 if (identifier_p (id))
3831 result = make_typename_type (parser->scope, id, typename_type,
3832 /*complain=*/tf_none);
3833 if (result == error_mark_node)
3834 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3835 return result;
3837 return make_typename_type (parser->scope, id, typename_type, tf_error);
3840 /* This is a wrapper around the
3841 make_{pointer,ptrmem,reference}_declarator functions that decides
3842 which one to call based on the CODE and CLASS_TYPE arguments. The
3843 CODE argument should be one of the values returned by
3844 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3845 appertain to the pointer or reference. */
3847 static cp_declarator *
3848 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3849 cp_cv_quals cv_qualifiers,
3850 cp_declarator *target,
3851 tree attributes)
3853 if (code == ERROR_MARK || target == cp_error_declarator)
3854 return cp_error_declarator;
3856 if (code == INDIRECT_REF)
3857 if (class_type == NULL_TREE)
3858 return make_pointer_declarator (cv_qualifiers, target, attributes);
3859 else
3860 return make_ptrmem_declarator (cv_qualifiers, class_type,
3861 target, attributes);
3862 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3863 return make_reference_declarator (cv_qualifiers, target,
3864 false, attributes);
3865 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3866 return make_reference_declarator (cv_qualifiers, target,
3867 true, attributes);
3868 gcc_unreachable ();
3871 /* Create a new C++ parser. */
3873 static cp_parser *
3874 cp_parser_new (void)
3876 cp_parser *parser;
3877 cp_lexer *lexer;
3878 unsigned i;
3880 /* cp_lexer_new_main is called before doing GC allocation because
3881 cp_lexer_new_main might load a PCH file. */
3882 lexer = cp_lexer_new_main ();
3884 /* Initialize the binops_by_token so that we can get the tree
3885 directly from the token. */
3886 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3887 binops_by_token[binops[i].token_type] = binops[i];
3889 parser = ggc_cleared_alloc<cp_parser> ();
3890 parser->lexer = lexer;
3891 parser->context = cp_parser_context_new (NULL);
3893 /* For now, we always accept GNU extensions. */
3894 parser->allow_gnu_extensions_p = 1;
3896 /* The `>' token is a greater-than operator, not the end of a
3897 template-id. */
3898 parser->greater_than_is_operator_p = true;
3900 parser->default_arg_ok_p = true;
3902 /* We are not parsing a constant-expression. */
3903 parser->integral_constant_expression_p = false;
3904 parser->allow_non_integral_constant_expression_p = false;
3905 parser->non_integral_constant_expression_p = false;
3907 /* Local variable names are not forbidden. */
3908 parser->local_variables_forbidden_p = false;
3910 /* We are not processing an `extern "C"' declaration. */
3911 parser->in_unbraced_linkage_specification_p = false;
3913 /* We are not processing a declarator. */
3914 parser->in_declarator_p = false;
3916 /* We are not processing a template-argument-list. */
3917 parser->in_template_argument_list_p = false;
3919 /* We are not in an iteration statement. */
3920 parser->in_statement = 0;
3922 /* We are not in a switch statement. */
3923 parser->in_switch_statement_p = false;
3925 /* We are not parsing a type-id inside an expression. */
3926 parser->in_type_id_in_expr_p = false;
3928 /* Declarations aren't implicitly extern "C". */
3929 parser->implicit_extern_c = false;
3931 /* String literals should be translated to the execution character set. */
3932 parser->translate_strings_p = true;
3934 /* We are not parsing a function body. */
3935 parser->in_function_body = false;
3937 /* We can correct until told otherwise. */
3938 parser->colon_corrects_to_scope_p = true;
3940 /* The unparsed function queue is empty. */
3941 push_unparsed_function_queues (parser);
3943 /* There are no classes being defined. */
3944 parser->num_classes_being_defined = 0;
3946 /* No template parameters apply. */
3947 parser->num_template_parameter_lists = 0;
3949 /* Special parsing data structures. */
3950 parser->omp_declare_simd = NULL;
3951 parser->oacc_routine = NULL;
3953 /* Not declaring an implicit function template. */
3954 parser->auto_is_implicit_function_template_parm_p = false;
3955 parser->fully_implicit_function_template_p = false;
3956 parser->implicit_template_parms = 0;
3957 parser->implicit_template_scope = 0;
3959 /* Allow constrained-type-specifiers. */
3960 parser->prevent_constrained_type_specifiers = 0;
3962 /* We haven't yet seen an 'extern "C"'. */
3963 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3965 return parser;
3968 /* Create a cp_lexer structure which will emit the tokens in CACHE
3969 and push it onto the parser's lexer stack. This is used for delayed
3970 parsing of in-class method bodies and default arguments, and should
3971 not be confused with tentative parsing. */
3972 static void
3973 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3975 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3976 lexer->next = parser->lexer;
3977 parser->lexer = lexer;
3979 /* Move the current source position to that of the first token in the
3980 new lexer. */
3981 cp_lexer_set_source_position_from_token (lexer->next_token);
3984 /* Pop the top lexer off the parser stack. This is never used for the
3985 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3986 static void
3987 cp_parser_pop_lexer (cp_parser *parser)
3989 cp_lexer *lexer = parser->lexer;
3990 parser->lexer = lexer->next;
3991 cp_lexer_destroy (lexer);
3993 /* Put the current source position back where it was before this
3994 lexer was pushed. */
3995 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3998 /* Lexical conventions [gram.lex] */
4000 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4001 identifier. */
4003 static cp_expr
4004 cp_parser_identifier (cp_parser* parser)
4006 cp_token *token;
4008 /* Look for the identifier. */
4009 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4010 /* Return the value. */
4011 if (token)
4012 return cp_expr (token->u.value, token->location);
4013 else
4014 return error_mark_node;
4017 /* Parse a sequence of adjacent string constants. Returns a
4018 TREE_STRING representing the combined, nul-terminated string
4019 constant. If TRANSLATE is true, translate the string to the
4020 execution character set. If WIDE_OK is true, a wide string is
4021 invalid here.
4023 C++98 [lex.string] says that if a narrow string literal token is
4024 adjacent to a wide string literal token, the behavior is undefined.
4025 However, C99 6.4.5p4 says that this results in a wide string literal.
4026 We follow C99 here, for consistency with the C front end.
4028 This code is largely lifted from lex_string() in c-lex.c.
4030 FUTURE: ObjC++ will need to handle @-strings here. */
4031 static cp_expr
4032 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4033 bool lookup_udlit = true)
4035 tree value;
4036 size_t count;
4037 struct obstack str_ob;
4038 cpp_string str, istr, *strs;
4039 cp_token *tok;
4040 enum cpp_ttype type, curr_type;
4041 int have_suffix_p = 0;
4042 tree string_tree;
4043 tree suffix_id = NULL_TREE;
4044 bool curr_tok_is_userdef_p = false;
4046 tok = cp_lexer_peek_token (parser->lexer);
4047 if (!cp_parser_is_string_literal (tok))
4049 cp_parser_error (parser, "expected string-literal");
4050 return error_mark_node;
4053 location_t loc = tok->location;
4055 if (cpp_userdef_string_p (tok->type))
4057 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4058 curr_type = cpp_userdef_string_remove_type (tok->type);
4059 curr_tok_is_userdef_p = true;
4061 else
4063 string_tree = tok->u.value;
4064 curr_type = tok->type;
4066 type = curr_type;
4068 /* Try to avoid the overhead of creating and destroying an obstack
4069 for the common case of just one string. */
4070 if (!cp_parser_is_string_literal
4071 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4073 cp_lexer_consume_token (parser->lexer);
4075 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4076 str.len = TREE_STRING_LENGTH (string_tree);
4077 count = 1;
4079 if (curr_tok_is_userdef_p)
4081 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4082 have_suffix_p = 1;
4083 curr_type = cpp_userdef_string_remove_type (tok->type);
4085 else
4086 curr_type = tok->type;
4088 strs = &str;
4090 else
4092 location_t last_tok_loc = tok->location;
4093 gcc_obstack_init (&str_ob);
4094 count = 0;
4098 cp_lexer_consume_token (parser->lexer);
4099 count++;
4100 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4101 str.len = TREE_STRING_LENGTH (string_tree);
4103 if (curr_tok_is_userdef_p)
4105 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4106 if (have_suffix_p == 0)
4108 suffix_id = curr_suffix_id;
4109 have_suffix_p = 1;
4111 else if (have_suffix_p == 1
4112 && curr_suffix_id != suffix_id)
4114 error ("inconsistent user-defined literal suffixes"
4115 " %qD and %qD in string literal",
4116 suffix_id, curr_suffix_id);
4117 have_suffix_p = -1;
4119 curr_type = cpp_userdef_string_remove_type (tok->type);
4121 else
4122 curr_type = tok->type;
4124 if (type != curr_type)
4126 if (type == CPP_STRING)
4127 type = curr_type;
4128 else if (curr_type != CPP_STRING)
4130 rich_location rich_loc (line_table, tok->location);
4131 rich_loc.add_range (last_tok_loc, false);
4132 error_at (&rich_loc,
4133 "unsupported non-standard concatenation "
4134 "of string literals");
4138 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4140 last_tok_loc = tok->location;
4142 tok = cp_lexer_peek_token (parser->lexer);
4143 if (cpp_userdef_string_p (tok->type))
4145 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4146 curr_type = cpp_userdef_string_remove_type (tok->type);
4147 curr_tok_is_userdef_p = true;
4149 else
4151 string_tree = tok->u.value;
4152 curr_type = tok->type;
4153 curr_tok_is_userdef_p = false;
4156 while (cp_parser_is_string_literal (tok));
4158 /* A string literal built by concatenation has its caret=start at
4159 the start of the initial string, and its finish at the finish of
4160 the final string literal. */
4161 loc = make_location (loc, loc, get_finish (last_tok_loc));
4163 strs = (cpp_string *) obstack_finish (&str_ob);
4166 if (type != CPP_STRING && !wide_ok)
4168 cp_parser_error (parser, "a wide string is invalid in this context");
4169 type = CPP_STRING;
4172 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4173 (parse_in, strs, count, &istr, type))
4175 value = build_string (istr.len, (const char *)istr.text);
4176 free (CONST_CAST (unsigned char *, istr.text));
4178 switch (type)
4180 default:
4181 case CPP_STRING:
4182 case CPP_UTF8STRING:
4183 TREE_TYPE (value) = char_array_type_node;
4184 break;
4185 case CPP_STRING16:
4186 TREE_TYPE (value) = char16_array_type_node;
4187 break;
4188 case CPP_STRING32:
4189 TREE_TYPE (value) = char32_array_type_node;
4190 break;
4191 case CPP_WSTRING:
4192 TREE_TYPE (value) = wchar_array_type_node;
4193 break;
4196 value = fix_string_type (value);
4198 if (have_suffix_p)
4200 tree literal = build_userdef_literal (suffix_id, value,
4201 OT_NONE, NULL_TREE);
4202 if (lookup_udlit)
4203 value = cp_parser_userdef_string_literal (literal);
4204 else
4205 value = literal;
4208 else
4209 /* cpp_interpret_string has issued an error. */
4210 value = error_mark_node;
4212 if (count > 1)
4213 obstack_free (&str_ob, 0);
4215 return cp_expr (value, loc);
4218 /* Look up a literal operator with the name and the exact arguments. */
4220 static tree
4221 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4223 tree decl;
4224 decl = lookup_name (name);
4225 if (!decl || !is_overloaded_fn (decl))
4226 return error_mark_node;
4228 for (lkp_iterator iter (decl); iter; ++iter)
4230 unsigned int ix;
4231 bool found = true;
4232 tree fn = *iter;
4233 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4234 if (parmtypes != NULL_TREE)
4236 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4237 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4239 tree tparm = TREE_VALUE (parmtypes);
4240 tree targ = TREE_TYPE ((*args)[ix]);
4241 bool ptr = TYPE_PTR_P (tparm);
4242 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4243 if ((ptr || arr || !same_type_p (tparm, targ))
4244 && (!ptr || !arr
4245 || !same_type_p (TREE_TYPE (tparm),
4246 TREE_TYPE (targ))))
4247 found = false;
4249 if (found
4250 && ix == vec_safe_length (args)
4251 /* May be this should be sufficient_parms_p instead,
4252 depending on how exactly should user-defined literals
4253 work in presence of default arguments on the literal
4254 operator parameters. */
4255 && parmtypes == void_list_node)
4256 return decl;
4260 return error_mark_node;
4263 /* Parse a user-defined char constant. Returns a call to a user-defined
4264 literal operator taking the character as an argument. */
4266 static cp_expr
4267 cp_parser_userdef_char_literal (cp_parser *parser)
4269 cp_token *token = cp_lexer_consume_token (parser->lexer);
4270 tree literal = token->u.value;
4271 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4272 tree value = USERDEF_LITERAL_VALUE (literal);
4273 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4274 tree decl, result;
4276 /* Build up a call to the user-defined operator */
4277 /* Lookup the name we got back from the id-expression. */
4278 vec<tree, va_gc> *args = make_tree_vector ();
4279 vec_safe_push (args, value);
4280 decl = lookup_literal_operator (name, args);
4281 if (!decl || decl == error_mark_node)
4283 error ("unable to find character literal operator %qD with %qT argument",
4284 name, TREE_TYPE (value));
4285 release_tree_vector (args);
4286 return error_mark_node;
4288 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4289 release_tree_vector (args);
4290 return result;
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4296 static tree
4297 make_char_string_pack (tree value)
4299 tree charvec;
4300 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4301 const char *str = TREE_STRING_POINTER (value);
4302 int i, len = TREE_STRING_LENGTH (value) - 1;
4303 tree argvec = make_tree_vec (1);
4305 /* Fill in CHARVEC with all of the parameters. */
4306 charvec = make_tree_vec (len);
4307 for (i = 0; i < len; ++i)
4309 unsigned char s[3] = { '\'', str[i], '\'' };
4310 cpp_string in = { 3, s };
4311 cpp_string out = { 0, 0 };
4312 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4313 return NULL_TREE;
4314 gcc_assert (out.len == 2);
4315 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4316 out.text[0]);
4319 /* Build the argument packs. */
4320 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4322 TREE_VEC_ELT (argvec, 0) = argpack;
4324 return argvec;
4327 /* A subroutine of cp_parser_userdef_numeric_literal to
4328 create a char... template parameter pack from a string node. */
4330 static tree
4331 make_string_pack (tree value)
4333 tree charvec;
4334 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4335 const unsigned char *str
4336 = (const unsigned char *) TREE_STRING_POINTER (value);
4337 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4338 int len = TREE_STRING_LENGTH (value) / sz - 1;
4339 tree argvec = make_tree_vec (2);
4341 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4342 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4344 /* First template parm is character type. */
4345 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4347 /* Fill in CHARVEC with all of the parameters. */
4348 charvec = make_tree_vec (len);
4349 for (int i = 0; i < len; ++i)
4350 TREE_VEC_ELT (charvec, i)
4351 = double_int_to_tree (str_char_type_node,
4352 double_int::from_buffer (str + i * sz, sz));
4354 /* Build the argument packs. */
4355 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4357 TREE_VEC_ELT (argvec, 1) = argpack;
4359 return argvec;
4362 /* Parse a user-defined numeric constant. returns a call to a user-defined
4363 literal operator. */
4365 static cp_expr
4366 cp_parser_userdef_numeric_literal (cp_parser *parser)
4368 cp_token *token = cp_lexer_consume_token (parser->lexer);
4369 tree literal = token->u.value;
4370 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4371 tree value = USERDEF_LITERAL_VALUE (literal);
4372 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4373 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4374 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4375 tree decl, result;
4376 vec<tree, va_gc> *args;
4378 /* Look for a literal operator taking the exact type of numeric argument
4379 as the literal value. */
4380 args = make_tree_vector ();
4381 vec_safe_push (args, value);
4382 decl = lookup_literal_operator (name, args);
4383 if (decl && decl != error_mark_node)
4385 result = finish_call_expr (decl, &args, false, true,
4386 tf_warning_or_error);
4388 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4390 warning_at (token->location, OPT_Woverflow,
4391 "integer literal exceeds range of %qT type",
4392 long_long_unsigned_type_node);
4394 else
4396 if (overflow > 0)
4397 warning_at (token->location, OPT_Woverflow,
4398 "floating literal exceeds range of %qT type",
4399 long_double_type_node);
4400 else if (overflow < 0)
4401 warning_at (token->location, OPT_Woverflow,
4402 "floating literal truncated to zero");
4405 release_tree_vector (args);
4406 return result;
4408 release_tree_vector (args);
4410 /* If the numeric argument didn't work, look for a raw literal
4411 operator taking a const char* argument consisting of the number
4412 in string format. */
4413 args = make_tree_vector ();
4414 vec_safe_push (args, num_string);
4415 decl = lookup_literal_operator (name, args);
4416 if (decl && decl != error_mark_node)
4418 result = finish_call_expr (decl, &args, false, true,
4419 tf_warning_or_error);
4420 release_tree_vector (args);
4421 return result;
4423 release_tree_vector (args);
4425 /* If the raw literal didn't work, look for a non-type template
4426 function with parameter pack char.... Call the function with
4427 template parameter characters representing the number. */
4428 args = make_tree_vector ();
4429 decl = lookup_literal_operator (name, args);
4430 if (decl && decl != error_mark_node)
4432 tree tmpl_args = make_char_string_pack (num_string);
4433 if (tmpl_args == NULL_TREE)
4435 error ("failed to translate literal to execution character set %qT",
4436 num_string);
4437 return error_mark_node;
4439 decl = lookup_template_function (decl, tmpl_args);
4440 result = finish_call_expr (decl, &args, false, true,
4441 tf_warning_or_error);
4442 release_tree_vector (args);
4443 return result;
4446 release_tree_vector (args);
4448 /* In C++14 the standard library defines complex number suffixes that
4449 conflict with GNU extensions. Prefer them if <complex> is #included. */
4450 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4451 bool i14 = (cxx_dialect > cxx11
4452 && (id_equal (suffix_id, "i")
4453 || id_equal (suffix_id, "if")
4454 || id_equal (suffix_id, "il")));
4455 diagnostic_t kind = DK_ERROR;
4456 int opt = 0;
4458 if (i14 && ext)
4460 tree cxlit = lookup_qualified_name (std_node,
4461 get_identifier ("complex_literals"),
4462 0, false, false);
4463 if (cxlit == error_mark_node)
4465 /* No <complex>, so pedwarn and use GNU semantics. */
4466 kind = DK_PEDWARN;
4467 opt = OPT_Wpedantic;
4471 bool complained
4472 = emit_diagnostic (kind, input_location, opt,
4473 "unable to find numeric literal operator %qD", name);
4475 if (!complained)
4476 /* Don't inform either. */;
4477 else if (i14)
4479 inform (token->location, "add %<using namespace std::complex_literals%> "
4480 "(from <complex>) to enable the C++14 user-defined literal "
4481 "suffixes");
4482 if (ext)
4483 inform (token->location, "or use %<j%> instead of %<i%> for the "
4484 "GNU built-in suffix");
4486 else if (!ext)
4487 inform (token->location, "use -fext-numeric-literals "
4488 "to enable more built-in suffixes");
4490 if (kind == DK_ERROR)
4491 value = error_mark_node;
4492 else
4494 /* Use the built-in semantics. */
4495 tree type;
4496 if (id_equal (suffix_id, "i"))
4498 if (TREE_CODE (value) == INTEGER_CST)
4499 type = integer_type_node;
4500 else
4501 type = double_type_node;
4503 else if (id_equal (suffix_id, "if"))
4504 type = float_type_node;
4505 else /* if (id_equal (suffix_id, "il")) */
4506 type = long_double_type_node;
4508 value = build_complex (build_complex_type (type),
4509 fold_convert (type, integer_zero_node),
4510 fold_convert (type, value));
4513 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4514 /* Avoid repeated diagnostics. */
4515 token->u.value = value;
4516 return value;
4519 /* Parse a user-defined string constant. Returns a call to a user-defined
4520 literal operator taking a character pointer and the length of the string
4521 as arguments. */
4523 static tree
4524 cp_parser_userdef_string_literal (tree literal)
4526 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4527 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4528 tree value = USERDEF_LITERAL_VALUE (literal);
4529 int len = TREE_STRING_LENGTH (value)
4530 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4531 tree decl, result;
4532 vec<tree, va_gc> *args;
4534 /* Build up a call to the user-defined operator. */
4535 /* Lookup the name we got back from the id-expression. */
4536 args = make_tree_vector ();
4537 vec_safe_push (args, value);
4538 vec_safe_push (args, build_int_cst (size_type_node, len));
4539 decl = lookup_literal_operator (name, args);
4541 if (decl && decl != error_mark_node)
4543 result = finish_call_expr (decl, &args, false, true,
4544 tf_warning_or_error);
4545 release_tree_vector (args);
4546 return result;
4548 release_tree_vector (args);
4550 /* Look for a template function with typename parameter CharT
4551 and parameter pack CharT... Call the function with
4552 template parameter characters representing the string. */
4553 args = make_tree_vector ();
4554 decl = lookup_literal_operator (name, args);
4555 if (decl && decl != error_mark_node)
4557 tree tmpl_args = make_string_pack (value);
4558 decl = lookup_template_function (decl, tmpl_args);
4559 result = finish_call_expr (decl, &args, false, true,
4560 tf_warning_or_error);
4561 release_tree_vector (args);
4562 return result;
4564 release_tree_vector (args);
4566 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4567 name, TREE_TYPE (value), size_type_node);
4568 return error_mark_node;
4572 /* Basic concepts [gram.basic] */
4574 /* Parse a translation-unit.
4576 translation-unit:
4577 declaration-seq [opt]
4579 Returns TRUE if all went well. */
4581 static bool
4582 cp_parser_translation_unit (cp_parser* parser)
4584 /* The address of the first non-permanent object on the declarator
4585 obstack. */
4586 static void *declarator_obstack_base;
4588 bool success;
4590 /* Create the declarator obstack, if necessary. */
4591 if (!cp_error_declarator)
4593 gcc_obstack_init (&declarator_obstack);
4594 /* Create the error declarator. */
4595 cp_error_declarator = make_declarator (cdk_error);
4596 /* Create the empty parameter list. */
4597 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4598 UNKNOWN_LOCATION);
4599 /* Remember where the base of the declarator obstack lies. */
4600 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4603 cp_parser_declaration_seq_opt (parser);
4605 /* If there are no tokens left then all went well. */
4606 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4608 /* Get rid of the token array; we don't need it any more. */
4609 cp_lexer_destroy (parser->lexer);
4610 parser->lexer = NULL;
4612 /* This file might have been a context that's implicitly extern
4613 "C". If so, pop the lang context. (Only relevant for PCH.) */
4614 if (parser->implicit_extern_c)
4616 pop_lang_context ();
4617 parser->implicit_extern_c = false;
4620 /* Finish up. */
4621 finish_translation_unit ();
4623 success = true;
4625 else
4627 cp_parser_error (parser, "expected declaration");
4628 success = false;
4631 /* Make sure the declarator obstack was fully cleaned up. */
4632 gcc_assert (obstack_next_free (&declarator_obstack)
4633 == declarator_obstack_base);
4635 /* All went well. */
4636 return success;
4639 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4640 decltype context. */
4642 static inline tsubst_flags_t
4643 complain_flags (bool decltype_p)
4645 tsubst_flags_t complain = tf_warning_or_error;
4646 if (decltype_p)
4647 complain |= tf_decltype;
4648 return complain;
4651 /* We're about to parse a collection of statements. If we're currently
4652 parsing tentatively, set up a firewall so that any nested
4653 cp_parser_commit_to_tentative_parse won't affect the current context. */
4655 static cp_token_position
4656 cp_parser_start_tentative_firewall (cp_parser *parser)
4658 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4659 return 0;
4661 cp_parser_parse_tentatively (parser);
4662 cp_parser_commit_to_topmost_tentative_parse (parser);
4663 return cp_lexer_token_position (parser->lexer, false);
4666 /* We've finished parsing the collection of statements. Wrap up the
4667 firewall and replace the relevant tokens with the parsed form. */
4669 static void
4670 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4671 tree expr)
4673 if (!start)
4674 return;
4676 /* Finish the firewall level. */
4677 cp_parser_parse_definitely (parser);
4678 /* And remember the result of the parse for when we try again. */
4679 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4680 token->type = CPP_PREPARSED_EXPR;
4681 token->u.value = expr;
4682 token->keyword = RID_MAX;
4683 cp_lexer_purge_tokens_after (parser->lexer, start);
4686 /* Like the above functions, but let the user modify the tokens. Used by
4687 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4688 later parses, so it makes sense to localize the effects of
4689 cp_parser_commit_to_tentative_parse. */
4691 struct tentative_firewall
4693 cp_parser *parser;
4694 bool set;
4696 tentative_firewall (cp_parser *p): parser(p)
4698 /* If we're currently parsing tentatively, start a committed level as a
4699 firewall and then an inner tentative parse. */
4700 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4702 cp_parser_parse_tentatively (parser);
4703 cp_parser_commit_to_topmost_tentative_parse (parser);
4704 cp_parser_parse_tentatively (parser);
4708 ~tentative_firewall()
4710 if (set)
4712 /* Finish the inner tentative parse and the firewall, propagating any
4713 uncommitted error state to the outer tentative parse. */
4714 bool err = cp_parser_error_occurred (parser);
4715 cp_parser_parse_definitely (parser);
4716 cp_parser_parse_definitely (parser);
4717 if (err)
4718 cp_parser_simulate_error (parser);
4723 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4724 This class is for tracking such a matching pair of symbols.
4725 In particular, it tracks the location of the first token,
4726 so that if the second token is missing, we can highlight the
4727 location of the first token when notifying the user about the
4728 problem. */
4730 template <typename traits_t>
4731 class token_pair
4733 public:
4734 /* token_pair's ctor. */
4735 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4737 /* If the next token is the opening symbol for this pair, consume it and
4738 return true.
4739 Otherwise, issue an error and return false.
4740 In either case, record the location of the opening token. */
4742 bool require_open (cp_parser *parser)
4744 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4745 return cp_parser_require (parser, traits_t::open_token_type,
4746 traits_t::required_token_open);
4749 /* Consume the next token from PARSER, recording its location as
4750 that of the opening token within the pair. */
4752 cp_token * consume_open (cp_parser *parser)
4754 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4755 gcc_assert (tok->type == traits_t::open_token_type);
4756 m_open_loc = tok->location;
4757 return tok;
4760 /* If the next token is the closing symbol for this pair, consume it
4761 and return it.
4762 Otherwise, issue an error, highlighting the location of the
4763 corresponding opening token, and return NULL. */
4765 cp_token *require_close (cp_parser *parser) const
4767 return cp_parser_require (parser, traits_t::close_token_type,
4768 traits_t::required_token_close,
4769 m_open_loc);
4772 private:
4773 location_t m_open_loc;
4776 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4778 struct matching_paren_traits
4780 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4781 static const enum required_token required_token_open = RT_OPEN_PAREN;
4782 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4783 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4786 /* "matching_parens" is a token_pair<T> class for tracking matching
4787 pairs of parentheses. */
4789 typedef token_pair<matching_paren_traits> matching_parens;
4791 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4793 struct matching_brace_traits
4795 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4796 static const enum required_token required_token_open = RT_OPEN_BRACE;
4797 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4798 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4801 /* "matching_braces" is a token_pair<T> class for tracking matching
4802 pairs of braces. */
4804 typedef token_pair<matching_brace_traits> matching_braces;
4807 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4808 enclosing parentheses. */
4810 static cp_expr
4811 cp_parser_statement_expr (cp_parser *parser)
4813 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4815 /* Consume the '('. */
4816 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4817 matching_parens parens;
4818 parens.consume_open (parser);
4819 /* Start the statement-expression. */
4820 tree expr = begin_stmt_expr ();
4821 /* Parse the compound-statement. */
4822 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4823 /* Finish up. */
4824 expr = finish_stmt_expr (expr, false);
4825 /* Consume the ')'. */
4826 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4827 if (!parens.require_close (parser))
4828 cp_parser_skip_to_end_of_statement (parser);
4830 cp_parser_end_tentative_firewall (parser, start, expr);
4831 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4832 return cp_expr (expr, combined_loc);
4835 /* Expressions [gram.expr] */
4837 /* Parse a fold-operator.
4839 fold-operator:
4840 - * / % ^ & | = < > << >>
4841 = -= *= /= %= ^= &= |= <<= >>=
4842 == != <= >= && || , .* ->*
4844 This returns the tree code corresponding to the matched operator
4845 as an int. When the current token matches a compound assignment
4846 opertor, the resulting tree code is the negative value of the
4847 non-assignment operator. */
4849 static int
4850 cp_parser_fold_operator (cp_token *token)
4852 switch (token->type)
4854 case CPP_PLUS: return PLUS_EXPR;
4855 case CPP_MINUS: return MINUS_EXPR;
4856 case CPP_MULT: return MULT_EXPR;
4857 case CPP_DIV: return TRUNC_DIV_EXPR;
4858 case CPP_MOD: return TRUNC_MOD_EXPR;
4859 case CPP_XOR: return BIT_XOR_EXPR;
4860 case CPP_AND: return BIT_AND_EXPR;
4861 case CPP_OR: return BIT_IOR_EXPR;
4862 case CPP_LSHIFT: return LSHIFT_EXPR;
4863 case CPP_RSHIFT: return RSHIFT_EXPR;
4865 case CPP_EQ: return -NOP_EXPR;
4866 case CPP_PLUS_EQ: return -PLUS_EXPR;
4867 case CPP_MINUS_EQ: return -MINUS_EXPR;
4868 case CPP_MULT_EQ: return -MULT_EXPR;
4869 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4870 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4871 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4872 case CPP_AND_EQ: return -BIT_AND_EXPR;
4873 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4874 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4875 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4877 case CPP_EQ_EQ: return EQ_EXPR;
4878 case CPP_NOT_EQ: return NE_EXPR;
4879 case CPP_LESS: return LT_EXPR;
4880 case CPP_GREATER: return GT_EXPR;
4881 case CPP_LESS_EQ: return LE_EXPR;
4882 case CPP_GREATER_EQ: return GE_EXPR;
4884 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4885 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4887 case CPP_COMMA: return COMPOUND_EXPR;
4889 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4890 case CPP_DEREF_STAR: return MEMBER_REF;
4892 default: return ERROR_MARK;
4896 /* Returns true if CODE indicates a binary expression, which is not allowed in
4897 the LHS of a fold-expression. More codes will need to be added to use this
4898 function in other contexts. */
4900 static bool
4901 is_binary_op (tree_code code)
4903 switch (code)
4905 case PLUS_EXPR:
4906 case POINTER_PLUS_EXPR:
4907 case MINUS_EXPR:
4908 case MULT_EXPR:
4909 case TRUNC_DIV_EXPR:
4910 case TRUNC_MOD_EXPR:
4911 case BIT_XOR_EXPR:
4912 case BIT_AND_EXPR:
4913 case BIT_IOR_EXPR:
4914 case LSHIFT_EXPR:
4915 case RSHIFT_EXPR:
4917 case MODOP_EXPR:
4919 case EQ_EXPR:
4920 case NE_EXPR:
4921 case LE_EXPR:
4922 case GE_EXPR:
4923 case LT_EXPR:
4924 case GT_EXPR:
4926 case TRUTH_ANDIF_EXPR:
4927 case TRUTH_ORIF_EXPR:
4929 case COMPOUND_EXPR:
4931 case DOTSTAR_EXPR:
4932 case MEMBER_REF:
4933 return true;
4935 default:
4936 return false;
4940 /* If the next token is a suitable fold operator, consume it and return as
4941 the function above. */
4943 static int
4944 cp_parser_fold_operator (cp_parser *parser)
4946 cp_token* token = cp_lexer_peek_token (parser->lexer);
4947 int code = cp_parser_fold_operator (token);
4948 if (code != ERROR_MARK)
4949 cp_lexer_consume_token (parser->lexer);
4950 return code;
4953 /* Parse a fold-expression.
4955 fold-expression:
4956 ( ... folding-operator cast-expression)
4957 ( cast-expression folding-operator ... )
4958 ( cast-expression folding operator ... folding-operator cast-expression)
4960 Note that the '(' and ')' are matched in primary expression. */
4962 static cp_expr
4963 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4965 cp_id_kind pidk;
4967 // Left fold.
4968 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4970 cp_lexer_consume_token (parser->lexer);
4971 int op = cp_parser_fold_operator (parser);
4972 if (op == ERROR_MARK)
4974 cp_parser_error (parser, "expected binary operator");
4975 return error_mark_node;
4978 tree expr = cp_parser_cast_expression (parser, false, false,
4979 false, &pidk);
4980 if (expr == error_mark_node)
4981 return error_mark_node;
4982 return finish_left_unary_fold_expr (expr, op);
4985 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4986 int op = cp_parser_fold_operator (parser);
4987 if (op == ERROR_MARK)
4989 cp_parser_error (parser, "expected binary operator");
4990 return error_mark_node;
4993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4995 cp_parser_error (parser, "expected ...");
4996 return error_mark_node;
4998 cp_lexer_consume_token (parser->lexer);
5000 /* The operands of a fold-expression are cast-expressions, so binary or
5001 conditional expressions are not allowed. We check this here to avoid
5002 tentative parsing. */
5003 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5004 /* OK, the expression was parenthesized. */;
5005 else if (is_binary_op (TREE_CODE (expr1)))
5006 error_at (location_of (expr1),
5007 "binary expression in operand of fold-expression");
5008 else if (TREE_CODE (expr1) == COND_EXPR
5009 || (REFERENCE_REF_P (expr1)
5010 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5011 error_at (location_of (expr1),
5012 "conditional expression in operand of fold-expression");
5014 // Right fold.
5015 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5016 return finish_right_unary_fold_expr (expr1, op);
5018 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5020 cp_parser_error (parser, "mismatched operator in fold-expression");
5021 return error_mark_node;
5023 cp_lexer_consume_token (parser->lexer);
5025 // Binary left or right fold.
5026 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5027 if (expr2 == error_mark_node)
5028 return error_mark_node;
5029 return finish_binary_fold_expr (expr1, expr2, op);
5032 /* Parse a primary-expression.
5034 primary-expression:
5035 literal
5036 this
5037 ( expression )
5038 id-expression
5039 lambda-expression (C++11)
5041 GNU Extensions:
5043 primary-expression:
5044 ( compound-statement )
5045 __builtin_va_arg ( assignment-expression , type-id )
5046 __builtin_offsetof ( type-id , offsetof-expression )
5048 C++ Extensions:
5049 __has_nothrow_assign ( type-id )
5050 __has_nothrow_constructor ( type-id )
5051 __has_nothrow_copy ( type-id )
5052 __has_trivial_assign ( type-id )
5053 __has_trivial_constructor ( type-id )
5054 __has_trivial_copy ( type-id )
5055 __has_trivial_destructor ( type-id )
5056 __has_virtual_destructor ( type-id )
5057 __is_abstract ( type-id )
5058 __is_base_of ( type-id , type-id )
5059 __is_class ( type-id )
5060 __is_empty ( type-id )
5061 __is_enum ( type-id )
5062 __is_final ( type-id )
5063 __is_literal_type ( type-id )
5064 __is_pod ( type-id )
5065 __is_polymorphic ( type-id )
5066 __is_std_layout ( type-id )
5067 __is_trivial ( type-id )
5068 __is_union ( type-id )
5070 Objective-C++ Extension:
5072 primary-expression:
5073 objc-expression
5075 literal:
5076 __null
5078 ADDRESS_P is true iff this expression was immediately preceded by
5079 "&" and therefore might denote a pointer-to-member. CAST_P is true
5080 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5081 true iff this expression is a template argument.
5083 Returns a representation of the expression. Upon return, *IDK
5084 indicates what kind of id-expression (if any) was present. */
5086 static cp_expr
5087 cp_parser_primary_expression (cp_parser *parser,
5088 bool address_p,
5089 bool cast_p,
5090 bool template_arg_p,
5091 bool decltype_p,
5092 cp_id_kind *idk)
5094 cp_token *token = NULL;
5096 /* Assume the primary expression is not an id-expression. */
5097 *idk = CP_ID_KIND_NONE;
5099 /* Peek at the next token. */
5100 token = cp_lexer_peek_token (parser->lexer);
5101 switch ((int) token->type)
5103 /* literal:
5104 integer-literal
5105 character-literal
5106 floating-literal
5107 string-literal
5108 boolean-literal
5109 pointer-literal
5110 user-defined-literal */
5111 case CPP_CHAR:
5112 case CPP_CHAR16:
5113 case CPP_CHAR32:
5114 case CPP_WCHAR:
5115 case CPP_UTF8CHAR:
5116 case CPP_NUMBER:
5117 case CPP_PREPARSED_EXPR:
5118 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5119 return cp_parser_userdef_numeric_literal (parser);
5120 token = cp_lexer_consume_token (parser->lexer);
5121 if (TREE_CODE (token->u.value) == FIXED_CST)
5123 error_at (token->location,
5124 "fixed-point types not supported in C++");
5125 return error_mark_node;
5127 /* Floating-point literals are only allowed in an integral
5128 constant expression if they are cast to an integral or
5129 enumeration type. */
5130 if (TREE_CODE (token->u.value) == REAL_CST
5131 && parser->integral_constant_expression_p
5132 && pedantic)
5134 /* CAST_P will be set even in invalid code like "int(2.7 +
5135 ...)". Therefore, we have to check that the next token
5136 is sure to end the cast. */
5137 if (cast_p)
5139 cp_token *next_token;
5141 next_token = cp_lexer_peek_token (parser->lexer);
5142 if (/* The comma at the end of an
5143 enumerator-definition. */
5144 next_token->type != CPP_COMMA
5145 /* The curly brace at the end of an enum-specifier. */
5146 && next_token->type != CPP_CLOSE_BRACE
5147 /* The end of a statement. */
5148 && next_token->type != CPP_SEMICOLON
5149 /* The end of the cast-expression. */
5150 && next_token->type != CPP_CLOSE_PAREN
5151 /* The end of an array bound. */
5152 && next_token->type != CPP_CLOSE_SQUARE
5153 /* The closing ">" in a template-argument-list. */
5154 && (next_token->type != CPP_GREATER
5155 || parser->greater_than_is_operator_p)
5156 /* C++0x only: A ">>" treated like two ">" tokens,
5157 in a template-argument-list. */
5158 && (next_token->type != CPP_RSHIFT
5159 || (cxx_dialect == cxx98)
5160 || parser->greater_than_is_operator_p))
5161 cast_p = false;
5164 /* If we are within a cast, then the constraint that the
5165 cast is to an integral or enumeration type will be
5166 checked at that point. If we are not within a cast, then
5167 this code is invalid. */
5168 if (!cast_p)
5169 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5171 return cp_expr (token->u.value, token->location);
5173 case CPP_CHAR_USERDEF:
5174 case CPP_CHAR16_USERDEF:
5175 case CPP_CHAR32_USERDEF:
5176 case CPP_WCHAR_USERDEF:
5177 case CPP_UTF8CHAR_USERDEF:
5178 return cp_parser_userdef_char_literal (parser);
5180 case CPP_STRING:
5181 case CPP_STRING16:
5182 case CPP_STRING32:
5183 case CPP_WSTRING:
5184 case CPP_UTF8STRING:
5185 case CPP_STRING_USERDEF:
5186 case CPP_STRING16_USERDEF:
5187 case CPP_STRING32_USERDEF:
5188 case CPP_WSTRING_USERDEF:
5189 case CPP_UTF8STRING_USERDEF:
5190 /* ??? Should wide strings be allowed when parser->translate_strings_p
5191 is false (i.e. in attributes)? If not, we can kill the third
5192 argument to cp_parser_string_literal. */
5193 return cp_parser_string_literal (parser,
5194 parser->translate_strings_p,
5195 true);
5197 case CPP_OPEN_PAREN:
5198 /* If we see `( { ' then we are looking at the beginning of
5199 a GNU statement-expression. */
5200 if (cp_parser_allow_gnu_extensions_p (parser)
5201 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5203 /* Statement-expressions are not allowed by the standard. */
5204 pedwarn (token->location, OPT_Wpedantic,
5205 "ISO C++ forbids braced-groups within expressions");
5207 /* And they're not allowed outside of a function-body; you
5208 cannot, for example, write:
5210 int i = ({ int j = 3; j + 1; });
5212 at class or namespace scope. */
5213 if (!parser->in_function_body
5214 || parser->in_template_argument_list_p)
5216 error_at (token->location,
5217 "statement-expressions are not allowed outside "
5218 "functions nor in template-argument lists");
5219 cp_parser_skip_to_end_of_block_or_statement (parser);
5220 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5221 cp_lexer_consume_token (parser->lexer);
5222 return error_mark_node;
5224 else
5225 return cp_parser_statement_expr (parser);
5227 /* Otherwise it's a normal parenthesized expression. */
5229 cp_expr expr;
5230 bool saved_greater_than_is_operator_p;
5232 location_t open_paren_loc = token->location;
5234 /* Consume the `('. */
5235 matching_parens parens;
5236 parens.consume_open (parser);
5237 /* Within a parenthesized expression, a `>' token is always
5238 the greater-than operator. */
5239 saved_greater_than_is_operator_p
5240 = parser->greater_than_is_operator_p;
5241 parser->greater_than_is_operator_p = true;
5243 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5244 /* Left fold expression. */
5245 expr = NULL_TREE;
5246 else
5247 /* Parse the parenthesized expression. */
5248 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5250 token = cp_lexer_peek_token (parser->lexer);
5251 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5253 expr = cp_parser_fold_expression (parser, expr);
5254 if (expr != error_mark_node
5255 && cxx_dialect < cxx17
5256 && !in_system_header_at (input_location))
5257 pedwarn (input_location, 0, "fold-expressions only available "
5258 "with -std=c++17 or -std=gnu++17");
5260 else
5261 /* Let the front end know that this expression was
5262 enclosed in parentheses. This matters in case, for
5263 example, the expression is of the form `A::B', since
5264 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5265 not. */
5266 expr = finish_parenthesized_expr (expr);
5268 /* DR 705: Wrapping an unqualified name in parentheses
5269 suppresses arg-dependent lookup. We want to pass back
5270 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5271 (c++/37862), but none of the others. */
5272 if (*idk != CP_ID_KIND_QUALIFIED)
5273 *idk = CP_ID_KIND_NONE;
5275 /* The `>' token might be the end of a template-id or
5276 template-parameter-list now. */
5277 parser->greater_than_is_operator_p
5278 = saved_greater_than_is_operator_p;
5280 /* Consume the `)'. */
5281 token = cp_lexer_peek_token (parser->lexer);
5282 location_t close_paren_loc = token->location;
5283 expr.set_range (open_paren_loc, close_paren_loc);
5284 if (!parens.require_close (parser)
5285 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5286 cp_parser_skip_to_end_of_statement (parser);
5288 return expr;
5291 case CPP_OPEN_SQUARE:
5293 if (c_dialect_objc ())
5295 /* We might have an Objective-C++ message. */
5296 cp_parser_parse_tentatively (parser);
5297 tree msg = cp_parser_objc_message_expression (parser);
5298 /* If that works out, we're done ... */
5299 if (cp_parser_parse_definitely (parser))
5300 return msg;
5301 /* ... else, fall though to see if it's a lambda. */
5303 cp_expr lam = cp_parser_lambda_expression (parser);
5304 /* Don't warn about a failed tentative parse. */
5305 if (cp_parser_error_occurred (parser))
5306 return error_mark_node;
5307 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5308 return lam;
5311 case CPP_OBJC_STRING:
5312 if (c_dialect_objc ())
5313 /* We have an Objective-C++ string literal. */
5314 return cp_parser_objc_expression (parser);
5315 cp_parser_error (parser, "expected primary-expression");
5316 return error_mark_node;
5318 case CPP_KEYWORD:
5319 switch (token->keyword)
5321 /* These two are the boolean literals. */
5322 case RID_TRUE:
5323 cp_lexer_consume_token (parser->lexer);
5324 return cp_expr (boolean_true_node, token->location);
5325 case RID_FALSE:
5326 cp_lexer_consume_token (parser->lexer);
5327 return cp_expr (boolean_false_node, token->location);
5329 /* The `__null' literal. */
5330 case RID_NULL:
5331 cp_lexer_consume_token (parser->lexer);
5332 return cp_expr (null_node, token->location);
5334 /* The `nullptr' literal. */
5335 case RID_NULLPTR:
5336 cp_lexer_consume_token (parser->lexer);
5337 return cp_expr (nullptr_node, token->location);
5339 /* Recognize the `this' keyword. */
5340 case RID_THIS:
5341 cp_lexer_consume_token (parser->lexer);
5342 if (parser->local_variables_forbidden_p)
5344 error_at (token->location,
5345 "%<this%> may not be used in this context");
5346 return error_mark_node;
5348 /* Pointers cannot appear in constant-expressions. */
5349 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5350 return error_mark_node;
5351 return cp_expr (finish_this_expr (), token->location);
5353 /* The `operator' keyword can be the beginning of an
5354 id-expression. */
5355 case RID_OPERATOR:
5356 goto id_expression;
5358 case RID_FUNCTION_NAME:
5359 case RID_PRETTY_FUNCTION_NAME:
5360 case RID_C99_FUNCTION_NAME:
5362 non_integral_constant name;
5364 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5365 __func__ are the names of variables -- but they are
5366 treated specially. Therefore, they are handled here,
5367 rather than relying on the generic id-expression logic
5368 below. Grammatically, these names are id-expressions.
5370 Consume the token. */
5371 token = cp_lexer_consume_token (parser->lexer);
5373 switch (token->keyword)
5375 case RID_FUNCTION_NAME:
5376 name = NIC_FUNC_NAME;
5377 break;
5378 case RID_PRETTY_FUNCTION_NAME:
5379 name = NIC_PRETTY_FUNC;
5380 break;
5381 case RID_C99_FUNCTION_NAME:
5382 name = NIC_C99_FUNC;
5383 break;
5384 default:
5385 gcc_unreachable ();
5388 if (cp_parser_non_integral_constant_expression (parser, name))
5389 return error_mark_node;
5391 /* Look up the name. */
5392 return finish_fname (token->u.value);
5395 case RID_VA_ARG:
5397 tree expression;
5398 tree type;
5399 source_location type_location;
5400 location_t start_loc
5401 = cp_lexer_peek_token (parser->lexer)->location;
5402 /* The `__builtin_va_arg' construct is used to handle
5403 `va_arg'. Consume the `__builtin_va_arg' token. */
5404 cp_lexer_consume_token (parser->lexer);
5405 /* Look for the opening `('. */
5406 matching_parens parens;
5407 parens.require_open (parser);
5408 /* Now, parse the assignment-expression. */
5409 expression = cp_parser_assignment_expression (parser);
5410 /* Look for the `,'. */
5411 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5412 type_location = cp_lexer_peek_token (parser->lexer)->location;
5413 /* Parse the type-id. */
5415 type_id_in_expr_sentinel s (parser);
5416 type = cp_parser_type_id (parser);
5418 /* Look for the closing `)'. */
5419 location_t finish_loc
5420 = cp_lexer_peek_token (parser->lexer)->location;
5421 parens.require_close (parser);
5422 /* Using `va_arg' in a constant-expression is not
5423 allowed. */
5424 if (cp_parser_non_integral_constant_expression (parser,
5425 NIC_VA_ARG))
5426 return error_mark_node;
5427 /* Construct a location of the form:
5428 __builtin_va_arg (v, int)
5429 ~~~~~~~~~~~~~~~~~~~~~^~~~
5430 with the caret at the type, ranging from the start of the
5431 "__builtin_va_arg" token to the close paren. */
5432 location_t combined_loc
5433 = make_location (type_location, start_loc, finish_loc);
5434 return build_x_va_arg (combined_loc, expression, type);
5437 case RID_OFFSETOF:
5438 return cp_parser_builtin_offsetof (parser);
5440 case RID_HAS_NOTHROW_ASSIGN:
5441 case RID_HAS_NOTHROW_CONSTRUCTOR:
5442 case RID_HAS_NOTHROW_COPY:
5443 case RID_HAS_TRIVIAL_ASSIGN:
5444 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5445 case RID_HAS_TRIVIAL_COPY:
5446 case RID_HAS_TRIVIAL_DESTRUCTOR:
5447 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5448 case RID_HAS_VIRTUAL_DESTRUCTOR:
5449 case RID_IS_ABSTRACT:
5450 case RID_IS_AGGREGATE:
5451 case RID_IS_BASE_OF:
5452 case RID_IS_CLASS:
5453 case RID_IS_EMPTY:
5454 case RID_IS_ENUM:
5455 case RID_IS_FINAL:
5456 case RID_IS_LITERAL_TYPE:
5457 case RID_IS_POD:
5458 case RID_IS_POLYMORPHIC:
5459 case RID_IS_SAME_AS:
5460 case RID_IS_STD_LAYOUT:
5461 case RID_IS_TRIVIAL:
5462 case RID_IS_TRIVIALLY_ASSIGNABLE:
5463 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5464 case RID_IS_TRIVIALLY_COPYABLE:
5465 case RID_IS_UNION:
5466 case RID_IS_ASSIGNABLE:
5467 case RID_IS_CONSTRUCTIBLE:
5468 return cp_parser_trait_expr (parser, token->keyword);
5470 // C++ concepts
5471 case RID_REQUIRES:
5472 return cp_parser_requires_expression (parser);
5474 /* Objective-C++ expressions. */
5475 case RID_AT_ENCODE:
5476 case RID_AT_PROTOCOL:
5477 case RID_AT_SELECTOR:
5478 return cp_parser_objc_expression (parser);
5480 case RID_TEMPLATE:
5481 if (parser->in_function_body
5482 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5483 == CPP_LESS))
5485 error_at (token->location,
5486 "a template declaration cannot appear at block scope");
5487 cp_parser_skip_to_end_of_block_or_statement (parser);
5488 return error_mark_node;
5490 /* FALLTHRU */
5491 default:
5492 cp_parser_error (parser, "expected primary-expression");
5493 return error_mark_node;
5496 /* An id-expression can start with either an identifier, a
5497 `::' as the beginning of a qualified-id, or the "operator"
5498 keyword. */
5499 case CPP_NAME:
5500 case CPP_SCOPE:
5501 case CPP_TEMPLATE_ID:
5502 case CPP_NESTED_NAME_SPECIFIER:
5504 id_expression:
5505 cp_expr id_expression;
5506 cp_expr decl;
5507 const char *error_msg;
5508 bool template_p;
5509 bool done;
5510 cp_token *id_expr_token;
5512 /* Parse the id-expression. */
5513 id_expression
5514 = cp_parser_id_expression (parser,
5515 /*template_keyword_p=*/false,
5516 /*check_dependency_p=*/true,
5517 &template_p,
5518 /*declarator_p=*/false,
5519 /*optional_p=*/false);
5520 if (id_expression == error_mark_node)
5521 return error_mark_node;
5522 id_expr_token = token;
5523 token = cp_lexer_peek_token (parser->lexer);
5524 done = (token->type != CPP_OPEN_SQUARE
5525 && token->type != CPP_OPEN_PAREN
5526 && token->type != CPP_DOT
5527 && token->type != CPP_DEREF
5528 && token->type != CPP_PLUS_PLUS
5529 && token->type != CPP_MINUS_MINUS);
5530 /* If we have a template-id, then no further lookup is
5531 required. If the template-id was for a template-class, we
5532 will sometimes have a TYPE_DECL at this point. */
5533 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5534 || TREE_CODE (id_expression) == TYPE_DECL)
5535 decl = id_expression;
5536 /* Look up the name. */
5537 else
5539 tree ambiguous_decls;
5541 /* If we already know that this lookup is ambiguous, then
5542 we've already issued an error message; there's no reason
5543 to check again. */
5544 if (id_expr_token->type == CPP_NAME
5545 && id_expr_token->error_reported)
5547 cp_parser_simulate_error (parser);
5548 return error_mark_node;
5551 decl = cp_parser_lookup_name (parser, id_expression,
5552 none_type,
5553 template_p,
5554 /*is_namespace=*/false,
5555 /*check_dependency=*/true,
5556 &ambiguous_decls,
5557 id_expr_token->location);
5558 /* If the lookup was ambiguous, an error will already have
5559 been issued. */
5560 if (ambiguous_decls)
5561 return error_mark_node;
5563 /* In Objective-C++, we may have an Objective-C 2.0
5564 dot-syntax for classes here. */
5565 if (c_dialect_objc ()
5566 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5567 && TREE_CODE (decl) == TYPE_DECL
5568 && objc_is_class_name (decl))
5570 tree component;
5571 cp_lexer_consume_token (parser->lexer);
5572 component = cp_parser_identifier (parser);
5573 if (component == error_mark_node)
5574 return error_mark_node;
5576 tree result = objc_build_class_component_ref (id_expression,
5577 component);
5578 /* Build a location of the form:
5579 expr.component
5580 ~~~~~^~~~~~~~~
5581 with caret at the start of the component name (at
5582 input_location), ranging from the start of the id_expression
5583 to the end of the component name. */
5584 location_t combined_loc
5585 = make_location (input_location, id_expression.get_start (),
5586 get_finish (input_location));
5587 protected_set_expr_location (result, combined_loc);
5588 return result;
5591 /* In Objective-C++, an instance variable (ivar) may be preferred
5592 to whatever cp_parser_lookup_name() found.
5593 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5594 rest of c-family, we have to do a little extra work to preserve
5595 any location information in cp_expr "decl". Given that
5596 objc_lookup_ivar is implemented in "c-family" and "objc", we
5597 have a trip through the pure "tree" type, rather than cp_expr.
5598 Naively copying it back to "decl" would implicitly give the
5599 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5600 store an EXPR_LOCATION. Hence we only update "decl" (and
5601 hence its location_t) if we get back a different tree node. */
5602 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5603 id_expression);
5604 if (decl_tree != decl.get_value ())
5605 decl = cp_expr (decl_tree);
5607 /* If name lookup gives us a SCOPE_REF, then the
5608 qualifying scope was dependent. */
5609 if (TREE_CODE (decl) == SCOPE_REF)
5611 /* At this point, we do not know if DECL is a valid
5612 integral constant expression. We assume that it is
5613 in fact such an expression, so that code like:
5615 template <int N> struct A {
5616 int a[B<N>::i];
5619 is accepted. At template-instantiation time, we
5620 will check that B<N>::i is actually a constant. */
5621 return decl;
5623 /* Check to see if DECL is a local variable in a context
5624 where that is forbidden. */
5625 if (parser->local_variables_forbidden_p
5626 && local_variable_p (decl))
5628 error_at (id_expr_token->location,
5629 "local variable %qD may not appear in this context",
5630 decl.get_value ());
5631 return error_mark_node;
5635 if (processing_template_decl)
5636 if (tree fns = maybe_get_fns (decl))
5637 /* It's too difficult to mark ths in all the places where
5638 we know for sure we need to keep the lookup, so do it
5639 now. The cost is extra GC to recycle the lookups
5640 resolved at parse time. */
5641 lookup_keep (fns);
5643 decl = (finish_id_expression
5644 (id_expression, decl, parser->scope,
5645 idk,
5646 parser->integral_constant_expression_p,
5647 parser->allow_non_integral_constant_expression_p,
5648 &parser->non_integral_constant_expression_p,
5649 template_p, done, address_p,
5650 template_arg_p,
5651 &error_msg,
5652 id_expression.get_location ()));
5653 if (error_msg)
5654 cp_parser_error (parser, error_msg);
5655 decl.set_location (id_expr_token->location);
5656 return decl;
5659 /* Anything else is an error. */
5660 default:
5661 cp_parser_error (parser, "expected primary-expression");
5662 return error_mark_node;
5666 static inline cp_expr
5667 cp_parser_primary_expression (cp_parser *parser,
5668 bool address_p,
5669 bool cast_p,
5670 bool template_arg_p,
5671 cp_id_kind *idk)
5673 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5674 /*decltype*/false, idk);
5677 /* Parse an id-expression.
5679 id-expression:
5680 unqualified-id
5681 qualified-id
5683 qualified-id:
5684 :: [opt] nested-name-specifier template [opt] unqualified-id
5685 :: identifier
5686 :: operator-function-id
5687 :: template-id
5689 Return a representation of the unqualified portion of the
5690 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5691 a `::' or nested-name-specifier.
5693 Often, if the id-expression was a qualified-id, the caller will
5694 want to make a SCOPE_REF to represent the qualified-id. This
5695 function does not do this in order to avoid wastefully creating
5696 SCOPE_REFs when they are not required.
5698 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5699 `template' keyword.
5701 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5702 uninstantiated templates.
5704 If *TEMPLATE_P is non-NULL, it is set to true iff the
5705 `template' keyword is used to explicitly indicate that the entity
5706 named is a template.
5708 If DECLARATOR_P is true, the id-expression is appearing as part of
5709 a declarator, rather than as part of an expression. */
5711 static cp_expr
5712 cp_parser_id_expression (cp_parser *parser,
5713 bool template_keyword_p,
5714 bool check_dependency_p,
5715 bool *template_p,
5716 bool declarator_p,
5717 bool optional_p)
5719 bool global_scope_p;
5720 bool nested_name_specifier_p;
5722 /* Assume the `template' keyword was not used. */
5723 if (template_p)
5724 *template_p = template_keyword_p;
5726 /* Look for the optional `::' operator. */
5727 global_scope_p
5728 = (!template_keyword_p
5729 && (cp_parser_global_scope_opt (parser,
5730 /*current_scope_valid_p=*/false)
5731 != NULL_TREE));
5733 /* Look for the optional nested-name-specifier. */
5734 nested_name_specifier_p
5735 = (cp_parser_nested_name_specifier_opt (parser,
5736 /*typename_keyword_p=*/false,
5737 check_dependency_p,
5738 /*type_p=*/false,
5739 declarator_p,
5740 template_keyword_p)
5741 != NULL_TREE);
5743 /* If there is a nested-name-specifier, then we are looking at
5744 the first qualified-id production. */
5745 if (nested_name_specifier_p)
5747 tree saved_scope;
5748 tree saved_object_scope;
5749 tree saved_qualifying_scope;
5750 cp_expr unqualified_id;
5751 bool is_template;
5753 /* See if the next token is the `template' keyword. */
5754 if (!template_p)
5755 template_p = &is_template;
5756 *template_p = cp_parser_optional_template_keyword (parser);
5757 /* Name lookup we do during the processing of the
5758 unqualified-id might obliterate SCOPE. */
5759 saved_scope = parser->scope;
5760 saved_object_scope = parser->object_scope;
5761 saved_qualifying_scope = parser->qualifying_scope;
5762 /* Process the final unqualified-id. */
5763 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5764 check_dependency_p,
5765 declarator_p,
5766 /*optional_p=*/false);
5767 /* Restore the SAVED_SCOPE for our caller. */
5768 parser->scope = saved_scope;
5769 parser->object_scope = saved_object_scope;
5770 parser->qualifying_scope = saved_qualifying_scope;
5772 return unqualified_id;
5774 /* Otherwise, if we are in global scope, then we are looking at one
5775 of the other qualified-id productions. */
5776 else if (global_scope_p)
5778 cp_token *token;
5779 tree id;
5781 /* Peek at the next token. */
5782 token = cp_lexer_peek_token (parser->lexer);
5784 /* If it's an identifier, and the next token is not a "<", then
5785 we can avoid the template-id case. This is an optimization
5786 for this common case. */
5787 if (token->type == CPP_NAME
5788 && !cp_parser_nth_token_starts_template_argument_list_p
5789 (parser, 2))
5790 return cp_parser_identifier (parser);
5792 cp_parser_parse_tentatively (parser);
5793 /* Try a template-id. */
5794 id = cp_parser_template_id (parser,
5795 /*template_keyword_p=*/false,
5796 /*check_dependency_p=*/true,
5797 none_type,
5798 declarator_p);
5799 /* If that worked, we're done. */
5800 if (cp_parser_parse_definitely (parser))
5801 return id;
5803 /* Peek at the next token. (Changes in the token buffer may
5804 have invalidated the pointer obtained above.) */
5805 token = cp_lexer_peek_token (parser->lexer);
5807 switch (token->type)
5809 case CPP_NAME:
5810 return cp_parser_identifier (parser);
5812 case CPP_KEYWORD:
5813 if (token->keyword == RID_OPERATOR)
5814 return cp_parser_operator_function_id (parser);
5815 /* Fall through. */
5817 default:
5818 cp_parser_error (parser, "expected id-expression");
5819 return error_mark_node;
5822 else
5823 return cp_parser_unqualified_id (parser, template_keyword_p,
5824 /*check_dependency_p=*/true,
5825 declarator_p,
5826 optional_p);
5829 /* Parse an unqualified-id.
5831 unqualified-id:
5832 identifier
5833 operator-function-id
5834 conversion-function-id
5835 ~ class-name
5836 template-id
5838 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5839 keyword, in a construct like `A::template ...'.
5841 Returns a representation of unqualified-id. For the `identifier'
5842 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5843 production a BIT_NOT_EXPR is returned; the operand of the
5844 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5845 other productions, see the documentation accompanying the
5846 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5847 names are looked up in uninstantiated templates. If DECLARATOR_P
5848 is true, the unqualified-id is appearing as part of a declarator,
5849 rather than as part of an expression. */
5851 static cp_expr
5852 cp_parser_unqualified_id (cp_parser* parser,
5853 bool template_keyword_p,
5854 bool check_dependency_p,
5855 bool declarator_p,
5856 bool optional_p)
5858 cp_token *token;
5860 /* Peek at the next token. */
5861 token = cp_lexer_peek_token (parser->lexer);
5863 switch ((int) token->type)
5865 case CPP_NAME:
5867 tree id;
5869 /* We don't know yet whether or not this will be a
5870 template-id. */
5871 cp_parser_parse_tentatively (parser);
5872 /* Try a template-id. */
5873 id = cp_parser_template_id (parser, template_keyword_p,
5874 check_dependency_p,
5875 none_type,
5876 declarator_p);
5877 /* If it worked, we're done. */
5878 if (cp_parser_parse_definitely (parser))
5879 return id;
5880 /* Otherwise, it's an ordinary identifier. */
5881 return cp_parser_identifier (parser);
5884 case CPP_TEMPLATE_ID:
5885 return cp_parser_template_id (parser, template_keyword_p,
5886 check_dependency_p,
5887 none_type,
5888 declarator_p);
5890 case CPP_COMPL:
5892 tree type_decl;
5893 tree qualifying_scope;
5894 tree object_scope;
5895 tree scope;
5896 bool done;
5898 /* Consume the `~' token. */
5899 cp_lexer_consume_token (parser->lexer);
5900 /* Parse the class-name. The standard, as written, seems to
5901 say that:
5903 template <typename T> struct S { ~S (); };
5904 template <typename T> S<T>::~S() {}
5906 is invalid, since `~' must be followed by a class-name, but
5907 `S<T>' is dependent, and so not known to be a class.
5908 That's not right; we need to look in uninstantiated
5909 templates. A further complication arises from:
5911 template <typename T> void f(T t) {
5912 t.T::~T();
5915 Here, it is not possible to look up `T' in the scope of `T'
5916 itself. We must look in both the current scope, and the
5917 scope of the containing complete expression.
5919 Yet another issue is:
5921 struct S {
5922 int S;
5923 ~S();
5926 S::~S() {}
5928 The standard does not seem to say that the `S' in `~S'
5929 should refer to the type `S' and not the data member
5930 `S::S'. */
5932 /* DR 244 says that we look up the name after the "~" in the
5933 same scope as we looked up the qualifying name. That idea
5934 isn't fully worked out; it's more complicated than that. */
5935 scope = parser->scope;
5936 object_scope = parser->object_scope;
5937 qualifying_scope = parser->qualifying_scope;
5939 /* Check for invalid scopes. */
5940 if (scope == error_mark_node)
5942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5943 cp_lexer_consume_token (parser->lexer);
5944 return error_mark_node;
5946 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5948 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5949 error_at (token->location,
5950 "scope %qT before %<~%> is not a class-name",
5951 scope);
5952 cp_parser_simulate_error (parser);
5953 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5954 cp_lexer_consume_token (parser->lexer);
5955 return error_mark_node;
5957 gcc_assert (!scope || TYPE_P (scope));
5959 /* If the name is of the form "X::~X" it's OK even if X is a
5960 typedef. */
5961 token = cp_lexer_peek_token (parser->lexer);
5962 if (scope
5963 && token->type == CPP_NAME
5964 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5965 != CPP_LESS)
5966 && (token->u.value == TYPE_IDENTIFIER (scope)
5967 || (CLASS_TYPE_P (scope)
5968 && constructor_name_p (token->u.value, scope))))
5970 cp_lexer_consume_token (parser->lexer);
5971 return build_nt (BIT_NOT_EXPR, scope);
5974 /* ~auto means the destructor of whatever the object is. */
5975 if (cp_parser_is_keyword (token, RID_AUTO))
5977 if (cxx_dialect < cxx14)
5978 pedwarn (input_location, 0,
5979 "%<~auto%> only available with "
5980 "-std=c++14 or -std=gnu++14");
5981 cp_lexer_consume_token (parser->lexer);
5982 return build_nt (BIT_NOT_EXPR, make_auto ());
5985 /* If there was an explicit qualification (S::~T), first look
5986 in the scope given by the qualification (i.e., S).
5988 Note: in the calls to cp_parser_class_name below we pass
5989 typename_type so that lookup finds the injected-class-name
5990 rather than the constructor. */
5991 done = false;
5992 type_decl = NULL_TREE;
5993 if (scope)
5995 cp_parser_parse_tentatively (parser);
5996 type_decl = cp_parser_class_name (parser,
5997 /*typename_keyword_p=*/false,
5998 /*template_keyword_p=*/false,
5999 typename_type,
6000 /*check_dependency=*/false,
6001 /*class_head_p=*/false,
6002 declarator_p);
6003 if (cp_parser_parse_definitely (parser))
6004 done = true;
6006 /* In "N::S::~S", look in "N" as well. */
6007 if (!done && scope && qualifying_scope)
6009 cp_parser_parse_tentatively (parser);
6010 parser->scope = qualifying_scope;
6011 parser->object_scope = NULL_TREE;
6012 parser->qualifying_scope = NULL_TREE;
6013 type_decl
6014 = cp_parser_class_name (parser,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6017 typename_type,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6020 declarator_p);
6021 if (cp_parser_parse_definitely (parser))
6022 done = true;
6024 /* In "p->S::~T", look in the scope given by "*p" as well. */
6025 else if (!done && object_scope)
6027 cp_parser_parse_tentatively (parser);
6028 parser->scope = object_scope;
6029 parser->object_scope = NULL_TREE;
6030 parser->qualifying_scope = NULL_TREE;
6031 type_decl
6032 = cp_parser_class_name (parser,
6033 /*typename_keyword_p=*/false,
6034 /*template_keyword_p=*/false,
6035 typename_type,
6036 /*check_dependency=*/false,
6037 /*class_head_p=*/false,
6038 declarator_p);
6039 if (cp_parser_parse_definitely (parser))
6040 done = true;
6042 /* Look in the surrounding context. */
6043 if (!done)
6045 parser->scope = NULL_TREE;
6046 parser->object_scope = NULL_TREE;
6047 parser->qualifying_scope = NULL_TREE;
6048 if (processing_template_decl)
6049 cp_parser_parse_tentatively (parser);
6050 type_decl
6051 = cp_parser_class_name (parser,
6052 /*typename_keyword_p=*/false,
6053 /*template_keyword_p=*/false,
6054 typename_type,
6055 /*check_dependency=*/false,
6056 /*class_head_p=*/false,
6057 declarator_p);
6058 if (processing_template_decl
6059 && ! cp_parser_parse_definitely (parser))
6061 /* We couldn't find a type with this name. If we're parsing
6062 tentatively, fail and try something else. */
6063 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6065 cp_parser_simulate_error (parser);
6066 return error_mark_node;
6068 /* Otherwise, accept it and check for a match at instantiation
6069 time. */
6070 type_decl = cp_parser_identifier (parser);
6071 if (type_decl != error_mark_node)
6072 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6073 return type_decl;
6076 /* If an error occurred, assume that the name of the
6077 destructor is the same as the name of the qualifying
6078 class. That allows us to keep parsing after running
6079 into ill-formed destructor names. */
6080 if (type_decl == error_mark_node && scope)
6081 return build_nt (BIT_NOT_EXPR, scope);
6082 else if (type_decl == error_mark_node)
6083 return error_mark_node;
6085 /* Check that destructor name and scope match. */
6086 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6088 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6089 error_at (token->location,
6090 "declaration of %<~%T%> as member of %qT",
6091 type_decl, scope);
6092 cp_parser_simulate_error (parser);
6093 return error_mark_node;
6096 /* [class.dtor]
6098 A typedef-name that names a class shall not be used as the
6099 identifier in the declarator for a destructor declaration. */
6100 if (declarator_p
6101 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6102 && !DECL_SELF_REFERENCE_P (type_decl)
6103 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6104 error_at (token->location,
6105 "typedef-name %qD used as destructor declarator",
6106 type_decl);
6108 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6111 case CPP_KEYWORD:
6112 if (token->keyword == RID_OPERATOR)
6114 cp_expr id;
6116 /* This could be a template-id, so we try that first. */
6117 cp_parser_parse_tentatively (parser);
6118 /* Try a template-id. */
6119 id = cp_parser_template_id (parser, template_keyword_p,
6120 /*check_dependency_p=*/true,
6121 none_type,
6122 declarator_p);
6123 /* If that worked, we're done. */
6124 if (cp_parser_parse_definitely (parser))
6125 return id;
6126 /* We still don't know whether we're looking at an
6127 operator-function-id or a conversion-function-id. */
6128 cp_parser_parse_tentatively (parser);
6129 /* Try an operator-function-id. */
6130 id = cp_parser_operator_function_id (parser);
6131 /* If that didn't work, try a conversion-function-id. */
6132 if (!cp_parser_parse_definitely (parser))
6133 id = cp_parser_conversion_function_id (parser);
6135 return id;
6137 /* Fall through. */
6139 default:
6140 if (optional_p)
6141 return NULL_TREE;
6142 cp_parser_error (parser, "expected unqualified-id");
6143 return error_mark_node;
6147 /* Parse an (optional) nested-name-specifier.
6149 nested-name-specifier: [C++98]
6150 class-or-namespace-name :: nested-name-specifier [opt]
6151 class-or-namespace-name :: template nested-name-specifier [opt]
6153 nested-name-specifier: [C++0x]
6154 type-name ::
6155 namespace-name ::
6156 nested-name-specifier identifier ::
6157 nested-name-specifier template [opt] simple-template-id ::
6159 PARSER->SCOPE should be set appropriately before this function is
6160 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6161 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6162 in name lookups.
6164 Sets PARSER->SCOPE to the class (TYPE) or namespace
6165 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6166 it unchanged if there is no nested-name-specifier. Returns the new
6167 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6169 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6170 part of a declaration and/or decl-specifier. */
6172 static tree
6173 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6174 bool typename_keyword_p,
6175 bool check_dependency_p,
6176 bool type_p,
6177 bool is_declaration,
6178 bool template_keyword_p /* = false */)
6180 bool success = false;
6181 cp_token_position start = 0;
6182 cp_token *token;
6184 /* Remember where the nested-name-specifier starts. */
6185 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6187 start = cp_lexer_token_position (parser->lexer, false);
6188 push_deferring_access_checks (dk_deferred);
6191 while (true)
6193 tree new_scope;
6194 tree old_scope;
6195 tree saved_qualifying_scope;
6197 /* Spot cases that cannot be the beginning of a
6198 nested-name-specifier. */
6199 token = cp_lexer_peek_token (parser->lexer);
6201 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6202 the already parsed nested-name-specifier. */
6203 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6205 /* Grab the nested-name-specifier and continue the loop. */
6206 cp_parser_pre_parsed_nested_name_specifier (parser);
6207 /* If we originally encountered this nested-name-specifier
6208 with IS_DECLARATION set to false, we will not have
6209 resolved TYPENAME_TYPEs, so we must do so here. */
6210 if (is_declaration
6211 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6213 new_scope = resolve_typename_type (parser->scope,
6214 /*only_current_p=*/false);
6215 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6216 parser->scope = new_scope;
6218 success = true;
6219 continue;
6222 /* Spot cases that cannot be the beginning of a
6223 nested-name-specifier. On the second and subsequent times
6224 through the loop, we look for the `template' keyword. */
6225 if (success && token->keyword == RID_TEMPLATE)
6227 /* A template-id can start a nested-name-specifier. */
6228 else if (token->type == CPP_TEMPLATE_ID)
6230 /* DR 743: decltype can be used in a nested-name-specifier. */
6231 else if (token_is_decltype (token))
6233 else
6235 /* If the next token is not an identifier, then it is
6236 definitely not a type-name or namespace-name. */
6237 if (token->type != CPP_NAME)
6238 break;
6239 /* If the following token is neither a `<' (to begin a
6240 template-id), nor a `::', then we are not looking at a
6241 nested-name-specifier. */
6242 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6244 if (token->type == CPP_COLON
6245 && parser->colon_corrects_to_scope_p
6246 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6248 gcc_rich_location richloc (token->location);
6249 richloc.add_fixit_replace ("::");
6250 error_at (&richloc,
6251 "found %<:%> in nested-name-specifier, "
6252 "expected %<::%>");
6253 token->type = CPP_SCOPE;
6256 if (token->type != CPP_SCOPE
6257 && !cp_parser_nth_token_starts_template_argument_list_p
6258 (parser, 2))
6259 break;
6262 /* The nested-name-specifier is optional, so we parse
6263 tentatively. */
6264 cp_parser_parse_tentatively (parser);
6266 /* Look for the optional `template' keyword, if this isn't the
6267 first time through the loop. */
6268 if (success)
6269 template_keyword_p = cp_parser_optional_template_keyword (parser);
6271 /* Save the old scope since the name lookup we are about to do
6272 might destroy it. */
6273 old_scope = parser->scope;
6274 saved_qualifying_scope = parser->qualifying_scope;
6275 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6276 look up names in "X<T>::I" in order to determine that "Y" is
6277 a template. So, if we have a typename at this point, we make
6278 an effort to look through it. */
6279 if (is_declaration
6280 && !typename_keyword_p
6281 && parser->scope
6282 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6283 parser->scope = resolve_typename_type (parser->scope,
6284 /*only_current_p=*/false);
6285 /* Parse the qualifying entity. */
6286 new_scope
6287 = cp_parser_qualifying_entity (parser,
6288 typename_keyword_p,
6289 template_keyword_p,
6290 check_dependency_p,
6291 type_p,
6292 is_declaration);
6293 /* Look for the `::' token. */
6294 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6296 /* If we found what we wanted, we keep going; otherwise, we're
6297 done. */
6298 if (!cp_parser_parse_definitely (parser))
6300 bool error_p = false;
6302 /* Restore the OLD_SCOPE since it was valid before the
6303 failed attempt at finding the last
6304 class-or-namespace-name. */
6305 parser->scope = old_scope;
6306 parser->qualifying_scope = saved_qualifying_scope;
6308 /* If the next token is a decltype, and the one after that is a
6309 `::', then the decltype has failed to resolve to a class or
6310 enumeration type. Give this error even when parsing
6311 tentatively since it can't possibly be valid--and we're going
6312 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6313 won't get another chance.*/
6314 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6315 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6316 == CPP_SCOPE))
6318 token = cp_lexer_consume_token (parser->lexer);
6319 error_at (token->location, "decltype evaluates to %qT, "
6320 "which is not a class or enumeration type",
6321 token->u.tree_check_value->value);
6322 parser->scope = error_mark_node;
6323 error_p = true;
6324 /* As below. */
6325 success = true;
6326 cp_lexer_consume_token (parser->lexer);
6329 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6330 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6332 /* If we have a non-type template-id followed by ::, it can't
6333 possibly be valid. */
6334 token = cp_lexer_peek_token (parser->lexer);
6335 tree tid = token->u.tree_check_value->value;
6336 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6337 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6339 tree tmpl = NULL_TREE;
6340 if (is_overloaded_fn (tid))
6342 tree fns = get_fns (tid);
6343 if (OVL_SINGLE_P (fns))
6344 tmpl = OVL_FIRST (fns);
6345 error_at (token->location, "function template-id %qD "
6346 "in nested-name-specifier", tid);
6348 else
6350 /* Variable template. */
6351 tmpl = TREE_OPERAND (tid, 0);
6352 gcc_assert (variable_template_p (tmpl));
6353 error_at (token->location, "variable template-id %qD "
6354 "in nested-name-specifier", tid);
6356 if (tmpl)
6357 inform (DECL_SOURCE_LOCATION (tmpl),
6358 "%qD declared here", tmpl);
6360 parser->scope = error_mark_node;
6361 error_p = true;
6362 /* As below. */
6363 success = true;
6364 cp_lexer_consume_token (parser->lexer);
6365 cp_lexer_consume_token (parser->lexer);
6369 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6370 break;
6371 /* If the next token is an identifier, and the one after
6372 that is a `::', then any valid interpretation would have
6373 found a class-or-namespace-name. */
6374 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6375 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6376 == CPP_SCOPE)
6377 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6378 != CPP_COMPL))
6380 token = cp_lexer_consume_token (parser->lexer);
6381 if (!error_p)
6383 if (!token->error_reported)
6385 tree decl;
6386 tree ambiguous_decls;
6388 decl = cp_parser_lookup_name (parser, token->u.value,
6389 none_type,
6390 /*is_template=*/false,
6391 /*is_namespace=*/false,
6392 /*check_dependency=*/true,
6393 &ambiguous_decls,
6394 token->location);
6395 if (TREE_CODE (decl) == TEMPLATE_DECL)
6396 error_at (token->location,
6397 "%qD used without template arguments",
6398 decl);
6399 else if (ambiguous_decls)
6401 // cp_parser_lookup_name has the same diagnostic,
6402 // thus make sure to emit it at most once.
6403 if (cp_parser_uncommitted_to_tentative_parse_p
6404 (parser))
6406 error_at (token->location,
6407 "reference to %qD is ambiguous",
6408 token->u.value);
6409 print_candidates (ambiguous_decls);
6411 decl = error_mark_node;
6413 else
6415 if (cxx_dialect != cxx98)
6416 cp_parser_name_lookup_error
6417 (parser, token->u.value, decl, NLE_NOT_CXX98,
6418 token->location);
6419 else
6420 cp_parser_name_lookup_error
6421 (parser, token->u.value, decl, NLE_CXX98,
6422 token->location);
6425 parser->scope = error_mark_node;
6426 error_p = true;
6427 /* Treat this as a successful nested-name-specifier
6428 due to:
6430 [basic.lookup.qual]
6432 If the name found is not a class-name (clause
6433 _class_) or namespace-name (_namespace.def_), the
6434 program is ill-formed. */
6435 success = true;
6437 cp_lexer_consume_token (parser->lexer);
6439 break;
6441 /* We've found one valid nested-name-specifier. */
6442 success = true;
6443 /* Name lookup always gives us a DECL. */
6444 if (TREE_CODE (new_scope) == TYPE_DECL)
6445 new_scope = TREE_TYPE (new_scope);
6446 /* Uses of "template" must be followed by actual templates. */
6447 if (template_keyword_p
6448 && !(CLASS_TYPE_P (new_scope)
6449 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6451 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6452 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6453 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6454 == TEMPLATE_ID_EXPR)))
6455 permerror (input_location, TYPE_P (new_scope)
6456 ? G_("%qT is not a template")
6457 : G_("%qD is not a template"),
6458 new_scope);
6459 /* If it is a class scope, try to complete it; we are about to
6460 be looking up names inside the class. */
6461 if (TYPE_P (new_scope)
6462 /* Since checking types for dependency can be expensive,
6463 avoid doing it if the type is already complete. */
6464 && !COMPLETE_TYPE_P (new_scope)
6465 /* Do not try to complete dependent types. */
6466 && !dependent_type_p (new_scope))
6468 new_scope = complete_type (new_scope);
6469 /* If it is a typedef to current class, use the current
6470 class instead, as the typedef won't have any names inside
6471 it yet. */
6472 if (!COMPLETE_TYPE_P (new_scope)
6473 && currently_open_class (new_scope))
6474 new_scope = TYPE_MAIN_VARIANT (new_scope);
6476 /* Make sure we look in the right scope the next time through
6477 the loop. */
6478 parser->scope = new_scope;
6481 /* If parsing tentatively, replace the sequence of tokens that makes
6482 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6483 token. That way, should we re-parse the token stream, we will
6484 not have to repeat the effort required to do the parse, nor will
6485 we issue duplicate error messages. */
6486 if (success && start)
6488 cp_token *token;
6490 token = cp_lexer_token_at (parser->lexer, start);
6491 /* Reset the contents of the START token. */
6492 token->type = CPP_NESTED_NAME_SPECIFIER;
6493 /* Retrieve any deferred checks. Do not pop this access checks yet
6494 so the memory will not be reclaimed during token replacing below. */
6495 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6496 token->u.tree_check_value->value = parser->scope;
6497 token->u.tree_check_value->checks = get_deferred_access_checks ();
6498 token->u.tree_check_value->qualifying_scope =
6499 parser->qualifying_scope;
6500 token->keyword = RID_MAX;
6502 /* Purge all subsequent tokens. */
6503 cp_lexer_purge_tokens_after (parser->lexer, start);
6506 if (start)
6507 pop_to_parent_deferring_access_checks ();
6509 return success ? parser->scope : NULL_TREE;
6512 /* Parse a nested-name-specifier. See
6513 cp_parser_nested_name_specifier_opt for details. This function
6514 behaves identically, except that it will an issue an error if no
6515 nested-name-specifier is present. */
6517 static tree
6518 cp_parser_nested_name_specifier (cp_parser *parser,
6519 bool typename_keyword_p,
6520 bool check_dependency_p,
6521 bool type_p,
6522 bool is_declaration)
6524 tree scope;
6526 /* Look for the nested-name-specifier. */
6527 scope = cp_parser_nested_name_specifier_opt (parser,
6528 typename_keyword_p,
6529 check_dependency_p,
6530 type_p,
6531 is_declaration);
6532 /* If it was not present, issue an error message. */
6533 if (!scope)
6535 cp_parser_error (parser, "expected nested-name-specifier");
6536 parser->scope = NULL_TREE;
6539 return scope;
6542 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6543 this is either a class-name or a namespace-name (which corresponds
6544 to the class-or-namespace-name production in the grammar). For
6545 C++0x, it can also be a type-name that refers to an enumeration
6546 type or a simple-template-id.
6548 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6549 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6550 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6551 TYPE_P is TRUE iff the next name should be taken as a class-name,
6552 even the same name is declared to be another entity in the same
6553 scope.
6555 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6556 specified by the class-or-namespace-name. If neither is found the
6557 ERROR_MARK_NODE is returned. */
6559 static tree
6560 cp_parser_qualifying_entity (cp_parser *parser,
6561 bool typename_keyword_p,
6562 bool template_keyword_p,
6563 bool check_dependency_p,
6564 bool type_p,
6565 bool is_declaration)
6567 tree saved_scope;
6568 tree saved_qualifying_scope;
6569 tree saved_object_scope;
6570 tree scope;
6571 bool only_class_p;
6572 bool successful_parse_p;
6574 /* DR 743: decltype can appear in a nested-name-specifier. */
6575 if (cp_lexer_next_token_is_decltype (parser->lexer))
6577 scope = cp_parser_decltype (parser);
6578 if (TREE_CODE (scope) != ENUMERAL_TYPE
6579 && !MAYBE_CLASS_TYPE_P (scope))
6581 cp_parser_simulate_error (parser);
6582 return error_mark_node;
6584 if (TYPE_NAME (scope))
6585 scope = TYPE_NAME (scope);
6586 return scope;
6589 /* Before we try to parse the class-name, we must save away the
6590 current PARSER->SCOPE since cp_parser_class_name will destroy
6591 it. */
6592 saved_scope = parser->scope;
6593 saved_qualifying_scope = parser->qualifying_scope;
6594 saved_object_scope = parser->object_scope;
6595 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6596 there is no need to look for a namespace-name. */
6597 only_class_p = template_keyword_p
6598 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6599 if (!only_class_p)
6600 cp_parser_parse_tentatively (parser);
6601 scope = cp_parser_class_name (parser,
6602 typename_keyword_p,
6603 template_keyword_p,
6604 type_p ? class_type : none_type,
6605 check_dependency_p,
6606 /*class_head_p=*/false,
6607 is_declaration,
6608 /*enum_ok=*/cxx_dialect > cxx98);
6609 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6610 /* If that didn't work, try for a namespace-name. */
6611 if (!only_class_p && !successful_parse_p)
6613 /* Restore the saved scope. */
6614 parser->scope = saved_scope;
6615 parser->qualifying_scope = saved_qualifying_scope;
6616 parser->object_scope = saved_object_scope;
6617 /* If we are not looking at an identifier followed by the scope
6618 resolution operator, then this is not part of a
6619 nested-name-specifier. (Note that this function is only used
6620 to parse the components of a nested-name-specifier.) */
6621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6622 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6623 return error_mark_node;
6624 scope = cp_parser_namespace_name (parser);
6627 return scope;
6630 /* Return true if we are looking at a compound-literal, false otherwise. */
6632 static bool
6633 cp_parser_compound_literal_p (cp_parser *parser)
6635 cp_lexer_save_tokens (parser->lexer);
6637 /* Skip tokens until the next token is a closing parenthesis.
6638 If we find the closing `)', and the next token is a `{', then
6639 we are looking at a compound-literal. */
6640 bool compound_literal_p
6641 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6642 /*consume_paren=*/true)
6643 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6645 /* Roll back the tokens we skipped. */
6646 cp_lexer_rollback_tokens (parser->lexer);
6648 return compound_literal_p;
6651 /* Return true if EXPR is the integer constant zero or a complex constant
6652 of zero, without any folding, but ignoring location wrappers. */
6654 bool
6655 literal_integer_zerop (const_tree expr)
6657 return (location_wrapper_p (expr)
6658 && integer_zerop (TREE_OPERAND (expr, 0)));
6661 /* Parse a postfix-expression.
6663 postfix-expression:
6664 primary-expression
6665 postfix-expression [ expression ]
6666 postfix-expression ( expression-list [opt] )
6667 simple-type-specifier ( expression-list [opt] )
6668 typename :: [opt] nested-name-specifier identifier
6669 ( expression-list [opt] )
6670 typename :: [opt] nested-name-specifier template [opt] template-id
6671 ( expression-list [opt] )
6672 postfix-expression . template [opt] id-expression
6673 postfix-expression -> template [opt] id-expression
6674 postfix-expression . pseudo-destructor-name
6675 postfix-expression -> pseudo-destructor-name
6676 postfix-expression ++
6677 postfix-expression --
6678 dynamic_cast < type-id > ( expression )
6679 static_cast < type-id > ( expression )
6680 reinterpret_cast < type-id > ( expression )
6681 const_cast < type-id > ( expression )
6682 typeid ( expression )
6683 typeid ( type-id )
6685 GNU Extension:
6687 postfix-expression:
6688 ( type-id ) { initializer-list , [opt] }
6690 This extension is a GNU version of the C99 compound-literal
6691 construct. (The C99 grammar uses `type-name' instead of `type-id',
6692 but they are essentially the same concept.)
6694 If ADDRESS_P is true, the postfix expression is the operand of the
6695 `&' operator. CAST_P is true if this expression is the target of a
6696 cast.
6698 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6699 class member access expressions [expr.ref].
6701 Returns a representation of the expression. */
6703 static cp_expr
6704 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6705 bool member_access_only_p, bool decltype_p,
6706 cp_id_kind * pidk_return)
6708 cp_token *token;
6709 location_t loc;
6710 enum rid keyword;
6711 cp_id_kind idk = CP_ID_KIND_NONE;
6712 cp_expr postfix_expression = NULL_TREE;
6713 bool is_member_access = false;
6715 /* Peek at the next token. */
6716 token = cp_lexer_peek_token (parser->lexer);
6717 loc = token->location;
6718 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6720 /* Some of the productions are determined by keywords. */
6721 keyword = token->keyword;
6722 switch (keyword)
6724 case RID_DYNCAST:
6725 case RID_STATCAST:
6726 case RID_REINTCAST:
6727 case RID_CONSTCAST:
6729 tree type;
6730 cp_expr expression;
6731 const char *saved_message;
6732 bool saved_in_type_id_in_expr_p;
6734 /* All of these can be handled in the same way from the point
6735 of view of parsing. Begin by consuming the token
6736 identifying the cast. */
6737 cp_lexer_consume_token (parser->lexer);
6739 /* New types cannot be defined in the cast. */
6740 saved_message = parser->type_definition_forbidden_message;
6741 parser->type_definition_forbidden_message
6742 = G_("types may not be defined in casts");
6744 /* Look for the opening `<'. */
6745 cp_parser_require (parser, CPP_LESS, RT_LESS);
6746 /* Parse the type to which we are casting. */
6747 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6748 parser->in_type_id_in_expr_p = true;
6749 type = cp_parser_type_id (parser);
6750 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6751 /* Look for the closing `>'. */
6752 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6753 /* Restore the old message. */
6754 parser->type_definition_forbidden_message = saved_message;
6756 bool saved_greater_than_is_operator_p
6757 = parser->greater_than_is_operator_p;
6758 parser->greater_than_is_operator_p = true;
6760 /* And the expression which is being cast. */
6761 matching_parens parens;
6762 parens.require_open (parser);
6763 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6764 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6765 RT_CLOSE_PAREN);
6766 location_t end_loc = close_paren ?
6767 close_paren->location : UNKNOWN_LOCATION;
6769 parser->greater_than_is_operator_p
6770 = saved_greater_than_is_operator_p;
6772 /* Only type conversions to integral or enumeration types
6773 can be used in constant-expressions. */
6774 if (!cast_valid_in_integral_constant_expression_p (type)
6775 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6777 postfix_expression = error_mark_node;
6778 break;
6781 switch (keyword)
6783 case RID_DYNCAST:
6784 postfix_expression
6785 = build_dynamic_cast (type, expression, tf_warning_or_error);
6786 break;
6787 case RID_STATCAST:
6788 postfix_expression
6789 = build_static_cast (type, expression, tf_warning_or_error);
6790 break;
6791 case RID_REINTCAST:
6792 postfix_expression
6793 = build_reinterpret_cast (type, expression,
6794 tf_warning_or_error);
6795 break;
6796 case RID_CONSTCAST:
6797 postfix_expression
6798 = build_const_cast (type, expression, tf_warning_or_error);
6799 break;
6800 default:
6801 gcc_unreachable ();
6804 /* Construct a location e.g. :
6805 reinterpret_cast <int *> (expr)
6806 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6807 ranging from the start of the "*_cast" token to the final closing
6808 paren, with the caret at the start. */
6809 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6810 postfix_expression.set_location (cp_cast_loc);
6812 break;
6814 case RID_TYPEID:
6816 tree type;
6817 const char *saved_message;
6818 bool saved_in_type_id_in_expr_p;
6820 /* Consume the `typeid' token. */
6821 cp_lexer_consume_token (parser->lexer);
6822 /* Look for the `(' token. */
6823 matching_parens parens;
6824 parens.require_open (parser);
6825 /* Types cannot be defined in a `typeid' expression. */
6826 saved_message = parser->type_definition_forbidden_message;
6827 parser->type_definition_forbidden_message
6828 = G_("types may not be defined in a %<typeid%> expression");
6829 /* We can't be sure yet whether we're looking at a type-id or an
6830 expression. */
6831 cp_parser_parse_tentatively (parser);
6832 /* Try a type-id first. */
6833 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6834 parser->in_type_id_in_expr_p = true;
6835 type = cp_parser_type_id (parser);
6836 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6837 /* Look for the `)' token. Otherwise, we can't be sure that
6838 we're not looking at an expression: consider `typeid (int
6839 (3))', for example. */
6840 cp_token *close_paren = parens.require_close (parser);
6841 /* If all went well, simply lookup the type-id. */
6842 if (cp_parser_parse_definitely (parser))
6843 postfix_expression = get_typeid (type, tf_warning_or_error);
6844 /* Otherwise, fall back to the expression variant. */
6845 else
6847 tree expression;
6849 /* Look for an expression. */
6850 expression = cp_parser_expression (parser, & idk);
6851 /* Compute its typeid. */
6852 postfix_expression = build_typeid (expression, tf_warning_or_error);
6853 /* Look for the `)' token. */
6854 close_paren = parens.require_close (parser);
6856 /* Restore the saved message. */
6857 parser->type_definition_forbidden_message = saved_message;
6858 /* `typeid' may not appear in an integral constant expression. */
6859 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6860 postfix_expression = error_mark_node;
6862 /* Construct a location e.g. :
6863 typeid (expr)
6864 ^~~~~~~~~~~~~
6865 ranging from the start of the "typeid" token to the final closing
6866 paren, with the caret at the start. */
6867 if (close_paren)
6869 location_t typeid_loc
6870 = make_location (start_loc, start_loc, close_paren->location);
6871 postfix_expression.set_location (typeid_loc);
6872 postfix_expression.maybe_add_location_wrapper ();
6875 break;
6877 case RID_TYPENAME:
6879 tree type;
6880 /* The syntax permitted here is the same permitted for an
6881 elaborated-type-specifier. */
6882 ++parser->prevent_constrained_type_specifiers;
6883 type = cp_parser_elaborated_type_specifier (parser,
6884 /*is_friend=*/false,
6885 /*is_declaration=*/false);
6886 --parser->prevent_constrained_type_specifiers;
6887 postfix_expression = cp_parser_functional_cast (parser, type);
6889 break;
6891 case RID_ADDRESSOF:
6892 case RID_BUILTIN_SHUFFLE:
6893 case RID_BUILTIN_LAUNDER:
6895 vec<tree, va_gc> *vec;
6896 unsigned int i;
6897 tree p;
6899 cp_lexer_consume_token (parser->lexer);
6900 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6901 /*cast_p=*/false, /*allow_expansion_p=*/true,
6902 /*non_constant_p=*/NULL);
6903 if (vec == NULL)
6905 postfix_expression = error_mark_node;
6906 break;
6909 FOR_EACH_VEC_ELT (*vec, i, p)
6910 mark_exp_read (p);
6912 switch (keyword)
6914 case RID_ADDRESSOF:
6915 if (vec->length () == 1)
6916 postfix_expression
6917 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6918 else
6920 error_at (loc, "wrong number of arguments to "
6921 "%<__builtin_addressof%>");
6922 postfix_expression = error_mark_node;
6924 break;
6926 case RID_BUILTIN_LAUNDER:
6927 if (vec->length () == 1)
6928 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6929 tf_warning_or_error);
6930 else
6932 error_at (loc, "wrong number of arguments to "
6933 "%<__builtin_launder%>");
6934 postfix_expression = error_mark_node;
6936 break;
6938 case RID_BUILTIN_SHUFFLE:
6939 if (vec->length () == 2)
6940 postfix_expression
6941 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6942 (*vec)[1], tf_warning_or_error);
6943 else if (vec->length () == 3)
6944 postfix_expression
6945 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6946 (*vec)[2], tf_warning_or_error);
6947 else
6949 error_at (loc, "wrong number of arguments to "
6950 "%<__builtin_shuffle%>");
6951 postfix_expression = error_mark_node;
6953 break;
6955 default:
6956 gcc_unreachable ();
6958 break;
6961 default:
6963 tree type;
6965 /* If the next thing is a simple-type-specifier, we may be
6966 looking at a functional cast. We could also be looking at
6967 an id-expression. So, we try the functional cast, and if
6968 that doesn't work we fall back to the primary-expression. */
6969 cp_parser_parse_tentatively (parser);
6970 /* Look for the simple-type-specifier. */
6971 ++parser->prevent_constrained_type_specifiers;
6972 type = cp_parser_simple_type_specifier (parser,
6973 /*decl_specs=*/NULL,
6974 CP_PARSER_FLAGS_NONE);
6975 --parser->prevent_constrained_type_specifiers;
6976 /* Parse the cast itself. */
6977 if (!cp_parser_error_occurred (parser))
6978 postfix_expression
6979 = cp_parser_functional_cast (parser, type);
6980 /* If that worked, we're done. */
6981 if (cp_parser_parse_definitely (parser))
6982 break;
6984 /* If the functional-cast didn't work out, try a
6985 compound-literal. */
6986 if (cp_parser_allow_gnu_extensions_p (parser)
6987 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6989 cp_expr initializer = NULL_TREE;
6991 cp_parser_parse_tentatively (parser);
6993 matching_parens parens;
6994 parens.consume_open (parser);
6996 /* Avoid calling cp_parser_type_id pointlessly, see comment
6997 in cp_parser_cast_expression about c++/29234. */
6998 if (!cp_parser_compound_literal_p (parser))
6999 cp_parser_simulate_error (parser);
7000 else
7002 /* Parse the type. */
7003 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7004 parser->in_type_id_in_expr_p = true;
7005 type = cp_parser_type_id (parser);
7006 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7007 parens.require_close (parser);
7010 /* If things aren't going well, there's no need to
7011 keep going. */
7012 if (!cp_parser_error_occurred (parser))
7014 bool non_constant_p;
7015 /* Parse the brace-enclosed initializer list. */
7016 initializer = cp_parser_braced_list (parser,
7017 &non_constant_p);
7019 /* If that worked, we're definitely looking at a
7020 compound-literal expression. */
7021 if (cp_parser_parse_definitely (parser))
7023 /* Warn the user that a compound literal is not
7024 allowed in standard C++. */
7025 pedwarn (input_location, OPT_Wpedantic,
7026 "ISO C++ forbids compound-literals");
7027 /* For simplicity, we disallow compound literals in
7028 constant-expressions. We could
7029 allow compound literals of integer type, whose
7030 initializer was a constant, in constant
7031 expressions. Permitting that usage, as a further
7032 extension, would not change the meaning of any
7033 currently accepted programs. (Of course, as
7034 compound literals are not part of ISO C++, the
7035 standard has nothing to say.) */
7036 if (cp_parser_non_integral_constant_expression (parser,
7037 NIC_NCC))
7039 postfix_expression = error_mark_node;
7040 break;
7042 /* Form the representation of the compound-literal. */
7043 postfix_expression
7044 = finish_compound_literal (type, initializer,
7045 tf_warning_or_error, fcl_c99);
7046 postfix_expression.set_location (initializer.get_location ());
7047 break;
7051 /* It must be a primary-expression. */
7052 postfix_expression
7053 = cp_parser_primary_expression (parser, address_p, cast_p,
7054 /*template_arg_p=*/false,
7055 decltype_p,
7056 &idk);
7058 break;
7061 /* Note that we don't need to worry about calling build_cplus_new on a
7062 class-valued CALL_EXPR in decltype when it isn't the end of the
7063 postfix-expression; unary_complex_lvalue will take care of that for
7064 all these cases. */
7066 /* Keep looping until the postfix-expression is complete. */
7067 while (true)
7069 if (idk == CP_ID_KIND_UNQUALIFIED
7070 && identifier_p (postfix_expression)
7071 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7072 /* It is not a Koenig lookup function call. */
7073 postfix_expression
7074 = unqualified_name_lookup_error (postfix_expression);
7076 /* Peek at the next token. */
7077 token = cp_lexer_peek_token (parser->lexer);
7079 switch (token->type)
7081 case CPP_OPEN_SQUARE:
7082 if (cp_next_tokens_can_be_std_attribute_p (parser))
7084 cp_parser_error (parser,
7085 "two consecutive %<[%> shall "
7086 "only introduce an attribute");
7087 return error_mark_node;
7089 postfix_expression
7090 = cp_parser_postfix_open_square_expression (parser,
7091 postfix_expression,
7092 false,
7093 decltype_p);
7094 postfix_expression.set_range (start_loc,
7095 postfix_expression.get_location ());
7097 idk = CP_ID_KIND_NONE;
7098 is_member_access = false;
7099 break;
7101 case CPP_OPEN_PAREN:
7102 /* postfix-expression ( expression-list [opt] ) */
7104 bool koenig_p;
7105 bool is_builtin_constant_p;
7106 bool saved_integral_constant_expression_p = false;
7107 bool saved_non_integral_constant_expression_p = false;
7108 tsubst_flags_t complain = complain_flags (decltype_p);
7109 vec<tree, va_gc> *args;
7110 location_t close_paren_loc = UNKNOWN_LOCATION;
7112 is_member_access = false;
7114 is_builtin_constant_p
7115 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7116 if (is_builtin_constant_p)
7118 /* The whole point of __builtin_constant_p is to allow
7119 non-constant expressions to appear as arguments. */
7120 saved_integral_constant_expression_p
7121 = parser->integral_constant_expression_p;
7122 saved_non_integral_constant_expression_p
7123 = parser->non_integral_constant_expression_p;
7124 parser->integral_constant_expression_p = false;
7126 args = (cp_parser_parenthesized_expression_list
7127 (parser, non_attr,
7128 /*cast_p=*/false, /*allow_expansion_p=*/true,
7129 /*non_constant_p=*/NULL,
7130 /*close_paren_loc=*/&close_paren_loc,
7131 /*wrap_locations_p=*/true));
7132 if (is_builtin_constant_p)
7134 parser->integral_constant_expression_p
7135 = saved_integral_constant_expression_p;
7136 parser->non_integral_constant_expression_p
7137 = saved_non_integral_constant_expression_p;
7140 if (args == NULL)
7142 postfix_expression = error_mark_node;
7143 break;
7146 /* Function calls are not permitted in
7147 constant-expressions. */
7148 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7149 && cp_parser_non_integral_constant_expression (parser,
7150 NIC_FUNC_CALL))
7152 postfix_expression = error_mark_node;
7153 release_tree_vector (args);
7154 break;
7157 koenig_p = false;
7158 if (idk == CP_ID_KIND_UNQUALIFIED
7159 || idk == CP_ID_KIND_TEMPLATE_ID)
7161 if (identifier_p (postfix_expression))
7163 if (!args->is_empty ())
7165 koenig_p = true;
7166 if (!any_type_dependent_arguments_p (args))
7167 postfix_expression
7168 = perform_koenig_lookup (postfix_expression, args,
7169 complain);
7171 else
7172 postfix_expression
7173 = unqualified_fn_lookup_error (postfix_expression);
7175 /* We do not perform argument-dependent lookup if
7176 normal lookup finds a non-function, in accordance
7177 with the expected resolution of DR 218. */
7178 else if (!args->is_empty ()
7179 && is_overloaded_fn (postfix_expression))
7181 tree fn = get_first_fn (postfix_expression);
7182 fn = STRIP_TEMPLATE (fn);
7184 /* Do not do argument dependent lookup if regular
7185 lookup finds a member function or a block-scope
7186 function declaration. [basic.lookup.argdep]/3 */
7187 if (!DECL_FUNCTION_MEMBER_P (fn)
7188 && !DECL_LOCAL_FUNCTION_P (fn))
7190 koenig_p = true;
7191 if (!any_type_dependent_arguments_p (args))
7192 postfix_expression
7193 = perform_koenig_lookup (postfix_expression, args,
7194 complain);
7199 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7201 tree instance = TREE_OPERAND (postfix_expression, 0);
7202 tree fn = TREE_OPERAND (postfix_expression, 1);
7204 if (processing_template_decl
7205 && (type_dependent_object_expression_p (instance)
7206 || (!BASELINK_P (fn)
7207 && TREE_CODE (fn) != FIELD_DECL)
7208 || type_dependent_expression_p (fn)
7209 || any_type_dependent_arguments_p (args)))
7211 maybe_generic_this_capture (instance, fn);
7212 postfix_expression
7213 = build_min_nt_call_vec (postfix_expression, args);
7214 release_tree_vector (args);
7215 break;
7218 if (BASELINK_P (fn))
7220 postfix_expression
7221 = (build_new_method_call
7222 (instance, fn, &args, NULL_TREE,
7223 (idk == CP_ID_KIND_QUALIFIED
7224 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7225 : LOOKUP_NORMAL),
7226 /*fn_p=*/NULL,
7227 complain));
7229 else
7230 postfix_expression
7231 = finish_call_expr (postfix_expression, &args,
7232 /*disallow_virtual=*/false,
7233 /*koenig_p=*/false,
7234 complain);
7236 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7237 || TREE_CODE (postfix_expression) == MEMBER_REF
7238 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7239 postfix_expression = (build_offset_ref_call_from_tree
7240 (postfix_expression, &args,
7241 complain));
7242 else if (idk == CP_ID_KIND_QUALIFIED)
7243 /* A call to a static class member, or a namespace-scope
7244 function. */
7245 postfix_expression
7246 = finish_call_expr (postfix_expression, &args,
7247 /*disallow_virtual=*/true,
7248 koenig_p,
7249 complain);
7250 else
7251 /* All other function calls. */
7252 postfix_expression
7253 = finish_call_expr (postfix_expression, &args,
7254 /*disallow_virtual=*/false,
7255 koenig_p,
7256 complain);
7258 if (close_paren_loc != UNKNOWN_LOCATION)
7260 location_t combined_loc = make_location (token->location,
7261 start_loc,
7262 close_paren_loc);
7263 postfix_expression.set_location (combined_loc);
7266 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7267 idk = CP_ID_KIND_NONE;
7269 release_tree_vector (args);
7271 break;
7273 case CPP_DOT:
7274 case CPP_DEREF:
7275 /* postfix-expression . template [opt] id-expression
7276 postfix-expression . pseudo-destructor-name
7277 postfix-expression -> template [opt] id-expression
7278 postfix-expression -> pseudo-destructor-name */
7280 /* Consume the `.' or `->' operator. */
7281 cp_lexer_consume_token (parser->lexer);
7283 postfix_expression
7284 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7285 postfix_expression,
7286 false, &idk, loc);
7288 is_member_access = true;
7289 break;
7291 case CPP_PLUS_PLUS:
7292 /* postfix-expression ++ */
7293 /* Consume the `++' token. */
7294 cp_lexer_consume_token (parser->lexer);
7295 /* Generate a representation for the complete expression. */
7296 postfix_expression
7297 = finish_increment_expr (postfix_expression,
7298 POSTINCREMENT_EXPR);
7299 /* Increments may not appear in constant-expressions. */
7300 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7301 postfix_expression = error_mark_node;
7302 idk = CP_ID_KIND_NONE;
7303 is_member_access = false;
7304 break;
7306 case CPP_MINUS_MINUS:
7307 /* postfix-expression -- */
7308 /* Consume the `--' token. */
7309 cp_lexer_consume_token (parser->lexer);
7310 /* Generate a representation for the complete expression. */
7311 postfix_expression
7312 = finish_increment_expr (postfix_expression,
7313 POSTDECREMENT_EXPR);
7314 /* Decrements may not appear in constant-expressions. */
7315 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7316 postfix_expression = error_mark_node;
7317 idk = CP_ID_KIND_NONE;
7318 is_member_access = false;
7319 break;
7321 default:
7322 if (pidk_return != NULL)
7323 * pidk_return = idk;
7324 if (member_access_only_p)
7325 return is_member_access
7326 ? postfix_expression
7327 : cp_expr (error_mark_node);
7328 else
7329 return postfix_expression;
7333 /* We should never get here. */
7334 gcc_unreachable ();
7335 return error_mark_node;
7338 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7339 by cp_parser_builtin_offsetof. We're looking for
7341 postfix-expression [ expression ]
7342 postfix-expression [ braced-init-list ] (C++11)
7344 FOR_OFFSETOF is set if we're being called in that context, which
7345 changes how we deal with integer constant expressions. */
7347 static tree
7348 cp_parser_postfix_open_square_expression (cp_parser *parser,
7349 tree postfix_expression,
7350 bool for_offsetof,
7351 bool decltype_p)
7353 tree index = NULL_TREE;
7354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7355 bool saved_greater_than_is_operator_p;
7357 /* Consume the `[' token. */
7358 cp_lexer_consume_token (parser->lexer);
7360 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7361 parser->greater_than_is_operator_p = true;
7363 /* Parse the index expression. */
7364 /* ??? For offsetof, there is a question of what to allow here. If
7365 offsetof is not being used in an integral constant expression context,
7366 then we *could* get the right answer by computing the value at runtime.
7367 If we are in an integral constant expression context, then we might
7368 could accept any constant expression; hard to say without analysis.
7369 Rather than open the barn door too wide right away, allow only integer
7370 constant expressions here. */
7371 if (for_offsetof)
7372 index = cp_parser_constant_expression (parser);
7373 else
7375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7377 bool expr_nonconst_p;
7378 cp_lexer_set_source_position (parser->lexer);
7379 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7380 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7382 else
7383 index = cp_parser_expression (parser);
7386 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7388 /* Look for the closing `]'. */
7389 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7391 /* Build the ARRAY_REF. */
7392 postfix_expression = grok_array_decl (loc, postfix_expression,
7393 index, decltype_p);
7395 /* When not doing offsetof, array references are not permitted in
7396 constant-expressions. */
7397 if (!for_offsetof
7398 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7399 postfix_expression = error_mark_node;
7401 return postfix_expression;
7404 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7405 dereference of incomplete type, returns true if error_mark_node should
7406 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7407 and *DEPENDENT_P. */
7409 bool
7410 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7411 bool *dependent_p)
7413 /* In a template, be permissive by treating an object expression
7414 of incomplete type as dependent (after a pedwarn). */
7415 diagnostic_t kind = (processing_template_decl
7416 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7418 switch (TREE_CODE (*postfix_expression))
7420 case CAST_EXPR:
7421 case REINTERPRET_CAST_EXPR:
7422 case CONST_CAST_EXPR:
7423 case STATIC_CAST_EXPR:
7424 case DYNAMIC_CAST_EXPR:
7425 case IMPLICIT_CONV_EXPR:
7426 case VIEW_CONVERT_EXPR:
7427 case NON_LVALUE_EXPR:
7428 kind = DK_ERROR;
7429 break;
7430 case OVERLOAD:
7431 /* Don't emit any diagnostic for OVERLOADs. */
7432 kind = DK_IGNORED;
7433 break;
7434 default:
7435 /* Avoid clobbering e.g. DECLs. */
7436 if (!EXPR_P (*postfix_expression))
7437 kind = DK_ERROR;
7438 break;
7441 if (kind == DK_IGNORED)
7442 return false;
7444 location_t exploc = location_of (*postfix_expression);
7445 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7446 if (!MAYBE_CLASS_TYPE_P (*scope))
7447 return true;
7448 if (kind == DK_ERROR)
7449 *scope = *postfix_expression = error_mark_node;
7450 else if (processing_template_decl)
7452 *dependent_p = true;
7453 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7455 return false;
7458 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7459 by cp_parser_builtin_offsetof. We're looking for
7461 postfix-expression . template [opt] id-expression
7462 postfix-expression . pseudo-destructor-name
7463 postfix-expression -> template [opt] id-expression
7464 postfix-expression -> pseudo-destructor-name
7466 FOR_OFFSETOF is set if we're being called in that context. That sorta
7467 limits what of the above we'll actually accept, but nevermind.
7468 TOKEN_TYPE is the "." or "->" token, which will already have been
7469 removed from the stream. */
7471 static tree
7472 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7473 enum cpp_ttype token_type,
7474 cp_expr postfix_expression,
7475 bool for_offsetof, cp_id_kind *idk,
7476 location_t location)
7478 tree name;
7479 bool dependent_p;
7480 bool pseudo_destructor_p;
7481 tree scope = NULL_TREE;
7482 location_t start_loc = postfix_expression.get_start ();
7484 /* If this is a `->' operator, dereference the pointer. */
7485 if (token_type == CPP_DEREF)
7486 postfix_expression = build_x_arrow (location, postfix_expression,
7487 tf_warning_or_error);
7488 /* Check to see whether or not the expression is type-dependent and
7489 not the current instantiation. */
7490 dependent_p = type_dependent_object_expression_p (postfix_expression);
7491 /* The identifier following the `->' or `.' is not qualified. */
7492 parser->scope = NULL_TREE;
7493 parser->qualifying_scope = NULL_TREE;
7494 parser->object_scope = NULL_TREE;
7495 *idk = CP_ID_KIND_NONE;
7497 /* Enter the scope corresponding to the type of the object
7498 given by the POSTFIX_EXPRESSION. */
7499 if (!dependent_p)
7501 scope = TREE_TYPE (postfix_expression);
7502 /* According to the standard, no expression should ever have
7503 reference type. Unfortunately, we do not currently match
7504 the standard in this respect in that our internal representation
7505 of an expression may have reference type even when the standard
7506 says it does not. Therefore, we have to manually obtain the
7507 underlying type here. */
7508 scope = non_reference (scope);
7509 /* The type of the POSTFIX_EXPRESSION must be complete. */
7510 /* Unlike the object expression in other contexts, *this is not
7511 required to be of complete type for purposes of class member
7512 access (5.2.5) outside the member function body. */
7513 if (postfix_expression != current_class_ref
7514 && scope != error_mark_node
7515 && !currently_open_class (scope))
7517 scope = complete_type (scope);
7518 if (!COMPLETE_TYPE_P (scope)
7519 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7520 &dependent_p))
7521 return error_mark_node;
7524 if (!dependent_p)
7526 /* Let the name lookup machinery know that we are processing a
7527 class member access expression. */
7528 parser->context->object_type = scope;
7529 /* If something went wrong, we want to be able to discern that case,
7530 as opposed to the case where there was no SCOPE due to the type
7531 of expression being dependent. */
7532 if (!scope)
7533 scope = error_mark_node;
7534 /* If the SCOPE was erroneous, make the various semantic analysis
7535 functions exit quickly -- and without issuing additional error
7536 messages. */
7537 if (scope == error_mark_node)
7538 postfix_expression = error_mark_node;
7542 if (dependent_p)
7543 /* Tell cp_parser_lookup_name that there was an object, even though it's
7544 type-dependent. */
7545 parser->context->object_type = unknown_type_node;
7547 /* Assume this expression is not a pseudo-destructor access. */
7548 pseudo_destructor_p = false;
7550 /* If the SCOPE is a scalar type, then, if this is a valid program,
7551 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7552 is type dependent, it can be pseudo-destructor-name or something else.
7553 Try to parse it as pseudo-destructor-name first. */
7554 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7556 tree s;
7557 tree type;
7559 cp_parser_parse_tentatively (parser);
7560 /* Parse the pseudo-destructor-name. */
7561 s = NULL_TREE;
7562 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7563 &s, &type);
7564 if (dependent_p
7565 && (cp_parser_error_occurred (parser)
7566 || !SCALAR_TYPE_P (type)))
7567 cp_parser_abort_tentative_parse (parser);
7568 else if (cp_parser_parse_definitely (parser))
7570 pseudo_destructor_p = true;
7571 postfix_expression
7572 = finish_pseudo_destructor_expr (postfix_expression,
7573 s, type, location);
7577 if (!pseudo_destructor_p)
7579 /* If the SCOPE is not a scalar type, we are looking at an
7580 ordinary class member access expression, rather than a
7581 pseudo-destructor-name. */
7582 bool template_p;
7583 cp_token *token = cp_lexer_peek_token (parser->lexer);
7584 /* Parse the id-expression. */
7585 name = (cp_parser_id_expression
7586 (parser,
7587 cp_parser_optional_template_keyword (parser),
7588 /*check_dependency_p=*/true,
7589 &template_p,
7590 /*declarator_p=*/false,
7591 /*optional_p=*/false));
7592 /* In general, build a SCOPE_REF if the member name is qualified.
7593 However, if the name was not dependent and has already been
7594 resolved; there is no need to build the SCOPE_REF. For example;
7596 struct X { void f(); };
7597 template <typename T> void f(T* t) { t->X::f(); }
7599 Even though "t" is dependent, "X::f" is not and has been resolved
7600 to a BASELINK; there is no need to include scope information. */
7602 /* But we do need to remember that there was an explicit scope for
7603 virtual function calls. */
7604 if (parser->scope)
7605 *idk = CP_ID_KIND_QUALIFIED;
7607 /* If the name is a template-id that names a type, we will get a
7608 TYPE_DECL here. That is invalid code. */
7609 if (TREE_CODE (name) == TYPE_DECL)
7611 error_at (token->location, "invalid use of %qD", name);
7612 postfix_expression = error_mark_node;
7614 else
7616 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7618 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7620 error_at (token->location, "%<%D::%D%> is not a class member",
7621 parser->scope, name);
7622 postfix_expression = error_mark_node;
7624 else
7625 name = build_qualified_name (/*type=*/NULL_TREE,
7626 parser->scope,
7627 name,
7628 template_p);
7629 parser->scope = NULL_TREE;
7630 parser->qualifying_scope = NULL_TREE;
7631 parser->object_scope = NULL_TREE;
7633 if (parser->scope && name && BASELINK_P (name))
7634 adjust_result_of_qualified_name_lookup
7635 (name, parser->scope, scope);
7636 postfix_expression
7637 = finish_class_member_access_expr (postfix_expression, name,
7638 template_p,
7639 tf_warning_or_error);
7640 /* Build a location e.g.:
7641 ptr->access_expr
7642 ~~~^~~~~~~~~~~~~
7643 where the caret is at the deref token, ranging from
7644 the start of postfix_expression to the end of the access expr. */
7645 location_t end_loc
7646 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7647 location_t combined_loc
7648 = make_location (input_location, start_loc, end_loc);
7649 protected_set_expr_location (postfix_expression, combined_loc);
7653 /* We no longer need to look up names in the scope of the object on
7654 the left-hand side of the `.' or `->' operator. */
7655 parser->context->object_type = NULL_TREE;
7657 /* Outside of offsetof, these operators may not appear in
7658 constant-expressions. */
7659 if (!for_offsetof
7660 && (cp_parser_non_integral_constant_expression
7661 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7662 postfix_expression = error_mark_node;
7664 return postfix_expression;
7667 /* Parse a parenthesized expression-list.
7669 expression-list:
7670 assignment-expression
7671 expression-list, assignment-expression
7673 attribute-list:
7674 expression-list
7675 identifier
7676 identifier, expression-list
7678 CAST_P is true if this expression is the target of a cast.
7680 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7681 argument pack.
7683 WRAP_LOCATIONS_P is true if expressions within this list for which
7684 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7685 their source locations.
7687 Returns a vector of trees. Each element is a representation of an
7688 assignment-expression. NULL is returned if the ( and or ) are
7689 missing. An empty, but allocated, vector is returned on no
7690 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7691 if we are parsing an attribute list for an attribute that wants a
7692 plain identifier argument, normal_attr for an attribute that wants
7693 an expression, or non_attr if we aren't parsing an attribute list. If
7694 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7695 not all of the expressions in the list were constant.
7696 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7697 will be written to with the location of the closing parenthesis. If
7698 an error occurs, it may or may not be written to. */
7700 static vec<tree, va_gc> *
7701 cp_parser_parenthesized_expression_list (cp_parser* parser,
7702 int is_attribute_list,
7703 bool cast_p,
7704 bool allow_expansion_p,
7705 bool *non_constant_p,
7706 location_t *close_paren_loc,
7707 bool wrap_locations_p)
7709 vec<tree, va_gc> *expression_list;
7710 bool fold_expr_p = is_attribute_list != non_attr;
7711 tree identifier = NULL_TREE;
7712 bool saved_greater_than_is_operator_p;
7714 /* Assume all the expressions will be constant. */
7715 if (non_constant_p)
7716 *non_constant_p = false;
7718 matching_parens parens;
7719 if (!parens.require_open (parser))
7720 return NULL;
7722 expression_list = make_tree_vector ();
7724 /* Within a parenthesized expression, a `>' token is always
7725 the greater-than operator. */
7726 saved_greater_than_is_operator_p
7727 = parser->greater_than_is_operator_p;
7728 parser->greater_than_is_operator_p = true;
7730 cp_expr expr (NULL_TREE);
7732 /* Consume expressions until there are no more. */
7733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7734 while (true)
7736 /* At the beginning of attribute lists, check to see if the
7737 next token is an identifier. */
7738 if (is_attribute_list == id_attr
7739 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7741 cp_token *token;
7743 /* Consume the identifier. */
7744 token = cp_lexer_consume_token (parser->lexer);
7745 /* Save the identifier. */
7746 identifier = token->u.value;
7748 else
7750 bool expr_non_constant_p;
7752 /* Parse the next assignment-expression. */
7753 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7755 /* A braced-init-list. */
7756 cp_lexer_set_source_position (parser->lexer);
7757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7758 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7759 if (non_constant_p && expr_non_constant_p)
7760 *non_constant_p = true;
7762 else if (non_constant_p)
7764 expr = (cp_parser_constant_expression
7765 (parser, /*allow_non_constant_p=*/true,
7766 &expr_non_constant_p));
7767 if (expr_non_constant_p)
7768 *non_constant_p = true;
7770 else
7771 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7772 cast_p);
7774 if (fold_expr_p)
7775 expr = instantiate_non_dependent_expr (expr);
7777 /* If we have an ellipsis, then this is an expression
7778 expansion. */
7779 if (allow_expansion_p
7780 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7782 /* Consume the `...'. */
7783 cp_lexer_consume_token (parser->lexer);
7785 /* Build the argument pack. */
7786 expr = make_pack_expansion (expr);
7789 if (wrap_locations_p)
7790 expr.maybe_add_location_wrapper ();
7792 /* Add it to the list. We add error_mark_node
7793 expressions to the list, so that we can still tell if
7794 the correct form for a parenthesized expression-list
7795 is found. That gives better errors. */
7796 vec_safe_push (expression_list, expr.get_value ());
7798 if (expr == error_mark_node)
7799 goto skip_comma;
7802 /* After the first item, attribute lists look the same as
7803 expression lists. */
7804 is_attribute_list = non_attr;
7806 get_comma:;
7807 /* If the next token isn't a `,', then we are done. */
7808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7809 break;
7811 /* Otherwise, consume the `,' and keep going. */
7812 cp_lexer_consume_token (parser->lexer);
7815 if (close_paren_loc)
7816 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7818 if (!parens.require_close (parser))
7820 int ending;
7822 skip_comma:;
7823 /* We try and resync to an unnested comma, as that will give the
7824 user better diagnostics. */
7825 ending = cp_parser_skip_to_closing_parenthesis (parser,
7826 /*recovering=*/true,
7827 /*or_comma=*/true,
7828 /*consume_paren=*/true);
7829 if (ending < 0)
7830 goto get_comma;
7831 if (!ending)
7833 parser->greater_than_is_operator_p
7834 = saved_greater_than_is_operator_p;
7835 return NULL;
7839 parser->greater_than_is_operator_p
7840 = saved_greater_than_is_operator_p;
7842 if (identifier)
7843 vec_safe_insert (expression_list, 0, identifier);
7845 return expression_list;
7848 /* Parse a pseudo-destructor-name.
7850 pseudo-destructor-name:
7851 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7852 :: [opt] nested-name-specifier template template-id :: ~ type-name
7853 :: [opt] nested-name-specifier [opt] ~ type-name
7855 If either of the first two productions is used, sets *SCOPE to the
7856 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7857 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7858 or ERROR_MARK_NODE if the parse fails. */
7860 static void
7861 cp_parser_pseudo_destructor_name (cp_parser* parser,
7862 tree object,
7863 tree* scope,
7864 tree* type)
7866 bool nested_name_specifier_p;
7868 /* Handle ~auto. */
7869 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7870 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7871 && !type_dependent_expression_p (object))
7873 if (cxx_dialect < cxx14)
7874 pedwarn (input_location, 0,
7875 "%<~auto%> only available with "
7876 "-std=c++14 or -std=gnu++14");
7877 cp_lexer_consume_token (parser->lexer);
7878 cp_lexer_consume_token (parser->lexer);
7879 *scope = NULL_TREE;
7880 *type = TREE_TYPE (object);
7881 return;
7884 /* Assume that things will not work out. */
7885 *type = error_mark_node;
7887 /* Look for the optional `::' operator. */
7888 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7889 /* Look for the optional nested-name-specifier. */
7890 nested_name_specifier_p
7891 = (cp_parser_nested_name_specifier_opt (parser,
7892 /*typename_keyword_p=*/false,
7893 /*check_dependency_p=*/true,
7894 /*type_p=*/false,
7895 /*is_declaration=*/false)
7896 != NULL_TREE);
7897 /* Now, if we saw a nested-name-specifier, we might be doing the
7898 second production. */
7899 if (nested_name_specifier_p
7900 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7902 /* Consume the `template' keyword. */
7903 cp_lexer_consume_token (parser->lexer);
7904 /* Parse the template-id. */
7905 cp_parser_template_id (parser,
7906 /*template_keyword_p=*/true,
7907 /*check_dependency_p=*/false,
7908 class_type,
7909 /*is_declaration=*/true);
7910 /* Look for the `::' token. */
7911 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7913 /* If the next token is not a `~', then there might be some
7914 additional qualification. */
7915 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7917 /* At this point, we're looking for "type-name :: ~". The type-name
7918 must not be a class-name, since this is a pseudo-destructor. So,
7919 it must be either an enum-name, or a typedef-name -- both of which
7920 are just identifiers. So, we peek ahead to check that the "::"
7921 and "~" tokens are present; if they are not, then we can avoid
7922 calling type_name. */
7923 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7924 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7925 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7927 cp_parser_error (parser, "non-scalar type");
7928 return;
7931 /* Look for the type-name. */
7932 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7933 if (*scope == error_mark_node)
7934 return;
7936 /* Look for the `::' token. */
7937 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7939 else
7940 *scope = NULL_TREE;
7942 /* Look for the `~'. */
7943 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7945 /* Once we see the ~, this has to be a pseudo-destructor. */
7946 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7947 cp_parser_commit_to_topmost_tentative_parse (parser);
7949 /* Look for the type-name again. We are not responsible for
7950 checking that it matches the first type-name. */
7951 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7954 /* Parse a unary-expression.
7956 unary-expression:
7957 postfix-expression
7958 ++ cast-expression
7959 -- cast-expression
7960 unary-operator cast-expression
7961 sizeof unary-expression
7962 sizeof ( type-id )
7963 alignof ( type-id ) [C++0x]
7964 new-expression
7965 delete-expression
7967 GNU Extensions:
7969 unary-expression:
7970 __extension__ cast-expression
7971 __alignof__ unary-expression
7972 __alignof__ ( type-id )
7973 alignof unary-expression [C++0x]
7974 __real__ cast-expression
7975 __imag__ cast-expression
7976 && identifier
7977 sizeof ( type-id ) { initializer-list , [opt] }
7978 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7979 __alignof__ ( type-id ) { initializer-list , [opt] }
7981 ADDRESS_P is true iff the unary-expression is appearing as the
7982 operand of the `&' operator. CAST_P is true if this expression is
7983 the target of a cast.
7985 Returns a representation of the expression. */
7987 static cp_expr
7988 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7989 bool address_p, bool cast_p, bool decltype_p)
7991 cp_token *token;
7992 enum tree_code unary_operator;
7994 /* Peek at the next token. */
7995 token = cp_lexer_peek_token (parser->lexer);
7996 /* Some keywords give away the kind of expression. */
7997 if (token->type == CPP_KEYWORD)
7999 enum rid keyword = token->keyword;
8001 switch (keyword)
8003 case RID_ALIGNOF:
8004 case RID_SIZEOF:
8006 tree operand, ret;
8007 enum tree_code op;
8008 location_t start_loc = token->location;
8010 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8011 bool std_alignof = id_equal (token->u.value, "alignof");
8013 /* Consume the token. */
8014 cp_lexer_consume_token (parser->lexer);
8015 /* Parse the operand. */
8016 operand = cp_parser_sizeof_operand (parser, keyword);
8018 if (TYPE_P (operand))
8019 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8020 true);
8021 else
8023 /* ISO C++ defines alignof only with types, not with
8024 expressions. So pedwarn if alignof is used with a non-
8025 type expression. However, __alignof__ is ok. */
8026 if (std_alignof)
8027 pedwarn (token->location, OPT_Wpedantic,
8028 "ISO C++ does not allow %<alignof%> "
8029 "with a non-type");
8031 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8033 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8034 SIZEOF_EXPR with the original operand. */
8035 if (op == SIZEOF_EXPR && ret != error_mark_node)
8037 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8039 if (!processing_template_decl && TYPE_P (operand))
8041 ret = build_min (SIZEOF_EXPR, size_type_node,
8042 build1 (NOP_EXPR, operand,
8043 error_mark_node));
8044 SIZEOF_EXPR_TYPE_P (ret) = 1;
8046 else
8047 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8048 TREE_SIDE_EFFECTS (ret) = 0;
8049 TREE_READONLY (ret) = 1;
8053 /* Construct a location e.g. :
8054 alignof (expr)
8055 ^~~~~~~~~~~~~~
8056 with start == caret at the start of the "alignof"/"sizeof"
8057 token, with the endpoint at the final closing paren. */
8058 location_t finish_loc
8059 = cp_lexer_previous_token (parser->lexer)->location;
8060 location_t compound_loc
8061 = make_location (start_loc, start_loc, finish_loc);
8063 cp_expr ret_expr (ret);
8064 ret_expr.set_location (compound_loc);
8065 ret_expr = ret_expr.maybe_add_location_wrapper ();
8066 return ret_expr;
8069 case RID_NEW:
8070 return cp_parser_new_expression (parser);
8072 case RID_DELETE:
8073 return cp_parser_delete_expression (parser);
8075 case RID_EXTENSION:
8077 /* The saved value of the PEDANTIC flag. */
8078 int saved_pedantic;
8079 tree expr;
8081 /* Save away the PEDANTIC flag. */
8082 cp_parser_extension_opt (parser, &saved_pedantic);
8083 /* Parse the cast-expression. */
8084 expr = cp_parser_simple_cast_expression (parser);
8085 /* Restore the PEDANTIC flag. */
8086 pedantic = saved_pedantic;
8088 return expr;
8091 case RID_REALPART:
8092 case RID_IMAGPART:
8094 tree expression;
8096 /* Consume the `__real__' or `__imag__' token. */
8097 cp_lexer_consume_token (parser->lexer);
8098 /* Parse the cast-expression. */
8099 expression = cp_parser_simple_cast_expression (parser);
8100 /* Create the complete representation. */
8101 return build_x_unary_op (token->location,
8102 (keyword == RID_REALPART
8103 ? REALPART_EXPR : IMAGPART_EXPR),
8104 expression,
8105 tf_warning_or_error);
8107 break;
8109 case RID_TRANSACTION_ATOMIC:
8110 case RID_TRANSACTION_RELAXED:
8111 return cp_parser_transaction_expression (parser, keyword);
8113 case RID_NOEXCEPT:
8115 tree expr;
8116 const char *saved_message;
8117 bool saved_integral_constant_expression_p;
8118 bool saved_non_integral_constant_expression_p;
8119 bool saved_greater_than_is_operator_p;
8121 location_t start_loc = token->location;
8123 cp_lexer_consume_token (parser->lexer);
8124 matching_parens parens;
8125 parens.require_open (parser);
8127 saved_message = parser->type_definition_forbidden_message;
8128 parser->type_definition_forbidden_message
8129 = G_("types may not be defined in %<noexcept%> expressions");
8131 saved_integral_constant_expression_p
8132 = parser->integral_constant_expression_p;
8133 saved_non_integral_constant_expression_p
8134 = parser->non_integral_constant_expression_p;
8135 parser->integral_constant_expression_p = false;
8137 saved_greater_than_is_operator_p
8138 = parser->greater_than_is_operator_p;
8139 parser->greater_than_is_operator_p = true;
8141 ++cp_unevaluated_operand;
8142 ++c_inhibit_evaluation_warnings;
8143 ++cp_noexcept_operand;
8144 expr = cp_parser_expression (parser);
8145 --cp_noexcept_operand;
8146 --c_inhibit_evaluation_warnings;
8147 --cp_unevaluated_operand;
8149 parser->greater_than_is_operator_p
8150 = saved_greater_than_is_operator_p;
8152 parser->integral_constant_expression_p
8153 = saved_integral_constant_expression_p;
8154 parser->non_integral_constant_expression_p
8155 = saved_non_integral_constant_expression_p;
8157 parser->type_definition_forbidden_message = saved_message;
8159 location_t finish_loc
8160 = cp_lexer_peek_token (parser->lexer)->location;
8161 parens.require_close (parser);
8163 /* Construct a location of the form:
8164 noexcept (expr)
8165 ^~~~~~~~~~~~~~~
8166 with start == caret, finishing at the close-paren. */
8167 location_t noexcept_loc
8168 = make_location (start_loc, start_loc, finish_loc);
8170 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8171 noexcept_loc);
8174 default:
8175 break;
8179 /* Look for the `:: new' and `:: delete', which also signal the
8180 beginning of a new-expression, or delete-expression,
8181 respectively. If the next token is `::', then it might be one of
8182 these. */
8183 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8185 enum rid keyword;
8187 /* See if the token after the `::' is one of the keywords in
8188 which we're interested. */
8189 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8190 /* If it's `new', we have a new-expression. */
8191 if (keyword == RID_NEW)
8192 return cp_parser_new_expression (parser);
8193 /* Similarly, for `delete'. */
8194 else if (keyword == RID_DELETE)
8195 return cp_parser_delete_expression (parser);
8198 /* Look for a unary operator. */
8199 unary_operator = cp_parser_unary_operator (token);
8200 /* The `++' and `--' operators can be handled similarly, even though
8201 they are not technically unary-operators in the grammar. */
8202 if (unary_operator == ERROR_MARK)
8204 if (token->type == CPP_PLUS_PLUS)
8205 unary_operator = PREINCREMENT_EXPR;
8206 else if (token->type == CPP_MINUS_MINUS)
8207 unary_operator = PREDECREMENT_EXPR;
8208 /* Handle the GNU address-of-label extension. */
8209 else if (cp_parser_allow_gnu_extensions_p (parser)
8210 && token->type == CPP_AND_AND)
8212 tree identifier;
8213 tree expression;
8214 location_t start_loc = token->location;
8216 /* Consume the '&&' token. */
8217 cp_lexer_consume_token (parser->lexer);
8218 /* Look for the identifier. */
8219 location_t finish_loc
8220 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8221 identifier = cp_parser_identifier (parser);
8222 /* Construct a location of the form:
8223 &&label
8224 ^~~~~~~
8225 with caret==start at the "&&", finish at the end of the label. */
8226 location_t combined_loc
8227 = make_location (start_loc, start_loc, finish_loc);
8228 /* Create an expression representing the address. */
8229 expression = finish_label_address_expr (identifier, combined_loc);
8230 if (cp_parser_non_integral_constant_expression (parser,
8231 NIC_ADDR_LABEL))
8232 expression = error_mark_node;
8233 return expression;
8236 if (unary_operator != ERROR_MARK)
8238 cp_expr cast_expression;
8239 cp_expr expression = error_mark_node;
8240 non_integral_constant non_constant_p = NIC_NONE;
8241 location_t loc = token->location;
8242 tsubst_flags_t complain = complain_flags (decltype_p);
8244 /* Consume the operator token. */
8245 token = cp_lexer_consume_token (parser->lexer);
8246 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8248 /* Parse the cast-expression. */
8249 cast_expression
8250 = cp_parser_cast_expression (parser,
8251 unary_operator == ADDR_EXPR,
8252 /*cast_p=*/false,
8253 /*decltype*/false,
8254 pidk);
8256 /* Make a location:
8257 OP_TOKEN CAST_EXPRESSION
8258 ^~~~~~~~~~~~~~~~~~~~~~~~~
8259 with start==caret at the operator token, and
8260 extending to the end of the cast_expression. */
8261 loc = make_location (loc, loc, cast_expression.get_finish ());
8263 /* Now, build an appropriate representation. */
8264 switch (unary_operator)
8266 case INDIRECT_REF:
8267 non_constant_p = NIC_STAR;
8268 expression = build_x_indirect_ref (loc, cast_expression,
8269 RO_UNARY_STAR,
8270 complain);
8271 /* TODO: build_x_indirect_ref does not always honor the
8272 location, so ensure it is set. */
8273 expression.set_location (loc);
8274 break;
8276 case ADDR_EXPR:
8277 non_constant_p = NIC_ADDR;
8278 /* Fall through. */
8279 case BIT_NOT_EXPR:
8280 expression = build_x_unary_op (loc, unary_operator,
8281 cast_expression,
8282 complain);
8283 /* TODO: build_x_unary_op does not always honor the location,
8284 so ensure it is set. */
8285 expression.set_location (loc);
8286 break;
8288 case PREINCREMENT_EXPR:
8289 case PREDECREMENT_EXPR:
8290 non_constant_p = unary_operator == PREINCREMENT_EXPR
8291 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8292 /* Fall through. */
8293 case NEGATE_EXPR:
8294 /* Immediately fold negation of a constant, unless the constant is 0
8295 (since -0 == 0) or it would overflow. */
8296 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8297 && CONSTANT_CLASS_P (cast_expression)
8298 && !integer_zerop (cast_expression)
8299 && !TREE_OVERFLOW (cast_expression))
8301 tree folded = fold_build1 (unary_operator,
8302 TREE_TYPE (cast_expression),
8303 cast_expression);
8304 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8306 expression = cp_expr (folded, loc);
8307 break;
8310 /* Fall through. */
8311 case UNARY_PLUS_EXPR:
8312 case TRUTH_NOT_EXPR:
8313 expression = finish_unary_op_expr (loc, unary_operator,
8314 cast_expression, complain);
8315 break;
8317 default:
8318 gcc_unreachable ();
8321 if (non_constant_p != NIC_NONE
8322 && cp_parser_non_integral_constant_expression (parser,
8323 non_constant_p))
8324 expression = error_mark_node;
8326 return expression;
8329 return cp_parser_postfix_expression (parser, address_p, cast_p,
8330 /*member_access_only_p=*/false,
8331 decltype_p,
8332 pidk);
8335 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8336 unary-operator, the corresponding tree code is returned. */
8338 static enum tree_code
8339 cp_parser_unary_operator (cp_token* token)
8341 switch (token->type)
8343 case CPP_MULT:
8344 return INDIRECT_REF;
8346 case CPP_AND:
8347 return ADDR_EXPR;
8349 case CPP_PLUS:
8350 return UNARY_PLUS_EXPR;
8352 case CPP_MINUS:
8353 return NEGATE_EXPR;
8355 case CPP_NOT:
8356 return TRUTH_NOT_EXPR;
8358 case CPP_COMPL:
8359 return BIT_NOT_EXPR;
8361 default:
8362 return ERROR_MARK;
8366 /* Parse a new-expression.
8368 new-expression:
8369 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8370 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8372 Returns a representation of the expression. */
8374 static tree
8375 cp_parser_new_expression (cp_parser* parser)
8377 bool global_scope_p;
8378 vec<tree, va_gc> *placement;
8379 tree type;
8380 vec<tree, va_gc> *initializer;
8381 tree nelts = NULL_TREE;
8382 tree ret;
8384 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8386 /* Look for the optional `::' operator. */
8387 global_scope_p
8388 = (cp_parser_global_scope_opt (parser,
8389 /*current_scope_valid_p=*/false)
8390 != NULL_TREE);
8391 /* Look for the `new' operator. */
8392 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8393 /* There's no easy way to tell a new-placement from the
8394 `( type-id )' construct. */
8395 cp_parser_parse_tentatively (parser);
8396 /* Look for a new-placement. */
8397 placement = cp_parser_new_placement (parser);
8398 /* If that didn't work out, there's no new-placement. */
8399 if (!cp_parser_parse_definitely (parser))
8401 if (placement != NULL)
8402 release_tree_vector (placement);
8403 placement = NULL;
8406 /* If the next token is a `(', then we have a parenthesized
8407 type-id. */
8408 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8410 cp_token *token;
8411 const char *saved_message = parser->type_definition_forbidden_message;
8413 /* Consume the `('. */
8414 matching_parens parens;
8415 parens.consume_open (parser);
8417 /* Parse the type-id. */
8418 parser->type_definition_forbidden_message
8419 = G_("types may not be defined in a new-expression");
8421 type_id_in_expr_sentinel s (parser);
8422 type = cp_parser_type_id (parser);
8424 parser->type_definition_forbidden_message = saved_message;
8426 /* Look for the closing `)'. */
8427 parens.require_close (parser);
8428 token = cp_lexer_peek_token (parser->lexer);
8429 /* There should not be a direct-new-declarator in this production,
8430 but GCC used to allowed this, so we check and emit a sensible error
8431 message for this case. */
8432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8434 error_at (token->location,
8435 "array bound forbidden after parenthesized type-id");
8436 inform (token->location,
8437 "try removing the parentheses around the type-id");
8438 cp_parser_direct_new_declarator (parser);
8441 /* Otherwise, there must be a new-type-id. */
8442 else
8443 type = cp_parser_new_type_id (parser, &nelts);
8445 /* If the next token is a `(' or '{', then we have a new-initializer. */
8446 cp_token *token = cp_lexer_peek_token (parser->lexer);
8447 if (token->type == CPP_OPEN_PAREN
8448 || token->type == CPP_OPEN_BRACE)
8449 initializer = cp_parser_new_initializer (parser);
8450 else
8451 initializer = NULL;
8453 /* A new-expression may not appear in an integral constant
8454 expression. */
8455 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8456 ret = error_mark_node;
8457 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8458 of a new-type-id or type-id of a new-expression, the new-expression shall
8459 contain a new-initializer of the form ( assignment-expression )".
8460 Additionally, consistently with the spirit of DR 1467, we want to accept
8461 'new auto { 2 }' too. */
8462 else if ((ret = type_uses_auto (type))
8463 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8464 && (vec_safe_length (initializer) != 1
8465 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8466 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8468 error_at (token->location,
8469 "initialization of new-expression for type %<auto%> "
8470 "requires exactly one element");
8471 ret = error_mark_node;
8473 else
8475 /* Construct a location e.g.:
8476 ptr = new int[100]
8477 ^~~~~~~~~~~~
8478 with caret == start at the start of the "new" token, and the end
8479 at the end of the final token we consumed. */
8480 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8481 location_t end_loc = get_finish (end_tok->location);
8482 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8484 /* Create a representation of the new-expression. */
8485 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8486 tf_warning_or_error);
8487 protected_set_expr_location (ret, combined_loc);
8490 if (placement != NULL)
8491 release_tree_vector (placement);
8492 if (initializer != NULL)
8493 release_tree_vector (initializer);
8495 return ret;
8498 /* Parse a new-placement.
8500 new-placement:
8501 ( expression-list )
8503 Returns the same representation as for an expression-list. */
8505 static vec<tree, va_gc> *
8506 cp_parser_new_placement (cp_parser* parser)
8508 vec<tree, va_gc> *expression_list;
8510 /* Parse the expression-list. */
8511 expression_list = (cp_parser_parenthesized_expression_list
8512 (parser, non_attr, /*cast_p=*/false,
8513 /*allow_expansion_p=*/true,
8514 /*non_constant_p=*/NULL));
8516 if (expression_list && expression_list->is_empty ())
8517 error ("expected expression-list or type-id");
8519 return expression_list;
8522 /* Parse a new-type-id.
8524 new-type-id:
8525 type-specifier-seq new-declarator [opt]
8527 Returns the TYPE allocated. If the new-type-id indicates an array
8528 type, *NELTS is set to the number of elements in the last array
8529 bound; the TYPE will not include the last array bound. */
8531 static tree
8532 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8534 cp_decl_specifier_seq type_specifier_seq;
8535 cp_declarator *new_declarator;
8536 cp_declarator *declarator;
8537 cp_declarator *outer_declarator;
8538 const char *saved_message;
8540 /* The type-specifier sequence must not contain type definitions.
8541 (It cannot contain declarations of new types either, but if they
8542 are not definitions we will catch that because they are not
8543 complete.) */
8544 saved_message = parser->type_definition_forbidden_message;
8545 parser->type_definition_forbidden_message
8546 = G_("types may not be defined in a new-type-id");
8547 /* Parse the type-specifier-seq. */
8548 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8549 /*is_trailing_return=*/false,
8550 &type_specifier_seq);
8551 /* Restore the old message. */
8552 parser->type_definition_forbidden_message = saved_message;
8554 if (type_specifier_seq.type == error_mark_node)
8555 return error_mark_node;
8557 /* Parse the new-declarator. */
8558 new_declarator = cp_parser_new_declarator_opt (parser);
8560 /* Determine the number of elements in the last array dimension, if
8561 any. */
8562 *nelts = NULL_TREE;
8563 /* Skip down to the last array dimension. */
8564 declarator = new_declarator;
8565 outer_declarator = NULL;
8566 while (declarator && (declarator->kind == cdk_pointer
8567 || declarator->kind == cdk_ptrmem))
8569 outer_declarator = declarator;
8570 declarator = declarator->declarator;
8572 while (declarator
8573 && declarator->kind == cdk_array
8574 && declarator->declarator
8575 && declarator->declarator->kind == cdk_array)
8577 outer_declarator = declarator;
8578 declarator = declarator->declarator;
8581 if (declarator && declarator->kind == cdk_array)
8583 *nelts = declarator->u.array.bounds;
8584 if (*nelts == error_mark_node)
8585 *nelts = integer_one_node;
8587 if (outer_declarator)
8588 outer_declarator->declarator = declarator->declarator;
8589 else
8590 new_declarator = NULL;
8593 return groktypename (&type_specifier_seq, new_declarator, false);
8596 /* Parse an (optional) new-declarator.
8598 new-declarator:
8599 ptr-operator new-declarator [opt]
8600 direct-new-declarator
8602 Returns the declarator. */
8604 static cp_declarator *
8605 cp_parser_new_declarator_opt (cp_parser* parser)
8607 enum tree_code code;
8608 tree type, std_attributes = NULL_TREE;
8609 cp_cv_quals cv_quals;
8611 /* We don't know if there's a ptr-operator next, or not. */
8612 cp_parser_parse_tentatively (parser);
8613 /* Look for a ptr-operator. */
8614 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8615 /* If that worked, look for more new-declarators. */
8616 if (cp_parser_parse_definitely (parser))
8618 cp_declarator *declarator;
8620 /* Parse another optional declarator. */
8621 declarator = cp_parser_new_declarator_opt (parser);
8623 declarator = cp_parser_make_indirect_declarator
8624 (code, type, cv_quals, declarator, std_attributes);
8626 return declarator;
8629 /* If the next token is a `[', there is a direct-new-declarator. */
8630 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8631 return cp_parser_direct_new_declarator (parser);
8633 return NULL;
8636 /* Parse a direct-new-declarator.
8638 direct-new-declarator:
8639 [ expression ]
8640 direct-new-declarator [constant-expression]
8644 static cp_declarator *
8645 cp_parser_direct_new_declarator (cp_parser* parser)
8647 cp_declarator *declarator = NULL;
8649 while (true)
8651 tree expression;
8652 cp_token *token;
8654 /* Look for the opening `['. */
8655 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8657 token = cp_lexer_peek_token (parser->lexer);
8658 expression = cp_parser_expression (parser);
8659 /* The standard requires that the expression have integral
8660 type. DR 74 adds enumeration types. We believe that the
8661 real intent is that these expressions be handled like the
8662 expression in a `switch' condition, which also allows
8663 classes with a single conversion to integral or
8664 enumeration type. */
8665 if (!processing_template_decl)
8667 expression
8668 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8669 expression,
8670 /*complain=*/true);
8671 if (!expression)
8673 error_at (token->location,
8674 "expression in new-declarator must have integral "
8675 "or enumeration type");
8676 expression = error_mark_node;
8680 /* Look for the closing `]'. */
8681 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8683 /* Add this bound to the declarator. */
8684 declarator = make_array_declarator (declarator, expression);
8686 /* If the next token is not a `[', then there are no more
8687 bounds. */
8688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8689 break;
8692 return declarator;
8695 /* Parse a new-initializer.
8697 new-initializer:
8698 ( expression-list [opt] )
8699 braced-init-list
8701 Returns a representation of the expression-list. */
8703 static vec<tree, va_gc> *
8704 cp_parser_new_initializer (cp_parser* parser)
8706 vec<tree, va_gc> *expression_list;
8708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8710 tree t;
8711 bool expr_non_constant_p;
8712 cp_lexer_set_source_position (parser->lexer);
8713 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8714 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8715 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8716 expression_list = make_tree_vector_single (t);
8718 else
8719 expression_list = (cp_parser_parenthesized_expression_list
8720 (parser, non_attr, /*cast_p=*/false,
8721 /*allow_expansion_p=*/true,
8722 /*non_constant_p=*/NULL));
8724 return expression_list;
8727 /* Parse a delete-expression.
8729 delete-expression:
8730 :: [opt] delete cast-expression
8731 :: [opt] delete [ ] cast-expression
8733 Returns a representation of the expression. */
8735 static tree
8736 cp_parser_delete_expression (cp_parser* parser)
8738 bool global_scope_p;
8739 bool array_p;
8740 tree expression;
8742 /* Look for the optional `::' operator. */
8743 global_scope_p
8744 = (cp_parser_global_scope_opt (parser,
8745 /*current_scope_valid_p=*/false)
8746 != NULL_TREE);
8747 /* Look for the `delete' keyword. */
8748 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8749 /* See if the array syntax is in use. */
8750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8752 /* Consume the `[' token. */
8753 cp_lexer_consume_token (parser->lexer);
8754 /* Look for the `]' token. */
8755 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8756 /* Remember that this is the `[]' construct. */
8757 array_p = true;
8759 else
8760 array_p = false;
8762 /* Parse the cast-expression. */
8763 expression = cp_parser_simple_cast_expression (parser);
8765 /* A delete-expression may not appear in an integral constant
8766 expression. */
8767 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8768 return error_mark_node;
8770 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8771 tf_warning_or_error);
8774 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8775 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8776 0 otherwise. */
8778 static int
8779 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8781 cp_token *token = cp_lexer_peek_token (parser->lexer);
8782 switch (token->type)
8784 case CPP_COMMA:
8785 case CPP_SEMICOLON:
8786 case CPP_QUERY:
8787 case CPP_COLON:
8788 case CPP_CLOSE_SQUARE:
8789 case CPP_CLOSE_PAREN:
8790 case CPP_CLOSE_BRACE:
8791 case CPP_OPEN_BRACE:
8792 case CPP_DOT:
8793 case CPP_DOT_STAR:
8794 case CPP_DEREF:
8795 case CPP_DEREF_STAR:
8796 case CPP_DIV:
8797 case CPP_MOD:
8798 case CPP_LSHIFT:
8799 case CPP_RSHIFT:
8800 case CPP_LESS:
8801 case CPP_GREATER:
8802 case CPP_LESS_EQ:
8803 case CPP_GREATER_EQ:
8804 case CPP_EQ_EQ:
8805 case CPP_NOT_EQ:
8806 case CPP_EQ:
8807 case CPP_MULT_EQ:
8808 case CPP_DIV_EQ:
8809 case CPP_MOD_EQ:
8810 case CPP_PLUS_EQ:
8811 case CPP_MINUS_EQ:
8812 case CPP_RSHIFT_EQ:
8813 case CPP_LSHIFT_EQ:
8814 case CPP_AND_EQ:
8815 case CPP_XOR_EQ:
8816 case CPP_OR_EQ:
8817 case CPP_XOR:
8818 case CPP_OR:
8819 case CPP_OR_OR:
8820 case CPP_EOF:
8821 case CPP_ELLIPSIS:
8822 return 0;
8824 case CPP_OPEN_PAREN:
8825 /* In ((type ()) () the last () isn't a valid cast-expression,
8826 so the whole must be parsed as postfix-expression. */
8827 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8828 != CPP_CLOSE_PAREN;
8830 case CPP_OPEN_SQUARE:
8831 /* '[' may start a primary-expression in obj-c++ and in C++11,
8832 as a lambda-expression, eg, '(void)[]{}'. */
8833 if (cxx_dialect >= cxx11)
8834 return -1;
8835 return c_dialect_objc ();
8837 case CPP_PLUS_PLUS:
8838 case CPP_MINUS_MINUS:
8839 /* '++' and '--' may or may not start a cast-expression:
8841 struct T { void operator++(int); };
8842 void f() { (T())++; }
8846 int a;
8847 (int)++a; */
8848 return -1;
8850 default:
8851 return 1;
8855 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8856 in the order: const_cast, static_cast, reinterpret_cast.
8858 Don't suggest dynamic_cast.
8860 Return the first legal cast kind found, or NULL otherwise. */
8862 static const char *
8863 get_cast_suggestion (tree dst_type, tree orig_expr)
8865 tree trial;
8867 /* Reuse the parser logic by attempting to build the various kinds of
8868 cast, with "complain" disabled.
8869 Identify the first such cast that is valid. */
8871 /* Don't attempt to run such logic within template processing. */
8872 if (processing_template_decl)
8873 return NULL;
8875 /* First try const_cast. */
8876 trial = build_const_cast (dst_type, orig_expr, tf_none);
8877 if (trial != error_mark_node)
8878 return "const_cast";
8880 /* If that fails, try static_cast. */
8881 trial = build_static_cast (dst_type, orig_expr, tf_none);
8882 if (trial != error_mark_node)
8883 return "static_cast";
8885 /* Finally, try reinterpret_cast. */
8886 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8887 if (trial != error_mark_node)
8888 return "reinterpret_cast";
8890 /* No such cast possible. */
8891 return NULL;
8894 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8895 suggesting how to convert a C-style cast of the form:
8897 (DST_TYPE)ORIG_EXPR
8899 to a C++-style cast.
8901 The primary range of RICHLOC is asssumed to be that of the original
8902 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8903 of the parens in the C-style cast. */
8905 static void
8906 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8907 location_t close_paren_loc, tree orig_expr,
8908 tree dst_type)
8910 /* This function is non-trivial, so bail out now if the warning isn't
8911 going to be emitted. */
8912 if (!warn_old_style_cast)
8913 return;
8915 /* Try to find a legal C++ cast, trying them in order:
8916 const_cast, static_cast, reinterpret_cast. */
8917 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8918 if (!cast_suggestion)
8919 return;
8921 /* Replace the open paren with "CAST_SUGGESTION<". */
8922 pretty_printer pp;
8923 pp_printf (&pp, "%s<", cast_suggestion);
8924 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8926 /* Replace the close paren with "> (". */
8927 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8929 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8930 rich_loc->add_fixit_insert_after (")");
8934 /* Parse a cast-expression.
8936 cast-expression:
8937 unary-expression
8938 ( type-id ) cast-expression
8940 ADDRESS_P is true iff the unary-expression is appearing as the
8941 operand of the `&' operator. CAST_P is true if this expression is
8942 the target of a cast.
8944 Returns a representation of the expression. */
8946 static cp_expr
8947 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8948 bool decltype_p, cp_id_kind * pidk)
8950 /* If it's a `(', then we might be looking at a cast. */
8951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8953 tree type = NULL_TREE;
8954 cp_expr expr (NULL_TREE);
8955 int cast_expression = 0;
8956 const char *saved_message;
8958 /* There's no way to know yet whether or not this is a cast.
8959 For example, `(int (3))' is a unary-expression, while `(int)
8960 3' is a cast. So, we resort to parsing tentatively. */
8961 cp_parser_parse_tentatively (parser);
8962 /* Types may not be defined in a cast. */
8963 saved_message = parser->type_definition_forbidden_message;
8964 parser->type_definition_forbidden_message
8965 = G_("types may not be defined in casts");
8966 /* Consume the `('. */
8967 matching_parens parens;
8968 cp_token *open_paren = parens.consume_open (parser);
8969 location_t open_paren_loc = open_paren->location;
8970 location_t close_paren_loc = UNKNOWN_LOCATION;
8972 /* A very tricky bit is that `(struct S) { 3 }' is a
8973 compound-literal (which we permit in C++ as an extension).
8974 But, that construct is not a cast-expression -- it is a
8975 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8976 is legal; if the compound-literal were a cast-expression,
8977 you'd need an extra set of parentheses.) But, if we parse
8978 the type-id, and it happens to be a class-specifier, then we
8979 will commit to the parse at that point, because we cannot
8980 undo the action that is done when creating a new class. So,
8981 then we cannot back up and do a postfix-expression.
8983 Another tricky case is the following (c++/29234):
8985 struct S { void operator () (); };
8987 void foo ()
8989 ( S()() );
8992 As a type-id we parse the parenthesized S()() as a function
8993 returning a function, groktypename complains and we cannot
8994 back up in this case either.
8996 Therefore, we scan ahead to the closing `)', and check to see
8997 if the tokens after the `)' can start a cast-expression. Otherwise
8998 we are dealing with an unary-expression, a postfix-expression
8999 or something else.
9001 Yet another tricky case, in C++11, is the following (c++/54891):
9003 (void)[]{};
9005 The issue is that usually, besides the case of lambda-expressions,
9006 the parenthesized type-id cannot be followed by '[', and, eg, we
9007 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9008 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9009 we don't commit, we try a cast-expression, then an unary-expression.
9011 Save tokens so that we can put them back. */
9012 cp_lexer_save_tokens (parser->lexer);
9014 /* We may be looking at a cast-expression. */
9015 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9016 /*consume_paren=*/true))
9017 cast_expression
9018 = cp_parser_tokens_start_cast_expression (parser);
9020 /* Roll back the tokens we skipped. */
9021 cp_lexer_rollback_tokens (parser->lexer);
9022 /* If we aren't looking at a cast-expression, simulate an error so
9023 that the call to cp_parser_error_occurred below returns true. */
9024 if (!cast_expression)
9025 cp_parser_simulate_error (parser);
9026 else
9028 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9029 parser->in_type_id_in_expr_p = true;
9030 /* Look for the type-id. */
9031 type = cp_parser_type_id (parser);
9032 /* Look for the closing `)'. */
9033 cp_token *close_paren = parens.require_close (parser);
9034 if (close_paren)
9035 close_paren_loc = close_paren->location;
9036 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9039 /* Restore the saved message. */
9040 parser->type_definition_forbidden_message = saved_message;
9042 /* At this point this can only be either a cast or a
9043 parenthesized ctor such as `(T ())' that looks like a cast to
9044 function returning T. */
9045 if (!cp_parser_error_occurred (parser))
9047 /* Only commit if the cast-expression doesn't start with
9048 '++', '--', or '[' in C++11. */
9049 if (cast_expression > 0)
9050 cp_parser_commit_to_topmost_tentative_parse (parser);
9052 expr = cp_parser_cast_expression (parser,
9053 /*address_p=*/false,
9054 /*cast_p=*/true,
9055 /*decltype_p=*/false,
9056 pidk);
9058 if (cp_parser_parse_definitely (parser))
9060 /* Warn about old-style casts, if so requested. */
9061 if (warn_old_style_cast
9062 && !in_system_header_at (input_location)
9063 && !VOID_TYPE_P (type)
9064 && current_lang_name != lang_name_c)
9066 gcc_rich_location rich_loc (input_location);
9067 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9068 expr, type);
9069 warning_at (&rich_loc, OPT_Wold_style_cast,
9070 "use of old-style cast to %q#T", type);
9073 /* Only type conversions to integral or enumeration types
9074 can be used in constant-expressions. */
9075 if (!cast_valid_in_integral_constant_expression_p (type)
9076 && cp_parser_non_integral_constant_expression (parser,
9077 NIC_CAST))
9078 return error_mark_node;
9080 /* Perform the cast. */
9081 /* Make a location:
9082 (TYPE) EXPR
9083 ^~~~~~~~~~~
9084 with start==caret at the open paren, extending to the
9085 end of "expr". */
9086 location_t cast_loc = make_location (open_paren_loc,
9087 open_paren_loc,
9088 expr.get_finish ());
9089 expr = build_c_cast (cast_loc, type, expr);
9090 return expr;
9093 else
9094 cp_parser_abort_tentative_parse (parser);
9097 /* If we get here, then it's not a cast, so it must be a
9098 unary-expression. */
9099 return cp_parser_unary_expression (parser, pidk, address_p,
9100 cast_p, decltype_p);
9103 /* Parse a binary expression of the general form:
9105 pm-expression:
9106 cast-expression
9107 pm-expression .* cast-expression
9108 pm-expression ->* cast-expression
9110 multiplicative-expression:
9111 pm-expression
9112 multiplicative-expression * pm-expression
9113 multiplicative-expression / pm-expression
9114 multiplicative-expression % pm-expression
9116 additive-expression:
9117 multiplicative-expression
9118 additive-expression + multiplicative-expression
9119 additive-expression - multiplicative-expression
9121 shift-expression:
9122 additive-expression
9123 shift-expression << additive-expression
9124 shift-expression >> additive-expression
9126 relational-expression:
9127 shift-expression
9128 relational-expression < shift-expression
9129 relational-expression > shift-expression
9130 relational-expression <= shift-expression
9131 relational-expression >= shift-expression
9133 GNU Extension:
9135 relational-expression:
9136 relational-expression <? shift-expression
9137 relational-expression >? shift-expression
9139 equality-expression:
9140 relational-expression
9141 equality-expression == relational-expression
9142 equality-expression != relational-expression
9144 and-expression:
9145 equality-expression
9146 and-expression & equality-expression
9148 exclusive-or-expression:
9149 and-expression
9150 exclusive-or-expression ^ and-expression
9152 inclusive-or-expression:
9153 exclusive-or-expression
9154 inclusive-or-expression | exclusive-or-expression
9156 logical-and-expression:
9157 inclusive-or-expression
9158 logical-and-expression && inclusive-or-expression
9160 logical-or-expression:
9161 logical-and-expression
9162 logical-or-expression || logical-and-expression
9164 All these are implemented with a single function like:
9166 binary-expression:
9167 simple-cast-expression
9168 binary-expression <token> binary-expression
9170 CAST_P is true if this expression is the target of a cast.
9172 The binops_by_token map is used to get the tree codes for each <token> type.
9173 binary-expressions are associated according to a precedence table. */
9175 #define TOKEN_PRECEDENCE(token) \
9176 (((token->type == CPP_GREATER \
9177 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9178 && !parser->greater_than_is_operator_p) \
9179 ? PREC_NOT_OPERATOR \
9180 : binops_by_token[token->type].prec)
9182 static cp_expr
9183 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9184 bool no_toplevel_fold_p,
9185 bool decltype_p,
9186 enum cp_parser_prec prec,
9187 cp_id_kind * pidk)
9189 cp_parser_expression_stack stack;
9190 cp_parser_expression_stack_entry *sp = &stack[0];
9191 cp_parser_expression_stack_entry current;
9192 cp_expr rhs;
9193 cp_token *token;
9194 enum tree_code rhs_type;
9195 enum cp_parser_prec new_prec, lookahead_prec;
9196 tree overload;
9198 /* Parse the first expression. */
9199 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9200 ? TRUTH_NOT_EXPR : ERROR_MARK);
9201 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9202 cast_p, decltype_p, pidk);
9203 current.prec = prec;
9205 if (cp_parser_error_occurred (parser))
9206 return error_mark_node;
9208 for (;;)
9210 /* Get an operator token. */
9211 token = cp_lexer_peek_token (parser->lexer);
9213 if (warn_cxx11_compat
9214 && token->type == CPP_RSHIFT
9215 && !parser->greater_than_is_operator_p)
9217 if (warning_at (token->location, OPT_Wc__11_compat,
9218 "%<>>%> operator is treated"
9219 " as two right angle brackets in C++11"))
9220 inform (token->location,
9221 "suggest parentheses around %<>>%> expression");
9224 new_prec = TOKEN_PRECEDENCE (token);
9225 if (new_prec != PREC_NOT_OPERATOR
9226 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9227 /* This is a fold-expression; handle it later. */
9228 new_prec = PREC_NOT_OPERATOR;
9230 /* Popping an entry off the stack means we completed a subexpression:
9231 - either we found a token which is not an operator (`>' where it is not
9232 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9233 will happen repeatedly;
9234 - or, we found an operator which has lower priority. This is the case
9235 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9236 parsing `3 * 4'. */
9237 if (new_prec <= current.prec)
9239 if (sp == stack)
9240 break;
9241 else
9242 goto pop;
9245 get_rhs:
9246 current.tree_type = binops_by_token[token->type].tree_type;
9247 current.loc = token->location;
9249 /* We used the operator token. */
9250 cp_lexer_consume_token (parser->lexer);
9252 /* For "false && x" or "true || x", x will never be executed;
9253 disable warnings while evaluating it. */
9254 if (current.tree_type == TRUTH_ANDIF_EXPR)
9255 c_inhibit_evaluation_warnings +=
9256 cp_fully_fold (current.lhs) == truthvalue_false_node;
9257 else if (current.tree_type == TRUTH_ORIF_EXPR)
9258 c_inhibit_evaluation_warnings +=
9259 cp_fully_fold (current.lhs) == truthvalue_true_node;
9261 /* Extract another operand. It may be the RHS of this expression
9262 or the LHS of a new, higher priority expression. */
9263 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9264 ? TRUTH_NOT_EXPR : ERROR_MARK);
9265 rhs = cp_parser_simple_cast_expression (parser);
9267 /* Get another operator token. Look up its precedence to avoid
9268 building a useless (immediately popped) stack entry for common
9269 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9270 token = cp_lexer_peek_token (parser->lexer);
9271 lookahead_prec = TOKEN_PRECEDENCE (token);
9272 if (lookahead_prec != PREC_NOT_OPERATOR
9273 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9274 lookahead_prec = PREC_NOT_OPERATOR;
9275 if (lookahead_prec > new_prec)
9277 /* ... and prepare to parse the RHS of the new, higher priority
9278 expression. Since precedence levels on the stack are
9279 monotonically increasing, we do not have to care about
9280 stack overflows. */
9281 *sp = current;
9282 ++sp;
9283 current.lhs = rhs;
9284 current.lhs_type = rhs_type;
9285 current.prec = new_prec;
9286 new_prec = lookahead_prec;
9287 goto get_rhs;
9289 pop:
9290 lookahead_prec = new_prec;
9291 /* If the stack is not empty, we have parsed into LHS the right side
9292 (`4' in the example above) of an expression we had suspended.
9293 We can use the information on the stack to recover the LHS (`3')
9294 from the stack together with the tree code (`MULT_EXPR'), and
9295 the precedence of the higher level subexpression
9296 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9297 which will be used to actually build the additive expression. */
9298 rhs = current.lhs;
9299 rhs_type = current.lhs_type;
9300 --sp;
9301 current = *sp;
9304 /* Undo the disabling of warnings done above. */
9305 if (current.tree_type == TRUTH_ANDIF_EXPR)
9306 c_inhibit_evaluation_warnings -=
9307 cp_fully_fold (current.lhs) == truthvalue_false_node;
9308 else if (current.tree_type == TRUTH_ORIF_EXPR)
9309 c_inhibit_evaluation_warnings -=
9310 cp_fully_fold (current.lhs) == truthvalue_true_node;
9312 if (warn_logical_not_paren
9313 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9314 && current.lhs_type == TRUTH_NOT_EXPR
9315 /* Avoid warning for !!x == y. */
9316 && (TREE_CODE (current.lhs) != NE_EXPR
9317 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9318 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9319 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9320 /* Avoid warning for !b == y where b is boolean. */
9321 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9322 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9323 != BOOLEAN_TYPE))))
9324 /* Avoid warning for !!b == y where b is boolean. */
9325 && (!DECL_P (current.lhs)
9326 || TREE_TYPE (current.lhs) == NULL_TREE
9327 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9328 warn_logical_not_parentheses (current.loc, current.tree_type,
9329 current.lhs, maybe_constant_value (rhs));
9331 overload = NULL;
9333 location_t combined_loc = make_location (current.loc,
9334 current.lhs.get_start (),
9335 rhs.get_finish ());
9337 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9338 ERROR_MARK for everything that is not a binary expression.
9339 This makes warn_about_parentheses miss some warnings that
9340 involve unary operators. For unary expressions we should
9341 pass the correct tree_code unless the unary expression was
9342 surrounded by parentheses.
9344 if (no_toplevel_fold_p
9345 && lookahead_prec <= current.prec
9346 && sp == stack)
9348 if (current.lhs == error_mark_node || rhs == error_mark_node)
9349 current.lhs = error_mark_node;
9350 else
9352 current.lhs
9353 = build_min (current.tree_type,
9354 TREE_CODE_CLASS (current.tree_type)
9355 == tcc_comparison
9356 ? boolean_type_node : TREE_TYPE (current.lhs),
9357 current.lhs.get_value (), rhs.get_value ());
9358 SET_EXPR_LOCATION (current.lhs, combined_loc);
9361 else
9363 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9364 current.lhs, current.lhs_type,
9365 rhs, rhs_type, &overload,
9366 complain_flags (decltype_p));
9367 /* TODO: build_x_binary_op doesn't always honor the location. */
9368 current.lhs.set_location (combined_loc);
9370 current.lhs_type = current.tree_type;
9372 /* If the binary operator required the use of an overloaded operator,
9373 then this expression cannot be an integral constant-expression.
9374 An overloaded operator can be used even if both operands are
9375 otherwise permissible in an integral constant-expression if at
9376 least one of the operands is of enumeration type. */
9378 if (overload
9379 && cp_parser_non_integral_constant_expression (parser,
9380 NIC_OVERLOADED))
9381 return error_mark_node;
9384 return current.lhs;
9387 static cp_expr
9388 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9389 bool no_toplevel_fold_p,
9390 enum cp_parser_prec prec,
9391 cp_id_kind * pidk)
9393 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9394 /*decltype*/false, prec, pidk);
9397 /* Parse the `? expression : assignment-expression' part of a
9398 conditional-expression. The LOGICAL_OR_EXPR is the
9399 logical-or-expression that started the conditional-expression.
9400 Returns a representation of the entire conditional-expression.
9402 This routine is used by cp_parser_assignment_expression.
9404 ? expression : assignment-expression
9406 GNU Extensions:
9408 ? : assignment-expression */
9410 static tree
9411 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9413 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9414 cp_expr assignment_expr;
9415 struct cp_token *token;
9416 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9418 /* Consume the `?' token. */
9419 cp_lexer_consume_token (parser->lexer);
9420 token = cp_lexer_peek_token (parser->lexer);
9421 if (cp_parser_allow_gnu_extensions_p (parser)
9422 && token->type == CPP_COLON)
9424 pedwarn (token->location, OPT_Wpedantic,
9425 "ISO C++ does not allow ?: with omitted middle operand");
9426 /* Implicit true clause. */
9427 expr = NULL_TREE;
9428 c_inhibit_evaluation_warnings +=
9429 folded_logical_or_expr == truthvalue_true_node;
9430 warn_for_omitted_condop (token->location, logical_or_expr);
9432 else
9434 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9435 parser->colon_corrects_to_scope_p = false;
9436 /* Parse the expression. */
9437 c_inhibit_evaluation_warnings +=
9438 folded_logical_or_expr == truthvalue_false_node;
9439 expr = cp_parser_expression (parser);
9440 c_inhibit_evaluation_warnings +=
9441 ((folded_logical_or_expr == truthvalue_true_node)
9442 - (folded_logical_or_expr == truthvalue_false_node));
9443 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9446 /* The next token should be a `:'. */
9447 cp_parser_require (parser, CPP_COLON, RT_COLON);
9448 /* Parse the assignment-expression. */
9449 assignment_expr = cp_parser_assignment_expression (parser);
9450 c_inhibit_evaluation_warnings -=
9451 folded_logical_or_expr == truthvalue_true_node;
9453 /* Make a location:
9454 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9455 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9456 with the caret at the "?", ranging from the start of
9457 the logical_or_expr to the end of the assignment_expr. */
9458 loc = make_location (loc,
9459 logical_or_expr.get_start (),
9460 assignment_expr.get_finish ());
9462 /* Build the conditional-expression. */
9463 return build_x_conditional_expr (loc, logical_or_expr,
9464 expr,
9465 assignment_expr,
9466 tf_warning_or_error);
9469 /* Parse an assignment-expression.
9471 assignment-expression:
9472 conditional-expression
9473 logical-or-expression assignment-operator assignment_expression
9474 throw-expression
9476 CAST_P is true if this expression is the target of a cast.
9477 DECLTYPE_P is true if this expression is the operand of decltype.
9479 Returns a representation for the expression. */
9481 static cp_expr
9482 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9483 bool cast_p, bool decltype_p)
9485 cp_expr expr;
9487 /* If the next token is the `throw' keyword, then we're looking at
9488 a throw-expression. */
9489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9490 expr = cp_parser_throw_expression (parser);
9491 /* Otherwise, it must be that we are looking at a
9492 logical-or-expression. */
9493 else
9495 /* Parse the binary expressions (logical-or-expression). */
9496 expr = cp_parser_binary_expression (parser, cast_p, false,
9497 decltype_p,
9498 PREC_NOT_OPERATOR, pidk);
9499 /* If the next token is a `?' then we're actually looking at a
9500 conditional-expression. */
9501 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9502 return cp_parser_question_colon_clause (parser, expr);
9503 else
9505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9507 /* If it's an assignment-operator, we're using the second
9508 production. */
9509 enum tree_code assignment_operator
9510 = cp_parser_assignment_operator_opt (parser);
9511 if (assignment_operator != ERROR_MARK)
9513 bool non_constant_p;
9515 /* Parse the right-hand side of the assignment. */
9516 cp_expr rhs = cp_parser_initializer_clause (parser,
9517 &non_constant_p);
9519 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9520 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9522 /* An assignment may not appear in a
9523 constant-expression. */
9524 if (cp_parser_non_integral_constant_expression (parser,
9525 NIC_ASSIGNMENT))
9526 return error_mark_node;
9527 /* Build the assignment expression. Its default
9528 location:
9529 LHS = RHS
9530 ~~~~^~~~~
9531 is the location of the '=' token as the
9532 caret, ranging from the start of the lhs to the
9533 end of the rhs. */
9534 loc = make_location (loc,
9535 expr.get_start (),
9536 rhs.get_finish ());
9537 expr = build_x_modify_expr (loc, expr,
9538 assignment_operator,
9539 rhs,
9540 complain_flags (decltype_p));
9541 /* TODO: build_x_modify_expr doesn't honor the location,
9542 so we must set it here. */
9543 expr.set_location (loc);
9548 return expr;
9551 /* Parse an (optional) assignment-operator.
9553 assignment-operator: one of
9554 = *= /= %= += -= >>= <<= &= ^= |=
9556 GNU Extension:
9558 assignment-operator: one of
9559 <?= >?=
9561 If the next token is an assignment operator, the corresponding tree
9562 code is returned, and the token is consumed. For example, for
9563 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9564 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9565 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9566 operator, ERROR_MARK is returned. */
9568 static enum tree_code
9569 cp_parser_assignment_operator_opt (cp_parser* parser)
9571 enum tree_code op;
9572 cp_token *token;
9574 /* Peek at the next token. */
9575 token = cp_lexer_peek_token (parser->lexer);
9577 switch (token->type)
9579 case CPP_EQ:
9580 op = NOP_EXPR;
9581 break;
9583 case CPP_MULT_EQ:
9584 op = MULT_EXPR;
9585 break;
9587 case CPP_DIV_EQ:
9588 op = TRUNC_DIV_EXPR;
9589 break;
9591 case CPP_MOD_EQ:
9592 op = TRUNC_MOD_EXPR;
9593 break;
9595 case CPP_PLUS_EQ:
9596 op = PLUS_EXPR;
9597 break;
9599 case CPP_MINUS_EQ:
9600 op = MINUS_EXPR;
9601 break;
9603 case CPP_RSHIFT_EQ:
9604 op = RSHIFT_EXPR;
9605 break;
9607 case CPP_LSHIFT_EQ:
9608 op = LSHIFT_EXPR;
9609 break;
9611 case CPP_AND_EQ:
9612 op = BIT_AND_EXPR;
9613 break;
9615 case CPP_XOR_EQ:
9616 op = BIT_XOR_EXPR;
9617 break;
9619 case CPP_OR_EQ:
9620 op = BIT_IOR_EXPR;
9621 break;
9623 default:
9624 /* Nothing else is an assignment operator. */
9625 op = ERROR_MARK;
9628 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9629 if (op != ERROR_MARK
9630 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9631 op = ERROR_MARK;
9633 /* If it was an assignment operator, consume it. */
9634 if (op != ERROR_MARK)
9635 cp_lexer_consume_token (parser->lexer);
9637 return op;
9640 /* Parse an expression.
9642 expression:
9643 assignment-expression
9644 expression , assignment-expression
9646 CAST_P is true if this expression is the target of a cast.
9647 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9648 except possibly parenthesized or on the RHS of a comma (N3276).
9650 Returns a representation of the expression. */
9652 static cp_expr
9653 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9654 bool cast_p, bool decltype_p)
9656 cp_expr expression = NULL_TREE;
9657 location_t loc = UNKNOWN_LOCATION;
9659 while (true)
9661 cp_expr assignment_expression;
9663 /* Parse the next assignment-expression. */
9664 assignment_expression
9665 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9667 /* We don't create a temporary for a call that is the immediate operand
9668 of decltype or on the RHS of a comma. But when we see a comma, we
9669 need to create a temporary for a call on the LHS. */
9670 if (decltype_p && !processing_template_decl
9671 && TREE_CODE (assignment_expression) == CALL_EXPR
9672 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9673 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9674 assignment_expression
9675 = build_cplus_new (TREE_TYPE (assignment_expression),
9676 assignment_expression, tf_warning_or_error);
9678 /* If this is the first assignment-expression, we can just
9679 save it away. */
9680 if (!expression)
9681 expression = assignment_expression;
9682 else
9684 /* Create a location with caret at the comma, ranging
9685 from the start of the LHS to the end of the RHS. */
9686 loc = make_location (loc,
9687 expression.get_start (),
9688 assignment_expression.get_finish ());
9689 expression = build_x_compound_expr (loc, expression,
9690 assignment_expression,
9691 complain_flags (decltype_p));
9692 expression.set_location (loc);
9694 /* If the next token is not a comma, or we're in a fold-expression, then
9695 we are done with the expression. */
9696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9697 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9698 break;
9699 /* Consume the `,'. */
9700 loc = cp_lexer_peek_token (parser->lexer)->location;
9701 cp_lexer_consume_token (parser->lexer);
9702 /* A comma operator cannot appear in a constant-expression. */
9703 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9704 expression = error_mark_node;
9707 return expression;
9710 /* Parse a constant-expression.
9712 constant-expression:
9713 conditional-expression
9715 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9716 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9717 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9718 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9719 only parse a conditional-expression, otherwise parse an
9720 assignment-expression. See below for rationale. */
9722 static cp_expr
9723 cp_parser_constant_expression (cp_parser* parser,
9724 bool allow_non_constant_p,
9725 bool *non_constant_p,
9726 bool strict_p)
9728 bool saved_integral_constant_expression_p;
9729 bool saved_allow_non_integral_constant_expression_p;
9730 bool saved_non_integral_constant_expression_p;
9731 cp_expr expression;
9733 /* It might seem that we could simply parse the
9734 conditional-expression, and then check to see if it were
9735 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9736 one that the compiler can figure out is constant, possibly after
9737 doing some simplifications or optimizations. The standard has a
9738 precise definition of constant-expression, and we must honor
9739 that, even though it is somewhat more restrictive.
9741 For example:
9743 int i[(2, 3)];
9745 is not a legal declaration, because `(2, 3)' is not a
9746 constant-expression. The `,' operator is forbidden in a
9747 constant-expression. However, GCC's constant-folding machinery
9748 will fold this operation to an INTEGER_CST for `3'. */
9750 /* Save the old settings. */
9751 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9752 saved_allow_non_integral_constant_expression_p
9753 = parser->allow_non_integral_constant_expression_p;
9754 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9755 /* We are now parsing a constant-expression. */
9756 parser->integral_constant_expression_p = true;
9757 parser->allow_non_integral_constant_expression_p
9758 = (allow_non_constant_p || cxx_dialect >= cxx11);
9759 parser->non_integral_constant_expression_p = false;
9760 /* Although the grammar says "conditional-expression", when not STRICT_P,
9761 we parse an "assignment-expression", which also permits
9762 "throw-expression" and the use of assignment operators. In the case
9763 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9764 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9765 actually essential that we look for an assignment-expression.
9766 For example, cp_parser_initializer_clauses uses this function to
9767 determine whether a particular assignment-expression is in fact
9768 constant. */
9769 if (strict_p)
9771 /* Parse the binary expressions (logical-or-expression). */
9772 expression = cp_parser_binary_expression (parser, false, false, false,
9773 PREC_NOT_OPERATOR, NULL);
9774 /* If the next token is a `?' then we're actually looking at
9775 a conditional-expression; otherwise we're done. */
9776 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9777 expression = cp_parser_question_colon_clause (parser, expression);
9779 else
9780 expression = cp_parser_assignment_expression (parser);
9781 /* Restore the old settings. */
9782 parser->integral_constant_expression_p
9783 = saved_integral_constant_expression_p;
9784 parser->allow_non_integral_constant_expression_p
9785 = saved_allow_non_integral_constant_expression_p;
9786 if (cxx_dialect >= cxx11)
9788 /* Require an rvalue constant expression here; that's what our
9789 callers expect. Reference constant expressions are handled
9790 separately in e.g. cp_parser_template_argument. */
9791 tree decay = expression;
9792 if (TREE_TYPE (expression)
9793 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9794 decay = build_address (expression);
9795 bool is_const = potential_rvalue_constant_expression (decay);
9796 parser->non_integral_constant_expression_p = !is_const;
9797 if (!is_const && !allow_non_constant_p)
9798 require_potential_rvalue_constant_expression (decay);
9800 if (allow_non_constant_p)
9801 *non_constant_p = parser->non_integral_constant_expression_p;
9802 parser->non_integral_constant_expression_p
9803 = saved_non_integral_constant_expression_p;
9805 return expression;
9808 /* Parse __builtin_offsetof.
9810 offsetof-expression:
9811 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9813 offsetof-member-designator:
9814 id-expression
9815 | offsetof-member-designator "." id-expression
9816 | offsetof-member-designator "[" expression "]"
9817 | offsetof-member-designator "->" id-expression */
9819 static cp_expr
9820 cp_parser_builtin_offsetof (cp_parser *parser)
9822 int save_ice_p, save_non_ice_p;
9823 tree type;
9824 cp_expr expr;
9825 cp_id_kind dummy;
9826 cp_token *token;
9827 location_t finish_loc;
9829 /* We're about to accept non-integral-constant things, but will
9830 definitely yield an integral constant expression. Save and
9831 restore these values around our local parsing. */
9832 save_ice_p = parser->integral_constant_expression_p;
9833 save_non_ice_p = parser->non_integral_constant_expression_p;
9835 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9837 /* Consume the "__builtin_offsetof" token. */
9838 cp_lexer_consume_token (parser->lexer);
9839 /* Consume the opening `('. */
9840 matching_parens parens;
9841 parens.require_open (parser);
9842 /* Parse the type-id. */
9843 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9845 const char *saved_message = parser->type_definition_forbidden_message;
9846 parser->type_definition_forbidden_message
9847 = G_("types may not be defined within __builtin_offsetof");
9848 type = cp_parser_type_id (parser);
9849 parser->type_definition_forbidden_message = saved_message;
9851 /* Look for the `,'. */
9852 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9853 token = cp_lexer_peek_token (parser->lexer);
9855 /* Build the (type *)null that begins the traditional offsetof macro. */
9856 tree object_ptr
9857 = build_static_cast (build_pointer_type (type), null_pointer_node,
9858 tf_warning_or_error);
9860 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9861 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9862 true, &dummy, token->location);
9863 while (true)
9865 token = cp_lexer_peek_token (parser->lexer);
9866 switch (token->type)
9868 case CPP_OPEN_SQUARE:
9869 /* offsetof-member-designator "[" expression "]" */
9870 expr = cp_parser_postfix_open_square_expression (parser, expr,
9871 true, false);
9872 break;
9874 case CPP_DEREF:
9875 /* offsetof-member-designator "->" identifier */
9876 expr = grok_array_decl (token->location, expr,
9877 integer_zero_node, false);
9878 /* FALLTHRU */
9880 case CPP_DOT:
9881 /* offsetof-member-designator "." identifier */
9882 cp_lexer_consume_token (parser->lexer);
9883 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9884 expr, true, &dummy,
9885 token->location);
9886 break;
9888 case CPP_CLOSE_PAREN:
9889 /* Consume the ")" token. */
9890 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9891 cp_lexer_consume_token (parser->lexer);
9892 goto success;
9894 default:
9895 /* Error. We know the following require will fail, but
9896 that gives the proper error message. */
9897 parens.require_close (parser);
9898 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9899 expr = error_mark_node;
9900 goto failure;
9904 success:
9905 /* Make a location of the form:
9906 __builtin_offsetof (struct s, f)
9907 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9908 with caret at the type-id, ranging from the start of the
9909 "_builtin_offsetof" token to the close paren. */
9910 loc = make_location (loc, start_loc, finish_loc);
9911 /* The result will be an INTEGER_CST, so we need to explicitly
9912 preserve the location. */
9913 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9915 failure:
9916 parser->integral_constant_expression_p = save_ice_p;
9917 parser->non_integral_constant_expression_p = save_non_ice_p;
9919 expr = expr.maybe_add_location_wrapper ();
9920 return expr;
9923 /* Parse a trait expression.
9925 Returns a representation of the expression, the underlying type
9926 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9928 static cp_expr
9929 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9931 cp_trait_kind kind;
9932 tree type1, type2 = NULL_TREE;
9933 bool binary = false;
9934 bool variadic = false;
9936 switch (keyword)
9938 case RID_HAS_NOTHROW_ASSIGN:
9939 kind = CPTK_HAS_NOTHROW_ASSIGN;
9940 break;
9941 case RID_HAS_NOTHROW_CONSTRUCTOR:
9942 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9943 break;
9944 case RID_HAS_NOTHROW_COPY:
9945 kind = CPTK_HAS_NOTHROW_COPY;
9946 break;
9947 case RID_HAS_TRIVIAL_ASSIGN:
9948 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9949 break;
9950 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9951 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9952 break;
9953 case RID_HAS_TRIVIAL_COPY:
9954 kind = CPTK_HAS_TRIVIAL_COPY;
9955 break;
9956 case RID_HAS_TRIVIAL_DESTRUCTOR:
9957 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9958 break;
9959 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9960 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9961 break;
9962 case RID_HAS_VIRTUAL_DESTRUCTOR:
9963 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9964 break;
9965 case RID_IS_ABSTRACT:
9966 kind = CPTK_IS_ABSTRACT;
9967 break;
9968 case RID_IS_AGGREGATE:
9969 kind = CPTK_IS_AGGREGATE;
9970 break;
9971 case RID_IS_BASE_OF:
9972 kind = CPTK_IS_BASE_OF;
9973 binary = true;
9974 break;
9975 case RID_IS_CLASS:
9976 kind = CPTK_IS_CLASS;
9977 break;
9978 case RID_IS_EMPTY:
9979 kind = CPTK_IS_EMPTY;
9980 break;
9981 case RID_IS_ENUM:
9982 kind = CPTK_IS_ENUM;
9983 break;
9984 case RID_IS_FINAL:
9985 kind = CPTK_IS_FINAL;
9986 break;
9987 case RID_IS_LITERAL_TYPE:
9988 kind = CPTK_IS_LITERAL_TYPE;
9989 break;
9990 case RID_IS_POD:
9991 kind = CPTK_IS_POD;
9992 break;
9993 case RID_IS_POLYMORPHIC:
9994 kind = CPTK_IS_POLYMORPHIC;
9995 break;
9996 case RID_IS_SAME_AS:
9997 kind = CPTK_IS_SAME_AS;
9998 binary = true;
9999 break;
10000 case RID_IS_STD_LAYOUT:
10001 kind = CPTK_IS_STD_LAYOUT;
10002 break;
10003 case RID_IS_TRIVIAL:
10004 kind = CPTK_IS_TRIVIAL;
10005 break;
10006 case RID_IS_TRIVIALLY_ASSIGNABLE:
10007 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10008 binary = true;
10009 break;
10010 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10011 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10012 variadic = true;
10013 break;
10014 case RID_IS_TRIVIALLY_COPYABLE:
10015 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10016 break;
10017 case RID_IS_UNION:
10018 kind = CPTK_IS_UNION;
10019 break;
10020 case RID_UNDERLYING_TYPE:
10021 kind = CPTK_UNDERLYING_TYPE;
10022 break;
10023 case RID_BASES:
10024 kind = CPTK_BASES;
10025 break;
10026 case RID_DIRECT_BASES:
10027 kind = CPTK_DIRECT_BASES;
10028 break;
10029 case RID_IS_ASSIGNABLE:
10030 kind = CPTK_IS_ASSIGNABLE;
10031 binary = true;
10032 break;
10033 case RID_IS_CONSTRUCTIBLE:
10034 kind = CPTK_IS_CONSTRUCTIBLE;
10035 variadic = true;
10036 break;
10037 default:
10038 gcc_unreachable ();
10041 /* Get location of initial token. */
10042 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10044 /* Consume the token. */
10045 cp_lexer_consume_token (parser->lexer);
10047 matching_parens parens;
10048 parens.require_open (parser);
10051 type_id_in_expr_sentinel s (parser);
10052 type1 = cp_parser_type_id (parser);
10055 if (type1 == error_mark_node)
10056 return error_mark_node;
10058 if (binary)
10060 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10063 type_id_in_expr_sentinel s (parser);
10064 type2 = cp_parser_type_id (parser);
10067 if (type2 == error_mark_node)
10068 return error_mark_node;
10070 else if (variadic)
10072 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10074 cp_lexer_consume_token (parser->lexer);
10075 tree elt = cp_parser_type_id (parser);
10076 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10078 cp_lexer_consume_token (parser->lexer);
10079 elt = make_pack_expansion (elt);
10081 if (elt == error_mark_node)
10082 return error_mark_node;
10083 type2 = tree_cons (NULL_TREE, elt, type2);
10087 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10088 parens.require_close (parser);
10090 /* Construct a location of the form:
10091 __is_trivially_copyable(_Tp)
10092 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10093 with start == caret, finishing at the close-paren. */
10094 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10096 /* Complete the trait expression, which may mean either processing
10097 the trait expr now or saving it for template instantiation. */
10098 switch (kind)
10100 case CPTK_UNDERLYING_TYPE:
10101 return cp_expr (finish_underlying_type (type1), trait_loc);
10102 case CPTK_BASES:
10103 return cp_expr (finish_bases (type1, false), trait_loc);
10104 case CPTK_DIRECT_BASES:
10105 return cp_expr (finish_bases (type1, true), trait_loc);
10106 default:
10107 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10111 /* Parse a lambda expression.
10113 lambda-expression:
10114 lambda-introducer lambda-declarator [opt] compound-statement
10116 Returns a representation of the expression. */
10118 static cp_expr
10119 cp_parser_lambda_expression (cp_parser* parser)
10121 tree lambda_expr = build_lambda_expr ();
10122 tree type;
10123 bool ok = true;
10124 cp_token *token = cp_lexer_peek_token (parser->lexer);
10125 cp_token_position start = 0;
10127 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10129 if (cp_unevaluated_operand)
10131 if (!token->error_reported)
10133 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10134 "lambda-expression in unevaluated context");
10135 token->error_reported = true;
10137 ok = false;
10139 else if (parser->in_template_argument_list_p)
10141 if (!token->error_reported)
10143 error_at (token->location, "lambda-expression in template-argument");
10144 token->error_reported = true;
10146 ok = false;
10149 /* We may be in the middle of deferred access check. Disable
10150 it now. */
10151 push_deferring_access_checks (dk_no_deferred);
10153 cp_parser_lambda_introducer (parser, lambda_expr);
10155 type = begin_lambda_type (lambda_expr);
10156 if (type == error_mark_node)
10157 return error_mark_node;
10159 record_lambda_scope (lambda_expr);
10161 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10162 determine_visibility (TYPE_NAME (type));
10164 /* Now that we've started the type, add the capture fields for any
10165 explicit captures. */
10166 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10169 /* Inside the class, surrounding template-parameter-lists do not apply. */
10170 unsigned int saved_num_template_parameter_lists
10171 = parser->num_template_parameter_lists;
10172 unsigned char in_statement = parser->in_statement;
10173 bool in_switch_statement_p = parser->in_switch_statement_p;
10174 bool fully_implicit_function_template_p
10175 = parser->fully_implicit_function_template_p;
10176 tree implicit_template_parms = parser->implicit_template_parms;
10177 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10178 bool auto_is_implicit_function_template_parm_p
10179 = parser->auto_is_implicit_function_template_parm_p;
10181 parser->num_template_parameter_lists = 0;
10182 parser->in_statement = 0;
10183 parser->in_switch_statement_p = false;
10184 parser->fully_implicit_function_template_p = false;
10185 parser->implicit_template_parms = 0;
10186 parser->implicit_template_scope = 0;
10187 parser->auto_is_implicit_function_template_parm_p = false;
10189 /* By virtue of defining a local class, a lambda expression has access to
10190 the private variables of enclosing classes. */
10192 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10194 if (ok && cp_parser_error_occurred (parser))
10195 ok = false;
10197 if (ok)
10199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10200 && cp_parser_start_tentative_firewall (parser))
10201 start = token;
10202 cp_parser_lambda_body (parser, lambda_expr);
10204 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10206 if (cp_parser_skip_to_closing_brace (parser))
10207 cp_lexer_consume_token (parser->lexer);
10210 /* The capture list was built up in reverse order; fix that now. */
10211 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10212 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10214 if (ok)
10215 maybe_add_lambda_conv_op (type);
10217 type = finish_struct (type, /*attributes=*/NULL_TREE);
10219 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10220 parser->in_statement = in_statement;
10221 parser->in_switch_statement_p = in_switch_statement_p;
10222 parser->fully_implicit_function_template_p
10223 = fully_implicit_function_template_p;
10224 parser->implicit_template_parms = implicit_template_parms;
10225 parser->implicit_template_scope = implicit_template_scope;
10226 parser->auto_is_implicit_function_template_parm_p
10227 = auto_is_implicit_function_template_parm_p;
10230 /* This field is only used during parsing of the lambda. */
10231 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10233 /* This lambda shouldn't have any proxies left at this point. */
10234 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10235 /* And now that we're done, push proxies for an enclosing lambda. */
10236 insert_pending_capture_proxies ();
10238 /* Update the lambda expression to a range. */
10239 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10240 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10241 token->location,
10242 end_tok->location);
10244 if (ok)
10245 lambda_expr = build_lambda_object (lambda_expr);
10246 else
10247 lambda_expr = error_mark_node;
10249 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10251 pop_deferring_access_checks ();
10253 return lambda_expr;
10256 /* Parse the beginning of a lambda expression.
10258 lambda-introducer:
10259 [ lambda-capture [opt] ]
10261 LAMBDA_EXPR is the current representation of the lambda expression. */
10263 static void
10264 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10266 /* Need commas after the first capture. */
10267 bool first = true;
10269 /* Eat the leading `['. */
10270 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10272 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10273 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10274 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10275 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10276 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10277 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10279 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10281 cp_lexer_consume_token (parser->lexer);
10282 first = false;
10285 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10287 cp_token* capture_token;
10288 tree capture_id;
10289 tree capture_init_expr;
10290 cp_id_kind idk = CP_ID_KIND_NONE;
10291 bool explicit_init_p = false;
10293 enum capture_kind_type
10295 BY_COPY,
10296 BY_REFERENCE
10298 enum capture_kind_type capture_kind = BY_COPY;
10300 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10302 error ("expected end of capture-list");
10303 return;
10306 if (first)
10307 first = false;
10308 else
10309 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10311 /* Possibly capture `this'. */
10312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10314 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10315 if (cxx_dialect < cxx2a
10316 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10317 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10318 "with by-copy capture default");
10319 cp_lexer_consume_token (parser->lexer);
10320 add_capture (lambda_expr,
10321 /*id=*/this_identifier,
10322 /*initializer=*/finish_this_expr (),
10323 /*by_reference_p=*/true,
10324 explicit_init_p);
10325 continue;
10328 /* Possibly capture `*this'. */
10329 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10330 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10332 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10333 if (cxx_dialect < cxx17)
10334 pedwarn (loc, 0, "%<*this%> capture only available with "
10335 "-std=c++17 or -std=gnu++17");
10336 cp_lexer_consume_token (parser->lexer);
10337 cp_lexer_consume_token (parser->lexer);
10338 add_capture (lambda_expr,
10339 /*id=*/this_identifier,
10340 /*initializer=*/finish_this_expr (),
10341 /*by_reference_p=*/false,
10342 explicit_init_p);
10343 continue;
10346 /* Remember whether we want to capture as a reference or not. */
10347 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10349 capture_kind = BY_REFERENCE;
10350 cp_lexer_consume_token (parser->lexer);
10353 /* Get the identifier. */
10354 capture_token = cp_lexer_peek_token (parser->lexer);
10355 capture_id = cp_parser_identifier (parser);
10357 if (capture_id == error_mark_node)
10358 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10359 delimiters, but I modified this to stop on unnested ']' as well. It
10360 was already changed to stop on unnested '}', so the
10361 "closing_parenthesis" name is no more misleading with my change. */
10363 cp_parser_skip_to_closing_parenthesis (parser,
10364 /*recovering=*/true,
10365 /*or_comma=*/true,
10366 /*consume_paren=*/true);
10367 break;
10370 /* Find the initializer for this capture. */
10371 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10372 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10373 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10375 bool direct, non_constant;
10376 /* An explicit initializer exists. */
10377 if (cxx_dialect < cxx14)
10378 pedwarn (input_location, 0,
10379 "lambda capture initializers "
10380 "only available with -std=c++14 or -std=gnu++14");
10381 capture_init_expr = cp_parser_initializer (parser, &direct,
10382 &non_constant, true);
10383 explicit_init_p = true;
10384 if (capture_init_expr == NULL_TREE)
10386 error ("empty initializer for lambda init-capture");
10387 capture_init_expr = error_mark_node;
10390 else
10392 const char* error_msg;
10394 /* Turn the identifier into an id-expression. */
10395 capture_init_expr
10396 = cp_parser_lookup_name_simple (parser, capture_id,
10397 capture_token->location);
10399 if (capture_init_expr == error_mark_node)
10401 unqualified_name_lookup_error (capture_id);
10402 continue;
10404 else if (!VAR_P (capture_init_expr)
10405 && TREE_CODE (capture_init_expr) != PARM_DECL)
10407 error_at (capture_token->location,
10408 "capture of non-variable %qE",
10409 capture_init_expr);
10410 if (DECL_P (capture_init_expr))
10411 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10412 "%q#D declared here", capture_init_expr);
10413 continue;
10415 if (VAR_P (capture_init_expr)
10416 && decl_storage_duration (capture_init_expr) != dk_auto)
10418 if (pedwarn (capture_token->location, 0, "capture of variable "
10419 "%qD with non-automatic storage duration",
10420 capture_init_expr))
10421 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10422 "%q#D declared here", capture_init_expr);
10423 continue;
10426 capture_init_expr
10427 = finish_id_expression
10428 (capture_id,
10429 capture_init_expr,
10430 parser->scope,
10431 &idk,
10432 /*integral_constant_expression_p=*/false,
10433 /*allow_non_integral_constant_expression_p=*/false,
10434 /*non_integral_constant_expression_p=*/NULL,
10435 /*template_p=*/false,
10436 /*done=*/true,
10437 /*address_p=*/false,
10438 /*template_arg_p=*/false,
10439 &error_msg,
10440 capture_token->location);
10442 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10444 cp_lexer_consume_token (parser->lexer);
10445 capture_init_expr = make_pack_expansion (capture_init_expr);
10449 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10450 && !explicit_init_p)
10452 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10453 && capture_kind == BY_COPY)
10454 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10455 "of %qD redundant with by-copy capture default",
10456 capture_id);
10457 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10458 && capture_kind == BY_REFERENCE)
10459 pedwarn (capture_token->location, 0, "explicit by-reference "
10460 "capture of %qD redundant with by-reference capture "
10461 "default", capture_id);
10464 add_capture (lambda_expr,
10465 capture_id,
10466 capture_init_expr,
10467 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10468 explicit_init_p);
10470 /* If there is any qualification still in effect, clear it
10471 now; we will be starting fresh with the next capture. */
10472 parser->scope = NULL_TREE;
10473 parser->qualifying_scope = NULL_TREE;
10474 parser->object_scope = NULL_TREE;
10477 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10480 /* Parse the (optional) middle of a lambda expression.
10482 lambda-declarator:
10483 < template-parameter-list [opt] >
10484 ( parameter-declaration-clause [opt] )
10485 attribute-specifier [opt]
10486 decl-specifier-seq [opt]
10487 exception-specification [opt]
10488 lambda-return-type-clause [opt]
10490 LAMBDA_EXPR is the current representation of the lambda expression. */
10492 static bool
10493 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10495 /* 5.1.1.4 of the standard says:
10496 If a lambda-expression does not include a lambda-declarator, it is as if
10497 the lambda-declarator were ().
10498 This means an empty parameter list, no attributes, and no exception
10499 specification. */
10500 tree param_list = void_list_node;
10501 tree attributes = NULL_TREE;
10502 tree exception_spec = NULL_TREE;
10503 tree template_param_list = NULL_TREE;
10504 tree tx_qual = NULL_TREE;
10505 tree return_type = NULL_TREE;
10506 cp_decl_specifier_seq lambda_specs;
10507 clear_decl_specs (&lambda_specs);
10509 /* The template-parameter-list is optional, but must begin with
10510 an opening angle if present. */
10511 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10513 if (cxx_dialect < cxx14)
10514 pedwarn (parser->lexer->next_token->location, 0,
10515 "lambda templates are only available with "
10516 "-std=c++14 or -std=gnu++14");
10517 else if (cxx_dialect < cxx2a)
10518 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10519 "lambda templates are only available with "
10520 "-std=c++2a or -std=gnu++2a");
10522 cp_lexer_consume_token (parser->lexer);
10524 template_param_list = cp_parser_template_parameter_list (parser);
10526 cp_parser_skip_to_end_of_template_parameter_list (parser);
10528 /* We just processed one more parameter list. */
10529 ++parser->num_template_parameter_lists;
10532 /* The parameter-declaration-clause is optional (unless
10533 template-parameter-list was given), but must begin with an
10534 opening parenthesis if present. */
10535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10537 matching_parens parens;
10538 parens.consume_open (parser);
10540 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10542 /* Parse parameters. */
10543 param_list = cp_parser_parameter_declaration_clause (parser);
10545 /* Default arguments shall not be specified in the
10546 parameter-declaration-clause of a lambda-declarator. */
10547 if (cxx_dialect < cxx14)
10548 for (tree t = param_list; t; t = TREE_CHAIN (t))
10549 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10550 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10551 "default argument specified for lambda parameter");
10553 parens.require_close (parser);
10555 attributes = cp_parser_attributes_opt (parser);
10557 /* In the decl-specifier-seq of the lambda-declarator, each
10558 decl-specifier shall either be mutable or constexpr. */
10559 int declares_class_or_enum;
10560 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10561 cp_parser_decl_specifier_seq (parser,
10562 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10563 &lambda_specs, &declares_class_or_enum);
10564 if (lambda_specs.storage_class == sc_mutable)
10566 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10567 if (lambda_specs.conflicting_specifiers_p)
10568 error_at (lambda_specs.locations[ds_storage_class],
10569 "duplicate %<mutable%>");
10572 tx_qual = cp_parser_tx_qualifier_opt (parser);
10574 /* Parse optional exception specification. */
10575 exception_spec = cp_parser_exception_specification_opt (parser);
10577 /* Parse optional trailing return type. */
10578 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10580 cp_lexer_consume_token (parser->lexer);
10581 return_type = cp_parser_trailing_type_id (parser);
10584 /* The function parameters must be in scope all the way until after the
10585 trailing-return-type in case of decltype. */
10586 pop_bindings_and_leave_scope ();
10588 else if (template_param_list != NULL_TREE) // generate diagnostic
10589 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10591 /* Create the function call operator.
10593 Messing with declarators like this is no uglier than building up the
10594 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10595 other code. */
10597 cp_decl_specifier_seq return_type_specs;
10598 cp_declarator* declarator;
10599 tree fco;
10600 int quals;
10601 void *p;
10603 clear_decl_specs (&return_type_specs);
10604 return_type_specs.type = make_auto ();
10606 if (lambda_specs.locations[ds_constexpr])
10608 if (cxx_dialect >= cxx17)
10609 return_type_specs.locations[ds_constexpr]
10610 = lambda_specs.locations[ds_constexpr];
10611 else
10612 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10613 "lambda only available with -std=c++17 or -std=gnu++17");
10616 p = obstack_alloc (&declarator_obstack, 0);
10618 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10620 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10621 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10622 declarator = make_call_declarator (declarator, param_list, quals,
10623 VIRT_SPEC_UNSPECIFIED,
10624 REF_QUAL_NONE,
10625 tx_qual,
10626 exception_spec,
10627 /*late_return_type=*/NULL_TREE,
10628 /*requires_clause*/NULL_TREE);
10629 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10630 if (return_type)
10631 declarator->u.function.late_return_type = return_type;
10633 fco = grokmethod (&return_type_specs,
10634 declarator,
10635 attributes);
10636 if (fco != error_mark_node)
10638 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10639 DECL_ARTIFICIAL (fco) = 1;
10640 /* Give the object parameter a different name. */
10641 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10643 if (template_param_list)
10645 fco = finish_member_template_decl (fco);
10646 finish_template_decl (template_param_list);
10647 --parser->num_template_parameter_lists;
10649 else if (parser->fully_implicit_function_template_p)
10650 fco = finish_fully_implicit_template (parser, fco);
10652 finish_member_declaration (fco);
10654 obstack_free (&declarator_obstack, p);
10656 return (fco != error_mark_node);
10660 /* Parse the body of a lambda expression, which is simply
10662 compound-statement
10664 but which requires special handling.
10665 LAMBDA_EXPR is the current representation of the lambda expression. */
10667 static void
10668 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10670 bool nested = (current_function_decl != NULL_TREE);
10671 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10672 bool in_function_body = parser->in_function_body;
10674 if (nested)
10675 push_function_context ();
10676 else
10677 /* Still increment function_depth so that we don't GC in the
10678 middle of an expression. */
10679 ++function_depth;
10681 vec<tree> omp_privatization_save;
10682 save_omp_privatization_clauses (omp_privatization_save);
10683 /* Clear this in case we're in the middle of a default argument. */
10684 parser->local_variables_forbidden_p = false;
10685 parser->in_function_body = true;
10688 local_specialization_stack s (lss_copy);
10689 tree fco = lambda_function (lambda_expr);
10690 tree body = start_lambda_function (fco, lambda_expr);
10691 matching_braces braces;
10693 if (braces.require_open (parser))
10695 tree compound_stmt = begin_compound_stmt (0);
10697 /* Originally C++11 required us to peek for 'return expr'; and
10698 process it specially here to deduce the return type. N3638
10699 removed the need for that. */
10701 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10702 cp_parser_label_declaration (parser);
10703 cp_parser_statement_seq_opt (parser, NULL_TREE);
10704 braces.require_close (parser);
10706 finish_compound_stmt (compound_stmt);
10709 finish_lambda_function (body);
10712 restore_omp_privatization_clauses (omp_privatization_save);
10713 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10714 parser->in_function_body = in_function_body;
10715 if (nested)
10716 pop_function_context();
10717 else
10718 --function_depth;
10721 /* Statements [gram.stmt.stmt] */
10723 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10725 static void
10726 add_debug_begin_stmt (location_t loc)
10728 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10729 return;
10730 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10731 /* A concept is never expanded normally. */
10732 return;
10734 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10735 SET_EXPR_LOCATION (stmt, loc);
10736 add_stmt (stmt);
10739 /* Parse a statement.
10741 statement:
10742 labeled-statement
10743 expression-statement
10744 compound-statement
10745 selection-statement
10746 iteration-statement
10747 jump-statement
10748 declaration-statement
10749 try-block
10751 C++11:
10753 statement:
10754 labeled-statement
10755 attribute-specifier-seq (opt) expression-statement
10756 attribute-specifier-seq (opt) compound-statement
10757 attribute-specifier-seq (opt) selection-statement
10758 attribute-specifier-seq (opt) iteration-statement
10759 attribute-specifier-seq (opt) jump-statement
10760 declaration-statement
10761 attribute-specifier-seq (opt) try-block
10763 init-statement:
10764 expression-statement
10765 simple-declaration
10767 TM Extension:
10769 statement:
10770 atomic-statement
10772 IN_COMPOUND is true when the statement is nested inside a
10773 cp_parser_compound_statement; this matters for certain pragmas.
10775 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10776 is a (possibly labeled) if statement which is not enclosed in braces
10777 and has an else clause. This is used to implement -Wparentheses.
10779 CHAIN is a vector of if-else-if conditions. */
10781 static void
10782 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10783 bool in_compound, bool *if_p, vec<tree> *chain,
10784 location_t *loc_after_labels)
10786 tree statement, std_attrs = NULL_TREE;
10787 cp_token *token;
10788 location_t statement_location, attrs_location;
10790 restart:
10791 if (if_p != NULL)
10792 *if_p = false;
10793 /* There is no statement yet. */
10794 statement = NULL_TREE;
10796 saved_token_sentinel saved_tokens (parser->lexer);
10797 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10798 if (c_dialect_objc ())
10799 /* In obj-c++, seeing '[[' might be the either the beginning of
10800 c++11 attributes, or a nested objc-message-expression. So
10801 let's parse the c++11 attributes tentatively. */
10802 cp_parser_parse_tentatively (parser);
10803 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10804 if (c_dialect_objc ())
10806 if (!cp_parser_parse_definitely (parser))
10807 std_attrs = NULL_TREE;
10810 /* Peek at the next token. */
10811 token = cp_lexer_peek_token (parser->lexer);
10812 /* Remember the location of the first token in the statement. */
10813 statement_location = token->location;
10814 add_debug_begin_stmt (statement_location);
10815 /* If this is a keyword, then that will often determine what kind of
10816 statement we have. */
10817 if (token->type == CPP_KEYWORD)
10819 enum rid keyword = token->keyword;
10821 switch (keyword)
10823 case RID_CASE:
10824 case RID_DEFAULT:
10825 /* Looks like a labeled-statement with a case label.
10826 Parse the label, and then use tail recursion to parse
10827 the statement. */
10828 cp_parser_label_for_labeled_statement (parser, std_attrs);
10829 in_compound = false;
10830 goto restart;
10832 case RID_IF:
10833 case RID_SWITCH:
10834 statement = cp_parser_selection_statement (parser, if_p, chain);
10835 break;
10837 case RID_WHILE:
10838 case RID_DO:
10839 case RID_FOR:
10840 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10841 break;
10843 case RID_BREAK:
10844 case RID_CONTINUE:
10845 case RID_RETURN:
10846 case RID_GOTO:
10847 statement = cp_parser_jump_statement (parser);
10848 break;
10850 /* Objective-C++ exception-handling constructs. */
10851 case RID_AT_TRY:
10852 case RID_AT_CATCH:
10853 case RID_AT_FINALLY:
10854 case RID_AT_SYNCHRONIZED:
10855 case RID_AT_THROW:
10856 statement = cp_parser_objc_statement (parser);
10857 break;
10859 case RID_TRY:
10860 statement = cp_parser_try_block (parser);
10861 break;
10863 case RID_NAMESPACE:
10864 /* This must be a namespace alias definition. */
10865 cp_parser_declaration_statement (parser);
10866 return;
10868 case RID_TRANSACTION_ATOMIC:
10869 case RID_TRANSACTION_RELAXED:
10870 case RID_SYNCHRONIZED:
10871 case RID_ATOMIC_NOEXCEPT:
10872 case RID_ATOMIC_CANCEL:
10873 statement = cp_parser_transaction (parser, token);
10874 break;
10875 case RID_TRANSACTION_CANCEL:
10876 statement = cp_parser_transaction_cancel (parser);
10877 break;
10879 default:
10880 /* It might be a keyword like `int' that can start a
10881 declaration-statement. */
10882 break;
10885 else if (token->type == CPP_NAME)
10887 /* If the next token is a `:', then we are looking at a
10888 labeled-statement. */
10889 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10890 if (token->type == CPP_COLON)
10892 /* Looks like a labeled-statement with an ordinary label.
10893 Parse the label, and then use tail recursion to parse
10894 the statement. */
10896 cp_parser_label_for_labeled_statement (parser, std_attrs);
10897 in_compound = false;
10898 goto restart;
10901 /* Anything that starts with a `{' must be a compound-statement. */
10902 else if (token->type == CPP_OPEN_BRACE)
10903 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10904 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10905 a statement all its own. */
10906 else if (token->type == CPP_PRAGMA)
10908 /* Only certain OpenMP pragmas are attached to statements, and thus
10909 are considered statements themselves. All others are not. In
10910 the context of a compound, accept the pragma as a "statement" and
10911 return so that we can check for a close brace. Otherwise we
10912 require a real statement and must go back and read one. */
10913 if (in_compound)
10914 cp_parser_pragma (parser, pragma_compound, if_p);
10915 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10916 goto restart;
10917 return;
10919 else if (token->type == CPP_EOF)
10921 cp_parser_error (parser, "expected statement");
10922 return;
10925 /* Everything else must be a declaration-statement or an
10926 expression-statement. Try for the declaration-statement
10927 first, unless we are looking at a `;', in which case we know that
10928 we have an expression-statement. */
10929 if (!statement)
10931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10933 if (std_attrs != NULL_TREE)
10935 /* Attributes should be parsed as part of the the
10936 declaration, so let's un-parse them. */
10937 saved_tokens.rollback();
10938 std_attrs = NULL_TREE;
10941 cp_parser_parse_tentatively (parser);
10942 /* Try to parse the declaration-statement. */
10943 cp_parser_declaration_statement (parser);
10944 /* If that worked, we're done. */
10945 if (cp_parser_parse_definitely (parser))
10946 return;
10948 /* All preceding labels have been parsed at this point. */
10949 if (loc_after_labels != NULL)
10950 *loc_after_labels = statement_location;
10952 /* Look for an expression-statement instead. */
10953 statement = cp_parser_expression_statement (parser, in_statement_expr);
10955 /* Handle [[fallthrough]];. */
10956 if (attribute_fallthrough_p (std_attrs))
10958 /* The next token after the fallthrough attribute is ';'. */
10959 if (statement == NULL_TREE)
10961 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10962 statement = build_call_expr_internal_loc (statement_location,
10963 IFN_FALLTHROUGH,
10964 void_type_node, 0);
10965 finish_expr_stmt (statement);
10967 else
10968 warning_at (statement_location, OPT_Wattributes,
10969 "%<fallthrough%> attribute not followed by %<;%>");
10970 std_attrs = NULL_TREE;
10974 /* Set the line number for the statement. */
10975 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10976 SET_EXPR_LOCATION (statement, statement_location);
10978 /* Allow "[[fallthrough]];", but warn otherwise. */
10979 if (std_attrs != NULL_TREE)
10980 warning_at (attrs_location,
10981 OPT_Wattributes,
10982 "attributes at the beginning of statement are ignored");
10985 /* Append ATTR to attribute list ATTRS. */
10987 static tree
10988 attr_chainon (tree attrs, tree attr)
10990 if (attrs == error_mark_node)
10991 return error_mark_node;
10992 if (attr == error_mark_node)
10993 return error_mark_node;
10994 return chainon (attrs, attr);
10997 /* Parse the label for a labeled-statement, i.e.
10999 identifier :
11000 case constant-expression :
11001 default :
11003 GNU Extension:
11004 case constant-expression ... constant-expression : statement
11006 When a label is parsed without errors, the label is added to the
11007 parse tree by the finish_* functions, so this function doesn't
11008 have to return the label. */
11010 static void
11011 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11013 cp_token *token;
11014 tree label = NULL_TREE;
11015 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11017 /* The next token should be an identifier. */
11018 token = cp_lexer_peek_token (parser->lexer);
11019 if (token->type != CPP_NAME
11020 && token->type != CPP_KEYWORD)
11022 cp_parser_error (parser, "expected labeled-statement");
11023 return;
11026 /* Remember whether this case or a user-defined label is allowed to fall
11027 through to. */
11028 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11030 parser->colon_corrects_to_scope_p = false;
11031 switch (token->keyword)
11033 case RID_CASE:
11035 tree expr, expr_hi;
11036 cp_token *ellipsis;
11038 /* Consume the `case' token. */
11039 cp_lexer_consume_token (parser->lexer);
11040 /* Parse the constant-expression. */
11041 expr = cp_parser_constant_expression (parser);
11042 if (check_for_bare_parameter_packs (expr))
11043 expr = error_mark_node;
11045 ellipsis = cp_lexer_peek_token (parser->lexer);
11046 if (ellipsis->type == CPP_ELLIPSIS)
11048 /* Consume the `...' token. */
11049 cp_lexer_consume_token (parser->lexer);
11050 expr_hi = cp_parser_constant_expression (parser);
11051 if (check_for_bare_parameter_packs (expr_hi))
11052 expr_hi = error_mark_node;
11054 /* We don't need to emit warnings here, as the common code
11055 will do this for us. */
11057 else
11058 expr_hi = NULL_TREE;
11060 if (parser->in_switch_statement_p)
11062 tree l = finish_case_label (token->location, expr, expr_hi);
11063 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11064 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11066 else
11067 error_at (token->location,
11068 "case label %qE not within a switch statement",
11069 expr);
11071 break;
11073 case RID_DEFAULT:
11074 /* Consume the `default' token. */
11075 cp_lexer_consume_token (parser->lexer);
11077 if (parser->in_switch_statement_p)
11079 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11080 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11081 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11083 else
11084 error_at (token->location, "case label not within a switch statement");
11085 break;
11087 default:
11088 /* Anything else must be an ordinary label. */
11089 label = finish_label_stmt (cp_parser_identifier (parser));
11090 if (label && TREE_CODE (label) == LABEL_DECL)
11091 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11092 break;
11095 /* Require the `:' token. */
11096 cp_parser_require (parser, CPP_COLON, RT_COLON);
11098 /* An ordinary label may optionally be followed by attributes.
11099 However, this is only permitted if the attributes are then
11100 followed by a semicolon. This is because, for backward
11101 compatibility, when parsing
11102 lab: __attribute__ ((unused)) int i;
11103 we want the attribute to attach to "i", not "lab". */
11104 if (label != NULL_TREE
11105 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11107 tree attrs;
11108 cp_parser_parse_tentatively (parser);
11109 attrs = cp_parser_gnu_attributes_opt (parser);
11110 if (attrs == NULL_TREE
11111 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11112 cp_parser_abort_tentative_parse (parser);
11113 else if (!cp_parser_parse_definitely (parser))
11115 else
11116 attributes = attr_chainon (attributes, attrs);
11119 if (attributes != NULL_TREE)
11120 cplus_decl_attributes (&label, attributes, 0);
11122 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11125 /* Parse an expression-statement.
11127 expression-statement:
11128 expression [opt] ;
11130 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11131 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11132 indicates whether this expression-statement is part of an
11133 expression statement. */
11135 static tree
11136 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11138 tree statement = NULL_TREE;
11139 cp_token *token = cp_lexer_peek_token (parser->lexer);
11140 location_t loc = token->location;
11142 /* There might be attribute fallthrough. */
11143 tree attr = cp_parser_gnu_attributes_opt (parser);
11145 /* If the next token is a ';', then there is no expression
11146 statement. */
11147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11149 statement = cp_parser_expression (parser);
11150 if (statement == error_mark_node
11151 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11153 cp_parser_skip_to_end_of_block_or_statement (parser);
11154 return error_mark_node;
11158 /* Handle [[fallthrough]];. */
11159 if (attribute_fallthrough_p (attr))
11161 /* The next token after the fallthrough attribute is ';'. */
11162 if (statement == NULL_TREE)
11163 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11164 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11165 void_type_node, 0);
11166 else
11167 warning_at (loc, OPT_Wattributes,
11168 "%<fallthrough%> attribute not followed by %<;%>");
11169 attr = NULL_TREE;
11172 /* Allow "[[fallthrough]];", but warn otherwise. */
11173 if (attr != NULL_TREE)
11174 warning_at (loc, OPT_Wattributes,
11175 "attributes at the beginning of statement are ignored");
11177 /* Give a helpful message for "A<T>::type t;" and the like. */
11178 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11179 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11181 if (TREE_CODE (statement) == SCOPE_REF)
11182 error_at (token->location, "need %<typename%> before %qE because "
11183 "%qT is a dependent scope",
11184 statement, TREE_OPERAND (statement, 0));
11185 else if (is_overloaded_fn (statement)
11186 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11188 /* A::A a; */
11189 tree fn = get_first_fn (statement);
11190 error_at (token->location,
11191 "%<%T::%D%> names the constructor, not the type",
11192 DECL_CONTEXT (fn), DECL_NAME (fn));
11196 /* Consume the final `;'. */
11197 cp_parser_consume_semicolon_at_end_of_statement (parser);
11199 if (in_statement_expr
11200 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11201 /* This is the final expression statement of a statement
11202 expression. */
11203 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11204 else if (statement)
11205 statement = finish_expr_stmt (statement);
11207 return statement;
11210 /* Parse a compound-statement.
11212 compound-statement:
11213 { statement-seq [opt] }
11215 GNU extension:
11217 compound-statement:
11218 { label-declaration-seq [opt] statement-seq [opt] }
11220 label-declaration-seq:
11221 label-declaration
11222 label-declaration-seq label-declaration
11224 Returns a tree representing the statement. */
11226 static tree
11227 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11228 int bcs_flags, bool function_body)
11230 tree compound_stmt;
11231 matching_braces braces;
11233 /* Consume the `{'. */
11234 if (!braces.require_open (parser))
11235 return error_mark_node;
11236 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11237 && !function_body && cxx_dialect < cxx14)
11238 pedwarn (input_location, OPT_Wpedantic,
11239 "compound-statement in %<constexpr%> function");
11240 /* Begin the compound-statement. */
11241 compound_stmt = begin_compound_stmt (bcs_flags);
11242 /* If the next keyword is `__label__' we have a label declaration. */
11243 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11244 cp_parser_label_declaration (parser);
11245 /* Parse an (optional) statement-seq. */
11246 cp_parser_statement_seq_opt (parser, in_statement_expr);
11247 /* Finish the compound-statement. */
11248 finish_compound_stmt (compound_stmt);
11249 /* Consume the `}'. */
11250 braces.require_close (parser);
11252 return compound_stmt;
11255 /* Parse an (optional) statement-seq.
11257 statement-seq:
11258 statement
11259 statement-seq [opt] statement */
11261 static void
11262 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11264 /* Scan statements until there aren't any more. */
11265 while (true)
11267 cp_token *token = cp_lexer_peek_token (parser->lexer);
11269 /* If we are looking at a `}', then we have run out of
11270 statements; the same is true if we have reached the end
11271 of file, or have stumbled upon a stray '@end'. */
11272 if (token->type == CPP_CLOSE_BRACE
11273 || token->type == CPP_EOF
11274 || token->type == CPP_PRAGMA_EOL
11275 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11276 break;
11278 /* If we are in a compound statement and find 'else' then
11279 something went wrong. */
11280 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11282 if (parser->in_statement & IN_IF_STMT)
11283 break;
11284 else
11286 token = cp_lexer_consume_token (parser->lexer);
11287 error_at (token->location, "%<else%> without a previous %<if%>");
11291 /* Parse the statement. */
11292 cp_parser_statement (parser, in_statement_expr, true, NULL);
11296 /* Return true if this is the C++20 version of range-based-for with
11297 init-statement. */
11299 static bool
11300 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11302 bool r = false;
11304 /* Save tokens so that we can put them back. */
11305 cp_lexer_save_tokens (parser->lexer);
11307 /* There has to be an unnested ; followed by an unnested :. */
11308 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11309 /*recovering=*/false,
11310 CPP_SEMICOLON,
11311 /*consume_paren=*/false) != -1)
11312 goto out;
11314 /* We found the semicolon, eat it now. */
11315 cp_lexer_consume_token (parser->lexer);
11317 /* Now look for ':' that is not nested in () or {}. */
11318 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11319 /*recovering=*/false,
11320 CPP_COLON,
11321 /*consume_paren=*/false) == -1);
11323 out:
11324 /* Roll back the tokens we skipped. */
11325 cp_lexer_rollback_tokens (parser->lexer);
11327 return r;
11330 /* Return true if we're looking at (init; cond), false otherwise. */
11332 static bool
11333 cp_parser_init_statement_p (cp_parser *parser)
11335 /* Save tokens so that we can put them back. */
11336 cp_lexer_save_tokens (parser->lexer);
11338 /* Look for ';' that is not nested in () or {}. */
11339 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11340 /*recovering=*/false,
11341 CPP_SEMICOLON,
11342 /*consume_paren=*/false);
11344 /* Roll back the tokens we skipped. */
11345 cp_lexer_rollback_tokens (parser->lexer);
11347 return ret == -1;
11350 /* Parse a selection-statement.
11352 selection-statement:
11353 if ( init-statement [opt] condition ) statement
11354 if ( init-statement [opt] condition ) statement else statement
11355 switch ( init-statement [opt] condition ) statement
11357 Returns the new IF_STMT or SWITCH_STMT.
11359 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11360 is a (possibly labeled) if statement which is not enclosed in
11361 braces and has an else clause. This is used to implement
11362 -Wparentheses.
11364 CHAIN is a vector of if-else-if conditions. This is used to implement
11365 -Wduplicated-cond. */
11367 static tree
11368 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11369 vec<tree> *chain)
11371 cp_token *token;
11372 enum rid keyword;
11373 token_indent_info guard_tinfo;
11375 if (if_p != NULL)
11376 *if_p = false;
11378 /* Peek at the next token. */
11379 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11380 guard_tinfo = get_token_indent_info (token);
11382 /* See what kind of keyword it is. */
11383 keyword = token->keyword;
11384 switch (keyword)
11386 case RID_IF:
11387 case RID_SWITCH:
11389 tree statement;
11390 tree condition;
11392 bool cx = false;
11393 if (keyword == RID_IF
11394 && cp_lexer_next_token_is_keyword (parser->lexer,
11395 RID_CONSTEXPR))
11397 cx = true;
11398 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11399 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11400 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11401 "with -std=c++17 or -std=gnu++17");
11404 /* Look for the `('. */
11405 matching_parens parens;
11406 if (!parens.require_open (parser))
11408 cp_parser_skip_to_end_of_statement (parser);
11409 return error_mark_node;
11412 /* Begin the selection-statement. */
11413 if (keyword == RID_IF)
11415 statement = begin_if_stmt ();
11416 IF_STMT_CONSTEXPR_P (statement) = cx;
11418 else
11419 statement = begin_switch_stmt ();
11421 /* Parse the optional init-statement. */
11422 if (cp_parser_init_statement_p (parser))
11424 tree decl;
11425 if (cxx_dialect < cxx17)
11426 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11427 "init-statement in selection statements only available "
11428 "with -std=c++17 or -std=gnu++17");
11429 cp_parser_init_statement (parser, &decl);
11432 /* Parse the condition. */
11433 condition = cp_parser_condition (parser);
11434 /* Look for the `)'. */
11435 if (!parens.require_close (parser))
11436 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11437 /*consume_paren=*/true);
11439 if (keyword == RID_IF)
11441 bool nested_if;
11442 unsigned char in_statement;
11444 /* Add the condition. */
11445 condition = finish_if_stmt_cond (condition, statement);
11447 if (warn_duplicated_cond)
11448 warn_duplicated_cond_add_or_warn (token->location, condition,
11449 &chain);
11451 /* Parse the then-clause. */
11452 in_statement = parser->in_statement;
11453 parser->in_statement |= IN_IF_STMT;
11455 /* Outside a template, the non-selected branch of a constexpr
11456 if is a 'discarded statement', i.e. unevaluated. */
11457 bool was_discarded = in_discarded_stmt;
11458 bool discard_then = (cx && !processing_template_decl
11459 && integer_zerop (condition));
11460 if (discard_then)
11462 in_discarded_stmt = true;
11463 ++c_inhibit_evaluation_warnings;
11466 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11467 guard_tinfo);
11469 parser->in_statement = in_statement;
11471 finish_then_clause (statement);
11473 if (discard_then)
11475 THEN_CLAUSE (statement) = NULL_TREE;
11476 in_discarded_stmt = was_discarded;
11477 --c_inhibit_evaluation_warnings;
11480 /* If the next token is `else', parse the else-clause. */
11481 if (cp_lexer_next_token_is_keyword (parser->lexer,
11482 RID_ELSE))
11484 bool discard_else = (cx && !processing_template_decl
11485 && integer_nonzerop (condition));
11486 if (discard_else)
11488 in_discarded_stmt = true;
11489 ++c_inhibit_evaluation_warnings;
11492 guard_tinfo
11493 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11494 /* Consume the `else' keyword. */
11495 cp_lexer_consume_token (parser->lexer);
11496 if (warn_duplicated_cond)
11498 if (cp_lexer_next_token_is_keyword (parser->lexer,
11499 RID_IF)
11500 && chain == NULL)
11502 /* We've got "if (COND) else if (COND2)". Start
11503 the condition chain and add COND as the first
11504 element. */
11505 chain = new vec<tree> ();
11506 if (!CONSTANT_CLASS_P (condition)
11507 && !TREE_SIDE_EFFECTS (condition))
11509 /* Wrap it in a NOP_EXPR so that we can set the
11510 location of the condition. */
11511 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11512 condition);
11513 SET_EXPR_LOCATION (e, token->location);
11514 chain->safe_push (e);
11517 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11518 RID_IF))
11520 /* This is if-else without subsequent if. Zap the
11521 condition chain; we would have already warned at
11522 this point. */
11523 delete chain;
11524 chain = NULL;
11527 begin_else_clause (statement);
11528 /* Parse the else-clause. */
11529 cp_parser_implicitly_scoped_statement (parser, NULL,
11530 guard_tinfo, chain);
11532 finish_else_clause (statement);
11534 /* If we are currently parsing a then-clause, then
11535 IF_P will not be NULL. We set it to true to
11536 indicate that this if statement has an else clause.
11537 This may trigger the Wparentheses warning below
11538 when we get back up to the parent if statement. */
11539 if (if_p != NULL)
11540 *if_p = true;
11542 if (discard_else)
11544 ELSE_CLAUSE (statement) = NULL_TREE;
11545 in_discarded_stmt = was_discarded;
11546 --c_inhibit_evaluation_warnings;
11549 else
11551 /* This if statement does not have an else clause. If
11552 NESTED_IF is true, then the then-clause has an if
11553 statement which does have an else clause. We warn
11554 about the potential ambiguity. */
11555 if (nested_if)
11556 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11557 "suggest explicit braces to avoid ambiguous"
11558 " %<else%>");
11559 if (warn_duplicated_cond)
11561 /* We don't need the condition chain anymore. */
11562 delete chain;
11563 chain = NULL;
11567 /* Now we're all done with the if-statement. */
11568 finish_if_stmt (statement);
11570 else
11572 bool in_switch_statement_p;
11573 unsigned char in_statement;
11575 /* Add the condition. */
11576 finish_switch_cond (condition, statement);
11578 /* Parse the body of the switch-statement. */
11579 in_switch_statement_p = parser->in_switch_statement_p;
11580 in_statement = parser->in_statement;
11581 parser->in_switch_statement_p = true;
11582 parser->in_statement |= IN_SWITCH_STMT;
11583 cp_parser_implicitly_scoped_statement (parser, if_p,
11584 guard_tinfo);
11585 parser->in_switch_statement_p = in_switch_statement_p;
11586 parser->in_statement = in_statement;
11588 /* Now we're all done with the switch-statement. */
11589 finish_switch_stmt (statement);
11592 return statement;
11594 break;
11596 default:
11597 cp_parser_error (parser, "expected selection-statement");
11598 return error_mark_node;
11602 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11603 If we have seen at least one decl-specifier, and the next token
11604 is not a parenthesis, then we must be looking at a declaration.
11605 (After "int (" we might be looking at a functional cast.) */
11607 static void
11608 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11609 bool any_specifiers_p)
11611 if (any_specifiers_p
11612 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11613 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11614 && !cp_parser_error_occurred (parser))
11615 cp_parser_commit_to_tentative_parse (parser);
11618 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11619 The declarator shall not specify a function or an array. Returns
11620 TRUE if the declarator is valid, FALSE otherwise. */
11622 static bool
11623 cp_parser_check_condition_declarator (cp_parser* parser,
11624 cp_declarator *declarator,
11625 location_t loc)
11627 if (declarator == cp_error_declarator
11628 || function_declarator_p (declarator)
11629 || declarator->kind == cdk_array)
11631 if (declarator == cp_error_declarator)
11632 /* Already complained. */;
11633 else if (declarator->kind == cdk_array)
11634 error_at (loc, "condition declares an array");
11635 else
11636 error_at (loc, "condition declares a function");
11637 if (parser->fully_implicit_function_template_p)
11638 abort_fully_implicit_template (parser);
11639 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11640 /*or_comma=*/false,
11641 /*consume_paren=*/false);
11642 return false;
11644 else
11645 return true;
11648 /* Parse a condition.
11650 condition:
11651 expression
11652 type-specifier-seq declarator = initializer-clause
11653 type-specifier-seq declarator braced-init-list
11655 GNU Extension:
11657 condition:
11658 type-specifier-seq declarator asm-specification [opt]
11659 attributes [opt] = assignment-expression
11661 Returns the expression that should be tested. */
11663 static tree
11664 cp_parser_condition (cp_parser* parser)
11666 cp_decl_specifier_seq type_specifiers;
11667 const char *saved_message;
11668 int declares_class_or_enum;
11670 /* Try the declaration first. */
11671 cp_parser_parse_tentatively (parser);
11672 /* New types are not allowed in the type-specifier-seq for a
11673 condition. */
11674 saved_message = parser->type_definition_forbidden_message;
11675 parser->type_definition_forbidden_message
11676 = G_("types may not be defined in conditions");
11677 /* Parse the type-specifier-seq. */
11678 cp_parser_decl_specifier_seq (parser,
11679 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11680 &type_specifiers,
11681 &declares_class_or_enum);
11682 /* Restore the saved message. */
11683 parser->type_definition_forbidden_message = saved_message;
11685 cp_parser_maybe_commit_to_declaration (parser,
11686 type_specifiers.any_specifiers_p);
11688 /* If all is well, we might be looking at a declaration. */
11689 if (!cp_parser_error_occurred (parser))
11691 tree decl;
11692 tree asm_specification;
11693 tree attributes;
11694 cp_declarator *declarator;
11695 tree initializer = NULL_TREE;
11696 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11698 /* Parse the declarator. */
11699 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11700 /*ctor_dtor_or_conv_p=*/NULL,
11701 /*parenthesized_p=*/NULL,
11702 /*member_p=*/false,
11703 /*friend_p=*/false);
11704 /* Parse the attributes. */
11705 attributes = cp_parser_attributes_opt (parser);
11706 /* Parse the asm-specification. */
11707 asm_specification = cp_parser_asm_specification_opt (parser);
11708 /* If the next token is not an `=' or '{', then we might still be
11709 looking at an expression. For example:
11711 if (A(a).x)
11713 looks like a decl-specifier-seq and a declarator -- but then
11714 there is no `=', so this is an expression. */
11715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11716 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11717 cp_parser_simulate_error (parser);
11719 /* If we did see an `=' or '{', then we are looking at a declaration
11720 for sure. */
11721 if (cp_parser_parse_definitely (parser))
11723 tree pushed_scope;
11724 bool non_constant_p = false;
11725 int flags = LOOKUP_ONLYCONVERTING;
11727 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11728 return error_mark_node;
11730 /* Create the declaration. */
11731 decl = start_decl (declarator, &type_specifiers,
11732 /*initialized_p=*/true,
11733 attributes, /*prefix_attributes=*/NULL_TREE,
11734 &pushed_scope);
11736 /* Parse the initializer. */
11737 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11739 initializer = cp_parser_braced_list (parser, &non_constant_p);
11740 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11741 flags = 0;
11743 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11745 /* Consume the `='. */
11746 cp_lexer_consume_token (parser->lexer);
11747 initializer = cp_parser_initializer_clause (parser,
11748 &non_constant_p);
11750 else
11752 cp_parser_error (parser, "expected initializer");
11753 initializer = error_mark_node;
11755 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11756 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11758 /* Process the initializer. */
11759 cp_finish_decl (decl,
11760 initializer, !non_constant_p,
11761 asm_specification,
11762 flags);
11764 if (pushed_scope)
11765 pop_scope (pushed_scope);
11767 return convert_from_reference (decl);
11770 /* If we didn't even get past the declarator successfully, we are
11771 definitely not looking at a declaration. */
11772 else
11773 cp_parser_abort_tentative_parse (parser);
11775 /* Otherwise, we are looking at an expression. */
11776 return cp_parser_expression (parser);
11779 /* Parses a for-statement or range-for-statement until the closing ')',
11780 not included. */
11782 static tree
11783 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11785 tree init, scope, decl;
11786 bool is_range_for;
11788 /* Begin the for-statement. */
11789 scope = begin_for_scope (&init);
11791 /* Parse the initialization. */
11792 is_range_for = cp_parser_init_statement (parser, &decl);
11794 if (is_range_for)
11795 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11796 else
11797 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11800 static tree
11801 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11802 unsigned short unroll)
11804 /* Normal for loop */
11805 tree condition = NULL_TREE;
11806 tree expression = NULL_TREE;
11807 tree stmt;
11809 stmt = begin_for_stmt (scope, init);
11810 /* The init-statement has already been parsed in
11811 cp_parser_init_statement, so no work is needed here. */
11812 finish_init_stmt (stmt);
11814 /* If there's a condition, process it. */
11815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11816 condition = cp_parser_condition (parser);
11817 else if (ivdep)
11819 cp_parser_error (parser, "missing loop condition in loop with "
11820 "%<GCC ivdep%> pragma");
11821 condition = error_mark_node;
11823 else if (unroll)
11825 cp_parser_error (parser, "missing loop condition in loop with "
11826 "%<GCC unroll%> pragma");
11827 condition = error_mark_node;
11829 finish_for_cond (condition, stmt, ivdep, unroll);
11830 /* Look for the `;'. */
11831 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11833 /* If there's an expression, process it. */
11834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11835 expression = cp_parser_expression (parser);
11836 finish_for_expr (expression, stmt);
11838 return stmt;
11841 /* Tries to parse a range-based for-statement:
11843 range-based-for:
11844 decl-specifier-seq declarator : expression
11846 The decl-specifier-seq declarator and the `:' are already parsed by
11847 cp_parser_init_statement. If processing_template_decl it returns a
11848 newly created RANGE_FOR_STMT; if not, it is converted to a
11849 regular FOR_STMT. */
11851 static tree
11852 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11853 bool ivdep, unsigned short unroll)
11855 tree stmt, range_expr;
11856 auto_vec <cxx_binding *, 16> bindings;
11857 auto_vec <tree, 16> names;
11858 tree decomp_first_name = NULL_TREE;
11859 unsigned int decomp_cnt = 0;
11861 /* Get the range declaration momentarily out of the way so that
11862 the range expression doesn't clash with it. */
11863 if (range_decl != error_mark_node)
11865 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11867 tree v = DECL_VALUE_EXPR (range_decl);
11868 /* For decomposition declaration get all of the corresponding
11869 declarations out of the way. */
11870 if (TREE_CODE (v) == ARRAY_REF
11871 && VAR_P (TREE_OPERAND (v, 0))
11872 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11874 tree d = range_decl;
11875 range_decl = TREE_OPERAND (v, 0);
11876 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11877 decomp_first_name = d;
11878 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11880 tree name = DECL_NAME (d);
11881 names.safe_push (name);
11882 bindings.safe_push (IDENTIFIER_BINDING (name));
11883 IDENTIFIER_BINDING (name)
11884 = IDENTIFIER_BINDING (name)->previous;
11888 if (names.is_empty ())
11890 tree name = DECL_NAME (range_decl);
11891 names.safe_push (name);
11892 bindings.safe_push (IDENTIFIER_BINDING (name));
11893 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11899 bool expr_non_constant_p;
11900 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11902 else
11903 range_expr = cp_parser_expression (parser);
11905 /* Put the range declaration(s) back into scope. */
11906 for (unsigned int i = 0; i < names.length (); i++)
11908 cxx_binding *binding = bindings[i];
11909 binding->previous = IDENTIFIER_BINDING (names[i]);
11910 IDENTIFIER_BINDING (names[i]) = binding;
11913 /* If in template, STMT is converted to a normal for-statement
11914 at instantiation. If not, it is done just ahead. */
11915 if (processing_template_decl)
11917 if (check_for_bare_parameter_packs (range_expr))
11918 range_expr = error_mark_node;
11919 stmt = begin_range_for_stmt (scope, init);
11920 if (ivdep)
11921 RANGE_FOR_IVDEP (stmt) = 1;
11922 if (unroll)
11923 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11924 finish_range_for_decl (stmt, range_decl, range_expr);
11925 if (!type_dependent_expression_p (range_expr)
11926 /* do_auto_deduction doesn't mess with template init-lists. */
11927 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11928 do_range_for_auto_deduction (range_decl, range_expr);
11930 else
11932 stmt = begin_for_stmt (scope, init);
11933 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11934 decomp_first_name, decomp_cnt, ivdep,
11935 unroll);
11937 return stmt;
11940 /* Subroutine of cp_convert_range_for: given the initializer expression,
11941 builds up the range temporary. */
11943 static tree
11944 build_range_temp (tree range_expr)
11946 tree range_type, range_temp;
11948 /* Find out the type deduced by the declaration
11949 `auto &&__range = range_expr'. */
11950 range_type = cp_build_reference_type (make_auto (), true);
11951 range_type = do_auto_deduction (range_type, range_expr,
11952 type_uses_auto (range_type));
11954 /* Create the __range variable. */
11955 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
11956 range_type);
11957 TREE_USED (range_temp) = 1;
11958 DECL_ARTIFICIAL (range_temp) = 1;
11960 return range_temp;
11963 /* Used by cp_parser_range_for in template context: we aren't going to
11964 do a full conversion yet, but we still need to resolve auto in the
11965 type of the for-range-declaration if present. This is basically
11966 a shortcut version of cp_convert_range_for. */
11968 static void
11969 do_range_for_auto_deduction (tree decl, tree range_expr)
11971 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11972 if (auto_node)
11974 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11975 range_temp = convert_from_reference (build_range_temp (range_expr));
11976 iter_type = (cp_parser_perform_range_for_lookup
11977 (range_temp, &begin_dummy, &end_dummy));
11978 if (iter_type)
11980 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11981 iter_type);
11982 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11983 RO_UNARY_STAR,
11984 tf_warning_or_error);
11985 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11986 iter_decl, auto_node);
11991 /* Converts a range-based for-statement into a normal
11992 for-statement, as per the definition.
11994 for (RANGE_DECL : RANGE_EXPR)
11995 BLOCK
11997 should be equivalent to:
12000 auto &&__range = RANGE_EXPR;
12001 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12002 __begin != __end;
12003 ++__begin)
12005 RANGE_DECL = *__begin;
12006 BLOCK
12010 If RANGE_EXPR is an array:
12011 BEGIN_EXPR = __range
12012 END_EXPR = __range + ARRAY_SIZE(__range)
12013 Else if RANGE_EXPR has a member 'begin' or 'end':
12014 BEGIN_EXPR = __range.begin()
12015 END_EXPR = __range.end()
12016 Else:
12017 BEGIN_EXPR = begin(__range)
12018 END_EXPR = end(__range);
12020 If __range has a member 'begin' but not 'end', or vice versa, we must
12021 still use the second alternative (it will surely fail, however).
12022 When calling begin()/end() in the third alternative we must use
12023 argument dependent lookup, but always considering 'std' as an associated
12024 namespace. */
12026 tree
12027 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12028 tree decomp_first_name, unsigned int decomp_cnt,
12029 bool ivdep, unsigned short unroll)
12031 tree begin, end;
12032 tree iter_type, begin_expr, end_expr;
12033 tree condition, expression;
12035 range_expr = mark_lvalue_use (range_expr);
12037 if (range_decl == error_mark_node || range_expr == error_mark_node)
12038 /* If an error happened previously do nothing or else a lot of
12039 unhelpful errors would be issued. */
12040 begin_expr = end_expr = iter_type = error_mark_node;
12041 else
12043 tree range_temp;
12045 if (VAR_P (range_expr)
12046 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12047 /* Can't bind a reference to an array of runtime bound. */
12048 range_temp = range_expr;
12049 else
12051 range_temp = build_range_temp (range_expr);
12052 pushdecl (range_temp);
12053 cp_finish_decl (range_temp, range_expr,
12054 /*is_constant_init*/false, NULL_TREE,
12055 LOOKUP_ONLYCONVERTING);
12056 range_temp = convert_from_reference (range_temp);
12058 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12059 &begin_expr, &end_expr);
12062 /* The new for initialization statement. */
12063 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12064 iter_type);
12065 TREE_USED (begin) = 1;
12066 DECL_ARTIFICIAL (begin) = 1;
12067 pushdecl (begin);
12068 cp_finish_decl (begin, begin_expr,
12069 /*is_constant_init*/false, NULL_TREE,
12070 LOOKUP_ONLYCONVERTING);
12072 if (cxx_dialect >= cxx17)
12073 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12074 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12075 TREE_USED (end) = 1;
12076 DECL_ARTIFICIAL (end) = 1;
12077 pushdecl (end);
12078 cp_finish_decl (end, end_expr,
12079 /*is_constant_init*/false, NULL_TREE,
12080 LOOKUP_ONLYCONVERTING);
12082 finish_init_stmt (statement);
12084 /* The new for condition. */
12085 condition = build_x_binary_op (input_location, NE_EXPR,
12086 begin, ERROR_MARK,
12087 end, ERROR_MARK,
12088 NULL, tf_warning_or_error);
12089 finish_for_cond (condition, statement, ivdep, unroll);
12091 /* The new increment expression. */
12092 expression = finish_unary_op_expr (input_location,
12093 PREINCREMENT_EXPR, begin,
12094 tf_warning_or_error);
12095 finish_for_expr (expression, statement);
12097 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12098 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12100 /* The declaration is initialized with *__begin inside the loop body. */
12101 cp_finish_decl (range_decl,
12102 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12103 tf_warning_or_error),
12104 /*is_constant_init*/false, NULL_TREE,
12105 LOOKUP_ONLYCONVERTING);
12106 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12107 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12109 return statement;
12112 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12113 We need to solve both at the same time because the method used
12114 depends on the existence of members begin or end.
12115 Returns the type deduced for the iterator expression. */
12117 static tree
12118 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12120 if (error_operand_p (range))
12122 *begin = *end = error_mark_node;
12123 return error_mark_node;
12126 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12128 error ("range-based %<for%> expression of type %qT "
12129 "has incomplete type", TREE_TYPE (range));
12130 *begin = *end = error_mark_node;
12131 return error_mark_node;
12133 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12135 /* If RANGE is an array, we will use pointer arithmetic. */
12136 *begin = decay_conversion (range, tf_warning_or_error);
12137 *end = build_binary_op (input_location, PLUS_EXPR,
12138 range,
12139 array_type_nelts_top (TREE_TYPE (range)),
12140 false);
12141 return TREE_TYPE (*begin);
12143 else
12145 /* If it is not an array, we must do a bit of magic. */
12146 tree id_begin, id_end;
12147 tree member_begin, member_end;
12149 *begin = *end = error_mark_node;
12151 id_begin = get_identifier ("begin");
12152 id_end = get_identifier ("end");
12153 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12154 /*protect=*/2, /*want_type=*/false,
12155 tf_warning_or_error);
12156 member_end = lookup_member (TREE_TYPE (range), id_end,
12157 /*protect=*/2, /*want_type=*/false,
12158 tf_warning_or_error);
12160 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12162 /* Use the member functions. */
12163 *begin = cp_parser_range_for_member_function (range, id_begin);
12164 *end = cp_parser_range_for_member_function (range, id_end);
12166 else
12168 /* Use global functions with ADL. */
12169 vec<tree, va_gc> *vec;
12170 vec = make_tree_vector ();
12172 vec_safe_push (vec, range);
12174 member_begin = perform_koenig_lookup (id_begin, vec,
12175 tf_warning_or_error);
12176 *begin = finish_call_expr (member_begin, &vec, false, true,
12177 tf_warning_or_error);
12178 member_end = perform_koenig_lookup (id_end, vec,
12179 tf_warning_or_error);
12180 *end = finish_call_expr (member_end, &vec, false, true,
12181 tf_warning_or_error);
12183 release_tree_vector (vec);
12186 /* Last common checks. */
12187 if (*begin == error_mark_node || *end == error_mark_node)
12189 /* If one of the expressions is an error do no more checks. */
12190 *begin = *end = error_mark_node;
12191 return error_mark_node;
12193 else if (type_dependent_expression_p (*begin)
12194 || type_dependent_expression_p (*end))
12195 /* Can happen, when, eg, in a template context, Koenig lookup
12196 can't resolve begin/end (c++/58503). */
12197 return NULL_TREE;
12198 else
12200 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12201 /* The unqualified type of the __begin and __end temporaries should
12202 be the same, as required by the multiple auto declaration. */
12203 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12205 if (cxx_dialect >= cxx17
12206 && (build_x_binary_op (input_location, NE_EXPR,
12207 *begin, ERROR_MARK,
12208 *end, ERROR_MARK,
12209 NULL, tf_none)
12210 != error_mark_node))
12211 /* P0184R0 allows __begin and __end to have different types,
12212 but make sure they are comparable so we can give a better
12213 diagnostic. */;
12214 else
12215 error ("inconsistent begin/end types in range-based %<for%> "
12216 "statement: %qT and %qT",
12217 TREE_TYPE (*begin), TREE_TYPE (*end));
12219 return iter_type;
12224 /* Helper function for cp_parser_perform_range_for_lookup.
12225 Builds a tree for RANGE.IDENTIFIER(). */
12227 static tree
12228 cp_parser_range_for_member_function (tree range, tree identifier)
12230 tree member, res;
12231 vec<tree, va_gc> *vec;
12233 member = finish_class_member_access_expr (range, identifier,
12234 false, tf_warning_or_error);
12235 if (member == error_mark_node)
12236 return error_mark_node;
12238 vec = make_tree_vector ();
12239 res = finish_call_expr (member, &vec,
12240 /*disallow_virtual=*/false,
12241 /*koenig_p=*/false,
12242 tf_warning_or_error);
12243 release_tree_vector (vec);
12244 return res;
12247 /* Parse an iteration-statement.
12249 iteration-statement:
12250 while ( condition ) statement
12251 do statement while ( expression ) ;
12252 for ( init-statement condition [opt] ; expression [opt] )
12253 statement
12255 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12257 static tree
12258 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12259 unsigned short unroll)
12261 cp_token *token;
12262 enum rid keyword;
12263 tree statement;
12264 unsigned char in_statement;
12265 token_indent_info guard_tinfo;
12267 /* Peek at the next token. */
12268 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12269 if (!token)
12270 return error_mark_node;
12272 guard_tinfo = get_token_indent_info (token);
12274 /* Remember whether or not we are already within an iteration
12275 statement. */
12276 in_statement = parser->in_statement;
12278 /* See what kind of keyword it is. */
12279 keyword = token->keyword;
12280 switch (keyword)
12282 case RID_WHILE:
12284 tree condition;
12286 /* Begin the while-statement. */
12287 statement = begin_while_stmt ();
12288 /* Look for the `('. */
12289 matching_parens parens;
12290 parens.require_open (parser);
12291 /* Parse the condition. */
12292 condition = cp_parser_condition (parser);
12293 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12294 /* Look for the `)'. */
12295 parens.require_close (parser);
12296 /* Parse the dependent statement. */
12297 parser->in_statement = IN_ITERATION_STMT;
12298 bool prev = note_iteration_stmt_body_start ();
12299 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12300 note_iteration_stmt_body_end (prev);
12301 parser->in_statement = in_statement;
12302 /* We're done with the while-statement. */
12303 finish_while_stmt (statement);
12305 break;
12307 case RID_DO:
12309 tree expression;
12311 /* Begin the do-statement. */
12312 statement = begin_do_stmt ();
12313 /* Parse the body of the do-statement. */
12314 parser->in_statement = IN_ITERATION_STMT;
12315 bool prev = note_iteration_stmt_body_start ();
12316 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12317 note_iteration_stmt_body_end (prev);
12318 parser->in_statement = in_statement;
12319 finish_do_body (statement);
12320 /* Look for the `while' keyword. */
12321 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12322 /* Look for the `('. */
12323 matching_parens parens;
12324 parens.require_open (parser);
12325 /* Parse the expression. */
12326 expression = cp_parser_expression (parser);
12327 /* We're done with the do-statement. */
12328 finish_do_stmt (expression, statement, ivdep, unroll);
12329 /* Look for the `)'. */
12330 parens.require_close (parser);
12331 /* Look for the `;'. */
12332 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12334 break;
12336 case RID_FOR:
12338 /* Look for the `('. */
12339 matching_parens parens;
12340 parens.require_open (parser);
12342 statement = cp_parser_for (parser, ivdep, unroll);
12344 /* Look for the `)'. */
12345 parens.require_close (parser);
12347 /* Parse the body of the for-statement. */
12348 parser->in_statement = IN_ITERATION_STMT;
12349 bool prev = note_iteration_stmt_body_start ();
12350 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12351 note_iteration_stmt_body_end (prev);
12352 parser->in_statement = in_statement;
12354 /* We're done with the for-statement. */
12355 finish_for_stmt (statement);
12357 break;
12359 default:
12360 cp_parser_error (parser, "expected iteration-statement");
12361 statement = error_mark_node;
12362 break;
12365 return statement;
12368 /* Parse a init-statement or the declarator of a range-based-for.
12369 Returns true if a range-based-for declaration is seen.
12371 init-statement:
12372 expression-statement
12373 simple-declaration */
12375 static bool
12376 cp_parser_init_statement (cp_parser *parser, tree *decl)
12378 /* If the next token is a `;', then we have an empty
12379 expression-statement. Grammatically, this is also a
12380 simple-declaration, but an invalid one, because it does not
12381 declare anything. Therefore, if we did not handle this case
12382 specially, we would issue an error message about an invalid
12383 declaration. */
12384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12386 bool is_range_for = false;
12387 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12389 /* Try to parse the init-statement. */
12390 if (cp_parser_range_based_for_with_init_p (parser))
12392 tree dummy;
12393 cp_parser_parse_tentatively (parser);
12394 /* Parse the declaration. */
12395 cp_parser_simple_declaration (parser,
12396 /*function_definition_allowed_p=*/false,
12397 &dummy);
12398 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12399 if (!cp_parser_parse_definitely (parser))
12400 /* That didn't work, try to parse it as an expression-statement. */
12401 cp_parser_expression_statement (parser, NULL_TREE);
12403 if (cxx_dialect < cxx2a)
12405 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12406 "range-based %<for%> loops with initializer only "
12407 "available with -std=c++2a or -std=gnu++2a");
12408 *decl = error_mark_node;
12412 /* A colon is used in range-based for. */
12413 parser->colon_corrects_to_scope_p = false;
12415 /* We're going to speculatively look for a declaration, falling back
12416 to an expression, if necessary. */
12417 cp_parser_parse_tentatively (parser);
12418 /* Parse the declaration. */
12419 cp_parser_simple_declaration (parser,
12420 /*function_definition_allowed_p=*/false,
12421 decl);
12422 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12423 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12425 /* It is a range-for, consume the ':'. */
12426 cp_lexer_consume_token (parser->lexer);
12427 is_range_for = true;
12428 if (cxx_dialect < cxx11)
12429 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12430 "range-based %<for%> loops only available with "
12431 "-std=c++11 or -std=gnu++11");
12433 else
12434 /* The ';' is not consumed yet because we told
12435 cp_parser_simple_declaration not to. */
12436 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12438 if (cp_parser_parse_definitely (parser))
12439 return is_range_for;
12440 /* If the tentative parse failed, then we shall need to look for an
12441 expression-statement. */
12443 /* If we are here, it is an expression-statement. */
12444 cp_parser_expression_statement (parser, NULL_TREE);
12445 return false;
12448 /* Parse a jump-statement.
12450 jump-statement:
12451 break ;
12452 continue ;
12453 return expression [opt] ;
12454 return braced-init-list ;
12455 goto identifier ;
12457 GNU extension:
12459 jump-statement:
12460 goto * expression ;
12462 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12464 static tree
12465 cp_parser_jump_statement (cp_parser* parser)
12467 tree statement = error_mark_node;
12468 cp_token *token;
12469 enum rid keyword;
12470 unsigned char in_statement;
12472 /* Peek at the next token. */
12473 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12474 if (!token)
12475 return error_mark_node;
12477 /* See what kind of keyword it is. */
12478 keyword = token->keyword;
12479 switch (keyword)
12481 case RID_BREAK:
12482 in_statement = parser->in_statement & ~IN_IF_STMT;
12483 switch (in_statement)
12485 case 0:
12486 error_at (token->location, "break statement not within loop or switch");
12487 break;
12488 default:
12489 gcc_assert ((in_statement & IN_SWITCH_STMT)
12490 || in_statement == IN_ITERATION_STMT);
12491 statement = finish_break_stmt ();
12492 if (in_statement == IN_ITERATION_STMT)
12493 break_maybe_infinite_loop ();
12494 break;
12495 case IN_OMP_BLOCK:
12496 error_at (token->location, "invalid exit from OpenMP structured block");
12497 break;
12498 case IN_OMP_FOR:
12499 error_at (token->location, "break statement used with OpenMP for loop");
12500 break;
12502 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12503 break;
12505 case RID_CONTINUE:
12506 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12508 case 0:
12509 error_at (token->location, "continue statement not within a loop");
12510 break;
12511 /* Fall through. */
12512 case IN_ITERATION_STMT:
12513 case IN_OMP_FOR:
12514 statement = finish_continue_stmt ();
12515 break;
12516 case IN_OMP_BLOCK:
12517 error_at (token->location, "invalid exit from OpenMP structured block");
12518 break;
12519 default:
12520 gcc_unreachable ();
12522 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12523 break;
12525 case RID_RETURN:
12527 tree expr;
12528 bool expr_non_constant_p;
12530 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12532 cp_lexer_set_source_position (parser->lexer);
12533 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12534 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12536 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12537 expr = cp_parser_expression (parser);
12538 else
12539 /* If the next token is a `;', then there is no
12540 expression. */
12541 expr = NULL_TREE;
12542 /* Build the return-statement. */
12543 if (current_function_auto_return_pattern && in_discarded_stmt)
12544 /* Don't deduce from a discarded return statement. */;
12545 else
12546 statement = finish_return_stmt (expr);
12547 /* Look for the final `;'. */
12548 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12550 break;
12552 case RID_GOTO:
12553 if (parser->in_function_body
12554 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12556 error ("%<goto%> in %<constexpr%> function");
12557 cp_function_chain->invalid_constexpr = true;
12560 /* Create the goto-statement. */
12561 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12563 /* Issue a warning about this use of a GNU extension. */
12564 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12565 /* Consume the '*' token. */
12566 cp_lexer_consume_token (parser->lexer);
12567 /* Parse the dependent expression. */
12568 finish_goto_stmt (cp_parser_expression (parser));
12570 else
12571 finish_goto_stmt (cp_parser_identifier (parser));
12572 /* Look for the final `;'. */
12573 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12574 break;
12576 default:
12577 cp_parser_error (parser, "expected jump-statement");
12578 break;
12581 return statement;
12584 /* Parse a declaration-statement.
12586 declaration-statement:
12587 block-declaration */
12589 static void
12590 cp_parser_declaration_statement (cp_parser* parser)
12592 void *p;
12594 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12595 p = obstack_alloc (&declarator_obstack, 0);
12597 /* Parse the block-declaration. */
12598 cp_parser_block_declaration (parser, /*statement_p=*/true);
12600 /* Free any declarators allocated. */
12601 obstack_free (&declarator_obstack, p);
12604 /* Some dependent statements (like `if (cond) statement'), are
12605 implicitly in their own scope. In other words, if the statement is
12606 a single statement (as opposed to a compound-statement), it is
12607 none-the-less treated as if it were enclosed in braces. Any
12608 declarations appearing in the dependent statement are out of scope
12609 after control passes that point. This function parses a statement,
12610 but ensures that is in its own scope, even if it is not a
12611 compound-statement.
12613 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12614 is a (possibly labeled) if statement which is not enclosed in
12615 braces and has an else clause. This is used to implement
12616 -Wparentheses.
12618 CHAIN is a vector of if-else-if conditions. This is used to implement
12619 -Wduplicated-cond.
12621 Returns the new statement. */
12623 static tree
12624 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12625 const token_indent_info &guard_tinfo,
12626 vec<tree> *chain)
12628 tree statement;
12629 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12630 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12631 token_indent_info body_tinfo
12632 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12634 if (if_p != NULL)
12635 *if_p = false;
12637 /* Mark if () ; with a special NOP_EXPR. */
12638 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12640 cp_lexer_consume_token (parser->lexer);
12641 statement = add_stmt (build_empty_stmt (body_loc));
12643 if (guard_tinfo.keyword == RID_IF
12644 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12645 warning_at (body_loc, OPT_Wempty_body,
12646 "suggest braces around empty body in an %<if%> statement");
12647 else if (guard_tinfo.keyword == RID_ELSE)
12648 warning_at (body_loc, OPT_Wempty_body,
12649 "suggest braces around empty body in an %<else%> statement");
12651 /* if a compound is opened, we simply parse the statement directly. */
12652 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12653 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12654 /* If the token is not a `{', then we must take special action. */
12655 else
12657 /* Create a compound-statement. */
12658 statement = begin_compound_stmt (0);
12659 /* Parse the dependent-statement. */
12660 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12661 &body_loc_after_labels);
12662 /* Finish the dummy compound-statement. */
12663 finish_compound_stmt (statement);
12666 token_indent_info next_tinfo
12667 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12668 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12670 if (body_loc_after_labels != UNKNOWN_LOCATION
12671 && next_tinfo.type != CPP_SEMICOLON)
12672 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12673 guard_tinfo.location, guard_tinfo.keyword);
12675 /* Return the statement. */
12676 return statement;
12679 /* For some dependent statements (like `while (cond) statement'), we
12680 have already created a scope. Therefore, even if the dependent
12681 statement is a compound-statement, we do not want to create another
12682 scope. */
12684 static void
12685 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12686 const token_indent_info &guard_tinfo)
12688 /* If the token is a `{', then we must take special action. */
12689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12691 token_indent_info body_tinfo
12692 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12693 location_t loc_after_labels = UNKNOWN_LOCATION;
12695 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12696 &loc_after_labels);
12697 token_indent_info next_tinfo
12698 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12699 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12701 if (loc_after_labels != UNKNOWN_LOCATION
12702 && next_tinfo.type != CPP_SEMICOLON)
12703 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12704 guard_tinfo.location,
12705 guard_tinfo.keyword);
12707 else
12709 /* Avoid calling cp_parser_compound_statement, so that we
12710 don't create a new scope. Do everything else by hand. */
12711 matching_braces braces;
12712 braces.require_open (parser);
12713 /* If the next keyword is `__label__' we have a label declaration. */
12714 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12715 cp_parser_label_declaration (parser);
12716 /* Parse an (optional) statement-seq. */
12717 cp_parser_statement_seq_opt (parser, NULL_TREE);
12718 braces.require_close (parser);
12722 /* Declarations [gram.dcl.dcl] */
12724 /* Parse an optional declaration-sequence.
12726 declaration-seq:
12727 declaration
12728 declaration-seq declaration */
12730 static void
12731 cp_parser_declaration_seq_opt (cp_parser* parser)
12733 while (true)
12735 cp_token *token;
12737 token = cp_lexer_peek_token (parser->lexer);
12739 if (token->type == CPP_CLOSE_BRACE
12740 || token->type == CPP_EOF
12741 || token->type == CPP_PRAGMA_EOL)
12742 break;
12744 if (token->type == CPP_SEMICOLON)
12746 /* A declaration consisting of a single semicolon is
12747 invalid. Allow it unless we're being pedantic. */
12748 cp_lexer_consume_token (parser->lexer);
12749 if (!in_system_header_at (input_location))
12750 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12751 continue;
12754 /* If we're entering or exiting a region that's implicitly
12755 extern "C", modify the lang context appropriately. */
12756 if (!parser->implicit_extern_c && token->implicit_extern_c)
12758 push_lang_context (lang_name_c);
12759 parser->implicit_extern_c = true;
12761 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12763 pop_lang_context ();
12764 parser->implicit_extern_c = false;
12767 if (token->type == CPP_PRAGMA)
12769 /* A top-level declaration can consist solely of a #pragma.
12770 A nested declaration cannot, so this is done here and not
12771 in cp_parser_declaration. (A #pragma at block scope is
12772 handled in cp_parser_statement.) */
12773 cp_parser_pragma (parser, pragma_external, NULL);
12774 continue;
12777 /* Parse the declaration itself. */
12778 cp_parser_declaration (parser);
12782 /* Parse a declaration.
12784 declaration:
12785 block-declaration
12786 function-definition
12787 template-declaration
12788 explicit-instantiation
12789 explicit-specialization
12790 linkage-specification
12791 namespace-definition
12793 C++17:
12794 deduction-guide
12796 GNU extension:
12798 declaration:
12799 __extension__ declaration */
12801 static void
12802 cp_parser_declaration (cp_parser* parser)
12804 cp_token token1;
12805 cp_token token2;
12806 int saved_pedantic;
12807 void *p;
12808 tree attributes = NULL_TREE;
12810 /* Check for the `__extension__' keyword. */
12811 if (cp_parser_extension_opt (parser, &saved_pedantic))
12813 /* Parse the qualified declaration. */
12814 cp_parser_declaration (parser);
12815 /* Restore the PEDANTIC flag. */
12816 pedantic = saved_pedantic;
12818 return;
12821 /* Try to figure out what kind of declaration is present. */
12822 token1 = *cp_lexer_peek_token (parser->lexer);
12824 if (token1.type != CPP_EOF)
12825 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12826 else
12828 token2.type = CPP_EOF;
12829 token2.keyword = RID_MAX;
12832 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12833 p = obstack_alloc (&declarator_obstack, 0);
12835 /* If the next token is `extern' and the following token is a string
12836 literal, then we have a linkage specification. */
12837 if (token1.keyword == RID_EXTERN
12838 && cp_parser_is_pure_string_literal (&token2))
12839 cp_parser_linkage_specification (parser);
12840 /* If the next token is `template', then we have either a template
12841 declaration, an explicit instantiation, or an explicit
12842 specialization. */
12843 else if (token1.keyword == RID_TEMPLATE)
12845 /* `template <>' indicates a template specialization. */
12846 if (token2.type == CPP_LESS
12847 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12848 cp_parser_explicit_specialization (parser);
12849 /* `template <' indicates a template declaration. */
12850 else if (token2.type == CPP_LESS)
12851 cp_parser_template_declaration (parser, /*member_p=*/false);
12852 /* Anything else must be an explicit instantiation. */
12853 else
12854 cp_parser_explicit_instantiation (parser);
12856 /* If the next token is `export', then we have a template
12857 declaration. */
12858 else if (token1.keyword == RID_EXPORT)
12859 cp_parser_template_declaration (parser, /*member_p=*/false);
12860 /* If the next token is `extern', 'static' or 'inline' and the one
12861 after that is `template', we have a GNU extended explicit
12862 instantiation directive. */
12863 else if (cp_parser_allow_gnu_extensions_p (parser)
12864 && (token1.keyword == RID_EXTERN
12865 || token1.keyword == RID_STATIC
12866 || token1.keyword == RID_INLINE)
12867 && token2.keyword == RID_TEMPLATE)
12868 cp_parser_explicit_instantiation (parser);
12869 /* If the next token is `namespace', check for a named or unnamed
12870 namespace definition. */
12871 else if (token1.keyword == RID_NAMESPACE
12872 && (/* A named namespace definition. */
12873 (token2.type == CPP_NAME
12874 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12875 != CPP_EQ))
12876 || (token2.type == CPP_OPEN_SQUARE
12877 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12878 == CPP_OPEN_SQUARE)
12879 /* An unnamed namespace definition. */
12880 || token2.type == CPP_OPEN_BRACE
12881 || token2.keyword == RID_ATTRIBUTE))
12882 cp_parser_namespace_definition (parser);
12883 /* An inline (associated) namespace definition. */
12884 else if (token1.keyword == RID_INLINE
12885 && token2.keyword == RID_NAMESPACE)
12886 cp_parser_namespace_definition (parser);
12887 /* Objective-C++ declaration/definition. */
12888 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12889 cp_parser_objc_declaration (parser, NULL_TREE);
12890 else if (c_dialect_objc ()
12891 && token1.keyword == RID_ATTRIBUTE
12892 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12893 cp_parser_objc_declaration (parser, attributes);
12894 /* At this point we may have a template declared by a concept
12895 introduction. */
12896 else if (flag_concepts
12897 && cp_parser_template_declaration_after_export (parser,
12898 /*member_p=*/false))
12899 /* We did. */;
12900 else
12901 /* Try to parse a block-declaration, or a function-definition. */
12902 cp_parser_block_declaration (parser, /*statement_p=*/false);
12904 /* Free any declarators allocated. */
12905 obstack_free (&declarator_obstack, p);
12908 /* Parse a block-declaration.
12910 block-declaration:
12911 simple-declaration
12912 asm-definition
12913 namespace-alias-definition
12914 using-declaration
12915 using-directive
12917 GNU Extension:
12919 block-declaration:
12920 __extension__ block-declaration
12922 C++0x Extension:
12924 block-declaration:
12925 static_assert-declaration
12927 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12928 part of a declaration-statement. */
12930 static void
12931 cp_parser_block_declaration (cp_parser *parser,
12932 bool statement_p)
12934 cp_token *token1;
12935 int saved_pedantic;
12937 /* Check for the `__extension__' keyword. */
12938 if (cp_parser_extension_opt (parser, &saved_pedantic))
12940 /* Parse the qualified declaration. */
12941 cp_parser_block_declaration (parser, statement_p);
12942 /* Restore the PEDANTIC flag. */
12943 pedantic = saved_pedantic;
12945 return;
12948 /* Peek at the next token to figure out which kind of declaration is
12949 present. */
12950 token1 = cp_lexer_peek_token (parser->lexer);
12952 /* If the next keyword is `asm', we have an asm-definition. */
12953 if (token1->keyword == RID_ASM)
12955 if (statement_p)
12956 cp_parser_commit_to_tentative_parse (parser);
12957 cp_parser_asm_definition (parser);
12959 /* If the next keyword is `namespace', we have a
12960 namespace-alias-definition. */
12961 else if (token1->keyword == RID_NAMESPACE)
12962 cp_parser_namespace_alias_definition (parser);
12963 /* If the next keyword is `using', we have a
12964 using-declaration, a using-directive, or an alias-declaration. */
12965 else if (token1->keyword == RID_USING)
12967 cp_token *token2;
12969 if (statement_p)
12970 cp_parser_commit_to_tentative_parse (parser);
12971 /* If the token after `using' is `namespace', then we have a
12972 using-directive. */
12973 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12974 if (token2->keyword == RID_NAMESPACE)
12975 cp_parser_using_directive (parser);
12976 /* If the second token after 'using' is '=', then we have an
12977 alias-declaration. */
12978 else if (cxx_dialect >= cxx11
12979 && token2->type == CPP_NAME
12980 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12981 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12982 cp_parser_alias_declaration (parser);
12983 /* Otherwise, it's a using-declaration. */
12984 else
12985 cp_parser_using_declaration (parser,
12986 /*access_declaration_p=*/false);
12988 /* If the next keyword is `__label__' we have a misplaced label
12989 declaration. */
12990 else if (token1->keyword == RID_LABEL)
12992 cp_lexer_consume_token (parser->lexer);
12993 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12994 cp_parser_skip_to_end_of_statement (parser);
12995 /* If the next token is now a `;', consume it. */
12996 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12997 cp_lexer_consume_token (parser->lexer);
12999 /* If the next token is `static_assert' we have a static assertion. */
13000 else if (token1->keyword == RID_STATIC_ASSERT)
13001 cp_parser_static_assert (parser, /*member_p=*/false);
13002 /* Anything else must be a simple-declaration. */
13003 else
13004 cp_parser_simple_declaration (parser, !statement_p,
13005 /*maybe_range_for_decl*/NULL);
13008 /* Parse a simple-declaration.
13010 simple-declaration:
13011 decl-specifier-seq [opt] init-declarator-list [opt] ;
13012 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13013 brace-or-equal-initializer ;
13015 init-declarator-list:
13016 init-declarator
13017 init-declarator-list , init-declarator
13019 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13020 function-definition as a simple-declaration.
13022 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13023 parsed declaration if it is an uninitialized single declarator not followed
13024 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13025 if present, will not be consumed. */
13027 static void
13028 cp_parser_simple_declaration (cp_parser* parser,
13029 bool function_definition_allowed_p,
13030 tree *maybe_range_for_decl)
13032 cp_decl_specifier_seq decl_specifiers;
13033 int declares_class_or_enum;
13034 bool saw_declarator;
13035 location_t comma_loc = UNKNOWN_LOCATION;
13036 location_t init_loc = UNKNOWN_LOCATION;
13038 if (maybe_range_for_decl)
13039 *maybe_range_for_decl = NULL_TREE;
13041 /* Defer access checks until we know what is being declared; the
13042 checks for names appearing in the decl-specifier-seq should be
13043 done as if we were in the scope of the thing being declared. */
13044 push_deferring_access_checks (dk_deferred);
13046 /* Parse the decl-specifier-seq. We have to keep track of whether
13047 or not the decl-specifier-seq declares a named class or
13048 enumeration type, since that is the only case in which the
13049 init-declarator-list is allowed to be empty.
13051 [dcl.dcl]
13053 In a simple-declaration, the optional init-declarator-list can be
13054 omitted only when declaring a class or enumeration, that is when
13055 the decl-specifier-seq contains either a class-specifier, an
13056 elaborated-type-specifier, or an enum-specifier. */
13057 cp_parser_decl_specifier_seq (parser,
13058 CP_PARSER_FLAGS_OPTIONAL,
13059 &decl_specifiers,
13060 &declares_class_or_enum);
13061 /* We no longer need to defer access checks. */
13062 stop_deferring_access_checks ();
13064 /* In a block scope, a valid declaration must always have a
13065 decl-specifier-seq. By not trying to parse declarators, we can
13066 resolve the declaration/expression ambiguity more quickly. */
13067 if (!function_definition_allowed_p
13068 && !decl_specifiers.any_specifiers_p)
13070 cp_parser_error (parser, "expected declaration");
13071 goto done;
13074 /* If the next two tokens are both identifiers, the code is
13075 erroneous. The usual cause of this situation is code like:
13077 T t;
13079 where "T" should name a type -- but does not. */
13080 if (!decl_specifiers.any_type_specifiers_p
13081 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13083 /* If parsing tentatively, we should commit; we really are
13084 looking at a declaration. */
13085 cp_parser_commit_to_tentative_parse (parser);
13086 /* Give up. */
13087 goto done;
13090 cp_parser_maybe_commit_to_declaration (parser,
13091 decl_specifiers.any_specifiers_p);
13093 /* Look for C++17 decomposition declaration. */
13094 for (size_t n = 1; ; n++)
13095 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13096 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13097 continue;
13098 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13099 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13100 && decl_specifiers.any_specifiers_p)
13102 tree decl
13103 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13104 maybe_range_for_decl,
13105 &init_loc);
13107 /* The next token should be either a `,' or a `;'. */
13108 cp_token *token = cp_lexer_peek_token (parser->lexer);
13109 /* If it's a `;', we are done. */
13110 if (token->type == CPP_SEMICOLON)
13111 goto finish;
13112 else if (maybe_range_for_decl)
13114 if (*maybe_range_for_decl == NULL_TREE)
13115 *maybe_range_for_decl = error_mark_node;
13116 goto finish;
13118 /* Anything else is an error. */
13119 else
13121 /* If we have already issued an error message we don't need
13122 to issue another one. */
13123 if ((decl != error_mark_node
13124 && DECL_INITIAL (decl) != error_mark_node)
13125 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13126 cp_parser_error (parser, "expected %<,%> or %<;%>");
13127 /* Skip tokens until we reach the end of the statement. */
13128 cp_parser_skip_to_end_of_statement (parser);
13129 /* If the next token is now a `;', consume it. */
13130 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13131 cp_lexer_consume_token (parser->lexer);
13132 goto done;
13135 else
13136 break;
13138 tree last_type;
13139 bool auto_specifier_p;
13140 /* NULL_TREE if both variable and function declaration are allowed,
13141 error_mark_node if function declaration are not allowed and
13142 a FUNCTION_DECL that should be diagnosed if it is followed by
13143 variable declarations. */
13144 tree auto_function_declaration;
13146 last_type = NULL_TREE;
13147 auto_specifier_p
13148 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13149 auto_function_declaration = NULL_TREE;
13151 /* Keep going until we hit the `;' at the end of the simple
13152 declaration. */
13153 saw_declarator = false;
13154 while (cp_lexer_next_token_is_not (parser->lexer,
13155 CPP_SEMICOLON))
13157 cp_token *token;
13158 bool function_definition_p;
13159 tree decl;
13160 tree auto_result = NULL_TREE;
13162 if (saw_declarator)
13164 /* If we are processing next declarator, comma is expected */
13165 token = cp_lexer_peek_token (parser->lexer);
13166 gcc_assert (token->type == CPP_COMMA);
13167 cp_lexer_consume_token (parser->lexer);
13168 if (maybe_range_for_decl)
13170 *maybe_range_for_decl = error_mark_node;
13171 if (comma_loc == UNKNOWN_LOCATION)
13172 comma_loc = token->location;
13175 else
13176 saw_declarator = true;
13178 /* Parse the init-declarator. */
13179 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13180 /*checks=*/NULL,
13181 function_definition_allowed_p,
13182 /*member_p=*/false,
13183 declares_class_or_enum,
13184 &function_definition_p,
13185 maybe_range_for_decl,
13186 &init_loc,
13187 &auto_result);
13188 /* If an error occurred while parsing tentatively, exit quickly.
13189 (That usually happens when in the body of a function; each
13190 statement is treated as a declaration-statement until proven
13191 otherwise.) */
13192 if (cp_parser_error_occurred (parser))
13193 goto done;
13195 if (auto_specifier_p && cxx_dialect >= cxx14)
13197 /* If the init-declarator-list contains more than one
13198 init-declarator, they shall all form declarations of
13199 variables. */
13200 if (auto_function_declaration == NULL_TREE)
13201 auto_function_declaration
13202 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13203 else if (TREE_CODE (decl) == FUNCTION_DECL
13204 || auto_function_declaration != error_mark_node)
13206 error_at (decl_specifiers.locations[ds_type_spec],
13207 "non-variable %qD in declaration with more than one "
13208 "declarator with placeholder type",
13209 TREE_CODE (decl) == FUNCTION_DECL
13210 ? decl : auto_function_declaration);
13211 auto_function_declaration = error_mark_node;
13215 if (auto_result
13216 && (!processing_template_decl || !type_uses_auto (auto_result)))
13218 if (last_type
13219 && last_type != error_mark_node
13220 && !same_type_p (auto_result, last_type))
13222 /* If the list of declarators contains more than one declarator,
13223 the type of each declared variable is determined as described
13224 above. If the type deduced for the template parameter U is not
13225 the same in each deduction, the program is ill-formed. */
13226 error_at (decl_specifiers.locations[ds_type_spec],
13227 "inconsistent deduction for %qT: %qT and then %qT",
13228 decl_specifiers.type, last_type, auto_result);
13229 last_type = error_mark_node;
13231 else
13232 last_type = auto_result;
13235 /* Handle function definitions specially. */
13236 if (function_definition_p)
13238 /* If the next token is a `,', then we are probably
13239 processing something like:
13241 void f() {}, *p;
13243 which is erroneous. */
13244 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13246 cp_token *token = cp_lexer_peek_token (parser->lexer);
13247 error_at (token->location,
13248 "mixing"
13249 " declarations and function-definitions is forbidden");
13251 /* Otherwise, we're done with the list of declarators. */
13252 else
13254 pop_deferring_access_checks ();
13255 return;
13258 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13259 *maybe_range_for_decl = decl;
13260 /* The next token should be either a `,' or a `;'. */
13261 token = cp_lexer_peek_token (parser->lexer);
13262 /* If it's a `,', there are more declarators to come. */
13263 if (token->type == CPP_COMMA)
13264 /* will be consumed next time around */;
13265 /* If it's a `;', we are done. */
13266 else if (token->type == CPP_SEMICOLON)
13267 break;
13268 else if (maybe_range_for_decl)
13270 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13271 permerror (decl_specifiers.locations[ds_type_spec],
13272 "types may not be defined in a for-range-declaration");
13273 break;
13275 /* Anything else is an error. */
13276 else
13278 /* If we have already issued an error message we don't need
13279 to issue another one. */
13280 if ((decl != error_mark_node
13281 && DECL_INITIAL (decl) != error_mark_node)
13282 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13283 cp_parser_error (parser, "expected %<,%> or %<;%>");
13284 /* Skip tokens until we reach the end of the statement. */
13285 cp_parser_skip_to_end_of_statement (parser);
13286 /* If the next token is now a `;', consume it. */
13287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13288 cp_lexer_consume_token (parser->lexer);
13289 goto done;
13291 /* After the first time around, a function-definition is not
13292 allowed -- even if it was OK at first. For example:
13294 int i, f() {}
13296 is not valid. */
13297 function_definition_allowed_p = false;
13300 /* Issue an error message if no declarators are present, and the
13301 decl-specifier-seq does not itself declare a class or
13302 enumeration: [dcl.dcl]/3. */
13303 if (!saw_declarator)
13305 if (cp_parser_declares_only_class_p (parser))
13307 if (!declares_class_or_enum
13308 && decl_specifiers.type
13309 && OVERLOAD_TYPE_P (decl_specifiers.type))
13310 /* Ensure an error is issued anyway when finish_decltype_type,
13311 called via cp_parser_decl_specifier_seq, returns a class or
13312 an enumeration (c++/51786). */
13313 decl_specifiers.type = NULL_TREE;
13314 shadow_tag (&decl_specifiers);
13316 /* Perform any deferred access checks. */
13317 perform_deferred_access_checks (tf_warning_or_error);
13320 /* Consume the `;'. */
13321 finish:
13322 if (!maybe_range_for_decl)
13323 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13324 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13326 if (init_loc != UNKNOWN_LOCATION)
13327 error_at (init_loc, "initializer in range-based %<for%> loop");
13328 if (comma_loc != UNKNOWN_LOCATION)
13329 error_at (comma_loc,
13330 "multiple declarations in range-based %<for%> loop");
13333 done:
13334 pop_deferring_access_checks ();
13337 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13338 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13339 initializer ; */
13341 static tree
13342 cp_parser_decomposition_declaration (cp_parser *parser,
13343 cp_decl_specifier_seq *decl_specifiers,
13344 tree *maybe_range_for_decl,
13345 location_t *init_loc)
13347 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13348 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13349 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13351 /* Parse the identifier-list. */
13352 auto_vec<cp_expr, 10> v;
13353 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13354 while (true)
13356 cp_expr e = cp_parser_identifier (parser);
13357 if (e.get_value () == error_mark_node)
13358 break;
13359 v.safe_push (e);
13360 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13361 break;
13362 cp_lexer_consume_token (parser->lexer);
13365 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13366 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13368 end_loc = UNKNOWN_LOCATION;
13369 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13370 false);
13371 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13372 cp_lexer_consume_token (parser->lexer);
13373 else
13375 cp_parser_skip_to_end_of_statement (parser);
13376 return error_mark_node;
13380 if (cxx_dialect < cxx17)
13381 pedwarn (loc, 0, "structured bindings only available with "
13382 "-std=c++17 or -std=gnu++17");
13384 tree pushed_scope;
13385 cp_declarator *declarator = make_declarator (cdk_decomp);
13386 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13387 declarator->id_loc = loc;
13388 if (ref_qual != REF_QUAL_NONE)
13389 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13390 ref_qual == REF_QUAL_RVALUE,
13391 NULL_TREE);
13392 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13393 NULL_TREE, decl_specifiers->attributes,
13394 &pushed_scope);
13395 tree orig_decl = decl;
13397 unsigned int i;
13398 cp_expr e;
13399 cp_decl_specifier_seq decl_specs;
13400 clear_decl_specs (&decl_specs);
13401 decl_specs.type = make_auto ();
13402 tree prev = decl;
13403 FOR_EACH_VEC_ELT (v, i, e)
13405 if (i == 0)
13406 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13407 else
13408 declarator->u.id.unqualified_name = e.get_value ();
13409 declarator->id_loc = e.get_location ();
13410 tree elt_pushed_scope;
13411 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13412 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13413 if (decl2 == error_mark_node)
13414 decl = error_mark_node;
13415 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13417 /* Ensure we've diagnosed redeclaration if we aren't creating
13418 a new VAR_DECL. */
13419 gcc_assert (errorcount);
13420 decl = error_mark_node;
13422 else
13423 prev = decl2;
13424 if (elt_pushed_scope)
13425 pop_scope (elt_pushed_scope);
13428 if (v.is_empty ())
13430 error_at (loc, "empty structured binding declaration");
13431 decl = error_mark_node;
13434 if (maybe_range_for_decl == NULL
13435 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13437 bool non_constant_p = false, is_direct_init = false;
13438 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13439 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13440 &non_constant_p);
13441 if (initializer == NULL_TREE
13442 || (TREE_CODE (initializer) == TREE_LIST
13443 && TREE_CHAIN (initializer))
13444 || (is_direct_init
13445 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13446 && CONSTRUCTOR_NELTS (initializer) != 1))
13448 error_at (loc, "invalid initializer for structured binding "
13449 "declaration");
13450 initializer = error_mark_node;
13453 if (decl != error_mark_node)
13455 cp_maybe_mangle_decomp (decl, prev, v.length ());
13456 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13457 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13458 cp_finish_decomp (decl, prev, v.length ());
13461 else if (decl != error_mark_node)
13463 *maybe_range_for_decl = prev;
13464 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13465 the underlying DECL. */
13466 cp_finish_decomp (decl, prev, v.length ());
13469 if (pushed_scope)
13470 pop_scope (pushed_scope);
13472 if (decl == error_mark_node && DECL_P (orig_decl))
13474 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13475 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13478 return decl;
13481 /* Parse a decl-specifier-seq.
13483 decl-specifier-seq:
13484 decl-specifier-seq [opt] decl-specifier
13485 decl-specifier attribute-specifier-seq [opt] (C++11)
13487 decl-specifier:
13488 storage-class-specifier
13489 type-specifier
13490 function-specifier
13491 friend
13492 typedef
13494 GNU Extension:
13496 decl-specifier:
13497 attributes
13499 Concepts Extension:
13501 decl-specifier:
13502 concept
13504 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13506 The parser flags FLAGS is used to control type-specifier parsing.
13508 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13509 flags:
13511 1: one of the decl-specifiers is an elaborated-type-specifier
13512 (i.e., a type declaration)
13513 2: one of the decl-specifiers is an enum-specifier or a
13514 class-specifier (i.e., a type definition)
13518 static void
13519 cp_parser_decl_specifier_seq (cp_parser* parser,
13520 cp_parser_flags flags,
13521 cp_decl_specifier_seq *decl_specs,
13522 int* declares_class_or_enum)
13524 bool constructor_possible_p = !parser->in_declarator_p;
13525 bool found_decl_spec = false;
13526 cp_token *start_token = NULL;
13527 cp_decl_spec ds;
13529 /* Clear DECL_SPECS. */
13530 clear_decl_specs (decl_specs);
13532 /* Assume no class or enumeration type is declared. */
13533 *declares_class_or_enum = 0;
13535 /* Keep reading specifiers until there are no more to read. */
13536 while (true)
13538 bool constructor_p;
13539 cp_token *token;
13540 ds = ds_last;
13542 /* Peek at the next token. */
13543 token = cp_lexer_peek_token (parser->lexer);
13545 /* Save the first token of the decl spec list for error
13546 reporting. */
13547 if (!start_token)
13548 start_token = token;
13549 /* Handle attributes. */
13550 if (cp_next_tokens_can_be_attribute_p (parser))
13552 /* Parse the attributes. */
13553 tree attrs = cp_parser_attributes_opt (parser);
13555 /* In a sequence of declaration specifiers, c++11 attributes
13556 appertain to the type that precede them. In that case
13557 [dcl.spec]/1 says:
13559 The attribute-specifier-seq affects the type only for
13560 the declaration it appears in, not other declarations
13561 involving the same type.
13563 But for now let's force the user to position the
13564 attribute either at the beginning of the declaration or
13565 after the declarator-id, which would clearly mean that it
13566 applies to the declarator. */
13567 if (cxx11_attribute_p (attrs))
13569 if (!found_decl_spec)
13570 /* The c++11 attribute is at the beginning of the
13571 declaration. It appertains to the entity being
13572 declared. */;
13573 else
13575 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13577 /* This is an attribute following a
13578 class-specifier. */
13579 if (decl_specs->type_definition_p)
13580 warn_misplaced_attr_for_class_type (token->location,
13581 decl_specs->type);
13582 attrs = NULL_TREE;
13584 else
13586 decl_specs->std_attributes
13587 = attr_chainon (decl_specs->std_attributes, attrs);
13588 if (decl_specs->locations[ds_std_attribute] == 0)
13589 decl_specs->locations[ds_std_attribute] = token->location;
13591 continue;
13595 decl_specs->attributes
13596 = attr_chainon (decl_specs->attributes, attrs);
13597 if (decl_specs->locations[ds_attribute] == 0)
13598 decl_specs->locations[ds_attribute] = token->location;
13599 continue;
13601 /* Assume we will find a decl-specifier keyword. */
13602 found_decl_spec = true;
13603 /* If the next token is an appropriate keyword, we can simply
13604 add it to the list. */
13605 switch (token->keyword)
13607 /* decl-specifier:
13608 friend
13609 constexpr */
13610 case RID_FRIEND:
13611 if (!at_class_scope_p ())
13613 gcc_rich_location richloc (token->location);
13614 richloc.add_fixit_remove ();
13615 error_at (&richloc, "%<friend%> used outside of class");
13616 cp_lexer_purge_token (parser->lexer);
13618 else
13620 ds = ds_friend;
13621 /* Consume the token. */
13622 cp_lexer_consume_token (parser->lexer);
13624 break;
13626 case RID_CONSTEXPR:
13627 ds = ds_constexpr;
13628 cp_lexer_consume_token (parser->lexer);
13629 break;
13631 case RID_CONCEPT:
13632 ds = ds_concept;
13633 cp_lexer_consume_token (parser->lexer);
13634 break;
13636 /* function-specifier:
13637 inline
13638 virtual
13639 explicit */
13640 case RID_INLINE:
13641 case RID_VIRTUAL:
13642 case RID_EXPLICIT:
13643 cp_parser_function_specifier_opt (parser, decl_specs);
13644 break;
13646 /* decl-specifier:
13647 typedef */
13648 case RID_TYPEDEF:
13649 ds = ds_typedef;
13650 /* Consume the token. */
13651 cp_lexer_consume_token (parser->lexer);
13652 /* A constructor declarator cannot appear in a typedef. */
13653 constructor_possible_p = false;
13654 /* The "typedef" keyword can only occur in a declaration; we
13655 may as well commit at this point. */
13656 cp_parser_commit_to_tentative_parse (parser);
13658 if (decl_specs->storage_class != sc_none)
13659 decl_specs->conflicting_specifiers_p = true;
13660 break;
13662 /* storage-class-specifier:
13663 auto
13664 register
13665 static
13666 extern
13667 mutable
13669 GNU Extension:
13670 thread */
13671 case RID_AUTO:
13672 if (cxx_dialect == cxx98)
13674 /* Consume the token. */
13675 cp_lexer_consume_token (parser->lexer);
13677 /* Complain about `auto' as a storage specifier, if
13678 we're complaining about C++0x compatibility. */
13679 gcc_rich_location richloc (token->location);
13680 richloc.add_fixit_remove ();
13681 warning_at (&richloc, OPT_Wc__11_compat,
13682 "%<auto%> changes meaning in C++11; "
13683 "please remove it");
13685 /* Set the storage class anyway. */
13686 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13687 token);
13689 else
13690 /* C++0x auto type-specifier. */
13691 found_decl_spec = false;
13692 break;
13694 case RID_REGISTER:
13695 case RID_STATIC:
13696 case RID_EXTERN:
13697 case RID_MUTABLE:
13698 /* Consume the token. */
13699 cp_lexer_consume_token (parser->lexer);
13700 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13701 token);
13702 break;
13703 case RID_THREAD:
13704 /* Consume the token. */
13705 ds = ds_thread;
13706 cp_lexer_consume_token (parser->lexer);
13707 break;
13709 default:
13710 /* We did not yet find a decl-specifier yet. */
13711 found_decl_spec = false;
13712 break;
13715 if (found_decl_spec
13716 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13717 && token->keyword != RID_CONSTEXPR)
13718 error ("decl-specifier invalid in condition");
13720 if (found_decl_spec
13721 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13722 && token->keyword != RID_MUTABLE
13723 && token->keyword != RID_CONSTEXPR)
13724 error_at (token->location, "%qD invalid in lambda",
13725 ridpointers[token->keyword]);
13727 if (ds != ds_last)
13728 set_and_check_decl_spec_loc (decl_specs, ds, token);
13730 /* Constructors are a special case. The `S' in `S()' is not a
13731 decl-specifier; it is the beginning of the declarator. */
13732 constructor_p
13733 = (!found_decl_spec
13734 && constructor_possible_p
13735 && (cp_parser_constructor_declarator_p
13736 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13738 /* If we don't have a DECL_SPEC yet, then we must be looking at
13739 a type-specifier. */
13740 if (!found_decl_spec && !constructor_p)
13742 int decl_spec_declares_class_or_enum;
13743 bool is_cv_qualifier;
13744 tree type_spec;
13746 type_spec
13747 = cp_parser_type_specifier (parser, flags,
13748 decl_specs,
13749 /*is_declaration=*/true,
13750 &decl_spec_declares_class_or_enum,
13751 &is_cv_qualifier);
13752 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13754 /* If this type-specifier referenced a user-defined type
13755 (a typedef, class-name, etc.), then we can't allow any
13756 more such type-specifiers henceforth.
13758 [dcl.spec]
13760 The longest sequence of decl-specifiers that could
13761 possibly be a type name is taken as the
13762 decl-specifier-seq of a declaration. The sequence shall
13763 be self-consistent as described below.
13765 [dcl.type]
13767 As a general rule, at most one type-specifier is allowed
13768 in the complete decl-specifier-seq of a declaration. The
13769 only exceptions are the following:
13771 -- const or volatile can be combined with any other
13772 type-specifier.
13774 -- signed or unsigned can be combined with char, long,
13775 short, or int.
13777 -- ..
13779 Example:
13781 typedef char* Pc;
13782 void g (const int Pc);
13784 Here, Pc is *not* part of the decl-specifier seq; it's
13785 the declarator. Therefore, once we see a type-specifier
13786 (other than a cv-qualifier), we forbid any additional
13787 user-defined types. We *do* still allow things like `int
13788 int' to be considered a decl-specifier-seq, and issue the
13789 error message later. */
13790 if (type_spec && !is_cv_qualifier)
13791 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13792 /* A constructor declarator cannot follow a type-specifier. */
13793 if (type_spec)
13795 constructor_possible_p = false;
13796 found_decl_spec = true;
13797 if (!is_cv_qualifier)
13798 decl_specs->any_type_specifiers_p = true;
13800 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13801 error_at (token->location, "type-specifier invalid in lambda");
13805 /* If we still do not have a DECL_SPEC, then there are no more
13806 decl-specifiers. */
13807 if (!found_decl_spec)
13808 break;
13810 decl_specs->any_specifiers_p = true;
13811 /* After we see one decl-specifier, further decl-specifiers are
13812 always optional. */
13813 flags |= CP_PARSER_FLAGS_OPTIONAL;
13816 /* Don't allow a friend specifier with a class definition. */
13817 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13818 && (*declares_class_or_enum & 2))
13819 error_at (decl_specs->locations[ds_friend],
13820 "class definition may not be declared a friend");
13823 /* Parse an (optional) storage-class-specifier.
13825 storage-class-specifier:
13826 auto
13827 register
13828 static
13829 extern
13830 mutable
13832 GNU Extension:
13834 storage-class-specifier:
13835 thread
13837 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13839 static tree
13840 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13842 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13844 case RID_AUTO:
13845 if (cxx_dialect != cxx98)
13846 return NULL_TREE;
13847 /* Fall through for C++98. */
13848 gcc_fallthrough ();
13850 case RID_REGISTER:
13851 case RID_STATIC:
13852 case RID_EXTERN:
13853 case RID_MUTABLE:
13854 case RID_THREAD:
13855 /* Consume the token. */
13856 return cp_lexer_consume_token (parser->lexer)->u.value;
13858 default:
13859 return NULL_TREE;
13863 /* Parse an (optional) function-specifier.
13865 function-specifier:
13866 inline
13867 virtual
13868 explicit
13870 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13871 Updates DECL_SPECS, if it is non-NULL. */
13873 static tree
13874 cp_parser_function_specifier_opt (cp_parser* parser,
13875 cp_decl_specifier_seq *decl_specs)
13877 cp_token *token = cp_lexer_peek_token (parser->lexer);
13878 switch (token->keyword)
13880 case RID_INLINE:
13881 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13882 break;
13884 case RID_VIRTUAL:
13885 /* 14.5.2.3 [temp.mem]
13887 A member function template shall not be virtual. */
13888 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13889 && current_class_type)
13890 error_at (token->location, "templates may not be %<virtual%>");
13891 else
13892 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13893 break;
13895 case RID_EXPLICIT:
13896 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13897 break;
13899 default:
13900 return NULL_TREE;
13903 /* Consume the token. */
13904 return cp_lexer_consume_token (parser->lexer)->u.value;
13907 /* Parse a linkage-specification.
13909 linkage-specification:
13910 extern string-literal { declaration-seq [opt] }
13911 extern string-literal declaration */
13913 static void
13914 cp_parser_linkage_specification (cp_parser* parser)
13916 tree linkage;
13918 /* Look for the `extern' keyword. */
13919 cp_token *extern_token
13920 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13922 /* Look for the string-literal. */
13923 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13924 linkage = cp_parser_string_literal (parser, false, false);
13926 /* Transform the literal into an identifier. If the literal is a
13927 wide-character string, or contains embedded NULs, then we can't
13928 handle it as the user wants. */
13929 if (strlen (TREE_STRING_POINTER (linkage))
13930 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13932 cp_parser_error (parser, "invalid linkage-specification");
13933 /* Assume C++ linkage. */
13934 linkage = lang_name_cplusplus;
13936 else
13937 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13939 /* We're now using the new linkage. */
13940 push_lang_context (linkage);
13942 /* Preserve the location of the the innermost linkage specification,
13943 tracking the locations of nested specifications via a local. */
13944 location_t saved_location
13945 = parser->innermost_linkage_specification_location;
13946 /* Construct a location ranging from the start of the "extern" to
13947 the end of the string-literal, with the caret at the start, e.g.:
13948 extern "C" {
13949 ^~~~~~~~~~
13951 parser->innermost_linkage_specification_location
13952 = make_location (extern_token->location,
13953 extern_token->location,
13954 get_finish (string_token->location));
13956 /* If the next token is a `{', then we're using the first
13957 production. */
13958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13960 cp_ensure_no_omp_declare_simd (parser);
13961 cp_ensure_no_oacc_routine (parser);
13963 /* Consume the `{' token. */
13964 matching_braces braces;
13965 braces.consume_open (parser)->location;
13966 /* Parse the declarations. */
13967 cp_parser_declaration_seq_opt (parser);
13968 /* Look for the closing `}'. */
13969 braces.require_close (parser);
13971 /* Otherwise, there's just one declaration. */
13972 else
13974 bool saved_in_unbraced_linkage_specification_p;
13976 saved_in_unbraced_linkage_specification_p
13977 = parser->in_unbraced_linkage_specification_p;
13978 parser->in_unbraced_linkage_specification_p = true;
13979 cp_parser_declaration (parser);
13980 parser->in_unbraced_linkage_specification_p
13981 = saved_in_unbraced_linkage_specification_p;
13984 /* We're done with the linkage-specification. */
13985 pop_lang_context ();
13987 /* Restore location of parent linkage specification, if any. */
13988 parser->innermost_linkage_specification_location = saved_location;
13991 /* Parse a static_assert-declaration.
13993 static_assert-declaration:
13994 static_assert ( constant-expression , string-literal ) ;
13995 static_assert ( constant-expression ) ; (C++17)
13997 If MEMBER_P, this static_assert is a class member. */
13999 static void
14000 cp_parser_static_assert(cp_parser *parser, bool member_p)
14002 cp_expr condition;
14003 location_t token_loc;
14004 tree message;
14005 bool dummy;
14007 /* Peek at the `static_assert' token so we can keep track of exactly
14008 where the static assertion started. */
14009 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14011 /* Look for the `static_assert' keyword. */
14012 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14013 RT_STATIC_ASSERT))
14014 return;
14016 /* We know we are in a static assertion; commit to any tentative
14017 parse. */
14018 if (cp_parser_parsing_tentatively (parser))
14019 cp_parser_commit_to_tentative_parse (parser);
14021 /* Parse the `(' starting the static assertion condition. */
14022 matching_parens parens;
14023 parens.require_open (parser);
14025 /* Parse the constant-expression. Allow a non-constant expression
14026 here in order to give better diagnostics in finish_static_assert. */
14027 condition =
14028 cp_parser_constant_expression (parser,
14029 /*allow_non_constant_p=*/true,
14030 /*non_constant_p=*/&dummy);
14032 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14034 if (cxx_dialect < cxx17)
14035 pedwarn (input_location, OPT_Wpedantic,
14036 "static_assert without a message "
14037 "only available with -std=c++17 or -std=gnu++17");
14038 /* Eat the ')' */
14039 cp_lexer_consume_token (parser->lexer);
14040 message = build_string (1, "");
14041 TREE_TYPE (message) = char_array_type_node;
14042 fix_string_type (message);
14044 else
14046 /* Parse the separating `,'. */
14047 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14049 /* Parse the string-literal message. */
14050 message = cp_parser_string_literal (parser,
14051 /*translate=*/false,
14052 /*wide_ok=*/true);
14054 /* A `)' completes the static assertion. */
14055 if (!parens.require_close (parser))
14056 cp_parser_skip_to_closing_parenthesis (parser,
14057 /*recovering=*/true,
14058 /*or_comma=*/false,
14059 /*consume_paren=*/true);
14062 /* A semicolon terminates the declaration. */
14063 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14065 /* Get the location for the static assertion. Use that of the
14066 condition if available, otherwise, use that of the "static_assert"
14067 token. */
14068 location_t assert_loc = condition.get_location ();
14069 if (assert_loc == UNKNOWN_LOCATION)
14070 assert_loc = token_loc;
14072 /* Complete the static assertion, which may mean either processing
14073 the static assert now or saving it for template instantiation. */
14074 finish_static_assert (condition, message, assert_loc, member_p);
14077 /* Parse the expression in decltype ( expression ). */
14079 static tree
14080 cp_parser_decltype_expr (cp_parser *parser,
14081 bool &id_expression_or_member_access_p)
14083 cp_token *id_expr_start_token;
14084 tree expr;
14086 /* Since we're going to preserve any side-effects from this parse, set up a
14087 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14088 in the expression. */
14089 tentative_firewall firewall (parser);
14091 /* First, try parsing an id-expression. */
14092 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14093 cp_parser_parse_tentatively (parser);
14094 expr = cp_parser_id_expression (parser,
14095 /*template_keyword_p=*/false,
14096 /*check_dependency_p=*/true,
14097 /*template_p=*/NULL,
14098 /*declarator_p=*/false,
14099 /*optional_p=*/false);
14101 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14103 bool non_integral_constant_expression_p = false;
14104 tree id_expression = expr;
14105 cp_id_kind idk;
14106 const char *error_msg;
14108 if (identifier_p (expr))
14109 /* Lookup the name we got back from the id-expression. */
14110 expr = cp_parser_lookup_name_simple (parser, expr,
14111 id_expr_start_token->location);
14113 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14114 /* A template without args is not a complete id-expression. */
14115 expr = error_mark_node;
14117 if (expr
14118 && expr != error_mark_node
14119 && TREE_CODE (expr) != TYPE_DECL
14120 && (TREE_CODE (expr) != BIT_NOT_EXPR
14121 || !TYPE_P (TREE_OPERAND (expr, 0)))
14122 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14124 /* Complete lookup of the id-expression. */
14125 expr = (finish_id_expression
14126 (id_expression, expr, parser->scope, &idk,
14127 /*integral_constant_expression_p=*/false,
14128 /*allow_non_integral_constant_expression_p=*/true,
14129 &non_integral_constant_expression_p,
14130 /*template_p=*/false,
14131 /*done=*/true,
14132 /*address_p=*/false,
14133 /*template_arg_p=*/false,
14134 &error_msg,
14135 id_expr_start_token->location));
14137 if (expr == error_mark_node)
14138 /* We found an id-expression, but it was something that we
14139 should not have found. This is an error, not something
14140 we can recover from, so note that we found an
14141 id-expression and we'll recover as gracefully as
14142 possible. */
14143 id_expression_or_member_access_p = true;
14146 if (expr
14147 && expr != error_mark_node
14148 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14149 /* We have an id-expression. */
14150 id_expression_or_member_access_p = true;
14153 if (!id_expression_or_member_access_p)
14155 /* Abort the id-expression parse. */
14156 cp_parser_abort_tentative_parse (parser);
14158 /* Parsing tentatively, again. */
14159 cp_parser_parse_tentatively (parser);
14161 /* Parse a class member access. */
14162 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14163 /*cast_p=*/false, /*decltype*/true,
14164 /*member_access_only_p=*/true, NULL);
14166 if (expr
14167 && expr != error_mark_node
14168 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14169 /* We have an id-expression. */
14170 id_expression_or_member_access_p = true;
14173 if (id_expression_or_member_access_p)
14174 /* We have parsed the complete id-expression or member access. */
14175 cp_parser_parse_definitely (parser);
14176 else
14178 /* Abort our attempt to parse an id-expression or member access
14179 expression. */
14180 cp_parser_abort_tentative_parse (parser);
14182 /* Commit to the tentative_firewall so we get syntax errors. */
14183 cp_parser_commit_to_tentative_parse (parser);
14185 /* Parse a full expression. */
14186 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14187 /*decltype_p=*/true);
14190 return expr;
14193 /* Parse a `decltype' type. Returns the type.
14195 simple-type-specifier:
14196 decltype ( expression )
14197 C++14 proposal:
14198 decltype ( auto ) */
14200 static tree
14201 cp_parser_decltype (cp_parser *parser)
14203 bool id_expression_or_member_access_p = false;
14204 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14206 if (start_token->type == CPP_DECLTYPE)
14208 /* Already parsed. */
14209 cp_lexer_consume_token (parser->lexer);
14210 return saved_checks_value (start_token->u.tree_check_value);
14213 /* Look for the `decltype' token. */
14214 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14215 return error_mark_node;
14217 /* Parse the opening `('. */
14218 matching_parens parens;
14219 if (!parens.require_open (parser))
14220 return error_mark_node;
14222 push_deferring_access_checks (dk_deferred);
14224 tree expr = NULL_TREE;
14226 if (cxx_dialect >= cxx14
14227 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14228 /* decltype (auto) */
14229 cp_lexer_consume_token (parser->lexer);
14230 else
14232 /* decltype (expression) */
14234 /* Types cannot be defined in a `decltype' expression. Save away the
14235 old message and set the new one. */
14236 const char *saved_message = parser->type_definition_forbidden_message;
14237 parser->type_definition_forbidden_message
14238 = G_("types may not be defined in %<decltype%> expressions");
14240 /* The restrictions on constant-expressions do not apply inside
14241 decltype expressions. */
14242 bool saved_integral_constant_expression_p
14243 = parser->integral_constant_expression_p;
14244 bool saved_non_integral_constant_expression_p
14245 = parser->non_integral_constant_expression_p;
14246 parser->integral_constant_expression_p = false;
14248 /* Within a parenthesized expression, a `>' token is always
14249 the greater-than operator. */
14250 bool saved_greater_than_is_operator_p
14251 = parser->greater_than_is_operator_p;
14252 parser->greater_than_is_operator_p = true;
14254 /* Do not actually evaluate the expression. */
14255 ++cp_unevaluated_operand;
14257 /* Do not warn about problems with the expression. */
14258 ++c_inhibit_evaluation_warnings;
14260 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14262 /* Go back to evaluating expressions. */
14263 --cp_unevaluated_operand;
14264 --c_inhibit_evaluation_warnings;
14266 /* The `>' token might be the end of a template-id or
14267 template-parameter-list now. */
14268 parser->greater_than_is_operator_p
14269 = saved_greater_than_is_operator_p;
14271 /* Restore the old message and the integral constant expression
14272 flags. */
14273 parser->type_definition_forbidden_message = saved_message;
14274 parser->integral_constant_expression_p
14275 = saved_integral_constant_expression_p;
14276 parser->non_integral_constant_expression_p
14277 = saved_non_integral_constant_expression_p;
14280 /* Parse to the closing `)'. */
14281 if (!parens.require_close (parser))
14283 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14284 /*consume_paren=*/true);
14285 pop_deferring_access_checks ();
14286 return error_mark_node;
14289 if (!expr)
14291 /* Build auto. */
14292 expr = make_decltype_auto ();
14293 AUTO_IS_DECLTYPE (expr) = true;
14295 else
14296 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14297 tf_warning_or_error);
14299 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14300 it again. */
14301 start_token->type = CPP_DECLTYPE;
14302 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14303 start_token->u.tree_check_value->value = expr;
14304 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14305 start_token->keyword = RID_MAX;
14306 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14308 pop_to_parent_deferring_access_checks ();
14310 return expr;
14313 /* Special member functions [gram.special] */
14315 /* Parse a conversion-function-id.
14317 conversion-function-id:
14318 operator conversion-type-id
14320 Returns an IDENTIFIER_NODE representing the operator. */
14322 static tree
14323 cp_parser_conversion_function_id (cp_parser* parser)
14325 tree type;
14326 tree saved_scope;
14327 tree saved_qualifying_scope;
14328 tree saved_object_scope;
14329 tree pushed_scope = NULL_TREE;
14331 /* Look for the `operator' token. */
14332 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14333 return error_mark_node;
14334 /* When we parse the conversion-type-id, the current scope will be
14335 reset. However, we need that information in able to look up the
14336 conversion function later, so we save it here. */
14337 saved_scope = parser->scope;
14338 saved_qualifying_scope = parser->qualifying_scope;
14339 saved_object_scope = parser->object_scope;
14340 /* We must enter the scope of the class so that the names of
14341 entities declared within the class are available in the
14342 conversion-type-id. For example, consider:
14344 struct S {
14345 typedef int I;
14346 operator I();
14349 S::operator I() { ... }
14351 In order to see that `I' is a type-name in the definition, we
14352 must be in the scope of `S'. */
14353 if (saved_scope)
14354 pushed_scope = push_scope (saved_scope);
14355 /* Parse the conversion-type-id. */
14356 type = cp_parser_conversion_type_id (parser);
14357 /* Leave the scope of the class, if any. */
14358 if (pushed_scope)
14359 pop_scope (pushed_scope);
14360 /* Restore the saved scope. */
14361 parser->scope = saved_scope;
14362 parser->qualifying_scope = saved_qualifying_scope;
14363 parser->object_scope = saved_object_scope;
14364 /* If the TYPE is invalid, indicate failure. */
14365 if (type == error_mark_node)
14366 return error_mark_node;
14367 return make_conv_op_name (type);
14370 /* Parse a conversion-type-id:
14372 conversion-type-id:
14373 type-specifier-seq conversion-declarator [opt]
14375 Returns the TYPE specified. */
14377 static tree
14378 cp_parser_conversion_type_id (cp_parser* parser)
14380 tree attributes;
14381 cp_decl_specifier_seq type_specifiers;
14382 cp_declarator *declarator;
14383 tree type_specified;
14384 const char *saved_message;
14386 /* Parse the attributes. */
14387 attributes = cp_parser_attributes_opt (parser);
14389 saved_message = parser->type_definition_forbidden_message;
14390 parser->type_definition_forbidden_message
14391 = G_("types may not be defined in a conversion-type-id");
14393 /* Parse the type-specifiers. */
14394 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14395 /*is_trailing_return=*/false,
14396 &type_specifiers);
14398 parser->type_definition_forbidden_message = saved_message;
14400 /* If that didn't work, stop. */
14401 if (type_specifiers.type == error_mark_node)
14402 return error_mark_node;
14403 /* Parse the conversion-declarator. */
14404 declarator = cp_parser_conversion_declarator_opt (parser);
14406 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14407 /*initialized=*/0, &attributes);
14408 if (attributes)
14409 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14411 /* Don't give this error when parsing tentatively. This happens to
14412 work because we always parse this definitively once. */
14413 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14414 && type_uses_auto (type_specified))
14416 if (cxx_dialect < cxx14)
14418 error ("invalid use of %<auto%> in conversion operator");
14419 return error_mark_node;
14421 else if (template_parm_scope_p ())
14422 warning (0, "use of %<auto%> in member template "
14423 "conversion operator can never be deduced");
14426 return type_specified;
14429 /* Parse an (optional) conversion-declarator.
14431 conversion-declarator:
14432 ptr-operator conversion-declarator [opt]
14436 static cp_declarator *
14437 cp_parser_conversion_declarator_opt (cp_parser* parser)
14439 enum tree_code code;
14440 tree class_type, std_attributes = NULL_TREE;
14441 cp_cv_quals cv_quals;
14443 /* We don't know if there's a ptr-operator next, or not. */
14444 cp_parser_parse_tentatively (parser);
14445 /* Try the ptr-operator. */
14446 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14447 &std_attributes);
14448 /* If it worked, look for more conversion-declarators. */
14449 if (cp_parser_parse_definitely (parser))
14451 cp_declarator *declarator;
14453 /* Parse another optional declarator. */
14454 declarator = cp_parser_conversion_declarator_opt (parser);
14456 declarator = cp_parser_make_indirect_declarator
14457 (code, class_type, cv_quals, declarator, std_attributes);
14459 return declarator;
14462 return NULL;
14465 /* Parse an (optional) ctor-initializer.
14467 ctor-initializer:
14468 : mem-initializer-list */
14470 static void
14471 cp_parser_ctor_initializer_opt (cp_parser* parser)
14473 /* If the next token is not a `:', then there is no
14474 ctor-initializer. */
14475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14477 /* Do default initialization of any bases and members. */
14478 if (DECL_CONSTRUCTOR_P (current_function_decl))
14479 finish_mem_initializers (NULL_TREE);
14480 return;
14483 /* Consume the `:' token. */
14484 cp_lexer_consume_token (parser->lexer);
14485 /* And the mem-initializer-list. */
14486 cp_parser_mem_initializer_list (parser);
14489 /* Parse a mem-initializer-list.
14491 mem-initializer-list:
14492 mem-initializer ... [opt]
14493 mem-initializer ... [opt] , mem-initializer-list */
14495 static void
14496 cp_parser_mem_initializer_list (cp_parser* parser)
14498 tree mem_initializer_list = NULL_TREE;
14499 tree target_ctor = error_mark_node;
14500 cp_token *token = cp_lexer_peek_token (parser->lexer);
14502 /* Let the semantic analysis code know that we are starting the
14503 mem-initializer-list. */
14504 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14505 error_at (token->location,
14506 "only constructors take member initializers");
14508 /* Loop through the list. */
14509 while (true)
14511 tree mem_initializer;
14513 token = cp_lexer_peek_token (parser->lexer);
14514 /* Parse the mem-initializer. */
14515 mem_initializer = cp_parser_mem_initializer (parser);
14516 /* If the next token is a `...', we're expanding member initializers. */
14517 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14518 if (ellipsis
14519 || (mem_initializer != error_mark_node
14520 && check_for_bare_parameter_packs (TREE_PURPOSE
14521 (mem_initializer))))
14523 /* Consume the `...'. */
14524 if (ellipsis)
14525 cp_lexer_consume_token (parser->lexer);
14527 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14528 can be expanded but members cannot. */
14529 if (mem_initializer != error_mark_node
14530 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14532 error_at (token->location,
14533 "cannot expand initializer for member %qD",
14534 TREE_PURPOSE (mem_initializer));
14535 mem_initializer = error_mark_node;
14538 /* Construct the pack expansion type. */
14539 if (mem_initializer != error_mark_node)
14540 mem_initializer = make_pack_expansion (mem_initializer);
14542 if (target_ctor != error_mark_node
14543 && mem_initializer != error_mark_node)
14545 error ("mem-initializer for %qD follows constructor delegation",
14546 TREE_PURPOSE (mem_initializer));
14547 mem_initializer = error_mark_node;
14549 /* Look for a target constructor. */
14550 if (mem_initializer != error_mark_node
14551 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14552 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14554 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14555 if (mem_initializer_list)
14557 error ("constructor delegation follows mem-initializer for %qD",
14558 TREE_PURPOSE (mem_initializer_list));
14559 mem_initializer = error_mark_node;
14561 target_ctor = mem_initializer;
14563 /* Add it to the list, unless it was erroneous. */
14564 if (mem_initializer != error_mark_node)
14566 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14567 mem_initializer_list = mem_initializer;
14569 /* If the next token is not a `,', we're done. */
14570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14571 break;
14572 /* Consume the `,' token. */
14573 cp_lexer_consume_token (parser->lexer);
14576 /* Perform semantic analysis. */
14577 if (DECL_CONSTRUCTOR_P (current_function_decl))
14578 finish_mem_initializers (mem_initializer_list);
14581 /* Parse a mem-initializer.
14583 mem-initializer:
14584 mem-initializer-id ( expression-list [opt] )
14585 mem-initializer-id braced-init-list
14587 GNU extension:
14589 mem-initializer:
14590 ( expression-list [opt] )
14592 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14593 class) or FIELD_DECL (for a non-static data member) to initialize;
14594 the TREE_VALUE is the expression-list. An empty initialization
14595 list is represented by void_list_node. */
14597 static tree
14598 cp_parser_mem_initializer (cp_parser* parser)
14600 tree mem_initializer_id;
14601 tree expression_list;
14602 tree member;
14603 cp_token *token = cp_lexer_peek_token (parser->lexer);
14605 /* Find out what is being initialized. */
14606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14608 permerror (token->location,
14609 "anachronistic old-style base class initializer");
14610 mem_initializer_id = NULL_TREE;
14612 else
14614 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14615 if (mem_initializer_id == error_mark_node)
14616 return mem_initializer_id;
14618 member = expand_member_init (mem_initializer_id);
14619 if (member && !DECL_P (member))
14620 in_base_initializer = 1;
14622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14624 bool expr_non_constant_p;
14625 cp_lexer_set_source_position (parser->lexer);
14626 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14627 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14628 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14629 expression_list = build_tree_list (NULL_TREE, expression_list);
14631 else
14633 vec<tree, va_gc> *vec;
14634 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14635 /*cast_p=*/false,
14636 /*allow_expansion_p=*/true,
14637 /*non_constant_p=*/NULL);
14638 if (vec == NULL)
14639 return error_mark_node;
14640 expression_list = build_tree_list_vec (vec);
14641 release_tree_vector (vec);
14644 if (expression_list == error_mark_node)
14645 return error_mark_node;
14646 if (!expression_list)
14647 expression_list = void_type_node;
14649 in_base_initializer = 0;
14651 return member ? build_tree_list (member, expression_list) : error_mark_node;
14654 /* Parse a mem-initializer-id.
14656 mem-initializer-id:
14657 :: [opt] nested-name-specifier [opt] class-name
14658 decltype-specifier (C++11)
14659 identifier
14661 Returns a TYPE indicating the class to be initialized for the first
14662 production (and the second in C++11). Returns an IDENTIFIER_NODE
14663 indicating the data member to be initialized for the last production. */
14665 static tree
14666 cp_parser_mem_initializer_id (cp_parser* parser)
14668 bool global_scope_p;
14669 bool nested_name_specifier_p;
14670 bool template_p = false;
14671 tree id;
14673 cp_token *token = cp_lexer_peek_token (parser->lexer);
14675 /* `typename' is not allowed in this context ([temp.res]). */
14676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14678 error_at (token->location,
14679 "keyword %<typename%> not allowed in this context (a qualified "
14680 "member initializer is implicitly a type)");
14681 cp_lexer_consume_token (parser->lexer);
14683 /* Look for the optional `::' operator. */
14684 global_scope_p
14685 = (cp_parser_global_scope_opt (parser,
14686 /*current_scope_valid_p=*/false)
14687 != NULL_TREE);
14688 /* Look for the optional nested-name-specifier. The simplest way to
14689 implement:
14691 [temp.res]
14693 The keyword `typename' is not permitted in a base-specifier or
14694 mem-initializer; in these contexts a qualified name that
14695 depends on a template-parameter is implicitly assumed to be a
14696 type name.
14698 is to assume that we have seen the `typename' keyword at this
14699 point. */
14700 nested_name_specifier_p
14701 = (cp_parser_nested_name_specifier_opt (parser,
14702 /*typename_keyword_p=*/true,
14703 /*check_dependency_p=*/true,
14704 /*type_p=*/true,
14705 /*is_declaration=*/true)
14706 != NULL_TREE);
14707 if (nested_name_specifier_p)
14708 template_p = cp_parser_optional_template_keyword (parser);
14709 /* If there is a `::' operator or a nested-name-specifier, then we
14710 are definitely looking for a class-name. */
14711 if (global_scope_p || nested_name_specifier_p)
14712 return cp_parser_class_name (parser,
14713 /*typename_keyword_p=*/true,
14714 /*template_keyword_p=*/template_p,
14715 typename_type,
14716 /*check_dependency_p=*/true,
14717 /*class_head_p=*/false,
14718 /*is_declaration=*/true);
14719 /* Otherwise, we could also be looking for an ordinary identifier. */
14720 cp_parser_parse_tentatively (parser);
14721 if (cp_lexer_next_token_is_decltype (parser->lexer))
14722 /* Try a decltype-specifier. */
14723 id = cp_parser_decltype (parser);
14724 else
14725 /* Otherwise, try a class-name. */
14726 id = cp_parser_class_name (parser,
14727 /*typename_keyword_p=*/true,
14728 /*template_keyword_p=*/false,
14729 none_type,
14730 /*check_dependency_p=*/true,
14731 /*class_head_p=*/false,
14732 /*is_declaration=*/true);
14733 /* If we found one, we're done. */
14734 if (cp_parser_parse_definitely (parser))
14735 return id;
14736 /* Otherwise, look for an ordinary identifier. */
14737 return cp_parser_identifier (parser);
14740 /* Overloading [gram.over] */
14742 /* Parse an operator-function-id.
14744 operator-function-id:
14745 operator operator
14747 Returns an IDENTIFIER_NODE for the operator which is a
14748 human-readable spelling of the identifier, e.g., `operator +'. */
14750 static cp_expr
14751 cp_parser_operator_function_id (cp_parser* parser)
14753 /* Look for the `operator' keyword. */
14754 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14755 return error_mark_node;
14756 /* And then the name of the operator itself. */
14757 return cp_parser_operator (parser);
14760 /* Return an identifier node for a user-defined literal operator.
14761 The suffix identifier is chained to the operator name identifier. */
14763 tree
14764 cp_literal_operator_id (const char* name)
14766 tree identifier;
14767 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14768 + strlen (name) + 10);
14769 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14770 identifier = get_identifier (buffer);
14772 return identifier;
14775 /* Parse an operator.
14777 operator:
14778 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14779 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14780 || ++ -- , ->* -> () []
14782 GNU Extensions:
14784 operator:
14785 <? >? <?= >?=
14787 Returns an IDENTIFIER_NODE for the operator which is a
14788 human-readable spelling of the identifier, e.g., `operator +'. */
14790 static cp_expr
14791 cp_parser_operator (cp_parser* parser)
14793 tree id = NULL_TREE;
14794 cp_token *token;
14795 bool utf8 = false;
14797 /* Peek at the next token. */
14798 token = cp_lexer_peek_token (parser->lexer);
14800 location_t start_loc = token->location;
14802 /* Figure out which operator we have. */
14803 enum tree_code op = ERROR_MARK;
14804 bool assop = false;
14805 bool consumed = false;
14806 switch (token->type)
14808 case CPP_KEYWORD:
14810 /* The keyword should be either `new' or `delete'. */
14811 if (token->keyword == RID_NEW)
14812 op = NEW_EXPR;
14813 else if (token->keyword == RID_DELETE)
14814 op = DELETE_EXPR;
14815 else
14816 break;
14818 /* Consume the `new' or `delete' token. */
14819 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14821 /* Peek at the next token. */
14822 token = cp_lexer_peek_token (parser->lexer);
14823 /* If it's a `[' token then this is the array variant of the
14824 operator. */
14825 if (token->type == CPP_OPEN_SQUARE)
14827 /* Consume the `[' token. */
14828 cp_lexer_consume_token (parser->lexer);
14829 /* Look for the `]' token. */
14830 if (cp_token *close_token
14831 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14832 end_loc = close_token->location;
14833 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14835 start_loc = make_location (start_loc, start_loc, end_loc);
14836 consumed = true;
14837 break;
14840 case CPP_PLUS:
14841 op = PLUS_EXPR;
14842 break;
14844 case CPP_MINUS:
14845 op = MINUS_EXPR;
14846 break;
14848 case CPP_MULT:
14849 op = MULT_EXPR;
14850 break;
14852 case CPP_DIV:
14853 op = TRUNC_DIV_EXPR;
14854 break;
14856 case CPP_MOD:
14857 op = TRUNC_MOD_EXPR;
14858 break;
14860 case CPP_XOR:
14861 op = BIT_XOR_EXPR;
14862 break;
14864 case CPP_AND:
14865 op = BIT_AND_EXPR;
14866 break;
14868 case CPP_OR:
14869 op = BIT_IOR_EXPR;
14870 break;
14872 case CPP_COMPL:
14873 op = BIT_NOT_EXPR;
14874 break;
14876 case CPP_NOT:
14877 op = TRUTH_NOT_EXPR;
14878 break;
14880 case CPP_EQ:
14881 assop = true;
14882 op = NOP_EXPR;
14883 break;
14885 case CPP_LESS:
14886 op = LT_EXPR;
14887 break;
14889 case CPP_GREATER:
14890 op = GT_EXPR;
14891 break;
14893 case CPP_PLUS_EQ:
14894 assop = true;
14895 op = PLUS_EXPR;
14896 break;
14898 case CPP_MINUS_EQ:
14899 assop = true;
14900 op = MINUS_EXPR;
14901 break;
14903 case CPP_MULT_EQ:
14904 assop = true;
14905 op = MULT_EXPR;
14906 break;
14908 case CPP_DIV_EQ:
14909 assop = true;
14910 op = TRUNC_DIV_EXPR;
14911 break;
14913 case CPP_MOD_EQ:
14914 assop = true;
14915 op = TRUNC_MOD_EXPR;
14916 break;
14918 case CPP_XOR_EQ:
14919 assop = true;
14920 op = BIT_XOR_EXPR;
14921 break;
14923 case CPP_AND_EQ:
14924 assop = true;
14925 op = BIT_AND_EXPR;
14926 break;
14928 case CPP_OR_EQ:
14929 assop = true;
14930 op = BIT_IOR_EXPR;
14931 break;
14933 case CPP_LSHIFT:
14934 op = LSHIFT_EXPR;
14935 break;
14937 case CPP_RSHIFT:
14938 op = RSHIFT_EXPR;
14939 break;
14941 case CPP_LSHIFT_EQ:
14942 assop = true;
14943 op = LSHIFT_EXPR;
14944 break;
14946 case CPP_RSHIFT_EQ:
14947 assop = true;
14948 op = RSHIFT_EXPR;
14949 break;
14951 case CPP_EQ_EQ:
14952 op = EQ_EXPR;
14953 break;
14955 case CPP_NOT_EQ:
14956 op = NE_EXPR;
14957 break;
14959 case CPP_LESS_EQ:
14960 op = LE_EXPR;
14961 break;
14963 case CPP_GREATER_EQ:
14964 op = GE_EXPR;
14965 break;
14967 case CPP_AND_AND:
14968 op = TRUTH_ANDIF_EXPR;
14969 break;
14971 case CPP_OR_OR:
14972 op = TRUTH_ORIF_EXPR;
14973 break;
14975 case CPP_PLUS_PLUS:
14976 op = POSTINCREMENT_EXPR;
14977 break;
14979 case CPP_MINUS_MINUS:
14980 op = PREDECREMENT_EXPR;
14981 break;
14983 case CPP_COMMA:
14984 op = COMPOUND_EXPR;
14985 break;
14987 case CPP_DEREF_STAR:
14988 op = MEMBER_REF;
14989 break;
14991 case CPP_DEREF:
14992 op = COMPONENT_REF;
14993 break;
14995 case CPP_OPEN_PAREN:
14997 /* Consume the `('. */
14998 matching_parens parens;
14999 parens.consume_open (parser);
15000 /* Look for the matching `)'. */
15001 parens.require_close (parser);
15002 op = CALL_EXPR;
15003 consumed = true;
15004 break;
15007 case CPP_OPEN_SQUARE:
15008 /* Consume the `['. */
15009 cp_lexer_consume_token (parser->lexer);
15010 /* Look for the matching `]'. */
15011 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15012 op = ARRAY_REF;
15013 consumed = true;
15014 break;
15016 case CPP_UTF8STRING:
15017 case CPP_UTF8STRING_USERDEF:
15018 utf8 = true;
15019 /* FALLTHRU */
15020 case CPP_STRING:
15021 case CPP_WSTRING:
15022 case CPP_STRING16:
15023 case CPP_STRING32:
15024 case CPP_STRING_USERDEF:
15025 case CPP_WSTRING_USERDEF:
15026 case CPP_STRING16_USERDEF:
15027 case CPP_STRING32_USERDEF:
15029 tree str, string_tree;
15030 int sz, len;
15032 if (cxx_dialect == cxx98)
15033 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15035 /* Consume the string. */
15036 str = cp_parser_string_literal (parser, /*translate=*/true,
15037 /*wide_ok=*/true, /*lookup_udlit=*/false);
15038 if (str == error_mark_node)
15039 return error_mark_node;
15040 else if (TREE_CODE (str) == USERDEF_LITERAL)
15042 string_tree = USERDEF_LITERAL_VALUE (str);
15043 id = USERDEF_LITERAL_SUFFIX_ID (str);
15045 else
15047 string_tree = str;
15048 /* Look for the suffix identifier. */
15049 token = cp_lexer_peek_token (parser->lexer);
15050 if (token->type == CPP_NAME)
15051 id = cp_parser_identifier (parser);
15052 else if (token->type == CPP_KEYWORD)
15054 error ("unexpected keyword;"
15055 " remove space between quotes and suffix identifier");
15056 return error_mark_node;
15058 else
15060 error ("expected suffix identifier");
15061 return error_mark_node;
15064 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15065 (TREE_TYPE (TREE_TYPE (string_tree))));
15066 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15067 if (len != 0)
15069 error ("expected empty string after %<operator%> keyword");
15070 return error_mark_node;
15072 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15073 != char_type_node)
15075 error ("invalid encoding prefix in literal operator");
15076 return error_mark_node;
15078 if (id != error_mark_node)
15080 const char *name = IDENTIFIER_POINTER (id);
15081 id = cp_literal_operator_id (name);
15083 return id;
15086 default:
15087 /* Anything else is an error. */
15088 break;
15091 /* If we have selected an identifier, we need to consume the
15092 operator token. */
15093 if (op != ERROR_MARK)
15095 id = ovl_op_identifier (assop, op);
15096 if (!consumed)
15097 cp_lexer_consume_token (parser->lexer);
15099 /* Otherwise, no valid operator name was present. */
15100 else
15102 cp_parser_error (parser, "expected operator");
15103 id = error_mark_node;
15106 return cp_expr (id, start_loc);
15109 /* Parse a template-declaration.
15111 template-declaration:
15112 export [opt] template < template-parameter-list > declaration
15114 If MEMBER_P is TRUE, this template-declaration occurs within a
15115 class-specifier.
15117 The grammar rule given by the standard isn't correct. What
15118 is really meant is:
15120 template-declaration:
15121 export [opt] template-parameter-list-seq
15122 decl-specifier-seq [opt] init-declarator [opt] ;
15123 export [opt] template-parameter-list-seq
15124 function-definition
15126 template-parameter-list-seq:
15127 template-parameter-list-seq [opt]
15128 template < template-parameter-list >
15130 Concept Extensions:
15132 template-parameter-list-seq:
15133 template < template-parameter-list > requires-clause [opt]
15135 requires-clause:
15136 requires logical-or-expression */
15138 static void
15139 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15141 /* Check for `export'. */
15142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15144 /* Consume the `export' token. */
15145 cp_lexer_consume_token (parser->lexer);
15146 /* Warn that we do not support `export'. */
15147 warning (0, "keyword %<export%> not implemented, and will be ignored");
15150 cp_parser_template_declaration_after_export (parser, member_p);
15153 /* Parse a template-parameter-list.
15155 template-parameter-list:
15156 template-parameter
15157 template-parameter-list , template-parameter
15159 Returns a TREE_LIST. Each node represents a template parameter.
15160 The nodes are connected via their TREE_CHAINs. */
15162 static tree
15163 cp_parser_template_parameter_list (cp_parser* parser)
15165 tree parameter_list = NULL_TREE;
15167 begin_template_parm_list ();
15169 /* The loop below parses the template parms. We first need to know
15170 the total number of template parms to be able to compute proper
15171 canonical types of each dependent type. So after the loop, when
15172 we know the total number of template parms,
15173 end_template_parm_list computes the proper canonical types and
15174 fixes up the dependent types accordingly. */
15175 while (true)
15177 tree parameter;
15178 bool is_non_type;
15179 bool is_parameter_pack;
15180 location_t parm_loc;
15182 /* Parse the template-parameter. */
15183 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15184 parameter = cp_parser_template_parameter (parser,
15185 &is_non_type,
15186 &is_parameter_pack);
15187 /* Add it to the list. */
15188 if (parameter != error_mark_node)
15189 parameter_list = process_template_parm (parameter_list,
15190 parm_loc,
15191 parameter,
15192 is_non_type,
15193 is_parameter_pack);
15194 else
15196 tree err_parm = build_tree_list (parameter, parameter);
15197 parameter_list = chainon (parameter_list, err_parm);
15200 /* If the next token is not a `,', we're done. */
15201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15202 break;
15203 /* Otherwise, consume the `,' token. */
15204 cp_lexer_consume_token (parser->lexer);
15207 return end_template_parm_list (parameter_list);
15210 /* Parse a introduction-list.
15212 introduction-list:
15213 introduced-parameter
15214 introduction-list , introduced-parameter
15216 introduced-parameter:
15217 ...[opt] identifier
15219 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15220 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15221 WILDCARD_DECL will also have DECL_NAME set and token location in
15222 DECL_SOURCE_LOCATION. */
15224 static tree
15225 cp_parser_introduction_list (cp_parser *parser)
15227 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15229 while (true)
15231 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15232 if (is_pack)
15233 cp_lexer_consume_token (parser->lexer);
15235 /* Build placeholder. */
15236 tree parm = build_nt (WILDCARD_DECL);
15237 DECL_SOURCE_LOCATION (parm)
15238 = cp_lexer_peek_token (parser->lexer)->location;
15239 DECL_NAME (parm) = cp_parser_identifier (parser);
15240 WILDCARD_PACK_P (parm) = is_pack;
15241 vec_safe_push (introduction_vec, parm);
15243 /* If the next token is not a `,', we're done. */
15244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15245 break;
15246 /* Otherwise, consume the `,' token. */
15247 cp_lexer_consume_token (parser->lexer);
15250 /* Convert the vec into a TREE_VEC. */
15251 tree introduction_list = make_tree_vec (introduction_vec->length ());
15252 unsigned int n;
15253 tree parm;
15254 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15255 TREE_VEC_ELT (introduction_list, n) = parm;
15257 release_tree_vector (introduction_vec);
15258 return introduction_list;
15261 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15262 is an abstract declarator. */
15264 static inline cp_declarator*
15265 get_id_declarator (cp_declarator *declarator)
15267 cp_declarator *d = declarator;
15268 while (d && d->kind != cdk_id)
15269 d = d->declarator;
15270 return d;
15273 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15274 is an abstract declarator. */
15276 static inline tree
15277 get_unqualified_id (cp_declarator *declarator)
15279 declarator = get_id_declarator (declarator);
15280 if (declarator)
15281 return declarator->u.id.unqualified_name;
15282 else
15283 return NULL_TREE;
15286 /* Returns true if DECL represents a constrained-parameter. */
15288 static inline bool
15289 is_constrained_parameter (tree decl)
15291 return (decl
15292 && TREE_CODE (decl) == TYPE_DECL
15293 && CONSTRAINED_PARM_CONCEPT (decl)
15294 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15297 /* Returns true if PARM declares a constrained-parameter. */
15299 static inline bool
15300 is_constrained_parameter (cp_parameter_declarator *parm)
15302 return is_constrained_parameter (parm->decl_specifiers.type);
15305 /* Check that the type parameter is only a declarator-id, and that its
15306 type is not cv-qualified. */
15308 bool
15309 cp_parser_check_constrained_type_parm (cp_parser *parser,
15310 cp_parameter_declarator *parm)
15312 if (!parm->declarator)
15313 return true;
15315 if (parm->declarator->kind != cdk_id)
15317 cp_parser_error (parser, "invalid constrained type parameter");
15318 return false;
15321 /* Don't allow cv-qualified type parameters. */
15322 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15323 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15325 cp_parser_error (parser, "cv-qualified type parameter");
15326 return false;
15329 return true;
15332 /* Finish parsing/processing a template type parameter and checking
15333 various restrictions. */
15335 static inline tree
15336 cp_parser_constrained_type_template_parm (cp_parser *parser,
15337 tree id,
15338 cp_parameter_declarator* parmdecl)
15340 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15341 return finish_template_type_parm (class_type_node, id);
15342 else
15343 return error_mark_node;
15346 static tree
15347 finish_constrained_template_template_parm (tree proto, tree id)
15349 /* FIXME: This should probably be copied, and we may need to adjust
15350 the template parameter depths. */
15351 tree saved_parms = current_template_parms;
15352 begin_template_parm_list ();
15353 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15354 end_template_parm_list ();
15356 tree parm = finish_template_template_parm (class_type_node, id);
15357 current_template_parms = saved_parms;
15359 return parm;
15362 /* Finish parsing/processing a template template parameter by borrowing
15363 the template parameter list from the prototype parameter. */
15365 static tree
15366 cp_parser_constrained_template_template_parm (cp_parser *parser,
15367 tree proto,
15368 tree id,
15369 cp_parameter_declarator *parmdecl)
15371 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15372 return error_mark_node;
15373 return finish_constrained_template_template_parm (proto, id);
15376 /* Create a new non-type template parameter from the given PARM
15377 declarator. */
15379 static tree
15380 constrained_non_type_template_parm (bool *is_non_type,
15381 cp_parameter_declarator *parm)
15383 *is_non_type = true;
15384 cp_declarator *decl = parm->declarator;
15385 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15386 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15387 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15390 /* Build a constrained template parameter based on the PARMDECL
15391 declarator. The type of PARMDECL is the constrained type, which
15392 refers to the prototype template parameter that ultimately
15393 specifies the type of the declared parameter. */
15395 static tree
15396 finish_constrained_parameter (cp_parser *parser,
15397 cp_parameter_declarator *parmdecl,
15398 bool *is_non_type,
15399 bool *is_parameter_pack)
15401 tree decl = parmdecl->decl_specifiers.type;
15402 tree id = get_unqualified_id (parmdecl->declarator);
15403 tree def = parmdecl->default_argument;
15404 tree proto = DECL_INITIAL (decl);
15406 /* A template parameter constrained by a variadic concept shall also
15407 be declared as a template parameter pack. */
15408 bool is_variadic = template_parameter_pack_p (proto);
15409 if (is_variadic && !*is_parameter_pack)
15410 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15412 /* Build the parameter. Return an error if the declarator was invalid. */
15413 tree parm;
15414 if (TREE_CODE (proto) == TYPE_DECL)
15415 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15416 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15417 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15418 parmdecl);
15419 else
15420 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15421 if (parm == error_mark_node)
15422 return error_mark_node;
15424 /* Finish the parameter decl and create a node attaching the
15425 default argument and constraint. */
15426 parm = build_tree_list (def, parm);
15427 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15429 return parm;
15432 /* Returns true if the parsed type actually represents the declaration
15433 of a type template-parameter. */
15435 static inline bool
15436 declares_constrained_type_template_parameter (tree type)
15438 return (is_constrained_parameter (type)
15439 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15443 /* Returns true if the parsed type actually represents the declaration of
15444 a template template-parameter. */
15446 static bool
15447 declares_constrained_template_template_parameter (tree type)
15449 return (is_constrained_parameter (type)
15450 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15453 /* Parse a default argument for a type template-parameter.
15454 Note that diagnostics are handled in cp_parser_template_parameter. */
15456 static tree
15457 cp_parser_default_type_template_argument (cp_parser *parser)
15459 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15461 /* Consume the `=' token. */
15462 cp_lexer_consume_token (parser->lexer);
15464 cp_token *token = cp_lexer_peek_token (parser->lexer);
15466 /* Parse the default-argument. */
15467 push_deferring_access_checks (dk_no_deferred);
15468 tree default_argument = cp_parser_type_id (parser);
15469 pop_deferring_access_checks ();
15471 if (flag_concepts && type_uses_auto (default_argument))
15473 error_at (token->location,
15474 "invalid use of %<auto%> in default template argument");
15475 return error_mark_node;
15478 return default_argument;
15481 /* Parse a default argument for a template template-parameter. */
15483 static tree
15484 cp_parser_default_template_template_argument (cp_parser *parser)
15486 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15488 bool is_template;
15490 /* Consume the `='. */
15491 cp_lexer_consume_token (parser->lexer);
15492 /* Parse the id-expression. */
15493 push_deferring_access_checks (dk_no_deferred);
15494 /* save token before parsing the id-expression, for error
15495 reporting */
15496 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15497 tree default_argument
15498 = cp_parser_id_expression (parser,
15499 /*template_keyword_p=*/false,
15500 /*check_dependency_p=*/true,
15501 /*template_p=*/&is_template,
15502 /*declarator_p=*/false,
15503 /*optional_p=*/false);
15504 if (TREE_CODE (default_argument) == TYPE_DECL)
15505 /* If the id-expression was a template-id that refers to
15506 a template-class, we already have the declaration here,
15507 so no further lookup is needed. */
15509 else
15510 /* Look up the name. */
15511 default_argument
15512 = cp_parser_lookup_name (parser, default_argument,
15513 none_type,
15514 /*is_template=*/is_template,
15515 /*is_namespace=*/false,
15516 /*check_dependency=*/true,
15517 /*ambiguous_decls=*/NULL,
15518 token->location);
15519 /* See if the default argument is valid. */
15520 default_argument = check_template_template_default_arg (default_argument);
15521 pop_deferring_access_checks ();
15522 return default_argument;
15525 /* Parse a template-parameter.
15527 template-parameter:
15528 type-parameter
15529 parameter-declaration
15531 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15532 the parameter. The TREE_PURPOSE is the default value, if any.
15533 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15534 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15535 set to true iff this parameter is a parameter pack. */
15537 static tree
15538 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15539 bool *is_parameter_pack)
15541 cp_token *token;
15542 cp_parameter_declarator *parameter_declarator;
15543 tree parm;
15545 /* Assume it is a type parameter or a template parameter. */
15546 *is_non_type = false;
15547 /* Assume it not a parameter pack. */
15548 *is_parameter_pack = false;
15549 /* Peek at the next token. */
15550 token = cp_lexer_peek_token (parser->lexer);
15551 /* If it is `template', we have a type-parameter. */
15552 if (token->keyword == RID_TEMPLATE)
15553 return cp_parser_type_parameter (parser, is_parameter_pack);
15554 /* If it is `class' or `typename' we do not know yet whether it is a
15555 type parameter or a non-type parameter. Consider:
15557 template <typename T, typename T::X X> ...
15561 template <class C, class D*> ...
15563 Here, the first parameter is a type parameter, and the second is
15564 a non-type parameter. We can tell by looking at the token after
15565 the identifier -- if it is a `,', `=', or `>' then we have a type
15566 parameter. */
15567 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15569 /* Peek at the token after `class' or `typename'. */
15570 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15571 /* If it's an ellipsis, we have a template type parameter
15572 pack. */
15573 if (token->type == CPP_ELLIPSIS)
15574 return cp_parser_type_parameter (parser, is_parameter_pack);
15575 /* If it's an identifier, skip it. */
15576 if (token->type == CPP_NAME)
15577 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15578 /* Now, see if the token looks like the end of a template
15579 parameter. */
15580 if (token->type == CPP_COMMA
15581 || token->type == CPP_EQ
15582 || token->type == CPP_GREATER)
15583 return cp_parser_type_parameter (parser, is_parameter_pack);
15586 /* Otherwise, it is a non-type parameter or a constrained parameter.
15588 [temp.param]
15590 When parsing a default template-argument for a non-type
15591 template-parameter, the first non-nested `>' is taken as the end
15592 of the template parameter-list rather than a greater-than
15593 operator. */
15594 parameter_declarator
15595 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15596 /*parenthesized_p=*/NULL);
15598 if (!parameter_declarator)
15599 return error_mark_node;
15601 /* If the parameter declaration is marked as a parameter pack, set
15602 *IS_PARAMETER_PACK to notify the caller. */
15603 if (parameter_declarator->template_parameter_pack_p)
15604 *is_parameter_pack = true;
15606 if (parameter_declarator->default_argument)
15608 /* Can happen in some cases of erroneous input (c++/34892). */
15609 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15610 /* Consume the `...' for better error recovery. */
15611 cp_lexer_consume_token (parser->lexer);
15614 // The parameter may have been constrained.
15615 if (is_constrained_parameter (parameter_declarator))
15616 return finish_constrained_parameter (parser,
15617 parameter_declarator,
15618 is_non_type,
15619 is_parameter_pack);
15621 // Now we're sure that the parameter is a non-type parameter.
15622 *is_non_type = true;
15624 parm = grokdeclarator (parameter_declarator->declarator,
15625 &parameter_declarator->decl_specifiers,
15626 TPARM, /*initialized=*/0,
15627 /*attrlist=*/NULL);
15628 if (parm == error_mark_node)
15629 return error_mark_node;
15631 return build_tree_list (parameter_declarator->default_argument, parm);
15634 /* Parse a type-parameter.
15636 type-parameter:
15637 class identifier [opt]
15638 class identifier [opt] = type-id
15639 typename identifier [opt]
15640 typename identifier [opt] = type-id
15641 template < template-parameter-list > class identifier [opt]
15642 template < template-parameter-list > class identifier [opt]
15643 = id-expression
15645 GNU Extension (variadic templates):
15647 type-parameter:
15648 class ... identifier [opt]
15649 typename ... identifier [opt]
15651 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15652 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15653 the declaration of the parameter.
15655 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15657 static tree
15658 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15660 cp_token *token;
15661 tree parameter;
15663 /* Look for a keyword to tell us what kind of parameter this is. */
15664 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15665 if (!token)
15666 return error_mark_node;
15668 switch (token->keyword)
15670 case RID_CLASS:
15671 case RID_TYPENAME:
15673 tree identifier;
15674 tree default_argument;
15676 /* If the next token is an ellipsis, we have a template
15677 argument pack. */
15678 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15680 /* Consume the `...' token. */
15681 cp_lexer_consume_token (parser->lexer);
15682 maybe_warn_variadic_templates ();
15684 *is_parameter_pack = true;
15687 /* If the next token is an identifier, then it names the
15688 parameter. */
15689 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15690 identifier = cp_parser_identifier (parser);
15691 else
15692 identifier = NULL_TREE;
15694 /* Create the parameter. */
15695 parameter = finish_template_type_parm (class_type_node, identifier);
15697 /* If the next token is an `=', we have a default argument. */
15698 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15700 default_argument
15701 = cp_parser_default_type_template_argument (parser);
15703 /* Template parameter packs cannot have default
15704 arguments. */
15705 if (*is_parameter_pack)
15707 if (identifier)
15708 error_at (token->location,
15709 "template parameter pack %qD cannot have a "
15710 "default argument", identifier);
15711 else
15712 error_at (token->location,
15713 "template parameter packs cannot have "
15714 "default arguments");
15715 default_argument = NULL_TREE;
15717 else if (check_for_bare_parameter_packs (default_argument))
15718 default_argument = error_mark_node;
15720 else
15721 default_argument = NULL_TREE;
15723 /* Create the combined representation of the parameter and the
15724 default argument. */
15725 parameter = build_tree_list (default_argument, parameter);
15727 break;
15729 case RID_TEMPLATE:
15731 tree identifier;
15732 tree default_argument;
15734 /* Look for the `<'. */
15735 cp_parser_require (parser, CPP_LESS, RT_LESS);
15736 /* Parse the template-parameter-list. */
15737 cp_parser_template_parameter_list (parser);
15738 /* Look for the `>'. */
15739 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15741 // If template requirements are present, parse them.
15742 if (flag_concepts)
15744 tree reqs = get_shorthand_constraints (current_template_parms);
15745 if (tree r = cp_parser_requires_clause_opt (parser))
15746 reqs = conjoin_constraints (reqs, normalize_expression (r));
15747 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15750 /* Look for the `class' or 'typename' keywords. */
15751 cp_parser_type_parameter_key (parser);
15752 /* If the next token is an ellipsis, we have a template
15753 argument pack. */
15754 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15756 /* Consume the `...' token. */
15757 cp_lexer_consume_token (parser->lexer);
15758 maybe_warn_variadic_templates ();
15760 *is_parameter_pack = true;
15762 /* If the next token is an `=', then there is a
15763 default-argument. If the next token is a `>', we are at
15764 the end of the parameter-list. If the next token is a `,',
15765 then we are at the end of this parameter. */
15766 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15767 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15768 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15770 identifier = cp_parser_identifier (parser);
15771 /* Treat invalid names as if the parameter were nameless. */
15772 if (identifier == error_mark_node)
15773 identifier = NULL_TREE;
15775 else
15776 identifier = NULL_TREE;
15778 /* Create the template parameter. */
15779 parameter = finish_template_template_parm (class_type_node,
15780 identifier);
15782 /* If the next token is an `=', then there is a
15783 default-argument. */
15784 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15786 default_argument
15787 = cp_parser_default_template_template_argument (parser);
15789 /* Template parameter packs cannot have default
15790 arguments. */
15791 if (*is_parameter_pack)
15793 if (identifier)
15794 error_at (token->location,
15795 "template parameter pack %qD cannot "
15796 "have a default argument",
15797 identifier);
15798 else
15799 error_at (token->location, "template parameter packs cannot "
15800 "have default arguments");
15801 default_argument = NULL_TREE;
15804 else
15805 default_argument = NULL_TREE;
15807 /* Create the combined representation of the parameter and the
15808 default argument. */
15809 parameter = build_tree_list (default_argument, parameter);
15811 break;
15813 default:
15814 gcc_unreachable ();
15815 break;
15818 return parameter;
15821 /* Parse a template-id.
15823 template-id:
15824 template-name < template-argument-list [opt] >
15826 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15827 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15828 returned. Otherwise, if the template-name names a function, or set
15829 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15830 names a class, returns a TYPE_DECL for the specialization.
15832 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15833 uninstantiated templates. */
15835 static tree
15836 cp_parser_template_id (cp_parser *parser,
15837 bool template_keyword_p,
15838 bool check_dependency_p,
15839 enum tag_types tag_type,
15840 bool is_declaration)
15842 tree templ;
15843 tree arguments;
15844 tree template_id;
15845 cp_token_position start_of_id = 0;
15846 cp_token *next_token = NULL, *next_token_2 = NULL;
15847 bool is_identifier;
15849 /* If the next token corresponds to a template-id, there is no need
15850 to reparse it. */
15851 cp_token *token = cp_lexer_peek_token (parser->lexer);
15852 if (token->type == CPP_TEMPLATE_ID)
15854 cp_lexer_consume_token (parser->lexer);
15855 return saved_checks_value (token->u.tree_check_value);
15858 /* Avoid performing name lookup if there is no possibility of
15859 finding a template-id. */
15860 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15861 || (token->type == CPP_NAME
15862 && !cp_parser_nth_token_starts_template_argument_list_p
15863 (parser, 2)))
15865 cp_parser_error (parser, "expected template-id");
15866 return error_mark_node;
15869 /* Remember where the template-id starts. */
15870 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15871 start_of_id = cp_lexer_token_position (parser->lexer, false);
15873 push_deferring_access_checks (dk_deferred);
15875 /* Parse the template-name. */
15876 is_identifier = false;
15877 templ = cp_parser_template_name (parser, template_keyword_p,
15878 check_dependency_p,
15879 is_declaration,
15880 tag_type,
15881 &is_identifier);
15882 if (templ == error_mark_node || is_identifier)
15884 pop_deferring_access_checks ();
15885 return templ;
15888 /* Since we're going to preserve any side-effects from this parse, set up a
15889 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15890 in the template arguments. */
15891 tentative_firewall firewall (parser);
15893 /* If we find the sequence `[:' after a template-name, it's probably
15894 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15895 parse correctly the argument list. */
15896 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15897 == CPP_OPEN_SQUARE)
15898 && next_token->flags & DIGRAPH
15899 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15900 == CPP_COLON)
15901 && !(next_token_2->flags & PREV_WHITE))
15903 cp_parser_parse_tentatively (parser);
15904 /* Change `:' into `::'. */
15905 next_token_2->type = CPP_SCOPE;
15906 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15907 CPP_LESS. */
15908 cp_lexer_consume_token (parser->lexer);
15910 /* Parse the arguments. */
15911 arguments = cp_parser_enclosed_template_argument_list (parser);
15912 if (!cp_parser_parse_definitely (parser))
15914 /* If we couldn't parse an argument list, then we revert our changes
15915 and return simply an error. Maybe this is not a template-id
15916 after all. */
15917 next_token_2->type = CPP_COLON;
15918 cp_parser_error (parser, "expected %<<%>");
15919 pop_deferring_access_checks ();
15920 return error_mark_node;
15922 /* Otherwise, emit an error about the invalid digraph, but continue
15923 parsing because we got our argument list. */
15924 if (permerror (next_token->location,
15925 "%<<::%> cannot begin a template-argument list"))
15927 static bool hint = false;
15928 inform (next_token->location,
15929 "%<<:%> is an alternate spelling for %<[%>."
15930 " Insert whitespace between %<<%> and %<::%>");
15931 if (!hint && !flag_permissive)
15933 inform (next_token->location, "(if you use %<-fpermissive%> "
15934 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15935 "accept your code)");
15936 hint = true;
15940 else
15942 /* Look for the `<' that starts the template-argument-list. */
15943 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15945 pop_deferring_access_checks ();
15946 return error_mark_node;
15948 /* Parse the arguments. */
15949 arguments = cp_parser_enclosed_template_argument_list (parser);
15952 /* Set the location to be of the form:
15953 template-name < template-argument-list [opt] >
15954 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15955 with caret == start at the start of the template-name,
15956 ranging until the closing '>'. */
15957 location_t finish_loc
15958 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15959 location_t combined_loc
15960 = make_location (token->location, token->location, finish_loc);
15962 /* Check for concepts autos where they don't belong. We could
15963 identify types in some cases of idnetifier TEMPL, looking ahead
15964 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15965 types. We reject them in functions, but if what we have is an
15966 identifier, even with none_type we can't conclude it's NOT a
15967 type, we have to wait for template substitution. */
15968 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15969 template_id = error_mark_node;
15970 /* Build a representation of the specialization. */
15971 else if (identifier_p (templ))
15972 template_id = build_min_nt_loc (combined_loc,
15973 TEMPLATE_ID_EXPR,
15974 templ, arguments);
15975 else if (DECL_TYPE_TEMPLATE_P (templ)
15976 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15978 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15979 template (rather than some instantiation thereof) only if
15980 is not nested within some other construct. For example, in
15981 "template <typename T> void f(T) { A<T>::", A<T> is just an
15982 instantiation of A. */
15983 bool entering_scope
15984 = (template_parm_scope_p ()
15985 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
15986 template_id
15987 = finish_template_type (templ, arguments, entering_scope);
15989 /* A template-like identifier may be a partial concept id. */
15990 else if (flag_concepts
15991 && (template_id = (cp_parser_maybe_partial_concept_id
15992 (parser, templ, arguments))))
15993 return template_id;
15994 else if (variable_template_p (templ))
15996 template_id = lookup_template_variable (templ, arguments);
15997 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15998 SET_EXPR_LOCATION (template_id, combined_loc);
16000 else
16002 /* If it's not a class-template or a template-template, it should be
16003 a function-template. */
16004 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16005 || TREE_CODE (templ) == OVERLOAD
16006 || BASELINK_P (templ)));
16008 template_id = lookup_template_function (templ, arguments);
16009 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16010 SET_EXPR_LOCATION (template_id, combined_loc);
16013 /* If parsing tentatively, replace the sequence of tokens that makes
16014 up the template-id with a CPP_TEMPLATE_ID token. That way,
16015 should we re-parse the token stream, we will not have to repeat
16016 the effort required to do the parse, nor will we issue duplicate
16017 error messages about problems during instantiation of the
16018 template. */
16019 if (start_of_id
16020 /* Don't do this if we had a parse error in a declarator; re-parsing
16021 might succeed if a name changes meaning (60361). */
16022 && !(cp_parser_error_occurred (parser)
16023 && cp_parser_parsing_tentatively (parser)
16024 && parser->in_declarator_p))
16026 /* Reset the contents of the START_OF_ID token. */
16027 token->type = CPP_TEMPLATE_ID;
16028 token->location = combined_loc;
16030 /* Retrieve any deferred checks. Do not pop this access checks yet
16031 so the memory will not be reclaimed during token replacing below. */
16032 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16033 token->u.tree_check_value->value = template_id;
16034 token->u.tree_check_value->checks = get_deferred_access_checks ();
16035 token->keyword = RID_MAX;
16037 /* Purge all subsequent tokens. */
16038 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16040 /* ??? Can we actually assume that, if template_id ==
16041 error_mark_node, we will have issued a diagnostic to the
16042 user, as opposed to simply marking the tentative parse as
16043 failed? */
16044 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16045 error_at (token->location, "parse error in template argument list");
16048 pop_to_parent_deferring_access_checks ();
16049 return template_id;
16052 /* Parse a template-name.
16054 template-name:
16055 identifier
16057 The standard should actually say:
16059 template-name:
16060 identifier
16061 operator-function-id
16063 A defect report has been filed about this issue.
16065 A conversion-function-id cannot be a template name because they cannot
16066 be part of a template-id. In fact, looking at this code:
16068 a.operator K<int>()
16070 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16071 It is impossible to call a templated conversion-function-id with an
16072 explicit argument list, since the only allowed template parameter is
16073 the type to which it is converting.
16075 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16076 `template' keyword, in a construction like:
16078 T::template f<3>()
16080 In that case `f' is taken to be a template-name, even though there
16081 is no way of knowing for sure.
16083 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16084 name refers to a set of overloaded functions, at least one of which
16085 is a template, or an IDENTIFIER_NODE with the name of the template,
16086 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16087 names are looked up inside uninstantiated templates. */
16089 static tree
16090 cp_parser_template_name (cp_parser* parser,
16091 bool template_keyword_p,
16092 bool check_dependency_p,
16093 bool is_declaration,
16094 enum tag_types tag_type,
16095 bool *is_identifier)
16097 tree identifier;
16098 tree decl;
16099 cp_token *token = cp_lexer_peek_token (parser->lexer);
16101 /* If the next token is `operator', then we have either an
16102 operator-function-id or a conversion-function-id. */
16103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16105 /* We don't know whether we're looking at an
16106 operator-function-id or a conversion-function-id. */
16107 cp_parser_parse_tentatively (parser);
16108 /* Try an operator-function-id. */
16109 identifier = cp_parser_operator_function_id (parser);
16110 /* If that didn't work, try a conversion-function-id. */
16111 if (!cp_parser_parse_definitely (parser))
16113 cp_parser_error (parser, "expected template-name");
16114 return error_mark_node;
16117 /* Look for the identifier. */
16118 else
16119 identifier = cp_parser_identifier (parser);
16121 /* If we didn't find an identifier, we don't have a template-id. */
16122 if (identifier == error_mark_node)
16123 return error_mark_node;
16125 /* If the name immediately followed the `template' keyword, then it
16126 is a template-name. However, if the next token is not `<', then
16127 we do not treat it as a template-name, since it is not being used
16128 as part of a template-id. This enables us to handle constructs
16129 like:
16131 template <typename T> struct S { S(); };
16132 template <typename T> S<T>::S();
16134 correctly. We would treat `S' as a template -- if it were `S<T>'
16135 -- but we do not if there is no `<'. */
16137 if (processing_template_decl
16138 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16140 /* In a declaration, in a dependent context, we pretend that the
16141 "template" keyword was present in order to improve error
16142 recovery. For example, given:
16144 template <typename T> void f(T::X<int>);
16146 we want to treat "X<int>" as a template-id. */
16147 if (is_declaration
16148 && !template_keyword_p
16149 && parser->scope && TYPE_P (parser->scope)
16150 && check_dependency_p
16151 && dependent_scope_p (parser->scope)
16152 /* Do not do this for dtors (or ctors), since they never
16153 need the template keyword before their name. */
16154 && !constructor_name_p (identifier, parser->scope))
16156 cp_token_position start = 0;
16158 /* Explain what went wrong. */
16159 error_at (token->location, "non-template %qD used as template",
16160 identifier);
16161 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16162 parser->scope, identifier);
16163 /* If parsing tentatively, find the location of the "<" token. */
16164 if (cp_parser_simulate_error (parser))
16165 start = cp_lexer_token_position (parser->lexer, true);
16166 /* Parse the template arguments so that we can issue error
16167 messages about them. */
16168 cp_lexer_consume_token (parser->lexer);
16169 cp_parser_enclosed_template_argument_list (parser);
16170 /* Skip tokens until we find a good place from which to
16171 continue parsing. */
16172 cp_parser_skip_to_closing_parenthesis (parser,
16173 /*recovering=*/true,
16174 /*or_comma=*/true,
16175 /*consume_paren=*/false);
16176 /* If parsing tentatively, permanently remove the
16177 template argument list. That will prevent duplicate
16178 error messages from being issued about the missing
16179 "template" keyword. */
16180 if (start)
16181 cp_lexer_purge_tokens_after (parser->lexer, start);
16182 if (is_identifier)
16183 *is_identifier = true;
16184 parser->context->object_type = NULL_TREE;
16185 return identifier;
16188 /* If the "template" keyword is present, then there is generally
16189 no point in doing name-lookup, so we just return IDENTIFIER.
16190 But, if the qualifying scope is non-dependent then we can
16191 (and must) do name-lookup normally. */
16192 if (template_keyword_p)
16194 tree scope = (parser->scope ? parser->scope
16195 : parser->context->object_type);
16196 if (scope && TYPE_P (scope)
16197 && (!CLASS_TYPE_P (scope)
16198 || (check_dependency_p && dependent_type_p (scope))))
16200 /* We're optimizing away the call to cp_parser_lookup_name, but
16201 we still need to do this. */
16202 parser->context->object_type = NULL_TREE;
16203 return identifier;
16208 /* Look up the name. */
16209 decl = cp_parser_lookup_name (parser, identifier,
16210 tag_type,
16211 /*is_template=*/true,
16212 /*is_namespace=*/false,
16213 check_dependency_p,
16214 /*ambiguous_decls=*/NULL,
16215 token->location);
16217 decl = strip_using_decl (decl);
16219 /* If DECL is a template, then the name was a template-name. */
16220 if (TREE_CODE (decl) == TEMPLATE_DECL)
16222 if (TREE_DEPRECATED (decl)
16223 && deprecated_state != DEPRECATED_SUPPRESS)
16224 warn_deprecated_use (decl, NULL_TREE);
16226 else
16228 /* The standard does not explicitly indicate whether a name that
16229 names a set of overloaded declarations, some of which are
16230 templates, is a template-name. However, such a name should
16231 be a template-name; otherwise, there is no way to form a
16232 template-id for the overloaded templates. */
16233 bool found = false;
16235 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16236 !found && iter; ++iter)
16237 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16238 found = true;
16240 if (!found)
16242 /* The name does not name a template. */
16243 cp_parser_error (parser, "expected template-name");
16244 return error_mark_node;
16248 /* If DECL is dependent, and refers to a function, then just return
16249 its name; we will look it up again during template instantiation. */
16250 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16252 tree scope = ovl_scope (decl);
16253 if (TYPE_P (scope) && dependent_type_p (scope))
16254 return identifier;
16257 return decl;
16260 /* Parse a template-argument-list.
16262 template-argument-list:
16263 template-argument ... [opt]
16264 template-argument-list , template-argument ... [opt]
16266 Returns a TREE_VEC containing the arguments. */
16268 static tree
16269 cp_parser_template_argument_list (cp_parser* parser)
16271 tree fixed_args[10];
16272 unsigned n_args = 0;
16273 unsigned alloced = 10;
16274 tree *arg_ary = fixed_args;
16275 tree vec;
16276 bool saved_in_template_argument_list_p;
16277 bool saved_ice_p;
16278 bool saved_non_ice_p;
16280 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16281 parser->in_template_argument_list_p = true;
16282 /* Even if the template-id appears in an integral
16283 constant-expression, the contents of the argument list do
16284 not. */
16285 saved_ice_p = parser->integral_constant_expression_p;
16286 parser->integral_constant_expression_p = false;
16287 saved_non_ice_p = parser->non_integral_constant_expression_p;
16288 parser->non_integral_constant_expression_p = false;
16290 /* Parse the arguments. */
16293 tree argument;
16295 if (n_args)
16296 /* Consume the comma. */
16297 cp_lexer_consume_token (parser->lexer);
16299 /* Parse the template-argument. */
16300 argument = cp_parser_template_argument (parser);
16302 /* If the next token is an ellipsis, we're expanding a template
16303 argument pack. */
16304 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16306 if (argument == error_mark_node)
16308 cp_token *token = cp_lexer_peek_token (parser->lexer);
16309 error_at (token->location,
16310 "expected parameter pack before %<...%>");
16312 /* Consume the `...' token. */
16313 cp_lexer_consume_token (parser->lexer);
16315 /* Make the argument into a TYPE_PACK_EXPANSION or
16316 EXPR_PACK_EXPANSION. */
16317 argument = make_pack_expansion (argument);
16320 if (n_args == alloced)
16322 alloced *= 2;
16324 if (arg_ary == fixed_args)
16326 arg_ary = XNEWVEC (tree, alloced);
16327 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16329 else
16330 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16332 arg_ary[n_args++] = argument;
16334 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16336 vec = make_tree_vec (n_args);
16338 while (n_args--)
16339 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16341 if (arg_ary != fixed_args)
16342 free (arg_ary);
16343 parser->non_integral_constant_expression_p = saved_non_ice_p;
16344 parser->integral_constant_expression_p = saved_ice_p;
16345 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16346 if (CHECKING_P)
16347 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16348 return vec;
16351 /* Parse a template-argument.
16353 template-argument:
16354 assignment-expression
16355 type-id
16356 id-expression
16358 The representation is that of an assignment-expression, type-id, or
16359 id-expression -- except that the qualified id-expression is
16360 evaluated, so that the value returned is either a DECL or an
16361 OVERLOAD.
16363 Although the standard says "assignment-expression", it forbids
16364 throw-expressions or assignments in the template argument.
16365 Therefore, we use "conditional-expression" instead. */
16367 static tree
16368 cp_parser_template_argument (cp_parser* parser)
16370 tree argument;
16371 bool template_p;
16372 bool address_p;
16373 bool maybe_type_id = false;
16374 cp_token *token = NULL, *argument_start_token = NULL;
16375 location_t loc = 0;
16376 cp_id_kind idk;
16378 /* There's really no way to know what we're looking at, so we just
16379 try each alternative in order.
16381 [temp.arg]
16383 In a template-argument, an ambiguity between a type-id and an
16384 expression is resolved to a type-id, regardless of the form of
16385 the corresponding template-parameter.
16387 Therefore, we try a type-id first. */
16388 cp_parser_parse_tentatively (parser);
16389 argument = cp_parser_template_type_arg (parser);
16390 /* If there was no error parsing the type-id but the next token is a
16391 '>>', our behavior depends on which dialect of C++ we're
16392 parsing. In C++98, we probably found a typo for '> >'. But there
16393 are type-id which are also valid expressions. For instance:
16395 struct X { int operator >> (int); };
16396 template <int V> struct Foo {};
16397 Foo<X () >> 5> r;
16399 Here 'X()' is a valid type-id of a function type, but the user just
16400 wanted to write the expression "X() >> 5". Thus, we remember that we
16401 found a valid type-id, but we still try to parse the argument as an
16402 expression to see what happens.
16404 In C++0x, the '>>' will be considered two separate '>'
16405 tokens. */
16406 if (!cp_parser_error_occurred (parser)
16407 && cxx_dialect == cxx98
16408 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16410 maybe_type_id = true;
16411 cp_parser_abort_tentative_parse (parser);
16413 else
16415 /* If the next token isn't a `,' or a `>', then this argument wasn't
16416 really finished. This means that the argument is not a valid
16417 type-id. */
16418 if (!cp_parser_next_token_ends_template_argument_p (parser))
16419 cp_parser_error (parser, "expected template-argument");
16420 /* If that worked, we're done. */
16421 if (cp_parser_parse_definitely (parser))
16422 return argument;
16424 /* We're still not sure what the argument will be. */
16425 cp_parser_parse_tentatively (parser);
16426 /* Try a template. */
16427 argument_start_token = cp_lexer_peek_token (parser->lexer);
16428 argument = cp_parser_id_expression (parser,
16429 /*template_keyword_p=*/false,
16430 /*check_dependency_p=*/true,
16431 &template_p,
16432 /*declarator_p=*/false,
16433 /*optional_p=*/false);
16434 /* If the next token isn't a `,' or a `>', then this argument wasn't
16435 really finished. */
16436 if (!cp_parser_next_token_ends_template_argument_p (parser))
16437 cp_parser_error (parser, "expected template-argument");
16438 if (!cp_parser_error_occurred (parser))
16440 /* Figure out what is being referred to. If the id-expression
16441 was for a class template specialization, then we will have a
16442 TYPE_DECL at this point. There is no need to do name lookup
16443 at this point in that case. */
16444 if (TREE_CODE (argument) != TYPE_DECL)
16445 argument = cp_parser_lookup_name (parser, argument,
16446 none_type,
16447 /*is_template=*/template_p,
16448 /*is_namespace=*/false,
16449 /*check_dependency=*/true,
16450 /*ambiguous_decls=*/NULL,
16451 argument_start_token->location);
16452 /* Handle a constrained-type-specifier for a non-type template
16453 parameter. */
16454 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16455 argument = decl;
16456 else if (TREE_CODE (argument) != TEMPLATE_DECL
16457 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16458 cp_parser_error (parser, "expected template-name");
16460 if (cp_parser_parse_definitely (parser))
16462 if (TREE_DEPRECATED (argument))
16463 warn_deprecated_use (argument, NULL_TREE);
16464 return argument;
16466 /* It must be a non-type argument. In C++17 any constant-expression is
16467 allowed. */
16468 if (cxx_dialect > cxx14)
16469 goto general_expr;
16471 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16473 -- an integral constant-expression of integral or enumeration
16474 type; or
16476 -- the name of a non-type template-parameter; or
16478 -- the name of an object or function with external linkage...
16480 -- the address of an object or function with external linkage...
16482 -- a pointer to member... */
16483 /* Look for a non-type template parameter. */
16484 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16486 cp_parser_parse_tentatively (parser);
16487 argument = cp_parser_primary_expression (parser,
16488 /*address_p=*/false,
16489 /*cast_p=*/false,
16490 /*template_arg_p=*/true,
16491 &idk);
16492 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16493 || !cp_parser_next_token_ends_template_argument_p (parser))
16494 cp_parser_simulate_error (parser);
16495 if (cp_parser_parse_definitely (parser))
16496 return argument;
16499 /* If the next token is "&", the argument must be the address of an
16500 object or function with external linkage. */
16501 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16502 if (address_p)
16504 loc = cp_lexer_peek_token (parser->lexer)->location;
16505 cp_lexer_consume_token (parser->lexer);
16507 /* See if we might have an id-expression. */
16508 token = cp_lexer_peek_token (parser->lexer);
16509 if (token->type == CPP_NAME
16510 || token->keyword == RID_OPERATOR
16511 || token->type == CPP_SCOPE
16512 || token->type == CPP_TEMPLATE_ID
16513 || token->type == CPP_NESTED_NAME_SPECIFIER)
16515 cp_parser_parse_tentatively (parser);
16516 argument = cp_parser_primary_expression (parser,
16517 address_p,
16518 /*cast_p=*/false,
16519 /*template_arg_p=*/true,
16520 &idk);
16521 if (cp_parser_error_occurred (parser)
16522 || !cp_parser_next_token_ends_template_argument_p (parser))
16523 cp_parser_abort_tentative_parse (parser);
16524 else
16526 tree probe;
16528 if (INDIRECT_REF_P (argument))
16530 /* Strip the dereference temporarily. */
16531 gcc_assert (REFERENCE_REF_P (argument));
16532 argument = TREE_OPERAND (argument, 0);
16535 /* If we're in a template, we represent a qualified-id referring
16536 to a static data member as a SCOPE_REF even if the scope isn't
16537 dependent so that we can check access control later. */
16538 probe = argument;
16539 if (TREE_CODE (probe) == SCOPE_REF)
16540 probe = TREE_OPERAND (probe, 1);
16541 if (VAR_P (probe))
16543 /* A variable without external linkage might still be a
16544 valid constant-expression, so no error is issued here
16545 if the external-linkage check fails. */
16546 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16547 cp_parser_simulate_error (parser);
16549 else if (is_overloaded_fn (argument))
16550 /* All overloaded functions are allowed; if the external
16551 linkage test does not pass, an error will be issued
16552 later. */
16554 else if (address_p
16555 && (TREE_CODE (argument) == OFFSET_REF
16556 || TREE_CODE (argument) == SCOPE_REF))
16557 /* A pointer-to-member. */
16559 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16561 else
16562 cp_parser_simulate_error (parser);
16564 if (cp_parser_parse_definitely (parser))
16566 if (address_p)
16567 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16568 tf_warning_or_error);
16569 else
16570 argument = convert_from_reference (argument);
16571 return argument;
16575 /* If the argument started with "&", there are no other valid
16576 alternatives at this point. */
16577 if (address_p)
16579 cp_parser_error (parser, "invalid non-type template argument");
16580 return error_mark_node;
16583 general_expr:
16584 /* If the argument wasn't successfully parsed as a type-id followed
16585 by '>>', the argument can only be a constant expression now.
16586 Otherwise, we try parsing the constant-expression tentatively,
16587 because the argument could really be a type-id. */
16588 if (maybe_type_id)
16589 cp_parser_parse_tentatively (parser);
16591 if (cxx_dialect <= cxx14)
16592 argument = cp_parser_constant_expression (parser);
16593 else
16595 /* With C++17 generalized non-type template arguments we need to handle
16596 lvalue constant expressions, too. */
16597 argument = cp_parser_assignment_expression (parser);
16598 require_potential_constant_expression (argument);
16601 if (!maybe_type_id)
16602 return argument;
16603 if (!cp_parser_next_token_ends_template_argument_p (parser))
16604 cp_parser_error (parser, "expected template-argument");
16605 if (cp_parser_parse_definitely (parser))
16606 return argument;
16607 /* We did our best to parse the argument as a non type-id, but that
16608 was the only alternative that matched (albeit with a '>' after
16609 it). We can assume it's just a typo from the user, and a
16610 diagnostic will then be issued. */
16611 return cp_parser_template_type_arg (parser);
16614 /* Parse an explicit-instantiation.
16616 explicit-instantiation:
16617 template declaration
16619 Although the standard says `declaration', what it really means is:
16621 explicit-instantiation:
16622 template decl-specifier-seq [opt] declarator [opt] ;
16624 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16625 supposed to be allowed. A defect report has been filed about this
16626 issue.
16628 GNU Extension:
16630 explicit-instantiation:
16631 storage-class-specifier template
16632 decl-specifier-seq [opt] declarator [opt] ;
16633 function-specifier template
16634 decl-specifier-seq [opt] declarator [opt] ; */
16636 static void
16637 cp_parser_explicit_instantiation (cp_parser* parser)
16639 int declares_class_or_enum;
16640 cp_decl_specifier_seq decl_specifiers;
16641 tree extension_specifier = NULL_TREE;
16643 timevar_push (TV_TEMPLATE_INST);
16645 /* Look for an (optional) storage-class-specifier or
16646 function-specifier. */
16647 if (cp_parser_allow_gnu_extensions_p (parser))
16649 extension_specifier
16650 = cp_parser_storage_class_specifier_opt (parser);
16651 if (!extension_specifier)
16652 extension_specifier
16653 = cp_parser_function_specifier_opt (parser,
16654 /*decl_specs=*/NULL);
16657 /* Look for the `template' keyword. */
16658 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16659 /* Let the front end know that we are processing an explicit
16660 instantiation. */
16661 begin_explicit_instantiation ();
16662 /* [temp.explicit] says that we are supposed to ignore access
16663 control while processing explicit instantiation directives. */
16664 push_deferring_access_checks (dk_no_check);
16665 /* Parse a decl-specifier-seq. */
16666 cp_parser_decl_specifier_seq (parser,
16667 CP_PARSER_FLAGS_OPTIONAL,
16668 &decl_specifiers,
16669 &declares_class_or_enum);
16670 /* If there was exactly one decl-specifier, and it declared a class,
16671 and there's no declarator, then we have an explicit type
16672 instantiation. */
16673 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16675 tree type;
16677 type = check_tag_decl (&decl_specifiers,
16678 /*explicit_type_instantiation_p=*/true);
16679 /* Turn access control back on for names used during
16680 template instantiation. */
16681 pop_deferring_access_checks ();
16682 if (type)
16683 do_type_instantiation (type, extension_specifier,
16684 /*complain=*/tf_error);
16686 else
16688 cp_declarator *declarator;
16689 tree decl;
16691 /* Parse the declarator. */
16692 declarator
16693 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16694 /*ctor_dtor_or_conv_p=*/NULL,
16695 /*parenthesized_p=*/NULL,
16696 /*member_p=*/false,
16697 /*friend_p=*/false);
16698 if (declares_class_or_enum & 2)
16699 cp_parser_check_for_definition_in_return_type (declarator,
16700 decl_specifiers.type,
16701 decl_specifiers.locations[ds_type_spec]);
16702 if (declarator != cp_error_declarator)
16704 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16705 permerror (decl_specifiers.locations[ds_inline],
16706 "explicit instantiation shall not use"
16707 " %<inline%> specifier");
16708 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16709 permerror (decl_specifiers.locations[ds_constexpr],
16710 "explicit instantiation shall not use"
16711 " %<constexpr%> specifier");
16713 decl = grokdeclarator (declarator, &decl_specifiers,
16714 NORMAL, 0, &decl_specifiers.attributes);
16715 /* Turn access control back on for names used during
16716 template instantiation. */
16717 pop_deferring_access_checks ();
16718 /* Do the explicit instantiation. */
16719 do_decl_instantiation (decl, extension_specifier);
16721 else
16723 pop_deferring_access_checks ();
16724 /* Skip the body of the explicit instantiation. */
16725 cp_parser_skip_to_end_of_statement (parser);
16728 /* We're done with the instantiation. */
16729 end_explicit_instantiation ();
16731 cp_parser_consume_semicolon_at_end_of_statement (parser);
16733 timevar_pop (TV_TEMPLATE_INST);
16736 /* Parse an explicit-specialization.
16738 explicit-specialization:
16739 template < > declaration
16741 Although the standard says `declaration', what it really means is:
16743 explicit-specialization:
16744 template <> decl-specifier [opt] init-declarator [opt] ;
16745 template <> function-definition
16746 template <> explicit-specialization
16747 template <> template-declaration */
16749 static void
16750 cp_parser_explicit_specialization (cp_parser* parser)
16752 bool need_lang_pop;
16753 cp_token *token = cp_lexer_peek_token (parser->lexer);
16755 /* Look for the `template' keyword. */
16756 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16757 /* Look for the `<'. */
16758 cp_parser_require (parser, CPP_LESS, RT_LESS);
16759 /* Look for the `>'. */
16760 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16761 /* We have processed another parameter list. */
16762 ++parser->num_template_parameter_lists;
16763 /* [temp]
16765 A template ... explicit specialization ... shall not have C
16766 linkage. */
16767 if (current_lang_name == lang_name_c)
16769 error_at (token->location, "template specialization with C linkage");
16770 maybe_show_extern_c_location ();
16771 /* Give it C++ linkage to avoid confusing other parts of the
16772 front end. */
16773 push_lang_context (lang_name_cplusplus);
16774 need_lang_pop = true;
16776 else
16777 need_lang_pop = false;
16778 /* Let the front end know that we are beginning a specialization. */
16779 if (!begin_specialization ())
16781 end_specialization ();
16782 return;
16785 /* If the next keyword is `template', we need to figure out whether
16786 or not we're looking a template-declaration. */
16787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16789 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16790 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16791 cp_parser_template_declaration_after_export (parser,
16792 /*member_p=*/false);
16793 else
16794 cp_parser_explicit_specialization (parser);
16796 else
16797 /* Parse the dependent declaration. */
16798 cp_parser_single_declaration (parser,
16799 /*checks=*/NULL,
16800 /*member_p=*/false,
16801 /*explicit_specialization_p=*/true,
16802 /*friend_p=*/NULL);
16803 /* We're done with the specialization. */
16804 end_specialization ();
16805 /* For the erroneous case of a template with C linkage, we pushed an
16806 implicit C++ linkage scope; exit that scope now. */
16807 if (need_lang_pop)
16808 pop_lang_context ();
16809 /* We're done with this parameter list. */
16810 --parser->num_template_parameter_lists;
16813 /* Parse a type-specifier.
16815 type-specifier:
16816 simple-type-specifier
16817 class-specifier
16818 enum-specifier
16819 elaborated-type-specifier
16820 cv-qualifier
16822 GNU Extension:
16824 type-specifier:
16825 __complex__
16827 Returns a representation of the type-specifier. For a
16828 class-specifier, enum-specifier, or elaborated-type-specifier, a
16829 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16831 The parser flags FLAGS is used to control type-specifier parsing.
16833 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16834 in a decl-specifier-seq.
16836 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16837 class-specifier, enum-specifier, or elaborated-type-specifier, then
16838 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16839 if a type is declared; 2 if it is defined. Otherwise, it is set to
16840 zero.
16842 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16843 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16844 is set to FALSE. */
16846 static tree
16847 cp_parser_type_specifier (cp_parser* parser,
16848 cp_parser_flags flags,
16849 cp_decl_specifier_seq *decl_specs,
16850 bool is_declaration,
16851 int* declares_class_or_enum,
16852 bool* is_cv_qualifier)
16854 tree type_spec = NULL_TREE;
16855 cp_token *token;
16856 enum rid keyword;
16857 cp_decl_spec ds = ds_last;
16859 /* Assume this type-specifier does not declare a new type. */
16860 if (declares_class_or_enum)
16861 *declares_class_or_enum = 0;
16862 /* And that it does not specify a cv-qualifier. */
16863 if (is_cv_qualifier)
16864 *is_cv_qualifier = false;
16865 /* Peek at the next token. */
16866 token = cp_lexer_peek_token (parser->lexer);
16868 /* If we're looking at a keyword, we can use that to guide the
16869 production we choose. */
16870 keyword = token->keyword;
16871 switch (keyword)
16873 case RID_ENUM:
16874 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16875 goto elaborated_type_specifier;
16877 /* Look for the enum-specifier. */
16878 type_spec = cp_parser_enum_specifier (parser);
16879 /* If that worked, we're done. */
16880 if (type_spec)
16882 if (declares_class_or_enum)
16883 *declares_class_or_enum = 2;
16884 if (decl_specs)
16885 cp_parser_set_decl_spec_type (decl_specs,
16886 type_spec,
16887 token,
16888 /*type_definition_p=*/true);
16889 return type_spec;
16891 else
16892 goto elaborated_type_specifier;
16894 /* Any of these indicate either a class-specifier, or an
16895 elaborated-type-specifier. */
16896 case RID_CLASS:
16897 case RID_STRUCT:
16898 case RID_UNION:
16899 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16900 goto elaborated_type_specifier;
16902 /* Parse tentatively so that we can back up if we don't find a
16903 class-specifier. */
16904 cp_parser_parse_tentatively (parser);
16905 /* Look for the class-specifier. */
16906 type_spec = cp_parser_class_specifier (parser);
16907 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16908 /* If that worked, we're done. */
16909 if (cp_parser_parse_definitely (parser))
16911 if (declares_class_or_enum)
16912 *declares_class_or_enum = 2;
16913 if (decl_specs)
16914 cp_parser_set_decl_spec_type (decl_specs,
16915 type_spec,
16916 token,
16917 /*type_definition_p=*/true);
16918 return type_spec;
16921 /* Fall through. */
16922 elaborated_type_specifier:
16923 /* We're declaring (not defining) a class or enum. */
16924 if (declares_class_or_enum)
16925 *declares_class_or_enum = 1;
16927 /* Fall through. */
16928 case RID_TYPENAME:
16929 /* Look for an elaborated-type-specifier. */
16930 type_spec
16931 = (cp_parser_elaborated_type_specifier
16932 (parser,
16933 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16934 is_declaration));
16935 if (decl_specs)
16936 cp_parser_set_decl_spec_type (decl_specs,
16937 type_spec,
16938 token,
16939 /*type_definition_p=*/false);
16940 return type_spec;
16942 case RID_CONST:
16943 ds = ds_const;
16944 if (is_cv_qualifier)
16945 *is_cv_qualifier = true;
16946 break;
16948 case RID_VOLATILE:
16949 ds = ds_volatile;
16950 if (is_cv_qualifier)
16951 *is_cv_qualifier = true;
16952 break;
16954 case RID_RESTRICT:
16955 ds = ds_restrict;
16956 if (is_cv_qualifier)
16957 *is_cv_qualifier = true;
16958 break;
16960 case RID_COMPLEX:
16961 /* The `__complex__' keyword is a GNU extension. */
16962 ds = ds_complex;
16963 break;
16965 default:
16966 break;
16969 /* Handle simple keywords. */
16970 if (ds != ds_last)
16972 if (decl_specs)
16974 set_and_check_decl_spec_loc (decl_specs, ds, token);
16975 decl_specs->any_specifiers_p = true;
16977 return cp_lexer_consume_token (parser->lexer)->u.value;
16980 /* If we do not already have a type-specifier, assume we are looking
16981 at a simple-type-specifier. */
16982 type_spec = cp_parser_simple_type_specifier (parser,
16983 decl_specs,
16984 flags);
16986 /* If we didn't find a type-specifier, and a type-specifier was not
16987 optional in this context, issue an error message. */
16988 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16990 cp_parser_error (parser, "expected type specifier");
16991 return error_mark_node;
16994 return type_spec;
16997 /* Parse a simple-type-specifier.
16999 simple-type-specifier:
17000 :: [opt] nested-name-specifier [opt] type-name
17001 :: [opt] nested-name-specifier template template-id
17002 char
17003 wchar_t
17004 bool
17005 short
17007 long
17008 signed
17009 unsigned
17010 float
17011 double
17012 void
17014 C++11 Extension:
17016 simple-type-specifier:
17017 auto
17018 decltype ( expression )
17019 char16_t
17020 char32_t
17021 __underlying_type ( type-id )
17023 C++17 extension:
17025 nested-name-specifier(opt) template-name
17027 GNU Extension:
17029 simple-type-specifier:
17030 __int128
17031 __typeof__ unary-expression
17032 __typeof__ ( type-id )
17033 __typeof__ ( type-id ) { initializer-list , [opt] }
17035 Concepts Extension:
17037 simple-type-specifier:
17038 constrained-type-specifier
17040 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17041 appropriately updated. */
17043 static tree
17044 cp_parser_simple_type_specifier (cp_parser* parser,
17045 cp_decl_specifier_seq *decl_specs,
17046 cp_parser_flags flags)
17048 tree type = NULL_TREE;
17049 cp_token *token;
17050 int idx;
17052 /* Peek at the next token. */
17053 token = cp_lexer_peek_token (parser->lexer);
17055 /* If we're looking at a keyword, things are easy. */
17056 switch (token->keyword)
17058 case RID_CHAR:
17059 if (decl_specs)
17060 decl_specs->explicit_char_p = true;
17061 type = char_type_node;
17062 break;
17063 case RID_CHAR16:
17064 type = char16_type_node;
17065 break;
17066 case RID_CHAR32:
17067 type = char32_type_node;
17068 break;
17069 case RID_WCHAR:
17070 type = wchar_type_node;
17071 break;
17072 case RID_BOOL:
17073 type = boolean_type_node;
17074 break;
17075 case RID_SHORT:
17076 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17077 type = short_integer_type_node;
17078 break;
17079 case RID_INT:
17080 if (decl_specs)
17081 decl_specs->explicit_int_p = true;
17082 type = integer_type_node;
17083 break;
17084 case RID_INT_N_0:
17085 case RID_INT_N_1:
17086 case RID_INT_N_2:
17087 case RID_INT_N_3:
17088 idx = token->keyword - RID_INT_N_0;
17089 if (! int_n_enabled_p [idx])
17090 break;
17091 if (decl_specs)
17093 decl_specs->explicit_intN_p = true;
17094 decl_specs->int_n_idx = idx;
17096 type = int_n_trees [idx].signed_type;
17097 break;
17098 case RID_LONG:
17099 if (decl_specs)
17100 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17101 type = long_integer_type_node;
17102 break;
17103 case RID_SIGNED:
17104 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17105 type = integer_type_node;
17106 break;
17107 case RID_UNSIGNED:
17108 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17109 type = unsigned_type_node;
17110 break;
17111 case RID_FLOAT:
17112 type = float_type_node;
17113 break;
17114 case RID_DOUBLE:
17115 type = double_type_node;
17116 break;
17117 case RID_VOID:
17118 type = void_type_node;
17119 break;
17121 case RID_AUTO:
17122 maybe_warn_cpp0x (CPP0X_AUTO);
17123 if (parser->auto_is_implicit_function_template_parm_p)
17125 /* The 'auto' might be the placeholder return type for a function decl
17126 with trailing return type. */
17127 bool have_trailing_return_fn_decl = false;
17129 cp_parser_parse_tentatively (parser);
17130 cp_lexer_consume_token (parser->lexer);
17131 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17132 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17133 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17134 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17136 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17138 cp_lexer_consume_token (parser->lexer);
17139 cp_parser_skip_to_closing_parenthesis (parser,
17140 /*recovering*/false,
17141 /*or_comma*/false,
17142 /*consume_paren*/true);
17143 continue;
17146 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17148 have_trailing_return_fn_decl = true;
17149 break;
17152 cp_lexer_consume_token (parser->lexer);
17154 cp_parser_abort_tentative_parse (parser);
17156 if (have_trailing_return_fn_decl)
17158 type = make_auto ();
17159 break;
17162 if (cxx_dialect >= cxx14)
17164 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17165 type = TREE_TYPE (type);
17167 else
17168 type = error_mark_node;
17170 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17172 if (cxx_dialect < cxx14)
17173 error_at (token->location,
17174 "use of %<auto%> in lambda parameter declaration "
17175 "only available with "
17176 "-std=c++14 or -std=gnu++14");
17178 else if (cxx_dialect < cxx14)
17179 error_at (token->location,
17180 "use of %<auto%> in parameter declaration "
17181 "only available with "
17182 "-std=c++14 or -std=gnu++14");
17183 else if (!flag_concepts)
17184 pedwarn (token->location, 0,
17185 "use of %<auto%> in parameter declaration "
17186 "only available with -fconcepts");
17188 else
17189 type = make_auto ();
17190 break;
17192 case RID_DECLTYPE:
17193 /* Since DR 743, decltype can either be a simple-type-specifier by
17194 itself or begin a nested-name-specifier. Parsing it will replace
17195 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17196 handling below decide what to do. */
17197 cp_parser_decltype (parser);
17198 cp_lexer_set_token_position (parser->lexer, token);
17199 break;
17201 case RID_TYPEOF:
17202 /* Consume the `typeof' token. */
17203 cp_lexer_consume_token (parser->lexer);
17204 /* Parse the operand to `typeof'. */
17205 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17206 /* If it is not already a TYPE, take its type. */
17207 if (!TYPE_P (type))
17208 type = finish_typeof (type);
17210 if (decl_specs)
17211 cp_parser_set_decl_spec_type (decl_specs, type,
17212 token,
17213 /*type_definition_p=*/false);
17215 return type;
17217 case RID_UNDERLYING_TYPE:
17218 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17219 if (decl_specs)
17220 cp_parser_set_decl_spec_type (decl_specs, type,
17221 token,
17222 /*type_definition_p=*/false);
17224 return type;
17226 case RID_BASES:
17227 case RID_DIRECT_BASES:
17228 type = cp_parser_trait_expr (parser, token->keyword);
17229 if (decl_specs)
17230 cp_parser_set_decl_spec_type (decl_specs, type,
17231 token,
17232 /*type_definition_p=*/false);
17233 return type;
17234 default:
17235 break;
17238 /* If token is an already-parsed decltype not followed by ::,
17239 it's a simple-type-specifier. */
17240 if (token->type == CPP_DECLTYPE
17241 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17243 type = saved_checks_value (token->u.tree_check_value);
17244 if (decl_specs)
17246 cp_parser_set_decl_spec_type (decl_specs, type,
17247 token,
17248 /*type_definition_p=*/false);
17249 /* Remember that we are handling a decltype in order to
17250 implement the resolution of DR 1510 when the argument
17251 isn't instantiation dependent. */
17252 decl_specs->decltype_p = true;
17254 cp_lexer_consume_token (parser->lexer);
17255 return type;
17258 /* If the type-specifier was for a built-in type, we're done. */
17259 if (type)
17261 /* Record the type. */
17262 if (decl_specs
17263 && (token->keyword != RID_SIGNED
17264 && token->keyword != RID_UNSIGNED
17265 && token->keyword != RID_SHORT
17266 && token->keyword != RID_LONG))
17267 cp_parser_set_decl_spec_type (decl_specs,
17268 type,
17269 token,
17270 /*type_definition_p=*/false);
17271 if (decl_specs)
17272 decl_specs->any_specifiers_p = true;
17274 /* Consume the token. */
17275 cp_lexer_consume_token (parser->lexer);
17277 if (type == error_mark_node)
17278 return error_mark_node;
17280 /* There is no valid C++ program where a non-template type is
17281 followed by a "<". That usually indicates that the user thought
17282 that the type was a template. */
17283 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17284 token->location);
17286 return TYPE_NAME (type);
17289 /* The type-specifier must be a user-defined type. */
17290 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17292 bool qualified_p;
17293 bool global_p;
17295 /* Don't gobble tokens or issue error messages if this is an
17296 optional type-specifier. */
17297 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17298 cp_parser_parse_tentatively (parser);
17300 token = cp_lexer_peek_token (parser->lexer);
17302 /* Look for the optional `::' operator. */
17303 global_p
17304 = (cp_parser_global_scope_opt (parser,
17305 /*current_scope_valid_p=*/false)
17306 != NULL_TREE);
17307 /* Look for the nested-name specifier. */
17308 qualified_p
17309 = (cp_parser_nested_name_specifier_opt (parser,
17310 /*typename_keyword_p=*/false,
17311 /*check_dependency_p=*/true,
17312 /*type_p=*/false,
17313 /*is_declaration=*/false)
17314 != NULL_TREE);
17315 /* If we have seen a nested-name-specifier, and the next token
17316 is `template', then we are using the template-id production. */
17317 if (parser->scope
17318 && cp_parser_optional_template_keyword (parser))
17320 /* Look for the template-id. */
17321 type = cp_parser_template_id (parser,
17322 /*template_keyword_p=*/true,
17323 /*check_dependency_p=*/true,
17324 none_type,
17325 /*is_declaration=*/false);
17326 /* If the template-id did not name a type, we are out of
17327 luck. */
17328 if (TREE_CODE (type) != TYPE_DECL)
17330 cp_parser_error (parser, "expected template-id for type");
17331 type = NULL_TREE;
17334 /* Otherwise, look for a type-name. */
17335 else
17336 type = cp_parser_type_name (parser);
17337 /* Keep track of all name-lookups performed in class scopes. */
17338 if (type
17339 && !global_p
17340 && !qualified_p
17341 && TREE_CODE (type) == TYPE_DECL
17342 && identifier_p (DECL_NAME (type)))
17343 maybe_note_name_used_in_class (DECL_NAME (type), type);
17344 /* If it didn't work out, we don't have a TYPE. */
17345 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17346 && !cp_parser_parse_definitely (parser))
17347 type = NULL_TREE;
17348 if (!type && cxx_dialect >= cxx17)
17350 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17351 cp_parser_parse_tentatively (parser);
17353 cp_parser_global_scope_opt (parser,
17354 /*current_scope_valid_p=*/false);
17355 cp_parser_nested_name_specifier_opt (parser,
17356 /*typename_keyword_p=*/false,
17357 /*check_dependency_p=*/true,
17358 /*type_p=*/false,
17359 /*is_declaration=*/false);
17360 tree name = cp_parser_identifier (parser);
17361 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17362 && parser->scope != error_mark_node)
17364 tree tmpl = cp_parser_lookup_name (parser, name,
17365 none_type,
17366 /*is_template=*/false,
17367 /*is_namespace=*/false,
17368 /*check_dependency=*/true,
17369 /*ambiguous_decls=*/NULL,
17370 token->location);
17371 if (tmpl && tmpl != error_mark_node
17372 && (DECL_CLASS_TEMPLATE_P (tmpl)
17373 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17374 type = make_template_placeholder (tmpl);
17375 else
17377 type = error_mark_node;
17378 if (!cp_parser_simulate_error (parser))
17379 cp_parser_name_lookup_error (parser, name, tmpl,
17380 NLE_TYPE, token->location);
17383 else
17384 type = error_mark_node;
17386 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17387 && !cp_parser_parse_definitely (parser))
17388 type = NULL_TREE;
17390 if (type && decl_specs)
17391 cp_parser_set_decl_spec_type (decl_specs, type,
17392 token,
17393 /*type_definition_p=*/false);
17396 /* If we didn't get a type-name, issue an error message. */
17397 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17399 cp_parser_error (parser, "expected type-name");
17400 return error_mark_node;
17403 if (type && type != error_mark_node)
17405 /* See if TYPE is an Objective-C type, and if so, parse and
17406 accept any protocol references following it. Do this before
17407 the cp_parser_check_for_invalid_template_id() call, because
17408 Objective-C types can be followed by '<...>' which would
17409 enclose protocol names rather than template arguments, and so
17410 everything is fine. */
17411 if (c_dialect_objc () && !parser->scope
17412 && (objc_is_id (type) || objc_is_class_name (type)))
17414 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17415 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17417 /* Clobber the "unqualified" type previously entered into
17418 DECL_SPECS with the new, improved protocol-qualified version. */
17419 if (decl_specs)
17420 decl_specs->type = qual_type;
17422 return qual_type;
17425 /* There is no valid C++ program where a non-template type is
17426 followed by a "<". That usually indicates that the user
17427 thought that the type was a template. */
17428 cp_parser_check_for_invalid_template_id (parser, type,
17429 none_type,
17430 token->location);
17433 return type;
17436 /* Parse a type-name.
17438 type-name:
17439 class-name
17440 enum-name
17441 typedef-name
17442 simple-template-id [in c++0x]
17444 enum-name:
17445 identifier
17447 typedef-name:
17448 identifier
17450 Concepts:
17452 type-name:
17453 concept-name
17454 partial-concept-id
17456 concept-name:
17457 identifier
17459 Returns a TYPE_DECL for the type. */
17461 static tree
17462 cp_parser_type_name (cp_parser* parser)
17464 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17467 /* See above. */
17468 static tree
17469 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17471 tree type_decl;
17473 /* We can't know yet whether it is a class-name or not. */
17474 cp_parser_parse_tentatively (parser);
17475 /* Try a class-name. */
17476 type_decl = cp_parser_class_name (parser,
17477 typename_keyword_p,
17478 /*template_keyword_p=*/false,
17479 none_type,
17480 /*check_dependency_p=*/true,
17481 /*class_head_p=*/false,
17482 /*is_declaration=*/false);
17483 /* If it's not a class-name, keep looking. */
17484 if (!cp_parser_parse_definitely (parser))
17486 if (cxx_dialect < cxx11)
17487 /* It must be a typedef-name or an enum-name. */
17488 return cp_parser_nonclass_name (parser);
17490 cp_parser_parse_tentatively (parser);
17491 /* It is either a simple-template-id representing an
17492 instantiation of an alias template... */
17493 type_decl = cp_parser_template_id (parser,
17494 /*template_keyword_p=*/false,
17495 /*check_dependency_p=*/true,
17496 none_type,
17497 /*is_declaration=*/false);
17498 /* Note that this must be an instantiation of an alias template
17499 because [temp.names]/6 says:
17501 A template-id that names an alias template specialization
17502 is a type-name.
17504 Whereas [temp.names]/7 says:
17506 A simple-template-id that names a class template
17507 specialization is a class-name.
17509 With concepts, this could also be a partial-concept-id that
17510 declares a non-type template parameter. */
17511 if (type_decl != NULL_TREE
17512 && TREE_CODE (type_decl) == TYPE_DECL
17513 && TYPE_DECL_ALIAS_P (type_decl))
17514 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17515 else if (is_constrained_parameter (type_decl))
17516 /* Don't do anything. */ ;
17517 else
17518 cp_parser_simulate_error (parser);
17520 if (!cp_parser_parse_definitely (parser))
17521 /* ... Or a typedef-name or an enum-name. */
17522 return cp_parser_nonclass_name (parser);
17525 return type_decl;
17528 /* Check if DECL and ARGS can form a constrained-type-specifier.
17529 If ARGS is non-null, we try to form a concept check of the
17530 form DECL<?, ARGS> where ? is a wildcard that matches any
17531 kind of template argument. If ARGS is NULL, then we try to
17532 form a concept check of the form DECL<?>. */
17534 static tree
17535 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17536 tree decl, tree args)
17538 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17540 /* If we a constrained-type-specifier cannot be deduced. */
17541 if (parser->prevent_constrained_type_specifiers)
17542 return NULL_TREE;
17544 /* A constrained type specifier can only be found in an
17545 overload set or as a reference to a template declaration.
17547 FIXME: This might be masking a bug. It's possible that
17548 that the deduction below is causing template specializations
17549 to be formed with the wildcard as an argument. */
17550 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17551 return NULL_TREE;
17553 /* Try to build a call expression that evaluates the
17554 concept. This can fail if the overload set refers
17555 only to non-templates. */
17556 tree placeholder = build_nt (WILDCARD_DECL);
17557 tree check = build_concept_check (decl, placeholder, args);
17558 if (check == error_mark_node)
17559 return NULL_TREE;
17561 /* Deduce the checked constraint and the prototype parameter.
17563 FIXME: In certain cases, failure to deduce should be a
17564 diagnosable error. */
17565 tree conc;
17566 tree proto;
17567 if (!deduce_constrained_parameter (check, conc, proto))
17568 return NULL_TREE;
17570 /* In template parameter scope, this results in a constrained
17571 parameter. Return a descriptor of that parm. */
17572 if (processing_template_parmlist)
17573 return build_constrained_parameter (conc, proto, args);
17575 /* In a parameter-declaration-clause, constrained-type
17576 specifiers result in invented template parameters. */
17577 if (parser->auto_is_implicit_function_template_parm_p)
17579 tree x = build_constrained_parameter (conc, proto, args);
17580 return synthesize_implicit_template_parm (parser, x);
17582 else
17584 /* Otherwise, we're in a context where the constrained
17585 type name is deduced and the constraint applies
17586 after deduction. */
17587 return make_constrained_auto (conc, args);
17590 return NULL_TREE;
17593 /* If DECL refers to a concept, return a TYPE_DECL representing
17594 the result of using the constrained type specifier in the
17595 current context. DECL refers to a concept if
17597 - it is an overload set containing a function concept taking a single
17598 type argument, or
17600 - it is a variable concept taking a single type argument. */
17602 static tree
17603 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17605 if (flag_concepts
17606 && (TREE_CODE (decl) == OVERLOAD
17607 || BASELINK_P (decl)
17608 || variable_concept_p (decl)))
17609 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17610 else
17611 return NULL_TREE;
17614 /* Check if DECL and ARGS form a partial-concept-id. If so,
17615 assign ID to the resulting constrained placeholder.
17617 Returns true if the partial-concept-id designates a placeholder
17618 and false otherwise. Note that *id is set to NULL_TREE in
17619 this case. */
17621 static tree
17622 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17624 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17627 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17628 or a concept-name.
17630 enum-name:
17631 identifier
17633 typedef-name:
17634 identifier
17636 concept-name:
17637 identifier
17639 Returns a TYPE_DECL for the type. */
17641 static tree
17642 cp_parser_nonclass_name (cp_parser* parser)
17644 tree type_decl;
17645 tree identifier;
17647 cp_token *token = cp_lexer_peek_token (parser->lexer);
17648 identifier = cp_parser_identifier (parser);
17649 if (identifier == error_mark_node)
17650 return error_mark_node;
17652 /* Look up the type-name. */
17653 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17655 type_decl = strip_using_decl (type_decl);
17657 /* If we found an overload set, then it may refer to a concept-name. */
17658 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17659 type_decl = decl;
17661 if (TREE_CODE (type_decl) != TYPE_DECL
17662 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17664 /* See if this is an Objective-C type. */
17665 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17666 tree type = objc_get_protocol_qualified_type (identifier, protos);
17667 if (type)
17668 type_decl = TYPE_NAME (type);
17671 /* Issue an error if we did not find a type-name. */
17672 if (TREE_CODE (type_decl) != TYPE_DECL
17673 /* In Objective-C, we have the complication that class names are
17674 normally type names and start declarations (eg, the
17675 "NSObject" in "NSObject *object;"), but can be used in an
17676 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17677 is an expression. So, a classname followed by a dot is not a
17678 valid type-name. */
17679 || (objc_is_class_name (TREE_TYPE (type_decl))
17680 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17682 if (!cp_parser_simulate_error (parser))
17683 cp_parser_name_lookup_error (parser, identifier, type_decl,
17684 NLE_TYPE, token->location);
17685 return error_mark_node;
17687 /* Remember that the name was used in the definition of the
17688 current class so that we can check later to see if the
17689 meaning would have been different after the class was
17690 entirely defined. */
17691 else if (type_decl != error_mark_node
17692 && !parser->scope)
17693 maybe_note_name_used_in_class (identifier, type_decl);
17695 return type_decl;
17698 /* Parse an elaborated-type-specifier. Note that the grammar given
17699 here incorporates the resolution to DR68.
17701 elaborated-type-specifier:
17702 class-key :: [opt] nested-name-specifier [opt] identifier
17703 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17704 enum-key :: [opt] nested-name-specifier [opt] identifier
17705 typename :: [opt] nested-name-specifier identifier
17706 typename :: [opt] nested-name-specifier template [opt]
17707 template-id
17709 GNU extension:
17711 elaborated-type-specifier:
17712 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17713 class-key attributes :: [opt] nested-name-specifier [opt]
17714 template [opt] template-id
17715 enum attributes :: [opt] nested-name-specifier [opt] identifier
17717 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17718 declared `friend'. If IS_DECLARATION is TRUE, then this
17719 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17720 something is being declared.
17722 Returns the TYPE specified. */
17724 static tree
17725 cp_parser_elaborated_type_specifier (cp_parser* parser,
17726 bool is_friend,
17727 bool is_declaration)
17729 enum tag_types tag_type;
17730 tree identifier;
17731 tree type = NULL_TREE;
17732 tree attributes = NULL_TREE;
17733 tree globalscope;
17734 cp_token *token = NULL;
17736 /* See if we're looking at the `enum' keyword. */
17737 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17739 /* Consume the `enum' token. */
17740 cp_lexer_consume_token (parser->lexer);
17741 /* Remember that it's an enumeration type. */
17742 tag_type = enum_type;
17743 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17744 enums) is used here. */
17745 cp_token *token = cp_lexer_peek_token (parser->lexer);
17746 if (cp_parser_is_keyword (token, RID_CLASS)
17747 || cp_parser_is_keyword (token, RID_STRUCT))
17749 gcc_rich_location richloc (token->location);
17750 richloc.add_range (input_location, false);
17751 richloc.add_fixit_remove ();
17752 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17753 "a scoped enum must not use the %qD keyword",
17754 token->u.value);
17755 /* Consume the `struct' or `class' and parse it anyway. */
17756 cp_lexer_consume_token (parser->lexer);
17758 /* Parse the attributes. */
17759 attributes = cp_parser_attributes_opt (parser);
17761 /* Or, it might be `typename'. */
17762 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17763 RID_TYPENAME))
17765 /* Consume the `typename' token. */
17766 cp_lexer_consume_token (parser->lexer);
17767 /* Remember that it's a `typename' type. */
17768 tag_type = typename_type;
17770 /* Otherwise it must be a class-key. */
17771 else
17773 tag_type = cp_parser_class_key (parser);
17774 if (tag_type == none_type)
17775 return error_mark_node;
17776 /* Parse the attributes. */
17777 attributes = cp_parser_attributes_opt (parser);
17780 /* Look for the `::' operator. */
17781 globalscope = cp_parser_global_scope_opt (parser,
17782 /*current_scope_valid_p=*/false);
17783 /* Look for the nested-name-specifier. */
17784 tree nested_name_specifier;
17785 if (tag_type == typename_type && !globalscope)
17787 nested_name_specifier
17788 = cp_parser_nested_name_specifier (parser,
17789 /*typename_keyword_p=*/true,
17790 /*check_dependency_p=*/true,
17791 /*type_p=*/true,
17792 is_declaration);
17793 if (!nested_name_specifier)
17794 return error_mark_node;
17796 else
17797 /* Even though `typename' is not present, the proposed resolution
17798 to Core Issue 180 says that in `class A<T>::B', `B' should be
17799 considered a type-name, even if `A<T>' is dependent. */
17800 nested_name_specifier
17801 = cp_parser_nested_name_specifier_opt (parser,
17802 /*typename_keyword_p=*/true,
17803 /*check_dependency_p=*/true,
17804 /*type_p=*/true,
17805 is_declaration);
17806 /* For everything but enumeration types, consider a template-id.
17807 For an enumeration type, consider only a plain identifier. */
17808 if (tag_type != enum_type)
17810 bool template_p = false;
17811 tree decl;
17813 /* Allow the `template' keyword. */
17814 template_p = cp_parser_optional_template_keyword (parser);
17815 /* If we didn't see `template', we don't know if there's a
17816 template-id or not. */
17817 if (!template_p)
17818 cp_parser_parse_tentatively (parser);
17819 /* Parse the template-id. */
17820 token = cp_lexer_peek_token (parser->lexer);
17821 decl = cp_parser_template_id (parser, template_p,
17822 /*check_dependency_p=*/true,
17823 tag_type,
17824 is_declaration);
17825 /* If we didn't find a template-id, look for an ordinary
17826 identifier. */
17827 if (!template_p && !cp_parser_parse_definitely (parser))
17829 /* We can get here when cp_parser_template_id, called by
17830 cp_parser_class_name with tag_type == none_type, succeeds
17831 and caches a BASELINK. Then, when called again here,
17832 instead of failing and returning an error_mark_node
17833 returns it (see template/typename17.C in C++11).
17834 ??? Could we diagnose this earlier? */
17835 else if (tag_type == typename_type && BASELINK_P (decl))
17837 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17838 type = error_mark_node;
17840 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17841 in effect, then we must assume that, upon instantiation, the
17842 template will correspond to a class. */
17843 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17844 && tag_type == typename_type)
17845 type = make_typename_type (parser->scope, decl,
17846 typename_type,
17847 /*complain=*/tf_error);
17848 /* If the `typename' keyword is in effect and DECL is not a type
17849 decl, then type is non existent. */
17850 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17852 else if (TREE_CODE (decl) == TYPE_DECL)
17854 type = check_elaborated_type_specifier (tag_type, decl,
17855 /*allow_template_p=*/true);
17857 /* If the next token is a semicolon, this must be a specialization,
17858 instantiation, or friend declaration. Check the scope while we
17859 still know whether or not we had a nested-name-specifier. */
17860 if (type != error_mark_node
17861 && !nested_name_specifier && !is_friend
17862 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17863 check_unqualified_spec_or_inst (type, token->location);
17865 else if (decl == error_mark_node)
17866 type = error_mark_node;
17869 if (!type)
17871 token = cp_lexer_peek_token (parser->lexer);
17872 identifier = cp_parser_identifier (parser);
17874 if (identifier == error_mark_node)
17876 parser->scope = NULL_TREE;
17877 return error_mark_node;
17880 /* For a `typename', we needn't call xref_tag. */
17881 if (tag_type == typename_type
17882 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17883 return cp_parser_make_typename_type (parser, identifier,
17884 token->location);
17886 /* Template parameter lists apply only if we are not within a
17887 function parameter list. */
17888 bool template_parm_lists_apply
17889 = parser->num_template_parameter_lists;
17890 if (template_parm_lists_apply)
17891 for (cp_binding_level *s = current_binding_level;
17892 s && s->kind != sk_template_parms;
17893 s = s->level_chain)
17894 if (s->kind == sk_function_parms)
17895 template_parm_lists_apply = false;
17897 /* Look up a qualified name in the usual way. */
17898 if (parser->scope)
17900 tree decl;
17901 tree ambiguous_decls;
17903 decl = cp_parser_lookup_name (parser, identifier,
17904 tag_type,
17905 /*is_template=*/false,
17906 /*is_namespace=*/false,
17907 /*check_dependency=*/true,
17908 &ambiguous_decls,
17909 token->location);
17911 /* If the lookup was ambiguous, an error will already have been
17912 issued. */
17913 if (ambiguous_decls)
17914 return error_mark_node;
17916 /* If we are parsing friend declaration, DECL may be a
17917 TEMPLATE_DECL tree node here. However, we need to check
17918 whether this TEMPLATE_DECL results in valid code. Consider
17919 the following example:
17921 namespace N {
17922 template <class T> class C {};
17924 class X {
17925 template <class T> friend class N::C; // #1, valid code
17927 template <class T> class Y {
17928 friend class N::C; // #2, invalid code
17931 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17932 name lookup of `N::C'. We see that friend declaration must
17933 be template for the code to be valid. Note that
17934 processing_template_decl does not work here since it is
17935 always 1 for the above two cases. */
17937 decl = (cp_parser_maybe_treat_template_as_class
17938 (decl, /*tag_name_p=*/is_friend
17939 && template_parm_lists_apply));
17941 if (TREE_CODE (decl) != TYPE_DECL)
17943 cp_parser_diagnose_invalid_type_name (parser,
17944 identifier,
17945 token->location);
17946 return error_mark_node;
17949 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17951 bool allow_template = (template_parm_lists_apply
17952 || DECL_SELF_REFERENCE_P (decl));
17953 type = check_elaborated_type_specifier (tag_type, decl,
17954 allow_template);
17956 if (type == error_mark_node)
17957 return error_mark_node;
17960 /* Forward declarations of nested types, such as
17962 class C1::C2;
17963 class C1::C2::C3;
17965 are invalid unless all components preceding the final '::'
17966 are complete. If all enclosing types are complete, these
17967 declarations become merely pointless.
17969 Invalid forward declarations of nested types are errors
17970 caught elsewhere in parsing. Those that are pointless arrive
17971 here. */
17973 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17974 && !is_friend && !processing_explicit_instantiation)
17975 warning (0, "declaration %qD does not declare anything", decl);
17977 type = TREE_TYPE (decl);
17979 else
17981 /* An elaborated-type-specifier sometimes introduces a new type and
17982 sometimes names an existing type. Normally, the rule is that it
17983 introduces a new type only if there is not an existing type of
17984 the same name already in scope. For example, given:
17986 struct S {};
17987 void f() { struct S s; }
17989 the `struct S' in the body of `f' is the same `struct S' as in
17990 the global scope; the existing definition is used. However, if
17991 there were no global declaration, this would introduce a new
17992 local class named `S'.
17994 An exception to this rule applies to the following code:
17996 namespace N { struct S; }
17998 Here, the elaborated-type-specifier names a new type
17999 unconditionally; even if there is already an `S' in the
18000 containing scope this declaration names a new type.
18001 This exception only applies if the elaborated-type-specifier
18002 forms the complete declaration:
18004 [class.name]
18006 A declaration consisting solely of `class-key identifier ;' is
18007 either a redeclaration of the name in the current scope or a
18008 forward declaration of the identifier as a class name. It
18009 introduces the name into the current scope.
18011 We are in this situation precisely when the next token is a `;'.
18013 An exception to the exception is that a `friend' declaration does
18014 *not* name a new type; i.e., given:
18016 struct S { friend struct T; };
18018 `T' is not a new type in the scope of `S'.
18020 Also, `new struct S' or `sizeof (struct S)' never results in the
18021 definition of a new type; a new type can only be declared in a
18022 declaration context. */
18024 tag_scope ts;
18025 bool template_p;
18027 if (is_friend)
18028 /* Friends have special name lookup rules. */
18029 ts = ts_within_enclosing_non_class;
18030 else if (is_declaration
18031 && cp_lexer_next_token_is (parser->lexer,
18032 CPP_SEMICOLON))
18033 /* This is a `class-key identifier ;' */
18034 ts = ts_current;
18035 else
18036 ts = ts_global;
18038 template_p =
18039 (template_parm_lists_apply
18040 && (cp_parser_next_token_starts_class_definition_p (parser)
18041 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18042 /* An unqualified name was used to reference this type, so
18043 there were no qualifying templates. */
18044 if (template_parm_lists_apply
18045 && !cp_parser_check_template_parameters (parser,
18046 /*num_templates=*/0,
18047 /*template_id*/false,
18048 token->location,
18049 /*declarator=*/NULL))
18050 return error_mark_node;
18051 type = xref_tag (tag_type, identifier, ts, template_p);
18055 if (type == error_mark_node)
18056 return error_mark_node;
18058 /* Allow attributes on forward declarations of classes. */
18059 if (attributes)
18061 if (TREE_CODE (type) == TYPENAME_TYPE)
18062 warning (OPT_Wattributes,
18063 "attributes ignored on uninstantiated type");
18064 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18065 && ! processing_explicit_instantiation)
18066 warning (OPT_Wattributes,
18067 "attributes ignored on template instantiation");
18068 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18069 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18070 else
18071 warning (OPT_Wattributes,
18072 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18075 if (tag_type != enum_type)
18077 /* Indicate whether this class was declared as a `class' or as a
18078 `struct'. */
18079 if (CLASS_TYPE_P (type))
18080 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18081 cp_parser_check_class_key (tag_type, type);
18084 /* A "<" cannot follow an elaborated type specifier. If that
18085 happens, the user was probably trying to form a template-id. */
18086 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18087 token->location);
18089 return type;
18092 /* Parse an enum-specifier.
18094 enum-specifier:
18095 enum-head { enumerator-list [opt] }
18096 enum-head { enumerator-list , } [C++0x]
18098 enum-head:
18099 enum-key identifier [opt] enum-base [opt]
18100 enum-key nested-name-specifier identifier enum-base [opt]
18102 enum-key:
18103 enum
18104 enum class [C++0x]
18105 enum struct [C++0x]
18107 enum-base: [C++0x]
18108 : type-specifier-seq
18110 opaque-enum-specifier:
18111 enum-key identifier enum-base [opt] ;
18113 GNU Extensions:
18114 enum-key attributes[opt] identifier [opt] enum-base [opt]
18115 { enumerator-list [opt] }attributes[opt]
18116 enum-key attributes[opt] identifier [opt] enum-base [opt]
18117 { enumerator-list, }attributes[opt] [C++0x]
18119 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18120 if the token stream isn't an enum-specifier after all. */
18122 static tree
18123 cp_parser_enum_specifier (cp_parser* parser)
18125 tree identifier;
18126 tree type = NULL_TREE;
18127 tree prev_scope;
18128 tree nested_name_specifier = NULL_TREE;
18129 tree attributes;
18130 bool scoped_enum_p = false;
18131 bool has_underlying_type = false;
18132 bool nested_being_defined = false;
18133 bool new_value_list = false;
18134 bool is_new_type = false;
18135 bool is_unnamed = false;
18136 tree underlying_type = NULL_TREE;
18137 cp_token *type_start_token = NULL;
18138 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18140 parser->colon_corrects_to_scope_p = false;
18142 /* Parse tentatively so that we can back up if we don't find a
18143 enum-specifier. */
18144 cp_parser_parse_tentatively (parser);
18146 /* Caller guarantees that the current token is 'enum', an identifier
18147 possibly follows, and the token after that is an opening brace.
18148 If we don't have an identifier, fabricate an anonymous name for
18149 the enumeration being defined. */
18150 cp_lexer_consume_token (parser->lexer);
18152 /* Parse the "class" or "struct", which indicates a scoped
18153 enumeration type in C++0x. */
18154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18155 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18157 if (cxx_dialect < cxx11)
18158 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18160 /* Consume the `struct' or `class' token. */
18161 cp_lexer_consume_token (parser->lexer);
18163 scoped_enum_p = true;
18166 attributes = cp_parser_attributes_opt (parser);
18168 /* Clear the qualification. */
18169 parser->scope = NULL_TREE;
18170 parser->qualifying_scope = NULL_TREE;
18171 parser->object_scope = NULL_TREE;
18173 /* Figure out in what scope the declaration is being placed. */
18174 prev_scope = current_scope ();
18176 type_start_token = cp_lexer_peek_token (parser->lexer);
18178 push_deferring_access_checks (dk_no_check);
18179 nested_name_specifier
18180 = cp_parser_nested_name_specifier_opt (parser,
18181 /*typename_keyword_p=*/true,
18182 /*check_dependency_p=*/false,
18183 /*type_p=*/false,
18184 /*is_declaration=*/false);
18186 if (nested_name_specifier)
18188 tree name;
18190 identifier = cp_parser_identifier (parser);
18191 name = cp_parser_lookup_name (parser, identifier,
18192 enum_type,
18193 /*is_template=*/false,
18194 /*is_namespace=*/false,
18195 /*check_dependency=*/true,
18196 /*ambiguous_decls=*/NULL,
18197 input_location);
18198 if (name && name != error_mark_node)
18200 type = TREE_TYPE (name);
18201 if (TREE_CODE (type) == TYPENAME_TYPE)
18203 /* Are template enums allowed in ISO? */
18204 if (template_parm_scope_p ())
18205 pedwarn (type_start_token->location, OPT_Wpedantic,
18206 "%qD is an enumeration template", name);
18207 /* ignore a typename reference, for it will be solved by name
18208 in start_enum. */
18209 type = NULL_TREE;
18212 else if (nested_name_specifier == error_mark_node)
18213 /* We already issued an error. */;
18214 else
18216 error_at (type_start_token->location,
18217 "%qD does not name an enumeration in %qT",
18218 identifier, nested_name_specifier);
18219 nested_name_specifier = error_mark_node;
18222 else
18224 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18225 identifier = cp_parser_identifier (parser);
18226 else
18228 identifier = make_anon_name ();
18229 is_unnamed = true;
18230 if (scoped_enum_p)
18231 error_at (type_start_token->location,
18232 "unnamed scoped enum is not allowed");
18235 pop_deferring_access_checks ();
18237 /* Check for the `:' that denotes a specified underlying type in C++0x.
18238 Note that a ':' could also indicate a bitfield width, however. */
18239 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18241 cp_decl_specifier_seq type_specifiers;
18243 /* Consume the `:'. */
18244 cp_lexer_consume_token (parser->lexer);
18246 /* Parse the type-specifier-seq. */
18247 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18248 /*is_trailing_return=*/false,
18249 &type_specifiers);
18251 /* At this point this is surely not elaborated type specifier. */
18252 if (!cp_parser_parse_definitely (parser))
18253 return NULL_TREE;
18255 if (cxx_dialect < cxx11)
18256 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18258 has_underlying_type = true;
18260 /* If that didn't work, stop. */
18261 if (type_specifiers.type != error_mark_node)
18263 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18264 /*initialized=*/0, NULL);
18265 if (underlying_type == error_mark_node
18266 || check_for_bare_parameter_packs (underlying_type))
18267 underlying_type = NULL_TREE;
18271 /* Look for the `{' but don't consume it yet. */
18272 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18274 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18276 cp_parser_error (parser, "expected %<{%>");
18277 if (has_underlying_type)
18279 type = NULL_TREE;
18280 goto out;
18283 /* An opaque-enum-specifier must have a ';' here. */
18284 if ((scoped_enum_p || underlying_type)
18285 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18287 cp_parser_error (parser, "expected %<;%> or %<{%>");
18288 if (has_underlying_type)
18290 type = NULL_TREE;
18291 goto out;
18296 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18297 return NULL_TREE;
18299 if (nested_name_specifier)
18301 if (CLASS_TYPE_P (nested_name_specifier))
18303 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18304 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18305 push_scope (nested_name_specifier);
18307 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18309 push_nested_namespace (nested_name_specifier);
18313 /* Issue an error message if type-definitions are forbidden here. */
18314 if (!cp_parser_check_type_definition (parser))
18315 type = error_mark_node;
18316 else
18317 /* Create the new type. We do this before consuming the opening
18318 brace so the enum will be recorded as being on the line of its
18319 tag (or the 'enum' keyword, if there is no tag). */
18320 type = start_enum (identifier, type, underlying_type,
18321 attributes, scoped_enum_p, &is_new_type);
18323 /* If the next token is not '{' it is an opaque-enum-specifier or an
18324 elaborated-type-specifier. */
18325 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18327 timevar_push (TV_PARSE_ENUM);
18328 if (nested_name_specifier
18329 && nested_name_specifier != error_mark_node)
18331 /* The following catches invalid code such as:
18332 enum class S<int>::E { A, B, C }; */
18333 if (!processing_specialization
18334 && CLASS_TYPE_P (nested_name_specifier)
18335 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18336 error_at (type_start_token->location, "cannot add an enumerator "
18337 "list to a template instantiation");
18339 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18341 error_at (type_start_token->location,
18342 "%<%T::%E%> has not been declared",
18343 TYPE_CONTEXT (nested_name_specifier),
18344 nested_name_specifier);
18345 type = error_mark_node;
18347 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18348 && !CLASS_TYPE_P (nested_name_specifier))
18350 error_at (type_start_token->location, "nested name specifier "
18351 "%qT for enum declaration does not name a class "
18352 "or namespace", nested_name_specifier);
18353 type = error_mark_node;
18355 /* If that scope does not contain the scope in which the
18356 class was originally declared, the program is invalid. */
18357 else if (prev_scope && !is_ancestor (prev_scope,
18358 nested_name_specifier))
18360 if (at_namespace_scope_p ())
18361 error_at (type_start_token->location,
18362 "declaration of %qD in namespace %qD which does not "
18363 "enclose %qD",
18364 type, prev_scope, nested_name_specifier);
18365 else
18366 error_at (type_start_token->location,
18367 "declaration of %qD in %qD which does not "
18368 "enclose %qD",
18369 type, prev_scope, nested_name_specifier);
18370 type = error_mark_node;
18372 /* If that scope is the scope where the declaration is being placed
18373 the program is invalid. */
18374 else if (CLASS_TYPE_P (nested_name_specifier)
18375 && CLASS_TYPE_P (prev_scope)
18376 && same_type_p (nested_name_specifier, prev_scope))
18378 permerror (type_start_token->location,
18379 "extra qualification not allowed");
18380 nested_name_specifier = NULL_TREE;
18384 if (scoped_enum_p)
18385 begin_scope (sk_scoped_enum, type);
18387 /* Consume the opening brace. */
18388 matching_braces braces;
18389 braces.consume_open (parser);
18391 if (type == error_mark_node)
18392 ; /* Nothing to add */
18393 else if (OPAQUE_ENUM_P (type)
18394 || (cxx_dialect > cxx98 && processing_specialization))
18396 new_value_list = true;
18397 SET_OPAQUE_ENUM_P (type, false);
18398 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18400 else
18402 error_at (type_start_token->location,
18403 "multiple definition of %q#T", type);
18404 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18405 "previous definition here");
18406 type = error_mark_node;
18409 if (type == error_mark_node)
18410 cp_parser_skip_to_end_of_block_or_statement (parser);
18411 /* If the next token is not '}', then there are some enumerators. */
18412 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18414 if (is_unnamed && !scoped_enum_p)
18415 pedwarn (type_start_token->location, OPT_Wpedantic,
18416 "ISO C++ forbids empty unnamed enum");
18418 else
18419 cp_parser_enumerator_list (parser, type);
18421 /* Consume the final '}'. */
18422 braces.require_close (parser);
18424 if (scoped_enum_p)
18425 finish_scope ();
18426 timevar_pop (TV_PARSE_ENUM);
18428 else
18430 /* If a ';' follows, then it is an opaque-enum-specifier
18431 and additional restrictions apply. */
18432 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18434 if (is_unnamed)
18435 error_at (type_start_token->location,
18436 "opaque-enum-specifier without name");
18437 else if (nested_name_specifier)
18438 error_at (type_start_token->location,
18439 "opaque-enum-specifier must use a simple identifier");
18443 /* Look for trailing attributes to apply to this enumeration, and
18444 apply them if appropriate. */
18445 if (cp_parser_allow_gnu_extensions_p (parser))
18447 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18448 cplus_decl_attributes (&type,
18449 trailing_attr,
18450 (int) ATTR_FLAG_TYPE_IN_PLACE);
18453 /* Finish up the enumeration. */
18454 if (type != error_mark_node)
18456 if (new_value_list)
18457 finish_enum_value_list (type);
18458 if (is_new_type)
18459 finish_enum (type);
18462 if (nested_name_specifier)
18464 if (CLASS_TYPE_P (nested_name_specifier))
18466 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18467 pop_scope (nested_name_specifier);
18469 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18471 pop_nested_namespace (nested_name_specifier);
18474 out:
18475 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18476 return type;
18479 /* Parse an enumerator-list. The enumerators all have the indicated
18480 TYPE.
18482 enumerator-list:
18483 enumerator-definition
18484 enumerator-list , enumerator-definition */
18486 static void
18487 cp_parser_enumerator_list (cp_parser* parser, tree type)
18489 while (true)
18491 /* Parse an enumerator-definition. */
18492 cp_parser_enumerator_definition (parser, type);
18494 /* If the next token is not a ',', we've reached the end of
18495 the list. */
18496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18497 break;
18498 /* Otherwise, consume the `,' and keep going. */
18499 cp_lexer_consume_token (parser->lexer);
18500 /* If the next token is a `}', there is a trailing comma. */
18501 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18503 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18504 pedwarn (input_location, OPT_Wpedantic,
18505 "comma at end of enumerator list");
18506 break;
18511 /* Parse an enumerator-definition. The enumerator has the indicated
18512 TYPE.
18514 enumerator-definition:
18515 enumerator
18516 enumerator = constant-expression
18518 enumerator:
18519 identifier
18521 GNU Extensions:
18523 enumerator-definition:
18524 enumerator attributes [opt]
18525 enumerator attributes [opt] = constant-expression */
18527 static void
18528 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18530 tree identifier;
18531 tree value;
18532 location_t loc;
18534 /* Save the input location because we are interested in the location
18535 of the identifier and not the location of the explicit value. */
18536 loc = cp_lexer_peek_token (parser->lexer)->location;
18538 /* Look for the identifier. */
18539 identifier = cp_parser_identifier (parser);
18540 if (identifier == error_mark_node)
18541 return;
18543 /* Parse any specified attributes. */
18544 tree attrs = cp_parser_attributes_opt (parser);
18546 /* If the next token is an '=', then there is an explicit value. */
18547 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18549 /* Consume the `=' token. */
18550 cp_lexer_consume_token (parser->lexer);
18551 /* Parse the value. */
18552 value = cp_parser_constant_expression (parser);
18554 else
18555 value = NULL_TREE;
18557 /* If we are processing a template, make sure the initializer of the
18558 enumerator doesn't contain any bare template parameter pack. */
18559 if (check_for_bare_parameter_packs (value))
18560 value = error_mark_node;
18562 /* Create the enumerator. */
18563 build_enumerator (identifier, value, type, attrs, loc);
18566 /* Parse a namespace-name.
18568 namespace-name:
18569 original-namespace-name
18570 namespace-alias
18572 Returns the NAMESPACE_DECL for the namespace. */
18574 static tree
18575 cp_parser_namespace_name (cp_parser* parser)
18577 tree identifier;
18578 tree namespace_decl;
18580 cp_token *token = cp_lexer_peek_token (parser->lexer);
18582 /* Get the name of the namespace. */
18583 identifier = cp_parser_identifier (parser);
18584 if (identifier == error_mark_node)
18585 return error_mark_node;
18587 /* Look up the identifier in the currently active scope. Look only
18588 for namespaces, due to:
18590 [basic.lookup.udir]
18592 When looking up a namespace-name in a using-directive or alias
18593 definition, only namespace names are considered.
18595 And:
18597 [basic.lookup.qual]
18599 During the lookup of a name preceding the :: scope resolution
18600 operator, object, function, and enumerator names are ignored.
18602 (Note that cp_parser_qualifying_entity only calls this
18603 function if the token after the name is the scope resolution
18604 operator.) */
18605 namespace_decl = cp_parser_lookup_name (parser, identifier,
18606 none_type,
18607 /*is_template=*/false,
18608 /*is_namespace=*/true,
18609 /*check_dependency=*/true,
18610 /*ambiguous_decls=*/NULL,
18611 token->location);
18612 /* If it's not a namespace, issue an error. */
18613 if (namespace_decl == error_mark_node
18614 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18616 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18618 error_at (token->location, "%qD is not a namespace-name", identifier);
18619 if (namespace_decl == error_mark_node
18620 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18621 suggest_alternative_in_explicit_scope (token->location, identifier,
18622 parser->scope);
18624 cp_parser_error (parser, "expected namespace-name");
18625 namespace_decl = error_mark_node;
18628 return namespace_decl;
18631 /* Parse a namespace-definition.
18633 namespace-definition:
18634 named-namespace-definition
18635 unnamed-namespace-definition
18637 named-namespace-definition:
18638 original-namespace-definition
18639 extension-namespace-definition
18641 original-namespace-definition:
18642 namespace identifier { namespace-body }
18644 extension-namespace-definition:
18645 namespace original-namespace-name { namespace-body }
18647 unnamed-namespace-definition:
18648 namespace { namespace-body } */
18650 static void
18651 cp_parser_namespace_definition (cp_parser* parser)
18653 tree identifier;
18654 int nested_definition_count = 0;
18656 cp_ensure_no_omp_declare_simd (parser);
18657 cp_ensure_no_oacc_routine (parser);
18659 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18661 if (is_inline)
18663 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18664 cp_lexer_consume_token (parser->lexer);
18667 /* Look for the `namespace' keyword. */
18668 cp_token* token
18669 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18671 /* Parse any specified attributes before the identifier. */
18672 tree attribs = cp_parser_attributes_opt (parser);
18674 for (;;)
18676 identifier = NULL_TREE;
18678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18680 identifier = cp_parser_identifier (parser);
18682 if (cp_next_tokens_can_be_std_attribute_p (parser))
18683 pedwarn (input_location, OPT_Wpedantic,
18684 "standard attributes on namespaces must precede "
18685 "the namespace name");
18687 /* Parse any attributes specified after the identifier. */
18688 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18692 break;
18694 if (!nested_definition_count && cxx_dialect < cxx17)
18695 pedwarn (input_location, OPT_Wpedantic,
18696 "nested namespace definitions only available with "
18697 "-std=c++17 or -std=gnu++17");
18699 /* Nested namespace names can create new namespaces (unlike
18700 other qualified-ids). */
18701 if (int count = identifier ? push_namespace (identifier) : 0)
18702 nested_definition_count += count;
18703 else
18704 cp_parser_error (parser, "nested namespace name required");
18705 cp_lexer_consume_token (parser->lexer);
18708 if (nested_definition_count && !identifier)
18709 cp_parser_error (parser, "namespace name required");
18711 if (nested_definition_count && attribs)
18712 error_at (token->location,
18713 "a nested namespace definition cannot have attributes");
18714 if (nested_definition_count && is_inline)
18715 error_at (token->location,
18716 "a nested namespace definition cannot be inline");
18718 /* Start the namespace. */
18719 nested_definition_count += push_namespace (identifier, is_inline);
18721 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18723 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18725 /* Look for the `{' to validate starting the namespace. */
18726 matching_braces braces;
18727 if (braces.require_open (parser))
18729 /* Parse the body of the namespace. */
18730 cp_parser_namespace_body (parser);
18732 /* Look for the final `}'. */
18733 braces.require_close (parser);
18736 if (has_visibility)
18737 pop_visibility (1);
18739 /* Pop the nested namespace definitions. */
18740 while (nested_definition_count--)
18741 pop_namespace ();
18744 /* Parse a namespace-body.
18746 namespace-body:
18747 declaration-seq [opt] */
18749 static void
18750 cp_parser_namespace_body (cp_parser* parser)
18752 cp_parser_declaration_seq_opt (parser);
18755 /* Parse a namespace-alias-definition.
18757 namespace-alias-definition:
18758 namespace identifier = qualified-namespace-specifier ; */
18760 static void
18761 cp_parser_namespace_alias_definition (cp_parser* parser)
18763 tree identifier;
18764 tree namespace_specifier;
18766 cp_token *token = cp_lexer_peek_token (parser->lexer);
18768 /* Look for the `namespace' keyword. */
18769 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18770 /* Look for the identifier. */
18771 identifier = cp_parser_identifier (parser);
18772 if (identifier == error_mark_node)
18773 return;
18774 /* Look for the `=' token. */
18775 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18776 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18778 error_at (token->location, "%<namespace%> definition is not allowed here");
18779 /* Skip the definition. */
18780 cp_lexer_consume_token (parser->lexer);
18781 if (cp_parser_skip_to_closing_brace (parser))
18782 cp_lexer_consume_token (parser->lexer);
18783 return;
18785 cp_parser_require (parser, CPP_EQ, RT_EQ);
18786 /* Look for the qualified-namespace-specifier. */
18787 namespace_specifier
18788 = cp_parser_qualified_namespace_specifier (parser);
18789 /* Look for the `;' token. */
18790 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18792 /* Register the alias in the symbol table. */
18793 do_namespace_alias (identifier, namespace_specifier);
18796 /* Parse a qualified-namespace-specifier.
18798 qualified-namespace-specifier:
18799 :: [opt] nested-name-specifier [opt] namespace-name
18801 Returns a NAMESPACE_DECL corresponding to the specified
18802 namespace. */
18804 static tree
18805 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18807 /* Look for the optional `::'. */
18808 cp_parser_global_scope_opt (parser,
18809 /*current_scope_valid_p=*/false);
18811 /* Look for the optional nested-name-specifier. */
18812 cp_parser_nested_name_specifier_opt (parser,
18813 /*typename_keyword_p=*/false,
18814 /*check_dependency_p=*/true,
18815 /*type_p=*/false,
18816 /*is_declaration=*/true);
18818 return cp_parser_namespace_name (parser);
18821 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18822 access declaration.
18824 using-declaration:
18825 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18826 using :: unqualified-id ;
18828 access-declaration:
18829 qualified-id ;
18833 static bool
18834 cp_parser_using_declaration (cp_parser* parser,
18835 bool access_declaration_p)
18837 cp_token *token;
18838 bool typename_p = false;
18839 bool global_scope_p;
18840 tree decl;
18841 tree identifier;
18842 tree qscope;
18843 int oldcount = errorcount;
18844 cp_token *diag_token = NULL;
18846 if (access_declaration_p)
18848 diag_token = cp_lexer_peek_token (parser->lexer);
18849 cp_parser_parse_tentatively (parser);
18851 else
18853 /* Look for the `using' keyword. */
18854 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18856 again:
18857 /* Peek at the next token. */
18858 token = cp_lexer_peek_token (parser->lexer);
18859 /* See if it's `typename'. */
18860 if (token->keyword == RID_TYPENAME)
18862 /* Remember that we've seen it. */
18863 typename_p = true;
18864 /* Consume the `typename' token. */
18865 cp_lexer_consume_token (parser->lexer);
18869 /* Look for the optional global scope qualification. */
18870 global_scope_p
18871 = (cp_parser_global_scope_opt (parser,
18872 /*current_scope_valid_p=*/false)
18873 != NULL_TREE);
18875 /* If we saw `typename', or didn't see `::', then there must be a
18876 nested-name-specifier present. */
18877 if (typename_p || !global_scope_p)
18879 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18880 /*check_dependency_p=*/true,
18881 /*type_p=*/false,
18882 /*is_declaration=*/true);
18883 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18885 cp_parser_skip_to_end_of_block_or_statement (parser);
18886 return false;
18889 /* Otherwise, we could be in either of the two productions. In that
18890 case, treat the nested-name-specifier as optional. */
18891 else
18892 qscope = cp_parser_nested_name_specifier_opt (parser,
18893 /*typename_keyword_p=*/false,
18894 /*check_dependency_p=*/true,
18895 /*type_p=*/false,
18896 /*is_declaration=*/true);
18897 if (!qscope)
18898 qscope = global_namespace;
18899 else if (UNSCOPED_ENUM_P (qscope))
18900 qscope = CP_TYPE_CONTEXT (qscope);
18902 if (access_declaration_p && cp_parser_error_occurred (parser))
18903 /* Something has already gone wrong; there's no need to parse
18904 further. Since an error has occurred, the return value of
18905 cp_parser_parse_definitely will be false, as required. */
18906 return cp_parser_parse_definitely (parser);
18908 token = cp_lexer_peek_token (parser->lexer);
18909 /* Parse the unqualified-id. */
18910 identifier = cp_parser_unqualified_id (parser,
18911 /*template_keyword_p=*/false,
18912 /*check_dependency_p=*/true,
18913 /*declarator_p=*/true,
18914 /*optional_p=*/false);
18916 if (access_declaration_p)
18918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18919 cp_parser_simulate_error (parser);
18920 if (!cp_parser_parse_definitely (parser))
18921 return false;
18923 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18925 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18926 if (cxx_dialect < cxx17
18927 && !in_system_header_at (ell->location))
18928 pedwarn (ell->location, 0,
18929 "pack expansion in using-declaration only available "
18930 "with -std=c++17 or -std=gnu++17");
18931 qscope = make_pack_expansion (qscope);
18934 /* The function we call to handle a using-declaration is different
18935 depending on what scope we are in. */
18936 if (qscope == error_mark_node || identifier == error_mark_node)
18938 else if (!identifier_p (identifier)
18939 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18940 /* [namespace.udecl]
18942 A using declaration shall not name a template-id. */
18943 error_at (token->location,
18944 "a template-id may not appear in a using-declaration");
18945 else
18947 if (at_class_scope_p ())
18949 /* Create the USING_DECL. */
18950 decl = do_class_using_decl (qscope, identifier);
18952 if (decl && typename_p)
18953 USING_DECL_TYPENAME_P (decl) = 1;
18955 if (check_for_bare_parameter_packs (decl))
18957 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18958 return false;
18960 else
18961 /* Add it to the list of members in this class. */
18962 finish_member_declaration (decl);
18964 else
18966 decl = cp_parser_lookup_name_simple (parser,
18967 identifier,
18968 token->location);
18969 if (decl == error_mark_node)
18970 cp_parser_name_lookup_error (parser, identifier,
18971 decl, NLE_NULL,
18972 token->location);
18973 else if (check_for_bare_parameter_packs (decl))
18975 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18976 return false;
18978 else if (!at_namespace_scope_p ())
18979 finish_local_using_decl (decl, qscope, identifier);
18980 else
18981 finish_namespace_using_decl (decl, qscope, identifier);
18985 if (!access_declaration_p
18986 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18988 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18989 if (cxx_dialect < cxx17)
18990 pedwarn (comma->location, 0,
18991 "comma-separated list in using-declaration only available "
18992 "with -std=c++17 or -std=gnu++17");
18993 goto again;
18996 /* Look for the final `;'. */
18997 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18999 if (access_declaration_p && errorcount == oldcount)
19000 warning_at (diag_token->location, OPT_Wdeprecated,
19001 "access declarations are deprecated "
19002 "in favour of using-declarations; "
19003 "suggestion: add the %<using%> keyword");
19005 return true;
19008 /* Parse an alias-declaration.
19010 alias-declaration:
19011 using identifier attribute-specifier-seq [opt] = type-id */
19013 static tree
19014 cp_parser_alias_declaration (cp_parser* parser)
19016 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19017 location_t id_location;
19018 cp_declarator *declarator;
19019 cp_decl_specifier_seq decl_specs;
19020 bool member_p;
19021 const char *saved_message = NULL;
19023 /* Look for the `using' keyword. */
19024 cp_token *using_token
19025 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19026 if (using_token == NULL)
19027 return error_mark_node;
19029 id_location = cp_lexer_peek_token (parser->lexer)->location;
19030 id = cp_parser_identifier (parser);
19031 if (id == error_mark_node)
19032 return error_mark_node;
19034 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19035 attributes = cp_parser_attributes_opt (parser);
19036 if (attributes == error_mark_node)
19037 return error_mark_node;
19039 cp_parser_require (parser, CPP_EQ, RT_EQ);
19041 if (cp_parser_error_occurred (parser))
19042 return error_mark_node;
19044 cp_parser_commit_to_tentative_parse (parser);
19046 /* Now we are going to parse the type-id of the declaration. */
19049 [dcl.type]/3 says:
19051 "A type-specifier-seq shall not define a class or enumeration
19052 unless it appears in the type-id of an alias-declaration (7.1.3) that
19053 is not the declaration of a template-declaration."
19055 In other words, if we currently are in an alias template, the
19056 type-id should not define a type.
19058 So let's set parser->type_definition_forbidden_message in that
19059 case; cp_parser_check_type_definition (called by
19060 cp_parser_class_specifier) will then emit an error if a type is
19061 defined in the type-id. */
19062 if (parser->num_template_parameter_lists)
19064 saved_message = parser->type_definition_forbidden_message;
19065 parser->type_definition_forbidden_message =
19066 G_("types may not be defined in alias template declarations");
19069 type = cp_parser_type_id (parser);
19071 /* Restore the error message if need be. */
19072 if (parser->num_template_parameter_lists)
19073 parser->type_definition_forbidden_message = saved_message;
19075 if (type == error_mark_node
19076 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19078 cp_parser_skip_to_end_of_block_or_statement (parser);
19079 return error_mark_node;
19082 /* A typedef-name can also be introduced by an alias-declaration. The
19083 identifier following the using keyword becomes a typedef-name. It has
19084 the same semantics as if it were introduced by the typedef
19085 specifier. In particular, it does not define a new type and it shall
19086 not appear in the type-id. */
19088 clear_decl_specs (&decl_specs);
19089 decl_specs.type = type;
19090 if (attributes != NULL_TREE)
19092 decl_specs.attributes = attributes;
19093 set_and_check_decl_spec_loc (&decl_specs,
19094 ds_attribute,
19095 attrs_token);
19097 set_and_check_decl_spec_loc (&decl_specs,
19098 ds_typedef,
19099 using_token);
19100 set_and_check_decl_spec_loc (&decl_specs,
19101 ds_alias,
19102 using_token);
19104 if (parser->num_template_parameter_lists
19105 && !cp_parser_check_template_parameters (parser,
19106 /*num_templates=*/0,
19107 /*template_id*/false,
19108 id_location,
19109 /*declarator=*/NULL))
19110 return error_mark_node;
19112 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19113 declarator->id_loc = id_location;
19115 member_p = at_class_scope_p ();
19116 if (member_p)
19117 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19118 NULL_TREE, attributes);
19119 else
19120 decl = start_decl (declarator, &decl_specs, 0,
19121 attributes, NULL_TREE, &pushed_scope);
19122 if (decl == error_mark_node)
19123 return decl;
19125 // Attach constraints to the alias declaration.
19126 if (flag_concepts && current_template_parms)
19128 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19129 tree constr = build_constraints (reqs, NULL_TREE);
19130 set_constraints (decl, constr);
19133 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19135 if (pushed_scope)
19136 pop_scope (pushed_scope);
19138 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19139 added into the symbol table; otherwise, return the TYPE_DECL. */
19140 if (DECL_LANG_SPECIFIC (decl)
19141 && DECL_TEMPLATE_INFO (decl)
19142 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19144 decl = DECL_TI_TEMPLATE (decl);
19145 if (member_p)
19146 check_member_template (decl);
19149 return decl;
19152 /* Parse a using-directive.
19154 using-directive:
19155 using namespace :: [opt] nested-name-specifier [opt]
19156 namespace-name ; */
19158 static void
19159 cp_parser_using_directive (cp_parser* parser)
19161 tree namespace_decl;
19162 tree attribs;
19164 /* Look for the `using' keyword. */
19165 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19166 /* And the `namespace' keyword. */
19167 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19168 /* Look for the optional `::' operator. */
19169 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19170 /* And the optional nested-name-specifier. */
19171 cp_parser_nested_name_specifier_opt (parser,
19172 /*typename_keyword_p=*/false,
19173 /*check_dependency_p=*/true,
19174 /*type_p=*/false,
19175 /*is_declaration=*/true);
19176 /* Get the namespace being used. */
19177 namespace_decl = cp_parser_namespace_name (parser);
19178 /* And any specified attributes. */
19179 attribs = cp_parser_attributes_opt (parser);
19181 /* Update the symbol table. */
19182 if (namespace_bindings_p ())
19183 finish_namespace_using_directive (namespace_decl, attribs);
19184 else
19185 finish_local_using_directive (namespace_decl, attribs);
19187 /* Look for the final `;'. */
19188 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19191 /* Parse an asm-definition.
19193 asm-definition:
19194 asm ( string-literal ) ;
19196 GNU Extension:
19198 asm-definition:
19199 asm volatile [opt] ( string-literal ) ;
19200 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19201 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19202 : asm-operand-list [opt] ) ;
19203 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19204 : asm-operand-list [opt]
19205 : asm-clobber-list [opt] ) ;
19206 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19207 : asm-clobber-list [opt]
19208 : asm-goto-list ) ; */
19210 static void
19211 cp_parser_asm_definition (cp_parser* parser)
19213 tree string;
19214 tree outputs = NULL_TREE;
19215 tree inputs = NULL_TREE;
19216 tree clobbers = NULL_TREE;
19217 tree labels = NULL_TREE;
19218 tree asm_stmt;
19219 bool volatile_p = false;
19220 bool extended_p = false;
19221 bool invalid_inputs_p = false;
19222 bool invalid_outputs_p = false;
19223 bool goto_p = false;
19224 required_token missing = RT_NONE;
19226 /* Look for the `asm' keyword. */
19227 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19229 if (parser->in_function_body
19230 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19232 error ("%<asm%> in %<constexpr%> function");
19233 cp_function_chain->invalid_constexpr = true;
19236 /* See if the next token is `volatile'. */
19237 if (cp_parser_allow_gnu_extensions_p (parser)
19238 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19240 /* Remember that we saw the `volatile' keyword. */
19241 volatile_p = true;
19242 /* Consume the token. */
19243 cp_lexer_consume_token (parser->lexer);
19245 if (cp_parser_allow_gnu_extensions_p (parser)
19246 && parser->in_function_body
19247 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19249 /* Remember that we saw the `goto' keyword. */
19250 goto_p = true;
19251 /* Consume the token. */
19252 cp_lexer_consume_token (parser->lexer);
19254 /* Look for the opening `('. */
19255 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19256 return;
19257 /* Look for the string. */
19258 string = cp_parser_string_literal (parser, false, false);
19259 if (string == error_mark_node)
19261 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19262 /*consume_paren=*/true);
19263 return;
19266 /* If we're allowing GNU extensions, check for the extended assembly
19267 syntax. Unfortunately, the `:' tokens need not be separated by
19268 a space in C, and so, for compatibility, we tolerate that here
19269 too. Doing that means that we have to treat the `::' operator as
19270 two `:' tokens. */
19271 if (cp_parser_allow_gnu_extensions_p (parser)
19272 && parser->in_function_body
19273 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19274 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19276 bool inputs_p = false;
19277 bool clobbers_p = false;
19278 bool labels_p = false;
19280 /* The extended syntax was used. */
19281 extended_p = true;
19283 /* Look for outputs. */
19284 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19286 /* Consume the `:'. */
19287 cp_lexer_consume_token (parser->lexer);
19288 /* Parse the output-operands. */
19289 if (cp_lexer_next_token_is_not (parser->lexer,
19290 CPP_COLON)
19291 && cp_lexer_next_token_is_not (parser->lexer,
19292 CPP_SCOPE)
19293 && cp_lexer_next_token_is_not (parser->lexer,
19294 CPP_CLOSE_PAREN)
19295 && !goto_p)
19297 outputs = cp_parser_asm_operand_list (parser);
19298 if (outputs == error_mark_node)
19299 invalid_outputs_p = true;
19302 /* If the next token is `::', there are no outputs, and the
19303 next token is the beginning of the inputs. */
19304 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19305 /* The inputs are coming next. */
19306 inputs_p = true;
19308 /* Look for inputs. */
19309 if (inputs_p
19310 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19312 /* Consume the `:' or `::'. */
19313 cp_lexer_consume_token (parser->lexer);
19314 /* Parse the output-operands. */
19315 if (cp_lexer_next_token_is_not (parser->lexer,
19316 CPP_COLON)
19317 && cp_lexer_next_token_is_not (parser->lexer,
19318 CPP_SCOPE)
19319 && cp_lexer_next_token_is_not (parser->lexer,
19320 CPP_CLOSE_PAREN))
19322 inputs = cp_parser_asm_operand_list (parser);
19323 if (inputs == error_mark_node)
19324 invalid_inputs_p = true;
19327 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19328 /* The clobbers are coming next. */
19329 clobbers_p = true;
19331 /* Look for clobbers. */
19332 if (clobbers_p
19333 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19335 clobbers_p = true;
19336 /* Consume the `:' or `::'. */
19337 cp_lexer_consume_token (parser->lexer);
19338 /* Parse the clobbers. */
19339 if (cp_lexer_next_token_is_not (parser->lexer,
19340 CPP_COLON)
19341 && cp_lexer_next_token_is_not (parser->lexer,
19342 CPP_CLOSE_PAREN))
19343 clobbers = cp_parser_asm_clobber_list (parser);
19345 else if (goto_p
19346 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19347 /* The labels are coming next. */
19348 labels_p = true;
19350 /* Look for labels. */
19351 if (labels_p
19352 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19354 labels_p = true;
19355 /* Consume the `:' or `::'. */
19356 cp_lexer_consume_token (parser->lexer);
19357 /* Parse the labels. */
19358 labels = cp_parser_asm_label_list (parser);
19361 if (goto_p && !labels_p)
19362 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19364 else if (goto_p)
19365 missing = RT_COLON_SCOPE;
19367 /* Look for the closing `)'. */
19368 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19369 missing ? missing : RT_CLOSE_PAREN))
19370 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19371 /*consume_paren=*/true);
19372 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19374 if (!invalid_inputs_p && !invalid_outputs_p)
19376 /* Create the ASM_EXPR. */
19377 if (parser->in_function_body)
19379 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19380 inputs, clobbers, labels);
19381 /* If the extended syntax was not used, mark the ASM_EXPR. */
19382 if (!extended_p)
19384 tree temp = asm_stmt;
19385 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19386 temp = TREE_OPERAND (temp, 0);
19388 ASM_INPUT_P (temp) = 1;
19391 else
19392 symtab->finalize_toplevel_asm (string);
19396 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19397 type that comes from the decl-specifier-seq. */
19399 static tree
19400 strip_declarator_types (tree type, cp_declarator *declarator)
19402 for (cp_declarator *d = declarator; d;)
19403 switch (d->kind)
19405 case cdk_id:
19406 case cdk_decomp:
19407 case cdk_error:
19408 d = NULL;
19409 break;
19411 default:
19412 if (TYPE_PTRMEMFUNC_P (type))
19413 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19414 type = TREE_TYPE (type);
19415 d = d->declarator;
19416 break;
19419 return type;
19422 /* Declarators [gram.dcl.decl] */
19424 /* Parse an init-declarator.
19426 init-declarator:
19427 declarator initializer [opt]
19429 GNU Extension:
19431 init-declarator:
19432 declarator asm-specification [opt] attributes [opt] initializer [opt]
19434 function-definition:
19435 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19436 function-body
19437 decl-specifier-seq [opt] declarator function-try-block
19439 GNU Extension:
19441 function-definition:
19442 __extension__ function-definition
19444 TM Extension:
19446 function-definition:
19447 decl-specifier-seq [opt] declarator function-transaction-block
19449 The DECL_SPECIFIERS apply to this declarator. Returns a
19450 representation of the entity declared. If MEMBER_P is TRUE, then
19451 this declarator appears in a class scope. The new DECL created by
19452 this declarator is returned.
19454 The CHECKS are access checks that should be performed once we know
19455 what entity is being declared (and, therefore, what classes have
19456 befriended it).
19458 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19459 for a function-definition here as well. If the declarator is a
19460 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19461 be TRUE upon return. By that point, the function-definition will
19462 have been completely parsed.
19464 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19465 is FALSE.
19467 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19468 parsed declaration if it is an uninitialized single declarator not followed
19469 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19470 if present, will not be consumed. If returned, this declarator will be
19471 created with SD_INITIALIZED but will not call cp_finish_decl.
19473 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19474 and there is an initializer, the pointed location_t is set to the
19475 location of the '=' or `(', or '{' in C++11 token introducing the
19476 initializer. */
19478 static tree
19479 cp_parser_init_declarator (cp_parser* parser,
19480 cp_decl_specifier_seq *decl_specifiers,
19481 vec<deferred_access_check, va_gc> *checks,
19482 bool function_definition_allowed_p,
19483 bool member_p,
19484 int declares_class_or_enum,
19485 bool* function_definition_p,
19486 tree* maybe_range_for_decl,
19487 location_t* init_loc,
19488 tree* auto_result)
19490 cp_token *token = NULL, *asm_spec_start_token = NULL,
19491 *attributes_start_token = NULL;
19492 cp_declarator *declarator;
19493 tree prefix_attributes;
19494 tree attributes = NULL;
19495 tree asm_specification;
19496 tree initializer;
19497 tree decl = NULL_TREE;
19498 tree scope;
19499 int is_initialized;
19500 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19501 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19502 "(...)". */
19503 enum cpp_ttype initialization_kind;
19504 bool is_direct_init = false;
19505 bool is_non_constant_init;
19506 int ctor_dtor_or_conv_p;
19507 bool friend_p = cp_parser_friend_p (decl_specifiers);
19508 tree pushed_scope = NULL_TREE;
19509 bool range_for_decl_p = false;
19510 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19511 location_t tmp_init_loc = UNKNOWN_LOCATION;
19513 /* Gather the attributes that were provided with the
19514 decl-specifiers. */
19515 prefix_attributes = decl_specifiers->attributes;
19517 /* Assume that this is not the declarator for a function
19518 definition. */
19519 if (function_definition_p)
19520 *function_definition_p = false;
19522 /* Default arguments are only permitted for function parameters. */
19523 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19524 parser->default_arg_ok_p = false;
19526 /* Defer access checks while parsing the declarator; we cannot know
19527 what names are accessible until we know what is being
19528 declared. */
19529 resume_deferring_access_checks ();
19531 token = cp_lexer_peek_token (parser->lexer);
19533 /* Parse the declarator. */
19534 declarator
19535 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19536 &ctor_dtor_or_conv_p,
19537 /*parenthesized_p=*/NULL,
19538 member_p, friend_p);
19539 /* Gather up the deferred checks. */
19540 stop_deferring_access_checks ();
19542 parser->default_arg_ok_p = saved_default_arg_ok_p;
19544 /* If the DECLARATOR was erroneous, there's no need to go
19545 further. */
19546 if (declarator == cp_error_declarator)
19547 return error_mark_node;
19549 /* Check that the number of template-parameter-lists is OK. */
19550 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19551 token->location))
19552 return error_mark_node;
19554 if (declares_class_or_enum & 2)
19555 cp_parser_check_for_definition_in_return_type (declarator,
19556 decl_specifiers->type,
19557 decl_specifiers->locations[ds_type_spec]);
19559 /* Figure out what scope the entity declared by the DECLARATOR is
19560 located in. `grokdeclarator' sometimes changes the scope, so
19561 we compute it now. */
19562 scope = get_scope_of_declarator (declarator);
19564 /* Perform any lookups in the declared type which were thought to be
19565 dependent, but are not in the scope of the declarator. */
19566 decl_specifiers->type
19567 = maybe_update_decl_type (decl_specifiers->type, scope);
19569 /* If we're allowing GNU extensions, look for an
19570 asm-specification. */
19571 if (cp_parser_allow_gnu_extensions_p (parser))
19573 /* Look for an asm-specification. */
19574 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19575 asm_specification = cp_parser_asm_specification_opt (parser);
19577 else
19578 asm_specification = NULL_TREE;
19580 /* Look for attributes. */
19581 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19582 attributes = cp_parser_attributes_opt (parser);
19584 /* Peek at the next token. */
19585 token = cp_lexer_peek_token (parser->lexer);
19587 bool bogus_implicit_tmpl = false;
19589 if (function_declarator_p (declarator))
19591 /* Handle C++17 deduction guides. */
19592 if (!decl_specifiers->type
19593 && ctor_dtor_or_conv_p <= 0
19594 && cxx_dialect >= cxx17)
19596 cp_declarator *id = get_id_declarator (declarator);
19597 tree name = id->u.id.unqualified_name;
19598 parser->scope = id->u.id.qualifying_scope;
19599 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19600 if (tmpl
19601 && (DECL_CLASS_TEMPLATE_P (tmpl)
19602 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19604 id->u.id.unqualified_name = dguide_name (tmpl);
19605 id->u.id.sfk = sfk_deduction_guide;
19606 ctor_dtor_or_conv_p = 1;
19610 /* Check to see if the token indicates the start of a
19611 function-definition. */
19612 if (cp_parser_token_starts_function_definition_p (token))
19614 if (!function_definition_allowed_p)
19616 /* If a function-definition should not appear here, issue an
19617 error message. */
19618 cp_parser_error (parser,
19619 "a function-definition is not allowed here");
19620 return error_mark_node;
19623 location_t func_brace_location
19624 = cp_lexer_peek_token (parser->lexer)->location;
19626 /* Neither attributes nor an asm-specification are allowed
19627 on a function-definition. */
19628 if (asm_specification)
19629 error_at (asm_spec_start_token->location,
19630 "an asm-specification is not allowed "
19631 "on a function-definition");
19632 if (attributes)
19633 error_at (attributes_start_token->location,
19634 "attributes are not allowed "
19635 "on a function-definition");
19636 /* This is a function-definition. */
19637 *function_definition_p = true;
19639 /* Parse the function definition. */
19640 if (member_p)
19641 decl = cp_parser_save_member_function_body (parser,
19642 decl_specifiers,
19643 declarator,
19644 prefix_attributes);
19645 else
19646 decl =
19647 (cp_parser_function_definition_from_specifiers_and_declarator
19648 (parser, decl_specifiers, prefix_attributes, declarator));
19650 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19652 /* This is where the prologue starts... */
19653 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19654 = func_brace_location;
19657 return decl;
19660 else if (parser->fully_implicit_function_template_p)
19662 /* A non-template declaration involving a function parameter list
19663 containing an implicit template parameter will be made into a
19664 template. If the resulting declaration is not going to be an
19665 actual function then finish the template scope here to prevent it.
19666 An error message will be issued once we have a decl to talk about.
19668 FIXME probably we should do type deduction rather than create an
19669 implicit template, but the standard currently doesn't allow it. */
19670 bogus_implicit_tmpl = true;
19671 finish_fully_implicit_template (parser, NULL_TREE);
19674 /* [dcl.dcl]
19676 Only in function declarations for constructors, destructors, type
19677 conversions, and deduction guides can the decl-specifier-seq be omitted.
19679 We explicitly postpone this check past the point where we handle
19680 function-definitions because we tolerate function-definitions
19681 that are missing their return types in some modes. */
19682 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19684 cp_parser_error (parser,
19685 "expected constructor, destructor, or type conversion");
19686 return error_mark_node;
19689 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19690 if (token->type == CPP_EQ
19691 || token->type == CPP_OPEN_PAREN
19692 || token->type == CPP_OPEN_BRACE)
19694 is_initialized = SD_INITIALIZED;
19695 initialization_kind = token->type;
19696 if (maybe_range_for_decl)
19697 *maybe_range_for_decl = error_mark_node;
19698 tmp_init_loc = token->location;
19699 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19700 *init_loc = tmp_init_loc;
19702 if (token->type == CPP_EQ
19703 && function_declarator_p (declarator))
19705 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19706 if (t2->keyword == RID_DEFAULT)
19707 is_initialized = SD_DEFAULTED;
19708 else if (t2->keyword == RID_DELETE)
19709 is_initialized = SD_DELETED;
19712 else
19714 /* If the init-declarator isn't initialized and isn't followed by a
19715 `,' or `;', it's not a valid init-declarator. */
19716 if (token->type != CPP_COMMA
19717 && token->type != CPP_SEMICOLON)
19719 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19720 range_for_decl_p = true;
19721 else
19723 if (!maybe_range_for_decl)
19724 cp_parser_error (parser, "expected initializer");
19725 return error_mark_node;
19728 is_initialized = SD_UNINITIALIZED;
19729 initialization_kind = CPP_EOF;
19732 /* Because start_decl has side-effects, we should only call it if we
19733 know we're going ahead. By this point, we know that we cannot
19734 possibly be looking at any other construct. */
19735 cp_parser_commit_to_tentative_parse (parser);
19737 /* Enter the newly declared entry in the symbol table. If we're
19738 processing a declaration in a class-specifier, we wait until
19739 after processing the initializer. */
19740 if (!member_p)
19742 if (parser->in_unbraced_linkage_specification_p)
19743 decl_specifiers->storage_class = sc_extern;
19744 decl = start_decl (declarator, decl_specifiers,
19745 range_for_decl_p? SD_INITIALIZED : is_initialized,
19746 attributes, prefix_attributes, &pushed_scope);
19747 cp_finalize_omp_declare_simd (parser, decl);
19748 cp_finalize_oacc_routine (parser, decl, false);
19749 /* Adjust location of decl if declarator->id_loc is more appropriate:
19750 set, and decl wasn't merged with another decl, in which case its
19751 location would be different from input_location, and more accurate. */
19752 if (DECL_P (decl)
19753 && declarator->id_loc != UNKNOWN_LOCATION
19754 && DECL_SOURCE_LOCATION (decl) == input_location)
19755 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19757 else if (scope)
19758 /* Enter the SCOPE. That way unqualified names appearing in the
19759 initializer will be looked up in SCOPE. */
19760 pushed_scope = push_scope (scope);
19762 /* Perform deferred access control checks, now that we know in which
19763 SCOPE the declared entity resides. */
19764 if (!member_p && decl)
19766 tree saved_current_function_decl = NULL_TREE;
19768 /* If the entity being declared is a function, pretend that we
19769 are in its scope. If it is a `friend', it may have access to
19770 things that would not otherwise be accessible. */
19771 if (TREE_CODE (decl) == FUNCTION_DECL)
19773 saved_current_function_decl = current_function_decl;
19774 current_function_decl = decl;
19777 /* Perform access checks for template parameters. */
19778 cp_parser_perform_template_parameter_access_checks (checks);
19780 /* Perform the access control checks for the declarator and the
19781 decl-specifiers. */
19782 perform_deferred_access_checks (tf_warning_or_error);
19784 /* Restore the saved value. */
19785 if (TREE_CODE (decl) == FUNCTION_DECL)
19786 current_function_decl = saved_current_function_decl;
19789 /* Parse the initializer. */
19790 initializer = NULL_TREE;
19791 is_direct_init = false;
19792 is_non_constant_init = true;
19793 if (is_initialized)
19795 if (function_declarator_p (declarator))
19797 if (initialization_kind == CPP_EQ)
19798 initializer = cp_parser_pure_specifier (parser);
19799 else
19801 /* If the declaration was erroneous, we don't really
19802 know what the user intended, so just silently
19803 consume the initializer. */
19804 if (decl != error_mark_node)
19805 error_at (tmp_init_loc, "initializer provided for function");
19806 cp_parser_skip_to_closing_parenthesis (parser,
19807 /*recovering=*/true,
19808 /*or_comma=*/false,
19809 /*consume_paren=*/true);
19812 else
19814 /* We want to record the extra mangling scope for in-class
19815 initializers of class members and initializers of static data
19816 member templates. The former involves deferring
19817 parsing of the initializer until end of class as with default
19818 arguments. So right here we only handle the latter. */
19819 if (!member_p && processing_template_decl && decl != error_mark_node)
19820 start_lambda_scope (decl);
19821 initializer = cp_parser_initializer (parser,
19822 &is_direct_init,
19823 &is_non_constant_init);
19824 if (!member_p && processing_template_decl && decl != error_mark_node)
19825 finish_lambda_scope ();
19826 if (initializer == error_mark_node)
19827 cp_parser_skip_to_end_of_statement (parser);
19831 /* The old parser allows attributes to appear after a parenthesized
19832 initializer. Mark Mitchell proposed removing this functionality
19833 on the GCC mailing lists on 2002-08-13. This parser accepts the
19834 attributes -- but ignores them. Made a permerror in GCC 8. */
19835 if (cp_parser_allow_gnu_extensions_p (parser)
19836 && initialization_kind == CPP_OPEN_PAREN
19837 && cp_parser_attributes_opt (parser)
19838 && permerror (input_location,
19839 "attributes after parenthesized initializer ignored"))
19841 static bool hint;
19842 if (flag_permissive && !hint)
19844 hint = true;
19845 inform (input_location,
19846 "this flexibility is deprecated and will be removed");
19850 /* And now complain about a non-function implicit template. */
19851 if (bogus_implicit_tmpl && decl != error_mark_node)
19852 error_at (DECL_SOURCE_LOCATION (decl),
19853 "non-function %qD declared as implicit template", decl);
19855 /* For an in-class declaration, use `grokfield' to create the
19856 declaration. */
19857 if (member_p)
19859 if (pushed_scope)
19861 pop_scope (pushed_scope);
19862 pushed_scope = NULL_TREE;
19864 decl = grokfield (declarator, decl_specifiers,
19865 initializer, !is_non_constant_init,
19866 /*asmspec=*/NULL_TREE,
19867 attr_chainon (attributes, prefix_attributes));
19868 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19869 cp_parser_save_default_args (parser, decl);
19870 cp_finalize_omp_declare_simd (parser, decl);
19871 cp_finalize_oacc_routine (parser, decl, false);
19874 /* Finish processing the declaration. But, skip member
19875 declarations. */
19876 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19878 cp_finish_decl (decl,
19879 initializer, !is_non_constant_init,
19880 asm_specification,
19881 /* If the initializer is in parentheses, then this is
19882 a direct-initialization, which means that an
19883 `explicit' constructor is OK. Otherwise, an
19884 `explicit' constructor cannot be used. */
19885 ((is_direct_init || !is_initialized)
19886 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19888 else if ((cxx_dialect != cxx98) && friend_p
19889 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19890 /* Core issue #226 (C++0x only): A default template-argument
19891 shall not be specified in a friend class template
19892 declaration. */
19893 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19894 /*is_partial=*/false, /*is_friend_decl=*/1);
19896 if (!friend_p && pushed_scope)
19897 pop_scope (pushed_scope);
19899 if (function_declarator_p (declarator)
19900 && parser->fully_implicit_function_template_p)
19902 if (member_p)
19903 decl = finish_fully_implicit_template (parser, decl);
19904 else
19905 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19908 if (auto_result && is_initialized && decl_specifiers->type
19909 && type_uses_auto (decl_specifiers->type))
19910 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19912 return decl;
19915 /* Parse a declarator.
19917 declarator:
19918 direct-declarator
19919 ptr-operator declarator
19921 abstract-declarator:
19922 ptr-operator abstract-declarator [opt]
19923 direct-abstract-declarator
19925 GNU Extensions:
19927 declarator:
19928 attributes [opt] direct-declarator
19929 attributes [opt] ptr-operator declarator
19931 abstract-declarator:
19932 attributes [opt] ptr-operator abstract-declarator [opt]
19933 attributes [opt] direct-abstract-declarator
19935 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19936 detect constructors, destructors, deduction guides, or conversion operators.
19937 It is set to -1 if the declarator is a name, and +1 if it is a
19938 function. Otherwise it is set to zero. Usually you just want to
19939 test for >0, but internally the negative value is used.
19941 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19942 a decl-specifier-seq unless it declares a constructor, destructor,
19943 or conversion. It might seem that we could check this condition in
19944 semantic analysis, rather than parsing, but that makes it difficult
19945 to handle something like `f()'. We want to notice that there are
19946 no decl-specifiers, and therefore realize that this is an
19947 expression, not a declaration.)
19949 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19950 the declarator is a direct-declarator of the form "(...)".
19952 MEMBER_P is true iff this declarator is a member-declarator.
19954 FRIEND_P is true iff this declarator is a friend. */
19956 static cp_declarator *
19957 cp_parser_declarator (cp_parser* parser,
19958 cp_parser_declarator_kind dcl_kind,
19959 int* ctor_dtor_or_conv_p,
19960 bool* parenthesized_p,
19961 bool member_p, bool friend_p)
19963 cp_declarator *declarator;
19964 enum tree_code code;
19965 cp_cv_quals cv_quals;
19966 tree class_type;
19967 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19969 /* Assume this is not a constructor, destructor, or type-conversion
19970 operator. */
19971 if (ctor_dtor_or_conv_p)
19972 *ctor_dtor_or_conv_p = 0;
19974 if (cp_parser_allow_gnu_extensions_p (parser))
19975 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19977 /* Check for the ptr-operator production. */
19978 cp_parser_parse_tentatively (parser);
19979 /* Parse the ptr-operator. */
19980 code = cp_parser_ptr_operator (parser,
19981 &class_type,
19982 &cv_quals,
19983 &std_attributes);
19985 /* If that worked, then we have a ptr-operator. */
19986 if (cp_parser_parse_definitely (parser))
19988 /* If a ptr-operator was found, then this declarator was not
19989 parenthesized. */
19990 if (parenthesized_p)
19991 *parenthesized_p = true;
19992 /* The dependent declarator is optional if we are parsing an
19993 abstract-declarator. */
19994 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19995 cp_parser_parse_tentatively (parser);
19997 /* Parse the dependent declarator. */
19998 declarator = cp_parser_declarator (parser, dcl_kind,
19999 /*ctor_dtor_or_conv_p=*/NULL,
20000 /*parenthesized_p=*/NULL,
20001 /*member_p=*/false,
20002 friend_p);
20004 /* If we are parsing an abstract-declarator, we must handle the
20005 case where the dependent declarator is absent. */
20006 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20007 && !cp_parser_parse_definitely (parser))
20008 declarator = NULL;
20010 declarator = cp_parser_make_indirect_declarator
20011 (code, class_type, cv_quals, declarator, std_attributes);
20013 /* Everything else is a direct-declarator. */
20014 else
20016 if (parenthesized_p)
20017 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20018 CPP_OPEN_PAREN);
20019 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20020 ctor_dtor_or_conv_p,
20021 member_p, friend_p);
20024 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20025 declarator->attributes = gnu_attributes;
20026 return declarator;
20029 /* Parse a direct-declarator or direct-abstract-declarator.
20031 direct-declarator:
20032 declarator-id
20033 direct-declarator ( parameter-declaration-clause )
20034 cv-qualifier-seq [opt]
20035 ref-qualifier [opt]
20036 exception-specification [opt]
20037 direct-declarator [ constant-expression [opt] ]
20038 ( declarator )
20040 direct-abstract-declarator:
20041 direct-abstract-declarator [opt]
20042 ( parameter-declaration-clause )
20043 cv-qualifier-seq [opt]
20044 ref-qualifier [opt]
20045 exception-specification [opt]
20046 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20047 ( abstract-declarator )
20049 Returns a representation of the declarator. DCL_KIND is
20050 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20051 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20052 we are parsing a direct-declarator. It is
20053 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20054 of ambiguity we prefer an abstract declarator, as per
20055 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20056 as for cp_parser_declarator. */
20058 static cp_declarator *
20059 cp_parser_direct_declarator (cp_parser* parser,
20060 cp_parser_declarator_kind dcl_kind,
20061 int* ctor_dtor_or_conv_p,
20062 bool member_p, bool friend_p)
20064 cp_token *token;
20065 cp_declarator *declarator = NULL;
20066 tree scope = NULL_TREE;
20067 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20068 bool saved_in_declarator_p = parser->in_declarator_p;
20069 bool first = true;
20070 tree pushed_scope = NULL_TREE;
20071 cp_token *open_paren = NULL, *close_paren = NULL;
20073 while (true)
20075 /* Peek at the next token. */
20076 token = cp_lexer_peek_token (parser->lexer);
20077 if (token->type == CPP_OPEN_PAREN)
20079 /* This is either a parameter-declaration-clause, or a
20080 parenthesized declarator. When we know we are parsing a
20081 named declarator, it must be a parenthesized declarator
20082 if FIRST is true. For instance, `(int)' is a
20083 parameter-declaration-clause, with an omitted
20084 direct-abstract-declarator. But `((*))', is a
20085 parenthesized abstract declarator. Finally, when T is a
20086 template parameter `(T)' is a
20087 parameter-declaration-clause, and not a parenthesized
20088 named declarator.
20090 We first try and parse a parameter-declaration-clause,
20091 and then try a nested declarator (if FIRST is true).
20093 It is not an error for it not to be a
20094 parameter-declaration-clause, even when FIRST is
20095 false. Consider,
20097 int i (int);
20098 int i (3);
20100 The first is the declaration of a function while the
20101 second is the definition of a variable, including its
20102 initializer.
20104 Having seen only the parenthesis, we cannot know which of
20105 these two alternatives should be selected. Even more
20106 complex are examples like:
20108 int i (int (a));
20109 int i (int (3));
20111 The former is a function-declaration; the latter is a
20112 variable initialization.
20114 Thus again, we try a parameter-declaration-clause, and if
20115 that fails, we back out and return. */
20117 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20119 tree params;
20120 bool is_declarator = false;
20122 open_paren = NULL;
20124 /* In a member-declarator, the only valid interpretation
20125 of a parenthesis is the start of a
20126 parameter-declaration-clause. (It is invalid to
20127 initialize a static data member with a parenthesized
20128 initializer; only the "=" form of initialization is
20129 permitted.) */
20130 if (!member_p)
20131 cp_parser_parse_tentatively (parser);
20133 /* Consume the `('. */
20134 matching_parens parens;
20135 parens.consume_open (parser);
20136 if (first)
20138 /* If this is going to be an abstract declarator, we're
20139 in a declarator and we can't have default args. */
20140 parser->default_arg_ok_p = false;
20141 parser->in_declarator_p = true;
20144 begin_scope (sk_function_parms, NULL_TREE);
20146 /* Parse the parameter-declaration-clause. */
20147 params = cp_parser_parameter_declaration_clause (parser);
20149 /* Consume the `)'. */
20150 parens.require_close (parser);
20152 /* If all went well, parse the cv-qualifier-seq,
20153 ref-qualifier and the exception-specification. */
20154 if (member_p || cp_parser_parse_definitely (parser))
20156 cp_cv_quals cv_quals;
20157 cp_virt_specifiers virt_specifiers;
20158 cp_ref_qualifier ref_qual;
20159 tree exception_specification;
20160 tree late_return;
20161 tree attrs;
20162 bool memfn = (member_p || (pushed_scope
20163 && CLASS_TYPE_P (pushed_scope)));
20165 is_declarator = true;
20167 if (ctor_dtor_or_conv_p)
20168 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20169 first = false;
20171 /* Parse the cv-qualifier-seq. */
20172 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20173 /* Parse the ref-qualifier. */
20174 ref_qual = cp_parser_ref_qualifier_opt (parser);
20175 /* Parse the tx-qualifier. */
20176 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20177 /* And the exception-specification. */
20178 exception_specification
20179 = cp_parser_exception_specification_opt (parser);
20181 attrs = cp_parser_std_attribute_spec_seq (parser);
20183 /* In here, we handle cases where attribute is used after
20184 the function declaration. For example:
20185 void func (int x) __attribute__((vector(..))); */
20186 tree gnu_attrs = NULL_TREE;
20187 tree requires_clause = NULL_TREE;
20188 late_return = (cp_parser_late_return_type_opt
20189 (parser, declarator, requires_clause,
20190 memfn ? cv_quals : -1));
20192 /* Parse the virt-specifier-seq. */
20193 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20195 /* Create the function-declarator. */
20196 declarator = make_call_declarator (declarator,
20197 params,
20198 cv_quals,
20199 virt_specifiers,
20200 ref_qual,
20201 tx_qual,
20202 exception_specification,
20203 late_return,
20204 requires_clause);
20205 declarator->std_attributes = attrs;
20206 declarator->attributes = gnu_attrs;
20207 /* Any subsequent parameter lists are to do with
20208 return type, so are not those of the declared
20209 function. */
20210 parser->default_arg_ok_p = false;
20213 /* Remove the function parms from scope. */
20214 pop_bindings_and_leave_scope ();
20216 if (is_declarator)
20217 /* Repeat the main loop. */
20218 continue;
20221 /* If this is the first, we can try a parenthesized
20222 declarator. */
20223 if (first)
20225 bool saved_in_type_id_in_expr_p;
20227 parser->default_arg_ok_p = saved_default_arg_ok_p;
20228 parser->in_declarator_p = saved_in_declarator_p;
20230 open_paren = token;
20231 /* Consume the `('. */
20232 matching_parens parens;
20233 parens.consume_open (parser);
20234 /* Parse the nested declarator. */
20235 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20236 parser->in_type_id_in_expr_p = true;
20237 declarator
20238 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20239 /*parenthesized_p=*/NULL,
20240 member_p, friend_p);
20241 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20242 first = false;
20243 /* Expect a `)'. */
20244 close_paren = cp_lexer_peek_token (parser->lexer);
20245 if (!parens.require_close (parser))
20246 declarator = cp_error_declarator;
20247 if (declarator == cp_error_declarator)
20248 break;
20250 goto handle_declarator;
20252 /* Otherwise, we must be done. */
20253 else
20254 break;
20256 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20257 && token->type == CPP_OPEN_SQUARE
20258 && !cp_next_tokens_can_be_attribute_p (parser))
20260 /* Parse an array-declarator. */
20261 tree bounds, attrs;
20263 if (ctor_dtor_or_conv_p)
20264 *ctor_dtor_or_conv_p = 0;
20266 open_paren = NULL;
20267 first = false;
20268 parser->default_arg_ok_p = false;
20269 parser->in_declarator_p = true;
20270 /* Consume the `['. */
20271 cp_lexer_consume_token (parser->lexer);
20272 /* Peek at the next token. */
20273 token = cp_lexer_peek_token (parser->lexer);
20274 /* If the next token is `]', then there is no
20275 constant-expression. */
20276 if (token->type != CPP_CLOSE_SQUARE)
20278 bool non_constant_p;
20279 bounds
20280 = cp_parser_constant_expression (parser,
20281 /*allow_non_constant=*/true,
20282 &non_constant_p);
20283 if (!non_constant_p)
20284 /* OK */;
20285 else if (error_operand_p (bounds))
20286 /* Already gave an error. */;
20287 else if (!parser->in_function_body
20288 || current_binding_level->kind == sk_function_parms)
20290 /* Normally, the array bound must be an integral constant
20291 expression. However, as an extension, we allow VLAs
20292 in function scopes as long as they aren't part of a
20293 parameter declaration. */
20294 cp_parser_error (parser,
20295 "array bound is not an integer constant");
20296 bounds = error_mark_node;
20298 else if (processing_template_decl
20299 && !type_dependent_expression_p (bounds))
20301 /* Remember this wasn't a constant-expression. */
20302 bounds = build_nop (TREE_TYPE (bounds), bounds);
20303 TREE_SIDE_EFFECTS (bounds) = 1;
20306 else
20307 bounds = NULL_TREE;
20308 /* Look for the closing `]'. */
20309 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20311 declarator = cp_error_declarator;
20312 break;
20315 attrs = cp_parser_std_attribute_spec_seq (parser);
20316 declarator = make_array_declarator (declarator, bounds);
20317 declarator->std_attributes = attrs;
20319 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20322 tree qualifying_scope;
20323 tree unqualified_name;
20324 tree attrs;
20325 special_function_kind sfk;
20326 bool abstract_ok;
20327 bool pack_expansion_p = false;
20328 cp_token *declarator_id_start_token;
20330 /* Parse a declarator-id */
20331 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20332 if (abstract_ok)
20334 cp_parser_parse_tentatively (parser);
20336 /* If we see an ellipsis, we should be looking at a
20337 parameter pack. */
20338 if (token->type == CPP_ELLIPSIS)
20340 /* Consume the `...' */
20341 cp_lexer_consume_token (parser->lexer);
20343 pack_expansion_p = true;
20347 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20348 unqualified_name
20349 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20350 qualifying_scope = parser->scope;
20351 if (abstract_ok)
20353 bool okay = false;
20355 if (!unqualified_name && pack_expansion_p)
20357 /* Check whether an error occurred. */
20358 okay = !cp_parser_error_occurred (parser);
20360 /* We already consumed the ellipsis to mark a
20361 parameter pack, but we have no way to report it,
20362 so abort the tentative parse. We will be exiting
20363 immediately anyway. */
20364 cp_parser_abort_tentative_parse (parser);
20366 else
20367 okay = cp_parser_parse_definitely (parser);
20369 if (!okay)
20370 unqualified_name = error_mark_node;
20371 else if (unqualified_name
20372 && (qualifying_scope
20373 || (!identifier_p (unqualified_name))))
20375 cp_parser_error (parser, "expected unqualified-id");
20376 unqualified_name = error_mark_node;
20380 if (!unqualified_name)
20381 return NULL;
20382 if (unqualified_name == error_mark_node)
20384 declarator = cp_error_declarator;
20385 pack_expansion_p = false;
20386 declarator->parameter_pack_p = false;
20387 break;
20390 attrs = cp_parser_std_attribute_spec_seq (parser);
20392 if (qualifying_scope && at_namespace_scope_p ()
20393 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20395 /* In the declaration of a member of a template class
20396 outside of the class itself, the SCOPE will sometimes
20397 be a TYPENAME_TYPE. For example, given:
20399 template <typename T>
20400 int S<T>::R::i = 3;
20402 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20403 this context, we must resolve S<T>::R to an ordinary
20404 type, rather than a typename type.
20406 The reason we normally avoid resolving TYPENAME_TYPEs
20407 is that a specialization of `S' might render
20408 `S<T>::R' not a type. However, if `S' is
20409 specialized, then this `i' will not be used, so there
20410 is no harm in resolving the types here. */
20411 tree type;
20413 /* Resolve the TYPENAME_TYPE. */
20414 type = resolve_typename_type (qualifying_scope,
20415 /*only_current_p=*/false);
20416 /* If that failed, the declarator is invalid. */
20417 if (TREE_CODE (type) == TYPENAME_TYPE)
20419 if (typedef_variant_p (type))
20420 error_at (declarator_id_start_token->location,
20421 "cannot define member of dependent typedef "
20422 "%qT", type);
20423 else
20424 error_at (declarator_id_start_token->location,
20425 "%<%T::%E%> is not a type",
20426 TYPE_CONTEXT (qualifying_scope),
20427 TYPE_IDENTIFIER (qualifying_scope));
20429 qualifying_scope = type;
20432 sfk = sfk_none;
20434 if (unqualified_name)
20436 tree class_type;
20438 if (qualifying_scope
20439 && CLASS_TYPE_P (qualifying_scope))
20440 class_type = qualifying_scope;
20441 else
20442 class_type = current_class_type;
20444 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20446 tree name_type = TREE_TYPE (unqualified_name);
20448 if (!class_type || !same_type_p (name_type, class_type))
20450 /* We do not attempt to print the declarator
20451 here because we do not have enough
20452 information about its original syntactic
20453 form. */
20454 cp_parser_error (parser, "invalid declarator");
20455 declarator = cp_error_declarator;
20456 break;
20458 else if (qualifying_scope
20459 && CLASSTYPE_USE_TEMPLATE (name_type))
20461 error_at (declarator_id_start_token->location,
20462 "invalid use of constructor as a template");
20463 inform (declarator_id_start_token->location,
20464 "use %<%T::%D%> instead of %<%T::%D%> to "
20465 "name the constructor in a qualified name",
20466 class_type,
20467 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20468 class_type, name_type);
20469 declarator = cp_error_declarator;
20470 break;
20472 unqualified_name = constructor_name (class_type);
20475 if (class_type)
20477 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20478 sfk = sfk_destructor;
20479 else if (identifier_p (unqualified_name)
20480 && IDENTIFIER_CONV_OP_P (unqualified_name))
20481 sfk = sfk_conversion;
20482 else if (/* There's no way to declare a constructor
20483 for an unnamed type, even if the type
20484 got a name for linkage purposes. */
20485 !TYPE_WAS_UNNAMED (class_type)
20486 /* Handle correctly (c++/19200):
20488 struct S {
20489 struct T{};
20490 friend void S(T);
20493 and also:
20495 namespace N {
20496 void S();
20499 struct S {
20500 friend void N::S();
20501 }; */
20502 && (!friend_p || class_type == qualifying_scope)
20503 && constructor_name_p (unqualified_name,
20504 class_type))
20505 sfk = sfk_constructor;
20506 else if (is_overloaded_fn (unqualified_name)
20507 && DECL_CONSTRUCTOR_P (get_first_fn
20508 (unqualified_name)))
20509 sfk = sfk_constructor;
20511 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20512 *ctor_dtor_or_conv_p = -1;
20515 declarator = make_id_declarator (qualifying_scope,
20516 unqualified_name,
20517 sfk);
20518 declarator->std_attributes = attrs;
20519 declarator->id_loc = token->location;
20520 declarator->parameter_pack_p = pack_expansion_p;
20522 if (pack_expansion_p)
20523 maybe_warn_variadic_templates ();
20526 handle_declarator:;
20527 scope = get_scope_of_declarator (declarator);
20528 if (scope)
20530 /* Any names that appear after the declarator-id for a
20531 member are looked up in the containing scope. */
20532 if (at_function_scope_p ())
20534 /* But declarations with qualified-ids can't appear in a
20535 function. */
20536 cp_parser_error (parser, "qualified-id in declaration");
20537 declarator = cp_error_declarator;
20538 break;
20540 pushed_scope = push_scope (scope);
20542 parser->in_declarator_p = true;
20543 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20544 || (declarator && declarator->kind == cdk_id))
20545 /* Default args are only allowed on function
20546 declarations. */
20547 parser->default_arg_ok_p = saved_default_arg_ok_p;
20548 else
20549 parser->default_arg_ok_p = false;
20551 first = false;
20553 /* We're done. */
20554 else
20555 break;
20558 /* For an abstract declarator, we might wind up with nothing at this
20559 point. That's an error; the declarator is not optional. */
20560 if (!declarator)
20561 cp_parser_error (parser, "expected declarator");
20562 else if (open_paren)
20564 /* Record overly parenthesized declarator so we can give a
20565 diagnostic about confusing decl/expr disambiguation. */
20566 if (declarator->kind == cdk_array)
20568 /* If the open and close parens are on different lines, this
20569 is probably a formatting thing, so ignore. */
20570 expanded_location open = expand_location (open_paren->location);
20571 expanded_location close = expand_location (close_paren->location);
20572 if (open.line != close.line || open.file != close.file)
20573 open_paren = NULL;
20575 if (open_paren)
20576 declarator->parenthesized = open_paren->location;
20579 /* If we entered a scope, we must exit it now. */
20580 if (pushed_scope)
20581 pop_scope (pushed_scope);
20583 parser->default_arg_ok_p = saved_default_arg_ok_p;
20584 parser->in_declarator_p = saved_in_declarator_p;
20586 return declarator;
20589 /* Parse a ptr-operator.
20591 ptr-operator:
20592 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20593 * cv-qualifier-seq [opt]
20595 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20596 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20598 GNU Extension:
20600 ptr-operator:
20601 & cv-qualifier-seq [opt]
20603 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20604 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20605 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20606 filled in with the TYPE containing the member. *CV_QUALS is
20607 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20608 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20609 Note that the tree codes returned by this function have nothing
20610 to do with the types of trees that will be eventually be created
20611 to represent the pointer or reference type being parsed. They are
20612 just constants with suggestive names. */
20613 static enum tree_code
20614 cp_parser_ptr_operator (cp_parser* parser,
20615 tree* type,
20616 cp_cv_quals *cv_quals,
20617 tree *attributes)
20619 enum tree_code code = ERROR_MARK;
20620 cp_token *token;
20621 tree attrs = NULL_TREE;
20623 /* Assume that it's not a pointer-to-member. */
20624 *type = NULL_TREE;
20625 /* And that there are no cv-qualifiers. */
20626 *cv_quals = TYPE_UNQUALIFIED;
20628 /* Peek at the next token. */
20629 token = cp_lexer_peek_token (parser->lexer);
20631 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20632 if (token->type == CPP_MULT)
20633 code = INDIRECT_REF;
20634 else if (token->type == CPP_AND)
20635 code = ADDR_EXPR;
20636 else if ((cxx_dialect != cxx98) &&
20637 token->type == CPP_AND_AND) /* C++0x only */
20638 code = NON_LVALUE_EXPR;
20640 if (code != ERROR_MARK)
20642 /* Consume the `*', `&' or `&&'. */
20643 cp_lexer_consume_token (parser->lexer);
20645 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20646 `&', if we are allowing GNU extensions. (The only qualifier
20647 that can legally appear after `&' is `restrict', but that is
20648 enforced during semantic analysis. */
20649 if (code == INDIRECT_REF
20650 || cp_parser_allow_gnu_extensions_p (parser))
20651 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20653 attrs = cp_parser_std_attribute_spec_seq (parser);
20654 if (attributes != NULL)
20655 *attributes = attrs;
20657 else
20659 /* Try the pointer-to-member case. */
20660 cp_parser_parse_tentatively (parser);
20661 /* Look for the optional `::' operator. */
20662 cp_parser_global_scope_opt (parser,
20663 /*current_scope_valid_p=*/false);
20664 /* Look for the nested-name specifier. */
20665 token = cp_lexer_peek_token (parser->lexer);
20666 cp_parser_nested_name_specifier (parser,
20667 /*typename_keyword_p=*/false,
20668 /*check_dependency_p=*/true,
20669 /*type_p=*/false,
20670 /*is_declaration=*/false);
20671 /* If we found it, and the next token is a `*', then we are
20672 indeed looking at a pointer-to-member operator. */
20673 if (!cp_parser_error_occurred (parser)
20674 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20676 /* Indicate that the `*' operator was used. */
20677 code = INDIRECT_REF;
20679 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20680 error_at (token->location, "%qD is a namespace", parser->scope);
20681 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20682 error_at (token->location, "cannot form pointer to member of "
20683 "non-class %q#T", parser->scope);
20684 else
20686 /* The type of which the member is a member is given by the
20687 current SCOPE. */
20688 *type = parser->scope;
20689 /* The next name will not be qualified. */
20690 parser->scope = NULL_TREE;
20691 parser->qualifying_scope = NULL_TREE;
20692 parser->object_scope = NULL_TREE;
20693 /* Look for optional c++11 attributes. */
20694 attrs = cp_parser_std_attribute_spec_seq (parser);
20695 if (attributes != NULL)
20696 *attributes = attrs;
20697 /* Look for the optional cv-qualifier-seq. */
20698 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20701 /* If that didn't work we don't have a ptr-operator. */
20702 if (!cp_parser_parse_definitely (parser))
20703 cp_parser_error (parser, "expected ptr-operator");
20706 return code;
20709 /* Parse an (optional) cv-qualifier-seq.
20711 cv-qualifier-seq:
20712 cv-qualifier cv-qualifier-seq [opt]
20714 cv-qualifier:
20715 const
20716 volatile
20718 GNU Extension:
20720 cv-qualifier:
20721 __restrict__
20723 Returns a bitmask representing the cv-qualifiers. */
20725 static cp_cv_quals
20726 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20728 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20730 while (true)
20732 cp_token *token;
20733 cp_cv_quals cv_qualifier;
20735 /* Peek at the next token. */
20736 token = cp_lexer_peek_token (parser->lexer);
20737 /* See if it's a cv-qualifier. */
20738 switch (token->keyword)
20740 case RID_CONST:
20741 cv_qualifier = TYPE_QUAL_CONST;
20742 break;
20744 case RID_VOLATILE:
20745 cv_qualifier = TYPE_QUAL_VOLATILE;
20746 break;
20748 case RID_RESTRICT:
20749 cv_qualifier = TYPE_QUAL_RESTRICT;
20750 break;
20752 default:
20753 cv_qualifier = TYPE_UNQUALIFIED;
20754 break;
20757 if (!cv_qualifier)
20758 break;
20760 if (cv_quals & cv_qualifier)
20762 gcc_rich_location richloc (token->location);
20763 richloc.add_fixit_remove ();
20764 error_at (&richloc, "duplicate cv-qualifier");
20765 cp_lexer_purge_token (parser->lexer);
20767 else
20769 cp_lexer_consume_token (parser->lexer);
20770 cv_quals |= cv_qualifier;
20774 return cv_quals;
20777 /* Parse an (optional) ref-qualifier
20779 ref-qualifier:
20783 Returns cp_ref_qualifier representing ref-qualifier. */
20785 static cp_ref_qualifier
20786 cp_parser_ref_qualifier_opt (cp_parser* parser)
20788 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20790 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20791 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20792 return ref_qual;
20794 while (true)
20796 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20797 cp_token *token = cp_lexer_peek_token (parser->lexer);
20799 switch (token->type)
20801 case CPP_AND:
20802 curr_ref_qual = REF_QUAL_LVALUE;
20803 break;
20805 case CPP_AND_AND:
20806 curr_ref_qual = REF_QUAL_RVALUE;
20807 break;
20809 default:
20810 curr_ref_qual = REF_QUAL_NONE;
20811 break;
20814 if (!curr_ref_qual)
20815 break;
20816 else if (ref_qual)
20818 error_at (token->location, "multiple ref-qualifiers");
20819 cp_lexer_purge_token (parser->lexer);
20821 else
20823 ref_qual = curr_ref_qual;
20824 cp_lexer_consume_token (parser->lexer);
20828 return ref_qual;
20831 /* Parse an optional tx-qualifier.
20833 tx-qualifier:
20834 transaction_safe
20835 transaction_safe_dynamic */
20837 static tree
20838 cp_parser_tx_qualifier_opt (cp_parser *parser)
20840 cp_token *token = cp_lexer_peek_token (parser->lexer);
20841 if (token->type == CPP_NAME)
20843 tree name = token->u.value;
20844 const char *p = IDENTIFIER_POINTER (name);
20845 const int len = strlen ("transaction_safe");
20846 if (!strncmp (p, "transaction_safe", len))
20848 p += len;
20849 if (*p == '\0'
20850 || !strcmp (p, "_dynamic"))
20852 cp_lexer_consume_token (parser->lexer);
20853 if (!flag_tm)
20855 error ("%qE requires %<-fgnu-tm%>", name);
20856 return NULL_TREE;
20858 else
20859 return name;
20863 return NULL_TREE;
20866 /* Parse an (optional) virt-specifier-seq.
20868 virt-specifier-seq:
20869 virt-specifier virt-specifier-seq [opt]
20871 virt-specifier:
20872 override
20873 final
20875 Returns a bitmask representing the virt-specifiers. */
20877 static cp_virt_specifiers
20878 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20880 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20882 while (true)
20884 cp_token *token;
20885 cp_virt_specifiers virt_specifier;
20887 /* Peek at the next token. */
20888 token = cp_lexer_peek_token (parser->lexer);
20889 /* See if it's a virt-specifier-qualifier. */
20890 if (token->type != CPP_NAME)
20891 break;
20892 if (id_equal (token->u.value, "override"))
20894 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20895 virt_specifier = VIRT_SPEC_OVERRIDE;
20897 else if (id_equal (token->u.value, "final"))
20899 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20900 virt_specifier = VIRT_SPEC_FINAL;
20902 else if (id_equal (token->u.value, "__final"))
20904 virt_specifier = VIRT_SPEC_FINAL;
20906 else
20907 break;
20909 if (virt_specifiers & virt_specifier)
20911 gcc_rich_location richloc (token->location);
20912 richloc.add_fixit_remove ();
20913 error_at (&richloc, "duplicate virt-specifier");
20914 cp_lexer_purge_token (parser->lexer);
20916 else
20918 cp_lexer_consume_token (parser->lexer);
20919 virt_specifiers |= virt_specifier;
20922 return virt_specifiers;
20925 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20926 is in scope even though it isn't real. */
20928 void
20929 inject_this_parameter (tree ctype, cp_cv_quals quals)
20931 tree this_parm;
20933 if (current_class_ptr)
20935 /* We don't clear this between NSDMIs. Is it already what we want? */
20936 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20937 if (DECL_P (current_class_ptr)
20938 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20939 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20940 && cp_type_quals (type) == quals)
20941 return;
20944 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20945 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20946 current_class_ptr = NULL_TREE;
20947 current_class_ref
20948 = cp_build_fold_indirect_ref (this_parm);
20949 current_class_ptr = this_parm;
20952 /* Return true iff our current scope is a non-static data member
20953 initializer. */
20955 bool
20956 parsing_nsdmi (void)
20958 /* We recognize NSDMI context by the context-less 'this' pointer set up
20959 by the function above. */
20960 if (current_class_ptr
20961 && TREE_CODE (current_class_ptr) == PARM_DECL
20962 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20963 return true;
20964 return false;
20967 /* Parse a late-specified return type, if any. This is not a separate
20968 non-terminal, but part of a function declarator, which looks like
20970 -> trailing-type-specifier-seq abstract-declarator(opt)
20972 Returns the type indicated by the type-id.
20974 In addition to this, parse any queued up #pragma omp declare simd
20975 clauses, and #pragma acc routine clauses.
20977 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20978 function. */
20980 static tree
20981 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20982 tree& requires_clause, cp_cv_quals quals)
20984 cp_token *token;
20985 tree type = NULL_TREE;
20986 bool declare_simd_p = (parser->omp_declare_simd
20987 && declarator
20988 && declarator->kind == cdk_id);
20990 bool oacc_routine_p = (parser->oacc_routine
20991 && declarator
20992 && declarator->kind == cdk_id);
20994 /* Peek at the next token. */
20995 token = cp_lexer_peek_token (parser->lexer);
20996 /* A late-specified return type is indicated by an initial '->'. */
20997 if (token->type != CPP_DEREF
20998 && token->keyword != RID_REQUIRES
20999 && !(token->type == CPP_NAME
21000 && token->u.value == ridpointers[RID_REQUIRES])
21001 && !(declare_simd_p || oacc_routine_p))
21002 return NULL_TREE;
21004 tree save_ccp = current_class_ptr;
21005 tree save_ccr = current_class_ref;
21006 if (quals >= 0)
21008 /* DR 1207: 'this' is in scope in the trailing return type. */
21009 inject_this_parameter (current_class_type, quals);
21012 if (token->type == CPP_DEREF)
21014 /* Consume the ->. */
21015 cp_lexer_consume_token (parser->lexer);
21017 type = cp_parser_trailing_type_id (parser);
21020 /* Function declarations may be followed by a trailing
21021 requires-clause. */
21022 requires_clause = cp_parser_requires_clause_opt (parser);
21024 if (declare_simd_p)
21025 declarator->attributes
21026 = cp_parser_late_parsing_omp_declare_simd (parser,
21027 declarator->attributes);
21028 if (oacc_routine_p)
21029 declarator->attributes
21030 = cp_parser_late_parsing_oacc_routine (parser,
21031 declarator->attributes);
21033 if (quals >= 0)
21035 current_class_ptr = save_ccp;
21036 current_class_ref = save_ccr;
21039 return type;
21042 /* Parse a declarator-id.
21044 declarator-id:
21045 id-expression
21046 :: [opt] nested-name-specifier [opt] type-name
21048 In the `id-expression' case, the value returned is as for
21049 cp_parser_id_expression if the id-expression was an unqualified-id.
21050 If the id-expression was a qualified-id, then a SCOPE_REF is
21051 returned. The first operand is the scope (either a NAMESPACE_DECL
21052 or TREE_TYPE), but the second is still just a representation of an
21053 unqualified-id. */
21055 static tree
21056 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21058 tree id;
21059 /* The expression must be an id-expression. Assume that qualified
21060 names are the names of types so that:
21062 template <class T>
21063 int S<T>::R::i = 3;
21065 will work; we must treat `S<T>::R' as the name of a type.
21066 Similarly, assume that qualified names are templates, where
21067 required, so that:
21069 template <class T>
21070 int S<T>::R<T>::i = 3;
21072 will work, too. */
21073 id = cp_parser_id_expression (parser,
21074 /*template_keyword_p=*/false,
21075 /*check_dependency_p=*/false,
21076 /*template_p=*/NULL,
21077 /*declarator_p=*/true,
21078 optional_p);
21079 if (id && BASELINK_P (id))
21080 id = BASELINK_FUNCTIONS (id);
21081 return id;
21084 /* Parse a type-id.
21086 type-id:
21087 type-specifier-seq abstract-declarator [opt]
21089 Returns the TYPE specified. */
21091 static tree
21092 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21093 bool is_trailing_return)
21095 cp_decl_specifier_seq type_specifier_seq;
21096 cp_declarator *abstract_declarator;
21098 /* Parse the type-specifier-seq. */
21099 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21100 is_trailing_return,
21101 &type_specifier_seq);
21102 if (is_template_arg && type_specifier_seq.type
21103 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21104 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21105 /* A bare template name as a template argument is a template template
21106 argument, not a placeholder, so fail parsing it as a type argument. */
21108 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21109 cp_parser_simulate_error (parser);
21110 return error_mark_node;
21112 if (type_specifier_seq.type == error_mark_node)
21113 return error_mark_node;
21115 /* There might or might not be an abstract declarator. */
21116 cp_parser_parse_tentatively (parser);
21117 /* Look for the declarator. */
21118 abstract_declarator
21119 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21120 /*parenthesized_p=*/NULL,
21121 /*member_p=*/false,
21122 /*friend_p=*/false);
21123 /* Check to see if there really was a declarator. */
21124 if (!cp_parser_parse_definitely (parser))
21125 abstract_declarator = NULL;
21127 if (type_specifier_seq.type
21128 /* The concepts TS allows 'auto' as a type-id. */
21129 && (!flag_concepts || parser->in_type_id_in_expr_p)
21130 /* None of the valid uses of 'auto' in C++14 involve the type-id
21131 nonterminal, but it is valid in a trailing-return-type. */
21132 && !(cxx_dialect >= cxx14 && is_trailing_return))
21133 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21135 /* A type-id with type 'auto' is only ok if the abstract declarator
21136 is a function declarator with a late-specified return type.
21138 A type-id with 'auto' is also valid in a trailing-return-type
21139 in a compound-requirement. */
21140 if (abstract_declarator
21141 && abstract_declarator->kind == cdk_function
21142 && abstract_declarator->u.function.late_return_type)
21143 /* OK */;
21144 else if (parser->in_result_type_constraint_p)
21145 /* OK */;
21146 else
21148 location_t loc = type_specifier_seq.locations[ds_type_spec];
21149 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21151 error_at (loc, "missing template arguments after %qT",
21152 auto_node);
21153 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21154 tmpl);
21156 else
21157 error_at (loc, "invalid use of %qT", auto_node);
21158 return error_mark_node;
21162 return groktypename (&type_specifier_seq, abstract_declarator,
21163 is_template_arg);
21166 static tree
21167 cp_parser_type_id (cp_parser *parser)
21169 return cp_parser_type_id_1 (parser, false, false);
21172 static tree
21173 cp_parser_template_type_arg (cp_parser *parser)
21175 tree r;
21176 const char *saved_message = parser->type_definition_forbidden_message;
21177 parser->type_definition_forbidden_message
21178 = G_("types may not be defined in template arguments");
21179 r = cp_parser_type_id_1 (parser, true, false);
21180 parser->type_definition_forbidden_message = saved_message;
21181 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21183 error ("invalid use of %<auto%> in template argument");
21184 r = error_mark_node;
21186 return r;
21189 static tree
21190 cp_parser_trailing_type_id (cp_parser *parser)
21192 return cp_parser_type_id_1 (parser, false, true);
21195 /* Parse a type-specifier-seq.
21197 type-specifier-seq:
21198 type-specifier type-specifier-seq [opt]
21200 GNU extension:
21202 type-specifier-seq:
21203 attributes type-specifier-seq [opt]
21205 If IS_DECLARATION is true, we are at the start of a "condition" or
21206 exception-declaration, so we might be followed by a declarator-id.
21208 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21209 i.e. we've just seen "->".
21211 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21213 static void
21214 cp_parser_type_specifier_seq (cp_parser* parser,
21215 bool is_declaration,
21216 bool is_trailing_return,
21217 cp_decl_specifier_seq *type_specifier_seq)
21219 bool seen_type_specifier = false;
21220 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21221 cp_token *start_token = NULL;
21223 /* Clear the TYPE_SPECIFIER_SEQ. */
21224 clear_decl_specs (type_specifier_seq);
21226 /* In the context of a trailing return type, enum E { } is an
21227 elaborated-type-specifier followed by a function-body, not an
21228 enum-specifier. */
21229 if (is_trailing_return)
21230 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21232 /* Parse the type-specifiers and attributes. */
21233 while (true)
21235 tree type_specifier;
21236 bool is_cv_qualifier;
21238 /* Check for attributes first. */
21239 if (cp_next_tokens_can_be_attribute_p (parser))
21241 type_specifier_seq->attributes
21242 = attr_chainon (type_specifier_seq->attributes,
21243 cp_parser_attributes_opt (parser));
21244 continue;
21247 /* record the token of the beginning of the type specifier seq,
21248 for error reporting purposes*/
21249 if (!start_token)
21250 start_token = cp_lexer_peek_token (parser->lexer);
21252 /* Look for the type-specifier. */
21253 type_specifier = cp_parser_type_specifier (parser,
21254 flags,
21255 type_specifier_seq,
21256 /*is_declaration=*/false,
21257 NULL,
21258 &is_cv_qualifier);
21259 if (!type_specifier)
21261 /* If the first type-specifier could not be found, this is not a
21262 type-specifier-seq at all. */
21263 if (!seen_type_specifier)
21265 /* Set in_declarator_p to avoid skipping to the semicolon. */
21266 int in_decl = parser->in_declarator_p;
21267 parser->in_declarator_p = true;
21269 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21270 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21271 cp_parser_error (parser, "expected type-specifier");
21273 parser->in_declarator_p = in_decl;
21275 type_specifier_seq->type = error_mark_node;
21276 return;
21278 /* If subsequent type-specifiers could not be found, the
21279 type-specifier-seq is complete. */
21280 break;
21283 seen_type_specifier = true;
21284 /* The standard says that a condition can be:
21286 type-specifier-seq declarator = assignment-expression
21288 However, given:
21290 struct S {};
21291 if (int S = ...)
21293 we should treat the "S" as a declarator, not as a
21294 type-specifier. The standard doesn't say that explicitly for
21295 type-specifier-seq, but it does say that for
21296 decl-specifier-seq in an ordinary declaration. Perhaps it
21297 would be clearer just to allow a decl-specifier-seq here, and
21298 then add a semantic restriction that if any decl-specifiers
21299 that are not type-specifiers appear, the program is invalid. */
21300 if (is_declaration && !is_cv_qualifier)
21301 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21305 /* Return whether the function currently being declared has an associated
21306 template parameter list. */
21308 static bool
21309 function_being_declared_is_template_p (cp_parser* parser)
21311 if (!current_template_parms || processing_template_parmlist)
21312 return false;
21314 if (parser->implicit_template_scope)
21315 return true;
21317 if (at_class_scope_p ()
21318 && TYPE_BEING_DEFINED (current_class_type))
21319 return parser->num_template_parameter_lists != 0;
21321 return ((int) parser->num_template_parameter_lists > template_class_depth
21322 (current_class_type));
21325 /* Parse a parameter-declaration-clause.
21327 parameter-declaration-clause:
21328 parameter-declaration-list [opt] ... [opt]
21329 parameter-declaration-list , ...
21331 Returns a representation for the parameter declarations. A return
21332 value of NULL indicates a parameter-declaration-clause consisting
21333 only of an ellipsis. */
21335 static tree
21336 cp_parser_parameter_declaration_clause (cp_parser* parser)
21338 tree parameters;
21339 cp_token *token;
21340 bool ellipsis_p;
21342 temp_override<bool> cleanup
21343 (parser->auto_is_implicit_function_template_parm_p);
21345 if (!processing_specialization
21346 && !processing_template_parmlist
21347 && !processing_explicit_instantiation
21348 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21349 actual function or a random abstract declarator. */
21350 && parser->default_arg_ok_p)
21351 if (!current_function_decl
21352 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21353 parser->auto_is_implicit_function_template_parm_p = true;
21355 /* Peek at the next token. */
21356 token = cp_lexer_peek_token (parser->lexer);
21357 /* Check for trivial parameter-declaration-clauses. */
21358 if (token->type == CPP_ELLIPSIS)
21360 /* Consume the `...' token. */
21361 cp_lexer_consume_token (parser->lexer);
21362 return NULL_TREE;
21364 else if (token->type == CPP_CLOSE_PAREN)
21365 /* There are no parameters. */
21367 #ifdef SYSTEM_IMPLICIT_EXTERN_C
21368 if (in_system_header_at (input_location)
21369 && current_class_type == NULL
21370 && current_lang_name == lang_name_c)
21371 return NULL_TREE;
21372 else
21373 #endif
21374 return void_list_node;
21376 /* Check for `(void)', too, which is a special case. */
21377 else if (token->keyword == RID_VOID
21378 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21379 == CPP_CLOSE_PAREN))
21381 /* Consume the `void' token. */
21382 cp_lexer_consume_token (parser->lexer);
21383 /* There are no parameters. */
21384 return void_list_node;
21387 /* Parse the parameter-declaration-list. */
21388 parameters = cp_parser_parameter_declaration_list (parser);
21389 /* If a parse error occurred while parsing the
21390 parameter-declaration-list, then the entire
21391 parameter-declaration-clause is erroneous. */
21392 if (parameters == error_mark_node)
21393 return NULL_TREE;
21395 /* Peek at the next token. */
21396 token = cp_lexer_peek_token (parser->lexer);
21397 /* If it's a `,', the clause should terminate with an ellipsis. */
21398 if (token->type == CPP_COMMA)
21400 /* Consume the `,'. */
21401 cp_lexer_consume_token (parser->lexer);
21402 /* Expect an ellipsis. */
21403 ellipsis_p
21404 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21406 /* It might also be `...' if the optional trailing `,' was
21407 omitted. */
21408 else if (token->type == CPP_ELLIPSIS)
21410 /* Consume the `...' token. */
21411 cp_lexer_consume_token (parser->lexer);
21412 /* And remember that we saw it. */
21413 ellipsis_p = true;
21415 else
21416 ellipsis_p = false;
21418 /* Finish the parameter list. */
21419 if (!ellipsis_p)
21420 parameters = chainon (parameters, void_list_node);
21422 return parameters;
21425 /* Parse a parameter-declaration-list.
21427 parameter-declaration-list:
21428 parameter-declaration
21429 parameter-declaration-list , parameter-declaration
21431 Returns a representation of the parameter-declaration-list, as for
21432 cp_parser_parameter_declaration_clause. However, the
21433 `void_list_node' is never appended to the list. */
21435 static tree
21436 cp_parser_parameter_declaration_list (cp_parser* parser)
21438 tree parameters = NULL_TREE;
21439 tree *tail = &parameters;
21440 bool saved_in_unbraced_linkage_specification_p;
21441 int index = 0;
21443 /* The special considerations that apply to a function within an
21444 unbraced linkage specifications do not apply to the parameters
21445 to the function. */
21446 saved_in_unbraced_linkage_specification_p
21447 = parser->in_unbraced_linkage_specification_p;
21448 parser->in_unbraced_linkage_specification_p = false;
21450 /* Look for more parameters. */
21451 while (true)
21453 cp_parameter_declarator *parameter;
21454 tree decl = error_mark_node;
21455 bool parenthesized_p = false;
21457 /* Parse the parameter. */
21458 parameter
21459 = cp_parser_parameter_declaration (parser,
21460 /*template_parm_p=*/false,
21461 &parenthesized_p);
21463 /* We don't know yet if the enclosing context is deprecated, so wait
21464 and warn in grokparms if appropriate. */
21465 deprecated_state = DEPRECATED_SUPPRESS;
21467 if (parameter)
21469 decl = grokdeclarator (parameter->declarator,
21470 &parameter->decl_specifiers,
21471 PARM,
21472 parameter->default_argument != NULL_TREE,
21473 &parameter->decl_specifiers.attributes);
21474 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21475 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21478 deprecated_state = DEPRECATED_NORMAL;
21480 /* If a parse error occurred parsing the parameter declaration,
21481 then the entire parameter-declaration-list is erroneous. */
21482 if (decl == error_mark_node)
21484 parameters = error_mark_node;
21485 break;
21488 if (parameter->decl_specifiers.attributes)
21489 cplus_decl_attributes (&decl,
21490 parameter->decl_specifiers.attributes,
21492 if (DECL_NAME (decl))
21493 decl = pushdecl (decl);
21495 if (decl != error_mark_node)
21497 retrofit_lang_decl (decl);
21498 DECL_PARM_INDEX (decl) = ++index;
21499 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21502 /* Add the new parameter to the list. */
21503 *tail = build_tree_list (parameter->default_argument, decl);
21504 tail = &TREE_CHAIN (*tail);
21506 /* Peek at the next token. */
21507 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21508 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21509 /* These are for Objective-C++ */
21510 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21511 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21512 /* The parameter-declaration-list is complete. */
21513 break;
21514 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21516 cp_token *token;
21518 /* Peek at the next token. */
21519 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21520 /* If it's an ellipsis, then the list is complete. */
21521 if (token->type == CPP_ELLIPSIS)
21522 break;
21523 /* Otherwise, there must be more parameters. Consume the
21524 `,'. */
21525 cp_lexer_consume_token (parser->lexer);
21526 /* When parsing something like:
21528 int i(float f, double d)
21530 we can tell after seeing the declaration for "f" that we
21531 are not looking at an initialization of a variable "i",
21532 but rather at the declaration of a function "i".
21534 Due to the fact that the parsing of template arguments
21535 (as specified to a template-id) requires backtracking we
21536 cannot use this technique when inside a template argument
21537 list. */
21538 if (!parser->in_template_argument_list_p
21539 && !parser->in_type_id_in_expr_p
21540 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21541 /* However, a parameter-declaration of the form
21542 "float(f)" (which is a valid declaration of a
21543 parameter "f") can also be interpreted as an
21544 expression (the conversion of "f" to "float"). */
21545 && !parenthesized_p)
21546 cp_parser_commit_to_tentative_parse (parser);
21548 else
21550 cp_parser_error (parser, "expected %<,%> or %<...%>");
21551 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21552 cp_parser_skip_to_closing_parenthesis (parser,
21553 /*recovering=*/true,
21554 /*or_comma=*/false,
21555 /*consume_paren=*/false);
21556 break;
21560 parser->in_unbraced_linkage_specification_p
21561 = saved_in_unbraced_linkage_specification_p;
21563 /* Reset implicit_template_scope if we are about to leave the function
21564 parameter list that introduced it. Note that for out-of-line member
21565 definitions, there will be one or more class scopes before we get to
21566 the template parameter scope. */
21568 if (cp_binding_level *its = parser->implicit_template_scope)
21569 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21571 while (maybe_its->kind == sk_class)
21572 maybe_its = maybe_its->level_chain;
21573 if (maybe_its == its)
21575 parser->implicit_template_parms = 0;
21576 parser->implicit_template_scope = 0;
21580 return parameters;
21583 /* Parse a parameter declaration.
21585 parameter-declaration:
21586 decl-specifier-seq ... [opt] declarator
21587 decl-specifier-seq declarator = assignment-expression
21588 decl-specifier-seq ... [opt] abstract-declarator [opt]
21589 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21591 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21592 declares a template parameter. (In that case, a non-nested `>'
21593 token encountered during the parsing of the assignment-expression
21594 is not interpreted as a greater-than operator.)
21596 Returns a representation of the parameter, or NULL if an error
21597 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21598 true iff the declarator is of the form "(p)". */
21600 static cp_parameter_declarator *
21601 cp_parser_parameter_declaration (cp_parser *parser,
21602 bool template_parm_p,
21603 bool *parenthesized_p)
21605 int declares_class_or_enum;
21606 cp_decl_specifier_seq decl_specifiers;
21607 cp_declarator *declarator;
21608 tree default_argument;
21609 cp_token *token = NULL, *declarator_token_start = NULL;
21610 const char *saved_message;
21611 bool template_parameter_pack_p = false;
21613 /* In a template parameter, `>' is not an operator.
21615 [temp.param]
21617 When parsing a default template-argument for a non-type
21618 template-parameter, the first non-nested `>' is taken as the end
21619 of the template parameter-list rather than a greater-than
21620 operator. */
21622 /* Type definitions may not appear in parameter types. */
21623 saved_message = parser->type_definition_forbidden_message;
21624 parser->type_definition_forbidden_message
21625 = G_("types may not be defined in parameter types");
21627 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21628 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21629 (current_template_parms)) : 0);
21631 /* Parse the declaration-specifiers. */
21632 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21633 cp_parser_decl_specifier_seq (parser,
21634 CP_PARSER_FLAGS_NONE,
21635 &decl_specifiers,
21636 &declares_class_or_enum);
21638 /* Complain about missing 'typename' or other invalid type names. */
21639 if (!decl_specifiers.any_type_specifiers_p
21640 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21641 decl_specifiers.type = error_mark_node;
21643 /* If an error occurred, there's no reason to attempt to parse the
21644 rest of the declaration. */
21645 if (cp_parser_error_occurred (parser))
21647 parser->type_definition_forbidden_message = saved_message;
21648 return NULL;
21651 /* Peek at the next token. */
21652 token = cp_lexer_peek_token (parser->lexer);
21654 /* If the next token is a `)', `,', `=', `>', or `...', then there
21655 is no declarator. However, when variadic templates are enabled,
21656 there may be a declarator following `...'. */
21657 if (token->type == CPP_CLOSE_PAREN
21658 || token->type == CPP_COMMA
21659 || token->type == CPP_EQ
21660 || token->type == CPP_GREATER)
21662 declarator = NULL;
21663 if (parenthesized_p)
21664 *parenthesized_p = false;
21666 /* Otherwise, there should be a declarator. */
21667 else
21669 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21670 parser->default_arg_ok_p = false;
21672 /* After seeing a decl-specifier-seq, if the next token is not a
21673 "(", there is no possibility that the code is a valid
21674 expression. Therefore, if parsing tentatively, we commit at
21675 this point. */
21676 if (!parser->in_template_argument_list_p
21677 /* In an expression context, having seen:
21679 (int((char ...
21681 we cannot be sure whether we are looking at a
21682 function-type (taking a "char" as a parameter) or a cast
21683 of some object of type "char" to "int". */
21684 && !parser->in_type_id_in_expr_p
21685 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21686 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21687 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21688 cp_parser_commit_to_tentative_parse (parser);
21689 /* Parse the declarator. */
21690 declarator_token_start = token;
21691 declarator = cp_parser_declarator (parser,
21692 CP_PARSER_DECLARATOR_EITHER,
21693 /*ctor_dtor_or_conv_p=*/NULL,
21694 parenthesized_p,
21695 /*member_p=*/false,
21696 /*friend_p=*/false);
21697 parser->default_arg_ok_p = saved_default_arg_ok_p;
21698 /* After the declarator, allow more attributes. */
21699 decl_specifiers.attributes
21700 = attr_chainon (decl_specifiers.attributes,
21701 cp_parser_attributes_opt (parser));
21703 /* If the declarator is a template parameter pack, remember that and
21704 clear the flag in the declarator itself so we don't get errors
21705 from grokdeclarator. */
21706 if (template_parm_p && declarator && declarator->parameter_pack_p)
21708 declarator->parameter_pack_p = false;
21709 template_parameter_pack_p = true;
21713 /* If the next token is an ellipsis, and we have not seen a declarator
21714 name, and if either the type of the declarator contains parameter
21715 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21716 for, eg, abbreviated integral type names), then we actually have a
21717 parameter pack expansion expression. Otherwise, leave the ellipsis
21718 for a C-style variadic function. */
21719 token = cp_lexer_peek_token (parser->lexer);
21721 /* If a function parameter pack was specified and an implicit template
21722 parameter was introduced during cp_parser_parameter_declaration,
21723 change any implicit parameters introduced into packs. */
21724 if (parser->implicit_template_parms
21725 && ((token->type == CPP_ELLIPSIS
21726 && declarator_can_be_parameter_pack (declarator))
21727 || (declarator && declarator->parameter_pack_p)))
21729 int latest_template_parm_idx = TREE_VEC_LENGTH
21730 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21732 if (latest_template_parm_idx != template_parm_idx)
21733 decl_specifiers.type = convert_generic_types_to_packs
21734 (decl_specifiers.type,
21735 template_parm_idx, latest_template_parm_idx);
21738 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21740 tree type = decl_specifiers.type;
21742 if (type && DECL_P (type))
21743 type = TREE_TYPE (type);
21745 if (((type
21746 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21747 && (template_parm_p || uses_parameter_packs (type)))
21748 || (!type && template_parm_p))
21749 && declarator_can_be_parameter_pack (declarator))
21751 /* Consume the `...'. */
21752 cp_lexer_consume_token (parser->lexer);
21753 maybe_warn_variadic_templates ();
21755 /* Build a pack expansion type */
21756 if (template_parm_p)
21757 template_parameter_pack_p = true;
21758 else if (declarator)
21759 declarator->parameter_pack_p = true;
21760 else
21761 decl_specifiers.type = make_pack_expansion (type);
21765 /* The restriction on defining new types applies only to the type
21766 of the parameter, not to the default argument. */
21767 parser->type_definition_forbidden_message = saved_message;
21769 /* If the next token is `=', then process a default argument. */
21770 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21772 tree type = decl_specifiers.type;
21773 token = cp_lexer_peek_token (parser->lexer);
21774 /* If we are defining a class, then the tokens that make up the
21775 default argument must be saved and processed later. */
21776 if (!template_parm_p && at_class_scope_p ()
21777 && TYPE_BEING_DEFINED (current_class_type)
21778 && !LAMBDA_TYPE_P (current_class_type))
21779 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21781 // A constrained-type-specifier may declare a type template-parameter.
21782 else if (declares_constrained_type_template_parameter (type))
21783 default_argument
21784 = cp_parser_default_type_template_argument (parser);
21786 // A constrained-type-specifier may declare a template-template-parameter.
21787 else if (declares_constrained_template_template_parameter (type))
21788 default_argument
21789 = cp_parser_default_template_template_argument (parser);
21791 /* Outside of a class definition, we can just parse the
21792 assignment-expression. */
21793 else
21794 default_argument
21795 = cp_parser_default_argument (parser, template_parm_p);
21797 if (!parser->default_arg_ok_p)
21799 permerror (token->location,
21800 "default arguments are only "
21801 "permitted for function parameters");
21803 else if ((declarator && declarator->parameter_pack_p)
21804 || template_parameter_pack_p
21805 || (decl_specifiers.type
21806 && PACK_EXPANSION_P (decl_specifiers.type)))
21808 /* Find the name of the parameter pack. */
21809 cp_declarator *id_declarator = declarator;
21810 while (id_declarator && id_declarator->kind != cdk_id)
21811 id_declarator = id_declarator->declarator;
21813 if (id_declarator && id_declarator->kind == cdk_id)
21814 error_at (declarator_token_start->location,
21815 template_parm_p
21816 ? G_("template parameter pack %qD "
21817 "cannot have a default argument")
21818 : G_("parameter pack %qD cannot have "
21819 "a default argument"),
21820 id_declarator->u.id.unqualified_name);
21821 else
21822 error_at (declarator_token_start->location,
21823 template_parm_p
21824 ? G_("template parameter pack cannot have "
21825 "a default argument")
21826 : G_("parameter pack cannot have a "
21827 "default argument"));
21829 default_argument = NULL_TREE;
21832 else
21833 default_argument = NULL_TREE;
21835 /* Generate a location for the parameter, ranging from the start of the
21836 initial token to the end of the final token (using input_location for
21837 the latter, set up by cp_lexer_set_source_position_from_token when
21838 consuming tokens).
21840 If we have a identifier, then use it for the caret location, e.g.
21842 extern int callee (int one, int (*two)(int, int), float three);
21843 ~~~~~~^~~~~~~~~~~~~~
21845 otherwise, reuse the start location for the caret location e.g.:
21847 extern int callee (int one, int (*)(int, int), float three);
21848 ^~~~~~~~~~~~~~~~~
21851 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21852 ? declarator->id_loc
21853 : decl_spec_token_start->location);
21854 location_t param_loc = make_location (caret_loc,
21855 decl_spec_token_start->location,
21856 input_location);
21858 return make_parameter_declarator (&decl_specifiers,
21859 declarator,
21860 default_argument,
21861 param_loc,
21862 template_parameter_pack_p);
21865 /* Parse a default argument and return it.
21867 TEMPLATE_PARM_P is true if this is a default argument for a
21868 non-type template parameter. */
21869 static tree
21870 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21872 tree default_argument = NULL_TREE;
21873 bool saved_greater_than_is_operator_p;
21874 bool saved_local_variables_forbidden_p;
21875 bool non_constant_p, is_direct_init;
21877 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21878 set correctly. */
21879 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21880 parser->greater_than_is_operator_p = !template_parm_p;
21881 /* Local variable names (and the `this' keyword) may not
21882 appear in a default argument. */
21883 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21884 parser->local_variables_forbidden_p = true;
21885 /* Parse the assignment-expression. */
21886 if (template_parm_p)
21887 push_deferring_access_checks (dk_no_deferred);
21888 tree saved_class_ptr = NULL_TREE;
21889 tree saved_class_ref = NULL_TREE;
21890 /* The "this" pointer is not valid in a default argument. */
21891 if (cfun)
21893 saved_class_ptr = current_class_ptr;
21894 cp_function_chain->x_current_class_ptr = NULL_TREE;
21895 saved_class_ref = current_class_ref;
21896 cp_function_chain->x_current_class_ref = NULL_TREE;
21898 default_argument
21899 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21900 /* Restore the "this" pointer. */
21901 if (cfun)
21903 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21904 cp_function_chain->x_current_class_ref = saved_class_ref;
21906 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21907 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21908 if (template_parm_p)
21909 pop_deferring_access_checks ();
21910 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21911 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21913 return default_argument;
21916 /* Parse a function-body.
21918 function-body:
21919 compound_statement */
21921 static void
21922 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21924 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21925 ? BCS_TRY_BLOCK : BCS_NORMAL),
21926 true);
21929 /* Parse a ctor-initializer-opt followed by a function-body. Return
21930 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21931 is true we are parsing a function-try-block. */
21933 static void
21934 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21935 bool in_function_try_block)
21937 tree body, list;
21938 const bool check_body_p =
21939 DECL_CONSTRUCTOR_P (current_function_decl)
21940 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21941 tree last = NULL;
21943 /* Begin the function body. */
21944 body = begin_function_body ();
21945 /* Parse the optional ctor-initializer. */
21946 cp_parser_ctor_initializer_opt (parser);
21948 /* If we're parsing a constexpr constructor definition, we need
21949 to check that the constructor body is indeed empty. However,
21950 before we get to cp_parser_function_body lot of junk has been
21951 generated, so we can't just check that we have an empty block.
21952 Rather we take a snapshot of the outermost block, and check whether
21953 cp_parser_function_body changed its state. */
21954 if (check_body_p)
21956 list = cur_stmt_list;
21957 if (STATEMENT_LIST_TAIL (list))
21958 last = STATEMENT_LIST_TAIL (list)->stmt;
21960 /* Parse the function-body. */
21961 cp_parser_function_body (parser, in_function_try_block);
21962 if (check_body_p)
21963 check_constexpr_ctor_body (last, list, /*complain=*/true);
21964 /* Finish the function body. */
21965 finish_function_body (body);
21968 /* Parse an initializer.
21970 initializer:
21971 = initializer-clause
21972 ( expression-list )
21974 Returns an expression representing the initializer. If no
21975 initializer is present, NULL_TREE is returned.
21977 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21978 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21979 set to TRUE if there is no initializer present. If there is an
21980 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21981 is set to true; otherwise it is set to false. */
21983 static tree
21984 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21985 bool* non_constant_p, bool subexpression_p)
21987 cp_token *token;
21988 tree init;
21990 /* Peek at the next token. */
21991 token = cp_lexer_peek_token (parser->lexer);
21993 /* Let our caller know whether or not this initializer was
21994 parenthesized. */
21995 *is_direct_init = (token->type != CPP_EQ);
21996 /* Assume that the initializer is constant. */
21997 *non_constant_p = false;
21999 if (token->type == CPP_EQ)
22001 /* Consume the `='. */
22002 cp_lexer_consume_token (parser->lexer);
22003 /* Parse the initializer-clause. */
22004 init = cp_parser_initializer_clause (parser, non_constant_p);
22006 else if (token->type == CPP_OPEN_PAREN)
22008 vec<tree, va_gc> *vec;
22009 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22010 /*cast_p=*/false,
22011 /*allow_expansion_p=*/true,
22012 non_constant_p);
22013 if (vec == NULL)
22014 return error_mark_node;
22015 init = build_tree_list_vec (vec);
22016 release_tree_vector (vec);
22018 else if (token->type == CPP_OPEN_BRACE)
22020 cp_lexer_set_source_position (parser->lexer);
22021 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22022 init = cp_parser_braced_list (parser, non_constant_p);
22023 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22025 else
22027 /* Anything else is an error. */
22028 cp_parser_error (parser, "expected initializer");
22029 init = error_mark_node;
22032 if (!subexpression_p && check_for_bare_parameter_packs (init))
22033 init = error_mark_node;
22035 return init;
22038 /* Parse an initializer-clause.
22040 initializer-clause:
22041 assignment-expression
22042 braced-init-list
22044 Returns an expression representing the initializer.
22046 If the `assignment-expression' production is used the value
22047 returned is simply a representation for the expression.
22049 Otherwise, calls cp_parser_braced_list. */
22051 static cp_expr
22052 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22054 cp_expr initializer;
22056 /* Assume the expression is constant. */
22057 *non_constant_p = false;
22059 /* If it is not a `{', then we are looking at an
22060 assignment-expression. */
22061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22063 initializer
22064 = cp_parser_constant_expression (parser,
22065 /*allow_non_constant_p=*/true,
22066 non_constant_p);
22068 else
22069 initializer = cp_parser_braced_list (parser, non_constant_p);
22071 return initializer;
22074 /* Parse a brace-enclosed initializer list.
22076 braced-init-list:
22077 { initializer-list , [opt] }
22078 { designated-initializer-list , [opt] }
22081 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22082 the elements of the initializer-list (or NULL, if the last
22083 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22084 NULL_TREE. There is no way to detect whether or not the optional
22085 trailing `,' was provided. NON_CONSTANT_P is as for
22086 cp_parser_initializer. */
22088 static cp_expr
22089 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22091 tree initializer;
22092 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22094 /* Consume the `{' token. */
22095 matching_braces braces;
22096 braces.require_open (parser);
22097 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22098 initializer = make_node (CONSTRUCTOR);
22099 /* If it's not a `}', then there is a non-trivial initializer. */
22100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22102 /* Parse the initializer list. */
22103 CONSTRUCTOR_ELTS (initializer)
22104 = cp_parser_initializer_list (parser, non_constant_p);
22105 /* A trailing `,' token is allowed. */
22106 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22107 cp_lexer_consume_token (parser->lexer);
22109 else
22110 *non_constant_p = false;
22111 /* Now, there should be a trailing `}'. */
22112 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22113 braces.require_close (parser);
22114 TREE_TYPE (initializer) = init_list_type_node;
22116 cp_expr result (initializer);
22117 /* Build a location of the form:
22118 { ... }
22119 ^~~~~~~
22120 with caret==start at the open brace, finish at the close brace. */
22121 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22122 result.set_location (combined_loc);
22123 return result;
22126 /* Consume tokens up to, and including, the next non-nested closing `]'.
22127 Returns true iff we found a closing `]'. */
22129 static bool
22130 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22132 unsigned square_depth = 0;
22134 while (true)
22136 cp_token * token = cp_lexer_peek_token (parser->lexer);
22138 switch (token->type)
22140 case CPP_EOF:
22141 case CPP_PRAGMA_EOL:
22142 /* If we've run out of tokens, then there is no closing `]'. */
22143 return false;
22145 case CPP_OPEN_SQUARE:
22146 ++square_depth;
22147 break;
22149 case CPP_CLOSE_SQUARE:
22150 if (!square_depth--)
22152 cp_lexer_consume_token (parser->lexer);
22153 return true;
22155 break;
22157 default:
22158 break;
22161 /* Consume the token. */
22162 cp_lexer_consume_token (parser->lexer);
22166 /* Return true if we are looking at an array-designator, false otherwise. */
22168 static bool
22169 cp_parser_array_designator_p (cp_parser *parser)
22171 /* Consume the `['. */
22172 cp_lexer_consume_token (parser->lexer);
22174 cp_lexer_save_tokens (parser->lexer);
22176 /* Skip tokens until the next token is a closing square bracket.
22177 If we find the closing `]', and the next token is a `=', then
22178 we are looking at an array designator. */
22179 bool array_designator_p
22180 = (cp_parser_skip_to_closing_square_bracket (parser)
22181 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22183 /* Roll back the tokens we skipped. */
22184 cp_lexer_rollback_tokens (parser->lexer);
22186 return array_designator_p;
22189 /* Parse an initializer-list.
22191 initializer-list:
22192 initializer-clause ... [opt]
22193 initializer-list , initializer-clause ... [opt]
22195 C++2A Extension:
22197 designated-initializer-list:
22198 designated-initializer-clause
22199 designated-initializer-list , designated-initializer-clause
22201 designated-initializer-clause:
22202 designator brace-or-equal-initializer
22204 designator:
22205 . identifier
22207 GNU Extension:
22209 initializer-list:
22210 designation initializer-clause ...[opt]
22211 initializer-list , designation initializer-clause ...[opt]
22213 designation:
22214 . identifier =
22215 identifier :
22216 [ constant-expression ] =
22218 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22219 for the initializer. If the INDEX of the elt is non-NULL, it is the
22220 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22221 as for cp_parser_initializer. */
22223 static vec<constructor_elt, va_gc> *
22224 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22226 vec<constructor_elt, va_gc> *v = NULL;
22227 bool first_p = true;
22228 tree first_designator = NULL_TREE;
22230 /* Assume all of the expressions are constant. */
22231 *non_constant_p = false;
22233 /* Parse the rest of the list. */
22234 while (true)
22236 cp_token *token;
22237 tree designator;
22238 tree initializer;
22239 bool clause_non_constant_p;
22240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22242 /* Handle the C++2A syntax, '. id ='. */
22243 if ((cxx_dialect >= cxx2a
22244 || cp_parser_allow_gnu_extensions_p (parser))
22245 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22246 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22247 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22248 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22249 == CPP_OPEN_BRACE)))
22251 if (cxx_dialect < cxx2a)
22252 pedwarn (loc, OPT_Wpedantic,
22253 "C++ designated initializers only available with "
22254 "-std=c++2a or -std=gnu++2a");
22255 /* Consume the `.'. */
22256 cp_lexer_consume_token (parser->lexer);
22257 /* Consume the identifier. */
22258 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22259 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22260 /* Consume the `='. */
22261 cp_lexer_consume_token (parser->lexer);
22263 /* Also, if the next token is an identifier and the following one is a
22264 colon, we are looking at the GNU designated-initializer
22265 syntax. */
22266 else if (cp_parser_allow_gnu_extensions_p (parser)
22267 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22268 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22269 == CPP_COLON))
22271 /* Warn the user that they are using an extension. */
22272 pedwarn (loc, OPT_Wpedantic,
22273 "ISO C++ does not allow GNU designated initializers");
22274 /* Consume the identifier. */
22275 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22276 /* Consume the `:'. */
22277 cp_lexer_consume_token (parser->lexer);
22279 /* Also handle C99 array designators, '[ const ] ='. */
22280 else if (cp_parser_allow_gnu_extensions_p (parser)
22281 && !c_dialect_objc ()
22282 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22284 /* In C++11, [ could start a lambda-introducer. */
22285 bool non_const = false;
22287 cp_parser_parse_tentatively (parser);
22289 if (!cp_parser_array_designator_p (parser))
22291 cp_parser_simulate_error (parser);
22292 designator = NULL_TREE;
22294 else
22296 designator = cp_parser_constant_expression (parser, true,
22297 &non_const);
22298 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22299 cp_parser_require (parser, CPP_EQ, RT_EQ);
22302 if (!cp_parser_parse_definitely (parser))
22303 designator = NULL_TREE;
22304 else if (non_const
22305 && (!require_potential_rvalue_constant_expression
22306 (designator)))
22307 designator = NULL_TREE;
22308 if (designator)
22309 /* Warn the user that they are using an extension. */
22310 pedwarn (loc, OPT_Wpedantic,
22311 "ISO C++ does not allow C99 designated initializers");
22313 else
22314 designator = NULL_TREE;
22316 if (first_p)
22318 first_designator = designator;
22319 first_p = false;
22321 else if (cxx_dialect >= cxx2a
22322 && first_designator != error_mark_node
22323 && (!first_designator != !designator))
22325 error_at (loc, "either all initializer clauses should be designated "
22326 "or none of them should be");
22327 first_designator = error_mark_node;
22329 else if (cxx_dialect < cxx2a && !first_designator)
22330 first_designator = designator;
22332 /* Parse the initializer. */
22333 initializer = cp_parser_initializer_clause (parser,
22334 &clause_non_constant_p);
22335 /* If any clause is non-constant, so is the entire initializer. */
22336 if (clause_non_constant_p)
22337 *non_constant_p = true;
22339 /* If we have an ellipsis, this is an initializer pack
22340 expansion. */
22341 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22343 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22345 /* Consume the `...'. */
22346 cp_lexer_consume_token (parser->lexer);
22348 if (designator && cxx_dialect >= cxx2a)
22349 error_at (loc,
22350 "%<...%> not allowed in designated initializer list");
22352 /* Turn the initializer into an initializer expansion. */
22353 initializer = make_pack_expansion (initializer);
22356 /* Add it to the vector. */
22357 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22359 /* If the next token is not a comma, we have reached the end of
22360 the list. */
22361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22362 break;
22364 /* Peek at the next token. */
22365 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22366 /* If the next token is a `}', then we're still done. An
22367 initializer-clause can have a trailing `,' after the
22368 initializer-list and before the closing `}'. */
22369 if (token->type == CPP_CLOSE_BRACE)
22370 break;
22372 /* Consume the `,' token. */
22373 cp_lexer_consume_token (parser->lexer);
22376 /* The same identifier shall not appear in multiple designators
22377 of a designated-initializer-list. */
22378 if (first_designator)
22380 unsigned int i;
22381 tree designator, val;
22382 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22383 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22385 if (IDENTIFIER_MARKED (designator))
22387 error_at (cp_expr_loc_or_loc (val, input_location),
22388 "%<.%s%> designator used multiple times in "
22389 "the same initializer list",
22390 IDENTIFIER_POINTER (designator));
22391 (*v)[i].index = NULL_TREE;
22393 else
22394 IDENTIFIER_MARKED (designator) = 1;
22396 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22397 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22398 IDENTIFIER_MARKED (designator) = 0;
22401 return v;
22404 /* Classes [gram.class] */
22406 /* Parse a class-name.
22408 class-name:
22409 identifier
22410 template-id
22412 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22413 to indicate that names looked up in dependent types should be
22414 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22415 keyword has been used to indicate that the name that appears next
22416 is a template. TAG_TYPE indicates the explicit tag given before
22417 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22418 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22419 is the class being defined in a class-head. If ENUM_OK is TRUE,
22420 enum-names are also accepted.
22422 Returns the TYPE_DECL representing the class. */
22424 static tree
22425 cp_parser_class_name (cp_parser *parser,
22426 bool typename_keyword_p,
22427 bool template_keyword_p,
22428 enum tag_types tag_type,
22429 bool check_dependency_p,
22430 bool class_head_p,
22431 bool is_declaration,
22432 bool enum_ok)
22434 tree decl;
22435 tree scope;
22436 bool typename_p;
22437 cp_token *token;
22438 tree identifier = NULL_TREE;
22440 /* All class-names start with an identifier. */
22441 token = cp_lexer_peek_token (parser->lexer);
22442 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22444 cp_parser_error (parser, "expected class-name");
22445 return error_mark_node;
22448 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22449 to a template-id, so we save it here. */
22450 scope = parser->scope;
22451 if (scope == error_mark_node)
22452 return error_mark_node;
22454 /* Any name names a type if we're following the `typename' keyword
22455 in a qualified name where the enclosing scope is type-dependent. */
22456 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22457 && dependent_type_p (scope));
22458 /* Handle the common case (an identifier, but not a template-id)
22459 efficiently. */
22460 if (token->type == CPP_NAME
22461 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22463 cp_token *identifier_token;
22464 bool ambiguous_p;
22466 /* Look for the identifier. */
22467 identifier_token = cp_lexer_peek_token (parser->lexer);
22468 ambiguous_p = identifier_token->error_reported;
22469 identifier = cp_parser_identifier (parser);
22470 /* If the next token isn't an identifier, we are certainly not
22471 looking at a class-name. */
22472 if (identifier == error_mark_node)
22473 decl = error_mark_node;
22474 /* If we know this is a type-name, there's no need to look it
22475 up. */
22476 else if (typename_p)
22477 decl = identifier;
22478 else
22480 tree ambiguous_decls;
22481 /* If we already know that this lookup is ambiguous, then
22482 we've already issued an error message; there's no reason
22483 to check again. */
22484 if (ambiguous_p)
22486 cp_parser_simulate_error (parser);
22487 return error_mark_node;
22489 /* If the next token is a `::', then the name must be a type
22490 name.
22492 [basic.lookup.qual]
22494 During the lookup for a name preceding the :: scope
22495 resolution operator, object, function, and enumerator
22496 names are ignored. */
22497 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22498 tag_type = scope_type;
22499 /* Look up the name. */
22500 decl = cp_parser_lookup_name (parser, identifier,
22501 tag_type,
22502 /*is_template=*/false,
22503 /*is_namespace=*/false,
22504 check_dependency_p,
22505 &ambiguous_decls,
22506 identifier_token->location);
22507 if (ambiguous_decls)
22509 if (cp_parser_parsing_tentatively (parser))
22510 cp_parser_simulate_error (parser);
22511 return error_mark_node;
22515 else
22517 /* Try a template-id. */
22518 decl = cp_parser_template_id (parser, template_keyword_p,
22519 check_dependency_p,
22520 tag_type,
22521 is_declaration);
22522 if (decl == error_mark_node)
22523 return error_mark_node;
22526 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22528 /* If this is a typename, create a TYPENAME_TYPE. */
22529 if (typename_p && decl != error_mark_node)
22531 decl = make_typename_type (scope, decl, typename_type,
22532 /*complain=*/tf_error);
22533 if (decl != error_mark_node)
22534 decl = TYPE_NAME (decl);
22537 decl = strip_using_decl (decl);
22539 /* Check to see that it is really the name of a class. */
22540 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22541 && identifier_p (TREE_OPERAND (decl, 0))
22542 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22543 /* Situations like this:
22545 template <typename T> struct A {
22546 typename T::template X<int>::I i;
22549 are problematic. Is `T::template X<int>' a class-name? The
22550 standard does not seem to be definitive, but there is no other
22551 valid interpretation of the following `::'. Therefore, those
22552 names are considered class-names. */
22554 decl = make_typename_type (scope, decl, tag_type, tf_error);
22555 if (decl != error_mark_node)
22556 decl = TYPE_NAME (decl);
22558 else if (TREE_CODE (decl) != TYPE_DECL
22559 || TREE_TYPE (decl) == error_mark_node
22560 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22561 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22562 /* In Objective-C 2.0, a classname followed by '.' starts a
22563 dot-syntax expression, and it's not a type-name. */
22564 || (c_dialect_objc ()
22565 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22566 && objc_is_class_name (decl)))
22567 decl = error_mark_node;
22569 if (decl == error_mark_node)
22570 cp_parser_error (parser, "expected class-name");
22571 else if (identifier && !parser->scope)
22572 maybe_note_name_used_in_class (identifier, decl);
22574 return decl;
22577 /* Parse a class-specifier.
22579 class-specifier:
22580 class-head { member-specification [opt] }
22582 Returns the TREE_TYPE representing the class. */
22584 static tree
22585 cp_parser_class_specifier_1 (cp_parser* parser)
22587 tree type;
22588 tree attributes = NULL_TREE;
22589 bool nested_name_specifier_p;
22590 unsigned saved_num_template_parameter_lists;
22591 bool saved_in_function_body;
22592 unsigned char in_statement;
22593 bool in_switch_statement_p;
22594 bool saved_in_unbraced_linkage_specification_p;
22595 tree old_scope = NULL_TREE;
22596 tree scope = NULL_TREE;
22597 cp_token *closing_brace;
22599 push_deferring_access_checks (dk_no_deferred);
22601 /* Parse the class-head. */
22602 type = cp_parser_class_head (parser,
22603 &nested_name_specifier_p);
22604 /* If the class-head was a semantic disaster, skip the entire body
22605 of the class. */
22606 if (!type)
22608 cp_parser_skip_to_end_of_block_or_statement (parser);
22609 pop_deferring_access_checks ();
22610 return error_mark_node;
22613 /* Look for the `{'. */
22614 matching_braces braces;
22615 if (!braces.require_open (parser))
22617 pop_deferring_access_checks ();
22618 return error_mark_node;
22621 cp_ensure_no_omp_declare_simd (parser);
22622 cp_ensure_no_oacc_routine (parser);
22624 /* Issue an error message if type-definitions are forbidden here. */
22625 cp_parser_check_type_definition (parser);
22626 /* Remember that we are defining one more class. */
22627 ++parser->num_classes_being_defined;
22628 /* Inside the class, surrounding template-parameter-lists do not
22629 apply. */
22630 saved_num_template_parameter_lists
22631 = parser->num_template_parameter_lists;
22632 parser->num_template_parameter_lists = 0;
22633 /* We are not in a function body. */
22634 saved_in_function_body = parser->in_function_body;
22635 parser->in_function_body = false;
22636 /* Or in a loop. */
22637 in_statement = parser->in_statement;
22638 parser->in_statement = 0;
22639 /* Or in a switch. */
22640 in_switch_statement_p = parser->in_switch_statement_p;
22641 parser->in_switch_statement_p = false;
22642 /* We are not immediately inside an extern "lang" block. */
22643 saved_in_unbraced_linkage_specification_p
22644 = parser->in_unbraced_linkage_specification_p;
22645 parser->in_unbraced_linkage_specification_p = false;
22647 // Associate constraints with the type.
22648 if (flag_concepts)
22649 type = associate_classtype_constraints (type);
22651 /* Start the class. */
22652 if (nested_name_specifier_p)
22654 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22655 old_scope = push_inner_scope (scope);
22657 type = begin_class_definition (type);
22659 if (type == error_mark_node)
22660 /* If the type is erroneous, skip the entire body of the class. */
22661 cp_parser_skip_to_closing_brace (parser);
22662 else
22663 /* Parse the member-specification. */
22664 cp_parser_member_specification_opt (parser);
22666 /* Look for the trailing `}'. */
22667 closing_brace = braces.require_close (parser);
22668 /* Look for trailing attributes to apply to this class. */
22669 if (cp_parser_allow_gnu_extensions_p (parser))
22670 attributes = cp_parser_gnu_attributes_opt (parser);
22671 if (type != error_mark_node)
22672 type = finish_struct (type, attributes);
22673 if (nested_name_specifier_p)
22674 pop_inner_scope (old_scope, scope);
22676 /* We've finished a type definition. Check for the common syntax
22677 error of forgetting a semicolon after the definition. We need to
22678 be careful, as we can't just check for not-a-semicolon and be done
22679 with it; the user might have typed:
22681 class X { } c = ...;
22682 class X { } *p = ...;
22684 and so forth. Instead, enumerate all the possible tokens that
22685 might follow this production; if we don't see one of them, then
22686 complain and silently insert the semicolon. */
22688 cp_token *token = cp_lexer_peek_token (parser->lexer);
22689 bool want_semicolon = true;
22691 if (cp_next_tokens_can_be_std_attribute_p (parser))
22692 /* Don't try to parse c++11 attributes here. As per the
22693 grammar, that should be a task for
22694 cp_parser_decl_specifier_seq. */
22695 want_semicolon = false;
22697 switch (token->type)
22699 case CPP_NAME:
22700 case CPP_SEMICOLON:
22701 case CPP_MULT:
22702 case CPP_AND:
22703 case CPP_OPEN_PAREN:
22704 case CPP_CLOSE_PAREN:
22705 case CPP_COMMA:
22706 want_semicolon = false;
22707 break;
22709 /* While it's legal for type qualifiers and storage class
22710 specifiers to follow type definitions in the grammar, only
22711 compiler testsuites contain code like that. Assume that if
22712 we see such code, then what we're really seeing is a case
22713 like:
22715 class X { }
22716 const <type> var = ...;
22720 class Y { }
22721 static <type> func (...) ...
22723 i.e. the qualifier or specifier applies to the next
22724 declaration. To do so, however, we need to look ahead one
22725 more token to see if *that* token is a type specifier.
22727 This code could be improved to handle:
22729 class Z { }
22730 static const <type> var = ...; */
22731 case CPP_KEYWORD:
22732 if (keyword_is_decl_specifier (token->keyword))
22734 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22736 /* Handling user-defined types here would be nice, but very
22737 tricky. */
22738 want_semicolon
22739 = (lookahead->type == CPP_KEYWORD
22740 && keyword_begins_type_specifier (lookahead->keyword));
22742 break;
22743 default:
22744 break;
22747 /* If we don't have a type, then something is very wrong and we
22748 shouldn't try to do anything clever. Likewise for not seeing the
22749 closing brace. */
22750 if (closing_brace && TYPE_P (type) && want_semicolon)
22752 /* Locate the closing brace. */
22753 cp_token_position prev
22754 = cp_lexer_previous_token_position (parser->lexer);
22755 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22756 location_t loc = prev_token->location;
22758 /* We want to suggest insertion of a ';' immediately *after* the
22759 closing brace, so, if we can, offset the location by 1 column. */
22760 location_t next_loc = loc;
22761 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22762 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22764 rich_location richloc (line_table, next_loc);
22766 /* If we successfully offset the location, suggest the fix-it. */
22767 if (next_loc != loc)
22768 richloc.add_fixit_insert_before (next_loc, ";");
22770 if (CLASSTYPE_DECLARED_CLASS (type))
22771 error_at (&richloc,
22772 "expected %<;%> after class definition");
22773 else if (TREE_CODE (type) == RECORD_TYPE)
22774 error_at (&richloc,
22775 "expected %<;%> after struct definition");
22776 else if (TREE_CODE (type) == UNION_TYPE)
22777 error_at (&richloc,
22778 "expected %<;%> after union definition");
22779 else
22780 gcc_unreachable ();
22782 /* Unget one token and smash it to look as though we encountered
22783 a semicolon in the input stream. */
22784 cp_lexer_set_token_position (parser->lexer, prev);
22785 token = cp_lexer_peek_token (parser->lexer);
22786 token->type = CPP_SEMICOLON;
22787 token->keyword = RID_MAX;
22791 /* If this class is not itself within the scope of another class,
22792 then we need to parse the bodies of all of the queued function
22793 definitions. Note that the queued functions defined in a class
22794 are not always processed immediately following the
22795 class-specifier for that class. Consider:
22797 struct A {
22798 struct B { void f() { sizeof (A); } };
22801 If `f' were processed before the processing of `A' were
22802 completed, there would be no way to compute the size of `A'.
22803 Note that the nesting we are interested in here is lexical --
22804 not the semantic nesting given by TYPE_CONTEXT. In particular,
22805 for:
22807 struct A { struct B; };
22808 struct A::B { void f() { } };
22810 there is no need to delay the parsing of `A::B::f'. */
22811 if (--parser->num_classes_being_defined == 0)
22813 tree decl;
22814 tree class_type = NULL_TREE;
22815 tree pushed_scope = NULL_TREE;
22816 unsigned ix;
22817 cp_default_arg_entry *e;
22818 tree save_ccp, save_ccr;
22820 if (any_erroneous_template_args_p (type))
22822 /* Skip default arguments, NSDMIs, etc, in order to improve
22823 error recovery (c++/71169, c++/71832). */
22824 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22825 vec_safe_truncate (unparsed_nsdmis, 0);
22826 vec_safe_truncate (unparsed_classes, 0);
22827 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22830 /* In a first pass, parse default arguments to the functions.
22831 Then, in a second pass, parse the bodies of the functions.
22832 This two-phased approach handles cases like:
22834 struct S {
22835 void f() { g(); }
22836 void g(int i = 3);
22840 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22842 decl = e->decl;
22843 /* If there are default arguments that have not yet been processed,
22844 take care of them now. */
22845 if (class_type != e->class_type)
22847 if (pushed_scope)
22848 pop_scope (pushed_scope);
22849 class_type = e->class_type;
22850 pushed_scope = push_scope (class_type);
22852 /* Make sure that any template parameters are in scope. */
22853 maybe_begin_member_template_processing (decl);
22854 /* Parse the default argument expressions. */
22855 cp_parser_late_parsing_default_args (parser, decl);
22856 /* Remove any template parameters from the symbol table. */
22857 maybe_end_member_template_processing ();
22859 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22860 /* Now parse any NSDMIs. */
22861 save_ccp = current_class_ptr;
22862 save_ccr = current_class_ref;
22863 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22865 if (class_type != DECL_CONTEXT (decl))
22867 if (pushed_scope)
22868 pop_scope (pushed_scope);
22869 class_type = DECL_CONTEXT (decl);
22870 pushed_scope = push_scope (class_type);
22872 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22873 cp_parser_late_parsing_nsdmi (parser, decl);
22875 vec_safe_truncate (unparsed_nsdmis, 0);
22876 current_class_ptr = save_ccp;
22877 current_class_ref = save_ccr;
22878 if (pushed_scope)
22879 pop_scope (pushed_scope);
22881 /* Now do some post-NSDMI bookkeeping. */
22882 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22883 after_nsdmi_defaulted_late_checks (class_type);
22884 vec_safe_truncate (unparsed_classes, 0);
22885 after_nsdmi_defaulted_late_checks (type);
22887 /* Now parse the body of the functions. */
22888 if (flag_openmp)
22890 /* OpenMP UDRs need to be parsed before all other functions. */
22891 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22892 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22893 cp_parser_late_parsing_for_member (parser, decl);
22894 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22895 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22896 cp_parser_late_parsing_for_member (parser, decl);
22898 else
22899 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22900 cp_parser_late_parsing_for_member (parser, decl);
22901 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22903 else
22904 vec_safe_push (unparsed_classes, type);
22906 /* Put back any saved access checks. */
22907 pop_deferring_access_checks ();
22909 /* Restore saved state. */
22910 parser->in_switch_statement_p = in_switch_statement_p;
22911 parser->in_statement = in_statement;
22912 parser->in_function_body = saved_in_function_body;
22913 parser->num_template_parameter_lists
22914 = saved_num_template_parameter_lists;
22915 parser->in_unbraced_linkage_specification_p
22916 = saved_in_unbraced_linkage_specification_p;
22918 return type;
22921 static tree
22922 cp_parser_class_specifier (cp_parser* parser)
22924 tree ret;
22925 timevar_push (TV_PARSE_STRUCT);
22926 ret = cp_parser_class_specifier_1 (parser);
22927 timevar_pop (TV_PARSE_STRUCT);
22928 return ret;
22931 /* Parse a class-head.
22933 class-head:
22934 class-key identifier [opt] base-clause [opt]
22935 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22936 class-key nested-name-specifier [opt] template-id
22937 base-clause [opt]
22939 class-virt-specifier:
22940 final
22942 GNU Extensions:
22943 class-key attributes identifier [opt] base-clause [opt]
22944 class-key attributes nested-name-specifier identifier base-clause [opt]
22945 class-key attributes nested-name-specifier [opt] template-id
22946 base-clause [opt]
22948 Upon return BASES is initialized to the list of base classes (or
22949 NULL, if there are none) in the same form returned by
22950 cp_parser_base_clause.
22952 Returns the TYPE of the indicated class. Sets
22953 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22954 involving a nested-name-specifier was used, and FALSE otherwise.
22956 Returns error_mark_node if this is not a class-head.
22958 Returns NULL_TREE if the class-head is syntactically valid, but
22959 semantically invalid in a way that means we should skip the entire
22960 body of the class. */
22962 static tree
22963 cp_parser_class_head (cp_parser* parser,
22964 bool* nested_name_specifier_p)
22966 tree nested_name_specifier;
22967 enum tag_types class_key;
22968 tree id = NULL_TREE;
22969 tree type = NULL_TREE;
22970 tree attributes;
22971 tree bases;
22972 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22973 bool template_id_p = false;
22974 bool qualified_p = false;
22975 bool invalid_nested_name_p = false;
22976 bool invalid_explicit_specialization_p = false;
22977 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22978 tree pushed_scope = NULL_TREE;
22979 unsigned num_templates;
22980 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22981 /* Assume no nested-name-specifier will be present. */
22982 *nested_name_specifier_p = false;
22983 /* Assume no template parameter lists will be used in defining the
22984 type. */
22985 num_templates = 0;
22986 parser->colon_corrects_to_scope_p = false;
22988 /* Look for the class-key. */
22989 class_key = cp_parser_class_key (parser);
22990 if (class_key == none_type)
22991 return error_mark_node;
22993 location_t class_head_start_location = input_location;
22995 /* Parse the attributes. */
22996 attributes = cp_parser_attributes_opt (parser);
22998 /* If the next token is `::', that is invalid -- but sometimes
22999 people do try to write:
23001 struct ::S {};
23003 Handle this gracefully by accepting the extra qualifier, and then
23004 issuing an error about it later if this really is a
23005 class-head. If it turns out just to be an elaborated type
23006 specifier, remain silent. */
23007 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23008 qualified_p = true;
23010 push_deferring_access_checks (dk_no_check);
23012 /* Determine the name of the class. Begin by looking for an
23013 optional nested-name-specifier. */
23014 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23015 nested_name_specifier
23016 = cp_parser_nested_name_specifier_opt (parser,
23017 /*typename_keyword_p=*/false,
23018 /*check_dependency_p=*/false,
23019 /*type_p=*/true,
23020 /*is_declaration=*/false);
23021 /* If there was a nested-name-specifier, then there *must* be an
23022 identifier. */
23024 cp_token *bad_template_keyword = NULL;
23026 if (nested_name_specifier)
23028 type_start_token = cp_lexer_peek_token (parser->lexer);
23029 /* Although the grammar says `identifier', it really means
23030 `class-name' or `template-name'. You are only allowed to
23031 define a class that has already been declared with this
23032 syntax.
23034 The proposed resolution for Core Issue 180 says that wherever
23035 you see `class T::X' you should treat `X' as a type-name.
23037 It is OK to define an inaccessible class; for example:
23039 class A { class B; };
23040 class A::B {};
23042 We do not know if we will see a class-name, or a
23043 template-name. We look for a class-name first, in case the
23044 class-name is a template-id; if we looked for the
23045 template-name first we would stop after the template-name. */
23046 cp_parser_parse_tentatively (parser);
23047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23048 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23049 type = cp_parser_class_name (parser,
23050 /*typename_keyword_p=*/false,
23051 /*template_keyword_p=*/false,
23052 class_type,
23053 /*check_dependency_p=*/false,
23054 /*class_head_p=*/true,
23055 /*is_declaration=*/false);
23056 /* If that didn't work, ignore the nested-name-specifier. */
23057 if (!cp_parser_parse_definitely (parser))
23059 invalid_nested_name_p = true;
23060 type_start_token = cp_lexer_peek_token (parser->lexer);
23061 id = cp_parser_identifier (parser);
23062 if (id == error_mark_node)
23063 id = NULL_TREE;
23065 /* If we could not find a corresponding TYPE, treat this
23066 declaration like an unqualified declaration. */
23067 if (type == error_mark_node)
23068 nested_name_specifier = NULL_TREE;
23069 /* Otherwise, count the number of templates used in TYPE and its
23070 containing scopes. */
23071 else
23072 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23074 /* Otherwise, the identifier is optional. */
23075 else
23077 /* We don't know whether what comes next is a template-id,
23078 an identifier, or nothing at all. */
23079 cp_parser_parse_tentatively (parser);
23080 /* Check for a template-id. */
23081 type_start_token = cp_lexer_peek_token (parser->lexer);
23082 id = cp_parser_template_id (parser,
23083 /*template_keyword_p=*/false,
23084 /*check_dependency_p=*/true,
23085 class_key,
23086 /*is_declaration=*/true);
23087 /* If that didn't work, it could still be an identifier. */
23088 if (!cp_parser_parse_definitely (parser))
23090 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23092 type_start_token = cp_lexer_peek_token (parser->lexer);
23093 id = cp_parser_identifier (parser);
23095 else
23096 id = NULL_TREE;
23098 else
23100 template_id_p = true;
23101 ++num_templates;
23105 pop_deferring_access_checks ();
23107 if (id)
23109 cp_parser_check_for_invalid_template_id (parser, id,
23110 class_key,
23111 type_start_token->location);
23113 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23115 /* If it's not a `:' or a `{' then we can't really be looking at a
23116 class-head, since a class-head only appears as part of a
23117 class-specifier. We have to detect this situation before calling
23118 xref_tag, since that has irreversible side-effects. */
23119 if (!cp_parser_next_token_starts_class_definition_p (parser))
23121 cp_parser_error (parser, "expected %<{%> or %<:%>");
23122 type = error_mark_node;
23123 goto out;
23126 /* At this point, we're going ahead with the class-specifier, even
23127 if some other problem occurs. */
23128 cp_parser_commit_to_tentative_parse (parser);
23129 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23131 cp_parser_error (parser,
23132 "cannot specify %<override%> for a class");
23133 type = error_mark_node;
23134 goto out;
23136 /* Issue the error about the overly-qualified name now. */
23137 if (qualified_p)
23139 cp_parser_error (parser,
23140 "global qualification of class name is invalid");
23141 type = error_mark_node;
23142 goto out;
23144 else if (invalid_nested_name_p)
23146 cp_parser_error (parser,
23147 "qualified name does not name a class");
23148 type = error_mark_node;
23149 goto out;
23151 else if (nested_name_specifier)
23153 tree scope;
23155 if (bad_template_keyword)
23156 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23157 keyword template shall not appear at the top level. */
23158 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23159 "keyword %<template%> not allowed in class-head-name");
23161 /* Reject typedef-names in class heads. */
23162 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23164 error_at (type_start_token->location,
23165 "invalid class name in declaration of %qD",
23166 type);
23167 type = NULL_TREE;
23168 goto done;
23171 /* Figure out in what scope the declaration is being placed. */
23172 scope = current_scope ();
23173 /* If that scope does not contain the scope in which the
23174 class was originally declared, the program is invalid. */
23175 if (scope && !is_ancestor (scope, nested_name_specifier))
23177 if (at_namespace_scope_p ())
23178 error_at (type_start_token->location,
23179 "declaration of %qD in namespace %qD which does not "
23180 "enclose %qD",
23181 type, scope, nested_name_specifier);
23182 else
23183 error_at (type_start_token->location,
23184 "declaration of %qD in %qD which does not enclose %qD",
23185 type, scope, nested_name_specifier);
23186 type = NULL_TREE;
23187 goto done;
23189 /* [dcl.meaning]
23191 A declarator-id shall not be qualified except for the
23192 definition of a ... nested class outside of its class
23193 ... [or] the definition or explicit instantiation of a
23194 class member of a namespace outside of its namespace. */
23195 if (scope == nested_name_specifier)
23197 permerror (nested_name_specifier_token_start->location,
23198 "extra qualification not allowed");
23199 nested_name_specifier = NULL_TREE;
23200 num_templates = 0;
23203 /* An explicit-specialization must be preceded by "template <>". If
23204 it is not, try to recover gracefully. */
23205 if (at_namespace_scope_p ()
23206 && parser->num_template_parameter_lists == 0
23207 && !processing_template_parmlist
23208 && template_id_p)
23210 /* Build a location of this form:
23211 struct typename <ARGS>
23212 ^~~~~~~~~~~~~~~~~~~~~~
23213 with caret==start at the start token, and
23214 finishing at the end of the type. */
23215 location_t reported_loc
23216 = make_location (class_head_start_location,
23217 class_head_start_location,
23218 get_finish (type_start_token->location));
23219 rich_location richloc (line_table, reported_loc);
23220 richloc.add_fixit_insert_before (class_head_start_location,
23221 "template <> ");
23222 error_at (&richloc,
23223 "an explicit specialization must be preceded by"
23224 " %<template <>%>");
23225 invalid_explicit_specialization_p = true;
23226 /* Take the same action that would have been taken by
23227 cp_parser_explicit_specialization. */
23228 ++parser->num_template_parameter_lists;
23229 begin_specialization ();
23231 /* There must be no "return" statements between this point and the
23232 end of this function; set "type "to the correct return value and
23233 use "goto done;" to return. */
23234 /* Make sure that the right number of template parameters were
23235 present. */
23236 if (!cp_parser_check_template_parameters (parser, num_templates,
23237 template_id_p,
23238 type_start_token->location,
23239 /*declarator=*/NULL))
23241 /* If something went wrong, there is no point in even trying to
23242 process the class-definition. */
23243 type = NULL_TREE;
23244 goto done;
23247 /* Look up the type. */
23248 if (template_id_p)
23250 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23251 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23252 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23254 error_at (type_start_token->location,
23255 "function template %qD redeclared as a class template", id);
23256 type = error_mark_node;
23258 else
23260 type = TREE_TYPE (id);
23261 type = maybe_process_partial_specialization (type);
23263 /* Check the scope while we still know whether or not we had a
23264 nested-name-specifier. */
23265 if (type != error_mark_node)
23266 check_unqualified_spec_or_inst (type, type_start_token->location);
23268 if (nested_name_specifier)
23269 pushed_scope = push_scope (nested_name_specifier);
23271 else if (nested_name_specifier)
23273 tree class_type;
23275 /* Given:
23277 template <typename T> struct S { struct T };
23278 template <typename T> struct S<T>::T { };
23280 we will get a TYPENAME_TYPE when processing the definition of
23281 `S::T'. We need to resolve it to the actual type before we
23282 try to define it. */
23283 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23285 class_type = resolve_typename_type (TREE_TYPE (type),
23286 /*only_current_p=*/false);
23287 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23288 type = TYPE_NAME (class_type);
23289 else
23291 cp_parser_error (parser, "could not resolve typename type");
23292 type = error_mark_node;
23296 if (maybe_process_partial_specialization (TREE_TYPE (type))
23297 == error_mark_node)
23299 type = NULL_TREE;
23300 goto done;
23303 class_type = current_class_type;
23304 /* Enter the scope indicated by the nested-name-specifier. */
23305 pushed_scope = push_scope (nested_name_specifier);
23306 /* Get the canonical version of this type. */
23307 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23308 /* Call push_template_decl if it seems like we should be defining a
23309 template either from the template headers or the type we're
23310 defining, so that we diagnose both extra and missing headers. */
23311 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23312 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23313 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23315 type = push_template_decl (type);
23316 if (type == error_mark_node)
23318 type = NULL_TREE;
23319 goto done;
23323 type = TREE_TYPE (type);
23324 *nested_name_specifier_p = true;
23326 else /* The name is not a nested name. */
23328 /* If the class was unnamed, create a dummy name. */
23329 if (!id)
23330 id = make_anon_name ();
23331 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23332 ? ts_within_enclosing_non_class
23333 : ts_current);
23334 type = xref_tag (class_key, id, tag_scope,
23335 parser->num_template_parameter_lists);
23338 /* Indicate whether this class was declared as a `class' or as a
23339 `struct'. */
23340 if (TREE_CODE (type) == RECORD_TYPE)
23341 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23342 cp_parser_check_class_key (class_key, type);
23344 /* If this type was already complete, and we see another definition,
23345 that's an error. */
23346 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23348 error_at (type_start_token->location, "redefinition of %q#T",
23349 type);
23350 inform (location_of (type), "previous definition of %q#T",
23351 type);
23352 type = NULL_TREE;
23353 goto done;
23355 else if (type == error_mark_node)
23356 type = NULL_TREE;
23358 if (type)
23360 /* Apply attributes now, before any use of the class as a template
23361 argument in its base list. */
23362 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23363 fixup_attribute_variants (type);
23366 /* We will have entered the scope containing the class; the names of
23367 base classes should be looked up in that context. For example:
23369 struct A { struct B {}; struct C; };
23370 struct A::C : B {};
23372 is valid. */
23374 /* Get the list of base-classes, if there is one. */
23375 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23377 /* PR59482: enter the class scope so that base-specifiers are looked
23378 up correctly. */
23379 if (type)
23380 pushclass (type);
23381 bases = cp_parser_base_clause (parser);
23382 /* PR59482: get out of the previously pushed class scope so that the
23383 subsequent pops pop the right thing. */
23384 if (type)
23385 popclass ();
23387 else
23388 bases = NULL_TREE;
23390 /* If we're really defining a class, process the base classes.
23391 If they're invalid, fail. */
23392 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23393 xref_basetypes (type, bases);
23395 done:
23396 /* Leave the scope given by the nested-name-specifier. We will
23397 enter the class scope itself while processing the members. */
23398 if (pushed_scope)
23399 pop_scope (pushed_scope);
23401 if (invalid_explicit_specialization_p)
23403 end_specialization ();
23404 --parser->num_template_parameter_lists;
23407 if (type)
23408 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23409 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23410 CLASSTYPE_FINAL (type) = 1;
23411 out:
23412 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23413 return type;
23416 /* Parse a class-key.
23418 class-key:
23419 class
23420 struct
23421 union
23423 Returns the kind of class-key specified, or none_type to indicate
23424 error. */
23426 static enum tag_types
23427 cp_parser_class_key (cp_parser* parser)
23429 cp_token *token;
23430 enum tag_types tag_type;
23432 /* Look for the class-key. */
23433 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23434 if (!token)
23435 return none_type;
23437 /* Check to see if the TOKEN is a class-key. */
23438 tag_type = cp_parser_token_is_class_key (token);
23439 if (!tag_type)
23440 cp_parser_error (parser, "expected class-key");
23441 return tag_type;
23444 /* Parse a type-parameter-key.
23446 type-parameter-key:
23447 class
23448 typename
23451 static void
23452 cp_parser_type_parameter_key (cp_parser* parser)
23454 /* Look for the type-parameter-key. */
23455 enum tag_types tag_type = none_type;
23456 cp_token *token = cp_lexer_peek_token (parser->lexer);
23457 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23459 cp_lexer_consume_token (parser->lexer);
23460 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23461 /* typename is not allowed in a template template parameter
23462 by the standard until C++17. */
23463 pedwarn (token->location, OPT_Wpedantic,
23464 "ISO C++ forbids typename key in template template parameter;"
23465 " use -std=c++17 or -std=gnu++17");
23467 else
23468 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23470 return;
23473 /* Parse an (optional) member-specification.
23475 member-specification:
23476 member-declaration member-specification [opt]
23477 access-specifier : member-specification [opt] */
23479 static void
23480 cp_parser_member_specification_opt (cp_parser* parser)
23482 while (true)
23484 cp_token *token;
23485 enum rid keyword;
23487 /* Peek at the next token. */
23488 token = cp_lexer_peek_token (parser->lexer);
23489 /* If it's a `}', or EOF then we've seen all the members. */
23490 if (token->type == CPP_CLOSE_BRACE
23491 || token->type == CPP_EOF
23492 || token->type == CPP_PRAGMA_EOL)
23493 break;
23495 /* See if this token is a keyword. */
23496 keyword = token->keyword;
23497 switch (keyword)
23499 case RID_PUBLIC:
23500 case RID_PROTECTED:
23501 case RID_PRIVATE:
23502 /* Consume the access-specifier. */
23503 cp_lexer_consume_token (parser->lexer);
23504 /* Remember which access-specifier is active. */
23505 current_access_specifier = token->u.value;
23506 /* Look for the `:'. */
23507 cp_parser_require (parser, CPP_COLON, RT_COLON);
23508 break;
23510 default:
23511 /* Accept #pragmas at class scope. */
23512 if (token->type == CPP_PRAGMA)
23514 cp_parser_pragma (parser, pragma_member, NULL);
23515 break;
23518 /* Otherwise, the next construction must be a
23519 member-declaration. */
23520 cp_parser_member_declaration (parser);
23525 /* Parse a member-declaration.
23527 member-declaration:
23528 decl-specifier-seq [opt] member-declarator-list [opt] ;
23529 function-definition ; [opt]
23530 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23531 using-declaration
23532 template-declaration
23533 alias-declaration
23535 member-declarator-list:
23536 member-declarator
23537 member-declarator-list , member-declarator
23539 member-declarator:
23540 declarator pure-specifier [opt]
23541 declarator constant-initializer [opt]
23542 identifier [opt] : constant-expression
23544 GNU Extensions:
23546 member-declaration:
23547 __extension__ member-declaration
23549 member-declarator:
23550 declarator attributes [opt] pure-specifier [opt]
23551 declarator attributes [opt] constant-initializer [opt]
23552 identifier [opt] attributes [opt] : constant-expression
23554 C++0x Extensions:
23556 member-declaration:
23557 static_assert-declaration */
23559 static void
23560 cp_parser_member_declaration (cp_parser* parser)
23562 cp_decl_specifier_seq decl_specifiers;
23563 tree prefix_attributes;
23564 tree decl;
23565 int declares_class_or_enum;
23566 bool friend_p;
23567 cp_token *token = NULL;
23568 cp_token *decl_spec_token_start = NULL;
23569 cp_token *initializer_token_start = NULL;
23570 int saved_pedantic;
23571 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23573 /* Check for the `__extension__' keyword. */
23574 if (cp_parser_extension_opt (parser, &saved_pedantic))
23576 /* Recurse. */
23577 cp_parser_member_declaration (parser);
23578 /* Restore the old value of the PEDANTIC flag. */
23579 pedantic = saved_pedantic;
23581 return;
23584 /* Check for a template-declaration. */
23585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23587 /* An explicit specialization here is an error condition, and we
23588 expect the specialization handler to detect and report this. */
23589 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23590 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23591 cp_parser_explicit_specialization (parser);
23592 else
23593 cp_parser_template_declaration (parser, /*member_p=*/true);
23595 return;
23597 /* Check for a template introduction. */
23598 else if (cp_parser_template_declaration_after_export (parser, true))
23599 return;
23601 /* Check for a using-declaration. */
23602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23604 if (cxx_dialect < cxx11)
23606 /* Parse the using-declaration. */
23607 cp_parser_using_declaration (parser,
23608 /*access_declaration_p=*/false);
23609 return;
23611 else
23613 tree decl;
23614 bool alias_decl_expected;
23615 cp_parser_parse_tentatively (parser);
23616 decl = cp_parser_alias_declaration (parser);
23617 /* Note that if we actually see the '=' token after the
23618 identifier, cp_parser_alias_declaration commits the
23619 tentative parse. In that case, we really expect an
23620 alias-declaration. Otherwise, we expect a using
23621 declaration. */
23622 alias_decl_expected =
23623 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23624 cp_parser_parse_definitely (parser);
23626 if (alias_decl_expected)
23627 finish_member_declaration (decl);
23628 else
23629 cp_parser_using_declaration (parser,
23630 /*access_declaration_p=*/false);
23631 return;
23635 /* Check for @defs. */
23636 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23638 tree ivar, member;
23639 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23640 ivar = ivar_chains;
23641 while (ivar)
23643 member = ivar;
23644 ivar = TREE_CHAIN (member);
23645 TREE_CHAIN (member) = NULL_TREE;
23646 finish_member_declaration (member);
23648 return;
23651 /* If the next token is `static_assert' we have a static assertion. */
23652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23654 cp_parser_static_assert (parser, /*member_p=*/true);
23655 return;
23658 parser->colon_corrects_to_scope_p = false;
23660 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23661 goto out;
23663 /* Parse the decl-specifier-seq. */
23664 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23665 cp_parser_decl_specifier_seq (parser,
23666 CP_PARSER_FLAGS_OPTIONAL,
23667 &decl_specifiers,
23668 &declares_class_or_enum);
23669 /* Check for an invalid type-name. */
23670 if (!decl_specifiers.any_type_specifiers_p
23671 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23672 goto out;
23673 /* If there is no declarator, then the decl-specifier-seq should
23674 specify a type. */
23675 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23677 /* If there was no decl-specifier-seq, and the next token is a
23678 `;', then we have something like:
23680 struct S { ; };
23682 [class.mem]
23684 Each member-declaration shall declare at least one member
23685 name of the class. */
23686 if (!decl_specifiers.any_specifiers_p)
23688 cp_token *token = cp_lexer_peek_token (parser->lexer);
23689 if (!in_system_header_at (token->location))
23691 gcc_rich_location richloc (token->location);
23692 richloc.add_fixit_remove ();
23693 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23696 else
23698 tree type;
23700 /* See if this declaration is a friend. */
23701 friend_p = cp_parser_friend_p (&decl_specifiers);
23702 /* If there were decl-specifiers, check to see if there was
23703 a class-declaration. */
23704 type = check_tag_decl (&decl_specifiers,
23705 /*explicit_type_instantiation_p=*/false);
23706 /* Nested classes have already been added to the class, but
23707 a `friend' needs to be explicitly registered. */
23708 if (friend_p)
23710 /* If the `friend' keyword was present, the friend must
23711 be introduced with a class-key. */
23712 if (!declares_class_or_enum && cxx_dialect < cxx11)
23713 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23714 "in C++03 a class-key must be used "
23715 "when declaring a friend");
23716 /* In this case:
23718 template <typename T> struct A {
23719 friend struct A<T>::B;
23722 A<T>::B will be represented by a TYPENAME_TYPE, and
23723 therefore not recognized by check_tag_decl. */
23724 if (!type)
23726 type = decl_specifiers.type;
23727 if (type && TREE_CODE (type) == TYPE_DECL)
23728 type = TREE_TYPE (type);
23730 if (!type || !TYPE_P (type))
23731 error_at (decl_spec_token_start->location,
23732 "friend declaration does not name a class or "
23733 "function");
23734 else
23735 make_friend_class (current_class_type, type,
23736 /*complain=*/true);
23738 /* If there is no TYPE, an error message will already have
23739 been issued. */
23740 else if (!type || type == error_mark_node)
23742 /* An anonymous aggregate has to be handled specially; such
23743 a declaration really declares a data member (with a
23744 particular type), as opposed to a nested class. */
23745 else if (ANON_AGGR_TYPE_P (type))
23747 /* C++11 9.5/6. */
23748 if (decl_specifiers.storage_class != sc_none)
23749 error_at (decl_spec_token_start->location,
23750 "a storage class on an anonymous aggregate "
23751 "in class scope is not allowed");
23753 /* Remove constructors and such from TYPE, now that we
23754 know it is an anonymous aggregate. */
23755 fixup_anonymous_aggr (type);
23756 /* And make the corresponding data member. */
23757 decl = build_decl (decl_spec_token_start->location,
23758 FIELD_DECL, NULL_TREE, type);
23759 /* Add it to the class. */
23760 finish_member_declaration (decl);
23762 else
23763 cp_parser_check_access_in_redeclaration
23764 (TYPE_NAME (type),
23765 decl_spec_token_start->location);
23768 else
23770 bool assume_semicolon = false;
23772 /* Clear attributes from the decl_specifiers but keep them
23773 around as prefix attributes that apply them to the entity
23774 being declared. */
23775 prefix_attributes = decl_specifiers.attributes;
23776 decl_specifiers.attributes = NULL_TREE;
23778 /* See if these declarations will be friends. */
23779 friend_p = cp_parser_friend_p (&decl_specifiers);
23781 /* Keep going until we hit the `;' at the end of the
23782 declaration. */
23783 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23785 tree attributes = NULL_TREE;
23786 tree first_attribute;
23787 tree initializer;
23788 bool named_bitfld = false;
23790 /* Peek at the next token. */
23791 token = cp_lexer_peek_token (parser->lexer);
23793 /* The following code wants to know early if it is a bit-field
23794 or some other declaration. Attributes can appear before
23795 the `:' token. Skip over them without consuming any tokens
23796 to peek if they are followed by `:'. */
23797 if (cp_next_tokens_can_be_attribute_p (parser)
23798 || (token->type == CPP_NAME
23799 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23800 && (named_bitfld = true)))
23802 size_t n
23803 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23804 token = cp_lexer_peek_nth_token (parser->lexer, n);
23807 /* Check for a bitfield declaration. */
23808 if (token->type == CPP_COLON
23809 || (token->type == CPP_NAME
23810 && token == cp_lexer_peek_token (parser->lexer)
23811 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23812 && (named_bitfld = true)))
23814 tree identifier;
23815 tree width;
23816 tree late_attributes = NULL_TREE;
23818 if (named_bitfld)
23819 identifier = cp_parser_identifier (parser);
23820 else
23821 identifier = NULL_TREE;
23823 /* Look for attributes that apply to the bitfield. */
23824 attributes = cp_parser_attributes_opt (parser);
23826 /* Consume the `:' token. */
23827 cp_lexer_consume_token (parser->lexer);
23829 /* Get the width of the bitfield. */
23830 width = cp_parser_constant_expression (parser, false, NULL,
23831 cxx_dialect >= cxx11);
23833 /* In C++2A and as extension for C++11 and above we allow
23834 default member initializers for bit-fields. */
23835 initializer = NULL_TREE;
23836 if (cxx_dialect >= cxx11
23837 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23838 || cp_lexer_next_token_is (parser->lexer,
23839 CPP_OPEN_BRACE)))
23841 location_t loc
23842 = cp_lexer_peek_token (parser->lexer)->location;
23843 if (cxx_dialect < cxx2a
23844 && !in_system_header_at (loc)
23845 && identifier != NULL_TREE)
23846 pedwarn (loc, 0,
23847 "default member initializers for bit-fields "
23848 "only available with -std=c++2a or "
23849 "-std=gnu++2a");
23851 initializer = cp_parser_save_nsdmi (parser);
23852 if (identifier == NULL_TREE)
23854 error_at (loc, "default member initializer for "
23855 "unnamed bit-field");
23856 initializer = NULL_TREE;
23859 else
23861 /* Look for attributes that apply to the bitfield after
23862 the `:' token and width. This is where GCC used to
23863 parse attributes in the past, pedwarn if there is
23864 a std attribute. */
23865 if (cp_next_tokens_can_be_std_attribute_p (parser))
23866 pedwarn (input_location, OPT_Wpedantic,
23867 "ISO C++ allows bit-field attributes only "
23868 "before the %<:%> token");
23870 late_attributes = cp_parser_attributes_opt (parser);
23873 attributes = attr_chainon (attributes, late_attributes);
23875 /* Remember which attributes are prefix attributes and
23876 which are not. */
23877 first_attribute = attributes;
23878 /* Combine the attributes. */
23879 attributes = attr_chainon (prefix_attributes, attributes);
23881 /* Create the bitfield declaration. */
23882 decl = grokbitfield (identifier
23883 ? make_id_declarator (NULL_TREE,
23884 identifier,
23885 sfk_none)
23886 : NULL,
23887 &decl_specifiers,
23888 width, initializer,
23889 attributes);
23891 else
23893 cp_declarator *declarator;
23894 tree asm_specification;
23895 int ctor_dtor_or_conv_p;
23897 /* Parse the declarator. */
23898 declarator
23899 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23900 &ctor_dtor_or_conv_p,
23901 /*parenthesized_p=*/NULL,
23902 /*member_p=*/true,
23903 friend_p);
23905 /* If something went wrong parsing the declarator, make sure
23906 that we at least consume some tokens. */
23907 if (declarator == cp_error_declarator)
23909 /* Skip to the end of the statement. */
23910 cp_parser_skip_to_end_of_statement (parser);
23911 /* If the next token is not a semicolon, that is
23912 probably because we just skipped over the body of
23913 a function. So, we consume a semicolon if
23914 present, but do not issue an error message if it
23915 is not present. */
23916 if (cp_lexer_next_token_is (parser->lexer,
23917 CPP_SEMICOLON))
23918 cp_lexer_consume_token (parser->lexer);
23919 goto out;
23922 if (declares_class_or_enum & 2)
23923 cp_parser_check_for_definition_in_return_type
23924 (declarator, decl_specifiers.type,
23925 decl_specifiers.locations[ds_type_spec]);
23927 /* Look for an asm-specification. */
23928 asm_specification = cp_parser_asm_specification_opt (parser);
23929 /* Look for attributes that apply to the declaration. */
23930 attributes = cp_parser_attributes_opt (parser);
23931 /* Remember which attributes are prefix attributes and
23932 which are not. */
23933 first_attribute = attributes;
23934 /* Combine the attributes. */
23935 attributes = attr_chainon (prefix_attributes, attributes);
23937 /* If it's an `=', then we have a constant-initializer or a
23938 pure-specifier. It is not correct to parse the
23939 initializer before registering the member declaration
23940 since the member declaration should be in scope while
23941 its initializer is processed. However, the rest of the
23942 front end does not yet provide an interface that allows
23943 us to handle this correctly. */
23944 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23946 /* In [class.mem]:
23948 A pure-specifier shall be used only in the declaration of
23949 a virtual function.
23951 A member-declarator can contain a constant-initializer
23952 only if it declares a static member of integral or
23953 enumeration type.
23955 Therefore, if the DECLARATOR is for a function, we look
23956 for a pure-specifier; otherwise, we look for a
23957 constant-initializer. When we call `grokfield', it will
23958 perform more stringent semantics checks. */
23959 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23960 if (function_declarator_p (declarator)
23961 || (decl_specifiers.type
23962 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23963 && declarator->kind == cdk_id
23964 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23965 == FUNCTION_TYPE)))
23966 initializer = cp_parser_pure_specifier (parser);
23967 else if (decl_specifiers.storage_class != sc_static)
23968 initializer = cp_parser_save_nsdmi (parser);
23969 else if (cxx_dialect >= cxx11)
23971 bool nonconst;
23972 /* Don't require a constant rvalue in C++11, since we
23973 might want a reference constant. We'll enforce
23974 constancy later. */
23975 cp_lexer_consume_token (parser->lexer);
23976 /* Parse the initializer. */
23977 initializer = cp_parser_initializer_clause (parser,
23978 &nonconst);
23980 else
23981 /* Parse the initializer. */
23982 initializer = cp_parser_constant_initializer (parser);
23984 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23985 && !function_declarator_p (declarator))
23987 bool x;
23988 if (decl_specifiers.storage_class != sc_static)
23989 initializer = cp_parser_save_nsdmi (parser);
23990 else
23991 initializer = cp_parser_initializer (parser, &x, &x);
23993 /* Otherwise, there is no initializer. */
23994 else
23995 initializer = NULL_TREE;
23997 /* See if we are probably looking at a function
23998 definition. We are certainly not looking at a
23999 member-declarator. Calling `grokfield' has
24000 side-effects, so we must not do it unless we are sure
24001 that we are looking at a member-declarator. */
24002 if (cp_parser_token_starts_function_definition_p
24003 (cp_lexer_peek_token (parser->lexer)))
24005 /* The grammar does not allow a pure-specifier to be
24006 used when a member function is defined. (It is
24007 possible that this fact is an oversight in the
24008 standard, since a pure function may be defined
24009 outside of the class-specifier. */
24010 if (initializer && initializer_token_start)
24011 error_at (initializer_token_start->location,
24012 "pure-specifier on function-definition");
24013 decl = cp_parser_save_member_function_body (parser,
24014 &decl_specifiers,
24015 declarator,
24016 attributes);
24017 if (parser->fully_implicit_function_template_p)
24018 decl = finish_fully_implicit_template (parser, decl);
24019 /* If the member was not a friend, declare it here. */
24020 if (!friend_p)
24021 finish_member_declaration (decl);
24022 /* Peek at the next token. */
24023 token = cp_lexer_peek_token (parser->lexer);
24024 /* If the next token is a semicolon, consume it. */
24025 if (token->type == CPP_SEMICOLON)
24027 location_t semicolon_loc
24028 = cp_lexer_consume_token (parser->lexer)->location;
24029 gcc_rich_location richloc (semicolon_loc);
24030 richloc.add_fixit_remove ();
24031 warning_at (&richloc, OPT_Wextra_semi,
24032 "extra %<;%> after in-class "
24033 "function definition");
24035 goto out;
24037 else
24038 if (declarator->kind == cdk_function)
24039 declarator->id_loc = token->location;
24040 /* Create the declaration. */
24041 decl = grokfield (declarator, &decl_specifiers,
24042 initializer, /*init_const_expr_p=*/true,
24043 asm_specification, attributes);
24044 if (parser->fully_implicit_function_template_p)
24046 if (friend_p)
24047 finish_fully_implicit_template (parser, 0);
24048 else
24049 decl = finish_fully_implicit_template (parser, decl);
24053 cp_finalize_omp_declare_simd (parser, decl);
24054 cp_finalize_oacc_routine (parser, decl, false);
24056 /* Reset PREFIX_ATTRIBUTES. */
24057 if (attributes != error_mark_node)
24059 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24060 attributes = TREE_CHAIN (attributes);
24061 if (attributes)
24062 TREE_CHAIN (attributes) = NULL_TREE;
24065 /* If there is any qualification still in effect, clear it
24066 now; we will be starting fresh with the next declarator. */
24067 parser->scope = NULL_TREE;
24068 parser->qualifying_scope = NULL_TREE;
24069 parser->object_scope = NULL_TREE;
24070 /* If it's a `,', then there are more declarators. */
24071 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24073 cp_lexer_consume_token (parser->lexer);
24074 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24076 cp_token *token = cp_lexer_previous_token (parser->lexer);
24077 gcc_rich_location richloc (token->location);
24078 richloc.add_fixit_remove ();
24079 error_at (&richloc, "stray %<,%> at end of "
24080 "member declaration");
24083 /* If the next token isn't a `;', then we have a parse error. */
24084 else if (cp_lexer_next_token_is_not (parser->lexer,
24085 CPP_SEMICOLON))
24087 /* The next token might be a ways away from where the
24088 actual semicolon is missing. Find the previous token
24089 and use that for our error position. */
24090 cp_token *token = cp_lexer_previous_token (parser->lexer);
24091 gcc_rich_location richloc (token->location);
24092 richloc.add_fixit_insert_after (";");
24093 error_at (&richloc, "expected %<;%> at end of "
24094 "member declaration");
24096 /* Assume that the user meant to provide a semicolon. If
24097 we were to cp_parser_skip_to_end_of_statement, we might
24098 skip to a semicolon inside a member function definition
24099 and issue nonsensical error messages. */
24100 assume_semicolon = true;
24103 if (decl)
24105 /* Add DECL to the list of members. */
24106 if (!friend_p
24107 /* Explicitly include, eg, NSDMIs, for better error
24108 recovery (c++/58650). */
24109 || !DECL_DECLARES_FUNCTION_P (decl))
24110 finish_member_declaration (decl);
24112 if (TREE_CODE (decl) == FUNCTION_DECL)
24113 cp_parser_save_default_args (parser, decl);
24114 else if (TREE_CODE (decl) == FIELD_DECL
24115 && DECL_INITIAL (decl))
24116 /* Add DECL to the queue of NSDMI to be parsed later. */
24117 vec_safe_push (unparsed_nsdmis, decl);
24120 if (assume_semicolon)
24121 goto out;
24125 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24126 out:
24127 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24130 /* Parse a pure-specifier.
24132 pure-specifier:
24135 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24136 Otherwise, ERROR_MARK_NODE is returned. */
24138 static tree
24139 cp_parser_pure_specifier (cp_parser* parser)
24141 cp_token *token;
24143 /* Look for the `=' token. */
24144 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24145 return error_mark_node;
24146 /* Look for the `0' token. */
24147 token = cp_lexer_peek_token (parser->lexer);
24149 if (token->type == CPP_EOF
24150 || token->type == CPP_PRAGMA_EOL)
24151 return error_mark_node;
24153 cp_lexer_consume_token (parser->lexer);
24155 /* Accept = default or = delete in c++0x mode. */
24156 if (token->keyword == RID_DEFAULT
24157 || token->keyword == RID_DELETE)
24159 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24160 return token->u.value;
24163 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24164 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24166 cp_parser_error (parser,
24167 "invalid pure specifier (only %<= 0%> is allowed)");
24168 cp_parser_skip_to_end_of_statement (parser);
24169 return error_mark_node;
24171 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24173 error_at (token->location, "templates may not be %<virtual%>");
24174 return error_mark_node;
24177 return integer_zero_node;
24180 /* Parse a constant-initializer.
24182 constant-initializer:
24183 = constant-expression
24185 Returns a representation of the constant-expression. */
24187 static tree
24188 cp_parser_constant_initializer (cp_parser* parser)
24190 /* Look for the `=' token. */
24191 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24192 return error_mark_node;
24194 /* It is invalid to write:
24196 struct S { static const int i = { 7 }; };
24199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24201 cp_parser_error (parser,
24202 "a brace-enclosed initializer is not allowed here");
24203 /* Consume the opening brace. */
24204 matching_braces braces;
24205 braces.consume_open (parser);
24206 /* Skip the initializer. */
24207 cp_parser_skip_to_closing_brace (parser);
24208 /* Look for the trailing `}'. */
24209 braces.require_close (parser);
24211 return error_mark_node;
24214 return cp_parser_constant_expression (parser);
24217 /* Derived classes [gram.class.derived] */
24219 /* Parse a base-clause.
24221 base-clause:
24222 : base-specifier-list
24224 base-specifier-list:
24225 base-specifier ... [opt]
24226 base-specifier-list , base-specifier ... [opt]
24228 Returns a TREE_LIST representing the base-classes, in the order in
24229 which they were declared. The representation of each node is as
24230 described by cp_parser_base_specifier.
24232 In the case that no bases are specified, this function will return
24233 NULL_TREE, not ERROR_MARK_NODE. */
24235 static tree
24236 cp_parser_base_clause (cp_parser* parser)
24238 tree bases = NULL_TREE;
24240 /* Look for the `:' that begins the list. */
24241 cp_parser_require (parser, CPP_COLON, RT_COLON);
24243 /* Scan the base-specifier-list. */
24244 while (true)
24246 cp_token *token;
24247 tree base;
24248 bool pack_expansion_p = false;
24250 /* Look for the base-specifier. */
24251 base = cp_parser_base_specifier (parser);
24252 /* Look for the (optional) ellipsis. */
24253 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24255 /* Consume the `...'. */
24256 cp_lexer_consume_token (parser->lexer);
24258 pack_expansion_p = true;
24261 /* Add BASE to the front of the list. */
24262 if (base && base != error_mark_node)
24264 if (pack_expansion_p)
24265 /* Make this a pack expansion type. */
24266 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24268 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24270 TREE_CHAIN (base) = bases;
24271 bases = base;
24274 /* Peek at the next token. */
24275 token = cp_lexer_peek_token (parser->lexer);
24276 /* If it's not a comma, then the list is complete. */
24277 if (token->type != CPP_COMMA)
24278 break;
24279 /* Consume the `,'. */
24280 cp_lexer_consume_token (parser->lexer);
24283 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24284 base class had a qualified name. However, the next name that
24285 appears is certainly not qualified. */
24286 parser->scope = NULL_TREE;
24287 parser->qualifying_scope = NULL_TREE;
24288 parser->object_scope = NULL_TREE;
24290 return nreverse (bases);
24293 /* Parse a base-specifier.
24295 base-specifier:
24296 :: [opt] nested-name-specifier [opt] class-name
24297 virtual access-specifier [opt] :: [opt] nested-name-specifier
24298 [opt] class-name
24299 access-specifier virtual [opt] :: [opt] nested-name-specifier
24300 [opt] class-name
24302 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24303 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24304 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24305 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24307 static tree
24308 cp_parser_base_specifier (cp_parser* parser)
24310 cp_token *token;
24311 bool done = false;
24312 bool virtual_p = false;
24313 bool duplicate_virtual_error_issued_p = false;
24314 bool duplicate_access_error_issued_p = false;
24315 bool class_scope_p, template_p;
24316 tree access = access_default_node;
24317 tree type;
24319 /* Process the optional `virtual' and `access-specifier'. */
24320 while (!done)
24322 /* Peek at the next token. */
24323 token = cp_lexer_peek_token (parser->lexer);
24324 /* Process `virtual'. */
24325 switch (token->keyword)
24327 case RID_VIRTUAL:
24328 /* If `virtual' appears more than once, issue an error. */
24329 if (virtual_p && !duplicate_virtual_error_issued_p)
24331 cp_parser_error (parser,
24332 "%<virtual%> specified more than once in base-specifier");
24333 duplicate_virtual_error_issued_p = true;
24336 virtual_p = true;
24338 /* Consume the `virtual' token. */
24339 cp_lexer_consume_token (parser->lexer);
24341 break;
24343 case RID_PUBLIC:
24344 case RID_PROTECTED:
24345 case RID_PRIVATE:
24346 /* If more than one access specifier appears, issue an
24347 error. */
24348 if (access != access_default_node
24349 && !duplicate_access_error_issued_p)
24351 cp_parser_error (parser,
24352 "more than one access specifier in base-specifier");
24353 duplicate_access_error_issued_p = true;
24356 access = ridpointers[(int) token->keyword];
24358 /* Consume the access-specifier. */
24359 cp_lexer_consume_token (parser->lexer);
24361 break;
24363 default:
24364 done = true;
24365 break;
24368 /* It is not uncommon to see programs mechanically, erroneously, use
24369 the 'typename' keyword to denote (dependent) qualified types
24370 as base classes. */
24371 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24373 token = cp_lexer_peek_token (parser->lexer);
24374 if (!processing_template_decl)
24375 error_at (token->location,
24376 "keyword %<typename%> not allowed outside of templates");
24377 else
24378 error_at (token->location,
24379 "keyword %<typename%> not allowed in this context "
24380 "(the base class is implicitly a type)");
24381 cp_lexer_consume_token (parser->lexer);
24384 /* Look for the optional `::' operator. */
24385 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24386 /* Look for the nested-name-specifier. The simplest way to
24387 implement:
24389 [temp.res]
24391 The keyword `typename' is not permitted in a base-specifier or
24392 mem-initializer; in these contexts a qualified name that
24393 depends on a template-parameter is implicitly assumed to be a
24394 type name.
24396 is to pretend that we have seen the `typename' keyword at this
24397 point. */
24398 cp_parser_nested_name_specifier_opt (parser,
24399 /*typename_keyword_p=*/true,
24400 /*check_dependency_p=*/true,
24401 /*type_p=*/true,
24402 /*is_declaration=*/true);
24403 /* If the base class is given by a qualified name, assume that names
24404 we see are type names or templates, as appropriate. */
24405 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24406 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24408 if (!parser->scope
24409 && cp_lexer_next_token_is_decltype (parser->lexer))
24410 /* DR 950 allows decltype as a base-specifier. */
24411 type = cp_parser_decltype (parser);
24412 else
24414 /* Otherwise, look for the class-name. */
24415 type = cp_parser_class_name (parser,
24416 class_scope_p,
24417 template_p,
24418 typename_type,
24419 /*check_dependency_p=*/true,
24420 /*class_head_p=*/false,
24421 /*is_declaration=*/true);
24422 type = TREE_TYPE (type);
24425 if (type == error_mark_node)
24426 return error_mark_node;
24428 return finish_base_specifier (type, access, virtual_p);
24431 /* Exception handling [gram.exception] */
24433 /* Parse an (optional) noexcept-specification.
24435 noexcept-specification:
24436 noexcept ( constant-expression ) [opt]
24438 If no noexcept-specification is present, returns NULL_TREE.
24439 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24440 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24441 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24442 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24443 in which case a boolean condition is returned instead. */
24445 static tree
24446 cp_parser_noexcept_specification_opt (cp_parser* parser,
24447 bool require_constexpr,
24448 bool* consumed_expr,
24449 bool return_cond)
24451 cp_token *token;
24452 const char *saved_message;
24454 /* Peek at the next token. */
24455 token = cp_lexer_peek_token (parser->lexer);
24457 /* Is it a noexcept-specification? */
24458 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24460 tree expr;
24461 cp_lexer_consume_token (parser->lexer);
24463 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24465 matching_parens parens;
24466 parens.consume_open (parser);
24468 if (require_constexpr)
24470 /* Types may not be defined in an exception-specification. */
24471 saved_message = parser->type_definition_forbidden_message;
24472 parser->type_definition_forbidden_message
24473 = G_("types may not be defined in an exception-specification");
24475 expr = cp_parser_constant_expression (parser);
24477 /* Restore the saved message. */
24478 parser->type_definition_forbidden_message = saved_message;
24480 else
24482 expr = cp_parser_expression (parser);
24483 *consumed_expr = true;
24486 parens.require_close (parser);
24488 else
24490 expr = boolean_true_node;
24491 if (!require_constexpr)
24492 *consumed_expr = false;
24495 /* We cannot build a noexcept-spec right away because this will check
24496 that expr is a constexpr. */
24497 if (!return_cond)
24498 return build_noexcept_spec (expr, tf_warning_or_error);
24499 else
24500 return expr;
24502 else
24503 return NULL_TREE;
24506 /* Parse an (optional) exception-specification.
24508 exception-specification:
24509 throw ( type-id-list [opt] )
24511 Returns a TREE_LIST representing the exception-specification. The
24512 TREE_VALUE of each node is a type. */
24514 static tree
24515 cp_parser_exception_specification_opt (cp_parser* parser)
24517 cp_token *token;
24518 tree type_id_list;
24519 const char *saved_message;
24521 /* Peek at the next token. */
24522 token = cp_lexer_peek_token (parser->lexer);
24524 /* Is it a noexcept-specification? */
24525 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24526 false);
24527 if (type_id_list != NULL_TREE)
24528 return type_id_list;
24530 /* If it's not `throw', then there's no exception-specification. */
24531 if (!cp_parser_is_keyword (token, RID_THROW))
24532 return NULL_TREE;
24534 location_t loc = token->location;
24536 /* Consume the `throw'. */
24537 cp_lexer_consume_token (parser->lexer);
24539 /* Look for the `('. */
24540 matching_parens parens;
24541 parens.require_open (parser);
24543 /* Peek at the next token. */
24544 token = cp_lexer_peek_token (parser->lexer);
24545 /* If it's not a `)', then there is a type-id-list. */
24546 if (token->type != CPP_CLOSE_PAREN)
24548 /* Types may not be defined in an exception-specification. */
24549 saved_message = parser->type_definition_forbidden_message;
24550 parser->type_definition_forbidden_message
24551 = G_("types may not be defined in an exception-specification");
24552 /* Parse the type-id-list. */
24553 type_id_list = cp_parser_type_id_list (parser);
24554 /* Restore the saved message. */
24555 parser->type_definition_forbidden_message = saved_message;
24557 if (cxx_dialect >= cxx17)
24559 error_at (loc, "ISO C++17 does not allow dynamic exception "
24560 "specifications");
24561 type_id_list = NULL_TREE;
24563 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24564 warning_at (loc, OPT_Wdeprecated,
24565 "dynamic exception specifications are deprecated in "
24566 "C++11");
24568 /* In C++17, throw() is equivalent to noexcept (true). throw()
24569 is deprecated in C++11 and above as well, but is still widely used,
24570 so don't warn about it yet. */
24571 else if (cxx_dialect >= cxx17)
24572 type_id_list = noexcept_true_spec;
24573 else
24574 type_id_list = empty_except_spec;
24576 /* Look for the `)'. */
24577 parens.require_close (parser);
24579 return type_id_list;
24582 /* Parse an (optional) type-id-list.
24584 type-id-list:
24585 type-id ... [opt]
24586 type-id-list , type-id ... [opt]
24588 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24589 in the order that the types were presented. */
24591 static tree
24592 cp_parser_type_id_list (cp_parser* parser)
24594 tree types = NULL_TREE;
24596 while (true)
24598 cp_token *token;
24599 tree type;
24601 token = cp_lexer_peek_token (parser->lexer);
24603 /* Get the next type-id. */
24604 type = cp_parser_type_id (parser);
24605 /* Check for invalid 'auto'. */
24606 if (flag_concepts && type_uses_auto (type))
24608 error_at (token->location,
24609 "invalid use of %<auto%> in exception-specification");
24610 type = error_mark_node;
24612 /* Parse the optional ellipsis. */
24613 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24615 /* Consume the `...'. */
24616 cp_lexer_consume_token (parser->lexer);
24618 /* Turn the type into a pack expansion expression. */
24619 type = make_pack_expansion (type);
24621 /* Add it to the list. */
24622 types = add_exception_specifier (types, type, /*complain=*/1);
24623 /* Peek at the next token. */
24624 token = cp_lexer_peek_token (parser->lexer);
24625 /* If it is not a `,', we are done. */
24626 if (token->type != CPP_COMMA)
24627 break;
24628 /* Consume the `,'. */
24629 cp_lexer_consume_token (parser->lexer);
24632 return nreverse (types);
24635 /* Parse a try-block.
24637 try-block:
24638 try compound-statement handler-seq */
24640 static tree
24641 cp_parser_try_block (cp_parser* parser)
24643 tree try_block;
24645 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24646 if (parser->in_function_body
24647 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24648 error ("%<try%> in %<constexpr%> function");
24650 try_block = begin_try_block ();
24651 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24652 finish_try_block (try_block);
24653 cp_parser_handler_seq (parser);
24654 finish_handler_sequence (try_block);
24656 return try_block;
24659 /* Parse a function-try-block.
24661 function-try-block:
24662 try ctor-initializer [opt] function-body handler-seq */
24664 static void
24665 cp_parser_function_try_block (cp_parser* parser)
24667 tree compound_stmt;
24668 tree try_block;
24670 /* Look for the `try' keyword. */
24671 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24672 return;
24673 /* Let the rest of the front end know where we are. */
24674 try_block = begin_function_try_block (&compound_stmt);
24675 /* Parse the function-body. */
24676 cp_parser_ctor_initializer_opt_and_function_body
24677 (parser, /*in_function_try_block=*/true);
24678 /* We're done with the `try' part. */
24679 finish_function_try_block (try_block);
24680 /* Parse the handlers. */
24681 cp_parser_handler_seq (parser);
24682 /* We're done with the handlers. */
24683 finish_function_handler_sequence (try_block, compound_stmt);
24686 /* Parse a handler-seq.
24688 handler-seq:
24689 handler handler-seq [opt] */
24691 static void
24692 cp_parser_handler_seq (cp_parser* parser)
24694 while (true)
24696 cp_token *token;
24698 /* Parse the handler. */
24699 cp_parser_handler (parser);
24700 /* Peek at the next token. */
24701 token = cp_lexer_peek_token (parser->lexer);
24702 /* If it's not `catch' then there are no more handlers. */
24703 if (!cp_parser_is_keyword (token, RID_CATCH))
24704 break;
24708 /* Parse a handler.
24710 handler:
24711 catch ( exception-declaration ) compound-statement */
24713 static void
24714 cp_parser_handler (cp_parser* parser)
24716 tree handler;
24717 tree declaration;
24719 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24720 handler = begin_handler ();
24721 matching_parens parens;
24722 parens.require_open (parser);
24723 declaration = cp_parser_exception_declaration (parser);
24724 finish_handler_parms (declaration, handler);
24725 parens.require_close (parser);
24726 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24727 finish_handler (handler);
24730 /* Parse an exception-declaration.
24732 exception-declaration:
24733 type-specifier-seq declarator
24734 type-specifier-seq abstract-declarator
24735 type-specifier-seq
24738 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24739 ellipsis variant is used. */
24741 static tree
24742 cp_parser_exception_declaration (cp_parser* parser)
24744 cp_decl_specifier_seq type_specifiers;
24745 cp_declarator *declarator;
24746 const char *saved_message;
24748 /* If it's an ellipsis, it's easy to handle. */
24749 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24751 /* Consume the `...' token. */
24752 cp_lexer_consume_token (parser->lexer);
24753 return NULL_TREE;
24756 /* Types may not be defined in exception-declarations. */
24757 saved_message = parser->type_definition_forbidden_message;
24758 parser->type_definition_forbidden_message
24759 = G_("types may not be defined in exception-declarations");
24761 /* Parse the type-specifier-seq. */
24762 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24763 /*is_trailing_return=*/false,
24764 &type_specifiers);
24765 /* If it's a `)', then there is no declarator. */
24766 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24767 declarator = NULL;
24768 else
24769 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24770 /*ctor_dtor_or_conv_p=*/NULL,
24771 /*parenthesized_p=*/NULL,
24772 /*member_p=*/false,
24773 /*friend_p=*/false);
24775 /* Restore the saved message. */
24776 parser->type_definition_forbidden_message = saved_message;
24778 if (!type_specifiers.any_specifiers_p)
24779 return error_mark_node;
24781 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24784 /* Parse a throw-expression.
24786 throw-expression:
24787 throw assignment-expression [opt]
24789 Returns a THROW_EXPR representing the throw-expression. */
24791 static tree
24792 cp_parser_throw_expression (cp_parser* parser)
24794 tree expression;
24795 cp_token* token;
24797 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24798 token = cp_lexer_peek_token (parser->lexer);
24799 /* Figure out whether or not there is an assignment-expression
24800 following the "throw" keyword. */
24801 if (token->type == CPP_COMMA
24802 || token->type == CPP_SEMICOLON
24803 || token->type == CPP_CLOSE_PAREN
24804 || token->type == CPP_CLOSE_SQUARE
24805 || token->type == CPP_CLOSE_BRACE
24806 || token->type == CPP_COLON)
24807 expression = NULL_TREE;
24808 else
24809 expression = cp_parser_assignment_expression (parser);
24811 return build_throw (expression);
24814 /* GNU Extensions */
24816 /* Parse an (optional) asm-specification.
24818 asm-specification:
24819 asm ( string-literal )
24821 If the asm-specification is present, returns a STRING_CST
24822 corresponding to the string-literal. Otherwise, returns
24823 NULL_TREE. */
24825 static tree
24826 cp_parser_asm_specification_opt (cp_parser* parser)
24828 cp_token *token;
24829 tree asm_specification;
24831 /* Peek at the next token. */
24832 token = cp_lexer_peek_token (parser->lexer);
24833 /* If the next token isn't the `asm' keyword, then there's no
24834 asm-specification. */
24835 if (!cp_parser_is_keyword (token, RID_ASM))
24836 return NULL_TREE;
24838 /* Consume the `asm' token. */
24839 cp_lexer_consume_token (parser->lexer);
24840 /* Look for the `('. */
24841 matching_parens parens;
24842 parens.require_open (parser);
24844 /* Look for the string-literal. */
24845 asm_specification = cp_parser_string_literal (parser, false, false);
24847 /* Look for the `)'. */
24848 parens.require_close (parser);
24850 return asm_specification;
24853 /* Parse an asm-operand-list.
24855 asm-operand-list:
24856 asm-operand
24857 asm-operand-list , asm-operand
24859 asm-operand:
24860 string-literal ( expression )
24861 [ string-literal ] string-literal ( expression )
24863 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24864 each node is the expression. The TREE_PURPOSE is itself a
24865 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24866 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24867 is a STRING_CST for the string literal before the parenthesis. Returns
24868 ERROR_MARK_NODE if any of the operands are invalid. */
24870 static tree
24871 cp_parser_asm_operand_list (cp_parser* parser)
24873 tree asm_operands = NULL_TREE;
24874 bool invalid_operands = false;
24876 while (true)
24878 tree string_literal;
24879 tree expression;
24880 tree name;
24882 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24884 /* Consume the `[' token. */
24885 cp_lexer_consume_token (parser->lexer);
24886 /* Read the operand name. */
24887 name = cp_parser_identifier (parser);
24888 if (name != error_mark_node)
24889 name = build_string (IDENTIFIER_LENGTH (name),
24890 IDENTIFIER_POINTER (name));
24891 /* Look for the closing `]'. */
24892 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24894 else
24895 name = NULL_TREE;
24896 /* Look for the string-literal. */
24897 string_literal = cp_parser_string_literal (parser, false, false);
24899 /* Look for the `('. */
24900 matching_parens parens;
24901 parens.require_open (parser);
24902 /* Parse the expression. */
24903 expression = cp_parser_expression (parser);
24904 /* Look for the `)'. */
24905 parens.require_close (parser);
24907 if (name == error_mark_node
24908 || string_literal == error_mark_node
24909 || expression == error_mark_node)
24910 invalid_operands = true;
24912 /* Add this operand to the list. */
24913 asm_operands = tree_cons (build_tree_list (name, string_literal),
24914 expression,
24915 asm_operands);
24916 /* If the next token is not a `,', there are no more
24917 operands. */
24918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24919 break;
24920 /* Consume the `,'. */
24921 cp_lexer_consume_token (parser->lexer);
24924 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24927 /* Parse an asm-clobber-list.
24929 asm-clobber-list:
24930 string-literal
24931 asm-clobber-list , string-literal
24933 Returns a TREE_LIST, indicating the clobbers in the order that they
24934 appeared. The TREE_VALUE of each node is a STRING_CST. */
24936 static tree
24937 cp_parser_asm_clobber_list (cp_parser* parser)
24939 tree clobbers = NULL_TREE;
24941 while (true)
24943 tree string_literal;
24945 /* Look for the string literal. */
24946 string_literal = cp_parser_string_literal (parser, false, false);
24947 /* Add it to the list. */
24948 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24949 /* If the next token is not a `,', then the list is
24950 complete. */
24951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24952 break;
24953 /* Consume the `,' token. */
24954 cp_lexer_consume_token (parser->lexer);
24957 return clobbers;
24960 /* Parse an asm-label-list.
24962 asm-label-list:
24963 identifier
24964 asm-label-list , identifier
24966 Returns a TREE_LIST, indicating the labels in the order that they
24967 appeared. The TREE_VALUE of each node is a label. */
24969 static tree
24970 cp_parser_asm_label_list (cp_parser* parser)
24972 tree labels = NULL_TREE;
24974 while (true)
24976 tree identifier, label, name;
24978 /* Look for the identifier. */
24979 identifier = cp_parser_identifier (parser);
24980 if (!error_operand_p (identifier))
24982 label = lookup_label (identifier);
24983 if (TREE_CODE (label) == LABEL_DECL)
24985 TREE_USED (label) = 1;
24986 check_goto (label);
24987 name = build_string (IDENTIFIER_LENGTH (identifier),
24988 IDENTIFIER_POINTER (identifier));
24989 labels = tree_cons (name, label, labels);
24992 /* If the next token is not a `,', then the list is
24993 complete. */
24994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24995 break;
24996 /* Consume the `,' token. */
24997 cp_lexer_consume_token (parser->lexer);
25000 return nreverse (labels);
25003 /* Return TRUE iff the next tokens in the stream are possibly the
25004 beginning of a GNU extension attribute. */
25006 static bool
25007 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25009 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25012 /* Return TRUE iff the next tokens in the stream are possibly the
25013 beginning of a standard C++-11 attribute specifier. */
25015 static bool
25016 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25018 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25021 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25022 beginning of a standard C++-11 attribute specifier. */
25024 static bool
25025 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25027 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25029 return (cxx_dialect >= cxx11
25030 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25031 || (token->type == CPP_OPEN_SQUARE
25032 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25033 && token->type == CPP_OPEN_SQUARE)));
25036 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25037 beginning of a GNU extension attribute. */
25039 static bool
25040 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25042 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25044 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25047 /* Return true iff the next tokens can be the beginning of either a
25048 GNU attribute list, or a standard C++11 attribute sequence. */
25050 static bool
25051 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25053 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25054 || cp_next_tokens_can_be_std_attribute_p (parser));
25057 /* Return true iff the next Nth tokens can be the beginning of either
25058 a GNU attribute list, or a standard C++11 attribute sequence. */
25060 static bool
25061 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25063 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25064 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25067 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25068 of GNU attributes, or return NULL. */
25070 static tree
25071 cp_parser_attributes_opt (cp_parser *parser)
25073 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25074 return cp_parser_gnu_attributes_opt (parser);
25075 return cp_parser_std_attribute_spec_seq (parser);
25078 /* Parse an (optional) series of attributes.
25080 attributes:
25081 attributes attribute
25083 attribute:
25084 __attribute__ (( attribute-list [opt] ))
25086 The return value is as for cp_parser_gnu_attribute_list. */
25088 static tree
25089 cp_parser_gnu_attributes_opt (cp_parser* parser)
25091 tree attributes = NULL_TREE;
25093 temp_override<bool> cleanup
25094 (parser->auto_is_implicit_function_template_parm_p, false);
25096 while (true)
25098 cp_token *token;
25099 tree attribute_list;
25100 bool ok = true;
25102 /* Peek at the next token. */
25103 token = cp_lexer_peek_token (parser->lexer);
25104 /* If it's not `__attribute__', then we're done. */
25105 if (token->keyword != RID_ATTRIBUTE)
25106 break;
25108 /* Consume the `__attribute__' keyword. */
25109 cp_lexer_consume_token (parser->lexer);
25110 /* Look for the two `(' tokens. */
25111 matching_parens outer_parens;
25112 outer_parens.require_open (parser);
25113 matching_parens inner_parens;
25114 inner_parens.require_open (parser);
25116 /* Peek at the next token. */
25117 token = cp_lexer_peek_token (parser->lexer);
25118 if (token->type != CPP_CLOSE_PAREN)
25119 /* Parse the attribute-list. */
25120 attribute_list = cp_parser_gnu_attribute_list (parser);
25121 else
25122 /* If the next token is a `)', then there is no attribute
25123 list. */
25124 attribute_list = NULL;
25126 /* Look for the two `)' tokens. */
25127 if (!inner_parens.require_close (parser))
25128 ok = false;
25129 if (!outer_parens.require_close (parser))
25130 ok = false;
25131 if (!ok)
25132 cp_parser_skip_to_end_of_statement (parser);
25134 /* Add these new attributes to the list. */
25135 attributes = attr_chainon (attributes, attribute_list);
25138 return attributes;
25141 /* Parse a GNU attribute-list.
25143 attribute-list:
25144 attribute
25145 attribute-list , attribute
25147 attribute:
25148 identifier
25149 identifier ( identifier )
25150 identifier ( identifier , expression-list )
25151 identifier ( expression-list )
25153 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25154 to an attribute. The TREE_PURPOSE of each node is the identifier
25155 indicating which attribute is in use. The TREE_VALUE represents
25156 the arguments, if any. */
25158 static tree
25159 cp_parser_gnu_attribute_list (cp_parser* parser)
25161 tree attribute_list = NULL_TREE;
25162 bool save_translate_strings_p = parser->translate_strings_p;
25164 parser->translate_strings_p = false;
25165 while (true)
25167 cp_token *token;
25168 tree identifier;
25169 tree attribute;
25171 /* Look for the identifier. We also allow keywords here; for
25172 example `__attribute__ ((const))' is legal. */
25173 token = cp_lexer_peek_token (parser->lexer);
25174 if (token->type == CPP_NAME
25175 || token->type == CPP_KEYWORD)
25177 tree arguments = NULL_TREE;
25179 /* Consume the token, but save it since we need it for the
25180 SIMD enabled function parsing. */
25181 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25183 /* Save away the identifier that indicates which attribute
25184 this is. */
25185 identifier = (token->type == CPP_KEYWORD)
25186 /* For keywords, use the canonical spelling, not the
25187 parsed identifier. */
25188 ? ridpointers[(int) token->keyword]
25189 : id_token->u.value;
25191 identifier = canonicalize_attr_name (identifier);
25192 attribute = build_tree_list (identifier, NULL_TREE);
25194 /* Peek at the next token. */
25195 token = cp_lexer_peek_token (parser->lexer);
25196 /* If it's an `(', then parse the attribute arguments. */
25197 if (token->type == CPP_OPEN_PAREN)
25199 vec<tree, va_gc> *vec;
25200 int attr_flag = (attribute_takes_identifier_p (identifier)
25201 ? id_attr : normal_attr);
25202 vec = cp_parser_parenthesized_expression_list
25203 (parser, attr_flag, /*cast_p=*/false,
25204 /*allow_expansion_p=*/false,
25205 /*non_constant_p=*/NULL);
25206 if (vec == NULL)
25207 arguments = error_mark_node;
25208 else
25210 arguments = build_tree_list_vec (vec);
25211 release_tree_vector (vec);
25213 /* Save the arguments away. */
25214 TREE_VALUE (attribute) = arguments;
25217 if (arguments != error_mark_node)
25219 /* Add this attribute to the list. */
25220 TREE_CHAIN (attribute) = attribute_list;
25221 attribute_list = attribute;
25224 token = cp_lexer_peek_token (parser->lexer);
25226 /* Now, look for more attributes. If the next token isn't a
25227 `,', we're done. */
25228 if (token->type != CPP_COMMA)
25229 break;
25231 /* Consume the comma and keep going. */
25232 cp_lexer_consume_token (parser->lexer);
25234 parser->translate_strings_p = save_translate_strings_p;
25236 /* We built up the list in reverse order. */
25237 return nreverse (attribute_list);
25240 /* Parse a standard C++11 attribute.
25242 The returned representation is a TREE_LIST which TREE_PURPOSE is
25243 the scoped name of the attribute, and the TREE_VALUE is its
25244 arguments list.
25246 Note that the scoped name of the attribute is itself a TREE_LIST
25247 which TREE_PURPOSE is the namespace of the attribute, and
25248 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25249 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25250 and which TREE_PURPOSE is directly the attribute name.
25252 Clients of the attribute code should use get_attribute_namespace
25253 and get_attribute_name to get the actual namespace and name of
25254 attributes, regardless of their being GNU or C++11 attributes.
25256 attribute:
25257 attribute-token attribute-argument-clause [opt]
25259 attribute-token:
25260 identifier
25261 attribute-scoped-token
25263 attribute-scoped-token:
25264 attribute-namespace :: identifier
25266 attribute-namespace:
25267 identifier
25269 attribute-argument-clause:
25270 ( balanced-token-seq )
25272 balanced-token-seq:
25273 balanced-token [opt]
25274 balanced-token-seq balanced-token
25276 balanced-token:
25277 ( balanced-token-seq )
25278 [ balanced-token-seq ]
25279 { balanced-token-seq }. */
25281 static tree
25282 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25284 tree attribute, attr_id = NULL_TREE, arguments;
25285 cp_token *token;
25287 temp_override<bool> cleanup
25288 (parser->auto_is_implicit_function_template_parm_p, false);
25290 /* First, parse name of the attribute, a.k.a attribute-token. */
25292 token = cp_lexer_peek_token (parser->lexer);
25293 if (token->type == CPP_NAME)
25294 attr_id = token->u.value;
25295 else if (token->type == CPP_KEYWORD)
25296 attr_id = ridpointers[(int) token->keyword];
25297 else if (token->flags & NAMED_OP)
25298 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25300 if (attr_id == NULL_TREE)
25301 return NULL_TREE;
25303 cp_lexer_consume_token (parser->lexer);
25305 token = cp_lexer_peek_token (parser->lexer);
25306 if (token->type == CPP_SCOPE)
25308 /* We are seeing a scoped attribute token. */
25310 cp_lexer_consume_token (parser->lexer);
25311 if (attr_ns)
25312 error_at (token->location, "attribute using prefix used together "
25313 "with scoped attribute token");
25314 attr_ns = attr_id;
25316 token = cp_lexer_consume_token (parser->lexer);
25317 if (token->type == CPP_NAME)
25318 attr_id = token->u.value;
25319 else if (token->type == CPP_KEYWORD)
25320 attr_id = ridpointers[(int) token->keyword];
25321 else if (token->flags & NAMED_OP)
25322 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25323 else
25325 error_at (token->location,
25326 "expected an identifier for the attribute name");
25327 return error_mark_node;
25330 attr_id = canonicalize_attr_name (attr_id);
25331 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25332 NULL_TREE);
25333 token = cp_lexer_peek_token (parser->lexer);
25335 else if (attr_ns)
25336 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25337 NULL_TREE);
25338 else
25340 attr_id = canonicalize_attr_name (attr_id);
25341 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25342 NULL_TREE);
25343 /* C++11 noreturn attribute is equivalent to GNU's. */
25344 if (is_attribute_p ("noreturn", attr_id))
25345 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25346 /* C++14 deprecated attribute is equivalent to GNU's. */
25347 else if (is_attribute_p ("deprecated", attr_id))
25348 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25349 /* C++17 fallthrough attribute is equivalent to GNU's. */
25350 else if (is_attribute_p ("fallthrough", attr_id))
25351 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25352 /* Transactional Memory TS optimize_for_synchronized attribute is
25353 equivalent to GNU transaction_callable. */
25354 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25355 TREE_PURPOSE (attribute)
25356 = get_identifier ("transaction_callable");
25357 /* Transactional Memory attributes are GNU attributes. */
25358 else if (tm_attr_to_mask (attr_id))
25359 TREE_PURPOSE (attribute) = attr_id;
25362 /* Now parse the optional argument clause of the attribute. */
25364 if (token->type != CPP_OPEN_PAREN)
25365 return attribute;
25368 vec<tree, va_gc> *vec;
25369 int attr_flag = normal_attr;
25371 if (attr_ns == gnu_identifier
25372 && attribute_takes_identifier_p (attr_id))
25373 /* A GNU attribute that takes an identifier in parameter. */
25374 attr_flag = id_attr;
25376 vec = cp_parser_parenthesized_expression_list
25377 (parser, attr_flag, /*cast_p=*/false,
25378 /*allow_expansion_p=*/true,
25379 /*non_constant_p=*/NULL);
25380 if (vec == NULL)
25381 arguments = error_mark_node;
25382 else
25384 arguments = build_tree_list_vec (vec);
25385 release_tree_vector (vec);
25388 if (arguments == error_mark_node)
25389 attribute = error_mark_node;
25390 else
25391 TREE_VALUE (attribute) = arguments;
25394 return attribute;
25397 /* Check that the attribute ATTRIBUTE appears at most once in the
25398 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25399 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25400 isn't implemented yet in GCC. */
25402 static void
25403 cp_parser_check_std_attribute (tree attributes, tree attribute)
25405 if (attributes)
25407 tree name = get_attribute_name (attribute);
25408 if (is_attribute_p ("noreturn", name)
25409 && lookup_attribute ("noreturn", attributes))
25410 error ("attribute %<noreturn%> can appear at most once "
25411 "in an attribute-list");
25412 else if (is_attribute_p ("deprecated", name)
25413 && lookup_attribute ("deprecated", attributes))
25414 error ("attribute %<deprecated%> can appear at most once "
25415 "in an attribute-list");
25419 /* Parse a list of standard C++-11 attributes.
25421 attribute-list:
25422 attribute [opt]
25423 attribute-list , attribute[opt]
25424 attribute ...
25425 attribute-list , attribute ...
25428 static tree
25429 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25431 tree attributes = NULL_TREE, attribute = NULL_TREE;
25432 cp_token *token = NULL;
25434 while (true)
25436 attribute = cp_parser_std_attribute (parser, attr_ns);
25437 if (attribute == error_mark_node)
25438 break;
25439 if (attribute != NULL_TREE)
25441 cp_parser_check_std_attribute (attributes, attribute);
25442 TREE_CHAIN (attribute) = attributes;
25443 attributes = attribute;
25445 token = cp_lexer_peek_token (parser->lexer);
25446 if (token->type == CPP_ELLIPSIS)
25448 cp_lexer_consume_token (parser->lexer);
25449 if (attribute == NULL_TREE)
25450 error_at (token->location,
25451 "expected attribute before %<...%>");
25452 else
25454 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25455 if (pack == error_mark_node)
25456 return error_mark_node;
25457 TREE_VALUE (attribute) = pack;
25459 token = cp_lexer_peek_token (parser->lexer);
25461 if (token->type != CPP_COMMA)
25462 break;
25463 cp_lexer_consume_token (parser->lexer);
25465 attributes = nreverse (attributes);
25466 return attributes;
25469 /* Parse a standard C++-11 attribute specifier.
25471 attribute-specifier:
25472 [ [ attribute-using-prefix [opt] attribute-list ] ]
25473 alignment-specifier
25475 attribute-using-prefix:
25476 using attribute-namespace :
25478 alignment-specifier:
25479 alignas ( type-id ... [opt] )
25480 alignas ( alignment-expression ... [opt] ). */
25482 static tree
25483 cp_parser_std_attribute_spec (cp_parser *parser)
25485 tree attributes = NULL_TREE;
25486 cp_token *token = cp_lexer_peek_token (parser->lexer);
25488 if (token->type == CPP_OPEN_SQUARE
25489 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25491 tree attr_ns = NULL_TREE;
25493 cp_lexer_consume_token (parser->lexer);
25494 cp_lexer_consume_token (parser->lexer);
25496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25498 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25499 if (token->type == CPP_NAME)
25500 attr_ns = token->u.value;
25501 else if (token->type == CPP_KEYWORD)
25502 attr_ns = ridpointers[(int) token->keyword];
25503 else if (token->flags & NAMED_OP)
25504 attr_ns = get_identifier (cpp_type2name (token->type,
25505 token->flags));
25506 if (attr_ns
25507 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25509 if (cxx_dialect < cxx17
25510 && !in_system_header_at (input_location))
25511 pedwarn (input_location, 0,
25512 "attribute using prefix only available "
25513 "with -std=c++17 or -std=gnu++17");
25515 cp_lexer_consume_token (parser->lexer);
25516 cp_lexer_consume_token (parser->lexer);
25517 cp_lexer_consume_token (parser->lexer);
25519 else
25520 attr_ns = NULL_TREE;
25523 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25525 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25526 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25527 cp_parser_skip_to_end_of_statement (parser);
25528 else
25529 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25530 when we are sure that we have actually parsed them. */
25531 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25533 else
25535 tree alignas_expr;
25537 /* Look for an alignment-specifier. */
25539 token = cp_lexer_peek_token (parser->lexer);
25541 if (token->type != CPP_KEYWORD
25542 || token->keyword != RID_ALIGNAS)
25543 return NULL_TREE;
25545 cp_lexer_consume_token (parser->lexer);
25546 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25548 matching_parens parens;
25549 if (!parens.require_open (parser))
25550 return error_mark_node;
25552 cp_parser_parse_tentatively (parser);
25553 alignas_expr = cp_parser_type_id (parser);
25555 if (!cp_parser_parse_definitely (parser))
25557 alignas_expr = cp_parser_assignment_expression (parser);
25558 if (alignas_expr == error_mark_node)
25559 cp_parser_skip_to_end_of_statement (parser);
25560 if (alignas_expr == NULL_TREE
25561 || alignas_expr == error_mark_node)
25562 return alignas_expr;
25565 alignas_expr = cxx_alignas_expr (alignas_expr);
25566 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25568 /* Handle alignas (pack...). */
25569 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25571 cp_lexer_consume_token (parser->lexer);
25572 alignas_expr = make_pack_expansion (alignas_expr);
25575 /* Something went wrong, so don't build the attribute. */
25576 if (alignas_expr == error_mark_node)
25577 return error_mark_node;
25579 if (!parens.require_close (parser))
25580 return error_mark_node;
25582 /* Build the C++-11 representation of an 'aligned'
25583 attribute. */
25584 attributes
25585 = build_tree_list (build_tree_list (gnu_identifier,
25586 aligned_identifier), alignas_expr);
25589 return attributes;
25592 /* Parse a standard C++-11 attribute-specifier-seq.
25594 attribute-specifier-seq:
25595 attribute-specifier-seq [opt] attribute-specifier
25598 static tree
25599 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25601 tree attr_specs = NULL_TREE;
25602 tree attr_last = NULL_TREE;
25604 while (true)
25606 tree attr_spec = cp_parser_std_attribute_spec (parser);
25607 if (attr_spec == NULL_TREE)
25608 break;
25609 if (attr_spec == error_mark_node)
25610 return error_mark_node;
25612 if (attr_last)
25613 TREE_CHAIN (attr_last) = attr_spec;
25614 else
25615 attr_specs = attr_last = attr_spec;
25616 attr_last = tree_last (attr_last);
25619 return attr_specs;
25622 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25623 return index of the first token after balanced-token, or N on failure. */
25625 static size_t
25626 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25628 size_t orig_n = n;
25629 int nparens = 0, nbraces = 0, nsquares = 0;
25631 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25633 case CPP_EOF:
25634 case CPP_PRAGMA_EOL:
25635 /* Ran out of tokens. */
25636 return orig_n;
25637 case CPP_OPEN_PAREN:
25638 ++nparens;
25639 break;
25640 case CPP_OPEN_BRACE:
25641 ++nbraces;
25642 break;
25643 case CPP_OPEN_SQUARE:
25644 ++nsquares;
25645 break;
25646 case CPP_CLOSE_PAREN:
25647 --nparens;
25648 break;
25649 case CPP_CLOSE_BRACE:
25650 --nbraces;
25651 break;
25652 case CPP_CLOSE_SQUARE:
25653 --nsquares;
25654 break;
25655 default:
25656 break;
25658 while (nparens || nbraces || nsquares);
25659 return n;
25662 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25663 return index of the first token after the GNU attribute tokens, or N on
25664 failure. */
25666 static size_t
25667 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25669 while (true)
25671 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25672 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25673 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25674 break;
25676 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25677 if (n2 == n + 2)
25678 break;
25679 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25680 break;
25681 n = n2 + 1;
25683 return n;
25686 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25687 next token), return index of the first token after the standard C++11
25688 attribute tokens, or N on failure. */
25690 static size_t
25691 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25693 while (true)
25695 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25696 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25698 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25699 if (n2 == n + 1)
25700 break;
25701 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25702 break;
25703 n = n2 + 1;
25705 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25706 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25708 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25709 if (n2 == n + 1)
25710 break;
25711 n = n2;
25713 else
25714 break;
25716 return n;
25719 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25720 as the next token), return index of the first token after the attribute
25721 tokens, or N on failure. */
25723 static size_t
25724 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25726 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25727 return cp_parser_skip_gnu_attributes_opt (parser, n);
25728 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25731 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25732 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25733 current value of the PEDANTIC flag, regardless of whether or not
25734 the `__extension__' keyword is present. The caller is responsible
25735 for restoring the value of the PEDANTIC flag. */
25737 static bool
25738 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25740 /* Save the old value of the PEDANTIC flag. */
25741 *saved_pedantic = pedantic;
25743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25745 /* Consume the `__extension__' token. */
25746 cp_lexer_consume_token (parser->lexer);
25747 /* We're not being pedantic while the `__extension__' keyword is
25748 in effect. */
25749 pedantic = 0;
25751 return true;
25754 return false;
25757 /* Parse a label declaration.
25759 label-declaration:
25760 __label__ label-declarator-seq ;
25762 label-declarator-seq:
25763 identifier , label-declarator-seq
25764 identifier */
25766 static void
25767 cp_parser_label_declaration (cp_parser* parser)
25769 /* Look for the `__label__' keyword. */
25770 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25772 while (true)
25774 tree identifier;
25776 /* Look for an identifier. */
25777 identifier = cp_parser_identifier (parser);
25778 /* If we failed, stop. */
25779 if (identifier == error_mark_node)
25780 break;
25781 /* Declare it as a label. */
25782 finish_label_decl (identifier);
25783 /* If the next token is a `;', stop. */
25784 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25785 break;
25786 /* Look for the `,' separating the label declarations. */
25787 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25790 /* Look for the final `;'. */
25791 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25794 // -------------------------------------------------------------------------- //
25795 // Requires Clause
25797 // Parse a requires clause.
25799 // requires-clause:
25800 // 'requires' logical-or-expression
25802 // The required logical-or-expression must be a constant expression. Note
25803 // that we don't check that the expression is constepxr here. We defer until
25804 // we analyze constraints and then, we only check atomic constraints.
25805 static tree
25806 cp_parser_requires_clause (cp_parser *parser)
25808 // Parse the requires clause so that it is not automatically folded.
25809 ++processing_template_decl;
25810 tree expr = cp_parser_binary_expression (parser, false, false,
25811 PREC_NOT_OPERATOR, NULL);
25812 if (check_for_bare_parameter_packs (expr))
25813 expr = error_mark_node;
25814 --processing_template_decl;
25815 return expr;
25818 // Optionally parse a requires clause:
25819 static tree
25820 cp_parser_requires_clause_opt (cp_parser *parser)
25822 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25823 if (tok->keyword != RID_REQUIRES)
25825 if (!flag_concepts && tok->type == CPP_NAME
25826 && tok->u.value == ridpointers[RID_REQUIRES])
25828 error_at (cp_lexer_peek_token (parser->lexer)->location,
25829 "%<requires%> only available with -fconcepts");
25830 /* Parse and discard the requires-clause. */
25831 cp_lexer_consume_token (parser->lexer);
25832 cp_parser_requires_clause (parser);
25834 return NULL_TREE;
25836 cp_lexer_consume_token (parser->lexer);
25837 return cp_parser_requires_clause (parser);
25841 /*---------------------------------------------------------------------------
25842 Requires expressions
25843 ---------------------------------------------------------------------------*/
25845 /* Parse a requires expression
25847 requirement-expression:
25848 'requires' requirement-parameter-list [opt] requirement-body */
25849 static tree
25850 cp_parser_requires_expression (cp_parser *parser)
25852 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25853 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25855 /* A requires-expression shall appear only within a concept
25856 definition or a requires-clause.
25858 TODO: Implement this diagnostic correctly. */
25859 if (!processing_template_decl)
25861 error_at (loc, "a requires expression cannot appear outside a template");
25862 cp_parser_skip_to_end_of_statement (parser);
25863 return error_mark_node;
25866 tree parms, reqs;
25868 /* Local parameters are delared as variables within the scope
25869 of the expression. They are not visible past the end of
25870 the expression. Expressions within the requires-expression
25871 are unevaluated. */
25872 struct scope_sentinel
25874 scope_sentinel ()
25876 ++cp_unevaluated_operand;
25877 begin_scope (sk_block, NULL_TREE);
25880 ~scope_sentinel ()
25882 pop_bindings_and_leave_scope ();
25883 --cp_unevaluated_operand;
25885 } s;
25887 /* Parse the optional parameter list. */
25888 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25890 parms = cp_parser_requirement_parameter_list (parser);
25891 if (parms == error_mark_node)
25892 return error_mark_node;
25894 else
25895 parms = NULL_TREE;
25897 /* Parse the requirement body. */
25898 reqs = cp_parser_requirement_body (parser);
25899 if (reqs == error_mark_node)
25900 return error_mark_node;
25903 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25904 the parm chain. */
25905 grokparms (parms, &parms);
25906 return finish_requires_expr (parms, reqs);
25909 /* Parse a parameterized requirement.
25911 requirement-parameter-list:
25912 '(' parameter-declaration-clause ')' */
25913 static tree
25914 cp_parser_requirement_parameter_list (cp_parser *parser)
25916 matching_parens parens;
25917 if (!parens.require_open (parser))
25918 return error_mark_node;
25920 tree parms = cp_parser_parameter_declaration_clause (parser);
25922 if (!parens.require_close (parser))
25923 return error_mark_node;
25925 return parms;
25928 /* Parse the body of a requirement.
25930 requirement-body:
25931 '{' requirement-list '}' */
25932 static tree
25933 cp_parser_requirement_body (cp_parser *parser)
25935 matching_braces braces;
25936 if (!braces.require_open (parser))
25937 return error_mark_node;
25939 tree reqs = cp_parser_requirement_list (parser);
25941 if (!braces.require_close (parser))
25942 return error_mark_node;
25944 return reqs;
25947 /* Parse a list of requirements.
25949 requirement-list:
25950 requirement
25951 requirement-list ';' requirement[opt] */
25952 static tree
25953 cp_parser_requirement_list (cp_parser *parser)
25955 tree result = NULL_TREE;
25956 while (true)
25958 tree req = cp_parser_requirement (parser);
25959 if (req == error_mark_node)
25960 return error_mark_node;
25962 result = tree_cons (NULL_TREE, req, result);
25964 /* If we see a semi-colon, consume it. */
25965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25966 cp_lexer_consume_token (parser->lexer);
25968 /* Stop processing at the end of the list. */
25969 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25970 break;
25973 /* Reverse the order of requirements so they are analyzed in
25974 declaration order. */
25975 return nreverse (result);
25978 /* Parse a syntactic requirement or type requirement.
25980 requirement:
25981 simple-requirement
25982 compound-requirement
25983 type-requirement
25984 nested-requirement */
25985 static tree
25986 cp_parser_requirement (cp_parser *parser)
25988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25989 return cp_parser_compound_requirement (parser);
25990 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25991 return cp_parser_type_requirement (parser);
25992 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25993 return cp_parser_nested_requirement (parser);
25994 else
25995 return cp_parser_simple_requirement (parser);
25998 /* Parse a simple requirement.
26000 simple-requirement:
26001 expression ';' */
26002 static tree
26003 cp_parser_simple_requirement (cp_parser *parser)
26005 tree expr = cp_parser_expression (parser, NULL, false, false);
26006 if (!expr || expr == error_mark_node)
26007 return error_mark_node;
26009 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26010 return error_mark_node;
26012 return finish_simple_requirement (expr);
26015 /* Parse a type requirement
26017 type-requirement
26018 nested-name-specifier [opt] required-type-name ';'
26020 required-type-name:
26021 type-name
26022 'template' [opt] simple-template-id */
26023 static tree
26024 cp_parser_type_requirement (cp_parser *parser)
26026 cp_lexer_consume_token (parser->lexer);
26028 // Save the scope before parsing name specifiers.
26029 tree saved_scope = parser->scope;
26030 tree saved_object_scope = parser->object_scope;
26031 tree saved_qualifying_scope = parser->qualifying_scope;
26032 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26033 cp_parser_nested_name_specifier_opt (parser,
26034 /*typename_keyword_p=*/true,
26035 /*check_dependency_p=*/false,
26036 /*type_p=*/true,
26037 /*is_declaration=*/false);
26039 tree type;
26040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26042 cp_lexer_consume_token (parser->lexer);
26043 type = cp_parser_template_id (parser,
26044 /*template_keyword_p=*/true,
26045 /*check_dependency=*/false,
26046 /*tag_type=*/none_type,
26047 /*is_declaration=*/false);
26048 type = make_typename_type (parser->scope, type, typename_type,
26049 /*complain=*/tf_error);
26051 else
26052 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26054 if (TREE_CODE (type) == TYPE_DECL)
26055 type = TREE_TYPE (type);
26057 parser->scope = saved_scope;
26058 parser->object_scope = saved_object_scope;
26059 parser->qualifying_scope = saved_qualifying_scope;
26061 if (type == error_mark_node)
26062 cp_parser_skip_to_end_of_statement (parser);
26064 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26065 return error_mark_node;
26066 if (type == error_mark_node)
26067 return error_mark_node;
26069 return finish_type_requirement (type);
26072 /* Parse a compound requirement
26074 compound-requirement:
26075 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26076 static tree
26077 cp_parser_compound_requirement (cp_parser *parser)
26079 /* Parse an expression enclosed in '{ }'s. */
26080 matching_braces braces;
26081 if (!braces.require_open (parser))
26082 return error_mark_node;
26084 tree expr = cp_parser_expression (parser, NULL, false, false);
26085 if (!expr || expr == error_mark_node)
26086 return error_mark_node;
26088 if (!braces.require_close (parser))
26089 return error_mark_node;
26091 /* Parse the optional noexcept. */
26092 bool noexcept_p = false;
26093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26095 cp_lexer_consume_token (parser->lexer);
26096 noexcept_p = true;
26099 /* Parse the optional trailing return type. */
26100 tree type = NULL_TREE;
26101 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26103 cp_lexer_consume_token (parser->lexer);
26104 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26105 parser->in_result_type_constraint_p = true;
26106 type = cp_parser_trailing_type_id (parser);
26107 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26108 if (type == error_mark_node)
26109 return error_mark_node;
26112 return finish_compound_requirement (expr, type, noexcept_p);
26115 /* Parse a nested requirement. This is the same as a requires clause.
26117 nested-requirement:
26118 requires-clause */
26119 static tree
26120 cp_parser_nested_requirement (cp_parser *parser)
26122 cp_lexer_consume_token (parser->lexer);
26123 tree req = cp_parser_requires_clause (parser);
26124 if (req == error_mark_node)
26125 return error_mark_node;
26126 return finish_nested_requirement (req);
26129 /* Support Functions */
26131 /* Return the appropriate prefer_type argument for lookup_name_real based on
26132 tag_type and template_mem_access. */
26134 static inline int
26135 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26137 /* DR 141: When looking in the current enclosing context for a template-name
26138 after -> or ., only consider class templates. */
26139 if (template_mem_access)
26140 return 2;
26141 switch (tag_type)
26143 case none_type: return 0; // No preference.
26144 case scope_type: return 1; // Type or namespace.
26145 default: return 2; // Type only.
26149 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26150 NAME should have one of the representations used for an
26151 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26152 is returned. If PARSER->SCOPE is a dependent type, then a
26153 SCOPE_REF is returned.
26155 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26156 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26157 was formed. Abstractly, such entities should not be passed to this
26158 function, because they do not need to be looked up, but it is
26159 simpler to check for this special case here, rather than at the
26160 call-sites.
26162 In cases not explicitly covered above, this function returns a
26163 DECL, OVERLOAD, or baselink representing the result of the lookup.
26164 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26165 is returned.
26167 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26168 (e.g., "struct") that was used. In that case bindings that do not
26169 refer to types are ignored.
26171 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26172 ignored.
26174 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26175 are ignored.
26177 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26178 types.
26180 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26181 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26182 NULL_TREE otherwise. */
26184 static cp_expr
26185 cp_parser_lookup_name (cp_parser *parser, tree name,
26186 enum tag_types tag_type,
26187 bool is_template,
26188 bool is_namespace,
26189 bool check_dependency,
26190 tree *ambiguous_decls,
26191 location_t name_location)
26193 tree decl;
26194 tree object_type = parser->context->object_type;
26196 /* Assume that the lookup will be unambiguous. */
26197 if (ambiguous_decls)
26198 *ambiguous_decls = NULL_TREE;
26200 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26201 no longer valid. Note that if we are parsing tentatively, and
26202 the parse fails, OBJECT_TYPE will be automatically restored. */
26203 parser->context->object_type = NULL_TREE;
26205 if (name == error_mark_node)
26206 return error_mark_node;
26208 /* A template-id has already been resolved; there is no lookup to
26209 do. */
26210 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26211 return name;
26212 if (BASELINK_P (name))
26214 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26215 == TEMPLATE_ID_EXPR);
26216 return name;
26219 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26220 it should already have been checked to make sure that the name
26221 used matches the type being destroyed. */
26222 if (TREE_CODE (name) == BIT_NOT_EXPR)
26224 tree type;
26226 /* Figure out to which type this destructor applies. */
26227 if (parser->scope)
26228 type = parser->scope;
26229 else if (object_type)
26230 type = object_type;
26231 else
26232 type = current_class_type;
26233 /* If that's not a class type, there is no destructor. */
26234 if (!type || !CLASS_TYPE_P (type))
26235 return error_mark_node;
26237 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26238 lazily_declare_fn (sfk_destructor, type);
26240 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26241 return dtor;
26243 return error_mark_node;
26246 /* By this point, the NAME should be an ordinary identifier. If
26247 the id-expression was a qualified name, the qualifying scope is
26248 stored in PARSER->SCOPE at this point. */
26249 gcc_assert (identifier_p (name));
26251 /* Perform the lookup. */
26252 if (parser->scope)
26254 bool dependent_p;
26256 if (parser->scope == error_mark_node)
26257 return error_mark_node;
26259 /* If the SCOPE is dependent, the lookup must be deferred until
26260 the template is instantiated -- unless we are explicitly
26261 looking up names in uninstantiated templates. Even then, we
26262 cannot look up the name if the scope is not a class type; it
26263 might, for example, be a template type parameter. */
26264 dependent_p = (TYPE_P (parser->scope)
26265 && dependent_scope_p (parser->scope));
26266 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26267 && dependent_p)
26268 /* Defer lookup. */
26269 decl = error_mark_node;
26270 else
26272 tree pushed_scope = NULL_TREE;
26274 /* If PARSER->SCOPE is a dependent type, then it must be a
26275 class type, and we must not be checking dependencies;
26276 otherwise, we would have processed this lookup above. So
26277 that PARSER->SCOPE is not considered a dependent base by
26278 lookup_member, we must enter the scope here. */
26279 if (dependent_p)
26280 pushed_scope = push_scope (parser->scope);
26282 /* If the PARSER->SCOPE is a template specialization, it
26283 may be instantiated during name lookup. In that case,
26284 errors may be issued. Even if we rollback the current
26285 tentative parse, those errors are valid. */
26286 decl = lookup_qualified_name (parser->scope, name,
26287 prefer_type_arg (tag_type),
26288 /*complain=*/true);
26290 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26291 lookup result and the nested-name-specifier nominates a class C:
26292 * if the name specified after the nested-name-specifier, when
26293 looked up in C, is the injected-class-name of C (Clause 9), or
26294 * if the name specified after the nested-name-specifier is the
26295 same as the identifier or the simple-template-id's template-
26296 name in the last component of the nested-name-specifier,
26297 the name is instead considered to name the constructor of
26298 class C. [ Note: for example, the constructor is not an
26299 acceptable lookup result in an elaborated-type-specifier so
26300 the constructor would not be used in place of the
26301 injected-class-name. --end note ] Such a constructor name
26302 shall be used only in the declarator-id of a declaration that
26303 names a constructor or in a using-declaration. */
26304 if (tag_type == none_type
26305 && DECL_SELF_REFERENCE_P (decl)
26306 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26307 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26308 prefer_type_arg (tag_type),
26309 /*complain=*/true);
26311 /* If we have a single function from a using decl, pull it out. */
26312 if (TREE_CODE (decl) == OVERLOAD
26313 && !really_overloaded_fn (decl))
26314 decl = OVL_FUNCTION (decl);
26316 if (pushed_scope)
26317 pop_scope (pushed_scope);
26320 /* If the scope is a dependent type and either we deferred lookup or
26321 we did lookup but didn't find the name, rememeber the name. */
26322 if (decl == error_mark_node && TYPE_P (parser->scope)
26323 && dependent_type_p (parser->scope))
26325 if (tag_type)
26327 tree type;
26329 /* The resolution to Core Issue 180 says that `struct
26330 A::B' should be considered a type-name, even if `A'
26331 is dependent. */
26332 type = make_typename_type (parser->scope, name, tag_type,
26333 /*complain=*/tf_error);
26334 if (type != error_mark_node)
26335 decl = TYPE_NAME (type);
26337 else if (is_template
26338 && (cp_parser_next_token_ends_template_argument_p (parser)
26339 || cp_lexer_next_token_is (parser->lexer,
26340 CPP_CLOSE_PAREN)))
26341 decl = make_unbound_class_template (parser->scope,
26342 name, NULL_TREE,
26343 /*complain=*/tf_error);
26344 else
26345 decl = build_qualified_name (/*type=*/NULL_TREE,
26346 parser->scope, name,
26347 is_template);
26349 parser->qualifying_scope = parser->scope;
26350 parser->object_scope = NULL_TREE;
26352 else if (object_type)
26354 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26355 OBJECT_TYPE is not a class. */
26356 if (CLASS_TYPE_P (object_type))
26357 /* If the OBJECT_TYPE is a template specialization, it may
26358 be instantiated during name lookup. In that case, errors
26359 may be issued. Even if we rollback the current tentative
26360 parse, those errors are valid. */
26361 decl = lookup_member (object_type,
26362 name,
26363 /*protect=*/0,
26364 prefer_type_arg (tag_type),
26365 tf_warning_or_error);
26366 else
26367 decl = NULL_TREE;
26369 if (!decl)
26370 /* Look it up in the enclosing context. DR 141: When looking for a
26371 template-name after -> or ., only consider class templates. */
26372 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26373 /*nonclass=*/0,
26374 /*block_p=*/true, is_namespace, 0);
26375 if (object_type == unknown_type_node)
26376 /* The object is type-dependent, so we can't look anything up; we used
26377 this to get the DR 141 behavior. */
26378 object_type = NULL_TREE;
26379 parser->object_scope = object_type;
26380 parser->qualifying_scope = NULL_TREE;
26382 else
26384 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26385 /*nonclass=*/0,
26386 /*block_p=*/true, is_namespace, 0);
26387 parser->qualifying_scope = NULL_TREE;
26388 parser->object_scope = NULL_TREE;
26391 /* If the lookup failed, let our caller know. */
26392 if (!decl || decl == error_mark_node)
26393 return error_mark_node;
26395 /* Pull out the template from an injected-class-name (or multiple). */
26396 if (is_template)
26397 decl = maybe_get_template_decl_from_type_decl (decl);
26399 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26400 if (TREE_CODE (decl) == TREE_LIST)
26402 if (ambiguous_decls)
26403 *ambiguous_decls = decl;
26404 /* The error message we have to print is too complicated for
26405 cp_parser_error, so we incorporate its actions directly. */
26406 if (!cp_parser_simulate_error (parser))
26408 error_at (name_location, "reference to %qD is ambiguous",
26409 name);
26410 print_candidates (decl);
26412 return error_mark_node;
26415 gcc_assert (DECL_P (decl)
26416 || TREE_CODE (decl) == OVERLOAD
26417 || TREE_CODE (decl) == SCOPE_REF
26418 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26419 || BASELINK_P (decl));
26421 /* If we have resolved the name of a member declaration, check to
26422 see if the declaration is accessible. When the name resolves to
26423 set of overloaded functions, accessibility is checked when
26424 overload resolution is done.
26426 During an explicit instantiation, access is not checked at all,
26427 as per [temp.explicit]. */
26428 if (DECL_P (decl))
26429 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26431 maybe_record_typedef_use (decl);
26433 return cp_expr (decl, name_location);
26436 /* Like cp_parser_lookup_name, but for use in the typical case where
26437 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26438 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26440 static tree
26441 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26443 return cp_parser_lookup_name (parser, name,
26444 none_type,
26445 /*is_template=*/false,
26446 /*is_namespace=*/false,
26447 /*check_dependency=*/true,
26448 /*ambiguous_decls=*/NULL,
26449 location);
26452 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26453 the current context, return the TYPE_DECL. If TAG_NAME_P is
26454 true, the DECL indicates the class being defined in a class-head,
26455 or declared in an elaborated-type-specifier.
26457 Otherwise, return DECL. */
26459 static tree
26460 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26462 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26463 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26465 struct A {
26466 template <typename T> struct B;
26469 template <typename T> struct A::B {};
26471 Similarly, in an elaborated-type-specifier:
26473 namespace N { struct X{}; }
26475 struct A {
26476 template <typename T> friend struct N::X;
26479 However, if the DECL refers to a class type, and we are in
26480 the scope of the class, then the name lookup automatically
26481 finds the TYPE_DECL created by build_self_reference rather
26482 than a TEMPLATE_DECL. For example, in:
26484 template <class T> struct S {
26485 S s;
26488 there is no need to handle such case. */
26490 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26491 return DECL_TEMPLATE_RESULT (decl);
26493 return decl;
26496 /* If too many, or too few, template-parameter lists apply to the
26497 declarator, issue an error message. Returns TRUE if all went well,
26498 and FALSE otherwise. */
26500 static bool
26501 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26502 cp_declarator *declarator,
26503 location_t declarator_location)
26505 switch (declarator->kind)
26507 case cdk_id:
26509 unsigned num_templates = 0;
26510 tree scope = declarator->u.id.qualifying_scope;
26511 bool template_id_p = false;
26513 if (scope)
26514 num_templates = num_template_headers_for_class (scope);
26515 else if (TREE_CODE (declarator->u.id.unqualified_name)
26516 == TEMPLATE_ID_EXPR)
26518 /* If the DECLARATOR has the form `X<y>' then it uses one
26519 additional level of template parameters. */
26520 ++num_templates;
26521 template_id_p = true;
26524 return cp_parser_check_template_parameters
26525 (parser, num_templates, template_id_p, declarator_location,
26526 declarator);
26529 case cdk_function:
26530 case cdk_array:
26531 case cdk_pointer:
26532 case cdk_reference:
26533 case cdk_ptrmem:
26534 return (cp_parser_check_declarator_template_parameters
26535 (parser, declarator->declarator, declarator_location));
26537 case cdk_decomp:
26538 case cdk_error:
26539 return true;
26541 default:
26542 gcc_unreachable ();
26544 return false;
26547 /* NUM_TEMPLATES were used in the current declaration. If that is
26548 invalid, return FALSE and issue an error messages. Otherwise,
26549 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26550 declarator and we can print more accurate diagnostics. */
26552 static bool
26553 cp_parser_check_template_parameters (cp_parser* parser,
26554 unsigned num_templates,
26555 bool template_id_p,
26556 location_t location,
26557 cp_declarator *declarator)
26559 /* If there are the same number of template classes and parameter
26560 lists, that's OK. */
26561 if (parser->num_template_parameter_lists == num_templates)
26562 return true;
26563 /* If there are more, but only one more, and the name ends in an identifier,
26564 then we are declaring a primary template. That's OK too. */
26565 if (!template_id_p
26566 && parser->num_template_parameter_lists == num_templates + 1)
26567 return true;
26568 /* If there are more template classes than parameter lists, we have
26569 something like:
26571 template <class T> void S<T>::R<T>::f (); */
26572 if (parser->num_template_parameter_lists < num_templates)
26574 if (declarator && !current_function_decl)
26575 error_at (location, "specializing member %<%T::%E%> "
26576 "requires %<template<>%> syntax",
26577 declarator->u.id.qualifying_scope,
26578 declarator->u.id.unqualified_name);
26579 else if (declarator)
26580 error_at (location, "invalid declaration of %<%T::%E%>",
26581 declarator->u.id.qualifying_scope,
26582 declarator->u.id.unqualified_name);
26583 else
26584 error_at (location, "too few template-parameter-lists");
26585 return false;
26587 /* Otherwise, there are too many template parameter lists. We have
26588 something like:
26590 template <class T> template <class U> void S::f(); */
26591 error_at (location, "too many template-parameter-lists");
26592 return false;
26595 /* Parse an optional `::' token indicating that the following name is
26596 from the global namespace. If so, PARSER->SCOPE is set to the
26597 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26598 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26599 Returns the new value of PARSER->SCOPE, if the `::' token is
26600 present, and NULL_TREE otherwise. */
26602 static tree
26603 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26605 cp_token *token;
26607 /* Peek at the next token. */
26608 token = cp_lexer_peek_token (parser->lexer);
26609 /* If we're looking at a `::' token then we're starting from the
26610 global namespace, not our current location. */
26611 if (token->type == CPP_SCOPE)
26613 /* Consume the `::' token. */
26614 cp_lexer_consume_token (parser->lexer);
26615 /* Set the SCOPE so that we know where to start the lookup. */
26616 parser->scope = global_namespace;
26617 parser->qualifying_scope = global_namespace;
26618 parser->object_scope = NULL_TREE;
26620 return parser->scope;
26622 else if (!current_scope_valid_p)
26624 parser->scope = NULL_TREE;
26625 parser->qualifying_scope = NULL_TREE;
26626 parser->object_scope = NULL_TREE;
26629 return NULL_TREE;
26632 /* Returns TRUE if the upcoming token sequence is the start of a
26633 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26634 declarator is preceded by the `friend' specifier. */
26636 static bool
26637 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26639 bool constructor_p;
26640 bool outside_class_specifier_p;
26641 tree nested_name_specifier;
26642 cp_token *next_token;
26644 /* The common case is that this is not a constructor declarator, so
26645 try to avoid doing lots of work if at all possible. It's not
26646 valid declare a constructor at function scope. */
26647 if (parser->in_function_body)
26648 return false;
26649 /* And only certain tokens can begin a constructor declarator. */
26650 next_token = cp_lexer_peek_token (parser->lexer);
26651 if (next_token->type != CPP_NAME
26652 && next_token->type != CPP_SCOPE
26653 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26654 && next_token->type != CPP_TEMPLATE_ID)
26655 return false;
26657 /* Parse tentatively; we are going to roll back all of the tokens
26658 consumed here. */
26659 cp_parser_parse_tentatively (parser);
26660 /* Assume that we are looking at a constructor declarator. */
26661 constructor_p = true;
26663 /* Look for the optional `::' operator. */
26664 cp_parser_global_scope_opt (parser,
26665 /*current_scope_valid_p=*/false);
26666 /* Look for the nested-name-specifier. */
26667 nested_name_specifier
26668 = (cp_parser_nested_name_specifier_opt (parser,
26669 /*typename_keyword_p=*/false,
26670 /*check_dependency_p=*/false,
26671 /*type_p=*/false,
26672 /*is_declaration=*/false));
26674 outside_class_specifier_p = (!at_class_scope_p ()
26675 || !TYPE_BEING_DEFINED (current_class_type)
26676 || friend_p);
26678 /* Outside of a class-specifier, there must be a
26679 nested-name-specifier. Except in C++17 mode, where we
26680 might be declaring a guiding declaration. */
26681 if (!nested_name_specifier && outside_class_specifier_p
26682 && cxx_dialect < cxx17)
26683 constructor_p = false;
26684 else if (nested_name_specifier == error_mark_node)
26685 constructor_p = false;
26687 /* If we have a class scope, this is easy; DR 147 says that S::S always
26688 names the constructor, and no other qualified name could. */
26689 if (constructor_p && nested_name_specifier
26690 && CLASS_TYPE_P (nested_name_specifier))
26692 tree id = cp_parser_unqualified_id (parser,
26693 /*template_keyword_p=*/false,
26694 /*check_dependency_p=*/false,
26695 /*declarator_p=*/true,
26696 /*optional_p=*/false);
26697 if (is_overloaded_fn (id))
26698 id = DECL_NAME (get_first_fn (id));
26699 if (!constructor_name_p (id, nested_name_specifier))
26700 constructor_p = false;
26702 /* If we still think that this might be a constructor-declarator,
26703 look for a class-name. */
26704 else if (constructor_p)
26706 /* If we have:
26708 template <typename T> struct S {
26709 S();
26712 we must recognize that the nested `S' names a class. */
26713 if (cxx_dialect >= cxx17)
26714 cp_parser_parse_tentatively (parser);
26716 tree type_decl;
26717 type_decl = cp_parser_class_name (parser,
26718 /*typename_keyword_p=*/false,
26719 /*template_keyword_p=*/false,
26720 none_type,
26721 /*check_dependency_p=*/false,
26722 /*class_head_p=*/false,
26723 /*is_declaration=*/false);
26725 if (cxx_dialect >= cxx17
26726 && !cp_parser_parse_definitely (parser))
26728 type_decl = NULL_TREE;
26729 tree tmpl = cp_parser_template_name (parser,
26730 /*template_keyword*/false,
26731 /*check_dependency_p*/false,
26732 /*is_declaration*/false,
26733 none_type,
26734 /*is_identifier*/NULL);
26735 if (DECL_CLASS_TEMPLATE_P (tmpl)
26736 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26737 /* It's a deduction guide, return true. */;
26738 else
26739 cp_parser_simulate_error (parser);
26742 /* If there was no class-name, then this is not a constructor.
26743 Otherwise, if we are in a class-specifier and we aren't
26744 handling a friend declaration, check that its type matches
26745 current_class_type (c++/38313). Note: error_mark_node
26746 is left alone for error recovery purposes. */
26747 constructor_p = (!cp_parser_error_occurred (parser)
26748 && (outside_class_specifier_p
26749 || type_decl == NULL_TREE
26750 || type_decl == error_mark_node
26751 || same_type_p (current_class_type,
26752 TREE_TYPE (type_decl))));
26754 /* If we're still considering a constructor, we have to see a `(',
26755 to begin the parameter-declaration-clause, followed by either a
26756 `)', an `...', or a decl-specifier. We need to check for a
26757 type-specifier to avoid being fooled into thinking that:
26759 S (f) (int);
26761 is a constructor. (It is actually a function named `f' that
26762 takes one parameter (of type `int') and returns a value of type
26763 `S'. */
26764 if (constructor_p
26765 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26766 constructor_p = false;
26768 if (constructor_p
26769 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26770 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26771 /* A parameter declaration begins with a decl-specifier,
26772 which is either the "attribute" keyword, a storage class
26773 specifier, or (usually) a type-specifier. */
26774 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26776 tree type;
26777 tree pushed_scope = NULL_TREE;
26778 unsigned saved_num_template_parameter_lists;
26780 /* Names appearing in the type-specifier should be looked up
26781 in the scope of the class. */
26782 if (current_class_type)
26783 type = NULL_TREE;
26784 else if (type_decl)
26786 type = TREE_TYPE (type_decl);
26787 if (TREE_CODE (type) == TYPENAME_TYPE)
26789 type = resolve_typename_type (type,
26790 /*only_current_p=*/false);
26791 if (TREE_CODE (type) == TYPENAME_TYPE)
26793 cp_parser_abort_tentative_parse (parser);
26794 return false;
26797 pushed_scope = push_scope (type);
26800 /* Inside the constructor parameter list, surrounding
26801 template-parameter-lists do not apply. */
26802 saved_num_template_parameter_lists
26803 = parser->num_template_parameter_lists;
26804 parser->num_template_parameter_lists = 0;
26806 /* Look for the type-specifier. */
26807 cp_parser_type_specifier (parser,
26808 CP_PARSER_FLAGS_NONE,
26809 /*decl_specs=*/NULL,
26810 /*is_declarator=*/true,
26811 /*declares_class_or_enum=*/NULL,
26812 /*is_cv_qualifier=*/NULL);
26814 parser->num_template_parameter_lists
26815 = saved_num_template_parameter_lists;
26817 /* Leave the scope of the class. */
26818 if (pushed_scope)
26819 pop_scope (pushed_scope);
26821 constructor_p = !cp_parser_error_occurred (parser);
26825 /* We did not really want to consume any tokens. */
26826 cp_parser_abort_tentative_parse (parser);
26828 return constructor_p;
26831 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26832 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26833 they must be performed once we are in the scope of the function.
26835 Returns the function defined. */
26837 static tree
26838 cp_parser_function_definition_from_specifiers_and_declarator
26839 (cp_parser* parser,
26840 cp_decl_specifier_seq *decl_specifiers,
26841 tree attributes,
26842 const cp_declarator *declarator)
26844 tree fn;
26845 bool success_p;
26847 /* Begin the function-definition. */
26848 success_p = start_function (decl_specifiers, declarator, attributes);
26850 /* The things we're about to see are not directly qualified by any
26851 template headers we've seen thus far. */
26852 reset_specialization ();
26854 /* If there were names looked up in the decl-specifier-seq that we
26855 did not check, check them now. We must wait until we are in the
26856 scope of the function to perform the checks, since the function
26857 might be a friend. */
26858 perform_deferred_access_checks (tf_warning_or_error);
26860 if (success_p)
26862 cp_finalize_omp_declare_simd (parser, current_function_decl);
26863 parser->omp_declare_simd = NULL;
26864 cp_finalize_oacc_routine (parser, current_function_decl, true);
26865 parser->oacc_routine = NULL;
26868 if (!success_p)
26870 /* Skip the entire function. */
26871 cp_parser_skip_to_end_of_block_or_statement (parser);
26872 fn = error_mark_node;
26874 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26876 /* Seen already, skip it. An error message has already been output. */
26877 cp_parser_skip_to_end_of_block_or_statement (parser);
26878 fn = current_function_decl;
26879 current_function_decl = NULL_TREE;
26880 /* If this is a function from a class, pop the nested class. */
26881 if (current_class_name)
26882 pop_nested_class ();
26884 else
26886 timevar_id_t tv;
26887 if (DECL_DECLARED_INLINE_P (current_function_decl))
26888 tv = TV_PARSE_INLINE;
26889 else
26890 tv = TV_PARSE_FUNC;
26891 timevar_push (tv);
26892 fn = cp_parser_function_definition_after_declarator (parser,
26893 /*inline_p=*/false);
26894 timevar_pop (tv);
26897 return fn;
26900 /* Parse the part of a function-definition that follows the
26901 declarator. INLINE_P is TRUE iff this function is an inline
26902 function defined within a class-specifier.
26904 Returns the function defined. */
26906 static tree
26907 cp_parser_function_definition_after_declarator (cp_parser* parser,
26908 bool inline_p)
26910 tree fn;
26911 bool saved_in_unbraced_linkage_specification_p;
26912 bool saved_in_function_body;
26913 unsigned saved_num_template_parameter_lists;
26914 cp_token *token;
26915 bool fully_implicit_function_template_p
26916 = parser->fully_implicit_function_template_p;
26917 parser->fully_implicit_function_template_p = false;
26918 tree implicit_template_parms
26919 = parser->implicit_template_parms;
26920 parser->implicit_template_parms = 0;
26921 cp_binding_level* implicit_template_scope
26922 = parser->implicit_template_scope;
26923 parser->implicit_template_scope = 0;
26925 saved_in_function_body = parser->in_function_body;
26926 parser->in_function_body = true;
26927 /* If the next token is `return', then the code may be trying to
26928 make use of the "named return value" extension that G++ used to
26929 support. */
26930 token = cp_lexer_peek_token (parser->lexer);
26931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26933 /* Consume the `return' keyword. */
26934 cp_lexer_consume_token (parser->lexer);
26935 /* Look for the identifier that indicates what value is to be
26936 returned. */
26937 cp_parser_identifier (parser);
26938 /* Issue an error message. */
26939 error_at (token->location,
26940 "named return values are no longer supported");
26941 /* Skip tokens until we reach the start of the function body. */
26942 while (true)
26944 cp_token *token = cp_lexer_peek_token (parser->lexer);
26945 if (token->type == CPP_OPEN_BRACE
26946 || token->type == CPP_EOF
26947 || token->type == CPP_PRAGMA_EOL)
26948 break;
26949 cp_lexer_consume_token (parser->lexer);
26952 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26953 anything declared inside `f'. */
26954 saved_in_unbraced_linkage_specification_p
26955 = parser->in_unbraced_linkage_specification_p;
26956 parser->in_unbraced_linkage_specification_p = false;
26957 /* Inside the function, surrounding template-parameter-lists do not
26958 apply. */
26959 saved_num_template_parameter_lists
26960 = parser->num_template_parameter_lists;
26961 parser->num_template_parameter_lists = 0;
26963 /* If the next token is `try', `__transaction_atomic', or
26964 `__transaction_relaxed`, then we are looking at either function-try-block
26965 or function-transaction-block. Note that all of these include the
26966 function-body. */
26967 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26968 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26969 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26970 RID_TRANSACTION_RELAXED))
26971 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26972 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26973 cp_parser_function_try_block (parser);
26974 else
26975 cp_parser_ctor_initializer_opt_and_function_body
26976 (parser, /*in_function_try_block=*/false);
26978 /* Finish the function. */
26979 fn = finish_function (inline_p);
26980 /* Generate code for it, if necessary. */
26981 expand_or_defer_fn (fn);
26982 /* Restore the saved values. */
26983 parser->in_unbraced_linkage_specification_p
26984 = saved_in_unbraced_linkage_specification_p;
26985 parser->num_template_parameter_lists
26986 = saved_num_template_parameter_lists;
26987 parser->in_function_body = saved_in_function_body;
26989 parser->fully_implicit_function_template_p
26990 = fully_implicit_function_template_p;
26991 parser->implicit_template_parms
26992 = implicit_template_parms;
26993 parser->implicit_template_scope
26994 = implicit_template_scope;
26996 if (parser->fully_implicit_function_template_p)
26997 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26999 return fn;
27002 /* Parse a template-declaration body (following argument list). */
27004 static void
27005 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27006 tree parameter_list,
27007 bool member_p)
27009 tree decl = NULL_TREE;
27010 bool friend_p = false;
27012 /* We just processed one more parameter list. */
27013 ++parser->num_template_parameter_lists;
27015 /* Get the deferred access checks from the parameter list. These
27016 will be checked once we know what is being declared, as for a
27017 member template the checks must be performed in the scope of the
27018 class containing the member. */
27019 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27021 /* Tentatively parse for a new template parameter list, which can either be
27022 the template keyword or a template introduction. */
27023 if (cp_parser_template_declaration_after_export (parser, member_p))
27024 /* OK */;
27025 else if (cxx_dialect >= cxx11
27026 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27027 decl = cp_parser_alias_declaration (parser);
27028 else
27030 /* There are no access checks when parsing a template, as we do not
27031 know if a specialization will be a friend. */
27032 push_deferring_access_checks (dk_no_check);
27033 cp_token *token = cp_lexer_peek_token (parser->lexer);
27034 decl = cp_parser_single_declaration (parser,
27035 checks,
27036 member_p,
27037 /*explicit_specialization_p=*/false,
27038 &friend_p);
27039 pop_deferring_access_checks ();
27041 /* If this is a member template declaration, let the front
27042 end know. */
27043 if (member_p && !friend_p && decl)
27045 if (TREE_CODE (decl) == TYPE_DECL)
27046 cp_parser_check_access_in_redeclaration (decl, token->location);
27048 decl = finish_member_template_decl (decl);
27050 else if (friend_p && decl
27051 && DECL_DECLARES_TYPE_P (decl))
27052 make_friend_class (current_class_type, TREE_TYPE (decl),
27053 /*complain=*/true);
27055 /* We are done with the current parameter list. */
27056 --parser->num_template_parameter_lists;
27058 pop_deferring_access_checks ();
27060 /* Finish up. */
27061 finish_template_decl (parameter_list);
27063 /* Check the template arguments for a literal operator template. */
27064 if (decl
27065 && DECL_DECLARES_FUNCTION_P (decl)
27066 && UDLIT_OPER_P (DECL_NAME (decl)))
27068 bool ok = true;
27069 if (parameter_list == NULL_TREE)
27070 ok = false;
27071 else
27073 int num_parms = TREE_VEC_LENGTH (parameter_list);
27074 if (num_parms == 1)
27076 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27077 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27078 if (TREE_TYPE (parm) != char_type_node
27079 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27080 ok = false;
27082 else if (num_parms == 2 && cxx_dialect >= cxx14)
27084 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27085 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27086 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27087 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27088 if (parm == error_mark_node
27089 || TREE_TYPE (parm) != TREE_TYPE (type)
27090 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27091 ok = false;
27093 else
27094 ok = false;
27096 if (!ok)
27098 if (cxx_dialect >= cxx14)
27099 error ("literal operator template %qD has invalid parameter list."
27100 " Expected non-type template argument pack <char...>"
27101 " or <typename CharT, CharT...>",
27102 decl);
27103 else
27104 error ("literal operator template %qD has invalid parameter list."
27105 " Expected non-type template argument pack <char...>",
27106 decl);
27110 /* Register member declarations. */
27111 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27112 finish_member_declaration (decl);
27113 /* If DECL is a function template, we must return to parse it later.
27114 (Even though there is no definition, there might be default
27115 arguments that need handling.) */
27116 if (member_p && decl
27117 && DECL_DECLARES_FUNCTION_P (decl))
27118 vec_safe_push (unparsed_funs_with_definitions, decl);
27121 /* Parse a template introduction header for a template-declaration. Returns
27122 false if tentative parse fails. */
27124 static bool
27125 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27127 cp_parser_parse_tentatively (parser);
27129 tree saved_scope = parser->scope;
27130 tree saved_object_scope = parser->object_scope;
27131 tree saved_qualifying_scope = parser->qualifying_scope;
27133 /* Look for the optional `::' operator. */
27134 cp_parser_global_scope_opt (parser,
27135 /*current_scope_valid_p=*/false);
27136 /* Look for the nested-name-specifier. */
27137 cp_parser_nested_name_specifier_opt (parser,
27138 /*typename_keyword_p=*/false,
27139 /*check_dependency_p=*/true,
27140 /*type_p=*/false,
27141 /*is_declaration=*/false);
27143 cp_token *token = cp_lexer_peek_token (parser->lexer);
27144 tree concept_name = cp_parser_identifier (parser);
27146 /* Look up the concept for which we will be matching
27147 template parameters. */
27148 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27149 token->location);
27150 parser->scope = saved_scope;
27151 parser->object_scope = saved_object_scope;
27152 parser->qualifying_scope = saved_qualifying_scope;
27154 if (concept_name == error_mark_node)
27155 cp_parser_simulate_error (parser);
27157 /* Look for opening brace for introduction. */
27158 matching_braces braces;
27159 braces.require_open (parser);
27161 if (!cp_parser_parse_definitely (parser))
27162 return false;
27164 push_deferring_access_checks (dk_deferred);
27166 /* Build vector of placeholder parameters and grab
27167 matching identifiers. */
27168 tree introduction_list = cp_parser_introduction_list (parser);
27170 /* The introduction-list shall not be empty. */
27171 int nargs = TREE_VEC_LENGTH (introduction_list);
27172 if (nargs == 0)
27174 error ("empty introduction-list");
27175 return true;
27178 /* Look for closing brace for introduction. */
27179 if (!braces.require_close (parser))
27180 return true;
27182 if (tmpl_decl == error_mark_node)
27184 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27185 token->location);
27186 return true;
27189 /* Build and associate the constraint. */
27190 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27191 if (parms && parms != error_mark_node)
27193 cp_parser_template_declaration_after_parameters (parser, parms,
27194 member_p);
27195 return true;
27198 error_at (token->location, "no matching concept for template-introduction");
27199 return true;
27202 /* Parse a normal template-declaration following the template keyword. */
27204 static void
27205 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27207 tree parameter_list;
27208 bool need_lang_pop;
27209 location_t location = input_location;
27211 /* Look for the `<' token. */
27212 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27213 return;
27214 if (at_class_scope_p () && current_function_decl)
27216 /* 14.5.2.2 [temp.mem]
27218 A local class shall not have member templates. */
27219 error_at (location,
27220 "invalid declaration of member template in local class");
27221 cp_parser_skip_to_end_of_block_or_statement (parser);
27222 return;
27224 /* [temp]
27226 A template ... shall not have C linkage. */
27227 if (current_lang_name == lang_name_c)
27229 error_at (location, "template with C linkage");
27230 maybe_show_extern_c_location ();
27231 /* Give it C++ linkage to avoid confusing other parts of the
27232 front end. */
27233 push_lang_context (lang_name_cplusplus);
27234 need_lang_pop = true;
27236 else
27237 need_lang_pop = false;
27239 /* We cannot perform access checks on the template parameter
27240 declarations until we know what is being declared, just as we
27241 cannot check the decl-specifier list. */
27242 push_deferring_access_checks (dk_deferred);
27244 /* If the next token is `>', then we have an invalid
27245 specialization. Rather than complain about an invalid template
27246 parameter, issue an error message here. */
27247 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27249 cp_parser_error (parser, "invalid explicit specialization");
27250 begin_specialization ();
27251 parameter_list = NULL_TREE;
27253 else
27255 /* Parse the template parameters. */
27256 parameter_list = cp_parser_template_parameter_list (parser);
27259 /* Look for the `>'. */
27260 cp_parser_skip_to_end_of_template_parameter_list (parser);
27262 /* Manage template requirements */
27263 if (flag_concepts)
27265 tree reqs = get_shorthand_constraints (current_template_parms);
27266 if (tree r = cp_parser_requires_clause_opt (parser))
27267 reqs = conjoin_constraints (reqs, normalize_expression (r));
27268 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27271 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27272 member_p);
27274 /* For the erroneous case of a template with C linkage, we pushed an
27275 implicit C++ linkage scope; exit that scope now. */
27276 if (need_lang_pop)
27277 pop_lang_context ();
27280 /* Parse a template-declaration, assuming that the `export' (and
27281 `extern') keywords, if present, has already been scanned. MEMBER_P
27282 is as for cp_parser_template_declaration. */
27284 static bool
27285 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27289 cp_lexer_consume_token (parser->lexer);
27290 cp_parser_explicit_template_declaration (parser, member_p);
27291 return true;
27293 else if (flag_concepts)
27294 return cp_parser_template_introduction (parser, member_p);
27296 return false;
27299 /* Perform the deferred access checks from a template-parameter-list.
27300 CHECKS is a TREE_LIST of access checks, as returned by
27301 get_deferred_access_checks. */
27303 static void
27304 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27306 ++processing_template_parmlist;
27307 perform_access_checks (checks, tf_warning_or_error);
27308 --processing_template_parmlist;
27311 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27312 `function-definition' sequence that follows a template header.
27313 If MEMBER_P is true, this declaration appears in a class scope.
27315 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27316 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27318 static tree
27319 cp_parser_single_declaration (cp_parser* parser,
27320 vec<deferred_access_check, va_gc> *checks,
27321 bool member_p,
27322 bool explicit_specialization_p,
27323 bool* friend_p)
27325 int declares_class_or_enum;
27326 tree decl = NULL_TREE;
27327 cp_decl_specifier_seq decl_specifiers;
27328 bool function_definition_p = false;
27329 cp_token *decl_spec_token_start;
27331 /* This function is only used when processing a template
27332 declaration. */
27333 gcc_assert (innermost_scope_kind () == sk_template_parms
27334 || innermost_scope_kind () == sk_template_spec);
27336 /* Defer access checks until we know what is being declared. */
27337 push_deferring_access_checks (dk_deferred);
27339 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27340 alternative. */
27341 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27342 cp_parser_decl_specifier_seq (parser,
27343 CP_PARSER_FLAGS_OPTIONAL,
27344 &decl_specifiers,
27345 &declares_class_or_enum);
27346 if (friend_p)
27347 *friend_p = cp_parser_friend_p (&decl_specifiers);
27349 /* There are no template typedefs. */
27350 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27352 error_at (decl_spec_token_start->location,
27353 "template declaration of %<typedef%>");
27354 decl = error_mark_node;
27357 /* Gather up the access checks that occurred the
27358 decl-specifier-seq. */
27359 stop_deferring_access_checks ();
27361 /* Check for the declaration of a template class. */
27362 if (declares_class_or_enum)
27364 if (cp_parser_declares_only_class_p (parser)
27365 || (declares_class_or_enum & 2))
27367 // If this is a declaration, but not a definition, associate
27368 // any constraints with the type declaration. Constraints
27369 // are associated with definitions in cp_parser_class_specifier.
27370 if (declares_class_or_enum == 1)
27371 associate_classtype_constraints (decl_specifiers.type);
27373 decl = shadow_tag (&decl_specifiers);
27375 /* In this case:
27377 struct C {
27378 friend template <typename T> struct A<T>::B;
27381 A<T>::B will be represented by a TYPENAME_TYPE, and
27382 therefore not recognized by shadow_tag. */
27383 if (friend_p && *friend_p
27384 && !decl
27385 && decl_specifiers.type
27386 && TYPE_P (decl_specifiers.type))
27387 decl = decl_specifiers.type;
27389 if (decl && decl != error_mark_node)
27390 decl = TYPE_NAME (decl);
27391 else
27392 decl = error_mark_node;
27394 /* Perform access checks for template parameters. */
27395 cp_parser_perform_template_parameter_access_checks (checks);
27397 /* Give a helpful diagnostic for
27398 template <class T> struct A { } a;
27399 if we aren't already recovering from an error. */
27400 if (!cp_parser_declares_only_class_p (parser)
27401 && !seen_error ())
27403 error_at (cp_lexer_peek_token (parser->lexer)->location,
27404 "a class template declaration must not declare "
27405 "anything else");
27406 cp_parser_skip_to_end_of_block_or_statement (parser);
27407 goto out;
27412 /* Complain about missing 'typename' or other invalid type names. */
27413 if (!decl_specifiers.any_type_specifiers_p
27414 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27416 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27417 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27418 the rest of this declaration. */
27419 decl = error_mark_node;
27420 goto out;
27423 /* If it's not a template class, try for a template function. If
27424 the next token is a `;', then this declaration does not declare
27425 anything. But, if there were errors in the decl-specifiers, then
27426 the error might well have come from an attempted class-specifier.
27427 In that case, there's no need to warn about a missing declarator. */
27428 if (!decl
27429 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27430 || decl_specifiers.type != error_mark_node))
27432 decl = cp_parser_init_declarator (parser,
27433 &decl_specifiers,
27434 checks,
27435 /*function_definition_allowed_p=*/true,
27436 member_p,
27437 declares_class_or_enum,
27438 &function_definition_p,
27439 NULL, NULL, NULL);
27441 /* 7.1.1-1 [dcl.stc]
27443 A storage-class-specifier shall not be specified in an explicit
27444 specialization... */
27445 if (decl
27446 && explicit_specialization_p
27447 && decl_specifiers.storage_class != sc_none)
27449 error_at (decl_spec_token_start->location,
27450 "explicit template specialization cannot have a storage class");
27451 decl = error_mark_node;
27454 if (decl && VAR_P (decl))
27455 check_template_variable (decl);
27458 /* Look for a trailing `;' after the declaration. */
27459 if (!function_definition_p
27460 && (decl == error_mark_node
27461 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27462 cp_parser_skip_to_end_of_block_or_statement (parser);
27464 out:
27465 pop_deferring_access_checks ();
27467 /* Clear any current qualification; whatever comes next is the start
27468 of something new. */
27469 parser->scope = NULL_TREE;
27470 parser->qualifying_scope = NULL_TREE;
27471 parser->object_scope = NULL_TREE;
27473 return decl;
27476 /* Parse a cast-expression that is not the operand of a unary "&". */
27478 static cp_expr
27479 cp_parser_simple_cast_expression (cp_parser *parser)
27481 return cp_parser_cast_expression (parser, /*address_p=*/false,
27482 /*cast_p=*/false, /*decltype*/false, NULL);
27485 /* Parse a functional cast to TYPE. Returns an expression
27486 representing the cast. */
27488 static cp_expr
27489 cp_parser_functional_cast (cp_parser* parser, tree type)
27491 vec<tree, va_gc> *vec;
27492 tree expression_list;
27493 cp_expr cast;
27494 bool nonconst_p;
27496 location_t start_loc = input_location;
27498 if (!type)
27499 type = error_mark_node;
27501 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27503 cp_lexer_set_source_position (parser->lexer);
27504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27505 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27506 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27507 if (TREE_CODE (type) == TYPE_DECL)
27508 type = TREE_TYPE (type);
27510 cast = finish_compound_literal (type, expression_list,
27511 tf_warning_or_error, fcl_functional);
27512 /* Create a location of the form:
27513 type_name{i, f}
27514 ^~~~~~~~~~~~~~~
27515 with caret == start at the start of the type name,
27516 finishing at the closing brace. */
27517 location_t finish_loc
27518 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27519 location_t combined_loc = make_location (start_loc, start_loc,
27520 finish_loc);
27521 cast.set_location (combined_loc);
27522 return cast;
27526 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27527 /*cast_p=*/true,
27528 /*allow_expansion_p=*/true,
27529 /*non_constant_p=*/NULL);
27530 if (vec == NULL)
27531 expression_list = error_mark_node;
27532 else
27534 expression_list = build_tree_list_vec (vec);
27535 release_tree_vector (vec);
27538 cast = build_functional_cast (type, expression_list,
27539 tf_warning_or_error);
27540 /* [expr.const]/1: In an integral constant expression "only type
27541 conversions to integral or enumeration type can be used". */
27542 if (TREE_CODE (type) == TYPE_DECL)
27543 type = TREE_TYPE (type);
27544 if (cast != error_mark_node
27545 && !cast_valid_in_integral_constant_expression_p (type)
27546 && cp_parser_non_integral_constant_expression (parser,
27547 NIC_CONSTRUCTOR))
27548 return error_mark_node;
27550 /* Create a location of the form:
27551 float(i)
27552 ^~~~~~~~
27553 with caret == start at the start of the type name,
27554 finishing at the closing paren. */
27555 location_t finish_loc
27556 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27557 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27558 cast.set_location (combined_loc);
27559 return cast;
27562 /* Save the tokens that make up the body of a member function defined
27563 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27564 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27565 specifiers applied to the declaration. Returns the FUNCTION_DECL
27566 for the member function. */
27568 static tree
27569 cp_parser_save_member_function_body (cp_parser* parser,
27570 cp_decl_specifier_seq *decl_specifiers,
27571 cp_declarator *declarator,
27572 tree attributes)
27574 cp_token *first;
27575 cp_token *last;
27576 tree fn;
27577 bool function_try_block = false;
27579 /* Create the FUNCTION_DECL. */
27580 fn = grokmethod (decl_specifiers, declarator, attributes);
27581 cp_finalize_omp_declare_simd (parser, fn);
27582 cp_finalize_oacc_routine (parser, fn, true);
27583 /* If something went badly wrong, bail out now. */
27584 if (fn == error_mark_node)
27586 /* If there's a function-body, skip it. */
27587 if (cp_parser_token_starts_function_definition_p
27588 (cp_lexer_peek_token (parser->lexer)))
27589 cp_parser_skip_to_end_of_block_or_statement (parser);
27590 return error_mark_node;
27593 /* Remember it, if there default args to post process. */
27594 cp_parser_save_default_args (parser, fn);
27596 /* Save away the tokens that make up the body of the
27597 function. */
27598 first = parser->lexer->next_token;
27600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27601 cp_lexer_consume_token (parser->lexer);
27602 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27603 RID_TRANSACTION_ATOMIC))
27605 cp_lexer_consume_token (parser->lexer);
27606 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27607 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27608 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27609 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27610 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27611 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27612 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27614 cp_lexer_consume_token (parser->lexer);
27615 cp_lexer_consume_token (parser->lexer);
27616 cp_lexer_consume_token (parser->lexer);
27617 cp_lexer_consume_token (parser->lexer);
27618 cp_lexer_consume_token (parser->lexer);
27620 else
27621 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27622 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27624 cp_lexer_consume_token (parser->lexer);
27625 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27626 break;
27630 /* Handle function try blocks. */
27631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27633 cp_lexer_consume_token (parser->lexer);
27634 function_try_block = true;
27636 /* We can have braced-init-list mem-initializers before the fn body. */
27637 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27639 cp_lexer_consume_token (parser->lexer);
27640 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27642 /* cache_group will stop after an un-nested { } pair, too. */
27643 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27644 break;
27646 /* variadic mem-inits have ... after the ')'. */
27647 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27648 cp_lexer_consume_token (parser->lexer);
27651 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27652 /* Handle function try blocks. */
27653 if (function_try_block)
27654 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27655 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27656 last = parser->lexer->next_token;
27658 /* Save away the inline definition; we will process it when the
27659 class is complete. */
27660 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27661 DECL_PENDING_INLINE_P (fn) = 1;
27663 /* We need to know that this was defined in the class, so that
27664 friend templates are handled correctly. */
27665 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27667 /* Add FN to the queue of functions to be parsed later. */
27668 vec_safe_push (unparsed_funs_with_definitions, fn);
27670 return fn;
27673 /* Save the tokens that make up the in-class initializer for a non-static
27674 data member. Returns a DEFAULT_ARG. */
27676 static tree
27677 cp_parser_save_nsdmi (cp_parser* parser)
27679 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27682 /* Parse a template-argument-list, as well as the trailing ">" (but
27683 not the opening "<"). See cp_parser_template_argument_list for the
27684 return value. */
27686 static tree
27687 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27689 tree arguments;
27690 tree saved_scope;
27691 tree saved_qualifying_scope;
27692 tree saved_object_scope;
27693 bool saved_greater_than_is_operator_p;
27694 int saved_unevaluated_operand;
27695 int saved_inhibit_evaluation_warnings;
27697 /* [temp.names]
27699 When parsing a template-id, the first non-nested `>' is taken as
27700 the end of the template-argument-list rather than a greater-than
27701 operator. */
27702 saved_greater_than_is_operator_p
27703 = parser->greater_than_is_operator_p;
27704 parser->greater_than_is_operator_p = false;
27705 /* Parsing the argument list may modify SCOPE, so we save it
27706 here. */
27707 saved_scope = parser->scope;
27708 saved_qualifying_scope = parser->qualifying_scope;
27709 saved_object_scope = parser->object_scope;
27710 /* We need to evaluate the template arguments, even though this
27711 template-id may be nested within a "sizeof". */
27712 saved_unevaluated_operand = cp_unevaluated_operand;
27713 cp_unevaluated_operand = 0;
27714 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27715 c_inhibit_evaluation_warnings = 0;
27716 /* Parse the template-argument-list itself. */
27717 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27718 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27719 arguments = NULL_TREE;
27720 else
27721 arguments = cp_parser_template_argument_list (parser);
27722 /* Look for the `>' that ends the template-argument-list. If we find
27723 a '>>' instead, it's probably just a typo. */
27724 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27726 if (cxx_dialect != cxx98)
27728 /* In C++0x, a `>>' in a template argument list or cast
27729 expression is considered to be two separate `>'
27730 tokens. So, change the current token to a `>', but don't
27731 consume it: it will be consumed later when the outer
27732 template argument list (or cast expression) is parsed.
27733 Note that this replacement of `>' for `>>' is necessary
27734 even if we are parsing tentatively: in the tentative
27735 case, after calling
27736 cp_parser_enclosed_template_argument_list we will always
27737 throw away all of the template arguments and the first
27738 closing `>', either because the template argument list
27739 was erroneous or because we are replacing those tokens
27740 with a CPP_TEMPLATE_ID token. The second `>' (which will
27741 not have been thrown away) is needed either to close an
27742 outer template argument list or to complete a new-style
27743 cast. */
27744 cp_token *token = cp_lexer_peek_token (parser->lexer);
27745 token->type = CPP_GREATER;
27747 else if (!saved_greater_than_is_operator_p)
27749 /* If we're in a nested template argument list, the '>>' has
27750 to be a typo for '> >'. We emit the error message, but we
27751 continue parsing and we push a '>' as next token, so that
27752 the argument list will be parsed correctly. Note that the
27753 global source location is still on the token before the
27754 '>>', so we need to say explicitly where we want it. */
27755 cp_token *token = cp_lexer_peek_token (parser->lexer);
27756 gcc_rich_location richloc (token->location);
27757 richloc.add_fixit_replace ("> >");
27758 error_at (&richloc, "%<>>%> should be %<> >%> "
27759 "within a nested template argument list");
27761 token->type = CPP_GREATER;
27763 else
27765 /* If this is not a nested template argument list, the '>>'
27766 is a typo for '>'. Emit an error message and continue.
27767 Same deal about the token location, but here we can get it
27768 right by consuming the '>>' before issuing the diagnostic. */
27769 cp_token *token = cp_lexer_consume_token (parser->lexer);
27770 error_at (token->location,
27771 "spurious %<>>%>, use %<>%> to terminate "
27772 "a template argument list");
27775 else
27776 cp_parser_skip_to_end_of_template_parameter_list (parser);
27777 /* The `>' token might be a greater-than operator again now. */
27778 parser->greater_than_is_operator_p
27779 = saved_greater_than_is_operator_p;
27780 /* Restore the SAVED_SCOPE. */
27781 parser->scope = saved_scope;
27782 parser->qualifying_scope = saved_qualifying_scope;
27783 parser->object_scope = saved_object_scope;
27784 cp_unevaluated_operand = saved_unevaluated_operand;
27785 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27787 return arguments;
27790 /* MEMBER_FUNCTION is a member function, or a friend. If default
27791 arguments, or the body of the function have not yet been parsed,
27792 parse them now. */
27794 static void
27795 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27797 timevar_push (TV_PARSE_INMETH);
27798 /* If this member is a template, get the underlying
27799 FUNCTION_DECL. */
27800 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27801 member_function = DECL_TEMPLATE_RESULT (member_function);
27803 /* There should not be any class definitions in progress at this
27804 point; the bodies of members are only parsed outside of all class
27805 definitions. */
27806 gcc_assert (parser->num_classes_being_defined == 0);
27807 /* While we're parsing the member functions we might encounter more
27808 classes. We want to handle them right away, but we don't want
27809 them getting mixed up with functions that are currently in the
27810 queue. */
27811 push_unparsed_function_queues (parser);
27813 /* Make sure that any template parameters are in scope. */
27814 maybe_begin_member_template_processing (member_function);
27816 /* If the body of the function has not yet been parsed, parse it
27817 now. */
27818 if (DECL_PENDING_INLINE_P (member_function))
27820 tree function_scope;
27821 cp_token_cache *tokens;
27823 /* The function is no longer pending; we are processing it. */
27824 tokens = DECL_PENDING_INLINE_INFO (member_function);
27825 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27826 DECL_PENDING_INLINE_P (member_function) = 0;
27828 /* If this is a local class, enter the scope of the containing
27829 function. */
27830 function_scope = current_function_decl;
27831 if (function_scope)
27832 push_function_context ();
27834 /* Push the body of the function onto the lexer stack. */
27835 cp_parser_push_lexer_for_tokens (parser, tokens);
27837 /* Let the front end know that we going to be defining this
27838 function. */
27839 start_preparsed_function (member_function, NULL_TREE,
27840 SF_PRE_PARSED | SF_INCLASS_INLINE);
27842 /* Don't do access checking if it is a templated function. */
27843 if (processing_template_decl)
27844 push_deferring_access_checks (dk_no_check);
27846 /* #pragma omp declare reduction needs special parsing. */
27847 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27849 parser->lexer->in_pragma = true;
27850 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27851 finish_function (/*inline_p=*/true);
27852 cp_check_omp_declare_reduction (member_function);
27854 else
27855 /* Now, parse the body of the function. */
27856 cp_parser_function_definition_after_declarator (parser,
27857 /*inline_p=*/true);
27859 if (processing_template_decl)
27860 pop_deferring_access_checks ();
27862 /* Leave the scope of the containing function. */
27863 if (function_scope)
27864 pop_function_context ();
27865 cp_parser_pop_lexer (parser);
27868 /* Remove any template parameters from the symbol table. */
27869 maybe_end_member_template_processing ();
27871 /* Restore the queue. */
27872 pop_unparsed_function_queues (parser);
27873 timevar_pop (TV_PARSE_INMETH);
27876 /* If DECL contains any default args, remember it on the unparsed
27877 functions queue. */
27879 static void
27880 cp_parser_save_default_args (cp_parser* parser, tree decl)
27882 tree probe;
27884 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27885 probe;
27886 probe = TREE_CHAIN (probe))
27887 if (TREE_PURPOSE (probe))
27889 cp_default_arg_entry entry = {current_class_type, decl};
27890 vec_safe_push (unparsed_funs_with_default_args, entry);
27891 break;
27895 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27896 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27897 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27898 from the parameter-type-list. */
27900 static tree
27901 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27902 tree default_arg, tree parmtype)
27904 cp_token_cache *tokens;
27905 tree parsed_arg;
27906 bool dummy;
27908 if (default_arg == error_mark_node)
27909 return error_mark_node;
27911 /* Push the saved tokens for the default argument onto the parser's
27912 lexer stack. */
27913 tokens = DEFARG_TOKENS (default_arg);
27914 cp_parser_push_lexer_for_tokens (parser, tokens);
27916 start_lambda_scope (decl);
27918 /* Parse the default argument. */
27919 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27920 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27921 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27923 finish_lambda_scope ();
27925 if (parsed_arg == error_mark_node)
27926 cp_parser_skip_to_end_of_statement (parser);
27928 if (!processing_template_decl)
27930 /* In a non-template class, check conversions now. In a template,
27931 we'll wait and instantiate these as needed. */
27932 if (TREE_CODE (decl) == PARM_DECL)
27933 parsed_arg = check_default_argument (parmtype, parsed_arg,
27934 tf_warning_or_error);
27935 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27936 parsed_arg = error_mark_node;
27937 else
27938 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27941 /* If the token stream has not been completely used up, then
27942 there was extra junk after the end of the default
27943 argument. */
27944 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27946 if (TREE_CODE (decl) == PARM_DECL)
27947 cp_parser_error (parser, "expected %<,%>");
27948 else
27949 cp_parser_error (parser, "expected %<;%>");
27952 /* Revert to the main lexer. */
27953 cp_parser_pop_lexer (parser);
27955 return parsed_arg;
27958 /* FIELD is a non-static data member with an initializer which we saved for
27959 later; parse it now. */
27961 static void
27962 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27964 tree def;
27966 maybe_begin_member_template_processing (field);
27968 push_unparsed_function_queues (parser);
27969 def = cp_parser_late_parse_one_default_arg (parser, field,
27970 DECL_INITIAL (field),
27971 NULL_TREE);
27972 pop_unparsed_function_queues (parser);
27974 maybe_end_member_template_processing ();
27976 DECL_INITIAL (field) = def;
27979 /* FN is a FUNCTION_DECL which may contains a parameter with an
27980 unparsed DEFAULT_ARG. Parse the default args now. This function
27981 assumes that the current scope is the scope in which the default
27982 argument should be processed. */
27984 static void
27985 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27987 bool saved_local_variables_forbidden_p;
27988 tree parm, parmdecl;
27990 /* While we're parsing the default args, we might (due to the
27991 statement expression extension) encounter more classes. We want
27992 to handle them right away, but we don't want them getting mixed
27993 up with default args that are currently in the queue. */
27994 push_unparsed_function_queues (parser);
27996 /* Local variable names (and the `this' keyword) may not appear
27997 in a default argument. */
27998 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27999 parser->local_variables_forbidden_p = true;
28001 push_defarg_context (fn);
28003 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28004 parmdecl = DECL_ARGUMENTS (fn);
28005 parm && parm != void_list_node;
28006 parm = TREE_CHAIN (parm),
28007 parmdecl = DECL_CHAIN (parmdecl))
28009 tree default_arg = TREE_PURPOSE (parm);
28010 tree parsed_arg;
28011 vec<tree, va_gc> *insts;
28012 tree copy;
28013 unsigned ix;
28015 if (!default_arg)
28016 continue;
28018 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28019 /* This can happen for a friend declaration for a function
28020 already declared with default arguments. */
28021 continue;
28023 parsed_arg
28024 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28025 default_arg,
28026 TREE_VALUE (parm));
28027 TREE_PURPOSE (parm) = parsed_arg;
28029 /* Update any instantiations we've already created. */
28030 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28031 vec_safe_iterate (insts, ix, &copy); ix++)
28032 TREE_PURPOSE (copy) = parsed_arg;
28035 pop_defarg_context ();
28037 /* Make sure no default arg is missing. */
28038 check_default_args (fn);
28040 /* Restore the state of local_variables_forbidden_p. */
28041 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28043 /* Restore the queue. */
28044 pop_unparsed_function_queues (parser);
28047 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28049 sizeof ... ( identifier )
28051 where the 'sizeof' token has already been consumed. */
28053 static tree
28054 cp_parser_sizeof_pack (cp_parser *parser)
28056 /* Consume the `...'. */
28057 cp_lexer_consume_token (parser->lexer);
28058 maybe_warn_variadic_templates ();
28060 matching_parens parens;
28061 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28062 if (paren)
28063 parens.consume_open (parser);
28064 else
28065 permerror (cp_lexer_peek_token (parser->lexer)->location,
28066 "%<sizeof...%> argument must be surrounded by parentheses");
28068 cp_token *token = cp_lexer_peek_token (parser->lexer);
28069 tree name = cp_parser_identifier (parser);
28070 if (name == error_mark_node)
28071 return error_mark_node;
28072 /* The name is not qualified. */
28073 parser->scope = NULL_TREE;
28074 parser->qualifying_scope = NULL_TREE;
28075 parser->object_scope = NULL_TREE;
28076 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28077 if (expr == error_mark_node)
28078 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28079 token->location);
28080 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28081 expr = TREE_TYPE (expr);
28082 else if (TREE_CODE (expr) == CONST_DECL)
28083 expr = DECL_INITIAL (expr);
28084 expr = make_pack_expansion (expr);
28085 PACK_EXPANSION_SIZEOF_P (expr) = true;
28087 if (paren)
28088 parens.require_close (parser);
28090 return expr;
28093 /* Parse the operand of `sizeof' (or a similar operator). Returns
28094 either a TYPE or an expression, depending on the form of the
28095 input. The KEYWORD indicates which kind of expression we have
28096 encountered. */
28098 static tree
28099 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28101 tree expr = NULL_TREE;
28102 const char *saved_message;
28103 char *tmp;
28104 bool saved_integral_constant_expression_p;
28105 bool saved_non_integral_constant_expression_p;
28107 /* If it's a `...', then we are computing the length of a parameter
28108 pack. */
28109 if (keyword == RID_SIZEOF
28110 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28111 return cp_parser_sizeof_pack (parser);
28113 /* Types cannot be defined in a `sizeof' expression. Save away the
28114 old message. */
28115 saved_message = parser->type_definition_forbidden_message;
28116 /* And create the new one. */
28117 tmp = concat ("types may not be defined in %<",
28118 IDENTIFIER_POINTER (ridpointers[keyword]),
28119 "%> expressions", NULL);
28120 parser->type_definition_forbidden_message = tmp;
28122 /* The restrictions on constant-expressions do not apply inside
28123 sizeof expressions. */
28124 saved_integral_constant_expression_p
28125 = parser->integral_constant_expression_p;
28126 saved_non_integral_constant_expression_p
28127 = parser->non_integral_constant_expression_p;
28128 parser->integral_constant_expression_p = false;
28130 /* Do not actually evaluate the expression. */
28131 ++cp_unevaluated_operand;
28132 ++c_inhibit_evaluation_warnings;
28133 /* If it's a `(', then we might be looking at the type-id
28134 construction. */
28135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28137 tree type = NULL_TREE;
28139 /* We can't be sure yet whether we're looking at a type-id or an
28140 expression. */
28141 cp_parser_parse_tentatively (parser);
28143 matching_parens parens;
28144 parens.consume_open (parser);
28146 /* Note: as a GNU Extension, compound literals are considered
28147 postfix-expressions as they are in C99, so they are valid
28148 arguments to sizeof. See comment in cp_parser_cast_expression
28149 for details. */
28150 if (cp_parser_compound_literal_p (parser))
28151 cp_parser_simulate_error (parser);
28152 else
28154 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28155 parser->in_type_id_in_expr_p = true;
28156 /* Look for the type-id. */
28157 type = cp_parser_type_id (parser);
28158 /* Look for the closing `)'. */
28159 parens.require_close (parser);
28160 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28163 /* If all went well, then we're done. */
28164 if (cp_parser_parse_definitely (parser))
28166 cp_decl_specifier_seq decl_specs;
28168 /* Build a trivial decl-specifier-seq. */
28169 clear_decl_specs (&decl_specs);
28170 decl_specs.type = type;
28172 /* Call grokdeclarator to figure out what type this is. */
28173 expr = grokdeclarator (NULL,
28174 &decl_specs,
28175 TYPENAME,
28176 /*initialized=*/0,
28177 /*attrlist=*/NULL);
28181 /* If the type-id production did not work out, then we must be
28182 looking at the unary-expression production. */
28183 if (!expr)
28184 expr = cp_parser_unary_expression (parser);
28186 /* Go back to evaluating expressions. */
28187 --cp_unevaluated_operand;
28188 --c_inhibit_evaluation_warnings;
28190 /* Free the message we created. */
28191 free (tmp);
28192 /* And restore the old one. */
28193 parser->type_definition_forbidden_message = saved_message;
28194 parser->integral_constant_expression_p
28195 = saved_integral_constant_expression_p;
28196 parser->non_integral_constant_expression_p
28197 = saved_non_integral_constant_expression_p;
28199 return expr;
28202 /* If the current declaration has no declarator, return true. */
28204 static bool
28205 cp_parser_declares_only_class_p (cp_parser *parser)
28207 /* If the next token is a `;' or a `,' then there is no
28208 declarator. */
28209 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28210 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28213 /* Update the DECL_SPECS to reflect the storage class indicated by
28214 KEYWORD. */
28216 static void
28217 cp_parser_set_storage_class (cp_parser *parser,
28218 cp_decl_specifier_seq *decl_specs,
28219 enum rid keyword,
28220 cp_token *token)
28222 cp_storage_class storage_class;
28224 if (parser->in_unbraced_linkage_specification_p)
28226 error_at (token->location, "invalid use of %qD in linkage specification",
28227 ridpointers[keyword]);
28228 return;
28230 else if (decl_specs->storage_class != sc_none)
28232 decl_specs->conflicting_specifiers_p = true;
28233 return;
28236 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28237 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28238 && decl_specs->gnu_thread_keyword_p)
28240 pedwarn (decl_specs->locations[ds_thread], 0,
28241 "%<__thread%> before %qD", ridpointers[keyword]);
28244 switch (keyword)
28246 case RID_AUTO:
28247 storage_class = sc_auto;
28248 break;
28249 case RID_REGISTER:
28250 storage_class = sc_register;
28251 break;
28252 case RID_STATIC:
28253 storage_class = sc_static;
28254 break;
28255 case RID_EXTERN:
28256 storage_class = sc_extern;
28257 break;
28258 case RID_MUTABLE:
28259 storage_class = sc_mutable;
28260 break;
28261 default:
28262 gcc_unreachable ();
28264 decl_specs->storage_class = storage_class;
28265 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28267 /* A storage class specifier cannot be applied alongside a typedef
28268 specifier. If there is a typedef specifier present then set
28269 conflicting_specifiers_p which will trigger an error later
28270 on in grokdeclarator. */
28271 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28272 decl_specs->conflicting_specifiers_p = true;
28275 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28276 is true, the type is a class or enum definition. */
28278 static void
28279 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28280 tree type_spec,
28281 cp_token *token,
28282 bool type_definition_p)
28284 decl_specs->any_specifiers_p = true;
28286 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28287 (with, for example, in "typedef int wchar_t;") we remember that
28288 this is what happened. In system headers, we ignore these
28289 declarations so that G++ can work with system headers that are not
28290 C++-safe. */
28291 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28292 && !type_definition_p
28293 && (type_spec == boolean_type_node
28294 || type_spec == char16_type_node
28295 || type_spec == char32_type_node
28296 || type_spec == wchar_type_node)
28297 && (decl_specs->type
28298 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28299 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28300 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28301 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28303 decl_specs->redefined_builtin_type = type_spec;
28304 set_and_check_decl_spec_loc (decl_specs,
28305 ds_redefined_builtin_type_spec,
28306 token);
28307 if (!decl_specs->type)
28309 decl_specs->type = type_spec;
28310 decl_specs->type_definition_p = false;
28311 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28314 else if (decl_specs->type)
28315 decl_specs->multiple_types_p = true;
28316 else
28318 decl_specs->type = type_spec;
28319 decl_specs->type_definition_p = type_definition_p;
28320 decl_specs->redefined_builtin_type = NULL_TREE;
28321 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28325 /* True iff TOKEN is the GNU keyword __thread. */
28327 static bool
28328 token_is__thread (cp_token *token)
28330 gcc_assert (token->keyword == RID_THREAD);
28331 return id_equal (token->u.value, "__thread");
28334 /* Set the location for a declarator specifier and check if it is
28335 duplicated.
28337 DECL_SPECS is the sequence of declarator specifiers onto which to
28338 set the location.
28340 DS is the single declarator specifier to set which location is to
28341 be set onto the existing sequence of declarators.
28343 LOCATION is the location for the declarator specifier to
28344 consider. */
28346 static void
28347 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28348 cp_decl_spec ds, cp_token *token)
28350 gcc_assert (ds < ds_last);
28352 if (decl_specs == NULL)
28353 return;
28355 source_location location = token->location;
28357 if (decl_specs->locations[ds] == 0)
28359 decl_specs->locations[ds] = location;
28360 if (ds == ds_thread)
28361 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28363 else
28365 if (ds == ds_long)
28367 if (decl_specs->locations[ds_long_long] != 0)
28368 error_at (location,
28369 "%<long long long%> is too long for GCC");
28370 else
28372 decl_specs->locations[ds_long_long] = location;
28373 pedwarn_cxx98 (location,
28374 OPT_Wlong_long,
28375 "ISO C++ 1998 does not support %<long long%>");
28378 else if (ds == ds_thread)
28380 bool gnu = token_is__thread (token);
28381 gcc_rich_location richloc (location);
28382 if (gnu != decl_specs->gnu_thread_keyword_p)
28384 richloc.add_range (decl_specs->locations[ds_thread], false);
28385 error_at (&richloc,
28386 "both %<__thread%> and %<thread_local%> specified");
28388 else
28390 richloc.add_fixit_remove ();
28391 error_at (&richloc, "duplicate %qD", token->u.value);
28394 else
28396 static const char *const decl_spec_names[] = {
28397 "signed",
28398 "unsigned",
28399 "short",
28400 "long",
28401 "const",
28402 "volatile",
28403 "restrict",
28404 "inline",
28405 "virtual",
28406 "explicit",
28407 "friend",
28408 "typedef",
28409 "using",
28410 "constexpr",
28411 "__complex"
28413 gcc_rich_location richloc (location);
28414 richloc.add_fixit_remove ();
28415 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28420 /* Return true iff the declarator specifier DS is present in the
28421 sequence of declarator specifiers DECL_SPECS. */
28423 bool
28424 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28425 cp_decl_spec ds)
28427 gcc_assert (ds < ds_last);
28429 if (decl_specs == NULL)
28430 return false;
28432 return decl_specs->locations[ds] != 0;
28435 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28436 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28438 static bool
28439 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28441 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28444 /* Issue an error message indicating that TOKEN_DESC was expected.
28445 If KEYWORD is true, it indicated this function is called by
28446 cp_parser_require_keword and the required token can only be
28447 a indicated keyword.
28449 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28450 within any error as the location of an "opening" token matching
28451 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28452 RT_CLOSE_PAREN). */
28454 static void
28455 cp_parser_required_error (cp_parser *parser,
28456 required_token token_desc,
28457 bool keyword,
28458 location_t matching_location)
28460 if (cp_parser_simulate_error (parser))
28461 return;
28463 const char *gmsgid = NULL;
28464 switch (token_desc)
28466 case RT_NEW:
28467 gmsgid = G_("expected %<new%>");
28468 break;
28469 case RT_DELETE:
28470 gmsgid = G_("expected %<delete%>");
28471 break;
28472 case RT_RETURN:
28473 gmsgid = G_("expected %<return%>");
28474 break;
28475 case RT_WHILE:
28476 gmsgid = G_("expected %<while%>");
28477 break;
28478 case RT_EXTERN:
28479 gmsgid = G_("expected %<extern%>");
28480 break;
28481 case RT_STATIC_ASSERT:
28482 gmsgid = G_("expected %<static_assert%>");
28483 break;
28484 case RT_DECLTYPE:
28485 gmsgid = G_("expected %<decltype%>");
28486 break;
28487 case RT_OPERATOR:
28488 gmsgid = G_("expected %<operator%>");
28489 break;
28490 case RT_CLASS:
28491 gmsgid = G_("expected %<class%>");
28492 break;
28493 case RT_TEMPLATE:
28494 gmsgid = G_("expected %<template%>");
28495 break;
28496 case RT_NAMESPACE:
28497 gmsgid = G_("expected %<namespace%>");
28498 break;
28499 case RT_USING:
28500 gmsgid = G_("expected %<using%>");
28501 break;
28502 case RT_ASM:
28503 gmsgid = G_("expected %<asm%>");
28504 break;
28505 case RT_TRY:
28506 gmsgid = G_("expected %<try%>");
28507 break;
28508 case RT_CATCH:
28509 gmsgid = G_("expected %<catch%>");
28510 break;
28511 case RT_THROW:
28512 gmsgid = G_("expected %<throw%>");
28513 break;
28514 case RT_LABEL:
28515 gmsgid = G_("expected %<__label__%>");
28516 break;
28517 case RT_AT_TRY:
28518 gmsgid = G_("expected %<@try%>");
28519 break;
28520 case RT_AT_SYNCHRONIZED:
28521 gmsgid = G_("expected %<@synchronized%>");
28522 break;
28523 case RT_AT_THROW:
28524 gmsgid = G_("expected %<@throw%>");
28525 break;
28526 case RT_TRANSACTION_ATOMIC:
28527 gmsgid = G_("expected %<__transaction_atomic%>");
28528 break;
28529 case RT_TRANSACTION_RELAXED:
28530 gmsgid = G_("expected %<__transaction_relaxed%>");
28531 break;
28532 default:
28533 break;
28536 if (!gmsgid && !keyword)
28538 switch (token_desc)
28540 case RT_SEMICOLON:
28541 gmsgid = G_("expected %<;%>");
28542 break;
28543 case RT_OPEN_PAREN:
28544 gmsgid = G_("expected %<(%>");
28545 break;
28546 case RT_CLOSE_BRACE:
28547 gmsgid = G_("expected %<}%>");
28548 break;
28549 case RT_OPEN_BRACE:
28550 gmsgid = G_("expected %<{%>");
28551 break;
28552 case RT_CLOSE_SQUARE:
28553 gmsgid = G_("expected %<]%>");
28554 break;
28555 case RT_OPEN_SQUARE:
28556 gmsgid = G_("expected %<[%>");
28557 break;
28558 case RT_COMMA:
28559 gmsgid = G_("expected %<,%>");
28560 break;
28561 case RT_SCOPE:
28562 gmsgid = G_("expected %<::%>");
28563 break;
28564 case RT_LESS:
28565 gmsgid = G_("expected %<<%>");
28566 break;
28567 case RT_GREATER:
28568 gmsgid = G_("expected %<>%>");
28569 break;
28570 case RT_EQ:
28571 gmsgid = G_("expected %<=%>");
28572 break;
28573 case RT_ELLIPSIS:
28574 gmsgid = G_("expected %<...%>");
28575 break;
28576 case RT_MULT:
28577 gmsgid = G_("expected %<*%>");
28578 break;
28579 case RT_COMPL:
28580 gmsgid = G_("expected %<~%>");
28581 break;
28582 case RT_COLON:
28583 gmsgid = G_("expected %<:%>");
28584 break;
28585 case RT_COLON_SCOPE:
28586 gmsgid = G_("expected %<:%> or %<::%>");
28587 break;
28588 case RT_CLOSE_PAREN:
28589 gmsgid = G_("expected %<)%>");
28590 break;
28591 case RT_COMMA_CLOSE_PAREN:
28592 gmsgid = G_("expected %<,%> or %<)%>");
28593 break;
28594 case RT_PRAGMA_EOL:
28595 gmsgid = G_("expected end of line");
28596 break;
28597 case RT_NAME:
28598 gmsgid = G_("expected identifier");
28599 break;
28600 case RT_SELECT:
28601 gmsgid = G_("expected selection-statement");
28602 break;
28603 case RT_ITERATION:
28604 gmsgid = G_("expected iteration-statement");
28605 break;
28606 case RT_JUMP:
28607 gmsgid = G_("expected jump-statement");
28608 break;
28609 case RT_CLASS_KEY:
28610 gmsgid = G_("expected class-key");
28611 break;
28612 case RT_CLASS_TYPENAME_TEMPLATE:
28613 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28614 break;
28615 default:
28616 gcc_unreachable ();
28620 if (gmsgid)
28621 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28625 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28626 issue an error message indicating that TOKEN_DESC was expected.
28628 Returns the token consumed, if the token had the appropriate type.
28629 Otherwise, returns NULL.
28631 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28632 within any error as the location of an "opening" token matching
28633 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28634 RT_CLOSE_PAREN). */
28636 static cp_token *
28637 cp_parser_require (cp_parser* parser,
28638 enum cpp_ttype type,
28639 required_token token_desc,
28640 location_t matching_location)
28642 if (cp_lexer_next_token_is (parser->lexer, type))
28643 return cp_lexer_consume_token (parser->lexer);
28644 else
28646 /* Output the MESSAGE -- unless we're parsing tentatively. */
28647 if (!cp_parser_simulate_error (parser))
28648 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28649 matching_location);
28650 return NULL;
28654 /* An error message is produced if the next token is not '>'.
28655 All further tokens are skipped until the desired token is
28656 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28658 static void
28659 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28661 /* Current level of '< ... >'. */
28662 unsigned level = 0;
28663 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28664 unsigned nesting_depth = 0;
28666 /* Are we ready, yet? If not, issue error message. */
28667 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28668 return;
28670 /* Skip tokens until the desired token is found. */
28671 while (true)
28673 /* Peek at the next token. */
28674 switch (cp_lexer_peek_token (parser->lexer)->type)
28676 case CPP_LESS:
28677 if (!nesting_depth)
28678 ++level;
28679 break;
28681 case CPP_RSHIFT:
28682 if (cxx_dialect == cxx98)
28683 /* C++0x views the `>>' operator as two `>' tokens, but
28684 C++98 does not. */
28685 break;
28686 else if (!nesting_depth && level-- == 0)
28688 /* We've hit a `>>' where the first `>' closes the
28689 template argument list, and the second `>' is
28690 spurious. Just consume the `>>' and stop; we've
28691 already produced at least one error. */
28692 cp_lexer_consume_token (parser->lexer);
28693 return;
28695 /* Fall through for C++0x, so we handle the second `>' in
28696 the `>>'. */
28697 gcc_fallthrough ();
28699 case CPP_GREATER:
28700 if (!nesting_depth && level-- == 0)
28702 /* We've reached the token we want, consume it and stop. */
28703 cp_lexer_consume_token (parser->lexer);
28704 return;
28706 break;
28708 case CPP_OPEN_PAREN:
28709 case CPP_OPEN_SQUARE:
28710 ++nesting_depth;
28711 break;
28713 case CPP_CLOSE_PAREN:
28714 case CPP_CLOSE_SQUARE:
28715 if (nesting_depth-- == 0)
28716 return;
28717 break;
28719 case CPP_EOF:
28720 case CPP_PRAGMA_EOL:
28721 case CPP_SEMICOLON:
28722 case CPP_OPEN_BRACE:
28723 case CPP_CLOSE_BRACE:
28724 /* The '>' was probably forgotten, don't look further. */
28725 return;
28727 default:
28728 break;
28731 /* Consume this token. */
28732 cp_lexer_consume_token (parser->lexer);
28736 /* If the next token is the indicated keyword, consume it. Otherwise,
28737 issue an error message indicating that TOKEN_DESC was expected.
28739 Returns the token consumed, if the token had the appropriate type.
28740 Otherwise, returns NULL. */
28742 static cp_token *
28743 cp_parser_require_keyword (cp_parser* parser,
28744 enum rid keyword,
28745 required_token token_desc)
28747 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28749 if (token && token->keyword != keyword)
28751 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28752 UNKNOWN_LOCATION);
28753 return NULL;
28756 return token;
28759 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28760 function-definition. */
28762 static bool
28763 cp_parser_token_starts_function_definition_p (cp_token* token)
28765 return (/* An ordinary function-body begins with an `{'. */
28766 token->type == CPP_OPEN_BRACE
28767 /* A ctor-initializer begins with a `:'. */
28768 || token->type == CPP_COLON
28769 /* A function-try-block begins with `try'. */
28770 || token->keyword == RID_TRY
28771 /* A function-transaction-block begins with `__transaction_atomic'
28772 or `__transaction_relaxed'. */
28773 || token->keyword == RID_TRANSACTION_ATOMIC
28774 || token->keyword == RID_TRANSACTION_RELAXED
28775 /* The named return value extension begins with `return'. */
28776 || token->keyword == RID_RETURN);
28779 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28780 definition. */
28782 static bool
28783 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28785 cp_token *token;
28787 token = cp_lexer_peek_token (parser->lexer);
28788 return (token->type == CPP_OPEN_BRACE
28789 || (token->type == CPP_COLON
28790 && !parser->colon_doesnt_start_class_def_p));
28793 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28794 C++0x) ending a template-argument. */
28796 static bool
28797 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28799 cp_token *token;
28801 token = cp_lexer_peek_token (parser->lexer);
28802 return (token->type == CPP_COMMA
28803 || token->type == CPP_GREATER
28804 || token->type == CPP_ELLIPSIS
28805 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28808 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28809 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28811 static bool
28812 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28813 size_t n)
28815 cp_token *token;
28817 token = cp_lexer_peek_nth_token (parser->lexer, n);
28818 if (token->type == CPP_LESS)
28819 return true;
28820 /* Check for the sequence `<::' in the original code. It would be lexed as
28821 `[:', where `[' is a digraph, and there is no whitespace before
28822 `:'. */
28823 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28825 cp_token *token2;
28826 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28827 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28828 return true;
28830 return false;
28833 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28834 or none_type otherwise. */
28836 static enum tag_types
28837 cp_parser_token_is_class_key (cp_token* token)
28839 switch (token->keyword)
28841 case RID_CLASS:
28842 return class_type;
28843 case RID_STRUCT:
28844 return record_type;
28845 case RID_UNION:
28846 return union_type;
28848 default:
28849 return none_type;
28853 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28854 or none_type otherwise or if the token is null. */
28856 static enum tag_types
28857 cp_parser_token_is_type_parameter_key (cp_token* token)
28859 if (!token)
28860 return none_type;
28862 switch (token->keyword)
28864 case RID_CLASS:
28865 return class_type;
28866 case RID_TYPENAME:
28867 return typename_type;
28869 default:
28870 return none_type;
28874 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28876 static void
28877 cp_parser_check_class_key (enum tag_types class_key, tree type)
28879 if (type == error_mark_node)
28880 return;
28881 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28883 if (permerror (input_location, "%qs tag used in naming %q#T",
28884 class_key == union_type ? "union"
28885 : class_key == record_type ? "struct" : "class",
28886 type))
28887 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28888 "%q#T was previously declared here", type);
28892 /* Issue an error message if DECL is redeclared with different
28893 access than its original declaration [class.access.spec/3].
28894 This applies to nested classes, nested class templates and
28895 enumerations [class.mem/1]. */
28897 static void
28898 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28900 if (!decl
28901 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28902 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28903 return;
28905 if ((TREE_PRIVATE (decl)
28906 != (current_access_specifier == access_private_node))
28907 || (TREE_PROTECTED (decl)
28908 != (current_access_specifier == access_protected_node)))
28909 error_at (location, "%qD redeclared with different access", decl);
28912 /* Look for the `template' keyword, as a syntactic disambiguator.
28913 Return TRUE iff it is present, in which case it will be
28914 consumed. */
28916 static bool
28917 cp_parser_optional_template_keyword (cp_parser *parser)
28919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28921 /* In C++98 the `template' keyword can only be used within templates;
28922 outside templates the parser can always figure out what is a
28923 template and what is not. In C++11, per the resolution of DR 468,
28924 `template' is allowed in cases where it is not strictly necessary. */
28925 if (!processing_template_decl
28926 && pedantic && cxx_dialect == cxx98)
28928 cp_token *token = cp_lexer_peek_token (parser->lexer);
28929 pedwarn (token->location, OPT_Wpedantic,
28930 "in C++98 %<template%> (as a disambiguator) is only "
28931 "allowed within templates");
28932 /* If this part of the token stream is rescanned, the same
28933 error message would be generated. So, we purge the token
28934 from the stream. */
28935 cp_lexer_purge_token (parser->lexer);
28936 return false;
28938 else
28940 /* Consume the `template' keyword. */
28941 cp_lexer_consume_token (parser->lexer);
28942 return true;
28945 return false;
28948 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28949 set PARSER->SCOPE, and perform other related actions. */
28951 static void
28952 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28954 struct tree_check *check_value;
28956 /* Get the stored value. */
28957 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28958 /* Set the scope from the stored value. */
28959 parser->scope = saved_checks_value (check_value);
28960 parser->qualifying_scope = check_value->qualifying_scope;
28961 parser->object_scope = NULL_TREE;
28964 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28965 encounter the end of a block before what we were looking for. */
28967 static bool
28968 cp_parser_cache_group (cp_parser *parser,
28969 enum cpp_ttype end,
28970 unsigned depth)
28972 while (true)
28974 cp_token *token = cp_lexer_peek_token (parser->lexer);
28976 /* Abort a parenthesized expression if we encounter a semicolon. */
28977 if ((end == CPP_CLOSE_PAREN || depth == 0)
28978 && token->type == CPP_SEMICOLON)
28979 return true;
28980 /* If we've reached the end of the file, stop. */
28981 if (token->type == CPP_EOF
28982 || (end != CPP_PRAGMA_EOL
28983 && token->type == CPP_PRAGMA_EOL))
28984 return true;
28985 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28986 /* We've hit the end of an enclosing block, so there's been some
28987 kind of syntax error. */
28988 return true;
28990 /* Consume the token. */
28991 cp_lexer_consume_token (parser->lexer);
28992 /* See if it starts a new group. */
28993 if (token->type == CPP_OPEN_BRACE)
28995 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28996 /* In theory this should probably check end == '}', but
28997 cp_parser_save_member_function_body needs it to exit
28998 after either '}' or ')' when called with ')'. */
28999 if (depth == 0)
29000 return false;
29002 else if (token->type == CPP_OPEN_PAREN)
29004 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29005 if (depth == 0 && end == CPP_CLOSE_PAREN)
29006 return false;
29008 else if (token->type == CPP_PRAGMA)
29009 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29010 else if (token->type == end)
29011 return false;
29015 /* Like above, for caching a default argument or NSDMI. Both of these are
29016 terminated by a non-nested comma, but it can be unclear whether or not a
29017 comma is nested in a template argument list unless we do more parsing.
29018 In order to handle this ambiguity, when we encounter a ',' after a '<'
29019 we try to parse what follows as a parameter-declaration-list (in the
29020 case of a default argument) or a member-declarator (in the case of an
29021 NSDMI). If that succeeds, then we stop caching. */
29023 static tree
29024 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29026 unsigned depth = 0;
29027 int maybe_template_id = 0;
29028 cp_token *first_token;
29029 cp_token *token;
29030 tree default_argument;
29032 /* Add tokens until we have processed the entire default
29033 argument. We add the range [first_token, token). */
29034 first_token = cp_lexer_peek_token (parser->lexer);
29035 if (first_token->type == CPP_OPEN_BRACE)
29037 /* For list-initialization, this is straightforward. */
29038 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29039 token = cp_lexer_peek_token (parser->lexer);
29041 else while (true)
29043 bool done = false;
29045 /* Peek at the next token. */
29046 token = cp_lexer_peek_token (parser->lexer);
29047 /* What we do depends on what token we have. */
29048 switch (token->type)
29050 /* In valid code, a default argument must be
29051 immediately followed by a `,' `)', or `...'. */
29052 case CPP_COMMA:
29053 if (depth == 0 && maybe_template_id)
29055 /* If we've seen a '<', we might be in a
29056 template-argument-list. Until Core issue 325 is
29057 resolved, we don't know how this situation ought
29058 to be handled, so try to DTRT. We check whether
29059 what comes after the comma is a valid parameter
29060 declaration list. If it is, then the comma ends
29061 the default argument; otherwise the default
29062 argument continues. */
29063 bool error = false;
29064 cp_token *peek;
29066 /* Set ITALP so cp_parser_parameter_declaration_list
29067 doesn't decide to commit to this parse. */
29068 bool saved_italp = parser->in_template_argument_list_p;
29069 parser->in_template_argument_list_p = true;
29071 cp_parser_parse_tentatively (parser);
29073 if (nsdmi)
29075 /* Parse declarators until we reach a non-comma or
29076 somthing that cannot be an initializer.
29077 Just checking whether we're looking at a single
29078 declarator is insufficient. Consider:
29079 int var = tuple<T,U>::x;
29080 The template parameter 'U' looks exactly like a
29081 declarator. */
29084 int ctor_dtor_or_conv_p;
29085 cp_lexer_consume_token (parser->lexer);
29086 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29087 &ctor_dtor_or_conv_p,
29088 /*parenthesized_p=*/NULL,
29089 /*member_p=*/true,
29090 /*friend_p=*/false);
29091 peek = cp_lexer_peek_token (parser->lexer);
29092 if (cp_parser_error_occurred (parser))
29093 break;
29095 while (peek->type == CPP_COMMA);
29096 /* If we met an '=' or ';' then the original comma
29097 was the end of the NSDMI. Otherwise assume
29098 we're still in the NSDMI. */
29099 error = (peek->type != CPP_EQ
29100 && peek->type != CPP_SEMICOLON);
29102 else
29104 cp_lexer_consume_token (parser->lexer);
29105 begin_scope (sk_function_parms, NULL_TREE);
29106 if (cp_parser_parameter_declaration_list (parser)
29107 == error_mark_node)
29108 error = true;
29109 pop_bindings_and_leave_scope ();
29111 if (!cp_parser_error_occurred (parser) && !error)
29112 done = true;
29113 cp_parser_abort_tentative_parse (parser);
29115 parser->in_template_argument_list_p = saved_italp;
29116 break;
29118 /* FALLTHRU */
29119 case CPP_CLOSE_PAREN:
29120 case CPP_ELLIPSIS:
29121 /* If we run into a non-nested `;', `}', or `]',
29122 then the code is invalid -- but the default
29123 argument is certainly over. */
29124 case CPP_SEMICOLON:
29125 case CPP_CLOSE_BRACE:
29126 case CPP_CLOSE_SQUARE:
29127 if (depth == 0
29128 /* Handle correctly int n = sizeof ... ( p ); */
29129 && token->type != CPP_ELLIPSIS)
29130 done = true;
29131 /* Update DEPTH, if necessary. */
29132 else if (token->type == CPP_CLOSE_PAREN
29133 || token->type == CPP_CLOSE_BRACE
29134 || token->type == CPP_CLOSE_SQUARE)
29135 --depth;
29136 break;
29138 case CPP_OPEN_PAREN:
29139 case CPP_OPEN_SQUARE:
29140 case CPP_OPEN_BRACE:
29141 ++depth;
29142 break;
29144 case CPP_LESS:
29145 if (depth == 0)
29146 /* This might be the comparison operator, or it might
29147 start a template argument list. */
29148 ++maybe_template_id;
29149 break;
29151 case CPP_RSHIFT:
29152 if (cxx_dialect == cxx98)
29153 break;
29154 /* Fall through for C++0x, which treats the `>>'
29155 operator like two `>' tokens in certain
29156 cases. */
29157 gcc_fallthrough ();
29159 case CPP_GREATER:
29160 if (depth == 0)
29162 /* This might be an operator, or it might close a
29163 template argument list. But if a previous '<'
29164 started a template argument list, this will have
29165 closed it, so we can't be in one anymore. */
29166 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29167 if (maybe_template_id < 0)
29168 maybe_template_id = 0;
29170 break;
29172 /* If we run out of tokens, issue an error message. */
29173 case CPP_EOF:
29174 case CPP_PRAGMA_EOL:
29175 error_at (token->location, "file ends in default argument");
29176 return error_mark_node;
29178 case CPP_NAME:
29179 case CPP_SCOPE:
29180 /* In these cases, we should look for template-ids.
29181 For example, if the default argument is
29182 `X<int, double>()', we need to do name lookup to
29183 figure out whether or not `X' is a template; if
29184 so, the `,' does not end the default argument.
29186 That is not yet done. */
29187 break;
29189 default:
29190 break;
29193 /* If we've reached the end, stop. */
29194 if (done)
29195 break;
29197 /* Add the token to the token block. */
29198 token = cp_lexer_consume_token (parser->lexer);
29201 /* Create a DEFAULT_ARG to represent the unparsed default
29202 argument. */
29203 default_argument = make_node (DEFAULT_ARG);
29204 DEFARG_TOKENS (default_argument)
29205 = cp_token_cache_new (first_token, token);
29206 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29208 return default_argument;
29211 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29213 location_t
29214 defarg_location (tree default_argument)
29216 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29217 location_t start = tokens->first->location;
29218 location_t end = tokens->last->location;
29219 return make_location (start, start, end);
29222 /* Begin parsing tentatively. We always save tokens while parsing
29223 tentatively so that if the tentative parsing fails we can restore the
29224 tokens. */
29226 static void
29227 cp_parser_parse_tentatively (cp_parser* parser)
29229 /* Enter a new parsing context. */
29230 parser->context = cp_parser_context_new (parser->context);
29231 /* Begin saving tokens. */
29232 cp_lexer_save_tokens (parser->lexer);
29233 /* In order to avoid repetitive access control error messages,
29234 access checks are queued up until we are no longer parsing
29235 tentatively. */
29236 push_deferring_access_checks (dk_deferred);
29239 /* Commit to the currently active tentative parse. */
29241 static void
29242 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29244 cp_parser_context *context;
29245 cp_lexer *lexer;
29247 /* Mark all of the levels as committed. */
29248 lexer = parser->lexer;
29249 for (context = parser->context; context->next; context = context->next)
29251 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29252 break;
29253 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29254 while (!cp_lexer_saving_tokens (lexer))
29255 lexer = lexer->next;
29256 cp_lexer_commit_tokens (lexer);
29260 /* Commit to the topmost currently active tentative parse.
29262 Note that this function shouldn't be called when there are
29263 irreversible side-effects while in a tentative state. For
29264 example, we shouldn't create a permanent entry in the symbol
29265 table, or issue an error message that might not apply if the
29266 tentative parse is aborted. */
29268 static void
29269 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29271 cp_parser_context *context = parser->context;
29272 cp_lexer *lexer = parser->lexer;
29274 if (context)
29276 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29277 return;
29278 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29280 while (!cp_lexer_saving_tokens (lexer))
29281 lexer = lexer->next;
29282 cp_lexer_commit_tokens (lexer);
29286 /* Abort the currently active tentative parse. All consumed tokens
29287 will be rolled back, and no diagnostics will be issued. */
29289 static void
29290 cp_parser_abort_tentative_parse (cp_parser* parser)
29292 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29293 || errorcount > 0);
29294 cp_parser_simulate_error (parser);
29295 /* Now, pretend that we want to see if the construct was
29296 successfully parsed. */
29297 cp_parser_parse_definitely (parser);
29300 /* Stop parsing tentatively. If a parse error has occurred, restore the
29301 token stream. Otherwise, commit to the tokens we have consumed.
29302 Returns true if no error occurred; false otherwise. */
29304 static bool
29305 cp_parser_parse_definitely (cp_parser* parser)
29307 bool error_occurred;
29308 cp_parser_context *context;
29310 /* Remember whether or not an error occurred, since we are about to
29311 destroy that information. */
29312 error_occurred = cp_parser_error_occurred (parser);
29313 /* Remove the topmost context from the stack. */
29314 context = parser->context;
29315 parser->context = context->next;
29316 /* If no parse errors occurred, commit to the tentative parse. */
29317 if (!error_occurred)
29319 /* Commit to the tokens read tentatively, unless that was
29320 already done. */
29321 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29322 cp_lexer_commit_tokens (parser->lexer);
29324 pop_to_parent_deferring_access_checks ();
29326 /* Otherwise, if errors occurred, roll back our state so that things
29327 are just as they were before we began the tentative parse. */
29328 else
29330 cp_lexer_rollback_tokens (parser->lexer);
29331 pop_deferring_access_checks ();
29333 /* Add the context to the front of the free list. */
29334 context->next = cp_parser_context_free_list;
29335 cp_parser_context_free_list = context;
29337 return !error_occurred;
29340 /* Returns true if we are parsing tentatively and are not committed to
29341 this tentative parse. */
29343 static bool
29344 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29346 return (cp_parser_parsing_tentatively (parser)
29347 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29350 /* Returns nonzero iff an error has occurred during the most recent
29351 tentative parse. */
29353 static bool
29354 cp_parser_error_occurred (cp_parser* parser)
29356 return (cp_parser_parsing_tentatively (parser)
29357 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29360 /* Returns nonzero if GNU extensions are allowed. */
29362 static bool
29363 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29365 return parser->allow_gnu_extensions_p;
29368 /* Objective-C++ Productions */
29371 /* Parse an Objective-C expression, which feeds into a primary-expression
29372 above.
29374 objc-expression:
29375 objc-message-expression
29376 objc-string-literal
29377 objc-encode-expression
29378 objc-protocol-expression
29379 objc-selector-expression
29381 Returns a tree representation of the expression. */
29383 static cp_expr
29384 cp_parser_objc_expression (cp_parser* parser)
29386 /* Try to figure out what kind of declaration is present. */
29387 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29389 switch (kwd->type)
29391 case CPP_OPEN_SQUARE:
29392 return cp_parser_objc_message_expression (parser);
29394 case CPP_OBJC_STRING:
29395 kwd = cp_lexer_consume_token (parser->lexer);
29396 return objc_build_string_object (kwd->u.value);
29398 case CPP_KEYWORD:
29399 switch (kwd->keyword)
29401 case RID_AT_ENCODE:
29402 return cp_parser_objc_encode_expression (parser);
29404 case RID_AT_PROTOCOL:
29405 return cp_parser_objc_protocol_expression (parser);
29407 case RID_AT_SELECTOR:
29408 return cp_parser_objc_selector_expression (parser);
29410 default:
29411 break;
29413 /* FALLTHRU */
29414 default:
29415 error_at (kwd->location,
29416 "misplaced %<@%D%> Objective-C++ construct",
29417 kwd->u.value);
29418 cp_parser_skip_to_end_of_block_or_statement (parser);
29421 return error_mark_node;
29424 /* Parse an Objective-C message expression.
29426 objc-message-expression:
29427 [ objc-message-receiver objc-message-args ]
29429 Returns a representation of an Objective-C message. */
29431 static tree
29432 cp_parser_objc_message_expression (cp_parser* parser)
29434 tree receiver, messageargs;
29436 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29437 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29438 receiver = cp_parser_objc_message_receiver (parser);
29439 messageargs = cp_parser_objc_message_args (parser);
29440 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29441 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29443 tree result = objc_build_message_expr (receiver, messageargs);
29445 /* Construct a location e.g.
29446 [self func1:5]
29447 ^~~~~~~~~~~~~~
29448 ranging from the '[' to the ']', with the caret at the start. */
29449 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29450 protected_set_expr_location (result, combined_loc);
29452 return result;
29455 /* Parse an objc-message-receiver.
29457 objc-message-receiver:
29458 expression
29459 simple-type-specifier
29461 Returns a representation of the type or expression. */
29463 static tree
29464 cp_parser_objc_message_receiver (cp_parser* parser)
29466 tree rcv;
29468 /* An Objective-C message receiver may be either (1) a type
29469 or (2) an expression. */
29470 cp_parser_parse_tentatively (parser);
29471 rcv = cp_parser_expression (parser);
29473 /* If that worked out, fine. */
29474 if (cp_parser_parse_definitely (parser))
29475 return rcv;
29477 cp_parser_parse_tentatively (parser);
29478 rcv = cp_parser_simple_type_specifier (parser,
29479 /*decl_specs=*/NULL,
29480 CP_PARSER_FLAGS_NONE);
29482 if (cp_parser_parse_definitely (parser))
29483 return objc_get_class_reference (rcv);
29485 cp_parser_error (parser, "objective-c++ message receiver expected");
29486 return error_mark_node;
29489 /* Parse the arguments and selectors comprising an Objective-C message.
29491 objc-message-args:
29492 objc-selector
29493 objc-selector-args
29494 objc-selector-args , objc-comma-args
29496 objc-selector-args:
29497 objc-selector [opt] : assignment-expression
29498 objc-selector-args objc-selector [opt] : assignment-expression
29500 objc-comma-args:
29501 assignment-expression
29502 objc-comma-args , assignment-expression
29504 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29505 selector arguments and TREE_VALUE containing a list of comma
29506 arguments. */
29508 static tree
29509 cp_parser_objc_message_args (cp_parser* parser)
29511 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29512 bool maybe_unary_selector_p = true;
29513 cp_token *token = cp_lexer_peek_token (parser->lexer);
29515 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29517 tree selector = NULL_TREE, arg;
29519 if (token->type != CPP_COLON)
29520 selector = cp_parser_objc_selector (parser);
29522 /* Detect if we have a unary selector. */
29523 if (maybe_unary_selector_p
29524 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29525 return build_tree_list (selector, NULL_TREE);
29527 maybe_unary_selector_p = false;
29528 cp_parser_require (parser, CPP_COLON, RT_COLON);
29529 arg = cp_parser_assignment_expression (parser);
29531 sel_args
29532 = chainon (sel_args,
29533 build_tree_list (selector, arg));
29535 token = cp_lexer_peek_token (parser->lexer);
29538 /* Handle non-selector arguments, if any. */
29539 while (token->type == CPP_COMMA)
29541 tree arg;
29543 cp_lexer_consume_token (parser->lexer);
29544 arg = cp_parser_assignment_expression (parser);
29546 addl_args
29547 = chainon (addl_args,
29548 build_tree_list (NULL_TREE, arg));
29550 token = cp_lexer_peek_token (parser->lexer);
29553 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29555 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29556 return build_tree_list (error_mark_node, error_mark_node);
29559 return build_tree_list (sel_args, addl_args);
29562 /* Parse an Objective-C encode expression.
29564 objc-encode-expression:
29565 @encode objc-typename
29567 Returns an encoded representation of the type argument. */
29569 static cp_expr
29570 cp_parser_objc_encode_expression (cp_parser* parser)
29572 tree type;
29573 cp_token *token;
29574 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29576 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29577 matching_parens parens;
29578 parens.require_open (parser);
29579 token = cp_lexer_peek_token (parser->lexer);
29580 type = complete_type (cp_parser_type_id (parser));
29581 parens.require_close (parser);
29583 if (!type)
29585 error_at (token->location,
29586 "%<@encode%> must specify a type as an argument");
29587 return error_mark_node;
29590 /* This happens if we find @encode(T) (where T is a template
29591 typename or something dependent on a template typename) when
29592 parsing a template. In that case, we can't compile it
29593 immediately, but we rather create an AT_ENCODE_EXPR which will
29594 need to be instantiated when the template is used.
29596 if (dependent_type_p (type))
29598 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29599 TREE_READONLY (value) = 1;
29600 return value;
29604 /* Build a location of the form:
29605 @encode(int)
29606 ^~~~~~~~~~~~
29607 with caret==start at the @ token, finishing at the close paren. */
29608 location_t combined_loc
29609 = make_location (start_loc, start_loc,
29610 cp_lexer_previous_token (parser->lexer)->location);
29612 return cp_expr (objc_build_encode_expr (type), combined_loc);
29615 /* Parse an Objective-C @defs expression. */
29617 static tree
29618 cp_parser_objc_defs_expression (cp_parser *parser)
29620 tree name;
29622 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29623 matching_parens parens;
29624 parens.require_open (parser);
29625 name = cp_parser_identifier (parser);
29626 parens.require_close (parser);
29628 return objc_get_class_ivars (name);
29631 /* Parse an Objective-C protocol expression.
29633 objc-protocol-expression:
29634 @protocol ( identifier )
29636 Returns a representation of the protocol expression. */
29638 static tree
29639 cp_parser_objc_protocol_expression (cp_parser* parser)
29641 tree proto;
29642 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29644 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29645 matching_parens parens;
29646 parens.require_open (parser);
29647 proto = cp_parser_identifier (parser);
29648 parens.require_close (parser);
29650 /* Build a location of the form:
29651 @protocol(prot)
29652 ^~~~~~~~~~~~~~~
29653 with caret==start at the @ token, finishing at the close paren. */
29654 location_t combined_loc
29655 = make_location (start_loc, start_loc,
29656 cp_lexer_previous_token (parser->lexer)->location);
29657 tree result = objc_build_protocol_expr (proto);
29658 protected_set_expr_location (result, combined_loc);
29659 return result;
29662 /* Parse an Objective-C selector expression.
29664 objc-selector-expression:
29665 @selector ( objc-method-signature )
29667 objc-method-signature:
29668 objc-selector
29669 objc-selector-seq
29671 objc-selector-seq:
29672 objc-selector :
29673 objc-selector-seq objc-selector :
29675 Returns a representation of the method selector. */
29677 static tree
29678 cp_parser_objc_selector_expression (cp_parser* parser)
29680 tree sel_seq = NULL_TREE;
29681 bool maybe_unary_selector_p = true;
29682 cp_token *token;
29683 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29685 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29686 matching_parens parens;
29687 parens.require_open (parser);
29688 token = cp_lexer_peek_token (parser->lexer);
29690 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29691 || token->type == CPP_SCOPE)
29693 tree selector = NULL_TREE;
29695 if (token->type != CPP_COLON
29696 || token->type == CPP_SCOPE)
29697 selector = cp_parser_objc_selector (parser);
29699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29700 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29702 /* Detect if we have a unary selector. */
29703 if (maybe_unary_selector_p)
29705 sel_seq = selector;
29706 goto finish_selector;
29708 else
29710 cp_parser_error (parser, "expected %<:%>");
29713 maybe_unary_selector_p = false;
29714 token = cp_lexer_consume_token (parser->lexer);
29716 if (token->type == CPP_SCOPE)
29718 sel_seq
29719 = chainon (sel_seq,
29720 build_tree_list (selector, NULL_TREE));
29721 sel_seq
29722 = chainon (sel_seq,
29723 build_tree_list (NULL_TREE, NULL_TREE));
29725 else
29726 sel_seq
29727 = chainon (sel_seq,
29728 build_tree_list (selector, NULL_TREE));
29730 token = cp_lexer_peek_token (parser->lexer);
29733 finish_selector:
29734 parens.require_close (parser);
29737 /* Build a location of the form:
29738 @selector(func)
29739 ^~~~~~~~~~~~~~~
29740 with caret==start at the @ token, finishing at the close paren. */
29741 location_t combined_loc
29742 = make_location (loc, loc,
29743 cp_lexer_previous_token (parser->lexer)->location);
29744 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29745 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29746 protected_set_expr_location (result, combined_loc);
29747 return result;
29750 /* Parse a list of identifiers.
29752 objc-identifier-list:
29753 identifier
29754 objc-identifier-list , identifier
29756 Returns a TREE_LIST of identifier nodes. */
29758 static tree
29759 cp_parser_objc_identifier_list (cp_parser* parser)
29761 tree identifier;
29762 tree list;
29763 cp_token *sep;
29765 identifier = cp_parser_identifier (parser);
29766 if (identifier == error_mark_node)
29767 return error_mark_node;
29769 list = build_tree_list (NULL_TREE, identifier);
29770 sep = cp_lexer_peek_token (parser->lexer);
29772 while (sep->type == CPP_COMMA)
29774 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29775 identifier = cp_parser_identifier (parser);
29776 if (identifier == error_mark_node)
29777 return list;
29779 list = chainon (list, build_tree_list (NULL_TREE,
29780 identifier));
29781 sep = cp_lexer_peek_token (parser->lexer);
29784 return list;
29787 /* Parse an Objective-C alias declaration.
29789 objc-alias-declaration:
29790 @compatibility_alias identifier identifier ;
29792 This function registers the alias mapping with the Objective-C front end.
29793 It returns nothing. */
29795 static void
29796 cp_parser_objc_alias_declaration (cp_parser* parser)
29798 tree alias, orig;
29800 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29801 alias = cp_parser_identifier (parser);
29802 orig = cp_parser_identifier (parser);
29803 objc_declare_alias (alias, orig);
29804 cp_parser_consume_semicolon_at_end_of_statement (parser);
29807 /* Parse an Objective-C class forward-declaration.
29809 objc-class-declaration:
29810 @class objc-identifier-list ;
29812 The function registers the forward declarations with the Objective-C
29813 front end. It returns nothing. */
29815 static void
29816 cp_parser_objc_class_declaration (cp_parser* parser)
29818 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29819 while (true)
29821 tree id;
29823 id = cp_parser_identifier (parser);
29824 if (id == error_mark_node)
29825 break;
29827 objc_declare_class (id);
29829 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29830 cp_lexer_consume_token (parser->lexer);
29831 else
29832 break;
29834 cp_parser_consume_semicolon_at_end_of_statement (parser);
29837 /* Parse a list of Objective-C protocol references.
29839 objc-protocol-refs-opt:
29840 objc-protocol-refs [opt]
29842 objc-protocol-refs:
29843 < objc-identifier-list >
29845 Returns a TREE_LIST of identifiers, if any. */
29847 static tree
29848 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29850 tree protorefs = NULL_TREE;
29852 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29854 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29855 protorefs = cp_parser_objc_identifier_list (parser);
29856 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29859 return protorefs;
29862 /* Parse a Objective-C visibility specification. */
29864 static void
29865 cp_parser_objc_visibility_spec (cp_parser* parser)
29867 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29869 switch (vis->keyword)
29871 case RID_AT_PRIVATE:
29872 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29873 break;
29874 case RID_AT_PROTECTED:
29875 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29876 break;
29877 case RID_AT_PUBLIC:
29878 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29879 break;
29880 case RID_AT_PACKAGE:
29881 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29882 break;
29883 default:
29884 return;
29887 /* Eat '@private'/'@protected'/'@public'. */
29888 cp_lexer_consume_token (parser->lexer);
29891 /* Parse an Objective-C method type. Return 'true' if it is a class
29892 (+) method, and 'false' if it is an instance (-) method. */
29894 static inline bool
29895 cp_parser_objc_method_type (cp_parser* parser)
29897 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29898 return true;
29899 else
29900 return false;
29903 /* Parse an Objective-C protocol qualifier. */
29905 static tree
29906 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29908 tree quals = NULL_TREE, node;
29909 cp_token *token = cp_lexer_peek_token (parser->lexer);
29911 node = token->u.value;
29913 while (node && identifier_p (node)
29914 && (node == ridpointers [(int) RID_IN]
29915 || node == ridpointers [(int) RID_OUT]
29916 || node == ridpointers [(int) RID_INOUT]
29917 || node == ridpointers [(int) RID_BYCOPY]
29918 || node == ridpointers [(int) RID_BYREF]
29919 || node == ridpointers [(int) RID_ONEWAY]))
29921 quals = tree_cons (NULL_TREE, node, quals);
29922 cp_lexer_consume_token (parser->lexer);
29923 token = cp_lexer_peek_token (parser->lexer);
29924 node = token->u.value;
29927 return quals;
29930 /* Parse an Objective-C typename. */
29932 static tree
29933 cp_parser_objc_typename (cp_parser* parser)
29935 tree type_name = NULL_TREE;
29937 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29939 tree proto_quals, cp_type = NULL_TREE;
29941 matching_parens parens;
29942 parens.consume_open (parser); /* Eat '('. */
29943 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29945 /* An ObjC type name may consist of just protocol qualifiers, in which
29946 case the type shall default to 'id'. */
29947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29949 cp_type = cp_parser_type_id (parser);
29951 /* If the type could not be parsed, an error has already
29952 been produced. For error recovery, behave as if it had
29953 not been specified, which will use the default type
29954 'id'. */
29955 if (cp_type == error_mark_node)
29957 cp_type = NULL_TREE;
29958 /* We need to skip to the closing parenthesis as
29959 cp_parser_type_id() does not seem to do it for
29960 us. */
29961 cp_parser_skip_to_closing_parenthesis (parser,
29962 /*recovering=*/true,
29963 /*or_comma=*/false,
29964 /*consume_paren=*/false);
29968 parens.require_close (parser);
29969 type_name = build_tree_list (proto_quals, cp_type);
29972 return type_name;
29975 /* Check to see if TYPE refers to an Objective-C selector name. */
29977 static bool
29978 cp_parser_objc_selector_p (enum cpp_ttype type)
29980 return (type == CPP_NAME || type == CPP_KEYWORD
29981 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29982 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29983 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29984 || type == CPP_XOR || type == CPP_XOR_EQ);
29987 /* Parse an Objective-C selector. */
29989 static tree
29990 cp_parser_objc_selector (cp_parser* parser)
29992 cp_token *token = cp_lexer_consume_token (parser->lexer);
29994 if (!cp_parser_objc_selector_p (token->type))
29996 error_at (token->location, "invalid Objective-C++ selector name");
29997 return error_mark_node;
30000 /* C++ operator names are allowed to appear in ObjC selectors. */
30001 switch (token->type)
30003 case CPP_AND_AND: return get_identifier ("and");
30004 case CPP_AND_EQ: return get_identifier ("and_eq");
30005 case CPP_AND: return get_identifier ("bitand");
30006 case CPP_OR: return get_identifier ("bitor");
30007 case CPP_COMPL: return get_identifier ("compl");
30008 case CPP_NOT: return get_identifier ("not");
30009 case CPP_NOT_EQ: return get_identifier ("not_eq");
30010 case CPP_OR_OR: return get_identifier ("or");
30011 case CPP_OR_EQ: return get_identifier ("or_eq");
30012 case CPP_XOR: return get_identifier ("xor");
30013 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30014 default: return token->u.value;
30018 /* Parse an Objective-C params list. */
30020 static tree
30021 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30023 tree params = NULL_TREE;
30024 bool maybe_unary_selector_p = true;
30025 cp_token *token = cp_lexer_peek_token (parser->lexer);
30027 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30029 tree selector = NULL_TREE, type_name, identifier;
30030 tree parm_attr = NULL_TREE;
30032 if (token->keyword == RID_ATTRIBUTE)
30033 break;
30035 if (token->type != CPP_COLON)
30036 selector = cp_parser_objc_selector (parser);
30038 /* Detect if we have a unary selector. */
30039 if (maybe_unary_selector_p
30040 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30042 params = selector; /* Might be followed by attributes. */
30043 break;
30046 maybe_unary_selector_p = false;
30047 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30049 /* Something went quite wrong. There should be a colon
30050 here, but there is not. Stop parsing parameters. */
30051 break;
30053 type_name = cp_parser_objc_typename (parser);
30054 /* New ObjC allows attributes on parameters too. */
30055 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30056 parm_attr = cp_parser_attributes_opt (parser);
30057 identifier = cp_parser_identifier (parser);
30059 params
30060 = chainon (params,
30061 objc_build_keyword_decl (selector,
30062 type_name,
30063 identifier,
30064 parm_attr));
30066 token = cp_lexer_peek_token (parser->lexer);
30069 if (params == NULL_TREE)
30071 cp_parser_error (parser, "objective-c++ method declaration is expected");
30072 return error_mark_node;
30075 /* We allow tail attributes for the method. */
30076 if (token->keyword == RID_ATTRIBUTE)
30078 *attributes = cp_parser_attributes_opt (parser);
30079 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30080 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30081 return params;
30082 cp_parser_error (parser,
30083 "method attributes must be specified at the end");
30084 return error_mark_node;
30087 if (params == NULL_TREE)
30089 cp_parser_error (parser, "objective-c++ method declaration is expected");
30090 return error_mark_node;
30092 return params;
30095 /* Parse the non-keyword Objective-C params. */
30097 static tree
30098 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30099 tree* attributes)
30101 tree params = make_node (TREE_LIST);
30102 cp_token *token = cp_lexer_peek_token (parser->lexer);
30103 *ellipsisp = false; /* Initially, assume no ellipsis. */
30105 while (token->type == CPP_COMMA)
30107 cp_parameter_declarator *parmdecl;
30108 tree parm;
30110 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30111 token = cp_lexer_peek_token (parser->lexer);
30113 if (token->type == CPP_ELLIPSIS)
30115 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30116 *ellipsisp = true;
30117 token = cp_lexer_peek_token (parser->lexer);
30118 break;
30121 /* TODO: parse attributes for tail parameters. */
30122 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30123 parm = grokdeclarator (parmdecl->declarator,
30124 &parmdecl->decl_specifiers,
30125 PARM, /*initialized=*/0,
30126 /*attrlist=*/NULL);
30128 chainon (params, build_tree_list (NULL_TREE, parm));
30129 token = cp_lexer_peek_token (parser->lexer);
30132 /* We allow tail attributes for the method. */
30133 if (token->keyword == RID_ATTRIBUTE)
30135 if (*attributes == NULL_TREE)
30137 *attributes = cp_parser_attributes_opt (parser);
30138 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30139 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30140 return params;
30142 else
30143 /* We have an error, but parse the attributes, so that we can
30144 carry on. */
30145 *attributes = cp_parser_attributes_opt (parser);
30147 cp_parser_error (parser,
30148 "method attributes must be specified at the end");
30149 return error_mark_node;
30152 return params;
30155 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30157 static void
30158 cp_parser_objc_interstitial_code (cp_parser* parser)
30160 cp_token *token = cp_lexer_peek_token (parser->lexer);
30162 /* If the next token is `extern' and the following token is a string
30163 literal, then we have a linkage specification. */
30164 if (token->keyword == RID_EXTERN
30165 && cp_parser_is_pure_string_literal
30166 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30167 cp_parser_linkage_specification (parser);
30168 /* Handle #pragma, if any. */
30169 else if (token->type == CPP_PRAGMA)
30170 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30171 /* Allow stray semicolons. */
30172 else if (token->type == CPP_SEMICOLON)
30173 cp_lexer_consume_token (parser->lexer);
30174 /* Mark methods as optional or required, when building protocols. */
30175 else if (token->keyword == RID_AT_OPTIONAL)
30177 cp_lexer_consume_token (parser->lexer);
30178 objc_set_method_opt (true);
30180 else if (token->keyword == RID_AT_REQUIRED)
30182 cp_lexer_consume_token (parser->lexer);
30183 objc_set_method_opt (false);
30185 else if (token->keyword == RID_NAMESPACE)
30186 cp_parser_namespace_definition (parser);
30187 /* Other stray characters must generate errors. */
30188 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30190 cp_lexer_consume_token (parser->lexer);
30191 error ("stray %qs between Objective-C++ methods",
30192 token->type == CPP_OPEN_BRACE ? "{" : "}");
30194 /* Finally, try to parse a block-declaration, or a function-definition. */
30195 else
30196 cp_parser_block_declaration (parser, /*statement_p=*/false);
30199 /* Parse a method signature. */
30201 static tree
30202 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30204 tree rettype, kwdparms, optparms;
30205 bool ellipsis = false;
30206 bool is_class_method;
30208 is_class_method = cp_parser_objc_method_type (parser);
30209 rettype = cp_parser_objc_typename (parser);
30210 *attributes = NULL_TREE;
30211 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30212 if (kwdparms == error_mark_node)
30213 return error_mark_node;
30214 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30215 if (optparms == error_mark_node)
30216 return error_mark_node;
30218 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30221 static bool
30222 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30224 tree tattr;
30225 cp_lexer_save_tokens (parser->lexer);
30226 tattr = cp_parser_attributes_opt (parser);
30227 gcc_assert (tattr) ;
30229 /* If the attributes are followed by a method introducer, this is not allowed.
30230 Dump the attributes and flag the situation. */
30231 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30232 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30233 return true;
30235 /* Otherwise, the attributes introduce some interstitial code, possibly so
30236 rewind to allow that check. */
30237 cp_lexer_rollback_tokens (parser->lexer);
30238 return false;
30241 /* Parse an Objective-C method prototype list. */
30243 static void
30244 cp_parser_objc_method_prototype_list (cp_parser* parser)
30246 cp_token *token = cp_lexer_peek_token (parser->lexer);
30248 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30250 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30252 tree attributes, sig;
30253 bool is_class_method;
30254 if (token->type == CPP_PLUS)
30255 is_class_method = true;
30256 else
30257 is_class_method = false;
30258 sig = cp_parser_objc_method_signature (parser, &attributes);
30259 if (sig == error_mark_node)
30261 cp_parser_skip_to_end_of_block_or_statement (parser);
30262 token = cp_lexer_peek_token (parser->lexer);
30263 continue;
30265 objc_add_method_declaration (is_class_method, sig, attributes);
30266 cp_parser_consume_semicolon_at_end_of_statement (parser);
30268 else if (token->keyword == RID_AT_PROPERTY)
30269 cp_parser_objc_at_property_declaration (parser);
30270 else if (token->keyword == RID_ATTRIBUTE
30271 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30272 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30273 OPT_Wattributes,
30274 "prefix attributes are ignored for methods");
30275 else
30276 /* Allow for interspersed non-ObjC++ code. */
30277 cp_parser_objc_interstitial_code (parser);
30279 token = cp_lexer_peek_token (parser->lexer);
30282 if (token->type != CPP_EOF)
30283 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30284 else
30285 cp_parser_error (parser, "expected %<@end%>");
30287 objc_finish_interface ();
30290 /* Parse an Objective-C method definition list. */
30292 static void
30293 cp_parser_objc_method_definition_list (cp_parser* parser)
30295 cp_token *token = cp_lexer_peek_token (parser->lexer);
30297 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30299 tree meth;
30301 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30303 cp_token *ptk;
30304 tree sig, attribute;
30305 bool is_class_method;
30306 if (token->type == CPP_PLUS)
30307 is_class_method = true;
30308 else
30309 is_class_method = false;
30310 push_deferring_access_checks (dk_deferred);
30311 sig = cp_parser_objc_method_signature (parser, &attribute);
30312 if (sig == error_mark_node)
30314 cp_parser_skip_to_end_of_block_or_statement (parser);
30315 token = cp_lexer_peek_token (parser->lexer);
30316 continue;
30318 objc_start_method_definition (is_class_method, sig, attribute,
30319 NULL_TREE);
30321 /* For historical reasons, we accept an optional semicolon. */
30322 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30323 cp_lexer_consume_token (parser->lexer);
30325 ptk = cp_lexer_peek_token (parser->lexer);
30326 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30327 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30329 perform_deferred_access_checks (tf_warning_or_error);
30330 stop_deferring_access_checks ();
30331 meth = cp_parser_function_definition_after_declarator (parser,
30332 false);
30333 pop_deferring_access_checks ();
30334 objc_finish_method_definition (meth);
30337 /* The following case will be removed once @synthesize is
30338 completely implemented. */
30339 else if (token->keyword == RID_AT_PROPERTY)
30340 cp_parser_objc_at_property_declaration (parser);
30341 else if (token->keyword == RID_AT_SYNTHESIZE)
30342 cp_parser_objc_at_synthesize_declaration (parser);
30343 else if (token->keyword == RID_AT_DYNAMIC)
30344 cp_parser_objc_at_dynamic_declaration (parser);
30345 else if (token->keyword == RID_ATTRIBUTE
30346 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30347 warning_at (token->location, OPT_Wattributes,
30348 "prefix attributes are ignored for methods");
30349 else
30350 /* Allow for interspersed non-ObjC++ code. */
30351 cp_parser_objc_interstitial_code (parser);
30353 token = cp_lexer_peek_token (parser->lexer);
30356 if (token->type != CPP_EOF)
30357 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30358 else
30359 cp_parser_error (parser, "expected %<@end%>");
30361 objc_finish_implementation ();
30364 /* Parse Objective-C ivars. */
30366 static void
30367 cp_parser_objc_class_ivars (cp_parser* parser)
30369 cp_token *token = cp_lexer_peek_token (parser->lexer);
30371 if (token->type != CPP_OPEN_BRACE)
30372 return; /* No ivars specified. */
30374 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30375 token = cp_lexer_peek_token (parser->lexer);
30377 while (token->type != CPP_CLOSE_BRACE
30378 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30380 cp_decl_specifier_seq declspecs;
30381 int decl_class_or_enum_p;
30382 tree prefix_attributes;
30384 cp_parser_objc_visibility_spec (parser);
30386 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30387 break;
30389 cp_parser_decl_specifier_seq (parser,
30390 CP_PARSER_FLAGS_OPTIONAL,
30391 &declspecs,
30392 &decl_class_or_enum_p);
30394 /* auto, register, static, extern, mutable. */
30395 if (declspecs.storage_class != sc_none)
30397 cp_parser_error (parser, "invalid type for instance variable");
30398 declspecs.storage_class = sc_none;
30401 /* thread_local. */
30402 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30404 cp_parser_error (parser, "invalid type for instance variable");
30405 declspecs.locations[ds_thread] = 0;
30408 /* typedef. */
30409 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30411 cp_parser_error (parser, "invalid type for instance variable");
30412 declspecs.locations[ds_typedef] = 0;
30415 prefix_attributes = declspecs.attributes;
30416 declspecs.attributes = NULL_TREE;
30418 /* Keep going until we hit the `;' at the end of the
30419 declaration. */
30420 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30422 tree width = NULL_TREE, attributes, first_attribute, decl;
30423 cp_declarator *declarator = NULL;
30424 int ctor_dtor_or_conv_p;
30426 /* Check for a (possibly unnamed) bitfield declaration. */
30427 token = cp_lexer_peek_token (parser->lexer);
30428 if (token->type == CPP_COLON)
30429 goto eat_colon;
30431 if (token->type == CPP_NAME
30432 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30433 == CPP_COLON))
30435 /* Get the name of the bitfield. */
30436 declarator = make_id_declarator (NULL_TREE,
30437 cp_parser_identifier (parser),
30438 sfk_none);
30440 eat_colon:
30441 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30442 /* Get the width of the bitfield. */
30443 width
30444 = cp_parser_constant_expression (parser);
30446 else
30448 /* Parse the declarator. */
30449 declarator
30450 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30451 &ctor_dtor_or_conv_p,
30452 /*parenthesized_p=*/NULL,
30453 /*member_p=*/false,
30454 /*friend_p=*/false);
30457 /* Look for attributes that apply to the ivar. */
30458 attributes = cp_parser_attributes_opt (parser);
30459 /* Remember which attributes are prefix attributes and
30460 which are not. */
30461 first_attribute = attributes;
30462 /* Combine the attributes. */
30463 attributes = attr_chainon (prefix_attributes, attributes);
30465 if (width)
30466 /* Create the bitfield declaration. */
30467 decl = grokbitfield (declarator, &declspecs,
30468 width, NULL_TREE, attributes);
30469 else
30470 decl = grokfield (declarator, &declspecs,
30471 NULL_TREE, /*init_const_expr_p=*/false,
30472 NULL_TREE, attributes);
30474 /* Add the instance variable. */
30475 if (decl != error_mark_node && decl != NULL_TREE)
30476 objc_add_instance_variable (decl);
30478 /* Reset PREFIX_ATTRIBUTES. */
30479 if (attributes != error_mark_node)
30481 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30482 attributes = TREE_CHAIN (attributes);
30483 if (attributes)
30484 TREE_CHAIN (attributes) = NULL_TREE;
30487 token = cp_lexer_peek_token (parser->lexer);
30489 if (token->type == CPP_COMMA)
30491 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30492 continue;
30494 break;
30497 cp_parser_consume_semicolon_at_end_of_statement (parser);
30498 token = cp_lexer_peek_token (parser->lexer);
30501 if (token->keyword == RID_AT_END)
30502 cp_parser_error (parser, "expected %<}%>");
30504 /* Do not consume the RID_AT_END, so it will be read again as terminating
30505 the @interface of @implementation. */
30506 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30507 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30509 /* For historical reasons, we accept an optional semicolon. */
30510 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30511 cp_lexer_consume_token (parser->lexer);
30514 /* Parse an Objective-C protocol declaration. */
30516 static void
30517 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30519 tree proto, protorefs;
30520 cp_token *tok;
30522 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30525 tok = cp_lexer_peek_token (parser->lexer);
30526 error_at (tok->location, "identifier expected after %<@protocol%>");
30527 cp_parser_consume_semicolon_at_end_of_statement (parser);
30528 return;
30531 /* See if we have a forward declaration or a definition. */
30532 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30534 /* Try a forward declaration first. */
30535 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30537 while (true)
30539 tree id;
30541 id = cp_parser_identifier (parser);
30542 if (id == error_mark_node)
30543 break;
30545 objc_declare_protocol (id, attributes);
30547 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30548 cp_lexer_consume_token (parser->lexer);
30549 else
30550 break;
30552 cp_parser_consume_semicolon_at_end_of_statement (parser);
30555 /* Ok, we got a full-fledged definition (or at least should). */
30556 else
30558 proto = cp_parser_identifier (parser);
30559 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30560 objc_start_protocol (proto, protorefs, attributes);
30561 cp_parser_objc_method_prototype_list (parser);
30565 /* Parse an Objective-C superclass or category. */
30567 static void
30568 cp_parser_objc_superclass_or_category (cp_parser *parser,
30569 bool iface_p,
30570 tree *super,
30571 tree *categ, bool *is_class_extension)
30573 cp_token *next = cp_lexer_peek_token (parser->lexer);
30575 *super = *categ = NULL_TREE;
30576 *is_class_extension = false;
30577 if (next->type == CPP_COLON)
30579 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30580 *super = cp_parser_identifier (parser);
30582 else if (next->type == CPP_OPEN_PAREN)
30584 matching_parens parens;
30585 parens.consume_open (parser); /* Eat '('. */
30587 /* If there is no category name, and this is an @interface, we
30588 have a class extension. */
30589 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30591 *categ = NULL_TREE;
30592 *is_class_extension = true;
30594 else
30595 *categ = cp_parser_identifier (parser);
30597 parens.require_close (parser);
30601 /* Parse an Objective-C class interface. */
30603 static void
30604 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30606 tree name, super, categ, protos;
30607 bool is_class_extension;
30609 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30610 name = cp_parser_identifier (parser);
30611 if (name == error_mark_node)
30613 /* It's hard to recover because even if valid @interface stuff
30614 is to follow, we can't compile it (or validate it) if we
30615 don't even know which class it refers to. Let's assume this
30616 was a stray '@interface' token in the stream and skip it.
30618 return;
30620 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30621 &is_class_extension);
30622 protos = cp_parser_objc_protocol_refs_opt (parser);
30624 /* We have either a class or a category on our hands. */
30625 if (categ || is_class_extension)
30626 objc_start_category_interface (name, categ, protos, attributes);
30627 else
30629 objc_start_class_interface (name, super, protos, attributes);
30630 /* Handle instance variable declarations, if any. */
30631 cp_parser_objc_class_ivars (parser);
30632 objc_continue_interface ();
30635 cp_parser_objc_method_prototype_list (parser);
30638 /* Parse an Objective-C class implementation. */
30640 static void
30641 cp_parser_objc_class_implementation (cp_parser* parser)
30643 tree name, super, categ;
30644 bool is_class_extension;
30646 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30647 name = cp_parser_identifier (parser);
30648 if (name == error_mark_node)
30650 /* It's hard to recover because even if valid @implementation
30651 stuff is to follow, we can't compile it (or validate it) if
30652 we don't even know which class it refers to. Let's assume
30653 this was a stray '@implementation' token in the stream and
30654 skip it.
30656 return;
30658 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30659 &is_class_extension);
30661 /* We have either a class or a category on our hands. */
30662 if (categ)
30663 objc_start_category_implementation (name, categ);
30664 else
30666 objc_start_class_implementation (name, super);
30667 /* Handle instance variable declarations, if any. */
30668 cp_parser_objc_class_ivars (parser);
30669 objc_continue_implementation ();
30672 cp_parser_objc_method_definition_list (parser);
30675 /* Consume the @end token and finish off the implementation. */
30677 static void
30678 cp_parser_objc_end_implementation (cp_parser* parser)
30680 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30681 objc_finish_implementation ();
30684 /* Parse an Objective-C declaration. */
30686 static void
30687 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30689 /* Try to figure out what kind of declaration is present. */
30690 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30692 if (attributes)
30693 switch (kwd->keyword)
30695 case RID_AT_ALIAS:
30696 case RID_AT_CLASS:
30697 case RID_AT_END:
30698 error_at (kwd->location, "attributes may not be specified before"
30699 " the %<@%D%> Objective-C++ keyword",
30700 kwd->u.value);
30701 attributes = NULL;
30702 break;
30703 case RID_AT_IMPLEMENTATION:
30704 warning_at (kwd->location, OPT_Wattributes,
30705 "prefix attributes are ignored before %<@%D%>",
30706 kwd->u.value);
30707 attributes = NULL;
30708 default:
30709 break;
30712 switch (kwd->keyword)
30714 case RID_AT_ALIAS:
30715 cp_parser_objc_alias_declaration (parser);
30716 break;
30717 case RID_AT_CLASS:
30718 cp_parser_objc_class_declaration (parser);
30719 break;
30720 case RID_AT_PROTOCOL:
30721 cp_parser_objc_protocol_declaration (parser, attributes);
30722 break;
30723 case RID_AT_INTERFACE:
30724 cp_parser_objc_class_interface (parser, attributes);
30725 break;
30726 case RID_AT_IMPLEMENTATION:
30727 cp_parser_objc_class_implementation (parser);
30728 break;
30729 case RID_AT_END:
30730 cp_parser_objc_end_implementation (parser);
30731 break;
30732 default:
30733 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30734 kwd->u.value);
30735 cp_parser_skip_to_end_of_block_or_statement (parser);
30739 /* Parse an Objective-C try-catch-finally statement.
30741 objc-try-catch-finally-stmt:
30742 @try compound-statement objc-catch-clause-seq [opt]
30743 objc-finally-clause [opt]
30745 objc-catch-clause-seq:
30746 objc-catch-clause objc-catch-clause-seq [opt]
30748 objc-catch-clause:
30749 @catch ( objc-exception-declaration ) compound-statement
30751 objc-finally-clause:
30752 @finally compound-statement
30754 objc-exception-declaration:
30755 parameter-declaration
30756 '...'
30758 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30760 Returns NULL_TREE.
30762 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30763 for C. Keep them in sync. */
30765 static tree
30766 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30768 location_t location;
30769 tree stmt;
30771 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30772 location = cp_lexer_peek_token (parser->lexer)->location;
30773 objc_maybe_warn_exceptions (location);
30774 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30775 node, lest it get absorbed into the surrounding block. */
30776 stmt = push_stmt_list ();
30777 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30778 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30780 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30782 cp_parameter_declarator *parm;
30783 tree parameter_declaration = error_mark_node;
30784 bool seen_open_paren = false;
30785 matching_parens parens;
30787 cp_lexer_consume_token (parser->lexer);
30788 if (parens.require_open (parser))
30789 seen_open_paren = true;
30790 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30792 /* We have "@catch (...)" (where the '...' are literally
30793 what is in the code). Skip the '...'.
30794 parameter_declaration is set to NULL_TREE, and
30795 objc_being_catch_clauses() knows that that means
30796 '...'. */
30797 cp_lexer_consume_token (parser->lexer);
30798 parameter_declaration = NULL_TREE;
30800 else
30802 /* We have "@catch (NSException *exception)" or something
30803 like that. Parse the parameter declaration. */
30804 parm = cp_parser_parameter_declaration (parser, false, NULL);
30805 if (parm == NULL)
30806 parameter_declaration = error_mark_node;
30807 else
30808 parameter_declaration = grokdeclarator (parm->declarator,
30809 &parm->decl_specifiers,
30810 PARM, /*initialized=*/0,
30811 /*attrlist=*/NULL);
30813 if (seen_open_paren)
30814 parens.require_close (parser);
30815 else
30817 /* If there was no open parenthesis, we are recovering from
30818 an error, and we are trying to figure out what mistake
30819 the user has made. */
30821 /* If there is an immediate closing parenthesis, the user
30822 probably forgot the opening one (ie, they typed "@catch
30823 NSException *e)". Parse the closing parenthesis and keep
30824 going. */
30825 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30826 cp_lexer_consume_token (parser->lexer);
30828 /* If these is no immediate closing parenthesis, the user
30829 probably doesn't know that parenthesis are required at
30830 all (ie, they typed "@catch NSException *e"). So, just
30831 forget about the closing parenthesis and keep going. */
30833 objc_begin_catch_clause (parameter_declaration);
30834 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30835 objc_finish_catch_clause ();
30837 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30839 cp_lexer_consume_token (parser->lexer);
30840 location = cp_lexer_peek_token (parser->lexer)->location;
30841 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30842 node, lest it get absorbed into the surrounding block. */
30843 stmt = push_stmt_list ();
30844 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30845 objc_build_finally_clause (location, pop_stmt_list (stmt));
30848 return objc_finish_try_stmt ();
30851 /* Parse an Objective-C synchronized statement.
30853 objc-synchronized-stmt:
30854 @synchronized ( expression ) compound-statement
30856 Returns NULL_TREE. */
30858 static tree
30859 cp_parser_objc_synchronized_statement (cp_parser *parser)
30861 location_t location;
30862 tree lock, stmt;
30864 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30866 location = cp_lexer_peek_token (parser->lexer)->location;
30867 objc_maybe_warn_exceptions (location);
30868 matching_parens parens;
30869 parens.require_open (parser);
30870 lock = cp_parser_expression (parser);
30871 parens.require_close (parser);
30873 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30874 node, lest it get absorbed into the surrounding block. */
30875 stmt = push_stmt_list ();
30876 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30878 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30881 /* Parse an Objective-C throw statement.
30883 objc-throw-stmt:
30884 @throw assignment-expression [opt] ;
30886 Returns a constructed '@throw' statement. */
30888 static tree
30889 cp_parser_objc_throw_statement (cp_parser *parser)
30891 tree expr = NULL_TREE;
30892 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30894 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30897 expr = cp_parser_expression (parser);
30899 cp_parser_consume_semicolon_at_end_of_statement (parser);
30901 return objc_build_throw_stmt (loc, expr);
30904 /* Parse an Objective-C statement. */
30906 static tree
30907 cp_parser_objc_statement (cp_parser * parser)
30909 /* Try to figure out what kind of declaration is present. */
30910 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30912 switch (kwd->keyword)
30914 case RID_AT_TRY:
30915 return cp_parser_objc_try_catch_finally_statement (parser);
30916 case RID_AT_SYNCHRONIZED:
30917 return cp_parser_objc_synchronized_statement (parser);
30918 case RID_AT_THROW:
30919 return cp_parser_objc_throw_statement (parser);
30920 default:
30921 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30922 kwd->u.value);
30923 cp_parser_skip_to_end_of_block_or_statement (parser);
30926 return error_mark_node;
30929 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30930 look ahead to see if an objc keyword follows the attributes. This
30931 is to detect the use of prefix attributes on ObjC @interface and
30932 @protocol. */
30934 static bool
30935 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30937 cp_lexer_save_tokens (parser->lexer);
30938 *attrib = cp_parser_attributes_opt (parser);
30939 gcc_assert (*attrib);
30940 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30942 cp_lexer_commit_tokens (parser->lexer);
30943 return true;
30945 cp_lexer_rollback_tokens (parser->lexer);
30946 return false;
30949 /* This routine is a minimal replacement for
30950 c_parser_struct_declaration () used when parsing the list of
30951 types/names or ObjC++ properties. For example, when parsing the
30952 code
30954 @property (readonly) int a, b, c;
30956 this function is responsible for parsing "int a, int b, int c" and
30957 returning the declarations as CHAIN of DECLs.
30959 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30960 similar parsing. */
30961 static tree
30962 cp_parser_objc_struct_declaration (cp_parser *parser)
30964 tree decls = NULL_TREE;
30965 cp_decl_specifier_seq declspecs;
30966 int decl_class_or_enum_p;
30967 tree prefix_attributes;
30969 cp_parser_decl_specifier_seq (parser,
30970 CP_PARSER_FLAGS_NONE,
30971 &declspecs,
30972 &decl_class_or_enum_p);
30974 if (declspecs.type == error_mark_node)
30975 return error_mark_node;
30977 /* auto, register, static, extern, mutable. */
30978 if (declspecs.storage_class != sc_none)
30980 cp_parser_error (parser, "invalid type for property");
30981 declspecs.storage_class = sc_none;
30984 /* thread_local. */
30985 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30987 cp_parser_error (parser, "invalid type for property");
30988 declspecs.locations[ds_thread] = 0;
30991 /* typedef. */
30992 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30994 cp_parser_error (parser, "invalid type for property");
30995 declspecs.locations[ds_typedef] = 0;
30998 prefix_attributes = declspecs.attributes;
30999 declspecs.attributes = NULL_TREE;
31001 /* Keep going until we hit the `;' at the end of the declaration. */
31002 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31004 tree attributes, first_attribute, decl;
31005 cp_declarator *declarator;
31006 cp_token *token;
31008 /* Parse the declarator. */
31009 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31010 NULL, NULL, false, false);
31012 /* Look for attributes that apply to the ivar. */
31013 attributes = cp_parser_attributes_opt (parser);
31014 /* Remember which attributes are prefix attributes and
31015 which are not. */
31016 first_attribute = attributes;
31017 /* Combine the attributes. */
31018 attributes = attr_chainon (prefix_attributes, attributes);
31020 decl = grokfield (declarator, &declspecs,
31021 NULL_TREE, /*init_const_expr_p=*/false,
31022 NULL_TREE, attributes);
31024 if (decl == error_mark_node || decl == NULL_TREE)
31025 return error_mark_node;
31027 /* Reset PREFIX_ATTRIBUTES. */
31028 if (attributes != error_mark_node)
31030 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31031 attributes = TREE_CHAIN (attributes);
31032 if (attributes)
31033 TREE_CHAIN (attributes) = NULL_TREE;
31036 DECL_CHAIN (decl) = decls;
31037 decls = decl;
31039 token = cp_lexer_peek_token (parser->lexer);
31040 if (token->type == CPP_COMMA)
31042 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31043 continue;
31045 else
31046 break;
31048 return decls;
31051 /* Parse an Objective-C @property declaration. The syntax is:
31053 objc-property-declaration:
31054 '@property' objc-property-attributes[opt] struct-declaration ;
31056 objc-property-attributes:
31057 '(' objc-property-attribute-list ')'
31059 objc-property-attribute-list:
31060 objc-property-attribute
31061 objc-property-attribute-list, objc-property-attribute
31063 objc-property-attribute
31064 'getter' = identifier
31065 'setter' = identifier
31066 'readonly'
31067 'readwrite'
31068 'assign'
31069 'retain'
31070 'copy'
31071 'nonatomic'
31073 For example:
31074 @property NSString *name;
31075 @property (readonly) id object;
31076 @property (retain, nonatomic, getter=getTheName) id name;
31077 @property int a, b, c;
31079 PS: This function is identical to
31080 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31081 static void
31082 cp_parser_objc_at_property_declaration (cp_parser *parser)
31084 /* The following variables hold the attributes of the properties as
31085 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31086 seen. When we see an attribute, we set them to 'true' (if they
31087 are boolean properties) or to the identifier (if they have an
31088 argument, ie, for getter and setter). Note that here we only
31089 parse the list of attributes, check the syntax and accumulate the
31090 attributes that we find. objc_add_property_declaration() will
31091 then process the information. */
31092 bool property_assign = false;
31093 bool property_copy = false;
31094 tree property_getter_ident = NULL_TREE;
31095 bool property_nonatomic = false;
31096 bool property_readonly = false;
31097 bool property_readwrite = false;
31098 bool property_retain = false;
31099 tree property_setter_ident = NULL_TREE;
31101 /* 'properties' is the list of properties that we read. Usually a
31102 single one, but maybe more (eg, in "@property int a, b, c;" there
31103 are three). */
31104 tree properties;
31105 location_t loc;
31107 loc = cp_lexer_peek_token (parser->lexer)->location;
31109 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31111 /* Parse the optional attribute list... */
31112 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31114 /* Eat the '('. */
31115 matching_parens parens;
31116 parens.consume_open (parser);
31118 while (true)
31120 bool syntax_error = false;
31121 cp_token *token = cp_lexer_peek_token (parser->lexer);
31122 enum rid keyword;
31124 if (token->type != CPP_NAME)
31126 cp_parser_error (parser, "expected identifier");
31127 break;
31129 keyword = C_RID_CODE (token->u.value);
31130 cp_lexer_consume_token (parser->lexer);
31131 switch (keyword)
31133 case RID_ASSIGN: property_assign = true; break;
31134 case RID_COPY: property_copy = true; break;
31135 case RID_NONATOMIC: property_nonatomic = true; break;
31136 case RID_READONLY: property_readonly = true; break;
31137 case RID_READWRITE: property_readwrite = true; break;
31138 case RID_RETAIN: property_retain = true; break;
31140 case RID_GETTER:
31141 case RID_SETTER:
31142 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31144 if (keyword == RID_GETTER)
31145 cp_parser_error (parser,
31146 "missing %<=%> (after %<getter%> attribute)");
31147 else
31148 cp_parser_error (parser,
31149 "missing %<=%> (after %<setter%> attribute)");
31150 syntax_error = true;
31151 break;
31153 cp_lexer_consume_token (parser->lexer); /* eat the = */
31154 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31156 cp_parser_error (parser, "expected identifier");
31157 syntax_error = true;
31158 break;
31160 if (keyword == RID_SETTER)
31162 if (property_setter_ident != NULL_TREE)
31164 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31165 cp_lexer_consume_token (parser->lexer);
31167 else
31168 property_setter_ident = cp_parser_objc_selector (parser);
31169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31170 cp_parser_error (parser, "setter name must terminate with %<:%>");
31171 else
31172 cp_lexer_consume_token (parser->lexer);
31174 else
31176 if (property_getter_ident != NULL_TREE)
31178 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31179 cp_lexer_consume_token (parser->lexer);
31181 else
31182 property_getter_ident = cp_parser_objc_selector (parser);
31184 break;
31185 default:
31186 cp_parser_error (parser, "unknown property attribute");
31187 syntax_error = true;
31188 break;
31191 if (syntax_error)
31192 break;
31194 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31195 cp_lexer_consume_token (parser->lexer);
31196 else
31197 break;
31200 /* FIXME: "@property (setter, assign);" will generate a spurious
31201 "error: expected ‘)’ before ‘,’ token". This is because
31202 cp_parser_require, unlike the C counterpart, will produce an
31203 error even if we are in error recovery. */
31204 if (!parens.require_close (parser))
31206 cp_parser_skip_to_closing_parenthesis (parser,
31207 /*recovering=*/true,
31208 /*or_comma=*/false,
31209 /*consume_paren=*/true);
31213 /* ... and the property declaration(s). */
31214 properties = cp_parser_objc_struct_declaration (parser);
31216 if (properties == error_mark_node)
31218 cp_parser_skip_to_end_of_statement (parser);
31219 /* If the next token is now a `;', consume it. */
31220 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31221 cp_lexer_consume_token (parser->lexer);
31222 return;
31225 if (properties == NULL_TREE)
31226 cp_parser_error (parser, "expected identifier");
31227 else
31229 /* Comma-separated properties are chained together in
31230 reverse order; add them one by one. */
31231 properties = nreverse (properties);
31233 for (; properties; properties = TREE_CHAIN (properties))
31234 objc_add_property_declaration (loc, copy_node (properties),
31235 property_readonly, property_readwrite,
31236 property_assign, property_retain,
31237 property_copy, property_nonatomic,
31238 property_getter_ident, property_setter_ident);
31241 cp_parser_consume_semicolon_at_end_of_statement (parser);
31244 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31246 objc-synthesize-declaration:
31247 @synthesize objc-synthesize-identifier-list ;
31249 objc-synthesize-identifier-list:
31250 objc-synthesize-identifier
31251 objc-synthesize-identifier-list, objc-synthesize-identifier
31253 objc-synthesize-identifier
31254 identifier
31255 identifier = identifier
31257 For example:
31258 @synthesize MyProperty;
31259 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31261 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31262 for C. Keep them in sync.
31264 static void
31265 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31267 tree list = NULL_TREE;
31268 location_t loc;
31269 loc = cp_lexer_peek_token (parser->lexer)->location;
31271 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31272 while (true)
31274 tree property, ivar;
31275 property = cp_parser_identifier (parser);
31276 if (property == error_mark_node)
31278 cp_parser_consume_semicolon_at_end_of_statement (parser);
31279 return;
31281 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31283 cp_lexer_consume_token (parser->lexer);
31284 ivar = cp_parser_identifier (parser);
31285 if (ivar == error_mark_node)
31287 cp_parser_consume_semicolon_at_end_of_statement (parser);
31288 return;
31291 else
31292 ivar = NULL_TREE;
31293 list = chainon (list, build_tree_list (ivar, property));
31294 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31295 cp_lexer_consume_token (parser->lexer);
31296 else
31297 break;
31299 cp_parser_consume_semicolon_at_end_of_statement (parser);
31300 objc_add_synthesize_declaration (loc, list);
31303 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31305 objc-dynamic-declaration:
31306 @dynamic identifier-list ;
31308 For example:
31309 @dynamic MyProperty;
31310 @dynamic MyProperty, AnotherProperty;
31312 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31313 for C. Keep them in sync.
31315 static void
31316 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31318 tree list = NULL_TREE;
31319 location_t loc;
31320 loc = cp_lexer_peek_token (parser->lexer)->location;
31322 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31323 while (true)
31325 tree property;
31326 property = cp_parser_identifier (parser);
31327 if (property == error_mark_node)
31329 cp_parser_consume_semicolon_at_end_of_statement (parser);
31330 return;
31332 list = chainon (list, build_tree_list (NULL, property));
31333 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31334 cp_lexer_consume_token (parser->lexer);
31335 else
31336 break;
31338 cp_parser_consume_semicolon_at_end_of_statement (parser);
31339 objc_add_dynamic_declaration (loc, list);
31343 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31345 /* Returns name of the next clause.
31346 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31347 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31348 returned and the token is consumed. */
31350 static pragma_omp_clause
31351 cp_parser_omp_clause_name (cp_parser *parser)
31353 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31355 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31356 result = PRAGMA_OACC_CLAUSE_AUTO;
31357 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31358 result = PRAGMA_OMP_CLAUSE_IF;
31359 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31360 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31361 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31362 result = PRAGMA_OACC_CLAUSE_DELETE;
31363 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31364 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31365 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31366 result = PRAGMA_OMP_CLAUSE_FOR;
31367 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31369 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31370 const char *p = IDENTIFIER_POINTER (id);
31372 switch (p[0])
31374 case 'a':
31375 if (!strcmp ("aligned", p))
31376 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31377 else if (!strcmp ("async", p))
31378 result = PRAGMA_OACC_CLAUSE_ASYNC;
31379 break;
31380 case 'c':
31381 if (!strcmp ("collapse", p))
31382 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31383 else if (!strcmp ("copy", p))
31384 result = PRAGMA_OACC_CLAUSE_COPY;
31385 else if (!strcmp ("copyin", p))
31386 result = PRAGMA_OMP_CLAUSE_COPYIN;
31387 else if (!strcmp ("copyout", p))
31388 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31389 else if (!strcmp ("copyprivate", p))
31390 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31391 else if (!strcmp ("create", p))
31392 result = PRAGMA_OACC_CLAUSE_CREATE;
31393 break;
31394 case 'd':
31395 if (!strcmp ("defaultmap", p))
31396 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31397 else if (!strcmp ("depend", p))
31398 result = PRAGMA_OMP_CLAUSE_DEPEND;
31399 else if (!strcmp ("device", p))
31400 result = PRAGMA_OMP_CLAUSE_DEVICE;
31401 else if (!strcmp ("deviceptr", p))
31402 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31403 else if (!strcmp ("device_resident", p))
31404 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31405 else if (!strcmp ("dist_schedule", p))
31406 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31407 break;
31408 case 'f':
31409 if (!strcmp ("final", p))
31410 result = PRAGMA_OMP_CLAUSE_FINAL;
31411 else if (!strcmp ("finalize", p))
31412 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31413 else if (!strcmp ("firstprivate", p))
31414 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31415 else if (!strcmp ("from", p))
31416 result = PRAGMA_OMP_CLAUSE_FROM;
31417 break;
31418 case 'g':
31419 if (!strcmp ("gang", p))
31420 result = PRAGMA_OACC_CLAUSE_GANG;
31421 else if (!strcmp ("grainsize", p))
31422 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31423 break;
31424 case 'h':
31425 if (!strcmp ("hint", p))
31426 result = PRAGMA_OMP_CLAUSE_HINT;
31427 else if (!strcmp ("host", p))
31428 result = PRAGMA_OACC_CLAUSE_HOST;
31429 break;
31430 case 'i':
31431 if (!strcmp ("if_present", p))
31432 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31433 else if (!strcmp ("inbranch", p))
31434 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31435 else if (!strcmp ("independent", p))
31436 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31437 else if (!strcmp ("is_device_ptr", p))
31438 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31439 break;
31440 case 'l':
31441 if (!strcmp ("lastprivate", p))
31442 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31443 else if (!strcmp ("linear", p))
31444 result = PRAGMA_OMP_CLAUSE_LINEAR;
31445 else if (!strcmp ("link", p))
31446 result = PRAGMA_OMP_CLAUSE_LINK;
31447 break;
31448 case 'm':
31449 if (!strcmp ("map", p))
31450 result = PRAGMA_OMP_CLAUSE_MAP;
31451 else if (!strcmp ("mergeable", p))
31452 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31453 break;
31454 case 'n':
31455 if (!strcmp ("nogroup", p))
31456 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31457 else if (!strcmp ("notinbranch", p))
31458 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31459 else if (!strcmp ("nowait", p))
31460 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31461 else if (!strcmp ("num_gangs", p))
31462 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31463 else if (!strcmp ("num_tasks", p))
31464 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31465 else if (!strcmp ("num_teams", p))
31466 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31467 else if (!strcmp ("num_threads", p))
31468 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31469 else if (!strcmp ("num_workers", p))
31470 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31471 break;
31472 case 'o':
31473 if (!strcmp ("ordered", p))
31474 result = PRAGMA_OMP_CLAUSE_ORDERED;
31475 break;
31476 case 'p':
31477 if (!strcmp ("parallel", p))
31478 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31479 else if (!strcmp ("present", p))
31480 result = PRAGMA_OACC_CLAUSE_PRESENT;
31481 else if (!strcmp ("present_or_copy", p)
31482 || !strcmp ("pcopy", p))
31483 result = PRAGMA_OACC_CLAUSE_COPY;
31484 else if (!strcmp ("present_or_copyin", p)
31485 || !strcmp ("pcopyin", p))
31486 result = PRAGMA_OACC_CLAUSE_COPYIN;
31487 else if (!strcmp ("present_or_copyout", p)
31488 || !strcmp ("pcopyout", p))
31489 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31490 else if (!strcmp ("present_or_create", p)
31491 || !strcmp ("pcreate", p))
31492 result = PRAGMA_OACC_CLAUSE_CREATE;
31493 else if (!strcmp ("priority", p))
31494 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31495 else if (!strcmp ("proc_bind", p))
31496 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31497 break;
31498 case 'r':
31499 if (!strcmp ("reduction", p))
31500 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31501 break;
31502 case 's':
31503 if (!strcmp ("safelen", p))
31504 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31505 else if (!strcmp ("schedule", p))
31506 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31507 else if (!strcmp ("sections", p))
31508 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31509 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31510 result = PRAGMA_OACC_CLAUSE_HOST;
31511 else if (!strcmp ("seq", p))
31512 result = PRAGMA_OACC_CLAUSE_SEQ;
31513 else if (!strcmp ("shared", p))
31514 result = PRAGMA_OMP_CLAUSE_SHARED;
31515 else if (!strcmp ("simd", p))
31516 result = PRAGMA_OMP_CLAUSE_SIMD;
31517 else if (!strcmp ("simdlen", p))
31518 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31519 break;
31520 case 't':
31521 if (!strcmp ("taskgroup", p))
31522 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31523 else if (!strcmp ("thread_limit", p))
31524 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31525 else if (!strcmp ("threads", p))
31526 result = PRAGMA_OMP_CLAUSE_THREADS;
31527 else if (!strcmp ("tile", p))
31528 result = PRAGMA_OACC_CLAUSE_TILE;
31529 else if (!strcmp ("to", p))
31530 result = PRAGMA_OMP_CLAUSE_TO;
31531 break;
31532 case 'u':
31533 if (!strcmp ("uniform", p))
31534 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31535 else if (!strcmp ("untied", p))
31536 result = PRAGMA_OMP_CLAUSE_UNTIED;
31537 else if (!strcmp ("use_device", p))
31538 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31539 else if (!strcmp ("use_device_ptr", p))
31540 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31541 break;
31542 case 'v':
31543 if (!strcmp ("vector", p))
31544 result = PRAGMA_OACC_CLAUSE_VECTOR;
31545 else if (!strcmp ("vector_length", p))
31546 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31547 break;
31548 case 'w':
31549 if (!strcmp ("wait", p))
31550 result = PRAGMA_OACC_CLAUSE_WAIT;
31551 else if (!strcmp ("worker", p))
31552 result = PRAGMA_OACC_CLAUSE_WORKER;
31553 break;
31557 if (result != PRAGMA_OMP_CLAUSE_NONE)
31558 cp_lexer_consume_token (parser->lexer);
31560 return result;
31563 /* Validate that a clause of the given type does not already exist. */
31565 static void
31566 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31567 const char *name, location_t location)
31569 tree c;
31571 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31572 if (OMP_CLAUSE_CODE (c) == code)
31574 error_at (location, "too many %qs clauses", name);
31575 break;
31579 /* OpenMP 2.5:
31580 variable-list:
31581 identifier
31582 variable-list , identifier
31584 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31585 colon). An opening parenthesis will have been consumed by the caller.
31587 If KIND is nonzero, create the appropriate node and install the decl
31588 in OMP_CLAUSE_DECL and add the node to the head of the list.
31590 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31591 return the list created.
31593 COLON can be NULL if only closing parenthesis should end the list,
31594 or pointer to bool which will receive false if the list is terminated
31595 by closing parenthesis or true if the list is terminated by colon. */
31597 static tree
31598 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31599 tree list, bool *colon)
31601 cp_token *token;
31602 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31603 if (colon)
31605 parser->colon_corrects_to_scope_p = false;
31606 *colon = false;
31608 while (1)
31610 tree name, decl;
31612 token = cp_lexer_peek_token (parser->lexer);
31613 if (kind != 0
31614 && current_class_ptr
31615 && cp_parser_is_keyword (token, RID_THIS))
31617 decl = finish_this_expr ();
31618 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31619 || CONVERT_EXPR_P (decl))
31620 decl = TREE_OPERAND (decl, 0);
31621 cp_lexer_consume_token (parser->lexer);
31623 else
31625 name = cp_parser_id_expression (parser, /*template_p=*/false,
31626 /*check_dependency_p=*/true,
31627 /*template_p=*/NULL,
31628 /*declarator_p=*/false,
31629 /*optional_p=*/false);
31630 if (name == error_mark_node)
31631 goto skip_comma;
31633 if (identifier_p (name))
31634 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31635 else
31636 decl = name;
31637 if (decl == error_mark_node)
31638 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31639 token->location);
31641 if (decl == error_mark_node)
31643 else if (kind != 0)
31645 switch (kind)
31647 case OMP_CLAUSE__CACHE_:
31648 /* The OpenACC cache directive explicitly only allows "array
31649 elements or subarrays". */
31650 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31652 error_at (token->location, "expected %<[%>");
31653 decl = error_mark_node;
31654 break;
31656 /* FALLTHROUGH. */
31657 case OMP_CLAUSE_MAP:
31658 case OMP_CLAUSE_FROM:
31659 case OMP_CLAUSE_TO:
31660 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31662 location_t loc
31663 = cp_lexer_peek_token (parser->lexer)->location;
31664 cp_id_kind idk = CP_ID_KIND_NONE;
31665 cp_lexer_consume_token (parser->lexer);
31666 decl = convert_from_reference (decl);
31667 decl
31668 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31669 decl, false,
31670 &idk, loc);
31672 /* FALLTHROUGH. */
31673 case OMP_CLAUSE_DEPEND:
31674 case OMP_CLAUSE_REDUCTION:
31675 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31677 tree low_bound = NULL_TREE, length = NULL_TREE;
31679 parser->colon_corrects_to_scope_p = false;
31680 cp_lexer_consume_token (parser->lexer);
31681 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31682 low_bound = cp_parser_expression (parser);
31683 if (!colon)
31684 parser->colon_corrects_to_scope_p
31685 = saved_colon_corrects_to_scope_p;
31686 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31687 length = integer_one_node;
31688 else
31690 /* Look for `:'. */
31691 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31692 goto skip_comma;
31693 if (!cp_lexer_next_token_is (parser->lexer,
31694 CPP_CLOSE_SQUARE))
31695 length = cp_parser_expression (parser);
31697 /* Look for the closing `]'. */
31698 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31699 RT_CLOSE_SQUARE))
31700 goto skip_comma;
31702 decl = tree_cons (low_bound, length, decl);
31704 break;
31705 default:
31706 break;
31709 tree u = build_omp_clause (token->location, kind);
31710 OMP_CLAUSE_DECL (u) = decl;
31711 OMP_CLAUSE_CHAIN (u) = list;
31712 list = u;
31714 else
31715 list = tree_cons (decl, NULL_TREE, list);
31717 get_comma:
31718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31719 break;
31720 cp_lexer_consume_token (parser->lexer);
31723 if (colon)
31724 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31726 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31728 *colon = true;
31729 cp_parser_require (parser, CPP_COLON, RT_COLON);
31730 return list;
31733 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31735 int ending;
31737 /* Try to resync to an unnested comma. Copied from
31738 cp_parser_parenthesized_expression_list. */
31739 skip_comma:
31740 if (colon)
31741 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31742 ending = cp_parser_skip_to_closing_parenthesis (parser,
31743 /*recovering=*/true,
31744 /*or_comma=*/true,
31745 /*consume_paren=*/true);
31746 if (ending < 0)
31747 goto get_comma;
31750 return list;
31753 /* Similarly, but expect leading and trailing parenthesis. This is a very
31754 common case for omp clauses. */
31756 static tree
31757 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31759 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31760 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31761 return list;
31764 /* OpenACC 2.0:
31765 copy ( variable-list )
31766 copyin ( variable-list )
31767 copyout ( variable-list )
31768 create ( variable-list )
31769 delete ( variable-list )
31770 present ( variable-list ) */
31772 static tree
31773 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31774 tree list)
31776 enum gomp_map_kind kind;
31777 switch (c_kind)
31779 case PRAGMA_OACC_CLAUSE_COPY:
31780 kind = GOMP_MAP_TOFROM;
31781 break;
31782 case PRAGMA_OACC_CLAUSE_COPYIN:
31783 kind = GOMP_MAP_TO;
31784 break;
31785 case PRAGMA_OACC_CLAUSE_COPYOUT:
31786 kind = GOMP_MAP_FROM;
31787 break;
31788 case PRAGMA_OACC_CLAUSE_CREATE:
31789 kind = GOMP_MAP_ALLOC;
31790 break;
31791 case PRAGMA_OACC_CLAUSE_DELETE:
31792 kind = GOMP_MAP_RELEASE;
31793 break;
31794 case PRAGMA_OACC_CLAUSE_DEVICE:
31795 kind = GOMP_MAP_FORCE_TO;
31796 break;
31797 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31798 kind = GOMP_MAP_DEVICE_RESIDENT;
31799 break;
31800 case PRAGMA_OACC_CLAUSE_HOST:
31801 kind = GOMP_MAP_FORCE_FROM;
31802 break;
31803 case PRAGMA_OACC_CLAUSE_LINK:
31804 kind = GOMP_MAP_LINK;
31805 break;
31806 case PRAGMA_OACC_CLAUSE_PRESENT:
31807 kind = GOMP_MAP_FORCE_PRESENT;
31808 break;
31809 default:
31810 gcc_unreachable ();
31812 tree nl, c;
31813 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31815 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31816 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31818 return nl;
31821 /* OpenACC 2.0:
31822 deviceptr ( variable-list ) */
31824 static tree
31825 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31827 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31828 tree vars, t;
31830 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31831 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31832 variable-list must only allow for pointer variables. */
31833 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31834 for (t = vars; t; t = TREE_CHAIN (t))
31836 tree v = TREE_PURPOSE (t);
31837 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31838 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31839 OMP_CLAUSE_DECL (u) = v;
31840 OMP_CLAUSE_CHAIN (u) = list;
31841 list = u;
31844 return list;
31847 /* OpenACC 2.5:
31848 auto
31849 finalize
31850 independent
31851 nohost
31852 seq */
31854 static tree
31855 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31856 enum omp_clause_code code,
31857 tree list, location_t location)
31859 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31860 tree c = build_omp_clause (location, code);
31861 OMP_CLAUSE_CHAIN (c) = list;
31862 return c;
31865 /* OpenACC:
31866 num_gangs ( expression )
31867 num_workers ( expression )
31868 vector_length ( expression ) */
31870 static tree
31871 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31872 const char *str, tree list)
31874 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31876 matching_parens parens;
31877 if (!parens.require_open (parser))
31878 return list;
31880 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31882 if (t == error_mark_node
31883 || !parens.require_close (parser))
31885 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31886 /*or_comma=*/false,
31887 /*consume_paren=*/true);
31888 return list;
31891 check_no_duplicate_clause (list, code, str, loc);
31893 tree c = build_omp_clause (loc, code);
31894 OMP_CLAUSE_OPERAND (c, 0) = t;
31895 OMP_CLAUSE_CHAIN (c) = list;
31896 return c;
31899 /* OpenACC:
31901 gang [( gang-arg-list )]
31902 worker [( [num:] int-expr )]
31903 vector [( [length:] int-expr )]
31905 where gang-arg is one of:
31907 [num:] int-expr
31908 static: size-expr
31910 and size-expr may be:
31913 int-expr
31916 static tree
31917 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31918 const char *str, tree list)
31920 const char *id = "num";
31921 cp_lexer *lexer = parser->lexer;
31922 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31923 location_t loc = cp_lexer_peek_token (lexer)->location;
31925 if (kind == OMP_CLAUSE_VECTOR)
31926 id = "length";
31928 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31930 matching_parens parens;
31931 parens.consume_open (parser);
31935 cp_token *next = cp_lexer_peek_token (lexer);
31936 int idx = 0;
31938 /* Gang static argument. */
31939 if (kind == OMP_CLAUSE_GANG
31940 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31942 cp_lexer_consume_token (lexer);
31944 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31945 goto cleanup_error;
31947 idx = 1;
31948 if (ops[idx] != NULL)
31950 cp_parser_error (parser, "too many %<static%> arguments");
31951 goto cleanup_error;
31954 /* Check for the '*' argument. */
31955 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31956 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31957 || cp_lexer_nth_token_is (parser->lexer, 2,
31958 CPP_CLOSE_PAREN)))
31960 cp_lexer_consume_token (lexer);
31961 ops[idx] = integer_minus_one_node;
31963 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31965 cp_lexer_consume_token (lexer);
31966 continue;
31968 else break;
31971 /* Worker num: argument and vector length: arguments. */
31972 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31973 && id_equal (next->u.value, id)
31974 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31976 cp_lexer_consume_token (lexer); /* id */
31977 cp_lexer_consume_token (lexer); /* ':' */
31980 /* Now collect the actual argument. */
31981 if (ops[idx] != NULL_TREE)
31983 cp_parser_error (parser, "unexpected argument");
31984 goto cleanup_error;
31987 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31988 false);
31989 if (expr == error_mark_node)
31990 goto cleanup_error;
31992 mark_exp_read (expr);
31993 ops[idx] = expr;
31995 if (kind == OMP_CLAUSE_GANG
31996 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31998 cp_lexer_consume_token (lexer);
31999 continue;
32001 break;
32003 while (1);
32005 if (!parens.require_close (parser))
32006 goto cleanup_error;
32009 check_no_duplicate_clause (list, kind, str, loc);
32011 c = build_omp_clause (loc, kind);
32013 if (ops[1])
32014 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32016 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32017 OMP_CLAUSE_CHAIN (c) = list;
32019 return c;
32021 cleanup_error:
32022 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32023 return list;
32026 /* OpenACC 2.0:
32027 tile ( size-expr-list ) */
32029 static tree
32030 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32032 tree c, expr = error_mark_node;
32033 tree tile = NULL_TREE;
32035 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32036 so, but the spec authors never considered such a case and have
32037 differing opinions on what it might mean, including 'not
32038 allowed'.) */
32039 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32040 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32041 clause_loc);
32043 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32044 return list;
32048 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32049 return list;
32051 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32052 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32053 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32055 cp_lexer_consume_token (parser->lexer);
32056 expr = integer_zero_node;
32058 else
32059 expr = cp_parser_constant_expression (parser);
32061 tile = tree_cons (NULL_TREE, expr, tile);
32063 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32065 /* Consume the trailing ')'. */
32066 cp_lexer_consume_token (parser->lexer);
32068 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32069 tile = nreverse (tile);
32070 OMP_CLAUSE_TILE_LIST (c) = tile;
32071 OMP_CLAUSE_CHAIN (c) = list;
32072 return c;
32075 /* OpenACC 2.0
32076 Parse wait clause or directive parameters. */
32078 static tree
32079 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32081 vec<tree, va_gc> *args;
32082 tree t, args_tree;
32084 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32085 /*cast_p=*/false,
32086 /*allow_expansion_p=*/true,
32087 /*non_constant_p=*/NULL);
32089 if (args == NULL || args->length () == 0)
32091 cp_parser_error (parser, "expected integer expression before ')'");
32092 if (args != NULL)
32093 release_tree_vector (args);
32094 return list;
32097 args_tree = build_tree_list_vec (args);
32099 release_tree_vector (args);
32101 for (t = args_tree; t; t = TREE_CHAIN (t))
32103 tree targ = TREE_VALUE (t);
32105 if (targ != error_mark_node)
32107 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32108 error ("%<wait%> expression must be integral");
32109 else
32111 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32113 targ = mark_rvalue_use (targ);
32114 OMP_CLAUSE_DECL (c) = targ;
32115 OMP_CLAUSE_CHAIN (c) = list;
32116 list = c;
32121 return list;
32124 /* OpenACC:
32125 wait ( int-expr-list ) */
32127 static tree
32128 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32130 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32132 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32133 return list;
32135 list = cp_parser_oacc_wait_list (parser, location, list);
32137 return list;
32140 /* OpenMP 3.0:
32141 collapse ( constant-expression ) */
32143 static tree
32144 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32146 tree c, num;
32147 location_t loc;
32148 HOST_WIDE_INT n;
32150 loc = cp_lexer_peek_token (parser->lexer)->location;
32151 matching_parens parens;
32152 if (!parens.require_open (parser))
32153 return list;
32155 num = cp_parser_constant_expression (parser);
32157 if (!parens.require_close (parser))
32158 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32159 /*or_comma=*/false,
32160 /*consume_paren=*/true);
32162 if (num == error_mark_node)
32163 return list;
32164 num = fold_non_dependent_expr (num);
32165 if (!tree_fits_shwi_p (num)
32166 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32167 || (n = tree_to_shwi (num)) <= 0
32168 || (int) n != n)
32170 error_at (loc, "collapse argument needs positive constant integer expression");
32171 return list;
32174 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32175 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32176 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32177 OMP_CLAUSE_CHAIN (c) = list;
32178 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32180 return c;
32183 /* OpenMP 2.5:
32184 default ( none | shared )
32186 OpenACC:
32187 default ( none | present ) */
32189 static tree
32190 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32191 location_t location, bool is_oacc)
32193 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32194 tree c;
32196 matching_parens parens;
32197 if (!parens.require_open (parser))
32198 return list;
32199 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32201 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32202 const char *p = IDENTIFIER_POINTER (id);
32204 switch (p[0])
32206 case 'n':
32207 if (strcmp ("none", p) != 0)
32208 goto invalid_kind;
32209 kind = OMP_CLAUSE_DEFAULT_NONE;
32210 break;
32212 case 'p':
32213 if (strcmp ("present", p) != 0 || !is_oacc)
32214 goto invalid_kind;
32215 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32216 break;
32218 case 's':
32219 if (strcmp ("shared", p) != 0 || is_oacc)
32220 goto invalid_kind;
32221 kind = OMP_CLAUSE_DEFAULT_SHARED;
32222 break;
32224 default:
32225 goto invalid_kind;
32228 cp_lexer_consume_token (parser->lexer);
32230 else
32232 invalid_kind:
32233 if (is_oacc)
32234 cp_parser_error (parser, "expected %<none%> or %<present%>");
32235 else
32236 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32239 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32240 || !parens.require_close (parser))
32241 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32242 /*or_comma=*/false,
32243 /*consume_paren=*/true);
32245 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32246 return list;
32248 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32249 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32250 OMP_CLAUSE_CHAIN (c) = list;
32251 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32253 return c;
32256 /* OpenMP 3.1:
32257 final ( expression ) */
32259 static tree
32260 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32262 tree t, c;
32264 matching_parens parens;
32265 if (!parens.require_open (parser))
32266 return list;
32268 t = cp_parser_condition (parser);
32270 if (t == error_mark_node
32271 || !parens.require_close (parser))
32272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32273 /*or_comma=*/false,
32274 /*consume_paren=*/true);
32276 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32278 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32279 OMP_CLAUSE_FINAL_EXPR (c) = t;
32280 OMP_CLAUSE_CHAIN (c) = list;
32282 return c;
32285 /* OpenMP 2.5:
32286 if ( expression )
32288 OpenMP 4.5:
32289 if ( directive-name-modifier : expression )
32291 directive-name-modifier:
32292 parallel | task | taskloop | target data | target | target update
32293 | target enter data | target exit data */
32295 static tree
32296 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32297 bool is_omp)
32299 tree t, c;
32300 enum tree_code if_modifier = ERROR_MARK;
32302 matching_parens parens;
32303 if (!parens.require_open (parser))
32304 return list;
32306 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32308 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32309 const char *p = IDENTIFIER_POINTER (id);
32310 int n = 2;
32312 if (strcmp ("parallel", p) == 0)
32313 if_modifier = OMP_PARALLEL;
32314 else if (strcmp ("task", p) == 0)
32315 if_modifier = OMP_TASK;
32316 else if (strcmp ("taskloop", p) == 0)
32317 if_modifier = OMP_TASKLOOP;
32318 else if (strcmp ("target", p) == 0)
32320 if_modifier = OMP_TARGET;
32321 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32323 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32324 p = IDENTIFIER_POINTER (id);
32325 if (strcmp ("data", p) == 0)
32326 if_modifier = OMP_TARGET_DATA;
32327 else if (strcmp ("update", p) == 0)
32328 if_modifier = OMP_TARGET_UPDATE;
32329 else if (strcmp ("enter", p) == 0)
32330 if_modifier = OMP_TARGET_ENTER_DATA;
32331 else if (strcmp ("exit", p) == 0)
32332 if_modifier = OMP_TARGET_EXIT_DATA;
32333 if (if_modifier != OMP_TARGET)
32334 n = 3;
32335 else
32337 location_t loc
32338 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32339 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32340 "or %<exit%>");
32341 if_modifier = ERROR_MARK;
32343 if (if_modifier == OMP_TARGET_ENTER_DATA
32344 || if_modifier == OMP_TARGET_EXIT_DATA)
32346 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32348 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32349 p = IDENTIFIER_POINTER (id);
32350 if (strcmp ("data", p) == 0)
32351 n = 4;
32353 if (n != 4)
32355 location_t loc
32356 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32357 error_at (loc, "expected %<data%>");
32358 if_modifier = ERROR_MARK;
32363 if (if_modifier != ERROR_MARK)
32365 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32367 while (n-- > 0)
32368 cp_lexer_consume_token (parser->lexer);
32370 else
32372 if (n > 2)
32374 location_t loc
32375 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32376 error_at (loc, "expected %<:%>");
32378 if_modifier = ERROR_MARK;
32383 t = cp_parser_condition (parser);
32385 if (t == error_mark_node
32386 || !parens.require_close (parser))
32387 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32388 /*or_comma=*/false,
32389 /*consume_paren=*/true);
32391 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32392 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32394 if (if_modifier != ERROR_MARK
32395 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32397 const char *p = NULL;
32398 switch (if_modifier)
32400 case OMP_PARALLEL: p = "parallel"; break;
32401 case OMP_TASK: p = "task"; break;
32402 case OMP_TASKLOOP: p = "taskloop"; break;
32403 case OMP_TARGET_DATA: p = "target data"; break;
32404 case OMP_TARGET: p = "target"; break;
32405 case OMP_TARGET_UPDATE: p = "target update"; break;
32406 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32407 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32408 default: gcc_unreachable ();
32410 error_at (location, "too many %<if%> clauses with %qs modifier",
32412 return list;
32414 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32416 if (!is_omp)
32417 error_at (location, "too many %<if%> clauses");
32418 else
32419 error_at (location, "too many %<if%> clauses without modifier");
32420 return list;
32422 else if (if_modifier == ERROR_MARK
32423 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32425 error_at (location, "if any %<if%> clause has modifier, then all "
32426 "%<if%> clauses have to use modifier");
32427 return list;
32431 c = build_omp_clause (location, OMP_CLAUSE_IF);
32432 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32433 OMP_CLAUSE_IF_EXPR (c) = t;
32434 OMP_CLAUSE_CHAIN (c) = list;
32436 return c;
32439 /* OpenMP 3.1:
32440 mergeable */
32442 static tree
32443 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32444 tree list, location_t location)
32446 tree c;
32448 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32449 location);
32451 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32452 OMP_CLAUSE_CHAIN (c) = list;
32453 return c;
32456 /* OpenMP 2.5:
32457 nowait */
32459 static tree
32460 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32461 tree list, location_t location)
32463 tree c;
32465 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32467 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32468 OMP_CLAUSE_CHAIN (c) = list;
32469 return c;
32472 /* OpenMP 2.5:
32473 num_threads ( expression ) */
32475 static tree
32476 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32477 location_t location)
32479 tree t, c;
32481 matching_parens parens;
32482 if (!parens.require_open (parser))
32483 return list;
32485 t = cp_parser_expression (parser);
32487 if (t == error_mark_node
32488 || !parens.require_close (parser))
32489 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32490 /*or_comma=*/false,
32491 /*consume_paren=*/true);
32493 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32494 "num_threads", location);
32496 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32497 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32498 OMP_CLAUSE_CHAIN (c) = list;
32500 return c;
32503 /* OpenMP 4.5:
32504 num_tasks ( expression ) */
32506 static tree
32507 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32508 location_t location)
32510 tree t, c;
32512 matching_parens parens;
32513 if (!parens.require_open (parser))
32514 return list;
32516 t = cp_parser_expression (parser);
32518 if (t == error_mark_node
32519 || !parens.require_close (parser))
32520 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32521 /*or_comma=*/false,
32522 /*consume_paren=*/true);
32524 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32525 "num_tasks", location);
32527 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32528 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32529 OMP_CLAUSE_CHAIN (c) = list;
32531 return c;
32534 /* OpenMP 4.5:
32535 grainsize ( expression ) */
32537 static tree
32538 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32539 location_t location)
32541 tree t, c;
32543 matching_parens parens;
32544 if (!parens.require_open (parser))
32545 return list;
32547 t = cp_parser_expression (parser);
32549 if (t == error_mark_node
32550 || !parens.require_close (parser))
32551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32552 /*or_comma=*/false,
32553 /*consume_paren=*/true);
32555 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32556 "grainsize", location);
32558 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32559 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32560 OMP_CLAUSE_CHAIN (c) = list;
32562 return c;
32565 /* OpenMP 4.5:
32566 priority ( expression ) */
32568 static tree
32569 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32570 location_t location)
32572 tree t, c;
32574 matching_parens parens;
32575 if (!parens.require_open (parser))
32576 return list;
32578 t = cp_parser_expression (parser);
32580 if (t == error_mark_node
32581 || !parens.require_close (parser))
32582 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32583 /*or_comma=*/false,
32584 /*consume_paren=*/true);
32586 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32587 "priority", location);
32589 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32590 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32591 OMP_CLAUSE_CHAIN (c) = list;
32593 return c;
32596 /* OpenMP 4.5:
32597 hint ( expression ) */
32599 static tree
32600 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32601 location_t location)
32603 tree t, c;
32605 matching_parens parens;
32606 if (!parens.require_open (parser))
32607 return list;
32609 t = cp_parser_expression (parser);
32611 if (t == error_mark_node
32612 || !parens.require_close (parser))
32613 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32614 /*or_comma=*/false,
32615 /*consume_paren=*/true);
32617 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32619 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32620 OMP_CLAUSE_HINT_EXPR (c) = t;
32621 OMP_CLAUSE_CHAIN (c) = list;
32623 return c;
32626 /* OpenMP 4.5:
32627 defaultmap ( tofrom : scalar ) */
32629 static tree
32630 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32631 location_t location)
32633 tree c, id;
32634 const char *p;
32636 matching_parens parens;
32637 if (!parens.require_open (parser))
32638 return list;
32640 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32642 cp_parser_error (parser, "expected %<tofrom%>");
32643 goto out_err;
32645 id = cp_lexer_peek_token (parser->lexer)->u.value;
32646 p = IDENTIFIER_POINTER (id);
32647 if (strcmp (p, "tofrom") != 0)
32649 cp_parser_error (parser, "expected %<tofrom%>");
32650 goto out_err;
32652 cp_lexer_consume_token (parser->lexer);
32653 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32654 goto out_err;
32656 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32658 cp_parser_error (parser, "expected %<scalar%>");
32659 goto out_err;
32661 id = cp_lexer_peek_token (parser->lexer)->u.value;
32662 p = IDENTIFIER_POINTER (id);
32663 if (strcmp (p, "scalar") != 0)
32665 cp_parser_error (parser, "expected %<scalar%>");
32666 goto out_err;
32668 cp_lexer_consume_token (parser->lexer);
32669 if (!parens.require_close (parser))
32670 goto out_err;
32672 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32673 location);
32675 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32676 OMP_CLAUSE_CHAIN (c) = list;
32677 return c;
32679 out_err:
32680 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32681 /*or_comma=*/false,
32682 /*consume_paren=*/true);
32683 return list;
32686 /* OpenMP 2.5:
32687 ordered
32689 OpenMP 4.5:
32690 ordered ( constant-expression ) */
32692 static tree
32693 cp_parser_omp_clause_ordered (cp_parser *parser,
32694 tree list, location_t location)
32696 tree c, num = NULL_TREE;
32697 HOST_WIDE_INT n;
32699 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32700 "ordered", location);
32702 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32704 matching_parens parens;
32705 parens.consume_open (parser);
32707 num = cp_parser_constant_expression (parser);
32709 if (!parens.require_close (parser))
32710 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32711 /*or_comma=*/false,
32712 /*consume_paren=*/true);
32714 if (num == error_mark_node)
32715 return list;
32716 num = fold_non_dependent_expr (num);
32717 if (!tree_fits_shwi_p (num)
32718 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32719 || (n = tree_to_shwi (num)) <= 0
32720 || (int) n != n)
32722 error_at (location,
32723 "ordered argument needs positive constant integer "
32724 "expression");
32725 return list;
32729 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32730 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32731 OMP_CLAUSE_CHAIN (c) = list;
32732 return c;
32735 /* OpenMP 2.5:
32736 reduction ( reduction-operator : variable-list )
32738 reduction-operator:
32739 One of: + * - & ^ | && ||
32741 OpenMP 3.1:
32743 reduction-operator:
32744 One of: + * - & ^ | && || min max
32746 OpenMP 4.0:
32748 reduction-operator:
32749 One of: + * - & ^ | && ||
32750 id-expression */
32752 static tree
32753 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32755 enum tree_code code = ERROR_MARK;
32756 tree nlist, c, id = NULL_TREE;
32758 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32759 return list;
32761 switch (cp_lexer_peek_token (parser->lexer)->type)
32763 case CPP_PLUS: code = PLUS_EXPR; break;
32764 case CPP_MULT: code = MULT_EXPR; break;
32765 case CPP_MINUS: code = MINUS_EXPR; break;
32766 case CPP_AND: code = BIT_AND_EXPR; break;
32767 case CPP_XOR: code = BIT_XOR_EXPR; break;
32768 case CPP_OR: code = BIT_IOR_EXPR; break;
32769 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32770 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32771 default: break;
32774 if (code != ERROR_MARK)
32775 cp_lexer_consume_token (parser->lexer);
32776 else
32778 bool saved_colon_corrects_to_scope_p;
32779 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32780 parser->colon_corrects_to_scope_p = false;
32781 id = cp_parser_id_expression (parser, /*template_p=*/false,
32782 /*check_dependency_p=*/true,
32783 /*template_p=*/NULL,
32784 /*declarator_p=*/false,
32785 /*optional_p=*/false);
32786 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32787 if (identifier_p (id))
32789 const char *p = IDENTIFIER_POINTER (id);
32791 if (strcmp (p, "min") == 0)
32792 code = MIN_EXPR;
32793 else if (strcmp (p, "max") == 0)
32794 code = MAX_EXPR;
32795 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32796 code = PLUS_EXPR;
32797 else if (id == ovl_op_identifier (false, MULT_EXPR))
32798 code = MULT_EXPR;
32799 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32800 code = MINUS_EXPR;
32801 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32802 code = BIT_AND_EXPR;
32803 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32804 code = BIT_IOR_EXPR;
32805 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32806 code = BIT_XOR_EXPR;
32807 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32808 code = TRUTH_ANDIF_EXPR;
32809 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32810 code = TRUTH_ORIF_EXPR;
32811 id = omp_reduction_id (code, id, NULL_TREE);
32812 tree scope = parser->scope;
32813 if (scope)
32814 id = build_qualified_name (NULL_TREE, scope, id, false);
32815 parser->scope = NULL_TREE;
32816 parser->qualifying_scope = NULL_TREE;
32817 parser->object_scope = NULL_TREE;
32819 else
32821 error ("invalid reduction-identifier");
32822 resync_fail:
32823 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32824 /*or_comma=*/false,
32825 /*consume_paren=*/true);
32826 return list;
32830 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32831 goto resync_fail;
32833 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32834 NULL);
32835 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32837 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32838 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32841 return nlist;
32844 /* OpenMP 2.5:
32845 schedule ( schedule-kind )
32846 schedule ( schedule-kind , expression )
32848 schedule-kind:
32849 static | dynamic | guided | runtime | auto
32851 OpenMP 4.5:
32852 schedule ( schedule-modifier : schedule-kind )
32853 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32855 schedule-modifier:
32856 simd
32857 monotonic
32858 nonmonotonic */
32860 static tree
32861 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32863 tree c, t;
32864 int modifiers = 0, nmodifiers = 0;
32866 matching_parens parens;
32867 if (!parens.require_open (parser))
32868 return list;
32870 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32872 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32874 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32875 const char *p = IDENTIFIER_POINTER (id);
32876 if (strcmp ("simd", p) == 0)
32877 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32878 else if (strcmp ("monotonic", p) == 0)
32879 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32880 else if (strcmp ("nonmonotonic", p) == 0)
32881 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32882 else
32883 break;
32884 cp_lexer_consume_token (parser->lexer);
32885 if (nmodifiers++ == 0
32886 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32887 cp_lexer_consume_token (parser->lexer);
32888 else
32890 cp_parser_require (parser, CPP_COLON, RT_COLON);
32891 break;
32895 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32897 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32898 const char *p = IDENTIFIER_POINTER (id);
32900 switch (p[0])
32902 case 'd':
32903 if (strcmp ("dynamic", p) != 0)
32904 goto invalid_kind;
32905 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32906 break;
32908 case 'g':
32909 if (strcmp ("guided", p) != 0)
32910 goto invalid_kind;
32911 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32912 break;
32914 case 'r':
32915 if (strcmp ("runtime", p) != 0)
32916 goto invalid_kind;
32917 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32918 break;
32920 default:
32921 goto invalid_kind;
32924 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32925 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32926 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32927 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32928 else
32929 goto invalid_kind;
32930 cp_lexer_consume_token (parser->lexer);
32932 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32933 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32934 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32935 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32937 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32938 "specified");
32939 modifiers = 0;
32942 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32944 cp_token *token;
32945 cp_lexer_consume_token (parser->lexer);
32947 token = cp_lexer_peek_token (parser->lexer);
32948 t = cp_parser_assignment_expression (parser);
32950 if (t == error_mark_node)
32951 goto resync_fail;
32952 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32953 error_at (token->location, "schedule %<runtime%> does not take "
32954 "a %<chunk_size%> parameter");
32955 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32956 error_at (token->location, "schedule %<auto%> does not take "
32957 "a %<chunk_size%> parameter");
32958 else
32959 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32961 if (!parens.require_close (parser))
32962 goto resync_fail;
32964 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32965 goto resync_fail;
32967 OMP_CLAUSE_SCHEDULE_KIND (c)
32968 = (enum omp_clause_schedule_kind)
32969 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32971 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32972 OMP_CLAUSE_CHAIN (c) = list;
32973 return c;
32975 invalid_kind:
32976 cp_parser_error (parser, "invalid schedule kind");
32977 resync_fail:
32978 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32979 /*or_comma=*/false,
32980 /*consume_paren=*/true);
32981 return list;
32984 /* OpenMP 3.0:
32985 untied */
32987 static tree
32988 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32989 tree list, location_t location)
32991 tree c;
32993 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32995 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32996 OMP_CLAUSE_CHAIN (c) = list;
32997 return c;
33000 /* OpenMP 4.0:
33001 inbranch
33002 notinbranch */
33004 static tree
33005 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33006 tree list, location_t location)
33008 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33009 tree c = build_omp_clause (location, code);
33010 OMP_CLAUSE_CHAIN (c) = list;
33011 return c;
33014 /* OpenMP 4.0:
33015 parallel
33017 sections
33018 taskgroup */
33020 static tree
33021 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33022 enum omp_clause_code code,
33023 tree list, location_t location)
33025 tree c = build_omp_clause (location, code);
33026 OMP_CLAUSE_CHAIN (c) = list;
33027 return c;
33030 /* OpenMP 4.5:
33031 nogroup */
33033 static tree
33034 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33035 tree list, location_t location)
33037 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33038 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33039 OMP_CLAUSE_CHAIN (c) = list;
33040 return c;
33043 /* OpenMP 4.5:
33044 simd
33045 threads */
33047 static tree
33048 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33049 enum omp_clause_code code,
33050 tree list, location_t location)
33052 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33053 tree c = build_omp_clause (location, code);
33054 OMP_CLAUSE_CHAIN (c) = list;
33055 return c;
33058 /* OpenMP 4.0:
33059 num_teams ( expression ) */
33061 static tree
33062 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33063 location_t location)
33065 tree t, c;
33067 matching_parens parens;
33068 if (!parens.require_open (parser))
33069 return list;
33071 t = cp_parser_expression (parser);
33073 if (t == error_mark_node
33074 || !parens.require_close (parser))
33075 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33076 /*or_comma=*/false,
33077 /*consume_paren=*/true);
33079 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33080 "num_teams", location);
33082 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33083 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33084 OMP_CLAUSE_CHAIN (c) = list;
33086 return c;
33089 /* OpenMP 4.0:
33090 thread_limit ( expression ) */
33092 static tree
33093 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33094 location_t location)
33096 tree t, c;
33098 matching_parens parens;
33099 if (!parens.require_open (parser))
33100 return list;
33102 t = cp_parser_expression (parser);
33104 if (t == error_mark_node
33105 || !parens.require_close (parser))
33106 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33107 /*or_comma=*/false,
33108 /*consume_paren=*/true);
33110 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33111 "thread_limit", location);
33113 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33114 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33115 OMP_CLAUSE_CHAIN (c) = list;
33117 return c;
33120 /* OpenMP 4.0:
33121 aligned ( variable-list )
33122 aligned ( variable-list : constant-expression ) */
33124 static tree
33125 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33127 tree nlist, c, alignment = NULL_TREE;
33128 bool colon;
33130 matching_parens parens;
33131 if (!parens.require_open (parser))
33132 return list;
33134 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33135 &colon);
33137 if (colon)
33139 alignment = cp_parser_constant_expression (parser);
33141 if (!parens.require_close (parser))
33142 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33143 /*or_comma=*/false,
33144 /*consume_paren=*/true);
33146 if (alignment == error_mark_node)
33147 alignment = NULL_TREE;
33150 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33151 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33153 return nlist;
33156 /* OpenMP 4.0:
33157 linear ( variable-list )
33158 linear ( variable-list : expression )
33160 OpenMP 4.5:
33161 linear ( modifier ( variable-list ) )
33162 linear ( modifier ( variable-list ) : expression ) */
33164 static tree
33165 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33166 bool declare_simd)
33168 tree nlist, c, step = integer_one_node;
33169 bool colon;
33170 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33172 matching_parens parens;
33173 if (!parens.require_open (parser))
33174 return list;
33176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33178 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33179 const char *p = IDENTIFIER_POINTER (id);
33181 if (strcmp ("ref", p) == 0)
33182 kind = OMP_CLAUSE_LINEAR_REF;
33183 else if (strcmp ("val", p) == 0)
33184 kind = OMP_CLAUSE_LINEAR_VAL;
33185 else if (strcmp ("uval", p) == 0)
33186 kind = OMP_CLAUSE_LINEAR_UVAL;
33187 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33188 cp_lexer_consume_token (parser->lexer);
33189 else
33190 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33193 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33194 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33195 &colon);
33196 else
33198 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33199 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33200 if (colon)
33201 cp_parser_require (parser, CPP_COLON, RT_COLON);
33202 else if (!parens.require_close (parser))
33203 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33204 /*or_comma=*/false,
33205 /*consume_paren=*/true);
33208 if (colon)
33210 step = NULL_TREE;
33211 if (declare_simd
33212 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33213 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33215 cp_token *token = cp_lexer_peek_token (parser->lexer);
33216 cp_parser_parse_tentatively (parser);
33217 step = cp_parser_id_expression (parser, /*template_p=*/false,
33218 /*check_dependency_p=*/true,
33219 /*template_p=*/NULL,
33220 /*declarator_p=*/false,
33221 /*optional_p=*/false);
33222 if (step != error_mark_node)
33223 step = cp_parser_lookup_name_simple (parser, step, token->location);
33224 if (step == error_mark_node)
33226 step = NULL_TREE;
33227 cp_parser_abort_tentative_parse (parser);
33229 else if (!cp_parser_parse_definitely (parser))
33230 step = NULL_TREE;
33232 if (!step)
33233 step = cp_parser_expression (parser);
33235 if (!parens.require_close (parser))
33236 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33237 /*or_comma=*/false,
33238 /*consume_paren=*/true);
33240 if (step == error_mark_node)
33241 return list;
33244 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33246 OMP_CLAUSE_LINEAR_STEP (c) = step;
33247 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33250 return nlist;
33253 /* OpenMP 4.0:
33254 safelen ( constant-expression ) */
33256 static tree
33257 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33258 location_t location)
33260 tree t, c;
33262 matching_parens parens;
33263 if (!parens.require_open (parser))
33264 return list;
33266 t = cp_parser_constant_expression (parser);
33268 if (t == error_mark_node
33269 || !parens.require_close (parser))
33270 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33271 /*or_comma=*/false,
33272 /*consume_paren=*/true);
33274 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33276 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33277 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33278 OMP_CLAUSE_CHAIN (c) = list;
33280 return c;
33283 /* OpenMP 4.0:
33284 simdlen ( constant-expression ) */
33286 static tree
33287 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33288 location_t location)
33290 tree t, c;
33292 matching_parens parens;
33293 if (!parens.require_open (parser))
33294 return list;
33296 t = cp_parser_constant_expression (parser);
33298 if (t == error_mark_node
33299 || !parens.require_close (parser))
33300 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33301 /*or_comma=*/false,
33302 /*consume_paren=*/true);
33304 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33306 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33307 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33308 OMP_CLAUSE_CHAIN (c) = list;
33310 return c;
33313 /* OpenMP 4.5:
33314 vec:
33315 identifier [+/- integer]
33316 vec , identifier [+/- integer]
33319 static tree
33320 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33321 tree list)
33323 tree vec = NULL;
33325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33327 cp_parser_error (parser, "expected identifier");
33328 return list;
33331 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33333 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33334 tree t, identifier = cp_parser_identifier (parser);
33335 tree addend = NULL;
33337 if (identifier == error_mark_node)
33338 t = error_mark_node;
33339 else
33341 t = cp_parser_lookup_name_simple
33342 (parser, identifier,
33343 cp_lexer_peek_token (parser->lexer)->location);
33344 if (t == error_mark_node)
33345 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33346 id_loc);
33349 bool neg = false;
33350 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33351 neg = true;
33352 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33354 addend = integer_zero_node;
33355 goto add_to_vector;
33357 cp_lexer_consume_token (parser->lexer);
33359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33361 cp_parser_error (parser, "expected integer");
33362 return list;
33365 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33366 if (TREE_CODE (addend) != INTEGER_CST)
33368 cp_parser_error (parser, "expected integer");
33369 return list;
33371 cp_lexer_consume_token (parser->lexer);
33373 add_to_vector:
33374 if (t != error_mark_node)
33376 vec = tree_cons (addend, t, vec);
33377 if (neg)
33378 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33382 break;
33384 cp_lexer_consume_token (parser->lexer);
33387 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33389 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33390 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33391 OMP_CLAUSE_DECL (u) = nreverse (vec);
33392 OMP_CLAUSE_CHAIN (u) = list;
33393 return u;
33395 return list;
33398 /* OpenMP 4.0:
33399 depend ( depend-kind : variable-list )
33401 depend-kind:
33402 in | out | inout
33404 OpenMP 4.5:
33405 depend ( source )
33407 depend ( sink : vec ) */
33409 static tree
33410 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33412 tree nlist, c;
33413 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33415 matching_parens parens;
33416 if (!parens.require_open (parser))
33417 return list;
33419 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33421 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33422 const char *p = IDENTIFIER_POINTER (id);
33424 if (strcmp ("in", p) == 0)
33425 kind = OMP_CLAUSE_DEPEND_IN;
33426 else if (strcmp ("inout", p) == 0)
33427 kind = OMP_CLAUSE_DEPEND_INOUT;
33428 else if (strcmp ("out", p) == 0)
33429 kind = OMP_CLAUSE_DEPEND_OUT;
33430 else if (strcmp ("source", p) == 0)
33431 kind = OMP_CLAUSE_DEPEND_SOURCE;
33432 else if (strcmp ("sink", p) == 0)
33433 kind = OMP_CLAUSE_DEPEND_SINK;
33434 else
33435 goto invalid_kind;
33437 else
33438 goto invalid_kind;
33440 cp_lexer_consume_token (parser->lexer);
33442 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33444 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33445 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33446 OMP_CLAUSE_DECL (c) = NULL_TREE;
33447 OMP_CLAUSE_CHAIN (c) = list;
33448 if (!parens.require_close (parser))
33449 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33450 /*or_comma=*/false,
33451 /*consume_paren=*/true);
33452 return c;
33455 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33456 goto resync_fail;
33458 if (kind == OMP_CLAUSE_DEPEND_SINK)
33459 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33460 else
33462 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33463 list, NULL);
33465 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33466 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33468 return nlist;
33470 invalid_kind:
33471 cp_parser_error (parser, "invalid depend kind");
33472 resync_fail:
33473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33474 /*or_comma=*/false,
33475 /*consume_paren=*/true);
33476 return list;
33479 /* OpenMP 4.0:
33480 map ( map-kind : variable-list )
33481 map ( variable-list )
33483 map-kind:
33484 alloc | to | from | tofrom
33486 OpenMP 4.5:
33487 map-kind:
33488 alloc | to | from | tofrom | release | delete
33490 map ( always [,] map-kind: variable-list ) */
33492 static tree
33493 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33495 tree nlist, c;
33496 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33497 bool always = false;
33499 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33500 return list;
33502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33504 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33505 const char *p = IDENTIFIER_POINTER (id);
33507 if (strcmp ("always", p) == 0)
33509 int nth = 2;
33510 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33511 nth++;
33512 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33513 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33514 == RID_DELETE))
33515 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33516 == CPP_COLON))
33518 always = true;
33519 cp_lexer_consume_token (parser->lexer);
33520 if (nth == 3)
33521 cp_lexer_consume_token (parser->lexer);
33526 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33527 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33529 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33530 const char *p = IDENTIFIER_POINTER (id);
33532 if (strcmp ("alloc", p) == 0)
33533 kind = GOMP_MAP_ALLOC;
33534 else if (strcmp ("to", p) == 0)
33535 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33536 else if (strcmp ("from", p) == 0)
33537 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33538 else if (strcmp ("tofrom", p) == 0)
33539 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33540 else if (strcmp ("release", p) == 0)
33541 kind = GOMP_MAP_RELEASE;
33542 else
33544 cp_parser_error (parser, "invalid map kind");
33545 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33546 /*or_comma=*/false,
33547 /*consume_paren=*/true);
33548 return list;
33550 cp_lexer_consume_token (parser->lexer);
33551 cp_lexer_consume_token (parser->lexer);
33553 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33554 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33556 kind = GOMP_MAP_DELETE;
33557 cp_lexer_consume_token (parser->lexer);
33558 cp_lexer_consume_token (parser->lexer);
33561 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33562 NULL);
33564 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33565 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33567 return nlist;
33570 /* OpenMP 4.0:
33571 device ( expression ) */
33573 static tree
33574 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33575 location_t location)
33577 tree t, c;
33579 matching_parens parens;
33580 if (!parens.require_open (parser))
33581 return list;
33583 t = cp_parser_expression (parser);
33585 if (t == error_mark_node
33586 || !parens.require_close (parser))
33587 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33588 /*or_comma=*/false,
33589 /*consume_paren=*/true);
33591 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33592 "device", location);
33594 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33595 OMP_CLAUSE_DEVICE_ID (c) = t;
33596 OMP_CLAUSE_CHAIN (c) = list;
33598 return c;
33601 /* OpenMP 4.0:
33602 dist_schedule ( static )
33603 dist_schedule ( static , expression ) */
33605 static tree
33606 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33607 location_t location)
33609 tree c, t;
33611 matching_parens parens;
33612 if (!parens.require_open (parser))
33613 return list;
33615 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33617 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33618 goto invalid_kind;
33619 cp_lexer_consume_token (parser->lexer);
33621 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33623 cp_lexer_consume_token (parser->lexer);
33625 t = cp_parser_assignment_expression (parser);
33627 if (t == error_mark_node)
33628 goto resync_fail;
33629 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33631 if (!parens.require_close (parser))
33632 goto resync_fail;
33634 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33635 goto resync_fail;
33637 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33638 location);
33639 OMP_CLAUSE_CHAIN (c) = list;
33640 return c;
33642 invalid_kind:
33643 cp_parser_error (parser, "invalid dist_schedule kind");
33644 resync_fail:
33645 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33646 /*or_comma=*/false,
33647 /*consume_paren=*/true);
33648 return list;
33651 /* OpenMP 4.0:
33652 proc_bind ( proc-bind-kind )
33654 proc-bind-kind:
33655 master | close | spread */
33657 static tree
33658 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33659 location_t location)
33661 tree c;
33662 enum omp_clause_proc_bind_kind kind;
33664 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33665 return list;
33667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33669 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33670 const char *p = IDENTIFIER_POINTER (id);
33672 if (strcmp ("master", p) == 0)
33673 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33674 else if (strcmp ("close", p) == 0)
33675 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33676 else if (strcmp ("spread", p) == 0)
33677 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33678 else
33679 goto invalid_kind;
33681 else
33682 goto invalid_kind;
33684 cp_lexer_consume_token (parser->lexer);
33685 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33686 goto resync_fail;
33688 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33689 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33690 location);
33691 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33692 OMP_CLAUSE_CHAIN (c) = list;
33693 return c;
33695 invalid_kind:
33696 cp_parser_error (parser, "invalid depend kind");
33697 resync_fail:
33698 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33699 /*or_comma=*/false,
33700 /*consume_paren=*/true);
33701 return list;
33704 /* OpenACC:
33705 async [( int-expr )] */
33707 static tree
33708 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33710 tree c, t;
33711 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33713 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33715 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33717 matching_parens parens;
33718 parens.consume_open (parser);
33720 t = cp_parser_expression (parser);
33721 if (t == error_mark_node
33722 || !parens.require_close (parser))
33723 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33724 /*or_comma=*/false,
33725 /*consume_paren=*/true);
33728 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33730 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33731 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33732 OMP_CLAUSE_CHAIN (c) = list;
33733 list = c;
33735 return list;
33738 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33739 is a bitmask in MASK. Return the list of clauses found. */
33741 static tree
33742 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33743 const char *where, cp_token *pragma_tok,
33744 bool finish_p = true)
33746 tree clauses = NULL;
33747 bool first = true;
33749 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33751 location_t here;
33752 pragma_omp_clause c_kind;
33753 omp_clause_code code;
33754 const char *c_name;
33755 tree prev = clauses;
33757 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33758 cp_lexer_consume_token (parser->lexer);
33760 here = cp_lexer_peek_token (parser->lexer)->location;
33761 c_kind = cp_parser_omp_clause_name (parser);
33763 switch (c_kind)
33765 case PRAGMA_OACC_CLAUSE_ASYNC:
33766 clauses = cp_parser_oacc_clause_async (parser, clauses);
33767 c_name = "async";
33768 break;
33769 case PRAGMA_OACC_CLAUSE_AUTO:
33770 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33771 clauses, here);
33772 c_name = "auto";
33773 break;
33774 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33775 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33776 c_name = "collapse";
33777 break;
33778 case PRAGMA_OACC_CLAUSE_COPY:
33779 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33780 c_name = "copy";
33781 break;
33782 case PRAGMA_OACC_CLAUSE_COPYIN:
33783 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33784 c_name = "copyin";
33785 break;
33786 case PRAGMA_OACC_CLAUSE_COPYOUT:
33787 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33788 c_name = "copyout";
33789 break;
33790 case PRAGMA_OACC_CLAUSE_CREATE:
33791 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33792 c_name = "create";
33793 break;
33794 case PRAGMA_OACC_CLAUSE_DELETE:
33795 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33796 c_name = "delete";
33797 break;
33798 case PRAGMA_OMP_CLAUSE_DEFAULT:
33799 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33800 c_name = "default";
33801 break;
33802 case PRAGMA_OACC_CLAUSE_DEVICE:
33803 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33804 c_name = "device";
33805 break;
33806 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33807 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33808 c_name = "deviceptr";
33809 break;
33810 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33811 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33812 c_name = "device_resident";
33813 break;
33814 case PRAGMA_OACC_CLAUSE_FINALIZE:
33815 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
33816 clauses, here);
33817 c_name = "finalize";
33818 break;
33819 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33820 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33821 clauses);
33822 c_name = "firstprivate";
33823 break;
33824 case PRAGMA_OACC_CLAUSE_GANG:
33825 c_name = "gang";
33826 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33827 c_name, clauses);
33828 break;
33829 case PRAGMA_OACC_CLAUSE_HOST:
33830 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33831 c_name = "host";
33832 break;
33833 case PRAGMA_OACC_CLAUSE_IF:
33834 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33835 c_name = "if";
33836 break;
33837 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
33838 clauses = cp_parser_oacc_simple_clause (parser,
33839 OMP_CLAUSE_IF_PRESENT,
33840 clauses, here);
33841 c_name = "if_present";
33842 break;
33843 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33844 clauses = cp_parser_oacc_simple_clause (parser,
33845 OMP_CLAUSE_INDEPENDENT,
33846 clauses, here);
33847 c_name = "independent";
33848 break;
33849 case PRAGMA_OACC_CLAUSE_LINK:
33850 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33851 c_name = "link";
33852 break;
33853 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33854 code = OMP_CLAUSE_NUM_GANGS;
33855 c_name = "num_gangs";
33856 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33857 clauses);
33858 break;
33859 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33860 c_name = "num_workers";
33861 code = OMP_CLAUSE_NUM_WORKERS;
33862 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33863 clauses);
33864 break;
33865 case PRAGMA_OACC_CLAUSE_PRESENT:
33866 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33867 c_name = "present";
33868 break;
33869 case PRAGMA_OACC_CLAUSE_PRIVATE:
33870 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33871 clauses);
33872 c_name = "private";
33873 break;
33874 case PRAGMA_OACC_CLAUSE_REDUCTION:
33875 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33876 c_name = "reduction";
33877 break;
33878 case PRAGMA_OACC_CLAUSE_SEQ:
33879 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33880 clauses, here);
33881 c_name = "seq";
33882 break;
33883 case PRAGMA_OACC_CLAUSE_TILE:
33884 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33885 c_name = "tile";
33886 break;
33887 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33888 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33889 clauses);
33890 c_name = "use_device";
33891 break;
33892 case PRAGMA_OACC_CLAUSE_VECTOR:
33893 c_name = "vector";
33894 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33895 c_name, clauses);
33896 break;
33897 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33898 c_name = "vector_length";
33899 code = OMP_CLAUSE_VECTOR_LENGTH;
33900 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33901 clauses);
33902 break;
33903 case PRAGMA_OACC_CLAUSE_WAIT:
33904 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33905 c_name = "wait";
33906 break;
33907 case PRAGMA_OACC_CLAUSE_WORKER:
33908 c_name = "worker";
33909 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33910 c_name, clauses);
33911 break;
33912 default:
33913 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33914 goto saw_error;
33917 first = false;
33919 if (((mask >> c_kind) & 1) == 0)
33921 /* Remove the invalid clause(s) from the list to avoid
33922 confusing the rest of the compiler. */
33923 clauses = prev;
33924 error_at (here, "%qs is not valid for %qs", c_name, where);
33928 saw_error:
33929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33931 if (finish_p)
33932 return finish_omp_clauses (clauses, C_ORT_ACC);
33934 return clauses;
33937 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33938 is a bitmask in MASK. Return the list of clauses found; the result
33939 of clause default goes in *pdefault. */
33941 static tree
33942 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33943 const char *where, cp_token *pragma_tok,
33944 bool finish_p = true)
33946 tree clauses = NULL;
33947 bool first = true;
33948 cp_token *token = NULL;
33950 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33952 pragma_omp_clause c_kind;
33953 const char *c_name;
33954 tree prev = clauses;
33956 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33957 cp_lexer_consume_token (parser->lexer);
33959 token = cp_lexer_peek_token (parser->lexer);
33960 c_kind = cp_parser_omp_clause_name (parser);
33962 switch (c_kind)
33964 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33965 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33966 token->location);
33967 c_name = "collapse";
33968 break;
33969 case PRAGMA_OMP_CLAUSE_COPYIN:
33970 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33971 c_name = "copyin";
33972 break;
33973 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33974 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33975 clauses);
33976 c_name = "copyprivate";
33977 break;
33978 case PRAGMA_OMP_CLAUSE_DEFAULT:
33979 clauses = cp_parser_omp_clause_default (parser, clauses,
33980 token->location, false);
33981 c_name = "default";
33982 break;
33983 case PRAGMA_OMP_CLAUSE_FINAL:
33984 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33985 c_name = "final";
33986 break;
33987 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33988 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33989 clauses);
33990 c_name = "firstprivate";
33991 break;
33992 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33993 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33994 token->location);
33995 c_name = "grainsize";
33996 break;
33997 case PRAGMA_OMP_CLAUSE_HINT:
33998 clauses = cp_parser_omp_clause_hint (parser, clauses,
33999 token->location);
34000 c_name = "hint";
34001 break;
34002 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34003 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34004 token->location);
34005 c_name = "defaultmap";
34006 break;
34007 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34008 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34009 clauses);
34010 c_name = "use_device_ptr";
34011 break;
34012 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34013 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34014 clauses);
34015 c_name = "is_device_ptr";
34016 break;
34017 case PRAGMA_OMP_CLAUSE_IF:
34018 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34019 true);
34020 c_name = "if";
34021 break;
34022 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34023 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34024 clauses);
34025 c_name = "lastprivate";
34026 break;
34027 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34028 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34029 token->location);
34030 c_name = "mergeable";
34031 break;
34032 case PRAGMA_OMP_CLAUSE_NOWAIT:
34033 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34034 c_name = "nowait";
34035 break;
34036 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34037 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34038 token->location);
34039 c_name = "num_tasks";
34040 break;
34041 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34042 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34043 token->location);
34044 c_name = "num_threads";
34045 break;
34046 case PRAGMA_OMP_CLAUSE_ORDERED:
34047 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34048 token->location);
34049 c_name = "ordered";
34050 break;
34051 case PRAGMA_OMP_CLAUSE_PRIORITY:
34052 clauses = cp_parser_omp_clause_priority (parser, clauses,
34053 token->location);
34054 c_name = "priority";
34055 break;
34056 case PRAGMA_OMP_CLAUSE_PRIVATE:
34057 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34058 clauses);
34059 c_name = "private";
34060 break;
34061 case PRAGMA_OMP_CLAUSE_REDUCTION:
34062 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34063 c_name = "reduction";
34064 break;
34065 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34066 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34067 token->location);
34068 c_name = "schedule";
34069 break;
34070 case PRAGMA_OMP_CLAUSE_SHARED:
34071 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34072 clauses);
34073 c_name = "shared";
34074 break;
34075 case PRAGMA_OMP_CLAUSE_UNTIED:
34076 clauses = cp_parser_omp_clause_untied (parser, clauses,
34077 token->location);
34078 c_name = "untied";
34079 break;
34080 case PRAGMA_OMP_CLAUSE_INBRANCH:
34081 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34082 clauses, token->location);
34083 c_name = "inbranch";
34084 break;
34085 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34086 clauses = cp_parser_omp_clause_branch (parser,
34087 OMP_CLAUSE_NOTINBRANCH,
34088 clauses, token->location);
34089 c_name = "notinbranch";
34090 break;
34091 case PRAGMA_OMP_CLAUSE_PARALLEL:
34092 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34093 clauses, token->location);
34094 c_name = "parallel";
34095 if (!first)
34097 clause_not_first:
34098 error_at (token->location, "%qs must be the first clause of %qs",
34099 c_name, where);
34100 clauses = prev;
34102 break;
34103 case PRAGMA_OMP_CLAUSE_FOR:
34104 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34105 clauses, token->location);
34106 c_name = "for";
34107 if (!first)
34108 goto clause_not_first;
34109 break;
34110 case PRAGMA_OMP_CLAUSE_SECTIONS:
34111 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34112 clauses, token->location);
34113 c_name = "sections";
34114 if (!first)
34115 goto clause_not_first;
34116 break;
34117 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34118 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34119 clauses, token->location);
34120 c_name = "taskgroup";
34121 if (!first)
34122 goto clause_not_first;
34123 break;
34124 case PRAGMA_OMP_CLAUSE_LINK:
34125 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34126 c_name = "to";
34127 break;
34128 case PRAGMA_OMP_CLAUSE_TO:
34129 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34130 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34131 clauses);
34132 else
34133 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34134 c_name = "to";
34135 break;
34136 case PRAGMA_OMP_CLAUSE_FROM:
34137 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34138 c_name = "from";
34139 break;
34140 case PRAGMA_OMP_CLAUSE_UNIFORM:
34141 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34142 clauses);
34143 c_name = "uniform";
34144 break;
34145 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34146 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34147 token->location);
34148 c_name = "num_teams";
34149 break;
34150 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34151 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34152 token->location);
34153 c_name = "thread_limit";
34154 break;
34155 case PRAGMA_OMP_CLAUSE_ALIGNED:
34156 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34157 c_name = "aligned";
34158 break;
34159 case PRAGMA_OMP_CLAUSE_LINEAR:
34161 bool declare_simd = false;
34162 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34163 declare_simd = true;
34164 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34166 c_name = "linear";
34167 break;
34168 case PRAGMA_OMP_CLAUSE_DEPEND:
34169 clauses = cp_parser_omp_clause_depend (parser, clauses,
34170 token->location);
34171 c_name = "depend";
34172 break;
34173 case PRAGMA_OMP_CLAUSE_MAP:
34174 clauses = cp_parser_omp_clause_map (parser, clauses);
34175 c_name = "map";
34176 break;
34177 case PRAGMA_OMP_CLAUSE_DEVICE:
34178 clauses = cp_parser_omp_clause_device (parser, clauses,
34179 token->location);
34180 c_name = "device";
34181 break;
34182 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34183 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34184 token->location);
34185 c_name = "dist_schedule";
34186 break;
34187 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34188 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34189 token->location);
34190 c_name = "proc_bind";
34191 break;
34192 case PRAGMA_OMP_CLAUSE_SAFELEN:
34193 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34194 token->location);
34195 c_name = "safelen";
34196 break;
34197 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34198 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34199 token->location);
34200 c_name = "simdlen";
34201 break;
34202 case PRAGMA_OMP_CLAUSE_NOGROUP:
34203 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34204 token->location);
34205 c_name = "nogroup";
34206 break;
34207 case PRAGMA_OMP_CLAUSE_THREADS:
34208 clauses
34209 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34210 clauses, token->location);
34211 c_name = "threads";
34212 break;
34213 case PRAGMA_OMP_CLAUSE_SIMD:
34214 clauses
34215 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34216 clauses, token->location);
34217 c_name = "simd";
34218 break;
34219 default:
34220 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34221 goto saw_error;
34224 first = false;
34226 if (((mask >> c_kind) & 1) == 0)
34228 /* Remove the invalid clause(s) from the list to avoid
34229 confusing the rest of the compiler. */
34230 clauses = prev;
34231 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34234 saw_error:
34235 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34236 if (finish_p)
34238 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34239 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34240 else
34241 return finish_omp_clauses (clauses, C_ORT_OMP);
34243 return clauses;
34246 /* OpenMP 2.5:
34247 structured-block:
34248 statement
34250 In practice, we're also interested in adding the statement to an
34251 outer node. So it is convenient if we work around the fact that
34252 cp_parser_statement calls add_stmt. */
34254 static unsigned
34255 cp_parser_begin_omp_structured_block (cp_parser *parser)
34257 unsigned save = parser->in_statement;
34259 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34260 This preserves the "not within loop or switch" style error messages
34261 for nonsense cases like
34262 void foo() {
34263 #pragma omp single
34264 break;
34267 if (parser->in_statement)
34268 parser->in_statement = IN_OMP_BLOCK;
34270 return save;
34273 static void
34274 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34276 parser->in_statement = save;
34279 static tree
34280 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34282 tree stmt = begin_omp_structured_block ();
34283 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34285 cp_parser_statement (parser, NULL_TREE, false, if_p);
34287 cp_parser_end_omp_structured_block (parser, save);
34288 return finish_omp_structured_block (stmt);
34291 /* OpenMP 2.5:
34292 # pragma omp atomic new-line
34293 expression-stmt
34295 expression-stmt:
34296 x binop= expr | x++ | ++x | x-- | --x
34297 binop:
34298 +, *, -, /, &, ^, |, <<, >>
34300 where x is an lvalue expression with scalar type.
34302 OpenMP 3.1:
34303 # pragma omp atomic new-line
34304 update-stmt
34306 # pragma omp atomic read new-line
34307 read-stmt
34309 # pragma omp atomic write new-line
34310 write-stmt
34312 # pragma omp atomic update new-line
34313 update-stmt
34315 # pragma omp atomic capture new-line
34316 capture-stmt
34318 # pragma omp atomic capture new-line
34319 capture-block
34321 read-stmt:
34322 v = x
34323 write-stmt:
34324 x = expr
34325 update-stmt:
34326 expression-stmt | x = x binop expr
34327 capture-stmt:
34328 v = expression-stmt
34329 capture-block:
34330 { v = x; update-stmt; } | { update-stmt; v = x; }
34332 OpenMP 4.0:
34333 update-stmt:
34334 expression-stmt | x = x binop expr | x = expr binop x
34335 capture-stmt:
34336 v = update-stmt
34337 capture-block:
34338 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34340 where x and v are lvalue expressions with scalar type. */
34342 static void
34343 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34345 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34346 tree rhs1 = NULL_TREE, orig_lhs;
34347 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34348 bool structured_block = false;
34349 bool seq_cst = false;
34351 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34353 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34354 const char *p = IDENTIFIER_POINTER (id);
34356 if (!strcmp (p, "seq_cst"))
34358 seq_cst = true;
34359 cp_lexer_consume_token (parser->lexer);
34360 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34361 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34362 cp_lexer_consume_token (parser->lexer);
34365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34367 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34368 const char *p = IDENTIFIER_POINTER (id);
34370 if (!strcmp (p, "read"))
34371 code = OMP_ATOMIC_READ;
34372 else if (!strcmp (p, "write"))
34373 code = NOP_EXPR;
34374 else if (!strcmp (p, "update"))
34375 code = OMP_ATOMIC;
34376 else if (!strcmp (p, "capture"))
34377 code = OMP_ATOMIC_CAPTURE_NEW;
34378 else
34379 p = NULL;
34380 if (p)
34381 cp_lexer_consume_token (parser->lexer);
34383 if (!seq_cst)
34385 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34386 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34387 cp_lexer_consume_token (parser->lexer);
34389 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34391 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34392 const char *p = IDENTIFIER_POINTER (id);
34394 if (!strcmp (p, "seq_cst"))
34396 seq_cst = true;
34397 cp_lexer_consume_token (parser->lexer);
34401 cp_parser_require_pragma_eol (parser, pragma_tok);
34403 switch (code)
34405 case OMP_ATOMIC_READ:
34406 case NOP_EXPR: /* atomic write */
34407 v = cp_parser_unary_expression (parser);
34408 if (v == error_mark_node)
34409 goto saw_error;
34410 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34411 goto saw_error;
34412 if (code == NOP_EXPR)
34413 lhs = cp_parser_expression (parser);
34414 else
34415 lhs = cp_parser_unary_expression (parser);
34416 if (lhs == error_mark_node)
34417 goto saw_error;
34418 if (code == NOP_EXPR)
34420 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34421 opcode. */
34422 code = OMP_ATOMIC;
34423 rhs = lhs;
34424 lhs = v;
34425 v = NULL_TREE;
34427 goto done;
34428 case OMP_ATOMIC_CAPTURE_NEW:
34429 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34431 cp_lexer_consume_token (parser->lexer);
34432 structured_block = true;
34434 else
34436 v = cp_parser_unary_expression (parser);
34437 if (v == error_mark_node)
34438 goto saw_error;
34439 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34440 goto saw_error;
34442 default:
34443 break;
34446 restart:
34447 lhs = cp_parser_unary_expression (parser);
34448 orig_lhs = lhs;
34449 switch (TREE_CODE (lhs))
34451 case ERROR_MARK:
34452 goto saw_error;
34454 case POSTINCREMENT_EXPR:
34455 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34456 code = OMP_ATOMIC_CAPTURE_OLD;
34457 /* FALLTHROUGH */
34458 case PREINCREMENT_EXPR:
34459 lhs = TREE_OPERAND (lhs, 0);
34460 opcode = PLUS_EXPR;
34461 rhs = integer_one_node;
34462 break;
34464 case POSTDECREMENT_EXPR:
34465 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34466 code = OMP_ATOMIC_CAPTURE_OLD;
34467 /* FALLTHROUGH */
34468 case PREDECREMENT_EXPR:
34469 lhs = TREE_OPERAND (lhs, 0);
34470 opcode = MINUS_EXPR;
34471 rhs = integer_one_node;
34472 break;
34474 case COMPOUND_EXPR:
34475 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34476 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34477 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34478 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34479 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34480 (TREE_OPERAND (lhs, 1), 0), 0)))
34481 == BOOLEAN_TYPE)
34482 /* Undo effects of boolean_increment for post {in,de}crement. */
34483 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34484 /* FALLTHRU */
34485 case MODIFY_EXPR:
34486 if (TREE_CODE (lhs) == MODIFY_EXPR
34487 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34489 /* Undo effects of boolean_increment. */
34490 if (integer_onep (TREE_OPERAND (lhs, 1)))
34492 /* This is pre or post increment. */
34493 rhs = TREE_OPERAND (lhs, 1);
34494 lhs = TREE_OPERAND (lhs, 0);
34495 opcode = NOP_EXPR;
34496 if (code == OMP_ATOMIC_CAPTURE_NEW
34497 && !structured_block
34498 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34499 code = OMP_ATOMIC_CAPTURE_OLD;
34500 break;
34503 /* FALLTHRU */
34504 default:
34505 switch (cp_lexer_peek_token (parser->lexer)->type)
34507 case CPP_MULT_EQ:
34508 opcode = MULT_EXPR;
34509 break;
34510 case CPP_DIV_EQ:
34511 opcode = TRUNC_DIV_EXPR;
34512 break;
34513 case CPP_PLUS_EQ:
34514 opcode = PLUS_EXPR;
34515 break;
34516 case CPP_MINUS_EQ:
34517 opcode = MINUS_EXPR;
34518 break;
34519 case CPP_LSHIFT_EQ:
34520 opcode = LSHIFT_EXPR;
34521 break;
34522 case CPP_RSHIFT_EQ:
34523 opcode = RSHIFT_EXPR;
34524 break;
34525 case CPP_AND_EQ:
34526 opcode = BIT_AND_EXPR;
34527 break;
34528 case CPP_OR_EQ:
34529 opcode = BIT_IOR_EXPR;
34530 break;
34531 case CPP_XOR_EQ:
34532 opcode = BIT_XOR_EXPR;
34533 break;
34534 case CPP_EQ:
34535 enum cp_parser_prec oprec;
34536 cp_token *token;
34537 cp_lexer_consume_token (parser->lexer);
34538 cp_parser_parse_tentatively (parser);
34539 rhs1 = cp_parser_simple_cast_expression (parser);
34540 if (rhs1 == error_mark_node)
34542 cp_parser_abort_tentative_parse (parser);
34543 cp_parser_simple_cast_expression (parser);
34544 goto saw_error;
34546 token = cp_lexer_peek_token (parser->lexer);
34547 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34549 cp_parser_abort_tentative_parse (parser);
34550 cp_parser_parse_tentatively (parser);
34551 rhs = cp_parser_binary_expression (parser, false, true,
34552 PREC_NOT_OPERATOR, NULL);
34553 if (rhs == error_mark_node)
34555 cp_parser_abort_tentative_parse (parser);
34556 cp_parser_binary_expression (parser, false, true,
34557 PREC_NOT_OPERATOR, NULL);
34558 goto saw_error;
34560 switch (TREE_CODE (rhs))
34562 case MULT_EXPR:
34563 case TRUNC_DIV_EXPR:
34564 case RDIV_EXPR:
34565 case PLUS_EXPR:
34566 case MINUS_EXPR:
34567 case LSHIFT_EXPR:
34568 case RSHIFT_EXPR:
34569 case BIT_AND_EXPR:
34570 case BIT_IOR_EXPR:
34571 case BIT_XOR_EXPR:
34572 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34574 if (cp_parser_parse_definitely (parser))
34576 opcode = TREE_CODE (rhs);
34577 rhs1 = TREE_OPERAND (rhs, 0);
34578 rhs = TREE_OPERAND (rhs, 1);
34579 goto stmt_done;
34581 else
34582 goto saw_error;
34584 break;
34585 default:
34586 break;
34588 cp_parser_abort_tentative_parse (parser);
34589 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34591 rhs = cp_parser_expression (parser);
34592 if (rhs == error_mark_node)
34593 goto saw_error;
34594 opcode = NOP_EXPR;
34595 rhs1 = NULL_TREE;
34596 goto stmt_done;
34598 cp_parser_error (parser,
34599 "invalid form of %<#pragma omp atomic%>");
34600 goto saw_error;
34602 if (!cp_parser_parse_definitely (parser))
34603 goto saw_error;
34604 switch (token->type)
34606 case CPP_SEMICOLON:
34607 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34609 code = OMP_ATOMIC_CAPTURE_OLD;
34610 v = lhs;
34611 lhs = NULL_TREE;
34612 lhs1 = rhs1;
34613 rhs1 = NULL_TREE;
34614 cp_lexer_consume_token (parser->lexer);
34615 goto restart;
34617 else if (structured_block)
34619 opcode = NOP_EXPR;
34620 rhs = rhs1;
34621 rhs1 = NULL_TREE;
34622 goto stmt_done;
34624 cp_parser_error (parser,
34625 "invalid form of %<#pragma omp atomic%>");
34626 goto saw_error;
34627 case CPP_MULT:
34628 opcode = MULT_EXPR;
34629 break;
34630 case CPP_DIV:
34631 opcode = TRUNC_DIV_EXPR;
34632 break;
34633 case CPP_PLUS:
34634 opcode = PLUS_EXPR;
34635 break;
34636 case CPP_MINUS:
34637 opcode = MINUS_EXPR;
34638 break;
34639 case CPP_LSHIFT:
34640 opcode = LSHIFT_EXPR;
34641 break;
34642 case CPP_RSHIFT:
34643 opcode = RSHIFT_EXPR;
34644 break;
34645 case CPP_AND:
34646 opcode = BIT_AND_EXPR;
34647 break;
34648 case CPP_OR:
34649 opcode = BIT_IOR_EXPR;
34650 break;
34651 case CPP_XOR:
34652 opcode = BIT_XOR_EXPR;
34653 break;
34654 default:
34655 cp_parser_error (parser,
34656 "invalid operator for %<#pragma omp atomic%>");
34657 goto saw_error;
34659 oprec = TOKEN_PRECEDENCE (token);
34660 gcc_assert (oprec != PREC_NOT_OPERATOR);
34661 if (commutative_tree_code (opcode))
34662 oprec = (enum cp_parser_prec) (oprec - 1);
34663 cp_lexer_consume_token (parser->lexer);
34664 rhs = cp_parser_binary_expression (parser, false, false,
34665 oprec, NULL);
34666 if (rhs == error_mark_node)
34667 goto saw_error;
34668 goto stmt_done;
34669 /* FALLTHROUGH */
34670 default:
34671 cp_parser_error (parser,
34672 "invalid operator for %<#pragma omp atomic%>");
34673 goto saw_error;
34675 cp_lexer_consume_token (parser->lexer);
34677 rhs = cp_parser_expression (parser);
34678 if (rhs == error_mark_node)
34679 goto saw_error;
34680 break;
34682 stmt_done:
34683 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34685 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34686 goto saw_error;
34687 v = cp_parser_unary_expression (parser);
34688 if (v == error_mark_node)
34689 goto saw_error;
34690 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34691 goto saw_error;
34692 lhs1 = cp_parser_unary_expression (parser);
34693 if (lhs1 == error_mark_node)
34694 goto saw_error;
34696 if (structured_block)
34698 cp_parser_consume_semicolon_at_end_of_statement (parser);
34699 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34701 done:
34702 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34703 if (!structured_block)
34704 cp_parser_consume_semicolon_at_end_of_statement (parser);
34705 return;
34707 saw_error:
34708 cp_parser_skip_to_end_of_block_or_statement (parser);
34709 if (structured_block)
34711 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34712 cp_lexer_consume_token (parser->lexer);
34713 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34715 cp_parser_skip_to_end_of_block_or_statement (parser);
34716 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34717 cp_lexer_consume_token (parser->lexer);
34723 /* OpenMP 2.5:
34724 # pragma omp barrier new-line */
34726 static void
34727 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34729 cp_parser_require_pragma_eol (parser, pragma_tok);
34730 finish_omp_barrier ();
34733 /* OpenMP 2.5:
34734 # pragma omp critical [(name)] new-line
34735 structured-block
34737 OpenMP 4.5:
34738 # pragma omp critical [(name) [hint(expression)]] new-line
34739 structured-block */
34741 #define OMP_CRITICAL_CLAUSE_MASK \
34742 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34744 static tree
34745 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34747 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34749 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34751 matching_parens parens;
34752 parens.consume_open (parser);
34754 name = cp_parser_identifier (parser);
34756 if (name == error_mark_node
34757 || !parens.require_close (parser))
34758 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34759 /*or_comma=*/false,
34760 /*consume_paren=*/true);
34761 if (name == error_mark_node)
34762 name = NULL;
34764 clauses = cp_parser_omp_all_clauses (parser,
34765 OMP_CRITICAL_CLAUSE_MASK,
34766 "#pragma omp critical", pragma_tok);
34768 else
34769 cp_parser_require_pragma_eol (parser, pragma_tok);
34771 stmt = cp_parser_omp_structured_block (parser, if_p);
34772 return c_finish_omp_critical (input_location, stmt, name, clauses);
34775 /* OpenMP 2.5:
34776 # pragma omp flush flush-vars[opt] new-line
34778 flush-vars:
34779 ( variable-list ) */
34781 static void
34782 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34785 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34786 cp_parser_require_pragma_eol (parser, pragma_tok);
34788 finish_omp_flush ();
34791 /* Helper function, to parse omp for increment expression. */
34793 static tree
34794 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34796 tree cond = cp_parser_binary_expression (parser, false, true,
34797 PREC_NOT_OPERATOR, NULL);
34798 if (cond == error_mark_node
34799 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34801 cp_parser_skip_to_end_of_statement (parser);
34802 return error_mark_node;
34805 switch (TREE_CODE (cond))
34807 case GT_EXPR:
34808 case GE_EXPR:
34809 case LT_EXPR:
34810 case LE_EXPR:
34811 break;
34812 case NE_EXPR:
34813 /* Fall through: OpenMP disallows NE_EXPR. */
34814 gcc_fallthrough ();
34815 default:
34816 return error_mark_node;
34819 /* If decl is an iterator, preserve LHS and RHS of the relational
34820 expr until finish_omp_for. */
34821 if (decl
34822 && (type_dependent_expression_p (decl)
34823 || CLASS_TYPE_P (TREE_TYPE (decl))))
34824 return cond;
34826 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
34827 TREE_CODE (cond),
34828 TREE_OPERAND (cond, 0), ERROR_MARK,
34829 TREE_OPERAND (cond, 1), ERROR_MARK,
34830 /*overload=*/NULL, tf_warning_or_error);
34833 /* Helper function, to parse omp for increment expression. */
34835 static tree
34836 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34838 cp_token *token = cp_lexer_peek_token (parser->lexer);
34839 enum tree_code op;
34840 tree lhs, rhs;
34841 cp_id_kind idk;
34842 bool decl_first;
34844 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34846 op = (token->type == CPP_PLUS_PLUS
34847 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34848 cp_lexer_consume_token (parser->lexer);
34849 lhs = cp_parser_simple_cast_expression (parser);
34850 if (lhs != decl
34851 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34852 return error_mark_node;
34853 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34856 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34857 if (lhs != decl
34858 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34859 return error_mark_node;
34861 token = cp_lexer_peek_token (parser->lexer);
34862 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34864 op = (token->type == CPP_PLUS_PLUS
34865 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34866 cp_lexer_consume_token (parser->lexer);
34867 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34870 op = cp_parser_assignment_operator_opt (parser);
34871 if (op == ERROR_MARK)
34872 return error_mark_node;
34874 if (op != NOP_EXPR)
34876 rhs = cp_parser_assignment_expression (parser);
34877 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34878 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34881 lhs = cp_parser_binary_expression (parser, false, false,
34882 PREC_ADDITIVE_EXPRESSION, NULL);
34883 token = cp_lexer_peek_token (parser->lexer);
34884 decl_first = (lhs == decl
34885 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34886 if (decl_first)
34887 lhs = NULL_TREE;
34888 if (token->type != CPP_PLUS
34889 && token->type != CPP_MINUS)
34890 return error_mark_node;
34894 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34895 cp_lexer_consume_token (parser->lexer);
34896 rhs = cp_parser_binary_expression (parser, false, false,
34897 PREC_ADDITIVE_EXPRESSION, NULL);
34898 token = cp_lexer_peek_token (parser->lexer);
34899 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34901 if (lhs == NULL_TREE)
34903 if (op == PLUS_EXPR)
34904 lhs = rhs;
34905 else
34906 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34907 tf_warning_or_error);
34909 else
34910 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34911 ERROR_MARK, NULL, tf_warning_or_error);
34914 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34916 if (!decl_first)
34918 if ((rhs != decl
34919 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34920 || op == MINUS_EXPR)
34921 return error_mark_node;
34922 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34924 else
34925 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34927 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34930 /* Parse the initialization statement of an OpenMP for loop.
34932 Return true if the resulting construct should have an
34933 OMP_CLAUSE_PRIVATE added to it. */
34935 static tree
34936 cp_parser_omp_for_loop_init (cp_parser *parser,
34937 tree &this_pre_body,
34938 vec<tree, va_gc> *&for_block,
34939 tree &init,
34940 tree &orig_init,
34941 tree &decl,
34942 tree &real_decl)
34944 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34945 return NULL_TREE;
34947 tree add_private_clause = NULL_TREE;
34949 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34951 init-expr:
34952 var = lb
34953 integer-type var = lb
34954 random-access-iterator-type var = lb
34955 pointer-type var = lb
34957 cp_decl_specifier_seq type_specifiers;
34959 /* First, try to parse as an initialized declaration. See
34960 cp_parser_condition, from whence the bulk of this is copied. */
34962 cp_parser_parse_tentatively (parser);
34963 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34964 /*is_trailing_return=*/false,
34965 &type_specifiers);
34966 if (cp_parser_parse_definitely (parser))
34968 /* If parsing a type specifier seq succeeded, then this
34969 MUST be a initialized declaration. */
34970 tree asm_specification, attributes;
34971 cp_declarator *declarator;
34973 declarator = cp_parser_declarator (parser,
34974 CP_PARSER_DECLARATOR_NAMED,
34975 /*ctor_dtor_or_conv_p=*/NULL,
34976 /*parenthesized_p=*/NULL,
34977 /*member_p=*/false,
34978 /*friend_p=*/false);
34979 attributes = cp_parser_attributes_opt (parser);
34980 asm_specification = cp_parser_asm_specification_opt (parser);
34982 if (declarator == cp_error_declarator)
34983 cp_parser_skip_to_end_of_statement (parser);
34985 else
34987 tree pushed_scope, auto_node;
34989 decl = start_decl (declarator, &type_specifiers,
34990 SD_INITIALIZED, attributes,
34991 /*prefix_attributes=*/NULL_TREE,
34992 &pushed_scope);
34994 auto_node = type_uses_auto (TREE_TYPE (decl));
34995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34997 if (cp_lexer_next_token_is (parser->lexer,
34998 CPP_OPEN_PAREN))
34999 error ("parenthesized initialization is not allowed in "
35000 "OpenMP %<for%> loop");
35001 else
35002 /* Trigger an error. */
35003 cp_parser_require (parser, CPP_EQ, RT_EQ);
35005 init = error_mark_node;
35006 cp_parser_skip_to_end_of_statement (parser);
35008 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35009 || type_dependent_expression_p (decl)
35010 || auto_node)
35012 bool is_direct_init, is_non_constant_init;
35014 init = cp_parser_initializer (parser,
35015 &is_direct_init,
35016 &is_non_constant_init);
35018 if (auto_node)
35020 TREE_TYPE (decl)
35021 = do_auto_deduction (TREE_TYPE (decl), init,
35022 auto_node);
35024 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35025 && !type_dependent_expression_p (decl))
35026 goto non_class;
35029 cp_finish_decl (decl, init, !is_non_constant_init,
35030 asm_specification,
35031 LOOKUP_ONLYCONVERTING);
35032 orig_init = init;
35033 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35035 vec_safe_push (for_block, this_pre_body);
35036 init = NULL_TREE;
35038 else
35040 init = pop_stmt_list (this_pre_body);
35041 if (init && TREE_CODE (init) == STATEMENT_LIST)
35043 tree_stmt_iterator i = tsi_start (init);
35044 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35045 while (!tsi_end_p (i))
35047 tree t = tsi_stmt (i);
35048 if (TREE_CODE (t) == DECL_EXPR
35049 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35051 tsi_delink (&i);
35052 vec_safe_push (for_block, t);
35053 continue;
35055 break;
35057 if (tsi_one_before_end_p (i))
35059 tree t = tsi_stmt (i);
35060 tsi_delink (&i);
35061 free_stmt_list (init);
35062 init = t;
35066 this_pre_body = NULL_TREE;
35068 else
35070 /* Consume '='. */
35071 cp_lexer_consume_token (parser->lexer);
35072 init = cp_parser_assignment_expression (parser);
35074 non_class:
35075 if (TYPE_REF_P (TREE_TYPE (decl)))
35076 init = error_mark_node;
35077 else
35078 cp_finish_decl (decl, NULL_TREE,
35079 /*init_const_expr_p=*/false,
35080 asm_specification,
35081 LOOKUP_ONLYCONVERTING);
35084 if (pushed_scope)
35085 pop_scope (pushed_scope);
35088 else
35090 cp_id_kind idk;
35091 /* If parsing a type specifier sequence failed, then
35092 this MUST be a simple expression. */
35093 cp_parser_parse_tentatively (parser);
35094 decl = cp_parser_primary_expression (parser, false, false,
35095 false, &idk);
35096 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35097 if (!cp_parser_error_occurred (parser)
35098 && decl
35099 && (TREE_CODE (decl) == COMPONENT_REF
35100 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35102 cp_parser_abort_tentative_parse (parser);
35103 cp_parser_parse_tentatively (parser);
35104 cp_token *token = cp_lexer_peek_token (parser->lexer);
35105 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35106 /*check_dependency_p=*/true,
35107 /*template_p=*/NULL,
35108 /*declarator_p=*/false,
35109 /*optional_p=*/false);
35110 if (name != error_mark_node
35111 && last_tok == cp_lexer_peek_token (parser->lexer))
35113 decl = cp_parser_lookup_name_simple (parser, name,
35114 token->location);
35115 if (TREE_CODE (decl) == FIELD_DECL)
35116 add_private_clause = omp_privatize_field (decl, false);
35118 cp_parser_abort_tentative_parse (parser);
35119 cp_parser_parse_tentatively (parser);
35120 decl = cp_parser_primary_expression (parser, false, false,
35121 false, &idk);
35123 if (!cp_parser_error_occurred (parser)
35124 && decl
35125 && DECL_P (decl)
35126 && CLASS_TYPE_P (TREE_TYPE (decl)))
35128 tree rhs;
35130 cp_parser_parse_definitely (parser);
35131 cp_parser_require (parser, CPP_EQ, RT_EQ);
35132 rhs = cp_parser_assignment_expression (parser);
35133 orig_init = rhs;
35134 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35135 decl, NOP_EXPR,
35136 rhs,
35137 tf_warning_or_error));
35138 if (!add_private_clause)
35139 add_private_clause = decl;
35141 else
35143 decl = NULL;
35144 cp_parser_abort_tentative_parse (parser);
35145 init = cp_parser_expression (parser);
35146 if (init)
35148 if (TREE_CODE (init) == MODIFY_EXPR
35149 || TREE_CODE (init) == MODOP_EXPR)
35150 real_decl = TREE_OPERAND (init, 0);
35154 return add_private_clause;
35157 /* Parse the restricted form of the for statement allowed by OpenMP. */
35159 static tree
35160 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35161 tree *cclauses, bool *if_p)
35163 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35164 tree real_decl, initv, condv, incrv, declv;
35165 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35166 location_t loc_first;
35167 bool collapse_err = false;
35168 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35169 vec<tree, va_gc> *for_block = make_tree_vector ();
35170 auto_vec<tree, 4> orig_inits;
35171 bool tiling = false;
35173 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35174 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35175 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35176 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35178 tiling = true;
35179 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35181 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35182 && OMP_CLAUSE_ORDERED_EXPR (cl))
35184 ordered_cl = cl;
35185 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35188 if (ordered && ordered < collapse)
35190 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35191 "%<ordered%> clause parameter is less than %<collapse%>");
35192 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35193 = build_int_cst (NULL_TREE, collapse);
35194 ordered = collapse;
35196 if (ordered)
35198 for (tree *pc = &clauses; *pc; )
35199 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35201 error_at (OMP_CLAUSE_LOCATION (*pc),
35202 "%<linear%> clause may not be specified together "
35203 "with %<ordered%> clause with a parameter");
35204 *pc = OMP_CLAUSE_CHAIN (*pc);
35206 else
35207 pc = &OMP_CLAUSE_CHAIN (*pc);
35210 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35211 count = ordered ? ordered : collapse;
35213 declv = make_tree_vec (count);
35214 initv = make_tree_vec (count);
35215 condv = make_tree_vec (count);
35216 incrv = make_tree_vec (count);
35218 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35220 for (i = 0; i < count; i++)
35222 int bracecount = 0;
35223 tree add_private_clause = NULL_TREE;
35224 location_t loc;
35226 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35228 if (!collapse_err)
35229 cp_parser_error (parser, "for statement expected");
35230 return NULL;
35232 loc = cp_lexer_consume_token (parser->lexer)->location;
35234 matching_parens parens;
35235 if (!parens.require_open (parser))
35236 return NULL;
35238 init = orig_init = decl = real_decl = NULL;
35239 this_pre_body = push_stmt_list ();
35241 add_private_clause
35242 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35243 init, orig_init, decl, real_decl);
35245 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35246 if (this_pre_body)
35248 this_pre_body = pop_stmt_list (this_pre_body);
35249 if (pre_body)
35251 tree t = pre_body;
35252 pre_body = push_stmt_list ();
35253 add_stmt (t);
35254 add_stmt (this_pre_body);
35255 pre_body = pop_stmt_list (pre_body);
35257 else
35258 pre_body = this_pre_body;
35261 if (decl)
35262 real_decl = decl;
35263 if (cclauses != NULL
35264 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35265 && real_decl != NULL_TREE)
35267 tree *c;
35268 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35269 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35270 && OMP_CLAUSE_DECL (*c) == real_decl)
35272 error_at (loc, "iteration variable %qD"
35273 " should not be firstprivate", real_decl);
35274 *c = OMP_CLAUSE_CHAIN (*c);
35276 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35277 && OMP_CLAUSE_DECL (*c) == real_decl)
35279 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35280 tree l = *c;
35281 *c = OMP_CLAUSE_CHAIN (*c);
35282 if (code == OMP_SIMD)
35284 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35285 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35287 else
35289 OMP_CLAUSE_CHAIN (l) = clauses;
35290 clauses = l;
35292 add_private_clause = NULL_TREE;
35294 else
35296 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35297 && OMP_CLAUSE_DECL (*c) == real_decl)
35298 add_private_clause = NULL_TREE;
35299 c = &OMP_CLAUSE_CHAIN (*c);
35303 if (add_private_clause)
35305 tree c;
35306 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35308 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35309 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35310 && OMP_CLAUSE_DECL (c) == decl)
35311 break;
35312 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35313 && OMP_CLAUSE_DECL (c) == decl)
35314 error_at (loc, "iteration variable %qD "
35315 "should not be firstprivate",
35316 decl);
35317 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35318 && OMP_CLAUSE_DECL (c) == decl)
35319 error_at (loc, "iteration variable %qD should not be reduction",
35320 decl);
35322 if (c == NULL)
35324 if (code != OMP_SIMD)
35325 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35326 else if (collapse == 1)
35327 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35328 else
35329 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35330 OMP_CLAUSE_DECL (c) = add_private_clause;
35331 c = finish_omp_clauses (c, C_ORT_OMP);
35332 if (c)
35334 OMP_CLAUSE_CHAIN (c) = clauses;
35335 clauses = c;
35336 /* For linear, signal that we need to fill up
35337 the so far unknown linear step. */
35338 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35339 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35344 cond = NULL;
35345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35346 cond = cp_parser_omp_for_cond (parser, decl);
35347 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35349 incr = NULL;
35350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35352 /* If decl is an iterator, preserve the operator on decl
35353 until finish_omp_for. */
35354 if (real_decl
35355 && ((processing_template_decl
35356 && (TREE_TYPE (real_decl) == NULL_TREE
35357 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35358 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35359 incr = cp_parser_omp_for_incr (parser, real_decl);
35360 else
35361 incr = cp_parser_expression (parser);
35362 if (!EXPR_HAS_LOCATION (incr))
35363 protected_set_expr_location (incr, input_location);
35366 if (!parens.require_close (parser))
35367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35368 /*or_comma=*/false,
35369 /*consume_paren=*/true);
35371 TREE_VEC_ELT (declv, i) = decl;
35372 TREE_VEC_ELT (initv, i) = init;
35373 TREE_VEC_ELT (condv, i) = cond;
35374 TREE_VEC_ELT (incrv, i) = incr;
35375 if (orig_init)
35377 orig_inits.safe_grow_cleared (i + 1);
35378 orig_inits[i] = orig_init;
35381 if (i == count - 1)
35382 break;
35384 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35385 in between the collapsed for loops to be still considered perfectly
35386 nested. Hopefully the final version clarifies this.
35387 For now handle (multiple) {'s and empty statements. */
35388 cp_parser_parse_tentatively (parser);
35389 for (;;)
35391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35392 break;
35393 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35395 cp_lexer_consume_token (parser->lexer);
35396 bracecount++;
35398 else if (bracecount
35399 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35400 cp_lexer_consume_token (parser->lexer);
35401 else
35403 loc = cp_lexer_peek_token (parser->lexer)->location;
35404 error_at (loc, "not enough for loops to collapse");
35405 collapse_err = true;
35406 cp_parser_abort_tentative_parse (parser);
35407 declv = NULL_TREE;
35408 break;
35412 if (declv)
35414 cp_parser_parse_definitely (parser);
35415 nbraces += bracecount;
35419 if (nbraces)
35420 if_p = NULL;
35422 /* Note that we saved the original contents of this flag when we entered
35423 the structured block, and so we don't need to re-save it here. */
35424 parser->in_statement = IN_OMP_FOR;
35426 /* Note that the grammar doesn't call for a structured block here,
35427 though the loop as a whole is a structured block. */
35428 body = push_stmt_list ();
35429 cp_parser_statement (parser, NULL_TREE, false, if_p);
35430 body = pop_stmt_list (body);
35432 if (declv == NULL_TREE)
35433 ret = NULL_TREE;
35434 else
35435 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35436 body, pre_body, &orig_inits, clauses);
35438 while (nbraces)
35440 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35442 cp_lexer_consume_token (parser->lexer);
35443 nbraces--;
35445 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35446 cp_lexer_consume_token (parser->lexer);
35447 else
35449 if (!collapse_err)
35451 error_at (cp_lexer_peek_token (parser->lexer)->location,
35452 "collapsed loops not perfectly nested");
35454 collapse_err = true;
35455 cp_parser_statement_seq_opt (parser, NULL);
35456 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35457 break;
35461 while (!for_block->is_empty ())
35463 tree t = for_block->pop ();
35464 if (TREE_CODE (t) == STATEMENT_LIST)
35465 add_stmt (pop_stmt_list (t));
35466 else
35467 add_stmt (t);
35469 release_tree_vector (for_block);
35471 return ret;
35474 /* Helper function for OpenMP parsing, split clauses and call
35475 finish_omp_clauses on each of the set of clauses afterwards. */
35477 static void
35478 cp_omp_split_clauses (location_t loc, enum tree_code code,
35479 omp_clause_mask mask, tree clauses, tree *cclauses)
35481 int i;
35482 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35483 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35484 if (cclauses[i])
35485 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35488 /* OpenMP 4.0:
35489 #pragma omp simd simd-clause[optseq] new-line
35490 for-loop */
35492 #define OMP_SIMD_CLAUSE_MASK \
35493 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35502 static tree
35503 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35504 char *p_name, omp_clause_mask mask, tree *cclauses,
35505 bool *if_p)
35507 tree clauses, sb, ret;
35508 unsigned int save;
35509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35511 strcat (p_name, " simd");
35512 mask |= OMP_SIMD_CLAUSE_MASK;
35514 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35515 cclauses == NULL);
35516 if (cclauses)
35518 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35519 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35520 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35521 OMP_CLAUSE_ORDERED);
35522 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35524 error_at (OMP_CLAUSE_LOCATION (c),
35525 "%<ordered%> clause with parameter may not be specified "
35526 "on %qs construct", p_name);
35527 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35531 sb = begin_omp_structured_block ();
35532 save = cp_parser_begin_omp_structured_block (parser);
35534 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35536 cp_parser_end_omp_structured_block (parser, save);
35537 add_stmt (finish_omp_structured_block (sb));
35539 return ret;
35542 /* OpenMP 2.5:
35543 #pragma omp for for-clause[optseq] new-line
35544 for-loop
35546 OpenMP 4.0:
35547 #pragma omp for simd for-simd-clause[optseq] new-line
35548 for-loop */
35550 #define OMP_FOR_CLAUSE_MASK \
35551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35561 static tree
35562 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35563 char *p_name, omp_clause_mask mask, tree *cclauses,
35564 bool *if_p)
35566 tree clauses, sb, ret;
35567 unsigned int save;
35568 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35570 strcat (p_name, " for");
35571 mask |= OMP_FOR_CLAUSE_MASK;
35572 /* parallel for{, simd} disallows nowait clause, but for
35573 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35574 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35575 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35576 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35577 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35578 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35580 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35582 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35583 const char *p = IDENTIFIER_POINTER (id);
35585 if (strcmp (p, "simd") == 0)
35587 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35588 if (cclauses == NULL)
35589 cclauses = cclauses_buf;
35591 cp_lexer_consume_token (parser->lexer);
35592 if (!flag_openmp) /* flag_openmp_simd */
35593 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35594 cclauses, if_p);
35595 sb = begin_omp_structured_block ();
35596 save = cp_parser_begin_omp_structured_block (parser);
35597 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35598 cclauses, if_p);
35599 cp_parser_end_omp_structured_block (parser, save);
35600 tree body = finish_omp_structured_block (sb);
35601 if (ret == NULL)
35602 return ret;
35603 ret = make_node (OMP_FOR);
35604 TREE_TYPE (ret) = void_type_node;
35605 OMP_FOR_BODY (ret) = body;
35606 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35607 SET_EXPR_LOCATION (ret, loc);
35608 add_stmt (ret);
35609 return ret;
35612 if (!flag_openmp) /* flag_openmp_simd */
35614 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35615 return NULL_TREE;
35618 /* Composite distribute parallel for disallows linear clause. */
35619 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35620 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35622 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35623 cclauses == NULL);
35624 if (cclauses)
35626 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35627 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35630 sb = begin_omp_structured_block ();
35631 save = cp_parser_begin_omp_structured_block (parser);
35633 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35635 cp_parser_end_omp_structured_block (parser, save);
35636 add_stmt (finish_omp_structured_block (sb));
35638 return ret;
35641 /* OpenMP 2.5:
35642 # pragma omp master new-line
35643 structured-block */
35645 static tree
35646 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35648 cp_parser_require_pragma_eol (parser, pragma_tok);
35649 return c_finish_omp_master (input_location,
35650 cp_parser_omp_structured_block (parser, if_p));
35653 /* OpenMP 2.5:
35654 # pragma omp ordered new-line
35655 structured-block
35657 OpenMP 4.5:
35658 # pragma omp ordered ordered-clauses new-line
35659 structured-block */
35661 #define OMP_ORDERED_CLAUSE_MASK \
35662 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35665 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35666 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35668 static bool
35669 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35670 enum pragma_context context, bool *if_p)
35672 location_t loc = pragma_tok->location;
35674 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35676 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35677 const char *p = IDENTIFIER_POINTER (id);
35679 if (strcmp (p, "depend") == 0)
35681 if (!flag_openmp) /* flag_openmp_simd */
35683 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35684 return false;
35686 if (context == pragma_stmt)
35688 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35689 "%<depend%> clause may only be used in compound "
35690 "statements");
35691 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35692 return false;
35694 tree clauses
35695 = cp_parser_omp_all_clauses (parser,
35696 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35697 "#pragma omp ordered", pragma_tok);
35698 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35699 return false;
35703 tree clauses
35704 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35705 "#pragma omp ordered", pragma_tok);
35707 if (!flag_openmp /* flag_openmp_simd */
35708 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35709 return false;
35711 c_finish_omp_ordered (loc, clauses,
35712 cp_parser_omp_structured_block (parser, if_p));
35713 return true;
35716 /* OpenMP 2.5:
35718 section-scope:
35719 { section-sequence }
35721 section-sequence:
35722 section-directive[opt] structured-block
35723 section-sequence section-directive structured-block */
35725 static tree
35726 cp_parser_omp_sections_scope (cp_parser *parser)
35728 tree stmt, substmt;
35729 bool error_suppress = false;
35730 cp_token *tok;
35732 matching_braces braces;
35733 if (!braces.require_open (parser))
35734 return NULL_TREE;
35736 stmt = push_stmt_list ();
35738 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35739 != PRAGMA_OMP_SECTION)
35741 substmt = cp_parser_omp_structured_block (parser, NULL);
35742 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35743 add_stmt (substmt);
35746 while (1)
35748 tok = cp_lexer_peek_token (parser->lexer);
35749 if (tok->type == CPP_CLOSE_BRACE)
35750 break;
35751 if (tok->type == CPP_EOF)
35752 break;
35754 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35756 cp_lexer_consume_token (parser->lexer);
35757 cp_parser_require_pragma_eol (parser, tok);
35758 error_suppress = false;
35760 else if (!error_suppress)
35762 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35763 error_suppress = true;
35766 substmt = cp_parser_omp_structured_block (parser, NULL);
35767 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35768 add_stmt (substmt);
35770 braces.require_close (parser);
35772 substmt = pop_stmt_list (stmt);
35774 stmt = make_node (OMP_SECTIONS);
35775 TREE_TYPE (stmt) = void_type_node;
35776 OMP_SECTIONS_BODY (stmt) = substmt;
35778 add_stmt (stmt);
35779 return stmt;
35782 /* OpenMP 2.5:
35783 # pragma omp sections sections-clause[optseq] newline
35784 sections-scope */
35786 #define OMP_SECTIONS_CLAUSE_MASK \
35787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35793 static tree
35794 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35795 char *p_name, omp_clause_mask mask, tree *cclauses)
35797 tree clauses, ret;
35798 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35800 strcat (p_name, " sections");
35801 mask |= OMP_SECTIONS_CLAUSE_MASK;
35802 if (cclauses)
35803 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35805 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35806 cclauses == NULL);
35807 if (cclauses)
35809 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35810 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35813 ret = cp_parser_omp_sections_scope (parser);
35814 if (ret)
35815 OMP_SECTIONS_CLAUSES (ret) = clauses;
35817 return ret;
35820 /* OpenMP 2.5:
35821 # pragma omp parallel parallel-clause[optseq] new-line
35822 structured-block
35823 # pragma omp parallel for parallel-for-clause[optseq] new-line
35824 structured-block
35825 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35826 structured-block
35828 OpenMP 4.0:
35829 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35830 structured-block */
35832 #define OMP_PARALLEL_CLAUSE_MASK \
35833 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35843 static tree
35844 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35845 char *p_name, omp_clause_mask mask, tree *cclauses,
35846 bool *if_p)
35848 tree stmt, clauses, block;
35849 unsigned int save;
35850 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35852 strcat (p_name, " parallel");
35853 mask |= OMP_PARALLEL_CLAUSE_MASK;
35854 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35855 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35856 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35857 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35861 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35862 if (cclauses == NULL)
35863 cclauses = cclauses_buf;
35865 cp_lexer_consume_token (parser->lexer);
35866 if (!flag_openmp) /* flag_openmp_simd */
35867 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35868 if_p);
35869 block = begin_omp_parallel ();
35870 save = cp_parser_begin_omp_structured_block (parser);
35871 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35872 if_p);
35873 cp_parser_end_omp_structured_block (parser, save);
35874 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35875 block);
35876 if (ret == NULL_TREE)
35877 return ret;
35878 OMP_PARALLEL_COMBINED (stmt) = 1;
35879 return stmt;
35881 /* When combined with distribute, parallel has to be followed by for.
35882 #pragma omp target parallel is allowed though. */
35883 else if (cclauses
35884 && (mask & (OMP_CLAUSE_MASK_1
35885 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35887 error_at (loc, "expected %<for%> after %qs", p_name);
35888 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35889 return NULL_TREE;
35891 else if (!flag_openmp) /* flag_openmp_simd */
35893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35894 return NULL_TREE;
35896 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35898 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35899 const char *p = IDENTIFIER_POINTER (id);
35900 if (strcmp (p, "sections") == 0)
35902 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35903 cclauses = cclauses_buf;
35905 cp_lexer_consume_token (parser->lexer);
35906 block = begin_omp_parallel ();
35907 save = cp_parser_begin_omp_structured_block (parser);
35908 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35909 cp_parser_end_omp_structured_block (parser, save);
35910 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35911 block);
35912 OMP_PARALLEL_COMBINED (stmt) = 1;
35913 return stmt;
35917 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35918 cclauses == NULL);
35919 if (cclauses)
35921 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35922 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35925 block = begin_omp_parallel ();
35926 save = cp_parser_begin_omp_structured_block (parser);
35927 cp_parser_statement (parser, NULL_TREE, false, if_p);
35928 cp_parser_end_omp_structured_block (parser, save);
35929 stmt = finish_omp_parallel (clauses, block);
35930 return stmt;
35933 /* OpenMP 2.5:
35934 # pragma omp single single-clause[optseq] new-line
35935 structured-block */
35937 #define OMP_SINGLE_CLAUSE_MASK \
35938 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35943 static tree
35944 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35946 tree stmt = make_node (OMP_SINGLE);
35947 TREE_TYPE (stmt) = void_type_node;
35949 OMP_SINGLE_CLAUSES (stmt)
35950 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35951 "#pragma omp single", pragma_tok);
35952 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35954 return add_stmt (stmt);
35957 /* OpenMP 3.0:
35958 # pragma omp task task-clause[optseq] new-line
35959 structured-block */
35961 #define OMP_TASK_CLAUSE_MASK \
35962 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35973 static tree
35974 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35976 tree clauses, block;
35977 unsigned int save;
35979 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35980 "#pragma omp task", pragma_tok);
35981 block = begin_omp_task ();
35982 save = cp_parser_begin_omp_structured_block (parser);
35983 cp_parser_statement (parser, NULL_TREE, false, if_p);
35984 cp_parser_end_omp_structured_block (parser, save);
35985 return finish_omp_task (clauses, block);
35988 /* OpenMP 3.0:
35989 # pragma omp taskwait new-line */
35991 static void
35992 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35994 cp_parser_require_pragma_eol (parser, pragma_tok);
35995 finish_omp_taskwait ();
35998 /* OpenMP 3.1:
35999 # pragma omp taskyield new-line */
36001 static void
36002 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36004 cp_parser_require_pragma_eol (parser, pragma_tok);
36005 finish_omp_taskyield ();
36008 /* OpenMP 4.0:
36009 # pragma omp taskgroup new-line
36010 structured-block */
36012 static tree
36013 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36015 cp_parser_require_pragma_eol (parser, pragma_tok);
36016 return c_finish_omp_taskgroup (input_location,
36017 cp_parser_omp_structured_block (parser,
36018 if_p));
36022 /* OpenMP 2.5:
36023 # pragma omp threadprivate (variable-list) */
36025 static void
36026 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36028 tree vars;
36030 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36031 cp_parser_require_pragma_eol (parser, pragma_tok);
36033 finish_omp_threadprivate (vars);
36036 /* OpenMP 4.0:
36037 # pragma omp cancel cancel-clause[optseq] new-line */
36039 #define OMP_CANCEL_CLAUSE_MASK \
36040 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36046 static void
36047 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36049 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36050 "#pragma omp cancel", pragma_tok);
36051 finish_omp_cancel (clauses);
36054 /* OpenMP 4.0:
36055 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36057 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36063 static void
36064 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36065 enum pragma_context context)
36067 tree clauses;
36068 bool point_seen = false;
36070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36072 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36073 const char *p = IDENTIFIER_POINTER (id);
36075 if (strcmp (p, "point") == 0)
36077 cp_lexer_consume_token (parser->lexer);
36078 point_seen = true;
36081 if (!point_seen)
36083 cp_parser_error (parser, "expected %<point%>");
36084 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36085 return;
36088 if (context != pragma_compound)
36090 if (context == pragma_stmt)
36091 error_at (pragma_tok->location,
36092 "%<#pragma %s%> may only be used in compound statements",
36093 "omp cancellation point");
36094 else
36095 cp_parser_error (parser, "expected declaration specifiers");
36096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36097 return;
36100 clauses = cp_parser_omp_all_clauses (parser,
36101 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36102 "#pragma omp cancellation point",
36103 pragma_tok);
36104 finish_omp_cancellation_point (clauses);
36107 /* OpenMP 4.0:
36108 #pragma omp distribute distribute-clause[optseq] new-line
36109 for-loop */
36111 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36112 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36118 static tree
36119 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36120 char *p_name, omp_clause_mask mask, tree *cclauses,
36121 bool *if_p)
36123 tree clauses, sb, ret;
36124 unsigned int save;
36125 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36127 strcat (p_name, " distribute");
36128 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36130 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36132 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36133 const char *p = IDENTIFIER_POINTER (id);
36134 bool simd = false;
36135 bool parallel = false;
36137 if (strcmp (p, "simd") == 0)
36138 simd = true;
36139 else
36140 parallel = strcmp (p, "parallel") == 0;
36141 if (parallel || simd)
36143 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36144 if (cclauses == NULL)
36145 cclauses = cclauses_buf;
36146 cp_lexer_consume_token (parser->lexer);
36147 if (!flag_openmp) /* flag_openmp_simd */
36149 if (simd)
36150 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36151 cclauses, if_p);
36152 else
36153 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36154 cclauses, if_p);
36156 sb = begin_omp_structured_block ();
36157 save = cp_parser_begin_omp_structured_block (parser);
36158 if (simd)
36159 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36160 cclauses, if_p);
36161 else
36162 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36163 cclauses, if_p);
36164 cp_parser_end_omp_structured_block (parser, save);
36165 tree body = finish_omp_structured_block (sb);
36166 if (ret == NULL)
36167 return ret;
36168 ret = make_node (OMP_DISTRIBUTE);
36169 TREE_TYPE (ret) = void_type_node;
36170 OMP_FOR_BODY (ret) = body;
36171 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36172 SET_EXPR_LOCATION (ret, loc);
36173 add_stmt (ret);
36174 return ret;
36177 if (!flag_openmp) /* flag_openmp_simd */
36179 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36180 return NULL_TREE;
36183 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36184 cclauses == NULL);
36185 if (cclauses)
36187 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36188 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36191 sb = begin_omp_structured_block ();
36192 save = cp_parser_begin_omp_structured_block (parser);
36194 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36196 cp_parser_end_omp_structured_block (parser, save);
36197 add_stmt (finish_omp_structured_block (sb));
36199 return ret;
36202 /* OpenMP 4.0:
36203 # pragma omp teams teams-clause[optseq] new-line
36204 structured-block */
36206 #define OMP_TEAMS_CLAUSE_MASK \
36207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36215 static tree
36216 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36217 char *p_name, omp_clause_mask mask, tree *cclauses,
36218 bool *if_p)
36220 tree clauses, sb, ret;
36221 unsigned int save;
36222 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36224 strcat (p_name, " teams");
36225 mask |= OMP_TEAMS_CLAUSE_MASK;
36227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36230 const char *p = IDENTIFIER_POINTER (id);
36231 if (strcmp (p, "distribute") == 0)
36233 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36234 if (cclauses == NULL)
36235 cclauses = cclauses_buf;
36237 cp_lexer_consume_token (parser->lexer);
36238 if (!flag_openmp) /* flag_openmp_simd */
36239 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36240 cclauses, if_p);
36241 sb = begin_omp_structured_block ();
36242 save = cp_parser_begin_omp_structured_block (parser);
36243 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36244 cclauses, if_p);
36245 cp_parser_end_omp_structured_block (parser, save);
36246 tree body = finish_omp_structured_block (sb);
36247 if (ret == NULL)
36248 return ret;
36249 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36250 ret = make_node (OMP_TEAMS);
36251 TREE_TYPE (ret) = void_type_node;
36252 OMP_TEAMS_CLAUSES (ret) = clauses;
36253 OMP_TEAMS_BODY (ret) = body;
36254 OMP_TEAMS_COMBINED (ret) = 1;
36255 SET_EXPR_LOCATION (ret, loc);
36256 return add_stmt (ret);
36259 if (!flag_openmp) /* flag_openmp_simd */
36261 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36262 return NULL_TREE;
36265 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36266 cclauses == NULL);
36267 if (cclauses)
36269 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36270 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36273 tree stmt = make_node (OMP_TEAMS);
36274 TREE_TYPE (stmt) = void_type_node;
36275 OMP_TEAMS_CLAUSES (stmt) = clauses;
36276 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36277 SET_EXPR_LOCATION (stmt, loc);
36279 return add_stmt (stmt);
36282 /* OpenMP 4.0:
36283 # pragma omp target data target-data-clause[optseq] new-line
36284 structured-block */
36286 #define OMP_TARGET_DATA_CLAUSE_MASK \
36287 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36292 static tree
36293 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36295 tree clauses
36296 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36297 "#pragma omp target data", pragma_tok);
36298 int map_seen = 0;
36299 for (tree *pc = &clauses; *pc;)
36301 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36302 switch (OMP_CLAUSE_MAP_KIND (*pc))
36304 case GOMP_MAP_TO:
36305 case GOMP_MAP_ALWAYS_TO:
36306 case GOMP_MAP_FROM:
36307 case GOMP_MAP_ALWAYS_FROM:
36308 case GOMP_MAP_TOFROM:
36309 case GOMP_MAP_ALWAYS_TOFROM:
36310 case GOMP_MAP_ALLOC:
36311 map_seen = 3;
36312 break;
36313 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36314 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36315 case GOMP_MAP_ALWAYS_POINTER:
36316 break;
36317 default:
36318 map_seen |= 1;
36319 error_at (OMP_CLAUSE_LOCATION (*pc),
36320 "%<#pragma omp target data%> with map-type other "
36321 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36322 "on %<map%> clause");
36323 *pc = OMP_CLAUSE_CHAIN (*pc);
36324 continue;
36326 pc = &OMP_CLAUSE_CHAIN (*pc);
36329 if (map_seen != 3)
36331 if (map_seen == 0)
36332 error_at (pragma_tok->location,
36333 "%<#pragma omp target data%> must contain at least "
36334 "one %<map%> clause");
36335 return NULL_TREE;
36338 tree stmt = make_node (OMP_TARGET_DATA);
36339 TREE_TYPE (stmt) = void_type_node;
36340 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36342 keep_next_level (true);
36343 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36345 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36346 return add_stmt (stmt);
36349 /* OpenMP 4.5:
36350 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36351 structured-block */
36353 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36354 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36360 static tree
36361 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36362 enum pragma_context context)
36364 bool data_seen = false;
36365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36367 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36368 const char *p = IDENTIFIER_POINTER (id);
36370 if (strcmp (p, "data") == 0)
36372 cp_lexer_consume_token (parser->lexer);
36373 data_seen = true;
36376 if (!data_seen)
36378 cp_parser_error (parser, "expected %<data%>");
36379 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36380 return NULL_TREE;
36383 if (context == pragma_stmt)
36385 error_at (pragma_tok->location,
36386 "%<#pragma %s%> may only be used in compound statements",
36387 "omp target enter data");
36388 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36389 return NULL_TREE;
36392 tree clauses
36393 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36394 "#pragma omp target enter data", pragma_tok);
36395 int map_seen = 0;
36396 for (tree *pc = &clauses; *pc;)
36398 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36399 switch (OMP_CLAUSE_MAP_KIND (*pc))
36401 case GOMP_MAP_TO:
36402 case GOMP_MAP_ALWAYS_TO:
36403 case GOMP_MAP_ALLOC:
36404 map_seen = 3;
36405 break;
36406 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36407 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36408 case GOMP_MAP_ALWAYS_POINTER:
36409 break;
36410 default:
36411 map_seen |= 1;
36412 error_at (OMP_CLAUSE_LOCATION (*pc),
36413 "%<#pragma omp target enter data%> with map-type other "
36414 "than %<to%> or %<alloc%> on %<map%> clause");
36415 *pc = OMP_CLAUSE_CHAIN (*pc);
36416 continue;
36418 pc = &OMP_CLAUSE_CHAIN (*pc);
36421 if (map_seen != 3)
36423 if (map_seen == 0)
36424 error_at (pragma_tok->location,
36425 "%<#pragma omp target enter data%> must contain at least "
36426 "one %<map%> clause");
36427 return NULL_TREE;
36430 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36431 TREE_TYPE (stmt) = void_type_node;
36432 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36433 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36434 return add_stmt (stmt);
36437 /* OpenMP 4.5:
36438 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36439 structured-block */
36441 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36448 static tree
36449 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36450 enum pragma_context context)
36452 bool data_seen = false;
36453 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36455 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36456 const char *p = IDENTIFIER_POINTER (id);
36458 if (strcmp (p, "data") == 0)
36460 cp_lexer_consume_token (parser->lexer);
36461 data_seen = true;
36464 if (!data_seen)
36466 cp_parser_error (parser, "expected %<data%>");
36467 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36468 return NULL_TREE;
36471 if (context == pragma_stmt)
36473 error_at (pragma_tok->location,
36474 "%<#pragma %s%> may only be used in compound statements",
36475 "omp target exit data");
36476 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36477 return NULL_TREE;
36480 tree clauses
36481 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36482 "#pragma omp target exit data", pragma_tok);
36483 int map_seen = 0;
36484 for (tree *pc = &clauses; *pc;)
36486 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36487 switch (OMP_CLAUSE_MAP_KIND (*pc))
36489 case GOMP_MAP_FROM:
36490 case GOMP_MAP_ALWAYS_FROM:
36491 case GOMP_MAP_RELEASE:
36492 case GOMP_MAP_DELETE:
36493 map_seen = 3;
36494 break;
36495 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36496 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36497 case GOMP_MAP_ALWAYS_POINTER:
36498 break;
36499 default:
36500 map_seen |= 1;
36501 error_at (OMP_CLAUSE_LOCATION (*pc),
36502 "%<#pragma omp target exit data%> with map-type other "
36503 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36504 " clause");
36505 *pc = OMP_CLAUSE_CHAIN (*pc);
36506 continue;
36508 pc = &OMP_CLAUSE_CHAIN (*pc);
36511 if (map_seen != 3)
36513 if (map_seen == 0)
36514 error_at (pragma_tok->location,
36515 "%<#pragma omp target exit data%> must contain at least "
36516 "one %<map%> clause");
36517 return NULL_TREE;
36520 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36521 TREE_TYPE (stmt) = void_type_node;
36522 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36523 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36524 return add_stmt (stmt);
36527 /* OpenMP 4.0:
36528 # pragma omp target update target-update-clause[optseq] new-line */
36530 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36538 static bool
36539 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36540 enum pragma_context context)
36542 if (context == pragma_stmt)
36544 error_at (pragma_tok->location,
36545 "%<#pragma %s%> may only be used in compound statements",
36546 "omp target update");
36547 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36548 return false;
36551 tree clauses
36552 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36553 "#pragma omp target update", pragma_tok);
36554 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36555 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36557 error_at (pragma_tok->location,
36558 "%<#pragma omp target update%> must contain at least one "
36559 "%<from%> or %<to%> clauses");
36560 return false;
36563 tree stmt = make_node (OMP_TARGET_UPDATE);
36564 TREE_TYPE (stmt) = void_type_node;
36565 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36566 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36567 add_stmt (stmt);
36568 return false;
36571 /* OpenMP 4.0:
36572 # pragma omp target target-clause[optseq] new-line
36573 structured-block */
36575 #define OMP_TARGET_CLAUSE_MASK \
36576 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36586 static bool
36587 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36588 enum pragma_context context, bool *if_p)
36590 tree *pc = NULL, stmt;
36592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36595 const char *p = IDENTIFIER_POINTER (id);
36596 enum tree_code ccode = ERROR_MARK;
36598 if (strcmp (p, "teams") == 0)
36599 ccode = OMP_TEAMS;
36600 else if (strcmp (p, "parallel") == 0)
36601 ccode = OMP_PARALLEL;
36602 else if (strcmp (p, "simd") == 0)
36603 ccode = OMP_SIMD;
36604 if (ccode != ERROR_MARK)
36606 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36607 char p_name[sizeof ("#pragma omp target teams distribute "
36608 "parallel for simd")];
36610 cp_lexer_consume_token (parser->lexer);
36611 strcpy (p_name, "#pragma omp target");
36612 if (!flag_openmp) /* flag_openmp_simd */
36614 tree stmt;
36615 switch (ccode)
36617 case OMP_TEAMS:
36618 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36619 OMP_TARGET_CLAUSE_MASK,
36620 cclauses, if_p);
36621 break;
36622 case OMP_PARALLEL:
36623 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36624 OMP_TARGET_CLAUSE_MASK,
36625 cclauses, if_p);
36626 break;
36627 case OMP_SIMD:
36628 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36629 OMP_TARGET_CLAUSE_MASK,
36630 cclauses, if_p);
36631 break;
36632 default:
36633 gcc_unreachable ();
36635 return stmt != NULL_TREE;
36637 keep_next_level (true);
36638 tree sb = begin_omp_structured_block (), ret;
36639 unsigned save = cp_parser_begin_omp_structured_block (parser);
36640 switch (ccode)
36642 case OMP_TEAMS:
36643 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36644 OMP_TARGET_CLAUSE_MASK, cclauses,
36645 if_p);
36646 break;
36647 case OMP_PARALLEL:
36648 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36649 OMP_TARGET_CLAUSE_MASK, cclauses,
36650 if_p);
36651 break;
36652 case OMP_SIMD:
36653 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36654 OMP_TARGET_CLAUSE_MASK, cclauses,
36655 if_p);
36656 break;
36657 default:
36658 gcc_unreachable ();
36660 cp_parser_end_omp_structured_block (parser, save);
36661 tree body = finish_omp_structured_block (sb);
36662 if (ret == NULL_TREE)
36663 return false;
36664 if (ccode == OMP_TEAMS && !processing_template_decl)
36666 /* For combined target teams, ensure the num_teams and
36667 thread_limit clause expressions are evaluated on the host,
36668 before entering the target construct. */
36669 tree c;
36670 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36671 c; c = OMP_CLAUSE_CHAIN (c))
36672 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36673 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36674 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36676 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36677 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36678 if (expr == error_mark_node)
36679 continue;
36680 tree tmp = TARGET_EXPR_SLOT (expr);
36681 add_stmt (expr);
36682 OMP_CLAUSE_OPERAND (c, 0) = expr;
36683 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36684 OMP_CLAUSE_FIRSTPRIVATE);
36685 OMP_CLAUSE_DECL (tc) = tmp;
36686 OMP_CLAUSE_CHAIN (tc)
36687 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36688 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36691 tree stmt = make_node (OMP_TARGET);
36692 TREE_TYPE (stmt) = void_type_node;
36693 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36694 OMP_TARGET_BODY (stmt) = body;
36695 OMP_TARGET_COMBINED (stmt) = 1;
36696 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36697 add_stmt (stmt);
36698 pc = &OMP_TARGET_CLAUSES (stmt);
36699 goto check_clauses;
36701 else if (!flag_openmp) /* flag_openmp_simd */
36703 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36704 return false;
36706 else if (strcmp (p, "data") == 0)
36708 cp_lexer_consume_token (parser->lexer);
36709 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36710 return true;
36712 else if (strcmp (p, "enter") == 0)
36714 cp_lexer_consume_token (parser->lexer);
36715 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36716 return false;
36718 else if (strcmp (p, "exit") == 0)
36720 cp_lexer_consume_token (parser->lexer);
36721 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36722 return false;
36724 else if (strcmp (p, "update") == 0)
36726 cp_lexer_consume_token (parser->lexer);
36727 return cp_parser_omp_target_update (parser, pragma_tok, context);
36730 if (!flag_openmp) /* flag_openmp_simd */
36732 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36733 return false;
36736 stmt = make_node (OMP_TARGET);
36737 TREE_TYPE (stmt) = void_type_node;
36739 OMP_TARGET_CLAUSES (stmt)
36740 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36741 "#pragma omp target", pragma_tok);
36742 pc = &OMP_TARGET_CLAUSES (stmt);
36743 keep_next_level (true);
36744 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36746 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36747 add_stmt (stmt);
36749 check_clauses:
36750 while (*pc)
36752 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36753 switch (OMP_CLAUSE_MAP_KIND (*pc))
36755 case GOMP_MAP_TO:
36756 case GOMP_MAP_ALWAYS_TO:
36757 case GOMP_MAP_FROM:
36758 case GOMP_MAP_ALWAYS_FROM:
36759 case GOMP_MAP_TOFROM:
36760 case GOMP_MAP_ALWAYS_TOFROM:
36761 case GOMP_MAP_ALLOC:
36762 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36763 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36764 case GOMP_MAP_ALWAYS_POINTER:
36765 break;
36766 default:
36767 error_at (OMP_CLAUSE_LOCATION (*pc),
36768 "%<#pragma omp target%> with map-type other "
36769 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36770 "on %<map%> clause");
36771 *pc = OMP_CLAUSE_CHAIN (*pc);
36772 continue;
36774 pc = &OMP_CLAUSE_CHAIN (*pc);
36776 return true;
36779 /* OpenACC 2.0:
36780 # pragma acc cache (variable-list) new-line
36783 static tree
36784 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36786 tree stmt, clauses;
36788 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36789 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36791 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36793 stmt = make_node (OACC_CACHE);
36794 TREE_TYPE (stmt) = void_type_node;
36795 OACC_CACHE_CLAUSES (stmt) = clauses;
36796 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36797 add_stmt (stmt);
36799 return stmt;
36802 /* OpenACC 2.0:
36803 # pragma acc data oacc-data-clause[optseq] new-line
36804 structured-block */
36806 #define OACC_DATA_CLAUSE_MASK \
36807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36815 static tree
36816 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36818 tree stmt, clauses, block;
36819 unsigned int save;
36821 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36822 "#pragma acc data", pragma_tok);
36824 block = begin_omp_parallel ();
36825 save = cp_parser_begin_omp_structured_block (parser);
36826 cp_parser_statement (parser, NULL_TREE, false, if_p);
36827 cp_parser_end_omp_structured_block (parser, save);
36828 stmt = finish_oacc_data (clauses, block);
36829 return stmt;
36832 /* OpenACC 2.0:
36833 # pragma acc host_data <clauses> new-line
36834 structured-block */
36836 #define OACC_HOST_DATA_CLAUSE_MASK \
36837 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36839 static tree
36840 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36842 tree stmt, clauses, block;
36843 unsigned int save;
36845 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36846 "#pragma acc host_data", pragma_tok);
36848 block = begin_omp_parallel ();
36849 save = cp_parser_begin_omp_structured_block (parser);
36850 cp_parser_statement (parser, NULL_TREE, false, if_p);
36851 cp_parser_end_omp_structured_block (parser, save);
36852 stmt = finish_oacc_host_data (clauses, block);
36853 return stmt;
36856 /* OpenACC 2.0:
36857 # pragma acc declare oacc-data-clause[optseq] new-line
36860 #define OACC_DECLARE_CLAUSE_MASK \
36861 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36870 static tree
36871 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36873 tree clauses, stmt;
36874 bool error = false;
36876 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36877 "#pragma acc declare", pragma_tok, true);
36880 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36882 error_at (pragma_tok->location,
36883 "no valid clauses specified in %<#pragma acc declare%>");
36884 return NULL_TREE;
36887 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36889 location_t loc = OMP_CLAUSE_LOCATION (t);
36890 tree decl = OMP_CLAUSE_DECL (t);
36891 if (!DECL_P (decl))
36893 error_at (loc, "array section in %<#pragma acc declare%>");
36894 error = true;
36895 continue;
36897 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36898 switch (OMP_CLAUSE_MAP_KIND (t))
36900 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36901 case GOMP_MAP_ALLOC:
36902 case GOMP_MAP_TO:
36903 case GOMP_MAP_FORCE_DEVICEPTR:
36904 case GOMP_MAP_DEVICE_RESIDENT:
36905 break;
36907 case GOMP_MAP_LINK:
36908 if (!global_bindings_p ()
36909 && (TREE_STATIC (decl)
36910 || !DECL_EXTERNAL (decl)))
36912 error_at (loc,
36913 "%qD must be a global variable in "
36914 "%<#pragma acc declare link%>",
36915 decl);
36916 error = true;
36917 continue;
36919 break;
36921 default:
36922 if (global_bindings_p ())
36924 error_at (loc, "invalid OpenACC clause at file scope");
36925 error = true;
36926 continue;
36928 if (DECL_EXTERNAL (decl))
36930 error_at (loc,
36931 "invalid use of %<extern%> variable %qD "
36932 "in %<#pragma acc declare%>", decl);
36933 error = true;
36934 continue;
36936 else if (TREE_PUBLIC (decl))
36938 error_at (loc,
36939 "invalid use of %<global%> variable %qD "
36940 "in %<#pragma acc declare%>", decl);
36941 error = true;
36942 continue;
36944 break;
36947 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36948 || lookup_attribute ("omp declare target link",
36949 DECL_ATTRIBUTES (decl)))
36951 error_at (loc, "variable %qD used more than once with "
36952 "%<#pragma acc declare%>", decl);
36953 error = true;
36954 continue;
36957 if (!error)
36959 tree id;
36961 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36962 id = get_identifier ("omp declare target link");
36963 else
36964 id = get_identifier ("omp declare target");
36966 DECL_ATTRIBUTES (decl)
36967 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36968 if (global_bindings_p ())
36970 symtab_node *node = symtab_node::get (decl);
36971 if (node != NULL)
36973 node->offloadable = 1;
36974 if (ENABLE_OFFLOADING)
36976 g->have_offload = true;
36977 if (is_a <varpool_node *> (node))
36978 vec_safe_push (offload_vars, decl);
36985 if (error || global_bindings_p ())
36986 return NULL_TREE;
36988 stmt = make_node (OACC_DECLARE);
36989 TREE_TYPE (stmt) = void_type_node;
36990 OACC_DECLARE_CLAUSES (stmt) = clauses;
36991 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36993 add_stmt (stmt);
36995 return NULL_TREE;
36998 /* OpenACC 2.0:
36999 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37003 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37005 LOC is the location of the #pragma token.
37008 #define OACC_ENTER_DATA_CLAUSE_MASK \
37009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37015 #define OACC_EXIT_DATA_CLAUSE_MASK \
37016 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37023 static tree
37024 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37025 bool enter)
37027 location_t loc = pragma_tok->location;
37028 tree stmt, clauses;
37029 const char *p = "";
37031 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37032 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37034 if (strcmp (p, "data") != 0)
37036 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37037 enter ? "enter" : "exit");
37038 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37039 return NULL_TREE;
37042 cp_lexer_consume_token (parser->lexer);
37044 if (enter)
37045 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37046 "#pragma acc enter data", pragma_tok);
37047 else
37048 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37049 "#pragma acc exit data", pragma_tok);
37051 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37053 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37054 enter ? "enter" : "exit");
37055 return NULL_TREE;
37058 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37059 TREE_TYPE (stmt) = void_type_node;
37060 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37061 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37062 add_stmt (stmt);
37063 return stmt;
37066 /* OpenACC 2.0:
37067 # pragma acc loop oacc-loop-clause[optseq] new-line
37068 structured-block */
37070 #define OACC_LOOP_CLAUSE_MASK \
37071 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37082 static tree
37083 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37084 omp_clause_mask mask, tree *cclauses, bool *if_p)
37086 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37088 strcat (p_name, " loop");
37089 mask |= OACC_LOOP_CLAUSE_MASK;
37091 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37092 cclauses == NULL);
37093 if (cclauses)
37095 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37096 if (*cclauses)
37097 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37098 if (clauses)
37099 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37102 tree block = begin_omp_structured_block ();
37103 int save = cp_parser_begin_omp_structured_block (parser);
37104 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37105 cp_parser_end_omp_structured_block (parser, save);
37106 add_stmt (finish_omp_structured_block (block));
37108 return stmt;
37111 /* OpenACC 2.0:
37112 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37113 structured-block
37117 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37118 structured-block
37121 #define OACC_KERNELS_CLAUSE_MASK \
37122 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37136 #define OACC_PARALLEL_CLAUSE_MASK \
37137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37154 static tree
37155 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37156 char *p_name, bool *if_p)
37158 omp_clause_mask mask;
37159 enum tree_code code;
37160 switch (cp_parser_pragma_kind (pragma_tok))
37162 case PRAGMA_OACC_KERNELS:
37163 strcat (p_name, " kernels");
37164 mask = OACC_KERNELS_CLAUSE_MASK;
37165 code = OACC_KERNELS;
37166 break;
37167 case PRAGMA_OACC_PARALLEL:
37168 strcat (p_name, " parallel");
37169 mask = OACC_PARALLEL_CLAUSE_MASK;
37170 code = OACC_PARALLEL;
37171 break;
37172 default:
37173 gcc_unreachable ();
37176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37178 const char *p
37179 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37180 if (strcmp (p, "loop") == 0)
37182 cp_lexer_consume_token (parser->lexer);
37183 tree block = begin_omp_parallel ();
37184 tree clauses;
37185 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37186 if_p);
37187 return finish_omp_construct (code, block, clauses);
37191 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37193 tree block = begin_omp_parallel ();
37194 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37195 cp_parser_statement (parser, NULL_TREE, false, if_p);
37196 cp_parser_end_omp_structured_block (parser, save);
37197 return finish_omp_construct (code, block, clauses);
37200 /* OpenACC 2.0:
37201 # pragma acc update oacc-update-clause[optseq] new-line
37204 #define OACC_UPDATE_CLAUSE_MASK \
37205 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37212 static tree
37213 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37215 tree stmt, clauses;
37217 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37218 "#pragma acc update", pragma_tok);
37220 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37222 error_at (pragma_tok->location,
37223 "%<#pragma acc update%> must contain at least one "
37224 "%<device%> or %<host%> or %<self%> clause");
37225 return NULL_TREE;
37228 stmt = make_node (OACC_UPDATE);
37229 TREE_TYPE (stmt) = void_type_node;
37230 OACC_UPDATE_CLAUSES (stmt) = clauses;
37231 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37232 add_stmt (stmt);
37233 return stmt;
37236 /* OpenACC 2.0:
37237 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37239 LOC is the location of the #pragma token.
37242 #define OACC_WAIT_CLAUSE_MASK \
37243 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37245 static tree
37246 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37248 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37249 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37251 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37252 list = cp_parser_oacc_wait_list (parser, loc, list);
37254 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37255 "#pragma acc wait", pragma_tok);
37257 stmt = c_finish_oacc_wait (loc, list, clauses);
37258 stmt = finish_expr_stmt (stmt);
37260 return stmt;
37263 /* OpenMP 4.0:
37264 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37266 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37274 static void
37275 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37276 enum pragma_context context)
37278 bool first_p = parser->omp_declare_simd == NULL;
37279 cp_omp_declare_simd_data data;
37280 if (first_p)
37282 data.error_seen = false;
37283 data.fndecl_seen = false;
37284 data.tokens = vNULL;
37285 data.clauses = NULL_TREE;
37286 /* It is safe to take the address of a local variable; it will only be
37287 used while this scope is live. */
37288 parser->omp_declare_simd = &data;
37291 /* Store away all pragma tokens. */
37292 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37293 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37294 cp_lexer_consume_token (parser->lexer);
37295 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37296 parser->omp_declare_simd->error_seen = true;
37297 cp_parser_require_pragma_eol (parser, pragma_tok);
37298 struct cp_token_cache *cp
37299 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37300 parser->omp_declare_simd->tokens.safe_push (cp);
37302 if (first_p)
37304 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37305 cp_parser_pragma (parser, context, NULL);
37306 switch (context)
37308 case pragma_external:
37309 cp_parser_declaration (parser);
37310 break;
37311 case pragma_member:
37312 cp_parser_member_declaration (parser);
37313 break;
37314 case pragma_objc_icode:
37315 cp_parser_block_declaration (parser, /*statement_p=*/false);
37316 break;
37317 default:
37318 cp_parser_declaration_statement (parser);
37319 break;
37321 if (parser->omp_declare_simd
37322 && !parser->omp_declare_simd->error_seen
37323 && !parser->omp_declare_simd->fndecl_seen)
37324 error_at (pragma_tok->location,
37325 "%<#pragma omp declare simd%> not immediately followed by "
37326 "function declaration or definition");
37327 data.tokens.release ();
37328 parser->omp_declare_simd = NULL;
37332 /* Finalize #pragma omp declare simd clauses after direct declarator has
37333 been parsed, and put that into "omp declare simd" attribute. */
37335 static tree
37336 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37338 struct cp_token_cache *ce;
37339 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37340 int i;
37342 if (!data->error_seen && data->fndecl_seen)
37344 error ("%<#pragma omp declare simd%> not immediately followed by "
37345 "a single function declaration or definition");
37346 data->error_seen = true;
37348 if (data->error_seen)
37349 return attrs;
37351 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37353 tree c, cl;
37355 cp_parser_push_lexer_for_tokens (parser, ce);
37356 parser->lexer->in_pragma = true;
37357 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37358 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37359 cp_lexer_consume_token (parser->lexer);
37360 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37361 "#pragma omp declare simd", pragma_tok);
37362 cp_parser_pop_lexer (parser);
37363 if (cl)
37364 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37365 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37366 TREE_CHAIN (c) = attrs;
37367 if (processing_template_decl)
37368 ATTR_IS_DEPENDENT (c) = 1;
37369 attrs = c;
37372 data->fndecl_seen = true;
37373 return attrs;
37377 /* OpenMP 4.0:
37378 # pragma omp declare target new-line
37379 declarations and definitions
37380 # pragma omp end declare target new-line
37382 OpenMP 4.5:
37383 # pragma omp declare target ( extended-list ) new-line
37385 # pragma omp declare target declare-target-clauses[seq] new-line */
37387 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37388 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37391 static void
37392 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37394 tree clauses = NULL_TREE;
37395 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37396 clauses
37397 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37398 "#pragma omp declare target", pragma_tok);
37399 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37401 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37402 clauses);
37403 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37404 cp_parser_require_pragma_eol (parser, pragma_tok);
37406 else
37408 cp_parser_require_pragma_eol (parser, pragma_tok);
37409 scope_chain->omp_declare_target_attribute++;
37410 return;
37412 if (scope_chain->omp_declare_target_attribute)
37413 error_at (pragma_tok->location,
37414 "%<#pragma omp declare target%> with clauses in between "
37415 "%<#pragma omp declare target%> without clauses and "
37416 "%<#pragma omp end declare target%>");
37417 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37419 tree t = OMP_CLAUSE_DECL (c), id;
37420 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37421 tree at2 = lookup_attribute ("omp declare target link",
37422 DECL_ATTRIBUTES (t));
37423 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37425 id = get_identifier ("omp declare target link");
37426 std::swap (at1, at2);
37428 else
37429 id = get_identifier ("omp declare target");
37430 if (at2)
37432 error_at (OMP_CLAUSE_LOCATION (c),
37433 "%qD specified both in declare target %<link%> and %<to%>"
37434 " clauses", t);
37435 continue;
37437 if (!at1)
37439 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37440 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37441 continue;
37443 symtab_node *node = symtab_node::get (t);
37444 if (node != NULL)
37446 node->offloadable = 1;
37447 if (ENABLE_OFFLOADING)
37449 g->have_offload = true;
37450 if (is_a <varpool_node *> (node))
37451 vec_safe_push (offload_vars, t);
37458 static void
37459 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37461 const char *p = "";
37462 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37464 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37465 p = IDENTIFIER_POINTER (id);
37467 if (strcmp (p, "declare") == 0)
37469 cp_lexer_consume_token (parser->lexer);
37470 p = "";
37471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37473 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37474 p = IDENTIFIER_POINTER (id);
37476 if (strcmp (p, "target") == 0)
37477 cp_lexer_consume_token (parser->lexer);
37478 else
37480 cp_parser_error (parser, "expected %<target%>");
37481 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37482 return;
37485 else
37487 cp_parser_error (parser, "expected %<declare%>");
37488 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37489 return;
37491 cp_parser_require_pragma_eol (parser, pragma_tok);
37492 if (!scope_chain->omp_declare_target_attribute)
37493 error_at (pragma_tok->location,
37494 "%<#pragma omp end declare target%> without corresponding "
37495 "%<#pragma omp declare target%>");
37496 else
37497 scope_chain->omp_declare_target_attribute--;
37500 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37501 expression and optional initializer clause of
37502 #pragma omp declare reduction. We store the expression(s) as
37503 either 3, 6 or 7 special statements inside of the artificial function's
37504 body. The first two statements are DECL_EXPRs for the artificial
37505 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37506 expression that uses those variables.
37507 If there was any INITIALIZER clause, this is followed by further statements,
37508 the fourth and fifth statements are DECL_EXPRs for the artificial
37509 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37510 constructor variant (first token after open paren is not omp_priv),
37511 then the sixth statement is a statement with the function call expression
37512 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37513 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37514 to initialize the OMP_PRIV artificial variable and there is seventh
37515 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37517 static bool
37518 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37520 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37521 gcc_assert (TYPE_REF_P (type));
37522 type = TREE_TYPE (type);
37523 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37524 DECL_ARTIFICIAL (omp_out) = 1;
37525 pushdecl (omp_out);
37526 add_decl_expr (omp_out);
37527 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37528 DECL_ARTIFICIAL (omp_in) = 1;
37529 pushdecl (omp_in);
37530 add_decl_expr (omp_in);
37531 tree combiner;
37532 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37534 keep_next_level (true);
37535 tree block = begin_omp_structured_block ();
37536 combiner = cp_parser_expression (parser);
37537 finish_expr_stmt (combiner);
37538 block = finish_omp_structured_block (block);
37539 add_stmt (block);
37541 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37542 return false;
37544 const char *p = "";
37545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37547 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37548 p = IDENTIFIER_POINTER (id);
37551 if (strcmp (p, "initializer") == 0)
37553 cp_lexer_consume_token (parser->lexer);
37554 matching_parens parens;
37555 if (!parens.require_open (parser))
37556 return false;
37558 p = "";
37559 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37561 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37562 p = IDENTIFIER_POINTER (id);
37565 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37566 DECL_ARTIFICIAL (omp_priv) = 1;
37567 pushdecl (omp_priv);
37568 add_decl_expr (omp_priv);
37569 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37570 DECL_ARTIFICIAL (omp_orig) = 1;
37571 pushdecl (omp_orig);
37572 add_decl_expr (omp_orig);
37574 keep_next_level (true);
37575 block = begin_omp_structured_block ();
37577 bool ctor = false;
37578 if (strcmp (p, "omp_priv") == 0)
37580 bool is_direct_init, is_non_constant_init;
37581 ctor = true;
37582 cp_lexer_consume_token (parser->lexer);
37583 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37584 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37585 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37586 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37587 == CPP_CLOSE_PAREN
37588 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37589 == CPP_CLOSE_PAREN))
37591 finish_omp_structured_block (block);
37592 error ("invalid initializer clause");
37593 return false;
37595 initializer = cp_parser_initializer (parser, &is_direct_init,
37596 &is_non_constant_init);
37597 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37598 NULL_TREE, LOOKUP_ONLYCONVERTING);
37600 else
37602 cp_parser_parse_tentatively (parser);
37603 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37604 /*check_dependency_p=*/true,
37605 /*template_p=*/NULL,
37606 /*declarator_p=*/false,
37607 /*optional_p=*/false);
37608 vec<tree, va_gc> *args;
37609 if (fn_name == error_mark_node
37610 || cp_parser_error_occurred (parser)
37611 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37612 || ((args = cp_parser_parenthesized_expression_list
37613 (parser, non_attr, /*cast_p=*/false,
37614 /*allow_expansion_p=*/true,
37615 /*non_constant_p=*/NULL)),
37616 cp_parser_error_occurred (parser)))
37618 finish_omp_structured_block (block);
37619 cp_parser_abort_tentative_parse (parser);
37620 cp_parser_error (parser, "expected id-expression (arguments)");
37621 return false;
37623 unsigned int i;
37624 tree arg;
37625 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37626 if (arg == omp_priv
37627 || (TREE_CODE (arg) == ADDR_EXPR
37628 && TREE_OPERAND (arg, 0) == omp_priv))
37629 break;
37630 cp_parser_abort_tentative_parse (parser);
37631 if (arg == NULL_TREE)
37632 error ("one of the initializer call arguments should be %<omp_priv%>"
37633 " or %<&omp_priv%>");
37634 initializer = cp_parser_postfix_expression (parser, false, false, false,
37635 false, NULL);
37636 finish_expr_stmt (initializer);
37639 block = finish_omp_structured_block (block);
37640 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37641 add_stmt (block);
37643 if (ctor)
37644 add_decl_expr (omp_orig);
37646 if (!parens.require_close (parser))
37647 return false;
37650 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37651 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37652 UNKNOWN_LOCATION);
37654 return true;
37657 /* OpenMP 4.0
37658 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37659 initializer-clause[opt] new-line
37661 initializer-clause:
37662 initializer (omp_priv initializer)
37663 initializer (function-name (argument-list)) */
37665 static void
37666 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37667 enum pragma_context)
37669 auto_vec<tree> types;
37670 enum tree_code reduc_code = ERROR_MARK;
37671 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37672 unsigned int i;
37673 cp_token *first_token;
37674 cp_token_cache *cp;
37675 int errs;
37676 void *p;
37678 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37679 p = obstack_alloc (&declarator_obstack, 0);
37681 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37682 goto fail;
37684 switch (cp_lexer_peek_token (parser->lexer)->type)
37686 case CPP_PLUS:
37687 reduc_code = PLUS_EXPR;
37688 break;
37689 case CPP_MULT:
37690 reduc_code = MULT_EXPR;
37691 break;
37692 case CPP_MINUS:
37693 reduc_code = MINUS_EXPR;
37694 break;
37695 case CPP_AND:
37696 reduc_code = BIT_AND_EXPR;
37697 break;
37698 case CPP_XOR:
37699 reduc_code = BIT_XOR_EXPR;
37700 break;
37701 case CPP_OR:
37702 reduc_code = BIT_IOR_EXPR;
37703 break;
37704 case CPP_AND_AND:
37705 reduc_code = TRUTH_ANDIF_EXPR;
37706 break;
37707 case CPP_OR_OR:
37708 reduc_code = TRUTH_ORIF_EXPR;
37709 break;
37710 case CPP_NAME:
37711 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37712 break;
37713 default:
37714 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37715 "%<|%>, %<&&%>, %<||%> or identifier");
37716 goto fail;
37719 if (reduc_code != ERROR_MARK)
37720 cp_lexer_consume_token (parser->lexer);
37722 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37723 if (reduc_id == error_mark_node)
37724 goto fail;
37726 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37727 goto fail;
37729 /* Types may not be defined in declare reduction type list. */
37730 const char *saved_message;
37731 saved_message = parser->type_definition_forbidden_message;
37732 parser->type_definition_forbidden_message
37733 = G_("types may not be defined in declare reduction type list");
37734 bool saved_colon_corrects_to_scope_p;
37735 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37736 parser->colon_corrects_to_scope_p = false;
37737 bool saved_colon_doesnt_start_class_def_p;
37738 saved_colon_doesnt_start_class_def_p
37739 = parser->colon_doesnt_start_class_def_p;
37740 parser->colon_doesnt_start_class_def_p = true;
37742 while (true)
37744 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37745 type = cp_parser_type_id (parser);
37746 if (type == error_mark_node)
37748 else if (ARITHMETIC_TYPE_P (type)
37749 && (orig_reduc_id == NULL_TREE
37750 || (TREE_CODE (type) != COMPLEX_TYPE
37751 && (id_equal (orig_reduc_id, "min")
37752 || id_equal (orig_reduc_id, "max")))))
37753 error_at (loc, "predeclared arithmetic type %qT in "
37754 "%<#pragma omp declare reduction%>", type);
37755 else if (TREE_CODE (type) == FUNCTION_TYPE
37756 || TREE_CODE (type) == METHOD_TYPE
37757 || TREE_CODE (type) == ARRAY_TYPE)
37758 error_at (loc, "function or array type %qT in "
37759 "%<#pragma omp declare reduction%>", type);
37760 else if (TYPE_REF_P (type))
37761 error_at (loc, "reference type %qT in "
37762 "%<#pragma omp declare reduction%>", type);
37763 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37764 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37765 "%<#pragma omp declare reduction%>", type);
37766 else
37767 types.safe_push (type);
37769 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37770 cp_lexer_consume_token (parser->lexer);
37771 else
37772 break;
37775 /* Restore the saved message. */
37776 parser->type_definition_forbidden_message = saved_message;
37777 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37778 parser->colon_doesnt_start_class_def_p
37779 = saved_colon_doesnt_start_class_def_p;
37781 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37782 || types.is_empty ())
37784 fail:
37785 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37786 goto done;
37789 first_token = cp_lexer_peek_token (parser->lexer);
37790 cp = NULL;
37791 errs = errorcount;
37792 FOR_EACH_VEC_ELT (types, i, type)
37794 tree fntype
37795 = build_function_type_list (void_type_node,
37796 cp_build_reference_type (type, false),
37797 NULL_TREE);
37798 tree this_reduc_id = reduc_id;
37799 if (!dependent_type_p (type))
37800 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37801 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37802 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37803 DECL_ARTIFICIAL (fndecl) = 1;
37804 DECL_EXTERNAL (fndecl) = 1;
37805 DECL_DECLARED_INLINE_P (fndecl) = 1;
37806 DECL_IGNORED_P (fndecl) = 1;
37807 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37808 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37809 DECL_ATTRIBUTES (fndecl)
37810 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37811 DECL_ATTRIBUTES (fndecl));
37812 if (processing_template_decl)
37813 fndecl = push_template_decl (fndecl);
37814 bool block_scope = false;
37815 tree block = NULL_TREE;
37816 if (current_function_decl)
37818 block_scope = true;
37819 DECL_CONTEXT (fndecl) = global_namespace;
37820 if (!processing_template_decl)
37821 pushdecl (fndecl);
37823 else if (current_class_type)
37825 if (cp == NULL)
37827 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37828 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37829 cp_lexer_consume_token (parser->lexer);
37830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37831 goto fail;
37832 cp = cp_token_cache_new (first_token,
37833 cp_lexer_peek_nth_token (parser->lexer,
37834 2));
37836 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37837 finish_member_declaration (fndecl);
37838 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37839 DECL_PENDING_INLINE_P (fndecl) = 1;
37840 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37841 continue;
37843 else
37845 DECL_CONTEXT (fndecl) = current_namespace;
37846 pushdecl (fndecl);
37848 if (!block_scope)
37849 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37850 else
37851 block = begin_omp_structured_block ();
37852 if (cp)
37854 cp_parser_push_lexer_for_tokens (parser, cp);
37855 parser->lexer->in_pragma = true;
37857 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37859 if (!block_scope)
37860 finish_function (/*inline_p=*/false);
37861 else
37862 DECL_CONTEXT (fndecl) = current_function_decl;
37863 if (cp)
37864 cp_parser_pop_lexer (parser);
37865 goto fail;
37867 if (cp)
37868 cp_parser_pop_lexer (parser);
37869 if (!block_scope)
37870 finish_function (/*inline_p=*/false);
37871 else
37873 DECL_CONTEXT (fndecl) = current_function_decl;
37874 block = finish_omp_structured_block (block);
37875 if (TREE_CODE (block) == BIND_EXPR)
37876 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37877 else if (TREE_CODE (block) == STATEMENT_LIST)
37878 DECL_SAVED_TREE (fndecl) = block;
37879 if (processing_template_decl)
37880 add_decl_expr (fndecl);
37882 cp_check_omp_declare_reduction (fndecl);
37883 if (cp == NULL && types.length () > 1)
37884 cp = cp_token_cache_new (first_token,
37885 cp_lexer_peek_nth_token (parser->lexer, 2));
37886 if (errs != errorcount)
37887 break;
37890 cp_parser_require_pragma_eol (parser, pragma_tok);
37892 done:
37893 /* Free any declarators allocated. */
37894 obstack_free (&declarator_obstack, p);
37897 /* OpenMP 4.0
37898 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37899 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37900 initializer-clause[opt] new-line
37901 #pragma omp declare target new-line */
37903 static bool
37904 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37905 enum pragma_context context)
37907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37909 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37910 const char *p = IDENTIFIER_POINTER (id);
37912 if (strcmp (p, "simd") == 0)
37914 cp_lexer_consume_token (parser->lexer);
37915 cp_parser_omp_declare_simd (parser, pragma_tok,
37916 context);
37917 return true;
37919 cp_ensure_no_omp_declare_simd (parser);
37920 if (strcmp (p, "reduction") == 0)
37922 cp_lexer_consume_token (parser->lexer);
37923 cp_parser_omp_declare_reduction (parser, pragma_tok,
37924 context);
37925 return false;
37927 if (!flag_openmp) /* flag_openmp_simd */
37929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37930 return false;
37932 if (strcmp (p, "target") == 0)
37934 cp_lexer_consume_token (parser->lexer);
37935 cp_parser_omp_declare_target (parser, pragma_tok);
37936 return false;
37939 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37940 "or %<target%>");
37941 cp_parser_require_pragma_eol (parser, pragma_tok);
37942 return false;
37945 /* OpenMP 4.5:
37946 #pragma omp taskloop taskloop-clause[optseq] new-line
37947 for-loop
37949 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37950 for-loop */
37952 #define OMP_TASKLOOP_CLAUSE_MASK \
37953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37968 static tree
37969 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37970 char *p_name, omp_clause_mask mask, tree *cclauses,
37971 bool *if_p)
37973 tree clauses, sb, ret;
37974 unsigned int save;
37975 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37977 strcat (p_name, " taskloop");
37978 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37980 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37982 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37983 const char *p = IDENTIFIER_POINTER (id);
37985 if (strcmp (p, "simd") == 0)
37987 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37988 if (cclauses == NULL)
37989 cclauses = cclauses_buf;
37991 cp_lexer_consume_token (parser->lexer);
37992 if (!flag_openmp) /* flag_openmp_simd */
37993 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37994 cclauses, if_p);
37995 sb = begin_omp_structured_block ();
37996 save = cp_parser_begin_omp_structured_block (parser);
37997 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37998 cclauses, if_p);
37999 cp_parser_end_omp_structured_block (parser, save);
38000 tree body = finish_omp_structured_block (sb);
38001 if (ret == NULL)
38002 return ret;
38003 ret = make_node (OMP_TASKLOOP);
38004 TREE_TYPE (ret) = void_type_node;
38005 OMP_FOR_BODY (ret) = body;
38006 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38007 SET_EXPR_LOCATION (ret, loc);
38008 add_stmt (ret);
38009 return ret;
38012 if (!flag_openmp) /* flag_openmp_simd */
38014 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38015 return NULL_TREE;
38018 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38019 cclauses == NULL);
38020 if (cclauses)
38022 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38023 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38026 sb = begin_omp_structured_block ();
38027 save = cp_parser_begin_omp_structured_block (parser);
38029 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38030 if_p);
38032 cp_parser_end_omp_structured_block (parser, save);
38033 add_stmt (finish_omp_structured_block (sb));
38035 return ret;
38039 /* OpenACC 2.0:
38040 # pragma acc routine oacc-routine-clause[optseq] new-line
38041 function-definition
38043 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38046 #define OACC_ROUTINE_CLAUSE_MASK \
38047 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38053 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38054 component, which must resolve to a declared namespace-scope
38055 function. The clauses are either processed directly (for a named
38056 function), or defered until the immediatley following declaration
38057 is parsed. */
38059 static void
38060 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38061 enum pragma_context context)
38063 gcc_checking_assert (context == pragma_external);
38064 /* The checking for "another pragma following this one" in the "no optional
38065 '( name )'" case makes sure that we dont re-enter. */
38066 gcc_checking_assert (parser->oacc_routine == NULL);
38068 cp_oacc_routine_data data;
38069 data.error_seen = false;
38070 data.fndecl_seen = false;
38071 data.tokens = vNULL;
38072 data.clauses = NULL_TREE;
38073 data.loc = pragma_tok->location;
38074 /* It is safe to take the address of a local variable; it will only be
38075 used while this scope is live. */
38076 parser->oacc_routine = &data;
38078 /* Look for optional '( name )'. */
38079 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38081 matching_parens parens;
38082 parens.consume_open (parser); /* '(' */
38084 /* We parse the name as an id-expression. If it resolves to
38085 anything other than a non-overloaded function at namespace
38086 scope, it's an error. */
38087 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38088 tree name = cp_parser_id_expression (parser,
38089 /*template_keyword_p=*/false,
38090 /*check_dependency_p=*/false,
38091 /*template_p=*/NULL,
38092 /*declarator_p=*/false,
38093 /*optional_p=*/false);
38094 tree decl = (identifier_p (name)
38095 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38096 : name);
38097 if (name != error_mark_node && decl == error_mark_node)
38098 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38100 if (decl == error_mark_node
38101 || !parens.require_close (parser))
38103 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38104 parser->oacc_routine = NULL;
38105 return;
38108 data.clauses
38109 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38110 "#pragma acc routine",
38111 cp_lexer_peek_token (parser->lexer));
38113 if (decl && is_overloaded_fn (decl)
38114 && (TREE_CODE (decl) != FUNCTION_DECL
38115 || DECL_FUNCTION_TEMPLATE_P (decl)))
38117 error_at (name_loc,
38118 "%<#pragma acc routine%> names a set of overloads");
38119 parser->oacc_routine = NULL;
38120 return;
38123 /* Perhaps we should use the same rule as declarations in different
38124 namespaces? */
38125 if (!DECL_NAMESPACE_SCOPE_P (decl))
38127 error_at (name_loc,
38128 "%qD does not refer to a namespace scope function", decl);
38129 parser->oacc_routine = NULL;
38130 return;
38133 if (TREE_CODE (decl) != FUNCTION_DECL)
38135 error_at (name_loc, "%qD does not refer to a function", decl);
38136 parser->oacc_routine = NULL;
38137 return;
38140 cp_finalize_oacc_routine (parser, decl, false);
38141 parser->oacc_routine = NULL;
38143 else /* No optional '( name )'. */
38145 /* Store away all pragma tokens. */
38146 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38147 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38148 cp_lexer_consume_token (parser->lexer);
38149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38150 parser->oacc_routine->error_seen = true;
38151 cp_parser_require_pragma_eol (parser, pragma_tok);
38152 struct cp_token_cache *cp
38153 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38154 parser->oacc_routine->tokens.safe_push (cp);
38156 /* Emit a helpful diagnostic if there's another pragma following this
38157 one. */
38158 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38160 cp_ensure_no_oacc_routine (parser);
38161 data.tokens.release ();
38162 /* ..., and then just keep going. */
38163 return;
38166 /* We only have to consider the pragma_external case here. */
38167 cp_parser_declaration (parser);
38168 if (parser->oacc_routine
38169 && !parser->oacc_routine->fndecl_seen)
38170 cp_ensure_no_oacc_routine (parser);
38171 else
38172 parser->oacc_routine = NULL;
38173 data.tokens.release ();
38177 /* Finalize #pragma acc routine clauses after direct declarator has
38178 been parsed. */
38180 static tree
38181 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38183 struct cp_token_cache *ce;
38184 cp_oacc_routine_data *data = parser->oacc_routine;
38186 if (!data->error_seen && data->fndecl_seen)
38188 error_at (data->loc,
38189 "%<#pragma acc routine%> not immediately followed by "
38190 "a single function declaration or definition");
38191 data->error_seen = true;
38193 if (data->error_seen)
38194 return attrs;
38196 gcc_checking_assert (data->tokens.length () == 1);
38197 ce = data->tokens[0];
38199 cp_parser_push_lexer_for_tokens (parser, ce);
38200 parser->lexer->in_pragma = true;
38201 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38203 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38204 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38205 parser->oacc_routine->clauses
38206 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38207 "#pragma acc routine", pragma_tok);
38208 cp_parser_pop_lexer (parser);
38209 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38210 fndecl_seen. */
38212 return attrs;
38215 /* Apply any saved OpenACC routine clauses to a just-parsed
38216 declaration. */
38218 static void
38219 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38221 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38223 /* Keep going if we're in error reporting mode. */
38224 if (parser->oacc_routine->error_seen
38225 || fndecl == error_mark_node)
38226 return;
38228 if (parser->oacc_routine->fndecl_seen)
38230 error_at (parser->oacc_routine->loc,
38231 "%<#pragma acc routine%> not immediately followed by"
38232 " a single function declaration or definition");
38233 parser->oacc_routine = NULL;
38234 return;
38236 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38238 cp_ensure_no_oacc_routine (parser);
38239 return;
38242 if (oacc_get_fn_attrib (fndecl))
38244 error_at (parser->oacc_routine->loc,
38245 "%<#pragma acc routine%> already applied to %qD", fndecl);
38246 parser->oacc_routine = NULL;
38247 return;
38250 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38252 error_at (parser->oacc_routine->loc,
38253 TREE_USED (fndecl)
38254 ? G_("%<#pragma acc routine%> must be applied before use")
38255 : G_("%<#pragma acc routine%> must be applied before "
38256 "definition"));
38257 parser->oacc_routine = NULL;
38258 return;
38261 /* Process the routine's dimension clauses. */
38262 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38263 oacc_replace_fn_attrib (fndecl, dims);
38265 /* Add an "omp declare target" attribute. */
38266 DECL_ATTRIBUTES (fndecl)
38267 = tree_cons (get_identifier ("omp declare target"),
38268 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38270 /* Don't unset parser->oacc_routine here: we may still need it to
38271 diagnose wrong usage. But, remember that we've used this "#pragma acc
38272 routine". */
38273 parser->oacc_routine->fndecl_seen = true;
38277 /* Main entry point to OpenMP statement pragmas. */
38279 static void
38280 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38282 tree stmt;
38283 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38284 omp_clause_mask mask (0);
38286 switch (cp_parser_pragma_kind (pragma_tok))
38288 case PRAGMA_OACC_ATOMIC:
38289 cp_parser_omp_atomic (parser, pragma_tok);
38290 return;
38291 case PRAGMA_OACC_CACHE:
38292 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38293 break;
38294 case PRAGMA_OACC_DATA:
38295 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38296 break;
38297 case PRAGMA_OACC_ENTER_DATA:
38298 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38299 break;
38300 case PRAGMA_OACC_EXIT_DATA:
38301 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38302 break;
38303 case PRAGMA_OACC_HOST_DATA:
38304 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38305 break;
38306 case PRAGMA_OACC_KERNELS:
38307 case PRAGMA_OACC_PARALLEL:
38308 strcpy (p_name, "#pragma acc");
38309 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38310 if_p);
38311 break;
38312 case PRAGMA_OACC_LOOP:
38313 strcpy (p_name, "#pragma acc");
38314 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38315 if_p);
38316 break;
38317 case PRAGMA_OACC_UPDATE:
38318 stmt = cp_parser_oacc_update (parser, pragma_tok);
38319 break;
38320 case PRAGMA_OACC_WAIT:
38321 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38322 break;
38323 case PRAGMA_OMP_ATOMIC:
38324 cp_parser_omp_atomic (parser, pragma_tok);
38325 return;
38326 case PRAGMA_OMP_CRITICAL:
38327 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38328 break;
38329 case PRAGMA_OMP_DISTRIBUTE:
38330 strcpy (p_name, "#pragma omp");
38331 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38332 if_p);
38333 break;
38334 case PRAGMA_OMP_FOR:
38335 strcpy (p_name, "#pragma omp");
38336 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38337 if_p);
38338 break;
38339 case PRAGMA_OMP_MASTER:
38340 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38341 break;
38342 case PRAGMA_OMP_PARALLEL:
38343 strcpy (p_name, "#pragma omp");
38344 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38345 if_p);
38346 break;
38347 case PRAGMA_OMP_SECTIONS:
38348 strcpy (p_name, "#pragma omp");
38349 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38350 break;
38351 case PRAGMA_OMP_SIMD:
38352 strcpy (p_name, "#pragma omp");
38353 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38354 if_p);
38355 break;
38356 case PRAGMA_OMP_SINGLE:
38357 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38358 break;
38359 case PRAGMA_OMP_TASK:
38360 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38361 break;
38362 case PRAGMA_OMP_TASKGROUP:
38363 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38364 break;
38365 case PRAGMA_OMP_TASKLOOP:
38366 strcpy (p_name, "#pragma omp");
38367 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38368 if_p);
38369 break;
38370 case PRAGMA_OMP_TEAMS:
38371 strcpy (p_name, "#pragma omp");
38372 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38373 if_p);
38374 break;
38375 default:
38376 gcc_unreachable ();
38379 protected_set_expr_location (stmt, pragma_tok->location);
38382 /* Transactional Memory parsing routines. */
38384 /* Parse a transaction attribute.
38386 txn-attribute:
38387 attribute
38388 [ [ identifier ] ]
38390 We use this instead of cp_parser_attributes_opt for transactions to avoid
38391 the pedwarn in C++98 mode. */
38393 static tree
38394 cp_parser_txn_attribute_opt (cp_parser *parser)
38396 cp_token *token;
38397 tree attr_name, attr = NULL;
38399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38400 return cp_parser_attributes_opt (parser);
38402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38403 return NULL_TREE;
38404 cp_lexer_consume_token (parser->lexer);
38405 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38406 goto error1;
38408 token = cp_lexer_peek_token (parser->lexer);
38409 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38411 token = cp_lexer_consume_token (parser->lexer);
38413 attr_name = (token->type == CPP_KEYWORD
38414 /* For keywords, use the canonical spelling,
38415 not the parsed identifier. */
38416 ? ridpointers[(int) token->keyword]
38417 : token->u.value);
38418 attr = build_tree_list (attr_name, NULL_TREE);
38420 else
38421 cp_parser_error (parser, "expected identifier");
38423 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38424 error1:
38425 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38426 return attr;
38429 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38431 transaction-statement:
38432 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38433 compound-statement
38434 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38437 static tree
38438 cp_parser_transaction (cp_parser *parser, cp_token *token)
38440 unsigned char old_in = parser->in_transaction;
38441 unsigned char this_in = 1, new_in;
38442 enum rid keyword = token->keyword;
38443 tree stmt, attrs, noex;
38445 cp_lexer_consume_token (parser->lexer);
38447 if (keyword == RID_TRANSACTION_RELAXED
38448 || keyword == RID_SYNCHRONIZED)
38449 this_in |= TM_STMT_ATTR_RELAXED;
38450 else
38452 attrs = cp_parser_txn_attribute_opt (parser);
38453 if (attrs)
38454 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38457 /* Parse a noexcept specification. */
38458 if (keyword == RID_ATOMIC_NOEXCEPT)
38459 noex = boolean_true_node;
38460 else if (keyword == RID_ATOMIC_CANCEL)
38462 /* cancel-and-throw is unimplemented. */
38463 sorry ("atomic_cancel");
38464 noex = NULL_TREE;
38466 else
38467 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38469 /* Keep track if we're in the lexical scope of an outer transaction. */
38470 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38472 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38474 parser->in_transaction = new_in;
38475 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38476 parser->in_transaction = old_in;
38478 finish_transaction_stmt (stmt, NULL, this_in, noex);
38480 return stmt;
38483 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38485 transaction-expression:
38486 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38487 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38490 static tree
38491 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38493 unsigned char old_in = parser->in_transaction;
38494 unsigned char this_in = 1;
38495 cp_token *token;
38496 tree expr, noex;
38497 bool noex_expr;
38498 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38500 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38501 || keyword == RID_TRANSACTION_RELAXED);
38503 if (!flag_tm)
38504 error_at (loc,
38505 keyword == RID_TRANSACTION_RELAXED
38506 ? G_("%<__transaction_relaxed%> without transactional memory "
38507 "support enabled")
38508 : G_("%<__transaction_atomic%> without transactional memory "
38509 "support enabled"));
38511 token = cp_parser_require_keyword (parser, keyword,
38512 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38513 : RT_TRANSACTION_RELAXED));
38514 gcc_assert (token != NULL);
38516 if (keyword == RID_TRANSACTION_RELAXED)
38517 this_in |= TM_STMT_ATTR_RELAXED;
38519 /* Set this early. This might mean that we allow transaction_cancel in
38520 an expression that we find out later actually has to be a constexpr.
38521 However, we expect that cxx_constant_value will be able to deal with
38522 this; also, if the noexcept has no constexpr, then what we parse next
38523 really is a transaction's body. */
38524 parser->in_transaction = this_in;
38526 /* Parse a noexcept specification. */
38527 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38528 true);
38530 if (!noex || !noex_expr
38531 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38533 matching_parens parens;
38534 parens.require_open (parser);
38536 expr = cp_parser_expression (parser);
38537 expr = finish_parenthesized_expr (expr);
38539 parens.require_close (parser);
38541 else
38543 /* The only expression that is available got parsed for the noexcept
38544 already. noexcept is true then. */
38545 expr = noex;
38546 noex = boolean_true_node;
38549 expr = build_transaction_expr (token->location, expr, this_in, noex);
38550 parser->in_transaction = old_in;
38552 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38553 return error_mark_node;
38555 return (flag_tm ? expr : error_mark_node);
38558 /* Parse a function-transaction-block.
38560 function-transaction-block:
38561 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38562 function-body
38563 __transaction_atomic txn-attribute[opt] function-try-block
38564 __transaction_relaxed ctor-initializer[opt] function-body
38565 __transaction_relaxed function-try-block
38568 static void
38569 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38571 unsigned char old_in = parser->in_transaction;
38572 unsigned char new_in = 1;
38573 tree compound_stmt, stmt, attrs;
38574 cp_token *token;
38576 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38577 || keyword == RID_TRANSACTION_RELAXED);
38578 token = cp_parser_require_keyword (parser, keyword,
38579 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38580 : RT_TRANSACTION_RELAXED));
38581 gcc_assert (token != NULL);
38583 if (keyword == RID_TRANSACTION_RELAXED)
38584 new_in |= TM_STMT_ATTR_RELAXED;
38585 else
38587 attrs = cp_parser_txn_attribute_opt (parser);
38588 if (attrs)
38589 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38592 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38594 parser->in_transaction = new_in;
38596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38597 cp_parser_function_try_block (parser);
38598 else
38599 cp_parser_ctor_initializer_opt_and_function_body
38600 (parser, /*in_function_try_block=*/false);
38602 parser->in_transaction = old_in;
38604 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38607 /* Parse a __transaction_cancel statement.
38609 cancel-statement:
38610 __transaction_cancel txn-attribute[opt] ;
38611 __transaction_cancel txn-attribute[opt] throw-expression ;
38613 ??? Cancel and throw is not yet implemented. */
38615 static tree
38616 cp_parser_transaction_cancel (cp_parser *parser)
38618 cp_token *token;
38619 bool is_outer = false;
38620 tree stmt, attrs;
38622 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38623 RT_TRANSACTION_CANCEL);
38624 gcc_assert (token != NULL);
38626 attrs = cp_parser_txn_attribute_opt (parser);
38627 if (attrs)
38628 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38630 /* ??? Parse cancel-and-throw here. */
38632 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38634 if (!flag_tm)
38636 error_at (token->location, "%<__transaction_cancel%> without "
38637 "transactional memory support enabled");
38638 return error_mark_node;
38640 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38642 error_at (token->location, "%<__transaction_cancel%> within a "
38643 "%<__transaction_relaxed%>");
38644 return error_mark_node;
38646 else if (is_outer)
38648 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38649 && !is_tm_may_cancel_outer (current_function_decl))
38651 error_at (token->location, "outer %<__transaction_cancel%> not "
38652 "within outer %<__transaction_atomic%>");
38653 error_at (token->location,
38654 " or a %<transaction_may_cancel_outer%> function");
38655 return error_mark_node;
38658 else if (parser->in_transaction == 0)
38660 error_at (token->location, "%<__transaction_cancel%> not within "
38661 "%<__transaction_atomic%>");
38662 return error_mark_node;
38665 stmt = build_tm_abort_call (token->location, is_outer);
38666 add_stmt (stmt);
38668 return stmt;
38671 /* The parser. */
38673 static GTY (()) cp_parser *the_parser;
38676 /* Special handling for the first token or line in the file. The first
38677 thing in the file might be #pragma GCC pch_preprocess, which loads a
38678 PCH file, which is a GC collection point. So we need to handle this
38679 first pragma without benefit of an existing lexer structure.
38681 Always returns one token to the caller in *FIRST_TOKEN. This is
38682 either the true first token of the file, or the first token after
38683 the initial pragma. */
38685 static void
38686 cp_parser_initial_pragma (cp_token *first_token)
38688 tree name = NULL;
38690 cp_lexer_get_preprocessor_token (NULL, first_token);
38691 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38692 return;
38694 cp_lexer_get_preprocessor_token (NULL, first_token);
38695 if (first_token->type == CPP_STRING)
38697 name = first_token->u.value;
38699 cp_lexer_get_preprocessor_token (NULL, first_token);
38700 if (first_token->type != CPP_PRAGMA_EOL)
38701 error_at (first_token->location,
38702 "junk at end of %<#pragma GCC pch_preprocess%>");
38704 else
38705 error_at (first_token->location, "expected string literal");
38707 /* Skip to the end of the pragma. */
38708 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38709 cp_lexer_get_preprocessor_token (NULL, first_token);
38711 /* Now actually load the PCH file. */
38712 if (name)
38713 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38715 /* Read one more token to return to our caller. We have to do this
38716 after reading the PCH file in, since its pointers have to be
38717 live. */
38718 cp_lexer_get_preprocessor_token (NULL, first_token);
38721 /* Parse a pragma GCC ivdep. */
38723 static bool
38724 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38726 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38727 return true;
38730 /* Parse a pragma GCC unroll. */
38732 static unsigned short
38733 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38735 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38736 tree expr = cp_parser_constant_expression (parser);
38737 unsigned short unroll;
38738 expr = maybe_constant_value (expr);
38739 HOST_WIDE_INT lunroll = 0;
38740 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38741 || TREE_CODE (expr) != INTEGER_CST
38742 || (lunroll = tree_to_shwi (expr)) < 0
38743 || lunroll >= USHRT_MAX)
38745 error_at (location, "%<#pragma GCC unroll%> requires an"
38746 " assignment-expression that evaluates to a non-negative"
38747 " integral constant less than %u", USHRT_MAX);
38748 unroll = 0;
38750 else
38752 unroll = (unsigned short)lunroll;
38753 if (unroll == 0)
38754 unroll = 1;
38756 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38757 return unroll;
38760 /* Normal parsing of a pragma token. Here we can (and must) use the
38761 regular lexer. */
38763 static bool
38764 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38766 cp_token *pragma_tok;
38767 unsigned int id;
38768 tree stmt;
38769 bool ret;
38771 pragma_tok = cp_lexer_consume_token (parser->lexer);
38772 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38773 parser->lexer->in_pragma = true;
38775 id = cp_parser_pragma_kind (pragma_tok);
38776 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38777 cp_ensure_no_omp_declare_simd (parser);
38778 switch (id)
38780 case PRAGMA_GCC_PCH_PREPROCESS:
38781 error_at (pragma_tok->location,
38782 "%<#pragma GCC pch_preprocess%> must be first");
38783 break;
38785 case PRAGMA_OMP_BARRIER:
38786 switch (context)
38788 case pragma_compound:
38789 cp_parser_omp_barrier (parser, pragma_tok);
38790 return false;
38791 case pragma_stmt:
38792 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38793 "used in compound statements", "omp barrier");
38794 break;
38795 default:
38796 goto bad_stmt;
38798 break;
38800 case PRAGMA_OMP_FLUSH:
38801 switch (context)
38803 case pragma_compound:
38804 cp_parser_omp_flush (parser, pragma_tok);
38805 return false;
38806 case pragma_stmt:
38807 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38808 "used in compound statements", "omp flush");
38809 break;
38810 default:
38811 goto bad_stmt;
38813 break;
38815 case PRAGMA_OMP_TASKWAIT:
38816 switch (context)
38818 case pragma_compound:
38819 cp_parser_omp_taskwait (parser, pragma_tok);
38820 return false;
38821 case pragma_stmt:
38822 error_at (pragma_tok->location,
38823 "%<#pragma %s%> may only be used in compound statements",
38824 "omp taskwait");
38825 break;
38826 default:
38827 goto bad_stmt;
38829 break;
38831 case PRAGMA_OMP_TASKYIELD:
38832 switch (context)
38834 case pragma_compound:
38835 cp_parser_omp_taskyield (parser, pragma_tok);
38836 return false;
38837 case pragma_stmt:
38838 error_at (pragma_tok->location,
38839 "%<#pragma %s%> may only be used in compound statements",
38840 "omp taskyield");
38841 break;
38842 default:
38843 goto bad_stmt;
38845 break;
38847 case PRAGMA_OMP_CANCEL:
38848 switch (context)
38850 case pragma_compound:
38851 cp_parser_omp_cancel (parser, pragma_tok);
38852 return false;
38853 case pragma_stmt:
38854 error_at (pragma_tok->location,
38855 "%<#pragma %s%> may only be used in compound statements",
38856 "omp cancel");
38857 break;
38858 default:
38859 goto bad_stmt;
38861 break;
38863 case PRAGMA_OMP_CANCELLATION_POINT:
38864 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38865 return false;
38867 case PRAGMA_OMP_THREADPRIVATE:
38868 cp_parser_omp_threadprivate (parser, pragma_tok);
38869 return false;
38871 case PRAGMA_OMP_DECLARE:
38872 return cp_parser_omp_declare (parser, pragma_tok, context);
38874 case PRAGMA_OACC_DECLARE:
38875 cp_parser_oacc_declare (parser, pragma_tok);
38876 return false;
38878 case PRAGMA_OACC_ENTER_DATA:
38879 if (context == pragma_stmt)
38881 error_at (pragma_tok->location,
38882 "%<#pragma %s%> may only be used in compound statements",
38883 "acc enter data");
38884 break;
38886 else if (context != pragma_compound)
38887 goto bad_stmt;
38888 cp_parser_omp_construct (parser, pragma_tok, if_p);
38889 return true;
38891 case PRAGMA_OACC_EXIT_DATA:
38892 if (context == pragma_stmt)
38894 error_at (pragma_tok->location,
38895 "%<#pragma %s%> may only be used in compound statements",
38896 "acc exit data");
38897 break;
38899 else if (context != pragma_compound)
38900 goto bad_stmt;
38901 cp_parser_omp_construct (parser, pragma_tok, if_p);
38902 return true;
38904 case PRAGMA_OACC_ROUTINE:
38905 if (context != pragma_external)
38907 error_at (pragma_tok->location,
38908 "%<#pragma acc routine%> must be at file scope");
38909 break;
38911 cp_parser_oacc_routine (parser, pragma_tok, context);
38912 return false;
38914 case PRAGMA_OACC_UPDATE:
38915 if (context == pragma_stmt)
38917 error_at (pragma_tok->location,
38918 "%<#pragma %s%> may only be used in compound statements",
38919 "acc update");
38920 break;
38922 else if (context != pragma_compound)
38923 goto bad_stmt;
38924 cp_parser_omp_construct (parser, pragma_tok, if_p);
38925 return true;
38927 case PRAGMA_OACC_WAIT:
38928 if (context == pragma_stmt)
38930 error_at (pragma_tok->location,
38931 "%<#pragma %s%> may only be used in compound statements",
38932 "acc wait");
38933 break;
38935 else if (context != pragma_compound)
38936 goto bad_stmt;
38937 cp_parser_omp_construct (parser, pragma_tok, if_p);
38938 return true;
38940 case PRAGMA_OACC_ATOMIC:
38941 case PRAGMA_OACC_CACHE:
38942 case PRAGMA_OACC_DATA:
38943 case PRAGMA_OACC_HOST_DATA:
38944 case PRAGMA_OACC_KERNELS:
38945 case PRAGMA_OACC_PARALLEL:
38946 case PRAGMA_OACC_LOOP:
38947 case PRAGMA_OMP_ATOMIC:
38948 case PRAGMA_OMP_CRITICAL:
38949 case PRAGMA_OMP_DISTRIBUTE:
38950 case PRAGMA_OMP_FOR:
38951 case PRAGMA_OMP_MASTER:
38952 case PRAGMA_OMP_PARALLEL:
38953 case PRAGMA_OMP_SECTIONS:
38954 case PRAGMA_OMP_SIMD:
38955 case PRAGMA_OMP_SINGLE:
38956 case PRAGMA_OMP_TASK:
38957 case PRAGMA_OMP_TASKGROUP:
38958 case PRAGMA_OMP_TASKLOOP:
38959 case PRAGMA_OMP_TEAMS:
38960 if (context != pragma_stmt && context != pragma_compound)
38961 goto bad_stmt;
38962 stmt = push_omp_privatization_clauses (false);
38963 cp_parser_omp_construct (parser, pragma_tok, if_p);
38964 pop_omp_privatization_clauses (stmt);
38965 return true;
38967 case PRAGMA_OMP_ORDERED:
38968 if (context != pragma_stmt && context != pragma_compound)
38969 goto bad_stmt;
38970 stmt = push_omp_privatization_clauses (false);
38971 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38972 pop_omp_privatization_clauses (stmt);
38973 return ret;
38975 case PRAGMA_OMP_TARGET:
38976 if (context != pragma_stmt && context != pragma_compound)
38977 goto bad_stmt;
38978 stmt = push_omp_privatization_clauses (false);
38979 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38980 pop_omp_privatization_clauses (stmt);
38981 return ret;
38983 case PRAGMA_OMP_END_DECLARE_TARGET:
38984 cp_parser_omp_end_declare_target (parser, pragma_tok);
38985 return false;
38987 case PRAGMA_OMP_SECTION:
38988 error_at (pragma_tok->location,
38989 "%<#pragma omp section%> may only be used in "
38990 "%<#pragma omp sections%> construct");
38991 break;
38993 case PRAGMA_IVDEP:
38995 if (context == pragma_external)
38997 error_at (pragma_tok->location,
38998 "%<#pragma GCC ivdep%> must be inside a function");
38999 break;
39001 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39002 unsigned short unroll;
39003 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39004 if (tok->type == CPP_PRAGMA
39005 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39007 tok = cp_lexer_consume_token (parser->lexer);
39008 unroll = cp_parser_pragma_unroll (parser, tok);
39009 tok = cp_lexer_peek_token (the_parser->lexer);
39011 else
39012 unroll = 0;
39013 if (tok->type != CPP_KEYWORD
39014 || (tok->keyword != RID_FOR
39015 && tok->keyword != RID_WHILE
39016 && tok->keyword != RID_DO))
39018 cp_parser_error (parser, "for, while or do statement expected");
39019 return false;
39021 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39022 return true;
39025 case PRAGMA_UNROLL:
39027 if (context == pragma_external)
39029 error_at (pragma_tok->location,
39030 "%<#pragma GCC unroll%> must be inside a function");
39031 break;
39033 const unsigned short unroll
39034 = cp_parser_pragma_unroll (parser, pragma_tok);
39035 bool ivdep;
39036 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39037 if (tok->type == CPP_PRAGMA
39038 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39040 tok = cp_lexer_consume_token (parser->lexer);
39041 ivdep = cp_parser_pragma_ivdep (parser, tok);
39042 tok = cp_lexer_peek_token (the_parser->lexer);
39044 else
39045 ivdep = false;
39046 if (tok->type != CPP_KEYWORD
39047 || (tok->keyword != RID_FOR
39048 && tok->keyword != RID_WHILE
39049 && tok->keyword != RID_DO))
39051 cp_parser_error (parser, "for, while or do statement expected");
39052 return false;
39054 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39055 return true;
39058 default:
39059 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39060 c_invoke_pragma_handler (id);
39061 break;
39063 bad_stmt:
39064 cp_parser_error (parser, "expected declaration specifiers");
39065 break;
39068 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39069 return false;
39072 /* The interface the pragma parsers have to the lexer. */
39074 enum cpp_ttype
39075 pragma_lex (tree *value, location_t *loc)
39077 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39078 enum cpp_ttype ret = tok->type;
39080 *value = tok->u.value;
39081 if (loc)
39082 *loc = tok->location;
39084 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39085 ret = CPP_EOF;
39086 else if (ret == CPP_STRING)
39087 *value = cp_parser_string_literal (the_parser, false, false);
39088 else
39090 if (ret == CPP_KEYWORD)
39091 ret = CPP_NAME;
39092 cp_lexer_consume_token (the_parser->lexer);
39095 return ret;
39099 /* External interface. */
39101 /* Parse one entire translation unit. */
39103 void
39104 c_parse_file (void)
39106 static bool already_called = false;
39108 if (already_called)
39109 fatal_error (input_location,
39110 "inter-module optimizations not implemented for C++");
39111 already_called = true;
39113 the_parser = cp_parser_new ();
39114 push_deferring_access_checks (flag_access_control
39115 ? dk_no_deferred : dk_no_check);
39116 cp_parser_translation_unit (the_parser);
39117 the_parser = NULL;
39120 /* Create an identifier for a generic parameter type (a synthesized
39121 template parameter implied by `auto' or a concept identifier). */
39123 static GTY(()) int generic_parm_count;
39124 static tree
39125 make_generic_type_name ()
39127 char buf[32];
39128 sprintf (buf, "auto:%d", ++generic_parm_count);
39129 return get_identifier (buf);
39132 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39133 (creating a new template parameter list if necessary). Returns the newly
39134 created template type parm. */
39136 static tree
39137 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39139 gcc_assert (current_binding_level->kind == sk_function_parms);
39141 /* Before committing to modifying any scope, if we're in an
39142 implicit template scope, and we're trying to synthesize a
39143 constrained parameter, try to find a previous parameter with
39144 the same name. This is the same-type rule for abbreviated
39145 function templates.
39147 NOTE: We can generate implicit parameters when tentatively
39148 parsing a nested name specifier, only to reject that parse
39149 later. However, matching the same template-id as part of a
39150 direct-declarator should generate an identical template
39151 parameter, so this rule will merge them. */
39152 if (parser->implicit_template_scope && constr)
39154 tree t = parser->implicit_template_parms;
39155 while (t)
39157 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39159 tree d = TREE_VALUE (t);
39160 if (TREE_CODE (d) == PARM_DECL)
39161 /* Return the TEMPLATE_PARM_INDEX. */
39162 d = DECL_INITIAL (d);
39163 return d;
39165 t = TREE_CHAIN (t);
39169 /* We are either continuing a function template that already contains implicit
39170 template parameters, creating a new fully-implicit function template, or
39171 extending an existing explicit function template with implicit template
39172 parameters. */
39174 cp_binding_level *const entry_scope = current_binding_level;
39176 bool become_template = false;
39177 cp_binding_level *parent_scope = 0;
39179 if (parser->implicit_template_scope)
39181 gcc_assert (parser->implicit_template_parms);
39183 current_binding_level = parser->implicit_template_scope;
39185 else
39187 /* Roll back to the existing template parameter scope (in the case of
39188 extending an explicit function template) or introduce a new template
39189 parameter scope ahead of the function parameter scope (or class scope
39190 in the case of out-of-line member definitions). The function scope is
39191 added back after template parameter synthesis below. */
39193 cp_binding_level *scope = entry_scope;
39195 while (scope->kind == sk_function_parms)
39197 parent_scope = scope;
39198 scope = scope->level_chain;
39200 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39202 /* If not defining a class, then any class scope is a scope level in
39203 an out-of-line member definition. In this case simply wind back
39204 beyond the first such scope to inject the template parameter list.
39205 Otherwise wind back to the class being defined. The latter can
39206 occur in class member friend declarations such as:
39208 class A {
39209 void foo (auto);
39211 class B {
39212 friend void A::foo (auto);
39215 The template parameter list synthesized for the friend declaration
39216 must be injected in the scope of 'B'. This can also occur in
39217 erroneous cases such as:
39219 struct A {
39220 struct B {
39221 void foo (auto);
39223 void B::foo (auto) {}
39226 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39227 but, nevertheless, the template parameter list synthesized for the
39228 declarator should be injected into the scope of 'A' as if the
39229 ill-formed template was specified explicitly. */
39231 while (scope->kind == sk_class && !scope->defining_class_p)
39233 parent_scope = scope;
39234 scope = scope->level_chain;
39238 current_binding_level = scope;
39240 if (scope->kind != sk_template_parms
39241 || !function_being_declared_is_template_p (parser))
39243 /* Introduce a new template parameter list for implicit template
39244 parameters. */
39246 become_template = true;
39248 parser->implicit_template_scope
39249 = begin_scope (sk_template_parms, NULL);
39251 ++processing_template_decl;
39253 parser->fully_implicit_function_template_p = true;
39254 ++parser->num_template_parameter_lists;
39256 else
39258 /* Synthesize implicit template parameters at the end of the explicit
39259 template parameter list. */
39261 gcc_assert (current_template_parms);
39263 parser->implicit_template_scope = scope;
39265 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39266 parser->implicit_template_parms
39267 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39271 /* Synthesize a new template parameter and track the current template
39272 parameter chain with implicit_template_parms. */
39274 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39275 tree synth_id = make_generic_type_name ();
39276 tree synth_tmpl_parm;
39277 bool non_type = false;
39279 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39280 synth_tmpl_parm
39281 = finish_template_type_parm (class_type_node, synth_id);
39282 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39283 synth_tmpl_parm
39284 = finish_constrained_template_template_parm (proto, synth_id);
39285 else
39287 synth_tmpl_parm = copy_decl (proto);
39288 DECL_NAME (synth_tmpl_parm) = synth_id;
39289 non_type = true;
39292 // Attach the constraint to the parm before processing.
39293 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39294 TREE_TYPE (node) = constr;
39295 tree new_parm
39296 = process_template_parm (parser->implicit_template_parms,
39297 input_location,
39298 node,
39299 /*non_type=*/non_type,
39300 /*param_pack=*/false);
39302 // Chain the new parameter to the list of implicit parameters.
39303 if (parser->implicit_template_parms)
39304 parser->implicit_template_parms
39305 = TREE_CHAIN (parser->implicit_template_parms);
39306 else
39307 parser->implicit_template_parms = new_parm;
39309 tree new_decl = get_local_decls ();
39310 if (non_type)
39311 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39312 new_decl = DECL_INITIAL (new_decl);
39314 /* If creating a fully implicit function template, start the new implicit
39315 template parameter list with this synthesized type, otherwise grow the
39316 current template parameter list. */
39318 if (become_template)
39320 parent_scope->level_chain = current_binding_level;
39322 tree new_parms = make_tree_vec (1);
39323 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39324 current_template_parms = tree_cons (size_int (processing_template_decl),
39325 new_parms, current_template_parms);
39327 else
39329 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39330 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39331 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39332 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39335 // If the new parameter was constrained, we need to add that to the
39336 // constraints in the template parameter list.
39337 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39339 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39340 reqs = conjoin_constraints (reqs, req);
39341 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39344 current_binding_level = entry_scope;
39346 return new_decl;
39349 /* Finish the declaration of a fully implicit function template. Such a
39350 template has no explicit template parameter list so has not been through the
39351 normal template head and tail processing. synthesize_implicit_template_parm
39352 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39353 provided if the declaration is a class member such that its template
39354 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39355 form is returned. Otherwise NULL_TREE is returned. */
39357 static tree
39358 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39360 gcc_assert (parser->fully_implicit_function_template_p);
39362 if (member_decl_opt && member_decl_opt != error_mark_node
39363 && DECL_VIRTUAL_P (member_decl_opt))
39365 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39366 "implicit templates may not be %<virtual%>");
39367 DECL_VIRTUAL_P (member_decl_opt) = false;
39370 if (member_decl_opt)
39371 member_decl_opt = finish_member_template_decl (member_decl_opt);
39372 end_template_decl ();
39374 parser->fully_implicit_function_template_p = false;
39375 parser->implicit_template_parms = 0;
39376 parser->implicit_template_scope = 0;
39377 --parser->num_template_parameter_lists;
39379 return member_decl_opt;
39382 /* Like finish_fully_implicit_template, but to be used in error
39383 recovery, rearranging scopes so that we restore the state we had
39384 before synthesize_implicit_template_parm inserted the implement
39385 template parms scope. */
39387 static void
39388 abort_fully_implicit_template (cp_parser *parser)
39390 cp_binding_level *return_to_scope = current_binding_level;
39392 if (parser->implicit_template_scope
39393 && return_to_scope != parser->implicit_template_scope)
39395 cp_binding_level *child = return_to_scope;
39396 for (cp_binding_level *scope = child->level_chain;
39397 scope != parser->implicit_template_scope;
39398 scope = child->level_chain)
39399 child = scope;
39400 child->level_chain = parser->implicit_template_scope->level_chain;
39401 parser->implicit_template_scope->level_chain = return_to_scope;
39402 current_binding_level = parser->implicit_template_scope;
39404 else
39405 return_to_scope = return_to_scope->level_chain;
39407 finish_fully_implicit_template (parser, NULL);
39409 gcc_assert (current_binding_level == return_to_scope);
39412 /* Helper function for diagnostics that have complained about things
39413 being used with 'extern "C"' linkage.
39415 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39417 void
39418 maybe_show_extern_c_location (void)
39420 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39421 inform (the_parser->innermost_linkage_specification_location,
39422 "%<extern \"C\"%> linkage started here");
39425 #include "gt-cp-parser.h"