Add support for grouping of related diagnostics (PR other/84889)
[official-gcc.git] / gcc / cp / parser.c
blobaa5286b24ccbb6e7c8d77bffbcc60c15d7045744
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 auto_diagnostic_group d;
3284 error_at (location,
3285 "invalid use of template-name %qE without an argument list",
3286 decl);
3287 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3288 inform (location, "class template argument deduction is only available "
3289 "with -std=c++17 or -std=gnu++17");
3290 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3292 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3293 error_at (location, "invalid use of destructor %qD as a type", id);
3294 else if (TREE_CODE (decl) == TYPE_DECL)
3295 /* Something like 'unsigned A a;' */
3296 error_at (location, "invalid combination of multiple type-specifiers");
3297 else if (!parser->scope)
3299 /* Issue an error message. */
3300 auto_diagnostic_group d;
3301 name_hint hint;
3302 if (TREE_CODE (id) == IDENTIFIER_NODE)
3303 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3304 if (hint)
3306 gcc_rich_location richloc (location);
3307 richloc.add_fixit_replace (hint.suggestion ());
3308 error_at (&richloc,
3309 "%qE does not name a type; did you mean %qs?",
3310 id, hint.suggestion ());
3312 else
3313 error_at (location, "%qE does not name a type", id);
3314 /* If we're in a template class, it's possible that the user was
3315 referring to a type from a base class. For example:
3317 template <typename T> struct A { typedef T X; };
3318 template <typename T> struct B : public A<T> { X x; };
3320 The user should have said "typename A<T>::X". */
3321 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3322 inform (location, "C++11 %<constexpr%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3325 inform (location, "C++11 %<noexcept%> only available with "
3326 "-std=c++11 or -std=gnu++11");
3327 else if (cxx_dialect < cxx11
3328 && TREE_CODE (id) == IDENTIFIER_NODE
3329 && id_equal (id, "thread_local"))
3330 inform (location, "C++11 %<thread_local%> only available with "
3331 "-std=c++11 or -std=gnu++11");
3332 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3333 inform (location, "%<concept%> only available with -fconcepts");
3334 else if (processing_template_decl && current_class_type
3335 && TYPE_BINFO (current_class_type))
3337 tree b;
3339 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3341 b = TREE_CHAIN (b))
3343 tree base_type = BINFO_TYPE (b);
3344 if (CLASS_TYPE_P (base_type)
3345 && dependent_type_p (base_type))
3347 tree field;
3348 /* Go from a particular instantiation of the
3349 template (which will have an empty TYPE_FIELDs),
3350 to the main version. */
3351 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3352 for (field = TYPE_FIELDS (base_type);
3353 field;
3354 field = DECL_CHAIN (field))
3355 if (TREE_CODE (field) == TYPE_DECL
3356 && DECL_NAME (field) == id)
3358 inform (location,
3359 "(perhaps %<typename %T::%E%> was intended)",
3360 BINFO_TYPE (b), id);
3361 break;
3363 if (field)
3364 break;
3369 /* Here we diagnose qualified-ids where the scope is actually correct,
3370 but the identifier does not resolve to a valid type name. */
3371 else if (parser->scope != error_mark_node)
3373 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3375 auto_diagnostic_group d;
3376 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3377 error_at (location_of (id),
3378 "%qE in namespace %qE does not name a template type",
3379 id, parser->scope);
3380 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3381 error_at (location_of (id),
3382 "%qE in namespace %qE does not name a template type",
3383 TREE_OPERAND (id, 0), parser->scope);
3384 else
3385 error_at (location_of (id),
3386 "%qE in namespace %qE does not name a type",
3387 id, parser->scope);
3388 if (DECL_P (decl))
3389 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3390 else if (decl == error_mark_node)
3391 suggest_alternative_in_explicit_scope (location, id,
3392 parser->scope);
3394 else if (CLASS_TYPE_P (parser->scope)
3395 && constructor_name_p (id, parser->scope))
3397 /* A<T>::A<T>() */
3398 auto_diagnostic_group d;
3399 error_at (location, "%<%T::%E%> names the constructor, not"
3400 " the type", parser->scope, id);
3401 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3402 error_at (location, "and %qT has no template constructors",
3403 parser->scope);
3405 else if (TYPE_P (parser->scope)
3406 && dependent_scope_p (parser->scope))
3408 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3409 error_at (location,
3410 "need %<typename%> before %<%T::%D::%E%> because "
3411 "%<%T::%D%> is a dependent scope",
3412 TYPE_CONTEXT (parser->scope),
3413 TYPENAME_TYPE_FULLNAME (parser->scope),
3415 TYPE_CONTEXT (parser->scope),
3416 TYPENAME_TYPE_FULLNAME (parser->scope));
3417 else
3418 error_at (location, "need %<typename%> before %<%T::%E%> because "
3419 "%qT is a dependent scope",
3420 parser->scope, id, parser->scope);
3422 else if (TYPE_P (parser->scope))
3424 auto_diagnostic_group d;
3425 if (!COMPLETE_TYPE_P (parser->scope))
3426 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3427 parser->scope);
3428 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3429 error_at (location_of (id),
3430 "%qE in %q#T does not name a template type",
3431 id, parser->scope);
3432 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3433 error_at (location_of (id),
3434 "%qE in %q#T does not name a template type",
3435 TREE_OPERAND (id, 0), parser->scope);
3436 else
3437 error_at (location_of (id),
3438 "%qE in %q#T does not name a type",
3439 id, parser->scope);
3440 if (DECL_P (decl))
3441 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3443 else
3444 gcc_unreachable ();
3448 /* Check for a common situation where a type-name should be present,
3449 but is not, and issue a sensible error message. Returns true if an
3450 invalid type-name was detected.
3452 The situation handled by this function are variable declarations of the
3453 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3454 Usually, `ID' should name a type, but if we got here it means that it
3455 does not. We try to emit the best possible error message depending on
3456 how exactly the id-expression looks like. */
3458 static bool
3459 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3461 tree id;
3462 cp_token *token = cp_lexer_peek_token (parser->lexer);
3464 /* Avoid duplicate error about ambiguous lookup. */
3465 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3467 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3468 if (next->type == CPP_NAME && next->error_reported)
3469 goto out;
3472 cp_parser_parse_tentatively (parser);
3473 id = cp_parser_id_expression (parser,
3474 /*template_keyword_p=*/false,
3475 /*check_dependency_p=*/true,
3476 /*template_p=*/NULL,
3477 /*declarator_p=*/false,
3478 /*optional_p=*/false);
3479 /* If the next token is a (, this is a function with no explicit return
3480 type, i.e. constructor, destructor or conversion op. */
3481 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3482 || TREE_CODE (id) == TYPE_DECL)
3484 cp_parser_abort_tentative_parse (parser);
3485 return false;
3487 if (!cp_parser_parse_definitely (parser))
3488 return false;
3490 /* Emit a diagnostic for the invalid type. */
3491 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3492 out:
3493 /* If we aren't in the middle of a declarator (i.e. in a
3494 parameter-declaration-clause), skip to the end of the declaration;
3495 there's no point in trying to process it. */
3496 if (!parser->in_declarator_p)
3497 cp_parser_skip_to_end_of_block_or_statement (parser);
3498 return true;
3501 /* Consume tokens up to, and including, the next non-nested closing `)'.
3502 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3503 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3504 found an unnested token of that type. */
3506 static int
3507 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3508 bool recovering,
3509 cpp_ttype or_ttype,
3510 bool consume_paren)
3512 unsigned paren_depth = 0;
3513 unsigned brace_depth = 0;
3514 unsigned square_depth = 0;
3515 unsigned condop_depth = 0;
3517 if (recovering && or_ttype == CPP_EOF
3518 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3519 return 0;
3521 while (true)
3523 cp_token * token = cp_lexer_peek_token (parser->lexer);
3525 /* Have we found what we're looking for before the closing paren? */
3526 if (token->type == or_ttype && or_ttype != CPP_EOF
3527 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3528 return -1;
3530 switch (token->type)
3532 case CPP_EOF:
3533 case CPP_PRAGMA_EOL:
3534 /* If we've run out of tokens, then there is no closing `)'. */
3535 return 0;
3537 /* This is good for lambda expression capture-lists. */
3538 case CPP_OPEN_SQUARE:
3539 ++square_depth;
3540 break;
3541 case CPP_CLOSE_SQUARE:
3542 if (!square_depth--)
3543 return 0;
3544 break;
3546 case CPP_SEMICOLON:
3547 /* This matches the processing in skip_to_end_of_statement. */
3548 if (!brace_depth)
3549 return 0;
3550 break;
3552 case CPP_OPEN_BRACE:
3553 ++brace_depth;
3554 break;
3555 case CPP_CLOSE_BRACE:
3556 if (!brace_depth--)
3557 return 0;
3558 break;
3560 case CPP_OPEN_PAREN:
3561 if (!brace_depth)
3562 ++paren_depth;
3563 break;
3565 case CPP_CLOSE_PAREN:
3566 if (!brace_depth && !paren_depth--)
3568 if (consume_paren)
3569 cp_lexer_consume_token (parser->lexer);
3570 return 1;
3572 break;
3574 case CPP_QUERY:
3575 if (!brace_depth && !paren_depth && !square_depth)
3576 ++condop_depth;
3577 break;
3579 case CPP_COLON:
3580 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3581 condop_depth--;
3582 break;
3584 default:
3585 break;
3588 /* Consume the token. */
3589 cp_lexer_consume_token (parser->lexer);
3593 /* Consume tokens up to, and including, the next non-nested closing `)'.
3594 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3595 are doing error recovery. Returns -1 if OR_COMMA is true and we
3596 found an unnested token of that type. */
3598 static int
3599 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3600 bool recovering,
3601 bool or_comma,
3602 bool consume_paren)
3604 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3605 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3606 ttype, consume_paren);
3609 /* Consume tokens until we reach the end of the current statement.
3610 Normally, that will be just before consuming a `;'. However, if a
3611 non-nested `}' comes first, then we stop before consuming that. */
3613 static void
3614 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3616 unsigned nesting_depth = 0;
3618 /* Unwind generic function template scope if necessary. */
3619 if (parser->fully_implicit_function_template_p)
3620 abort_fully_implicit_template (parser);
3622 while (true)
3624 cp_token *token = cp_lexer_peek_token (parser->lexer);
3626 switch (token->type)
3628 case CPP_EOF:
3629 case CPP_PRAGMA_EOL:
3630 /* If we've run out of tokens, stop. */
3631 return;
3633 case CPP_SEMICOLON:
3634 /* If the next token is a `;', we have reached the end of the
3635 statement. */
3636 if (!nesting_depth)
3637 return;
3638 break;
3640 case CPP_CLOSE_BRACE:
3641 /* If this is a non-nested '}', stop before consuming it.
3642 That way, when confronted with something like:
3644 { 3 + }
3646 we stop before consuming the closing '}', even though we
3647 have not yet reached a `;'. */
3648 if (nesting_depth == 0)
3649 return;
3651 /* If it is the closing '}' for a block that we have
3652 scanned, stop -- but only after consuming the token.
3653 That way given:
3655 void f g () { ... }
3656 typedef int I;
3658 we will stop after the body of the erroneously declared
3659 function, but before consuming the following `typedef'
3660 declaration. */
3661 if (--nesting_depth == 0)
3663 cp_lexer_consume_token (parser->lexer);
3664 return;
3666 break;
3668 case CPP_OPEN_BRACE:
3669 ++nesting_depth;
3670 break;
3672 default:
3673 break;
3676 /* Consume the token. */
3677 cp_lexer_consume_token (parser->lexer);
3681 /* This function is called at the end of a statement or declaration.
3682 If the next token is a semicolon, it is consumed; otherwise, error
3683 recovery is attempted. */
3685 static void
3686 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3688 /* Look for the trailing `;'. */
3689 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3691 /* If there is additional (erroneous) input, skip to the end of
3692 the statement. */
3693 cp_parser_skip_to_end_of_statement (parser);
3694 /* If the next token is now a `;', consume it. */
3695 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3696 cp_lexer_consume_token (parser->lexer);
3700 /* Skip tokens until we have consumed an entire block, or until we
3701 have consumed a non-nested `;'. */
3703 static void
3704 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3706 int nesting_depth = 0;
3708 /* Unwind generic function template scope if necessary. */
3709 if (parser->fully_implicit_function_template_p)
3710 abort_fully_implicit_template (parser);
3712 while (nesting_depth >= 0)
3714 cp_token *token = cp_lexer_peek_token (parser->lexer);
3716 switch (token->type)
3718 case CPP_EOF:
3719 case CPP_PRAGMA_EOL:
3720 /* If we've run out of tokens, stop. */
3721 return;
3723 case CPP_SEMICOLON:
3724 /* Stop if this is an unnested ';'. */
3725 if (!nesting_depth)
3726 nesting_depth = -1;
3727 break;
3729 case CPP_CLOSE_BRACE:
3730 /* Stop if this is an unnested '}', or closes the outermost
3731 nesting level. */
3732 nesting_depth--;
3733 if (nesting_depth < 0)
3734 return;
3735 if (!nesting_depth)
3736 nesting_depth = -1;
3737 break;
3739 case CPP_OPEN_BRACE:
3740 /* Nest. */
3741 nesting_depth++;
3742 break;
3744 default:
3745 break;
3748 /* Consume the token. */
3749 cp_lexer_consume_token (parser->lexer);
3753 /* Skip tokens until a non-nested closing curly brace is the next
3754 token, or there are no more tokens. Return true in the first case,
3755 false otherwise. */
3757 static bool
3758 cp_parser_skip_to_closing_brace (cp_parser *parser)
3760 unsigned nesting_depth = 0;
3762 while (true)
3764 cp_token *token = cp_lexer_peek_token (parser->lexer);
3766 switch (token->type)
3768 case CPP_EOF:
3769 case CPP_PRAGMA_EOL:
3770 /* If we've run out of tokens, stop. */
3771 return false;
3773 case CPP_CLOSE_BRACE:
3774 /* If the next token is a non-nested `}', then we have reached
3775 the end of the current block. */
3776 if (nesting_depth-- == 0)
3777 return true;
3778 break;
3780 case CPP_OPEN_BRACE:
3781 /* If it the next token is a `{', then we are entering a new
3782 block. Consume the entire block. */
3783 ++nesting_depth;
3784 break;
3786 default:
3787 break;
3790 /* Consume the token. */
3791 cp_lexer_consume_token (parser->lexer);
3795 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3796 parameter is the PRAGMA token, allowing us to purge the entire pragma
3797 sequence. */
3799 static void
3800 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3802 cp_token *token;
3804 parser->lexer->in_pragma = false;
3807 token = cp_lexer_consume_token (parser->lexer);
3808 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3810 /* Ensure that the pragma is not parsed again. */
3811 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3814 /* Require pragma end of line, resyncing with it as necessary. The
3815 arguments are as for cp_parser_skip_to_pragma_eol. */
3817 static void
3818 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3820 parser->lexer->in_pragma = false;
3821 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3822 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3825 /* This is a simple wrapper around make_typename_type. When the id is
3826 an unresolved identifier node, we can provide a superior diagnostic
3827 using cp_parser_diagnose_invalid_type_name. */
3829 static tree
3830 cp_parser_make_typename_type (cp_parser *parser, tree id,
3831 location_t id_location)
3833 tree result;
3834 if (identifier_p (id))
3836 result = make_typename_type (parser->scope, id, typename_type,
3837 /*complain=*/tf_none);
3838 if (result == error_mark_node)
3839 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3840 return result;
3842 return make_typename_type (parser->scope, id, typename_type, tf_error);
3845 /* This is a wrapper around the
3846 make_{pointer,ptrmem,reference}_declarator functions that decides
3847 which one to call based on the CODE and CLASS_TYPE arguments. The
3848 CODE argument should be one of the values returned by
3849 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3850 appertain to the pointer or reference. */
3852 static cp_declarator *
3853 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3854 cp_cv_quals cv_qualifiers,
3855 cp_declarator *target,
3856 tree attributes)
3858 if (code == ERROR_MARK || target == cp_error_declarator)
3859 return cp_error_declarator;
3861 if (code == INDIRECT_REF)
3862 if (class_type == NULL_TREE)
3863 return make_pointer_declarator (cv_qualifiers, target, attributes);
3864 else
3865 return make_ptrmem_declarator (cv_qualifiers, class_type,
3866 target, attributes);
3867 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3868 return make_reference_declarator (cv_qualifiers, target,
3869 false, attributes);
3870 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3871 return make_reference_declarator (cv_qualifiers, target,
3872 true, attributes);
3873 gcc_unreachable ();
3876 /* Create a new C++ parser. */
3878 static cp_parser *
3879 cp_parser_new (void)
3881 cp_parser *parser;
3882 cp_lexer *lexer;
3883 unsigned i;
3885 /* cp_lexer_new_main is called before doing GC allocation because
3886 cp_lexer_new_main might load a PCH file. */
3887 lexer = cp_lexer_new_main ();
3889 /* Initialize the binops_by_token so that we can get the tree
3890 directly from the token. */
3891 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3892 binops_by_token[binops[i].token_type] = binops[i];
3894 parser = ggc_cleared_alloc<cp_parser> ();
3895 parser->lexer = lexer;
3896 parser->context = cp_parser_context_new (NULL);
3898 /* For now, we always accept GNU extensions. */
3899 parser->allow_gnu_extensions_p = 1;
3901 /* The `>' token is a greater-than operator, not the end of a
3902 template-id. */
3903 parser->greater_than_is_operator_p = true;
3905 parser->default_arg_ok_p = true;
3907 /* We are not parsing a constant-expression. */
3908 parser->integral_constant_expression_p = false;
3909 parser->allow_non_integral_constant_expression_p = false;
3910 parser->non_integral_constant_expression_p = false;
3912 /* Local variable names are not forbidden. */
3913 parser->local_variables_forbidden_p = false;
3915 /* We are not processing an `extern "C"' declaration. */
3916 parser->in_unbraced_linkage_specification_p = false;
3918 /* We are not processing a declarator. */
3919 parser->in_declarator_p = false;
3921 /* We are not processing a template-argument-list. */
3922 parser->in_template_argument_list_p = false;
3924 /* We are not in an iteration statement. */
3925 parser->in_statement = 0;
3927 /* We are not in a switch statement. */
3928 parser->in_switch_statement_p = false;
3930 /* We are not parsing a type-id inside an expression. */
3931 parser->in_type_id_in_expr_p = false;
3933 /* Declarations aren't implicitly extern "C". */
3934 parser->implicit_extern_c = false;
3936 /* String literals should be translated to the execution character set. */
3937 parser->translate_strings_p = true;
3939 /* We are not parsing a function body. */
3940 parser->in_function_body = false;
3942 /* We can correct until told otherwise. */
3943 parser->colon_corrects_to_scope_p = true;
3945 /* The unparsed function queue is empty. */
3946 push_unparsed_function_queues (parser);
3948 /* There are no classes being defined. */
3949 parser->num_classes_being_defined = 0;
3951 /* No template parameters apply. */
3952 parser->num_template_parameter_lists = 0;
3954 /* Special parsing data structures. */
3955 parser->omp_declare_simd = NULL;
3956 parser->oacc_routine = NULL;
3958 /* Not declaring an implicit function template. */
3959 parser->auto_is_implicit_function_template_parm_p = false;
3960 parser->fully_implicit_function_template_p = false;
3961 parser->implicit_template_parms = 0;
3962 parser->implicit_template_scope = 0;
3964 /* Allow constrained-type-specifiers. */
3965 parser->prevent_constrained_type_specifiers = 0;
3967 /* We haven't yet seen an 'extern "C"'. */
3968 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3970 return parser;
3973 /* Create a cp_lexer structure which will emit the tokens in CACHE
3974 and push it onto the parser's lexer stack. This is used for delayed
3975 parsing of in-class method bodies and default arguments, and should
3976 not be confused with tentative parsing. */
3977 static void
3978 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3980 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3981 lexer->next = parser->lexer;
3982 parser->lexer = lexer;
3984 /* Move the current source position to that of the first token in the
3985 new lexer. */
3986 cp_lexer_set_source_position_from_token (lexer->next_token);
3989 /* Pop the top lexer off the parser stack. This is never used for the
3990 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3991 static void
3992 cp_parser_pop_lexer (cp_parser *parser)
3994 cp_lexer *lexer = parser->lexer;
3995 parser->lexer = lexer->next;
3996 cp_lexer_destroy (lexer);
3998 /* Put the current source position back where it was before this
3999 lexer was pushed. */
4000 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4003 /* Lexical conventions [gram.lex] */
4005 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4006 identifier. */
4008 static cp_expr
4009 cp_parser_identifier (cp_parser* parser)
4011 cp_token *token;
4013 /* Look for the identifier. */
4014 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4015 /* Return the value. */
4016 if (token)
4017 return cp_expr (token->u.value, token->location);
4018 else
4019 return error_mark_node;
4022 /* Parse a sequence of adjacent string constants. Returns a
4023 TREE_STRING representing the combined, nul-terminated string
4024 constant. If TRANSLATE is true, translate the string to the
4025 execution character set. If WIDE_OK is true, a wide string is
4026 invalid here.
4028 C++98 [lex.string] says that if a narrow string literal token is
4029 adjacent to a wide string literal token, the behavior is undefined.
4030 However, C99 6.4.5p4 says that this results in a wide string literal.
4031 We follow C99 here, for consistency with the C front end.
4033 This code is largely lifted from lex_string() in c-lex.c.
4035 FUTURE: ObjC++ will need to handle @-strings here. */
4036 static cp_expr
4037 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4038 bool lookup_udlit = true)
4040 tree value;
4041 size_t count;
4042 struct obstack str_ob;
4043 cpp_string str, istr, *strs;
4044 cp_token *tok;
4045 enum cpp_ttype type, curr_type;
4046 int have_suffix_p = 0;
4047 tree string_tree;
4048 tree suffix_id = NULL_TREE;
4049 bool curr_tok_is_userdef_p = false;
4051 tok = cp_lexer_peek_token (parser->lexer);
4052 if (!cp_parser_is_string_literal (tok))
4054 cp_parser_error (parser, "expected string-literal");
4055 return error_mark_node;
4058 location_t loc = tok->location;
4060 if (cpp_userdef_string_p (tok->type))
4062 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4063 curr_type = cpp_userdef_string_remove_type (tok->type);
4064 curr_tok_is_userdef_p = true;
4066 else
4068 string_tree = tok->u.value;
4069 curr_type = tok->type;
4071 type = curr_type;
4073 /* Try to avoid the overhead of creating and destroying an obstack
4074 for the common case of just one string. */
4075 if (!cp_parser_is_string_literal
4076 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4078 cp_lexer_consume_token (parser->lexer);
4080 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4081 str.len = TREE_STRING_LENGTH (string_tree);
4082 count = 1;
4084 if (curr_tok_is_userdef_p)
4086 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4087 have_suffix_p = 1;
4088 curr_type = cpp_userdef_string_remove_type (tok->type);
4090 else
4091 curr_type = tok->type;
4093 strs = &str;
4095 else
4097 location_t last_tok_loc = tok->location;
4098 gcc_obstack_init (&str_ob);
4099 count = 0;
4103 cp_lexer_consume_token (parser->lexer);
4104 count++;
4105 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4106 str.len = TREE_STRING_LENGTH (string_tree);
4108 if (curr_tok_is_userdef_p)
4110 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4111 if (have_suffix_p == 0)
4113 suffix_id = curr_suffix_id;
4114 have_suffix_p = 1;
4116 else if (have_suffix_p == 1
4117 && curr_suffix_id != suffix_id)
4119 error ("inconsistent user-defined literal suffixes"
4120 " %qD and %qD in string literal",
4121 suffix_id, curr_suffix_id);
4122 have_suffix_p = -1;
4124 curr_type = cpp_userdef_string_remove_type (tok->type);
4126 else
4127 curr_type = tok->type;
4129 if (type != curr_type)
4131 if (type == CPP_STRING)
4132 type = curr_type;
4133 else if (curr_type != CPP_STRING)
4135 rich_location rich_loc (line_table, tok->location);
4136 rich_loc.add_range (last_tok_loc, false);
4137 error_at (&rich_loc,
4138 "unsupported non-standard concatenation "
4139 "of string literals");
4143 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4145 last_tok_loc = tok->location;
4147 tok = cp_lexer_peek_token (parser->lexer);
4148 if (cpp_userdef_string_p (tok->type))
4150 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4151 curr_type = cpp_userdef_string_remove_type (tok->type);
4152 curr_tok_is_userdef_p = true;
4154 else
4156 string_tree = tok->u.value;
4157 curr_type = tok->type;
4158 curr_tok_is_userdef_p = false;
4161 while (cp_parser_is_string_literal (tok));
4163 /* A string literal built by concatenation has its caret=start at
4164 the start of the initial string, and its finish at the finish of
4165 the final string literal. */
4166 loc = make_location (loc, loc, get_finish (last_tok_loc));
4168 strs = (cpp_string *) obstack_finish (&str_ob);
4171 if (type != CPP_STRING && !wide_ok)
4173 cp_parser_error (parser, "a wide string is invalid in this context");
4174 type = CPP_STRING;
4177 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4178 (parse_in, strs, count, &istr, type))
4180 value = build_string (istr.len, (const char *)istr.text);
4181 free (CONST_CAST (unsigned char *, istr.text));
4183 switch (type)
4185 default:
4186 case CPP_STRING:
4187 case CPP_UTF8STRING:
4188 TREE_TYPE (value) = char_array_type_node;
4189 break;
4190 case CPP_STRING16:
4191 TREE_TYPE (value) = char16_array_type_node;
4192 break;
4193 case CPP_STRING32:
4194 TREE_TYPE (value) = char32_array_type_node;
4195 break;
4196 case CPP_WSTRING:
4197 TREE_TYPE (value) = wchar_array_type_node;
4198 break;
4201 value = fix_string_type (value);
4203 if (have_suffix_p)
4205 tree literal = build_userdef_literal (suffix_id, value,
4206 OT_NONE, NULL_TREE);
4207 if (lookup_udlit)
4208 value = cp_parser_userdef_string_literal (literal);
4209 else
4210 value = literal;
4213 else
4214 /* cpp_interpret_string has issued an error. */
4215 value = error_mark_node;
4217 if (count > 1)
4218 obstack_free (&str_ob, 0);
4220 return cp_expr (value, loc);
4223 /* Look up a literal operator with the name and the exact arguments. */
4225 static tree
4226 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4228 tree decl;
4229 decl = lookup_name (name);
4230 if (!decl || !is_overloaded_fn (decl))
4231 return error_mark_node;
4233 for (lkp_iterator iter (decl); iter; ++iter)
4235 unsigned int ix;
4236 bool found = true;
4237 tree fn = *iter;
4238 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4239 if (parmtypes != NULL_TREE)
4241 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4242 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4244 tree tparm = TREE_VALUE (parmtypes);
4245 tree targ = TREE_TYPE ((*args)[ix]);
4246 bool ptr = TYPE_PTR_P (tparm);
4247 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4248 if ((ptr || arr || !same_type_p (tparm, targ))
4249 && (!ptr || !arr
4250 || !same_type_p (TREE_TYPE (tparm),
4251 TREE_TYPE (targ))))
4252 found = false;
4254 if (found
4255 && ix == vec_safe_length (args)
4256 /* May be this should be sufficient_parms_p instead,
4257 depending on how exactly should user-defined literals
4258 work in presence of default arguments on the literal
4259 operator parameters. */
4260 && parmtypes == void_list_node)
4261 return decl;
4265 return error_mark_node;
4268 /* Parse a user-defined char constant. Returns a call to a user-defined
4269 literal operator taking the character as an argument. */
4271 static cp_expr
4272 cp_parser_userdef_char_literal (cp_parser *parser)
4274 cp_token *token = cp_lexer_consume_token (parser->lexer);
4275 tree literal = token->u.value;
4276 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4277 tree value = USERDEF_LITERAL_VALUE (literal);
4278 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4279 tree decl, result;
4281 /* Build up a call to the user-defined operator */
4282 /* Lookup the name we got back from the id-expression. */
4283 vec<tree, va_gc> *args = make_tree_vector ();
4284 vec_safe_push (args, value);
4285 decl = lookup_literal_operator (name, args);
4286 if (!decl || decl == error_mark_node)
4288 error ("unable to find character literal operator %qD with %qT argument",
4289 name, TREE_TYPE (value));
4290 release_tree_vector (args);
4291 return error_mark_node;
4293 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4294 release_tree_vector (args);
4295 return result;
4298 /* A subroutine of cp_parser_userdef_numeric_literal to
4299 create a char... template parameter pack from a string node. */
4301 static tree
4302 make_char_string_pack (tree value)
4304 tree charvec;
4305 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4306 const char *str = TREE_STRING_POINTER (value);
4307 int i, len = TREE_STRING_LENGTH (value) - 1;
4308 tree argvec = make_tree_vec (1);
4310 /* Fill in CHARVEC with all of the parameters. */
4311 charvec = make_tree_vec (len);
4312 for (i = 0; i < len; ++i)
4314 unsigned char s[3] = { '\'', str[i], '\'' };
4315 cpp_string in = { 3, s };
4316 cpp_string out = { 0, 0 };
4317 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4318 return NULL_TREE;
4319 gcc_assert (out.len == 2);
4320 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4321 out.text[0]);
4324 /* Build the argument packs. */
4325 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4327 TREE_VEC_ELT (argvec, 0) = argpack;
4329 return argvec;
4332 /* A subroutine of cp_parser_userdef_numeric_literal to
4333 create a char... template parameter pack from a string node. */
4335 static tree
4336 make_string_pack (tree value)
4338 tree charvec;
4339 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4340 const unsigned char *str
4341 = (const unsigned char *) TREE_STRING_POINTER (value);
4342 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4343 int len = TREE_STRING_LENGTH (value) / sz - 1;
4344 tree argvec = make_tree_vec (2);
4346 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4347 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4349 /* First template parm is character type. */
4350 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4352 /* Fill in CHARVEC with all of the parameters. */
4353 charvec = make_tree_vec (len);
4354 for (int i = 0; i < len; ++i)
4355 TREE_VEC_ELT (charvec, i)
4356 = double_int_to_tree (str_char_type_node,
4357 double_int::from_buffer (str + i * sz, sz));
4359 /* Build the argument packs. */
4360 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4362 TREE_VEC_ELT (argvec, 1) = argpack;
4364 return argvec;
4367 /* Parse a user-defined numeric constant. returns a call to a user-defined
4368 literal operator. */
4370 static cp_expr
4371 cp_parser_userdef_numeric_literal (cp_parser *parser)
4373 cp_token *token = cp_lexer_consume_token (parser->lexer);
4374 tree literal = token->u.value;
4375 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4376 tree value = USERDEF_LITERAL_VALUE (literal);
4377 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4378 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4379 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4380 tree decl, result;
4381 vec<tree, va_gc> *args;
4383 /* Look for a literal operator taking the exact type of numeric argument
4384 as the literal value. */
4385 args = make_tree_vector ();
4386 vec_safe_push (args, value);
4387 decl = lookup_literal_operator (name, args);
4388 if (decl && decl != error_mark_node)
4390 result = finish_call_expr (decl, &args, false, true,
4391 tf_warning_or_error);
4393 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4395 warning_at (token->location, OPT_Woverflow,
4396 "integer literal exceeds range of %qT type",
4397 long_long_unsigned_type_node);
4399 else
4401 if (overflow > 0)
4402 warning_at (token->location, OPT_Woverflow,
4403 "floating literal exceeds range of %qT type",
4404 long_double_type_node);
4405 else if (overflow < 0)
4406 warning_at (token->location, OPT_Woverflow,
4407 "floating literal truncated to zero");
4410 release_tree_vector (args);
4411 return result;
4413 release_tree_vector (args);
4415 /* If the numeric argument didn't work, look for a raw literal
4416 operator taking a const char* argument consisting of the number
4417 in string format. */
4418 args = make_tree_vector ();
4419 vec_safe_push (args, num_string);
4420 decl = lookup_literal_operator (name, args);
4421 if (decl && decl != error_mark_node)
4423 result = finish_call_expr (decl, &args, false, true,
4424 tf_warning_or_error);
4425 release_tree_vector (args);
4426 return result;
4428 release_tree_vector (args);
4430 /* If the raw literal didn't work, look for a non-type template
4431 function with parameter pack char.... Call the function with
4432 template parameter characters representing the number. */
4433 args = make_tree_vector ();
4434 decl = lookup_literal_operator (name, args);
4435 if (decl && decl != error_mark_node)
4437 tree tmpl_args = make_char_string_pack (num_string);
4438 if (tmpl_args == NULL_TREE)
4440 error ("failed to translate literal to execution character set %qT",
4441 num_string);
4442 return error_mark_node;
4444 decl = lookup_template_function (decl, tmpl_args);
4445 result = finish_call_expr (decl, &args, false, true,
4446 tf_warning_or_error);
4447 release_tree_vector (args);
4448 return result;
4451 release_tree_vector (args);
4453 /* In C++14 the standard library defines complex number suffixes that
4454 conflict with GNU extensions. Prefer them if <complex> is #included. */
4455 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4456 bool i14 = (cxx_dialect > cxx11
4457 && (id_equal (suffix_id, "i")
4458 || id_equal (suffix_id, "if")
4459 || id_equal (suffix_id, "il")));
4460 diagnostic_t kind = DK_ERROR;
4461 int opt = 0;
4463 if (i14 && ext)
4465 tree cxlit = lookup_qualified_name (std_node,
4466 get_identifier ("complex_literals"),
4467 0, false, false);
4468 if (cxlit == error_mark_node)
4470 /* No <complex>, so pedwarn and use GNU semantics. */
4471 kind = DK_PEDWARN;
4472 opt = OPT_Wpedantic;
4476 bool complained
4477 = emit_diagnostic (kind, input_location, opt,
4478 "unable to find numeric literal operator %qD", name);
4480 if (!complained)
4481 /* Don't inform either. */;
4482 else if (i14)
4484 inform (token->location, "add %<using namespace std::complex_literals%> "
4485 "(from <complex>) to enable the C++14 user-defined literal "
4486 "suffixes");
4487 if (ext)
4488 inform (token->location, "or use %<j%> instead of %<i%> for the "
4489 "GNU built-in suffix");
4491 else if (!ext)
4492 inform (token->location, "use -fext-numeric-literals "
4493 "to enable more built-in suffixes");
4495 if (kind == DK_ERROR)
4496 value = error_mark_node;
4497 else
4499 /* Use the built-in semantics. */
4500 tree type;
4501 if (id_equal (suffix_id, "i"))
4503 if (TREE_CODE (value) == INTEGER_CST)
4504 type = integer_type_node;
4505 else
4506 type = double_type_node;
4508 else if (id_equal (suffix_id, "if"))
4509 type = float_type_node;
4510 else /* if (id_equal (suffix_id, "il")) */
4511 type = long_double_type_node;
4513 value = build_complex (build_complex_type (type),
4514 fold_convert (type, integer_zero_node),
4515 fold_convert (type, value));
4518 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4519 /* Avoid repeated diagnostics. */
4520 token->u.value = value;
4521 return value;
4524 /* Parse a user-defined string constant. Returns a call to a user-defined
4525 literal operator taking a character pointer and the length of the string
4526 as arguments. */
4528 static tree
4529 cp_parser_userdef_string_literal (tree literal)
4531 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4532 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4533 tree value = USERDEF_LITERAL_VALUE (literal);
4534 int len = TREE_STRING_LENGTH (value)
4535 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4536 tree decl, result;
4537 vec<tree, va_gc> *args;
4539 /* Build up a call to the user-defined operator. */
4540 /* Lookup the name we got back from the id-expression. */
4541 args = make_tree_vector ();
4542 vec_safe_push (args, value);
4543 vec_safe_push (args, build_int_cst (size_type_node, len));
4544 decl = lookup_literal_operator (name, args);
4546 if (decl && decl != error_mark_node)
4548 result = finish_call_expr (decl, &args, false, true,
4549 tf_warning_or_error);
4550 release_tree_vector (args);
4551 return result;
4553 release_tree_vector (args);
4555 /* Look for a template function with typename parameter CharT
4556 and parameter pack CharT... Call the function with
4557 template parameter characters representing the string. */
4558 args = make_tree_vector ();
4559 decl = lookup_literal_operator (name, args);
4560 if (decl && decl != error_mark_node)
4562 tree tmpl_args = make_string_pack (value);
4563 decl = lookup_template_function (decl, tmpl_args);
4564 result = finish_call_expr (decl, &args, false, true,
4565 tf_warning_or_error);
4566 release_tree_vector (args);
4567 return result;
4569 release_tree_vector (args);
4571 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4572 name, TREE_TYPE (value), size_type_node);
4573 return error_mark_node;
4577 /* Basic concepts [gram.basic] */
4579 /* Parse a translation-unit.
4581 translation-unit:
4582 declaration-seq [opt]
4584 Returns TRUE if all went well. */
4586 static bool
4587 cp_parser_translation_unit (cp_parser* parser)
4589 /* The address of the first non-permanent object on the declarator
4590 obstack. */
4591 static void *declarator_obstack_base;
4593 bool success;
4595 /* Create the declarator obstack, if necessary. */
4596 if (!cp_error_declarator)
4598 gcc_obstack_init (&declarator_obstack);
4599 /* Create the error declarator. */
4600 cp_error_declarator = make_declarator (cdk_error);
4601 /* Create the empty parameter list. */
4602 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4603 UNKNOWN_LOCATION);
4604 /* Remember where the base of the declarator obstack lies. */
4605 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4608 cp_parser_declaration_seq_opt (parser);
4610 /* If there are no tokens left then all went well. */
4611 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4613 /* Get rid of the token array; we don't need it any more. */
4614 cp_lexer_destroy (parser->lexer);
4615 parser->lexer = NULL;
4617 /* This file might have been a context that's implicitly extern
4618 "C". If so, pop the lang context. (Only relevant for PCH.) */
4619 if (parser->implicit_extern_c)
4621 pop_lang_context ();
4622 parser->implicit_extern_c = false;
4625 /* Finish up. */
4626 finish_translation_unit ();
4628 success = true;
4630 else
4632 cp_parser_error (parser, "expected declaration");
4633 success = false;
4636 /* Make sure the declarator obstack was fully cleaned up. */
4637 gcc_assert (obstack_next_free (&declarator_obstack)
4638 == declarator_obstack_base);
4640 /* All went well. */
4641 return success;
4644 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4645 decltype context. */
4647 static inline tsubst_flags_t
4648 complain_flags (bool decltype_p)
4650 tsubst_flags_t complain = tf_warning_or_error;
4651 if (decltype_p)
4652 complain |= tf_decltype;
4653 return complain;
4656 /* We're about to parse a collection of statements. If we're currently
4657 parsing tentatively, set up a firewall so that any nested
4658 cp_parser_commit_to_tentative_parse won't affect the current context. */
4660 static cp_token_position
4661 cp_parser_start_tentative_firewall (cp_parser *parser)
4663 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4664 return 0;
4666 cp_parser_parse_tentatively (parser);
4667 cp_parser_commit_to_topmost_tentative_parse (parser);
4668 return cp_lexer_token_position (parser->lexer, false);
4671 /* We've finished parsing the collection of statements. Wrap up the
4672 firewall and replace the relevant tokens with the parsed form. */
4674 static void
4675 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4676 tree expr)
4678 if (!start)
4679 return;
4681 /* Finish the firewall level. */
4682 cp_parser_parse_definitely (parser);
4683 /* And remember the result of the parse for when we try again. */
4684 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4685 token->type = CPP_PREPARSED_EXPR;
4686 token->u.value = expr;
4687 token->keyword = RID_MAX;
4688 cp_lexer_purge_tokens_after (parser->lexer, start);
4691 /* Like the above functions, but let the user modify the tokens. Used by
4692 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4693 later parses, so it makes sense to localize the effects of
4694 cp_parser_commit_to_tentative_parse. */
4696 struct tentative_firewall
4698 cp_parser *parser;
4699 bool set;
4701 tentative_firewall (cp_parser *p): parser(p)
4703 /* If we're currently parsing tentatively, start a committed level as a
4704 firewall and then an inner tentative parse. */
4705 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4707 cp_parser_parse_tentatively (parser);
4708 cp_parser_commit_to_topmost_tentative_parse (parser);
4709 cp_parser_parse_tentatively (parser);
4713 ~tentative_firewall()
4715 if (set)
4717 /* Finish the inner tentative parse and the firewall, propagating any
4718 uncommitted error state to the outer tentative parse. */
4719 bool err = cp_parser_error_occurred (parser);
4720 cp_parser_parse_definitely (parser);
4721 cp_parser_parse_definitely (parser);
4722 if (err)
4723 cp_parser_simulate_error (parser);
4728 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4729 This class is for tracking such a matching pair of symbols.
4730 In particular, it tracks the location of the first token,
4731 so that if the second token is missing, we can highlight the
4732 location of the first token when notifying the user about the
4733 problem. */
4735 template <typename traits_t>
4736 class token_pair
4738 public:
4739 /* token_pair's ctor. */
4740 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4742 /* If the next token is the opening symbol for this pair, consume it and
4743 return true.
4744 Otherwise, issue an error and return false.
4745 In either case, record the location of the opening token. */
4747 bool require_open (cp_parser *parser)
4749 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4750 return cp_parser_require (parser, traits_t::open_token_type,
4751 traits_t::required_token_open);
4754 /* Consume the next token from PARSER, recording its location as
4755 that of the opening token within the pair. */
4757 cp_token * consume_open (cp_parser *parser)
4759 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4760 gcc_assert (tok->type == traits_t::open_token_type);
4761 m_open_loc = tok->location;
4762 return tok;
4765 /* If the next token is the closing symbol for this pair, consume it
4766 and return it.
4767 Otherwise, issue an error, highlighting the location of the
4768 corresponding opening token, and return NULL. */
4770 cp_token *require_close (cp_parser *parser) const
4772 return cp_parser_require (parser, traits_t::close_token_type,
4773 traits_t::required_token_close,
4774 m_open_loc);
4777 private:
4778 location_t m_open_loc;
4781 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4783 struct matching_paren_traits
4785 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4786 static const enum required_token required_token_open = RT_OPEN_PAREN;
4787 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4788 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4791 /* "matching_parens" is a token_pair<T> class for tracking matching
4792 pairs of parentheses. */
4794 typedef token_pair<matching_paren_traits> matching_parens;
4796 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4798 struct matching_brace_traits
4800 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4801 static const enum required_token required_token_open = RT_OPEN_BRACE;
4802 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4803 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4806 /* "matching_braces" is a token_pair<T> class for tracking matching
4807 pairs of braces. */
4809 typedef token_pair<matching_brace_traits> matching_braces;
4812 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4813 enclosing parentheses. */
4815 static cp_expr
4816 cp_parser_statement_expr (cp_parser *parser)
4818 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4820 /* Consume the '('. */
4821 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4822 matching_parens parens;
4823 parens.consume_open (parser);
4824 /* Start the statement-expression. */
4825 tree expr = begin_stmt_expr ();
4826 /* Parse the compound-statement. */
4827 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4828 /* Finish up. */
4829 expr = finish_stmt_expr (expr, false);
4830 /* Consume the ')'. */
4831 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4832 if (!parens.require_close (parser))
4833 cp_parser_skip_to_end_of_statement (parser);
4835 cp_parser_end_tentative_firewall (parser, start, expr);
4836 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4837 return cp_expr (expr, combined_loc);
4840 /* Expressions [gram.expr] */
4842 /* Parse a fold-operator.
4844 fold-operator:
4845 - * / % ^ & | = < > << >>
4846 = -= *= /= %= ^= &= |= <<= >>=
4847 == != <= >= && || , .* ->*
4849 This returns the tree code corresponding to the matched operator
4850 as an int. When the current token matches a compound assignment
4851 opertor, the resulting tree code is the negative value of the
4852 non-assignment operator. */
4854 static int
4855 cp_parser_fold_operator (cp_token *token)
4857 switch (token->type)
4859 case CPP_PLUS: return PLUS_EXPR;
4860 case CPP_MINUS: return MINUS_EXPR;
4861 case CPP_MULT: return MULT_EXPR;
4862 case CPP_DIV: return TRUNC_DIV_EXPR;
4863 case CPP_MOD: return TRUNC_MOD_EXPR;
4864 case CPP_XOR: return BIT_XOR_EXPR;
4865 case CPP_AND: return BIT_AND_EXPR;
4866 case CPP_OR: return BIT_IOR_EXPR;
4867 case CPP_LSHIFT: return LSHIFT_EXPR;
4868 case CPP_RSHIFT: return RSHIFT_EXPR;
4870 case CPP_EQ: return -NOP_EXPR;
4871 case CPP_PLUS_EQ: return -PLUS_EXPR;
4872 case CPP_MINUS_EQ: return -MINUS_EXPR;
4873 case CPP_MULT_EQ: return -MULT_EXPR;
4874 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4875 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4876 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4877 case CPP_AND_EQ: return -BIT_AND_EXPR;
4878 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4879 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4880 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4882 case CPP_EQ_EQ: return EQ_EXPR;
4883 case CPP_NOT_EQ: return NE_EXPR;
4884 case CPP_LESS: return LT_EXPR;
4885 case CPP_GREATER: return GT_EXPR;
4886 case CPP_LESS_EQ: return LE_EXPR;
4887 case CPP_GREATER_EQ: return GE_EXPR;
4889 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4890 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4892 case CPP_COMMA: return COMPOUND_EXPR;
4894 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4895 case CPP_DEREF_STAR: return MEMBER_REF;
4897 default: return ERROR_MARK;
4901 /* Returns true if CODE indicates a binary expression, which is not allowed in
4902 the LHS of a fold-expression. More codes will need to be added to use this
4903 function in other contexts. */
4905 static bool
4906 is_binary_op (tree_code code)
4908 switch (code)
4910 case PLUS_EXPR:
4911 case POINTER_PLUS_EXPR:
4912 case MINUS_EXPR:
4913 case MULT_EXPR:
4914 case TRUNC_DIV_EXPR:
4915 case TRUNC_MOD_EXPR:
4916 case BIT_XOR_EXPR:
4917 case BIT_AND_EXPR:
4918 case BIT_IOR_EXPR:
4919 case LSHIFT_EXPR:
4920 case RSHIFT_EXPR:
4922 case MODOP_EXPR:
4924 case EQ_EXPR:
4925 case NE_EXPR:
4926 case LE_EXPR:
4927 case GE_EXPR:
4928 case LT_EXPR:
4929 case GT_EXPR:
4931 case TRUTH_ANDIF_EXPR:
4932 case TRUTH_ORIF_EXPR:
4934 case COMPOUND_EXPR:
4936 case DOTSTAR_EXPR:
4937 case MEMBER_REF:
4938 return true;
4940 default:
4941 return false;
4945 /* If the next token is a suitable fold operator, consume it and return as
4946 the function above. */
4948 static int
4949 cp_parser_fold_operator (cp_parser *parser)
4951 cp_token* token = cp_lexer_peek_token (parser->lexer);
4952 int code = cp_parser_fold_operator (token);
4953 if (code != ERROR_MARK)
4954 cp_lexer_consume_token (parser->lexer);
4955 return code;
4958 /* Parse a fold-expression.
4960 fold-expression:
4961 ( ... folding-operator cast-expression)
4962 ( cast-expression folding-operator ... )
4963 ( cast-expression folding operator ... folding-operator cast-expression)
4965 Note that the '(' and ')' are matched in primary expression. */
4967 static cp_expr
4968 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4970 cp_id_kind pidk;
4972 // Left fold.
4973 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4975 cp_lexer_consume_token (parser->lexer);
4976 int op = cp_parser_fold_operator (parser);
4977 if (op == ERROR_MARK)
4979 cp_parser_error (parser, "expected binary operator");
4980 return error_mark_node;
4983 tree expr = cp_parser_cast_expression (parser, false, false,
4984 false, &pidk);
4985 if (expr == error_mark_node)
4986 return error_mark_node;
4987 return finish_left_unary_fold_expr (expr, op);
4990 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4991 int op = cp_parser_fold_operator (parser);
4992 if (op == ERROR_MARK)
4994 cp_parser_error (parser, "expected binary operator");
4995 return error_mark_node;
4998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5000 cp_parser_error (parser, "expected ...");
5001 return error_mark_node;
5003 cp_lexer_consume_token (parser->lexer);
5005 /* The operands of a fold-expression are cast-expressions, so binary or
5006 conditional expressions are not allowed. We check this here to avoid
5007 tentative parsing. */
5008 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5009 /* OK, the expression was parenthesized. */;
5010 else if (is_binary_op (TREE_CODE (expr1)))
5011 error_at (location_of (expr1),
5012 "binary expression in operand of fold-expression");
5013 else if (TREE_CODE (expr1) == COND_EXPR
5014 || (REFERENCE_REF_P (expr1)
5015 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5016 error_at (location_of (expr1),
5017 "conditional expression in operand of fold-expression");
5019 // Right fold.
5020 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5021 return finish_right_unary_fold_expr (expr1, op);
5023 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5025 cp_parser_error (parser, "mismatched operator in fold-expression");
5026 return error_mark_node;
5028 cp_lexer_consume_token (parser->lexer);
5030 // Binary left or right fold.
5031 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5032 if (expr2 == error_mark_node)
5033 return error_mark_node;
5034 return finish_binary_fold_expr (expr1, expr2, op);
5037 /* Parse a primary-expression.
5039 primary-expression:
5040 literal
5041 this
5042 ( expression )
5043 id-expression
5044 lambda-expression (C++11)
5046 GNU Extensions:
5048 primary-expression:
5049 ( compound-statement )
5050 __builtin_va_arg ( assignment-expression , type-id )
5051 __builtin_offsetof ( type-id , offsetof-expression )
5053 C++ Extensions:
5054 __has_nothrow_assign ( type-id )
5055 __has_nothrow_constructor ( type-id )
5056 __has_nothrow_copy ( type-id )
5057 __has_trivial_assign ( type-id )
5058 __has_trivial_constructor ( type-id )
5059 __has_trivial_copy ( type-id )
5060 __has_trivial_destructor ( type-id )
5061 __has_virtual_destructor ( type-id )
5062 __is_abstract ( type-id )
5063 __is_base_of ( type-id , type-id )
5064 __is_class ( type-id )
5065 __is_empty ( type-id )
5066 __is_enum ( type-id )
5067 __is_final ( type-id )
5068 __is_literal_type ( type-id )
5069 __is_pod ( type-id )
5070 __is_polymorphic ( type-id )
5071 __is_std_layout ( type-id )
5072 __is_trivial ( type-id )
5073 __is_union ( type-id )
5075 Objective-C++ Extension:
5077 primary-expression:
5078 objc-expression
5080 literal:
5081 __null
5083 ADDRESS_P is true iff this expression was immediately preceded by
5084 "&" and therefore might denote a pointer-to-member. CAST_P is true
5085 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5086 true iff this expression is a template argument.
5088 Returns a representation of the expression. Upon return, *IDK
5089 indicates what kind of id-expression (if any) was present. */
5091 static cp_expr
5092 cp_parser_primary_expression (cp_parser *parser,
5093 bool address_p,
5094 bool cast_p,
5095 bool template_arg_p,
5096 bool decltype_p,
5097 cp_id_kind *idk)
5099 cp_token *token = NULL;
5101 /* Assume the primary expression is not an id-expression. */
5102 *idk = CP_ID_KIND_NONE;
5104 /* Peek at the next token. */
5105 token = cp_lexer_peek_token (parser->lexer);
5106 switch ((int) token->type)
5108 /* literal:
5109 integer-literal
5110 character-literal
5111 floating-literal
5112 string-literal
5113 boolean-literal
5114 pointer-literal
5115 user-defined-literal */
5116 case CPP_CHAR:
5117 case CPP_CHAR16:
5118 case CPP_CHAR32:
5119 case CPP_WCHAR:
5120 case CPP_UTF8CHAR:
5121 case CPP_NUMBER:
5122 case CPP_PREPARSED_EXPR:
5123 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5124 return cp_parser_userdef_numeric_literal (parser);
5125 token = cp_lexer_consume_token (parser->lexer);
5126 if (TREE_CODE (token->u.value) == FIXED_CST)
5128 error_at (token->location,
5129 "fixed-point types not supported in C++");
5130 return error_mark_node;
5132 /* Floating-point literals are only allowed in an integral
5133 constant expression if they are cast to an integral or
5134 enumeration type. */
5135 if (TREE_CODE (token->u.value) == REAL_CST
5136 && parser->integral_constant_expression_p
5137 && pedantic)
5139 /* CAST_P will be set even in invalid code like "int(2.7 +
5140 ...)". Therefore, we have to check that the next token
5141 is sure to end the cast. */
5142 if (cast_p)
5144 cp_token *next_token;
5146 next_token = cp_lexer_peek_token (parser->lexer);
5147 if (/* The comma at the end of an
5148 enumerator-definition. */
5149 next_token->type != CPP_COMMA
5150 /* The curly brace at the end of an enum-specifier. */
5151 && next_token->type != CPP_CLOSE_BRACE
5152 /* The end of a statement. */
5153 && next_token->type != CPP_SEMICOLON
5154 /* The end of the cast-expression. */
5155 && next_token->type != CPP_CLOSE_PAREN
5156 /* The end of an array bound. */
5157 && next_token->type != CPP_CLOSE_SQUARE
5158 /* The closing ">" in a template-argument-list. */
5159 && (next_token->type != CPP_GREATER
5160 || parser->greater_than_is_operator_p)
5161 /* C++0x only: A ">>" treated like two ">" tokens,
5162 in a template-argument-list. */
5163 && (next_token->type != CPP_RSHIFT
5164 || (cxx_dialect == cxx98)
5165 || parser->greater_than_is_operator_p))
5166 cast_p = false;
5169 /* If we are within a cast, then the constraint that the
5170 cast is to an integral or enumeration type will be
5171 checked at that point. If we are not within a cast, then
5172 this code is invalid. */
5173 if (!cast_p)
5174 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5176 return cp_expr (token->u.value, token->location);
5178 case CPP_CHAR_USERDEF:
5179 case CPP_CHAR16_USERDEF:
5180 case CPP_CHAR32_USERDEF:
5181 case CPP_WCHAR_USERDEF:
5182 case CPP_UTF8CHAR_USERDEF:
5183 return cp_parser_userdef_char_literal (parser);
5185 case CPP_STRING:
5186 case CPP_STRING16:
5187 case CPP_STRING32:
5188 case CPP_WSTRING:
5189 case CPP_UTF8STRING:
5190 case CPP_STRING_USERDEF:
5191 case CPP_STRING16_USERDEF:
5192 case CPP_STRING32_USERDEF:
5193 case CPP_WSTRING_USERDEF:
5194 case CPP_UTF8STRING_USERDEF:
5195 /* ??? Should wide strings be allowed when parser->translate_strings_p
5196 is false (i.e. in attributes)? If not, we can kill the third
5197 argument to cp_parser_string_literal. */
5198 return cp_parser_string_literal (parser,
5199 parser->translate_strings_p,
5200 true);
5202 case CPP_OPEN_PAREN:
5203 /* If we see `( { ' then we are looking at the beginning of
5204 a GNU statement-expression. */
5205 if (cp_parser_allow_gnu_extensions_p (parser)
5206 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5208 /* Statement-expressions are not allowed by the standard. */
5209 pedwarn (token->location, OPT_Wpedantic,
5210 "ISO C++ forbids braced-groups within expressions");
5212 /* And they're not allowed outside of a function-body; you
5213 cannot, for example, write:
5215 int i = ({ int j = 3; j + 1; });
5217 at class or namespace scope. */
5218 if (!parser->in_function_body
5219 || parser->in_template_argument_list_p)
5221 error_at (token->location,
5222 "statement-expressions are not allowed outside "
5223 "functions nor in template-argument lists");
5224 cp_parser_skip_to_end_of_block_or_statement (parser);
5225 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5226 cp_lexer_consume_token (parser->lexer);
5227 return error_mark_node;
5229 else
5230 return cp_parser_statement_expr (parser);
5232 /* Otherwise it's a normal parenthesized expression. */
5234 cp_expr expr;
5235 bool saved_greater_than_is_operator_p;
5237 location_t open_paren_loc = token->location;
5239 /* Consume the `('. */
5240 matching_parens parens;
5241 parens.consume_open (parser);
5242 /* Within a parenthesized expression, a `>' token is always
5243 the greater-than operator. */
5244 saved_greater_than_is_operator_p
5245 = parser->greater_than_is_operator_p;
5246 parser->greater_than_is_operator_p = true;
5248 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5249 /* Left fold expression. */
5250 expr = NULL_TREE;
5251 else
5252 /* Parse the parenthesized expression. */
5253 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5255 token = cp_lexer_peek_token (parser->lexer);
5256 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5258 expr = cp_parser_fold_expression (parser, expr);
5259 if (expr != error_mark_node
5260 && cxx_dialect < cxx17
5261 && !in_system_header_at (input_location))
5262 pedwarn (input_location, 0, "fold-expressions only available "
5263 "with -std=c++17 or -std=gnu++17");
5265 else
5266 /* Let the front end know that this expression was
5267 enclosed in parentheses. This matters in case, for
5268 example, the expression is of the form `A::B', since
5269 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5270 not. */
5271 expr = finish_parenthesized_expr (expr);
5273 /* DR 705: Wrapping an unqualified name in parentheses
5274 suppresses arg-dependent lookup. We want to pass back
5275 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5276 (c++/37862), but none of the others. */
5277 if (*idk != CP_ID_KIND_QUALIFIED)
5278 *idk = CP_ID_KIND_NONE;
5280 /* The `>' token might be the end of a template-id or
5281 template-parameter-list now. */
5282 parser->greater_than_is_operator_p
5283 = saved_greater_than_is_operator_p;
5285 /* Consume the `)'. */
5286 token = cp_lexer_peek_token (parser->lexer);
5287 location_t close_paren_loc = token->location;
5288 expr.set_range (open_paren_loc, close_paren_loc);
5289 if (!parens.require_close (parser)
5290 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5291 cp_parser_skip_to_end_of_statement (parser);
5293 return expr;
5296 case CPP_OPEN_SQUARE:
5298 if (c_dialect_objc ())
5300 /* We might have an Objective-C++ message. */
5301 cp_parser_parse_tentatively (parser);
5302 tree msg = cp_parser_objc_message_expression (parser);
5303 /* If that works out, we're done ... */
5304 if (cp_parser_parse_definitely (parser))
5305 return msg;
5306 /* ... else, fall though to see if it's a lambda. */
5308 cp_expr lam = cp_parser_lambda_expression (parser);
5309 /* Don't warn about a failed tentative parse. */
5310 if (cp_parser_error_occurred (parser))
5311 return error_mark_node;
5312 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5313 return lam;
5316 case CPP_OBJC_STRING:
5317 if (c_dialect_objc ())
5318 /* We have an Objective-C++ string literal. */
5319 return cp_parser_objc_expression (parser);
5320 cp_parser_error (parser, "expected primary-expression");
5321 return error_mark_node;
5323 case CPP_KEYWORD:
5324 switch (token->keyword)
5326 /* These two are the boolean literals. */
5327 case RID_TRUE:
5328 cp_lexer_consume_token (parser->lexer);
5329 return cp_expr (boolean_true_node, token->location);
5330 case RID_FALSE:
5331 cp_lexer_consume_token (parser->lexer);
5332 return cp_expr (boolean_false_node, token->location);
5334 /* The `__null' literal. */
5335 case RID_NULL:
5336 cp_lexer_consume_token (parser->lexer);
5337 return cp_expr (null_node, token->location);
5339 /* The `nullptr' literal. */
5340 case RID_NULLPTR:
5341 cp_lexer_consume_token (parser->lexer);
5342 return cp_expr (nullptr_node, token->location);
5344 /* Recognize the `this' keyword. */
5345 case RID_THIS:
5346 cp_lexer_consume_token (parser->lexer);
5347 if (parser->local_variables_forbidden_p)
5349 error_at (token->location,
5350 "%<this%> may not be used in this context");
5351 return error_mark_node;
5353 /* Pointers cannot appear in constant-expressions. */
5354 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5355 return error_mark_node;
5356 return cp_expr (finish_this_expr (), token->location);
5358 /* The `operator' keyword can be the beginning of an
5359 id-expression. */
5360 case RID_OPERATOR:
5361 goto id_expression;
5363 case RID_FUNCTION_NAME:
5364 case RID_PRETTY_FUNCTION_NAME:
5365 case RID_C99_FUNCTION_NAME:
5367 non_integral_constant name;
5369 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5370 __func__ are the names of variables -- but they are
5371 treated specially. Therefore, they are handled here,
5372 rather than relying on the generic id-expression logic
5373 below. Grammatically, these names are id-expressions.
5375 Consume the token. */
5376 token = cp_lexer_consume_token (parser->lexer);
5378 switch (token->keyword)
5380 case RID_FUNCTION_NAME:
5381 name = NIC_FUNC_NAME;
5382 break;
5383 case RID_PRETTY_FUNCTION_NAME:
5384 name = NIC_PRETTY_FUNC;
5385 break;
5386 case RID_C99_FUNCTION_NAME:
5387 name = NIC_C99_FUNC;
5388 break;
5389 default:
5390 gcc_unreachable ();
5393 if (cp_parser_non_integral_constant_expression (parser, name))
5394 return error_mark_node;
5396 /* Look up the name. */
5397 return finish_fname (token->u.value);
5400 case RID_VA_ARG:
5402 tree expression;
5403 tree type;
5404 source_location type_location;
5405 location_t start_loc
5406 = cp_lexer_peek_token (parser->lexer)->location;
5407 /* The `__builtin_va_arg' construct is used to handle
5408 `va_arg'. Consume the `__builtin_va_arg' token. */
5409 cp_lexer_consume_token (parser->lexer);
5410 /* Look for the opening `('. */
5411 matching_parens parens;
5412 parens.require_open (parser);
5413 /* Now, parse the assignment-expression. */
5414 expression = cp_parser_assignment_expression (parser);
5415 /* Look for the `,'. */
5416 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5417 type_location = cp_lexer_peek_token (parser->lexer)->location;
5418 /* Parse the type-id. */
5420 type_id_in_expr_sentinel s (parser);
5421 type = cp_parser_type_id (parser);
5423 /* Look for the closing `)'. */
5424 location_t finish_loc
5425 = cp_lexer_peek_token (parser->lexer)->location;
5426 parens.require_close (parser);
5427 /* Using `va_arg' in a constant-expression is not
5428 allowed. */
5429 if (cp_parser_non_integral_constant_expression (parser,
5430 NIC_VA_ARG))
5431 return error_mark_node;
5432 /* Construct a location of the form:
5433 __builtin_va_arg (v, int)
5434 ~~~~~~~~~~~~~~~~~~~~~^~~~
5435 with the caret at the type, ranging from the start of the
5436 "__builtin_va_arg" token to the close paren. */
5437 location_t combined_loc
5438 = make_location (type_location, start_loc, finish_loc);
5439 return build_x_va_arg (combined_loc, expression, type);
5442 case RID_OFFSETOF:
5443 return cp_parser_builtin_offsetof (parser);
5445 case RID_HAS_NOTHROW_ASSIGN:
5446 case RID_HAS_NOTHROW_CONSTRUCTOR:
5447 case RID_HAS_NOTHROW_COPY:
5448 case RID_HAS_TRIVIAL_ASSIGN:
5449 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5450 case RID_HAS_TRIVIAL_COPY:
5451 case RID_HAS_TRIVIAL_DESTRUCTOR:
5452 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5453 case RID_HAS_VIRTUAL_DESTRUCTOR:
5454 case RID_IS_ABSTRACT:
5455 case RID_IS_AGGREGATE:
5456 case RID_IS_BASE_OF:
5457 case RID_IS_CLASS:
5458 case RID_IS_EMPTY:
5459 case RID_IS_ENUM:
5460 case RID_IS_FINAL:
5461 case RID_IS_LITERAL_TYPE:
5462 case RID_IS_POD:
5463 case RID_IS_POLYMORPHIC:
5464 case RID_IS_SAME_AS:
5465 case RID_IS_STD_LAYOUT:
5466 case RID_IS_TRIVIAL:
5467 case RID_IS_TRIVIALLY_ASSIGNABLE:
5468 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5469 case RID_IS_TRIVIALLY_COPYABLE:
5470 case RID_IS_UNION:
5471 case RID_IS_ASSIGNABLE:
5472 case RID_IS_CONSTRUCTIBLE:
5473 return cp_parser_trait_expr (parser, token->keyword);
5475 // C++ concepts
5476 case RID_REQUIRES:
5477 return cp_parser_requires_expression (parser);
5479 /* Objective-C++ expressions. */
5480 case RID_AT_ENCODE:
5481 case RID_AT_PROTOCOL:
5482 case RID_AT_SELECTOR:
5483 return cp_parser_objc_expression (parser);
5485 case RID_TEMPLATE:
5486 if (parser->in_function_body
5487 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5488 == CPP_LESS))
5490 error_at (token->location,
5491 "a template declaration cannot appear at block scope");
5492 cp_parser_skip_to_end_of_block_or_statement (parser);
5493 return error_mark_node;
5495 /* FALLTHRU */
5496 default:
5497 cp_parser_error (parser, "expected primary-expression");
5498 return error_mark_node;
5501 /* An id-expression can start with either an identifier, a
5502 `::' as the beginning of a qualified-id, or the "operator"
5503 keyword. */
5504 case CPP_NAME:
5505 case CPP_SCOPE:
5506 case CPP_TEMPLATE_ID:
5507 case CPP_NESTED_NAME_SPECIFIER:
5509 id_expression:
5510 cp_expr id_expression;
5511 cp_expr decl;
5512 const char *error_msg;
5513 bool template_p;
5514 bool done;
5515 cp_token *id_expr_token;
5517 /* Parse the id-expression. */
5518 id_expression
5519 = cp_parser_id_expression (parser,
5520 /*template_keyword_p=*/false,
5521 /*check_dependency_p=*/true,
5522 &template_p,
5523 /*declarator_p=*/false,
5524 /*optional_p=*/false);
5525 if (id_expression == error_mark_node)
5526 return error_mark_node;
5527 id_expr_token = token;
5528 token = cp_lexer_peek_token (parser->lexer);
5529 done = (token->type != CPP_OPEN_SQUARE
5530 && token->type != CPP_OPEN_PAREN
5531 && token->type != CPP_DOT
5532 && token->type != CPP_DEREF
5533 && token->type != CPP_PLUS_PLUS
5534 && token->type != CPP_MINUS_MINUS);
5535 /* If we have a template-id, then no further lookup is
5536 required. If the template-id was for a template-class, we
5537 will sometimes have a TYPE_DECL at this point. */
5538 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5539 || TREE_CODE (id_expression) == TYPE_DECL)
5540 decl = id_expression;
5541 /* Look up the name. */
5542 else
5544 tree ambiguous_decls;
5546 /* If we already know that this lookup is ambiguous, then
5547 we've already issued an error message; there's no reason
5548 to check again. */
5549 if (id_expr_token->type == CPP_NAME
5550 && id_expr_token->error_reported)
5552 cp_parser_simulate_error (parser);
5553 return error_mark_node;
5556 decl = cp_parser_lookup_name (parser, id_expression,
5557 none_type,
5558 template_p,
5559 /*is_namespace=*/false,
5560 /*check_dependency=*/true,
5561 &ambiguous_decls,
5562 id_expr_token->location);
5563 /* If the lookup was ambiguous, an error will already have
5564 been issued. */
5565 if (ambiguous_decls)
5566 return error_mark_node;
5568 /* In Objective-C++, we may have an Objective-C 2.0
5569 dot-syntax for classes here. */
5570 if (c_dialect_objc ()
5571 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5572 && TREE_CODE (decl) == TYPE_DECL
5573 && objc_is_class_name (decl))
5575 tree component;
5576 cp_lexer_consume_token (parser->lexer);
5577 component = cp_parser_identifier (parser);
5578 if (component == error_mark_node)
5579 return error_mark_node;
5581 tree result = objc_build_class_component_ref (id_expression,
5582 component);
5583 /* Build a location of the form:
5584 expr.component
5585 ~~~~~^~~~~~~~~
5586 with caret at the start of the component name (at
5587 input_location), ranging from the start of the id_expression
5588 to the end of the component name. */
5589 location_t combined_loc
5590 = make_location (input_location, id_expression.get_start (),
5591 get_finish (input_location));
5592 protected_set_expr_location (result, combined_loc);
5593 return result;
5596 /* In Objective-C++, an instance variable (ivar) may be preferred
5597 to whatever cp_parser_lookup_name() found.
5598 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5599 rest of c-family, we have to do a little extra work to preserve
5600 any location information in cp_expr "decl". Given that
5601 objc_lookup_ivar is implemented in "c-family" and "objc", we
5602 have a trip through the pure "tree" type, rather than cp_expr.
5603 Naively copying it back to "decl" would implicitly give the
5604 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5605 store an EXPR_LOCATION. Hence we only update "decl" (and
5606 hence its location_t) if we get back a different tree node. */
5607 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5608 id_expression);
5609 if (decl_tree != decl.get_value ())
5610 decl = cp_expr (decl_tree);
5612 /* If name lookup gives us a SCOPE_REF, then the
5613 qualifying scope was dependent. */
5614 if (TREE_CODE (decl) == SCOPE_REF)
5616 /* At this point, we do not know if DECL is a valid
5617 integral constant expression. We assume that it is
5618 in fact such an expression, so that code like:
5620 template <int N> struct A {
5621 int a[B<N>::i];
5624 is accepted. At template-instantiation time, we
5625 will check that B<N>::i is actually a constant. */
5626 return decl;
5628 /* Check to see if DECL is a local variable in a context
5629 where that is forbidden. */
5630 if (parser->local_variables_forbidden_p
5631 && local_variable_p (decl))
5633 error_at (id_expr_token->location,
5634 "local variable %qD may not appear in this context",
5635 decl.get_value ());
5636 return error_mark_node;
5640 if (processing_template_decl)
5641 if (tree fns = maybe_get_fns (decl))
5642 /* It's too difficult to mark ths in all the places where
5643 we know for sure we need to keep the lookup, so do it
5644 now. The cost is extra GC to recycle the lookups
5645 resolved at parse time. */
5646 lookup_keep (fns);
5648 decl = (finish_id_expression
5649 (id_expression, decl, parser->scope,
5650 idk,
5651 parser->integral_constant_expression_p,
5652 parser->allow_non_integral_constant_expression_p,
5653 &parser->non_integral_constant_expression_p,
5654 template_p, done, address_p,
5655 template_arg_p,
5656 &error_msg,
5657 id_expression.get_location ()));
5658 if (error_msg)
5659 cp_parser_error (parser, error_msg);
5660 decl.set_location (id_expr_token->location);
5661 return decl;
5664 /* Anything else is an error. */
5665 default:
5666 cp_parser_error (parser, "expected primary-expression");
5667 return error_mark_node;
5671 static inline cp_expr
5672 cp_parser_primary_expression (cp_parser *parser,
5673 bool address_p,
5674 bool cast_p,
5675 bool template_arg_p,
5676 cp_id_kind *idk)
5678 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5679 /*decltype*/false, idk);
5682 /* Parse an id-expression.
5684 id-expression:
5685 unqualified-id
5686 qualified-id
5688 qualified-id:
5689 :: [opt] nested-name-specifier template [opt] unqualified-id
5690 :: identifier
5691 :: operator-function-id
5692 :: template-id
5694 Return a representation of the unqualified portion of the
5695 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5696 a `::' or nested-name-specifier.
5698 Often, if the id-expression was a qualified-id, the caller will
5699 want to make a SCOPE_REF to represent the qualified-id. This
5700 function does not do this in order to avoid wastefully creating
5701 SCOPE_REFs when they are not required.
5703 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5704 `template' keyword.
5706 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5707 uninstantiated templates.
5709 If *TEMPLATE_P is non-NULL, it is set to true iff the
5710 `template' keyword is used to explicitly indicate that the entity
5711 named is a template.
5713 If DECLARATOR_P is true, the id-expression is appearing as part of
5714 a declarator, rather than as part of an expression. */
5716 static cp_expr
5717 cp_parser_id_expression (cp_parser *parser,
5718 bool template_keyword_p,
5719 bool check_dependency_p,
5720 bool *template_p,
5721 bool declarator_p,
5722 bool optional_p)
5724 bool global_scope_p;
5725 bool nested_name_specifier_p;
5727 /* Assume the `template' keyword was not used. */
5728 if (template_p)
5729 *template_p = template_keyword_p;
5731 /* Look for the optional `::' operator. */
5732 global_scope_p
5733 = (!template_keyword_p
5734 && (cp_parser_global_scope_opt (parser,
5735 /*current_scope_valid_p=*/false)
5736 != NULL_TREE));
5738 /* Look for the optional nested-name-specifier. */
5739 nested_name_specifier_p
5740 = (cp_parser_nested_name_specifier_opt (parser,
5741 /*typename_keyword_p=*/false,
5742 check_dependency_p,
5743 /*type_p=*/false,
5744 declarator_p,
5745 template_keyword_p)
5746 != NULL_TREE);
5748 /* If there is a nested-name-specifier, then we are looking at
5749 the first qualified-id production. */
5750 if (nested_name_specifier_p)
5752 tree saved_scope;
5753 tree saved_object_scope;
5754 tree saved_qualifying_scope;
5755 cp_expr unqualified_id;
5756 bool is_template;
5758 /* See if the next token is the `template' keyword. */
5759 if (!template_p)
5760 template_p = &is_template;
5761 *template_p = cp_parser_optional_template_keyword (parser);
5762 /* Name lookup we do during the processing of the
5763 unqualified-id might obliterate SCOPE. */
5764 saved_scope = parser->scope;
5765 saved_object_scope = parser->object_scope;
5766 saved_qualifying_scope = parser->qualifying_scope;
5767 /* Process the final unqualified-id. */
5768 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5769 check_dependency_p,
5770 declarator_p,
5771 /*optional_p=*/false);
5772 /* Restore the SAVED_SCOPE for our caller. */
5773 parser->scope = saved_scope;
5774 parser->object_scope = saved_object_scope;
5775 parser->qualifying_scope = saved_qualifying_scope;
5777 return unqualified_id;
5779 /* Otherwise, if we are in global scope, then we are looking at one
5780 of the other qualified-id productions. */
5781 else if (global_scope_p)
5783 cp_token *token;
5784 tree id;
5786 /* Peek at the next token. */
5787 token = cp_lexer_peek_token (parser->lexer);
5789 /* If it's an identifier, and the next token is not a "<", then
5790 we can avoid the template-id case. This is an optimization
5791 for this common case. */
5792 if (token->type == CPP_NAME
5793 && !cp_parser_nth_token_starts_template_argument_list_p
5794 (parser, 2))
5795 return cp_parser_identifier (parser);
5797 cp_parser_parse_tentatively (parser);
5798 /* Try a template-id. */
5799 id = cp_parser_template_id (parser,
5800 /*template_keyword_p=*/false,
5801 /*check_dependency_p=*/true,
5802 none_type,
5803 declarator_p);
5804 /* If that worked, we're done. */
5805 if (cp_parser_parse_definitely (parser))
5806 return id;
5808 /* Peek at the next token. (Changes in the token buffer may
5809 have invalidated the pointer obtained above.) */
5810 token = cp_lexer_peek_token (parser->lexer);
5812 switch (token->type)
5814 case CPP_NAME:
5815 return cp_parser_identifier (parser);
5817 case CPP_KEYWORD:
5818 if (token->keyword == RID_OPERATOR)
5819 return cp_parser_operator_function_id (parser);
5820 /* Fall through. */
5822 default:
5823 cp_parser_error (parser, "expected id-expression");
5824 return error_mark_node;
5827 else
5828 return cp_parser_unqualified_id (parser, template_keyword_p,
5829 /*check_dependency_p=*/true,
5830 declarator_p,
5831 optional_p);
5834 /* Parse an unqualified-id.
5836 unqualified-id:
5837 identifier
5838 operator-function-id
5839 conversion-function-id
5840 ~ class-name
5841 template-id
5843 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5844 keyword, in a construct like `A::template ...'.
5846 Returns a representation of unqualified-id. For the `identifier'
5847 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5848 production a BIT_NOT_EXPR is returned; the operand of the
5849 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5850 other productions, see the documentation accompanying the
5851 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5852 names are looked up in uninstantiated templates. If DECLARATOR_P
5853 is true, the unqualified-id is appearing as part of a declarator,
5854 rather than as part of an expression. */
5856 static cp_expr
5857 cp_parser_unqualified_id (cp_parser* parser,
5858 bool template_keyword_p,
5859 bool check_dependency_p,
5860 bool declarator_p,
5861 bool optional_p)
5863 cp_token *token;
5865 /* Peek at the next token. */
5866 token = cp_lexer_peek_token (parser->lexer);
5868 switch ((int) token->type)
5870 case CPP_NAME:
5872 tree id;
5874 /* We don't know yet whether or not this will be a
5875 template-id. */
5876 cp_parser_parse_tentatively (parser);
5877 /* Try a template-id. */
5878 id = cp_parser_template_id (parser, template_keyword_p,
5879 check_dependency_p,
5880 none_type,
5881 declarator_p);
5882 /* If it worked, we're done. */
5883 if (cp_parser_parse_definitely (parser))
5884 return id;
5885 /* Otherwise, it's an ordinary identifier. */
5886 return cp_parser_identifier (parser);
5889 case CPP_TEMPLATE_ID:
5890 return cp_parser_template_id (parser, template_keyword_p,
5891 check_dependency_p,
5892 none_type,
5893 declarator_p);
5895 case CPP_COMPL:
5897 tree type_decl;
5898 tree qualifying_scope;
5899 tree object_scope;
5900 tree scope;
5901 bool done;
5903 /* Consume the `~' token. */
5904 cp_lexer_consume_token (parser->lexer);
5905 /* Parse the class-name. The standard, as written, seems to
5906 say that:
5908 template <typename T> struct S { ~S (); };
5909 template <typename T> S<T>::~S() {}
5911 is invalid, since `~' must be followed by a class-name, but
5912 `S<T>' is dependent, and so not known to be a class.
5913 That's not right; we need to look in uninstantiated
5914 templates. A further complication arises from:
5916 template <typename T> void f(T t) {
5917 t.T::~T();
5920 Here, it is not possible to look up `T' in the scope of `T'
5921 itself. We must look in both the current scope, and the
5922 scope of the containing complete expression.
5924 Yet another issue is:
5926 struct S {
5927 int S;
5928 ~S();
5931 S::~S() {}
5933 The standard does not seem to say that the `S' in `~S'
5934 should refer to the type `S' and not the data member
5935 `S::S'. */
5937 /* DR 244 says that we look up the name after the "~" in the
5938 same scope as we looked up the qualifying name. That idea
5939 isn't fully worked out; it's more complicated than that. */
5940 scope = parser->scope;
5941 object_scope = parser->object_scope;
5942 qualifying_scope = parser->qualifying_scope;
5944 /* Check for invalid scopes. */
5945 if (scope == error_mark_node)
5947 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5948 cp_lexer_consume_token (parser->lexer);
5949 return error_mark_node;
5951 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5953 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5954 error_at (token->location,
5955 "scope %qT before %<~%> is not a class-name",
5956 scope);
5957 cp_parser_simulate_error (parser);
5958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5959 cp_lexer_consume_token (parser->lexer);
5960 return error_mark_node;
5962 gcc_assert (!scope || TYPE_P (scope));
5964 /* If the name is of the form "X::~X" it's OK even if X is a
5965 typedef. */
5966 token = cp_lexer_peek_token (parser->lexer);
5967 if (scope
5968 && token->type == CPP_NAME
5969 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5970 != CPP_LESS)
5971 && (token->u.value == TYPE_IDENTIFIER (scope)
5972 || (CLASS_TYPE_P (scope)
5973 && constructor_name_p (token->u.value, scope))))
5975 cp_lexer_consume_token (parser->lexer);
5976 return build_nt (BIT_NOT_EXPR, scope);
5979 /* ~auto means the destructor of whatever the object is. */
5980 if (cp_parser_is_keyword (token, RID_AUTO))
5982 if (cxx_dialect < cxx14)
5983 pedwarn (input_location, 0,
5984 "%<~auto%> only available with "
5985 "-std=c++14 or -std=gnu++14");
5986 cp_lexer_consume_token (parser->lexer);
5987 return build_nt (BIT_NOT_EXPR, make_auto ());
5990 /* If there was an explicit qualification (S::~T), first look
5991 in the scope given by the qualification (i.e., S).
5993 Note: in the calls to cp_parser_class_name below we pass
5994 typename_type so that lookup finds the injected-class-name
5995 rather than the constructor. */
5996 done = false;
5997 type_decl = NULL_TREE;
5998 if (scope)
6000 cp_parser_parse_tentatively (parser);
6001 type_decl = cp_parser_class_name (parser,
6002 /*typename_keyword_p=*/false,
6003 /*template_keyword_p=*/false,
6004 typename_type,
6005 /*check_dependency=*/false,
6006 /*class_head_p=*/false,
6007 declarator_p);
6008 if (cp_parser_parse_definitely (parser))
6009 done = true;
6011 /* In "N::S::~S", look in "N" as well. */
6012 if (!done && scope && qualifying_scope)
6014 cp_parser_parse_tentatively (parser);
6015 parser->scope = qualifying_scope;
6016 parser->object_scope = NULL_TREE;
6017 parser->qualifying_scope = NULL_TREE;
6018 type_decl
6019 = cp_parser_class_name (parser,
6020 /*typename_keyword_p=*/false,
6021 /*template_keyword_p=*/false,
6022 typename_type,
6023 /*check_dependency=*/false,
6024 /*class_head_p=*/false,
6025 declarator_p);
6026 if (cp_parser_parse_definitely (parser))
6027 done = true;
6029 /* In "p->S::~T", look in the scope given by "*p" as well. */
6030 else if (!done && object_scope)
6032 cp_parser_parse_tentatively (parser);
6033 parser->scope = object_scope;
6034 parser->object_scope = NULL_TREE;
6035 parser->qualifying_scope = NULL_TREE;
6036 type_decl
6037 = cp_parser_class_name (parser,
6038 /*typename_keyword_p=*/false,
6039 /*template_keyword_p=*/false,
6040 typename_type,
6041 /*check_dependency=*/false,
6042 /*class_head_p=*/false,
6043 declarator_p);
6044 if (cp_parser_parse_definitely (parser))
6045 done = true;
6047 /* Look in the surrounding context. */
6048 if (!done)
6050 parser->scope = NULL_TREE;
6051 parser->object_scope = NULL_TREE;
6052 parser->qualifying_scope = NULL_TREE;
6053 if (processing_template_decl)
6054 cp_parser_parse_tentatively (parser);
6055 type_decl
6056 = cp_parser_class_name (parser,
6057 /*typename_keyword_p=*/false,
6058 /*template_keyword_p=*/false,
6059 typename_type,
6060 /*check_dependency=*/false,
6061 /*class_head_p=*/false,
6062 declarator_p);
6063 if (processing_template_decl
6064 && ! cp_parser_parse_definitely (parser))
6066 /* We couldn't find a type with this name. If we're parsing
6067 tentatively, fail and try something else. */
6068 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6070 cp_parser_simulate_error (parser);
6071 return error_mark_node;
6073 /* Otherwise, accept it and check for a match at instantiation
6074 time. */
6075 type_decl = cp_parser_identifier (parser);
6076 if (type_decl != error_mark_node)
6077 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6078 return type_decl;
6081 /* If an error occurred, assume that the name of the
6082 destructor is the same as the name of the qualifying
6083 class. That allows us to keep parsing after running
6084 into ill-formed destructor names. */
6085 if (type_decl == error_mark_node && scope)
6086 return build_nt (BIT_NOT_EXPR, scope);
6087 else if (type_decl == error_mark_node)
6088 return error_mark_node;
6090 /* Check that destructor name and scope match. */
6091 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6093 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6094 error_at (token->location,
6095 "declaration of %<~%T%> as member of %qT",
6096 type_decl, scope);
6097 cp_parser_simulate_error (parser);
6098 return error_mark_node;
6101 /* [class.dtor]
6103 A typedef-name that names a class shall not be used as the
6104 identifier in the declarator for a destructor declaration. */
6105 if (declarator_p
6106 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6107 && !DECL_SELF_REFERENCE_P (type_decl)
6108 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6109 error_at (token->location,
6110 "typedef-name %qD used as destructor declarator",
6111 type_decl);
6113 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6116 case CPP_KEYWORD:
6117 if (token->keyword == RID_OPERATOR)
6119 cp_expr id;
6121 /* This could be a template-id, so we try that first. */
6122 cp_parser_parse_tentatively (parser);
6123 /* Try a template-id. */
6124 id = cp_parser_template_id (parser, template_keyword_p,
6125 /*check_dependency_p=*/true,
6126 none_type,
6127 declarator_p);
6128 /* If that worked, we're done. */
6129 if (cp_parser_parse_definitely (parser))
6130 return id;
6131 /* We still don't know whether we're looking at an
6132 operator-function-id or a conversion-function-id. */
6133 cp_parser_parse_tentatively (parser);
6134 /* Try an operator-function-id. */
6135 id = cp_parser_operator_function_id (parser);
6136 /* If that didn't work, try a conversion-function-id. */
6137 if (!cp_parser_parse_definitely (parser))
6138 id = cp_parser_conversion_function_id (parser);
6140 return id;
6142 /* Fall through. */
6144 default:
6145 if (optional_p)
6146 return NULL_TREE;
6147 cp_parser_error (parser, "expected unqualified-id");
6148 return error_mark_node;
6152 /* Parse an (optional) nested-name-specifier.
6154 nested-name-specifier: [C++98]
6155 class-or-namespace-name :: nested-name-specifier [opt]
6156 class-or-namespace-name :: template nested-name-specifier [opt]
6158 nested-name-specifier: [C++0x]
6159 type-name ::
6160 namespace-name ::
6161 nested-name-specifier identifier ::
6162 nested-name-specifier template [opt] simple-template-id ::
6164 PARSER->SCOPE should be set appropriately before this function is
6165 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6166 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6167 in name lookups.
6169 Sets PARSER->SCOPE to the class (TYPE) or namespace
6170 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6171 it unchanged if there is no nested-name-specifier. Returns the new
6172 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6174 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6175 part of a declaration and/or decl-specifier. */
6177 static tree
6178 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6179 bool typename_keyword_p,
6180 bool check_dependency_p,
6181 bool type_p,
6182 bool is_declaration,
6183 bool template_keyword_p /* = false */)
6185 bool success = false;
6186 cp_token_position start = 0;
6187 cp_token *token;
6189 /* Remember where the nested-name-specifier starts. */
6190 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6192 start = cp_lexer_token_position (parser->lexer, false);
6193 push_deferring_access_checks (dk_deferred);
6196 while (true)
6198 tree new_scope;
6199 tree old_scope;
6200 tree saved_qualifying_scope;
6202 /* Spot cases that cannot be the beginning of a
6203 nested-name-specifier. */
6204 token = cp_lexer_peek_token (parser->lexer);
6206 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6207 the already parsed nested-name-specifier. */
6208 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6210 /* Grab the nested-name-specifier and continue the loop. */
6211 cp_parser_pre_parsed_nested_name_specifier (parser);
6212 /* If we originally encountered this nested-name-specifier
6213 with IS_DECLARATION set to false, we will not have
6214 resolved TYPENAME_TYPEs, so we must do so here. */
6215 if (is_declaration
6216 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6218 new_scope = resolve_typename_type (parser->scope,
6219 /*only_current_p=*/false);
6220 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6221 parser->scope = new_scope;
6223 success = true;
6224 continue;
6227 /* Spot cases that cannot be the beginning of a
6228 nested-name-specifier. On the second and subsequent times
6229 through the loop, we look for the `template' keyword. */
6230 if (success && token->keyword == RID_TEMPLATE)
6232 /* A template-id can start a nested-name-specifier. */
6233 else if (token->type == CPP_TEMPLATE_ID)
6235 /* DR 743: decltype can be used in a nested-name-specifier. */
6236 else if (token_is_decltype (token))
6238 else
6240 /* If the next token is not an identifier, then it is
6241 definitely not a type-name or namespace-name. */
6242 if (token->type != CPP_NAME)
6243 break;
6244 /* If the following token is neither a `<' (to begin a
6245 template-id), nor a `::', then we are not looking at a
6246 nested-name-specifier. */
6247 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6249 if (token->type == CPP_COLON
6250 && parser->colon_corrects_to_scope_p
6251 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6253 gcc_rich_location richloc (token->location);
6254 richloc.add_fixit_replace ("::");
6255 error_at (&richloc,
6256 "found %<:%> in nested-name-specifier, "
6257 "expected %<::%>");
6258 token->type = CPP_SCOPE;
6261 if (token->type != CPP_SCOPE
6262 && !cp_parser_nth_token_starts_template_argument_list_p
6263 (parser, 2))
6264 break;
6267 /* The nested-name-specifier is optional, so we parse
6268 tentatively. */
6269 cp_parser_parse_tentatively (parser);
6271 /* Look for the optional `template' keyword, if this isn't the
6272 first time through the loop. */
6273 if (success)
6274 template_keyword_p = cp_parser_optional_template_keyword (parser);
6276 /* Save the old scope since the name lookup we are about to do
6277 might destroy it. */
6278 old_scope = parser->scope;
6279 saved_qualifying_scope = parser->qualifying_scope;
6280 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6281 look up names in "X<T>::I" in order to determine that "Y" is
6282 a template. So, if we have a typename at this point, we make
6283 an effort to look through it. */
6284 if (is_declaration
6285 && !typename_keyword_p
6286 && parser->scope
6287 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6288 parser->scope = resolve_typename_type (parser->scope,
6289 /*only_current_p=*/false);
6290 /* Parse the qualifying entity. */
6291 new_scope
6292 = cp_parser_qualifying_entity (parser,
6293 typename_keyword_p,
6294 template_keyword_p,
6295 check_dependency_p,
6296 type_p,
6297 is_declaration);
6298 /* Look for the `::' token. */
6299 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6301 /* If we found what we wanted, we keep going; otherwise, we're
6302 done. */
6303 if (!cp_parser_parse_definitely (parser))
6305 bool error_p = false;
6307 /* Restore the OLD_SCOPE since it was valid before the
6308 failed attempt at finding the last
6309 class-or-namespace-name. */
6310 parser->scope = old_scope;
6311 parser->qualifying_scope = saved_qualifying_scope;
6313 /* If the next token is a decltype, and the one after that is a
6314 `::', then the decltype has failed to resolve to a class or
6315 enumeration type. Give this error even when parsing
6316 tentatively since it can't possibly be valid--and we're going
6317 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6318 won't get another chance.*/
6319 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6320 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6321 == CPP_SCOPE))
6323 token = cp_lexer_consume_token (parser->lexer);
6324 error_at (token->location, "decltype evaluates to %qT, "
6325 "which is not a class or enumeration type",
6326 token->u.tree_check_value->value);
6327 parser->scope = error_mark_node;
6328 error_p = true;
6329 /* As below. */
6330 success = true;
6331 cp_lexer_consume_token (parser->lexer);
6334 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6335 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6337 /* If we have a non-type template-id followed by ::, it can't
6338 possibly be valid. */
6339 token = cp_lexer_peek_token (parser->lexer);
6340 tree tid = token->u.tree_check_value->value;
6341 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6342 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6344 tree tmpl = NULL_TREE;
6345 if (is_overloaded_fn (tid))
6347 tree fns = get_fns (tid);
6348 if (OVL_SINGLE_P (fns))
6349 tmpl = OVL_FIRST (fns);
6350 error_at (token->location, "function template-id %qD "
6351 "in nested-name-specifier", tid);
6353 else
6355 /* Variable template. */
6356 tmpl = TREE_OPERAND (tid, 0);
6357 gcc_assert (variable_template_p (tmpl));
6358 error_at (token->location, "variable template-id %qD "
6359 "in nested-name-specifier", tid);
6361 if (tmpl)
6362 inform (DECL_SOURCE_LOCATION (tmpl),
6363 "%qD declared here", tmpl);
6365 parser->scope = error_mark_node;
6366 error_p = true;
6367 /* As below. */
6368 success = true;
6369 cp_lexer_consume_token (parser->lexer);
6370 cp_lexer_consume_token (parser->lexer);
6374 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6375 break;
6376 /* If the next token is an identifier, and the one after
6377 that is a `::', then any valid interpretation would have
6378 found a class-or-namespace-name. */
6379 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6380 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6381 == CPP_SCOPE)
6382 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6383 != CPP_COMPL))
6385 token = cp_lexer_consume_token (parser->lexer);
6386 if (!error_p)
6388 if (!token->error_reported)
6390 tree decl;
6391 tree ambiguous_decls;
6393 decl = cp_parser_lookup_name (parser, token->u.value,
6394 none_type,
6395 /*is_template=*/false,
6396 /*is_namespace=*/false,
6397 /*check_dependency=*/true,
6398 &ambiguous_decls,
6399 token->location);
6400 if (TREE_CODE (decl) == TEMPLATE_DECL)
6401 error_at (token->location,
6402 "%qD used without template arguments",
6403 decl);
6404 else if (ambiguous_decls)
6406 // cp_parser_lookup_name has the same diagnostic,
6407 // thus make sure to emit it at most once.
6408 if (cp_parser_uncommitted_to_tentative_parse_p
6409 (parser))
6411 error_at (token->location,
6412 "reference to %qD is ambiguous",
6413 token->u.value);
6414 print_candidates (ambiguous_decls);
6416 decl = error_mark_node;
6418 else
6420 if (cxx_dialect != cxx98)
6421 cp_parser_name_lookup_error
6422 (parser, token->u.value, decl, NLE_NOT_CXX98,
6423 token->location);
6424 else
6425 cp_parser_name_lookup_error
6426 (parser, token->u.value, decl, NLE_CXX98,
6427 token->location);
6430 parser->scope = error_mark_node;
6431 error_p = true;
6432 /* Treat this as a successful nested-name-specifier
6433 due to:
6435 [basic.lookup.qual]
6437 If the name found is not a class-name (clause
6438 _class_) or namespace-name (_namespace.def_), the
6439 program is ill-formed. */
6440 success = true;
6442 cp_lexer_consume_token (parser->lexer);
6444 break;
6446 /* We've found one valid nested-name-specifier. */
6447 success = true;
6448 /* Name lookup always gives us a DECL. */
6449 if (TREE_CODE (new_scope) == TYPE_DECL)
6450 new_scope = TREE_TYPE (new_scope);
6451 /* Uses of "template" must be followed by actual templates. */
6452 if (template_keyword_p
6453 && !(CLASS_TYPE_P (new_scope)
6454 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6455 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6456 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6457 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6458 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6459 == TEMPLATE_ID_EXPR)))
6460 permerror (input_location, TYPE_P (new_scope)
6461 ? G_("%qT is not a template")
6462 : G_("%qD is not a template"),
6463 new_scope);
6464 /* If it is a class scope, try to complete it; we are about to
6465 be looking up names inside the class. */
6466 if (TYPE_P (new_scope)
6467 /* Since checking types for dependency can be expensive,
6468 avoid doing it if the type is already complete. */
6469 && !COMPLETE_TYPE_P (new_scope)
6470 /* Do not try to complete dependent types. */
6471 && !dependent_type_p (new_scope))
6473 new_scope = complete_type (new_scope);
6474 /* If it is a typedef to current class, use the current
6475 class instead, as the typedef won't have any names inside
6476 it yet. */
6477 if (!COMPLETE_TYPE_P (new_scope)
6478 && currently_open_class (new_scope))
6479 new_scope = TYPE_MAIN_VARIANT (new_scope);
6481 /* Make sure we look in the right scope the next time through
6482 the loop. */
6483 parser->scope = new_scope;
6486 /* If parsing tentatively, replace the sequence of tokens that makes
6487 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6488 token. That way, should we re-parse the token stream, we will
6489 not have to repeat the effort required to do the parse, nor will
6490 we issue duplicate error messages. */
6491 if (success && start)
6493 cp_token *token;
6495 token = cp_lexer_token_at (parser->lexer, start);
6496 /* Reset the contents of the START token. */
6497 token->type = CPP_NESTED_NAME_SPECIFIER;
6498 /* Retrieve any deferred checks. Do not pop this access checks yet
6499 so the memory will not be reclaimed during token replacing below. */
6500 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6501 token->u.tree_check_value->value = parser->scope;
6502 token->u.tree_check_value->checks = get_deferred_access_checks ();
6503 token->u.tree_check_value->qualifying_scope =
6504 parser->qualifying_scope;
6505 token->keyword = RID_MAX;
6507 /* Purge all subsequent tokens. */
6508 cp_lexer_purge_tokens_after (parser->lexer, start);
6511 if (start)
6512 pop_to_parent_deferring_access_checks ();
6514 return success ? parser->scope : NULL_TREE;
6517 /* Parse a nested-name-specifier. See
6518 cp_parser_nested_name_specifier_opt for details. This function
6519 behaves identically, except that it will an issue an error if no
6520 nested-name-specifier is present. */
6522 static tree
6523 cp_parser_nested_name_specifier (cp_parser *parser,
6524 bool typename_keyword_p,
6525 bool check_dependency_p,
6526 bool type_p,
6527 bool is_declaration)
6529 tree scope;
6531 /* Look for the nested-name-specifier. */
6532 scope = cp_parser_nested_name_specifier_opt (parser,
6533 typename_keyword_p,
6534 check_dependency_p,
6535 type_p,
6536 is_declaration);
6537 /* If it was not present, issue an error message. */
6538 if (!scope)
6540 cp_parser_error (parser, "expected nested-name-specifier");
6541 parser->scope = NULL_TREE;
6544 return scope;
6547 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6548 this is either a class-name or a namespace-name (which corresponds
6549 to the class-or-namespace-name production in the grammar). For
6550 C++0x, it can also be a type-name that refers to an enumeration
6551 type or a simple-template-id.
6553 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6554 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6555 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6556 TYPE_P is TRUE iff the next name should be taken as a class-name,
6557 even the same name is declared to be another entity in the same
6558 scope.
6560 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6561 specified by the class-or-namespace-name. If neither is found the
6562 ERROR_MARK_NODE is returned. */
6564 static tree
6565 cp_parser_qualifying_entity (cp_parser *parser,
6566 bool typename_keyword_p,
6567 bool template_keyword_p,
6568 bool check_dependency_p,
6569 bool type_p,
6570 bool is_declaration)
6572 tree saved_scope;
6573 tree saved_qualifying_scope;
6574 tree saved_object_scope;
6575 tree scope;
6576 bool only_class_p;
6577 bool successful_parse_p;
6579 /* DR 743: decltype can appear in a nested-name-specifier. */
6580 if (cp_lexer_next_token_is_decltype (parser->lexer))
6582 scope = cp_parser_decltype (parser);
6583 if (TREE_CODE (scope) != ENUMERAL_TYPE
6584 && !MAYBE_CLASS_TYPE_P (scope))
6586 cp_parser_simulate_error (parser);
6587 return error_mark_node;
6589 if (TYPE_NAME (scope))
6590 scope = TYPE_NAME (scope);
6591 return scope;
6594 /* Before we try to parse the class-name, we must save away the
6595 current PARSER->SCOPE since cp_parser_class_name will destroy
6596 it. */
6597 saved_scope = parser->scope;
6598 saved_qualifying_scope = parser->qualifying_scope;
6599 saved_object_scope = parser->object_scope;
6600 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6601 there is no need to look for a namespace-name. */
6602 only_class_p = template_keyword_p
6603 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6604 if (!only_class_p)
6605 cp_parser_parse_tentatively (parser);
6606 scope = cp_parser_class_name (parser,
6607 typename_keyword_p,
6608 template_keyword_p,
6609 type_p ? class_type : none_type,
6610 check_dependency_p,
6611 /*class_head_p=*/false,
6612 is_declaration,
6613 /*enum_ok=*/cxx_dialect > cxx98);
6614 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6615 /* If that didn't work, try for a namespace-name. */
6616 if (!only_class_p && !successful_parse_p)
6618 /* Restore the saved scope. */
6619 parser->scope = saved_scope;
6620 parser->qualifying_scope = saved_qualifying_scope;
6621 parser->object_scope = saved_object_scope;
6622 /* If we are not looking at an identifier followed by the scope
6623 resolution operator, then this is not part of a
6624 nested-name-specifier. (Note that this function is only used
6625 to parse the components of a nested-name-specifier.) */
6626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6627 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6628 return error_mark_node;
6629 scope = cp_parser_namespace_name (parser);
6632 return scope;
6635 /* Return true if we are looking at a compound-literal, false otherwise. */
6637 static bool
6638 cp_parser_compound_literal_p (cp_parser *parser)
6640 cp_lexer_save_tokens (parser->lexer);
6642 /* Skip tokens until the next token is a closing parenthesis.
6643 If we find the closing `)', and the next token is a `{', then
6644 we are looking at a compound-literal. */
6645 bool compound_literal_p
6646 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6647 /*consume_paren=*/true)
6648 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6650 /* Roll back the tokens we skipped. */
6651 cp_lexer_rollback_tokens (parser->lexer);
6653 return compound_literal_p;
6656 /* Return true if EXPR is the integer constant zero or a complex constant
6657 of zero, without any folding, but ignoring location wrappers. */
6659 bool
6660 literal_integer_zerop (const_tree expr)
6662 return (location_wrapper_p (expr)
6663 && integer_zerop (TREE_OPERAND (expr, 0)));
6666 /* Parse a postfix-expression.
6668 postfix-expression:
6669 primary-expression
6670 postfix-expression [ expression ]
6671 postfix-expression ( expression-list [opt] )
6672 simple-type-specifier ( expression-list [opt] )
6673 typename :: [opt] nested-name-specifier identifier
6674 ( expression-list [opt] )
6675 typename :: [opt] nested-name-specifier template [opt] template-id
6676 ( expression-list [opt] )
6677 postfix-expression . template [opt] id-expression
6678 postfix-expression -> template [opt] id-expression
6679 postfix-expression . pseudo-destructor-name
6680 postfix-expression -> pseudo-destructor-name
6681 postfix-expression ++
6682 postfix-expression --
6683 dynamic_cast < type-id > ( expression )
6684 static_cast < type-id > ( expression )
6685 reinterpret_cast < type-id > ( expression )
6686 const_cast < type-id > ( expression )
6687 typeid ( expression )
6688 typeid ( type-id )
6690 GNU Extension:
6692 postfix-expression:
6693 ( type-id ) { initializer-list , [opt] }
6695 This extension is a GNU version of the C99 compound-literal
6696 construct. (The C99 grammar uses `type-name' instead of `type-id',
6697 but they are essentially the same concept.)
6699 If ADDRESS_P is true, the postfix expression is the operand of the
6700 `&' operator. CAST_P is true if this expression is the target of a
6701 cast.
6703 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6704 class member access expressions [expr.ref].
6706 Returns a representation of the expression. */
6708 static cp_expr
6709 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6710 bool member_access_only_p, bool decltype_p,
6711 cp_id_kind * pidk_return)
6713 cp_token *token;
6714 location_t loc;
6715 enum rid keyword;
6716 cp_id_kind idk = CP_ID_KIND_NONE;
6717 cp_expr postfix_expression = NULL_TREE;
6718 bool is_member_access = false;
6720 /* Peek at the next token. */
6721 token = cp_lexer_peek_token (parser->lexer);
6722 loc = token->location;
6723 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6725 /* Some of the productions are determined by keywords. */
6726 keyword = token->keyword;
6727 switch (keyword)
6729 case RID_DYNCAST:
6730 case RID_STATCAST:
6731 case RID_REINTCAST:
6732 case RID_CONSTCAST:
6734 tree type;
6735 cp_expr expression;
6736 const char *saved_message;
6737 bool saved_in_type_id_in_expr_p;
6739 /* All of these can be handled in the same way from the point
6740 of view of parsing. Begin by consuming the token
6741 identifying the cast. */
6742 cp_lexer_consume_token (parser->lexer);
6744 /* New types cannot be defined in the cast. */
6745 saved_message = parser->type_definition_forbidden_message;
6746 parser->type_definition_forbidden_message
6747 = G_("types may not be defined in casts");
6749 /* Look for the opening `<'. */
6750 cp_parser_require (parser, CPP_LESS, RT_LESS);
6751 /* Parse the type to which we are casting. */
6752 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6753 parser->in_type_id_in_expr_p = true;
6754 type = cp_parser_type_id (parser);
6755 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6756 /* Look for the closing `>'. */
6757 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6758 /* Restore the old message. */
6759 parser->type_definition_forbidden_message = saved_message;
6761 bool saved_greater_than_is_operator_p
6762 = parser->greater_than_is_operator_p;
6763 parser->greater_than_is_operator_p = true;
6765 /* And the expression which is being cast. */
6766 matching_parens parens;
6767 parens.require_open (parser);
6768 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6769 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6770 RT_CLOSE_PAREN);
6771 location_t end_loc = close_paren ?
6772 close_paren->location : UNKNOWN_LOCATION;
6774 parser->greater_than_is_operator_p
6775 = saved_greater_than_is_operator_p;
6777 /* Only type conversions to integral or enumeration types
6778 can be used in constant-expressions. */
6779 if (!cast_valid_in_integral_constant_expression_p (type)
6780 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6782 postfix_expression = error_mark_node;
6783 break;
6786 switch (keyword)
6788 case RID_DYNCAST:
6789 postfix_expression
6790 = build_dynamic_cast (type, expression, tf_warning_or_error);
6791 break;
6792 case RID_STATCAST:
6793 postfix_expression
6794 = build_static_cast (type, expression, tf_warning_or_error);
6795 break;
6796 case RID_REINTCAST:
6797 postfix_expression
6798 = build_reinterpret_cast (type, expression,
6799 tf_warning_or_error);
6800 break;
6801 case RID_CONSTCAST:
6802 postfix_expression
6803 = build_const_cast (type, expression, tf_warning_or_error);
6804 break;
6805 default:
6806 gcc_unreachable ();
6809 /* Construct a location e.g. :
6810 reinterpret_cast <int *> (expr)
6811 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6812 ranging from the start of the "*_cast" token to the final closing
6813 paren, with the caret at the start. */
6814 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6815 postfix_expression.set_location (cp_cast_loc);
6817 break;
6819 case RID_TYPEID:
6821 tree type;
6822 const char *saved_message;
6823 bool saved_in_type_id_in_expr_p;
6825 /* Consume the `typeid' token. */
6826 cp_lexer_consume_token (parser->lexer);
6827 /* Look for the `(' token. */
6828 matching_parens parens;
6829 parens.require_open (parser);
6830 /* Types cannot be defined in a `typeid' expression. */
6831 saved_message = parser->type_definition_forbidden_message;
6832 parser->type_definition_forbidden_message
6833 = G_("types may not be defined in a %<typeid%> expression");
6834 /* We can't be sure yet whether we're looking at a type-id or an
6835 expression. */
6836 cp_parser_parse_tentatively (parser);
6837 /* Try a type-id first. */
6838 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6839 parser->in_type_id_in_expr_p = true;
6840 type = cp_parser_type_id (parser);
6841 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6842 /* Look for the `)' token. Otherwise, we can't be sure that
6843 we're not looking at an expression: consider `typeid (int
6844 (3))', for example. */
6845 cp_token *close_paren = parens.require_close (parser);
6846 /* If all went well, simply lookup the type-id. */
6847 if (cp_parser_parse_definitely (parser))
6848 postfix_expression = get_typeid (type, tf_warning_or_error);
6849 /* Otherwise, fall back to the expression variant. */
6850 else
6852 tree expression;
6854 /* Look for an expression. */
6855 expression = cp_parser_expression (parser, & idk);
6856 /* Compute its typeid. */
6857 postfix_expression = build_typeid (expression, tf_warning_or_error);
6858 /* Look for the `)' token. */
6859 close_paren = parens.require_close (parser);
6861 /* Restore the saved message. */
6862 parser->type_definition_forbidden_message = saved_message;
6863 /* `typeid' may not appear in an integral constant expression. */
6864 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6865 postfix_expression = error_mark_node;
6867 /* Construct a location e.g. :
6868 typeid (expr)
6869 ^~~~~~~~~~~~~
6870 ranging from the start of the "typeid" token to the final closing
6871 paren, with the caret at the start. */
6872 if (close_paren)
6874 location_t typeid_loc
6875 = make_location (start_loc, start_loc, close_paren->location);
6876 postfix_expression.set_location (typeid_loc);
6877 postfix_expression.maybe_add_location_wrapper ();
6880 break;
6882 case RID_TYPENAME:
6884 tree type;
6885 /* The syntax permitted here is the same permitted for an
6886 elaborated-type-specifier. */
6887 ++parser->prevent_constrained_type_specifiers;
6888 type = cp_parser_elaborated_type_specifier (parser,
6889 /*is_friend=*/false,
6890 /*is_declaration=*/false);
6891 --parser->prevent_constrained_type_specifiers;
6892 postfix_expression = cp_parser_functional_cast (parser, type);
6894 break;
6896 case RID_ADDRESSOF:
6897 case RID_BUILTIN_SHUFFLE:
6898 case RID_BUILTIN_LAUNDER:
6900 vec<tree, va_gc> *vec;
6901 unsigned int i;
6902 tree p;
6904 cp_lexer_consume_token (parser->lexer);
6905 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6906 /*cast_p=*/false, /*allow_expansion_p=*/true,
6907 /*non_constant_p=*/NULL);
6908 if (vec == NULL)
6910 postfix_expression = error_mark_node;
6911 break;
6914 FOR_EACH_VEC_ELT (*vec, i, p)
6915 mark_exp_read (p);
6917 switch (keyword)
6919 case RID_ADDRESSOF:
6920 if (vec->length () == 1)
6921 postfix_expression
6922 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6923 else
6925 error_at (loc, "wrong number of arguments to "
6926 "%<__builtin_addressof%>");
6927 postfix_expression = error_mark_node;
6929 break;
6931 case RID_BUILTIN_LAUNDER:
6932 if (vec->length () == 1)
6933 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6934 tf_warning_or_error);
6935 else
6937 error_at (loc, "wrong number of arguments to "
6938 "%<__builtin_launder%>");
6939 postfix_expression = error_mark_node;
6941 break;
6943 case RID_BUILTIN_SHUFFLE:
6944 if (vec->length () == 2)
6945 postfix_expression
6946 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6947 (*vec)[1], tf_warning_or_error);
6948 else if (vec->length () == 3)
6949 postfix_expression
6950 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6951 (*vec)[2], tf_warning_or_error);
6952 else
6954 error_at (loc, "wrong number of arguments to "
6955 "%<__builtin_shuffle%>");
6956 postfix_expression = error_mark_node;
6958 break;
6960 default:
6961 gcc_unreachable ();
6963 break;
6966 default:
6968 tree type;
6970 /* If the next thing is a simple-type-specifier, we may be
6971 looking at a functional cast. We could also be looking at
6972 an id-expression. So, we try the functional cast, and if
6973 that doesn't work we fall back to the primary-expression. */
6974 cp_parser_parse_tentatively (parser);
6975 /* Look for the simple-type-specifier. */
6976 ++parser->prevent_constrained_type_specifiers;
6977 type = cp_parser_simple_type_specifier (parser,
6978 /*decl_specs=*/NULL,
6979 CP_PARSER_FLAGS_NONE);
6980 --parser->prevent_constrained_type_specifiers;
6981 /* Parse the cast itself. */
6982 if (!cp_parser_error_occurred (parser))
6983 postfix_expression
6984 = cp_parser_functional_cast (parser, type);
6985 /* If that worked, we're done. */
6986 if (cp_parser_parse_definitely (parser))
6987 break;
6989 /* If the functional-cast didn't work out, try a
6990 compound-literal. */
6991 if (cp_parser_allow_gnu_extensions_p (parser)
6992 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6994 cp_expr initializer = NULL_TREE;
6996 cp_parser_parse_tentatively (parser);
6998 matching_parens parens;
6999 parens.consume_open (parser);
7001 /* Avoid calling cp_parser_type_id pointlessly, see comment
7002 in cp_parser_cast_expression about c++/29234. */
7003 if (!cp_parser_compound_literal_p (parser))
7004 cp_parser_simulate_error (parser);
7005 else
7007 /* Parse the type. */
7008 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7009 parser->in_type_id_in_expr_p = true;
7010 type = cp_parser_type_id (parser);
7011 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7012 parens.require_close (parser);
7015 /* If things aren't going well, there's no need to
7016 keep going. */
7017 if (!cp_parser_error_occurred (parser))
7019 bool non_constant_p;
7020 /* Parse the brace-enclosed initializer list. */
7021 initializer = cp_parser_braced_list (parser,
7022 &non_constant_p);
7024 /* If that worked, we're definitely looking at a
7025 compound-literal expression. */
7026 if (cp_parser_parse_definitely (parser))
7028 /* Warn the user that a compound literal is not
7029 allowed in standard C++. */
7030 pedwarn (input_location, OPT_Wpedantic,
7031 "ISO C++ forbids compound-literals");
7032 /* For simplicity, we disallow compound literals in
7033 constant-expressions. We could
7034 allow compound literals of integer type, whose
7035 initializer was a constant, in constant
7036 expressions. Permitting that usage, as a further
7037 extension, would not change the meaning of any
7038 currently accepted programs. (Of course, as
7039 compound literals are not part of ISO C++, the
7040 standard has nothing to say.) */
7041 if (cp_parser_non_integral_constant_expression (parser,
7042 NIC_NCC))
7044 postfix_expression = error_mark_node;
7045 break;
7047 /* Form the representation of the compound-literal. */
7048 postfix_expression
7049 = finish_compound_literal (type, initializer,
7050 tf_warning_or_error, fcl_c99);
7051 postfix_expression.set_location (initializer.get_location ());
7052 break;
7056 /* It must be a primary-expression. */
7057 postfix_expression
7058 = cp_parser_primary_expression (parser, address_p, cast_p,
7059 /*template_arg_p=*/false,
7060 decltype_p,
7061 &idk);
7063 break;
7066 /* Note that we don't need to worry about calling build_cplus_new on a
7067 class-valued CALL_EXPR in decltype when it isn't the end of the
7068 postfix-expression; unary_complex_lvalue will take care of that for
7069 all these cases. */
7071 /* Keep looping until the postfix-expression is complete. */
7072 while (true)
7074 if (idk == CP_ID_KIND_UNQUALIFIED
7075 && identifier_p (postfix_expression)
7076 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7077 /* It is not a Koenig lookup function call. */
7078 postfix_expression
7079 = unqualified_name_lookup_error (postfix_expression);
7081 /* Peek at the next token. */
7082 token = cp_lexer_peek_token (parser->lexer);
7084 switch (token->type)
7086 case CPP_OPEN_SQUARE:
7087 if (cp_next_tokens_can_be_std_attribute_p (parser))
7089 cp_parser_error (parser,
7090 "two consecutive %<[%> shall "
7091 "only introduce an attribute");
7092 return error_mark_node;
7094 postfix_expression
7095 = cp_parser_postfix_open_square_expression (parser,
7096 postfix_expression,
7097 false,
7098 decltype_p);
7099 postfix_expression.set_range (start_loc,
7100 postfix_expression.get_location ());
7102 idk = CP_ID_KIND_NONE;
7103 is_member_access = false;
7104 break;
7106 case CPP_OPEN_PAREN:
7107 /* postfix-expression ( expression-list [opt] ) */
7109 bool koenig_p;
7110 bool is_builtin_constant_p;
7111 bool saved_integral_constant_expression_p = false;
7112 bool saved_non_integral_constant_expression_p = false;
7113 tsubst_flags_t complain = complain_flags (decltype_p);
7114 vec<tree, va_gc> *args;
7115 location_t close_paren_loc = UNKNOWN_LOCATION;
7117 is_member_access = false;
7119 is_builtin_constant_p
7120 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7121 if (is_builtin_constant_p)
7123 /* The whole point of __builtin_constant_p is to allow
7124 non-constant expressions to appear as arguments. */
7125 saved_integral_constant_expression_p
7126 = parser->integral_constant_expression_p;
7127 saved_non_integral_constant_expression_p
7128 = parser->non_integral_constant_expression_p;
7129 parser->integral_constant_expression_p = false;
7131 args = (cp_parser_parenthesized_expression_list
7132 (parser, non_attr,
7133 /*cast_p=*/false, /*allow_expansion_p=*/true,
7134 /*non_constant_p=*/NULL,
7135 /*close_paren_loc=*/&close_paren_loc,
7136 /*wrap_locations_p=*/true));
7137 if (is_builtin_constant_p)
7139 parser->integral_constant_expression_p
7140 = saved_integral_constant_expression_p;
7141 parser->non_integral_constant_expression_p
7142 = saved_non_integral_constant_expression_p;
7145 if (args == NULL)
7147 postfix_expression = error_mark_node;
7148 break;
7151 /* Function calls are not permitted in
7152 constant-expressions. */
7153 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7154 && cp_parser_non_integral_constant_expression (parser,
7155 NIC_FUNC_CALL))
7157 postfix_expression = error_mark_node;
7158 release_tree_vector (args);
7159 break;
7162 koenig_p = false;
7163 if (idk == CP_ID_KIND_UNQUALIFIED
7164 || idk == CP_ID_KIND_TEMPLATE_ID)
7166 if (identifier_p (postfix_expression))
7168 if (!args->is_empty ())
7170 koenig_p = true;
7171 if (!any_type_dependent_arguments_p (args))
7172 postfix_expression
7173 = perform_koenig_lookup (postfix_expression, args,
7174 complain);
7176 else
7177 postfix_expression
7178 = unqualified_fn_lookup_error (postfix_expression);
7180 /* We do not perform argument-dependent lookup if
7181 normal lookup finds a non-function, in accordance
7182 with the expected resolution of DR 218. */
7183 else if (!args->is_empty ()
7184 && is_overloaded_fn (postfix_expression))
7186 tree fn = get_first_fn (postfix_expression);
7187 fn = STRIP_TEMPLATE (fn);
7189 /* Do not do argument dependent lookup if regular
7190 lookup finds a member function or a block-scope
7191 function declaration. [basic.lookup.argdep]/3 */
7192 if (!DECL_FUNCTION_MEMBER_P (fn)
7193 && !DECL_LOCAL_FUNCTION_P (fn))
7195 koenig_p = true;
7196 if (!any_type_dependent_arguments_p (args))
7197 postfix_expression
7198 = perform_koenig_lookup (postfix_expression, args,
7199 complain);
7204 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7206 tree instance = TREE_OPERAND (postfix_expression, 0);
7207 tree fn = TREE_OPERAND (postfix_expression, 1);
7209 if (processing_template_decl
7210 && (type_dependent_object_expression_p (instance)
7211 || (!BASELINK_P (fn)
7212 && TREE_CODE (fn) != FIELD_DECL)
7213 || type_dependent_expression_p (fn)
7214 || any_type_dependent_arguments_p (args)))
7216 maybe_generic_this_capture (instance, fn);
7217 postfix_expression
7218 = build_min_nt_call_vec (postfix_expression, args);
7219 release_tree_vector (args);
7220 break;
7223 if (BASELINK_P (fn))
7225 postfix_expression
7226 = (build_new_method_call
7227 (instance, fn, &args, NULL_TREE,
7228 (idk == CP_ID_KIND_QUALIFIED
7229 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7230 : LOOKUP_NORMAL),
7231 /*fn_p=*/NULL,
7232 complain));
7234 else
7235 postfix_expression
7236 = finish_call_expr (postfix_expression, &args,
7237 /*disallow_virtual=*/false,
7238 /*koenig_p=*/false,
7239 complain);
7241 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7242 || TREE_CODE (postfix_expression) == MEMBER_REF
7243 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7244 postfix_expression = (build_offset_ref_call_from_tree
7245 (postfix_expression, &args,
7246 complain));
7247 else if (idk == CP_ID_KIND_QUALIFIED)
7248 /* A call to a static class member, or a namespace-scope
7249 function. */
7250 postfix_expression
7251 = finish_call_expr (postfix_expression, &args,
7252 /*disallow_virtual=*/true,
7253 koenig_p,
7254 complain);
7255 else
7256 /* All other function calls. */
7257 postfix_expression
7258 = finish_call_expr (postfix_expression, &args,
7259 /*disallow_virtual=*/false,
7260 koenig_p,
7261 complain);
7263 if (close_paren_loc != UNKNOWN_LOCATION)
7265 location_t combined_loc = make_location (token->location,
7266 start_loc,
7267 close_paren_loc);
7268 postfix_expression.set_location (combined_loc);
7271 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7272 idk = CP_ID_KIND_NONE;
7274 release_tree_vector (args);
7276 break;
7278 case CPP_DOT:
7279 case CPP_DEREF:
7280 /* postfix-expression . template [opt] id-expression
7281 postfix-expression . pseudo-destructor-name
7282 postfix-expression -> template [opt] id-expression
7283 postfix-expression -> pseudo-destructor-name */
7285 /* Consume the `.' or `->' operator. */
7286 cp_lexer_consume_token (parser->lexer);
7288 postfix_expression
7289 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7290 postfix_expression,
7291 false, &idk, loc);
7293 is_member_access = true;
7294 break;
7296 case CPP_PLUS_PLUS:
7297 /* postfix-expression ++ */
7298 /* Consume the `++' token. */
7299 cp_lexer_consume_token (parser->lexer);
7300 /* Generate a representation for the complete expression. */
7301 postfix_expression
7302 = finish_increment_expr (postfix_expression,
7303 POSTINCREMENT_EXPR);
7304 /* Increments may not appear in constant-expressions. */
7305 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7306 postfix_expression = error_mark_node;
7307 idk = CP_ID_KIND_NONE;
7308 is_member_access = false;
7309 break;
7311 case CPP_MINUS_MINUS:
7312 /* postfix-expression -- */
7313 /* Consume the `--' token. */
7314 cp_lexer_consume_token (parser->lexer);
7315 /* Generate a representation for the complete expression. */
7316 postfix_expression
7317 = finish_increment_expr (postfix_expression,
7318 POSTDECREMENT_EXPR);
7319 /* Decrements may not appear in constant-expressions. */
7320 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7321 postfix_expression = error_mark_node;
7322 idk = CP_ID_KIND_NONE;
7323 is_member_access = false;
7324 break;
7326 default:
7327 if (pidk_return != NULL)
7328 * pidk_return = idk;
7329 if (member_access_only_p)
7330 return is_member_access
7331 ? postfix_expression
7332 : cp_expr (error_mark_node);
7333 else
7334 return postfix_expression;
7338 /* We should never get here. */
7339 gcc_unreachable ();
7340 return error_mark_node;
7343 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7344 by cp_parser_builtin_offsetof. We're looking for
7346 postfix-expression [ expression ]
7347 postfix-expression [ braced-init-list ] (C++11)
7349 FOR_OFFSETOF is set if we're being called in that context, which
7350 changes how we deal with integer constant expressions. */
7352 static tree
7353 cp_parser_postfix_open_square_expression (cp_parser *parser,
7354 tree postfix_expression,
7355 bool for_offsetof,
7356 bool decltype_p)
7358 tree index = NULL_TREE;
7359 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7360 bool saved_greater_than_is_operator_p;
7362 /* Consume the `[' token. */
7363 cp_lexer_consume_token (parser->lexer);
7365 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7366 parser->greater_than_is_operator_p = true;
7368 /* Parse the index expression. */
7369 /* ??? For offsetof, there is a question of what to allow here. If
7370 offsetof is not being used in an integral constant expression context,
7371 then we *could* get the right answer by computing the value at runtime.
7372 If we are in an integral constant expression context, then we might
7373 could accept any constant expression; hard to say without analysis.
7374 Rather than open the barn door too wide right away, allow only integer
7375 constant expressions here. */
7376 if (for_offsetof)
7377 index = cp_parser_constant_expression (parser);
7378 else
7380 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7382 bool expr_nonconst_p;
7383 cp_lexer_set_source_position (parser->lexer);
7384 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7385 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7387 else
7388 index = cp_parser_expression (parser);
7391 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7393 /* Look for the closing `]'. */
7394 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7396 /* Build the ARRAY_REF. */
7397 postfix_expression = grok_array_decl (loc, postfix_expression,
7398 index, decltype_p);
7400 /* When not doing offsetof, array references are not permitted in
7401 constant-expressions. */
7402 if (!for_offsetof
7403 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7404 postfix_expression = error_mark_node;
7406 return postfix_expression;
7409 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7410 dereference of incomplete type, returns true if error_mark_node should
7411 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7412 and *DEPENDENT_P. */
7414 bool
7415 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7416 bool *dependent_p)
7418 /* In a template, be permissive by treating an object expression
7419 of incomplete type as dependent (after a pedwarn). */
7420 diagnostic_t kind = (processing_template_decl
7421 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7423 switch (TREE_CODE (*postfix_expression))
7425 case CAST_EXPR:
7426 case REINTERPRET_CAST_EXPR:
7427 case CONST_CAST_EXPR:
7428 case STATIC_CAST_EXPR:
7429 case DYNAMIC_CAST_EXPR:
7430 case IMPLICIT_CONV_EXPR:
7431 case VIEW_CONVERT_EXPR:
7432 case NON_LVALUE_EXPR:
7433 kind = DK_ERROR;
7434 break;
7435 case OVERLOAD:
7436 /* Don't emit any diagnostic for OVERLOADs. */
7437 kind = DK_IGNORED;
7438 break;
7439 default:
7440 /* Avoid clobbering e.g. DECLs. */
7441 if (!EXPR_P (*postfix_expression))
7442 kind = DK_ERROR;
7443 break;
7446 if (kind == DK_IGNORED)
7447 return false;
7449 location_t exploc = location_of (*postfix_expression);
7450 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7451 if (!MAYBE_CLASS_TYPE_P (*scope))
7452 return true;
7453 if (kind == DK_ERROR)
7454 *scope = *postfix_expression = error_mark_node;
7455 else if (processing_template_decl)
7457 *dependent_p = true;
7458 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7460 return false;
7463 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7464 by cp_parser_builtin_offsetof. We're looking for
7466 postfix-expression . template [opt] id-expression
7467 postfix-expression . pseudo-destructor-name
7468 postfix-expression -> template [opt] id-expression
7469 postfix-expression -> pseudo-destructor-name
7471 FOR_OFFSETOF is set if we're being called in that context. That sorta
7472 limits what of the above we'll actually accept, but nevermind.
7473 TOKEN_TYPE is the "." or "->" token, which will already have been
7474 removed from the stream. */
7476 static tree
7477 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7478 enum cpp_ttype token_type,
7479 cp_expr postfix_expression,
7480 bool for_offsetof, cp_id_kind *idk,
7481 location_t location)
7483 tree name;
7484 bool dependent_p;
7485 bool pseudo_destructor_p;
7486 tree scope = NULL_TREE;
7487 location_t start_loc = postfix_expression.get_start ();
7489 /* If this is a `->' operator, dereference the pointer. */
7490 if (token_type == CPP_DEREF)
7491 postfix_expression = build_x_arrow (location, postfix_expression,
7492 tf_warning_or_error);
7493 /* Check to see whether or not the expression is type-dependent and
7494 not the current instantiation. */
7495 dependent_p = type_dependent_object_expression_p (postfix_expression);
7496 /* The identifier following the `->' or `.' is not qualified. */
7497 parser->scope = NULL_TREE;
7498 parser->qualifying_scope = NULL_TREE;
7499 parser->object_scope = NULL_TREE;
7500 *idk = CP_ID_KIND_NONE;
7502 /* Enter the scope corresponding to the type of the object
7503 given by the POSTFIX_EXPRESSION. */
7504 if (!dependent_p)
7506 scope = TREE_TYPE (postfix_expression);
7507 /* According to the standard, no expression should ever have
7508 reference type. Unfortunately, we do not currently match
7509 the standard in this respect in that our internal representation
7510 of an expression may have reference type even when the standard
7511 says it does not. Therefore, we have to manually obtain the
7512 underlying type here. */
7513 scope = non_reference (scope);
7514 /* The type of the POSTFIX_EXPRESSION must be complete. */
7515 /* Unlike the object expression in other contexts, *this is not
7516 required to be of complete type for purposes of class member
7517 access (5.2.5) outside the member function body. */
7518 if (postfix_expression != current_class_ref
7519 && scope != error_mark_node
7520 && !currently_open_class (scope))
7522 scope = complete_type (scope);
7523 if (!COMPLETE_TYPE_P (scope)
7524 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7525 &dependent_p))
7526 return error_mark_node;
7529 if (!dependent_p)
7531 /* Let the name lookup machinery know that we are processing a
7532 class member access expression. */
7533 parser->context->object_type = scope;
7534 /* If something went wrong, we want to be able to discern that case,
7535 as opposed to the case where there was no SCOPE due to the type
7536 of expression being dependent. */
7537 if (!scope)
7538 scope = error_mark_node;
7539 /* If the SCOPE was erroneous, make the various semantic analysis
7540 functions exit quickly -- and without issuing additional error
7541 messages. */
7542 if (scope == error_mark_node)
7543 postfix_expression = error_mark_node;
7547 if (dependent_p)
7548 /* Tell cp_parser_lookup_name that there was an object, even though it's
7549 type-dependent. */
7550 parser->context->object_type = unknown_type_node;
7552 /* Assume this expression is not a pseudo-destructor access. */
7553 pseudo_destructor_p = false;
7555 /* If the SCOPE is a scalar type, then, if this is a valid program,
7556 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7557 is type dependent, it can be pseudo-destructor-name or something else.
7558 Try to parse it as pseudo-destructor-name first. */
7559 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7561 tree s;
7562 tree type;
7564 cp_parser_parse_tentatively (parser);
7565 /* Parse the pseudo-destructor-name. */
7566 s = NULL_TREE;
7567 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7568 &s, &type);
7569 if (dependent_p
7570 && (cp_parser_error_occurred (parser)
7571 || !SCALAR_TYPE_P (type)))
7572 cp_parser_abort_tentative_parse (parser);
7573 else if (cp_parser_parse_definitely (parser))
7575 pseudo_destructor_p = true;
7576 postfix_expression
7577 = finish_pseudo_destructor_expr (postfix_expression,
7578 s, type, location);
7582 if (!pseudo_destructor_p)
7584 /* If the SCOPE is not a scalar type, we are looking at an
7585 ordinary class member access expression, rather than a
7586 pseudo-destructor-name. */
7587 bool template_p;
7588 cp_token *token = cp_lexer_peek_token (parser->lexer);
7589 /* Parse the id-expression. */
7590 name = (cp_parser_id_expression
7591 (parser,
7592 cp_parser_optional_template_keyword (parser),
7593 /*check_dependency_p=*/true,
7594 &template_p,
7595 /*declarator_p=*/false,
7596 /*optional_p=*/false));
7597 /* In general, build a SCOPE_REF if the member name is qualified.
7598 However, if the name was not dependent and has already been
7599 resolved; there is no need to build the SCOPE_REF. For example;
7601 struct X { void f(); };
7602 template <typename T> void f(T* t) { t->X::f(); }
7604 Even though "t" is dependent, "X::f" is not and has been resolved
7605 to a BASELINK; there is no need to include scope information. */
7607 /* But we do need to remember that there was an explicit scope for
7608 virtual function calls. */
7609 if (parser->scope)
7610 *idk = CP_ID_KIND_QUALIFIED;
7612 /* If the name is a template-id that names a type, we will get a
7613 TYPE_DECL here. That is invalid code. */
7614 if (TREE_CODE (name) == TYPE_DECL)
7616 error_at (token->location, "invalid use of %qD", name);
7617 postfix_expression = error_mark_node;
7619 else
7621 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7623 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7625 error_at (token->location, "%<%D::%D%> is not a class member",
7626 parser->scope, name);
7627 postfix_expression = error_mark_node;
7629 else
7630 name = build_qualified_name (/*type=*/NULL_TREE,
7631 parser->scope,
7632 name,
7633 template_p);
7634 parser->scope = NULL_TREE;
7635 parser->qualifying_scope = NULL_TREE;
7636 parser->object_scope = NULL_TREE;
7638 if (parser->scope && name && BASELINK_P (name))
7639 adjust_result_of_qualified_name_lookup
7640 (name, parser->scope, scope);
7641 postfix_expression
7642 = finish_class_member_access_expr (postfix_expression, name,
7643 template_p,
7644 tf_warning_or_error);
7645 /* Build a location e.g.:
7646 ptr->access_expr
7647 ~~~^~~~~~~~~~~~~
7648 where the caret is at the deref token, ranging from
7649 the start of postfix_expression to the end of the access expr. */
7650 location_t end_loc
7651 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7652 location_t combined_loc
7653 = make_location (input_location, start_loc, end_loc);
7654 protected_set_expr_location (postfix_expression, combined_loc);
7658 /* We no longer need to look up names in the scope of the object on
7659 the left-hand side of the `.' or `->' operator. */
7660 parser->context->object_type = NULL_TREE;
7662 /* Outside of offsetof, these operators may not appear in
7663 constant-expressions. */
7664 if (!for_offsetof
7665 && (cp_parser_non_integral_constant_expression
7666 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7667 postfix_expression = error_mark_node;
7669 return postfix_expression;
7672 /* Parse a parenthesized expression-list.
7674 expression-list:
7675 assignment-expression
7676 expression-list, assignment-expression
7678 attribute-list:
7679 expression-list
7680 identifier
7681 identifier, expression-list
7683 CAST_P is true if this expression is the target of a cast.
7685 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7686 argument pack.
7688 WRAP_LOCATIONS_P is true if expressions within this list for which
7689 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7690 their source locations.
7692 Returns a vector of trees. Each element is a representation of an
7693 assignment-expression. NULL is returned if the ( and or ) are
7694 missing. An empty, but allocated, vector is returned on no
7695 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7696 if we are parsing an attribute list for an attribute that wants a
7697 plain identifier argument, normal_attr for an attribute that wants
7698 an expression, or non_attr if we aren't parsing an attribute list. If
7699 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7700 not all of the expressions in the list were constant.
7701 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7702 will be written to with the location of the closing parenthesis. If
7703 an error occurs, it may or may not be written to. */
7705 static vec<tree, va_gc> *
7706 cp_parser_parenthesized_expression_list (cp_parser* parser,
7707 int is_attribute_list,
7708 bool cast_p,
7709 bool allow_expansion_p,
7710 bool *non_constant_p,
7711 location_t *close_paren_loc,
7712 bool wrap_locations_p)
7714 vec<tree, va_gc> *expression_list;
7715 bool fold_expr_p = is_attribute_list != non_attr;
7716 tree identifier = NULL_TREE;
7717 bool saved_greater_than_is_operator_p;
7719 /* Assume all the expressions will be constant. */
7720 if (non_constant_p)
7721 *non_constant_p = false;
7723 matching_parens parens;
7724 if (!parens.require_open (parser))
7725 return NULL;
7727 expression_list = make_tree_vector ();
7729 /* Within a parenthesized expression, a `>' token is always
7730 the greater-than operator. */
7731 saved_greater_than_is_operator_p
7732 = parser->greater_than_is_operator_p;
7733 parser->greater_than_is_operator_p = true;
7735 cp_expr expr (NULL_TREE);
7737 /* Consume expressions until there are no more. */
7738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7739 while (true)
7741 /* At the beginning of attribute lists, check to see if the
7742 next token is an identifier. */
7743 if (is_attribute_list == id_attr
7744 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7746 cp_token *token;
7748 /* Consume the identifier. */
7749 token = cp_lexer_consume_token (parser->lexer);
7750 /* Save the identifier. */
7751 identifier = token->u.value;
7753 else
7755 bool expr_non_constant_p;
7757 /* Parse the next assignment-expression. */
7758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7760 /* A braced-init-list. */
7761 cp_lexer_set_source_position (parser->lexer);
7762 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7763 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7764 if (non_constant_p && expr_non_constant_p)
7765 *non_constant_p = true;
7767 else if (non_constant_p)
7769 expr = (cp_parser_constant_expression
7770 (parser, /*allow_non_constant_p=*/true,
7771 &expr_non_constant_p));
7772 if (expr_non_constant_p)
7773 *non_constant_p = true;
7775 else
7776 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7777 cast_p);
7779 if (fold_expr_p)
7780 expr = instantiate_non_dependent_expr (expr);
7782 /* If we have an ellipsis, then this is an expression
7783 expansion. */
7784 if (allow_expansion_p
7785 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7787 /* Consume the `...'. */
7788 cp_lexer_consume_token (parser->lexer);
7790 /* Build the argument pack. */
7791 expr = make_pack_expansion (expr);
7794 if (wrap_locations_p)
7795 expr.maybe_add_location_wrapper ();
7797 /* Add it to the list. We add error_mark_node
7798 expressions to the list, so that we can still tell if
7799 the correct form for a parenthesized expression-list
7800 is found. That gives better errors. */
7801 vec_safe_push (expression_list, expr.get_value ());
7803 if (expr == error_mark_node)
7804 goto skip_comma;
7807 /* After the first item, attribute lists look the same as
7808 expression lists. */
7809 is_attribute_list = non_attr;
7811 get_comma:;
7812 /* If the next token isn't a `,', then we are done. */
7813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7814 break;
7816 /* Otherwise, consume the `,' and keep going. */
7817 cp_lexer_consume_token (parser->lexer);
7820 if (close_paren_loc)
7821 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7823 if (!parens.require_close (parser))
7825 int ending;
7827 skip_comma:;
7828 /* We try and resync to an unnested comma, as that will give the
7829 user better diagnostics. */
7830 ending = cp_parser_skip_to_closing_parenthesis (parser,
7831 /*recovering=*/true,
7832 /*or_comma=*/true,
7833 /*consume_paren=*/true);
7834 if (ending < 0)
7835 goto get_comma;
7836 if (!ending)
7838 parser->greater_than_is_operator_p
7839 = saved_greater_than_is_operator_p;
7840 return NULL;
7844 parser->greater_than_is_operator_p
7845 = saved_greater_than_is_operator_p;
7847 if (identifier)
7848 vec_safe_insert (expression_list, 0, identifier);
7850 return expression_list;
7853 /* Parse a pseudo-destructor-name.
7855 pseudo-destructor-name:
7856 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7857 :: [opt] nested-name-specifier template template-id :: ~ type-name
7858 :: [opt] nested-name-specifier [opt] ~ type-name
7860 If either of the first two productions is used, sets *SCOPE to the
7861 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7862 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7863 or ERROR_MARK_NODE if the parse fails. */
7865 static void
7866 cp_parser_pseudo_destructor_name (cp_parser* parser,
7867 tree object,
7868 tree* scope,
7869 tree* type)
7871 bool nested_name_specifier_p;
7873 /* Handle ~auto. */
7874 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7875 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7876 && !type_dependent_expression_p (object))
7878 if (cxx_dialect < cxx14)
7879 pedwarn (input_location, 0,
7880 "%<~auto%> only available with "
7881 "-std=c++14 or -std=gnu++14");
7882 cp_lexer_consume_token (parser->lexer);
7883 cp_lexer_consume_token (parser->lexer);
7884 *scope = NULL_TREE;
7885 *type = TREE_TYPE (object);
7886 return;
7889 /* Assume that things will not work out. */
7890 *type = error_mark_node;
7892 /* Look for the optional `::' operator. */
7893 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7894 /* Look for the optional nested-name-specifier. */
7895 nested_name_specifier_p
7896 = (cp_parser_nested_name_specifier_opt (parser,
7897 /*typename_keyword_p=*/false,
7898 /*check_dependency_p=*/true,
7899 /*type_p=*/false,
7900 /*is_declaration=*/false)
7901 != NULL_TREE);
7902 /* Now, if we saw a nested-name-specifier, we might be doing the
7903 second production. */
7904 if (nested_name_specifier_p
7905 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7907 /* Consume the `template' keyword. */
7908 cp_lexer_consume_token (parser->lexer);
7909 /* Parse the template-id. */
7910 cp_parser_template_id (parser,
7911 /*template_keyword_p=*/true,
7912 /*check_dependency_p=*/false,
7913 class_type,
7914 /*is_declaration=*/true);
7915 /* Look for the `::' token. */
7916 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7918 /* If the next token is not a `~', then there might be some
7919 additional qualification. */
7920 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7922 /* At this point, we're looking for "type-name :: ~". The type-name
7923 must not be a class-name, since this is a pseudo-destructor. So,
7924 it must be either an enum-name, or a typedef-name -- both of which
7925 are just identifiers. So, we peek ahead to check that the "::"
7926 and "~" tokens are present; if they are not, then we can avoid
7927 calling type_name. */
7928 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7929 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7930 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7932 cp_parser_error (parser, "non-scalar type");
7933 return;
7936 /* Look for the type-name. */
7937 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7938 if (*scope == error_mark_node)
7939 return;
7941 /* Look for the `::' token. */
7942 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7944 else
7945 *scope = NULL_TREE;
7947 /* Look for the `~'. */
7948 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7950 /* Once we see the ~, this has to be a pseudo-destructor. */
7951 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7952 cp_parser_commit_to_topmost_tentative_parse (parser);
7954 /* Look for the type-name again. We are not responsible for
7955 checking that it matches the first type-name. */
7956 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7959 /* Parse a unary-expression.
7961 unary-expression:
7962 postfix-expression
7963 ++ cast-expression
7964 -- cast-expression
7965 unary-operator cast-expression
7966 sizeof unary-expression
7967 sizeof ( type-id )
7968 alignof ( type-id ) [C++0x]
7969 new-expression
7970 delete-expression
7972 GNU Extensions:
7974 unary-expression:
7975 __extension__ cast-expression
7976 __alignof__ unary-expression
7977 __alignof__ ( type-id )
7978 alignof unary-expression [C++0x]
7979 __real__ cast-expression
7980 __imag__ cast-expression
7981 && identifier
7982 sizeof ( type-id ) { initializer-list , [opt] }
7983 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7984 __alignof__ ( type-id ) { initializer-list , [opt] }
7986 ADDRESS_P is true iff the unary-expression is appearing as the
7987 operand of the `&' operator. CAST_P is true if this expression is
7988 the target of a cast.
7990 Returns a representation of the expression. */
7992 static cp_expr
7993 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7994 bool address_p, bool cast_p, bool decltype_p)
7996 cp_token *token;
7997 enum tree_code unary_operator;
7999 /* Peek at the next token. */
8000 token = cp_lexer_peek_token (parser->lexer);
8001 /* Some keywords give away the kind of expression. */
8002 if (token->type == CPP_KEYWORD)
8004 enum rid keyword = token->keyword;
8006 switch (keyword)
8008 case RID_ALIGNOF:
8009 case RID_SIZEOF:
8011 tree operand, ret;
8012 enum tree_code op;
8013 location_t start_loc = token->location;
8015 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8016 bool std_alignof = id_equal (token->u.value, "alignof");
8018 /* Consume the token. */
8019 cp_lexer_consume_token (parser->lexer);
8020 /* Parse the operand. */
8021 operand = cp_parser_sizeof_operand (parser, keyword);
8023 if (TYPE_P (operand))
8024 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8025 true);
8026 else
8028 /* ISO C++ defines alignof only with types, not with
8029 expressions. So pedwarn if alignof is used with a non-
8030 type expression. However, __alignof__ is ok. */
8031 if (std_alignof)
8032 pedwarn (token->location, OPT_Wpedantic,
8033 "ISO C++ does not allow %<alignof%> "
8034 "with a non-type");
8036 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8038 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8039 SIZEOF_EXPR with the original operand. */
8040 if (op == SIZEOF_EXPR && ret != error_mark_node)
8042 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8044 if (!processing_template_decl && TYPE_P (operand))
8046 ret = build_min (SIZEOF_EXPR, size_type_node,
8047 build1 (NOP_EXPR, operand,
8048 error_mark_node));
8049 SIZEOF_EXPR_TYPE_P (ret) = 1;
8051 else
8052 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8053 TREE_SIDE_EFFECTS (ret) = 0;
8054 TREE_READONLY (ret) = 1;
8058 /* Construct a location e.g. :
8059 alignof (expr)
8060 ^~~~~~~~~~~~~~
8061 with start == caret at the start of the "alignof"/"sizeof"
8062 token, with the endpoint at the final closing paren. */
8063 location_t finish_loc
8064 = cp_lexer_previous_token (parser->lexer)->location;
8065 location_t compound_loc
8066 = make_location (start_loc, start_loc, finish_loc);
8068 cp_expr ret_expr (ret);
8069 ret_expr.set_location (compound_loc);
8070 ret_expr = ret_expr.maybe_add_location_wrapper ();
8071 return ret_expr;
8074 case RID_NEW:
8075 return cp_parser_new_expression (parser);
8077 case RID_DELETE:
8078 return cp_parser_delete_expression (parser);
8080 case RID_EXTENSION:
8082 /* The saved value of the PEDANTIC flag. */
8083 int saved_pedantic;
8084 tree expr;
8086 /* Save away the PEDANTIC flag. */
8087 cp_parser_extension_opt (parser, &saved_pedantic);
8088 /* Parse the cast-expression. */
8089 expr = cp_parser_simple_cast_expression (parser);
8090 /* Restore the PEDANTIC flag. */
8091 pedantic = saved_pedantic;
8093 return expr;
8096 case RID_REALPART:
8097 case RID_IMAGPART:
8099 tree expression;
8101 /* Consume the `__real__' or `__imag__' token. */
8102 cp_lexer_consume_token (parser->lexer);
8103 /* Parse the cast-expression. */
8104 expression = cp_parser_simple_cast_expression (parser);
8105 /* Create the complete representation. */
8106 return build_x_unary_op (token->location,
8107 (keyword == RID_REALPART
8108 ? REALPART_EXPR : IMAGPART_EXPR),
8109 expression,
8110 tf_warning_or_error);
8112 break;
8114 case RID_TRANSACTION_ATOMIC:
8115 case RID_TRANSACTION_RELAXED:
8116 return cp_parser_transaction_expression (parser, keyword);
8118 case RID_NOEXCEPT:
8120 tree expr;
8121 const char *saved_message;
8122 bool saved_integral_constant_expression_p;
8123 bool saved_non_integral_constant_expression_p;
8124 bool saved_greater_than_is_operator_p;
8126 location_t start_loc = token->location;
8128 cp_lexer_consume_token (parser->lexer);
8129 matching_parens parens;
8130 parens.require_open (parser);
8132 saved_message = parser->type_definition_forbidden_message;
8133 parser->type_definition_forbidden_message
8134 = G_("types may not be defined in %<noexcept%> expressions");
8136 saved_integral_constant_expression_p
8137 = parser->integral_constant_expression_p;
8138 saved_non_integral_constant_expression_p
8139 = parser->non_integral_constant_expression_p;
8140 parser->integral_constant_expression_p = false;
8142 saved_greater_than_is_operator_p
8143 = parser->greater_than_is_operator_p;
8144 parser->greater_than_is_operator_p = true;
8146 ++cp_unevaluated_operand;
8147 ++c_inhibit_evaluation_warnings;
8148 ++cp_noexcept_operand;
8149 expr = cp_parser_expression (parser);
8150 --cp_noexcept_operand;
8151 --c_inhibit_evaluation_warnings;
8152 --cp_unevaluated_operand;
8154 parser->greater_than_is_operator_p
8155 = saved_greater_than_is_operator_p;
8157 parser->integral_constant_expression_p
8158 = saved_integral_constant_expression_p;
8159 parser->non_integral_constant_expression_p
8160 = saved_non_integral_constant_expression_p;
8162 parser->type_definition_forbidden_message = saved_message;
8164 location_t finish_loc
8165 = cp_lexer_peek_token (parser->lexer)->location;
8166 parens.require_close (parser);
8168 /* Construct a location of the form:
8169 noexcept (expr)
8170 ^~~~~~~~~~~~~~~
8171 with start == caret, finishing at the close-paren. */
8172 location_t noexcept_loc
8173 = make_location (start_loc, start_loc, finish_loc);
8175 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8176 noexcept_loc);
8179 default:
8180 break;
8184 /* Look for the `:: new' and `:: delete', which also signal the
8185 beginning of a new-expression, or delete-expression,
8186 respectively. If the next token is `::', then it might be one of
8187 these. */
8188 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8190 enum rid keyword;
8192 /* See if the token after the `::' is one of the keywords in
8193 which we're interested. */
8194 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8195 /* If it's `new', we have a new-expression. */
8196 if (keyword == RID_NEW)
8197 return cp_parser_new_expression (parser);
8198 /* Similarly, for `delete'. */
8199 else if (keyword == RID_DELETE)
8200 return cp_parser_delete_expression (parser);
8203 /* Look for a unary operator. */
8204 unary_operator = cp_parser_unary_operator (token);
8205 /* The `++' and `--' operators can be handled similarly, even though
8206 they are not technically unary-operators in the grammar. */
8207 if (unary_operator == ERROR_MARK)
8209 if (token->type == CPP_PLUS_PLUS)
8210 unary_operator = PREINCREMENT_EXPR;
8211 else if (token->type == CPP_MINUS_MINUS)
8212 unary_operator = PREDECREMENT_EXPR;
8213 /* Handle the GNU address-of-label extension. */
8214 else if (cp_parser_allow_gnu_extensions_p (parser)
8215 && token->type == CPP_AND_AND)
8217 tree identifier;
8218 tree expression;
8219 location_t start_loc = token->location;
8221 /* Consume the '&&' token. */
8222 cp_lexer_consume_token (parser->lexer);
8223 /* Look for the identifier. */
8224 location_t finish_loc
8225 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8226 identifier = cp_parser_identifier (parser);
8227 /* Construct a location of the form:
8228 &&label
8229 ^~~~~~~
8230 with caret==start at the "&&", finish at the end of the label. */
8231 location_t combined_loc
8232 = make_location (start_loc, start_loc, finish_loc);
8233 /* Create an expression representing the address. */
8234 expression = finish_label_address_expr (identifier, combined_loc);
8235 if (cp_parser_non_integral_constant_expression (parser,
8236 NIC_ADDR_LABEL))
8237 expression = error_mark_node;
8238 return expression;
8241 if (unary_operator != ERROR_MARK)
8243 cp_expr cast_expression;
8244 cp_expr expression = error_mark_node;
8245 non_integral_constant non_constant_p = NIC_NONE;
8246 location_t loc = token->location;
8247 tsubst_flags_t complain = complain_flags (decltype_p);
8249 /* Consume the operator token. */
8250 token = cp_lexer_consume_token (parser->lexer);
8251 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8253 /* Parse the cast-expression. */
8254 cast_expression
8255 = cp_parser_cast_expression (parser,
8256 unary_operator == ADDR_EXPR,
8257 /*cast_p=*/false,
8258 /*decltype*/false,
8259 pidk);
8261 /* Make a location:
8262 OP_TOKEN CAST_EXPRESSION
8263 ^~~~~~~~~~~~~~~~~~~~~~~~~
8264 with start==caret at the operator token, and
8265 extending to the end of the cast_expression. */
8266 loc = make_location (loc, loc, cast_expression.get_finish ());
8268 /* Now, build an appropriate representation. */
8269 switch (unary_operator)
8271 case INDIRECT_REF:
8272 non_constant_p = NIC_STAR;
8273 expression = build_x_indirect_ref (loc, cast_expression,
8274 RO_UNARY_STAR,
8275 complain);
8276 /* TODO: build_x_indirect_ref does not always honor the
8277 location, so ensure it is set. */
8278 expression.set_location (loc);
8279 break;
8281 case ADDR_EXPR:
8282 non_constant_p = NIC_ADDR;
8283 /* Fall through. */
8284 case BIT_NOT_EXPR:
8285 expression = build_x_unary_op (loc, unary_operator,
8286 cast_expression,
8287 complain);
8288 /* TODO: build_x_unary_op does not always honor the location,
8289 so ensure it is set. */
8290 expression.set_location (loc);
8291 break;
8293 case PREINCREMENT_EXPR:
8294 case PREDECREMENT_EXPR:
8295 non_constant_p = unary_operator == PREINCREMENT_EXPR
8296 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8297 /* Fall through. */
8298 case NEGATE_EXPR:
8299 /* Immediately fold negation of a constant, unless the constant is 0
8300 (since -0 == 0) or it would overflow. */
8301 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8302 && CONSTANT_CLASS_P (cast_expression)
8303 && !integer_zerop (cast_expression)
8304 && !TREE_OVERFLOW (cast_expression))
8306 tree folded = fold_build1 (unary_operator,
8307 TREE_TYPE (cast_expression),
8308 cast_expression);
8309 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8311 expression = cp_expr (folded, loc);
8312 break;
8315 /* Fall through. */
8316 case UNARY_PLUS_EXPR:
8317 case TRUTH_NOT_EXPR:
8318 expression = finish_unary_op_expr (loc, unary_operator,
8319 cast_expression, complain);
8320 break;
8322 default:
8323 gcc_unreachable ();
8326 if (non_constant_p != NIC_NONE
8327 && cp_parser_non_integral_constant_expression (parser,
8328 non_constant_p))
8329 expression = error_mark_node;
8331 return expression;
8334 return cp_parser_postfix_expression (parser, address_p, cast_p,
8335 /*member_access_only_p=*/false,
8336 decltype_p,
8337 pidk);
8340 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8341 unary-operator, the corresponding tree code is returned. */
8343 static enum tree_code
8344 cp_parser_unary_operator (cp_token* token)
8346 switch (token->type)
8348 case CPP_MULT:
8349 return INDIRECT_REF;
8351 case CPP_AND:
8352 return ADDR_EXPR;
8354 case CPP_PLUS:
8355 return UNARY_PLUS_EXPR;
8357 case CPP_MINUS:
8358 return NEGATE_EXPR;
8360 case CPP_NOT:
8361 return TRUTH_NOT_EXPR;
8363 case CPP_COMPL:
8364 return BIT_NOT_EXPR;
8366 default:
8367 return ERROR_MARK;
8371 /* Parse a new-expression.
8373 new-expression:
8374 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8375 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8377 Returns a representation of the expression. */
8379 static tree
8380 cp_parser_new_expression (cp_parser* parser)
8382 bool global_scope_p;
8383 vec<tree, va_gc> *placement;
8384 tree type;
8385 vec<tree, va_gc> *initializer;
8386 tree nelts = NULL_TREE;
8387 tree ret;
8389 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8391 /* Look for the optional `::' operator. */
8392 global_scope_p
8393 = (cp_parser_global_scope_opt (parser,
8394 /*current_scope_valid_p=*/false)
8395 != NULL_TREE);
8396 /* Look for the `new' operator. */
8397 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8398 /* There's no easy way to tell a new-placement from the
8399 `( type-id )' construct. */
8400 cp_parser_parse_tentatively (parser);
8401 /* Look for a new-placement. */
8402 placement = cp_parser_new_placement (parser);
8403 /* If that didn't work out, there's no new-placement. */
8404 if (!cp_parser_parse_definitely (parser))
8406 if (placement != NULL)
8407 release_tree_vector (placement);
8408 placement = NULL;
8411 /* If the next token is a `(', then we have a parenthesized
8412 type-id. */
8413 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8415 cp_token *token;
8416 const char *saved_message = parser->type_definition_forbidden_message;
8418 /* Consume the `('. */
8419 matching_parens parens;
8420 parens.consume_open (parser);
8422 /* Parse the type-id. */
8423 parser->type_definition_forbidden_message
8424 = G_("types may not be defined in a new-expression");
8426 type_id_in_expr_sentinel s (parser);
8427 type = cp_parser_type_id (parser);
8429 parser->type_definition_forbidden_message = saved_message;
8431 /* Look for the closing `)'. */
8432 parens.require_close (parser);
8433 token = cp_lexer_peek_token (parser->lexer);
8434 /* There should not be a direct-new-declarator in this production,
8435 but GCC used to allowed this, so we check and emit a sensible error
8436 message for this case. */
8437 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8439 error_at (token->location,
8440 "array bound forbidden after parenthesized type-id");
8441 inform (token->location,
8442 "try removing the parentheses around the type-id");
8443 cp_parser_direct_new_declarator (parser);
8446 /* Otherwise, there must be a new-type-id. */
8447 else
8448 type = cp_parser_new_type_id (parser, &nelts);
8450 /* If the next token is a `(' or '{', then we have a new-initializer. */
8451 cp_token *token = cp_lexer_peek_token (parser->lexer);
8452 if (token->type == CPP_OPEN_PAREN
8453 || token->type == CPP_OPEN_BRACE)
8454 initializer = cp_parser_new_initializer (parser);
8455 else
8456 initializer = NULL;
8458 /* A new-expression may not appear in an integral constant
8459 expression. */
8460 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8461 ret = error_mark_node;
8462 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8463 of a new-type-id or type-id of a new-expression, the new-expression shall
8464 contain a new-initializer of the form ( assignment-expression )".
8465 Additionally, consistently with the spirit of DR 1467, we want to accept
8466 'new auto { 2 }' too. */
8467 else if ((ret = type_uses_auto (type))
8468 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8469 && (vec_safe_length (initializer) != 1
8470 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8471 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8473 error_at (token->location,
8474 "initialization of new-expression for type %<auto%> "
8475 "requires exactly one element");
8476 ret = error_mark_node;
8478 else
8480 /* Construct a location e.g.:
8481 ptr = new int[100]
8482 ^~~~~~~~~~~~
8483 with caret == start at the start of the "new" token, and the end
8484 at the end of the final token we consumed. */
8485 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8486 location_t end_loc = get_finish (end_tok->location);
8487 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8489 /* Create a representation of the new-expression. */
8490 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8491 tf_warning_or_error);
8492 protected_set_expr_location (ret, combined_loc);
8495 if (placement != NULL)
8496 release_tree_vector (placement);
8497 if (initializer != NULL)
8498 release_tree_vector (initializer);
8500 return ret;
8503 /* Parse a new-placement.
8505 new-placement:
8506 ( expression-list )
8508 Returns the same representation as for an expression-list. */
8510 static vec<tree, va_gc> *
8511 cp_parser_new_placement (cp_parser* parser)
8513 vec<tree, va_gc> *expression_list;
8515 /* Parse the expression-list. */
8516 expression_list = (cp_parser_parenthesized_expression_list
8517 (parser, non_attr, /*cast_p=*/false,
8518 /*allow_expansion_p=*/true,
8519 /*non_constant_p=*/NULL));
8521 if (expression_list && expression_list->is_empty ())
8522 error ("expected expression-list or type-id");
8524 return expression_list;
8527 /* Parse a new-type-id.
8529 new-type-id:
8530 type-specifier-seq new-declarator [opt]
8532 Returns the TYPE allocated. If the new-type-id indicates an array
8533 type, *NELTS is set to the number of elements in the last array
8534 bound; the TYPE will not include the last array bound. */
8536 static tree
8537 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8539 cp_decl_specifier_seq type_specifier_seq;
8540 cp_declarator *new_declarator;
8541 cp_declarator *declarator;
8542 cp_declarator *outer_declarator;
8543 const char *saved_message;
8545 /* The type-specifier sequence must not contain type definitions.
8546 (It cannot contain declarations of new types either, but if they
8547 are not definitions we will catch that because they are not
8548 complete.) */
8549 saved_message = parser->type_definition_forbidden_message;
8550 parser->type_definition_forbidden_message
8551 = G_("types may not be defined in a new-type-id");
8552 /* Parse the type-specifier-seq. */
8553 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8554 /*is_trailing_return=*/false,
8555 &type_specifier_seq);
8556 /* Restore the old message. */
8557 parser->type_definition_forbidden_message = saved_message;
8559 if (type_specifier_seq.type == error_mark_node)
8560 return error_mark_node;
8562 /* Parse the new-declarator. */
8563 new_declarator = cp_parser_new_declarator_opt (parser);
8565 /* Determine the number of elements in the last array dimension, if
8566 any. */
8567 *nelts = NULL_TREE;
8568 /* Skip down to the last array dimension. */
8569 declarator = new_declarator;
8570 outer_declarator = NULL;
8571 while (declarator && (declarator->kind == cdk_pointer
8572 || declarator->kind == cdk_ptrmem))
8574 outer_declarator = declarator;
8575 declarator = declarator->declarator;
8577 while (declarator
8578 && declarator->kind == cdk_array
8579 && declarator->declarator
8580 && declarator->declarator->kind == cdk_array)
8582 outer_declarator = declarator;
8583 declarator = declarator->declarator;
8586 if (declarator && declarator->kind == cdk_array)
8588 *nelts = declarator->u.array.bounds;
8589 if (*nelts == error_mark_node)
8590 *nelts = integer_one_node;
8592 if (outer_declarator)
8593 outer_declarator->declarator = declarator->declarator;
8594 else
8595 new_declarator = NULL;
8598 return groktypename (&type_specifier_seq, new_declarator, false);
8601 /* Parse an (optional) new-declarator.
8603 new-declarator:
8604 ptr-operator new-declarator [opt]
8605 direct-new-declarator
8607 Returns the declarator. */
8609 static cp_declarator *
8610 cp_parser_new_declarator_opt (cp_parser* parser)
8612 enum tree_code code;
8613 tree type, std_attributes = NULL_TREE;
8614 cp_cv_quals cv_quals;
8616 /* We don't know if there's a ptr-operator next, or not. */
8617 cp_parser_parse_tentatively (parser);
8618 /* Look for a ptr-operator. */
8619 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8620 /* If that worked, look for more new-declarators. */
8621 if (cp_parser_parse_definitely (parser))
8623 cp_declarator *declarator;
8625 /* Parse another optional declarator. */
8626 declarator = cp_parser_new_declarator_opt (parser);
8628 declarator = cp_parser_make_indirect_declarator
8629 (code, type, cv_quals, declarator, std_attributes);
8631 return declarator;
8634 /* If the next token is a `[', there is a direct-new-declarator. */
8635 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8636 return cp_parser_direct_new_declarator (parser);
8638 return NULL;
8641 /* Parse a direct-new-declarator.
8643 direct-new-declarator:
8644 [ expression ]
8645 direct-new-declarator [constant-expression]
8649 static cp_declarator *
8650 cp_parser_direct_new_declarator (cp_parser* parser)
8652 cp_declarator *declarator = NULL;
8654 while (true)
8656 tree expression;
8657 cp_token *token;
8659 /* Look for the opening `['. */
8660 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8662 token = cp_lexer_peek_token (parser->lexer);
8663 expression = cp_parser_expression (parser);
8664 /* The standard requires that the expression have integral
8665 type. DR 74 adds enumeration types. We believe that the
8666 real intent is that these expressions be handled like the
8667 expression in a `switch' condition, which also allows
8668 classes with a single conversion to integral or
8669 enumeration type. */
8670 if (!processing_template_decl)
8672 expression
8673 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8674 expression,
8675 /*complain=*/true);
8676 if (!expression)
8678 error_at (token->location,
8679 "expression in new-declarator must have integral "
8680 "or enumeration type");
8681 expression = error_mark_node;
8685 /* Look for the closing `]'. */
8686 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8688 /* Add this bound to the declarator. */
8689 declarator = make_array_declarator (declarator, expression);
8691 /* If the next token is not a `[', then there are no more
8692 bounds. */
8693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8694 break;
8697 return declarator;
8700 /* Parse a new-initializer.
8702 new-initializer:
8703 ( expression-list [opt] )
8704 braced-init-list
8706 Returns a representation of the expression-list. */
8708 static vec<tree, va_gc> *
8709 cp_parser_new_initializer (cp_parser* parser)
8711 vec<tree, va_gc> *expression_list;
8713 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8715 tree t;
8716 bool expr_non_constant_p;
8717 cp_lexer_set_source_position (parser->lexer);
8718 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8719 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8720 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8721 expression_list = make_tree_vector_single (t);
8723 else
8724 expression_list = (cp_parser_parenthesized_expression_list
8725 (parser, non_attr, /*cast_p=*/false,
8726 /*allow_expansion_p=*/true,
8727 /*non_constant_p=*/NULL));
8729 return expression_list;
8732 /* Parse a delete-expression.
8734 delete-expression:
8735 :: [opt] delete cast-expression
8736 :: [opt] delete [ ] cast-expression
8738 Returns a representation of the expression. */
8740 static tree
8741 cp_parser_delete_expression (cp_parser* parser)
8743 bool global_scope_p;
8744 bool array_p;
8745 tree expression;
8747 /* Look for the optional `::' operator. */
8748 global_scope_p
8749 = (cp_parser_global_scope_opt (parser,
8750 /*current_scope_valid_p=*/false)
8751 != NULL_TREE);
8752 /* Look for the `delete' keyword. */
8753 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8754 /* See if the array syntax is in use. */
8755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8757 /* Consume the `[' token. */
8758 cp_lexer_consume_token (parser->lexer);
8759 /* Look for the `]' token. */
8760 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8761 /* Remember that this is the `[]' construct. */
8762 array_p = true;
8764 else
8765 array_p = false;
8767 /* Parse the cast-expression. */
8768 expression = cp_parser_simple_cast_expression (parser);
8770 /* A delete-expression may not appear in an integral constant
8771 expression. */
8772 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8773 return error_mark_node;
8775 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8776 tf_warning_or_error);
8779 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8780 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8781 0 otherwise. */
8783 static int
8784 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8786 cp_token *token = cp_lexer_peek_token (parser->lexer);
8787 switch (token->type)
8789 case CPP_COMMA:
8790 case CPP_SEMICOLON:
8791 case CPP_QUERY:
8792 case CPP_COLON:
8793 case CPP_CLOSE_SQUARE:
8794 case CPP_CLOSE_PAREN:
8795 case CPP_CLOSE_BRACE:
8796 case CPP_OPEN_BRACE:
8797 case CPP_DOT:
8798 case CPP_DOT_STAR:
8799 case CPP_DEREF:
8800 case CPP_DEREF_STAR:
8801 case CPP_DIV:
8802 case CPP_MOD:
8803 case CPP_LSHIFT:
8804 case CPP_RSHIFT:
8805 case CPP_LESS:
8806 case CPP_GREATER:
8807 case CPP_LESS_EQ:
8808 case CPP_GREATER_EQ:
8809 case CPP_EQ_EQ:
8810 case CPP_NOT_EQ:
8811 case CPP_EQ:
8812 case CPP_MULT_EQ:
8813 case CPP_DIV_EQ:
8814 case CPP_MOD_EQ:
8815 case CPP_PLUS_EQ:
8816 case CPP_MINUS_EQ:
8817 case CPP_RSHIFT_EQ:
8818 case CPP_LSHIFT_EQ:
8819 case CPP_AND_EQ:
8820 case CPP_XOR_EQ:
8821 case CPP_OR_EQ:
8822 case CPP_XOR:
8823 case CPP_OR:
8824 case CPP_OR_OR:
8825 case CPP_EOF:
8826 case CPP_ELLIPSIS:
8827 return 0;
8829 case CPP_OPEN_PAREN:
8830 /* In ((type ()) () the last () isn't a valid cast-expression,
8831 so the whole must be parsed as postfix-expression. */
8832 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8833 != CPP_CLOSE_PAREN;
8835 case CPP_OPEN_SQUARE:
8836 /* '[' may start a primary-expression in obj-c++ and in C++11,
8837 as a lambda-expression, eg, '(void)[]{}'. */
8838 if (cxx_dialect >= cxx11)
8839 return -1;
8840 return c_dialect_objc ();
8842 case CPP_PLUS_PLUS:
8843 case CPP_MINUS_MINUS:
8844 /* '++' and '--' may or may not start a cast-expression:
8846 struct T { void operator++(int); };
8847 void f() { (T())++; }
8851 int a;
8852 (int)++a; */
8853 return -1;
8855 default:
8856 return 1;
8860 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8861 in the order: const_cast, static_cast, reinterpret_cast.
8863 Don't suggest dynamic_cast.
8865 Return the first legal cast kind found, or NULL otherwise. */
8867 static const char *
8868 get_cast_suggestion (tree dst_type, tree orig_expr)
8870 tree trial;
8872 /* Reuse the parser logic by attempting to build the various kinds of
8873 cast, with "complain" disabled.
8874 Identify the first such cast that is valid. */
8876 /* Don't attempt to run such logic within template processing. */
8877 if (processing_template_decl)
8878 return NULL;
8880 /* First try const_cast. */
8881 trial = build_const_cast (dst_type, orig_expr, tf_none);
8882 if (trial != error_mark_node)
8883 return "const_cast";
8885 /* If that fails, try static_cast. */
8886 trial = build_static_cast (dst_type, orig_expr, tf_none);
8887 if (trial != error_mark_node)
8888 return "static_cast";
8890 /* Finally, try reinterpret_cast. */
8891 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8892 if (trial != error_mark_node)
8893 return "reinterpret_cast";
8895 /* No such cast possible. */
8896 return NULL;
8899 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8900 suggesting how to convert a C-style cast of the form:
8902 (DST_TYPE)ORIG_EXPR
8904 to a C++-style cast.
8906 The primary range of RICHLOC is asssumed to be that of the original
8907 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8908 of the parens in the C-style cast. */
8910 static void
8911 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8912 location_t close_paren_loc, tree orig_expr,
8913 tree dst_type)
8915 /* This function is non-trivial, so bail out now if the warning isn't
8916 going to be emitted. */
8917 if (!warn_old_style_cast)
8918 return;
8920 /* Try to find a legal C++ cast, trying them in order:
8921 const_cast, static_cast, reinterpret_cast. */
8922 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8923 if (!cast_suggestion)
8924 return;
8926 /* Replace the open paren with "CAST_SUGGESTION<". */
8927 pretty_printer pp;
8928 pp_printf (&pp, "%s<", cast_suggestion);
8929 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8931 /* Replace the close paren with "> (". */
8932 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8934 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8935 rich_loc->add_fixit_insert_after (")");
8939 /* Parse a cast-expression.
8941 cast-expression:
8942 unary-expression
8943 ( type-id ) cast-expression
8945 ADDRESS_P is true iff the unary-expression is appearing as the
8946 operand of the `&' operator. CAST_P is true if this expression is
8947 the target of a cast.
8949 Returns a representation of the expression. */
8951 static cp_expr
8952 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8953 bool decltype_p, cp_id_kind * pidk)
8955 /* If it's a `(', then we might be looking at a cast. */
8956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8958 tree type = NULL_TREE;
8959 cp_expr expr (NULL_TREE);
8960 int cast_expression = 0;
8961 const char *saved_message;
8963 /* There's no way to know yet whether or not this is a cast.
8964 For example, `(int (3))' is a unary-expression, while `(int)
8965 3' is a cast. So, we resort to parsing tentatively. */
8966 cp_parser_parse_tentatively (parser);
8967 /* Types may not be defined in a cast. */
8968 saved_message = parser->type_definition_forbidden_message;
8969 parser->type_definition_forbidden_message
8970 = G_("types may not be defined in casts");
8971 /* Consume the `('. */
8972 matching_parens parens;
8973 cp_token *open_paren = parens.consume_open (parser);
8974 location_t open_paren_loc = open_paren->location;
8975 location_t close_paren_loc = UNKNOWN_LOCATION;
8977 /* A very tricky bit is that `(struct S) { 3 }' is a
8978 compound-literal (which we permit in C++ as an extension).
8979 But, that construct is not a cast-expression -- it is a
8980 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8981 is legal; if the compound-literal were a cast-expression,
8982 you'd need an extra set of parentheses.) But, if we parse
8983 the type-id, and it happens to be a class-specifier, then we
8984 will commit to the parse at that point, because we cannot
8985 undo the action that is done when creating a new class. So,
8986 then we cannot back up and do a postfix-expression.
8988 Another tricky case is the following (c++/29234):
8990 struct S { void operator () (); };
8992 void foo ()
8994 ( S()() );
8997 As a type-id we parse the parenthesized S()() as a function
8998 returning a function, groktypename complains and we cannot
8999 back up in this case either.
9001 Therefore, we scan ahead to the closing `)', and check to see
9002 if the tokens after the `)' can start a cast-expression. Otherwise
9003 we are dealing with an unary-expression, a postfix-expression
9004 or something else.
9006 Yet another tricky case, in C++11, is the following (c++/54891):
9008 (void)[]{};
9010 The issue is that usually, besides the case of lambda-expressions,
9011 the parenthesized type-id cannot be followed by '[', and, eg, we
9012 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9013 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9014 we don't commit, we try a cast-expression, then an unary-expression.
9016 Save tokens so that we can put them back. */
9017 cp_lexer_save_tokens (parser->lexer);
9019 /* We may be looking at a cast-expression. */
9020 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9021 /*consume_paren=*/true))
9022 cast_expression
9023 = cp_parser_tokens_start_cast_expression (parser);
9025 /* Roll back the tokens we skipped. */
9026 cp_lexer_rollback_tokens (parser->lexer);
9027 /* If we aren't looking at a cast-expression, simulate an error so
9028 that the call to cp_parser_error_occurred below returns true. */
9029 if (!cast_expression)
9030 cp_parser_simulate_error (parser);
9031 else
9033 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9034 parser->in_type_id_in_expr_p = true;
9035 /* Look for the type-id. */
9036 type = cp_parser_type_id (parser);
9037 /* Look for the closing `)'. */
9038 cp_token *close_paren = parens.require_close (parser);
9039 if (close_paren)
9040 close_paren_loc = close_paren->location;
9041 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9044 /* Restore the saved message. */
9045 parser->type_definition_forbidden_message = saved_message;
9047 /* At this point this can only be either a cast or a
9048 parenthesized ctor such as `(T ())' that looks like a cast to
9049 function returning T. */
9050 if (!cp_parser_error_occurred (parser))
9052 /* Only commit if the cast-expression doesn't start with
9053 '++', '--', or '[' in C++11. */
9054 if (cast_expression > 0)
9055 cp_parser_commit_to_topmost_tentative_parse (parser);
9057 expr = cp_parser_cast_expression (parser,
9058 /*address_p=*/false,
9059 /*cast_p=*/true,
9060 /*decltype_p=*/false,
9061 pidk);
9063 if (cp_parser_parse_definitely (parser))
9065 /* Warn about old-style casts, if so requested. */
9066 if (warn_old_style_cast
9067 && !in_system_header_at (input_location)
9068 && !VOID_TYPE_P (type)
9069 && current_lang_name != lang_name_c)
9071 gcc_rich_location rich_loc (input_location);
9072 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9073 expr, type);
9074 warning_at (&rich_loc, OPT_Wold_style_cast,
9075 "use of old-style cast to %q#T", type);
9078 /* Only type conversions to integral or enumeration types
9079 can be used in constant-expressions. */
9080 if (!cast_valid_in_integral_constant_expression_p (type)
9081 && cp_parser_non_integral_constant_expression (parser,
9082 NIC_CAST))
9083 return error_mark_node;
9085 /* Perform the cast. */
9086 /* Make a location:
9087 (TYPE) EXPR
9088 ^~~~~~~~~~~
9089 with start==caret at the open paren, extending to the
9090 end of "expr". */
9091 location_t cast_loc = make_location (open_paren_loc,
9092 open_paren_loc,
9093 expr.get_finish ());
9094 expr = build_c_cast (cast_loc, type, expr);
9095 return expr;
9098 else
9099 cp_parser_abort_tentative_parse (parser);
9102 /* If we get here, then it's not a cast, so it must be a
9103 unary-expression. */
9104 return cp_parser_unary_expression (parser, pidk, address_p,
9105 cast_p, decltype_p);
9108 /* Parse a binary expression of the general form:
9110 pm-expression:
9111 cast-expression
9112 pm-expression .* cast-expression
9113 pm-expression ->* cast-expression
9115 multiplicative-expression:
9116 pm-expression
9117 multiplicative-expression * pm-expression
9118 multiplicative-expression / pm-expression
9119 multiplicative-expression % pm-expression
9121 additive-expression:
9122 multiplicative-expression
9123 additive-expression + multiplicative-expression
9124 additive-expression - multiplicative-expression
9126 shift-expression:
9127 additive-expression
9128 shift-expression << additive-expression
9129 shift-expression >> additive-expression
9131 relational-expression:
9132 shift-expression
9133 relational-expression < shift-expression
9134 relational-expression > shift-expression
9135 relational-expression <= shift-expression
9136 relational-expression >= shift-expression
9138 GNU Extension:
9140 relational-expression:
9141 relational-expression <? shift-expression
9142 relational-expression >? shift-expression
9144 equality-expression:
9145 relational-expression
9146 equality-expression == relational-expression
9147 equality-expression != relational-expression
9149 and-expression:
9150 equality-expression
9151 and-expression & equality-expression
9153 exclusive-or-expression:
9154 and-expression
9155 exclusive-or-expression ^ and-expression
9157 inclusive-or-expression:
9158 exclusive-or-expression
9159 inclusive-or-expression | exclusive-or-expression
9161 logical-and-expression:
9162 inclusive-or-expression
9163 logical-and-expression && inclusive-or-expression
9165 logical-or-expression:
9166 logical-and-expression
9167 logical-or-expression || logical-and-expression
9169 All these are implemented with a single function like:
9171 binary-expression:
9172 simple-cast-expression
9173 binary-expression <token> binary-expression
9175 CAST_P is true if this expression is the target of a cast.
9177 The binops_by_token map is used to get the tree codes for each <token> type.
9178 binary-expressions are associated according to a precedence table. */
9180 #define TOKEN_PRECEDENCE(token) \
9181 (((token->type == CPP_GREATER \
9182 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9183 && !parser->greater_than_is_operator_p) \
9184 ? PREC_NOT_OPERATOR \
9185 : binops_by_token[token->type].prec)
9187 static cp_expr
9188 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9189 bool no_toplevel_fold_p,
9190 bool decltype_p,
9191 enum cp_parser_prec prec,
9192 cp_id_kind * pidk)
9194 cp_parser_expression_stack stack;
9195 cp_parser_expression_stack_entry *sp = &stack[0];
9196 cp_parser_expression_stack_entry current;
9197 cp_expr rhs;
9198 cp_token *token;
9199 enum tree_code rhs_type;
9200 enum cp_parser_prec new_prec, lookahead_prec;
9201 tree overload;
9203 /* Parse the first expression. */
9204 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9205 ? TRUTH_NOT_EXPR : ERROR_MARK);
9206 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9207 cast_p, decltype_p, pidk);
9208 current.prec = prec;
9210 if (cp_parser_error_occurred (parser))
9211 return error_mark_node;
9213 for (;;)
9215 /* Get an operator token. */
9216 token = cp_lexer_peek_token (parser->lexer);
9218 if (warn_cxx11_compat
9219 && token->type == CPP_RSHIFT
9220 && !parser->greater_than_is_operator_p)
9222 if (warning_at (token->location, OPT_Wc__11_compat,
9223 "%<>>%> operator is treated"
9224 " as two right angle brackets in C++11"))
9225 inform (token->location,
9226 "suggest parentheses around %<>>%> expression");
9229 new_prec = TOKEN_PRECEDENCE (token);
9230 if (new_prec != PREC_NOT_OPERATOR
9231 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9232 /* This is a fold-expression; handle it later. */
9233 new_prec = PREC_NOT_OPERATOR;
9235 /* Popping an entry off the stack means we completed a subexpression:
9236 - either we found a token which is not an operator (`>' where it is not
9237 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9238 will happen repeatedly;
9239 - or, we found an operator which has lower priority. This is the case
9240 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9241 parsing `3 * 4'. */
9242 if (new_prec <= current.prec)
9244 if (sp == stack)
9245 break;
9246 else
9247 goto pop;
9250 get_rhs:
9251 current.tree_type = binops_by_token[token->type].tree_type;
9252 current.loc = token->location;
9254 /* We used the operator token. */
9255 cp_lexer_consume_token (parser->lexer);
9257 /* For "false && x" or "true || x", x will never be executed;
9258 disable warnings while evaluating it. */
9259 if (current.tree_type == TRUTH_ANDIF_EXPR)
9260 c_inhibit_evaluation_warnings +=
9261 cp_fully_fold (current.lhs) == truthvalue_false_node;
9262 else if (current.tree_type == TRUTH_ORIF_EXPR)
9263 c_inhibit_evaluation_warnings +=
9264 cp_fully_fold (current.lhs) == truthvalue_true_node;
9266 /* Extract another operand. It may be the RHS of this expression
9267 or the LHS of a new, higher priority expression. */
9268 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9269 ? TRUTH_NOT_EXPR : ERROR_MARK);
9270 rhs = cp_parser_simple_cast_expression (parser);
9272 /* Get another operator token. Look up its precedence to avoid
9273 building a useless (immediately popped) stack entry for common
9274 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9275 token = cp_lexer_peek_token (parser->lexer);
9276 lookahead_prec = TOKEN_PRECEDENCE (token);
9277 if (lookahead_prec != PREC_NOT_OPERATOR
9278 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9279 lookahead_prec = PREC_NOT_OPERATOR;
9280 if (lookahead_prec > new_prec)
9282 /* ... and prepare to parse the RHS of the new, higher priority
9283 expression. Since precedence levels on the stack are
9284 monotonically increasing, we do not have to care about
9285 stack overflows. */
9286 *sp = current;
9287 ++sp;
9288 current.lhs = rhs;
9289 current.lhs_type = rhs_type;
9290 current.prec = new_prec;
9291 new_prec = lookahead_prec;
9292 goto get_rhs;
9294 pop:
9295 lookahead_prec = new_prec;
9296 /* If the stack is not empty, we have parsed into LHS the right side
9297 (`4' in the example above) of an expression we had suspended.
9298 We can use the information on the stack to recover the LHS (`3')
9299 from the stack together with the tree code (`MULT_EXPR'), and
9300 the precedence of the higher level subexpression
9301 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9302 which will be used to actually build the additive expression. */
9303 rhs = current.lhs;
9304 rhs_type = current.lhs_type;
9305 --sp;
9306 current = *sp;
9309 /* Undo the disabling of warnings done above. */
9310 if (current.tree_type == TRUTH_ANDIF_EXPR)
9311 c_inhibit_evaluation_warnings -=
9312 cp_fully_fold (current.lhs) == truthvalue_false_node;
9313 else if (current.tree_type == TRUTH_ORIF_EXPR)
9314 c_inhibit_evaluation_warnings -=
9315 cp_fully_fold (current.lhs) == truthvalue_true_node;
9317 if (warn_logical_not_paren
9318 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9319 && current.lhs_type == TRUTH_NOT_EXPR
9320 /* Avoid warning for !!x == y. */
9321 && (TREE_CODE (current.lhs) != NE_EXPR
9322 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9323 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9324 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9325 /* Avoid warning for !b == y where b is boolean. */
9326 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9327 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9328 != BOOLEAN_TYPE))))
9329 /* Avoid warning for !!b == y where b is boolean. */
9330 && (!DECL_P (current.lhs)
9331 || TREE_TYPE (current.lhs) == NULL_TREE
9332 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9333 warn_logical_not_parentheses (current.loc, current.tree_type,
9334 current.lhs, maybe_constant_value (rhs));
9336 overload = NULL;
9338 location_t combined_loc = make_location (current.loc,
9339 current.lhs.get_start (),
9340 rhs.get_finish ());
9342 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9343 ERROR_MARK for everything that is not a binary expression.
9344 This makes warn_about_parentheses miss some warnings that
9345 involve unary operators. For unary expressions we should
9346 pass the correct tree_code unless the unary expression was
9347 surrounded by parentheses.
9349 if (no_toplevel_fold_p
9350 && lookahead_prec <= current.prec
9351 && sp == stack)
9353 if (current.lhs == error_mark_node || rhs == error_mark_node)
9354 current.lhs = error_mark_node;
9355 else
9357 current.lhs
9358 = build_min (current.tree_type,
9359 TREE_CODE_CLASS (current.tree_type)
9360 == tcc_comparison
9361 ? boolean_type_node : TREE_TYPE (current.lhs),
9362 current.lhs.get_value (), rhs.get_value ());
9363 SET_EXPR_LOCATION (current.lhs, combined_loc);
9366 else
9368 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9369 current.lhs, current.lhs_type,
9370 rhs, rhs_type, &overload,
9371 complain_flags (decltype_p));
9372 /* TODO: build_x_binary_op doesn't always honor the location. */
9373 current.lhs.set_location (combined_loc);
9375 current.lhs_type = current.tree_type;
9377 /* If the binary operator required the use of an overloaded operator,
9378 then this expression cannot be an integral constant-expression.
9379 An overloaded operator can be used even if both operands are
9380 otherwise permissible in an integral constant-expression if at
9381 least one of the operands is of enumeration type. */
9383 if (overload
9384 && cp_parser_non_integral_constant_expression (parser,
9385 NIC_OVERLOADED))
9386 return error_mark_node;
9389 return current.lhs;
9392 static cp_expr
9393 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9394 bool no_toplevel_fold_p,
9395 enum cp_parser_prec prec,
9396 cp_id_kind * pidk)
9398 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9399 /*decltype*/false, prec, pidk);
9402 /* Parse the `? expression : assignment-expression' part of a
9403 conditional-expression. The LOGICAL_OR_EXPR is the
9404 logical-or-expression that started the conditional-expression.
9405 Returns a representation of the entire conditional-expression.
9407 This routine is used by cp_parser_assignment_expression.
9409 ? expression : assignment-expression
9411 GNU Extensions:
9413 ? : assignment-expression */
9415 static tree
9416 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9418 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9419 cp_expr assignment_expr;
9420 struct cp_token *token;
9421 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9423 /* Consume the `?' token. */
9424 cp_lexer_consume_token (parser->lexer);
9425 token = cp_lexer_peek_token (parser->lexer);
9426 if (cp_parser_allow_gnu_extensions_p (parser)
9427 && token->type == CPP_COLON)
9429 pedwarn (token->location, OPT_Wpedantic,
9430 "ISO C++ does not allow ?: with omitted middle operand");
9431 /* Implicit true clause. */
9432 expr = NULL_TREE;
9433 c_inhibit_evaluation_warnings +=
9434 folded_logical_or_expr == truthvalue_true_node;
9435 warn_for_omitted_condop (token->location, logical_or_expr);
9437 else
9439 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9440 parser->colon_corrects_to_scope_p = false;
9441 /* Parse the expression. */
9442 c_inhibit_evaluation_warnings +=
9443 folded_logical_or_expr == truthvalue_false_node;
9444 expr = cp_parser_expression (parser);
9445 c_inhibit_evaluation_warnings +=
9446 ((folded_logical_or_expr == truthvalue_true_node)
9447 - (folded_logical_or_expr == truthvalue_false_node));
9448 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9451 /* The next token should be a `:'. */
9452 cp_parser_require (parser, CPP_COLON, RT_COLON);
9453 /* Parse the assignment-expression. */
9454 assignment_expr = cp_parser_assignment_expression (parser);
9455 c_inhibit_evaluation_warnings -=
9456 folded_logical_or_expr == truthvalue_true_node;
9458 /* Make a location:
9459 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9460 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9461 with the caret at the "?", ranging from the start of
9462 the logical_or_expr to the end of the assignment_expr. */
9463 loc = make_location (loc,
9464 logical_or_expr.get_start (),
9465 assignment_expr.get_finish ());
9467 /* Build the conditional-expression. */
9468 return build_x_conditional_expr (loc, logical_or_expr,
9469 expr,
9470 assignment_expr,
9471 tf_warning_or_error);
9474 /* Parse an assignment-expression.
9476 assignment-expression:
9477 conditional-expression
9478 logical-or-expression assignment-operator assignment_expression
9479 throw-expression
9481 CAST_P is true if this expression is the target of a cast.
9482 DECLTYPE_P is true if this expression is the operand of decltype.
9484 Returns a representation for the expression. */
9486 static cp_expr
9487 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9488 bool cast_p, bool decltype_p)
9490 cp_expr expr;
9492 /* If the next token is the `throw' keyword, then we're looking at
9493 a throw-expression. */
9494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9495 expr = cp_parser_throw_expression (parser);
9496 /* Otherwise, it must be that we are looking at a
9497 logical-or-expression. */
9498 else
9500 /* Parse the binary expressions (logical-or-expression). */
9501 expr = cp_parser_binary_expression (parser, cast_p, false,
9502 decltype_p,
9503 PREC_NOT_OPERATOR, pidk);
9504 /* If the next token is a `?' then we're actually looking at a
9505 conditional-expression. */
9506 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9507 return cp_parser_question_colon_clause (parser, expr);
9508 else
9510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9512 /* If it's an assignment-operator, we're using the second
9513 production. */
9514 enum tree_code assignment_operator
9515 = cp_parser_assignment_operator_opt (parser);
9516 if (assignment_operator != ERROR_MARK)
9518 bool non_constant_p;
9520 /* Parse the right-hand side of the assignment. */
9521 cp_expr rhs = cp_parser_initializer_clause (parser,
9522 &non_constant_p);
9524 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9525 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9527 /* An assignment may not appear in a
9528 constant-expression. */
9529 if (cp_parser_non_integral_constant_expression (parser,
9530 NIC_ASSIGNMENT))
9531 return error_mark_node;
9532 /* Build the assignment expression. Its default
9533 location:
9534 LHS = RHS
9535 ~~~~^~~~~
9536 is the location of the '=' token as the
9537 caret, ranging from the start of the lhs to the
9538 end of the rhs. */
9539 loc = make_location (loc,
9540 expr.get_start (),
9541 rhs.get_finish ());
9542 expr = build_x_modify_expr (loc, expr,
9543 assignment_operator,
9544 rhs,
9545 complain_flags (decltype_p));
9546 /* TODO: build_x_modify_expr doesn't honor the location,
9547 so we must set it here. */
9548 expr.set_location (loc);
9553 return expr;
9556 /* Parse an (optional) assignment-operator.
9558 assignment-operator: one of
9559 = *= /= %= += -= >>= <<= &= ^= |=
9561 GNU Extension:
9563 assignment-operator: one of
9564 <?= >?=
9566 If the next token is an assignment operator, the corresponding tree
9567 code is returned, and the token is consumed. For example, for
9568 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9569 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9570 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9571 operator, ERROR_MARK is returned. */
9573 static enum tree_code
9574 cp_parser_assignment_operator_opt (cp_parser* parser)
9576 enum tree_code op;
9577 cp_token *token;
9579 /* Peek at the next token. */
9580 token = cp_lexer_peek_token (parser->lexer);
9582 switch (token->type)
9584 case CPP_EQ:
9585 op = NOP_EXPR;
9586 break;
9588 case CPP_MULT_EQ:
9589 op = MULT_EXPR;
9590 break;
9592 case CPP_DIV_EQ:
9593 op = TRUNC_DIV_EXPR;
9594 break;
9596 case CPP_MOD_EQ:
9597 op = TRUNC_MOD_EXPR;
9598 break;
9600 case CPP_PLUS_EQ:
9601 op = PLUS_EXPR;
9602 break;
9604 case CPP_MINUS_EQ:
9605 op = MINUS_EXPR;
9606 break;
9608 case CPP_RSHIFT_EQ:
9609 op = RSHIFT_EXPR;
9610 break;
9612 case CPP_LSHIFT_EQ:
9613 op = LSHIFT_EXPR;
9614 break;
9616 case CPP_AND_EQ:
9617 op = BIT_AND_EXPR;
9618 break;
9620 case CPP_XOR_EQ:
9621 op = BIT_XOR_EXPR;
9622 break;
9624 case CPP_OR_EQ:
9625 op = BIT_IOR_EXPR;
9626 break;
9628 default:
9629 /* Nothing else is an assignment operator. */
9630 op = ERROR_MARK;
9633 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9634 if (op != ERROR_MARK
9635 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9636 op = ERROR_MARK;
9638 /* If it was an assignment operator, consume it. */
9639 if (op != ERROR_MARK)
9640 cp_lexer_consume_token (parser->lexer);
9642 return op;
9645 /* Parse an expression.
9647 expression:
9648 assignment-expression
9649 expression , assignment-expression
9651 CAST_P is true if this expression is the target of a cast.
9652 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9653 except possibly parenthesized or on the RHS of a comma (N3276).
9655 Returns a representation of the expression. */
9657 static cp_expr
9658 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9659 bool cast_p, bool decltype_p)
9661 cp_expr expression = NULL_TREE;
9662 location_t loc = UNKNOWN_LOCATION;
9664 while (true)
9666 cp_expr assignment_expression;
9668 /* Parse the next assignment-expression. */
9669 assignment_expression
9670 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9672 /* We don't create a temporary for a call that is the immediate operand
9673 of decltype or on the RHS of a comma. But when we see a comma, we
9674 need to create a temporary for a call on the LHS. */
9675 if (decltype_p && !processing_template_decl
9676 && TREE_CODE (assignment_expression) == CALL_EXPR
9677 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9678 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9679 assignment_expression
9680 = build_cplus_new (TREE_TYPE (assignment_expression),
9681 assignment_expression, tf_warning_or_error);
9683 /* If this is the first assignment-expression, we can just
9684 save it away. */
9685 if (!expression)
9686 expression = assignment_expression;
9687 else
9689 /* Create a location with caret at the comma, ranging
9690 from the start of the LHS to the end of the RHS. */
9691 loc = make_location (loc,
9692 expression.get_start (),
9693 assignment_expression.get_finish ());
9694 expression = build_x_compound_expr (loc, expression,
9695 assignment_expression,
9696 complain_flags (decltype_p));
9697 expression.set_location (loc);
9699 /* If the next token is not a comma, or we're in a fold-expression, then
9700 we are done with the expression. */
9701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9702 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9703 break;
9704 /* Consume the `,'. */
9705 loc = cp_lexer_peek_token (parser->lexer)->location;
9706 cp_lexer_consume_token (parser->lexer);
9707 /* A comma operator cannot appear in a constant-expression. */
9708 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9709 expression = error_mark_node;
9712 return expression;
9715 /* Parse a constant-expression.
9717 constant-expression:
9718 conditional-expression
9720 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9721 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9722 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9723 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9724 only parse a conditional-expression, otherwise parse an
9725 assignment-expression. See below for rationale. */
9727 static cp_expr
9728 cp_parser_constant_expression (cp_parser* parser,
9729 bool allow_non_constant_p,
9730 bool *non_constant_p,
9731 bool strict_p)
9733 bool saved_integral_constant_expression_p;
9734 bool saved_allow_non_integral_constant_expression_p;
9735 bool saved_non_integral_constant_expression_p;
9736 cp_expr expression;
9738 /* It might seem that we could simply parse the
9739 conditional-expression, and then check to see if it were
9740 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9741 one that the compiler can figure out is constant, possibly after
9742 doing some simplifications or optimizations. The standard has a
9743 precise definition of constant-expression, and we must honor
9744 that, even though it is somewhat more restrictive.
9746 For example:
9748 int i[(2, 3)];
9750 is not a legal declaration, because `(2, 3)' is not a
9751 constant-expression. The `,' operator is forbidden in a
9752 constant-expression. However, GCC's constant-folding machinery
9753 will fold this operation to an INTEGER_CST for `3'. */
9755 /* Save the old settings. */
9756 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9757 saved_allow_non_integral_constant_expression_p
9758 = parser->allow_non_integral_constant_expression_p;
9759 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9760 /* We are now parsing a constant-expression. */
9761 parser->integral_constant_expression_p = true;
9762 parser->allow_non_integral_constant_expression_p
9763 = (allow_non_constant_p || cxx_dialect >= cxx11);
9764 parser->non_integral_constant_expression_p = false;
9765 /* Although the grammar says "conditional-expression", when not STRICT_P,
9766 we parse an "assignment-expression", which also permits
9767 "throw-expression" and the use of assignment operators. In the case
9768 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9769 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9770 actually essential that we look for an assignment-expression.
9771 For example, cp_parser_initializer_clauses uses this function to
9772 determine whether a particular assignment-expression is in fact
9773 constant. */
9774 if (strict_p)
9776 /* Parse the binary expressions (logical-or-expression). */
9777 expression = cp_parser_binary_expression (parser, false, false, false,
9778 PREC_NOT_OPERATOR, NULL);
9779 /* If the next token is a `?' then we're actually looking at
9780 a conditional-expression; otherwise we're done. */
9781 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9782 expression = cp_parser_question_colon_clause (parser, expression);
9784 else
9785 expression = cp_parser_assignment_expression (parser);
9786 /* Restore the old settings. */
9787 parser->integral_constant_expression_p
9788 = saved_integral_constant_expression_p;
9789 parser->allow_non_integral_constant_expression_p
9790 = saved_allow_non_integral_constant_expression_p;
9791 if (cxx_dialect >= cxx11)
9793 /* Require an rvalue constant expression here; that's what our
9794 callers expect. Reference constant expressions are handled
9795 separately in e.g. cp_parser_template_argument. */
9796 tree decay = expression;
9797 if (TREE_TYPE (expression)
9798 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9799 decay = build_address (expression);
9800 bool is_const = potential_rvalue_constant_expression (decay);
9801 parser->non_integral_constant_expression_p = !is_const;
9802 if (!is_const && !allow_non_constant_p)
9803 require_potential_rvalue_constant_expression (decay);
9805 if (allow_non_constant_p)
9806 *non_constant_p = parser->non_integral_constant_expression_p;
9807 parser->non_integral_constant_expression_p
9808 = saved_non_integral_constant_expression_p;
9810 return expression;
9813 /* Parse __builtin_offsetof.
9815 offsetof-expression:
9816 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9818 offsetof-member-designator:
9819 id-expression
9820 | offsetof-member-designator "." id-expression
9821 | offsetof-member-designator "[" expression "]"
9822 | offsetof-member-designator "->" id-expression */
9824 static cp_expr
9825 cp_parser_builtin_offsetof (cp_parser *parser)
9827 int save_ice_p, save_non_ice_p;
9828 tree type;
9829 cp_expr expr;
9830 cp_id_kind dummy;
9831 cp_token *token;
9832 location_t finish_loc;
9834 /* We're about to accept non-integral-constant things, but will
9835 definitely yield an integral constant expression. Save and
9836 restore these values around our local parsing. */
9837 save_ice_p = parser->integral_constant_expression_p;
9838 save_non_ice_p = parser->non_integral_constant_expression_p;
9840 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9842 /* Consume the "__builtin_offsetof" token. */
9843 cp_lexer_consume_token (parser->lexer);
9844 /* Consume the opening `('. */
9845 matching_parens parens;
9846 parens.require_open (parser);
9847 /* Parse the type-id. */
9848 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9850 const char *saved_message = parser->type_definition_forbidden_message;
9851 parser->type_definition_forbidden_message
9852 = G_("types may not be defined within __builtin_offsetof");
9853 type = cp_parser_type_id (parser);
9854 parser->type_definition_forbidden_message = saved_message;
9856 /* Look for the `,'. */
9857 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9858 token = cp_lexer_peek_token (parser->lexer);
9860 /* Build the (type *)null that begins the traditional offsetof macro. */
9861 tree object_ptr
9862 = build_static_cast (build_pointer_type (type), null_pointer_node,
9863 tf_warning_or_error);
9865 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9866 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9867 true, &dummy, token->location);
9868 while (true)
9870 token = cp_lexer_peek_token (parser->lexer);
9871 switch (token->type)
9873 case CPP_OPEN_SQUARE:
9874 /* offsetof-member-designator "[" expression "]" */
9875 expr = cp_parser_postfix_open_square_expression (parser, expr,
9876 true, false);
9877 break;
9879 case CPP_DEREF:
9880 /* offsetof-member-designator "->" identifier */
9881 expr = grok_array_decl (token->location, expr,
9882 integer_zero_node, false);
9883 /* FALLTHRU */
9885 case CPP_DOT:
9886 /* offsetof-member-designator "." identifier */
9887 cp_lexer_consume_token (parser->lexer);
9888 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9889 expr, true, &dummy,
9890 token->location);
9891 break;
9893 case CPP_CLOSE_PAREN:
9894 /* Consume the ")" token. */
9895 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9896 cp_lexer_consume_token (parser->lexer);
9897 goto success;
9899 default:
9900 /* Error. We know the following require will fail, but
9901 that gives the proper error message. */
9902 parens.require_close (parser);
9903 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9904 expr = error_mark_node;
9905 goto failure;
9909 success:
9910 /* Make a location of the form:
9911 __builtin_offsetof (struct s, f)
9912 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9913 with caret at the type-id, ranging from the start of the
9914 "_builtin_offsetof" token to the close paren. */
9915 loc = make_location (loc, start_loc, finish_loc);
9916 /* The result will be an INTEGER_CST, so we need to explicitly
9917 preserve the location. */
9918 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9920 failure:
9921 parser->integral_constant_expression_p = save_ice_p;
9922 parser->non_integral_constant_expression_p = save_non_ice_p;
9924 expr = expr.maybe_add_location_wrapper ();
9925 return expr;
9928 /* Parse a trait expression.
9930 Returns a representation of the expression, the underlying type
9931 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9933 static cp_expr
9934 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9936 cp_trait_kind kind;
9937 tree type1, type2 = NULL_TREE;
9938 bool binary = false;
9939 bool variadic = false;
9941 switch (keyword)
9943 case RID_HAS_NOTHROW_ASSIGN:
9944 kind = CPTK_HAS_NOTHROW_ASSIGN;
9945 break;
9946 case RID_HAS_NOTHROW_CONSTRUCTOR:
9947 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9948 break;
9949 case RID_HAS_NOTHROW_COPY:
9950 kind = CPTK_HAS_NOTHROW_COPY;
9951 break;
9952 case RID_HAS_TRIVIAL_ASSIGN:
9953 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9954 break;
9955 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9956 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9957 break;
9958 case RID_HAS_TRIVIAL_COPY:
9959 kind = CPTK_HAS_TRIVIAL_COPY;
9960 break;
9961 case RID_HAS_TRIVIAL_DESTRUCTOR:
9962 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9963 break;
9964 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9965 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9966 break;
9967 case RID_HAS_VIRTUAL_DESTRUCTOR:
9968 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9969 break;
9970 case RID_IS_ABSTRACT:
9971 kind = CPTK_IS_ABSTRACT;
9972 break;
9973 case RID_IS_AGGREGATE:
9974 kind = CPTK_IS_AGGREGATE;
9975 break;
9976 case RID_IS_BASE_OF:
9977 kind = CPTK_IS_BASE_OF;
9978 binary = true;
9979 break;
9980 case RID_IS_CLASS:
9981 kind = CPTK_IS_CLASS;
9982 break;
9983 case RID_IS_EMPTY:
9984 kind = CPTK_IS_EMPTY;
9985 break;
9986 case RID_IS_ENUM:
9987 kind = CPTK_IS_ENUM;
9988 break;
9989 case RID_IS_FINAL:
9990 kind = CPTK_IS_FINAL;
9991 break;
9992 case RID_IS_LITERAL_TYPE:
9993 kind = CPTK_IS_LITERAL_TYPE;
9994 break;
9995 case RID_IS_POD:
9996 kind = CPTK_IS_POD;
9997 break;
9998 case RID_IS_POLYMORPHIC:
9999 kind = CPTK_IS_POLYMORPHIC;
10000 break;
10001 case RID_IS_SAME_AS:
10002 kind = CPTK_IS_SAME_AS;
10003 binary = true;
10004 break;
10005 case RID_IS_STD_LAYOUT:
10006 kind = CPTK_IS_STD_LAYOUT;
10007 break;
10008 case RID_IS_TRIVIAL:
10009 kind = CPTK_IS_TRIVIAL;
10010 break;
10011 case RID_IS_TRIVIALLY_ASSIGNABLE:
10012 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10013 binary = true;
10014 break;
10015 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10016 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10017 variadic = true;
10018 break;
10019 case RID_IS_TRIVIALLY_COPYABLE:
10020 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10021 break;
10022 case RID_IS_UNION:
10023 kind = CPTK_IS_UNION;
10024 break;
10025 case RID_UNDERLYING_TYPE:
10026 kind = CPTK_UNDERLYING_TYPE;
10027 break;
10028 case RID_BASES:
10029 kind = CPTK_BASES;
10030 break;
10031 case RID_DIRECT_BASES:
10032 kind = CPTK_DIRECT_BASES;
10033 break;
10034 case RID_IS_ASSIGNABLE:
10035 kind = CPTK_IS_ASSIGNABLE;
10036 binary = true;
10037 break;
10038 case RID_IS_CONSTRUCTIBLE:
10039 kind = CPTK_IS_CONSTRUCTIBLE;
10040 variadic = true;
10041 break;
10042 default:
10043 gcc_unreachable ();
10046 /* Get location of initial token. */
10047 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10049 /* Consume the token. */
10050 cp_lexer_consume_token (parser->lexer);
10052 matching_parens parens;
10053 parens.require_open (parser);
10056 type_id_in_expr_sentinel s (parser);
10057 type1 = cp_parser_type_id (parser);
10060 if (type1 == error_mark_node)
10061 return error_mark_node;
10063 if (binary)
10065 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10068 type_id_in_expr_sentinel s (parser);
10069 type2 = cp_parser_type_id (parser);
10072 if (type2 == error_mark_node)
10073 return error_mark_node;
10075 else if (variadic)
10077 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10079 cp_lexer_consume_token (parser->lexer);
10080 tree elt = cp_parser_type_id (parser);
10081 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10083 cp_lexer_consume_token (parser->lexer);
10084 elt = make_pack_expansion (elt);
10086 if (elt == error_mark_node)
10087 return error_mark_node;
10088 type2 = tree_cons (NULL_TREE, elt, type2);
10092 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10093 parens.require_close (parser);
10095 /* Construct a location of the form:
10096 __is_trivially_copyable(_Tp)
10097 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10098 with start == caret, finishing at the close-paren. */
10099 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10101 /* Complete the trait expression, which may mean either processing
10102 the trait expr now or saving it for template instantiation. */
10103 switch (kind)
10105 case CPTK_UNDERLYING_TYPE:
10106 return cp_expr (finish_underlying_type (type1), trait_loc);
10107 case CPTK_BASES:
10108 return cp_expr (finish_bases (type1, false), trait_loc);
10109 case CPTK_DIRECT_BASES:
10110 return cp_expr (finish_bases (type1, true), trait_loc);
10111 default:
10112 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10116 /* Parse a lambda expression.
10118 lambda-expression:
10119 lambda-introducer lambda-declarator [opt] compound-statement
10121 Returns a representation of the expression. */
10123 static cp_expr
10124 cp_parser_lambda_expression (cp_parser* parser)
10126 tree lambda_expr = build_lambda_expr ();
10127 tree type;
10128 bool ok = true;
10129 cp_token *token = cp_lexer_peek_token (parser->lexer);
10130 cp_token_position start = 0;
10132 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10134 if (cp_unevaluated_operand)
10136 if (!token->error_reported)
10138 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10139 "lambda-expression in unevaluated context");
10140 token->error_reported = true;
10142 ok = false;
10144 else if (parser->in_template_argument_list_p)
10146 if (!token->error_reported)
10148 error_at (token->location, "lambda-expression in template-argument");
10149 token->error_reported = true;
10151 ok = false;
10154 /* We may be in the middle of deferred access check. Disable
10155 it now. */
10156 push_deferring_access_checks (dk_no_deferred);
10158 cp_parser_lambda_introducer (parser, lambda_expr);
10160 type = begin_lambda_type (lambda_expr);
10161 if (type == error_mark_node)
10162 return error_mark_node;
10164 record_lambda_scope (lambda_expr);
10166 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10167 determine_visibility (TYPE_NAME (type));
10169 /* Now that we've started the type, add the capture fields for any
10170 explicit captures. */
10171 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10174 /* Inside the class, surrounding template-parameter-lists do not apply. */
10175 unsigned int saved_num_template_parameter_lists
10176 = parser->num_template_parameter_lists;
10177 unsigned char in_statement = parser->in_statement;
10178 bool in_switch_statement_p = parser->in_switch_statement_p;
10179 bool fully_implicit_function_template_p
10180 = parser->fully_implicit_function_template_p;
10181 tree implicit_template_parms = parser->implicit_template_parms;
10182 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10183 bool auto_is_implicit_function_template_parm_p
10184 = parser->auto_is_implicit_function_template_parm_p;
10186 parser->num_template_parameter_lists = 0;
10187 parser->in_statement = 0;
10188 parser->in_switch_statement_p = false;
10189 parser->fully_implicit_function_template_p = false;
10190 parser->implicit_template_parms = 0;
10191 parser->implicit_template_scope = 0;
10192 parser->auto_is_implicit_function_template_parm_p = false;
10194 /* By virtue of defining a local class, a lambda expression has access to
10195 the private variables of enclosing classes. */
10197 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10199 if (ok && cp_parser_error_occurred (parser))
10200 ok = false;
10202 if (ok)
10204 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10205 && cp_parser_start_tentative_firewall (parser))
10206 start = token;
10207 cp_parser_lambda_body (parser, lambda_expr);
10209 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10211 if (cp_parser_skip_to_closing_brace (parser))
10212 cp_lexer_consume_token (parser->lexer);
10215 /* The capture list was built up in reverse order; fix that now. */
10216 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10217 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10219 if (ok)
10220 maybe_add_lambda_conv_op (type);
10222 type = finish_struct (type, /*attributes=*/NULL_TREE);
10224 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10225 parser->in_statement = in_statement;
10226 parser->in_switch_statement_p = in_switch_statement_p;
10227 parser->fully_implicit_function_template_p
10228 = fully_implicit_function_template_p;
10229 parser->implicit_template_parms = implicit_template_parms;
10230 parser->implicit_template_scope = implicit_template_scope;
10231 parser->auto_is_implicit_function_template_parm_p
10232 = auto_is_implicit_function_template_parm_p;
10235 /* This field is only used during parsing of the lambda. */
10236 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10238 /* This lambda shouldn't have any proxies left at this point. */
10239 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10240 /* And now that we're done, push proxies for an enclosing lambda. */
10241 insert_pending_capture_proxies ();
10243 /* Update the lambda expression to a range. */
10244 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10245 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10246 token->location,
10247 end_tok->location);
10249 if (ok)
10250 lambda_expr = build_lambda_object (lambda_expr);
10251 else
10252 lambda_expr = error_mark_node;
10254 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10256 pop_deferring_access_checks ();
10258 return lambda_expr;
10261 /* Parse the beginning of a lambda expression.
10263 lambda-introducer:
10264 [ lambda-capture [opt] ]
10266 LAMBDA_EXPR is the current representation of the lambda expression. */
10268 static void
10269 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10271 /* Need commas after the first capture. */
10272 bool first = true;
10274 /* Eat the leading `['. */
10275 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10277 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10278 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10279 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10280 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10281 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10282 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10284 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10286 cp_lexer_consume_token (parser->lexer);
10287 first = false;
10290 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10292 cp_token* capture_token;
10293 tree capture_id;
10294 tree capture_init_expr;
10295 cp_id_kind idk = CP_ID_KIND_NONE;
10296 bool explicit_init_p = false;
10298 enum capture_kind_type
10300 BY_COPY,
10301 BY_REFERENCE
10303 enum capture_kind_type capture_kind = BY_COPY;
10305 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10307 error ("expected end of capture-list");
10308 return;
10311 if (first)
10312 first = false;
10313 else
10314 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10316 /* Possibly capture `this'. */
10317 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10319 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10320 if (cxx_dialect < cxx2a
10321 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10322 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10323 "with by-copy capture default");
10324 cp_lexer_consume_token (parser->lexer);
10325 add_capture (lambda_expr,
10326 /*id=*/this_identifier,
10327 /*initializer=*/finish_this_expr (),
10328 /*by_reference_p=*/true,
10329 explicit_init_p);
10330 continue;
10333 /* Possibly capture `*this'. */
10334 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10335 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10338 if (cxx_dialect < cxx17)
10339 pedwarn (loc, 0, "%<*this%> capture only available with "
10340 "-std=c++17 or -std=gnu++17");
10341 cp_lexer_consume_token (parser->lexer);
10342 cp_lexer_consume_token (parser->lexer);
10343 add_capture (lambda_expr,
10344 /*id=*/this_identifier,
10345 /*initializer=*/finish_this_expr (),
10346 /*by_reference_p=*/false,
10347 explicit_init_p);
10348 continue;
10351 /* Remember whether we want to capture as a reference or not. */
10352 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10354 capture_kind = BY_REFERENCE;
10355 cp_lexer_consume_token (parser->lexer);
10358 /* Get the identifier. */
10359 capture_token = cp_lexer_peek_token (parser->lexer);
10360 capture_id = cp_parser_identifier (parser);
10362 if (capture_id == error_mark_node)
10363 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10364 delimiters, but I modified this to stop on unnested ']' as well. It
10365 was already changed to stop on unnested '}', so the
10366 "closing_parenthesis" name is no more misleading with my change. */
10368 cp_parser_skip_to_closing_parenthesis (parser,
10369 /*recovering=*/true,
10370 /*or_comma=*/true,
10371 /*consume_paren=*/true);
10372 break;
10375 /* Find the initializer for this capture. */
10376 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10377 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10378 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10380 bool direct, non_constant;
10381 /* An explicit initializer exists. */
10382 if (cxx_dialect < cxx14)
10383 pedwarn (input_location, 0,
10384 "lambda capture initializers "
10385 "only available with -std=c++14 or -std=gnu++14");
10386 capture_init_expr = cp_parser_initializer (parser, &direct,
10387 &non_constant, true);
10388 explicit_init_p = true;
10389 if (capture_init_expr == NULL_TREE)
10391 error ("empty initializer for lambda init-capture");
10392 capture_init_expr = error_mark_node;
10395 else
10397 const char* error_msg;
10399 /* Turn the identifier into an id-expression. */
10400 capture_init_expr
10401 = cp_parser_lookup_name_simple (parser, capture_id,
10402 capture_token->location);
10404 if (capture_init_expr == error_mark_node)
10406 unqualified_name_lookup_error (capture_id);
10407 continue;
10409 else if (!VAR_P (capture_init_expr)
10410 && TREE_CODE (capture_init_expr) != PARM_DECL)
10412 error_at (capture_token->location,
10413 "capture of non-variable %qE",
10414 capture_init_expr);
10415 if (DECL_P (capture_init_expr))
10416 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10417 "%q#D declared here", capture_init_expr);
10418 continue;
10420 if (VAR_P (capture_init_expr)
10421 && decl_storage_duration (capture_init_expr) != dk_auto)
10423 if (pedwarn (capture_token->location, 0, "capture of variable "
10424 "%qD with non-automatic storage duration",
10425 capture_init_expr))
10426 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10427 "%q#D declared here", capture_init_expr);
10428 continue;
10431 capture_init_expr
10432 = finish_id_expression
10433 (capture_id,
10434 capture_init_expr,
10435 parser->scope,
10436 &idk,
10437 /*integral_constant_expression_p=*/false,
10438 /*allow_non_integral_constant_expression_p=*/false,
10439 /*non_integral_constant_expression_p=*/NULL,
10440 /*template_p=*/false,
10441 /*done=*/true,
10442 /*address_p=*/false,
10443 /*template_arg_p=*/false,
10444 &error_msg,
10445 capture_token->location);
10447 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10449 cp_lexer_consume_token (parser->lexer);
10450 capture_init_expr = make_pack_expansion (capture_init_expr);
10454 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10455 && !explicit_init_p)
10457 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10458 && capture_kind == BY_COPY)
10459 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10460 "of %qD redundant with by-copy capture default",
10461 capture_id);
10462 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10463 && capture_kind == BY_REFERENCE)
10464 pedwarn (capture_token->location, 0, "explicit by-reference "
10465 "capture of %qD redundant with by-reference capture "
10466 "default", capture_id);
10469 add_capture (lambda_expr,
10470 capture_id,
10471 capture_init_expr,
10472 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10473 explicit_init_p);
10475 /* If there is any qualification still in effect, clear it
10476 now; we will be starting fresh with the next capture. */
10477 parser->scope = NULL_TREE;
10478 parser->qualifying_scope = NULL_TREE;
10479 parser->object_scope = NULL_TREE;
10482 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10485 /* Parse the (optional) middle of a lambda expression.
10487 lambda-declarator:
10488 < template-parameter-list [opt] >
10489 ( parameter-declaration-clause [opt] )
10490 attribute-specifier [opt]
10491 decl-specifier-seq [opt]
10492 exception-specification [opt]
10493 lambda-return-type-clause [opt]
10495 LAMBDA_EXPR is the current representation of the lambda expression. */
10497 static bool
10498 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10500 /* 5.1.1.4 of the standard says:
10501 If a lambda-expression does not include a lambda-declarator, it is as if
10502 the lambda-declarator were ().
10503 This means an empty parameter list, no attributes, and no exception
10504 specification. */
10505 tree param_list = void_list_node;
10506 tree attributes = NULL_TREE;
10507 tree exception_spec = NULL_TREE;
10508 tree template_param_list = NULL_TREE;
10509 tree tx_qual = NULL_TREE;
10510 tree return_type = NULL_TREE;
10511 cp_decl_specifier_seq lambda_specs;
10512 clear_decl_specs (&lambda_specs);
10514 /* The template-parameter-list is optional, but must begin with
10515 an opening angle if present. */
10516 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10518 if (cxx_dialect < cxx14)
10519 pedwarn (parser->lexer->next_token->location, 0,
10520 "lambda templates are only available with "
10521 "-std=c++14 or -std=gnu++14");
10522 else if (cxx_dialect < cxx2a)
10523 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10524 "lambda templates are only available with "
10525 "-std=c++2a or -std=gnu++2a");
10527 cp_lexer_consume_token (parser->lexer);
10529 template_param_list = cp_parser_template_parameter_list (parser);
10531 cp_parser_skip_to_end_of_template_parameter_list (parser);
10533 /* We just processed one more parameter list. */
10534 ++parser->num_template_parameter_lists;
10537 /* The parameter-declaration-clause is optional (unless
10538 template-parameter-list was given), but must begin with an
10539 opening parenthesis if present. */
10540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10542 matching_parens parens;
10543 parens.consume_open (parser);
10545 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10547 /* Parse parameters. */
10548 param_list = cp_parser_parameter_declaration_clause (parser);
10550 /* Default arguments shall not be specified in the
10551 parameter-declaration-clause of a lambda-declarator. */
10552 if (cxx_dialect < cxx14)
10553 for (tree t = param_list; t; t = TREE_CHAIN (t))
10554 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10555 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10556 "default argument specified for lambda parameter");
10558 parens.require_close (parser);
10560 attributes = cp_parser_attributes_opt (parser);
10562 /* In the decl-specifier-seq of the lambda-declarator, each
10563 decl-specifier shall either be mutable or constexpr. */
10564 int declares_class_or_enum;
10565 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10566 cp_parser_decl_specifier_seq (parser,
10567 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10568 &lambda_specs, &declares_class_or_enum);
10569 if (lambda_specs.storage_class == sc_mutable)
10571 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10572 if (lambda_specs.conflicting_specifiers_p)
10573 error_at (lambda_specs.locations[ds_storage_class],
10574 "duplicate %<mutable%>");
10577 tx_qual = cp_parser_tx_qualifier_opt (parser);
10579 /* Parse optional exception specification. */
10580 exception_spec = cp_parser_exception_specification_opt (parser);
10582 /* Parse optional trailing return type. */
10583 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10585 cp_lexer_consume_token (parser->lexer);
10586 return_type = cp_parser_trailing_type_id (parser);
10589 /* The function parameters must be in scope all the way until after the
10590 trailing-return-type in case of decltype. */
10591 pop_bindings_and_leave_scope ();
10593 else if (template_param_list != NULL_TREE) // generate diagnostic
10594 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10596 /* Create the function call operator.
10598 Messing with declarators like this is no uglier than building up the
10599 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10600 other code. */
10602 cp_decl_specifier_seq return_type_specs;
10603 cp_declarator* declarator;
10604 tree fco;
10605 int quals;
10606 void *p;
10608 clear_decl_specs (&return_type_specs);
10609 return_type_specs.type = make_auto ();
10611 if (lambda_specs.locations[ds_constexpr])
10613 if (cxx_dialect >= cxx17)
10614 return_type_specs.locations[ds_constexpr]
10615 = lambda_specs.locations[ds_constexpr];
10616 else
10617 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10618 "lambda only available with -std=c++17 or -std=gnu++17");
10621 p = obstack_alloc (&declarator_obstack, 0);
10623 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10625 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10626 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10627 declarator = make_call_declarator (declarator, param_list, quals,
10628 VIRT_SPEC_UNSPECIFIED,
10629 REF_QUAL_NONE,
10630 tx_qual,
10631 exception_spec,
10632 /*late_return_type=*/NULL_TREE,
10633 /*requires_clause*/NULL_TREE);
10634 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10635 if (return_type)
10636 declarator->u.function.late_return_type = return_type;
10638 fco = grokmethod (&return_type_specs,
10639 declarator,
10640 attributes);
10641 if (fco != error_mark_node)
10643 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10644 DECL_ARTIFICIAL (fco) = 1;
10645 /* Give the object parameter a different name. */
10646 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10648 if (template_param_list)
10650 fco = finish_member_template_decl (fco);
10651 finish_template_decl (template_param_list);
10652 --parser->num_template_parameter_lists;
10654 else if (parser->fully_implicit_function_template_p)
10655 fco = finish_fully_implicit_template (parser, fco);
10657 finish_member_declaration (fco);
10659 obstack_free (&declarator_obstack, p);
10661 return (fco != error_mark_node);
10665 /* Parse the body of a lambda expression, which is simply
10667 compound-statement
10669 but which requires special handling.
10670 LAMBDA_EXPR is the current representation of the lambda expression. */
10672 static void
10673 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10675 bool nested = (current_function_decl != NULL_TREE);
10676 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10677 bool in_function_body = parser->in_function_body;
10679 if (nested)
10680 push_function_context ();
10681 else
10682 /* Still increment function_depth so that we don't GC in the
10683 middle of an expression. */
10684 ++function_depth;
10686 vec<tree> omp_privatization_save;
10687 save_omp_privatization_clauses (omp_privatization_save);
10688 /* Clear this in case we're in the middle of a default argument. */
10689 parser->local_variables_forbidden_p = false;
10690 parser->in_function_body = true;
10693 local_specialization_stack s (lss_copy);
10694 tree fco = lambda_function (lambda_expr);
10695 tree body = start_lambda_function (fco, lambda_expr);
10696 matching_braces braces;
10698 if (braces.require_open (parser))
10700 tree compound_stmt = begin_compound_stmt (0);
10702 /* Originally C++11 required us to peek for 'return expr'; and
10703 process it specially here to deduce the return type. N3638
10704 removed the need for that. */
10706 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10707 cp_parser_label_declaration (parser);
10708 cp_parser_statement_seq_opt (parser, NULL_TREE);
10709 braces.require_close (parser);
10711 finish_compound_stmt (compound_stmt);
10714 finish_lambda_function (body);
10717 restore_omp_privatization_clauses (omp_privatization_save);
10718 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10719 parser->in_function_body = in_function_body;
10720 if (nested)
10721 pop_function_context();
10722 else
10723 --function_depth;
10726 /* Statements [gram.stmt.stmt] */
10728 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10730 static void
10731 add_debug_begin_stmt (location_t loc)
10733 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10734 return;
10735 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10736 /* A concept is never expanded normally. */
10737 return;
10739 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10740 SET_EXPR_LOCATION (stmt, loc);
10741 add_stmt (stmt);
10744 /* Parse a statement.
10746 statement:
10747 labeled-statement
10748 expression-statement
10749 compound-statement
10750 selection-statement
10751 iteration-statement
10752 jump-statement
10753 declaration-statement
10754 try-block
10756 C++11:
10758 statement:
10759 labeled-statement
10760 attribute-specifier-seq (opt) expression-statement
10761 attribute-specifier-seq (opt) compound-statement
10762 attribute-specifier-seq (opt) selection-statement
10763 attribute-specifier-seq (opt) iteration-statement
10764 attribute-specifier-seq (opt) jump-statement
10765 declaration-statement
10766 attribute-specifier-seq (opt) try-block
10768 init-statement:
10769 expression-statement
10770 simple-declaration
10772 TM Extension:
10774 statement:
10775 atomic-statement
10777 IN_COMPOUND is true when the statement is nested inside a
10778 cp_parser_compound_statement; this matters for certain pragmas.
10780 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10781 is a (possibly labeled) if statement which is not enclosed in braces
10782 and has an else clause. This is used to implement -Wparentheses.
10784 CHAIN is a vector of if-else-if conditions. */
10786 static void
10787 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10788 bool in_compound, bool *if_p, vec<tree> *chain,
10789 location_t *loc_after_labels)
10791 tree statement, std_attrs = NULL_TREE;
10792 cp_token *token;
10793 location_t statement_location, attrs_location;
10795 restart:
10796 if (if_p != NULL)
10797 *if_p = false;
10798 /* There is no statement yet. */
10799 statement = NULL_TREE;
10801 saved_token_sentinel saved_tokens (parser->lexer);
10802 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10803 if (c_dialect_objc ())
10804 /* In obj-c++, seeing '[[' might be the either the beginning of
10805 c++11 attributes, or a nested objc-message-expression. So
10806 let's parse the c++11 attributes tentatively. */
10807 cp_parser_parse_tentatively (parser);
10808 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10809 if (c_dialect_objc ())
10811 if (!cp_parser_parse_definitely (parser))
10812 std_attrs = NULL_TREE;
10815 /* Peek at the next token. */
10816 token = cp_lexer_peek_token (parser->lexer);
10817 /* Remember the location of the first token in the statement. */
10818 statement_location = token->location;
10819 add_debug_begin_stmt (statement_location);
10820 /* If this is a keyword, then that will often determine what kind of
10821 statement we have. */
10822 if (token->type == CPP_KEYWORD)
10824 enum rid keyword = token->keyword;
10826 switch (keyword)
10828 case RID_CASE:
10829 case RID_DEFAULT:
10830 /* Looks like a labeled-statement with a case label.
10831 Parse the label, and then use tail recursion to parse
10832 the statement. */
10833 cp_parser_label_for_labeled_statement (parser, std_attrs);
10834 in_compound = false;
10835 goto restart;
10837 case RID_IF:
10838 case RID_SWITCH:
10839 statement = cp_parser_selection_statement (parser, if_p, chain);
10840 break;
10842 case RID_WHILE:
10843 case RID_DO:
10844 case RID_FOR:
10845 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10846 break;
10848 case RID_BREAK:
10849 case RID_CONTINUE:
10850 case RID_RETURN:
10851 case RID_GOTO:
10852 statement = cp_parser_jump_statement (parser);
10853 break;
10855 /* Objective-C++ exception-handling constructs. */
10856 case RID_AT_TRY:
10857 case RID_AT_CATCH:
10858 case RID_AT_FINALLY:
10859 case RID_AT_SYNCHRONIZED:
10860 case RID_AT_THROW:
10861 statement = cp_parser_objc_statement (parser);
10862 break;
10864 case RID_TRY:
10865 statement = cp_parser_try_block (parser);
10866 break;
10868 case RID_NAMESPACE:
10869 /* This must be a namespace alias definition. */
10870 cp_parser_declaration_statement (parser);
10871 return;
10873 case RID_TRANSACTION_ATOMIC:
10874 case RID_TRANSACTION_RELAXED:
10875 case RID_SYNCHRONIZED:
10876 case RID_ATOMIC_NOEXCEPT:
10877 case RID_ATOMIC_CANCEL:
10878 statement = cp_parser_transaction (parser, token);
10879 break;
10880 case RID_TRANSACTION_CANCEL:
10881 statement = cp_parser_transaction_cancel (parser);
10882 break;
10884 default:
10885 /* It might be a keyword like `int' that can start a
10886 declaration-statement. */
10887 break;
10890 else if (token->type == CPP_NAME)
10892 /* If the next token is a `:', then we are looking at a
10893 labeled-statement. */
10894 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10895 if (token->type == CPP_COLON)
10897 /* Looks like a labeled-statement with an ordinary label.
10898 Parse the label, and then use tail recursion to parse
10899 the statement. */
10901 cp_parser_label_for_labeled_statement (parser, std_attrs);
10902 in_compound = false;
10903 goto restart;
10906 /* Anything that starts with a `{' must be a compound-statement. */
10907 else if (token->type == CPP_OPEN_BRACE)
10908 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10909 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10910 a statement all its own. */
10911 else if (token->type == CPP_PRAGMA)
10913 /* Only certain OpenMP pragmas are attached to statements, and thus
10914 are considered statements themselves. All others are not. In
10915 the context of a compound, accept the pragma as a "statement" and
10916 return so that we can check for a close brace. Otherwise we
10917 require a real statement and must go back and read one. */
10918 if (in_compound)
10919 cp_parser_pragma (parser, pragma_compound, if_p);
10920 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10921 goto restart;
10922 return;
10924 else if (token->type == CPP_EOF)
10926 cp_parser_error (parser, "expected statement");
10927 return;
10930 /* Everything else must be a declaration-statement or an
10931 expression-statement. Try for the declaration-statement
10932 first, unless we are looking at a `;', in which case we know that
10933 we have an expression-statement. */
10934 if (!statement)
10936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10938 if (std_attrs != NULL_TREE)
10940 /* Attributes should be parsed as part of the the
10941 declaration, so let's un-parse them. */
10942 saved_tokens.rollback();
10943 std_attrs = NULL_TREE;
10946 cp_parser_parse_tentatively (parser);
10947 /* Try to parse the declaration-statement. */
10948 cp_parser_declaration_statement (parser);
10949 /* If that worked, we're done. */
10950 if (cp_parser_parse_definitely (parser))
10951 return;
10953 /* All preceding labels have been parsed at this point. */
10954 if (loc_after_labels != NULL)
10955 *loc_after_labels = statement_location;
10957 /* Look for an expression-statement instead. */
10958 statement = cp_parser_expression_statement (parser, in_statement_expr);
10960 /* Handle [[fallthrough]];. */
10961 if (attribute_fallthrough_p (std_attrs))
10963 /* The next token after the fallthrough attribute is ';'. */
10964 if (statement == NULL_TREE)
10966 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10967 statement = build_call_expr_internal_loc (statement_location,
10968 IFN_FALLTHROUGH,
10969 void_type_node, 0);
10970 finish_expr_stmt (statement);
10972 else
10973 warning_at (statement_location, OPT_Wattributes,
10974 "%<fallthrough%> attribute not followed by %<;%>");
10975 std_attrs = NULL_TREE;
10979 /* Set the line number for the statement. */
10980 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10981 SET_EXPR_LOCATION (statement, statement_location);
10983 /* Allow "[[fallthrough]];", but warn otherwise. */
10984 if (std_attrs != NULL_TREE)
10985 warning_at (attrs_location,
10986 OPT_Wattributes,
10987 "attributes at the beginning of statement are ignored");
10990 /* Append ATTR to attribute list ATTRS. */
10992 static tree
10993 attr_chainon (tree attrs, tree attr)
10995 if (attrs == error_mark_node)
10996 return error_mark_node;
10997 if (attr == error_mark_node)
10998 return error_mark_node;
10999 return chainon (attrs, attr);
11002 /* Parse the label for a labeled-statement, i.e.
11004 identifier :
11005 case constant-expression :
11006 default :
11008 GNU Extension:
11009 case constant-expression ... constant-expression : statement
11011 When a label is parsed without errors, the label is added to the
11012 parse tree by the finish_* functions, so this function doesn't
11013 have to return the label. */
11015 static void
11016 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11018 cp_token *token;
11019 tree label = NULL_TREE;
11020 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11022 /* The next token should be an identifier. */
11023 token = cp_lexer_peek_token (parser->lexer);
11024 if (token->type != CPP_NAME
11025 && token->type != CPP_KEYWORD)
11027 cp_parser_error (parser, "expected labeled-statement");
11028 return;
11031 /* Remember whether this case or a user-defined label is allowed to fall
11032 through to. */
11033 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11035 parser->colon_corrects_to_scope_p = false;
11036 switch (token->keyword)
11038 case RID_CASE:
11040 tree expr, expr_hi;
11041 cp_token *ellipsis;
11043 /* Consume the `case' token. */
11044 cp_lexer_consume_token (parser->lexer);
11045 /* Parse the constant-expression. */
11046 expr = cp_parser_constant_expression (parser);
11047 if (check_for_bare_parameter_packs (expr))
11048 expr = error_mark_node;
11050 ellipsis = cp_lexer_peek_token (parser->lexer);
11051 if (ellipsis->type == CPP_ELLIPSIS)
11053 /* Consume the `...' token. */
11054 cp_lexer_consume_token (parser->lexer);
11055 expr_hi = cp_parser_constant_expression (parser);
11056 if (check_for_bare_parameter_packs (expr_hi))
11057 expr_hi = error_mark_node;
11059 /* We don't need to emit warnings here, as the common code
11060 will do this for us. */
11062 else
11063 expr_hi = NULL_TREE;
11065 if (parser->in_switch_statement_p)
11067 tree l = finish_case_label (token->location, expr, expr_hi);
11068 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11069 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11071 else
11072 error_at (token->location,
11073 "case label %qE not within a switch statement",
11074 expr);
11076 break;
11078 case RID_DEFAULT:
11079 /* Consume the `default' token. */
11080 cp_lexer_consume_token (parser->lexer);
11082 if (parser->in_switch_statement_p)
11084 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11085 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11086 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11088 else
11089 error_at (token->location, "case label not within a switch statement");
11090 break;
11092 default:
11093 /* Anything else must be an ordinary label. */
11094 label = finish_label_stmt (cp_parser_identifier (parser));
11095 if (label && TREE_CODE (label) == LABEL_DECL)
11096 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11097 break;
11100 /* Require the `:' token. */
11101 cp_parser_require (parser, CPP_COLON, RT_COLON);
11103 /* An ordinary label may optionally be followed by attributes.
11104 However, this is only permitted if the attributes are then
11105 followed by a semicolon. This is because, for backward
11106 compatibility, when parsing
11107 lab: __attribute__ ((unused)) int i;
11108 we want the attribute to attach to "i", not "lab". */
11109 if (label != NULL_TREE
11110 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11112 tree attrs;
11113 cp_parser_parse_tentatively (parser);
11114 attrs = cp_parser_gnu_attributes_opt (parser);
11115 if (attrs == NULL_TREE
11116 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11117 cp_parser_abort_tentative_parse (parser);
11118 else if (!cp_parser_parse_definitely (parser))
11120 else
11121 attributes = attr_chainon (attributes, attrs);
11124 if (attributes != NULL_TREE)
11125 cplus_decl_attributes (&label, attributes, 0);
11127 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11130 /* Parse an expression-statement.
11132 expression-statement:
11133 expression [opt] ;
11135 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11136 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11137 indicates whether this expression-statement is part of an
11138 expression statement. */
11140 static tree
11141 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11143 tree statement = NULL_TREE;
11144 cp_token *token = cp_lexer_peek_token (parser->lexer);
11145 location_t loc = token->location;
11147 /* There might be attribute fallthrough. */
11148 tree attr = cp_parser_gnu_attributes_opt (parser);
11150 /* If the next token is a ';', then there is no expression
11151 statement. */
11152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11154 statement = cp_parser_expression (parser);
11155 if (statement == error_mark_node
11156 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11158 cp_parser_skip_to_end_of_block_or_statement (parser);
11159 return error_mark_node;
11163 /* Handle [[fallthrough]];. */
11164 if (attribute_fallthrough_p (attr))
11166 /* The next token after the fallthrough attribute is ';'. */
11167 if (statement == NULL_TREE)
11168 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11169 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11170 void_type_node, 0);
11171 else
11172 warning_at (loc, OPT_Wattributes,
11173 "%<fallthrough%> attribute not followed by %<;%>");
11174 attr = NULL_TREE;
11177 /* Allow "[[fallthrough]];", but warn otherwise. */
11178 if (attr != NULL_TREE)
11179 warning_at (loc, OPT_Wattributes,
11180 "attributes at the beginning of statement are ignored");
11182 /* Give a helpful message for "A<T>::type t;" and the like. */
11183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11184 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11186 if (TREE_CODE (statement) == SCOPE_REF)
11187 error_at (token->location, "need %<typename%> before %qE because "
11188 "%qT is a dependent scope",
11189 statement, TREE_OPERAND (statement, 0));
11190 else if (is_overloaded_fn (statement)
11191 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11193 /* A::A a; */
11194 tree fn = get_first_fn (statement);
11195 error_at (token->location,
11196 "%<%T::%D%> names the constructor, not the type",
11197 DECL_CONTEXT (fn), DECL_NAME (fn));
11201 /* Consume the final `;'. */
11202 cp_parser_consume_semicolon_at_end_of_statement (parser);
11204 if (in_statement_expr
11205 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11206 /* This is the final expression statement of a statement
11207 expression. */
11208 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11209 else if (statement)
11210 statement = finish_expr_stmt (statement);
11212 return statement;
11215 /* Parse a compound-statement.
11217 compound-statement:
11218 { statement-seq [opt] }
11220 GNU extension:
11222 compound-statement:
11223 { label-declaration-seq [opt] statement-seq [opt] }
11225 label-declaration-seq:
11226 label-declaration
11227 label-declaration-seq label-declaration
11229 Returns a tree representing the statement. */
11231 static tree
11232 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11233 int bcs_flags, bool function_body)
11235 tree compound_stmt;
11236 matching_braces braces;
11238 /* Consume the `{'. */
11239 if (!braces.require_open (parser))
11240 return error_mark_node;
11241 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11242 && !function_body && cxx_dialect < cxx14)
11243 pedwarn (input_location, OPT_Wpedantic,
11244 "compound-statement in %<constexpr%> function");
11245 /* Begin the compound-statement. */
11246 compound_stmt = begin_compound_stmt (bcs_flags);
11247 /* If the next keyword is `__label__' we have a label declaration. */
11248 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11249 cp_parser_label_declaration (parser);
11250 /* Parse an (optional) statement-seq. */
11251 cp_parser_statement_seq_opt (parser, in_statement_expr);
11252 /* Finish the compound-statement. */
11253 finish_compound_stmt (compound_stmt);
11254 /* Consume the `}'. */
11255 braces.require_close (parser);
11257 return compound_stmt;
11260 /* Parse an (optional) statement-seq.
11262 statement-seq:
11263 statement
11264 statement-seq [opt] statement */
11266 static void
11267 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11269 /* Scan statements until there aren't any more. */
11270 while (true)
11272 cp_token *token = cp_lexer_peek_token (parser->lexer);
11274 /* If we are looking at a `}', then we have run out of
11275 statements; the same is true if we have reached the end
11276 of file, or have stumbled upon a stray '@end'. */
11277 if (token->type == CPP_CLOSE_BRACE
11278 || token->type == CPP_EOF
11279 || token->type == CPP_PRAGMA_EOL
11280 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11281 break;
11283 /* If we are in a compound statement and find 'else' then
11284 something went wrong. */
11285 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11287 if (parser->in_statement & IN_IF_STMT)
11288 break;
11289 else
11291 token = cp_lexer_consume_token (parser->lexer);
11292 error_at (token->location, "%<else%> without a previous %<if%>");
11296 /* Parse the statement. */
11297 cp_parser_statement (parser, in_statement_expr, true, NULL);
11301 /* Return true if this is the C++20 version of range-based-for with
11302 init-statement. */
11304 static bool
11305 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11307 bool r = false;
11309 /* Save tokens so that we can put them back. */
11310 cp_lexer_save_tokens (parser->lexer);
11312 /* There has to be an unnested ; followed by an unnested :. */
11313 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11314 /*recovering=*/false,
11315 CPP_SEMICOLON,
11316 /*consume_paren=*/false) != -1)
11317 goto out;
11319 /* We found the semicolon, eat it now. */
11320 cp_lexer_consume_token (parser->lexer);
11322 /* Now look for ':' that is not nested in () or {}. */
11323 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11324 /*recovering=*/false,
11325 CPP_COLON,
11326 /*consume_paren=*/false) == -1);
11328 out:
11329 /* Roll back the tokens we skipped. */
11330 cp_lexer_rollback_tokens (parser->lexer);
11332 return r;
11335 /* Return true if we're looking at (init; cond), false otherwise. */
11337 static bool
11338 cp_parser_init_statement_p (cp_parser *parser)
11340 /* Save tokens so that we can put them back. */
11341 cp_lexer_save_tokens (parser->lexer);
11343 /* Look for ';' that is not nested in () or {}. */
11344 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11345 /*recovering=*/false,
11346 CPP_SEMICOLON,
11347 /*consume_paren=*/false);
11349 /* Roll back the tokens we skipped. */
11350 cp_lexer_rollback_tokens (parser->lexer);
11352 return ret == -1;
11355 /* Parse a selection-statement.
11357 selection-statement:
11358 if ( init-statement [opt] condition ) statement
11359 if ( init-statement [opt] condition ) statement else statement
11360 switch ( init-statement [opt] condition ) statement
11362 Returns the new IF_STMT or SWITCH_STMT.
11364 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11365 is a (possibly labeled) if statement which is not enclosed in
11366 braces and has an else clause. This is used to implement
11367 -Wparentheses.
11369 CHAIN is a vector of if-else-if conditions. This is used to implement
11370 -Wduplicated-cond. */
11372 static tree
11373 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11374 vec<tree> *chain)
11376 cp_token *token;
11377 enum rid keyword;
11378 token_indent_info guard_tinfo;
11380 if (if_p != NULL)
11381 *if_p = false;
11383 /* Peek at the next token. */
11384 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11385 guard_tinfo = get_token_indent_info (token);
11387 /* See what kind of keyword it is. */
11388 keyword = token->keyword;
11389 switch (keyword)
11391 case RID_IF:
11392 case RID_SWITCH:
11394 tree statement;
11395 tree condition;
11397 bool cx = false;
11398 if (keyword == RID_IF
11399 && cp_lexer_next_token_is_keyword (parser->lexer,
11400 RID_CONSTEXPR))
11402 cx = true;
11403 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11404 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11405 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11406 "with -std=c++17 or -std=gnu++17");
11409 /* Look for the `('. */
11410 matching_parens parens;
11411 if (!parens.require_open (parser))
11413 cp_parser_skip_to_end_of_statement (parser);
11414 return error_mark_node;
11417 /* Begin the selection-statement. */
11418 if (keyword == RID_IF)
11420 statement = begin_if_stmt ();
11421 IF_STMT_CONSTEXPR_P (statement) = cx;
11423 else
11424 statement = begin_switch_stmt ();
11426 /* Parse the optional init-statement. */
11427 if (cp_parser_init_statement_p (parser))
11429 tree decl;
11430 if (cxx_dialect < cxx17)
11431 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11432 "init-statement in selection statements only available "
11433 "with -std=c++17 or -std=gnu++17");
11434 cp_parser_init_statement (parser, &decl);
11437 /* Parse the condition. */
11438 condition = cp_parser_condition (parser);
11439 /* Look for the `)'. */
11440 if (!parens.require_close (parser))
11441 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11442 /*consume_paren=*/true);
11444 if (keyword == RID_IF)
11446 bool nested_if;
11447 unsigned char in_statement;
11449 /* Add the condition. */
11450 condition = finish_if_stmt_cond (condition, statement);
11452 if (warn_duplicated_cond)
11453 warn_duplicated_cond_add_or_warn (token->location, condition,
11454 &chain);
11456 /* Parse the then-clause. */
11457 in_statement = parser->in_statement;
11458 parser->in_statement |= IN_IF_STMT;
11460 /* Outside a template, the non-selected branch of a constexpr
11461 if is a 'discarded statement', i.e. unevaluated. */
11462 bool was_discarded = in_discarded_stmt;
11463 bool discard_then = (cx && !processing_template_decl
11464 && integer_zerop (condition));
11465 if (discard_then)
11467 in_discarded_stmt = true;
11468 ++c_inhibit_evaluation_warnings;
11471 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11472 guard_tinfo);
11474 parser->in_statement = in_statement;
11476 finish_then_clause (statement);
11478 if (discard_then)
11480 THEN_CLAUSE (statement) = NULL_TREE;
11481 in_discarded_stmt = was_discarded;
11482 --c_inhibit_evaluation_warnings;
11485 /* If the next token is `else', parse the else-clause. */
11486 if (cp_lexer_next_token_is_keyword (parser->lexer,
11487 RID_ELSE))
11489 bool discard_else = (cx && !processing_template_decl
11490 && integer_nonzerop (condition));
11491 if (discard_else)
11493 in_discarded_stmt = true;
11494 ++c_inhibit_evaluation_warnings;
11497 guard_tinfo
11498 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11499 /* Consume the `else' keyword. */
11500 cp_lexer_consume_token (parser->lexer);
11501 if (warn_duplicated_cond)
11503 if (cp_lexer_next_token_is_keyword (parser->lexer,
11504 RID_IF)
11505 && chain == NULL)
11507 /* We've got "if (COND) else if (COND2)". Start
11508 the condition chain and add COND as the first
11509 element. */
11510 chain = new vec<tree> ();
11511 if (!CONSTANT_CLASS_P (condition)
11512 && !TREE_SIDE_EFFECTS (condition))
11514 /* Wrap it in a NOP_EXPR so that we can set the
11515 location of the condition. */
11516 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11517 condition);
11518 SET_EXPR_LOCATION (e, token->location);
11519 chain->safe_push (e);
11522 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11523 RID_IF))
11525 /* This is if-else without subsequent if. Zap the
11526 condition chain; we would have already warned at
11527 this point. */
11528 delete chain;
11529 chain = NULL;
11532 begin_else_clause (statement);
11533 /* Parse the else-clause. */
11534 cp_parser_implicitly_scoped_statement (parser, NULL,
11535 guard_tinfo, chain);
11537 finish_else_clause (statement);
11539 /* If we are currently parsing a then-clause, then
11540 IF_P will not be NULL. We set it to true to
11541 indicate that this if statement has an else clause.
11542 This may trigger the Wparentheses warning below
11543 when we get back up to the parent if statement. */
11544 if (if_p != NULL)
11545 *if_p = true;
11547 if (discard_else)
11549 ELSE_CLAUSE (statement) = NULL_TREE;
11550 in_discarded_stmt = was_discarded;
11551 --c_inhibit_evaluation_warnings;
11554 else
11556 /* This if statement does not have an else clause. If
11557 NESTED_IF is true, then the then-clause has an if
11558 statement which does have an else clause. We warn
11559 about the potential ambiguity. */
11560 if (nested_if)
11561 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11562 "suggest explicit braces to avoid ambiguous"
11563 " %<else%>");
11564 if (warn_duplicated_cond)
11566 /* We don't need the condition chain anymore. */
11567 delete chain;
11568 chain = NULL;
11572 /* Now we're all done with the if-statement. */
11573 finish_if_stmt (statement);
11575 else
11577 bool in_switch_statement_p;
11578 unsigned char in_statement;
11580 /* Add the condition. */
11581 finish_switch_cond (condition, statement);
11583 /* Parse the body of the switch-statement. */
11584 in_switch_statement_p = parser->in_switch_statement_p;
11585 in_statement = parser->in_statement;
11586 parser->in_switch_statement_p = true;
11587 parser->in_statement |= IN_SWITCH_STMT;
11588 cp_parser_implicitly_scoped_statement (parser, if_p,
11589 guard_tinfo);
11590 parser->in_switch_statement_p = in_switch_statement_p;
11591 parser->in_statement = in_statement;
11593 /* Now we're all done with the switch-statement. */
11594 finish_switch_stmt (statement);
11597 return statement;
11599 break;
11601 default:
11602 cp_parser_error (parser, "expected selection-statement");
11603 return error_mark_node;
11607 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11608 If we have seen at least one decl-specifier, and the next token
11609 is not a parenthesis, then we must be looking at a declaration.
11610 (After "int (" we might be looking at a functional cast.) */
11612 static void
11613 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11614 bool any_specifiers_p)
11616 if (any_specifiers_p
11617 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11618 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11619 && !cp_parser_error_occurred (parser))
11620 cp_parser_commit_to_tentative_parse (parser);
11623 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11624 The declarator shall not specify a function or an array. Returns
11625 TRUE if the declarator is valid, FALSE otherwise. */
11627 static bool
11628 cp_parser_check_condition_declarator (cp_parser* parser,
11629 cp_declarator *declarator,
11630 location_t loc)
11632 if (declarator == cp_error_declarator
11633 || function_declarator_p (declarator)
11634 || declarator->kind == cdk_array)
11636 if (declarator == cp_error_declarator)
11637 /* Already complained. */;
11638 else if (declarator->kind == cdk_array)
11639 error_at (loc, "condition declares an array");
11640 else
11641 error_at (loc, "condition declares a function");
11642 if (parser->fully_implicit_function_template_p)
11643 abort_fully_implicit_template (parser);
11644 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11645 /*or_comma=*/false,
11646 /*consume_paren=*/false);
11647 return false;
11649 else
11650 return true;
11653 /* Parse a condition.
11655 condition:
11656 expression
11657 type-specifier-seq declarator = initializer-clause
11658 type-specifier-seq declarator braced-init-list
11660 GNU Extension:
11662 condition:
11663 type-specifier-seq declarator asm-specification [opt]
11664 attributes [opt] = assignment-expression
11666 Returns the expression that should be tested. */
11668 static tree
11669 cp_parser_condition (cp_parser* parser)
11671 cp_decl_specifier_seq type_specifiers;
11672 const char *saved_message;
11673 int declares_class_or_enum;
11675 /* Try the declaration first. */
11676 cp_parser_parse_tentatively (parser);
11677 /* New types are not allowed in the type-specifier-seq for a
11678 condition. */
11679 saved_message = parser->type_definition_forbidden_message;
11680 parser->type_definition_forbidden_message
11681 = G_("types may not be defined in conditions");
11682 /* Parse the type-specifier-seq. */
11683 cp_parser_decl_specifier_seq (parser,
11684 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11685 &type_specifiers,
11686 &declares_class_or_enum);
11687 /* Restore the saved message. */
11688 parser->type_definition_forbidden_message = saved_message;
11690 cp_parser_maybe_commit_to_declaration (parser,
11691 type_specifiers.any_specifiers_p);
11693 /* If all is well, we might be looking at a declaration. */
11694 if (!cp_parser_error_occurred (parser))
11696 tree decl;
11697 tree asm_specification;
11698 tree attributes;
11699 cp_declarator *declarator;
11700 tree initializer = NULL_TREE;
11701 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11703 /* Parse the declarator. */
11704 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11705 /*ctor_dtor_or_conv_p=*/NULL,
11706 /*parenthesized_p=*/NULL,
11707 /*member_p=*/false,
11708 /*friend_p=*/false);
11709 /* Parse the attributes. */
11710 attributes = cp_parser_attributes_opt (parser);
11711 /* Parse the asm-specification. */
11712 asm_specification = cp_parser_asm_specification_opt (parser);
11713 /* If the next token is not an `=' or '{', then we might still be
11714 looking at an expression. For example:
11716 if (A(a).x)
11718 looks like a decl-specifier-seq and a declarator -- but then
11719 there is no `=', so this is an expression. */
11720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11721 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11722 cp_parser_simulate_error (parser);
11724 /* If we did see an `=' or '{', then we are looking at a declaration
11725 for sure. */
11726 if (cp_parser_parse_definitely (parser))
11728 tree pushed_scope;
11729 bool non_constant_p = false;
11730 int flags = LOOKUP_ONLYCONVERTING;
11732 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11733 return error_mark_node;
11735 /* Create the declaration. */
11736 decl = start_decl (declarator, &type_specifiers,
11737 /*initialized_p=*/true,
11738 attributes, /*prefix_attributes=*/NULL_TREE,
11739 &pushed_scope);
11741 /* Parse the initializer. */
11742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11744 initializer = cp_parser_braced_list (parser, &non_constant_p);
11745 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11746 flags = 0;
11748 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11750 /* Consume the `='. */
11751 cp_lexer_consume_token (parser->lexer);
11752 initializer = cp_parser_initializer_clause (parser,
11753 &non_constant_p);
11755 else
11757 cp_parser_error (parser, "expected initializer");
11758 initializer = error_mark_node;
11760 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11761 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11763 /* Process the initializer. */
11764 cp_finish_decl (decl,
11765 initializer, !non_constant_p,
11766 asm_specification,
11767 flags);
11769 if (pushed_scope)
11770 pop_scope (pushed_scope);
11772 return convert_from_reference (decl);
11775 /* If we didn't even get past the declarator successfully, we are
11776 definitely not looking at a declaration. */
11777 else
11778 cp_parser_abort_tentative_parse (parser);
11780 /* Otherwise, we are looking at an expression. */
11781 return cp_parser_expression (parser);
11784 /* Parses a for-statement or range-for-statement until the closing ')',
11785 not included. */
11787 static tree
11788 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11790 tree init, scope, decl;
11791 bool is_range_for;
11793 /* Begin the for-statement. */
11794 scope = begin_for_scope (&init);
11796 /* Parse the initialization. */
11797 is_range_for = cp_parser_init_statement (parser, &decl);
11799 if (is_range_for)
11800 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11801 else
11802 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11805 static tree
11806 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11807 unsigned short unroll)
11809 /* Normal for loop */
11810 tree condition = NULL_TREE;
11811 tree expression = NULL_TREE;
11812 tree stmt;
11814 stmt = begin_for_stmt (scope, init);
11815 /* The init-statement has already been parsed in
11816 cp_parser_init_statement, so no work is needed here. */
11817 finish_init_stmt (stmt);
11819 /* If there's a condition, process it. */
11820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11821 condition = cp_parser_condition (parser);
11822 else if (ivdep)
11824 cp_parser_error (parser, "missing loop condition in loop with "
11825 "%<GCC ivdep%> pragma");
11826 condition = error_mark_node;
11828 else if (unroll)
11830 cp_parser_error (parser, "missing loop condition in loop with "
11831 "%<GCC unroll%> pragma");
11832 condition = error_mark_node;
11834 finish_for_cond (condition, stmt, ivdep, unroll);
11835 /* Look for the `;'. */
11836 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11838 /* If there's an expression, process it. */
11839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11840 expression = cp_parser_expression (parser);
11841 finish_for_expr (expression, stmt);
11843 return stmt;
11846 /* Tries to parse a range-based for-statement:
11848 range-based-for:
11849 decl-specifier-seq declarator : expression
11851 The decl-specifier-seq declarator and the `:' are already parsed by
11852 cp_parser_init_statement. If processing_template_decl it returns a
11853 newly created RANGE_FOR_STMT; if not, it is converted to a
11854 regular FOR_STMT. */
11856 static tree
11857 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11858 bool ivdep, unsigned short unroll)
11860 tree stmt, range_expr;
11861 auto_vec <cxx_binding *, 16> bindings;
11862 auto_vec <tree, 16> names;
11863 tree decomp_first_name = NULL_TREE;
11864 unsigned int decomp_cnt = 0;
11866 /* Get the range declaration momentarily out of the way so that
11867 the range expression doesn't clash with it. */
11868 if (range_decl != error_mark_node)
11870 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11872 tree v = DECL_VALUE_EXPR (range_decl);
11873 /* For decomposition declaration get all of the corresponding
11874 declarations out of the way. */
11875 if (TREE_CODE (v) == ARRAY_REF
11876 && VAR_P (TREE_OPERAND (v, 0))
11877 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11879 tree d = range_decl;
11880 range_decl = TREE_OPERAND (v, 0);
11881 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11882 decomp_first_name = d;
11883 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11885 tree name = DECL_NAME (d);
11886 names.safe_push (name);
11887 bindings.safe_push (IDENTIFIER_BINDING (name));
11888 IDENTIFIER_BINDING (name)
11889 = IDENTIFIER_BINDING (name)->previous;
11893 if (names.is_empty ())
11895 tree name = DECL_NAME (range_decl);
11896 names.safe_push (name);
11897 bindings.safe_push (IDENTIFIER_BINDING (name));
11898 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11902 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11904 bool expr_non_constant_p;
11905 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11907 else
11908 range_expr = cp_parser_expression (parser);
11910 /* Put the range declaration(s) back into scope. */
11911 for (unsigned int i = 0; i < names.length (); i++)
11913 cxx_binding *binding = bindings[i];
11914 binding->previous = IDENTIFIER_BINDING (names[i]);
11915 IDENTIFIER_BINDING (names[i]) = binding;
11918 /* If in template, STMT is converted to a normal for-statement
11919 at instantiation. If not, it is done just ahead. */
11920 if (processing_template_decl)
11922 if (check_for_bare_parameter_packs (range_expr))
11923 range_expr = error_mark_node;
11924 stmt = begin_range_for_stmt (scope, init);
11925 if (ivdep)
11926 RANGE_FOR_IVDEP (stmt) = 1;
11927 if (unroll)
11928 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11929 finish_range_for_decl (stmt, range_decl, range_expr);
11930 if (!type_dependent_expression_p (range_expr)
11931 /* do_auto_deduction doesn't mess with template init-lists. */
11932 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11933 do_range_for_auto_deduction (range_decl, range_expr);
11935 else
11937 stmt = begin_for_stmt (scope, init);
11938 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11939 decomp_first_name, decomp_cnt, ivdep,
11940 unroll);
11942 return stmt;
11945 /* Subroutine of cp_convert_range_for: given the initializer expression,
11946 builds up the range temporary. */
11948 static tree
11949 build_range_temp (tree range_expr)
11951 tree range_type, range_temp;
11953 /* Find out the type deduced by the declaration
11954 `auto &&__range = range_expr'. */
11955 range_type = cp_build_reference_type (make_auto (), true);
11956 range_type = do_auto_deduction (range_type, range_expr,
11957 type_uses_auto (range_type));
11959 /* Create the __range variable. */
11960 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
11961 range_type);
11962 TREE_USED (range_temp) = 1;
11963 DECL_ARTIFICIAL (range_temp) = 1;
11965 return range_temp;
11968 /* Used by cp_parser_range_for in template context: we aren't going to
11969 do a full conversion yet, but we still need to resolve auto in the
11970 type of the for-range-declaration if present. This is basically
11971 a shortcut version of cp_convert_range_for. */
11973 static void
11974 do_range_for_auto_deduction (tree decl, tree range_expr)
11976 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11977 if (auto_node)
11979 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11980 range_temp = convert_from_reference (build_range_temp (range_expr));
11981 iter_type = (cp_parser_perform_range_for_lookup
11982 (range_temp, &begin_dummy, &end_dummy));
11983 if (iter_type)
11985 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11986 iter_type);
11987 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11988 RO_UNARY_STAR,
11989 tf_warning_or_error);
11990 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11991 iter_decl, auto_node);
11996 /* Converts a range-based for-statement into a normal
11997 for-statement, as per the definition.
11999 for (RANGE_DECL : RANGE_EXPR)
12000 BLOCK
12002 should be equivalent to:
12005 auto &&__range = RANGE_EXPR;
12006 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12007 __begin != __end;
12008 ++__begin)
12010 RANGE_DECL = *__begin;
12011 BLOCK
12015 If RANGE_EXPR is an array:
12016 BEGIN_EXPR = __range
12017 END_EXPR = __range + ARRAY_SIZE(__range)
12018 Else if RANGE_EXPR has a member 'begin' or 'end':
12019 BEGIN_EXPR = __range.begin()
12020 END_EXPR = __range.end()
12021 Else:
12022 BEGIN_EXPR = begin(__range)
12023 END_EXPR = end(__range);
12025 If __range has a member 'begin' but not 'end', or vice versa, we must
12026 still use the second alternative (it will surely fail, however).
12027 When calling begin()/end() in the third alternative we must use
12028 argument dependent lookup, but always considering 'std' as an associated
12029 namespace. */
12031 tree
12032 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12033 tree decomp_first_name, unsigned int decomp_cnt,
12034 bool ivdep, unsigned short unroll)
12036 tree begin, end;
12037 tree iter_type, begin_expr, end_expr;
12038 tree condition, expression;
12040 range_expr = mark_lvalue_use (range_expr);
12042 if (range_decl == error_mark_node || range_expr == error_mark_node)
12043 /* If an error happened previously do nothing or else a lot of
12044 unhelpful errors would be issued. */
12045 begin_expr = end_expr = iter_type = error_mark_node;
12046 else
12048 tree range_temp;
12050 if (VAR_P (range_expr)
12051 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12052 /* Can't bind a reference to an array of runtime bound. */
12053 range_temp = range_expr;
12054 else
12056 range_temp = build_range_temp (range_expr);
12057 pushdecl (range_temp);
12058 cp_finish_decl (range_temp, range_expr,
12059 /*is_constant_init*/false, NULL_TREE,
12060 LOOKUP_ONLYCONVERTING);
12061 range_temp = convert_from_reference (range_temp);
12063 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12064 &begin_expr, &end_expr);
12067 /* The new for initialization statement. */
12068 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12069 iter_type);
12070 TREE_USED (begin) = 1;
12071 DECL_ARTIFICIAL (begin) = 1;
12072 pushdecl (begin);
12073 cp_finish_decl (begin, begin_expr,
12074 /*is_constant_init*/false, NULL_TREE,
12075 LOOKUP_ONLYCONVERTING);
12077 if (cxx_dialect >= cxx17)
12078 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12079 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12080 TREE_USED (end) = 1;
12081 DECL_ARTIFICIAL (end) = 1;
12082 pushdecl (end);
12083 cp_finish_decl (end, end_expr,
12084 /*is_constant_init*/false, NULL_TREE,
12085 LOOKUP_ONLYCONVERTING);
12087 finish_init_stmt (statement);
12089 /* The new for condition. */
12090 condition = build_x_binary_op (input_location, NE_EXPR,
12091 begin, ERROR_MARK,
12092 end, ERROR_MARK,
12093 NULL, tf_warning_or_error);
12094 finish_for_cond (condition, statement, ivdep, unroll);
12096 /* The new increment expression. */
12097 expression = finish_unary_op_expr (input_location,
12098 PREINCREMENT_EXPR, begin,
12099 tf_warning_or_error);
12100 finish_for_expr (expression, statement);
12102 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12103 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12105 /* The declaration is initialized with *__begin inside the loop body. */
12106 cp_finish_decl (range_decl,
12107 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12108 tf_warning_or_error),
12109 /*is_constant_init*/false, NULL_TREE,
12110 LOOKUP_ONLYCONVERTING);
12111 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12112 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12114 return statement;
12117 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12118 We need to solve both at the same time because the method used
12119 depends on the existence of members begin or end.
12120 Returns the type deduced for the iterator expression. */
12122 static tree
12123 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12125 if (error_operand_p (range))
12127 *begin = *end = error_mark_node;
12128 return error_mark_node;
12131 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12133 error ("range-based %<for%> expression of type %qT "
12134 "has incomplete type", TREE_TYPE (range));
12135 *begin = *end = error_mark_node;
12136 return error_mark_node;
12138 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12140 /* If RANGE is an array, we will use pointer arithmetic. */
12141 *begin = decay_conversion (range, tf_warning_or_error);
12142 *end = build_binary_op (input_location, PLUS_EXPR,
12143 range,
12144 array_type_nelts_top (TREE_TYPE (range)),
12145 false);
12146 return TREE_TYPE (*begin);
12148 else
12150 /* If it is not an array, we must do a bit of magic. */
12151 tree id_begin, id_end;
12152 tree member_begin, member_end;
12154 *begin = *end = error_mark_node;
12156 id_begin = get_identifier ("begin");
12157 id_end = get_identifier ("end");
12158 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12159 /*protect=*/2, /*want_type=*/false,
12160 tf_warning_or_error);
12161 member_end = lookup_member (TREE_TYPE (range), id_end,
12162 /*protect=*/2, /*want_type=*/false,
12163 tf_warning_or_error);
12165 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12167 /* Use the member functions. */
12168 *begin = cp_parser_range_for_member_function (range, id_begin);
12169 *end = cp_parser_range_for_member_function (range, id_end);
12171 else
12173 /* Use global functions with ADL. */
12174 vec<tree, va_gc> *vec;
12175 vec = make_tree_vector ();
12177 vec_safe_push (vec, range);
12179 member_begin = perform_koenig_lookup (id_begin, vec,
12180 tf_warning_or_error);
12181 *begin = finish_call_expr (member_begin, &vec, false, true,
12182 tf_warning_or_error);
12183 member_end = perform_koenig_lookup (id_end, vec,
12184 tf_warning_or_error);
12185 *end = finish_call_expr (member_end, &vec, false, true,
12186 tf_warning_or_error);
12188 release_tree_vector (vec);
12191 /* Last common checks. */
12192 if (*begin == error_mark_node || *end == error_mark_node)
12194 /* If one of the expressions is an error do no more checks. */
12195 *begin = *end = error_mark_node;
12196 return error_mark_node;
12198 else if (type_dependent_expression_p (*begin)
12199 || type_dependent_expression_p (*end))
12200 /* Can happen, when, eg, in a template context, Koenig lookup
12201 can't resolve begin/end (c++/58503). */
12202 return NULL_TREE;
12203 else
12205 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12206 /* The unqualified type of the __begin and __end temporaries should
12207 be the same, as required by the multiple auto declaration. */
12208 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12210 if (cxx_dialect >= cxx17
12211 && (build_x_binary_op (input_location, NE_EXPR,
12212 *begin, ERROR_MARK,
12213 *end, ERROR_MARK,
12214 NULL, tf_none)
12215 != error_mark_node))
12216 /* P0184R0 allows __begin and __end to have different types,
12217 but make sure they are comparable so we can give a better
12218 diagnostic. */;
12219 else
12220 error ("inconsistent begin/end types in range-based %<for%> "
12221 "statement: %qT and %qT",
12222 TREE_TYPE (*begin), TREE_TYPE (*end));
12224 return iter_type;
12229 /* Helper function for cp_parser_perform_range_for_lookup.
12230 Builds a tree for RANGE.IDENTIFIER(). */
12232 static tree
12233 cp_parser_range_for_member_function (tree range, tree identifier)
12235 tree member, res;
12236 vec<tree, va_gc> *vec;
12238 member = finish_class_member_access_expr (range, identifier,
12239 false, tf_warning_or_error);
12240 if (member == error_mark_node)
12241 return error_mark_node;
12243 vec = make_tree_vector ();
12244 res = finish_call_expr (member, &vec,
12245 /*disallow_virtual=*/false,
12246 /*koenig_p=*/false,
12247 tf_warning_or_error);
12248 release_tree_vector (vec);
12249 return res;
12252 /* Parse an iteration-statement.
12254 iteration-statement:
12255 while ( condition ) statement
12256 do statement while ( expression ) ;
12257 for ( init-statement condition [opt] ; expression [opt] )
12258 statement
12260 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12262 static tree
12263 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12264 unsigned short unroll)
12266 cp_token *token;
12267 enum rid keyword;
12268 tree statement;
12269 unsigned char in_statement;
12270 token_indent_info guard_tinfo;
12272 /* Peek at the next token. */
12273 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12274 if (!token)
12275 return error_mark_node;
12277 guard_tinfo = get_token_indent_info (token);
12279 /* Remember whether or not we are already within an iteration
12280 statement. */
12281 in_statement = parser->in_statement;
12283 /* See what kind of keyword it is. */
12284 keyword = token->keyword;
12285 switch (keyword)
12287 case RID_WHILE:
12289 tree condition;
12291 /* Begin the while-statement. */
12292 statement = begin_while_stmt ();
12293 /* Look for the `('. */
12294 matching_parens parens;
12295 parens.require_open (parser);
12296 /* Parse the condition. */
12297 condition = cp_parser_condition (parser);
12298 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12299 /* Look for the `)'. */
12300 parens.require_close (parser);
12301 /* Parse the dependent statement. */
12302 parser->in_statement = IN_ITERATION_STMT;
12303 bool prev = note_iteration_stmt_body_start ();
12304 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12305 note_iteration_stmt_body_end (prev);
12306 parser->in_statement = in_statement;
12307 /* We're done with the while-statement. */
12308 finish_while_stmt (statement);
12310 break;
12312 case RID_DO:
12314 tree expression;
12316 /* Begin the do-statement. */
12317 statement = begin_do_stmt ();
12318 /* Parse the body of the do-statement. */
12319 parser->in_statement = IN_ITERATION_STMT;
12320 bool prev = note_iteration_stmt_body_start ();
12321 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12322 note_iteration_stmt_body_end (prev);
12323 parser->in_statement = in_statement;
12324 finish_do_body (statement);
12325 /* Look for the `while' keyword. */
12326 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12327 /* Look for the `('. */
12328 matching_parens parens;
12329 parens.require_open (parser);
12330 /* Parse the expression. */
12331 expression = cp_parser_expression (parser);
12332 /* We're done with the do-statement. */
12333 finish_do_stmt (expression, statement, ivdep, unroll);
12334 /* Look for the `)'. */
12335 parens.require_close (parser);
12336 /* Look for the `;'. */
12337 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12339 break;
12341 case RID_FOR:
12343 /* Look for the `('. */
12344 matching_parens parens;
12345 parens.require_open (parser);
12347 statement = cp_parser_for (parser, ivdep, unroll);
12349 /* Look for the `)'. */
12350 parens.require_close (parser);
12352 /* Parse the body of the for-statement. */
12353 parser->in_statement = IN_ITERATION_STMT;
12354 bool prev = note_iteration_stmt_body_start ();
12355 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12356 note_iteration_stmt_body_end (prev);
12357 parser->in_statement = in_statement;
12359 /* We're done with the for-statement. */
12360 finish_for_stmt (statement);
12362 break;
12364 default:
12365 cp_parser_error (parser, "expected iteration-statement");
12366 statement = error_mark_node;
12367 break;
12370 return statement;
12373 /* Parse a init-statement or the declarator of a range-based-for.
12374 Returns true if a range-based-for declaration is seen.
12376 init-statement:
12377 expression-statement
12378 simple-declaration */
12380 static bool
12381 cp_parser_init_statement (cp_parser *parser, tree *decl)
12383 /* If the next token is a `;', then we have an empty
12384 expression-statement. Grammatically, this is also a
12385 simple-declaration, but an invalid one, because it does not
12386 declare anything. Therefore, if we did not handle this case
12387 specially, we would issue an error message about an invalid
12388 declaration. */
12389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12391 bool is_range_for = false;
12392 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12394 /* Try to parse the init-statement. */
12395 if (cp_parser_range_based_for_with_init_p (parser))
12397 tree dummy;
12398 cp_parser_parse_tentatively (parser);
12399 /* Parse the declaration. */
12400 cp_parser_simple_declaration (parser,
12401 /*function_definition_allowed_p=*/false,
12402 &dummy);
12403 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12404 if (!cp_parser_parse_definitely (parser))
12405 /* That didn't work, try to parse it as an expression-statement. */
12406 cp_parser_expression_statement (parser, NULL_TREE);
12408 if (cxx_dialect < cxx2a)
12410 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12411 "range-based %<for%> loops with initializer only "
12412 "available with -std=c++2a or -std=gnu++2a");
12413 *decl = error_mark_node;
12417 /* A colon is used in range-based for. */
12418 parser->colon_corrects_to_scope_p = false;
12420 /* We're going to speculatively look for a declaration, falling back
12421 to an expression, if necessary. */
12422 cp_parser_parse_tentatively (parser);
12423 /* Parse the declaration. */
12424 cp_parser_simple_declaration (parser,
12425 /*function_definition_allowed_p=*/false,
12426 decl);
12427 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12428 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12430 /* It is a range-for, consume the ':'. */
12431 cp_lexer_consume_token (parser->lexer);
12432 is_range_for = true;
12433 if (cxx_dialect < cxx11)
12434 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12435 "range-based %<for%> loops only available with "
12436 "-std=c++11 or -std=gnu++11");
12438 else
12439 /* The ';' is not consumed yet because we told
12440 cp_parser_simple_declaration not to. */
12441 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12443 if (cp_parser_parse_definitely (parser))
12444 return is_range_for;
12445 /* If the tentative parse failed, then we shall need to look for an
12446 expression-statement. */
12448 /* If we are here, it is an expression-statement. */
12449 cp_parser_expression_statement (parser, NULL_TREE);
12450 return false;
12453 /* Parse a jump-statement.
12455 jump-statement:
12456 break ;
12457 continue ;
12458 return expression [opt] ;
12459 return braced-init-list ;
12460 goto identifier ;
12462 GNU extension:
12464 jump-statement:
12465 goto * expression ;
12467 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12469 static tree
12470 cp_parser_jump_statement (cp_parser* parser)
12472 tree statement = error_mark_node;
12473 cp_token *token;
12474 enum rid keyword;
12475 unsigned char in_statement;
12477 /* Peek at the next token. */
12478 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12479 if (!token)
12480 return error_mark_node;
12482 /* See what kind of keyword it is. */
12483 keyword = token->keyword;
12484 switch (keyword)
12486 case RID_BREAK:
12487 in_statement = parser->in_statement & ~IN_IF_STMT;
12488 switch (in_statement)
12490 case 0:
12491 error_at (token->location, "break statement not within loop or switch");
12492 break;
12493 default:
12494 gcc_assert ((in_statement & IN_SWITCH_STMT)
12495 || in_statement == IN_ITERATION_STMT);
12496 statement = finish_break_stmt ();
12497 if (in_statement == IN_ITERATION_STMT)
12498 break_maybe_infinite_loop ();
12499 break;
12500 case IN_OMP_BLOCK:
12501 error_at (token->location, "invalid exit from OpenMP structured block");
12502 break;
12503 case IN_OMP_FOR:
12504 error_at (token->location, "break statement used with OpenMP for loop");
12505 break;
12507 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12508 break;
12510 case RID_CONTINUE:
12511 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12513 case 0:
12514 error_at (token->location, "continue statement not within a loop");
12515 break;
12516 /* Fall through. */
12517 case IN_ITERATION_STMT:
12518 case IN_OMP_FOR:
12519 statement = finish_continue_stmt ();
12520 break;
12521 case IN_OMP_BLOCK:
12522 error_at (token->location, "invalid exit from OpenMP structured block");
12523 break;
12524 default:
12525 gcc_unreachable ();
12527 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12528 break;
12530 case RID_RETURN:
12532 tree expr;
12533 bool expr_non_constant_p;
12535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12537 cp_lexer_set_source_position (parser->lexer);
12538 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12539 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12541 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12542 expr = cp_parser_expression (parser);
12543 else
12544 /* If the next token is a `;', then there is no
12545 expression. */
12546 expr = NULL_TREE;
12547 /* Build the return-statement. */
12548 if (current_function_auto_return_pattern && in_discarded_stmt)
12549 /* Don't deduce from a discarded return statement. */;
12550 else
12551 statement = finish_return_stmt (expr);
12552 /* Look for the final `;'. */
12553 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12555 break;
12557 case RID_GOTO:
12558 if (parser->in_function_body
12559 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12561 error ("%<goto%> in %<constexpr%> function");
12562 cp_function_chain->invalid_constexpr = true;
12565 /* Create the goto-statement. */
12566 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12568 /* Issue a warning about this use of a GNU extension. */
12569 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12570 /* Consume the '*' token. */
12571 cp_lexer_consume_token (parser->lexer);
12572 /* Parse the dependent expression. */
12573 finish_goto_stmt (cp_parser_expression (parser));
12575 else
12576 finish_goto_stmt (cp_parser_identifier (parser));
12577 /* Look for the final `;'. */
12578 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12579 break;
12581 default:
12582 cp_parser_error (parser, "expected jump-statement");
12583 break;
12586 return statement;
12589 /* Parse a declaration-statement.
12591 declaration-statement:
12592 block-declaration */
12594 static void
12595 cp_parser_declaration_statement (cp_parser* parser)
12597 void *p;
12599 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12600 p = obstack_alloc (&declarator_obstack, 0);
12602 /* Parse the block-declaration. */
12603 cp_parser_block_declaration (parser, /*statement_p=*/true);
12605 /* Free any declarators allocated. */
12606 obstack_free (&declarator_obstack, p);
12609 /* Some dependent statements (like `if (cond) statement'), are
12610 implicitly in their own scope. In other words, if the statement is
12611 a single statement (as opposed to a compound-statement), it is
12612 none-the-less treated as if it were enclosed in braces. Any
12613 declarations appearing in the dependent statement are out of scope
12614 after control passes that point. This function parses a statement,
12615 but ensures that is in its own scope, even if it is not a
12616 compound-statement.
12618 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12619 is a (possibly labeled) if statement which is not enclosed in
12620 braces and has an else clause. This is used to implement
12621 -Wparentheses.
12623 CHAIN is a vector of if-else-if conditions. This is used to implement
12624 -Wduplicated-cond.
12626 Returns the new statement. */
12628 static tree
12629 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12630 const token_indent_info &guard_tinfo,
12631 vec<tree> *chain)
12633 tree statement;
12634 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12635 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12636 token_indent_info body_tinfo
12637 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12639 if (if_p != NULL)
12640 *if_p = false;
12642 /* Mark if () ; with a special NOP_EXPR. */
12643 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12645 cp_lexer_consume_token (parser->lexer);
12646 statement = add_stmt (build_empty_stmt (body_loc));
12648 if (guard_tinfo.keyword == RID_IF
12649 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12650 warning_at (body_loc, OPT_Wempty_body,
12651 "suggest braces around empty body in an %<if%> statement");
12652 else if (guard_tinfo.keyword == RID_ELSE)
12653 warning_at (body_loc, OPT_Wempty_body,
12654 "suggest braces around empty body in an %<else%> statement");
12656 /* if a compound is opened, we simply parse the statement directly. */
12657 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12658 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12659 /* If the token is not a `{', then we must take special action. */
12660 else
12662 /* Create a compound-statement. */
12663 statement = begin_compound_stmt (0);
12664 /* Parse the dependent-statement. */
12665 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12666 &body_loc_after_labels);
12667 /* Finish the dummy compound-statement. */
12668 finish_compound_stmt (statement);
12671 token_indent_info next_tinfo
12672 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12673 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12675 if (body_loc_after_labels != UNKNOWN_LOCATION
12676 && next_tinfo.type != CPP_SEMICOLON)
12677 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12678 guard_tinfo.location, guard_tinfo.keyword);
12680 /* Return the statement. */
12681 return statement;
12684 /* For some dependent statements (like `while (cond) statement'), we
12685 have already created a scope. Therefore, even if the dependent
12686 statement is a compound-statement, we do not want to create another
12687 scope. */
12689 static void
12690 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12691 const token_indent_info &guard_tinfo)
12693 /* If the token is a `{', then we must take special action. */
12694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12696 token_indent_info body_tinfo
12697 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12698 location_t loc_after_labels = UNKNOWN_LOCATION;
12700 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12701 &loc_after_labels);
12702 token_indent_info next_tinfo
12703 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12704 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12706 if (loc_after_labels != UNKNOWN_LOCATION
12707 && next_tinfo.type != CPP_SEMICOLON)
12708 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12709 guard_tinfo.location,
12710 guard_tinfo.keyword);
12712 else
12714 /* Avoid calling cp_parser_compound_statement, so that we
12715 don't create a new scope. Do everything else by hand. */
12716 matching_braces braces;
12717 braces.require_open (parser);
12718 /* If the next keyword is `__label__' we have a label declaration. */
12719 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12720 cp_parser_label_declaration (parser);
12721 /* Parse an (optional) statement-seq. */
12722 cp_parser_statement_seq_opt (parser, NULL_TREE);
12723 braces.require_close (parser);
12727 /* Declarations [gram.dcl.dcl] */
12729 /* Parse an optional declaration-sequence.
12731 declaration-seq:
12732 declaration
12733 declaration-seq declaration */
12735 static void
12736 cp_parser_declaration_seq_opt (cp_parser* parser)
12738 while (true)
12740 cp_token *token;
12742 token = cp_lexer_peek_token (parser->lexer);
12744 if (token->type == CPP_CLOSE_BRACE
12745 || token->type == CPP_EOF
12746 || token->type == CPP_PRAGMA_EOL)
12747 break;
12749 if (token->type == CPP_SEMICOLON)
12751 /* A declaration consisting of a single semicolon is
12752 invalid. Allow it unless we're being pedantic. */
12753 cp_lexer_consume_token (parser->lexer);
12754 if (!in_system_header_at (input_location))
12755 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12756 continue;
12759 /* If we're entering or exiting a region that's implicitly
12760 extern "C", modify the lang context appropriately. */
12761 if (!parser->implicit_extern_c && token->implicit_extern_c)
12763 push_lang_context (lang_name_c);
12764 parser->implicit_extern_c = true;
12766 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12768 pop_lang_context ();
12769 parser->implicit_extern_c = false;
12772 if (token->type == CPP_PRAGMA)
12774 /* A top-level declaration can consist solely of a #pragma.
12775 A nested declaration cannot, so this is done here and not
12776 in cp_parser_declaration. (A #pragma at block scope is
12777 handled in cp_parser_statement.) */
12778 cp_parser_pragma (parser, pragma_external, NULL);
12779 continue;
12782 /* Parse the declaration itself. */
12783 cp_parser_declaration (parser);
12787 /* Parse a declaration.
12789 declaration:
12790 block-declaration
12791 function-definition
12792 template-declaration
12793 explicit-instantiation
12794 explicit-specialization
12795 linkage-specification
12796 namespace-definition
12798 C++17:
12799 deduction-guide
12801 GNU extension:
12803 declaration:
12804 __extension__ declaration */
12806 static void
12807 cp_parser_declaration (cp_parser* parser)
12809 cp_token token1;
12810 cp_token token2;
12811 int saved_pedantic;
12812 void *p;
12813 tree attributes = NULL_TREE;
12815 /* Check for the `__extension__' keyword. */
12816 if (cp_parser_extension_opt (parser, &saved_pedantic))
12818 /* Parse the qualified declaration. */
12819 cp_parser_declaration (parser);
12820 /* Restore the PEDANTIC flag. */
12821 pedantic = saved_pedantic;
12823 return;
12826 /* Try to figure out what kind of declaration is present. */
12827 token1 = *cp_lexer_peek_token (parser->lexer);
12829 if (token1.type != CPP_EOF)
12830 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12831 else
12833 token2.type = CPP_EOF;
12834 token2.keyword = RID_MAX;
12837 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12838 p = obstack_alloc (&declarator_obstack, 0);
12840 /* If the next token is `extern' and the following token is a string
12841 literal, then we have a linkage specification. */
12842 if (token1.keyword == RID_EXTERN
12843 && cp_parser_is_pure_string_literal (&token2))
12844 cp_parser_linkage_specification (parser);
12845 /* If the next token is `template', then we have either a template
12846 declaration, an explicit instantiation, or an explicit
12847 specialization. */
12848 else if (token1.keyword == RID_TEMPLATE)
12850 /* `template <>' indicates a template specialization. */
12851 if (token2.type == CPP_LESS
12852 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12853 cp_parser_explicit_specialization (parser);
12854 /* `template <' indicates a template declaration. */
12855 else if (token2.type == CPP_LESS)
12856 cp_parser_template_declaration (parser, /*member_p=*/false);
12857 /* Anything else must be an explicit instantiation. */
12858 else
12859 cp_parser_explicit_instantiation (parser);
12861 /* If the next token is `export', then we have a template
12862 declaration. */
12863 else if (token1.keyword == RID_EXPORT)
12864 cp_parser_template_declaration (parser, /*member_p=*/false);
12865 /* If the next token is `extern', 'static' or 'inline' and the one
12866 after that is `template', we have a GNU extended explicit
12867 instantiation directive. */
12868 else if (cp_parser_allow_gnu_extensions_p (parser)
12869 && (token1.keyword == RID_EXTERN
12870 || token1.keyword == RID_STATIC
12871 || token1.keyword == RID_INLINE)
12872 && token2.keyword == RID_TEMPLATE)
12873 cp_parser_explicit_instantiation (parser);
12874 /* If the next token is `namespace', check for a named or unnamed
12875 namespace definition. */
12876 else if (token1.keyword == RID_NAMESPACE
12877 && (/* A named namespace definition. */
12878 (token2.type == CPP_NAME
12879 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12880 != CPP_EQ))
12881 || (token2.type == CPP_OPEN_SQUARE
12882 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12883 == CPP_OPEN_SQUARE)
12884 /* An unnamed namespace definition. */
12885 || token2.type == CPP_OPEN_BRACE
12886 || token2.keyword == RID_ATTRIBUTE))
12887 cp_parser_namespace_definition (parser);
12888 /* An inline (associated) namespace definition. */
12889 else if (token1.keyword == RID_INLINE
12890 && token2.keyword == RID_NAMESPACE)
12891 cp_parser_namespace_definition (parser);
12892 /* Objective-C++ declaration/definition. */
12893 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12894 cp_parser_objc_declaration (parser, NULL_TREE);
12895 else if (c_dialect_objc ()
12896 && token1.keyword == RID_ATTRIBUTE
12897 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12898 cp_parser_objc_declaration (parser, attributes);
12899 /* At this point we may have a template declared by a concept
12900 introduction. */
12901 else if (flag_concepts
12902 && cp_parser_template_declaration_after_export (parser,
12903 /*member_p=*/false))
12904 /* We did. */;
12905 else
12906 /* Try to parse a block-declaration, or a function-definition. */
12907 cp_parser_block_declaration (parser, /*statement_p=*/false);
12909 /* Free any declarators allocated. */
12910 obstack_free (&declarator_obstack, p);
12913 /* Parse a block-declaration.
12915 block-declaration:
12916 simple-declaration
12917 asm-definition
12918 namespace-alias-definition
12919 using-declaration
12920 using-directive
12922 GNU Extension:
12924 block-declaration:
12925 __extension__ block-declaration
12927 C++0x Extension:
12929 block-declaration:
12930 static_assert-declaration
12932 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12933 part of a declaration-statement. */
12935 static void
12936 cp_parser_block_declaration (cp_parser *parser,
12937 bool statement_p)
12939 cp_token *token1;
12940 int saved_pedantic;
12942 /* Check for the `__extension__' keyword. */
12943 if (cp_parser_extension_opt (parser, &saved_pedantic))
12945 /* Parse the qualified declaration. */
12946 cp_parser_block_declaration (parser, statement_p);
12947 /* Restore the PEDANTIC flag. */
12948 pedantic = saved_pedantic;
12950 return;
12953 /* Peek at the next token to figure out which kind of declaration is
12954 present. */
12955 token1 = cp_lexer_peek_token (parser->lexer);
12957 /* If the next keyword is `asm', we have an asm-definition. */
12958 if (token1->keyword == RID_ASM)
12960 if (statement_p)
12961 cp_parser_commit_to_tentative_parse (parser);
12962 cp_parser_asm_definition (parser);
12964 /* If the next keyword is `namespace', we have a
12965 namespace-alias-definition. */
12966 else if (token1->keyword == RID_NAMESPACE)
12967 cp_parser_namespace_alias_definition (parser);
12968 /* If the next keyword is `using', we have a
12969 using-declaration, a using-directive, or an alias-declaration. */
12970 else if (token1->keyword == RID_USING)
12972 cp_token *token2;
12974 if (statement_p)
12975 cp_parser_commit_to_tentative_parse (parser);
12976 /* If the token after `using' is `namespace', then we have a
12977 using-directive. */
12978 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12979 if (token2->keyword == RID_NAMESPACE)
12980 cp_parser_using_directive (parser);
12981 /* If the second token after 'using' is '=', then we have an
12982 alias-declaration. */
12983 else if (cxx_dialect >= cxx11
12984 && token2->type == CPP_NAME
12985 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12986 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12987 cp_parser_alias_declaration (parser);
12988 /* Otherwise, it's a using-declaration. */
12989 else
12990 cp_parser_using_declaration (parser,
12991 /*access_declaration_p=*/false);
12993 /* If the next keyword is `__label__' we have a misplaced label
12994 declaration. */
12995 else if (token1->keyword == RID_LABEL)
12997 cp_lexer_consume_token (parser->lexer);
12998 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12999 cp_parser_skip_to_end_of_statement (parser);
13000 /* If the next token is now a `;', consume it. */
13001 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13002 cp_lexer_consume_token (parser->lexer);
13004 /* If the next token is `static_assert' we have a static assertion. */
13005 else if (token1->keyword == RID_STATIC_ASSERT)
13006 cp_parser_static_assert (parser, /*member_p=*/false);
13007 /* Anything else must be a simple-declaration. */
13008 else
13009 cp_parser_simple_declaration (parser, !statement_p,
13010 /*maybe_range_for_decl*/NULL);
13013 /* Parse a simple-declaration.
13015 simple-declaration:
13016 decl-specifier-seq [opt] init-declarator-list [opt] ;
13017 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13018 brace-or-equal-initializer ;
13020 init-declarator-list:
13021 init-declarator
13022 init-declarator-list , init-declarator
13024 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13025 function-definition as a simple-declaration.
13027 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13028 parsed declaration if it is an uninitialized single declarator not followed
13029 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13030 if present, will not be consumed. */
13032 static void
13033 cp_parser_simple_declaration (cp_parser* parser,
13034 bool function_definition_allowed_p,
13035 tree *maybe_range_for_decl)
13037 cp_decl_specifier_seq decl_specifiers;
13038 int declares_class_or_enum;
13039 bool saw_declarator;
13040 location_t comma_loc = UNKNOWN_LOCATION;
13041 location_t init_loc = UNKNOWN_LOCATION;
13043 if (maybe_range_for_decl)
13044 *maybe_range_for_decl = NULL_TREE;
13046 /* Defer access checks until we know what is being declared; the
13047 checks for names appearing in the decl-specifier-seq should be
13048 done as if we were in the scope of the thing being declared. */
13049 push_deferring_access_checks (dk_deferred);
13051 /* Parse the decl-specifier-seq. We have to keep track of whether
13052 or not the decl-specifier-seq declares a named class or
13053 enumeration type, since that is the only case in which the
13054 init-declarator-list is allowed to be empty.
13056 [dcl.dcl]
13058 In a simple-declaration, the optional init-declarator-list can be
13059 omitted only when declaring a class or enumeration, that is when
13060 the decl-specifier-seq contains either a class-specifier, an
13061 elaborated-type-specifier, or an enum-specifier. */
13062 cp_parser_decl_specifier_seq (parser,
13063 CP_PARSER_FLAGS_OPTIONAL,
13064 &decl_specifiers,
13065 &declares_class_or_enum);
13066 /* We no longer need to defer access checks. */
13067 stop_deferring_access_checks ();
13069 /* In a block scope, a valid declaration must always have a
13070 decl-specifier-seq. By not trying to parse declarators, we can
13071 resolve the declaration/expression ambiguity more quickly. */
13072 if (!function_definition_allowed_p
13073 && !decl_specifiers.any_specifiers_p)
13075 cp_parser_error (parser, "expected declaration");
13076 goto done;
13079 /* If the next two tokens are both identifiers, the code is
13080 erroneous. The usual cause of this situation is code like:
13082 T t;
13084 where "T" should name a type -- but does not. */
13085 if (!decl_specifiers.any_type_specifiers_p
13086 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13088 /* If parsing tentatively, we should commit; we really are
13089 looking at a declaration. */
13090 cp_parser_commit_to_tentative_parse (parser);
13091 /* Give up. */
13092 goto done;
13095 cp_parser_maybe_commit_to_declaration (parser,
13096 decl_specifiers.any_specifiers_p);
13098 /* Look for C++17 decomposition declaration. */
13099 for (size_t n = 1; ; n++)
13100 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13101 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13102 continue;
13103 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13104 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13105 && decl_specifiers.any_specifiers_p)
13107 tree decl
13108 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13109 maybe_range_for_decl,
13110 &init_loc);
13112 /* The next token should be either a `,' or a `;'. */
13113 cp_token *token = cp_lexer_peek_token (parser->lexer);
13114 /* If it's a `;', we are done. */
13115 if (token->type == CPP_SEMICOLON)
13116 goto finish;
13117 else if (maybe_range_for_decl)
13119 if (*maybe_range_for_decl == NULL_TREE)
13120 *maybe_range_for_decl = error_mark_node;
13121 goto finish;
13123 /* Anything else is an error. */
13124 else
13126 /* If we have already issued an error message we don't need
13127 to issue another one. */
13128 if ((decl != error_mark_node
13129 && DECL_INITIAL (decl) != error_mark_node)
13130 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13131 cp_parser_error (parser, "expected %<,%> or %<;%>");
13132 /* Skip tokens until we reach the end of the statement. */
13133 cp_parser_skip_to_end_of_statement (parser);
13134 /* If the next token is now a `;', consume it. */
13135 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13136 cp_lexer_consume_token (parser->lexer);
13137 goto done;
13140 else
13141 break;
13143 tree last_type;
13144 bool auto_specifier_p;
13145 /* NULL_TREE if both variable and function declaration are allowed,
13146 error_mark_node if function declaration are not allowed and
13147 a FUNCTION_DECL that should be diagnosed if it is followed by
13148 variable declarations. */
13149 tree auto_function_declaration;
13151 last_type = NULL_TREE;
13152 auto_specifier_p
13153 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13154 auto_function_declaration = NULL_TREE;
13156 /* Keep going until we hit the `;' at the end of the simple
13157 declaration. */
13158 saw_declarator = false;
13159 while (cp_lexer_next_token_is_not (parser->lexer,
13160 CPP_SEMICOLON))
13162 cp_token *token;
13163 bool function_definition_p;
13164 tree decl;
13165 tree auto_result = NULL_TREE;
13167 if (saw_declarator)
13169 /* If we are processing next declarator, comma is expected */
13170 token = cp_lexer_peek_token (parser->lexer);
13171 gcc_assert (token->type == CPP_COMMA);
13172 cp_lexer_consume_token (parser->lexer);
13173 if (maybe_range_for_decl)
13175 *maybe_range_for_decl = error_mark_node;
13176 if (comma_loc == UNKNOWN_LOCATION)
13177 comma_loc = token->location;
13180 else
13181 saw_declarator = true;
13183 /* Parse the init-declarator. */
13184 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13185 /*checks=*/NULL,
13186 function_definition_allowed_p,
13187 /*member_p=*/false,
13188 declares_class_or_enum,
13189 &function_definition_p,
13190 maybe_range_for_decl,
13191 &init_loc,
13192 &auto_result);
13193 /* If an error occurred while parsing tentatively, exit quickly.
13194 (That usually happens when in the body of a function; each
13195 statement is treated as a declaration-statement until proven
13196 otherwise.) */
13197 if (cp_parser_error_occurred (parser))
13198 goto done;
13200 if (auto_specifier_p && cxx_dialect >= cxx14)
13202 /* If the init-declarator-list contains more than one
13203 init-declarator, they shall all form declarations of
13204 variables. */
13205 if (auto_function_declaration == NULL_TREE)
13206 auto_function_declaration
13207 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13208 else if (TREE_CODE (decl) == FUNCTION_DECL
13209 || auto_function_declaration != error_mark_node)
13211 error_at (decl_specifiers.locations[ds_type_spec],
13212 "non-variable %qD in declaration with more than one "
13213 "declarator with placeholder type",
13214 TREE_CODE (decl) == FUNCTION_DECL
13215 ? decl : auto_function_declaration);
13216 auto_function_declaration = error_mark_node;
13220 if (auto_result
13221 && (!processing_template_decl || !type_uses_auto (auto_result)))
13223 if (last_type
13224 && last_type != error_mark_node
13225 && !same_type_p (auto_result, last_type))
13227 /* If the list of declarators contains more than one declarator,
13228 the type of each declared variable is determined as described
13229 above. If the type deduced for the template parameter U is not
13230 the same in each deduction, the program is ill-formed. */
13231 error_at (decl_specifiers.locations[ds_type_spec],
13232 "inconsistent deduction for %qT: %qT and then %qT",
13233 decl_specifiers.type, last_type, auto_result);
13234 last_type = error_mark_node;
13236 else
13237 last_type = auto_result;
13240 /* Handle function definitions specially. */
13241 if (function_definition_p)
13243 /* If the next token is a `,', then we are probably
13244 processing something like:
13246 void f() {}, *p;
13248 which is erroneous. */
13249 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13251 cp_token *token = cp_lexer_peek_token (parser->lexer);
13252 error_at (token->location,
13253 "mixing"
13254 " declarations and function-definitions is forbidden");
13256 /* Otherwise, we're done with the list of declarators. */
13257 else
13259 pop_deferring_access_checks ();
13260 return;
13263 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13264 *maybe_range_for_decl = decl;
13265 /* The next token should be either a `,' or a `;'. */
13266 token = cp_lexer_peek_token (parser->lexer);
13267 /* If it's a `,', there are more declarators to come. */
13268 if (token->type == CPP_COMMA)
13269 /* will be consumed next time around */;
13270 /* If it's a `;', we are done. */
13271 else if (token->type == CPP_SEMICOLON)
13272 break;
13273 else if (maybe_range_for_decl)
13275 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13276 permerror (decl_specifiers.locations[ds_type_spec],
13277 "types may not be defined in a for-range-declaration");
13278 break;
13280 /* Anything else is an error. */
13281 else
13283 /* If we have already issued an error message we don't need
13284 to issue another one. */
13285 if ((decl != error_mark_node
13286 && DECL_INITIAL (decl) != error_mark_node)
13287 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13288 cp_parser_error (parser, "expected %<,%> or %<;%>");
13289 /* Skip tokens until we reach the end of the statement. */
13290 cp_parser_skip_to_end_of_statement (parser);
13291 /* If the next token is now a `;', consume it. */
13292 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13293 cp_lexer_consume_token (parser->lexer);
13294 goto done;
13296 /* After the first time around, a function-definition is not
13297 allowed -- even if it was OK at first. For example:
13299 int i, f() {}
13301 is not valid. */
13302 function_definition_allowed_p = false;
13305 /* Issue an error message if no declarators are present, and the
13306 decl-specifier-seq does not itself declare a class or
13307 enumeration: [dcl.dcl]/3. */
13308 if (!saw_declarator)
13310 if (cp_parser_declares_only_class_p (parser))
13312 if (!declares_class_or_enum
13313 && decl_specifiers.type
13314 && OVERLOAD_TYPE_P (decl_specifiers.type))
13315 /* Ensure an error is issued anyway when finish_decltype_type,
13316 called via cp_parser_decl_specifier_seq, returns a class or
13317 an enumeration (c++/51786). */
13318 decl_specifiers.type = NULL_TREE;
13319 shadow_tag (&decl_specifiers);
13321 /* Perform any deferred access checks. */
13322 perform_deferred_access_checks (tf_warning_or_error);
13325 /* Consume the `;'. */
13326 finish:
13327 if (!maybe_range_for_decl)
13328 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13329 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13331 if (init_loc != UNKNOWN_LOCATION)
13332 error_at (init_loc, "initializer in range-based %<for%> loop");
13333 if (comma_loc != UNKNOWN_LOCATION)
13334 error_at (comma_loc,
13335 "multiple declarations in range-based %<for%> loop");
13338 done:
13339 pop_deferring_access_checks ();
13342 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13343 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13344 initializer ; */
13346 static tree
13347 cp_parser_decomposition_declaration (cp_parser *parser,
13348 cp_decl_specifier_seq *decl_specifiers,
13349 tree *maybe_range_for_decl,
13350 location_t *init_loc)
13352 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13353 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13354 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13356 /* Parse the identifier-list. */
13357 auto_vec<cp_expr, 10> v;
13358 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13359 while (true)
13361 cp_expr e = cp_parser_identifier (parser);
13362 if (e.get_value () == error_mark_node)
13363 break;
13364 v.safe_push (e);
13365 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13366 break;
13367 cp_lexer_consume_token (parser->lexer);
13370 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13371 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13373 end_loc = UNKNOWN_LOCATION;
13374 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13375 false);
13376 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13377 cp_lexer_consume_token (parser->lexer);
13378 else
13380 cp_parser_skip_to_end_of_statement (parser);
13381 return error_mark_node;
13385 if (cxx_dialect < cxx17)
13386 pedwarn (loc, 0, "structured bindings only available with "
13387 "-std=c++17 or -std=gnu++17");
13389 tree pushed_scope;
13390 cp_declarator *declarator = make_declarator (cdk_decomp);
13391 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13392 declarator->id_loc = loc;
13393 if (ref_qual != REF_QUAL_NONE)
13394 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13395 ref_qual == REF_QUAL_RVALUE,
13396 NULL_TREE);
13397 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13398 NULL_TREE, decl_specifiers->attributes,
13399 &pushed_scope);
13400 tree orig_decl = decl;
13402 unsigned int i;
13403 cp_expr e;
13404 cp_decl_specifier_seq decl_specs;
13405 clear_decl_specs (&decl_specs);
13406 decl_specs.type = make_auto ();
13407 tree prev = decl;
13408 FOR_EACH_VEC_ELT (v, i, e)
13410 if (i == 0)
13411 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13412 else
13413 declarator->u.id.unqualified_name = e.get_value ();
13414 declarator->id_loc = e.get_location ();
13415 tree elt_pushed_scope;
13416 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13417 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13418 if (decl2 == error_mark_node)
13419 decl = error_mark_node;
13420 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13422 /* Ensure we've diagnosed redeclaration if we aren't creating
13423 a new VAR_DECL. */
13424 gcc_assert (errorcount);
13425 decl = error_mark_node;
13427 else
13428 prev = decl2;
13429 if (elt_pushed_scope)
13430 pop_scope (elt_pushed_scope);
13433 if (v.is_empty ())
13435 error_at (loc, "empty structured binding declaration");
13436 decl = error_mark_node;
13439 if (maybe_range_for_decl == NULL
13440 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13442 bool non_constant_p = false, is_direct_init = false;
13443 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13444 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13445 &non_constant_p);
13446 if (initializer == NULL_TREE
13447 || (TREE_CODE (initializer) == TREE_LIST
13448 && TREE_CHAIN (initializer))
13449 || (is_direct_init
13450 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13451 && CONSTRUCTOR_NELTS (initializer) != 1))
13453 error_at (loc, "invalid initializer for structured binding "
13454 "declaration");
13455 initializer = error_mark_node;
13458 if (decl != error_mark_node)
13460 cp_maybe_mangle_decomp (decl, prev, v.length ());
13461 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13462 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13463 cp_finish_decomp (decl, prev, v.length ());
13466 else if (decl != error_mark_node)
13468 *maybe_range_for_decl = prev;
13469 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13470 the underlying DECL. */
13471 cp_finish_decomp (decl, prev, v.length ());
13474 if (pushed_scope)
13475 pop_scope (pushed_scope);
13477 if (decl == error_mark_node && DECL_P (orig_decl))
13479 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13480 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13483 return decl;
13486 /* Parse a decl-specifier-seq.
13488 decl-specifier-seq:
13489 decl-specifier-seq [opt] decl-specifier
13490 decl-specifier attribute-specifier-seq [opt] (C++11)
13492 decl-specifier:
13493 storage-class-specifier
13494 type-specifier
13495 function-specifier
13496 friend
13497 typedef
13499 GNU Extension:
13501 decl-specifier:
13502 attributes
13504 Concepts Extension:
13506 decl-specifier:
13507 concept
13509 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13511 The parser flags FLAGS is used to control type-specifier parsing.
13513 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13514 flags:
13516 1: one of the decl-specifiers is an elaborated-type-specifier
13517 (i.e., a type declaration)
13518 2: one of the decl-specifiers is an enum-specifier or a
13519 class-specifier (i.e., a type definition)
13523 static void
13524 cp_parser_decl_specifier_seq (cp_parser* parser,
13525 cp_parser_flags flags,
13526 cp_decl_specifier_seq *decl_specs,
13527 int* declares_class_or_enum)
13529 bool constructor_possible_p = !parser->in_declarator_p;
13530 bool found_decl_spec = false;
13531 cp_token *start_token = NULL;
13532 cp_decl_spec ds;
13534 /* Clear DECL_SPECS. */
13535 clear_decl_specs (decl_specs);
13537 /* Assume no class or enumeration type is declared. */
13538 *declares_class_or_enum = 0;
13540 /* Keep reading specifiers until there are no more to read. */
13541 while (true)
13543 bool constructor_p;
13544 cp_token *token;
13545 ds = ds_last;
13547 /* Peek at the next token. */
13548 token = cp_lexer_peek_token (parser->lexer);
13550 /* Save the first token of the decl spec list for error
13551 reporting. */
13552 if (!start_token)
13553 start_token = token;
13554 /* Handle attributes. */
13555 if (cp_next_tokens_can_be_attribute_p (parser))
13557 /* Parse the attributes. */
13558 tree attrs = cp_parser_attributes_opt (parser);
13560 /* In a sequence of declaration specifiers, c++11 attributes
13561 appertain to the type that precede them. In that case
13562 [dcl.spec]/1 says:
13564 The attribute-specifier-seq affects the type only for
13565 the declaration it appears in, not other declarations
13566 involving the same type.
13568 But for now let's force the user to position the
13569 attribute either at the beginning of the declaration or
13570 after the declarator-id, which would clearly mean that it
13571 applies to the declarator. */
13572 if (cxx11_attribute_p (attrs))
13574 if (!found_decl_spec)
13575 /* The c++11 attribute is at the beginning of the
13576 declaration. It appertains to the entity being
13577 declared. */;
13578 else
13580 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13582 /* This is an attribute following a
13583 class-specifier. */
13584 if (decl_specs->type_definition_p)
13585 warn_misplaced_attr_for_class_type (token->location,
13586 decl_specs->type);
13587 attrs = NULL_TREE;
13589 else
13591 decl_specs->std_attributes
13592 = attr_chainon (decl_specs->std_attributes, attrs);
13593 if (decl_specs->locations[ds_std_attribute] == 0)
13594 decl_specs->locations[ds_std_attribute] = token->location;
13596 continue;
13600 decl_specs->attributes
13601 = attr_chainon (decl_specs->attributes, attrs);
13602 if (decl_specs->locations[ds_attribute] == 0)
13603 decl_specs->locations[ds_attribute] = token->location;
13604 continue;
13606 /* Assume we will find a decl-specifier keyword. */
13607 found_decl_spec = true;
13608 /* If the next token is an appropriate keyword, we can simply
13609 add it to the list. */
13610 switch (token->keyword)
13612 /* decl-specifier:
13613 friend
13614 constexpr */
13615 case RID_FRIEND:
13616 if (!at_class_scope_p ())
13618 gcc_rich_location richloc (token->location);
13619 richloc.add_fixit_remove ();
13620 error_at (&richloc, "%<friend%> used outside of class");
13621 cp_lexer_purge_token (parser->lexer);
13623 else
13625 ds = ds_friend;
13626 /* Consume the token. */
13627 cp_lexer_consume_token (parser->lexer);
13629 break;
13631 case RID_CONSTEXPR:
13632 ds = ds_constexpr;
13633 cp_lexer_consume_token (parser->lexer);
13634 break;
13636 case RID_CONCEPT:
13637 ds = ds_concept;
13638 cp_lexer_consume_token (parser->lexer);
13639 break;
13641 /* function-specifier:
13642 inline
13643 virtual
13644 explicit */
13645 case RID_INLINE:
13646 case RID_VIRTUAL:
13647 case RID_EXPLICIT:
13648 cp_parser_function_specifier_opt (parser, decl_specs);
13649 break;
13651 /* decl-specifier:
13652 typedef */
13653 case RID_TYPEDEF:
13654 ds = ds_typedef;
13655 /* Consume the token. */
13656 cp_lexer_consume_token (parser->lexer);
13657 /* A constructor declarator cannot appear in a typedef. */
13658 constructor_possible_p = false;
13659 /* The "typedef" keyword can only occur in a declaration; we
13660 may as well commit at this point. */
13661 cp_parser_commit_to_tentative_parse (parser);
13663 if (decl_specs->storage_class != sc_none)
13664 decl_specs->conflicting_specifiers_p = true;
13665 break;
13667 /* storage-class-specifier:
13668 auto
13669 register
13670 static
13671 extern
13672 mutable
13674 GNU Extension:
13675 thread */
13676 case RID_AUTO:
13677 if (cxx_dialect == cxx98)
13679 /* Consume the token. */
13680 cp_lexer_consume_token (parser->lexer);
13682 /* Complain about `auto' as a storage specifier, if
13683 we're complaining about C++0x compatibility. */
13684 gcc_rich_location richloc (token->location);
13685 richloc.add_fixit_remove ();
13686 warning_at (&richloc, OPT_Wc__11_compat,
13687 "%<auto%> changes meaning in C++11; "
13688 "please remove it");
13690 /* Set the storage class anyway. */
13691 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13692 token);
13694 else
13695 /* C++0x auto type-specifier. */
13696 found_decl_spec = false;
13697 break;
13699 case RID_REGISTER:
13700 case RID_STATIC:
13701 case RID_EXTERN:
13702 case RID_MUTABLE:
13703 /* Consume the token. */
13704 cp_lexer_consume_token (parser->lexer);
13705 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13706 token);
13707 break;
13708 case RID_THREAD:
13709 /* Consume the token. */
13710 ds = ds_thread;
13711 cp_lexer_consume_token (parser->lexer);
13712 break;
13714 default:
13715 /* We did not yet find a decl-specifier yet. */
13716 found_decl_spec = false;
13717 break;
13720 if (found_decl_spec
13721 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13722 && token->keyword != RID_CONSTEXPR)
13723 error ("decl-specifier invalid in condition");
13725 if (found_decl_spec
13726 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13727 && token->keyword != RID_MUTABLE
13728 && token->keyword != RID_CONSTEXPR)
13729 error_at (token->location, "%qD invalid in lambda",
13730 ridpointers[token->keyword]);
13732 if (ds != ds_last)
13733 set_and_check_decl_spec_loc (decl_specs, ds, token);
13735 /* Constructors are a special case. The `S' in `S()' is not a
13736 decl-specifier; it is the beginning of the declarator. */
13737 constructor_p
13738 = (!found_decl_spec
13739 && constructor_possible_p
13740 && (cp_parser_constructor_declarator_p
13741 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13743 /* If we don't have a DECL_SPEC yet, then we must be looking at
13744 a type-specifier. */
13745 if (!found_decl_spec && !constructor_p)
13747 int decl_spec_declares_class_or_enum;
13748 bool is_cv_qualifier;
13749 tree type_spec;
13751 type_spec
13752 = cp_parser_type_specifier (parser, flags,
13753 decl_specs,
13754 /*is_declaration=*/true,
13755 &decl_spec_declares_class_or_enum,
13756 &is_cv_qualifier);
13757 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13759 /* If this type-specifier referenced a user-defined type
13760 (a typedef, class-name, etc.), then we can't allow any
13761 more such type-specifiers henceforth.
13763 [dcl.spec]
13765 The longest sequence of decl-specifiers that could
13766 possibly be a type name is taken as the
13767 decl-specifier-seq of a declaration. The sequence shall
13768 be self-consistent as described below.
13770 [dcl.type]
13772 As a general rule, at most one type-specifier is allowed
13773 in the complete decl-specifier-seq of a declaration. The
13774 only exceptions are the following:
13776 -- const or volatile can be combined with any other
13777 type-specifier.
13779 -- signed or unsigned can be combined with char, long,
13780 short, or int.
13782 -- ..
13784 Example:
13786 typedef char* Pc;
13787 void g (const int Pc);
13789 Here, Pc is *not* part of the decl-specifier seq; it's
13790 the declarator. Therefore, once we see a type-specifier
13791 (other than a cv-qualifier), we forbid any additional
13792 user-defined types. We *do* still allow things like `int
13793 int' to be considered a decl-specifier-seq, and issue the
13794 error message later. */
13795 if (type_spec && !is_cv_qualifier)
13796 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13797 /* A constructor declarator cannot follow a type-specifier. */
13798 if (type_spec)
13800 constructor_possible_p = false;
13801 found_decl_spec = true;
13802 if (!is_cv_qualifier)
13803 decl_specs->any_type_specifiers_p = true;
13805 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13806 error_at (token->location, "type-specifier invalid in lambda");
13810 /* If we still do not have a DECL_SPEC, then there are no more
13811 decl-specifiers. */
13812 if (!found_decl_spec)
13813 break;
13815 decl_specs->any_specifiers_p = true;
13816 /* After we see one decl-specifier, further decl-specifiers are
13817 always optional. */
13818 flags |= CP_PARSER_FLAGS_OPTIONAL;
13821 /* Don't allow a friend specifier with a class definition. */
13822 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13823 && (*declares_class_or_enum & 2))
13824 error_at (decl_specs->locations[ds_friend],
13825 "class definition may not be declared a friend");
13828 /* Parse an (optional) storage-class-specifier.
13830 storage-class-specifier:
13831 auto
13832 register
13833 static
13834 extern
13835 mutable
13837 GNU Extension:
13839 storage-class-specifier:
13840 thread
13842 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13844 static tree
13845 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13847 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13849 case RID_AUTO:
13850 if (cxx_dialect != cxx98)
13851 return NULL_TREE;
13852 /* Fall through for C++98. */
13853 gcc_fallthrough ();
13855 case RID_REGISTER:
13856 case RID_STATIC:
13857 case RID_EXTERN:
13858 case RID_MUTABLE:
13859 case RID_THREAD:
13860 /* Consume the token. */
13861 return cp_lexer_consume_token (parser->lexer)->u.value;
13863 default:
13864 return NULL_TREE;
13868 /* Parse an (optional) function-specifier.
13870 function-specifier:
13871 inline
13872 virtual
13873 explicit
13875 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13876 Updates DECL_SPECS, if it is non-NULL. */
13878 static tree
13879 cp_parser_function_specifier_opt (cp_parser* parser,
13880 cp_decl_specifier_seq *decl_specs)
13882 cp_token *token = cp_lexer_peek_token (parser->lexer);
13883 switch (token->keyword)
13885 case RID_INLINE:
13886 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13887 break;
13889 case RID_VIRTUAL:
13890 /* 14.5.2.3 [temp.mem]
13892 A member function template shall not be virtual. */
13893 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13894 && current_class_type)
13895 error_at (token->location, "templates may not be %<virtual%>");
13896 else
13897 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13898 break;
13900 case RID_EXPLICIT:
13901 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13902 break;
13904 default:
13905 return NULL_TREE;
13908 /* Consume the token. */
13909 return cp_lexer_consume_token (parser->lexer)->u.value;
13912 /* Parse a linkage-specification.
13914 linkage-specification:
13915 extern string-literal { declaration-seq [opt] }
13916 extern string-literal declaration */
13918 static void
13919 cp_parser_linkage_specification (cp_parser* parser)
13921 tree linkage;
13923 /* Look for the `extern' keyword. */
13924 cp_token *extern_token
13925 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13927 /* Look for the string-literal. */
13928 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13929 linkage = cp_parser_string_literal (parser, false, false);
13931 /* Transform the literal into an identifier. If the literal is a
13932 wide-character string, or contains embedded NULs, then we can't
13933 handle it as the user wants. */
13934 if (strlen (TREE_STRING_POINTER (linkage))
13935 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13937 cp_parser_error (parser, "invalid linkage-specification");
13938 /* Assume C++ linkage. */
13939 linkage = lang_name_cplusplus;
13941 else
13942 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13944 /* We're now using the new linkage. */
13945 push_lang_context (linkage);
13947 /* Preserve the location of the the innermost linkage specification,
13948 tracking the locations of nested specifications via a local. */
13949 location_t saved_location
13950 = parser->innermost_linkage_specification_location;
13951 /* Construct a location ranging from the start of the "extern" to
13952 the end of the string-literal, with the caret at the start, e.g.:
13953 extern "C" {
13954 ^~~~~~~~~~
13956 parser->innermost_linkage_specification_location
13957 = make_location (extern_token->location,
13958 extern_token->location,
13959 get_finish (string_token->location));
13961 /* If the next token is a `{', then we're using the first
13962 production. */
13963 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13965 cp_ensure_no_omp_declare_simd (parser);
13966 cp_ensure_no_oacc_routine (parser);
13968 /* Consume the `{' token. */
13969 matching_braces braces;
13970 braces.consume_open (parser)->location;
13971 /* Parse the declarations. */
13972 cp_parser_declaration_seq_opt (parser);
13973 /* Look for the closing `}'. */
13974 braces.require_close (parser);
13976 /* Otherwise, there's just one declaration. */
13977 else
13979 bool saved_in_unbraced_linkage_specification_p;
13981 saved_in_unbraced_linkage_specification_p
13982 = parser->in_unbraced_linkage_specification_p;
13983 parser->in_unbraced_linkage_specification_p = true;
13984 cp_parser_declaration (parser);
13985 parser->in_unbraced_linkage_specification_p
13986 = saved_in_unbraced_linkage_specification_p;
13989 /* We're done with the linkage-specification. */
13990 pop_lang_context ();
13992 /* Restore location of parent linkage specification, if any. */
13993 parser->innermost_linkage_specification_location = saved_location;
13996 /* Parse a static_assert-declaration.
13998 static_assert-declaration:
13999 static_assert ( constant-expression , string-literal ) ;
14000 static_assert ( constant-expression ) ; (C++17)
14002 If MEMBER_P, this static_assert is a class member. */
14004 static void
14005 cp_parser_static_assert(cp_parser *parser, bool member_p)
14007 cp_expr condition;
14008 location_t token_loc;
14009 tree message;
14010 bool dummy;
14012 /* Peek at the `static_assert' token so we can keep track of exactly
14013 where the static assertion started. */
14014 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14016 /* Look for the `static_assert' keyword. */
14017 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14018 RT_STATIC_ASSERT))
14019 return;
14021 /* We know we are in a static assertion; commit to any tentative
14022 parse. */
14023 if (cp_parser_parsing_tentatively (parser))
14024 cp_parser_commit_to_tentative_parse (parser);
14026 /* Parse the `(' starting the static assertion condition. */
14027 matching_parens parens;
14028 parens.require_open (parser);
14030 /* Parse the constant-expression. Allow a non-constant expression
14031 here in order to give better diagnostics in finish_static_assert. */
14032 condition =
14033 cp_parser_constant_expression (parser,
14034 /*allow_non_constant_p=*/true,
14035 /*non_constant_p=*/&dummy);
14037 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14039 if (cxx_dialect < cxx17)
14040 pedwarn (input_location, OPT_Wpedantic,
14041 "static_assert without a message "
14042 "only available with -std=c++17 or -std=gnu++17");
14043 /* Eat the ')' */
14044 cp_lexer_consume_token (parser->lexer);
14045 message = build_string (1, "");
14046 TREE_TYPE (message) = char_array_type_node;
14047 fix_string_type (message);
14049 else
14051 /* Parse the separating `,'. */
14052 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14054 /* Parse the string-literal message. */
14055 message = cp_parser_string_literal (parser,
14056 /*translate=*/false,
14057 /*wide_ok=*/true);
14059 /* A `)' completes the static assertion. */
14060 if (!parens.require_close (parser))
14061 cp_parser_skip_to_closing_parenthesis (parser,
14062 /*recovering=*/true,
14063 /*or_comma=*/false,
14064 /*consume_paren=*/true);
14067 /* A semicolon terminates the declaration. */
14068 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14070 /* Get the location for the static assertion. Use that of the
14071 condition if available, otherwise, use that of the "static_assert"
14072 token. */
14073 location_t assert_loc = condition.get_location ();
14074 if (assert_loc == UNKNOWN_LOCATION)
14075 assert_loc = token_loc;
14077 /* Complete the static assertion, which may mean either processing
14078 the static assert now or saving it for template instantiation. */
14079 finish_static_assert (condition, message, assert_loc, member_p);
14082 /* Parse the expression in decltype ( expression ). */
14084 static tree
14085 cp_parser_decltype_expr (cp_parser *parser,
14086 bool &id_expression_or_member_access_p)
14088 cp_token *id_expr_start_token;
14089 tree expr;
14091 /* Since we're going to preserve any side-effects from this parse, set up a
14092 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14093 in the expression. */
14094 tentative_firewall firewall (parser);
14096 /* First, try parsing an id-expression. */
14097 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14098 cp_parser_parse_tentatively (parser);
14099 expr = cp_parser_id_expression (parser,
14100 /*template_keyword_p=*/false,
14101 /*check_dependency_p=*/true,
14102 /*template_p=*/NULL,
14103 /*declarator_p=*/false,
14104 /*optional_p=*/false);
14106 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14108 bool non_integral_constant_expression_p = false;
14109 tree id_expression = expr;
14110 cp_id_kind idk;
14111 const char *error_msg;
14113 if (identifier_p (expr))
14114 /* Lookup the name we got back from the id-expression. */
14115 expr = cp_parser_lookup_name_simple (parser, expr,
14116 id_expr_start_token->location);
14118 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14119 /* A template without args is not a complete id-expression. */
14120 expr = error_mark_node;
14122 if (expr
14123 && expr != error_mark_node
14124 && TREE_CODE (expr) != TYPE_DECL
14125 && (TREE_CODE (expr) != BIT_NOT_EXPR
14126 || !TYPE_P (TREE_OPERAND (expr, 0)))
14127 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14129 /* Complete lookup of the id-expression. */
14130 expr = (finish_id_expression
14131 (id_expression, expr, parser->scope, &idk,
14132 /*integral_constant_expression_p=*/false,
14133 /*allow_non_integral_constant_expression_p=*/true,
14134 &non_integral_constant_expression_p,
14135 /*template_p=*/false,
14136 /*done=*/true,
14137 /*address_p=*/false,
14138 /*template_arg_p=*/false,
14139 &error_msg,
14140 id_expr_start_token->location));
14142 if (expr == error_mark_node)
14143 /* We found an id-expression, but it was something that we
14144 should not have found. This is an error, not something
14145 we can recover from, so note that we found an
14146 id-expression and we'll recover as gracefully as
14147 possible. */
14148 id_expression_or_member_access_p = true;
14151 if (expr
14152 && expr != error_mark_node
14153 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14154 /* We have an id-expression. */
14155 id_expression_or_member_access_p = true;
14158 if (!id_expression_or_member_access_p)
14160 /* Abort the id-expression parse. */
14161 cp_parser_abort_tentative_parse (parser);
14163 /* Parsing tentatively, again. */
14164 cp_parser_parse_tentatively (parser);
14166 /* Parse a class member access. */
14167 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14168 /*cast_p=*/false, /*decltype*/true,
14169 /*member_access_only_p=*/true, NULL);
14171 if (expr
14172 && expr != error_mark_node
14173 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14174 /* We have an id-expression. */
14175 id_expression_or_member_access_p = true;
14178 if (id_expression_or_member_access_p)
14179 /* We have parsed the complete id-expression or member access. */
14180 cp_parser_parse_definitely (parser);
14181 else
14183 /* Abort our attempt to parse an id-expression or member access
14184 expression. */
14185 cp_parser_abort_tentative_parse (parser);
14187 /* Commit to the tentative_firewall so we get syntax errors. */
14188 cp_parser_commit_to_tentative_parse (parser);
14190 /* Parse a full expression. */
14191 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14192 /*decltype_p=*/true);
14195 return expr;
14198 /* Parse a `decltype' type. Returns the type.
14200 simple-type-specifier:
14201 decltype ( expression )
14202 C++14 proposal:
14203 decltype ( auto ) */
14205 static tree
14206 cp_parser_decltype (cp_parser *parser)
14208 bool id_expression_or_member_access_p = false;
14209 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14211 if (start_token->type == CPP_DECLTYPE)
14213 /* Already parsed. */
14214 cp_lexer_consume_token (parser->lexer);
14215 return saved_checks_value (start_token->u.tree_check_value);
14218 /* Look for the `decltype' token. */
14219 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14220 return error_mark_node;
14222 /* Parse the opening `('. */
14223 matching_parens parens;
14224 if (!parens.require_open (parser))
14225 return error_mark_node;
14227 push_deferring_access_checks (dk_deferred);
14229 tree expr = NULL_TREE;
14231 if (cxx_dialect >= cxx14
14232 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14233 /* decltype (auto) */
14234 cp_lexer_consume_token (parser->lexer);
14235 else
14237 /* decltype (expression) */
14239 /* Types cannot be defined in a `decltype' expression. Save away the
14240 old message and set the new one. */
14241 const char *saved_message = parser->type_definition_forbidden_message;
14242 parser->type_definition_forbidden_message
14243 = G_("types may not be defined in %<decltype%> expressions");
14245 /* The restrictions on constant-expressions do not apply inside
14246 decltype expressions. */
14247 bool saved_integral_constant_expression_p
14248 = parser->integral_constant_expression_p;
14249 bool saved_non_integral_constant_expression_p
14250 = parser->non_integral_constant_expression_p;
14251 parser->integral_constant_expression_p = false;
14253 /* Within a parenthesized expression, a `>' token is always
14254 the greater-than operator. */
14255 bool saved_greater_than_is_operator_p
14256 = parser->greater_than_is_operator_p;
14257 parser->greater_than_is_operator_p = true;
14259 /* Do not actually evaluate the expression. */
14260 ++cp_unevaluated_operand;
14262 /* Do not warn about problems with the expression. */
14263 ++c_inhibit_evaluation_warnings;
14265 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14267 /* Go back to evaluating expressions. */
14268 --cp_unevaluated_operand;
14269 --c_inhibit_evaluation_warnings;
14271 /* The `>' token might be the end of a template-id or
14272 template-parameter-list now. */
14273 parser->greater_than_is_operator_p
14274 = saved_greater_than_is_operator_p;
14276 /* Restore the old message and the integral constant expression
14277 flags. */
14278 parser->type_definition_forbidden_message = saved_message;
14279 parser->integral_constant_expression_p
14280 = saved_integral_constant_expression_p;
14281 parser->non_integral_constant_expression_p
14282 = saved_non_integral_constant_expression_p;
14285 /* Parse to the closing `)'. */
14286 if (!parens.require_close (parser))
14288 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14289 /*consume_paren=*/true);
14290 pop_deferring_access_checks ();
14291 return error_mark_node;
14294 if (!expr)
14296 /* Build auto. */
14297 expr = make_decltype_auto ();
14298 AUTO_IS_DECLTYPE (expr) = true;
14300 else
14301 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14302 tf_warning_or_error);
14304 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14305 it again. */
14306 start_token->type = CPP_DECLTYPE;
14307 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14308 start_token->u.tree_check_value->value = expr;
14309 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14310 start_token->keyword = RID_MAX;
14311 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14313 pop_to_parent_deferring_access_checks ();
14315 return expr;
14318 /* Special member functions [gram.special] */
14320 /* Parse a conversion-function-id.
14322 conversion-function-id:
14323 operator conversion-type-id
14325 Returns an IDENTIFIER_NODE representing the operator. */
14327 static tree
14328 cp_parser_conversion_function_id (cp_parser* parser)
14330 tree type;
14331 tree saved_scope;
14332 tree saved_qualifying_scope;
14333 tree saved_object_scope;
14334 tree pushed_scope = NULL_TREE;
14336 /* Look for the `operator' token. */
14337 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14338 return error_mark_node;
14339 /* When we parse the conversion-type-id, the current scope will be
14340 reset. However, we need that information in able to look up the
14341 conversion function later, so we save it here. */
14342 saved_scope = parser->scope;
14343 saved_qualifying_scope = parser->qualifying_scope;
14344 saved_object_scope = parser->object_scope;
14345 /* We must enter the scope of the class so that the names of
14346 entities declared within the class are available in the
14347 conversion-type-id. For example, consider:
14349 struct S {
14350 typedef int I;
14351 operator I();
14354 S::operator I() { ... }
14356 In order to see that `I' is a type-name in the definition, we
14357 must be in the scope of `S'. */
14358 if (saved_scope)
14359 pushed_scope = push_scope (saved_scope);
14360 /* Parse the conversion-type-id. */
14361 type = cp_parser_conversion_type_id (parser);
14362 /* Leave the scope of the class, if any. */
14363 if (pushed_scope)
14364 pop_scope (pushed_scope);
14365 /* Restore the saved scope. */
14366 parser->scope = saved_scope;
14367 parser->qualifying_scope = saved_qualifying_scope;
14368 parser->object_scope = saved_object_scope;
14369 /* If the TYPE is invalid, indicate failure. */
14370 if (type == error_mark_node)
14371 return error_mark_node;
14372 return make_conv_op_name (type);
14375 /* Parse a conversion-type-id:
14377 conversion-type-id:
14378 type-specifier-seq conversion-declarator [opt]
14380 Returns the TYPE specified. */
14382 static tree
14383 cp_parser_conversion_type_id (cp_parser* parser)
14385 tree attributes;
14386 cp_decl_specifier_seq type_specifiers;
14387 cp_declarator *declarator;
14388 tree type_specified;
14389 const char *saved_message;
14391 /* Parse the attributes. */
14392 attributes = cp_parser_attributes_opt (parser);
14394 saved_message = parser->type_definition_forbidden_message;
14395 parser->type_definition_forbidden_message
14396 = G_("types may not be defined in a conversion-type-id");
14398 /* Parse the type-specifiers. */
14399 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14400 /*is_trailing_return=*/false,
14401 &type_specifiers);
14403 parser->type_definition_forbidden_message = saved_message;
14405 /* If that didn't work, stop. */
14406 if (type_specifiers.type == error_mark_node)
14407 return error_mark_node;
14408 /* Parse the conversion-declarator. */
14409 declarator = cp_parser_conversion_declarator_opt (parser);
14411 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14412 /*initialized=*/0, &attributes);
14413 if (attributes)
14414 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14416 /* Don't give this error when parsing tentatively. This happens to
14417 work because we always parse this definitively once. */
14418 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14419 && type_uses_auto (type_specified))
14421 if (cxx_dialect < cxx14)
14423 error ("invalid use of %<auto%> in conversion operator");
14424 return error_mark_node;
14426 else if (template_parm_scope_p ())
14427 warning (0, "use of %<auto%> in member template "
14428 "conversion operator can never be deduced");
14431 return type_specified;
14434 /* Parse an (optional) conversion-declarator.
14436 conversion-declarator:
14437 ptr-operator conversion-declarator [opt]
14441 static cp_declarator *
14442 cp_parser_conversion_declarator_opt (cp_parser* parser)
14444 enum tree_code code;
14445 tree class_type, std_attributes = NULL_TREE;
14446 cp_cv_quals cv_quals;
14448 /* We don't know if there's a ptr-operator next, or not. */
14449 cp_parser_parse_tentatively (parser);
14450 /* Try the ptr-operator. */
14451 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14452 &std_attributes);
14453 /* If it worked, look for more conversion-declarators. */
14454 if (cp_parser_parse_definitely (parser))
14456 cp_declarator *declarator;
14458 /* Parse another optional declarator. */
14459 declarator = cp_parser_conversion_declarator_opt (parser);
14461 declarator = cp_parser_make_indirect_declarator
14462 (code, class_type, cv_quals, declarator, std_attributes);
14464 return declarator;
14467 return NULL;
14470 /* Parse an (optional) ctor-initializer.
14472 ctor-initializer:
14473 : mem-initializer-list */
14475 static void
14476 cp_parser_ctor_initializer_opt (cp_parser* parser)
14478 /* If the next token is not a `:', then there is no
14479 ctor-initializer. */
14480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14482 /* Do default initialization of any bases and members. */
14483 if (DECL_CONSTRUCTOR_P (current_function_decl))
14484 finish_mem_initializers (NULL_TREE);
14485 return;
14488 /* Consume the `:' token. */
14489 cp_lexer_consume_token (parser->lexer);
14490 /* And the mem-initializer-list. */
14491 cp_parser_mem_initializer_list (parser);
14494 /* Parse a mem-initializer-list.
14496 mem-initializer-list:
14497 mem-initializer ... [opt]
14498 mem-initializer ... [opt] , mem-initializer-list */
14500 static void
14501 cp_parser_mem_initializer_list (cp_parser* parser)
14503 tree mem_initializer_list = NULL_TREE;
14504 tree target_ctor = error_mark_node;
14505 cp_token *token = cp_lexer_peek_token (parser->lexer);
14507 /* Let the semantic analysis code know that we are starting the
14508 mem-initializer-list. */
14509 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14510 error_at (token->location,
14511 "only constructors take member initializers");
14513 /* Loop through the list. */
14514 while (true)
14516 tree mem_initializer;
14518 token = cp_lexer_peek_token (parser->lexer);
14519 /* Parse the mem-initializer. */
14520 mem_initializer = cp_parser_mem_initializer (parser);
14521 /* If the next token is a `...', we're expanding member initializers. */
14522 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14523 if (ellipsis
14524 || (mem_initializer != error_mark_node
14525 && check_for_bare_parameter_packs (TREE_PURPOSE
14526 (mem_initializer))))
14528 /* Consume the `...'. */
14529 if (ellipsis)
14530 cp_lexer_consume_token (parser->lexer);
14532 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14533 can be expanded but members cannot. */
14534 if (mem_initializer != error_mark_node
14535 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14537 error_at (token->location,
14538 "cannot expand initializer for member %qD",
14539 TREE_PURPOSE (mem_initializer));
14540 mem_initializer = error_mark_node;
14543 /* Construct the pack expansion type. */
14544 if (mem_initializer != error_mark_node)
14545 mem_initializer = make_pack_expansion (mem_initializer);
14547 if (target_ctor != error_mark_node
14548 && mem_initializer != error_mark_node)
14550 error ("mem-initializer for %qD follows constructor delegation",
14551 TREE_PURPOSE (mem_initializer));
14552 mem_initializer = error_mark_node;
14554 /* Look for a target constructor. */
14555 if (mem_initializer != error_mark_node
14556 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14557 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14559 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14560 if (mem_initializer_list)
14562 error ("constructor delegation follows mem-initializer for %qD",
14563 TREE_PURPOSE (mem_initializer_list));
14564 mem_initializer = error_mark_node;
14566 target_ctor = mem_initializer;
14568 /* Add it to the list, unless it was erroneous. */
14569 if (mem_initializer != error_mark_node)
14571 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14572 mem_initializer_list = mem_initializer;
14574 /* If the next token is not a `,', we're done. */
14575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14576 break;
14577 /* Consume the `,' token. */
14578 cp_lexer_consume_token (parser->lexer);
14581 /* Perform semantic analysis. */
14582 if (DECL_CONSTRUCTOR_P (current_function_decl))
14583 finish_mem_initializers (mem_initializer_list);
14586 /* Parse a mem-initializer.
14588 mem-initializer:
14589 mem-initializer-id ( expression-list [opt] )
14590 mem-initializer-id braced-init-list
14592 GNU extension:
14594 mem-initializer:
14595 ( expression-list [opt] )
14597 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14598 class) or FIELD_DECL (for a non-static data member) to initialize;
14599 the TREE_VALUE is the expression-list. An empty initialization
14600 list is represented by void_list_node. */
14602 static tree
14603 cp_parser_mem_initializer (cp_parser* parser)
14605 tree mem_initializer_id;
14606 tree expression_list;
14607 tree member;
14608 cp_token *token = cp_lexer_peek_token (parser->lexer);
14610 /* Find out what is being initialized. */
14611 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14613 permerror (token->location,
14614 "anachronistic old-style base class initializer");
14615 mem_initializer_id = NULL_TREE;
14617 else
14619 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14620 if (mem_initializer_id == error_mark_node)
14621 return mem_initializer_id;
14623 member = expand_member_init (mem_initializer_id);
14624 if (member && !DECL_P (member))
14625 in_base_initializer = 1;
14627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14629 bool expr_non_constant_p;
14630 cp_lexer_set_source_position (parser->lexer);
14631 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14632 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14633 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14634 expression_list = build_tree_list (NULL_TREE, expression_list);
14636 else
14638 vec<tree, va_gc> *vec;
14639 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14640 /*cast_p=*/false,
14641 /*allow_expansion_p=*/true,
14642 /*non_constant_p=*/NULL);
14643 if (vec == NULL)
14644 return error_mark_node;
14645 expression_list = build_tree_list_vec (vec);
14646 release_tree_vector (vec);
14649 if (expression_list == error_mark_node)
14650 return error_mark_node;
14651 if (!expression_list)
14652 expression_list = void_type_node;
14654 in_base_initializer = 0;
14656 return member ? build_tree_list (member, expression_list) : error_mark_node;
14659 /* Parse a mem-initializer-id.
14661 mem-initializer-id:
14662 :: [opt] nested-name-specifier [opt] class-name
14663 decltype-specifier (C++11)
14664 identifier
14666 Returns a TYPE indicating the class to be initialized for the first
14667 production (and the second in C++11). Returns an IDENTIFIER_NODE
14668 indicating the data member to be initialized for the last production. */
14670 static tree
14671 cp_parser_mem_initializer_id (cp_parser* parser)
14673 bool global_scope_p;
14674 bool nested_name_specifier_p;
14675 bool template_p = false;
14676 tree id;
14678 cp_token *token = cp_lexer_peek_token (parser->lexer);
14680 /* `typename' is not allowed in this context ([temp.res]). */
14681 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14683 error_at (token->location,
14684 "keyword %<typename%> not allowed in this context (a qualified "
14685 "member initializer is implicitly a type)");
14686 cp_lexer_consume_token (parser->lexer);
14688 /* Look for the optional `::' operator. */
14689 global_scope_p
14690 = (cp_parser_global_scope_opt (parser,
14691 /*current_scope_valid_p=*/false)
14692 != NULL_TREE);
14693 /* Look for the optional nested-name-specifier. The simplest way to
14694 implement:
14696 [temp.res]
14698 The keyword `typename' is not permitted in a base-specifier or
14699 mem-initializer; in these contexts a qualified name that
14700 depends on a template-parameter is implicitly assumed to be a
14701 type name.
14703 is to assume that we have seen the `typename' keyword at this
14704 point. */
14705 nested_name_specifier_p
14706 = (cp_parser_nested_name_specifier_opt (parser,
14707 /*typename_keyword_p=*/true,
14708 /*check_dependency_p=*/true,
14709 /*type_p=*/true,
14710 /*is_declaration=*/true)
14711 != NULL_TREE);
14712 if (nested_name_specifier_p)
14713 template_p = cp_parser_optional_template_keyword (parser);
14714 /* If there is a `::' operator or a nested-name-specifier, then we
14715 are definitely looking for a class-name. */
14716 if (global_scope_p || nested_name_specifier_p)
14717 return cp_parser_class_name (parser,
14718 /*typename_keyword_p=*/true,
14719 /*template_keyword_p=*/template_p,
14720 typename_type,
14721 /*check_dependency_p=*/true,
14722 /*class_head_p=*/false,
14723 /*is_declaration=*/true);
14724 /* Otherwise, we could also be looking for an ordinary identifier. */
14725 cp_parser_parse_tentatively (parser);
14726 if (cp_lexer_next_token_is_decltype (parser->lexer))
14727 /* Try a decltype-specifier. */
14728 id = cp_parser_decltype (parser);
14729 else
14730 /* Otherwise, try a class-name. */
14731 id = cp_parser_class_name (parser,
14732 /*typename_keyword_p=*/true,
14733 /*template_keyword_p=*/false,
14734 none_type,
14735 /*check_dependency_p=*/true,
14736 /*class_head_p=*/false,
14737 /*is_declaration=*/true);
14738 /* If we found one, we're done. */
14739 if (cp_parser_parse_definitely (parser))
14740 return id;
14741 /* Otherwise, look for an ordinary identifier. */
14742 return cp_parser_identifier (parser);
14745 /* Overloading [gram.over] */
14747 /* Parse an operator-function-id.
14749 operator-function-id:
14750 operator operator
14752 Returns an IDENTIFIER_NODE for the operator which is a
14753 human-readable spelling of the identifier, e.g., `operator +'. */
14755 static cp_expr
14756 cp_parser_operator_function_id (cp_parser* parser)
14758 /* Look for the `operator' keyword. */
14759 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14760 return error_mark_node;
14761 /* And then the name of the operator itself. */
14762 return cp_parser_operator (parser);
14765 /* Return an identifier node for a user-defined literal operator.
14766 The suffix identifier is chained to the operator name identifier. */
14768 tree
14769 cp_literal_operator_id (const char* name)
14771 tree identifier;
14772 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14773 + strlen (name) + 10);
14774 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14775 identifier = get_identifier (buffer);
14777 return identifier;
14780 /* Parse an operator.
14782 operator:
14783 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14784 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14785 || ++ -- , ->* -> () []
14787 GNU Extensions:
14789 operator:
14790 <? >? <?= >?=
14792 Returns an IDENTIFIER_NODE for the operator which is a
14793 human-readable spelling of the identifier, e.g., `operator +'. */
14795 static cp_expr
14796 cp_parser_operator (cp_parser* parser)
14798 tree id = NULL_TREE;
14799 cp_token *token;
14800 bool utf8 = false;
14802 /* Peek at the next token. */
14803 token = cp_lexer_peek_token (parser->lexer);
14805 location_t start_loc = token->location;
14807 /* Figure out which operator we have. */
14808 enum tree_code op = ERROR_MARK;
14809 bool assop = false;
14810 bool consumed = false;
14811 switch (token->type)
14813 case CPP_KEYWORD:
14815 /* The keyword should be either `new' or `delete'. */
14816 if (token->keyword == RID_NEW)
14817 op = NEW_EXPR;
14818 else if (token->keyword == RID_DELETE)
14819 op = DELETE_EXPR;
14820 else
14821 break;
14823 /* Consume the `new' or `delete' token. */
14824 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14826 /* Peek at the next token. */
14827 token = cp_lexer_peek_token (parser->lexer);
14828 /* If it's a `[' token then this is the array variant of the
14829 operator. */
14830 if (token->type == CPP_OPEN_SQUARE)
14832 /* Consume the `[' token. */
14833 cp_lexer_consume_token (parser->lexer);
14834 /* Look for the `]' token. */
14835 if (cp_token *close_token
14836 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14837 end_loc = close_token->location;
14838 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14840 start_loc = make_location (start_loc, start_loc, end_loc);
14841 consumed = true;
14842 break;
14845 case CPP_PLUS:
14846 op = PLUS_EXPR;
14847 break;
14849 case CPP_MINUS:
14850 op = MINUS_EXPR;
14851 break;
14853 case CPP_MULT:
14854 op = MULT_EXPR;
14855 break;
14857 case CPP_DIV:
14858 op = TRUNC_DIV_EXPR;
14859 break;
14861 case CPP_MOD:
14862 op = TRUNC_MOD_EXPR;
14863 break;
14865 case CPP_XOR:
14866 op = BIT_XOR_EXPR;
14867 break;
14869 case CPP_AND:
14870 op = BIT_AND_EXPR;
14871 break;
14873 case CPP_OR:
14874 op = BIT_IOR_EXPR;
14875 break;
14877 case CPP_COMPL:
14878 op = BIT_NOT_EXPR;
14879 break;
14881 case CPP_NOT:
14882 op = TRUTH_NOT_EXPR;
14883 break;
14885 case CPP_EQ:
14886 assop = true;
14887 op = NOP_EXPR;
14888 break;
14890 case CPP_LESS:
14891 op = LT_EXPR;
14892 break;
14894 case CPP_GREATER:
14895 op = GT_EXPR;
14896 break;
14898 case CPP_PLUS_EQ:
14899 assop = true;
14900 op = PLUS_EXPR;
14901 break;
14903 case CPP_MINUS_EQ:
14904 assop = true;
14905 op = MINUS_EXPR;
14906 break;
14908 case CPP_MULT_EQ:
14909 assop = true;
14910 op = MULT_EXPR;
14911 break;
14913 case CPP_DIV_EQ:
14914 assop = true;
14915 op = TRUNC_DIV_EXPR;
14916 break;
14918 case CPP_MOD_EQ:
14919 assop = true;
14920 op = TRUNC_MOD_EXPR;
14921 break;
14923 case CPP_XOR_EQ:
14924 assop = true;
14925 op = BIT_XOR_EXPR;
14926 break;
14928 case CPP_AND_EQ:
14929 assop = true;
14930 op = BIT_AND_EXPR;
14931 break;
14933 case CPP_OR_EQ:
14934 assop = true;
14935 op = BIT_IOR_EXPR;
14936 break;
14938 case CPP_LSHIFT:
14939 op = LSHIFT_EXPR;
14940 break;
14942 case CPP_RSHIFT:
14943 op = RSHIFT_EXPR;
14944 break;
14946 case CPP_LSHIFT_EQ:
14947 assop = true;
14948 op = LSHIFT_EXPR;
14949 break;
14951 case CPP_RSHIFT_EQ:
14952 assop = true;
14953 op = RSHIFT_EXPR;
14954 break;
14956 case CPP_EQ_EQ:
14957 op = EQ_EXPR;
14958 break;
14960 case CPP_NOT_EQ:
14961 op = NE_EXPR;
14962 break;
14964 case CPP_LESS_EQ:
14965 op = LE_EXPR;
14966 break;
14968 case CPP_GREATER_EQ:
14969 op = GE_EXPR;
14970 break;
14972 case CPP_AND_AND:
14973 op = TRUTH_ANDIF_EXPR;
14974 break;
14976 case CPP_OR_OR:
14977 op = TRUTH_ORIF_EXPR;
14978 break;
14980 case CPP_PLUS_PLUS:
14981 op = POSTINCREMENT_EXPR;
14982 break;
14984 case CPP_MINUS_MINUS:
14985 op = PREDECREMENT_EXPR;
14986 break;
14988 case CPP_COMMA:
14989 op = COMPOUND_EXPR;
14990 break;
14992 case CPP_DEREF_STAR:
14993 op = MEMBER_REF;
14994 break;
14996 case CPP_DEREF:
14997 op = COMPONENT_REF;
14998 break;
15000 case CPP_OPEN_PAREN:
15002 /* Consume the `('. */
15003 matching_parens parens;
15004 parens.consume_open (parser);
15005 /* Look for the matching `)'. */
15006 parens.require_close (parser);
15007 op = CALL_EXPR;
15008 consumed = true;
15009 break;
15012 case CPP_OPEN_SQUARE:
15013 /* Consume the `['. */
15014 cp_lexer_consume_token (parser->lexer);
15015 /* Look for the matching `]'. */
15016 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15017 op = ARRAY_REF;
15018 consumed = true;
15019 break;
15021 case CPP_UTF8STRING:
15022 case CPP_UTF8STRING_USERDEF:
15023 utf8 = true;
15024 /* FALLTHRU */
15025 case CPP_STRING:
15026 case CPP_WSTRING:
15027 case CPP_STRING16:
15028 case CPP_STRING32:
15029 case CPP_STRING_USERDEF:
15030 case CPP_WSTRING_USERDEF:
15031 case CPP_STRING16_USERDEF:
15032 case CPP_STRING32_USERDEF:
15034 tree str, string_tree;
15035 int sz, len;
15037 if (cxx_dialect == cxx98)
15038 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15040 /* Consume the string. */
15041 str = cp_parser_string_literal (parser, /*translate=*/true,
15042 /*wide_ok=*/true, /*lookup_udlit=*/false);
15043 if (str == error_mark_node)
15044 return error_mark_node;
15045 else if (TREE_CODE (str) == USERDEF_LITERAL)
15047 string_tree = USERDEF_LITERAL_VALUE (str);
15048 id = USERDEF_LITERAL_SUFFIX_ID (str);
15050 else
15052 string_tree = str;
15053 /* Look for the suffix identifier. */
15054 token = cp_lexer_peek_token (parser->lexer);
15055 if (token->type == CPP_NAME)
15056 id = cp_parser_identifier (parser);
15057 else if (token->type == CPP_KEYWORD)
15059 error ("unexpected keyword;"
15060 " remove space between quotes and suffix identifier");
15061 return error_mark_node;
15063 else
15065 error ("expected suffix identifier");
15066 return error_mark_node;
15069 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15070 (TREE_TYPE (TREE_TYPE (string_tree))));
15071 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15072 if (len != 0)
15074 error ("expected empty string after %<operator%> keyword");
15075 return error_mark_node;
15077 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15078 != char_type_node)
15080 error ("invalid encoding prefix in literal operator");
15081 return error_mark_node;
15083 if (id != error_mark_node)
15085 const char *name = IDENTIFIER_POINTER (id);
15086 id = cp_literal_operator_id (name);
15088 return id;
15091 default:
15092 /* Anything else is an error. */
15093 break;
15096 /* If we have selected an identifier, we need to consume the
15097 operator token. */
15098 if (op != ERROR_MARK)
15100 id = ovl_op_identifier (assop, op);
15101 if (!consumed)
15102 cp_lexer_consume_token (parser->lexer);
15104 /* Otherwise, no valid operator name was present. */
15105 else
15107 cp_parser_error (parser, "expected operator");
15108 id = error_mark_node;
15111 return cp_expr (id, start_loc);
15114 /* Parse a template-declaration.
15116 template-declaration:
15117 export [opt] template < template-parameter-list > declaration
15119 If MEMBER_P is TRUE, this template-declaration occurs within a
15120 class-specifier.
15122 The grammar rule given by the standard isn't correct. What
15123 is really meant is:
15125 template-declaration:
15126 export [opt] template-parameter-list-seq
15127 decl-specifier-seq [opt] init-declarator [opt] ;
15128 export [opt] template-parameter-list-seq
15129 function-definition
15131 template-parameter-list-seq:
15132 template-parameter-list-seq [opt]
15133 template < template-parameter-list >
15135 Concept Extensions:
15137 template-parameter-list-seq:
15138 template < template-parameter-list > requires-clause [opt]
15140 requires-clause:
15141 requires logical-or-expression */
15143 static void
15144 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15146 /* Check for `export'. */
15147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15149 /* Consume the `export' token. */
15150 cp_lexer_consume_token (parser->lexer);
15151 /* Warn that we do not support `export'. */
15152 warning (0, "keyword %<export%> not implemented, and will be ignored");
15155 cp_parser_template_declaration_after_export (parser, member_p);
15158 /* Parse a template-parameter-list.
15160 template-parameter-list:
15161 template-parameter
15162 template-parameter-list , template-parameter
15164 Returns a TREE_LIST. Each node represents a template parameter.
15165 The nodes are connected via their TREE_CHAINs. */
15167 static tree
15168 cp_parser_template_parameter_list (cp_parser* parser)
15170 tree parameter_list = NULL_TREE;
15172 begin_template_parm_list ();
15174 /* The loop below parses the template parms. We first need to know
15175 the total number of template parms to be able to compute proper
15176 canonical types of each dependent type. So after the loop, when
15177 we know the total number of template parms,
15178 end_template_parm_list computes the proper canonical types and
15179 fixes up the dependent types accordingly. */
15180 while (true)
15182 tree parameter;
15183 bool is_non_type;
15184 bool is_parameter_pack;
15185 location_t parm_loc;
15187 /* Parse the template-parameter. */
15188 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15189 parameter = cp_parser_template_parameter (parser,
15190 &is_non_type,
15191 &is_parameter_pack);
15192 /* Add it to the list. */
15193 if (parameter != error_mark_node)
15194 parameter_list = process_template_parm (parameter_list,
15195 parm_loc,
15196 parameter,
15197 is_non_type,
15198 is_parameter_pack);
15199 else
15201 tree err_parm = build_tree_list (parameter, parameter);
15202 parameter_list = chainon (parameter_list, err_parm);
15205 /* If the next token is not a `,', we're done. */
15206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15207 break;
15208 /* Otherwise, consume the `,' token. */
15209 cp_lexer_consume_token (parser->lexer);
15212 return end_template_parm_list (parameter_list);
15215 /* Parse a introduction-list.
15217 introduction-list:
15218 introduced-parameter
15219 introduction-list , introduced-parameter
15221 introduced-parameter:
15222 ...[opt] identifier
15224 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15225 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15226 WILDCARD_DECL will also have DECL_NAME set and token location in
15227 DECL_SOURCE_LOCATION. */
15229 static tree
15230 cp_parser_introduction_list (cp_parser *parser)
15232 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15234 while (true)
15236 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15237 if (is_pack)
15238 cp_lexer_consume_token (parser->lexer);
15240 /* Build placeholder. */
15241 tree parm = build_nt (WILDCARD_DECL);
15242 DECL_SOURCE_LOCATION (parm)
15243 = cp_lexer_peek_token (parser->lexer)->location;
15244 DECL_NAME (parm) = cp_parser_identifier (parser);
15245 WILDCARD_PACK_P (parm) = is_pack;
15246 vec_safe_push (introduction_vec, parm);
15248 /* If the next token is not a `,', we're done. */
15249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15250 break;
15251 /* Otherwise, consume the `,' token. */
15252 cp_lexer_consume_token (parser->lexer);
15255 /* Convert the vec into a TREE_VEC. */
15256 tree introduction_list = make_tree_vec (introduction_vec->length ());
15257 unsigned int n;
15258 tree parm;
15259 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15260 TREE_VEC_ELT (introduction_list, n) = parm;
15262 release_tree_vector (introduction_vec);
15263 return introduction_list;
15266 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15267 is an abstract declarator. */
15269 static inline cp_declarator*
15270 get_id_declarator (cp_declarator *declarator)
15272 cp_declarator *d = declarator;
15273 while (d && d->kind != cdk_id)
15274 d = d->declarator;
15275 return d;
15278 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15279 is an abstract declarator. */
15281 static inline tree
15282 get_unqualified_id (cp_declarator *declarator)
15284 declarator = get_id_declarator (declarator);
15285 if (declarator)
15286 return declarator->u.id.unqualified_name;
15287 else
15288 return NULL_TREE;
15291 /* Returns true if DECL represents a constrained-parameter. */
15293 static inline bool
15294 is_constrained_parameter (tree decl)
15296 return (decl
15297 && TREE_CODE (decl) == TYPE_DECL
15298 && CONSTRAINED_PARM_CONCEPT (decl)
15299 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15302 /* Returns true if PARM declares a constrained-parameter. */
15304 static inline bool
15305 is_constrained_parameter (cp_parameter_declarator *parm)
15307 return is_constrained_parameter (parm->decl_specifiers.type);
15310 /* Check that the type parameter is only a declarator-id, and that its
15311 type is not cv-qualified. */
15313 bool
15314 cp_parser_check_constrained_type_parm (cp_parser *parser,
15315 cp_parameter_declarator *parm)
15317 if (!parm->declarator)
15318 return true;
15320 if (parm->declarator->kind != cdk_id)
15322 cp_parser_error (parser, "invalid constrained type parameter");
15323 return false;
15326 /* Don't allow cv-qualified type parameters. */
15327 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15328 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15330 cp_parser_error (parser, "cv-qualified type parameter");
15331 return false;
15334 return true;
15337 /* Finish parsing/processing a template type parameter and checking
15338 various restrictions. */
15340 static inline tree
15341 cp_parser_constrained_type_template_parm (cp_parser *parser,
15342 tree id,
15343 cp_parameter_declarator* parmdecl)
15345 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15346 return finish_template_type_parm (class_type_node, id);
15347 else
15348 return error_mark_node;
15351 static tree
15352 finish_constrained_template_template_parm (tree proto, tree id)
15354 /* FIXME: This should probably be copied, and we may need to adjust
15355 the template parameter depths. */
15356 tree saved_parms = current_template_parms;
15357 begin_template_parm_list ();
15358 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15359 end_template_parm_list ();
15361 tree parm = finish_template_template_parm (class_type_node, id);
15362 current_template_parms = saved_parms;
15364 return parm;
15367 /* Finish parsing/processing a template template parameter by borrowing
15368 the template parameter list from the prototype parameter. */
15370 static tree
15371 cp_parser_constrained_template_template_parm (cp_parser *parser,
15372 tree proto,
15373 tree id,
15374 cp_parameter_declarator *parmdecl)
15376 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15377 return error_mark_node;
15378 return finish_constrained_template_template_parm (proto, id);
15381 /* Create a new non-type template parameter from the given PARM
15382 declarator. */
15384 static tree
15385 constrained_non_type_template_parm (bool *is_non_type,
15386 cp_parameter_declarator *parm)
15388 *is_non_type = true;
15389 cp_declarator *decl = parm->declarator;
15390 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15391 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15392 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15395 /* Build a constrained template parameter based on the PARMDECL
15396 declarator. The type of PARMDECL is the constrained type, which
15397 refers to the prototype template parameter that ultimately
15398 specifies the type of the declared parameter. */
15400 static tree
15401 finish_constrained_parameter (cp_parser *parser,
15402 cp_parameter_declarator *parmdecl,
15403 bool *is_non_type,
15404 bool *is_parameter_pack)
15406 tree decl = parmdecl->decl_specifiers.type;
15407 tree id = get_unqualified_id (parmdecl->declarator);
15408 tree def = parmdecl->default_argument;
15409 tree proto = DECL_INITIAL (decl);
15411 /* A template parameter constrained by a variadic concept shall also
15412 be declared as a template parameter pack. */
15413 bool is_variadic = template_parameter_pack_p (proto);
15414 if (is_variadic && !*is_parameter_pack)
15415 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15417 /* Build the parameter. Return an error if the declarator was invalid. */
15418 tree parm;
15419 if (TREE_CODE (proto) == TYPE_DECL)
15420 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15421 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15422 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15423 parmdecl);
15424 else
15425 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15426 if (parm == error_mark_node)
15427 return error_mark_node;
15429 /* Finish the parameter decl and create a node attaching the
15430 default argument and constraint. */
15431 parm = build_tree_list (def, parm);
15432 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15434 return parm;
15437 /* Returns true if the parsed type actually represents the declaration
15438 of a type template-parameter. */
15440 static inline bool
15441 declares_constrained_type_template_parameter (tree type)
15443 return (is_constrained_parameter (type)
15444 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15448 /* Returns true if the parsed type actually represents the declaration of
15449 a template template-parameter. */
15451 static bool
15452 declares_constrained_template_template_parameter (tree type)
15454 return (is_constrained_parameter (type)
15455 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15458 /* Parse a default argument for a type template-parameter.
15459 Note that diagnostics are handled in cp_parser_template_parameter. */
15461 static tree
15462 cp_parser_default_type_template_argument (cp_parser *parser)
15464 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15466 /* Consume the `=' token. */
15467 cp_lexer_consume_token (parser->lexer);
15469 cp_token *token = cp_lexer_peek_token (parser->lexer);
15471 /* Parse the default-argument. */
15472 push_deferring_access_checks (dk_no_deferred);
15473 tree default_argument = cp_parser_type_id (parser);
15474 pop_deferring_access_checks ();
15476 if (flag_concepts && type_uses_auto (default_argument))
15478 error_at (token->location,
15479 "invalid use of %<auto%> in default template argument");
15480 return error_mark_node;
15483 return default_argument;
15486 /* Parse a default argument for a template template-parameter. */
15488 static tree
15489 cp_parser_default_template_template_argument (cp_parser *parser)
15491 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15493 bool is_template;
15495 /* Consume the `='. */
15496 cp_lexer_consume_token (parser->lexer);
15497 /* Parse the id-expression. */
15498 push_deferring_access_checks (dk_no_deferred);
15499 /* save token before parsing the id-expression, for error
15500 reporting */
15501 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15502 tree default_argument
15503 = cp_parser_id_expression (parser,
15504 /*template_keyword_p=*/false,
15505 /*check_dependency_p=*/true,
15506 /*template_p=*/&is_template,
15507 /*declarator_p=*/false,
15508 /*optional_p=*/false);
15509 if (TREE_CODE (default_argument) == TYPE_DECL)
15510 /* If the id-expression was a template-id that refers to
15511 a template-class, we already have the declaration here,
15512 so no further lookup is needed. */
15514 else
15515 /* Look up the name. */
15516 default_argument
15517 = cp_parser_lookup_name (parser, default_argument,
15518 none_type,
15519 /*is_template=*/is_template,
15520 /*is_namespace=*/false,
15521 /*check_dependency=*/true,
15522 /*ambiguous_decls=*/NULL,
15523 token->location);
15524 /* See if the default argument is valid. */
15525 default_argument = check_template_template_default_arg (default_argument);
15526 pop_deferring_access_checks ();
15527 return default_argument;
15530 /* Parse a template-parameter.
15532 template-parameter:
15533 type-parameter
15534 parameter-declaration
15536 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15537 the parameter. The TREE_PURPOSE is the default value, if any.
15538 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15539 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15540 set to true iff this parameter is a parameter pack. */
15542 static tree
15543 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15544 bool *is_parameter_pack)
15546 cp_token *token;
15547 cp_parameter_declarator *parameter_declarator;
15548 tree parm;
15550 /* Assume it is a type parameter or a template parameter. */
15551 *is_non_type = false;
15552 /* Assume it not a parameter pack. */
15553 *is_parameter_pack = false;
15554 /* Peek at the next token. */
15555 token = cp_lexer_peek_token (parser->lexer);
15556 /* If it is `template', we have a type-parameter. */
15557 if (token->keyword == RID_TEMPLATE)
15558 return cp_parser_type_parameter (parser, is_parameter_pack);
15559 /* If it is `class' or `typename' we do not know yet whether it is a
15560 type parameter or a non-type parameter. Consider:
15562 template <typename T, typename T::X X> ...
15566 template <class C, class D*> ...
15568 Here, the first parameter is a type parameter, and the second is
15569 a non-type parameter. We can tell by looking at the token after
15570 the identifier -- if it is a `,', `=', or `>' then we have a type
15571 parameter. */
15572 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15574 /* Peek at the token after `class' or `typename'. */
15575 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15576 /* If it's an ellipsis, we have a template type parameter
15577 pack. */
15578 if (token->type == CPP_ELLIPSIS)
15579 return cp_parser_type_parameter (parser, is_parameter_pack);
15580 /* If it's an identifier, skip it. */
15581 if (token->type == CPP_NAME)
15582 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15583 /* Now, see if the token looks like the end of a template
15584 parameter. */
15585 if (token->type == CPP_COMMA
15586 || token->type == CPP_EQ
15587 || token->type == CPP_GREATER)
15588 return cp_parser_type_parameter (parser, is_parameter_pack);
15591 /* Otherwise, it is a non-type parameter or a constrained parameter.
15593 [temp.param]
15595 When parsing a default template-argument for a non-type
15596 template-parameter, the first non-nested `>' is taken as the end
15597 of the template parameter-list rather than a greater-than
15598 operator. */
15599 parameter_declarator
15600 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15601 /*parenthesized_p=*/NULL);
15603 if (!parameter_declarator)
15604 return error_mark_node;
15606 /* If the parameter declaration is marked as a parameter pack, set
15607 *IS_PARAMETER_PACK to notify the caller. */
15608 if (parameter_declarator->template_parameter_pack_p)
15609 *is_parameter_pack = true;
15611 if (parameter_declarator->default_argument)
15613 /* Can happen in some cases of erroneous input (c++/34892). */
15614 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15615 /* Consume the `...' for better error recovery. */
15616 cp_lexer_consume_token (parser->lexer);
15619 // The parameter may have been constrained.
15620 if (is_constrained_parameter (parameter_declarator))
15621 return finish_constrained_parameter (parser,
15622 parameter_declarator,
15623 is_non_type,
15624 is_parameter_pack);
15626 // Now we're sure that the parameter is a non-type parameter.
15627 *is_non_type = true;
15629 parm = grokdeclarator (parameter_declarator->declarator,
15630 &parameter_declarator->decl_specifiers,
15631 TPARM, /*initialized=*/0,
15632 /*attrlist=*/NULL);
15633 if (parm == error_mark_node)
15634 return error_mark_node;
15636 return build_tree_list (parameter_declarator->default_argument, parm);
15639 /* Parse a type-parameter.
15641 type-parameter:
15642 class identifier [opt]
15643 class identifier [opt] = type-id
15644 typename identifier [opt]
15645 typename identifier [opt] = type-id
15646 template < template-parameter-list > class identifier [opt]
15647 template < template-parameter-list > class identifier [opt]
15648 = id-expression
15650 GNU Extension (variadic templates):
15652 type-parameter:
15653 class ... identifier [opt]
15654 typename ... identifier [opt]
15656 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15657 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15658 the declaration of the parameter.
15660 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15662 static tree
15663 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15665 cp_token *token;
15666 tree parameter;
15668 /* Look for a keyword to tell us what kind of parameter this is. */
15669 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15670 if (!token)
15671 return error_mark_node;
15673 switch (token->keyword)
15675 case RID_CLASS:
15676 case RID_TYPENAME:
15678 tree identifier;
15679 tree default_argument;
15681 /* If the next token is an ellipsis, we have a template
15682 argument pack. */
15683 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15685 /* Consume the `...' token. */
15686 cp_lexer_consume_token (parser->lexer);
15687 maybe_warn_variadic_templates ();
15689 *is_parameter_pack = true;
15692 /* If the next token is an identifier, then it names the
15693 parameter. */
15694 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15695 identifier = cp_parser_identifier (parser);
15696 else
15697 identifier = NULL_TREE;
15699 /* Create the parameter. */
15700 parameter = finish_template_type_parm (class_type_node, identifier);
15702 /* If the next token is an `=', we have a default argument. */
15703 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15705 default_argument
15706 = cp_parser_default_type_template_argument (parser);
15708 /* Template parameter packs cannot have default
15709 arguments. */
15710 if (*is_parameter_pack)
15712 if (identifier)
15713 error_at (token->location,
15714 "template parameter pack %qD cannot have a "
15715 "default argument", identifier);
15716 else
15717 error_at (token->location,
15718 "template parameter packs cannot have "
15719 "default arguments");
15720 default_argument = NULL_TREE;
15722 else if (check_for_bare_parameter_packs (default_argument))
15723 default_argument = error_mark_node;
15725 else
15726 default_argument = NULL_TREE;
15728 /* Create the combined representation of the parameter and the
15729 default argument. */
15730 parameter = build_tree_list (default_argument, parameter);
15732 break;
15734 case RID_TEMPLATE:
15736 tree identifier;
15737 tree default_argument;
15739 /* Look for the `<'. */
15740 cp_parser_require (parser, CPP_LESS, RT_LESS);
15741 /* Parse the template-parameter-list. */
15742 cp_parser_template_parameter_list (parser);
15743 /* Look for the `>'. */
15744 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15746 // If template requirements are present, parse them.
15747 if (flag_concepts)
15749 tree reqs = get_shorthand_constraints (current_template_parms);
15750 if (tree r = cp_parser_requires_clause_opt (parser))
15751 reqs = conjoin_constraints (reqs, normalize_expression (r));
15752 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15755 /* Look for the `class' or 'typename' keywords. */
15756 cp_parser_type_parameter_key (parser);
15757 /* If the next token is an ellipsis, we have a template
15758 argument pack. */
15759 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15761 /* Consume the `...' token. */
15762 cp_lexer_consume_token (parser->lexer);
15763 maybe_warn_variadic_templates ();
15765 *is_parameter_pack = true;
15767 /* If the next token is an `=', then there is a
15768 default-argument. If the next token is a `>', we are at
15769 the end of the parameter-list. If the next token is a `,',
15770 then we are at the end of this parameter. */
15771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15772 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15773 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15775 identifier = cp_parser_identifier (parser);
15776 /* Treat invalid names as if the parameter were nameless. */
15777 if (identifier == error_mark_node)
15778 identifier = NULL_TREE;
15780 else
15781 identifier = NULL_TREE;
15783 /* Create the template parameter. */
15784 parameter = finish_template_template_parm (class_type_node,
15785 identifier);
15787 /* If the next token is an `=', then there is a
15788 default-argument. */
15789 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15791 default_argument
15792 = cp_parser_default_template_template_argument (parser);
15794 /* Template parameter packs cannot have default
15795 arguments. */
15796 if (*is_parameter_pack)
15798 if (identifier)
15799 error_at (token->location,
15800 "template parameter pack %qD cannot "
15801 "have a default argument",
15802 identifier);
15803 else
15804 error_at (token->location, "template parameter packs cannot "
15805 "have default arguments");
15806 default_argument = NULL_TREE;
15809 else
15810 default_argument = NULL_TREE;
15812 /* Create the combined representation of the parameter and the
15813 default argument. */
15814 parameter = build_tree_list (default_argument, parameter);
15816 break;
15818 default:
15819 gcc_unreachable ();
15820 break;
15823 return parameter;
15826 /* Parse a template-id.
15828 template-id:
15829 template-name < template-argument-list [opt] >
15831 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15832 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15833 returned. Otherwise, if the template-name names a function, or set
15834 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15835 names a class, returns a TYPE_DECL for the specialization.
15837 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15838 uninstantiated templates. */
15840 static tree
15841 cp_parser_template_id (cp_parser *parser,
15842 bool template_keyword_p,
15843 bool check_dependency_p,
15844 enum tag_types tag_type,
15845 bool is_declaration)
15847 tree templ;
15848 tree arguments;
15849 tree template_id;
15850 cp_token_position start_of_id = 0;
15851 cp_token *next_token = NULL, *next_token_2 = NULL;
15852 bool is_identifier;
15854 /* If the next token corresponds to a template-id, there is no need
15855 to reparse it. */
15856 cp_token *token = cp_lexer_peek_token (parser->lexer);
15857 if (token->type == CPP_TEMPLATE_ID)
15859 cp_lexer_consume_token (parser->lexer);
15860 return saved_checks_value (token->u.tree_check_value);
15863 /* Avoid performing name lookup if there is no possibility of
15864 finding a template-id. */
15865 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15866 || (token->type == CPP_NAME
15867 && !cp_parser_nth_token_starts_template_argument_list_p
15868 (parser, 2)))
15870 cp_parser_error (parser, "expected template-id");
15871 return error_mark_node;
15874 /* Remember where the template-id starts. */
15875 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15876 start_of_id = cp_lexer_token_position (parser->lexer, false);
15878 push_deferring_access_checks (dk_deferred);
15880 /* Parse the template-name. */
15881 is_identifier = false;
15882 templ = cp_parser_template_name (parser, template_keyword_p,
15883 check_dependency_p,
15884 is_declaration,
15885 tag_type,
15886 &is_identifier);
15887 if (templ == error_mark_node || is_identifier)
15889 pop_deferring_access_checks ();
15890 return templ;
15893 /* Since we're going to preserve any side-effects from this parse, set up a
15894 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15895 in the template arguments. */
15896 tentative_firewall firewall (parser);
15898 /* If we find the sequence `[:' after a template-name, it's probably
15899 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15900 parse correctly the argument list. */
15901 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15902 == CPP_OPEN_SQUARE)
15903 && next_token->flags & DIGRAPH
15904 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15905 == CPP_COLON)
15906 && !(next_token_2->flags & PREV_WHITE))
15908 cp_parser_parse_tentatively (parser);
15909 /* Change `:' into `::'. */
15910 next_token_2->type = CPP_SCOPE;
15911 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15912 CPP_LESS. */
15913 cp_lexer_consume_token (parser->lexer);
15915 /* Parse the arguments. */
15916 arguments = cp_parser_enclosed_template_argument_list (parser);
15917 if (!cp_parser_parse_definitely (parser))
15919 /* If we couldn't parse an argument list, then we revert our changes
15920 and return simply an error. Maybe this is not a template-id
15921 after all. */
15922 next_token_2->type = CPP_COLON;
15923 cp_parser_error (parser, "expected %<<%>");
15924 pop_deferring_access_checks ();
15925 return error_mark_node;
15927 /* Otherwise, emit an error about the invalid digraph, but continue
15928 parsing because we got our argument list. */
15929 if (permerror (next_token->location,
15930 "%<<::%> cannot begin a template-argument list"))
15932 static bool hint = false;
15933 inform (next_token->location,
15934 "%<<:%> is an alternate spelling for %<[%>."
15935 " Insert whitespace between %<<%> and %<::%>");
15936 if (!hint && !flag_permissive)
15938 inform (next_token->location, "(if you use %<-fpermissive%> "
15939 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15940 "accept your code)");
15941 hint = true;
15945 else
15947 /* Look for the `<' that starts the template-argument-list. */
15948 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15950 pop_deferring_access_checks ();
15951 return error_mark_node;
15953 /* Parse the arguments. */
15954 arguments = cp_parser_enclosed_template_argument_list (parser);
15957 /* Set the location to be of the form:
15958 template-name < template-argument-list [opt] >
15959 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15960 with caret == start at the start of the template-name,
15961 ranging until the closing '>'. */
15962 location_t finish_loc
15963 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15964 location_t combined_loc
15965 = make_location (token->location, token->location, finish_loc);
15967 /* Check for concepts autos where they don't belong. We could
15968 identify types in some cases of idnetifier TEMPL, looking ahead
15969 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15970 types. We reject them in functions, but if what we have is an
15971 identifier, even with none_type we can't conclude it's NOT a
15972 type, we have to wait for template substitution. */
15973 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15974 template_id = error_mark_node;
15975 /* Build a representation of the specialization. */
15976 else if (identifier_p (templ))
15977 template_id = build_min_nt_loc (combined_loc,
15978 TEMPLATE_ID_EXPR,
15979 templ, arguments);
15980 else if (DECL_TYPE_TEMPLATE_P (templ)
15981 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15983 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15984 template (rather than some instantiation thereof) only if
15985 is not nested within some other construct. For example, in
15986 "template <typename T> void f(T) { A<T>::", A<T> is just an
15987 instantiation of A. */
15988 bool entering_scope
15989 = (template_parm_scope_p ()
15990 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
15991 template_id
15992 = finish_template_type (templ, arguments, entering_scope);
15994 /* A template-like identifier may be a partial concept id. */
15995 else if (flag_concepts
15996 && (template_id = (cp_parser_maybe_partial_concept_id
15997 (parser, templ, arguments))))
15998 return template_id;
15999 else if (variable_template_p (templ))
16001 template_id = lookup_template_variable (templ, arguments);
16002 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16003 SET_EXPR_LOCATION (template_id, combined_loc);
16005 else
16007 /* If it's not a class-template or a template-template, it should be
16008 a function-template. */
16009 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16010 || TREE_CODE (templ) == OVERLOAD
16011 || BASELINK_P (templ)));
16013 template_id = lookup_template_function (templ, arguments);
16014 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16015 SET_EXPR_LOCATION (template_id, combined_loc);
16018 /* If parsing tentatively, replace the sequence of tokens that makes
16019 up the template-id with a CPP_TEMPLATE_ID token. That way,
16020 should we re-parse the token stream, we will not have to repeat
16021 the effort required to do the parse, nor will we issue duplicate
16022 error messages about problems during instantiation of the
16023 template. */
16024 if (start_of_id
16025 /* Don't do this if we had a parse error in a declarator; re-parsing
16026 might succeed if a name changes meaning (60361). */
16027 && !(cp_parser_error_occurred (parser)
16028 && cp_parser_parsing_tentatively (parser)
16029 && parser->in_declarator_p))
16031 /* Reset the contents of the START_OF_ID token. */
16032 token->type = CPP_TEMPLATE_ID;
16033 token->location = combined_loc;
16035 /* Retrieve any deferred checks. Do not pop this access checks yet
16036 so the memory will not be reclaimed during token replacing below. */
16037 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16038 token->u.tree_check_value->value = template_id;
16039 token->u.tree_check_value->checks = get_deferred_access_checks ();
16040 token->keyword = RID_MAX;
16042 /* Purge all subsequent tokens. */
16043 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16045 /* ??? Can we actually assume that, if template_id ==
16046 error_mark_node, we will have issued a diagnostic to the
16047 user, as opposed to simply marking the tentative parse as
16048 failed? */
16049 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16050 error_at (token->location, "parse error in template argument list");
16053 pop_to_parent_deferring_access_checks ();
16054 return template_id;
16057 /* Parse a template-name.
16059 template-name:
16060 identifier
16062 The standard should actually say:
16064 template-name:
16065 identifier
16066 operator-function-id
16068 A defect report has been filed about this issue.
16070 A conversion-function-id cannot be a template name because they cannot
16071 be part of a template-id. In fact, looking at this code:
16073 a.operator K<int>()
16075 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16076 It is impossible to call a templated conversion-function-id with an
16077 explicit argument list, since the only allowed template parameter is
16078 the type to which it is converting.
16080 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16081 `template' keyword, in a construction like:
16083 T::template f<3>()
16085 In that case `f' is taken to be a template-name, even though there
16086 is no way of knowing for sure.
16088 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16089 name refers to a set of overloaded functions, at least one of which
16090 is a template, or an IDENTIFIER_NODE with the name of the template,
16091 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16092 names are looked up inside uninstantiated templates. */
16094 static tree
16095 cp_parser_template_name (cp_parser* parser,
16096 bool template_keyword_p,
16097 bool check_dependency_p,
16098 bool is_declaration,
16099 enum tag_types tag_type,
16100 bool *is_identifier)
16102 tree identifier;
16103 tree decl;
16104 cp_token *token = cp_lexer_peek_token (parser->lexer);
16106 /* If the next token is `operator', then we have either an
16107 operator-function-id or a conversion-function-id. */
16108 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16110 /* We don't know whether we're looking at an
16111 operator-function-id or a conversion-function-id. */
16112 cp_parser_parse_tentatively (parser);
16113 /* Try an operator-function-id. */
16114 identifier = cp_parser_operator_function_id (parser);
16115 /* If that didn't work, try a conversion-function-id. */
16116 if (!cp_parser_parse_definitely (parser))
16118 cp_parser_error (parser, "expected template-name");
16119 return error_mark_node;
16122 /* Look for the identifier. */
16123 else
16124 identifier = cp_parser_identifier (parser);
16126 /* If we didn't find an identifier, we don't have a template-id. */
16127 if (identifier == error_mark_node)
16128 return error_mark_node;
16130 /* If the name immediately followed the `template' keyword, then it
16131 is a template-name. However, if the next token is not `<', then
16132 we do not treat it as a template-name, since it is not being used
16133 as part of a template-id. This enables us to handle constructs
16134 like:
16136 template <typename T> struct S { S(); };
16137 template <typename T> S<T>::S();
16139 correctly. We would treat `S' as a template -- if it were `S<T>'
16140 -- but we do not if there is no `<'. */
16142 if (processing_template_decl
16143 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16145 /* In a declaration, in a dependent context, we pretend that the
16146 "template" keyword was present in order to improve error
16147 recovery. For example, given:
16149 template <typename T> void f(T::X<int>);
16151 we want to treat "X<int>" as a template-id. */
16152 if (is_declaration
16153 && !template_keyword_p
16154 && parser->scope && TYPE_P (parser->scope)
16155 && check_dependency_p
16156 && dependent_scope_p (parser->scope)
16157 /* Do not do this for dtors (or ctors), since they never
16158 need the template keyword before their name. */
16159 && !constructor_name_p (identifier, parser->scope))
16161 cp_token_position start = 0;
16163 /* Explain what went wrong. */
16164 error_at (token->location, "non-template %qD used as template",
16165 identifier);
16166 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16167 parser->scope, identifier);
16168 /* If parsing tentatively, find the location of the "<" token. */
16169 if (cp_parser_simulate_error (parser))
16170 start = cp_lexer_token_position (parser->lexer, true);
16171 /* Parse the template arguments so that we can issue error
16172 messages about them. */
16173 cp_lexer_consume_token (parser->lexer);
16174 cp_parser_enclosed_template_argument_list (parser);
16175 /* Skip tokens until we find a good place from which to
16176 continue parsing. */
16177 cp_parser_skip_to_closing_parenthesis (parser,
16178 /*recovering=*/true,
16179 /*or_comma=*/true,
16180 /*consume_paren=*/false);
16181 /* If parsing tentatively, permanently remove the
16182 template argument list. That will prevent duplicate
16183 error messages from being issued about the missing
16184 "template" keyword. */
16185 if (start)
16186 cp_lexer_purge_tokens_after (parser->lexer, start);
16187 if (is_identifier)
16188 *is_identifier = true;
16189 parser->context->object_type = NULL_TREE;
16190 return identifier;
16193 /* If the "template" keyword is present, then there is generally
16194 no point in doing name-lookup, so we just return IDENTIFIER.
16195 But, if the qualifying scope is non-dependent then we can
16196 (and must) do name-lookup normally. */
16197 if (template_keyword_p)
16199 tree scope = (parser->scope ? parser->scope
16200 : parser->context->object_type);
16201 if (scope && TYPE_P (scope)
16202 && (!CLASS_TYPE_P (scope)
16203 || (check_dependency_p && dependent_type_p (scope))))
16205 /* We're optimizing away the call to cp_parser_lookup_name, but
16206 we still need to do this. */
16207 parser->context->object_type = NULL_TREE;
16208 return identifier;
16213 /* Look up the name. */
16214 decl = cp_parser_lookup_name (parser, identifier,
16215 tag_type,
16216 /*is_template=*/true,
16217 /*is_namespace=*/false,
16218 check_dependency_p,
16219 /*ambiguous_decls=*/NULL,
16220 token->location);
16222 decl = strip_using_decl (decl);
16224 /* If DECL is a template, then the name was a template-name. */
16225 if (TREE_CODE (decl) == TEMPLATE_DECL)
16227 if (TREE_DEPRECATED (decl)
16228 && deprecated_state != DEPRECATED_SUPPRESS)
16229 warn_deprecated_use (decl, NULL_TREE);
16231 else
16233 /* The standard does not explicitly indicate whether a name that
16234 names a set of overloaded declarations, some of which are
16235 templates, is a template-name. However, such a name should
16236 be a template-name; otherwise, there is no way to form a
16237 template-id for the overloaded templates. */
16238 bool found = false;
16240 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16241 !found && iter; ++iter)
16242 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16243 found = true;
16245 if (!found)
16247 /* The name does not name a template. */
16248 cp_parser_error (parser, "expected template-name");
16249 return error_mark_node;
16253 /* If DECL is dependent, and refers to a function, then just return
16254 its name; we will look it up again during template instantiation. */
16255 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16257 tree scope = ovl_scope (decl);
16258 if (TYPE_P (scope) && dependent_type_p (scope))
16259 return identifier;
16262 return decl;
16265 /* Parse a template-argument-list.
16267 template-argument-list:
16268 template-argument ... [opt]
16269 template-argument-list , template-argument ... [opt]
16271 Returns a TREE_VEC containing the arguments. */
16273 static tree
16274 cp_parser_template_argument_list (cp_parser* parser)
16276 tree fixed_args[10];
16277 unsigned n_args = 0;
16278 unsigned alloced = 10;
16279 tree *arg_ary = fixed_args;
16280 tree vec;
16281 bool saved_in_template_argument_list_p;
16282 bool saved_ice_p;
16283 bool saved_non_ice_p;
16285 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16286 parser->in_template_argument_list_p = true;
16287 /* Even if the template-id appears in an integral
16288 constant-expression, the contents of the argument list do
16289 not. */
16290 saved_ice_p = parser->integral_constant_expression_p;
16291 parser->integral_constant_expression_p = false;
16292 saved_non_ice_p = parser->non_integral_constant_expression_p;
16293 parser->non_integral_constant_expression_p = false;
16295 /* Parse the arguments. */
16298 tree argument;
16300 if (n_args)
16301 /* Consume the comma. */
16302 cp_lexer_consume_token (parser->lexer);
16304 /* Parse the template-argument. */
16305 argument = cp_parser_template_argument (parser);
16307 /* If the next token is an ellipsis, we're expanding a template
16308 argument pack. */
16309 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16311 if (argument == error_mark_node)
16313 cp_token *token = cp_lexer_peek_token (parser->lexer);
16314 error_at (token->location,
16315 "expected parameter pack before %<...%>");
16317 /* Consume the `...' token. */
16318 cp_lexer_consume_token (parser->lexer);
16320 /* Make the argument into a TYPE_PACK_EXPANSION or
16321 EXPR_PACK_EXPANSION. */
16322 argument = make_pack_expansion (argument);
16325 if (n_args == alloced)
16327 alloced *= 2;
16329 if (arg_ary == fixed_args)
16331 arg_ary = XNEWVEC (tree, alloced);
16332 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16334 else
16335 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16337 arg_ary[n_args++] = argument;
16339 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16341 vec = make_tree_vec (n_args);
16343 while (n_args--)
16344 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16346 if (arg_ary != fixed_args)
16347 free (arg_ary);
16348 parser->non_integral_constant_expression_p = saved_non_ice_p;
16349 parser->integral_constant_expression_p = saved_ice_p;
16350 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16351 if (CHECKING_P)
16352 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16353 return vec;
16356 /* Parse a template-argument.
16358 template-argument:
16359 assignment-expression
16360 type-id
16361 id-expression
16363 The representation is that of an assignment-expression, type-id, or
16364 id-expression -- except that the qualified id-expression is
16365 evaluated, so that the value returned is either a DECL or an
16366 OVERLOAD.
16368 Although the standard says "assignment-expression", it forbids
16369 throw-expressions or assignments in the template argument.
16370 Therefore, we use "conditional-expression" instead. */
16372 static tree
16373 cp_parser_template_argument (cp_parser* parser)
16375 tree argument;
16376 bool template_p;
16377 bool address_p;
16378 bool maybe_type_id = false;
16379 cp_token *token = NULL, *argument_start_token = NULL;
16380 location_t loc = 0;
16381 cp_id_kind idk;
16383 /* There's really no way to know what we're looking at, so we just
16384 try each alternative in order.
16386 [temp.arg]
16388 In a template-argument, an ambiguity between a type-id and an
16389 expression is resolved to a type-id, regardless of the form of
16390 the corresponding template-parameter.
16392 Therefore, we try a type-id first. */
16393 cp_parser_parse_tentatively (parser);
16394 argument = cp_parser_template_type_arg (parser);
16395 /* If there was no error parsing the type-id but the next token is a
16396 '>>', our behavior depends on which dialect of C++ we're
16397 parsing. In C++98, we probably found a typo for '> >'. But there
16398 are type-id which are also valid expressions. For instance:
16400 struct X { int operator >> (int); };
16401 template <int V> struct Foo {};
16402 Foo<X () >> 5> r;
16404 Here 'X()' is a valid type-id of a function type, but the user just
16405 wanted to write the expression "X() >> 5". Thus, we remember that we
16406 found a valid type-id, but we still try to parse the argument as an
16407 expression to see what happens.
16409 In C++0x, the '>>' will be considered two separate '>'
16410 tokens. */
16411 if (!cp_parser_error_occurred (parser)
16412 && cxx_dialect == cxx98
16413 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16415 maybe_type_id = true;
16416 cp_parser_abort_tentative_parse (parser);
16418 else
16420 /* If the next token isn't a `,' or a `>', then this argument wasn't
16421 really finished. This means that the argument is not a valid
16422 type-id. */
16423 if (!cp_parser_next_token_ends_template_argument_p (parser))
16424 cp_parser_error (parser, "expected template-argument");
16425 /* If that worked, we're done. */
16426 if (cp_parser_parse_definitely (parser))
16427 return argument;
16429 /* We're still not sure what the argument will be. */
16430 cp_parser_parse_tentatively (parser);
16431 /* Try a template. */
16432 argument_start_token = cp_lexer_peek_token (parser->lexer);
16433 argument = cp_parser_id_expression (parser,
16434 /*template_keyword_p=*/false,
16435 /*check_dependency_p=*/true,
16436 &template_p,
16437 /*declarator_p=*/false,
16438 /*optional_p=*/false);
16439 /* If the next token isn't a `,' or a `>', then this argument wasn't
16440 really finished. */
16441 if (!cp_parser_next_token_ends_template_argument_p (parser))
16442 cp_parser_error (parser, "expected template-argument");
16443 if (!cp_parser_error_occurred (parser))
16445 /* Figure out what is being referred to. If the id-expression
16446 was for a class template specialization, then we will have a
16447 TYPE_DECL at this point. There is no need to do name lookup
16448 at this point in that case. */
16449 if (TREE_CODE (argument) != TYPE_DECL)
16450 argument = cp_parser_lookup_name (parser, argument,
16451 none_type,
16452 /*is_template=*/template_p,
16453 /*is_namespace=*/false,
16454 /*check_dependency=*/true,
16455 /*ambiguous_decls=*/NULL,
16456 argument_start_token->location);
16457 /* Handle a constrained-type-specifier for a non-type template
16458 parameter. */
16459 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16460 argument = decl;
16461 else if (TREE_CODE (argument) != TEMPLATE_DECL
16462 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16463 cp_parser_error (parser, "expected template-name");
16465 if (cp_parser_parse_definitely (parser))
16467 if (TREE_DEPRECATED (argument))
16468 warn_deprecated_use (argument, NULL_TREE);
16469 return argument;
16471 /* It must be a non-type argument. In C++17 any constant-expression is
16472 allowed. */
16473 if (cxx_dialect > cxx14)
16474 goto general_expr;
16476 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16478 -- an integral constant-expression of integral or enumeration
16479 type; or
16481 -- the name of a non-type template-parameter; or
16483 -- the name of an object or function with external linkage...
16485 -- the address of an object or function with external linkage...
16487 -- a pointer to member... */
16488 /* Look for a non-type template parameter. */
16489 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16491 cp_parser_parse_tentatively (parser);
16492 argument = cp_parser_primary_expression (parser,
16493 /*address_p=*/false,
16494 /*cast_p=*/false,
16495 /*template_arg_p=*/true,
16496 &idk);
16497 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16498 || !cp_parser_next_token_ends_template_argument_p (parser))
16499 cp_parser_simulate_error (parser);
16500 if (cp_parser_parse_definitely (parser))
16501 return argument;
16504 /* If the next token is "&", the argument must be the address of an
16505 object or function with external linkage. */
16506 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16507 if (address_p)
16509 loc = cp_lexer_peek_token (parser->lexer)->location;
16510 cp_lexer_consume_token (parser->lexer);
16512 /* See if we might have an id-expression. */
16513 token = cp_lexer_peek_token (parser->lexer);
16514 if (token->type == CPP_NAME
16515 || token->keyword == RID_OPERATOR
16516 || token->type == CPP_SCOPE
16517 || token->type == CPP_TEMPLATE_ID
16518 || token->type == CPP_NESTED_NAME_SPECIFIER)
16520 cp_parser_parse_tentatively (parser);
16521 argument = cp_parser_primary_expression (parser,
16522 address_p,
16523 /*cast_p=*/false,
16524 /*template_arg_p=*/true,
16525 &idk);
16526 if (cp_parser_error_occurred (parser)
16527 || !cp_parser_next_token_ends_template_argument_p (parser))
16528 cp_parser_abort_tentative_parse (parser);
16529 else
16531 tree probe;
16533 if (INDIRECT_REF_P (argument))
16535 /* Strip the dereference temporarily. */
16536 gcc_assert (REFERENCE_REF_P (argument));
16537 argument = TREE_OPERAND (argument, 0);
16540 /* If we're in a template, we represent a qualified-id referring
16541 to a static data member as a SCOPE_REF even if the scope isn't
16542 dependent so that we can check access control later. */
16543 probe = argument;
16544 if (TREE_CODE (probe) == SCOPE_REF)
16545 probe = TREE_OPERAND (probe, 1);
16546 if (VAR_P (probe))
16548 /* A variable without external linkage might still be a
16549 valid constant-expression, so no error is issued here
16550 if the external-linkage check fails. */
16551 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16552 cp_parser_simulate_error (parser);
16554 else if (is_overloaded_fn (argument))
16555 /* All overloaded functions are allowed; if the external
16556 linkage test does not pass, an error will be issued
16557 later. */
16559 else if (address_p
16560 && (TREE_CODE (argument) == OFFSET_REF
16561 || TREE_CODE (argument) == SCOPE_REF))
16562 /* A pointer-to-member. */
16564 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16566 else
16567 cp_parser_simulate_error (parser);
16569 if (cp_parser_parse_definitely (parser))
16571 if (address_p)
16572 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16573 tf_warning_or_error);
16574 else
16575 argument = convert_from_reference (argument);
16576 return argument;
16580 /* If the argument started with "&", there are no other valid
16581 alternatives at this point. */
16582 if (address_p)
16584 cp_parser_error (parser, "invalid non-type template argument");
16585 return error_mark_node;
16588 general_expr:
16589 /* If the argument wasn't successfully parsed as a type-id followed
16590 by '>>', the argument can only be a constant expression now.
16591 Otherwise, we try parsing the constant-expression tentatively,
16592 because the argument could really be a type-id. */
16593 if (maybe_type_id)
16594 cp_parser_parse_tentatively (parser);
16596 if (cxx_dialect <= cxx14)
16597 argument = cp_parser_constant_expression (parser);
16598 else
16600 /* With C++17 generalized non-type template arguments we need to handle
16601 lvalue constant expressions, too. */
16602 argument = cp_parser_assignment_expression (parser);
16603 require_potential_constant_expression (argument);
16606 if (!maybe_type_id)
16607 return argument;
16608 if (!cp_parser_next_token_ends_template_argument_p (parser))
16609 cp_parser_error (parser, "expected template-argument");
16610 if (cp_parser_parse_definitely (parser))
16611 return argument;
16612 /* We did our best to parse the argument as a non type-id, but that
16613 was the only alternative that matched (albeit with a '>' after
16614 it). We can assume it's just a typo from the user, and a
16615 diagnostic will then be issued. */
16616 return cp_parser_template_type_arg (parser);
16619 /* Parse an explicit-instantiation.
16621 explicit-instantiation:
16622 template declaration
16624 Although the standard says `declaration', what it really means is:
16626 explicit-instantiation:
16627 template decl-specifier-seq [opt] declarator [opt] ;
16629 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16630 supposed to be allowed. A defect report has been filed about this
16631 issue.
16633 GNU Extension:
16635 explicit-instantiation:
16636 storage-class-specifier template
16637 decl-specifier-seq [opt] declarator [opt] ;
16638 function-specifier template
16639 decl-specifier-seq [opt] declarator [opt] ; */
16641 static void
16642 cp_parser_explicit_instantiation (cp_parser* parser)
16644 int declares_class_or_enum;
16645 cp_decl_specifier_seq decl_specifiers;
16646 tree extension_specifier = NULL_TREE;
16648 timevar_push (TV_TEMPLATE_INST);
16650 /* Look for an (optional) storage-class-specifier or
16651 function-specifier. */
16652 if (cp_parser_allow_gnu_extensions_p (parser))
16654 extension_specifier
16655 = cp_parser_storage_class_specifier_opt (parser);
16656 if (!extension_specifier)
16657 extension_specifier
16658 = cp_parser_function_specifier_opt (parser,
16659 /*decl_specs=*/NULL);
16662 /* Look for the `template' keyword. */
16663 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16664 /* Let the front end know that we are processing an explicit
16665 instantiation. */
16666 begin_explicit_instantiation ();
16667 /* [temp.explicit] says that we are supposed to ignore access
16668 control while processing explicit instantiation directives. */
16669 push_deferring_access_checks (dk_no_check);
16670 /* Parse a decl-specifier-seq. */
16671 cp_parser_decl_specifier_seq (parser,
16672 CP_PARSER_FLAGS_OPTIONAL,
16673 &decl_specifiers,
16674 &declares_class_or_enum);
16675 /* If there was exactly one decl-specifier, and it declared a class,
16676 and there's no declarator, then we have an explicit type
16677 instantiation. */
16678 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16680 tree type;
16682 type = check_tag_decl (&decl_specifiers,
16683 /*explicit_type_instantiation_p=*/true);
16684 /* Turn access control back on for names used during
16685 template instantiation. */
16686 pop_deferring_access_checks ();
16687 if (type)
16688 do_type_instantiation (type, extension_specifier,
16689 /*complain=*/tf_error);
16691 else
16693 cp_declarator *declarator;
16694 tree decl;
16696 /* Parse the declarator. */
16697 declarator
16698 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16699 /*ctor_dtor_or_conv_p=*/NULL,
16700 /*parenthesized_p=*/NULL,
16701 /*member_p=*/false,
16702 /*friend_p=*/false);
16703 if (declares_class_or_enum & 2)
16704 cp_parser_check_for_definition_in_return_type (declarator,
16705 decl_specifiers.type,
16706 decl_specifiers.locations[ds_type_spec]);
16707 if (declarator != cp_error_declarator)
16709 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16710 permerror (decl_specifiers.locations[ds_inline],
16711 "explicit instantiation shall not use"
16712 " %<inline%> specifier");
16713 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16714 permerror (decl_specifiers.locations[ds_constexpr],
16715 "explicit instantiation shall not use"
16716 " %<constexpr%> specifier");
16718 decl = grokdeclarator (declarator, &decl_specifiers,
16719 NORMAL, 0, &decl_specifiers.attributes);
16720 /* Turn access control back on for names used during
16721 template instantiation. */
16722 pop_deferring_access_checks ();
16723 /* Do the explicit instantiation. */
16724 do_decl_instantiation (decl, extension_specifier);
16726 else
16728 pop_deferring_access_checks ();
16729 /* Skip the body of the explicit instantiation. */
16730 cp_parser_skip_to_end_of_statement (parser);
16733 /* We're done with the instantiation. */
16734 end_explicit_instantiation ();
16736 cp_parser_consume_semicolon_at_end_of_statement (parser);
16738 timevar_pop (TV_TEMPLATE_INST);
16741 /* Parse an explicit-specialization.
16743 explicit-specialization:
16744 template < > declaration
16746 Although the standard says `declaration', what it really means is:
16748 explicit-specialization:
16749 template <> decl-specifier [opt] init-declarator [opt] ;
16750 template <> function-definition
16751 template <> explicit-specialization
16752 template <> template-declaration */
16754 static void
16755 cp_parser_explicit_specialization (cp_parser* parser)
16757 bool need_lang_pop;
16758 cp_token *token = cp_lexer_peek_token (parser->lexer);
16760 /* Look for the `template' keyword. */
16761 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16762 /* Look for the `<'. */
16763 cp_parser_require (parser, CPP_LESS, RT_LESS);
16764 /* Look for the `>'. */
16765 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16766 /* We have processed another parameter list. */
16767 ++parser->num_template_parameter_lists;
16768 /* [temp]
16770 A template ... explicit specialization ... shall not have C
16771 linkage. */
16772 if (current_lang_name == lang_name_c)
16774 error_at (token->location, "template specialization with C linkage");
16775 maybe_show_extern_c_location ();
16776 /* Give it C++ linkage to avoid confusing other parts of the
16777 front end. */
16778 push_lang_context (lang_name_cplusplus);
16779 need_lang_pop = true;
16781 else
16782 need_lang_pop = false;
16783 /* Let the front end know that we are beginning a specialization. */
16784 if (!begin_specialization ())
16786 end_specialization ();
16787 return;
16790 /* If the next keyword is `template', we need to figure out whether
16791 or not we're looking a template-declaration. */
16792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16794 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16795 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16796 cp_parser_template_declaration_after_export (parser,
16797 /*member_p=*/false);
16798 else
16799 cp_parser_explicit_specialization (parser);
16801 else
16802 /* Parse the dependent declaration. */
16803 cp_parser_single_declaration (parser,
16804 /*checks=*/NULL,
16805 /*member_p=*/false,
16806 /*explicit_specialization_p=*/true,
16807 /*friend_p=*/NULL);
16808 /* We're done with the specialization. */
16809 end_specialization ();
16810 /* For the erroneous case of a template with C linkage, we pushed an
16811 implicit C++ linkage scope; exit that scope now. */
16812 if (need_lang_pop)
16813 pop_lang_context ();
16814 /* We're done with this parameter list. */
16815 --parser->num_template_parameter_lists;
16818 /* Parse a type-specifier.
16820 type-specifier:
16821 simple-type-specifier
16822 class-specifier
16823 enum-specifier
16824 elaborated-type-specifier
16825 cv-qualifier
16827 GNU Extension:
16829 type-specifier:
16830 __complex__
16832 Returns a representation of the type-specifier. For a
16833 class-specifier, enum-specifier, or elaborated-type-specifier, a
16834 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16836 The parser flags FLAGS is used to control type-specifier parsing.
16838 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16839 in a decl-specifier-seq.
16841 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16842 class-specifier, enum-specifier, or elaborated-type-specifier, then
16843 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16844 if a type is declared; 2 if it is defined. Otherwise, it is set to
16845 zero.
16847 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16848 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16849 is set to FALSE. */
16851 static tree
16852 cp_parser_type_specifier (cp_parser* parser,
16853 cp_parser_flags flags,
16854 cp_decl_specifier_seq *decl_specs,
16855 bool is_declaration,
16856 int* declares_class_or_enum,
16857 bool* is_cv_qualifier)
16859 tree type_spec = NULL_TREE;
16860 cp_token *token;
16861 enum rid keyword;
16862 cp_decl_spec ds = ds_last;
16864 /* Assume this type-specifier does not declare a new type. */
16865 if (declares_class_or_enum)
16866 *declares_class_or_enum = 0;
16867 /* And that it does not specify a cv-qualifier. */
16868 if (is_cv_qualifier)
16869 *is_cv_qualifier = false;
16870 /* Peek at the next token. */
16871 token = cp_lexer_peek_token (parser->lexer);
16873 /* If we're looking at a keyword, we can use that to guide the
16874 production we choose. */
16875 keyword = token->keyword;
16876 switch (keyword)
16878 case RID_ENUM:
16879 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16880 goto elaborated_type_specifier;
16882 /* Look for the enum-specifier. */
16883 type_spec = cp_parser_enum_specifier (parser);
16884 /* If that worked, we're done. */
16885 if (type_spec)
16887 if (declares_class_or_enum)
16888 *declares_class_or_enum = 2;
16889 if (decl_specs)
16890 cp_parser_set_decl_spec_type (decl_specs,
16891 type_spec,
16892 token,
16893 /*type_definition_p=*/true);
16894 return type_spec;
16896 else
16897 goto elaborated_type_specifier;
16899 /* Any of these indicate either a class-specifier, or an
16900 elaborated-type-specifier. */
16901 case RID_CLASS:
16902 case RID_STRUCT:
16903 case RID_UNION:
16904 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16905 goto elaborated_type_specifier;
16907 /* Parse tentatively so that we can back up if we don't find a
16908 class-specifier. */
16909 cp_parser_parse_tentatively (parser);
16910 /* Look for the class-specifier. */
16911 type_spec = cp_parser_class_specifier (parser);
16912 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16913 /* If that worked, we're done. */
16914 if (cp_parser_parse_definitely (parser))
16916 if (declares_class_or_enum)
16917 *declares_class_or_enum = 2;
16918 if (decl_specs)
16919 cp_parser_set_decl_spec_type (decl_specs,
16920 type_spec,
16921 token,
16922 /*type_definition_p=*/true);
16923 return type_spec;
16926 /* Fall through. */
16927 elaborated_type_specifier:
16928 /* We're declaring (not defining) a class or enum. */
16929 if (declares_class_or_enum)
16930 *declares_class_or_enum = 1;
16932 /* Fall through. */
16933 case RID_TYPENAME:
16934 /* Look for an elaborated-type-specifier. */
16935 type_spec
16936 = (cp_parser_elaborated_type_specifier
16937 (parser,
16938 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16939 is_declaration));
16940 if (decl_specs)
16941 cp_parser_set_decl_spec_type (decl_specs,
16942 type_spec,
16943 token,
16944 /*type_definition_p=*/false);
16945 return type_spec;
16947 case RID_CONST:
16948 ds = ds_const;
16949 if (is_cv_qualifier)
16950 *is_cv_qualifier = true;
16951 break;
16953 case RID_VOLATILE:
16954 ds = ds_volatile;
16955 if (is_cv_qualifier)
16956 *is_cv_qualifier = true;
16957 break;
16959 case RID_RESTRICT:
16960 ds = ds_restrict;
16961 if (is_cv_qualifier)
16962 *is_cv_qualifier = true;
16963 break;
16965 case RID_COMPLEX:
16966 /* The `__complex__' keyword is a GNU extension. */
16967 ds = ds_complex;
16968 break;
16970 default:
16971 break;
16974 /* Handle simple keywords. */
16975 if (ds != ds_last)
16977 if (decl_specs)
16979 set_and_check_decl_spec_loc (decl_specs, ds, token);
16980 decl_specs->any_specifiers_p = true;
16982 return cp_lexer_consume_token (parser->lexer)->u.value;
16985 /* If we do not already have a type-specifier, assume we are looking
16986 at a simple-type-specifier. */
16987 type_spec = cp_parser_simple_type_specifier (parser,
16988 decl_specs,
16989 flags);
16991 /* If we didn't find a type-specifier, and a type-specifier was not
16992 optional in this context, issue an error message. */
16993 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16995 cp_parser_error (parser, "expected type specifier");
16996 return error_mark_node;
16999 return type_spec;
17002 /* Parse a simple-type-specifier.
17004 simple-type-specifier:
17005 :: [opt] nested-name-specifier [opt] type-name
17006 :: [opt] nested-name-specifier template template-id
17007 char
17008 wchar_t
17009 bool
17010 short
17012 long
17013 signed
17014 unsigned
17015 float
17016 double
17017 void
17019 C++11 Extension:
17021 simple-type-specifier:
17022 auto
17023 decltype ( expression )
17024 char16_t
17025 char32_t
17026 __underlying_type ( type-id )
17028 C++17 extension:
17030 nested-name-specifier(opt) template-name
17032 GNU Extension:
17034 simple-type-specifier:
17035 __int128
17036 __typeof__ unary-expression
17037 __typeof__ ( type-id )
17038 __typeof__ ( type-id ) { initializer-list , [opt] }
17040 Concepts Extension:
17042 simple-type-specifier:
17043 constrained-type-specifier
17045 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17046 appropriately updated. */
17048 static tree
17049 cp_parser_simple_type_specifier (cp_parser* parser,
17050 cp_decl_specifier_seq *decl_specs,
17051 cp_parser_flags flags)
17053 tree type = NULL_TREE;
17054 cp_token *token;
17055 int idx;
17057 /* Peek at the next token. */
17058 token = cp_lexer_peek_token (parser->lexer);
17060 /* If we're looking at a keyword, things are easy. */
17061 switch (token->keyword)
17063 case RID_CHAR:
17064 if (decl_specs)
17065 decl_specs->explicit_char_p = true;
17066 type = char_type_node;
17067 break;
17068 case RID_CHAR16:
17069 type = char16_type_node;
17070 break;
17071 case RID_CHAR32:
17072 type = char32_type_node;
17073 break;
17074 case RID_WCHAR:
17075 type = wchar_type_node;
17076 break;
17077 case RID_BOOL:
17078 type = boolean_type_node;
17079 break;
17080 case RID_SHORT:
17081 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17082 type = short_integer_type_node;
17083 break;
17084 case RID_INT:
17085 if (decl_specs)
17086 decl_specs->explicit_int_p = true;
17087 type = integer_type_node;
17088 break;
17089 case RID_INT_N_0:
17090 case RID_INT_N_1:
17091 case RID_INT_N_2:
17092 case RID_INT_N_3:
17093 idx = token->keyword - RID_INT_N_0;
17094 if (! int_n_enabled_p [idx])
17095 break;
17096 if (decl_specs)
17098 decl_specs->explicit_intN_p = true;
17099 decl_specs->int_n_idx = idx;
17101 type = int_n_trees [idx].signed_type;
17102 break;
17103 case RID_LONG:
17104 if (decl_specs)
17105 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17106 type = long_integer_type_node;
17107 break;
17108 case RID_SIGNED:
17109 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17110 type = integer_type_node;
17111 break;
17112 case RID_UNSIGNED:
17113 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17114 type = unsigned_type_node;
17115 break;
17116 case RID_FLOAT:
17117 type = float_type_node;
17118 break;
17119 case RID_DOUBLE:
17120 type = double_type_node;
17121 break;
17122 case RID_VOID:
17123 type = void_type_node;
17124 break;
17126 case RID_AUTO:
17127 maybe_warn_cpp0x (CPP0X_AUTO);
17128 if (parser->auto_is_implicit_function_template_parm_p)
17130 /* The 'auto' might be the placeholder return type for a function decl
17131 with trailing return type. */
17132 bool have_trailing_return_fn_decl = false;
17134 cp_parser_parse_tentatively (parser);
17135 cp_lexer_consume_token (parser->lexer);
17136 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17137 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17138 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17139 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17143 cp_lexer_consume_token (parser->lexer);
17144 cp_parser_skip_to_closing_parenthesis (parser,
17145 /*recovering*/false,
17146 /*or_comma*/false,
17147 /*consume_paren*/true);
17148 continue;
17151 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17153 have_trailing_return_fn_decl = true;
17154 break;
17157 cp_lexer_consume_token (parser->lexer);
17159 cp_parser_abort_tentative_parse (parser);
17161 if (have_trailing_return_fn_decl)
17163 type = make_auto ();
17164 break;
17167 if (cxx_dialect >= cxx14)
17169 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17170 type = TREE_TYPE (type);
17172 else
17173 type = error_mark_node;
17175 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17177 if (cxx_dialect < cxx14)
17178 error_at (token->location,
17179 "use of %<auto%> in lambda parameter declaration "
17180 "only available with "
17181 "-std=c++14 or -std=gnu++14");
17183 else if (cxx_dialect < cxx14)
17184 error_at (token->location,
17185 "use of %<auto%> in parameter declaration "
17186 "only available with "
17187 "-std=c++14 or -std=gnu++14");
17188 else if (!flag_concepts)
17189 pedwarn (token->location, 0,
17190 "use of %<auto%> in parameter declaration "
17191 "only available with -fconcepts");
17193 else
17194 type = make_auto ();
17195 break;
17197 case RID_DECLTYPE:
17198 /* Since DR 743, decltype can either be a simple-type-specifier by
17199 itself or begin a nested-name-specifier. Parsing it will replace
17200 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17201 handling below decide what to do. */
17202 cp_parser_decltype (parser);
17203 cp_lexer_set_token_position (parser->lexer, token);
17204 break;
17206 case RID_TYPEOF:
17207 /* Consume the `typeof' token. */
17208 cp_lexer_consume_token (parser->lexer);
17209 /* Parse the operand to `typeof'. */
17210 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17211 /* If it is not already a TYPE, take its type. */
17212 if (!TYPE_P (type))
17213 type = finish_typeof (type);
17215 if (decl_specs)
17216 cp_parser_set_decl_spec_type (decl_specs, type,
17217 token,
17218 /*type_definition_p=*/false);
17220 return type;
17222 case RID_UNDERLYING_TYPE:
17223 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17224 if (decl_specs)
17225 cp_parser_set_decl_spec_type (decl_specs, type,
17226 token,
17227 /*type_definition_p=*/false);
17229 return type;
17231 case RID_BASES:
17232 case RID_DIRECT_BASES:
17233 type = cp_parser_trait_expr (parser, token->keyword);
17234 if (decl_specs)
17235 cp_parser_set_decl_spec_type (decl_specs, type,
17236 token,
17237 /*type_definition_p=*/false);
17238 return type;
17239 default:
17240 break;
17243 /* If token is an already-parsed decltype not followed by ::,
17244 it's a simple-type-specifier. */
17245 if (token->type == CPP_DECLTYPE
17246 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17248 type = saved_checks_value (token->u.tree_check_value);
17249 if (decl_specs)
17251 cp_parser_set_decl_spec_type (decl_specs, type,
17252 token,
17253 /*type_definition_p=*/false);
17254 /* Remember that we are handling a decltype in order to
17255 implement the resolution of DR 1510 when the argument
17256 isn't instantiation dependent. */
17257 decl_specs->decltype_p = true;
17259 cp_lexer_consume_token (parser->lexer);
17260 return type;
17263 /* If the type-specifier was for a built-in type, we're done. */
17264 if (type)
17266 /* Record the type. */
17267 if (decl_specs
17268 && (token->keyword != RID_SIGNED
17269 && token->keyword != RID_UNSIGNED
17270 && token->keyword != RID_SHORT
17271 && token->keyword != RID_LONG))
17272 cp_parser_set_decl_spec_type (decl_specs,
17273 type,
17274 token,
17275 /*type_definition_p=*/false);
17276 if (decl_specs)
17277 decl_specs->any_specifiers_p = true;
17279 /* Consume the token. */
17280 cp_lexer_consume_token (parser->lexer);
17282 if (type == error_mark_node)
17283 return error_mark_node;
17285 /* There is no valid C++ program where a non-template type is
17286 followed by a "<". That usually indicates that the user thought
17287 that the type was a template. */
17288 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17289 token->location);
17291 return TYPE_NAME (type);
17294 /* The type-specifier must be a user-defined type. */
17295 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17297 bool qualified_p;
17298 bool global_p;
17300 /* Don't gobble tokens or issue error messages if this is an
17301 optional type-specifier. */
17302 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17303 cp_parser_parse_tentatively (parser);
17305 token = cp_lexer_peek_token (parser->lexer);
17307 /* Look for the optional `::' operator. */
17308 global_p
17309 = (cp_parser_global_scope_opt (parser,
17310 /*current_scope_valid_p=*/false)
17311 != NULL_TREE);
17312 /* Look for the nested-name specifier. */
17313 qualified_p
17314 = (cp_parser_nested_name_specifier_opt (parser,
17315 /*typename_keyword_p=*/false,
17316 /*check_dependency_p=*/true,
17317 /*type_p=*/false,
17318 /*is_declaration=*/false)
17319 != NULL_TREE);
17320 /* If we have seen a nested-name-specifier, and the next token
17321 is `template', then we are using the template-id production. */
17322 if (parser->scope
17323 && cp_parser_optional_template_keyword (parser))
17325 /* Look for the template-id. */
17326 type = cp_parser_template_id (parser,
17327 /*template_keyword_p=*/true,
17328 /*check_dependency_p=*/true,
17329 none_type,
17330 /*is_declaration=*/false);
17331 /* If the template-id did not name a type, we are out of
17332 luck. */
17333 if (TREE_CODE (type) != TYPE_DECL)
17335 cp_parser_error (parser, "expected template-id for type");
17336 type = NULL_TREE;
17339 /* Otherwise, look for a type-name. */
17340 else
17341 type = cp_parser_type_name (parser);
17342 /* Keep track of all name-lookups performed in class scopes. */
17343 if (type
17344 && !global_p
17345 && !qualified_p
17346 && TREE_CODE (type) == TYPE_DECL
17347 && identifier_p (DECL_NAME (type)))
17348 maybe_note_name_used_in_class (DECL_NAME (type), type);
17349 /* If it didn't work out, we don't have a TYPE. */
17350 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17351 && !cp_parser_parse_definitely (parser))
17352 type = NULL_TREE;
17353 if (!type && cxx_dialect >= cxx17)
17355 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17356 cp_parser_parse_tentatively (parser);
17358 cp_parser_global_scope_opt (parser,
17359 /*current_scope_valid_p=*/false);
17360 cp_parser_nested_name_specifier_opt (parser,
17361 /*typename_keyword_p=*/false,
17362 /*check_dependency_p=*/true,
17363 /*type_p=*/false,
17364 /*is_declaration=*/false);
17365 tree name = cp_parser_identifier (parser);
17366 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17367 && parser->scope != error_mark_node)
17369 tree tmpl = cp_parser_lookup_name (parser, name,
17370 none_type,
17371 /*is_template=*/false,
17372 /*is_namespace=*/false,
17373 /*check_dependency=*/true,
17374 /*ambiguous_decls=*/NULL,
17375 token->location);
17376 if (tmpl && tmpl != error_mark_node
17377 && (DECL_CLASS_TEMPLATE_P (tmpl)
17378 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17379 type = make_template_placeholder (tmpl);
17380 else
17382 type = error_mark_node;
17383 if (!cp_parser_simulate_error (parser))
17384 cp_parser_name_lookup_error (parser, name, tmpl,
17385 NLE_TYPE, token->location);
17388 else
17389 type = error_mark_node;
17391 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17392 && !cp_parser_parse_definitely (parser))
17393 type = NULL_TREE;
17395 if (type && decl_specs)
17396 cp_parser_set_decl_spec_type (decl_specs, type,
17397 token,
17398 /*type_definition_p=*/false);
17401 /* If we didn't get a type-name, issue an error message. */
17402 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17404 cp_parser_error (parser, "expected type-name");
17405 return error_mark_node;
17408 if (type && type != error_mark_node)
17410 /* See if TYPE is an Objective-C type, and if so, parse and
17411 accept any protocol references following it. Do this before
17412 the cp_parser_check_for_invalid_template_id() call, because
17413 Objective-C types can be followed by '<...>' which would
17414 enclose protocol names rather than template arguments, and so
17415 everything is fine. */
17416 if (c_dialect_objc () && !parser->scope
17417 && (objc_is_id (type) || objc_is_class_name (type)))
17419 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17420 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17422 /* Clobber the "unqualified" type previously entered into
17423 DECL_SPECS with the new, improved protocol-qualified version. */
17424 if (decl_specs)
17425 decl_specs->type = qual_type;
17427 return qual_type;
17430 /* There is no valid C++ program where a non-template type is
17431 followed by a "<". That usually indicates that the user
17432 thought that the type was a template. */
17433 cp_parser_check_for_invalid_template_id (parser, type,
17434 none_type,
17435 token->location);
17438 return type;
17441 /* Parse a type-name.
17443 type-name:
17444 class-name
17445 enum-name
17446 typedef-name
17447 simple-template-id [in c++0x]
17449 enum-name:
17450 identifier
17452 typedef-name:
17453 identifier
17455 Concepts:
17457 type-name:
17458 concept-name
17459 partial-concept-id
17461 concept-name:
17462 identifier
17464 Returns a TYPE_DECL for the type. */
17466 static tree
17467 cp_parser_type_name (cp_parser* parser)
17469 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17472 /* See above. */
17473 static tree
17474 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17476 tree type_decl;
17478 /* We can't know yet whether it is a class-name or not. */
17479 cp_parser_parse_tentatively (parser);
17480 /* Try a class-name. */
17481 type_decl = cp_parser_class_name (parser,
17482 typename_keyword_p,
17483 /*template_keyword_p=*/false,
17484 none_type,
17485 /*check_dependency_p=*/true,
17486 /*class_head_p=*/false,
17487 /*is_declaration=*/false);
17488 /* If it's not a class-name, keep looking. */
17489 if (!cp_parser_parse_definitely (parser))
17491 if (cxx_dialect < cxx11)
17492 /* It must be a typedef-name or an enum-name. */
17493 return cp_parser_nonclass_name (parser);
17495 cp_parser_parse_tentatively (parser);
17496 /* It is either a simple-template-id representing an
17497 instantiation of an alias template... */
17498 type_decl = cp_parser_template_id (parser,
17499 /*template_keyword_p=*/false,
17500 /*check_dependency_p=*/true,
17501 none_type,
17502 /*is_declaration=*/false);
17503 /* Note that this must be an instantiation of an alias template
17504 because [temp.names]/6 says:
17506 A template-id that names an alias template specialization
17507 is a type-name.
17509 Whereas [temp.names]/7 says:
17511 A simple-template-id that names a class template
17512 specialization is a class-name.
17514 With concepts, this could also be a partial-concept-id that
17515 declares a non-type template parameter. */
17516 if (type_decl != NULL_TREE
17517 && TREE_CODE (type_decl) == TYPE_DECL
17518 && TYPE_DECL_ALIAS_P (type_decl))
17519 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17520 else if (is_constrained_parameter (type_decl))
17521 /* Don't do anything. */ ;
17522 else
17523 cp_parser_simulate_error (parser);
17525 if (!cp_parser_parse_definitely (parser))
17526 /* ... Or a typedef-name or an enum-name. */
17527 return cp_parser_nonclass_name (parser);
17530 return type_decl;
17533 /* Check if DECL and ARGS can form a constrained-type-specifier.
17534 If ARGS is non-null, we try to form a concept check of the
17535 form DECL<?, ARGS> where ? is a wildcard that matches any
17536 kind of template argument. If ARGS is NULL, then we try to
17537 form a concept check of the form DECL<?>. */
17539 static tree
17540 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17541 tree decl, tree args)
17543 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17545 /* If we a constrained-type-specifier cannot be deduced. */
17546 if (parser->prevent_constrained_type_specifiers)
17547 return NULL_TREE;
17549 /* A constrained type specifier can only be found in an
17550 overload set or as a reference to a template declaration.
17552 FIXME: This might be masking a bug. It's possible that
17553 that the deduction below is causing template specializations
17554 to be formed with the wildcard as an argument. */
17555 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17556 return NULL_TREE;
17558 /* Try to build a call expression that evaluates the
17559 concept. This can fail if the overload set refers
17560 only to non-templates. */
17561 tree placeholder = build_nt (WILDCARD_DECL);
17562 tree check = build_concept_check (decl, placeholder, args);
17563 if (check == error_mark_node)
17564 return NULL_TREE;
17566 /* Deduce the checked constraint and the prototype parameter.
17568 FIXME: In certain cases, failure to deduce should be a
17569 diagnosable error. */
17570 tree conc;
17571 tree proto;
17572 if (!deduce_constrained_parameter (check, conc, proto))
17573 return NULL_TREE;
17575 /* In template parameter scope, this results in a constrained
17576 parameter. Return a descriptor of that parm. */
17577 if (processing_template_parmlist)
17578 return build_constrained_parameter (conc, proto, args);
17580 /* In a parameter-declaration-clause, constrained-type
17581 specifiers result in invented template parameters. */
17582 if (parser->auto_is_implicit_function_template_parm_p)
17584 tree x = build_constrained_parameter (conc, proto, args);
17585 return synthesize_implicit_template_parm (parser, x);
17587 else
17589 /* Otherwise, we're in a context where the constrained
17590 type name is deduced and the constraint applies
17591 after deduction. */
17592 return make_constrained_auto (conc, args);
17595 return NULL_TREE;
17598 /* If DECL refers to a concept, return a TYPE_DECL representing
17599 the result of using the constrained type specifier in the
17600 current context. DECL refers to a concept if
17602 - it is an overload set containing a function concept taking a single
17603 type argument, or
17605 - it is a variable concept taking a single type argument. */
17607 static tree
17608 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17610 if (flag_concepts
17611 && (TREE_CODE (decl) == OVERLOAD
17612 || BASELINK_P (decl)
17613 || variable_concept_p (decl)))
17614 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17615 else
17616 return NULL_TREE;
17619 /* Check if DECL and ARGS form a partial-concept-id. If so,
17620 assign ID to the resulting constrained placeholder.
17622 Returns true if the partial-concept-id designates a placeholder
17623 and false otherwise. Note that *id is set to NULL_TREE in
17624 this case. */
17626 static tree
17627 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17629 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17632 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17633 or a concept-name.
17635 enum-name:
17636 identifier
17638 typedef-name:
17639 identifier
17641 concept-name:
17642 identifier
17644 Returns a TYPE_DECL for the type. */
17646 static tree
17647 cp_parser_nonclass_name (cp_parser* parser)
17649 tree type_decl;
17650 tree identifier;
17652 cp_token *token = cp_lexer_peek_token (parser->lexer);
17653 identifier = cp_parser_identifier (parser);
17654 if (identifier == error_mark_node)
17655 return error_mark_node;
17657 /* Look up the type-name. */
17658 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17660 type_decl = strip_using_decl (type_decl);
17662 /* If we found an overload set, then it may refer to a concept-name. */
17663 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17664 type_decl = decl;
17666 if (TREE_CODE (type_decl) != TYPE_DECL
17667 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17669 /* See if this is an Objective-C type. */
17670 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17671 tree type = objc_get_protocol_qualified_type (identifier, protos);
17672 if (type)
17673 type_decl = TYPE_NAME (type);
17676 /* Issue an error if we did not find a type-name. */
17677 if (TREE_CODE (type_decl) != TYPE_DECL
17678 /* In Objective-C, we have the complication that class names are
17679 normally type names and start declarations (eg, the
17680 "NSObject" in "NSObject *object;"), but can be used in an
17681 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17682 is an expression. So, a classname followed by a dot is not a
17683 valid type-name. */
17684 || (objc_is_class_name (TREE_TYPE (type_decl))
17685 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17687 if (!cp_parser_simulate_error (parser))
17688 cp_parser_name_lookup_error (parser, identifier, type_decl,
17689 NLE_TYPE, token->location);
17690 return error_mark_node;
17692 /* Remember that the name was used in the definition of the
17693 current class so that we can check later to see if the
17694 meaning would have been different after the class was
17695 entirely defined. */
17696 else if (type_decl != error_mark_node
17697 && !parser->scope)
17698 maybe_note_name_used_in_class (identifier, type_decl);
17700 return type_decl;
17703 /* Parse an elaborated-type-specifier. Note that the grammar given
17704 here incorporates the resolution to DR68.
17706 elaborated-type-specifier:
17707 class-key :: [opt] nested-name-specifier [opt] identifier
17708 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17709 enum-key :: [opt] nested-name-specifier [opt] identifier
17710 typename :: [opt] nested-name-specifier identifier
17711 typename :: [opt] nested-name-specifier template [opt]
17712 template-id
17714 GNU extension:
17716 elaborated-type-specifier:
17717 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17718 class-key attributes :: [opt] nested-name-specifier [opt]
17719 template [opt] template-id
17720 enum attributes :: [opt] nested-name-specifier [opt] identifier
17722 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17723 declared `friend'. If IS_DECLARATION is TRUE, then this
17724 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17725 something is being declared.
17727 Returns the TYPE specified. */
17729 static tree
17730 cp_parser_elaborated_type_specifier (cp_parser* parser,
17731 bool is_friend,
17732 bool is_declaration)
17734 enum tag_types tag_type;
17735 tree identifier;
17736 tree type = NULL_TREE;
17737 tree attributes = NULL_TREE;
17738 tree globalscope;
17739 cp_token *token = NULL;
17741 /* See if we're looking at the `enum' keyword. */
17742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17744 /* Consume the `enum' token. */
17745 cp_lexer_consume_token (parser->lexer);
17746 /* Remember that it's an enumeration type. */
17747 tag_type = enum_type;
17748 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17749 enums) is used here. */
17750 cp_token *token = cp_lexer_peek_token (parser->lexer);
17751 if (cp_parser_is_keyword (token, RID_CLASS)
17752 || cp_parser_is_keyword (token, RID_STRUCT))
17754 gcc_rich_location richloc (token->location);
17755 richloc.add_range (input_location, false);
17756 richloc.add_fixit_remove ();
17757 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17758 "a scoped enum must not use the %qD keyword",
17759 token->u.value);
17760 /* Consume the `struct' or `class' and parse it anyway. */
17761 cp_lexer_consume_token (parser->lexer);
17763 /* Parse the attributes. */
17764 attributes = cp_parser_attributes_opt (parser);
17766 /* Or, it might be `typename'. */
17767 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17768 RID_TYPENAME))
17770 /* Consume the `typename' token. */
17771 cp_lexer_consume_token (parser->lexer);
17772 /* Remember that it's a `typename' type. */
17773 tag_type = typename_type;
17775 /* Otherwise it must be a class-key. */
17776 else
17778 tag_type = cp_parser_class_key (parser);
17779 if (tag_type == none_type)
17780 return error_mark_node;
17781 /* Parse the attributes. */
17782 attributes = cp_parser_attributes_opt (parser);
17785 /* Look for the `::' operator. */
17786 globalscope = cp_parser_global_scope_opt (parser,
17787 /*current_scope_valid_p=*/false);
17788 /* Look for the nested-name-specifier. */
17789 tree nested_name_specifier;
17790 if (tag_type == typename_type && !globalscope)
17792 nested_name_specifier
17793 = cp_parser_nested_name_specifier (parser,
17794 /*typename_keyword_p=*/true,
17795 /*check_dependency_p=*/true,
17796 /*type_p=*/true,
17797 is_declaration);
17798 if (!nested_name_specifier)
17799 return error_mark_node;
17801 else
17802 /* Even though `typename' is not present, the proposed resolution
17803 to Core Issue 180 says that in `class A<T>::B', `B' should be
17804 considered a type-name, even if `A<T>' is dependent. */
17805 nested_name_specifier
17806 = cp_parser_nested_name_specifier_opt (parser,
17807 /*typename_keyword_p=*/true,
17808 /*check_dependency_p=*/true,
17809 /*type_p=*/true,
17810 is_declaration);
17811 /* For everything but enumeration types, consider a template-id.
17812 For an enumeration type, consider only a plain identifier. */
17813 if (tag_type != enum_type)
17815 bool template_p = false;
17816 tree decl;
17818 /* Allow the `template' keyword. */
17819 template_p = cp_parser_optional_template_keyword (parser);
17820 /* If we didn't see `template', we don't know if there's a
17821 template-id or not. */
17822 if (!template_p)
17823 cp_parser_parse_tentatively (parser);
17824 /* Parse the template-id. */
17825 token = cp_lexer_peek_token (parser->lexer);
17826 decl = cp_parser_template_id (parser, template_p,
17827 /*check_dependency_p=*/true,
17828 tag_type,
17829 is_declaration);
17830 /* If we didn't find a template-id, look for an ordinary
17831 identifier. */
17832 if (!template_p && !cp_parser_parse_definitely (parser))
17834 /* We can get here when cp_parser_template_id, called by
17835 cp_parser_class_name with tag_type == none_type, succeeds
17836 and caches a BASELINK. Then, when called again here,
17837 instead of failing and returning an error_mark_node
17838 returns it (see template/typename17.C in C++11).
17839 ??? Could we diagnose this earlier? */
17840 else if (tag_type == typename_type && BASELINK_P (decl))
17842 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17843 type = error_mark_node;
17845 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17846 in effect, then we must assume that, upon instantiation, the
17847 template will correspond to a class. */
17848 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17849 && tag_type == typename_type)
17850 type = make_typename_type (parser->scope, decl,
17851 typename_type,
17852 /*complain=*/tf_error);
17853 /* If the `typename' keyword is in effect and DECL is not a type
17854 decl, then type is non existent. */
17855 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17857 else if (TREE_CODE (decl) == TYPE_DECL)
17859 type = check_elaborated_type_specifier (tag_type, decl,
17860 /*allow_template_p=*/true);
17862 /* If the next token is a semicolon, this must be a specialization,
17863 instantiation, or friend declaration. Check the scope while we
17864 still know whether or not we had a nested-name-specifier. */
17865 if (type != error_mark_node
17866 && !nested_name_specifier && !is_friend
17867 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17868 check_unqualified_spec_or_inst (type, token->location);
17870 else if (decl == error_mark_node)
17871 type = error_mark_node;
17874 if (!type)
17876 token = cp_lexer_peek_token (parser->lexer);
17877 identifier = cp_parser_identifier (parser);
17879 if (identifier == error_mark_node)
17881 parser->scope = NULL_TREE;
17882 return error_mark_node;
17885 /* For a `typename', we needn't call xref_tag. */
17886 if (tag_type == typename_type
17887 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17888 return cp_parser_make_typename_type (parser, identifier,
17889 token->location);
17891 /* Template parameter lists apply only if we are not within a
17892 function parameter list. */
17893 bool template_parm_lists_apply
17894 = parser->num_template_parameter_lists;
17895 if (template_parm_lists_apply)
17896 for (cp_binding_level *s = current_binding_level;
17897 s && s->kind != sk_template_parms;
17898 s = s->level_chain)
17899 if (s->kind == sk_function_parms)
17900 template_parm_lists_apply = false;
17902 /* Look up a qualified name in the usual way. */
17903 if (parser->scope)
17905 tree decl;
17906 tree ambiguous_decls;
17908 decl = cp_parser_lookup_name (parser, identifier,
17909 tag_type,
17910 /*is_template=*/false,
17911 /*is_namespace=*/false,
17912 /*check_dependency=*/true,
17913 &ambiguous_decls,
17914 token->location);
17916 /* If the lookup was ambiguous, an error will already have been
17917 issued. */
17918 if (ambiguous_decls)
17919 return error_mark_node;
17921 /* If we are parsing friend declaration, DECL may be a
17922 TEMPLATE_DECL tree node here. However, we need to check
17923 whether this TEMPLATE_DECL results in valid code. Consider
17924 the following example:
17926 namespace N {
17927 template <class T> class C {};
17929 class X {
17930 template <class T> friend class N::C; // #1, valid code
17932 template <class T> class Y {
17933 friend class N::C; // #2, invalid code
17936 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17937 name lookup of `N::C'. We see that friend declaration must
17938 be template for the code to be valid. Note that
17939 processing_template_decl does not work here since it is
17940 always 1 for the above two cases. */
17942 decl = (cp_parser_maybe_treat_template_as_class
17943 (decl, /*tag_name_p=*/is_friend
17944 && template_parm_lists_apply));
17946 if (TREE_CODE (decl) != TYPE_DECL)
17948 cp_parser_diagnose_invalid_type_name (parser,
17949 identifier,
17950 token->location);
17951 return error_mark_node;
17954 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17956 bool allow_template = (template_parm_lists_apply
17957 || DECL_SELF_REFERENCE_P (decl));
17958 type = check_elaborated_type_specifier (tag_type, decl,
17959 allow_template);
17961 if (type == error_mark_node)
17962 return error_mark_node;
17965 /* Forward declarations of nested types, such as
17967 class C1::C2;
17968 class C1::C2::C3;
17970 are invalid unless all components preceding the final '::'
17971 are complete. If all enclosing types are complete, these
17972 declarations become merely pointless.
17974 Invalid forward declarations of nested types are errors
17975 caught elsewhere in parsing. Those that are pointless arrive
17976 here. */
17978 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17979 && !is_friend && !processing_explicit_instantiation)
17980 warning (0, "declaration %qD does not declare anything", decl);
17982 type = TREE_TYPE (decl);
17984 else
17986 /* An elaborated-type-specifier sometimes introduces a new type and
17987 sometimes names an existing type. Normally, the rule is that it
17988 introduces a new type only if there is not an existing type of
17989 the same name already in scope. For example, given:
17991 struct S {};
17992 void f() { struct S s; }
17994 the `struct S' in the body of `f' is the same `struct S' as in
17995 the global scope; the existing definition is used. However, if
17996 there were no global declaration, this would introduce a new
17997 local class named `S'.
17999 An exception to this rule applies to the following code:
18001 namespace N { struct S; }
18003 Here, the elaborated-type-specifier names a new type
18004 unconditionally; even if there is already an `S' in the
18005 containing scope this declaration names a new type.
18006 This exception only applies if the elaborated-type-specifier
18007 forms the complete declaration:
18009 [class.name]
18011 A declaration consisting solely of `class-key identifier ;' is
18012 either a redeclaration of the name in the current scope or a
18013 forward declaration of the identifier as a class name. It
18014 introduces the name into the current scope.
18016 We are in this situation precisely when the next token is a `;'.
18018 An exception to the exception is that a `friend' declaration does
18019 *not* name a new type; i.e., given:
18021 struct S { friend struct T; };
18023 `T' is not a new type in the scope of `S'.
18025 Also, `new struct S' or `sizeof (struct S)' never results in the
18026 definition of a new type; a new type can only be declared in a
18027 declaration context. */
18029 tag_scope ts;
18030 bool template_p;
18032 if (is_friend)
18033 /* Friends have special name lookup rules. */
18034 ts = ts_within_enclosing_non_class;
18035 else if (is_declaration
18036 && cp_lexer_next_token_is (parser->lexer,
18037 CPP_SEMICOLON))
18038 /* This is a `class-key identifier ;' */
18039 ts = ts_current;
18040 else
18041 ts = ts_global;
18043 template_p =
18044 (template_parm_lists_apply
18045 && (cp_parser_next_token_starts_class_definition_p (parser)
18046 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18047 /* An unqualified name was used to reference this type, so
18048 there were no qualifying templates. */
18049 if (template_parm_lists_apply
18050 && !cp_parser_check_template_parameters (parser,
18051 /*num_templates=*/0,
18052 /*template_id*/false,
18053 token->location,
18054 /*declarator=*/NULL))
18055 return error_mark_node;
18056 type = xref_tag (tag_type, identifier, ts, template_p);
18060 if (type == error_mark_node)
18061 return error_mark_node;
18063 /* Allow attributes on forward declarations of classes. */
18064 if (attributes)
18066 if (TREE_CODE (type) == TYPENAME_TYPE)
18067 warning (OPT_Wattributes,
18068 "attributes ignored on uninstantiated type");
18069 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18070 && ! processing_explicit_instantiation)
18071 warning (OPT_Wattributes,
18072 "attributes ignored on template instantiation");
18073 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18074 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18075 else
18076 warning (OPT_Wattributes,
18077 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18080 if (tag_type != enum_type)
18082 /* Indicate whether this class was declared as a `class' or as a
18083 `struct'. */
18084 if (CLASS_TYPE_P (type))
18085 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18086 cp_parser_check_class_key (tag_type, type);
18089 /* A "<" cannot follow an elaborated type specifier. If that
18090 happens, the user was probably trying to form a template-id. */
18091 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18092 token->location);
18094 return type;
18097 /* Parse an enum-specifier.
18099 enum-specifier:
18100 enum-head { enumerator-list [opt] }
18101 enum-head { enumerator-list , } [C++0x]
18103 enum-head:
18104 enum-key identifier [opt] enum-base [opt]
18105 enum-key nested-name-specifier identifier enum-base [opt]
18107 enum-key:
18108 enum
18109 enum class [C++0x]
18110 enum struct [C++0x]
18112 enum-base: [C++0x]
18113 : type-specifier-seq
18115 opaque-enum-specifier:
18116 enum-key identifier enum-base [opt] ;
18118 GNU Extensions:
18119 enum-key attributes[opt] identifier [opt] enum-base [opt]
18120 { enumerator-list [opt] }attributes[opt]
18121 enum-key attributes[opt] identifier [opt] enum-base [opt]
18122 { enumerator-list, }attributes[opt] [C++0x]
18124 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18125 if the token stream isn't an enum-specifier after all. */
18127 static tree
18128 cp_parser_enum_specifier (cp_parser* parser)
18130 tree identifier;
18131 tree type = NULL_TREE;
18132 tree prev_scope;
18133 tree nested_name_specifier = NULL_TREE;
18134 tree attributes;
18135 bool scoped_enum_p = false;
18136 bool has_underlying_type = false;
18137 bool nested_being_defined = false;
18138 bool new_value_list = false;
18139 bool is_new_type = false;
18140 bool is_unnamed = false;
18141 tree underlying_type = NULL_TREE;
18142 cp_token *type_start_token = NULL;
18143 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18145 parser->colon_corrects_to_scope_p = false;
18147 /* Parse tentatively so that we can back up if we don't find a
18148 enum-specifier. */
18149 cp_parser_parse_tentatively (parser);
18151 /* Caller guarantees that the current token is 'enum', an identifier
18152 possibly follows, and the token after that is an opening brace.
18153 If we don't have an identifier, fabricate an anonymous name for
18154 the enumeration being defined. */
18155 cp_lexer_consume_token (parser->lexer);
18157 /* Parse the "class" or "struct", which indicates a scoped
18158 enumeration type in C++0x. */
18159 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18160 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18162 if (cxx_dialect < cxx11)
18163 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18165 /* Consume the `struct' or `class' token. */
18166 cp_lexer_consume_token (parser->lexer);
18168 scoped_enum_p = true;
18171 attributes = cp_parser_attributes_opt (parser);
18173 /* Clear the qualification. */
18174 parser->scope = NULL_TREE;
18175 parser->qualifying_scope = NULL_TREE;
18176 parser->object_scope = NULL_TREE;
18178 /* Figure out in what scope the declaration is being placed. */
18179 prev_scope = current_scope ();
18181 type_start_token = cp_lexer_peek_token (parser->lexer);
18183 push_deferring_access_checks (dk_no_check);
18184 nested_name_specifier
18185 = cp_parser_nested_name_specifier_opt (parser,
18186 /*typename_keyword_p=*/true,
18187 /*check_dependency_p=*/false,
18188 /*type_p=*/false,
18189 /*is_declaration=*/false);
18191 if (nested_name_specifier)
18193 tree name;
18195 identifier = cp_parser_identifier (parser);
18196 name = cp_parser_lookup_name (parser, identifier,
18197 enum_type,
18198 /*is_template=*/false,
18199 /*is_namespace=*/false,
18200 /*check_dependency=*/true,
18201 /*ambiguous_decls=*/NULL,
18202 input_location);
18203 if (name && name != error_mark_node)
18205 type = TREE_TYPE (name);
18206 if (TREE_CODE (type) == TYPENAME_TYPE)
18208 /* Are template enums allowed in ISO? */
18209 if (template_parm_scope_p ())
18210 pedwarn (type_start_token->location, OPT_Wpedantic,
18211 "%qD is an enumeration template", name);
18212 /* ignore a typename reference, for it will be solved by name
18213 in start_enum. */
18214 type = NULL_TREE;
18217 else if (nested_name_specifier == error_mark_node)
18218 /* We already issued an error. */;
18219 else
18221 error_at (type_start_token->location,
18222 "%qD does not name an enumeration in %qT",
18223 identifier, nested_name_specifier);
18224 nested_name_specifier = error_mark_node;
18227 else
18229 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18230 identifier = cp_parser_identifier (parser);
18231 else
18233 identifier = make_anon_name ();
18234 is_unnamed = true;
18235 if (scoped_enum_p)
18236 error_at (type_start_token->location,
18237 "unnamed scoped enum is not allowed");
18240 pop_deferring_access_checks ();
18242 /* Check for the `:' that denotes a specified underlying type in C++0x.
18243 Note that a ':' could also indicate a bitfield width, however. */
18244 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18246 cp_decl_specifier_seq type_specifiers;
18248 /* Consume the `:'. */
18249 cp_lexer_consume_token (parser->lexer);
18251 /* Parse the type-specifier-seq. */
18252 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18253 /*is_trailing_return=*/false,
18254 &type_specifiers);
18256 /* At this point this is surely not elaborated type specifier. */
18257 if (!cp_parser_parse_definitely (parser))
18258 return NULL_TREE;
18260 if (cxx_dialect < cxx11)
18261 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18263 has_underlying_type = true;
18265 /* If that didn't work, stop. */
18266 if (type_specifiers.type != error_mark_node)
18268 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18269 /*initialized=*/0, NULL);
18270 if (underlying_type == error_mark_node
18271 || check_for_bare_parameter_packs (underlying_type))
18272 underlying_type = NULL_TREE;
18276 /* Look for the `{' but don't consume it yet. */
18277 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18279 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18281 cp_parser_error (parser, "expected %<{%>");
18282 if (has_underlying_type)
18284 type = NULL_TREE;
18285 goto out;
18288 /* An opaque-enum-specifier must have a ';' here. */
18289 if ((scoped_enum_p || underlying_type)
18290 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18292 cp_parser_error (parser, "expected %<;%> or %<{%>");
18293 if (has_underlying_type)
18295 type = NULL_TREE;
18296 goto out;
18301 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18302 return NULL_TREE;
18304 if (nested_name_specifier)
18306 if (CLASS_TYPE_P (nested_name_specifier))
18308 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18309 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18310 push_scope (nested_name_specifier);
18312 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18314 push_nested_namespace (nested_name_specifier);
18318 /* Issue an error message if type-definitions are forbidden here. */
18319 if (!cp_parser_check_type_definition (parser))
18320 type = error_mark_node;
18321 else
18322 /* Create the new type. We do this before consuming the opening
18323 brace so the enum will be recorded as being on the line of its
18324 tag (or the 'enum' keyword, if there is no tag). */
18325 type = start_enum (identifier, type, underlying_type,
18326 attributes, scoped_enum_p, &is_new_type);
18328 /* If the next token is not '{' it is an opaque-enum-specifier or an
18329 elaborated-type-specifier. */
18330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18332 timevar_push (TV_PARSE_ENUM);
18333 if (nested_name_specifier
18334 && nested_name_specifier != error_mark_node)
18336 /* The following catches invalid code such as:
18337 enum class S<int>::E { A, B, C }; */
18338 if (!processing_specialization
18339 && CLASS_TYPE_P (nested_name_specifier)
18340 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18341 error_at (type_start_token->location, "cannot add an enumerator "
18342 "list to a template instantiation");
18344 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18346 error_at (type_start_token->location,
18347 "%<%T::%E%> has not been declared",
18348 TYPE_CONTEXT (nested_name_specifier),
18349 nested_name_specifier);
18350 type = error_mark_node;
18352 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18353 && !CLASS_TYPE_P (nested_name_specifier))
18355 error_at (type_start_token->location, "nested name specifier "
18356 "%qT for enum declaration does not name a class "
18357 "or namespace", nested_name_specifier);
18358 type = error_mark_node;
18360 /* If that scope does not contain the scope in which the
18361 class was originally declared, the program is invalid. */
18362 else if (prev_scope && !is_ancestor (prev_scope,
18363 nested_name_specifier))
18365 if (at_namespace_scope_p ())
18366 error_at (type_start_token->location,
18367 "declaration of %qD in namespace %qD which does not "
18368 "enclose %qD",
18369 type, prev_scope, nested_name_specifier);
18370 else
18371 error_at (type_start_token->location,
18372 "declaration of %qD in %qD which does not "
18373 "enclose %qD",
18374 type, prev_scope, nested_name_specifier);
18375 type = error_mark_node;
18377 /* If that scope is the scope where the declaration is being placed
18378 the program is invalid. */
18379 else if (CLASS_TYPE_P (nested_name_specifier)
18380 && CLASS_TYPE_P (prev_scope)
18381 && same_type_p (nested_name_specifier, prev_scope))
18383 permerror (type_start_token->location,
18384 "extra qualification not allowed");
18385 nested_name_specifier = NULL_TREE;
18389 if (scoped_enum_p)
18390 begin_scope (sk_scoped_enum, type);
18392 /* Consume the opening brace. */
18393 matching_braces braces;
18394 braces.consume_open (parser);
18396 if (type == error_mark_node)
18397 ; /* Nothing to add */
18398 else if (OPAQUE_ENUM_P (type)
18399 || (cxx_dialect > cxx98 && processing_specialization))
18401 new_value_list = true;
18402 SET_OPAQUE_ENUM_P (type, false);
18403 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18405 else
18407 error_at (type_start_token->location,
18408 "multiple definition of %q#T", type);
18409 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18410 "previous definition here");
18411 type = error_mark_node;
18414 if (type == error_mark_node)
18415 cp_parser_skip_to_end_of_block_or_statement (parser);
18416 /* If the next token is not '}', then there are some enumerators. */
18417 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18419 if (is_unnamed && !scoped_enum_p)
18420 pedwarn (type_start_token->location, OPT_Wpedantic,
18421 "ISO C++ forbids empty unnamed enum");
18423 else
18424 cp_parser_enumerator_list (parser, type);
18426 /* Consume the final '}'. */
18427 braces.require_close (parser);
18429 if (scoped_enum_p)
18430 finish_scope ();
18431 timevar_pop (TV_PARSE_ENUM);
18433 else
18435 /* If a ';' follows, then it is an opaque-enum-specifier
18436 and additional restrictions apply. */
18437 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18439 if (is_unnamed)
18440 error_at (type_start_token->location,
18441 "opaque-enum-specifier without name");
18442 else if (nested_name_specifier)
18443 error_at (type_start_token->location,
18444 "opaque-enum-specifier must use a simple identifier");
18448 /* Look for trailing attributes to apply to this enumeration, and
18449 apply them if appropriate. */
18450 if (cp_parser_allow_gnu_extensions_p (parser))
18452 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18453 cplus_decl_attributes (&type,
18454 trailing_attr,
18455 (int) ATTR_FLAG_TYPE_IN_PLACE);
18458 /* Finish up the enumeration. */
18459 if (type != error_mark_node)
18461 if (new_value_list)
18462 finish_enum_value_list (type);
18463 if (is_new_type)
18464 finish_enum (type);
18467 if (nested_name_specifier)
18469 if (CLASS_TYPE_P (nested_name_specifier))
18471 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18472 pop_scope (nested_name_specifier);
18474 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18476 pop_nested_namespace (nested_name_specifier);
18479 out:
18480 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18481 return type;
18484 /* Parse an enumerator-list. The enumerators all have the indicated
18485 TYPE.
18487 enumerator-list:
18488 enumerator-definition
18489 enumerator-list , enumerator-definition */
18491 static void
18492 cp_parser_enumerator_list (cp_parser* parser, tree type)
18494 while (true)
18496 /* Parse an enumerator-definition. */
18497 cp_parser_enumerator_definition (parser, type);
18499 /* If the next token is not a ',', we've reached the end of
18500 the list. */
18501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18502 break;
18503 /* Otherwise, consume the `,' and keep going. */
18504 cp_lexer_consume_token (parser->lexer);
18505 /* If the next token is a `}', there is a trailing comma. */
18506 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18508 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18509 pedwarn (input_location, OPT_Wpedantic,
18510 "comma at end of enumerator list");
18511 break;
18516 /* Parse an enumerator-definition. The enumerator has the indicated
18517 TYPE.
18519 enumerator-definition:
18520 enumerator
18521 enumerator = constant-expression
18523 enumerator:
18524 identifier
18526 GNU Extensions:
18528 enumerator-definition:
18529 enumerator attributes [opt]
18530 enumerator attributes [opt] = constant-expression */
18532 static void
18533 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18535 tree identifier;
18536 tree value;
18537 location_t loc;
18539 /* Save the input location because we are interested in the location
18540 of the identifier and not the location of the explicit value. */
18541 loc = cp_lexer_peek_token (parser->lexer)->location;
18543 /* Look for the identifier. */
18544 identifier = cp_parser_identifier (parser);
18545 if (identifier == error_mark_node)
18546 return;
18548 /* Parse any specified attributes. */
18549 tree attrs = cp_parser_attributes_opt (parser);
18551 /* If the next token is an '=', then there is an explicit value. */
18552 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18554 /* Consume the `=' token. */
18555 cp_lexer_consume_token (parser->lexer);
18556 /* Parse the value. */
18557 value = cp_parser_constant_expression (parser);
18559 else
18560 value = NULL_TREE;
18562 /* If we are processing a template, make sure the initializer of the
18563 enumerator doesn't contain any bare template parameter pack. */
18564 if (check_for_bare_parameter_packs (value))
18565 value = error_mark_node;
18567 /* Create the enumerator. */
18568 build_enumerator (identifier, value, type, attrs, loc);
18571 /* Parse a namespace-name.
18573 namespace-name:
18574 original-namespace-name
18575 namespace-alias
18577 Returns the NAMESPACE_DECL for the namespace. */
18579 static tree
18580 cp_parser_namespace_name (cp_parser* parser)
18582 tree identifier;
18583 tree namespace_decl;
18585 cp_token *token = cp_lexer_peek_token (parser->lexer);
18587 /* Get the name of the namespace. */
18588 identifier = cp_parser_identifier (parser);
18589 if (identifier == error_mark_node)
18590 return error_mark_node;
18592 /* Look up the identifier in the currently active scope. Look only
18593 for namespaces, due to:
18595 [basic.lookup.udir]
18597 When looking up a namespace-name in a using-directive or alias
18598 definition, only namespace names are considered.
18600 And:
18602 [basic.lookup.qual]
18604 During the lookup of a name preceding the :: scope resolution
18605 operator, object, function, and enumerator names are ignored.
18607 (Note that cp_parser_qualifying_entity only calls this
18608 function if the token after the name is the scope resolution
18609 operator.) */
18610 namespace_decl = cp_parser_lookup_name (parser, identifier,
18611 none_type,
18612 /*is_template=*/false,
18613 /*is_namespace=*/true,
18614 /*check_dependency=*/true,
18615 /*ambiguous_decls=*/NULL,
18616 token->location);
18617 /* If it's not a namespace, issue an error. */
18618 if (namespace_decl == error_mark_node
18619 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18621 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18623 auto_diagnostic_group d;
18624 error_at (token->location, "%qD is not a namespace-name", identifier);
18625 if (namespace_decl == error_mark_node
18626 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18627 suggest_alternative_in_explicit_scope (token->location, identifier,
18628 parser->scope);
18630 cp_parser_error (parser, "expected namespace-name");
18631 namespace_decl = error_mark_node;
18634 return namespace_decl;
18637 /* Parse a namespace-definition.
18639 namespace-definition:
18640 named-namespace-definition
18641 unnamed-namespace-definition
18643 named-namespace-definition:
18644 original-namespace-definition
18645 extension-namespace-definition
18647 original-namespace-definition:
18648 namespace identifier { namespace-body }
18650 extension-namespace-definition:
18651 namespace original-namespace-name { namespace-body }
18653 unnamed-namespace-definition:
18654 namespace { namespace-body } */
18656 static void
18657 cp_parser_namespace_definition (cp_parser* parser)
18659 tree identifier;
18660 int nested_definition_count = 0;
18662 cp_ensure_no_omp_declare_simd (parser);
18663 cp_ensure_no_oacc_routine (parser);
18665 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18667 if (is_inline)
18669 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18670 cp_lexer_consume_token (parser->lexer);
18673 /* Look for the `namespace' keyword. */
18674 cp_token* token
18675 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18677 /* Parse any specified attributes before the identifier. */
18678 tree attribs = cp_parser_attributes_opt (parser);
18680 for (;;)
18682 identifier = NULL_TREE;
18684 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18686 identifier = cp_parser_identifier (parser);
18688 if (cp_next_tokens_can_be_std_attribute_p (parser))
18689 pedwarn (input_location, OPT_Wpedantic,
18690 "standard attributes on namespaces must precede "
18691 "the namespace name");
18693 /* Parse any attributes specified after the identifier. */
18694 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18698 break;
18700 if (!nested_definition_count && cxx_dialect < cxx17)
18701 pedwarn (input_location, OPT_Wpedantic,
18702 "nested namespace definitions only available with "
18703 "-std=c++17 or -std=gnu++17");
18705 /* Nested namespace names can create new namespaces (unlike
18706 other qualified-ids). */
18707 if (int count = identifier ? push_namespace (identifier) : 0)
18708 nested_definition_count += count;
18709 else
18710 cp_parser_error (parser, "nested namespace name required");
18711 cp_lexer_consume_token (parser->lexer);
18714 if (nested_definition_count && !identifier)
18715 cp_parser_error (parser, "namespace name required");
18717 if (nested_definition_count && attribs)
18718 error_at (token->location,
18719 "a nested namespace definition cannot have attributes");
18720 if (nested_definition_count && is_inline)
18721 error_at (token->location,
18722 "a nested namespace definition cannot be inline");
18724 /* Start the namespace. */
18725 nested_definition_count += push_namespace (identifier, is_inline);
18727 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18729 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18731 /* Look for the `{' to validate starting the namespace. */
18732 matching_braces braces;
18733 if (braces.require_open (parser))
18735 /* Parse the body of the namespace. */
18736 cp_parser_namespace_body (parser);
18738 /* Look for the final `}'. */
18739 braces.require_close (parser);
18742 if (has_visibility)
18743 pop_visibility (1);
18745 /* Pop the nested namespace definitions. */
18746 while (nested_definition_count--)
18747 pop_namespace ();
18750 /* Parse a namespace-body.
18752 namespace-body:
18753 declaration-seq [opt] */
18755 static void
18756 cp_parser_namespace_body (cp_parser* parser)
18758 cp_parser_declaration_seq_opt (parser);
18761 /* Parse a namespace-alias-definition.
18763 namespace-alias-definition:
18764 namespace identifier = qualified-namespace-specifier ; */
18766 static void
18767 cp_parser_namespace_alias_definition (cp_parser* parser)
18769 tree identifier;
18770 tree namespace_specifier;
18772 cp_token *token = cp_lexer_peek_token (parser->lexer);
18774 /* Look for the `namespace' keyword. */
18775 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18776 /* Look for the identifier. */
18777 identifier = cp_parser_identifier (parser);
18778 if (identifier == error_mark_node)
18779 return;
18780 /* Look for the `=' token. */
18781 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18782 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18784 error_at (token->location, "%<namespace%> definition is not allowed here");
18785 /* Skip the definition. */
18786 cp_lexer_consume_token (parser->lexer);
18787 if (cp_parser_skip_to_closing_brace (parser))
18788 cp_lexer_consume_token (parser->lexer);
18789 return;
18791 cp_parser_require (parser, CPP_EQ, RT_EQ);
18792 /* Look for the qualified-namespace-specifier. */
18793 namespace_specifier
18794 = cp_parser_qualified_namespace_specifier (parser);
18795 /* Look for the `;' token. */
18796 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18798 /* Register the alias in the symbol table. */
18799 do_namespace_alias (identifier, namespace_specifier);
18802 /* Parse a qualified-namespace-specifier.
18804 qualified-namespace-specifier:
18805 :: [opt] nested-name-specifier [opt] namespace-name
18807 Returns a NAMESPACE_DECL corresponding to the specified
18808 namespace. */
18810 static tree
18811 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18813 /* Look for the optional `::'. */
18814 cp_parser_global_scope_opt (parser,
18815 /*current_scope_valid_p=*/false);
18817 /* Look for the optional nested-name-specifier. */
18818 cp_parser_nested_name_specifier_opt (parser,
18819 /*typename_keyword_p=*/false,
18820 /*check_dependency_p=*/true,
18821 /*type_p=*/false,
18822 /*is_declaration=*/true);
18824 return cp_parser_namespace_name (parser);
18827 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18828 access declaration.
18830 using-declaration:
18831 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18832 using :: unqualified-id ;
18834 access-declaration:
18835 qualified-id ;
18839 static bool
18840 cp_parser_using_declaration (cp_parser* parser,
18841 bool access_declaration_p)
18843 cp_token *token;
18844 bool typename_p = false;
18845 bool global_scope_p;
18846 tree decl;
18847 tree identifier;
18848 tree qscope;
18849 int oldcount = errorcount;
18850 cp_token *diag_token = NULL;
18852 if (access_declaration_p)
18854 diag_token = cp_lexer_peek_token (parser->lexer);
18855 cp_parser_parse_tentatively (parser);
18857 else
18859 /* Look for the `using' keyword. */
18860 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18862 again:
18863 /* Peek at the next token. */
18864 token = cp_lexer_peek_token (parser->lexer);
18865 /* See if it's `typename'. */
18866 if (token->keyword == RID_TYPENAME)
18868 /* Remember that we've seen it. */
18869 typename_p = true;
18870 /* Consume the `typename' token. */
18871 cp_lexer_consume_token (parser->lexer);
18875 /* Look for the optional global scope qualification. */
18876 global_scope_p
18877 = (cp_parser_global_scope_opt (parser,
18878 /*current_scope_valid_p=*/false)
18879 != NULL_TREE);
18881 /* If we saw `typename', or didn't see `::', then there must be a
18882 nested-name-specifier present. */
18883 if (typename_p || !global_scope_p)
18885 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18886 /*check_dependency_p=*/true,
18887 /*type_p=*/false,
18888 /*is_declaration=*/true);
18889 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18891 cp_parser_skip_to_end_of_block_or_statement (parser);
18892 return false;
18895 /* Otherwise, we could be in either of the two productions. In that
18896 case, treat the nested-name-specifier as optional. */
18897 else
18898 qscope = cp_parser_nested_name_specifier_opt (parser,
18899 /*typename_keyword_p=*/false,
18900 /*check_dependency_p=*/true,
18901 /*type_p=*/false,
18902 /*is_declaration=*/true);
18903 if (!qscope)
18904 qscope = global_namespace;
18905 else if (UNSCOPED_ENUM_P (qscope))
18906 qscope = CP_TYPE_CONTEXT (qscope);
18908 if (access_declaration_p && cp_parser_error_occurred (parser))
18909 /* Something has already gone wrong; there's no need to parse
18910 further. Since an error has occurred, the return value of
18911 cp_parser_parse_definitely will be false, as required. */
18912 return cp_parser_parse_definitely (parser);
18914 token = cp_lexer_peek_token (parser->lexer);
18915 /* Parse the unqualified-id. */
18916 identifier = cp_parser_unqualified_id (parser,
18917 /*template_keyword_p=*/false,
18918 /*check_dependency_p=*/true,
18919 /*declarator_p=*/true,
18920 /*optional_p=*/false);
18922 if (access_declaration_p)
18924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18925 cp_parser_simulate_error (parser);
18926 if (!cp_parser_parse_definitely (parser))
18927 return false;
18929 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18931 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18932 if (cxx_dialect < cxx17
18933 && !in_system_header_at (ell->location))
18934 pedwarn (ell->location, 0,
18935 "pack expansion in using-declaration only available "
18936 "with -std=c++17 or -std=gnu++17");
18937 qscope = make_pack_expansion (qscope);
18940 /* The function we call to handle a using-declaration is different
18941 depending on what scope we are in. */
18942 if (qscope == error_mark_node || identifier == error_mark_node)
18944 else if (!identifier_p (identifier)
18945 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18946 /* [namespace.udecl]
18948 A using declaration shall not name a template-id. */
18949 error_at (token->location,
18950 "a template-id may not appear in a using-declaration");
18951 else
18953 if (at_class_scope_p ())
18955 /* Create the USING_DECL. */
18956 decl = do_class_using_decl (qscope, identifier);
18958 if (decl && typename_p)
18959 USING_DECL_TYPENAME_P (decl) = 1;
18961 if (check_for_bare_parameter_packs (decl))
18963 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18964 return false;
18966 else
18967 /* Add it to the list of members in this class. */
18968 finish_member_declaration (decl);
18970 else
18972 decl = cp_parser_lookup_name_simple (parser,
18973 identifier,
18974 token->location);
18975 if (decl == error_mark_node)
18976 cp_parser_name_lookup_error (parser, identifier,
18977 decl, NLE_NULL,
18978 token->location);
18979 else if (check_for_bare_parameter_packs (decl))
18981 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18982 return false;
18984 else if (!at_namespace_scope_p ())
18985 finish_local_using_decl (decl, qscope, identifier);
18986 else
18987 finish_namespace_using_decl (decl, qscope, identifier);
18991 if (!access_declaration_p
18992 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18994 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18995 if (cxx_dialect < cxx17)
18996 pedwarn (comma->location, 0,
18997 "comma-separated list in using-declaration only available "
18998 "with -std=c++17 or -std=gnu++17");
18999 goto again;
19002 /* Look for the final `;'. */
19003 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19005 if (access_declaration_p && errorcount == oldcount)
19006 warning_at (diag_token->location, OPT_Wdeprecated,
19007 "access declarations are deprecated "
19008 "in favour of using-declarations; "
19009 "suggestion: add the %<using%> keyword");
19011 return true;
19014 /* Parse an alias-declaration.
19016 alias-declaration:
19017 using identifier attribute-specifier-seq [opt] = type-id */
19019 static tree
19020 cp_parser_alias_declaration (cp_parser* parser)
19022 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19023 location_t id_location;
19024 cp_declarator *declarator;
19025 cp_decl_specifier_seq decl_specs;
19026 bool member_p;
19027 const char *saved_message = NULL;
19029 /* Look for the `using' keyword. */
19030 cp_token *using_token
19031 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19032 if (using_token == NULL)
19033 return error_mark_node;
19035 id_location = cp_lexer_peek_token (parser->lexer)->location;
19036 id = cp_parser_identifier (parser);
19037 if (id == error_mark_node)
19038 return error_mark_node;
19040 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19041 attributes = cp_parser_attributes_opt (parser);
19042 if (attributes == error_mark_node)
19043 return error_mark_node;
19045 cp_parser_require (parser, CPP_EQ, RT_EQ);
19047 if (cp_parser_error_occurred (parser))
19048 return error_mark_node;
19050 cp_parser_commit_to_tentative_parse (parser);
19052 /* Now we are going to parse the type-id of the declaration. */
19055 [dcl.type]/3 says:
19057 "A type-specifier-seq shall not define a class or enumeration
19058 unless it appears in the type-id of an alias-declaration (7.1.3) that
19059 is not the declaration of a template-declaration."
19061 In other words, if we currently are in an alias template, the
19062 type-id should not define a type.
19064 So let's set parser->type_definition_forbidden_message in that
19065 case; cp_parser_check_type_definition (called by
19066 cp_parser_class_specifier) will then emit an error if a type is
19067 defined in the type-id. */
19068 if (parser->num_template_parameter_lists)
19070 saved_message = parser->type_definition_forbidden_message;
19071 parser->type_definition_forbidden_message =
19072 G_("types may not be defined in alias template declarations");
19075 type = cp_parser_type_id (parser);
19077 /* Restore the error message if need be. */
19078 if (parser->num_template_parameter_lists)
19079 parser->type_definition_forbidden_message = saved_message;
19081 if (type == error_mark_node
19082 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19084 cp_parser_skip_to_end_of_block_or_statement (parser);
19085 return error_mark_node;
19088 /* A typedef-name can also be introduced by an alias-declaration. The
19089 identifier following the using keyword becomes a typedef-name. It has
19090 the same semantics as if it were introduced by the typedef
19091 specifier. In particular, it does not define a new type and it shall
19092 not appear in the type-id. */
19094 clear_decl_specs (&decl_specs);
19095 decl_specs.type = type;
19096 if (attributes != NULL_TREE)
19098 decl_specs.attributes = attributes;
19099 set_and_check_decl_spec_loc (&decl_specs,
19100 ds_attribute,
19101 attrs_token);
19103 set_and_check_decl_spec_loc (&decl_specs,
19104 ds_typedef,
19105 using_token);
19106 set_and_check_decl_spec_loc (&decl_specs,
19107 ds_alias,
19108 using_token);
19110 if (parser->num_template_parameter_lists
19111 && !cp_parser_check_template_parameters (parser,
19112 /*num_templates=*/0,
19113 /*template_id*/false,
19114 id_location,
19115 /*declarator=*/NULL))
19116 return error_mark_node;
19118 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19119 declarator->id_loc = id_location;
19121 member_p = at_class_scope_p ();
19122 if (member_p)
19123 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19124 NULL_TREE, attributes);
19125 else
19126 decl = start_decl (declarator, &decl_specs, 0,
19127 attributes, NULL_TREE, &pushed_scope);
19128 if (decl == error_mark_node)
19129 return decl;
19131 // Attach constraints to the alias declaration.
19132 if (flag_concepts && current_template_parms)
19134 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19135 tree constr = build_constraints (reqs, NULL_TREE);
19136 set_constraints (decl, constr);
19139 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19141 if (pushed_scope)
19142 pop_scope (pushed_scope);
19144 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19145 added into the symbol table; otherwise, return the TYPE_DECL. */
19146 if (DECL_LANG_SPECIFIC (decl)
19147 && DECL_TEMPLATE_INFO (decl)
19148 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19150 decl = DECL_TI_TEMPLATE (decl);
19151 if (member_p)
19152 check_member_template (decl);
19155 return decl;
19158 /* Parse a using-directive.
19160 using-directive:
19161 using namespace :: [opt] nested-name-specifier [opt]
19162 namespace-name ; */
19164 static void
19165 cp_parser_using_directive (cp_parser* parser)
19167 tree namespace_decl;
19168 tree attribs;
19170 /* Look for the `using' keyword. */
19171 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19172 /* And the `namespace' keyword. */
19173 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19174 /* Look for the optional `::' operator. */
19175 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19176 /* And the optional nested-name-specifier. */
19177 cp_parser_nested_name_specifier_opt (parser,
19178 /*typename_keyword_p=*/false,
19179 /*check_dependency_p=*/true,
19180 /*type_p=*/false,
19181 /*is_declaration=*/true);
19182 /* Get the namespace being used. */
19183 namespace_decl = cp_parser_namespace_name (parser);
19184 /* And any specified attributes. */
19185 attribs = cp_parser_attributes_opt (parser);
19187 /* Update the symbol table. */
19188 if (namespace_bindings_p ())
19189 finish_namespace_using_directive (namespace_decl, attribs);
19190 else
19191 finish_local_using_directive (namespace_decl, attribs);
19193 /* Look for the final `;'. */
19194 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19197 /* Parse an asm-definition.
19199 asm-definition:
19200 asm ( string-literal ) ;
19202 GNU Extension:
19204 asm-definition:
19205 asm volatile [opt] ( string-literal ) ;
19206 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19207 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19208 : asm-operand-list [opt] ) ;
19209 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19210 : asm-operand-list [opt]
19211 : asm-clobber-list [opt] ) ;
19212 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19213 : asm-clobber-list [opt]
19214 : asm-goto-list ) ; */
19216 static void
19217 cp_parser_asm_definition (cp_parser* parser)
19219 tree string;
19220 tree outputs = NULL_TREE;
19221 tree inputs = NULL_TREE;
19222 tree clobbers = NULL_TREE;
19223 tree labels = NULL_TREE;
19224 tree asm_stmt;
19225 bool volatile_p = false;
19226 bool extended_p = false;
19227 bool invalid_inputs_p = false;
19228 bool invalid_outputs_p = false;
19229 bool goto_p = false;
19230 required_token missing = RT_NONE;
19232 /* Look for the `asm' keyword. */
19233 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19235 if (parser->in_function_body
19236 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19238 error ("%<asm%> in %<constexpr%> function");
19239 cp_function_chain->invalid_constexpr = true;
19242 /* See if the next token is `volatile'. */
19243 if (cp_parser_allow_gnu_extensions_p (parser)
19244 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19246 /* Remember that we saw the `volatile' keyword. */
19247 volatile_p = true;
19248 /* Consume the token. */
19249 cp_lexer_consume_token (parser->lexer);
19251 if (cp_parser_allow_gnu_extensions_p (parser)
19252 && parser->in_function_body
19253 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19255 /* Remember that we saw the `goto' keyword. */
19256 goto_p = true;
19257 /* Consume the token. */
19258 cp_lexer_consume_token (parser->lexer);
19260 /* Look for the opening `('. */
19261 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19262 return;
19263 /* Look for the string. */
19264 string = cp_parser_string_literal (parser, false, false);
19265 if (string == error_mark_node)
19267 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19268 /*consume_paren=*/true);
19269 return;
19272 /* If we're allowing GNU extensions, check for the extended assembly
19273 syntax. Unfortunately, the `:' tokens need not be separated by
19274 a space in C, and so, for compatibility, we tolerate that here
19275 too. Doing that means that we have to treat the `::' operator as
19276 two `:' tokens. */
19277 if (cp_parser_allow_gnu_extensions_p (parser)
19278 && parser->in_function_body
19279 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19280 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19282 bool inputs_p = false;
19283 bool clobbers_p = false;
19284 bool labels_p = false;
19286 /* The extended syntax was used. */
19287 extended_p = true;
19289 /* Look for outputs. */
19290 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19292 /* Consume the `:'. */
19293 cp_lexer_consume_token (parser->lexer);
19294 /* Parse the output-operands. */
19295 if (cp_lexer_next_token_is_not (parser->lexer,
19296 CPP_COLON)
19297 && cp_lexer_next_token_is_not (parser->lexer,
19298 CPP_SCOPE)
19299 && cp_lexer_next_token_is_not (parser->lexer,
19300 CPP_CLOSE_PAREN)
19301 && !goto_p)
19303 outputs = cp_parser_asm_operand_list (parser);
19304 if (outputs == error_mark_node)
19305 invalid_outputs_p = true;
19308 /* If the next token is `::', there are no outputs, and the
19309 next token is the beginning of the inputs. */
19310 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19311 /* The inputs are coming next. */
19312 inputs_p = true;
19314 /* Look for inputs. */
19315 if (inputs_p
19316 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19318 /* Consume the `:' or `::'. */
19319 cp_lexer_consume_token (parser->lexer);
19320 /* Parse the output-operands. */
19321 if (cp_lexer_next_token_is_not (parser->lexer,
19322 CPP_COLON)
19323 && cp_lexer_next_token_is_not (parser->lexer,
19324 CPP_SCOPE)
19325 && cp_lexer_next_token_is_not (parser->lexer,
19326 CPP_CLOSE_PAREN))
19328 inputs = cp_parser_asm_operand_list (parser);
19329 if (inputs == error_mark_node)
19330 invalid_inputs_p = true;
19333 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19334 /* The clobbers are coming next. */
19335 clobbers_p = true;
19337 /* Look for clobbers. */
19338 if (clobbers_p
19339 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19341 clobbers_p = true;
19342 /* Consume the `:' or `::'. */
19343 cp_lexer_consume_token (parser->lexer);
19344 /* Parse the clobbers. */
19345 if (cp_lexer_next_token_is_not (parser->lexer,
19346 CPP_COLON)
19347 && cp_lexer_next_token_is_not (parser->lexer,
19348 CPP_CLOSE_PAREN))
19349 clobbers = cp_parser_asm_clobber_list (parser);
19351 else if (goto_p
19352 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19353 /* The labels are coming next. */
19354 labels_p = true;
19356 /* Look for labels. */
19357 if (labels_p
19358 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19360 labels_p = true;
19361 /* Consume the `:' or `::'. */
19362 cp_lexer_consume_token (parser->lexer);
19363 /* Parse the labels. */
19364 labels = cp_parser_asm_label_list (parser);
19367 if (goto_p && !labels_p)
19368 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19370 else if (goto_p)
19371 missing = RT_COLON_SCOPE;
19373 /* Look for the closing `)'. */
19374 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19375 missing ? missing : RT_CLOSE_PAREN))
19376 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19377 /*consume_paren=*/true);
19378 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19380 if (!invalid_inputs_p && !invalid_outputs_p)
19382 /* Create the ASM_EXPR. */
19383 if (parser->in_function_body)
19385 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19386 inputs, clobbers, labels);
19387 /* If the extended syntax was not used, mark the ASM_EXPR. */
19388 if (!extended_p)
19390 tree temp = asm_stmt;
19391 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19392 temp = TREE_OPERAND (temp, 0);
19394 ASM_INPUT_P (temp) = 1;
19397 else
19398 symtab->finalize_toplevel_asm (string);
19402 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19403 type that comes from the decl-specifier-seq. */
19405 static tree
19406 strip_declarator_types (tree type, cp_declarator *declarator)
19408 for (cp_declarator *d = declarator; d;)
19409 switch (d->kind)
19411 case cdk_id:
19412 case cdk_decomp:
19413 case cdk_error:
19414 d = NULL;
19415 break;
19417 default:
19418 if (TYPE_PTRMEMFUNC_P (type))
19419 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19420 type = TREE_TYPE (type);
19421 d = d->declarator;
19422 break;
19425 return type;
19428 /* Declarators [gram.dcl.decl] */
19430 /* Parse an init-declarator.
19432 init-declarator:
19433 declarator initializer [opt]
19435 GNU Extension:
19437 init-declarator:
19438 declarator asm-specification [opt] attributes [opt] initializer [opt]
19440 function-definition:
19441 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19442 function-body
19443 decl-specifier-seq [opt] declarator function-try-block
19445 GNU Extension:
19447 function-definition:
19448 __extension__ function-definition
19450 TM Extension:
19452 function-definition:
19453 decl-specifier-seq [opt] declarator function-transaction-block
19455 The DECL_SPECIFIERS apply to this declarator. Returns a
19456 representation of the entity declared. If MEMBER_P is TRUE, then
19457 this declarator appears in a class scope. The new DECL created by
19458 this declarator is returned.
19460 The CHECKS are access checks that should be performed once we know
19461 what entity is being declared (and, therefore, what classes have
19462 befriended it).
19464 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19465 for a function-definition here as well. If the declarator is a
19466 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19467 be TRUE upon return. By that point, the function-definition will
19468 have been completely parsed.
19470 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19471 is FALSE.
19473 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19474 parsed declaration if it is an uninitialized single declarator not followed
19475 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19476 if present, will not be consumed. If returned, this declarator will be
19477 created with SD_INITIALIZED but will not call cp_finish_decl.
19479 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19480 and there is an initializer, the pointed location_t is set to the
19481 location of the '=' or `(', or '{' in C++11 token introducing the
19482 initializer. */
19484 static tree
19485 cp_parser_init_declarator (cp_parser* parser,
19486 cp_decl_specifier_seq *decl_specifiers,
19487 vec<deferred_access_check, va_gc> *checks,
19488 bool function_definition_allowed_p,
19489 bool member_p,
19490 int declares_class_or_enum,
19491 bool* function_definition_p,
19492 tree* maybe_range_for_decl,
19493 location_t* init_loc,
19494 tree* auto_result)
19496 cp_token *token = NULL, *asm_spec_start_token = NULL,
19497 *attributes_start_token = NULL;
19498 cp_declarator *declarator;
19499 tree prefix_attributes;
19500 tree attributes = NULL;
19501 tree asm_specification;
19502 tree initializer;
19503 tree decl = NULL_TREE;
19504 tree scope;
19505 int is_initialized;
19506 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19507 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19508 "(...)". */
19509 enum cpp_ttype initialization_kind;
19510 bool is_direct_init = false;
19511 bool is_non_constant_init;
19512 int ctor_dtor_or_conv_p;
19513 bool friend_p = cp_parser_friend_p (decl_specifiers);
19514 tree pushed_scope = NULL_TREE;
19515 bool range_for_decl_p = false;
19516 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19517 location_t tmp_init_loc = UNKNOWN_LOCATION;
19519 /* Gather the attributes that were provided with the
19520 decl-specifiers. */
19521 prefix_attributes = decl_specifiers->attributes;
19523 /* Assume that this is not the declarator for a function
19524 definition. */
19525 if (function_definition_p)
19526 *function_definition_p = false;
19528 /* Default arguments are only permitted for function parameters. */
19529 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19530 parser->default_arg_ok_p = false;
19532 /* Defer access checks while parsing the declarator; we cannot know
19533 what names are accessible until we know what is being
19534 declared. */
19535 resume_deferring_access_checks ();
19537 token = cp_lexer_peek_token (parser->lexer);
19539 /* Parse the declarator. */
19540 declarator
19541 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19542 &ctor_dtor_or_conv_p,
19543 /*parenthesized_p=*/NULL,
19544 member_p, friend_p);
19545 /* Gather up the deferred checks. */
19546 stop_deferring_access_checks ();
19548 parser->default_arg_ok_p = saved_default_arg_ok_p;
19550 /* If the DECLARATOR was erroneous, there's no need to go
19551 further. */
19552 if (declarator == cp_error_declarator)
19553 return error_mark_node;
19555 /* Check that the number of template-parameter-lists is OK. */
19556 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19557 token->location))
19558 return error_mark_node;
19560 if (declares_class_or_enum & 2)
19561 cp_parser_check_for_definition_in_return_type (declarator,
19562 decl_specifiers->type,
19563 decl_specifiers->locations[ds_type_spec]);
19565 /* Figure out what scope the entity declared by the DECLARATOR is
19566 located in. `grokdeclarator' sometimes changes the scope, so
19567 we compute it now. */
19568 scope = get_scope_of_declarator (declarator);
19570 /* Perform any lookups in the declared type which were thought to be
19571 dependent, but are not in the scope of the declarator. */
19572 decl_specifiers->type
19573 = maybe_update_decl_type (decl_specifiers->type, scope);
19575 /* If we're allowing GNU extensions, look for an
19576 asm-specification. */
19577 if (cp_parser_allow_gnu_extensions_p (parser))
19579 /* Look for an asm-specification. */
19580 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19581 asm_specification = cp_parser_asm_specification_opt (parser);
19583 else
19584 asm_specification = NULL_TREE;
19586 /* Look for attributes. */
19587 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19588 attributes = cp_parser_attributes_opt (parser);
19590 /* Peek at the next token. */
19591 token = cp_lexer_peek_token (parser->lexer);
19593 bool bogus_implicit_tmpl = false;
19595 if (function_declarator_p (declarator))
19597 /* Handle C++17 deduction guides. */
19598 if (!decl_specifiers->type
19599 && ctor_dtor_or_conv_p <= 0
19600 && cxx_dialect >= cxx17)
19602 cp_declarator *id = get_id_declarator (declarator);
19603 tree name = id->u.id.unqualified_name;
19604 parser->scope = id->u.id.qualifying_scope;
19605 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19606 if (tmpl
19607 && (DECL_CLASS_TEMPLATE_P (tmpl)
19608 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19610 id->u.id.unqualified_name = dguide_name (tmpl);
19611 id->u.id.sfk = sfk_deduction_guide;
19612 ctor_dtor_or_conv_p = 1;
19616 /* Check to see if the token indicates the start of a
19617 function-definition. */
19618 if (cp_parser_token_starts_function_definition_p (token))
19620 if (!function_definition_allowed_p)
19622 /* If a function-definition should not appear here, issue an
19623 error message. */
19624 cp_parser_error (parser,
19625 "a function-definition is not allowed here");
19626 return error_mark_node;
19629 location_t func_brace_location
19630 = cp_lexer_peek_token (parser->lexer)->location;
19632 /* Neither attributes nor an asm-specification are allowed
19633 on a function-definition. */
19634 if (asm_specification)
19635 error_at (asm_spec_start_token->location,
19636 "an asm-specification is not allowed "
19637 "on a function-definition");
19638 if (attributes)
19639 error_at (attributes_start_token->location,
19640 "attributes are not allowed "
19641 "on a function-definition");
19642 /* This is a function-definition. */
19643 *function_definition_p = true;
19645 /* Parse the function definition. */
19646 if (member_p)
19647 decl = cp_parser_save_member_function_body (parser,
19648 decl_specifiers,
19649 declarator,
19650 prefix_attributes);
19651 else
19652 decl =
19653 (cp_parser_function_definition_from_specifiers_and_declarator
19654 (parser, decl_specifiers, prefix_attributes, declarator));
19656 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19658 /* This is where the prologue starts... */
19659 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19660 = func_brace_location;
19663 return decl;
19666 else if (parser->fully_implicit_function_template_p)
19668 /* A non-template declaration involving a function parameter list
19669 containing an implicit template parameter will be made into a
19670 template. If the resulting declaration is not going to be an
19671 actual function then finish the template scope here to prevent it.
19672 An error message will be issued once we have a decl to talk about.
19674 FIXME probably we should do type deduction rather than create an
19675 implicit template, but the standard currently doesn't allow it. */
19676 bogus_implicit_tmpl = true;
19677 finish_fully_implicit_template (parser, NULL_TREE);
19680 /* [dcl.dcl]
19682 Only in function declarations for constructors, destructors, type
19683 conversions, and deduction guides can the decl-specifier-seq be omitted.
19685 We explicitly postpone this check past the point where we handle
19686 function-definitions because we tolerate function-definitions
19687 that are missing their return types in some modes. */
19688 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19690 cp_parser_error (parser,
19691 "expected constructor, destructor, or type conversion");
19692 return error_mark_node;
19695 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19696 if (token->type == CPP_EQ
19697 || token->type == CPP_OPEN_PAREN
19698 || token->type == CPP_OPEN_BRACE)
19700 is_initialized = SD_INITIALIZED;
19701 initialization_kind = token->type;
19702 if (maybe_range_for_decl)
19703 *maybe_range_for_decl = error_mark_node;
19704 tmp_init_loc = token->location;
19705 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19706 *init_loc = tmp_init_loc;
19708 if (token->type == CPP_EQ
19709 && function_declarator_p (declarator))
19711 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19712 if (t2->keyword == RID_DEFAULT)
19713 is_initialized = SD_DEFAULTED;
19714 else if (t2->keyword == RID_DELETE)
19715 is_initialized = SD_DELETED;
19718 else
19720 /* If the init-declarator isn't initialized and isn't followed by a
19721 `,' or `;', it's not a valid init-declarator. */
19722 if (token->type != CPP_COMMA
19723 && token->type != CPP_SEMICOLON)
19725 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19726 range_for_decl_p = true;
19727 else
19729 if (!maybe_range_for_decl)
19730 cp_parser_error (parser, "expected initializer");
19731 return error_mark_node;
19734 is_initialized = SD_UNINITIALIZED;
19735 initialization_kind = CPP_EOF;
19738 /* Because start_decl has side-effects, we should only call it if we
19739 know we're going ahead. By this point, we know that we cannot
19740 possibly be looking at any other construct. */
19741 cp_parser_commit_to_tentative_parse (parser);
19743 /* Enter the newly declared entry in the symbol table. If we're
19744 processing a declaration in a class-specifier, we wait until
19745 after processing the initializer. */
19746 if (!member_p)
19748 if (parser->in_unbraced_linkage_specification_p)
19749 decl_specifiers->storage_class = sc_extern;
19750 decl = start_decl (declarator, decl_specifiers,
19751 range_for_decl_p? SD_INITIALIZED : is_initialized,
19752 attributes, prefix_attributes, &pushed_scope);
19753 cp_finalize_omp_declare_simd (parser, decl);
19754 cp_finalize_oacc_routine (parser, decl, false);
19755 /* Adjust location of decl if declarator->id_loc is more appropriate:
19756 set, and decl wasn't merged with another decl, in which case its
19757 location would be different from input_location, and more accurate. */
19758 if (DECL_P (decl)
19759 && declarator->id_loc != UNKNOWN_LOCATION
19760 && DECL_SOURCE_LOCATION (decl) == input_location)
19761 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19763 else if (scope)
19764 /* Enter the SCOPE. That way unqualified names appearing in the
19765 initializer will be looked up in SCOPE. */
19766 pushed_scope = push_scope (scope);
19768 /* Perform deferred access control checks, now that we know in which
19769 SCOPE the declared entity resides. */
19770 if (!member_p && decl)
19772 tree saved_current_function_decl = NULL_TREE;
19774 /* If the entity being declared is a function, pretend that we
19775 are in its scope. If it is a `friend', it may have access to
19776 things that would not otherwise be accessible. */
19777 if (TREE_CODE (decl) == FUNCTION_DECL)
19779 saved_current_function_decl = current_function_decl;
19780 current_function_decl = decl;
19783 /* Perform access checks for template parameters. */
19784 cp_parser_perform_template_parameter_access_checks (checks);
19786 /* Perform the access control checks for the declarator and the
19787 decl-specifiers. */
19788 perform_deferred_access_checks (tf_warning_or_error);
19790 /* Restore the saved value. */
19791 if (TREE_CODE (decl) == FUNCTION_DECL)
19792 current_function_decl = saved_current_function_decl;
19795 /* Parse the initializer. */
19796 initializer = NULL_TREE;
19797 is_direct_init = false;
19798 is_non_constant_init = true;
19799 if (is_initialized)
19801 if (function_declarator_p (declarator))
19803 if (initialization_kind == CPP_EQ)
19804 initializer = cp_parser_pure_specifier (parser);
19805 else
19807 /* If the declaration was erroneous, we don't really
19808 know what the user intended, so just silently
19809 consume the initializer. */
19810 if (decl != error_mark_node)
19811 error_at (tmp_init_loc, "initializer provided for function");
19812 cp_parser_skip_to_closing_parenthesis (parser,
19813 /*recovering=*/true,
19814 /*or_comma=*/false,
19815 /*consume_paren=*/true);
19818 else
19820 /* We want to record the extra mangling scope for in-class
19821 initializers of class members and initializers of static data
19822 member templates. The former involves deferring
19823 parsing of the initializer until end of class as with default
19824 arguments. So right here we only handle the latter. */
19825 if (!member_p && processing_template_decl && decl != error_mark_node)
19826 start_lambda_scope (decl);
19827 initializer = cp_parser_initializer (parser,
19828 &is_direct_init,
19829 &is_non_constant_init);
19830 if (!member_p && processing_template_decl && decl != error_mark_node)
19831 finish_lambda_scope ();
19832 if (initializer == error_mark_node)
19833 cp_parser_skip_to_end_of_statement (parser);
19837 /* The old parser allows attributes to appear after a parenthesized
19838 initializer. Mark Mitchell proposed removing this functionality
19839 on the GCC mailing lists on 2002-08-13. This parser accepts the
19840 attributes -- but ignores them. Made a permerror in GCC 8. */
19841 if (cp_parser_allow_gnu_extensions_p (parser)
19842 && initialization_kind == CPP_OPEN_PAREN
19843 && cp_parser_attributes_opt (parser)
19844 && permerror (input_location,
19845 "attributes after parenthesized initializer ignored"))
19847 static bool hint;
19848 if (flag_permissive && !hint)
19850 hint = true;
19851 inform (input_location,
19852 "this flexibility is deprecated and will be removed");
19856 /* And now complain about a non-function implicit template. */
19857 if (bogus_implicit_tmpl && decl != error_mark_node)
19858 error_at (DECL_SOURCE_LOCATION (decl),
19859 "non-function %qD declared as implicit template", decl);
19861 /* For an in-class declaration, use `grokfield' to create the
19862 declaration. */
19863 if (member_p)
19865 if (pushed_scope)
19867 pop_scope (pushed_scope);
19868 pushed_scope = NULL_TREE;
19870 decl = grokfield (declarator, decl_specifiers,
19871 initializer, !is_non_constant_init,
19872 /*asmspec=*/NULL_TREE,
19873 attr_chainon (attributes, prefix_attributes));
19874 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19875 cp_parser_save_default_args (parser, decl);
19876 cp_finalize_omp_declare_simd (parser, decl);
19877 cp_finalize_oacc_routine (parser, decl, false);
19880 /* Finish processing the declaration. But, skip member
19881 declarations. */
19882 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19884 cp_finish_decl (decl,
19885 initializer, !is_non_constant_init,
19886 asm_specification,
19887 /* If the initializer is in parentheses, then this is
19888 a direct-initialization, which means that an
19889 `explicit' constructor is OK. Otherwise, an
19890 `explicit' constructor cannot be used. */
19891 ((is_direct_init || !is_initialized)
19892 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19894 else if ((cxx_dialect != cxx98) && friend_p
19895 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19896 /* Core issue #226 (C++0x only): A default template-argument
19897 shall not be specified in a friend class template
19898 declaration. */
19899 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19900 /*is_partial=*/false, /*is_friend_decl=*/1);
19902 if (!friend_p && pushed_scope)
19903 pop_scope (pushed_scope);
19905 if (function_declarator_p (declarator)
19906 && parser->fully_implicit_function_template_p)
19908 if (member_p)
19909 decl = finish_fully_implicit_template (parser, decl);
19910 else
19911 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19914 if (auto_result && is_initialized && decl_specifiers->type
19915 && type_uses_auto (decl_specifiers->type))
19916 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19918 return decl;
19921 /* Parse a declarator.
19923 declarator:
19924 direct-declarator
19925 ptr-operator declarator
19927 abstract-declarator:
19928 ptr-operator abstract-declarator [opt]
19929 direct-abstract-declarator
19931 GNU Extensions:
19933 declarator:
19934 attributes [opt] direct-declarator
19935 attributes [opt] ptr-operator declarator
19937 abstract-declarator:
19938 attributes [opt] ptr-operator abstract-declarator [opt]
19939 attributes [opt] direct-abstract-declarator
19941 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19942 detect constructors, destructors, deduction guides, or conversion operators.
19943 It is set to -1 if the declarator is a name, and +1 if it is a
19944 function. Otherwise it is set to zero. Usually you just want to
19945 test for >0, but internally the negative value is used.
19947 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19948 a decl-specifier-seq unless it declares a constructor, destructor,
19949 or conversion. It might seem that we could check this condition in
19950 semantic analysis, rather than parsing, but that makes it difficult
19951 to handle something like `f()'. We want to notice that there are
19952 no decl-specifiers, and therefore realize that this is an
19953 expression, not a declaration.)
19955 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19956 the declarator is a direct-declarator of the form "(...)".
19958 MEMBER_P is true iff this declarator is a member-declarator.
19960 FRIEND_P is true iff this declarator is a friend. */
19962 static cp_declarator *
19963 cp_parser_declarator (cp_parser* parser,
19964 cp_parser_declarator_kind dcl_kind,
19965 int* ctor_dtor_or_conv_p,
19966 bool* parenthesized_p,
19967 bool member_p, bool friend_p)
19969 cp_declarator *declarator;
19970 enum tree_code code;
19971 cp_cv_quals cv_quals;
19972 tree class_type;
19973 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19975 /* Assume this is not a constructor, destructor, or type-conversion
19976 operator. */
19977 if (ctor_dtor_or_conv_p)
19978 *ctor_dtor_or_conv_p = 0;
19980 if (cp_parser_allow_gnu_extensions_p (parser))
19981 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19983 /* Check for the ptr-operator production. */
19984 cp_parser_parse_tentatively (parser);
19985 /* Parse the ptr-operator. */
19986 code = cp_parser_ptr_operator (parser,
19987 &class_type,
19988 &cv_quals,
19989 &std_attributes);
19991 /* If that worked, then we have a ptr-operator. */
19992 if (cp_parser_parse_definitely (parser))
19994 /* If a ptr-operator was found, then this declarator was not
19995 parenthesized. */
19996 if (parenthesized_p)
19997 *parenthesized_p = true;
19998 /* The dependent declarator is optional if we are parsing an
19999 abstract-declarator. */
20000 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20001 cp_parser_parse_tentatively (parser);
20003 /* Parse the dependent declarator. */
20004 declarator = cp_parser_declarator (parser, dcl_kind,
20005 /*ctor_dtor_or_conv_p=*/NULL,
20006 /*parenthesized_p=*/NULL,
20007 /*member_p=*/false,
20008 friend_p);
20010 /* If we are parsing an abstract-declarator, we must handle the
20011 case where the dependent declarator is absent. */
20012 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20013 && !cp_parser_parse_definitely (parser))
20014 declarator = NULL;
20016 declarator = cp_parser_make_indirect_declarator
20017 (code, class_type, cv_quals, declarator, std_attributes);
20019 /* Everything else is a direct-declarator. */
20020 else
20022 if (parenthesized_p)
20023 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20024 CPP_OPEN_PAREN);
20025 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20026 ctor_dtor_or_conv_p,
20027 member_p, friend_p);
20030 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20031 declarator->attributes = gnu_attributes;
20032 return declarator;
20035 /* Parse a direct-declarator or direct-abstract-declarator.
20037 direct-declarator:
20038 declarator-id
20039 direct-declarator ( parameter-declaration-clause )
20040 cv-qualifier-seq [opt]
20041 ref-qualifier [opt]
20042 exception-specification [opt]
20043 direct-declarator [ constant-expression [opt] ]
20044 ( declarator )
20046 direct-abstract-declarator:
20047 direct-abstract-declarator [opt]
20048 ( parameter-declaration-clause )
20049 cv-qualifier-seq [opt]
20050 ref-qualifier [opt]
20051 exception-specification [opt]
20052 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20053 ( abstract-declarator )
20055 Returns a representation of the declarator. DCL_KIND is
20056 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20057 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20058 we are parsing a direct-declarator. It is
20059 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20060 of ambiguity we prefer an abstract declarator, as per
20061 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20062 as for cp_parser_declarator. */
20064 static cp_declarator *
20065 cp_parser_direct_declarator (cp_parser* parser,
20066 cp_parser_declarator_kind dcl_kind,
20067 int* ctor_dtor_or_conv_p,
20068 bool member_p, bool friend_p)
20070 cp_token *token;
20071 cp_declarator *declarator = NULL;
20072 tree scope = NULL_TREE;
20073 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20074 bool saved_in_declarator_p = parser->in_declarator_p;
20075 bool first = true;
20076 tree pushed_scope = NULL_TREE;
20077 cp_token *open_paren = NULL, *close_paren = NULL;
20079 while (true)
20081 /* Peek at the next token. */
20082 token = cp_lexer_peek_token (parser->lexer);
20083 if (token->type == CPP_OPEN_PAREN)
20085 /* This is either a parameter-declaration-clause, or a
20086 parenthesized declarator. When we know we are parsing a
20087 named declarator, it must be a parenthesized declarator
20088 if FIRST is true. For instance, `(int)' is a
20089 parameter-declaration-clause, with an omitted
20090 direct-abstract-declarator. But `((*))', is a
20091 parenthesized abstract declarator. Finally, when T is a
20092 template parameter `(T)' is a
20093 parameter-declaration-clause, and not a parenthesized
20094 named declarator.
20096 We first try and parse a parameter-declaration-clause,
20097 and then try a nested declarator (if FIRST is true).
20099 It is not an error for it not to be a
20100 parameter-declaration-clause, even when FIRST is
20101 false. Consider,
20103 int i (int);
20104 int i (3);
20106 The first is the declaration of a function while the
20107 second is the definition of a variable, including its
20108 initializer.
20110 Having seen only the parenthesis, we cannot know which of
20111 these two alternatives should be selected. Even more
20112 complex are examples like:
20114 int i (int (a));
20115 int i (int (3));
20117 The former is a function-declaration; the latter is a
20118 variable initialization.
20120 Thus again, we try a parameter-declaration-clause, and if
20121 that fails, we back out and return. */
20123 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20125 tree params;
20126 bool is_declarator = false;
20128 open_paren = NULL;
20130 /* In a member-declarator, the only valid interpretation
20131 of a parenthesis is the start of a
20132 parameter-declaration-clause. (It is invalid to
20133 initialize a static data member with a parenthesized
20134 initializer; only the "=" form of initialization is
20135 permitted.) */
20136 if (!member_p)
20137 cp_parser_parse_tentatively (parser);
20139 /* Consume the `('. */
20140 matching_parens parens;
20141 parens.consume_open (parser);
20142 if (first)
20144 /* If this is going to be an abstract declarator, we're
20145 in a declarator and we can't have default args. */
20146 parser->default_arg_ok_p = false;
20147 parser->in_declarator_p = true;
20150 begin_scope (sk_function_parms, NULL_TREE);
20152 /* Parse the parameter-declaration-clause. */
20153 params = cp_parser_parameter_declaration_clause (parser);
20155 /* Consume the `)'. */
20156 parens.require_close (parser);
20158 /* If all went well, parse the cv-qualifier-seq,
20159 ref-qualifier and the exception-specification. */
20160 if (member_p || cp_parser_parse_definitely (parser))
20162 cp_cv_quals cv_quals;
20163 cp_virt_specifiers virt_specifiers;
20164 cp_ref_qualifier ref_qual;
20165 tree exception_specification;
20166 tree late_return;
20167 tree attrs;
20168 bool memfn = (member_p || (pushed_scope
20169 && CLASS_TYPE_P (pushed_scope)));
20171 is_declarator = true;
20173 if (ctor_dtor_or_conv_p)
20174 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20175 first = false;
20177 /* Parse the cv-qualifier-seq. */
20178 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20179 /* Parse the ref-qualifier. */
20180 ref_qual = cp_parser_ref_qualifier_opt (parser);
20181 /* Parse the tx-qualifier. */
20182 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20183 /* And the exception-specification. */
20184 exception_specification
20185 = cp_parser_exception_specification_opt (parser);
20187 attrs = cp_parser_std_attribute_spec_seq (parser);
20189 /* In here, we handle cases where attribute is used after
20190 the function declaration. For example:
20191 void func (int x) __attribute__((vector(..))); */
20192 tree gnu_attrs = NULL_TREE;
20193 tree requires_clause = NULL_TREE;
20194 late_return = (cp_parser_late_return_type_opt
20195 (parser, declarator, requires_clause,
20196 memfn ? cv_quals : -1));
20198 /* Parse the virt-specifier-seq. */
20199 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20201 /* Create the function-declarator. */
20202 declarator = make_call_declarator (declarator,
20203 params,
20204 cv_quals,
20205 virt_specifiers,
20206 ref_qual,
20207 tx_qual,
20208 exception_specification,
20209 late_return,
20210 requires_clause);
20211 declarator->std_attributes = attrs;
20212 declarator->attributes = gnu_attrs;
20213 /* Any subsequent parameter lists are to do with
20214 return type, so are not those of the declared
20215 function. */
20216 parser->default_arg_ok_p = false;
20219 /* Remove the function parms from scope. */
20220 pop_bindings_and_leave_scope ();
20222 if (is_declarator)
20223 /* Repeat the main loop. */
20224 continue;
20227 /* If this is the first, we can try a parenthesized
20228 declarator. */
20229 if (first)
20231 bool saved_in_type_id_in_expr_p;
20233 parser->default_arg_ok_p = saved_default_arg_ok_p;
20234 parser->in_declarator_p = saved_in_declarator_p;
20236 open_paren = token;
20237 /* Consume the `('. */
20238 matching_parens parens;
20239 parens.consume_open (parser);
20240 /* Parse the nested declarator. */
20241 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20242 parser->in_type_id_in_expr_p = true;
20243 declarator
20244 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20245 /*parenthesized_p=*/NULL,
20246 member_p, friend_p);
20247 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20248 first = false;
20249 /* Expect a `)'. */
20250 close_paren = cp_lexer_peek_token (parser->lexer);
20251 if (!parens.require_close (parser))
20252 declarator = cp_error_declarator;
20253 if (declarator == cp_error_declarator)
20254 break;
20256 goto handle_declarator;
20258 /* Otherwise, we must be done. */
20259 else
20260 break;
20262 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20263 && token->type == CPP_OPEN_SQUARE
20264 && !cp_next_tokens_can_be_attribute_p (parser))
20266 /* Parse an array-declarator. */
20267 tree bounds, attrs;
20269 if (ctor_dtor_or_conv_p)
20270 *ctor_dtor_or_conv_p = 0;
20272 open_paren = NULL;
20273 first = false;
20274 parser->default_arg_ok_p = false;
20275 parser->in_declarator_p = true;
20276 /* Consume the `['. */
20277 cp_lexer_consume_token (parser->lexer);
20278 /* Peek at the next token. */
20279 token = cp_lexer_peek_token (parser->lexer);
20280 /* If the next token is `]', then there is no
20281 constant-expression. */
20282 if (token->type != CPP_CLOSE_SQUARE)
20284 bool non_constant_p;
20285 bounds
20286 = cp_parser_constant_expression (parser,
20287 /*allow_non_constant=*/true,
20288 &non_constant_p);
20289 if (!non_constant_p)
20290 /* OK */;
20291 else if (error_operand_p (bounds))
20292 /* Already gave an error. */;
20293 else if (!parser->in_function_body
20294 || current_binding_level->kind == sk_function_parms)
20296 /* Normally, the array bound must be an integral constant
20297 expression. However, as an extension, we allow VLAs
20298 in function scopes as long as they aren't part of a
20299 parameter declaration. */
20300 cp_parser_error (parser,
20301 "array bound is not an integer constant");
20302 bounds = error_mark_node;
20304 else if (processing_template_decl
20305 && !type_dependent_expression_p (bounds))
20307 /* Remember this wasn't a constant-expression. */
20308 bounds = build_nop (TREE_TYPE (bounds), bounds);
20309 TREE_SIDE_EFFECTS (bounds) = 1;
20312 else
20313 bounds = NULL_TREE;
20314 /* Look for the closing `]'. */
20315 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20317 declarator = cp_error_declarator;
20318 break;
20321 attrs = cp_parser_std_attribute_spec_seq (parser);
20322 declarator = make_array_declarator (declarator, bounds);
20323 declarator->std_attributes = attrs;
20325 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20328 tree qualifying_scope;
20329 tree unqualified_name;
20330 tree attrs;
20331 special_function_kind sfk;
20332 bool abstract_ok;
20333 bool pack_expansion_p = false;
20334 cp_token *declarator_id_start_token;
20336 /* Parse a declarator-id */
20337 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20338 if (abstract_ok)
20340 cp_parser_parse_tentatively (parser);
20342 /* If we see an ellipsis, we should be looking at a
20343 parameter pack. */
20344 if (token->type == CPP_ELLIPSIS)
20346 /* Consume the `...' */
20347 cp_lexer_consume_token (parser->lexer);
20349 pack_expansion_p = true;
20353 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20354 unqualified_name
20355 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20356 qualifying_scope = parser->scope;
20357 if (abstract_ok)
20359 bool okay = false;
20361 if (!unqualified_name && pack_expansion_p)
20363 /* Check whether an error occurred. */
20364 okay = !cp_parser_error_occurred (parser);
20366 /* We already consumed the ellipsis to mark a
20367 parameter pack, but we have no way to report it,
20368 so abort the tentative parse. We will be exiting
20369 immediately anyway. */
20370 cp_parser_abort_tentative_parse (parser);
20372 else
20373 okay = cp_parser_parse_definitely (parser);
20375 if (!okay)
20376 unqualified_name = error_mark_node;
20377 else if (unqualified_name
20378 && (qualifying_scope
20379 || (!identifier_p (unqualified_name))))
20381 cp_parser_error (parser, "expected unqualified-id");
20382 unqualified_name = error_mark_node;
20386 if (!unqualified_name)
20387 return NULL;
20388 if (unqualified_name == error_mark_node)
20390 declarator = cp_error_declarator;
20391 pack_expansion_p = false;
20392 declarator->parameter_pack_p = false;
20393 break;
20396 attrs = cp_parser_std_attribute_spec_seq (parser);
20398 if (qualifying_scope && at_namespace_scope_p ()
20399 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20401 /* In the declaration of a member of a template class
20402 outside of the class itself, the SCOPE will sometimes
20403 be a TYPENAME_TYPE. For example, given:
20405 template <typename T>
20406 int S<T>::R::i = 3;
20408 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20409 this context, we must resolve S<T>::R to an ordinary
20410 type, rather than a typename type.
20412 The reason we normally avoid resolving TYPENAME_TYPEs
20413 is that a specialization of `S' might render
20414 `S<T>::R' not a type. However, if `S' is
20415 specialized, then this `i' will not be used, so there
20416 is no harm in resolving the types here. */
20417 tree type;
20419 /* Resolve the TYPENAME_TYPE. */
20420 type = resolve_typename_type (qualifying_scope,
20421 /*only_current_p=*/false);
20422 /* If that failed, the declarator is invalid. */
20423 if (TREE_CODE (type) == TYPENAME_TYPE)
20425 if (typedef_variant_p (type))
20426 error_at (declarator_id_start_token->location,
20427 "cannot define member of dependent typedef "
20428 "%qT", type);
20429 else
20430 error_at (declarator_id_start_token->location,
20431 "%<%T::%E%> is not a type",
20432 TYPE_CONTEXT (qualifying_scope),
20433 TYPE_IDENTIFIER (qualifying_scope));
20435 qualifying_scope = type;
20438 sfk = sfk_none;
20440 if (unqualified_name)
20442 tree class_type;
20444 if (qualifying_scope
20445 && CLASS_TYPE_P (qualifying_scope))
20446 class_type = qualifying_scope;
20447 else
20448 class_type = current_class_type;
20450 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20452 tree name_type = TREE_TYPE (unqualified_name);
20454 if (!class_type || !same_type_p (name_type, class_type))
20456 /* We do not attempt to print the declarator
20457 here because we do not have enough
20458 information about its original syntactic
20459 form. */
20460 cp_parser_error (parser, "invalid declarator");
20461 declarator = cp_error_declarator;
20462 break;
20464 else if (qualifying_scope
20465 && CLASSTYPE_USE_TEMPLATE (name_type))
20467 error_at (declarator_id_start_token->location,
20468 "invalid use of constructor as a template");
20469 inform (declarator_id_start_token->location,
20470 "use %<%T::%D%> instead of %<%T::%D%> to "
20471 "name the constructor in a qualified name",
20472 class_type,
20473 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20474 class_type, name_type);
20475 declarator = cp_error_declarator;
20476 break;
20478 unqualified_name = constructor_name (class_type);
20481 if (class_type)
20483 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20484 sfk = sfk_destructor;
20485 else if (identifier_p (unqualified_name)
20486 && IDENTIFIER_CONV_OP_P (unqualified_name))
20487 sfk = sfk_conversion;
20488 else if (/* There's no way to declare a constructor
20489 for an unnamed type, even if the type
20490 got a name for linkage purposes. */
20491 !TYPE_WAS_UNNAMED (class_type)
20492 /* Handle correctly (c++/19200):
20494 struct S {
20495 struct T{};
20496 friend void S(T);
20499 and also:
20501 namespace N {
20502 void S();
20505 struct S {
20506 friend void N::S();
20507 }; */
20508 && (!friend_p || class_type == qualifying_scope)
20509 && constructor_name_p (unqualified_name,
20510 class_type))
20511 sfk = sfk_constructor;
20512 else if (is_overloaded_fn (unqualified_name)
20513 && DECL_CONSTRUCTOR_P (get_first_fn
20514 (unqualified_name)))
20515 sfk = sfk_constructor;
20517 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20518 *ctor_dtor_or_conv_p = -1;
20521 declarator = make_id_declarator (qualifying_scope,
20522 unqualified_name,
20523 sfk);
20524 declarator->std_attributes = attrs;
20525 declarator->id_loc = token->location;
20526 declarator->parameter_pack_p = pack_expansion_p;
20528 if (pack_expansion_p)
20529 maybe_warn_variadic_templates ();
20532 handle_declarator:;
20533 scope = get_scope_of_declarator (declarator);
20534 if (scope)
20536 /* Any names that appear after the declarator-id for a
20537 member are looked up in the containing scope. */
20538 if (at_function_scope_p ())
20540 /* But declarations with qualified-ids can't appear in a
20541 function. */
20542 cp_parser_error (parser, "qualified-id in declaration");
20543 declarator = cp_error_declarator;
20544 break;
20546 pushed_scope = push_scope (scope);
20548 parser->in_declarator_p = true;
20549 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20550 || (declarator && declarator->kind == cdk_id))
20551 /* Default args are only allowed on function
20552 declarations. */
20553 parser->default_arg_ok_p = saved_default_arg_ok_p;
20554 else
20555 parser->default_arg_ok_p = false;
20557 first = false;
20559 /* We're done. */
20560 else
20561 break;
20564 /* For an abstract declarator, we might wind up with nothing at this
20565 point. That's an error; the declarator is not optional. */
20566 if (!declarator)
20567 cp_parser_error (parser, "expected declarator");
20568 else if (open_paren)
20570 /* Record overly parenthesized declarator so we can give a
20571 diagnostic about confusing decl/expr disambiguation. */
20572 if (declarator->kind == cdk_array)
20574 /* If the open and close parens are on different lines, this
20575 is probably a formatting thing, so ignore. */
20576 expanded_location open = expand_location (open_paren->location);
20577 expanded_location close = expand_location (close_paren->location);
20578 if (open.line != close.line || open.file != close.file)
20579 open_paren = NULL;
20581 if (open_paren)
20582 declarator->parenthesized = open_paren->location;
20585 /* If we entered a scope, we must exit it now. */
20586 if (pushed_scope)
20587 pop_scope (pushed_scope);
20589 parser->default_arg_ok_p = saved_default_arg_ok_p;
20590 parser->in_declarator_p = saved_in_declarator_p;
20592 return declarator;
20595 /* Parse a ptr-operator.
20597 ptr-operator:
20598 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20599 * cv-qualifier-seq [opt]
20601 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20602 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20604 GNU Extension:
20606 ptr-operator:
20607 & cv-qualifier-seq [opt]
20609 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20610 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20611 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20612 filled in with the TYPE containing the member. *CV_QUALS is
20613 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20614 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20615 Note that the tree codes returned by this function have nothing
20616 to do with the types of trees that will be eventually be created
20617 to represent the pointer or reference type being parsed. They are
20618 just constants with suggestive names. */
20619 static enum tree_code
20620 cp_parser_ptr_operator (cp_parser* parser,
20621 tree* type,
20622 cp_cv_quals *cv_quals,
20623 tree *attributes)
20625 enum tree_code code = ERROR_MARK;
20626 cp_token *token;
20627 tree attrs = NULL_TREE;
20629 /* Assume that it's not a pointer-to-member. */
20630 *type = NULL_TREE;
20631 /* And that there are no cv-qualifiers. */
20632 *cv_quals = TYPE_UNQUALIFIED;
20634 /* Peek at the next token. */
20635 token = cp_lexer_peek_token (parser->lexer);
20637 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20638 if (token->type == CPP_MULT)
20639 code = INDIRECT_REF;
20640 else if (token->type == CPP_AND)
20641 code = ADDR_EXPR;
20642 else if ((cxx_dialect != cxx98) &&
20643 token->type == CPP_AND_AND) /* C++0x only */
20644 code = NON_LVALUE_EXPR;
20646 if (code != ERROR_MARK)
20648 /* Consume the `*', `&' or `&&'. */
20649 cp_lexer_consume_token (parser->lexer);
20651 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20652 `&', if we are allowing GNU extensions. (The only qualifier
20653 that can legally appear after `&' is `restrict', but that is
20654 enforced during semantic analysis. */
20655 if (code == INDIRECT_REF
20656 || cp_parser_allow_gnu_extensions_p (parser))
20657 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20659 attrs = cp_parser_std_attribute_spec_seq (parser);
20660 if (attributes != NULL)
20661 *attributes = attrs;
20663 else
20665 /* Try the pointer-to-member case. */
20666 cp_parser_parse_tentatively (parser);
20667 /* Look for the optional `::' operator. */
20668 cp_parser_global_scope_opt (parser,
20669 /*current_scope_valid_p=*/false);
20670 /* Look for the nested-name specifier. */
20671 token = cp_lexer_peek_token (parser->lexer);
20672 cp_parser_nested_name_specifier (parser,
20673 /*typename_keyword_p=*/false,
20674 /*check_dependency_p=*/true,
20675 /*type_p=*/false,
20676 /*is_declaration=*/false);
20677 /* If we found it, and the next token is a `*', then we are
20678 indeed looking at a pointer-to-member operator. */
20679 if (!cp_parser_error_occurred (parser)
20680 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20682 /* Indicate that the `*' operator was used. */
20683 code = INDIRECT_REF;
20685 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20686 error_at (token->location, "%qD is a namespace", parser->scope);
20687 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20688 error_at (token->location, "cannot form pointer to member of "
20689 "non-class %q#T", parser->scope);
20690 else
20692 /* The type of which the member is a member is given by the
20693 current SCOPE. */
20694 *type = parser->scope;
20695 /* The next name will not be qualified. */
20696 parser->scope = NULL_TREE;
20697 parser->qualifying_scope = NULL_TREE;
20698 parser->object_scope = NULL_TREE;
20699 /* Look for optional c++11 attributes. */
20700 attrs = cp_parser_std_attribute_spec_seq (parser);
20701 if (attributes != NULL)
20702 *attributes = attrs;
20703 /* Look for the optional cv-qualifier-seq. */
20704 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20707 /* If that didn't work we don't have a ptr-operator. */
20708 if (!cp_parser_parse_definitely (parser))
20709 cp_parser_error (parser, "expected ptr-operator");
20712 return code;
20715 /* Parse an (optional) cv-qualifier-seq.
20717 cv-qualifier-seq:
20718 cv-qualifier cv-qualifier-seq [opt]
20720 cv-qualifier:
20721 const
20722 volatile
20724 GNU Extension:
20726 cv-qualifier:
20727 __restrict__
20729 Returns a bitmask representing the cv-qualifiers. */
20731 static cp_cv_quals
20732 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20734 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20736 while (true)
20738 cp_token *token;
20739 cp_cv_quals cv_qualifier;
20741 /* Peek at the next token. */
20742 token = cp_lexer_peek_token (parser->lexer);
20743 /* See if it's a cv-qualifier. */
20744 switch (token->keyword)
20746 case RID_CONST:
20747 cv_qualifier = TYPE_QUAL_CONST;
20748 break;
20750 case RID_VOLATILE:
20751 cv_qualifier = TYPE_QUAL_VOLATILE;
20752 break;
20754 case RID_RESTRICT:
20755 cv_qualifier = TYPE_QUAL_RESTRICT;
20756 break;
20758 default:
20759 cv_qualifier = TYPE_UNQUALIFIED;
20760 break;
20763 if (!cv_qualifier)
20764 break;
20766 if (cv_quals & cv_qualifier)
20768 gcc_rich_location richloc (token->location);
20769 richloc.add_fixit_remove ();
20770 error_at (&richloc, "duplicate cv-qualifier");
20771 cp_lexer_purge_token (parser->lexer);
20773 else
20775 cp_lexer_consume_token (parser->lexer);
20776 cv_quals |= cv_qualifier;
20780 return cv_quals;
20783 /* Parse an (optional) ref-qualifier
20785 ref-qualifier:
20789 Returns cp_ref_qualifier representing ref-qualifier. */
20791 static cp_ref_qualifier
20792 cp_parser_ref_qualifier_opt (cp_parser* parser)
20794 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20796 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20797 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20798 return ref_qual;
20800 while (true)
20802 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20803 cp_token *token = cp_lexer_peek_token (parser->lexer);
20805 switch (token->type)
20807 case CPP_AND:
20808 curr_ref_qual = REF_QUAL_LVALUE;
20809 break;
20811 case CPP_AND_AND:
20812 curr_ref_qual = REF_QUAL_RVALUE;
20813 break;
20815 default:
20816 curr_ref_qual = REF_QUAL_NONE;
20817 break;
20820 if (!curr_ref_qual)
20821 break;
20822 else if (ref_qual)
20824 error_at (token->location, "multiple ref-qualifiers");
20825 cp_lexer_purge_token (parser->lexer);
20827 else
20829 ref_qual = curr_ref_qual;
20830 cp_lexer_consume_token (parser->lexer);
20834 return ref_qual;
20837 /* Parse an optional tx-qualifier.
20839 tx-qualifier:
20840 transaction_safe
20841 transaction_safe_dynamic */
20843 static tree
20844 cp_parser_tx_qualifier_opt (cp_parser *parser)
20846 cp_token *token = cp_lexer_peek_token (parser->lexer);
20847 if (token->type == CPP_NAME)
20849 tree name = token->u.value;
20850 const char *p = IDENTIFIER_POINTER (name);
20851 const int len = strlen ("transaction_safe");
20852 if (!strncmp (p, "transaction_safe", len))
20854 p += len;
20855 if (*p == '\0'
20856 || !strcmp (p, "_dynamic"))
20858 cp_lexer_consume_token (parser->lexer);
20859 if (!flag_tm)
20861 error ("%qE requires %<-fgnu-tm%>", name);
20862 return NULL_TREE;
20864 else
20865 return name;
20869 return NULL_TREE;
20872 /* Parse an (optional) virt-specifier-seq.
20874 virt-specifier-seq:
20875 virt-specifier virt-specifier-seq [opt]
20877 virt-specifier:
20878 override
20879 final
20881 Returns a bitmask representing the virt-specifiers. */
20883 static cp_virt_specifiers
20884 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20886 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20888 while (true)
20890 cp_token *token;
20891 cp_virt_specifiers virt_specifier;
20893 /* Peek at the next token. */
20894 token = cp_lexer_peek_token (parser->lexer);
20895 /* See if it's a virt-specifier-qualifier. */
20896 if (token->type != CPP_NAME)
20897 break;
20898 if (id_equal (token->u.value, "override"))
20900 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20901 virt_specifier = VIRT_SPEC_OVERRIDE;
20903 else if (id_equal (token->u.value, "final"))
20905 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20906 virt_specifier = VIRT_SPEC_FINAL;
20908 else if (id_equal (token->u.value, "__final"))
20910 virt_specifier = VIRT_SPEC_FINAL;
20912 else
20913 break;
20915 if (virt_specifiers & virt_specifier)
20917 gcc_rich_location richloc (token->location);
20918 richloc.add_fixit_remove ();
20919 error_at (&richloc, "duplicate virt-specifier");
20920 cp_lexer_purge_token (parser->lexer);
20922 else
20924 cp_lexer_consume_token (parser->lexer);
20925 virt_specifiers |= virt_specifier;
20928 return virt_specifiers;
20931 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20932 is in scope even though it isn't real. */
20934 void
20935 inject_this_parameter (tree ctype, cp_cv_quals quals)
20937 tree this_parm;
20939 if (current_class_ptr)
20941 /* We don't clear this between NSDMIs. Is it already what we want? */
20942 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20943 if (DECL_P (current_class_ptr)
20944 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20945 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20946 && cp_type_quals (type) == quals)
20947 return;
20950 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20951 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20952 current_class_ptr = NULL_TREE;
20953 current_class_ref
20954 = cp_build_fold_indirect_ref (this_parm);
20955 current_class_ptr = this_parm;
20958 /* Return true iff our current scope is a non-static data member
20959 initializer. */
20961 bool
20962 parsing_nsdmi (void)
20964 /* We recognize NSDMI context by the context-less 'this' pointer set up
20965 by the function above. */
20966 if (current_class_ptr
20967 && TREE_CODE (current_class_ptr) == PARM_DECL
20968 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20969 return true;
20970 return false;
20973 /* Parse a late-specified return type, if any. This is not a separate
20974 non-terminal, but part of a function declarator, which looks like
20976 -> trailing-type-specifier-seq abstract-declarator(opt)
20978 Returns the type indicated by the type-id.
20980 In addition to this, parse any queued up #pragma omp declare simd
20981 clauses, and #pragma acc routine clauses.
20983 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20984 function. */
20986 static tree
20987 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20988 tree& requires_clause, cp_cv_quals quals)
20990 cp_token *token;
20991 tree type = NULL_TREE;
20992 bool declare_simd_p = (parser->omp_declare_simd
20993 && declarator
20994 && declarator->kind == cdk_id);
20996 bool oacc_routine_p = (parser->oacc_routine
20997 && declarator
20998 && declarator->kind == cdk_id);
21000 /* Peek at the next token. */
21001 token = cp_lexer_peek_token (parser->lexer);
21002 /* A late-specified return type is indicated by an initial '->'. */
21003 if (token->type != CPP_DEREF
21004 && token->keyword != RID_REQUIRES
21005 && !(token->type == CPP_NAME
21006 && token->u.value == ridpointers[RID_REQUIRES])
21007 && !(declare_simd_p || oacc_routine_p))
21008 return NULL_TREE;
21010 tree save_ccp = current_class_ptr;
21011 tree save_ccr = current_class_ref;
21012 if (quals >= 0)
21014 /* DR 1207: 'this' is in scope in the trailing return type. */
21015 inject_this_parameter (current_class_type, quals);
21018 if (token->type == CPP_DEREF)
21020 /* Consume the ->. */
21021 cp_lexer_consume_token (parser->lexer);
21023 type = cp_parser_trailing_type_id (parser);
21026 /* Function declarations may be followed by a trailing
21027 requires-clause. */
21028 requires_clause = cp_parser_requires_clause_opt (parser);
21030 if (declare_simd_p)
21031 declarator->attributes
21032 = cp_parser_late_parsing_omp_declare_simd (parser,
21033 declarator->attributes);
21034 if (oacc_routine_p)
21035 declarator->attributes
21036 = cp_parser_late_parsing_oacc_routine (parser,
21037 declarator->attributes);
21039 if (quals >= 0)
21041 current_class_ptr = save_ccp;
21042 current_class_ref = save_ccr;
21045 return type;
21048 /* Parse a declarator-id.
21050 declarator-id:
21051 id-expression
21052 :: [opt] nested-name-specifier [opt] type-name
21054 In the `id-expression' case, the value returned is as for
21055 cp_parser_id_expression if the id-expression was an unqualified-id.
21056 If the id-expression was a qualified-id, then a SCOPE_REF is
21057 returned. The first operand is the scope (either a NAMESPACE_DECL
21058 or TREE_TYPE), but the second is still just a representation of an
21059 unqualified-id. */
21061 static tree
21062 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21064 tree id;
21065 /* The expression must be an id-expression. Assume that qualified
21066 names are the names of types so that:
21068 template <class T>
21069 int S<T>::R::i = 3;
21071 will work; we must treat `S<T>::R' as the name of a type.
21072 Similarly, assume that qualified names are templates, where
21073 required, so that:
21075 template <class T>
21076 int S<T>::R<T>::i = 3;
21078 will work, too. */
21079 id = cp_parser_id_expression (parser,
21080 /*template_keyword_p=*/false,
21081 /*check_dependency_p=*/false,
21082 /*template_p=*/NULL,
21083 /*declarator_p=*/true,
21084 optional_p);
21085 if (id && BASELINK_P (id))
21086 id = BASELINK_FUNCTIONS (id);
21087 return id;
21090 /* Parse a type-id.
21092 type-id:
21093 type-specifier-seq abstract-declarator [opt]
21095 Returns the TYPE specified. */
21097 static tree
21098 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21099 bool is_trailing_return)
21101 cp_decl_specifier_seq type_specifier_seq;
21102 cp_declarator *abstract_declarator;
21104 /* Parse the type-specifier-seq. */
21105 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21106 is_trailing_return,
21107 &type_specifier_seq);
21108 if (is_template_arg && type_specifier_seq.type
21109 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21110 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21111 /* A bare template name as a template argument is a template template
21112 argument, not a placeholder, so fail parsing it as a type argument. */
21114 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21115 cp_parser_simulate_error (parser);
21116 return error_mark_node;
21118 if (type_specifier_seq.type == error_mark_node)
21119 return error_mark_node;
21121 /* There might or might not be an abstract declarator. */
21122 cp_parser_parse_tentatively (parser);
21123 /* Look for the declarator. */
21124 abstract_declarator
21125 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21126 /*parenthesized_p=*/NULL,
21127 /*member_p=*/false,
21128 /*friend_p=*/false);
21129 /* Check to see if there really was a declarator. */
21130 if (!cp_parser_parse_definitely (parser))
21131 abstract_declarator = NULL;
21133 if (type_specifier_seq.type
21134 /* The concepts TS allows 'auto' as a type-id. */
21135 && (!flag_concepts || parser->in_type_id_in_expr_p)
21136 /* None of the valid uses of 'auto' in C++14 involve the type-id
21137 nonterminal, but it is valid in a trailing-return-type. */
21138 && !(cxx_dialect >= cxx14 && is_trailing_return))
21139 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21141 /* A type-id with type 'auto' is only ok if the abstract declarator
21142 is a function declarator with a late-specified return type.
21144 A type-id with 'auto' is also valid in a trailing-return-type
21145 in a compound-requirement. */
21146 if (abstract_declarator
21147 && abstract_declarator->kind == cdk_function
21148 && abstract_declarator->u.function.late_return_type)
21149 /* OK */;
21150 else if (parser->in_result_type_constraint_p)
21151 /* OK */;
21152 else
21154 location_t loc = type_specifier_seq.locations[ds_type_spec];
21155 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21157 error_at (loc, "missing template arguments after %qT",
21158 auto_node);
21159 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21160 tmpl);
21162 else
21163 error_at (loc, "invalid use of %qT", auto_node);
21164 return error_mark_node;
21168 return groktypename (&type_specifier_seq, abstract_declarator,
21169 is_template_arg);
21172 static tree
21173 cp_parser_type_id (cp_parser *parser)
21175 return cp_parser_type_id_1 (parser, false, false);
21178 static tree
21179 cp_parser_template_type_arg (cp_parser *parser)
21181 tree r;
21182 const char *saved_message = parser->type_definition_forbidden_message;
21183 parser->type_definition_forbidden_message
21184 = G_("types may not be defined in template arguments");
21185 r = cp_parser_type_id_1 (parser, true, false);
21186 parser->type_definition_forbidden_message = saved_message;
21187 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21189 error ("invalid use of %<auto%> in template argument");
21190 r = error_mark_node;
21192 return r;
21195 static tree
21196 cp_parser_trailing_type_id (cp_parser *parser)
21198 return cp_parser_type_id_1 (parser, false, true);
21201 /* Parse a type-specifier-seq.
21203 type-specifier-seq:
21204 type-specifier type-specifier-seq [opt]
21206 GNU extension:
21208 type-specifier-seq:
21209 attributes type-specifier-seq [opt]
21211 If IS_DECLARATION is true, we are at the start of a "condition" or
21212 exception-declaration, so we might be followed by a declarator-id.
21214 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21215 i.e. we've just seen "->".
21217 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21219 static void
21220 cp_parser_type_specifier_seq (cp_parser* parser,
21221 bool is_declaration,
21222 bool is_trailing_return,
21223 cp_decl_specifier_seq *type_specifier_seq)
21225 bool seen_type_specifier = false;
21226 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21227 cp_token *start_token = NULL;
21229 /* Clear the TYPE_SPECIFIER_SEQ. */
21230 clear_decl_specs (type_specifier_seq);
21232 /* In the context of a trailing return type, enum E { } is an
21233 elaborated-type-specifier followed by a function-body, not an
21234 enum-specifier. */
21235 if (is_trailing_return)
21236 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21238 /* Parse the type-specifiers and attributes. */
21239 while (true)
21241 tree type_specifier;
21242 bool is_cv_qualifier;
21244 /* Check for attributes first. */
21245 if (cp_next_tokens_can_be_attribute_p (parser))
21247 type_specifier_seq->attributes
21248 = attr_chainon (type_specifier_seq->attributes,
21249 cp_parser_attributes_opt (parser));
21250 continue;
21253 /* record the token of the beginning of the type specifier seq,
21254 for error reporting purposes*/
21255 if (!start_token)
21256 start_token = cp_lexer_peek_token (parser->lexer);
21258 /* Look for the type-specifier. */
21259 type_specifier = cp_parser_type_specifier (parser,
21260 flags,
21261 type_specifier_seq,
21262 /*is_declaration=*/false,
21263 NULL,
21264 &is_cv_qualifier);
21265 if (!type_specifier)
21267 /* If the first type-specifier could not be found, this is not a
21268 type-specifier-seq at all. */
21269 if (!seen_type_specifier)
21271 /* Set in_declarator_p to avoid skipping to the semicolon. */
21272 int in_decl = parser->in_declarator_p;
21273 parser->in_declarator_p = true;
21275 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21276 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21277 cp_parser_error (parser, "expected type-specifier");
21279 parser->in_declarator_p = in_decl;
21281 type_specifier_seq->type = error_mark_node;
21282 return;
21284 /* If subsequent type-specifiers could not be found, the
21285 type-specifier-seq is complete. */
21286 break;
21289 seen_type_specifier = true;
21290 /* The standard says that a condition can be:
21292 type-specifier-seq declarator = assignment-expression
21294 However, given:
21296 struct S {};
21297 if (int S = ...)
21299 we should treat the "S" as a declarator, not as a
21300 type-specifier. The standard doesn't say that explicitly for
21301 type-specifier-seq, but it does say that for
21302 decl-specifier-seq in an ordinary declaration. Perhaps it
21303 would be clearer just to allow a decl-specifier-seq here, and
21304 then add a semantic restriction that if any decl-specifiers
21305 that are not type-specifiers appear, the program is invalid. */
21306 if (is_declaration && !is_cv_qualifier)
21307 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21311 /* Return whether the function currently being declared has an associated
21312 template parameter list. */
21314 static bool
21315 function_being_declared_is_template_p (cp_parser* parser)
21317 if (!current_template_parms || processing_template_parmlist)
21318 return false;
21320 if (parser->implicit_template_scope)
21321 return true;
21323 if (at_class_scope_p ()
21324 && TYPE_BEING_DEFINED (current_class_type))
21325 return parser->num_template_parameter_lists != 0;
21327 return ((int) parser->num_template_parameter_lists > template_class_depth
21328 (current_class_type));
21331 /* Parse a parameter-declaration-clause.
21333 parameter-declaration-clause:
21334 parameter-declaration-list [opt] ... [opt]
21335 parameter-declaration-list , ...
21337 Returns a representation for the parameter declarations. A return
21338 value of NULL indicates a parameter-declaration-clause consisting
21339 only of an ellipsis. */
21341 static tree
21342 cp_parser_parameter_declaration_clause (cp_parser* parser)
21344 tree parameters;
21345 cp_token *token;
21346 bool ellipsis_p;
21348 temp_override<bool> cleanup
21349 (parser->auto_is_implicit_function_template_parm_p);
21351 if (!processing_specialization
21352 && !processing_template_parmlist
21353 && !processing_explicit_instantiation
21354 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21355 actual function or a random abstract declarator. */
21356 && parser->default_arg_ok_p)
21357 if (!current_function_decl
21358 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21359 parser->auto_is_implicit_function_template_parm_p = true;
21361 /* Peek at the next token. */
21362 token = cp_lexer_peek_token (parser->lexer);
21363 /* Check for trivial parameter-declaration-clauses. */
21364 if (token->type == CPP_ELLIPSIS)
21366 /* Consume the `...' token. */
21367 cp_lexer_consume_token (parser->lexer);
21368 return NULL_TREE;
21370 else if (token->type == CPP_CLOSE_PAREN)
21371 /* There are no parameters. */
21373 #ifdef SYSTEM_IMPLICIT_EXTERN_C
21374 if (in_system_header_at (input_location)
21375 && current_class_type == NULL
21376 && current_lang_name == lang_name_c)
21377 return NULL_TREE;
21378 else
21379 #endif
21380 return void_list_node;
21382 /* Check for `(void)', too, which is a special case. */
21383 else if (token->keyword == RID_VOID
21384 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21385 == CPP_CLOSE_PAREN))
21387 /* Consume the `void' token. */
21388 cp_lexer_consume_token (parser->lexer);
21389 /* There are no parameters. */
21390 return void_list_node;
21393 /* Parse the parameter-declaration-list. */
21394 parameters = cp_parser_parameter_declaration_list (parser);
21395 /* If a parse error occurred while parsing the
21396 parameter-declaration-list, then the entire
21397 parameter-declaration-clause is erroneous. */
21398 if (parameters == error_mark_node)
21399 return NULL_TREE;
21401 /* Peek at the next token. */
21402 token = cp_lexer_peek_token (parser->lexer);
21403 /* If it's a `,', the clause should terminate with an ellipsis. */
21404 if (token->type == CPP_COMMA)
21406 /* Consume the `,'. */
21407 cp_lexer_consume_token (parser->lexer);
21408 /* Expect an ellipsis. */
21409 ellipsis_p
21410 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21412 /* It might also be `...' if the optional trailing `,' was
21413 omitted. */
21414 else if (token->type == CPP_ELLIPSIS)
21416 /* Consume the `...' token. */
21417 cp_lexer_consume_token (parser->lexer);
21418 /* And remember that we saw it. */
21419 ellipsis_p = true;
21421 else
21422 ellipsis_p = false;
21424 /* Finish the parameter list. */
21425 if (!ellipsis_p)
21426 parameters = chainon (parameters, void_list_node);
21428 return parameters;
21431 /* Parse a parameter-declaration-list.
21433 parameter-declaration-list:
21434 parameter-declaration
21435 parameter-declaration-list , parameter-declaration
21437 Returns a representation of the parameter-declaration-list, as for
21438 cp_parser_parameter_declaration_clause. However, the
21439 `void_list_node' is never appended to the list. */
21441 static tree
21442 cp_parser_parameter_declaration_list (cp_parser* parser)
21444 tree parameters = NULL_TREE;
21445 tree *tail = &parameters;
21446 bool saved_in_unbraced_linkage_specification_p;
21447 int index = 0;
21449 /* The special considerations that apply to a function within an
21450 unbraced linkage specifications do not apply to the parameters
21451 to the function. */
21452 saved_in_unbraced_linkage_specification_p
21453 = parser->in_unbraced_linkage_specification_p;
21454 parser->in_unbraced_linkage_specification_p = false;
21456 /* Look for more parameters. */
21457 while (true)
21459 cp_parameter_declarator *parameter;
21460 tree decl = error_mark_node;
21461 bool parenthesized_p = false;
21463 /* Parse the parameter. */
21464 parameter
21465 = cp_parser_parameter_declaration (parser,
21466 /*template_parm_p=*/false,
21467 &parenthesized_p);
21469 /* We don't know yet if the enclosing context is deprecated, so wait
21470 and warn in grokparms if appropriate. */
21471 deprecated_state = DEPRECATED_SUPPRESS;
21473 if (parameter)
21475 decl = grokdeclarator (parameter->declarator,
21476 &parameter->decl_specifiers,
21477 PARM,
21478 parameter->default_argument != NULL_TREE,
21479 &parameter->decl_specifiers.attributes);
21480 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21481 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21484 deprecated_state = DEPRECATED_NORMAL;
21486 /* If a parse error occurred parsing the parameter declaration,
21487 then the entire parameter-declaration-list is erroneous. */
21488 if (decl == error_mark_node)
21490 parameters = error_mark_node;
21491 break;
21494 if (parameter->decl_specifiers.attributes)
21495 cplus_decl_attributes (&decl,
21496 parameter->decl_specifiers.attributes,
21498 if (DECL_NAME (decl))
21499 decl = pushdecl (decl);
21501 if (decl != error_mark_node)
21503 retrofit_lang_decl (decl);
21504 DECL_PARM_INDEX (decl) = ++index;
21505 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21508 /* Add the new parameter to the list. */
21509 *tail = build_tree_list (parameter->default_argument, decl);
21510 tail = &TREE_CHAIN (*tail);
21512 /* Peek at the next token. */
21513 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21514 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21515 /* These are for Objective-C++ */
21516 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21517 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21518 /* The parameter-declaration-list is complete. */
21519 break;
21520 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21522 cp_token *token;
21524 /* Peek at the next token. */
21525 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21526 /* If it's an ellipsis, then the list is complete. */
21527 if (token->type == CPP_ELLIPSIS)
21528 break;
21529 /* Otherwise, there must be more parameters. Consume the
21530 `,'. */
21531 cp_lexer_consume_token (parser->lexer);
21532 /* When parsing something like:
21534 int i(float f, double d)
21536 we can tell after seeing the declaration for "f" that we
21537 are not looking at an initialization of a variable "i",
21538 but rather at the declaration of a function "i".
21540 Due to the fact that the parsing of template arguments
21541 (as specified to a template-id) requires backtracking we
21542 cannot use this technique when inside a template argument
21543 list. */
21544 if (!parser->in_template_argument_list_p
21545 && !parser->in_type_id_in_expr_p
21546 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21547 /* However, a parameter-declaration of the form
21548 "float(f)" (which is a valid declaration of a
21549 parameter "f") can also be interpreted as an
21550 expression (the conversion of "f" to "float"). */
21551 && !parenthesized_p)
21552 cp_parser_commit_to_tentative_parse (parser);
21554 else
21556 cp_parser_error (parser, "expected %<,%> or %<...%>");
21557 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21558 cp_parser_skip_to_closing_parenthesis (parser,
21559 /*recovering=*/true,
21560 /*or_comma=*/false,
21561 /*consume_paren=*/false);
21562 break;
21566 parser->in_unbraced_linkage_specification_p
21567 = saved_in_unbraced_linkage_specification_p;
21569 /* Reset implicit_template_scope if we are about to leave the function
21570 parameter list that introduced it. Note that for out-of-line member
21571 definitions, there will be one or more class scopes before we get to
21572 the template parameter scope. */
21574 if (cp_binding_level *its = parser->implicit_template_scope)
21575 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21577 while (maybe_its->kind == sk_class)
21578 maybe_its = maybe_its->level_chain;
21579 if (maybe_its == its)
21581 parser->implicit_template_parms = 0;
21582 parser->implicit_template_scope = 0;
21586 return parameters;
21589 /* Parse a parameter declaration.
21591 parameter-declaration:
21592 decl-specifier-seq ... [opt] declarator
21593 decl-specifier-seq declarator = assignment-expression
21594 decl-specifier-seq ... [opt] abstract-declarator [opt]
21595 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21597 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21598 declares a template parameter. (In that case, a non-nested `>'
21599 token encountered during the parsing of the assignment-expression
21600 is not interpreted as a greater-than operator.)
21602 Returns a representation of the parameter, or NULL if an error
21603 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21604 true iff the declarator is of the form "(p)". */
21606 static cp_parameter_declarator *
21607 cp_parser_parameter_declaration (cp_parser *parser,
21608 bool template_parm_p,
21609 bool *parenthesized_p)
21611 int declares_class_or_enum;
21612 cp_decl_specifier_seq decl_specifiers;
21613 cp_declarator *declarator;
21614 tree default_argument;
21615 cp_token *token = NULL, *declarator_token_start = NULL;
21616 const char *saved_message;
21617 bool template_parameter_pack_p = false;
21619 /* In a template parameter, `>' is not an operator.
21621 [temp.param]
21623 When parsing a default template-argument for a non-type
21624 template-parameter, the first non-nested `>' is taken as the end
21625 of the template parameter-list rather than a greater-than
21626 operator. */
21628 /* Type definitions may not appear in parameter types. */
21629 saved_message = parser->type_definition_forbidden_message;
21630 parser->type_definition_forbidden_message
21631 = G_("types may not be defined in parameter types");
21633 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21634 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21635 (current_template_parms)) : 0);
21637 /* Parse the declaration-specifiers. */
21638 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21639 cp_parser_decl_specifier_seq (parser,
21640 CP_PARSER_FLAGS_NONE,
21641 &decl_specifiers,
21642 &declares_class_or_enum);
21644 /* Complain about missing 'typename' or other invalid type names. */
21645 if (!decl_specifiers.any_type_specifiers_p
21646 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21647 decl_specifiers.type = error_mark_node;
21649 /* If an error occurred, there's no reason to attempt to parse the
21650 rest of the declaration. */
21651 if (cp_parser_error_occurred (parser))
21653 parser->type_definition_forbidden_message = saved_message;
21654 return NULL;
21657 /* Peek at the next token. */
21658 token = cp_lexer_peek_token (parser->lexer);
21660 /* If the next token is a `)', `,', `=', `>', or `...', then there
21661 is no declarator. However, when variadic templates are enabled,
21662 there may be a declarator following `...'. */
21663 if (token->type == CPP_CLOSE_PAREN
21664 || token->type == CPP_COMMA
21665 || token->type == CPP_EQ
21666 || token->type == CPP_GREATER)
21668 declarator = NULL;
21669 if (parenthesized_p)
21670 *parenthesized_p = false;
21672 /* Otherwise, there should be a declarator. */
21673 else
21675 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21676 parser->default_arg_ok_p = false;
21678 /* After seeing a decl-specifier-seq, if the next token is not a
21679 "(", there is no possibility that the code is a valid
21680 expression. Therefore, if parsing tentatively, we commit at
21681 this point. */
21682 if (!parser->in_template_argument_list_p
21683 /* In an expression context, having seen:
21685 (int((char ...
21687 we cannot be sure whether we are looking at a
21688 function-type (taking a "char" as a parameter) or a cast
21689 of some object of type "char" to "int". */
21690 && !parser->in_type_id_in_expr_p
21691 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21692 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21693 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21694 cp_parser_commit_to_tentative_parse (parser);
21695 /* Parse the declarator. */
21696 declarator_token_start = token;
21697 declarator = cp_parser_declarator (parser,
21698 CP_PARSER_DECLARATOR_EITHER,
21699 /*ctor_dtor_or_conv_p=*/NULL,
21700 parenthesized_p,
21701 /*member_p=*/false,
21702 /*friend_p=*/false);
21703 parser->default_arg_ok_p = saved_default_arg_ok_p;
21704 /* After the declarator, allow more attributes. */
21705 decl_specifiers.attributes
21706 = attr_chainon (decl_specifiers.attributes,
21707 cp_parser_attributes_opt (parser));
21709 /* If the declarator is a template parameter pack, remember that and
21710 clear the flag in the declarator itself so we don't get errors
21711 from grokdeclarator. */
21712 if (template_parm_p && declarator && declarator->parameter_pack_p)
21714 declarator->parameter_pack_p = false;
21715 template_parameter_pack_p = true;
21719 /* If the next token is an ellipsis, and we have not seen a declarator
21720 name, and if either the type of the declarator contains parameter
21721 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21722 for, eg, abbreviated integral type names), then we actually have a
21723 parameter pack expansion expression. Otherwise, leave the ellipsis
21724 for a C-style variadic function. */
21725 token = cp_lexer_peek_token (parser->lexer);
21727 /* If a function parameter pack was specified and an implicit template
21728 parameter was introduced during cp_parser_parameter_declaration,
21729 change any implicit parameters introduced into packs. */
21730 if (parser->implicit_template_parms
21731 && ((token->type == CPP_ELLIPSIS
21732 && declarator_can_be_parameter_pack (declarator))
21733 || (declarator && declarator->parameter_pack_p)))
21735 int latest_template_parm_idx = TREE_VEC_LENGTH
21736 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21738 if (latest_template_parm_idx != template_parm_idx)
21739 decl_specifiers.type = convert_generic_types_to_packs
21740 (decl_specifiers.type,
21741 template_parm_idx, latest_template_parm_idx);
21744 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21746 tree type = decl_specifiers.type;
21748 if (type && DECL_P (type))
21749 type = TREE_TYPE (type);
21751 if (((type
21752 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21753 && (template_parm_p || uses_parameter_packs (type)))
21754 || (!type && template_parm_p))
21755 && declarator_can_be_parameter_pack (declarator))
21757 /* Consume the `...'. */
21758 cp_lexer_consume_token (parser->lexer);
21759 maybe_warn_variadic_templates ();
21761 /* Build a pack expansion type */
21762 if (template_parm_p)
21763 template_parameter_pack_p = true;
21764 else if (declarator)
21765 declarator->parameter_pack_p = true;
21766 else
21767 decl_specifiers.type = make_pack_expansion (type);
21771 /* The restriction on defining new types applies only to the type
21772 of the parameter, not to the default argument. */
21773 parser->type_definition_forbidden_message = saved_message;
21775 /* If the next token is `=', then process a default argument. */
21776 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21778 tree type = decl_specifiers.type;
21779 token = cp_lexer_peek_token (parser->lexer);
21780 /* If we are defining a class, then the tokens that make up the
21781 default argument must be saved and processed later. */
21782 if (!template_parm_p && at_class_scope_p ()
21783 && TYPE_BEING_DEFINED (current_class_type)
21784 && !LAMBDA_TYPE_P (current_class_type))
21785 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21787 // A constrained-type-specifier may declare a type template-parameter.
21788 else if (declares_constrained_type_template_parameter (type))
21789 default_argument
21790 = cp_parser_default_type_template_argument (parser);
21792 // A constrained-type-specifier may declare a template-template-parameter.
21793 else if (declares_constrained_template_template_parameter (type))
21794 default_argument
21795 = cp_parser_default_template_template_argument (parser);
21797 /* Outside of a class definition, we can just parse the
21798 assignment-expression. */
21799 else
21800 default_argument
21801 = cp_parser_default_argument (parser, template_parm_p);
21803 if (!parser->default_arg_ok_p)
21805 permerror (token->location,
21806 "default arguments are only "
21807 "permitted for function parameters");
21809 else if ((declarator && declarator->parameter_pack_p)
21810 || template_parameter_pack_p
21811 || (decl_specifiers.type
21812 && PACK_EXPANSION_P (decl_specifiers.type)))
21814 /* Find the name of the parameter pack. */
21815 cp_declarator *id_declarator = declarator;
21816 while (id_declarator && id_declarator->kind != cdk_id)
21817 id_declarator = id_declarator->declarator;
21819 if (id_declarator && id_declarator->kind == cdk_id)
21820 error_at (declarator_token_start->location,
21821 template_parm_p
21822 ? G_("template parameter pack %qD "
21823 "cannot have a default argument")
21824 : G_("parameter pack %qD cannot have "
21825 "a default argument"),
21826 id_declarator->u.id.unqualified_name);
21827 else
21828 error_at (declarator_token_start->location,
21829 template_parm_p
21830 ? G_("template parameter pack cannot have "
21831 "a default argument")
21832 : G_("parameter pack cannot have a "
21833 "default argument"));
21835 default_argument = NULL_TREE;
21838 else
21839 default_argument = NULL_TREE;
21841 /* Generate a location for the parameter, ranging from the start of the
21842 initial token to the end of the final token (using input_location for
21843 the latter, set up by cp_lexer_set_source_position_from_token when
21844 consuming tokens).
21846 If we have a identifier, then use it for the caret location, e.g.
21848 extern int callee (int one, int (*two)(int, int), float three);
21849 ~~~~~~^~~~~~~~~~~~~~
21851 otherwise, reuse the start location for the caret location e.g.:
21853 extern int callee (int one, int (*)(int, int), float three);
21854 ^~~~~~~~~~~~~~~~~
21857 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21858 ? declarator->id_loc
21859 : decl_spec_token_start->location);
21860 location_t param_loc = make_location (caret_loc,
21861 decl_spec_token_start->location,
21862 input_location);
21864 return make_parameter_declarator (&decl_specifiers,
21865 declarator,
21866 default_argument,
21867 param_loc,
21868 template_parameter_pack_p);
21871 /* Parse a default argument and return it.
21873 TEMPLATE_PARM_P is true if this is a default argument for a
21874 non-type template parameter. */
21875 static tree
21876 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21878 tree default_argument = NULL_TREE;
21879 bool saved_greater_than_is_operator_p;
21880 bool saved_local_variables_forbidden_p;
21881 bool non_constant_p, is_direct_init;
21883 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21884 set correctly. */
21885 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21886 parser->greater_than_is_operator_p = !template_parm_p;
21887 /* Local variable names (and the `this' keyword) may not
21888 appear in a default argument. */
21889 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21890 parser->local_variables_forbidden_p = true;
21891 /* Parse the assignment-expression. */
21892 if (template_parm_p)
21893 push_deferring_access_checks (dk_no_deferred);
21894 tree saved_class_ptr = NULL_TREE;
21895 tree saved_class_ref = NULL_TREE;
21896 /* The "this" pointer is not valid in a default argument. */
21897 if (cfun)
21899 saved_class_ptr = current_class_ptr;
21900 cp_function_chain->x_current_class_ptr = NULL_TREE;
21901 saved_class_ref = current_class_ref;
21902 cp_function_chain->x_current_class_ref = NULL_TREE;
21904 default_argument
21905 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21906 /* Restore the "this" pointer. */
21907 if (cfun)
21909 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21910 cp_function_chain->x_current_class_ref = saved_class_ref;
21912 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21913 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21914 if (template_parm_p)
21915 pop_deferring_access_checks ();
21916 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21917 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21919 return default_argument;
21922 /* Parse a function-body.
21924 function-body:
21925 compound_statement */
21927 static void
21928 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21930 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21931 ? BCS_TRY_BLOCK : BCS_NORMAL),
21932 true);
21935 /* Parse a ctor-initializer-opt followed by a function-body. Return
21936 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21937 is true we are parsing a function-try-block. */
21939 static void
21940 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21941 bool in_function_try_block)
21943 tree body, list;
21944 const bool check_body_p =
21945 DECL_CONSTRUCTOR_P (current_function_decl)
21946 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21947 tree last = NULL;
21949 /* Begin the function body. */
21950 body = begin_function_body ();
21951 /* Parse the optional ctor-initializer. */
21952 cp_parser_ctor_initializer_opt (parser);
21954 /* If we're parsing a constexpr constructor definition, we need
21955 to check that the constructor body is indeed empty. However,
21956 before we get to cp_parser_function_body lot of junk has been
21957 generated, so we can't just check that we have an empty block.
21958 Rather we take a snapshot of the outermost block, and check whether
21959 cp_parser_function_body changed its state. */
21960 if (check_body_p)
21962 list = cur_stmt_list;
21963 if (STATEMENT_LIST_TAIL (list))
21964 last = STATEMENT_LIST_TAIL (list)->stmt;
21966 /* Parse the function-body. */
21967 cp_parser_function_body (parser, in_function_try_block);
21968 if (check_body_p)
21969 check_constexpr_ctor_body (last, list, /*complain=*/true);
21970 /* Finish the function body. */
21971 finish_function_body (body);
21974 /* Parse an initializer.
21976 initializer:
21977 = initializer-clause
21978 ( expression-list )
21980 Returns an expression representing the initializer. If no
21981 initializer is present, NULL_TREE is returned.
21983 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21984 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21985 set to TRUE if there is no initializer present. If there is an
21986 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21987 is set to true; otherwise it is set to false. */
21989 static tree
21990 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21991 bool* non_constant_p, bool subexpression_p)
21993 cp_token *token;
21994 tree init;
21996 /* Peek at the next token. */
21997 token = cp_lexer_peek_token (parser->lexer);
21999 /* Let our caller know whether or not this initializer was
22000 parenthesized. */
22001 *is_direct_init = (token->type != CPP_EQ);
22002 /* Assume that the initializer is constant. */
22003 *non_constant_p = false;
22005 if (token->type == CPP_EQ)
22007 /* Consume the `='. */
22008 cp_lexer_consume_token (parser->lexer);
22009 /* Parse the initializer-clause. */
22010 init = cp_parser_initializer_clause (parser, non_constant_p);
22012 else if (token->type == CPP_OPEN_PAREN)
22014 vec<tree, va_gc> *vec;
22015 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22016 /*cast_p=*/false,
22017 /*allow_expansion_p=*/true,
22018 non_constant_p);
22019 if (vec == NULL)
22020 return error_mark_node;
22021 init = build_tree_list_vec (vec);
22022 release_tree_vector (vec);
22024 else if (token->type == CPP_OPEN_BRACE)
22026 cp_lexer_set_source_position (parser->lexer);
22027 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22028 init = cp_parser_braced_list (parser, non_constant_p);
22029 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22031 else
22033 /* Anything else is an error. */
22034 cp_parser_error (parser, "expected initializer");
22035 init = error_mark_node;
22038 if (!subexpression_p && check_for_bare_parameter_packs (init))
22039 init = error_mark_node;
22041 return init;
22044 /* Parse an initializer-clause.
22046 initializer-clause:
22047 assignment-expression
22048 braced-init-list
22050 Returns an expression representing the initializer.
22052 If the `assignment-expression' production is used the value
22053 returned is simply a representation for the expression.
22055 Otherwise, calls cp_parser_braced_list. */
22057 static cp_expr
22058 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22060 cp_expr initializer;
22062 /* Assume the expression is constant. */
22063 *non_constant_p = false;
22065 /* If it is not a `{', then we are looking at an
22066 assignment-expression. */
22067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22069 initializer
22070 = cp_parser_constant_expression (parser,
22071 /*allow_non_constant_p=*/true,
22072 non_constant_p);
22074 else
22075 initializer = cp_parser_braced_list (parser, non_constant_p);
22077 return initializer;
22080 /* Parse a brace-enclosed initializer list.
22082 braced-init-list:
22083 { initializer-list , [opt] }
22084 { designated-initializer-list , [opt] }
22087 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22088 the elements of the initializer-list (or NULL, if the last
22089 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22090 NULL_TREE. There is no way to detect whether or not the optional
22091 trailing `,' was provided. NON_CONSTANT_P is as for
22092 cp_parser_initializer. */
22094 static cp_expr
22095 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22097 tree initializer;
22098 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22100 /* Consume the `{' token. */
22101 matching_braces braces;
22102 braces.require_open (parser);
22103 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22104 initializer = make_node (CONSTRUCTOR);
22105 /* If it's not a `}', then there is a non-trivial initializer. */
22106 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22108 /* Parse the initializer list. */
22109 CONSTRUCTOR_ELTS (initializer)
22110 = cp_parser_initializer_list (parser, non_constant_p);
22111 /* A trailing `,' token is allowed. */
22112 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22113 cp_lexer_consume_token (parser->lexer);
22115 else
22116 *non_constant_p = false;
22117 /* Now, there should be a trailing `}'. */
22118 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22119 braces.require_close (parser);
22120 TREE_TYPE (initializer) = init_list_type_node;
22122 cp_expr result (initializer);
22123 /* Build a location of the form:
22124 { ... }
22125 ^~~~~~~
22126 with caret==start at the open brace, finish at the close brace. */
22127 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22128 result.set_location (combined_loc);
22129 return result;
22132 /* Consume tokens up to, and including, the next non-nested closing `]'.
22133 Returns true iff we found a closing `]'. */
22135 static bool
22136 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22138 unsigned square_depth = 0;
22140 while (true)
22142 cp_token * token = cp_lexer_peek_token (parser->lexer);
22144 switch (token->type)
22146 case CPP_EOF:
22147 case CPP_PRAGMA_EOL:
22148 /* If we've run out of tokens, then there is no closing `]'. */
22149 return false;
22151 case CPP_OPEN_SQUARE:
22152 ++square_depth;
22153 break;
22155 case CPP_CLOSE_SQUARE:
22156 if (!square_depth--)
22158 cp_lexer_consume_token (parser->lexer);
22159 return true;
22161 break;
22163 default:
22164 break;
22167 /* Consume the token. */
22168 cp_lexer_consume_token (parser->lexer);
22172 /* Return true if we are looking at an array-designator, false otherwise. */
22174 static bool
22175 cp_parser_array_designator_p (cp_parser *parser)
22177 /* Consume the `['. */
22178 cp_lexer_consume_token (parser->lexer);
22180 cp_lexer_save_tokens (parser->lexer);
22182 /* Skip tokens until the next token is a closing square bracket.
22183 If we find the closing `]', and the next token is a `=', then
22184 we are looking at an array designator. */
22185 bool array_designator_p
22186 = (cp_parser_skip_to_closing_square_bracket (parser)
22187 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22189 /* Roll back the tokens we skipped. */
22190 cp_lexer_rollback_tokens (parser->lexer);
22192 return array_designator_p;
22195 /* Parse an initializer-list.
22197 initializer-list:
22198 initializer-clause ... [opt]
22199 initializer-list , initializer-clause ... [opt]
22201 C++2A Extension:
22203 designated-initializer-list:
22204 designated-initializer-clause
22205 designated-initializer-list , designated-initializer-clause
22207 designated-initializer-clause:
22208 designator brace-or-equal-initializer
22210 designator:
22211 . identifier
22213 GNU Extension:
22215 initializer-list:
22216 designation initializer-clause ...[opt]
22217 initializer-list , designation initializer-clause ...[opt]
22219 designation:
22220 . identifier =
22221 identifier :
22222 [ constant-expression ] =
22224 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22225 for the initializer. If the INDEX of the elt is non-NULL, it is the
22226 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22227 as for cp_parser_initializer. */
22229 static vec<constructor_elt, va_gc> *
22230 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22232 vec<constructor_elt, va_gc> *v = NULL;
22233 bool first_p = true;
22234 tree first_designator = NULL_TREE;
22236 /* Assume all of the expressions are constant. */
22237 *non_constant_p = false;
22239 /* Parse the rest of the list. */
22240 while (true)
22242 cp_token *token;
22243 tree designator;
22244 tree initializer;
22245 bool clause_non_constant_p;
22246 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22248 /* Handle the C++2A syntax, '. id ='. */
22249 if ((cxx_dialect >= cxx2a
22250 || cp_parser_allow_gnu_extensions_p (parser))
22251 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22252 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22253 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22254 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22255 == CPP_OPEN_BRACE)))
22257 if (cxx_dialect < cxx2a)
22258 pedwarn (loc, OPT_Wpedantic,
22259 "C++ designated initializers only available with "
22260 "-std=c++2a or -std=gnu++2a");
22261 /* Consume the `.'. */
22262 cp_lexer_consume_token (parser->lexer);
22263 /* Consume the identifier. */
22264 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22265 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22266 /* Consume the `='. */
22267 cp_lexer_consume_token (parser->lexer);
22269 /* Also, if the next token is an identifier and the following one is a
22270 colon, we are looking at the GNU designated-initializer
22271 syntax. */
22272 else if (cp_parser_allow_gnu_extensions_p (parser)
22273 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22274 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22275 == CPP_COLON))
22277 /* Warn the user that they are using an extension. */
22278 pedwarn (loc, OPT_Wpedantic,
22279 "ISO C++ does not allow GNU designated initializers");
22280 /* Consume the identifier. */
22281 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22282 /* Consume the `:'. */
22283 cp_lexer_consume_token (parser->lexer);
22285 /* Also handle C99 array designators, '[ const ] ='. */
22286 else if (cp_parser_allow_gnu_extensions_p (parser)
22287 && !c_dialect_objc ()
22288 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22290 /* In C++11, [ could start a lambda-introducer. */
22291 bool non_const = false;
22293 cp_parser_parse_tentatively (parser);
22295 if (!cp_parser_array_designator_p (parser))
22297 cp_parser_simulate_error (parser);
22298 designator = NULL_TREE;
22300 else
22302 designator = cp_parser_constant_expression (parser, true,
22303 &non_const);
22304 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22305 cp_parser_require (parser, CPP_EQ, RT_EQ);
22308 if (!cp_parser_parse_definitely (parser))
22309 designator = NULL_TREE;
22310 else if (non_const
22311 && (!require_potential_rvalue_constant_expression
22312 (designator)))
22313 designator = NULL_TREE;
22314 if (designator)
22315 /* Warn the user that they are using an extension. */
22316 pedwarn (loc, OPT_Wpedantic,
22317 "ISO C++ does not allow C99 designated initializers");
22319 else
22320 designator = NULL_TREE;
22322 if (first_p)
22324 first_designator = designator;
22325 first_p = false;
22327 else if (cxx_dialect >= cxx2a
22328 && first_designator != error_mark_node
22329 && (!first_designator != !designator))
22331 error_at (loc, "either all initializer clauses should be designated "
22332 "or none of them should be");
22333 first_designator = error_mark_node;
22335 else if (cxx_dialect < cxx2a && !first_designator)
22336 first_designator = designator;
22338 /* Parse the initializer. */
22339 initializer = cp_parser_initializer_clause (parser,
22340 &clause_non_constant_p);
22341 /* If any clause is non-constant, so is the entire initializer. */
22342 if (clause_non_constant_p)
22343 *non_constant_p = true;
22345 /* If we have an ellipsis, this is an initializer pack
22346 expansion. */
22347 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22349 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22351 /* Consume the `...'. */
22352 cp_lexer_consume_token (parser->lexer);
22354 if (designator && cxx_dialect >= cxx2a)
22355 error_at (loc,
22356 "%<...%> not allowed in designated initializer list");
22358 /* Turn the initializer into an initializer expansion. */
22359 initializer = make_pack_expansion (initializer);
22362 /* Add it to the vector. */
22363 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22365 /* If the next token is not a comma, we have reached the end of
22366 the list. */
22367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22368 break;
22370 /* Peek at the next token. */
22371 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22372 /* If the next token is a `}', then we're still done. An
22373 initializer-clause can have a trailing `,' after the
22374 initializer-list and before the closing `}'. */
22375 if (token->type == CPP_CLOSE_BRACE)
22376 break;
22378 /* Consume the `,' token. */
22379 cp_lexer_consume_token (parser->lexer);
22382 /* The same identifier shall not appear in multiple designators
22383 of a designated-initializer-list. */
22384 if (first_designator)
22386 unsigned int i;
22387 tree designator, val;
22388 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22389 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22391 if (IDENTIFIER_MARKED (designator))
22393 error_at (cp_expr_loc_or_loc (val, input_location),
22394 "%<.%s%> designator used multiple times in "
22395 "the same initializer list",
22396 IDENTIFIER_POINTER (designator));
22397 (*v)[i].index = NULL_TREE;
22399 else
22400 IDENTIFIER_MARKED (designator) = 1;
22402 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22403 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22404 IDENTIFIER_MARKED (designator) = 0;
22407 return v;
22410 /* Classes [gram.class] */
22412 /* Parse a class-name.
22414 class-name:
22415 identifier
22416 template-id
22418 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22419 to indicate that names looked up in dependent types should be
22420 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22421 keyword has been used to indicate that the name that appears next
22422 is a template. TAG_TYPE indicates the explicit tag given before
22423 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22424 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22425 is the class being defined in a class-head. If ENUM_OK is TRUE,
22426 enum-names are also accepted.
22428 Returns the TYPE_DECL representing the class. */
22430 static tree
22431 cp_parser_class_name (cp_parser *parser,
22432 bool typename_keyword_p,
22433 bool template_keyword_p,
22434 enum tag_types tag_type,
22435 bool check_dependency_p,
22436 bool class_head_p,
22437 bool is_declaration,
22438 bool enum_ok)
22440 tree decl;
22441 tree scope;
22442 bool typename_p;
22443 cp_token *token;
22444 tree identifier = NULL_TREE;
22446 /* All class-names start with an identifier. */
22447 token = cp_lexer_peek_token (parser->lexer);
22448 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22450 cp_parser_error (parser, "expected class-name");
22451 return error_mark_node;
22454 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22455 to a template-id, so we save it here. */
22456 scope = parser->scope;
22457 if (scope == error_mark_node)
22458 return error_mark_node;
22460 /* Any name names a type if we're following the `typename' keyword
22461 in a qualified name where the enclosing scope is type-dependent. */
22462 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22463 && dependent_type_p (scope));
22464 /* Handle the common case (an identifier, but not a template-id)
22465 efficiently. */
22466 if (token->type == CPP_NAME
22467 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22469 cp_token *identifier_token;
22470 bool ambiguous_p;
22472 /* Look for the identifier. */
22473 identifier_token = cp_lexer_peek_token (parser->lexer);
22474 ambiguous_p = identifier_token->error_reported;
22475 identifier = cp_parser_identifier (parser);
22476 /* If the next token isn't an identifier, we are certainly not
22477 looking at a class-name. */
22478 if (identifier == error_mark_node)
22479 decl = error_mark_node;
22480 /* If we know this is a type-name, there's no need to look it
22481 up. */
22482 else if (typename_p)
22483 decl = identifier;
22484 else
22486 tree ambiguous_decls;
22487 /* If we already know that this lookup is ambiguous, then
22488 we've already issued an error message; there's no reason
22489 to check again. */
22490 if (ambiguous_p)
22492 cp_parser_simulate_error (parser);
22493 return error_mark_node;
22495 /* If the next token is a `::', then the name must be a type
22496 name.
22498 [basic.lookup.qual]
22500 During the lookup for a name preceding the :: scope
22501 resolution operator, object, function, and enumerator
22502 names are ignored. */
22503 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22504 tag_type = scope_type;
22505 /* Look up the name. */
22506 decl = cp_parser_lookup_name (parser, identifier,
22507 tag_type,
22508 /*is_template=*/false,
22509 /*is_namespace=*/false,
22510 check_dependency_p,
22511 &ambiguous_decls,
22512 identifier_token->location);
22513 if (ambiguous_decls)
22515 if (cp_parser_parsing_tentatively (parser))
22516 cp_parser_simulate_error (parser);
22517 return error_mark_node;
22521 else
22523 /* Try a template-id. */
22524 decl = cp_parser_template_id (parser, template_keyword_p,
22525 check_dependency_p,
22526 tag_type,
22527 is_declaration);
22528 if (decl == error_mark_node)
22529 return error_mark_node;
22532 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22534 /* If this is a typename, create a TYPENAME_TYPE. */
22535 if (typename_p && decl != error_mark_node)
22537 decl = make_typename_type (scope, decl, typename_type,
22538 /*complain=*/tf_error);
22539 if (decl != error_mark_node)
22540 decl = TYPE_NAME (decl);
22543 decl = strip_using_decl (decl);
22545 /* Check to see that it is really the name of a class. */
22546 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22547 && identifier_p (TREE_OPERAND (decl, 0))
22548 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22549 /* Situations like this:
22551 template <typename T> struct A {
22552 typename T::template X<int>::I i;
22555 are problematic. Is `T::template X<int>' a class-name? The
22556 standard does not seem to be definitive, but there is no other
22557 valid interpretation of the following `::'. Therefore, those
22558 names are considered class-names. */
22560 decl = make_typename_type (scope, decl, tag_type, tf_error);
22561 if (decl != error_mark_node)
22562 decl = TYPE_NAME (decl);
22564 else if (TREE_CODE (decl) != TYPE_DECL
22565 || TREE_TYPE (decl) == error_mark_node
22566 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22567 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22568 /* In Objective-C 2.0, a classname followed by '.' starts a
22569 dot-syntax expression, and it's not a type-name. */
22570 || (c_dialect_objc ()
22571 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22572 && objc_is_class_name (decl)))
22573 decl = error_mark_node;
22575 if (decl == error_mark_node)
22576 cp_parser_error (parser, "expected class-name");
22577 else if (identifier && !parser->scope)
22578 maybe_note_name_used_in_class (identifier, decl);
22580 return decl;
22583 /* Parse a class-specifier.
22585 class-specifier:
22586 class-head { member-specification [opt] }
22588 Returns the TREE_TYPE representing the class. */
22590 static tree
22591 cp_parser_class_specifier_1 (cp_parser* parser)
22593 tree type;
22594 tree attributes = NULL_TREE;
22595 bool nested_name_specifier_p;
22596 unsigned saved_num_template_parameter_lists;
22597 bool saved_in_function_body;
22598 unsigned char in_statement;
22599 bool in_switch_statement_p;
22600 bool saved_in_unbraced_linkage_specification_p;
22601 tree old_scope = NULL_TREE;
22602 tree scope = NULL_TREE;
22603 cp_token *closing_brace;
22605 push_deferring_access_checks (dk_no_deferred);
22607 /* Parse the class-head. */
22608 type = cp_parser_class_head (parser,
22609 &nested_name_specifier_p);
22610 /* If the class-head was a semantic disaster, skip the entire body
22611 of the class. */
22612 if (!type)
22614 cp_parser_skip_to_end_of_block_or_statement (parser);
22615 pop_deferring_access_checks ();
22616 return error_mark_node;
22619 /* Look for the `{'. */
22620 matching_braces braces;
22621 if (!braces.require_open (parser))
22623 pop_deferring_access_checks ();
22624 return error_mark_node;
22627 cp_ensure_no_omp_declare_simd (parser);
22628 cp_ensure_no_oacc_routine (parser);
22630 /* Issue an error message if type-definitions are forbidden here. */
22631 cp_parser_check_type_definition (parser);
22632 /* Remember that we are defining one more class. */
22633 ++parser->num_classes_being_defined;
22634 /* Inside the class, surrounding template-parameter-lists do not
22635 apply. */
22636 saved_num_template_parameter_lists
22637 = parser->num_template_parameter_lists;
22638 parser->num_template_parameter_lists = 0;
22639 /* We are not in a function body. */
22640 saved_in_function_body = parser->in_function_body;
22641 parser->in_function_body = false;
22642 /* Or in a loop. */
22643 in_statement = parser->in_statement;
22644 parser->in_statement = 0;
22645 /* Or in a switch. */
22646 in_switch_statement_p = parser->in_switch_statement_p;
22647 parser->in_switch_statement_p = false;
22648 /* We are not immediately inside an extern "lang" block. */
22649 saved_in_unbraced_linkage_specification_p
22650 = parser->in_unbraced_linkage_specification_p;
22651 parser->in_unbraced_linkage_specification_p = false;
22653 // Associate constraints with the type.
22654 if (flag_concepts)
22655 type = associate_classtype_constraints (type);
22657 /* Start the class. */
22658 if (nested_name_specifier_p)
22660 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22661 old_scope = push_inner_scope (scope);
22663 type = begin_class_definition (type);
22665 if (type == error_mark_node)
22666 /* If the type is erroneous, skip the entire body of the class. */
22667 cp_parser_skip_to_closing_brace (parser);
22668 else
22669 /* Parse the member-specification. */
22670 cp_parser_member_specification_opt (parser);
22672 /* Look for the trailing `}'. */
22673 closing_brace = braces.require_close (parser);
22674 /* Look for trailing attributes to apply to this class. */
22675 if (cp_parser_allow_gnu_extensions_p (parser))
22676 attributes = cp_parser_gnu_attributes_opt (parser);
22677 if (type != error_mark_node)
22678 type = finish_struct (type, attributes);
22679 if (nested_name_specifier_p)
22680 pop_inner_scope (old_scope, scope);
22682 /* We've finished a type definition. Check for the common syntax
22683 error of forgetting a semicolon after the definition. We need to
22684 be careful, as we can't just check for not-a-semicolon and be done
22685 with it; the user might have typed:
22687 class X { } c = ...;
22688 class X { } *p = ...;
22690 and so forth. Instead, enumerate all the possible tokens that
22691 might follow this production; if we don't see one of them, then
22692 complain and silently insert the semicolon. */
22694 cp_token *token = cp_lexer_peek_token (parser->lexer);
22695 bool want_semicolon = true;
22697 if (cp_next_tokens_can_be_std_attribute_p (parser))
22698 /* Don't try to parse c++11 attributes here. As per the
22699 grammar, that should be a task for
22700 cp_parser_decl_specifier_seq. */
22701 want_semicolon = false;
22703 switch (token->type)
22705 case CPP_NAME:
22706 case CPP_SEMICOLON:
22707 case CPP_MULT:
22708 case CPP_AND:
22709 case CPP_OPEN_PAREN:
22710 case CPP_CLOSE_PAREN:
22711 case CPP_COMMA:
22712 want_semicolon = false;
22713 break;
22715 /* While it's legal for type qualifiers and storage class
22716 specifiers to follow type definitions in the grammar, only
22717 compiler testsuites contain code like that. Assume that if
22718 we see such code, then what we're really seeing is a case
22719 like:
22721 class X { }
22722 const <type> var = ...;
22726 class Y { }
22727 static <type> func (...) ...
22729 i.e. the qualifier or specifier applies to the next
22730 declaration. To do so, however, we need to look ahead one
22731 more token to see if *that* token is a type specifier.
22733 This code could be improved to handle:
22735 class Z { }
22736 static const <type> var = ...; */
22737 case CPP_KEYWORD:
22738 if (keyword_is_decl_specifier (token->keyword))
22740 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22742 /* Handling user-defined types here would be nice, but very
22743 tricky. */
22744 want_semicolon
22745 = (lookahead->type == CPP_KEYWORD
22746 && keyword_begins_type_specifier (lookahead->keyword));
22748 break;
22749 default:
22750 break;
22753 /* If we don't have a type, then something is very wrong and we
22754 shouldn't try to do anything clever. Likewise for not seeing the
22755 closing brace. */
22756 if (closing_brace && TYPE_P (type) && want_semicolon)
22758 /* Locate the closing brace. */
22759 cp_token_position prev
22760 = cp_lexer_previous_token_position (parser->lexer);
22761 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22762 location_t loc = prev_token->location;
22764 /* We want to suggest insertion of a ';' immediately *after* the
22765 closing brace, so, if we can, offset the location by 1 column. */
22766 location_t next_loc = loc;
22767 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22768 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22770 rich_location richloc (line_table, next_loc);
22772 /* If we successfully offset the location, suggest the fix-it. */
22773 if (next_loc != loc)
22774 richloc.add_fixit_insert_before (next_loc, ";");
22776 if (CLASSTYPE_DECLARED_CLASS (type))
22777 error_at (&richloc,
22778 "expected %<;%> after class definition");
22779 else if (TREE_CODE (type) == RECORD_TYPE)
22780 error_at (&richloc,
22781 "expected %<;%> after struct definition");
22782 else if (TREE_CODE (type) == UNION_TYPE)
22783 error_at (&richloc,
22784 "expected %<;%> after union definition");
22785 else
22786 gcc_unreachable ();
22788 /* Unget one token and smash it to look as though we encountered
22789 a semicolon in the input stream. */
22790 cp_lexer_set_token_position (parser->lexer, prev);
22791 token = cp_lexer_peek_token (parser->lexer);
22792 token->type = CPP_SEMICOLON;
22793 token->keyword = RID_MAX;
22797 /* If this class is not itself within the scope of another class,
22798 then we need to parse the bodies of all of the queued function
22799 definitions. Note that the queued functions defined in a class
22800 are not always processed immediately following the
22801 class-specifier for that class. Consider:
22803 struct A {
22804 struct B { void f() { sizeof (A); } };
22807 If `f' were processed before the processing of `A' were
22808 completed, there would be no way to compute the size of `A'.
22809 Note that the nesting we are interested in here is lexical --
22810 not the semantic nesting given by TYPE_CONTEXT. In particular,
22811 for:
22813 struct A { struct B; };
22814 struct A::B { void f() { } };
22816 there is no need to delay the parsing of `A::B::f'. */
22817 if (--parser->num_classes_being_defined == 0)
22819 tree decl;
22820 tree class_type = NULL_TREE;
22821 tree pushed_scope = NULL_TREE;
22822 unsigned ix;
22823 cp_default_arg_entry *e;
22824 tree save_ccp, save_ccr;
22826 if (any_erroneous_template_args_p (type))
22828 /* Skip default arguments, NSDMIs, etc, in order to improve
22829 error recovery (c++/71169, c++/71832). */
22830 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22831 vec_safe_truncate (unparsed_nsdmis, 0);
22832 vec_safe_truncate (unparsed_classes, 0);
22833 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22836 /* In a first pass, parse default arguments to the functions.
22837 Then, in a second pass, parse the bodies of the functions.
22838 This two-phased approach handles cases like:
22840 struct S {
22841 void f() { g(); }
22842 void g(int i = 3);
22846 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22848 decl = e->decl;
22849 /* If there are default arguments that have not yet been processed,
22850 take care of them now. */
22851 if (class_type != e->class_type)
22853 if (pushed_scope)
22854 pop_scope (pushed_scope);
22855 class_type = e->class_type;
22856 pushed_scope = push_scope (class_type);
22858 /* Make sure that any template parameters are in scope. */
22859 maybe_begin_member_template_processing (decl);
22860 /* Parse the default argument expressions. */
22861 cp_parser_late_parsing_default_args (parser, decl);
22862 /* Remove any template parameters from the symbol table. */
22863 maybe_end_member_template_processing ();
22865 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22866 /* Now parse any NSDMIs. */
22867 save_ccp = current_class_ptr;
22868 save_ccr = current_class_ref;
22869 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22871 if (class_type != DECL_CONTEXT (decl))
22873 if (pushed_scope)
22874 pop_scope (pushed_scope);
22875 class_type = DECL_CONTEXT (decl);
22876 pushed_scope = push_scope (class_type);
22878 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22879 cp_parser_late_parsing_nsdmi (parser, decl);
22881 vec_safe_truncate (unparsed_nsdmis, 0);
22882 current_class_ptr = save_ccp;
22883 current_class_ref = save_ccr;
22884 if (pushed_scope)
22885 pop_scope (pushed_scope);
22887 /* Now do some post-NSDMI bookkeeping. */
22888 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22889 after_nsdmi_defaulted_late_checks (class_type);
22890 vec_safe_truncate (unparsed_classes, 0);
22891 after_nsdmi_defaulted_late_checks (type);
22893 /* Now parse the body of the functions. */
22894 if (flag_openmp)
22896 /* OpenMP UDRs need to be parsed before all other functions. */
22897 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22898 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22899 cp_parser_late_parsing_for_member (parser, decl);
22900 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22901 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22902 cp_parser_late_parsing_for_member (parser, decl);
22904 else
22905 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22906 cp_parser_late_parsing_for_member (parser, decl);
22907 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22909 else
22910 vec_safe_push (unparsed_classes, type);
22912 /* Put back any saved access checks. */
22913 pop_deferring_access_checks ();
22915 /* Restore saved state. */
22916 parser->in_switch_statement_p = in_switch_statement_p;
22917 parser->in_statement = in_statement;
22918 parser->in_function_body = saved_in_function_body;
22919 parser->num_template_parameter_lists
22920 = saved_num_template_parameter_lists;
22921 parser->in_unbraced_linkage_specification_p
22922 = saved_in_unbraced_linkage_specification_p;
22924 return type;
22927 static tree
22928 cp_parser_class_specifier (cp_parser* parser)
22930 tree ret;
22931 timevar_push (TV_PARSE_STRUCT);
22932 ret = cp_parser_class_specifier_1 (parser);
22933 timevar_pop (TV_PARSE_STRUCT);
22934 return ret;
22937 /* Parse a class-head.
22939 class-head:
22940 class-key identifier [opt] base-clause [opt]
22941 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22942 class-key nested-name-specifier [opt] template-id
22943 base-clause [opt]
22945 class-virt-specifier:
22946 final
22948 GNU Extensions:
22949 class-key attributes identifier [opt] base-clause [opt]
22950 class-key attributes nested-name-specifier identifier base-clause [opt]
22951 class-key attributes nested-name-specifier [opt] template-id
22952 base-clause [opt]
22954 Upon return BASES is initialized to the list of base classes (or
22955 NULL, if there are none) in the same form returned by
22956 cp_parser_base_clause.
22958 Returns the TYPE of the indicated class. Sets
22959 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22960 involving a nested-name-specifier was used, and FALSE otherwise.
22962 Returns error_mark_node if this is not a class-head.
22964 Returns NULL_TREE if the class-head is syntactically valid, but
22965 semantically invalid in a way that means we should skip the entire
22966 body of the class. */
22968 static tree
22969 cp_parser_class_head (cp_parser* parser,
22970 bool* nested_name_specifier_p)
22972 tree nested_name_specifier;
22973 enum tag_types class_key;
22974 tree id = NULL_TREE;
22975 tree type = NULL_TREE;
22976 tree attributes;
22977 tree bases;
22978 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22979 bool template_id_p = false;
22980 bool qualified_p = false;
22981 bool invalid_nested_name_p = false;
22982 bool invalid_explicit_specialization_p = false;
22983 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22984 tree pushed_scope = NULL_TREE;
22985 unsigned num_templates;
22986 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22987 /* Assume no nested-name-specifier will be present. */
22988 *nested_name_specifier_p = false;
22989 /* Assume no template parameter lists will be used in defining the
22990 type. */
22991 num_templates = 0;
22992 parser->colon_corrects_to_scope_p = false;
22994 /* Look for the class-key. */
22995 class_key = cp_parser_class_key (parser);
22996 if (class_key == none_type)
22997 return error_mark_node;
22999 location_t class_head_start_location = input_location;
23001 /* Parse the attributes. */
23002 attributes = cp_parser_attributes_opt (parser);
23004 /* If the next token is `::', that is invalid -- but sometimes
23005 people do try to write:
23007 struct ::S {};
23009 Handle this gracefully by accepting the extra qualifier, and then
23010 issuing an error about it later if this really is a
23011 class-head. If it turns out just to be an elaborated type
23012 specifier, remain silent. */
23013 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23014 qualified_p = true;
23016 push_deferring_access_checks (dk_no_check);
23018 /* Determine the name of the class. Begin by looking for an
23019 optional nested-name-specifier. */
23020 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23021 nested_name_specifier
23022 = cp_parser_nested_name_specifier_opt (parser,
23023 /*typename_keyword_p=*/false,
23024 /*check_dependency_p=*/false,
23025 /*type_p=*/true,
23026 /*is_declaration=*/false);
23027 /* If there was a nested-name-specifier, then there *must* be an
23028 identifier. */
23030 cp_token *bad_template_keyword = NULL;
23032 if (nested_name_specifier)
23034 type_start_token = cp_lexer_peek_token (parser->lexer);
23035 /* Although the grammar says `identifier', it really means
23036 `class-name' or `template-name'. You are only allowed to
23037 define a class that has already been declared with this
23038 syntax.
23040 The proposed resolution for Core Issue 180 says that wherever
23041 you see `class T::X' you should treat `X' as a type-name.
23043 It is OK to define an inaccessible class; for example:
23045 class A { class B; };
23046 class A::B {};
23048 We do not know if we will see a class-name, or a
23049 template-name. We look for a class-name first, in case the
23050 class-name is a template-id; if we looked for the
23051 template-name first we would stop after the template-name. */
23052 cp_parser_parse_tentatively (parser);
23053 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23054 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23055 type = cp_parser_class_name (parser,
23056 /*typename_keyword_p=*/false,
23057 /*template_keyword_p=*/false,
23058 class_type,
23059 /*check_dependency_p=*/false,
23060 /*class_head_p=*/true,
23061 /*is_declaration=*/false);
23062 /* If that didn't work, ignore the nested-name-specifier. */
23063 if (!cp_parser_parse_definitely (parser))
23065 invalid_nested_name_p = true;
23066 type_start_token = cp_lexer_peek_token (parser->lexer);
23067 id = cp_parser_identifier (parser);
23068 if (id == error_mark_node)
23069 id = NULL_TREE;
23071 /* If we could not find a corresponding TYPE, treat this
23072 declaration like an unqualified declaration. */
23073 if (type == error_mark_node)
23074 nested_name_specifier = NULL_TREE;
23075 /* Otherwise, count the number of templates used in TYPE and its
23076 containing scopes. */
23077 else
23078 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23080 /* Otherwise, the identifier is optional. */
23081 else
23083 /* We don't know whether what comes next is a template-id,
23084 an identifier, or nothing at all. */
23085 cp_parser_parse_tentatively (parser);
23086 /* Check for a template-id. */
23087 type_start_token = cp_lexer_peek_token (parser->lexer);
23088 id = cp_parser_template_id (parser,
23089 /*template_keyword_p=*/false,
23090 /*check_dependency_p=*/true,
23091 class_key,
23092 /*is_declaration=*/true);
23093 /* If that didn't work, it could still be an identifier. */
23094 if (!cp_parser_parse_definitely (parser))
23096 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23098 type_start_token = cp_lexer_peek_token (parser->lexer);
23099 id = cp_parser_identifier (parser);
23101 else
23102 id = NULL_TREE;
23104 else
23106 template_id_p = true;
23107 ++num_templates;
23111 pop_deferring_access_checks ();
23113 if (id)
23115 cp_parser_check_for_invalid_template_id (parser, id,
23116 class_key,
23117 type_start_token->location);
23119 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23121 /* If it's not a `:' or a `{' then we can't really be looking at a
23122 class-head, since a class-head only appears as part of a
23123 class-specifier. We have to detect this situation before calling
23124 xref_tag, since that has irreversible side-effects. */
23125 if (!cp_parser_next_token_starts_class_definition_p (parser))
23127 cp_parser_error (parser, "expected %<{%> or %<:%>");
23128 type = error_mark_node;
23129 goto out;
23132 /* At this point, we're going ahead with the class-specifier, even
23133 if some other problem occurs. */
23134 cp_parser_commit_to_tentative_parse (parser);
23135 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23137 cp_parser_error (parser,
23138 "cannot specify %<override%> for a class");
23139 type = error_mark_node;
23140 goto out;
23142 /* Issue the error about the overly-qualified name now. */
23143 if (qualified_p)
23145 cp_parser_error (parser,
23146 "global qualification of class name is invalid");
23147 type = error_mark_node;
23148 goto out;
23150 else if (invalid_nested_name_p)
23152 cp_parser_error (parser,
23153 "qualified name does not name a class");
23154 type = error_mark_node;
23155 goto out;
23157 else if (nested_name_specifier)
23159 tree scope;
23161 if (bad_template_keyword)
23162 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23163 keyword template shall not appear at the top level. */
23164 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23165 "keyword %<template%> not allowed in class-head-name");
23167 /* Reject typedef-names in class heads. */
23168 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23170 error_at (type_start_token->location,
23171 "invalid class name in declaration of %qD",
23172 type);
23173 type = NULL_TREE;
23174 goto done;
23177 /* Figure out in what scope the declaration is being placed. */
23178 scope = current_scope ();
23179 /* If that scope does not contain the scope in which the
23180 class was originally declared, the program is invalid. */
23181 if (scope && !is_ancestor (scope, nested_name_specifier))
23183 if (at_namespace_scope_p ())
23184 error_at (type_start_token->location,
23185 "declaration of %qD in namespace %qD which does not "
23186 "enclose %qD",
23187 type, scope, nested_name_specifier);
23188 else
23189 error_at (type_start_token->location,
23190 "declaration of %qD in %qD which does not enclose %qD",
23191 type, scope, nested_name_specifier);
23192 type = NULL_TREE;
23193 goto done;
23195 /* [dcl.meaning]
23197 A declarator-id shall not be qualified except for the
23198 definition of a ... nested class outside of its class
23199 ... [or] the definition or explicit instantiation of a
23200 class member of a namespace outside of its namespace. */
23201 if (scope == nested_name_specifier)
23203 permerror (nested_name_specifier_token_start->location,
23204 "extra qualification not allowed");
23205 nested_name_specifier = NULL_TREE;
23206 num_templates = 0;
23209 /* An explicit-specialization must be preceded by "template <>". If
23210 it is not, try to recover gracefully. */
23211 if (at_namespace_scope_p ()
23212 && parser->num_template_parameter_lists == 0
23213 && !processing_template_parmlist
23214 && template_id_p)
23216 /* Build a location of this form:
23217 struct typename <ARGS>
23218 ^~~~~~~~~~~~~~~~~~~~~~
23219 with caret==start at the start token, and
23220 finishing at the end of the type. */
23221 location_t reported_loc
23222 = make_location (class_head_start_location,
23223 class_head_start_location,
23224 get_finish (type_start_token->location));
23225 rich_location richloc (line_table, reported_loc);
23226 richloc.add_fixit_insert_before (class_head_start_location,
23227 "template <> ");
23228 error_at (&richloc,
23229 "an explicit specialization must be preceded by"
23230 " %<template <>%>");
23231 invalid_explicit_specialization_p = true;
23232 /* Take the same action that would have been taken by
23233 cp_parser_explicit_specialization. */
23234 ++parser->num_template_parameter_lists;
23235 begin_specialization ();
23237 /* There must be no "return" statements between this point and the
23238 end of this function; set "type "to the correct return value and
23239 use "goto done;" to return. */
23240 /* Make sure that the right number of template parameters were
23241 present. */
23242 if (!cp_parser_check_template_parameters (parser, num_templates,
23243 template_id_p,
23244 type_start_token->location,
23245 /*declarator=*/NULL))
23247 /* If something went wrong, there is no point in even trying to
23248 process the class-definition. */
23249 type = NULL_TREE;
23250 goto done;
23253 /* Look up the type. */
23254 if (template_id_p)
23256 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23257 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23258 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23260 error_at (type_start_token->location,
23261 "function template %qD redeclared as a class template", id);
23262 type = error_mark_node;
23264 else
23266 type = TREE_TYPE (id);
23267 type = maybe_process_partial_specialization (type);
23269 /* Check the scope while we still know whether or not we had a
23270 nested-name-specifier. */
23271 if (type != error_mark_node)
23272 check_unqualified_spec_or_inst (type, type_start_token->location);
23274 if (nested_name_specifier)
23275 pushed_scope = push_scope (nested_name_specifier);
23277 else if (nested_name_specifier)
23279 tree class_type;
23281 /* Given:
23283 template <typename T> struct S { struct T };
23284 template <typename T> struct S<T>::T { };
23286 we will get a TYPENAME_TYPE when processing the definition of
23287 `S::T'. We need to resolve it to the actual type before we
23288 try to define it. */
23289 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23291 class_type = resolve_typename_type (TREE_TYPE (type),
23292 /*only_current_p=*/false);
23293 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23294 type = TYPE_NAME (class_type);
23295 else
23297 cp_parser_error (parser, "could not resolve typename type");
23298 type = error_mark_node;
23302 if (maybe_process_partial_specialization (TREE_TYPE (type))
23303 == error_mark_node)
23305 type = NULL_TREE;
23306 goto done;
23309 class_type = current_class_type;
23310 /* Enter the scope indicated by the nested-name-specifier. */
23311 pushed_scope = push_scope (nested_name_specifier);
23312 /* Get the canonical version of this type. */
23313 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23314 /* Call push_template_decl if it seems like we should be defining a
23315 template either from the template headers or the type we're
23316 defining, so that we diagnose both extra and missing headers. */
23317 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23318 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23319 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23321 type = push_template_decl (type);
23322 if (type == error_mark_node)
23324 type = NULL_TREE;
23325 goto done;
23329 type = TREE_TYPE (type);
23330 *nested_name_specifier_p = true;
23332 else /* The name is not a nested name. */
23334 /* If the class was unnamed, create a dummy name. */
23335 if (!id)
23336 id = make_anon_name ();
23337 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23338 ? ts_within_enclosing_non_class
23339 : ts_current);
23340 type = xref_tag (class_key, id, tag_scope,
23341 parser->num_template_parameter_lists);
23344 /* Indicate whether this class was declared as a `class' or as a
23345 `struct'. */
23346 if (TREE_CODE (type) == RECORD_TYPE)
23347 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23348 cp_parser_check_class_key (class_key, type);
23350 /* If this type was already complete, and we see another definition,
23351 that's an error. */
23352 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23354 error_at (type_start_token->location, "redefinition of %q#T",
23355 type);
23356 inform (location_of (type), "previous definition of %q#T",
23357 type);
23358 type = NULL_TREE;
23359 goto done;
23361 else if (type == error_mark_node)
23362 type = NULL_TREE;
23364 if (type)
23366 /* Apply attributes now, before any use of the class as a template
23367 argument in its base list. */
23368 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23369 fixup_attribute_variants (type);
23372 /* We will have entered the scope containing the class; the names of
23373 base classes should be looked up in that context. For example:
23375 struct A { struct B {}; struct C; };
23376 struct A::C : B {};
23378 is valid. */
23380 /* Get the list of base-classes, if there is one. */
23381 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23383 /* PR59482: enter the class scope so that base-specifiers are looked
23384 up correctly. */
23385 if (type)
23386 pushclass (type);
23387 bases = cp_parser_base_clause (parser);
23388 /* PR59482: get out of the previously pushed class scope so that the
23389 subsequent pops pop the right thing. */
23390 if (type)
23391 popclass ();
23393 else
23394 bases = NULL_TREE;
23396 /* If we're really defining a class, process the base classes.
23397 If they're invalid, fail. */
23398 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23399 xref_basetypes (type, bases);
23401 done:
23402 /* Leave the scope given by the nested-name-specifier. We will
23403 enter the class scope itself while processing the members. */
23404 if (pushed_scope)
23405 pop_scope (pushed_scope);
23407 if (invalid_explicit_specialization_p)
23409 end_specialization ();
23410 --parser->num_template_parameter_lists;
23413 if (type)
23414 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23415 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23416 CLASSTYPE_FINAL (type) = 1;
23417 out:
23418 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23419 return type;
23422 /* Parse a class-key.
23424 class-key:
23425 class
23426 struct
23427 union
23429 Returns the kind of class-key specified, or none_type to indicate
23430 error. */
23432 static enum tag_types
23433 cp_parser_class_key (cp_parser* parser)
23435 cp_token *token;
23436 enum tag_types tag_type;
23438 /* Look for the class-key. */
23439 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23440 if (!token)
23441 return none_type;
23443 /* Check to see if the TOKEN is a class-key. */
23444 tag_type = cp_parser_token_is_class_key (token);
23445 if (!tag_type)
23446 cp_parser_error (parser, "expected class-key");
23447 return tag_type;
23450 /* Parse a type-parameter-key.
23452 type-parameter-key:
23453 class
23454 typename
23457 static void
23458 cp_parser_type_parameter_key (cp_parser* parser)
23460 /* Look for the type-parameter-key. */
23461 enum tag_types tag_type = none_type;
23462 cp_token *token = cp_lexer_peek_token (parser->lexer);
23463 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23465 cp_lexer_consume_token (parser->lexer);
23466 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23467 /* typename is not allowed in a template template parameter
23468 by the standard until C++17. */
23469 pedwarn (token->location, OPT_Wpedantic,
23470 "ISO C++ forbids typename key in template template parameter;"
23471 " use -std=c++17 or -std=gnu++17");
23473 else
23474 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23476 return;
23479 /* Parse an (optional) member-specification.
23481 member-specification:
23482 member-declaration member-specification [opt]
23483 access-specifier : member-specification [opt] */
23485 static void
23486 cp_parser_member_specification_opt (cp_parser* parser)
23488 while (true)
23490 cp_token *token;
23491 enum rid keyword;
23493 /* Peek at the next token. */
23494 token = cp_lexer_peek_token (parser->lexer);
23495 /* If it's a `}', or EOF then we've seen all the members. */
23496 if (token->type == CPP_CLOSE_BRACE
23497 || token->type == CPP_EOF
23498 || token->type == CPP_PRAGMA_EOL)
23499 break;
23501 /* See if this token is a keyword. */
23502 keyword = token->keyword;
23503 switch (keyword)
23505 case RID_PUBLIC:
23506 case RID_PROTECTED:
23507 case RID_PRIVATE:
23508 /* Consume the access-specifier. */
23509 cp_lexer_consume_token (parser->lexer);
23510 /* Remember which access-specifier is active. */
23511 current_access_specifier = token->u.value;
23512 /* Look for the `:'. */
23513 cp_parser_require (parser, CPP_COLON, RT_COLON);
23514 break;
23516 default:
23517 /* Accept #pragmas at class scope. */
23518 if (token->type == CPP_PRAGMA)
23520 cp_parser_pragma (parser, pragma_member, NULL);
23521 break;
23524 /* Otherwise, the next construction must be a
23525 member-declaration. */
23526 cp_parser_member_declaration (parser);
23531 /* Parse a member-declaration.
23533 member-declaration:
23534 decl-specifier-seq [opt] member-declarator-list [opt] ;
23535 function-definition ; [opt]
23536 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23537 using-declaration
23538 template-declaration
23539 alias-declaration
23541 member-declarator-list:
23542 member-declarator
23543 member-declarator-list , member-declarator
23545 member-declarator:
23546 declarator pure-specifier [opt]
23547 declarator constant-initializer [opt]
23548 identifier [opt] : constant-expression
23550 GNU Extensions:
23552 member-declaration:
23553 __extension__ member-declaration
23555 member-declarator:
23556 declarator attributes [opt] pure-specifier [opt]
23557 declarator attributes [opt] constant-initializer [opt]
23558 identifier [opt] attributes [opt] : constant-expression
23560 C++0x Extensions:
23562 member-declaration:
23563 static_assert-declaration */
23565 static void
23566 cp_parser_member_declaration (cp_parser* parser)
23568 cp_decl_specifier_seq decl_specifiers;
23569 tree prefix_attributes;
23570 tree decl;
23571 int declares_class_or_enum;
23572 bool friend_p;
23573 cp_token *token = NULL;
23574 cp_token *decl_spec_token_start = NULL;
23575 cp_token *initializer_token_start = NULL;
23576 int saved_pedantic;
23577 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23579 /* Check for the `__extension__' keyword. */
23580 if (cp_parser_extension_opt (parser, &saved_pedantic))
23582 /* Recurse. */
23583 cp_parser_member_declaration (parser);
23584 /* Restore the old value of the PEDANTIC flag. */
23585 pedantic = saved_pedantic;
23587 return;
23590 /* Check for a template-declaration. */
23591 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23593 /* An explicit specialization here is an error condition, and we
23594 expect the specialization handler to detect and report this. */
23595 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23596 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23597 cp_parser_explicit_specialization (parser);
23598 else
23599 cp_parser_template_declaration (parser, /*member_p=*/true);
23601 return;
23603 /* Check for a template introduction. */
23604 else if (cp_parser_template_declaration_after_export (parser, true))
23605 return;
23607 /* Check for a using-declaration. */
23608 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23610 if (cxx_dialect < cxx11)
23612 /* Parse the using-declaration. */
23613 cp_parser_using_declaration (parser,
23614 /*access_declaration_p=*/false);
23615 return;
23617 else
23619 tree decl;
23620 bool alias_decl_expected;
23621 cp_parser_parse_tentatively (parser);
23622 decl = cp_parser_alias_declaration (parser);
23623 /* Note that if we actually see the '=' token after the
23624 identifier, cp_parser_alias_declaration commits the
23625 tentative parse. In that case, we really expect an
23626 alias-declaration. Otherwise, we expect a using
23627 declaration. */
23628 alias_decl_expected =
23629 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23630 cp_parser_parse_definitely (parser);
23632 if (alias_decl_expected)
23633 finish_member_declaration (decl);
23634 else
23635 cp_parser_using_declaration (parser,
23636 /*access_declaration_p=*/false);
23637 return;
23641 /* Check for @defs. */
23642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23644 tree ivar, member;
23645 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23646 ivar = ivar_chains;
23647 while (ivar)
23649 member = ivar;
23650 ivar = TREE_CHAIN (member);
23651 TREE_CHAIN (member) = NULL_TREE;
23652 finish_member_declaration (member);
23654 return;
23657 /* If the next token is `static_assert' we have a static assertion. */
23658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23660 cp_parser_static_assert (parser, /*member_p=*/true);
23661 return;
23664 parser->colon_corrects_to_scope_p = false;
23666 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23667 goto out;
23669 /* Parse the decl-specifier-seq. */
23670 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23671 cp_parser_decl_specifier_seq (parser,
23672 CP_PARSER_FLAGS_OPTIONAL,
23673 &decl_specifiers,
23674 &declares_class_or_enum);
23675 /* Check for an invalid type-name. */
23676 if (!decl_specifiers.any_type_specifiers_p
23677 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23678 goto out;
23679 /* If there is no declarator, then the decl-specifier-seq should
23680 specify a type. */
23681 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23683 /* If there was no decl-specifier-seq, and the next token is a
23684 `;', then we have something like:
23686 struct S { ; };
23688 [class.mem]
23690 Each member-declaration shall declare at least one member
23691 name of the class. */
23692 if (!decl_specifiers.any_specifiers_p)
23694 cp_token *token = cp_lexer_peek_token (parser->lexer);
23695 if (!in_system_header_at (token->location))
23697 gcc_rich_location richloc (token->location);
23698 richloc.add_fixit_remove ();
23699 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23702 else
23704 tree type;
23706 /* See if this declaration is a friend. */
23707 friend_p = cp_parser_friend_p (&decl_specifiers);
23708 /* If there were decl-specifiers, check to see if there was
23709 a class-declaration. */
23710 type = check_tag_decl (&decl_specifiers,
23711 /*explicit_type_instantiation_p=*/false);
23712 /* Nested classes have already been added to the class, but
23713 a `friend' needs to be explicitly registered. */
23714 if (friend_p)
23716 /* If the `friend' keyword was present, the friend must
23717 be introduced with a class-key. */
23718 if (!declares_class_or_enum && cxx_dialect < cxx11)
23719 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23720 "in C++03 a class-key must be used "
23721 "when declaring a friend");
23722 /* In this case:
23724 template <typename T> struct A {
23725 friend struct A<T>::B;
23728 A<T>::B will be represented by a TYPENAME_TYPE, and
23729 therefore not recognized by check_tag_decl. */
23730 if (!type)
23732 type = decl_specifiers.type;
23733 if (type && TREE_CODE (type) == TYPE_DECL)
23734 type = TREE_TYPE (type);
23736 if (!type || !TYPE_P (type))
23737 error_at (decl_spec_token_start->location,
23738 "friend declaration does not name a class or "
23739 "function");
23740 else
23741 make_friend_class (current_class_type, type,
23742 /*complain=*/true);
23744 /* If there is no TYPE, an error message will already have
23745 been issued. */
23746 else if (!type || type == error_mark_node)
23748 /* An anonymous aggregate has to be handled specially; such
23749 a declaration really declares a data member (with a
23750 particular type), as opposed to a nested class. */
23751 else if (ANON_AGGR_TYPE_P (type))
23753 /* C++11 9.5/6. */
23754 if (decl_specifiers.storage_class != sc_none)
23755 error_at (decl_spec_token_start->location,
23756 "a storage class on an anonymous aggregate "
23757 "in class scope is not allowed");
23759 /* Remove constructors and such from TYPE, now that we
23760 know it is an anonymous aggregate. */
23761 fixup_anonymous_aggr (type);
23762 /* And make the corresponding data member. */
23763 decl = build_decl (decl_spec_token_start->location,
23764 FIELD_DECL, NULL_TREE, type);
23765 /* Add it to the class. */
23766 finish_member_declaration (decl);
23768 else
23769 cp_parser_check_access_in_redeclaration
23770 (TYPE_NAME (type),
23771 decl_spec_token_start->location);
23774 else
23776 bool assume_semicolon = false;
23778 /* Clear attributes from the decl_specifiers but keep them
23779 around as prefix attributes that apply them to the entity
23780 being declared. */
23781 prefix_attributes = decl_specifiers.attributes;
23782 decl_specifiers.attributes = NULL_TREE;
23784 /* See if these declarations will be friends. */
23785 friend_p = cp_parser_friend_p (&decl_specifiers);
23787 /* Keep going until we hit the `;' at the end of the
23788 declaration. */
23789 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23791 tree attributes = NULL_TREE;
23792 tree first_attribute;
23793 tree initializer;
23794 bool named_bitfld = false;
23796 /* Peek at the next token. */
23797 token = cp_lexer_peek_token (parser->lexer);
23799 /* The following code wants to know early if it is a bit-field
23800 or some other declaration. Attributes can appear before
23801 the `:' token. Skip over them without consuming any tokens
23802 to peek if they are followed by `:'. */
23803 if (cp_next_tokens_can_be_attribute_p (parser)
23804 || (token->type == CPP_NAME
23805 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23806 && (named_bitfld = true)))
23808 size_t n
23809 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23810 token = cp_lexer_peek_nth_token (parser->lexer, n);
23813 /* Check for a bitfield declaration. */
23814 if (token->type == CPP_COLON
23815 || (token->type == CPP_NAME
23816 && token == cp_lexer_peek_token (parser->lexer)
23817 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23818 && (named_bitfld = true)))
23820 tree identifier;
23821 tree width;
23822 tree late_attributes = NULL_TREE;
23824 if (named_bitfld)
23825 identifier = cp_parser_identifier (parser);
23826 else
23827 identifier = NULL_TREE;
23829 /* Look for attributes that apply to the bitfield. */
23830 attributes = cp_parser_attributes_opt (parser);
23832 /* Consume the `:' token. */
23833 cp_lexer_consume_token (parser->lexer);
23835 /* Get the width of the bitfield. */
23836 width = cp_parser_constant_expression (parser, false, NULL,
23837 cxx_dialect >= cxx11);
23839 /* In C++2A and as extension for C++11 and above we allow
23840 default member initializers for bit-fields. */
23841 initializer = NULL_TREE;
23842 if (cxx_dialect >= cxx11
23843 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23844 || cp_lexer_next_token_is (parser->lexer,
23845 CPP_OPEN_BRACE)))
23847 location_t loc
23848 = cp_lexer_peek_token (parser->lexer)->location;
23849 if (cxx_dialect < cxx2a
23850 && !in_system_header_at (loc)
23851 && identifier != NULL_TREE)
23852 pedwarn (loc, 0,
23853 "default member initializers for bit-fields "
23854 "only available with -std=c++2a or "
23855 "-std=gnu++2a");
23857 initializer = cp_parser_save_nsdmi (parser);
23858 if (identifier == NULL_TREE)
23860 error_at (loc, "default member initializer for "
23861 "unnamed bit-field");
23862 initializer = NULL_TREE;
23865 else
23867 /* Look for attributes that apply to the bitfield after
23868 the `:' token and width. This is where GCC used to
23869 parse attributes in the past, pedwarn if there is
23870 a std attribute. */
23871 if (cp_next_tokens_can_be_std_attribute_p (parser))
23872 pedwarn (input_location, OPT_Wpedantic,
23873 "ISO C++ allows bit-field attributes only "
23874 "before the %<:%> token");
23876 late_attributes = cp_parser_attributes_opt (parser);
23879 attributes = attr_chainon (attributes, late_attributes);
23881 /* Remember which attributes are prefix attributes and
23882 which are not. */
23883 first_attribute = attributes;
23884 /* Combine the attributes. */
23885 attributes = attr_chainon (prefix_attributes, attributes);
23887 /* Create the bitfield declaration. */
23888 decl = grokbitfield (identifier
23889 ? make_id_declarator (NULL_TREE,
23890 identifier,
23891 sfk_none)
23892 : NULL,
23893 &decl_specifiers,
23894 width, initializer,
23895 attributes);
23897 else
23899 cp_declarator *declarator;
23900 tree asm_specification;
23901 int ctor_dtor_or_conv_p;
23903 /* Parse the declarator. */
23904 declarator
23905 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23906 &ctor_dtor_or_conv_p,
23907 /*parenthesized_p=*/NULL,
23908 /*member_p=*/true,
23909 friend_p);
23911 /* If something went wrong parsing the declarator, make sure
23912 that we at least consume some tokens. */
23913 if (declarator == cp_error_declarator)
23915 /* Skip to the end of the statement. */
23916 cp_parser_skip_to_end_of_statement (parser);
23917 /* If the next token is not a semicolon, that is
23918 probably because we just skipped over the body of
23919 a function. So, we consume a semicolon if
23920 present, but do not issue an error message if it
23921 is not present. */
23922 if (cp_lexer_next_token_is (parser->lexer,
23923 CPP_SEMICOLON))
23924 cp_lexer_consume_token (parser->lexer);
23925 goto out;
23928 if (declares_class_or_enum & 2)
23929 cp_parser_check_for_definition_in_return_type
23930 (declarator, decl_specifiers.type,
23931 decl_specifiers.locations[ds_type_spec]);
23933 /* Look for an asm-specification. */
23934 asm_specification = cp_parser_asm_specification_opt (parser);
23935 /* Look for attributes that apply to the declaration. */
23936 attributes = cp_parser_attributes_opt (parser);
23937 /* Remember which attributes are prefix attributes and
23938 which are not. */
23939 first_attribute = attributes;
23940 /* Combine the attributes. */
23941 attributes = attr_chainon (prefix_attributes, attributes);
23943 /* If it's an `=', then we have a constant-initializer or a
23944 pure-specifier. It is not correct to parse the
23945 initializer before registering the member declaration
23946 since the member declaration should be in scope while
23947 its initializer is processed. However, the rest of the
23948 front end does not yet provide an interface that allows
23949 us to handle this correctly. */
23950 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23952 /* In [class.mem]:
23954 A pure-specifier shall be used only in the declaration of
23955 a virtual function.
23957 A member-declarator can contain a constant-initializer
23958 only if it declares a static member of integral or
23959 enumeration type.
23961 Therefore, if the DECLARATOR is for a function, we look
23962 for a pure-specifier; otherwise, we look for a
23963 constant-initializer. When we call `grokfield', it will
23964 perform more stringent semantics checks. */
23965 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23966 if (function_declarator_p (declarator)
23967 || (decl_specifiers.type
23968 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23969 && declarator->kind == cdk_id
23970 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23971 == FUNCTION_TYPE)))
23972 initializer = cp_parser_pure_specifier (parser);
23973 else if (decl_specifiers.storage_class != sc_static)
23974 initializer = cp_parser_save_nsdmi (parser);
23975 else if (cxx_dialect >= cxx11)
23977 bool nonconst;
23978 /* Don't require a constant rvalue in C++11, since we
23979 might want a reference constant. We'll enforce
23980 constancy later. */
23981 cp_lexer_consume_token (parser->lexer);
23982 /* Parse the initializer. */
23983 initializer = cp_parser_initializer_clause (parser,
23984 &nonconst);
23986 else
23987 /* Parse the initializer. */
23988 initializer = cp_parser_constant_initializer (parser);
23990 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23991 && !function_declarator_p (declarator))
23993 bool x;
23994 if (decl_specifiers.storage_class != sc_static)
23995 initializer = cp_parser_save_nsdmi (parser);
23996 else
23997 initializer = cp_parser_initializer (parser, &x, &x);
23999 /* Otherwise, there is no initializer. */
24000 else
24001 initializer = NULL_TREE;
24003 /* See if we are probably looking at a function
24004 definition. We are certainly not looking at a
24005 member-declarator. Calling `grokfield' has
24006 side-effects, so we must not do it unless we are sure
24007 that we are looking at a member-declarator. */
24008 if (cp_parser_token_starts_function_definition_p
24009 (cp_lexer_peek_token (parser->lexer)))
24011 /* The grammar does not allow a pure-specifier to be
24012 used when a member function is defined. (It is
24013 possible that this fact is an oversight in the
24014 standard, since a pure function may be defined
24015 outside of the class-specifier. */
24016 if (initializer && initializer_token_start)
24017 error_at (initializer_token_start->location,
24018 "pure-specifier on function-definition");
24019 decl = cp_parser_save_member_function_body (parser,
24020 &decl_specifiers,
24021 declarator,
24022 attributes);
24023 if (parser->fully_implicit_function_template_p)
24024 decl = finish_fully_implicit_template (parser, decl);
24025 /* If the member was not a friend, declare it here. */
24026 if (!friend_p)
24027 finish_member_declaration (decl);
24028 /* Peek at the next token. */
24029 token = cp_lexer_peek_token (parser->lexer);
24030 /* If the next token is a semicolon, consume it. */
24031 if (token->type == CPP_SEMICOLON)
24033 location_t semicolon_loc
24034 = cp_lexer_consume_token (parser->lexer)->location;
24035 gcc_rich_location richloc (semicolon_loc);
24036 richloc.add_fixit_remove ();
24037 warning_at (&richloc, OPT_Wextra_semi,
24038 "extra %<;%> after in-class "
24039 "function definition");
24041 goto out;
24043 else
24044 if (declarator->kind == cdk_function)
24045 declarator->id_loc = token->location;
24046 /* Create the declaration. */
24047 decl = grokfield (declarator, &decl_specifiers,
24048 initializer, /*init_const_expr_p=*/true,
24049 asm_specification, attributes);
24050 if (parser->fully_implicit_function_template_p)
24052 if (friend_p)
24053 finish_fully_implicit_template (parser, 0);
24054 else
24055 decl = finish_fully_implicit_template (parser, decl);
24059 cp_finalize_omp_declare_simd (parser, decl);
24060 cp_finalize_oacc_routine (parser, decl, false);
24062 /* Reset PREFIX_ATTRIBUTES. */
24063 if (attributes != error_mark_node)
24065 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24066 attributes = TREE_CHAIN (attributes);
24067 if (attributes)
24068 TREE_CHAIN (attributes) = NULL_TREE;
24071 /* If there is any qualification still in effect, clear it
24072 now; we will be starting fresh with the next declarator. */
24073 parser->scope = NULL_TREE;
24074 parser->qualifying_scope = NULL_TREE;
24075 parser->object_scope = NULL_TREE;
24076 /* If it's a `,', then there are more declarators. */
24077 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24079 cp_lexer_consume_token (parser->lexer);
24080 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24082 cp_token *token = cp_lexer_previous_token (parser->lexer);
24083 gcc_rich_location richloc (token->location);
24084 richloc.add_fixit_remove ();
24085 error_at (&richloc, "stray %<,%> at end of "
24086 "member declaration");
24089 /* If the next token isn't a `;', then we have a parse error. */
24090 else if (cp_lexer_next_token_is_not (parser->lexer,
24091 CPP_SEMICOLON))
24093 /* The next token might be a ways away from where the
24094 actual semicolon is missing. Find the previous token
24095 and use that for our error position. */
24096 cp_token *token = cp_lexer_previous_token (parser->lexer);
24097 gcc_rich_location richloc (token->location);
24098 richloc.add_fixit_insert_after (";");
24099 error_at (&richloc, "expected %<;%> at end of "
24100 "member declaration");
24102 /* Assume that the user meant to provide a semicolon. If
24103 we were to cp_parser_skip_to_end_of_statement, we might
24104 skip to a semicolon inside a member function definition
24105 and issue nonsensical error messages. */
24106 assume_semicolon = true;
24109 if (decl)
24111 /* Add DECL to the list of members. */
24112 if (!friend_p
24113 /* Explicitly include, eg, NSDMIs, for better error
24114 recovery (c++/58650). */
24115 || !DECL_DECLARES_FUNCTION_P (decl))
24116 finish_member_declaration (decl);
24118 if (TREE_CODE (decl) == FUNCTION_DECL)
24119 cp_parser_save_default_args (parser, decl);
24120 else if (TREE_CODE (decl) == FIELD_DECL
24121 && DECL_INITIAL (decl))
24122 /* Add DECL to the queue of NSDMI to be parsed later. */
24123 vec_safe_push (unparsed_nsdmis, decl);
24126 if (assume_semicolon)
24127 goto out;
24131 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24132 out:
24133 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24136 /* Parse a pure-specifier.
24138 pure-specifier:
24141 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24142 Otherwise, ERROR_MARK_NODE is returned. */
24144 static tree
24145 cp_parser_pure_specifier (cp_parser* parser)
24147 cp_token *token;
24149 /* Look for the `=' token. */
24150 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24151 return error_mark_node;
24152 /* Look for the `0' token. */
24153 token = cp_lexer_peek_token (parser->lexer);
24155 if (token->type == CPP_EOF
24156 || token->type == CPP_PRAGMA_EOL)
24157 return error_mark_node;
24159 cp_lexer_consume_token (parser->lexer);
24161 /* Accept = default or = delete in c++0x mode. */
24162 if (token->keyword == RID_DEFAULT
24163 || token->keyword == RID_DELETE)
24165 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24166 return token->u.value;
24169 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24170 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24172 cp_parser_error (parser,
24173 "invalid pure specifier (only %<= 0%> is allowed)");
24174 cp_parser_skip_to_end_of_statement (parser);
24175 return error_mark_node;
24177 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24179 error_at (token->location, "templates may not be %<virtual%>");
24180 return error_mark_node;
24183 return integer_zero_node;
24186 /* Parse a constant-initializer.
24188 constant-initializer:
24189 = constant-expression
24191 Returns a representation of the constant-expression. */
24193 static tree
24194 cp_parser_constant_initializer (cp_parser* parser)
24196 /* Look for the `=' token. */
24197 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24198 return error_mark_node;
24200 /* It is invalid to write:
24202 struct S { static const int i = { 7 }; };
24205 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24207 cp_parser_error (parser,
24208 "a brace-enclosed initializer is not allowed here");
24209 /* Consume the opening brace. */
24210 matching_braces braces;
24211 braces.consume_open (parser);
24212 /* Skip the initializer. */
24213 cp_parser_skip_to_closing_brace (parser);
24214 /* Look for the trailing `}'. */
24215 braces.require_close (parser);
24217 return error_mark_node;
24220 return cp_parser_constant_expression (parser);
24223 /* Derived classes [gram.class.derived] */
24225 /* Parse a base-clause.
24227 base-clause:
24228 : base-specifier-list
24230 base-specifier-list:
24231 base-specifier ... [opt]
24232 base-specifier-list , base-specifier ... [opt]
24234 Returns a TREE_LIST representing the base-classes, in the order in
24235 which they were declared. The representation of each node is as
24236 described by cp_parser_base_specifier.
24238 In the case that no bases are specified, this function will return
24239 NULL_TREE, not ERROR_MARK_NODE. */
24241 static tree
24242 cp_parser_base_clause (cp_parser* parser)
24244 tree bases = NULL_TREE;
24246 /* Look for the `:' that begins the list. */
24247 cp_parser_require (parser, CPP_COLON, RT_COLON);
24249 /* Scan the base-specifier-list. */
24250 while (true)
24252 cp_token *token;
24253 tree base;
24254 bool pack_expansion_p = false;
24256 /* Look for the base-specifier. */
24257 base = cp_parser_base_specifier (parser);
24258 /* Look for the (optional) ellipsis. */
24259 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24261 /* Consume the `...'. */
24262 cp_lexer_consume_token (parser->lexer);
24264 pack_expansion_p = true;
24267 /* Add BASE to the front of the list. */
24268 if (base && base != error_mark_node)
24270 if (pack_expansion_p)
24271 /* Make this a pack expansion type. */
24272 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24274 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24276 TREE_CHAIN (base) = bases;
24277 bases = base;
24280 /* Peek at the next token. */
24281 token = cp_lexer_peek_token (parser->lexer);
24282 /* If it's not a comma, then the list is complete. */
24283 if (token->type != CPP_COMMA)
24284 break;
24285 /* Consume the `,'. */
24286 cp_lexer_consume_token (parser->lexer);
24289 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24290 base class had a qualified name. However, the next name that
24291 appears is certainly not qualified. */
24292 parser->scope = NULL_TREE;
24293 parser->qualifying_scope = NULL_TREE;
24294 parser->object_scope = NULL_TREE;
24296 return nreverse (bases);
24299 /* Parse a base-specifier.
24301 base-specifier:
24302 :: [opt] nested-name-specifier [opt] class-name
24303 virtual access-specifier [opt] :: [opt] nested-name-specifier
24304 [opt] class-name
24305 access-specifier virtual [opt] :: [opt] nested-name-specifier
24306 [opt] class-name
24308 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24309 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24310 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24311 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24313 static tree
24314 cp_parser_base_specifier (cp_parser* parser)
24316 cp_token *token;
24317 bool done = false;
24318 bool virtual_p = false;
24319 bool duplicate_virtual_error_issued_p = false;
24320 bool duplicate_access_error_issued_p = false;
24321 bool class_scope_p, template_p;
24322 tree access = access_default_node;
24323 tree type;
24325 /* Process the optional `virtual' and `access-specifier'. */
24326 while (!done)
24328 /* Peek at the next token. */
24329 token = cp_lexer_peek_token (parser->lexer);
24330 /* Process `virtual'. */
24331 switch (token->keyword)
24333 case RID_VIRTUAL:
24334 /* If `virtual' appears more than once, issue an error. */
24335 if (virtual_p && !duplicate_virtual_error_issued_p)
24337 cp_parser_error (parser,
24338 "%<virtual%> specified more than once in base-specifier");
24339 duplicate_virtual_error_issued_p = true;
24342 virtual_p = true;
24344 /* Consume the `virtual' token. */
24345 cp_lexer_consume_token (parser->lexer);
24347 break;
24349 case RID_PUBLIC:
24350 case RID_PROTECTED:
24351 case RID_PRIVATE:
24352 /* If more than one access specifier appears, issue an
24353 error. */
24354 if (access != access_default_node
24355 && !duplicate_access_error_issued_p)
24357 cp_parser_error (parser,
24358 "more than one access specifier in base-specifier");
24359 duplicate_access_error_issued_p = true;
24362 access = ridpointers[(int) token->keyword];
24364 /* Consume the access-specifier. */
24365 cp_lexer_consume_token (parser->lexer);
24367 break;
24369 default:
24370 done = true;
24371 break;
24374 /* It is not uncommon to see programs mechanically, erroneously, use
24375 the 'typename' keyword to denote (dependent) qualified types
24376 as base classes. */
24377 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24379 token = cp_lexer_peek_token (parser->lexer);
24380 if (!processing_template_decl)
24381 error_at (token->location,
24382 "keyword %<typename%> not allowed outside of templates");
24383 else
24384 error_at (token->location,
24385 "keyword %<typename%> not allowed in this context "
24386 "(the base class is implicitly a type)");
24387 cp_lexer_consume_token (parser->lexer);
24390 /* Look for the optional `::' operator. */
24391 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24392 /* Look for the nested-name-specifier. The simplest way to
24393 implement:
24395 [temp.res]
24397 The keyword `typename' is not permitted in a base-specifier or
24398 mem-initializer; in these contexts a qualified name that
24399 depends on a template-parameter is implicitly assumed to be a
24400 type name.
24402 is to pretend that we have seen the `typename' keyword at this
24403 point. */
24404 cp_parser_nested_name_specifier_opt (parser,
24405 /*typename_keyword_p=*/true,
24406 /*check_dependency_p=*/true,
24407 /*type_p=*/true,
24408 /*is_declaration=*/true);
24409 /* If the base class is given by a qualified name, assume that names
24410 we see are type names or templates, as appropriate. */
24411 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24412 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24414 if (!parser->scope
24415 && cp_lexer_next_token_is_decltype (parser->lexer))
24416 /* DR 950 allows decltype as a base-specifier. */
24417 type = cp_parser_decltype (parser);
24418 else
24420 /* Otherwise, look for the class-name. */
24421 type = cp_parser_class_name (parser,
24422 class_scope_p,
24423 template_p,
24424 typename_type,
24425 /*check_dependency_p=*/true,
24426 /*class_head_p=*/false,
24427 /*is_declaration=*/true);
24428 type = TREE_TYPE (type);
24431 if (type == error_mark_node)
24432 return error_mark_node;
24434 return finish_base_specifier (type, access, virtual_p);
24437 /* Exception handling [gram.exception] */
24439 /* Parse an (optional) noexcept-specification.
24441 noexcept-specification:
24442 noexcept ( constant-expression ) [opt]
24444 If no noexcept-specification is present, returns NULL_TREE.
24445 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24446 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24447 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24448 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24449 in which case a boolean condition is returned instead. */
24451 static tree
24452 cp_parser_noexcept_specification_opt (cp_parser* parser,
24453 bool require_constexpr,
24454 bool* consumed_expr,
24455 bool return_cond)
24457 cp_token *token;
24458 const char *saved_message;
24460 /* Peek at the next token. */
24461 token = cp_lexer_peek_token (parser->lexer);
24463 /* Is it a noexcept-specification? */
24464 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24466 tree expr;
24467 cp_lexer_consume_token (parser->lexer);
24469 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24471 matching_parens parens;
24472 parens.consume_open (parser);
24474 if (require_constexpr)
24476 /* Types may not be defined in an exception-specification. */
24477 saved_message = parser->type_definition_forbidden_message;
24478 parser->type_definition_forbidden_message
24479 = G_("types may not be defined in an exception-specification");
24481 expr = cp_parser_constant_expression (parser);
24483 /* Restore the saved message. */
24484 parser->type_definition_forbidden_message = saved_message;
24486 else
24488 expr = cp_parser_expression (parser);
24489 *consumed_expr = true;
24492 parens.require_close (parser);
24494 else
24496 expr = boolean_true_node;
24497 if (!require_constexpr)
24498 *consumed_expr = false;
24501 /* We cannot build a noexcept-spec right away because this will check
24502 that expr is a constexpr. */
24503 if (!return_cond)
24504 return build_noexcept_spec (expr, tf_warning_or_error);
24505 else
24506 return expr;
24508 else
24509 return NULL_TREE;
24512 /* Parse an (optional) exception-specification.
24514 exception-specification:
24515 throw ( type-id-list [opt] )
24517 Returns a TREE_LIST representing the exception-specification. The
24518 TREE_VALUE of each node is a type. */
24520 static tree
24521 cp_parser_exception_specification_opt (cp_parser* parser)
24523 cp_token *token;
24524 tree type_id_list;
24525 const char *saved_message;
24527 /* Peek at the next token. */
24528 token = cp_lexer_peek_token (parser->lexer);
24530 /* Is it a noexcept-specification? */
24531 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24532 false);
24533 if (type_id_list != NULL_TREE)
24534 return type_id_list;
24536 /* If it's not `throw', then there's no exception-specification. */
24537 if (!cp_parser_is_keyword (token, RID_THROW))
24538 return NULL_TREE;
24540 location_t loc = token->location;
24542 /* Consume the `throw'. */
24543 cp_lexer_consume_token (parser->lexer);
24545 /* Look for the `('. */
24546 matching_parens parens;
24547 parens.require_open (parser);
24549 /* Peek at the next token. */
24550 token = cp_lexer_peek_token (parser->lexer);
24551 /* If it's not a `)', then there is a type-id-list. */
24552 if (token->type != CPP_CLOSE_PAREN)
24554 /* Types may not be defined in an exception-specification. */
24555 saved_message = parser->type_definition_forbidden_message;
24556 parser->type_definition_forbidden_message
24557 = G_("types may not be defined in an exception-specification");
24558 /* Parse the type-id-list. */
24559 type_id_list = cp_parser_type_id_list (parser);
24560 /* Restore the saved message. */
24561 parser->type_definition_forbidden_message = saved_message;
24563 if (cxx_dialect >= cxx17)
24565 error_at (loc, "ISO C++17 does not allow dynamic exception "
24566 "specifications");
24567 type_id_list = NULL_TREE;
24569 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24570 warning_at (loc, OPT_Wdeprecated,
24571 "dynamic exception specifications are deprecated in "
24572 "C++11");
24574 /* In C++17, throw() is equivalent to noexcept (true). throw()
24575 is deprecated in C++11 and above as well, but is still widely used,
24576 so don't warn about it yet. */
24577 else if (cxx_dialect >= cxx17)
24578 type_id_list = noexcept_true_spec;
24579 else
24580 type_id_list = empty_except_spec;
24582 /* Look for the `)'. */
24583 parens.require_close (parser);
24585 return type_id_list;
24588 /* Parse an (optional) type-id-list.
24590 type-id-list:
24591 type-id ... [opt]
24592 type-id-list , type-id ... [opt]
24594 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24595 in the order that the types were presented. */
24597 static tree
24598 cp_parser_type_id_list (cp_parser* parser)
24600 tree types = NULL_TREE;
24602 while (true)
24604 cp_token *token;
24605 tree type;
24607 token = cp_lexer_peek_token (parser->lexer);
24609 /* Get the next type-id. */
24610 type = cp_parser_type_id (parser);
24611 /* Check for invalid 'auto'. */
24612 if (flag_concepts && type_uses_auto (type))
24614 error_at (token->location,
24615 "invalid use of %<auto%> in exception-specification");
24616 type = error_mark_node;
24618 /* Parse the optional ellipsis. */
24619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24621 /* Consume the `...'. */
24622 cp_lexer_consume_token (parser->lexer);
24624 /* Turn the type into a pack expansion expression. */
24625 type = make_pack_expansion (type);
24627 /* Add it to the list. */
24628 types = add_exception_specifier (types, type, /*complain=*/1);
24629 /* Peek at the next token. */
24630 token = cp_lexer_peek_token (parser->lexer);
24631 /* If it is not a `,', we are done. */
24632 if (token->type != CPP_COMMA)
24633 break;
24634 /* Consume the `,'. */
24635 cp_lexer_consume_token (parser->lexer);
24638 return nreverse (types);
24641 /* Parse a try-block.
24643 try-block:
24644 try compound-statement handler-seq */
24646 static tree
24647 cp_parser_try_block (cp_parser* parser)
24649 tree try_block;
24651 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24652 if (parser->in_function_body
24653 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24654 error ("%<try%> in %<constexpr%> function");
24656 try_block = begin_try_block ();
24657 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24658 finish_try_block (try_block);
24659 cp_parser_handler_seq (parser);
24660 finish_handler_sequence (try_block);
24662 return try_block;
24665 /* Parse a function-try-block.
24667 function-try-block:
24668 try ctor-initializer [opt] function-body handler-seq */
24670 static void
24671 cp_parser_function_try_block (cp_parser* parser)
24673 tree compound_stmt;
24674 tree try_block;
24676 /* Look for the `try' keyword. */
24677 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24678 return;
24679 /* Let the rest of the front end know where we are. */
24680 try_block = begin_function_try_block (&compound_stmt);
24681 /* Parse the function-body. */
24682 cp_parser_ctor_initializer_opt_and_function_body
24683 (parser, /*in_function_try_block=*/true);
24684 /* We're done with the `try' part. */
24685 finish_function_try_block (try_block);
24686 /* Parse the handlers. */
24687 cp_parser_handler_seq (parser);
24688 /* We're done with the handlers. */
24689 finish_function_handler_sequence (try_block, compound_stmt);
24692 /* Parse a handler-seq.
24694 handler-seq:
24695 handler handler-seq [opt] */
24697 static void
24698 cp_parser_handler_seq (cp_parser* parser)
24700 while (true)
24702 cp_token *token;
24704 /* Parse the handler. */
24705 cp_parser_handler (parser);
24706 /* Peek at the next token. */
24707 token = cp_lexer_peek_token (parser->lexer);
24708 /* If it's not `catch' then there are no more handlers. */
24709 if (!cp_parser_is_keyword (token, RID_CATCH))
24710 break;
24714 /* Parse a handler.
24716 handler:
24717 catch ( exception-declaration ) compound-statement */
24719 static void
24720 cp_parser_handler (cp_parser* parser)
24722 tree handler;
24723 tree declaration;
24725 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24726 handler = begin_handler ();
24727 matching_parens parens;
24728 parens.require_open (parser);
24729 declaration = cp_parser_exception_declaration (parser);
24730 finish_handler_parms (declaration, handler);
24731 parens.require_close (parser);
24732 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24733 finish_handler (handler);
24736 /* Parse an exception-declaration.
24738 exception-declaration:
24739 type-specifier-seq declarator
24740 type-specifier-seq abstract-declarator
24741 type-specifier-seq
24744 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24745 ellipsis variant is used. */
24747 static tree
24748 cp_parser_exception_declaration (cp_parser* parser)
24750 cp_decl_specifier_seq type_specifiers;
24751 cp_declarator *declarator;
24752 const char *saved_message;
24754 /* If it's an ellipsis, it's easy to handle. */
24755 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24757 /* Consume the `...' token. */
24758 cp_lexer_consume_token (parser->lexer);
24759 return NULL_TREE;
24762 /* Types may not be defined in exception-declarations. */
24763 saved_message = parser->type_definition_forbidden_message;
24764 parser->type_definition_forbidden_message
24765 = G_("types may not be defined in exception-declarations");
24767 /* Parse the type-specifier-seq. */
24768 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24769 /*is_trailing_return=*/false,
24770 &type_specifiers);
24771 /* If it's a `)', then there is no declarator. */
24772 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24773 declarator = NULL;
24774 else
24775 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24776 /*ctor_dtor_or_conv_p=*/NULL,
24777 /*parenthesized_p=*/NULL,
24778 /*member_p=*/false,
24779 /*friend_p=*/false);
24781 /* Restore the saved message. */
24782 parser->type_definition_forbidden_message = saved_message;
24784 if (!type_specifiers.any_specifiers_p)
24785 return error_mark_node;
24787 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24790 /* Parse a throw-expression.
24792 throw-expression:
24793 throw assignment-expression [opt]
24795 Returns a THROW_EXPR representing the throw-expression. */
24797 static tree
24798 cp_parser_throw_expression (cp_parser* parser)
24800 tree expression;
24801 cp_token* token;
24803 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24804 token = cp_lexer_peek_token (parser->lexer);
24805 /* Figure out whether or not there is an assignment-expression
24806 following the "throw" keyword. */
24807 if (token->type == CPP_COMMA
24808 || token->type == CPP_SEMICOLON
24809 || token->type == CPP_CLOSE_PAREN
24810 || token->type == CPP_CLOSE_SQUARE
24811 || token->type == CPP_CLOSE_BRACE
24812 || token->type == CPP_COLON)
24813 expression = NULL_TREE;
24814 else
24815 expression = cp_parser_assignment_expression (parser);
24817 return build_throw (expression);
24820 /* GNU Extensions */
24822 /* Parse an (optional) asm-specification.
24824 asm-specification:
24825 asm ( string-literal )
24827 If the asm-specification is present, returns a STRING_CST
24828 corresponding to the string-literal. Otherwise, returns
24829 NULL_TREE. */
24831 static tree
24832 cp_parser_asm_specification_opt (cp_parser* parser)
24834 cp_token *token;
24835 tree asm_specification;
24837 /* Peek at the next token. */
24838 token = cp_lexer_peek_token (parser->lexer);
24839 /* If the next token isn't the `asm' keyword, then there's no
24840 asm-specification. */
24841 if (!cp_parser_is_keyword (token, RID_ASM))
24842 return NULL_TREE;
24844 /* Consume the `asm' token. */
24845 cp_lexer_consume_token (parser->lexer);
24846 /* Look for the `('. */
24847 matching_parens parens;
24848 parens.require_open (parser);
24850 /* Look for the string-literal. */
24851 asm_specification = cp_parser_string_literal (parser, false, false);
24853 /* Look for the `)'. */
24854 parens.require_close (parser);
24856 return asm_specification;
24859 /* Parse an asm-operand-list.
24861 asm-operand-list:
24862 asm-operand
24863 asm-operand-list , asm-operand
24865 asm-operand:
24866 string-literal ( expression )
24867 [ string-literal ] string-literal ( expression )
24869 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24870 each node is the expression. The TREE_PURPOSE is itself a
24871 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24872 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24873 is a STRING_CST for the string literal before the parenthesis. Returns
24874 ERROR_MARK_NODE if any of the operands are invalid. */
24876 static tree
24877 cp_parser_asm_operand_list (cp_parser* parser)
24879 tree asm_operands = NULL_TREE;
24880 bool invalid_operands = false;
24882 while (true)
24884 tree string_literal;
24885 tree expression;
24886 tree name;
24888 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24890 /* Consume the `[' token. */
24891 cp_lexer_consume_token (parser->lexer);
24892 /* Read the operand name. */
24893 name = cp_parser_identifier (parser);
24894 if (name != error_mark_node)
24895 name = build_string (IDENTIFIER_LENGTH (name),
24896 IDENTIFIER_POINTER (name));
24897 /* Look for the closing `]'. */
24898 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24900 else
24901 name = NULL_TREE;
24902 /* Look for the string-literal. */
24903 string_literal = cp_parser_string_literal (parser, false, false);
24905 /* Look for the `('. */
24906 matching_parens parens;
24907 parens.require_open (parser);
24908 /* Parse the expression. */
24909 expression = cp_parser_expression (parser);
24910 /* Look for the `)'. */
24911 parens.require_close (parser);
24913 if (name == error_mark_node
24914 || string_literal == error_mark_node
24915 || expression == error_mark_node)
24916 invalid_operands = true;
24918 /* Add this operand to the list. */
24919 asm_operands = tree_cons (build_tree_list (name, string_literal),
24920 expression,
24921 asm_operands);
24922 /* If the next token is not a `,', there are no more
24923 operands. */
24924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24925 break;
24926 /* Consume the `,'. */
24927 cp_lexer_consume_token (parser->lexer);
24930 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24933 /* Parse an asm-clobber-list.
24935 asm-clobber-list:
24936 string-literal
24937 asm-clobber-list , string-literal
24939 Returns a TREE_LIST, indicating the clobbers in the order that they
24940 appeared. The TREE_VALUE of each node is a STRING_CST. */
24942 static tree
24943 cp_parser_asm_clobber_list (cp_parser* parser)
24945 tree clobbers = NULL_TREE;
24947 while (true)
24949 tree string_literal;
24951 /* Look for the string literal. */
24952 string_literal = cp_parser_string_literal (parser, false, false);
24953 /* Add it to the list. */
24954 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24955 /* If the next token is not a `,', then the list is
24956 complete. */
24957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24958 break;
24959 /* Consume the `,' token. */
24960 cp_lexer_consume_token (parser->lexer);
24963 return clobbers;
24966 /* Parse an asm-label-list.
24968 asm-label-list:
24969 identifier
24970 asm-label-list , identifier
24972 Returns a TREE_LIST, indicating the labels in the order that they
24973 appeared. The TREE_VALUE of each node is a label. */
24975 static tree
24976 cp_parser_asm_label_list (cp_parser* parser)
24978 tree labels = NULL_TREE;
24980 while (true)
24982 tree identifier, label, name;
24984 /* Look for the identifier. */
24985 identifier = cp_parser_identifier (parser);
24986 if (!error_operand_p (identifier))
24988 label = lookup_label (identifier);
24989 if (TREE_CODE (label) == LABEL_DECL)
24991 TREE_USED (label) = 1;
24992 check_goto (label);
24993 name = build_string (IDENTIFIER_LENGTH (identifier),
24994 IDENTIFIER_POINTER (identifier));
24995 labels = tree_cons (name, label, labels);
24998 /* If the next token is not a `,', then the list is
24999 complete. */
25000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25001 break;
25002 /* Consume the `,' token. */
25003 cp_lexer_consume_token (parser->lexer);
25006 return nreverse (labels);
25009 /* Return TRUE iff the next tokens in the stream are possibly the
25010 beginning of a GNU extension attribute. */
25012 static bool
25013 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25015 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25018 /* Return TRUE iff the next tokens in the stream are possibly the
25019 beginning of a standard C++-11 attribute specifier. */
25021 static bool
25022 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25024 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25027 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25028 beginning of a standard C++-11 attribute specifier. */
25030 static bool
25031 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25033 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25035 return (cxx_dialect >= cxx11
25036 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25037 || (token->type == CPP_OPEN_SQUARE
25038 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25039 && token->type == CPP_OPEN_SQUARE)));
25042 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25043 beginning of a GNU extension attribute. */
25045 static bool
25046 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25048 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25050 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25053 /* Return true iff the next tokens can be the beginning of either a
25054 GNU attribute list, or a standard C++11 attribute sequence. */
25056 static bool
25057 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25059 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25060 || cp_next_tokens_can_be_std_attribute_p (parser));
25063 /* Return true iff the next Nth tokens can be the beginning of either
25064 a GNU attribute list, or a standard C++11 attribute sequence. */
25066 static bool
25067 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25069 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25070 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25073 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25074 of GNU attributes, or return NULL. */
25076 static tree
25077 cp_parser_attributes_opt (cp_parser *parser)
25079 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25080 return cp_parser_gnu_attributes_opt (parser);
25081 return cp_parser_std_attribute_spec_seq (parser);
25084 /* Parse an (optional) series of attributes.
25086 attributes:
25087 attributes attribute
25089 attribute:
25090 __attribute__ (( attribute-list [opt] ))
25092 The return value is as for cp_parser_gnu_attribute_list. */
25094 static tree
25095 cp_parser_gnu_attributes_opt (cp_parser* parser)
25097 tree attributes = NULL_TREE;
25099 temp_override<bool> cleanup
25100 (parser->auto_is_implicit_function_template_parm_p, false);
25102 while (true)
25104 cp_token *token;
25105 tree attribute_list;
25106 bool ok = true;
25108 /* Peek at the next token. */
25109 token = cp_lexer_peek_token (parser->lexer);
25110 /* If it's not `__attribute__', then we're done. */
25111 if (token->keyword != RID_ATTRIBUTE)
25112 break;
25114 /* Consume the `__attribute__' keyword. */
25115 cp_lexer_consume_token (parser->lexer);
25116 /* Look for the two `(' tokens. */
25117 matching_parens outer_parens;
25118 outer_parens.require_open (parser);
25119 matching_parens inner_parens;
25120 inner_parens.require_open (parser);
25122 /* Peek at the next token. */
25123 token = cp_lexer_peek_token (parser->lexer);
25124 if (token->type != CPP_CLOSE_PAREN)
25125 /* Parse the attribute-list. */
25126 attribute_list = cp_parser_gnu_attribute_list (parser);
25127 else
25128 /* If the next token is a `)', then there is no attribute
25129 list. */
25130 attribute_list = NULL;
25132 /* Look for the two `)' tokens. */
25133 if (!inner_parens.require_close (parser))
25134 ok = false;
25135 if (!outer_parens.require_close (parser))
25136 ok = false;
25137 if (!ok)
25138 cp_parser_skip_to_end_of_statement (parser);
25140 /* Add these new attributes to the list. */
25141 attributes = attr_chainon (attributes, attribute_list);
25144 return attributes;
25147 /* Parse a GNU attribute-list.
25149 attribute-list:
25150 attribute
25151 attribute-list , attribute
25153 attribute:
25154 identifier
25155 identifier ( identifier )
25156 identifier ( identifier , expression-list )
25157 identifier ( expression-list )
25159 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25160 to an attribute. The TREE_PURPOSE of each node is the identifier
25161 indicating which attribute is in use. The TREE_VALUE represents
25162 the arguments, if any. */
25164 static tree
25165 cp_parser_gnu_attribute_list (cp_parser* parser)
25167 tree attribute_list = NULL_TREE;
25168 bool save_translate_strings_p = parser->translate_strings_p;
25170 parser->translate_strings_p = false;
25171 while (true)
25173 cp_token *token;
25174 tree identifier;
25175 tree attribute;
25177 /* Look for the identifier. We also allow keywords here; for
25178 example `__attribute__ ((const))' is legal. */
25179 token = cp_lexer_peek_token (parser->lexer);
25180 if (token->type == CPP_NAME
25181 || token->type == CPP_KEYWORD)
25183 tree arguments = NULL_TREE;
25185 /* Consume the token, but save it since we need it for the
25186 SIMD enabled function parsing. */
25187 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25189 /* Save away the identifier that indicates which attribute
25190 this is. */
25191 identifier = (token->type == CPP_KEYWORD)
25192 /* For keywords, use the canonical spelling, not the
25193 parsed identifier. */
25194 ? ridpointers[(int) token->keyword]
25195 : id_token->u.value;
25197 identifier = canonicalize_attr_name (identifier);
25198 attribute = build_tree_list (identifier, NULL_TREE);
25200 /* Peek at the next token. */
25201 token = cp_lexer_peek_token (parser->lexer);
25202 /* If it's an `(', then parse the attribute arguments. */
25203 if (token->type == CPP_OPEN_PAREN)
25205 vec<tree, va_gc> *vec;
25206 int attr_flag = (attribute_takes_identifier_p (identifier)
25207 ? id_attr : normal_attr);
25208 vec = cp_parser_parenthesized_expression_list
25209 (parser, attr_flag, /*cast_p=*/false,
25210 /*allow_expansion_p=*/false,
25211 /*non_constant_p=*/NULL);
25212 if (vec == NULL)
25213 arguments = error_mark_node;
25214 else
25216 arguments = build_tree_list_vec (vec);
25217 release_tree_vector (vec);
25219 /* Save the arguments away. */
25220 TREE_VALUE (attribute) = arguments;
25223 if (arguments != error_mark_node)
25225 /* Add this attribute to the list. */
25226 TREE_CHAIN (attribute) = attribute_list;
25227 attribute_list = attribute;
25230 token = cp_lexer_peek_token (parser->lexer);
25232 /* Now, look for more attributes. If the next token isn't a
25233 `,', we're done. */
25234 if (token->type != CPP_COMMA)
25235 break;
25237 /* Consume the comma and keep going. */
25238 cp_lexer_consume_token (parser->lexer);
25240 parser->translate_strings_p = save_translate_strings_p;
25242 /* We built up the list in reverse order. */
25243 return nreverse (attribute_list);
25246 /* Parse a standard C++11 attribute.
25248 The returned representation is a TREE_LIST which TREE_PURPOSE is
25249 the scoped name of the attribute, and the TREE_VALUE is its
25250 arguments list.
25252 Note that the scoped name of the attribute is itself a TREE_LIST
25253 which TREE_PURPOSE is the namespace of the attribute, and
25254 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25255 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25256 and which TREE_PURPOSE is directly the attribute name.
25258 Clients of the attribute code should use get_attribute_namespace
25259 and get_attribute_name to get the actual namespace and name of
25260 attributes, regardless of their being GNU or C++11 attributes.
25262 attribute:
25263 attribute-token attribute-argument-clause [opt]
25265 attribute-token:
25266 identifier
25267 attribute-scoped-token
25269 attribute-scoped-token:
25270 attribute-namespace :: identifier
25272 attribute-namespace:
25273 identifier
25275 attribute-argument-clause:
25276 ( balanced-token-seq )
25278 balanced-token-seq:
25279 balanced-token [opt]
25280 balanced-token-seq balanced-token
25282 balanced-token:
25283 ( balanced-token-seq )
25284 [ balanced-token-seq ]
25285 { balanced-token-seq }. */
25287 static tree
25288 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25290 tree attribute, attr_id = NULL_TREE, arguments;
25291 cp_token *token;
25293 temp_override<bool> cleanup
25294 (parser->auto_is_implicit_function_template_parm_p, false);
25296 /* First, parse name of the attribute, a.k.a attribute-token. */
25298 token = cp_lexer_peek_token (parser->lexer);
25299 if (token->type == CPP_NAME)
25300 attr_id = token->u.value;
25301 else if (token->type == CPP_KEYWORD)
25302 attr_id = ridpointers[(int) token->keyword];
25303 else if (token->flags & NAMED_OP)
25304 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25306 if (attr_id == NULL_TREE)
25307 return NULL_TREE;
25309 cp_lexer_consume_token (parser->lexer);
25311 token = cp_lexer_peek_token (parser->lexer);
25312 if (token->type == CPP_SCOPE)
25314 /* We are seeing a scoped attribute token. */
25316 cp_lexer_consume_token (parser->lexer);
25317 if (attr_ns)
25318 error_at (token->location, "attribute using prefix used together "
25319 "with scoped attribute token");
25320 attr_ns = attr_id;
25322 token = cp_lexer_consume_token (parser->lexer);
25323 if (token->type == CPP_NAME)
25324 attr_id = token->u.value;
25325 else if (token->type == CPP_KEYWORD)
25326 attr_id = ridpointers[(int) token->keyword];
25327 else if (token->flags & NAMED_OP)
25328 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25329 else
25331 error_at (token->location,
25332 "expected an identifier for the attribute name");
25333 return error_mark_node;
25336 attr_id = canonicalize_attr_name (attr_id);
25337 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25338 NULL_TREE);
25339 token = cp_lexer_peek_token (parser->lexer);
25341 else if (attr_ns)
25342 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25343 NULL_TREE);
25344 else
25346 attr_id = canonicalize_attr_name (attr_id);
25347 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25348 NULL_TREE);
25349 /* C++11 noreturn attribute is equivalent to GNU's. */
25350 if (is_attribute_p ("noreturn", attr_id))
25351 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25352 /* C++14 deprecated attribute is equivalent to GNU's. */
25353 else if (is_attribute_p ("deprecated", attr_id))
25354 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25355 /* C++17 fallthrough attribute is equivalent to GNU's. */
25356 else if (is_attribute_p ("fallthrough", attr_id))
25357 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25358 /* Transactional Memory TS optimize_for_synchronized attribute is
25359 equivalent to GNU transaction_callable. */
25360 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25361 TREE_PURPOSE (attribute)
25362 = get_identifier ("transaction_callable");
25363 /* Transactional Memory attributes are GNU attributes. */
25364 else if (tm_attr_to_mask (attr_id))
25365 TREE_PURPOSE (attribute) = attr_id;
25368 /* Now parse the optional argument clause of the attribute. */
25370 if (token->type != CPP_OPEN_PAREN)
25371 return attribute;
25374 vec<tree, va_gc> *vec;
25375 int attr_flag = normal_attr;
25377 if (attr_ns == gnu_identifier
25378 && attribute_takes_identifier_p (attr_id))
25379 /* A GNU attribute that takes an identifier in parameter. */
25380 attr_flag = id_attr;
25382 vec = cp_parser_parenthesized_expression_list
25383 (parser, attr_flag, /*cast_p=*/false,
25384 /*allow_expansion_p=*/true,
25385 /*non_constant_p=*/NULL);
25386 if (vec == NULL)
25387 arguments = error_mark_node;
25388 else
25390 arguments = build_tree_list_vec (vec);
25391 release_tree_vector (vec);
25394 if (arguments == error_mark_node)
25395 attribute = error_mark_node;
25396 else
25397 TREE_VALUE (attribute) = arguments;
25400 return attribute;
25403 /* Check that the attribute ATTRIBUTE appears at most once in the
25404 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25405 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25406 isn't implemented yet in GCC. */
25408 static void
25409 cp_parser_check_std_attribute (tree attributes, tree attribute)
25411 if (attributes)
25413 tree name = get_attribute_name (attribute);
25414 if (is_attribute_p ("noreturn", name)
25415 && lookup_attribute ("noreturn", attributes))
25416 error ("attribute %<noreturn%> can appear at most once "
25417 "in an attribute-list");
25418 else if (is_attribute_p ("deprecated", name)
25419 && lookup_attribute ("deprecated", attributes))
25420 error ("attribute %<deprecated%> can appear at most once "
25421 "in an attribute-list");
25425 /* Parse a list of standard C++-11 attributes.
25427 attribute-list:
25428 attribute [opt]
25429 attribute-list , attribute[opt]
25430 attribute ...
25431 attribute-list , attribute ...
25434 static tree
25435 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25437 tree attributes = NULL_TREE, attribute = NULL_TREE;
25438 cp_token *token = NULL;
25440 while (true)
25442 attribute = cp_parser_std_attribute (parser, attr_ns);
25443 if (attribute == error_mark_node)
25444 break;
25445 if (attribute != NULL_TREE)
25447 cp_parser_check_std_attribute (attributes, attribute);
25448 TREE_CHAIN (attribute) = attributes;
25449 attributes = attribute;
25451 token = cp_lexer_peek_token (parser->lexer);
25452 if (token->type == CPP_ELLIPSIS)
25454 cp_lexer_consume_token (parser->lexer);
25455 if (attribute == NULL_TREE)
25456 error_at (token->location,
25457 "expected attribute before %<...%>");
25458 else
25460 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25461 if (pack == error_mark_node)
25462 return error_mark_node;
25463 TREE_VALUE (attribute) = pack;
25465 token = cp_lexer_peek_token (parser->lexer);
25467 if (token->type != CPP_COMMA)
25468 break;
25469 cp_lexer_consume_token (parser->lexer);
25471 attributes = nreverse (attributes);
25472 return attributes;
25475 /* Parse a standard C++-11 attribute specifier.
25477 attribute-specifier:
25478 [ [ attribute-using-prefix [opt] attribute-list ] ]
25479 alignment-specifier
25481 attribute-using-prefix:
25482 using attribute-namespace :
25484 alignment-specifier:
25485 alignas ( type-id ... [opt] )
25486 alignas ( alignment-expression ... [opt] ). */
25488 static tree
25489 cp_parser_std_attribute_spec (cp_parser *parser)
25491 tree attributes = NULL_TREE;
25492 cp_token *token = cp_lexer_peek_token (parser->lexer);
25494 if (token->type == CPP_OPEN_SQUARE
25495 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25497 tree attr_ns = NULL_TREE;
25499 cp_lexer_consume_token (parser->lexer);
25500 cp_lexer_consume_token (parser->lexer);
25502 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25504 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25505 if (token->type == CPP_NAME)
25506 attr_ns = token->u.value;
25507 else if (token->type == CPP_KEYWORD)
25508 attr_ns = ridpointers[(int) token->keyword];
25509 else if (token->flags & NAMED_OP)
25510 attr_ns = get_identifier (cpp_type2name (token->type,
25511 token->flags));
25512 if (attr_ns
25513 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25515 if (cxx_dialect < cxx17
25516 && !in_system_header_at (input_location))
25517 pedwarn (input_location, 0,
25518 "attribute using prefix only available "
25519 "with -std=c++17 or -std=gnu++17");
25521 cp_lexer_consume_token (parser->lexer);
25522 cp_lexer_consume_token (parser->lexer);
25523 cp_lexer_consume_token (parser->lexer);
25525 else
25526 attr_ns = NULL_TREE;
25529 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25531 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25532 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25533 cp_parser_skip_to_end_of_statement (parser);
25534 else
25535 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25536 when we are sure that we have actually parsed them. */
25537 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25539 else
25541 tree alignas_expr;
25543 /* Look for an alignment-specifier. */
25545 token = cp_lexer_peek_token (parser->lexer);
25547 if (token->type != CPP_KEYWORD
25548 || token->keyword != RID_ALIGNAS)
25549 return NULL_TREE;
25551 cp_lexer_consume_token (parser->lexer);
25552 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25554 matching_parens parens;
25555 if (!parens.require_open (parser))
25556 return error_mark_node;
25558 cp_parser_parse_tentatively (parser);
25559 alignas_expr = cp_parser_type_id (parser);
25561 if (!cp_parser_parse_definitely (parser))
25563 alignas_expr = cp_parser_assignment_expression (parser);
25564 if (alignas_expr == error_mark_node)
25565 cp_parser_skip_to_end_of_statement (parser);
25566 if (alignas_expr == NULL_TREE
25567 || alignas_expr == error_mark_node)
25568 return alignas_expr;
25571 alignas_expr = cxx_alignas_expr (alignas_expr);
25572 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25574 /* Handle alignas (pack...). */
25575 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25577 cp_lexer_consume_token (parser->lexer);
25578 alignas_expr = make_pack_expansion (alignas_expr);
25581 /* Something went wrong, so don't build the attribute. */
25582 if (alignas_expr == error_mark_node)
25583 return error_mark_node;
25585 if (!parens.require_close (parser))
25586 return error_mark_node;
25588 /* Build the C++-11 representation of an 'aligned'
25589 attribute. */
25590 attributes
25591 = build_tree_list (build_tree_list (gnu_identifier,
25592 aligned_identifier), alignas_expr);
25595 return attributes;
25598 /* Parse a standard C++-11 attribute-specifier-seq.
25600 attribute-specifier-seq:
25601 attribute-specifier-seq [opt] attribute-specifier
25604 static tree
25605 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25607 tree attr_specs = NULL_TREE;
25608 tree attr_last = NULL_TREE;
25610 while (true)
25612 tree attr_spec = cp_parser_std_attribute_spec (parser);
25613 if (attr_spec == NULL_TREE)
25614 break;
25615 if (attr_spec == error_mark_node)
25616 return error_mark_node;
25618 if (attr_last)
25619 TREE_CHAIN (attr_last) = attr_spec;
25620 else
25621 attr_specs = attr_last = attr_spec;
25622 attr_last = tree_last (attr_last);
25625 return attr_specs;
25628 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25629 return index of the first token after balanced-token, or N on failure. */
25631 static size_t
25632 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25634 size_t orig_n = n;
25635 int nparens = 0, nbraces = 0, nsquares = 0;
25637 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25639 case CPP_EOF:
25640 case CPP_PRAGMA_EOL:
25641 /* Ran out of tokens. */
25642 return orig_n;
25643 case CPP_OPEN_PAREN:
25644 ++nparens;
25645 break;
25646 case CPP_OPEN_BRACE:
25647 ++nbraces;
25648 break;
25649 case CPP_OPEN_SQUARE:
25650 ++nsquares;
25651 break;
25652 case CPP_CLOSE_PAREN:
25653 --nparens;
25654 break;
25655 case CPP_CLOSE_BRACE:
25656 --nbraces;
25657 break;
25658 case CPP_CLOSE_SQUARE:
25659 --nsquares;
25660 break;
25661 default:
25662 break;
25664 while (nparens || nbraces || nsquares);
25665 return n;
25668 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25669 return index of the first token after the GNU attribute tokens, or N on
25670 failure. */
25672 static size_t
25673 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25675 while (true)
25677 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25678 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25679 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25680 break;
25682 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25683 if (n2 == n + 2)
25684 break;
25685 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25686 break;
25687 n = n2 + 1;
25689 return n;
25692 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25693 next token), return index of the first token after the standard C++11
25694 attribute tokens, or N on failure. */
25696 static size_t
25697 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25699 while (true)
25701 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25702 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25704 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25705 if (n2 == n + 1)
25706 break;
25707 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25708 break;
25709 n = n2 + 1;
25711 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25712 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25714 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25715 if (n2 == n + 1)
25716 break;
25717 n = n2;
25719 else
25720 break;
25722 return n;
25725 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25726 as the next token), return index of the first token after the attribute
25727 tokens, or N on failure. */
25729 static size_t
25730 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25732 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25733 return cp_parser_skip_gnu_attributes_opt (parser, n);
25734 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25737 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25738 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25739 current value of the PEDANTIC flag, regardless of whether or not
25740 the `__extension__' keyword is present. The caller is responsible
25741 for restoring the value of the PEDANTIC flag. */
25743 static bool
25744 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25746 /* Save the old value of the PEDANTIC flag. */
25747 *saved_pedantic = pedantic;
25749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25751 /* Consume the `__extension__' token. */
25752 cp_lexer_consume_token (parser->lexer);
25753 /* We're not being pedantic while the `__extension__' keyword is
25754 in effect. */
25755 pedantic = 0;
25757 return true;
25760 return false;
25763 /* Parse a label declaration.
25765 label-declaration:
25766 __label__ label-declarator-seq ;
25768 label-declarator-seq:
25769 identifier , label-declarator-seq
25770 identifier */
25772 static void
25773 cp_parser_label_declaration (cp_parser* parser)
25775 /* Look for the `__label__' keyword. */
25776 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25778 while (true)
25780 tree identifier;
25782 /* Look for an identifier. */
25783 identifier = cp_parser_identifier (parser);
25784 /* If we failed, stop. */
25785 if (identifier == error_mark_node)
25786 break;
25787 /* Declare it as a label. */
25788 finish_label_decl (identifier);
25789 /* If the next token is a `;', stop. */
25790 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25791 break;
25792 /* Look for the `,' separating the label declarations. */
25793 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25796 /* Look for the final `;'. */
25797 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25800 // -------------------------------------------------------------------------- //
25801 // Requires Clause
25803 // Parse a requires clause.
25805 // requires-clause:
25806 // 'requires' logical-or-expression
25808 // The required logical-or-expression must be a constant expression. Note
25809 // that we don't check that the expression is constepxr here. We defer until
25810 // we analyze constraints and then, we only check atomic constraints.
25811 static tree
25812 cp_parser_requires_clause (cp_parser *parser)
25814 // Parse the requires clause so that it is not automatically folded.
25815 ++processing_template_decl;
25816 tree expr = cp_parser_binary_expression (parser, false, false,
25817 PREC_NOT_OPERATOR, NULL);
25818 if (check_for_bare_parameter_packs (expr))
25819 expr = error_mark_node;
25820 --processing_template_decl;
25821 return expr;
25824 // Optionally parse a requires clause:
25825 static tree
25826 cp_parser_requires_clause_opt (cp_parser *parser)
25828 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25829 if (tok->keyword != RID_REQUIRES)
25831 if (!flag_concepts && tok->type == CPP_NAME
25832 && tok->u.value == ridpointers[RID_REQUIRES])
25834 error_at (cp_lexer_peek_token (parser->lexer)->location,
25835 "%<requires%> only available with -fconcepts");
25836 /* Parse and discard the requires-clause. */
25837 cp_lexer_consume_token (parser->lexer);
25838 cp_parser_requires_clause (parser);
25840 return NULL_TREE;
25842 cp_lexer_consume_token (parser->lexer);
25843 return cp_parser_requires_clause (parser);
25847 /*---------------------------------------------------------------------------
25848 Requires expressions
25849 ---------------------------------------------------------------------------*/
25851 /* Parse a requires expression
25853 requirement-expression:
25854 'requires' requirement-parameter-list [opt] requirement-body */
25855 static tree
25856 cp_parser_requires_expression (cp_parser *parser)
25858 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25859 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25861 /* A requires-expression shall appear only within a concept
25862 definition or a requires-clause.
25864 TODO: Implement this diagnostic correctly. */
25865 if (!processing_template_decl)
25867 error_at (loc, "a requires expression cannot appear outside a template");
25868 cp_parser_skip_to_end_of_statement (parser);
25869 return error_mark_node;
25872 tree parms, reqs;
25874 /* Local parameters are delared as variables within the scope
25875 of the expression. They are not visible past the end of
25876 the expression. Expressions within the requires-expression
25877 are unevaluated. */
25878 struct scope_sentinel
25880 scope_sentinel ()
25882 ++cp_unevaluated_operand;
25883 begin_scope (sk_block, NULL_TREE);
25886 ~scope_sentinel ()
25888 pop_bindings_and_leave_scope ();
25889 --cp_unevaluated_operand;
25891 } s;
25893 /* Parse the optional parameter list. */
25894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25896 parms = cp_parser_requirement_parameter_list (parser);
25897 if (parms == error_mark_node)
25898 return error_mark_node;
25900 else
25901 parms = NULL_TREE;
25903 /* Parse the requirement body. */
25904 reqs = cp_parser_requirement_body (parser);
25905 if (reqs == error_mark_node)
25906 return error_mark_node;
25909 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25910 the parm chain. */
25911 grokparms (parms, &parms);
25912 return finish_requires_expr (parms, reqs);
25915 /* Parse a parameterized requirement.
25917 requirement-parameter-list:
25918 '(' parameter-declaration-clause ')' */
25919 static tree
25920 cp_parser_requirement_parameter_list (cp_parser *parser)
25922 matching_parens parens;
25923 if (!parens.require_open (parser))
25924 return error_mark_node;
25926 tree parms = cp_parser_parameter_declaration_clause (parser);
25928 if (!parens.require_close (parser))
25929 return error_mark_node;
25931 return parms;
25934 /* Parse the body of a requirement.
25936 requirement-body:
25937 '{' requirement-list '}' */
25938 static tree
25939 cp_parser_requirement_body (cp_parser *parser)
25941 matching_braces braces;
25942 if (!braces.require_open (parser))
25943 return error_mark_node;
25945 tree reqs = cp_parser_requirement_list (parser);
25947 if (!braces.require_close (parser))
25948 return error_mark_node;
25950 return reqs;
25953 /* Parse a list of requirements.
25955 requirement-list:
25956 requirement
25957 requirement-list ';' requirement[opt] */
25958 static tree
25959 cp_parser_requirement_list (cp_parser *parser)
25961 tree result = NULL_TREE;
25962 while (true)
25964 tree req = cp_parser_requirement (parser);
25965 if (req == error_mark_node)
25966 return error_mark_node;
25968 result = tree_cons (NULL_TREE, req, result);
25970 /* If we see a semi-colon, consume it. */
25971 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25972 cp_lexer_consume_token (parser->lexer);
25974 /* Stop processing at the end of the list. */
25975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25976 break;
25979 /* Reverse the order of requirements so they are analyzed in
25980 declaration order. */
25981 return nreverse (result);
25984 /* Parse a syntactic requirement or type requirement.
25986 requirement:
25987 simple-requirement
25988 compound-requirement
25989 type-requirement
25990 nested-requirement */
25991 static tree
25992 cp_parser_requirement (cp_parser *parser)
25994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25995 return cp_parser_compound_requirement (parser);
25996 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25997 return cp_parser_type_requirement (parser);
25998 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25999 return cp_parser_nested_requirement (parser);
26000 else
26001 return cp_parser_simple_requirement (parser);
26004 /* Parse a simple requirement.
26006 simple-requirement:
26007 expression ';' */
26008 static tree
26009 cp_parser_simple_requirement (cp_parser *parser)
26011 tree expr = cp_parser_expression (parser, NULL, false, false);
26012 if (!expr || expr == error_mark_node)
26013 return error_mark_node;
26015 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26016 return error_mark_node;
26018 return finish_simple_requirement (expr);
26021 /* Parse a type requirement
26023 type-requirement
26024 nested-name-specifier [opt] required-type-name ';'
26026 required-type-name:
26027 type-name
26028 'template' [opt] simple-template-id */
26029 static tree
26030 cp_parser_type_requirement (cp_parser *parser)
26032 cp_lexer_consume_token (parser->lexer);
26034 // Save the scope before parsing name specifiers.
26035 tree saved_scope = parser->scope;
26036 tree saved_object_scope = parser->object_scope;
26037 tree saved_qualifying_scope = parser->qualifying_scope;
26038 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26039 cp_parser_nested_name_specifier_opt (parser,
26040 /*typename_keyword_p=*/true,
26041 /*check_dependency_p=*/false,
26042 /*type_p=*/true,
26043 /*is_declaration=*/false);
26045 tree type;
26046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26048 cp_lexer_consume_token (parser->lexer);
26049 type = cp_parser_template_id (parser,
26050 /*template_keyword_p=*/true,
26051 /*check_dependency=*/false,
26052 /*tag_type=*/none_type,
26053 /*is_declaration=*/false);
26054 type = make_typename_type (parser->scope, type, typename_type,
26055 /*complain=*/tf_error);
26057 else
26058 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26060 if (TREE_CODE (type) == TYPE_DECL)
26061 type = TREE_TYPE (type);
26063 parser->scope = saved_scope;
26064 parser->object_scope = saved_object_scope;
26065 parser->qualifying_scope = saved_qualifying_scope;
26067 if (type == error_mark_node)
26068 cp_parser_skip_to_end_of_statement (parser);
26070 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26071 return error_mark_node;
26072 if (type == error_mark_node)
26073 return error_mark_node;
26075 return finish_type_requirement (type);
26078 /* Parse a compound requirement
26080 compound-requirement:
26081 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26082 static tree
26083 cp_parser_compound_requirement (cp_parser *parser)
26085 /* Parse an expression enclosed in '{ }'s. */
26086 matching_braces braces;
26087 if (!braces.require_open (parser))
26088 return error_mark_node;
26090 tree expr = cp_parser_expression (parser, NULL, false, false);
26091 if (!expr || expr == error_mark_node)
26092 return error_mark_node;
26094 if (!braces.require_close (parser))
26095 return error_mark_node;
26097 /* Parse the optional noexcept. */
26098 bool noexcept_p = false;
26099 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26101 cp_lexer_consume_token (parser->lexer);
26102 noexcept_p = true;
26105 /* Parse the optional trailing return type. */
26106 tree type = NULL_TREE;
26107 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26109 cp_lexer_consume_token (parser->lexer);
26110 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26111 parser->in_result_type_constraint_p = true;
26112 type = cp_parser_trailing_type_id (parser);
26113 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26114 if (type == error_mark_node)
26115 return error_mark_node;
26118 return finish_compound_requirement (expr, type, noexcept_p);
26121 /* Parse a nested requirement. This is the same as a requires clause.
26123 nested-requirement:
26124 requires-clause */
26125 static tree
26126 cp_parser_nested_requirement (cp_parser *parser)
26128 cp_lexer_consume_token (parser->lexer);
26129 tree req = cp_parser_requires_clause (parser);
26130 if (req == error_mark_node)
26131 return error_mark_node;
26132 return finish_nested_requirement (req);
26135 /* Support Functions */
26137 /* Return the appropriate prefer_type argument for lookup_name_real based on
26138 tag_type and template_mem_access. */
26140 static inline int
26141 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26143 /* DR 141: When looking in the current enclosing context for a template-name
26144 after -> or ., only consider class templates. */
26145 if (template_mem_access)
26146 return 2;
26147 switch (tag_type)
26149 case none_type: return 0; // No preference.
26150 case scope_type: return 1; // Type or namespace.
26151 default: return 2; // Type only.
26155 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26156 NAME should have one of the representations used for an
26157 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26158 is returned. If PARSER->SCOPE is a dependent type, then a
26159 SCOPE_REF is returned.
26161 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26162 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26163 was formed. Abstractly, such entities should not be passed to this
26164 function, because they do not need to be looked up, but it is
26165 simpler to check for this special case here, rather than at the
26166 call-sites.
26168 In cases not explicitly covered above, this function returns a
26169 DECL, OVERLOAD, or baselink representing the result of the lookup.
26170 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26171 is returned.
26173 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26174 (e.g., "struct") that was used. In that case bindings that do not
26175 refer to types are ignored.
26177 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26178 ignored.
26180 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26181 are ignored.
26183 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26184 types.
26186 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26187 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26188 NULL_TREE otherwise. */
26190 static cp_expr
26191 cp_parser_lookup_name (cp_parser *parser, tree name,
26192 enum tag_types tag_type,
26193 bool is_template,
26194 bool is_namespace,
26195 bool check_dependency,
26196 tree *ambiguous_decls,
26197 location_t name_location)
26199 tree decl;
26200 tree object_type = parser->context->object_type;
26202 /* Assume that the lookup will be unambiguous. */
26203 if (ambiguous_decls)
26204 *ambiguous_decls = NULL_TREE;
26206 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26207 no longer valid. Note that if we are parsing tentatively, and
26208 the parse fails, OBJECT_TYPE will be automatically restored. */
26209 parser->context->object_type = NULL_TREE;
26211 if (name == error_mark_node)
26212 return error_mark_node;
26214 /* A template-id has already been resolved; there is no lookup to
26215 do. */
26216 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26217 return name;
26218 if (BASELINK_P (name))
26220 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26221 == TEMPLATE_ID_EXPR);
26222 return name;
26225 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26226 it should already have been checked to make sure that the name
26227 used matches the type being destroyed. */
26228 if (TREE_CODE (name) == BIT_NOT_EXPR)
26230 tree type;
26232 /* Figure out to which type this destructor applies. */
26233 if (parser->scope)
26234 type = parser->scope;
26235 else if (object_type)
26236 type = object_type;
26237 else
26238 type = current_class_type;
26239 /* If that's not a class type, there is no destructor. */
26240 if (!type || !CLASS_TYPE_P (type))
26241 return error_mark_node;
26243 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26244 lazily_declare_fn (sfk_destructor, type);
26246 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26247 return dtor;
26249 return error_mark_node;
26252 /* By this point, the NAME should be an ordinary identifier. If
26253 the id-expression was a qualified name, the qualifying scope is
26254 stored in PARSER->SCOPE at this point. */
26255 gcc_assert (identifier_p (name));
26257 /* Perform the lookup. */
26258 if (parser->scope)
26260 bool dependent_p;
26262 if (parser->scope == error_mark_node)
26263 return error_mark_node;
26265 /* If the SCOPE is dependent, the lookup must be deferred until
26266 the template is instantiated -- unless we are explicitly
26267 looking up names in uninstantiated templates. Even then, we
26268 cannot look up the name if the scope is not a class type; it
26269 might, for example, be a template type parameter. */
26270 dependent_p = (TYPE_P (parser->scope)
26271 && dependent_scope_p (parser->scope));
26272 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26273 && dependent_p)
26274 /* Defer lookup. */
26275 decl = error_mark_node;
26276 else
26278 tree pushed_scope = NULL_TREE;
26280 /* If PARSER->SCOPE is a dependent type, then it must be a
26281 class type, and we must not be checking dependencies;
26282 otherwise, we would have processed this lookup above. So
26283 that PARSER->SCOPE is not considered a dependent base by
26284 lookup_member, we must enter the scope here. */
26285 if (dependent_p)
26286 pushed_scope = push_scope (parser->scope);
26288 /* If the PARSER->SCOPE is a template specialization, it
26289 may be instantiated during name lookup. In that case,
26290 errors may be issued. Even if we rollback the current
26291 tentative parse, those errors are valid. */
26292 decl = lookup_qualified_name (parser->scope, name,
26293 prefer_type_arg (tag_type),
26294 /*complain=*/true);
26296 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26297 lookup result and the nested-name-specifier nominates a class C:
26298 * if the name specified after the nested-name-specifier, when
26299 looked up in C, is the injected-class-name of C (Clause 9), or
26300 * if the name specified after the nested-name-specifier is the
26301 same as the identifier or the simple-template-id's template-
26302 name in the last component of the nested-name-specifier,
26303 the name is instead considered to name the constructor of
26304 class C. [ Note: for example, the constructor is not an
26305 acceptable lookup result in an elaborated-type-specifier so
26306 the constructor would not be used in place of the
26307 injected-class-name. --end note ] Such a constructor name
26308 shall be used only in the declarator-id of a declaration that
26309 names a constructor or in a using-declaration. */
26310 if (tag_type == none_type
26311 && DECL_SELF_REFERENCE_P (decl)
26312 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26313 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26314 prefer_type_arg (tag_type),
26315 /*complain=*/true);
26317 /* If we have a single function from a using decl, pull it out. */
26318 if (TREE_CODE (decl) == OVERLOAD
26319 && !really_overloaded_fn (decl))
26320 decl = OVL_FUNCTION (decl);
26322 if (pushed_scope)
26323 pop_scope (pushed_scope);
26326 /* If the scope is a dependent type and either we deferred lookup or
26327 we did lookup but didn't find the name, rememeber the name. */
26328 if (decl == error_mark_node && TYPE_P (parser->scope)
26329 && dependent_type_p (parser->scope))
26331 if (tag_type)
26333 tree type;
26335 /* The resolution to Core Issue 180 says that `struct
26336 A::B' should be considered a type-name, even if `A'
26337 is dependent. */
26338 type = make_typename_type (parser->scope, name, tag_type,
26339 /*complain=*/tf_error);
26340 if (type != error_mark_node)
26341 decl = TYPE_NAME (type);
26343 else if (is_template
26344 && (cp_parser_next_token_ends_template_argument_p (parser)
26345 || cp_lexer_next_token_is (parser->lexer,
26346 CPP_CLOSE_PAREN)))
26347 decl = make_unbound_class_template (parser->scope,
26348 name, NULL_TREE,
26349 /*complain=*/tf_error);
26350 else
26351 decl = build_qualified_name (/*type=*/NULL_TREE,
26352 parser->scope, name,
26353 is_template);
26355 parser->qualifying_scope = parser->scope;
26356 parser->object_scope = NULL_TREE;
26358 else if (object_type)
26360 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26361 OBJECT_TYPE is not a class. */
26362 if (CLASS_TYPE_P (object_type))
26363 /* If the OBJECT_TYPE is a template specialization, it may
26364 be instantiated during name lookup. In that case, errors
26365 may be issued. Even if we rollback the current tentative
26366 parse, those errors are valid. */
26367 decl = lookup_member (object_type,
26368 name,
26369 /*protect=*/0,
26370 prefer_type_arg (tag_type),
26371 tf_warning_or_error);
26372 else
26373 decl = NULL_TREE;
26375 if (!decl)
26376 /* Look it up in the enclosing context. DR 141: When looking for a
26377 template-name after -> or ., only consider class templates. */
26378 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26379 /*nonclass=*/0,
26380 /*block_p=*/true, is_namespace, 0);
26381 if (object_type == unknown_type_node)
26382 /* The object is type-dependent, so we can't look anything up; we used
26383 this to get the DR 141 behavior. */
26384 object_type = NULL_TREE;
26385 parser->object_scope = object_type;
26386 parser->qualifying_scope = NULL_TREE;
26388 else
26390 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26391 /*nonclass=*/0,
26392 /*block_p=*/true, is_namespace, 0);
26393 parser->qualifying_scope = NULL_TREE;
26394 parser->object_scope = NULL_TREE;
26397 /* If the lookup failed, let our caller know. */
26398 if (!decl || decl == error_mark_node)
26399 return error_mark_node;
26401 /* Pull out the template from an injected-class-name (or multiple). */
26402 if (is_template)
26403 decl = maybe_get_template_decl_from_type_decl (decl);
26405 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26406 if (TREE_CODE (decl) == TREE_LIST)
26408 if (ambiguous_decls)
26409 *ambiguous_decls = decl;
26410 /* The error message we have to print is too complicated for
26411 cp_parser_error, so we incorporate its actions directly. */
26412 if (!cp_parser_simulate_error (parser))
26414 error_at (name_location, "reference to %qD is ambiguous",
26415 name);
26416 print_candidates (decl);
26418 return error_mark_node;
26421 gcc_assert (DECL_P (decl)
26422 || TREE_CODE (decl) == OVERLOAD
26423 || TREE_CODE (decl) == SCOPE_REF
26424 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26425 || BASELINK_P (decl));
26427 /* If we have resolved the name of a member declaration, check to
26428 see if the declaration is accessible. When the name resolves to
26429 set of overloaded functions, accessibility is checked when
26430 overload resolution is done.
26432 During an explicit instantiation, access is not checked at all,
26433 as per [temp.explicit]. */
26434 if (DECL_P (decl))
26435 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26437 maybe_record_typedef_use (decl);
26439 return cp_expr (decl, name_location);
26442 /* Like cp_parser_lookup_name, but for use in the typical case where
26443 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26444 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26446 static tree
26447 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26449 return cp_parser_lookup_name (parser, name,
26450 none_type,
26451 /*is_template=*/false,
26452 /*is_namespace=*/false,
26453 /*check_dependency=*/true,
26454 /*ambiguous_decls=*/NULL,
26455 location);
26458 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26459 the current context, return the TYPE_DECL. If TAG_NAME_P is
26460 true, the DECL indicates the class being defined in a class-head,
26461 or declared in an elaborated-type-specifier.
26463 Otherwise, return DECL. */
26465 static tree
26466 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26468 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26469 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26471 struct A {
26472 template <typename T> struct B;
26475 template <typename T> struct A::B {};
26477 Similarly, in an elaborated-type-specifier:
26479 namespace N { struct X{}; }
26481 struct A {
26482 template <typename T> friend struct N::X;
26485 However, if the DECL refers to a class type, and we are in
26486 the scope of the class, then the name lookup automatically
26487 finds the TYPE_DECL created by build_self_reference rather
26488 than a TEMPLATE_DECL. For example, in:
26490 template <class T> struct S {
26491 S s;
26494 there is no need to handle such case. */
26496 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26497 return DECL_TEMPLATE_RESULT (decl);
26499 return decl;
26502 /* If too many, or too few, template-parameter lists apply to the
26503 declarator, issue an error message. Returns TRUE if all went well,
26504 and FALSE otherwise. */
26506 static bool
26507 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26508 cp_declarator *declarator,
26509 location_t declarator_location)
26511 switch (declarator->kind)
26513 case cdk_id:
26515 unsigned num_templates = 0;
26516 tree scope = declarator->u.id.qualifying_scope;
26517 bool template_id_p = false;
26519 if (scope)
26520 num_templates = num_template_headers_for_class (scope);
26521 else if (TREE_CODE (declarator->u.id.unqualified_name)
26522 == TEMPLATE_ID_EXPR)
26524 /* If the DECLARATOR has the form `X<y>' then it uses one
26525 additional level of template parameters. */
26526 ++num_templates;
26527 template_id_p = true;
26530 return cp_parser_check_template_parameters
26531 (parser, num_templates, template_id_p, declarator_location,
26532 declarator);
26535 case cdk_function:
26536 case cdk_array:
26537 case cdk_pointer:
26538 case cdk_reference:
26539 case cdk_ptrmem:
26540 return (cp_parser_check_declarator_template_parameters
26541 (parser, declarator->declarator, declarator_location));
26543 case cdk_decomp:
26544 case cdk_error:
26545 return true;
26547 default:
26548 gcc_unreachable ();
26550 return false;
26553 /* NUM_TEMPLATES were used in the current declaration. If that is
26554 invalid, return FALSE and issue an error messages. Otherwise,
26555 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26556 declarator and we can print more accurate diagnostics. */
26558 static bool
26559 cp_parser_check_template_parameters (cp_parser* parser,
26560 unsigned num_templates,
26561 bool template_id_p,
26562 location_t location,
26563 cp_declarator *declarator)
26565 /* If there are the same number of template classes and parameter
26566 lists, that's OK. */
26567 if (parser->num_template_parameter_lists == num_templates)
26568 return true;
26569 /* If there are more, but only one more, and the name ends in an identifier,
26570 then we are declaring a primary template. That's OK too. */
26571 if (!template_id_p
26572 && parser->num_template_parameter_lists == num_templates + 1)
26573 return true;
26574 /* If there are more template classes than parameter lists, we have
26575 something like:
26577 template <class T> void S<T>::R<T>::f (); */
26578 if (parser->num_template_parameter_lists < num_templates)
26580 if (declarator && !current_function_decl)
26581 error_at (location, "specializing member %<%T::%E%> "
26582 "requires %<template<>%> syntax",
26583 declarator->u.id.qualifying_scope,
26584 declarator->u.id.unqualified_name);
26585 else if (declarator)
26586 error_at (location, "invalid declaration of %<%T::%E%>",
26587 declarator->u.id.qualifying_scope,
26588 declarator->u.id.unqualified_name);
26589 else
26590 error_at (location, "too few template-parameter-lists");
26591 return false;
26593 /* Otherwise, there are too many template parameter lists. We have
26594 something like:
26596 template <class T> template <class U> void S::f(); */
26597 error_at (location, "too many template-parameter-lists");
26598 return false;
26601 /* Parse an optional `::' token indicating that the following name is
26602 from the global namespace. If so, PARSER->SCOPE is set to the
26603 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26604 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26605 Returns the new value of PARSER->SCOPE, if the `::' token is
26606 present, and NULL_TREE otherwise. */
26608 static tree
26609 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26611 cp_token *token;
26613 /* Peek at the next token. */
26614 token = cp_lexer_peek_token (parser->lexer);
26615 /* If we're looking at a `::' token then we're starting from the
26616 global namespace, not our current location. */
26617 if (token->type == CPP_SCOPE)
26619 /* Consume the `::' token. */
26620 cp_lexer_consume_token (parser->lexer);
26621 /* Set the SCOPE so that we know where to start the lookup. */
26622 parser->scope = global_namespace;
26623 parser->qualifying_scope = global_namespace;
26624 parser->object_scope = NULL_TREE;
26626 return parser->scope;
26628 else if (!current_scope_valid_p)
26630 parser->scope = NULL_TREE;
26631 parser->qualifying_scope = NULL_TREE;
26632 parser->object_scope = NULL_TREE;
26635 return NULL_TREE;
26638 /* Returns TRUE if the upcoming token sequence is the start of a
26639 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26640 declarator is preceded by the `friend' specifier. */
26642 static bool
26643 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26645 bool constructor_p;
26646 bool outside_class_specifier_p;
26647 tree nested_name_specifier;
26648 cp_token *next_token;
26650 /* The common case is that this is not a constructor declarator, so
26651 try to avoid doing lots of work if at all possible. It's not
26652 valid declare a constructor at function scope. */
26653 if (parser->in_function_body)
26654 return false;
26655 /* And only certain tokens can begin a constructor declarator. */
26656 next_token = cp_lexer_peek_token (parser->lexer);
26657 if (next_token->type != CPP_NAME
26658 && next_token->type != CPP_SCOPE
26659 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26660 && next_token->type != CPP_TEMPLATE_ID)
26661 return false;
26663 /* Parse tentatively; we are going to roll back all of the tokens
26664 consumed here. */
26665 cp_parser_parse_tentatively (parser);
26666 /* Assume that we are looking at a constructor declarator. */
26667 constructor_p = true;
26669 /* Look for the optional `::' operator. */
26670 cp_parser_global_scope_opt (parser,
26671 /*current_scope_valid_p=*/false);
26672 /* Look for the nested-name-specifier. */
26673 nested_name_specifier
26674 = (cp_parser_nested_name_specifier_opt (parser,
26675 /*typename_keyword_p=*/false,
26676 /*check_dependency_p=*/false,
26677 /*type_p=*/false,
26678 /*is_declaration=*/false));
26680 outside_class_specifier_p = (!at_class_scope_p ()
26681 || !TYPE_BEING_DEFINED (current_class_type)
26682 || friend_p);
26684 /* Outside of a class-specifier, there must be a
26685 nested-name-specifier. Except in C++17 mode, where we
26686 might be declaring a guiding declaration. */
26687 if (!nested_name_specifier && outside_class_specifier_p
26688 && cxx_dialect < cxx17)
26689 constructor_p = false;
26690 else if (nested_name_specifier == error_mark_node)
26691 constructor_p = false;
26693 /* If we have a class scope, this is easy; DR 147 says that S::S always
26694 names the constructor, and no other qualified name could. */
26695 if (constructor_p && nested_name_specifier
26696 && CLASS_TYPE_P (nested_name_specifier))
26698 tree id = cp_parser_unqualified_id (parser,
26699 /*template_keyword_p=*/false,
26700 /*check_dependency_p=*/false,
26701 /*declarator_p=*/true,
26702 /*optional_p=*/false);
26703 if (is_overloaded_fn (id))
26704 id = DECL_NAME (get_first_fn (id));
26705 if (!constructor_name_p (id, nested_name_specifier))
26706 constructor_p = false;
26708 /* If we still think that this might be a constructor-declarator,
26709 look for a class-name. */
26710 else if (constructor_p)
26712 /* If we have:
26714 template <typename T> struct S {
26715 S();
26718 we must recognize that the nested `S' names a class. */
26719 if (cxx_dialect >= cxx17)
26720 cp_parser_parse_tentatively (parser);
26722 tree type_decl;
26723 type_decl = cp_parser_class_name (parser,
26724 /*typename_keyword_p=*/false,
26725 /*template_keyword_p=*/false,
26726 none_type,
26727 /*check_dependency_p=*/false,
26728 /*class_head_p=*/false,
26729 /*is_declaration=*/false);
26731 if (cxx_dialect >= cxx17
26732 && !cp_parser_parse_definitely (parser))
26734 type_decl = NULL_TREE;
26735 tree tmpl = cp_parser_template_name (parser,
26736 /*template_keyword*/false,
26737 /*check_dependency_p*/false,
26738 /*is_declaration*/false,
26739 none_type,
26740 /*is_identifier*/NULL);
26741 if (DECL_CLASS_TEMPLATE_P (tmpl)
26742 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26743 /* It's a deduction guide, return true. */;
26744 else
26745 cp_parser_simulate_error (parser);
26748 /* If there was no class-name, then this is not a constructor.
26749 Otherwise, if we are in a class-specifier and we aren't
26750 handling a friend declaration, check that its type matches
26751 current_class_type (c++/38313). Note: error_mark_node
26752 is left alone for error recovery purposes. */
26753 constructor_p = (!cp_parser_error_occurred (parser)
26754 && (outside_class_specifier_p
26755 || type_decl == NULL_TREE
26756 || type_decl == error_mark_node
26757 || same_type_p (current_class_type,
26758 TREE_TYPE (type_decl))));
26760 /* If we're still considering a constructor, we have to see a `(',
26761 to begin the parameter-declaration-clause, followed by either a
26762 `)', an `...', or a decl-specifier. We need to check for a
26763 type-specifier to avoid being fooled into thinking that:
26765 S (f) (int);
26767 is a constructor. (It is actually a function named `f' that
26768 takes one parameter (of type `int') and returns a value of type
26769 `S'. */
26770 if (constructor_p
26771 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26772 constructor_p = false;
26774 if (constructor_p
26775 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26776 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26777 /* A parameter declaration begins with a decl-specifier,
26778 which is either the "attribute" keyword, a storage class
26779 specifier, or (usually) a type-specifier. */
26780 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26782 tree type;
26783 tree pushed_scope = NULL_TREE;
26784 unsigned saved_num_template_parameter_lists;
26786 /* Names appearing in the type-specifier should be looked up
26787 in the scope of the class. */
26788 if (current_class_type)
26789 type = NULL_TREE;
26790 else if (type_decl)
26792 type = TREE_TYPE (type_decl);
26793 if (TREE_CODE (type) == TYPENAME_TYPE)
26795 type = resolve_typename_type (type,
26796 /*only_current_p=*/false);
26797 if (TREE_CODE (type) == TYPENAME_TYPE)
26799 cp_parser_abort_tentative_parse (parser);
26800 return false;
26803 pushed_scope = push_scope (type);
26806 /* Inside the constructor parameter list, surrounding
26807 template-parameter-lists do not apply. */
26808 saved_num_template_parameter_lists
26809 = parser->num_template_parameter_lists;
26810 parser->num_template_parameter_lists = 0;
26812 /* Look for the type-specifier. */
26813 cp_parser_type_specifier (parser,
26814 CP_PARSER_FLAGS_NONE,
26815 /*decl_specs=*/NULL,
26816 /*is_declarator=*/true,
26817 /*declares_class_or_enum=*/NULL,
26818 /*is_cv_qualifier=*/NULL);
26820 parser->num_template_parameter_lists
26821 = saved_num_template_parameter_lists;
26823 /* Leave the scope of the class. */
26824 if (pushed_scope)
26825 pop_scope (pushed_scope);
26827 constructor_p = !cp_parser_error_occurred (parser);
26831 /* We did not really want to consume any tokens. */
26832 cp_parser_abort_tentative_parse (parser);
26834 return constructor_p;
26837 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26838 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26839 they must be performed once we are in the scope of the function.
26841 Returns the function defined. */
26843 static tree
26844 cp_parser_function_definition_from_specifiers_and_declarator
26845 (cp_parser* parser,
26846 cp_decl_specifier_seq *decl_specifiers,
26847 tree attributes,
26848 const cp_declarator *declarator)
26850 tree fn;
26851 bool success_p;
26853 /* Begin the function-definition. */
26854 success_p = start_function (decl_specifiers, declarator, attributes);
26856 /* The things we're about to see are not directly qualified by any
26857 template headers we've seen thus far. */
26858 reset_specialization ();
26860 /* If there were names looked up in the decl-specifier-seq that we
26861 did not check, check them now. We must wait until we are in the
26862 scope of the function to perform the checks, since the function
26863 might be a friend. */
26864 perform_deferred_access_checks (tf_warning_or_error);
26866 if (success_p)
26868 cp_finalize_omp_declare_simd (parser, current_function_decl);
26869 parser->omp_declare_simd = NULL;
26870 cp_finalize_oacc_routine (parser, current_function_decl, true);
26871 parser->oacc_routine = NULL;
26874 if (!success_p)
26876 /* Skip the entire function. */
26877 cp_parser_skip_to_end_of_block_or_statement (parser);
26878 fn = error_mark_node;
26880 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26882 /* Seen already, skip it. An error message has already been output. */
26883 cp_parser_skip_to_end_of_block_or_statement (parser);
26884 fn = current_function_decl;
26885 current_function_decl = NULL_TREE;
26886 /* If this is a function from a class, pop the nested class. */
26887 if (current_class_name)
26888 pop_nested_class ();
26890 else
26892 timevar_id_t tv;
26893 if (DECL_DECLARED_INLINE_P (current_function_decl))
26894 tv = TV_PARSE_INLINE;
26895 else
26896 tv = TV_PARSE_FUNC;
26897 timevar_push (tv);
26898 fn = cp_parser_function_definition_after_declarator (parser,
26899 /*inline_p=*/false);
26900 timevar_pop (tv);
26903 return fn;
26906 /* Parse the part of a function-definition that follows the
26907 declarator. INLINE_P is TRUE iff this function is an inline
26908 function defined within a class-specifier.
26910 Returns the function defined. */
26912 static tree
26913 cp_parser_function_definition_after_declarator (cp_parser* parser,
26914 bool inline_p)
26916 tree fn;
26917 bool saved_in_unbraced_linkage_specification_p;
26918 bool saved_in_function_body;
26919 unsigned saved_num_template_parameter_lists;
26920 cp_token *token;
26921 bool fully_implicit_function_template_p
26922 = parser->fully_implicit_function_template_p;
26923 parser->fully_implicit_function_template_p = false;
26924 tree implicit_template_parms
26925 = parser->implicit_template_parms;
26926 parser->implicit_template_parms = 0;
26927 cp_binding_level* implicit_template_scope
26928 = parser->implicit_template_scope;
26929 parser->implicit_template_scope = 0;
26931 saved_in_function_body = parser->in_function_body;
26932 parser->in_function_body = true;
26933 /* If the next token is `return', then the code may be trying to
26934 make use of the "named return value" extension that G++ used to
26935 support. */
26936 token = cp_lexer_peek_token (parser->lexer);
26937 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26939 /* Consume the `return' keyword. */
26940 cp_lexer_consume_token (parser->lexer);
26941 /* Look for the identifier that indicates what value is to be
26942 returned. */
26943 cp_parser_identifier (parser);
26944 /* Issue an error message. */
26945 error_at (token->location,
26946 "named return values are no longer supported");
26947 /* Skip tokens until we reach the start of the function body. */
26948 while (true)
26950 cp_token *token = cp_lexer_peek_token (parser->lexer);
26951 if (token->type == CPP_OPEN_BRACE
26952 || token->type == CPP_EOF
26953 || token->type == CPP_PRAGMA_EOL)
26954 break;
26955 cp_lexer_consume_token (parser->lexer);
26958 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26959 anything declared inside `f'. */
26960 saved_in_unbraced_linkage_specification_p
26961 = parser->in_unbraced_linkage_specification_p;
26962 parser->in_unbraced_linkage_specification_p = false;
26963 /* Inside the function, surrounding template-parameter-lists do not
26964 apply. */
26965 saved_num_template_parameter_lists
26966 = parser->num_template_parameter_lists;
26967 parser->num_template_parameter_lists = 0;
26969 /* If the next token is `try', `__transaction_atomic', or
26970 `__transaction_relaxed`, then we are looking at either function-try-block
26971 or function-transaction-block. Note that all of these include the
26972 function-body. */
26973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26974 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26975 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26976 RID_TRANSACTION_RELAXED))
26977 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26978 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26979 cp_parser_function_try_block (parser);
26980 else
26981 cp_parser_ctor_initializer_opt_and_function_body
26982 (parser, /*in_function_try_block=*/false);
26984 /* Finish the function. */
26985 fn = finish_function (inline_p);
26986 /* Generate code for it, if necessary. */
26987 expand_or_defer_fn (fn);
26988 /* Restore the saved values. */
26989 parser->in_unbraced_linkage_specification_p
26990 = saved_in_unbraced_linkage_specification_p;
26991 parser->num_template_parameter_lists
26992 = saved_num_template_parameter_lists;
26993 parser->in_function_body = saved_in_function_body;
26995 parser->fully_implicit_function_template_p
26996 = fully_implicit_function_template_p;
26997 parser->implicit_template_parms
26998 = implicit_template_parms;
26999 parser->implicit_template_scope
27000 = implicit_template_scope;
27002 if (parser->fully_implicit_function_template_p)
27003 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27005 return fn;
27008 /* Parse a template-declaration body (following argument list). */
27010 static void
27011 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27012 tree parameter_list,
27013 bool member_p)
27015 tree decl = NULL_TREE;
27016 bool friend_p = false;
27018 /* We just processed one more parameter list. */
27019 ++parser->num_template_parameter_lists;
27021 /* Get the deferred access checks from the parameter list. These
27022 will be checked once we know what is being declared, as for a
27023 member template the checks must be performed in the scope of the
27024 class containing the member. */
27025 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27027 /* Tentatively parse for a new template parameter list, which can either be
27028 the template keyword or a template introduction. */
27029 if (cp_parser_template_declaration_after_export (parser, member_p))
27030 /* OK */;
27031 else if (cxx_dialect >= cxx11
27032 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27033 decl = cp_parser_alias_declaration (parser);
27034 else
27036 /* There are no access checks when parsing a template, as we do not
27037 know if a specialization will be a friend. */
27038 push_deferring_access_checks (dk_no_check);
27039 cp_token *token = cp_lexer_peek_token (parser->lexer);
27040 decl = cp_parser_single_declaration (parser,
27041 checks,
27042 member_p,
27043 /*explicit_specialization_p=*/false,
27044 &friend_p);
27045 pop_deferring_access_checks ();
27047 /* If this is a member template declaration, let the front
27048 end know. */
27049 if (member_p && !friend_p && decl)
27051 if (TREE_CODE (decl) == TYPE_DECL)
27052 cp_parser_check_access_in_redeclaration (decl, token->location);
27054 decl = finish_member_template_decl (decl);
27056 else if (friend_p && decl
27057 && DECL_DECLARES_TYPE_P (decl))
27058 make_friend_class (current_class_type, TREE_TYPE (decl),
27059 /*complain=*/true);
27061 /* We are done with the current parameter list. */
27062 --parser->num_template_parameter_lists;
27064 pop_deferring_access_checks ();
27066 /* Finish up. */
27067 finish_template_decl (parameter_list);
27069 /* Check the template arguments for a literal operator template. */
27070 if (decl
27071 && DECL_DECLARES_FUNCTION_P (decl)
27072 && UDLIT_OPER_P (DECL_NAME (decl)))
27074 bool ok = true;
27075 if (parameter_list == NULL_TREE)
27076 ok = false;
27077 else
27079 int num_parms = TREE_VEC_LENGTH (parameter_list);
27080 if (num_parms == 1)
27082 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27083 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27084 if (TREE_TYPE (parm) != char_type_node
27085 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27086 ok = false;
27088 else if (num_parms == 2 && cxx_dialect >= cxx14)
27090 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27091 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27092 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27093 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27094 if (parm == error_mark_node
27095 || TREE_TYPE (parm) != TREE_TYPE (type)
27096 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27097 ok = false;
27099 else
27100 ok = false;
27102 if (!ok)
27104 if (cxx_dialect >= cxx14)
27105 error ("literal operator template %qD has invalid parameter list."
27106 " Expected non-type template argument pack <char...>"
27107 " or <typename CharT, CharT...>",
27108 decl);
27109 else
27110 error ("literal operator template %qD has invalid parameter list."
27111 " Expected non-type template argument pack <char...>",
27112 decl);
27116 /* Register member declarations. */
27117 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27118 finish_member_declaration (decl);
27119 /* If DECL is a function template, we must return to parse it later.
27120 (Even though there is no definition, there might be default
27121 arguments that need handling.) */
27122 if (member_p && decl
27123 && DECL_DECLARES_FUNCTION_P (decl))
27124 vec_safe_push (unparsed_funs_with_definitions, decl);
27127 /* Parse a template introduction header for a template-declaration. Returns
27128 false if tentative parse fails. */
27130 static bool
27131 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27133 cp_parser_parse_tentatively (parser);
27135 tree saved_scope = parser->scope;
27136 tree saved_object_scope = parser->object_scope;
27137 tree saved_qualifying_scope = parser->qualifying_scope;
27139 /* Look for the optional `::' operator. */
27140 cp_parser_global_scope_opt (parser,
27141 /*current_scope_valid_p=*/false);
27142 /* Look for the nested-name-specifier. */
27143 cp_parser_nested_name_specifier_opt (parser,
27144 /*typename_keyword_p=*/false,
27145 /*check_dependency_p=*/true,
27146 /*type_p=*/false,
27147 /*is_declaration=*/false);
27149 cp_token *token = cp_lexer_peek_token (parser->lexer);
27150 tree concept_name = cp_parser_identifier (parser);
27152 /* Look up the concept for which we will be matching
27153 template parameters. */
27154 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27155 token->location);
27156 parser->scope = saved_scope;
27157 parser->object_scope = saved_object_scope;
27158 parser->qualifying_scope = saved_qualifying_scope;
27160 if (concept_name == error_mark_node)
27161 cp_parser_simulate_error (parser);
27163 /* Look for opening brace for introduction. */
27164 matching_braces braces;
27165 braces.require_open (parser);
27167 if (!cp_parser_parse_definitely (parser))
27168 return false;
27170 push_deferring_access_checks (dk_deferred);
27172 /* Build vector of placeholder parameters and grab
27173 matching identifiers. */
27174 tree introduction_list = cp_parser_introduction_list (parser);
27176 /* The introduction-list shall not be empty. */
27177 int nargs = TREE_VEC_LENGTH (introduction_list);
27178 if (nargs == 0)
27180 error ("empty introduction-list");
27181 return true;
27184 /* Look for closing brace for introduction. */
27185 if (!braces.require_close (parser))
27186 return true;
27188 if (tmpl_decl == error_mark_node)
27190 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27191 token->location);
27192 return true;
27195 /* Build and associate the constraint. */
27196 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27197 if (parms && parms != error_mark_node)
27199 cp_parser_template_declaration_after_parameters (parser, parms,
27200 member_p);
27201 return true;
27204 error_at (token->location, "no matching concept for template-introduction");
27205 return true;
27208 /* Parse a normal template-declaration following the template keyword. */
27210 static void
27211 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27213 tree parameter_list;
27214 bool need_lang_pop;
27215 location_t location = input_location;
27217 /* Look for the `<' token. */
27218 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27219 return;
27220 if (at_class_scope_p () && current_function_decl)
27222 /* 14.5.2.2 [temp.mem]
27224 A local class shall not have member templates. */
27225 error_at (location,
27226 "invalid declaration of member template in local class");
27227 cp_parser_skip_to_end_of_block_or_statement (parser);
27228 return;
27230 /* [temp]
27232 A template ... shall not have C linkage. */
27233 if (current_lang_name == lang_name_c)
27235 error_at (location, "template with C linkage");
27236 maybe_show_extern_c_location ();
27237 /* Give it C++ linkage to avoid confusing other parts of the
27238 front end. */
27239 push_lang_context (lang_name_cplusplus);
27240 need_lang_pop = true;
27242 else
27243 need_lang_pop = false;
27245 /* We cannot perform access checks on the template parameter
27246 declarations until we know what is being declared, just as we
27247 cannot check the decl-specifier list. */
27248 push_deferring_access_checks (dk_deferred);
27250 /* If the next token is `>', then we have an invalid
27251 specialization. Rather than complain about an invalid template
27252 parameter, issue an error message here. */
27253 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27255 cp_parser_error (parser, "invalid explicit specialization");
27256 begin_specialization ();
27257 parameter_list = NULL_TREE;
27259 else
27261 /* Parse the template parameters. */
27262 parameter_list = cp_parser_template_parameter_list (parser);
27265 /* Look for the `>'. */
27266 cp_parser_skip_to_end_of_template_parameter_list (parser);
27268 /* Manage template requirements */
27269 if (flag_concepts)
27271 tree reqs = get_shorthand_constraints (current_template_parms);
27272 if (tree r = cp_parser_requires_clause_opt (parser))
27273 reqs = conjoin_constraints (reqs, normalize_expression (r));
27274 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27277 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27278 member_p);
27280 /* For the erroneous case of a template with C linkage, we pushed an
27281 implicit C++ linkage scope; exit that scope now. */
27282 if (need_lang_pop)
27283 pop_lang_context ();
27286 /* Parse a template-declaration, assuming that the `export' (and
27287 `extern') keywords, if present, has already been scanned. MEMBER_P
27288 is as for cp_parser_template_declaration. */
27290 static bool
27291 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27295 cp_lexer_consume_token (parser->lexer);
27296 cp_parser_explicit_template_declaration (parser, member_p);
27297 return true;
27299 else if (flag_concepts)
27300 return cp_parser_template_introduction (parser, member_p);
27302 return false;
27305 /* Perform the deferred access checks from a template-parameter-list.
27306 CHECKS is a TREE_LIST of access checks, as returned by
27307 get_deferred_access_checks. */
27309 static void
27310 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27312 ++processing_template_parmlist;
27313 perform_access_checks (checks, tf_warning_or_error);
27314 --processing_template_parmlist;
27317 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27318 `function-definition' sequence that follows a template header.
27319 If MEMBER_P is true, this declaration appears in a class scope.
27321 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27322 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27324 static tree
27325 cp_parser_single_declaration (cp_parser* parser,
27326 vec<deferred_access_check, va_gc> *checks,
27327 bool member_p,
27328 bool explicit_specialization_p,
27329 bool* friend_p)
27331 int declares_class_or_enum;
27332 tree decl = NULL_TREE;
27333 cp_decl_specifier_seq decl_specifiers;
27334 bool function_definition_p = false;
27335 cp_token *decl_spec_token_start;
27337 /* This function is only used when processing a template
27338 declaration. */
27339 gcc_assert (innermost_scope_kind () == sk_template_parms
27340 || innermost_scope_kind () == sk_template_spec);
27342 /* Defer access checks until we know what is being declared. */
27343 push_deferring_access_checks (dk_deferred);
27345 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27346 alternative. */
27347 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27348 cp_parser_decl_specifier_seq (parser,
27349 CP_PARSER_FLAGS_OPTIONAL,
27350 &decl_specifiers,
27351 &declares_class_or_enum);
27352 if (friend_p)
27353 *friend_p = cp_parser_friend_p (&decl_specifiers);
27355 /* There are no template typedefs. */
27356 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27358 error_at (decl_spec_token_start->location,
27359 "template declaration of %<typedef%>");
27360 decl = error_mark_node;
27363 /* Gather up the access checks that occurred the
27364 decl-specifier-seq. */
27365 stop_deferring_access_checks ();
27367 /* Check for the declaration of a template class. */
27368 if (declares_class_or_enum)
27370 if (cp_parser_declares_only_class_p (parser)
27371 || (declares_class_or_enum & 2))
27373 // If this is a declaration, but not a definition, associate
27374 // any constraints with the type declaration. Constraints
27375 // are associated with definitions in cp_parser_class_specifier.
27376 if (declares_class_or_enum == 1)
27377 associate_classtype_constraints (decl_specifiers.type);
27379 decl = shadow_tag (&decl_specifiers);
27381 /* In this case:
27383 struct C {
27384 friend template <typename T> struct A<T>::B;
27387 A<T>::B will be represented by a TYPENAME_TYPE, and
27388 therefore not recognized by shadow_tag. */
27389 if (friend_p && *friend_p
27390 && !decl
27391 && decl_specifiers.type
27392 && TYPE_P (decl_specifiers.type))
27393 decl = decl_specifiers.type;
27395 if (decl && decl != error_mark_node)
27396 decl = TYPE_NAME (decl);
27397 else
27398 decl = error_mark_node;
27400 /* Perform access checks for template parameters. */
27401 cp_parser_perform_template_parameter_access_checks (checks);
27403 /* Give a helpful diagnostic for
27404 template <class T> struct A { } a;
27405 if we aren't already recovering from an error. */
27406 if (!cp_parser_declares_only_class_p (parser)
27407 && !seen_error ())
27409 error_at (cp_lexer_peek_token (parser->lexer)->location,
27410 "a class template declaration must not declare "
27411 "anything else");
27412 cp_parser_skip_to_end_of_block_or_statement (parser);
27413 goto out;
27418 /* Complain about missing 'typename' or other invalid type names. */
27419 if (!decl_specifiers.any_type_specifiers_p
27420 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27422 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27423 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27424 the rest of this declaration. */
27425 decl = error_mark_node;
27426 goto out;
27429 /* If it's not a template class, try for a template function. If
27430 the next token is a `;', then this declaration does not declare
27431 anything. But, if there were errors in the decl-specifiers, then
27432 the error might well have come from an attempted class-specifier.
27433 In that case, there's no need to warn about a missing declarator. */
27434 if (!decl
27435 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27436 || decl_specifiers.type != error_mark_node))
27438 decl = cp_parser_init_declarator (parser,
27439 &decl_specifiers,
27440 checks,
27441 /*function_definition_allowed_p=*/true,
27442 member_p,
27443 declares_class_or_enum,
27444 &function_definition_p,
27445 NULL, NULL, NULL);
27447 /* 7.1.1-1 [dcl.stc]
27449 A storage-class-specifier shall not be specified in an explicit
27450 specialization... */
27451 if (decl
27452 && explicit_specialization_p
27453 && decl_specifiers.storage_class != sc_none)
27455 error_at (decl_spec_token_start->location,
27456 "explicit template specialization cannot have a storage class");
27457 decl = error_mark_node;
27460 if (decl && VAR_P (decl))
27461 check_template_variable (decl);
27464 /* Look for a trailing `;' after the declaration. */
27465 if (!function_definition_p
27466 && (decl == error_mark_node
27467 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27468 cp_parser_skip_to_end_of_block_or_statement (parser);
27470 out:
27471 pop_deferring_access_checks ();
27473 /* Clear any current qualification; whatever comes next is the start
27474 of something new. */
27475 parser->scope = NULL_TREE;
27476 parser->qualifying_scope = NULL_TREE;
27477 parser->object_scope = NULL_TREE;
27479 return decl;
27482 /* Parse a cast-expression that is not the operand of a unary "&". */
27484 static cp_expr
27485 cp_parser_simple_cast_expression (cp_parser *parser)
27487 return cp_parser_cast_expression (parser, /*address_p=*/false,
27488 /*cast_p=*/false, /*decltype*/false, NULL);
27491 /* Parse a functional cast to TYPE. Returns an expression
27492 representing the cast. */
27494 static cp_expr
27495 cp_parser_functional_cast (cp_parser* parser, tree type)
27497 vec<tree, va_gc> *vec;
27498 tree expression_list;
27499 cp_expr cast;
27500 bool nonconst_p;
27502 location_t start_loc = input_location;
27504 if (!type)
27505 type = error_mark_node;
27507 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27509 cp_lexer_set_source_position (parser->lexer);
27510 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27511 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27512 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27513 if (TREE_CODE (type) == TYPE_DECL)
27514 type = TREE_TYPE (type);
27516 cast = finish_compound_literal (type, expression_list,
27517 tf_warning_or_error, fcl_functional);
27518 /* Create a location of the form:
27519 type_name{i, f}
27520 ^~~~~~~~~~~~~~~
27521 with caret == start at the start of the type name,
27522 finishing at the closing brace. */
27523 location_t finish_loc
27524 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27525 location_t combined_loc = make_location (start_loc, start_loc,
27526 finish_loc);
27527 cast.set_location (combined_loc);
27528 return cast;
27532 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27533 /*cast_p=*/true,
27534 /*allow_expansion_p=*/true,
27535 /*non_constant_p=*/NULL);
27536 if (vec == NULL)
27537 expression_list = error_mark_node;
27538 else
27540 expression_list = build_tree_list_vec (vec);
27541 release_tree_vector (vec);
27544 cast = build_functional_cast (type, expression_list,
27545 tf_warning_or_error);
27546 /* [expr.const]/1: In an integral constant expression "only type
27547 conversions to integral or enumeration type can be used". */
27548 if (TREE_CODE (type) == TYPE_DECL)
27549 type = TREE_TYPE (type);
27550 if (cast != error_mark_node
27551 && !cast_valid_in_integral_constant_expression_p (type)
27552 && cp_parser_non_integral_constant_expression (parser,
27553 NIC_CONSTRUCTOR))
27554 return error_mark_node;
27556 /* Create a location of the form:
27557 float(i)
27558 ^~~~~~~~
27559 with caret == start at the start of the type name,
27560 finishing at the closing paren. */
27561 location_t finish_loc
27562 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27563 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27564 cast.set_location (combined_loc);
27565 return cast;
27568 /* Save the tokens that make up the body of a member function defined
27569 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27570 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27571 specifiers applied to the declaration. Returns the FUNCTION_DECL
27572 for the member function. */
27574 static tree
27575 cp_parser_save_member_function_body (cp_parser* parser,
27576 cp_decl_specifier_seq *decl_specifiers,
27577 cp_declarator *declarator,
27578 tree attributes)
27580 cp_token *first;
27581 cp_token *last;
27582 tree fn;
27583 bool function_try_block = false;
27585 /* Create the FUNCTION_DECL. */
27586 fn = grokmethod (decl_specifiers, declarator, attributes);
27587 cp_finalize_omp_declare_simd (parser, fn);
27588 cp_finalize_oacc_routine (parser, fn, true);
27589 /* If something went badly wrong, bail out now. */
27590 if (fn == error_mark_node)
27592 /* If there's a function-body, skip it. */
27593 if (cp_parser_token_starts_function_definition_p
27594 (cp_lexer_peek_token (parser->lexer)))
27595 cp_parser_skip_to_end_of_block_or_statement (parser);
27596 return error_mark_node;
27599 /* Remember it, if there default args to post process. */
27600 cp_parser_save_default_args (parser, fn);
27602 /* Save away the tokens that make up the body of the
27603 function. */
27604 first = parser->lexer->next_token;
27606 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27607 cp_lexer_consume_token (parser->lexer);
27608 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27609 RID_TRANSACTION_ATOMIC))
27611 cp_lexer_consume_token (parser->lexer);
27612 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27613 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27614 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27615 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27616 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27617 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27618 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27620 cp_lexer_consume_token (parser->lexer);
27621 cp_lexer_consume_token (parser->lexer);
27622 cp_lexer_consume_token (parser->lexer);
27623 cp_lexer_consume_token (parser->lexer);
27624 cp_lexer_consume_token (parser->lexer);
27626 else
27627 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27628 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27630 cp_lexer_consume_token (parser->lexer);
27631 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27632 break;
27636 /* Handle function try blocks. */
27637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27639 cp_lexer_consume_token (parser->lexer);
27640 function_try_block = true;
27642 /* We can have braced-init-list mem-initializers before the fn body. */
27643 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27645 cp_lexer_consume_token (parser->lexer);
27646 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27648 /* cache_group will stop after an un-nested { } pair, too. */
27649 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27650 break;
27652 /* variadic mem-inits have ... after the ')'. */
27653 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27654 cp_lexer_consume_token (parser->lexer);
27657 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27658 /* Handle function try blocks. */
27659 if (function_try_block)
27660 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27661 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27662 last = parser->lexer->next_token;
27664 /* Save away the inline definition; we will process it when the
27665 class is complete. */
27666 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27667 DECL_PENDING_INLINE_P (fn) = 1;
27669 /* We need to know that this was defined in the class, so that
27670 friend templates are handled correctly. */
27671 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27673 /* Add FN to the queue of functions to be parsed later. */
27674 vec_safe_push (unparsed_funs_with_definitions, fn);
27676 return fn;
27679 /* Save the tokens that make up the in-class initializer for a non-static
27680 data member. Returns a DEFAULT_ARG. */
27682 static tree
27683 cp_parser_save_nsdmi (cp_parser* parser)
27685 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27688 /* Parse a template-argument-list, as well as the trailing ">" (but
27689 not the opening "<"). See cp_parser_template_argument_list for the
27690 return value. */
27692 static tree
27693 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27695 tree arguments;
27696 tree saved_scope;
27697 tree saved_qualifying_scope;
27698 tree saved_object_scope;
27699 bool saved_greater_than_is_operator_p;
27700 int saved_unevaluated_operand;
27701 int saved_inhibit_evaluation_warnings;
27703 /* [temp.names]
27705 When parsing a template-id, the first non-nested `>' is taken as
27706 the end of the template-argument-list rather than a greater-than
27707 operator. */
27708 saved_greater_than_is_operator_p
27709 = parser->greater_than_is_operator_p;
27710 parser->greater_than_is_operator_p = false;
27711 /* Parsing the argument list may modify SCOPE, so we save it
27712 here. */
27713 saved_scope = parser->scope;
27714 saved_qualifying_scope = parser->qualifying_scope;
27715 saved_object_scope = parser->object_scope;
27716 /* We need to evaluate the template arguments, even though this
27717 template-id may be nested within a "sizeof". */
27718 saved_unevaluated_operand = cp_unevaluated_operand;
27719 cp_unevaluated_operand = 0;
27720 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27721 c_inhibit_evaluation_warnings = 0;
27722 /* Parse the template-argument-list itself. */
27723 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27724 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27725 arguments = NULL_TREE;
27726 else
27727 arguments = cp_parser_template_argument_list (parser);
27728 /* Look for the `>' that ends the template-argument-list. If we find
27729 a '>>' instead, it's probably just a typo. */
27730 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27732 if (cxx_dialect != cxx98)
27734 /* In C++0x, a `>>' in a template argument list or cast
27735 expression is considered to be two separate `>'
27736 tokens. So, change the current token to a `>', but don't
27737 consume it: it will be consumed later when the outer
27738 template argument list (or cast expression) is parsed.
27739 Note that this replacement of `>' for `>>' is necessary
27740 even if we are parsing tentatively: in the tentative
27741 case, after calling
27742 cp_parser_enclosed_template_argument_list we will always
27743 throw away all of the template arguments and the first
27744 closing `>', either because the template argument list
27745 was erroneous or because we are replacing those tokens
27746 with a CPP_TEMPLATE_ID token. The second `>' (which will
27747 not have been thrown away) is needed either to close an
27748 outer template argument list or to complete a new-style
27749 cast. */
27750 cp_token *token = cp_lexer_peek_token (parser->lexer);
27751 token->type = CPP_GREATER;
27753 else if (!saved_greater_than_is_operator_p)
27755 /* If we're in a nested template argument list, the '>>' has
27756 to be a typo for '> >'. We emit the error message, but we
27757 continue parsing and we push a '>' as next token, so that
27758 the argument list will be parsed correctly. Note that the
27759 global source location is still on the token before the
27760 '>>', so we need to say explicitly where we want it. */
27761 cp_token *token = cp_lexer_peek_token (parser->lexer);
27762 gcc_rich_location richloc (token->location);
27763 richloc.add_fixit_replace ("> >");
27764 error_at (&richloc, "%<>>%> should be %<> >%> "
27765 "within a nested template argument list");
27767 token->type = CPP_GREATER;
27769 else
27771 /* If this is not a nested template argument list, the '>>'
27772 is a typo for '>'. Emit an error message and continue.
27773 Same deal about the token location, but here we can get it
27774 right by consuming the '>>' before issuing the diagnostic. */
27775 cp_token *token = cp_lexer_consume_token (parser->lexer);
27776 error_at (token->location,
27777 "spurious %<>>%>, use %<>%> to terminate "
27778 "a template argument list");
27781 else
27782 cp_parser_skip_to_end_of_template_parameter_list (parser);
27783 /* The `>' token might be a greater-than operator again now. */
27784 parser->greater_than_is_operator_p
27785 = saved_greater_than_is_operator_p;
27786 /* Restore the SAVED_SCOPE. */
27787 parser->scope = saved_scope;
27788 parser->qualifying_scope = saved_qualifying_scope;
27789 parser->object_scope = saved_object_scope;
27790 cp_unevaluated_operand = saved_unevaluated_operand;
27791 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27793 return arguments;
27796 /* MEMBER_FUNCTION is a member function, or a friend. If default
27797 arguments, or the body of the function have not yet been parsed,
27798 parse them now. */
27800 static void
27801 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27803 timevar_push (TV_PARSE_INMETH);
27804 /* If this member is a template, get the underlying
27805 FUNCTION_DECL. */
27806 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27807 member_function = DECL_TEMPLATE_RESULT (member_function);
27809 /* There should not be any class definitions in progress at this
27810 point; the bodies of members are only parsed outside of all class
27811 definitions. */
27812 gcc_assert (parser->num_classes_being_defined == 0);
27813 /* While we're parsing the member functions we might encounter more
27814 classes. We want to handle them right away, but we don't want
27815 them getting mixed up with functions that are currently in the
27816 queue. */
27817 push_unparsed_function_queues (parser);
27819 /* Make sure that any template parameters are in scope. */
27820 maybe_begin_member_template_processing (member_function);
27822 /* If the body of the function has not yet been parsed, parse it
27823 now. */
27824 if (DECL_PENDING_INLINE_P (member_function))
27826 tree function_scope;
27827 cp_token_cache *tokens;
27829 /* The function is no longer pending; we are processing it. */
27830 tokens = DECL_PENDING_INLINE_INFO (member_function);
27831 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27832 DECL_PENDING_INLINE_P (member_function) = 0;
27834 /* If this is a local class, enter the scope of the containing
27835 function. */
27836 function_scope = current_function_decl;
27837 if (function_scope)
27838 push_function_context ();
27840 /* Push the body of the function onto the lexer stack. */
27841 cp_parser_push_lexer_for_tokens (parser, tokens);
27843 /* Let the front end know that we going to be defining this
27844 function. */
27845 start_preparsed_function (member_function, NULL_TREE,
27846 SF_PRE_PARSED | SF_INCLASS_INLINE);
27848 /* Don't do access checking if it is a templated function. */
27849 if (processing_template_decl)
27850 push_deferring_access_checks (dk_no_check);
27852 /* #pragma omp declare reduction needs special parsing. */
27853 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27855 parser->lexer->in_pragma = true;
27856 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27857 finish_function (/*inline_p=*/true);
27858 cp_check_omp_declare_reduction (member_function);
27860 else
27861 /* Now, parse the body of the function. */
27862 cp_parser_function_definition_after_declarator (parser,
27863 /*inline_p=*/true);
27865 if (processing_template_decl)
27866 pop_deferring_access_checks ();
27868 /* Leave the scope of the containing function. */
27869 if (function_scope)
27870 pop_function_context ();
27871 cp_parser_pop_lexer (parser);
27874 /* Remove any template parameters from the symbol table. */
27875 maybe_end_member_template_processing ();
27877 /* Restore the queue. */
27878 pop_unparsed_function_queues (parser);
27879 timevar_pop (TV_PARSE_INMETH);
27882 /* If DECL contains any default args, remember it on the unparsed
27883 functions queue. */
27885 static void
27886 cp_parser_save_default_args (cp_parser* parser, tree decl)
27888 tree probe;
27890 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27891 probe;
27892 probe = TREE_CHAIN (probe))
27893 if (TREE_PURPOSE (probe))
27895 cp_default_arg_entry entry = {current_class_type, decl};
27896 vec_safe_push (unparsed_funs_with_default_args, entry);
27897 break;
27901 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27902 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27903 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27904 from the parameter-type-list. */
27906 static tree
27907 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27908 tree default_arg, tree parmtype)
27910 cp_token_cache *tokens;
27911 tree parsed_arg;
27912 bool dummy;
27914 if (default_arg == error_mark_node)
27915 return error_mark_node;
27917 /* Push the saved tokens for the default argument onto the parser's
27918 lexer stack. */
27919 tokens = DEFARG_TOKENS (default_arg);
27920 cp_parser_push_lexer_for_tokens (parser, tokens);
27922 start_lambda_scope (decl);
27924 /* Parse the default argument. */
27925 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27926 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27927 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27929 finish_lambda_scope ();
27931 if (parsed_arg == error_mark_node)
27932 cp_parser_skip_to_end_of_statement (parser);
27934 if (!processing_template_decl)
27936 /* In a non-template class, check conversions now. In a template,
27937 we'll wait and instantiate these as needed. */
27938 if (TREE_CODE (decl) == PARM_DECL)
27939 parsed_arg = check_default_argument (parmtype, parsed_arg,
27940 tf_warning_or_error);
27941 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27942 parsed_arg = error_mark_node;
27943 else
27944 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27947 /* If the token stream has not been completely used up, then
27948 there was extra junk after the end of the default
27949 argument. */
27950 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27952 if (TREE_CODE (decl) == PARM_DECL)
27953 cp_parser_error (parser, "expected %<,%>");
27954 else
27955 cp_parser_error (parser, "expected %<;%>");
27958 /* Revert to the main lexer. */
27959 cp_parser_pop_lexer (parser);
27961 return parsed_arg;
27964 /* FIELD is a non-static data member with an initializer which we saved for
27965 later; parse it now. */
27967 static void
27968 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27970 tree def;
27972 maybe_begin_member_template_processing (field);
27974 push_unparsed_function_queues (parser);
27975 def = cp_parser_late_parse_one_default_arg (parser, field,
27976 DECL_INITIAL (field),
27977 NULL_TREE);
27978 pop_unparsed_function_queues (parser);
27980 maybe_end_member_template_processing ();
27982 DECL_INITIAL (field) = def;
27985 /* FN is a FUNCTION_DECL which may contains a parameter with an
27986 unparsed DEFAULT_ARG. Parse the default args now. This function
27987 assumes that the current scope is the scope in which the default
27988 argument should be processed. */
27990 static void
27991 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27993 bool saved_local_variables_forbidden_p;
27994 tree parm, parmdecl;
27996 /* While we're parsing the default args, we might (due to the
27997 statement expression extension) encounter more classes. We want
27998 to handle them right away, but we don't want them getting mixed
27999 up with default args that are currently in the queue. */
28000 push_unparsed_function_queues (parser);
28002 /* Local variable names (and the `this' keyword) may not appear
28003 in a default argument. */
28004 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28005 parser->local_variables_forbidden_p = true;
28007 push_defarg_context (fn);
28009 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28010 parmdecl = DECL_ARGUMENTS (fn);
28011 parm && parm != void_list_node;
28012 parm = TREE_CHAIN (parm),
28013 parmdecl = DECL_CHAIN (parmdecl))
28015 tree default_arg = TREE_PURPOSE (parm);
28016 tree parsed_arg;
28017 vec<tree, va_gc> *insts;
28018 tree copy;
28019 unsigned ix;
28021 if (!default_arg)
28022 continue;
28024 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28025 /* This can happen for a friend declaration for a function
28026 already declared with default arguments. */
28027 continue;
28029 parsed_arg
28030 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28031 default_arg,
28032 TREE_VALUE (parm));
28033 TREE_PURPOSE (parm) = parsed_arg;
28035 /* Update any instantiations we've already created. */
28036 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28037 vec_safe_iterate (insts, ix, &copy); ix++)
28038 TREE_PURPOSE (copy) = parsed_arg;
28041 pop_defarg_context ();
28043 /* Make sure no default arg is missing. */
28044 check_default_args (fn);
28046 /* Restore the state of local_variables_forbidden_p. */
28047 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28049 /* Restore the queue. */
28050 pop_unparsed_function_queues (parser);
28053 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28055 sizeof ... ( identifier )
28057 where the 'sizeof' token has already been consumed. */
28059 static tree
28060 cp_parser_sizeof_pack (cp_parser *parser)
28062 /* Consume the `...'. */
28063 cp_lexer_consume_token (parser->lexer);
28064 maybe_warn_variadic_templates ();
28066 matching_parens parens;
28067 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28068 if (paren)
28069 parens.consume_open (parser);
28070 else
28071 permerror (cp_lexer_peek_token (parser->lexer)->location,
28072 "%<sizeof...%> argument must be surrounded by parentheses");
28074 cp_token *token = cp_lexer_peek_token (parser->lexer);
28075 tree name = cp_parser_identifier (parser);
28076 if (name == error_mark_node)
28077 return error_mark_node;
28078 /* The name is not qualified. */
28079 parser->scope = NULL_TREE;
28080 parser->qualifying_scope = NULL_TREE;
28081 parser->object_scope = NULL_TREE;
28082 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28083 if (expr == error_mark_node)
28084 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28085 token->location);
28086 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28087 expr = TREE_TYPE (expr);
28088 else if (TREE_CODE (expr) == CONST_DECL)
28089 expr = DECL_INITIAL (expr);
28090 expr = make_pack_expansion (expr);
28091 PACK_EXPANSION_SIZEOF_P (expr) = true;
28093 if (paren)
28094 parens.require_close (parser);
28096 return expr;
28099 /* Parse the operand of `sizeof' (or a similar operator). Returns
28100 either a TYPE or an expression, depending on the form of the
28101 input. The KEYWORD indicates which kind of expression we have
28102 encountered. */
28104 static tree
28105 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28107 tree expr = NULL_TREE;
28108 const char *saved_message;
28109 char *tmp;
28110 bool saved_integral_constant_expression_p;
28111 bool saved_non_integral_constant_expression_p;
28113 /* If it's a `...', then we are computing the length of a parameter
28114 pack. */
28115 if (keyword == RID_SIZEOF
28116 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28117 return cp_parser_sizeof_pack (parser);
28119 /* Types cannot be defined in a `sizeof' expression. Save away the
28120 old message. */
28121 saved_message = parser->type_definition_forbidden_message;
28122 /* And create the new one. */
28123 tmp = concat ("types may not be defined in %<",
28124 IDENTIFIER_POINTER (ridpointers[keyword]),
28125 "%> expressions", NULL);
28126 parser->type_definition_forbidden_message = tmp;
28128 /* The restrictions on constant-expressions do not apply inside
28129 sizeof expressions. */
28130 saved_integral_constant_expression_p
28131 = parser->integral_constant_expression_p;
28132 saved_non_integral_constant_expression_p
28133 = parser->non_integral_constant_expression_p;
28134 parser->integral_constant_expression_p = false;
28136 /* Do not actually evaluate the expression. */
28137 ++cp_unevaluated_operand;
28138 ++c_inhibit_evaluation_warnings;
28139 /* If it's a `(', then we might be looking at the type-id
28140 construction. */
28141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28143 tree type = NULL_TREE;
28145 /* We can't be sure yet whether we're looking at a type-id or an
28146 expression. */
28147 cp_parser_parse_tentatively (parser);
28149 matching_parens parens;
28150 parens.consume_open (parser);
28152 /* Note: as a GNU Extension, compound literals are considered
28153 postfix-expressions as they are in C99, so they are valid
28154 arguments to sizeof. See comment in cp_parser_cast_expression
28155 for details. */
28156 if (cp_parser_compound_literal_p (parser))
28157 cp_parser_simulate_error (parser);
28158 else
28160 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28161 parser->in_type_id_in_expr_p = true;
28162 /* Look for the type-id. */
28163 type = cp_parser_type_id (parser);
28164 /* Look for the closing `)'. */
28165 parens.require_close (parser);
28166 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28169 /* If all went well, then we're done. */
28170 if (cp_parser_parse_definitely (parser))
28172 cp_decl_specifier_seq decl_specs;
28174 /* Build a trivial decl-specifier-seq. */
28175 clear_decl_specs (&decl_specs);
28176 decl_specs.type = type;
28178 /* Call grokdeclarator to figure out what type this is. */
28179 expr = grokdeclarator (NULL,
28180 &decl_specs,
28181 TYPENAME,
28182 /*initialized=*/0,
28183 /*attrlist=*/NULL);
28187 /* If the type-id production did not work out, then we must be
28188 looking at the unary-expression production. */
28189 if (!expr)
28190 expr = cp_parser_unary_expression (parser);
28192 /* Go back to evaluating expressions. */
28193 --cp_unevaluated_operand;
28194 --c_inhibit_evaluation_warnings;
28196 /* Free the message we created. */
28197 free (tmp);
28198 /* And restore the old one. */
28199 parser->type_definition_forbidden_message = saved_message;
28200 parser->integral_constant_expression_p
28201 = saved_integral_constant_expression_p;
28202 parser->non_integral_constant_expression_p
28203 = saved_non_integral_constant_expression_p;
28205 return expr;
28208 /* If the current declaration has no declarator, return true. */
28210 static bool
28211 cp_parser_declares_only_class_p (cp_parser *parser)
28213 /* If the next token is a `;' or a `,' then there is no
28214 declarator. */
28215 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28216 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28219 /* Update the DECL_SPECS to reflect the storage class indicated by
28220 KEYWORD. */
28222 static void
28223 cp_parser_set_storage_class (cp_parser *parser,
28224 cp_decl_specifier_seq *decl_specs,
28225 enum rid keyword,
28226 cp_token *token)
28228 cp_storage_class storage_class;
28230 if (parser->in_unbraced_linkage_specification_p)
28232 error_at (token->location, "invalid use of %qD in linkage specification",
28233 ridpointers[keyword]);
28234 return;
28236 else if (decl_specs->storage_class != sc_none)
28238 decl_specs->conflicting_specifiers_p = true;
28239 return;
28242 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28243 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28244 && decl_specs->gnu_thread_keyword_p)
28246 pedwarn (decl_specs->locations[ds_thread], 0,
28247 "%<__thread%> before %qD", ridpointers[keyword]);
28250 switch (keyword)
28252 case RID_AUTO:
28253 storage_class = sc_auto;
28254 break;
28255 case RID_REGISTER:
28256 storage_class = sc_register;
28257 break;
28258 case RID_STATIC:
28259 storage_class = sc_static;
28260 break;
28261 case RID_EXTERN:
28262 storage_class = sc_extern;
28263 break;
28264 case RID_MUTABLE:
28265 storage_class = sc_mutable;
28266 break;
28267 default:
28268 gcc_unreachable ();
28270 decl_specs->storage_class = storage_class;
28271 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28273 /* A storage class specifier cannot be applied alongside a typedef
28274 specifier. If there is a typedef specifier present then set
28275 conflicting_specifiers_p which will trigger an error later
28276 on in grokdeclarator. */
28277 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28278 decl_specs->conflicting_specifiers_p = true;
28281 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28282 is true, the type is a class or enum definition. */
28284 static void
28285 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28286 tree type_spec,
28287 cp_token *token,
28288 bool type_definition_p)
28290 decl_specs->any_specifiers_p = true;
28292 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28293 (with, for example, in "typedef int wchar_t;") we remember that
28294 this is what happened. In system headers, we ignore these
28295 declarations so that G++ can work with system headers that are not
28296 C++-safe. */
28297 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28298 && !type_definition_p
28299 && (type_spec == boolean_type_node
28300 || type_spec == char16_type_node
28301 || type_spec == char32_type_node
28302 || type_spec == wchar_type_node)
28303 && (decl_specs->type
28304 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28305 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28306 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28307 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28309 decl_specs->redefined_builtin_type = type_spec;
28310 set_and_check_decl_spec_loc (decl_specs,
28311 ds_redefined_builtin_type_spec,
28312 token);
28313 if (!decl_specs->type)
28315 decl_specs->type = type_spec;
28316 decl_specs->type_definition_p = false;
28317 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28320 else if (decl_specs->type)
28321 decl_specs->multiple_types_p = true;
28322 else
28324 decl_specs->type = type_spec;
28325 decl_specs->type_definition_p = type_definition_p;
28326 decl_specs->redefined_builtin_type = NULL_TREE;
28327 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28331 /* True iff TOKEN is the GNU keyword __thread. */
28333 static bool
28334 token_is__thread (cp_token *token)
28336 gcc_assert (token->keyword == RID_THREAD);
28337 return id_equal (token->u.value, "__thread");
28340 /* Set the location for a declarator specifier and check if it is
28341 duplicated.
28343 DECL_SPECS is the sequence of declarator specifiers onto which to
28344 set the location.
28346 DS is the single declarator specifier to set which location is to
28347 be set onto the existing sequence of declarators.
28349 LOCATION is the location for the declarator specifier to
28350 consider. */
28352 static void
28353 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28354 cp_decl_spec ds, cp_token *token)
28356 gcc_assert (ds < ds_last);
28358 if (decl_specs == NULL)
28359 return;
28361 source_location location = token->location;
28363 if (decl_specs->locations[ds] == 0)
28365 decl_specs->locations[ds] = location;
28366 if (ds == ds_thread)
28367 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28369 else
28371 if (ds == ds_long)
28373 if (decl_specs->locations[ds_long_long] != 0)
28374 error_at (location,
28375 "%<long long long%> is too long for GCC");
28376 else
28378 decl_specs->locations[ds_long_long] = location;
28379 pedwarn_cxx98 (location,
28380 OPT_Wlong_long,
28381 "ISO C++ 1998 does not support %<long long%>");
28384 else if (ds == ds_thread)
28386 bool gnu = token_is__thread (token);
28387 gcc_rich_location richloc (location);
28388 if (gnu != decl_specs->gnu_thread_keyword_p)
28390 richloc.add_range (decl_specs->locations[ds_thread], false);
28391 error_at (&richloc,
28392 "both %<__thread%> and %<thread_local%> specified");
28394 else
28396 richloc.add_fixit_remove ();
28397 error_at (&richloc, "duplicate %qD", token->u.value);
28400 else
28402 static const char *const decl_spec_names[] = {
28403 "signed",
28404 "unsigned",
28405 "short",
28406 "long",
28407 "const",
28408 "volatile",
28409 "restrict",
28410 "inline",
28411 "virtual",
28412 "explicit",
28413 "friend",
28414 "typedef",
28415 "using",
28416 "constexpr",
28417 "__complex"
28419 gcc_rich_location richloc (location);
28420 richloc.add_fixit_remove ();
28421 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28426 /* Return true iff the declarator specifier DS is present in the
28427 sequence of declarator specifiers DECL_SPECS. */
28429 bool
28430 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28431 cp_decl_spec ds)
28433 gcc_assert (ds < ds_last);
28435 if (decl_specs == NULL)
28436 return false;
28438 return decl_specs->locations[ds] != 0;
28441 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28442 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28444 static bool
28445 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28447 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28450 /* Issue an error message indicating that TOKEN_DESC was expected.
28451 If KEYWORD is true, it indicated this function is called by
28452 cp_parser_require_keword and the required token can only be
28453 a indicated keyword.
28455 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28456 within any error as the location of an "opening" token matching
28457 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28458 RT_CLOSE_PAREN). */
28460 static void
28461 cp_parser_required_error (cp_parser *parser,
28462 required_token token_desc,
28463 bool keyword,
28464 location_t matching_location)
28466 if (cp_parser_simulate_error (parser))
28467 return;
28469 const char *gmsgid = NULL;
28470 switch (token_desc)
28472 case RT_NEW:
28473 gmsgid = G_("expected %<new%>");
28474 break;
28475 case RT_DELETE:
28476 gmsgid = G_("expected %<delete%>");
28477 break;
28478 case RT_RETURN:
28479 gmsgid = G_("expected %<return%>");
28480 break;
28481 case RT_WHILE:
28482 gmsgid = G_("expected %<while%>");
28483 break;
28484 case RT_EXTERN:
28485 gmsgid = G_("expected %<extern%>");
28486 break;
28487 case RT_STATIC_ASSERT:
28488 gmsgid = G_("expected %<static_assert%>");
28489 break;
28490 case RT_DECLTYPE:
28491 gmsgid = G_("expected %<decltype%>");
28492 break;
28493 case RT_OPERATOR:
28494 gmsgid = G_("expected %<operator%>");
28495 break;
28496 case RT_CLASS:
28497 gmsgid = G_("expected %<class%>");
28498 break;
28499 case RT_TEMPLATE:
28500 gmsgid = G_("expected %<template%>");
28501 break;
28502 case RT_NAMESPACE:
28503 gmsgid = G_("expected %<namespace%>");
28504 break;
28505 case RT_USING:
28506 gmsgid = G_("expected %<using%>");
28507 break;
28508 case RT_ASM:
28509 gmsgid = G_("expected %<asm%>");
28510 break;
28511 case RT_TRY:
28512 gmsgid = G_("expected %<try%>");
28513 break;
28514 case RT_CATCH:
28515 gmsgid = G_("expected %<catch%>");
28516 break;
28517 case RT_THROW:
28518 gmsgid = G_("expected %<throw%>");
28519 break;
28520 case RT_LABEL:
28521 gmsgid = G_("expected %<__label__%>");
28522 break;
28523 case RT_AT_TRY:
28524 gmsgid = G_("expected %<@try%>");
28525 break;
28526 case RT_AT_SYNCHRONIZED:
28527 gmsgid = G_("expected %<@synchronized%>");
28528 break;
28529 case RT_AT_THROW:
28530 gmsgid = G_("expected %<@throw%>");
28531 break;
28532 case RT_TRANSACTION_ATOMIC:
28533 gmsgid = G_("expected %<__transaction_atomic%>");
28534 break;
28535 case RT_TRANSACTION_RELAXED:
28536 gmsgid = G_("expected %<__transaction_relaxed%>");
28537 break;
28538 default:
28539 break;
28542 if (!gmsgid && !keyword)
28544 switch (token_desc)
28546 case RT_SEMICOLON:
28547 gmsgid = G_("expected %<;%>");
28548 break;
28549 case RT_OPEN_PAREN:
28550 gmsgid = G_("expected %<(%>");
28551 break;
28552 case RT_CLOSE_BRACE:
28553 gmsgid = G_("expected %<}%>");
28554 break;
28555 case RT_OPEN_BRACE:
28556 gmsgid = G_("expected %<{%>");
28557 break;
28558 case RT_CLOSE_SQUARE:
28559 gmsgid = G_("expected %<]%>");
28560 break;
28561 case RT_OPEN_SQUARE:
28562 gmsgid = G_("expected %<[%>");
28563 break;
28564 case RT_COMMA:
28565 gmsgid = G_("expected %<,%>");
28566 break;
28567 case RT_SCOPE:
28568 gmsgid = G_("expected %<::%>");
28569 break;
28570 case RT_LESS:
28571 gmsgid = G_("expected %<<%>");
28572 break;
28573 case RT_GREATER:
28574 gmsgid = G_("expected %<>%>");
28575 break;
28576 case RT_EQ:
28577 gmsgid = G_("expected %<=%>");
28578 break;
28579 case RT_ELLIPSIS:
28580 gmsgid = G_("expected %<...%>");
28581 break;
28582 case RT_MULT:
28583 gmsgid = G_("expected %<*%>");
28584 break;
28585 case RT_COMPL:
28586 gmsgid = G_("expected %<~%>");
28587 break;
28588 case RT_COLON:
28589 gmsgid = G_("expected %<:%>");
28590 break;
28591 case RT_COLON_SCOPE:
28592 gmsgid = G_("expected %<:%> or %<::%>");
28593 break;
28594 case RT_CLOSE_PAREN:
28595 gmsgid = G_("expected %<)%>");
28596 break;
28597 case RT_COMMA_CLOSE_PAREN:
28598 gmsgid = G_("expected %<,%> or %<)%>");
28599 break;
28600 case RT_PRAGMA_EOL:
28601 gmsgid = G_("expected end of line");
28602 break;
28603 case RT_NAME:
28604 gmsgid = G_("expected identifier");
28605 break;
28606 case RT_SELECT:
28607 gmsgid = G_("expected selection-statement");
28608 break;
28609 case RT_ITERATION:
28610 gmsgid = G_("expected iteration-statement");
28611 break;
28612 case RT_JUMP:
28613 gmsgid = G_("expected jump-statement");
28614 break;
28615 case RT_CLASS_KEY:
28616 gmsgid = G_("expected class-key");
28617 break;
28618 case RT_CLASS_TYPENAME_TEMPLATE:
28619 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28620 break;
28621 default:
28622 gcc_unreachable ();
28626 if (gmsgid)
28627 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28631 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28632 issue an error message indicating that TOKEN_DESC was expected.
28634 Returns the token consumed, if the token had the appropriate type.
28635 Otherwise, returns NULL.
28637 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28638 within any error as the location of an "opening" token matching
28639 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28640 RT_CLOSE_PAREN). */
28642 static cp_token *
28643 cp_parser_require (cp_parser* parser,
28644 enum cpp_ttype type,
28645 required_token token_desc,
28646 location_t matching_location)
28648 if (cp_lexer_next_token_is (parser->lexer, type))
28649 return cp_lexer_consume_token (parser->lexer);
28650 else
28652 /* Output the MESSAGE -- unless we're parsing tentatively. */
28653 if (!cp_parser_simulate_error (parser))
28654 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28655 matching_location);
28656 return NULL;
28660 /* An error message is produced if the next token is not '>'.
28661 All further tokens are skipped until the desired token is
28662 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28664 static void
28665 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28667 /* Current level of '< ... >'. */
28668 unsigned level = 0;
28669 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28670 unsigned nesting_depth = 0;
28672 /* Are we ready, yet? If not, issue error message. */
28673 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28674 return;
28676 /* Skip tokens until the desired token is found. */
28677 while (true)
28679 /* Peek at the next token. */
28680 switch (cp_lexer_peek_token (parser->lexer)->type)
28682 case CPP_LESS:
28683 if (!nesting_depth)
28684 ++level;
28685 break;
28687 case CPP_RSHIFT:
28688 if (cxx_dialect == cxx98)
28689 /* C++0x views the `>>' operator as two `>' tokens, but
28690 C++98 does not. */
28691 break;
28692 else if (!nesting_depth && level-- == 0)
28694 /* We've hit a `>>' where the first `>' closes the
28695 template argument list, and the second `>' is
28696 spurious. Just consume the `>>' and stop; we've
28697 already produced at least one error. */
28698 cp_lexer_consume_token (parser->lexer);
28699 return;
28701 /* Fall through for C++0x, so we handle the second `>' in
28702 the `>>'. */
28703 gcc_fallthrough ();
28705 case CPP_GREATER:
28706 if (!nesting_depth && level-- == 0)
28708 /* We've reached the token we want, consume it and stop. */
28709 cp_lexer_consume_token (parser->lexer);
28710 return;
28712 break;
28714 case CPP_OPEN_PAREN:
28715 case CPP_OPEN_SQUARE:
28716 ++nesting_depth;
28717 break;
28719 case CPP_CLOSE_PAREN:
28720 case CPP_CLOSE_SQUARE:
28721 if (nesting_depth-- == 0)
28722 return;
28723 break;
28725 case CPP_EOF:
28726 case CPP_PRAGMA_EOL:
28727 case CPP_SEMICOLON:
28728 case CPP_OPEN_BRACE:
28729 case CPP_CLOSE_BRACE:
28730 /* The '>' was probably forgotten, don't look further. */
28731 return;
28733 default:
28734 break;
28737 /* Consume this token. */
28738 cp_lexer_consume_token (parser->lexer);
28742 /* If the next token is the indicated keyword, consume it. Otherwise,
28743 issue an error message indicating that TOKEN_DESC was expected.
28745 Returns the token consumed, if the token had the appropriate type.
28746 Otherwise, returns NULL. */
28748 static cp_token *
28749 cp_parser_require_keyword (cp_parser* parser,
28750 enum rid keyword,
28751 required_token token_desc)
28753 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28755 if (token && token->keyword != keyword)
28757 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28758 UNKNOWN_LOCATION);
28759 return NULL;
28762 return token;
28765 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28766 function-definition. */
28768 static bool
28769 cp_parser_token_starts_function_definition_p (cp_token* token)
28771 return (/* An ordinary function-body begins with an `{'. */
28772 token->type == CPP_OPEN_BRACE
28773 /* A ctor-initializer begins with a `:'. */
28774 || token->type == CPP_COLON
28775 /* A function-try-block begins with `try'. */
28776 || token->keyword == RID_TRY
28777 /* A function-transaction-block begins with `__transaction_atomic'
28778 or `__transaction_relaxed'. */
28779 || token->keyword == RID_TRANSACTION_ATOMIC
28780 || token->keyword == RID_TRANSACTION_RELAXED
28781 /* The named return value extension begins with `return'. */
28782 || token->keyword == RID_RETURN);
28785 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28786 definition. */
28788 static bool
28789 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28791 cp_token *token;
28793 token = cp_lexer_peek_token (parser->lexer);
28794 return (token->type == CPP_OPEN_BRACE
28795 || (token->type == CPP_COLON
28796 && !parser->colon_doesnt_start_class_def_p));
28799 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28800 C++0x) ending a template-argument. */
28802 static bool
28803 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28805 cp_token *token;
28807 token = cp_lexer_peek_token (parser->lexer);
28808 return (token->type == CPP_COMMA
28809 || token->type == CPP_GREATER
28810 || token->type == CPP_ELLIPSIS
28811 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28814 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28815 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28817 static bool
28818 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28819 size_t n)
28821 cp_token *token;
28823 token = cp_lexer_peek_nth_token (parser->lexer, n);
28824 if (token->type == CPP_LESS)
28825 return true;
28826 /* Check for the sequence `<::' in the original code. It would be lexed as
28827 `[:', where `[' is a digraph, and there is no whitespace before
28828 `:'. */
28829 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28831 cp_token *token2;
28832 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28833 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28834 return true;
28836 return false;
28839 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28840 or none_type otherwise. */
28842 static enum tag_types
28843 cp_parser_token_is_class_key (cp_token* token)
28845 switch (token->keyword)
28847 case RID_CLASS:
28848 return class_type;
28849 case RID_STRUCT:
28850 return record_type;
28851 case RID_UNION:
28852 return union_type;
28854 default:
28855 return none_type;
28859 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28860 or none_type otherwise or if the token is null. */
28862 static enum tag_types
28863 cp_parser_token_is_type_parameter_key (cp_token* token)
28865 if (!token)
28866 return none_type;
28868 switch (token->keyword)
28870 case RID_CLASS:
28871 return class_type;
28872 case RID_TYPENAME:
28873 return typename_type;
28875 default:
28876 return none_type;
28880 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28882 static void
28883 cp_parser_check_class_key (enum tag_types class_key, tree type)
28885 if (type == error_mark_node)
28886 return;
28887 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28889 if (permerror (input_location, "%qs tag used in naming %q#T",
28890 class_key == union_type ? "union"
28891 : class_key == record_type ? "struct" : "class",
28892 type))
28893 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28894 "%q#T was previously declared here", type);
28898 /* Issue an error message if DECL is redeclared with different
28899 access than its original declaration [class.access.spec/3].
28900 This applies to nested classes, nested class templates and
28901 enumerations [class.mem/1]. */
28903 static void
28904 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28906 if (!decl
28907 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28908 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28909 return;
28911 if ((TREE_PRIVATE (decl)
28912 != (current_access_specifier == access_private_node))
28913 || (TREE_PROTECTED (decl)
28914 != (current_access_specifier == access_protected_node)))
28915 error_at (location, "%qD redeclared with different access", decl);
28918 /* Look for the `template' keyword, as a syntactic disambiguator.
28919 Return TRUE iff it is present, in which case it will be
28920 consumed. */
28922 static bool
28923 cp_parser_optional_template_keyword (cp_parser *parser)
28925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28927 /* In C++98 the `template' keyword can only be used within templates;
28928 outside templates the parser can always figure out what is a
28929 template and what is not. In C++11, per the resolution of DR 468,
28930 `template' is allowed in cases where it is not strictly necessary. */
28931 if (!processing_template_decl
28932 && pedantic && cxx_dialect == cxx98)
28934 cp_token *token = cp_lexer_peek_token (parser->lexer);
28935 pedwarn (token->location, OPT_Wpedantic,
28936 "in C++98 %<template%> (as a disambiguator) is only "
28937 "allowed within templates");
28938 /* If this part of the token stream is rescanned, the same
28939 error message would be generated. So, we purge the token
28940 from the stream. */
28941 cp_lexer_purge_token (parser->lexer);
28942 return false;
28944 else
28946 /* Consume the `template' keyword. */
28947 cp_lexer_consume_token (parser->lexer);
28948 return true;
28951 return false;
28954 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28955 set PARSER->SCOPE, and perform other related actions. */
28957 static void
28958 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28960 struct tree_check *check_value;
28962 /* Get the stored value. */
28963 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28964 /* Set the scope from the stored value. */
28965 parser->scope = saved_checks_value (check_value);
28966 parser->qualifying_scope = check_value->qualifying_scope;
28967 parser->object_scope = NULL_TREE;
28970 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28971 encounter the end of a block before what we were looking for. */
28973 static bool
28974 cp_parser_cache_group (cp_parser *parser,
28975 enum cpp_ttype end,
28976 unsigned depth)
28978 while (true)
28980 cp_token *token = cp_lexer_peek_token (parser->lexer);
28982 /* Abort a parenthesized expression if we encounter a semicolon. */
28983 if ((end == CPP_CLOSE_PAREN || depth == 0)
28984 && token->type == CPP_SEMICOLON)
28985 return true;
28986 /* If we've reached the end of the file, stop. */
28987 if (token->type == CPP_EOF
28988 || (end != CPP_PRAGMA_EOL
28989 && token->type == CPP_PRAGMA_EOL))
28990 return true;
28991 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28992 /* We've hit the end of an enclosing block, so there's been some
28993 kind of syntax error. */
28994 return true;
28996 /* Consume the token. */
28997 cp_lexer_consume_token (parser->lexer);
28998 /* See if it starts a new group. */
28999 if (token->type == CPP_OPEN_BRACE)
29001 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29002 /* In theory this should probably check end == '}', but
29003 cp_parser_save_member_function_body needs it to exit
29004 after either '}' or ')' when called with ')'. */
29005 if (depth == 0)
29006 return false;
29008 else if (token->type == CPP_OPEN_PAREN)
29010 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29011 if (depth == 0 && end == CPP_CLOSE_PAREN)
29012 return false;
29014 else if (token->type == CPP_PRAGMA)
29015 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29016 else if (token->type == end)
29017 return false;
29021 /* Like above, for caching a default argument or NSDMI. Both of these are
29022 terminated by a non-nested comma, but it can be unclear whether or not a
29023 comma is nested in a template argument list unless we do more parsing.
29024 In order to handle this ambiguity, when we encounter a ',' after a '<'
29025 we try to parse what follows as a parameter-declaration-list (in the
29026 case of a default argument) or a member-declarator (in the case of an
29027 NSDMI). If that succeeds, then we stop caching. */
29029 static tree
29030 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29032 unsigned depth = 0;
29033 int maybe_template_id = 0;
29034 cp_token *first_token;
29035 cp_token *token;
29036 tree default_argument;
29038 /* Add tokens until we have processed the entire default
29039 argument. We add the range [first_token, token). */
29040 first_token = cp_lexer_peek_token (parser->lexer);
29041 if (first_token->type == CPP_OPEN_BRACE)
29043 /* For list-initialization, this is straightforward. */
29044 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29045 token = cp_lexer_peek_token (parser->lexer);
29047 else while (true)
29049 bool done = false;
29051 /* Peek at the next token. */
29052 token = cp_lexer_peek_token (parser->lexer);
29053 /* What we do depends on what token we have. */
29054 switch (token->type)
29056 /* In valid code, a default argument must be
29057 immediately followed by a `,' `)', or `...'. */
29058 case CPP_COMMA:
29059 if (depth == 0 && maybe_template_id)
29061 /* If we've seen a '<', we might be in a
29062 template-argument-list. Until Core issue 325 is
29063 resolved, we don't know how this situation ought
29064 to be handled, so try to DTRT. We check whether
29065 what comes after the comma is a valid parameter
29066 declaration list. If it is, then the comma ends
29067 the default argument; otherwise the default
29068 argument continues. */
29069 bool error = false;
29070 cp_token *peek;
29072 /* Set ITALP so cp_parser_parameter_declaration_list
29073 doesn't decide to commit to this parse. */
29074 bool saved_italp = parser->in_template_argument_list_p;
29075 parser->in_template_argument_list_p = true;
29077 cp_parser_parse_tentatively (parser);
29079 if (nsdmi)
29081 /* Parse declarators until we reach a non-comma or
29082 somthing that cannot be an initializer.
29083 Just checking whether we're looking at a single
29084 declarator is insufficient. Consider:
29085 int var = tuple<T,U>::x;
29086 The template parameter 'U' looks exactly like a
29087 declarator. */
29090 int ctor_dtor_or_conv_p;
29091 cp_lexer_consume_token (parser->lexer);
29092 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29093 &ctor_dtor_or_conv_p,
29094 /*parenthesized_p=*/NULL,
29095 /*member_p=*/true,
29096 /*friend_p=*/false);
29097 peek = cp_lexer_peek_token (parser->lexer);
29098 if (cp_parser_error_occurred (parser))
29099 break;
29101 while (peek->type == CPP_COMMA);
29102 /* If we met an '=' or ';' then the original comma
29103 was the end of the NSDMI. Otherwise assume
29104 we're still in the NSDMI. */
29105 error = (peek->type != CPP_EQ
29106 && peek->type != CPP_SEMICOLON);
29108 else
29110 cp_lexer_consume_token (parser->lexer);
29111 begin_scope (sk_function_parms, NULL_TREE);
29112 if (cp_parser_parameter_declaration_list (parser)
29113 == error_mark_node)
29114 error = true;
29115 pop_bindings_and_leave_scope ();
29117 if (!cp_parser_error_occurred (parser) && !error)
29118 done = true;
29119 cp_parser_abort_tentative_parse (parser);
29121 parser->in_template_argument_list_p = saved_italp;
29122 break;
29124 /* FALLTHRU */
29125 case CPP_CLOSE_PAREN:
29126 case CPP_ELLIPSIS:
29127 /* If we run into a non-nested `;', `}', or `]',
29128 then the code is invalid -- but the default
29129 argument is certainly over. */
29130 case CPP_SEMICOLON:
29131 case CPP_CLOSE_BRACE:
29132 case CPP_CLOSE_SQUARE:
29133 if (depth == 0
29134 /* Handle correctly int n = sizeof ... ( p ); */
29135 && token->type != CPP_ELLIPSIS)
29136 done = true;
29137 /* Update DEPTH, if necessary. */
29138 else if (token->type == CPP_CLOSE_PAREN
29139 || token->type == CPP_CLOSE_BRACE
29140 || token->type == CPP_CLOSE_SQUARE)
29141 --depth;
29142 break;
29144 case CPP_OPEN_PAREN:
29145 case CPP_OPEN_SQUARE:
29146 case CPP_OPEN_BRACE:
29147 ++depth;
29148 break;
29150 case CPP_LESS:
29151 if (depth == 0)
29152 /* This might be the comparison operator, or it might
29153 start a template argument list. */
29154 ++maybe_template_id;
29155 break;
29157 case CPP_RSHIFT:
29158 if (cxx_dialect == cxx98)
29159 break;
29160 /* Fall through for C++0x, which treats the `>>'
29161 operator like two `>' tokens in certain
29162 cases. */
29163 gcc_fallthrough ();
29165 case CPP_GREATER:
29166 if (depth == 0)
29168 /* This might be an operator, or it might close a
29169 template argument list. But if a previous '<'
29170 started a template argument list, this will have
29171 closed it, so we can't be in one anymore. */
29172 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29173 if (maybe_template_id < 0)
29174 maybe_template_id = 0;
29176 break;
29178 /* If we run out of tokens, issue an error message. */
29179 case CPP_EOF:
29180 case CPP_PRAGMA_EOL:
29181 error_at (token->location, "file ends in default argument");
29182 return error_mark_node;
29184 case CPP_NAME:
29185 case CPP_SCOPE:
29186 /* In these cases, we should look for template-ids.
29187 For example, if the default argument is
29188 `X<int, double>()', we need to do name lookup to
29189 figure out whether or not `X' is a template; if
29190 so, the `,' does not end the default argument.
29192 That is not yet done. */
29193 break;
29195 default:
29196 break;
29199 /* If we've reached the end, stop. */
29200 if (done)
29201 break;
29203 /* Add the token to the token block. */
29204 token = cp_lexer_consume_token (parser->lexer);
29207 /* Create a DEFAULT_ARG to represent the unparsed default
29208 argument. */
29209 default_argument = make_node (DEFAULT_ARG);
29210 DEFARG_TOKENS (default_argument)
29211 = cp_token_cache_new (first_token, token);
29212 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29214 return default_argument;
29217 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29219 location_t
29220 defarg_location (tree default_argument)
29222 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29223 location_t start = tokens->first->location;
29224 location_t end = tokens->last->location;
29225 return make_location (start, start, end);
29228 /* Begin parsing tentatively. We always save tokens while parsing
29229 tentatively so that if the tentative parsing fails we can restore the
29230 tokens. */
29232 static void
29233 cp_parser_parse_tentatively (cp_parser* parser)
29235 /* Enter a new parsing context. */
29236 parser->context = cp_parser_context_new (parser->context);
29237 /* Begin saving tokens. */
29238 cp_lexer_save_tokens (parser->lexer);
29239 /* In order to avoid repetitive access control error messages,
29240 access checks are queued up until we are no longer parsing
29241 tentatively. */
29242 push_deferring_access_checks (dk_deferred);
29245 /* Commit to the currently active tentative parse. */
29247 static void
29248 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29250 cp_parser_context *context;
29251 cp_lexer *lexer;
29253 /* Mark all of the levels as committed. */
29254 lexer = parser->lexer;
29255 for (context = parser->context; context->next; context = context->next)
29257 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29258 break;
29259 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29260 while (!cp_lexer_saving_tokens (lexer))
29261 lexer = lexer->next;
29262 cp_lexer_commit_tokens (lexer);
29266 /* Commit to the topmost currently active tentative parse.
29268 Note that this function shouldn't be called when there are
29269 irreversible side-effects while in a tentative state. For
29270 example, we shouldn't create a permanent entry in the symbol
29271 table, or issue an error message that might not apply if the
29272 tentative parse is aborted. */
29274 static void
29275 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29277 cp_parser_context *context = parser->context;
29278 cp_lexer *lexer = parser->lexer;
29280 if (context)
29282 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29283 return;
29284 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29286 while (!cp_lexer_saving_tokens (lexer))
29287 lexer = lexer->next;
29288 cp_lexer_commit_tokens (lexer);
29292 /* Abort the currently active tentative parse. All consumed tokens
29293 will be rolled back, and no diagnostics will be issued. */
29295 static void
29296 cp_parser_abort_tentative_parse (cp_parser* parser)
29298 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29299 || errorcount > 0);
29300 cp_parser_simulate_error (parser);
29301 /* Now, pretend that we want to see if the construct was
29302 successfully parsed. */
29303 cp_parser_parse_definitely (parser);
29306 /* Stop parsing tentatively. If a parse error has occurred, restore the
29307 token stream. Otherwise, commit to the tokens we have consumed.
29308 Returns true if no error occurred; false otherwise. */
29310 static bool
29311 cp_parser_parse_definitely (cp_parser* parser)
29313 bool error_occurred;
29314 cp_parser_context *context;
29316 /* Remember whether or not an error occurred, since we are about to
29317 destroy that information. */
29318 error_occurred = cp_parser_error_occurred (parser);
29319 /* Remove the topmost context from the stack. */
29320 context = parser->context;
29321 parser->context = context->next;
29322 /* If no parse errors occurred, commit to the tentative parse. */
29323 if (!error_occurred)
29325 /* Commit to the tokens read tentatively, unless that was
29326 already done. */
29327 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29328 cp_lexer_commit_tokens (parser->lexer);
29330 pop_to_parent_deferring_access_checks ();
29332 /* Otherwise, if errors occurred, roll back our state so that things
29333 are just as they were before we began the tentative parse. */
29334 else
29336 cp_lexer_rollback_tokens (parser->lexer);
29337 pop_deferring_access_checks ();
29339 /* Add the context to the front of the free list. */
29340 context->next = cp_parser_context_free_list;
29341 cp_parser_context_free_list = context;
29343 return !error_occurred;
29346 /* Returns true if we are parsing tentatively and are not committed to
29347 this tentative parse. */
29349 static bool
29350 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29352 return (cp_parser_parsing_tentatively (parser)
29353 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29356 /* Returns nonzero iff an error has occurred during the most recent
29357 tentative parse. */
29359 static bool
29360 cp_parser_error_occurred (cp_parser* parser)
29362 return (cp_parser_parsing_tentatively (parser)
29363 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29366 /* Returns nonzero if GNU extensions are allowed. */
29368 static bool
29369 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29371 return parser->allow_gnu_extensions_p;
29374 /* Objective-C++ Productions */
29377 /* Parse an Objective-C expression, which feeds into a primary-expression
29378 above.
29380 objc-expression:
29381 objc-message-expression
29382 objc-string-literal
29383 objc-encode-expression
29384 objc-protocol-expression
29385 objc-selector-expression
29387 Returns a tree representation of the expression. */
29389 static cp_expr
29390 cp_parser_objc_expression (cp_parser* parser)
29392 /* Try to figure out what kind of declaration is present. */
29393 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29395 switch (kwd->type)
29397 case CPP_OPEN_SQUARE:
29398 return cp_parser_objc_message_expression (parser);
29400 case CPP_OBJC_STRING:
29401 kwd = cp_lexer_consume_token (parser->lexer);
29402 return objc_build_string_object (kwd->u.value);
29404 case CPP_KEYWORD:
29405 switch (kwd->keyword)
29407 case RID_AT_ENCODE:
29408 return cp_parser_objc_encode_expression (parser);
29410 case RID_AT_PROTOCOL:
29411 return cp_parser_objc_protocol_expression (parser);
29413 case RID_AT_SELECTOR:
29414 return cp_parser_objc_selector_expression (parser);
29416 default:
29417 break;
29419 /* FALLTHRU */
29420 default:
29421 error_at (kwd->location,
29422 "misplaced %<@%D%> Objective-C++ construct",
29423 kwd->u.value);
29424 cp_parser_skip_to_end_of_block_or_statement (parser);
29427 return error_mark_node;
29430 /* Parse an Objective-C message expression.
29432 objc-message-expression:
29433 [ objc-message-receiver objc-message-args ]
29435 Returns a representation of an Objective-C message. */
29437 static tree
29438 cp_parser_objc_message_expression (cp_parser* parser)
29440 tree receiver, messageargs;
29442 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29443 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29444 receiver = cp_parser_objc_message_receiver (parser);
29445 messageargs = cp_parser_objc_message_args (parser);
29446 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29449 tree result = objc_build_message_expr (receiver, messageargs);
29451 /* Construct a location e.g.
29452 [self func1:5]
29453 ^~~~~~~~~~~~~~
29454 ranging from the '[' to the ']', with the caret at the start. */
29455 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29456 protected_set_expr_location (result, combined_loc);
29458 return result;
29461 /* Parse an objc-message-receiver.
29463 objc-message-receiver:
29464 expression
29465 simple-type-specifier
29467 Returns a representation of the type or expression. */
29469 static tree
29470 cp_parser_objc_message_receiver (cp_parser* parser)
29472 tree rcv;
29474 /* An Objective-C message receiver may be either (1) a type
29475 or (2) an expression. */
29476 cp_parser_parse_tentatively (parser);
29477 rcv = cp_parser_expression (parser);
29479 /* If that worked out, fine. */
29480 if (cp_parser_parse_definitely (parser))
29481 return rcv;
29483 cp_parser_parse_tentatively (parser);
29484 rcv = cp_parser_simple_type_specifier (parser,
29485 /*decl_specs=*/NULL,
29486 CP_PARSER_FLAGS_NONE);
29488 if (cp_parser_parse_definitely (parser))
29489 return objc_get_class_reference (rcv);
29491 cp_parser_error (parser, "objective-c++ message receiver expected");
29492 return error_mark_node;
29495 /* Parse the arguments and selectors comprising an Objective-C message.
29497 objc-message-args:
29498 objc-selector
29499 objc-selector-args
29500 objc-selector-args , objc-comma-args
29502 objc-selector-args:
29503 objc-selector [opt] : assignment-expression
29504 objc-selector-args objc-selector [opt] : assignment-expression
29506 objc-comma-args:
29507 assignment-expression
29508 objc-comma-args , assignment-expression
29510 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29511 selector arguments and TREE_VALUE containing a list of comma
29512 arguments. */
29514 static tree
29515 cp_parser_objc_message_args (cp_parser* parser)
29517 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29518 bool maybe_unary_selector_p = true;
29519 cp_token *token = cp_lexer_peek_token (parser->lexer);
29521 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29523 tree selector = NULL_TREE, arg;
29525 if (token->type != CPP_COLON)
29526 selector = cp_parser_objc_selector (parser);
29528 /* Detect if we have a unary selector. */
29529 if (maybe_unary_selector_p
29530 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29531 return build_tree_list (selector, NULL_TREE);
29533 maybe_unary_selector_p = false;
29534 cp_parser_require (parser, CPP_COLON, RT_COLON);
29535 arg = cp_parser_assignment_expression (parser);
29537 sel_args
29538 = chainon (sel_args,
29539 build_tree_list (selector, arg));
29541 token = cp_lexer_peek_token (parser->lexer);
29544 /* Handle non-selector arguments, if any. */
29545 while (token->type == CPP_COMMA)
29547 tree arg;
29549 cp_lexer_consume_token (parser->lexer);
29550 arg = cp_parser_assignment_expression (parser);
29552 addl_args
29553 = chainon (addl_args,
29554 build_tree_list (NULL_TREE, arg));
29556 token = cp_lexer_peek_token (parser->lexer);
29559 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29561 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29562 return build_tree_list (error_mark_node, error_mark_node);
29565 return build_tree_list (sel_args, addl_args);
29568 /* Parse an Objective-C encode expression.
29570 objc-encode-expression:
29571 @encode objc-typename
29573 Returns an encoded representation of the type argument. */
29575 static cp_expr
29576 cp_parser_objc_encode_expression (cp_parser* parser)
29578 tree type;
29579 cp_token *token;
29580 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29582 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29583 matching_parens parens;
29584 parens.require_open (parser);
29585 token = cp_lexer_peek_token (parser->lexer);
29586 type = complete_type (cp_parser_type_id (parser));
29587 parens.require_close (parser);
29589 if (!type)
29591 error_at (token->location,
29592 "%<@encode%> must specify a type as an argument");
29593 return error_mark_node;
29596 /* This happens if we find @encode(T) (where T is a template
29597 typename or something dependent on a template typename) when
29598 parsing a template. In that case, we can't compile it
29599 immediately, but we rather create an AT_ENCODE_EXPR which will
29600 need to be instantiated when the template is used.
29602 if (dependent_type_p (type))
29604 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29605 TREE_READONLY (value) = 1;
29606 return value;
29610 /* Build a location of the form:
29611 @encode(int)
29612 ^~~~~~~~~~~~
29613 with caret==start at the @ token, finishing at the close paren. */
29614 location_t combined_loc
29615 = make_location (start_loc, start_loc,
29616 cp_lexer_previous_token (parser->lexer)->location);
29618 return cp_expr (objc_build_encode_expr (type), combined_loc);
29621 /* Parse an Objective-C @defs expression. */
29623 static tree
29624 cp_parser_objc_defs_expression (cp_parser *parser)
29626 tree name;
29628 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29629 matching_parens parens;
29630 parens.require_open (parser);
29631 name = cp_parser_identifier (parser);
29632 parens.require_close (parser);
29634 return objc_get_class_ivars (name);
29637 /* Parse an Objective-C protocol expression.
29639 objc-protocol-expression:
29640 @protocol ( identifier )
29642 Returns a representation of the protocol expression. */
29644 static tree
29645 cp_parser_objc_protocol_expression (cp_parser* parser)
29647 tree proto;
29648 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29650 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29651 matching_parens parens;
29652 parens.require_open (parser);
29653 proto = cp_parser_identifier (parser);
29654 parens.require_close (parser);
29656 /* Build a location of the form:
29657 @protocol(prot)
29658 ^~~~~~~~~~~~~~~
29659 with caret==start at the @ token, finishing at the close paren. */
29660 location_t combined_loc
29661 = make_location (start_loc, start_loc,
29662 cp_lexer_previous_token (parser->lexer)->location);
29663 tree result = objc_build_protocol_expr (proto);
29664 protected_set_expr_location (result, combined_loc);
29665 return result;
29668 /* Parse an Objective-C selector expression.
29670 objc-selector-expression:
29671 @selector ( objc-method-signature )
29673 objc-method-signature:
29674 objc-selector
29675 objc-selector-seq
29677 objc-selector-seq:
29678 objc-selector :
29679 objc-selector-seq objc-selector :
29681 Returns a representation of the method selector. */
29683 static tree
29684 cp_parser_objc_selector_expression (cp_parser* parser)
29686 tree sel_seq = NULL_TREE;
29687 bool maybe_unary_selector_p = true;
29688 cp_token *token;
29689 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29691 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29692 matching_parens parens;
29693 parens.require_open (parser);
29694 token = cp_lexer_peek_token (parser->lexer);
29696 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29697 || token->type == CPP_SCOPE)
29699 tree selector = NULL_TREE;
29701 if (token->type != CPP_COLON
29702 || token->type == CPP_SCOPE)
29703 selector = cp_parser_objc_selector (parser);
29705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29706 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29708 /* Detect if we have a unary selector. */
29709 if (maybe_unary_selector_p)
29711 sel_seq = selector;
29712 goto finish_selector;
29714 else
29716 cp_parser_error (parser, "expected %<:%>");
29719 maybe_unary_selector_p = false;
29720 token = cp_lexer_consume_token (parser->lexer);
29722 if (token->type == CPP_SCOPE)
29724 sel_seq
29725 = chainon (sel_seq,
29726 build_tree_list (selector, NULL_TREE));
29727 sel_seq
29728 = chainon (sel_seq,
29729 build_tree_list (NULL_TREE, NULL_TREE));
29731 else
29732 sel_seq
29733 = chainon (sel_seq,
29734 build_tree_list (selector, NULL_TREE));
29736 token = cp_lexer_peek_token (parser->lexer);
29739 finish_selector:
29740 parens.require_close (parser);
29743 /* Build a location of the form:
29744 @selector(func)
29745 ^~~~~~~~~~~~~~~
29746 with caret==start at the @ token, finishing at the close paren. */
29747 location_t combined_loc
29748 = make_location (loc, loc,
29749 cp_lexer_previous_token (parser->lexer)->location);
29750 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29751 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29752 protected_set_expr_location (result, combined_loc);
29753 return result;
29756 /* Parse a list of identifiers.
29758 objc-identifier-list:
29759 identifier
29760 objc-identifier-list , identifier
29762 Returns a TREE_LIST of identifier nodes. */
29764 static tree
29765 cp_parser_objc_identifier_list (cp_parser* parser)
29767 tree identifier;
29768 tree list;
29769 cp_token *sep;
29771 identifier = cp_parser_identifier (parser);
29772 if (identifier == error_mark_node)
29773 return error_mark_node;
29775 list = build_tree_list (NULL_TREE, identifier);
29776 sep = cp_lexer_peek_token (parser->lexer);
29778 while (sep->type == CPP_COMMA)
29780 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29781 identifier = cp_parser_identifier (parser);
29782 if (identifier == error_mark_node)
29783 return list;
29785 list = chainon (list, build_tree_list (NULL_TREE,
29786 identifier));
29787 sep = cp_lexer_peek_token (parser->lexer);
29790 return list;
29793 /* Parse an Objective-C alias declaration.
29795 objc-alias-declaration:
29796 @compatibility_alias identifier identifier ;
29798 This function registers the alias mapping with the Objective-C front end.
29799 It returns nothing. */
29801 static void
29802 cp_parser_objc_alias_declaration (cp_parser* parser)
29804 tree alias, orig;
29806 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29807 alias = cp_parser_identifier (parser);
29808 orig = cp_parser_identifier (parser);
29809 objc_declare_alias (alias, orig);
29810 cp_parser_consume_semicolon_at_end_of_statement (parser);
29813 /* Parse an Objective-C class forward-declaration.
29815 objc-class-declaration:
29816 @class objc-identifier-list ;
29818 The function registers the forward declarations with the Objective-C
29819 front end. It returns nothing. */
29821 static void
29822 cp_parser_objc_class_declaration (cp_parser* parser)
29824 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29825 while (true)
29827 tree id;
29829 id = cp_parser_identifier (parser);
29830 if (id == error_mark_node)
29831 break;
29833 objc_declare_class (id);
29835 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29836 cp_lexer_consume_token (parser->lexer);
29837 else
29838 break;
29840 cp_parser_consume_semicolon_at_end_of_statement (parser);
29843 /* Parse a list of Objective-C protocol references.
29845 objc-protocol-refs-opt:
29846 objc-protocol-refs [opt]
29848 objc-protocol-refs:
29849 < objc-identifier-list >
29851 Returns a TREE_LIST of identifiers, if any. */
29853 static tree
29854 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29856 tree protorefs = NULL_TREE;
29858 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29860 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29861 protorefs = cp_parser_objc_identifier_list (parser);
29862 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29865 return protorefs;
29868 /* Parse a Objective-C visibility specification. */
29870 static void
29871 cp_parser_objc_visibility_spec (cp_parser* parser)
29873 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29875 switch (vis->keyword)
29877 case RID_AT_PRIVATE:
29878 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29879 break;
29880 case RID_AT_PROTECTED:
29881 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29882 break;
29883 case RID_AT_PUBLIC:
29884 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29885 break;
29886 case RID_AT_PACKAGE:
29887 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29888 break;
29889 default:
29890 return;
29893 /* Eat '@private'/'@protected'/'@public'. */
29894 cp_lexer_consume_token (parser->lexer);
29897 /* Parse an Objective-C method type. Return 'true' if it is a class
29898 (+) method, and 'false' if it is an instance (-) method. */
29900 static inline bool
29901 cp_parser_objc_method_type (cp_parser* parser)
29903 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29904 return true;
29905 else
29906 return false;
29909 /* Parse an Objective-C protocol qualifier. */
29911 static tree
29912 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29914 tree quals = NULL_TREE, node;
29915 cp_token *token = cp_lexer_peek_token (parser->lexer);
29917 node = token->u.value;
29919 while (node && identifier_p (node)
29920 && (node == ridpointers [(int) RID_IN]
29921 || node == ridpointers [(int) RID_OUT]
29922 || node == ridpointers [(int) RID_INOUT]
29923 || node == ridpointers [(int) RID_BYCOPY]
29924 || node == ridpointers [(int) RID_BYREF]
29925 || node == ridpointers [(int) RID_ONEWAY]))
29927 quals = tree_cons (NULL_TREE, node, quals);
29928 cp_lexer_consume_token (parser->lexer);
29929 token = cp_lexer_peek_token (parser->lexer);
29930 node = token->u.value;
29933 return quals;
29936 /* Parse an Objective-C typename. */
29938 static tree
29939 cp_parser_objc_typename (cp_parser* parser)
29941 tree type_name = NULL_TREE;
29943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29945 tree proto_quals, cp_type = NULL_TREE;
29947 matching_parens parens;
29948 parens.consume_open (parser); /* Eat '('. */
29949 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29951 /* An ObjC type name may consist of just protocol qualifiers, in which
29952 case the type shall default to 'id'. */
29953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29955 cp_type = cp_parser_type_id (parser);
29957 /* If the type could not be parsed, an error has already
29958 been produced. For error recovery, behave as if it had
29959 not been specified, which will use the default type
29960 'id'. */
29961 if (cp_type == error_mark_node)
29963 cp_type = NULL_TREE;
29964 /* We need to skip to the closing parenthesis as
29965 cp_parser_type_id() does not seem to do it for
29966 us. */
29967 cp_parser_skip_to_closing_parenthesis (parser,
29968 /*recovering=*/true,
29969 /*or_comma=*/false,
29970 /*consume_paren=*/false);
29974 parens.require_close (parser);
29975 type_name = build_tree_list (proto_quals, cp_type);
29978 return type_name;
29981 /* Check to see if TYPE refers to an Objective-C selector name. */
29983 static bool
29984 cp_parser_objc_selector_p (enum cpp_ttype type)
29986 return (type == CPP_NAME || type == CPP_KEYWORD
29987 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29988 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29989 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29990 || type == CPP_XOR || type == CPP_XOR_EQ);
29993 /* Parse an Objective-C selector. */
29995 static tree
29996 cp_parser_objc_selector (cp_parser* parser)
29998 cp_token *token = cp_lexer_consume_token (parser->lexer);
30000 if (!cp_parser_objc_selector_p (token->type))
30002 error_at (token->location, "invalid Objective-C++ selector name");
30003 return error_mark_node;
30006 /* C++ operator names are allowed to appear in ObjC selectors. */
30007 switch (token->type)
30009 case CPP_AND_AND: return get_identifier ("and");
30010 case CPP_AND_EQ: return get_identifier ("and_eq");
30011 case CPP_AND: return get_identifier ("bitand");
30012 case CPP_OR: return get_identifier ("bitor");
30013 case CPP_COMPL: return get_identifier ("compl");
30014 case CPP_NOT: return get_identifier ("not");
30015 case CPP_NOT_EQ: return get_identifier ("not_eq");
30016 case CPP_OR_OR: return get_identifier ("or");
30017 case CPP_OR_EQ: return get_identifier ("or_eq");
30018 case CPP_XOR: return get_identifier ("xor");
30019 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30020 default: return token->u.value;
30024 /* Parse an Objective-C params list. */
30026 static tree
30027 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30029 tree params = NULL_TREE;
30030 bool maybe_unary_selector_p = true;
30031 cp_token *token = cp_lexer_peek_token (parser->lexer);
30033 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30035 tree selector = NULL_TREE, type_name, identifier;
30036 tree parm_attr = NULL_TREE;
30038 if (token->keyword == RID_ATTRIBUTE)
30039 break;
30041 if (token->type != CPP_COLON)
30042 selector = cp_parser_objc_selector (parser);
30044 /* Detect if we have a unary selector. */
30045 if (maybe_unary_selector_p
30046 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30048 params = selector; /* Might be followed by attributes. */
30049 break;
30052 maybe_unary_selector_p = false;
30053 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30055 /* Something went quite wrong. There should be a colon
30056 here, but there is not. Stop parsing parameters. */
30057 break;
30059 type_name = cp_parser_objc_typename (parser);
30060 /* New ObjC allows attributes on parameters too. */
30061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30062 parm_attr = cp_parser_attributes_opt (parser);
30063 identifier = cp_parser_identifier (parser);
30065 params
30066 = chainon (params,
30067 objc_build_keyword_decl (selector,
30068 type_name,
30069 identifier,
30070 parm_attr));
30072 token = cp_lexer_peek_token (parser->lexer);
30075 if (params == NULL_TREE)
30077 cp_parser_error (parser, "objective-c++ method declaration is expected");
30078 return error_mark_node;
30081 /* We allow tail attributes for the method. */
30082 if (token->keyword == RID_ATTRIBUTE)
30084 *attributes = cp_parser_attributes_opt (parser);
30085 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30086 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30087 return params;
30088 cp_parser_error (parser,
30089 "method attributes must be specified at the end");
30090 return error_mark_node;
30093 if (params == NULL_TREE)
30095 cp_parser_error (parser, "objective-c++ method declaration is expected");
30096 return error_mark_node;
30098 return params;
30101 /* Parse the non-keyword Objective-C params. */
30103 static tree
30104 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30105 tree* attributes)
30107 tree params = make_node (TREE_LIST);
30108 cp_token *token = cp_lexer_peek_token (parser->lexer);
30109 *ellipsisp = false; /* Initially, assume no ellipsis. */
30111 while (token->type == CPP_COMMA)
30113 cp_parameter_declarator *parmdecl;
30114 tree parm;
30116 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30117 token = cp_lexer_peek_token (parser->lexer);
30119 if (token->type == CPP_ELLIPSIS)
30121 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30122 *ellipsisp = true;
30123 token = cp_lexer_peek_token (parser->lexer);
30124 break;
30127 /* TODO: parse attributes for tail parameters. */
30128 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30129 parm = grokdeclarator (parmdecl->declarator,
30130 &parmdecl->decl_specifiers,
30131 PARM, /*initialized=*/0,
30132 /*attrlist=*/NULL);
30134 chainon (params, build_tree_list (NULL_TREE, parm));
30135 token = cp_lexer_peek_token (parser->lexer);
30138 /* We allow tail attributes for the method. */
30139 if (token->keyword == RID_ATTRIBUTE)
30141 if (*attributes == NULL_TREE)
30143 *attributes = cp_parser_attributes_opt (parser);
30144 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30145 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30146 return params;
30148 else
30149 /* We have an error, but parse the attributes, so that we can
30150 carry on. */
30151 *attributes = cp_parser_attributes_opt (parser);
30153 cp_parser_error (parser,
30154 "method attributes must be specified at the end");
30155 return error_mark_node;
30158 return params;
30161 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30163 static void
30164 cp_parser_objc_interstitial_code (cp_parser* parser)
30166 cp_token *token = cp_lexer_peek_token (parser->lexer);
30168 /* If the next token is `extern' and the following token is a string
30169 literal, then we have a linkage specification. */
30170 if (token->keyword == RID_EXTERN
30171 && cp_parser_is_pure_string_literal
30172 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30173 cp_parser_linkage_specification (parser);
30174 /* Handle #pragma, if any. */
30175 else if (token->type == CPP_PRAGMA)
30176 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30177 /* Allow stray semicolons. */
30178 else if (token->type == CPP_SEMICOLON)
30179 cp_lexer_consume_token (parser->lexer);
30180 /* Mark methods as optional or required, when building protocols. */
30181 else if (token->keyword == RID_AT_OPTIONAL)
30183 cp_lexer_consume_token (parser->lexer);
30184 objc_set_method_opt (true);
30186 else if (token->keyword == RID_AT_REQUIRED)
30188 cp_lexer_consume_token (parser->lexer);
30189 objc_set_method_opt (false);
30191 else if (token->keyword == RID_NAMESPACE)
30192 cp_parser_namespace_definition (parser);
30193 /* Other stray characters must generate errors. */
30194 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30196 cp_lexer_consume_token (parser->lexer);
30197 error ("stray %qs between Objective-C++ methods",
30198 token->type == CPP_OPEN_BRACE ? "{" : "}");
30200 /* Finally, try to parse a block-declaration, or a function-definition. */
30201 else
30202 cp_parser_block_declaration (parser, /*statement_p=*/false);
30205 /* Parse a method signature. */
30207 static tree
30208 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30210 tree rettype, kwdparms, optparms;
30211 bool ellipsis = false;
30212 bool is_class_method;
30214 is_class_method = cp_parser_objc_method_type (parser);
30215 rettype = cp_parser_objc_typename (parser);
30216 *attributes = NULL_TREE;
30217 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30218 if (kwdparms == error_mark_node)
30219 return error_mark_node;
30220 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30221 if (optparms == error_mark_node)
30222 return error_mark_node;
30224 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30227 static bool
30228 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30230 tree tattr;
30231 cp_lexer_save_tokens (parser->lexer);
30232 tattr = cp_parser_attributes_opt (parser);
30233 gcc_assert (tattr) ;
30235 /* If the attributes are followed by a method introducer, this is not allowed.
30236 Dump the attributes and flag the situation. */
30237 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30238 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30239 return true;
30241 /* Otherwise, the attributes introduce some interstitial code, possibly so
30242 rewind to allow that check. */
30243 cp_lexer_rollback_tokens (parser->lexer);
30244 return false;
30247 /* Parse an Objective-C method prototype list. */
30249 static void
30250 cp_parser_objc_method_prototype_list (cp_parser* parser)
30252 cp_token *token = cp_lexer_peek_token (parser->lexer);
30254 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30256 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30258 tree attributes, sig;
30259 bool is_class_method;
30260 if (token->type == CPP_PLUS)
30261 is_class_method = true;
30262 else
30263 is_class_method = false;
30264 sig = cp_parser_objc_method_signature (parser, &attributes);
30265 if (sig == error_mark_node)
30267 cp_parser_skip_to_end_of_block_or_statement (parser);
30268 token = cp_lexer_peek_token (parser->lexer);
30269 continue;
30271 objc_add_method_declaration (is_class_method, sig, attributes);
30272 cp_parser_consume_semicolon_at_end_of_statement (parser);
30274 else if (token->keyword == RID_AT_PROPERTY)
30275 cp_parser_objc_at_property_declaration (parser);
30276 else if (token->keyword == RID_ATTRIBUTE
30277 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30278 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30279 OPT_Wattributes,
30280 "prefix attributes are ignored for methods");
30281 else
30282 /* Allow for interspersed non-ObjC++ code. */
30283 cp_parser_objc_interstitial_code (parser);
30285 token = cp_lexer_peek_token (parser->lexer);
30288 if (token->type != CPP_EOF)
30289 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30290 else
30291 cp_parser_error (parser, "expected %<@end%>");
30293 objc_finish_interface ();
30296 /* Parse an Objective-C method definition list. */
30298 static void
30299 cp_parser_objc_method_definition_list (cp_parser* parser)
30301 cp_token *token = cp_lexer_peek_token (parser->lexer);
30303 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30305 tree meth;
30307 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30309 cp_token *ptk;
30310 tree sig, attribute;
30311 bool is_class_method;
30312 if (token->type == CPP_PLUS)
30313 is_class_method = true;
30314 else
30315 is_class_method = false;
30316 push_deferring_access_checks (dk_deferred);
30317 sig = cp_parser_objc_method_signature (parser, &attribute);
30318 if (sig == error_mark_node)
30320 cp_parser_skip_to_end_of_block_or_statement (parser);
30321 token = cp_lexer_peek_token (parser->lexer);
30322 continue;
30324 objc_start_method_definition (is_class_method, sig, attribute,
30325 NULL_TREE);
30327 /* For historical reasons, we accept an optional semicolon. */
30328 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30329 cp_lexer_consume_token (parser->lexer);
30331 ptk = cp_lexer_peek_token (parser->lexer);
30332 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30333 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30335 perform_deferred_access_checks (tf_warning_or_error);
30336 stop_deferring_access_checks ();
30337 meth = cp_parser_function_definition_after_declarator (parser,
30338 false);
30339 pop_deferring_access_checks ();
30340 objc_finish_method_definition (meth);
30343 /* The following case will be removed once @synthesize is
30344 completely implemented. */
30345 else if (token->keyword == RID_AT_PROPERTY)
30346 cp_parser_objc_at_property_declaration (parser);
30347 else if (token->keyword == RID_AT_SYNTHESIZE)
30348 cp_parser_objc_at_synthesize_declaration (parser);
30349 else if (token->keyword == RID_AT_DYNAMIC)
30350 cp_parser_objc_at_dynamic_declaration (parser);
30351 else if (token->keyword == RID_ATTRIBUTE
30352 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30353 warning_at (token->location, OPT_Wattributes,
30354 "prefix attributes are ignored for methods");
30355 else
30356 /* Allow for interspersed non-ObjC++ code. */
30357 cp_parser_objc_interstitial_code (parser);
30359 token = cp_lexer_peek_token (parser->lexer);
30362 if (token->type != CPP_EOF)
30363 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30364 else
30365 cp_parser_error (parser, "expected %<@end%>");
30367 objc_finish_implementation ();
30370 /* Parse Objective-C ivars. */
30372 static void
30373 cp_parser_objc_class_ivars (cp_parser* parser)
30375 cp_token *token = cp_lexer_peek_token (parser->lexer);
30377 if (token->type != CPP_OPEN_BRACE)
30378 return; /* No ivars specified. */
30380 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30381 token = cp_lexer_peek_token (parser->lexer);
30383 while (token->type != CPP_CLOSE_BRACE
30384 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30386 cp_decl_specifier_seq declspecs;
30387 int decl_class_or_enum_p;
30388 tree prefix_attributes;
30390 cp_parser_objc_visibility_spec (parser);
30392 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30393 break;
30395 cp_parser_decl_specifier_seq (parser,
30396 CP_PARSER_FLAGS_OPTIONAL,
30397 &declspecs,
30398 &decl_class_or_enum_p);
30400 /* auto, register, static, extern, mutable. */
30401 if (declspecs.storage_class != sc_none)
30403 cp_parser_error (parser, "invalid type for instance variable");
30404 declspecs.storage_class = sc_none;
30407 /* thread_local. */
30408 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30410 cp_parser_error (parser, "invalid type for instance variable");
30411 declspecs.locations[ds_thread] = 0;
30414 /* typedef. */
30415 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30417 cp_parser_error (parser, "invalid type for instance variable");
30418 declspecs.locations[ds_typedef] = 0;
30421 prefix_attributes = declspecs.attributes;
30422 declspecs.attributes = NULL_TREE;
30424 /* Keep going until we hit the `;' at the end of the
30425 declaration. */
30426 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30428 tree width = NULL_TREE, attributes, first_attribute, decl;
30429 cp_declarator *declarator = NULL;
30430 int ctor_dtor_or_conv_p;
30432 /* Check for a (possibly unnamed) bitfield declaration. */
30433 token = cp_lexer_peek_token (parser->lexer);
30434 if (token->type == CPP_COLON)
30435 goto eat_colon;
30437 if (token->type == CPP_NAME
30438 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30439 == CPP_COLON))
30441 /* Get the name of the bitfield. */
30442 declarator = make_id_declarator (NULL_TREE,
30443 cp_parser_identifier (parser),
30444 sfk_none);
30446 eat_colon:
30447 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30448 /* Get the width of the bitfield. */
30449 width
30450 = cp_parser_constant_expression (parser);
30452 else
30454 /* Parse the declarator. */
30455 declarator
30456 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30457 &ctor_dtor_or_conv_p,
30458 /*parenthesized_p=*/NULL,
30459 /*member_p=*/false,
30460 /*friend_p=*/false);
30463 /* Look for attributes that apply to the ivar. */
30464 attributes = cp_parser_attributes_opt (parser);
30465 /* Remember which attributes are prefix attributes and
30466 which are not. */
30467 first_attribute = attributes;
30468 /* Combine the attributes. */
30469 attributes = attr_chainon (prefix_attributes, attributes);
30471 if (width)
30472 /* Create the bitfield declaration. */
30473 decl = grokbitfield (declarator, &declspecs,
30474 width, NULL_TREE, attributes);
30475 else
30476 decl = grokfield (declarator, &declspecs,
30477 NULL_TREE, /*init_const_expr_p=*/false,
30478 NULL_TREE, attributes);
30480 /* Add the instance variable. */
30481 if (decl != error_mark_node && decl != NULL_TREE)
30482 objc_add_instance_variable (decl);
30484 /* Reset PREFIX_ATTRIBUTES. */
30485 if (attributes != error_mark_node)
30487 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30488 attributes = TREE_CHAIN (attributes);
30489 if (attributes)
30490 TREE_CHAIN (attributes) = NULL_TREE;
30493 token = cp_lexer_peek_token (parser->lexer);
30495 if (token->type == CPP_COMMA)
30497 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30498 continue;
30500 break;
30503 cp_parser_consume_semicolon_at_end_of_statement (parser);
30504 token = cp_lexer_peek_token (parser->lexer);
30507 if (token->keyword == RID_AT_END)
30508 cp_parser_error (parser, "expected %<}%>");
30510 /* Do not consume the RID_AT_END, so it will be read again as terminating
30511 the @interface of @implementation. */
30512 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30513 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30515 /* For historical reasons, we accept an optional semicolon. */
30516 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30517 cp_lexer_consume_token (parser->lexer);
30520 /* Parse an Objective-C protocol declaration. */
30522 static void
30523 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30525 tree proto, protorefs;
30526 cp_token *tok;
30528 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30531 tok = cp_lexer_peek_token (parser->lexer);
30532 error_at (tok->location, "identifier expected after %<@protocol%>");
30533 cp_parser_consume_semicolon_at_end_of_statement (parser);
30534 return;
30537 /* See if we have a forward declaration or a definition. */
30538 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30540 /* Try a forward declaration first. */
30541 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30543 while (true)
30545 tree id;
30547 id = cp_parser_identifier (parser);
30548 if (id == error_mark_node)
30549 break;
30551 objc_declare_protocol (id, attributes);
30553 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30554 cp_lexer_consume_token (parser->lexer);
30555 else
30556 break;
30558 cp_parser_consume_semicolon_at_end_of_statement (parser);
30561 /* Ok, we got a full-fledged definition (or at least should). */
30562 else
30564 proto = cp_parser_identifier (parser);
30565 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30566 objc_start_protocol (proto, protorefs, attributes);
30567 cp_parser_objc_method_prototype_list (parser);
30571 /* Parse an Objective-C superclass or category. */
30573 static void
30574 cp_parser_objc_superclass_or_category (cp_parser *parser,
30575 bool iface_p,
30576 tree *super,
30577 tree *categ, bool *is_class_extension)
30579 cp_token *next = cp_lexer_peek_token (parser->lexer);
30581 *super = *categ = NULL_TREE;
30582 *is_class_extension = false;
30583 if (next->type == CPP_COLON)
30585 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30586 *super = cp_parser_identifier (parser);
30588 else if (next->type == CPP_OPEN_PAREN)
30590 matching_parens parens;
30591 parens.consume_open (parser); /* Eat '('. */
30593 /* If there is no category name, and this is an @interface, we
30594 have a class extension. */
30595 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30597 *categ = NULL_TREE;
30598 *is_class_extension = true;
30600 else
30601 *categ = cp_parser_identifier (parser);
30603 parens.require_close (parser);
30607 /* Parse an Objective-C class interface. */
30609 static void
30610 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30612 tree name, super, categ, protos;
30613 bool is_class_extension;
30615 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30616 name = cp_parser_identifier (parser);
30617 if (name == error_mark_node)
30619 /* It's hard to recover because even if valid @interface stuff
30620 is to follow, we can't compile it (or validate it) if we
30621 don't even know which class it refers to. Let's assume this
30622 was a stray '@interface' token in the stream and skip it.
30624 return;
30626 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30627 &is_class_extension);
30628 protos = cp_parser_objc_protocol_refs_opt (parser);
30630 /* We have either a class or a category on our hands. */
30631 if (categ || is_class_extension)
30632 objc_start_category_interface (name, categ, protos, attributes);
30633 else
30635 objc_start_class_interface (name, super, protos, attributes);
30636 /* Handle instance variable declarations, if any. */
30637 cp_parser_objc_class_ivars (parser);
30638 objc_continue_interface ();
30641 cp_parser_objc_method_prototype_list (parser);
30644 /* Parse an Objective-C class implementation. */
30646 static void
30647 cp_parser_objc_class_implementation (cp_parser* parser)
30649 tree name, super, categ;
30650 bool is_class_extension;
30652 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30653 name = cp_parser_identifier (parser);
30654 if (name == error_mark_node)
30656 /* It's hard to recover because even if valid @implementation
30657 stuff is to follow, we can't compile it (or validate it) if
30658 we don't even know which class it refers to. Let's assume
30659 this was a stray '@implementation' token in the stream and
30660 skip it.
30662 return;
30664 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30665 &is_class_extension);
30667 /* We have either a class or a category on our hands. */
30668 if (categ)
30669 objc_start_category_implementation (name, categ);
30670 else
30672 objc_start_class_implementation (name, super);
30673 /* Handle instance variable declarations, if any. */
30674 cp_parser_objc_class_ivars (parser);
30675 objc_continue_implementation ();
30678 cp_parser_objc_method_definition_list (parser);
30681 /* Consume the @end token and finish off the implementation. */
30683 static void
30684 cp_parser_objc_end_implementation (cp_parser* parser)
30686 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30687 objc_finish_implementation ();
30690 /* Parse an Objective-C declaration. */
30692 static void
30693 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30695 /* Try to figure out what kind of declaration is present. */
30696 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30698 if (attributes)
30699 switch (kwd->keyword)
30701 case RID_AT_ALIAS:
30702 case RID_AT_CLASS:
30703 case RID_AT_END:
30704 error_at (kwd->location, "attributes may not be specified before"
30705 " the %<@%D%> Objective-C++ keyword",
30706 kwd->u.value);
30707 attributes = NULL;
30708 break;
30709 case RID_AT_IMPLEMENTATION:
30710 warning_at (kwd->location, OPT_Wattributes,
30711 "prefix attributes are ignored before %<@%D%>",
30712 kwd->u.value);
30713 attributes = NULL;
30714 default:
30715 break;
30718 switch (kwd->keyword)
30720 case RID_AT_ALIAS:
30721 cp_parser_objc_alias_declaration (parser);
30722 break;
30723 case RID_AT_CLASS:
30724 cp_parser_objc_class_declaration (parser);
30725 break;
30726 case RID_AT_PROTOCOL:
30727 cp_parser_objc_protocol_declaration (parser, attributes);
30728 break;
30729 case RID_AT_INTERFACE:
30730 cp_parser_objc_class_interface (parser, attributes);
30731 break;
30732 case RID_AT_IMPLEMENTATION:
30733 cp_parser_objc_class_implementation (parser);
30734 break;
30735 case RID_AT_END:
30736 cp_parser_objc_end_implementation (parser);
30737 break;
30738 default:
30739 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30740 kwd->u.value);
30741 cp_parser_skip_to_end_of_block_or_statement (parser);
30745 /* Parse an Objective-C try-catch-finally statement.
30747 objc-try-catch-finally-stmt:
30748 @try compound-statement objc-catch-clause-seq [opt]
30749 objc-finally-clause [opt]
30751 objc-catch-clause-seq:
30752 objc-catch-clause objc-catch-clause-seq [opt]
30754 objc-catch-clause:
30755 @catch ( objc-exception-declaration ) compound-statement
30757 objc-finally-clause:
30758 @finally compound-statement
30760 objc-exception-declaration:
30761 parameter-declaration
30762 '...'
30764 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30766 Returns NULL_TREE.
30768 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30769 for C. Keep them in sync. */
30771 static tree
30772 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30774 location_t location;
30775 tree stmt;
30777 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30778 location = cp_lexer_peek_token (parser->lexer)->location;
30779 objc_maybe_warn_exceptions (location);
30780 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30781 node, lest it get absorbed into the surrounding block. */
30782 stmt = push_stmt_list ();
30783 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30784 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30786 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30788 cp_parameter_declarator *parm;
30789 tree parameter_declaration = error_mark_node;
30790 bool seen_open_paren = false;
30791 matching_parens parens;
30793 cp_lexer_consume_token (parser->lexer);
30794 if (parens.require_open (parser))
30795 seen_open_paren = true;
30796 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30798 /* We have "@catch (...)" (where the '...' are literally
30799 what is in the code). Skip the '...'.
30800 parameter_declaration is set to NULL_TREE, and
30801 objc_being_catch_clauses() knows that that means
30802 '...'. */
30803 cp_lexer_consume_token (parser->lexer);
30804 parameter_declaration = NULL_TREE;
30806 else
30808 /* We have "@catch (NSException *exception)" or something
30809 like that. Parse the parameter declaration. */
30810 parm = cp_parser_parameter_declaration (parser, false, NULL);
30811 if (parm == NULL)
30812 parameter_declaration = error_mark_node;
30813 else
30814 parameter_declaration = grokdeclarator (parm->declarator,
30815 &parm->decl_specifiers,
30816 PARM, /*initialized=*/0,
30817 /*attrlist=*/NULL);
30819 if (seen_open_paren)
30820 parens.require_close (parser);
30821 else
30823 /* If there was no open parenthesis, we are recovering from
30824 an error, and we are trying to figure out what mistake
30825 the user has made. */
30827 /* If there is an immediate closing parenthesis, the user
30828 probably forgot the opening one (ie, they typed "@catch
30829 NSException *e)". Parse the closing parenthesis and keep
30830 going. */
30831 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30832 cp_lexer_consume_token (parser->lexer);
30834 /* If these is no immediate closing parenthesis, the user
30835 probably doesn't know that parenthesis are required at
30836 all (ie, they typed "@catch NSException *e"). So, just
30837 forget about the closing parenthesis and keep going. */
30839 objc_begin_catch_clause (parameter_declaration);
30840 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30841 objc_finish_catch_clause ();
30843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30845 cp_lexer_consume_token (parser->lexer);
30846 location = cp_lexer_peek_token (parser->lexer)->location;
30847 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30848 node, lest it get absorbed into the surrounding block. */
30849 stmt = push_stmt_list ();
30850 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30851 objc_build_finally_clause (location, pop_stmt_list (stmt));
30854 return objc_finish_try_stmt ();
30857 /* Parse an Objective-C synchronized statement.
30859 objc-synchronized-stmt:
30860 @synchronized ( expression ) compound-statement
30862 Returns NULL_TREE. */
30864 static tree
30865 cp_parser_objc_synchronized_statement (cp_parser *parser)
30867 location_t location;
30868 tree lock, stmt;
30870 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30872 location = cp_lexer_peek_token (parser->lexer)->location;
30873 objc_maybe_warn_exceptions (location);
30874 matching_parens parens;
30875 parens.require_open (parser);
30876 lock = cp_parser_expression (parser);
30877 parens.require_close (parser);
30879 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30880 node, lest it get absorbed into the surrounding block. */
30881 stmt = push_stmt_list ();
30882 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30884 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30887 /* Parse an Objective-C throw statement.
30889 objc-throw-stmt:
30890 @throw assignment-expression [opt] ;
30892 Returns a constructed '@throw' statement. */
30894 static tree
30895 cp_parser_objc_throw_statement (cp_parser *parser)
30897 tree expr = NULL_TREE;
30898 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30900 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30903 expr = cp_parser_expression (parser);
30905 cp_parser_consume_semicolon_at_end_of_statement (parser);
30907 return objc_build_throw_stmt (loc, expr);
30910 /* Parse an Objective-C statement. */
30912 static tree
30913 cp_parser_objc_statement (cp_parser * parser)
30915 /* Try to figure out what kind of declaration is present. */
30916 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30918 switch (kwd->keyword)
30920 case RID_AT_TRY:
30921 return cp_parser_objc_try_catch_finally_statement (parser);
30922 case RID_AT_SYNCHRONIZED:
30923 return cp_parser_objc_synchronized_statement (parser);
30924 case RID_AT_THROW:
30925 return cp_parser_objc_throw_statement (parser);
30926 default:
30927 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30928 kwd->u.value);
30929 cp_parser_skip_to_end_of_block_or_statement (parser);
30932 return error_mark_node;
30935 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30936 look ahead to see if an objc keyword follows the attributes. This
30937 is to detect the use of prefix attributes on ObjC @interface and
30938 @protocol. */
30940 static bool
30941 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30943 cp_lexer_save_tokens (parser->lexer);
30944 *attrib = cp_parser_attributes_opt (parser);
30945 gcc_assert (*attrib);
30946 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30948 cp_lexer_commit_tokens (parser->lexer);
30949 return true;
30951 cp_lexer_rollback_tokens (parser->lexer);
30952 return false;
30955 /* This routine is a minimal replacement for
30956 c_parser_struct_declaration () used when parsing the list of
30957 types/names or ObjC++ properties. For example, when parsing the
30958 code
30960 @property (readonly) int a, b, c;
30962 this function is responsible for parsing "int a, int b, int c" and
30963 returning the declarations as CHAIN of DECLs.
30965 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30966 similar parsing. */
30967 static tree
30968 cp_parser_objc_struct_declaration (cp_parser *parser)
30970 tree decls = NULL_TREE;
30971 cp_decl_specifier_seq declspecs;
30972 int decl_class_or_enum_p;
30973 tree prefix_attributes;
30975 cp_parser_decl_specifier_seq (parser,
30976 CP_PARSER_FLAGS_NONE,
30977 &declspecs,
30978 &decl_class_or_enum_p);
30980 if (declspecs.type == error_mark_node)
30981 return error_mark_node;
30983 /* auto, register, static, extern, mutable. */
30984 if (declspecs.storage_class != sc_none)
30986 cp_parser_error (parser, "invalid type for property");
30987 declspecs.storage_class = sc_none;
30990 /* thread_local. */
30991 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30993 cp_parser_error (parser, "invalid type for property");
30994 declspecs.locations[ds_thread] = 0;
30997 /* typedef. */
30998 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31000 cp_parser_error (parser, "invalid type for property");
31001 declspecs.locations[ds_typedef] = 0;
31004 prefix_attributes = declspecs.attributes;
31005 declspecs.attributes = NULL_TREE;
31007 /* Keep going until we hit the `;' at the end of the declaration. */
31008 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31010 tree attributes, first_attribute, decl;
31011 cp_declarator *declarator;
31012 cp_token *token;
31014 /* Parse the declarator. */
31015 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31016 NULL, NULL, false, false);
31018 /* Look for attributes that apply to the ivar. */
31019 attributes = cp_parser_attributes_opt (parser);
31020 /* Remember which attributes are prefix attributes and
31021 which are not. */
31022 first_attribute = attributes;
31023 /* Combine the attributes. */
31024 attributes = attr_chainon (prefix_attributes, attributes);
31026 decl = grokfield (declarator, &declspecs,
31027 NULL_TREE, /*init_const_expr_p=*/false,
31028 NULL_TREE, attributes);
31030 if (decl == error_mark_node || decl == NULL_TREE)
31031 return error_mark_node;
31033 /* Reset PREFIX_ATTRIBUTES. */
31034 if (attributes != error_mark_node)
31036 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31037 attributes = TREE_CHAIN (attributes);
31038 if (attributes)
31039 TREE_CHAIN (attributes) = NULL_TREE;
31042 DECL_CHAIN (decl) = decls;
31043 decls = decl;
31045 token = cp_lexer_peek_token (parser->lexer);
31046 if (token->type == CPP_COMMA)
31048 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31049 continue;
31051 else
31052 break;
31054 return decls;
31057 /* Parse an Objective-C @property declaration. The syntax is:
31059 objc-property-declaration:
31060 '@property' objc-property-attributes[opt] struct-declaration ;
31062 objc-property-attributes:
31063 '(' objc-property-attribute-list ')'
31065 objc-property-attribute-list:
31066 objc-property-attribute
31067 objc-property-attribute-list, objc-property-attribute
31069 objc-property-attribute
31070 'getter' = identifier
31071 'setter' = identifier
31072 'readonly'
31073 'readwrite'
31074 'assign'
31075 'retain'
31076 'copy'
31077 'nonatomic'
31079 For example:
31080 @property NSString *name;
31081 @property (readonly) id object;
31082 @property (retain, nonatomic, getter=getTheName) id name;
31083 @property int a, b, c;
31085 PS: This function is identical to
31086 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31087 static void
31088 cp_parser_objc_at_property_declaration (cp_parser *parser)
31090 /* The following variables hold the attributes of the properties as
31091 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31092 seen. When we see an attribute, we set them to 'true' (if they
31093 are boolean properties) or to the identifier (if they have an
31094 argument, ie, for getter and setter). Note that here we only
31095 parse the list of attributes, check the syntax and accumulate the
31096 attributes that we find. objc_add_property_declaration() will
31097 then process the information. */
31098 bool property_assign = false;
31099 bool property_copy = false;
31100 tree property_getter_ident = NULL_TREE;
31101 bool property_nonatomic = false;
31102 bool property_readonly = false;
31103 bool property_readwrite = false;
31104 bool property_retain = false;
31105 tree property_setter_ident = NULL_TREE;
31107 /* 'properties' is the list of properties that we read. Usually a
31108 single one, but maybe more (eg, in "@property int a, b, c;" there
31109 are three). */
31110 tree properties;
31111 location_t loc;
31113 loc = cp_lexer_peek_token (parser->lexer)->location;
31115 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31117 /* Parse the optional attribute list... */
31118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31120 /* Eat the '('. */
31121 matching_parens parens;
31122 parens.consume_open (parser);
31124 while (true)
31126 bool syntax_error = false;
31127 cp_token *token = cp_lexer_peek_token (parser->lexer);
31128 enum rid keyword;
31130 if (token->type != CPP_NAME)
31132 cp_parser_error (parser, "expected identifier");
31133 break;
31135 keyword = C_RID_CODE (token->u.value);
31136 cp_lexer_consume_token (parser->lexer);
31137 switch (keyword)
31139 case RID_ASSIGN: property_assign = true; break;
31140 case RID_COPY: property_copy = true; break;
31141 case RID_NONATOMIC: property_nonatomic = true; break;
31142 case RID_READONLY: property_readonly = true; break;
31143 case RID_READWRITE: property_readwrite = true; break;
31144 case RID_RETAIN: property_retain = true; break;
31146 case RID_GETTER:
31147 case RID_SETTER:
31148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31150 if (keyword == RID_GETTER)
31151 cp_parser_error (parser,
31152 "missing %<=%> (after %<getter%> attribute)");
31153 else
31154 cp_parser_error (parser,
31155 "missing %<=%> (after %<setter%> attribute)");
31156 syntax_error = true;
31157 break;
31159 cp_lexer_consume_token (parser->lexer); /* eat the = */
31160 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31162 cp_parser_error (parser, "expected identifier");
31163 syntax_error = true;
31164 break;
31166 if (keyword == RID_SETTER)
31168 if (property_setter_ident != NULL_TREE)
31170 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31171 cp_lexer_consume_token (parser->lexer);
31173 else
31174 property_setter_ident = cp_parser_objc_selector (parser);
31175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31176 cp_parser_error (parser, "setter name must terminate with %<:%>");
31177 else
31178 cp_lexer_consume_token (parser->lexer);
31180 else
31182 if (property_getter_ident != NULL_TREE)
31184 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31185 cp_lexer_consume_token (parser->lexer);
31187 else
31188 property_getter_ident = cp_parser_objc_selector (parser);
31190 break;
31191 default:
31192 cp_parser_error (parser, "unknown property attribute");
31193 syntax_error = true;
31194 break;
31197 if (syntax_error)
31198 break;
31200 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31201 cp_lexer_consume_token (parser->lexer);
31202 else
31203 break;
31206 /* FIXME: "@property (setter, assign);" will generate a spurious
31207 "error: expected ‘)’ before ‘,’ token". This is because
31208 cp_parser_require, unlike the C counterpart, will produce an
31209 error even if we are in error recovery. */
31210 if (!parens.require_close (parser))
31212 cp_parser_skip_to_closing_parenthesis (parser,
31213 /*recovering=*/true,
31214 /*or_comma=*/false,
31215 /*consume_paren=*/true);
31219 /* ... and the property declaration(s). */
31220 properties = cp_parser_objc_struct_declaration (parser);
31222 if (properties == error_mark_node)
31224 cp_parser_skip_to_end_of_statement (parser);
31225 /* If the next token is now a `;', consume it. */
31226 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31227 cp_lexer_consume_token (parser->lexer);
31228 return;
31231 if (properties == NULL_TREE)
31232 cp_parser_error (parser, "expected identifier");
31233 else
31235 /* Comma-separated properties are chained together in
31236 reverse order; add them one by one. */
31237 properties = nreverse (properties);
31239 for (; properties; properties = TREE_CHAIN (properties))
31240 objc_add_property_declaration (loc, copy_node (properties),
31241 property_readonly, property_readwrite,
31242 property_assign, property_retain,
31243 property_copy, property_nonatomic,
31244 property_getter_ident, property_setter_ident);
31247 cp_parser_consume_semicolon_at_end_of_statement (parser);
31250 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31252 objc-synthesize-declaration:
31253 @synthesize objc-synthesize-identifier-list ;
31255 objc-synthesize-identifier-list:
31256 objc-synthesize-identifier
31257 objc-synthesize-identifier-list, objc-synthesize-identifier
31259 objc-synthesize-identifier
31260 identifier
31261 identifier = identifier
31263 For example:
31264 @synthesize MyProperty;
31265 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31267 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31268 for C. Keep them in sync.
31270 static void
31271 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31273 tree list = NULL_TREE;
31274 location_t loc;
31275 loc = cp_lexer_peek_token (parser->lexer)->location;
31277 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31278 while (true)
31280 tree property, ivar;
31281 property = cp_parser_identifier (parser);
31282 if (property == error_mark_node)
31284 cp_parser_consume_semicolon_at_end_of_statement (parser);
31285 return;
31287 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31289 cp_lexer_consume_token (parser->lexer);
31290 ivar = cp_parser_identifier (parser);
31291 if (ivar == error_mark_node)
31293 cp_parser_consume_semicolon_at_end_of_statement (parser);
31294 return;
31297 else
31298 ivar = NULL_TREE;
31299 list = chainon (list, build_tree_list (ivar, property));
31300 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31301 cp_lexer_consume_token (parser->lexer);
31302 else
31303 break;
31305 cp_parser_consume_semicolon_at_end_of_statement (parser);
31306 objc_add_synthesize_declaration (loc, list);
31309 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31311 objc-dynamic-declaration:
31312 @dynamic identifier-list ;
31314 For example:
31315 @dynamic MyProperty;
31316 @dynamic MyProperty, AnotherProperty;
31318 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31319 for C. Keep them in sync.
31321 static void
31322 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31324 tree list = NULL_TREE;
31325 location_t loc;
31326 loc = cp_lexer_peek_token (parser->lexer)->location;
31328 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31329 while (true)
31331 tree property;
31332 property = cp_parser_identifier (parser);
31333 if (property == error_mark_node)
31335 cp_parser_consume_semicolon_at_end_of_statement (parser);
31336 return;
31338 list = chainon (list, build_tree_list (NULL, property));
31339 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31340 cp_lexer_consume_token (parser->lexer);
31341 else
31342 break;
31344 cp_parser_consume_semicolon_at_end_of_statement (parser);
31345 objc_add_dynamic_declaration (loc, list);
31349 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31351 /* Returns name of the next clause.
31352 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31353 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31354 returned and the token is consumed. */
31356 static pragma_omp_clause
31357 cp_parser_omp_clause_name (cp_parser *parser)
31359 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31361 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31362 result = PRAGMA_OACC_CLAUSE_AUTO;
31363 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31364 result = PRAGMA_OMP_CLAUSE_IF;
31365 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31366 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31367 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31368 result = PRAGMA_OACC_CLAUSE_DELETE;
31369 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31370 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31371 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31372 result = PRAGMA_OMP_CLAUSE_FOR;
31373 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31375 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31376 const char *p = IDENTIFIER_POINTER (id);
31378 switch (p[0])
31380 case 'a':
31381 if (!strcmp ("aligned", p))
31382 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31383 else if (!strcmp ("async", p))
31384 result = PRAGMA_OACC_CLAUSE_ASYNC;
31385 break;
31386 case 'c':
31387 if (!strcmp ("collapse", p))
31388 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31389 else if (!strcmp ("copy", p))
31390 result = PRAGMA_OACC_CLAUSE_COPY;
31391 else if (!strcmp ("copyin", p))
31392 result = PRAGMA_OMP_CLAUSE_COPYIN;
31393 else if (!strcmp ("copyout", p))
31394 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31395 else if (!strcmp ("copyprivate", p))
31396 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31397 else if (!strcmp ("create", p))
31398 result = PRAGMA_OACC_CLAUSE_CREATE;
31399 break;
31400 case 'd':
31401 if (!strcmp ("defaultmap", p))
31402 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31403 else if (!strcmp ("depend", p))
31404 result = PRAGMA_OMP_CLAUSE_DEPEND;
31405 else if (!strcmp ("device", p))
31406 result = PRAGMA_OMP_CLAUSE_DEVICE;
31407 else if (!strcmp ("deviceptr", p))
31408 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31409 else if (!strcmp ("device_resident", p))
31410 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31411 else if (!strcmp ("dist_schedule", p))
31412 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31413 break;
31414 case 'f':
31415 if (!strcmp ("final", p))
31416 result = PRAGMA_OMP_CLAUSE_FINAL;
31417 else if (!strcmp ("finalize", p))
31418 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31419 else if (!strcmp ("firstprivate", p))
31420 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31421 else if (!strcmp ("from", p))
31422 result = PRAGMA_OMP_CLAUSE_FROM;
31423 break;
31424 case 'g':
31425 if (!strcmp ("gang", p))
31426 result = PRAGMA_OACC_CLAUSE_GANG;
31427 else if (!strcmp ("grainsize", p))
31428 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31429 break;
31430 case 'h':
31431 if (!strcmp ("hint", p))
31432 result = PRAGMA_OMP_CLAUSE_HINT;
31433 else if (!strcmp ("host", p))
31434 result = PRAGMA_OACC_CLAUSE_HOST;
31435 break;
31436 case 'i':
31437 if (!strcmp ("if_present", p))
31438 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31439 else if (!strcmp ("inbranch", p))
31440 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31441 else if (!strcmp ("independent", p))
31442 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31443 else if (!strcmp ("is_device_ptr", p))
31444 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31445 break;
31446 case 'l':
31447 if (!strcmp ("lastprivate", p))
31448 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31449 else if (!strcmp ("linear", p))
31450 result = PRAGMA_OMP_CLAUSE_LINEAR;
31451 else if (!strcmp ("link", p))
31452 result = PRAGMA_OMP_CLAUSE_LINK;
31453 break;
31454 case 'm':
31455 if (!strcmp ("map", p))
31456 result = PRAGMA_OMP_CLAUSE_MAP;
31457 else if (!strcmp ("mergeable", p))
31458 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31459 break;
31460 case 'n':
31461 if (!strcmp ("nogroup", p))
31462 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31463 else if (!strcmp ("notinbranch", p))
31464 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31465 else if (!strcmp ("nowait", p))
31466 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31467 else if (!strcmp ("num_gangs", p))
31468 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31469 else if (!strcmp ("num_tasks", p))
31470 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31471 else if (!strcmp ("num_teams", p))
31472 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31473 else if (!strcmp ("num_threads", p))
31474 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31475 else if (!strcmp ("num_workers", p))
31476 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31477 break;
31478 case 'o':
31479 if (!strcmp ("ordered", p))
31480 result = PRAGMA_OMP_CLAUSE_ORDERED;
31481 break;
31482 case 'p':
31483 if (!strcmp ("parallel", p))
31484 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31485 else if (!strcmp ("present", p))
31486 result = PRAGMA_OACC_CLAUSE_PRESENT;
31487 else if (!strcmp ("present_or_copy", p)
31488 || !strcmp ("pcopy", p))
31489 result = PRAGMA_OACC_CLAUSE_COPY;
31490 else if (!strcmp ("present_or_copyin", p)
31491 || !strcmp ("pcopyin", p))
31492 result = PRAGMA_OACC_CLAUSE_COPYIN;
31493 else if (!strcmp ("present_or_copyout", p)
31494 || !strcmp ("pcopyout", p))
31495 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31496 else if (!strcmp ("present_or_create", p)
31497 || !strcmp ("pcreate", p))
31498 result = PRAGMA_OACC_CLAUSE_CREATE;
31499 else if (!strcmp ("priority", p))
31500 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31501 else if (!strcmp ("proc_bind", p))
31502 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31503 break;
31504 case 'r':
31505 if (!strcmp ("reduction", p))
31506 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31507 break;
31508 case 's':
31509 if (!strcmp ("safelen", p))
31510 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31511 else if (!strcmp ("schedule", p))
31512 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31513 else if (!strcmp ("sections", p))
31514 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31515 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31516 result = PRAGMA_OACC_CLAUSE_HOST;
31517 else if (!strcmp ("seq", p))
31518 result = PRAGMA_OACC_CLAUSE_SEQ;
31519 else if (!strcmp ("shared", p))
31520 result = PRAGMA_OMP_CLAUSE_SHARED;
31521 else if (!strcmp ("simd", p))
31522 result = PRAGMA_OMP_CLAUSE_SIMD;
31523 else if (!strcmp ("simdlen", p))
31524 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31525 break;
31526 case 't':
31527 if (!strcmp ("taskgroup", p))
31528 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31529 else if (!strcmp ("thread_limit", p))
31530 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31531 else if (!strcmp ("threads", p))
31532 result = PRAGMA_OMP_CLAUSE_THREADS;
31533 else if (!strcmp ("tile", p))
31534 result = PRAGMA_OACC_CLAUSE_TILE;
31535 else if (!strcmp ("to", p))
31536 result = PRAGMA_OMP_CLAUSE_TO;
31537 break;
31538 case 'u':
31539 if (!strcmp ("uniform", p))
31540 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31541 else if (!strcmp ("untied", p))
31542 result = PRAGMA_OMP_CLAUSE_UNTIED;
31543 else if (!strcmp ("use_device", p))
31544 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31545 else if (!strcmp ("use_device_ptr", p))
31546 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31547 break;
31548 case 'v':
31549 if (!strcmp ("vector", p))
31550 result = PRAGMA_OACC_CLAUSE_VECTOR;
31551 else if (!strcmp ("vector_length", p))
31552 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31553 break;
31554 case 'w':
31555 if (!strcmp ("wait", p))
31556 result = PRAGMA_OACC_CLAUSE_WAIT;
31557 else if (!strcmp ("worker", p))
31558 result = PRAGMA_OACC_CLAUSE_WORKER;
31559 break;
31563 if (result != PRAGMA_OMP_CLAUSE_NONE)
31564 cp_lexer_consume_token (parser->lexer);
31566 return result;
31569 /* Validate that a clause of the given type does not already exist. */
31571 static void
31572 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31573 const char *name, location_t location)
31575 tree c;
31577 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31578 if (OMP_CLAUSE_CODE (c) == code)
31580 error_at (location, "too many %qs clauses", name);
31581 break;
31585 /* OpenMP 2.5:
31586 variable-list:
31587 identifier
31588 variable-list , identifier
31590 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31591 colon). An opening parenthesis will have been consumed by the caller.
31593 If KIND is nonzero, create the appropriate node and install the decl
31594 in OMP_CLAUSE_DECL and add the node to the head of the list.
31596 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31597 return the list created.
31599 COLON can be NULL if only closing parenthesis should end the list,
31600 or pointer to bool which will receive false if the list is terminated
31601 by closing parenthesis or true if the list is terminated by colon. */
31603 static tree
31604 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31605 tree list, bool *colon)
31607 cp_token *token;
31608 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31609 if (colon)
31611 parser->colon_corrects_to_scope_p = false;
31612 *colon = false;
31614 while (1)
31616 tree name, decl;
31618 token = cp_lexer_peek_token (parser->lexer);
31619 if (kind != 0
31620 && current_class_ptr
31621 && cp_parser_is_keyword (token, RID_THIS))
31623 decl = finish_this_expr ();
31624 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31625 || CONVERT_EXPR_P (decl))
31626 decl = TREE_OPERAND (decl, 0);
31627 cp_lexer_consume_token (parser->lexer);
31629 else
31631 name = cp_parser_id_expression (parser, /*template_p=*/false,
31632 /*check_dependency_p=*/true,
31633 /*template_p=*/NULL,
31634 /*declarator_p=*/false,
31635 /*optional_p=*/false);
31636 if (name == error_mark_node)
31637 goto skip_comma;
31639 if (identifier_p (name))
31640 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31641 else
31642 decl = name;
31643 if (decl == error_mark_node)
31644 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31645 token->location);
31647 if (decl == error_mark_node)
31649 else if (kind != 0)
31651 switch (kind)
31653 case OMP_CLAUSE__CACHE_:
31654 /* The OpenACC cache directive explicitly only allows "array
31655 elements or subarrays". */
31656 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31658 error_at (token->location, "expected %<[%>");
31659 decl = error_mark_node;
31660 break;
31662 /* FALLTHROUGH. */
31663 case OMP_CLAUSE_MAP:
31664 case OMP_CLAUSE_FROM:
31665 case OMP_CLAUSE_TO:
31666 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31668 location_t loc
31669 = cp_lexer_peek_token (parser->lexer)->location;
31670 cp_id_kind idk = CP_ID_KIND_NONE;
31671 cp_lexer_consume_token (parser->lexer);
31672 decl = convert_from_reference (decl);
31673 decl
31674 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31675 decl, false,
31676 &idk, loc);
31678 /* FALLTHROUGH. */
31679 case OMP_CLAUSE_DEPEND:
31680 case OMP_CLAUSE_REDUCTION:
31681 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31683 tree low_bound = NULL_TREE, length = NULL_TREE;
31685 parser->colon_corrects_to_scope_p = false;
31686 cp_lexer_consume_token (parser->lexer);
31687 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31688 low_bound = cp_parser_expression (parser);
31689 if (!colon)
31690 parser->colon_corrects_to_scope_p
31691 = saved_colon_corrects_to_scope_p;
31692 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31693 length = integer_one_node;
31694 else
31696 /* Look for `:'. */
31697 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31698 goto skip_comma;
31699 if (!cp_lexer_next_token_is (parser->lexer,
31700 CPP_CLOSE_SQUARE))
31701 length = cp_parser_expression (parser);
31703 /* Look for the closing `]'. */
31704 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31705 RT_CLOSE_SQUARE))
31706 goto skip_comma;
31708 decl = tree_cons (low_bound, length, decl);
31710 break;
31711 default:
31712 break;
31715 tree u = build_omp_clause (token->location, kind);
31716 OMP_CLAUSE_DECL (u) = decl;
31717 OMP_CLAUSE_CHAIN (u) = list;
31718 list = u;
31720 else
31721 list = tree_cons (decl, NULL_TREE, list);
31723 get_comma:
31724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31725 break;
31726 cp_lexer_consume_token (parser->lexer);
31729 if (colon)
31730 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31732 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31734 *colon = true;
31735 cp_parser_require (parser, CPP_COLON, RT_COLON);
31736 return list;
31739 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31741 int ending;
31743 /* Try to resync to an unnested comma. Copied from
31744 cp_parser_parenthesized_expression_list. */
31745 skip_comma:
31746 if (colon)
31747 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31748 ending = cp_parser_skip_to_closing_parenthesis (parser,
31749 /*recovering=*/true,
31750 /*or_comma=*/true,
31751 /*consume_paren=*/true);
31752 if (ending < 0)
31753 goto get_comma;
31756 return list;
31759 /* Similarly, but expect leading and trailing parenthesis. This is a very
31760 common case for omp clauses. */
31762 static tree
31763 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31765 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31766 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31767 return list;
31770 /* OpenACC 2.0:
31771 copy ( variable-list )
31772 copyin ( variable-list )
31773 copyout ( variable-list )
31774 create ( variable-list )
31775 delete ( variable-list )
31776 present ( variable-list ) */
31778 static tree
31779 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31780 tree list)
31782 enum gomp_map_kind kind;
31783 switch (c_kind)
31785 case PRAGMA_OACC_CLAUSE_COPY:
31786 kind = GOMP_MAP_TOFROM;
31787 break;
31788 case PRAGMA_OACC_CLAUSE_COPYIN:
31789 kind = GOMP_MAP_TO;
31790 break;
31791 case PRAGMA_OACC_CLAUSE_COPYOUT:
31792 kind = GOMP_MAP_FROM;
31793 break;
31794 case PRAGMA_OACC_CLAUSE_CREATE:
31795 kind = GOMP_MAP_ALLOC;
31796 break;
31797 case PRAGMA_OACC_CLAUSE_DELETE:
31798 kind = GOMP_MAP_RELEASE;
31799 break;
31800 case PRAGMA_OACC_CLAUSE_DEVICE:
31801 kind = GOMP_MAP_FORCE_TO;
31802 break;
31803 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31804 kind = GOMP_MAP_DEVICE_RESIDENT;
31805 break;
31806 case PRAGMA_OACC_CLAUSE_HOST:
31807 kind = GOMP_MAP_FORCE_FROM;
31808 break;
31809 case PRAGMA_OACC_CLAUSE_LINK:
31810 kind = GOMP_MAP_LINK;
31811 break;
31812 case PRAGMA_OACC_CLAUSE_PRESENT:
31813 kind = GOMP_MAP_FORCE_PRESENT;
31814 break;
31815 default:
31816 gcc_unreachable ();
31818 tree nl, c;
31819 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31821 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31822 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31824 return nl;
31827 /* OpenACC 2.0:
31828 deviceptr ( variable-list ) */
31830 static tree
31831 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31833 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31834 tree vars, t;
31836 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31837 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31838 variable-list must only allow for pointer variables. */
31839 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31840 for (t = vars; t; t = TREE_CHAIN (t))
31842 tree v = TREE_PURPOSE (t);
31843 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31844 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31845 OMP_CLAUSE_DECL (u) = v;
31846 OMP_CLAUSE_CHAIN (u) = list;
31847 list = u;
31850 return list;
31853 /* OpenACC 2.5:
31854 auto
31855 finalize
31856 independent
31857 nohost
31858 seq */
31860 static tree
31861 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31862 enum omp_clause_code code,
31863 tree list, location_t location)
31865 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31866 tree c = build_omp_clause (location, code);
31867 OMP_CLAUSE_CHAIN (c) = list;
31868 return c;
31871 /* OpenACC:
31872 num_gangs ( expression )
31873 num_workers ( expression )
31874 vector_length ( expression ) */
31876 static tree
31877 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31878 const char *str, tree list)
31880 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31882 matching_parens parens;
31883 if (!parens.require_open (parser))
31884 return list;
31886 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31888 if (t == error_mark_node
31889 || !parens.require_close (parser))
31891 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31892 /*or_comma=*/false,
31893 /*consume_paren=*/true);
31894 return list;
31897 check_no_duplicate_clause (list, code, str, loc);
31899 tree c = build_omp_clause (loc, code);
31900 OMP_CLAUSE_OPERAND (c, 0) = t;
31901 OMP_CLAUSE_CHAIN (c) = list;
31902 return c;
31905 /* OpenACC:
31907 gang [( gang-arg-list )]
31908 worker [( [num:] int-expr )]
31909 vector [( [length:] int-expr )]
31911 where gang-arg is one of:
31913 [num:] int-expr
31914 static: size-expr
31916 and size-expr may be:
31919 int-expr
31922 static tree
31923 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31924 const char *str, tree list)
31926 const char *id = "num";
31927 cp_lexer *lexer = parser->lexer;
31928 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31929 location_t loc = cp_lexer_peek_token (lexer)->location;
31931 if (kind == OMP_CLAUSE_VECTOR)
31932 id = "length";
31934 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31936 matching_parens parens;
31937 parens.consume_open (parser);
31941 cp_token *next = cp_lexer_peek_token (lexer);
31942 int idx = 0;
31944 /* Gang static argument. */
31945 if (kind == OMP_CLAUSE_GANG
31946 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31948 cp_lexer_consume_token (lexer);
31950 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31951 goto cleanup_error;
31953 idx = 1;
31954 if (ops[idx] != NULL)
31956 cp_parser_error (parser, "too many %<static%> arguments");
31957 goto cleanup_error;
31960 /* Check for the '*' argument. */
31961 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31962 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31963 || cp_lexer_nth_token_is (parser->lexer, 2,
31964 CPP_CLOSE_PAREN)))
31966 cp_lexer_consume_token (lexer);
31967 ops[idx] = integer_minus_one_node;
31969 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31971 cp_lexer_consume_token (lexer);
31972 continue;
31974 else break;
31977 /* Worker num: argument and vector length: arguments. */
31978 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31979 && id_equal (next->u.value, id)
31980 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31982 cp_lexer_consume_token (lexer); /* id */
31983 cp_lexer_consume_token (lexer); /* ':' */
31986 /* Now collect the actual argument. */
31987 if (ops[idx] != NULL_TREE)
31989 cp_parser_error (parser, "unexpected argument");
31990 goto cleanup_error;
31993 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31994 false);
31995 if (expr == error_mark_node)
31996 goto cleanup_error;
31998 mark_exp_read (expr);
31999 ops[idx] = expr;
32001 if (kind == OMP_CLAUSE_GANG
32002 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32004 cp_lexer_consume_token (lexer);
32005 continue;
32007 break;
32009 while (1);
32011 if (!parens.require_close (parser))
32012 goto cleanup_error;
32015 check_no_duplicate_clause (list, kind, str, loc);
32017 c = build_omp_clause (loc, kind);
32019 if (ops[1])
32020 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32022 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32023 OMP_CLAUSE_CHAIN (c) = list;
32025 return c;
32027 cleanup_error:
32028 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32029 return list;
32032 /* OpenACC 2.0:
32033 tile ( size-expr-list ) */
32035 static tree
32036 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32038 tree c, expr = error_mark_node;
32039 tree tile = NULL_TREE;
32041 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32042 so, but the spec authors never considered such a case and have
32043 differing opinions on what it might mean, including 'not
32044 allowed'.) */
32045 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32046 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32047 clause_loc);
32049 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32050 return list;
32054 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32055 return list;
32057 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32058 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32059 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32061 cp_lexer_consume_token (parser->lexer);
32062 expr = integer_zero_node;
32064 else
32065 expr = cp_parser_constant_expression (parser);
32067 tile = tree_cons (NULL_TREE, expr, tile);
32069 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32071 /* Consume the trailing ')'. */
32072 cp_lexer_consume_token (parser->lexer);
32074 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32075 tile = nreverse (tile);
32076 OMP_CLAUSE_TILE_LIST (c) = tile;
32077 OMP_CLAUSE_CHAIN (c) = list;
32078 return c;
32081 /* OpenACC 2.0
32082 Parse wait clause or directive parameters. */
32084 static tree
32085 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32087 vec<tree, va_gc> *args;
32088 tree t, args_tree;
32090 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32091 /*cast_p=*/false,
32092 /*allow_expansion_p=*/true,
32093 /*non_constant_p=*/NULL);
32095 if (args == NULL || args->length () == 0)
32097 cp_parser_error (parser, "expected integer expression before ')'");
32098 if (args != NULL)
32099 release_tree_vector (args);
32100 return list;
32103 args_tree = build_tree_list_vec (args);
32105 release_tree_vector (args);
32107 for (t = args_tree; t; t = TREE_CHAIN (t))
32109 tree targ = TREE_VALUE (t);
32111 if (targ != error_mark_node)
32113 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32114 error ("%<wait%> expression must be integral");
32115 else
32117 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32119 targ = mark_rvalue_use (targ);
32120 OMP_CLAUSE_DECL (c) = targ;
32121 OMP_CLAUSE_CHAIN (c) = list;
32122 list = c;
32127 return list;
32130 /* OpenACC:
32131 wait ( int-expr-list ) */
32133 static tree
32134 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32136 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32138 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32139 return list;
32141 list = cp_parser_oacc_wait_list (parser, location, list);
32143 return list;
32146 /* OpenMP 3.0:
32147 collapse ( constant-expression ) */
32149 static tree
32150 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32152 tree c, num;
32153 location_t loc;
32154 HOST_WIDE_INT n;
32156 loc = cp_lexer_peek_token (parser->lexer)->location;
32157 matching_parens parens;
32158 if (!parens.require_open (parser))
32159 return list;
32161 num = cp_parser_constant_expression (parser);
32163 if (!parens.require_close (parser))
32164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32165 /*or_comma=*/false,
32166 /*consume_paren=*/true);
32168 if (num == error_mark_node)
32169 return list;
32170 num = fold_non_dependent_expr (num);
32171 if (!tree_fits_shwi_p (num)
32172 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32173 || (n = tree_to_shwi (num)) <= 0
32174 || (int) n != n)
32176 error_at (loc, "collapse argument needs positive constant integer expression");
32177 return list;
32180 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32181 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32182 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32183 OMP_CLAUSE_CHAIN (c) = list;
32184 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32186 return c;
32189 /* OpenMP 2.5:
32190 default ( none | shared )
32192 OpenACC:
32193 default ( none | present ) */
32195 static tree
32196 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32197 location_t location, bool is_oacc)
32199 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32200 tree c;
32202 matching_parens parens;
32203 if (!parens.require_open (parser))
32204 return list;
32205 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32207 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32208 const char *p = IDENTIFIER_POINTER (id);
32210 switch (p[0])
32212 case 'n':
32213 if (strcmp ("none", p) != 0)
32214 goto invalid_kind;
32215 kind = OMP_CLAUSE_DEFAULT_NONE;
32216 break;
32218 case 'p':
32219 if (strcmp ("present", p) != 0 || !is_oacc)
32220 goto invalid_kind;
32221 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32222 break;
32224 case 's':
32225 if (strcmp ("shared", p) != 0 || is_oacc)
32226 goto invalid_kind;
32227 kind = OMP_CLAUSE_DEFAULT_SHARED;
32228 break;
32230 default:
32231 goto invalid_kind;
32234 cp_lexer_consume_token (parser->lexer);
32236 else
32238 invalid_kind:
32239 if (is_oacc)
32240 cp_parser_error (parser, "expected %<none%> or %<present%>");
32241 else
32242 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32245 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32246 || !parens.require_close (parser))
32247 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32248 /*or_comma=*/false,
32249 /*consume_paren=*/true);
32251 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32252 return list;
32254 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32255 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32256 OMP_CLAUSE_CHAIN (c) = list;
32257 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32259 return c;
32262 /* OpenMP 3.1:
32263 final ( expression ) */
32265 static tree
32266 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32268 tree t, c;
32270 matching_parens parens;
32271 if (!parens.require_open (parser))
32272 return list;
32274 t = cp_parser_condition (parser);
32276 if (t == error_mark_node
32277 || !parens.require_close (parser))
32278 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32279 /*or_comma=*/false,
32280 /*consume_paren=*/true);
32282 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32284 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32285 OMP_CLAUSE_FINAL_EXPR (c) = t;
32286 OMP_CLAUSE_CHAIN (c) = list;
32288 return c;
32291 /* OpenMP 2.5:
32292 if ( expression )
32294 OpenMP 4.5:
32295 if ( directive-name-modifier : expression )
32297 directive-name-modifier:
32298 parallel | task | taskloop | target data | target | target update
32299 | target enter data | target exit data */
32301 static tree
32302 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32303 bool is_omp)
32305 tree t, c;
32306 enum tree_code if_modifier = ERROR_MARK;
32308 matching_parens parens;
32309 if (!parens.require_open (parser))
32310 return list;
32312 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32314 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32315 const char *p = IDENTIFIER_POINTER (id);
32316 int n = 2;
32318 if (strcmp ("parallel", p) == 0)
32319 if_modifier = OMP_PARALLEL;
32320 else if (strcmp ("task", p) == 0)
32321 if_modifier = OMP_TASK;
32322 else if (strcmp ("taskloop", p) == 0)
32323 if_modifier = OMP_TASKLOOP;
32324 else if (strcmp ("target", p) == 0)
32326 if_modifier = OMP_TARGET;
32327 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32329 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32330 p = IDENTIFIER_POINTER (id);
32331 if (strcmp ("data", p) == 0)
32332 if_modifier = OMP_TARGET_DATA;
32333 else if (strcmp ("update", p) == 0)
32334 if_modifier = OMP_TARGET_UPDATE;
32335 else if (strcmp ("enter", p) == 0)
32336 if_modifier = OMP_TARGET_ENTER_DATA;
32337 else if (strcmp ("exit", p) == 0)
32338 if_modifier = OMP_TARGET_EXIT_DATA;
32339 if (if_modifier != OMP_TARGET)
32340 n = 3;
32341 else
32343 location_t loc
32344 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32345 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32346 "or %<exit%>");
32347 if_modifier = ERROR_MARK;
32349 if (if_modifier == OMP_TARGET_ENTER_DATA
32350 || if_modifier == OMP_TARGET_EXIT_DATA)
32352 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32354 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32355 p = IDENTIFIER_POINTER (id);
32356 if (strcmp ("data", p) == 0)
32357 n = 4;
32359 if (n != 4)
32361 location_t loc
32362 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32363 error_at (loc, "expected %<data%>");
32364 if_modifier = ERROR_MARK;
32369 if (if_modifier != ERROR_MARK)
32371 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32373 while (n-- > 0)
32374 cp_lexer_consume_token (parser->lexer);
32376 else
32378 if (n > 2)
32380 location_t loc
32381 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32382 error_at (loc, "expected %<:%>");
32384 if_modifier = ERROR_MARK;
32389 t = cp_parser_condition (parser);
32391 if (t == error_mark_node
32392 || !parens.require_close (parser))
32393 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32394 /*or_comma=*/false,
32395 /*consume_paren=*/true);
32397 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32398 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32400 if (if_modifier != ERROR_MARK
32401 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32403 const char *p = NULL;
32404 switch (if_modifier)
32406 case OMP_PARALLEL: p = "parallel"; break;
32407 case OMP_TASK: p = "task"; break;
32408 case OMP_TASKLOOP: p = "taskloop"; break;
32409 case OMP_TARGET_DATA: p = "target data"; break;
32410 case OMP_TARGET: p = "target"; break;
32411 case OMP_TARGET_UPDATE: p = "target update"; break;
32412 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32413 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32414 default: gcc_unreachable ();
32416 error_at (location, "too many %<if%> clauses with %qs modifier",
32418 return list;
32420 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32422 if (!is_omp)
32423 error_at (location, "too many %<if%> clauses");
32424 else
32425 error_at (location, "too many %<if%> clauses without modifier");
32426 return list;
32428 else if (if_modifier == ERROR_MARK
32429 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32431 error_at (location, "if any %<if%> clause has modifier, then all "
32432 "%<if%> clauses have to use modifier");
32433 return list;
32437 c = build_omp_clause (location, OMP_CLAUSE_IF);
32438 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32439 OMP_CLAUSE_IF_EXPR (c) = t;
32440 OMP_CLAUSE_CHAIN (c) = list;
32442 return c;
32445 /* OpenMP 3.1:
32446 mergeable */
32448 static tree
32449 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32450 tree list, location_t location)
32452 tree c;
32454 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32455 location);
32457 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32458 OMP_CLAUSE_CHAIN (c) = list;
32459 return c;
32462 /* OpenMP 2.5:
32463 nowait */
32465 static tree
32466 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32467 tree list, location_t location)
32469 tree c;
32471 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32473 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32474 OMP_CLAUSE_CHAIN (c) = list;
32475 return c;
32478 /* OpenMP 2.5:
32479 num_threads ( expression ) */
32481 static tree
32482 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32483 location_t location)
32485 tree t, c;
32487 matching_parens parens;
32488 if (!parens.require_open (parser))
32489 return list;
32491 t = cp_parser_expression (parser);
32493 if (t == error_mark_node
32494 || !parens.require_close (parser))
32495 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32496 /*or_comma=*/false,
32497 /*consume_paren=*/true);
32499 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32500 "num_threads", location);
32502 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32503 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32504 OMP_CLAUSE_CHAIN (c) = list;
32506 return c;
32509 /* OpenMP 4.5:
32510 num_tasks ( expression ) */
32512 static tree
32513 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32514 location_t location)
32516 tree t, c;
32518 matching_parens parens;
32519 if (!parens.require_open (parser))
32520 return list;
32522 t = cp_parser_expression (parser);
32524 if (t == error_mark_node
32525 || !parens.require_close (parser))
32526 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32527 /*or_comma=*/false,
32528 /*consume_paren=*/true);
32530 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32531 "num_tasks", location);
32533 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32534 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32535 OMP_CLAUSE_CHAIN (c) = list;
32537 return c;
32540 /* OpenMP 4.5:
32541 grainsize ( expression ) */
32543 static tree
32544 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32545 location_t location)
32547 tree t, c;
32549 matching_parens parens;
32550 if (!parens.require_open (parser))
32551 return list;
32553 t = cp_parser_expression (parser);
32555 if (t == error_mark_node
32556 || !parens.require_close (parser))
32557 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32558 /*or_comma=*/false,
32559 /*consume_paren=*/true);
32561 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32562 "grainsize", location);
32564 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32565 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32566 OMP_CLAUSE_CHAIN (c) = list;
32568 return c;
32571 /* OpenMP 4.5:
32572 priority ( expression ) */
32574 static tree
32575 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32576 location_t location)
32578 tree t, c;
32580 matching_parens parens;
32581 if (!parens.require_open (parser))
32582 return list;
32584 t = cp_parser_expression (parser);
32586 if (t == error_mark_node
32587 || !parens.require_close (parser))
32588 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32589 /*or_comma=*/false,
32590 /*consume_paren=*/true);
32592 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32593 "priority", location);
32595 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32596 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32597 OMP_CLAUSE_CHAIN (c) = list;
32599 return c;
32602 /* OpenMP 4.5:
32603 hint ( expression ) */
32605 static tree
32606 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32607 location_t location)
32609 tree t, c;
32611 matching_parens parens;
32612 if (!parens.require_open (parser))
32613 return list;
32615 t = cp_parser_expression (parser);
32617 if (t == error_mark_node
32618 || !parens.require_close (parser))
32619 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32620 /*or_comma=*/false,
32621 /*consume_paren=*/true);
32623 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32625 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32626 OMP_CLAUSE_HINT_EXPR (c) = t;
32627 OMP_CLAUSE_CHAIN (c) = list;
32629 return c;
32632 /* OpenMP 4.5:
32633 defaultmap ( tofrom : scalar ) */
32635 static tree
32636 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32637 location_t location)
32639 tree c, id;
32640 const char *p;
32642 matching_parens parens;
32643 if (!parens.require_open (parser))
32644 return list;
32646 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32648 cp_parser_error (parser, "expected %<tofrom%>");
32649 goto out_err;
32651 id = cp_lexer_peek_token (parser->lexer)->u.value;
32652 p = IDENTIFIER_POINTER (id);
32653 if (strcmp (p, "tofrom") != 0)
32655 cp_parser_error (parser, "expected %<tofrom%>");
32656 goto out_err;
32658 cp_lexer_consume_token (parser->lexer);
32659 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32660 goto out_err;
32662 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32664 cp_parser_error (parser, "expected %<scalar%>");
32665 goto out_err;
32667 id = cp_lexer_peek_token (parser->lexer)->u.value;
32668 p = IDENTIFIER_POINTER (id);
32669 if (strcmp (p, "scalar") != 0)
32671 cp_parser_error (parser, "expected %<scalar%>");
32672 goto out_err;
32674 cp_lexer_consume_token (parser->lexer);
32675 if (!parens.require_close (parser))
32676 goto out_err;
32678 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32679 location);
32681 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32682 OMP_CLAUSE_CHAIN (c) = list;
32683 return c;
32685 out_err:
32686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32687 /*or_comma=*/false,
32688 /*consume_paren=*/true);
32689 return list;
32692 /* OpenMP 2.5:
32693 ordered
32695 OpenMP 4.5:
32696 ordered ( constant-expression ) */
32698 static tree
32699 cp_parser_omp_clause_ordered (cp_parser *parser,
32700 tree list, location_t location)
32702 tree c, num = NULL_TREE;
32703 HOST_WIDE_INT n;
32705 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32706 "ordered", location);
32708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32710 matching_parens parens;
32711 parens.consume_open (parser);
32713 num = cp_parser_constant_expression (parser);
32715 if (!parens.require_close (parser))
32716 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32717 /*or_comma=*/false,
32718 /*consume_paren=*/true);
32720 if (num == error_mark_node)
32721 return list;
32722 num = fold_non_dependent_expr (num);
32723 if (!tree_fits_shwi_p (num)
32724 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32725 || (n = tree_to_shwi (num)) <= 0
32726 || (int) n != n)
32728 error_at (location,
32729 "ordered argument needs positive constant integer "
32730 "expression");
32731 return list;
32735 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32736 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32737 OMP_CLAUSE_CHAIN (c) = list;
32738 return c;
32741 /* OpenMP 2.5:
32742 reduction ( reduction-operator : variable-list )
32744 reduction-operator:
32745 One of: + * - & ^ | && ||
32747 OpenMP 3.1:
32749 reduction-operator:
32750 One of: + * - & ^ | && || min max
32752 OpenMP 4.0:
32754 reduction-operator:
32755 One of: + * - & ^ | && ||
32756 id-expression */
32758 static tree
32759 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32761 enum tree_code code = ERROR_MARK;
32762 tree nlist, c, id = NULL_TREE;
32764 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32765 return list;
32767 switch (cp_lexer_peek_token (parser->lexer)->type)
32769 case CPP_PLUS: code = PLUS_EXPR; break;
32770 case CPP_MULT: code = MULT_EXPR; break;
32771 case CPP_MINUS: code = MINUS_EXPR; break;
32772 case CPP_AND: code = BIT_AND_EXPR; break;
32773 case CPP_XOR: code = BIT_XOR_EXPR; break;
32774 case CPP_OR: code = BIT_IOR_EXPR; break;
32775 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32776 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32777 default: break;
32780 if (code != ERROR_MARK)
32781 cp_lexer_consume_token (parser->lexer);
32782 else
32784 bool saved_colon_corrects_to_scope_p;
32785 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32786 parser->colon_corrects_to_scope_p = false;
32787 id = cp_parser_id_expression (parser, /*template_p=*/false,
32788 /*check_dependency_p=*/true,
32789 /*template_p=*/NULL,
32790 /*declarator_p=*/false,
32791 /*optional_p=*/false);
32792 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32793 if (identifier_p (id))
32795 const char *p = IDENTIFIER_POINTER (id);
32797 if (strcmp (p, "min") == 0)
32798 code = MIN_EXPR;
32799 else if (strcmp (p, "max") == 0)
32800 code = MAX_EXPR;
32801 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32802 code = PLUS_EXPR;
32803 else if (id == ovl_op_identifier (false, MULT_EXPR))
32804 code = MULT_EXPR;
32805 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32806 code = MINUS_EXPR;
32807 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32808 code = BIT_AND_EXPR;
32809 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32810 code = BIT_IOR_EXPR;
32811 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32812 code = BIT_XOR_EXPR;
32813 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32814 code = TRUTH_ANDIF_EXPR;
32815 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32816 code = TRUTH_ORIF_EXPR;
32817 id = omp_reduction_id (code, id, NULL_TREE);
32818 tree scope = parser->scope;
32819 if (scope)
32820 id = build_qualified_name (NULL_TREE, scope, id, false);
32821 parser->scope = NULL_TREE;
32822 parser->qualifying_scope = NULL_TREE;
32823 parser->object_scope = NULL_TREE;
32825 else
32827 error ("invalid reduction-identifier");
32828 resync_fail:
32829 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32830 /*or_comma=*/false,
32831 /*consume_paren=*/true);
32832 return list;
32836 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32837 goto resync_fail;
32839 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32840 NULL);
32841 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32843 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32844 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32847 return nlist;
32850 /* OpenMP 2.5:
32851 schedule ( schedule-kind )
32852 schedule ( schedule-kind , expression )
32854 schedule-kind:
32855 static | dynamic | guided | runtime | auto
32857 OpenMP 4.5:
32858 schedule ( schedule-modifier : schedule-kind )
32859 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32861 schedule-modifier:
32862 simd
32863 monotonic
32864 nonmonotonic */
32866 static tree
32867 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32869 tree c, t;
32870 int modifiers = 0, nmodifiers = 0;
32872 matching_parens parens;
32873 if (!parens.require_open (parser))
32874 return list;
32876 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32878 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32880 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32881 const char *p = IDENTIFIER_POINTER (id);
32882 if (strcmp ("simd", p) == 0)
32883 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32884 else if (strcmp ("monotonic", p) == 0)
32885 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32886 else if (strcmp ("nonmonotonic", p) == 0)
32887 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32888 else
32889 break;
32890 cp_lexer_consume_token (parser->lexer);
32891 if (nmodifiers++ == 0
32892 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32893 cp_lexer_consume_token (parser->lexer);
32894 else
32896 cp_parser_require (parser, CPP_COLON, RT_COLON);
32897 break;
32901 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32903 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32904 const char *p = IDENTIFIER_POINTER (id);
32906 switch (p[0])
32908 case 'd':
32909 if (strcmp ("dynamic", p) != 0)
32910 goto invalid_kind;
32911 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32912 break;
32914 case 'g':
32915 if (strcmp ("guided", p) != 0)
32916 goto invalid_kind;
32917 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32918 break;
32920 case 'r':
32921 if (strcmp ("runtime", p) != 0)
32922 goto invalid_kind;
32923 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32924 break;
32926 default:
32927 goto invalid_kind;
32930 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32931 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32932 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32933 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32934 else
32935 goto invalid_kind;
32936 cp_lexer_consume_token (parser->lexer);
32938 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32939 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32940 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32941 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32943 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32944 "specified");
32945 modifiers = 0;
32948 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32950 cp_token *token;
32951 cp_lexer_consume_token (parser->lexer);
32953 token = cp_lexer_peek_token (parser->lexer);
32954 t = cp_parser_assignment_expression (parser);
32956 if (t == error_mark_node)
32957 goto resync_fail;
32958 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32959 error_at (token->location, "schedule %<runtime%> does not take "
32960 "a %<chunk_size%> parameter");
32961 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32962 error_at (token->location, "schedule %<auto%> does not take "
32963 "a %<chunk_size%> parameter");
32964 else
32965 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32967 if (!parens.require_close (parser))
32968 goto resync_fail;
32970 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32971 goto resync_fail;
32973 OMP_CLAUSE_SCHEDULE_KIND (c)
32974 = (enum omp_clause_schedule_kind)
32975 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32977 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32978 OMP_CLAUSE_CHAIN (c) = list;
32979 return c;
32981 invalid_kind:
32982 cp_parser_error (parser, "invalid schedule kind");
32983 resync_fail:
32984 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32985 /*or_comma=*/false,
32986 /*consume_paren=*/true);
32987 return list;
32990 /* OpenMP 3.0:
32991 untied */
32993 static tree
32994 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32995 tree list, location_t location)
32997 tree c;
32999 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33001 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33002 OMP_CLAUSE_CHAIN (c) = list;
33003 return c;
33006 /* OpenMP 4.0:
33007 inbranch
33008 notinbranch */
33010 static tree
33011 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33012 tree list, location_t location)
33014 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33015 tree c = build_omp_clause (location, code);
33016 OMP_CLAUSE_CHAIN (c) = list;
33017 return c;
33020 /* OpenMP 4.0:
33021 parallel
33023 sections
33024 taskgroup */
33026 static tree
33027 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33028 enum omp_clause_code code,
33029 tree list, location_t location)
33031 tree c = build_omp_clause (location, code);
33032 OMP_CLAUSE_CHAIN (c) = list;
33033 return c;
33036 /* OpenMP 4.5:
33037 nogroup */
33039 static tree
33040 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33041 tree list, location_t location)
33043 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33044 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33045 OMP_CLAUSE_CHAIN (c) = list;
33046 return c;
33049 /* OpenMP 4.5:
33050 simd
33051 threads */
33053 static tree
33054 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33055 enum omp_clause_code code,
33056 tree list, location_t location)
33058 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33059 tree c = build_omp_clause (location, code);
33060 OMP_CLAUSE_CHAIN (c) = list;
33061 return c;
33064 /* OpenMP 4.0:
33065 num_teams ( expression ) */
33067 static tree
33068 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33069 location_t location)
33071 tree t, c;
33073 matching_parens parens;
33074 if (!parens.require_open (parser))
33075 return list;
33077 t = cp_parser_expression (parser);
33079 if (t == error_mark_node
33080 || !parens.require_close (parser))
33081 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33082 /*or_comma=*/false,
33083 /*consume_paren=*/true);
33085 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33086 "num_teams", location);
33088 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33089 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33090 OMP_CLAUSE_CHAIN (c) = list;
33092 return c;
33095 /* OpenMP 4.0:
33096 thread_limit ( expression ) */
33098 static tree
33099 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33100 location_t location)
33102 tree t, c;
33104 matching_parens parens;
33105 if (!parens.require_open (parser))
33106 return list;
33108 t = cp_parser_expression (parser);
33110 if (t == error_mark_node
33111 || !parens.require_close (parser))
33112 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33113 /*or_comma=*/false,
33114 /*consume_paren=*/true);
33116 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33117 "thread_limit", location);
33119 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33120 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33121 OMP_CLAUSE_CHAIN (c) = list;
33123 return c;
33126 /* OpenMP 4.0:
33127 aligned ( variable-list )
33128 aligned ( variable-list : constant-expression ) */
33130 static tree
33131 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33133 tree nlist, c, alignment = NULL_TREE;
33134 bool colon;
33136 matching_parens parens;
33137 if (!parens.require_open (parser))
33138 return list;
33140 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33141 &colon);
33143 if (colon)
33145 alignment = cp_parser_constant_expression (parser);
33147 if (!parens.require_close (parser))
33148 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33149 /*or_comma=*/false,
33150 /*consume_paren=*/true);
33152 if (alignment == error_mark_node)
33153 alignment = NULL_TREE;
33156 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33157 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33159 return nlist;
33162 /* OpenMP 4.0:
33163 linear ( variable-list )
33164 linear ( variable-list : expression )
33166 OpenMP 4.5:
33167 linear ( modifier ( variable-list ) )
33168 linear ( modifier ( variable-list ) : expression ) */
33170 static tree
33171 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33172 bool declare_simd)
33174 tree nlist, c, step = integer_one_node;
33175 bool colon;
33176 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33178 matching_parens parens;
33179 if (!parens.require_open (parser))
33180 return list;
33182 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33184 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33185 const char *p = IDENTIFIER_POINTER (id);
33187 if (strcmp ("ref", p) == 0)
33188 kind = OMP_CLAUSE_LINEAR_REF;
33189 else if (strcmp ("val", p) == 0)
33190 kind = OMP_CLAUSE_LINEAR_VAL;
33191 else if (strcmp ("uval", p) == 0)
33192 kind = OMP_CLAUSE_LINEAR_UVAL;
33193 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33194 cp_lexer_consume_token (parser->lexer);
33195 else
33196 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33199 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33200 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33201 &colon);
33202 else
33204 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33205 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33206 if (colon)
33207 cp_parser_require (parser, CPP_COLON, RT_COLON);
33208 else if (!parens.require_close (parser))
33209 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33210 /*or_comma=*/false,
33211 /*consume_paren=*/true);
33214 if (colon)
33216 step = NULL_TREE;
33217 if (declare_simd
33218 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33219 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33221 cp_token *token = cp_lexer_peek_token (parser->lexer);
33222 cp_parser_parse_tentatively (parser);
33223 step = cp_parser_id_expression (parser, /*template_p=*/false,
33224 /*check_dependency_p=*/true,
33225 /*template_p=*/NULL,
33226 /*declarator_p=*/false,
33227 /*optional_p=*/false);
33228 if (step != error_mark_node)
33229 step = cp_parser_lookup_name_simple (parser, step, token->location);
33230 if (step == error_mark_node)
33232 step = NULL_TREE;
33233 cp_parser_abort_tentative_parse (parser);
33235 else if (!cp_parser_parse_definitely (parser))
33236 step = NULL_TREE;
33238 if (!step)
33239 step = cp_parser_expression (parser);
33241 if (!parens.require_close (parser))
33242 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33243 /*or_comma=*/false,
33244 /*consume_paren=*/true);
33246 if (step == error_mark_node)
33247 return list;
33250 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33252 OMP_CLAUSE_LINEAR_STEP (c) = step;
33253 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33256 return nlist;
33259 /* OpenMP 4.0:
33260 safelen ( constant-expression ) */
33262 static tree
33263 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33264 location_t location)
33266 tree t, c;
33268 matching_parens parens;
33269 if (!parens.require_open (parser))
33270 return list;
33272 t = cp_parser_constant_expression (parser);
33274 if (t == error_mark_node
33275 || !parens.require_close (parser))
33276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33280 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33282 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33283 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33284 OMP_CLAUSE_CHAIN (c) = list;
33286 return c;
33289 /* OpenMP 4.0:
33290 simdlen ( constant-expression ) */
33292 static tree
33293 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33294 location_t location)
33296 tree t, c;
33298 matching_parens parens;
33299 if (!parens.require_open (parser))
33300 return list;
33302 t = cp_parser_constant_expression (parser);
33304 if (t == error_mark_node
33305 || !parens.require_close (parser))
33306 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33307 /*or_comma=*/false,
33308 /*consume_paren=*/true);
33310 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33312 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33313 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33314 OMP_CLAUSE_CHAIN (c) = list;
33316 return c;
33319 /* OpenMP 4.5:
33320 vec:
33321 identifier [+/- integer]
33322 vec , identifier [+/- integer]
33325 static tree
33326 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33327 tree list)
33329 tree vec = NULL;
33331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33333 cp_parser_error (parser, "expected identifier");
33334 return list;
33337 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33339 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33340 tree t, identifier = cp_parser_identifier (parser);
33341 tree addend = NULL;
33343 if (identifier == error_mark_node)
33344 t = error_mark_node;
33345 else
33347 t = cp_parser_lookup_name_simple
33348 (parser, identifier,
33349 cp_lexer_peek_token (parser->lexer)->location);
33350 if (t == error_mark_node)
33351 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33352 id_loc);
33355 bool neg = false;
33356 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33357 neg = true;
33358 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33360 addend = integer_zero_node;
33361 goto add_to_vector;
33363 cp_lexer_consume_token (parser->lexer);
33365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33367 cp_parser_error (parser, "expected integer");
33368 return list;
33371 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33372 if (TREE_CODE (addend) != INTEGER_CST)
33374 cp_parser_error (parser, "expected integer");
33375 return list;
33377 cp_lexer_consume_token (parser->lexer);
33379 add_to_vector:
33380 if (t != error_mark_node)
33382 vec = tree_cons (addend, t, vec);
33383 if (neg)
33384 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33388 break;
33390 cp_lexer_consume_token (parser->lexer);
33393 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33395 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33396 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33397 OMP_CLAUSE_DECL (u) = nreverse (vec);
33398 OMP_CLAUSE_CHAIN (u) = list;
33399 return u;
33401 return list;
33404 /* OpenMP 4.0:
33405 depend ( depend-kind : variable-list )
33407 depend-kind:
33408 in | out | inout
33410 OpenMP 4.5:
33411 depend ( source )
33413 depend ( sink : vec ) */
33415 static tree
33416 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33418 tree nlist, c;
33419 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33421 matching_parens parens;
33422 if (!parens.require_open (parser))
33423 return list;
33425 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33427 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33428 const char *p = IDENTIFIER_POINTER (id);
33430 if (strcmp ("in", p) == 0)
33431 kind = OMP_CLAUSE_DEPEND_IN;
33432 else if (strcmp ("inout", p) == 0)
33433 kind = OMP_CLAUSE_DEPEND_INOUT;
33434 else if (strcmp ("out", p) == 0)
33435 kind = OMP_CLAUSE_DEPEND_OUT;
33436 else if (strcmp ("source", p) == 0)
33437 kind = OMP_CLAUSE_DEPEND_SOURCE;
33438 else if (strcmp ("sink", p) == 0)
33439 kind = OMP_CLAUSE_DEPEND_SINK;
33440 else
33441 goto invalid_kind;
33443 else
33444 goto invalid_kind;
33446 cp_lexer_consume_token (parser->lexer);
33448 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33450 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33451 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33452 OMP_CLAUSE_DECL (c) = NULL_TREE;
33453 OMP_CLAUSE_CHAIN (c) = list;
33454 if (!parens.require_close (parser))
33455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33456 /*or_comma=*/false,
33457 /*consume_paren=*/true);
33458 return c;
33461 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33462 goto resync_fail;
33464 if (kind == OMP_CLAUSE_DEPEND_SINK)
33465 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33466 else
33468 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33469 list, NULL);
33471 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33472 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33474 return nlist;
33476 invalid_kind:
33477 cp_parser_error (parser, "invalid depend kind");
33478 resync_fail:
33479 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33480 /*or_comma=*/false,
33481 /*consume_paren=*/true);
33482 return list;
33485 /* OpenMP 4.0:
33486 map ( map-kind : variable-list )
33487 map ( variable-list )
33489 map-kind:
33490 alloc | to | from | tofrom
33492 OpenMP 4.5:
33493 map-kind:
33494 alloc | to | from | tofrom | release | delete
33496 map ( always [,] map-kind: variable-list ) */
33498 static tree
33499 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33501 tree nlist, c;
33502 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33503 bool always = false;
33505 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33506 return list;
33508 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33510 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33511 const char *p = IDENTIFIER_POINTER (id);
33513 if (strcmp ("always", p) == 0)
33515 int nth = 2;
33516 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33517 nth++;
33518 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33519 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33520 == RID_DELETE))
33521 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33522 == CPP_COLON))
33524 always = true;
33525 cp_lexer_consume_token (parser->lexer);
33526 if (nth == 3)
33527 cp_lexer_consume_token (parser->lexer);
33532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33533 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33535 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33536 const char *p = IDENTIFIER_POINTER (id);
33538 if (strcmp ("alloc", p) == 0)
33539 kind = GOMP_MAP_ALLOC;
33540 else if (strcmp ("to", p) == 0)
33541 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33542 else if (strcmp ("from", p) == 0)
33543 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33544 else if (strcmp ("tofrom", p) == 0)
33545 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33546 else if (strcmp ("release", p) == 0)
33547 kind = GOMP_MAP_RELEASE;
33548 else
33550 cp_parser_error (parser, "invalid map kind");
33551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33552 /*or_comma=*/false,
33553 /*consume_paren=*/true);
33554 return list;
33556 cp_lexer_consume_token (parser->lexer);
33557 cp_lexer_consume_token (parser->lexer);
33559 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33560 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33562 kind = GOMP_MAP_DELETE;
33563 cp_lexer_consume_token (parser->lexer);
33564 cp_lexer_consume_token (parser->lexer);
33567 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33568 NULL);
33570 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33571 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33573 return nlist;
33576 /* OpenMP 4.0:
33577 device ( expression ) */
33579 static tree
33580 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33581 location_t location)
33583 tree t, c;
33585 matching_parens parens;
33586 if (!parens.require_open (parser))
33587 return list;
33589 t = cp_parser_expression (parser);
33591 if (t == error_mark_node
33592 || !parens.require_close (parser))
33593 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33594 /*or_comma=*/false,
33595 /*consume_paren=*/true);
33597 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33598 "device", location);
33600 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33601 OMP_CLAUSE_DEVICE_ID (c) = t;
33602 OMP_CLAUSE_CHAIN (c) = list;
33604 return c;
33607 /* OpenMP 4.0:
33608 dist_schedule ( static )
33609 dist_schedule ( static , expression ) */
33611 static tree
33612 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33613 location_t location)
33615 tree c, t;
33617 matching_parens parens;
33618 if (!parens.require_open (parser))
33619 return list;
33621 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33623 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33624 goto invalid_kind;
33625 cp_lexer_consume_token (parser->lexer);
33627 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33629 cp_lexer_consume_token (parser->lexer);
33631 t = cp_parser_assignment_expression (parser);
33633 if (t == error_mark_node)
33634 goto resync_fail;
33635 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33637 if (!parens.require_close (parser))
33638 goto resync_fail;
33640 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33641 goto resync_fail;
33643 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33644 location);
33645 OMP_CLAUSE_CHAIN (c) = list;
33646 return c;
33648 invalid_kind:
33649 cp_parser_error (parser, "invalid dist_schedule kind");
33650 resync_fail:
33651 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33652 /*or_comma=*/false,
33653 /*consume_paren=*/true);
33654 return list;
33657 /* OpenMP 4.0:
33658 proc_bind ( proc-bind-kind )
33660 proc-bind-kind:
33661 master | close | spread */
33663 static tree
33664 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33665 location_t location)
33667 tree c;
33668 enum omp_clause_proc_bind_kind kind;
33670 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33671 return list;
33673 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33675 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33676 const char *p = IDENTIFIER_POINTER (id);
33678 if (strcmp ("master", p) == 0)
33679 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33680 else if (strcmp ("close", p) == 0)
33681 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33682 else if (strcmp ("spread", p) == 0)
33683 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33684 else
33685 goto invalid_kind;
33687 else
33688 goto invalid_kind;
33690 cp_lexer_consume_token (parser->lexer);
33691 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33692 goto resync_fail;
33694 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33695 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33696 location);
33697 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33698 OMP_CLAUSE_CHAIN (c) = list;
33699 return c;
33701 invalid_kind:
33702 cp_parser_error (parser, "invalid depend kind");
33703 resync_fail:
33704 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33705 /*or_comma=*/false,
33706 /*consume_paren=*/true);
33707 return list;
33710 /* OpenACC:
33711 async [( int-expr )] */
33713 static tree
33714 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33716 tree c, t;
33717 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33719 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33721 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33723 matching_parens parens;
33724 parens.consume_open (parser);
33726 t = cp_parser_expression (parser);
33727 if (t == error_mark_node
33728 || !parens.require_close (parser))
33729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33730 /*or_comma=*/false,
33731 /*consume_paren=*/true);
33734 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33736 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33737 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33738 OMP_CLAUSE_CHAIN (c) = list;
33739 list = c;
33741 return list;
33744 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33745 is a bitmask in MASK. Return the list of clauses found. */
33747 static tree
33748 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33749 const char *where, cp_token *pragma_tok,
33750 bool finish_p = true)
33752 tree clauses = NULL;
33753 bool first = true;
33755 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33757 location_t here;
33758 pragma_omp_clause c_kind;
33759 omp_clause_code code;
33760 const char *c_name;
33761 tree prev = clauses;
33763 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33764 cp_lexer_consume_token (parser->lexer);
33766 here = cp_lexer_peek_token (parser->lexer)->location;
33767 c_kind = cp_parser_omp_clause_name (parser);
33769 switch (c_kind)
33771 case PRAGMA_OACC_CLAUSE_ASYNC:
33772 clauses = cp_parser_oacc_clause_async (parser, clauses);
33773 c_name = "async";
33774 break;
33775 case PRAGMA_OACC_CLAUSE_AUTO:
33776 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33777 clauses, here);
33778 c_name = "auto";
33779 break;
33780 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33781 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33782 c_name = "collapse";
33783 break;
33784 case PRAGMA_OACC_CLAUSE_COPY:
33785 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33786 c_name = "copy";
33787 break;
33788 case PRAGMA_OACC_CLAUSE_COPYIN:
33789 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33790 c_name = "copyin";
33791 break;
33792 case PRAGMA_OACC_CLAUSE_COPYOUT:
33793 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33794 c_name = "copyout";
33795 break;
33796 case PRAGMA_OACC_CLAUSE_CREATE:
33797 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33798 c_name = "create";
33799 break;
33800 case PRAGMA_OACC_CLAUSE_DELETE:
33801 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33802 c_name = "delete";
33803 break;
33804 case PRAGMA_OMP_CLAUSE_DEFAULT:
33805 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33806 c_name = "default";
33807 break;
33808 case PRAGMA_OACC_CLAUSE_DEVICE:
33809 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33810 c_name = "device";
33811 break;
33812 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33813 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33814 c_name = "deviceptr";
33815 break;
33816 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33817 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33818 c_name = "device_resident";
33819 break;
33820 case PRAGMA_OACC_CLAUSE_FINALIZE:
33821 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
33822 clauses, here);
33823 c_name = "finalize";
33824 break;
33825 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33826 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33827 clauses);
33828 c_name = "firstprivate";
33829 break;
33830 case PRAGMA_OACC_CLAUSE_GANG:
33831 c_name = "gang";
33832 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33833 c_name, clauses);
33834 break;
33835 case PRAGMA_OACC_CLAUSE_HOST:
33836 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33837 c_name = "host";
33838 break;
33839 case PRAGMA_OACC_CLAUSE_IF:
33840 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33841 c_name = "if";
33842 break;
33843 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
33844 clauses = cp_parser_oacc_simple_clause (parser,
33845 OMP_CLAUSE_IF_PRESENT,
33846 clauses, here);
33847 c_name = "if_present";
33848 break;
33849 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33850 clauses = cp_parser_oacc_simple_clause (parser,
33851 OMP_CLAUSE_INDEPENDENT,
33852 clauses, here);
33853 c_name = "independent";
33854 break;
33855 case PRAGMA_OACC_CLAUSE_LINK:
33856 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33857 c_name = "link";
33858 break;
33859 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33860 code = OMP_CLAUSE_NUM_GANGS;
33861 c_name = "num_gangs";
33862 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33863 clauses);
33864 break;
33865 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33866 c_name = "num_workers";
33867 code = OMP_CLAUSE_NUM_WORKERS;
33868 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33869 clauses);
33870 break;
33871 case PRAGMA_OACC_CLAUSE_PRESENT:
33872 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33873 c_name = "present";
33874 break;
33875 case PRAGMA_OACC_CLAUSE_PRIVATE:
33876 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33877 clauses);
33878 c_name = "private";
33879 break;
33880 case PRAGMA_OACC_CLAUSE_REDUCTION:
33881 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33882 c_name = "reduction";
33883 break;
33884 case PRAGMA_OACC_CLAUSE_SEQ:
33885 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33886 clauses, here);
33887 c_name = "seq";
33888 break;
33889 case PRAGMA_OACC_CLAUSE_TILE:
33890 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33891 c_name = "tile";
33892 break;
33893 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33894 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33895 clauses);
33896 c_name = "use_device";
33897 break;
33898 case PRAGMA_OACC_CLAUSE_VECTOR:
33899 c_name = "vector";
33900 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33901 c_name, clauses);
33902 break;
33903 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33904 c_name = "vector_length";
33905 code = OMP_CLAUSE_VECTOR_LENGTH;
33906 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33907 clauses);
33908 break;
33909 case PRAGMA_OACC_CLAUSE_WAIT:
33910 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33911 c_name = "wait";
33912 break;
33913 case PRAGMA_OACC_CLAUSE_WORKER:
33914 c_name = "worker";
33915 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33916 c_name, clauses);
33917 break;
33918 default:
33919 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33920 goto saw_error;
33923 first = false;
33925 if (((mask >> c_kind) & 1) == 0)
33927 /* Remove the invalid clause(s) from the list to avoid
33928 confusing the rest of the compiler. */
33929 clauses = prev;
33930 error_at (here, "%qs is not valid for %qs", c_name, where);
33934 saw_error:
33935 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33937 if (finish_p)
33938 return finish_omp_clauses (clauses, C_ORT_ACC);
33940 return clauses;
33943 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33944 is a bitmask in MASK. Return the list of clauses found; the result
33945 of clause default goes in *pdefault. */
33947 static tree
33948 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33949 const char *where, cp_token *pragma_tok,
33950 bool finish_p = true)
33952 tree clauses = NULL;
33953 bool first = true;
33954 cp_token *token = NULL;
33956 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33958 pragma_omp_clause c_kind;
33959 const char *c_name;
33960 tree prev = clauses;
33962 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33963 cp_lexer_consume_token (parser->lexer);
33965 token = cp_lexer_peek_token (parser->lexer);
33966 c_kind = cp_parser_omp_clause_name (parser);
33968 switch (c_kind)
33970 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33971 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33972 token->location);
33973 c_name = "collapse";
33974 break;
33975 case PRAGMA_OMP_CLAUSE_COPYIN:
33976 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33977 c_name = "copyin";
33978 break;
33979 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33980 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33981 clauses);
33982 c_name = "copyprivate";
33983 break;
33984 case PRAGMA_OMP_CLAUSE_DEFAULT:
33985 clauses = cp_parser_omp_clause_default (parser, clauses,
33986 token->location, false);
33987 c_name = "default";
33988 break;
33989 case PRAGMA_OMP_CLAUSE_FINAL:
33990 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33991 c_name = "final";
33992 break;
33993 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33994 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33995 clauses);
33996 c_name = "firstprivate";
33997 break;
33998 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33999 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34000 token->location);
34001 c_name = "grainsize";
34002 break;
34003 case PRAGMA_OMP_CLAUSE_HINT:
34004 clauses = cp_parser_omp_clause_hint (parser, clauses,
34005 token->location);
34006 c_name = "hint";
34007 break;
34008 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34009 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34010 token->location);
34011 c_name = "defaultmap";
34012 break;
34013 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34014 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34015 clauses);
34016 c_name = "use_device_ptr";
34017 break;
34018 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34019 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34020 clauses);
34021 c_name = "is_device_ptr";
34022 break;
34023 case PRAGMA_OMP_CLAUSE_IF:
34024 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34025 true);
34026 c_name = "if";
34027 break;
34028 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34029 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34030 clauses);
34031 c_name = "lastprivate";
34032 break;
34033 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34034 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34035 token->location);
34036 c_name = "mergeable";
34037 break;
34038 case PRAGMA_OMP_CLAUSE_NOWAIT:
34039 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34040 c_name = "nowait";
34041 break;
34042 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34043 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34044 token->location);
34045 c_name = "num_tasks";
34046 break;
34047 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34048 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34049 token->location);
34050 c_name = "num_threads";
34051 break;
34052 case PRAGMA_OMP_CLAUSE_ORDERED:
34053 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34054 token->location);
34055 c_name = "ordered";
34056 break;
34057 case PRAGMA_OMP_CLAUSE_PRIORITY:
34058 clauses = cp_parser_omp_clause_priority (parser, clauses,
34059 token->location);
34060 c_name = "priority";
34061 break;
34062 case PRAGMA_OMP_CLAUSE_PRIVATE:
34063 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34064 clauses);
34065 c_name = "private";
34066 break;
34067 case PRAGMA_OMP_CLAUSE_REDUCTION:
34068 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34069 c_name = "reduction";
34070 break;
34071 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34072 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34073 token->location);
34074 c_name = "schedule";
34075 break;
34076 case PRAGMA_OMP_CLAUSE_SHARED:
34077 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34078 clauses);
34079 c_name = "shared";
34080 break;
34081 case PRAGMA_OMP_CLAUSE_UNTIED:
34082 clauses = cp_parser_omp_clause_untied (parser, clauses,
34083 token->location);
34084 c_name = "untied";
34085 break;
34086 case PRAGMA_OMP_CLAUSE_INBRANCH:
34087 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34088 clauses, token->location);
34089 c_name = "inbranch";
34090 break;
34091 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34092 clauses = cp_parser_omp_clause_branch (parser,
34093 OMP_CLAUSE_NOTINBRANCH,
34094 clauses, token->location);
34095 c_name = "notinbranch";
34096 break;
34097 case PRAGMA_OMP_CLAUSE_PARALLEL:
34098 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34099 clauses, token->location);
34100 c_name = "parallel";
34101 if (!first)
34103 clause_not_first:
34104 error_at (token->location, "%qs must be the first clause of %qs",
34105 c_name, where);
34106 clauses = prev;
34108 break;
34109 case PRAGMA_OMP_CLAUSE_FOR:
34110 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34111 clauses, token->location);
34112 c_name = "for";
34113 if (!first)
34114 goto clause_not_first;
34115 break;
34116 case PRAGMA_OMP_CLAUSE_SECTIONS:
34117 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34118 clauses, token->location);
34119 c_name = "sections";
34120 if (!first)
34121 goto clause_not_first;
34122 break;
34123 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34124 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34125 clauses, token->location);
34126 c_name = "taskgroup";
34127 if (!first)
34128 goto clause_not_first;
34129 break;
34130 case PRAGMA_OMP_CLAUSE_LINK:
34131 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34132 c_name = "to";
34133 break;
34134 case PRAGMA_OMP_CLAUSE_TO:
34135 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34136 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34137 clauses);
34138 else
34139 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34140 c_name = "to";
34141 break;
34142 case PRAGMA_OMP_CLAUSE_FROM:
34143 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34144 c_name = "from";
34145 break;
34146 case PRAGMA_OMP_CLAUSE_UNIFORM:
34147 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34148 clauses);
34149 c_name = "uniform";
34150 break;
34151 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34152 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34153 token->location);
34154 c_name = "num_teams";
34155 break;
34156 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34157 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34158 token->location);
34159 c_name = "thread_limit";
34160 break;
34161 case PRAGMA_OMP_CLAUSE_ALIGNED:
34162 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34163 c_name = "aligned";
34164 break;
34165 case PRAGMA_OMP_CLAUSE_LINEAR:
34167 bool declare_simd = false;
34168 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34169 declare_simd = true;
34170 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34172 c_name = "linear";
34173 break;
34174 case PRAGMA_OMP_CLAUSE_DEPEND:
34175 clauses = cp_parser_omp_clause_depend (parser, clauses,
34176 token->location);
34177 c_name = "depend";
34178 break;
34179 case PRAGMA_OMP_CLAUSE_MAP:
34180 clauses = cp_parser_omp_clause_map (parser, clauses);
34181 c_name = "map";
34182 break;
34183 case PRAGMA_OMP_CLAUSE_DEVICE:
34184 clauses = cp_parser_omp_clause_device (parser, clauses,
34185 token->location);
34186 c_name = "device";
34187 break;
34188 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34189 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34190 token->location);
34191 c_name = "dist_schedule";
34192 break;
34193 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34194 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34195 token->location);
34196 c_name = "proc_bind";
34197 break;
34198 case PRAGMA_OMP_CLAUSE_SAFELEN:
34199 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34200 token->location);
34201 c_name = "safelen";
34202 break;
34203 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34204 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34205 token->location);
34206 c_name = "simdlen";
34207 break;
34208 case PRAGMA_OMP_CLAUSE_NOGROUP:
34209 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34210 token->location);
34211 c_name = "nogroup";
34212 break;
34213 case PRAGMA_OMP_CLAUSE_THREADS:
34214 clauses
34215 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34216 clauses, token->location);
34217 c_name = "threads";
34218 break;
34219 case PRAGMA_OMP_CLAUSE_SIMD:
34220 clauses
34221 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34222 clauses, token->location);
34223 c_name = "simd";
34224 break;
34225 default:
34226 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34227 goto saw_error;
34230 first = false;
34232 if (((mask >> c_kind) & 1) == 0)
34234 /* Remove the invalid clause(s) from the list to avoid
34235 confusing the rest of the compiler. */
34236 clauses = prev;
34237 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34240 saw_error:
34241 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34242 if (finish_p)
34244 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34245 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34246 else
34247 return finish_omp_clauses (clauses, C_ORT_OMP);
34249 return clauses;
34252 /* OpenMP 2.5:
34253 structured-block:
34254 statement
34256 In practice, we're also interested in adding the statement to an
34257 outer node. So it is convenient if we work around the fact that
34258 cp_parser_statement calls add_stmt. */
34260 static unsigned
34261 cp_parser_begin_omp_structured_block (cp_parser *parser)
34263 unsigned save = parser->in_statement;
34265 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34266 This preserves the "not within loop or switch" style error messages
34267 for nonsense cases like
34268 void foo() {
34269 #pragma omp single
34270 break;
34273 if (parser->in_statement)
34274 parser->in_statement = IN_OMP_BLOCK;
34276 return save;
34279 static void
34280 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34282 parser->in_statement = save;
34285 static tree
34286 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34288 tree stmt = begin_omp_structured_block ();
34289 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34291 cp_parser_statement (parser, NULL_TREE, false, if_p);
34293 cp_parser_end_omp_structured_block (parser, save);
34294 return finish_omp_structured_block (stmt);
34297 /* OpenMP 2.5:
34298 # pragma omp atomic new-line
34299 expression-stmt
34301 expression-stmt:
34302 x binop= expr | x++ | ++x | x-- | --x
34303 binop:
34304 +, *, -, /, &, ^, |, <<, >>
34306 where x is an lvalue expression with scalar type.
34308 OpenMP 3.1:
34309 # pragma omp atomic new-line
34310 update-stmt
34312 # pragma omp atomic read new-line
34313 read-stmt
34315 # pragma omp atomic write new-line
34316 write-stmt
34318 # pragma omp atomic update new-line
34319 update-stmt
34321 # pragma omp atomic capture new-line
34322 capture-stmt
34324 # pragma omp atomic capture new-line
34325 capture-block
34327 read-stmt:
34328 v = x
34329 write-stmt:
34330 x = expr
34331 update-stmt:
34332 expression-stmt | x = x binop expr
34333 capture-stmt:
34334 v = expression-stmt
34335 capture-block:
34336 { v = x; update-stmt; } | { update-stmt; v = x; }
34338 OpenMP 4.0:
34339 update-stmt:
34340 expression-stmt | x = x binop expr | x = expr binop x
34341 capture-stmt:
34342 v = update-stmt
34343 capture-block:
34344 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34346 where x and v are lvalue expressions with scalar type. */
34348 static void
34349 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34351 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34352 tree rhs1 = NULL_TREE, orig_lhs;
34353 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34354 bool structured_block = false;
34355 bool seq_cst = false;
34357 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34359 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34360 const char *p = IDENTIFIER_POINTER (id);
34362 if (!strcmp (p, "seq_cst"))
34364 seq_cst = true;
34365 cp_lexer_consume_token (parser->lexer);
34366 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34367 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34368 cp_lexer_consume_token (parser->lexer);
34371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34373 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34374 const char *p = IDENTIFIER_POINTER (id);
34376 if (!strcmp (p, "read"))
34377 code = OMP_ATOMIC_READ;
34378 else if (!strcmp (p, "write"))
34379 code = NOP_EXPR;
34380 else if (!strcmp (p, "update"))
34381 code = OMP_ATOMIC;
34382 else if (!strcmp (p, "capture"))
34383 code = OMP_ATOMIC_CAPTURE_NEW;
34384 else
34385 p = NULL;
34386 if (p)
34387 cp_lexer_consume_token (parser->lexer);
34389 if (!seq_cst)
34391 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34392 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34393 cp_lexer_consume_token (parser->lexer);
34395 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34397 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34398 const char *p = IDENTIFIER_POINTER (id);
34400 if (!strcmp (p, "seq_cst"))
34402 seq_cst = true;
34403 cp_lexer_consume_token (parser->lexer);
34407 cp_parser_require_pragma_eol (parser, pragma_tok);
34409 switch (code)
34411 case OMP_ATOMIC_READ:
34412 case NOP_EXPR: /* atomic write */
34413 v = cp_parser_unary_expression (parser);
34414 if (v == error_mark_node)
34415 goto saw_error;
34416 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34417 goto saw_error;
34418 if (code == NOP_EXPR)
34419 lhs = cp_parser_expression (parser);
34420 else
34421 lhs = cp_parser_unary_expression (parser);
34422 if (lhs == error_mark_node)
34423 goto saw_error;
34424 if (code == NOP_EXPR)
34426 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34427 opcode. */
34428 code = OMP_ATOMIC;
34429 rhs = lhs;
34430 lhs = v;
34431 v = NULL_TREE;
34433 goto done;
34434 case OMP_ATOMIC_CAPTURE_NEW:
34435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34437 cp_lexer_consume_token (parser->lexer);
34438 structured_block = true;
34440 else
34442 v = cp_parser_unary_expression (parser);
34443 if (v == error_mark_node)
34444 goto saw_error;
34445 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34446 goto saw_error;
34448 default:
34449 break;
34452 restart:
34453 lhs = cp_parser_unary_expression (parser);
34454 orig_lhs = lhs;
34455 switch (TREE_CODE (lhs))
34457 case ERROR_MARK:
34458 goto saw_error;
34460 case POSTINCREMENT_EXPR:
34461 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34462 code = OMP_ATOMIC_CAPTURE_OLD;
34463 /* FALLTHROUGH */
34464 case PREINCREMENT_EXPR:
34465 lhs = TREE_OPERAND (lhs, 0);
34466 opcode = PLUS_EXPR;
34467 rhs = integer_one_node;
34468 break;
34470 case POSTDECREMENT_EXPR:
34471 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34472 code = OMP_ATOMIC_CAPTURE_OLD;
34473 /* FALLTHROUGH */
34474 case PREDECREMENT_EXPR:
34475 lhs = TREE_OPERAND (lhs, 0);
34476 opcode = MINUS_EXPR;
34477 rhs = integer_one_node;
34478 break;
34480 case COMPOUND_EXPR:
34481 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34482 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34483 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34484 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34485 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34486 (TREE_OPERAND (lhs, 1), 0), 0)))
34487 == BOOLEAN_TYPE)
34488 /* Undo effects of boolean_increment for post {in,de}crement. */
34489 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34490 /* FALLTHRU */
34491 case MODIFY_EXPR:
34492 if (TREE_CODE (lhs) == MODIFY_EXPR
34493 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34495 /* Undo effects of boolean_increment. */
34496 if (integer_onep (TREE_OPERAND (lhs, 1)))
34498 /* This is pre or post increment. */
34499 rhs = TREE_OPERAND (lhs, 1);
34500 lhs = TREE_OPERAND (lhs, 0);
34501 opcode = NOP_EXPR;
34502 if (code == OMP_ATOMIC_CAPTURE_NEW
34503 && !structured_block
34504 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34505 code = OMP_ATOMIC_CAPTURE_OLD;
34506 break;
34509 /* FALLTHRU */
34510 default:
34511 switch (cp_lexer_peek_token (parser->lexer)->type)
34513 case CPP_MULT_EQ:
34514 opcode = MULT_EXPR;
34515 break;
34516 case CPP_DIV_EQ:
34517 opcode = TRUNC_DIV_EXPR;
34518 break;
34519 case CPP_PLUS_EQ:
34520 opcode = PLUS_EXPR;
34521 break;
34522 case CPP_MINUS_EQ:
34523 opcode = MINUS_EXPR;
34524 break;
34525 case CPP_LSHIFT_EQ:
34526 opcode = LSHIFT_EXPR;
34527 break;
34528 case CPP_RSHIFT_EQ:
34529 opcode = RSHIFT_EXPR;
34530 break;
34531 case CPP_AND_EQ:
34532 opcode = BIT_AND_EXPR;
34533 break;
34534 case CPP_OR_EQ:
34535 opcode = BIT_IOR_EXPR;
34536 break;
34537 case CPP_XOR_EQ:
34538 opcode = BIT_XOR_EXPR;
34539 break;
34540 case CPP_EQ:
34541 enum cp_parser_prec oprec;
34542 cp_token *token;
34543 cp_lexer_consume_token (parser->lexer);
34544 cp_parser_parse_tentatively (parser);
34545 rhs1 = cp_parser_simple_cast_expression (parser);
34546 if (rhs1 == error_mark_node)
34548 cp_parser_abort_tentative_parse (parser);
34549 cp_parser_simple_cast_expression (parser);
34550 goto saw_error;
34552 token = cp_lexer_peek_token (parser->lexer);
34553 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34555 cp_parser_abort_tentative_parse (parser);
34556 cp_parser_parse_tentatively (parser);
34557 rhs = cp_parser_binary_expression (parser, false, true,
34558 PREC_NOT_OPERATOR, NULL);
34559 if (rhs == error_mark_node)
34561 cp_parser_abort_tentative_parse (parser);
34562 cp_parser_binary_expression (parser, false, true,
34563 PREC_NOT_OPERATOR, NULL);
34564 goto saw_error;
34566 switch (TREE_CODE (rhs))
34568 case MULT_EXPR:
34569 case TRUNC_DIV_EXPR:
34570 case RDIV_EXPR:
34571 case PLUS_EXPR:
34572 case MINUS_EXPR:
34573 case LSHIFT_EXPR:
34574 case RSHIFT_EXPR:
34575 case BIT_AND_EXPR:
34576 case BIT_IOR_EXPR:
34577 case BIT_XOR_EXPR:
34578 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34580 if (cp_parser_parse_definitely (parser))
34582 opcode = TREE_CODE (rhs);
34583 rhs1 = TREE_OPERAND (rhs, 0);
34584 rhs = TREE_OPERAND (rhs, 1);
34585 goto stmt_done;
34587 else
34588 goto saw_error;
34590 break;
34591 default:
34592 break;
34594 cp_parser_abort_tentative_parse (parser);
34595 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34597 rhs = cp_parser_expression (parser);
34598 if (rhs == error_mark_node)
34599 goto saw_error;
34600 opcode = NOP_EXPR;
34601 rhs1 = NULL_TREE;
34602 goto stmt_done;
34604 cp_parser_error (parser,
34605 "invalid form of %<#pragma omp atomic%>");
34606 goto saw_error;
34608 if (!cp_parser_parse_definitely (parser))
34609 goto saw_error;
34610 switch (token->type)
34612 case CPP_SEMICOLON:
34613 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34615 code = OMP_ATOMIC_CAPTURE_OLD;
34616 v = lhs;
34617 lhs = NULL_TREE;
34618 lhs1 = rhs1;
34619 rhs1 = NULL_TREE;
34620 cp_lexer_consume_token (parser->lexer);
34621 goto restart;
34623 else if (structured_block)
34625 opcode = NOP_EXPR;
34626 rhs = rhs1;
34627 rhs1 = NULL_TREE;
34628 goto stmt_done;
34630 cp_parser_error (parser,
34631 "invalid form of %<#pragma omp atomic%>");
34632 goto saw_error;
34633 case CPP_MULT:
34634 opcode = MULT_EXPR;
34635 break;
34636 case CPP_DIV:
34637 opcode = TRUNC_DIV_EXPR;
34638 break;
34639 case CPP_PLUS:
34640 opcode = PLUS_EXPR;
34641 break;
34642 case CPP_MINUS:
34643 opcode = MINUS_EXPR;
34644 break;
34645 case CPP_LSHIFT:
34646 opcode = LSHIFT_EXPR;
34647 break;
34648 case CPP_RSHIFT:
34649 opcode = RSHIFT_EXPR;
34650 break;
34651 case CPP_AND:
34652 opcode = BIT_AND_EXPR;
34653 break;
34654 case CPP_OR:
34655 opcode = BIT_IOR_EXPR;
34656 break;
34657 case CPP_XOR:
34658 opcode = BIT_XOR_EXPR;
34659 break;
34660 default:
34661 cp_parser_error (parser,
34662 "invalid operator for %<#pragma omp atomic%>");
34663 goto saw_error;
34665 oprec = TOKEN_PRECEDENCE (token);
34666 gcc_assert (oprec != PREC_NOT_OPERATOR);
34667 if (commutative_tree_code (opcode))
34668 oprec = (enum cp_parser_prec) (oprec - 1);
34669 cp_lexer_consume_token (parser->lexer);
34670 rhs = cp_parser_binary_expression (parser, false, false,
34671 oprec, NULL);
34672 if (rhs == error_mark_node)
34673 goto saw_error;
34674 goto stmt_done;
34675 /* FALLTHROUGH */
34676 default:
34677 cp_parser_error (parser,
34678 "invalid operator for %<#pragma omp atomic%>");
34679 goto saw_error;
34681 cp_lexer_consume_token (parser->lexer);
34683 rhs = cp_parser_expression (parser);
34684 if (rhs == error_mark_node)
34685 goto saw_error;
34686 break;
34688 stmt_done:
34689 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34691 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34692 goto saw_error;
34693 v = cp_parser_unary_expression (parser);
34694 if (v == error_mark_node)
34695 goto saw_error;
34696 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34697 goto saw_error;
34698 lhs1 = cp_parser_unary_expression (parser);
34699 if (lhs1 == error_mark_node)
34700 goto saw_error;
34702 if (structured_block)
34704 cp_parser_consume_semicolon_at_end_of_statement (parser);
34705 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34707 done:
34708 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34709 if (!structured_block)
34710 cp_parser_consume_semicolon_at_end_of_statement (parser);
34711 return;
34713 saw_error:
34714 cp_parser_skip_to_end_of_block_or_statement (parser);
34715 if (structured_block)
34717 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34718 cp_lexer_consume_token (parser->lexer);
34719 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34721 cp_parser_skip_to_end_of_block_or_statement (parser);
34722 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34723 cp_lexer_consume_token (parser->lexer);
34729 /* OpenMP 2.5:
34730 # pragma omp barrier new-line */
34732 static void
34733 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34735 cp_parser_require_pragma_eol (parser, pragma_tok);
34736 finish_omp_barrier ();
34739 /* OpenMP 2.5:
34740 # pragma omp critical [(name)] new-line
34741 structured-block
34743 OpenMP 4.5:
34744 # pragma omp critical [(name) [hint(expression)]] new-line
34745 structured-block */
34747 #define OMP_CRITICAL_CLAUSE_MASK \
34748 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34750 static tree
34751 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34753 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34757 matching_parens parens;
34758 parens.consume_open (parser);
34760 name = cp_parser_identifier (parser);
34762 if (name == error_mark_node
34763 || !parens.require_close (parser))
34764 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34765 /*or_comma=*/false,
34766 /*consume_paren=*/true);
34767 if (name == error_mark_node)
34768 name = NULL;
34770 clauses = cp_parser_omp_all_clauses (parser,
34771 OMP_CRITICAL_CLAUSE_MASK,
34772 "#pragma omp critical", pragma_tok);
34774 else
34775 cp_parser_require_pragma_eol (parser, pragma_tok);
34777 stmt = cp_parser_omp_structured_block (parser, if_p);
34778 return c_finish_omp_critical (input_location, stmt, name, clauses);
34781 /* OpenMP 2.5:
34782 # pragma omp flush flush-vars[opt] new-line
34784 flush-vars:
34785 ( variable-list ) */
34787 static void
34788 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34791 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34792 cp_parser_require_pragma_eol (parser, pragma_tok);
34794 finish_omp_flush ();
34797 /* Helper function, to parse omp for increment expression. */
34799 static tree
34800 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34802 tree cond = cp_parser_binary_expression (parser, false, true,
34803 PREC_NOT_OPERATOR, NULL);
34804 if (cond == error_mark_node
34805 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34807 cp_parser_skip_to_end_of_statement (parser);
34808 return error_mark_node;
34811 switch (TREE_CODE (cond))
34813 case GT_EXPR:
34814 case GE_EXPR:
34815 case LT_EXPR:
34816 case LE_EXPR:
34817 break;
34818 case NE_EXPR:
34819 /* Fall through: OpenMP disallows NE_EXPR. */
34820 gcc_fallthrough ();
34821 default:
34822 return error_mark_node;
34825 /* If decl is an iterator, preserve LHS and RHS of the relational
34826 expr until finish_omp_for. */
34827 if (decl
34828 && (type_dependent_expression_p (decl)
34829 || CLASS_TYPE_P (TREE_TYPE (decl))))
34830 return cond;
34832 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
34833 TREE_CODE (cond),
34834 TREE_OPERAND (cond, 0), ERROR_MARK,
34835 TREE_OPERAND (cond, 1), ERROR_MARK,
34836 /*overload=*/NULL, tf_warning_or_error);
34839 /* Helper function, to parse omp for increment expression. */
34841 static tree
34842 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34844 cp_token *token = cp_lexer_peek_token (parser->lexer);
34845 enum tree_code op;
34846 tree lhs, rhs;
34847 cp_id_kind idk;
34848 bool decl_first;
34850 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34852 op = (token->type == CPP_PLUS_PLUS
34853 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34854 cp_lexer_consume_token (parser->lexer);
34855 lhs = cp_parser_simple_cast_expression (parser);
34856 if (lhs != decl
34857 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34858 return error_mark_node;
34859 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34862 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34863 if (lhs != decl
34864 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34865 return error_mark_node;
34867 token = cp_lexer_peek_token (parser->lexer);
34868 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34870 op = (token->type == CPP_PLUS_PLUS
34871 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34872 cp_lexer_consume_token (parser->lexer);
34873 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34876 op = cp_parser_assignment_operator_opt (parser);
34877 if (op == ERROR_MARK)
34878 return error_mark_node;
34880 if (op != NOP_EXPR)
34882 rhs = cp_parser_assignment_expression (parser);
34883 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34884 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34887 lhs = cp_parser_binary_expression (parser, false, false,
34888 PREC_ADDITIVE_EXPRESSION, NULL);
34889 token = cp_lexer_peek_token (parser->lexer);
34890 decl_first = (lhs == decl
34891 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34892 if (decl_first)
34893 lhs = NULL_TREE;
34894 if (token->type != CPP_PLUS
34895 && token->type != CPP_MINUS)
34896 return error_mark_node;
34900 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34901 cp_lexer_consume_token (parser->lexer);
34902 rhs = cp_parser_binary_expression (parser, false, false,
34903 PREC_ADDITIVE_EXPRESSION, NULL);
34904 token = cp_lexer_peek_token (parser->lexer);
34905 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34907 if (lhs == NULL_TREE)
34909 if (op == PLUS_EXPR)
34910 lhs = rhs;
34911 else
34912 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34913 tf_warning_or_error);
34915 else
34916 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34917 ERROR_MARK, NULL, tf_warning_or_error);
34920 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34922 if (!decl_first)
34924 if ((rhs != decl
34925 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34926 || op == MINUS_EXPR)
34927 return error_mark_node;
34928 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34930 else
34931 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34933 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34936 /* Parse the initialization statement of an OpenMP for loop.
34938 Return true if the resulting construct should have an
34939 OMP_CLAUSE_PRIVATE added to it. */
34941 static tree
34942 cp_parser_omp_for_loop_init (cp_parser *parser,
34943 tree &this_pre_body,
34944 vec<tree, va_gc> *&for_block,
34945 tree &init,
34946 tree &orig_init,
34947 tree &decl,
34948 tree &real_decl)
34950 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34951 return NULL_TREE;
34953 tree add_private_clause = NULL_TREE;
34955 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34957 init-expr:
34958 var = lb
34959 integer-type var = lb
34960 random-access-iterator-type var = lb
34961 pointer-type var = lb
34963 cp_decl_specifier_seq type_specifiers;
34965 /* First, try to parse as an initialized declaration. See
34966 cp_parser_condition, from whence the bulk of this is copied. */
34968 cp_parser_parse_tentatively (parser);
34969 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34970 /*is_trailing_return=*/false,
34971 &type_specifiers);
34972 if (cp_parser_parse_definitely (parser))
34974 /* If parsing a type specifier seq succeeded, then this
34975 MUST be a initialized declaration. */
34976 tree asm_specification, attributes;
34977 cp_declarator *declarator;
34979 declarator = cp_parser_declarator (parser,
34980 CP_PARSER_DECLARATOR_NAMED,
34981 /*ctor_dtor_or_conv_p=*/NULL,
34982 /*parenthesized_p=*/NULL,
34983 /*member_p=*/false,
34984 /*friend_p=*/false);
34985 attributes = cp_parser_attributes_opt (parser);
34986 asm_specification = cp_parser_asm_specification_opt (parser);
34988 if (declarator == cp_error_declarator)
34989 cp_parser_skip_to_end_of_statement (parser);
34991 else
34993 tree pushed_scope, auto_node;
34995 decl = start_decl (declarator, &type_specifiers,
34996 SD_INITIALIZED, attributes,
34997 /*prefix_attributes=*/NULL_TREE,
34998 &pushed_scope);
35000 auto_node = type_uses_auto (TREE_TYPE (decl));
35001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35003 if (cp_lexer_next_token_is (parser->lexer,
35004 CPP_OPEN_PAREN))
35005 error ("parenthesized initialization is not allowed in "
35006 "OpenMP %<for%> loop");
35007 else
35008 /* Trigger an error. */
35009 cp_parser_require (parser, CPP_EQ, RT_EQ);
35011 init = error_mark_node;
35012 cp_parser_skip_to_end_of_statement (parser);
35014 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35015 || type_dependent_expression_p (decl)
35016 || auto_node)
35018 bool is_direct_init, is_non_constant_init;
35020 init = cp_parser_initializer (parser,
35021 &is_direct_init,
35022 &is_non_constant_init);
35024 if (auto_node)
35026 TREE_TYPE (decl)
35027 = do_auto_deduction (TREE_TYPE (decl), init,
35028 auto_node);
35030 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35031 && !type_dependent_expression_p (decl))
35032 goto non_class;
35035 cp_finish_decl (decl, init, !is_non_constant_init,
35036 asm_specification,
35037 LOOKUP_ONLYCONVERTING);
35038 orig_init = init;
35039 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35041 vec_safe_push (for_block, this_pre_body);
35042 init = NULL_TREE;
35044 else
35046 init = pop_stmt_list (this_pre_body);
35047 if (init && TREE_CODE (init) == STATEMENT_LIST)
35049 tree_stmt_iterator i = tsi_start (init);
35050 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35051 while (!tsi_end_p (i))
35053 tree t = tsi_stmt (i);
35054 if (TREE_CODE (t) == DECL_EXPR
35055 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35057 tsi_delink (&i);
35058 vec_safe_push (for_block, t);
35059 continue;
35061 break;
35063 if (tsi_one_before_end_p (i))
35065 tree t = tsi_stmt (i);
35066 tsi_delink (&i);
35067 free_stmt_list (init);
35068 init = t;
35072 this_pre_body = NULL_TREE;
35074 else
35076 /* Consume '='. */
35077 cp_lexer_consume_token (parser->lexer);
35078 init = cp_parser_assignment_expression (parser);
35080 non_class:
35081 if (TYPE_REF_P (TREE_TYPE (decl)))
35082 init = error_mark_node;
35083 else
35084 cp_finish_decl (decl, NULL_TREE,
35085 /*init_const_expr_p=*/false,
35086 asm_specification,
35087 LOOKUP_ONLYCONVERTING);
35090 if (pushed_scope)
35091 pop_scope (pushed_scope);
35094 else
35096 cp_id_kind idk;
35097 /* If parsing a type specifier sequence failed, then
35098 this MUST be a simple expression. */
35099 cp_parser_parse_tentatively (parser);
35100 decl = cp_parser_primary_expression (parser, false, false,
35101 false, &idk);
35102 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35103 if (!cp_parser_error_occurred (parser)
35104 && decl
35105 && (TREE_CODE (decl) == COMPONENT_REF
35106 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35108 cp_parser_abort_tentative_parse (parser);
35109 cp_parser_parse_tentatively (parser);
35110 cp_token *token = cp_lexer_peek_token (parser->lexer);
35111 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35112 /*check_dependency_p=*/true,
35113 /*template_p=*/NULL,
35114 /*declarator_p=*/false,
35115 /*optional_p=*/false);
35116 if (name != error_mark_node
35117 && last_tok == cp_lexer_peek_token (parser->lexer))
35119 decl = cp_parser_lookup_name_simple (parser, name,
35120 token->location);
35121 if (TREE_CODE (decl) == FIELD_DECL)
35122 add_private_clause = omp_privatize_field (decl, false);
35124 cp_parser_abort_tentative_parse (parser);
35125 cp_parser_parse_tentatively (parser);
35126 decl = cp_parser_primary_expression (parser, false, false,
35127 false, &idk);
35129 if (!cp_parser_error_occurred (parser)
35130 && decl
35131 && DECL_P (decl)
35132 && CLASS_TYPE_P (TREE_TYPE (decl)))
35134 tree rhs;
35136 cp_parser_parse_definitely (parser);
35137 cp_parser_require (parser, CPP_EQ, RT_EQ);
35138 rhs = cp_parser_assignment_expression (parser);
35139 orig_init = rhs;
35140 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35141 decl, NOP_EXPR,
35142 rhs,
35143 tf_warning_or_error));
35144 if (!add_private_clause)
35145 add_private_clause = decl;
35147 else
35149 decl = NULL;
35150 cp_parser_abort_tentative_parse (parser);
35151 init = cp_parser_expression (parser);
35152 if (init)
35154 if (TREE_CODE (init) == MODIFY_EXPR
35155 || TREE_CODE (init) == MODOP_EXPR)
35156 real_decl = TREE_OPERAND (init, 0);
35160 return add_private_clause;
35163 /* Parse the restricted form of the for statement allowed by OpenMP. */
35165 static tree
35166 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35167 tree *cclauses, bool *if_p)
35169 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35170 tree real_decl, initv, condv, incrv, declv;
35171 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35172 location_t loc_first;
35173 bool collapse_err = false;
35174 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35175 vec<tree, va_gc> *for_block = make_tree_vector ();
35176 auto_vec<tree, 4> orig_inits;
35177 bool tiling = false;
35179 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35180 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35181 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35182 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35184 tiling = true;
35185 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35187 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35188 && OMP_CLAUSE_ORDERED_EXPR (cl))
35190 ordered_cl = cl;
35191 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35194 if (ordered && ordered < collapse)
35196 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35197 "%<ordered%> clause parameter is less than %<collapse%>");
35198 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35199 = build_int_cst (NULL_TREE, collapse);
35200 ordered = collapse;
35202 if (ordered)
35204 for (tree *pc = &clauses; *pc; )
35205 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35207 error_at (OMP_CLAUSE_LOCATION (*pc),
35208 "%<linear%> clause may not be specified together "
35209 "with %<ordered%> clause with a parameter");
35210 *pc = OMP_CLAUSE_CHAIN (*pc);
35212 else
35213 pc = &OMP_CLAUSE_CHAIN (*pc);
35216 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35217 count = ordered ? ordered : collapse;
35219 declv = make_tree_vec (count);
35220 initv = make_tree_vec (count);
35221 condv = make_tree_vec (count);
35222 incrv = make_tree_vec (count);
35224 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35226 for (i = 0; i < count; i++)
35228 int bracecount = 0;
35229 tree add_private_clause = NULL_TREE;
35230 location_t loc;
35232 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35234 if (!collapse_err)
35235 cp_parser_error (parser, "for statement expected");
35236 return NULL;
35238 loc = cp_lexer_consume_token (parser->lexer)->location;
35240 matching_parens parens;
35241 if (!parens.require_open (parser))
35242 return NULL;
35244 init = orig_init = decl = real_decl = NULL;
35245 this_pre_body = push_stmt_list ();
35247 add_private_clause
35248 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35249 init, orig_init, decl, real_decl);
35251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35252 if (this_pre_body)
35254 this_pre_body = pop_stmt_list (this_pre_body);
35255 if (pre_body)
35257 tree t = pre_body;
35258 pre_body = push_stmt_list ();
35259 add_stmt (t);
35260 add_stmt (this_pre_body);
35261 pre_body = pop_stmt_list (pre_body);
35263 else
35264 pre_body = this_pre_body;
35267 if (decl)
35268 real_decl = decl;
35269 if (cclauses != NULL
35270 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35271 && real_decl != NULL_TREE)
35273 tree *c;
35274 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35275 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35276 && OMP_CLAUSE_DECL (*c) == real_decl)
35278 error_at (loc, "iteration variable %qD"
35279 " should not be firstprivate", real_decl);
35280 *c = OMP_CLAUSE_CHAIN (*c);
35282 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35283 && OMP_CLAUSE_DECL (*c) == real_decl)
35285 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35286 tree l = *c;
35287 *c = OMP_CLAUSE_CHAIN (*c);
35288 if (code == OMP_SIMD)
35290 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35291 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35293 else
35295 OMP_CLAUSE_CHAIN (l) = clauses;
35296 clauses = l;
35298 add_private_clause = NULL_TREE;
35300 else
35302 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35303 && OMP_CLAUSE_DECL (*c) == real_decl)
35304 add_private_clause = NULL_TREE;
35305 c = &OMP_CLAUSE_CHAIN (*c);
35309 if (add_private_clause)
35311 tree c;
35312 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35314 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35315 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35316 && OMP_CLAUSE_DECL (c) == decl)
35317 break;
35318 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35319 && OMP_CLAUSE_DECL (c) == decl)
35320 error_at (loc, "iteration variable %qD "
35321 "should not be firstprivate",
35322 decl);
35323 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35324 && OMP_CLAUSE_DECL (c) == decl)
35325 error_at (loc, "iteration variable %qD should not be reduction",
35326 decl);
35328 if (c == NULL)
35330 if (code != OMP_SIMD)
35331 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35332 else if (collapse == 1)
35333 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35334 else
35335 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35336 OMP_CLAUSE_DECL (c) = add_private_clause;
35337 c = finish_omp_clauses (c, C_ORT_OMP);
35338 if (c)
35340 OMP_CLAUSE_CHAIN (c) = clauses;
35341 clauses = c;
35342 /* For linear, signal that we need to fill up
35343 the so far unknown linear step. */
35344 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35345 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35350 cond = NULL;
35351 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35352 cond = cp_parser_omp_for_cond (parser, decl);
35353 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35355 incr = NULL;
35356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35358 /* If decl is an iterator, preserve the operator on decl
35359 until finish_omp_for. */
35360 if (real_decl
35361 && ((processing_template_decl
35362 && (TREE_TYPE (real_decl) == NULL_TREE
35363 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35364 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35365 incr = cp_parser_omp_for_incr (parser, real_decl);
35366 else
35367 incr = cp_parser_expression (parser);
35368 if (!EXPR_HAS_LOCATION (incr))
35369 protected_set_expr_location (incr, input_location);
35372 if (!parens.require_close (parser))
35373 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35374 /*or_comma=*/false,
35375 /*consume_paren=*/true);
35377 TREE_VEC_ELT (declv, i) = decl;
35378 TREE_VEC_ELT (initv, i) = init;
35379 TREE_VEC_ELT (condv, i) = cond;
35380 TREE_VEC_ELT (incrv, i) = incr;
35381 if (orig_init)
35383 orig_inits.safe_grow_cleared (i + 1);
35384 orig_inits[i] = orig_init;
35387 if (i == count - 1)
35388 break;
35390 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35391 in between the collapsed for loops to be still considered perfectly
35392 nested. Hopefully the final version clarifies this.
35393 For now handle (multiple) {'s and empty statements. */
35394 cp_parser_parse_tentatively (parser);
35395 for (;;)
35397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35398 break;
35399 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35401 cp_lexer_consume_token (parser->lexer);
35402 bracecount++;
35404 else if (bracecount
35405 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35406 cp_lexer_consume_token (parser->lexer);
35407 else
35409 loc = cp_lexer_peek_token (parser->lexer)->location;
35410 error_at (loc, "not enough for loops to collapse");
35411 collapse_err = true;
35412 cp_parser_abort_tentative_parse (parser);
35413 declv = NULL_TREE;
35414 break;
35418 if (declv)
35420 cp_parser_parse_definitely (parser);
35421 nbraces += bracecount;
35425 if (nbraces)
35426 if_p = NULL;
35428 /* Note that we saved the original contents of this flag when we entered
35429 the structured block, and so we don't need to re-save it here. */
35430 parser->in_statement = IN_OMP_FOR;
35432 /* Note that the grammar doesn't call for a structured block here,
35433 though the loop as a whole is a structured block. */
35434 body = push_stmt_list ();
35435 cp_parser_statement (parser, NULL_TREE, false, if_p);
35436 body = pop_stmt_list (body);
35438 if (declv == NULL_TREE)
35439 ret = NULL_TREE;
35440 else
35441 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35442 body, pre_body, &orig_inits, clauses);
35444 while (nbraces)
35446 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35448 cp_lexer_consume_token (parser->lexer);
35449 nbraces--;
35451 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35452 cp_lexer_consume_token (parser->lexer);
35453 else
35455 if (!collapse_err)
35457 error_at (cp_lexer_peek_token (parser->lexer)->location,
35458 "collapsed loops not perfectly nested");
35460 collapse_err = true;
35461 cp_parser_statement_seq_opt (parser, NULL);
35462 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35463 break;
35467 while (!for_block->is_empty ())
35469 tree t = for_block->pop ();
35470 if (TREE_CODE (t) == STATEMENT_LIST)
35471 add_stmt (pop_stmt_list (t));
35472 else
35473 add_stmt (t);
35475 release_tree_vector (for_block);
35477 return ret;
35480 /* Helper function for OpenMP parsing, split clauses and call
35481 finish_omp_clauses on each of the set of clauses afterwards. */
35483 static void
35484 cp_omp_split_clauses (location_t loc, enum tree_code code,
35485 omp_clause_mask mask, tree clauses, tree *cclauses)
35487 int i;
35488 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35489 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35490 if (cclauses[i])
35491 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35494 /* OpenMP 4.0:
35495 #pragma omp simd simd-clause[optseq] new-line
35496 for-loop */
35498 #define OMP_SIMD_CLAUSE_MASK \
35499 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35508 static tree
35509 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35510 char *p_name, omp_clause_mask mask, tree *cclauses,
35511 bool *if_p)
35513 tree clauses, sb, ret;
35514 unsigned int save;
35515 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35517 strcat (p_name, " simd");
35518 mask |= OMP_SIMD_CLAUSE_MASK;
35520 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35521 cclauses == NULL);
35522 if (cclauses)
35524 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35525 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35526 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35527 OMP_CLAUSE_ORDERED);
35528 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35530 error_at (OMP_CLAUSE_LOCATION (c),
35531 "%<ordered%> clause with parameter may not be specified "
35532 "on %qs construct", p_name);
35533 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35537 sb = begin_omp_structured_block ();
35538 save = cp_parser_begin_omp_structured_block (parser);
35540 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35542 cp_parser_end_omp_structured_block (parser, save);
35543 add_stmt (finish_omp_structured_block (sb));
35545 return ret;
35548 /* OpenMP 2.5:
35549 #pragma omp for for-clause[optseq] new-line
35550 for-loop
35552 OpenMP 4.0:
35553 #pragma omp for simd for-simd-clause[optseq] new-line
35554 for-loop */
35556 #define OMP_FOR_CLAUSE_MASK \
35557 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35567 static tree
35568 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35569 char *p_name, omp_clause_mask mask, tree *cclauses,
35570 bool *if_p)
35572 tree clauses, sb, ret;
35573 unsigned int save;
35574 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35576 strcat (p_name, " for");
35577 mask |= OMP_FOR_CLAUSE_MASK;
35578 /* parallel for{, simd} disallows nowait clause, but for
35579 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35580 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35581 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35582 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35583 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35584 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35586 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35589 const char *p = IDENTIFIER_POINTER (id);
35591 if (strcmp (p, "simd") == 0)
35593 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35594 if (cclauses == NULL)
35595 cclauses = cclauses_buf;
35597 cp_lexer_consume_token (parser->lexer);
35598 if (!flag_openmp) /* flag_openmp_simd */
35599 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35600 cclauses, if_p);
35601 sb = begin_omp_structured_block ();
35602 save = cp_parser_begin_omp_structured_block (parser);
35603 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35604 cclauses, if_p);
35605 cp_parser_end_omp_structured_block (parser, save);
35606 tree body = finish_omp_structured_block (sb);
35607 if (ret == NULL)
35608 return ret;
35609 ret = make_node (OMP_FOR);
35610 TREE_TYPE (ret) = void_type_node;
35611 OMP_FOR_BODY (ret) = body;
35612 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35613 SET_EXPR_LOCATION (ret, loc);
35614 add_stmt (ret);
35615 return ret;
35618 if (!flag_openmp) /* flag_openmp_simd */
35620 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35621 return NULL_TREE;
35624 /* Composite distribute parallel for disallows linear clause. */
35625 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35626 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35628 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35629 cclauses == NULL);
35630 if (cclauses)
35632 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35633 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35636 sb = begin_omp_structured_block ();
35637 save = cp_parser_begin_omp_structured_block (parser);
35639 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35641 cp_parser_end_omp_structured_block (parser, save);
35642 add_stmt (finish_omp_structured_block (sb));
35644 return ret;
35647 /* OpenMP 2.5:
35648 # pragma omp master new-line
35649 structured-block */
35651 static tree
35652 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35654 cp_parser_require_pragma_eol (parser, pragma_tok);
35655 return c_finish_omp_master (input_location,
35656 cp_parser_omp_structured_block (parser, if_p));
35659 /* OpenMP 2.5:
35660 # pragma omp ordered new-line
35661 structured-block
35663 OpenMP 4.5:
35664 # pragma omp ordered ordered-clauses new-line
35665 structured-block */
35667 #define OMP_ORDERED_CLAUSE_MASK \
35668 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35671 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35672 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35674 static bool
35675 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35676 enum pragma_context context, bool *if_p)
35678 location_t loc = pragma_tok->location;
35680 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35682 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35683 const char *p = IDENTIFIER_POINTER (id);
35685 if (strcmp (p, "depend") == 0)
35687 if (!flag_openmp) /* flag_openmp_simd */
35689 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35690 return false;
35692 if (context == pragma_stmt)
35694 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35695 "%<depend%> clause may only be used in compound "
35696 "statements");
35697 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35698 return false;
35700 tree clauses
35701 = cp_parser_omp_all_clauses (parser,
35702 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35703 "#pragma omp ordered", pragma_tok);
35704 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35705 return false;
35709 tree clauses
35710 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35711 "#pragma omp ordered", pragma_tok);
35713 if (!flag_openmp /* flag_openmp_simd */
35714 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35715 return false;
35717 c_finish_omp_ordered (loc, clauses,
35718 cp_parser_omp_structured_block (parser, if_p));
35719 return true;
35722 /* OpenMP 2.5:
35724 section-scope:
35725 { section-sequence }
35727 section-sequence:
35728 section-directive[opt] structured-block
35729 section-sequence section-directive structured-block */
35731 static tree
35732 cp_parser_omp_sections_scope (cp_parser *parser)
35734 tree stmt, substmt;
35735 bool error_suppress = false;
35736 cp_token *tok;
35738 matching_braces braces;
35739 if (!braces.require_open (parser))
35740 return NULL_TREE;
35742 stmt = push_stmt_list ();
35744 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35745 != PRAGMA_OMP_SECTION)
35747 substmt = cp_parser_omp_structured_block (parser, NULL);
35748 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35749 add_stmt (substmt);
35752 while (1)
35754 tok = cp_lexer_peek_token (parser->lexer);
35755 if (tok->type == CPP_CLOSE_BRACE)
35756 break;
35757 if (tok->type == CPP_EOF)
35758 break;
35760 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35762 cp_lexer_consume_token (parser->lexer);
35763 cp_parser_require_pragma_eol (parser, tok);
35764 error_suppress = false;
35766 else if (!error_suppress)
35768 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35769 error_suppress = true;
35772 substmt = cp_parser_omp_structured_block (parser, NULL);
35773 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35774 add_stmt (substmt);
35776 braces.require_close (parser);
35778 substmt = pop_stmt_list (stmt);
35780 stmt = make_node (OMP_SECTIONS);
35781 TREE_TYPE (stmt) = void_type_node;
35782 OMP_SECTIONS_BODY (stmt) = substmt;
35784 add_stmt (stmt);
35785 return stmt;
35788 /* OpenMP 2.5:
35789 # pragma omp sections sections-clause[optseq] newline
35790 sections-scope */
35792 #define OMP_SECTIONS_CLAUSE_MASK \
35793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35799 static tree
35800 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35801 char *p_name, omp_clause_mask mask, tree *cclauses)
35803 tree clauses, ret;
35804 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35806 strcat (p_name, " sections");
35807 mask |= OMP_SECTIONS_CLAUSE_MASK;
35808 if (cclauses)
35809 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35811 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35812 cclauses == NULL);
35813 if (cclauses)
35815 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35816 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35819 ret = cp_parser_omp_sections_scope (parser);
35820 if (ret)
35821 OMP_SECTIONS_CLAUSES (ret) = clauses;
35823 return ret;
35826 /* OpenMP 2.5:
35827 # pragma omp parallel parallel-clause[optseq] new-line
35828 structured-block
35829 # pragma omp parallel for parallel-for-clause[optseq] new-line
35830 structured-block
35831 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35832 structured-block
35834 OpenMP 4.0:
35835 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35836 structured-block */
35838 #define OMP_PARALLEL_CLAUSE_MASK \
35839 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35849 static tree
35850 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35851 char *p_name, omp_clause_mask mask, tree *cclauses,
35852 bool *if_p)
35854 tree stmt, clauses, block;
35855 unsigned int save;
35856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35858 strcat (p_name, " parallel");
35859 mask |= OMP_PARALLEL_CLAUSE_MASK;
35860 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35861 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35862 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35863 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35867 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35868 if (cclauses == NULL)
35869 cclauses = cclauses_buf;
35871 cp_lexer_consume_token (parser->lexer);
35872 if (!flag_openmp) /* flag_openmp_simd */
35873 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35874 if_p);
35875 block = begin_omp_parallel ();
35876 save = cp_parser_begin_omp_structured_block (parser);
35877 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35878 if_p);
35879 cp_parser_end_omp_structured_block (parser, save);
35880 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35881 block);
35882 if (ret == NULL_TREE)
35883 return ret;
35884 OMP_PARALLEL_COMBINED (stmt) = 1;
35885 return stmt;
35887 /* When combined with distribute, parallel has to be followed by for.
35888 #pragma omp target parallel is allowed though. */
35889 else if (cclauses
35890 && (mask & (OMP_CLAUSE_MASK_1
35891 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35893 error_at (loc, "expected %<for%> after %qs", p_name);
35894 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35895 return NULL_TREE;
35897 else if (!flag_openmp) /* flag_openmp_simd */
35899 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35900 return NULL_TREE;
35902 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35904 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35905 const char *p = IDENTIFIER_POINTER (id);
35906 if (strcmp (p, "sections") == 0)
35908 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35909 cclauses = cclauses_buf;
35911 cp_lexer_consume_token (parser->lexer);
35912 block = begin_omp_parallel ();
35913 save = cp_parser_begin_omp_structured_block (parser);
35914 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35915 cp_parser_end_omp_structured_block (parser, save);
35916 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35917 block);
35918 OMP_PARALLEL_COMBINED (stmt) = 1;
35919 return stmt;
35923 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35924 cclauses == NULL);
35925 if (cclauses)
35927 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35928 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35931 block = begin_omp_parallel ();
35932 save = cp_parser_begin_omp_structured_block (parser);
35933 cp_parser_statement (parser, NULL_TREE, false, if_p);
35934 cp_parser_end_omp_structured_block (parser, save);
35935 stmt = finish_omp_parallel (clauses, block);
35936 return stmt;
35939 /* OpenMP 2.5:
35940 # pragma omp single single-clause[optseq] new-line
35941 structured-block */
35943 #define OMP_SINGLE_CLAUSE_MASK \
35944 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35949 static tree
35950 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35952 tree stmt = make_node (OMP_SINGLE);
35953 TREE_TYPE (stmt) = void_type_node;
35955 OMP_SINGLE_CLAUSES (stmt)
35956 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35957 "#pragma omp single", pragma_tok);
35958 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35960 return add_stmt (stmt);
35963 /* OpenMP 3.0:
35964 # pragma omp task task-clause[optseq] new-line
35965 structured-block */
35967 #define OMP_TASK_CLAUSE_MASK \
35968 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35979 static tree
35980 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35982 tree clauses, block;
35983 unsigned int save;
35985 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35986 "#pragma omp task", pragma_tok);
35987 block = begin_omp_task ();
35988 save = cp_parser_begin_omp_structured_block (parser);
35989 cp_parser_statement (parser, NULL_TREE, false, if_p);
35990 cp_parser_end_omp_structured_block (parser, save);
35991 return finish_omp_task (clauses, block);
35994 /* OpenMP 3.0:
35995 # pragma omp taskwait new-line */
35997 static void
35998 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
36000 cp_parser_require_pragma_eol (parser, pragma_tok);
36001 finish_omp_taskwait ();
36004 /* OpenMP 3.1:
36005 # pragma omp taskyield new-line */
36007 static void
36008 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36010 cp_parser_require_pragma_eol (parser, pragma_tok);
36011 finish_omp_taskyield ();
36014 /* OpenMP 4.0:
36015 # pragma omp taskgroup new-line
36016 structured-block */
36018 static tree
36019 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36021 cp_parser_require_pragma_eol (parser, pragma_tok);
36022 return c_finish_omp_taskgroup (input_location,
36023 cp_parser_omp_structured_block (parser,
36024 if_p));
36028 /* OpenMP 2.5:
36029 # pragma omp threadprivate (variable-list) */
36031 static void
36032 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36034 tree vars;
36036 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36037 cp_parser_require_pragma_eol (parser, pragma_tok);
36039 finish_omp_threadprivate (vars);
36042 /* OpenMP 4.0:
36043 # pragma omp cancel cancel-clause[optseq] new-line */
36045 #define OMP_CANCEL_CLAUSE_MASK \
36046 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36052 static void
36053 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36055 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36056 "#pragma omp cancel", pragma_tok);
36057 finish_omp_cancel (clauses);
36060 /* OpenMP 4.0:
36061 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36063 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36064 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36069 static void
36070 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36071 enum pragma_context context)
36073 tree clauses;
36074 bool point_seen = false;
36076 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36078 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36079 const char *p = IDENTIFIER_POINTER (id);
36081 if (strcmp (p, "point") == 0)
36083 cp_lexer_consume_token (parser->lexer);
36084 point_seen = true;
36087 if (!point_seen)
36089 cp_parser_error (parser, "expected %<point%>");
36090 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36091 return;
36094 if (context != pragma_compound)
36096 if (context == pragma_stmt)
36097 error_at (pragma_tok->location,
36098 "%<#pragma %s%> may only be used in compound statements",
36099 "omp cancellation point");
36100 else
36101 cp_parser_error (parser, "expected declaration specifiers");
36102 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36103 return;
36106 clauses = cp_parser_omp_all_clauses (parser,
36107 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36108 "#pragma omp cancellation point",
36109 pragma_tok);
36110 finish_omp_cancellation_point (clauses);
36113 /* OpenMP 4.0:
36114 #pragma omp distribute distribute-clause[optseq] new-line
36115 for-loop */
36117 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36118 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36124 static tree
36125 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36126 char *p_name, omp_clause_mask mask, tree *cclauses,
36127 bool *if_p)
36129 tree clauses, sb, ret;
36130 unsigned int save;
36131 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36133 strcat (p_name, " distribute");
36134 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36136 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36138 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36139 const char *p = IDENTIFIER_POINTER (id);
36140 bool simd = false;
36141 bool parallel = false;
36143 if (strcmp (p, "simd") == 0)
36144 simd = true;
36145 else
36146 parallel = strcmp (p, "parallel") == 0;
36147 if (parallel || simd)
36149 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36150 if (cclauses == NULL)
36151 cclauses = cclauses_buf;
36152 cp_lexer_consume_token (parser->lexer);
36153 if (!flag_openmp) /* flag_openmp_simd */
36155 if (simd)
36156 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36157 cclauses, if_p);
36158 else
36159 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36160 cclauses, if_p);
36162 sb = begin_omp_structured_block ();
36163 save = cp_parser_begin_omp_structured_block (parser);
36164 if (simd)
36165 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36166 cclauses, if_p);
36167 else
36168 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36169 cclauses, if_p);
36170 cp_parser_end_omp_structured_block (parser, save);
36171 tree body = finish_omp_structured_block (sb);
36172 if (ret == NULL)
36173 return ret;
36174 ret = make_node (OMP_DISTRIBUTE);
36175 TREE_TYPE (ret) = void_type_node;
36176 OMP_FOR_BODY (ret) = body;
36177 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36178 SET_EXPR_LOCATION (ret, loc);
36179 add_stmt (ret);
36180 return ret;
36183 if (!flag_openmp) /* flag_openmp_simd */
36185 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36186 return NULL_TREE;
36189 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36190 cclauses == NULL);
36191 if (cclauses)
36193 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36194 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36197 sb = begin_omp_structured_block ();
36198 save = cp_parser_begin_omp_structured_block (parser);
36200 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36202 cp_parser_end_omp_structured_block (parser, save);
36203 add_stmt (finish_omp_structured_block (sb));
36205 return ret;
36208 /* OpenMP 4.0:
36209 # pragma omp teams teams-clause[optseq] new-line
36210 structured-block */
36212 #define OMP_TEAMS_CLAUSE_MASK \
36213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36221 static tree
36222 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36223 char *p_name, omp_clause_mask mask, tree *cclauses,
36224 bool *if_p)
36226 tree clauses, sb, ret;
36227 unsigned int save;
36228 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36230 strcat (p_name, " teams");
36231 mask |= OMP_TEAMS_CLAUSE_MASK;
36233 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36235 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36236 const char *p = IDENTIFIER_POINTER (id);
36237 if (strcmp (p, "distribute") == 0)
36239 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36240 if (cclauses == NULL)
36241 cclauses = cclauses_buf;
36243 cp_lexer_consume_token (parser->lexer);
36244 if (!flag_openmp) /* flag_openmp_simd */
36245 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36246 cclauses, if_p);
36247 sb = begin_omp_structured_block ();
36248 save = cp_parser_begin_omp_structured_block (parser);
36249 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36250 cclauses, if_p);
36251 cp_parser_end_omp_structured_block (parser, save);
36252 tree body = finish_omp_structured_block (sb);
36253 if (ret == NULL)
36254 return ret;
36255 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36256 ret = make_node (OMP_TEAMS);
36257 TREE_TYPE (ret) = void_type_node;
36258 OMP_TEAMS_CLAUSES (ret) = clauses;
36259 OMP_TEAMS_BODY (ret) = body;
36260 OMP_TEAMS_COMBINED (ret) = 1;
36261 SET_EXPR_LOCATION (ret, loc);
36262 return add_stmt (ret);
36265 if (!flag_openmp) /* flag_openmp_simd */
36267 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36268 return NULL_TREE;
36271 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36272 cclauses == NULL);
36273 if (cclauses)
36275 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36276 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36279 tree stmt = make_node (OMP_TEAMS);
36280 TREE_TYPE (stmt) = void_type_node;
36281 OMP_TEAMS_CLAUSES (stmt) = clauses;
36282 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36283 SET_EXPR_LOCATION (stmt, loc);
36285 return add_stmt (stmt);
36288 /* OpenMP 4.0:
36289 # pragma omp target data target-data-clause[optseq] new-line
36290 structured-block */
36292 #define OMP_TARGET_DATA_CLAUSE_MASK \
36293 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36298 static tree
36299 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36301 tree clauses
36302 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36303 "#pragma omp target data", pragma_tok);
36304 int map_seen = 0;
36305 for (tree *pc = &clauses; *pc;)
36307 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36308 switch (OMP_CLAUSE_MAP_KIND (*pc))
36310 case GOMP_MAP_TO:
36311 case GOMP_MAP_ALWAYS_TO:
36312 case GOMP_MAP_FROM:
36313 case GOMP_MAP_ALWAYS_FROM:
36314 case GOMP_MAP_TOFROM:
36315 case GOMP_MAP_ALWAYS_TOFROM:
36316 case GOMP_MAP_ALLOC:
36317 map_seen = 3;
36318 break;
36319 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36320 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36321 case GOMP_MAP_ALWAYS_POINTER:
36322 break;
36323 default:
36324 map_seen |= 1;
36325 error_at (OMP_CLAUSE_LOCATION (*pc),
36326 "%<#pragma omp target data%> with map-type other "
36327 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36328 "on %<map%> clause");
36329 *pc = OMP_CLAUSE_CHAIN (*pc);
36330 continue;
36332 pc = &OMP_CLAUSE_CHAIN (*pc);
36335 if (map_seen != 3)
36337 if (map_seen == 0)
36338 error_at (pragma_tok->location,
36339 "%<#pragma omp target data%> must contain at least "
36340 "one %<map%> clause");
36341 return NULL_TREE;
36344 tree stmt = make_node (OMP_TARGET_DATA);
36345 TREE_TYPE (stmt) = void_type_node;
36346 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36348 keep_next_level (true);
36349 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36351 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36352 return add_stmt (stmt);
36355 /* OpenMP 4.5:
36356 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36357 structured-block */
36359 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36366 static tree
36367 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36368 enum pragma_context context)
36370 bool data_seen = false;
36371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36373 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36374 const char *p = IDENTIFIER_POINTER (id);
36376 if (strcmp (p, "data") == 0)
36378 cp_lexer_consume_token (parser->lexer);
36379 data_seen = true;
36382 if (!data_seen)
36384 cp_parser_error (parser, "expected %<data%>");
36385 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36386 return NULL_TREE;
36389 if (context == pragma_stmt)
36391 error_at (pragma_tok->location,
36392 "%<#pragma %s%> may only be used in compound statements",
36393 "omp target enter data");
36394 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36395 return NULL_TREE;
36398 tree clauses
36399 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36400 "#pragma omp target enter data", pragma_tok);
36401 int map_seen = 0;
36402 for (tree *pc = &clauses; *pc;)
36404 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36405 switch (OMP_CLAUSE_MAP_KIND (*pc))
36407 case GOMP_MAP_TO:
36408 case GOMP_MAP_ALWAYS_TO:
36409 case GOMP_MAP_ALLOC:
36410 map_seen = 3;
36411 break;
36412 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36413 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36414 case GOMP_MAP_ALWAYS_POINTER:
36415 break;
36416 default:
36417 map_seen |= 1;
36418 error_at (OMP_CLAUSE_LOCATION (*pc),
36419 "%<#pragma omp target enter data%> with map-type other "
36420 "than %<to%> or %<alloc%> on %<map%> clause");
36421 *pc = OMP_CLAUSE_CHAIN (*pc);
36422 continue;
36424 pc = &OMP_CLAUSE_CHAIN (*pc);
36427 if (map_seen != 3)
36429 if (map_seen == 0)
36430 error_at (pragma_tok->location,
36431 "%<#pragma omp target enter data%> must contain at least "
36432 "one %<map%> clause");
36433 return NULL_TREE;
36436 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36437 TREE_TYPE (stmt) = void_type_node;
36438 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36439 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36440 return add_stmt (stmt);
36443 /* OpenMP 4.5:
36444 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36445 structured-block */
36447 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36448 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36454 static tree
36455 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36456 enum pragma_context context)
36458 bool data_seen = false;
36459 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36461 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36462 const char *p = IDENTIFIER_POINTER (id);
36464 if (strcmp (p, "data") == 0)
36466 cp_lexer_consume_token (parser->lexer);
36467 data_seen = true;
36470 if (!data_seen)
36472 cp_parser_error (parser, "expected %<data%>");
36473 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36474 return NULL_TREE;
36477 if (context == pragma_stmt)
36479 error_at (pragma_tok->location,
36480 "%<#pragma %s%> may only be used in compound statements",
36481 "omp target exit data");
36482 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36483 return NULL_TREE;
36486 tree clauses
36487 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36488 "#pragma omp target exit data", pragma_tok);
36489 int map_seen = 0;
36490 for (tree *pc = &clauses; *pc;)
36492 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36493 switch (OMP_CLAUSE_MAP_KIND (*pc))
36495 case GOMP_MAP_FROM:
36496 case GOMP_MAP_ALWAYS_FROM:
36497 case GOMP_MAP_RELEASE:
36498 case GOMP_MAP_DELETE:
36499 map_seen = 3;
36500 break;
36501 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36502 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36503 case GOMP_MAP_ALWAYS_POINTER:
36504 break;
36505 default:
36506 map_seen |= 1;
36507 error_at (OMP_CLAUSE_LOCATION (*pc),
36508 "%<#pragma omp target exit data%> with map-type other "
36509 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36510 " clause");
36511 *pc = OMP_CLAUSE_CHAIN (*pc);
36512 continue;
36514 pc = &OMP_CLAUSE_CHAIN (*pc);
36517 if (map_seen != 3)
36519 if (map_seen == 0)
36520 error_at (pragma_tok->location,
36521 "%<#pragma omp target exit data%> must contain at least "
36522 "one %<map%> clause");
36523 return NULL_TREE;
36526 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36527 TREE_TYPE (stmt) = void_type_node;
36528 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36529 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36530 return add_stmt (stmt);
36533 /* OpenMP 4.0:
36534 # pragma omp target update target-update-clause[optseq] new-line */
36536 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36544 static bool
36545 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36546 enum pragma_context context)
36548 if (context == pragma_stmt)
36550 error_at (pragma_tok->location,
36551 "%<#pragma %s%> may only be used in compound statements",
36552 "omp target update");
36553 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36554 return false;
36557 tree clauses
36558 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36559 "#pragma omp target update", pragma_tok);
36560 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36561 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36563 error_at (pragma_tok->location,
36564 "%<#pragma omp target update%> must contain at least one "
36565 "%<from%> or %<to%> clauses");
36566 return false;
36569 tree stmt = make_node (OMP_TARGET_UPDATE);
36570 TREE_TYPE (stmt) = void_type_node;
36571 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36572 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36573 add_stmt (stmt);
36574 return false;
36577 /* OpenMP 4.0:
36578 # pragma omp target target-clause[optseq] new-line
36579 structured-block */
36581 #define OMP_TARGET_CLAUSE_MASK \
36582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36592 static bool
36593 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36594 enum pragma_context context, bool *if_p)
36596 tree *pc = NULL, stmt;
36598 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36600 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36601 const char *p = IDENTIFIER_POINTER (id);
36602 enum tree_code ccode = ERROR_MARK;
36604 if (strcmp (p, "teams") == 0)
36605 ccode = OMP_TEAMS;
36606 else if (strcmp (p, "parallel") == 0)
36607 ccode = OMP_PARALLEL;
36608 else if (strcmp (p, "simd") == 0)
36609 ccode = OMP_SIMD;
36610 if (ccode != ERROR_MARK)
36612 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36613 char p_name[sizeof ("#pragma omp target teams distribute "
36614 "parallel for simd")];
36616 cp_lexer_consume_token (parser->lexer);
36617 strcpy (p_name, "#pragma omp target");
36618 if (!flag_openmp) /* flag_openmp_simd */
36620 tree stmt;
36621 switch (ccode)
36623 case OMP_TEAMS:
36624 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36625 OMP_TARGET_CLAUSE_MASK,
36626 cclauses, if_p);
36627 break;
36628 case OMP_PARALLEL:
36629 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36630 OMP_TARGET_CLAUSE_MASK,
36631 cclauses, if_p);
36632 break;
36633 case OMP_SIMD:
36634 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36635 OMP_TARGET_CLAUSE_MASK,
36636 cclauses, if_p);
36637 break;
36638 default:
36639 gcc_unreachable ();
36641 return stmt != NULL_TREE;
36643 keep_next_level (true);
36644 tree sb = begin_omp_structured_block (), ret;
36645 unsigned save = cp_parser_begin_omp_structured_block (parser);
36646 switch (ccode)
36648 case OMP_TEAMS:
36649 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36650 OMP_TARGET_CLAUSE_MASK, cclauses,
36651 if_p);
36652 break;
36653 case OMP_PARALLEL:
36654 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36655 OMP_TARGET_CLAUSE_MASK, cclauses,
36656 if_p);
36657 break;
36658 case OMP_SIMD:
36659 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36660 OMP_TARGET_CLAUSE_MASK, cclauses,
36661 if_p);
36662 break;
36663 default:
36664 gcc_unreachable ();
36666 cp_parser_end_omp_structured_block (parser, save);
36667 tree body = finish_omp_structured_block (sb);
36668 if (ret == NULL_TREE)
36669 return false;
36670 if (ccode == OMP_TEAMS && !processing_template_decl)
36672 /* For combined target teams, ensure the num_teams and
36673 thread_limit clause expressions are evaluated on the host,
36674 before entering the target construct. */
36675 tree c;
36676 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36677 c; c = OMP_CLAUSE_CHAIN (c))
36678 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36679 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36680 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36682 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36683 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36684 if (expr == error_mark_node)
36685 continue;
36686 tree tmp = TARGET_EXPR_SLOT (expr);
36687 add_stmt (expr);
36688 OMP_CLAUSE_OPERAND (c, 0) = expr;
36689 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36690 OMP_CLAUSE_FIRSTPRIVATE);
36691 OMP_CLAUSE_DECL (tc) = tmp;
36692 OMP_CLAUSE_CHAIN (tc)
36693 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36694 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36697 tree stmt = make_node (OMP_TARGET);
36698 TREE_TYPE (stmt) = void_type_node;
36699 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36700 OMP_TARGET_BODY (stmt) = body;
36701 OMP_TARGET_COMBINED (stmt) = 1;
36702 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36703 add_stmt (stmt);
36704 pc = &OMP_TARGET_CLAUSES (stmt);
36705 goto check_clauses;
36707 else if (!flag_openmp) /* flag_openmp_simd */
36709 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36710 return false;
36712 else if (strcmp (p, "data") == 0)
36714 cp_lexer_consume_token (parser->lexer);
36715 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36716 return true;
36718 else if (strcmp (p, "enter") == 0)
36720 cp_lexer_consume_token (parser->lexer);
36721 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36722 return false;
36724 else if (strcmp (p, "exit") == 0)
36726 cp_lexer_consume_token (parser->lexer);
36727 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36728 return false;
36730 else if (strcmp (p, "update") == 0)
36732 cp_lexer_consume_token (parser->lexer);
36733 return cp_parser_omp_target_update (parser, pragma_tok, context);
36736 if (!flag_openmp) /* flag_openmp_simd */
36738 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36739 return false;
36742 stmt = make_node (OMP_TARGET);
36743 TREE_TYPE (stmt) = void_type_node;
36745 OMP_TARGET_CLAUSES (stmt)
36746 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36747 "#pragma omp target", pragma_tok);
36748 pc = &OMP_TARGET_CLAUSES (stmt);
36749 keep_next_level (true);
36750 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36752 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36753 add_stmt (stmt);
36755 check_clauses:
36756 while (*pc)
36758 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36759 switch (OMP_CLAUSE_MAP_KIND (*pc))
36761 case GOMP_MAP_TO:
36762 case GOMP_MAP_ALWAYS_TO:
36763 case GOMP_MAP_FROM:
36764 case GOMP_MAP_ALWAYS_FROM:
36765 case GOMP_MAP_TOFROM:
36766 case GOMP_MAP_ALWAYS_TOFROM:
36767 case GOMP_MAP_ALLOC:
36768 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36769 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36770 case GOMP_MAP_ALWAYS_POINTER:
36771 break;
36772 default:
36773 error_at (OMP_CLAUSE_LOCATION (*pc),
36774 "%<#pragma omp target%> with map-type other "
36775 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36776 "on %<map%> clause");
36777 *pc = OMP_CLAUSE_CHAIN (*pc);
36778 continue;
36780 pc = &OMP_CLAUSE_CHAIN (*pc);
36782 return true;
36785 /* OpenACC 2.0:
36786 # pragma acc cache (variable-list) new-line
36789 static tree
36790 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36792 tree stmt, clauses;
36794 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36795 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36797 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36799 stmt = make_node (OACC_CACHE);
36800 TREE_TYPE (stmt) = void_type_node;
36801 OACC_CACHE_CLAUSES (stmt) = clauses;
36802 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36803 add_stmt (stmt);
36805 return stmt;
36808 /* OpenACC 2.0:
36809 # pragma acc data oacc-data-clause[optseq] new-line
36810 structured-block */
36812 #define OACC_DATA_CLAUSE_MASK \
36813 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36821 static tree
36822 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36824 tree stmt, clauses, block;
36825 unsigned int save;
36827 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36828 "#pragma acc data", pragma_tok);
36830 block = begin_omp_parallel ();
36831 save = cp_parser_begin_omp_structured_block (parser);
36832 cp_parser_statement (parser, NULL_TREE, false, if_p);
36833 cp_parser_end_omp_structured_block (parser, save);
36834 stmt = finish_oacc_data (clauses, block);
36835 return stmt;
36838 /* OpenACC 2.0:
36839 # pragma acc host_data <clauses> new-line
36840 structured-block */
36842 #define OACC_HOST_DATA_CLAUSE_MASK \
36843 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36845 static tree
36846 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36848 tree stmt, clauses, block;
36849 unsigned int save;
36851 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36852 "#pragma acc host_data", pragma_tok);
36854 block = begin_omp_parallel ();
36855 save = cp_parser_begin_omp_structured_block (parser);
36856 cp_parser_statement (parser, NULL_TREE, false, if_p);
36857 cp_parser_end_omp_structured_block (parser, save);
36858 stmt = finish_oacc_host_data (clauses, block);
36859 return stmt;
36862 /* OpenACC 2.0:
36863 # pragma acc declare oacc-data-clause[optseq] new-line
36866 #define OACC_DECLARE_CLAUSE_MASK \
36867 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36876 static tree
36877 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36879 tree clauses, stmt;
36880 bool error = false;
36882 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36883 "#pragma acc declare", pragma_tok, true);
36886 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36888 error_at (pragma_tok->location,
36889 "no valid clauses specified in %<#pragma acc declare%>");
36890 return NULL_TREE;
36893 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36895 location_t loc = OMP_CLAUSE_LOCATION (t);
36896 tree decl = OMP_CLAUSE_DECL (t);
36897 if (!DECL_P (decl))
36899 error_at (loc, "array section in %<#pragma acc declare%>");
36900 error = true;
36901 continue;
36903 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36904 switch (OMP_CLAUSE_MAP_KIND (t))
36906 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36907 case GOMP_MAP_ALLOC:
36908 case GOMP_MAP_TO:
36909 case GOMP_MAP_FORCE_DEVICEPTR:
36910 case GOMP_MAP_DEVICE_RESIDENT:
36911 break;
36913 case GOMP_MAP_LINK:
36914 if (!global_bindings_p ()
36915 && (TREE_STATIC (decl)
36916 || !DECL_EXTERNAL (decl)))
36918 error_at (loc,
36919 "%qD must be a global variable in "
36920 "%<#pragma acc declare link%>",
36921 decl);
36922 error = true;
36923 continue;
36925 break;
36927 default:
36928 if (global_bindings_p ())
36930 error_at (loc, "invalid OpenACC clause at file scope");
36931 error = true;
36932 continue;
36934 if (DECL_EXTERNAL (decl))
36936 error_at (loc,
36937 "invalid use of %<extern%> variable %qD "
36938 "in %<#pragma acc declare%>", decl);
36939 error = true;
36940 continue;
36942 else if (TREE_PUBLIC (decl))
36944 error_at (loc,
36945 "invalid use of %<global%> variable %qD "
36946 "in %<#pragma acc declare%>", decl);
36947 error = true;
36948 continue;
36950 break;
36953 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36954 || lookup_attribute ("omp declare target link",
36955 DECL_ATTRIBUTES (decl)))
36957 error_at (loc, "variable %qD used more than once with "
36958 "%<#pragma acc declare%>", decl);
36959 error = true;
36960 continue;
36963 if (!error)
36965 tree id;
36967 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36968 id = get_identifier ("omp declare target link");
36969 else
36970 id = get_identifier ("omp declare target");
36972 DECL_ATTRIBUTES (decl)
36973 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36974 if (global_bindings_p ())
36976 symtab_node *node = symtab_node::get (decl);
36977 if (node != NULL)
36979 node->offloadable = 1;
36980 if (ENABLE_OFFLOADING)
36982 g->have_offload = true;
36983 if (is_a <varpool_node *> (node))
36984 vec_safe_push (offload_vars, decl);
36991 if (error || global_bindings_p ())
36992 return NULL_TREE;
36994 stmt = make_node (OACC_DECLARE);
36995 TREE_TYPE (stmt) = void_type_node;
36996 OACC_DECLARE_CLAUSES (stmt) = clauses;
36997 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36999 add_stmt (stmt);
37001 return NULL_TREE;
37004 /* OpenACC 2.0:
37005 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37009 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37011 LOC is the location of the #pragma token.
37014 #define OACC_ENTER_DATA_CLAUSE_MASK \
37015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37021 #define OACC_EXIT_DATA_CLAUSE_MASK \
37022 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37029 static tree
37030 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37031 bool enter)
37033 location_t loc = pragma_tok->location;
37034 tree stmt, clauses;
37035 const char *p = "";
37037 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37038 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37040 if (strcmp (p, "data") != 0)
37042 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37043 enter ? "enter" : "exit");
37044 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37045 return NULL_TREE;
37048 cp_lexer_consume_token (parser->lexer);
37050 if (enter)
37051 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37052 "#pragma acc enter data", pragma_tok);
37053 else
37054 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37055 "#pragma acc exit data", pragma_tok);
37057 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37059 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37060 enter ? "enter" : "exit");
37061 return NULL_TREE;
37064 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37065 TREE_TYPE (stmt) = void_type_node;
37066 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37067 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37068 add_stmt (stmt);
37069 return stmt;
37072 /* OpenACC 2.0:
37073 # pragma acc loop oacc-loop-clause[optseq] new-line
37074 structured-block */
37076 #define OACC_LOOP_CLAUSE_MASK \
37077 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37088 static tree
37089 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37090 omp_clause_mask mask, tree *cclauses, bool *if_p)
37092 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37094 strcat (p_name, " loop");
37095 mask |= OACC_LOOP_CLAUSE_MASK;
37097 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37098 cclauses == NULL);
37099 if (cclauses)
37101 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37102 if (*cclauses)
37103 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37104 if (clauses)
37105 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37108 tree block = begin_omp_structured_block ();
37109 int save = cp_parser_begin_omp_structured_block (parser);
37110 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37111 cp_parser_end_omp_structured_block (parser, save);
37112 add_stmt (finish_omp_structured_block (block));
37114 return stmt;
37117 /* OpenACC 2.0:
37118 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37119 structured-block
37123 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37124 structured-block
37127 #define OACC_KERNELS_CLAUSE_MASK \
37128 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37142 #define OACC_PARALLEL_CLAUSE_MASK \
37143 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37160 static tree
37161 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37162 char *p_name, bool *if_p)
37164 omp_clause_mask mask;
37165 enum tree_code code;
37166 switch (cp_parser_pragma_kind (pragma_tok))
37168 case PRAGMA_OACC_KERNELS:
37169 strcat (p_name, " kernels");
37170 mask = OACC_KERNELS_CLAUSE_MASK;
37171 code = OACC_KERNELS;
37172 break;
37173 case PRAGMA_OACC_PARALLEL:
37174 strcat (p_name, " parallel");
37175 mask = OACC_PARALLEL_CLAUSE_MASK;
37176 code = OACC_PARALLEL;
37177 break;
37178 default:
37179 gcc_unreachable ();
37182 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37184 const char *p
37185 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37186 if (strcmp (p, "loop") == 0)
37188 cp_lexer_consume_token (parser->lexer);
37189 tree block = begin_omp_parallel ();
37190 tree clauses;
37191 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37192 if_p);
37193 return finish_omp_construct (code, block, clauses);
37197 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37199 tree block = begin_omp_parallel ();
37200 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37201 cp_parser_statement (parser, NULL_TREE, false, if_p);
37202 cp_parser_end_omp_structured_block (parser, save);
37203 return finish_omp_construct (code, block, clauses);
37206 /* OpenACC 2.0:
37207 # pragma acc update oacc-update-clause[optseq] new-line
37210 #define OACC_UPDATE_CLAUSE_MASK \
37211 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37218 static tree
37219 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37221 tree stmt, clauses;
37223 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37224 "#pragma acc update", pragma_tok);
37226 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37228 error_at (pragma_tok->location,
37229 "%<#pragma acc update%> must contain at least one "
37230 "%<device%> or %<host%> or %<self%> clause");
37231 return NULL_TREE;
37234 stmt = make_node (OACC_UPDATE);
37235 TREE_TYPE (stmt) = void_type_node;
37236 OACC_UPDATE_CLAUSES (stmt) = clauses;
37237 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37238 add_stmt (stmt);
37239 return stmt;
37242 /* OpenACC 2.0:
37243 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37245 LOC is the location of the #pragma token.
37248 #define OACC_WAIT_CLAUSE_MASK \
37249 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37251 static tree
37252 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37254 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37255 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37257 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37258 list = cp_parser_oacc_wait_list (parser, loc, list);
37260 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37261 "#pragma acc wait", pragma_tok);
37263 stmt = c_finish_oacc_wait (loc, list, clauses);
37264 stmt = finish_expr_stmt (stmt);
37266 return stmt;
37269 /* OpenMP 4.0:
37270 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37272 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37273 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37280 static void
37281 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37282 enum pragma_context context)
37284 bool first_p = parser->omp_declare_simd == NULL;
37285 cp_omp_declare_simd_data data;
37286 if (first_p)
37288 data.error_seen = false;
37289 data.fndecl_seen = false;
37290 data.tokens = vNULL;
37291 data.clauses = NULL_TREE;
37292 /* It is safe to take the address of a local variable; it will only be
37293 used while this scope is live. */
37294 parser->omp_declare_simd = &data;
37297 /* Store away all pragma tokens. */
37298 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37299 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37300 cp_lexer_consume_token (parser->lexer);
37301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37302 parser->omp_declare_simd->error_seen = true;
37303 cp_parser_require_pragma_eol (parser, pragma_tok);
37304 struct cp_token_cache *cp
37305 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37306 parser->omp_declare_simd->tokens.safe_push (cp);
37308 if (first_p)
37310 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37311 cp_parser_pragma (parser, context, NULL);
37312 switch (context)
37314 case pragma_external:
37315 cp_parser_declaration (parser);
37316 break;
37317 case pragma_member:
37318 cp_parser_member_declaration (parser);
37319 break;
37320 case pragma_objc_icode:
37321 cp_parser_block_declaration (parser, /*statement_p=*/false);
37322 break;
37323 default:
37324 cp_parser_declaration_statement (parser);
37325 break;
37327 if (parser->omp_declare_simd
37328 && !parser->omp_declare_simd->error_seen
37329 && !parser->omp_declare_simd->fndecl_seen)
37330 error_at (pragma_tok->location,
37331 "%<#pragma omp declare simd%> not immediately followed by "
37332 "function declaration or definition");
37333 data.tokens.release ();
37334 parser->omp_declare_simd = NULL;
37338 /* Finalize #pragma omp declare simd clauses after direct declarator has
37339 been parsed, and put that into "omp declare simd" attribute. */
37341 static tree
37342 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37344 struct cp_token_cache *ce;
37345 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37346 int i;
37348 if (!data->error_seen && data->fndecl_seen)
37350 error ("%<#pragma omp declare simd%> not immediately followed by "
37351 "a single function declaration or definition");
37352 data->error_seen = true;
37354 if (data->error_seen)
37355 return attrs;
37357 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37359 tree c, cl;
37361 cp_parser_push_lexer_for_tokens (parser, ce);
37362 parser->lexer->in_pragma = true;
37363 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37364 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37365 cp_lexer_consume_token (parser->lexer);
37366 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37367 "#pragma omp declare simd", pragma_tok);
37368 cp_parser_pop_lexer (parser);
37369 if (cl)
37370 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37371 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37372 TREE_CHAIN (c) = attrs;
37373 if (processing_template_decl)
37374 ATTR_IS_DEPENDENT (c) = 1;
37375 attrs = c;
37378 data->fndecl_seen = true;
37379 return attrs;
37383 /* OpenMP 4.0:
37384 # pragma omp declare target new-line
37385 declarations and definitions
37386 # pragma omp end declare target new-line
37388 OpenMP 4.5:
37389 # pragma omp declare target ( extended-list ) new-line
37391 # pragma omp declare target declare-target-clauses[seq] new-line */
37393 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37394 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37397 static void
37398 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37400 tree clauses = NULL_TREE;
37401 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37402 clauses
37403 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37404 "#pragma omp declare target", pragma_tok);
37405 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37407 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37408 clauses);
37409 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37410 cp_parser_require_pragma_eol (parser, pragma_tok);
37412 else
37414 cp_parser_require_pragma_eol (parser, pragma_tok);
37415 scope_chain->omp_declare_target_attribute++;
37416 return;
37418 if (scope_chain->omp_declare_target_attribute)
37419 error_at (pragma_tok->location,
37420 "%<#pragma omp declare target%> with clauses in between "
37421 "%<#pragma omp declare target%> without clauses and "
37422 "%<#pragma omp end declare target%>");
37423 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37425 tree t = OMP_CLAUSE_DECL (c), id;
37426 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37427 tree at2 = lookup_attribute ("omp declare target link",
37428 DECL_ATTRIBUTES (t));
37429 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37431 id = get_identifier ("omp declare target link");
37432 std::swap (at1, at2);
37434 else
37435 id = get_identifier ("omp declare target");
37436 if (at2)
37438 error_at (OMP_CLAUSE_LOCATION (c),
37439 "%qD specified both in declare target %<link%> and %<to%>"
37440 " clauses", t);
37441 continue;
37443 if (!at1)
37445 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37446 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37447 continue;
37449 symtab_node *node = symtab_node::get (t);
37450 if (node != NULL)
37452 node->offloadable = 1;
37453 if (ENABLE_OFFLOADING)
37455 g->have_offload = true;
37456 if (is_a <varpool_node *> (node))
37457 vec_safe_push (offload_vars, t);
37464 static void
37465 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37467 const char *p = "";
37468 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37470 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37471 p = IDENTIFIER_POINTER (id);
37473 if (strcmp (p, "declare") == 0)
37475 cp_lexer_consume_token (parser->lexer);
37476 p = "";
37477 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37479 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37480 p = IDENTIFIER_POINTER (id);
37482 if (strcmp (p, "target") == 0)
37483 cp_lexer_consume_token (parser->lexer);
37484 else
37486 cp_parser_error (parser, "expected %<target%>");
37487 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37488 return;
37491 else
37493 cp_parser_error (parser, "expected %<declare%>");
37494 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37495 return;
37497 cp_parser_require_pragma_eol (parser, pragma_tok);
37498 if (!scope_chain->omp_declare_target_attribute)
37499 error_at (pragma_tok->location,
37500 "%<#pragma omp end declare target%> without corresponding "
37501 "%<#pragma omp declare target%>");
37502 else
37503 scope_chain->omp_declare_target_attribute--;
37506 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37507 expression and optional initializer clause of
37508 #pragma omp declare reduction. We store the expression(s) as
37509 either 3, 6 or 7 special statements inside of the artificial function's
37510 body. The first two statements are DECL_EXPRs for the artificial
37511 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37512 expression that uses those variables.
37513 If there was any INITIALIZER clause, this is followed by further statements,
37514 the fourth and fifth statements are DECL_EXPRs for the artificial
37515 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37516 constructor variant (first token after open paren is not omp_priv),
37517 then the sixth statement is a statement with the function call expression
37518 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37519 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37520 to initialize the OMP_PRIV artificial variable and there is seventh
37521 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37523 static bool
37524 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37526 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37527 gcc_assert (TYPE_REF_P (type));
37528 type = TREE_TYPE (type);
37529 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37530 DECL_ARTIFICIAL (omp_out) = 1;
37531 pushdecl (omp_out);
37532 add_decl_expr (omp_out);
37533 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37534 DECL_ARTIFICIAL (omp_in) = 1;
37535 pushdecl (omp_in);
37536 add_decl_expr (omp_in);
37537 tree combiner;
37538 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37540 keep_next_level (true);
37541 tree block = begin_omp_structured_block ();
37542 combiner = cp_parser_expression (parser);
37543 finish_expr_stmt (combiner);
37544 block = finish_omp_structured_block (block);
37545 add_stmt (block);
37547 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37548 return false;
37550 const char *p = "";
37551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37553 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37554 p = IDENTIFIER_POINTER (id);
37557 if (strcmp (p, "initializer") == 0)
37559 cp_lexer_consume_token (parser->lexer);
37560 matching_parens parens;
37561 if (!parens.require_open (parser))
37562 return false;
37564 p = "";
37565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37567 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37568 p = IDENTIFIER_POINTER (id);
37571 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37572 DECL_ARTIFICIAL (omp_priv) = 1;
37573 pushdecl (omp_priv);
37574 add_decl_expr (omp_priv);
37575 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37576 DECL_ARTIFICIAL (omp_orig) = 1;
37577 pushdecl (omp_orig);
37578 add_decl_expr (omp_orig);
37580 keep_next_level (true);
37581 block = begin_omp_structured_block ();
37583 bool ctor = false;
37584 if (strcmp (p, "omp_priv") == 0)
37586 bool is_direct_init, is_non_constant_init;
37587 ctor = true;
37588 cp_lexer_consume_token (parser->lexer);
37589 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37590 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37591 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37592 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37593 == CPP_CLOSE_PAREN
37594 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37595 == CPP_CLOSE_PAREN))
37597 finish_omp_structured_block (block);
37598 error ("invalid initializer clause");
37599 return false;
37601 initializer = cp_parser_initializer (parser, &is_direct_init,
37602 &is_non_constant_init);
37603 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37604 NULL_TREE, LOOKUP_ONLYCONVERTING);
37606 else
37608 cp_parser_parse_tentatively (parser);
37609 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37610 /*check_dependency_p=*/true,
37611 /*template_p=*/NULL,
37612 /*declarator_p=*/false,
37613 /*optional_p=*/false);
37614 vec<tree, va_gc> *args;
37615 if (fn_name == error_mark_node
37616 || cp_parser_error_occurred (parser)
37617 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37618 || ((args = cp_parser_parenthesized_expression_list
37619 (parser, non_attr, /*cast_p=*/false,
37620 /*allow_expansion_p=*/true,
37621 /*non_constant_p=*/NULL)),
37622 cp_parser_error_occurred (parser)))
37624 finish_omp_structured_block (block);
37625 cp_parser_abort_tentative_parse (parser);
37626 cp_parser_error (parser, "expected id-expression (arguments)");
37627 return false;
37629 unsigned int i;
37630 tree arg;
37631 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37632 if (arg == omp_priv
37633 || (TREE_CODE (arg) == ADDR_EXPR
37634 && TREE_OPERAND (arg, 0) == omp_priv))
37635 break;
37636 cp_parser_abort_tentative_parse (parser);
37637 if (arg == NULL_TREE)
37638 error ("one of the initializer call arguments should be %<omp_priv%>"
37639 " or %<&omp_priv%>");
37640 initializer = cp_parser_postfix_expression (parser, false, false, false,
37641 false, NULL);
37642 finish_expr_stmt (initializer);
37645 block = finish_omp_structured_block (block);
37646 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37647 add_stmt (block);
37649 if (ctor)
37650 add_decl_expr (omp_orig);
37652 if (!parens.require_close (parser))
37653 return false;
37656 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37657 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37658 UNKNOWN_LOCATION);
37660 return true;
37663 /* OpenMP 4.0
37664 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37665 initializer-clause[opt] new-line
37667 initializer-clause:
37668 initializer (omp_priv initializer)
37669 initializer (function-name (argument-list)) */
37671 static void
37672 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37673 enum pragma_context)
37675 auto_vec<tree> types;
37676 enum tree_code reduc_code = ERROR_MARK;
37677 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37678 unsigned int i;
37679 cp_token *first_token;
37680 cp_token_cache *cp;
37681 int errs;
37682 void *p;
37684 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37685 p = obstack_alloc (&declarator_obstack, 0);
37687 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37688 goto fail;
37690 switch (cp_lexer_peek_token (parser->lexer)->type)
37692 case CPP_PLUS:
37693 reduc_code = PLUS_EXPR;
37694 break;
37695 case CPP_MULT:
37696 reduc_code = MULT_EXPR;
37697 break;
37698 case CPP_MINUS:
37699 reduc_code = MINUS_EXPR;
37700 break;
37701 case CPP_AND:
37702 reduc_code = BIT_AND_EXPR;
37703 break;
37704 case CPP_XOR:
37705 reduc_code = BIT_XOR_EXPR;
37706 break;
37707 case CPP_OR:
37708 reduc_code = BIT_IOR_EXPR;
37709 break;
37710 case CPP_AND_AND:
37711 reduc_code = TRUTH_ANDIF_EXPR;
37712 break;
37713 case CPP_OR_OR:
37714 reduc_code = TRUTH_ORIF_EXPR;
37715 break;
37716 case CPP_NAME:
37717 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37718 break;
37719 default:
37720 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37721 "%<|%>, %<&&%>, %<||%> or identifier");
37722 goto fail;
37725 if (reduc_code != ERROR_MARK)
37726 cp_lexer_consume_token (parser->lexer);
37728 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37729 if (reduc_id == error_mark_node)
37730 goto fail;
37732 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37733 goto fail;
37735 /* Types may not be defined in declare reduction type list. */
37736 const char *saved_message;
37737 saved_message = parser->type_definition_forbidden_message;
37738 parser->type_definition_forbidden_message
37739 = G_("types may not be defined in declare reduction type list");
37740 bool saved_colon_corrects_to_scope_p;
37741 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37742 parser->colon_corrects_to_scope_p = false;
37743 bool saved_colon_doesnt_start_class_def_p;
37744 saved_colon_doesnt_start_class_def_p
37745 = parser->colon_doesnt_start_class_def_p;
37746 parser->colon_doesnt_start_class_def_p = true;
37748 while (true)
37750 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37751 type = cp_parser_type_id (parser);
37752 if (type == error_mark_node)
37754 else if (ARITHMETIC_TYPE_P (type)
37755 && (orig_reduc_id == NULL_TREE
37756 || (TREE_CODE (type) != COMPLEX_TYPE
37757 && (id_equal (orig_reduc_id, "min")
37758 || id_equal (orig_reduc_id, "max")))))
37759 error_at (loc, "predeclared arithmetic type %qT in "
37760 "%<#pragma omp declare reduction%>", type);
37761 else if (TREE_CODE (type) == FUNCTION_TYPE
37762 || TREE_CODE (type) == METHOD_TYPE
37763 || TREE_CODE (type) == ARRAY_TYPE)
37764 error_at (loc, "function or array type %qT in "
37765 "%<#pragma omp declare reduction%>", type);
37766 else if (TYPE_REF_P (type))
37767 error_at (loc, "reference type %qT in "
37768 "%<#pragma omp declare reduction%>", type);
37769 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37770 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37771 "%<#pragma omp declare reduction%>", type);
37772 else
37773 types.safe_push (type);
37775 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37776 cp_lexer_consume_token (parser->lexer);
37777 else
37778 break;
37781 /* Restore the saved message. */
37782 parser->type_definition_forbidden_message = saved_message;
37783 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37784 parser->colon_doesnt_start_class_def_p
37785 = saved_colon_doesnt_start_class_def_p;
37787 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37788 || types.is_empty ())
37790 fail:
37791 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37792 goto done;
37795 first_token = cp_lexer_peek_token (parser->lexer);
37796 cp = NULL;
37797 errs = errorcount;
37798 FOR_EACH_VEC_ELT (types, i, type)
37800 tree fntype
37801 = build_function_type_list (void_type_node,
37802 cp_build_reference_type (type, false),
37803 NULL_TREE);
37804 tree this_reduc_id = reduc_id;
37805 if (!dependent_type_p (type))
37806 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37807 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37808 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37809 DECL_ARTIFICIAL (fndecl) = 1;
37810 DECL_EXTERNAL (fndecl) = 1;
37811 DECL_DECLARED_INLINE_P (fndecl) = 1;
37812 DECL_IGNORED_P (fndecl) = 1;
37813 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37814 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37815 DECL_ATTRIBUTES (fndecl)
37816 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37817 DECL_ATTRIBUTES (fndecl));
37818 if (processing_template_decl)
37819 fndecl = push_template_decl (fndecl);
37820 bool block_scope = false;
37821 tree block = NULL_TREE;
37822 if (current_function_decl)
37824 block_scope = true;
37825 DECL_CONTEXT (fndecl) = global_namespace;
37826 if (!processing_template_decl)
37827 pushdecl (fndecl);
37829 else if (current_class_type)
37831 if (cp == NULL)
37833 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37834 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37835 cp_lexer_consume_token (parser->lexer);
37836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37837 goto fail;
37838 cp = cp_token_cache_new (first_token,
37839 cp_lexer_peek_nth_token (parser->lexer,
37840 2));
37842 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37843 finish_member_declaration (fndecl);
37844 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37845 DECL_PENDING_INLINE_P (fndecl) = 1;
37846 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37847 continue;
37849 else
37851 DECL_CONTEXT (fndecl) = current_namespace;
37852 pushdecl (fndecl);
37854 if (!block_scope)
37855 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37856 else
37857 block = begin_omp_structured_block ();
37858 if (cp)
37860 cp_parser_push_lexer_for_tokens (parser, cp);
37861 parser->lexer->in_pragma = true;
37863 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37865 if (!block_scope)
37866 finish_function (/*inline_p=*/false);
37867 else
37868 DECL_CONTEXT (fndecl) = current_function_decl;
37869 if (cp)
37870 cp_parser_pop_lexer (parser);
37871 goto fail;
37873 if (cp)
37874 cp_parser_pop_lexer (parser);
37875 if (!block_scope)
37876 finish_function (/*inline_p=*/false);
37877 else
37879 DECL_CONTEXT (fndecl) = current_function_decl;
37880 block = finish_omp_structured_block (block);
37881 if (TREE_CODE (block) == BIND_EXPR)
37882 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37883 else if (TREE_CODE (block) == STATEMENT_LIST)
37884 DECL_SAVED_TREE (fndecl) = block;
37885 if (processing_template_decl)
37886 add_decl_expr (fndecl);
37888 cp_check_omp_declare_reduction (fndecl);
37889 if (cp == NULL && types.length () > 1)
37890 cp = cp_token_cache_new (first_token,
37891 cp_lexer_peek_nth_token (parser->lexer, 2));
37892 if (errs != errorcount)
37893 break;
37896 cp_parser_require_pragma_eol (parser, pragma_tok);
37898 done:
37899 /* Free any declarators allocated. */
37900 obstack_free (&declarator_obstack, p);
37903 /* OpenMP 4.0
37904 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37905 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37906 initializer-clause[opt] new-line
37907 #pragma omp declare target new-line */
37909 static bool
37910 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37911 enum pragma_context context)
37913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37916 const char *p = IDENTIFIER_POINTER (id);
37918 if (strcmp (p, "simd") == 0)
37920 cp_lexer_consume_token (parser->lexer);
37921 cp_parser_omp_declare_simd (parser, pragma_tok,
37922 context);
37923 return true;
37925 cp_ensure_no_omp_declare_simd (parser);
37926 if (strcmp (p, "reduction") == 0)
37928 cp_lexer_consume_token (parser->lexer);
37929 cp_parser_omp_declare_reduction (parser, pragma_tok,
37930 context);
37931 return false;
37933 if (!flag_openmp) /* flag_openmp_simd */
37935 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37936 return false;
37938 if (strcmp (p, "target") == 0)
37940 cp_lexer_consume_token (parser->lexer);
37941 cp_parser_omp_declare_target (parser, pragma_tok);
37942 return false;
37945 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37946 "or %<target%>");
37947 cp_parser_require_pragma_eol (parser, pragma_tok);
37948 return false;
37951 /* OpenMP 4.5:
37952 #pragma omp taskloop taskloop-clause[optseq] new-line
37953 for-loop
37955 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37956 for-loop */
37958 #define OMP_TASKLOOP_CLAUSE_MASK \
37959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37974 static tree
37975 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37976 char *p_name, omp_clause_mask mask, tree *cclauses,
37977 bool *if_p)
37979 tree clauses, sb, ret;
37980 unsigned int save;
37981 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37983 strcat (p_name, " taskloop");
37984 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37986 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37988 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37989 const char *p = IDENTIFIER_POINTER (id);
37991 if (strcmp (p, "simd") == 0)
37993 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37994 if (cclauses == NULL)
37995 cclauses = cclauses_buf;
37997 cp_lexer_consume_token (parser->lexer);
37998 if (!flag_openmp) /* flag_openmp_simd */
37999 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38000 cclauses, if_p);
38001 sb = begin_omp_structured_block ();
38002 save = cp_parser_begin_omp_structured_block (parser);
38003 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38004 cclauses, if_p);
38005 cp_parser_end_omp_structured_block (parser, save);
38006 tree body = finish_omp_structured_block (sb);
38007 if (ret == NULL)
38008 return ret;
38009 ret = make_node (OMP_TASKLOOP);
38010 TREE_TYPE (ret) = void_type_node;
38011 OMP_FOR_BODY (ret) = body;
38012 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38013 SET_EXPR_LOCATION (ret, loc);
38014 add_stmt (ret);
38015 return ret;
38018 if (!flag_openmp) /* flag_openmp_simd */
38020 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38021 return NULL_TREE;
38024 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38025 cclauses == NULL);
38026 if (cclauses)
38028 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38029 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38032 sb = begin_omp_structured_block ();
38033 save = cp_parser_begin_omp_structured_block (parser);
38035 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38036 if_p);
38038 cp_parser_end_omp_structured_block (parser, save);
38039 add_stmt (finish_omp_structured_block (sb));
38041 return ret;
38045 /* OpenACC 2.0:
38046 # pragma acc routine oacc-routine-clause[optseq] new-line
38047 function-definition
38049 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38052 #define OACC_ROUTINE_CLAUSE_MASK \
38053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38059 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38060 component, which must resolve to a declared namespace-scope
38061 function. The clauses are either processed directly (for a named
38062 function), or defered until the immediatley following declaration
38063 is parsed. */
38065 static void
38066 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38067 enum pragma_context context)
38069 gcc_checking_assert (context == pragma_external);
38070 /* The checking for "another pragma following this one" in the "no optional
38071 '( name )'" case makes sure that we dont re-enter. */
38072 gcc_checking_assert (parser->oacc_routine == NULL);
38074 cp_oacc_routine_data data;
38075 data.error_seen = false;
38076 data.fndecl_seen = false;
38077 data.tokens = vNULL;
38078 data.clauses = NULL_TREE;
38079 data.loc = pragma_tok->location;
38080 /* It is safe to take the address of a local variable; it will only be
38081 used while this scope is live. */
38082 parser->oacc_routine = &data;
38084 /* Look for optional '( name )'. */
38085 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38087 matching_parens parens;
38088 parens.consume_open (parser); /* '(' */
38090 /* We parse the name as an id-expression. If it resolves to
38091 anything other than a non-overloaded function at namespace
38092 scope, it's an error. */
38093 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38094 tree name = cp_parser_id_expression (parser,
38095 /*template_keyword_p=*/false,
38096 /*check_dependency_p=*/false,
38097 /*template_p=*/NULL,
38098 /*declarator_p=*/false,
38099 /*optional_p=*/false);
38100 tree decl = (identifier_p (name)
38101 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38102 : name);
38103 if (name != error_mark_node && decl == error_mark_node)
38104 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38106 if (decl == error_mark_node
38107 || !parens.require_close (parser))
38109 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38110 parser->oacc_routine = NULL;
38111 return;
38114 data.clauses
38115 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38116 "#pragma acc routine",
38117 cp_lexer_peek_token (parser->lexer));
38119 if (decl && is_overloaded_fn (decl)
38120 && (TREE_CODE (decl) != FUNCTION_DECL
38121 || DECL_FUNCTION_TEMPLATE_P (decl)))
38123 error_at (name_loc,
38124 "%<#pragma acc routine%> names a set of overloads");
38125 parser->oacc_routine = NULL;
38126 return;
38129 /* Perhaps we should use the same rule as declarations in different
38130 namespaces? */
38131 if (!DECL_NAMESPACE_SCOPE_P (decl))
38133 error_at (name_loc,
38134 "%qD does not refer to a namespace scope function", decl);
38135 parser->oacc_routine = NULL;
38136 return;
38139 if (TREE_CODE (decl) != FUNCTION_DECL)
38141 error_at (name_loc, "%qD does not refer to a function", decl);
38142 parser->oacc_routine = NULL;
38143 return;
38146 cp_finalize_oacc_routine (parser, decl, false);
38147 parser->oacc_routine = NULL;
38149 else /* No optional '( name )'. */
38151 /* Store away all pragma tokens. */
38152 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38153 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38154 cp_lexer_consume_token (parser->lexer);
38155 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38156 parser->oacc_routine->error_seen = true;
38157 cp_parser_require_pragma_eol (parser, pragma_tok);
38158 struct cp_token_cache *cp
38159 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38160 parser->oacc_routine->tokens.safe_push (cp);
38162 /* Emit a helpful diagnostic if there's another pragma following this
38163 one. */
38164 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38166 cp_ensure_no_oacc_routine (parser);
38167 data.tokens.release ();
38168 /* ..., and then just keep going. */
38169 return;
38172 /* We only have to consider the pragma_external case here. */
38173 cp_parser_declaration (parser);
38174 if (parser->oacc_routine
38175 && !parser->oacc_routine->fndecl_seen)
38176 cp_ensure_no_oacc_routine (parser);
38177 else
38178 parser->oacc_routine = NULL;
38179 data.tokens.release ();
38183 /* Finalize #pragma acc routine clauses after direct declarator has
38184 been parsed. */
38186 static tree
38187 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38189 struct cp_token_cache *ce;
38190 cp_oacc_routine_data *data = parser->oacc_routine;
38192 if (!data->error_seen && data->fndecl_seen)
38194 error_at (data->loc,
38195 "%<#pragma acc routine%> not immediately followed by "
38196 "a single function declaration or definition");
38197 data->error_seen = true;
38199 if (data->error_seen)
38200 return attrs;
38202 gcc_checking_assert (data->tokens.length () == 1);
38203 ce = data->tokens[0];
38205 cp_parser_push_lexer_for_tokens (parser, ce);
38206 parser->lexer->in_pragma = true;
38207 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38209 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38210 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38211 parser->oacc_routine->clauses
38212 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38213 "#pragma acc routine", pragma_tok);
38214 cp_parser_pop_lexer (parser);
38215 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38216 fndecl_seen. */
38218 return attrs;
38221 /* Apply any saved OpenACC routine clauses to a just-parsed
38222 declaration. */
38224 static void
38225 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38227 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38229 /* Keep going if we're in error reporting mode. */
38230 if (parser->oacc_routine->error_seen
38231 || fndecl == error_mark_node)
38232 return;
38234 if (parser->oacc_routine->fndecl_seen)
38236 error_at (parser->oacc_routine->loc,
38237 "%<#pragma acc routine%> not immediately followed by"
38238 " a single function declaration or definition");
38239 parser->oacc_routine = NULL;
38240 return;
38242 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38244 cp_ensure_no_oacc_routine (parser);
38245 return;
38248 if (oacc_get_fn_attrib (fndecl))
38250 error_at (parser->oacc_routine->loc,
38251 "%<#pragma acc routine%> already applied to %qD", fndecl);
38252 parser->oacc_routine = NULL;
38253 return;
38256 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38258 error_at (parser->oacc_routine->loc,
38259 TREE_USED (fndecl)
38260 ? G_("%<#pragma acc routine%> must be applied before use")
38261 : G_("%<#pragma acc routine%> must be applied before "
38262 "definition"));
38263 parser->oacc_routine = NULL;
38264 return;
38267 /* Process the routine's dimension clauses. */
38268 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38269 oacc_replace_fn_attrib (fndecl, dims);
38271 /* Add an "omp declare target" attribute. */
38272 DECL_ATTRIBUTES (fndecl)
38273 = tree_cons (get_identifier ("omp declare target"),
38274 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38276 /* Don't unset parser->oacc_routine here: we may still need it to
38277 diagnose wrong usage. But, remember that we've used this "#pragma acc
38278 routine". */
38279 parser->oacc_routine->fndecl_seen = true;
38283 /* Main entry point to OpenMP statement pragmas. */
38285 static void
38286 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38288 tree stmt;
38289 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38290 omp_clause_mask mask (0);
38292 switch (cp_parser_pragma_kind (pragma_tok))
38294 case PRAGMA_OACC_ATOMIC:
38295 cp_parser_omp_atomic (parser, pragma_tok);
38296 return;
38297 case PRAGMA_OACC_CACHE:
38298 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38299 break;
38300 case PRAGMA_OACC_DATA:
38301 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38302 break;
38303 case PRAGMA_OACC_ENTER_DATA:
38304 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38305 break;
38306 case PRAGMA_OACC_EXIT_DATA:
38307 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38308 break;
38309 case PRAGMA_OACC_HOST_DATA:
38310 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38311 break;
38312 case PRAGMA_OACC_KERNELS:
38313 case PRAGMA_OACC_PARALLEL:
38314 strcpy (p_name, "#pragma acc");
38315 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38316 if_p);
38317 break;
38318 case PRAGMA_OACC_LOOP:
38319 strcpy (p_name, "#pragma acc");
38320 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38321 if_p);
38322 break;
38323 case PRAGMA_OACC_UPDATE:
38324 stmt = cp_parser_oacc_update (parser, pragma_tok);
38325 break;
38326 case PRAGMA_OACC_WAIT:
38327 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38328 break;
38329 case PRAGMA_OMP_ATOMIC:
38330 cp_parser_omp_atomic (parser, pragma_tok);
38331 return;
38332 case PRAGMA_OMP_CRITICAL:
38333 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38334 break;
38335 case PRAGMA_OMP_DISTRIBUTE:
38336 strcpy (p_name, "#pragma omp");
38337 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38338 if_p);
38339 break;
38340 case PRAGMA_OMP_FOR:
38341 strcpy (p_name, "#pragma omp");
38342 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38343 if_p);
38344 break;
38345 case PRAGMA_OMP_MASTER:
38346 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38347 break;
38348 case PRAGMA_OMP_PARALLEL:
38349 strcpy (p_name, "#pragma omp");
38350 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38351 if_p);
38352 break;
38353 case PRAGMA_OMP_SECTIONS:
38354 strcpy (p_name, "#pragma omp");
38355 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38356 break;
38357 case PRAGMA_OMP_SIMD:
38358 strcpy (p_name, "#pragma omp");
38359 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38360 if_p);
38361 break;
38362 case PRAGMA_OMP_SINGLE:
38363 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38364 break;
38365 case PRAGMA_OMP_TASK:
38366 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38367 break;
38368 case PRAGMA_OMP_TASKGROUP:
38369 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38370 break;
38371 case PRAGMA_OMP_TASKLOOP:
38372 strcpy (p_name, "#pragma omp");
38373 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38374 if_p);
38375 break;
38376 case PRAGMA_OMP_TEAMS:
38377 strcpy (p_name, "#pragma omp");
38378 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38379 if_p);
38380 break;
38381 default:
38382 gcc_unreachable ();
38385 protected_set_expr_location (stmt, pragma_tok->location);
38388 /* Transactional Memory parsing routines. */
38390 /* Parse a transaction attribute.
38392 txn-attribute:
38393 attribute
38394 [ [ identifier ] ]
38396 We use this instead of cp_parser_attributes_opt for transactions to avoid
38397 the pedwarn in C++98 mode. */
38399 static tree
38400 cp_parser_txn_attribute_opt (cp_parser *parser)
38402 cp_token *token;
38403 tree attr_name, attr = NULL;
38405 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38406 return cp_parser_attributes_opt (parser);
38408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38409 return NULL_TREE;
38410 cp_lexer_consume_token (parser->lexer);
38411 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38412 goto error1;
38414 token = cp_lexer_peek_token (parser->lexer);
38415 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38417 token = cp_lexer_consume_token (parser->lexer);
38419 attr_name = (token->type == CPP_KEYWORD
38420 /* For keywords, use the canonical spelling,
38421 not the parsed identifier. */
38422 ? ridpointers[(int) token->keyword]
38423 : token->u.value);
38424 attr = build_tree_list (attr_name, NULL_TREE);
38426 else
38427 cp_parser_error (parser, "expected identifier");
38429 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38430 error1:
38431 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38432 return attr;
38435 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38437 transaction-statement:
38438 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38439 compound-statement
38440 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38443 static tree
38444 cp_parser_transaction (cp_parser *parser, cp_token *token)
38446 unsigned char old_in = parser->in_transaction;
38447 unsigned char this_in = 1, new_in;
38448 enum rid keyword = token->keyword;
38449 tree stmt, attrs, noex;
38451 cp_lexer_consume_token (parser->lexer);
38453 if (keyword == RID_TRANSACTION_RELAXED
38454 || keyword == RID_SYNCHRONIZED)
38455 this_in |= TM_STMT_ATTR_RELAXED;
38456 else
38458 attrs = cp_parser_txn_attribute_opt (parser);
38459 if (attrs)
38460 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38463 /* Parse a noexcept specification. */
38464 if (keyword == RID_ATOMIC_NOEXCEPT)
38465 noex = boolean_true_node;
38466 else if (keyword == RID_ATOMIC_CANCEL)
38468 /* cancel-and-throw is unimplemented. */
38469 sorry ("atomic_cancel");
38470 noex = NULL_TREE;
38472 else
38473 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38475 /* Keep track if we're in the lexical scope of an outer transaction. */
38476 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38478 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38480 parser->in_transaction = new_in;
38481 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38482 parser->in_transaction = old_in;
38484 finish_transaction_stmt (stmt, NULL, this_in, noex);
38486 return stmt;
38489 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38491 transaction-expression:
38492 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38493 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38496 static tree
38497 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38499 unsigned char old_in = parser->in_transaction;
38500 unsigned char this_in = 1;
38501 cp_token *token;
38502 tree expr, noex;
38503 bool noex_expr;
38504 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38506 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38507 || keyword == RID_TRANSACTION_RELAXED);
38509 if (!flag_tm)
38510 error_at (loc,
38511 keyword == RID_TRANSACTION_RELAXED
38512 ? G_("%<__transaction_relaxed%> without transactional memory "
38513 "support enabled")
38514 : G_("%<__transaction_atomic%> without transactional memory "
38515 "support enabled"));
38517 token = cp_parser_require_keyword (parser, keyword,
38518 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38519 : RT_TRANSACTION_RELAXED));
38520 gcc_assert (token != NULL);
38522 if (keyword == RID_TRANSACTION_RELAXED)
38523 this_in |= TM_STMT_ATTR_RELAXED;
38525 /* Set this early. This might mean that we allow transaction_cancel in
38526 an expression that we find out later actually has to be a constexpr.
38527 However, we expect that cxx_constant_value will be able to deal with
38528 this; also, if the noexcept has no constexpr, then what we parse next
38529 really is a transaction's body. */
38530 parser->in_transaction = this_in;
38532 /* Parse a noexcept specification. */
38533 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38534 true);
38536 if (!noex || !noex_expr
38537 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38539 matching_parens parens;
38540 parens.require_open (parser);
38542 expr = cp_parser_expression (parser);
38543 expr = finish_parenthesized_expr (expr);
38545 parens.require_close (parser);
38547 else
38549 /* The only expression that is available got parsed for the noexcept
38550 already. noexcept is true then. */
38551 expr = noex;
38552 noex = boolean_true_node;
38555 expr = build_transaction_expr (token->location, expr, this_in, noex);
38556 parser->in_transaction = old_in;
38558 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38559 return error_mark_node;
38561 return (flag_tm ? expr : error_mark_node);
38564 /* Parse a function-transaction-block.
38566 function-transaction-block:
38567 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38568 function-body
38569 __transaction_atomic txn-attribute[opt] function-try-block
38570 __transaction_relaxed ctor-initializer[opt] function-body
38571 __transaction_relaxed function-try-block
38574 static void
38575 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38577 unsigned char old_in = parser->in_transaction;
38578 unsigned char new_in = 1;
38579 tree compound_stmt, stmt, attrs;
38580 cp_token *token;
38582 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38583 || keyword == RID_TRANSACTION_RELAXED);
38584 token = cp_parser_require_keyword (parser, keyword,
38585 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38586 : RT_TRANSACTION_RELAXED));
38587 gcc_assert (token != NULL);
38589 if (keyword == RID_TRANSACTION_RELAXED)
38590 new_in |= TM_STMT_ATTR_RELAXED;
38591 else
38593 attrs = cp_parser_txn_attribute_opt (parser);
38594 if (attrs)
38595 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38598 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38600 parser->in_transaction = new_in;
38602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38603 cp_parser_function_try_block (parser);
38604 else
38605 cp_parser_ctor_initializer_opt_and_function_body
38606 (parser, /*in_function_try_block=*/false);
38608 parser->in_transaction = old_in;
38610 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38613 /* Parse a __transaction_cancel statement.
38615 cancel-statement:
38616 __transaction_cancel txn-attribute[opt] ;
38617 __transaction_cancel txn-attribute[opt] throw-expression ;
38619 ??? Cancel and throw is not yet implemented. */
38621 static tree
38622 cp_parser_transaction_cancel (cp_parser *parser)
38624 cp_token *token;
38625 bool is_outer = false;
38626 tree stmt, attrs;
38628 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38629 RT_TRANSACTION_CANCEL);
38630 gcc_assert (token != NULL);
38632 attrs = cp_parser_txn_attribute_opt (parser);
38633 if (attrs)
38634 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38636 /* ??? Parse cancel-and-throw here. */
38638 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38640 if (!flag_tm)
38642 error_at (token->location, "%<__transaction_cancel%> without "
38643 "transactional memory support enabled");
38644 return error_mark_node;
38646 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38648 error_at (token->location, "%<__transaction_cancel%> within a "
38649 "%<__transaction_relaxed%>");
38650 return error_mark_node;
38652 else if (is_outer)
38654 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38655 && !is_tm_may_cancel_outer (current_function_decl))
38657 error_at (token->location, "outer %<__transaction_cancel%> not "
38658 "within outer %<__transaction_atomic%>");
38659 error_at (token->location,
38660 " or a %<transaction_may_cancel_outer%> function");
38661 return error_mark_node;
38664 else if (parser->in_transaction == 0)
38666 error_at (token->location, "%<__transaction_cancel%> not within "
38667 "%<__transaction_atomic%>");
38668 return error_mark_node;
38671 stmt = build_tm_abort_call (token->location, is_outer);
38672 add_stmt (stmt);
38674 return stmt;
38677 /* The parser. */
38679 static GTY (()) cp_parser *the_parser;
38682 /* Special handling for the first token or line in the file. The first
38683 thing in the file might be #pragma GCC pch_preprocess, which loads a
38684 PCH file, which is a GC collection point. So we need to handle this
38685 first pragma without benefit of an existing lexer structure.
38687 Always returns one token to the caller in *FIRST_TOKEN. This is
38688 either the true first token of the file, or the first token after
38689 the initial pragma. */
38691 static void
38692 cp_parser_initial_pragma (cp_token *first_token)
38694 tree name = NULL;
38696 cp_lexer_get_preprocessor_token (NULL, first_token);
38697 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38698 return;
38700 cp_lexer_get_preprocessor_token (NULL, first_token);
38701 if (first_token->type == CPP_STRING)
38703 name = first_token->u.value;
38705 cp_lexer_get_preprocessor_token (NULL, first_token);
38706 if (first_token->type != CPP_PRAGMA_EOL)
38707 error_at (first_token->location,
38708 "junk at end of %<#pragma GCC pch_preprocess%>");
38710 else
38711 error_at (first_token->location, "expected string literal");
38713 /* Skip to the end of the pragma. */
38714 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38715 cp_lexer_get_preprocessor_token (NULL, first_token);
38717 /* Now actually load the PCH file. */
38718 if (name)
38719 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38721 /* Read one more token to return to our caller. We have to do this
38722 after reading the PCH file in, since its pointers have to be
38723 live. */
38724 cp_lexer_get_preprocessor_token (NULL, first_token);
38727 /* Parse a pragma GCC ivdep. */
38729 static bool
38730 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38732 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38733 return true;
38736 /* Parse a pragma GCC unroll. */
38738 static unsigned short
38739 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38741 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38742 tree expr = cp_parser_constant_expression (parser);
38743 unsigned short unroll;
38744 expr = maybe_constant_value (expr);
38745 HOST_WIDE_INT lunroll = 0;
38746 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38747 || TREE_CODE (expr) != INTEGER_CST
38748 || (lunroll = tree_to_shwi (expr)) < 0
38749 || lunroll >= USHRT_MAX)
38751 error_at (location, "%<#pragma GCC unroll%> requires an"
38752 " assignment-expression that evaluates to a non-negative"
38753 " integral constant less than %u", USHRT_MAX);
38754 unroll = 0;
38756 else
38758 unroll = (unsigned short)lunroll;
38759 if (unroll == 0)
38760 unroll = 1;
38762 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38763 return unroll;
38766 /* Normal parsing of a pragma token. Here we can (and must) use the
38767 regular lexer. */
38769 static bool
38770 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38772 cp_token *pragma_tok;
38773 unsigned int id;
38774 tree stmt;
38775 bool ret;
38777 pragma_tok = cp_lexer_consume_token (parser->lexer);
38778 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38779 parser->lexer->in_pragma = true;
38781 id = cp_parser_pragma_kind (pragma_tok);
38782 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38783 cp_ensure_no_omp_declare_simd (parser);
38784 switch (id)
38786 case PRAGMA_GCC_PCH_PREPROCESS:
38787 error_at (pragma_tok->location,
38788 "%<#pragma GCC pch_preprocess%> must be first");
38789 break;
38791 case PRAGMA_OMP_BARRIER:
38792 switch (context)
38794 case pragma_compound:
38795 cp_parser_omp_barrier (parser, pragma_tok);
38796 return false;
38797 case pragma_stmt:
38798 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38799 "used in compound statements", "omp barrier");
38800 break;
38801 default:
38802 goto bad_stmt;
38804 break;
38806 case PRAGMA_OMP_FLUSH:
38807 switch (context)
38809 case pragma_compound:
38810 cp_parser_omp_flush (parser, pragma_tok);
38811 return false;
38812 case pragma_stmt:
38813 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38814 "used in compound statements", "omp flush");
38815 break;
38816 default:
38817 goto bad_stmt;
38819 break;
38821 case PRAGMA_OMP_TASKWAIT:
38822 switch (context)
38824 case pragma_compound:
38825 cp_parser_omp_taskwait (parser, pragma_tok);
38826 return false;
38827 case pragma_stmt:
38828 error_at (pragma_tok->location,
38829 "%<#pragma %s%> may only be used in compound statements",
38830 "omp taskwait");
38831 break;
38832 default:
38833 goto bad_stmt;
38835 break;
38837 case PRAGMA_OMP_TASKYIELD:
38838 switch (context)
38840 case pragma_compound:
38841 cp_parser_omp_taskyield (parser, pragma_tok);
38842 return false;
38843 case pragma_stmt:
38844 error_at (pragma_tok->location,
38845 "%<#pragma %s%> may only be used in compound statements",
38846 "omp taskyield");
38847 break;
38848 default:
38849 goto bad_stmt;
38851 break;
38853 case PRAGMA_OMP_CANCEL:
38854 switch (context)
38856 case pragma_compound:
38857 cp_parser_omp_cancel (parser, pragma_tok);
38858 return false;
38859 case pragma_stmt:
38860 error_at (pragma_tok->location,
38861 "%<#pragma %s%> may only be used in compound statements",
38862 "omp cancel");
38863 break;
38864 default:
38865 goto bad_stmt;
38867 break;
38869 case PRAGMA_OMP_CANCELLATION_POINT:
38870 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38871 return false;
38873 case PRAGMA_OMP_THREADPRIVATE:
38874 cp_parser_omp_threadprivate (parser, pragma_tok);
38875 return false;
38877 case PRAGMA_OMP_DECLARE:
38878 return cp_parser_omp_declare (parser, pragma_tok, context);
38880 case PRAGMA_OACC_DECLARE:
38881 cp_parser_oacc_declare (parser, pragma_tok);
38882 return false;
38884 case PRAGMA_OACC_ENTER_DATA:
38885 if (context == pragma_stmt)
38887 error_at (pragma_tok->location,
38888 "%<#pragma %s%> may only be used in compound statements",
38889 "acc enter data");
38890 break;
38892 else if (context != pragma_compound)
38893 goto bad_stmt;
38894 cp_parser_omp_construct (parser, pragma_tok, if_p);
38895 return true;
38897 case PRAGMA_OACC_EXIT_DATA:
38898 if (context == pragma_stmt)
38900 error_at (pragma_tok->location,
38901 "%<#pragma %s%> may only be used in compound statements",
38902 "acc exit data");
38903 break;
38905 else if (context != pragma_compound)
38906 goto bad_stmt;
38907 cp_parser_omp_construct (parser, pragma_tok, if_p);
38908 return true;
38910 case PRAGMA_OACC_ROUTINE:
38911 if (context != pragma_external)
38913 error_at (pragma_tok->location,
38914 "%<#pragma acc routine%> must be at file scope");
38915 break;
38917 cp_parser_oacc_routine (parser, pragma_tok, context);
38918 return false;
38920 case PRAGMA_OACC_UPDATE:
38921 if (context == pragma_stmt)
38923 error_at (pragma_tok->location,
38924 "%<#pragma %s%> may only be used in compound statements",
38925 "acc update");
38926 break;
38928 else if (context != pragma_compound)
38929 goto bad_stmt;
38930 cp_parser_omp_construct (parser, pragma_tok, if_p);
38931 return true;
38933 case PRAGMA_OACC_WAIT:
38934 if (context == pragma_stmt)
38936 error_at (pragma_tok->location,
38937 "%<#pragma %s%> may only be used in compound statements",
38938 "acc wait");
38939 break;
38941 else if (context != pragma_compound)
38942 goto bad_stmt;
38943 cp_parser_omp_construct (parser, pragma_tok, if_p);
38944 return true;
38946 case PRAGMA_OACC_ATOMIC:
38947 case PRAGMA_OACC_CACHE:
38948 case PRAGMA_OACC_DATA:
38949 case PRAGMA_OACC_HOST_DATA:
38950 case PRAGMA_OACC_KERNELS:
38951 case PRAGMA_OACC_PARALLEL:
38952 case PRAGMA_OACC_LOOP:
38953 case PRAGMA_OMP_ATOMIC:
38954 case PRAGMA_OMP_CRITICAL:
38955 case PRAGMA_OMP_DISTRIBUTE:
38956 case PRAGMA_OMP_FOR:
38957 case PRAGMA_OMP_MASTER:
38958 case PRAGMA_OMP_PARALLEL:
38959 case PRAGMA_OMP_SECTIONS:
38960 case PRAGMA_OMP_SIMD:
38961 case PRAGMA_OMP_SINGLE:
38962 case PRAGMA_OMP_TASK:
38963 case PRAGMA_OMP_TASKGROUP:
38964 case PRAGMA_OMP_TASKLOOP:
38965 case PRAGMA_OMP_TEAMS:
38966 if (context != pragma_stmt && context != pragma_compound)
38967 goto bad_stmt;
38968 stmt = push_omp_privatization_clauses (false);
38969 cp_parser_omp_construct (parser, pragma_tok, if_p);
38970 pop_omp_privatization_clauses (stmt);
38971 return true;
38973 case PRAGMA_OMP_ORDERED:
38974 if (context != pragma_stmt && context != pragma_compound)
38975 goto bad_stmt;
38976 stmt = push_omp_privatization_clauses (false);
38977 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38978 pop_omp_privatization_clauses (stmt);
38979 return ret;
38981 case PRAGMA_OMP_TARGET:
38982 if (context != pragma_stmt && context != pragma_compound)
38983 goto bad_stmt;
38984 stmt = push_omp_privatization_clauses (false);
38985 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38986 pop_omp_privatization_clauses (stmt);
38987 return ret;
38989 case PRAGMA_OMP_END_DECLARE_TARGET:
38990 cp_parser_omp_end_declare_target (parser, pragma_tok);
38991 return false;
38993 case PRAGMA_OMP_SECTION:
38994 error_at (pragma_tok->location,
38995 "%<#pragma omp section%> may only be used in "
38996 "%<#pragma omp sections%> construct");
38997 break;
38999 case PRAGMA_IVDEP:
39001 if (context == pragma_external)
39003 error_at (pragma_tok->location,
39004 "%<#pragma GCC ivdep%> must be inside a function");
39005 break;
39007 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39008 unsigned short unroll;
39009 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39010 if (tok->type == CPP_PRAGMA
39011 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39013 tok = cp_lexer_consume_token (parser->lexer);
39014 unroll = cp_parser_pragma_unroll (parser, tok);
39015 tok = cp_lexer_peek_token (the_parser->lexer);
39017 else
39018 unroll = 0;
39019 if (tok->type != CPP_KEYWORD
39020 || (tok->keyword != RID_FOR
39021 && tok->keyword != RID_WHILE
39022 && tok->keyword != RID_DO))
39024 cp_parser_error (parser, "for, while or do statement expected");
39025 return false;
39027 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39028 return true;
39031 case PRAGMA_UNROLL:
39033 if (context == pragma_external)
39035 error_at (pragma_tok->location,
39036 "%<#pragma GCC unroll%> must be inside a function");
39037 break;
39039 const unsigned short unroll
39040 = cp_parser_pragma_unroll (parser, pragma_tok);
39041 bool ivdep;
39042 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39043 if (tok->type == CPP_PRAGMA
39044 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39046 tok = cp_lexer_consume_token (parser->lexer);
39047 ivdep = cp_parser_pragma_ivdep (parser, tok);
39048 tok = cp_lexer_peek_token (the_parser->lexer);
39050 else
39051 ivdep = false;
39052 if (tok->type != CPP_KEYWORD
39053 || (tok->keyword != RID_FOR
39054 && tok->keyword != RID_WHILE
39055 && tok->keyword != RID_DO))
39057 cp_parser_error (parser, "for, while or do statement expected");
39058 return false;
39060 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39061 return true;
39064 default:
39065 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39066 c_invoke_pragma_handler (id);
39067 break;
39069 bad_stmt:
39070 cp_parser_error (parser, "expected declaration specifiers");
39071 break;
39074 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39075 return false;
39078 /* The interface the pragma parsers have to the lexer. */
39080 enum cpp_ttype
39081 pragma_lex (tree *value, location_t *loc)
39083 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39084 enum cpp_ttype ret = tok->type;
39086 *value = tok->u.value;
39087 if (loc)
39088 *loc = tok->location;
39090 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39091 ret = CPP_EOF;
39092 else if (ret == CPP_STRING)
39093 *value = cp_parser_string_literal (the_parser, false, false);
39094 else
39096 if (ret == CPP_KEYWORD)
39097 ret = CPP_NAME;
39098 cp_lexer_consume_token (the_parser->lexer);
39101 return ret;
39105 /* External interface. */
39107 /* Parse one entire translation unit. */
39109 void
39110 c_parse_file (void)
39112 static bool already_called = false;
39114 if (already_called)
39115 fatal_error (input_location,
39116 "inter-module optimizations not implemented for C++");
39117 already_called = true;
39119 the_parser = cp_parser_new ();
39120 push_deferring_access_checks (flag_access_control
39121 ? dk_no_deferred : dk_no_check);
39122 cp_parser_translation_unit (the_parser);
39123 the_parser = NULL;
39126 /* Create an identifier for a generic parameter type (a synthesized
39127 template parameter implied by `auto' or a concept identifier). */
39129 static GTY(()) int generic_parm_count;
39130 static tree
39131 make_generic_type_name ()
39133 char buf[32];
39134 sprintf (buf, "auto:%d", ++generic_parm_count);
39135 return get_identifier (buf);
39138 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39139 (creating a new template parameter list if necessary). Returns the newly
39140 created template type parm. */
39142 static tree
39143 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39145 gcc_assert (current_binding_level->kind == sk_function_parms);
39147 /* Before committing to modifying any scope, if we're in an
39148 implicit template scope, and we're trying to synthesize a
39149 constrained parameter, try to find a previous parameter with
39150 the same name. This is the same-type rule for abbreviated
39151 function templates.
39153 NOTE: We can generate implicit parameters when tentatively
39154 parsing a nested name specifier, only to reject that parse
39155 later. However, matching the same template-id as part of a
39156 direct-declarator should generate an identical template
39157 parameter, so this rule will merge them. */
39158 if (parser->implicit_template_scope && constr)
39160 tree t = parser->implicit_template_parms;
39161 while (t)
39163 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39165 tree d = TREE_VALUE (t);
39166 if (TREE_CODE (d) == PARM_DECL)
39167 /* Return the TEMPLATE_PARM_INDEX. */
39168 d = DECL_INITIAL (d);
39169 return d;
39171 t = TREE_CHAIN (t);
39175 /* We are either continuing a function template that already contains implicit
39176 template parameters, creating a new fully-implicit function template, or
39177 extending an existing explicit function template with implicit template
39178 parameters. */
39180 cp_binding_level *const entry_scope = current_binding_level;
39182 bool become_template = false;
39183 cp_binding_level *parent_scope = 0;
39185 if (parser->implicit_template_scope)
39187 gcc_assert (parser->implicit_template_parms);
39189 current_binding_level = parser->implicit_template_scope;
39191 else
39193 /* Roll back to the existing template parameter scope (in the case of
39194 extending an explicit function template) or introduce a new template
39195 parameter scope ahead of the function parameter scope (or class scope
39196 in the case of out-of-line member definitions). The function scope is
39197 added back after template parameter synthesis below. */
39199 cp_binding_level *scope = entry_scope;
39201 while (scope->kind == sk_function_parms)
39203 parent_scope = scope;
39204 scope = scope->level_chain;
39206 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39208 /* If not defining a class, then any class scope is a scope level in
39209 an out-of-line member definition. In this case simply wind back
39210 beyond the first such scope to inject the template parameter list.
39211 Otherwise wind back to the class being defined. The latter can
39212 occur in class member friend declarations such as:
39214 class A {
39215 void foo (auto);
39217 class B {
39218 friend void A::foo (auto);
39221 The template parameter list synthesized for the friend declaration
39222 must be injected in the scope of 'B'. This can also occur in
39223 erroneous cases such as:
39225 struct A {
39226 struct B {
39227 void foo (auto);
39229 void B::foo (auto) {}
39232 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39233 but, nevertheless, the template parameter list synthesized for the
39234 declarator should be injected into the scope of 'A' as if the
39235 ill-formed template was specified explicitly. */
39237 while (scope->kind == sk_class && !scope->defining_class_p)
39239 parent_scope = scope;
39240 scope = scope->level_chain;
39244 current_binding_level = scope;
39246 if (scope->kind != sk_template_parms
39247 || !function_being_declared_is_template_p (parser))
39249 /* Introduce a new template parameter list for implicit template
39250 parameters. */
39252 become_template = true;
39254 parser->implicit_template_scope
39255 = begin_scope (sk_template_parms, NULL);
39257 ++processing_template_decl;
39259 parser->fully_implicit_function_template_p = true;
39260 ++parser->num_template_parameter_lists;
39262 else
39264 /* Synthesize implicit template parameters at the end of the explicit
39265 template parameter list. */
39267 gcc_assert (current_template_parms);
39269 parser->implicit_template_scope = scope;
39271 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39272 parser->implicit_template_parms
39273 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39277 /* Synthesize a new template parameter and track the current template
39278 parameter chain with implicit_template_parms. */
39280 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39281 tree synth_id = make_generic_type_name ();
39282 tree synth_tmpl_parm;
39283 bool non_type = false;
39285 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39286 synth_tmpl_parm
39287 = finish_template_type_parm (class_type_node, synth_id);
39288 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39289 synth_tmpl_parm
39290 = finish_constrained_template_template_parm (proto, synth_id);
39291 else
39293 synth_tmpl_parm = copy_decl (proto);
39294 DECL_NAME (synth_tmpl_parm) = synth_id;
39295 non_type = true;
39298 // Attach the constraint to the parm before processing.
39299 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39300 TREE_TYPE (node) = constr;
39301 tree new_parm
39302 = process_template_parm (parser->implicit_template_parms,
39303 input_location,
39304 node,
39305 /*non_type=*/non_type,
39306 /*param_pack=*/false);
39308 // Chain the new parameter to the list of implicit parameters.
39309 if (parser->implicit_template_parms)
39310 parser->implicit_template_parms
39311 = TREE_CHAIN (parser->implicit_template_parms);
39312 else
39313 parser->implicit_template_parms = new_parm;
39315 tree new_decl = get_local_decls ();
39316 if (non_type)
39317 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39318 new_decl = DECL_INITIAL (new_decl);
39320 /* If creating a fully implicit function template, start the new implicit
39321 template parameter list with this synthesized type, otherwise grow the
39322 current template parameter list. */
39324 if (become_template)
39326 parent_scope->level_chain = current_binding_level;
39328 tree new_parms = make_tree_vec (1);
39329 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39330 current_template_parms = tree_cons (size_int (processing_template_decl),
39331 new_parms, current_template_parms);
39333 else
39335 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39336 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39337 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39338 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39341 // If the new parameter was constrained, we need to add that to the
39342 // constraints in the template parameter list.
39343 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39345 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39346 reqs = conjoin_constraints (reqs, req);
39347 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39350 current_binding_level = entry_scope;
39352 return new_decl;
39355 /* Finish the declaration of a fully implicit function template. Such a
39356 template has no explicit template parameter list so has not been through the
39357 normal template head and tail processing. synthesize_implicit_template_parm
39358 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39359 provided if the declaration is a class member such that its template
39360 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39361 form is returned. Otherwise NULL_TREE is returned. */
39363 static tree
39364 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39366 gcc_assert (parser->fully_implicit_function_template_p);
39368 if (member_decl_opt && member_decl_opt != error_mark_node
39369 && DECL_VIRTUAL_P (member_decl_opt))
39371 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39372 "implicit templates may not be %<virtual%>");
39373 DECL_VIRTUAL_P (member_decl_opt) = false;
39376 if (member_decl_opt)
39377 member_decl_opt = finish_member_template_decl (member_decl_opt);
39378 end_template_decl ();
39380 parser->fully_implicit_function_template_p = false;
39381 parser->implicit_template_parms = 0;
39382 parser->implicit_template_scope = 0;
39383 --parser->num_template_parameter_lists;
39385 return member_decl_opt;
39388 /* Like finish_fully_implicit_template, but to be used in error
39389 recovery, rearranging scopes so that we restore the state we had
39390 before synthesize_implicit_template_parm inserted the implement
39391 template parms scope. */
39393 static void
39394 abort_fully_implicit_template (cp_parser *parser)
39396 cp_binding_level *return_to_scope = current_binding_level;
39398 if (parser->implicit_template_scope
39399 && return_to_scope != parser->implicit_template_scope)
39401 cp_binding_level *child = return_to_scope;
39402 for (cp_binding_level *scope = child->level_chain;
39403 scope != parser->implicit_template_scope;
39404 scope = child->level_chain)
39405 child = scope;
39406 child->level_chain = parser->implicit_template_scope->level_chain;
39407 parser->implicit_template_scope->level_chain = return_to_scope;
39408 current_binding_level = parser->implicit_template_scope;
39410 else
39411 return_to_scope = return_to_scope->level_chain;
39413 finish_fully_implicit_template (parser, NULL);
39415 gcc_assert (current_binding_level == return_to_scope);
39418 /* Helper function for diagnostics that have complained about things
39419 being used with 'extern "C"' linkage.
39421 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39423 void
39424 maybe_show_extern_c_location (void)
39426 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39427 inform (the_parser->innermost_linkage_specification_location,
39428 "%<extern \"C\"%> linkage started here");
39431 #include "gt-cp-parser.h"