C++: less verbose error-recovery for version conflict markers
[official-gcc.git] / gcc / cp / parser.c
bloba076de146e6c4e7ab5db159171fcaaf8278b245c
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)) = get_identifier ("__closure");
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;
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,
11956 get_identifier ("__for_range"), 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,
12064 get_identifier ("__for_begin"), 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,
12075 get_identifier ("__for_end"), iter_type);
12076 TREE_USED (end) = 1;
12077 DECL_ARTIFICIAL (end) = 1;
12078 pushdecl (end);
12079 cp_finish_decl (end, end_expr,
12080 /*is_constant_init*/false, NULL_TREE,
12081 LOOKUP_ONLYCONVERTING);
12083 finish_init_stmt (statement);
12085 /* The new for condition. */
12086 condition = build_x_binary_op (input_location, NE_EXPR,
12087 begin, ERROR_MARK,
12088 end, ERROR_MARK,
12089 NULL, tf_warning_or_error);
12090 finish_for_cond (condition, statement, ivdep, unroll);
12092 /* The new increment expression. */
12093 expression = finish_unary_op_expr (input_location,
12094 PREINCREMENT_EXPR, begin,
12095 tf_warning_or_error);
12096 finish_for_expr (expression, statement);
12098 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12099 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12101 /* The declaration is initialized with *__begin inside the loop body. */
12102 cp_finish_decl (range_decl,
12103 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12104 tf_warning_or_error),
12105 /*is_constant_init*/false, NULL_TREE,
12106 LOOKUP_ONLYCONVERTING);
12107 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12108 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12110 return statement;
12113 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12114 We need to solve both at the same time because the method used
12115 depends on the existence of members begin or end.
12116 Returns the type deduced for the iterator expression. */
12118 static tree
12119 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12121 if (error_operand_p (range))
12123 *begin = *end = error_mark_node;
12124 return error_mark_node;
12127 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12129 error ("range-based %<for%> expression of type %qT "
12130 "has incomplete type", TREE_TYPE (range));
12131 *begin = *end = error_mark_node;
12132 return error_mark_node;
12134 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12136 /* If RANGE is an array, we will use pointer arithmetic. */
12137 *begin = decay_conversion (range, tf_warning_or_error);
12138 *end = build_binary_op (input_location, PLUS_EXPR,
12139 range,
12140 array_type_nelts_top (TREE_TYPE (range)),
12141 false);
12142 return TREE_TYPE (*begin);
12144 else
12146 /* If it is not an array, we must do a bit of magic. */
12147 tree id_begin, id_end;
12148 tree member_begin, member_end;
12150 *begin = *end = error_mark_node;
12152 id_begin = get_identifier ("begin");
12153 id_end = get_identifier ("end");
12154 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12155 /*protect=*/2, /*want_type=*/false,
12156 tf_warning_or_error);
12157 member_end = lookup_member (TREE_TYPE (range), id_end,
12158 /*protect=*/2, /*want_type=*/false,
12159 tf_warning_or_error);
12161 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12163 /* Use the member functions. */
12164 *begin = cp_parser_range_for_member_function (range, id_begin);
12165 *end = cp_parser_range_for_member_function (range, id_end);
12167 else
12169 /* Use global functions with ADL. */
12170 vec<tree, va_gc> *vec;
12171 vec = make_tree_vector ();
12173 vec_safe_push (vec, range);
12175 member_begin = perform_koenig_lookup (id_begin, vec,
12176 tf_warning_or_error);
12177 *begin = finish_call_expr (member_begin, &vec, false, true,
12178 tf_warning_or_error);
12179 member_end = perform_koenig_lookup (id_end, vec,
12180 tf_warning_or_error);
12181 *end = finish_call_expr (member_end, &vec, false, true,
12182 tf_warning_or_error);
12184 release_tree_vector (vec);
12187 /* Last common checks. */
12188 if (*begin == error_mark_node || *end == error_mark_node)
12190 /* If one of the expressions is an error do no more checks. */
12191 *begin = *end = error_mark_node;
12192 return error_mark_node;
12194 else if (type_dependent_expression_p (*begin)
12195 || type_dependent_expression_p (*end))
12196 /* Can happen, when, eg, in a template context, Koenig lookup
12197 can't resolve begin/end (c++/58503). */
12198 return NULL_TREE;
12199 else
12201 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12202 /* The unqualified type of the __begin and __end temporaries should
12203 be the same, as required by the multiple auto declaration. */
12204 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12206 if (cxx_dialect >= cxx17
12207 && (build_x_binary_op (input_location, NE_EXPR,
12208 *begin, ERROR_MARK,
12209 *end, ERROR_MARK,
12210 NULL, tf_none)
12211 != error_mark_node))
12212 /* P0184R0 allows __begin and __end to have different types,
12213 but make sure they are comparable so we can give a better
12214 diagnostic. */;
12215 else
12216 error ("inconsistent begin/end types in range-based %<for%> "
12217 "statement: %qT and %qT",
12218 TREE_TYPE (*begin), TREE_TYPE (*end));
12220 return iter_type;
12225 /* Helper function for cp_parser_perform_range_for_lookup.
12226 Builds a tree for RANGE.IDENTIFIER(). */
12228 static tree
12229 cp_parser_range_for_member_function (tree range, tree identifier)
12231 tree member, res;
12232 vec<tree, va_gc> *vec;
12234 member = finish_class_member_access_expr (range, identifier,
12235 false, tf_warning_or_error);
12236 if (member == error_mark_node)
12237 return error_mark_node;
12239 vec = make_tree_vector ();
12240 res = finish_call_expr (member, &vec,
12241 /*disallow_virtual=*/false,
12242 /*koenig_p=*/false,
12243 tf_warning_or_error);
12244 release_tree_vector (vec);
12245 return res;
12248 /* Parse an iteration-statement.
12250 iteration-statement:
12251 while ( condition ) statement
12252 do statement while ( expression ) ;
12253 for ( init-statement condition [opt] ; expression [opt] )
12254 statement
12256 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12258 static tree
12259 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12260 unsigned short unroll)
12262 cp_token *token;
12263 enum rid keyword;
12264 tree statement;
12265 unsigned char in_statement;
12266 token_indent_info guard_tinfo;
12268 /* Peek at the next token. */
12269 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12270 if (!token)
12271 return error_mark_node;
12273 guard_tinfo = get_token_indent_info (token);
12275 /* Remember whether or not we are already within an iteration
12276 statement. */
12277 in_statement = parser->in_statement;
12279 /* See what kind of keyword it is. */
12280 keyword = token->keyword;
12281 switch (keyword)
12283 case RID_WHILE:
12285 tree condition;
12287 /* Begin the while-statement. */
12288 statement = begin_while_stmt ();
12289 /* Look for the `('. */
12290 matching_parens parens;
12291 parens.require_open (parser);
12292 /* Parse the condition. */
12293 condition = cp_parser_condition (parser);
12294 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12295 /* Look for the `)'. */
12296 parens.require_close (parser);
12297 /* Parse the dependent statement. */
12298 parser->in_statement = IN_ITERATION_STMT;
12299 bool prev = note_iteration_stmt_body_start ();
12300 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12301 note_iteration_stmt_body_end (prev);
12302 parser->in_statement = in_statement;
12303 /* We're done with the while-statement. */
12304 finish_while_stmt (statement);
12306 break;
12308 case RID_DO:
12310 tree expression;
12312 /* Begin the do-statement. */
12313 statement = begin_do_stmt ();
12314 /* Parse the body of the do-statement. */
12315 parser->in_statement = IN_ITERATION_STMT;
12316 bool prev = note_iteration_stmt_body_start ();
12317 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12318 note_iteration_stmt_body_end (prev);
12319 parser->in_statement = in_statement;
12320 finish_do_body (statement);
12321 /* Look for the `while' keyword. */
12322 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12323 /* Look for the `('. */
12324 matching_parens parens;
12325 parens.require_open (parser);
12326 /* Parse the expression. */
12327 expression = cp_parser_expression (parser);
12328 /* We're done with the do-statement. */
12329 finish_do_stmt (expression, statement, ivdep, unroll);
12330 /* Look for the `)'. */
12331 parens.require_close (parser);
12332 /* Look for the `;'. */
12333 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12335 break;
12337 case RID_FOR:
12339 /* Look for the `('. */
12340 matching_parens parens;
12341 parens.require_open (parser);
12343 statement = cp_parser_for (parser, ivdep, unroll);
12345 /* Look for the `)'. */
12346 parens.require_close (parser);
12348 /* Parse the body of the for-statement. */
12349 parser->in_statement = IN_ITERATION_STMT;
12350 bool prev = note_iteration_stmt_body_start ();
12351 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12352 note_iteration_stmt_body_end (prev);
12353 parser->in_statement = in_statement;
12355 /* We're done with the for-statement. */
12356 finish_for_stmt (statement);
12358 break;
12360 default:
12361 cp_parser_error (parser, "expected iteration-statement");
12362 statement = error_mark_node;
12363 break;
12366 return statement;
12369 /* Parse a init-statement or the declarator of a range-based-for.
12370 Returns true if a range-based-for declaration is seen.
12372 init-statement:
12373 expression-statement
12374 simple-declaration */
12376 static bool
12377 cp_parser_init_statement (cp_parser *parser, tree *decl)
12379 /* If the next token is a `;', then we have an empty
12380 expression-statement. Grammatically, this is also a
12381 simple-declaration, but an invalid one, because it does not
12382 declare anything. Therefore, if we did not handle this case
12383 specially, we would issue an error message about an invalid
12384 declaration. */
12385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12387 bool is_range_for = false;
12388 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12390 /* Try to parse the init-statement. */
12391 if (cp_parser_range_based_for_with_init_p (parser))
12393 tree dummy;
12394 cp_parser_parse_tentatively (parser);
12395 /* Parse the declaration. */
12396 cp_parser_simple_declaration (parser,
12397 /*function_definition_allowed_p=*/false,
12398 &dummy);
12399 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12400 if (!cp_parser_parse_definitely (parser))
12401 /* That didn't work, try to parse it as an expression-statement. */
12402 cp_parser_expression_statement (parser, NULL_TREE);
12404 if (cxx_dialect < cxx2a)
12406 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12407 "range-based %<for%> loops with initializer only "
12408 "available with -std=c++2a or -std=gnu++2a");
12409 *decl = error_mark_node;
12413 /* A colon is used in range-based for. */
12414 parser->colon_corrects_to_scope_p = false;
12416 /* We're going to speculatively look for a declaration, falling back
12417 to an expression, if necessary. */
12418 cp_parser_parse_tentatively (parser);
12419 /* Parse the declaration. */
12420 cp_parser_simple_declaration (parser,
12421 /*function_definition_allowed_p=*/false,
12422 decl);
12423 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12424 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12426 /* It is a range-for, consume the ':'. */
12427 cp_lexer_consume_token (parser->lexer);
12428 is_range_for = true;
12429 if (cxx_dialect < cxx11)
12430 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12431 "range-based %<for%> loops only available with "
12432 "-std=c++11 or -std=gnu++11");
12434 else
12435 /* The ';' is not consumed yet because we told
12436 cp_parser_simple_declaration not to. */
12437 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12439 if (cp_parser_parse_definitely (parser))
12440 return is_range_for;
12441 /* If the tentative parse failed, then we shall need to look for an
12442 expression-statement. */
12444 /* If we are here, it is an expression-statement. */
12445 cp_parser_expression_statement (parser, NULL_TREE);
12446 return false;
12449 /* Parse a jump-statement.
12451 jump-statement:
12452 break ;
12453 continue ;
12454 return expression [opt] ;
12455 return braced-init-list ;
12456 goto identifier ;
12458 GNU extension:
12460 jump-statement:
12461 goto * expression ;
12463 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12465 static tree
12466 cp_parser_jump_statement (cp_parser* parser)
12468 tree statement = error_mark_node;
12469 cp_token *token;
12470 enum rid keyword;
12471 unsigned char in_statement;
12473 /* Peek at the next token. */
12474 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12475 if (!token)
12476 return error_mark_node;
12478 /* See what kind of keyword it is. */
12479 keyword = token->keyword;
12480 switch (keyword)
12482 case RID_BREAK:
12483 in_statement = parser->in_statement & ~IN_IF_STMT;
12484 switch (in_statement)
12486 case 0:
12487 error_at (token->location, "break statement not within loop or switch");
12488 break;
12489 default:
12490 gcc_assert ((in_statement & IN_SWITCH_STMT)
12491 || in_statement == IN_ITERATION_STMT);
12492 statement = finish_break_stmt ();
12493 if (in_statement == IN_ITERATION_STMT)
12494 break_maybe_infinite_loop ();
12495 break;
12496 case IN_OMP_BLOCK:
12497 error_at (token->location, "invalid exit from OpenMP structured block");
12498 break;
12499 case IN_OMP_FOR:
12500 error_at (token->location, "break statement used with OpenMP for loop");
12501 break;
12503 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12504 break;
12506 case RID_CONTINUE:
12507 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12509 case 0:
12510 error_at (token->location, "continue statement not within a loop");
12511 break;
12512 /* Fall through. */
12513 case IN_ITERATION_STMT:
12514 case IN_OMP_FOR:
12515 statement = finish_continue_stmt ();
12516 break;
12517 case IN_OMP_BLOCK:
12518 error_at (token->location, "invalid exit from OpenMP structured block");
12519 break;
12520 default:
12521 gcc_unreachable ();
12523 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12524 break;
12526 case RID_RETURN:
12528 tree expr;
12529 bool expr_non_constant_p;
12531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12533 cp_lexer_set_source_position (parser->lexer);
12534 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12535 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12537 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12538 expr = cp_parser_expression (parser);
12539 else
12540 /* If the next token is a `;', then there is no
12541 expression. */
12542 expr = NULL_TREE;
12543 /* Build the return-statement. */
12544 if (current_function_auto_return_pattern && in_discarded_stmt)
12545 /* Don't deduce from a discarded return statement. */;
12546 else
12547 statement = finish_return_stmt (expr);
12548 /* Look for the final `;'. */
12549 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12551 break;
12553 case RID_GOTO:
12554 if (parser->in_function_body
12555 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12557 error ("%<goto%> in %<constexpr%> function");
12558 cp_function_chain->invalid_constexpr = true;
12561 /* Create the goto-statement. */
12562 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12564 /* Issue a warning about this use of a GNU extension. */
12565 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12566 /* Consume the '*' token. */
12567 cp_lexer_consume_token (parser->lexer);
12568 /* Parse the dependent expression. */
12569 finish_goto_stmt (cp_parser_expression (parser));
12571 else
12572 finish_goto_stmt (cp_parser_identifier (parser));
12573 /* Look for the final `;'. */
12574 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12575 break;
12577 default:
12578 cp_parser_error (parser, "expected jump-statement");
12579 break;
12582 return statement;
12585 /* Parse a declaration-statement.
12587 declaration-statement:
12588 block-declaration */
12590 static void
12591 cp_parser_declaration_statement (cp_parser* parser)
12593 void *p;
12595 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12596 p = obstack_alloc (&declarator_obstack, 0);
12598 /* Parse the block-declaration. */
12599 cp_parser_block_declaration (parser, /*statement_p=*/true);
12601 /* Free any declarators allocated. */
12602 obstack_free (&declarator_obstack, p);
12605 /* Some dependent statements (like `if (cond) statement'), are
12606 implicitly in their own scope. In other words, if the statement is
12607 a single statement (as opposed to a compound-statement), it is
12608 none-the-less treated as if it were enclosed in braces. Any
12609 declarations appearing in the dependent statement are out of scope
12610 after control passes that point. This function parses a statement,
12611 but ensures that is in its own scope, even if it is not a
12612 compound-statement.
12614 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12615 is a (possibly labeled) if statement which is not enclosed in
12616 braces and has an else clause. This is used to implement
12617 -Wparentheses.
12619 CHAIN is a vector of if-else-if conditions. This is used to implement
12620 -Wduplicated-cond.
12622 Returns the new statement. */
12624 static tree
12625 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12626 const token_indent_info &guard_tinfo,
12627 vec<tree> *chain)
12629 tree statement;
12630 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12631 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12632 token_indent_info body_tinfo
12633 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12635 if (if_p != NULL)
12636 *if_p = false;
12638 /* Mark if () ; with a special NOP_EXPR. */
12639 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12641 cp_lexer_consume_token (parser->lexer);
12642 statement = add_stmt (build_empty_stmt (body_loc));
12644 if (guard_tinfo.keyword == RID_IF
12645 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12646 warning_at (body_loc, OPT_Wempty_body,
12647 "suggest braces around empty body in an %<if%> statement");
12648 else if (guard_tinfo.keyword == RID_ELSE)
12649 warning_at (body_loc, OPT_Wempty_body,
12650 "suggest braces around empty body in an %<else%> statement");
12652 /* if a compound is opened, we simply parse the statement directly. */
12653 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12654 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12655 /* If the token is not a `{', then we must take special action. */
12656 else
12658 /* Create a compound-statement. */
12659 statement = begin_compound_stmt (0);
12660 /* Parse the dependent-statement. */
12661 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12662 &body_loc_after_labels);
12663 /* Finish the dummy compound-statement. */
12664 finish_compound_stmt (statement);
12667 token_indent_info next_tinfo
12668 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12669 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12671 if (body_loc_after_labels != UNKNOWN_LOCATION
12672 && next_tinfo.type != CPP_SEMICOLON)
12673 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12674 guard_tinfo.location, guard_tinfo.keyword);
12676 /* Return the statement. */
12677 return statement;
12680 /* For some dependent statements (like `while (cond) statement'), we
12681 have already created a scope. Therefore, even if the dependent
12682 statement is a compound-statement, we do not want to create another
12683 scope. */
12685 static void
12686 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12687 const token_indent_info &guard_tinfo)
12689 /* If the token is a `{', then we must take special action. */
12690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12692 token_indent_info body_tinfo
12693 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12694 location_t loc_after_labels = UNKNOWN_LOCATION;
12696 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12697 &loc_after_labels);
12698 token_indent_info next_tinfo
12699 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12700 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12702 if (loc_after_labels != UNKNOWN_LOCATION
12703 && next_tinfo.type != CPP_SEMICOLON)
12704 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12705 guard_tinfo.location,
12706 guard_tinfo.keyword);
12708 else
12710 /* Avoid calling cp_parser_compound_statement, so that we
12711 don't create a new scope. Do everything else by hand. */
12712 matching_braces braces;
12713 braces.require_open (parser);
12714 /* If the next keyword is `__label__' we have a label declaration. */
12715 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12716 cp_parser_label_declaration (parser);
12717 /* Parse an (optional) statement-seq. */
12718 cp_parser_statement_seq_opt (parser, NULL_TREE);
12719 braces.require_close (parser);
12723 /* Declarations [gram.dcl.dcl] */
12725 /* Parse an optional declaration-sequence.
12727 declaration-seq:
12728 declaration
12729 declaration-seq declaration */
12731 static void
12732 cp_parser_declaration_seq_opt (cp_parser* parser)
12734 while (true)
12736 cp_token *token;
12738 token = cp_lexer_peek_token (parser->lexer);
12740 if (token->type == CPP_CLOSE_BRACE
12741 || token->type == CPP_EOF
12742 || token->type == CPP_PRAGMA_EOL)
12743 break;
12745 if (token->type == CPP_SEMICOLON)
12747 /* A declaration consisting of a single semicolon is
12748 invalid. Allow it unless we're being pedantic. */
12749 cp_lexer_consume_token (parser->lexer);
12750 if (!in_system_header_at (input_location))
12751 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12752 continue;
12755 /* If we're entering or exiting a region that's implicitly
12756 extern "C", modify the lang context appropriately. */
12757 if (!parser->implicit_extern_c && token->implicit_extern_c)
12759 push_lang_context (lang_name_c);
12760 parser->implicit_extern_c = true;
12762 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12764 pop_lang_context ();
12765 parser->implicit_extern_c = false;
12768 if (token->type == CPP_PRAGMA)
12770 /* A top-level declaration can consist solely of a #pragma.
12771 A nested declaration cannot, so this is done here and not
12772 in cp_parser_declaration. (A #pragma at block scope is
12773 handled in cp_parser_statement.) */
12774 cp_parser_pragma (parser, pragma_external, NULL);
12775 continue;
12778 /* Parse the declaration itself. */
12779 cp_parser_declaration (parser);
12783 /* Parse a declaration.
12785 declaration:
12786 block-declaration
12787 function-definition
12788 template-declaration
12789 explicit-instantiation
12790 explicit-specialization
12791 linkage-specification
12792 namespace-definition
12794 C++17:
12795 deduction-guide
12797 GNU extension:
12799 declaration:
12800 __extension__ declaration */
12802 static void
12803 cp_parser_declaration (cp_parser* parser)
12805 cp_token token1;
12806 cp_token token2;
12807 int saved_pedantic;
12808 void *p;
12809 tree attributes = NULL_TREE;
12811 /* Check for the `__extension__' keyword. */
12812 if (cp_parser_extension_opt (parser, &saved_pedantic))
12814 /* Parse the qualified declaration. */
12815 cp_parser_declaration (parser);
12816 /* Restore the PEDANTIC flag. */
12817 pedantic = saved_pedantic;
12819 return;
12822 /* Try to figure out what kind of declaration is present. */
12823 token1 = *cp_lexer_peek_token (parser->lexer);
12825 if (token1.type != CPP_EOF)
12826 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12827 else
12829 token2.type = CPP_EOF;
12830 token2.keyword = RID_MAX;
12833 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12834 p = obstack_alloc (&declarator_obstack, 0);
12836 /* If the next token is `extern' and the following token is a string
12837 literal, then we have a linkage specification. */
12838 if (token1.keyword == RID_EXTERN
12839 && cp_parser_is_pure_string_literal (&token2))
12840 cp_parser_linkage_specification (parser);
12841 /* If the next token is `template', then we have either a template
12842 declaration, an explicit instantiation, or an explicit
12843 specialization. */
12844 else if (token1.keyword == RID_TEMPLATE)
12846 /* `template <>' indicates a template specialization. */
12847 if (token2.type == CPP_LESS
12848 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12849 cp_parser_explicit_specialization (parser);
12850 /* `template <' indicates a template declaration. */
12851 else if (token2.type == CPP_LESS)
12852 cp_parser_template_declaration (parser, /*member_p=*/false);
12853 /* Anything else must be an explicit instantiation. */
12854 else
12855 cp_parser_explicit_instantiation (parser);
12857 /* If the next token is `export', then we have a template
12858 declaration. */
12859 else if (token1.keyword == RID_EXPORT)
12860 cp_parser_template_declaration (parser, /*member_p=*/false);
12861 /* If the next token is `extern', 'static' or 'inline' and the one
12862 after that is `template', we have a GNU extended explicit
12863 instantiation directive. */
12864 else if (cp_parser_allow_gnu_extensions_p (parser)
12865 && (token1.keyword == RID_EXTERN
12866 || token1.keyword == RID_STATIC
12867 || token1.keyword == RID_INLINE)
12868 && token2.keyword == RID_TEMPLATE)
12869 cp_parser_explicit_instantiation (parser);
12870 /* If the next token is `namespace', check for a named or unnamed
12871 namespace definition. */
12872 else if (token1.keyword == RID_NAMESPACE
12873 && (/* A named namespace definition. */
12874 (token2.type == CPP_NAME
12875 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12876 != CPP_EQ))
12877 || (token2.type == CPP_OPEN_SQUARE
12878 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12879 == CPP_OPEN_SQUARE)
12880 /* An unnamed namespace definition. */
12881 || token2.type == CPP_OPEN_BRACE
12882 || token2.keyword == RID_ATTRIBUTE))
12883 cp_parser_namespace_definition (parser);
12884 /* An inline (associated) namespace definition. */
12885 else if (token1.keyword == RID_INLINE
12886 && token2.keyword == RID_NAMESPACE)
12887 cp_parser_namespace_definition (parser);
12888 /* Objective-C++ declaration/definition. */
12889 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12890 cp_parser_objc_declaration (parser, NULL_TREE);
12891 else if (c_dialect_objc ()
12892 && token1.keyword == RID_ATTRIBUTE
12893 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12894 cp_parser_objc_declaration (parser, attributes);
12895 /* At this point we may have a template declared by a concept
12896 introduction. */
12897 else if (flag_concepts
12898 && cp_parser_template_declaration_after_export (parser,
12899 /*member_p=*/false))
12900 /* We did. */;
12901 else
12902 /* Try to parse a block-declaration, or a function-definition. */
12903 cp_parser_block_declaration (parser, /*statement_p=*/false);
12905 /* Free any declarators allocated. */
12906 obstack_free (&declarator_obstack, p);
12909 /* Parse a block-declaration.
12911 block-declaration:
12912 simple-declaration
12913 asm-definition
12914 namespace-alias-definition
12915 using-declaration
12916 using-directive
12918 GNU Extension:
12920 block-declaration:
12921 __extension__ block-declaration
12923 C++0x Extension:
12925 block-declaration:
12926 static_assert-declaration
12928 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12929 part of a declaration-statement. */
12931 static void
12932 cp_parser_block_declaration (cp_parser *parser,
12933 bool statement_p)
12935 cp_token *token1;
12936 int saved_pedantic;
12938 /* Check for the `__extension__' keyword. */
12939 if (cp_parser_extension_opt (parser, &saved_pedantic))
12941 /* Parse the qualified declaration. */
12942 cp_parser_block_declaration (parser, statement_p);
12943 /* Restore the PEDANTIC flag. */
12944 pedantic = saved_pedantic;
12946 return;
12949 /* Peek at the next token to figure out which kind of declaration is
12950 present. */
12951 token1 = cp_lexer_peek_token (parser->lexer);
12953 /* If the next keyword is `asm', we have an asm-definition. */
12954 if (token1->keyword == RID_ASM)
12956 if (statement_p)
12957 cp_parser_commit_to_tentative_parse (parser);
12958 cp_parser_asm_definition (parser);
12960 /* If the next keyword is `namespace', we have a
12961 namespace-alias-definition. */
12962 else if (token1->keyword == RID_NAMESPACE)
12963 cp_parser_namespace_alias_definition (parser);
12964 /* If the next keyword is `using', we have a
12965 using-declaration, a using-directive, or an alias-declaration. */
12966 else if (token1->keyword == RID_USING)
12968 cp_token *token2;
12970 if (statement_p)
12971 cp_parser_commit_to_tentative_parse (parser);
12972 /* If the token after `using' is `namespace', then we have a
12973 using-directive. */
12974 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12975 if (token2->keyword == RID_NAMESPACE)
12976 cp_parser_using_directive (parser);
12977 /* If the second token after 'using' is '=', then we have an
12978 alias-declaration. */
12979 else if (cxx_dialect >= cxx11
12980 && token2->type == CPP_NAME
12981 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12982 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12983 cp_parser_alias_declaration (parser);
12984 /* Otherwise, it's a using-declaration. */
12985 else
12986 cp_parser_using_declaration (parser,
12987 /*access_declaration_p=*/false);
12989 /* If the next keyword is `__label__' we have a misplaced label
12990 declaration. */
12991 else if (token1->keyword == RID_LABEL)
12993 cp_lexer_consume_token (parser->lexer);
12994 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12995 cp_parser_skip_to_end_of_statement (parser);
12996 /* If the next token is now a `;', consume it. */
12997 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12998 cp_lexer_consume_token (parser->lexer);
13000 /* If the next token is `static_assert' we have a static assertion. */
13001 else if (token1->keyword == RID_STATIC_ASSERT)
13002 cp_parser_static_assert (parser, /*member_p=*/false);
13003 /* Anything else must be a simple-declaration. */
13004 else
13005 cp_parser_simple_declaration (parser, !statement_p,
13006 /*maybe_range_for_decl*/NULL);
13009 /* Parse a simple-declaration.
13011 simple-declaration:
13012 decl-specifier-seq [opt] init-declarator-list [opt] ;
13013 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13014 brace-or-equal-initializer ;
13016 init-declarator-list:
13017 init-declarator
13018 init-declarator-list , init-declarator
13020 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13021 function-definition as a simple-declaration.
13023 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13024 parsed declaration if it is an uninitialized single declarator not followed
13025 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13026 if present, will not be consumed. */
13028 static void
13029 cp_parser_simple_declaration (cp_parser* parser,
13030 bool function_definition_allowed_p,
13031 tree *maybe_range_for_decl)
13033 cp_decl_specifier_seq decl_specifiers;
13034 int declares_class_or_enum;
13035 bool saw_declarator;
13036 location_t comma_loc = UNKNOWN_LOCATION;
13037 location_t init_loc = UNKNOWN_LOCATION;
13039 if (maybe_range_for_decl)
13040 *maybe_range_for_decl = NULL_TREE;
13042 /* Defer access checks until we know what is being declared; the
13043 checks for names appearing in the decl-specifier-seq should be
13044 done as if we were in the scope of the thing being declared. */
13045 push_deferring_access_checks (dk_deferred);
13047 /* Parse the decl-specifier-seq. We have to keep track of whether
13048 or not the decl-specifier-seq declares a named class or
13049 enumeration type, since that is the only case in which the
13050 init-declarator-list is allowed to be empty.
13052 [dcl.dcl]
13054 In a simple-declaration, the optional init-declarator-list can be
13055 omitted only when declaring a class or enumeration, that is when
13056 the decl-specifier-seq contains either a class-specifier, an
13057 elaborated-type-specifier, or an enum-specifier. */
13058 cp_parser_decl_specifier_seq (parser,
13059 CP_PARSER_FLAGS_OPTIONAL,
13060 &decl_specifiers,
13061 &declares_class_or_enum);
13062 /* We no longer need to defer access checks. */
13063 stop_deferring_access_checks ();
13065 /* In a block scope, a valid declaration must always have a
13066 decl-specifier-seq. By not trying to parse declarators, we can
13067 resolve the declaration/expression ambiguity more quickly. */
13068 if (!function_definition_allowed_p
13069 && !decl_specifiers.any_specifiers_p)
13071 cp_parser_error (parser, "expected declaration");
13072 goto done;
13075 /* If the next two tokens are both identifiers, the code is
13076 erroneous. The usual cause of this situation is code like:
13078 T t;
13080 where "T" should name a type -- but does not. */
13081 if (!decl_specifiers.any_type_specifiers_p
13082 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13084 /* If parsing tentatively, we should commit; we really are
13085 looking at a declaration. */
13086 cp_parser_commit_to_tentative_parse (parser);
13087 /* Give up. */
13088 goto done;
13091 cp_parser_maybe_commit_to_declaration (parser,
13092 decl_specifiers.any_specifiers_p);
13094 /* Look for C++17 decomposition declaration. */
13095 for (size_t n = 1; ; n++)
13096 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13097 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13098 continue;
13099 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13100 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13101 && decl_specifiers.any_specifiers_p)
13103 tree decl
13104 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13105 maybe_range_for_decl,
13106 &init_loc);
13108 /* The next token should be either a `,' or a `;'. */
13109 cp_token *token = cp_lexer_peek_token (parser->lexer);
13110 /* If it's a `;', we are done. */
13111 if (token->type == CPP_SEMICOLON)
13112 goto finish;
13113 else if (maybe_range_for_decl)
13115 if (*maybe_range_for_decl == NULL_TREE)
13116 *maybe_range_for_decl = error_mark_node;
13117 goto finish;
13119 /* Anything else is an error. */
13120 else
13122 /* If we have already issued an error message we don't need
13123 to issue another one. */
13124 if ((decl != error_mark_node
13125 && DECL_INITIAL (decl) != error_mark_node)
13126 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13127 cp_parser_error (parser, "expected %<,%> or %<;%>");
13128 /* Skip tokens until we reach the end of the statement. */
13129 cp_parser_skip_to_end_of_statement (parser);
13130 /* If the next token is now a `;', consume it. */
13131 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13132 cp_lexer_consume_token (parser->lexer);
13133 goto done;
13136 else
13137 break;
13139 tree last_type;
13140 bool auto_specifier_p;
13141 /* NULL_TREE if both variable and function declaration are allowed,
13142 error_mark_node if function declaration are not allowed and
13143 a FUNCTION_DECL that should be diagnosed if it is followed by
13144 variable declarations. */
13145 tree auto_function_declaration;
13147 last_type = NULL_TREE;
13148 auto_specifier_p
13149 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13150 auto_function_declaration = NULL_TREE;
13152 /* Keep going until we hit the `;' at the end of the simple
13153 declaration. */
13154 saw_declarator = false;
13155 while (cp_lexer_next_token_is_not (parser->lexer,
13156 CPP_SEMICOLON))
13158 cp_token *token;
13159 bool function_definition_p;
13160 tree decl;
13161 tree auto_result = NULL_TREE;
13163 if (saw_declarator)
13165 /* If we are processing next declarator, comma is expected */
13166 token = cp_lexer_peek_token (parser->lexer);
13167 gcc_assert (token->type == CPP_COMMA);
13168 cp_lexer_consume_token (parser->lexer);
13169 if (maybe_range_for_decl)
13171 *maybe_range_for_decl = error_mark_node;
13172 if (comma_loc == UNKNOWN_LOCATION)
13173 comma_loc = token->location;
13176 else
13177 saw_declarator = true;
13179 /* Parse the init-declarator. */
13180 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13181 /*checks=*/NULL,
13182 function_definition_allowed_p,
13183 /*member_p=*/false,
13184 declares_class_or_enum,
13185 &function_definition_p,
13186 maybe_range_for_decl,
13187 &init_loc,
13188 &auto_result);
13189 /* If an error occurred while parsing tentatively, exit quickly.
13190 (That usually happens when in the body of a function; each
13191 statement is treated as a declaration-statement until proven
13192 otherwise.) */
13193 if (cp_parser_error_occurred (parser))
13194 goto done;
13196 if (auto_specifier_p && cxx_dialect >= cxx14)
13198 /* If the init-declarator-list contains more than one
13199 init-declarator, they shall all form declarations of
13200 variables. */
13201 if (auto_function_declaration == NULL_TREE)
13202 auto_function_declaration
13203 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13204 else if (TREE_CODE (decl) == FUNCTION_DECL
13205 || auto_function_declaration != error_mark_node)
13207 error_at (decl_specifiers.locations[ds_type_spec],
13208 "non-variable %qD in declaration with more than one "
13209 "declarator with placeholder type",
13210 TREE_CODE (decl) == FUNCTION_DECL
13211 ? decl : auto_function_declaration);
13212 auto_function_declaration = error_mark_node;
13216 if (auto_result
13217 && (!processing_template_decl || !type_uses_auto (auto_result)))
13219 if (last_type
13220 && last_type != error_mark_node
13221 && !same_type_p (auto_result, last_type))
13223 /* If the list of declarators contains more than one declarator,
13224 the type of each declared variable is determined as described
13225 above. If the type deduced for the template parameter U is not
13226 the same in each deduction, the program is ill-formed. */
13227 error_at (decl_specifiers.locations[ds_type_spec],
13228 "inconsistent deduction for %qT: %qT and then %qT",
13229 decl_specifiers.type, last_type, auto_result);
13230 last_type = error_mark_node;
13232 else
13233 last_type = auto_result;
13236 /* Handle function definitions specially. */
13237 if (function_definition_p)
13239 /* If the next token is a `,', then we are probably
13240 processing something like:
13242 void f() {}, *p;
13244 which is erroneous. */
13245 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13247 cp_token *token = cp_lexer_peek_token (parser->lexer);
13248 error_at (token->location,
13249 "mixing"
13250 " declarations and function-definitions is forbidden");
13252 /* Otherwise, we're done with the list of declarators. */
13253 else
13255 pop_deferring_access_checks ();
13256 return;
13259 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13260 *maybe_range_for_decl = decl;
13261 /* The next token should be either a `,' or a `;'. */
13262 token = cp_lexer_peek_token (parser->lexer);
13263 /* If it's a `,', there are more declarators to come. */
13264 if (token->type == CPP_COMMA)
13265 /* will be consumed next time around */;
13266 /* If it's a `;', we are done. */
13267 else if (token->type == CPP_SEMICOLON)
13268 break;
13269 else if (maybe_range_for_decl)
13271 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13272 permerror (decl_specifiers.locations[ds_type_spec],
13273 "types may not be defined in a for-range-declaration");
13274 break;
13276 /* Anything else is an error. */
13277 else
13279 /* If we have already issued an error message we don't need
13280 to issue another one. */
13281 if ((decl != error_mark_node
13282 && DECL_INITIAL (decl) != error_mark_node)
13283 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13284 cp_parser_error (parser, "expected %<,%> or %<;%>");
13285 /* Skip tokens until we reach the end of the statement. */
13286 cp_parser_skip_to_end_of_statement (parser);
13287 /* If the next token is now a `;', consume it. */
13288 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13289 cp_lexer_consume_token (parser->lexer);
13290 goto done;
13292 /* After the first time around, a function-definition is not
13293 allowed -- even if it was OK at first. For example:
13295 int i, f() {}
13297 is not valid. */
13298 function_definition_allowed_p = false;
13301 /* Issue an error message if no declarators are present, and the
13302 decl-specifier-seq does not itself declare a class or
13303 enumeration: [dcl.dcl]/3. */
13304 if (!saw_declarator)
13306 if (cp_parser_declares_only_class_p (parser))
13308 if (!declares_class_or_enum
13309 && decl_specifiers.type
13310 && OVERLOAD_TYPE_P (decl_specifiers.type))
13311 /* Ensure an error is issued anyway when finish_decltype_type,
13312 called via cp_parser_decl_specifier_seq, returns a class or
13313 an enumeration (c++/51786). */
13314 decl_specifiers.type = NULL_TREE;
13315 shadow_tag (&decl_specifiers);
13317 /* Perform any deferred access checks. */
13318 perform_deferred_access_checks (tf_warning_or_error);
13321 /* Consume the `;'. */
13322 finish:
13323 if (!maybe_range_for_decl)
13324 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13325 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13327 if (init_loc != UNKNOWN_LOCATION)
13328 error_at (init_loc, "initializer in range-based %<for%> loop");
13329 if (comma_loc != UNKNOWN_LOCATION)
13330 error_at (comma_loc,
13331 "multiple declarations in range-based %<for%> loop");
13334 done:
13335 pop_deferring_access_checks ();
13338 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13339 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13340 initializer ; */
13342 static tree
13343 cp_parser_decomposition_declaration (cp_parser *parser,
13344 cp_decl_specifier_seq *decl_specifiers,
13345 tree *maybe_range_for_decl,
13346 location_t *init_loc)
13348 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13349 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13350 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13352 /* Parse the identifier-list. */
13353 auto_vec<cp_expr, 10> v;
13354 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13355 while (true)
13357 cp_expr e = cp_parser_identifier (parser);
13358 if (e.get_value () == error_mark_node)
13359 break;
13360 v.safe_push (e);
13361 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13362 break;
13363 cp_lexer_consume_token (parser->lexer);
13366 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13367 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13369 end_loc = UNKNOWN_LOCATION;
13370 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13371 false);
13372 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13373 cp_lexer_consume_token (parser->lexer);
13374 else
13376 cp_parser_skip_to_end_of_statement (parser);
13377 return error_mark_node;
13381 if (cxx_dialect < cxx17)
13382 pedwarn (loc, 0, "structured bindings only available with "
13383 "-std=c++17 or -std=gnu++17");
13385 tree pushed_scope;
13386 cp_declarator *declarator = make_declarator (cdk_decomp);
13387 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13388 declarator->id_loc = loc;
13389 if (ref_qual != REF_QUAL_NONE)
13390 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13391 ref_qual == REF_QUAL_RVALUE,
13392 NULL_TREE);
13393 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13394 NULL_TREE, decl_specifiers->attributes,
13395 &pushed_scope);
13396 tree orig_decl = decl;
13398 unsigned int i;
13399 cp_expr e;
13400 cp_decl_specifier_seq decl_specs;
13401 clear_decl_specs (&decl_specs);
13402 decl_specs.type = make_auto ();
13403 tree prev = decl;
13404 FOR_EACH_VEC_ELT (v, i, e)
13406 if (i == 0)
13407 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13408 else
13409 declarator->u.id.unqualified_name = e.get_value ();
13410 declarator->id_loc = e.get_location ();
13411 tree elt_pushed_scope;
13412 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13413 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13414 if (decl2 == error_mark_node)
13415 decl = error_mark_node;
13416 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13418 /* Ensure we've diagnosed redeclaration if we aren't creating
13419 a new VAR_DECL. */
13420 gcc_assert (errorcount);
13421 decl = error_mark_node;
13423 else
13424 prev = decl2;
13425 if (elt_pushed_scope)
13426 pop_scope (elt_pushed_scope);
13429 if (v.is_empty ())
13431 error_at (loc, "empty structured binding declaration");
13432 decl = error_mark_node;
13435 if (maybe_range_for_decl == NULL
13436 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13438 bool non_constant_p = false, is_direct_init = false;
13439 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13440 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13441 &non_constant_p);
13442 if (initializer == NULL_TREE
13443 || (TREE_CODE (initializer) == TREE_LIST
13444 && TREE_CHAIN (initializer))
13445 || (is_direct_init
13446 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13447 && CONSTRUCTOR_NELTS (initializer) != 1))
13449 error_at (loc, "invalid initializer for structured binding "
13450 "declaration");
13451 initializer = error_mark_node;
13454 if (decl != error_mark_node)
13456 cp_maybe_mangle_decomp (decl, prev, v.length ());
13457 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13458 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13459 cp_finish_decomp (decl, prev, v.length ());
13462 else if (decl != error_mark_node)
13464 *maybe_range_for_decl = prev;
13465 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13466 the underlying DECL. */
13467 cp_finish_decomp (decl, prev, v.length ());
13470 if (pushed_scope)
13471 pop_scope (pushed_scope);
13473 if (decl == error_mark_node && DECL_P (orig_decl))
13475 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13476 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13479 return decl;
13482 /* Parse a decl-specifier-seq.
13484 decl-specifier-seq:
13485 decl-specifier-seq [opt] decl-specifier
13486 decl-specifier attribute-specifier-seq [opt] (C++11)
13488 decl-specifier:
13489 storage-class-specifier
13490 type-specifier
13491 function-specifier
13492 friend
13493 typedef
13495 GNU Extension:
13497 decl-specifier:
13498 attributes
13500 Concepts Extension:
13502 decl-specifier:
13503 concept
13505 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13507 The parser flags FLAGS is used to control type-specifier parsing.
13509 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13510 flags:
13512 1: one of the decl-specifiers is an elaborated-type-specifier
13513 (i.e., a type declaration)
13514 2: one of the decl-specifiers is an enum-specifier or a
13515 class-specifier (i.e., a type definition)
13519 static void
13520 cp_parser_decl_specifier_seq (cp_parser* parser,
13521 cp_parser_flags flags,
13522 cp_decl_specifier_seq *decl_specs,
13523 int* declares_class_or_enum)
13525 bool constructor_possible_p = !parser->in_declarator_p;
13526 bool found_decl_spec = false;
13527 cp_token *start_token = NULL;
13528 cp_decl_spec ds;
13530 /* Clear DECL_SPECS. */
13531 clear_decl_specs (decl_specs);
13533 /* Assume no class or enumeration type is declared. */
13534 *declares_class_or_enum = 0;
13536 /* Keep reading specifiers until there are no more to read. */
13537 while (true)
13539 bool constructor_p;
13540 cp_token *token;
13541 ds = ds_last;
13543 /* Peek at the next token. */
13544 token = cp_lexer_peek_token (parser->lexer);
13546 /* Save the first token of the decl spec list for error
13547 reporting. */
13548 if (!start_token)
13549 start_token = token;
13550 /* Handle attributes. */
13551 if (cp_next_tokens_can_be_attribute_p (parser))
13553 /* Parse the attributes. */
13554 tree attrs = cp_parser_attributes_opt (parser);
13556 /* In a sequence of declaration specifiers, c++11 attributes
13557 appertain to the type that precede them. In that case
13558 [dcl.spec]/1 says:
13560 The attribute-specifier-seq affects the type only for
13561 the declaration it appears in, not other declarations
13562 involving the same type.
13564 But for now let's force the user to position the
13565 attribute either at the beginning of the declaration or
13566 after the declarator-id, which would clearly mean that it
13567 applies to the declarator. */
13568 if (cxx11_attribute_p (attrs))
13570 if (!found_decl_spec)
13571 /* The c++11 attribute is at the beginning of the
13572 declaration. It appertains to the entity being
13573 declared. */;
13574 else
13576 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13578 /* This is an attribute following a
13579 class-specifier. */
13580 if (decl_specs->type_definition_p)
13581 warn_misplaced_attr_for_class_type (token->location,
13582 decl_specs->type);
13583 attrs = NULL_TREE;
13585 else
13587 decl_specs->std_attributes
13588 = attr_chainon (decl_specs->std_attributes, attrs);
13589 if (decl_specs->locations[ds_std_attribute] == 0)
13590 decl_specs->locations[ds_std_attribute] = token->location;
13592 continue;
13596 decl_specs->attributes
13597 = attr_chainon (decl_specs->attributes, attrs);
13598 if (decl_specs->locations[ds_attribute] == 0)
13599 decl_specs->locations[ds_attribute] = token->location;
13600 continue;
13602 /* Assume we will find a decl-specifier keyword. */
13603 found_decl_spec = true;
13604 /* If the next token is an appropriate keyword, we can simply
13605 add it to the list. */
13606 switch (token->keyword)
13608 /* decl-specifier:
13609 friend
13610 constexpr */
13611 case RID_FRIEND:
13612 if (!at_class_scope_p ())
13614 gcc_rich_location richloc (token->location);
13615 richloc.add_fixit_remove ();
13616 error_at (&richloc, "%<friend%> used outside of class");
13617 cp_lexer_purge_token (parser->lexer);
13619 else
13621 ds = ds_friend;
13622 /* Consume the token. */
13623 cp_lexer_consume_token (parser->lexer);
13625 break;
13627 case RID_CONSTEXPR:
13628 ds = ds_constexpr;
13629 cp_lexer_consume_token (parser->lexer);
13630 break;
13632 case RID_CONCEPT:
13633 ds = ds_concept;
13634 cp_lexer_consume_token (parser->lexer);
13635 break;
13637 /* function-specifier:
13638 inline
13639 virtual
13640 explicit */
13641 case RID_INLINE:
13642 case RID_VIRTUAL:
13643 case RID_EXPLICIT:
13644 cp_parser_function_specifier_opt (parser, decl_specs);
13645 break;
13647 /* decl-specifier:
13648 typedef */
13649 case RID_TYPEDEF:
13650 ds = ds_typedef;
13651 /* Consume the token. */
13652 cp_lexer_consume_token (parser->lexer);
13653 /* A constructor declarator cannot appear in a typedef. */
13654 constructor_possible_p = false;
13655 /* The "typedef" keyword can only occur in a declaration; we
13656 may as well commit at this point. */
13657 cp_parser_commit_to_tentative_parse (parser);
13659 if (decl_specs->storage_class != sc_none)
13660 decl_specs->conflicting_specifiers_p = true;
13661 break;
13663 /* storage-class-specifier:
13664 auto
13665 register
13666 static
13667 extern
13668 mutable
13670 GNU Extension:
13671 thread */
13672 case RID_AUTO:
13673 if (cxx_dialect == cxx98)
13675 /* Consume the token. */
13676 cp_lexer_consume_token (parser->lexer);
13678 /* Complain about `auto' as a storage specifier, if
13679 we're complaining about C++0x compatibility. */
13680 gcc_rich_location richloc (token->location);
13681 richloc.add_fixit_remove ();
13682 warning_at (&richloc, OPT_Wc__11_compat,
13683 "%<auto%> changes meaning in C++11; "
13684 "please remove it");
13686 /* Set the storage class anyway. */
13687 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13688 token);
13690 else
13691 /* C++0x auto type-specifier. */
13692 found_decl_spec = false;
13693 break;
13695 case RID_REGISTER:
13696 case RID_STATIC:
13697 case RID_EXTERN:
13698 case RID_MUTABLE:
13699 /* Consume the token. */
13700 cp_lexer_consume_token (parser->lexer);
13701 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13702 token);
13703 break;
13704 case RID_THREAD:
13705 /* Consume the token. */
13706 ds = ds_thread;
13707 cp_lexer_consume_token (parser->lexer);
13708 break;
13710 default:
13711 /* We did not yet find a decl-specifier yet. */
13712 found_decl_spec = false;
13713 break;
13716 if (found_decl_spec
13717 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13718 && token->keyword != RID_CONSTEXPR)
13719 error ("decl-specifier invalid in condition");
13721 if (found_decl_spec
13722 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13723 && token->keyword != RID_MUTABLE
13724 && token->keyword != RID_CONSTEXPR)
13725 error_at (token->location, "%qD invalid in lambda",
13726 ridpointers[token->keyword]);
13728 if (ds != ds_last)
13729 set_and_check_decl_spec_loc (decl_specs, ds, token);
13731 /* Constructors are a special case. The `S' in `S()' is not a
13732 decl-specifier; it is the beginning of the declarator. */
13733 constructor_p
13734 = (!found_decl_spec
13735 && constructor_possible_p
13736 && (cp_parser_constructor_declarator_p
13737 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13739 /* If we don't have a DECL_SPEC yet, then we must be looking at
13740 a type-specifier. */
13741 if (!found_decl_spec && !constructor_p)
13743 int decl_spec_declares_class_or_enum;
13744 bool is_cv_qualifier;
13745 tree type_spec;
13747 type_spec
13748 = cp_parser_type_specifier (parser, flags,
13749 decl_specs,
13750 /*is_declaration=*/true,
13751 &decl_spec_declares_class_or_enum,
13752 &is_cv_qualifier);
13753 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13755 /* If this type-specifier referenced a user-defined type
13756 (a typedef, class-name, etc.), then we can't allow any
13757 more such type-specifiers henceforth.
13759 [dcl.spec]
13761 The longest sequence of decl-specifiers that could
13762 possibly be a type name is taken as the
13763 decl-specifier-seq of a declaration. The sequence shall
13764 be self-consistent as described below.
13766 [dcl.type]
13768 As a general rule, at most one type-specifier is allowed
13769 in the complete decl-specifier-seq of a declaration. The
13770 only exceptions are the following:
13772 -- const or volatile can be combined with any other
13773 type-specifier.
13775 -- signed or unsigned can be combined with char, long,
13776 short, or int.
13778 -- ..
13780 Example:
13782 typedef char* Pc;
13783 void g (const int Pc);
13785 Here, Pc is *not* part of the decl-specifier seq; it's
13786 the declarator. Therefore, once we see a type-specifier
13787 (other than a cv-qualifier), we forbid any additional
13788 user-defined types. We *do* still allow things like `int
13789 int' to be considered a decl-specifier-seq, and issue the
13790 error message later. */
13791 if (type_spec && !is_cv_qualifier)
13792 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13793 /* A constructor declarator cannot follow a type-specifier. */
13794 if (type_spec)
13796 constructor_possible_p = false;
13797 found_decl_spec = true;
13798 if (!is_cv_qualifier)
13799 decl_specs->any_type_specifiers_p = true;
13803 /* If we still do not have a DECL_SPEC, then there are no more
13804 decl-specifiers. */
13805 if (!found_decl_spec)
13806 break;
13808 decl_specs->any_specifiers_p = true;
13809 /* After we see one decl-specifier, further decl-specifiers are
13810 always optional. */
13811 flags |= CP_PARSER_FLAGS_OPTIONAL;
13814 /* Don't allow a friend specifier with a class definition. */
13815 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13816 && (*declares_class_or_enum & 2))
13817 error_at (decl_specs->locations[ds_friend],
13818 "class definition may not be declared a friend");
13821 /* Parse an (optional) storage-class-specifier.
13823 storage-class-specifier:
13824 auto
13825 register
13826 static
13827 extern
13828 mutable
13830 GNU Extension:
13832 storage-class-specifier:
13833 thread
13835 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13837 static tree
13838 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13840 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13842 case RID_AUTO:
13843 if (cxx_dialect != cxx98)
13844 return NULL_TREE;
13845 /* Fall through for C++98. */
13846 gcc_fallthrough ();
13848 case RID_REGISTER:
13849 case RID_STATIC:
13850 case RID_EXTERN:
13851 case RID_MUTABLE:
13852 case RID_THREAD:
13853 /* Consume the token. */
13854 return cp_lexer_consume_token (parser->lexer)->u.value;
13856 default:
13857 return NULL_TREE;
13861 /* Parse an (optional) function-specifier.
13863 function-specifier:
13864 inline
13865 virtual
13866 explicit
13868 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13869 Updates DECL_SPECS, if it is non-NULL. */
13871 static tree
13872 cp_parser_function_specifier_opt (cp_parser* parser,
13873 cp_decl_specifier_seq *decl_specs)
13875 cp_token *token = cp_lexer_peek_token (parser->lexer);
13876 switch (token->keyword)
13878 case RID_INLINE:
13879 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13880 break;
13882 case RID_VIRTUAL:
13883 /* 14.5.2.3 [temp.mem]
13885 A member function template shall not be virtual. */
13886 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13887 && current_class_type)
13888 error_at (token->location, "templates may not be %<virtual%>");
13889 else
13890 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13891 break;
13893 case RID_EXPLICIT:
13894 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13895 break;
13897 default:
13898 return NULL_TREE;
13901 /* Consume the token. */
13902 return cp_lexer_consume_token (parser->lexer)->u.value;
13905 /* Parse a linkage-specification.
13907 linkage-specification:
13908 extern string-literal { declaration-seq [opt] }
13909 extern string-literal declaration */
13911 static void
13912 cp_parser_linkage_specification (cp_parser* parser)
13914 tree linkage;
13916 /* Look for the `extern' keyword. */
13917 cp_token *extern_token
13918 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13920 /* Look for the string-literal. */
13921 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13922 linkage = cp_parser_string_literal (parser, false, false);
13924 /* Transform the literal into an identifier. If the literal is a
13925 wide-character string, or contains embedded NULs, then we can't
13926 handle it as the user wants. */
13927 if (strlen (TREE_STRING_POINTER (linkage))
13928 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13930 cp_parser_error (parser, "invalid linkage-specification");
13931 /* Assume C++ linkage. */
13932 linkage = lang_name_cplusplus;
13934 else
13935 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13937 /* We're now using the new linkage. */
13938 push_lang_context (linkage);
13940 /* Preserve the location of the the innermost linkage specification,
13941 tracking the locations of nested specifications via a local. */
13942 location_t saved_location
13943 = parser->innermost_linkage_specification_location;
13944 /* Construct a location ranging from the start of the "extern" to
13945 the end of the string-literal, with the caret at the start, e.g.:
13946 extern "C" {
13947 ^~~~~~~~~~
13949 parser->innermost_linkage_specification_location
13950 = make_location (extern_token->location,
13951 extern_token->location,
13952 get_finish (string_token->location));
13954 /* If the next token is a `{', then we're using the first
13955 production. */
13956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13958 cp_ensure_no_omp_declare_simd (parser);
13959 cp_ensure_no_oacc_routine (parser);
13961 /* Consume the `{' token. */
13962 matching_braces braces;
13963 braces.consume_open (parser)->location;
13964 /* Parse the declarations. */
13965 cp_parser_declaration_seq_opt (parser);
13966 /* Look for the closing `}'. */
13967 braces.require_close (parser);
13969 /* Otherwise, there's just one declaration. */
13970 else
13972 bool saved_in_unbraced_linkage_specification_p;
13974 saved_in_unbraced_linkage_specification_p
13975 = parser->in_unbraced_linkage_specification_p;
13976 parser->in_unbraced_linkage_specification_p = true;
13977 cp_parser_declaration (parser);
13978 parser->in_unbraced_linkage_specification_p
13979 = saved_in_unbraced_linkage_specification_p;
13982 /* We're done with the linkage-specification. */
13983 pop_lang_context ();
13985 /* Restore location of parent linkage specification, if any. */
13986 parser->innermost_linkage_specification_location = saved_location;
13989 /* Parse a static_assert-declaration.
13991 static_assert-declaration:
13992 static_assert ( constant-expression , string-literal ) ;
13993 static_assert ( constant-expression ) ; (C++17)
13995 If MEMBER_P, this static_assert is a class member. */
13997 static void
13998 cp_parser_static_assert(cp_parser *parser, bool member_p)
14000 cp_expr condition;
14001 location_t token_loc;
14002 tree message;
14003 bool dummy;
14005 /* Peek at the `static_assert' token so we can keep track of exactly
14006 where the static assertion started. */
14007 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14009 /* Look for the `static_assert' keyword. */
14010 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14011 RT_STATIC_ASSERT))
14012 return;
14014 /* We know we are in a static assertion; commit to any tentative
14015 parse. */
14016 if (cp_parser_parsing_tentatively (parser))
14017 cp_parser_commit_to_tentative_parse (parser);
14019 /* Parse the `(' starting the static assertion condition. */
14020 matching_parens parens;
14021 parens.require_open (parser);
14023 /* Parse the constant-expression. Allow a non-constant expression
14024 here in order to give better diagnostics in finish_static_assert. */
14025 condition =
14026 cp_parser_constant_expression (parser,
14027 /*allow_non_constant_p=*/true,
14028 /*non_constant_p=*/&dummy);
14030 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14032 if (cxx_dialect < cxx17)
14033 pedwarn (input_location, OPT_Wpedantic,
14034 "static_assert without a message "
14035 "only available with -std=c++17 or -std=gnu++17");
14036 /* Eat the ')' */
14037 cp_lexer_consume_token (parser->lexer);
14038 message = build_string (1, "");
14039 TREE_TYPE (message) = char_array_type_node;
14040 fix_string_type (message);
14042 else
14044 /* Parse the separating `,'. */
14045 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14047 /* Parse the string-literal message. */
14048 message = cp_parser_string_literal (parser,
14049 /*translate=*/false,
14050 /*wide_ok=*/true);
14052 /* A `)' completes the static assertion. */
14053 if (!parens.require_close (parser))
14054 cp_parser_skip_to_closing_parenthesis (parser,
14055 /*recovering=*/true,
14056 /*or_comma=*/false,
14057 /*consume_paren=*/true);
14060 /* A semicolon terminates the declaration. */
14061 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14063 /* Get the location for the static assertion. Use that of the
14064 condition if available, otherwise, use that of the "static_assert"
14065 token. */
14066 location_t assert_loc = condition.get_location ();
14067 if (assert_loc == UNKNOWN_LOCATION)
14068 assert_loc = token_loc;
14070 /* Complete the static assertion, which may mean either processing
14071 the static assert now or saving it for template instantiation. */
14072 finish_static_assert (condition, message, assert_loc, member_p);
14075 /* Parse the expression in decltype ( expression ). */
14077 static tree
14078 cp_parser_decltype_expr (cp_parser *parser,
14079 bool &id_expression_or_member_access_p)
14081 cp_token *id_expr_start_token;
14082 tree expr;
14084 /* Since we're going to preserve any side-effects from this parse, set up a
14085 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14086 in the expression. */
14087 tentative_firewall firewall (parser);
14089 /* First, try parsing an id-expression. */
14090 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14091 cp_parser_parse_tentatively (parser);
14092 expr = cp_parser_id_expression (parser,
14093 /*template_keyword_p=*/false,
14094 /*check_dependency_p=*/true,
14095 /*template_p=*/NULL,
14096 /*declarator_p=*/false,
14097 /*optional_p=*/false);
14099 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14101 bool non_integral_constant_expression_p = false;
14102 tree id_expression = expr;
14103 cp_id_kind idk;
14104 const char *error_msg;
14106 if (identifier_p (expr))
14107 /* Lookup the name we got back from the id-expression. */
14108 expr = cp_parser_lookup_name_simple (parser, expr,
14109 id_expr_start_token->location);
14111 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14112 /* A template without args is not a complete id-expression. */
14113 expr = error_mark_node;
14115 if (expr
14116 && expr != error_mark_node
14117 && TREE_CODE (expr) != TYPE_DECL
14118 && (TREE_CODE (expr) != BIT_NOT_EXPR
14119 || !TYPE_P (TREE_OPERAND (expr, 0)))
14120 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14122 /* Complete lookup of the id-expression. */
14123 expr = (finish_id_expression
14124 (id_expression, expr, parser->scope, &idk,
14125 /*integral_constant_expression_p=*/false,
14126 /*allow_non_integral_constant_expression_p=*/true,
14127 &non_integral_constant_expression_p,
14128 /*template_p=*/false,
14129 /*done=*/true,
14130 /*address_p=*/false,
14131 /*template_arg_p=*/false,
14132 &error_msg,
14133 id_expr_start_token->location));
14135 if (expr == error_mark_node)
14136 /* We found an id-expression, but it was something that we
14137 should not have found. This is an error, not something
14138 we can recover from, so note that we found an
14139 id-expression and we'll recover as gracefully as
14140 possible. */
14141 id_expression_or_member_access_p = true;
14144 if (expr
14145 && expr != error_mark_node
14146 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14147 /* We have an id-expression. */
14148 id_expression_or_member_access_p = true;
14151 if (!id_expression_or_member_access_p)
14153 /* Abort the id-expression parse. */
14154 cp_parser_abort_tentative_parse (parser);
14156 /* Parsing tentatively, again. */
14157 cp_parser_parse_tentatively (parser);
14159 /* Parse a class member access. */
14160 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14161 /*cast_p=*/false, /*decltype*/true,
14162 /*member_access_only_p=*/true, NULL);
14164 if (expr
14165 && expr != error_mark_node
14166 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14167 /* We have an id-expression. */
14168 id_expression_or_member_access_p = true;
14171 if (id_expression_or_member_access_p)
14172 /* We have parsed the complete id-expression or member access. */
14173 cp_parser_parse_definitely (parser);
14174 else
14176 /* Abort our attempt to parse an id-expression or member access
14177 expression. */
14178 cp_parser_abort_tentative_parse (parser);
14180 /* Commit to the tentative_firewall so we get syntax errors. */
14181 cp_parser_commit_to_tentative_parse (parser);
14183 /* Parse a full expression. */
14184 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14185 /*decltype_p=*/true);
14188 return expr;
14191 /* Parse a `decltype' type. Returns the type.
14193 simple-type-specifier:
14194 decltype ( expression )
14195 C++14 proposal:
14196 decltype ( auto ) */
14198 static tree
14199 cp_parser_decltype (cp_parser *parser)
14201 bool id_expression_or_member_access_p = false;
14202 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14204 if (start_token->type == CPP_DECLTYPE)
14206 /* Already parsed. */
14207 cp_lexer_consume_token (parser->lexer);
14208 return saved_checks_value (start_token->u.tree_check_value);
14211 /* Look for the `decltype' token. */
14212 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14213 return error_mark_node;
14215 /* Parse the opening `('. */
14216 matching_parens parens;
14217 if (!parens.require_open (parser))
14218 return error_mark_node;
14220 push_deferring_access_checks (dk_deferred);
14222 tree expr = NULL_TREE;
14224 if (cxx_dialect >= cxx14
14225 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14226 /* decltype (auto) */
14227 cp_lexer_consume_token (parser->lexer);
14228 else
14230 /* decltype (expression) */
14232 /* Types cannot be defined in a `decltype' expression. Save away the
14233 old message and set the new one. */
14234 const char *saved_message = parser->type_definition_forbidden_message;
14235 parser->type_definition_forbidden_message
14236 = G_("types may not be defined in %<decltype%> expressions");
14238 /* The restrictions on constant-expressions do not apply inside
14239 decltype expressions. */
14240 bool saved_integral_constant_expression_p
14241 = parser->integral_constant_expression_p;
14242 bool saved_non_integral_constant_expression_p
14243 = parser->non_integral_constant_expression_p;
14244 parser->integral_constant_expression_p = false;
14246 /* Within a parenthesized expression, a `>' token is always
14247 the greater-than operator. */
14248 bool saved_greater_than_is_operator_p
14249 = parser->greater_than_is_operator_p;
14250 parser->greater_than_is_operator_p = true;
14252 /* Do not actually evaluate the expression. */
14253 ++cp_unevaluated_operand;
14255 /* Do not warn about problems with the expression. */
14256 ++c_inhibit_evaluation_warnings;
14258 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14260 /* Go back to evaluating expressions. */
14261 --cp_unevaluated_operand;
14262 --c_inhibit_evaluation_warnings;
14264 /* The `>' token might be the end of a template-id or
14265 template-parameter-list now. */
14266 parser->greater_than_is_operator_p
14267 = saved_greater_than_is_operator_p;
14269 /* Restore the old message and the integral constant expression
14270 flags. */
14271 parser->type_definition_forbidden_message = saved_message;
14272 parser->integral_constant_expression_p
14273 = saved_integral_constant_expression_p;
14274 parser->non_integral_constant_expression_p
14275 = saved_non_integral_constant_expression_p;
14278 /* Parse to the closing `)'. */
14279 if (!parens.require_close (parser))
14281 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14282 /*consume_paren=*/true);
14283 pop_deferring_access_checks ();
14284 return error_mark_node;
14287 if (!expr)
14289 /* Build auto. */
14290 expr = make_decltype_auto ();
14291 AUTO_IS_DECLTYPE (expr) = true;
14293 else
14294 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14295 tf_warning_or_error);
14297 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14298 it again. */
14299 start_token->type = CPP_DECLTYPE;
14300 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14301 start_token->u.tree_check_value->value = expr;
14302 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14303 start_token->keyword = RID_MAX;
14304 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14306 pop_to_parent_deferring_access_checks ();
14308 return expr;
14311 /* Special member functions [gram.special] */
14313 /* Parse a conversion-function-id.
14315 conversion-function-id:
14316 operator conversion-type-id
14318 Returns an IDENTIFIER_NODE representing the operator. */
14320 static tree
14321 cp_parser_conversion_function_id (cp_parser* parser)
14323 tree type;
14324 tree saved_scope;
14325 tree saved_qualifying_scope;
14326 tree saved_object_scope;
14327 tree pushed_scope = NULL_TREE;
14329 /* Look for the `operator' token. */
14330 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14331 return error_mark_node;
14332 /* When we parse the conversion-type-id, the current scope will be
14333 reset. However, we need that information in able to look up the
14334 conversion function later, so we save it here. */
14335 saved_scope = parser->scope;
14336 saved_qualifying_scope = parser->qualifying_scope;
14337 saved_object_scope = parser->object_scope;
14338 /* We must enter the scope of the class so that the names of
14339 entities declared within the class are available in the
14340 conversion-type-id. For example, consider:
14342 struct S {
14343 typedef int I;
14344 operator I();
14347 S::operator I() { ... }
14349 In order to see that `I' is a type-name in the definition, we
14350 must be in the scope of `S'. */
14351 if (saved_scope)
14352 pushed_scope = push_scope (saved_scope);
14353 /* Parse the conversion-type-id. */
14354 type = cp_parser_conversion_type_id (parser);
14355 /* Leave the scope of the class, if any. */
14356 if (pushed_scope)
14357 pop_scope (pushed_scope);
14358 /* Restore the saved scope. */
14359 parser->scope = saved_scope;
14360 parser->qualifying_scope = saved_qualifying_scope;
14361 parser->object_scope = saved_object_scope;
14362 /* If the TYPE is invalid, indicate failure. */
14363 if (type == error_mark_node)
14364 return error_mark_node;
14365 return make_conv_op_name (type);
14368 /* Parse a conversion-type-id:
14370 conversion-type-id:
14371 type-specifier-seq conversion-declarator [opt]
14373 Returns the TYPE specified. */
14375 static tree
14376 cp_parser_conversion_type_id (cp_parser* parser)
14378 tree attributes;
14379 cp_decl_specifier_seq type_specifiers;
14380 cp_declarator *declarator;
14381 tree type_specified;
14382 const char *saved_message;
14384 /* Parse the attributes. */
14385 attributes = cp_parser_attributes_opt (parser);
14387 saved_message = parser->type_definition_forbidden_message;
14388 parser->type_definition_forbidden_message
14389 = G_("types may not be defined in a conversion-type-id");
14391 /* Parse the type-specifiers. */
14392 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14393 /*is_trailing_return=*/false,
14394 &type_specifiers);
14396 parser->type_definition_forbidden_message = saved_message;
14398 /* If that didn't work, stop. */
14399 if (type_specifiers.type == error_mark_node)
14400 return error_mark_node;
14401 /* Parse the conversion-declarator. */
14402 declarator = cp_parser_conversion_declarator_opt (parser);
14404 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14405 /*initialized=*/0, &attributes);
14406 if (attributes)
14407 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14409 /* Don't give this error when parsing tentatively. This happens to
14410 work because we always parse this definitively once. */
14411 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14412 && type_uses_auto (type_specified))
14414 if (cxx_dialect < cxx14)
14416 error ("invalid use of %<auto%> in conversion operator");
14417 return error_mark_node;
14419 else if (template_parm_scope_p ())
14420 warning (0, "use of %<auto%> in member template "
14421 "conversion operator can never be deduced");
14424 return type_specified;
14427 /* Parse an (optional) conversion-declarator.
14429 conversion-declarator:
14430 ptr-operator conversion-declarator [opt]
14434 static cp_declarator *
14435 cp_parser_conversion_declarator_opt (cp_parser* parser)
14437 enum tree_code code;
14438 tree class_type, std_attributes = NULL_TREE;
14439 cp_cv_quals cv_quals;
14441 /* We don't know if there's a ptr-operator next, or not. */
14442 cp_parser_parse_tentatively (parser);
14443 /* Try the ptr-operator. */
14444 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14445 &std_attributes);
14446 /* If it worked, look for more conversion-declarators. */
14447 if (cp_parser_parse_definitely (parser))
14449 cp_declarator *declarator;
14451 /* Parse another optional declarator. */
14452 declarator = cp_parser_conversion_declarator_opt (parser);
14454 declarator = cp_parser_make_indirect_declarator
14455 (code, class_type, cv_quals, declarator, std_attributes);
14457 return declarator;
14460 return NULL;
14463 /* Parse an (optional) ctor-initializer.
14465 ctor-initializer:
14466 : mem-initializer-list */
14468 static void
14469 cp_parser_ctor_initializer_opt (cp_parser* parser)
14471 /* If the next token is not a `:', then there is no
14472 ctor-initializer. */
14473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14475 /* Do default initialization of any bases and members. */
14476 if (DECL_CONSTRUCTOR_P (current_function_decl))
14477 finish_mem_initializers (NULL_TREE);
14478 return;
14481 /* Consume the `:' token. */
14482 cp_lexer_consume_token (parser->lexer);
14483 /* And the mem-initializer-list. */
14484 cp_parser_mem_initializer_list (parser);
14487 /* Parse a mem-initializer-list.
14489 mem-initializer-list:
14490 mem-initializer ... [opt]
14491 mem-initializer ... [opt] , mem-initializer-list */
14493 static void
14494 cp_parser_mem_initializer_list (cp_parser* parser)
14496 tree mem_initializer_list = NULL_TREE;
14497 tree target_ctor = error_mark_node;
14498 cp_token *token = cp_lexer_peek_token (parser->lexer);
14500 /* Let the semantic analysis code know that we are starting the
14501 mem-initializer-list. */
14502 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14503 error_at (token->location,
14504 "only constructors take member initializers");
14506 /* Loop through the list. */
14507 while (true)
14509 tree mem_initializer;
14511 token = cp_lexer_peek_token (parser->lexer);
14512 /* Parse the mem-initializer. */
14513 mem_initializer = cp_parser_mem_initializer (parser);
14514 /* If the next token is a `...', we're expanding member initializers. */
14515 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14516 if (ellipsis
14517 || (mem_initializer != error_mark_node
14518 && check_for_bare_parameter_packs (TREE_PURPOSE
14519 (mem_initializer))))
14521 /* Consume the `...'. */
14522 if (ellipsis)
14523 cp_lexer_consume_token (parser->lexer);
14525 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14526 can be expanded but members cannot. */
14527 if (mem_initializer != error_mark_node
14528 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14530 error_at (token->location,
14531 "cannot expand initializer for member %qD",
14532 TREE_PURPOSE (mem_initializer));
14533 mem_initializer = error_mark_node;
14536 /* Construct the pack expansion type. */
14537 if (mem_initializer != error_mark_node)
14538 mem_initializer = make_pack_expansion (mem_initializer);
14540 if (target_ctor != error_mark_node
14541 && mem_initializer != error_mark_node)
14543 error ("mem-initializer for %qD follows constructor delegation",
14544 TREE_PURPOSE (mem_initializer));
14545 mem_initializer = error_mark_node;
14547 /* Look for a target constructor. */
14548 if (mem_initializer != error_mark_node
14549 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14550 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14552 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14553 if (mem_initializer_list)
14555 error ("constructor delegation follows mem-initializer for %qD",
14556 TREE_PURPOSE (mem_initializer_list));
14557 mem_initializer = error_mark_node;
14559 target_ctor = mem_initializer;
14561 /* Add it to the list, unless it was erroneous. */
14562 if (mem_initializer != error_mark_node)
14564 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14565 mem_initializer_list = mem_initializer;
14567 /* If the next token is not a `,', we're done. */
14568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14569 break;
14570 /* Consume the `,' token. */
14571 cp_lexer_consume_token (parser->lexer);
14574 /* Perform semantic analysis. */
14575 if (DECL_CONSTRUCTOR_P (current_function_decl))
14576 finish_mem_initializers (mem_initializer_list);
14579 /* Parse a mem-initializer.
14581 mem-initializer:
14582 mem-initializer-id ( expression-list [opt] )
14583 mem-initializer-id braced-init-list
14585 GNU extension:
14587 mem-initializer:
14588 ( expression-list [opt] )
14590 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14591 class) or FIELD_DECL (for a non-static data member) to initialize;
14592 the TREE_VALUE is the expression-list. An empty initialization
14593 list is represented by void_list_node. */
14595 static tree
14596 cp_parser_mem_initializer (cp_parser* parser)
14598 tree mem_initializer_id;
14599 tree expression_list;
14600 tree member;
14601 cp_token *token = cp_lexer_peek_token (parser->lexer);
14603 /* Find out what is being initialized. */
14604 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14606 permerror (token->location,
14607 "anachronistic old-style base class initializer");
14608 mem_initializer_id = NULL_TREE;
14610 else
14612 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14613 if (mem_initializer_id == error_mark_node)
14614 return mem_initializer_id;
14616 member = expand_member_init (mem_initializer_id);
14617 if (member && !DECL_P (member))
14618 in_base_initializer = 1;
14620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14622 bool expr_non_constant_p;
14623 cp_lexer_set_source_position (parser->lexer);
14624 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14625 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14626 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14627 expression_list = build_tree_list (NULL_TREE, expression_list);
14629 else
14631 vec<tree, va_gc> *vec;
14632 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14633 /*cast_p=*/false,
14634 /*allow_expansion_p=*/true,
14635 /*non_constant_p=*/NULL);
14636 if (vec == NULL)
14637 return error_mark_node;
14638 expression_list = build_tree_list_vec (vec);
14639 release_tree_vector (vec);
14642 if (expression_list == error_mark_node)
14643 return error_mark_node;
14644 if (!expression_list)
14645 expression_list = void_type_node;
14647 in_base_initializer = 0;
14649 return member ? build_tree_list (member, expression_list) : error_mark_node;
14652 /* Parse a mem-initializer-id.
14654 mem-initializer-id:
14655 :: [opt] nested-name-specifier [opt] class-name
14656 decltype-specifier (C++11)
14657 identifier
14659 Returns a TYPE indicating the class to be initialized for the first
14660 production (and the second in C++11). Returns an IDENTIFIER_NODE
14661 indicating the data member to be initialized for the last production. */
14663 static tree
14664 cp_parser_mem_initializer_id (cp_parser* parser)
14666 bool global_scope_p;
14667 bool nested_name_specifier_p;
14668 bool template_p = false;
14669 tree id;
14671 cp_token *token = cp_lexer_peek_token (parser->lexer);
14673 /* `typename' is not allowed in this context ([temp.res]). */
14674 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14676 error_at (token->location,
14677 "keyword %<typename%> not allowed in this context (a qualified "
14678 "member initializer is implicitly a type)");
14679 cp_lexer_consume_token (parser->lexer);
14681 /* Look for the optional `::' operator. */
14682 global_scope_p
14683 = (cp_parser_global_scope_opt (parser,
14684 /*current_scope_valid_p=*/false)
14685 != NULL_TREE);
14686 /* Look for the optional nested-name-specifier. The simplest way to
14687 implement:
14689 [temp.res]
14691 The keyword `typename' is not permitted in a base-specifier or
14692 mem-initializer; in these contexts a qualified name that
14693 depends on a template-parameter is implicitly assumed to be a
14694 type name.
14696 is to assume that we have seen the `typename' keyword at this
14697 point. */
14698 nested_name_specifier_p
14699 = (cp_parser_nested_name_specifier_opt (parser,
14700 /*typename_keyword_p=*/true,
14701 /*check_dependency_p=*/true,
14702 /*type_p=*/true,
14703 /*is_declaration=*/true)
14704 != NULL_TREE);
14705 if (nested_name_specifier_p)
14706 template_p = cp_parser_optional_template_keyword (parser);
14707 /* If there is a `::' operator or a nested-name-specifier, then we
14708 are definitely looking for a class-name. */
14709 if (global_scope_p || nested_name_specifier_p)
14710 return cp_parser_class_name (parser,
14711 /*typename_keyword_p=*/true,
14712 /*template_keyword_p=*/template_p,
14713 typename_type,
14714 /*check_dependency_p=*/true,
14715 /*class_head_p=*/false,
14716 /*is_declaration=*/true);
14717 /* Otherwise, we could also be looking for an ordinary identifier. */
14718 cp_parser_parse_tentatively (parser);
14719 if (cp_lexer_next_token_is_decltype (parser->lexer))
14720 /* Try a decltype-specifier. */
14721 id = cp_parser_decltype (parser);
14722 else
14723 /* Otherwise, try a class-name. */
14724 id = cp_parser_class_name (parser,
14725 /*typename_keyword_p=*/true,
14726 /*template_keyword_p=*/false,
14727 none_type,
14728 /*check_dependency_p=*/true,
14729 /*class_head_p=*/false,
14730 /*is_declaration=*/true);
14731 /* If we found one, we're done. */
14732 if (cp_parser_parse_definitely (parser))
14733 return id;
14734 /* Otherwise, look for an ordinary identifier. */
14735 return cp_parser_identifier (parser);
14738 /* Overloading [gram.over] */
14740 /* Parse an operator-function-id.
14742 operator-function-id:
14743 operator operator
14745 Returns an IDENTIFIER_NODE for the operator which is a
14746 human-readable spelling of the identifier, e.g., `operator +'. */
14748 static cp_expr
14749 cp_parser_operator_function_id (cp_parser* parser)
14751 /* Look for the `operator' keyword. */
14752 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14753 return error_mark_node;
14754 /* And then the name of the operator itself. */
14755 return cp_parser_operator (parser);
14758 /* Return an identifier node for a user-defined literal operator.
14759 The suffix identifier is chained to the operator name identifier. */
14761 tree
14762 cp_literal_operator_id (const char* name)
14764 tree identifier;
14765 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14766 + strlen (name) + 10);
14767 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14768 identifier = get_identifier (buffer);
14770 return identifier;
14773 /* Parse an operator.
14775 operator:
14776 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14777 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14778 || ++ -- , ->* -> () []
14780 GNU Extensions:
14782 operator:
14783 <? >? <?= >?=
14785 Returns an IDENTIFIER_NODE for the operator which is a
14786 human-readable spelling of the identifier, e.g., `operator +'. */
14788 static cp_expr
14789 cp_parser_operator (cp_parser* parser)
14791 tree id = NULL_TREE;
14792 cp_token *token;
14793 bool utf8 = false;
14795 /* Peek at the next token. */
14796 token = cp_lexer_peek_token (parser->lexer);
14798 location_t start_loc = token->location;
14800 /* Figure out which operator we have. */
14801 enum tree_code op = ERROR_MARK;
14802 bool assop = false;
14803 bool consumed = false;
14804 switch (token->type)
14806 case CPP_KEYWORD:
14808 /* The keyword should be either `new' or `delete'. */
14809 if (token->keyword == RID_NEW)
14810 op = NEW_EXPR;
14811 else if (token->keyword == RID_DELETE)
14812 op = DELETE_EXPR;
14813 else
14814 break;
14816 /* Consume the `new' or `delete' token. */
14817 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14819 /* Peek at the next token. */
14820 token = cp_lexer_peek_token (parser->lexer);
14821 /* If it's a `[' token then this is the array variant of the
14822 operator. */
14823 if (token->type == CPP_OPEN_SQUARE)
14825 /* Consume the `[' token. */
14826 cp_lexer_consume_token (parser->lexer);
14827 /* Look for the `]' token. */
14828 if (cp_token *close_token
14829 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14830 end_loc = close_token->location;
14831 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14833 start_loc = make_location (start_loc, start_loc, end_loc);
14834 consumed = true;
14835 break;
14838 case CPP_PLUS:
14839 op = PLUS_EXPR;
14840 break;
14842 case CPP_MINUS:
14843 op = MINUS_EXPR;
14844 break;
14846 case CPP_MULT:
14847 op = MULT_EXPR;
14848 break;
14850 case CPP_DIV:
14851 op = TRUNC_DIV_EXPR;
14852 break;
14854 case CPP_MOD:
14855 op = TRUNC_MOD_EXPR;
14856 break;
14858 case CPP_XOR:
14859 op = BIT_XOR_EXPR;
14860 break;
14862 case CPP_AND:
14863 op = BIT_AND_EXPR;
14864 break;
14866 case CPP_OR:
14867 op = BIT_IOR_EXPR;
14868 break;
14870 case CPP_COMPL:
14871 op = BIT_NOT_EXPR;
14872 break;
14874 case CPP_NOT:
14875 op = TRUTH_NOT_EXPR;
14876 break;
14878 case CPP_EQ:
14879 assop = true;
14880 op = NOP_EXPR;
14881 break;
14883 case CPP_LESS:
14884 op = LT_EXPR;
14885 break;
14887 case CPP_GREATER:
14888 op = GT_EXPR;
14889 break;
14891 case CPP_PLUS_EQ:
14892 assop = true;
14893 op = PLUS_EXPR;
14894 break;
14896 case CPP_MINUS_EQ:
14897 assop = true;
14898 op = MINUS_EXPR;
14899 break;
14901 case CPP_MULT_EQ:
14902 assop = true;
14903 op = MULT_EXPR;
14904 break;
14906 case CPP_DIV_EQ:
14907 assop = true;
14908 op = TRUNC_DIV_EXPR;
14909 break;
14911 case CPP_MOD_EQ:
14912 assop = true;
14913 op = TRUNC_MOD_EXPR;
14914 break;
14916 case CPP_XOR_EQ:
14917 assop = true;
14918 op = BIT_XOR_EXPR;
14919 break;
14921 case CPP_AND_EQ:
14922 assop = true;
14923 op = BIT_AND_EXPR;
14924 break;
14926 case CPP_OR_EQ:
14927 assop = true;
14928 op = BIT_IOR_EXPR;
14929 break;
14931 case CPP_LSHIFT:
14932 op = LSHIFT_EXPR;
14933 break;
14935 case CPP_RSHIFT:
14936 op = RSHIFT_EXPR;
14937 break;
14939 case CPP_LSHIFT_EQ:
14940 assop = true;
14941 op = LSHIFT_EXPR;
14942 break;
14944 case CPP_RSHIFT_EQ:
14945 assop = true;
14946 op = RSHIFT_EXPR;
14947 break;
14949 case CPP_EQ_EQ:
14950 op = EQ_EXPR;
14951 break;
14953 case CPP_NOT_EQ:
14954 op = NE_EXPR;
14955 break;
14957 case CPP_LESS_EQ:
14958 op = LE_EXPR;
14959 break;
14961 case CPP_GREATER_EQ:
14962 op = GE_EXPR;
14963 break;
14965 case CPP_AND_AND:
14966 op = TRUTH_ANDIF_EXPR;
14967 break;
14969 case CPP_OR_OR:
14970 op = TRUTH_ORIF_EXPR;
14971 break;
14973 case CPP_PLUS_PLUS:
14974 op = POSTINCREMENT_EXPR;
14975 break;
14977 case CPP_MINUS_MINUS:
14978 op = PREDECREMENT_EXPR;
14979 break;
14981 case CPP_COMMA:
14982 op = COMPOUND_EXPR;
14983 break;
14985 case CPP_DEREF_STAR:
14986 op = MEMBER_REF;
14987 break;
14989 case CPP_DEREF:
14990 op = COMPONENT_REF;
14991 break;
14993 case CPP_OPEN_PAREN:
14995 /* Consume the `('. */
14996 matching_parens parens;
14997 parens.consume_open (parser);
14998 /* Look for the matching `)'. */
14999 parens.require_close (parser);
15000 op = CALL_EXPR;
15001 consumed = true;
15002 break;
15005 case CPP_OPEN_SQUARE:
15006 /* Consume the `['. */
15007 cp_lexer_consume_token (parser->lexer);
15008 /* Look for the matching `]'. */
15009 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15010 op = ARRAY_REF;
15011 consumed = true;
15012 break;
15014 case CPP_UTF8STRING:
15015 case CPP_UTF8STRING_USERDEF:
15016 utf8 = true;
15017 /* FALLTHRU */
15018 case CPP_STRING:
15019 case CPP_WSTRING:
15020 case CPP_STRING16:
15021 case CPP_STRING32:
15022 case CPP_STRING_USERDEF:
15023 case CPP_WSTRING_USERDEF:
15024 case CPP_STRING16_USERDEF:
15025 case CPP_STRING32_USERDEF:
15027 tree str, string_tree;
15028 int sz, len;
15030 if (cxx_dialect == cxx98)
15031 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15033 /* Consume the string. */
15034 str = cp_parser_string_literal (parser, /*translate=*/true,
15035 /*wide_ok=*/true, /*lookup_udlit=*/false);
15036 if (str == error_mark_node)
15037 return error_mark_node;
15038 else if (TREE_CODE (str) == USERDEF_LITERAL)
15040 string_tree = USERDEF_LITERAL_VALUE (str);
15041 id = USERDEF_LITERAL_SUFFIX_ID (str);
15043 else
15045 string_tree = str;
15046 /* Look for the suffix identifier. */
15047 token = cp_lexer_peek_token (parser->lexer);
15048 if (token->type == CPP_NAME)
15049 id = cp_parser_identifier (parser);
15050 else if (token->type == CPP_KEYWORD)
15052 error ("unexpected keyword;"
15053 " remove space between quotes and suffix identifier");
15054 return error_mark_node;
15056 else
15058 error ("expected suffix identifier");
15059 return error_mark_node;
15062 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15063 (TREE_TYPE (TREE_TYPE (string_tree))));
15064 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15065 if (len != 0)
15067 error ("expected empty string after %<operator%> keyword");
15068 return error_mark_node;
15070 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15071 != char_type_node)
15073 error ("invalid encoding prefix in literal operator");
15074 return error_mark_node;
15076 if (id != error_mark_node)
15078 const char *name = IDENTIFIER_POINTER (id);
15079 id = cp_literal_operator_id (name);
15081 return id;
15084 default:
15085 /* Anything else is an error. */
15086 break;
15089 /* If we have selected an identifier, we need to consume the
15090 operator token. */
15091 if (op != ERROR_MARK)
15093 id = ovl_op_identifier (assop, op);
15094 if (!consumed)
15095 cp_lexer_consume_token (parser->lexer);
15097 /* Otherwise, no valid operator name was present. */
15098 else
15100 cp_parser_error (parser, "expected operator");
15101 id = error_mark_node;
15104 return cp_expr (id, start_loc);
15107 /* Parse a template-declaration.
15109 template-declaration:
15110 export [opt] template < template-parameter-list > declaration
15112 If MEMBER_P is TRUE, this template-declaration occurs within a
15113 class-specifier.
15115 The grammar rule given by the standard isn't correct. What
15116 is really meant is:
15118 template-declaration:
15119 export [opt] template-parameter-list-seq
15120 decl-specifier-seq [opt] init-declarator [opt] ;
15121 export [opt] template-parameter-list-seq
15122 function-definition
15124 template-parameter-list-seq:
15125 template-parameter-list-seq [opt]
15126 template < template-parameter-list >
15128 Concept Extensions:
15130 template-parameter-list-seq:
15131 template < template-parameter-list > requires-clause [opt]
15133 requires-clause:
15134 requires logical-or-expression */
15136 static void
15137 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15139 /* Check for `export'. */
15140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15142 /* Consume the `export' token. */
15143 cp_lexer_consume_token (parser->lexer);
15144 /* Warn that we do not support `export'. */
15145 warning (0, "keyword %<export%> not implemented, and will be ignored");
15148 cp_parser_template_declaration_after_export (parser, member_p);
15151 /* Parse a template-parameter-list.
15153 template-parameter-list:
15154 template-parameter
15155 template-parameter-list , template-parameter
15157 Returns a TREE_LIST. Each node represents a template parameter.
15158 The nodes are connected via their TREE_CHAINs. */
15160 static tree
15161 cp_parser_template_parameter_list (cp_parser* parser)
15163 tree parameter_list = NULL_TREE;
15165 begin_template_parm_list ();
15167 /* The loop below parses the template parms. We first need to know
15168 the total number of template parms to be able to compute proper
15169 canonical types of each dependent type. So after the loop, when
15170 we know the total number of template parms,
15171 end_template_parm_list computes the proper canonical types and
15172 fixes up the dependent types accordingly. */
15173 while (true)
15175 tree parameter;
15176 bool is_non_type;
15177 bool is_parameter_pack;
15178 location_t parm_loc;
15180 /* Parse the template-parameter. */
15181 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15182 parameter = cp_parser_template_parameter (parser,
15183 &is_non_type,
15184 &is_parameter_pack);
15185 /* Add it to the list. */
15186 if (parameter != error_mark_node)
15187 parameter_list = process_template_parm (parameter_list,
15188 parm_loc,
15189 parameter,
15190 is_non_type,
15191 is_parameter_pack);
15192 else
15194 tree err_parm = build_tree_list (parameter, parameter);
15195 parameter_list = chainon (parameter_list, err_parm);
15198 /* If the next token is not a `,', we're done. */
15199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15200 break;
15201 /* Otherwise, consume the `,' token. */
15202 cp_lexer_consume_token (parser->lexer);
15205 return end_template_parm_list (parameter_list);
15208 /* Parse a introduction-list.
15210 introduction-list:
15211 introduced-parameter
15212 introduction-list , introduced-parameter
15214 introduced-parameter:
15215 ...[opt] identifier
15217 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15218 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15219 WILDCARD_DECL will also have DECL_NAME set and token location in
15220 DECL_SOURCE_LOCATION. */
15222 static tree
15223 cp_parser_introduction_list (cp_parser *parser)
15225 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15227 while (true)
15229 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15230 if (is_pack)
15231 cp_lexer_consume_token (parser->lexer);
15233 /* Build placeholder. */
15234 tree parm = build_nt (WILDCARD_DECL);
15235 DECL_SOURCE_LOCATION (parm)
15236 = cp_lexer_peek_token (parser->lexer)->location;
15237 DECL_NAME (parm) = cp_parser_identifier (parser);
15238 WILDCARD_PACK_P (parm) = is_pack;
15239 vec_safe_push (introduction_vec, parm);
15241 /* If the next token is not a `,', we're done. */
15242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15243 break;
15244 /* Otherwise, consume the `,' token. */
15245 cp_lexer_consume_token (parser->lexer);
15248 /* Convert the vec into a TREE_VEC. */
15249 tree introduction_list = make_tree_vec (introduction_vec->length ());
15250 unsigned int n;
15251 tree parm;
15252 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15253 TREE_VEC_ELT (introduction_list, n) = parm;
15255 release_tree_vector (introduction_vec);
15256 return introduction_list;
15259 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15260 is an abstract declarator. */
15262 static inline cp_declarator*
15263 get_id_declarator (cp_declarator *declarator)
15265 cp_declarator *d = declarator;
15266 while (d && d->kind != cdk_id)
15267 d = d->declarator;
15268 return d;
15271 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15272 is an abstract declarator. */
15274 static inline tree
15275 get_unqualified_id (cp_declarator *declarator)
15277 declarator = get_id_declarator (declarator);
15278 if (declarator)
15279 return declarator->u.id.unqualified_name;
15280 else
15281 return NULL_TREE;
15284 /* Returns true if DECL represents a constrained-parameter. */
15286 static inline bool
15287 is_constrained_parameter (tree decl)
15289 return (decl
15290 && TREE_CODE (decl) == TYPE_DECL
15291 && CONSTRAINED_PARM_CONCEPT (decl)
15292 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15295 /* Returns true if PARM declares a constrained-parameter. */
15297 static inline bool
15298 is_constrained_parameter (cp_parameter_declarator *parm)
15300 return is_constrained_parameter (parm->decl_specifiers.type);
15303 /* Check that the type parameter is only a declarator-id, and that its
15304 type is not cv-qualified. */
15306 bool
15307 cp_parser_check_constrained_type_parm (cp_parser *parser,
15308 cp_parameter_declarator *parm)
15310 if (!parm->declarator)
15311 return true;
15313 if (parm->declarator->kind != cdk_id)
15315 cp_parser_error (parser, "invalid constrained type parameter");
15316 return false;
15319 /* Don't allow cv-qualified type parameters. */
15320 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15321 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15323 cp_parser_error (parser, "cv-qualified type parameter");
15324 return false;
15327 return true;
15330 /* Finish parsing/processing a template type parameter and checking
15331 various restrictions. */
15333 static inline tree
15334 cp_parser_constrained_type_template_parm (cp_parser *parser,
15335 tree id,
15336 cp_parameter_declarator* parmdecl)
15338 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15339 return finish_template_type_parm (class_type_node, id);
15340 else
15341 return error_mark_node;
15344 static tree
15345 finish_constrained_template_template_parm (tree proto, tree id)
15347 /* FIXME: This should probably be copied, and we may need to adjust
15348 the template parameter depths. */
15349 tree saved_parms = current_template_parms;
15350 begin_template_parm_list ();
15351 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15352 end_template_parm_list ();
15354 tree parm = finish_template_template_parm (class_type_node, id);
15355 current_template_parms = saved_parms;
15357 return parm;
15360 /* Finish parsing/processing a template template parameter by borrowing
15361 the template parameter list from the prototype parameter. */
15363 static tree
15364 cp_parser_constrained_template_template_parm (cp_parser *parser,
15365 tree proto,
15366 tree id,
15367 cp_parameter_declarator *parmdecl)
15369 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15370 return error_mark_node;
15371 return finish_constrained_template_template_parm (proto, id);
15374 /* Create a new non-type template parameter from the given PARM
15375 declarator. */
15377 static tree
15378 constrained_non_type_template_parm (bool *is_non_type,
15379 cp_parameter_declarator *parm)
15381 *is_non_type = true;
15382 cp_declarator *decl = parm->declarator;
15383 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15384 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15385 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15388 /* Build a constrained template parameter based on the PARMDECL
15389 declarator. The type of PARMDECL is the constrained type, which
15390 refers to the prototype template parameter that ultimately
15391 specifies the type of the declared parameter. */
15393 static tree
15394 finish_constrained_parameter (cp_parser *parser,
15395 cp_parameter_declarator *parmdecl,
15396 bool *is_non_type,
15397 bool *is_parameter_pack)
15399 tree decl = parmdecl->decl_specifiers.type;
15400 tree id = get_unqualified_id (parmdecl->declarator);
15401 tree def = parmdecl->default_argument;
15402 tree proto = DECL_INITIAL (decl);
15404 /* A template parameter constrained by a variadic concept shall also
15405 be declared as a template parameter pack. */
15406 bool is_variadic = template_parameter_pack_p (proto);
15407 if (is_variadic && !*is_parameter_pack)
15408 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15410 /* Build the parameter. Return an error if the declarator was invalid. */
15411 tree parm;
15412 if (TREE_CODE (proto) == TYPE_DECL)
15413 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15414 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15415 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15416 parmdecl);
15417 else
15418 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15419 if (parm == error_mark_node)
15420 return error_mark_node;
15422 /* Finish the parameter decl and create a node attaching the
15423 default argument and constraint. */
15424 parm = build_tree_list (def, parm);
15425 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15427 return parm;
15430 /* Returns true if the parsed type actually represents the declaration
15431 of a type template-parameter. */
15433 static inline bool
15434 declares_constrained_type_template_parameter (tree type)
15436 return (is_constrained_parameter (type)
15437 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15441 /* Returns true if the parsed type actually represents the declaration of
15442 a template template-parameter. */
15444 static bool
15445 declares_constrained_template_template_parameter (tree type)
15447 return (is_constrained_parameter (type)
15448 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15451 /* Parse a default argument for a type template-parameter.
15452 Note that diagnostics are handled in cp_parser_template_parameter. */
15454 static tree
15455 cp_parser_default_type_template_argument (cp_parser *parser)
15457 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15459 /* Consume the `=' token. */
15460 cp_lexer_consume_token (parser->lexer);
15462 cp_token *token = cp_lexer_peek_token (parser->lexer);
15464 /* Parse the default-argument. */
15465 push_deferring_access_checks (dk_no_deferred);
15466 tree default_argument = cp_parser_type_id (parser);
15467 pop_deferring_access_checks ();
15469 if (flag_concepts && type_uses_auto (default_argument))
15471 error_at (token->location,
15472 "invalid use of %<auto%> in default template argument");
15473 return error_mark_node;
15476 return default_argument;
15479 /* Parse a default argument for a template template-parameter. */
15481 static tree
15482 cp_parser_default_template_template_argument (cp_parser *parser)
15484 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15486 bool is_template;
15488 /* Consume the `='. */
15489 cp_lexer_consume_token (parser->lexer);
15490 /* Parse the id-expression. */
15491 push_deferring_access_checks (dk_no_deferred);
15492 /* save token before parsing the id-expression, for error
15493 reporting */
15494 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15495 tree default_argument
15496 = cp_parser_id_expression (parser,
15497 /*template_keyword_p=*/false,
15498 /*check_dependency_p=*/true,
15499 /*template_p=*/&is_template,
15500 /*declarator_p=*/false,
15501 /*optional_p=*/false);
15502 if (TREE_CODE (default_argument) == TYPE_DECL)
15503 /* If the id-expression was a template-id that refers to
15504 a template-class, we already have the declaration here,
15505 so no further lookup is needed. */
15507 else
15508 /* Look up the name. */
15509 default_argument
15510 = cp_parser_lookup_name (parser, default_argument,
15511 none_type,
15512 /*is_template=*/is_template,
15513 /*is_namespace=*/false,
15514 /*check_dependency=*/true,
15515 /*ambiguous_decls=*/NULL,
15516 token->location);
15517 /* See if the default argument is valid. */
15518 default_argument = check_template_template_default_arg (default_argument);
15519 pop_deferring_access_checks ();
15520 return default_argument;
15523 /* Parse a template-parameter.
15525 template-parameter:
15526 type-parameter
15527 parameter-declaration
15529 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15530 the parameter. The TREE_PURPOSE is the default value, if any.
15531 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15532 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15533 set to true iff this parameter is a parameter pack. */
15535 static tree
15536 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15537 bool *is_parameter_pack)
15539 cp_token *token;
15540 cp_parameter_declarator *parameter_declarator;
15541 tree parm;
15543 /* Assume it is a type parameter or a template parameter. */
15544 *is_non_type = false;
15545 /* Assume it not a parameter pack. */
15546 *is_parameter_pack = false;
15547 /* Peek at the next token. */
15548 token = cp_lexer_peek_token (parser->lexer);
15549 /* If it is `template', we have a type-parameter. */
15550 if (token->keyword == RID_TEMPLATE)
15551 return cp_parser_type_parameter (parser, is_parameter_pack);
15552 /* If it is `class' or `typename' we do not know yet whether it is a
15553 type parameter or a non-type parameter. Consider:
15555 template <typename T, typename T::X X> ...
15559 template <class C, class D*> ...
15561 Here, the first parameter is a type parameter, and the second is
15562 a non-type parameter. We can tell by looking at the token after
15563 the identifier -- if it is a `,', `=', or `>' then we have a type
15564 parameter. */
15565 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15567 /* Peek at the token after `class' or `typename'. */
15568 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15569 /* If it's an ellipsis, we have a template type parameter
15570 pack. */
15571 if (token->type == CPP_ELLIPSIS)
15572 return cp_parser_type_parameter (parser, is_parameter_pack);
15573 /* If it's an identifier, skip it. */
15574 if (token->type == CPP_NAME)
15575 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15576 /* Now, see if the token looks like the end of a template
15577 parameter. */
15578 if (token->type == CPP_COMMA
15579 || token->type == CPP_EQ
15580 || token->type == CPP_GREATER)
15581 return cp_parser_type_parameter (parser, is_parameter_pack);
15584 /* Otherwise, it is a non-type parameter or a constrained parameter.
15586 [temp.param]
15588 When parsing a default template-argument for a non-type
15589 template-parameter, the first non-nested `>' is taken as the end
15590 of the template parameter-list rather than a greater-than
15591 operator. */
15592 parameter_declarator
15593 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15594 /*parenthesized_p=*/NULL);
15596 if (!parameter_declarator)
15597 return error_mark_node;
15599 /* If the parameter declaration is marked as a parameter pack, set
15600 *IS_PARAMETER_PACK to notify the caller. */
15601 if (parameter_declarator->template_parameter_pack_p)
15602 *is_parameter_pack = true;
15604 if (parameter_declarator->default_argument)
15606 /* Can happen in some cases of erroneous input (c++/34892). */
15607 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15608 /* Consume the `...' for better error recovery. */
15609 cp_lexer_consume_token (parser->lexer);
15612 // The parameter may have been constrained.
15613 if (is_constrained_parameter (parameter_declarator))
15614 return finish_constrained_parameter (parser,
15615 parameter_declarator,
15616 is_non_type,
15617 is_parameter_pack);
15619 // Now we're sure that the parameter is a non-type parameter.
15620 *is_non_type = true;
15622 parm = grokdeclarator (parameter_declarator->declarator,
15623 &parameter_declarator->decl_specifiers,
15624 TPARM, /*initialized=*/0,
15625 /*attrlist=*/NULL);
15626 if (parm == error_mark_node)
15627 return error_mark_node;
15629 return build_tree_list (parameter_declarator->default_argument, parm);
15632 /* Parse a type-parameter.
15634 type-parameter:
15635 class identifier [opt]
15636 class identifier [opt] = type-id
15637 typename identifier [opt]
15638 typename identifier [opt] = type-id
15639 template < template-parameter-list > class identifier [opt]
15640 template < template-parameter-list > class identifier [opt]
15641 = id-expression
15643 GNU Extension (variadic templates):
15645 type-parameter:
15646 class ... identifier [opt]
15647 typename ... identifier [opt]
15649 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15650 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15651 the declaration of the parameter.
15653 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15655 static tree
15656 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15658 cp_token *token;
15659 tree parameter;
15661 /* Look for a keyword to tell us what kind of parameter this is. */
15662 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15663 if (!token)
15664 return error_mark_node;
15666 switch (token->keyword)
15668 case RID_CLASS:
15669 case RID_TYPENAME:
15671 tree identifier;
15672 tree default_argument;
15674 /* If the next token is an ellipsis, we have a template
15675 argument pack. */
15676 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15678 /* Consume the `...' token. */
15679 cp_lexer_consume_token (parser->lexer);
15680 maybe_warn_variadic_templates ();
15682 *is_parameter_pack = true;
15685 /* If the next token is an identifier, then it names the
15686 parameter. */
15687 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15688 identifier = cp_parser_identifier (parser);
15689 else
15690 identifier = NULL_TREE;
15692 /* Create the parameter. */
15693 parameter = finish_template_type_parm (class_type_node, identifier);
15695 /* If the next token is an `=', we have a default argument. */
15696 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15698 default_argument
15699 = cp_parser_default_type_template_argument (parser);
15701 /* Template parameter packs cannot have default
15702 arguments. */
15703 if (*is_parameter_pack)
15705 if (identifier)
15706 error_at (token->location,
15707 "template parameter pack %qD cannot have a "
15708 "default argument", identifier);
15709 else
15710 error_at (token->location,
15711 "template parameter packs cannot have "
15712 "default arguments");
15713 default_argument = NULL_TREE;
15715 else if (check_for_bare_parameter_packs (default_argument))
15716 default_argument = error_mark_node;
15718 else
15719 default_argument = NULL_TREE;
15721 /* Create the combined representation of the parameter and the
15722 default argument. */
15723 parameter = build_tree_list (default_argument, parameter);
15725 break;
15727 case RID_TEMPLATE:
15729 tree identifier;
15730 tree default_argument;
15732 /* Look for the `<'. */
15733 cp_parser_require (parser, CPP_LESS, RT_LESS);
15734 /* Parse the template-parameter-list. */
15735 cp_parser_template_parameter_list (parser);
15736 /* Look for the `>'. */
15737 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15739 // If template requirements are present, parse them.
15740 if (flag_concepts)
15742 tree reqs = get_shorthand_constraints (current_template_parms);
15743 if (tree r = cp_parser_requires_clause_opt (parser))
15744 reqs = conjoin_constraints (reqs, normalize_expression (r));
15745 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15748 /* Look for the `class' or 'typename' keywords. */
15749 cp_parser_type_parameter_key (parser);
15750 /* If the next token is an ellipsis, we have a template
15751 argument pack. */
15752 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15754 /* Consume the `...' token. */
15755 cp_lexer_consume_token (parser->lexer);
15756 maybe_warn_variadic_templates ();
15758 *is_parameter_pack = true;
15760 /* If the next token is an `=', then there is a
15761 default-argument. If the next token is a `>', we are at
15762 the end of the parameter-list. If the next token is a `,',
15763 then we are at the end of this parameter. */
15764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15765 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15766 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15768 identifier = cp_parser_identifier (parser);
15769 /* Treat invalid names as if the parameter were nameless. */
15770 if (identifier == error_mark_node)
15771 identifier = NULL_TREE;
15773 else
15774 identifier = NULL_TREE;
15776 /* Create the template parameter. */
15777 parameter = finish_template_template_parm (class_type_node,
15778 identifier);
15780 /* If the next token is an `=', then there is a
15781 default-argument. */
15782 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15784 default_argument
15785 = cp_parser_default_template_template_argument (parser);
15787 /* Template parameter packs cannot have default
15788 arguments. */
15789 if (*is_parameter_pack)
15791 if (identifier)
15792 error_at (token->location,
15793 "template parameter pack %qD cannot "
15794 "have a default argument",
15795 identifier);
15796 else
15797 error_at (token->location, "template parameter packs cannot "
15798 "have default arguments");
15799 default_argument = NULL_TREE;
15802 else
15803 default_argument = NULL_TREE;
15805 /* Create the combined representation of the parameter and the
15806 default argument. */
15807 parameter = build_tree_list (default_argument, parameter);
15809 break;
15811 default:
15812 gcc_unreachable ();
15813 break;
15816 return parameter;
15819 /* Parse a template-id.
15821 template-id:
15822 template-name < template-argument-list [opt] >
15824 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15825 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15826 returned. Otherwise, if the template-name names a function, or set
15827 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15828 names a class, returns a TYPE_DECL for the specialization.
15830 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15831 uninstantiated templates. */
15833 static tree
15834 cp_parser_template_id (cp_parser *parser,
15835 bool template_keyword_p,
15836 bool check_dependency_p,
15837 enum tag_types tag_type,
15838 bool is_declaration)
15840 tree templ;
15841 tree arguments;
15842 tree template_id;
15843 cp_token_position start_of_id = 0;
15844 cp_token *next_token = NULL, *next_token_2 = NULL;
15845 bool is_identifier;
15847 /* If the next token corresponds to a template-id, there is no need
15848 to reparse it. */
15849 cp_token *token = cp_lexer_peek_token (parser->lexer);
15850 if (token->type == CPP_TEMPLATE_ID)
15852 cp_lexer_consume_token (parser->lexer);
15853 return saved_checks_value (token->u.tree_check_value);
15856 /* Avoid performing name lookup if there is no possibility of
15857 finding a template-id. */
15858 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15859 || (token->type == CPP_NAME
15860 && !cp_parser_nth_token_starts_template_argument_list_p
15861 (parser, 2)))
15863 cp_parser_error (parser, "expected template-id");
15864 return error_mark_node;
15867 /* Remember where the template-id starts. */
15868 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15869 start_of_id = cp_lexer_token_position (parser->lexer, false);
15871 push_deferring_access_checks (dk_deferred);
15873 /* Parse the template-name. */
15874 is_identifier = false;
15875 templ = cp_parser_template_name (parser, template_keyword_p,
15876 check_dependency_p,
15877 is_declaration,
15878 tag_type,
15879 &is_identifier);
15880 if (templ == error_mark_node || is_identifier)
15882 pop_deferring_access_checks ();
15883 return templ;
15886 /* Since we're going to preserve any side-effects from this parse, set up a
15887 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15888 in the template arguments. */
15889 tentative_firewall firewall (parser);
15891 /* If we find the sequence `[:' after a template-name, it's probably
15892 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15893 parse correctly the argument list. */
15894 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15895 == CPP_OPEN_SQUARE)
15896 && next_token->flags & DIGRAPH
15897 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15898 == CPP_COLON)
15899 && !(next_token_2->flags & PREV_WHITE))
15901 cp_parser_parse_tentatively (parser);
15902 /* Change `:' into `::'. */
15903 next_token_2->type = CPP_SCOPE;
15904 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15905 CPP_LESS. */
15906 cp_lexer_consume_token (parser->lexer);
15908 /* Parse the arguments. */
15909 arguments = cp_parser_enclosed_template_argument_list (parser);
15910 if (!cp_parser_parse_definitely (parser))
15912 /* If we couldn't parse an argument list, then we revert our changes
15913 and return simply an error. Maybe this is not a template-id
15914 after all. */
15915 next_token_2->type = CPP_COLON;
15916 cp_parser_error (parser, "expected %<<%>");
15917 pop_deferring_access_checks ();
15918 return error_mark_node;
15920 /* Otherwise, emit an error about the invalid digraph, but continue
15921 parsing because we got our argument list. */
15922 if (permerror (next_token->location,
15923 "%<<::%> cannot begin a template-argument list"))
15925 static bool hint = false;
15926 inform (next_token->location,
15927 "%<<:%> is an alternate spelling for %<[%>."
15928 " Insert whitespace between %<<%> and %<::%>");
15929 if (!hint && !flag_permissive)
15931 inform (next_token->location, "(if you use %<-fpermissive%> "
15932 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15933 "accept your code)");
15934 hint = true;
15938 else
15940 /* Look for the `<' that starts the template-argument-list. */
15941 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15943 pop_deferring_access_checks ();
15944 return error_mark_node;
15946 /* Parse the arguments. */
15947 arguments = cp_parser_enclosed_template_argument_list (parser);
15950 /* Set the location to be of the form:
15951 template-name < template-argument-list [opt] >
15952 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15953 with caret == start at the start of the template-name,
15954 ranging until the closing '>'. */
15955 location_t finish_loc
15956 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15957 location_t combined_loc
15958 = make_location (token->location, token->location, finish_loc);
15960 /* Check for concepts autos where they don't belong. We could
15961 identify types in some cases of idnetifier TEMPL, looking ahead
15962 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15963 types. We reject them in functions, but if what we have is an
15964 identifier, even with none_type we can't conclude it's NOT a
15965 type, we have to wait for template substitution. */
15966 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15967 template_id = error_mark_node;
15968 /* Build a representation of the specialization. */
15969 else if (identifier_p (templ))
15970 template_id = build_min_nt_loc (combined_loc,
15971 TEMPLATE_ID_EXPR,
15972 templ, arguments);
15973 else if (DECL_TYPE_TEMPLATE_P (templ)
15974 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15976 bool entering_scope;
15977 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15978 template (rather than some instantiation thereof) only if
15979 is not nested within some other construct. For example, in
15980 "template <typename T> void f(T) { A<T>::", A<T> is just an
15981 instantiation of A. */
15982 entering_scope = (template_parm_scope_p ()
15983 && cp_lexer_next_token_is (parser->lexer,
15984 CPP_SCOPE));
15985 template_id
15986 = finish_template_type (templ, arguments, entering_scope);
15988 /* A template-like identifier may be a partial concept id. */
15989 else if (flag_concepts
15990 && (template_id = (cp_parser_maybe_partial_concept_id
15991 (parser, templ, arguments))))
15992 return template_id;
15993 else if (variable_template_p (templ))
15995 template_id = lookup_template_variable (templ, arguments);
15996 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15997 SET_EXPR_LOCATION (template_id, combined_loc);
15999 else
16001 /* If it's not a class-template or a template-template, it should be
16002 a function-template. */
16003 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16004 || TREE_CODE (templ) == OVERLOAD
16005 || BASELINK_P (templ)));
16007 template_id = lookup_template_function (templ, arguments);
16008 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16009 SET_EXPR_LOCATION (template_id, combined_loc);
16012 /* If parsing tentatively, replace the sequence of tokens that makes
16013 up the template-id with a CPP_TEMPLATE_ID token. That way,
16014 should we re-parse the token stream, we will not have to repeat
16015 the effort required to do the parse, nor will we issue duplicate
16016 error messages about problems during instantiation of the
16017 template. */
16018 if (start_of_id
16019 /* Don't do this if we had a parse error in a declarator; re-parsing
16020 might succeed if a name changes meaning (60361). */
16021 && !(cp_parser_error_occurred (parser)
16022 && cp_parser_parsing_tentatively (parser)
16023 && parser->in_declarator_p))
16025 /* Reset the contents of the START_OF_ID token. */
16026 token->type = CPP_TEMPLATE_ID;
16027 token->location = combined_loc;
16029 /* Retrieve any deferred checks. Do not pop this access checks yet
16030 so the memory will not be reclaimed during token replacing below. */
16031 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16032 token->u.tree_check_value->value = template_id;
16033 token->u.tree_check_value->checks = get_deferred_access_checks ();
16034 token->keyword = RID_MAX;
16036 /* Purge all subsequent tokens. */
16037 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16039 /* ??? Can we actually assume that, if template_id ==
16040 error_mark_node, we will have issued a diagnostic to the
16041 user, as opposed to simply marking the tentative parse as
16042 failed? */
16043 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16044 error_at (token->location, "parse error in template argument list");
16047 pop_to_parent_deferring_access_checks ();
16048 return template_id;
16051 /* Parse a template-name.
16053 template-name:
16054 identifier
16056 The standard should actually say:
16058 template-name:
16059 identifier
16060 operator-function-id
16062 A defect report has been filed about this issue.
16064 A conversion-function-id cannot be a template name because they cannot
16065 be part of a template-id. In fact, looking at this code:
16067 a.operator K<int>()
16069 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16070 It is impossible to call a templated conversion-function-id with an
16071 explicit argument list, since the only allowed template parameter is
16072 the type to which it is converting.
16074 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16075 `template' keyword, in a construction like:
16077 T::template f<3>()
16079 In that case `f' is taken to be a template-name, even though there
16080 is no way of knowing for sure.
16082 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16083 name refers to a set of overloaded functions, at least one of which
16084 is a template, or an IDENTIFIER_NODE with the name of the template,
16085 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16086 names are looked up inside uninstantiated templates. */
16088 static tree
16089 cp_parser_template_name (cp_parser* parser,
16090 bool template_keyword_p,
16091 bool check_dependency_p,
16092 bool is_declaration,
16093 enum tag_types tag_type,
16094 bool *is_identifier)
16096 tree identifier;
16097 tree decl;
16098 cp_token *token = cp_lexer_peek_token (parser->lexer);
16100 /* If the next token is `operator', then we have either an
16101 operator-function-id or a conversion-function-id. */
16102 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16104 /* We don't know whether we're looking at an
16105 operator-function-id or a conversion-function-id. */
16106 cp_parser_parse_tentatively (parser);
16107 /* Try an operator-function-id. */
16108 identifier = cp_parser_operator_function_id (parser);
16109 /* If that didn't work, try a conversion-function-id. */
16110 if (!cp_parser_parse_definitely (parser))
16112 cp_parser_error (parser, "expected template-name");
16113 return error_mark_node;
16116 /* Look for the identifier. */
16117 else
16118 identifier = cp_parser_identifier (parser);
16120 /* If we didn't find an identifier, we don't have a template-id. */
16121 if (identifier == error_mark_node)
16122 return error_mark_node;
16124 /* If the name immediately followed the `template' keyword, then it
16125 is a template-name. However, if the next token is not `<', then
16126 we do not treat it as a template-name, since it is not being used
16127 as part of a template-id. This enables us to handle constructs
16128 like:
16130 template <typename T> struct S { S(); };
16131 template <typename T> S<T>::S();
16133 correctly. We would treat `S' as a template -- if it were `S<T>'
16134 -- but we do not if there is no `<'. */
16136 if (processing_template_decl
16137 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16139 /* In a declaration, in a dependent context, we pretend that the
16140 "template" keyword was present in order to improve error
16141 recovery. For example, given:
16143 template <typename T> void f(T::X<int>);
16145 we want to treat "X<int>" as a template-id. */
16146 if (is_declaration
16147 && !template_keyword_p
16148 && parser->scope && TYPE_P (parser->scope)
16149 && check_dependency_p
16150 && dependent_scope_p (parser->scope)
16151 /* Do not do this for dtors (or ctors), since they never
16152 need the template keyword before their name. */
16153 && !constructor_name_p (identifier, parser->scope))
16155 cp_token_position start = 0;
16157 /* Explain what went wrong. */
16158 error_at (token->location, "non-template %qD used as template",
16159 identifier);
16160 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16161 parser->scope, identifier);
16162 /* If parsing tentatively, find the location of the "<" token. */
16163 if (cp_parser_simulate_error (parser))
16164 start = cp_lexer_token_position (parser->lexer, true);
16165 /* Parse the template arguments so that we can issue error
16166 messages about them. */
16167 cp_lexer_consume_token (parser->lexer);
16168 cp_parser_enclosed_template_argument_list (parser);
16169 /* Skip tokens until we find a good place from which to
16170 continue parsing. */
16171 cp_parser_skip_to_closing_parenthesis (parser,
16172 /*recovering=*/true,
16173 /*or_comma=*/true,
16174 /*consume_paren=*/false);
16175 /* If parsing tentatively, permanently remove the
16176 template argument list. That will prevent duplicate
16177 error messages from being issued about the missing
16178 "template" keyword. */
16179 if (start)
16180 cp_lexer_purge_tokens_after (parser->lexer, start);
16181 if (is_identifier)
16182 *is_identifier = true;
16183 parser->context->object_type = NULL_TREE;
16184 return identifier;
16187 /* If the "template" keyword is present, then there is generally
16188 no point in doing name-lookup, so we just return IDENTIFIER.
16189 But, if the qualifying scope is non-dependent then we can
16190 (and must) do name-lookup normally. */
16191 if (template_keyword_p)
16193 tree scope = (parser->scope ? parser->scope
16194 : parser->context->object_type);
16195 if (scope && TYPE_P (scope)
16196 && (!CLASS_TYPE_P (scope)
16197 || (check_dependency_p && dependent_type_p (scope))))
16199 /* We're optimizing away the call to cp_parser_lookup_name, but
16200 we still need to do this. */
16201 parser->context->object_type = NULL_TREE;
16202 return identifier;
16207 /* Look up the name. */
16208 decl = cp_parser_lookup_name (parser, identifier,
16209 tag_type,
16210 /*is_template=*/true,
16211 /*is_namespace=*/false,
16212 check_dependency_p,
16213 /*ambiguous_decls=*/NULL,
16214 token->location);
16216 decl = strip_using_decl (decl);
16218 /* If DECL is a template, then the name was a template-name. */
16219 if (TREE_CODE (decl) == TEMPLATE_DECL)
16221 if (TREE_DEPRECATED (decl)
16222 && deprecated_state != DEPRECATED_SUPPRESS)
16223 warn_deprecated_use (decl, NULL_TREE);
16225 else
16227 /* The standard does not explicitly indicate whether a name that
16228 names a set of overloaded declarations, some of which are
16229 templates, is a template-name. However, such a name should
16230 be a template-name; otherwise, there is no way to form a
16231 template-id for the overloaded templates. */
16232 bool found = false;
16234 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16235 !found && iter; ++iter)
16236 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16237 found = true;
16239 if (!found)
16241 /* The name does not name a template. */
16242 cp_parser_error (parser, "expected template-name");
16243 return error_mark_node;
16247 /* If DECL is dependent, and refers to a function, then just return
16248 its name; we will look it up again during template instantiation. */
16249 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16251 tree scope = ovl_scope (decl);
16252 if (TYPE_P (scope) && dependent_type_p (scope))
16253 return identifier;
16256 return decl;
16259 /* Parse a template-argument-list.
16261 template-argument-list:
16262 template-argument ... [opt]
16263 template-argument-list , template-argument ... [opt]
16265 Returns a TREE_VEC containing the arguments. */
16267 static tree
16268 cp_parser_template_argument_list (cp_parser* parser)
16270 tree fixed_args[10];
16271 unsigned n_args = 0;
16272 unsigned alloced = 10;
16273 tree *arg_ary = fixed_args;
16274 tree vec;
16275 bool saved_in_template_argument_list_p;
16276 bool saved_ice_p;
16277 bool saved_non_ice_p;
16279 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16280 parser->in_template_argument_list_p = true;
16281 /* Even if the template-id appears in an integral
16282 constant-expression, the contents of the argument list do
16283 not. */
16284 saved_ice_p = parser->integral_constant_expression_p;
16285 parser->integral_constant_expression_p = false;
16286 saved_non_ice_p = parser->non_integral_constant_expression_p;
16287 parser->non_integral_constant_expression_p = false;
16289 /* Parse the arguments. */
16292 tree argument;
16294 if (n_args)
16295 /* Consume the comma. */
16296 cp_lexer_consume_token (parser->lexer);
16298 /* Parse the template-argument. */
16299 argument = cp_parser_template_argument (parser);
16301 /* If the next token is an ellipsis, we're expanding a template
16302 argument pack. */
16303 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16305 if (argument == error_mark_node)
16307 cp_token *token = cp_lexer_peek_token (parser->lexer);
16308 error_at (token->location,
16309 "expected parameter pack before %<...%>");
16311 /* Consume the `...' token. */
16312 cp_lexer_consume_token (parser->lexer);
16314 /* Make the argument into a TYPE_PACK_EXPANSION or
16315 EXPR_PACK_EXPANSION. */
16316 argument = make_pack_expansion (argument);
16319 if (n_args == alloced)
16321 alloced *= 2;
16323 if (arg_ary == fixed_args)
16325 arg_ary = XNEWVEC (tree, alloced);
16326 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16328 else
16329 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16331 arg_ary[n_args++] = argument;
16333 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16335 vec = make_tree_vec (n_args);
16337 while (n_args--)
16338 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16340 if (arg_ary != fixed_args)
16341 free (arg_ary);
16342 parser->non_integral_constant_expression_p = saved_non_ice_p;
16343 parser->integral_constant_expression_p = saved_ice_p;
16344 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16345 if (CHECKING_P)
16346 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16347 return vec;
16350 /* Parse a template-argument.
16352 template-argument:
16353 assignment-expression
16354 type-id
16355 id-expression
16357 The representation is that of an assignment-expression, type-id, or
16358 id-expression -- except that the qualified id-expression is
16359 evaluated, so that the value returned is either a DECL or an
16360 OVERLOAD.
16362 Although the standard says "assignment-expression", it forbids
16363 throw-expressions or assignments in the template argument.
16364 Therefore, we use "conditional-expression" instead. */
16366 static tree
16367 cp_parser_template_argument (cp_parser* parser)
16369 tree argument;
16370 bool template_p;
16371 bool address_p;
16372 bool maybe_type_id = false;
16373 cp_token *token = NULL, *argument_start_token = NULL;
16374 location_t loc = 0;
16375 cp_id_kind idk;
16377 /* There's really no way to know what we're looking at, so we just
16378 try each alternative in order.
16380 [temp.arg]
16382 In a template-argument, an ambiguity between a type-id and an
16383 expression is resolved to a type-id, regardless of the form of
16384 the corresponding template-parameter.
16386 Therefore, we try a type-id first. */
16387 cp_parser_parse_tentatively (parser);
16388 argument = cp_parser_template_type_arg (parser);
16389 /* If there was no error parsing the type-id but the next token is a
16390 '>>', our behavior depends on which dialect of C++ we're
16391 parsing. In C++98, we probably found a typo for '> >'. But there
16392 are type-id which are also valid expressions. For instance:
16394 struct X { int operator >> (int); };
16395 template <int V> struct Foo {};
16396 Foo<X () >> 5> r;
16398 Here 'X()' is a valid type-id of a function type, but the user just
16399 wanted to write the expression "X() >> 5". Thus, we remember that we
16400 found a valid type-id, but we still try to parse the argument as an
16401 expression to see what happens.
16403 In C++0x, the '>>' will be considered two separate '>'
16404 tokens. */
16405 if (!cp_parser_error_occurred (parser)
16406 && cxx_dialect == cxx98
16407 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16409 maybe_type_id = true;
16410 cp_parser_abort_tentative_parse (parser);
16412 else
16414 /* If the next token isn't a `,' or a `>', then this argument wasn't
16415 really finished. This means that the argument is not a valid
16416 type-id. */
16417 if (!cp_parser_next_token_ends_template_argument_p (parser))
16418 cp_parser_error (parser, "expected template-argument");
16419 /* If that worked, we're done. */
16420 if (cp_parser_parse_definitely (parser))
16421 return argument;
16423 /* We're still not sure what the argument will be. */
16424 cp_parser_parse_tentatively (parser);
16425 /* Try a template. */
16426 argument_start_token = cp_lexer_peek_token (parser->lexer);
16427 argument = cp_parser_id_expression (parser,
16428 /*template_keyword_p=*/false,
16429 /*check_dependency_p=*/true,
16430 &template_p,
16431 /*declarator_p=*/false,
16432 /*optional_p=*/false);
16433 /* If the next token isn't a `,' or a `>', then this argument wasn't
16434 really finished. */
16435 if (!cp_parser_next_token_ends_template_argument_p (parser))
16436 cp_parser_error (parser, "expected template-argument");
16437 if (!cp_parser_error_occurred (parser))
16439 /* Figure out what is being referred to. If the id-expression
16440 was for a class template specialization, then we will have a
16441 TYPE_DECL at this point. There is no need to do name lookup
16442 at this point in that case. */
16443 if (TREE_CODE (argument) != TYPE_DECL)
16444 argument = cp_parser_lookup_name (parser, argument,
16445 none_type,
16446 /*is_template=*/template_p,
16447 /*is_namespace=*/false,
16448 /*check_dependency=*/true,
16449 /*ambiguous_decls=*/NULL,
16450 argument_start_token->location);
16451 /* Handle a constrained-type-specifier for a non-type template
16452 parameter. */
16453 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16454 argument = decl;
16455 else if (TREE_CODE (argument) != TEMPLATE_DECL
16456 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16457 cp_parser_error (parser, "expected template-name");
16459 if (cp_parser_parse_definitely (parser))
16461 if (TREE_DEPRECATED (argument))
16462 warn_deprecated_use (argument, NULL_TREE);
16463 return argument;
16465 /* It must be a non-type argument. In C++17 any constant-expression is
16466 allowed. */
16467 if (cxx_dialect > cxx14)
16468 goto general_expr;
16470 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16472 -- an integral constant-expression of integral or enumeration
16473 type; or
16475 -- the name of a non-type template-parameter; or
16477 -- the name of an object or function with external linkage...
16479 -- the address of an object or function with external linkage...
16481 -- a pointer to member... */
16482 /* Look for a non-type template parameter. */
16483 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16485 cp_parser_parse_tentatively (parser);
16486 argument = cp_parser_primary_expression (parser,
16487 /*address_p=*/false,
16488 /*cast_p=*/false,
16489 /*template_arg_p=*/true,
16490 &idk);
16491 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16492 || !cp_parser_next_token_ends_template_argument_p (parser))
16493 cp_parser_simulate_error (parser);
16494 if (cp_parser_parse_definitely (parser))
16495 return argument;
16498 /* If the next token is "&", the argument must be the address of an
16499 object or function with external linkage. */
16500 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16501 if (address_p)
16503 loc = cp_lexer_peek_token (parser->lexer)->location;
16504 cp_lexer_consume_token (parser->lexer);
16506 /* See if we might have an id-expression. */
16507 token = cp_lexer_peek_token (parser->lexer);
16508 if (token->type == CPP_NAME
16509 || token->keyword == RID_OPERATOR
16510 || token->type == CPP_SCOPE
16511 || token->type == CPP_TEMPLATE_ID
16512 || token->type == CPP_NESTED_NAME_SPECIFIER)
16514 cp_parser_parse_tentatively (parser);
16515 argument = cp_parser_primary_expression (parser,
16516 address_p,
16517 /*cast_p=*/false,
16518 /*template_arg_p=*/true,
16519 &idk);
16520 if (cp_parser_error_occurred (parser)
16521 || !cp_parser_next_token_ends_template_argument_p (parser))
16522 cp_parser_abort_tentative_parse (parser);
16523 else
16525 tree probe;
16527 if (INDIRECT_REF_P (argument))
16529 /* Strip the dereference temporarily. */
16530 gcc_assert (REFERENCE_REF_P (argument));
16531 argument = TREE_OPERAND (argument, 0);
16534 /* If we're in a template, we represent a qualified-id referring
16535 to a static data member as a SCOPE_REF even if the scope isn't
16536 dependent so that we can check access control later. */
16537 probe = argument;
16538 if (TREE_CODE (probe) == SCOPE_REF)
16539 probe = TREE_OPERAND (probe, 1);
16540 if (VAR_P (probe))
16542 /* A variable without external linkage might still be a
16543 valid constant-expression, so no error is issued here
16544 if the external-linkage check fails. */
16545 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16546 cp_parser_simulate_error (parser);
16548 else if (is_overloaded_fn (argument))
16549 /* All overloaded functions are allowed; if the external
16550 linkage test does not pass, an error will be issued
16551 later. */
16553 else if (address_p
16554 && (TREE_CODE (argument) == OFFSET_REF
16555 || TREE_CODE (argument) == SCOPE_REF))
16556 /* A pointer-to-member. */
16558 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16560 else
16561 cp_parser_simulate_error (parser);
16563 if (cp_parser_parse_definitely (parser))
16565 if (address_p)
16566 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16567 tf_warning_or_error);
16568 else
16569 argument = convert_from_reference (argument);
16570 return argument;
16574 /* If the argument started with "&", there are no other valid
16575 alternatives at this point. */
16576 if (address_p)
16578 cp_parser_error (parser, "invalid non-type template argument");
16579 return error_mark_node;
16582 general_expr:
16583 /* If the argument wasn't successfully parsed as a type-id followed
16584 by '>>', the argument can only be a constant expression now.
16585 Otherwise, we try parsing the constant-expression tentatively,
16586 because the argument could really be a type-id. */
16587 if (maybe_type_id)
16588 cp_parser_parse_tentatively (parser);
16590 if (cxx_dialect <= cxx14)
16591 argument = cp_parser_constant_expression (parser);
16592 else
16594 /* With C++17 generalized non-type template arguments we need to handle
16595 lvalue constant expressions, too. */
16596 argument = cp_parser_assignment_expression (parser);
16597 require_potential_constant_expression (argument);
16600 if (!maybe_type_id)
16601 return argument;
16602 if (!cp_parser_next_token_ends_template_argument_p (parser))
16603 cp_parser_error (parser, "expected template-argument");
16604 if (cp_parser_parse_definitely (parser))
16605 return argument;
16606 /* We did our best to parse the argument as a non type-id, but that
16607 was the only alternative that matched (albeit with a '>' after
16608 it). We can assume it's just a typo from the user, and a
16609 diagnostic will then be issued. */
16610 return cp_parser_template_type_arg (parser);
16613 /* Parse an explicit-instantiation.
16615 explicit-instantiation:
16616 template declaration
16618 Although the standard says `declaration', what it really means is:
16620 explicit-instantiation:
16621 template decl-specifier-seq [opt] declarator [opt] ;
16623 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16624 supposed to be allowed. A defect report has been filed about this
16625 issue.
16627 GNU Extension:
16629 explicit-instantiation:
16630 storage-class-specifier template
16631 decl-specifier-seq [opt] declarator [opt] ;
16632 function-specifier template
16633 decl-specifier-seq [opt] declarator [opt] ; */
16635 static void
16636 cp_parser_explicit_instantiation (cp_parser* parser)
16638 int declares_class_or_enum;
16639 cp_decl_specifier_seq decl_specifiers;
16640 tree extension_specifier = NULL_TREE;
16642 timevar_push (TV_TEMPLATE_INST);
16644 /* Look for an (optional) storage-class-specifier or
16645 function-specifier. */
16646 if (cp_parser_allow_gnu_extensions_p (parser))
16648 extension_specifier
16649 = cp_parser_storage_class_specifier_opt (parser);
16650 if (!extension_specifier)
16651 extension_specifier
16652 = cp_parser_function_specifier_opt (parser,
16653 /*decl_specs=*/NULL);
16656 /* Look for the `template' keyword. */
16657 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16658 /* Let the front end know that we are processing an explicit
16659 instantiation. */
16660 begin_explicit_instantiation ();
16661 /* [temp.explicit] says that we are supposed to ignore access
16662 control while processing explicit instantiation directives. */
16663 push_deferring_access_checks (dk_no_check);
16664 /* Parse a decl-specifier-seq. */
16665 cp_parser_decl_specifier_seq (parser,
16666 CP_PARSER_FLAGS_OPTIONAL,
16667 &decl_specifiers,
16668 &declares_class_or_enum);
16669 /* If there was exactly one decl-specifier, and it declared a class,
16670 and there's no declarator, then we have an explicit type
16671 instantiation. */
16672 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16674 tree type;
16676 type = check_tag_decl (&decl_specifiers,
16677 /*explicit_type_instantiation_p=*/true);
16678 /* Turn access control back on for names used during
16679 template instantiation. */
16680 pop_deferring_access_checks ();
16681 if (type)
16682 do_type_instantiation (type, extension_specifier,
16683 /*complain=*/tf_error);
16685 else
16687 cp_declarator *declarator;
16688 tree decl;
16690 /* Parse the declarator. */
16691 declarator
16692 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16693 /*ctor_dtor_or_conv_p=*/NULL,
16694 /*parenthesized_p=*/NULL,
16695 /*member_p=*/false,
16696 /*friend_p=*/false);
16697 if (declares_class_or_enum & 2)
16698 cp_parser_check_for_definition_in_return_type (declarator,
16699 decl_specifiers.type,
16700 decl_specifiers.locations[ds_type_spec]);
16701 if (declarator != cp_error_declarator)
16703 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16704 permerror (decl_specifiers.locations[ds_inline],
16705 "explicit instantiation shall not use"
16706 " %<inline%> specifier");
16707 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16708 permerror (decl_specifiers.locations[ds_constexpr],
16709 "explicit instantiation shall not use"
16710 " %<constexpr%> specifier");
16712 decl = grokdeclarator (declarator, &decl_specifiers,
16713 NORMAL, 0, &decl_specifiers.attributes);
16714 /* Turn access control back on for names used during
16715 template instantiation. */
16716 pop_deferring_access_checks ();
16717 /* Do the explicit instantiation. */
16718 do_decl_instantiation (decl, extension_specifier);
16720 else
16722 pop_deferring_access_checks ();
16723 /* Skip the body of the explicit instantiation. */
16724 cp_parser_skip_to_end_of_statement (parser);
16727 /* We're done with the instantiation. */
16728 end_explicit_instantiation ();
16730 cp_parser_consume_semicolon_at_end_of_statement (parser);
16732 timevar_pop (TV_TEMPLATE_INST);
16735 /* Parse an explicit-specialization.
16737 explicit-specialization:
16738 template < > declaration
16740 Although the standard says `declaration', what it really means is:
16742 explicit-specialization:
16743 template <> decl-specifier [opt] init-declarator [opt] ;
16744 template <> function-definition
16745 template <> explicit-specialization
16746 template <> template-declaration */
16748 static void
16749 cp_parser_explicit_specialization (cp_parser* parser)
16751 bool need_lang_pop;
16752 cp_token *token = cp_lexer_peek_token (parser->lexer);
16754 /* Look for the `template' keyword. */
16755 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16756 /* Look for the `<'. */
16757 cp_parser_require (parser, CPP_LESS, RT_LESS);
16758 /* Look for the `>'. */
16759 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16760 /* We have processed another parameter list. */
16761 ++parser->num_template_parameter_lists;
16762 /* [temp]
16764 A template ... explicit specialization ... shall not have C
16765 linkage. */
16766 if (current_lang_name == lang_name_c)
16768 error_at (token->location, "template specialization with C linkage");
16769 maybe_show_extern_c_location ();
16770 /* Give it C++ linkage to avoid confusing other parts of the
16771 front end. */
16772 push_lang_context (lang_name_cplusplus);
16773 need_lang_pop = true;
16775 else
16776 need_lang_pop = false;
16777 /* Let the front end know that we are beginning a specialization. */
16778 if (!begin_specialization ())
16780 end_specialization ();
16781 return;
16784 /* If the next keyword is `template', we need to figure out whether
16785 or not we're looking a template-declaration. */
16786 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16788 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16789 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16790 cp_parser_template_declaration_after_export (parser,
16791 /*member_p=*/false);
16792 else
16793 cp_parser_explicit_specialization (parser);
16795 else
16796 /* Parse the dependent declaration. */
16797 cp_parser_single_declaration (parser,
16798 /*checks=*/NULL,
16799 /*member_p=*/false,
16800 /*explicit_specialization_p=*/true,
16801 /*friend_p=*/NULL);
16802 /* We're done with the specialization. */
16803 end_specialization ();
16804 /* For the erroneous case of a template with C linkage, we pushed an
16805 implicit C++ linkage scope; exit that scope now. */
16806 if (need_lang_pop)
16807 pop_lang_context ();
16808 /* We're done with this parameter list. */
16809 --parser->num_template_parameter_lists;
16812 /* Parse a type-specifier.
16814 type-specifier:
16815 simple-type-specifier
16816 class-specifier
16817 enum-specifier
16818 elaborated-type-specifier
16819 cv-qualifier
16821 GNU Extension:
16823 type-specifier:
16824 __complex__
16826 Returns a representation of the type-specifier. For a
16827 class-specifier, enum-specifier, or elaborated-type-specifier, a
16828 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16830 The parser flags FLAGS is used to control type-specifier parsing.
16832 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16833 in a decl-specifier-seq.
16835 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16836 class-specifier, enum-specifier, or elaborated-type-specifier, then
16837 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16838 if a type is declared; 2 if it is defined. Otherwise, it is set to
16839 zero.
16841 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16842 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16843 is set to FALSE. */
16845 static tree
16846 cp_parser_type_specifier (cp_parser* parser,
16847 cp_parser_flags flags,
16848 cp_decl_specifier_seq *decl_specs,
16849 bool is_declaration,
16850 int* declares_class_or_enum,
16851 bool* is_cv_qualifier)
16853 tree type_spec = NULL_TREE;
16854 cp_token *token;
16855 enum rid keyword;
16856 cp_decl_spec ds = ds_last;
16858 /* Assume this type-specifier does not declare a new type. */
16859 if (declares_class_or_enum)
16860 *declares_class_or_enum = 0;
16861 /* And that it does not specify a cv-qualifier. */
16862 if (is_cv_qualifier)
16863 *is_cv_qualifier = false;
16864 /* Peek at the next token. */
16865 token = cp_lexer_peek_token (parser->lexer);
16867 /* If we're looking at a keyword, we can use that to guide the
16868 production we choose. */
16869 keyword = token->keyword;
16870 switch (keyword)
16872 case RID_ENUM:
16873 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16874 goto elaborated_type_specifier;
16876 /* Look for the enum-specifier. */
16877 type_spec = cp_parser_enum_specifier (parser);
16878 /* If that worked, we're done. */
16879 if (type_spec)
16881 if (declares_class_or_enum)
16882 *declares_class_or_enum = 2;
16883 if (decl_specs)
16884 cp_parser_set_decl_spec_type (decl_specs,
16885 type_spec,
16886 token,
16887 /*type_definition_p=*/true);
16888 return type_spec;
16890 else
16891 goto elaborated_type_specifier;
16893 /* Any of these indicate either a class-specifier, or an
16894 elaborated-type-specifier. */
16895 case RID_CLASS:
16896 case RID_STRUCT:
16897 case RID_UNION:
16898 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16899 goto elaborated_type_specifier;
16901 /* Parse tentatively so that we can back up if we don't find a
16902 class-specifier. */
16903 cp_parser_parse_tentatively (parser);
16904 /* Look for the class-specifier. */
16905 type_spec = cp_parser_class_specifier (parser);
16906 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16907 /* If that worked, we're done. */
16908 if (cp_parser_parse_definitely (parser))
16910 if (declares_class_or_enum)
16911 *declares_class_or_enum = 2;
16912 if (decl_specs)
16913 cp_parser_set_decl_spec_type (decl_specs,
16914 type_spec,
16915 token,
16916 /*type_definition_p=*/true);
16917 return type_spec;
16920 /* Fall through. */
16921 elaborated_type_specifier:
16922 /* We're declaring (not defining) a class or enum. */
16923 if (declares_class_or_enum)
16924 *declares_class_or_enum = 1;
16926 /* Fall through. */
16927 case RID_TYPENAME:
16928 /* Look for an elaborated-type-specifier. */
16929 type_spec
16930 = (cp_parser_elaborated_type_specifier
16931 (parser,
16932 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16933 is_declaration));
16934 if (decl_specs)
16935 cp_parser_set_decl_spec_type (decl_specs,
16936 type_spec,
16937 token,
16938 /*type_definition_p=*/false);
16939 return type_spec;
16941 case RID_CONST:
16942 ds = ds_const;
16943 if (is_cv_qualifier)
16944 *is_cv_qualifier = true;
16945 break;
16947 case RID_VOLATILE:
16948 ds = ds_volatile;
16949 if (is_cv_qualifier)
16950 *is_cv_qualifier = true;
16951 break;
16953 case RID_RESTRICT:
16954 ds = ds_restrict;
16955 if (is_cv_qualifier)
16956 *is_cv_qualifier = true;
16957 break;
16959 case RID_COMPLEX:
16960 /* The `__complex__' keyword is a GNU extension. */
16961 ds = ds_complex;
16962 break;
16964 default:
16965 break;
16968 /* Handle simple keywords. */
16969 if (ds != ds_last)
16971 if (decl_specs)
16973 set_and_check_decl_spec_loc (decl_specs, ds, token);
16974 decl_specs->any_specifiers_p = true;
16976 return cp_lexer_consume_token (parser->lexer)->u.value;
16979 /* If we do not already have a type-specifier, assume we are looking
16980 at a simple-type-specifier. */
16981 type_spec = cp_parser_simple_type_specifier (parser,
16982 decl_specs,
16983 flags);
16985 /* If we didn't find a type-specifier, and a type-specifier was not
16986 optional in this context, issue an error message. */
16987 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16989 cp_parser_error (parser, "expected type specifier");
16990 return error_mark_node;
16993 return type_spec;
16996 /* Parse a simple-type-specifier.
16998 simple-type-specifier:
16999 :: [opt] nested-name-specifier [opt] type-name
17000 :: [opt] nested-name-specifier template template-id
17001 char
17002 wchar_t
17003 bool
17004 short
17006 long
17007 signed
17008 unsigned
17009 float
17010 double
17011 void
17013 C++11 Extension:
17015 simple-type-specifier:
17016 auto
17017 decltype ( expression )
17018 char16_t
17019 char32_t
17020 __underlying_type ( type-id )
17022 C++17 extension:
17024 nested-name-specifier(opt) template-name
17026 GNU Extension:
17028 simple-type-specifier:
17029 __int128
17030 __typeof__ unary-expression
17031 __typeof__ ( type-id )
17032 __typeof__ ( type-id ) { initializer-list , [opt] }
17034 Concepts Extension:
17036 simple-type-specifier:
17037 constrained-type-specifier
17039 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17040 appropriately updated. */
17042 static tree
17043 cp_parser_simple_type_specifier (cp_parser* parser,
17044 cp_decl_specifier_seq *decl_specs,
17045 cp_parser_flags flags)
17047 tree type = NULL_TREE;
17048 cp_token *token;
17049 int idx;
17051 /* Peek at the next token. */
17052 token = cp_lexer_peek_token (parser->lexer);
17054 /* If we're looking at a keyword, things are easy. */
17055 switch (token->keyword)
17057 case RID_CHAR:
17058 if (decl_specs)
17059 decl_specs->explicit_char_p = true;
17060 type = char_type_node;
17061 break;
17062 case RID_CHAR16:
17063 type = char16_type_node;
17064 break;
17065 case RID_CHAR32:
17066 type = char32_type_node;
17067 break;
17068 case RID_WCHAR:
17069 type = wchar_type_node;
17070 break;
17071 case RID_BOOL:
17072 type = boolean_type_node;
17073 break;
17074 case RID_SHORT:
17075 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17076 type = short_integer_type_node;
17077 break;
17078 case RID_INT:
17079 if (decl_specs)
17080 decl_specs->explicit_int_p = true;
17081 type = integer_type_node;
17082 break;
17083 case RID_INT_N_0:
17084 case RID_INT_N_1:
17085 case RID_INT_N_2:
17086 case RID_INT_N_3:
17087 idx = token->keyword - RID_INT_N_0;
17088 if (! int_n_enabled_p [idx])
17089 break;
17090 if (decl_specs)
17092 decl_specs->explicit_intN_p = true;
17093 decl_specs->int_n_idx = idx;
17095 type = int_n_trees [idx].signed_type;
17096 break;
17097 case RID_LONG:
17098 if (decl_specs)
17099 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17100 type = long_integer_type_node;
17101 break;
17102 case RID_SIGNED:
17103 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17104 type = integer_type_node;
17105 break;
17106 case RID_UNSIGNED:
17107 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17108 type = unsigned_type_node;
17109 break;
17110 case RID_FLOAT:
17111 type = float_type_node;
17112 break;
17113 case RID_DOUBLE:
17114 type = double_type_node;
17115 break;
17116 case RID_VOID:
17117 type = void_type_node;
17118 break;
17120 case RID_AUTO:
17121 maybe_warn_cpp0x (CPP0X_AUTO);
17122 if (parser->auto_is_implicit_function_template_parm_p)
17124 /* The 'auto' might be the placeholder return type for a function decl
17125 with trailing return type. */
17126 bool have_trailing_return_fn_decl = false;
17128 cp_parser_parse_tentatively (parser);
17129 cp_lexer_consume_token (parser->lexer);
17130 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17131 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17132 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17133 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17137 cp_lexer_consume_token (parser->lexer);
17138 cp_parser_skip_to_closing_parenthesis (parser,
17139 /*recovering*/false,
17140 /*or_comma*/false,
17141 /*consume_paren*/true);
17142 continue;
17145 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17147 have_trailing_return_fn_decl = true;
17148 break;
17151 cp_lexer_consume_token (parser->lexer);
17153 cp_parser_abort_tentative_parse (parser);
17155 if (have_trailing_return_fn_decl)
17157 type = make_auto ();
17158 break;
17161 if (cxx_dialect >= cxx14)
17163 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17164 type = TREE_TYPE (type);
17166 else
17167 type = error_mark_node;
17169 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17171 if (cxx_dialect < cxx14)
17172 error_at (token->location,
17173 "use of %<auto%> in lambda parameter declaration "
17174 "only available with "
17175 "-std=c++14 or -std=gnu++14");
17177 else if (cxx_dialect < cxx14)
17178 error_at (token->location,
17179 "use of %<auto%> in parameter declaration "
17180 "only available with "
17181 "-std=c++14 or -std=gnu++14");
17182 else if (!flag_concepts)
17183 pedwarn (token->location, 0,
17184 "use of %<auto%> in parameter declaration "
17185 "only available with -fconcepts");
17187 else
17188 type = make_auto ();
17189 break;
17191 case RID_DECLTYPE:
17192 /* Since DR 743, decltype can either be a simple-type-specifier by
17193 itself or begin a nested-name-specifier. Parsing it will replace
17194 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17195 handling below decide what to do. */
17196 cp_parser_decltype (parser);
17197 cp_lexer_set_token_position (parser->lexer, token);
17198 break;
17200 case RID_TYPEOF:
17201 /* Consume the `typeof' token. */
17202 cp_lexer_consume_token (parser->lexer);
17203 /* Parse the operand to `typeof'. */
17204 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17205 /* If it is not already a TYPE, take its type. */
17206 if (!TYPE_P (type))
17207 type = finish_typeof (type);
17209 if (decl_specs)
17210 cp_parser_set_decl_spec_type (decl_specs, type,
17211 token,
17212 /*type_definition_p=*/false);
17214 return type;
17216 case RID_UNDERLYING_TYPE:
17217 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17218 if (decl_specs)
17219 cp_parser_set_decl_spec_type (decl_specs, type,
17220 token,
17221 /*type_definition_p=*/false);
17223 return type;
17225 case RID_BASES:
17226 case RID_DIRECT_BASES:
17227 type = cp_parser_trait_expr (parser, token->keyword);
17228 if (decl_specs)
17229 cp_parser_set_decl_spec_type (decl_specs, type,
17230 token,
17231 /*type_definition_p=*/false);
17232 return type;
17233 default:
17234 break;
17237 /* If token is an already-parsed decltype not followed by ::,
17238 it's a simple-type-specifier. */
17239 if (token->type == CPP_DECLTYPE
17240 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17242 type = saved_checks_value (token->u.tree_check_value);
17243 if (decl_specs)
17245 cp_parser_set_decl_spec_type (decl_specs, type,
17246 token,
17247 /*type_definition_p=*/false);
17248 /* Remember that we are handling a decltype in order to
17249 implement the resolution of DR 1510 when the argument
17250 isn't instantiation dependent. */
17251 decl_specs->decltype_p = true;
17253 cp_lexer_consume_token (parser->lexer);
17254 return type;
17257 /* If the type-specifier was for a built-in type, we're done. */
17258 if (type)
17260 /* Record the type. */
17261 if (decl_specs
17262 && (token->keyword != RID_SIGNED
17263 && token->keyword != RID_UNSIGNED
17264 && token->keyword != RID_SHORT
17265 && token->keyword != RID_LONG))
17266 cp_parser_set_decl_spec_type (decl_specs,
17267 type,
17268 token,
17269 /*type_definition_p=*/false);
17270 if (decl_specs)
17271 decl_specs->any_specifiers_p = true;
17273 /* Consume the token. */
17274 cp_lexer_consume_token (parser->lexer);
17276 if (type == error_mark_node)
17277 return error_mark_node;
17279 /* There is no valid C++ program where a non-template type is
17280 followed by a "<". That usually indicates that the user thought
17281 that the type was a template. */
17282 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17283 token->location);
17285 return TYPE_NAME (type);
17288 /* The type-specifier must be a user-defined type. */
17289 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17291 bool qualified_p;
17292 bool global_p;
17294 /* Don't gobble tokens or issue error messages if this is an
17295 optional type-specifier. */
17296 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17297 cp_parser_parse_tentatively (parser);
17299 token = cp_lexer_peek_token (parser->lexer);
17301 /* Look for the optional `::' operator. */
17302 global_p
17303 = (cp_parser_global_scope_opt (parser,
17304 /*current_scope_valid_p=*/false)
17305 != NULL_TREE);
17306 /* Look for the nested-name specifier. */
17307 qualified_p
17308 = (cp_parser_nested_name_specifier_opt (parser,
17309 /*typename_keyword_p=*/false,
17310 /*check_dependency_p=*/true,
17311 /*type_p=*/false,
17312 /*is_declaration=*/false)
17313 != NULL_TREE);
17314 /* If we have seen a nested-name-specifier, and the next token
17315 is `template', then we are using the template-id production. */
17316 if (parser->scope
17317 && cp_parser_optional_template_keyword (parser))
17319 /* Look for the template-id. */
17320 type = cp_parser_template_id (parser,
17321 /*template_keyword_p=*/true,
17322 /*check_dependency_p=*/true,
17323 none_type,
17324 /*is_declaration=*/false);
17325 /* If the template-id did not name a type, we are out of
17326 luck. */
17327 if (TREE_CODE (type) != TYPE_DECL)
17329 cp_parser_error (parser, "expected template-id for type");
17330 type = NULL_TREE;
17333 /* Otherwise, look for a type-name. */
17334 else
17335 type = cp_parser_type_name (parser);
17336 /* Keep track of all name-lookups performed in class scopes. */
17337 if (type
17338 && !global_p
17339 && !qualified_p
17340 && TREE_CODE (type) == TYPE_DECL
17341 && identifier_p (DECL_NAME (type)))
17342 maybe_note_name_used_in_class (DECL_NAME (type), type);
17343 /* If it didn't work out, we don't have a TYPE. */
17344 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17345 && !cp_parser_parse_definitely (parser))
17346 type = NULL_TREE;
17347 if (!type && cxx_dialect >= cxx17)
17349 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17350 cp_parser_parse_tentatively (parser);
17352 cp_parser_global_scope_opt (parser,
17353 /*current_scope_valid_p=*/false);
17354 cp_parser_nested_name_specifier_opt (parser,
17355 /*typename_keyword_p=*/false,
17356 /*check_dependency_p=*/true,
17357 /*type_p=*/false,
17358 /*is_declaration=*/false);
17359 tree name = cp_parser_identifier (parser);
17360 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17361 && parser->scope != error_mark_node)
17363 tree tmpl = cp_parser_lookup_name (parser, name,
17364 none_type,
17365 /*is_template=*/false,
17366 /*is_namespace=*/false,
17367 /*check_dependency=*/true,
17368 /*ambiguous_decls=*/NULL,
17369 token->location);
17370 if (tmpl && tmpl != error_mark_node
17371 && (DECL_CLASS_TEMPLATE_P (tmpl)
17372 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17373 type = make_template_placeholder (tmpl);
17374 else
17376 type = error_mark_node;
17377 if (!cp_parser_simulate_error (parser))
17378 cp_parser_name_lookup_error (parser, name, tmpl,
17379 NLE_TYPE, token->location);
17382 else
17383 type = error_mark_node;
17385 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17386 && !cp_parser_parse_definitely (parser))
17387 type = NULL_TREE;
17389 if (type && decl_specs)
17390 cp_parser_set_decl_spec_type (decl_specs, type,
17391 token,
17392 /*type_definition_p=*/false);
17395 /* If we didn't get a type-name, issue an error message. */
17396 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17398 cp_parser_error (parser, "expected type-name");
17399 return error_mark_node;
17402 if (type && type != error_mark_node)
17404 /* See if TYPE is an Objective-C type, and if so, parse and
17405 accept any protocol references following it. Do this before
17406 the cp_parser_check_for_invalid_template_id() call, because
17407 Objective-C types can be followed by '<...>' which would
17408 enclose protocol names rather than template arguments, and so
17409 everything is fine. */
17410 if (c_dialect_objc () && !parser->scope
17411 && (objc_is_id (type) || objc_is_class_name (type)))
17413 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17414 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17416 /* Clobber the "unqualified" type previously entered into
17417 DECL_SPECS with the new, improved protocol-qualified version. */
17418 if (decl_specs)
17419 decl_specs->type = qual_type;
17421 return qual_type;
17424 /* There is no valid C++ program where a non-template type is
17425 followed by a "<". That usually indicates that the user
17426 thought that the type was a template. */
17427 cp_parser_check_for_invalid_template_id (parser, type,
17428 none_type,
17429 token->location);
17432 return type;
17435 /* Parse a type-name.
17437 type-name:
17438 class-name
17439 enum-name
17440 typedef-name
17441 simple-template-id [in c++0x]
17443 enum-name:
17444 identifier
17446 typedef-name:
17447 identifier
17449 Concepts:
17451 type-name:
17452 concept-name
17453 partial-concept-id
17455 concept-name:
17456 identifier
17458 Returns a TYPE_DECL for the type. */
17460 static tree
17461 cp_parser_type_name (cp_parser* parser)
17463 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17466 /* See above. */
17467 static tree
17468 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17470 tree type_decl;
17472 /* We can't know yet whether it is a class-name or not. */
17473 cp_parser_parse_tentatively (parser);
17474 /* Try a class-name. */
17475 type_decl = cp_parser_class_name (parser,
17476 typename_keyword_p,
17477 /*template_keyword_p=*/false,
17478 none_type,
17479 /*check_dependency_p=*/true,
17480 /*class_head_p=*/false,
17481 /*is_declaration=*/false);
17482 /* If it's not a class-name, keep looking. */
17483 if (!cp_parser_parse_definitely (parser))
17485 if (cxx_dialect < cxx11)
17486 /* It must be a typedef-name or an enum-name. */
17487 return cp_parser_nonclass_name (parser);
17489 cp_parser_parse_tentatively (parser);
17490 /* It is either a simple-template-id representing an
17491 instantiation of an alias template... */
17492 type_decl = cp_parser_template_id (parser,
17493 /*template_keyword_p=*/false,
17494 /*check_dependency_p=*/true,
17495 none_type,
17496 /*is_declaration=*/false);
17497 /* Note that this must be an instantiation of an alias template
17498 because [temp.names]/6 says:
17500 A template-id that names an alias template specialization
17501 is a type-name.
17503 Whereas [temp.names]/7 says:
17505 A simple-template-id that names a class template
17506 specialization is a class-name.
17508 With concepts, this could also be a partial-concept-id that
17509 declares a non-type template parameter. */
17510 if (type_decl != NULL_TREE
17511 && TREE_CODE (type_decl) == TYPE_DECL
17512 && TYPE_DECL_ALIAS_P (type_decl))
17513 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17514 else if (is_constrained_parameter (type_decl))
17515 /* Don't do anything. */ ;
17516 else
17517 cp_parser_simulate_error (parser);
17519 if (!cp_parser_parse_definitely (parser))
17520 /* ... Or a typedef-name or an enum-name. */
17521 return cp_parser_nonclass_name (parser);
17524 return type_decl;
17527 /* Check if DECL and ARGS can form a constrained-type-specifier.
17528 If ARGS is non-null, we try to form a concept check of the
17529 form DECL<?, ARGS> where ? is a wildcard that matches any
17530 kind of template argument. If ARGS is NULL, then we try to
17531 form a concept check of the form DECL<?>. */
17533 static tree
17534 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17535 tree decl, tree args)
17537 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17539 /* If we a constrained-type-specifier cannot be deduced. */
17540 if (parser->prevent_constrained_type_specifiers)
17541 return NULL_TREE;
17543 /* A constrained type specifier can only be found in an
17544 overload set or as a reference to a template declaration.
17546 FIXME: This might be masking a bug. It's possible that
17547 that the deduction below is causing template specializations
17548 to be formed with the wildcard as an argument. */
17549 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17550 return NULL_TREE;
17552 /* Try to build a call expression that evaluates the
17553 concept. This can fail if the overload set refers
17554 only to non-templates. */
17555 tree placeholder = build_nt (WILDCARD_DECL);
17556 tree check = build_concept_check (decl, placeholder, args);
17557 if (check == error_mark_node)
17558 return NULL_TREE;
17560 /* Deduce the checked constraint and the prototype parameter.
17562 FIXME: In certain cases, failure to deduce should be a
17563 diagnosable error. */
17564 tree conc;
17565 tree proto;
17566 if (!deduce_constrained_parameter (check, conc, proto))
17567 return NULL_TREE;
17569 /* In template parameter scope, this results in a constrained
17570 parameter. Return a descriptor of that parm. */
17571 if (processing_template_parmlist)
17572 return build_constrained_parameter (conc, proto, args);
17574 /* In a parameter-declaration-clause, constrained-type
17575 specifiers result in invented template parameters. */
17576 if (parser->auto_is_implicit_function_template_parm_p)
17578 tree x = build_constrained_parameter (conc, proto, args);
17579 return synthesize_implicit_template_parm (parser, x);
17581 else
17583 /* Otherwise, we're in a context where the constrained
17584 type name is deduced and the constraint applies
17585 after deduction. */
17586 return make_constrained_auto (conc, args);
17589 return NULL_TREE;
17592 /* If DECL refers to a concept, return a TYPE_DECL representing
17593 the result of using the constrained type specifier in the
17594 current context. DECL refers to a concept if
17596 - it is an overload set containing a function concept taking a single
17597 type argument, or
17599 - it is a variable concept taking a single type argument. */
17601 static tree
17602 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17604 if (flag_concepts
17605 && (TREE_CODE (decl) == OVERLOAD
17606 || BASELINK_P (decl)
17607 || variable_concept_p (decl)))
17608 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17609 else
17610 return NULL_TREE;
17613 /* Check if DECL and ARGS form a partial-concept-id. If so,
17614 assign ID to the resulting constrained placeholder.
17616 Returns true if the partial-concept-id designates a placeholder
17617 and false otherwise. Note that *id is set to NULL_TREE in
17618 this case. */
17620 static tree
17621 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17623 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17626 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17627 or a concept-name.
17629 enum-name:
17630 identifier
17632 typedef-name:
17633 identifier
17635 concept-name:
17636 identifier
17638 Returns a TYPE_DECL for the type. */
17640 static tree
17641 cp_parser_nonclass_name (cp_parser* parser)
17643 tree type_decl;
17644 tree identifier;
17646 cp_token *token = cp_lexer_peek_token (parser->lexer);
17647 identifier = cp_parser_identifier (parser);
17648 if (identifier == error_mark_node)
17649 return error_mark_node;
17651 /* Look up the type-name. */
17652 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17654 type_decl = strip_using_decl (type_decl);
17656 /* If we found an overload set, then it may refer to a concept-name. */
17657 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17658 type_decl = decl;
17660 if (TREE_CODE (type_decl) != TYPE_DECL
17661 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17663 /* See if this is an Objective-C type. */
17664 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17665 tree type = objc_get_protocol_qualified_type (identifier, protos);
17666 if (type)
17667 type_decl = TYPE_NAME (type);
17670 /* Issue an error if we did not find a type-name. */
17671 if (TREE_CODE (type_decl) != TYPE_DECL
17672 /* In Objective-C, we have the complication that class names are
17673 normally type names and start declarations (eg, the
17674 "NSObject" in "NSObject *object;"), but can be used in an
17675 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17676 is an expression. So, a classname followed by a dot is not a
17677 valid type-name. */
17678 || (objc_is_class_name (TREE_TYPE (type_decl))
17679 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17681 if (!cp_parser_simulate_error (parser))
17682 cp_parser_name_lookup_error (parser, identifier, type_decl,
17683 NLE_TYPE, token->location);
17684 return error_mark_node;
17686 /* Remember that the name was used in the definition of the
17687 current class so that we can check later to see if the
17688 meaning would have been different after the class was
17689 entirely defined. */
17690 else if (type_decl != error_mark_node
17691 && !parser->scope)
17692 maybe_note_name_used_in_class (identifier, type_decl);
17694 return type_decl;
17697 /* Parse an elaborated-type-specifier. Note that the grammar given
17698 here incorporates the resolution to DR68.
17700 elaborated-type-specifier:
17701 class-key :: [opt] nested-name-specifier [opt] identifier
17702 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17703 enum-key :: [opt] nested-name-specifier [opt] identifier
17704 typename :: [opt] nested-name-specifier identifier
17705 typename :: [opt] nested-name-specifier template [opt]
17706 template-id
17708 GNU extension:
17710 elaborated-type-specifier:
17711 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17712 class-key attributes :: [opt] nested-name-specifier [opt]
17713 template [opt] template-id
17714 enum attributes :: [opt] nested-name-specifier [opt] identifier
17716 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17717 declared `friend'. If IS_DECLARATION is TRUE, then this
17718 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17719 something is being declared.
17721 Returns the TYPE specified. */
17723 static tree
17724 cp_parser_elaborated_type_specifier (cp_parser* parser,
17725 bool is_friend,
17726 bool is_declaration)
17728 enum tag_types tag_type;
17729 tree identifier;
17730 tree type = NULL_TREE;
17731 tree attributes = NULL_TREE;
17732 tree globalscope;
17733 cp_token *token = NULL;
17735 /* See if we're looking at the `enum' keyword. */
17736 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17738 /* Consume the `enum' token. */
17739 cp_lexer_consume_token (parser->lexer);
17740 /* Remember that it's an enumeration type. */
17741 tag_type = enum_type;
17742 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17743 enums) is used here. */
17744 cp_token *token = cp_lexer_peek_token (parser->lexer);
17745 if (cp_parser_is_keyword (token, RID_CLASS)
17746 || cp_parser_is_keyword (token, RID_STRUCT))
17748 gcc_rich_location richloc (token->location);
17749 richloc.add_range (input_location, false);
17750 richloc.add_fixit_remove ();
17751 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17752 "a scoped enum must not use the %qD keyword",
17753 token->u.value);
17754 /* Consume the `struct' or `class' and parse it anyway. */
17755 cp_lexer_consume_token (parser->lexer);
17757 /* Parse the attributes. */
17758 attributes = cp_parser_attributes_opt (parser);
17760 /* Or, it might be `typename'. */
17761 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17762 RID_TYPENAME))
17764 /* Consume the `typename' token. */
17765 cp_lexer_consume_token (parser->lexer);
17766 /* Remember that it's a `typename' type. */
17767 tag_type = typename_type;
17769 /* Otherwise it must be a class-key. */
17770 else
17772 tag_type = cp_parser_class_key (parser);
17773 if (tag_type == none_type)
17774 return error_mark_node;
17775 /* Parse the attributes. */
17776 attributes = cp_parser_attributes_opt (parser);
17779 /* Look for the `::' operator. */
17780 globalscope = cp_parser_global_scope_opt (parser,
17781 /*current_scope_valid_p=*/false);
17782 /* Look for the nested-name-specifier. */
17783 tree nested_name_specifier;
17784 if (tag_type == typename_type && !globalscope)
17786 nested_name_specifier
17787 = cp_parser_nested_name_specifier (parser,
17788 /*typename_keyword_p=*/true,
17789 /*check_dependency_p=*/true,
17790 /*type_p=*/true,
17791 is_declaration);
17792 if (!nested_name_specifier)
17793 return error_mark_node;
17795 else
17796 /* Even though `typename' is not present, the proposed resolution
17797 to Core Issue 180 says that in `class A<T>::B', `B' should be
17798 considered a type-name, even if `A<T>' is dependent. */
17799 nested_name_specifier
17800 = cp_parser_nested_name_specifier_opt (parser,
17801 /*typename_keyword_p=*/true,
17802 /*check_dependency_p=*/true,
17803 /*type_p=*/true,
17804 is_declaration);
17805 /* For everything but enumeration types, consider a template-id.
17806 For an enumeration type, consider only a plain identifier. */
17807 if (tag_type != enum_type)
17809 bool template_p = false;
17810 tree decl;
17812 /* Allow the `template' keyword. */
17813 template_p = cp_parser_optional_template_keyword (parser);
17814 /* If we didn't see `template', we don't know if there's a
17815 template-id or not. */
17816 if (!template_p)
17817 cp_parser_parse_tentatively (parser);
17818 /* Parse the template-id. */
17819 token = cp_lexer_peek_token (parser->lexer);
17820 decl = cp_parser_template_id (parser, template_p,
17821 /*check_dependency_p=*/true,
17822 tag_type,
17823 is_declaration);
17824 /* If we didn't find a template-id, look for an ordinary
17825 identifier. */
17826 if (!template_p && !cp_parser_parse_definitely (parser))
17828 /* We can get here when cp_parser_template_id, called by
17829 cp_parser_class_name with tag_type == none_type, succeeds
17830 and caches a BASELINK. Then, when called again here,
17831 instead of failing and returning an error_mark_node
17832 returns it (see template/typename17.C in C++11).
17833 ??? Could we diagnose this earlier? */
17834 else if (tag_type == typename_type && BASELINK_P (decl))
17836 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17837 type = error_mark_node;
17839 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17840 in effect, then we must assume that, upon instantiation, the
17841 template will correspond to a class. */
17842 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17843 && tag_type == typename_type)
17844 type = make_typename_type (parser->scope, decl,
17845 typename_type,
17846 /*complain=*/tf_error);
17847 /* If the `typename' keyword is in effect and DECL is not a type
17848 decl, then type is non existent. */
17849 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17851 else if (TREE_CODE (decl) == TYPE_DECL)
17853 type = check_elaborated_type_specifier (tag_type, decl,
17854 /*allow_template_p=*/true);
17856 /* If the next token is a semicolon, this must be a specialization,
17857 instantiation, or friend declaration. Check the scope while we
17858 still know whether or not we had a nested-name-specifier. */
17859 if (type != error_mark_node
17860 && !nested_name_specifier && !is_friend
17861 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17862 check_unqualified_spec_or_inst (type, token->location);
17864 else if (decl == error_mark_node)
17865 type = error_mark_node;
17868 if (!type)
17870 token = cp_lexer_peek_token (parser->lexer);
17871 identifier = cp_parser_identifier (parser);
17873 if (identifier == error_mark_node)
17875 parser->scope = NULL_TREE;
17876 return error_mark_node;
17879 /* For a `typename', we needn't call xref_tag. */
17880 if (tag_type == typename_type
17881 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17882 return cp_parser_make_typename_type (parser, identifier,
17883 token->location);
17885 /* Template parameter lists apply only if we are not within a
17886 function parameter list. */
17887 bool template_parm_lists_apply
17888 = parser->num_template_parameter_lists;
17889 if (template_parm_lists_apply)
17890 for (cp_binding_level *s = current_binding_level;
17891 s && s->kind != sk_template_parms;
17892 s = s->level_chain)
17893 if (s->kind == sk_function_parms)
17894 template_parm_lists_apply = false;
17896 /* Look up a qualified name in the usual way. */
17897 if (parser->scope)
17899 tree decl;
17900 tree ambiguous_decls;
17902 decl = cp_parser_lookup_name (parser, identifier,
17903 tag_type,
17904 /*is_template=*/false,
17905 /*is_namespace=*/false,
17906 /*check_dependency=*/true,
17907 &ambiguous_decls,
17908 token->location);
17910 /* If the lookup was ambiguous, an error will already have been
17911 issued. */
17912 if (ambiguous_decls)
17913 return error_mark_node;
17915 /* If we are parsing friend declaration, DECL may be a
17916 TEMPLATE_DECL tree node here. However, we need to check
17917 whether this TEMPLATE_DECL results in valid code. Consider
17918 the following example:
17920 namespace N {
17921 template <class T> class C {};
17923 class X {
17924 template <class T> friend class N::C; // #1, valid code
17926 template <class T> class Y {
17927 friend class N::C; // #2, invalid code
17930 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17931 name lookup of `N::C'. We see that friend declaration must
17932 be template for the code to be valid. Note that
17933 processing_template_decl does not work here since it is
17934 always 1 for the above two cases. */
17936 decl = (cp_parser_maybe_treat_template_as_class
17937 (decl, /*tag_name_p=*/is_friend
17938 && template_parm_lists_apply));
17940 if (TREE_CODE (decl) != TYPE_DECL)
17942 cp_parser_diagnose_invalid_type_name (parser,
17943 identifier,
17944 token->location);
17945 return error_mark_node;
17948 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17950 bool allow_template = (template_parm_lists_apply
17951 || DECL_SELF_REFERENCE_P (decl));
17952 type = check_elaborated_type_specifier (tag_type, decl,
17953 allow_template);
17955 if (type == error_mark_node)
17956 return error_mark_node;
17959 /* Forward declarations of nested types, such as
17961 class C1::C2;
17962 class C1::C2::C3;
17964 are invalid unless all components preceding the final '::'
17965 are complete. If all enclosing types are complete, these
17966 declarations become merely pointless.
17968 Invalid forward declarations of nested types are errors
17969 caught elsewhere in parsing. Those that are pointless arrive
17970 here. */
17972 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17973 && !is_friend && !processing_explicit_instantiation)
17974 warning (0, "declaration %qD does not declare anything", decl);
17976 type = TREE_TYPE (decl);
17978 else
17980 /* An elaborated-type-specifier sometimes introduces a new type and
17981 sometimes names an existing type. Normally, the rule is that it
17982 introduces a new type only if there is not an existing type of
17983 the same name already in scope. For example, given:
17985 struct S {};
17986 void f() { struct S s; }
17988 the `struct S' in the body of `f' is the same `struct S' as in
17989 the global scope; the existing definition is used. However, if
17990 there were no global declaration, this would introduce a new
17991 local class named `S'.
17993 An exception to this rule applies to the following code:
17995 namespace N { struct S; }
17997 Here, the elaborated-type-specifier names a new type
17998 unconditionally; even if there is already an `S' in the
17999 containing scope this declaration names a new type.
18000 This exception only applies if the elaborated-type-specifier
18001 forms the complete declaration:
18003 [class.name]
18005 A declaration consisting solely of `class-key identifier ;' is
18006 either a redeclaration of the name in the current scope or a
18007 forward declaration of the identifier as a class name. It
18008 introduces the name into the current scope.
18010 We are in this situation precisely when the next token is a `;'.
18012 An exception to the exception is that a `friend' declaration does
18013 *not* name a new type; i.e., given:
18015 struct S { friend struct T; };
18017 `T' is not a new type in the scope of `S'.
18019 Also, `new struct S' or `sizeof (struct S)' never results in the
18020 definition of a new type; a new type can only be declared in a
18021 declaration context. */
18023 tag_scope ts;
18024 bool template_p;
18026 if (is_friend)
18027 /* Friends have special name lookup rules. */
18028 ts = ts_within_enclosing_non_class;
18029 else if (is_declaration
18030 && cp_lexer_next_token_is (parser->lexer,
18031 CPP_SEMICOLON))
18032 /* This is a `class-key identifier ;' */
18033 ts = ts_current;
18034 else
18035 ts = ts_global;
18037 template_p =
18038 (template_parm_lists_apply
18039 && (cp_parser_next_token_starts_class_definition_p (parser)
18040 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18041 /* An unqualified name was used to reference this type, so
18042 there were no qualifying templates. */
18043 if (template_parm_lists_apply
18044 && !cp_parser_check_template_parameters (parser,
18045 /*num_templates=*/0,
18046 /*template_id*/false,
18047 token->location,
18048 /*declarator=*/NULL))
18049 return error_mark_node;
18050 type = xref_tag (tag_type, identifier, ts, template_p);
18054 if (type == error_mark_node)
18055 return error_mark_node;
18057 /* Allow attributes on forward declarations of classes. */
18058 if (attributes)
18060 if (TREE_CODE (type) == TYPENAME_TYPE)
18061 warning (OPT_Wattributes,
18062 "attributes ignored on uninstantiated type");
18063 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18064 && ! processing_explicit_instantiation)
18065 warning (OPT_Wattributes,
18066 "attributes ignored on template instantiation");
18067 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18068 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18069 else
18070 warning (OPT_Wattributes,
18071 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18074 if (tag_type != enum_type)
18076 /* Indicate whether this class was declared as a `class' or as a
18077 `struct'. */
18078 if (CLASS_TYPE_P (type))
18079 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18080 cp_parser_check_class_key (tag_type, type);
18083 /* A "<" cannot follow an elaborated type specifier. If that
18084 happens, the user was probably trying to form a template-id. */
18085 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18086 token->location);
18088 return type;
18091 /* Parse an enum-specifier.
18093 enum-specifier:
18094 enum-head { enumerator-list [opt] }
18095 enum-head { enumerator-list , } [C++0x]
18097 enum-head:
18098 enum-key identifier [opt] enum-base [opt]
18099 enum-key nested-name-specifier identifier enum-base [opt]
18101 enum-key:
18102 enum
18103 enum class [C++0x]
18104 enum struct [C++0x]
18106 enum-base: [C++0x]
18107 : type-specifier-seq
18109 opaque-enum-specifier:
18110 enum-key identifier enum-base [opt] ;
18112 GNU Extensions:
18113 enum-key attributes[opt] identifier [opt] enum-base [opt]
18114 { enumerator-list [opt] }attributes[opt]
18115 enum-key attributes[opt] identifier [opt] enum-base [opt]
18116 { enumerator-list, }attributes[opt] [C++0x]
18118 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18119 if the token stream isn't an enum-specifier after all. */
18121 static tree
18122 cp_parser_enum_specifier (cp_parser* parser)
18124 tree identifier;
18125 tree type = NULL_TREE;
18126 tree prev_scope;
18127 tree nested_name_specifier = NULL_TREE;
18128 tree attributes;
18129 bool scoped_enum_p = false;
18130 bool has_underlying_type = false;
18131 bool nested_being_defined = false;
18132 bool new_value_list = false;
18133 bool is_new_type = false;
18134 bool is_unnamed = false;
18135 tree underlying_type = NULL_TREE;
18136 cp_token *type_start_token = NULL;
18137 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18139 parser->colon_corrects_to_scope_p = false;
18141 /* Parse tentatively so that we can back up if we don't find a
18142 enum-specifier. */
18143 cp_parser_parse_tentatively (parser);
18145 /* Caller guarantees that the current token is 'enum', an identifier
18146 possibly follows, and the token after that is an opening brace.
18147 If we don't have an identifier, fabricate an anonymous name for
18148 the enumeration being defined. */
18149 cp_lexer_consume_token (parser->lexer);
18151 /* Parse the "class" or "struct", which indicates a scoped
18152 enumeration type in C++0x. */
18153 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18154 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18156 if (cxx_dialect < cxx11)
18157 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18159 /* Consume the `struct' or `class' token. */
18160 cp_lexer_consume_token (parser->lexer);
18162 scoped_enum_p = true;
18165 attributes = cp_parser_attributes_opt (parser);
18167 /* Clear the qualification. */
18168 parser->scope = NULL_TREE;
18169 parser->qualifying_scope = NULL_TREE;
18170 parser->object_scope = NULL_TREE;
18172 /* Figure out in what scope the declaration is being placed. */
18173 prev_scope = current_scope ();
18175 type_start_token = cp_lexer_peek_token (parser->lexer);
18177 push_deferring_access_checks (dk_no_check);
18178 nested_name_specifier
18179 = cp_parser_nested_name_specifier_opt (parser,
18180 /*typename_keyword_p=*/true,
18181 /*check_dependency_p=*/false,
18182 /*type_p=*/false,
18183 /*is_declaration=*/false);
18185 if (nested_name_specifier)
18187 tree name;
18189 identifier = cp_parser_identifier (parser);
18190 name = cp_parser_lookup_name (parser, identifier,
18191 enum_type,
18192 /*is_template=*/false,
18193 /*is_namespace=*/false,
18194 /*check_dependency=*/true,
18195 /*ambiguous_decls=*/NULL,
18196 input_location);
18197 if (name && name != error_mark_node)
18199 type = TREE_TYPE (name);
18200 if (TREE_CODE (type) == TYPENAME_TYPE)
18202 /* Are template enums allowed in ISO? */
18203 if (template_parm_scope_p ())
18204 pedwarn (type_start_token->location, OPT_Wpedantic,
18205 "%qD is an enumeration template", name);
18206 /* ignore a typename reference, for it will be solved by name
18207 in start_enum. */
18208 type = NULL_TREE;
18211 else if (nested_name_specifier == error_mark_node)
18212 /* We already issued an error. */;
18213 else
18215 error_at (type_start_token->location,
18216 "%qD does not name an enumeration in %qT",
18217 identifier, nested_name_specifier);
18218 nested_name_specifier = error_mark_node;
18221 else
18223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18224 identifier = cp_parser_identifier (parser);
18225 else
18227 identifier = make_anon_name ();
18228 is_unnamed = true;
18229 if (scoped_enum_p)
18230 error_at (type_start_token->location,
18231 "unnamed scoped enum is not allowed");
18234 pop_deferring_access_checks ();
18236 /* Check for the `:' that denotes a specified underlying type in C++0x.
18237 Note that a ':' could also indicate a bitfield width, however. */
18238 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18240 cp_decl_specifier_seq type_specifiers;
18242 /* Consume the `:'. */
18243 cp_lexer_consume_token (parser->lexer);
18245 /* Parse the type-specifier-seq. */
18246 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18247 /*is_trailing_return=*/false,
18248 &type_specifiers);
18250 /* At this point this is surely not elaborated type specifier. */
18251 if (!cp_parser_parse_definitely (parser))
18252 return NULL_TREE;
18254 if (cxx_dialect < cxx11)
18255 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18257 has_underlying_type = true;
18259 /* If that didn't work, stop. */
18260 if (type_specifiers.type != error_mark_node)
18262 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18263 /*initialized=*/0, NULL);
18264 if (underlying_type == error_mark_node
18265 || check_for_bare_parameter_packs (underlying_type))
18266 underlying_type = NULL_TREE;
18270 /* Look for the `{' but don't consume it yet. */
18271 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18273 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18275 cp_parser_error (parser, "expected %<{%>");
18276 if (has_underlying_type)
18278 type = NULL_TREE;
18279 goto out;
18282 /* An opaque-enum-specifier must have a ';' here. */
18283 if ((scoped_enum_p || underlying_type)
18284 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18286 cp_parser_error (parser, "expected %<;%> or %<{%>");
18287 if (has_underlying_type)
18289 type = NULL_TREE;
18290 goto out;
18295 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18296 return NULL_TREE;
18298 if (nested_name_specifier)
18300 if (CLASS_TYPE_P (nested_name_specifier))
18302 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18303 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18304 push_scope (nested_name_specifier);
18306 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18308 push_nested_namespace (nested_name_specifier);
18312 /* Issue an error message if type-definitions are forbidden here. */
18313 if (!cp_parser_check_type_definition (parser))
18314 type = error_mark_node;
18315 else
18316 /* Create the new type. We do this before consuming the opening
18317 brace so the enum will be recorded as being on the line of its
18318 tag (or the 'enum' keyword, if there is no tag). */
18319 type = start_enum (identifier, type, underlying_type,
18320 attributes, scoped_enum_p, &is_new_type);
18322 /* If the next token is not '{' it is an opaque-enum-specifier or an
18323 elaborated-type-specifier. */
18324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18326 timevar_push (TV_PARSE_ENUM);
18327 if (nested_name_specifier
18328 && nested_name_specifier != error_mark_node)
18330 /* The following catches invalid code such as:
18331 enum class S<int>::E { A, B, C }; */
18332 if (!processing_specialization
18333 && CLASS_TYPE_P (nested_name_specifier)
18334 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18335 error_at (type_start_token->location, "cannot add an enumerator "
18336 "list to a template instantiation");
18338 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18340 error_at (type_start_token->location,
18341 "%<%T::%E%> has not been declared",
18342 TYPE_CONTEXT (nested_name_specifier),
18343 nested_name_specifier);
18344 type = error_mark_node;
18346 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18347 && !CLASS_TYPE_P (nested_name_specifier))
18349 error_at (type_start_token->location, "nested name specifier "
18350 "%qT for enum declaration does not name a class "
18351 "or namespace", nested_name_specifier);
18352 type = error_mark_node;
18354 /* If that scope does not contain the scope in which the
18355 class was originally declared, the program is invalid. */
18356 else if (prev_scope && !is_ancestor (prev_scope,
18357 nested_name_specifier))
18359 if (at_namespace_scope_p ())
18360 error_at (type_start_token->location,
18361 "declaration of %qD in namespace %qD which does not "
18362 "enclose %qD",
18363 type, prev_scope, nested_name_specifier);
18364 else
18365 error_at (type_start_token->location,
18366 "declaration of %qD in %qD which does not "
18367 "enclose %qD",
18368 type, prev_scope, nested_name_specifier);
18369 type = error_mark_node;
18371 /* If that scope is the scope where the declaration is being placed
18372 the program is invalid. */
18373 else if (CLASS_TYPE_P (nested_name_specifier)
18374 && CLASS_TYPE_P (prev_scope)
18375 && same_type_p (nested_name_specifier, prev_scope))
18377 permerror (type_start_token->location,
18378 "extra qualification not allowed");
18379 nested_name_specifier = NULL_TREE;
18383 if (scoped_enum_p)
18384 begin_scope (sk_scoped_enum, type);
18386 /* Consume the opening brace. */
18387 matching_braces braces;
18388 braces.consume_open (parser);
18390 if (type == error_mark_node)
18391 ; /* Nothing to add */
18392 else if (OPAQUE_ENUM_P (type)
18393 || (cxx_dialect > cxx98 && processing_specialization))
18395 new_value_list = true;
18396 SET_OPAQUE_ENUM_P (type, false);
18397 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18399 else
18401 error_at (type_start_token->location,
18402 "multiple definition of %q#T", type);
18403 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18404 "previous definition here");
18405 type = error_mark_node;
18408 if (type == error_mark_node)
18409 cp_parser_skip_to_end_of_block_or_statement (parser);
18410 /* If the next token is not '}', then there are some enumerators. */
18411 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18413 if (is_unnamed && !scoped_enum_p)
18414 pedwarn (type_start_token->location, OPT_Wpedantic,
18415 "ISO C++ forbids empty unnamed enum");
18417 else
18418 cp_parser_enumerator_list (parser, type);
18420 /* Consume the final '}'. */
18421 braces.require_close (parser);
18423 if (scoped_enum_p)
18424 finish_scope ();
18425 timevar_pop (TV_PARSE_ENUM);
18427 else
18429 /* If a ';' follows, then it is an opaque-enum-specifier
18430 and additional restrictions apply. */
18431 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18433 if (is_unnamed)
18434 error_at (type_start_token->location,
18435 "opaque-enum-specifier without name");
18436 else if (nested_name_specifier)
18437 error_at (type_start_token->location,
18438 "opaque-enum-specifier must use a simple identifier");
18442 /* Look for trailing attributes to apply to this enumeration, and
18443 apply them if appropriate. */
18444 if (cp_parser_allow_gnu_extensions_p (parser))
18446 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18447 cplus_decl_attributes (&type,
18448 trailing_attr,
18449 (int) ATTR_FLAG_TYPE_IN_PLACE);
18452 /* Finish up the enumeration. */
18453 if (type != error_mark_node)
18455 if (new_value_list)
18456 finish_enum_value_list (type);
18457 if (is_new_type)
18458 finish_enum (type);
18461 if (nested_name_specifier)
18463 if (CLASS_TYPE_P (nested_name_specifier))
18465 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18466 pop_scope (nested_name_specifier);
18468 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18470 pop_nested_namespace (nested_name_specifier);
18473 out:
18474 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18475 return type;
18478 /* Parse an enumerator-list. The enumerators all have the indicated
18479 TYPE.
18481 enumerator-list:
18482 enumerator-definition
18483 enumerator-list , enumerator-definition */
18485 static void
18486 cp_parser_enumerator_list (cp_parser* parser, tree type)
18488 while (true)
18490 /* Parse an enumerator-definition. */
18491 cp_parser_enumerator_definition (parser, type);
18493 /* If the next token is not a ',', we've reached the end of
18494 the list. */
18495 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18496 break;
18497 /* Otherwise, consume the `,' and keep going. */
18498 cp_lexer_consume_token (parser->lexer);
18499 /* If the next token is a `}', there is a trailing comma. */
18500 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18502 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18503 pedwarn (input_location, OPT_Wpedantic,
18504 "comma at end of enumerator list");
18505 break;
18510 /* Parse an enumerator-definition. The enumerator has the indicated
18511 TYPE.
18513 enumerator-definition:
18514 enumerator
18515 enumerator = constant-expression
18517 enumerator:
18518 identifier
18520 GNU Extensions:
18522 enumerator-definition:
18523 enumerator attributes [opt]
18524 enumerator attributes [opt] = constant-expression */
18526 static void
18527 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18529 tree identifier;
18530 tree value;
18531 location_t loc;
18533 /* Save the input location because we are interested in the location
18534 of the identifier and not the location of the explicit value. */
18535 loc = cp_lexer_peek_token (parser->lexer)->location;
18537 /* Look for the identifier. */
18538 identifier = cp_parser_identifier (parser);
18539 if (identifier == error_mark_node)
18540 return;
18542 /* Parse any specified attributes. */
18543 tree attrs = cp_parser_attributes_opt (parser);
18545 /* If the next token is an '=', then there is an explicit value. */
18546 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18548 /* Consume the `=' token. */
18549 cp_lexer_consume_token (parser->lexer);
18550 /* Parse the value. */
18551 value = cp_parser_constant_expression (parser);
18553 else
18554 value = NULL_TREE;
18556 /* If we are processing a template, make sure the initializer of the
18557 enumerator doesn't contain any bare template parameter pack. */
18558 if (check_for_bare_parameter_packs (value))
18559 value = error_mark_node;
18561 /* Create the enumerator. */
18562 build_enumerator (identifier, value, type, attrs, loc);
18565 /* Parse a namespace-name.
18567 namespace-name:
18568 original-namespace-name
18569 namespace-alias
18571 Returns the NAMESPACE_DECL for the namespace. */
18573 static tree
18574 cp_parser_namespace_name (cp_parser* parser)
18576 tree identifier;
18577 tree namespace_decl;
18579 cp_token *token = cp_lexer_peek_token (parser->lexer);
18581 /* Get the name of the namespace. */
18582 identifier = cp_parser_identifier (parser);
18583 if (identifier == error_mark_node)
18584 return error_mark_node;
18586 /* Look up the identifier in the currently active scope. Look only
18587 for namespaces, due to:
18589 [basic.lookup.udir]
18591 When looking up a namespace-name in a using-directive or alias
18592 definition, only namespace names are considered.
18594 And:
18596 [basic.lookup.qual]
18598 During the lookup of a name preceding the :: scope resolution
18599 operator, object, function, and enumerator names are ignored.
18601 (Note that cp_parser_qualifying_entity only calls this
18602 function if the token after the name is the scope resolution
18603 operator.) */
18604 namespace_decl = cp_parser_lookup_name (parser, identifier,
18605 none_type,
18606 /*is_template=*/false,
18607 /*is_namespace=*/true,
18608 /*check_dependency=*/true,
18609 /*ambiguous_decls=*/NULL,
18610 token->location);
18611 /* If it's not a namespace, issue an error. */
18612 if (namespace_decl == error_mark_node
18613 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18615 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18617 error_at (token->location, "%qD is not a namespace-name", identifier);
18618 if (namespace_decl == error_mark_node
18619 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18620 suggest_alternative_in_explicit_scope (token->location, identifier,
18621 parser->scope);
18623 cp_parser_error (parser, "expected namespace-name");
18624 namespace_decl = error_mark_node;
18627 return namespace_decl;
18630 /* Parse a namespace-definition.
18632 namespace-definition:
18633 named-namespace-definition
18634 unnamed-namespace-definition
18636 named-namespace-definition:
18637 original-namespace-definition
18638 extension-namespace-definition
18640 original-namespace-definition:
18641 namespace identifier { namespace-body }
18643 extension-namespace-definition:
18644 namespace original-namespace-name { namespace-body }
18646 unnamed-namespace-definition:
18647 namespace { namespace-body } */
18649 static void
18650 cp_parser_namespace_definition (cp_parser* parser)
18652 tree identifier;
18653 int nested_definition_count = 0;
18655 cp_ensure_no_omp_declare_simd (parser);
18656 cp_ensure_no_oacc_routine (parser);
18658 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18660 if (is_inline)
18662 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18663 cp_lexer_consume_token (parser->lexer);
18666 /* Look for the `namespace' keyword. */
18667 cp_token* token
18668 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18670 /* Parse any specified attributes before the identifier. */
18671 tree attribs = cp_parser_attributes_opt (parser);
18673 for (;;)
18675 identifier = NULL_TREE;
18677 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18679 identifier = cp_parser_identifier (parser);
18681 if (cp_next_tokens_can_be_std_attribute_p (parser))
18682 pedwarn (input_location, OPT_Wpedantic,
18683 "standard attributes on namespaces must precede "
18684 "the namespace name");
18686 /* Parse any attributes specified after the identifier. */
18687 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18691 break;
18693 if (!nested_definition_count && cxx_dialect < cxx17)
18694 pedwarn (input_location, OPT_Wpedantic,
18695 "nested namespace definitions only available with "
18696 "-std=c++17 or -std=gnu++17");
18698 /* Nested namespace names can create new namespaces (unlike
18699 other qualified-ids). */
18700 if (int count = identifier ? push_namespace (identifier) : 0)
18701 nested_definition_count += count;
18702 else
18703 cp_parser_error (parser, "nested namespace name required");
18704 cp_lexer_consume_token (parser->lexer);
18707 if (nested_definition_count && !identifier)
18708 cp_parser_error (parser, "namespace name required");
18710 if (nested_definition_count && attribs)
18711 error_at (token->location,
18712 "a nested namespace definition cannot have attributes");
18713 if (nested_definition_count && is_inline)
18714 error_at (token->location,
18715 "a nested namespace definition cannot be inline");
18717 /* Start the namespace. */
18718 nested_definition_count += push_namespace (identifier, is_inline);
18720 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18722 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18724 /* Look for the `{' to validate starting the namespace. */
18725 matching_braces braces;
18726 if (braces.require_open (parser))
18728 /* Parse the body of the namespace. */
18729 cp_parser_namespace_body (parser);
18731 /* Look for the final `}'. */
18732 braces.require_close (parser);
18735 if (has_visibility)
18736 pop_visibility (1);
18738 /* Pop the nested namespace definitions. */
18739 while (nested_definition_count--)
18740 pop_namespace ();
18743 /* Parse a namespace-body.
18745 namespace-body:
18746 declaration-seq [opt] */
18748 static void
18749 cp_parser_namespace_body (cp_parser* parser)
18751 cp_parser_declaration_seq_opt (parser);
18754 /* Parse a namespace-alias-definition.
18756 namespace-alias-definition:
18757 namespace identifier = qualified-namespace-specifier ; */
18759 static void
18760 cp_parser_namespace_alias_definition (cp_parser* parser)
18762 tree identifier;
18763 tree namespace_specifier;
18765 cp_token *token = cp_lexer_peek_token (parser->lexer);
18767 /* Look for the `namespace' keyword. */
18768 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18769 /* Look for the identifier. */
18770 identifier = cp_parser_identifier (parser);
18771 if (identifier == error_mark_node)
18772 return;
18773 /* Look for the `=' token. */
18774 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18775 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18777 error_at (token->location, "%<namespace%> definition is not allowed here");
18778 /* Skip the definition. */
18779 cp_lexer_consume_token (parser->lexer);
18780 if (cp_parser_skip_to_closing_brace (parser))
18781 cp_lexer_consume_token (parser->lexer);
18782 return;
18784 cp_parser_require (parser, CPP_EQ, RT_EQ);
18785 /* Look for the qualified-namespace-specifier. */
18786 namespace_specifier
18787 = cp_parser_qualified_namespace_specifier (parser);
18788 /* Look for the `;' token. */
18789 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18791 /* Register the alias in the symbol table. */
18792 do_namespace_alias (identifier, namespace_specifier);
18795 /* Parse a qualified-namespace-specifier.
18797 qualified-namespace-specifier:
18798 :: [opt] nested-name-specifier [opt] namespace-name
18800 Returns a NAMESPACE_DECL corresponding to the specified
18801 namespace. */
18803 static tree
18804 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18806 /* Look for the optional `::'. */
18807 cp_parser_global_scope_opt (parser,
18808 /*current_scope_valid_p=*/false);
18810 /* Look for the optional nested-name-specifier. */
18811 cp_parser_nested_name_specifier_opt (parser,
18812 /*typename_keyword_p=*/false,
18813 /*check_dependency_p=*/true,
18814 /*type_p=*/false,
18815 /*is_declaration=*/true);
18817 return cp_parser_namespace_name (parser);
18820 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18821 access declaration.
18823 using-declaration:
18824 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18825 using :: unqualified-id ;
18827 access-declaration:
18828 qualified-id ;
18832 static bool
18833 cp_parser_using_declaration (cp_parser* parser,
18834 bool access_declaration_p)
18836 cp_token *token;
18837 bool typename_p = false;
18838 bool global_scope_p;
18839 tree decl;
18840 tree identifier;
18841 tree qscope;
18842 int oldcount = errorcount;
18843 cp_token *diag_token = NULL;
18845 if (access_declaration_p)
18847 diag_token = cp_lexer_peek_token (parser->lexer);
18848 cp_parser_parse_tentatively (parser);
18850 else
18852 /* Look for the `using' keyword. */
18853 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18855 again:
18856 /* Peek at the next token. */
18857 token = cp_lexer_peek_token (parser->lexer);
18858 /* See if it's `typename'. */
18859 if (token->keyword == RID_TYPENAME)
18861 /* Remember that we've seen it. */
18862 typename_p = true;
18863 /* Consume the `typename' token. */
18864 cp_lexer_consume_token (parser->lexer);
18868 /* Look for the optional global scope qualification. */
18869 global_scope_p
18870 = (cp_parser_global_scope_opt (parser,
18871 /*current_scope_valid_p=*/false)
18872 != NULL_TREE);
18874 /* If we saw `typename', or didn't see `::', then there must be a
18875 nested-name-specifier present. */
18876 if (typename_p || !global_scope_p)
18878 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18879 /*check_dependency_p=*/true,
18880 /*type_p=*/false,
18881 /*is_declaration=*/true);
18882 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18884 cp_parser_skip_to_end_of_block_or_statement (parser);
18885 return false;
18888 /* Otherwise, we could be in either of the two productions. In that
18889 case, treat the nested-name-specifier as optional. */
18890 else
18891 qscope = cp_parser_nested_name_specifier_opt (parser,
18892 /*typename_keyword_p=*/false,
18893 /*check_dependency_p=*/true,
18894 /*type_p=*/false,
18895 /*is_declaration=*/true);
18896 if (!qscope)
18897 qscope = global_namespace;
18898 else if (UNSCOPED_ENUM_P (qscope))
18899 qscope = CP_TYPE_CONTEXT (qscope);
18901 if (access_declaration_p && cp_parser_error_occurred (parser))
18902 /* Something has already gone wrong; there's no need to parse
18903 further. Since an error has occurred, the return value of
18904 cp_parser_parse_definitely will be false, as required. */
18905 return cp_parser_parse_definitely (parser);
18907 token = cp_lexer_peek_token (parser->lexer);
18908 /* Parse the unqualified-id. */
18909 identifier = cp_parser_unqualified_id (parser,
18910 /*template_keyword_p=*/false,
18911 /*check_dependency_p=*/true,
18912 /*declarator_p=*/true,
18913 /*optional_p=*/false);
18915 if (access_declaration_p)
18917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18918 cp_parser_simulate_error (parser);
18919 if (!cp_parser_parse_definitely (parser))
18920 return false;
18922 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18924 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18925 if (cxx_dialect < cxx17
18926 && !in_system_header_at (ell->location))
18927 pedwarn (ell->location, 0,
18928 "pack expansion in using-declaration only available "
18929 "with -std=c++17 or -std=gnu++17");
18930 qscope = make_pack_expansion (qscope);
18933 /* The function we call to handle a using-declaration is different
18934 depending on what scope we are in. */
18935 if (qscope == error_mark_node || identifier == error_mark_node)
18937 else if (!identifier_p (identifier)
18938 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18939 /* [namespace.udecl]
18941 A using declaration shall not name a template-id. */
18942 error_at (token->location,
18943 "a template-id may not appear in a using-declaration");
18944 else
18946 if (at_class_scope_p ())
18948 /* Create the USING_DECL. */
18949 decl = do_class_using_decl (qscope, identifier);
18951 if (decl && typename_p)
18952 USING_DECL_TYPENAME_P (decl) = 1;
18954 if (check_for_bare_parameter_packs (decl))
18956 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18957 return false;
18959 else
18960 /* Add it to the list of members in this class. */
18961 finish_member_declaration (decl);
18963 else
18965 decl = cp_parser_lookup_name_simple (parser,
18966 identifier,
18967 token->location);
18968 if (decl == error_mark_node)
18969 cp_parser_name_lookup_error (parser, identifier,
18970 decl, NLE_NULL,
18971 token->location);
18972 else if (check_for_bare_parameter_packs (decl))
18974 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18975 return false;
18977 else if (!at_namespace_scope_p ())
18978 finish_local_using_decl (decl, qscope, identifier);
18979 else
18980 finish_namespace_using_decl (decl, qscope, identifier);
18984 if (!access_declaration_p
18985 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18987 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18988 if (cxx_dialect < cxx17)
18989 pedwarn (comma->location, 0,
18990 "comma-separated list in using-declaration only available "
18991 "with -std=c++17 or -std=gnu++17");
18992 goto again;
18995 /* Look for the final `;'. */
18996 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18998 if (access_declaration_p && errorcount == oldcount)
18999 warning_at (diag_token->location, OPT_Wdeprecated,
19000 "access declarations are deprecated "
19001 "in favour of using-declarations; "
19002 "suggestion: add the %<using%> keyword");
19004 return true;
19007 /* Parse an alias-declaration.
19009 alias-declaration:
19010 using identifier attribute-specifier-seq [opt] = type-id */
19012 static tree
19013 cp_parser_alias_declaration (cp_parser* parser)
19015 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19016 location_t id_location;
19017 cp_declarator *declarator;
19018 cp_decl_specifier_seq decl_specs;
19019 bool member_p;
19020 const char *saved_message = NULL;
19022 /* Look for the `using' keyword. */
19023 cp_token *using_token
19024 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19025 if (using_token == NULL)
19026 return error_mark_node;
19028 id_location = cp_lexer_peek_token (parser->lexer)->location;
19029 id = cp_parser_identifier (parser);
19030 if (id == error_mark_node)
19031 return error_mark_node;
19033 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19034 attributes = cp_parser_attributes_opt (parser);
19035 if (attributes == error_mark_node)
19036 return error_mark_node;
19038 cp_parser_require (parser, CPP_EQ, RT_EQ);
19040 if (cp_parser_error_occurred (parser))
19041 return error_mark_node;
19043 cp_parser_commit_to_tentative_parse (parser);
19045 /* Now we are going to parse the type-id of the declaration. */
19048 [dcl.type]/3 says:
19050 "A type-specifier-seq shall not define a class or enumeration
19051 unless it appears in the type-id of an alias-declaration (7.1.3) that
19052 is not the declaration of a template-declaration."
19054 In other words, if we currently are in an alias template, the
19055 type-id should not define a type.
19057 So let's set parser->type_definition_forbidden_message in that
19058 case; cp_parser_check_type_definition (called by
19059 cp_parser_class_specifier) will then emit an error if a type is
19060 defined in the type-id. */
19061 if (parser->num_template_parameter_lists)
19063 saved_message = parser->type_definition_forbidden_message;
19064 parser->type_definition_forbidden_message =
19065 G_("types may not be defined in alias template declarations");
19068 type = cp_parser_type_id (parser);
19070 /* Restore the error message if need be. */
19071 if (parser->num_template_parameter_lists)
19072 parser->type_definition_forbidden_message = saved_message;
19074 if (type == error_mark_node
19075 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19077 cp_parser_skip_to_end_of_block_or_statement (parser);
19078 return error_mark_node;
19081 /* A typedef-name can also be introduced by an alias-declaration. The
19082 identifier following the using keyword becomes a typedef-name. It has
19083 the same semantics as if it were introduced by the typedef
19084 specifier. In particular, it does not define a new type and it shall
19085 not appear in the type-id. */
19087 clear_decl_specs (&decl_specs);
19088 decl_specs.type = type;
19089 if (attributes != NULL_TREE)
19091 decl_specs.attributes = attributes;
19092 set_and_check_decl_spec_loc (&decl_specs,
19093 ds_attribute,
19094 attrs_token);
19096 set_and_check_decl_spec_loc (&decl_specs,
19097 ds_typedef,
19098 using_token);
19099 set_and_check_decl_spec_loc (&decl_specs,
19100 ds_alias,
19101 using_token);
19103 if (parser->num_template_parameter_lists
19104 && !cp_parser_check_template_parameters (parser,
19105 /*num_templates=*/0,
19106 /*template_id*/false,
19107 id_location,
19108 /*declarator=*/NULL))
19109 return error_mark_node;
19111 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19112 declarator->id_loc = id_location;
19114 member_p = at_class_scope_p ();
19115 if (member_p)
19116 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19117 NULL_TREE, attributes);
19118 else
19119 decl = start_decl (declarator, &decl_specs, 0,
19120 attributes, NULL_TREE, &pushed_scope);
19121 if (decl == error_mark_node)
19122 return decl;
19124 // Attach constraints to the alias declaration.
19125 if (flag_concepts && current_template_parms)
19127 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19128 tree constr = build_constraints (reqs, NULL_TREE);
19129 set_constraints (decl, constr);
19132 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19134 if (pushed_scope)
19135 pop_scope (pushed_scope);
19137 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19138 added into the symbol table; otherwise, return the TYPE_DECL. */
19139 if (DECL_LANG_SPECIFIC (decl)
19140 && DECL_TEMPLATE_INFO (decl)
19141 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19143 decl = DECL_TI_TEMPLATE (decl);
19144 if (member_p)
19145 check_member_template (decl);
19148 return decl;
19151 /* Parse a using-directive.
19153 using-directive:
19154 using namespace :: [opt] nested-name-specifier [opt]
19155 namespace-name ; */
19157 static void
19158 cp_parser_using_directive (cp_parser* parser)
19160 tree namespace_decl;
19161 tree attribs;
19163 /* Look for the `using' keyword. */
19164 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19165 /* And the `namespace' keyword. */
19166 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19167 /* Look for the optional `::' operator. */
19168 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19169 /* And the optional nested-name-specifier. */
19170 cp_parser_nested_name_specifier_opt (parser,
19171 /*typename_keyword_p=*/false,
19172 /*check_dependency_p=*/true,
19173 /*type_p=*/false,
19174 /*is_declaration=*/true);
19175 /* Get the namespace being used. */
19176 namespace_decl = cp_parser_namespace_name (parser);
19177 /* And any specified attributes. */
19178 attribs = cp_parser_attributes_opt (parser);
19180 /* Update the symbol table. */
19181 if (namespace_bindings_p ())
19182 finish_namespace_using_directive (namespace_decl, attribs);
19183 else
19184 finish_local_using_directive (namespace_decl, attribs);
19186 /* Look for the final `;'. */
19187 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19190 /* Parse an asm-definition.
19192 asm-definition:
19193 asm ( string-literal ) ;
19195 GNU Extension:
19197 asm-definition:
19198 asm volatile [opt] ( string-literal ) ;
19199 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19200 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19201 : asm-operand-list [opt] ) ;
19202 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19203 : asm-operand-list [opt]
19204 : asm-clobber-list [opt] ) ;
19205 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19206 : asm-clobber-list [opt]
19207 : asm-goto-list ) ; */
19209 static void
19210 cp_parser_asm_definition (cp_parser* parser)
19212 tree string;
19213 tree outputs = NULL_TREE;
19214 tree inputs = NULL_TREE;
19215 tree clobbers = NULL_TREE;
19216 tree labels = NULL_TREE;
19217 tree asm_stmt;
19218 bool volatile_p = false;
19219 bool extended_p = false;
19220 bool invalid_inputs_p = false;
19221 bool invalid_outputs_p = false;
19222 bool goto_p = false;
19223 required_token missing = RT_NONE;
19225 /* Look for the `asm' keyword. */
19226 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19228 if (parser->in_function_body
19229 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19231 error ("%<asm%> in %<constexpr%> function");
19232 cp_function_chain->invalid_constexpr = true;
19235 /* See if the next token is `volatile'. */
19236 if (cp_parser_allow_gnu_extensions_p (parser)
19237 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19239 /* Remember that we saw the `volatile' keyword. */
19240 volatile_p = true;
19241 /* Consume the token. */
19242 cp_lexer_consume_token (parser->lexer);
19244 if (cp_parser_allow_gnu_extensions_p (parser)
19245 && parser->in_function_body
19246 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19248 /* Remember that we saw the `goto' keyword. */
19249 goto_p = true;
19250 /* Consume the token. */
19251 cp_lexer_consume_token (parser->lexer);
19253 /* Look for the opening `('. */
19254 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19255 return;
19256 /* Look for the string. */
19257 string = cp_parser_string_literal (parser, false, false);
19258 if (string == error_mark_node)
19260 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19261 /*consume_paren=*/true);
19262 return;
19265 /* If we're allowing GNU extensions, check for the extended assembly
19266 syntax. Unfortunately, the `:' tokens need not be separated by
19267 a space in C, and so, for compatibility, we tolerate that here
19268 too. Doing that means that we have to treat the `::' operator as
19269 two `:' tokens. */
19270 if (cp_parser_allow_gnu_extensions_p (parser)
19271 && parser->in_function_body
19272 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19273 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19275 bool inputs_p = false;
19276 bool clobbers_p = false;
19277 bool labels_p = false;
19279 /* The extended syntax was used. */
19280 extended_p = true;
19282 /* Look for outputs. */
19283 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19285 /* Consume the `:'. */
19286 cp_lexer_consume_token (parser->lexer);
19287 /* Parse the output-operands. */
19288 if (cp_lexer_next_token_is_not (parser->lexer,
19289 CPP_COLON)
19290 && cp_lexer_next_token_is_not (parser->lexer,
19291 CPP_SCOPE)
19292 && cp_lexer_next_token_is_not (parser->lexer,
19293 CPP_CLOSE_PAREN)
19294 && !goto_p)
19296 outputs = cp_parser_asm_operand_list (parser);
19297 if (outputs == error_mark_node)
19298 invalid_outputs_p = true;
19301 /* If the next token is `::', there are no outputs, and the
19302 next token is the beginning of the inputs. */
19303 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19304 /* The inputs are coming next. */
19305 inputs_p = true;
19307 /* Look for inputs. */
19308 if (inputs_p
19309 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19311 /* Consume the `:' or `::'. */
19312 cp_lexer_consume_token (parser->lexer);
19313 /* Parse the output-operands. */
19314 if (cp_lexer_next_token_is_not (parser->lexer,
19315 CPP_COLON)
19316 && cp_lexer_next_token_is_not (parser->lexer,
19317 CPP_SCOPE)
19318 && cp_lexer_next_token_is_not (parser->lexer,
19319 CPP_CLOSE_PAREN))
19321 inputs = cp_parser_asm_operand_list (parser);
19322 if (inputs == error_mark_node)
19323 invalid_inputs_p = true;
19326 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19327 /* The clobbers are coming next. */
19328 clobbers_p = true;
19330 /* Look for clobbers. */
19331 if (clobbers_p
19332 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19334 clobbers_p = true;
19335 /* Consume the `:' or `::'. */
19336 cp_lexer_consume_token (parser->lexer);
19337 /* Parse the clobbers. */
19338 if (cp_lexer_next_token_is_not (parser->lexer,
19339 CPP_COLON)
19340 && cp_lexer_next_token_is_not (parser->lexer,
19341 CPP_CLOSE_PAREN))
19342 clobbers = cp_parser_asm_clobber_list (parser);
19344 else if (goto_p
19345 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19346 /* The labels are coming next. */
19347 labels_p = true;
19349 /* Look for labels. */
19350 if (labels_p
19351 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19353 labels_p = true;
19354 /* Consume the `:' or `::'. */
19355 cp_lexer_consume_token (parser->lexer);
19356 /* Parse the labels. */
19357 labels = cp_parser_asm_label_list (parser);
19360 if (goto_p && !labels_p)
19361 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19363 else if (goto_p)
19364 missing = RT_COLON_SCOPE;
19366 /* Look for the closing `)'. */
19367 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19368 missing ? missing : RT_CLOSE_PAREN))
19369 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19370 /*consume_paren=*/true);
19371 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19373 if (!invalid_inputs_p && !invalid_outputs_p)
19375 /* Create the ASM_EXPR. */
19376 if (parser->in_function_body)
19378 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19379 inputs, clobbers, labels);
19380 /* If the extended syntax was not used, mark the ASM_EXPR. */
19381 if (!extended_p)
19383 tree temp = asm_stmt;
19384 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19385 temp = TREE_OPERAND (temp, 0);
19387 ASM_INPUT_P (temp) = 1;
19390 else
19391 symtab->finalize_toplevel_asm (string);
19395 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19396 type that comes from the decl-specifier-seq. */
19398 static tree
19399 strip_declarator_types (tree type, cp_declarator *declarator)
19401 for (cp_declarator *d = declarator; d;)
19402 switch (d->kind)
19404 case cdk_id:
19405 case cdk_decomp:
19406 case cdk_error:
19407 d = NULL;
19408 break;
19410 default:
19411 if (TYPE_PTRMEMFUNC_P (type))
19412 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19413 type = TREE_TYPE (type);
19414 d = d->declarator;
19415 break;
19418 return type;
19421 /* Declarators [gram.dcl.decl] */
19423 /* Parse an init-declarator.
19425 init-declarator:
19426 declarator initializer [opt]
19428 GNU Extension:
19430 init-declarator:
19431 declarator asm-specification [opt] attributes [opt] initializer [opt]
19433 function-definition:
19434 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19435 function-body
19436 decl-specifier-seq [opt] declarator function-try-block
19438 GNU Extension:
19440 function-definition:
19441 __extension__ function-definition
19443 TM Extension:
19445 function-definition:
19446 decl-specifier-seq [opt] declarator function-transaction-block
19448 The DECL_SPECIFIERS apply to this declarator. Returns a
19449 representation of the entity declared. If MEMBER_P is TRUE, then
19450 this declarator appears in a class scope. The new DECL created by
19451 this declarator is returned.
19453 The CHECKS are access checks that should be performed once we know
19454 what entity is being declared (and, therefore, what classes have
19455 befriended it).
19457 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19458 for a function-definition here as well. If the declarator is a
19459 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19460 be TRUE upon return. By that point, the function-definition will
19461 have been completely parsed.
19463 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19464 is FALSE.
19466 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19467 parsed declaration if it is an uninitialized single declarator not followed
19468 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19469 if present, will not be consumed. If returned, this declarator will be
19470 created with SD_INITIALIZED but will not call cp_finish_decl.
19472 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19473 and there is an initializer, the pointed location_t is set to the
19474 location of the '=' or `(', or '{' in C++11 token introducing the
19475 initializer. */
19477 static tree
19478 cp_parser_init_declarator (cp_parser* parser,
19479 cp_decl_specifier_seq *decl_specifiers,
19480 vec<deferred_access_check, va_gc> *checks,
19481 bool function_definition_allowed_p,
19482 bool member_p,
19483 int declares_class_or_enum,
19484 bool* function_definition_p,
19485 tree* maybe_range_for_decl,
19486 location_t* init_loc,
19487 tree* auto_result)
19489 cp_token *token = NULL, *asm_spec_start_token = NULL,
19490 *attributes_start_token = NULL;
19491 cp_declarator *declarator;
19492 tree prefix_attributes;
19493 tree attributes = NULL;
19494 tree asm_specification;
19495 tree initializer;
19496 tree decl = NULL_TREE;
19497 tree scope;
19498 int is_initialized;
19499 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19500 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19501 "(...)". */
19502 enum cpp_ttype initialization_kind;
19503 bool is_direct_init = false;
19504 bool is_non_constant_init;
19505 int ctor_dtor_or_conv_p;
19506 bool friend_p = cp_parser_friend_p (decl_specifiers);
19507 tree pushed_scope = NULL_TREE;
19508 bool range_for_decl_p = false;
19509 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19510 location_t tmp_init_loc = UNKNOWN_LOCATION;
19512 /* Gather the attributes that were provided with the
19513 decl-specifiers. */
19514 prefix_attributes = decl_specifiers->attributes;
19516 /* Assume that this is not the declarator for a function
19517 definition. */
19518 if (function_definition_p)
19519 *function_definition_p = false;
19521 /* Default arguments are only permitted for function parameters. */
19522 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19523 parser->default_arg_ok_p = false;
19525 /* Defer access checks while parsing the declarator; we cannot know
19526 what names are accessible until we know what is being
19527 declared. */
19528 resume_deferring_access_checks ();
19530 token = cp_lexer_peek_token (parser->lexer);
19532 /* Parse the declarator. */
19533 declarator
19534 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19535 &ctor_dtor_or_conv_p,
19536 /*parenthesized_p=*/NULL,
19537 member_p, friend_p);
19538 /* Gather up the deferred checks. */
19539 stop_deferring_access_checks ();
19541 parser->default_arg_ok_p = saved_default_arg_ok_p;
19543 /* If the DECLARATOR was erroneous, there's no need to go
19544 further. */
19545 if (declarator == cp_error_declarator)
19546 return error_mark_node;
19548 /* Check that the number of template-parameter-lists is OK. */
19549 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19550 token->location))
19551 return error_mark_node;
19553 if (declares_class_or_enum & 2)
19554 cp_parser_check_for_definition_in_return_type (declarator,
19555 decl_specifiers->type,
19556 decl_specifiers->locations[ds_type_spec]);
19558 /* Figure out what scope the entity declared by the DECLARATOR is
19559 located in. `grokdeclarator' sometimes changes the scope, so
19560 we compute it now. */
19561 scope = get_scope_of_declarator (declarator);
19563 /* Perform any lookups in the declared type which were thought to be
19564 dependent, but are not in the scope of the declarator. */
19565 decl_specifiers->type
19566 = maybe_update_decl_type (decl_specifiers->type, scope);
19568 /* If we're allowing GNU extensions, look for an
19569 asm-specification. */
19570 if (cp_parser_allow_gnu_extensions_p (parser))
19572 /* Look for an asm-specification. */
19573 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19574 asm_specification = cp_parser_asm_specification_opt (parser);
19576 else
19577 asm_specification = NULL_TREE;
19579 /* Look for attributes. */
19580 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19581 attributes = cp_parser_attributes_opt (parser);
19583 /* Peek at the next token. */
19584 token = cp_lexer_peek_token (parser->lexer);
19586 bool bogus_implicit_tmpl = false;
19588 if (function_declarator_p (declarator))
19590 /* Handle C++17 deduction guides. */
19591 if (!decl_specifiers->type
19592 && ctor_dtor_or_conv_p <= 0
19593 && cxx_dialect >= cxx17)
19595 cp_declarator *id = get_id_declarator (declarator);
19596 tree name = id->u.id.unqualified_name;
19597 parser->scope = id->u.id.qualifying_scope;
19598 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19599 if (tmpl
19600 && (DECL_CLASS_TEMPLATE_P (tmpl)
19601 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19603 id->u.id.unqualified_name = dguide_name (tmpl);
19604 id->u.id.sfk = sfk_deduction_guide;
19605 ctor_dtor_or_conv_p = 1;
19609 /* Check to see if the token indicates the start of a
19610 function-definition. */
19611 if (cp_parser_token_starts_function_definition_p (token))
19613 if (!function_definition_allowed_p)
19615 /* If a function-definition should not appear here, issue an
19616 error message. */
19617 cp_parser_error (parser,
19618 "a function-definition is not allowed here");
19619 return error_mark_node;
19622 location_t func_brace_location
19623 = cp_lexer_peek_token (parser->lexer)->location;
19625 /* Neither attributes nor an asm-specification are allowed
19626 on a function-definition. */
19627 if (asm_specification)
19628 error_at (asm_spec_start_token->location,
19629 "an asm-specification is not allowed "
19630 "on a function-definition");
19631 if (attributes)
19632 error_at (attributes_start_token->location,
19633 "attributes are not allowed "
19634 "on a function-definition");
19635 /* This is a function-definition. */
19636 *function_definition_p = true;
19638 /* Parse the function definition. */
19639 if (member_p)
19640 decl = cp_parser_save_member_function_body (parser,
19641 decl_specifiers,
19642 declarator,
19643 prefix_attributes);
19644 else
19645 decl =
19646 (cp_parser_function_definition_from_specifiers_and_declarator
19647 (parser, decl_specifiers, prefix_attributes, declarator));
19649 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19651 /* This is where the prologue starts... */
19652 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19653 = func_brace_location;
19656 return decl;
19659 else if (parser->fully_implicit_function_template_p)
19661 /* A non-template declaration involving a function parameter list
19662 containing an implicit template parameter will be made into a
19663 template. If the resulting declaration is not going to be an
19664 actual function then finish the template scope here to prevent it.
19665 An error message will be issued once we have a decl to talk about.
19667 FIXME probably we should do type deduction rather than create an
19668 implicit template, but the standard currently doesn't allow it. */
19669 bogus_implicit_tmpl = true;
19670 finish_fully_implicit_template (parser, NULL_TREE);
19673 /* [dcl.dcl]
19675 Only in function declarations for constructors, destructors, type
19676 conversions, and deduction guides can the decl-specifier-seq be omitted.
19678 We explicitly postpone this check past the point where we handle
19679 function-definitions because we tolerate function-definitions
19680 that are missing their return types in some modes. */
19681 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19683 cp_parser_error (parser,
19684 "expected constructor, destructor, or type conversion");
19685 return error_mark_node;
19688 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19689 if (token->type == CPP_EQ
19690 || token->type == CPP_OPEN_PAREN
19691 || token->type == CPP_OPEN_BRACE)
19693 is_initialized = SD_INITIALIZED;
19694 initialization_kind = token->type;
19695 if (maybe_range_for_decl)
19696 *maybe_range_for_decl = error_mark_node;
19697 tmp_init_loc = token->location;
19698 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19699 *init_loc = tmp_init_loc;
19701 if (token->type == CPP_EQ
19702 && function_declarator_p (declarator))
19704 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19705 if (t2->keyword == RID_DEFAULT)
19706 is_initialized = SD_DEFAULTED;
19707 else if (t2->keyword == RID_DELETE)
19708 is_initialized = SD_DELETED;
19711 else
19713 /* If the init-declarator isn't initialized and isn't followed by a
19714 `,' or `;', it's not a valid init-declarator. */
19715 if (token->type != CPP_COMMA
19716 && token->type != CPP_SEMICOLON)
19718 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19719 range_for_decl_p = true;
19720 else
19722 if (!maybe_range_for_decl)
19723 cp_parser_error (parser, "expected initializer");
19724 return error_mark_node;
19727 is_initialized = SD_UNINITIALIZED;
19728 initialization_kind = CPP_EOF;
19731 /* Because start_decl has side-effects, we should only call it if we
19732 know we're going ahead. By this point, we know that we cannot
19733 possibly be looking at any other construct. */
19734 cp_parser_commit_to_tentative_parse (parser);
19736 /* Enter the newly declared entry in the symbol table. If we're
19737 processing a declaration in a class-specifier, we wait until
19738 after processing the initializer. */
19739 if (!member_p)
19741 if (parser->in_unbraced_linkage_specification_p)
19742 decl_specifiers->storage_class = sc_extern;
19743 decl = start_decl (declarator, decl_specifiers,
19744 range_for_decl_p? SD_INITIALIZED : is_initialized,
19745 attributes, prefix_attributes, &pushed_scope);
19746 cp_finalize_omp_declare_simd (parser, decl);
19747 cp_finalize_oacc_routine (parser, decl, false);
19748 /* Adjust location of decl if declarator->id_loc is more appropriate:
19749 set, and decl wasn't merged with another decl, in which case its
19750 location would be different from input_location, and more accurate. */
19751 if (DECL_P (decl)
19752 && declarator->id_loc != UNKNOWN_LOCATION
19753 && DECL_SOURCE_LOCATION (decl) == input_location)
19754 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19756 else if (scope)
19757 /* Enter the SCOPE. That way unqualified names appearing in the
19758 initializer will be looked up in SCOPE. */
19759 pushed_scope = push_scope (scope);
19761 /* Perform deferred access control checks, now that we know in which
19762 SCOPE the declared entity resides. */
19763 if (!member_p && decl)
19765 tree saved_current_function_decl = NULL_TREE;
19767 /* If the entity being declared is a function, pretend that we
19768 are in its scope. If it is a `friend', it may have access to
19769 things that would not otherwise be accessible. */
19770 if (TREE_CODE (decl) == FUNCTION_DECL)
19772 saved_current_function_decl = current_function_decl;
19773 current_function_decl = decl;
19776 /* Perform access checks for template parameters. */
19777 cp_parser_perform_template_parameter_access_checks (checks);
19779 /* Perform the access control checks for the declarator and the
19780 decl-specifiers. */
19781 perform_deferred_access_checks (tf_warning_or_error);
19783 /* Restore the saved value. */
19784 if (TREE_CODE (decl) == FUNCTION_DECL)
19785 current_function_decl = saved_current_function_decl;
19788 /* Parse the initializer. */
19789 initializer = NULL_TREE;
19790 is_direct_init = false;
19791 is_non_constant_init = true;
19792 if (is_initialized)
19794 if (function_declarator_p (declarator))
19796 if (initialization_kind == CPP_EQ)
19797 initializer = cp_parser_pure_specifier (parser);
19798 else
19800 /* If the declaration was erroneous, we don't really
19801 know what the user intended, so just silently
19802 consume the initializer. */
19803 if (decl != error_mark_node)
19804 error_at (tmp_init_loc, "initializer provided for function");
19805 cp_parser_skip_to_closing_parenthesis (parser,
19806 /*recovering=*/true,
19807 /*or_comma=*/false,
19808 /*consume_paren=*/true);
19811 else
19813 /* We want to record the extra mangling scope for in-class
19814 initializers of class members and initializers of static data
19815 member templates. The former involves deferring
19816 parsing of the initializer until end of class as with default
19817 arguments. So right here we only handle the latter. */
19818 if (!member_p && processing_template_decl && decl != error_mark_node)
19819 start_lambda_scope (decl);
19820 initializer = cp_parser_initializer (parser,
19821 &is_direct_init,
19822 &is_non_constant_init);
19823 if (!member_p && processing_template_decl && decl != error_mark_node)
19824 finish_lambda_scope ();
19825 if (initializer == error_mark_node)
19826 cp_parser_skip_to_end_of_statement (parser);
19830 /* The old parser allows attributes to appear after a parenthesized
19831 initializer. Mark Mitchell proposed removing this functionality
19832 on the GCC mailing lists on 2002-08-13. This parser accepts the
19833 attributes -- but ignores them. Made a permerror in GCC 8. */
19834 if (cp_parser_allow_gnu_extensions_p (parser)
19835 && initialization_kind == CPP_OPEN_PAREN
19836 && cp_parser_attributes_opt (parser)
19837 && permerror (input_location,
19838 "attributes after parenthesized initializer ignored"))
19840 static bool hint;
19841 if (flag_permissive && !hint)
19843 hint = true;
19844 inform (input_location,
19845 "this flexibility is deprecated and will be removed");
19849 /* And now complain about a non-function implicit template. */
19850 if (bogus_implicit_tmpl && decl != error_mark_node)
19851 error_at (DECL_SOURCE_LOCATION (decl),
19852 "non-function %qD declared as implicit template", decl);
19854 /* For an in-class declaration, use `grokfield' to create the
19855 declaration. */
19856 if (member_p)
19858 if (pushed_scope)
19860 pop_scope (pushed_scope);
19861 pushed_scope = NULL_TREE;
19863 decl = grokfield (declarator, decl_specifiers,
19864 initializer, !is_non_constant_init,
19865 /*asmspec=*/NULL_TREE,
19866 attr_chainon (attributes, prefix_attributes));
19867 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19868 cp_parser_save_default_args (parser, decl);
19869 cp_finalize_omp_declare_simd (parser, decl);
19870 cp_finalize_oacc_routine (parser, decl, false);
19873 /* Finish processing the declaration. But, skip member
19874 declarations. */
19875 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19877 cp_finish_decl (decl,
19878 initializer, !is_non_constant_init,
19879 asm_specification,
19880 /* If the initializer is in parentheses, then this is
19881 a direct-initialization, which means that an
19882 `explicit' constructor is OK. Otherwise, an
19883 `explicit' constructor cannot be used. */
19884 ((is_direct_init || !is_initialized)
19885 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19887 else if ((cxx_dialect != cxx98) && friend_p
19888 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19889 /* Core issue #226 (C++0x only): A default template-argument
19890 shall not be specified in a friend class template
19891 declaration. */
19892 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19893 /*is_partial=*/false, /*is_friend_decl=*/1);
19895 if (!friend_p && pushed_scope)
19896 pop_scope (pushed_scope);
19898 if (function_declarator_p (declarator)
19899 && parser->fully_implicit_function_template_p)
19901 if (member_p)
19902 decl = finish_fully_implicit_template (parser, decl);
19903 else
19904 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19907 if (auto_result && is_initialized && decl_specifiers->type
19908 && type_uses_auto (decl_specifiers->type))
19909 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19911 return decl;
19914 /* Parse a declarator.
19916 declarator:
19917 direct-declarator
19918 ptr-operator declarator
19920 abstract-declarator:
19921 ptr-operator abstract-declarator [opt]
19922 direct-abstract-declarator
19924 GNU Extensions:
19926 declarator:
19927 attributes [opt] direct-declarator
19928 attributes [opt] ptr-operator declarator
19930 abstract-declarator:
19931 attributes [opt] ptr-operator abstract-declarator [opt]
19932 attributes [opt] direct-abstract-declarator
19934 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19935 detect constructors, destructors, deduction guides, or conversion operators.
19936 It is set to -1 if the declarator is a name, and +1 if it is a
19937 function. Otherwise it is set to zero. Usually you just want to
19938 test for >0, but internally the negative value is used.
19940 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19941 a decl-specifier-seq unless it declares a constructor, destructor,
19942 or conversion. It might seem that we could check this condition in
19943 semantic analysis, rather than parsing, but that makes it difficult
19944 to handle something like `f()'. We want to notice that there are
19945 no decl-specifiers, and therefore realize that this is an
19946 expression, not a declaration.)
19948 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19949 the declarator is a direct-declarator of the form "(...)".
19951 MEMBER_P is true iff this declarator is a member-declarator.
19953 FRIEND_P is true iff this declarator is a friend. */
19955 static cp_declarator *
19956 cp_parser_declarator (cp_parser* parser,
19957 cp_parser_declarator_kind dcl_kind,
19958 int* ctor_dtor_or_conv_p,
19959 bool* parenthesized_p,
19960 bool member_p, bool friend_p)
19962 cp_declarator *declarator;
19963 enum tree_code code;
19964 cp_cv_quals cv_quals;
19965 tree class_type;
19966 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19968 /* Assume this is not a constructor, destructor, or type-conversion
19969 operator. */
19970 if (ctor_dtor_or_conv_p)
19971 *ctor_dtor_or_conv_p = 0;
19973 if (cp_parser_allow_gnu_extensions_p (parser))
19974 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19976 /* Check for the ptr-operator production. */
19977 cp_parser_parse_tentatively (parser);
19978 /* Parse the ptr-operator. */
19979 code = cp_parser_ptr_operator (parser,
19980 &class_type,
19981 &cv_quals,
19982 &std_attributes);
19984 /* If that worked, then we have a ptr-operator. */
19985 if (cp_parser_parse_definitely (parser))
19987 /* If a ptr-operator was found, then this declarator was not
19988 parenthesized. */
19989 if (parenthesized_p)
19990 *parenthesized_p = true;
19991 /* The dependent declarator is optional if we are parsing an
19992 abstract-declarator. */
19993 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19994 cp_parser_parse_tentatively (parser);
19996 /* Parse the dependent declarator. */
19997 declarator = cp_parser_declarator (parser, dcl_kind,
19998 /*ctor_dtor_or_conv_p=*/NULL,
19999 /*parenthesized_p=*/NULL,
20000 /*member_p=*/false,
20001 friend_p);
20003 /* If we are parsing an abstract-declarator, we must handle the
20004 case where the dependent declarator is absent. */
20005 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20006 && !cp_parser_parse_definitely (parser))
20007 declarator = NULL;
20009 declarator = cp_parser_make_indirect_declarator
20010 (code, class_type, cv_quals, declarator, std_attributes);
20012 /* Everything else is a direct-declarator. */
20013 else
20015 if (parenthesized_p)
20016 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20017 CPP_OPEN_PAREN);
20018 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20019 ctor_dtor_or_conv_p,
20020 member_p, friend_p);
20023 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20024 declarator->attributes = gnu_attributes;
20025 return declarator;
20028 /* Parse a direct-declarator or direct-abstract-declarator.
20030 direct-declarator:
20031 declarator-id
20032 direct-declarator ( parameter-declaration-clause )
20033 cv-qualifier-seq [opt]
20034 ref-qualifier [opt]
20035 exception-specification [opt]
20036 direct-declarator [ constant-expression [opt] ]
20037 ( declarator )
20039 direct-abstract-declarator:
20040 direct-abstract-declarator [opt]
20041 ( parameter-declaration-clause )
20042 cv-qualifier-seq [opt]
20043 ref-qualifier [opt]
20044 exception-specification [opt]
20045 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20046 ( abstract-declarator )
20048 Returns a representation of the declarator. DCL_KIND is
20049 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20050 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20051 we are parsing a direct-declarator. It is
20052 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20053 of ambiguity we prefer an abstract declarator, as per
20054 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20055 as for cp_parser_declarator. */
20057 static cp_declarator *
20058 cp_parser_direct_declarator (cp_parser* parser,
20059 cp_parser_declarator_kind dcl_kind,
20060 int* ctor_dtor_or_conv_p,
20061 bool member_p, bool friend_p)
20063 cp_token *token;
20064 cp_declarator *declarator = NULL;
20065 tree scope = NULL_TREE;
20066 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20067 bool saved_in_declarator_p = parser->in_declarator_p;
20068 bool first = true;
20069 tree pushed_scope = NULL_TREE;
20070 cp_token *open_paren = NULL, *close_paren = NULL;
20072 while (true)
20074 /* Peek at the next token. */
20075 token = cp_lexer_peek_token (parser->lexer);
20076 if (token->type == CPP_OPEN_PAREN)
20078 /* This is either a parameter-declaration-clause, or a
20079 parenthesized declarator. When we know we are parsing a
20080 named declarator, it must be a parenthesized declarator
20081 if FIRST is true. For instance, `(int)' is a
20082 parameter-declaration-clause, with an omitted
20083 direct-abstract-declarator. But `((*))', is a
20084 parenthesized abstract declarator. Finally, when T is a
20085 template parameter `(T)' is a
20086 parameter-declaration-clause, and not a parenthesized
20087 named declarator.
20089 We first try and parse a parameter-declaration-clause,
20090 and then try a nested declarator (if FIRST is true).
20092 It is not an error for it not to be a
20093 parameter-declaration-clause, even when FIRST is
20094 false. Consider,
20096 int i (int);
20097 int i (3);
20099 The first is the declaration of a function while the
20100 second is the definition of a variable, including its
20101 initializer.
20103 Having seen only the parenthesis, we cannot know which of
20104 these two alternatives should be selected. Even more
20105 complex are examples like:
20107 int i (int (a));
20108 int i (int (3));
20110 The former is a function-declaration; the latter is a
20111 variable initialization.
20113 Thus again, we try a parameter-declaration-clause, and if
20114 that fails, we back out and return. */
20116 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20118 tree params;
20119 bool is_declarator = false;
20121 open_paren = NULL;
20123 /* In a member-declarator, the only valid interpretation
20124 of a parenthesis is the start of a
20125 parameter-declaration-clause. (It is invalid to
20126 initialize a static data member with a parenthesized
20127 initializer; only the "=" form of initialization is
20128 permitted.) */
20129 if (!member_p)
20130 cp_parser_parse_tentatively (parser);
20132 /* Consume the `('. */
20133 matching_parens parens;
20134 parens.consume_open (parser);
20135 if (first)
20137 /* If this is going to be an abstract declarator, we're
20138 in a declarator and we can't have default args. */
20139 parser->default_arg_ok_p = false;
20140 parser->in_declarator_p = true;
20143 begin_scope (sk_function_parms, NULL_TREE);
20145 /* Parse the parameter-declaration-clause. */
20146 params = cp_parser_parameter_declaration_clause (parser);
20148 /* Consume the `)'. */
20149 parens.require_close (parser);
20151 /* If all went well, parse the cv-qualifier-seq,
20152 ref-qualifier and the exception-specification. */
20153 if (member_p || cp_parser_parse_definitely (parser))
20155 cp_cv_quals cv_quals;
20156 cp_virt_specifiers virt_specifiers;
20157 cp_ref_qualifier ref_qual;
20158 tree exception_specification;
20159 tree late_return;
20160 tree attrs;
20161 bool memfn = (member_p || (pushed_scope
20162 && CLASS_TYPE_P (pushed_scope)));
20164 is_declarator = true;
20166 if (ctor_dtor_or_conv_p)
20167 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20168 first = false;
20170 /* Parse the cv-qualifier-seq. */
20171 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20172 /* Parse the ref-qualifier. */
20173 ref_qual = cp_parser_ref_qualifier_opt (parser);
20174 /* Parse the tx-qualifier. */
20175 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20176 /* And the exception-specification. */
20177 exception_specification
20178 = cp_parser_exception_specification_opt (parser);
20180 attrs = cp_parser_std_attribute_spec_seq (parser);
20182 /* In here, we handle cases where attribute is used after
20183 the function declaration. For example:
20184 void func (int x) __attribute__((vector(..))); */
20185 tree gnu_attrs = NULL_TREE;
20186 tree requires_clause = NULL_TREE;
20187 late_return = (cp_parser_late_return_type_opt
20188 (parser, declarator, requires_clause,
20189 memfn ? cv_quals : -1));
20191 /* Parse the virt-specifier-seq. */
20192 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20194 /* Create the function-declarator. */
20195 declarator = make_call_declarator (declarator,
20196 params,
20197 cv_quals,
20198 virt_specifiers,
20199 ref_qual,
20200 tx_qual,
20201 exception_specification,
20202 late_return,
20203 requires_clause);
20204 declarator->std_attributes = attrs;
20205 declarator->attributes = gnu_attrs;
20206 /* Any subsequent parameter lists are to do with
20207 return type, so are not those of the declared
20208 function. */
20209 parser->default_arg_ok_p = false;
20212 /* Remove the function parms from scope. */
20213 pop_bindings_and_leave_scope ();
20215 if (is_declarator)
20216 /* Repeat the main loop. */
20217 continue;
20220 /* If this is the first, we can try a parenthesized
20221 declarator. */
20222 if (first)
20224 bool saved_in_type_id_in_expr_p;
20226 parser->default_arg_ok_p = saved_default_arg_ok_p;
20227 parser->in_declarator_p = saved_in_declarator_p;
20229 open_paren = token;
20230 /* Consume the `('. */
20231 matching_parens parens;
20232 parens.consume_open (parser);
20233 /* Parse the nested declarator. */
20234 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20235 parser->in_type_id_in_expr_p = true;
20236 declarator
20237 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20238 /*parenthesized_p=*/NULL,
20239 member_p, friend_p);
20240 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20241 first = false;
20242 /* Expect a `)'. */
20243 close_paren = cp_lexer_peek_token (parser->lexer);
20244 if (!parens.require_close (parser))
20245 declarator = cp_error_declarator;
20246 if (declarator == cp_error_declarator)
20247 break;
20249 goto handle_declarator;
20251 /* Otherwise, we must be done. */
20252 else
20253 break;
20255 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20256 && token->type == CPP_OPEN_SQUARE
20257 && !cp_next_tokens_can_be_attribute_p (parser))
20259 /* Parse an array-declarator. */
20260 tree bounds, attrs;
20262 if (ctor_dtor_or_conv_p)
20263 *ctor_dtor_or_conv_p = 0;
20265 open_paren = NULL;
20266 first = false;
20267 parser->default_arg_ok_p = false;
20268 parser->in_declarator_p = true;
20269 /* Consume the `['. */
20270 cp_lexer_consume_token (parser->lexer);
20271 /* Peek at the next token. */
20272 token = cp_lexer_peek_token (parser->lexer);
20273 /* If the next token is `]', then there is no
20274 constant-expression. */
20275 if (token->type != CPP_CLOSE_SQUARE)
20277 bool non_constant_p;
20278 bounds
20279 = cp_parser_constant_expression (parser,
20280 /*allow_non_constant=*/true,
20281 &non_constant_p);
20282 if (!non_constant_p)
20283 /* OK */;
20284 else if (error_operand_p (bounds))
20285 /* Already gave an error. */;
20286 else if (!parser->in_function_body
20287 || current_binding_level->kind == sk_function_parms)
20289 /* Normally, the array bound must be an integral constant
20290 expression. However, as an extension, we allow VLAs
20291 in function scopes as long as they aren't part of a
20292 parameter declaration. */
20293 cp_parser_error (parser,
20294 "array bound is not an integer constant");
20295 bounds = error_mark_node;
20297 else if (processing_template_decl
20298 && !type_dependent_expression_p (bounds))
20300 /* Remember this wasn't a constant-expression. */
20301 bounds = build_nop (TREE_TYPE (bounds), bounds);
20302 TREE_SIDE_EFFECTS (bounds) = 1;
20305 else
20306 bounds = NULL_TREE;
20307 /* Look for the closing `]'. */
20308 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20310 declarator = cp_error_declarator;
20311 break;
20314 attrs = cp_parser_std_attribute_spec_seq (parser);
20315 declarator = make_array_declarator (declarator, bounds);
20316 declarator->std_attributes = attrs;
20318 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20321 tree qualifying_scope;
20322 tree unqualified_name;
20323 tree attrs;
20324 special_function_kind sfk;
20325 bool abstract_ok;
20326 bool pack_expansion_p = false;
20327 cp_token *declarator_id_start_token;
20329 /* Parse a declarator-id */
20330 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20331 if (abstract_ok)
20333 cp_parser_parse_tentatively (parser);
20335 /* If we see an ellipsis, we should be looking at a
20336 parameter pack. */
20337 if (token->type == CPP_ELLIPSIS)
20339 /* Consume the `...' */
20340 cp_lexer_consume_token (parser->lexer);
20342 pack_expansion_p = true;
20346 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20347 unqualified_name
20348 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20349 qualifying_scope = parser->scope;
20350 if (abstract_ok)
20352 bool okay = false;
20354 if (!unqualified_name && pack_expansion_p)
20356 /* Check whether an error occurred. */
20357 okay = !cp_parser_error_occurred (parser);
20359 /* We already consumed the ellipsis to mark a
20360 parameter pack, but we have no way to report it,
20361 so abort the tentative parse. We will be exiting
20362 immediately anyway. */
20363 cp_parser_abort_tentative_parse (parser);
20365 else
20366 okay = cp_parser_parse_definitely (parser);
20368 if (!okay)
20369 unqualified_name = error_mark_node;
20370 else if (unqualified_name
20371 && (qualifying_scope
20372 || (!identifier_p (unqualified_name))))
20374 cp_parser_error (parser, "expected unqualified-id");
20375 unqualified_name = error_mark_node;
20379 if (!unqualified_name)
20380 return NULL;
20381 if (unqualified_name == error_mark_node)
20383 declarator = cp_error_declarator;
20384 pack_expansion_p = false;
20385 declarator->parameter_pack_p = false;
20386 break;
20389 attrs = cp_parser_std_attribute_spec_seq (parser);
20391 if (qualifying_scope && at_namespace_scope_p ()
20392 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20394 /* In the declaration of a member of a template class
20395 outside of the class itself, the SCOPE will sometimes
20396 be a TYPENAME_TYPE. For example, given:
20398 template <typename T>
20399 int S<T>::R::i = 3;
20401 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20402 this context, we must resolve S<T>::R to an ordinary
20403 type, rather than a typename type.
20405 The reason we normally avoid resolving TYPENAME_TYPEs
20406 is that a specialization of `S' might render
20407 `S<T>::R' not a type. However, if `S' is
20408 specialized, then this `i' will not be used, so there
20409 is no harm in resolving the types here. */
20410 tree type;
20412 /* Resolve the TYPENAME_TYPE. */
20413 type = resolve_typename_type (qualifying_scope,
20414 /*only_current_p=*/false);
20415 /* If that failed, the declarator is invalid. */
20416 if (TREE_CODE (type) == TYPENAME_TYPE)
20418 if (typedef_variant_p (type))
20419 error_at (declarator_id_start_token->location,
20420 "cannot define member of dependent typedef "
20421 "%qT", type);
20422 else
20423 error_at (declarator_id_start_token->location,
20424 "%<%T::%E%> is not a type",
20425 TYPE_CONTEXT (qualifying_scope),
20426 TYPE_IDENTIFIER (qualifying_scope));
20428 qualifying_scope = type;
20431 sfk = sfk_none;
20433 if (unqualified_name)
20435 tree class_type;
20437 if (qualifying_scope
20438 && CLASS_TYPE_P (qualifying_scope))
20439 class_type = qualifying_scope;
20440 else
20441 class_type = current_class_type;
20443 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20445 tree name_type = TREE_TYPE (unqualified_name);
20447 if (!class_type || !same_type_p (name_type, class_type))
20449 /* We do not attempt to print the declarator
20450 here because we do not have enough
20451 information about its original syntactic
20452 form. */
20453 cp_parser_error (parser, "invalid declarator");
20454 declarator = cp_error_declarator;
20455 break;
20457 else if (qualifying_scope
20458 && CLASSTYPE_USE_TEMPLATE (name_type))
20460 error_at (declarator_id_start_token->location,
20461 "invalid use of constructor as a template");
20462 inform (declarator_id_start_token->location,
20463 "use %<%T::%D%> instead of %<%T::%D%> to "
20464 "name the constructor in a qualified name",
20465 class_type,
20466 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20467 class_type, name_type);
20468 declarator = cp_error_declarator;
20469 break;
20471 unqualified_name = constructor_name (class_type);
20474 if (class_type)
20476 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20477 sfk = sfk_destructor;
20478 else if (identifier_p (unqualified_name)
20479 && IDENTIFIER_CONV_OP_P (unqualified_name))
20480 sfk = sfk_conversion;
20481 else if (/* There's no way to declare a constructor
20482 for an unnamed type, even if the type
20483 got a name for linkage purposes. */
20484 !TYPE_WAS_UNNAMED (class_type)
20485 /* Handle correctly (c++/19200):
20487 struct S {
20488 struct T{};
20489 friend void S(T);
20492 and also:
20494 namespace N {
20495 void S();
20498 struct S {
20499 friend void N::S();
20500 }; */
20501 && (!friend_p || class_type == qualifying_scope)
20502 && constructor_name_p (unqualified_name,
20503 class_type))
20504 sfk = sfk_constructor;
20505 else if (is_overloaded_fn (unqualified_name)
20506 && DECL_CONSTRUCTOR_P (get_first_fn
20507 (unqualified_name)))
20508 sfk = sfk_constructor;
20510 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20511 *ctor_dtor_or_conv_p = -1;
20514 declarator = make_id_declarator (qualifying_scope,
20515 unqualified_name,
20516 sfk);
20517 declarator->std_attributes = attrs;
20518 declarator->id_loc = token->location;
20519 declarator->parameter_pack_p = pack_expansion_p;
20521 if (pack_expansion_p)
20522 maybe_warn_variadic_templates ();
20525 handle_declarator:;
20526 scope = get_scope_of_declarator (declarator);
20527 if (scope)
20529 /* Any names that appear after the declarator-id for a
20530 member are looked up in the containing scope. */
20531 if (at_function_scope_p ())
20533 /* But declarations with qualified-ids can't appear in a
20534 function. */
20535 cp_parser_error (parser, "qualified-id in declaration");
20536 declarator = cp_error_declarator;
20537 break;
20539 pushed_scope = push_scope (scope);
20541 parser->in_declarator_p = true;
20542 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20543 || (declarator && declarator->kind == cdk_id))
20544 /* Default args are only allowed on function
20545 declarations. */
20546 parser->default_arg_ok_p = saved_default_arg_ok_p;
20547 else
20548 parser->default_arg_ok_p = false;
20550 first = false;
20552 /* We're done. */
20553 else
20554 break;
20557 /* For an abstract declarator, we might wind up with nothing at this
20558 point. That's an error; the declarator is not optional. */
20559 if (!declarator)
20560 cp_parser_error (parser, "expected declarator");
20561 else if (open_paren)
20563 /* Record overly parenthesized declarator so we can give a
20564 diagnostic about confusing decl/expr disambiguation. */
20565 if (declarator->kind == cdk_array)
20567 /* If the open and close parens are on different lines, this
20568 is probably a formatting thing, so ignore. */
20569 expanded_location open = expand_location (open_paren->location);
20570 expanded_location close = expand_location (close_paren->location);
20571 if (open.line != close.line || open.file != close.file)
20572 open_paren = NULL;
20574 if (open_paren)
20575 declarator->parenthesized = open_paren->location;
20578 /* If we entered a scope, we must exit it now. */
20579 if (pushed_scope)
20580 pop_scope (pushed_scope);
20582 parser->default_arg_ok_p = saved_default_arg_ok_p;
20583 parser->in_declarator_p = saved_in_declarator_p;
20585 return declarator;
20588 /* Parse a ptr-operator.
20590 ptr-operator:
20591 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20592 * cv-qualifier-seq [opt]
20594 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20595 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20597 GNU Extension:
20599 ptr-operator:
20600 & cv-qualifier-seq [opt]
20602 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20603 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20604 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20605 filled in with the TYPE containing the member. *CV_QUALS is
20606 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20607 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20608 Note that the tree codes returned by this function have nothing
20609 to do with the types of trees that will be eventually be created
20610 to represent the pointer or reference type being parsed. They are
20611 just constants with suggestive names. */
20612 static enum tree_code
20613 cp_parser_ptr_operator (cp_parser* parser,
20614 tree* type,
20615 cp_cv_quals *cv_quals,
20616 tree *attributes)
20618 enum tree_code code = ERROR_MARK;
20619 cp_token *token;
20620 tree attrs = NULL_TREE;
20622 /* Assume that it's not a pointer-to-member. */
20623 *type = NULL_TREE;
20624 /* And that there are no cv-qualifiers. */
20625 *cv_quals = TYPE_UNQUALIFIED;
20627 /* Peek at the next token. */
20628 token = cp_lexer_peek_token (parser->lexer);
20630 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20631 if (token->type == CPP_MULT)
20632 code = INDIRECT_REF;
20633 else if (token->type == CPP_AND)
20634 code = ADDR_EXPR;
20635 else if ((cxx_dialect != cxx98) &&
20636 token->type == CPP_AND_AND) /* C++0x only */
20637 code = NON_LVALUE_EXPR;
20639 if (code != ERROR_MARK)
20641 /* Consume the `*', `&' or `&&'. */
20642 cp_lexer_consume_token (parser->lexer);
20644 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20645 `&', if we are allowing GNU extensions. (The only qualifier
20646 that can legally appear after `&' is `restrict', but that is
20647 enforced during semantic analysis. */
20648 if (code == INDIRECT_REF
20649 || cp_parser_allow_gnu_extensions_p (parser))
20650 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20652 attrs = cp_parser_std_attribute_spec_seq (parser);
20653 if (attributes != NULL)
20654 *attributes = attrs;
20656 else
20658 /* Try the pointer-to-member case. */
20659 cp_parser_parse_tentatively (parser);
20660 /* Look for the optional `::' operator. */
20661 cp_parser_global_scope_opt (parser,
20662 /*current_scope_valid_p=*/false);
20663 /* Look for the nested-name specifier. */
20664 token = cp_lexer_peek_token (parser->lexer);
20665 cp_parser_nested_name_specifier (parser,
20666 /*typename_keyword_p=*/false,
20667 /*check_dependency_p=*/true,
20668 /*type_p=*/false,
20669 /*is_declaration=*/false);
20670 /* If we found it, and the next token is a `*', then we are
20671 indeed looking at a pointer-to-member operator. */
20672 if (!cp_parser_error_occurred (parser)
20673 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20675 /* Indicate that the `*' operator was used. */
20676 code = INDIRECT_REF;
20678 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20679 error_at (token->location, "%qD is a namespace", parser->scope);
20680 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20681 error_at (token->location, "cannot form pointer to member of "
20682 "non-class %q#T", parser->scope);
20683 else
20685 /* The type of which the member is a member is given by the
20686 current SCOPE. */
20687 *type = parser->scope;
20688 /* The next name will not be qualified. */
20689 parser->scope = NULL_TREE;
20690 parser->qualifying_scope = NULL_TREE;
20691 parser->object_scope = NULL_TREE;
20692 /* Look for optional c++11 attributes. */
20693 attrs = cp_parser_std_attribute_spec_seq (parser);
20694 if (attributes != NULL)
20695 *attributes = attrs;
20696 /* Look for the optional cv-qualifier-seq. */
20697 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20700 /* If that didn't work we don't have a ptr-operator. */
20701 if (!cp_parser_parse_definitely (parser))
20702 cp_parser_error (parser, "expected ptr-operator");
20705 return code;
20708 /* Parse an (optional) cv-qualifier-seq.
20710 cv-qualifier-seq:
20711 cv-qualifier cv-qualifier-seq [opt]
20713 cv-qualifier:
20714 const
20715 volatile
20717 GNU Extension:
20719 cv-qualifier:
20720 __restrict__
20722 Returns a bitmask representing the cv-qualifiers. */
20724 static cp_cv_quals
20725 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20727 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20729 while (true)
20731 cp_token *token;
20732 cp_cv_quals cv_qualifier;
20734 /* Peek at the next token. */
20735 token = cp_lexer_peek_token (parser->lexer);
20736 /* See if it's a cv-qualifier. */
20737 switch (token->keyword)
20739 case RID_CONST:
20740 cv_qualifier = TYPE_QUAL_CONST;
20741 break;
20743 case RID_VOLATILE:
20744 cv_qualifier = TYPE_QUAL_VOLATILE;
20745 break;
20747 case RID_RESTRICT:
20748 cv_qualifier = TYPE_QUAL_RESTRICT;
20749 break;
20751 default:
20752 cv_qualifier = TYPE_UNQUALIFIED;
20753 break;
20756 if (!cv_qualifier)
20757 break;
20759 if (cv_quals & cv_qualifier)
20761 gcc_rich_location richloc (token->location);
20762 richloc.add_fixit_remove ();
20763 error_at (&richloc, "duplicate cv-qualifier");
20764 cp_lexer_purge_token (parser->lexer);
20766 else
20768 cp_lexer_consume_token (parser->lexer);
20769 cv_quals |= cv_qualifier;
20773 return cv_quals;
20776 /* Parse an (optional) ref-qualifier
20778 ref-qualifier:
20782 Returns cp_ref_qualifier representing ref-qualifier. */
20784 static cp_ref_qualifier
20785 cp_parser_ref_qualifier_opt (cp_parser* parser)
20787 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20789 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20790 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20791 return ref_qual;
20793 while (true)
20795 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20796 cp_token *token = cp_lexer_peek_token (parser->lexer);
20798 switch (token->type)
20800 case CPP_AND:
20801 curr_ref_qual = REF_QUAL_LVALUE;
20802 break;
20804 case CPP_AND_AND:
20805 curr_ref_qual = REF_QUAL_RVALUE;
20806 break;
20808 default:
20809 curr_ref_qual = REF_QUAL_NONE;
20810 break;
20813 if (!curr_ref_qual)
20814 break;
20815 else if (ref_qual)
20817 error_at (token->location, "multiple ref-qualifiers");
20818 cp_lexer_purge_token (parser->lexer);
20820 else
20822 ref_qual = curr_ref_qual;
20823 cp_lexer_consume_token (parser->lexer);
20827 return ref_qual;
20830 /* Parse an optional tx-qualifier.
20832 tx-qualifier:
20833 transaction_safe
20834 transaction_safe_dynamic */
20836 static tree
20837 cp_parser_tx_qualifier_opt (cp_parser *parser)
20839 cp_token *token = cp_lexer_peek_token (parser->lexer);
20840 if (token->type == CPP_NAME)
20842 tree name = token->u.value;
20843 const char *p = IDENTIFIER_POINTER (name);
20844 const int len = strlen ("transaction_safe");
20845 if (!strncmp (p, "transaction_safe", len))
20847 p += len;
20848 if (*p == '\0'
20849 || !strcmp (p, "_dynamic"))
20851 cp_lexer_consume_token (parser->lexer);
20852 if (!flag_tm)
20854 error ("%qE requires %<-fgnu-tm%>", name);
20855 return NULL_TREE;
20857 else
20858 return name;
20862 return NULL_TREE;
20865 /* Parse an (optional) virt-specifier-seq.
20867 virt-specifier-seq:
20868 virt-specifier virt-specifier-seq [opt]
20870 virt-specifier:
20871 override
20872 final
20874 Returns a bitmask representing the virt-specifiers. */
20876 static cp_virt_specifiers
20877 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20879 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20881 while (true)
20883 cp_token *token;
20884 cp_virt_specifiers virt_specifier;
20886 /* Peek at the next token. */
20887 token = cp_lexer_peek_token (parser->lexer);
20888 /* See if it's a virt-specifier-qualifier. */
20889 if (token->type != CPP_NAME)
20890 break;
20891 if (id_equal (token->u.value, "override"))
20893 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20894 virt_specifier = VIRT_SPEC_OVERRIDE;
20896 else if (id_equal (token->u.value, "final"))
20898 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20899 virt_specifier = VIRT_SPEC_FINAL;
20901 else if (id_equal (token->u.value, "__final"))
20903 virt_specifier = VIRT_SPEC_FINAL;
20905 else
20906 break;
20908 if (virt_specifiers & virt_specifier)
20910 gcc_rich_location richloc (token->location);
20911 richloc.add_fixit_remove ();
20912 error_at (&richloc, "duplicate virt-specifier");
20913 cp_lexer_purge_token (parser->lexer);
20915 else
20917 cp_lexer_consume_token (parser->lexer);
20918 virt_specifiers |= virt_specifier;
20921 return virt_specifiers;
20924 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20925 is in scope even though it isn't real. */
20927 void
20928 inject_this_parameter (tree ctype, cp_cv_quals quals)
20930 tree this_parm;
20932 if (current_class_ptr)
20934 /* We don't clear this between NSDMIs. Is it already what we want? */
20935 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20936 if (DECL_P (current_class_ptr)
20937 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20938 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20939 && cp_type_quals (type) == quals)
20940 return;
20943 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20944 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20945 current_class_ptr = NULL_TREE;
20946 current_class_ref
20947 = cp_build_fold_indirect_ref (this_parm);
20948 current_class_ptr = this_parm;
20951 /* Return true iff our current scope is a non-static data member
20952 initializer. */
20954 bool
20955 parsing_nsdmi (void)
20957 /* We recognize NSDMI context by the context-less 'this' pointer set up
20958 by the function above. */
20959 if (current_class_ptr
20960 && TREE_CODE (current_class_ptr) == PARM_DECL
20961 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20962 return true;
20963 return false;
20966 /* Parse a late-specified return type, if any. This is not a separate
20967 non-terminal, but part of a function declarator, which looks like
20969 -> trailing-type-specifier-seq abstract-declarator(opt)
20971 Returns the type indicated by the type-id.
20973 In addition to this, parse any queued up #pragma omp declare simd
20974 clauses, and #pragma acc routine clauses.
20976 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20977 function. */
20979 static tree
20980 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20981 tree& requires_clause, cp_cv_quals quals)
20983 cp_token *token;
20984 tree type = NULL_TREE;
20985 bool declare_simd_p = (parser->omp_declare_simd
20986 && declarator
20987 && declarator->kind == cdk_id);
20989 bool oacc_routine_p = (parser->oacc_routine
20990 && declarator
20991 && declarator->kind == cdk_id);
20993 /* Peek at the next token. */
20994 token = cp_lexer_peek_token (parser->lexer);
20995 /* A late-specified return type is indicated by an initial '->'. */
20996 if (token->type != CPP_DEREF
20997 && token->keyword != RID_REQUIRES
20998 && !(token->type == CPP_NAME
20999 && token->u.value == ridpointers[RID_REQUIRES])
21000 && !(declare_simd_p || oacc_routine_p))
21001 return NULL_TREE;
21003 tree save_ccp = current_class_ptr;
21004 tree save_ccr = current_class_ref;
21005 if (quals >= 0)
21007 /* DR 1207: 'this' is in scope in the trailing return type. */
21008 inject_this_parameter (current_class_type, quals);
21011 if (token->type == CPP_DEREF)
21013 /* Consume the ->. */
21014 cp_lexer_consume_token (parser->lexer);
21016 type = cp_parser_trailing_type_id (parser);
21019 /* Function declarations may be followed by a trailing
21020 requires-clause. */
21021 requires_clause = cp_parser_requires_clause_opt (parser);
21023 if (declare_simd_p)
21024 declarator->attributes
21025 = cp_parser_late_parsing_omp_declare_simd (parser,
21026 declarator->attributes);
21027 if (oacc_routine_p)
21028 declarator->attributes
21029 = cp_parser_late_parsing_oacc_routine (parser,
21030 declarator->attributes);
21032 if (quals >= 0)
21034 current_class_ptr = save_ccp;
21035 current_class_ref = save_ccr;
21038 return type;
21041 /* Parse a declarator-id.
21043 declarator-id:
21044 id-expression
21045 :: [opt] nested-name-specifier [opt] type-name
21047 In the `id-expression' case, the value returned is as for
21048 cp_parser_id_expression if the id-expression was an unqualified-id.
21049 If the id-expression was a qualified-id, then a SCOPE_REF is
21050 returned. The first operand is the scope (either a NAMESPACE_DECL
21051 or TREE_TYPE), but the second is still just a representation of an
21052 unqualified-id. */
21054 static tree
21055 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21057 tree id;
21058 /* The expression must be an id-expression. Assume that qualified
21059 names are the names of types so that:
21061 template <class T>
21062 int S<T>::R::i = 3;
21064 will work; we must treat `S<T>::R' as the name of a type.
21065 Similarly, assume that qualified names are templates, where
21066 required, so that:
21068 template <class T>
21069 int S<T>::R<T>::i = 3;
21071 will work, too. */
21072 id = cp_parser_id_expression (parser,
21073 /*template_keyword_p=*/false,
21074 /*check_dependency_p=*/false,
21075 /*template_p=*/NULL,
21076 /*declarator_p=*/true,
21077 optional_p);
21078 if (id && BASELINK_P (id))
21079 id = BASELINK_FUNCTIONS (id);
21080 return id;
21083 /* Parse a type-id.
21085 type-id:
21086 type-specifier-seq abstract-declarator [opt]
21088 Returns the TYPE specified. */
21090 static tree
21091 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21092 bool is_trailing_return)
21094 cp_decl_specifier_seq type_specifier_seq;
21095 cp_declarator *abstract_declarator;
21097 /* Parse the type-specifier-seq. */
21098 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21099 is_trailing_return,
21100 &type_specifier_seq);
21101 if (is_template_arg && type_specifier_seq.type
21102 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21103 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21104 /* A bare template name as a template argument is a template template
21105 argument, not a placeholder, so fail parsing it as a type argument. */
21107 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21108 cp_parser_simulate_error (parser);
21109 return error_mark_node;
21111 if (type_specifier_seq.type == error_mark_node)
21112 return error_mark_node;
21114 /* There might or might not be an abstract declarator. */
21115 cp_parser_parse_tentatively (parser);
21116 /* Look for the declarator. */
21117 abstract_declarator
21118 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21119 /*parenthesized_p=*/NULL,
21120 /*member_p=*/false,
21121 /*friend_p=*/false);
21122 /* Check to see if there really was a declarator. */
21123 if (!cp_parser_parse_definitely (parser))
21124 abstract_declarator = NULL;
21126 if (type_specifier_seq.type
21127 /* The concepts TS allows 'auto' as a type-id. */
21128 && (!flag_concepts || parser->in_type_id_in_expr_p)
21129 /* None of the valid uses of 'auto' in C++14 involve the type-id
21130 nonterminal, but it is valid in a trailing-return-type. */
21131 && !(cxx_dialect >= cxx14 && is_trailing_return))
21132 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21134 /* A type-id with type 'auto' is only ok if the abstract declarator
21135 is a function declarator with a late-specified return type.
21137 A type-id with 'auto' is also valid in a trailing-return-type
21138 in a compound-requirement. */
21139 if (abstract_declarator
21140 && abstract_declarator->kind == cdk_function
21141 && abstract_declarator->u.function.late_return_type)
21142 /* OK */;
21143 else if (parser->in_result_type_constraint_p)
21144 /* OK */;
21145 else
21147 location_t loc = type_specifier_seq.locations[ds_type_spec];
21148 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21150 error_at (loc, "missing template arguments after %qT",
21151 auto_node);
21152 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21153 tmpl);
21155 else
21156 error_at (loc, "invalid use of %qT", auto_node);
21157 return error_mark_node;
21161 return groktypename (&type_specifier_seq, abstract_declarator,
21162 is_template_arg);
21165 static tree
21166 cp_parser_type_id (cp_parser *parser)
21168 return cp_parser_type_id_1 (parser, false, false);
21171 static tree
21172 cp_parser_template_type_arg (cp_parser *parser)
21174 tree r;
21175 const char *saved_message = parser->type_definition_forbidden_message;
21176 parser->type_definition_forbidden_message
21177 = G_("types may not be defined in template arguments");
21178 r = cp_parser_type_id_1 (parser, true, false);
21179 parser->type_definition_forbidden_message = saved_message;
21180 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21182 error ("invalid use of %<auto%> in template argument");
21183 r = error_mark_node;
21185 return r;
21188 static tree
21189 cp_parser_trailing_type_id (cp_parser *parser)
21191 return cp_parser_type_id_1 (parser, false, true);
21194 /* Parse a type-specifier-seq.
21196 type-specifier-seq:
21197 type-specifier type-specifier-seq [opt]
21199 GNU extension:
21201 type-specifier-seq:
21202 attributes type-specifier-seq [opt]
21204 If IS_DECLARATION is true, we are at the start of a "condition" or
21205 exception-declaration, so we might be followed by a declarator-id.
21207 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21208 i.e. we've just seen "->".
21210 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21212 static void
21213 cp_parser_type_specifier_seq (cp_parser* parser,
21214 bool is_declaration,
21215 bool is_trailing_return,
21216 cp_decl_specifier_seq *type_specifier_seq)
21218 bool seen_type_specifier = false;
21219 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21220 cp_token *start_token = NULL;
21222 /* Clear the TYPE_SPECIFIER_SEQ. */
21223 clear_decl_specs (type_specifier_seq);
21225 /* In the context of a trailing return type, enum E { } is an
21226 elaborated-type-specifier followed by a function-body, not an
21227 enum-specifier. */
21228 if (is_trailing_return)
21229 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21231 /* Parse the type-specifiers and attributes. */
21232 while (true)
21234 tree type_specifier;
21235 bool is_cv_qualifier;
21237 /* Check for attributes first. */
21238 if (cp_next_tokens_can_be_attribute_p (parser))
21240 type_specifier_seq->attributes
21241 = attr_chainon (type_specifier_seq->attributes,
21242 cp_parser_attributes_opt (parser));
21243 continue;
21246 /* record the token of the beginning of the type specifier seq,
21247 for error reporting purposes*/
21248 if (!start_token)
21249 start_token = cp_lexer_peek_token (parser->lexer);
21251 /* Look for the type-specifier. */
21252 type_specifier = cp_parser_type_specifier (parser,
21253 flags,
21254 type_specifier_seq,
21255 /*is_declaration=*/false,
21256 NULL,
21257 &is_cv_qualifier);
21258 if (!type_specifier)
21260 /* If the first type-specifier could not be found, this is not a
21261 type-specifier-seq at all. */
21262 if (!seen_type_specifier)
21264 /* Set in_declarator_p to avoid skipping to the semicolon. */
21265 int in_decl = parser->in_declarator_p;
21266 parser->in_declarator_p = true;
21268 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21269 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21270 cp_parser_error (parser, "expected type-specifier");
21272 parser->in_declarator_p = in_decl;
21274 type_specifier_seq->type = error_mark_node;
21275 return;
21277 /* If subsequent type-specifiers could not be found, the
21278 type-specifier-seq is complete. */
21279 break;
21282 seen_type_specifier = true;
21283 /* The standard says that a condition can be:
21285 type-specifier-seq declarator = assignment-expression
21287 However, given:
21289 struct S {};
21290 if (int S = ...)
21292 we should treat the "S" as a declarator, not as a
21293 type-specifier. The standard doesn't say that explicitly for
21294 type-specifier-seq, but it does say that for
21295 decl-specifier-seq in an ordinary declaration. Perhaps it
21296 would be clearer just to allow a decl-specifier-seq here, and
21297 then add a semantic restriction that if any decl-specifiers
21298 that are not type-specifiers appear, the program is invalid. */
21299 if (is_declaration && !is_cv_qualifier)
21300 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21304 /* Return whether the function currently being declared has an associated
21305 template parameter list. */
21307 static bool
21308 function_being_declared_is_template_p (cp_parser* parser)
21310 if (!current_template_parms || processing_template_parmlist)
21311 return false;
21313 if (parser->implicit_template_scope)
21314 return true;
21316 if (at_class_scope_p ()
21317 && TYPE_BEING_DEFINED (current_class_type))
21318 return parser->num_template_parameter_lists != 0;
21320 return ((int) parser->num_template_parameter_lists > template_class_depth
21321 (current_class_type));
21324 /* Parse a parameter-declaration-clause.
21326 parameter-declaration-clause:
21327 parameter-declaration-list [opt] ... [opt]
21328 parameter-declaration-list , ...
21330 Returns a representation for the parameter declarations. A return
21331 value of NULL indicates a parameter-declaration-clause consisting
21332 only of an ellipsis. */
21334 static tree
21335 cp_parser_parameter_declaration_clause (cp_parser* parser)
21337 tree parameters;
21338 cp_token *token;
21339 bool ellipsis_p;
21341 temp_override<bool> cleanup
21342 (parser->auto_is_implicit_function_template_parm_p);
21344 if (!processing_specialization
21345 && !processing_template_parmlist
21346 && !processing_explicit_instantiation
21347 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21348 actual function or a random abstract declarator. */
21349 && parser->default_arg_ok_p)
21350 if (!current_function_decl
21351 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21352 parser->auto_is_implicit_function_template_parm_p = true;
21354 /* Peek at the next token. */
21355 token = cp_lexer_peek_token (parser->lexer);
21356 /* Check for trivial parameter-declaration-clauses. */
21357 if (token->type == CPP_ELLIPSIS)
21359 /* Consume the `...' token. */
21360 cp_lexer_consume_token (parser->lexer);
21361 return NULL_TREE;
21363 else if (token->type == CPP_CLOSE_PAREN)
21364 /* There are no parameters. */
21366 #ifndef NO_IMPLICIT_EXTERN_C
21367 if (in_system_header_at (input_location)
21368 && current_class_type == NULL
21369 && current_lang_name == lang_name_c)
21370 return NULL_TREE;
21371 else
21372 #endif
21373 return void_list_node;
21375 /* Check for `(void)', too, which is a special case. */
21376 else if (token->keyword == RID_VOID
21377 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21378 == CPP_CLOSE_PAREN))
21380 /* Consume the `void' token. */
21381 cp_lexer_consume_token (parser->lexer);
21382 /* There are no parameters. */
21383 return void_list_node;
21386 /* Parse the parameter-declaration-list. */
21387 parameters = cp_parser_parameter_declaration_list (parser);
21388 /* If a parse error occurred while parsing the
21389 parameter-declaration-list, then the entire
21390 parameter-declaration-clause is erroneous. */
21391 if (parameters == error_mark_node)
21392 return NULL_TREE;
21394 /* Peek at the next token. */
21395 token = cp_lexer_peek_token (parser->lexer);
21396 /* If it's a `,', the clause should terminate with an ellipsis. */
21397 if (token->type == CPP_COMMA)
21399 /* Consume the `,'. */
21400 cp_lexer_consume_token (parser->lexer);
21401 /* Expect an ellipsis. */
21402 ellipsis_p
21403 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21405 /* It might also be `...' if the optional trailing `,' was
21406 omitted. */
21407 else if (token->type == CPP_ELLIPSIS)
21409 /* Consume the `...' token. */
21410 cp_lexer_consume_token (parser->lexer);
21411 /* And remember that we saw it. */
21412 ellipsis_p = true;
21414 else
21415 ellipsis_p = false;
21417 /* Finish the parameter list. */
21418 if (!ellipsis_p)
21419 parameters = chainon (parameters, void_list_node);
21421 return parameters;
21424 /* Parse a parameter-declaration-list.
21426 parameter-declaration-list:
21427 parameter-declaration
21428 parameter-declaration-list , parameter-declaration
21430 Returns a representation of the parameter-declaration-list, as for
21431 cp_parser_parameter_declaration_clause. However, the
21432 `void_list_node' is never appended to the list. */
21434 static tree
21435 cp_parser_parameter_declaration_list (cp_parser* parser)
21437 tree parameters = NULL_TREE;
21438 tree *tail = &parameters;
21439 bool saved_in_unbraced_linkage_specification_p;
21440 int index = 0;
21442 /* The special considerations that apply to a function within an
21443 unbraced linkage specifications do not apply to the parameters
21444 to the function. */
21445 saved_in_unbraced_linkage_specification_p
21446 = parser->in_unbraced_linkage_specification_p;
21447 parser->in_unbraced_linkage_specification_p = false;
21449 /* Look for more parameters. */
21450 while (true)
21452 cp_parameter_declarator *parameter;
21453 tree decl = error_mark_node;
21454 bool parenthesized_p = false;
21456 /* Parse the parameter. */
21457 parameter
21458 = cp_parser_parameter_declaration (parser,
21459 /*template_parm_p=*/false,
21460 &parenthesized_p);
21462 /* We don't know yet if the enclosing context is deprecated, so wait
21463 and warn in grokparms if appropriate. */
21464 deprecated_state = DEPRECATED_SUPPRESS;
21466 if (parameter)
21468 decl = grokdeclarator (parameter->declarator,
21469 &parameter->decl_specifiers,
21470 PARM,
21471 parameter->default_argument != NULL_TREE,
21472 &parameter->decl_specifiers.attributes);
21473 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21474 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21477 deprecated_state = DEPRECATED_NORMAL;
21479 /* If a parse error occurred parsing the parameter declaration,
21480 then the entire parameter-declaration-list is erroneous. */
21481 if (decl == error_mark_node)
21483 parameters = error_mark_node;
21484 break;
21487 if (parameter->decl_specifiers.attributes)
21488 cplus_decl_attributes (&decl,
21489 parameter->decl_specifiers.attributes,
21491 if (DECL_NAME (decl))
21492 decl = pushdecl (decl);
21494 if (decl != error_mark_node)
21496 retrofit_lang_decl (decl);
21497 DECL_PARM_INDEX (decl) = ++index;
21498 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21501 /* Add the new parameter to the list. */
21502 *tail = build_tree_list (parameter->default_argument, decl);
21503 tail = &TREE_CHAIN (*tail);
21505 /* Peek at the next token. */
21506 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21507 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21508 /* These are for Objective-C++ */
21509 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21510 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21511 /* The parameter-declaration-list is complete. */
21512 break;
21513 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21515 cp_token *token;
21517 /* Peek at the next token. */
21518 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21519 /* If it's an ellipsis, then the list is complete. */
21520 if (token->type == CPP_ELLIPSIS)
21521 break;
21522 /* Otherwise, there must be more parameters. Consume the
21523 `,'. */
21524 cp_lexer_consume_token (parser->lexer);
21525 /* When parsing something like:
21527 int i(float f, double d)
21529 we can tell after seeing the declaration for "f" that we
21530 are not looking at an initialization of a variable "i",
21531 but rather at the declaration of a function "i".
21533 Due to the fact that the parsing of template arguments
21534 (as specified to a template-id) requires backtracking we
21535 cannot use this technique when inside a template argument
21536 list. */
21537 if (!parser->in_template_argument_list_p
21538 && !parser->in_type_id_in_expr_p
21539 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21540 /* However, a parameter-declaration of the form
21541 "float(f)" (which is a valid declaration of a
21542 parameter "f") can also be interpreted as an
21543 expression (the conversion of "f" to "float"). */
21544 && !parenthesized_p)
21545 cp_parser_commit_to_tentative_parse (parser);
21547 else
21549 cp_parser_error (parser, "expected %<,%> or %<...%>");
21550 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21551 cp_parser_skip_to_closing_parenthesis (parser,
21552 /*recovering=*/true,
21553 /*or_comma=*/false,
21554 /*consume_paren=*/false);
21555 break;
21559 parser->in_unbraced_linkage_specification_p
21560 = saved_in_unbraced_linkage_specification_p;
21562 /* Reset implicit_template_scope if we are about to leave the function
21563 parameter list that introduced it. Note that for out-of-line member
21564 definitions, there will be one or more class scopes before we get to
21565 the template parameter scope. */
21567 if (cp_binding_level *its = parser->implicit_template_scope)
21568 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21570 while (maybe_its->kind == sk_class)
21571 maybe_its = maybe_its->level_chain;
21572 if (maybe_its == its)
21574 parser->implicit_template_parms = 0;
21575 parser->implicit_template_scope = 0;
21579 return parameters;
21582 /* Parse a parameter declaration.
21584 parameter-declaration:
21585 decl-specifier-seq ... [opt] declarator
21586 decl-specifier-seq declarator = assignment-expression
21587 decl-specifier-seq ... [opt] abstract-declarator [opt]
21588 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21590 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21591 declares a template parameter. (In that case, a non-nested `>'
21592 token encountered during the parsing of the assignment-expression
21593 is not interpreted as a greater-than operator.)
21595 Returns a representation of the parameter, or NULL if an error
21596 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21597 true iff the declarator is of the form "(p)". */
21599 static cp_parameter_declarator *
21600 cp_parser_parameter_declaration (cp_parser *parser,
21601 bool template_parm_p,
21602 bool *parenthesized_p)
21604 int declares_class_or_enum;
21605 cp_decl_specifier_seq decl_specifiers;
21606 cp_declarator *declarator;
21607 tree default_argument;
21608 cp_token *token = NULL, *declarator_token_start = NULL;
21609 const char *saved_message;
21610 bool template_parameter_pack_p = false;
21612 /* In a template parameter, `>' is not an operator.
21614 [temp.param]
21616 When parsing a default template-argument for a non-type
21617 template-parameter, the first non-nested `>' is taken as the end
21618 of the template parameter-list rather than a greater-than
21619 operator. */
21621 /* Type definitions may not appear in parameter types. */
21622 saved_message = parser->type_definition_forbidden_message;
21623 parser->type_definition_forbidden_message
21624 = G_("types may not be defined in parameter types");
21626 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21627 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21628 (current_template_parms)) : 0);
21630 /* Parse the declaration-specifiers. */
21631 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21632 cp_parser_decl_specifier_seq (parser,
21633 CP_PARSER_FLAGS_NONE,
21634 &decl_specifiers,
21635 &declares_class_or_enum);
21637 /* Complain about missing 'typename' or other invalid type names. */
21638 if (!decl_specifiers.any_type_specifiers_p
21639 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21640 decl_specifiers.type = error_mark_node;
21642 /* If an error occurred, there's no reason to attempt to parse the
21643 rest of the declaration. */
21644 if (cp_parser_error_occurred (parser))
21646 parser->type_definition_forbidden_message = saved_message;
21647 return NULL;
21650 /* Peek at the next token. */
21651 token = cp_lexer_peek_token (parser->lexer);
21653 /* If the next token is a `)', `,', `=', `>', or `...', then there
21654 is no declarator. However, when variadic templates are enabled,
21655 there may be a declarator following `...'. */
21656 if (token->type == CPP_CLOSE_PAREN
21657 || token->type == CPP_COMMA
21658 || token->type == CPP_EQ
21659 || token->type == CPP_GREATER)
21661 declarator = NULL;
21662 if (parenthesized_p)
21663 *parenthesized_p = false;
21665 /* Otherwise, there should be a declarator. */
21666 else
21668 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21669 parser->default_arg_ok_p = false;
21671 /* After seeing a decl-specifier-seq, if the next token is not a
21672 "(", there is no possibility that the code is a valid
21673 expression. Therefore, if parsing tentatively, we commit at
21674 this point. */
21675 if (!parser->in_template_argument_list_p
21676 /* In an expression context, having seen:
21678 (int((char ...
21680 we cannot be sure whether we are looking at a
21681 function-type (taking a "char" as a parameter) or a cast
21682 of some object of type "char" to "int". */
21683 && !parser->in_type_id_in_expr_p
21684 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21685 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21686 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21687 cp_parser_commit_to_tentative_parse (parser);
21688 /* Parse the declarator. */
21689 declarator_token_start = token;
21690 declarator = cp_parser_declarator (parser,
21691 CP_PARSER_DECLARATOR_EITHER,
21692 /*ctor_dtor_or_conv_p=*/NULL,
21693 parenthesized_p,
21694 /*member_p=*/false,
21695 /*friend_p=*/false);
21696 parser->default_arg_ok_p = saved_default_arg_ok_p;
21697 /* After the declarator, allow more attributes. */
21698 decl_specifiers.attributes
21699 = attr_chainon (decl_specifiers.attributes,
21700 cp_parser_attributes_opt (parser));
21702 /* If the declarator is a template parameter pack, remember that and
21703 clear the flag in the declarator itself so we don't get errors
21704 from grokdeclarator. */
21705 if (template_parm_p && declarator && declarator->parameter_pack_p)
21707 declarator->parameter_pack_p = false;
21708 template_parameter_pack_p = true;
21712 /* If the next token is an ellipsis, and we have not seen a declarator
21713 name, and if either the type of the declarator contains parameter
21714 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21715 for, eg, abbreviated integral type names), then we actually have a
21716 parameter pack expansion expression. Otherwise, leave the ellipsis
21717 for a C-style variadic function. */
21718 token = cp_lexer_peek_token (parser->lexer);
21720 /* If a function parameter pack was specified and an implicit template
21721 parameter was introduced during cp_parser_parameter_declaration,
21722 change any implicit parameters introduced into packs. */
21723 if (parser->implicit_template_parms
21724 && (token->type == CPP_ELLIPSIS
21725 || (declarator && declarator->parameter_pack_p)))
21727 int latest_template_parm_idx = TREE_VEC_LENGTH
21728 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21730 if (latest_template_parm_idx != template_parm_idx)
21731 decl_specifiers.type = convert_generic_types_to_packs
21732 (decl_specifiers.type,
21733 template_parm_idx, latest_template_parm_idx);
21736 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21738 tree type = decl_specifiers.type;
21740 if (type && DECL_P (type))
21741 type = TREE_TYPE (type);
21743 if (((type
21744 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21745 && (template_parm_p || uses_parameter_packs (type)))
21746 || (!type && template_parm_p))
21747 && declarator_can_be_parameter_pack (declarator))
21749 /* Consume the `...'. */
21750 cp_lexer_consume_token (parser->lexer);
21751 maybe_warn_variadic_templates ();
21753 /* Build a pack expansion type */
21754 if (template_parm_p)
21755 template_parameter_pack_p = true;
21756 else if (declarator)
21757 declarator->parameter_pack_p = true;
21758 else
21759 decl_specifiers.type = make_pack_expansion (type);
21763 /* The restriction on defining new types applies only to the type
21764 of the parameter, not to the default argument. */
21765 parser->type_definition_forbidden_message = saved_message;
21767 /* If the next token is `=', then process a default argument. */
21768 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21770 tree type = decl_specifiers.type;
21771 token = cp_lexer_peek_token (parser->lexer);
21772 /* If we are defining a class, then the tokens that make up the
21773 default argument must be saved and processed later. */
21774 if (!template_parm_p && at_class_scope_p ()
21775 && TYPE_BEING_DEFINED (current_class_type)
21776 && !LAMBDA_TYPE_P (current_class_type))
21777 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21779 // A constrained-type-specifier may declare a type template-parameter.
21780 else if (declares_constrained_type_template_parameter (type))
21781 default_argument
21782 = cp_parser_default_type_template_argument (parser);
21784 // A constrained-type-specifier may declare a template-template-parameter.
21785 else if (declares_constrained_template_template_parameter (type))
21786 default_argument
21787 = cp_parser_default_template_template_argument (parser);
21789 /* Outside of a class definition, we can just parse the
21790 assignment-expression. */
21791 else
21792 default_argument
21793 = cp_parser_default_argument (parser, template_parm_p);
21795 if (!parser->default_arg_ok_p)
21797 permerror (token->location,
21798 "default arguments are only "
21799 "permitted for function parameters");
21801 else if ((declarator && declarator->parameter_pack_p)
21802 || template_parameter_pack_p
21803 || (decl_specifiers.type
21804 && PACK_EXPANSION_P (decl_specifiers.type)))
21806 /* Find the name of the parameter pack. */
21807 cp_declarator *id_declarator = declarator;
21808 while (id_declarator && id_declarator->kind != cdk_id)
21809 id_declarator = id_declarator->declarator;
21811 if (id_declarator && id_declarator->kind == cdk_id)
21812 error_at (declarator_token_start->location,
21813 template_parm_p
21814 ? G_("template parameter pack %qD "
21815 "cannot have a default argument")
21816 : G_("parameter pack %qD cannot have "
21817 "a default argument"),
21818 id_declarator->u.id.unqualified_name);
21819 else
21820 error_at (declarator_token_start->location,
21821 template_parm_p
21822 ? G_("template parameter pack cannot have "
21823 "a default argument")
21824 : G_("parameter pack cannot have a "
21825 "default argument"));
21827 default_argument = NULL_TREE;
21830 else
21831 default_argument = NULL_TREE;
21833 /* Generate a location for the parameter, ranging from the start of the
21834 initial token to the end of the final token (using input_location for
21835 the latter, set up by cp_lexer_set_source_position_from_token when
21836 consuming tokens).
21838 If we have a identifier, then use it for the caret location, e.g.
21840 extern int callee (int one, int (*two)(int, int), float three);
21841 ~~~~~~^~~~~~~~~~~~~~
21843 otherwise, reuse the start location for the caret location e.g.:
21845 extern int callee (int one, int (*)(int, int), float three);
21846 ^~~~~~~~~~~~~~~~~
21849 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21850 ? declarator->id_loc
21851 : decl_spec_token_start->location);
21852 location_t param_loc = make_location (caret_loc,
21853 decl_spec_token_start->location,
21854 input_location);
21856 return make_parameter_declarator (&decl_specifiers,
21857 declarator,
21858 default_argument,
21859 param_loc,
21860 template_parameter_pack_p);
21863 /* Parse a default argument and return it.
21865 TEMPLATE_PARM_P is true if this is a default argument for a
21866 non-type template parameter. */
21867 static tree
21868 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21870 tree default_argument = NULL_TREE;
21871 bool saved_greater_than_is_operator_p;
21872 bool saved_local_variables_forbidden_p;
21873 bool non_constant_p, is_direct_init;
21875 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21876 set correctly. */
21877 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21878 parser->greater_than_is_operator_p = !template_parm_p;
21879 /* Local variable names (and the `this' keyword) may not
21880 appear in a default argument. */
21881 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21882 parser->local_variables_forbidden_p = true;
21883 /* Parse the assignment-expression. */
21884 if (template_parm_p)
21885 push_deferring_access_checks (dk_no_deferred);
21886 tree saved_class_ptr = NULL_TREE;
21887 tree saved_class_ref = NULL_TREE;
21888 /* The "this" pointer is not valid in a default argument. */
21889 if (cfun)
21891 saved_class_ptr = current_class_ptr;
21892 cp_function_chain->x_current_class_ptr = NULL_TREE;
21893 saved_class_ref = current_class_ref;
21894 cp_function_chain->x_current_class_ref = NULL_TREE;
21896 default_argument
21897 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21898 /* Restore the "this" pointer. */
21899 if (cfun)
21901 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21902 cp_function_chain->x_current_class_ref = saved_class_ref;
21904 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21905 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21906 if (template_parm_p)
21907 pop_deferring_access_checks ();
21908 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21909 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21911 return default_argument;
21914 /* Parse a function-body.
21916 function-body:
21917 compound_statement */
21919 static void
21920 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21922 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21923 ? BCS_TRY_BLOCK : BCS_NORMAL),
21924 true);
21927 /* Parse a ctor-initializer-opt followed by a function-body. Return
21928 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21929 is true we are parsing a function-try-block. */
21931 static void
21932 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21933 bool in_function_try_block)
21935 tree body, list;
21936 const bool check_body_p =
21937 DECL_CONSTRUCTOR_P (current_function_decl)
21938 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21939 tree last = NULL;
21941 /* Begin the function body. */
21942 body = begin_function_body ();
21943 /* Parse the optional ctor-initializer. */
21944 cp_parser_ctor_initializer_opt (parser);
21946 /* If we're parsing a constexpr constructor definition, we need
21947 to check that the constructor body is indeed empty. However,
21948 before we get to cp_parser_function_body lot of junk has been
21949 generated, so we can't just check that we have an empty block.
21950 Rather we take a snapshot of the outermost block, and check whether
21951 cp_parser_function_body changed its state. */
21952 if (check_body_p)
21954 list = cur_stmt_list;
21955 if (STATEMENT_LIST_TAIL (list))
21956 last = STATEMENT_LIST_TAIL (list)->stmt;
21958 /* Parse the function-body. */
21959 cp_parser_function_body (parser, in_function_try_block);
21960 if (check_body_p)
21961 check_constexpr_ctor_body (last, list, /*complain=*/true);
21962 /* Finish the function body. */
21963 finish_function_body (body);
21966 /* Parse an initializer.
21968 initializer:
21969 = initializer-clause
21970 ( expression-list )
21972 Returns an expression representing the initializer. If no
21973 initializer is present, NULL_TREE is returned.
21975 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21976 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21977 set to TRUE if there is no initializer present. If there is an
21978 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21979 is set to true; otherwise it is set to false. */
21981 static tree
21982 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21983 bool* non_constant_p, bool subexpression_p)
21985 cp_token *token;
21986 tree init;
21988 /* Peek at the next token. */
21989 token = cp_lexer_peek_token (parser->lexer);
21991 /* Let our caller know whether or not this initializer was
21992 parenthesized. */
21993 *is_direct_init = (token->type != CPP_EQ);
21994 /* Assume that the initializer is constant. */
21995 *non_constant_p = false;
21997 if (token->type == CPP_EQ)
21999 /* Consume the `='. */
22000 cp_lexer_consume_token (parser->lexer);
22001 /* Parse the initializer-clause. */
22002 init = cp_parser_initializer_clause (parser, non_constant_p);
22004 else if (token->type == CPP_OPEN_PAREN)
22006 vec<tree, va_gc> *vec;
22007 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22008 /*cast_p=*/false,
22009 /*allow_expansion_p=*/true,
22010 non_constant_p);
22011 if (vec == NULL)
22012 return error_mark_node;
22013 init = build_tree_list_vec (vec);
22014 release_tree_vector (vec);
22016 else if (token->type == CPP_OPEN_BRACE)
22018 cp_lexer_set_source_position (parser->lexer);
22019 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22020 init = cp_parser_braced_list (parser, non_constant_p);
22021 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22023 else
22025 /* Anything else is an error. */
22026 cp_parser_error (parser, "expected initializer");
22027 init = error_mark_node;
22030 if (!subexpression_p && check_for_bare_parameter_packs (init))
22031 init = error_mark_node;
22033 return init;
22036 /* Parse an initializer-clause.
22038 initializer-clause:
22039 assignment-expression
22040 braced-init-list
22042 Returns an expression representing the initializer.
22044 If the `assignment-expression' production is used the value
22045 returned is simply a representation for the expression.
22047 Otherwise, calls cp_parser_braced_list. */
22049 static cp_expr
22050 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22052 cp_expr initializer;
22054 /* Assume the expression is constant. */
22055 *non_constant_p = false;
22057 /* If it is not a `{', then we are looking at an
22058 assignment-expression. */
22059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22061 initializer
22062 = cp_parser_constant_expression (parser,
22063 /*allow_non_constant_p=*/true,
22064 non_constant_p);
22066 else
22067 initializer = cp_parser_braced_list (parser, non_constant_p);
22069 return initializer;
22072 /* Parse a brace-enclosed initializer list.
22074 braced-init-list:
22075 { initializer-list , [opt] }
22076 { designated-initializer-list , [opt] }
22079 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22080 the elements of the initializer-list (or NULL, if the last
22081 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22082 NULL_TREE. There is no way to detect whether or not the optional
22083 trailing `,' was provided. NON_CONSTANT_P is as for
22084 cp_parser_initializer. */
22086 static cp_expr
22087 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22089 tree initializer;
22090 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22092 /* Consume the `{' token. */
22093 matching_braces braces;
22094 braces.require_open (parser);
22095 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22096 initializer = make_node (CONSTRUCTOR);
22097 /* If it's not a `}', then there is a non-trivial initializer. */
22098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22100 /* Parse the initializer list. */
22101 CONSTRUCTOR_ELTS (initializer)
22102 = cp_parser_initializer_list (parser, non_constant_p);
22103 /* A trailing `,' token is allowed. */
22104 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22105 cp_lexer_consume_token (parser->lexer);
22107 else
22108 *non_constant_p = false;
22109 /* Now, there should be a trailing `}'. */
22110 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22111 braces.require_close (parser);
22112 TREE_TYPE (initializer) = init_list_type_node;
22114 cp_expr result (initializer);
22115 /* Build a location of the form:
22116 { ... }
22117 ^~~~~~~
22118 with caret==start at the open brace, finish at the close brace. */
22119 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22120 result.set_location (combined_loc);
22121 return result;
22124 /* Consume tokens up to, and including, the next non-nested closing `]'.
22125 Returns true iff we found a closing `]'. */
22127 static bool
22128 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22130 unsigned square_depth = 0;
22132 while (true)
22134 cp_token * token = cp_lexer_peek_token (parser->lexer);
22136 switch (token->type)
22138 case CPP_EOF:
22139 case CPP_PRAGMA_EOL:
22140 /* If we've run out of tokens, then there is no closing `]'. */
22141 return false;
22143 case CPP_OPEN_SQUARE:
22144 ++square_depth;
22145 break;
22147 case CPP_CLOSE_SQUARE:
22148 if (!square_depth--)
22150 cp_lexer_consume_token (parser->lexer);
22151 return true;
22153 break;
22155 default:
22156 break;
22159 /* Consume the token. */
22160 cp_lexer_consume_token (parser->lexer);
22164 /* Return true if we are looking at an array-designator, false otherwise. */
22166 static bool
22167 cp_parser_array_designator_p (cp_parser *parser)
22169 /* Consume the `['. */
22170 cp_lexer_consume_token (parser->lexer);
22172 cp_lexer_save_tokens (parser->lexer);
22174 /* Skip tokens until the next token is a closing square bracket.
22175 If we find the closing `]', and the next token is a `=', then
22176 we are looking at an array designator. */
22177 bool array_designator_p
22178 = (cp_parser_skip_to_closing_square_bracket (parser)
22179 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22181 /* Roll back the tokens we skipped. */
22182 cp_lexer_rollback_tokens (parser->lexer);
22184 return array_designator_p;
22187 /* Parse an initializer-list.
22189 initializer-list:
22190 initializer-clause ... [opt]
22191 initializer-list , initializer-clause ... [opt]
22193 C++2A Extension:
22195 designated-initializer-list:
22196 designated-initializer-clause
22197 designated-initializer-list , designated-initializer-clause
22199 designated-initializer-clause:
22200 designator brace-or-equal-initializer
22202 designator:
22203 . identifier
22205 GNU Extension:
22207 initializer-list:
22208 designation initializer-clause ...[opt]
22209 initializer-list , designation initializer-clause ...[opt]
22211 designation:
22212 . identifier =
22213 identifier :
22214 [ constant-expression ] =
22216 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22217 for the initializer. If the INDEX of the elt is non-NULL, it is the
22218 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22219 as for cp_parser_initializer. */
22221 static vec<constructor_elt, va_gc> *
22222 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22224 vec<constructor_elt, va_gc> *v = NULL;
22225 bool first_p = true;
22226 tree first_designator = NULL_TREE;
22228 /* Assume all of the expressions are constant. */
22229 *non_constant_p = false;
22231 /* Parse the rest of the list. */
22232 while (true)
22234 cp_token *token;
22235 tree designator;
22236 tree initializer;
22237 bool clause_non_constant_p;
22238 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22240 /* Handle the C++2A syntax, '. id ='. */
22241 if ((cxx_dialect >= cxx2a
22242 || cp_parser_allow_gnu_extensions_p (parser))
22243 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22244 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22245 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22246 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22247 == CPP_OPEN_BRACE)))
22249 if (cxx_dialect < cxx2a)
22250 pedwarn (loc, OPT_Wpedantic,
22251 "C++ designated initializers only available with "
22252 "-std=c++2a or -std=gnu++2a");
22253 /* Consume the `.'. */
22254 cp_lexer_consume_token (parser->lexer);
22255 /* Consume the identifier. */
22256 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22257 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22258 /* Consume the `='. */
22259 cp_lexer_consume_token (parser->lexer);
22261 /* Also, if the next token is an identifier and the following one is a
22262 colon, we are looking at the GNU designated-initializer
22263 syntax. */
22264 else if (cp_parser_allow_gnu_extensions_p (parser)
22265 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22266 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22267 == CPP_COLON))
22269 /* Warn the user that they are using an extension. */
22270 pedwarn (loc, OPT_Wpedantic,
22271 "ISO C++ does not allow GNU designated initializers");
22272 /* Consume the identifier. */
22273 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22274 /* Consume the `:'. */
22275 cp_lexer_consume_token (parser->lexer);
22277 /* Also handle C99 array designators, '[ const ] ='. */
22278 else if (cp_parser_allow_gnu_extensions_p (parser)
22279 && !c_dialect_objc ()
22280 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22282 /* In C++11, [ could start a lambda-introducer. */
22283 bool non_const = false;
22285 cp_parser_parse_tentatively (parser);
22287 if (!cp_parser_array_designator_p (parser))
22289 cp_parser_simulate_error (parser);
22290 designator = NULL_TREE;
22292 else
22294 designator = cp_parser_constant_expression (parser, true,
22295 &non_const);
22296 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22297 cp_parser_require (parser, CPP_EQ, RT_EQ);
22300 if (!cp_parser_parse_definitely (parser))
22301 designator = NULL_TREE;
22302 else if (non_const
22303 && (!require_potential_rvalue_constant_expression
22304 (designator)))
22305 designator = NULL_TREE;
22306 if (designator)
22307 /* Warn the user that they are using an extension. */
22308 pedwarn (loc, OPT_Wpedantic,
22309 "ISO C++ does not allow C99 designated initializers");
22311 else
22312 designator = NULL_TREE;
22314 if (first_p)
22316 first_designator = designator;
22317 first_p = false;
22319 else if (cxx_dialect >= cxx2a
22320 && first_designator != error_mark_node
22321 && (!first_designator != !designator))
22323 error_at (loc, "either all initializer clauses should be designated "
22324 "or none of them should be");
22325 first_designator = error_mark_node;
22327 else if (cxx_dialect < cxx2a && !first_designator)
22328 first_designator = designator;
22330 /* Parse the initializer. */
22331 initializer = cp_parser_initializer_clause (parser,
22332 &clause_non_constant_p);
22333 /* If any clause is non-constant, so is the entire initializer. */
22334 if (clause_non_constant_p)
22335 *non_constant_p = true;
22337 /* If we have an ellipsis, this is an initializer pack
22338 expansion. */
22339 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22341 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22343 /* Consume the `...'. */
22344 cp_lexer_consume_token (parser->lexer);
22346 if (designator && cxx_dialect >= cxx2a)
22347 error_at (loc,
22348 "%<...%> not allowed in designated initializer list");
22350 /* Turn the initializer into an initializer expansion. */
22351 initializer = make_pack_expansion (initializer);
22354 /* Add it to the vector. */
22355 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22357 /* If the next token is not a comma, we have reached the end of
22358 the list. */
22359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22360 break;
22362 /* Peek at the next token. */
22363 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22364 /* If the next token is a `}', then we're still done. An
22365 initializer-clause can have a trailing `,' after the
22366 initializer-list and before the closing `}'. */
22367 if (token->type == CPP_CLOSE_BRACE)
22368 break;
22370 /* Consume the `,' token. */
22371 cp_lexer_consume_token (parser->lexer);
22374 /* The same identifier shall not appear in multiple designators
22375 of a designated-initializer-list. */
22376 if (first_designator)
22378 unsigned int i;
22379 tree designator, val;
22380 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22381 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22383 if (IDENTIFIER_MARKED (designator))
22385 error_at (cp_expr_loc_or_loc (val, input_location),
22386 "%<.%s%> designator used multiple times in "
22387 "the same initializer list",
22388 IDENTIFIER_POINTER (designator));
22389 (*v)[i].index = NULL_TREE;
22391 else
22392 IDENTIFIER_MARKED (designator) = 1;
22394 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22395 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22396 IDENTIFIER_MARKED (designator) = 0;
22399 return v;
22402 /* Classes [gram.class] */
22404 /* Parse a class-name.
22406 class-name:
22407 identifier
22408 template-id
22410 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22411 to indicate that names looked up in dependent types should be
22412 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22413 keyword has been used to indicate that the name that appears next
22414 is a template. TAG_TYPE indicates the explicit tag given before
22415 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22416 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22417 is the class being defined in a class-head. If ENUM_OK is TRUE,
22418 enum-names are also accepted.
22420 Returns the TYPE_DECL representing the class. */
22422 static tree
22423 cp_parser_class_name (cp_parser *parser,
22424 bool typename_keyword_p,
22425 bool template_keyword_p,
22426 enum tag_types tag_type,
22427 bool check_dependency_p,
22428 bool class_head_p,
22429 bool is_declaration,
22430 bool enum_ok)
22432 tree decl;
22433 tree scope;
22434 bool typename_p;
22435 cp_token *token;
22436 tree identifier = NULL_TREE;
22438 /* All class-names start with an identifier. */
22439 token = cp_lexer_peek_token (parser->lexer);
22440 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22442 cp_parser_error (parser, "expected class-name");
22443 return error_mark_node;
22446 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22447 to a template-id, so we save it here. */
22448 scope = parser->scope;
22449 if (scope == error_mark_node)
22450 return error_mark_node;
22452 /* Any name names a type if we're following the `typename' keyword
22453 in a qualified name where the enclosing scope is type-dependent. */
22454 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22455 && dependent_type_p (scope));
22456 /* Handle the common case (an identifier, but not a template-id)
22457 efficiently. */
22458 if (token->type == CPP_NAME
22459 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22461 cp_token *identifier_token;
22462 bool ambiguous_p;
22464 /* Look for the identifier. */
22465 identifier_token = cp_lexer_peek_token (parser->lexer);
22466 ambiguous_p = identifier_token->error_reported;
22467 identifier = cp_parser_identifier (parser);
22468 /* If the next token isn't an identifier, we are certainly not
22469 looking at a class-name. */
22470 if (identifier == error_mark_node)
22471 decl = error_mark_node;
22472 /* If we know this is a type-name, there's no need to look it
22473 up. */
22474 else if (typename_p)
22475 decl = identifier;
22476 else
22478 tree ambiguous_decls;
22479 /* If we already know that this lookup is ambiguous, then
22480 we've already issued an error message; there's no reason
22481 to check again. */
22482 if (ambiguous_p)
22484 cp_parser_simulate_error (parser);
22485 return error_mark_node;
22487 /* If the next token is a `::', then the name must be a type
22488 name.
22490 [basic.lookup.qual]
22492 During the lookup for a name preceding the :: scope
22493 resolution operator, object, function, and enumerator
22494 names are ignored. */
22495 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22496 tag_type = scope_type;
22497 /* Look up the name. */
22498 decl = cp_parser_lookup_name (parser, identifier,
22499 tag_type,
22500 /*is_template=*/false,
22501 /*is_namespace=*/false,
22502 check_dependency_p,
22503 &ambiguous_decls,
22504 identifier_token->location);
22505 if (ambiguous_decls)
22507 if (cp_parser_parsing_tentatively (parser))
22508 cp_parser_simulate_error (parser);
22509 return error_mark_node;
22513 else
22515 /* Try a template-id. */
22516 decl = cp_parser_template_id (parser, template_keyword_p,
22517 check_dependency_p,
22518 tag_type,
22519 is_declaration);
22520 if (decl == error_mark_node)
22521 return error_mark_node;
22524 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22526 /* If this is a typename, create a TYPENAME_TYPE. */
22527 if (typename_p && decl != error_mark_node)
22529 decl = make_typename_type (scope, decl, typename_type,
22530 /*complain=*/tf_error);
22531 if (decl != error_mark_node)
22532 decl = TYPE_NAME (decl);
22535 decl = strip_using_decl (decl);
22537 /* Check to see that it is really the name of a class. */
22538 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22539 && identifier_p (TREE_OPERAND (decl, 0))
22540 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22541 /* Situations like this:
22543 template <typename T> struct A {
22544 typename T::template X<int>::I i;
22547 are problematic. Is `T::template X<int>' a class-name? The
22548 standard does not seem to be definitive, but there is no other
22549 valid interpretation of the following `::'. Therefore, those
22550 names are considered class-names. */
22552 decl = make_typename_type (scope, decl, tag_type, tf_error);
22553 if (decl != error_mark_node)
22554 decl = TYPE_NAME (decl);
22556 else if (TREE_CODE (decl) != TYPE_DECL
22557 || TREE_TYPE (decl) == error_mark_node
22558 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22559 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22560 /* In Objective-C 2.0, a classname followed by '.' starts a
22561 dot-syntax expression, and it's not a type-name. */
22562 || (c_dialect_objc ()
22563 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22564 && objc_is_class_name (decl)))
22565 decl = error_mark_node;
22567 if (decl == error_mark_node)
22568 cp_parser_error (parser, "expected class-name");
22569 else if (identifier && !parser->scope)
22570 maybe_note_name_used_in_class (identifier, decl);
22572 return decl;
22575 /* Parse a class-specifier.
22577 class-specifier:
22578 class-head { member-specification [opt] }
22580 Returns the TREE_TYPE representing the class. */
22582 static tree
22583 cp_parser_class_specifier_1 (cp_parser* parser)
22585 tree type;
22586 tree attributes = NULL_TREE;
22587 bool nested_name_specifier_p;
22588 unsigned saved_num_template_parameter_lists;
22589 bool saved_in_function_body;
22590 unsigned char in_statement;
22591 bool in_switch_statement_p;
22592 bool saved_in_unbraced_linkage_specification_p;
22593 tree old_scope = NULL_TREE;
22594 tree scope = NULL_TREE;
22595 cp_token *closing_brace;
22597 push_deferring_access_checks (dk_no_deferred);
22599 /* Parse the class-head. */
22600 type = cp_parser_class_head (parser,
22601 &nested_name_specifier_p);
22602 /* If the class-head was a semantic disaster, skip the entire body
22603 of the class. */
22604 if (!type)
22606 cp_parser_skip_to_end_of_block_or_statement (parser);
22607 pop_deferring_access_checks ();
22608 return error_mark_node;
22611 /* Look for the `{'. */
22612 matching_braces braces;
22613 if (!braces.require_open (parser))
22615 pop_deferring_access_checks ();
22616 return error_mark_node;
22619 cp_ensure_no_omp_declare_simd (parser);
22620 cp_ensure_no_oacc_routine (parser);
22622 /* Issue an error message if type-definitions are forbidden here. */
22623 cp_parser_check_type_definition (parser);
22624 /* Remember that we are defining one more class. */
22625 ++parser->num_classes_being_defined;
22626 /* Inside the class, surrounding template-parameter-lists do not
22627 apply. */
22628 saved_num_template_parameter_lists
22629 = parser->num_template_parameter_lists;
22630 parser->num_template_parameter_lists = 0;
22631 /* We are not in a function body. */
22632 saved_in_function_body = parser->in_function_body;
22633 parser->in_function_body = false;
22634 /* Or in a loop. */
22635 in_statement = parser->in_statement;
22636 parser->in_statement = 0;
22637 /* Or in a switch. */
22638 in_switch_statement_p = parser->in_switch_statement_p;
22639 parser->in_switch_statement_p = false;
22640 /* We are not immediately inside an extern "lang" block. */
22641 saved_in_unbraced_linkage_specification_p
22642 = parser->in_unbraced_linkage_specification_p;
22643 parser->in_unbraced_linkage_specification_p = false;
22645 // Associate constraints with the type.
22646 if (flag_concepts)
22647 type = associate_classtype_constraints (type);
22649 /* Start the class. */
22650 if (nested_name_specifier_p)
22652 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22653 old_scope = push_inner_scope (scope);
22655 type = begin_class_definition (type);
22657 if (type == error_mark_node)
22658 /* If the type is erroneous, skip the entire body of the class. */
22659 cp_parser_skip_to_closing_brace (parser);
22660 else
22661 /* Parse the member-specification. */
22662 cp_parser_member_specification_opt (parser);
22664 /* Look for the trailing `}'. */
22665 closing_brace = braces.require_close (parser);
22666 /* Look for trailing attributes to apply to this class. */
22667 if (cp_parser_allow_gnu_extensions_p (parser))
22668 attributes = cp_parser_gnu_attributes_opt (parser);
22669 if (type != error_mark_node)
22670 type = finish_struct (type, attributes);
22671 if (nested_name_specifier_p)
22672 pop_inner_scope (old_scope, scope);
22674 /* We've finished a type definition. Check for the common syntax
22675 error of forgetting a semicolon after the definition. We need to
22676 be careful, as we can't just check for not-a-semicolon and be done
22677 with it; the user might have typed:
22679 class X { } c = ...;
22680 class X { } *p = ...;
22682 and so forth. Instead, enumerate all the possible tokens that
22683 might follow this production; if we don't see one of them, then
22684 complain and silently insert the semicolon. */
22686 cp_token *token = cp_lexer_peek_token (parser->lexer);
22687 bool want_semicolon = true;
22689 if (cp_next_tokens_can_be_std_attribute_p (parser))
22690 /* Don't try to parse c++11 attributes here. As per the
22691 grammar, that should be a task for
22692 cp_parser_decl_specifier_seq. */
22693 want_semicolon = false;
22695 switch (token->type)
22697 case CPP_NAME:
22698 case CPP_SEMICOLON:
22699 case CPP_MULT:
22700 case CPP_AND:
22701 case CPP_OPEN_PAREN:
22702 case CPP_CLOSE_PAREN:
22703 case CPP_COMMA:
22704 want_semicolon = false;
22705 break;
22707 /* While it's legal for type qualifiers and storage class
22708 specifiers to follow type definitions in the grammar, only
22709 compiler testsuites contain code like that. Assume that if
22710 we see such code, then what we're really seeing is a case
22711 like:
22713 class X { }
22714 const <type> var = ...;
22718 class Y { }
22719 static <type> func (...) ...
22721 i.e. the qualifier or specifier applies to the next
22722 declaration. To do so, however, we need to look ahead one
22723 more token to see if *that* token is a type specifier.
22725 This code could be improved to handle:
22727 class Z { }
22728 static const <type> var = ...; */
22729 case CPP_KEYWORD:
22730 if (keyword_is_decl_specifier (token->keyword))
22732 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22734 /* Handling user-defined types here would be nice, but very
22735 tricky. */
22736 want_semicolon
22737 = (lookahead->type == CPP_KEYWORD
22738 && keyword_begins_type_specifier (lookahead->keyword));
22740 break;
22741 default:
22742 break;
22745 /* If we don't have a type, then something is very wrong and we
22746 shouldn't try to do anything clever. Likewise for not seeing the
22747 closing brace. */
22748 if (closing_brace && TYPE_P (type) && want_semicolon)
22750 /* Locate the closing brace. */
22751 cp_token_position prev
22752 = cp_lexer_previous_token_position (parser->lexer);
22753 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22754 location_t loc = prev_token->location;
22756 /* We want to suggest insertion of a ';' immediately *after* the
22757 closing brace, so, if we can, offset the location by 1 column. */
22758 location_t next_loc = loc;
22759 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22760 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22762 rich_location richloc (line_table, next_loc);
22764 /* If we successfully offset the location, suggest the fix-it. */
22765 if (next_loc != loc)
22766 richloc.add_fixit_insert_before (next_loc, ";");
22768 if (CLASSTYPE_DECLARED_CLASS (type))
22769 error_at (&richloc,
22770 "expected %<;%> after class definition");
22771 else if (TREE_CODE (type) == RECORD_TYPE)
22772 error_at (&richloc,
22773 "expected %<;%> after struct definition");
22774 else if (TREE_CODE (type) == UNION_TYPE)
22775 error_at (&richloc,
22776 "expected %<;%> after union definition");
22777 else
22778 gcc_unreachable ();
22780 /* Unget one token and smash it to look as though we encountered
22781 a semicolon in the input stream. */
22782 cp_lexer_set_token_position (parser->lexer, prev);
22783 token = cp_lexer_peek_token (parser->lexer);
22784 token->type = CPP_SEMICOLON;
22785 token->keyword = RID_MAX;
22789 /* If this class is not itself within the scope of another class,
22790 then we need to parse the bodies of all of the queued function
22791 definitions. Note that the queued functions defined in a class
22792 are not always processed immediately following the
22793 class-specifier for that class. Consider:
22795 struct A {
22796 struct B { void f() { sizeof (A); } };
22799 If `f' were processed before the processing of `A' were
22800 completed, there would be no way to compute the size of `A'.
22801 Note that the nesting we are interested in here is lexical --
22802 not the semantic nesting given by TYPE_CONTEXT. In particular,
22803 for:
22805 struct A { struct B; };
22806 struct A::B { void f() { } };
22808 there is no need to delay the parsing of `A::B::f'. */
22809 if (--parser->num_classes_being_defined == 0)
22811 tree decl;
22812 tree class_type = NULL_TREE;
22813 tree pushed_scope = NULL_TREE;
22814 unsigned ix;
22815 cp_default_arg_entry *e;
22816 tree save_ccp, save_ccr;
22818 if (any_erroneous_template_args_p (type))
22820 /* Skip default arguments, NSDMIs, etc, in order to improve
22821 error recovery (c++/71169, c++/71832). */
22822 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22823 vec_safe_truncate (unparsed_nsdmis, 0);
22824 vec_safe_truncate (unparsed_classes, 0);
22825 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22828 /* In a first pass, parse default arguments to the functions.
22829 Then, in a second pass, parse the bodies of the functions.
22830 This two-phased approach handles cases like:
22832 struct S {
22833 void f() { g(); }
22834 void g(int i = 3);
22838 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22840 decl = e->decl;
22841 /* If there are default arguments that have not yet been processed,
22842 take care of them now. */
22843 if (class_type != e->class_type)
22845 if (pushed_scope)
22846 pop_scope (pushed_scope);
22847 class_type = e->class_type;
22848 pushed_scope = push_scope (class_type);
22850 /* Make sure that any template parameters are in scope. */
22851 maybe_begin_member_template_processing (decl);
22852 /* Parse the default argument expressions. */
22853 cp_parser_late_parsing_default_args (parser, decl);
22854 /* Remove any template parameters from the symbol table. */
22855 maybe_end_member_template_processing ();
22857 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22858 /* Now parse any NSDMIs. */
22859 save_ccp = current_class_ptr;
22860 save_ccr = current_class_ref;
22861 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22863 if (class_type != DECL_CONTEXT (decl))
22865 if (pushed_scope)
22866 pop_scope (pushed_scope);
22867 class_type = DECL_CONTEXT (decl);
22868 pushed_scope = push_scope (class_type);
22870 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22871 cp_parser_late_parsing_nsdmi (parser, decl);
22873 vec_safe_truncate (unparsed_nsdmis, 0);
22874 current_class_ptr = save_ccp;
22875 current_class_ref = save_ccr;
22876 if (pushed_scope)
22877 pop_scope (pushed_scope);
22879 /* Now do some post-NSDMI bookkeeping. */
22880 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22881 after_nsdmi_defaulted_late_checks (class_type);
22882 vec_safe_truncate (unparsed_classes, 0);
22883 after_nsdmi_defaulted_late_checks (type);
22885 /* Now parse the body of the functions. */
22886 if (flag_openmp)
22888 /* OpenMP UDRs need to be parsed before all other functions. */
22889 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22890 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22891 cp_parser_late_parsing_for_member (parser, decl);
22892 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22893 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22894 cp_parser_late_parsing_for_member (parser, decl);
22896 else
22897 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22898 cp_parser_late_parsing_for_member (parser, decl);
22899 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22901 else
22902 vec_safe_push (unparsed_classes, type);
22904 /* Put back any saved access checks. */
22905 pop_deferring_access_checks ();
22907 /* Restore saved state. */
22908 parser->in_switch_statement_p = in_switch_statement_p;
22909 parser->in_statement = in_statement;
22910 parser->in_function_body = saved_in_function_body;
22911 parser->num_template_parameter_lists
22912 = saved_num_template_parameter_lists;
22913 parser->in_unbraced_linkage_specification_p
22914 = saved_in_unbraced_linkage_specification_p;
22916 return type;
22919 static tree
22920 cp_parser_class_specifier (cp_parser* parser)
22922 tree ret;
22923 timevar_push (TV_PARSE_STRUCT);
22924 ret = cp_parser_class_specifier_1 (parser);
22925 timevar_pop (TV_PARSE_STRUCT);
22926 return ret;
22929 /* Parse a class-head.
22931 class-head:
22932 class-key identifier [opt] base-clause [opt]
22933 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22934 class-key nested-name-specifier [opt] template-id
22935 base-clause [opt]
22937 class-virt-specifier:
22938 final
22940 GNU Extensions:
22941 class-key attributes identifier [opt] base-clause [opt]
22942 class-key attributes nested-name-specifier identifier base-clause [opt]
22943 class-key attributes nested-name-specifier [opt] template-id
22944 base-clause [opt]
22946 Upon return BASES is initialized to the list of base classes (or
22947 NULL, if there are none) in the same form returned by
22948 cp_parser_base_clause.
22950 Returns the TYPE of the indicated class. Sets
22951 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22952 involving a nested-name-specifier was used, and FALSE otherwise.
22954 Returns error_mark_node if this is not a class-head.
22956 Returns NULL_TREE if the class-head is syntactically valid, but
22957 semantically invalid in a way that means we should skip the entire
22958 body of the class. */
22960 static tree
22961 cp_parser_class_head (cp_parser* parser,
22962 bool* nested_name_specifier_p)
22964 tree nested_name_specifier;
22965 enum tag_types class_key;
22966 tree id = NULL_TREE;
22967 tree type = NULL_TREE;
22968 tree attributes;
22969 tree bases;
22970 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22971 bool template_id_p = false;
22972 bool qualified_p = false;
22973 bool invalid_nested_name_p = false;
22974 bool invalid_explicit_specialization_p = false;
22975 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22976 tree pushed_scope = NULL_TREE;
22977 unsigned num_templates;
22978 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22979 /* Assume no nested-name-specifier will be present. */
22980 *nested_name_specifier_p = false;
22981 /* Assume no template parameter lists will be used in defining the
22982 type. */
22983 num_templates = 0;
22984 parser->colon_corrects_to_scope_p = false;
22986 /* Look for the class-key. */
22987 class_key = cp_parser_class_key (parser);
22988 if (class_key == none_type)
22989 return error_mark_node;
22991 location_t class_head_start_location = input_location;
22993 /* Parse the attributes. */
22994 attributes = cp_parser_attributes_opt (parser);
22996 /* If the next token is `::', that is invalid -- but sometimes
22997 people do try to write:
22999 struct ::S {};
23001 Handle this gracefully by accepting the extra qualifier, and then
23002 issuing an error about it later if this really is a
23003 class-head. If it turns out just to be an elaborated type
23004 specifier, remain silent. */
23005 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23006 qualified_p = true;
23008 push_deferring_access_checks (dk_no_check);
23010 /* Determine the name of the class. Begin by looking for an
23011 optional nested-name-specifier. */
23012 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23013 nested_name_specifier
23014 = cp_parser_nested_name_specifier_opt (parser,
23015 /*typename_keyword_p=*/false,
23016 /*check_dependency_p=*/false,
23017 /*type_p=*/true,
23018 /*is_declaration=*/false);
23019 /* If there was a nested-name-specifier, then there *must* be an
23020 identifier. */
23022 cp_token *bad_template_keyword = NULL;
23024 if (nested_name_specifier)
23026 type_start_token = cp_lexer_peek_token (parser->lexer);
23027 /* Although the grammar says `identifier', it really means
23028 `class-name' or `template-name'. You are only allowed to
23029 define a class that has already been declared with this
23030 syntax.
23032 The proposed resolution for Core Issue 180 says that wherever
23033 you see `class T::X' you should treat `X' as a type-name.
23035 It is OK to define an inaccessible class; for example:
23037 class A { class B; };
23038 class A::B {};
23040 We do not know if we will see a class-name, or a
23041 template-name. We look for a class-name first, in case the
23042 class-name is a template-id; if we looked for the
23043 template-name first we would stop after the template-name. */
23044 cp_parser_parse_tentatively (parser);
23045 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23046 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23047 type = cp_parser_class_name (parser,
23048 /*typename_keyword_p=*/false,
23049 /*template_keyword_p=*/false,
23050 class_type,
23051 /*check_dependency_p=*/false,
23052 /*class_head_p=*/true,
23053 /*is_declaration=*/false);
23054 /* If that didn't work, ignore the nested-name-specifier. */
23055 if (!cp_parser_parse_definitely (parser))
23057 invalid_nested_name_p = true;
23058 type_start_token = cp_lexer_peek_token (parser->lexer);
23059 id = cp_parser_identifier (parser);
23060 if (id == error_mark_node)
23061 id = NULL_TREE;
23063 /* If we could not find a corresponding TYPE, treat this
23064 declaration like an unqualified declaration. */
23065 if (type == error_mark_node)
23066 nested_name_specifier = NULL_TREE;
23067 /* Otherwise, count the number of templates used in TYPE and its
23068 containing scopes. */
23069 else
23070 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23072 /* Otherwise, the identifier is optional. */
23073 else
23075 /* We don't know whether what comes next is a template-id,
23076 an identifier, or nothing at all. */
23077 cp_parser_parse_tentatively (parser);
23078 /* Check for a template-id. */
23079 type_start_token = cp_lexer_peek_token (parser->lexer);
23080 id = cp_parser_template_id (parser,
23081 /*template_keyword_p=*/false,
23082 /*check_dependency_p=*/true,
23083 class_key,
23084 /*is_declaration=*/true);
23085 /* If that didn't work, it could still be an identifier. */
23086 if (!cp_parser_parse_definitely (parser))
23088 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23090 type_start_token = cp_lexer_peek_token (parser->lexer);
23091 id = cp_parser_identifier (parser);
23093 else
23094 id = NULL_TREE;
23096 else
23098 template_id_p = true;
23099 ++num_templates;
23103 pop_deferring_access_checks ();
23105 if (id)
23107 cp_parser_check_for_invalid_template_id (parser, id,
23108 class_key,
23109 type_start_token->location);
23111 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23113 /* If it's not a `:' or a `{' then we can't really be looking at a
23114 class-head, since a class-head only appears as part of a
23115 class-specifier. We have to detect this situation before calling
23116 xref_tag, since that has irreversible side-effects. */
23117 if (!cp_parser_next_token_starts_class_definition_p (parser))
23119 cp_parser_error (parser, "expected %<{%> or %<:%>");
23120 type = error_mark_node;
23121 goto out;
23124 /* At this point, we're going ahead with the class-specifier, even
23125 if some other problem occurs. */
23126 cp_parser_commit_to_tentative_parse (parser);
23127 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23129 cp_parser_error (parser,
23130 "cannot specify %<override%> for a class");
23131 type = error_mark_node;
23132 goto out;
23134 /* Issue the error about the overly-qualified name now. */
23135 if (qualified_p)
23137 cp_parser_error (parser,
23138 "global qualification of class name is invalid");
23139 type = error_mark_node;
23140 goto out;
23142 else if (invalid_nested_name_p)
23144 cp_parser_error (parser,
23145 "qualified name does not name a class");
23146 type = error_mark_node;
23147 goto out;
23149 else if (nested_name_specifier)
23151 tree scope;
23153 if (bad_template_keyword)
23154 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23155 keyword template shall not appear at the top level. */
23156 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23157 "keyword %<template%> not allowed in class-head-name");
23159 /* Reject typedef-names in class heads. */
23160 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23162 error_at (type_start_token->location,
23163 "invalid class name in declaration of %qD",
23164 type);
23165 type = NULL_TREE;
23166 goto done;
23169 /* Figure out in what scope the declaration is being placed. */
23170 scope = current_scope ();
23171 /* If that scope does not contain the scope in which the
23172 class was originally declared, the program is invalid. */
23173 if (scope && !is_ancestor (scope, nested_name_specifier))
23175 if (at_namespace_scope_p ())
23176 error_at (type_start_token->location,
23177 "declaration of %qD in namespace %qD which does not "
23178 "enclose %qD",
23179 type, scope, nested_name_specifier);
23180 else
23181 error_at (type_start_token->location,
23182 "declaration of %qD in %qD which does not enclose %qD",
23183 type, scope, nested_name_specifier);
23184 type = NULL_TREE;
23185 goto done;
23187 /* [dcl.meaning]
23189 A declarator-id shall not be qualified except for the
23190 definition of a ... nested class outside of its class
23191 ... [or] the definition or explicit instantiation of a
23192 class member of a namespace outside of its namespace. */
23193 if (scope == nested_name_specifier)
23195 permerror (nested_name_specifier_token_start->location,
23196 "extra qualification not allowed");
23197 nested_name_specifier = NULL_TREE;
23198 num_templates = 0;
23201 /* An explicit-specialization must be preceded by "template <>". If
23202 it is not, try to recover gracefully. */
23203 if (at_namespace_scope_p ()
23204 && parser->num_template_parameter_lists == 0
23205 && !processing_template_parmlist
23206 && template_id_p)
23208 /* Build a location of this form:
23209 struct typename <ARGS>
23210 ^~~~~~~~~~~~~~~~~~~~~~
23211 with caret==start at the start token, and
23212 finishing at the end of the type. */
23213 location_t reported_loc
23214 = make_location (class_head_start_location,
23215 class_head_start_location,
23216 get_finish (type_start_token->location));
23217 rich_location richloc (line_table, reported_loc);
23218 richloc.add_fixit_insert_before (class_head_start_location,
23219 "template <> ");
23220 error_at (&richloc,
23221 "an explicit specialization must be preceded by"
23222 " %<template <>%>");
23223 invalid_explicit_specialization_p = true;
23224 /* Take the same action that would have been taken by
23225 cp_parser_explicit_specialization. */
23226 ++parser->num_template_parameter_lists;
23227 begin_specialization ();
23229 /* There must be no "return" statements between this point and the
23230 end of this function; set "type "to the correct return value and
23231 use "goto done;" to return. */
23232 /* Make sure that the right number of template parameters were
23233 present. */
23234 if (!cp_parser_check_template_parameters (parser, num_templates,
23235 template_id_p,
23236 type_start_token->location,
23237 /*declarator=*/NULL))
23239 /* If something went wrong, there is no point in even trying to
23240 process the class-definition. */
23241 type = NULL_TREE;
23242 goto done;
23245 /* Look up the type. */
23246 if (template_id_p)
23248 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23249 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23250 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23252 error_at (type_start_token->location,
23253 "function template %qD redeclared as a class template", id);
23254 type = error_mark_node;
23256 else
23258 type = TREE_TYPE (id);
23259 type = maybe_process_partial_specialization (type);
23261 /* Check the scope while we still know whether or not we had a
23262 nested-name-specifier. */
23263 if (type != error_mark_node)
23264 check_unqualified_spec_or_inst (type, type_start_token->location);
23266 if (nested_name_specifier)
23267 pushed_scope = push_scope (nested_name_specifier);
23269 else if (nested_name_specifier)
23271 tree class_type;
23273 /* Given:
23275 template <typename T> struct S { struct T };
23276 template <typename T> struct S<T>::T { };
23278 we will get a TYPENAME_TYPE when processing the definition of
23279 `S::T'. We need to resolve it to the actual type before we
23280 try to define it. */
23281 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23283 class_type = resolve_typename_type (TREE_TYPE (type),
23284 /*only_current_p=*/false);
23285 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23286 type = TYPE_NAME (class_type);
23287 else
23289 cp_parser_error (parser, "could not resolve typename type");
23290 type = error_mark_node;
23294 if (maybe_process_partial_specialization (TREE_TYPE (type))
23295 == error_mark_node)
23297 type = NULL_TREE;
23298 goto done;
23301 class_type = current_class_type;
23302 /* Enter the scope indicated by the nested-name-specifier. */
23303 pushed_scope = push_scope (nested_name_specifier);
23304 /* Get the canonical version of this type. */
23305 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23306 /* Call push_template_decl if it seems like we should be defining a
23307 template either from the template headers or the type we're
23308 defining, so that we diagnose both extra and missing headers. */
23309 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23310 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23311 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23313 type = push_template_decl (type);
23314 if (type == error_mark_node)
23316 type = NULL_TREE;
23317 goto done;
23321 type = TREE_TYPE (type);
23322 *nested_name_specifier_p = true;
23324 else /* The name is not a nested name. */
23326 /* If the class was unnamed, create a dummy name. */
23327 if (!id)
23328 id = make_anon_name ();
23329 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23330 ? ts_within_enclosing_non_class
23331 : ts_current);
23332 type = xref_tag (class_key, id, tag_scope,
23333 parser->num_template_parameter_lists);
23336 /* Indicate whether this class was declared as a `class' or as a
23337 `struct'. */
23338 if (TREE_CODE (type) == RECORD_TYPE)
23339 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23340 cp_parser_check_class_key (class_key, type);
23342 /* If this type was already complete, and we see another definition,
23343 that's an error. */
23344 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23346 error_at (type_start_token->location, "redefinition of %q#T",
23347 type);
23348 inform (location_of (type), "previous definition of %q#T",
23349 type);
23350 type = NULL_TREE;
23351 goto done;
23353 else if (type == error_mark_node)
23354 type = NULL_TREE;
23356 if (type)
23358 /* Apply attributes now, before any use of the class as a template
23359 argument in its base list. */
23360 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23361 fixup_attribute_variants (type);
23364 /* We will have entered the scope containing the class; the names of
23365 base classes should be looked up in that context. For example:
23367 struct A { struct B {}; struct C; };
23368 struct A::C : B {};
23370 is valid. */
23372 /* Get the list of base-classes, if there is one. */
23373 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23375 /* PR59482: enter the class scope so that base-specifiers are looked
23376 up correctly. */
23377 if (type)
23378 pushclass (type);
23379 bases = cp_parser_base_clause (parser);
23380 /* PR59482: get out of the previously pushed class scope so that the
23381 subsequent pops pop the right thing. */
23382 if (type)
23383 popclass ();
23385 else
23386 bases = NULL_TREE;
23388 /* If we're really defining a class, process the base classes.
23389 If they're invalid, fail. */
23390 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23391 xref_basetypes (type, bases);
23393 done:
23394 /* Leave the scope given by the nested-name-specifier. We will
23395 enter the class scope itself while processing the members. */
23396 if (pushed_scope)
23397 pop_scope (pushed_scope);
23399 if (invalid_explicit_specialization_p)
23401 end_specialization ();
23402 --parser->num_template_parameter_lists;
23405 if (type)
23406 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23407 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23408 CLASSTYPE_FINAL (type) = 1;
23409 out:
23410 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23411 return type;
23414 /* Parse a class-key.
23416 class-key:
23417 class
23418 struct
23419 union
23421 Returns the kind of class-key specified, or none_type to indicate
23422 error. */
23424 static enum tag_types
23425 cp_parser_class_key (cp_parser* parser)
23427 cp_token *token;
23428 enum tag_types tag_type;
23430 /* Look for the class-key. */
23431 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23432 if (!token)
23433 return none_type;
23435 /* Check to see if the TOKEN is a class-key. */
23436 tag_type = cp_parser_token_is_class_key (token);
23437 if (!tag_type)
23438 cp_parser_error (parser, "expected class-key");
23439 return tag_type;
23442 /* Parse a type-parameter-key.
23444 type-parameter-key:
23445 class
23446 typename
23449 static void
23450 cp_parser_type_parameter_key (cp_parser* parser)
23452 /* Look for the type-parameter-key. */
23453 enum tag_types tag_type = none_type;
23454 cp_token *token = cp_lexer_peek_token (parser->lexer);
23455 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23457 cp_lexer_consume_token (parser->lexer);
23458 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23459 /* typename is not allowed in a template template parameter
23460 by the standard until C++17. */
23461 pedwarn (token->location, OPT_Wpedantic,
23462 "ISO C++ forbids typename key in template template parameter;"
23463 " use -std=c++17 or -std=gnu++17");
23465 else
23466 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23468 return;
23471 /* Parse an (optional) member-specification.
23473 member-specification:
23474 member-declaration member-specification [opt]
23475 access-specifier : member-specification [opt] */
23477 static void
23478 cp_parser_member_specification_opt (cp_parser* parser)
23480 while (true)
23482 cp_token *token;
23483 enum rid keyword;
23485 /* Peek at the next token. */
23486 token = cp_lexer_peek_token (parser->lexer);
23487 /* If it's a `}', or EOF then we've seen all the members. */
23488 if (token->type == CPP_CLOSE_BRACE
23489 || token->type == CPP_EOF
23490 || token->type == CPP_PRAGMA_EOL)
23491 break;
23493 /* See if this token is a keyword. */
23494 keyword = token->keyword;
23495 switch (keyword)
23497 case RID_PUBLIC:
23498 case RID_PROTECTED:
23499 case RID_PRIVATE:
23500 /* Consume the access-specifier. */
23501 cp_lexer_consume_token (parser->lexer);
23502 /* Remember which access-specifier is active. */
23503 current_access_specifier = token->u.value;
23504 /* Look for the `:'. */
23505 cp_parser_require (parser, CPP_COLON, RT_COLON);
23506 break;
23508 default:
23509 /* Accept #pragmas at class scope. */
23510 if (token->type == CPP_PRAGMA)
23512 cp_parser_pragma (parser, pragma_member, NULL);
23513 break;
23516 /* Otherwise, the next construction must be a
23517 member-declaration. */
23518 cp_parser_member_declaration (parser);
23523 /* Parse a member-declaration.
23525 member-declaration:
23526 decl-specifier-seq [opt] member-declarator-list [opt] ;
23527 function-definition ; [opt]
23528 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23529 using-declaration
23530 template-declaration
23531 alias-declaration
23533 member-declarator-list:
23534 member-declarator
23535 member-declarator-list , member-declarator
23537 member-declarator:
23538 declarator pure-specifier [opt]
23539 declarator constant-initializer [opt]
23540 identifier [opt] : constant-expression
23542 GNU Extensions:
23544 member-declaration:
23545 __extension__ member-declaration
23547 member-declarator:
23548 declarator attributes [opt] pure-specifier [opt]
23549 declarator attributes [opt] constant-initializer [opt]
23550 identifier [opt] attributes [opt] : constant-expression
23552 C++0x Extensions:
23554 member-declaration:
23555 static_assert-declaration */
23557 static void
23558 cp_parser_member_declaration (cp_parser* parser)
23560 cp_decl_specifier_seq decl_specifiers;
23561 tree prefix_attributes;
23562 tree decl;
23563 int declares_class_or_enum;
23564 bool friend_p;
23565 cp_token *token = NULL;
23566 cp_token *decl_spec_token_start = NULL;
23567 cp_token *initializer_token_start = NULL;
23568 int saved_pedantic;
23569 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23571 /* Check for the `__extension__' keyword. */
23572 if (cp_parser_extension_opt (parser, &saved_pedantic))
23574 /* Recurse. */
23575 cp_parser_member_declaration (parser);
23576 /* Restore the old value of the PEDANTIC flag. */
23577 pedantic = saved_pedantic;
23579 return;
23582 /* Check for a template-declaration. */
23583 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23585 /* An explicit specialization here is an error condition, and we
23586 expect the specialization handler to detect and report this. */
23587 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23588 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23589 cp_parser_explicit_specialization (parser);
23590 else
23591 cp_parser_template_declaration (parser, /*member_p=*/true);
23593 return;
23595 /* Check for a template introduction. */
23596 else if (cp_parser_template_declaration_after_export (parser, true))
23597 return;
23599 /* Check for a using-declaration. */
23600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23602 if (cxx_dialect < cxx11)
23604 /* Parse the using-declaration. */
23605 cp_parser_using_declaration (parser,
23606 /*access_declaration_p=*/false);
23607 return;
23609 else
23611 tree decl;
23612 bool alias_decl_expected;
23613 cp_parser_parse_tentatively (parser);
23614 decl = cp_parser_alias_declaration (parser);
23615 /* Note that if we actually see the '=' token after the
23616 identifier, cp_parser_alias_declaration commits the
23617 tentative parse. In that case, we really expect an
23618 alias-declaration. Otherwise, we expect a using
23619 declaration. */
23620 alias_decl_expected =
23621 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23622 cp_parser_parse_definitely (parser);
23624 if (alias_decl_expected)
23625 finish_member_declaration (decl);
23626 else
23627 cp_parser_using_declaration (parser,
23628 /*access_declaration_p=*/false);
23629 return;
23633 /* Check for @defs. */
23634 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23636 tree ivar, member;
23637 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23638 ivar = ivar_chains;
23639 while (ivar)
23641 member = ivar;
23642 ivar = TREE_CHAIN (member);
23643 TREE_CHAIN (member) = NULL_TREE;
23644 finish_member_declaration (member);
23646 return;
23649 /* If the next token is `static_assert' we have a static assertion. */
23650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23652 cp_parser_static_assert (parser, /*member_p=*/true);
23653 return;
23656 parser->colon_corrects_to_scope_p = false;
23658 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23659 goto out;
23661 /* Parse the decl-specifier-seq. */
23662 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23663 cp_parser_decl_specifier_seq (parser,
23664 CP_PARSER_FLAGS_OPTIONAL,
23665 &decl_specifiers,
23666 &declares_class_or_enum);
23667 /* Check for an invalid type-name. */
23668 if (!decl_specifiers.any_type_specifiers_p
23669 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23670 goto out;
23671 /* If there is no declarator, then the decl-specifier-seq should
23672 specify a type. */
23673 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23675 /* If there was no decl-specifier-seq, and the next token is a
23676 `;', then we have something like:
23678 struct S { ; };
23680 [class.mem]
23682 Each member-declaration shall declare at least one member
23683 name of the class. */
23684 if (!decl_specifiers.any_specifiers_p)
23686 cp_token *token = cp_lexer_peek_token (parser->lexer);
23687 if (!in_system_header_at (token->location))
23689 gcc_rich_location richloc (token->location);
23690 richloc.add_fixit_remove ();
23691 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23694 else
23696 tree type;
23698 /* See if this declaration is a friend. */
23699 friend_p = cp_parser_friend_p (&decl_specifiers);
23700 /* If there were decl-specifiers, check to see if there was
23701 a class-declaration. */
23702 type = check_tag_decl (&decl_specifiers,
23703 /*explicit_type_instantiation_p=*/false);
23704 /* Nested classes have already been added to the class, but
23705 a `friend' needs to be explicitly registered. */
23706 if (friend_p)
23708 /* If the `friend' keyword was present, the friend must
23709 be introduced with a class-key. */
23710 if (!declares_class_or_enum && cxx_dialect < cxx11)
23711 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23712 "in C++03 a class-key must be used "
23713 "when declaring a friend");
23714 /* In this case:
23716 template <typename T> struct A {
23717 friend struct A<T>::B;
23720 A<T>::B will be represented by a TYPENAME_TYPE, and
23721 therefore not recognized by check_tag_decl. */
23722 if (!type)
23724 type = decl_specifiers.type;
23725 if (type && TREE_CODE (type) == TYPE_DECL)
23726 type = TREE_TYPE (type);
23728 if (!type || !TYPE_P (type))
23729 error_at (decl_spec_token_start->location,
23730 "friend declaration does not name a class or "
23731 "function");
23732 else
23733 make_friend_class (current_class_type, type,
23734 /*complain=*/true);
23736 /* If there is no TYPE, an error message will already have
23737 been issued. */
23738 else if (!type || type == error_mark_node)
23740 /* An anonymous aggregate has to be handled specially; such
23741 a declaration really declares a data member (with a
23742 particular type), as opposed to a nested class. */
23743 else if (ANON_AGGR_TYPE_P (type))
23745 /* C++11 9.5/6. */
23746 if (decl_specifiers.storage_class != sc_none)
23747 error_at (decl_spec_token_start->location,
23748 "a storage class on an anonymous aggregate "
23749 "in class scope is not allowed");
23751 /* Remove constructors and such from TYPE, now that we
23752 know it is an anonymous aggregate. */
23753 fixup_anonymous_aggr (type);
23754 /* And make the corresponding data member. */
23755 decl = build_decl (decl_spec_token_start->location,
23756 FIELD_DECL, NULL_TREE, type);
23757 /* Add it to the class. */
23758 finish_member_declaration (decl);
23760 else
23761 cp_parser_check_access_in_redeclaration
23762 (TYPE_NAME (type),
23763 decl_spec_token_start->location);
23766 else
23768 bool assume_semicolon = false;
23770 /* Clear attributes from the decl_specifiers but keep them
23771 around as prefix attributes that apply them to the entity
23772 being declared. */
23773 prefix_attributes = decl_specifiers.attributes;
23774 decl_specifiers.attributes = NULL_TREE;
23776 /* See if these declarations will be friends. */
23777 friend_p = cp_parser_friend_p (&decl_specifiers);
23779 /* Keep going until we hit the `;' at the end of the
23780 declaration. */
23781 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23783 tree attributes = NULL_TREE;
23784 tree first_attribute;
23785 tree initializer;
23786 bool named_bitfld = false;
23788 /* Peek at the next token. */
23789 token = cp_lexer_peek_token (parser->lexer);
23791 /* The following code wants to know early if it is a bit-field
23792 or some other declaration. Attributes can appear before
23793 the `:' token. Skip over them without consuming any tokens
23794 to peek if they are followed by `:'. */
23795 if (cp_next_tokens_can_be_attribute_p (parser)
23796 || (token->type == CPP_NAME
23797 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23798 && (named_bitfld = true)))
23800 size_t n
23801 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23802 token = cp_lexer_peek_nth_token (parser->lexer, n);
23805 /* Check for a bitfield declaration. */
23806 if (token->type == CPP_COLON
23807 || (token->type == CPP_NAME
23808 && token == cp_lexer_peek_token (parser->lexer)
23809 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23810 && (named_bitfld = true)))
23812 tree identifier;
23813 tree width;
23814 tree late_attributes = NULL_TREE;
23816 if (named_bitfld)
23817 identifier = cp_parser_identifier (parser);
23818 else
23819 identifier = NULL_TREE;
23821 /* Look for attributes that apply to the bitfield. */
23822 attributes = cp_parser_attributes_opt (parser);
23824 /* Consume the `:' token. */
23825 cp_lexer_consume_token (parser->lexer);
23827 /* Get the width of the bitfield. */
23828 width = cp_parser_constant_expression (parser, false, NULL,
23829 cxx_dialect >= cxx11);
23831 /* In C++2A and as extension for C++11 and above we allow
23832 default member initializers for bit-fields. */
23833 initializer = NULL_TREE;
23834 if (cxx_dialect >= cxx11
23835 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23836 || cp_lexer_next_token_is (parser->lexer,
23837 CPP_OPEN_BRACE)))
23839 location_t loc
23840 = cp_lexer_peek_token (parser->lexer)->location;
23841 if (cxx_dialect < cxx2a
23842 && !in_system_header_at (loc)
23843 && identifier != NULL_TREE)
23844 pedwarn (loc, 0,
23845 "default member initializers for bit-fields "
23846 "only available with -std=c++2a or "
23847 "-std=gnu++2a");
23849 initializer = cp_parser_save_nsdmi (parser);
23850 if (identifier == NULL_TREE)
23852 error_at (loc, "default member initializer for "
23853 "unnamed bit-field");
23854 initializer = NULL_TREE;
23857 else
23859 /* Look for attributes that apply to the bitfield after
23860 the `:' token and width. This is where GCC used to
23861 parse attributes in the past, pedwarn if there is
23862 a std attribute. */
23863 if (cp_next_tokens_can_be_std_attribute_p (parser))
23864 pedwarn (input_location, OPT_Wpedantic,
23865 "ISO C++ allows bit-field attributes only "
23866 "before the %<:%> token");
23868 late_attributes = cp_parser_attributes_opt (parser);
23871 attributes = attr_chainon (attributes, late_attributes);
23873 /* Remember which attributes are prefix attributes and
23874 which are not. */
23875 first_attribute = attributes;
23876 /* Combine the attributes. */
23877 attributes = attr_chainon (prefix_attributes, attributes);
23879 /* Create the bitfield declaration. */
23880 decl = grokbitfield (identifier
23881 ? make_id_declarator (NULL_TREE,
23882 identifier,
23883 sfk_none)
23884 : NULL,
23885 &decl_specifiers,
23886 width, initializer,
23887 attributes);
23889 else
23891 cp_declarator *declarator;
23892 tree asm_specification;
23893 int ctor_dtor_or_conv_p;
23895 /* Parse the declarator. */
23896 declarator
23897 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23898 &ctor_dtor_or_conv_p,
23899 /*parenthesized_p=*/NULL,
23900 /*member_p=*/true,
23901 friend_p);
23903 /* If something went wrong parsing the declarator, make sure
23904 that we at least consume some tokens. */
23905 if (declarator == cp_error_declarator)
23907 /* Skip to the end of the statement. */
23908 cp_parser_skip_to_end_of_statement (parser);
23909 /* If the next token is not a semicolon, that is
23910 probably because we just skipped over the body of
23911 a function. So, we consume a semicolon if
23912 present, but do not issue an error message if it
23913 is not present. */
23914 if (cp_lexer_next_token_is (parser->lexer,
23915 CPP_SEMICOLON))
23916 cp_lexer_consume_token (parser->lexer);
23917 goto out;
23920 if (declares_class_or_enum & 2)
23921 cp_parser_check_for_definition_in_return_type
23922 (declarator, decl_specifiers.type,
23923 decl_specifiers.locations[ds_type_spec]);
23925 /* Look for an asm-specification. */
23926 asm_specification = cp_parser_asm_specification_opt (parser);
23927 /* Look for attributes that apply to the declaration. */
23928 attributes = cp_parser_attributes_opt (parser);
23929 /* Remember which attributes are prefix attributes and
23930 which are not. */
23931 first_attribute = attributes;
23932 /* Combine the attributes. */
23933 attributes = attr_chainon (prefix_attributes, attributes);
23935 /* If it's an `=', then we have a constant-initializer or a
23936 pure-specifier. It is not correct to parse the
23937 initializer before registering the member declaration
23938 since the member declaration should be in scope while
23939 its initializer is processed. However, the rest of the
23940 front end does not yet provide an interface that allows
23941 us to handle this correctly. */
23942 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23944 /* In [class.mem]:
23946 A pure-specifier shall be used only in the declaration of
23947 a virtual function.
23949 A member-declarator can contain a constant-initializer
23950 only if it declares a static member of integral or
23951 enumeration type.
23953 Therefore, if the DECLARATOR is for a function, we look
23954 for a pure-specifier; otherwise, we look for a
23955 constant-initializer. When we call `grokfield', it will
23956 perform more stringent semantics checks. */
23957 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23958 if (function_declarator_p (declarator)
23959 || (decl_specifiers.type
23960 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23961 && declarator->kind == cdk_id
23962 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23963 == FUNCTION_TYPE)))
23964 initializer = cp_parser_pure_specifier (parser);
23965 else if (decl_specifiers.storage_class != sc_static)
23966 initializer = cp_parser_save_nsdmi (parser);
23967 else if (cxx_dialect >= cxx11)
23969 bool nonconst;
23970 /* Don't require a constant rvalue in C++11, since we
23971 might want a reference constant. We'll enforce
23972 constancy later. */
23973 cp_lexer_consume_token (parser->lexer);
23974 /* Parse the initializer. */
23975 initializer = cp_parser_initializer_clause (parser,
23976 &nonconst);
23978 else
23979 /* Parse the initializer. */
23980 initializer = cp_parser_constant_initializer (parser);
23982 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23983 && !function_declarator_p (declarator))
23985 bool x;
23986 if (decl_specifiers.storage_class != sc_static)
23987 initializer = cp_parser_save_nsdmi (parser);
23988 else
23989 initializer = cp_parser_initializer (parser, &x, &x);
23991 /* Otherwise, there is no initializer. */
23992 else
23993 initializer = NULL_TREE;
23995 /* See if we are probably looking at a function
23996 definition. We are certainly not looking at a
23997 member-declarator. Calling `grokfield' has
23998 side-effects, so we must not do it unless we are sure
23999 that we are looking at a member-declarator. */
24000 if (cp_parser_token_starts_function_definition_p
24001 (cp_lexer_peek_token (parser->lexer)))
24003 /* The grammar does not allow a pure-specifier to be
24004 used when a member function is defined. (It is
24005 possible that this fact is an oversight in the
24006 standard, since a pure function may be defined
24007 outside of the class-specifier. */
24008 if (initializer && initializer_token_start)
24009 error_at (initializer_token_start->location,
24010 "pure-specifier on function-definition");
24011 decl = cp_parser_save_member_function_body (parser,
24012 &decl_specifiers,
24013 declarator,
24014 attributes);
24015 if (parser->fully_implicit_function_template_p)
24016 decl = finish_fully_implicit_template (parser, decl);
24017 /* If the member was not a friend, declare it here. */
24018 if (!friend_p)
24019 finish_member_declaration (decl);
24020 /* Peek at the next token. */
24021 token = cp_lexer_peek_token (parser->lexer);
24022 /* If the next token is a semicolon, consume it. */
24023 if (token->type == CPP_SEMICOLON)
24025 location_t semicolon_loc
24026 = cp_lexer_consume_token (parser->lexer)->location;
24027 gcc_rich_location richloc (semicolon_loc);
24028 richloc.add_fixit_remove ();
24029 warning_at (&richloc, OPT_Wextra_semi,
24030 "extra %<;%> after in-class "
24031 "function definition");
24033 goto out;
24035 else
24036 if (declarator->kind == cdk_function)
24037 declarator->id_loc = token->location;
24038 /* Create the declaration. */
24039 decl = grokfield (declarator, &decl_specifiers,
24040 initializer, /*init_const_expr_p=*/true,
24041 asm_specification, attributes);
24042 if (parser->fully_implicit_function_template_p)
24044 if (friend_p)
24045 finish_fully_implicit_template (parser, 0);
24046 else
24047 decl = finish_fully_implicit_template (parser, decl);
24051 cp_finalize_omp_declare_simd (parser, decl);
24052 cp_finalize_oacc_routine (parser, decl, false);
24054 /* Reset PREFIX_ATTRIBUTES. */
24055 if (attributes != error_mark_node)
24057 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24058 attributes = TREE_CHAIN (attributes);
24059 if (attributes)
24060 TREE_CHAIN (attributes) = NULL_TREE;
24063 /* If there is any qualification still in effect, clear it
24064 now; we will be starting fresh with the next declarator. */
24065 parser->scope = NULL_TREE;
24066 parser->qualifying_scope = NULL_TREE;
24067 parser->object_scope = NULL_TREE;
24068 /* If it's a `,', then there are more declarators. */
24069 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24071 cp_lexer_consume_token (parser->lexer);
24072 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24074 cp_token *token = cp_lexer_previous_token (parser->lexer);
24075 gcc_rich_location richloc (token->location);
24076 richloc.add_fixit_remove ();
24077 error_at (&richloc, "stray %<,%> at end of "
24078 "member declaration");
24081 /* If the next token isn't a `;', then we have a parse error. */
24082 else if (cp_lexer_next_token_is_not (parser->lexer,
24083 CPP_SEMICOLON))
24085 /* The next token might be a ways away from where the
24086 actual semicolon is missing. Find the previous token
24087 and use that for our error position. */
24088 cp_token *token = cp_lexer_previous_token (parser->lexer);
24089 gcc_rich_location richloc (token->location);
24090 richloc.add_fixit_insert_after (";");
24091 error_at (&richloc, "expected %<;%> at end of "
24092 "member declaration");
24094 /* Assume that the user meant to provide a semicolon. If
24095 we were to cp_parser_skip_to_end_of_statement, we might
24096 skip to a semicolon inside a member function definition
24097 and issue nonsensical error messages. */
24098 assume_semicolon = true;
24101 if (decl)
24103 /* Add DECL to the list of members. */
24104 if (!friend_p
24105 /* Explicitly include, eg, NSDMIs, for better error
24106 recovery (c++/58650). */
24107 || !DECL_DECLARES_FUNCTION_P (decl))
24108 finish_member_declaration (decl);
24110 if (TREE_CODE (decl) == FUNCTION_DECL)
24111 cp_parser_save_default_args (parser, decl);
24112 else if (TREE_CODE (decl) == FIELD_DECL
24113 && DECL_INITIAL (decl))
24114 /* Add DECL to the queue of NSDMI to be parsed later. */
24115 vec_safe_push (unparsed_nsdmis, decl);
24118 if (assume_semicolon)
24119 goto out;
24123 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24124 out:
24125 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24128 /* Parse a pure-specifier.
24130 pure-specifier:
24133 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24134 Otherwise, ERROR_MARK_NODE is returned. */
24136 static tree
24137 cp_parser_pure_specifier (cp_parser* parser)
24139 cp_token *token;
24141 /* Look for the `=' token. */
24142 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24143 return error_mark_node;
24144 /* Look for the `0' token. */
24145 token = cp_lexer_peek_token (parser->lexer);
24147 if (token->type == CPP_EOF
24148 || token->type == CPP_PRAGMA_EOL)
24149 return error_mark_node;
24151 cp_lexer_consume_token (parser->lexer);
24153 /* Accept = default or = delete in c++0x mode. */
24154 if (token->keyword == RID_DEFAULT
24155 || token->keyword == RID_DELETE)
24157 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24158 return token->u.value;
24161 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24162 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24164 cp_parser_error (parser,
24165 "invalid pure specifier (only %<= 0%> is allowed)");
24166 cp_parser_skip_to_end_of_statement (parser);
24167 return error_mark_node;
24169 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24171 error_at (token->location, "templates may not be %<virtual%>");
24172 return error_mark_node;
24175 return integer_zero_node;
24178 /* Parse a constant-initializer.
24180 constant-initializer:
24181 = constant-expression
24183 Returns a representation of the constant-expression. */
24185 static tree
24186 cp_parser_constant_initializer (cp_parser* parser)
24188 /* Look for the `=' token. */
24189 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24190 return error_mark_node;
24192 /* It is invalid to write:
24194 struct S { static const int i = { 7 }; };
24197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24199 cp_parser_error (parser,
24200 "a brace-enclosed initializer is not allowed here");
24201 /* Consume the opening brace. */
24202 matching_braces braces;
24203 braces.consume_open (parser);
24204 /* Skip the initializer. */
24205 cp_parser_skip_to_closing_brace (parser);
24206 /* Look for the trailing `}'. */
24207 braces.require_close (parser);
24209 return error_mark_node;
24212 return cp_parser_constant_expression (parser);
24215 /* Derived classes [gram.class.derived] */
24217 /* Parse a base-clause.
24219 base-clause:
24220 : base-specifier-list
24222 base-specifier-list:
24223 base-specifier ... [opt]
24224 base-specifier-list , base-specifier ... [opt]
24226 Returns a TREE_LIST representing the base-classes, in the order in
24227 which they were declared. The representation of each node is as
24228 described by cp_parser_base_specifier.
24230 In the case that no bases are specified, this function will return
24231 NULL_TREE, not ERROR_MARK_NODE. */
24233 static tree
24234 cp_parser_base_clause (cp_parser* parser)
24236 tree bases = NULL_TREE;
24238 /* Look for the `:' that begins the list. */
24239 cp_parser_require (parser, CPP_COLON, RT_COLON);
24241 /* Scan the base-specifier-list. */
24242 while (true)
24244 cp_token *token;
24245 tree base;
24246 bool pack_expansion_p = false;
24248 /* Look for the base-specifier. */
24249 base = cp_parser_base_specifier (parser);
24250 /* Look for the (optional) ellipsis. */
24251 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24253 /* Consume the `...'. */
24254 cp_lexer_consume_token (parser->lexer);
24256 pack_expansion_p = true;
24259 /* Add BASE to the front of the list. */
24260 if (base && base != error_mark_node)
24262 if (pack_expansion_p)
24263 /* Make this a pack expansion type. */
24264 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24266 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24268 TREE_CHAIN (base) = bases;
24269 bases = base;
24272 /* Peek at the next token. */
24273 token = cp_lexer_peek_token (parser->lexer);
24274 /* If it's not a comma, then the list is complete. */
24275 if (token->type != CPP_COMMA)
24276 break;
24277 /* Consume the `,'. */
24278 cp_lexer_consume_token (parser->lexer);
24281 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24282 base class had a qualified name. However, the next name that
24283 appears is certainly not qualified. */
24284 parser->scope = NULL_TREE;
24285 parser->qualifying_scope = NULL_TREE;
24286 parser->object_scope = NULL_TREE;
24288 return nreverse (bases);
24291 /* Parse a base-specifier.
24293 base-specifier:
24294 :: [opt] nested-name-specifier [opt] class-name
24295 virtual access-specifier [opt] :: [opt] nested-name-specifier
24296 [opt] class-name
24297 access-specifier virtual [opt] :: [opt] nested-name-specifier
24298 [opt] class-name
24300 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24301 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24302 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24303 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24305 static tree
24306 cp_parser_base_specifier (cp_parser* parser)
24308 cp_token *token;
24309 bool done = false;
24310 bool virtual_p = false;
24311 bool duplicate_virtual_error_issued_p = false;
24312 bool duplicate_access_error_issued_p = false;
24313 bool class_scope_p, template_p;
24314 tree access = access_default_node;
24315 tree type;
24317 /* Process the optional `virtual' and `access-specifier'. */
24318 while (!done)
24320 /* Peek at the next token. */
24321 token = cp_lexer_peek_token (parser->lexer);
24322 /* Process `virtual'. */
24323 switch (token->keyword)
24325 case RID_VIRTUAL:
24326 /* If `virtual' appears more than once, issue an error. */
24327 if (virtual_p && !duplicate_virtual_error_issued_p)
24329 cp_parser_error (parser,
24330 "%<virtual%> specified more than once in base-specifier");
24331 duplicate_virtual_error_issued_p = true;
24334 virtual_p = true;
24336 /* Consume the `virtual' token. */
24337 cp_lexer_consume_token (parser->lexer);
24339 break;
24341 case RID_PUBLIC:
24342 case RID_PROTECTED:
24343 case RID_PRIVATE:
24344 /* If more than one access specifier appears, issue an
24345 error. */
24346 if (access != access_default_node
24347 && !duplicate_access_error_issued_p)
24349 cp_parser_error (parser,
24350 "more than one access specifier in base-specifier");
24351 duplicate_access_error_issued_p = true;
24354 access = ridpointers[(int) token->keyword];
24356 /* Consume the access-specifier. */
24357 cp_lexer_consume_token (parser->lexer);
24359 break;
24361 default:
24362 done = true;
24363 break;
24366 /* It is not uncommon to see programs mechanically, erroneously, use
24367 the 'typename' keyword to denote (dependent) qualified types
24368 as base classes. */
24369 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24371 token = cp_lexer_peek_token (parser->lexer);
24372 if (!processing_template_decl)
24373 error_at (token->location,
24374 "keyword %<typename%> not allowed outside of templates");
24375 else
24376 error_at (token->location,
24377 "keyword %<typename%> not allowed in this context "
24378 "(the base class is implicitly a type)");
24379 cp_lexer_consume_token (parser->lexer);
24382 /* Look for the optional `::' operator. */
24383 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24384 /* Look for the nested-name-specifier. The simplest way to
24385 implement:
24387 [temp.res]
24389 The keyword `typename' is not permitted in a base-specifier or
24390 mem-initializer; in these contexts a qualified name that
24391 depends on a template-parameter is implicitly assumed to be a
24392 type name.
24394 is to pretend that we have seen the `typename' keyword at this
24395 point. */
24396 cp_parser_nested_name_specifier_opt (parser,
24397 /*typename_keyword_p=*/true,
24398 /*check_dependency_p=*/true,
24399 /*type_p=*/true,
24400 /*is_declaration=*/true);
24401 /* If the base class is given by a qualified name, assume that names
24402 we see are type names or templates, as appropriate. */
24403 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24404 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24406 if (!parser->scope
24407 && cp_lexer_next_token_is_decltype (parser->lexer))
24408 /* DR 950 allows decltype as a base-specifier. */
24409 type = cp_parser_decltype (parser);
24410 else
24412 /* Otherwise, look for the class-name. */
24413 type = cp_parser_class_name (parser,
24414 class_scope_p,
24415 template_p,
24416 typename_type,
24417 /*check_dependency_p=*/true,
24418 /*class_head_p=*/false,
24419 /*is_declaration=*/true);
24420 type = TREE_TYPE (type);
24423 if (type == error_mark_node)
24424 return error_mark_node;
24426 return finish_base_specifier (type, access, virtual_p);
24429 /* Exception handling [gram.exception] */
24431 /* Parse an (optional) noexcept-specification.
24433 noexcept-specification:
24434 noexcept ( constant-expression ) [opt]
24436 If no noexcept-specification is present, returns NULL_TREE.
24437 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24438 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24439 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24440 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24441 in which case a boolean condition is returned instead. */
24443 static tree
24444 cp_parser_noexcept_specification_opt (cp_parser* parser,
24445 bool require_constexpr,
24446 bool* consumed_expr,
24447 bool return_cond)
24449 cp_token *token;
24450 const char *saved_message;
24452 /* Peek at the next token. */
24453 token = cp_lexer_peek_token (parser->lexer);
24455 /* Is it a noexcept-specification? */
24456 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24458 tree expr;
24459 cp_lexer_consume_token (parser->lexer);
24461 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24463 matching_parens parens;
24464 parens.consume_open (parser);
24466 if (require_constexpr)
24468 /* Types may not be defined in an exception-specification. */
24469 saved_message = parser->type_definition_forbidden_message;
24470 parser->type_definition_forbidden_message
24471 = G_("types may not be defined in an exception-specification");
24473 expr = cp_parser_constant_expression (parser);
24475 /* Restore the saved message. */
24476 parser->type_definition_forbidden_message = saved_message;
24478 else
24480 expr = cp_parser_expression (parser);
24481 *consumed_expr = true;
24484 parens.require_close (parser);
24486 else
24488 expr = boolean_true_node;
24489 if (!require_constexpr)
24490 *consumed_expr = false;
24493 /* We cannot build a noexcept-spec right away because this will check
24494 that expr is a constexpr. */
24495 if (!return_cond)
24496 return build_noexcept_spec (expr, tf_warning_or_error);
24497 else
24498 return expr;
24500 else
24501 return NULL_TREE;
24504 /* Parse an (optional) exception-specification.
24506 exception-specification:
24507 throw ( type-id-list [opt] )
24509 Returns a TREE_LIST representing the exception-specification. The
24510 TREE_VALUE of each node is a type. */
24512 static tree
24513 cp_parser_exception_specification_opt (cp_parser* parser)
24515 cp_token *token;
24516 tree type_id_list;
24517 const char *saved_message;
24519 /* Peek at the next token. */
24520 token = cp_lexer_peek_token (parser->lexer);
24522 /* Is it a noexcept-specification? */
24523 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24524 false);
24525 if (type_id_list != NULL_TREE)
24526 return type_id_list;
24528 /* If it's not `throw', then there's no exception-specification. */
24529 if (!cp_parser_is_keyword (token, RID_THROW))
24530 return NULL_TREE;
24532 location_t loc = token->location;
24534 /* Consume the `throw'. */
24535 cp_lexer_consume_token (parser->lexer);
24537 /* Look for the `('. */
24538 matching_parens parens;
24539 parens.require_open (parser);
24541 /* Peek at the next token. */
24542 token = cp_lexer_peek_token (parser->lexer);
24543 /* If it's not a `)', then there is a type-id-list. */
24544 if (token->type != CPP_CLOSE_PAREN)
24546 /* Types may not be defined in an exception-specification. */
24547 saved_message = parser->type_definition_forbidden_message;
24548 parser->type_definition_forbidden_message
24549 = G_("types may not be defined in an exception-specification");
24550 /* Parse the type-id-list. */
24551 type_id_list = cp_parser_type_id_list (parser);
24552 /* Restore the saved message. */
24553 parser->type_definition_forbidden_message = saved_message;
24555 if (cxx_dialect >= cxx17)
24557 error_at (loc, "ISO C++17 does not allow dynamic exception "
24558 "specifications");
24559 type_id_list = NULL_TREE;
24561 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24562 warning_at (loc, OPT_Wdeprecated,
24563 "dynamic exception specifications are deprecated in "
24564 "C++11");
24566 /* In C++17, throw() is equivalent to noexcept (true). throw()
24567 is deprecated in C++11 and above as well, but is still widely used,
24568 so don't warn about it yet. */
24569 else if (cxx_dialect >= cxx17)
24570 type_id_list = noexcept_true_spec;
24571 else
24572 type_id_list = empty_except_spec;
24574 /* Look for the `)'. */
24575 parens.require_close (parser);
24577 return type_id_list;
24580 /* Parse an (optional) type-id-list.
24582 type-id-list:
24583 type-id ... [opt]
24584 type-id-list , type-id ... [opt]
24586 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24587 in the order that the types were presented. */
24589 static tree
24590 cp_parser_type_id_list (cp_parser* parser)
24592 tree types = NULL_TREE;
24594 while (true)
24596 cp_token *token;
24597 tree type;
24599 token = cp_lexer_peek_token (parser->lexer);
24601 /* Get the next type-id. */
24602 type = cp_parser_type_id (parser);
24603 /* Check for invalid 'auto'. */
24604 if (flag_concepts && type_uses_auto (type))
24606 error_at (token->location,
24607 "invalid use of %<auto%> in exception-specification");
24608 type = error_mark_node;
24610 /* Parse the optional ellipsis. */
24611 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24613 /* Consume the `...'. */
24614 cp_lexer_consume_token (parser->lexer);
24616 /* Turn the type into a pack expansion expression. */
24617 type = make_pack_expansion (type);
24619 /* Add it to the list. */
24620 types = add_exception_specifier (types, type, /*complain=*/1);
24621 /* Peek at the next token. */
24622 token = cp_lexer_peek_token (parser->lexer);
24623 /* If it is not a `,', we are done. */
24624 if (token->type != CPP_COMMA)
24625 break;
24626 /* Consume the `,'. */
24627 cp_lexer_consume_token (parser->lexer);
24630 return nreverse (types);
24633 /* Parse a try-block.
24635 try-block:
24636 try compound-statement handler-seq */
24638 static tree
24639 cp_parser_try_block (cp_parser* parser)
24641 tree try_block;
24643 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24644 if (parser->in_function_body
24645 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24646 error ("%<try%> in %<constexpr%> function");
24648 try_block = begin_try_block ();
24649 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24650 finish_try_block (try_block);
24651 cp_parser_handler_seq (parser);
24652 finish_handler_sequence (try_block);
24654 return try_block;
24657 /* Parse a function-try-block.
24659 function-try-block:
24660 try ctor-initializer [opt] function-body handler-seq */
24662 static void
24663 cp_parser_function_try_block (cp_parser* parser)
24665 tree compound_stmt;
24666 tree try_block;
24668 /* Look for the `try' keyword. */
24669 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24670 return;
24671 /* Let the rest of the front end know where we are. */
24672 try_block = begin_function_try_block (&compound_stmt);
24673 /* Parse the function-body. */
24674 cp_parser_ctor_initializer_opt_and_function_body
24675 (parser, /*in_function_try_block=*/true);
24676 /* We're done with the `try' part. */
24677 finish_function_try_block (try_block);
24678 /* Parse the handlers. */
24679 cp_parser_handler_seq (parser);
24680 /* We're done with the handlers. */
24681 finish_function_handler_sequence (try_block, compound_stmt);
24684 /* Parse a handler-seq.
24686 handler-seq:
24687 handler handler-seq [opt] */
24689 static void
24690 cp_parser_handler_seq (cp_parser* parser)
24692 while (true)
24694 cp_token *token;
24696 /* Parse the handler. */
24697 cp_parser_handler (parser);
24698 /* Peek at the next token. */
24699 token = cp_lexer_peek_token (parser->lexer);
24700 /* If it's not `catch' then there are no more handlers. */
24701 if (!cp_parser_is_keyword (token, RID_CATCH))
24702 break;
24706 /* Parse a handler.
24708 handler:
24709 catch ( exception-declaration ) compound-statement */
24711 static void
24712 cp_parser_handler (cp_parser* parser)
24714 tree handler;
24715 tree declaration;
24717 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24718 handler = begin_handler ();
24719 matching_parens parens;
24720 parens.require_open (parser);
24721 declaration = cp_parser_exception_declaration (parser);
24722 finish_handler_parms (declaration, handler);
24723 parens.require_close (parser);
24724 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24725 finish_handler (handler);
24728 /* Parse an exception-declaration.
24730 exception-declaration:
24731 type-specifier-seq declarator
24732 type-specifier-seq abstract-declarator
24733 type-specifier-seq
24736 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24737 ellipsis variant is used. */
24739 static tree
24740 cp_parser_exception_declaration (cp_parser* parser)
24742 cp_decl_specifier_seq type_specifiers;
24743 cp_declarator *declarator;
24744 const char *saved_message;
24746 /* If it's an ellipsis, it's easy to handle. */
24747 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24749 /* Consume the `...' token. */
24750 cp_lexer_consume_token (parser->lexer);
24751 return NULL_TREE;
24754 /* Types may not be defined in exception-declarations. */
24755 saved_message = parser->type_definition_forbidden_message;
24756 parser->type_definition_forbidden_message
24757 = G_("types may not be defined in exception-declarations");
24759 /* Parse the type-specifier-seq. */
24760 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24761 /*is_trailing_return=*/false,
24762 &type_specifiers);
24763 /* If it's a `)', then there is no declarator. */
24764 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24765 declarator = NULL;
24766 else
24767 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24768 /*ctor_dtor_or_conv_p=*/NULL,
24769 /*parenthesized_p=*/NULL,
24770 /*member_p=*/false,
24771 /*friend_p=*/false);
24773 /* Restore the saved message. */
24774 parser->type_definition_forbidden_message = saved_message;
24776 if (!type_specifiers.any_specifiers_p)
24777 return error_mark_node;
24779 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24782 /* Parse a throw-expression.
24784 throw-expression:
24785 throw assignment-expression [opt]
24787 Returns a THROW_EXPR representing the throw-expression. */
24789 static tree
24790 cp_parser_throw_expression (cp_parser* parser)
24792 tree expression;
24793 cp_token* token;
24795 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24796 token = cp_lexer_peek_token (parser->lexer);
24797 /* Figure out whether or not there is an assignment-expression
24798 following the "throw" keyword. */
24799 if (token->type == CPP_COMMA
24800 || token->type == CPP_SEMICOLON
24801 || token->type == CPP_CLOSE_PAREN
24802 || token->type == CPP_CLOSE_SQUARE
24803 || token->type == CPP_CLOSE_BRACE
24804 || token->type == CPP_COLON)
24805 expression = NULL_TREE;
24806 else
24807 expression = cp_parser_assignment_expression (parser);
24809 return build_throw (expression);
24812 /* GNU Extensions */
24814 /* Parse an (optional) asm-specification.
24816 asm-specification:
24817 asm ( string-literal )
24819 If the asm-specification is present, returns a STRING_CST
24820 corresponding to the string-literal. Otherwise, returns
24821 NULL_TREE. */
24823 static tree
24824 cp_parser_asm_specification_opt (cp_parser* parser)
24826 cp_token *token;
24827 tree asm_specification;
24829 /* Peek at the next token. */
24830 token = cp_lexer_peek_token (parser->lexer);
24831 /* If the next token isn't the `asm' keyword, then there's no
24832 asm-specification. */
24833 if (!cp_parser_is_keyword (token, RID_ASM))
24834 return NULL_TREE;
24836 /* Consume the `asm' token. */
24837 cp_lexer_consume_token (parser->lexer);
24838 /* Look for the `('. */
24839 matching_parens parens;
24840 parens.require_open (parser);
24842 /* Look for the string-literal. */
24843 asm_specification = cp_parser_string_literal (parser, false, false);
24845 /* Look for the `)'. */
24846 parens.require_close (parser);
24848 return asm_specification;
24851 /* Parse an asm-operand-list.
24853 asm-operand-list:
24854 asm-operand
24855 asm-operand-list , asm-operand
24857 asm-operand:
24858 string-literal ( expression )
24859 [ string-literal ] string-literal ( expression )
24861 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24862 each node is the expression. The TREE_PURPOSE is itself a
24863 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24864 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24865 is a STRING_CST for the string literal before the parenthesis. Returns
24866 ERROR_MARK_NODE if any of the operands are invalid. */
24868 static tree
24869 cp_parser_asm_operand_list (cp_parser* parser)
24871 tree asm_operands = NULL_TREE;
24872 bool invalid_operands = false;
24874 while (true)
24876 tree string_literal;
24877 tree expression;
24878 tree name;
24880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24882 /* Consume the `[' token. */
24883 cp_lexer_consume_token (parser->lexer);
24884 /* Read the operand name. */
24885 name = cp_parser_identifier (parser);
24886 if (name != error_mark_node)
24887 name = build_string (IDENTIFIER_LENGTH (name),
24888 IDENTIFIER_POINTER (name));
24889 /* Look for the closing `]'. */
24890 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24892 else
24893 name = NULL_TREE;
24894 /* Look for the string-literal. */
24895 string_literal = cp_parser_string_literal (parser, false, false);
24897 /* Look for the `('. */
24898 matching_parens parens;
24899 parens.require_open (parser);
24900 /* Parse the expression. */
24901 expression = cp_parser_expression (parser);
24902 /* Look for the `)'. */
24903 parens.require_close (parser);
24905 if (name == error_mark_node
24906 || string_literal == error_mark_node
24907 || expression == error_mark_node)
24908 invalid_operands = true;
24910 /* Add this operand to the list. */
24911 asm_operands = tree_cons (build_tree_list (name, string_literal),
24912 expression,
24913 asm_operands);
24914 /* If the next token is not a `,', there are no more
24915 operands. */
24916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24917 break;
24918 /* Consume the `,'. */
24919 cp_lexer_consume_token (parser->lexer);
24922 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24925 /* Parse an asm-clobber-list.
24927 asm-clobber-list:
24928 string-literal
24929 asm-clobber-list , string-literal
24931 Returns a TREE_LIST, indicating the clobbers in the order that they
24932 appeared. The TREE_VALUE of each node is a STRING_CST. */
24934 static tree
24935 cp_parser_asm_clobber_list (cp_parser* parser)
24937 tree clobbers = NULL_TREE;
24939 while (true)
24941 tree string_literal;
24943 /* Look for the string literal. */
24944 string_literal = cp_parser_string_literal (parser, false, false);
24945 /* Add it to the list. */
24946 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24947 /* If the next token is not a `,', then the list is
24948 complete. */
24949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24950 break;
24951 /* Consume the `,' token. */
24952 cp_lexer_consume_token (parser->lexer);
24955 return clobbers;
24958 /* Parse an asm-label-list.
24960 asm-label-list:
24961 identifier
24962 asm-label-list , identifier
24964 Returns a TREE_LIST, indicating the labels in the order that they
24965 appeared. The TREE_VALUE of each node is a label. */
24967 static tree
24968 cp_parser_asm_label_list (cp_parser* parser)
24970 tree labels = NULL_TREE;
24972 while (true)
24974 tree identifier, label, name;
24976 /* Look for the identifier. */
24977 identifier = cp_parser_identifier (parser);
24978 if (!error_operand_p (identifier))
24980 label = lookup_label (identifier);
24981 if (TREE_CODE (label) == LABEL_DECL)
24983 TREE_USED (label) = 1;
24984 check_goto (label);
24985 name = build_string (IDENTIFIER_LENGTH (identifier),
24986 IDENTIFIER_POINTER (identifier));
24987 labels = tree_cons (name, label, labels);
24990 /* If the next token is not a `,', then the list is
24991 complete. */
24992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24993 break;
24994 /* Consume the `,' token. */
24995 cp_lexer_consume_token (parser->lexer);
24998 return nreverse (labels);
25001 /* Return TRUE iff the next tokens in the stream are possibly the
25002 beginning of a GNU extension attribute. */
25004 static bool
25005 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25007 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25010 /* Return TRUE iff the next tokens in the stream are possibly the
25011 beginning of a standard C++-11 attribute specifier. */
25013 static bool
25014 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25016 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25019 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25020 beginning of a standard C++-11 attribute specifier. */
25022 static bool
25023 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25025 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25027 return (cxx_dialect >= cxx11
25028 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25029 || (token->type == CPP_OPEN_SQUARE
25030 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25031 && token->type == CPP_OPEN_SQUARE)));
25034 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25035 beginning of a GNU extension attribute. */
25037 static bool
25038 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25040 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25042 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25045 /* Return true iff the next tokens can be the beginning of either a
25046 GNU attribute list, or a standard C++11 attribute sequence. */
25048 static bool
25049 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25051 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25052 || cp_next_tokens_can_be_std_attribute_p (parser));
25055 /* Return true iff the next Nth tokens can be the beginning of either
25056 a GNU attribute list, or a standard C++11 attribute sequence. */
25058 static bool
25059 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25061 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25062 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25065 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25066 of GNU attributes, or return NULL. */
25068 static tree
25069 cp_parser_attributes_opt (cp_parser *parser)
25071 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25072 return cp_parser_gnu_attributes_opt (parser);
25073 return cp_parser_std_attribute_spec_seq (parser);
25076 /* Parse an (optional) series of attributes.
25078 attributes:
25079 attributes attribute
25081 attribute:
25082 __attribute__ (( attribute-list [opt] ))
25084 The return value is as for cp_parser_gnu_attribute_list. */
25086 static tree
25087 cp_parser_gnu_attributes_opt (cp_parser* parser)
25089 tree attributes = NULL_TREE;
25091 temp_override<bool> cleanup
25092 (parser->auto_is_implicit_function_template_parm_p, false);
25094 while (true)
25096 cp_token *token;
25097 tree attribute_list;
25098 bool ok = true;
25100 /* Peek at the next token. */
25101 token = cp_lexer_peek_token (parser->lexer);
25102 /* If it's not `__attribute__', then we're done. */
25103 if (token->keyword != RID_ATTRIBUTE)
25104 break;
25106 /* Consume the `__attribute__' keyword. */
25107 cp_lexer_consume_token (parser->lexer);
25108 /* Look for the two `(' tokens. */
25109 matching_parens outer_parens;
25110 outer_parens.require_open (parser);
25111 matching_parens inner_parens;
25112 inner_parens.require_open (parser);
25114 /* Peek at the next token. */
25115 token = cp_lexer_peek_token (parser->lexer);
25116 if (token->type != CPP_CLOSE_PAREN)
25117 /* Parse the attribute-list. */
25118 attribute_list = cp_parser_gnu_attribute_list (parser);
25119 else
25120 /* If the next token is a `)', then there is no attribute
25121 list. */
25122 attribute_list = NULL;
25124 /* Look for the two `)' tokens. */
25125 if (!inner_parens.require_close (parser))
25126 ok = false;
25127 if (!outer_parens.require_close (parser))
25128 ok = false;
25129 if (!ok)
25130 cp_parser_skip_to_end_of_statement (parser);
25132 /* Add these new attributes to the list. */
25133 attributes = attr_chainon (attributes, attribute_list);
25136 return attributes;
25139 /* Parse a GNU attribute-list.
25141 attribute-list:
25142 attribute
25143 attribute-list , attribute
25145 attribute:
25146 identifier
25147 identifier ( identifier )
25148 identifier ( identifier , expression-list )
25149 identifier ( expression-list )
25151 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25152 to an attribute. The TREE_PURPOSE of each node is the identifier
25153 indicating which attribute is in use. The TREE_VALUE represents
25154 the arguments, if any. */
25156 static tree
25157 cp_parser_gnu_attribute_list (cp_parser* parser)
25159 tree attribute_list = NULL_TREE;
25160 bool save_translate_strings_p = parser->translate_strings_p;
25162 parser->translate_strings_p = false;
25163 while (true)
25165 cp_token *token;
25166 tree identifier;
25167 tree attribute;
25169 /* Look for the identifier. We also allow keywords here; for
25170 example `__attribute__ ((const))' is legal. */
25171 token = cp_lexer_peek_token (parser->lexer);
25172 if (token->type == CPP_NAME
25173 || token->type == CPP_KEYWORD)
25175 tree arguments = NULL_TREE;
25177 /* Consume the token, but save it since we need it for the
25178 SIMD enabled function parsing. */
25179 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25181 /* Save away the identifier that indicates which attribute
25182 this is. */
25183 identifier = (token->type == CPP_KEYWORD)
25184 /* For keywords, use the canonical spelling, not the
25185 parsed identifier. */
25186 ? ridpointers[(int) token->keyword]
25187 : id_token->u.value;
25189 identifier = canonicalize_attr_name (identifier);
25190 attribute = build_tree_list (identifier, NULL_TREE);
25192 /* Peek at the next token. */
25193 token = cp_lexer_peek_token (parser->lexer);
25194 /* If it's an `(', then parse the attribute arguments. */
25195 if (token->type == CPP_OPEN_PAREN)
25197 vec<tree, va_gc> *vec;
25198 int attr_flag = (attribute_takes_identifier_p (identifier)
25199 ? id_attr : normal_attr);
25200 vec = cp_parser_parenthesized_expression_list
25201 (parser, attr_flag, /*cast_p=*/false,
25202 /*allow_expansion_p=*/false,
25203 /*non_constant_p=*/NULL);
25204 if (vec == NULL)
25205 arguments = error_mark_node;
25206 else
25208 arguments = build_tree_list_vec (vec);
25209 release_tree_vector (vec);
25211 /* Save the arguments away. */
25212 TREE_VALUE (attribute) = arguments;
25215 if (arguments != error_mark_node)
25217 /* Add this attribute to the list. */
25218 TREE_CHAIN (attribute) = attribute_list;
25219 attribute_list = attribute;
25222 token = cp_lexer_peek_token (parser->lexer);
25224 /* Now, look for more attributes. If the next token isn't a
25225 `,', we're done. */
25226 if (token->type != CPP_COMMA)
25227 break;
25229 /* Consume the comma and keep going. */
25230 cp_lexer_consume_token (parser->lexer);
25232 parser->translate_strings_p = save_translate_strings_p;
25234 /* We built up the list in reverse order. */
25235 return nreverse (attribute_list);
25238 /* Parse a standard C++11 attribute.
25240 The returned representation is a TREE_LIST which TREE_PURPOSE is
25241 the scoped name of the attribute, and the TREE_VALUE is its
25242 arguments list.
25244 Note that the scoped name of the attribute is itself a TREE_LIST
25245 which TREE_PURPOSE is the namespace of the attribute, and
25246 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25247 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25248 and which TREE_PURPOSE is directly the attribute name.
25250 Clients of the attribute code should use get_attribute_namespace
25251 and get_attribute_name to get the actual namespace and name of
25252 attributes, regardless of their being GNU or C++11 attributes.
25254 attribute:
25255 attribute-token attribute-argument-clause [opt]
25257 attribute-token:
25258 identifier
25259 attribute-scoped-token
25261 attribute-scoped-token:
25262 attribute-namespace :: identifier
25264 attribute-namespace:
25265 identifier
25267 attribute-argument-clause:
25268 ( balanced-token-seq )
25270 balanced-token-seq:
25271 balanced-token [opt]
25272 balanced-token-seq balanced-token
25274 balanced-token:
25275 ( balanced-token-seq )
25276 [ balanced-token-seq ]
25277 { balanced-token-seq }. */
25279 static tree
25280 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25282 tree attribute, attr_id = NULL_TREE, arguments;
25283 cp_token *token;
25285 temp_override<bool> cleanup
25286 (parser->auto_is_implicit_function_template_parm_p, false);
25288 /* First, parse name of the attribute, a.k.a attribute-token. */
25290 token = cp_lexer_peek_token (parser->lexer);
25291 if (token->type == CPP_NAME)
25292 attr_id = token->u.value;
25293 else if (token->type == CPP_KEYWORD)
25294 attr_id = ridpointers[(int) token->keyword];
25295 else if (token->flags & NAMED_OP)
25296 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25298 if (attr_id == NULL_TREE)
25299 return NULL_TREE;
25301 cp_lexer_consume_token (parser->lexer);
25303 token = cp_lexer_peek_token (parser->lexer);
25304 if (token->type == CPP_SCOPE)
25306 /* We are seeing a scoped attribute token. */
25308 cp_lexer_consume_token (parser->lexer);
25309 if (attr_ns)
25310 error_at (token->location, "attribute using prefix used together "
25311 "with scoped attribute token");
25312 attr_ns = attr_id;
25314 token = cp_lexer_consume_token (parser->lexer);
25315 if (token->type == CPP_NAME)
25316 attr_id = token->u.value;
25317 else if (token->type == CPP_KEYWORD)
25318 attr_id = ridpointers[(int) token->keyword];
25319 else if (token->flags & NAMED_OP)
25320 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25321 else
25323 error_at (token->location,
25324 "expected an identifier for the attribute name");
25325 return error_mark_node;
25328 attr_id = canonicalize_attr_name (attr_id);
25329 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25330 NULL_TREE);
25331 token = cp_lexer_peek_token (parser->lexer);
25333 else if (attr_ns)
25334 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25335 NULL_TREE);
25336 else
25338 attr_id = canonicalize_attr_name (attr_id);
25339 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25340 NULL_TREE);
25341 /* C++11 noreturn attribute is equivalent to GNU's. */
25342 if (is_attribute_p ("noreturn", attr_id))
25343 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25344 /* C++14 deprecated attribute is equivalent to GNU's. */
25345 else if (is_attribute_p ("deprecated", attr_id))
25346 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25347 /* C++17 fallthrough attribute is equivalent to GNU's. */
25348 else if (is_attribute_p ("fallthrough", attr_id))
25349 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25350 /* Transactional Memory TS optimize_for_synchronized attribute is
25351 equivalent to GNU transaction_callable. */
25352 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25353 TREE_PURPOSE (attribute)
25354 = get_identifier ("transaction_callable");
25355 /* Transactional Memory attributes are GNU attributes. */
25356 else if (tm_attr_to_mask (attr_id))
25357 TREE_PURPOSE (attribute) = attr_id;
25360 /* Now parse the optional argument clause of the attribute. */
25362 if (token->type != CPP_OPEN_PAREN)
25363 return attribute;
25366 vec<tree, va_gc> *vec;
25367 int attr_flag = normal_attr;
25369 if (attr_ns == get_identifier ("gnu")
25370 && attribute_takes_identifier_p (attr_id))
25371 /* A GNU attribute that takes an identifier in parameter. */
25372 attr_flag = id_attr;
25374 vec = cp_parser_parenthesized_expression_list
25375 (parser, attr_flag, /*cast_p=*/false,
25376 /*allow_expansion_p=*/true,
25377 /*non_constant_p=*/NULL);
25378 if (vec == NULL)
25379 arguments = error_mark_node;
25380 else
25382 arguments = build_tree_list_vec (vec);
25383 release_tree_vector (vec);
25386 if (arguments == error_mark_node)
25387 attribute = error_mark_node;
25388 else
25389 TREE_VALUE (attribute) = arguments;
25392 return attribute;
25395 /* Check that the attribute ATTRIBUTE appears at most once in the
25396 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25397 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25398 isn't implemented yet in GCC. */
25400 static void
25401 cp_parser_check_std_attribute (tree attributes, tree attribute)
25403 if (attributes)
25405 tree name = get_attribute_name (attribute);
25406 if (is_attribute_p ("noreturn", name)
25407 && lookup_attribute ("noreturn", attributes))
25408 error ("attribute %<noreturn%> can appear at most once "
25409 "in an attribute-list");
25410 else if (is_attribute_p ("deprecated", name)
25411 && lookup_attribute ("deprecated", attributes))
25412 error ("attribute %<deprecated%> can appear at most once "
25413 "in an attribute-list");
25417 /* Parse a list of standard C++-11 attributes.
25419 attribute-list:
25420 attribute [opt]
25421 attribute-list , attribute[opt]
25422 attribute ...
25423 attribute-list , attribute ...
25426 static tree
25427 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25429 tree attributes = NULL_TREE, attribute = NULL_TREE;
25430 cp_token *token = NULL;
25432 while (true)
25434 attribute = cp_parser_std_attribute (parser, attr_ns);
25435 if (attribute == error_mark_node)
25436 break;
25437 if (attribute != NULL_TREE)
25439 cp_parser_check_std_attribute (attributes, attribute);
25440 TREE_CHAIN (attribute) = attributes;
25441 attributes = attribute;
25443 token = cp_lexer_peek_token (parser->lexer);
25444 if (token->type == CPP_ELLIPSIS)
25446 cp_lexer_consume_token (parser->lexer);
25447 if (attribute == NULL_TREE)
25448 error_at (token->location,
25449 "expected attribute before %<...%>");
25450 else
25452 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25453 if (pack == error_mark_node)
25454 return error_mark_node;
25455 TREE_VALUE (attribute) = pack;
25457 token = cp_lexer_peek_token (parser->lexer);
25459 if (token->type != CPP_COMMA)
25460 break;
25461 cp_lexer_consume_token (parser->lexer);
25463 attributes = nreverse (attributes);
25464 return attributes;
25467 /* Parse a standard C++-11 attribute specifier.
25469 attribute-specifier:
25470 [ [ attribute-using-prefix [opt] attribute-list ] ]
25471 alignment-specifier
25473 attribute-using-prefix:
25474 using attribute-namespace :
25476 alignment-specifier:
25477 alignas ( type-id ... [opt] )
25478 alignas ( alignment-expression ... [opt] ). */
25480 static tree
25481 cp_parser_std_attribute_spec (cp_parser *parser)
25483 tree attributes = NULL_TREE;
25484 cp_token *token = cp_lexer_peek_token (parser->lexer);
25486 if (token->type == CPP_OPEN_SQUARE
25487 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25489 tree attr_ns = NULL_TREE;
25491 cp_lexer_consume_token (parser->lexer);
25492 cp_lexer_consume_token (parser->lexer);
25494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25496 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25497 if (token->type == CPP_NAME)
25498 attr_ns = token->u.value;
25499 else if (token->type == CPP_KEYWORD)
25500 attr_ns = ridpointers[(int) token->keyword];
25501 else if (token->flags & NAMED_OP)
25502 attr_ns = get_identifier (cpp_type2name (token->type,
25503 token->flags));
25504 if (attr_ns
25505 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25507 if (cxx_dialect < cxx17
25508 && !in_system_header_at (input_location))
25509 pedwarn (input_location, 0,
25510 "attribute using prefix only available "
25511 "with -std=c++17 or -std=gnu++17");
25513 cp_lexer_consume_token (parser->lexer);
25514 cp_lexer_consume_token (parser->lexer);
25515 cp_lexer_consume_token (parser->lexer);
25517 else
25518 attr_ns = NULL_TREE;
25521 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25523 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25524 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25525 cp_parser_skip_to_end_of_statement (parser);
25526 else
25527 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25528 when we are sure that we have actually parsed them. */
25529 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25531 else
25533 tree alignas_expr;
25535 /* Look for an alignment-specifier. */
25537 token = cp_lexer_peek_token (parser->lexer);
25539 if (token->type != CPP_KEYWORD
25540 || token->keyword != RID_ALIGNAS)
25541 return NULL_TREE;
25543 cp_lexer_consume_token (parser->lexer);
25544 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25546 matching_parens parens;
25547 if (!parens.require_open (parser))
25548 return error_mark_node;
25550 cp_parser_parse_tentatively (parser);
25551 alignas_expr = cp_parser_type_id (parser);
25553 if (!cp_parser_parse_definitely (parser))
25555 alignas_expr = cp_parser_assignment_expression (parser);
25556 if (alignas_expr == error_mark_node)
25557 cp_parser_skip_to_end_of_statement (parser);
25558 if (alignas_expr == NULL_TREE
25559 || alignas_expr == error_mark_node)
25560 return alignas_expr;
25563 alignas_expr = cxx_alignas_expr (alignas_expr);
25564 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25566 /* Handle alignas (pack...). */
25567 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25569 cp_lexer_consume_token (parser->lexer);
25570 alignas_expr = make_pack_expansion (alignas_expr);
25573 /* Something went wrong, so don't build the attribute. */
25574 if (alignas_expr == error_mark_node)
25575 return error_mark_node;
25577 if (!parens.require_close (parser))
25578 return error_mark_node;
25580 /* Build the C++-11 representation of an 'aligned'
25581 attribute. */
25582 attributes =
25583 build_tree_list (build_tree_list (get_identifier ("gnu"),
25584 get_identifier ("aligned")),
25585 alignas_expr);
25588 return attributes;
25591 /* Parse a standard C++-11 attribute-specifier-seq.
25593 attribute-specifier-seq:
25594 attribute-specifier-seq [opt] attribute-specifier
25597 static tree
25598 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25600 tree attr_specs = NULL_TREE;
25601 tree attr_last = NULL_TREE;
25603 while (true)
25605 tree attr_spec = cp_parser_std_attribute_spec (parser);
25606 if (attr_spec == NULL_TREE)
25607 break;
25608 if (attr_spec == error_mark_node)
25609 return error_mark_node;
25611 if (attr_last)
25612 TREE_CHAIN (attr_last) = attr_spec;
25613 else
25614 attr_specs = attr_last = attr_spec;
25615 attr_last = tree_last (attr_last);
25618 return attr_specs;
25621 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25622 return index of the first token after balanced-token, or N on failure. */
25624 static size_t
25625 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25627 size_t orig_n = n;
25628 int nparens = 0, nbraces = 0, nsquares = 0;
25630 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25632 case CPP_EOF:
25633 case CPP_PRAGMA_EOL:
25634 /* Ran out of tokens. */
25635 return orig_n;
25636 case CPP_OPEN_PAREN:
25637 ++nparens;
25638 break;
25639 case CPP_OPEN_BRACE:
25640 ++nbraces;
25641 break;
25642 case CPP_OPEN_SQUARE:
25643 ++nsquares;
25644 break;
25645 case CPP_CLOSE_PAREN:
25646 --nparens;
25647 break;
25648 case CPP_CLOSE_BRACE:
25649 --nbraces;
25650 break;
25651 case CPP_CLOSE_SQUARE:
25652 --nsquares;
25653 break;
25654 default:
25655 break;
25657 while (nparens || nbraces || nsquares);
25658 return n;
25661 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25662 return index of the first token after the GNU attribute tokens, or N on
25663 failure. */
25665 static size_t
25666 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25668 while (true)
25670 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25671 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25672 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25673 break;
25675 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25676 if (n2 == n + 2)
25677 break;
25678 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25679 break;
25680 n = n2 + 1;
25682 return n;
25685 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25686 next token), return index of the first token after the standard C++11
25687 attribute tokens, or N on failure. */
25689 static size_t
25690 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25692 while (true)
25694 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25695 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25697 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25698 if (n2 == n + 1)
25699 break;
25700 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25701 break;
25702 n = n2 + 1;
25704 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25705 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25707 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25708 if (n2 == n + 1)
25709 break;
25710 n = n2;
25712 else
25713 break;
25715 return n;
25718 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25719 as the next token), return index of the first token after the attribute
25720 tokens, or N on failure. */
25722 static size_t
25723 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25725 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25726 return cp_parser_skip_gnu_attributes_opt (parser, n);
25727 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25730 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25731 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25732 current value of the PEDANTIC flag, regardless of whether or not
25733 the `__extension__' keyword is present. The caller is responsible
25734 for restoring the value of the PEDANTIC flag. */
25736 static bool
25737 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25739 /* Save the old value of the PEDANTIC flag. */
25740 *saved_pedantic = pedantic;
25742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25744 /* Consume the `__extension__' token. */
25745 cp_lexer_consume_token (parser->lexer);
25746 /* We're not being pedantic while the `__extension__' keyword is
25747 in effect. */
25748 pedantic = 0;
25750 return true;
25753 return false;
25756 /* Parse a label declaration.
25758 label-declaration:
25759 __label__ label-declarator-seq ;
25761 label-declarator-seq:
25762 identifier , label-declarator-seq
25763 identifier */
25765 static void
25766 cp_parser_label_declaration (cp_parser* parser)
25768 /* Look for the `__label__' keyword. */
25769 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25771 while (true)
25773 tree identifier;
25775 /* Look for an identifier. */
25776 identifier = cp_parser_identifier (parser);
25777 /* If we failed, stop. */
25778 if (identifier == error_mark_node)
25779 break;
25780 /* Declare it as a label. */
25781 finish_label_decl (identifier);
25782 /* If the next token is a `;', stop. */
25783 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25784 break;
25785 /* Look for the `,' separating the label declarations. */
25786 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25789 /* Look for the final `;'. */
25790 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25793 // -------------------------------------------------------------------------- //
25794 // Requires Clause
25796 // Parse a requires clause.
25798 // requires-clause:
25799 // 'requires' logical-or-expression
25801 // The required logical-or-expression must be a constant expression. Note
25802 // that we don't check that the expression is constepxr here. We defer until
25803 // we analyze constraints and then, we only check atomic constraints.
25804 static tree
25805 cp_parser_requires_clause (cp_parser *parser)
25807 // Parse the requires clause so that it is not automatically folded.
25808 ++processing_template_decl;
25809 tree expr = cp_parser_binary_expression (parser, false, false,
25810 PREC_NOT_OPERATOR, NULL);
25811 if (check_for_bare_parameter_packs (expr))
25812 expr = error_mark_node;
25813 --processing_template_decl;
25814 return expr;
25817 // Optionally parse a requires clause:
25818 static tree
25819 cp_parser_requires_clause_opt (cp_parser *parser)
25821 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25822 if (tok->keyword != RID_REQUIRES)
25824 if (!flag_concepts && tok->type == CPP_NAME
25825 && tok->u.value == ridpointers[RID_REQUIRES])
25827 error_at (cp_lexer_peek_token (parser->lexer)->location,
25828 "%<requires%> only available with -fconcepts");
25829 /* Parse and discard the requires-clause. */
25830 cp_lexer_consume_token (parser->lexer);
25831 cp_parser_requires_clause (parser);
25833 return NULL_TREE;
25835 cp_lexer_consume_token (parser->lexer);
25836 return cp_parser_requires_clause (parser);
25840 /*---------------------------------------------------------------------------
25841 Requires expressions
25842 ---------------------------------------------------------------------------*/
25844 /* Parse a requires expression
25846 requirement-expression:
25847 'requires' requirement-parameter-list [opt] requirement-body */
25848 static tree
25849 cp_parser_requires_expression (cp_parser *parser)
25851 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25852 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25854 /* A requires-expression shall appear only within a concept
25855 definition or a requires-clause.
25857 TODO: Implement this diagnostic correctly. */
25858 if (!processing_template_decl)
25860 error_at (loc, "a requires expression cannot appear outside a template");
25861 cp_parser_skip_to_end_of_statement (parser);
25862 return error_mark_node;
25865 tree parms, reqs;
25867 /* Local parameters are delared as variables within the scope
25868 of the expression. They are not visible past the end of
25869 the expression. Expressions within the requires-expression
25870 are unevaluated. */
25871 struct scope_sentinel
25873 scope_sentinel ()
25875 ++cp_unevaluated_operand;
25876 begin_scope (sk_block, NULL_TREE);
25879 ~scope_sentinel ()
25881 pop_bindings_and_leave_scope ();
25882 --cp_unevaluated_operand;
25884 } s;
25886 /* Parse the optional parameter list. */
25887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25889 parms = cp_parser_requirement_parameter_list (parser);
25890 if (parms == error_mark_node)
25891 return error_mark_node;
25893 else
25894 parms = NULL_TREE;
25896 /* Parse the requirement body. */
25897 reqs = cp_parser_requirement_body (parser);
25898 if (reqs == error_mark_node)
25899 return error_mark_node;
25902 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25903 the parm chain. */
25904 grokparms (parms, &parms);
25905 return finish_requires_expr (parms, reqs);
25908 /* Parse a parameterized requirement.
25910 requirement-parameter-list:
25911 '(' parameter-declaration-clause ')' */
25912 static tree
25913 cp_parser_requirement_parameter_list (cp_parser *parser)
25915 matching_parens parens;
25916 if (!parens.require_open (parser))
25917 return error_mark_node;
25919 tree parms = cp_parser_parameter_declaration_clause (parser);
25921 if (!parens.require_close (parser))
25922 return error_mark_node;
25924 return parms;
25927 /* Parse the body of a requirement.
25929 requirement-body:
25930 '{' requirement-list '}' */
25931 static tree
25932 cp_parser_requirement_body (cp_parser *parser)
25934 matching_braces braces;
25935 if (!braces.require_open (parser))
25936 return error_mark_node;
25938 tree reqs = cp_parser_requirement_list (parser);
25940 if (!braces.require_close (parser))
25941 return error_mark_node;
25943 return reqs;
25946 /* Parse a list of requirements.
25948 requirement-list:
25949 requirement
25950 requirement-list ';' requirement[opt] */
25951 static tree
25952 cp_parser_requirement_list (cp_parser *parser)
25954 tree result = NULL_TREE;
25955 while (true)
25957 tree req = cp_parser_requirement (parser);
25958 if (req == error_mark_node)
25959 return error_mark_node;
25961 result = tree_cons (NULL_TREE, req, result);
25963 /* If we see a semi-colon, consume it. */
25964 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25965 cp_lexer_consume_token (parser->lexer);
25967 /* Stop processing at the end of the list. */
25968 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25969 break;
25972 /* Reverse the order of requirements so they are analyzed in
25973 declaration order. */
25974 return nreverse (result);
25977 /* Parse a syntactic requirement or type requirement.
25979 requirement:
25980 simple-requirement
25981 compound-requirement
25982 type-requirement
25983 nested-requirement */
25984 static tree
25985 cp_parser_requirement (cp_parser *parser)
25987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25988 return cp_parser_compound_requirement (parser);
25989 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25990 return cp_parser_type_requirement (parser);
25991 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25992 return cp_parser_nested_requirement (parser);
25993 else
25994 return cp_parser_simple_requirement (parser);
25997 /* Parse a simple requirement.
25999 simple-requirement:
26000 expression ';' */
26001 static tree
26002 cp_parser_simple_requirement (cp_parser *parser)
26004 tree expr = cp_parser_expression (parser, NULL, false, false);
26005 if (!expr || expr == error_mark_node)
26006 return error_mark_node;
26008 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26009 return error_mark_node;
26011 return finish_simple_requirement (expr);
26014 /* Parse a type requirement
26016 type-requirement
26017 nested-name-specifier [opt] required-type-name ';'
26019 required-type-name:
26020 type-name
26021 'template' [opt] simple-template-id */
26022 static tree
26023 cp_parser_type_requirement (cp_parser *parser)
26025 cp_lexer_consume_token (parser->lexer);
26027 // Save the scope before parsing name specifiers.
26028 tree saved_scope = parser->scope;
26029 tree saved_object_scope = parser->object_scope;
26030 tree saved_qualifying_scope = parser->qualifying_scope;
26031 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26032 cp_parser_nested_name_specifier_opt (parser,
26033 /*typename_keyword_p=*/true,
26034 /*check_dependency_p=*/false,
26035 /*type_p=*/true,
26036 /*is_declaration=*/false);
26038 tree type;
26039 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26041 cp_lexer_consume_token (parser->lexer);
26042 type = cp_parser_template_id (parser,
26043 /*template_keyword_p=*/true,
26044 /*check_dependency=*/false,
26045 /*tag_type=*/none_type,
26046 /*is_declaration=*/false);
26047 type = make_typename_type (parser->scope, type, typename_type,
26048 /*complain=*/tf_error);
26050 else
26051 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26053 if (TREE_CODE (type) == TYPE_DECL)
26054 type = TREE_TYPE (type);
26056 parser->scope = saved_scope;
26057 parser->object_scope = saved_object_scope;
26058 parser->qualifying_scope = saved_qualifying_scope;
26060 if (type == error_mark_node)
26061 cp_parser_skip_to_end_of_statement (parser);
26063 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26064 return error_mark_node;
26065 if (type == error_mark_node)
26066 return error_mark_node;
26068 return finish_type_requirement (type);
26071 /* Parse a compound requirement
26073 compound-requirement:
26074 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26075 static tree
26076 cp_parser_compound_requirement (cp_parser *parser)
26078 /* Parse an expression enclosed in '{ }'s. */
26079 matching_braces braces;
26080 if (!braces.require_open (parser))
26081 return error_mark_node;
26083 tree expr = cp_parser_expression (parser, NULL, false, false);
26084 if (!expr || expr == error_mark_node)
26085 return error_mark_node;
26087 if (!braces.require_close (parser))
26088 return error_mark_node;
26090 /* Parse the optional noexcept. */
26091 bool noexcept_p = false;
26092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26094 cp_lexer_consume_token (parser->lexer);
26095 noexcept_p = true;
26098 /* Parse the optional trailing return type. */
26099 tree type = NULL_TREE;
26100 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26102 cp_lexer_consume_token (parser->lexer);
26103 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26104 parser->in_result_type_constraint_p = true;
26105 type = cp_parser_trailing_type_id (parser);
26106 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26107 if (type == error_mark_node)
26108 return error_mark_node;
26111 return finish_compound_requirement (expr, type, noexcept_p);
26114 /* Parse a nested requirement. This is the same as a requires clause.
26116 nested-requirement:
26117 requires-clause */
26118 static tree
26119 cp_parser_nested_requirement (cp_parser *parser)
26121 cp_lexer_consume_token (parser->lexer);
26122 tree req = cp_parser_requires_clause (parser);
26123 if (req == error_mark_node)
26124 return error_mark_node;
26125 return finish_nested_requirement (req);
26128 /* Support Functions */
26130 /* Return the appropriate prefer_type argument for lookup_name_real based on
26131 tag_type and template_mem_access. */
26133 static inline int
26134 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26136 /* DR 141: When looking in the current enclosing context for a template-name
26137 after -> or ., only consider class templates. */
26138 if (template_mem_access)
26139 return 2;
26140 switch (tag_type)
26142 case none_type: return 0; // No preference.
26143 case scope_type: return 1; // Type or namespace.
26144 default: return 2; // Type only.
26148 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26149 NAME should have one of the representations used for an
26150 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26151 is returned. If PARSER->SCOPE is a dependent type, then a
26152 SCOPE_REF is returned.
26154 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26155 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26156 was formed. Abstractly, such entities should not be passed to this
26157 function, because they do not need to be looked up, but it is
26158 simpler to check for this special case here, rather than at the
26159 call-sites.
26161 In cases not explicitly covered above, this function returns a
26162 DECL, OVERLOAD, or baselink representing the result of the lookup.
26163 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26164 is returned.
26166 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26167 (e.g., "struct") that was used. In that case bindings that do not
26168 refer to types are ignored.
26170 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26171 ignored.
26173 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26174 are ignored.
26176 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26177 types.
26179 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26180 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26181 NULL_TREE otherwise. */
26183 static cp_expr
26184 cp_parser_lookup_name (cp_parser *parser, tree name,
26185 enum tag_types tag_type,
26186 bool is_template,
26187 bool is_namespace,
26188 bool check_dependency,
26189 tree *ambiguous_decls,
26190 location_t name_location)
26192 tree decl;
26193 tree object_type = parser->context->object_type;
26195 /* Assume that the lookup will be unambiguous. */
26196 if (ambiguous_decls)
26197 *ambiguous_decls = NULL_TREE;
26199 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26200 no longer valid. Note that if we are parsing tentatively, and
26201 the parse fails, OBJECT_TYPE will be automatically restored. */
26202 parser->context->object_type = NULL_TREE;
26204 if (name == error_mark_node)
26205 return error_mark_node;
26207 /* A template-id has already been resolved; there is no lookup to
26208 do. */
26209 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26210 return name;
26211 if (BASELINK_P (name))
26213 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26214 == TEMPLATE_ID_EXPR);
26215 return name;
26218 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26219 it should already have been checked to make sure that the name
26220 used matches the type being destroyed. */
26221 if (TREE_CODE (name) == BIT_NOT_EXPR)
26223 tree type;
26225 /* Figure out to which type this destructor applies. */
26226 if (parser->scope)
26227 type = parser->scope;
26228 else if (object_type)
26229 type = object_type;
26230 else
26231 type = current_class_type;
26232 /* If that's not a class type, there is no destructor. */
26233 if (!type || !CLASS_TYPE_P (type))
26234 return error_mark_node;
26236 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26237 lazily_declare_fn (sfk_destructor, type);
26239 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26240 return dtor;
26242 return error_mark_node;
26245 /* By this point, the NAME should be an ordinary identifier. If
26246 the id-expression was a qualified name, the qualifying scope is
26247 stored in PARSER->SCOPE at this point. */
26248 gcc_assert (identifier_p (name));
26250 /* Perform the lookup. */
26251 if (parser->scope)
26253 bool dependent_p;
26255 if (parser->scope == error_mark_node)
26256 return error_mark_node;
26258 /* If the SCOPE is dependent, the lookup must be deferred until
26259 the template is instantiated -- unless we are explicitly
26260 looking up names in uninstantiated templates. Even then, we
26261 cannot look up the name if the scope is not a class type; it
26262 might, for example, be a template type parameter. */
26263 dependent_p = (TYPE_P (parser->scope)
26264 && dependent_scope_p (parser->scope));
26265 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26266 && dependent_p)
26267 /* Defer lookup. */
26268 decl = error_mark_node;
26269 else
26271 tree pushed_scope = NULL_TREE;
26273 /* If PARSER->SCOPE is a dependent type, then it must be a
26274 class type, and we must not be checking dependencies;
26275 otherwise, we would have processed this lookup above. So
26276 that PARSER->SCOPE is not considered a dependent base by
26277 lookup_member, we must enter the scope here. */
26278 if (dependent_p)
26279 pushed_scope = push_scope (parser->scope);
26281 /* If the PARSER->SCOPE is a template specialization, it
26282 may be instantiated during name lookup. In that case,
26283 errors may be issued. Even if we rollback the current
26284 tentative parse, those errors are valid. */
26285 decl = lookup_qualified_name (parser->scope, name,
26286 prefer_type_arg (tag_type),
26287 /*complain=*/true);
26289 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26290 lookup result and the nested-name-specifier nominates a class C:
26291 * if the name specified after the nested-name-specifier, when
26292 looked up in C, is the injected-class-name of C (Clause 9), or
26293 * if the name specified after the nested-name-specifier is the
26294 same as the identifier or the simple-template-id's template-
26295 name in the last component of the nested-name-specifier,
26296 the name is instead considered to name the constructor of
26297 class C. [ Note: for example, the constructor is not an
26298 acceptable lookup result in an elaborated-type-specifier so
26299 the constructor would not be used in place of the
26300 injected-class-name. --end note ] Such a constructor name
26301 shall be used only in the declarator-id of a declaration that
26302 names a constructor or in a using-declaration. */
26303 if (tag_type == none_type
26304 && DECL_SELF_REFERENCE_P (decl)
26305 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26306 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26307 prefer_type_arg (tag_type),
26308 /*complain=*/true);
26310 /* If we have a single function from a using decl, pull it out. */
26311 if (TREE_CODE (decl) == OVERLOAD
26312 && !really_overloaded_fn (decl))
26313 decl = OVL_FUNCTION (decl);
26315 if (pushed_scope)
26316 pop_scope (pushed_scope);
26319 /* If the scope is a dependent type and either we deferred lookup or
26320 we did lookup but didn't find the name, rememeber the name. */
26321 if (decl == error_mark_node && TYPE_P (parser->scope)
26322 && dependent_type_p (parser->scope))
26324 if (tag_type)
26326 tree type;
26328 /* The resolution to Core Issue 180 says that `struct
26329 A::B' should be considered a type-name, even if `A'
26330 is dependent. */
26331 type = make_typename_type (parser->scope, name, tag_type,
26332 /*complain=*/tf_error);
26333 if (type != error_mark_node)
26334 decl = TYPE_NAME (type);
26336 else if (is_template
26337 && (cp_parser_next_token_ends_template_argument_p (parser)
26338 || cp_lexer_next_token_is (parser->lexer,
26339 CPP_CLOSE_PAREN)))
26340 decl = make_unbound_class_template (parser->scope,
26341 name, NULL_TREE,
26342 /*complain=*/tf_error);
26343 else
26344 decl = build_qualified_name (/*type=*/NULL_TREE,
26345 parser->scope, name,
26346 is_template);
26348 parser->qualifying_scope = parser->scope;
26349 parser->object_scope = NULL_TREE;
26351 else if (object_type)
26353 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26354 OBJECT_TYPE is not a class. */
26355 if (CLASS_TYPE_P (object_type))
26356 /* If the OBJECT_TYPE is a template specialization, it may
26357 be instantiated during name lookup. In that case, errors
26358 may be issued. Even if we rollback the current tentative
26359 parse, those errors are valid. */
26360 decl = lookup_member (object_type,
26361 name,
26362 /*protect=*/0,
26363 prefer_type_arg (tag_type),
26364 tf_warning_or_error);
26365 else
26366 decl = NULL_TREE;
26368 if (!decl)
26369 /* Look it up in the enclosing context. DR 141: When looking for a
26370 template-name after -> or ., only consider class templates. */
26371 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26372 /*nonclass=*/0,
26373 /*block_p=*/true, is_namespace, 0);
26374 if (object_type == unknown_type_node)
26375 /* The object is type-dependent, so we can't look anything up; we used
26376 this to get the DR 141 behavior. */
26377 object_type = NULL_TREE;
26378 parser->object_scope = object_type;
26379 parser->qualifying_scope = NULL_TREE;
26381 else
26383 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26384 /*nonclass=*/0,
26385 /*block_p=*/true, is_namespace, 0);
26386 parser->qualifying_scope = NULL_TREE;
26387 parser->object_scope = NULL_TREE;
26390 /* If the lookup failed, let our caller know. */
26391 if (!decl || decl == error_mark_node)
26392 return error_mark_node;
26394 /* Pull out the template from an injected-class-name (or multiple). */
26395 if (is_template)
26396 decl = maybe_get_template_decl_from_type_decl (decl);
26398 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26399 if (TREE_CODE (decl) == TREE_LIST)
26401 if (ambiguous_decls)
26402 *ambiguous_decls = decl;
26403 /* The error message we have to print is too complicated for
26404 cp_parser_error, so we incorporate its actions directly. */
26405 if (!cp_parser_simulate_error (parser))
26407 error_at (name_location, "reference to %qD is ambiguous",
26408 name);
26409 print_candidates (decl);
26411 return error_mark_node;
26414 gcc_assert (DECL_P (decl)
26415 || TREE_CODE (decl) == OVERLOAD
26416 || TREE_CODE (decl) == SCOPE_REF
26417 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26418 || BASELINK_P (decl));
26420 /* If we have resolved the name of a member declaration, check to
26421 see if the declaration is accessible. When the name resolves to
26422 set of overloaded functions, accessibility is checked when
26423 overload resolution is done.
26425 During an explicit instantiation, access is not checked at all,
26426 as per [temp.explicit]. */
26427 if (DECL_P (decl))
26428 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26430 maybe_record_typedef_use (decl);
26432 return cp_expr (decl, name_location);
26435 /* Like cp_parser_lookup_name, but for use in the typical case where
26436 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26437 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26439 static tree
26440 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26442 return cp_parser_lookup_name (parser, name,
26443 none_type,
26444 /*is_template=*/false,
26445 /*is_namespace=*/false,
26446 /*check_dependency=*/true,
26447 /*ambiguous_decls=*/NULL,
26448 location);
26451 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26452 the current context, return the TYPE_DECL. If TAG_NAME_P is
26453 true, the DECL indicates the class being defined in a class-head,
26454 or declared in an elaborated-type-specifier.
26456 Otherwise, return DECL. */
26458 static tree
26459 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26461 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26462 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26464 struct A {
26465 template <typename T> struct B;
26468 template <typename T> struct A::B {};
26470 Similarly, in an elaborated-type-specifier:
26472 namespace N { struct X{}; }
26474 struct A {
26475 template <typename T> friend struct N::X;
26478 However, if the DECL refers to a class type, and we are in
26479 the scope of the class, then the name lookup automatically
26480 finds the TYPE_DECL created by build_self_reference rather
26481 than a TEMPLATE_DECL. For example, in:
26483 template <class T> struct S {
26484 S s;
26487 there is no need to handle such case. */
26489 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26490 return DECL_TEMPLATE_RESULT (decl);
26492 return decl;
26495 /* If too many, or too few, template-parameter lists apply to the
26496 declarator, issue an error message. Returns TRUE if all went well,
26497 and FALSE otherwise. */
26499 static bool
26500 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26501 cp_declarator *declarator,
26502 location_t declarator_location)
26504 switch (declarator->kind)
26506 case cdk_id:
26508 unsigned num_templates = 0;
26509 tree scope = declarator->u.id.qualifying_scope;
26510 bool template_id_p = false;
26512 if (scope)
26513 num_templates = num_template_headers_for_class (scope);
26514 else if (TREE_CODE (declarator->u.id.unqualified_name)
26515 == TEMPLATE_ID_EXPR)
26517 /* If the DECLARATOR has the form `X<y>' then it uses one
26518 additional level of template parameters. */
26519 ++num_templates;
26520 template_id_p = true;
26523 return cp_parser_check_template_parameters
26524 (parser, num_templates, template_id_p, declarator_location,
26525 declarator);
26528 case cdk_function:
26529 case cdk_array:
26530 case cdk_pointer:
26531 case cdk_reference:
26532 case cdk_ptrmem:
26533 return (cp_parser_check_declarator_template_parameters
26534 (parser, declarator->declarator, declarator_location));
26536 case cdk_decomp:
26537 case cdk_error:
26538 return true;
26540 default:
26541 gcc_unreachable ();
26543 return false;
26546 /* NUM_TEMPLATES were used in the current declaration. If that is
26547 invalid, return FALSE and issue an error messages. Otherwise,
26548 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26549 declarator and we can print more accurate diagnostics. */
26551 static bool
26552 cp_parser_check_template_parameters (cp_parser* parser,
26553 unsigned num_templates,
26554 bool template_id_p,
26555 location_t location,
26556 cp_declarator *declarator)
26558 /* If there are the same number of template classes and parameter
26559 lists, that's OK. */
26560 if (parser->num_template_parameter_lists == num_templates)
26561 return true;
26562 /* If there are more, but only one more, and the name ends in an identifier,
26563 then we are declaring a primary template. That's OK too. */
26564 if (!template_id_p
26565 && parser->num_template_parameter_lists == num_templates + 1)
26566 return true;
26567 /* If there are more template classes than parameter lists, we have
26568 something like:
26570 template <class T> void S<T>::R<T>::f (); */
26571 if (parser->num_template_parameter_lists < num_templates)
26573 if (declarator && !current_function_decl)
26574 error_at (location, "specializing member %<%T::%E%> "
26575 "requires %<template<>%> syntax",
26576 declarator->u.id.qualifying_scope,
26577 declarator->u.id.unqualified_name);
26578 else if (declarator)
26579 error_at (location, "invalid declaration of %<%T::%E%>",
26580 declarator->u.id.qualifying_scope,
26581 declarator->u.id.unqualified_name);
26582 else
26583 error_at (location, "too few template-parameter-lists");
26584 return false;
26586 /* Otherwise, there are too many template parameter lists. We have
26587 something like:
26589 template <class T> template <class U> void S::f(); */
26590 error_at (location, "too many template-parameter-lists");
26591 return false;
26594 /* Parse an optional `::' token indicating that the following name is
26595 from the global namespace. If so, PARSER->SCOPE is set to the
26596 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26597 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26598 Returns the new value of PARSER->SCOPE, if the `::' token is
26599 present, and NULL_TREE otherwise. */
26601 static tree
26602 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26604 cp_token *token;
26606 /* Peek at the next token. */
26607 token = cp_lexer_peek_token (parser->lexer);
26608 /* If we're looking at a `::' token then we're starting from the
26609 global namespace, not our current location. */
26610 if (token->type == CPP_SCOPE)
26612 /* Consume the `::' token. */
26613 cp_lexer_consume_token (parser->lexer);
26614 /* Set the SCOPE so that we know where to start the lookup. */
26615 parser->scope = global_namespace;
26616 parser->qualifying_scope = global_namespace;
26617 parser->object_scope = NULL_TREE;
26619 return parser->scope;
26621 else if (!current_scope_valid_p)
26623 parser->scope = NULL_TREE;
26624 parser->qualifying_scope = NULL_TREE;
26625 parser->object_scope = NULL_TREE;
26628 return NULL_TREE;
26631 /* Returns TRUE if the upcoming token sequence is the start of a
26632 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26633 declarator is preceded by the `friend' specifier. */
26635 static bool
26636 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26638 bool constructor_p;
26639 bool outside_class_specifier_p;
26640 tree nested_name_specifier;
26641 cp_token *next_token;
26643 /* The common case is that this is not a constructor declarator, so
26644 try to avoid doing lots of work if at all possible. It's not
26645 valid declare a constructor at function scope. */
26646 if (parser->in_function_body)
26647 return false;
26648 /* And only certain tokens can begin a constructor declarator. */
26649 next_token = cp_lexer_peek_token (parser->lexer);
26650 if (next_token->type != CPP_NAME
26651 && next_token->type != CPP_SCOPE
26652 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26653 && next_token->type != CPP_TEMPLATE_ID)
26654 return false;
26656 /* Parse tentatively; we are going to roll back all of the tokens
26657 consumed here. */
26658 cp_parser_parse_tentatively (parser);
26659 /* Assume that we are looking at a constructor declarator. */
26660 constructor_p = true;
26662 /* Look for the optional `::' operator. */
26663 cp_parser_global_scope_opt (parser,
26664 /*current_scope_valid_p=*/false);
26665 /* Look for the nested-name-specifier. */
26666 nested_name_specifier
26667 = (cp_parser_nested_name_specifier_opt (parser,
26668 /*typename_keyword_p=*/false,
26669 /*check_dependency_p=*/false,
26670 /*type_p=*/false,
26671 /*is_declaration=*/false));
26673 outside_class_specifier_p = (!at_class_scope_p ()
26674 || !TYPE_BEING_DEFINED (current_class_type)
26675 || friend_p);
26677 /* Outside of a class-specifier, there must be a
26678 nested-name-specifier. Except in C++17 mode, where we
26679 might be declaring a guiding declaration. */
26680 if (!nested_name_specifier && outside_class_specifier_p
26681 && cxx_dialect < cxx17)
26682 constructor_p = false;
26683 else if (nested_name_specifier == error_mark_node)
26684 constructor_p = false;
26686 /* If we have a class scope, this is easy; DR 147 says that S::S always
26687 names the constructor, and no other qualified name could. */
26688 if (constructor_p && nested_name_specifier
26689 && CLASS_TYPE_P (nested_name_specifier))
26691 tree id = cp_parser_unqualified_id (parser,
26692 /*template_keyword_p=*/false,
26693 /*check_dependency_p=*/false,
26694 /*declarator_p=*/true,
26695 /*optional_p=*/false);
26696 if (is_overloaded_fn (id))
26697 id = DECL_NAME (get_first_fn (id));
26698 if (!constructor_name_p (id, nested_name_specifier))
26699 constructor_p = false;
26701 /* If we still think that this might be a constructor-declarator,
26702 look for a class-name. */
26703 else if (constructor_p)
26705 /* If we have:
26707 template <typename T> struct S {
26708 S();
26711 we must recognize that the nested `S' names a class. */
26712 if (cxx_dialect >= cxx17)
26713 cp_parser_parse_tentatively (parser);
26715 tree type_decl;
26716 type_decl = cp_parser_class_name (parser,
26717 /*typename_keyword_p=*/false,
26718 /*template_keyword_p=*/false,
26719 none_type,
26720 /*check_dependency_p=*/false,
26721 /*class_head_p=*/false,
26722 /*is_declaration=*/false);
26724 if (cxx_dialect >= cxx17
26725 && !cp_parser_parse_definitely (parser))
26727 type_decl = NULL_TREE;
26728 tree tmpl = cp_parser_template_name (parser,
26729 /*template_keyword*/false,
26730 /*check_dependency_p*/false,
26731 /*is_declaration*/false,
26732 none_type,
26733 /*is_identifier*/NULL);
26734 if (DECL_CLASS_TEMPLATE_P (tmpl)
26735 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26736 /* It's a deduction guide, return true. */;
26737 else
26738 cp_parser_simulate_error (parser);
26741 /* If there was no class-name, then this is not a constructor.
26742 Otherwise, if we are in a class-specifier and we aren't
26743 handling a friend declaration, check that its type matches
26744 current_class_type (c++/38313). Note: error_mark_node
26745 is left alone for error recovery purposes. */
26746 constructor_p = (!cp_parser_error_occurred (parser)
26747 && (outside_class_specifier_p
26748 || type_decl == NULL_TREE
26749 || type_decl == error_mark_node
26750 || same_type_p (current_class_type,
26751 TREE_TYPE (type_decl))));
26753 /* If we're still considering a constructor, we have to see a `(',
26754 to begin the parameter-declaration-clause, followed by either a
26755 `)', an `...', or a decl-specifier. We need to check for a
26756 type-specifier to avoid being fooled into thinking that:
26758 S (f) (int);
26760 is a constructor. (It is actually a function named `f' that
26761 takes one parameter (of type `int') and returns a value of type
26762 `S'. */
26763 if (constructor_p
26764 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26765 constructor_p = false;
26767 if (constructor_p
26768 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26769 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26770 /* A parameter declaration begins with a decl-specifier,
26771 which is either the "attribute" keyword, a storage class
26772 specifier, or (usually) a type-specifier. */
26773 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26775 tree type;
26776 tree pushed_scope = NULL_TREE;
26777 unsigned saved_num_template_parameter_lists;
26779 /* Names appearing in the type-specifier should be looked up
26780 in the scope of the class. */
26781 if (current_class_type)
26782 type = NULL_TREE;
26783 else if (type_decl)
26785 type = TREE_TYPE (type_decl);
26786 if (TREE_CODE (type) == TYPENAME_TYPE)
26788 type = resolve_typename_type (type,
26789 /*only_current_p=*/false);
26790 if (TREE_CODE (type) == TYPENAME_TYPE)
26792 cp_parser_abort_tentative_parse (parser);
26793 return false;
26796 pushed_scope = push_scope (type);
26799 /* Inside the constructor parameter list, surrounding
26800 template-parameter-lists do not apply. */
26801 saved_num_template_parameter_lists
26802 = parser->num_template_parameter_lists;
26803 parser->num_template_parameter_lists = 0;
26805 /* Look for the type-specifier. */
26806 cp_parser_type_specifier (parser,
26807 CP_PARSER_FLAGS_NONE,
26808 /*decl_specs=*/NULL,
26809 /*is_declarator=*/true,
26810 /*declares_class_or_enum=*/NULL,
26811 /*is_cv_qualifier=*/NULL);
26813 parser->num_template_parameter_lists
26814 = saved_num_template_parameter_lists;
26816 /* Leave the scope of the class. */
26817 if (pushed_scope)
26818 pop_scope (pushed_scope);
26820 constructor_p = !cp_parser_error_occurred (parser);
26824 /* We did not really want to consume any tokens. */
26825 cp_parser_abort_tentative_parse (parser);
26827 return constructor_p;
26830 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26831 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26832 they must be performed once we are in the scope of the function.
26834 Returns the function defined. */
26836 static tree
26837 cp_parser_function_definition_from_specifiers_and_declarator
26838 (cp_parser* parser,
26839 cp_decl_specifier_seq *decl_specifiers,
26840 tree attributes,
26841 const cp_declarator *declarator)
26843 tree fn;
26844 bool success_p;
26846 /* Begin the function-definition. */
26847 success_p = start_function (decl_specifiers, declarator, attributes);
26849 /* The things we're about to see are not directly qualified by any
26850 template headers we've seen thus far. */
26851 reset_specialization ();
26853 /* If there were names looked up in the decl-specifier-seq that we
26854 did not check, check them now. We must wait until we are in the
26855 scope of the function to perform the checks, since the function
26856 might be a friend. */
26857 perform_deferred_access_checks (tf_warning_or_error);
26859 if (success_p)
26861 cp_finalize_omp_declare_simd (parser, current_function_decl);
26862 parser->omp_declare_simd = NULL;
26863 cp_finalize_oacc_routine (parser, current_function_decl, true);
26864 parser->oacc_routine = NULL;
26867 if (!success_p)
26869 /* Skip the entire function. */
26870 cp_parser_skip_to_end_of_block_or_statement (parser);
26871 fn = error_mark_node;
26873 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26875 /* Seen already, skip it. An error message has already been output. */
26876 cp_parser_skip_to_end_of_block_or_statement (parser);
26877 fn = current_function_decl;
26878 current_function_decl = NULL_TREE;
26879 /* If this is a function from a class, pop the nested class. */
26880 if (current_class_name)
26881 pop_nested_class ();
26883 else
26885 timevar_id_t tv;
26886 if (DECL_DECLARED_INLINE_P (current_function_decl))
26887 tv = TV_PARSE_INLINE;
26888 else
26889 tv = TV_PARSE_FUNC;
26890 timevar_push (tv);
26891 fn = cp_parser_function_definition_after_declarator (parser,
26892 /*inline_p=*/false);
26893 timevar_pop (tv);
26896 return fn;
26899 /* Parse the part of a function-definition that follows the
26900 declarator. INLINE_P is TRUE iff this function is an inline
26901 function defined within a class-specifier.
26903 Returns the function defined. */
26905 static tree
26906 cp_parser_function_definition_after_declarator (cp_parser* parser,
26907 bool inline_p)
26909 tree fn;
26910 bool saved_in_unbraced_linkage_specification_p;
26911 bool saved_in_function_body;
26912 unsigned saved_num_template_parameter_lists;
26913 cp_token *token;
26914 bool fully_implicit_function_template_p
26915 = parser->fully_implicit_function_template_p;
26916 parser->fully_implicit_function_template_p = false;
26917 tree implicit_template_parms
26918 = parser->implicit_template_parms;
26919 parser->implicit_template_parms = 0;
26920 cp_binding_level* implicit_template_scope
26921 = parser->implicit_template_scope;
26922 parser->implicit_template_scope = 0;
26924 saved_in_function_body = parser->in_function_body;
26925 parser->in_function_body = true;
26926 /* If the next token is `return', then the code may be trying to
26927 make use of the "named return value" extension that G++ used to
26928 support. */
26929 token = cp_lexer_peek_token (parser->lexer);
26930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26932 /* Consume the `return' keyword. */
26933 cp_lexer_consume_token (parser->lexer);
26934 /* Look for the identifier that indicates what value is to be
26935 returned. */
26936 cp_parser_identifier (parser);
26937 /* Issue an error message. */
26938 error_at (token->location,
26939 "named return values are no longer supported");
26940 /* Skip tokens until we reach the start of the function body. */
26941 while (true)
26943 cp_token *token = cp_lexer_peek_token (parser->lexer);
26944 if (token->type == CPP_OPEN_BRACE
26945 || token->type == CPP_EOF
26946 || token->type == CPP_PRAGMA_EOL)
26947 break;
26948 cp_lexer_consume_token (parser->lexer);
26951 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26952 anything declared inside `f'. */
26953 saved_in_unbraced_linkage_specification_p
26954 = parser->in_unbraced_linkage_specification_p;
26955 parser->in_unbraced_linkage_specification_p = false;
26956 /* Inside the function, surrounding template-parameter-lists do not
26957 apply. */
26958 saved_num_template_parameter_lists
26959 = parser->num_template_parameter_lists;
26960 parser->num_template_parameter_lists = 0;
26962 /* If the next token is `try', `__transaction_atomic', or
26963 `__transaction_relaxed`, then we are looking at either function-try-block
26964 or function-transaction-block. Note that all of these include the
26965 function-body. */
26966 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26967 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26968 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26969 RID_TRANSACTION_RELAXED))
26970 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26971 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26972 cp_parser_function_try_block (parser);
26973 else
26974 cp_parser_ctor_initializer_opt_and_function_body
26975 (parser, /*in_function_try_block=*/false);
26977 /* Finish the function. */
26978 fn = finish_function (inline_p);
26979 /* Generate code for it, if necessary. */
26980 expand_or_defer_fn (fn);
26981 /* Restore the saved values. */
26982 parser->in_unbraced_linkage_specification_p
26983 = saved_in_unbraced_linkage_specification_p;
26984 parser->num_template_parameter_lists
26985 = saved_num_template_parameter_lists;
26986 parser->in_function_body = saved_in_function_body;
26988 parser->fully_implicit_function_template_p
26989 = fully_implicit_function_template_p;
26990 parser->implicit_template_parms
26991 = implicit_template_parms;
26992 parser->implicit_template_scope
26993 = implicit_template_scope;
26995 if (parser->fully_implicit_function_template_p)
26996 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26998 return fn;
27001 /* Parse a template-declaration body (following argument list). */
27003 static void
27004 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27005 tree parameter_list,
27006 bool member_p)
27008 tree decl = NULL_TREE;
27009 bool friend_p = false;
27011 /* We just processed one more parameter list. */
27012 ++parser->num_template_parameter_lists;
27014 /* Get the deferred access checks from the parameter list. These
27015 will be checked once we know what is being declared, as for a
27016 member template the checks must be performed in the scope of the
27017 class containing the member. */
27018 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27020 /* Tentatively parse for a new template parameter list, which can either be
27021 the template keyword or a template introduction. */
27022 if (cp_parser_template_declaration_after_export (parser, member_p))
27023 /* OK */;
27024 else if (cxx_dialect >= cxx11
27025 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27026 decl = cp_parser_alias_declaration (parser);
27027 else
27029 /* There are no access checks when parsing a template, as we do not
27030 know if a specialization will be a friend. */
27031 push_deferring_access_checks (dk_no_check);
27032 cp_token *token = cp_lexer_peek_token (parser->lexer);
27033 decl = cp_parser_single_declaration (parser,
27034 checks,
27035 member_p,
27036 /*explicit_specialization_p=*/false,
27037 &friend_p);
27038 pop_deferring_access_checks ();
27040 /* If this is a member template declaration, let the front
27041 end know. */
27042 if (member_p && !friend_p && decl)
27044 if (TREE_CODE (decl) == TYPE_DECL)
27045 cp_parser_check_access_in_redeclaration (decl, token->location);
27047 decl = finish_member_template_decl (decl);
27049 else if (friend_p && decl
27050 && DECL_DECLARES_TYPE_P (decl))
27051 make_friend_class (current_class_type, TREE_TYPE (decl),
27052 /*complain=*/true);
27054 /* We are done with the current parameter list. */
27055 --parser->num_template_parameter_lists;
27057 pop_deferring_access_checks ();
27059 /* Finish up. */
27060 finish_template_decl (parameter_list);
27062 /* Check the template arguments for a literal operator template. */
27063 if (decl
27064 && DECL_DECLARES_FUNCTION_P (decl)
27065 && UDLIT_OPER_P (DECL_NAME (decl)))
27067 bool ok = true;
27068 if (parameter_list == NULL_TREE)
27069 ok = false;
27070 else
27072 int num_parms = TREE_VEC_LENGTH (parameter_list);
27073 if (num_parms == 1)
27075 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27076 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27077 if (TREE_TYPE (parm) != char_type_node
27078 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27079 ok = false;
27081 else if (num_parms == 2 && cxx_dialect >= cxx14)
27083 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27084 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27085 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27086 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27087 if (parm == error_mark_node
27088 || TREE_TYPE (parm) != TREE_TYPE (type)
27089 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27090 ok = false;
27092 else
27093 ok = false;
27095 if (!ok)
27097 if (cxx_dialect >= cxx14)
27098 error ("literal operator template %qD has invalid parameter list."
27099 " Expected non-type template argument pack <char...>"
27100 " or <typename CharT, CharT...>",
27101 decl);
27102 else
27103 error ("literal operator template %qD has invalid parameter list."
27104 " Expected non-type template argument pack <char...>",
27105 decl);
27109 /* Register member declarations. */
27110 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27111 finish_member_declaration (decl);
27112 /* If DECL is a function template, we must return to parse it later.
27113 (Even though there is no definition, there might be default
27114 arguments that need handling.) */
27115 if (member_p && decl
27116 && DECL_DECLARES_FUNCTION_P (decl))
27117 vec_safe_push (unparsed_funs_with_definitions, decl);
27120 /* Parse a template introduction header for a template-declaration. Returns
27121 false if tentative parse fails. */
27123 static bool
27124 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27126 cp_parser_parse_tentatively (parser);
27128 tree saved_scope = parser->scope;
27129 tree saved_object_scope = parser->object_scope;
27130 tree saved_qualifying_scope = parser->qualifying_scope;
27132 /* Look for the optional `::' operator. */
27133 cp_parser_global_scope_opt (parser,
27134 /*current_scope_valid_p=*/false);
27135 /* Look for the nested-name-specifier. */
27136 cp_parser_nested_name_specifier_opt (parser,
27137 /*typename_keyword_p=*/false,
27138 /*check_dependency_p=*/true,
27139 /*type_p=*/false,
27140 /*is_declaration=*/false);
27142 cp_token *token = cp_lexer_peek_token (parser->lexer);
27143 tree concept_name = cp_parser_identifier (parser);
27145 /* Look up the concept for which we will be matching
27146 template parameters. */
27147 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27148 token->location);
27149 parser->scope = saved_scope;
27150 parser->object_scope = saved_object_scope;
27151 parser->qualifying_scope = saved_qualifying_scope;
27153 if (concept_name == error_mark_node)
27154 cp_parser_simulate_error (parser);
27156 /* Look for opening brace for introduction. */
27157 matching_braces braces;
27158 braces.require_open (parser);
27160 if (!cp_parser_parse_definitely (parser))
27161 return false;
27163 push_deferring_access_checks (dk_deferred);
27165 /* Build vector of placeholder parameters and grab
27166 matching identifiers. */
27167 tree introduction_list = cp_parser_introduction_list (parser);
27169 /* The introduction-list shall not be empty. */
27170 int nargs = TREE_VEC_LENGTH (introduction_list);
27171 if (nargs == 0)
27173 error ("empty introduction-list");
27174 return true;
27177 /* Look for closing brace for introduction. */
27178 if (!braces.require_close (parser))
27179 return true;
27181 if (tmpl_decl == error_mark_node)
27183 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27184 token->location);
27185 return true;
27188 /* Build and associate the constraint. */
27189 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27190 if (parms && parms != error_mark_node)
27192 cp_parser_template_declaration_after_parameters (parser, parms,
27193 member_p);
27194 return true;
27197 error_at (token->location, "no matching concept for template-introduction");
27198 return true;
27201 /* Parse a normal template-declaration following the template keyword. */
27203 static void
27204 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27206 tree parameter_list;
27207 bool need_lang_pop;
27208 location_t location = input_location;
27210 /* Look for the `<' token. */
27211 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27212 return;
27213 if (at_class_scope_p () && current_function_decl)
27215 /* 14.5.2.2 [temp.mem]
27217 A local class shall not have member templates. */
27218 error_at (location,
27219 "invalid declaration of member template in local class");
27220 cp_parser_skip_to_end_of_block_or_statement (parser);
27221 return;
27223 /* [temp]
27225 A template ... shall not have C linkage. */
27226 if (current_lang_name == lang_name_c)
27228 error_at (location, "template with C linkage");
27229 maybe_show_extern_c_location ();
27230 /* Give it C++ linkage to avoid confusing other parts of the
27231 front end. */
27232 push_lang_context (lang_name_cplusplus);
27233 need_lang_pop = true;
27235 else
27236 need_lang_pop = false;
27238 /* We cannot perform access checks on the template parameter
27239 declarations until we know what is being declared, just as we
27240 cannot check the decl-specifier list. */
27241 push_deferring_access_checks (dk_deferred);
27243 /* If the next token is `>', then we have an invalid
27244 specialization. Rather than complain about an invalid template
27245 parameter, issue an error message here. */
27246 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27248 cp_parser_error (parser, "invalid explicit specialization");
27249 begin_specialization ();
27250 parameter_list = NULL_TREE;
27252 else
27254 /* Parse the template parameters. */
27255 parameter_list = cp_parser_template_parameter_list (parser);
27258 /* Look for the `>'. */
27259 cp_parser_skip_to_end_of_template_parameter_list (parser);
27261 /* Manage template requirements */
27262 if (flag_concepts)
27264 tree reqs = get_shorthand_constraints (current_template_parms);
27265 if (tree r = cp_parser_requires_clause_opt (parser))
27266 reqs = conjoin_constraints (reqs, normalize_expression (r));
27267 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27270 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27271 member_p);
27273 /* For the erroneous case of a template with C linkage, we pushed an
27274 implicit C++ linkage scope; exit that scope now. */
27275 if (need_lang_pop)
27276 pop_lang_context ();
27279 /* Parse a template-declaration, assuming that the `export' (and
27280 `extern') keywords, if present, has already been scanned. MEMBER_P
27281 is as for cp_parser_template_declaration. */
27283 static bool
27284 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27286 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27288 cp_lexer_consume_token (parser->lexer);
27289 cp_parser_explicit_template_declaration (parser, member_p);
27290 return true;
27292 else if (flag_concepts)
27293 return cp_parser_template_introduction (parser, member_p);
27295 return false;
27298 /* Perform the deferred access checks from a template-parameter-list.
27299 CHECKS is a TREE_LIST of access checks, as returned by
27300 get_deferred_access_checks. */
27302 static void
27303 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27305 ++processing_template_parmlist;
27306 perform_access_checks (checks, tf_warning_or_error);
27307 --processing_template_parmlist;
27310 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27311 `function-definition' sequence that follows a template header.
27312 If MEMBER_P is true, this declaration appears in a class scope.
27314 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27315 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27317 static tree
27318 cp_parser_single_declaration (cp_parser* parser,
27319 vec<deferred_access_check, va_gc> *checks,
27320 bool member_p,
27321 bool explicit_specialization_p,
27322 bool* friend_p)
27324 int declares_class_or_enum;
27325 tree decl = NULL_TREE;
27326 cp_decl_specifier_seq decl_specifiers;
27327 bool function_definition_p = false;
27328 cp_token *decl_spec_token_start;
27330 /* This function is only used when processing a template
27331 declaration. */
27332 gcc_assert (innermost_scope_kind () == sk_template_parms
27333 || innermost_scope_kind () == sk_template_spec);
27335 /* Defer access checks until we know what is being declared. */
27336 push_deferring_access_checks (dk_deferred);
27338 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27339 alternative. */
27340 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27341 cp_parser_decl_specifier_seq (parser,
27342 CP_PARSER_FLAGS_OPTIONAL,
27343 &decl_specifiers,
27344 &declares_class_or_enum);
27345 if (friend_p)
27346 *friend_p = cp_parser_friend_p (&decl_specifiers);
27348 /* There are no template typedefs. */
27349 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27351 error_at (decl_spec_token_start->location,
27352 "template declaration of %<typedef%>");
27353 decl = error_mark_node;
27356 /* Gather up the access checks that occurred the
27357 decl-specifier-seq. */
27358 stop_deferring_access_checks ();
27360 /* Check for the declaration of a template class. */
27361 if (declares_class_or_enum)
27363 if (cp_parser_declares_only_class_p (parser)
27364 || (declares_class_or_enum & 2))
27366 // If this is a declaration, but not a definition, associate
27367 // any constraints with the type declaration. Constraints
27368 // are associated with definitions in cp_parser_class_specifier.
27369 if (declares_class_or_enum == 1)
27370 associate_classtype_constraints (decl_specifiers.type);
27372 decl = shadow_tag (&decl_specifiers);
27374 /* In this case:
27376 struct C {
27377 friend template <typename T> struct A<T>::B;
27380 A<T>::B will be represented by a TYPENAME_TYPE, and
27381 therefore not recognized by shadow_tag. */
27382 if (friend_p && *friend_p
27383 && !decl
27384 && decl_specifiers.type
27385 && TYPE_P (decl_specifiers.type))
27386 decl = decl_specifiers.type;
27388 if (decl && decl != error_mark_node)
27389 decl = TYPE_NAME (decl);
27390 else
27391 decl = error_mark_node;
27393 /* Perform access checks for template parameters. */
27394 cp_parser_perform_template_parameter_access_checks (checks);
27396 /* Give a helpful diagnostic for
27397 template <class T> struct A { } a;
27398 if we aren't already recovering from an error. */
27399 if (!cp_parser_declares_only_class_p (parser)
27400 && !seen_error ())
27402 error_at (cp_lexer_peek_token (parser->lexer)->location,
27403 "a class template declaration must not declare "
27404 "anything else");
27405 cp_parser_skip_to_end_of_block_or_statement (parser);
27406 goto out;
27411 /* Complain about missing 'typename' or other invalid type names. */
27412 if (!decl_specifiers.any_type_specifiers_p
27413 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27415 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27416 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27417 the rest of this declaration. */
27418 decl = error_mark_node;
27419 goto out;
27422 /* If it's not a template class, try for a template function. If
27423 the next token is a `;', then this declaration does not declare
27424 anything. But, if there were errors in the decl-specifiers, then
27425 the error might well have come from an attempted class-specifier.
27426 In that case, there's no need to warn about a missing declarator. */
27427 if (!decl
27428 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27429 || decl_specifiers.type != error_mark_node))
27431 decl = cp_parser_init_declarator (parser,
27432 &decl_specifiers,
27433 checks,
27434 /*function_definition_allowed_p=*/true,
27435 member_p,
27436 declares_class_or_enum,
27437 &function_definition_p,
27438 NULL, NULL, NULL);
27440 /* 7.1.1-1 [dcl.stc]
27442 A storage-class-specifier shall not be specified in an explicit
27443 specialization... */
27444 if (decl
27445 && explicit_specialization_p
27446 && decl_specifiers.storage_class != sc_none)
27448 error_at (decl_spec_token_start->location,
27449 "explicit template specialization cannot have a storage class");
27450 decl = error_mark_node;
27453 if (decl && VAR_P (decl))
27454 check_template_variable (decl);
27457 /* Look for a trailing `;' after the declaration. */
27458 if (!function_definition_p
27459 && (decl == error_mark_node
27460 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27461 cp_parser_skip_to_end_of_block_or_statement (parser);
27463 out:
27464 pop_deferring_access_checks ();
27466 /* Clear any current qualification; whatever comes next is the start
27467 of something new. */
27468 parser->scope = NULL_TREE;
27469 parser->qualifying_scope = NULL_TREE;
27470 parser->object_scope = NULL_TREE;
27472 return decl;
27475 /* Parse a cast-expression that is not the operand of a unary "&". */
27477 static cp_expr
27478 cp_parser_simple_cast_expression (cp_parser *parser)
27480 return cp_parser_cast_expression (parser, /*address_p=*/false,
27481 /*cast_p=*/false, /*decltype*/false, NULL);
27484 /* Parse a functional cast to TYPE. Returns an expression
27485 representing the cast. */
27487 static cp_expr
27488 cp_parser_functional_cast (cp_parser* parser, tree type)
27490 vec<tree, va_gc> *vec;
27491 tree expression_list;
27492 cp_expr cast;
27493 bool nonconst_p;
27495 location_t start_loc = input_location;
27497 if (!type)
27498 type = error_mark_node;
27500 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27502 cp_lexer_set_source_position (parser->lexer);
27503 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27504 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27505 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27506 if (TREE_CODE (type) == TYPE_DECL)
27507 type = TREE_TYPE (type);
27509 cast = finish_compound_literal (type, expression_list,
27510 tf_warning_or_error, fcl_functional);
27511 /* Create a location of the form:
27512 type_name{i, f}
27513 ^~~~~~~~~~~~~~~
27514 with caret == start at the start of the type name,
27515 finishing at the closing brace. */
27516 location_t finish_loc
27517 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27518 location_t combined_loc = make_location (start_loc, start_loc,
27519 finish_loc);
27520 cast.set_location (combined_loc);
27521 return cast;
27525 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27526 /*cast_p=*/true,
27527 /*allow_expansion_p=*/true,
27528 /*non_constant_p=*/NULL);
27529 if (vec == NULL)
27530 expression_list = error_mark_node;
27531 else
27533 expression_list = build_tree_list_vec (vec);
27534 release_tree_vector (vec);
27537 cast = build_functional_cast (type, expression_list,
27538 tf_warning_or_error);
27539 /* [expr.const]/1: In an integral constant expression "only type
27540 conversions to integral or enumeration type can be used". */
27541 if (TREE_CODE (type) == TYPE_DECL)
27542 type = TREE_TYPE (type);
27543 if (cast != error_mark_node
27544 && !cast_valid_in_integral_constant_expression_p (type)
27545 && cp_parser_non_integral_constant_expression (parser,
27546 NIC_CONSTRUCTOR))
27547 return error_mark_node;
27549 /* Create a location of the form:
27550 float(i)
27551 ^~~~~~~~
27552 with caret == start at the start of the type name,
27553 finishing at the closing paren. */
27554 location_t finish_loc
27555 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27556 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27557 cast.set_location (combined_loc);
27558 return cast;
27561 /* Save the tokens that make up the body of a member function defined
27562 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27563 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27564 specifiers applied to the declaration. Returns the FUNCTION_DECL
27565 for the member function. */
27567 static tree
27568 cp_parser_save_member_function_body (cp_parser* parser,
27569 cp_decl_specifier_seq *decl_specifiers,
27570 cp_declarator *declarator,
27571 tree attributes)
27573 cp_token *first;
27574 cp_token *last;
27575 tree fn;
27576 bool function_try_block = false;
27578 /* Create the FUNCTION_DECL. */
27579 fn = grokmethod (decl_specifiers, declarator, attributes);
27580 cp_finalize_omp_declare_simd (parser, fn);
27581 cp_finalize_oacc_routine (parser, fn, true);
27582 /* If something went badly wrong, bail out now. */
27583 if (fn == error_mark_node)
27585 /* If there's a function-body, skip it. */
27586 if (cp_parser_token_starts_function_definition_p
27587 (cp_lexer_peek_token (parser->lexer)))
27588 cp_parser_skip_to_end_of_block_or_statement (parser);
27589 return error_mark_node;
27592 /* Remember it, if there default args to post process. */
27593 cp_parser_save_default_args (parser, fn);
27595 /* Save away the tokens that make up the body of the
27596 function. */
27597 first = parser->lexer->next_token;
27599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27600 cp_lexer_consume_token (parser->lexer);
27601 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27602 RID_TRANSACTION_ATOMIC))
27604 cp_lexer_consume_token (parser->lexer);
27605 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27607 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27608 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27609 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27610 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27611 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27613 cp_lexer_consume_token (parser->lexer);
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);
27619 else
27620 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27621 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27623 cp_lexer_consume_token (parser->lexer);
27624 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27625 break;
27629 /* Handle function try blocks. */
27630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27632 cp_lexer_consume_token (parser->lexer);
27633 function_try_block = true;
27635 /* We can have braced-init-list mem-initializers before the fn body. */
27636 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27638 cp_lexer_consume_token (parser->lexer);
27639 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27641 /* cache_group will stop after an un-nested { } pair, too. */
27642 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27643 break;
27645 /* variadic mem-inits have ... after the ')'. */
27646 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27647 cp_lexer_consume_token (parser->lexer);
27650 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27651 /* Handle function try blocks. */
27652 if (function_try_block)
27653 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27654 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27655 last = parser->lexer->next_token;
27657 /* Save away the inline definition; we will process it when the
27658 class is complete. */
27659 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27660 DECL_PENDING_INLINE_P (fn) = 1;
27662 /* We need to know that this was defined in the class, so that
27663 friend templates are handled correctly. */
27664 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27666 /* Add FN to the queue of functions to be parsed later. */
27667 vec_safe_push (unparsed_funs_with_definitions, fn);
27669 return fn;
27672 /* Save the tokens that make up the in-class initializer for a non-static
27673 data member. Returns a DEFAULT_ARG. */
27675 static tree
27676 cp_parser_save_nsdmi (cp_parser* parser)
27678 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27681 /* Parse a template-argument-list, as well as the trailing ">" (but
27682 not the opening "<"). See cp_parser_template_argument_list for the
27683 return value. */
27685 static tree
27686 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27688 tree arguments;
27689 tree saved_scope;
27690 tree saved_qualifying_scope;
27691 tree saved_object_scope;
27692 bool saved_greater_than_is_operator_p;
27693 int saved_unevaluated_operand;
27694 int saved_inhibit_evaluation_warnings;
27696 /* [temp.names]
27698 When parsing a template-id, the first non-nested `>' is taken as
27699 the end of the template-argument-list rather than a greater-than
27700 operator. */
27701 saved_greater_than_is_operator_p
27702 = parser->greater_than_is_operator_p;
27703 parser->greater_than_is_operator_p = false;
27704 /* Parsing the argument list may modify SCOPE, so we save it
27705 here. */
27706 saved_scope = parser->scope;
27707 saved_qualifying_scope = parser->qualifying_scope;
27708 saved_object_scope = parser->object_scope;
27709 /* We need to evaluate the template arguments, even though this
27710 template-id may be nested within a "sizeof". */
27711 saved_unevaluated_operand = cp_unevaluated_operand;
27712 cp_unevaluated_operand = 0;
27713 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27714 c_inhibit_evaluation_warnings = 0;
27715 /* Parse the template-argument-list itself. */
27716 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27717 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27718 arguments = NULL_TREE;
27719 else
27720 arguments = cp_parser_template_argument_list (parser);
27721 /* Look for the `>' that ends the template-argument-list. If we find
27722 a '>>' instead, it's probably just a typo. */
27723 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27725 if (cxx_dialect != cxx98)
27727 /* In C++0x, a `>>' in a template argument list or cast
27728 expression is considered to be two separate `>'
27729 tokens. So, change the current token to a `>', but don't
27730 consume it: it will be consumed later when the outer
27731 template argument list (or cast expression) is parsed.
27732 Note that this replacement of `>' for `>>' is necessary
27733 even if we are parsing tentatively: in the tentative
27734 case, after calling
27735 cp_parser_enclosed_template_argument_list we will always
27736 throw away all of the template arguments and the first
27737 closing `>', either because the template argument list
27738 was erroneous or because we are replacing those tokens
27739 with a CPP_TEMPLATE_ID token. The second `>' (which will
27740 not have been thrown away) is needed either to close an
27741 outer template argument list or to complete a new-style
27742 cast. */
27743 cp_token *token = cp_lexer_peek_token (parser->lexer);
27744 token->type = CPP_GREATER;
27746 else if (!saved_greater_than_is_operator_p)
27748 /* If we're in a nested template argument list, the '>>' has
27749 to be a typo for '> >'. We emit the error message, but we
27750 continue parsing and we push a '>' as next token, so that
27751 the argument list will be parsed correctly. Note that the
27752 global source location is still on the token before the
27753 '>>', so we need to say explicitly where we want it. */
27754 cp_token *token = cp_lexer_peek_token (parser->lexer);
27755 gcc_rich_location richloc (token->location);
27756 richloc.add_fixit_replace ("> >");
27757 error_at (&richloc, "%<>>%> should be %<> >%> "
27758 "within a nested template argument list");
27760 token->type = CPP_GREATER;
27762 else
27764 /* If this is not a nested template argument list, the '>>'
27765 is a typo for '>'. Emit an error message and continue.
27766 Same deal about the token location, but here we can get it
27767 right by consuming the '>>' before issuing the diagnostic. */
27768 cp_token *token = cp_lexer_consume_token (parser->lexer);
27769 error_at (token->location,
27770 "spurious %<>>%>, use %<>%> to terminate "
27771 "a template argument list");
27774 else
27775 cp_parser_skip_to_end_of_template_parameter_list (parser);
27776 /* The `>' token might be a greater-than operator again now. */
27777 parser->greater_than_is_operator_p
27778 = saved_greater_than_is_operator_p;
27779 /* Restore the SAVED_SCOPE. */
27780 parser->scope = saved_scope;
27781 parser->qualifying_scope = saved_qualifying_scope;
27782 parser->object_scope = saved_object_scope;
27783 cp_unevaluated_operand = saved_unevaluated_operand;
27784 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27786 return arguments;
27789 /* MEMBER_FUNCTION is a member function, or a friend. If default
27790 arguments, or the body of the function have not yet been parsed,
27791 parse them now. */
27793 static void
27794 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27796 timevar_push (TV_PARSE_INMETH);
27797 /* If this member is a template, get the underlying
27798 FUNCTION_DECL. */
27799 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27800 member_function = DECL_TEMPLATE_RESULT (member_function);
27802 /* There should not be any class definitions in progress at this
27803 point; the bodies of members are only parsed outside of all class
27804 definitions. */
27805 gcc_assert (parser->num_classes_being_defined == 0);
27806 /* While we're parsing the member functions we might encounter more
27807 classes. We want to handle them right away, but we don't want
27808 them getting mixed up with functions that are currently in the
27809 queue. */
27810 push_unparsed_function_queues (parser);
27812 /* Make sure that any template parameters are in scope. */
27813 maybe_begin_member_template_processing (member_function);
27815 /* If the body of the function has not yet been parsed, parse it
27816 now. */
27817 if (DECL_PENDING_INLINE_P (member_function))
27819 tree function_scope;
27820 cp_token_cache *tokens;
27822 /* The function is no longer pending; we are processing it. */
27823 tokens = DECL_PENDING_INLINE_INFO (member_function);
27824 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27825 DECL_PENDING_INLINE_P (member_function) = 0;
27827 /* If this is a local class, enter the scope of the containing
27828 function. */
27829 function_scope = current_function_decl;
27830 if (function_scope)
27831 push_function_context ();
27833 /* Push the body of the function onto the lexer stack. */
27834 cp_parser_push_lexer_for_tokens (parser, tokens);
27836 /* Let the front end know that we going to be defining this
27837 function. */
27838 start_preparsed_function (member_function, NULL_TREE,
27839 SF_PRE_PARSED | SF_INCLASS_INLINE);
27841 /* Don't do access checking if it is a templated function. */
27842 if (processing_template_decl)
27843 push_deferring_access_checks (dk_no_check);
27845 /* #pragma omp declare reduction needs special parsing. */
27846 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27848 parser->lexer->in_pragma = true;
27849 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27850 finish_function (/*inline_p=*/true);
27851 cp_check_omp_declare_reduction (member_function);
27853 else
27854 /* Now, parse the body of the function. */
27855 cp_parser_function_definition_after_declarator (parser,
27856 /*inline_p=*/true);
27858 if (processing_template_decl)
27859 pop_deferring_access_checks ();
27861 /* Leave the scope of the containing function. */
27862 if (function_scope)
27863 pop_function_context ();
27864 cp_parser_pop_lexer (parser);
27867 /* Remove any template parameters from the symbol table. */
27868 maybe_end_member_template_processing ();
27870 /* Restore the queue. */
27871 pop_unparsed_function_queues (parser);
27872 timevar_pop (TV_PARSE_INMETH);
27875 /* If DECL contains any default args, remember it on the unparsed
27876 functions queue. */
27878 static void
27879 cp_parser_save_default_args (cp_parser* parser, tree decl)
27881 tree probe;
27883 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27884 probe;
27885 probe = TREE_CHAIN (probe))
27886 if (TREE_PURPOSE (probe))
27888 cp_default_arg_entry entry = {current_class_type, decl};
27889 vec_safe_push (unparsed_funs_with_default_args, entry);
27890 break;
27894 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27895 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27896 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27897 from the parameter-type-list. */
27899 static tree
27900 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27901 tree default_arg, tree parmtype)
27903 cp_token_cache *tokens;
27904 tree parsed_arg;
27905 bool dummy;
27907 if (default_arg == error_mark_node)
27908 return error_mark_node;
27910 /* Push the saved tokens for the default argument onto the parser's
27911 lexer stack. */
27912 tokens = DEFARG_TOKENS (default_arg);
27913 cp_parser_push_lexer_for_tokens (parser, tokens);
27915 start_lambda_scope (decl);
27917 /* Parse the default argument. */
27918 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27919 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27920 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27922 finish_lambda_scope ();
27924 if (parsed_arg == error_mark_node)
27925 cp_parser_skip_to_end_of_statement (parser);
27927 if (!processing_template_decl)
27929 /* In a non-template class, check conversions now. In a template,
27930 we'll wait and instantiate these as needed. */
27931 if (TREE_CODE (decl) == PARM_DECL)
27932 parsed_arg = check_default_argument (parmtype, parsed_arg,
27933 tf_warning_or_error);
27934 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27935 parsed_arg = error_mark_node;
27936 else
27937 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27940 /* If the token stream has not been completely used up, then
27941 there was extra junk after the end of the default
27942 argument. */
27943 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27945 if (TREE_CODE (decl) == PARM_DECL)
27946 cp_parser_error (parser, "expected %<,%>");
27947 else
27948 cp_parser_error (parser, "expected %<;%>");
27951 /* Revert to the main lexer. */
27952 cp_parser_pop_lexer (parser);
27954 return parsed_arg;
27957 /* FIELD is a non-static data member with an initializer which we saved for
27958 later; parse it now. */
27960 static void
27961 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27963 tree def;
27965 maybe_begin_member_template_processing (field);
27967 push_unparsed_function_queues (parser);
27968 def = cp_parser_late_parse_one_default_arg (parser, field,
27969 DECL_INITIAL (field),
27970 NULL_TREE);
27971 pop_unparsed_function_queues (parser);
27973 maybe_end_member_template_processing ();
27975 DECL_INITIAL (field) = def;
27978 /* FN is a FUNCTION_DECL which may contains a parameter with an
27979 unparsed DEFAULT_ARG. Parse the default args now. This function
27980 assumes that the current scope is the scope in which the default
27981 argument should be processed. */
27983 static void
27984 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27986 bool saved_local_variables_forbidden_p;
27987 tree parm, parmdecl;
27989 /* While we're parsing the default args, we might (due to the
27990 statement expression extension) encounter more classes. We want
27991 to handle them right away, but we don't want them getting mixed
27992 up with default args that are currently in the queue. */
27993 push_unparsed_function_queues (parser);
27995 /* Local variable names (and the `this' keyword) may not appear
27996 in a default argument. */
27997 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27998 parser->local_variables_forbidden_p = true;
28000 push_defarg_context (fn);
28002 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28003 parmdecl = DECL_ARGUMENTS (fn);
28004 parm && parm != void_list_node;
28005 parm = TREE_CHAIN (parm),
28006 parmdecl = DECL_CHAIN (parmdecl))
28008 tree default_arg = TREE_PURPOSE (parm);
28009 tree parsed_arg;
28010 vec<tree, va_gc> *insts;
28011 tree copy;
28012 unsigned ix;
28014 if (!default_arg)
28015 continue;
28017 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28018 /* This can happen for a friend declaration for a function
28019 already declared with default arguments. */
28020 continue;
28022 parsed_arg
28023 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28024 default_arg,
28025 TREE_VALUE (parm));
28026 TREE_PURPOSE (parm) = parsed_arg;
28028 /* Update any instantiations we've already created. */
28029 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28030 vec_safe_iterate (insts, ix, &copy); ix++)
28031 TREE_PURPOSE (copy) = parsed_arg;
28034 pop_defarg_context ();
28036 /* Make sure no default arg is missing. */
28037 check_default_args (fn);
28039 /* Restore the state of local_variables_forbidden_p. */
28040 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28042 /* Restore the queue. */
28043 pop_unparsed_function_queues (parser);
28046 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28048 sizeof ... ( identifier )
28050 where the 'sizeof' token has already been consumed. */
28052 static tree
28053 cp_parser_sizeof_pack (cp_parser *parser)
28055 /* Consume the `...'. */
28056 cp_lexer_consume_token (parser->lexer);
28057 maybe_warn_variadic_templates ();
28059 matching_parens parens;
28060 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28061 if (paren)
28062 parens.consume_open (parser);
28063 else
28064 permerror (cp_lexer_peek_token (parser->lexer)->location,
28065 "%<sizeof...%> argument must be surrounded by parentheses");
28067 cp_token *token = cp_lexer_peek_token (parser->lexer);
28068 tree name = cp_parser_identifier (parser);
28069 if (name == error_mark_node)
28070 return error_mark_node;
28071 /* The name is not qualified. */
28072 parser->scope = NULL_TREE;
28073 parser->qualifying_scope = NULL_TREE;
28074 parser->object_scope = NULL_TREE;
28075 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28076 if (expr == error_mark_node)
28077 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28078 token->location);
28079 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28080 expr = TREE_TYPE (expr);
28081 else if (TREE_CODE (expr) == CONST_DECL)
28082 expr = DECL_INITIAL (expr);
28083 expr = make_pack_expansion (expr);
28084 PACK_EXPANSION_SIZEOF_P (expr) = true;
28086 if (paren)
28087 parens.require_close (parser);
28089 return expr;
28092 /* Parse the operand of `sizeof' (or a similar operator). Returns
28093 either a TYPE or an expression, depending on the form of the
28094 input. The KEYWORD indicates which kind of expression we have
28095 encountered. */
28097 static tree
28098 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28100 tree expr = NULL_TREE;
28101 const char *saved_message;
28102 char *tmp;
28103 bool saved_integral_constant_expression_p;
28104 bool saved_non_integral_constant_expression_p;
28106 /* If it's a `...', then we are computing the length of a parameter
28107 pack. */
28108 if (keyword == RID_SIZEOF
28109 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28110 return cp_parser_sizeof_pack (parser);
28112 /* Types cannot be defined in a `sizeof' expression. Save away the
28113 old message. */
28114 saved_message = parser->type_definition_forbidden_message;
28115 /* And create the new one. */
28116 tmp = concat ("types may not be defined in %<",
28117 IDENTIFIER_POINTER (ridpointers[keyword]),
28118 "%> expressions", NULL);
28119 parser->type_definition_forbidden_message = tmp;
28121 /* The restrictions on constant-expressions do not apply inside
28122 sizeof expressions. */
28123 saved_integral_constant_expression_p
28124 = parser->integral_constant_expression_p;
28125 saved_non_integral_constant_expression_p
28126 = parser->non_integral_constant_expression_p;
28127 parser->integral_constant_expression_p = false;
28129 /* Do not actually evaluate the expression. */
28130 ++cp_unevaluated_operand;
28131 ++c_inhibit_evaluation_warnings;
28132 /* If it's a `(', then we might be looking at the type-id
28133 construction. */
28134 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28136 tree type = NULL_TREE;
28138 /* We can't be sure yet whether we're looking at a type-id or an
28139 expression. */
28140 cp_parser_parse_tentatively (parser);
28142 matching_parens parens;
28143 parens.consume_open (parser);
28145 /* Note: as a GNU Extension, compound literals are considered
28146 postfix-expressions as they are in C99, so they are valid
28147 arguments to sizeof. See comment in cp_parser_cast_expression
28148 for details. */
28149 if (cp_parser_compound_literal_p (parser))
28150 cp_parser_simulate_error (parser);
28151 else
28153 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28154 parser->in_type_id_in_expr_p = true;
28155 /* Look for the type-id. */
28156 type = cp_parser_type_id (parser);
28157 /* Look for the closing `)'. */
28158 parens.require_close (parser);
28159 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28162 /* If all went well, then we're done. */
28163 if (cp_parser_parse_definitely (parser))
28165 cp_decl_specifier_seq decl_specs;
28167 /* Build a trivial decl-specifier-seq. */
28168 clear_decl_specs (&decl_specs);
28169 decl_specs.type = type;
28171 /* Call grokdeclarator to figure out what type this is. */
28172 expr = grokdeclarator (NULL,
28173 &decl_specs,
28174 TYPENAME,
28175 /*initialized=*/0,
28176 /*attrlist=*/NULL);
28180 /* If the type-id production did not work out, then we must be
28181 looking at the unary-expression production. */
28182 if (!expr)
28183 expr = cp_parser_unary_expression (parser);
28185 /* Go back to evaluating expressions. */
28186 --cp_unevaluated_operand;
28187 --c_inhibit_evaluation_warnings;
28189 /* Free the message we created. */
28190 free (tmp);
28191 /* And restore the old one. */
28192 parser->type_definition_forbidden_message = saved_message;
28193 parser->integral_constant_expression_p
28194 = saved_integral_constant_expression_p;
28195 parser->non_integral_constant_expression_p
28196 = saved_non_integral_constant_expression_p;
28198 return expr;
28201 /* If the current declaration has no declarator, return true. */
28203 static bool
28204 cp_parser_declares_only_class_p (cp_parser *parser)
28206 /* If the next token is a `;' or a `,' then there is no
28207 declarator. */
28208 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28209 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28212 /* Update the DECL_SPECS to reflect the storage class indicated by
28213 KEYWORD. */
28215 static void
28216 cp_parser_set_storage_class (cp_parser *parser,
28217 cp_decl_specifier_seq *decl_specs,
28218 enum rid keyword,
28219 cp_token *token)
28221 cp_storage_class storage_class;
28223 if (parser->in_unbraced_linkage_specification_p)
28225 error_at (token->location, "invalid use of %qD in linkage specification",
28226 ridpointers[keyword]);
28227 return;
28229 else if (decl_specs->storage_class != sc_none)
28231 decl_specs->conflicting_specifiers_p = true;
28232 return;
28235 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28236 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28237 && decl_specs->gnu_thread_keyword_p)
28239 pedwarn (decl_specs->locations[ds_thread], 0,
28240 "%<__thread%> before %qD", ridpointers[keyword]);
28243 switch (keyword)
28245 case RID_AUTO:
28246 storage_class = sc_auto;
28247 break;
28248 case RID_REGISTER:
28249 storage_class = sc_register;
28250 break;
28251 case RID_STATIC:
28252 storage_class = sc_static;
28253 break;
28254 case RID_EXTERN:
28255 storage_class = sc_extern;
28256 break;
28257 case RID_MUTABLE:
28258 storage_class = sc_mutable;
28259 break;
28260 default:
28261 gcc_unreachable ();
28263 decl_specs->storage_class = storage_class;
28264 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28266 /* A storage class specifier cannot be applied alongside a typedef
28267 specifier. If there is a typedef specifier present then set
28268 conflicting_specifiers_p which will trigger an error later
28269 on in grokdeclarator. */
28270 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28271 decl_specs->conflicting_specifiers_p = true;
28274 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28275 is true, the type is a class or enum definition. */
28277 static void
28278 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28279 tree type_spec,
28280 cp_token *token,
28281 bool type_definition_p)
28283 decl_specs->any_specifiers_p = true;
28285 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28286 (with, for example, in "typedef int wchar_t;") we remember that
28287 this is what happened. In system headers, we ignore these
28288 declarations so that G++ can work with system headers that are not
28289 C++-safe. */
28290 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28291 && !type_definition_p
28292 && (type_spec == boolean_type_node
28293 || type_spec == char16_type_node
28294 || type_spec == char32_type_node
28295 || type_spec == wchar_type_node)
28296 && (decl_specs->type
28297 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28298 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28299 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28300 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28302 decl_specs->redefined_builtin_type = type_spec;
28303 set_and_check_decl_spec_loc (decl_specs,
28304 ds_redefined_builtin_type_spec,
28305 token);
28306 if (!decl_specs->type)
28308 decl_specs->type = type_spec;
28309 decl_specs->type_definition_p = false;
28310 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28313 else if (decl_specs->type)
28314 decl_specs->multiple_types_p = true;
28315 else
28317 decl_specs->type = type_spec;
28318 decl_specs->type_definition_p = type_definition_p;
28319 decl_specs->redefined_builtin_type = NULL_TREE;
28320 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28324 /* True iff TOKEN is the GNU keyword __thread. */
28326 static bool
28327 token_is__thread (cp_token *token)
28329 gcc_assert (token->keyword == RID_THREAD);
28330 return id_equal (token->u.value, "__thread");
28333 /* Set the location for a declarator specifier and check if it is
28334 duplicated.
28336 DECL_SPECS is the sequence of declarator specifiers onto which to
28337 set the location.
28339 DS is the single declarator specifier to set which location is to
28340 be set onto the existing sequence of declarators.
28342 LOCATION is the location for the declarator specifier to
28343 consider. */
28345 static void
28346 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28347 cp_decl_spec ds, cp_token *token)
28349 gcc_assert (ds < ds_last);
28351 if (decl_specs == NULL)
28352 return;
28354 source_location location = token->location;
28356 if (decl_specs->locations[ds] == 0)
28358 decl_specs->locations[ds] = location;
28359 if (ds == ds_thread)
28360 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28362 else
28364 if (ds == ds_long)
28366 if (decl_specs->locations[ds_long_long] != 0)
28367 error_at (location,
28368 "%<long long long%> is too long for GCC");
28369 else
28371 decl_specs->locations[ds_long_long] = location;
28372 pedwarn_cxx98 (location,
28373 OPT_Wlong_long,
28374 "ISO C++ 1998 does not support %<long long%>");
28377 else if (ds == ds_thread)
28379 bool gnu = token_is__thread (token);
28380 if (gnu != decl_specs->gnu_thread_keyword_p)
28381 error_at (location,
28382 "both %<__thread%> and %<thread_local%> specified");
28383 else
28385 gcc_rich_location richloc (location);
28386 richloc.add_fixit_remove ();
28387 error_at (&richloc, "duplicate %qD", token->u.value);
28390 else
28392 static const char *const decl_spec_names[] = {
28393 "signed",
28394 "unsigned",
28395 "short",
28396 "long",
28397 "const",
28398 "volatile",
28399 "restrict",
28400 "inline",
28401 "virtual",
28402 "explicit",
28403 "friend",
28404 "typedef",
28405 "using",
28406 "constexpr",
28407 "__complex"
28409 gcc_rich_location richloc (location);
28410 richloc.add_fixit_remove ();
28411 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28416 /* Return true iff the declarator specifier DS is present in the
28417 sequence of declarator specifiers DECL_SPECS. */
28419 bool
28420 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28421 cp_decl_spec ds)
28423 gcc_assert (ds < ds_last);
28425 if (decl_specs == NULL)
28426 return false;
28428 return decl_specs->locations[ds] != 0;
28431 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28432 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28434 static bool
28435 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28437 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28440 /* Issue an error message indicating that TOKEN_DESC was expected.
28441 If KEYWORD is true, it indicated this function is called by
28442 cp_parser_require_keword and the required token can only be
28443 a indicated keyword.
28445 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28446 within any error as the location of an "opening" token matching
28447 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28448 RT_CLOSE_PAREN). */
28450 static void
28451 cp_parser_required_error (cp_parser *parser,
28452 required_token token_desc,
28453 bool keyword,
28454 location_t matching_location)
28456 if (cp_parser_simulate_error (parser))
28457 return;
28459 const char *gmsgid = NULL;
28460 switch (token_desc)
28462 case RT_NEW:
28463 gmsgid = G_("expected %<new%>");
28464 break;
28465 case RT_DELETE:
28466 gmsgid = G_("expected %<delete%>");
28467 break;
28468 case RT_RETURN:
28469 gmsgid = G_("expected %<return%>");
28470 break;
28471 case RT_WHILE:
28472 gmsgid = G_("expected %<while%>");
28473 break;
28474 case RT_EXTERN:
28475 gmsgid = G_("expected %<extern%>");
28476 break;
28477 case RT_STATIC_ASSERT:
28478 gmsgid = G_("expected %<static_assert%>");
28479 break;
28480 case RT_DECLTYPE:
28481 gmsgid = G_("expected %<decltype%>");
28482 break;
28483 case RT_OPERATOR:
28484 gmsgid = G_("expected %<operator%>");
28485 break;
28486 case RT_CLASS:
28487 gmsgid = G_("expected %<class%>");
28488 break;
28489 case RT_TEMPLATE:
28490 gmsgid = G_("expected %<template%>");
28491 break;
28492 case RT_NAMESPACE:
28493 gmsgid = G_("expected %<namespace%>");
28494 break;
28495 case RT_USING:
28496 gmsgid = G_("expected %<using%>");
28497 break;
28498 case RT_ASM:
28499 gmsgid = G_("expected %<asm%>");
28500 break;
28501 case RT_TRY:
28502 gmsgid = G_("expected %<try%>");
28503 break;
28504 case RT_CATCH:
28505 gmsgid = G_("expected %<catch%>");
28506 break;
28507 case RT_THROW:
28508 gmsgid = G_("expected %<throw%>");
28509 break;
28510 case RT_LABEL:
28511 gmsgid = G_("expected %<__label__%>");
28512 break;
28513 case RT_AT_TRY:
28514 gmsgid = G_("expected %<@try%>");
28515 break;
28516 case RT_AT_SYNCHRONIZED:
28517 gmsgid = G_("expected %<@synchronized%>");
28518 break;
28519 case RT_AT_THROW:
28520 gmsgid = G_("expected %<@throw%>");
28521 break;
28522 case RT_TRANSACTION_ATOMIC:
28523 gmsgid = G_("expected %<__transaction_atomic%>");
28524 break;
28525 case RT_TRANSACTION_RELAXED:
28526 gmsgid = G_("expected %<__transaction_relaxed%>");
28527 break;
28528 default:
28529 break;
28532 if (!gmsgid && !keyword)
28534 switch (token_desc)
28536 case RT_SEMICOLON:
28537 gmsgid = G_("expected %<;%>");
28538 break;
28539 case RT_OPEN_PAREN:
28540 gmsgid = G_("expected %<(%>");
28541 break;
28542 case RT_CLOSE_BRACE:
28543 gmsgid = G_("expected %<}%>");
28544 break;
28545 case RT_OPEN_BRACE:
28546 gmsgid = G_("expected %<{%>");
28547 break;
28548 case RT_CLOSE_SQUARE:
28549 gmsgid = G_("expected %<]%>");
28550 break;
28551 case RT_OPEN_SQUARE:
28552 gmsgid = G_("expected %<[%>");
28553 break;
28554 case RT_COMMA:
28555 gmsgid = G_("expected %<,%>");
28556 break;
28557 case RT_SCOPE:
28558 gmsgid = G_("expected %<::%>");
28559 break;
28560 case RT_LESS:
28561 gmsgid = G_("expected %<<%>");
28562 break;
28563 case RT_GREATER:
28564 gmsgid = G_("expected %<>%>");
28565 break;
28566 case RT_EQ:
28567 gmsgid = G_("expected %<=%>");
28568 break;
28569 case RT_ELLIPSIS:
28570 gmsgid = G_("expected %<...%>");
28571 break;
28572 case RT_MULT:
28573 gmsgid = G_("expected %<*%>");
28574 break;
28575 case RT_COMPL:
28576 gmsgid = G_("expected %<~%>");
28577 break;
28578 case RT_COLON:
28579 gmsgid = G_("expected %<:%>");
28580 break;
28581 case RT_COLON_SCOPE:
28582 gmsgid = G_("expected %<:%> or %<::%>");
28583 break;
28584 case RT_CLOSE_PAREN:
28585 gmsgid = G_("expected %<)%>");
28586 break;
28587 case RT_COMMA_CLOSE_PAREN:
28588 gmsgid = G_("expected %<,%> or %<)%>");
28589 break;
28590 case RT_PRAGMA_EOL:
28591 gmsgid = G_("expected end of line");
28592 break;
28593 case RT_NAME:
28594 gmsgid = G_("expected identifier");
28595 break;
28596 case RT_SELECT:
28597 gmsgid = G_("expected selection-statement");
28598 break;
28599 case RT_ITERATION:
28600 gmsgid = G_("expected iteration-statement");
28601 break;
28602 case RT_JUMP:
28603 gmsgid = G_("expected jump-statement");
28604 break;
28605 case RT_CLASS_KEY:
28606 gmsgid = G_("expected class-key");
28607 break;
28608 case RT_CLASS_TYPENAME_TEMPLATE:
28609 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28610 break;
28611 default:
28612 gcc_unreachable ();
28616 if (gmsgid)
28617 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28621 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28622 issue an error message indicating that TOKEN_DESC was expected.
28624 Returns the token consumed, if the token had the appropriate type.
28625 Otherwise, returns NULL.
28627 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28628 within any error as the location of an "opening" token matching
28629 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28630 RT_CLOSE_PAREN). */
28632 static cp_token *
28633 cp_parser_require (cp_parser* parser,
28634 enum cpp_ttype type,
28635 required_token token_desc,
28636 location_t matching_location)
28638 if (cp_lexer_next_token_is (parser->lexer, type))
28639 return cp_lexer_consume_token (parser->lexer);
28640 else
28642 /* Output the MESSAGE -- unless we're parsing tentatively. */
28643 if (!cp_parser_simulate_error (parser))
28644 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28645 matching_location);
28646 return NULL;
28650 /* An error message is produced if the next token is not '>'.
28651 All further tokens are skipped until the desired token is
28652 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28654 static void
28655 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28657 /* Current level of '< ... >'. */
28658 unsigned level = 0;
28659 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28660 unsigned nesting_depth = 0;
28662 /* Are we ready, yet? If not, issue error message. */
28663 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28664 return;
28666 /* Skip tokens until the desired token is found. */
28667 while (true)
28669 /* Peek at the next token. */
28670 switch (cp_lexer_peek_token (parser->lexer)->type)
28672 case CPP_LESS:
28673 if (!nesting_depth)
28674 ++level;
28675 break;
28677 case CPP_RSHIFT:
28678 if (cxx_dialect == cxx98)
28679 /* C++0x views the `>>' operator as two `>' tokens, but
28680 C++98 does not. */
28681 break;
28682 else if (!nesting_depth && level-- == 0)
28684 /* We've hit a `>>' where the first `>' closes the
28685 template argument list, and the second `>' is
28686 spurious. Just consume the `>>' and stop; we've
28687 already produced at least one error. */
28688 cp_lexer_consume_token (parser->lexer);
28689 return;
28691 /* Fall through for C++0x, so we handle the second `>' in
28692 the `>>'. */
28693 gcc_fallthrough ();
28695 case CPP_GREATER:
28696 if (!nesting_depth && level-- == 0)
28698 /* We've reached the token we want, consume it and stop. */
28699 cp_lexer_consume_token (parser->lexer);
28700 return;
28702 break;
28704 case CPP_OPEN_PAREN:
28705 case CPP_OPEN_SQUARE:
28706 ++nesting_depth;
28707 break;
28709 case CPP_CLOSE_PAREN:
28710 case CPP_CLOSE_SQUARE:
28711 if (nesting_depth-- == 0)
28712 return;
28713 break;
28715 case CPP_EOF:
28716 case CPP_PRAGMA_EOL:
28717 case CPP_SEMICOLON:
28718 case CPP_OPEN_BRACE:
28719 case CPP_CLOSE_BRACE:
28720 /* The '>' was probably forgotten, don't look further. */
28721 return;
28723 default:
28724 break;
28727 /* Consume this token. */
28728 cp_lexer_consume_token (parser->lexer);
28732 /* If the next token is the indicated keyword, consume it. Otherwise,
28733 issue an error message indicating that TOKEN_DESC was expected.
28735 Returns the token consumed, if the token had the appropriate type.
28736 Otherwise, returns NULL. */
28738 static cp_token *
28739 cp_parser_require_keyword (cp_parser* parser,
28740 enum rid keyword,
28741 required_token token_desc)
28743 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28745 if (token && token->keyword != keyword)
28747 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28748 UNKNOWN_LOCATION);
28749 return NULL;
28752 return token;
28755 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28756 function-definition. */
28758 static bool
28759 cp_parser_token_starts_function_definition_p (cp_token* token)
28761 return (/* An ordinary function-body begins with an `{'. */
28762 token->type == CPP_OPEN_BRACE
28763 /* A ctor-initializer begins with a `:'. */
28764 || token->type == CPP_COLON
28765 /* A function-try-block begins with `try'. */
28766 || token->keyword == RID_TRY
28767 /* A function-transaction-block begins with `__transaction_atomic'
28768 or `__transaction_relaxed'. */
28769 || token->keyword == RID_TRANSACTION_ATOMIC
28770 || token->keyword == RID_TRANSACTION_RELAXED
28771 /* The named return value extension begins with `return'. */
28772 || token->keyword == RID_RETURN);
28775 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28776 definition. */
28778 static bool
28779 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28781 cp_token *token;
28783 token = cp_lexer_peek_token (parser->lexer);
28784 return (token->type == CPP_OPEN_BRACE
28785 || (token->type == CPP_COLON
28786 && !parser->colon_doesnt_start_class_def_p));
28789 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28790 C++0x) ending a template-argument. */
28792 static bool
28793 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28795 cp_token *token;
28797 token = cp_lexer_peek_token (parser->lexer);
28798 return (token->type == CPP_COMMA
28799 || token->type == CPP_GREATER
28800 || token->type == CPP_ELLIPSIS
28801 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28804 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28805 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28807 static bool
28808 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28809 size_t n)
28811 cp_token *token;
28813 token = cp_lexer_peek_nth_token (parser->lexer, n);
28814 if (token->type == CPP_LESS)
28815 return true;
28816 /* Check for the sequence `<::' in the original code. It would be lexed as
28817 `[:', where `[' is a digraph, and there is no whitespace before
28818 `:'. */
28819 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28821 cp_token *token2;
28822 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28823 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28824 return true;
28826 return false;
28829 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28830 or none_type otherwise. */
28832 static enum tag_types
28833 cp_parser_token_is_class_key (cp_token* token)
28835 switch (token->keyword)
28837 case RID_CLASS:
28838 return class_type;
28839 case RID_STRUCT:
28840 return record_type;
28841 case RID_UNION:
28842 return union_type;
28844 default:
28845 return none_type;
28849 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28850 or none_type otherwise or if the token is null. */
28852 static enum tag_types
28853 cp_parser_token_is_type_parameter_key (cp_token* token)
28855 if (!token)
28856 return none_type;
28858 switch (token->keyword)
28860 case RID_CLASS:
28861 return class_type;
28862 case RID_TYPENAME:
28863 return typename_type;
28865 default:
28866 return none_type;
28870 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28872 static void
28873 cp_parser_check_class_key (enum tag_types class_key, tree type)
28875 if (type == error_mark_node)
28876 return;
28877 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28879 if (permerror (input_location, "%qs tag used in naming %q#T",
28880 class_key == union_type ? "union"
28881 : class_key == record_type ? "struct" : "class",
28882 type))
28883 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28884 "%q#T was previously declared here", type);
28888 /* Issue an error message if DECL is redeclared with different
28889 access than its original declaration [class.access.spec/3].
28890 This applies to nested classes, nested class templates and
28891 enumerations [class.mem/1]. */
28893 static void
28894 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28896 if (!decl
28897 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28898 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28899 return;
28901 if ((TREE_PRIVATE (decl)
28902 != (current_access_specifier == access_private_node))
28903 || (TREE_PROTECTED (decl)
28904 != (current_access_specifier == access_protected_node)))
28905 error_at (location, "%qD redeclared with different access", decl);
28908 /* Look for the `template' keyword, as a syntactic disambiguator.
28909 Return TRUE iff it is present, in which case it will be
28910 consumed. */
28912 static bool
28913 cp_parser_optional_template_keyword (cp_parser *parser)
28915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28917 /* In C++98 the `template' keyword can only be used within templates;
28918 outside templates the parser can always figure out what is a
28919 template and what is not. In C++11, per the resolution of DR 468,
28920 `template' is allowed in cases where it is not strictly necessary. */
28921 if (!processing_template_decl
28922 && pedantic && cxx_dialect == cxx98)
28924 cp_token *token = cp_lexer_peek_token (parser->lexer);
28925 pedwarn (token->location, OPT_Wpedantic,
28926 "in C++98 %<template%> (as a disambiguator) is only "
28927 "allowed within templates");
28928 /* If this part of the token stream is rescanned, the same
28929 error message would be generated. So, we purge the token
28930 from the stream. */
28931 cp_lexer_purge_token (parser->lexer);
28932 return false;
28934 else
28936 /* Consume the `template' keyword. */
28937 cp_lexer_consume_token (parser->lexer);
28938 return true;
28941 return false;
28944 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28945 set PARSER->SCOPE, and perform other related actions. */
28947 static void
28948 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28950 struct tree_check *check_value;
28952 /* Get the stored value. */
28953 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28954 /* Set the scope from the stored value. */
28955 parser->scope = saved_checks_value (check_value);
28956 parser->qualifying_scope = check_value->qualifying_scope;
28957 parser->object_scope = NULL_TREE;
28960 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28961 encounter the end of a block before what we were looking for. */
28963 static bool
28964 cp_parser_cache_group (cp_parser *parser,
28965 enum cpp_ttype end,
28966 unsigned depth)
28968 while (true)
28970 cp_token *token = cp_lexer_peek_token (parser->lexer);
28972 /* Abort a parenthesized expression if we encounter a semicolon. */
28973 if ((end == CPP_CLOSE_PAREN || depth == 0)
28974 && token->type == CPP_SEMICOLON)
28975 return true;
28976 /* If we've reached the end of the file, stop. */
28977 if (token->type == CPP_EOF
28978 || (end != CPP_PRAGMA_EOL
28979 && token->type == CPP_PRAGMA_EOL))
28980 return true;
28981 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28982 /* We've hit the end of an enclosing block, so there's been some
28983 kind of syntax error. */
28984 return true;
28986 /* Consume the token. */
28987 cp_lexer_consume_token (parser->lexer);
28988 /* See if it starts a new group. */
28989 if (token->type == CPP_OPEN_BRACE)
28991 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28992 /* In theory this should probably check end == '}', but
28993 cp_parser_save_member_function_body needs it to exit
28994 after either '}' or ')' when called with ')'. */
28995 if (depth == 0)
28996 return false;
28998 else if (token->type == CPP_OPEN_PAREN)
29000 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29001 if (depth == 0 && end == CPP_CLOSE_PAREN)
29002 return false;
29004 else if (token->type == CPP_PRAGMA)
29005 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29006 else if (token->type == end)
29007 return false;
29011 /* Like above, for caching a default argument or NSDMI. Both of these are
29012 terminated by a non-nested comma, but it can be unclear whether or not a
29013 comma is nested in a template argument list unless we do more parsing.
29014 In order to handle this ambiguity, when we encounter a ',' after a '<'
29015 we try to parse what follows as a parameter-declaration-list (in the
29016 case of a default argument) or a member-declarator (in the case of an
29017 NSDMI). If that succeeds, then we stop caching. */
29019 static tree
29020 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29022 unsigned depth = 0;
29023 int maybe_template_id = 0;
29024 cp_token *first_token;
29025 cp_token *token;
29026 tree default_argument;
29028 /* Add tokens until we have processed the entire default
29029 argument. We add the range [first_token, token). */
29030 first_token = cp_lexer_peek_token (parser->lexer);
29031 if (first_token->type == CPP_OPEN_BRACE)
29033 /* For list-initialization, this is straightforward. */
29034 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29035 token = cp_lexer_peek_token (parser->lexer);
29037 else while (true)
29039 bool done = false;
29041 /* Peek at the next token. */
29042 token = cp_lexer_peek_token (parser->lexer);
29043 /* What we do depends on what token we have. */
29044 switch (token->type)
29046 /* In valid code, a default argument must be
29047 immediately followed by a `,' `)', or `...'. */
29048 case CPP_COMMA:
29049 if (depth == 0 && maybe_template_id)
29051 /* If we've seen a '<', we might be in a
29052 template-argument-list. Until Core issue 325 is
29053 resolved, we don't know how this situation ought
29054 to be handled, so try to DTRT. We check whether
29055 what comes after the comma is a valid parameter
29056 declaration list. If it is, then the comma ends
29057 the default argument; otherwise the default
29058 argument continues. */
29059 bool error = false;
29060 cp_token *peek;
29062 /* Set ITALP so cp_parser_parameter_declaration_list
29063 doesn't decide to commit to this parse. */
29064 bool saved_italp = parser->in_template_argument_list_p;
29065 parser->in_template_argument_list_p = true;
29067 cp_parser_parse_tentatively (parser);
29069 if (nsdmi)
29071 /* Parse declarators until we reach a non-comma or
29072 somthing that cannot be an initializer.
29073 Just checking whether we're looking at a single
29074 declarator is insufficient. Consider:
29075 int var = tuple<T,U>::x;
29076 The template parameter 'U' looks exactly like a
29077 declarator. */
29080 int ctor_dtor_or_conv_p;
29081 cp_lexer_consume_token (parser->lexer);
29082 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29083 &ctor_dtor_or_conv_p,
29084 /*parenthesized_p=*/NULL,
29085 /*member_p=*/true,
29086 /*friend_p=*/false);
29087 peek = cp_lexer_peek_token (parser->lexer);
29088 if (cp_parser_error_occurred (parser))
29089 break;
29091 while (peek->type == CPP_COMMA);
29092 /* If we met an '=' or ';' then the original comma
29093 was the end of the NSDMI. Otherwise assume
29094 we're still in the NSDMI. */
29095 error = (peek->type != CPP_EQ
29096 && peek->type != CPP_SEMICOLON);
29098 else
29100 cp_lexer_consume_token (parser->lexer);
29101 begin_scope (sk_function_parms, NULL_TREE);
29102 if (cp_parser_parameter_declaration_list (parser)
29103 == error_mark_node)
29104 error = true;
29105 pop_bindings_and_leave_scope ();
29107 if (!cp_parser_error_occurred (parser) && !error)
29108 done = true;
29109 cp_parser_abort_tentative_parse (parser);
29111 parser->in_template_argument_list_p = saved_italp;
29112 break;
29114 /* FALLTHRU */
29115 case CPP_CLOSE_PAREN:
29116 case CPP_ELLIPSIS:
29117 /* If we run into a non-nested `;', `}', or `]',
29118 then the code is invalid -- but the default
29119 argument is certainly over. */
29120 case CPP_SEMICOLON:
29121 case CPP_CLOSE_BRACE:
29122 case CPP_CLOSE_SQUARE:
29123 if (depth == 0
29124 /* Handle correctly int n = sizeof ... ( p ); */
29125 && token->type != CPP_ELLIPSIS)
29126 done = true;
29127 /* Update DEPTH, if necessary. */
29128 else if (token->type == CPP_CLOSE_PAREN
29129 || token->type == CPP_CLOSE_BRACE
29130 || token->type == CPP_CLOSE_SQUARE)
29131 --depth;
29132 break;
29134 case CPP_OPEN_PAREN:
29135 case CPP_OPEN_SQUARE:
29136 case CPP_OPEN_BRACE:
29137 ++depth;
29138 break;
29140 case CPP_LESS:
29141 if (depth == 0)
29142 /* This might be the comparison operator, or it might
29143 start a template argument list. */
29144 ++maybe_template_id;
29145 break;
29147 case CPP_RSHIFT:
29148 if (cxx_dialect == cxx98)
29149 break;
29150 /* Fall through for C++0x, which treats the `>>'
29151 operator like two `>' tokens in certain
29152 cases. */
29153 gcc_fallthrough ();
29155 case CPP_GREATER:
29156 if (depth == 0)
29158 /* This might be an operator, or it might close a
29159 template argument list. But if a previous '<'
29160 started a template argument list, this will have
29161 closed it, so we can't be in one anymore. */
29162 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29163 if (maybe_template_id < 0)
29164 maybe_template_id = 0;
29166 break;
29168 /* If we run out of tokens, issue an error message. */
29169 case CPP_EOF:
29170 case CPP_PRAGMA_EOL:
29171 error_at (token->location, "file ends in default argument");
29172 return error_mark_node;
29174 case CPP_NAME:
29175 case CPP_SCOPE:
29176 /* In these cases, we should look for template-ids.
29177 For example, if the default argument is
29178 `X<int, double>()', we need to do name lookup to
29179 figure out whether or not `X' is a template; if
29180 so, the `,' does not end the default argument.
29182 That is not yet done. */
29183 break;
29185 default:
29186 break;
29189 /* If we've reached the end, stop. */
29190 if (done)
29191 break;
29193 /* Add the token to the token block. */
29194 token = cp_lexer_consume_token (parser->lexer);
29197 /* Create a DEFAULT_ARG to represent the unparsed default
29198 argument. */
29199 default_argument = make_node (DEFAULT_ARG);
29200 DEFARG_TOKENS (default_argument)
29201 = cp_token_cache_new (first_token, token);
29202 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29204 return default_argument;
29207 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29209 location_t
29210 defarg_location (tree default_argument)
29212 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29213 location_t start = tokens->first->location;
29214 location_t end = tokens->last->location;
29215 return make_location (start, start, end);
29218 /* Begin parsing tentatively. We always save tokens while parsing
29219 tentatively so that if the tentative parsing fails we can restore the
29220 tokens. */
29222 static void
29223 cp_parser_parse_tentatively (cp_parser* parser)
29225 /* Enter a new parsing context. */
29226 parser->context = cp_parser_context_new (parser->context);
29227 /* Begin saving tokens. */
29228 cp_lexer_save_tokens (parser->lexer);
29229 /* In order to avoid repetitive access control error messages,
29230 access checks are queued up until we are no longer parsing
29231 tentatively. */
29232 push_deferring_access_checks (dk_deferred);
29235 /* Commit to the currently active tentative parse. */
29237 static void
29238 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29240 cp_parser_context *context;
29241 cp_lexer *lexer;
29243 /* Mark all of the levels as committed. */
29244 lexer = parser->lexer;
29245 for (context = parser->context; context->next; context = context->next)
29247 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29248 break;
29249 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29250 while (!cp_lexer_saving_tokens (lexer))
29251 lexer = lexer->next;
29252 cp_lexer_commit_tokens (lexer);
29256 /* Commit to the topmost currently active tentative parse.
29258 Note that this function shouldn't be called when there are
29259 irreversible side-effects while in a tentative state. For
29260 example, we shouldn't create a permanent entry in the symbol
29261 table, or issue an error message that might not apply if the
29262 tentative parse is aborted. */
29264 static void
29265 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29267 cp_parser_context *context = parser->context;
29268 cp_lexer *lexer = parser->lexer;
29270 if (context)
29272 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29273 return;
29274 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29276 while (!cp_lexer_saving_tokens (lexer))
29277 lexer = lexer->next;
29278 cp_lexer_commit_tokens (lexer);
29282 /* Abort the currently active tentative parse. All consumed tokens
29283 will be rolled back, and no diagnostics will be issued. */
29285 static void
29286 cp_parser_abort_tentative_parse (cp_parser* parser)
29288 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29289 || errorcount > 0);
29290 cp_parser_simulate_error (parser);
29291 /* Now, pretend that we want to see if the construct was
29292 successfully parsed. */
29293 cp_parser_parse_definitely (parser);
29296 /* Stop parsing tentatively. If a parse error has occurred, restore the
29297 token stream. Otherwise, commit to the tokens we have consumed.
29298 Returns true if no error occurred; false otherwise. */
29300 static bool
29301 cp_parser_parse_definitely (cp_parser* parser)
29303 bool error_occurred;
29304 cp_parser_context *context;
29306 /* Remember whether or not an error occurred, since we are about to
29307 destroy that information. */
29308 error_occurred = cp_parser_error_occurred (parser);
29309 /* Remove the topmost context from the stack. */
29310 context = parser->context;
29311 parser->context = context->next;
29312 /* If no parse errors occurred, commit to the tentative parse. */
29313 if (!error_occurred)
29315 /* Commit to the tokens read tentatively, unless that was
29316 already done. */
29317 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29318 cp_lexer_commit_tokens (parser->lexer);
29320 pop_to_parent_deferring_access_checks ();
29322 /* Otherwise, if errors occurred, roll back our state so that things
29323 are just as they were before we began the tentative parse. */
29324 else
29326 cp_lexer_rollback_tokens (parser->lexer);
29327 pop_deferring_access_checks ();
29329 /* Add the context to the front of the free list. */
29330 context->next = cp_parser_context_free_list;
29331 cp_parser_context_free_list = context;
29333 return !error_occurred;
29336 /* Returns true if we are parsing tentatively and are not committed to
29337 this tentative parse. */
29339 static bool
29340 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29342 return (cp_parser_parsing_tentatively (parser)
29343 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29346 /* Returns nonzero iff an error has occurred during the most recent
29347 tentative parse. */
29349 static bool
29350 cp_parser_error_occurred (cp_parser* parser)
29352 return (cp_parser_parsing_tentatively (parser)
29353 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29356 /* Returns nonzero if GNU extensions are allowed. */
29358 static bool
29359 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29361 return parser->allow_gnu_extensions_p;
29364 /* Objective-C++ Productions */
29367 /* Parse an Objective-C expression, which feeds into a primary-expression
29368 above.
29370 objc-expression:
29371 objc-message-expression
29372 objc-string-literal
29373 objc-encode-expression
29374 objc-protocol-expression
29375 objc-selector-expression
29377 Returns a tree representation of the expression. */
29379 static cp_expr
29380 cp_parser_objc_expression (cp_parser* parser)
29382 /* Try to figure out what kind of declaration is present. */
29383 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29385 switch (kwd->type)
29387 case CPP_OPEN_SQUARE:
29388 return cp_parser_objc_message_expression (parser);
29390 case CPP_OBJC_STRING:
29391 kwd = cp_lexer_consume_token (parser->lexer);
29392 return objc_build_string_object (kwd->u.value);
29394 case CPP_KEYWORD:
29395 switch (kwd->keyword)
29397 case RID_AT_ENCODE:
29398 return cp_parser_objc_encode_expression (parser);
29400 case RID_AT_PROTOCOL:
29401 return cp_parser_objc_protocol_expression (parser);
29403 case RID_AT_SELECTOR:
29404 return cp_parser_objc_selector_expression (parser);
29406 default:
29407 break;
29409 /* FALLTHRU */
29410 default:
29411 error_at (kwd->location,
29412 "misplaced %<@%D%> Objective-C++ construct",
29413 kwd->u.value);
29414 cp_parser_skip_to_end_of_block_or_statement (parser);
29417 return error_mark_node;
29420 /* Parse an Objective-C message expression.
29422 objc-message-expression:
29423 [ objc-message-receiver objc-message-args ]
29425 Returns a representation of an Objective-C message. */
29427 static tree
29428 cp_parser_objc_message_expression (cp_parser* parser)
29430 tree receiver, messageargs;
29432 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29433 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29434 receiver = cp_parser_objc_message_receiver (parser);
29435 messageargs = cp_parser_objc_message_args (parser);
29436 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29437 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29439 tree result = objc_build_message_expr (receiver, messageargs);
29441 /* Construct a location e.g.
29442 [self func1:5]
29443 ^~~~~~~~~~~~~~
29444 ranging from the '[' to the ']', with the caret at the start. */
29445 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29446 protected_set_expr_location (result, combined_loc);
29448 return result;
29451 /* Parse an objc-message-receiver.
29453 objc-message-receiver:
29454 expression
29455 simple-type-specifier
29457 Returns a representation of the type or expression. */
29459 static tree
29460 cp_parser_objc_message_receiver (cp_parser* parser)
29462 tree rcv;
29464 /* An Objective-C message receiver may be either (1) a type
29465 or (2) an expression. */
29466 cp_parser_parse_tentatively (parser);
29467 rcv = cp_parser_expression (parser);
29469 /* If that worked out, fine. */
29470 if (cp_parser_parse_definitely (parser))
29471 return rcv;
29473 cp_parser_parse_tentatively (parser);
29474 rcv = cp_parser_simple_type_specifier (parser,
29475 /*decl_specs=*/NULL,
29476 CP_PARSER_FLAGS_NONE);
29478 if (cp_parser_parse_definitely (parser))
29479 return objc_get_class_reference (rcv);
29481 cp_parser_error (parser, "objective-c++ message receiver expected");
29482 return error_mark_node;
29485 /* Parse the arguments and selectors comprising an Objective-C message.
29487 objc-message-args:
29488 objc-selector
29489 objc-selector-args
29490 objc-selector-args , objc-comma-args
29492 objc-selector-args:
29493 objc-selector [opt] : assignment-expression
29494 objc-selector-args objc-selector [opt] : assignment-expression
29496 objc-comma-args:
29497 assignment-expression
29498 objc-comma-args , assignment-expression
29500 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29501 selector arguments and TREE_VALUE containing a list of comma
29502 arguments. */
29504 static tree
29505 cp_parser_objc_message_args (cp_parser* parser)
29507 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29508 bool maybe_unary_selector_p = true;
29509 cp_token *token = cp_lexer_peek_token (parser->lexer);
29511 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29513 tree selector = NULL_TREE, arg;
29515 if (token->type != CPP_COLON)
29516 selector = cp_parser_objc_selector (parser);
29518 /* Detect if we have a unary selector. */
29519 if (maybe_unary_selector_p
29520 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29521 return build_tree_list (selector, NULL_TREE);
29523 maybe_unary_selector_p = false;
29524 cp_parser_require (parser, CPP_COLON, RT_COLON);
29525 arg = cp_parser_assignment_expression (parser);
29527 sel_args
29528 = chainon (sel_args,
29529 build_tree_list (selector, arg));
29531 token = cp_lexer_peek_token (parser->lexer);
29534 /* Handle non-selector arguments, if any. */
29535 while (token->type == CPP_COMMA)
29537 tree arg;
29539 cp_lexer_consume_token (parser->lexer);
29540 arg = cp_parser_assignment_expression (parser);
29542 addl_args
29543 = chainon (addl_args,
29544 build_tree_list (NULL_TREE, arg));
29546 token = cp_lexer_peek_token (parser->lexer);
29549 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29551 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29552 return build_tree_list (error_mark_node, error_mark_node);
29555 return build_tree_list (sel_args, addl_args);
29558 /* Parse an Objective-C encode expression.
29560 objc-encode-expression:
29561 @encode objc-typename
29563 Returns an encoded representation of the type argument. */
29565 static cp_expr
29566 cp_parser_objc_encode_expression (cp_parser* parser)
29568 tree type;
29569 cp_token *token;
29570 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29572 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29573 matching_parens parens;
29574 parens.require_open (parser);
29575 token = cp_lexer_peek_token (parser->lexer);
29576 type = complete_type (cp_parser_type_id (parser));
29577 parens.require_close (parser);
29579 if (!type)
29581 error_at (token->location,
29582 "%<@encode%> must specify a type as an argument");
29583 return error_mark_node;
29586 /* This happens if we find @encode(T) (where T is a template
29587 typename or something dependent on a template typename) when
29588 parsing a template. In that case, we can't compile it
29589 immediately, but we rather create an AT_ENCODE_EXPR which will
29590 need to be instantiated when the template is used.
29592 if (dependent_type_p (type))
29594 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29595 TREE_READONLY (value) = 1;
29596 return value;
29600 /* Build a location of the form:
29601 @encode(int)
29602 ^~~~~~~~~~~~
29603 with caret==start at the @ token, finishing at the close paren. */
29604 location_t combined_loc
29605 = make_location (start_loc, start_loc,
29606 cp_lexer_previous_token (parser->lexer)->location);
29608 return cp_expr (objc_build_encode_expr (type), combined_loc);
29611 /* Parse an Objective-C @defs expression. */
29613 static tree
29614 cp_parser_objc_defs_expression (cp_parser *parser)
29616 tree name;
29618 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29619 matching_parens parens;
29620 parens.require_open (parser);
29621 name = cp_parser_identifier (parser);
29622 parens.require_close (parser);
29624 return objc_get_class_ivars (name);
29627 /* Parse an Objective-C protocol expression.
29629 objc-protocol-expression:
29630 @protocol ( identifier )
29632 Returns a representation of the protocol expression. */
29634 static tree
29635 cp_parser_objc_protocol_expression (cp_parser* parser)
29637 tree proto;
29638 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29640 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29641 matching_parens parens;
29642 parens.require_open (parser);
29643 proto = cp_parser_identifier (parser);
29644 parens.require_close (parser);
29646 /* Build a location of the form:
29647 @protocol(prot)
29648 ^~~~~~~~~~~~~~~
29649 with caret==start at the @ token, finishing at the close paren. */
29650 location_t combined_loc
29651 = make_location (start_loc, start_loc,
29652 cp_lexer_previous_token (parser->lexer)->location);
29653 tree result = objc_build_protocol_expr (proto);
29654 protected_set_expr_location (result, combined_loc);
29655 return result;
29658 /* Parse an Objective-C selector expression.
29660 objc-selector-expression:
29661 @selector ( objc-method-signature )
29663 objc-method-signature:
29664 objc-selector
29665 objc-selector-seq
29667 objc-selector-seq:
29668 objc-selector :
29669 objc-selector-seq objc-selector :
29671 Returns a representation of the method selector. */
29673 static tree
29674 cp_parser_objc_selector_expression (cp_parser* parser)
29676 tree sel_seq = NULL_TREE;
29677 bool maybe_unary_selector_p = true;
29678 cp_token *token;
29679 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29681 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29682 matching_parens parens;
29683 parens.require_open (parser);
29684 token = cp_lexer_peek_token (parser->lexer);
29686 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29687 || token->type == CPP_SCOPE)
29689 tree selector = NULL_TREE;
29691 if (token->type != CPP_COLON
29692 || token->type == CPP_SCOPE)
29693 selector = cp_parser_objc_selector (parser);
29695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29696 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29698 /* Detect if we have a unary selector. */
29699 if (maybe_unary_selector_p)
29701 sel_seq = selector;
29702 goto finish_selector;
29704 else
29706 cp_parser_error (parser, "expected %<:%>");
29709 maybe_unary_selector_p = false;
29710 token = cp_lexer_consume_token (parser->lexer);
29712 if (token->type == CPP_SCOPE)
29714 sel_seq
29715 = chainon (sel_seq,
29716 build_tree_list (selector, NULL_TREE));
29717 sel_seq
29718 = chainon (sel_seq,
29719 build_tree_list (NULL_TREE, NULL_TREE));
29721 else
29722 sel_seq
29723 = chainon (sel_seq,
29724 build_tree_list (selector, NULL_TREE));
29726 token = cp_lexer_peek_token (parser->lexer);
29729 finish_selector:
29730 parens.require_close (parser);
29733 /* Build a location of the form:
29734 @selector(func)
29735 ^~~~~~~~~~~~~~~
29736 with caret==start at the @ token, finishing at the close paren. */
29737 location_t combined_loc
29738 = make_location (loc, loc,
29739 cp_lexer_previous_token (parser->lexer)->location);
29740 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29741 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29742 protected_set_expr_location (result, combined_loc);
29743 return result;
29746 /* Parse a list of identifiers.
29748 objc-identifier-list:
29749 identifier
29750 objc-identifier-list , identifier
29752 Returns a TREE_LIST of identifier nodes. */
29754 static tree
29755 cp_parser_objc_identifier_list (cp_parser* parser)
29757 tree identifier;
29758 tree list;
29759 cp_token *sep;
29761 identifier = cp_parser_identifier (parser);
29762 if (identifier == error_mark_node)
29763 return error_mark_node;
29765 list = build_tree_list (NULL_TREE, identifier);
29766 sep = cp_lexer_peek_token (parser->lexer);
29768 while (sep->type == CPP_COMMA)
29770 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29771 identifier = cp_parser_identifier (parser);
29772 if (identifier == error_mark_node)
29773 return list;
29775 list = chainon (list, build_tree_list (NULL_TREE,
29776 identifier));
29777 sep = cp_lexer_peek_token (parser->lexer);
29780 return list;
29783 /* Parse an Objective-C alias declaration.
29785 objc-alias-declaration:
29786 @compatibility_alias identifier identifier ;
29788 This function registers the alias mapping with the Objective-C front end.
29789 It returns nothing. */
29791 static void
29792 cp_parser_objc_alias_declaration (cp_parser* parser)
29794 tree alias, orig;
29796 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29797 alias = cp_parser_identifier (parser);
29798 orig = cp_parser_identifier (parser);
29799 objc_declare_alias (alias, orig);
29800 cp_parser_consume_semicolon_at_end_of_statement (parser);
29803 /* Parse an Objective-C class forward-declaration.
29805 objc-class-declaration:
29806 @class objc-identifier-list ;
29808 The function registers the forward declarations with the Objective-C
29809 front end. It returns nothing. */
29811 static void
29812 cp_parser_objc_class_declaration (cp_parser* parser)
29814 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29815 while (true)
29817 tree id;
29819 id = cp_parser_identifier (parser);
29820 if (id == error_mark_node)
29821 break;
29823 objc_declare_class (id);
29825 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29826 cp_lexer_consume_token (parser->lexer);
29827 else
29828 break;
29830 cp_parser_consume_semicolon_at_end_of_statement (parser);
29833 /* Parse a list of Objective-C protocol references.
29835 objc-protocol-refs-opt:
29836 objc-protocol-refs [opt]
29838 objc-protocol-refs:
29839 < objc-identifier-list >
29841 Returns a TREE_LIST of identifiers, if any. */
29843 static tree
29844 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29846 tree protorefs = NULL_TREE;
29848 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29850 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29851 protorefs = cp_parser_objc_identifier_list (parser);
29852 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29855 return protorefs;
29858 /* Parse a Objective-C visibility specification. */
29860 static void
29861 cp_parser_objc_visibility_spec (cp_parser* parser)
29863 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29865 switch (vis->keyword)
29867 case RID_AT_PRIVATE:
29868 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29869 break;
29870 case RID_AT_PROTECTED:
29871 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29872 break;
29873 case RID_AT_PUBLIC:
29874 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29875 break;
29876 case RID_AT_PACKAGE:
29877 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29878 break;
29879 default:
29880 return;
29883 /* Eat '@private'/'@protected'/'@public'. */
29884 cp_lexer_consume_token (parser->lexer);
29887 /* Parse an Objective-C method type. Return 'true' if it is a class
29888 (+) method, and 'false' if it is an instance (-) method. */
29890 static inline bool
29891 cp_parser_objc_method_type (cp_parser* parser)
29893 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29894 return true;
29895 else
29896 return false;
29899 /* Parse an Objective-C protocol qualifier. */
29901 static tree
29902 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29904 tree quals = NULL_TREE, node;
29905 cp_token *token = cp_lexer_peek_token (parser->lexer);
29907 node = token->u.value;
29909 while (node && identifier_p (node)
29910 && (node == ridpointers [(int) RID_IN]
29911 || node == ridpointers [(int) RID_OUT]
29912 || node == ridpointers [(int) RID_INOUT]
29913 || node == ridpointers [(int) RID_BYCOPY]
29914 || node == ridpointers [(int) RID_BYREF]
29915 || node == ridpointers [(int) RID_ONEWAY]))
29917 quals = tree_cons (NULL_TREE, node, quals);
29918 cp_lexer_consume_token (parser->lexer);
29919 token = cp_lexer_peek_token (parser->lexer);
29920 node = token->u.value;
29923 return quals;
29926 /* Parse an Objective-C typename. */
29928 static tree
29929 cp_parser_objc_typename (cp_parser* parser)
29931 tree type_name = NULL_TREE;
29933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29935 tree proto_quals, cp_type = NULL_TREE;
29937 matching_parens parens;
29938 parens.consume_open (parser); /* Eat '('. */
29939 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29941 /* An ObjC type name may consist of just protocol qualifiers, in which
29942 case the type shall default to 'id'. */
29943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29945 cp_type = cp_parser_type_id (parser);
29947 /* If the type could not be parsed, an error has already
29948 been produced. For error recovery, behave as if it had
29949 not been specified, which will use the default type
29950 'id'. */
29951 if (cp_type == error_mark_node)
29953 cp_type = NULL_TREE;
29954 /* We need to skip to the closing parenthesis as
29955 cp_parser_type_id() does not seem to do it for
29956 us. */
29957 cp_parser_skip_to_closing_parenthesis (parser,
29958 /*recovering=*/true,
29959 /*or_comma=*/false,
29960 /*consume_paren=*/false);
29964 parens.require_close (parser);
29965 type_name = build_tree_list (proto_quals, cp_type);
29968 return type_name;
29971 /* Check to see if TYPE refers to an Objective-C selector name. */
29973 static bool
29974 cp_parser_objc_selector_p (enum cpp_ttype type)
29976 return (type == CPP_NAME || type == CPP_KEYWORD
29977 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29978 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29979 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29980 || type == CPP_XOR || type == CPP_XOR_EQ);
29983 /* Parse an Objective-C selector. */
29985 static tree
29986 cp_parser_objc_selector (cp_parser* parser)
29988 cp_token *token = cp_lexer_consume_token (parser->lexer);
29990 if (!cp_parser_objc_selector_p (token->type))
29992 error_at (token->location, "invalid Objective-C++ selector name");
29993 return error_mark_node;
29996 /* C++ operator names are allowed to appear in ObjC selectors. */
29997 switch (token->type)
29999 case CPP_AND_AND: return get_identifier ("and");
30000 case CPP_AND_EQ: return get_identifier ("and_eq");
30001 case CPP_AND: return get_identifier ("bitand");
30002 case CPP_OR: return get_identifier ("bitor");
30003 case CPP_COMPL: return get_identifier ("compl");
30004 case CPP_NOT: return get_identifier ("not");
30005 case CPP_NOT_EQ: return get_identifier ("not_eq");
30006 case CPP_OR_OR: return get_identifier ("or");
30007 case CPP_OR_EQ: return get_identifier ("or_eq");
30008 case CPP_XOR: return get_identifier ("xor");
30009 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30010 default: return token->u.value;
30014 /* Parse an Objective-C params list. */
30016 static tree
30017 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30019 tree params = NULL_TREE;
30020 bool maybe_unary_selector_p = true;
30021 cp_token *token = cp_lexer_peek_token (parser->lexer);
30023 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30025 tree selector = NULL_TREE, type_name, identifier;
30026 tree parm_attr = NULL_TREE;
30028 if (token->keyword == RID_ATTRIBUTE)
30029 break;
30031 if (token->type != CPP_COLON)
30032 selector = cp_parser_objc_selector (parser);
30034 /* Detect if we have a unary selector. */
30035 if (maybe_unary_selector_p
30036 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30038 params = selector; /* Might be followed by attributes. */
30039 break;
30042 maybe_unary_selector_p = false;
30043 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30045 /* Something went quite wrong. There should be a colon
30046 here, but there is not. Stop parsing parameters. */
30047 break;
30049 type_name = cp_parser_objc_typename (parser);
30050 /* New ObjC allows attributes on parameters too. */
30051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30052 parm_attr = cp_parser_attributes_opt (parser);
30053 identifier = cp_parser_identifier (parser);
30055 params
30056 = chainon (params,
30057 objc_build_keyword_decl (selector,
30058 type_name,
30059 identifier,
30060 parm_attr));
30062 token = cp_lexer_peek_token (parser->lexer);
30065 if (params == NULL_TREE)
30067 cp_parser_error (parser, "objective-c++ method declaration is expected");
30068 return error_mark_node;
30071 /* We allow tail attributes for the method. */
30072 if (token->keyword == RID_ATTRIBUTE)
30074 *attributes = cp_parser_attributes_opt (parser);
30075 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30076 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30077 return params;
30078 cp_parser_error (parser,
30079 "method attributes must be specified at the end");
30080 return error_mark_node;
30083 if (params == NULL_TREE)
30085 cp_parser_error (parser, "objective-c++ method declaration is expected");
30086 return error_mark_node;
30088 return params;
30091 /* Parse the non-keyword Objective-C params. */
30093 static tree
30094 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30095 tree* attributes)
30097 tree params = make_node (TREE_LIST);
30098 cp_token *token = cp_lexer_peek_token (parser->lexer);
30099 *ellipsisp = false; /* Initially, assume no ellipsis. */
30101 while (token->type == CPP_COMMA)
30103 cp_parameter_declarator *parmdecl;
30104 tree parm;
30106 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30107 token = cp_lexer_peek_token (parser->lexer);
30109 if (token->type == CPP_ELLIPSIS)
30111 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30112 *ellipsisp = true;
30113 token = cp_lexer_peek_token (parser->lexer);
30114 break;
30117 /* TODO: parse attributes for tail parameters. */
30118 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30119 parm = grokdeclarator (parmdecl->declarator,
30120 &parmdecl->decl_specifiers,
30121 PARM, /*initialized=*/0,
30122 /*attrlist=*/NULL);
30124 chainon (params, build_tree_list (NULL_TREE, parm));
30125 token = cp_lexer_peek_token (parser->lexer);
30128 /* We allow tail attributes for the method. */
30129 if (token->keyword == RID_ATTRIBUTE)
30131 if (*attributes == NULL_TREE)
30133 *attributes = cp_parser_attributes_opt (parser);
30134 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30135 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30136 return params;
30138 else
30139 /* We have an error, but parse the attributes, so that we can
30140 carry on. */
30141 *attributes = cp_parser_attributes_opt (parser);
30143 cp_parser_error (parser,
30144 "method attributes must be specified at the end");
30145 return error_mark_node;
30148 return params;
30151 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30153 static void
30154 cp_parser_objc_interstitial_code (cp_parser* parser)
30156 cp_token *token = cp_lexer_peek_token (parser->lexer);
30158 /* If the next token is `extern' and the following token is a string
30159 literal, then we have a linkage specification. */
30160 if (token->keyword == RID_EXTERN
30161 && cp_parser_is_pure_string_literal
30162 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30163 cp_parser_linkage_specification (parser);
30164 /* Handle #pragma, if any. */
30165 else if (token->type == CPP_PRAGMA)
30166 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30167 /* Allow stray semicolons. */
30168 else if (token->type == CPP_SEMICOLON)
30169 cp_lexer_consume_token (parser->lexer);
30170 /* Mark methods as optional or required, when building protocols. */
30171 else if (token->keyword == RID_AT_OPTIONAL)
30173 cp_lexer_consume_token (parser->lexer);
30174 objc_set_method_opt (true);
30176 else if (token->keyword == RID_AT_REQUIRED)
30178 cp_lexer_consume_token (parser->lexer);
30179 objc_set_method_opt (false);
30181 else if (token->keyword == RID_NAMESPACE)
30182 cp_parser_namespace_definition (parser);
30183 /* Other stray characters must generate errors. */
30184 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30186 cp_lexer_consume_token (parser->lexer);
30187 error ("stray %qs between Objective-C++ methods",
30188 token->type == CPP_OPEN_BRACE ? "{" : "}");
30190 /* Finally, try to parse a block-declaration, or a function-definition. */
30191 else
30192 cp_parser_block_declaration (parser, /*statement_p=*/false);
30195 /* Parse a method signature. */
30197 static tree
30198 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30200 tree rettype, kwdparms, optparms;
30201 bool ellipsis = false;
30202 bool is_class_method;
30204 is_class_method = cp_parser_objc_method_type (parser);
30205 rettype = cp_parser_objc_typename (parser);
30206 *attributes = NULL_TREE;
30207 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30208 if (kwdparms == error_mark_node)
30209 return error_mark_node;
30210 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30211 if (optparms == error_mark_node)
30212 return error_mark_node;
30214 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30217 static bool
30218 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30220 tree tattr;
30221 cp_lexer_save_tokens (parser->lexer);
30222 tattr = cp_parser_attributes_opt (parser);
30223 gcc_assert (tattr) ;
30225 /* If the attributes are followed by a method introducer, this is not allowed.
30226 Dump the attributes and flag the situation. */
30227 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30228 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30229 return true;
30231 /* Otherwise, the attributes introduce some interstitial code, possibly so
30232 rewind to allow that check. */
30233 cp_lexer_rollback_tokens (parser->lexer);
30234 return false;
30237 /* Parse an Objective-C method prototype list. */
30239 static void
30240 cp_parser_objc_method_prototype_list (cp_parser* parser)
30242 cp_token *token = cp_lexer_peek_token (parser->lexer);
30244 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30246 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30248 tree attributes, sig;
30249 bool is_class_method;
30250 if (token->type == CPP_PLUS)
30251 is_class_method = true;
30252 else
30253 is_class_method = false;
30254 sig = cp_parser_objc_method_signature (parser, &attributes);
30255 if (sig == error_mark_node)
30257 cp_parser_skip_to_end_of_block_or_statement (parser);
30258 token = cp_lexer_peek_token (parser->lexer);
30259 continue;
30261 objc_add_method_declaration (is_class_method, sig, attributes);
30262 cp_parser_consume_semicolon_at_end_of_statement (parser);
30264 else if (token->keyword == RID_AT_PROPERTY)
30265 cp_parser_objc_at_property_declaration (parser);
30266 else if (token->keyword == RID_ATTRIBUTE
30267 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30268 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30269 OPT_Wattributes,
30270 "prefix attributes are ignored for methods");
30271 else
30272 /* Allow for interspersed non-ObjC++ code. */
30273 cp_parser_objc_interstitial_code (parser);
30275 token = cp_lexer_peek_token (parser->lexer);
30278 if (token->type != CPP_EOF)
30279 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30280 else
30281 cp_parser_error (parser, "expected %<@end%>");
30283 objc_finish_interface ();
30286 /* Parse an Objective-C method definition list. */
30288 static void
30289 cp_parser_objc_method_definition_list (cp_parser* parser)
30291 cp_token *token = cp_lexer_peek_token (parser->lexer);
30293 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30295 tree meth;
30297 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30299 cp_token *ptk;
30300 tree sig, attribute;
30301 bool is_class_method;
30302 if (token->type == CPP_PLUS)
30303 is_class_method = true;
30304 else
30305 is_class_method = false;
30306 push_deferring_access_checks (dk_deferred);
30307 sig = cp_parser_objc_method_signature (parser, &attribute);
30308 if (sig == error_mark_node)
30310 cp_parser_skip_to_end_of_block_or_statement (parser);
30311 token = cp_lexer_peek_token (parser->lexer);
30312 continue;
30314 objc_start_method_definition (is_class_method, sig, attribute,
30315 NULL_TREE);
30317 /* For historical reasons, we accept an optional semicolon. */
30318 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30319 cp_lexer_consume_token (parser->lexer);
30321 ptk = cp_lexer_peek_token (parser->lexer);
30322 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30323 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30325 perform_deferred_access_checks (tf_warning_or_error);
30326 stop_deferring_access_checks ();
30327 meth = cp_parser_function_definition_after_declarator (parser,
30328 false);
30329 pop_deferring_access_checks ();
30330 objc_finish_method_definition (meth);
30333 /* The following case will be removed once @synthesize is
30334 completely implemented. */
30335 else if (token->keyword == RID_AT_PROPERTY)
30336 cp_parser_objc_at_property_declaration (parser);
30337 else if (token->keyword == RID_AT_SYNTHESIZE)
30338 cp_parser_objc_at_synthesize_declaration (parser);
30339 else if (token->keyword == RID_AT_DYNAMIC)
30340 cp_parser_objc_at_dynamic_declaration (parser);
30341 else if (token->keyword == RID_ATTRIBUTE
30342 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30343 warning_at (token->location, OPT_Wattributes,
30344 "prefix attributes are ignored for methods");
30345 else
30346 /* Allow for interspersed non-ObjC++ code. */
30347 cp_parser_objc_interstitial_code (parser);
30349 token = cp_lexer_peek_token (parser->lexer);
30352 if (token->type != CPP_EOF)
30353 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30354 else
30355 cp_parser_error (parser, "expected %<@end%>");
30357 objc_finish_implementation ();
30360 /* Parse Objective-C ivars. */
30362 static void
30363 cp_parser_objc_class_ivars (cp_parser* parser)
30365 cp_token *token = cp_lexer_peek_token (parser->lexer);
30367 if (token->type != CPP_OPEN_BRACE)
30368 return; /* No ivars specified. */
30370 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30371 token = cp_lexer_peek_token (parser->lexer);
30373 while (token->type != CPP_CLOSE_BRACE
30374 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30376 cp_decl_specifier_seq declspecs;
30377 int decl_class_or_enum_p;
30378 tree prefix_attributes;
30380 cp_parser_objc_visibility_spec (parser);
30382 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30383 break;
30385 cp_parser_decl_specifier_seq (parser,
30386 CP_PARSER_FLAGS_OPTIONAL,
30387 &declspecs,
30388 &decl_class_or_enum_p);
30390 /* auto, register, static, extern, mutable. */
30391 if (declspecs.storage_class != sc_none)
30393 cp_parser_error (parser, "invalid type for instance variable");
30394 declspecs.storage_class = sc_none;
30397 /* thread_local. */
30398 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30400 cp_parser_error (parser, "invalid type for instance variable");
30401 declspecs.locations[ds_thread] = 0;
30404 /* typedef. */
30405 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30407 cp_parser_error (parser, "invalid type for instance variable");
30408 declspecs.locations[ds_typedef] = 0;
30411 prefix_attributes = declspecs.attributes;
30412 declspecs.attributes = NULL_TREE;
30414 /* Keep going until we hit the `;' at the end of the
30415 declaration. */
30416 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30418 tree width = NULL_TREE, attributes, first_attribute, decl;
30419 cp_declarator *declarator = NULL;
30420 int ctor_dtor_or_conv_p;
30422 /* Check for a (possibly unnamed) bitfield declaration. */
30423 token = cp_lexer_peek_token (parser->lexer);
30424 if (token->type == CPP_COLON)
30425 goto eat_colon;
30427 if (token->type == CPP_NAME
30428 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30429 == CPP_COLON))
30431 /* Get the name of the bitfield. */
30432 declarator = make_id_declarator (NULL_TREE,
30433 cp_parser_identifier (parser),
30434 sfk_none);
30436 eat_colon:
30437 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30438 /* Get the width of the bitfield. */
30439 width
30440 = cp_parser_constant_expression (parser);
30442 else
30444 /* Parse the declarator. */
30445 declarator
30446 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30447 &ctor_dtor_or_conv_p,
30448 /*parenthesized_p=*/NULL,
30449 /*member_p=*/false,
30450 /*friend_p=*/false);
30453 /* Look for attributes that apply to the ivar. */
30454 attributes = cp_parser_attributes_opt (parser);
30455 /* Remember which attributes are prefix attributes and
30456 which are not. */
30457 first_attribute = attributes;
30458 /* Combine the attributes. */
30459 attributes = attr_chainon (prefix_attributes, attributes);
30461 if (width)
30462 /* Create the bitfield declaration. */
30463 decl = grokbitfield (declarator, &declspecs,
30464 width, NULL_TREE, attributes);
30465 else
30466 decl = grokfield (declarator, &declspecs,
30467 NULL_TREE, /*init_const_expr_p=*/false,
30468 NULL_TREE, attributes);
30470 /* Add the instance variable. */
30471 if (decl != error_mark_node && decl != NULL_TREE)
30472 objc_add_instance_variable (decl);
30474 /* Reset PREFIX_ATTRIBUTES. */
30475 if (attributes != error_mark_node)
30477 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30478 attributes = TREE_CHAIN (attributes);
30479 if (attributes)
30480 TREE_CHAIN (attributes) = NULL_TREE;
30483 token = cp_lexer_peek_token (parser->lexer);
30485 if (token->type == CPP_COMMA)
30487 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30488 continue;
30490 break;
30493 cp_parser_consume_semicolon_at_end_of_statement (parser);
30494 token = cp_lexer_peek_token (parser->lexer);
30497 if (token->keyword == RID_AT_END)
30498 cp_parser_error (parser, "expected %<}%>");
30500 /* Do not consume the RID_AT_END, so it will be read again as terminating
30501 the @interface of @implementation. */
30502 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30503 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30505 /* For historical reasons, we accept an optional semicolon. */
30506 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30507 cp_lexer_consume_token (parser->lexer);
30510 /* Parse an Objective-C protocol declaration. */
30512 static void
30513 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30515 tree proto, protorefs;
30516 cp_token *tok;
30518 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30521 tok = cp_lexer_peek_token (parser->lexer);
30522 error_at (tok->location, "identifier expected after %<@protocol%>");
30523 cp_parser_consume_semicolon_at_end_of_statement (parser);
30524 return;
30527 /* See if we have a forward declaration or a definition. */
30528 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30530 /* Try a forward declaration first. */
30531 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30533 while (true)
30535 tree id;
30537 id = cp_parser_identifier (parser);
30538 if (id == error_mark_node)
30539 break;
30541 objc_declare_protocol (id, attributes);
30543 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30544 cp_lexer_consume_token (parser->lexer);
30545 else
30546 break;
30548 cp_parser_consume_semicolon_at_end_of_statement (parser);
30551 /* Ok, we got a full-fledged definition (or at least should). */
30552 else
30554 proto = cp_parser_identifier (parser);
30555 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30556 objc_start_protocol (proto, protorefs, attributes);
30557 cp_parser_objc_method_prototype_list (parser);
30561 /* Parse an Objective-C superclass or category. */
30563 static void
30564 cp_parser_objc_superclass_or_category (cp_parser *parser,
30565 bool iface_p,
30566 tree *super,
30567 tree *categ, bool *is_class_extension)
30569 cp_token *next = cp_lexer_peek_token (parser->lexer);
30571 *super = *categ = NULL_TREE;
30572 *is_class_extension = false;
30573 if (next->type == CPP_COLON)
30575 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30576 *super = cp_parser_identifier (parser);
30578 else if (next->type == CPP_OPEN_PAREN)
30580 matching_parens parens;
30581 parens.consume_open (parser); /* Eat '('. */
30583 /* If there is no category name, and this is an @interface, we
30584 have a class extension. */
30585 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30587 *categ = NULL_TREE;
30588 *is_class_extension = true;
30590 else
30591 *categ = cp_parser_identifier (parser);
30593 parens.require_close (parser);
30597 /* Parse an Objective-C class interface. */
30599 static void
30600 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30602 tree name, super, categ, protos;
30603 bool is_class_extension;
30605 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30606 name = cp_parser_identifier (parser);
30607 if (name == error_mark_node)
30609 /* It's hard to recover because even if valid @interface stuff
30610 is to follow, we can't compile it (or validate it) if we
30611 don't even know which class it refers to. Let's assume this
30612 was a stray '@interface' token in the stream and skip it.
30614 return;
30616 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30617 &is_class_extension);
30618 protos = cp_parser_objc_protocol_refs_opt (parser);
30620 /* We have either a class or a category on our hands. */
30621 if (categ || is_class_extension)
30622 objc_start_category_interface (name, categ, protos, attributes);
30623 else
30625 objc_start_class_interface (name, super, protos, attributes);
30626 /* Handle instance variable declarations, if any. */
30627 cp_parser_objc_class_ivars (parser);
30628 objc_continue_interface ();
30631 cp_parser_objc_method_prototype_list (parser);
30634 /* Parse an Objective-C class implementation. */
30636 static void
30637 cp_parser_objc_class_implementation (cp_parser* parser)
30639 tree name, super, categ;
30640 bool is_class_extension;
30642 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30643 name = cp_parser_identifier (parser);
30644 if (name == error_mark_node)
30646 /* It's hard to recover because even if valid @implementation
30647 stuff is to follow, we can't compile it (or validate it) if
30648 we don't even know which class it refers to. Let's assume
30649 this was a stray '@implementation' token in the stream and
30650 skip it.
30652 return;
30654 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30655 &is_class_extension);
30657 /* We have either a class or a category on our hands. */
30658 if (categ)
30659 objc_start_category_implementation (name, categ);
30660 else
30662 objc_start_class_implementation (name, super);
30663 /* Handle instance variable declarations, if any. */
30664 cp_parser_objc_class_ivars (parser);
30665 objc_continue_implementation ();
30668 cp_parser_objc_method_definition_list (parser);
30671 /* Consume the @end token and finish off the implementation. */
30673 static void
30674 cp_parser_objc_end_implementation (cp_parser* parser)
30676 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30677 objc_finish_implementation ();
30680 /* Parse an Objective-C declaration. */
30682 static void
30683 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30685 /* Try to figure out what kind of declaration is present. */
30686 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30688 if (attributes)
30689 switch (kwd->keyword)
30691 case RID_AT_ALIAS:
30692 case RID_AT_CLASS:
30693 case RID_AT_END:
30694 error_at (kwd->location, "attributes may not be specified before"
30695 " the %<@%D%> Objective-C++ keyword",
30696 kwd->u.value);
30697 attributes = NULL;
30698 break;
30699 case RID_AT_IMPLEMENTATION:
30700 warning_at (kwd->location, OPT_Wattributes,
30701 "prefix attributes are ignored before %<@%D%>",
30702 kwd->u.value);
30703 attributes = NULL;
30704 default:
30705 break;
30708 switch (kwd->keyword)
30710 case RID_AT_ALIAS:
30711 cp_parser_objc_alias_declaration (parser);
30712 break;
30713 case RID_AT_CLASS:
30714 cp_parser_objc_class_declaration (parser);
30715 break;
30716 case RID_AT_PROTOCOL:
30717 cp_parser_objc_protocol_declaration (parser, attributes);
30718 break;
30719 case RID_AT_INTERFACE:
30720 cp_parser_objc_class_interface (parser, attributes);
30721 break;
30722 case RID_AT_IMPLEMENTATION:
30723 cp_parser_objc_class_implementation (parser);
30724 break;
30725 case RID_AT_END:
30726 cp_parser_objc_end_implementation (parser);
30727 break;
30728 default:
30729 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30730 kwd->u.value);
30731 cp_parser_skip_to_end_of_block_or_statement (parser);
30735 /* Parse an Objective-C try-catch-finally statement.
30737 objc-try-catch-finally-stmt:
30738 @try compound-statement objc-catch-clause-seq [opt]
30739 objc-finally-clause [opt]
30741 objc-catch-clause-seq:
30742 objc-catch-clause objc-catch-clause-seq [opt]
30744 objc-catch-clause:
30745 @catch ( objc-exception-declaration ) compound-statement
30747 objc-finally-clause:
30748 @finally compound-statement
30750 objc-exception-declaration:
30751 parameter-declaration
30752 '...'
30754 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30756 Returns NULL_TREE.
30758 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30759 for C. Keep them in sync. */
30761 static tree
30762 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30764 location_t location;
30765 tree stmt;
30767 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30768 location = cp_lexer_peek_token (parser->lexer)->location;
30769 objc_maybe_warn_exceptions (location);
30770 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30771 node, lest it get absorbed into the surrounding block. */
30772 stmt = push_stmt_list ();
30773 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30774 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30776 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30778 cp_parameter_declarator *parm;
30779 tree parameter_declaration = error_mark_node;
30780 bool seen_open_paren = false;
30781 matching_parens parens;
30783 cp_lexer_consume_token (parser->lexer);
30784 if (parens.require_open (parser))
30785 seen_open_paren = true;
30786 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30788 /* We have "@catch (...)" (where the '...' are literally
30789 what is in the code). Skip the '...'.
30790 parameter_declaration is set to NULL_TREE, and
30791 objc_being_catch_clauses() knows that that means
30792 '...'. */
30793 cp_lexer_consume_token (parser->lexer);
30794 parameter_declaration = NULL_TREE;
30796 else
30798 /* We have "@catch (NSException *exception)" or something
30799 like that. Parse the parameter declaration. */
30800 parm = cp_parser_parameter_declaration (parser, false, NULL);
30801 if (parm == NULL)
30802 parameter_declaration = error_mark_node;
30803 else
30804 parameter_declaration = grokdeclarator (parm->declarator,
30805 &parm->decl_specifiers,
30806 PARM, /*initialized=*/0,
30807 /*attrlist=*/NULL);
30809 if (seen_open_paren)
30810 parens.require_close (parser);
30811 else
30813 /* If there was no open parenthesis, we are recovering from
30814 an error, and we are trying to figure out what mistake
30815 the user has made. */
30817 /* If there is an immediate closing parenthesis, the user
30818 probably forgot the opening one (ie, they typed "@catch
30819 NSException *e)". Parse the closing parenthesis and keep
30820 going. */
30821 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30822 cp_lexer_consume_token (parser->lexer);
30824 /* If these is no immediate closing parenthesis, the user
30825 probably doesn't know that parenthesis are required at
30826 all (ie, they typed "@catch NSException *e"). So, just
30827 forget about the closing parenthesis and keep going. */
30829 objc_begin_catch_clause (parameter_declaration);
30830 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30831 objc_finish_catch_clause ();
30833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30835 cp_lexer_consume_token (parser->lexer);
30836 location = cp_lexer_peek_token (parser->lexer)->location;
30837 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30838 node, lest it get absorbed into the surrounding block. */
30839 stmt = push_stmt_list ();
30840 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30841 objc_build_finally_clause (location, pop_stmt_list (stmt));
30844 return objc_finish_try_stmt ();
30847 /* Parse an Objective-C synchronized statement.
30849 objc-synchronized-stmt:
30850 @synchronized ( expression ) compound-statement
30852 Returns NULL_TREE. */
30854 static tree
30855 cp_parser_objc_synchronized_statement (cp_parser *parser)
30857 location_t location;
30858 tree lock, stmt;
30860 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30862 location = cp_lexer_peek_token (parser->lexer)->location;
30863 objc_maybe_warn_exceptions (location);
30864 matching_parens parens;
30865 parens.require_open (parser);
30866 lock = cp_parser_expression (parser);
30867 parens.require_close (parser);
30869 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30870 node, lest it get absorbed into the surrounding block. */
30871 stmt = push_stmt_list ();
30872 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30874 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30877 /* Parse an Objective-C throw statement.
30879 objc-throw-stmt:
30880 @throw assignment-expression [opt] ;
30882 Returns a constructed '@throw' statement. */
30884 static tree
30885 cp_parser_objc_throw_statement (cp_parser *parser)
30887 tree expr = NULL_TREE;
30888 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30890 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30893 expr = cp_parser_expression (parser);
30895 cp_parser_consume_semicolon_at_end_of_statement (parser);
30897 return objc_build_throw_stmt (loc, expr);
30900 /* Parse an Objective-C statement. */
30902 static tree
30903 cp_parser_objc_statement (cp_parser * parser)
30905 /* Try to figure out what kind of declaration is present. */
30906 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30908 switch (kwd->keyword)
30910 case RID_AT_TRY:
30911 return cp_parser_objc_try_catch_finally_statement (parser);
30912 case RID_AT_SYNCHRONIZED:
30913 return cp_parser_objc_synchronized_statement (parser);
30914 case RID_AT_THROW:
30915 return cp_parser_objc_throw_statement (parser);
30916 default:
30917 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30918 kwd->u.value);
30919 cp_parser_skip_to_end_of_block_or_statement (parser);
30922 return error_mark_node;
30925 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30926 look ahead to see if an objc keyword follows the attributes. This
30927 is to detect the use of prefix attributes on ObjC @interface and
30928 @protocol. */
30930 static bool
30931 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30933 cp_lexer_save_tokens (parser->lexer);
30934 *attrib = cp_parser_attributes_opt (parser);
30935 gcc_assert (*attrib);
30936 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30938 cp_lexer_commit_tokens (parser->lexer);
30939 return true;
30941 cp_lexer_rollback_tokens (parser->lexer);
30942 return false;
30945 /* This routine is a minimal replacement for
30946 c_parser_struct_declaration () used when parsing the list of
30947 types/names or ObjC++ properties. For example, when parsing the
30948 code
30950 @property (readonly) int a, b, c;
30952 this function is responsible for parsing "int a, int b, int c" and
30953 returning the declarations as CHAIN of DECLs.
30955 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30956 similar parsing. */
30957 static tree
30958 cp_parser_objc_struct_declaration (cp_parser *parser)
30960 tree decls = NULL_TREE;
30961 cp_decl_specifier_seq declspecs;
30962 int decl_class_or_enum_p;
30963 tree prefix_attributes;
30965 cp_parser_decl_specifier_seq (parser,
30966 CP_PARSER_FLAGS_NONE,
30967 &declspecs,
30968 &decl_class_or_enum_p);
30970 if (declspecs.type == error_mark_node)
30971 return error_mark_node;
30973 /* auto, register, static, extern, mutable. */
30974 if (declspecs.storage_class != sc_none)
30976 cp_parser_error (parser, "invalid type for property");
30977 declspecs.storage_class = sc_none;
30980 /* thread_local. */
30981 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30983 cp_parser_error (parser, "invalid type for property");
30984 declspecs.locations[ds_thread] = 0;
30987 /* typedef. */
30988 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30990 cp_parser_error (parser, "invalid type for property");
30991 declspecs.locations[ds_typedef] = 0;
30994 prefix_attributes = declspecs.attributes;
30995 declspecs.attributes = NULL_TREE;
30997 /* Keep going until we hit the `;' at the end of the declaration. */
30998 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31000 tree attributes, first_attribute, decl;
31001 cp_declarator *declarator;
31002 cp_token *token;
31004 /* Parse the declarator. */
31005 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31006 NULL, NULL, false, false);
31008 /* Look for attributes that apply to the ivar. */
31009 attributes = cp_parser_attributes_opt (parser);
31010 /* Remember which attributes are prefix attributes and
31011 which are not. */
31012 first_attribute = attributes;
31013 /* Combine the attributes. */
31014 attributes = attr_chainon (prefix_attributes, attributes);
31016 decl = grokfield (declarator, &declspecs,
31017 NULL_TREE, /*init_const_expr_p=*/false,
31018 NULL_TREE, attributes);
31020 if (decl == error_mark_node || decl == NULL_TREE)
31021 return error_mark_node;
31023 /* Reset PREFIX_ATTRIBUTES. */
31024 if (attributes != error_mark_node)
31026 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31027 attributes = TREE_CHAIN (attributes);
31028 if (attributes)
31029 TREE_CHAIN (attributes) = NULL_TREE;
31032 DECL_CHAIN (decl) = decls;
31033 decls = decl;
31035 token = cp_lexer_peek_token (parser->lexer);
31036 if (token->type == CPP_COMMA)
31038 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31039 continue;
31041 else
31042 break;
31044 return decls;
31047 /* Parse an Objective-C @property declaration. The syntax is:
31049 objc-property-declaration:
31050 '@property' objc-property-attributes[opt] struct-declaration ;
31052 objc-property-attributes:
31053 '(' objc-property-attribute-list ')'
31055 objc-property-attribute-list:
31056 objc-property-attribute
31057 objc-property-attribute-list, objc-property-attribute
31059 objc-property-attribute
31060 'getter' = identifier
31061 'setter' = identifier
31062 'readonly'
31063 'readwrite'
31064 'assign'
31065 'retain'
31066 'copy'
31067 'nonatomic'
31069 For example:
31070 @property NSString *name;
31071 @property (readonly) id object;
31072 @property (retain, nonatomic, getter=getTheName) id name;
31073 @property int a, b, c;
31075 PS: This function is identical to
31076 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31077 static void
31078 cp_parser_objc_at_property_declaration (cp_parser *parser)
31080 /* The following variables hold the attributes of the properties as
31081 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31082 seen. When we see an attribute, we set them to 'true' (if they
31083 are boolean properties) or to the identifier (if they have an
31084 argument, ie, for getter and setter). Note that here we only
31085 parse the list of attributes, check the syntax and accumulate the
31086 attributes that we find. objc_add_property_declaration() will
31087 then process the information. */
31088 bool property_assign = false;
31089 bool property_copy = false;
31090 tree property_getter_ident = NULL_TREE;
31091 bool property_nonatomic = false;
31092 bool property_readonly = false;
31093 bool property_readwrite = false;
31094 bool property_retain = false;
31095 tree property_setter_ident = NULL_TREE;
31097 /* 'properties' is the list of properties that we read. Usually a
31098 single one, but maybe more (eg, in "@property int a, b, c;" there
31099 are three). */
31100 tree properties;
31101 location_t loc;
31103 loc = cp_lexer_peek_token (parser->lexer)->location;
31105 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31107 /* Parse the optional attribute list... */
31108 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31110 /* Eat the '('. */
31111 matching_parens parens;
31112 parens.consume_open (parser);
31114 while (true)
31116 bool syntax_error = false;
31117 cp_token *token = cp_lexer_peek_token (parser->lexer);
31118 enum rid keyword;
31120 if (token->type != CPP_NAME)
31122 cp_parser_error (parser, "expected identifier");
31123 break;
31125 keyword = C_RID_CODE (token->u.value);
31126 cp_lexer_consume_token (parser->lexer);
31127 switch (keyword)
31129 case RID_ASSIGN: property_assign = true; break;
31130 case RID_COPY: property_copy = true; break;
31131 case RID_NONATOMIC: property_nonatomic = true; break;
31132 case RID_READONLY: property_readonly = true; break;
31133 case RID_READWRITE: property_readwrite = true; break;
31134 case RID_RETAIN: property_retain = true; break;
31136 case RID_GETTER:
31137 case RID_SETTER:
31138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31140 if (keyword == RID_GETTER)
31141 cp_parser_error (parser,
31142 "missing %<=%> (after %<getter%> attribute)");
31143 else
31144 cp_parser_error (parser,
31145 "missing %<=%> (after %<setter%> attribute)");
31146 syntax_error = true;
31147 break;
31149 cp_lexer_consume_token (parser->lexer); /* eat the = */
31150 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31152 cp_parser_error (parser, "expected identifier");
31153 syntax_error = true;
31154 break;
31156 if (keyword == RID_SETTER)
31158 if (property_setter_ident != NULL_TREE)
31160 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31161 cp_lexer_consume_token (parser->lexer);
31163 else
31164 property_setter_ident = cp_parser_objc_selector (parser);
31165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31166 cp_parser_error (parser, "setter name must terminate with %<:%>");
31167 else
31168 cp_lexer_consume_token (parser->lexer);
31170 else
31172 if (property_getter_ident != NULL_TREE)
31174 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31175 cp_lexer_consume_token (parser->lexer);
31177 else
31178 property_getter_ident = cp_parser_objc_selector (parser);
31180 break;
31181 default:
31182 cp_parser_error (parser, "unknown property attribute");
31183 syntax_error = true;
31184 break;
31187 if (syntax_error)
31188 break;
31190 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31191 cp_lexer_consume_token (parser->lexer);
31192 else
31193 break;
31196 /* FIXME: "@property (setter, assign);" will generate a spurious
31197 "error: expected ‘)’ before ‘,’ token". This is because
31198 cp_parser_require, unlike the C counterpart, will produce an
31199 error even if we are in error recovery. */
31200 if (!parens.require_close (parser))
31202 cp_parser_skip_to_closing_parenthesis (parser,
31203 /*recovering=*/true,
31204 /*or_comma=*/false,
31205 /*consume_paren=*/true);
31209 /* ... and the property declaration(s). */
31210 properties = cp_parser_objc_struct_declaration (parser);
31212 if (properties == error_mark_node)
31214 cp_parser_skip_to_end_of_statement (parser);
31215 /* If the next token is now a `;', consume it. */
31216 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31217 cp_lexer_consume_token (parser->lexer);
31218 return;
31221 if (properties == NULL_TREE)
31222 cp_parser_error (parser, "expected identifier");
31223 else
31225 /* Comma-separated properties are chained together in
31226 reverse order; add them one by one. */
31227 properties = nreverse (properties);
31229 for (; properties; properties = TREE_CHAIN (properties))
31230 objc_add_property_declaration (loc, copy_node (properties),
31231 property_readonly, property_readwrite,
31232 property_assign, property_retain,
31233 property_copy, property_nonatomic,
31234 property_getter_ident, property_setter_ident);
31237 cp_parser_consume_semicolon_at_end_of_statement (parser);
31240 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31242 objc-synthesize-declaration:
31243 @synthesize objc-synthesize-identifier-list ;
31245 objc-synthesize-identifier-list:
31246 objc-synthesize-identifier
31247 objc-synthesize-identifier-list, objc-synthesize-identifier
31249 objc-synthesize-identifier
31250 identifier
31251 identifier = identifier
31253 For example:
31254 @synthesize MyProperty;
31255 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31257 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31258 for C. Keep them in sync.
31260 static void
31261 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31263 tree list = NULL_TREE;
31264 location_t loc;
31265 loc = cp_lexer_peek_token (parser->lexer)->location;
31267 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31268 while (true)
31270 tree property, ivar;
31271 property = cp_parser_identifier (parser);
31272 if (property == error_mark_node)
31274 cp_parser_consume_semicolon_at_end_of_statement (parser);
31275 return;
31277 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31279 cp_lexer_consume_token (parser->lexer);
31280 ivar = cp_parser_identifier (parser);
31281 if (ivar == error_mark_node)
31283 cp_parser_consume_semicolon_at_end_of_statement (parser);
31284 return;
31287 else
31288 ivar = NULL_TREE;
31289 list = chainon (list, build_tree_list (ivar, property));
31290 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31291 cp_lexer_consume_token (parser->lexer);
31292 else
31293 break;
31295 cp_parser_consume_semicolon_at_end_of_statement (parser);
31296 objc_add_synthesize_declaration (loc, list);
31299 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31301 objc-dynamic-declaration:
31302 @dynamic identifier-list ;
31304 For example:
31305 @dynamic MyProperty;
31306 @dynamic MyProperty, AnotherProperty;
31308 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31309 for C. Keep them in sync.
31311 static void
31312 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31314 tree list = NULL_TREE;
31315 location_t loc;
31316 loc = cp_lexer_peek_token (parser->lexer)->location;
31318 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31319 while (true)
31321 tree property;
31322 property = cp_parser_identifier (parser);
31323 if (property == error_mark_node)
31325 cp_parser_consume_semicolon_at_end_of_statement (parser);
31326 return;
31328 list = chainon (list, build_tree_list (NULL, property));
31329 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31330 cp_lexer_consume_token (parser->lexer);
31331 else
31332 break;
31334 cp_parser_consume_semicolon_at_end_of_statement (parser);
31335 objc_add_dynamic_declaration (loc, list);
31339 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31341 /* Returns name of the next clause.
31342 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31343 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31344 returned and the token is consumed. */
31346 static pragma_omp_clause
31347 cp_parser_omp_clause_name (cp_parser *parser)
31349 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31351 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31352 result = PRAGMA_OACC_CLAUSE_AUTO;
31353 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31354 result = PRAGMA_OMP_CLAUSE_IF;
31355 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31356 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31357 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31358 result = PRAGMA_OACC_CLAUSE_DELETE;
31359 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31360 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31361 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31362 result = PRAGMA_OMP_CLAUSE_FOR;
31363 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31365 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31366 const char *p = IDENTIFIER_POINTER (id);
31368 switch (p[0])
31370 case 'a':
31371 if (!strcmp ("aligned", p))
31372 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31373 else if (!strcmp ("async", p))
31374 result = PRAGMA_OACC_CLAUSE_ASYNC;
31375 break;
31376 case 'c':
31377 if (!strcmp ("collapse", p))
31378 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31379 else if (!strcmp ("copy", p))
31380 result = PRAGMA_OACC_CLAUSE_COPY;
31381 else if (!strcmp ("copyin", p))
31382 result = PRAGMA_OMP_CLAUSE_COPYIN;
31383 else if (!strcmp ("copyout", p))
31384 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31385 else if (!strcmp ("copyprivate", p))
31386 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31387 else if (!strcmp ("create", p))
31388 result = PRAGMA_OACC_CLAUSE_CREATE;
31389 break;
31390 case 'd':
31391 if (!strcmp ("defaultmap", p))
31392 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31393 else if (!strcmp ("depend", p))
31394 result = PRAGMA_OMP_CLAUSE_DEPEND;
31395 else if (!strcmp ("device", p))
31396 result = PRAGMA_OMP_CLAUSE_DEVICE;
31397 else if (!strcmp ("deviceptr", p))
31398 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31399 else if (!strcmp ("device_resident", p))
31400 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31401 else if (!strcmp ("dist_schedule", p))
31402 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31403 break;
31404 case 'f':
31405 if (!strcmp ("final", p))
31406 result = PRAGMA_OMP_CLAUSE_FINAL;
31407 else if (!strcmp ("finalize", p))
31408 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31409 else if (!strcmp ("firstprivate", p))
31410 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31411 else if (!strcmp ("from", p))
31412 result = PRAGMA_OMP_CLAUSE_FROM;
31413 break;
31414 case 'g':
31415 if (!strcmp ("gang", p))
31416 result = PRAGMA_OACC_CLAUSE_GANG;
31417 else if (!strcmp ("grainsize", p))
31418 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31419 break;
31420 case 'h':
31421 if (!strcmp ("hint", p))
31422 result = PRAGMA_OMP_CLAUSE_HINT;
31423 else if (!strcmp ("host", p))
31424 result = PRAGMA_OACC_CLAUSE_HOST;
31425 break;
31426 case 'i':
31427 if (!strcmp ("if_present", p))
31428 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31429 else if (!strcmp ("inbranch", p))
31430 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31431 else if (!strcmp ("independent", p))
31432 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31433 else if (!strcmp ("is_device_ptr", p))
31434 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31435 break;
31436 case 'l':
31437 if (!strcmp ("lastprivate", p))
31438 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31439 else if (!strcmp ("linear", p))
31440 result = PRAGMA_OMP_CLAUSE_LINEAR;
31441 else if (!strcmp ("link", p))
31442 result = PRAGMA_OMP_CLAUSE_LINK;
31443 break;
31444 case 'm':
31445 if (!strcmp ("map", p))
31446 result = PRAGMA_OMP_CLAUSE_MAP;
31447 else if (!strcmp ("mergeable", p))
31448 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31449 break;
31450 case 'n':
31451 if (!strcmp ("nogroup", p))
31452 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31453 else if (!strcmp ("notinbranch", p))
31454 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31455 else if (!strcmp ("nowait", p))
31456 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31457 else if (!strcmp ("num_gangs", p))
31458 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31459 else if (!strcmp ("num_tasks", p))
31460 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31461 else if (!strcmp ("num_teams", p))
31462 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31463 else if (!strcmp ("num_threads", p))
31464 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31465 else if (!strcmp ("num_workers", p))
31466 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31467 break;
31468 case 'o':
31469 if (!strcmp ("ordered", p))
31470 result = PRAGMA_OMP_CLAUSE_ORDERED;
31471 break;
31472 case 'p':
31473 if (!strcmp ("parallel", p))
31474 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31475 else if (!strcmp ("present", p))
31476 result = PRAGMA_OACC_CLAUSE_PRESENT;
31477 else if (!strcmp ("present_or_copy", p)
31478 || !strcmp ("pcopy", p))
31479 result = PRAGMA_OACC_CLAUSE_COPY;
31480 else if (!strcmp ("present_or_copyin", p)
31481 || !strcmp ("pcopyin", p))
31482 result = PRAGMA_OACC_CLAUSE_COPYIN;
31483 else if (!strcmp ("present_or_copyout", p)
31484 || !strcmp ("pcopyout", p))
31485 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31486 else if (!strcmp ("present_or_create", p)
31487 || !strcmp ("pcreate", p))
31488 result = PRAGMA_OACC_CLAUSE_CREATE;
31489 else if (!strcmp ("priority", p))
31490 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31491 else if (!strcmp ("proc_bind", p))
31492 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31493 break;
31494 case 'r':
31495 if (!strcmp ("reduction", p))
31496 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31497 break;
31498 case 's':
31499 if (!strcmp ("safelen", p))
31500 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31501 else if (!strcmp ("schedule", p))
31502 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31503 else if (!strcmp ("sections", p))
31504 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31505 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31506 result = PRAGMA_OACC_CLAUSE_HOST;
31507 else if (!strcmp ("seq", p))
31508 result = PRAGMA_OACC_CLAUSE_SEQ;
31509 else if (!strcmp ("shared", p))
31510 result = PRAGMA_OMP_CLAUSE_SHARED;
31511 else if (!strcmp ("simd", p))
31512 result = PRAGMA_OMP_CLAUSE_SIMD;
31513 else if (!strcmp ("simdlen", p))
31514 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31515 break;
31516 case 't':
31517 if (!strcmp ("taskgroup", p))
31518 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31519 else if (!strcmp ("thread_limit", p))
31520 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31521 else if (!strcmp ("threads", p))
31522 result = PRAGMA_OMP_CLAUSE_THREADS;
31523 else if (!strcmp ("tile", p))
31524 result = PRAGMA_OACC_CLAUSE_TILE;
31525 else if (!strcmp ("to", p))
31526 result = PRAGMA_OMP_CLAUSE_TO;
31527 break;
31528 case 'u':
31529 if (!strcmp ("uniform", p))
31530 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31531 else if (!strcmp ("untied", p))
31532 result = PRAGMA_OMP_CLAUSE_UNTIED;
31533 else if (!strcmp ("use_device", p))
31534 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31535 else if (!strcmp ("use_device_ptr", p))
31536 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31537 break;
31538 case 'v':
31539 if (!strcmp ("vector", p))
31540 result = PRAGMA_OACC_CLAUSE_VECTOR;
31541 else if (!strcmp ("vector_length", p))
31542 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31543 break;
31544 case 'w':
31545 if (!strcmp ("wait", p))
31546 result = PRAGMA_OACC_CLAUSE_WAIT;
31547 else if (!strcmp ("worker", p))
31548 result = PRAGMA_OACC_CLAUSE_WORKER;
31549 break;
31553 if (result != PRAGMA_OMP_CLAUSE_NONE)
31554 cp_lexer_consume_token (parser->lexer);
31556 return result;
31559 /* Validate that a clause of the given type does not already exist. */
31561 static void
31562 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31563 const char *name, location_t location)
31565 tree c;
31567 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31568 if (OMP_CLAUSE_CODE (c) == code)
31570 error_at (location, "too many %qs clauses", name);
31571 break;
31575 /* OpenMP 2.5:
31576 variable-list:
31577 identifier
31578 variable-list , identifier
31580 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31581 colon). An opening parenthesis will have been consumed by the caller.
31583 If KIND is nonzero, create the appropriate node and install the decl
31584 in OMP_CLAUSE_DECL and add the node to the head of the list.
31586 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31587 return the list created.
31589 COLON can be NULL if only closing parenthesis should end the list,
31590 or pointer to bool which will receive false if the list is terminated
31591 by closing parenthesis or true if the list is terminated by colon. */
31593 static tree
31594 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31595 tree list, bool *colon)
31597 cp_token *token;
31598 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31599 if (colon)
31601 parser->colon_corrects_to_scope_p = false;
31602 *colon = false;
31604 while (1)
31606 tree name, decl;
31608 token = cp_lexer_peek_token (parser->lexer);
31609 if (kind != 0
31610 && current_class_ptr
31611 && cp_parser_is_keyword (token, RID_THIS))
31613 decl = finish_this_expr ();
31614 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31615 || CONVERT_EXPR_P (decl))
31616 decl = TREE_OPERAND (decl, 0);
31617 cp_lexer_consume_token (parser->lexer);
31619 else
31621 name = cp_parser_id_expression (parser, /*template_p=*/false,
31622 /*check_dependency_p=*/true,
31623 /*template_p=*/NULL,
31624 /*declarator_p=*/false,
31625 /*optional_p=*/false);
31626 if (name == error_mark_node)
31627 goto skip_comma;
31629 if (identifier_p (name))
31630 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31631 else
31632 decl = name;
31633 if (decl == error_mark_node)
31634 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31635 token->location);
31637 if (decl == error_mark_node)
31639 else if (kind != 0)
31641 switch (kind)
31643 case OMP_CLAUSE__CACHE_:
31644 /* The OpenACC cache directive explicitly only allows "array
31645 elements or subarrays". */
31646 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31648 error_at (token->location, "expected %<[%>");
31649 decl = error_mark_node;
31650 break;
31652 /* FALLTHROUGH. */
31653 case OMP_CLAUSE_MAP:
31654 case OMP_CLAUSE_FROM:
31655 case OMP_CLAUSE_TO:
31656 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31658 location_t loc
31659 = cp_lexer_peek_token (parser->lexer)->location;
31660 cp_id_kind idk = CP_ID_KIND_NONE;
31661 cp_lexer_consume_token (parser->lexer);
31662 decl = convert_from_reference (decl);
31663 decl
31664 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31665 decl, false,
31666 &idk, loc);
31668 /* FALLTHROUGH. */
31669 case OMP_CLAUSE_DEPEND:
31670 case OMP_CLAUSE_REDUCTION:
31671 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31673 tree low_bound = NULL_TREE, length = NULL_TREE;
31675 parser->colon_corrects_to_scope_p = false;
31676 cp_lexer_consume_token (parser->lexer);
31677 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31678 low_bound = cp_parser_expression (parser);
31679 if (!colon)
31680 parser->colon_corrects_to_scope_p
31681 = saved_colon_corrects_to_scope_p;
31682 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31683 length = integer_one_node;
31684 else
31686 /* Look for `:'. */
31687 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31688 goto skip_comma;
31689 if (!cp_lexer_next_token_is (parser->lexer,
31690 CPP_CLOSE_SQUARE))
31691 length = cp_parser_expression (parser);
31693 /* Look for the closing `]'. */
31694 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31695 RT_CLOSE_SQUARE))
31696 goto skip_comma;
31698 decl = tree_cons (low_bound, length, decl);
31700 break;
31701 default:
31702 break;
31705 tree u = build_omp_clause (token->location, kind);
31706 OMP_CLAUSE_DECL (u) = decl;
31707 OMP_CLAUSE_CHAIN (u) = list;
31708 list = u;
31710 else
31711 list = tree_cons (decl, NULL_TREE, list);
31713 get_comma:
31714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31715 break;
31716 cp_lexer_consume_token (parser->lexer);
31719 if (colon)
31720 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31722 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31724 *colon = true;
31725 cp_parser_require (parser, CPP_COLON, RT_COLON);
31726 return list;
31729 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31731 int ending;
31733 /* Try to resync to an unnested comma. Copied from
31734 cp_parser_parenthesized_expression_list. */
31735 skip_comma:
31736 if (colon)
31737 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31738 ending = cp_parser_skip_to_closing_parenthesis (parser,
31739 /*recovering=*/true,
31740 /*or_comma=*/true,
31741 /*consume_paren=*/true);
31742 if (ending < 0)
31743 goto get_comma;
31746 return list;
31749 /* Similarly, but expect leading and trailing parenthesis. This is a very
31750 common case for omp clauses. */
31752 static tree
31753 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31755 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31756 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31757 return list;
31760 /* OpenACC 2.0:
31761 copy ( variable-list )
31762 copyin ( variable-list )
31763 copyout ( variable-list )
31764 create ( variable-list )
31765 delete ( variable-list )
31766 present ( variable-list ) */
31768 static tree
31769 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31770 tree list)
31772 enum gomp_map_kind kind;
31773 switch (c_kind)
31775 case PRAGMA_OACC_CLAUSE_COPY:
31776 kind = GOMP_MAP_TOFROM;
31777 break;
31778 case PRAGMA_OACC_CLAUSE_COPYIN:
31779 kind = GOMP_MAP_TO;
31780 break;
31781 case PRAGMA_OACC_CLAUSE_COPYOUT:
31782 kind = GOMP_MAP_FROM;
31783 break;
31784 case PRAGMA_OACC_CLAUSE_CREATE:
31785 kind = GOMP_MAP_ALLOC;
31786 break;
31787 case PRAGMA_OACC_CLAUSE_DELETE:
31788 kind = GOMP_MAP_RELEASE;
31789 break;
31790 case PRAGMA_OACC_CLAUSE_DEVICE:
31791 kind = GOMP_MAP_FORCE_TO;
31792 break;
31793 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31794 kind = GOMP_MAP_DEVICE_RESIDENT;
31795 break;
31796 case PRAGMA_OACC_CLAUSE_HOST:
31797 kind = GOMP_MAP_FORCE_FROM;
31798 break;
31799 case PRAGMA_OACC_CLAUSE_LINK:
31800 kind = GOMP_MAP_LINK;
31801 break;
31802 case PRAGMA_OACC_CLAUSE_PRESENT:
31803 kind = GOMP_MAP_FORCE_PRESENT;
31804 break;
31805 default:
31806 gcc_unreachable ();
31808 tree nl, c;
31809 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31811 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31812 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31814 return nl;
31817 /* OpenACC 2.0:
31818 deviceptr ( variable-list ) */
31820 static tree
31821 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31823 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31824 tree vars, t;
31826 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31827 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31828 variable-list must only allow for pointer variables. */
31829 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31830 for (t = vars; t; t = TREE_CHAIN (t))
31832 tree v = TREE_PURPOSE (t);
31833 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31834 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31835 OMP_CLAUSE_DECL (u) = v;
31836 OMP_CLAUSE_CHAIN (u) = list;
31837 list = u;
31840 return list;
31843 /* OpenACC 2.5:
31844 auto
31845 finalize
31846 independent
31847 nohost
31848 seq */
31850 static tree
31851 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31852 enum omp_clause_code code,
31853 tree list, location_t location)
31855 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31856 tree c = build_omp_clause (location, code);
31857 OMP_CLAUSE_CHAIN (c) = list;
31858 return c;
31861 /* OpenACC:
31862 num_gangs ( expression )
31863 num_workers ( expression )
31864 vector_length ( expression ) */
31866 static tree
31867 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31868 const char *str, tree list)
31870 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31872 matching_parens parens;
31873 if (!parens.require_open (parser))
31874 return list;
31876 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31878 if (t == error_mark_node
31879 || !parens.require_close (parser))
31881 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31882 /*or_comma=*/false,
31883 /*consume_paren=*/true);
31884 return list;
31887 check_no_duplicate_clause (list, code, str, loc);
31889 tree c = build_omp_clause (loc, code);
31890 OMP_CLAUSE_OPERAND (c, 0) = t;
31891 OMP_CLAUSE_CHAIN (c) = list;
31892 return c;
31895 /* OpenACC:
31897 gang [( gang-arg-list )]
31898 worker [( [num:] int-expr )]
31899 vector [( [length:] int-expr )]
31901 where gang-arg is one of:
31903 [num:] int-expr
31904 static: size-expr
31906 and size-expr may be:
31909 int-expr
31912 static tree
31913 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31914 const char *str, tree list)
31916 const char *id = "num";
31917 cp_lexer *lexer = parser->lexer;
31918 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31919 location_t loc = cp_lexer_peek_token (lexer)->location;
31921 if (kind == OMP_CLAUSE_VECTOR)
31922 id = "length";
31924 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31926 matching_parens parens;
31927 parens.consume_open (parser);
31931 cp_token *next = cp_lexer_peek_token (lexer);
31932 int idx = 0;
31934 /* Gang static argument. */
31935 if (kind == OMP_CLAUSE_GANG
31936 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31938 cp_lexer_consume_token (lexer);
31940 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31941 goto cleanup_error;
31943 idx = 1;
31944 if (ops[idx] != NULL)
31946 cp_parser_error (parser, "too many %<static%> arguments");
31947 goto cleanup_error;
31950 /* Check for the '*' argument. */
31951 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31952 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31953 || cp_lexer_nth_token_is (parser->lexer, 2,
31954 CPP_CLOSE_PAREN)))
31956 cp_lexer_consume_token (lexer);
31957 ops[idx] = integer_minus_one_node;
31959 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31961 cp_lexer_consume_token (lexer);
31962 continue;
31964 else break;
31967 /* Worker num: argument and vector length: arguments. */
31968 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31969 && id_equal (next->u.value, id)
31970 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31972 cp_lexer_consume_token (lexer); /* id */
31973 cp_lexer_consume_token (lexer); /* ':' */
31976 /* Now collect the actual argument. */
31977 if (ops[idx] != NULL_TREE)
31979 cp_parser_error (parser, "unexpected argument");
31980 goto cleanup_error;
31983 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31984 false);
31985 if (expr == error_mark_node)
31986 goto cleanup_error;
31988 mark_exp_read (expr);
31989 ops[idx] = expr;
31991 if (kind == OMP_CLAUSE_GANG
31992 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31994 cp_lexer_consume_token (lexer);
31995 continue;
31997 break;
31999 while (1);
32001 if (!parens.require_close (parser))
32002 goto cleanup_error;
32005 check_no_duplicate_clause (list, kind, str, loc);
32007 c = build_omp_clause (loc, kind);
32009 if (ops[1])
32010 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32012 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32013 OMP_CLAUSE_CHAIN (c) = list;
32015 return c;
32017 cleanup_error:
32018 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32019 return list;
32022 /* OpenACC 2.0:
32023 tile ( size-expr-list ) */
32025 static tree
32026 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32028 tree c, expr = error_mark_node;
32029 tree tile = NULL_TREE;
32031 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32032 so, but the spec authors never considered such a case and have
32033 differing opinions on what it might mean, including 'not
32034 allowed'.) */
32035 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32036 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32037 clause_loc);
32039 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32040 return list;
32044 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32045 return list;
32047 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32048 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32049 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32051 cp_lexer_consume_token (parser->lexer);
32052 expr = integer_zero_node;
32054 else
32055 expr = cp_parser_constant_expression (parser);
32057 tile = tree_cons (NULL_TREE, expr, tile);
32059 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32061 /* Consume the trailing ')'. */
32062 cp_lexer_consume_token (parser->lexer);
32064 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32065 tile = nreverse (tile);
32066 OMP_CLAUSE_TILE_LIST (c) = tile;
32067 OMP_CLAUSE_CHAIN (c) = list;
32068 return c;
32071 /* OpenACC 2.0
32072 Parse wait clause or directive parameters. */
32074 static tree
32075 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32077 vec<tree, va_gc> *args;
32078 tree t, args_tree;
32080 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32081 /*cast_p=*/false,
32082 /*allow_expansion_p=*/true,
32083 /*non_constant_p=*/NULL);
32085 if (args == NULL || args->length () == 0)
32087 cp_parser_error (parser, "expected integer expression before ')'");
32088 if (args != NULL)
32089 release_tree_vector (args);
32090 return list;
32093 args_tree = build_tree_list_vec (args);
32095 release_tree_vector (args);
32097 for (t = args_tree; t; t = TREE_CHAIN (t))
32099 tree targ = TREE_VALUE (t);
32101 if (targ != error_mark_node)
32103 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32104 error ("%<wait%> expression must be integral");
32105 else
32107 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32109 targ = mark_rvalue_use (targ);
32110 OMP_CLAUSE_DECL (c) = targ;
32111 OMP_CLAUSE_CHAIN (c) = list;
32112 list = c;
32117 return list;
32120 /* OpenACC:
32121 wait ( int-expr-list ) */
32123 static tree
32124 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32126 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32128 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32129 return list;
32131 list = cp_parser_oacc_wait_list (parser, location, list);
32133 return list;
32136 /* OpenMP 3.0:
32137 collapse ( constant-expression ) */
32139 static tree
32140 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32142 tree c, num;
32143 location_t loc;
32144 HOST_WIDE_INT n;
32146 loc = cp_lexer_peek_token (parser->lexer)->location;
32147 matching_parens parens;
32148 if (!parens.require_open (parser))
32149 return list;
32151 num = cp_parser_constant_expression (parser);
32153 if (!parens.require_close (parser))
32154 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32155 /*or_comma=*/false,
32156 /*consume_paren=*/true);
32158 if (num == error_mark_node)
32159 return list;
32160 num = fold_non_dependent_expr (num);
32161 if (!tree_fits_shwi_p (num)
32162 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32163 || (n = tree_to_shwi (num)) <= 0
32164 || (int) n != n)
32166 error_at (loc, "collapse argument needs positive constant integer expression");
32167 return list;
32170 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32171 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32172 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32173 OMP_CLAUSE_CHAIN (c) = list;
32174 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32176 return c;
32179 /* OpenMP 2.5:
32180 default ( none | shared )
32182 OpenACC:
32183 default ( none | present ) */
32185 static tree
32186 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32187 location_t location, bool is_oacc)
32189 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32190 tree c;
32192 matching_parens parens;
32193 if (!parens.require_open (parser))
32194 return list;
32195 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32197 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32198 const char *p = IDENTIFIER_POINTER (id);
32200 switch (p[0])
32202 case 'n':
32203 if (strcmp ("none", p) != 0)
32204 goto invalid_kind;
32205 kind = OMP_CLAUSE_DEFAULT_NONE;
32206 break;
32208 case 'p':
32209 if (strcmp ("present", p) != 0 || !is_oacc)
32210 goto invalid_kind;
32211 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32212 break;
32214 case 's':
32215 if (strcmp ("shared", p) != 0 || is_oacc)
32216 goto invalid_kind;
32217 kind = OMP_CLAUSE_DEFAULT_SHARED;
32218 break;
32220 default:
32221 goto invalid_kind;
32224 cp_lexer_consume_token (parser->lexer);
32226 else
32228 invalid_kind:
32229 if (is_oacc)
32230 cp_parser_error (parser, "expected %<none%> or %<present%>");
32231 else
32232 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32235 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32236 || !parens.require_close (parser))
32237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32238 /*or_comma=*/false,
32239 /*consume_paren=*/true);
32241 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32242 return list;
32244 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32245 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32246 OMP_CLAUSE_CHAIN (c) = list;
32247 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32249 return c;
32252 /* OpenMP 3.1:
32253 final ( expression ) */
32255 static tree
32256 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32258 tree t, c;
32260 matching_parens parens;
32261 if (!parens.require_open (parser))
32262 return list;
32264 t = cp_parser_condition (parser);
32266 if (t == error_mark_node
32267 || !parens.require_close (parser))
32268 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32269 /*or_comma=*/false,
32270 /*consume_paren=*/true);
32272 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32274 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32275 OMP_CLAUSE_FINAL_EXPR (c) = t;
32276 OMP_CLAUSE_CHAIN (c) = list;
32278 return c;
32281 /* OpenMP 2.5:
32282 if ( expression )
32284 OpenMP 4.5:
32285 if ( directive-name-modifier : expression )
32287 directive-name-modifier:
32288 parallel | task | taskloop | target data | target | target update
32289 | target enter data | target exit data */
32291 static tree
32292 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32293 bool is_omp)
32295 tree t, c;
32296 enum tree_code if_modifier = ERROR_MARK;
32298 matching_parens parens;
32299 if (!parens.require_open (parser))
32300 return list;
32302 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32304 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32305 const char *p = IDENTIFIER_POINTER (id);
32306 int n = 2;
32308 if (strcmp ("parallel", p) == 0)
32309 if_modifier = OMP_PARALLEL;
32310 else if (strcmp ("task", p) == 0)
32311 if_modifier = OMP_TASK;
32312 else if (strcmp ("taskloop", p) == 0)
32313 if_modifier = OMP_TASKLOOP;
32314 else if (strcmp ("target", p) == 0)
32316 if_modifier = OMP_TARGET;
32317 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32319 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32320 p = IDENTIFIER_POINTER (id);
32321 if (strcmp ("data", p) == 0)
32322 if_modifier = OMP_TARGET_DATA;
32323 else if (strcmp ("update", p) == 0)
32324 if_modifier = OMP_TARGET_UPDATE;
32325 else if (strcmp ("enter", p) == 0)
32326 if_modifier = OMP_TARGET_ENTER_DATA;
32327 else if (strcmp ("exit", p) == 0)
32328 if_modifier = OMP_TARGET_EXIT_DATA;
32329 if (if_modifier != OMP_TARGET)
32330 n = 3;
32331 else
32333 location_t loc
32334 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32335 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32336 "or %<exit%>");
32337 if_modifier = ERROR_MARK;
32339 if (if_modifier == OMP_TARGET_ENTER_DATA
32340 || if_modifier == OMP_TARGET_EXIT_DATA)
32342 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32344 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32345 p = IDENTIFIER_POINTER (id);
32346 if (strcmp ("data", p) == 0)
32347 n = 4;
32349 if (n != 4)
32351 location_t loc
32352 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32353 error_at (loc, "expected %<data%>");
32354 if_modifier = ERROR_MARK;
32359 if (if_modifier != ERROR_MARK)
32361 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32363 while (n-- > 0)
32364 cp_lexer_consume_token (parser->lexer);
32366 else
32368 if (n > 2)
32370 location_t loc
32371 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32372 error_at (loc, "expected %<:%>");
32374 if_modifier = ERROR_MARK;
32379 t = cp_parser_condition (parser);
32381 if (t == error_mark_node
32382 || !parens.require_close (parser))
32383 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32384 /*or_comma=*/false,
32385 /*consume_paren=*/true);
32387 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32388 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32390 if (if_modifier != ERROR_MARK
32391 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32393 const char *p = NULL;
32394 switch (if_modifier)
32396 case OMP_PARALLEL: p = "parallel"; break;
32397 case OMP_TASK: p = "task"; break;
32398 case OMP_TASKLOOP: p = "taskloop"; break;
32399 case OMP_TARGET_DATA: p = "target data"; break;
32400 case OMP_TARGET: p = "target"; break;
32401 case OMP_TARGET_UPDATE: p = "target update"; break;
32402 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32403 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32404 default: gcc_unreachable ();
32406 error_at (location, "too many %<if%> clauses with %qs modifier",
32408 return list;
32410 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32412 if (!is_omp)
32413 error_at (location, "too many %<if%> clauses");
32414 else
32415 error_at (location, "too many %<if%> clauses without modifier");
32416 return list;
32418 else if (if_modifier == ERROR_MARK
32419 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32421 error_at (location, "if any %<if%> clause has modifier, then all "
32422 "%<if%> clauses have to use modifier");
32423 return list;
32427 c = build_omp_clause (location, OMP_CLAUSE_IF);
32428 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32429 OMP_CLAUSE_IF_EXPR (c) = t;
32430 OMP_CLAUSE_CHAIN (c) = list;
32432 return c;
32435 /* OpenMP 3.1:
32436 mergeable */
32438 static tree
32439 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32440 tree list, location_t location)
32442 tree c;
32444 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32445 location);
32447 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32448 OMP_CLAUSE_CHAIN (c) = list;
32449 return c;
32452 /* OpenMP 2.5:
32453 nowait */
32455 static tree
32456 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32457 tree list, location_t location)
32459 tree c;
32461 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32463 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32464 OMP_CLAUSE_CHAIN (c) = list;
32465 return c;
32468 /* OpenMP 2.5:
32469 num_threads ( expression ) */
32471 static tree
32472 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32473 location_t location)
32475 tree t, c;
32477 matching_parens parens;
32478 if (!parens.require_open (parser))
32479 return list;
32481 t = cp_parser_expression (parser);
32483 if (t == error_mark_node
32484 || !parens.require_close (parser))
32485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32486 /*or_comma=*/false,
32487 /*consume_paren=*/true);
32489 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32490 "num_threads", location);
32492 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32493 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32494 OMP_CLAUSE_CHAIN (c) = list;
32496 return c;
32499 /* OpenMP 4.5:
32500 num_tasks ( expression ) */
32502 static tree
32503 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32504 location_t location)
32506 tree t, c;
32508 matching_parens parens;
32509 if (!parens.require_open (parser))
32510 return list;
32512 t = cp_parser_expression (parser);
32514 if (t == error_mark_node
32515 || !parens.require_close (parser))
32516 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32517 /*or_comma=*/false,
32518 /*consume_paren=*/true);
32520 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32521 "num_tasks", location);
32523 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32524 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32525 OMP_CLAUSE_CHAIN (c) = list;
32527 return c;
32530 /* OpenMP 4.5:
32531 grainsize ( expression ) */
32533 static tree
32534 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32535 location_t location)
32537 tree t, c;
32539 matching_parens parens;
32540 if (!parens.require_open (parser))
32541 return list;
32543 t = cp_parser_expression (parser);
32545 if (t == error_mark_node
32546 || !parens.require_close (parser))
32547 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32548 /*or_comma=*/false,
32549 /*consume_paren=*/true);
32551 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32552 "grainsize", location);
32554 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32555 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32556 OMP_CLAUSE_CHAIN (c) = list;
32558 return c;
32561 /* OpenMP 4.5:
32562 priority ( expression ) */
32564 static tree
32565 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32566 location_t location)
32568 tree t, c;
32570 matching_parens parens;
32571 if (!parens.require_open (parser))
32572 return list;
32574 t = cp_parser_expression (parser);
32576 if (t == error_mark_node
32577 || !parens.require_close (parser))
32578 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32579 /*or_comma=*/false,
32580 /*consume_paren=*/true);
32582 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32583 "priority", location);
32585 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32586 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32587 OMP_CLAUSE_CHAIN (c) = list;
32589 return c;
32592 /* OpenMP 4.5:
32593 hint ( expression ) */
32595 static tree
32596 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32597 location_t location)
32599 tree t, c;
32601 matching_parens parens;
32602 if (!parens.require_open (parser))
32603 return list;
32605 t = cp_parser_expression (parser);
32607 if (t == error_mark_node
32608 || !parens.require_close (parser))
32609 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32610 /*or_comma=*/false,
32611 /*consume_paren=*/true);
32613 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32615 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32616 OMP_CLAUSE_HINT_EXPR (c) = t;
32617 OMP_CLAUSE_CHAIN (c) = list;
32619 return c;
32622 /* OpenMP 4.5:
32623 defaultmap ( tofrom : scalar ) */
32625 static tree
32626 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32627 location_t location)
32629 tree c, id;
32630 const char *p;
32632 matching_parens parens;
32633 if (!parens.require_open (parser))
32634 return list;
32636 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32638 cp_parser_error (parser, "expected %<tofrom%>");
32639 goto out_err;
32641 id = cp_lexer_peek_token (parser->lexer)->u.value;
32642 p = IDENTIFIER_POINTER (id);
32643 if (strcmp (p, "tofrom") != 0)
32645 cp_parser_error (parser, "expected %<tofrom%>");
32646 goto out_err;
32648 cp_lexer_consume_token (parser->lexer);
32649 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32650 goto out_err;
32652 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32654 cp_parser_error (parser, "expected %<scalar%>");
32655 goto out_err;
32657 id = cp_lexer_peek_token (parser->lexer)->u.value;
32658 p = IDENTIFIER_POINTER (id);
32659 if (strcmp (p, "scalar") != 0)
32661 cp_parser_error (parser, "expected %<scalar%>");
32662 goto out_err;
32664 cp_lexer_consume_token (parser->lexer);
32665 if (!parens.require_close (parser))
32666 goto out_err;
32668 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32669 location);
32671 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32672 OMP_CLAUSE_CHAIN (c) = list;
32673 return c;
32675 out_err:
32676 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32677 /*or_comma=*/false,
32678 /*consume_paren=*/true);
32679 return list;
32682 /* OpenMP 2.5:
32683 ordered
32685 OpenMP 4.5:
32686 ordered ( constant-expression ) */
32688 static tree
32689 cp_parser_omp_clause_ordered (cp_parser *parser,
32690 tree list, location_t location)
32692 tree c, num = NULL_TREE;
32693 HOST_WIDE_INT n;
32695 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32696 "ordered", location);
32698 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32700 matching_parens parens;
32701 parens.consume_open (parser);
32703 num = cp_parser_constant_expression (parser);
32705 if (!parens.require_close (parser))
32706 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32707 /*or_comma=*/false,
32708 /*consume_paren=*/true);
32710 if (num == error_mark_node)
32711 return list;
32712 num = fold_non_dependent_expr (num);
32713 if (!tree_fits_shwi_p (num)
32714 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32715 || (n = tree_to_shwi (num)) <= 0
32716 || (int) n != n)
32718 error_at (location,
32719 "ordered argument needs positive constant integer "
32720 "expression");
32721 return list;
32725 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32726 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32727 OMP_CLAUSE_CHAIN (c) = list;
32728 return c;
32731 /* OpenMP 2.5:
32732 reduction ( reduction-operator : variable-list )
32734 reduction-operator:
32735 One of: + * - & ^ | && ||
32737 OpenMP 3.1:
32739 reduction-operator:
32740 One of: + * - & ^ | && || min max
32742 OpenMP 4.0:
32744 reduction-operator:
32745 One of: + * - & ^ | && ||
32746 id-expression */
32748 static tree
32749 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32751 enum tree_code code = ERROR_MARK;
32752 tree nlist, c, id = NULL_TREE;
32754 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32755 return list;
32757 switch (cp_lexer_peek_token (parser->lexer)->type)
32759 case CPP_PLUS: code = PLUS_EXPR; break;
32760 case CPP_MULT: code = MULT_EXPR; break;
32761 case CPP_MINUS: code = MINUS_EXPR; break;
32762 case CPP_AND: code = BIT_AND_EXPR; break;
32763 case CPP_XOR: code = BIT_XOR_EXPR; break;
32764 case CPP_OR: code = BIT_IOR_EXPR; break;
32765 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32766 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32767 default: break;
32770 if (code != ERROR_MARK)
32771 cp_lexer_consume_token (parser->lexer);
32772 else
32774 bool saved_colon_corrects_to_scope_p;
32775 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32776 parser->colon_corrects_to_scope_p = false;
32777 id = cp_parser_id_expression (parser, /*template_p=*/false,
32778 /*check_dependency_p=*/true,
32779 /*template_p=*/NULL,
32780 /*declarator_p=*/false,
32781 /*optional_p=*/false);
32782 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32783 if (identifier_p (id))
32785 const char *p = IDENTIFIER_POINTER (id);
32787 if (strcmp (p, "min") == 0)
32788 code = MIN_EXPR;
32789 else if (strcmp (p, "max") == 0)
32790 code = MAX_EXPR;
32791 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32792 code = PLUS_EXPR;
32793 else if (id == ovl_op_identifier (false, MULT_EXPR))
32794 code = MULT_EXPR;
32795 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32796 code = MINUS_EXPR;
32797 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32798 code = BIT_AND_EXPR;
32799 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32800 code = BIT_IOR_EXPR;
32801 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32802 code = BIT_XOR_EXPR;
32803 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32804 code = TRUTH_ANDIF_EXPR;
32805 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32806 code = TRUTH_ORIF_EXPR;
32807 id = omp_reduction_id (code, id, NULL_TREE);
32808 tree scope = parser->scope;
32809 if (scope)
32810 id = build_qualified_name (NULL_TREE, scope, id, false);
32811 parser->scope = NULL_TREE;
32812 parser->qualifying_scope = NULL_TREE;
32813 parser->object_scope = NULL_TREE;
32815 else
32817 error ("invalid reduction-identifier");
32818 resync_fail:
32819 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32820 /*or_comma=*/false,
32821 /*consume_paren=*/true);
32822 return list;
32826 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32827 goto resync_fail;
32829 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32830 NULL);
32831 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32833 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32834 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32837 return nlist;
32840 /* OpenMP 2.5:
32841 schedule ( schedule-kind )
32842 schedule ( schedule-kind , expression )
32844 schedule-kind:
32845 static | dynamic | guided | runtime | auto
32847 OpenMP 4.5:
32848 schedule ( schedule-modifier : schedule-kind )
32849 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32851 schedule-modifier:
32852 simd
32853 monotonic
32854 nonmonotonic */
32856 static tree
32857 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32859 tree c, t;
32860 int modifiers = 0, nmodifiers = 0;
32862 matching_parens parens;
32863 if (!parens.require_open (parser))
32864 return list;
32866 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32868 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32870 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32871 const char *p = IDENTIFIER_POINTER (id);
32872 if (strcmp ("simd", p) == 0)
32873 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32874 else if (strcmp ("monotonic", p) == 0)
32875 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32876 else if (strcmp ("nonmonotonic", p) == 0)
32877 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32878 else
32879 break;
32880 cp_lexer_consume_token (parser->lexer);
32881 if (nmodifiers++ == 0
32882 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32883 cp_lexer_consume_token (parser->lexer);
32884 else
32886 cp_parser_require (parser, CPP_COLON, RT_COLON);
32887 break;
32891 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32893 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32894 const char *p = IDENTIFIER_POINTER (id);
32896 switch (p[0])
32898 case 'd':
32899 if (strcmp ("dynamic", p) != 0)
32900 goto invalid_kind;
32901 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32902 break;
32904 case 'g':
32905 if (strcmp ("guided", p) != 0)
32906 goto invalid_kind;
32907 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32908 break;
32910 case 'r':
32911 if (strcmp ("runtime", p) != 0)
32912 goto invalid_kind;
32913 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32914 break;
32916 default:
32917 goto invalid_kind;
32920 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32921 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32922 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32923 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32924 else
32925 goto invalid_kind;
32926 cp_lexer_consume_token (parser->lexer);
32928 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32929 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32930 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32931 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32933 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32934 "specified");
32935 modifiers = 0;
32938 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32940 cp_token *token;
32941 cp_lexer_consume_token (parser->lexer);
32943 token = cp_lexer_peek_token (parser->lexer);
32944 t = cp_parser_assignment_expression (parser);
32946 if (t == error_mark_node)
32947 goto resync_fail;
32948 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32949 error_at (token->location, "schedule %<runtime%> does not take "
32950 "a %<chunk_size%> parameter");
32951 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32952 error_at (token->location, "schedule %<auto%> does not take "
32953 "a %<chunk_size%> parameter");
32954 else
32955 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32957 if (!parens.require_close (parser))
32958 goto resync_fail;
32960 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32961 goto resync_fail;
32963 OMP_CLAUSE_SCHEDULE_KIND (c)
32964 = (enum omp_clause_schedule_kind)
32965 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32967 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32968 OMP_CLAUSE_CHAIN (c) = list;
32969 return c;
32971 invalid_kind:
32972 cp_parser_error (parser, "invalid schedule kind");
32973 resync_fail:
32974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32975 /*or_comma=*/false,
32976 /*consume_paren=*/true);
32977 return list;
32980 /* OpenMP 3.0:
32981 untied */
32983 static tree
32984 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32985 tree list, location_t location)
32987 tree c;
32989 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32991 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32992 OMP_CLAUSE_CHAIN (c) = list;
32993 return c;
32996 /* OpenMP 4.0:
32997 inbranch
32998 notinbranch */
33000 static tree
33001 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33002 tree list, location_t location)
33004 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33005 tree c = build_omp_clause (location, code);
33006 OMP_CLAUSE_CHAIN (c) = list;
33007 return c;
33010 /* OpenMP 4.0:
33011 parallel
33013 sections
33014 taskgroup */
33016 static tree
33017 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33018 enum omp_clause_code code,
33019 tree list, location_t location)
33021 tree c = build_omp_clause (location, code);
33022 OMP_CLAUSE_CHAIN (c) = list;
33023 return c;
33026 /* OpenMP 4.5:
33027 nogroup */
33029 static tree
33030 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33031 tree list, location_t location)
33033 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33034 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33035 OMP_CLAUSE_CHAIN (c) = list;
33036 return c;
33039 /* OpenMP 4.5:
33040 simd
33041 threads */
33043 static tree
33044 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33045 enum omp_clause_code code,
33046 tree list, location_t location)
33048 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33049 tree c = build_omp_clause (location, code);
33050 OMP_CLAUSE_CHAIN (c) = list;
33051 return c;
33054 /* OpenMP 4.0:
33055 num_teams ( expression ) */
33057 static tree
33058 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33059 location_t location)
33061 tree t, c;
33063 matching_parens parens;
33064 if (!parens.require_open (parser))
33065 return list;
33067 t = cp_parser_expression (parser);
33069 if (t == error_mark_node
33070 || !parens.require_close (parser))
33071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33072 /*or_comma=*/false,
33073 /*consume_paren=*/true);
33075 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33076 "num_teams", location);
33078 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33079 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33080 OMP_CLAUSE_CHAIN (c) = list;
33082 return c;
33085 /* OpenMP 4.0:
33086 thread_limit ( expression ) */
33088 static tree
33089 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33090 location_t location)
33092 tree t, c;
33094 matching_parens parens;
33095 if (!parens.require_open (parser))
33096 return list;
33098 t = cp_parser_expression (parser);
33100 if (t == error_mark_node
33101 || !parens.require_close (parser))
33102 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33103 /*or_comma=*/false,
33104 /*consume_paren=*/true);
33106 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33107 "thread_limit", location);
33109 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33110 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33111 OMP_CLAUSE_CHAIN (c) = list;
33113 return c;
33116 /* OpenMP 4.0:
33117 aligned ( variable-list )
33118 aligned ( variable-list : constant-expression ) */
33120 static tree
33121 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33123 tree nlist, c, alignment = NULL_TREE;
33124 bool colon;
33126 matching_parens parens;
33127 if (!parens.require_open (parser))
33128 return list;
33130 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33131 &colon);
33133 if (colon)
33135 alignment = cp_parser_constant_expression (parser);
33137 if (!parens.require_close (parser))
33138 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33139 /*or_comma=*/false,
33140 /*consume_paren=*/true);
33142 if (alignment == error_mark_node)
33143 alignment = NULL_TREE;
33146 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33147 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33149 return nlist;
33152 /* OpenMP 4.0:
33153 linear ( variable-list )
33154 linear ( variable-list : expression )
33156 OpenMP 4.5:
33157 linear ( modifier ( variable-list ) )
33158 linear ( modifier ( variable-list ) : expression ) */
33160 static tree
33161 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33162 bool declare_simd)
33164 tree nlist, c, step = integer_one_node;
33165 bool colon;
33166 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33168 matching_parens parens;
33169 if (!parens.require_open (parser))
33170 return list;
33172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33174 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33175 const char *p = IDENTIFIER_POINTER (id);
33177 if (strcmp ("ref", p) == 0)
33178 kind = OMP_CLAUSE_LINEAR_REF;
33179 else if (strcmp ("val", p) == 0)
33180 kind = OMP_CLAUSE_LINEAR_VAL;
33181 else if (strcmp ("uval", p) == 0)
33182 kind = OMP_CLAUSE_LINEAR_UVAL;
33183 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33184 cp_lexer_consume_token (parser->lexer);
33185 else
33186 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33189 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33190 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33191 &colon);
33192 else
33194 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33195 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33196 if (colon)
33197 cp_parser_require (parser, CPP_COLON, RT_COLON);
33198 else if (!parens.require_close (parser))
33199 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33200 /*or_comma=*/false,
33201 /*consume_paren=*/true);
33204 if (colon)
33206 step = NULL_TREE;
33207 if (declare_simd
33208 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33209 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33211 cp_token *token = cp_lexer_peek_token (parser->lexer);
33212 cp_parser_parse_tentatively (parser);
33213 step = cp_parser_id_expression (parser, /*template_p=*/false,
33214 /*check_dependency_p=*/true,
33215 /*template_p=*/NULL,
33216 /*declarator_p=*/false,
33217 /*optional_p=*/false);
33218 if (step != error_mark_node)
33219 step = cp_parser_lookup_name_simple (parser, step, token->location);
33220 if (step == error_mark_node)
33222 step = NULL_TREE;
33223 cp_parser_abort_tentative_parse (parser);
33225 else if (!cp_parser_parse_definitely (parser))
33226 step = NULL_TREE;
33228 if (!step)
33229 step = cp_parser_expression (parser);
33231 if (!parens.require_close (parser))
33232 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33233 /*or_comma=*/false,
33234 /*consume_paren=*/true);
33236 if (step == error_mark_node)
33237 return list;
33240 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33242 OMP_CLAUSE_LINEAR_STEP (c) = step;
33243 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33246 return nlist;
33249 /* OpenMP 4.0:
33250 safelen ( constant-expression ) */
33252 static tree
33253 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33254 location_t location)
33256 tree t, c;
33258 matching_parens parens;
33259 if (!parens.require_open (parser))
33260 return list;
33262 t = cp_parser_constant_expression (parser);
33264 if (t == error_mark_node
33265 || !parens.require_close (parser))
33266 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33267 /*or_comma=*/false,
33268 /*consume_paren=*/true);
33270 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33272 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33273 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33274 OMP_CLAUSE_CHAIN (c) = list;
33276 return c;
33279 /* OpenMP 4.0:
33280 simdlen ( constant-expression ) */
33282 static tree
33283 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33284 location_t location)
33286 tree t, c;
33288 matching_parens parens;
33289 if (!parens.require_open (parser))
33290 return list;
33292 t = cp_parser_constant_expression (parser);
33294 if (t == error_mark_node
33295 || !parens.require_close (parser))
33296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33297 /*or_comma=*/false,
33298 /*consume_paren=*/true);
33300 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33302 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33303 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33304 OMP_CLAUSE_CHAIN (c) = list;
33306 return c;
33309 /* OpenMP 4.5:
33310 vec:
33311 identifier [+/- integer]
33312 vec , identifier [+/- integer]
33315 static tree
33316 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33317 tree list)
33319 tree vec = NULL;
33321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33323 cp_parser_error (parser, "expected identifier");
33324 return list;
33327 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33329 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33330 tree t, identifier = cp_parser_identifier (parser);
33331 tree addend = NULL;
33333 if (identifier == error_mark_node)
33334 t = error_mark_node;
33335 else
33337 t = cp_parser_lookup_name_simple
33338 (parser, identifier,
33339 cp_lexer_peek_token (parser->lexer)->location);
33340 if (t == error_mark_node)
33341 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33342 id_loc);
33345 bool neg = false;
33346 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33347 neg = true;
33348 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33350 addend = integer_zero_node;
33351 goto add_to_vector;
33353 cp_lexer_consume_token (parser->lexer);
33355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33357 cp_parser_error (parser, "expected integer");
33358 return list;
33361 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33362 if (TREE_CODE (addend) != INTEGER_CST)
33364 cp_parser_error (parser, "expected integer");
33365 return list;
33367 cp_lexer_consume_token (parser->lexer);
33369 add_to_vector:
33370 if (t != error_mark_node)
33372 vec = tree_cons (addend, t, vec);
33373 if (neg)
33374 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33378 break;
33380 cp_lexer_consume_token (parser->lexer);
33383 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33385 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33386 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33387 OMP_CLAUSE_DECL (u) = nreverse (vec);
33388 OMP_CLAUSE_CHAIN (u) = list;
33389 return u;
33391 return list;
33394 /* OpenMP 4.0:
33395 depend ( depend-kind : variable-list )
33397 depend-kind:
33398 in | out | inout
33400 OpenMP 4.5:
33401 depend ( source )
33403 depend ( sink : vec ) */
33405 static tree
33406 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33408 tree nlist, c;
33409 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33411 matching_parens parens;
33412 if (!parens.require_open (parser))
33413 return list;
33415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33417 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33418 const char *p = IDENTIFIER_POINTER (id);
33420 if (strcmp ("in", p) == 0)
33421 kind = OMP_CLAUSE_DEPEND_IN;
33422 else if (strcmp ("inout", p) == 0)
33423 kind = OMP_CLAUSE_DEPEND_INOUT;
33424 else if (strcmp ("out", p) == 0)
33425 kind = OMP_CLAUSE_DEPEND_OUT;
33426 else if (strcmp ("source", p) == 0)
33427 kind = OMP_CLAUSE_DEPEND_SOURCE;
33428 else if (strcmp ("sink", p) == 0)
33429 kind = OMP_CLAUSE_DEPEND_SINK;
33430 else
33431 goto invalid_kind;
33433 else
33434 goto invalid_kind;
33436 cp_lexer_consume_token (parser->lexer);
33438 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33440 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33441 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33442 OMP_CLAUSE_DECL (c) = NULL_TREE;
33443 OMP_CLAUSE_CHAIN (c) = list;
33444 if (!parens.require_close (parser))
33445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33446 /*or_comma=*/false,
33447 /*consume_paren=*/true);
33448 return c;
33451 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33452 goto resync_fail;
33454 if (kind == OMP_CLAUSE_DEPEND_SINK)
33455 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33456 else
33458 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33459 list, NULL);
33461 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33462 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33464 return nlist;
33466 invalid_kind:
33467 cp_parser_error (parser, "invalid depend kind");
33468 resync_fail:
33469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33470 /*or_comma=*/false,
33471 /*consume_paren=*/true);
33472 return list;
33475 /* OpenMP 4.0:
33476 map ( map-kind : variable-list )
33477 map ( variable-list )
33479 map-kind:
33480 alloc | to | from | tofrom
33482 OpenMP 4.5:
33483 map-kind:
33484 alloc | to | from | tofrom | release | delete
33486 map ( always [,] map-kind: variable-list ) */
33488 static tree
33489 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33491 tree nlist, c;
33492 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33493 bool always = false;
33495 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33496 return list;
33498 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33500 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33501 const char *p = IDENTIFIER_POINTER (id);
33503 if (strcmp ("always", p) == 0)
33505 int nth = 2;
33506 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33507 nth++;
33508 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33509 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33510 == RID_DELETE))
33511 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33512 == CPP_COLON))
33514 always = true;
33515 cp_lexer_consume_token (parser->lexer);
33516 if (nth == 3)
33517 cp_lexer_consume_token (parser->lexer);
33522 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33523 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33525 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33526 const char *p = IDENTIFIER_POINTER (id);
33528 if (strcmp ("alloc", p) == 0)
33529 kind = GOMP_MAP_ALLOC;
33530 else if (strcmp ("to", p) == 0)
33531 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33532 else if (strcmp ("from", p) == 0)
33533 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33534 else if (strcmp ("tofrom", p) == 0)
33535 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33536 else if (strcmp ("release", p) == 0)
33537 kind = GOMP_MAP_RELEASE;
33538 else
33540 cp_parser_error (parser, "invalid map kind");
33541 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33542 /*or_comma=*/false,
33543 /*consume_paren=*/true);
33544 return list;
33546 cp_lexer_consume_token (parser->lexer);
33547 cp_lexer_consume_token (parser->lexer);
33549 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33550 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33552 kind = GOMP_MAP_DELETE;
33553 cp_lexer_consume_token (parser->lexer);
33554 cp_lexer_consume_token (parser->lexer);
33557 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33558 NULL);
33560 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33561 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33563 return nlist;
33566 /* OpenMP 4.0:
33567 device ( expression ) */
33569 static tree
33570 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33571 location_t location)
33573 tree t, c;
33575 matching_parens parens;
33576 if (!parens.require_open (parser))
33577 return list;
33579 t = cp_parser_expression (parser);
33581 if (t == error_mark_node
33582 || !parens.require_close (parser))
33583 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33584 /*or_comma=*/false,
33585 /*consume_paren=*/true);
33587 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33588 "device", location);
33590 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33591 OMP_CLAUSE_DEVICE_ID (c) = t;
33592 OMP_CLAUSE_CHAIN (c) = list;
33594 return c;
33597 /* OpenMP 4.0:
33598 dist_schedule ( static )
33599 dist_schedule ( static , expression ) */
33601 static tree
33602 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33603 location_t location)
33605 tree c, t;
33607 matching_parens parens;
33608 if (!parens.require_open (parser))
33609 return list;
33611 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33613 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33614 goto invalid_kind;
33615 cp_lexer_consume_token (parser->lexer);
33617 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33619 cp_lexer_consume_token (parser->lexer);
33621 t = cp_parser_assignment_expression (parser);
33623 if (t == error_mark_node)
33624 goto resync_fail;
33625 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33627 if (!parens.require_close (parser))
33628 goto resync_fail;
33630 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33631 goto resync_fail;
33633 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33634 location);
33635 OMP_CLAUSE_CHAIN (c) = list;
33636 return c;
33638 invalid_kind:
33639 cp_parser_error (parser, "invalid dist_schedule kind");
33640 resync_fail:
33641 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33642 /*or_comma=*/false,
33643 /*consume_paren=*/true);
33644 return list;
33647 /* OpenMP 4.0:
33648 proc_bind ( proc-bind-kind )
33650 proc-bind-kind:
33651 master | close | spread */
33653 static tree
33654 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33655 location_t location)
33657 tree c;
33658 enum omp_clause_proc_bind_kind kind;
33660 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33661 return list;
33663 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33665 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33666 const char *p = IDENTIFIER_POINTER (id);
33668 if (strcmp ("master", p) == 0)
33669 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33670 else if (strcmp ("close", p) == 0)
33671 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33672 else if (strcmp ("spread", p) == 0)
33673 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33674 else
33675 goto invalid_kind;
33677 else
33678 goto invalid_kind;
33680 cp_lexer_consume_token (parser->lexer);
33681 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33682 goto resync_fail;
33684 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33685 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33686 location);
33687 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33688 OMP_CLAUSE_CHAIN (c) = list;
33689 return c;
33691 invalid_kind:
33692 cp_parser_error (parser, "invalid depend kind");
33693 resync_fail:
33694 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33695 /*or_comma=*/false,
33696 /*consume_paren=*/true);
33697 return list;
33700 /* OpenACC:
33701 async [( int-expr )] */
33703 static tree
33704 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33706 tree c, t;
33707 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33709 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33711 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33713 matching_parens parens;
33714 parens.consume_open (parser);
33716 t = cp_parser_expression (parser);
33717 if (t == error_mark_node
33718 || !parens.require_close (parser))
33719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33720 /*or_comma=*/false,
33721 /*consume_paren=*/true);
33724 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33726 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33727 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33728 OMP_CLAUSE_CHAIN (c) = list;
33729 list = c;
33731 return list;
33734 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33735 is a bitmask in MASK. Return the list of clauses found. */
33737 static tree
33738 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33739 const char *where, cp_token *pragma_tok,
33740 bool finish_p = true)
33742 tree clauses = NULL;
33743 bool first = true;
33745 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33747 location_t here;
33748 pragma_omp_clause c_kind;
33749 omp_clause_code code;
33750 const char *c_name;
33751 tree prev = clauses;
33753 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33754 cp_lexer_consume_token (parser->lexer);
33756 here = cp_lexer_peek_token (parser->lexer)->location;
33757 c_kind = cp_parser_omp_clause_name (parser);
33759 switch (c_kind)
33761 case PRAGMA_OACC_CLAUSE_ASYNC:
33762 clauses = cp_parser_oacc_clause_async (parser, clauses);
33763 c_name = "async";
33764 break;
33765 case PRAGMA_OACC_CLAUSE_AUTO:
33766 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33767 clauses, here);
33768 c_name = "auto";
33769 break;
33770 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33771 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33772 c_name = "collapse";
33773 break;
33774 case PRAGMA_OACC_CLAUSE_COPY:
33775 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33776 c_name = "copy";
33777 break;
33778 case PRAGMA_OACC_CLAUSE_COPYIN:
33779 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33780 c_name = "copyin";
33781 break;
33782 case PRAGMA_OACC_CLAUSE_COPYOUT:
33783 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33784 c_name = "copyout";
33785 break;
33786 case PRAGMA_OACC_CLAUSE_CREATE:
33787 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33788 c_name = "create";
33789 break;
33790 case PRAGMA_OACC_CLAUSE_DELETE:
33791 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33792 c_name = "delete";
33793 break;
33794 case PRAGMA_OMP_CLAUSE_DEFAULT:
33795 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33796 c_name = "default";
33797 break;
33798 case PRAGMA_OACC_CLAUSE_DEVICE:
33799 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33800 c_name = "device";
33801 break;
33802 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33803 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33804 c_name = "deviceptr";
33805 break;
33806 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33807 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33808 c_name = "device_resident";
33809 break;
33810 case PRAGMA_OACC_CLAUSE_FINALIZE:
33811 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
33812 clauses, here);
33813 c_name = "finalize";
33814 break;
33815 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33816 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33817 clauses);
33818 c_name = "firstprivate";
33819 break;
33820 case PRAGMA_OACC_CLAUSE_GANG:
33821 c_name = "gang";
33822 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33823 c_name, clauses);
33824 break;
33825 case PRAGMA_OACC_CLAUSE_HOST:
33826 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33827 c_name = "host";
33828 break;
33829 case PRAGMA_OACC_CLAUSE_IF:
33830 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33831 c_name = "if";
33832 break;
33833 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
33834 clauses = cp_parser_oacc_simple_clause (parser,
33835 OMP_CLAUSE_IF_PRESENT,
33836 clauses, here);
33837 c_name = "if_present";
33838 break;
33839 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33840 clauses = cp_parser_oacc_simple_clause (parser,
33841 OMP_CLAUSE_INDEPENDENT,
33842 clauses, here);
33843 c_name = "independent";
33844 break;
33845 case PRAGMA_OACC_CLAUSE_LINK:
33846 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33847 c_name = "link";
33848 break;
33849 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33850 code = OMP_CLAUSE_NUM_GANGS;
33851 c_name = "num_gangs";
33852 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33853 clauses);
33854 break;
33855 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33856 c_name = "num_workers";
33857 code = OMP_CLAUSE_NUM_WORKERS;
33858 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33859 clauses);
33860 break;
33861 case PRAGMA_OACC_CLAUSE_PRESENT:
33862 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33863 c_name = "present";
33864 break;
33865 case PRAGMA_OACC_CLAUSE_PRIVATE:
33866 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33867 clauses);
33868 c_name = "private";
33869 break;
33870 case PRAGMA_OACC_CLAUSE_REDUCTION:
33871 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33872 c_name = "reduction";
33873 break;
33874 case PRAGMA_OACC_CLAUSE_SEQ:
33875 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33876 clauses, here);
33877 c_name = "seq";
33878 break;
33879 case PRAGMA_OACC_CLAUSE_TILE:
33880 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33881 c_name = "tile";
33882 break;
33883 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33884 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33885 clauses);
33886 c_name = "use_device";
33887 break;
33888 case PRAGMA_OACC_CLAUSE_VECTOR:
33889 c_name = "vector";
33890 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33891 c_name, clauses);
33892 break;
33893 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33894 c_name = "vector_length";
33895 code = OMP_CLAUSE_VECTOR_LENGTH;
33896 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33897 clauses);
33898 break;
33899 case PRAGMA_OACC_CLAUSE_WAIT:
33900 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33901 c_name = "wait";
33902 break;
33903 case PRAGMA_OACC_CLAUSE_WORKER:
33904 c_name = "worker";
33905 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33906 c_name, clauses);
33907 break;
33908 default:
33909 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33910 goto saw_error;
33913 first = false;
33915 if (((mask >> c_kind) & 1) == 0)
33917 /* Remove the invalid clause(s) from the list to avoid
33918 confusing the rest of the compiler. */
33919 clauses = prev;
33920 error_at (here, "%qs is not valid for %qs", c_name, where);
33924 saw_error:
33925 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33927 if (finish_p)
33928 return finish_omp_clauses (clauses, C_ORT_ACC);
33930 return clauses;
33933 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33934 is a bitmask in MASK. Return the list of clauses found; the result
33935 of clause default goes in *pdefault. */
33937 static tree
33938 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33939 const char *where, cp_token *pragma_tok,
33940 bool finish_p = true)
33942 tree clauses = NULL;
33943 bool first = true;
33944 cp_token *token = NULL;
33946 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33948 pragma_omp_clause c_kind;
33949 const char *c_name;
33950 tree prev = clauses;
33952 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33953 cp_lexer_consume_token (parser->lexer);
33955 token = cp_lexer_peek_token (parser->lexer);
33956 c_kind = cp_parser_omp_clause_name (parser);
33958 switch (c_kind)
33960 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33961 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33962 token->location);
33963 c_name = "collapse";
33964 break;
33965 case PRAGMA_OMP_CLAUSE_COPYIN:
33966 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33967 c_name = "copyin";
33968 break;
33969 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33970 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33971 clauses);
33972 c_name = "copyprivate";
33973 break;
33974 case PRAGMA_OMP_CLAUSE_DEFAULT:
33975 clauses = cp_parser_omp_clause_default (parser, clauses,
33976 token->location, false);
33977 c_name = "default";
33978 break;
33979 case PRAGMA_OMP_CLAUSE_FINAL:
33980 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33981 c_name = "final";
33982 break;
33983 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33984 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33985 clauses);
33986 c_name = "firstprivate";
33987 break;
33988 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33989 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33990 token->location);
33991 c_name = "grainsize";
33992 break;
33993 case PRAGMA_OMP_CLAUSE_HINT:
33994 clauses = cp_parser_omp_clause_hint (parser, clauses,
33995 token->location);
33996 c_name = "hint";
33997 break;
33998 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33999 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34000 token->location);
34001 c_name = "defaultmap";
34002 break;
34003 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34004 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34005 clauses);
34006 c_name = "use_device_ptr";
34007 break;
34008 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34009 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34010 clauses);
34011 c_name = "is_device_ptr";
34012 break;
34013 case PRAGMA_OMP_CLAUSE_IF:
34014 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34015 true);
34016 c_name = "if";
34017 break;
34018 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34019 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34020 clauses);
34021 c_name = "lastprivate";
34022 break;
34023 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34024 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34025 token->location);
34026 c_name = "mergeable";
34027 break;
34028 case PRAGMA_OMP_CLAUSE_NOWAIT:
34029 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34030 c_name = "nowait";
34031 break;
34032 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34033 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34034 token->location);
34035 c_name = "num_tasks";
34036 break;
34037 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34038 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34039 token->location);
34040 c_name = "num_threads";
34041 break;
34042 case PRAGMA_OMP_CLAUSE_ORDERED:
34043 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34044 token->location);
34045 c_name = "ordered";
34046 break;
34047 case PRAGMA_OMP_CLAUSE_PRIORITY:
34048 clauses = cp_parser_omp_clause_priority (parser, clauses,
34049 token->location);
34050 c_name = "priority";
34051 break;
34052 case PRAGMA_OMP_CLAUSE_PRIVATE:
34053 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34054 clauses);
34055 c_name = "private";
34056 break;
34057 case PRAGMA_OMP_CLAUSE_REDUCTION:
34058 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34059 c_name = "reduction";
34060 break;
34061 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34062 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34063 token->location);
34064 c_name = "schedule";
34065 break;
34066 case PRAGMA_OMP_CLAUSE_SHARED:
34067 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34068 clauses);
34069 c_name = "shared";
34070 break;
34071 case PRAGMA_OMP_CLAUSE_UNTIED:
34072 clauses = cp_parser_omp_clause_untied (parser, clauses,
34073 token->location);
34074 c_name = "untied";
34075 break;
34076 case PRAGMA_OMP_CLAUSE_INBRANCH:
34077 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34078 clauses, token->location);
34079 c_name = "inbranch";
34080 break;
34081 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34082 clauses = cp_parser_omp_clause_branch (parser,
34083 OMP_CLAUSE_NOTINBRANCH,
34084 clauses, token->location);
34085 c_name = "notinbranch";
34086 break;
34087 case PRAGMA_OMP_CLAUSE_PARALLEL:
34088 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34089 clauses, token->location);
34090 c_name = "parallel";
34091 if (!first)
34093 clause_not_first:
34094 error_at (token->location, "%qs must be the first clause of %qs",
34095 c_name, where);
34096 clauses = prev;
34098 break;
34099 case PRAGMA_OMP_CLAUSE_FOR:
34100 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34101 clauses, token->location);
34102 c_name = "for";
34103 if (!first)
34104 goto clause_not_first;
34105 break;
34106 case PRAGMA_OMP_CLAUSE_SECTIONS:
34107 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34108 clauses, token->location);
34109 c_name = "sections";
34110 if (!first)
34111 goto clause_not_first;
34112 break;
34113 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34114 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34115 clauses, token->location);
34116 c_name = "taskgroup";
34117 if (!first)
34118 goto clause_not_first;
34119 break;
34120 case PRAGMA_OMP_CLAUSE_LINK:
34121 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34122 c_name = "to";
34123 break;
34124 case PRAGMA_OMP_CLAUSE_TO:
34125 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34126 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34127 clauses);
34128 else
34129 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34130 c_name = "to";
34131 break;
34132 case PRAGMA_OMP_CLAUSE_FROM:
34133 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34134 c_name = "from";
34135 break;
34136 case PRAGMA_OMP_CLAUSE_UNIFORM:
34137 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34138 clauses);
34139 c_name = "uniform";
34140 break;
34141 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34142 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34143 token->location);
34144 c_name = "num_teams";
34145 break;
34146 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34147 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34148 token->location);
34149 c_name = "thread_limit";
34150 break;
34151 case PRAGMA_OMP_CLAUSE_ALIGNED:
34152 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34153 c_name = "aligned";
34154 break;
34155 case PRAGMA_OMP_CLAUSE_LINEAR:
34157 bool declare_simd = false;
34158 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34159 declare_simd = true;
34160 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34162 c_name = "linear";
34163 break;
34164 case PRAGMA_OMP_CLAUSE_DEPEND:
34165 clauses = cp_parser_omp_clause_depend (parser, clauses,
34166 token->location);
34167 c_name = "depend";
34168 break;
34169 case PRAGMA_OMP_CLAUSE_MAP:
34170 clauses = cp_parser_omp_clause_map (parser, clauses);
34171 c_name = "map";
34172 break;
34173 case PRAGMA_OMP_CLAUSE_DEVICE:
34174 clauses = cp_parser_omp_clause_device (parser, clauses,
34175 token->location);
34176 c_name = "device";
34177 break;
34178 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34179 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34180 token->location);
34181 c_name = "dist_schedule";
34182 break;
34183 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34184 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34185 token->location);
34186 c_name = "proc_bind";
34187 break;
34188 case PRAGMA_OMP_CLAUSE_SAFELEN:
34189 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34190 token->location);
34191 c_name = "safelen";
34192 break;
34193 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34194 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34195 token->location);
34196 c_name = "simdlen";
34197 break;
34198 case PRAGMA_OMP_CLAUSE_NOGROUP:
34199 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34200 token->location);
34201 c_name = "nogroup";
34202 break;
34203 case PRAGMA_OMP_CLAUSE_THREADS:
34204 clauses
34205 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34206 clauses, token->location);
34207 c_name = "threads";
34208 break;
34209 case PRAGMA_OMP_CLAUSE_SIMD:
34210 clauses
34211 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34212 clauses, token->location);
34213 c_name = "simd";
34214 break;
34215 default:
34216 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34217 goto saw_error;
34220 first = false;
34222 if (((mask >> c_kind) & 1) == 0)
34224 /* Remove the invalid clause(s) from the list to avoid
34225 confusing the rest of the compiler. */
34226 clauses = prev;
34227 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34230 saw_error:
34231 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34232 if (finish_p)
34234 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34235 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34236 else
34237 return finish_omp_clauses (clauses, C_ORT_OMP);
34239 return clauses;
34242 /* OpenMP 2.5:
34243 structured-block:
34244 statement
34246 In practice, we're also interested in adding the statement to an
34247 outer node. So it is convenient if we work around the fact that
34248 cp_parser_statement calls add_stmt. */
34250 static unsigned
34251 cp_parser_begin_omp_structured_block (cp_parser *parser)
34253 unsigned save = parser->in_statement;
34255 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34256 This preserves the "not within loop or switch" style error messages
34257 for nonsense cases like
34258 void foo() {
34259 #pragma omp single
34260 break;
34263 if (parser->in_statement)
34264 parser->in_statement = IN_OMP_BLOCK;
34266 return save;
34269 static void
34270 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34272 parser->in_statement = save;
34275 static tree
34276 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34278 tree stmt = begin_omp_structured_block ();
34279 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34281 cp_parser_statement (parser, NULL_TREE, false, if_p);
34283 cp_parser_end_omp_structured_block (parser, save);
34284 return finish_omp_structured_block (stmt);
34287 /* OpenMP 2.5:
34288 # pragma omp atomic new-line
34289 expression-stmt
34291 expression-stmt:
34292 x binop= expr | x++ | ++x | x-- | --x
34293 binop:
34294 +, *, -, /, &, ^, |, <<, >>
34296 where x is an lvalue expression with scalar type.
34298 OpenMP 3.1:
34299 # pragma omp atomic new-line
34300 update-stmt
34302 # pragma omp atomic read new-line
34303 read-stmt
34305 # pragma omp atomic write new-line
34306 write-stmt
34308 # pragma omp atomic update new-line
34309 update-stmt
34311 # pragma omp atomic capture new-line
34312 capture-stmt
34314 # pragma omp atomic capture new-line
34315 capture-block
34317 read-stmt:
34318 v = x
34319 write-stmt:
34320 x = expr
34321 update-stmt:
34322 expression-stmt | x = x binop expr
34323 capture-stmt:
34324 v = expression-stmt
34325 capture-block:
34326 { v = x; update-stmt; } | { update-stmt; v = x; }
34328 OpenMP 4.0:
34329 update-stmt:
34330 expression-stmt | x = x binop expr | x = expr binop x
34331 capture-stmt:
34332 v = update-stmt
34333 capture-block:
34334 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34336 where x and v are lvalue expressions with scalar type. */
34338 static void
34339 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34341 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34342 tree rhs1 = NULL_TREE, orig_lhs;
34343 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34344 bool structured_block = false;
34345 bool seq_cst = false;
34347 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34349 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34350 const char *p = IDENTIFIER_POINTER (id);
34352 if (!strcmp (p, "seq_cst"))
34354 seq_cst = true;
34355 cp_lexer_consume_token (parser->lexer);
34356 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34357 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34358 cp_lexer_consume_token (parser->lexer);
34361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34363 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34364 const char *p = IDENTIFIER_POINTER (id);
34366 if (!strcmp (p, "read"))
34367 code = OMP_ATOMIC_READ;
34368 else if (!strcmp (p, "write"))
34369 code = NOP_EXPR;
34370 else if (!strcmp (p, "update"))
34371 code = OMP_ATOMIC;
34372 else if (!strcmp (p, "capture"))
34373 code = OMP_ATOMIC_CAPTURE_NEW;
34374 else
34375 p = NULL;
34376 if (p)
34377 cp_lexer_consume_token (parser->lexer);
34379 if (!seq_cst)
34381 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34382 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34383 cp_lexer_consume_token (parser->lexer);
34385 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34387 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34388 const char *p = IDENTIFIER_POINTER (id);
34390 if (!strcmp (p, "seq_cst"))
34392 seq_cst = true;
34393 cp_lexer_consume_token (parser->lexer);
34397 cp_parser_require_pragma_eol (parser, pragma_tok);
34399 switch (code)
34401 case OMP_ATOMIC_READ:
34402 case NOP_EXPR: /* atomic write */
34403 v = cp_parser_unary_expression (parser);
34404 if (v == error_mark_node)
34405 goto saw_error;
34406 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34407 goto saw_error;
34408 if (code == NOP_EXPR)
34409 lhs = cp_parser_expression (parser);
34410 else
34411 lhs = cp_parser_unary_expression (parser);
34412 if (lhs == error_mark_node)
34413 goto saw_error;
34414 if (code == NOP_EXPR)
34416 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34417 opcode. */
34418 code = OMP_ATOMIC;
34419 rhs = lhs;
34420 lhs = v;
34421 v = NULL_TREE;
34423 goto done;
34424 case OMP_ATOMIC_CAPTURE_NEW:
34425 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34427 cp_lexer_consume_token (parser->lexer);
34428 structured_block = true;
34430 else
34432 v = cp_parser_unary_expression (parser);
34433 if (v == error_mark_node)
34434 goto saw_error;
34435 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34436 goto saw_error;
34438 default:
34439 break;
34442 restart:
34443 lhs = cp_parser_unary_expression (parser);
34444 orig_lhs = lhs;
34445 switch (TREE_CODE (lhs))
34447 case ERROR_MARK:
34448 goto saw_error;
34450 case POSTINCREMENT_EXPR:
34451 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34452 code = OMP_ATOMIC_CAPTURE_OLD;
34453 /* FALLTHROUGH */
34454 case PREINCREMENT_EXPR:
34455 lhs = TREE_OPERAND (lhs, 0);
34456 opcode = PLUS_EXPR;
34457 rhs = integer_one_node;
34458 break;
34460 case POSTDECREMENT_EXPR:
34461 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34462 code = OMP_ATOMIC_CAPTURE_OLD;
34463 /* FALLTHROUGH */
34464 case PREDECREMENT_EXPR:
34465 lhs = TREE_OPERAND (lhs, 0);
34466 opcode = MINUS_EXPR;
34467 rhs = integer_one_node;
34468 break;
34470 case COMPOUND_EXPR:
34471 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34472 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34473 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34474 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34475 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34476 (TREE_OPERAND (lhs, 1), 0), 0)))
34477 == BOOLEAN_TYPE)
34478 /* Undo effects of boolean_increment for post {in,de}crement. */
34479 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34480 /* FALLTHRU */
34481 case MODIFY_EXPR:
34482 if (TREE_CODE (lhs) == MODIFY_EXPR
34483 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34485 /* Undo effects of boolean_increment. */
34486 if (integer_onep (TREE_OPERAND (lhs, 1)))
34488 /* This is pre or post increment. */
34489 rhs = TREE_OPERAND (lhs, 1);
34490 lhs = TREE_OPERAND (lhs, 0);
34491 opcode = NOP_EXPR;
34492 if (code == OMP_ATOMIC_CAPTURE_NEW
34493 && !structured_block
34494 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34495 code = OMP_ATOMIC_CAPTURE_OLD;
34496 break;
34499 /* FALLTHRU */
34500 default:
34501 switch (cp_lexer_peek_token (parser->lexer)->type)
34503 case CPP_MULT_EQ:
34504 opcode = MULT_EXPR;
34505 break;
34506 case CPP_DIV_EQ:
34507 opcode = TRUNC_DIV_EXPR;
34508 break;
34509 case CPP_PLUS_EQ:
34510 opcode = PLUS_EXPR;
34511 break;
34512 case CPP_MINUS_EQ:
34513 opcode = MINUS_EXPR;
34514 break;
34515 case CPP_LSHIFT_EQ:
34516 opcode = LSHIFT_EXPR;
34517 break;
34518 case CPP_RSHIFT_EQ:
34519 opcode = RSHIFT_EXPR;
34520 break;
34521 case CPP_AND_EQ:
34522 opcode = BIT_AND_EXPR;
34523 break;
34524 case CPP_OR_EQ:
34525 opcode = BIT_IOR_EXPR;
34526 break;
34527 case CPP_XOR_EQ:
34528 opcode = BIT_XOR_EXPR;
34529 break;
34530 case CPP_EQ:
34531 enum cp_parser_prec oprec;
34532 cp_token *token;
34533 cp_lexer_consume_token (parser->lexer);
34534 cp_parser_parse_tentatively (parser);
34535 rhs1 = cp_parser_simple_cast_expression (parser);
34536 if (rhs1 == error_mark_node)
34538 cp_parser_abort_tentative_parse (parser);
34539 cp_parser_simple_cast_expression (parser);
34540 goto saw_error;
34542 token = cp_lexer_peek_token (parser->lexer);
34543 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34545 cp_parser_abort_tentative_parse (parser);
34546 cp_parser_parse_tentatively (parser);
34547 rhs = cp_parser_binary_expression (parser, false, true,
34548 PREC_NOT_OPERATOR, NULL);
34549 if (rhs == error_mark_node)
34551 cp_parser_abort_tentative_parse (parser);
34552 cp_parser_binary_expression (parser, false, true,
34553 PREC_NOT_OPERATOR, NULL);
34554 goto saw_error;
34556 switch (TREE_CODE (rhs))
34558 case MULT_EXPR:
34559 case TRUNC_DIV_EXPR:
34560 case RDIV_EXPR:
34561 case PLUS_EXPR:
34562 case MINUS_EXPR:
34563 case LSHIFT_EXPR:
34564 case RSHIFT_EXPR:
34565 case BIT_AND_EXPR:
34566 case BIT_IOR_EXPR:
34567 case BIT_XOR_EXPR:
34568 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34570 if (cp_parser_parse_definitely (parser))
34572 opcode = TREE_CODE (rhs);
34573 rhs1 = TREE_OPERAND (rhs, 0);
34574 rhs = TREE_OPERAND (rhs, 1);
34575 goto stmt_done;
34577 else
34578 goto saw_error;
34580 break;
34581 default:
34582 break;
34584 cp_parser_abort_tentative_parse (parser);
34585 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34587 rhs = cp_parser_expression (parser);
34588 if (rhs == error_mark_node)
34589 goto saw_error;
34590 opcode = NOP_EXPR;
34591 rhs1 = NULL_TREE;
34592 goto stmt_done;
34594 cp_parser_error (parser,
34595 "invalid form of %<#pragma omp atomic%>");
34596 goto saw_error;
34598 if (!cp_parser_parse_definitely (parser))
34599 goto saw_error;
34600 switch (token->type)
34602 case CPP_SEMICOLON:
34603 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34605 code = OMP_ATOMIC_CAPTURE_OLD;
34606 v = lhs;
34607 lhs = NULL_TREE;
34608 lhs1 = rhs1;
34609 rhs1 = NULL_TREE;
34610 cp_lexer_consume_token (parser->lexer);
34611 goto restart;
34613 else if (structured_block)
34615 opcode = NOP_EXPR;
34616 rhs = rhs1;
34617 rhs1 = NULL_TREE;
34618 goto stmt_done;
34620 cp_parser_error (parser,
34621 "invalid form of %<#pragma omp atomic%>");
34622 goto saw_error;
34623 case CPP_MULT:
34624 opcode = MULT_EXPR;
34625 break;
34626 case CPP_DIV:
34627 opcode = TRUNC_DIV_EXPR;
34628 break;
34629 case CPP_PLUS:
34630 opcode = PLUS_EXPR;
34631 break;
34632 case CPP_MINUS:
34633 opcode = MINUS_EXPR;
34634 break;
34635 case CPP_LSHIFT:
34636 opcode = LSHIFT_EXPR;
34637 break;
34638 case CPP_RSHIFT:
34639 opcode = RSHIFT_EXPR;
34640 break;
34641 case CPP_AND:
34642 opcode = BIT_AND_EXPR;
34643 break;
34644 case CPP_OR:
34645 opcode = BIT_IOR_EXPR;
34646 break;
34647 case CPP_XOR:
34648 opcode = BIT_XOR_EXPR;
34649 break;
34650 default:
34651 cp_parser_error (parser,
34652 "invalid operator for %<#pragma omp atomic%>");
34653 goto saw_error;
34655 oprec = TOKEN_PRECEDENCE (token);
34656 gcc_assert (oprec != PREC_NOT_OPERATOR);
34657 if (commutative_tree_code (opcode))
34658 oprec = (enum cp_parser_prec) (oprec - 1);
34659 cp_lexer_consume_token (parser->lexer);
34660 rhs = cp_parser_binary_expression (parser, false, false,
34661 oprec, NULL);
34662 if (rhs == error_mark_node)
34663 goto saw_error;
34664 goto stmt_done;
34665 /* FALLTHROUGH */
34666 default:
34667 cp_parser_error (parser,
34668 "invalid operator for %<#pragma omp atomic%>");
34669 goto saw_error;
34671 cp_lexer_consume_token (parser->lexer);
34673 rhs = cp_parser_expression (parser);
34674 if (rhs == error_mark_node)
34675 goto saw_error;
34676 break;
34678 stmt_done:
34679 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34681 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34682 goto saw_error;
34683 v = cp_parser_unary_expression (parser);
34684 if (v == error_mark_node)
34685 goto saw_error;
34686 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34687 goto saw_error;
34688 lhs1 = cp_parser_unary_expression (parser);
34689 if (lhs1 == error_mark_node)
34690 goto saw_error;
34692 if (structured_block)
34694 cp_parser_consume_semicolon_at_end_of_statement (parser);
34695 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34697 done:
34698 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34699 if (!structured_block)
34700 cp_parser_consume_semicolon_at_end_of_statement (parser);
34701 return;
34703 saw_error:
34704 cp_parser_skip_to_end_of_block_or_statement (parser);
34705 if (structured_block)
34707 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34708 cp_lexer_consume_token (parser->lexer);
34709 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34711 cp_parser_skip_to_end_of_block_or_statement (parser);
34712 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34713 cp_lexer_consume_token (parser->lexer);
34719 /* OpenMP 2.5:
34720 # pragma omp barrier new-line */
34722 static void
34723 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34725 cp_parser_require_pragma_eol (parser, pragma_tok);
34726 finish_omp_barrier ();
34729 /* OpenMP 2.5:
34730 # pragma omp critical [(name)] new-line
34731 structured-block
34733 OpenMP 4.5:
34734 # pragma omp critical [(name) [hint(expression)]] new-line
34735 structured-block */
34737 #define OMP_CRITICAL_CLAUSE_MASK \
34738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34740 static tree
34741 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34743 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34747 matching_parens parens;
34748 parens.consume_open (parser);
34750 name = cp_parser_identifier (parser);
34752 if (name == error_mark_node
34753 || !parens.require_close (parser))
34754 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34755 /*or_comma=*/false,
34756 /*consume_paren=*/true);
34757 if (name == error_mark_node)
34758 name = NULL;
34760 clauses = cp_parser_omp_all_clauses (parser,
34761 OMP_CRITICAL_CLAUSE_MASK,
34762 "#pragma omp critical", pragma_tok);
34764 else
34765 cp_parser_require_pragma_eol (parser, pragma_tok);
34767 stmt = cp_parser_omp_structured_block (parser, if_p);
34768 return c_finish_omp_critical (input_location, stmt, name, clauses);
34771 /* OpenMP 2.5:
34772 # pragma omp flush flush-vars[opt] new-line
34774 flush-vars:
34775 ( variable-list ) */
34777 static void
34778 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34781 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34782 cp_parser_require_pragma_eol (parser, pragma_tok);
34784 finish_omp_flush ();
34787 /* Helper function, to parse omp for increment expression. */
34789 static tree
34790 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34792 tree cond = cp_parser_binary_expression (parser, false, true,
34793 PREC_NOT_OPERATOR, NULL);
34794 if (cond == error_mark_node
34795 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34797 cp_parser_skip_to_end_of_statement (parser);
34798 return error_mark_node;
34801 switch (TREE_CODE (cond))
34803 case GT_EXPR:
34804 case GE_EXPR:
34805 case LT_EXPR:
34806 case LE_EXPR:
34807 break;
34808 case NE_EXPR:
34809 /* Fall through: OpenMP disallows NE_EXPR. */
34810 gcc_fallthrough ();
34811 default:
34812 return error_mark_node;
34815 /* If decl is an iterator, preserve LHS and RHS of the relational
34816 expr until finish_omp_for. */
34817 if (decl
34818 && (type_dependent_expression_p (decl)
34819 || CLASS_TYPE_P (TREE_TYPE (decl))))
34820 return cond;
34822 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
34823 TREE_CODE (cond),
34824 TREE_OPERAND (cond, 0), ERROR_MARK,
34825 TREE_OPERAND (cond, 1), ERROR_MARK,
34826 /*overload=*/NULL, tf_warning_or_error);
34829 /* Helper function, to parse omp for increment expression. */
34831 static tree
34832 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34834 cp_token *token = cp_lexer_peek_token (parser->lexer);
34835 enum tree_code op;
34836 tree lhs, rhs;
34837 cp_id_kind idk;
34838 bool decl_first;
34840 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34842 op = (token->type == CPP_PLUS_PLUS
34843 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34844 cp_lexer_consume_token (parser->lexer);
34845 lhs = cp_parser_simple_cast_expression (parser);
34846 if (lhs != decl
34847 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34848 return error_mark_node;
34849 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34852 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34853 if (lhs != decl
34854 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34855 return error_mark_node;
34857 token = cp_lexer_peek_token (parser->lexer);
34858 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34860 op = (token->type == CPP_PLUS_PLUS
34861 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34862 cp_lexer_consume_token (parser->lexer);
34863 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34866 op = cp_parser_assignment_operator_opt (parser);
34867 if (op == ERROR_MARK)
34868 return error_mark_node;
34870 if (op != NOP_EXPR)
34872 rhs = cp_parser_assignment_expression (parser);
34873 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34874 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34877 lhs = cp_parser_binary_expression (parser, false, false,
34878 PREC_ADDITIVE_EXPRESSION, NULL);
34879 token = cp_lexer_peek_token (parser->lexer);
34880 decl_first = (lhs == decl
34881 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34882 if (decl_first)
34883 lhs = NULL_TREE;
34884 if (token->type != CPP_PLUS
34885 && token->type != CPP_MINUS)
34886 return error_mark_node;
34890 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34891 cp_lexer_consume_token (parser->lexer);
34892 rhs = cp_parser_binary_expression (parser, false, false,
34893 PREC_ADDITIVE_EXPRESSION, NULL);
34894 token = cp_lexer_peek_token (parser->lexer);
34895 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34897 if (lhs == NULL_TREE)
34899 if (op == PLUS_EXPR)
34900 lhs = rhs;
34901 else
34902 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34903 tf_warning_or_error);
34905 else
34906 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34907 ERROR_MARK, NULL, tf_warning_or_error);
34910 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34912 if (!decl_first)
34914 if ((rhs != decl
34915 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34916 || op == MINUS_EXPR)
34917 return error_mark_node;
34918 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34920 else
34921 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34923 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34926 /* Parse the initialization statement of an OpenMP for loop.
34928 Return true if the resulting construct should have an
34929 OMP_CLAUSE_PRIVATE added to it. */
34931 static tree
34932 cp_parser_omp_for_loop_init (cp_parser *parser,
34933 tree &this_pre_body,
34934 vec<tree, va_gc> *&for_block,
34935 tree &init,
34936 tree &orig_init,
34937 tree &decl,
34938 tree &real_decl)
34940 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34941 return NULL_TREE;
34943 tree add_private_clause = NULL_TREE;
34945 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34947 init-expr:
34948 var = lb
34949 integer-type var = lb
34950 random-access-iterator-type var = lb
34951 pointer-type var = lb
34953 cp_decl_specifier_seq type_specifiers;
34955 /* First, try to parse as an initialized declaration. See
34956 cp_parser_condition, from whence the bulk of this is copied. */
34958 cp_parser_parse_tentatively (parser);
34959 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34960 /*is_trailing_return=*/false,
34961 &type_specifiers);
34962 if (cp_parser_parse_definitely (parser))
34964 /* If parsing a type specifier seq succeeded, then this
34965 MUST be a initialized declaration. */
34966 tree asm_specification, attributes;
34967 cp_declarator *declarator;
34969 declarator = cp_parser_declarator (parser,
34970 CP_PARSER_DECLARATOR_NAMED,
34971 /*ctor_dtor_or_conv_p=*/NULL,
34972 /*parenthesized_p=*/NULL,
34973 /*member_p=*/false,
34974 /*friend_p=*/false);
34975 attributes = cp_parser_attributes_opt (parser);
34976 asm_specification = cp_parser_asm_specification_opt (parser);
34978 if (declarator == cp_error_declarator)
34979 cp_parser_skip_to_end_of_statement (parser);
34981 else
34983 tree pushed_scope, auto_node;
34985 decl = start_decl (declarator, &type_specifiers,
34986 SD_INITIALIZED, attributes,
34987 /*prefix_attributes=*/NULL_TREE,
34988 &pushed_scope);
34990 auto_node = type_uses_auto (TREE_TYPE (decl));
34991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34993 if (cp_lexer_next_token_is (parser->lexer,
34994 CPP_OPEN_PAREN))
34995 error ("parenthesized initialization is not allowed in "
34996 "OpenMP %<for%> loop");
34997 else
34998 /* Trigger an error. */
34999 cp_parser_require (parser, CPP_EQ, RT_EQ);
35001 init = error_mark_node;
35002 cp_parser_skip_to_end_of_statement (parser);
35004 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35005 || type_dependent_expression_p (decl)
35006 || auto_node)
35008 bool is_direct_init, is_non_constant_init;
35010 init = cp_parser_initializer (parser,
35011 &is_direct_init,
35012 &is_non_constant_init);
35014 if (auto_node)
35016 TREE_TYPE (decl)
35017 = do_auto_deduction (TREE_TYPE (decl), init,
35018 auto_node);
35020 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35021 && !type_dependent_expression_p (decl))
35022 goto non_class;
35025 cp_finish_decl (decl, init, !is_non_constant_init,
35026 asm_specification,
35027 LOOKUP_ONLYCONVERTING);
35028 orig_init = init;
35029 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35031 vec_safe_push (for_block, this_pre_body);
35032 init = NULL_TREE;
35034 else
35036 init = pop_stmt_list (this_pre_body);
35037 if (init && TREE_CODE (init) == STATEMENT_LIST)
35039 tree_stmt_iterator i = tsi_start (init);
35040 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35041 while (!tsi_end_p (i))
35043 tree t = tsi_stmt (i);
35044 if (TREE_CODE (t) == DECL_EXPR
35045 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35047 tsi_delink (&i);
35048 vec_safe_push (for_block, t);
35049 continue;
35051 break;
35053 if (tsi_one_before_end_p (i))
35055 tree t = tsi_stmt (i);
35056 tsi_delink (&i);
35057 free_stmt_list (init);
35058 init = t;
35062 this_pre_body = NULL_TREE;
35064 else
35066 /* Consume '='. */
35067 cp_lexer_consume_token (parser->lexer);
35068 init = cp_parser_assignment_expression (parser);
35070 non_class:
35071 if (TYPE_REF_P (TREE_TYPE (decl)))
35072 init = error_mark_node;
35073 else
35074 cp_finish_decl (decl, NULL_TREE,
35075 /*init_const_expr_p=*/false,
35076 asm_specification,
35077 LOOKUP_ONLYCONVERTING);
35080 if (pushed_scope)
35081 pop_scope (pushed_scope);
35084 else
35086 cp_id_kind idk;
35087 /* If parsing a type specifier sequence failed, then
35088 this MUST be a simple expression. */
35089 cp_parser_parse_tentatively (parser);
35090 decl = cp_parser_primary_expression (parser, false, false,
35091 false, &idk);
35092 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35093 if (!cp_parser_error_occurred (parser)
35094 && decl
35095 && (TREE_CODE (decl) == COMPONENT_REF
35096 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35098 cp_parser_abort_tentative_parse (parser);
35099 cp_parser_parse_tentatively (parser);
35100 cp_token *token = cp_lexer_peek_token (parser->lexer);
35101 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35102 /*check_dependency_p=*/true,
35103 /*template_p=*/NULL,
35104 /*declarator_p=*/false,
35105 /*optional_p=*/false);
35106 if (name != error_mark_node
35107 && last_tok == cp_lexer_peek_token (parser->lexer))
35109 decl = cp_parser_lookup_name_simple (parser, name,
35110 token->location);
35111 if (TREE_CODE (decl) == FIELD_DECL)
35112 add_private_clause = omp_privatize_field (decl, false);
35114 cp_parser_abort_tentative_parse (parser);
35115 cp_parser_parse_tentatively (parser);
35116 decl = cp_parser_primary_expression (parser, false, false,
35117 false, &idk);
35119 if (!cp_parser_error_occurred (parser)
35120 && decl
35121 && DECL_P (decl)
35122 && CLASS_TYPE_P (TREE_TYPE (decl)))
35124 tree rhs;
35126 cp_parser_parse_definitely (parser);
35127 cp_parser_require (parser, CPP_EQ, RT_EQ);
35128 rhs = cp_parser_assignment_expression (parser);
35129 orig_init = rhs;
35130 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35131 decl, NOP_EXPR,
35132 rhs,
35133 tf_warning_or_error));
35134 if (!add_private_clause)
35135 add_private_clause = decl;
35137 else
35139 decl = NULL;
35140 cp_parser_abort_tentative_parse (parser);
35141 init = cp_parser_expression (parser);
35142 if (init)
35144 if (TREE_CODE (init) == MODIFY_EXPR
35145 || TREE_CODE (init) == MODOP_EXPR)
35146 real_decl = TREE_OPERAND (init, 0);
35150 return add_private_clause;
35153 /* Parse the restricted form of the for statement allowed by OpenMP. */
35155 static tree
35156 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35157 tree *cclauses, bool *if_p)
35159 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35160 tree real_decl, initv, condv, incrv, declv;
35161 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35162 location_t loc_first;
35163 bool collapse_err = false;
35164 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35165 vec<tree, va_gc> *for_block = make_tree_vector ();
35166 auto_vec<tree, 4> orig_inits;
35167 bool tiling = false;
35169 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35170 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35171 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35172 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35174 tiling = true;
35175 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35177 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35178 && OMP_CLAUSE_ORDERED_EXPR (cl))
35180 ordered_cl = cl;
35181 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35184 if (ordered && ordered < collapse)
35186 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35187 "%<ordered%> clause parameter is less than %<collapse%>");
35188 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35189 = build_int_cst (NULL_TREE, collapse);
35190 ordered = collapse;
35192 if (ordered)
35194 for (tree *pc = &clauses; *pc; )
35195 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35197 error_at (OMP_CLAUSE_LOCATION (*pc),
35198 "%<linear%> clause may not be specified together "
35199 "with %<ordered%> clause with a parameter");
35200 *pc = OMP_CLAUSE_CHAIN (*pc);
35202 else
35203 pc = &OMP_CLAUSE_CHAIN (*pc);
35206 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35207 count = ordered ? ordered : collapse;
35209 declv = make_tree_vec (count);
35210 initv = make_tree_vec (count);
35211 condv = make_tree_vec (count);
35212 incrv = make_tree_vec (count);
35214 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35216 for (i = 0; i < count; i++)
35218 int bracecount = 0;
35219 tree add_private_clause = NULL_TREE;
35220 location_t loc;
35222 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35224 if (!collapse_err)
35225 cp_parser_error (parser, "for statement expected");
35226 return NULL;
35228 loc = cp_lexer_consume_token (parser->lexer)->location;
35230 matching_parens parens;
35231 if (!parens.require_open (parser))
35232 return NULL;
35234 init = orig_init = decl = real_decl = NULL;
35235 this_pre_body = push_stmt_list ();
35237 add_private_clause
35238 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35239 init, orig_init, decl, real_decl);
35241 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35242 if (this_pre_body)
35244 this_pre_body = pop_stmt_list (this_pre_body);
35245 if (pre_body)
35247 tree t = pre_body;
35248 pre_body = push_stmt_list ();
35249 add_stmt (t);
35250 add_stmt (this_pre_body);
35251 pre_body = pop_stmt_list (pre_body);
35253 else
35254 pre_body = this_pre_body;
35257 if (decl)
35258 real_decl = decl;
35259 if (cclauses != NULL
35260 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35261 && real_decl != NULL_TREE)
35263 tree *c;
35264 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35265 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35266 && OMP_CLAUSE_DECL (*c) == real_decl)
35268 error_at (loc, "iteration variable %qD"
35269 " should not be firstprivate", real_decl);
35270 *c = OMP_CLAUSE_CHAIN (*c);
35272 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35273 && OMP_CLAUSE_DECL (*c) == real_decl)
35275 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35276 tree l = *c;
35277 *c = OMP_CLAUSE_CHAIN (*c);
35278 if (code == OMP_SIMD)
35280 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35281 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35283 else
35285 OMP_CLAUSE_CHAIN (l) = clauses;
35286 clauses = l;
35288 add_private_clause = NULL_TREE;
35290 else
35292 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35293 && OMP_CLAUSE_DECL (*c) == real_decl)
35294 add_private_clause = NULL_TREE;
35295 c = &OMP_CLAUSE_CHAIN (*c);
35299 if (add_private_clause)
35301 tree c;
35302 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35304 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35305 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35306 && OMP_CLAUSE_DECL (c) == decl)
35307 break;
35308 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35309 && OMP_CLAUSE_DECL (c) == decl)
35310 error_at (loc, "iteration variable %qD "
35311 "should not be firstprivate",
35312 decl);
35313 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35314 && OMP_CLAUSE_DECL (c) == decl)
35315 error_at (loc, "iteration variable %qD should not be reduction",
35316 decl);
35318 if (c == NULL)
35320 if (code != OMP_SIMD)
35321 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35322 else if (collapse == 1)
35323 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35324 else
35325 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35326 OMP_CLAUSE_DECL (c) = add_private_clause;
35327 c = finish_omp_clauses (c, C_ORT_OMP);
35328 if (c)
35330 OMP_CLAUSE_CHAIN (c) = clauses;
35331 clauses = c;
35332 /* For linear, signal that we need to fill up
35333 the so far unknown linear step. */
35334 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35335 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35340 cond = NULL;
35341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35342 cond = cp_parser_omp_for_cond (parser, decl);
35343 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35345 incr = NULL;
35346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35348 /* If decl is an iterator, preserve the operator on decl
35349 until finish_omp_for. */
35350 if (real_decl
35351 && ((processing_template_decl
35352 && (TREE_TYPE (real_decl) == NULL_TREE
35353 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35354 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35355 incr = cp_parser_omp_for_incr (parser, real_decl);
35356 else
35357 incr = cp_parser_expression (parser);
35358 if (!EXPR_HAS_LOCATION (incr))
35359 protected_set_expr_location (incr, input_location);
35362 if (!parens.require_close (parser))
35363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35364 /*or_comma=*/false,
35365 /*consume_paren=*/true);
35367 TREE_VEC_ELT (declv, i) = decl;
35368 TREE_VEC_ELT (initv, i) = init;
35369 TREE_VEC_ELT (condv, i) = cond;
35370 TREE_VEC_ELT (incrv, i) = incr;
35371 if (orig_init)
35373 orig_inits.safe_grow_cleared (i + 1);
35374 orig_inits[i] = orig_init;
35377 if (i == count - 1)
35378 break;
35380 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35381 in between the collapsed for loops to be still considered perfectly
35382 nested. Hopefully the final version clarifies this.
35383 For now handle (multiple) {'s and empty statements. */
35384 cp_parser_parse_tentatively (parser);
35385 for (;;)
35387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35388 break;
35389 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35391 cp_lexer_consume_token (parser->lexer);
35392 bracecount++;
35394 else if (bracecount
35395 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35396 cp_lexer_consume_token (parser->lexer);
35397 else
35399 loc = cp_lexer_peek_token (parser->lexer)->location;
35400 error_at (loc, "not enough for loops to collapse");
35401 collapse_err = true;
35402 cp_parser_abort_tentative_parse (parser);
35403 declv = NULL_TREE;
35404 break;
35408 if (declv)
35410 cp_parser_parse_definitely (parser);
35411 nbraces += bracecount;
35415 if (nbraces)
35416 if_p = NULL;
35418 /* Note that we saved the original contents of this flag when we entered
35419 the structured block, and so we don't need to re-save it here. */
35420 parser->in_statement = IN_OMP_FOR;
35422 /* Note that the grammar doesn't call for a structured block here,
35423 though the loop as a whole is a structured block. */
35424 body = push_stmt_list ();
35425 cp_parser_statement (parser, NULL_TREE, false, if_p);
35426 body = pop_stmt_list (body);
35428 if (declv == NULL_TREE)
35429 ret = NULL_TREE;
35430 else
35431 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35432 body, pre_body, &orig_inits, clauses);
35434 while (nbraces)
35436 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35438 cp_lexer_consume_token (parser->lexer);
35439 nbraces--;
35441 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35442 cp_lexer_consume_token (parser->lexer);
35443 else
35445 if (!collapse_err)
35447 error_at (cp_lexer_peek_token (parser->lexer)->location,
35448 "collapsed loops not perfectly nested");
35450 collapse_err = true;
35451 cp_parser_statement_seq_opt (parser, NULL);
35452 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35453 break;
35457 while (!for_block->is_empty ())
35459 tree t = for_block->pop ();
35460 if (TREE_CODE (t) == STATEMENT_LIST)
35461 add_stmt (pop_stmt_list (t));
35462 else
35463 add_stmt (t);
35465 release_tree_vector (for_block);
35467 return ret;
35470 /* Helper function for OpenMP parsing, split clauses and call
35471 finish_omp_clauses on each of the set of clauses afterwards. */
35473 static void
35474 cp_omp_split_clauses (location_t loc, enum tree_code code,
35475 omp_clause_mask mask, tree clauses, tree *cclauses)
35477 int i;
35478 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35479 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35480 if (cclauses[i])
35481 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35484 /* OpenMP 4.0:
35485 #pragma omp simd simd-clause[optseq] new-line
35486 for-loop */
35488 #define OMP_SIMD_CLAUSE_MASK \
35489 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35498 static tree
35499 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35500 char *p_name, omp_clause_mask mask, tree *cclauses,
35501 bool *if_p)
35503 tree clauses, sb, ret;
35504 unsigned int save;
35505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35507 strcat (p_name, " simd");
35508 mask |= OMP_SIMD_CLAUSE_MASK;
35510 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35511 cclauses == NULL);
35512 if (cclauses)
35514 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35515 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35516 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35517 OMP_CLAUSE_ORDERED);
35518 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35520 error_at (OMP_CLAUSE_LOCATION (c),
35521 "%<ordered%> clause with parameter may not be specified "
35522 "on %qs construct", p_name);
35523 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35527 sb = begin_omp_structured_block ();
35528 save = cp_parser_begin_omp_structured_block (parser);
35530 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35532 cp_parser_end_omp_structured_block (parser, save);
35533 add_stmt (finish_omp_structured_block (sb));
35535 return ret;
35538 /* OpenMP 2.5:
35539 #pragma omp for for-clause[optseq] new-line
35540 for-loop
35542 OpenMP 4.0:
35543 #pragma omp for simd for-simd-clause[optseq] new-line
35544 for-loop */
35546 #define OMP_FOR_CLAUSE_MASK \
35547 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35557 static tree
35558 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35559 char *p_name, omp_clause_mask mask, tree *cclauses,
35560 bool *if_p)
35562 tree clauses, sb, ret;
35563 unsigned int save;
35564 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35566 strcat (p_name, " for");
35567 mask |= OMP_FOR_CLAUSE_MASK;
35568 /* parallel for{, simd} disallows nowait clause, but for
35569 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35570 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35571 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35572 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35573 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35574 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35576 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35578 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35579 const char *p = IDENTIFIER_POINTER (id);
35581 if (strcmp (p, "simd") == 0)
35583 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35584 if (cclauses == NULL)
35585 cclauses = cclauses_buf;
35587 cp_lexer_consume_token (parser->lexer);
35588 if (!flag_openmp) /* flag_openmp_simd */
35589 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35590 cclauses, if_p);
35591 sb = begin_omp_structured_block ();
35592 save = cp_parser_begin_omp_structured_block (parser);
35593 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35594 cclauses, if_p);
35595 cp_parser_end_omp_structured_block (parser, save);
35596 tree body = finish_omp_structured_block (sb);
35597 if (ret == NULL)
35598 return ret;
35599 ret = make_node (OMP_FOR);
35600 TREE_TYPE (ret) = void_type_node;
35601 OMP_FOR_BODY (ret) = body;
35602 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35603 SET_EXPR_LOCATION (ret, loc);
35604 add_stmt (ret);
35605 return ret;
35608 if (!flag_openmp) /* flag_openmp_simd */
35610 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35611 return NULL_TREE;
35614 /* Composite distribute parallel for disallows linear clause. */
35615 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35616 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35618 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35619 cclauses == NULL);
35620 if (cclauses)
35622 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35623 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35626 sb = begin_omp_structured_block ();
35627 save = cp_parser_begin_omp_structured_block (parser);
35629 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35631 cp_parser_end_omp_structured_block (parser, save);
35632 add_stmt (finish_omp_structured_block (sb));
35634 return ret;
35637 /* OpenMP 2.5:
35638 # pragma omp master new-line
35639 structured-block */
35641 static tree
35642 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35644 cp_parser_require_pragma_eol (parser, pragma_tok);
35645 return c_finish_omp_master (input_location,
35646 cp_parser_omp_structured_block (parser, if_p));
35649 /* OpenMP 2.5:
35650 # pragma omp ordered new-line
35651 structured-block
35653 OpenMP 4.5:
35654 # pragma omp ordered ordered-clauses new-line
35655 structured-block */
35657 #define OMP_ORDERED_CLAUSE_MASK \
35658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35661 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35662 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35664 static bool
35665 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35666 enum pragma_context context, bool *if_p)
35668 location_t loc = pragma_tok->location;
35670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35672 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35673 const char *p = IDENTIFIER_POINTER (id);
35675 if (strcmp (p, "depend") == 0)
35677 if (!flag_openmp) /* flag_openmp_simd */
35679 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35680 return false;
35682 if (context == pragma_stmt)
35684 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35685 "%<depend%> clause may only be used in compound "
35686 "statements");
35687 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35688 return false;
35690 tree clauses
35691 = cp_parser_omp_all_clauses (parser,
35692 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35693 "#pragma omp ordered", pragma_tok);
35694 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35695 return false;
35699 tree clauses
35700 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35701 "#pragma omp ordered", pragma_tok);
35703 if (!flag_openmp /* flag_openmp_simd */
35704 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35705 return false;
35707 c_finish_omp_ordered (loc, clauses,
35708 cp_parser_omp_structured_block (parser, if_p));
35709 return true;
35712 /* OpenMP 2.5:
35714 section-scope:
35715 { section-sequence }
35717 section-sequence:
35718 section-directive[opt] structured-block
35719 section-sequence section-directive structured-block */
35721 static tree
35722 cp_parser_omp_sections_scope (cp_parser *parser)
35724 tree stmt, substmt;
35725 bool error_suppress = false;
35726 cp_token *tok;
35728 matching_braces braces;
35729 if (!braces.require_open (parser))
35730 return NULL_TREE;
35732 stmt = push_stmt_list ();
35734 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35735 != PRAGMA_OMP_SECTION)
35737 substmt = cp_parser_omp_structured_block (parser, NULL);
35738 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35739 add_stmt (substmt);
35742 while (1)
35744 tok = cp_lexer_peek_token (parser->lexer);
35745 if (tok->type == CPP_CLOSE_BRACE)
35746 break;
35747 if (tok->type == CPP_EOF)
35748 break;
35750 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35752 cp_lexer_consume_token (parser->lexer);
35753 cp_parser_require_pragma_eol (parser, tok);
35754 error_suppress = false;
35756 else if (!error_suppress)
35758 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35759 error_suppress = true;
35762 substmt = cp_parser_omp_structured_block (parser, NULL);
35763 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35764 add_stmt (substmt);
35766 braces.require_close (parser);
35768 substmt = pop_stmt_list (stmt);
35770 stmt = make_node (OMP_SECTIONS);
35771 TREE_TYPE (stmt) = void_type_node;
35772 OMP_SECTIONS_BODY (stmt) = substmt;
35774 add_stmt (stmt);
35775 return stmt;
35778 /* OpenMP 2.5:
35779 # pragma omp sections sections-clause[optseq] newline
35780 sections-scope */
35782 #define OMP_SECTIONS_CLAUSE_MASK \
35783 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35789 static tree
35790 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35791 char *p_name, omp_clause_mask mask, tree *cclauses)
35793 tree clauses, ret;
35794 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35796 strcat (p_name, " sections");
35797 mask |= OMP_SECTIONS_CLAUSE_MASK;
35798 if (cclauses)
35799 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35801 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35802 cclauses == NULL);
35803 if (cclauses)
35805 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35806 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35809 ret = cp_parser_omp_sections_scope (parser);
35810 if (ret)
35811 OMP_SECTIONS_CLAUSES (ret) = clauses;
35813 return ret;
35816 /* OpenMP 2.5:
35817 # pragma omp parallel parallel-clause[optseq] new-line
35818 structured-block
35819 # pragma omp parallel for parallel-for-clause[optseq] new-line
35820 structured-block
35821 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35822 structured-block
35824 OpenMP 4.0:
35825 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35826 structured-block */
35828 #define OMP_PARALLEL_CLAUSE_MASK \
35829 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35839 static tree
35840 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35841 char *p_name, omp_clause_mask mask, tree *cclauses,
35842 bool *if_p)
35844 tree stmt, clauses, block;
35845 unsigned int save;
35846 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35848 strcat (p_name, " parallel");
35849 mask |= OMP_PARALLEL_CLAUSE_MASK;
35850 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35851 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35852 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35853 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35855 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35857 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35858 if (cclauses == NULL)
35859 cclauses = cclauses_buf;
35861 cp_lexer_consume_token (parser->lexer);
35862 if (!flag_openmp) /* flag_openmp_simd */
35863 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35864 if_p);
35865 block = begin_omp_parallel ();
35866 save = cp_parser_begin_omp_structured_block (parser);
35867 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35868 if_p);
35869 cp_parser_end_omp_structured_block (parser, save);
35870 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35871 block);
35872 if (ret == NULL_TREE)
35873 return ret;
35874 OMP_PARALLEL_COMBINED (stmt) = 1;
35875 return stmt;
35877 /* When combined with distribute, parallel has to be followed by for.
35878 #pragma omp target parallel is allowed though. */
35879 else if (cclauses
35880 && (mask & (OMP_CLAUSE_MASK_1
35881 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35883 error_at (loc, "expected %<for%> after %qs", p_name);
35884 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35885 return NULL_TREE;
35887 else if (!flag_openmp) /* flag_openmp_simd */
35889 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35890 return NULL_TREE;
35892 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35894 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35895 const char *p = IDENTIFIER_POINTER (id);
35896 if (strcmp (p, "sections") == 0)
35898 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35899 cclauses = cclauses_buf;
35901 cp_lexer_consume_token (parser->lexer);
35902 block = begin_omp_parallel ();
35903 save = cp_parser_begin_omp_structured_block (parser);
35904 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35905 cp_parser_end_omp_structured_block (parser, save);
35906 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35907 block);
35908 OMP_PARALLEL_COMBINED (stmt) = 1;
35909 return stmt;
35913 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35914 cclauses == NULL);
35915 if (cclauses)
35917 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35918 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35921 block = begin_omp_parallel ();
35922 save = cp_parser_begin_omp_structured_block (parser);
35923 cp_parser_statement (parser, NULL_TREE, false, if_p);
35924 cp_parser_end_omp_structured_block (parser, save);
35925 stmt = finish_omp_parallel (clauses, block);
35926 return stmt;
35929 /* OpenMP 2.5:
35930 # pragma omp single single-clause[optseq] new-line
35931 structured-block */
35933 #define OMP_SINGLE_CLAUSE_MASK \
35934 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35939 static tree
35940 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35942 tree stmt = make_node (OMP_SINGLE);
35943 TREE_TYPE (stmt) = void_type_node;
35945 OMP_SINGLE_CLAUSES (stmt)
35946 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35947 "#pragma omp single", pragma_tok);
35948 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35950 return add_stmt (stmt);
35953 /* OpenMP 3.0:
35954 # pragma omp task task-clause[optseq] new-line
35955 structured-block */
35957 #define OMP_TASK_CLAUSE_MASK \
35958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35969 static tree
35970 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35972 tree clauses, block;
35973 unsigned int save;
35975 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35976 "#pragma omp task", pragma_tok);
35977 block = begin_omp_task ();
35978 save = cp_parser_begin_omp_structured_block (parser);
35979 cp_parser_statement (parser, NULL_TREE, false, if_p);
35980 cp_parser_end_omp_structured_block (parser, save);
35981 return finish_omp_task (clauses, block);
35984 /* OpenMP 3.0:
35985 # pragma omp taskwait new-line */
35987 static void
35988 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35990 cp_parser_require_pragma_eol (parser, pragma_tok);
35991 finish_omp_taskwait ();
35994 /* OpenMP 3.1:
35995 # pragma omp taskyield new-line */
35997 static void
35998 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36000 cp_parser_require_pragma_eol (parser, pragma_tok);
36001 finish_omp_taskyield ();
36004 /* OpenMP 4.0:
36005 # pragma omp taskgroup new-line
36006 structured-block */
36008 static tree
36009 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36011 cp_parser_require_pragma_eol (parser, pragma_tok);
36012 return c_finish_omp_taskgroup (input_location,
36013 cp_parser_omp_structured_block (parser,
36014 if_p));
36018 /* OpenMP 2.5:
36019 # pragma omp threadprivate (variable-list) */
36021 static void
36022 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36024 tree vars;
36026 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36027 cp_parser_require_pragma_eol (parser, pragma_tok);
36029 finish_omp_threadprivate (vars);
36032 /* OpenMP 4.0:
36033 # pragma omp cancel cancel-clause[optseq] new-line */
36035 #define OMP_CANCEL_CLAUSE_MASK \
36036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36042 static void
36043 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36045 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36046 "#pragma omp cancel", pragma_tok);
36047 finish_omp_cancel (clauses);
36050 /* OpenMP 4.0:
36051 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36053 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36054 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36059 static void
36060 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36061 enum pragma_context context)
36063 tree clauses;
36064 bool point_seen = false;
36066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36068 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36069 const char *p = IDENTIFIER_POINTER (id);
36071 if (strcmp (p, "point") == 0)
36073 cp_lexer_consume_token (parser->lexer);
36074 point_seen = true;
36077 if (!point_seen)
36079 cp_parser_error (parser, "expected %<point%>");
36080 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36081 return;
36084 if (context != pragma_compound)
36086 if (context == pragma_stmt)
36087 error_at (pragma_tok->location,
36088 "%<#pragma %s%> may only be used in compound statements",
36089 "omp cancellation point");
36090 else
36091 cp_parser_error (parser, "expected declaration specifiers");
36092 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36093 return;
36096 clauses = cp_parser_omp_all_clauses (parser,
36097 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36098 "#pragma omp cancellation point",
36099 pragma_tok);
36100 finish_omp_cancellation_point (clauses);
36103 /* OpenMP 4.0:
36104 #pragma omp distribute distribute-clause[optseq] new-line
36105 for-loop */
36107 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36108 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36114 static tree
36115 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36116 char *p_name, omp_clause_mask mask, tree *cclauses,
36117 bool *if_p)
36119 tree clauses, sb, ret;
36120 unsigned int save;
36121 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36123 strcat (p_name, " distribute");
36124 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36126 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36129 const char *p = IDENTIFIER_POINTER (id);
36130 bool simd = false;
36131 bool parallel = false;
36133 if (strcmp (p, "simd") == 0)
36134 simd = true;
36135 else
36136 parallel = strcmp (p, "parallel") == 0;
36137 if (parallel || simd)
36139 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36140 if (cclauses == NULL)
36141 cclauses = cclauses_buf;
36142 cp_lexer_consume_token (parser->lexer);
36143 if (!flag_openmp) /* flag_openmp_simd */
36145 if (simd)
36146 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36147 cclauses, if_p);
36148 else
36149 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36150 cclauses, if_p);
36152 sb = begin_omp_structured_block ();
36153 save = cp_parser_begin_omp_structured_block (parser);
36154 if (simd)
36155 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36156 cclauses, if_p);
36157 else
36158 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36159 cclauses, if_p);
36160 cp_parser_end_omp_structured_block (parser, save);
36161 tree body = finish_omp_structured_block (sb);
36162 if (ret == NULL)
36163 return ret;
36164 ret = make_node (OMP_DISTRIBUTE);
36165 TREE_TYPE (ret) = void_type_node;
36166 OMP_FOR_BODY (ret) = body;
36167 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36168 SET_EXPR_LOCATION (ret, loc);
36169 add_stmt (ret);
36170 return ret;
36173 if (!flag_openmp) /* flag_openmp_simd */
36175 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36176 return NULL_TREE;
36179 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36180 cclauses == NULL);
36181 if (cclauses)
36183 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36184 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36187 sb = begin_omp_structured_block ();
36188 save = cp_parser_begin_omp_structured_block (parser);
36190 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36192 cp_parser_end_omp_structured_block (parser, save);
36193 add_stmt (finish_omp_structured_block (sb));
36195 return ret;
36198 /* OpenMP 4.0:
36199 # pragma omp teams teams-clause[optseq] new-line
36200 structured-block */
36202 #define OMP_TEAMS_CLAUSE_MASK \
36203 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36211 static tree
36212 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36213 char *p_name, omp_clause_mask mask, tree *cclauses,
36214 bool *if_p)
36216 tree clauses, sb, ret;
36217 unsigned int save;
36218 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36220 strcat (p_name, " teams");
36221 mask |= OMP_TEAMS_CLAUSE_MASK;
36223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36225 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36226 const char *p = IDENTIFIER_POINTER (id);
36227 if (strcmp (p, "distribute") == 0)
36229 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36230 if (cclauses == NULL)
36231 cclauses = cclauses_buf;
36233 cp_lexer_consume_token (parser->lexer);
36234 if (!flag_openmp) /* flag_openmp_simd */
36235 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36236 cclauses, if_p);
36237 sb = begin_omp_structured_block ();
36238 save = cp_parser_begin_omp_structured_block (parser);
36239 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36240 cclauses, if_p);
36241 cp_parser_end_omp_structured_block (parser, save);
36242 tree body = finish_omp_structured_block (sb);
36243 if (ret == NULL)
36244 return ret;
36245 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36246 ret = make_node (OMP_TEAMS);
36247 TREE_TYPE (ret) = void_type_node;
36248 OMP_TEAMS_CLAUSES (ret) = clauses;
36249 OMP_TEAMS_BODY (ret) = body;
36250 OMP_TEAMS_COMBINED (ret) = 1;
36251 SET_EXPR_LOCATION (ret, loc);
36252 return add_stmt (ret);
36255 if (!flag_openmp) /* flag_openmp_simd */
36257 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36258 return NULL_TREE;
36261 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36262 cclauses == NULL);
36263 if (cclauses)
36265 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36266 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36269 tree stmt = make_node (OMP_TEAMS);
36270 TREE_TYPE (stmt) = void_type_node;
36271 OMP_TEAMS_CLAUSES (stmt) = clauses;
36272 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36273 SET_EXPR_LOCATION (stmt, loc);
36275 return add_stmt (stmt);
36278 /* OpenMP 4.0:
36279 # pragma omp target data target-data-clause[optseq] new-line
36280 structured-block */
36282 #define OMP_TARGET_DATA_CLAUSE_MASK \
36283 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36288 static tree
36289 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36291 tree clauses
36292 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36293 "#pragma omp target data", pragma_tok);
36294 int map_seen = 0;
36295 for (tree *pc = &clauses; *pc;)
36297 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36298 switch (OMP_CLAUSE_MAP_KIND (*pc))
36300 case GOMP_MAP_TO:
36301 case GOMP_MAP_ALWAYS_TO:
36302 case GOMP_MAP_FROM:
36303 case GOMP_MAP_ALWAYS_FROM:
36304 case GOMP_MAP_TOFROM:
36305 case GOMP_MAP_ALWAYS_TOFROM:
36306 case GOMP_MAP_ALLOC:
36307 map_seen = 3;
36308 break;
36309 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36310 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36311 case GOMP_MAP_ALWAYS_POINTER:
36312 break;
36313 default:
36314 map_seen |= 1;
36315 error_at (OMP_CLAUSE_LOCATION (*pc),
36316 "%<#pragma omp target data%> with map-type other "
36317 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36318 "on %<map%> clause");
36319 *pc = OMP_CLAUSE_CHAIN (*pc);
36320 continue;
36322 pc = &OMP_CLAUSE_CHAIN (*pc);
36325 if (map_seen != 3)
36327 if (map_seen == 0)
36328 error_at (pragma_tok->location,
36329 "%<#pragma omp target data%> must contain at least "
36330 "one %<map%> clause");
36331 return NULL_TREE;
36334 tree stmt = make_node (OMP_TARGET_DATA);
36335 TREE_TYPE (stmt) = void_type_node;
36336 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36338 keep_next_level (true);
36339 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36341 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36342 return add_stmt (stmt);
36345 /* OpenMP 4.5:
36346 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36347 structured-block */
36349 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36350 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36356 static tree
36357 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36358 enum pragma_context context)
36360 bool data_seen = false;
36361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36363 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36364 const char *p = IDENTIFIER_POINTER (id);
36366 if (strcmp (p, "data") == 0)
36368 cp_lexer_consume_token (parser->lexer);
36369 data_seen = true;
36372 if (!data_seen)
36374 cp_parser_error (parser, "expected %<data%>");
36375 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36376 return NULL_TREE;
36379 if (context == pragma_stmt)
36381 error_at (pragma_tok->location,
36382 "%<#pragma %s%> may only be used in compound statements",
36383 "omp target enter data");
36384 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36385 return NULL_TREE;
36388 tree clauses
36389 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36390 "#pragma omp target enter data", pragma_tok);
36391 int map_seen = 0;
36392 for (tree *pc = &clauses; *pc;)
36394 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36395 switch (OMP_CLAUSE_MAP_KIND (*pc))
36397 case GOMP_MAP_TO:
36398 case GOMP_MAP_ALWAYS_TO:
36399 case GOMP_MAP_ALLOC:
36400 map_seen = 3;
36401 break;
36402 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36403 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36404 case GOMP_MAP_ALWAYS_POINTER:
36405 break;
36406 default:
36407 map_seen |= 1;
36408 error_at (OMP_CLAUSE_LOCATION (*pc),
36409 "%<#pragma omp target enter data%> with map-type other "
36410 "than %<to%> or %<alloc%> on %<map%> clause");
36411 *pc = OMP_CLAUSE_CHAIN (*pc);
36412 continue;
36414 pc = &OMP_CLAUSE_CHAIN (*pc);
36417 if (map_seen != 3)
36419 if (map_seen == 0)
36420 error_at (pragma_tok->location,
36421 "%<#pragma omp target enter data%> must contain at least "
36422 "one %<map%> clause");
36423 return NULL_TREE;
36426 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36427 TREE_TYPE (stmt) = void_type_node;
36428 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36429 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36430 return add_stmt (stmt);
36433 /* OpenMP 4.5:
36434 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36435 structured-block */
36437 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36438 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36444 static tree
36445 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36446 enum pragma_context context)
36448 bool data_seen = false;
36449 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36451 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36452 const char *p = IDENTIFIER_POINTER (id);
36454 if (strcmp (p, "data") == 0)
36456 cp_lexer_consume_token (parser->lexer);
36457 data_seen = true;
36460 if (!data_seen)
36462 cp_parser_error (parser, "expected %<data%>");
36463 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36464 return NULL_TREE;
36467 if (context == pragma_stmt)
36469 error_at (pragma_tok->location,
36470 "%<#pragma %s%> may only be used in compound statements",
36471 "omp target exit data");
36472 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36473 return NULL_TREE;
36476 tree clauses
36477 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36478 "#pragma omp target exit data", pragma_tok);
36479 int map_seen = 0;
36480 for (tree *pc = &clauses; *pc;)
36482 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36483 switch (OMP_CLAUSE_MAP_KIND (*pc))
36485 case GOMP_MAP_FROM:
36486 case GOMP_MAP_ALWAYS_FROM:
36487 case GOMP_MAP_RELEASE:
36488 case GOMP_MAP_DELETE:
36489 map_seen = 3;
36490 break;
36491 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36492 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36493 case GOMP_MAP_ALWAYS_POINTER:
36494 break;
36495 default:
36496 map_seen |= 1;
36497 error_at (OMP_CLAUSE_LOCATION (*pc),
36498 "%<#pragma omp target exit data%> with map-type other "
36499 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36500 " clause");
36501 *pc = OMP_CLAUSE_CHAIN (*pc);
36502 continue;
36504 pc = &OMP_CLAUSE_CHAIN (*pc);
36507 if (map_seen != 3)
36509 if (map_seen == 0)
36510 error_at (pragma_tok->location,
36511 "%<#pragma omp target exit data%> must contain at least "
36512 "one %<map%> clause");
36513 return NULL_TREE;
36516 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36517 TREE_TYPE (stmt) = void_type_node;
36518 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36519 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36520 return add_stmt (stmt);
36523 /* OpenMP 4.0:
36524 # pragma omp target update target-update-clause[optseq] new-line */
36526 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36527 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36534 static bool
36535 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36536 enum pragma_context context)
36538 if (context == pragma_stmt)
36540 error_at (pragma_tok->location,
36541 "%<#pragma %s%> may only be used in compound statements",
36542 "omp target update");
36543 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36544 return false;
36547 tree clauses
36548 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36549 "#pragma omp target update", pragma_tok);
36550 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36551 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36553 error_at (pragma_tok->location,
36554 "%<#pragma omp target update%> must contain at least one "
36555 "%<from%> or %<to%> clauses");
36556 return false;
36559 tree stmt = make_node (OMP_TARGET_UPDATE);
36560 TREE_TYPE (stmt) = void_type_node;
36561 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36562 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36563 add_stmt (stmt);
36564 return false;
36567 /* OpenMP 4.0:
36568 # pragma omp target target-clause[optseq] new-line
36569 structured-block */
36571 #define OMP_TARGET_CLAUSE_MASK \
36572 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36582 static bool
36583 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36584 enum pragma_context context, bool *if_p)
36586 tree *pc = NULL, stmt;
36588 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36590 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36591 const char *p = IDENTIFIER_POINTER (id);
36592 enum tree_code ccode = ERROR_MARK;
36594 if (strcmp (p, "teams") == 0)
36595 ccode = OMP_TEAMS;
36596 else if (strcmp (p, "parallel") == 0)
36597 ccode = OMP_PARALLEL;
36598 else if (strcmp (p, "simd") == 0)
36599 ccode = OMP_SIMD;
36600 if (ccode != ERROR_MARK)
36602 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36603 char p_name[sizeof ("#pragma omp target teams distribute "
36604 "parallel for simd")];
36606 cp_lexer_consume_token (parser->lexer);
36607 strcpy (p_name, "#pragma omp target");
36608 if (!flag_openmp) /* flag_openmp_simd */
36610 tree stmt;
36611 switch (ccode)
36613 case OMP_TEAMS:
36614 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36615 OMP_TARGET_CLAUSE_MASK,
36616 cclauses, if_p);
36617 break;
36618 case OMP_PARALLEL:
36619 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36620 OMP_TARGET_CLAUSE_MASK,
36621 cclauses, if_p);
36622 break;
36623 case OMP_SIMD:
36624 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36625 OMP_TARGET_CLAUSE_MASK,
36626 cclauses, if_p);
36627 break;
36628 default:
36629 gcc_unreachable ();
36631 return stmt != NULL_TREE;
36633 keep_next_level (true);
36634 tree sb = begin_omp_structured_block (), ret;
36635 unsigned save = cp_parser_begin_omp_structured_block (parser);
36636 switch (ccode)
36638 case OMP_TEAMS:
36639 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36640 OMP_TARGET_CLAUSE_MASK, cclauses,
36641 if_p);
36642 break;
36643 case OMP_PARALLEL:
36644 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36645 OMP_TARGET_CLAUSE_MASK, cclauses,
36646 if_p);
36647 break;
36648 case OMP_SIMD:
36649 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36650 OMP_TARGET_CLAUSE_MASK, cclauses,
36651 if_p);
36652 break;
36653 default:
36654 gcc_unreachable ();
36656 cp_parser_end_omp_structured_block (parser, save);
36657 tree body = finish_omp_structured_block (sb);
36658 if (ret == NULL_TREE)
36659 return false;
36660 if (ccode == OMP_TEAMS && !processing_template_decl)
36662 /* For combined target teams, ensure the num_teams and
36663 thread_limit clause expressions are evaluated on the host,
36664 before entering the target construct. */
36665 tree c;
36666 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36667 c; c = OMP_CLAUSE_CHAIN (c))
36668 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36669 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36670 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36672 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36673 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36674 if (expr == error_mark_node)
36675 continue;
36676 tree tmp = TARGET_EXPR_SLOT (expr);
36677 add_stmt (expr);
36678 OMP_CLAUSE_OPERAND (c, 0) = expr;
36679 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36680 OMP_CLAUSE_FIRSTPRIVATE);
36681 OMP_CLAUSE_DECL (tc) = tmp;
36682 OMP_CLAUSE_CHAIN (tc)
36683 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36684 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36687 tree stmt = make_node (OMP_TARGET);
36688 TREE_TYPE (stmt) = void_type_node;
36689 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36690 OMP_TARGET_BODY (stmt) = body;
36691 OMP_TARGET_COMBINED (stmt) = 1;
36692 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36693 add_stmt (stmt);
36694 pc = &OMP_TARGET_CLAUSES (stmt);
36695 goto check_clauses;
36697 else if (!flag_openmp) /* flag_openmp_simd */
36699 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36700 return false;
36702 else if (strcmp (p, "data") == 0)
36704 cp_lexer_consume_token (parser->lexer);
36705 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36706 return true;
36708 else if (strcmp (p, "enter") == 0)
36710 cp_lexer_consume_token (parser->lexer);
36711 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36712 return false;
36714 else if (strcmp (p, "exit") == 0)
36716 cp_lexer_consume_token (parser->lexer);
36717 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36718 return false;
36720 else if (strcmp (p, "update") == 0)
36722 cp_lexer_consume_token (parser->lexer);
36723 return cp_parser_omp_target_update (parser, pragma_tok, context);
36726 if (!flag_openmp) /* flag_openmp_simd */
36728 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36729 return false;
36732 stmt = make_node (OMP_TARGET);
36733 TREE_TYPE (stmt) = void_type_node;
36735 OMP_TARGET_CLAUSES (stmt)
36736 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36737 "#pragma omp target", pragma_tok);
36738 pc = &OMP_TARGET_CLAUSES (stmt);
36739 keep_next_level (true);
36740 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36742 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36743 add_stmt (stmt);
36745 check_clauses:
36746 while (*pc)
36748 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36749 switch (OMP_CLAUSE_MAP_KIND (*pc))
36751 case GOMP_MAP_TO:
36752 case GOMP_MAP_ALWAYS_TO:
36753 case GOMP_MAP_FROM:
36754 case GOMP_MAP_ALWAYS_FROM:
36755 case GOMP_MAP_TOFROM:
36756 case GOMP_MAP_ALWAYS_TOFROM:
36757 case GOMP_MAP_ALLOC:
36758 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36759 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36760 case GOMP_MAP_ALWAYS_POINTER:
36761 break;
36762 default:
36763 error_at (OMP_CLAUSE_LOCATION (*pc),
36764 "%<#pragma omp target%> with map-type other "
36765 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36766 "on %<map%> clause");
36767 *pc = OMP_CLAUSE_CHAIN (*pc);
36768 continue;
36770 pc = &OMP_CLAUSE_CHAIN (*pc);
36772 return true;
36775 /* OpenACC 2.0:
36776 # pragma acc cache (variable-list) new-line
36779 static tree
36780 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36782 tree stmt, clauses;
36784 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36785 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36787 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36789 stmt = make_node (OACC_CACHE);
36790 TREE_TYPE (stmt) = void_type_node;
36791 OACC_CACHE_CLAUSES (stmt) = clauses;
36792 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36793 add_stmt (stmt);
36795 return stmt;
36798 /* OpenACC 2.0:
36799 # pragma acc data oacc-data-clause[optseq] new-line
36800 structured-block */
36802 #define OACC_DATA_CLAUSE_MASK \
36803 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36811 static tree
36812 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36814 tree stmt, clauses, block;
36815 unsigned int save;
36817 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36818 "#pragma acc data", pragma_tok);
36820 block = begin_omp_parallel ();
36821 save = cp_parser_begin_omp_structured_block (parser);
36822 cp_parser_statement (parser, NULL_TREE, false, if_p);
36823 cp_parser_end_omp_structured_block (parser, save);
36824 stmt = finish_oacc_data (clauses, block);
36825 return stmt;
36828 /* OpenACC 2.0:
36829 # pragma acc host_data <clauses> new-line
36830 structured-block */
36832 #define OACC_HOST_DATA_CLAUSE_MASK \
36833 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36835 static tree
36836 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36838 tree stmt, clauses, block;
36839 unsigned int save;
36841 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36842 "#pragma acc host_data", pragma_tok);
36844 block = begin_omp_parallel ();
36845 save = cp_parser_begin_omp_structured_block (parser);
36846 cp_parser_statement (parser, NULL_TREE, false, if_p);
36847 cp_parser_end_omp_structured_block (parser, save);
36848 stmt = finish_oacc_host_data (clauses, block);
36849 return stmt;
36852 /* OpenACC 2.0:
36853 # pragma acc declare oacc-data-clause[optseq] new-line
36856 #define OACC_DECLARE_CLAUSE_MASK \
36857 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36866 static tree
36867 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36869 tree clauses, stmt;
36870 bool error = false;
36872 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36873 "#pragma acc declare", pragma_tok, true);
36876 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36878 error_at (pragma_tok->location,
36879 "no valid clauses specified in %<#pragma acc declare%>");
36880 return NULL_TREE;
36883 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36885 location_t loc = OMP_CLAUSE_LOCATION (t);
36886 tree decl = OMP_CLAUSE_DECL (t);
36887 if (!DECL_P (decl))
36889 error_at (loc, "array section in %<#pragma acc declare%>");
36890 error = true;
36891 continue;
36893 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36894 switch (OMP_CLAUSE_MAP_KIND (t))
36896 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36897 case GOMP_MAP_ALLOC:
36898 case GOMP_MAP_TO:
36899 case GOMP_MAP_FORCE_DEVICEPTR:
36900 case GOMP_MAP_DEVICE_RESIDENT:
36901 break;
36903 case GOMP_MAP_LINK:
36904 if (!global_bindings_p ()
36905 && (TREE_STATIC (decl)
36906 || !DECL_EXTERNAL (decl)))
36908 error_at (loc,
36909 "%qD must be a global variable in "
36910 "%<#pragma acc declare link%>",
36911 decl);
36912 error = true;
36913 continue;
36915 break;
36917 default:
36918 if (global_bindings_p ())
36920 error_at (loc, "invalid OpenACC clause at file scope");
36921 error = true;
36922 continue;
36924 if (DECL_EXTERNAL (decl))
36926 error_at (loc,
36927 "invalid use of %<extern%> variable %qD "
36928 "in %<#pragma acc declare%>", decl);
36929 error = true;
36930 continue;
36932 else if (TREE_PUBLIC (decl))
36934 error_at (loc,
36935 "invalid use of %<global%> variable %qD "
36936 "in %<#pragma acc declare%>", decl);
36937 error = true;
36938 continue;
36940 break;
36943 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36944 || lookup_attribute ("omp declare target link",
36945 DECL_ATTRIBUTES (decl)))
36947 error_at (loc, "variable %qD used more than once with "
36948 "%<#pragma acc declare%>", decl);
36949 error = true;
36950 continue;
36953 if (!error)
36955 tree id;
36957 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36958 id = get_identifier ("omp declare target link");
36959 else
36960 id = get_identifier ("omp declare target");
36962 DECL_ATTRIBUTES (decl)
36963 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36964 if (global_bindings_p ())
36966 symtab_node *node = symtab_node::get (decl);
36967 if (node != NULL)
36969 node->offloadable = 1;
36970 if (ENABLE_OFFLOADING)
36972 g->have_offload = true;
36973 if (is_a <varpool_node *> (node))
36974 vec_safe_push (offload_vars, decl);
36981 if (error || global_bindings_p ())
36982 return NULL_TREE;
36984 stmt = make_node (OACC_DECLARE);
36985 TREE_TYPE (stmt) = void_type_node;
36986 OACC_DECLARE_CLAUSES (stmt) = clauses;
36987 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36989 add_stmt (stmt);
36991 return NULL_TREE;
36994 /* OpenACC 2.0:
36995 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36999 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37001 LOC is the location of the #pragma token.
37004 #define OACC_ENTER_DATA_CLAUSE_MASK \
37005 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37011 #define OACC_EXIT_DATA_CLAUSE_MASK \
37012 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37019 static tree
37020 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37021 bool enter)
37023 location_t loc = pragma_tok->location;
37024 tree stmt, clauses;
37025 const char *p = "";
37027 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37028 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37030 if (strcmp (p, "data") != 0)
37032 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37033 enter ? "enter" : "exit");
37034 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37035 return NULL_TREE;
37038 cp_lexer_consume_token (parser->lexer);
37040 if (enter)
37041 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37042 "#pragma acc enter data", pragma_tok);
37043 else
37044 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37045 "#pragma acc exit data", pragma_tok);
37047 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37049 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37050 enter ? "enter" : "exit");
37051 return NULL_TREE;
37054 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37055 TREE_TYPE (stmt) = void_type_node;
37056 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37057 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37058 add_stmt (stmt);
37059 return stmt;
37062 /* OpenACC 2.0:
37063 # pragma acc loop oacc-loop-clause[optseq] new-line
37064 structured-block */
37066 #define OACC_LOOP_CLAUSE_MASK \
37067 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37078 static tree
37079 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37080 omp_clause_mask mask, tree *cclauses, bool *if_p)
37082 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37084 strcat (p_name, " loop");
37085 mask |= OACC_LOOP_CLAUSE_MASK;
37087 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37088 cclauses == NULL);
37089 if (cclauses)
37091 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37092 if (*cclauses)
37093 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37094 if (clauses)
37095 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37098 tree block = begin_omp_structured_block ();
37099 int save = cp_parser_begin_omp_structured_block (parser);
37100 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37101 cp_parser_end_omp_structured_block (parser, save);
37102 add_stmt (finish_omp_structured_block (block));
37104 return stmt;
37107 /* OpenACC 2.0:
37108 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37109 structured-block
37113 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37114 structured-block
37117 #define OACC_KERNELS_CLAUSE_MASK \
37118 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37132 #define OACC_PARALLEL_CLAUSE_MASK \
37133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37150 static tree
37151 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37152 char *p_name, bool *if_p)
37154 omp_clause_mask mask;
37155 enum tree_code code;
37156 switch (cp_parser_pragma_kind (pragma_tok))
37158 case PRAGMA_OACC_KERNELS:
37159 strcat (p_name, " kernels");
37160 mask = OACC_KERNELS_CLAUSE_MASK;
37161 code = OACC_KERNELS;
37162 break;
37163 case PRAGMA_OACC_PARALLEL:
37164 strcat (p_name, " parallel");
37165 mask = OACC_PARALLEL_CLAUSE_MASK;
37166 code = OACC_PARALLEL;
37167 break;
37168 default:
37169 gcc_unreachable ();
37172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37174 const char *p
37175 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37176 if (strcmp (p, "loop") == 0)
37178 cp_lexer_consume_token (parser->lexer);
37179 tree block = begin_omp_parallel ();
37180 tree clauses;
37181 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37182 if_p);
37183 return finish_omp_construct (code, block, clauses);
37187 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37189 tree block = begin_omp_parallel ();
37190 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37191 cp_parser_statement (parser, NULL_TREE, false, if_p);
37192 cp_parser_end_omp_structured_block (parser, save);
37193 return finish_omp_construct (code, block, clauses);
37196 /* OpenACC 2.0:
37197 # pragma acc update oacc-update-clause[optseq] new-line
37200 #define OACC_UPDATE_CLAUSE_MASK \
37201 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37208 static tree
37209 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37211 tree stmt, clauses;
37213 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37214 "#pragma acc update", pragma_tok);
37216 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37218 error_at (pragma_tok->location,
37219 "%<#pragma acc update%> must contain at least one "
37220 "%<device%> or %<host%> or %<self%> clause");
37221 return NULL_TREE;
37224 stmt = make_node (OACC_UPDATE);
37225 TREE_TYPE (stmt) = void_type_node;
37226 OACC_UPDATE_CLAUSES (stmt) = clauses;
37227 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37228 add_stmt (stmt);
37229 return stmt;
37232 /* OpenACC 2.0:
37233 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37235 LOC is the location of the #pragma token.
37238 #define OACC_WAIT_CLAUSE_MASK \
37239 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37241 static tree
37242 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37244 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37245 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37247 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37248 list = cp_parser_oacc_wait_list (parser, loc, list);
37250 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37251 "#pragma acc wait", pragma_tok);
37253 stmt = c_finish_oacc_wait (loc, list, clauses);
37254 stmt = finish_expr_stmt (stmt);
37256 return stmt;
37259 /* OpenMP 4.0:
37260 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37262 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37263 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37270 static void
37271 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37272 enum pragma_context context)
37274 bool first_p = parser->omp_declare_simd == NULL;
37275 cp_omp_declare_simd_data data;
37276 if (first_p)
37278 data.error_seen = false;
37279 data.fndecl_seen = false;
37280 data.tokens = vNULL;
37281 data.clauses = NULL_TREE;
37282 /* It is safe to take the address of a local variable; it will only be
37283 used while this scope is live. */
37284 parser->omp_declare_simd = &data;
37287 /* Store away all pragma tokens. */
37288 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37289 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37290 cp_lexer_consume_token (parser->lexer);
37291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37292 parser->omp_declare_simd->error_seen = true;
37293 cp_parser_require_pragma_eol (parser, pragma_tok);
37294 struct cp_token_cache *cp
37295 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37296 parser->omp_declare_simd->tokens.safe_push (cp);
37298 if (first_p)
37300 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37301 cp_parser_pragma (parser, context, NULL);
37302 switch (context)
37304 case pragma_external:
37305 cp_parser_declaration (parser);
37306 break;
37307 case pragma_member:
37308 cp_parser_member_declaration (parser);
37309 break;
37310 case pragma_objc_icode:
37311 cp_parser_block_declaration (parser, /*statement_p=*/false);
37312 break;
37313 default:
37314 cp_parser_declaration_statement (parser);
37315 break;
37317 if (parser->omp_declare_simd
37318 && !parser->omp_declare_simd->error_seen
37319 && !parser->omp_declare_simd->fndecl_seen)
37320 error_at (pragma_tok->location,
37321 "%<#pragma omp declare simd%> not immediately followed by "
37322 "function declaration or definition");
37323 data.tokens.release ();
37324 parser->omp_declare_simd = NULL;
37328 /* Finalize #pragma omp declare simd clauses after direct declarator has
37329 been parsed, and put that into "omp declare simd" attribute. */
37331 static tree
37332 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37334 struct cp_token_cache *ce;
37335 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37336 int i;
37338 if (!data->error_seen && data->fndecl_seen)
37340 error ("%<#pragma omp declare simd%> not immediately followed by "
37341 "a single function declaration or definition");
37342 data->error_seen = true;
37344 if (data->error_seen)
37345 return attrs;
37347 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37349 tree c, cl;
37351 cp_parser_push_lexer_for_tokens (parser, ce);
37352 parser->lexer->in_pragma = true;
37353 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37354 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37355 cp_lexer_consume_token (parser->lexer);
37356 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37357 "#pragma omp declare simd", pragma_tok);
37358 cp_parser_pop_lexer (parser);
37359 if (cl)
37360 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37361 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37362 TREE_CHAIN (c) = attrs;
37363 if (processing_template_decl)
37364 ATTR_IS_DEPENDENT (c) = 1;
37365 attrs = c;
37368 data->fndecl_seen = true;
37369 return attrs;
37373 /* OpenMP 4.0:
37374 # pragma omp declare target new-line
37375 declarations and definitions
37376 # pragma omp end declare target new-line
37378 OpenMP 4.5:
37379 # pragma omp declare target ( extended-list ) new-line
37381 # pragma omp declare target declare-target-clauses[seq] new-line */
37383 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37384 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37387 static void
37388 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37390 tree clauses = NULL_TREE;
37391 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37392 clauses
37393 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37394 "#pragma omp declare target", pragma_tok);
37395 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37397 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37398 clauses);
37399 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37400 cp_parser_require_pragma_eol (parser, pragma_tok);
37402 else
37404 cp_parser_require_pragma_eol (parser, pragma_tok);
37405 scope_chain->omp_declare_target_attribute++;
37406 return;
37408 if (scope_chain->omp_declare_target_attribute)
37409 error_at (pragma_tok->location,
37410 "%<#pragma omp declare target%> with clauses in between "
37411 "%<#pragma omp declare target%> without clauses and "
37412 "%<#pragma omp end declare target%>");
37413 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37415 tree t = OMP_CLAUSE_DECL (c), id;
37416 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37417 tree at2 = lookup_attribute ("omp declare target link",
37418 DECL_ATTRIBUTES (t));
37419 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37421 id = get_identifier ("omp declare target link");
37422 std::swap (at1, at2);
37424 else
37425 id = get_identifier ("omp declare target");
37426 if (at2)
37428 error_at (OMP_CLAUSE_LOCATION (c),
37429 "%qD specified both in declare target %<link%> and %<to%>"
37430 " clauses", t);
37431 continue;
37433 if (!at1)
37435 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37436 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37437 continue;
37439 symtab_node *node = symtab_node::get (t);
37440 if (node != NULL)
37442 node->offloadable = 1;
37443 if (ENABLE_OFFLOADING)
37445 g->have_offload = true;
37446 if (is_a <varpool_node *> (node))
37447 vec_safe_push (offload_vars, t);
37454 static void
37455 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37457 const char *p = "";
37458 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37460 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37461 p = IDENTIFIER_POINTER (id);
37463 if (strcmp (p, "declare") == 0)
37465 cp_lexer_consume_token (parser->lexer);
37466 p = "";
37467 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37469 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37470 p = IDENTIFIER_POINTER (id);
37472 if (strcmp (p, "target") == 0)
37473 cp_lexer_consume_token (parser->lexer);
37474 else
37476 cp_parser_error (parser, "expected %<target%>");
37477 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37478 return;
37481 else
37483 cp_parser_error (parser, "expected %<declare%>");
37484 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37485 return;
37487 cp_parser_require_pragma_eol (parser, pragma_tok);
37488 if (!scope_chain->omp_declare_target_attribute)
37489 error_at (pragma_tok->location,
37490 "%<#pragma omp end declare target%> without corresponding "
37491 "%<#pragma omp declare target%>");
37492 else
37493 scope_chain->omp_declare_target_attribute--;
37496 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37497 expression and optional initializer clause of
37498 #pragma omp declare reduction. We store the expression(s) as
37499 either 3, 6 or 7 special statements inside of the artificial function's
37500 body. The first two statements are DECL_EXPRs for the artificial
37501 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37502 expression that uses those variables.
37503 If there was any INITIALIZER clause, this is followed by further statements,
37504 the fourth and fifth statements are DECL_EXPRs for the artificial
37505 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37506 constructor variant (first token after open paren is not omp_priv),
37507 then the sixth statement is a statement with the function call expression
37508 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37509 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37510 to initialize the OMP_PRIV artificial variable and there is seventh
37511 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37513 static bool
37514 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37516 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37517 gcc_assert (TYPE_REF_P (type));
37518 type = TREE_TYPE (type);
37519 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37520 DECL_ARTIFICIAL (omp_out) = 1;
37521 pushdecl (omp_out);
37522 add_decl_expr (omp_out);
37523 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37524 DECL_ARTIFICIAL (omp_in) = 1;
37525 pushdecl (omp_in);
37526 add_decl_expr (omp_in);
37527 tree combiner;
37528 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37530 keep_next_level (true);
37531 tree block = begin_omp_structured_block ();
37532 combiner = cp_parser_expression (parser);
37533 finish_expr_stmt (combiner);
37534 block = finish_omp_structured_block (block);
37535 add_stmt (block);
37537 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37538 return false;
37540 const char *p = "";
37541 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37543 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37544 p = IDENTIFIER_POINTER (id);
37547 if (strcmp (p, "initializer") == 0)
37549 cp_lexer_consume_token (parser->lexer);
37550 matching_parens parens;
37551 if (!parens.require_open (parser))
37552 return false;
37554 p = "";
37555 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37557 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37558 p = IDENTIFIER_POINTER (id);
37561 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37562 DECL_ARTIFICIAL (omp_priv) = 1;
37563 pushdecl (omp_priv);
37564 add_decl_expr (omp_priv);
37565 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37566 DECL_ARTIFICIAL (omp_orig) = 1;
37567 pushdecl (omp_orig);
37568 add_decl_expr (omp_orig);
37570 keep_next_level (true);
37571 block = begin_omp_structured_block ();
37573 bool ctor = false;
37574 if (strcmp (p, "omp_priv") == 0)
37576 bool is_direct_init, is_non_constant_init;
37577 ctor = true;
37578 cp_lexer_consume_token (parser->lexer);
37579 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37580 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37581 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37582 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37583 == CPP_CLOSE_PAREN
37584 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37585 == CPP_CLOSE_PAREN))
37587 finish_omp_structured_block (block);
37588 error ("invalid initializer clause");
37589 return false;
37591 initializer = cp_parser_initializer (parser, &is_direct_init,
37592 &is_non_constant_init);
37593 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37594 NULL_TREE, LOOKUP_ONLYCONVERTING);
37596 else
37598 cp_parser_parse_tentatively (parser);
37599 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37600 /*check_dependency_p=*/true,
37601 /*template_p=*/NULL,
37602 /*declarator_p=*/false,
37603 /*optional_p=*/false);
37604 vec<tree, va_gc> *args;
37605 if (fn_name == error_mark_node
37606 || cp_parser_error_occurred (parser)
37607 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37608 || ((args = cp_parser_parenthesized_expression_list
37609 (parser, non_attr, /*cast_p=*/false,
37610 /*allow_expansion_p=*/true,
37611 /*non_constant_p=*/NULL)),
37612 cp_parser_error_occurred (parser)))
37614 finish_omp_structured_block (block);
37615 cp_parser_abort_tentative_parse (parser);
37616 cp_parser_error (parser, "expected id-expression (arguments)");
37617 return false;
37619 unsigned int i;
37620 tree arg;
37621 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37622 if (arg == omp_priv
37623 || (TREE_CODE (arg) == ADDR_EXPR
37624 && TREE_OPERAND (arg, 0) == omp_priv))
37625 break;
37626 cp_parser_abort_tentative_parse (parser);
37627 if (arg == NULL_TREE)
37628 error ("one of the initializer call arguments should be %<omp_priv%>"
37629 " or %<&omp_priv%>");
37630 initializer = cp_parser_postfix_expression (parser, false, false, false,
37631 false, NULL);
37632 finish_expr_stmt (initializer);
37635 block = finish_omp_structured_block (block);
37636 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37637 add_stmt (block);
37639 if (ctor)
37640 add_decl_expr (omp_orig);
37642 if (!parens.require_close (parser))
37643 return false;
37646 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37647 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37648 UNKNOWN_LOCATION);
37650 return true;
37653 /* OpenMP 4.0
37654 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37655 initializer-clause[opt] new-line
37657 initializer-clause:
37658 initializer (omp_priv initializer)
37659 initializer (function-name (argument-list)) */
37661 static void
37662 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37663 enum pragma_context)
37665 auto_vec<tree> types;
37666 enum tree_code reduc_code = ERROR_MARK;
37667 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37668 unsigned int i;
37669 cp_token *first_token;
37670 cp_token_cache *cp;
37671 int errs;
37672 void *p;
37674 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37675 p = obstack_alloc (&declarator_obstack, 0);
37677 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37678 goto fail;
37680 switch (cp_lexer_peek_token (parser->lexer)->type)
37682 case CPP_PLUS:
37683 reduc_code = PLUS_EXPR;
37684 break;
37685 case CPP_MULT:
37686 reduc_code = MULT_EXPR;
37687 break;
37688 case CPP_MINUS:
37689 reduc_code = MINUS_EXPR;
37690 break;
37691 case CPP_AND:
37692 reduc_code = BIT_AND_EXPR;
37693 break;
37694 case CPP_XOR:
37695 reduc_code = BIT_XOR_EXPR;
37696 break;
37697 case CPP_OR:
37698 reduc_code = BIT_IOR_EXPR;
37699 break;
37700 case CPP_AND_AND:
37701 reduc_code = TRUTH_ANDIF_EXPR;
37702 break;
37703 case CPP_OR_OR:
37704 reduc_code = TRUTH_ORIF_EXPR;
37705 break;
37706 case CPP_NAME:
37707 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37708 break;
37709 default:
37710 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37711 "%<|%>, %<&&%>, %<||%> or identifier");
37712 goto fail;
37715 if (reduc_code != ERROR_MARK)
37716 cp_lexer_consume_token (parser->lexer);
37718 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37719 if (reduc_id == error_mark_node)
37720 goto fail;
37722 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37723 goto fail;
37725 /* Types may not be defined in declare reduction type list. */
37726 const char *saved_message;
37727 saved_message = parser->type_definition_forbidden_message;
37728 parser->type_definition_forbidden_message
37729 = G_("types may not be defined in declare reduction type list");
37730 bool saved_colon_corrects_to_scope_p;
37731 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37732 parser->colon_corrects_to_scope_p = false;
37733 bool saved_colon_doesnt_start_class_def_p;
37734 saved_colon_doesnt_start_class_def_p
37735 = parser->colon_doesnt_start_class_def_p;
37736 parser->colon_doesnt_start_class_def_p = true;
37738 while (true)
37740 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37741 type = cp_parser_type_id (parser);
37742 if (type == error_mark_node)
37744 else if (ARITHMETIC_TYPE_P (type)
37745 && (orig_reduc_id == NULL_TREE
37746 || (TREE_CODE (type) != COMPLEX_TYPE
37747 && (id_equal (orig_reduc_id, "min")
37748 || id_equal (orig_reduc_id, "max")))))
37749 error_at (loc, "predeclared arithmetic type %qT in "
37750 "%<#pragma omp declare reduction%>", type);
37751 else if (TREE_CODE (type) == FUNCTION_TYPE
37752 || TREE_CODE (type) == METHOD_TYPE
37753 || TREE_CODE (type) == ARRAY_TYPE)
37754 error_at (loc, "function or array type %qT in "
37755 "%<#pragma omp declare reduction%>", type);
37756 else if (TYPE_REF_P (type))
37757 error_at (loc, "reference type %qT in "
37758 "%<#pragma omp declare reduction%>", type);
37759 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37760 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37761 "%<#pragma omp declare reduction%>", type);
37762 else
37763 types.safe_push (type);
37765 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37766 cp_lexer_consume_token (parser->lexer);
37767 else
37768 break;
37771 /* Restore the saved message. */
37772 parser->type_definition_forbidden_message = saved_message;
37773 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37774 parser->colon_doesnt_start_class_def_p
37775 = saved_colon_doesnt_start_class_def_p;
37777 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37778 || types.is_empty ())
37780 fail:
37781 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37782 goto done;
37785 first_token = cp_lexer_peek_token (parser->lexer);
37786 cp = NULL;
37787 errs = errorcount;
37788 FOR_EACH_VEC_ELT (types, i, type)
37790 tree fntype
37791 = build_function_type_list (void_type_node,
37792 cp_build_reference_type (type, false),
37793 NULL_TREE);
37794 tree this_reduc_id = reduc_id;
37795 if (!dependent_type_p (type))
37796 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37797 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37798 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37799 DECL_ARTIFICIAL (fndecl) = 1;
37800 DECL_EXTERNAL (fndecl) = 1;
37801 DECL_DECLARED_INLINE_P (fndecl) = 1;
37802 DECL_IGNORED_P (fndecl) = 1;
37803 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37804 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37805 DECL_ATTRIBUTES (fndecl)
37806 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37807 DECL_ATTRIBUTES (fndecl));
37808 if (processing_template_decl)
37809 fndecl = push_template_decl (fndecl);
37810 bool block_scope = false;
37811 tree block = NULL_TREE;
37812 if (current_function_decl)
37814 block_scope = true;
37815 DECL_CONTEXT (fndecl) = global_namespace;
37816 if (!processing_template_decl)
37817 pushdecl (fndecl);
37819 else if (current_class_type)
37821 if (cp == NULL)
37823 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37824 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37825 cp_lexer_consume_token (parser->lexer);
37826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37827 goto fail;
37828 cp = cp_token_cache_new (first_token,
37829 cp_lexer_peek_nth_token (parser->lexer,
37830 2));
37832 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37833 finish_member_declaration (fndecl);
37834 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37835 DECL_PENDING_INLINE_P (fndecl) = 1;
37836 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37837 continue;
37839 else
37841 DECL_CONTEXT (fndecl) = current_namespace;
37842 pushdecl (fndecl);
37844 if (!block_scope)
37845 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37846 else
37847 block = begin_omp_structured_block ();
37848 if (cp)
37850 cp_parser_push_lexer_for_tokens (parser, cp);
37851 parser->lexer->in_pragma = true;
37853 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37855 if (!block_scope)
37856 finish_function (/*inline_p=*/false);
37857 else
37858 DECL_CONTEXT (fndecl) = current_function_decl;
37859 if (cp)
37860 cp_parser_pop_lexer (parser);
37861 goto fail;
37863 if (cp)
37864 cp_parser_pop_lexer (parser);
37865 if (!block_scope)
37866 finish_function (/*inline_p=*/false);
37867 else
37869 DECL_CONTEXT (fndecl) = current_function_decl;
37870 block = finish_omp_structured_block (block);
37871 if (TREE_CODE (block) == BIND_EXPR)
37872 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37873 else if (TREE_CODE (block) == STATEMENT_LIST)
37874 DECL_SAVED_TREE (fndecl) = block;
37875 if (processing_template_decl)
37876 add_decl_expr (fndecl);
37878 cp_check_omp_declare_reduction (fndecl);
37879 if (cp == NULL && types.length () > 1)
37880 cp = cp_token_cache_new (first_token,
37881 cp_lexer_peek_nth_token (parser->lexer, 2));
37882 if (errs != errorcount)
37883 break;
37886 cp_parser_require_pragma_eol (parser, pragma_tok);
37888 done:
37889 /* Free any declarators allocated. */
37890 obstack_free (&declarator_obstack, p);
37893 /* OpenMP 4.0
37894 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37895 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37896 initializer-clause[opt] new-line
37897 #pragma omp declare target new-line */
37899 static bool
37900 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37901 enum pragma_context context)
37903 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37905 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37906 const char *p = IDENTIFIER_POINTER (id);
37908 if (strcmp (p, "simd") == 0)
37910 cp_lexer_consume_token (parser->lexer);
37911 cp_parser_omp_declare_simd (parser, pragma_tok,
37912 context);
37913 return true;
37915 cp_ensure_no_omp_declare_simd (parser);
37916 if (strcmp (p, "reduction") == 0)
37918 cp_lexer_consume_token (parser->lexer);
37919 cp_parser_omp_declare_reduction (parser, pragma_tok,
37920 context);
37921 return false;
37923 if (!flag_openmp) /* flag_openmp_simd */
37925 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37926 return false;
37928 if (strcmp (p, "target") == 0)
37930 cp_lexer_consume_token (parser->lexer);
37931 cp_parser_omp_declare_target (parser, pragma_tok);
37932 return false;
37935 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37936 "or %<target%>");
37937 cp_parser_require_pragma_eol (parser, pragma_tok);
37938 return false;
37941 /* OpenMP 4.5:
37942 #pragma omp taskloop taskloop-clause[optseq] new-line
37943 for-loop
37945 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37946 for-loop */
37948 #define OMP_TASKLOOP_CLAUSE_MASK \
37949 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37964 static tree
37965 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37966 char *p_name, omp_clause_mask mask, tree *cclauses,
37967 bool *if_p)
37969 tree clauses, sb, ret;
37970 unsigned int save;
37971 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37973 strcat (p_name, " taskloop");
37974 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37976 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37978 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37979 const char *p = IDENTIFIER_POINTER (id);
37981 if (strcmp (p, "simd") == 0)
37983 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37984 if (cclauses == NULL)
37985 cclauses = cclauses_buf;
37987 cp_lexer_consume_token (parser->lexer);
37988 if (!flag_openmp) /* flag_openmp_simd */
37989 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37990 cclauses, if_p);
37991 sb = begin_omp_structured_block ();
37992 save = cp_parser_begin_omp_structured_block (parser);
37993 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37994 cclauses, if_p);
37995 cp_parser_end_omp_structured_block (parser, save);
37996 tree body = finish_omp_structured_block (sb);
37997 if (ret == NULL)
37998 return ret;
37999 ret = make_node (OMP_TASKLOOP);
38000 TREE_TYPE (ret) = void_type_node;
38001 OMP_FOR_BODY (ret) = body;
38002 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38003 SET_EXPR_LOCATION (ret, loc);
38004 add_stmt (ret);
38005 return ret;
38008 if (!flag_openmp) /* flag_openmp_simd */
38010 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38011 return NULL_TREE;
38014 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38015 cclauses == NULL);
38016 if (cclauses)
38018 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38019 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38022 sb = begin_omp_structured_block ();
38023 save = cp_parser_begin_omp_structured_block (parser);
38025 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38026 if_p);
38028 cp_parser_end_omp_structured_block (parser, save);
38029 add_stmt (finish_omp_structured_block (sb));
38031 return ret;
38035 /* OpenACC 2.0:
38036 # pragma acc routine oacc-routine-clause[optseq] new-line
38037 function-definition
38039 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38042 #define OACC_ROUTINE_CLAUSE_MASK \
38043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38049 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38050 component, which must resolve to a declared namespace-scope
38051 function. The clauses are either processed directly (for a named
38052 function), or defered until the immediatley following declaration
38053 is parsed. */
38055 static void
38056 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38057 enum pragma_context context)
38059 gcc_checking_assert (context == pragma_external);
38060 /* The checking for "another pragma following this one" in the "no optional
38061 '( name )'" case makes sure that we dont re-enter. */
38062 gcc_checking_assert (parser->oacc_routine == NULL);
38064 cp_oacc_routine_data data;
38065 data.error_seen = false;
38066 data.fndecl_seen = false;
38067 data.tokens = vNULL;
38068 data.clauses = NULL_TREE;
38069 data.loc = pragma_tok->location;
38070 /* It is safe to take the address of a local variable; it will only be
38071 used while this scope is live. */
38072 parser->oacc_routine = &data;
38074 /* Look for optional '( name )'. */
38075 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38077 matching_parens parens;
38078 parens.consume_open (parser); /* '(' */
38080 /* We parse the name as an id-expression. If it resolves to
38081 anything other than a non-overloaded function at namespace
38082 scope, it's an error. */
38083 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38084 tree name = cp_parser_id_expression (parser,
38085 /*template_keyword_p=*/false,
38086 /*check_dependency_p=*/false,
38087 /*template_p=*/NULL,
38088 /*declarator_p=*/false,
38089 /*optional_p=*/false);
38090 tree decl = (identifier_p (name)
38091 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38092 : name);
38093 if (name != error_mark_node && decl == error_mark_node)
38094 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38096 if (decl == error_mark_node
38097 || !parens.require_close (parser))
38099 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38100 parser->oacc_routine = NULL;
38101 return;
38104 data.clauses
38105 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38106 "#pragma acc routine",
38107 cp_lexer_peek_token (parser->lexer));
38109 if (decl && is_overloaded_fn (decl)
38110 && (TREE_CODE (decl) != FUNCTION_DECL
38111 || DECL_FUNCTION_TEMPLATE_P (decl)))
38113 error_at (name_loc,
38114 "%<#pragma acc routine%> names a set of overloads");
38115 parser->oacc_routine = NULL;
38116 return;
38119 /* Perhaps we should use the same rule as declarations in different
38120 namespaces? */
38121 if (!DECL_NAMESPACE_SCOPE_P (decl))
38123 error_at (name_loc,
38124 "%qD does not refer to a namespace scope function", decl);
38125 parser->oacc_routine = NULL;
38126 return;
38129 if (TREE_CODE (decl) != FUNCTION_DECL)
38131 error_at (name_loc, "%qD does not refer to a function", decl);
38132 parser->oacc_routine = NULL;
38133 return;
38136 cp_finalize_oacc_routine (parser, decl, false);
38137 parser->oacc_routine = NULL;
38139 else /* No optional '( name )'. */
38141 /* Store away all pragma tokens. */
38142 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38143 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38144 cp_lexer_consume_token (parser->lexer);
38145 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38146 parser->oacc_routine->error_seen = true;
38147 cp_parser_require_pragma_eol (parser, pragma_tok);
38148 struct cp_token_cache *cp
38149 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38150 parser->oacc_routine->tokens.safe_push (cp);
38152 /* Emit a helpful diagnostic if there's another pragma following this
38153 one. */
38154 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38156 cp_ensure_no_oacc_routine (parser);
38157 data.tokens.release ();
38158 /* ..., and then just keep going. */
38159 return;
38162 /* We only have to consider the pragma_external case here. */
38163 cp_parser_declaration (parser);
38164 if (parser->oacc_routine
38165 && !parser->oacc_routine->fndecl_seen)
38166 cp_ensure_no_oacc_routine (parser);
38167 else
38168 parser->oacc_routine = NULL;
38169 data.tokens.release ();
38173 /* Finalize #pragma acc routine clauses after direct declarator has
38174 been parsed. */
38176 static tree
38177 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38179 struct cp_token_cache *ce;
38180 cp_oacc_routine_data *data = parser->oacc_routine;
38182 if (!data->error_seen && data->fndecl_seen)
38184 error_at (data->loc,
38185 "%<#pragma acc routine%> not immediately followed by "
38186 "a single function declaration or definition");
38187 data->error_seen = true;
38189 if (data->error_seen)
38190 return attrs;
38192 gcc_checking_assert (data->tokens.length () == 1);
38193 ce = data->tokens[0];
38195 cp_parser_push_lexer_for_tokens (parser, ce);
38196 parser->lexer->in_pragma = true;
38197 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38199 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38200 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38201 parser->oacc_routine->clauses
38202 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38203 "#pragma acc routine", pragma_tok);
38204 cp_parser_pop_lexer (parser);
38205 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38206 fndecl_seen. */
38208 return attrs;
38211 /* Apply any saved OpenACC routine clauses to a just-parsed
38212 declaration. */
38214 static void
38215 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38217 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38219 /* Keep going if we're in error reporting mode. */
38220 if (parser->oacc_routine->error_seen
38221 || fndecl == error_mark_node)
38222 return;
38224 if (parser->oacc_routine->fndecl_seen)
38226 error_at (parser->oacc_routine->loc,
38227 "%<#pragma acc routine%> not immediately followed by"
38228 " a single function declaration or definition");
38229 parser->oacc_routine = NULL;
38230 return;
38232 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38234 cp_ensure_no_oacc_routine (parser);
38235 return;
38238 if (oacc_get_fn_attrib (fndecl))
38240 error_at (parser->oacc_routine->loc,
38241 "%<#pragma acc routine%> already applied to %qD", fndecl);
38242 parser->oacc_routine = NULL;
38243 return;
38246 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38248 error_at (parser->oacc_routine->loc,
38249 TREE_USED (fndecl)
38250 ? G_("%<#pragma acc routine%> must be applied before use")
38251 : G_("%<#pragma acc routine%> must be applied before "
38252 "definition"));
38253 parser->oacc_routine = NULL;
38254 return;
38257 /* Process the routine's dimension clauses. */
38258 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38259 oacc_replace_fn_attrib (fndecl, dims);
38261 /* Add an "omp declare target" attribute. */
38262 DECL_ATTRIBUTES (fndecl)
38263 = tree_cons (get_identifier ("omp declare target"),
38264 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38266 /* Don't unset parser->oacc_routine here: we may still need it to
38267 diagnose wrong usage. But, remember that we've used this "#pragma acc
38268 routine". */
38269 parser->oacc_routine->fndecl_seen = true;
38273 /* Main entry point to OpenMP statement pragmas. */
38275 static void
38276 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38278 tree stmt;
38279 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38280 omp_clause_mask mask (0);
38282 switch (cp_parser_pragma_kind (pragma_tok))
38284 case PRAGMA_OACC_ATOMIC:
38285 cp_parser_omp_atomic (parser, pragma_tok);
38286 return;
38287 case PRAGMA_OACC_CACHE:
38288 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38289 break;
38290 case PRAGMA_OACC_DATA:
38291 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38292 break;
38293 case PRAGMA_OACC_ENTER_DATA:
38294 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38295 break;
38296 case PRAGMA_OACC_EXIT_DATA:
38297 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38298 break;
38299 case PRAGMA_OACC_HOST_DATA:
38300 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38301 break;
38302 case PRAGMA_OACC_KERNELS:
38303 case PRAGMA_OACC_PARALLEL:
38304 strcpy (p_name, "#pragma acc");
38305 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38306 if_p);
38307 break;
38308 case PRAGMA_OACC_LOOP:
38309 strcpy (p_name, "#pragma acc");
38310 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38311 if_p);
38312 break;
38313 case PRAGMA_OACC_UPDATE:
38314 stmt = cp_parser_oacc_update (parser, pragma_tok);
38315 break;
38316 case PRAGMA_OACC_WAIT:
38317 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38318 break;
38319 case PRAGMA_OMP_ATOMIC:
38320 cp_parser_omp_atomic (parser, pragma_tok);
38321 return;
38322 case PRAGMA_OMP_CRITICAL:
38323 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38324 break;
38325 case PRAGMA_OMP_DISTRIBUTE:
38326 strcpy (p_name, "#pragma omp");
38327 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38328 if_p);
38329 break;
38330 case PRAGMA_OMP_FOR:
38331 strcpy (p_name, "#pragma omp");
38332 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38333 if_p);
38334 break;
38335 case PRAGMA_OMP_MASTER:
38336 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38337 break;
38338 case PRAGMA_OMP_PARALLEL:
38339 strcpy (p_name, "#pragma omp");
38340 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38341 if_p);
38342 break;
38343 case PRAGMA_OMP_SECTIONS:
38344 strcpy (p_name, "#pragma omp");
38345 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38346 break;
38347 case PRAGMA_OMP_SIMD:
38348 strcpy (p_name, "#pragma omp");
38349 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38350 if_p);
38351 break;
38352 case PRAGMA_OMP_SINGLE:
38353 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38354 break;
38355 case PRAGMA_OMP_TASK:
38356 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38357 break;
38358 case PRAGMA_OMP_TASKGROUP:
38359 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38360 break;
38361 case PRAGMA_OMP_TASKLOOP:
38362 strcpy (p_name, "#pragma omp");
38363 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38364 if_p);
38365 break;
38366 case PRAGMA_OMP_TEAMS:
38367 strcpy (p_name, "#pragma omp");
38368 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38369 if_p);
38370 break;
38371 default:
38372 gcc_unreachable ();
38375 protected_set_expr_location (stmt, pragma_tok->location);
38378 /* Transactional Memory parsing routines. */
38380 /* Parse a transaction attribute.
38382 txn-attribute:
38383 attribute
38384 [ [ identifier ] ]
38386 We use this instead of cp_parser_attributes_opt for transactions to avoid
38387 the pedwarn in C++98 mode. */
38389 static tree
38390 cp_parser_txn_attribute_opt (cp_parser *parser)
38392 cp_token *token;
38393 tree attr_name, attr = NULL;
38395 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38396 return cp_parser_attributes_opt (parser);
38398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38399 return NULL_TREE;
38400 cp_lexer_consume_token (parser->lexer);
38401 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38402 goto error1;
38404 token = cp_lexer_peek_token (parser->lexer);
38405 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38407 token = cp_lexer_consume_token (parser->lexer);
38409 attr_name = (token->type == CPP_KEYWORD
38410 /* For keywords, use the canonical spelling,
38411 not the parsed identifier. */
38412 ? ridpointers[(int) token->keyword]
38413 : token->u.value);
38414 attr = build_tree_list (attr_name, NULL_TREE);
38416 else
38417 cp_parser_error (parser, "expected identifier");
38419 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38420 error1:
38421 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38422 return attr;
38425 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38427 transaction-statement:
38428 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38429 compound-statement
38430 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38433 static tree
38434 cp_parser_transaction (cp_parser *parser, cp_token *token)
38436 unsigned char old_in = parser->in_transaction;
38437 unsigned char this_in = 1, new_in;
38438 enum rid keyword = token->keyword;
38439 tree stmt, attrs, noex;
38441 cp_lexer_consume_token (parser->lexer);
38443 if (keyword == RID_TRANSACTION_RELAXED
38444 || keyword == RID_SYNCHRONIZED)
38445 this_in |= TM_STMT_ATTR_RELAXED;
38446 else
38448 attrs = cp_parser_txn_attribute_opt (parser);
38449 if (attrs)
38450 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38453 /* Parse a noexcept specification. */
38454 if (keyword == RID_ATOMIC_NOEXCEPT)
38455 noex = boolean_true_node;
38456 else if (keyword == RID_ATOMIC_CANCEL)
38458 /* cancel-and-throw is unimplemented. */
38459 sorry ("atomic_cancel");
38460 noex = NULL_TREE;
38462 else
38463 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38465 /* Keep track if we're in the lexical scope of an outer transaction. */
38466 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38468 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38470 parser->in_transaction = new_in;
38471 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38472 parser->in_transaction = old_in;
38474 finish_transaction_stmt (stmt, NULL, this_in, noex);
38476 return stmt;
38479 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38481 transaction-expression:
38482 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38483 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38486 static tree
38487 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38489 unsigned char old_in = parser->in_transaction;
38490 unsigned char this_in = 1;
38491 cp_token *token;
38492 tree expr, noex;
38493 bool noex_expr;
38494 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38496 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38497 || keyword == RID_TRANSACTION_RELAXED);
38499 if (!flag_tm)
38500 error_at (loc,
38501 keyword == RID_TRANSACTION_RELAXED
38502 ? G_("%<__transaction_relaxed%> without transactional memory "
38503 "support enabled")
38504 : G_("%<__transaction_atomic%> without transactional memory "
38505 "support enabled"));
38507 token = cp_parser_require_keyword (parser, keyword,
38508 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38509 : RT_TRANSACTION_RELAXED));
38510 gcc_assert (token != NULL);
38512 if (keyword == RID_TRANSACTION_RELAXED)
38513 this_in |= TM_STMT_ATTR_RELAXED;
38515 /* Set this early. This might mean that we allow transaction_cancel in
38516 an expression that we find out later actually has to be a constexpr.
38517 However, we expect that cxx_constant_value will be able to deal with
38518 this; also, if the noexcept has no constexpr, then what we parse next
38519 really is a transaction's body. */
38520 parser->in_transaction = this_in;
38522 /* Parse a noexcept specification. */
38523 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38524 true);
38526 if (!noex || !noex_expr
38527 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38529 matching_parens parens;
38530 parens.require_open (parser);
38532 expr = cp_parser_expression (parser);
38533 expr = finish_parenthesized_expr (expr);
38535 parens.require_close (parser);
38537 else
38539 /* The only expression that is available got parsed for the noexcept
38540 already. noexcept is true then. */
38541 expr = noex;
38542 noex = boolean_true_node;
38545 expr = build_transaction_expr (token->location, expr, this_in, noex);
38546 parser->in_transaction = old_in;
38548 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38549 return error_mark_node;
38551 return (flag_tm ? expr : error_mark_node);
38554 /* Parse a function-transaction-block.
38556 function-transaction-block:
38557 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38558 function-body
38559 __transaction_atomic txn-attribute[opt] function-try-block
38560 __transaction_relaxed ctor-initializer[opt] function-body
38561 __transaction_relaxed function-try-block
38564 static void
38565 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38567 unsigned char old_in = parser->in_transaction;
38568 unsigned char new_in = 1;
38569 tree compound_stmt, stmt, attrs;
38570 cp_token *token;
38572 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38573 || keyword == RID_TRANSACTION_RELAXED);
38574 token = cp_parser_require_keyword (parser, keyword,
38575 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38576 : RT_TRANSACTION_RELAXED));
38577 gcc_assert (token != NULL);
38579 if (keyword == RID_TRANSACTION_RELAXED)
38580 new_in |= TM_STMT_ATTR_RELAXED;
38581 else
38583 attrs = cp_parser_txn_attribute_opt (parser);
38584 if (attrs)
38585 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38588 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38590 parser->in_transaction = new_in;
38592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38593 cp_parser_function_try_block (parser);
38594 else
38595 cp_parser_ctor_initializer_opt_and_function_body
38596 (parser, /*in_function_try_block=*/false);
38598 parser->in_transaction = old_in;
38600 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38603 /* Parse a __transaction_cancel statement.
38605 cancel-statement:
38606 __transaction_cancel txn-attribute[opt] ;
38607 __transaction_cancel txn-attribute[opt] throw-expression ;
38609 ??? Cancel and throw is not yet implemented. */
38611 static tree
38612 cp_parser_transaction_cancel (cp_parser *parser)
38614 cp_token *token;
38615 bool is_outer = false;
38616 tree stmt, attrs;
38618 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38619 RT_TRANSACTION_CANCEL);
38620 gcc_assert (token != NULL);
38622 attrs = cp_parser_txn_attribute_opt (parser);
38623 if (attrs)
38624 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38626 /* ??? Parse cancel-and-throw here. */
38628 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38630 if (!flag_tm)
38632 error_at (token->location, "%<__transaction_cancel%> without "
38633 "transactional memory support enabled");
38634 return error_mark_node;
38636 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38638 error_at (token->location, "%<__transaction_cancel%> within a "
38639 "%<__transaction_relaxed%>");
38640 return error_mark_node;
38642 else if (is_outer)
38644 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38645 && !is_tm_may_cancel_outer (current_function_decl))
38647 error_at (token->location, "outer %<__transaction_cancel%> not "
38648 "within outer %<__transaction_atomic%>");
38649 error_at (token->location,
38650 " or a %<transaction_may_cancel_outer%> function");
38651 return error_mark_node;
38654 else if (parser->in_transaction == 0)
38656 error_at (token->location, "%<__transaction_cancel%> not within "
38657 "%<__transaction_atomic%>");
38658 return error_mark_node;
38661 stmt = build_tm_abort_call (token->location, is_outer);
38662 add_stmt (stmt);
38664 return stmt;
38667 /* The parser. */
38669 static GTY (()) cp_parser *the_parser;
38672 /* Special handling for the first token or line in the file. The first
38673 thing in the file might be #pragma GCC pch_preprocess, which loads a
38674 PCH file, which is a GC collection point. So we need to handle this
38675 first pragma without benefit of an existing lexer structure.
38677 Always returns one token to the caller in *FIRST_TOKEN. This is
38678 either the true first token of the file, or the first token after
38679 the initial pragma. */
38681 static void
38682 cp_parser_initial_pragma (cp_token *first_token)
38684 tree name = NULL;
38686 cp_lexer_get_preprocessor_token (NULL, first_token);
38687 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38688 return;
38690 cp_lexer_get_preprocessor_token (NULL, first_token);
38691 if (first_token->type == CPP_STRING)
38693 name = first_token->u.value;
38695 cp_lexer_get_preprocessor_token (NULL, first_token);
38696 if (first_token->type != CPP_PRAGMA_EOL)
38697 error_at (first_token->location,
38698 "junk at end of %<#pragma GCC pch_preprocess%>");
38700 else
38701 error_at (first_token->location, "expected string literal");
38703 /* Skip to the end of the pragma. */
38704 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38705 cp_lexer_get_preprocessor_token (NULL, first_token);
38707 /* Now actually load the PCH file. */
38708 if (name)
38709 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38711 /* Read one more token to return to our caller. We have to do this
38712 after reading the PCH file in, since its pointers have to be
38713 live. */
38714 cp_lexer_get_preprocessor_token (NULL, first_token);
38717 /* Parse a pragma GCC ivdep. */
38719 static bool
38720 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38722 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38723 return true;
38726 /* Parse a pragma GCC unroll. */
38728 static unsigned short
38729 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38731 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38732 tree expr = cp_parser_constant_expression (parser);
38733 unsigned short unroll;
38734 expr = maybe_constant_value (expr);
38735 HOST_WIDE_INT lunroll = 0;
38736 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38737 || TREE_CODE (expr) != INTEGER_CST
38738 || (lunroll = tree_to_shwi (expr)) < 0
38739 || lunroll >= USHRT_MAX)
38741 error_at (location, "%<#pragma GCC unroll%> requires an"
38742 " assignment-expression that evaluates to a non-negative"
38743 " integral constant less than %u", USHRT_MAX);
38744 unroll = 0;
38746 else
38748 unroll = (unsigned short)lunroll;
38749 if (unroll == 0)
38750 unroll = 1;
38752 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38753 return unroll;
38756 /* Normal parsing of a pragma token. Here we can (and must) use the
38757 regular lexer. */
38759 static bool
38760 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38762 cp_token *pragma_tok;
38763 unsigned int id;
38764 tree stmt;
38765 bool ret;
38767 pragma_tok = cp_lexer_consume_token (parser->lexer);
38768 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38769 parser->lexer->in_pragma = true;
38771 id = cp_parser_pragma_kind (pragma_tok);
38772 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38773 cp_ensure_no_omp_declare_simd (parser);
38774 switch (id)
38776 case PRAGMA_GCC_PCH_PREPROCESS:
38777 error_at (pragma_tok->location,
38778 "%<#pragma GCC pch_preprocess%> must be first");
38779 break;
38781 case PRAGMA_OMP_BARRIER:
38782 switch (context)
38784 case pragma_compound:
38785 cp_parser_omp_barrier (parser, pragma_tok);
38786 return false;
38787 case pragma_stmt:
38788 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38789 "used in compound statements", "omp barrier");
38790 break;
38791 default:
38792 goto bad_stmt;
38794 break;
38796 case PRAGMA_OMP_FLUSH:
38797 switch (context)
38799 case pragma_compound:
38800 cp_parser_omp_flush (parser, pragma_tok);
38801 return false;
38802 case pragma_stmt:
38803 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38804 "used in compound statements", "omp flush");
38805 break;
38806 default:
38807 goto bad_stmt;
38809 break;
38811 case PRAGMA_OMP_TASKWAIT:
38812 switch (context)
38814 case pragma_compound:
38815 cp_parser_omp_taskwait (parser, pragma_tok);
38816 return false;
38817 case pragma_stmt:
38818 error_at (pragma_tok->location,
38819 "%<#pragma %s%> may only be used in compound statements",
38820 "omp taskwait");
38821 break;
38822 default:
38823 goto bad_stmt;
38825 break;
38827 case PRAGMA_OMP_TASKYIELD:
38828 switch (context)
38830 case pragma_compound:
38831 cp_parser_omp_taskyield (parser, pragma_tok);
38832 return false;
38833 case pragma_stmt:
38834 error_at (pragma_tok->location,
38835 "%<#pragma %s%> may only be used in compound statements",
38836 "omp taskyield");
38837 break;
38838 default:
38839 goto bad_stmt;
38841 break;
38843 case PRAGMA_OMP_CANCEL:
38844 switch (context)
38846 case pragma_compound:
38847 cp_parser_omp_cancel (parser, pragma_tok);
38848 return false;
38849 case pragma_stmt:
38850 error_at (pragma_tok->location,
38851 "%<#pragma %s%> may only be used in compound statements",
38852 "omp cancel");
38853 break;
38854 default:
38855 goto bad_stmt;
38857 break;
38859 case PRAGMA_OMP_CANCELLATION_POINT:
38860 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38861 return false;
38863 case PRAGMA_OMP_THREADPRIVATE:
38864 cp_parser_omp_threadprivate (parser, pragma_tok);
38865 return false;
38867 case PRAGMA_OMP_DECLARE:
38868 return cp_parser_omp_declare (parser, pragma_tok, context);
38870 case PRAGMA_OACC_DECLARE:
38871 cp_parser_oacc_declare (parser, pragma_tok);
38872 return false;
38874 case PRAGMA_OACC_ENTER_DATA:
38875 if (context == pragma_stmt)
38877 error_at (pragma_tok->location,
38878 "%<#pragma %s%> may only be used in compound statements",
38879 "acc enter data");
38880 break;
38882 else if (context != pragma_compound)
38883 goto bad_stmt;
38884 cp_parser_omp_construct (parser, pragma_tok, if_p);
38885 return true;
38887 case PRAGMA_OACC_EXIT_DATA:
38888 if (context == pragma_stmt)
38890 error_at (pragma_tok->location,
38891 "%<#pragma %s%> may only be used in compound statements",
38892 "acc exit data");
38893 break;
38895 else if (context != pragma_compound)
38896 goto bad_stmt;
38897 cp_parser_omp_construct (parser, pragma_tok, if_p);
38898 return true;
38900 case PRAGMA_OACC_ROUTINE:
38901 if (context != pragma_external)
38903 error_at (pragma_tok->location,
38904 "%<#pragma acc routine%> must be at file scope");
38905 break;
38907 cp_parser_oacc_routine (parser, pragma_tok, context);
38908 return false;
38910 case PRAGMA_OACC_UPDATE:
38911 if (context == pragma_stmt)
38913 error_at (pragma_tok->location,
38914 "%<#pragma %s%> may only be used in compound statements",
38915 "acc update");
38916 break;
38918 else if (context != pragma_compound)
38919 goto bad_stmt;
38920 cp_parser_omp_construct (parser, pragma_tok, if_p);
38921 return true;
38923 case PRAGMA_OACC_WAIT:
38924 if (context == pragma_stmt)
38926 error_at (pragma_tok->location,
38927 "%<#pragma %s%> may only be used in compound statements",
38928 "acc wait");
38929 break;
38931 else if (context != pragma_compound)
38932 goto bad_stmt;
38933 cp_parser_omp_construct (parser, pragma_tok, if_p);
38934 return true;
38936 case PRAGMA_OACC_ATOMIC:
38937 case PRAGMA_OACC_CACHE:
38938 case PRAGMA_OACC_DATA:
38939 case PRAGMA_OACC_HOST_DATA:
38940 case PRAGMA_OACC_KERNELS:
38941 case PRAGMA_OACC_PARALLEL:
38942 case PRAGMA_OACC_LOOP:
38943 case PRAGMA_OMP_ATOMIC:
38944 case PRAGMA_OMP_CRITICAL:
38945 case PRAGMA_OMP_DISTRIBUTE:
38946 case PRAGMA_OMP_FOR:
38947 case PRAGMA_OMP_MASTER:
38948 case PRAGMA_OMP_PARALLEL:
38949 case PRAGMA_OMP_SECTIONS:
38950 case PRAGMA_OMP_SIMD:
38951 case PRAGMA_OMP_SINGLE:
38952 case PRAGMA_OMP_TASK:
38953 case PRAGMA_OMP_TASKGROUP:
38954 case PRAGMA_OMP_TASKLOOP:
38955 case PRAGMA_OMP_TEAMS:
38956 if (context != pragma_stmt && context != pragma_compound)
38957 goto bad_stmt;
38958 stmt = push_omp_privatization_clauses (false);
38959 cp_parser_omp_construct (parser, pragma_tok, if_p);
38960 pop_omp_privatization_clauses (stmt);
38961 return true;
38963 case PRAGMA_OMP_ORDERED:
38964 if (context != pragma_stmt && context != pragma_compound)
38965 goto bad_stmt;
38966 stmt = push_omp_privatization_clauses (false);
38967 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38968 pop_omp_privatization_clauses (stmt);
38969 return ret;
38971 case PRAGMA_OMP_TARGET:
38972 if (context != pragma_stmt && context != pragma_compound)
38973 goto bad_stmt;
38974 stmt = push_omp_privatization_clauses (false);
38975 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38976 pop_omp_privatization_clauses (stmt);
38977 return ret;
38979 case PRAGMA_OMP_END_DECLARE_TARGET:
38980 cp_parser_omp_end_declare_target (parser, pragma_tok);
38981 return false;
38983 case PRAGMA_OMP_SECTION:
38984 error_at (pragma_tok->location,
38985 "%<#pragma omp section%> may only be used in "
38986 "%<#pragma omp sections%> construct");
38987 break;
38989 case PRAGMA_IVDEP:
38991 if (context == pragma_external)
38993 error_at (pragma_tok->location,
38994 "%<#pragma GCC ivdep%> must be inside a function");
38995 break;
38997 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38998 unsigned short unroll;
38999 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39000 if (tok->type == CPP_PRAGMA
39001 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39003 tok = cp_lexer_consume_token (parser->lexer);
39004 unroll = cp_parser_pragma_unroll (parser, tok);
39005 tok = cp_lexer_peek_token (the_parser->lexer);
39007 else
39008 unroll = 0;
39009 if (tok->type != CPP_KEYWORD
39010 || (tok->keyword != RID_FOR
39011 && tok->keyword != RID_WHILE
39012 && tok->keyword != RID_DO))
39014 cp_parser_error (parser, "for, while or do statement expected");
39015 return false;
39017 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39018 return true;
39021 case PRAGMA_UNROLL:
39023 if (context == pragma_external)
39025 error_at (pragma_tok->location,
39026 "%<#pragma GCC unroll%> must be inside a function");
39027 break;
39029 const unsigned short unroll
39030 = cp_parser_pragma_unroll (parser, pragma_tok);
39031 bool ivdep;
39032 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39033 if (tok->type == CPP_PRAGMA
39034 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39036 tok = cp_lexer_consume_token (parser->lexer);
39037 ivdep = cp_parser_pragma_ivdep (parser, tok);
39038 tok = cp_lexer_peek_token (the_parser->lexer);
39040 else
39041 ivdep = false;
39042 if (tok->type != CPP_KEYWORD
39043 || (tok->keyword != RID_FOR
39044 && tok->keyword != RID_WHILE
39045 && tok->keyword != RID_DO))
39047 cp_parser_error (parser, "for, while or do statement expected");
39048 return false;
39050 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39051 return true;
39054 default:
39055 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39056 c_invoke_pragma_handler (id);
39057 break;
39059 bad_stmt:
39060 cp_parser_error (parser, "expected declaration specifiers");
39061 break;
39064 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39065 return false;
39068 /* The interface the pragma parsers have to the lexer. */
39070 enum cpp_ttype
39071 pragma_lex (tree *value, location_t *loc)
39073 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39074 enum cpp_ttype ret = tok->type;
39076 *value = tok->u.value;
39077 if (loc)
39078 *loc = tok->location;
39080 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39081 ret = CPP_EOF;
39082 else if (ret == CPP_STRING)
39083 *value = cp_parser_string_literal (the_parser, false, false);
39084 else
39086 if (ret == CPP_KEYWORD)
39087 ret = CPP_NAME;
39088 cp_lexer_consume_token (the_parser->lexer);
39091 return ret;
39095 /* External interface. */
39097 /* Parse one entire translation unit. */
39099 void
39100 c_parse_file (void)
39102 static bool already_called = false;
39104 if (already_called)
39105 fatal_error (input_location,
39106 "inter-module optimizations not implemented for C++");
39107 already_called = true;
39109 the_parser = cp_parser_new ();
39110 push_deferring_access_checks (flag_access_control
39111 ? dk_no_deferred : dk_no_check);
39112 cp_parser_translation_unit (the_parser);
39113 the_parser = NULL;
39116 /* Create an identifier for a generic parameter type (a synthesized
39117 template parameter implied by `auto' or a concept identifier). */
39119 static GTY(()) int generic_parm_count;
39120 static tree
39121 make_generic_type_name ()
39123 char buf[32];
39124 sprintf (buf, "auto:%d", ++generic_parm_count);
39125 return get_identifier (buf);
39128 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39129 (creating a new template parameter list if necessary). Returns the newly
39130 created template type parm. */
39132 static tree
39133 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39135 gcc_assert (current_binding_level->kind == sk_function_parms);
39137 /* Before committing to modifying any scope, if we're in an
39138 implicit template scope, and we're trying to synthesize a
39139 constrained parameter, try to find a previous parameter with
39140 the same name. This is the same-type rule for abbreviated
39141 function templates.
39143 NOTE: We can generate implicit parameters when tentatively
39144 parsing a nested name specifier, only to reject that parse
39145 later. However, matching the same template-id as part of a
39146 direct-declarator should generate an identical template
39147 parameter, so this rule will merge them. */
39148 if (parser->implicit_template_scope && constr)
39150 tree t = parser->implicit_template_parms;
39151 while (t)
39153 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39155 tree d = TREE_VALUE (t);
39156 if (TREE_CODE (d) == PARM_DECL)
39157 /* Return the TEMPLATE_PARM_INDEX. */
39158 d = DECL_INITIAL (d);
39159 return d;
39161 t = TREE_CHAIN (t);
39165 /* We are either continuing a function template that already contains implicit
39166 template parameters, creating a new fully-implicit function template, or
39167 extending an existing explicit function template with implicit template
39168 parameters. */
39170 cp_binding_level *const entry_scope = current_binding_level;
39172 bool become_template = false;
39173 cp_binding_level *parent_scope = 0;
39175 if (parser->implicit_template_scope)
39177 gcc_assert (parser->implicit_template_parms);
39179 current_binding_level = parser->implicit_template_scope;
39181 else
39183 /* Roll back to the existing template parameter scope (in the case of
39184 extending an explicit function template) or introduce a new template
39185 parameter scope ahead of the function parameter scope (or class scope
39186 in the case of out-of-line member definitions). The function scope is
39187 added back after template parameter synthesis below. */
39189 cp_binding_level *scope = entry_scope;
39191 while (scope->kind == sk_function_parms)
39193 parent_scope = scope;
39194 scope = scope->level_chain;
39196 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39198 /* If not defining a class, then any class scope is a scope level in
39199 an out-of-line member definition. In this case simply wind back
39200 beyond the first such scope to inject the template parameter list.
39201 Otherwise wind back to the class being defined. The latter can
39202 occur in class member friend declarations such as:
39204 class A {
39205 void foo (auto);
39207 class B {
39208 friend void A::foo (auto);
39211 The template parameter list synthesized for the friend declaration
39212 must be injected in the scope of 'B'. This can also occur in
39213 erroneous cases such as:
39215 struct A {
39216 struct B {
39217 void foo (auto);
39219 void B::foo (auto) {}
39222 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39223 but, nevertheless, the template parameter list synthesized for the
39224 declarator should be injected into the scope of 'A' as if the
39225 ill-formed template was specified explicitly. */
39227 while (scope->kind == sk_class && !scope->defining_class_p)
39229 parent_scope = scope;
39230 scope = scope->level_chain;
39234 current_binding_level = scope;
39236 if (scope->kind != sk_template_parms
39237 || !function_being_declared_is_template_p (parser))
39239 /* Introduce a new template parameter list for implicit template
39240 parameters. */
39242 become_template = true;
39244 parser->implicit_template_scope
39245 = begin_scope (sk_template_parms, NULL);
39247 ++processing_template_decl;
39249 parser->fully_implicit_function_template_p = true;
39250 ++parser->num_template_parameter_lists;
39252 else
39254 /* Synthesize implicit template parameters at the end of the explicit
39255 template parameter list. */
39257 gcc_assert (current_template_parms);
39259 parser->implicit_template_scope = scope;
39261 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39262 parser->implicit_template_parms
39263 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39267 /* Synthesize a new template parameter and track the current template
39268 parameter chain with implicit_template_parms. */
39270 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39271 tree synth_id = make_generic_type_name ();
39272 tree synth_tmpl_parm;
39273 bool non_type = false;
39275 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39276 synth_tmpl_parm
39277 = finish_template_type_parm (class_type_node, synth_id);
39278 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39279 synth_tmpl_parm
39280 = finish_constrained_template_template_parm (proto, synth_id);
39281 else
39283 synth_tmpl_parm = copy_decl (proto);
39284 DECL_NAME (synth_tmpl_parm) = synth_id;
39285 non_type = true;
39288 // Attach the constraint to the parm before processing.
39289 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39290 TREE_TYPE (node) = constr;
39291 tree new_parm
39292 = process_template_parm (parser->implicit_template_parms,
39293 input_location,
39294 node,
39295 /*non_type=*/non_type,
39296 /*param_pack=*/false);
39298 // Chain the new parameter to the list of implicit parameters.
39299 if (parser->implicit_template_parms)
39300 parser->implicit_template_parms
39301 = TREE_CHAIN (parser->implicit_template_parms);
39302 else
39303 parser->implicit_template_parms = new_parm;
39305 tree new_decl = get_local_decls ();
39306 if (non_type)
39307 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39308 new_decl = DECL_INITIAL (new_decl);
39310 /* If creating a fully implicit function template, start the new implicit
39311 template parameter list with this synthesized type, otherwise grow the
39312 current template parameter list. */
39314 if (become_template)
39316 parent_scope->level_chain = current_binding_level;
39318 tree new_parms = make_tree_vec (1);
39319 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39320 current_template_parms = tree_cons (size_int (processing_template_decl),
39321 new_parms, current_template_parms);
39323 else
39325 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39326 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39327 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39328 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39331 // If the new parameter was constrained, we need to add that to the
39332 // constraints in the template parameter list.
39333 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39335 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39336 reqs = conjoin_constraints (reqs, req);
39337 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39340 current_binding_level = entry_scope;
39342 return new_decl;
39345 /* Finish the declaration of a fully implicit function template. Such a
39346 template has no explicit template parameter list so has not been through the
39347 normal template head and tail processing. synthesize_implicit_template_parm
39348 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39349 provided if the declaration is a class member such that its template
39350 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39351 form is returned. Otherwise NULL_TREE is returned. */
39353 static tree
39354 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39356 gcc_assert (parser->fully_implicit_function_template_p);
39358 if (member_decl_opt && member_decl_opt != error_mark_node
39359 && DECL_VIRTUAL_P (member_decl_opt))
39361 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39362 "implicit templates may not be %<virtual%>");
39363 DECL_VIRTUAL_P (member_decl_opt) = false;
39366 if (member_decl_opt)
39367 member_decl_opt = finish_member_template_decl (member_decl_opt);
39368 end_template_decl ();
39370 parser->fully_implicit_function_template_p = false;
39371 parser->implicit_template_parms = 0;
39372 parser->implicit_template_scope = 0;
39373 --parser->num_template_parameter_lists;
39375 return member_decl_opt;
39378 /* Like finish_fully_implicit_template, but to be used in error
39379 recovery, rearranging scopes so that we restore the state we had
39380 before synthesize_implicit_template_parm inserted the implement
39381 template parms scope. */
39383 static void
39384 abort_fully_implicit_template (cp_parser *parser)
39386 cp_binding_level *return_to_scope = current_binding_level;
39388 if (parser->implicit_template_scope
39389 && return_to_scope != parser->implicit_template_scope)
39391 cp_binding_level *child = return_to_scope;
39392 for (cp_binding_level *scope = child->level_chain;
39393 scope != parser->implicit_template_scope;
39394 scope = child->level_chain)
39395 child = scope;
39396 child->level_chain = parser->implicit_template_scope->level_chain;
39397 parser->implicit_template_scope->level_chain = return_to_scope;
39398 current_binding_level = parser->implicit_template_scope;
39400 else
39401 return_to_scope = return_to_scope->level_chain;
39403 finish_fully_implicit_template (parser, NULL);
39405 gcc_assert (current_binding_level == return_to_scope);
39408 /* Helper function for diagnostics that have complained about things
39409 being used with 'extern "C"' linkage.
39411 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39413 void
39414 maybe_show_extern_c_location (void)
39416 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39417 inform (the_parser->innermost_linkage_specification_location,
39418 "%<extern \"C\"%> linkage started here");
39421 #include "gt-cp-parser.h"