re PR target/85056 ([nvptx] wrong declaration of external arrays)
[official-gcc.git] / gcc / cp / parser.c
blobe946d0b722926891a80306a00a5efbcf41acb854
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
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, 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 return;
2869 gcc_rich_location richloc (input_location);
2871 bool added_matching_location = false;
2873 if (missing_token_desc != RT_NONE)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser->lexer)->location;
2881 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location != UNKNOWN_LOCATION)
2887 added_matching_location
2888 = richloc.add_location_if_nearby (matching_location);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2895 identifiers. */
2896 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2897 token->u.value, token->flags, &richloc);
2899 if (missing_token_desc != RT_NONE)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location != UNKNOWN_LOCATION
2904 && !added_matching_location)
2905 inform (matching_location, "to match this %qs",
2906 get_matching_symbol (missing_token_desc));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2914 OTHER-TOKEN". */
2916 static void
2917 cp_parser_error (cp_parser* parser, const char* gmsgid)
2919 if (!cp_parser_simulate_error (parser))
2920 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2928 static void
2929 cp_parser_name_lookup_error (cp_parser* parser,
2930 tree name,
2931 tree decl,
2932 name_lookup_error desired,
2933 location_t location)
2935 /* If name lookup completely failed, tell the user that NAME was not
2936 declared. */
2937 if (decl == error_mark_node)
2939 if (parser->scope && parser->scope != global_namespace)
2940 error_at (location, "%<%E::%E%> has not been declared",
2941 parser->scope, name);
2942 else if (parser->scope == global_namespace)
2943 error_at (location, "%<::%E%> has not been declared", name);
2944 else if (parser->object_scope
2945 && !CLASS_TYPE_P (parser->object_scope))
2946 error_at (location, "request for member %qE in non-class type %qT",
2947 name, parser->object_scope);
2948 else if (parser->object_scope)
2949 error_at (location, "%<%T::%E%> has not been declared",
2950 parser->object_scope, name);
2951 else
2952 error_at (location, "%qE has not been declared", name);
2954 else if (parser->scope && parser->scope != global_namespace)
2956 switch (desired)
2958 case NLE_TYPE:
2959 error_at (location, "%<%E::%E%> is not a type",
2960 parser->scope, name);
2961 break;
2962 case NLE_CXX98:
2963 error_at (location, "%<%E::%E%> is not a class or namespace",
2964 parser->scope, name);
2965 break;
2966 case NLE_NOT_CXX98:
2967 error_at (location,
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser->scope, name);
2970 break;
2971 default:
2972 gcc_unreachable ();
2976 else if (parser->scope == global_namespace)
2978 switch (desired)
2980 case NLE_TYPE:
2981 error_at (location, "%<::%E%> is not a type", name);
2982 break;
2983 case NLE_CXX98:
2984 error_at (location, "%<::%E%> is not a class or namespace", name);
2985 break;
2986 case NLE_NOT_CXX98:
2987 error_at (location,
2988 "%<::%E%> is not a class, namespace, or enumeration",
2989 name);
2990 break;
2991 default:
2992 gcc_unreachable ();
2995 else
2997 switch (desired)
2999 case NLE_TYPE:
3000 error_at (location, "%qE is not a type", name);
3001 break;
3002 case NLE_CXX98:
3003 error_at (location, "%qE is not a class or namespace", name);
3004 break;
3005 case NLE_NOT_CXX98:
3006 error_at (location,
3007 "%qE is not a class, namespace, or enumeration", name);
3008 break;
3009 default:
3010 gcc_unreachable ();
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3019 static bool
3020 cp_parser_simulate_error (cp_parser* parser)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3024 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3025 return true;
3027 return false;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3032 issued. */
3034 static bool
3035 cp_parser_check_type_definition (cp_parser* parser)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser->type_definition_forbidden_message)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser->type_definition_forbidden_message);
3043 return false;
3045 return true;
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3054 static void
3055 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3056 tree type, location_t type_location)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3061 while (declarator
3062 && (declarator->kind == cdk_pointer
3063 || declarator->kind == cdk_reference
3064 || declarator->kind == cdk_ptrmem))
3065 declarator = declarator->declarator;
3066 if (declarator
3067 && declarator->kind == cdk_function)
3069 error_at (type_location,
3070 "new types may not be defined in a return type");
3071 inform (type_location,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3073 type);
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3083 static void
3084 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3085 tree type,
3086 enum tag_types tag_type,
3087 location_t location)
3089 cp_token_position start = 0;
3091 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3093 if (TREE_CODE (type) == TYPE_DECL)
3094 type = TREE_TYPE (type);
3095 if (TYPE_P (type) && !template_placeholder_p (type))
3096 error_at (location, "%qT is not a template", type);
3097 else if (identifier_p (type))
3099 if (tag_type != none_type)
3100 error_at (location, "%qE is not a class template", type);
3101 else
3102 error_at (location, "%qE is not a template", type);
3104 else
3105 error_at (location, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3108 start = cp_lexer_token_position (parser->lexer, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser->lexer);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3115 if (start)
3116 cp_lexer_purge_tokens_after (parser->lexer, start);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3125 static bool
3126 cp_parser_non_integral_constant_expression (cp_parser *parser,
3127 non_integral_constant thing)
3129 parser->non_integral_constant_expression_p = true;
3130 if (parser->integral_constant_expression_p)
3132 if (!parser->allow_non_integral_constant_expression_p)
3134 const char *msg = NULL;
3135 switch (thing)
3137 case NIC_FLOAT:
3138 pedwarn (input_location, OPT_Wpedantic,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3141 return true;
3142 case NIC_CAST:
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3146 return true;
3147 case NIC_TYPEID:
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3150 return true;
3151 case NIC_NCC:
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3154 return true;
3155 case NIC_FUNC_CALL:
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3158 return true;
3159 case NIC_INC:
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3162 return true;
3163 case NIC_DEC:
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_ARRAY_REF:
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_ADDR_LABEL:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_OVERLOADED:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_ASSIGNMENT:
3180 error ("an assignment cannot appear in a constant-expression");
3181 return true;
3182 case NIC_COMMA:
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_CONSTRUCTOR:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_TRANSACTION:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3193 return true;
3194 case NIC_THIS:
3195 msg = "this";
3196 break;
3197 case NIC_FUNC_NAME:
3198 msg = "__FUNCTION__";
3199 break;
3200 case NIC_PRETTY_FUNC:
3201 msg = "__PRETTY_FUNCTION__";
3202 break;
3203 case NIC_C99_FUNC:
3204 msg = "__func__";
3205 break;
3206 case NIC_VA_ARG:
3207 msg = "va_arg";
3208 break;
3209 case NIC_ARROW:
3210 msg = "->";
3211 break;
3212 case NIC_POINT:
3213 msg = ".";
3214 break;
3215 case NIC_STAR:
3216 msg = "*";
3217 break;
3218 case NIC_ADDR:
3219 msg = "&";
3220 break;
3221 case NIC_PREINCREMENT:
3222 msg = "++";
3223 break;
3224 case NIC_PREDECREMENT:
3225 msg = "--";
3226 break;
3227 case NIC_NEW:
3228 msg = "new";
3229 break;
3230 case NIC_DEL:
3231 msg = "delete";
3232 break;
3233 default:
3234 gcc_unreachable ();
3236 if (msg)
3237 error ("%qs cannot appear in a constant-expression", msg);
3238 return true;
3241 return false;
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3249 static void
3250 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3251 location_t location)
3253 tree decl, ambiguous_decls;
3254 cp_parser_commit_to_tentative_parse (parser);
3255 /* Try to lookup the identifier. */
3256 decl = cp_parser_lookup_name (parser, id, none_type,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls, location);
3261 if (ambiguous_decls)
3262 /* If the lookup was ambiguous, an error will already have
3263 been issued. */
3264 return;
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl))
3269 error_at (location,
3270 "invalid use of template-name %qE without an argument list",
3271 decl);
3272 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3273 inform (location, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3277 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3278 error_at (location, "invalid use of destructor %qD as a type", id);
3279 else if (TREE_CODE (decl) == TYPE_DECL)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location, "invalid combination of multiple type-specifiers");
3282 else if (!parser->scope)
3284 /* Issue an error message. */
3285 name_hint hint;
3286 if (TREE_CODE (id) == IDENTIFIER_NODE)
3287 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3288 if (hint)
3290 gcc_rich_location richloc (location);
3291 richloc.add_fixit_replace (hint.suggestion ());
3292 error_at (&richloc,
3293 "%qE does not name a type; did you mean %qs?",
3294 id, hint.suggestion ());
3296 else
3297 error_at (location, "%qE does not name a type", id);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3306 inform (location, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3309 inform (location, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect < cxx11
3312 && TREE_CODE (id) == IDENTIFIER_NODE
3313 && id_equal (id, "thread_local"))
3314 inform (location, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3317 inform (location, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl && current_class_type
3319 && TYPE_BINFO (current_class_type))
3321 tree b;
3323 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3325 b = TREE_CHAIN (b))
3327 tree base_type = BINFO_TYPE (b);
3328 if (CLASS_TYPE_P (base_type)
3329 && dependent_type_p (base_type))
3331 tree field;
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3336 for (field = TYPE_FIELDS (base_type);
3337 field;
3338 field = DECL_CHAIN (field))
3339 if (TREE_CODE (field) == TYPE_DECL
3340 && DECL_NAME (field) == id)
3342 inform (location,
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b), id);
3345 break;
3347 if (field)
3348 break;
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser->scope != error_mark_node)
3357 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3359 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3360 error_at (location_of (id),
3361 "%qE in namespace %qE does not name a template type",
3362 id, parser->scope);
3363 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3364 error_at (location_of (id),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id, 0), parser->scope);
3367 else
3368 error_at (location_of (id),
3369 "%qE in namespace %qE does not name a type",
3370 id, parser->scope);
3371 if (DECL_P (decl))
3372 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3373 else if (decl == error_mark_node)
3374 suggest_alternative_in_explicit_scope (location, id,
3375 parser->scope);
3377 else if (CLASS_TYPE_P (parser->scope)
3378 && constructor_name_p (id, parser->scope))
3380 /* A<T>::A<T>() */
3381 error_at (location, "%<%T::%E%> names the constructor, not"
3382 " the type", parser->scope, id);
3383 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3384 error_at (location, "and %qT has no template constructors",
3385 parser->scope);
3387 else if (TYPE_P (parser->scope)
3388 && dependent_scope_p (parser->scope))
3390 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3391 error_at (location,
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser->scope),
3395 TYPENAME_TYPE_FULLNAME (parser->scope),
3397 TYPE_CONTEXT (parser->scope),
3398 TYPENAME_TYPE_FULLNAME (parser->scope));
3399 else
3400 error_at (location, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser->scope, id, parser->scope);
3404 else if (TYPE_P (parser->scope))
3406 if (!COMPLETE_TYPE_P (parser->scope))
3407 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3408 parser->scope);
3409 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a template type",
3412 id, parser->scope);
3413 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id, 0), parser->scope);
3417 else
3418 error_at (location_of (id),
3419 "%qE in %q#T does not name a type",
3420 id, parser->scope);
3421 if (DECL_P (decl))
3422 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3424 else
3425 gcc_unreachable ();
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3439 static bool
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3442 tree id;
3443 cp_token *token = cp_lexer_peek_token (parser->lexer);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3448 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3449 if (next->type == CPP_NAME && next->error_reported)
3450 goto out;
3453 cp_parser_parse_tentatively (parser);
3454 id = cp_parser_id_expression (parser,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3463 || TREE_CODE (id) == TYPE_DECL)
3465 cp_parser_abort_tentative_parse (parser);
3466 return false;
3468 if (!cp_parser_parse_definitely (parser))
3469 return false;
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3473 out:
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser->in_declarator_p)
3478 cp_parser_skip_to_end_of_block_or_statement (parser);
3479 return true;
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3487 static int
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3489 bool recovering,
3490 cpp_ttype or_ttype,
3491 bool consume_paren)
3493 unsigned paren_depth = 0;
3494 unsigned brace_depth = 0;
3495 unsigned square_depth = 0;
3497 if (recovering && or_ttype == CPP_EOF
3498 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3499 return 0;
3501 while (true)
3503 cp_token * token = cp_lexer_peek_token (parser->lexer);
3505 /* Have we found what we're looking for before the closing paren? */
3506 if (token->type == or_ttype && or_ttype != CPP_EOF
3507 && !brace_depth && !paren_depth && !square_depth)
3508 return -1;
3510 switch (token->type)
3512 case CPP_EOF:
3513 case CPP_PRAGMA_EOL:
3514 /* If we've run out of tokens, then there is no closing `)'. */
3515 return 0;
3517 /* This is good for lambda expression capture-lists. */
3518 case CPP_OPEN_SQUARE:
3519 ++square_depth;
3520 break;
3521 case CPP_CLOSE_SQUARE:
3522 if (!square_depth--)
3523 return 0;
3524 break;
3526 case CPP_SEMICOLON:
3527 /* This matches the processing in skip_to_end_of_statement. */
3528 if (!brace_depth)
3529 return 0;
3530 break;
3532 case CPP_OPEN_BRACE:
3533 ++brace_depth;
3534 break;
3535 case CPP_CLOSE_BRACE:
3536 if (!brace_depth--)
3537 return 0;
3538 break;
3540 case CPP_OPEN_PAREN:
3541 if (!brace_depth)
3542 ++paren_depth;
3543 break;
3545 case CPP_CLOSE_PAREN:
3546 if (!brace_depth && !paren_depth--)
3548 if (consume_paren)
3549 cp_lexer_consume_token (parser->lexer);
3550 return 1;
3552 break;
3554 default:
3555 break;
3558 /* Consume the token. */
3559 cp_lexer_consume_token (parser->lexer);
3563 /* Consume tokens up to, and including, the next non-nested closing `)'.
3564 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3565 are doing error recovery. Returns -1 if OR_COMMA is true and we
3566 found an unnested token of that type. */
3568 static int
3569 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3570 bool recovering,
3571 bool or_comma,
3572 bool consume_paren)
3574 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3575 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3576 ttype, consume_paren);
3579 /* Consume tokens until we reach the end of the current statement.
3580 Normally, that will be just before consuming a `;'. However, if a
3581 non-nested `}' comes first, then we stop before consuming that. */
3583 static void
3584 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3586 unsigned nesting_depth = 0;
3588 /* Unwind generic function template scope if necessary. */
3589 if (parser->fully_implicit_function_template_p)
3590 abort_fully_implicit_template (parser);
3592 while (true)
3594 cp_token *token = cp_lexer_peek_token (parser->lexer);
3596 switch (token->type)
3598 case CPP_EOF:
3599 case CPP_PRAGMA_EOL:
3600 /* If we've run out of tokens, stop. */
3601 return;
3603 case CPP_SEMICOLON:
3604 /* If the next token is a `;', we have reached the end of the
3605 statement. */
3606 if (!nesting_depth)
3607 return;
3608 break;
3610 case CPP_CLOSE_BRACE:
3611 /* If this is a non-nested '}', stop before consuming it.
3612 That way, when confronted with something like:
3614 { 3 + }
3616 we stop before consuming the closing '}', even though we
3617 have not yet reached a `;'. */
3618 if (nesting_depth == 0)
3619 return;
3621 /* If it is the closing '}' for a block that we have
3622 scanned, stop -- but only after consuming the token.
3623 That way given:
3625 void f g () { ... }
3626 typedef int I;
3628 we will stop after the body of the erroneously declared
3629 function, but before consuming the following `typedef'
3630 declaration. */
3631 if (--nesting_depth == 0)
3633 cp_lexer_consume_token (parser->lexer);
3634 return;
3636 break;
3638 case CPP_OPEN_BRACE:
3639 ++nesting_depth;
3640 break;
3642 default:
3643 break;
3646 /* Consume the token. */
3647 cp_lexer_consume_token (parser->lexer);
3651 /* This function is called at the end of a statement or declaration.
3652 If the next token is a semicolon, it is consumed; otherwise, error
3653 recovery is attempted. */
3655 static void
3656 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3658 /* Look for the trailing `;'. */
3659 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3661 /* If there is additional (erroneous) input, skip to the end of
3662 the statement. */
3663 cp_parser_skip_to_end_of_statement (parser);
3664 /* If the next token is now a `;', consume it. */
3665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3666 cp_lexer_consume_token (parser->lexer);
3670 /* Skip tokens until we have consumed an entire block, or until we
3671 have consumed a non-nested `;'. */
3673 static void
3674 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3676 int nesting_depth = 0;
3678 /* Unwind generic function template scope if necessary. */
3679 if (parser->fully_implicit_function_template_p)
3680 abort_fully_implicit_template (parser);
3682 while (nesting_depth >= 0)
3684 cp_token *token = cp_lexer_peek_token (parser->lexer);
3686 switch (token->type)
3688 case CPP_EOF:
3689 case CPP_PRAGMA_EOL:
3690 /* If we've run out of tokens, stop. */
3691 return;
3693 case CPP_SEMICOLON:
3694 /* Stop if this is an unnested ';'. */
3695 if (!nesting_depth)
3696 nesting_depth = -1;
3697 break;
3699 case CPP_CLOSE_BRACE:
3700 /* Stop if this is an unnested '}', or closes the outermost
3701 nesting level. */
3702 nesting_depth--;
3703 if (nesting_depth < 0)
3704 return;
3705 if (!nesting_depth)
3706 nesting_depth = -1;
3707 break;
3709 case CPP_OPEN_BRACE:
3710 /* Nest. */
3711 nesting_depth++;
3712 break;
3714 default:
3715 break;
3718 /* Consume the token. */
3719 cp_lexer_consume_token (parser->lexer);
3723 /* Skip tokens until a non-nested closing curly brace is the next
3724 token, or there are no more tokens. Return true in the first case,
3725 false otherwise. */
3727 static bool
3728 cp_parser_skip_to_closing_brace (cp_parser *parser)
3730 unsigned nesting_depth = 0;
3732 while (true)
3734 cp_token *token = cp_lexer_peek_token (parser->lexer);
3736 switch (token->type)
3738 case CPP_EOF:
3739 case CPP_PRAGMA_EOL:
3740 /* If we've run out of tokens, stop. */
3741 return false;
3743 case CPP_CLOSE_BRACE:
3744 /* If the next token is a non-nested `}', then we have reached
3745 the end of the current block. */
3746 if (nesting_depth-- == 0)
3747 return true;
3748 break;
3750 case CPP_OPEN_BRACE:
3751 /* If it the next token is a `{', then we are entering a new
3752 block. Consume the entire block. */
3753 ++nesting_depth;
3754 break;
3756 default:
3757 break;
3760 /* Consume the token. */
3761 cp_lexer_consume_token (parser->lexer);
3765 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3766 parameter is the PRAGMA token, allowing us to purge the entire pragma
3767 sequence. */
3769 static void
3770 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3772 cp_token *token;
3774 parser->lexer->in_pragma = false;
3777 token = cp_lexer_consume_token (parser->lexer);
3778 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3780 /* Ensure that the pragma is not parsed again. */
3781 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3784 /* Require pragma end of line, resyncing with it as necessary. The
3785 arguments are as for cp_parser_skip_to_pragma_eol. */
3787 static void
3788 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3790 parser->lexer->in_pragma = false;
3791 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3792 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3795 /* This is a simple wrapper around make_typename_type. When the id is
3796 an unresolved identifier node, we can provide a superior diagnostic
3797 using cp_parser_diagnose_invalid_type_name. */
3799 static tree
3800 cp_parser_make_typename_type (cp_parser *parser, tree id,
3801 location_t id_location)
3803 tree result;
3804 if (identifier_p (id))
3806 result = make_typename_type (parser->scope, id, typename_type,
3807 /*complain=*/tf_none);
3808 if (result == error_mark_node)
3809 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3810 return result;
3812 return make_typename_type (parser->scope, id, typename_type, tf_error);
3815 /* This is a wrapper around the
3816 make_{pointer,ptrmem,reference}_declarator functions that decides
3817 which one to call based on the CODE and CLASS_TYPE arguments. The
3818 CODE argument should be one of the values returned by
3819 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3820 appertain to the pointer or reference. */
3822 static cp_declarator *
3823 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3824 cp_cv_quals cv_qualifiers,
3825 cp_declarator *target,
3826 tree attributes)
3828 if (code == ERROR_MARK || target == cp_error_declarator)
3829 return cp_error_declarator;
3831 if (code == INDIRECT_REF)
3832 if (class_type == NULL_TREE)
3833 return make_pointer_declarator (cv_qualifiers, target, attributes);
3834 else
3835 return make_ptrmem_declarator (cv_qualifiers, class_type,
3836 target, attributes);
3837 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3838 return make_reference_declarator (cv_qualifiers, target,
3839 false, attributes);
3840 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3841 return make_reference_declarator (cv_qualifiers, target,
3842 true, attributes);
3843 gcc_unreachable ();
3846 /* Create a new C++ parser. */
3848 static cp_parser *
3849 cp_parser_new (void)
3851 cp_parser *parser;
3852 cp_lexer *lexer;
3853 unsigned i;
3855 /* cp_lexer_new_main is called before doing GC allocation because
3856 cp_lexer_new_main might load a PCH file. */
3857 lexer = cp_lexer_new_main ();
3859 /* Initialize the binops_by_token so that we can get the tree
3860 directly from the token. */
3861 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3862 binops_by_token[binops[i].token_type] = binops[i];
3864 parser = ggc_cleared_alloc<cp_parser> ();
3865 parser->lexer = lexer;
3866 parser->context = cp_parser_context_new (NULL);
3868 /* For now, we always accept GNU extensions. */
3869 parser->allow_gnu_extensions_p = 1;
3871 /* The `>' token is a greater-than operator, not the end of a
3872 template-id. */
3873 parser->greater_than_is_operator_p = true;
3875 parser->default_arg_ok_p = true;
3877 /* We are not parsing a constant-expression. */
3878 parser->integral_constant_expression_p = false;
3879 parser->allow_non_integral_constant_expression_p = false;
3880 parser->non_integral_constant_expression_p = false;
3882 /* Local variable names are not forbidden. */
3883 parser->local_variables_forbidden_p = false;
3885 /* We are not processing an `extern "C"' declaration. */
3886 parser->in_unbraced_linkage_specification_p = false;
3888 /* We are not processing a declarator. */
3889 parser->in_declarator_p = false;
3891 /* We are not processing a template-argument-list. */
3892 parser->in_template_argument_list_p = false;
3894 /* We are not in an iteration statement. */
3895 parser->in_statement = 0;
3897 /* We are not in a switch statement. */
3898 parser->in_switch_statement_p = false;
3900 /* We are not parsing a type-id inside an expression. */
3901 parser->in_type_id_in_expr_p = false;
3903 /* Declarations aren't implicitly extern "C". */
3904 parser->implicit_extern_c = false;
3906 /* String literals should be translated to the execution character set. */
3907 parser->translate_strings_p = true;
3909 /* We are not parsing a function body. */
3910 parser->in_function_body = false;
3912 /* We can correct until told otherwise. */
3913 parser->colon_corrects_to_scope_p = true;
3915 /* The unparsed function queue is empty. */
3916 push_unparsed_function_queues (parser);
3918 /* There are no classes being defined. */
3919 parser->num_classes_being_defined = 0;
3921 /* No template parameters apply. */
3922 parser->num_template_parameter_lists = 0;
3924 /* Special parsing data structures. */
3925 parser->omp_declare_simd = NULL;
3926 parser->oacc_routine = NULL;
3928 /* Not declaring an implicit function template. */
3929 parser->auto_is_implicit_function_template_parm_p = false;
3930 parser->fully_implicit_function_template_p = false;
3931 parser->implicit_template_parms = 0;
3932 parser->implicit_template_scope = 0;
3934 /* Allow constrained-type-specifiers. */
3935 parser->prevent_constrained_type_specifiers = 0;
3937 /* We haven't yet seen an 'extern "C"'. */
3938 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3940 return parser;
3943 /* Create a cp_lexer structure which will emit the tokens in CACHE
3944 and push it onto the parser's lexer stack. This is used for delayed
3945 parsing of in-class method bodies and default arguments, and should
3946 not be confused with tentative parsing. */
3947 static void
3948 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3950 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3951 lexer->next = parser->lexer;
3952 parser->lexer = lexer;
3954 /* Move the current source position to that of the first token in the
3955 new lexer. */
3956 cp_lexer_set_source_position_from_token (lexer->next_token);
3959 /* Pop the top lexer off the parser stack. This is never used for the
3960 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3961 static void
3962 cp_parser_pop_lexer (cp_parser *parser)
3964 cp_lexer *lexer = parser->lexer;
3965 parser->lexer = lexer->next;
3966 cp_lexer_destroy (lexer);
3968 /* Put the current source position back where it was before this
3969 lexer was pushed. */
3970 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3973 /* Lexical conventions [gram.lex] */
3975 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3976 identifier. */
3978 static cp_expr
3979 cp_parser_identifier (cp_parser* parser)
3981 cp_token *token;
3983 /* Look for the identifier. */
3984 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3985 /* Return the value. */
3986 if (token)
3987 return cp_expr (token->u.value, token->location);
3988 else
3989 return error_mark_node;
3992 /* Parse a sequence of adjacent string constants. Returns a
3993 TREE_STRING representing the combined, nul-terminated string
3994 constant. If TRANSLATE is true, translate the string to the
3995 execution character set. If WIDE_OK is true, a wide string is
3996 invalid here.
3998 C++98 [lex.string] says that if a narrow string literal token is
3999 adjacent to a wide string literal token, the behavior is undefined.
4000 However, C99 6.4.5p4 says that this results in a wide string literal.
4001 We follow C99 here, for consistency with the C front end.
4003 This code is largely lifted from lex_string() in c-lex.c.
4005 FUTURE: ObjC++ will need to handle @-strings here. */
4006 static cp_expr
4007 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4008 bool lookup_udlit = true)
4010 tree value;
4011 size_t count;
4012 struct obstack str_ob;
4013 cpp_string str, istr, *strs;
4014 cp_token *tok;
4015 enum cpp_ttype type, curr_type;
4016 int have_suffix_p = 0;
4017 tree string_tree;
4018 tree suffix_id = NULL_TREE;
4019 bool curr_tok_is_userdef_p = false;
4021 tok = cp_lexer_peek_token (parser->lexer);
4022 if (!cp_parser_is_string_literal (tok))
4024 cp_parser_error (parser, "expected string-literal");
4025 return error_mark_node;
4028 location_t loc = tok->location;
4030 if (cpp_userdef_string_p (tok->type))
4032 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4033 curr_type = cpp_userdef_string_remove_type (tok->type);
4034 curr_tok_is_userdef_p = true;
4036 else
4038 string_tree = tok->u.value;
4039 curr_type = tok->type;
4041 type = curr_type;
4043 /* Try to avoid the overhead of creating and destroying an obstack
4044 for the common case of just one string. */
4045 if (!cp_parser_is_string_literal
4046 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4048 cp_lexer_consume_token (parser->lexer);
4050 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4051 str.len = TREE_STRING_LENGTH (string_tree);
4052 count = 1;
4054 if (curr_tok_is_userdef_p)
4056 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4057 have_suffix_p = 1;
4058 curr_type = cpp_userdef_string_remove_type (tok->type);
4060 else
4061 curr_type = tok->type;
4063 strs = &str;
4065 else
4067 location_t last_tok_loc = tok->location;
4068 gcc_obstack_init (&str_ob);
4069 count = 0;
4073 cp_lexer_consume_token (parser->lexer);
4074 count++;
4075 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4076 str.len = TREE_STRING_LENGTH (string_tree);
4078 if (curr_tok_is_userdef_p)
4080 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4081 if (have_suffix_p == 0)
4083 suffix_id = curr_suffix_id;
4084 have_suffix_p = 1;
4086 else if (have_suffix_p == 1
4087 && curr_suffix_id != suffix_id)
4089 error ("inconsistent user-defined literal suffixes"
4090 " %qD and %qD in string literal",
4091 suffix_id, curr_suffix_id);
4092 have_suffix_p = -1;
4094 curr_type = cpp_userdef_string_remove_type (tok->type);
4096 else
4097 curr_type = tok->type;
4099 if (type != curr_type)
4101 if (type == CPP_STRING)
4102 type = curr_type;
4103 else if (curr_type != CPP_STRING)
4105 rich_location rich_loc (line_table, tok->location);
4106 rich_loc.add_range (last_tok_loc, false);
4107 error_at (&rich_loc,
4108 "unsupported non-standard concatenation "
4109 "of string literals");
4113 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4115 last_tok_loc = tok->location;
4117 tok = cp_lexer_peek_token (parser->lexer);
4118 if (cpp_userdef_string_p (tok->type))
4120 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4121 curr_type = cpp_userdef_string_remove_type (tok->type);
4122 curr_tok_is_userdef_p = true;
4124 else
4126 string_tree = tok->u.value;
4127 curr_type = tok->type;
4128 curr_tok_is_userdef_p = false;
4131 while (cp_parser_is_string_literal (tok));
4133 /* A string literal built by concatenation has its caret=start at
4134 the start of the initial string, and its finish at the finish of
4135 the final string literal. */
4136 loc = make_location (loc, loc, get_finish (last_tok_loc));
4138 strs = (cpp_string *) obstack_finish (&str_ob);
4141 if (type != CPP_STRING && !wide_ok)
4143 cp_parser_error (parser, "a wide string is invalid in this context");
4144 type = CPP_STRING;
4147 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4148 (parse_in, strs, count, &istr, type))
4150 value = build_string (istr.len, (const char *)istr.text);
4151 free (CONST_CAST (unsigned char *, istr.text));
4153 switch (type)
4155 default:
4156 case CPP_STRING:
4157 case CPP_UTF8STRING:
4158 TREE_TYPE (value) = char_array_type_node;
4159 break;
4160 case CPP_STRING16:
4161 TREE_TYPE (value) = char16_array_type_node;
4162 break;
4163 case CPP_STRING32:
4164 TREE_TYPE (value) = char32_array_type_node;
4165 break;
4166 case CPP_WSTRING:
4167 TREE_TYPE (value) = wchar_array_type_node;
4168 break;
4171 value = fix_string_type (value);
4173 if (have_suffix_p)
4175 tree literal = build_userdef_literal (suffix_id, value,
4176 OT_NONE, NULL_TREE);
4177 if (lookup_udlit)
4178 value = cp_parser_userdef_string_literal (literal);
4179 else
4180 value = literal;
4183 else
4184 /* cpp_interpret_string has issued an error. */
4185 value = error_mark_node;
4187 if (count > 1)
4188 obstack_free (&str_ob, 0);
4190 return cp_expr (value, loc);
4193 /* Look up a literal operator with the name and the exact arguments. */
4195 static tree
4196 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4198 tree decl;
4199 decl = lookup_name (name);
4200 if (!decl || !is_overloaded_fn (decl))
4201 return error_mark_node;
4203 for (lkp_iterator iter (decl); iter; ++iter)
4205 unsigned int ix;
4206 bool found = true;
4207 tree fn = *iter;
4208 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4209 if (parmtypes != NULL_TREE)
4211 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4212 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4214 tree tparm = TREE_VALUE (parmtypes);
4215 tree targ = TREE_TYPE ((*args)[ix]);
4216 bool ptr = TYPE_PTR_P (tparm);
4217 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4218 if ((ptr || arr || !same_type_p (tparm, targ))
4219 && (!ptr || !arr
4220 || !same_type_p (TREE_TYPE (tparm),
4221 TREE_TYPE (targ))))
4222 found = false;
4224 if (found
4225 && ix == vec_safe_length (args)
4226 /* May be this should be sufficient_parms_p instead,
4227 depending on how exactly should user-defined literals
4228 work in presence of default arguments on the literal
4229 operator parameters. */
4230 && parmtypes == void_list_node)
4231 return decl;
4235 return error_mark_node;
4238 /* Parse a user-defined char constant. Returns a call to a user-defined
4239 literal operator taking the character as an argument. */
4241 static cp_expr
4242 cp_parser_userdef_char_literal (cp_parser *parser)
4244 cp_token *token = cp_lexer_consume_token (parser->lexer);
4245 tree literal = token->u.value;
4246 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4247 tree value = USERDEF_LITERAL_VALUE (literal);
4248 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4249 tree decl, result;
4251 /* Build up a call to the user-defined operator */
4252 /* Lookup the name we got back from the id-expression. */
4253 vec<tree, va_gc> *args = make_tree_vector ();
4254 vec_safe_push (args, value);
4255 decl = lookup_literal_operator (name, args);
4256 if (!decl || decl == error_mark_node)
4258 error ("unable to find character literal operator %qD with %qT argument",
4259 name, TREE_TYPE (value));
4260 release_tree_vector (args);
4261 return error_mark_node;
4263 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4264 release_tree_vector (args);
4265 return result;
4268 /* A subroutine of cp_parser_userdef_numeric_literal to
4269 create a char... template parameter pack from a string node. */
4271 static tree
4272 make_char_string_pack (tree value)
4274 tree charvec;
4275 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4276 const char *str = TREE_STRING_POINTER (value);
4277 int i, len = TREE_STRING_LENGTH (value) - 1;
4278 tree argvec = make_tree_vec (1);
4280 /* Fill in CHARVEC with all of the parameters. */
4281 charvec = make_tree_vec (len);
4282 for (i = 0; i < len; ++i)
4283 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4285 /* Build the argument packs. */
4286 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4288 TREE_VEC_ELT (argvec, 0) = argpack;
4290 return argvec;
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4296 static tree
4297 make_string_pack (tree value)
4299 tree charvec;
4300 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4301 const unsigned char *str
4302 = (const unsigned char *) TREE_STRING_POINTER (value);
4303 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4304 int len = TREE_STRING_LENGTH (value) / sz - 1;
4305 tree argvec = make_tree_vec (2);
4307 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4308 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4310 /* First template parm is character type. */
4311 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4313 /* Fill in CHARVEC with all of the parameters. */
4314 charvec = make_tree_vec (len);
4315 for (int i = 0; i < len; ++i)
4316 TREE_VEC_ELT (charvec, i)
4317 = double_int_to_tree (str_char_type_node,
4318 double_int::from_buffer (str + i * sz, sz));
4320 /* Build the argument packs. */
4321 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4323 TREE_VEC_ELT (argvec, 1) = argpack;
4325 return argvec;
4328 /* Parse a user-defined numeric constant. returns a call to a user-defined
4329 literal operator. */
4331 static cp_expr
4332 cp_parser_userdef_numeric_literal (cp_parser *parser)
4334 cp_token *token = cp_lexer_consume_token (parser->lexer);
4335 tree literal = token->u.value;
4336 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4337 tree value = USERDEF_LITERAL_VALUE (literal);
4338 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4339 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4340 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341 tree decl, result;
4342 vec<tree, va_gc> *args;
4344 /* Look for a literal operator taking the exact type of numeric argument
4345 as the literal value. */
4346 args = make_tree_vector ();
4347 vec_safe_push (args, value);
4348 decl = lookup_literal_operator (name, args);
4349 if (decl && decl != error_mark_node)
4351 result = finish_call_expr (decl, &args, false, true,
4352 tf_warning_or_error);
4354 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4356 warning_at (token->location, OPT_Woverflow,
4357 "integer literal exceeds range of %qT type",
4358 long_long_unsigned_type_node);
4360 else
4362 if (overflow > 0)
4363 warning_at (token->location, OPT_Woverflow,
4364 "floating literal exceeds range of %qT type",
4365 long_double_type_node);
4366 else if (overflow < 0)
4367 warning_at (token->location, OPT_Woverflow,
4368 "floating literal truncated to zero");
4371 release_tree_vector (args);
4372 return result;
4374 release_tree_vector (args);
4376 /* If the numeric argument didn't work, look for a raw literal
4377 operator taking a const char* argument consisting of the number
4378 in string format. */
4379 args = make_tree_vector ();
4380 vec_safe_push (args, num_string);
4381 decl = lookup_literal_operator (name, args);
4382 if (decl && decl != error_mark_node)
4384 result = finish_call_expr (decl, &args, false, true,
4385 tf_warning_or_error);
4386 release_tree_vector (args);
4387 return result;
4389 release_tree_vector (args);
4391 /* If the raw literal didn't work, look for a non-type template
4392 function with parameter pack char.... Call the function with
4393 template parameter characters representing the number. */
4394 args = make_tree_vector ();
4395 decl = lookup_literal_operator (name, args);
4396 if (decl && decl != error_mark_node)
4398 tree tmpl_args = make_char_string_pack (num_string);
4399 decl = lookup_template_function (decl, tmpl_args);
4400 result = finish_call_expr (decl, &args, false, true,
4401 tf_warning_or_error);
4402 release_tree_vector (args);
4403 return result;
4406 release_tree_vector (args);
4408 /* In C++14 the standard library defines complex number suffixes that
4409 conflict with GNU extensions. Prefer them if <complex> is #included. */
4410 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4411 bool i14 = (cxx_dialect > cxx11
4412 && (id_equal (suffix_id, "i")
4413 || id_equal (suffix_id, "if")
4414 || id_equal (suffix_id, "il")));
4415 diagnostic_t kind = DK_ERROR;
4416 int opt = 0;
4418 if (i14 && ext)
4420 tree cxlit = lookup_qualified_name (std_node,
4421 get_identifier ("complex_literals"),
4422 0, false, false);
4423 if (cxlit == error_mark_node)
4425 /* No <complex>, so pedwarn and use GNU semantics. */
4426 kind = DK_PEDWARN;
4427 opt = OPT_Wpedantic;
4431 bool complained
4432 = emit_diagnostic (kind, input_location, opt,
4433 "unable to find numeric literal operator %qD", name);
4435 if (!complained)
4436 /* Don't inform either. */;
4437 else if (i14)
4439 inform (token->location, "add %<using namespace std::complex_literals%> "
4440 "(from <complex>) to enable the C++14 user-defined literal "
4441 "suffixes");
4442 if (ext)
4443 inform (token->location, "or use %<j%> instead of %<i%> for the "
4444 "GNU built-in suffix");
4446 else if (!ext)
4447 inform (token->location, "use -fext-numeric-literals "
4448 "to enable more built-in suffixes");
4450 if (kind == DK_ERROR)
4451 value = error_mark_node;
4452 else
4454 /* Use the built-in semantics. */
4455 tree type;
4456 if (id_equal (suffix_id, "i"))
4458 if (TREE_CODE (value) == INTEGER_CST)
4459 type = integer_type_node;
4460 else
4461 type = double_type_node;
4463 else if (id_equal (suffix_id, "if"))
4464 type = float_type_node;
4465 else /* if (id_equal (suffix_id, "il")) */
4466 type = long_double_type_node;
4468 value = build_complex (build_complex_type (type),
4469 fold_convert (type, integer_zero_node),
4470 fold_convert (type, value));
4473 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4474 /* Avoid repeated diagnostics. */
4475 token->u.value = value;
4476 return value;
4479 /* Parse a user-defined string constant. Returns a call to a user-defined
4480 literal operator taking a character pointer and the length of the string
4481 as arguments. */
4483 static tree
4484 cp_parser_userdef_string_literal (tree literal)
4486 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4487 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4488 tree value = USERDEF_LITERAL_VALUE (literal);
4489 int len = TREE_STRING_LENGTH (value)
4490 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4491 tree decl, result;
4492 vec<tree, va_gc> *args;
4494 /* Build up a call to the user-defined operator. */
4495 /* Lookup the name we got back from the id-expression. */
4496 args = make_tree_vector ();
4497 vec_safe_push (args, value);
4498 vec_safe_push (args, build_int_cst (size_type_node, len));
4499 decl = lookup_literal_operator (name, args);
4501 if (decl && decl != error_mark_node)
4503 result = finish_call_expr (decl, &args, false, true,
4504 tf_warning_or_error);
4505 release_tree_vector (args);
4506 return result;
4508 release_tree_vector (args);
4510 /* Look for a template function with typename parameter CharT
4511 and parameter pack CharT... Call the function with
4512 template parameter characters representing the string. */
4513 args = make_tree_vector ();
4514 decl = lookup_literal_operator (name, args);
4515 if (decl && decl != error_mark_node)
4517 tree tmpl_args = make_string_pack (value);
4518 decl = lookup_template_function (decl, tmpl_args);
4519 result = finish_call_expr (decl, &args, false, true,
4520 tf_warning_or_error);
4521 release_tree_vector (args);
4522 return result;
4524 release_tree_vector (args);
4526 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4527 name, TREE_TYPE (value), size_type_node);
4528 return error_mark_node;
4532 /* Basic concepts [gram.basic] */
4534 /* Parse a translation-unit.
4536 translation-unit:
4537 declaration-seq [opt]
4539 Returns TRUE if all went well. */
4541 static bool
4542 cp_parser_translation_unit (cp_parser* parser)
4544 /* The address of the first non-permanent object on the declarator
4545 obstack. */
4546 static void *declarator_obstack_base;
4548 bool success;
4550 /* Create the declarator obstack, if necessary. */
4551 if (!cp_error_declarator)
4553 gcc_obstack_init (&declarator_obstack);
4554 /* Create the error declarator. */
4555 cp_error_declarator = make_declarator (cdk_error);
4556 /* Create the empty parameter list. */
4557 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4558 UNKNOWN_LOCATION);
4559 /* Remember where the base of the declarator obstack lies. */
4560 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4563 cp_parser_declaration_seq_opt (parser);
4565 /* If there are no tokens left then all went well. */
4566 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4568 /* Get rid of the token array; we don't need it any more. */
4569 cp_lexer_destroy (parser->lexer);
4570 parser->lexer = NULL;
4572 /* This file might have been a context that's implicitly extern
4573 "C". If so, pop the lang context. (Only relevant for PCH.) */
4574 if (parser->implicit_extern_c)
4576 pop_lang_context ();
4577 parser->implicit_extern_c = false;
4580 /* Finish up. */
4581 finish_translation_unit ();
4583 success = true;
4585 else
4587 cp_parser_error (parser, "expected declaration");
4588 success = false;
4591 /* Make sure the declarator obstack was fully cleaned up. */
4592 gcc_assert (obstack_next_free (&declarator_obstack)
4593 == declarator_obstack_base);
4595 /* All went well. */
4596 return success;
4599 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4600 decltype context. */
4602 static inline tsubst_flags_t
4603 complain_flags (bool decltype_p)
4605 tsubst_flags_t complain = tf_warning_or_error;
4606 if (decltype_p)
4607 complain |= tf_decltype;
4608 return complain;
4611 /* We're about to parse a collection of statements. If we're currently
4612 parsing tentatively, set up a firewall so that any nested
4613 cp_parser_commit_to_tentative_parse won't affect the current context. */
4615 static cp_token_position
4616 cp_parser_start_tentative_firewall (cp_parser *parser)
4618 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4619 return 0;
4621 cp_parser_parse_tentatively (parser);
4622 cp_parser_commit_to_topmost_tentative_parse (parser);
4623 return cp_lexer_token_position (parser->lexer, false);
4626 /* We've finished parsing the collection of statements. Wrap up the
4627 firewall and replace the relevant tokens with the parsed form. */
4629 static void
4630 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4631 tree expr)
4633 if (!start)
4634 return;
4636 /* Finish the firewall level. */
4637 cp_parser_parse_definitely (parser);
4638 /* And remember the result of the parse for when we try again. */
4639 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4640 token->type = CPP_PREPARSED_EXPR;
4641 token->u.value = expr;
4642 token->keyword = RID_MAX;
4643 cp_lexer_purge_tokens_after (parser->lexer, start);
4646 /* Like the above functions, but let the user modify the tokens. Used by
4647 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4648 later parses, so it makes sense to localize the effects of
4649 cp_parser_commit_to_tentative_parse. */
4651 struct tentative_firewall
4653 cp_parser *parser;
4654 bool set;
4656 tentative_firewall (cp_parser *p): parser(p)
4658 /* If we're currently parsing tentatively, start a committed level as a
4659 firewall and then an inner tentative parse. */
4660 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4662 cp_parser_parse_tentatively (parser);
4663 cp_parser_commit_to_topmost_tentative_parse (parser);
4664 cp_parser_parse_tentatively (parser);
4668 ~tentative_firewall()
4670 if (set)
4672 /* Finish the inner tentative parse and the firewall, propagating any
4673 uncommitted error state to the outer tentative parse. */
4674 bool err = cp_parser_error_occurred (parser);
4675 cp_parser_parse_definitely (parser);
4676 cp_parser_parse_definitely (parser);
4677 if (err)
4678 cp_parser_simulate_error (parser);
4683 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4684 This class is for tracking such a matching pair of symbols.
4685 In particular, it tracks the location of the first token,
4686 so that if the second token is missing, we can highlight the
4687 location of the first token when notifying the user about the
4688 problem. */
4690 template <typename traits_t>
4691 class token_pair
4693 public:
4694 /* token_pair's ctor. */
4695 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4697 /* If the next token is the opening symbol for this pair, consume it and
4698 return true.
4699 Otherwise, issue an error and return false.
4700 In either case, record the location of the opening token. */
4702 bool require_open (cp_parser *parser)
4704 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4705 return cp_parser_require (parser, traits_t::open_token_type,
4706 traits_t::required_token_open);
4709 /* Consume the next token from PARSER, recording its location as
4710 that of the opening token within the pair. */
4712 cp_token * consume_open (cp_parser *parser)
4714 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4715 gcc_assert (tok->type == traits_t::open_token_type);
4716 m_open_loc = tok->location;
4717 return tok;
4720 /* If the next token is the closing symbol for this pair, consume it
4721 and return it.
4722 Otherwise, issue an error, highlighting the location of the
4723 corresponding opening token, and return NULL. */
4725 cp_token *require_close (cp_parser *parser) const
4727 return cp_parser_require (parser, traits_t::close_token_type,
4728 traits_t::required_token_close,
4729 m_open_loc);
4732 private:
4733 location_t m_open_loc;
4736 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4738 struct matching_paren_traits
4740 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4741 static const enum required_token required_token_open = RT_OPEN_PAREN;
4742 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4743 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4746 /* "matching_parens" is a token_pair<T> class for tracking matching
4747 pairs of parentheses. */
4749 typedef token_pair<matching_paren_traits> matching_parens;
4751 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4753 struct matching_brace_traits
4755 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4756 static const enum required_token required_token_open = RT_OPEN_BRACE;
4757 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4758 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4761 /* "matching_braces" is a token_pair<T> class for tracking matching
4762 pairs of braces. */
4764 typedef token_pair<matching_brace_traits> matching_braces;
4767 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4768 enclosing parentheses. */
4770 static cp_expr
4771 cp_parser_statement_expr (cp_parser *parser)
4773 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4775 /* Consume the '('. */
4776 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4777 matching_parens parens;
4778 parens.consume_open (parser);
4779 /* Start the statement-expression. */
4780 tree expr = begin_stmt_expr ();
4781 /* Parse the compound-statement. */
4782 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4783 /* Finish up. */
4784 expr = finish_stmt_expr (expr, false);
4785 /* Consume the ')'. */
4786 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4787 if (!parens.require_close (parser))
4788 cp_parser_skip_to_end_of_statement (parser);
4790 cp_parser_end_tentative_firewall (parser, start, expr);
4791 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4792 return cp_expr (expr, combined_loc);
4795 /* Expressions [gram.expr] */
4797 /* Parse a fold-operator.
4799 fold-operator:
4800 - * / % ^ & | = < > << >>
4801 = -= *= /= %= ^= &= |= <<= >>=
4802 == != <= >= && || , .* ->*
4804 This returns the tree code corresponding to the matched operator
4805 as an int. When the current token matches a compound assignment
4806 opertor, the resulting tree code is the negative value of the
4807 non-assignment operator. */
4809 static int
4810 cp_parser_fold_operator (cp_token *token)
4812 switch (token->type)
4814 case CPP_PLUS: return PLUS_EXPR;
4815 case CPP_MINUS: return MINUS_EXPR;
4816 case CPP_MULT: return MULT_EXPR;
4817 case CPP_DIV: return TRUNC_DIV_EXPR;
4818 case CPP_MOD: return TRUNC_MOD_EXPR;
4819 case CPP_XOR: return BIT_XOR_EXPR;
4820 case CPP_AND: return BIT_AND_EXPR;
4821 case CPP_OR: return BIT_IOR_EXPR;
4822 case CPP_LSHIFT: return LSHIFT_EXPR;
4823 case CPP_RSHIFT: return RSHIFT_EXPR;
4825 case CPP_EQ: return -NOP_EXPR;
4826 case CPP_PLUS_EQ: return -PLUS_EXPR;
4827 case CPP_MINUS_EQ: return -MINUS_EXPR;
4828 case CPP_MULT_EQ: return -MULT_EXPR;
4829 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4830 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4831 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4832 case CPP_AND_EQ: return -BIT_AND_EXPR;
4833 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4834 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4835 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4837 case CPP_EQ_EQ: return EQ_EXPR;
4838 case CPP_NOT_EQ: return NE_EXPR;
4839 case CPP_LESS: return LT_EXPR;
4840 case CPP_GREATER: return GT_EXPR;
4841 case CPP_LESS_EQ: return LE_EXPR;
4842 case CPP_GREATER_EQ: return GE_EXPR;
4844 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4845 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4847 case CPP_COMMA: return COMPOUND_EXPR;
4849 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4850 case CPP_DEREF_STAR: return MEMBER_REF;
4852 default: return ERROR_MARK;
4856 /* Returns true if CODE indicates a binary expression, which is not allowed in
4857 the LHS of a fold-expression. More codes will need to be added to use this
4858 function in other contexts. */
4860 static bool
4861 is_binary_op (tree_code code)
4863 switch (code)
4865 case PLUS_EXPR:
4866 case POINTER_PLUS_EXPR:
4867 case MINUS_EXPR:
4868 case MULT_EXPR:
4869 case TRUNC_DIV_EXPR:
4870 case TRUNC_MOD_EXPR:
4871 case BIT_XOR_EXPR:
4872 case BIT_AND_EXPR:
4873 case BIT_IOR_EXPR:
4874 case LSHIFT_EXPR:
4875 case RSHIFT_EXPR:
4877 case MODOP_EXPR:
4879 case EQ_EXPR:
4880 case NE_EXPR:
4881 case LE_EXPR:
4882 case GE_EXPR:
4883 case LT_EXPR:
4884 case GT_EXPR:
4886 case TRUTH_ANDIF_EXPR:
4887 case TRUTH_ORIF_EXPR:
4889 case COMPOUND_EXPR:
4891 case DOTSTAR_EXPR:
4892 case MEMBER_REF:
4893 return true;
4895 default:
4896 return false;
4900 /* If the next token is a suitable fold operator, consume it and return as
4901 the function above. */
4903 static int
4904 cp_parser_fold_operator (cp_parser *parser)
4906 cp_token* token = cp_lexer_peek_token (parser->lexer);
4907 int code = cp_parser_fold_operator (token);
4908 if (code != ERROR_MARK)
4909 cp_lexer_consume_token (parser->lexer);
4910 return code;
4913 /* Parse a fold-expression.
4915 fold-expression:
4916 ( ... folding-operator cast-expression)
4917 ( cast-expression folding-operator ... )
4918 ( cast-expression folding operator ... folding-operator cast-expression)
4920 Note that the '(' and ')' are matched in primary expression. */
4922 static cp_expr
4923 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4925 cp_id_kind pidk;
4927 // Left fold.
4928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4930 cp_lexer_consume_token (parser->lexer);
4931 int op = cp_parser_fold_operator (parser);
4932 if (op == ERROR_MARK)
4934 cp_parser_error (parser, "expected binary operator");
4935 return error_mark_node;
4938 tree expr = cp_parser_cast_expression (parser, false, false,
4939 false, &pidk);
4940 if (expr == error_mark_node)
4941 return error_mark_node;
4942 return finish_left_unary_fold_expr (expr, op);
4945 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4946 int op = cp_parser_fold_operator (parser);
4947 if (op == ERROR_MARK)
4949 cp_parser_error (parser, "expected binary operator");
4950 return error_mark_node;
4953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4955 cp_parser_error (parser, "expected ...");
4956 return error_mark_node;
4958 cp_lexer_consume_token (parser->lexer);
4960 /* The operands of a fold-expression are cast-expressions, so binary or
4961 conditional expressions are not allowed. We check this here to avoid
4962 tentative parsing. */
4963 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4964 /* OK, the expression was parenthesized. */;
4965 else if (is_binary_op (TREE_CODE (expr1)))
4966 error_at (location_of (expr1),
4967 "binary expression in operand of fold-expression");
4968 else if (TREE_CODE (expr1) == COND_EXPR
4969 || (REFERENCE_REF_P (expr1)
4970 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4971 error_at (location_of (expr1),
4972 "conditional expression in operand of fold-expression");
4974 // Right fold.
4975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4976 return finish_right_unary_fold_expr (expr1, op);
4978 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4980 cp_parser_error (parser, "mismatched operator in fold-expression");
4981 return error_mark_node;
4983 cp_lexer_consume_token (parser->lexer);
4985 // Binary left or right fold.
4986 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4987 if (expr2 == error_mark_node)
4988 return error_mark_node;
4989 return finish_binary_fold_expr (expr1, expr2, op);
4992 /* Parse a primary-expression.
4994 primary-expression:
4995 literal
4996 this
4997 ( expression )
4998 id-expression
4999 lambda-expression (C++11)
5001 GNU Extensions:
5003 primary-expression:
5004 ( compound-statement )
5005 __builtin_va_arg ( assignment-expression , type-id )
5006 __builtin_offsetof ( type-id , offsetof-expression )
5008 C++ Extensions:
5009 __has_nothrow_assign ( type-id )
5010 __has_nothrow_constructor ( type-id )
5011 __has_nothrow_copy ( type-id )
5012 __has_trivial_assign ( type-id )
5013 __has_trivial_constructor ( type-id )
5014 __has_trivial_copy ( type-id )
5015 __has_trivial_destructor ( type-id )
5016 __has_virtual_destructor ( type-id )
5017 __is_abstract ( type-id )
5018 __is_base_of ( type-id , type-id )
5019 __is_class ( type-id )
5020 __is_empty ( type-id )
5021 __is_enum ( type-id )
5022 __is_final ( type-id )
5023 __is_literal_type ( type-id )
5024 __is_pod ( type-id )
5025 __is_polymorphic ( type-id )
5026 __is_std_layout ( type-id )
5027 __is_trivial ( type-id )
5028 __is_union ( type-id )
5030 Objective-C++ Extension:
5032 primary-expression:
5033 objc-expression
5035 literal:
5036 __null
5038 ADDRESS_P is true iff this expression was immediately preceded by
5039 "&" and therefore might denote a pointer-to-member. CAST_P is true
5040 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5041 true iff this expression is a template argument.
5043 Returns a representation of the expression. Upon return, *IDK
5044 indicates what kind of id-expression (if any) was present. */
5046 static cp_expr
5047 cp_parser_primary_expression (cp_parser *parser,
5048 bool address_p,
5049 bool cast_p,
5050 bool template_arg_p,
5051 bool decltype_p,
5052 cp_id_kind *idk)
5054 cp_token *token = NULL;
5056 /* Assume the primary expression is not an id-expression. */
5057 *idk = CP_ID_KIND_NONE;
5059 /* Peek at the next token. */
5060 token = cp_lexer_peek_token (parser->lexer);
5061 switch ((int) token->type)
5063 /* literal:
5064 integer-literal
5065 character-literal
5066 floating-literal
5067 string-literal
5068 boolean-literal
5069 pointer-literal
5070 user-defined-literal */
5071 case CPP_CHAR:
5072 case CPP_CHAR16:
5073 case CPP_CHAR32:
5074 case CPP_WCHAR:
5075 case CPP_UTF8CHAR:
5076 case CPP_NUMBER:
5077 case CPP_PREPARSED_EXPR:
5078 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5079 return cp_parser_userdef_numeric_literal (parser);
5080 token = cp_lexer_consume_token (parser->lexer);
5081 if (TREE_CODE (token->u.value) == FIXED_CST)
5083 error_at (token->location,
5084 "fixed-point types not supported in C++");
5085 return error_mark_node;
5087 /* Floating-point literals are only allowed in an integral
5088 constant expression if they are cast to an integral or
5089 enumeration type. */
5090 if (TREE_CODE (token->u.value) == REAL_CST
5091 && parser->integral_constant_expression_p
5092 && pedantic)
5094 /* CAST_P will be set even in invalid code like "int(2.7 +
5095 ...)". Therefore, we have to check that the next token
5096 is sure to end the cast. */
5097 if (cast_p)
5099 cp_token *next_token;
5101 next_token = cp_lexer_peek_token (parser->lexer);
5102 if (/* The comma at the end of an
5103 enumerator-definition. */
5104 next_token->type != CPP_COMMA
5105 /* The curly brace at the end of an enum-specifier. */
5106 && next_token->type != CPP_CLOSE_BRACE
5107 /* The end of a statement. */
5108 && next_token->type != CPP_SEMICOLON
5109 /* The end of the cast-expression. */
5110 && next_token->type != CPP_CLOSE_PAREN
5111 /* The end of an array bound. */
5112 && next_token->type != CPP_CLOSE_SQUARE
5113 /* The closing ">" in a template-argument-list. */
5114 && (next_token->type != CPP_GREATER
5115 || parser->greater_than_is_operator_p)
5116 /* C++0x only: A ">>" treated like two ">" tokens,
5117 in a template-argument-list. */
5118 && (next_token->type != CPP_RSHIFT
5119 || (cxx_dialect == cxx98)
5120 || parser->greater_than_is_operator_p))
5121 cast_p = false;
5124 /* If we are within a cast, then the constraint that the
5125 cast is to an integral or enumeration type will be
5126 checked at that point. If we are not within a cast, then
5127 this code is invalid. */
5128 if (!cast_p)
5129 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5131 return cp_expr (token->u.value, token->location);
5133 case CPP_CHAR_USERDEF:
5134 case CPP_CHAR16_USERDEF:
5135 case CPP_CHAR32_USERDEF:
5136 case CPP_WCHAR_USERDEF:
5137 case CPP_UTF8CHAR_USERDEF:
5138 return cp_parser_userdef_char_literal (parser);
5140 case CPP_STRING:
5141 case CPP_STRING16:
5142 case CPP_STRING32:
5143 case CPP_WSTRING:
5144 case CPP_UTF8STRING:
5145 case CPP_STRING_USERDEF:
5146 case CPP_STRING16_USERDEF:
5147 case CPP_STRING32_USERDEF:
5148 case CPP_WSTRING_USERDEF:
5149 case CPP_UTF8STRING_USERDEF:
5150 /* ??? Should wide strings be allowed when parser->translate_strings_p
5151 is false (i.e. in attributes)? If not, we can kill the third
5152 argument to cp_parser_string_literal. */
5153 return cp_parser_string_literal (parser,
5154 parser->translate_strings_p,
5155 true);
5157 case CPP_OPEN_PAREN:
5158 /* If we see `( { ' then we are looking at the beginning of
5159 a GNU statement-expression. */
5160 if (cp_parser_allow_gnu_extensions_p (parser)
5161 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5163 /* Statement-expressions are not allowed by the standard. */
5164 pedwarn (token->location, OPT_Wpedantic,
5165 "ISO C++ forbids braced-groups within expressions");
5167 /* And they're not allowed outside of a function-body; you
5168 cannot, for example, write:
5170 int i = ({ int j = 3; j + 1; });
5172 at class or namespace scope. */
5173 if (!parser->in_function_body
5174 || parser->in_template_argument_list_p)
5176 error_at (token->location,
5177 "statement-expressions are not allowed outside "
5178 "functions nor in template-argument lists");
5179 cp_parser_skip_to_end_of_block_or_statement (parser);
5180 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5181 cp_lexer_consume_token (parser->lexer);
5182 return error_mark_node;
5184 else
5185 return cp_parser_statement_expr (parser);
5187 /* Otherwise it's a normal parenthesized expression. */
5189 cp_expr expr;
5190 bool saved_greater_than_is_operator_p;
5192 location_t open_paren_loc = token->location;
5194 /* Consume the `('. */
5195 matching_parens parens;
5196 parens.consume_open (parser);
5197 /* Within a parenthesized expression, a `>' token is always
5198 the greater-than operator. */
5199 saved_greater_than_is_operator_p
5200 = parser->greater_than_is_operator_p;
5201 parser->greater_than_is_operator_p = true;
5203 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5204 /* Left fold expression. */
5205 expr = NULL_TREE;
5206 else
5207 /* Parse the parenthesized expression. */
5208 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5210 token = cp_lexer_peek_token (parser->lexer);
5211 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5213 expr = cp_parser_fold_expression (parser, expr);
5214 if (expr != error_mark_node
5215 && cxx_dialect < cxx17
5216 && !in_system_header_at (input_location))
5217 pedwarn (input_location, 0, "fold-expressions only available "
5218 "with -std=c++17 or -std=gnu++17");
5220 else
5221 /* Let the front end know that this expression was
5222 enclosed in parentheses. This matters in case, for
5223 example, the expression is of the form `A::B', since
5224 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5225 not. */
5226 expr = finish_parenthesized_expr (expr);
5228 /* DR 705: Wrapping an unqualified name in parentheses
5229 suppresses arg-dependent lookup. We want to pass back
5230 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5231 (c++/37862), but none of the others. */
5232 if (*idk != CP_ID_KIND_QUALIFIED)
5233 *idk = CP_ID_KIND_NONE;
5235 /* The `>' token might be the end of a template-id or
5236 template-parameter-list now. */
5237 parser->greater_than_is_operator_p
5238 = saved_greater_than_is_operator_p;
5240 /* Consume the `)'. */
5241 token = cp_lexer_peek_token (parser->lexer);
5242 location_t close_paren_loc = token->location;
5243 expr.set_range (open_paren_loc, close_paren_loc);
5244 if (!parens.require_close (parser)
5245 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5246 cp_parser_skip_to_end_of_statement (parser);
5248 return expr;
5251 case CPP_OPEN_SQUARE:
5253 if (c_dialect_objc ())
5255 /* We might have an Objective-C++ message. */
5256 cp_parser_parse_tentatively (parser);
5257 tree msg = cp_parser_objc_message_expression (parser);
5258 /* If that works out, we're done ... */
5259 if (cp_parser_parse_definitely (parser))
5260 return msg;
5261 /* ... else, fall though to see if it's a lambda. */
5263 cp_expr lam = cp_parser_lambda_expression (parser);
5264 /* Don't warn about a failed tentative parse. */
5265 if (cp_parser_error_occurred (parser))
5266 return error_mark_node;
5267 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5268 return lam;
5271 case CPP_OBJC_STRING:
5272 if (c_dialect_objc ())
5273 /* We have an Objective-C++ string literal. */
5274 return cp_parser_objc_expression (parser);
5275 cp_parser_error (parser, "expected primary-expression");
5276 return error_mark_node;
5278 case CPP_KEYWORD:
5279 switch (token->keyword)
5281 /* These two are the boolean literals. */
5282 case RID_TRUE:
5283 cp_lexer_consume_token (parser->lexer);
5284 return cp_expr (boolean_true_node, token->location);
5285 case RID_FALSE:
5286 cp_lexer_consume_token (parser->lexer);
5287 return cp_expr (boolean_false_node, token->location);
5289 /* The `__null' literal. */
5290 case RID_NULL:
5291 cp_lexer_consume_token (parser->lexer);
5292 return cp_expr (null_node, token->location);
5294 /* The `nullptr' literal. */
5295 case RID_NULLPTR:
5296 cp_lexer_consume_token (parser->lexer);
5297 return cp_expr (nullptr_node, token->location);
5299 /* Recognize the `this' keyword. */
5300 case RID_THIS:
5301 cp_lexer_consume_token (parser->lexer);
5302 if (parser->local_variables_forbidden_p)
5304 error_at (token->location,
5305 "%<this%> may not be used in this context");
5306 return error_mark_node;
5308 /* Pointers cannot appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5310 return error_mark_node;
5311 return cp_expr (finish_this_expr (), token->location);
5313 /* The `operator' keyword can be the beginning of an
5314 id-expression. */
5315 case RID_OPERATOR:
5316 goto id_expression;
5318 case RID_FUNCTION_NAME:
5319 case RID_PRETTY_FUNCTION_NAME:
5320 case RID_C99_FUNCTION_NAME:
5322 non_integral_constant name;
5324 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5325 __func__ are the names of variables -- but they are
5326 treated specially. Therefore, they are handled here,
5327 rather than relying on the generic id-expression logic
5328 below. Grammatically, these names are id-expressions.
5330 Consume the token. */
5331 token = cp_lexer_consume_token (parser->lexer);
5333 switch (token->keyword)
5335 case RID_FUNCTION_NAME:
5336 name = NIC_FUNC_NAME;
5337 break;
5338 case RID_PRETTY_FUNCTION_NAME:
5339 name = NIC_PRETTY_FUNC;
5340 break;
5341 case RID_C99_FUNCTION_NAME:
5342 name = NIC_C99_FUNC;
5343 break;
5344 default:
5345 gcc_unreachable ();
5348 if (cp_parser_non_integral_constant_expression (parser, name))
5349 return error_mark_node;
5351 /* Look up the name. */
5352 return finish_fname (token->u.value);
5355 case RID_VA_ARG:
5357 tree expression;
5358 tree type;
5359 source_location type_location;
5360 location_t start_loc
5361 = cp_lexer_peek_token (parser->lexer)->location;
5362 /* The `__builtin_va_arg' construct is used to handle
5363 `va_arg'. Consume the `__builtin_va_arg' token. */
5364 cp_lexer_consume_token (parser->lexer);
5365 /* Look for the opening `('. */
5366 matching_parens parens;
5367 parens.require_open (parser);
5368 /* Now, parse the assignment-expression. */
5369 expression = cp_parser_assignment_expression (parser);
5370 /* Look for the `,'. */
5371 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5372 type_location = cp_lexer_peek_token (parser->lexer)->location;
5373 /* Parse the type-id. */
5375 type_id_in_expr_sentinel s (parser);
5376 type = cp_parser_type_id (parser);
5378 /* Look for the closing `)'. */
5379 location_t finish_loc
5380 = cp_lexer_peek_token (parser->lexer)->location;
5381 parens.require_close (parser);
5382 /* Using `va_arg' in a constant-expression is not
5383 allowed. */
5384 if (cp_parser_non_integral_constant_expression (parser,
5385 NIC_VA_ARG))
5386 return error_mark_node;
5387 /* Construct a location of the form:
5388 __builtin_va_arg (v, int)
5389 ~~~~~~~~~~~~~~~~~~~~~^~~~
5390 with the caret at the type, ranging from the start of the
5391 "__builtin_va_arg" token to the close paren. */
5392 location_t combined_loc
5393 = make_location (type_location, start_loc, finish_loc);
5394 return build_x_va_arg (combined_loc, expression, type);
5397 case RID_OFFSETOF:
5398 return cp_parser_builtin_offsetof (parser);
5400 case RID_HAS_NOTHROW_ASSIGN:
5401 case RID_HAS_NOTHROW_CONSTRUCTOR:
5402 case RID_HAS_NOTHROW_COPY:
5403 case RID_HAS_TRIVIAL_ASSIGN:
5404 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5405 case RID_HAS_TRIVIAL_COPY:
5406 case RID_HAS_TRIVIAL_DESTRUCTOR:
5407 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5408 case RID_HAS_VIRTUAL_DESTRUCTOR:
5409 case RID_IS_ABSTRACT:
5410 case RID_IS_AGGREGATE:
5411 case RID_IS_BASE_OF:
5412 case RID_IS_CLASS:
5413 case RID_IS_EMPTY:
5414 case RID_IS_ENUM:
5415 case RID_IS_FINAL:
5416 case RID_IS_LITERAL_TYPE:
5417 case RID_IS_POD:
5418 case RID_IS_POLYMORPHIC:
5419 case RID_IS_SAME_AS:
5420 case RID_IS_STD_LAYOUT:
5421 case RID_IS_TRIVIAL:
5422 case RID_IS_TRIVIALLY_ASSIGNABLE:
5423 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5424 case RID_IS_TRIVIALLY_COPYABLE:
5425 case RID_IS_UNION:
5426 case RID_IS_ASSIGNABLE:
5427 case RID_IS_CONSTRUCTIBLE:
5428 return cp_parser_trait_expr (parser, token->keyword);
5430 // C++ concepts
5431 case RID_REQUIRES:
5432 return cp_parser_requires_expression (parser);
5434 /* Objective-C++ expressions. */
5435 case RID_AT_ENCODE:
5436 case RID_AT_PROTOCOL:
5437 case RID_AT_SELECTOR:
5438 return cp_parser_objc_expression (parser);
5440 case RID_TEMPLATE:
5441 if (parser->in_function_body
5442 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5443 == CPP_LESS))
5445 error_at (token->location,
5446 "a template declaration cannot appear at block scope");
5447 cp_parser_skip_to_end_of_block_or_statement (parser);
5448 return error_mark_node;
5450 /* FALLTHRU */
5451 default:
5452 cp_parser_error (parser, "expected primary-expression");
5453 return error_mark_node;
5456 /* An id-expression can start with either an identifier, a
5457 `::' as the beginning of a qualified-id, or the "operator"
5458 keyword. */
5459 case CPP_NAME:
5460 case CPP_SCOPE:
5461 case CPP_TEMPLATE_ID:
5462 case CPP_NESTED_NAME_SPECIFIER:
5464 id_expression:
5465 cp_expr id_expression;
5466 cp_expr decl;
5467 const char *error_msg;
5468 bool template_p;
5469 bool done;
5470 cp_token *id_expr_token;
5472 /* Parse the id-expression. */
5473 id_expression
5474 = cp_parser_id_expression (parser,
5475 /*template_keyword_p=*/false,
5476 /*check_dependency_p=*/true,
5477 &template_p,
5478 /*declarator_p=*/false,
5479 /*optional_p=*/false);
5480 if (id_expression == error_mark_node)
5481 return error_mark_node;
5482 id_expr_token = token;
5483 token = cp_lexer_peek_token (parser->lexer);
5484 done = (token->type != CPP_OPEN_SQUARE
5485 && token->type != CPP_OPEN_PAREN
5486 && token->type != CPP_DOT
5487 && token->type != CPP_DEREF
5488 && token->type != CPP_PLUS_PLUS
5489 && token->type != CPP_MINUS_MINUS);
5490 /* If we have a template-id, then no further lookup is
5491 required. If the template-id was for a template-class, we
5492 will sometimes have a TYPE_DECL at this point. */
5493 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5494 || TREE_CODE (id_expression) == TYPE_DECL)
5495 decl = id_expression;
5496 /* Look up the name. */
5497 else
5499 tree ambiguous_decls;
5501 /* If we already know that this lookup is ambiguous, then
5502 we've already issued an error message; there's no reason
5503 to check again. */
5504 if (id_expr_token->type == CPP_NAME
5505 && id_expr_token->error_reported)
5507 cp_parser_simulate_error (parser);
5508 return error_mark_node;
5511 decl = cp_parser_lookup_name (parser, id_expression,
5512 none_type,
5513 template_p,
5514 /*is_namespace=*/false,
5515 /*check_dependency=*/true,
5516 &ambiguous_decls,
5517 id_expr_token->location);
5518 /* If the lookup was ambiguous, an error will already have
5519 been issued. */
5520 if (ambiguous_decls)
5521 return error_mark_node;
5523 /* In Objective-C++, we may have an Objective-C 2.0
5524 dot-syntax for classes here. */
5525 if (c_dialect_objc ()
5526 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5527 && TREE_CODE (decl) == TYPE_DECL
5528 && objc_is_class_name (decl))
5530 tree component;
5531 cp_lexer_consume_token (parser->lexer);
5532 component = cp_parser_identifier (parser);
5533 if (component == error_mark_node)
5534 return error_mark_node;
5536 tree result = objc_build_class_component_ref (id_expression,
5537 component);
5538 /* Build a location of the form:
5539 expr.component
5540 ~~~~~^~~~~~~~~
5541 with caret at the start of the component name (at
5542 input_location), ranging from the start of the id_expression
5543 to the end of the component name. */
5544 location_t combined_loc
5545 = make_location (input_location, id_expression.get_start (),
5546 get_finish (input_location));
5547 protected_set_expr_location (result, combined_loc);
5548 return result;
5551 /* In Objective-C++, an instance variable (ivar) may be preferred
5552 to whatever cp_parser_lookup_name() found.
5553 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5554 rest of c-family, we have to do a little extra work to preserve
5555 any location information in cp_expr "decl". Given that
5556 objc_lookup_ivar is implemented in "c-family" and "objc", we
5557 have a trip through the pure "tree" type, rather than cp_expr.
5558 Naively copying it back to "decl" would implicitly give the
5559 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5560 store an EXPR_LOCATION. Hence we only update "decl" (and
5561 hence its location_t) if we get back a different tree node. */
5562 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5563 id_expression);
5564 if (decl_tree != decl.get_value ())
5565 decl = cp_expr (decl_tree);
5567 /* If name lookup gives us a SCOPE_REF, then the
5568 qualifying scope was dependent. */
5569 if (TREE_CODE (decl) == SCOPE_REF)
5571 /* At this point, we do not know if DECL is a valid
5572 integral constant expression. We assume that it is
5573 in fact such an expression, so that code like:
5575 template <int N> struct A {
5576 int a[B<N>::i];
5579 is accepted. At template-instantiation time, we
5580 will check that B<N>::i is actually a constant. */
5581 return decl;
5583 /* Check to see if DECL is a local variable in a context
5584 where that is forbidden. */
5585 if (parser->local_variables_forbidden_p
5586 && local_variable_p (decl))
5588 /* It might be that we only found DECL because we are
5589 trying to be generous with pre-ISO scoping rules.
5590 For example, consider:
5592 int i;
5593 void g() {
5594 for (int i = 0; i < 10; ++i) {}
5595 extern void f(int j = i);
5598 Here, name look up will originally find the out
5599 of scope `i'. We need to issue a warning message,
5600 but then use the global `i'. */
5601 decl = check_for_out_of_scope_variable (decl);
5602 if (local_variable_p (decl))
5604 error_at (id_expr_token->location,
5605 "local variable %qD may not appear in this context",
5606 decl.get_value ());
5607 return error_mark_node;
5612 decl = (finish_id_expression
5613 (id_expression, decl, parser->scope,
5614 idk,
5615 parser->integral_constant_expression_p,
5616 parser->allow_non_integral_constant_expression_p,
5617 &parser->non_integral_constant_expression_p,
5618 template_p, done, address_p,
5619 template_arg_p,
5620 &error_msg,
5621 id_expression.get_location ()));
5622 if (error_msg)
5623 cp_parser_error (parser, error_msg);
5624 decl.set_location (id_expr_token->location);
5625 return decl;
5628 /* Anything else is an error. */
5629 default:
5630 cp_parser_error (parser, "expected primary-expression");
5631 return error_mark_node;
5635 static inline cp_expr
5636 cp_parser_primary_expression (cp_parser *parser,
5637 bool address_p,
5638 bool cast_p,
5639 bool template_arg_p,
5640 cp_id_kind *idk)
5642 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5643 /*decltype*/false, idk);
5646 /* Parse an id-expression.
5648 id-expression:
5649 unqualified-id
5650 qualified-id
5652 qualified-id:
5653 :: [opt] nested-name-specifier template [opt] unqualified-id
5654 :: identifier
5655 :: operator-function-id
5656 :: template-id
5658 Return a representation of the unqualified portion of the
5659 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5660 a `::' or nested-name-specifier.
5662 Often, if the id-expression was a qualified-id, the caller will
5663 want to make a SCOPE_REF to represent the qualified-id. This
5664 function does not do this in order to avoid wastefully creating
5665 SCOPE_REFs when they are not required.
5667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5668 `template' keyword.
5670 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5671 uninstantiated templates.
5673 If *TEMPLATE_P is non-NULL, it is set to true iff the
5674 `template' keyword is used to explicitly indicate that the entity
5675 named is a template.
5677 If DECLARATOR_P is true, the id-expression is appearing as part of
5678 a declarator, rather than as part of an expression. */
5680 static cp_expr
5681 cp_parser_id_expression (cp_parser *parser,
5682 bool template_keyword_p,
5683 bool check_dependency_p,
5684 bool *template_p,
5685 bool declarator_p,
5686 bool optional_p)
5688 bool global_scope_p;
5689 bool nested_name_specifier_p;
5691 /* Assume the `template' keyword was not used. */
5692 if (template_p)
5693 *template_p = template_keyword_p;
5695 /* Look for the optional `::' operator. */
5696 global_scope_p
5697 = (!template_keyword_p
5698 && (cp_parser_global_scope_opt (parser,
5699 /*current_scope_valid_p=*/false)
5700 != NULL_TREE));
5702 /* Look for the optional nested-name-specifier. */
5703 nested_name_specifier_p
5704 = (cp_parser_nested_name_specifier_opt (parser,
5705 /*typename_keyword_p=*/false,
5706 check_dependency_p,
5707 /*type_p=*/false,
5708 declarator_p,
5709 template_keyword_p)
5710 != NULL_TREE);
5712 /* If there is a nested-name-specifier, then we are looking at
5713 the first qualified-id production. */
5714 if (nested_name_specifier_p)
5716 tree saved_scope;
5717 tree saved_object_scope;
5718 tree saved_qualifying_scope;
5719 cp_expr unqualified_id;
5720 bool is_template;
5722 /* See if the next token is the `template' keyword. */
5723 if (!template_p)
5724 template_p = &is_template;
5725 *template_p = cp_parser_optional_template_keyword (parser);
5726 /* Name lookup we do during the processing of the
5727 unqualified-id might obliterate SCOPE. */
5728 saved_scope = parser->scope;
5729 saved_object_scope = parser->object_scope;
5730 saved_qualifying_scope = parser->qualifying_scope;
5731 /* Process the final unqualified-id. */
5732 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5733 check_dependency_p,
5734 declarator_p,
5735 /*optional_p=*/false);
5736 /* Restore the SAVED_SCOPE for our caller. */
5737 parser->scope = saved_scope;
5738 parser->object_scope = saved_object_scope;
5739 parser->qualifying_scope = saved_qualifying_scope;
5741 return unqualified_id;
5743 /* Otherwise, if we are in global scope, then we are looking at one
5744 of the other qualified-id productions. */
5745 else if (global_scope_p)
5747 cp_token *token;
5748 tree id;
5750 /* Peek at the next token. */
5751 token = cp_lexer_peek_token (parser->lexer);
5753 /* If it's an identifier, and the next token is not a "<", then
5754 we can avoid the template-id case. This is an optimization
5755 for this common case. */
5756 if (token->type == CPP_NAME
5757 && !cp_parser_nth_token_starts_template_argument_list_p
5758 (parser, 2))
5759 return cp_parser_identifier (parser);
5761 cp_parser_parse_tentatively (parser);
5762 /* Try a template-id. */
5763 id = cp_parser_template_id (parser,
5764 /*template_keyword_p=*/false,
5765 /*check_dependency_p=*/true,
5766 none_type,
5767 declarator_p);
5768 /* If that worked, we're done. */
5769 if (cp_parser_parse_definitely (parser))
5770 return id;
5772 /* Peek at the next token. (Changes in the token buffer may
5773 have invalidated the pointer obtained above.) */
5774 token = cp_lexer_peek_token (parser->lexer);
5776 switch (token->type)
5778 case CPP_NAME:
5779 return cp_parser_identifier (parser);
5781 case CPP_KEYWORD:
5782 if (token->keyword == RID_OPERATOR)
5783 return cp_parser_operator_function_id (parser);
5784 /* Fall through. */
5786 default:
5787 cp_parser_error (parser, "expected id-expression");
5788 return error_mark_node;
5791 else
5792 return cp_parser_unqualified_id (parser, template_keyword_p,
5793 /*check_dependency_p=*/true,
5794 declarator_p,
5795 optional_p);
5798 /* Parse an unqualified-id.
5800 unqualified-id:
5801 identifier
5802 operator-function-id
5803 conversion-function-id
5804 ~ class-name
5805 template-id
5807 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5808 keyword, in a construct like `A::template ...'.
5810 Returns a representation of unqualified-id. For the `identifier'
5811 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5812 production a BIT_NOT_EXPR is returned; the operand of the
5813 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5814 other productions, see the documentation accompanying the
5815 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5816 names are looked up in uninstantiated templates. If DECLARATOR_P
5817 is true, the unqualified-id is appearing as part of a declarator,
5818 rather than as part of an expression. */
5820 static cp_expr
5821 cp_parser_unqualified_id (cp_parser* parser,
5822 bool template_keyword_p,
5823 bool check_dependency_p,
5824 bool declarator_p,
5825 bool optional_p)
5827 cp_token *token;
5829 /* Peek at the next token. */
5830 token = cp_lexer_peek_token (parser->lexer);
5832 switch ((int) token->type)
5834 case CPP_NAME:
5836 tree id;
5838 /* We don't know yet whether or not this will be a
5839 template-id. */
5840 cp_parser_parse_tentatively (parser);
5841 /* Try a template-id. */
5842 id = cp_parser_template_id (parser, template_keyword_p,
5843 check_dependency_p,
5844 none_type,
5845 declarator_p);
5846 /* If it worked, we're done. */
5847 if (cp_parser_parse_definitely (parser))
5848 return id;
5849 /* Otherwise, it's an ordinary identifier. */
5850 return cp_parser_identifier (parser);
5853 case CPP_TEMPLATE_ID:
5854 return cp_parser_template_id (parser, template_keyword_p,
5855 check_dependency_p,
5856 none_type,
5857 declarator_p);
5859 case CPP_COMPL:
5861 tree type_decl;
5862 tree qualifying_scope;
5863 tree object_scope;
5864 tree scope;
5865 bool done;
5867 /* Consume the `~' token. */
5868 cp_lexer_consume_token (parser->lexer);
5869 /* Parse the class-name. The standard, as written, seems to
5870 say that:
5872 template <typename T> struct S { ~S (); };
5873 template <typename T> S<T>::~S() {}
5875 is invalid, since `~' must be followed by a class-name, but
5876 `S<T>' is dependent, and so not known to be a class.
5877 That's not right; we need to look in uninstantiated
5878 templates. A further complication arises from:
5880 template <typename T> void f(T t) {
5881 t.T::~T();
5884 Here, it is not possible to look up `T' in the scope of `T'
5885 itself. We must look in both the current scope, and the
5886 scope of the containing complete expression.
5888 Yet another issue is:
5890 struct S {
5891 int S;
5892 ~S();
5895 S::~S() {}
5897 The standard does not seem to say that the `S' in `~S'
5898 should refer to the type `S' and not the data member
5899 `S::S'. */
5901 /* DR 244 says that we look up the name after the "~" in the
5902 same scope as we looked up the qualifying name. That idea
5903 isn't fully worked out; it's more complicated than that. */
5904 scope = parser->scope;
5905 object_scope = parser->object_scope;
5906 qualifying_scope = parser->qualifying_scope;
5908 /* Check for invalid scopes. */
5909 if (scope == error_mark_node)
5911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5912 cp_lexer_consume_token (parser->lexer);
5913 return error_mark_node;
5915 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5917 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5918 error_at (token->location,
5919 "scope %qT before %<~%> is not a class-name",
5920 scope);
5921 cp_parser_simulate_error (parser);
5922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5923 cp_lexer_consume_token (parser->lexer);
5924 return error_mark_node;
5926 gcc_assert (!scope || TYPE_P (scope));
5928 /* If the name is of the form "X::~X" it's OK even if X is a
5929 typedef. */
5930 token = cp_lexer_peek_token (parser->lexer);
5931 if (scope
5932 && token->type == CPP_NAME
5933 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5934 != CPP_LESS)
5935 && (token->u.value == TYPE_IDENTIFIER (scope)
5936 || (CLASS_TYPE_P (scope)
5937 && constructor_name_p (token->u.value, scope))))
5939 cp_lexer_consume_token (parser->lexer);
5940 return build_nt (BIT_NOT_EXPR, scope);
5943 /* ~auto means the destructor of whatever the object is. */
5944 if (cp_parser_is_keyword (token, RID_AUTO))
5946 if (cxx_dialect < cxx14)
5947 pedwarn (input_location, 0,
5948 "%<~auto%> only available with "
5949 "-std=c++14 or -std=gnu++14");
5950 cp_lexer_consume_token (parser->lexer);
5951 return build_nt (BIT_NOT_EXPR, make_auto ());
5954 /* If there was an explicit qualification (S::~T), first look
5955 in the scope given by the qualification (i.e., S).
5957 Note: in the calls to cp_parser_class_name below we pass
5958 typename_type so that lookup finds the injected-class-name
5959 rather than the constructor. */
5960 done = false;
5961 type_decl = NULL_TREE;
5962 if (scope)
5964 cp_parser_parse_tentatively (parser);
5965 type_decl = cp_parser_class_name (parser,
5966 /*typename_keyword_p=*/false,
5967 /*template_keyword_p=*/false,
5968 typename_type,
5969 /*check_dependency=*/false,
5970 /*class_head_p=*/false,
5971 declarator_p);
5972 if (cp_parser_parse_definitely (parser))
5973 done = true;
5975 /* In "N::S::~S", look in "N" as well. */
5976 if (!done && scope && qualifying_scope)
5978 cp_parser_parse_tentatively (parser);
5979 parser->scope = qualifying_scope;
5980 parser->object_scope = NULL_TREE;
5981 parser->qualifying_scope = NULL_TREE;
5982 type_decl
5983 = cp_parser_class_name (parser,
5984 /*typename_keyword_p=*/false,
5985 /*template_keyword_p=*/false,
5986 typename_type,
5987 /*check_dependency=*/false,
5988 /*class_head_p=*/false,
5989 declarator_p);
5990 if (cp_parser_parse_definitely (parser))
5991 done = true;
5993 /* In "p->S::~T", look in the scope given by "*p" as well. */
5994 else if (!done && object_scope)
5996 cp_parser_parse_tentatively (parser);
5997 parser->scope = object_scope;
5998 parser->object_scope = NULL_TREE;
5999 parser->qualifying_scope = NULL_TREE;
6000 type_decl
6001 = 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 /* Look in the surrounding context. */
6012 if (!done)
6014 parser->scope = NULL_TREE;
6015 parser->object_scope = NULL_TREE;
6016 parser->qualifying_scope = NULL_TREE;
6017 if (processing_template_decl)
6018 cp_parser_parse_tentatively (parser);
6019 type_decl
6020 = cp_parser_class_name (parser,
6021 /*typename_keyword_p=*/false,
6022 /*template_keyword_p=*/false,
6023 typename_type,
6024 /*check_dependency=*/false,
6025 /*class_head_p=*/false,
6026 declarator_p);
6027 if (processing_template_decl
6028 && ! cp_parser_parse_definitely (parser))
6030 /* We couldn't find a type with this name. If we're parsing
6031 tentatively, fail and try something else. */
6032 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6034 cp_parser_simulate_error (parser);
6035 return error_mark_node;
6037 /* Otherwise, accept it and check for a match at instantiation
6038 time. */
6039 type_decl = cp_parser_identifier (parser);
6040 if (type_decl != error_mark_node)
6041 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6042 return type_decl;
6045 /* If an error occurred, assume that the name of the
6046 destructor is the same as the name of the qualifying
6047 class. That allows us to keep parsing after running
6048 into ill-formed destructor names. */
6049 if (type_decl == error_mark_node && scope)
6050 return build_nt (BIT_NOT_EXPR, scope);
6051 else if (type_decl == error_mark_node)
6052 return error_mark_node;
6054 /* Check that destructor name and scope match. */
6055 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6057 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6058 error_at (token->location,
6059 "declaration of %<~%T%> as member of %qT",
6060 type_decl, scope);
6061 cp_parser_simulate_error (parser);
6062 return error_mark_node;
6065 /* [class.dtor]
6067 A typedef-name that names a class shall not be used as the
6068 identifier in the declarator for a destructor declaration. */
6069 if (declarator_p
6070 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6071 && !DECL_SELF_REFERENCE_P (type_decl)
6072 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6073 error_at (token->location,
6074 "typedef-name %qD used as destructor declarator",
6075 type_decl);
6077 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6080 case CPP_KEYWORD:
6081 if (token->keyword == RID_OPERATOR)
6083 cp_expr id;
6085 /* This could be a template-id, so we try that first. */
6086 cp_parser_parse_tentatively (parser);
6087 /* Try a template-id. */
6088 id = cp_parser_template_id (parser, template_keyword_p,
6089 /*check_dependency_p=*/true,
6090 none_type,
6091 declarator_p);
6092 /* If that worked, we're done. */
6093 if (cp_parser_parse_definitely (parser))
6094 return id;
6095 /* We still don't know whether we're looking at an
6096 operator-function-id or a conversion-function-id. */
6097 cp_parser_parse_tentatively (parser);
6098 /* Try an operator-function-id. */
6099 id = cp_parser_operator_function_id (parser);
6100 /* If that didn't work, try a conversion-function-id. */
6101 if (!cp_parser_parse_definitely (parser))
6102 id = cp_parser_conversion_function_id (parser);
6103 else if (UDLIT_OPER_P (id))
6105 /* 17.6.3.3.5 */
6106 const char *name = UDLIT_OP_SUFFIX (id);
6107 if (name[0] != '_' && !in_system_header_at (input_location)
6108 && declarator_p)
6109 warning (OPT_Wliteral_suffix,
6110 "literal operator suffixes not preceded by %<_%>"
6111 " are reserved for future standardization");
6114 return id;
6116 /* Fall through. */
6118 default:
6119 if (optional_p)
6120 return NULL_TREE;
6121 cp_parser_error (parser, "expected unqualified-id");
6122 return error_mark_node;
6126 /* Parse an (optional) nested-name-specifier.
6128 nested-name-specifier: [C++98]
6129 class-or-namespace-name :: nested-name-specifier [opt]
6130 class-or-namespace-name :: template nested-name-specifier [opt]
6132 nested-name-specifier: [C++0x]
6133 type-name ::
6134 namespace-name ::
6135 nested-name-specifier identifier ::
6136 nested-name-specifier template [opt] simple-template-id ::
6138 PARSER->SCOPE should be set appropriately before this function is
6139 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6140 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6141 in name lookups.
6143 Sets PARSER->SCOPE to the class (TYPE) or namespace
6144 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6145 it unchanged if there is no nested-name-specifier. Returns the new
6146 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6148 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6149 part of a declaration and/or decl-specifier. */
6151 static tree
6152 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6153 bool typename_keyword_p,
6154 bool check_dependency_p,
6155 bool type_p,
6156 bool is_declaration,
6157 bool template_keyword_p /* = false */)
6159 bool success = false;
6160 cp_token_position start = 0;
6161 cp_token *token;
6163 /* Remember where the nested-name-specifier starts. */
6164 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6166 start = cp_lexer_token_position (parser->lexer, false);
6167 push_deferring_access_checks (dk_deferred);
6170 while (true)
6172 tree new_scope;
6173 tree old_scope;
6174 tree saved_qualifying_scope;
6176 /* Spot cases that cannot be the beginning of a
6177 nested-name-specifier. */
6178 token = cp_lexer_peek_token (parser->lexer);
6180 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6181 the already parsed nested-name-specifier. */
6182 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6184 /* Grab the nested-name-specifier and continue the loop. */
6185 cp_parser_pre_parsed_nested_name_specifier (parser);
6186 /* If we originally encountered this nested-name-specifier
6187 with IS_DECLARATION set to false, we will not have
6188 resolved TYPENAME_TYPEs, so we must do so here. */
6189 if (is_declaration
6190 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6192 new_scope = resolve_typename_type (parser->scope,
6193 /*only_current_p=*/false);
6194 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6195 parser->scope = new_scope;
6197 success = true;
6198 continue;
6201 /* Spot cases that cannot be the beginning of a
6202 nested-name-specifier. On the second and subsequent times
6203 through the loop, we look for the `template' keyword. */
6204 if (success && token->keyword == RID_TEMPLATE)
6206 /* A template-id can start a nested-name-specifier. */
6207 else if (token->type == CPP_TEMPLATE_ID)
6209 /* DR 743: decltype can be used in a nested-name-specifier. */
6210 else if (token_is_decltype (token))
6212 else
6214 /* If the next token is not an identifier, then it is
6215 definitely not a type-name or namespace-name. */
6216 if (token->type != CPP_NAME)
6217 break;
6218 /* If the following token is neither a `<' (to begin a
6219 template-id), nor a `::', then we are not looking at a
6220 nested-name-specifier. */
6221 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6223 if (token->type == CPP_COLON
6224 && parser->colon_corrects_to_scope_p
6225 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6227 gcc_rich_location richloc (token->location);
6228 richloc.add_fixit_replace ("::");
6229 error_at (&richloc,
6230 "found %<:%> in nested-name-specifier, "
6231 "expected %<::%>");
6232 token->type = CPP_SCOPE;
6235 if (token->type != CPP_SCOPE
6236 && !cp_parser_nth_token_starts_template_argument_list_p
6237 (parser, 2))
6238 break;
6241 /* The nested-name-specifier is optional, so we parse
6242 tentatively. */
6243 cp_parser_parse_tentatively (parser);
6245 /* Look for the optional `template' keyword, if this isn't the
6246 first time through the loop. */
6247 if (success)
6248 template_keyword_p = cp_parser_optional_template_keyword (parser);
6250 /* Save the old scope since the name lookup we are about to do
6251 might destroy it. */
6252 old_scope = parser->scope;
6253 saved_qualifying_scope = parser->qualifying_scope;
6254 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6255 look up names in "X<T>::I" in order to determine that "Y" is
6256 a template. So, if we have a typename at this point, we make
6257 an effort to look through it. */
6258 if (is_declaration
6259 && !typename_keyword_p
6260 && parser->scope
6261 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6262 parser->scope = resolve_typename_type (parser->scope,
6263 /*only_current_p=*/false);
6264 /* Parse the qualifying entity. */
6265 new_scope
6266 = cp_parser_qualifying_entity (parser,
6267 typename_keyword_p,
6268 template_keyword_p,
6269 check_dependency_p,
6270 type_p,
6271 is_declaration);
6272 /* Look for the `::' token. */
6273 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6275 /* If we found what we wanted, we keep going; otherwise, we're
6276 done. */
6277 if (!cp_parser_parse_definitely (parser))
6279 bool error_p = false;
6281 /* Restore the OLD_SCOPE since it was valid before the
6282 failed attempt at finding the last
6283 class-or-namespace-name. */
6284 parser->scope = old_scope;
6285 parser->qualifying_scope = saved_qualifying_scope;
6287 /* If the next token is a decltype, and the one after that is a
6288 `::', then the decltype has failed to resolve to a class or
6289 enumeration type. Give this error even when parsing
6290 tentatively since it can't possibly be valid--and we're going
6291 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6292 won't get another chance.*/
6293 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6294 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6295 == CPP_SCOPE))
6297 token = cp_lexer_consume_token (parser->lexer);
6298 error_at (token->location, "decltype evaluates to %qT, "
6299 "which is not a class or enumeration type",
6300 token->u.tree_check_value->value);
6301 parser->scope = error_mark_node;
6302 error_p = true;
6303 /* As below. */
6304 success = true;
6305 cp_lexer_consume_token (parser->lexer);
6308 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6309 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6311 /* If we have a non-type template-id followed by ::, it can't
6312 possibly be valid. */
6313 token = cp_lexer_peek_token (parser->lexer);
6314 tree tid = token->u.tree_check_value->value;
6315 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6316 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6318 tree tmpl = NULL_TREE;
6319 if (is_overloaded_fn (tid))
6321 tree fns = get_fns (tid);
6322 if (OVL_SINGLE_P (fns))
6323 tmpl = OVL_FIRST (fns);
6324 error_at (token->location, "function template-id %qD "
6325 "in nested-name-specifier", tid);
6327 else
6329 /* Variable template. */
6330 tmpl = TREE_OPERAND (tid, 0);
6331 gcc_assert (variable_template_p (tmpl));
6332 error_at (token->location, "variable template-id %qD "
6333 "in nested-name-specifier", tid);
6335 if (tmpl)
6336 inform (DECL_SOURCE_LOCATION (tmpl),
6337 "%qD declared here", tmpl);
6339 parser->scope = error_mark_node;
6340 error_p = true;
6341 /* As below. */
6342 success = true;
6343 cp_lexer_consume_token (parser->lexer);
6344 cp_lexer_consume_token (parser->lexer);
6348 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6349 break;
6350 /* If the next token is an identifier, and the one after
6351 that is a `::', then any valid interpretation would have
6352 found a class-or-namespace-name. */
6353 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6354 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6355 == CPP_SCOPE)
6356 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6357 != CPP_COMPL))
6359 token = cp_lexer_consume_token (parser->lexer);
6360 if (!error_p)
6362 if (!token->error_reported)
6364 tree decl;
6365 tree ambiguous_decls;
6367 decl = cp_parser_lookup_name (parser, token->u.value,
6368 none_type,
6369 /*is_template=*/false,
6370 /*is_namespace=*/false,
6371 /*check_dependency=*/true,
6372 &ambiguous_decls,
6373 token->location);
6374 if (TREE_CODE (decl) == TEMPLATE_DECL)
6375 error_at (token->location,
6376 "%qD used without template parameters",
6377 decl);
6378 else if (ambiguous_decls)
6380 // cp_parser_lookup_name has the same diagnostic,
6381 // thus make sure to emit it at most once.
6382 if (cp_parser_uncommitted_to_tentative_parse_p
6383 (parser))
6385 error_at (token->location,
6386 "reference to %qD is ambiguous",
6387 token->u.value);
6388 print_candidates (ambiguous_decls);
6390 decl = error_mark_node;
6392 else
6394 if (cxx_dialect != cxx98)
6395 cp_parser_name_lookup_error
6396 (parser, token->u.value, decl, NLE_NOT_CXX98,
6397 token->location);
6398 else
6399 cp_parser_name_lookup_error
6400 (parser, token->u.value, decl, NLE_CXX98,
6401 token->location);
6404 parser->scope = error_mark_node;
6405 error_p = true;
6406 /* Treat this as a successful nested-name-specifier
6407 due to:
6409 [basic.lookup.qual]
6411 If the name found is not a class-name (clause
6412 _class_) or namespace-name (_namespace.def_), the
6413 program is ill-formed. */
6414 success = true;
6416 cp_lexer_consume_token (parser->lexer);
6418 break;
6420 /* We've found one valid nested-name-specifier. */
6421 success = true;
6422 /* Name lookup always gives us a DECL. */
6423 if (TREE_CODE (new_scope) == TYPE_DECL)
6424 new_scope = TREE_TYPE (new_scope);
6425 /* Uses of "template" must be followed by actual templates. */
6426 if (template_keyword_p
6427 && !(CLASS_TYPE_P (new_scope)
6428 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6429 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6430 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6431 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6432 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6433 == TEMPLATE_ID_EXPR)))
6434 permerror (input_location, TYPE_P (new_scope)
6435 ? G_("%qT is not a template")
6436 : G_("%qD is not a template"),
6437 new_scope);
6438 /* If it is a class scope, try to complete it; we are about to
6439 be looking up names inside the class. */
6440 if (TYPE_P (new_scope)
6441 /* Since checking types for dependency can be expensive,
6442 avoid doing it if the type is already complete. */
6443 && !COMPLETE_TYPE_P (new_scope)
6444 /* Do not try to complete dependent types. */
6445 && !dependent_type_p (new_scope))
6447 new_scope = complete_type (new_scope);
6448 /* If it is a typedef to current class, use the current
6449 class instead, as the typedef won't have any names inside
6450 it yet. */
6451 if (!COMPLETE_TYPE_P (new_scope)
6452 && currently_open_class (new_scope))
6453 new_scope = TYPE_MAIN_VARIANT (new_scope);
6455 /* Make sure we look in the right scope the next time through
6456 the loop. */
6457 parser->scope = new_scope;
6460 /* If parsing tentatively, replace the sequence of tokens that makes
6461 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6462 token. That way, should we re-parse the token stream, we will
6463 not have to repeat the effort required to do the parse, nor will
6464 we issue duplicate error messages. */
6465 if (success && start)
6467 cp_token *token;
6469 token = cp_lexer_token_at (parser->lexer, start);
6470 /* Reset the contents of the START token. */
6471 token->type = CPP_NESTED_NAME_SPECIFIER;
6472 /* Retrieve any deferred checks. Do not pop this access checks yet
6473 so the memory will not be reclaimed during token replacing below. */
6474 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6475 token->u.tree_check_value->value = parser->scope;
6476 token->u.tree_check_value->checks = get_deferred_access_checks ();
6477 token->u.tree_check_value->qualifying_scope =
6478 parser->qualifying_scope;
6479 token->keyword = RID_MAX;
6481 /* Purge all subsequent tokens. */
6482 cp_lexer_purge_tokens_after (parser->lexer, start);
6485 if (start)
6486 pop_to_parent_deferring_access_checks ();
6488 return success ? parser->scope : NULL_TREE;
6491 /* Parse a nested-name-specifier. See
6492 cp_parser_nested_name_specifier_opt for details. This function
6493 behaves identically, except that it will an issue an error if no
6494 nested-name-specifier is present. */
6496 static tree
6497 cp_parser_nested_name_specifier (cp_parser *parser,
6498 bool typename_keyword_p,
6499 bool check_dependency_p,
6500 bool type_p,
6501 bool is_declaration)
6503 tree scope;
6505 /* Look for the nested-name-specifier. */
6506 scope = cp_parser_nested_name_specifier_opt (parser,
6507 typename_keyword_p,
6508 check_dependency_p,
6509 type_p,
6510 is_declaration);
6511 /* If it was not present, issue an error message. */
6512 if (!scope)
6514 cp_parser_error (parser, "expected nested-name-specifier");
6515 parser->scope = NULL_TREE;
6518 return scope;
6521 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6522 this is either a class-name or a namespace-name (which corresponds
6523 to the class-or-namespace-name production in the grammar). For
6524 C++0x, it can also be a type-name that refers to an enumeration
6525 type or a simple-template-id.
6527 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6528 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6529 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6530 TYPE_P is TRUE iff the next name should be taken as a class-name,
6531 even the same name is declared to be another entity in the same
6532 scope.
6534 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6535 specified by the class-or-namespace-name. If neither is found the
6536 ERROR_MARK_NODE is returned. */
6538 static tree
6539 cp_parser_qualifying_entity (cp_parser *parser,
6540 bool typename_keyword_p,
6541 bool template_keyword_p,
6542 bool check_dependency_p,
6543 bool type_p,
6544 bool is_declaration)
6546 tree saved_scope;
6547 tree saved_qualifying_scope;
6548 tree saved_object_scope;
6549 tree scope;
6550 bool only_class_p;
6551 bool successful_parse_p;
6553 /* DR 743: decltype can appear in a nested-name-specifier. */
6554 if (cp_lexer_next_token_is_decltype (parser->lexer))
6556 scope = cp_parser_decltype (parser);
6557 if (TREE_CODE (scope) != ENUMERAL_TYPE
6558 && !MAYBE_CLASS_TYPE_P (scope))
6560 cp_parser_simulate_error (parser);
6561 return error_mark_node;
6563 if (TYPE_NAME (scope))
6564 scope = TYPE_NAME (scope);
6565 return scope;
6568 /* Before we try to parse the class-name, we must save away the
6569 current PARSER->SCOPE since cp_parser_class_name will destroy
6570 it. */
6571 saved_scope = parser->scope;
6572 saved_qualifying_scope = parser->qualifying_scope;
6573 saved_object_scope = parser->object_scope;
6574 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6575 there is no need to look for a namespace-name. */
6576 only_class_p = template_keyword_p
6577 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6578 if (!only_class_p)
6579 cp_parser_parse_tentatively (parser);
6580 scope = cp_parser_class_name (parser,
6581 typename_keyword_p,
6582 template_keyword_p,
6583 type_p ? class_type : none_type,
6584 check_dependency_p,
6585 /*class_head_p=*/false,
6586 is_declaration,
6587 /*enum_ok=*/cxx_dialect > cxx98);
6588 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6589 /* If that didn't work, try for a namespace-name. */
6590 if (!only_class_p && !successful_parse_p)
6592 /* Restore the saved scope. */
6593 parser->scope = saved_scope;
6594 parser->qualifying_scope = saved_qualifying_scope;
6595 parser->object_scope = saved_object_scope;
6596 /* If we are not looking at an identifier followed by the scope
6597 resolution operator, then this is not part of a
6598 nested-name-specifier. (Note that this function is only used
6599 to parse the components of a nested-name-specifier.) */
6600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6601 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6602 return error_mark_node;
6603 scope = cp_parser_namespace_name (parser);
6606 return scope;
6609 /* Return true if we are looking at a compound-literal, false otherwise. */
6611 static bool
6612 cp_parser_compound_literal_p (cp_parser *parser)
6614 cp_lexer_save_tokens (parser->lexer);
6616 /* Skip tokens until the next token is a closing parenthesis.
6617 If we find the closing `)', and the next token is a `{', then
6618 we are looking at a compound-literal. */
6619 bool compound_literal_p
6620 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6621 /*consume_paren=*/true)
6622 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6624 /* Roll back the tokens we skipped. */
6625 cp_lexer_rollback_tokens (parser->lexer);
6627 return compound_literal_p;
6630 /* Return true if EXPR is the integer constant zero or a complex constant
6631 of zero, without any folding, but ignoring location wrappers. */
6633 static bool
6634 literal_integer_zerop (const_tree expr)
6636 STRIP_ANY_LOCATION_WRAPPER (expr);
6637 return integer_zerop (expr);
6640 /* Parse a postfix-expression.
6642 postfix-expression:
6643 primary-expression
6644 postfix-expression [ expression ]
6645 postfix-expression ( expression-list [opt] )
6646 simple-type-specifier ( expression-list [opt] )
6647 typename :: [opt] nested-name-specifier identifier
6648 ( expression-list [opt] )
6649 typename :: [opt] nested-name-specifier template [opt] template-id
6650 ( expression-list [opt] )
6651 postfix-expression . template [opt] id-expression
6652 postfix-expression -> template [opt] id-expression
6653 postfix-expression . pseudo-destructor-name
6654 postfix-expression -> pseudo-destructor-name
6655 postfix-expression ++
6656 postfix-expression --
6657 dynamic_cast < type-id > ( expression )
6658 static_cast < type-id > ( expression )
6659 reinterpret_cast < type-id > ( expression )
6660 const_cast < type-id > ( expression )
6661 typeid ( expression )
6662 typeid ( type-id )
6664 GNU Extension:
6666 postfix-expression:
6667 ( type-id ) { initializer-list , [opt] }
6669 This extension is a GNU version of the C99 compound-literal
6670 construct. (The C99 grammar uses `type-name' instead of `type-id',
6671 but they are essentially the same concept.)
6673 If ADDRESS_P is true, the postfix expression is the operand of the
6674 `&' operator. CAST_P is true if this expression is the target of a
6675 cast.
6677 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6678 class member access expressions [expr.ref].
6680 Returns a representation of the expression. */
6682 static cp_expr
6683 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6684 bool member_access_only_p, bool decltype_p,
6685 cp_id_kind * pidk_return)
6687 cp_token *token;
6688 location_t loc;
6689 enum rid keyword;
6690 cp_id_kind idk = CP_ID_KIND_NONE;
6691 cp_expr postfix_expression = NULL_TREE;
6692 bool is_member_access = false;
6694 /* Peek at the next token. */
6695 token = cp_lexer_peek_token (parser->lexer);
6696 loc = token->location;
6697 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6699 /* Some of the productions are determined by keywords. */
6700 keyword = token->keyword;
6701 switch (keyword)
6703 case RID_DYNCAST:
6704 case RID_STATCAST:
6705 case RID_REINTCAST:
6706 case RID_CONSTCAST:
6708 tree type;
6709 cp_expr expression;
6710 const char *saved_message;
6711 bool saved_in_type_id_in_expr_p;
6713 /* All of these can be handled in the same way from the point
6714 of view of parsing. Begin by consuming the token
6715 identifying the cast. */
6716 cp_lexer_consume_token (parser->lexer);
6718 /* New types cannot be defined in the cast. */
6719 saved_message = parser->type_definition_forbidden_message;
6720 parser->type_definition_forbidden_message
6721 = G_("types may not be defined in casts");
6723 /* Look for the opening `<'. */
6724 cp_parser_require (parser, CPP_LESS, RT_LESS);
6725 /* Parse the type to which we are casting. */
6726 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6727 parser->in_type_id_in_expr_p = true;
6728 type = cp_parser_type_id (parser);
6729 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6730 /* Look for the closing `>'. */
6731 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6732 /* Restore the old message. */
6733 parser->type_definition_forbidden_message = saved_message;
6735 bool saved_greater_than_is_operator_p
6736 = parser->greater_than_is_operator_p;
6737 parser->greater_than_is_operator_p = true;
6739 /* And the expression which is being cast. */
6740 matching_parens parens;
6741 parens.require_open (parser);
6742 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6743 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6744 RT_CLOSE_PAREN);
6745 location_t end_loc = close_paren ?
6746 close_paren->location : UNKNOWN_LOCATION;
6748 parser->greater_than_is_operator_p
6749 = saved_greater_than_is_operator_p;
6751 /* Only type conversions to integral or enumeration types
6752 can be used in constant-expressions. */
6753 if (!cast_valid_in_integral_constant_expression_p (type)
6754 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6756 postfix_expression = error_mark_node;
6757 break;
6760 switch (keyword)
6762 case RID_DYNCAST:
6763 postfix_expression
6764 = build_dynamic_cast (type, expression, tf_warning_or_error);
6765 break;
6766 case RID_STATCAST:
6767 postfix_expression
6768 = build_static_cast (type, expression, tf_warning_or_error);
6769 break;
6770 case RID_REINTCAST:
6771 postfix_expression
6772 = build_reinterpret_cast (type, expression,
6773 tf_warning_or_error);
6774 break;
6775 case RID_CONSTCAST:
6776 postfix_expression
6777 = build_const_cast (type, expression, tf_warning_or_error);
6778 break;
6779 default:
6780 gcc_unreachable ();
6783 /* Construct a location e.g. :
6784 reinterpret_cast <int *> (expr)
6785 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6786 ranging from the start of the "*_cast" token to the final closing
6787 paren, with the caret at the start. */
6788 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6789 postfix_expression.set_location (cp_cast_loc);
6791 break;
6793 case RID_TYPEID:
6795 tree type;
6796 const char *saved_message;
6797 bool saved_in_type_id_in_expr_p;
6799 /* Consume the `typeid' token. */
6800 cp_lexer_consume_token (parser->lexer);
6801 /* Look for the `(' token. */
6802 matching_parens parens;
6803 parens.require_open (parser);
6804 /* Types cannot be defined in a `typeid' expression. */
6805 saved_message = parser->type_definition_forbidden_message;
6806 parser->type_definition_forbidden_message
6807 = G_("types may not be defined in a %<typeid%> expression");
6808 /* We can't be sure yet whether we're looking at a type-id or an
6809 expression. */
6810 cp_parser_parse_tentatively (parser);
6811 /* Try a type-id first. */
6812 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6813 parser->in_type_id_in_expr_p = true;
6814 type = cp_parser_type_id (parser);
6815 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6816 /* Look for the `)' token. Otherwise, we can't be sure that
6817 we're not looking at an expression: consider `typeid (int
6818 (3))', for example. */
6819 cp_token *close_paren = parens.require_close (parser);
6820 /* If all went well, simply lookup the type-id. */
6821 if (cp_parser_parse_definitely (parser))
6822 postfix_expression = get_typeid (type, tf_warning_or_error);
6823 /* Otherwise, fall back to the expression variant. */
6824 else
6826 tree expression;
6828 /* Look for an expression. */
6829 expression = cp_parser_expression (parser, & idk);
6830 /* Compute its typeid. */
6831 postfix_expression = build_typeid (expression, tf_warning_or_error);
6832 /* Look for the `)' token. */
6833 close_paren = parens.require_close (parser);
6835 /* Restore the saved message. */
6836 parser->type_definition_forbidden_message = saved_message;
6837 /* `typeid' may not appear in an integral constant expression. */
6838 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6839 postfix_expression = error_mark_node;
6841 /* Construct a location e.g. :
6842 typeid (expr)
6843 ^~~~~~~~~~~~~
6844 ranging from the start of the "typeid" token to the final closing
6845 paren, with the caret at the start. */
6846 if (close_paren)
6848 location_t typeid_loc
6849 = make_location (start_loc, start_loc, close_paren->location);
6850 postfix_expression.set_location (typeid_loc);
6851 postfix_expression.maybe_add_location_wrapper ();
6854 break;
6856 case RID_TYPENAME:
6858 tree type;
6859 /* The syntax permitted here is the same permitted for an
6860 elaborated-type-specifier. */
6861 ++parser->prevent_constrained_type_specifiers;
6862 type = cp_parser_elaborated_type_specifier (parser,
6863 /*is_friend=*/false,
6864 /*is_declaration=*/false);
6865 --parser->prevent_constrained_type_specifiers;
6866 postfix_expression = cp_parser_functional_cast (parser, type);
6868 break;
6870 case RID_ADDRESSOF:
6871 case RID_BUILTIN_SHUFFLE:
6872 case RID_BUILTIN_LAUNDER:
6874 vec<tree, va_gc> *vec;
6875 unsigned int i;
6876 tree p;
6878 cp_lexer_consume_token (parser->lexer);
6879 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6880 /*cast_p=*/false, /*allow_expansion_p=*/true,
6881 /*non_constant_p=*/NULL);
6882 if (vec == NULL)
6884 postfix_expression = error_mark_node;
6885 break;
6888 FOR_EACH_VEC_ELT (*vec, i, p)
6889 mark_exp_read (p);
6891 switch (keyword)
6893 case RID_ADDRESSOF:
6894 if (vec->length () == 1)
6895 postfix_expression
6896 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6897 else
6899 error_at (loc, "wrong number of arguments to "
6900 "%<__builtin_addressof%>");
6901 postfix_expression = error_mark_node;
6903 break;
6905 case RID_BUILTIN_LAUNDER:
6906 if (vec->length () == 1)
6907 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6908 tf_warning_or_error);
6909 else
6911 error_at (loc, "wrong number of arguments to "
6912 "%<__builtin_launder%>");
6913 postfix_expression = error_mark_node;
6915 break;
6917 case RID_BUILTIN_SHUFFLE:
6918 if (vec->length () == 2)
6919 postfix_expression
6920 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6921 (*vec)[1], tf_warning_or_error);
6922 else if (vec->length () == 3)
6923 postfix_expression
6924 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6925 (*vec)[2], tf_warning_or_error);
6926 else
6928 error_at (loc, "wrong number of arguments to "
6929 "%<__builtin_shuffle%>");
6930 postfix_expression = error_mark_node;
6932 break;
6934 default:
6935 gcc_unreachable ();
6937 break;
6940 default:
6942 tree type;
6944 /* If the next thing is a simple-type-specifier, we may be
6945 looking at a functional cast. We could also be looking at
6946 an id-expression. So, we try the functional cast, and if
6947 that doesn't work we fall back to the primary-expression. */
6948 cp_parser_parse_tentatively (parser);
6949 /* Look for the simple-type-specifier. */
6950 ++parser->prevent_constrained_type_specifiers;
6951 type = cp_parser_simple_type_specifier (parser,
6952 /*decl_specs=*/NULL,
6953 CP_PARSER_FLAGS_NONE);
6954 --parser->prevent_constrained_type_specifiers;
6955 /* Parse the cast itself. */
6956 if (!cp_parser_error_occurred (parser))
6957 postfix_expression
6958 = cp_parser_functional_cast (parser, type);
6959 /* If that worked, we're done. */
6960 if (cp_parser_parse_definitely (parser))
6961 break;
6963 /* If the functional-cast didn't work out, try a
6964 compound-literal. */
6965 if (cp_parser_allow_gnu_extensions_p (parser)
6966 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6968 cp_expr initializer = NULL_TREE;
6970 cp_parser_parse_tentatively (parser);
6972 matching_parens parens;
6973 parens.consume_open (parser);
6975 /* Avoid calling cp_parser_type_id pointlessly, see comment
6976 in cp_parser_cast_expression about c++/29234. */
6977 if (!cp_parser_compound_literal_p (parser))
6978 cp_parser_simulate_error (parser);
6979 else
6981 /* Parse the type. */
6982 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6983 parser->in_type_id_in_expr_p = true;
6984 type = cp_parser_type_id (parser);
6985 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6986 parens.require_close (parser);
6989 /* If things aren't going well, there's no need to
6990 keep going. */
6991 if (!cp_parser_error_occurred (parser))
6993 bool non_constant_p;
6994 /* Parse the brace-enclosed initializer list. */
6995 initializer = cp_parser_braced_list (parser,
6996 &non_constant_p);
6998 /* If that worked, we're definitely looking at a
6999 compound-literal expression. */
7000 if (cp_parser_parse_definitely (parser))
7002 /* Warn the user that a compound literal is not
7003 allowed in standard C++. */
7004 pedwarn (input_location, OPT_Wpedantic,
7005 "ISO C++ forbids compound-literals");
7006 /* For simplicity, we disallow compound literals in
7007 constant-expressions. We could
7008 allow compound literals of integer type, whose
7009 initializer was a constant, in constant
7010 expressions. Permitting that usage, as a further
7011 extension, would not change the meaning of any
7012 currently accepted programs. (Of course, as
7013 compound literals are not part of ISO C++, the
7014 standard has nothing to say.) */
7015 if (cp_parser_non_integral_constant_expression (parser,
7016 NIC_NCC))
7018 postfix_expression = error_mark_node;
7019 break;
7021 /* Form the representation of the compound-literal. */
7022 postfix_expression
7023 = finish_compound_literal (type, initializer,
7024 tf_warning_or_error, fcl_c99);
7025 postfix_expression.set_location (initializer.get_location ());
7026 break;
7030 /* It must be a primary-expression. */
7031 postfix_expression
7032 = cp_parser_primary_expression (parser, address_p, cast_p,
7033 /*template_arg_p=*/false,
7034 decltype_p,
7035 &idk);
7037 break;
7040 /* Note that we don't need to worry about calling build_cplus_new on a
7041 class-valued CALL_EXPR in decltype when it isn't the end of the
7042 postfix-expression; unary_complex_lvalue will take care of that for
7043 all these cases. */
7045 /* Keep looping until the postfix-expression is complete. */
7046 while (true)
7048 if (idk == CP_ID_KIND_UNQUALIFIED
7049 && identifier_p (postfix_expression)
7050 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7051 /* It is not a Koenig lookup function call. */
7052 postfix_expression
7053 = unqualified_name_lookup_error (postfix_expression);
7055 /* Peek at the next token. */
7056 token = cp_lexer_peek_token (parser->lexer);
7058 switch (token->type)
7060 case CPP_OPEN_SQUARE:
7061 if (cp_next_tokens_can_be_std_attribute_p (parser))
7063 cp_parser_error (parser,
7064 "two consecutive %<[%> shall "
7065 "only introduce an attribute");
7066 return error_mark_node;
7068 postfix_expression
7069 = cp_parser_postfix_open_square_expression (parser,
7070 postfix_expression,
7071 false,
7072 decltype_p);
7073 postfix_expression.set_range (start_loc,
7074 postfix_expression.get_location ());
7076 idk = CP_ID_KIND_NONE;
7077 is_member_access = false;
7078 break;
7080 case CPP_OPEN_PAREN:
7081 /* postfix-expression ( expression-list [opt] ) */
7083 bool koenig_p;
7084 bool is_builtin_constant_p;
7085 bool saved_integral_constant_expression_p = false;
7086 bool saved_non_integral_constant_expression_p = false;
7087 tsubst_flags_t complain = complain_flags (decltype_p);
7088 vec<tree, va_gc> *args;
7089 location_t close_paren_loc = UNKNOWN_LOCATION;
7091 is_member_access = false;
7093 is_builtin_constant_p
7094 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7095 if (is_builtin_constant_p)
7097 /* The whole point of __builtin_constant_p is to allow
7098 non-constant expressions to appear as arguments. */
7099 saved_integral_constant_expression_p
7100 = parser->integral_constant_expression_p;
7101 saved_non_integral_constant_expression_p
7102 = parser->non_integral_constant_expression_p;
7103 parser->integral_constant_expression_p = false;
7105 args = (cp_parser_parenthesized_expression_list
7106 (parser, non_attr,
7107 /*cast_p=*/false, /*allow_expansion_p=*/true,
7108 /*non_constant_p=*/NULL,
7109 /*close_paren_loc=*/&close_paren_loc,
7110 /*wrap_locations_p=*/true));
7111 if (is_builtin_constant_p)
7113 parser->integral_constant_expression_p
7114 = saved_integral_constant_expression_p;
7115 parser->non_integral_constant_expression_p
7116 = saved_non_integral_constant_expression_p;
7119 if (args == NULL)
7121 postfix_expression = error_mark_node;
7122 break;
7125 /* Function calls are not permitted in
7126 constant-expressions. */
7127 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7128 && cp_parser_non_integral_constant_expression (parser,
7129 NIC_FUNC_CALL))
7131 postfix_expression = error_mark_node;
7132 release_tree_vector (args);
7133 break;
7136 koenig_p = false;
7137 if (idk == CP_ID_KIND_UNQUALIFIED
7138 || idk == CP_ID_KIND_TEMPLATE_ID)
7140 if (identifier_p (postfix_expression))
7142 if (!args->is_empty ())
7144 koenig_p = true;
7145 if (!any_type_dependent_arguments_p (args))
7146 postfix_expression
7147 = perform_koenig_lookup (postfix_expression, args,
7148 complain);
7150 else
7151 postfix_expression
7152 = unqualified_fn_lookup_error (postfix_expression);
7154 /* We do not perform argument-dependent lookup if
7155 normal lookup finds a non-function, in accordance
7156 with the expected resolution of DR 218. */
7157 else if (!args->is_empty ()
7158 && is_overloaded_fn (postfix_expression))
7160 tree fn = get_first_fn (postfix_expression);
7161 fn = STRIP_TEMPLATE (fn);
7163 /* Do not do argument dependent lookup if regular
7164 lookup finds a member function or a block-scope
7165 function declaration. [basic.lookup.argdep]/3 */
7166 if (!DECL_FUNCTION_MEMBER_P (fn)
7167 && !DECL_LOCAL_FUNCTION_P (fn))
7169 koenig_p = true;
7170 if (!any_type_dependent_arguments_p (args))
7171 postfix_expression
7172 = perform_koenig_lookup (postfix_expression, args,
7173 complain);
7178 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7179 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7180 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7181 && vec_safe_length (args) == 3)
7183 tree arg0 = (*args)[0];
7184 tree arg1 = (*args)[1];
7185 tree arg2 = (*args)[2];
7186 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7187 | (literal_integer_zerop (arg2) << 2));
7188 warn_for_memset (input_location, arg0, arg2, literal_mask);
7191 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7193 tree instance = TREE_OPERAND (postfix_expression, 0);
7194 tree fn = TREE_OPERAND (postfix_expression, 1);
7196 if (processing_template_decl
7197 && (type_dependent_object_expression_p (instance)
7198 || (!BASELINK_P (fn)
7199 && TREE_CODE (fn) != FIELD_DECL)
7200 || type_dependent_expression_p (fn)
7201 || any_type_dependent_arguments_p (args)))
7203 maybe_generic_this_capture (instance, fn);
7204 postfix_expression
7205 = build_min_nt_call_vec (postfix_expression, args);
7206 release_tree_vector (args);
7207 break;
7210 if (BASELINK_P (fn))
7212 postfix_expression
7213 = (build_new_method_call
7214 (instance, fn, &args, NULL_TREE,
7215 (idk == CP_ID_KIND_QUALIFIED
7216 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7217 : LOOKUP_NORMAL),
7218 /*fn_p=*/NULL,
7219 complain));
7221 else
7222 postfix_expression
7223 = finish_call_expr (postfix_expression, &args,
7224 /*disallow_virtual=*/false,
7225 /*koenig_p=*/false,
7226 complain);
7228 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7229 || TREE_CODE (postfix_expression) == MEMBER_REF
7230 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7231 postfix_expression = (build_offset_ref_call_from_tree
7232 (postfix_expression, &args,
7233 complain));
7234 else if (idk == CP_ID_KIND_QUALIFIED)
7235 /* A call to a static class member, or a namespace-scope
7236 function. */
7237 postfix_expression
7238 = finish_call_expr (postfix_expression, &args,
7239 /*disallow_virtual=*/true,
7240 koenig_p,
7241 complain);
7242 else
7243 /* All other function calls. */
7244 postfix_expression
7245 = finish_call_expr (postfix_expression, &args,
7246 /*disallow_virtual=*/false,
7247 koenig_p,
7248 complain);
7250 if (close_paren_loc != UNKNOWN_LOCATION)
7252 location_t combined_loc = make_location (token->location,
7253 start_loc,
7254 close_paren_loc);
7255 postfix_expression.set_location (combined_loc);
7258 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7259 idk = CP_ID_KIND_NONE;
7261 release_tree_vector (args);
7263 break;
7265 case CPP_DOT:
7266 case CPP_DEREF:
7267 /* postfix-expression . template [opt] id-expression
7268 postfix-expression . pseudo-destructor-name
7269 postfix-expression -> template [opt] id-expression
7270 postfix-expression -> pseudo-destructor-name */
7272 /* Consume the `.' or `->' operator. */
7273 cp_lexer_consume_token (parser->lexer);
7275 postfix_expression
7276 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7277 postfix_expression,
7278 false, &idk, loc);
7280 is_member_access = true;
7281 break;
7283 case CPP_PLUS_PLUS:
7284 /* postfix-expression ++ */
7285 /* Consume the `++' token. */
7286 cp_lexer_consume_token (parser->lexer);
7287 /* Generate a representation for the complete expression. */
7288 postfix_expression
7289 = finish_increment_expr (postfix_expression,
7290 POSTINCREMENT_EXPR);
7291 /* Increments may not appear in constant-expressions. */
7292 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7293 postfix_expression = error_mark_node;
7294 idk = CP_ID_KIND_NONE;
7295 is_member_access = false;
7296 break;
7298 case CPP_MINUS_MINUS:
7299 /* postfix-expression -- */
7300 /* Consume the `--' token. */
7301 cp_lexer_consume_token (parser->lexer);
7302 /* Generate a representation for the complete expression. */
7303 postfix_expression
7304 = finish_increment_expr (postfix_expression,
7305 POSTDECREMENT_EXPR);
7306 /* Decrements may not appear in constant-expressions. */
7307 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7308 postfix_expression = error_mark_node;
7309 idk = CP_ID_KIND_NONE;
7310 is_member_access = false;
7311 break;
7313 default:
7314 if (pidk_return != NULL)
7315 * pidk_return = idk;
7316 if (member_access_only_p)
7317 return is_member_access
7318 ? postfix_expression
7319 : cp_expr (error_mark_node);
7320 else
7321 return postfix_expression;
7325 /* We should never get here. */
7326 gcc_unreachable ();
7327 return error_mark_node;
7330 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7331 by cp_parser_builtin_offsetof. We're looking for
7333 postfix-expression [ expression ]
7334 postfix-expression [ braced-init-list ] (C++11)
7336 FOR_OFFSETOF is set if we're being called in that context, which
7337 changes how we deal with integer constant expressions. */
7339 static tree
7340 cp_parser_postfix_open_square_expression (cp_parser *parser,
7341 tree postfix_expression,
7342 bool for_offsetof,
7343 bool decltype_p)
7345 tree index = NULL_TREE;
7346 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7347 bool saved_greater_than_is_operator_p;
7349 /* Consume the `[' token. */
7350 cp_lexer_consume_token (parser->lexer);
7352 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7353 parser->greater_than_is_operator_p = true;
7355 /* Parse the index expression. */
7356 /* ??? For offsetof, there is a question of what to allow here. If
7357 offsetof is not being used in an integral constant expression context,
7358 then we *could* get the right answer by computing the value at runtime.
7359 If we are in an integral constant expression context, then we might
7360 could accept any constant expression; hard to say without analysis.
7361 Rather than open the barn door too wide right away, allow only integer
7362 constant expressions here. */
7363 if (for_offsetof)
7364 index = cp_parser_constant_expression (parser);
7365 else
7367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7369 bool expr_nonconst_p;
7370 cp_lexer_set_source_position (parser->lexer);
7371 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7372 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7374 else
7375 index = cp_parser_expression (parser);
7378 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7380 /* Look for the closing `]'. */
7381 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7383 /* Build the ARRAY_REF. */
7384 postfix_expression = grok_array_decl (loc, postfix_expression,
7385 index, decltype_p);
7387 /* When not doing offsetof, array references are not permitted in
7388 constant-expressions. */
7389 if (!for_offsetof
7390 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7391 postfix_expression = error_mark_node;
7393 return postfix_expression;
7396 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7397 dereference of incomplete type, returns true if error_mark_node should
7398 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7399 and *DEPENDENT_P. */
7401 bool
7402 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7403 bool *dependent_p)
7405 /* In a template, be permissive by treating an object expression
7406 of incomplete type as dependent (after a pedwarn). */
7407 diagnostic_t kind = (processing_template_decl
7408 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7410 switch (TREE_CODE (*postfix_expression))
7412 case CAST_EXPR:
7413 case REINTERPRET_CAST_EXPR:
7414 case CONST_CAST_EXPR:
7415 case STATIC_CAST_EXPR:
7416 case DYNAMIC_CAST_EXPR:
7417 case IMPLICIT_CONV_EXPR:
7418 case VIEW_CONVERT_EXPR:
7419 case NON_LVALUE_EXPR:
7420 kind = DK_ERROR;
7421 break;
7422 case OVERLOAD:
7423 /* Don't emit any diagnostic for OVERLOADs. */
7424 kind = DK_IGNORED;
7425 break;
7426 default:
7427 /* Avoid clobbering e.g. DECLs. */
7428 if (!EXPR_P (*postfix_expression))
7429 kind = DK_ERROR;
7430 break;
7433 if (kind == DK_IGNORED)
7434 return false;
7436 location_t exploc = location_of (*postfix_expression);
7437 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7438 if (!MAYBE_CLASS_TYPE_P (*scope))
7439 return true;
7440 if (kind == DK_ERROR)
7441 *scope = *postfix_expression = error_mark_node;
7442 else if (processing_template_decl)
7444 *dependent_p = true;
7445 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7447 return false;
7450 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7451 by cp_parser_builtin_offsetof. We're looking for
7453 postfix-expression . template [opt] id-expression
7454 postfix-expression . pseudo-destructor-name
7455 postfix-expression -> template [opt] id-expression
7456 postfix-expression -> pseudo-destructor-name
7458 FOR_OFFSETOF is set if we're being called in that context. That sorta
7459 limits what of the above we'll actually accept, but nevermind.
7460 TOKEN_TYPE is the "." or "->" token, which will already have been
7461 removed from the stream. */
7463 static tree
7464 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7465 enum cpp_ttype token_type,
7466 cp_expr postfix_expression,
7467 bool for_offsetof, cp_id_kind *idk,
7468 location_t location)
7470 tree name;
7471 bool dependent_p;
7472 bool pseudo_destructor_p;
7473 tree scope = NULL_TREE;
7474 location_t start_loc = postfix_expression.get_start ();
7476 /* If this is a `->' operator, dereference the pointer. */
7477 if (token_type == CPP_DEREF)
7478 postfix_expression = build_x_arrow (location, postfix_expression,
7479 tf_warning_or_error);
7480 /* Check to see whether or not the expression is type-dependent and
7481 not the current instantiation. */
7482 dependent_p = type_dependent_object_expression_p (postfix_expression);
7483 /* The identifier following the `->' or `.' is not qualified. */
7484 parser->scope = NULL_TREE;
7485 parser->qualifying_scope = NULL_TREE;
7486 parser->object_scope = NULL_TREE;
7487 *idk = CP_ID_KIND_NONE;
7489 /* Enter the scope corresponding to the type of the object
7490 given by the POSTFIX_EXPRESSION. */
7491 if (!dependent_p)
7493 scope = TREE_TYPE (postfix_expression);
7494 /* According to the standard, no expression should ever have
7495 reference type. Unfortunately, we do not currently match
7496 the standard in this respect in that our internal representation
7497 of an expression may have reference type even when the standard
7498 says it does not. Therefore, we have to manually obtain the
7499 underlying type here. */
7500 scope = non_reference (scope);
7501 /* The type of the POSTFIX_EXPRESSION must be complete. */
7502 /* Unlike the object expression in other contexts, *this is not
7503 required to be of complete type for purposes of class member
7504 access (5.2.5) outside the member function body. */
7505 if (postfix_expression != current_class_ref
7506 && scope != error_mark_node
7507 && !(processing_template_decl
7508 && current_class_type
7509 && (same_type_ignoring_top_level_qualifiers_p
7510 (scope, current_class_type))))
7512 scope = complete_type (scope);
7513 if (!COMPLETE_TYPE_P (scope)
7514 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7515 &dependent_p))
7516 return error_mark_node;
7519 if (!dependent_p)
7521 /* Let the name lookup machinery know that we are processing a
7522 class member access expression. */
7523 parser->context->object_type = scope;
7524 /* If something went wrong, we want to be able to discern that case,
7525 as opposed to the case where there was no SCOPE due to the type
7526 of expression being dependent. */
7527 if (!scope)
7528 scope = error_mark_node;
7529 /* If the SCOPE was erroneous, make the various semantic analysis
7530 functions exit quickly -- and without issuing additional error
7531 messages. */
7532 if (scope == error_mark_node)
7533 postfix_expression = error_mark_node;
7537 if (dependent_p)
7538 /* Tell cp_parser_lookup_name that there was an object, even though it's
7539 type-dependent. */
7540 parser->context->object_type = unknown_type_node;
7542 /* Assume this expression is not a pseudo-destructor access. */
7543 pseudo_destructor_p = false;
7545 /* If the SCOPE is a scalar type, then, if this is a valid program,
7546 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7547 is type dependent, it can be pseudo-destructor-name or something else.
7548 Try to parse it as pseudo-destructor-name first. */
7549 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7551 tree s;
7552 tree type;
7554 cp_parser_parse_tentatively (parser);
7555 /* Parse the pseudo-destructor-name. */
7556 s = NULL_TREE;
7557 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7558 &s, &type);
7559 if (dependent_p
7560 && (cp_parser_error_occurred (parser)
7561 || !SCALAR_TYPE_P (type)))
7562 cp_parser_abort_tentative_parse (parser);
7563 else if (cp_parser_parse_definitely (parser))
7565 pseudo_destructor_p = true;
7566 postfix_expression
7567 = finish_pseudo_destructor_expr (postfix_expression,
7568 s, type, location);
7572 if (!pseudo_destructor_p)
7574 /* If the SCOPE is not a scalar type, we are looking at an
7575 ordinary class member access expression, rather than a
7576 pseudo-destructor-name. */
7577 bool template_p;
7578 cp_token *token = cp_lexer_peek_token (parser->lexer);
7579 /* Parse the id-expression. */
7580 name = (cp_parser_id_expression
7581 (parser,
7582 cp_parser_optional_template_keyword (parser),
7583 /*check_dependency_p=*/true,
7584 &template_p,
7585 /*declarator_p=*/false,
7586 /*optional_p=*/false));
7587 /* In general, build a SCOPE_REF if the member name is qualified.
7588 However, if the name was not dependent and has already been
7589 resolved; there is no need to build the SCOPE_REF. For example;
7591 struct X { void f(); };
7592 template <typename T> void f(T* t) { t->X::f(); }
7594 Even though "t" is dependent, "X::f" is not and has been resolved
7595 to a BASELINK; there is no need to include scope information. */
7597 /* But we do need to remember that there was an explicit scope for
7598 virtual function calls. */
7599 if (parser->scope)
7600 *idk = CP_ID_KIND_QUALIFIED;
7602 /* If the name is a template-id that names a type, we will get a
7603 TYPE_DECL here. That is invalid code. */
7604 if (TREE_CODE (name) == TYPE_DECL)
7606 error_at (token->location, "invalid use of %qD", name);
7607 postfix_expression = error_mark_node;
7609 else
7611 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7613 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7615 error_at (token->location, "%<%D::%D%> is not a class member",
7616 parser->scope, name);
7617 postfix_expression = error_mark_node;
7619 else
7620 name = build_qualified_name (/*type=*/NULL_TREE,
7621 parser->scope,
7622 name,
7623 template_p);
7624 parser->scope = NULL_TREE;
7625 parser->qualifying_scope = NULL_TREE;
7626 parser->object_scope = NULL_TREE;
7628 if (parser->scope && name && BASELINK_P (name))
7629 adjust_result_of_qualified_name_lookup
7630 (name, parser->scope, scope);
7631 postfix_expression
7632 = finish_class_member_access_expr (postfix_expression, name,
7633 template_p,
7634 tf_warning_or_error);
7635 /* Build a location e.g.:
7636 ptr->access_expr
7637 ~~~^~~~~~~~~~~~~
7638 where the caret is at the deref token, ranging from
7639 the start of postfix_expression to the end of the access expr. */
7640 location_t end_loc
7641 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7642 location_t combined_loc
7643 = make_location (input_location, start_loc, end_loc);
7644 protected_set_expr_location (postfix_expression, combined_loc);
7648 /* We no longer need to look up names in the scope of the object on
7649 the left-hand side of the `.' or `->' operator. */
7650 parser->context->object_type = NULL_TREE;
7652 /* Outside of offsetof, these operators may not appear in
7653 constant-expressions. */
7654 if (!for_offsetof
7655 && (cp_parser_non_integral_constant_expression
7656 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7657 postfix_expression = error_mark_node;
7659 return postfix_expression;
7662 /* Parse a parenthesized expression-list.
7664 expression-list:
7665 assignment-expression
7666 expression-list, assignment-expression
7668 attribute-list:
7669 expression-list
7670 identifier
7671 identifier, expression-list
7673 CAST_P is true if this expression is the target of a cast.
7675 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7676 argument pack.
7678 WRAP_LOCATIONS_P is true if expressions within this list for which
7679 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7680 their source locations.
7682 Returns a vector of trees. Each element is a representation of an
7683 assignment-expression. NULL is returned if the ( and or ) are
7684 missing. An empty, but allocated, vector is returned on no
7685 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7686 if we are parsing an attribute list for an attribute that wants a
7687 plain identifier argument, normal_attr for an attribute that wants
7688 an expression, or non_attr if we aren't parsing an attribute list. If
7689 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7690 not all of the expressions in the list were constant.
7691 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7692 will be written to with the location of the closing parenthesis. If
7693 an error occurs, it may or may not be written to. */
7695 static vec<tree, va_gc> *
7696 cp_parser_parenthesized_expression_list (cp_parser* parser,
7697 int is_attribute_list,
7698 bool cast_p,
7699 bool allow_expansion_p,
7700 bool *non_constant_p,
7701 location_t *close_paren_loc,
7702 bool wrap_locations_p)
7704 vec<tree, va_gc> *expression_list;
7705 bool fold_expr_p = is_attribute_list != non_attr;
7706 tree identifier = NULL_TREE;
7707 bool saved_greater_than_is_operator_p;
7709 /* Assume all the expressions will be constant. */
7710 if (non_constant_p)
7711 *non_constant_p = false;
7713 matching_parens parens;
7714 if (!parens.require_open (parser))
7715 return NULL;
7717 expression_list = make_tree_vector ();
7719 /* Within a parenthesized expression, a `>' token is always
7720 the greater-than operator. */
7721 saved_greater_than_is_operator_p
7722 = parser->greater_than_is_operator_p;
7723 parser->greater_than_is_operator_p = true;
7725 cp_expr expr (NULL_TREE);
7727 /* Consume expressions until there are no more. */
7728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7729 while (true)
7731 /* At the beginning of attribute lists, check to see if the
7732 next token is an identifier. */
7733 if (is_attribute_list == id_attr
7734 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7736 cp_token *token;
7738 /* Consume the identifier. */
7739 token = cp_lexer_consume_token (parser->lexer);
7740 /* Save the identifier. */
7741 identifier = token->u.value;
7743 else
7745 bool expr_non_constant_p;
7747 /* Parse the next assignment-expression. */
7748 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7750 /* A braced-init-list. */
7751 cp_lexer_set_source_position (parser->lexer);
7752 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7753 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7754 if (non_constant_p && expr_non_constant_p)
7755 *non_constant_p = true;
7757 else if (non_constant_p)
7759 expr = (cp_parser_constant_expression
7760 (parser, /*allow_non_constant_p=*/true,
7761 &expr_non_constant_p));
7762 if (expr_non_constant_p)
7763 *non_constant_p = true;
7765 else
7766 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7767 cast_p);
7769 if (fold_expr_p)
7770 expr = instantiate_non_dependent_expr (expr);
7772 /* If we have an ellipsis, then this is an expression
7773 expansion. */
7774 if (allow_expansion_p
7775 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7777 /* Consume the `...'. */
7778 cp_lexer_consume_token (parser->lexer);
7780 /* Build the argument pack. */
7781 expr = make_pack_expansion (expr);
7784 if (wrap_locations_p)
7785 expr.maybe_add_location_wrapper ();
7787 /* Add it to the list. We add error_mark_node
7788 expressions to the list, so that we can still tell if
7789 the correct form for a parenthesized expression-list
7790 is found. That gives better errors. */
7791 vec_safe_push (expression_list, expr.get_value ());
7793 if (expr == error_mark_node)
7794 goto skip_comma;
7797 /* After the first item, attribute lists look the same as
7798 expression lists. */
7799 is_attribute_list = non_attr;
7801 get_comma:;
7802 /* If the next token isn't a `,', then we are done. */
7803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7804 break;
7806 /* Otherwise, consume the `,' and keep going. */
7807 cp_lexer_consume_token (parser->lexer);
7810 if (close_paren_loc)
7811 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7813 if (!parens.require_close (parser))
7815 int ending;
7817 skip_comma:;
7818 /* We try and resync to an unnested comma, as that will give the
7819 user better diagnostics. */
7820 ending = cp_parser_skip_to_closing_parenthesis (parser,
7821 /*recovering=*/true,
7822 /*or_comma=*/true,
7823 /*consume_paren=*/true);
7824 if (ending < 0)
7825 goto get_comma;
7826 if (!ending)
7828 parser->greater_than_is_operator_p
7829 = saved_greater_than_is_operator_p;
7830 return NULL;
7834 parser->greater_than_is_operator_p
7835 = saved_greater_than_is_operator_p;
7837 if (identifier)
7838 vec_safe_insert (expression_list, 0, identifier);
7840 return expression_list;
7843 /* Parse a pseudo-destructor-name.
7845 pseudo-destructor-name:
7846 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7847 :: [opt] nested-name-specifier template template-id :: ~ type-name
7848 :: [opt] nested-name-specifier [opt] ~ type-name
7850 If either of the first two productions is used, sets *SCOPE to the
7851 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7852 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7853 or ERROR_MARK_NODE if the parse fails. */
7855 static void
7856 cp_parser_pseudo_destructor_name (cp_parser* parser,
7857 tree object,
7858 tree* scope,
7859 tree* type)
7861 bool nested_name_specifier_p;
7863 /* Handle ~auto. */
7864 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7865 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7866 && !type_dependent_expression_p (object))
7868 if (cxx_dialect < cxx14)
7869 pedwarn (input_location, 0,
7870 "%<~auto%> only available with "
7871 "-std=c++14 or -std=gnu++14");
7872 cp_lexer_consume_token (parser->lexer);
7873 cp_lexer_consume_token (parser->lexer);
7874 *scope = NULL_TREE;
7875 *type = TREE_TYPE (object);
7876 return;
7879 /* Assume that things will not work out. */
7880 *type = error_mark_node;
7882 /* Look for the optional `::' operator. */
7883 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7884 /* Look for the optional nested-name-specifier. */
7885 nested_name_specifier_p
7886 = (cp_parser_nested_name_specifier_opt (parser,
7887 /*typename_keyword_p=*/false,
7888 /*check_dependency_p=*/true,
7889 /*type_p=*/false,
7890 /*is_declaration=*/false)
7891 != NULL_TREE);
7892 /* Now, if we saw a nested-name-specifier, we might be doing the
7893 second production. */
7894 if (nested_name_specifier_p
7895 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7897 /* Consume the `template' keyword. */
7898 cp_lexer_consume_token (parser->lexer);
7899 /* Parse the template-id. */
7900 cp_parser_template_id (parser,
7901 /*template_keyword_p=*/true,
7902 /*check_dependency_p=*/false,
7903 class_type,
7904 /*is_declaration=*/true);
7905 /* Look for the `::' token. */
7906 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7908 /* If the next token is not a `~', then there might be some
7909 additional qualification. */
7910 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7912 /* At this point, we're looking for "type-name :: ~". The type-name
7913 must not be a class-name, since this is a pseudo-destructor. So,
7914 it must be either an enum-name, or a typedef-name -- both of which
7915 are just identifiers. So, we peek ahead to check that the "::"
7916 and "~" tokens are present; if they are not, then we can avoid
7917 calling type_name. */
7918 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7919 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7920 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7922 cp_parser_error (parser, "non-scalar type");
7923 return;
7926 /* Look for the type-name. */
7927 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7928 if (*scope == error_mark_node)
7929 return;
7931 /* Look for the `::' token. */
7932 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7934 else
7935 *scope = NULL_TREE;
7937 /* Look for the `~'. */
7938 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7940 /* Once we see the ~, this has to be a pseudo-destructor. */
7941 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7942 cp_parser_commit_to_topmost_tentative_parse (parser);
7944 /* Look for the type-name again. We are not responsible for
7945 checking that it matches the first type-name. */
7946 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7949 /* Parse a unary-expression.
7951 unary-expression:
7952 postfix-expression
7953 ++ cast-expression
7954 -- cast-expression
7955 unary-operator cast-expression
7956 sizeof unary-expression
7957 sizeof ( type-id )
7958 alignof ( type-id ) [C++0x]
7959 new-expression
7960 delete-expression
7962 GNU Extensions:
7964 unary-expression:
7965 __extension__ cast-expression
7966 __alignof__ unary-expression
7967 __alignof__ ( type-id )
7968 alignof unary-expression [C++0x]
7969 __real__ cast-expression
7970 __imag__ cast-expression
7971 && identifier
7972 sizeof ( type-id ) { initializer-list , [opt] }
7973 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7974 __alignof__ ( type-id ) { initializer-list , [opt] }
7976 ADDRESS_P is true iff the unary-expression is appearing as the
7977 operand of the `&' operator. CAST_P is true if this expression is
7978 the target of a cast.
7980 Returns a representation of the expression. */
7982 static cp_expr
7983 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7984 bool address_p, bool cast_p, bool decltype_p)
7986 cp_token *token;
7987 enum tree_code unary_operator;
7989 /* Peek at the next token. */
7990 token = cp_lexer_peek_token (parser->lexer);
7991 /* Some keywords give away the kind of expression. */
7992 if (token->type == CPP_KEYWORD)
7994 enum rid keyword = token->keyword;
7996 switch (keyword)
7998 case RID_ALIGNOF:
7999 case RID_SIZEOF:
8001 tree operand, ret;
8002 enum tree_code op;
8003 location_t start_loc = token->location;
8005 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8006 /* Consume the token. */
8007 cp_lexer_consume_token (parser->lexer);
8008 /* Parse the operand. */
8009 operand = cp_parser_sizeof_operand (parser, keyword);
8011 if (TYPE_P (operand))
8012 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8013 else
8015 /* ISO C++ defines alignof only with types, not with
8016 expressions. So pedwarn if alignof is used with a non-
8017 type expression. However, __alignof__ is ok. */
8018 if (id_equal (token->u.value, "alignof"))
8019 pedwarn (token->location, OPT_Wpedantic,
8020 "ISO C++ does not allow %<alignof%> "
8021 "with a non-type");
8023 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8025 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8026 SIZEOF_EXPR with the original operand. */
8027 if (op == SIZEOF_EXPR && ret != error_mark_node)
8029 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8031 if (!processing_template_decl && TYPE_P (operand))
8033 ret = build_min (SIZEOF_EXPR, size_type_node,
8034 build1 (NOP_EXPR, operand,
8035 error_mark_node));
8036 SIZEOF_EXPR_TYPE_P (ret) = 1;
8038 else
8039 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8040 TREE_SIDE_EFFECTS (ret) = 0;
8041 TREE_READONLY (ret) = 1;
8045 /* Construct a location e.g. :
8046 alignof (expr)
8047 ^~~~~~~~~~~~~~
8048 with start == caret at the start of the "alignof"/"sizeof"
8049 token, with the endpoint at the final closing paren. */
8050 location_t finish_loc
8051 = cp_lexer_previous_token (parser->lexer)->location;
8052 location_t compound_loc
8053 = make_location (start_loc, start_loc, finish_loc);
8055 cp_expr ret_expr (ret);
8056 ret_expr.set_location (compound_loc);
8057 ret_expr = ret_expr.maybe_add_location_wrapper ();
8058 return ret_expr;
8061 case RID_NEW:
8062 return cp_parser_new_expression (parser);
8064 case RID_DELETE:
8065 return cp_parser_delete_expression (parser);
8067 case RID_EXTENSION:
8069 /* The saved value of the PEDANTIC flag. */
8070 int saved_pedantic;
8071 tree expr;
8073 /* Save away the PEDANTIC flag. */
8074 cp_parser_extension_opt (parser, &saved_pedantic);
8075 /* Parse the cast-expression. */
8076 expr = cp_parser_simple_cast_expression (parser);
8077 /* Restore the PEDANTIC flag. */
8078 pedantic = saved_pedantic;
8080 return expr;
8083 case RID_REALPART:
8084 case RID_IMAGPART:
8086 tree expression;
8088 /* Consume the `__real__' or `__imag__' token. */
8089 cp_lexer_consume_token (parser->lexer);
8090 /* Parse the cast-expression. */
8091 expression = cp_parser_simple_cast_expression (parser);
8092 /* Create the complete representation. */
8093 return build_x_unary_op (token->location,
8094 (keyword == RID_REALPART
8095 ? REALPART_EXPR : IMAGPART_EXPR),
8096 expression,
8097 tf_warning_or_error);
8099 break;
8101 case RID_TRANSACTION_ATOMIC:
8102 case RID_TRANSACTION_RELAXED:
8103 return cp_parser_transaction_expression (parser, keyword);
8105 case RID_NOEXCEPT:
8107 tree expr;
8108 const char *saved_message;
8109 bool saved_integral_constant_expression_p;
8110 bool saved_non_integral_constant_expression_p;
8111 bool saved_greater_than_is_operator_p;
8113 location_t start_loc = token->location;
8115 cp_lexer_consume_token (parser->lexer);
8116 matching_parens parens;
8117 parens.require_open (parser);
8119 saved_message = parser->type_definition_forbidden_message;
8120 parser->type_definition_forbidden_message
8121 = G_("types may not be defined in %<noexcept%> expressions");
8123 saved_integral_constant_expression_p
8124 = parser->integral_constant_expression_p;
8125 saved_non_integral_constant_expression_p
8126 = parser->non_integral_constant_expression_p;
8127 parser->integral_constant_expression_p = false;
8129 saved_greater_than_is_operator_p
8130 = parser->greater_than_is_operator_p;
8131 parser->greater_than_is_operator_p = true;
8133 ++cp_unevaluated_operand;
8134 ++c_inhibit_evaluation_warnings;
8135 ++cp_noexcept_operand;
8136 expr = cp_parser_expression (parser);
8137 --cp_noexcept_operand;
8138 --c_inhibit_evaluation_warnings;
8139 --cp_unevaluated_operand;
8141 parser->greater_than_is_operator_p
8142 = saved_greater_than_is_operator_p;
8144 parser->integral_constant_expression_p
8145 = saved_integral_constant_expression_p;
8146 parser->non_integral_constant_expression_p
8147 = saved_non_integral_constant_expression_p;
8149 parser->type_definition_forbidden_message = saved_message;
8151 location_t finish_loc
8152 = cp_lexer_peek_token (parser->lexer)->location;
8153 parens.require_close (parser);
8155 /* Construct a location of the form:
8156 noexcept (expr)
8157 ^~~~~~~~~~~~~~~
8158 with start == caret, finishing at the close-paren. */
8159 location_t noexcept_loc
8160 = make_location (start_loc, start_loc, finish_loc);
8162 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8163 noexcept_loc);
8166 default:
8167 break;
8171 /* Look for the `:: new' and `:: delete', which also signal the
8172 beginning of a new-expression, or delete-expression,
8173 respectively. If the next token is `::', then it might be one of
8174 these. */
8175 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8177 enum rid keyword;
8179 /* See if the token after the `::' is one of the keywords in
8180 which we're interested. */
8181 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8182 /* If it's `new', we have a new-expression. */
8183 if (keyword == RID_NEW)
8184 return cp_parser_new_expression (parser);
8185 /* Similarly, for `delete'. */
8186 else if (keyword == RID_DELETE)
8187 return cp_parser_delete_expression (parser);
8190 /* Look for a unary operator. */
8191 unary_operator = cp_parser_unary_operator (token);
8192 /* The `++' and `--' operators can be handled similarly, even though
8193 they are not technically unary-operators in the grammar. */
8194 if (unary_operator == ERROR_MARK)
8196 if (token->type == CPP_PLUS_PLUS)
8197 unary_operator = PREINCREMENT_EXPR;
8198 else if (token->type == CPP_MINUS_MINUS)
8199 unary_operator = PREDECREMENT_EXPR;
8200 /* Handle the GNU address-of-label extension. */
8201 else if (cp_parser_allow_gnu_extensions_p (parser)
8202 && token->type == CPP_AND_AND)
8204 tree identifier;
8205 tree expression;
8206 location_t start_loc = token->location;
8208 /* Consume the '&&' token. */
8209 cp_lexer_consume_token (parser->lexer);
8210 /* Look for the identifier. */
8211 location_t finish_loc
8212 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8213 identifier = cp_parser_identifier (parser);
8214 /* Construct a location of the form:
8215 &&label
8216 ^~~~~~~
8217 with caret==start at the "&&", finish at the end of the label. */
8218 location_t combined_loc
8219 = make_location (start_loc, start_loc, finish_loc);
8220 /* Create an expression representing the address. */
8221 expression = finish_label_address_expr (identifier, combined_loc);
8222 if (cp_parser_non_integral_constant_expression (parser,
8223 NIC_ADDR_LABEL))
8224 expression = error_mark_node;
8225 return expression;
8228 if (unary_operator != ERROR_MARK)
8230 cp_expr cast_expression;
8231 cp_expr expression = error_mark_node;
8232 non_integral_constant non_constant_p = NIC_NONE;
8233 location_t loc = token->location;
8234 tsubst_flags_t complain = complain_flags (decltype_p);
8236 /* Consume the operator token. */
8237 token = cp_lexer_consume_token (parser->lexer);
8238 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8240 /* Parse the cast-expression. */
8241 cast_expression
8242 = cp_parser_cast_expression (parser,
8243 unary_operator == ADDR_EXPR,
8244 /*cast_p=*/false,
8245 /*decltype*/false,
8246 pidk);
8248 /* Make a location:
8249 OP_TOKEN CAST_EXPRESSION
8250 ^~~~~~~~~~~~~~~~~~~~~~~~~
8251 with start==caret at the operator token, and
8252 extending to the end of the cast_expression. */
8253 loc = make_location (loc, loc, cast_expression.get_finish ());
8255 /* Now, build an appropriate representation. */
8256 switch (unary_operator)
8258 case INDIRECT_REF:
8259 non_constant_p = NIC_STAR;
8260 expression = build_x_indirect_ref (loc, cast_expression,
8261 RO_UNARY_STAR,
8262 complain);
8263 /* TODO: build_x_indirect_ref does not always honor the
8264 location, so ensure it is set. */
8265 expression.set_location (loc);
8266 break;
8268 case ADDR_EXPR:
8269 non_constant_p = NIC_ADDR;
8270 /* Fall through. */
8271 case BIT_NOT_EXPR:
8272 expression = build_x_unary_op (loc, unary_operator,
8273 cast_expression,
8274 complain);
8275 /* TODO: build_x_unary_op does not always honor the location,
8276 so ensure it is set. */
8277 expression.set_location (loc);
8278 break;
8280 case PREINCREMENT_EXPR:
8281 case PREDECREMENT_EXPR:
8282 non_constant_p = unary_operator == PREINCREMENT_EXPR
8283 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8284 /* Fall through. */
8285 case NEGATE_EXPR:
8286 /* Immediately fold negation of a constant, unless the constant is 0
8287 (since -0 == 0) or it would overflow. */
8288 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8289 && CONSTANT_CLASS_P (cast_expression)
8290 && !integer_zerop (cast_expression)
8291 && !TREE_OVERFLOW (cast_expression))
8293 tree folded = fold_build1 (unary_operator,
8294 TREE_TYPE (cast_expression),
8295 cast_expression);
8296 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8298 expression = cp_expr (folded, loc);
8299 break;
8302 /* Fall through. */
8303 case UNARY_PLUS_EXPR:
8304 case TRUTH_NOT_EXPR:
8305 expression = finish_unary_op_expr (loc, unary_operator,
8306 cast_expression, complain);
8307 break;
8309 default:
8310 gcc_unreachable ();
8313 if (non_constant_p != NIC_NONE
8314 && cp_parser_non_integral_constant_expression (parser,
8315 non_constant_p))
8316 expression = error_mark_node;
8318 return expression;
8321 return cp_parser_postfix_expression (parser, address_p, cast_p,
8322 /*member_access_only_p=*/false,
8323 decltype_p,
8324 pidk);
8327 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8328 unary-operator, the corresponding tree code is returned. */
8330 static enum tree_code
8331 cp_parser_unary_operator (cp_token* token)
8333 switch (token->type)
8335 case CPP_MULT:
8336 return INDIRECT_REF;
8338 case CPP_AND:
8339 return ADDR_EXPR;
8341 case CPP_PLUS:
8342 return UNARY_PLUS_EXPR;
8344 case CPP_MINUS:
8345 return NEGATE_EXPR;
8347 case CPP_NOT:
8348 return TRUTH_NOT_EXPR;
8350 case CPP_COMPL:
8351 return BIT_NOT_EXPR;
8353 default:
8354 return ERROR_MARK;
8358 /* Parse a new-expression.
8360 new-expression:
8361 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8362 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8364 Returns a representation of the expression. */
8366 static tree
8367 cp_parser_new_expression (cp_parser* parser)
8369 bool global_scope_p;
8370 vec<tree, va_gc> *placement;
8371 tree type;
8372 vec<tree, va_gc> *initializer;
8373 tree nelts = NULL_TREE;
8374 tree ret;
8376 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8378 /* Look for the optional `::' operator. */
8379 global_scope_p
8380 = (cp_parser_global_scope_opt (parser,
8381 /*current_scope_valid_p=*/false)
8382 != NULL_TREE);
8383 /* Look for the `new' operator. */
8384 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8385 /* There's no easy way to tell a new-placement from the
8386 `( type-id )' construct. */
8387 cp_parser_parse_tentatively (parser);
8388 /* Look for a new-placement. */
8389 placement = cp_parser_new_placement (parser);
8390 /* If that didn't work out, there's no new-placement. */
8391 if (!cp_parser_parse_definitely (parser))
8393 if (placement != NULL)
8394 release_tree_vector (placement);
8395 placement = NULL;
8398 /* If the next token is a `(', then we have a parenthesized
8399 type-id. */
8400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8402 cp_token *token;
8403 const char *saved_message = parser->type_definition_forbidden_message;
8405 /* Consume the `('. */
8406 matching_parens parens;
8407 parens.consume_open (parser);
8409 /* Parse the type-id. */
8410 parser->type_definition_forbidden_message
8411 = G_("types may not be defined in a new-expression");
8413 type_id_in_expr_sentinel s (parser);
8414 type = cp_parser_type_id (parser);
8416 parser->type_definition_forbidden_message = saved_message;
8418 /* Look for the closing `)'. */
8419 parens.require_close (parser);
8420 token = cp_lexer_peek_token (parser->lexer);
8421 /* There should not be a direct-new-declarator in this production,
8422 but GCC used to allowed this, so we check and emit a sensible error
8423 message for this case. */
8424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8426 error_at (token->location,
8427 "array bound forbidden after parenthesized type-id");
8428 inform (token->location,
8429 "try removing the parentheses around the type-id");
8430 cp_parser_direct_new_declarator (parser);
8433 /* Otherwise, there must be a new-type-id. */
8434 else
8435 type = cp_parser_new_type_id (parser, &nelts);
8437 /* If the next token is a `(' or '{', then we have a new-initializer. */
8438 cp_token *token = cp_lexer_peek_token (parser->lexer);
8439 if (token->type == CPP_OPEN_PAREN
8440 || token->type == CPP_OPEN_BRACE)
8441 initializer = cp_parser_new_initializer (parser);
8442 else
8443 initializer = NULL;
8445 /* A new-expression may not appear in an integral constant
8446 expression. */
8447 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8448 ret = error_mark_node;
8449 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8450 of a new-type-id or type-id of a new-expression, the new-expression shall
8451 contain a new-initializer of the form ( assignment-expression )".
8452 Additionally, consistently with the spirit of DR 1467, we want to accept
8453 'new auto { 2 }' too. */
8454 else if ((ret = type_uses_auto (type))
8455 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8456 && (vec_safe_length (initializer) != 1
8457 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8458 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8460 error_at (token->location,
8461 "initialization of new-expression for type %<auto%> "
8462 "requires exactly one element");
8463 ret = error_mark_node;
8465 else
8467 /* Construct a location e.g.:
8468 ptr = new int[100]
8469 ^~~~~~~~~~~~
8470 with caret == start at the start of the "new" token, and the end
8471 at the end of the final token we consumed. */
8472 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8473 location_t end_loc = get_finish (end_tok->location);
8474 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8476 /* Create a representation of the new-expression. */
8477 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8478 tf_warning_or_error);
8479 protected_set_expr_location (ret, combined_loc);
8482 if (placement != NULL)
8483 release_tree_vector (placement);
8484 if (initializer != NULL)
8485 release_tree_vector (initializer);
8487 return ret;
8490 /* Parse a new-placement.
8492 new-placement:
8493 ( expression-list )
8495 Returns the same representation as for an expression-list. */
8497 static vec<tree, va_gc> *
8498 cp_parser_new_placement (cp_parser* parser)
8500 vec<tree, va_gc> *expression_list;
8502 /* Parse the expression-list. */
8503 expression_list = (cp_parser_parenthesized_expression_list
8504 (parser, non_attr, /*cast_p=*/false,
8505 /*allow_expansion_p=*/true,
8506 /*non_constant_p=*/NULL));
8508 if (expression_list && expression_list->is_empty ())
8509 error ("expected expression-list or type-id");
8511 return expression_list;
8514 /* Parse a new-type-id.
8516 new-type-id:
8517 type-specifier-seq new-declarator [opt]
8519 Returns the TYPE allocated. If the new-type-id indicates an array
8520 type, *NELTS is set to the number of elements in the last array
8521 bound; the TYPE will not include the last array bound. */
8523 static tree
8524 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8526 cp_decl_specifier_seq type_specifier_seq;
8527 cp_declarator *new_declarator;
8528 cp_declarator *declarator;
8529 cp_declarator *outer_declarator;
8530 const char *saved_message;
8532 /* The type-specifier sequence must not contain type definitions.
8533 (It cannot contain declarations of new types either, but if they
8534 are not definitions we will catch that because they are not
8535 complete.) */
8536 saved_message = parser->type_definition_forbidden_message;
8537 parser->type_definition_forbidden_message
8538 = G_("types may not be defined in a new-type-id");
8539 /* Parse the type-specifier-seq. */
8540 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8541 /*is_trailing_return=*/false,
8542 &type_specifier_seq);
8543 /* Restore the old message. */
8544 parser->type_definition_forbidden_message = saved_message;
8546 if (type_specifier_seq.type == error_mark_node)
8547 return error_mark_node;
8549 /* Parse the new-declarator. */
8550 new_declarator = cp_parser_new_declarator_opt (parser);
8552 /* Determine the number of elements in the last array dimension, if
8553 any. */
8554 *nelts = NULL_TREE;
8555 /* Skip down to the last array dimension. */
8556 declarator = new_declarator;
8557 outer_declarator = NULL;
8558 while (declarator && (declarator->kind == cdk_pointer
8559 || declarator->kind == cdk_ptrmem))
8561 outer_declarator = declarator;
8562 declarator = declarator->declarator;
8564 while (declarator
8565 && declarator->kind == cdk_array
8566 && declarator->declarator
8567 && declarator->declarator->kind == cdk_array)
8569 outer_declarator = declarator;
8570 declarator = declarator->declarator;
8573 if (declarator && declarator->kind == cdk_array)
8575 *nelts = declarator->u.array.bounds;
8576 if (*nelts == error_mark_node)
8577 *nelts = integer_one_node;
8579 if (outer_declarator)
8580 outer_declarator->declarator = declarator->declarator;
8581 else
8582 new_declarator = NULL;
8585 return groktypename (&type_specifier_seq, new_declarator, false);
8588 /* Parse an (optional) new-declarator.
8590 new-declarator:
8591 ptr-operator new-declarator [opt]
8592 direct-new-declarator
8594 Returns the declarator. */
8596 static cp_declarator *
8597 cp_parser_new_declarator_opt (cp_parser* parser)
8599 enum tree_code code;
8600 tree type, std_attributes = NULL_TREE;
8601 cp_cv_quals cv_quals;
8603 /* We don't know if there's a ptr-operator next, or not. */
8604 cp_parser_parse_tentatively (parser);
8605 /* Look for a ptr-operator. */
8606 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8607 /* If that worked, look for more new-declarators. */
8608 if (cp_parser_parse_definitely (parser))
8610 cp_declarator *declarator;
8612 /* Parse another optional declarator. */
8613 declarator = cp_parser_new_declarator_opt (parser);
8615 declarator = cp_parser_make_indirect_declarator
8616 (code, type, cv_quals, declarator, std_attributes);
8618 return declarator;
8621 /* If the next token is a `[', there is a direct-new-declarator. */
8622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8623 return cp_parser_direct_new_declarator (parser);
8625 return NULL;
8628 /* Parse a direct-new-declarator.
8630 direct-new-declarator:
8631 [ expression ]
8632 direct-new-declarator [constant-expression]
8636 static cp_declarator *
8637 cp_parser_direct_new_declarator (cp_parser* parser)
8639 cp_declarator *declarator = NULL;
8641 while (true)
8643 tree expression;
8644 cp_token *token;
8646 /* Look for the opening `['. */
8647 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8649 token = cp_lexer_peek_token (parser->lexer);
8650 expression = cp_parser_expression (parser);
8651 /* The standard requires that the expression have integral
8652 type. DR 74 adds enumeration types. We believe that the
8653 real intent is that these expressions be handled like the
8654 expression in a `switch' condition, which also allows
8655 classes with a single conversion to integral or
8656 enumeration type. */
8657 if (!processing_template_decl)
8659 expression
8660 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8661 expression,
8662 /*complain=*/true);
8663 if (!expression)
8665 error_at (token->location,
8666 "expression in new-declarator must have integral "
8667 "or enumeration type");
8668 expression = error_mark_node;
8672 /* Look for the closing `]'. */
8673 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8675 /* Add this bound to the declarator. */
8676 declarator = make_array_declarator (declarator, expression);
8678 /* If the next token is not a `[', then there are no more
8679 bounds. */
8680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8681 break;
8684 return declarator;
8687 /* Parse a new-initializer.
8689 new-initializer:
8690 ( expression-list [opt] )
8691 braced-init-list
8693 Returns a representation of the expression-list. */
8695 static vec<tree, va_gc> *
8696 cp_parser_new_initializer (cp_parser* parser)
8698 vec<tree, va_gc> *expression_list;
8700 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8702 tree t;
8703 bool expr_non_constant_p;
8704 cp_lexer_set_source_position (parser->lexer);
8705 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8706 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8707 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8708 expression_list = make_tree_vector_single (t);
8710 else
8711 expression_list = (cp_parser_parenthesized_expression_list
8712 (parser, non_attr, /*cast_p=*/false,
8713 /*allow_expansion_p=*/true,
8714 /*non_constant_p=*/NULL));
8716 return expression_list;
8719 /* Parse a delete-expression.
8721 delete-expression:
8722 :: [opt] delete cast-expression
8723 :: [opt] delete [ ] cast-expression
8725 Returns a representation of the expression. */
8727 static tree
8728 cp_parser_delete_expression (cp_parser* parser)
8730 bool global_scope_p;
8731 bool array_p;
8732 tree expression;
8734 /* Look for the optional `::' operator. */
8735 global_scope_p
8736 = (cp_parser_global_scope_opt (parser,
8737 /*current_scope_valid_p=*/false)
8738 != NULL_TREE);
8739 /* Look for the `delete' keyword. */
8740 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8741 /* See if the array syntax is in use. */
8742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8744 /* Consume the `[' token. */
8745 cp_lexer_consume_token (parser->lexer);
8746 /* Look for the `]' token. */
8747 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8748 /* Remember that this is the `[]' construct. */
8749 array_p = true;
8751 else
8752 array_p = false;
8754 /* Parse the cast-expression. */
8755 expression = cp_parser_simple_cast_expression (parser);
8757 /* A delete-expression may not appear in an integral constant
8758 expression. */
8759 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8760 return error_mark_node;
8762 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8763 tf_warning_or_error);
8766 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8767 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8768 0 otherwise. */
8770 static int
8771 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8773 cp_token *token = cp_lexer_peek_token (parser->lexer);
8774 switch (token->type)
8776 case CPP_COMMA:
8777 case CPP_SEMICOLON:
8778 case CPP_QUERY:
8779 case CPP_COLON:
8780 case CPP_CLOSE_SQUARE:
8781 case CPP_CLOSE_PAREN:
8782 case CPP_CLOSE_BRACE:
8783 case CPP_OPEN_BRACE:
8784 case CPP_DOT:
8785 case CPP_DOT_STAR:
8786 case CPP_DEREF:
8787 case CPP_DEREF_STAR:
8788 case CPP_DIV:
8789 case CPP_MOD:
8790 case CPP_LSHIFT:
8791 case CPP_RSHIFT:
8792 case CPP_LESS:
8793 case CPP_GREATER:
8794 case CPP_LESS_EQ:
8795 case CPP_GREATER_EQ:
8796 case CPP_EQ_EQ:
8797 case CPP_NOT_EQ:
8798 case CPP_EQ:
8799 case CPP_MULT_EQ:
8800 case CPP_DIV_EQ:
8801 case CPP_MOD_EQ:
8802 case CPP_PLUS_EQ:
8803 case CPP_MINUS_EQ:
8804 case CPP_RSHIFT_EQ:
8805 case CPP_LSHIFT_EQ:
8806 case CPP_AND_EQ:
8807 case CPP_XOR_EQ:
8808 case CPP_OR_EQ:
8809 case CPP_XOR:
8810 case CPP_OR:
8811 case CPP_OR_OR:
8812 case CPP_EOF:
8813 case CPP_ELLIPSIS:
8814 return 0;
8816 case CPP_OPEN_PAREN:
8817 /* In ((type ()) () the last () isn't a valid cast-expression,
8818 so the whole must be parsed as postfix-expression. */
8819 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8820 != CPP_CLOSE_PAREN;
8822 case CPP_OPEN_SQUARE:
8823 /* '[' may start a primary-expression in obj-c++ and in C++11,
8824 as a lambda-expression, eg, '(void)[]{}'. */
8825 if (cxx_dialect >= cxx11)
8826 return -1;
8827 return c_dialect_objc ();
8829 case CPP_PLUS_PLUS:
8830 case CPP_MINUS_MINUS:
8831 /* '++' and '--' may or may not start a cast-expression:
8833 struct T { void operator++(int); };
8834 void f() { (T())++; }
8838 int a;
8839 (int)++a; */
8840 return -1;
8842 default:
8843 return 1;
8847 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8848 in the order: const_cast, static_cast, reinterpret_cast.
8850 Don't suggest dynamic_cast.
8852 Return the first legal cast kind found, or NULL otherwise. */
8854 static const char *
8855 get_cast_suggestion (tree dst_type, tree orig_expr)
8857 tree trial;
8859 /* Reuse the parser logic by attempting to build the various kinds of
8860 cast, with "complain" disabled.
8861 Identify the first such cast that is valid. */
8863 /* Don't attempt to run such logic within template processing. */
8864 if (processing_template_decl)
8865 return NULL;
8867 /* First try const_cast. */
8868 trial = build_const_cast (dst_type, orig_expr, tf_none);
8869 if (trial != error_mark_node)
8870 return "const_cast";
8872 /* If that fails, try static_cast. */
8873 trial = build_static_cast (dst_type, orig_expr, tf_none);
8874 if (trial != error_mark_node)
8875 return "static_cast";
8877 /* Finally, try reinterpret_cast. */
8878 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8879 if (trial != error_mark_node)
8880 return "reinterpret_cast";
8882 /* No such cast possible. */
8883 return NULL;
8886 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8887 suggesting how to convert a C-style cast of the form:
8889 (DST_TYPE)ORIG_EXPR
8891 to a C++-style cast.
8893 The primary range of RICHLOC is asssumed to be that of the original
8894 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8895 of the parens in the C-style cast. */
8897 static void
8898 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8899 location_t close_paren_loc, tree orig_expr,
8900 tree dst_type)
8902 /* This function is non-trivial, so bail out now if the warning isn't
8903 going to be emitted. */
8904 if (!warn_old_style_cast)
8905 return;
8907 /* Try to find a legal C++ cast, trying them in order:
8908 const_cast, static_cast, reinterpret_cast. */
8909 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8910 if (!cast_suggestion)
8911 return;
8913 /* Replace the open paren with "CAST_SUGGESTION<". */
8914 pretty_printer pp;
8915 pp_printf (&pp, "%s<", cast_suggestion);
8916 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8918 /* Replace the close paren with "> (". */
8919 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8921 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8922 rich_loc->add_fixit_insert_after (")");
8926 /* Parse a cast-expression.
8928 cast-expression:
8929 unary-expression
8930 ( type-id ) cast-expression
8932 ADDRESS_P is true iff the unary-expression is appearing as the
8933 operand of the `&' operator. CAST_P is true if this expression is
8934 the target of a cast.
8936 Returns a representation of the expression. */
8938 static cp_expr
8939 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8940 bool decltype_p, cp_id_kind * pidk)
8942 /* If it's a `(', then we might be looking at a cast. */
8943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8945 tree type = NULL_TREE;
8946 cp_expr expr (NULL_TREE);
8947 int cast_expression = 0;
8948 const char *saved_message;
8950 /* There's no way to know yet whether or not this is a cast.
8951 For example, `(int (3))' is a unary-expression, while `(int)
8952 3' is a cast. So, we resort to parsing tentatively. */
8953 cp_parser_parse_tentatively (parser);
8954 /* Types may not be defined in a cast. */
8955 saved_message = parser->type_definition_forbidden_message;
8956 parser->type_definition_forbidden_message
8957 = G_("types may not be defined in casts");
8958 /* Consume the `('. */
8959 matching_parens parens;
8960 cp_token *open_paren = parens.consume_open (parser);
8961 location_t open_paren_loc = open_paren->location;
8962 location_t close_paren_loc = UNKNOWN_LOCATION;
8964 /* A very tricky bit is that `(struct S) { 3 }' is a
8965 compound-literal (which we permit in C++ as an extension).
8966 But, that construct is not a cast-expression -- it is a
8967 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8968 is legal; if the compound-literal were a cast-expression,
8969 you'd need an extra set of parentheses.) But, if we parse
8970 the type-id, and it happens to be a class-specifier, then we
8971 will commit to the parse at that point, because we cannot
8972 undo the action that is done when creating a new class. So,
8973 then we cannot back up and do a postfix-expression.
8975 Another tricky case is the following (c++/29234):
8977 struct S { void operator () (); };
8979 void foo ()
8981 ( S()() );
8984 As a type-id we parse the parenthesized S()() as a function
8985 returning a function, groktypename complains and we cannot
8986 back up in this case either.
8988 Therefore, we scan ahead to the closing `)', and check to see
8989 if the tokens after the `)' can start a cast-expression. Otherwise
8990 we are dealing with an unary-expression, a postfix-expression
8991 or something else.
8993 Yet another tricky case, in C++11, is the following (c++/54891):
8995 (void)[]{};
8997 The issue is that usually, besides the case of lambda-expressions,
8998 the parenthesized type-id cannot be followed by '[', and, eg, we
8999 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9000 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9001 we don't commit, we try a cast-expression, then an unary-expression.
9003 Save tokens so that we can put them back. */
9004 cp_lexer_save_tokens (parser->lexer);
9006 /* We may be looking at a cast-expression. */
9007 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9008 /*consume_paren=*/true))
9009 cast_expression
9010 = cp_parser_tokens_start_cast_expression (parser);
9012 /* Roll back the tokens we skipped. */
9013 cp_lexer_rollback_tokens (parser->lexer);
9014 /* If we aren't looking at a cast-expression, simulate an error so
9015 that the call to cp_parser_error_occurred below returns true. */
9016 if (!cast_expression)
9017 cp_parser_simulate_error (parser);
9018 else
9020 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9021 parser->in_type_id_in_expr_p = true;
9022 /* Look for the type-id. */
9023 type = cp_parser_type_id (parser);
9024 /* Look for the closing `)'. */
9025 cp_token *close_paren = parens.require_close (parser);
9026 if (close_paren)
9027 close_paren_loc = close_paren->location;
9028 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9031 /* Restore the saved message. */
9032 parser->type_definition_forbidden_message = saved_message;
9034 /* At this point this can only be either a cast or a
9035 parenthesized ctor such as `(T ())' that looks like a cast to
9036 function returning T. */
9037 if (!cp_parser_error_occurred (parser))
9039 /* Only commit if the cast-expression doesn't start with
9040 '++', '--', or '[' in C++11. */
9041 if (cast_expression > 0)
9042 cp_parser_commit_to_topmost_tentative_parse (parser);
9044 expr = cp_parser_cast_expression (parser,
9045 /*address_p=*/false,
9046 /*cast_p=*/true,
9047 /*decltype_p=*/false,
9048 pidk);
9050 if (cp_parser_parse_definitely (parser))
9052 /* Warn about old-style casts, if so requested. */
9053 if (warn_old_style_cast
9054 && !in_system_header_at (input_location)
9055 && !VOID_TYPE_P (type)
9056 && current_lang_name != lang_name_c)
9058 gcc_rich_location rich_loc (input_location);
9059 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9060 expr, type);
9061 warning_at (&rich_loc, OPT_Wold_style_cast,
9062 "use of old-style cast to %q#T", type);
9065 /* Only type conversions to integral or enumeration types
9066 can be used in constant-expressions. */
9067 if (!cast_valid_in_integral_constant_expression_p (type)
9068 && cp_parser_non_integral_constant_expression (parser,
9069 NIC_CAST))
9070 return error_mark_node;
9072 /* Perform the cast. */
9073 /* Make a location:
9074 (TYPE) EXPR
9075 ^~~~~~~~~~~
9076 with start==caret at the open paren, extending to the
9077 end of "expr". */
9078 location_t cast_loc = make_location (open_paren_loc,
9079 open_paren_loc,
9080 expr.get_finish ());
9081 expr = build_c_cast (cast_loc, type, expr);
9082 return expr;
9085 else
9086 cp_parser_abort_tentative_parse (parser);
9089 /* If we get here, then it's not a cast, so it must be a
9090 unary-expression. */
9091 return cp_parser_unary_expression (parser, pidk, address_p,
9092 cast_p, decltype_p);
9095 /* Parse a binary expression of the general form:
9097 pm-expression:
9098 cast-expression
9099 pm-expression .* cast-expression
9100 pm-expression ->* cast-expression
9102 multiplicative-expression:
9103 pm-expression
9104 multiplicative-expression * pm-expression
9105 multiplicative-expression / pm-expression
9106 multiplicative-expression % pm-expression
9108 additive-expression:
9109 multiplicative-expression
9110 additive-expression + multiplicative-expression
9111 additive-expression - multiplicative-expression
9113 shift-expression:
9114 additive-expression
9115 shift-expression << additive-expression
9116 shift-expression >> additive-expression
9118 relational-expression:
9119 shift-expression
9120 relational-expression < shift-expression
9121 relational-expression > shift-expression
9122 relational-expression <= shift-expression
9123 relational-expression >= shift-expression
9125 GNU Extension:
9127 relational-expression:
9128 relational-expression <? shift-expression
9129 relational-expression >? shift-expression
9131 equality-expression:
9132 relational-expression
9133 equality-expression == relational-expression
9134 equality-expression != relational-expression
9136 and-expression:
9137 equality-expression
9138 and-expression & equality-expression
9140 exclusive-or-expression:
9141 and-expression
9142 exclusive-or-expression ^ and-expression
9144 inclusive-or-expression:
9145 exclusive-or-expression
9146 inclusive-or-expression | exclusive-or-expression
9148 logical-and-expression:
9149 inclusive-or-expression
9150 logical-and-expression && inclusive-or-expression
9152 logical-or-expression:
9153 logical-and-expression
9154 logical-or-expression || logical-and-expression
9156 All these are implemented with a single function like:
9158 binary-expression:
9159 simple-cast-expression
9160 binary-expression <token> binary-expression
9162 CAST_P is true if this expression is the target of a cast.
9164 The binops_by_token map is used to get the tree codes for each <token> type.
9165 binary-expressions are associated according to a precedence table. */
9167 #define TOKEN_PRECEDENCE(token) \
9168 (((token->type == CPP_GREATER \
9169 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9170 && !parser->greater_than_is_operator_p) \
9171 ? PREC_NOT_OPERATOR \
9172 : binops_by_token[token->type].prec)
9174 static cp_expr
9175 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9176 bool no_toplevel_fold_p,
9177 bool decltype_p,
9178 enum cp_parser_prec prec,
9179 cp_id_kind * pidk)
9181 cp_parser_expression_stack stack;
9182 cp_parser_expression_stack_entry *sp = &stack[0];
9183 cp_parser_expression_stack_entry current;
9184 cp_expr rhs;
9185 cp_token *token;
9186 enum tree_code rhs_type;
9187 enum cp_parser_prec new_prec, lookahead_prec;
9188 tree overload;
9190 /* Parse the first expression. */
9191 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9192 ? TRUTH_NOT_EXPR : ERROR_MARK);
9193 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9194 cast_p, decltype_p, pidk);
9195 current.prec = prec;
9197 if (cp_parser_error_occurred (parser))
9198 return error_mark_node;
9200 for (;;)
9202 /* Get an operator token. */
9203 token = cp_lexer_peek_token (parser->lexer);
9205 if (warn_cxx11_compat
9206 && token->type == CPP_RSHIFT
9207 && !parser->greater_than_is_operator_p)
9209 if (warning_at (token->location, OPT_Wc__11_compat,
9210 "%<>>%> operator is treated"
9211 " as two right angle brackets in C++11"))
9212 inform (token->location,
9213 "suggest parentheses around %<>>%> expression");
9216 new_prec = TOKEN_PRECEDENCE (token);
9217 if (new_prec != PREC_NOT_OPERATOR
9218 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9219 /* This is a fold-expression; handle it later. */
9220 new_prec = PREC_NOT_OPERATOR;
9222 /* Popping an entry off the stack means we completed a subexpression:
9223 - either we found a token which is not an operator (`>' where it is not
9224 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9225 will happen repeatedly;
9226 - or, we found an operator which has lower priority. This is the case
9227 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9228 parsing `3 * 4'. */
9229 if (new_prec <= current.prec)
9231 if (sp == stack)
9232 break;
9233 else
9234 goto pop;
9237 get_rhs:
9238 current.tree_type = binops_by_token[token->type].tree_type;
9239 current.loc = token->location;
9241 /* We used the operator token. */
9242 cp_lexer_consume_token (parser->lexer);
9244 /* For "false && x" or "true || x", x will never be executed;
9245 disable warnings while evaluating it. */
9246 if (current.tree_type == TRUTH_ANDIF_EXPR)
9247 c_inhibit_evaluation_warnings +=
9248 cp_fully_fold (current.lhs) == truthvalue_false_node;
9249 else if (current.tree_type == TRUTH_ORIF_EXPR)
9250 c_inhibit_evaluation_warnings +=
9251 cp_fully_fold (current.lhs) == truthvalue_true_node;
9253 /* Extract another operand. It may be the RHS of this expression
9254 or the LHS of a new, higher priority expression. */
9255 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9256 ? TRUTH_NOT_EXPR : ERROR_MARK);
9257 rhs = cp_parser_simple_cast_expression (parser);
9259 /* Get another operator token. Look up its precedence to avoid
9260 building a useless (immediately popped) stack entry for common
9261 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9262 token = cp_lexer_peek_token (parser->lexer);
9263 lookahead_prec = TOKEN_PRECEDENCE (token);
9264 if (lookahead_prec != PREC_NOT_OPERATOR
9265 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9266 lookahead_prec = PREC_NOT_OPERATOR;
9267 if (lookahead_prec > new_prec)
9269 /* ... and prepare to parse the RHS of the new, higher priority
9270 expression. Since precedence levels on the stack are
9271 monotonically increasing, we do not have to care about
9272 stack overflows. */
9273 *sp = current;
9274 ++sp;
9275 current.lhs = rhs;
9276 current.lhs_type = rhs_type;
9277 current.prec = new_prec;
9278 new_prec = lookahead_prec;
9279 goto get_rhs;
9281 pop:
9282 lookahead_prec = new_prec;
9283 /* If the stack is not empty, we have parsed into LHS the right side
9284 (`4' in the example above) of an expression we had suspended.
9285 We can use the information on the stack to recover the LHS (`3')
9286 from the stack together with the tree code (`MULT_EXPR'), and
9287 the precedence of the higher level subexpression
9288 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9289 which will be used to actually build the additive expression. */
9290 rhs = current.lhs;
9291 rhs_type = current.lhs_type;
9292 --sp;
9293 current = *sp;
9296 /* Undo the disabling of warnings done above. */
9297 if (current.tree_type == TRUTH_ANDIF_EXPR)
9298 c_inhibit_evaluation_warnings -=
9299 cp_fully_fold (current.lhs) == truthvalue_false_node;
9300 else if (current.tree_type == TRUTH_ORIF_EXPR)
9301 c_inhibit_evaluation_warnings -=
9302 cp_fully_fold (current.lhs) == truthvalue_true_node;
9304 if (warn_logical_not_paren
9305 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9306 && current.lhs_type == TRUTH_NOT_EXPR
9307 /* Avoid warning for !!x == y. */
9308 && (TREE_CODE (current.lhs) != NE_EXPR
9309 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9310 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9311 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9312 /* Avoid warning for !b == y where b is boolean. */
9313 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9314 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9315 != BOOLEAN_TYPE))))
9316 /* Avoid warning for !!b == y where b is boolean. */
9317 && (!DECL_P (current.lhs)
9318 || TREE_TYPE (current.lhs) == NULL_TREE
9319 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9320 warn_logical_not_parentheses (current.loc, current.tree_type,
9321 current.lhs, maybe_constant_value (rhs));
9323 overload = NULL;
9325 location_t combined_loc = make_location (current.loc,
9326 current.lhs.get_start (),
9327 rhs.get_finish ());
9329 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9330 ERROR_MARK for everything that is not a binary expression.
9331 This makes warn_about_parentheses miss some warnings that
9332 involve unary operators. For unary expressions we should
9333 pass the correct tree_code unless the unary expression was
9334 surrounded by parentheses.
9336 if (no_toplevel_fold_p
9337 && lookahead_prec <= current.prec
9338 && sp == stack)
9340 if (current.lhs == error_mark_node || rhs == error_mark_node)
9341 current.lhs = error_mark_node;
9342 else
9344 current.lhs
9345 = build_min (current.tree_type,
9346 TREE_CODE_CLASS (current.tree_type)
9347 == tcc_comparison
9348 ? boolean_type_node : TREE_TYPE (current.lhs),
9349 current.lhs.get_value (), rhs.get_value ());
9350 SET_EXPR_LOCATION (current.lhs, combined_loc);
9353 else
9355 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9356 current.lhs, current.lhs_type,
9357 rhs, rhs_type, &overload,
9358 complain_flags (decltype_p));
9359 /* TODO: build_x_binary_op doesn't always honor the location. */
9360 current.lhs.set_location (combined_loc);
9362 current.lhs_type = current.tree_type;
9364 /* If the binary operator required the use of an overloaded operator,
9365 then this expression cannot be an integral constant-expression.
9366 An overloaded operator can be used even if both operands are
9367 otherwise permissible in an integral constant-expression if at
9368 least one of the operands is of enumeration type. */
9370 if (overload
9371 && cp_parser_non_integral_constant_expression (parser,
9372 NIC_OVERLOADED))
9373 return error_mark_node;
9376 return current.lhs;
9379 static cp_expr
9380 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9381 bool no_toplevel_fold_p,
9382 enum cp_parser_prec prec,
9383 cp_id_kind * pidk)
9385 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9386 /*decltype*/false, prec, pidk);
9389 /* Parse the `? expression : assignment-expression' part of a
9390 conditional-expression. The LOGICAL_OR_EXPR is the
9391 logical-or-expression that started the conditional-expression.
9392 Returns a representation of the entire conditional-expression.
9394 This routine is used by cp_parser_assignment_expression.
9396 ? expression : assignment-expression
9398 GNU Extensions:
9400 ? : assignment-expression */
9402 static tree
9403 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9405 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9406 cp_expr assignment_expr;
9407 struct cp_token *token;
9408 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9410 /* Consume the `?' token. */
9411 cp_lexer_consume_token (parser->lexer);
9412 token = cp_lexer_peek_token (parser->lexer);
9413 if (cp_parser_allow_gnu_extensions_p (parser)
9414 && token->type == CPP_COLON)
9416 pedwarn (token->location, OPT_Wpedantic,
9417 "ISO C++ does not allow ?: with omitted middle operand");
9418 /* Implicit true clause. */
9419 expr = NULL_TREE;
9420 c_inhibit_evaluation_warnings +=
9421 folded_logical_or_expr == truthvalue_true_node;
9422 warn_for_omitted_condop (token->location, logical_or_expr);
9424 else
9426 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9427 parser->colon_corrects_to_scope_p = false;
9428 /* Parse the expression. */
9429 c_inhibit_evaluation_warnings +=
9430 folded_logical_or_expr == truthvalue_false_node;
9431 expr = cp_parser_expression (parser);
9432 c_inhibit_evaluation_warnings +=
9433 ((folded_logical_or_expr == truthvalue_true_node)
9434 - (folded_logical_or_expr == truthvalue_false_node));
9435 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9438 /* The next token should be a `:'. */
9439 cp_parser_require (parser, CPP_COLON, RT_COLON);
9440 /* Parse the assignment-expression. */
9441 assignment_expr = cp_parser_assignment_expression (parser);
9442 c_inhibit_evaluation_warnings -=
9443 folded_logical_or_expr == truthvalue_true_node;
9445 /* Make a location:
9446 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9447 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9448 with the caret at the "?", ranging from the start of
9449 the logical_or_expr to the end of the assignment_expr. */
9450 loc = make_location (loc,
9451 logical_or_expr.get_start (),
9452 assignment_expr.get_finish ());
9454 /* Build the conditional-expression. */
9455 return build_x_conditional_expr (loc, logical_or_expr,
9456 expr,
9457 assignment_expr,
9458 tf_warning_or_error);
9461 /* Parse an assignment-expression.
9463 assignment-expression:
9464 conditional-expression
9465 logical-or-expression assignment-operator assignment_expression
9466 throw-expression
9468 CAST_P is true if this expression is the target of a cast.
9469 DECLTYPE_P is true if this expression is the operand of decltype.
9471 Returns a representation for the expression. */
9473 static cp_expr
9474 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9475 bool cast_p, bool decltype_p)
9477 cp_expr expr;
9479 /* If the next token is the `throw' keyword, then we're looking at
9480 a throw-expression. */
9481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9482 expr = cp_parser_throw_expression (parser);
9483 /* Otherwise, it must be that we are looking at a
9484 logical-or-expression. */
9485 else
9487 /* Parse the binary expressions (logical-or-expression). */
9488 expr = cp_parser_binary_expression (parser, cast_p, false,
9489 decltype_p,
9490 PREC_NOT_OPERATOR, pidk);
9491 /* If the next token is a `?' then we're actually looking at a
9492 conditional-expression. */
9493 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9494 return cp_parser_question_colon_clause (parser, expr);
9495 else
9497 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9499 /* If it's an assignment-operator, we're using the second
9500 production. */
9501 enum tree_code assignment_operator
9502 = cp_parser_assignment_operator_opt (parser);
9503 if (assignment_operator != ERROR_MARK)
9505 bool non_constant_p;
9507 /* Parse the right-hand side of the assignment. */
9508 cp_expr rhs = cp_parser_initializer_clause (parser,
9509 &non_constant_p);
9511 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9512 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9514 /* An assignment may not appear in a
9515 constant-expression. */
9516 if (cp_parser_non_integral_constant_expression (parser,
9517 NIC_ASSIGNMENT))
9518 return error_mark_node;
9519 /* Build the assignment expression. Its default
9520 location:
9521 LHS = RHS
9522 ~~~~^~~~~
9523 is the location of the '=' token as the
9524 caret, ranging from the start of the lhs to the
9525 end of the rhs. */
9526 loc = make_location (loc,
9527 expr.get_start (),
9528 rhs.get_finish ());
9529 expr = build_x_modify_expr (loc, expr,
9530 assignment_operator,
9531 rhs,
9532 complain_flags (decltype_p));
9533 /* TODO: build_x_modify_expr doesn't honor the location,
9534 so we must set it here. */
9535 expr.set_location (loc);
9540 return expr;
9543 /* Parse an (optional) assignment-operator.
9545 assignment-operator: one of
9546 = *= /= %= += -= >>= <<= &= ^= |=
9548 GNU Extension:
9550 assignment-operator: one of
9551 <?= >?=
9553 If the next token is an assignment operator, the corresponding tree
9554 code is returned, and the token is consumed. For example, for
9555 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9556 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9557 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9558 operator, ERROR_MARK is returned. */
9560 static enum tree_code
9561 cp_parser_assignment_operator_opt (cp_parser* parser)
9563 enum tree_code op;
9564 cp_token *token;
9566 /* Peek at the next token. */
9567 token = cp_lexer_peek_token (parser->lexer);
9569 switch (token->type)
9571 case CPP_EQ:
9572 op = NOP_EXPR;
9573 break;
9575 case CPP_MULT_EQ:
9576 op = MULT_EXPR;
9577 break;
9579 case CPP_DIV_EQ:
9580 op = TRUNC_DIV_EXPR;
9581 break;
9583 case CPP_MOD_EQ:
9584 op = TRUNC_MOD_EXPR;
9585 break;
9587 case CPP_PLUS_EQ:
9588 op = PLUS_EXPR;
9589 break;
9591 case CPP_MINUS_EQ:
9592 op = MINUS_EXPR;
9593 break;
9595 case CPP_RSHIFT_EQ:
9596 op = RSHIFT_EXPR;
9597 break;
9599 case CPP_LSHIFT_EQ:
9600 op = LSHIFT_EXPR;
9601 break;
9603 case CPP_AND_EQ:
9604 op = BIT_AND_EXPR;
9605 break;
9607 case CPP_XOR_EQ:
9608 op = BIT_XOR_EXPR;
9609 break;
9611 case CPP_OR_EQ:
9612 op = BIT_IOR_EXPR;
9613 break;
9615 default:
9616 /* Nothing else is an assignment operator. */
9617 op = ERROR_MARK;
9620 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9621 if (op != ERROR_MARK
9622 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9623 op = ERROR_MARK;
9625 /* If it was an assignment operator, consume it. */
9626 if (op != ERROR_MARK)
9627 cp_lexer_consume_token (parser->lexer);
9629 return op;
9632 /* Parse an expression.
9634 expression:
9635 assignment-expression
9636 expression , assignment-expression
9638 CAST_P is true if this expression is the target of a cast.
9639 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9640 except possibly parenthesized or on the RHS of a comma (N3276).
9642 Returns a representation of the expression. */
9644 static cp_expr
9645 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9646 bool cast_p, bool decltype_p)
9648 cp_expr expression = NULL_TREE;
9649 location_t loc = UNKNOWN_LOCATION;
9651 while (true)
9653 cp_expr assignment_expression;
9655 /* Parse the next assignment-expression. */
9656 assignment_expression
9657 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9659 /* We don't create a temporary for a call that is the immediate operand
9660 of decltype or on the RHS of a comma. But when we see a comma, we
9661 need to create a temporary for a call on the LHS. */
9662 if (decltype_p && !processing_template_decl
9663 && TREE_CODE (assignment_expression) == CALL_EXPR
9664 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9665 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9666 assignment_expression
9667 = build_cplus_new (TREE_TYPE (assignment_expression),
9668 assignment_expression, tf_warning_or_error);
9670 /* If this is the first assignment-expression, we can just
9671 save it away. */
9672 if (!expression)
9673 expression = assignment_expression;
9674 else
9676 /* Create a location with caret at the comma, ranging
9677 from the start of the LHS to the end of the RHS. */
9678 loc = make_location (loc,
9679 expression.get_start (),
9680 assignment_expression.get_finish ());
9681 expression = build_x_compound_expr (loc, expression,
9682 assignment_expression,
9683 complain_flags (decltype_p));
9684 expression.set_location (loc);
9686 /* If the next token is not a comma, or we're in a fold-expression, then
9687 we are done with the expression. */
9688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9689 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9690 break;
9691 /* Consume the `,'. */
9692 loc = cp_lexer_peek_token (parser->lexer)->location;
9693 cp_lexer_consume_token (parser->lexer);
9694 /* A comma operator cannot appear in a constant-expression. */
9695 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9696 expression = error_mark_node;
9699 return expression;
9702 /* Parse a constant-expression.
9704 constant-expression:
9705 conditional-expression
9707 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9708 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9709 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9710 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9711 only parse a conditional-expression, otherwise parse an
9712 assignment-expression. See below for rationale. */
9714 static cp_expr
9715 cp_parser_constant_expression (cp_parser* parser,
9716 bool allow_non_constant_p,
9717 bool *non_constant_p,
9718 bool strict_p)
9720 bool saved_integral_constant_expression_p;
9721 bool saved_allow_non_integral_constant_expression_p;
9722 bool saved_non_integral_constant_expression_p;
9723 cp_expr expression;
9725 /* It might seem that we could simply parse the
9726 conditional-expression, and then check to see if it were
9727 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9728 one that the compiler can figure out is constant, possibly after
9729 doing some simplifications or optimizations. The standard has a
9730 precise definition of constant-expression, and we must honor
9731 that, even though it is somewhat more restrictive.
9733 For example:
9735 int i[(2, 3)];
9737 is not a legal declaration, because `(2, 3)' is not a
9738 constant-expression. The `,' operator is forbidden in a
9739 constant-expression. However, GCC's constant-folding machinery
9740 will fold this operation to an INTEGER_CST for `3'. */
9742 /* Save the old settings. */
9743 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9744 saved_allow_non_integral_constant_expression_p
9745 = parser->allow_non_integral_constant_expression_p;
9746 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9747 /* We are now parsing a constant-expression. */
9748 parser->integral_constant_expression_p = true;
9749 parser->allow_non_integral_constant_expression_p
9750 = (allow_non_constant_p || cxx_dialect >= cxx11);
9751 parser->non_integral_constant_expression_p = false;
9752 /* Although the grammar says "conditional-expression", when not STRICT_P,
9753 we parse an "assignment-expression", which also permits
9754 "throw-expression" and the use of assignment operators. In the case
9755 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9756 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9757 actually essential that we look for an assignment-expression.
9758 For example, cp_parser_initializer_clauses uses this function to
9759 determine whether a particular assignment-expression is in fact
9760 constant. */
9761 if (strict_p)
9763 /* Parse the binary expressions (logical-or-expression). */
9764 expression = cp_parser_binary_expression (parser, false, false, false,
9765 PREC_NOT_OPERATOR, NULL);
9766 /* If the next token is a `?' then we're actually looking at
9767 a conditional-expression; otherwise we're done. */
9768 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9769 expression = cp_parser_question_colon_clause (parser, expression);
9771 else
9772 expression = cp_parser_assignment_expression (parser);
9773 /* Restore the old settings. */
9774 parser->integral_constant_expression_p
9775 = saved_integral_constant_expression_p;
9776 parser->allow_non_integral_constant_expression_p
9777 = saved_allow_non_integral_constant_expression_p;
9778 if (cxx_dialect >= cxx11)
9780 /* Require an rvalue constant expression here; that's what our
9781 callers expect. Reference constant expressions are handled
9782 separately in e.g. cp_parser_template_argument. */
9783 tree decay = expression;
9784 if (TREE_TYPE (expression)
9785 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9786 decay = build_address (expression);
9787 bool is_const = potential_rvalue_constant_expression (decay);
9788 parser->non_integral_constant_expression_p = !is_const;
9789 if (!is_const && !allow_non_constant_p)
9790 require_potential_rvalue_constant_expression (decay);
9792 if (allow_non_constant_p)
9793 *non_constant_p = parser->non_integral_constant_expression_p;
9794 parser->non_integral_constant_expression_p
9795 = saved_non_integral_constant_expression_p;
9797 return expression;
9800 /* Parse __builtin_offsetof.
9802 offsetof-expression:
9803 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9805 offsetof-member-designator:
9806 id-expression
9807 | offsetof-member-designator "." id-expression
9808 | offsetof-member-designator "[" expression "]"
9809 | offsetof-member-designator "->" id-expression */
9811 static cp_expr
9812 cp_parser_builtin_offsetof (cp_parser *parser)
9814 int save_ice_p, save_non_ice_p;
9815 tree type;
9816 cp_expr expr;
9817 cp_id_kind dummy;
9818 cp_token *token;
9819 location_t finish_loc;
9821 /* We're about to accept non-integral-constant things, but will
9822 definitely yield an integral constant expression. Save and
9823 restore these values around our local parsing. */
9824 save_ice_p = parser->integral_constant_expression_p;
9825 save_non_ice_p = parser->non_integral_constant_expression_p;
9827 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9829 /* Consume the "__builtin_offsetof" token. */
9830 cp_lexer_consume_token (parser->lexer);
9831 /* Consume the opening `('. */
9832 matching_parens parens;
9833 parens.require_open (parser);
9834 /* Parse the type-id. */
9835 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9836 type = cp_parser_type_id (parser);
9837 /* Look for the `,'. */
9838 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9839 token = cp_lexer_peek_token (parser->lexer);
9841 /* Build the (type *)null that begins the traditional offsetof macro. */
9842 tree object_ptr
9843 = build_static_cast (build_pointer_type (type), null_pointer_node,
9844 tf_warning_or_error);
9846 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9847 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9848 true, &dummy, token->location);
9849 while (true)
9851 token = cp_lexer_peek_token (parser->lexer);
9852 switch (token->type)
9854 case CPP_OPEN_SQUARE:
9855 /* offsetof-member-designator "[" expression "]" */
9856 expr = cp_parser_postfix_open_square_expression (parser, expr,
9857 true, false);
9858 break;
9860 case CPP_DEREF:
9861 /* offsetof-member-designator "->" identifier */
9862 expr = grok_array_decl (token->location, expr,
9863 integer_zero_node, false);
9864 /* FALLTHRU */
9866 case CPP_DOT:
9867 /* offsetof-member-designator "." identifier */
9868 cp_lexer_consume_token (parser->lexer);
9869 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9870 expr, true, &dummy,
9871 token->location);
9872 break;
9874 case CPP_CLOSE_PAREN:
9875 /* Consume the ")" token. */
9876 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9877 cp_lexer_consume_token (parser->lexer);
9878 goto success;
9880 default:
9881 /* Error. We know the following require will fail, but
9882 that gives the proper error message. */
9883 parens.require_close (parser);
9884 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9885 expr = error_mark_node;
9886 goto failure;
9890 success:
9891 /* Make a location of the form:
9892 __builtin_offsetof (struct s, f)
9893 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9894 with caret at the type-id, ranging from the start of the
9895 "_builtin_offsetof" token to the close paren. */
9896 loc = make_location (loc, start_loc, finish_loc);
9897 /* The result will be an INTEGER_CST, so we need to explicitly
9898 preserve the location. */
9899 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9901 failure:
9902 parser->integral_constant_expression_p = save_ice_p;
9903 parser->non_integral_constant_expression_p = save_non_ice_p;
9905 expr = expr.maybe_add_location_wrapper ();
9906 return expr;
9909 /* Parse a trait expression.
9911 Returns a representation of the expression, the underlying type
9912 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9914 static cp_expr
9915 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9917 cp_trait_kind kind;
9918 tree type1, type2 = NULL_TREE;
9919 bool binary = false;
9920 bool variadic = false;
9922 switch (keyword)
9924 case RID_HAS_NOTHROW_ASSIGN:
9925 kind = CPTK_HAS_NOTHROW_ASSIGN;
9926 break;
9927 case RID_HAS_NOTHROW_CONSTRUCTOR:
9928 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9929 break;
9930 case RID_HAS_NOTHROW_COPY:
9931 kind = CPTK_HAS_NOTHROW_COPY;
9932 break;
9933 case RID_HAS_TRIVIAL_ASSIGN:
9934 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9935 break;
9936 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9937 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9938 break;
9939 case RID_HAS_TRIVIAL_COPY:
9940 kind = CPTK_HAS_TRIVIAL_COPY;
9941 break;
9942 case RID_HAS_TRIVIAL_DESTRUCTOR:
9943 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9944 break;
9945 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9946 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9947 break;
9948 case RID_HAS_VIRTUAL_DESTRUCTOR:
9949 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9950 break;
9951 case RID_IS_ABSTRACT:
9952 kind = CPTK_IS_ABSTRACT;
9953 break;
9954 case RID_IS_AGGREGATE:
9955 kind = CPTK_IS_AGGREGATE;
9956 break;
9957 case RID_IS_BASE_OF:
9958 kind = CPTK_IS_BASE_OF;
9959 binary = true;
9960 break;
9961 case RID_IS_CLASS:
9962 kind = CPTK_IS_CLASS;
9963 break;
9964 case RID_IS_EMPTY:
9965 kind = CPTK_IS_EMPTY;
9966 break;
9967 case RID_IS_ENUM:
9968 kind = CPTK_IS_ENUM;
9969 break;
9970 case RID_IS_FINAL:
9971 kind = CPTK_IS_FINAL;
9972 break;
9973 case RID_IS_LITERAL_TYPE:
9974 kind = CPTK_IS_LITERAL_TYPE;
9975 break;
9976 case RID_IS_POD:
9977 kind = CPTK_IS_POD;
9978 break;
9979 case RID_IS_POLYMORPHIC:
9980 kind = CPTK_IS_POLYMORPHIC;
9981 break;
9982 case RID_IS_SAME_AS:
9983 kind = CPTK_IS_SAME_AS;
9984 binary = true;
9985 break;
9986 case RID_IS_STD_LAYOUT:
9987 kind = CPTK_IS_STD_LAYOUT;
9988 break;
9989 case RID_IS_TRIVIAL:
9990 kind = CPTK_IS_TRIVIAL;
9991 break;
9992 case RID_IS_TRIVIALLY_ASSIGNABLE:
9993 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9994 binary = true;
9995 break;
9996 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9997 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9998 variadic = true;
9999 break;
10000 case RID_IS_TRIVIALLY_COPYABLE:
10001 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10002 break;
10003 case RID_IS_UNION:
10004 kind = CPTK_IS_UNION;
10005 break;
10006 case RID_UNDERLYING_TYPE:
10007 kind = CPTK_UNDERLYING_TYPE;
10008 break;
10009 case RID_BASES:
10010 kind = CPTK_BASES;
10011 break;
10012 case RID_DIRECT_BASES:
10013 kind = CPTK_DIRECT_BASES;
10014 break;
10015 case RID_IS_ASSIGNABLE:
10016 kind = CPTK_IS_ASSIGNABLE;
10017 binary = true;
10018 break;
10019 case RID_IS_CONSTRUCTIBLE:
10020 kind = CPTK_IS_CONSTRUCTIBLE;
10021 variadic = true;
10022 break;
10023 default:
10024 gcc_unreachable ();
10027 /* Get location of initial token. */
10028 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10030 /* Consume the token. */
10031 cp_lexer_consume_token (parser->lexer);
10033 matching_parens parens;
10034 parens.require_open (parser);
10037 type_id_in_expr_sentinel s (parser);
10038 type1 = cp_parser_type_id (parser);
10041 if (type1 == error_mark_node)
10042 return error_mark_node;
10044 if (binary)
10046 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10049 type_id_in_expr_sentinel s (parser);
10050 type2 = cp_parser_type_id (parser);
10053 if (type2 == error_mark_node)
10054 return error_mark_node;
10056 else if (variadic)
10058 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10060 cp_lexer_consume_token (parser->lexer);
10061 tree elt = cp_parser_type_id (parser);
10062 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10064 cp_lexer_consume_token (parser->lexer);
10065 elt = make_pack_expansion (elt);
10067 if (elt == error_mark_node)
10068 return error_mark_node;
10069 type2 = tree_cons (NULL_TREE, elt, type2);
10073 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10074 parens.require_close (parser);
10076 /* Construct a location of the form:
10077 __is_trivially_copyable(_Tp)
10078 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10079 with start == caret, finishing at the close-paren. */
10080 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10082 /* Complete the trait expression, which may mean either processing
10083 the trait expr now or saving it for template instantiation. */
10084 switch (kind)
10086 case CPTK_UNDERLYING_TYPE:
10087 return cp_expr (finish_underlying_type (type1), trait_loc);
10088 case CPTK_BASES:
10089 return cp_expr (finish_bases (type1, false), trait_loc);
10090 case CPTK_DIRECT_BASES:
10091 return cp_expr (finish_bases (type1, true), trait_loc);
10092 default:
10093 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10097 /* Parse a lambda expression.
10099 lambda-expression:
10100 lambda-introducer lambda-declarator [opt] compound-statement
10102 Returns a representation of the expression. */
10104 static cp_expr
10105 cp_parser_lambda_expression (cp_parser* parser)
10107 tree lambda_expr = build_lambda_expr ();
10108 tree type;
10109 bool ok = true;
10110 cp_token *token = cp_lexer_peek_token (parser->lexer);
10111 cp_token_position start = 0;
10113 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10115 if (cp_unevaluated_operand)
10117 if (!token->error_reported)
10119 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10120 "lambda-expression in unevaluated context");
10121 token->error_reported = true;
10123 ok = false;
10125 else if (parser->in_template_argument_list_p)
10127 if (!token->error_reported)
10129 error_at (token->location, "lambda-expression in template-argument");
10130 token->error_reported = true;
10132 ok = false;
10135 /* We may be in the middle of deferred access check. Disable
10136 it now. */
10137 push_deferring_access_checks (dk_no_deferred);
10139 cp_parser_lambda_introducer (parser, lambda_expr);
10141 type = begin_lambda_type (lambda_expr);
10142 if (type == error_mark_node)
10143 return error_mark_node;
10145 record_lambda_scope (lambda_expr);
10147 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10148 determine_visibility (TYPE_NAME (type));
10150 /* Now that we've started the type, add the capture fields for any
10151 explicit captures. */
10152 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10155 /* Inside the class, surrounding template-parameter-lists do not apply. */
10156 unsigned int saved_num_template_parameter_lists
10157 = parser->num_template_parameter_lists;
10158 unsigned char in_statement = parser->in_statement;
10159 bool in_switch_statement_p = parser->in_switch_statement_p;
10160 bool fully_implicit_function_template_p
10161 = parser->fully_implicit_function_template_p;
10162 tree implicit_template_parms = parser->implicit_template_parms;
10163 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10164 bool auto_is_implicit_function_template_parm_p
10165 = parser->auto_is_implicit_function_template_parm_p;
10167 parser->num_template_parameter_lists = 0;
10168 parser->in_statement = 0;
10169 parser->in_switch_statement_p = false;
10170 parser->fully_implicit_function_template_p = false;
10171 parser->implicit_template_parms = 0;
10172 parser->implicit_template_scope = 0;
10173 parser->auto_is_implicit_function_template_parm_p = false;
10175 /* By virtue of defining a local class, a lambda expression has access to
10176 the private variables of enclosing classes. */
10178 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10180 if (ok && cp_parser_error_occurred (parser))
10181 ok = false;
10183 if (ok)
10185 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10186 && cp_parser_start_tentative_firewall (parser))
10187 start = token;
10188 cp_parser_lambda_body (parser, lambda_expr);
10190 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10192 if (cp_parser_skip_to_closing_brace (parser))
10193 cp_lexer_consume_token (parser->lexer);
10196 /* The capture list was built up in reverse order; fix that now. */
10197 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10198 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10200 if (ok)
10201 maybe_add_lambda_conv_op (type);
10203 type = finish_struct (type, /*attributes=*/NULL_TREE);
10205 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10206 parser->in_statement = in_statement;
10207 parser->in_switch_statement_p = in_switch_statement_p;
10208 parser->fully_implicit_function_template_p
10209 = fully_implicit_function_template_p;
10210 parser->implicit_template_parms = implicit_template_parms;
10211 parser->implicit_template_scope = implicit_template_scope;
10212 parser->auto_is_implicit_function_template_parm_p
10213 = auto_is_implicit_function_template_parm_p;
10216 /* This field is only used during parsing of the lambda. */
10217 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10219 /* This lambda shouldn't have any proxies left at this point. */
10220 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10221 /* And now that we're done, push proxies for an enclosing lambda. */
10222 insert_pending_capture_proxies ();
10224 if (ok)
10225 lambda_expr = build_lambda_object (lambda_expr);
10226 else
10227 lambda_expr = error_mark_node;
10229 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10231 pop_deferring_access_checks ();
10233 return lambda_expr;
10236 /* Parse the beginning of a lambda expression.
10238 lambda-introducer:
10239 [ lambda-capture [opt] ]
10241 LAMBDA_EXPR is the current representation of the lambda expression. */
10243 static void
10244 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10246 /* Need commas after the first capture. */
10247 bool first = true;
10249 /* Eat the leading `['. */
10250 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10252 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10253 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10254 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10255 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10256 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10257 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10259 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10261 cp_lexer_consume_token (parser->lexer);
10262 first = false;
10265 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10267 cp_token* capture_token;
10268 tree capture_id;
10269 tree capture_init_expr;
10270 cp_id_kind idk = CP_ID_KIND_NONE;
10271 bool explicit_init_p = false;
10273 enum capture_kind_type
10275 BY_COPY,
10276 BY_REFERENCE
10278 enum capture_kind_type capture_kind = BY_COPY;
10280 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10282 error ("expected end of capture-list");
10283 return;
10286 if (first)
10287 first = false;
10288 else
10289 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10291 /* Possibly capture `this'. */
10292 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10294 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10295 if (cxx_dialect < cxx2a
10296 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10297 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10298 "with by-copy capture default");
10299 cp_lexer_consume_token (parser->lexer);
10300 add_capture (lambda_expr,
10301 /*id=*/this_identifier,
10302 /*initializer=*/finish_this_expr (),
10303 /*by_reference_p=*/true,
10304 explicit_init_p);
10305 continue;
10308 /* Possibly capture `*this'. */
10309 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10310 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10312 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10313 if (cxx_dialect < cxx17)
10314 pedwarn (loc, 0, "%<*this%> capture only available with "
10315 "-std=c++17 or -std=gnu++17");
10316 cp_lexer_consume_token (parser->lexer);
10317 cp_lexer_consume_token (parser->lexer);
10318 add_capture (lambda_expr,
10319 /*id=*/this_identifier,
10320 /*initializer=*/finish_this_expr (),
10321 /*by_reference_p=*/false,
10322 explicit_init_p);
10323 continue;
10326 /* Remember whether we want to capture as a reference or not. */
10327 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10329 capture_kind = BY_REFERENCE;
10330 cp_lexer_consume_token (parser->lexer);
10333 /* Get the identifier. */
10334 capture_token = cp_lexer_peek_token (parser->lexer);
10335 capture_id = cp_parser_identifier (parser);
10337 if (capture_id == error_mark_node)
10338 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10339 delimiters, but I modified this to stop on unnested ']' as well. It
10340 was already changed to stop on unnested '}', so the
10341 "closing_parenthesis" name is no more misleading with my change. */
10343 cp_parser_skip_to_closing_parenthesis (parser,
10344 /*recovering=*/true,
10345 /*or_comma=*/true,
10346 /*consume_paren=*/true);
10347 break;
10350 /* Find the initializer for this capture. */
10351 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10352 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10353 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10355 bool direct, non_constant;
10356 /* An explicit initializer exists. */
10357 if (cxx_dialect < cxx14)
10358 pedwarn (input_location, 0,
10359 "lambda capture initializers "
10360 "only available with -std=c++14 or -std=gnu++14");
10361 capture_init_expr = cp_parser_initializer (parser, &direct,
10362 &non_constant);
10363 explicit_init_p = true;
10364 if (capture_init_expr == NULL_TREE)
10366 error ("empty initializer for lambda init-capture");
10367 capture_init_expr = error_mark_node;
10370 else
10372 const char* error_msg;
10374 /* Turn the identifier into an id-expression. */
10375 capture_init_expr
10376 = cp_parser_lookup_name_simple (parser, capture_id,
10377 capture_token->location);
10379 if (capture_init_expr == error_mark_node)
10381 unqualified_name_lookup_error (capture_id);
10382 continue;
10384 else if (!VAR_P (capture_init_expr)
10385 && TREE_CODE (capture_init_expr) != PARM_DECL)
10387 error_at (capture_token->location,
10388 "capture of non-variable %qE",
10389 capture_init_expr);
10390 if (DECL_P (capture_init_expr))
10391 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10392 "%q#D declared here", capture_init_expr);
10393 continue;
10395 if (VAR_P (capture_init_expr)
10396 && decl_storage_duration (capture_init_expr) != dk_auto)
10398 if (pedwarn (capture_token->location, 0, "capture of variable "
10399 "%qD with non-automatic storage duration",
10400 capture_init_expr))
10401 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10402 "%q#D declared here", capture_init_expr);
10403 continue;
10406 capture_init_expr
10407 = finish_id_expression
10408 (capture_id,
10409 capture_init_expr,
10410 parser->scope,
10411 &idk,
10412 /*integral_constant_expression_p=*/false,
10413 /*allow_non_integral_constant_expression_p=*/false,
10414 /*non_integral_constant_expression_p=*/NULL,
10415 /*template_p=*/false,
10416 /*done=*/true,
10417 /*address_p=*/false,
10418 /*template_arg_p=*/false,
10419 &error_msg,
10420 capture_token->location);
10422 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10424 cp_lexer_consume_token (parser->lexer);
10425 capture_init_expr = make_pack_expansion (capture_init_expr);
10429 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10430 && !explicit_init_p)
10432 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10433 && capture_kind == BY_COPY)
10434 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10435 "of %qD redundant with by-copy capture default",
10436 capture_id);
10437 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10438 && capture_kind == BY_REFERENCE)
10439 pedwarn (capture_token->location, 0, "explicit by-reference "
10440 "capture of %qD redundant with by-reference capture "
10441 "default", capture_id);
10444 add_capture (lambda_expr,
10445 capture_id,
10446 capture_init_expr,
10447 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10448 explicit_init_p);
10450 /* If there is any qualification still in effect, clear it
10451 now; we will be starting fresh with the next capture. */
10452 parser->scope = NULL_TREE;
10453 parser->qualifying_scope = NULL_TREE;
10454 parser->object_scope = NULL_TREE;
10457 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10460 /* Parse the (optional) middle of a lambda expression.
10462 lambda-declarator:
10463 < template-parameter-list [opt] >
10464 ( parameter-declaration-clause [opt] )
10465 attribute-specifier [opt]
10466 decl-specifier-seq [opt]
10467 exception-specification [opt]
10468 lambda-return-type-clause [opt]
10470 LAMBDA_EXPR is the current representation of the lambda expression. */
10472 static bool
10473 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10475 /* 5.1.1.4 of the standard says:
10476 If a lambda-expression does not include a lambda-declarator, it is as if
10477 the lambda-declarator were ().
10478 This means an empty parameter list, no attributes, and no exception
10479 specification. */
10480 tree param_list = void_list_node;
10481 tree attributes = NULL_TREE;
10482 tree exception_spec = NULL_TREE;
10483 tree template_param_list = NULL_TREE;
10484 tree tx_qual = NULL_TREE;
10485 tree return_type = NULL_TREE;
10486 cp_decl_specifier_seq lambda_specs;
10487 clear_decl_specs (&lambda_specs);
10489 /* The template-parameter-list is optional, but must begin with
10490 an opening angle if present. */
10491 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10493 if (cxx_dialect < cxx14)
10494 pedwarn (parser->lexer->next_token->location, 0,
10495 "lambda templates are only available with "
10496 "-std=c++14 or -std=gnu++14");
10497 else if (cxx_dialect < cxx2a)
10498 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10499 "lambda templates are only available with "
10500 "-std=c++2a or -std=gnu++2a");
10502 cp_lexer_consume_token (parser->lexer);
10504 template_param_list = cp_parser_template_parameter_list (parser);
10506 cp_parser_skip_to_end_of_template_parameter_list (parser);
10508 /* We just processed one more parameter list. */
10509 ++parser->num_template_parameter_lists;
10512 /* The parameter-declaration-clause is optional (unless
10513 template-parameter-list was given), but must begin with an
10514 opening parenthesis if present. */
10515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10517 matching_parens parens;
10518 parens.consume_open (parser);
10520 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10522 /* Parse parameters. */
10523 param_list = cp_parser_parameter_declaration_clause (parser);
10525 /* Default arguments shall not be specified in the
10526 parameter-declaration-clause of a lambda-declarator. */
10527 if (cxx_dialect < cxx14)
10528 for (tree t = param_list; t; t = TREE_CHAIN (t))
10529 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10530 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10531 "default argument specified for lambda parameter");
10533 parens.require_close (parser);
10535 attributes = cp_parser_attributes_opt (parser);
10537 /* In the decl-specifier-seq of the lambda-declarator, each
10538 decl-specifier shall either be mutable or constexpr. */
10539 int declares_class_or_enum;
10540 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10541 cp_parser_decl_specifier_seq (parser,
10542 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10543 &lambda_specs, &declares_class_or_enum);
10544 if (lambda_specs.storage_class == sc_mutable)
10546 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10547 if (lambda_specs.conflicting_specifiers_p)
10548 error_at (lambda_specs.locations[ds_storage_class],
10549 "duplicate %<mutable%>");
10552 tx_qual = cp_parser_tx_qualifier_opt (parser);
10554 /* Parse optional exception specification. */
10555 exception_spec = cp_parser_exception_specification_opt (parser);
10557 /* Parse optional trailing return type. */
10558 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10560 cp_lexer_consume_token (parser->lexer);
10561 return_type = cp_parser_trailing_type_id (parser);
10564 /* The function parameters must be in scope all the way until after the
10565 trailing-return-type in case of decltype. */
10566 pop_bindings_and_leave_scope ();
10568 else if (template_param_list != NULL_TREE) // generate diagnostic
10569 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10571 /* Create the function call operator.
10573 Messing with declarators like this is no uglier than building up the
10574 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10575 other code. */
10577 cp_decl_specifier_seq return_type_specs;
10578 cp_declarator* declarator;
10579 tree fco;
10580 int quals;
10581 void *p;
10583 clear_decl_specs (&return_type_specs);
10584 if (return_type)
10585 return_type_specs.type = return_type;
10586 else
10587 /* Maybe we will deduce the return type later. */
10588 return_type_specs.type = make_auto ();
10590 if (lambda_specs.locations[ds_constexpr])
10592 if (cxx_dialect >= cxx17)
10593 return_type_specs.locations[ds_constexpr]
10594 = lambda_specs.locations[ds_constexpr];
10595 else
10596 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10597 "lambda only available with -std=c++17 or -std=gnu++17");
10600 p = obstack_alloc (&declarator_obstack, 0);
10602 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10604 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10605 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10606 declarator = make_call_declarator (declarator, param_list, quals,
10607 VIRT_SPEC_UNSPECIFIED,
10608 REF_QUAL_NONE,
10609 tx_qual,
10610 exception_spec,
10611 /*late_return_type=*/NULL_TREE,
10612 /*requires_clause*/NULL_TREE);
10613 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10615 fco = grokmethod (&return_type_specs,
10616 declarator,
10617 attributes);
10618 if (fco != error_mark_node)
10620 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10621 DECL_ARTIFICIAL (fco) = 1;
10622 /* Give the object parameter a different name. */
10623 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10624 if (return_type)
10625 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10627 if (template_param_list)
10629 fco = finish_member_template_decl (fco);
10630 finish_template_decl (template_param_list);
10631 --parser->num_template_parameter_lists;
10633 else if (parser->fully_implicit_function_template_p)
10634 fco = finish_fully_implicit_template (parser, fco);
10636 finish_member_declaration (fco);
10638 obstack_free (&declarator_obstack, p);
10640 return (fco != error_mark_node);
10644 /* Parse the body of a lambda expression, which is simply
10646 compound-statement
10648 but which requires special handling.
10649 LAMBDA_EXPR is the current representation of the lambda expression. */
10651 static void
10652 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10654 bool nested = (current_function_decl != NULL_TREE);
10655 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10656 bool in_function_body = parser->in_function_body;
10658 if (nested)
10659 push_function_context ();
10660 else
10661 /* Still increment function_depth so that we don't GC in the
10662 middle of an expression. */
10663 ++function_depth;
10665 vec<tree> omp_privatization_save;
10666 save_omp_privatization_clauses (omp_privatization_save);
10667 /* Clear this in case we're in the middle of a default argument. */
10668 parser->local_variables_forbidden_p = false;
10669 parser->in_function_body = true;
10672 local_specialization_stack s (lss_copy);
10673 tree fco = lambda_function (lambda_expr);
10674 tree body = start_lambda_function (fco, lambda_expr);
10675 matching_braces braces;
10677 if (braces.require_open (parser))
10679 tree compound_stmt = begin_compound_stmt (0);
10681 /* Originally C++11 required us to peek for 'return expr'; and
10682 process it specially here to deduce the return type. N3638
10683 removed the need for that. */
10685 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10686 cp_parser_label_declaration (parser);
10687 cp_parser_statement_seq_opt (parser, NULL_TREE);
10688 braces.require_close (parser);
10690 finish_compound_stmt (compound_stmt);
10693 finish_lambda_function (body);
10696 restore_omp_privatization_clauses (omp_privatization_save);
10697 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10698 parser->in_function_body = in_function_body;
10699 if (nested)
10700 pop_function_context();
10701 else
10702 --function_depth;
10705 /* Statements [gram.stmt.stmt] */
10707 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10709 static void
10710 add_debug_begin_stmt (location_t loc)
10712 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10713 return;
10714 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10715 /* A concept is never expanded normally. */
10716 return;
10718 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10719 SET_EXPR_LOCATION (stmt, loc);
10720 add_stmt (stmt);
10723 /* Parse a statement.
10725 statement:
10726 labeled-statement
10727 expression-statement
10728 compound-statement
10729 selection-statement
10730 iteration-statement
10731 jump-statement
10732 declaration-statement
10733 try-block
10735 C++11:
10737 statement:
10738 labeled-statement
10739 attribute-specifier-seq (opt) expression-statement
10740 attribute-specifier-seq (opt) compound-statement
10741 attribute-specifier-seq (opt) selection-statement
10742 attribute-specifier-seq (opt) iteration-statement
10743 attribute-specifier-seq (opt) jump-statement
10744 declaration-statement
10745 attribute-specifier-seq (opt) try-block
10747 init-statement:
10748 expression-statement
10749 simple-declaration
10751 TM Extension:
10753 statement:
10754 atomic-statement
10756 IN_COMPOUND is true when the statement is nested inside a
10757 cp_parser_compound_statement; this matters for certain pragmas.
10759 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10760 is a (possibly labeled) if statement which is not enclosed in braces
10761 and has an else clause. This is used to implement -Wparentheses.
10763 CHAIN is a vector of if-else-if conditions. */
10765 static void
10766 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10767 bool in_compound, bool *if_p, vec<tree> *chain,
10768 location_t *loc_after_labels)
10770 tree statement, std_attrs = NULL_TREE;
10771 cp_token *token;
10772 location_t statement_location, attrs_location;
10774 restart:
10775 if (if_p != NULL)
10776 *if_p = false;
10777 /* There is no statement yet. */
10778 statement = NULL_TREE;
10780 saved_token_sentinel saved_tokens (parser->lexer);
10781 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10782 if (c_dialect_objc ())
10783 /* In obj-c++, seeing '[[' might be the either the beginning of
10784 c++11 attributes, or a nested objc-message-expression. So
10785 let's parse the c++11 attributes tentatively. */
10786 cp_parser_parse_tentatively (parser);
10787 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10788 if (c_dialect_objc ())
10790 if (!cp_parser_parse_definitely (parser))
10791 std_attrs = NULL_TREE;
10794 /* Peek at the next token. */
10795 token = cp_lexer_peek_token (parser->lexer);
10796 /* Remember the location of the first token in the statement. */
10797 statement_location = token->location;
10798 add_debug_begin_stmt (statement_location);
10799 /* If this is a keyword, then that will often determine what kind of
10800 statement we have. */
10801 if (token->type == CPP_KEYWORD)
10803 enum rid keyword = token->keyword;
10805 switch (keyword)
10807 case RID_CASE:
10808 case RID_DEFAULT:
10809 /* Looks like a labeled-statement with a case label.
10810 Parse the label, and then use tail recursion to parse
10811 the statement. */
10812 cp_parser_label_for_labeled_statement (parser, std_attrs);
10813 in_compound = false;
10814 goto restart;
10816 case RID_IF:
10817 case RID_SWITCH:
10818 statement = cp_parser_selection_statement (parser, if_p, chain);
10819 break;
10821 case RID_WHILE:
10822 case RID_DO:
10823 case RID_FOR:
10824 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10825 break;
10827 case RID_BREAK:
10828 case RID_CONTINUE:
10829 case RID_RETURN:
10830 case RID_GOTO:
10831 statement = cp_parser_jump_statement (parser);
10832 break;
10834 /* Objective-C++ exception-handling constructs. */
10835 case RID_AT_TRY:
10836 case RID_AT_CATCH:
10837 case RID_AT_FINALLY:
10838 case RID_AT_SYNCHRONIZED:
10839 case RID_AT_THROW:
10840 statement = cp_parser_objc_statement (parser);
10841 break;
10843 case RID_TRY:
10844 statement = cp_parser_try_block (parser);
10845 break;
10847 case RID_NAMESPACE:
10848 /* This must be a namespace alias definition. */
10849 cp_parser_declaration_statement (parser);
10850 return;
10852 case RID_TRANSACTION_ATOMIC:
10853 case RID_TRANSACTION_RELAXED:
10854 case RID_SYNCHRONIZED:
10855 case RID_ATOMIC_NOEXCEPT:
10856 case RID_ATOMIC_CANCEL:
10857 statement = cp_parser_transaction (parser, token);
10858 break;
10859 case RID_TRANSACTION_CANCEL:
10860 statement = cp_parser_transaction_cancel (parser);
10861 break;
10863 default:
10864 /* It might be a keyword like `int' that can start a
10865 declaration-statement. */
10866 break;
10869 else if (token->type == CPP_NAME)
10871 /* If the next token is a `:', then we are looking at a
10872 labeled-statement. */
10873 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10874 if (token->type == CPP_COLON)
10876 /* Looks like a labeled-statement with an ordinary label.
10877 Parse the label, and then use tail recursion to parse
10878 the statement. */
10880 cp_parser_label_for_labeled_statement (parser, std_attrs);
10881 in_compound = false;
10882 goto restart;
10885 /* Anything that starts with a `{' must be a compound-statement. */
10886 else if (token->type == CPP_OPEN_BRACE)
10887 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10888 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10889 a statement all its own. */
10890 else if (token->type == CPP_PRAGMA)
10892 /* Only certain OpenMP pragmas are attached to statements, and thus
10893 are considered statements themselves. All others are not. In
10894 the context of a compound, accept the pragma as a "statement" and
10895 return so that we can check for a close brace. Otherwise we
10896 require a real statement and must go back and read one. */
10897 if (in_compound)
10898 cp_parser_pragma (parser, pragma_compound, if_p);
10899 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10900 goto restart;
10901 return;
10903 else if (token->type == CPP_EOF)
10905 cp_parser_error (parser, "expected statement");
10906 return;
10909 /* Everything else must be a declaration-statement or an
10910 expression-statement. Try for the declaration-statement
10911 first, unless we are looking at a `;', in which case we know that
10912 we have an expression-statement. */
10913 if (!statement)
10915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10917 if (std_attrs != NULL_TREE)
10919 /* Attributes should be parsed as part of the the
10920 declaration, so let's un-parse them. */
10921 saved_tokens.rollback();
10922 std_attrs = NULL_TREE;
10925 cp_parser_parse_tentatively (parser);
10926 /* Try to parse the declaration-statement. */
10927 cp_parser_declaration_statement (parser);
10928 /* If that worked, we're done. */
10929 if (cp_parser_parse_definitely (parser))
10930 return;
10932 /* All preceding labels have been parsed at this point. */
10933 if (loc_after_labels != NULL)
10934 *loc_after_labels = statement_location;
10936 /* Look for an expression-statement instead. */
10937 statement = cp_parser_expression_statement (parser, in_statement_expr);
10939 /* Handle [[fallthrough]];. */
10940 if (attribute_fallthrough_p (std_attrs))
10942 /* The next token after the fallthrough attribute is ';'. */
10943 if (statement == NULL_TREE)
10945 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10946 statement = build_call_expr_internal_loc (statement_location,
10947 IFN_FALLTHROUGH,
10948 void_type_node, 0);
10949 finish_expr_stmt (statement);
10951 else
10952 warning_at (statement_location, OPT_Wattributes,
10953 "%<fallthrough%> attribute not followed by %<;%>");
10954 std_attrs = NULL_TREE;
10958 /* Set the line number for the statement. */
10959 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10960 SET_EXPR_LOCATION (statement, statement_location);
10962 /* Allow "[[fallthrough]];", but warn otherwise. */
10963 if (std_attrs != NULL_TREE)
10964 warning_at (attrs_location,
10965 OPT_Wattributes,
10966 "attributes at the beginning of statement are ignored");
10969 /* Append ATTR to attribute list ATTRS. */
10971 static tree
10972 attr_chainon (tree attrs, tree attr)
10974 if (attrs == error_mark_node)
10975 return error_mark_node;
10976 if (attr == error_mark_node)
10977 return error_mark_node;
10978 return chainon (attrs, attr);
10981 /* Parse the label for a labeled-statement, i.e.
10983 identifier :
10984 case constant-expression :
10985 default :
10987 GNU Extension:
10988 case constant-expression ... constant-expression : statement
10990 When a label is parsed without errors, the label is added to the
10991 parse tree by the finish_* functions, so this function doesn't
10992 have to return the label. */
10994 static void
10995 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10997 cp_token *token;
10998 tree label = NULL_TREE;
10999 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11001 /* The next token should be an identifier. */
11002 token = cp_lexer_peek_token (parser->lexer);
11003 if (token->type != CPP_NAME
11004 && token->type != CPP_KEYWORD)
11006 cp_parser_error (parser, "expected labeled-statement");
11007 return;
11010 /* Remember whether this case or a user-defined label is allowed to fall
11011 through to. */
11012 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11014 parser->colon_corrects_to_scope_p = false;
11015 switch (token->keyword)
11017 case RID_CASE:
11019 tree expr, expr_hi;
11020 cp_token *ellipsis;
11022 /* Consume the `case' token. */
11023 cp_lexer_consume_token (parser->lexer);
11024 /* Parse the constant-expression. */
11025 expr = cp_parser_constant_expression (parser);
11026 if (check_for_bare_parameter_packs (expr))
11027 expr = error_mark_node;
11029 ellipsis = cp_lexer_peek_token (parser->lexer);
11030 if (ellipsis->type == CPP_ELLIPSIS)
11032 /* Consume the `...' token. */
11033 cp_lexer_consume_token (parser->lexer);
11034 expr_hi = cp_parser_constant_expression (parser);
11035 if (check_for_bare_parameter_packs (expr_hi))
11036 expr_hi = error_mark_node;
11038 /* We don't need to emit warnings here, as the common code
11039 will do this for us. */
11041 else
11042 expr_hi = NULL_TREE;
11044 if (parser->in_switch_statement_p)
11046 tree l = finish_case_label (token->location, expr, expr_hi);
11047 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11048 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11050 else
11051 error_at (token->location,
11052 "case label %qE not within a switch statement",
11053 expr);
11055 break;
11057 case RID_DEFAULT:
11058 /* Consume the `default' token. */
11059 cp_lexer_consume_token (parser->lexer);
11061 if (parser->in_switch_statement_p)
11063 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11064 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11065 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11067 else
11068 error_at (token->location, "case label not within a switch statement");
11069 break;
11071 default:
11072 /* Anything else must be an ordinary label. */
11073 label = finish_label_stmt (cp_parser_identifier (parser));
11074 if (label && TREE_CODE (label) == LABEL_DECL)
11075 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11076 break;
11079 /* Require the `:' token. */
11080 cp_parser_require (parser, CPP_COLON, RT_COLON);
11082 /* An ordinary label may optionally be followed by attributes.
11083 However, this is only permitted if the attributes are then
11084 followed by a semicolon. This is because, for backward
11085 compatibility, when parsing
11086 lab: __attribute__ ((unused)) int i;
11087 we want the attribute to attach to "i", not "lab". */
11088 if (label != NULL_TREE
11089 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11091 tree attrs;
11092 cp_parser_parse_tentatively (parser);
11093 attrs = cp_parser_gnu_attributes_opt (parser);
11094 if (attrs == NULL_TREE
11095 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11096 cp_parser_abort_tentative_parse (parser);
11097 else if (!cp_parser_parse_definitely (parser))
11099 else
11100 attributes = attr_chainon (attributes, attrs);
11103 if (attributes != NULL_TREE)
11104 cplus_decl_attributes (&label, attributes, 0);
11106 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11109 /* Parse an expression-statement.
11111 expression-statement:
11112 expression [opt] ;
11114 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11115 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11116 indicates whether this expression-statement is part of an
11117 expression statement. */
11119 static tree
11120 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11122 tree statement = NULL_TREE;
11123 cp_token *token = cp_lexer_peek_token (parser->lexer);
11124 location_t loc = token->location;
11126 /* There might be attribute fallthrough. */
11127 tree attr = cp_parser_gnu_attributes_opt (parser);
11129 /* If the next token is a ';', then there is no expression
11130 statement. */
11131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11133 statement = cp_parser_expression (parser);
11134 if (statement == error_mark_node
11135 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11137 cp_parser_skip_to_end_of_block_or_statement (parser);
11138 return error_mark_node;
11142 /* Handle [[fallthrough]];. */
11143 if (attribute_fallthrough_p (attr))
11145 /* The next token after the fallthrough attribute is ';'. */
11146 if (statement == NULL_TREE)
11147 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11148 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11149 void_type_node, 0);
11150 else
11151 warning_at (loc, OPT_Wattributes,
11152 "%<fallthrough%> attribute not followed by %<;%>");
11153 attr = NULL_TREE;
11156 /* Allow "[[fallthrough]];", but warn otherwise. */
11157 if (attr != NULL_TREE)
11158 warning_at (loc, OPT_Wattributes,
11159 "attributes at the beginning of statement are ignored");
11161 /* Give a helpful message for "A<T>::type t;" and the like. */
11162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11163 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11165 if (TREE_CODE (statement) == SCOPE_REF)
11166 error_at (token->location, "need %<typename%> before %qE because "
11167 "%qT is a dependent scope",
11168 statement, TREE_OPERAND (statement, 0));
11169 else if (is_overloaded_fn (statement)
11170 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11172 /* A::A a; */
11173 tree fn = get_first_fn (statement);
11174 error_at (token->location,
11175 "%<%T::%D%> names the constructor, not the type",
11176 DECL_CONTEXT (fn), DECL_NAME (fn));
11180 /* Consume the final `;'. */
11181 cp_parser_consume_semicolon_at_end_of_statement (parser);
11183 if (in_statement_expr
11184 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11185 /* This is the final expression statement of a statement
11186 expression. */
11187 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11188 else if (statement)
11189 statement = finish_expr_stmt (statement);
11191 return statement;
11194 /* Parse a compound-statement.
11196 compound-statement:
11197 { statement-seq [opt] }
11199 GNU extension:
11201 compound-statement:
11202 { label-declaration-seq [opt] statement-seq [opt] }
11204 label-declaration-seq:
11205 label-declaration
11206 label-declaration-seq label-declaration
11208 Returns a tree representing the statement. */
11210 static tree
11211 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11212 int bcs_flags, bool function_body)
11214 tree compound_stmt;
11215 matching_braces braces;
11217 /* Consume the `{'. */
11218 if (!braces.require_open (parser))
11219 return error_mark_node;
11220 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11221 && !function_body && cxx_dialect < cxx14)
11222 pedwarn (input_location, OPT_Wpedantic,
11223 "compound-statement in %<constexpr%> function");
11224 /* Begin the compound-statement. */
11225 compound_stmt = begin_compound_stmt (bcs_flags);
11226 /* If the next keyword is `__label__' we have a label declaration. */
11227 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11228 cp_parser_label_declaration (parser);
11229 /* Parse an (optional) statement-seq. */
11230 cp_parser_statement_seq_opt (parser, in_statement_expr);
11231 /* Finish the compound-statement. */
11232 finish_compound_stmt (compound_stmt);
11233 /* Consume the `}'. */
11234 braces.require_close (parser);
11236 return compound_stmt;
11239 /* Parse an (optional) statement-seq.
11241 statement-seq:
11242 statement
11243 statement-seq [opt] statement */
11245 static void
11246 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11248 /* Scan statements until there aren't any more. */
11249 while (true)
11251 cp_token *token = cp_lexer_peek_token (parser->lexer);
11253 /* If we are looking at a `}', then we have run out of
11254 statements; the same is true if we have reached the end
11255 of file, or have stumbled upon a stray '@end'. */
11256 if (token->type == CPP_CLOSE_BRACE
11257 || token->type == CPP_EOF
11258 || token->type == CPP_PRAGMA_EOL
11259 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11260 break;
11262 /* If we are in a compound statement and find 'else' then
11263 something went wrong. */
11264 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11266 if (parser->in_statement & IN_IF_STMT)
11267 break;
11268 else
11270 token = cp_lexer_consume_token (parser->lexer);
11271 error_at (token->location, "%<else%> without a previous %<if%>");
11275 /* Parse the statement. */
11276 cp_parser_statement (parser, in_statement_expr, true, NULL);
11280 /* Return true if we're looking at (init; cond), false otherwise. */
11282 static bool
11283 cp_parser_init_statement_p (cp_parser *parser)
11285 /* Save tokens so that we can put them back. */
11286 cp_lexer_save_tokens (parser->lexer);
11288 /* Look for ';' that is not nested in () or {}. */
11289 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11290 /*recovering=*/false,
11291 CPP_SEMICOLON,
11292 /*consume_paren=*/false);
11294 /* Roll back the tokens we skipped. */
11295 cp_lexer_rollback_tokens (parser->lexer);
11297 return ret == -1;
11300 /* Parse a selection-statement.
11302 selection-statement:
11303 if ( init-statement [opt] condition ) statement
11304 if ( init-statement [opt] condition ) statement else statement
11305 switch ( init-statement [opt] condition ) statement
11307 Returns the new IF_STMT or SWITCH_STMT.
11309 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11310 is a (possibly labeled) if statement which is not enclosed in
11311 braces and has an else clause. This is used to implement
11312 -Wparentheses.
11314 CHAIN is a vector of if-else-if conditions. This is used to implement
11315 -Wduplicated-cond. */
11317 static tree
11318 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11319 vec<tree> *chain)
11321 cp_token *token;
11322 enum rid keyword;
11323 token_indent_info guard_tinfo;
11325 if (if_p != NULL)
11326 *if_p = false;
11328 /* Peek at the next token. */
11329 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11330 guard_tinfo = get_token_indent_info (token);
11332 /* See what kind of keyword it is. */
11333 keyword = token->keyword;
11334 switch (keyword)
11336 case RID_IF:
11337 case RID_SWITCH:
11339 tree statement;
11340 tree condition;
11342 bool cx = false;
11343 if (keyword == RID_IF
11344 && cp_lexer_next_token_is_keyword (parser->lexer,
11345 RID_CONSTEXPR))
11347 cx = true;
11348 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11349 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11350 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11351 "with -std=c++17 or -std=gnu++17");
11354 /* Look for the `('. */
11355 matching_parens parens;
11356 if (!parens.require_open (parser))
11358 cp_parser_skip_to_end_of_statement (parser);
11359 return error_mark_node;
11362 /* Begin the selection-statement. */
11363 if (keyword == RID_IF)
11365 statement = begin_if_stmt ();
11366 IF_STMT_CONSTEXPR_P (statement) = cx;
11368 else
11369 statement = begin_switch_stmt ();
11371 /* Parse the optional init-statement. */
11372 if (cp_parser_init_statement_p (parser))
11374 tree decl;
11375 if (cxx_dialect < cxx17)
11376 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11377 "init-statement in selection statements only available "
11378 "with -std=c++17 or -std=gnu++17");
11379 cp_parser_init_statement (parser, &decl);
11382 /* Parse the condition. */
11383 condition = cp_parser_condition (parser);
11384 /* Look for the `)'. */
11385 if (!parens.require_close (parser))
11386 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11387 /*consume_paren=*/true);
11389 if (keyword == RID_IF)
11391 bool nested_if;
11392 unsigned char in_statement;
11394 /* Add the condition. */
11395 condition = finish_if_stmt_cond (condition, statement);
11397 if (warn_duplicated_cond)
11398 warn_duplicated_cond_add_or_warn (token->location, condition,
11399 &chain);
11401 /* Parse the then-clause. */
11402 in_statement = parser->in_statement;
11403 parser->in_statement |= IN_IF_STMT;
11405 /* Outside a template, the non-selected branch of a constexpr
11406 if is a 'discarded statement', i.e. unevaluated. */
11407 bool was_discarded = in_discarded_stmt;
11408 bool discard_then = (cx && !processing_template_decl
11409 && integer_zerop (condition));
11410 if (discard_then)
11412 in_discarded_stmt = true;
11413 ++c_inhibit_evaluation_warnings;
11416 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11417 guard_tinfo);
11419 parser->in_statement = in_statement;
11421 finish_then_clause (statement);
11423 if (discard_then)
11425 THEN_CLAUSE (statement) = NULL_TREE;
11426 in_discarded_stmt = was_discarded;
11427 --c_inhibit_evaluation_warnings;
11430 /* If the next token is `else', parse the else-clause. */
11431 if (cp_lexer_next_token_is_keyword (parser->lexer,
11432 RID_ELSE))
11434 bool discard_else = (cx && !processing_template_decl
11435 && integer_nonzerop (condition));
11436 if (discard_else)
11438 in_discarded_stmt = true;
11439 ++c_inhibit_evaluation_warnings;
11442 guard_tinfo
11443 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11444 /* Consume the `else' keyword. */
11445 cp_lexer_consume_token (parser->lexer);
11446 if (warn_duplicated_cond)
11448 if (cp_lexer_next_token_is_keyword (parser->lexer,
11449 RID_IF)
11450 && chain == NULL)
11452 /* We've got "if (COND) else if (COND2)". Start
11453 the condition chain and add COND as the first
11454 element. */
11455 chain = new vec<tree> ();
11456 if (!CONSTANT_CLASS_P (condition)
11457 && !TREE_SIDE_EFFECTS (condition))
11459 /* Wrap it in a NOP_EXPR so that we can set the
11460 location of the condition. */
11461 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11462 condition);
11463 SET_EXPR_LOCATION (e, token->location);
11464 chain->safe_push (e);
11467 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11468 RID_IF))
11470 /* This is if-else without subsequent if. Zap the
11471 condition chain; we would have already warned at
11472 this point. */
11473 delete chain;
11474 chain = NULL;
11477 begin_else_clause (statement);
11478 /* Parse the else-clause. */
11479 cp_parser_implicitly_scoped_statement (parser, NULL,
11480 guard_tinfo, chain);
11482 finish_else_clause (statement);
11484 /* If we are currently parsing a then-clause, then
11485 IF_P will not be NULL. We set it to true to
11486 indicate that this if statement has an else clause.
11487 This may trigger the Wparentheses warning below
11488 when we get back up to the parent if statement. */
11489 if (if_p != NULL)
11490 *if_p = true;
11492 if (discard_else)
11494 ELSE_CLAUSE (statement) = NULL_TREE;
11495 in_discarded_stmt = was_discarded;
11496 --c_inhibit_evaluation_warnings;
11499 else
11501 /* This if statement does not have an else clause. If
11502 NESTED_IF is true, then the then-clause has an if
11503 statement which does have an else clause. We warn
11504 about the potential ambiguity. */
11505 if (nested_if)
11506 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11507 "suggest explicit braces to avoid ambiguous"
11508 " %<else%>");
11509 if (warn_duplicated_cond)
11511 /* We don't need the condition chain anymore. */
11512 delete chain;
11513 chain = NULL;
11517 /* Now we're all done with the if-statement. */
11518 finish_if_stmt (statement);
11520 else
11522 bool in_switch_statement_p;
11523 unsigned char in_statement;
11525 /* Add the condition. */
11526 finish_switch_cond (condition, statement);
11528 /* Parse the body of the switch-statement. */
11529 in_switch_statement_p = parser->in_switch_statement_p;
11530 in_statement = parser->in_statement;
11531 parser->in_switch_statement_p = true;
11532 parser->in_statement |= IN_SWITCH_STMT;
11533 cp_parser_implicitly_scoped_statement (parser, if_p,
11534 guard_tinfo);
11535 parser->in_switch_statement_p = in_switch_statement_p;
11536 parser->in_statement = in_statement;
11538 /* Now we're all done with the switch-statement. */
11539 finish_switch_stmt (statement);
11542 return statement;
11544 break;
11546 default:
11547 cp_parser_error (parser, "expected selection-statement");
11548 return error_mark_node;
11552 /* Parse a condition.
11554 condition:
11555 expression
11556 type-specifier-seq declarator = initializer-clause
11557 type-specifier-seq declarator braced-init-list
11559 GNU Extension:
11561 condition:
11562 type-specifier-seq declarator asm-specification [opt]
11563 attributes [opt] = assignment-expression
11565 Returns the expression that should be tested. */
11567 static tree
11568 cp_parser_condition (cp_parser* parser)
11570 cp_decl_specifier_seq type_specifiers;
11571 const char *saved_message;
11572 int declares_class_or_enum;
11574 /* Try the declaration first. */
11575 cp_parser_parse_tentatively (parser);
11576 /* New types are not allowed in the type-specifier-seq for a
11577 condition. */
11578 saved_message = parser->type_definition_forbidden_message;
11579 parser->type_definition_forbidden_message
11580 = G_("types may not be defined in conditions");
11581 /* Parse the type-specifier-seq. */
11582 cp_parser_decl_specifier_seq (parser,
11583 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11584 &type_specifiers,
11585 &declares_class_or_enum);
11586 /* Restore the saved message. */
11587 parser->type_definition_forbidden_message = saved_message;
11588 /* If all is well, we might be looking at a declaration. */
11589 if (!cp_parser_error_occurred (parser))
11591 tree decl;
11592 tree asm_specification;
11593 tree attributes;
11594 cp_declarator *declarator;
11595 tree initializer = NULL_TREE;
11597 /* Parse the declarator. */
11598 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11599 /*ctor_dtor_or_conv_p=*/NULL,
11600 /*parenthesized_p=*/NULL,
11601 /*member_p=*/false,
11602 /*friend_p=*/false);
11603 /* Parse the attributes. */
11604 attributes = cp_parser_attributes_opt (parser);
11605 /* Parse the asm-specification. */
11606 asm_specification = cp_parser_asm_specification_opt (parser);
11607 /* If the next token is not an `=' or '{', then we might still be
11608 looking at an expression. For example:
11610 if (A(a).x)
11612 looks like a decl-specifier-seq and a declarator -- but then
11613 there is no `=', so this is an expression. */
11614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11615 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11616 cp_parser_simulate_error (parser);
11618 /* If we did see an `=' or '{', then we are looking at a declaration
11619 for sure. */
11620 if (cp_parser_parse_definitely (parser))
11622 tree pushed_scope;
11623 bool non_constant_p;
11624 int flags = LOOKUP_ONLYCONVERTING;
11626 /* Create the declaration. */
11627 decl = start_decl (declarator, &type_specifiers,
11628 /*initialized_p=*/true,
11629 attributes, /*prefix_attributes=*/NULL_TREE,
11630 &pushed_scope);
11632 /* Parse the initializer. */
11633 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11635 initializer = cp_parser_braced_list (parser, &non_constant_p);
11636 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11637 flags = 0;
11639 else
11641 /* Consume the `='. */
11642 cp_parser_require (parser, CPP_EQ, RT_EQ);
11643 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11645 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11646 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11648 /* Process the initializer. */
11649 cp_finish_decl (decl,
11650 initializer, !non_constant_p,
11651 asm_specification,
11652 flags);
11654 if (pushed_scope)
11655 pop_scope (pushed_scope);
11657 return convert_from_reference (decl);
11660 /* If we didn't even get past the declarator successfully, we are
11661 definitely not looking at a declaration. */
11662 else
11663 cp_parser_abort_tentative_parse (parser);
11665 /* Otherwise, we are looking at an expression. */
11666 return cp_parser_expression (parser);
11669 /* Parses a for-statement or range-for-statement until the closing ')',
11670 not included. */
11672 static tree
11673 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11675 tree init, scope, decl;
11676 bool is_range_for;
11678 /* Begin the for-statement. */
11679 scope = begin_for_scope (&init);
11681 /* Parse the initialization. */
11682 is_range_for = cp_parser_init_statement (parser, &decl);
11684 if (is_range_for)
11685 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11686 else
11687 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11690 static tree
11691 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11692 unsigned short unroll)
11694 /* Normal for loop */
11695 tree condition = NULL_TREE;
11696 tree expression = NULL_TREE;
11697 tree stmt;
11699 stmt = begin_for_stmt (scope, init);
11700 /* The init-statement has already been parsed in
11701 cp_parser_init_statement, so no work is needed here. */
11702 finish_init_stmt (stmt);
11704 /* If there's a condition, process it. */
11705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11706 condition = cp_parser_condition (parser);
11707 else if (ivdep)
11709 cp_parser_error (parser, "missing loop condition in loop with "
11710 "%<GCC ivdep%> pragma");
11711 condition = error_mark_node;
11713 else if (unroll)
11715 cp_parser_error (parser, "missing loop condition in loop with "
11716 "%<GCC unroll%> pragma");
11717 condition = error_mark_node;
11719 finish_for_cond (condition, stmt, ivdep, unroll);
11720 /* Look for the `;'. */
11721 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11723 /* If there's an expression, process it. */
11724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11725 expression = cp_parser_expression (parser);
11726 finish_for_expr (expression, stmt);
11728 return stmt;
11731 /* Tries to parse a range-based for-statement:
11733 range-based-for:
11734 decl-specifier-seq declarator : expression
11736 The decl-specifier-seq declarator and the `:' are already parsed by
11737 cp_parser_init_statement. If processing_template_decl it returns a
11738 newly created RANGE_FOR_STMT; if not, it is converted to a
11739 regular FOR_STMT. */
11741 static tree
11742 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11743 bool ivdep, unsigned short unroll)
11745 tree stmt, range_expr;
11746 auto_vec <cxx_binding *, 16> bindings;
11747 auto_vec <tree, 16> names;
11748 tree decomp_first_name = NULL_TREE;
11749 unsigned int decomp_cnt = 0;
11751 /* Get the range declaration momentarily out of the way so that
11752 the range expression doesn't clash with it. */
11753 if (range_decl != error_mark_node)
11755 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11757 tree v = DECL_VALUE_EXPR (range_decl);
11758 /* For decomposition declaration get all of the corresponding
11759 declarations out of the way. */
11760 if (TREE_CODE (v) == ARRAY_REF
11761 && VAR_P (TREE_OPERAND (v, 0))
11762 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11764 tree d = range_decl;
11765 range_decl = TREE_OPERAND (v, 0);
11766 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11767 decomp_first_name = d;
11768 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11770 tree name = DECL_NAME (d);
11771 names.safe_push (name);
11772 bindings.safe_push (IDENTIFIER_BINDING (name));
11773 IDENTIFIER_BINDING (name)
11774 = IDENTIFIER_BINDING (name)->previous;
11778 if (names.is_empty ())
11780 tree name = DECL_NAME (range_decl);
11781 names.safe_push (name);
11782 bindings.safe_push (IDENTIFIER_BINDING (name));
11783 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11787 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11789 bool expr_non_constant_p;
11790 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11792 else
11793 range_expr = cp_parser_expression (parser);
11795 /* Put the range declaration(s) back into scope. */
11796 for (unsigned int i = 0; i < names.length (); i++)
11798 cxx_binding *binding = bindings[i];
11799 binding->previous = IDENTIFIER_BINDING (names[i]);
11800 IDENTIFIER_BINDING (names[i]) = binding;
11803 /* If in template, STMT is converted to a normal for-statement
11804 at instantiation. If not, it is done just ahead. */
11805 if (processing_template_decl)
11807 if (check_for_bare_parameter_packs (range_expr))
11808 range_expr = error_mark_node;
11809 stmt = begin_range_for_stmt (scope, init);
11810 if (ivdep)
11811 RANGE_FOR_IVDEP (stmt) = 1;
11812 if (unroll)
11813 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11814 finish_range_for_decl (stmt, range_decl, range_expr);
11815 if (!type_dependent_expression_p (range_expr)
11816 /* do_auto_deduction doesn't mess with template init-lists. */
11817 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11818 do_range_for_auto_deduction (range_decl, range_expr);
11820 else
11822 stmt = begin_for_stmt (scope, init);
11823 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11824 decomp_first_name, decomp_cnt, ivdep,
11825 unroll);
11827 return stmt;
11830 /* Subroutine of cp_convert_range_for: given the initializer expression,
11831 builds up the range temporary. */
11833 static tree
11834 build_range_temp (tree range_expr)
11836 tree range_type, range_temp;
11838 /* Find out the type deduced by the declaration
11839 `auto &&__range = range_expr'. */
11840 range_type = cp_build_reference_type (make_auto (), true);
11841 range_type = do_auto_deduction (range_type, range_expr,
11842 type_uses_auto (range_type));
11844 /* Create the __range variable. */
11845 range_temp = build_decl (input_location, VAR_DECL,
11846 get_identifier ("__for_range"), range_type);
11847 TREE_USED (range_temp) = 1;
11848 DECL_ARTIFICIAL (range_temp) = 1;
11850 return range_temp;
11853 /* Used by cp_parser_range_for in template context: we aren't going to
11854 do a full conversion yet, but we still need to resolve auto in the
11855 type of the for-range-declaration if present. This is basically
11856 a shortcut version of cp_convert_range_for. */
11858 static void
11859 do_range_for_auto_deduction (tree decl, tree range_expr)
11861 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11862 if (auto_node)
11864 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11865 range_temp = convert_from_reference (build_range_temp (range_expr));
11866 iter_type = (cp_parser_perform_range_for_lookup
11867 (range_temp, &begin_dummy, &end_dummy));
11868 if (iter_type)
11870 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11871 iter_type);
11872 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11873 RO_UNARY_STAR,
11874 tf_warning_or_error);
11875 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11876 iter_decl, auto_node);
11881 /* Converts a range-based for-statement into a normal
11882 for-statement, as per the definition.
11884 for (RANGE_DECL : RANGE_EXPR)
11885 BLOCK
11887 should be equivalent to:
11890 auto &&__range = RANGE_EXPR;
11891 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11892 __begin != __end;
11893 ++__begin)
11895 RANGE_DECL = *__begin;
11896 BLOCK
11900 If RANGE_EXPR is an array:
11901 BEGIN_EXPR = __range
11902 END_EXPR = __range + ARRAY_SIZE(__range)
11903 Else if RANGE_EXPR has a member 'begin' or 'end':
11904 BEGIN_EXPR = __range.begin()
11905 END_EXPR = __range.end()
11906 Else:
11907 BEGIN_EXPR = begin(__range)
11908 END_EXPR = end(__range);
11910 If __range has a member 'begin' but not 'end', or vice versa, we must
11911 still use the second alternative (it will surely fail, however).
11912 When calling begin()/end() in the third alternative we must use
11913 argument dependent lookup, but always considering 'std' as an associated
11914 namespace. */
11916 tree
11917 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11918 tree decomp_first_name, unsigned int decomp_cnt,
11919 bool ivdep, unsigned short unroll)
11921 tree begin, end;
11922 tree iter_type, begin_expr, end_expr;
11923 tree condition, expression;
11925 range_expr = mark_lvalue_use (range_expr);
11927 if (range_decl == error_mark_node || range_expr == error_mark_node)
11928 /* If an error happened previously do nothing or else a lot of
11929 unhelpful errors would be issued. */
11930 begin_expr = end_expr = iter_type = error_mark_node;
11931 else
11933 tree range_temp;
11935 if (VAR_P (range_expr)
11936 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11937 /* Can't bind a reference to an array of runtime bound. */
11938 range_temp = range_expr;
11939 else
11941 range_temp = build_range_temp (range_expr);
11942 pushdecl (range_temp);
11943 cp_finish_decl (range_temp, range_expr,
11944 /*is_constant_init*/false, NULL_TREE,
11945 LOOKUP_ONLYCONVERTING);
11946 range_temp = convert_from_reference (range_temp);
11948 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11949 &begin_expr, &end_expr);
11952 /* The new for initialization statement. */
11953 begin = build_decl (input_location, VAR_DECL,
11954 get_identifier ("__for_begin"), iter_type);
11955 TREE_USED (begin) = 1;
11956 DECL_ARTIFICIAL (begin) = 1;
11957 pushdecl (begin);
11958 cp_finish_decl (begin, begin_expr,
11959 /*is_constant_init*/false, NULL_TREE,
11960 LOOKUP_ONLYCONVERTING);
11962 if (cxx_dialect >= cxx17)
11963 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11964 end = build_decl (input_location, VAR_DECL,
11965 get_identifier ("__for_end"), iter_type);
11966 TREE_USED (end) = 1;
11967 DECL_ARTIFICIAL (end) = 1;
11968 pushdecl (end);
11969 cp_finish_decl (end, end_expr,
11970 /*is_constant_init*/false, NULL_TREE,
11971 LOOKUP_ONLYCONVERTING);
11973 finish_init_stmt (statement);
11975 /* The new for condition. */
11976 condition = build_x_binary_op (input_location, NE_EXPR,
11977 begin, ERROR_MARK,
11978 end, ERROR_MARK,
11979 NULL, tf_warning_or_error);
11980 finish_for_cond (condition, statement, ivdep, unroll);
11982 /* The new increment expression. */
11983 expression = finish_unary_op_expr (input_location,
11984 PREINCREMENT_EXPR, begin,
11985 tf_warning_or_error);
11986 finish_for_expr (expression, statement);
11988 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11989 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11991 /* The declaration is initialized with *__begin inside the loop body. */
11992 cp_finish_decl (range_decl,
11993 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11994 tf_warning_or_error),
11995 /*is_constant_init*/false, NULL_TREE,
11996 LOOKUP_ONLYCONVERTING);
11997 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11998 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12000 return statement;
12003 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12004 We need to solve both at the same time because the method used
12005 depends on the existence of members begin or end.
12006 Returns the type deduced for the iterator expression. */
12008 static tree
12009 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12011 if (error_operand_p (range))
12013 *begin = *end = error_mark_node;
12014 return error_mark_node;
12017 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12019 error ("range-based %<for%> expression of type %qT "
12020 "has incomplete type", TREE_TYPE (range));
12021 *begin = *end = error_mark_node;
12022 return error_mark_node;
12024 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12026 /* If RANGE is an array, we will use pointer arithmetic. */
12027 *begin = decay_conversion (range, tf_warning_or_error);
12028 *end = build_binary_op (input_location, PLUS_EXPR,
12029 range,
12030 array_type_nelts_top (TREE_TYPE (range)),
12031 false);
12032 return TREE_TYPE (*begin);
12034 else
12036 /* If it is not an array, we must do a bit of magic. */
12037 tree id_begin, id_end;
12038 tree member_begin, member_end;
12040 *begin = *end = error_mark_node;
12042 id_begin = get_identifier ("begin");
12043 id_end = get_identifier ("end");
12044 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12045 /*protect=*/2, /*want_type=*/false,
12046 tf_warning_or_error);
12047 member_end = lookup_member (TREE_TYPE (range), id_end,
12048 /*protect=*/2, /*want_type=*/false,
12049 tf_warning_or_error);
12051 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12053 /* Use the member functions. */
12054 if (member_begin != NULL_TREE)
12055 *begin = cp_parser_range_for_member_function (range, id_begin);
12056 else
12057 error ("range-based %<for%> expression of type %qT has an "
12058 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12060 if (member_end != NULL_TREE)
12061 *end = cp_parser_range_for_member_function (range, id_end);
12062 else
12063 error ("range-based %<for%> expression of type %qT has a "
12064 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12066 else
12068 /* Use global functions with ADL. */
12069 vec<tree, va_gc> *vec;
12070 vec = make_tree_vector ();
12072 vec_safe_push (vec, range);
12074 member_begin = perform_koenig_lookup (id_begin, vec,
12075 tf_warning_or_error);
12076 *begin = finish_call_expr (member_begin, &vec, false, true,
12077 tf_warning_or_error);
12078 member_end = perform_koenig_lookup (id_end, vec,
12079 tf_warning_or_error);
12080 *end = finish_call_expr (member_end, &vec, false, true,
12081 tf_warning_or_error);
12083 release_tree_vector (vec);
12086 /* Last common checks. */
12087 if (*begin == error_mark_node || *end == error_mark_node)
12089 /* If one of the expressions is an error do no more checks. */
12090 *begin = *end = error_mark_node;
12091 return error_mark_node;
12093 else if (type_dependent_expression_p (*begin)
12094 || type_dependent_expression_p (*end))
12095 /* Can happen, when, eg, in a template context, Koenig lookup
12096 can't resolve begin/end (c++/58503). */
12097 return NULL_TREE;
12098 else
12100 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12101 /* The unqualified type of the __begin and __end temporaries should
12102 be the same, as required by the multiple auto declaration. */
12103 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12105 if (cxx_dialect >= cxx17
12106 && (build_x_binary_op (input_location, NE_EXPR,
12107 *begin, ERROR_MARK,
12108 *end, ERROR_MARK,
12109 NULL, tf_none)
12110 != error_mark_node))
12111 /* P0184R0 allows __begin and __end to have different types,
12112 but make sure they are comparable so we can give a better
12113 diagnostic. */;
12114 else
12115 error ("inconsistent begin/end types in range-based %<for%> "
12116 "statement: %qT and %qT",
12117 TREE_TYPE (*begin), TREE_TYPE (*end));
12119 return iter_type;
12124 /* Helper function for cp_parser_perform_range_for_lookup.
12125 Builds a tree for RANGE.IDENTIFIER(). */
12127 static tree
12128 cp_parser_range_for_member_function (tree range, tree identifier)
12130 tree member, res;
12131 vec<tree, va_gc> *vec;
12133 member = finish_class_member_access_expr (range, identifier,
12134 false, tf_warning_or_error);
12135 if (member == error_mark_node)
12136 return error_mark_node;
12138 vec = make_tree_vector ();
12139 res = finish_call_expr (member, &vec,
12140 /*disallow_virtual=*/false,
12141 /*koenig_p=*/false,
12142 tf_warning_or_error);
12143 release_tree_vector (vec);
12144 return res;
12147 /* Parse an iteration-statement.
12149 iteration-statement:
12150 while ( condition ) statement
12151 do statement while ( expression ) ;
12152 for ( init-statement condition [opt] ; expression [opt] )
12153 statement
12155 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12157 static tree
12158 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12159 unsigned short unroll)
12161 cp_token *token;
12162 enum rid keyword;
12163 tree statement;
12164 unsigned char in_statement;
12165 token_indent_info guard_tinfo;
12167 /* Peek at the next token. */
12168 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12169 if (!token)
12170 return error_mark_node;
12172 guard_tinfo = get_token_indent_info (token);
12174 /* Remember whether or not we are already within an iteration
12175 statement. */
12176 in_statement = parser->in_statement;
12178 /* See what kind of keyword it is. */
12179 keyword = token->keyword;
12180 switch (keyword)
12182 case RID_WHILE:
12184 tree condition;
12186 /* Begin the while-statement. */
12187 statement = begin_while_stmt ();
12188 /* Look for the `('. */
12189 matching_parens parens;
12190 parens.require_open (parser);
12191 /* Parse the condition. */
12192 condition = cp_parser_condition (parser);
12193 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12194 /* Look for the `)'. */
12195 parens.require_close (parser);
12196 /* Parse the dependent statement. */
12197 parser->in_statement = IN_ITERATION_STMT;
12198 bool prev = note_iteration_stmt_body_start ();
12199 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12200 note_iteration_stmt_body_end (prev);
12201 parser->in_statement = in_statement;
12202 /* We're done with the while-statement. */
12203 finish_while_stmt (statement);
12205 break;
12207 case RID_DO:
12209 tree expression;
12211 /* Begin the do-statement. */
12212 statement = begin_do_stmt ();
12213 /* Parse the body of the do-statement. */
12214 parser->in_statement = IN_ITERATION_STMT;
12215 bool prev = note_iteration_stmt_body_start ();
12216 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12217 note_iteration_stmt_body_end (prev);
12218 parser->in_statement = in_statement;
12219 finish_do_body (statement);
12220 /* Look for the `while' keyword. */
12221 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12222 /* Look for the `('. */
12223 matching_parens parens;
12224 parens.require_open (parser);
12225 /* Parse the expression. */
12226 expression = cp_parser_expression (parser);
12227 /* We're done with the do-statement. */
12228 finish_do_stmt (expression, statement, ivdep, unroll);
12229 /* Look for the `)'. */
12230 parens.require_close (parser);
12231 /* Look for the `;'. */
12232 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12234 break;
12236 case RID_FOR:
12238 /* Look for the `('. */
12239 matching_parens parens;
12240 parens.require_open (parser);
12242 statement = cp_parser_for (parser, ivdep, unroll);
12244 /* Look for the `)'. */
12245 parens.require_close (parser);
12247 /* Parse the body of the for-statement. */
12248 parser->in_statement = IN_ITERATION_STMT;
12249 bool prev = note_iteration_stmt_body_start ();
12250 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12251 note_iteration_stmt_body_end (prev);
12252 parser->in_statement = in_statement;
12254 /* We're done with the for-statement. */
12255 finish_for_stmt (statement);
12257 break;
12259 default:
12260 cp_parser_error (parser, "expected iteration-statement");
12261 statement = error_mark_node;
12262 break;
12265 return statement;
12268 /* Parse a init-statement or the declarator of a range-based-for.
12269 Returns true if a range-based-for declaration is seen.
12271 init-statement:
12272 expression-statement
12273 simple-declaration */
12275 static bool
12276 cp_parser_init_statement (cp_parser* parser, tree *decl)
12278 /* If the next token is a `;', then we have an empty
12279 expression-statement. Grammatically, this is also a
12280 simple-declaration, but an invalid one, because it does not
12281 declare anything. Therefore, if we did not handle this case
12282 specially, we would issue an error message about an invalid
12283 declaration. */
12284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12286 bool is_range_for = false;
12287 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12289 /* A colon is used in range-based for. */
12290 parser->colon_corrects_to_scope_p = false;
12292 /* We're going to speculatively look for a declaration, falling back
12293 to an expression, if necessary. */
12294 cp_parser_parse_tentatively (parser);
12295 /* Parse the declaration. */
12296 cp_parser_simple_declaration (parser,
12297 /*function_definition_allowed_p=*/false,
12298 decl);
12299 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12300 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12302 /* It is a range-for, consume the ':' */
12303 cp_lexer_consume_token (parser->lexer);
12304 is_range_for = true;
12305 if (cxx_dialect < cxx11)
12307 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12308 "range-based %<for%> loops only available with "
12309 "-std=c++11 or -std=gnu++11");
12310 *decl = error_mark_node;
12313 else
12314 /* The ';' is not consumed yet because we told
12315 cp_parser_simple_declaration not to. */
12316 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12318 if (cp_parser_parse_definitely (parser))
12319 return is_range_for;
12320 /* If the tentative parse failed, then we shall need to look for an
12321 expression-statement. */
12323 /* If we are here, it is an expression-statement. */
12324 cp_parser_expression_statement (parser, NULL_TREE);
12325 return false;
12328 /* Parse a jump-statement.
12330 jump-statement:
12331 break ;
12332 continue ;
12333 return expression [opt] ;
12334 return braced-init-list ;
12335 goto identifier ;
12337 GNU extension:
12339 jump-statement:
12340 goto * expression ;
12342 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12344 static tree
12345 cp_parser_jump_statement (cp_parser* parser)
12347 tree statement = error_mark_node;
12348 cp_token *token;
12349 enum rid keyword;
12350 unsigned char in_statement;
12352 /* Peek at the next token. */
12353 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12354 if (!token)
12355 return error_mark_node;
12357 /* See what kind of keyword it is. */
12358 keyword = token->keyword;
12359 switch (keyword)
12361 case RID_BREAK:
12362 in_statement = parser->in_statement & ~IN_IF_STMT;
12363 switch (in_statement)
12365 case 0:
12366 error_at (token->location, "break statement not within loop or switch");
12367 break;
12368 default:
12369 gcc_assert ((in_statement & IN_SWITCH_STMT)
12370 || in_statement == IN_ITERATION_STMT);
12371 statement = finish_break_stmt ();
12372 if (in_statement == IN_ITERATION_STMT)
12373 break_maybe_infinite_loop ();
12374 break;
12375 case IN_OMP_BLOCK:
12376 error_at (token->location, "invalid exit from OpenMP structured block");
12377 break;
12378 case IN_OMP_FOR:
12379 error_at (token->location, "break statement used with OpenMP for loop");
12380 break;
12382 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12383 break;
12385 case RID_CONTINUE:
12386 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12388 case 0:
12389 error_at (token->location, "continue statement not within a loop");
12390 break;
12391 /* Fall through. */
12392 case IN_ITERATION_STMT:
12393 case IN_OMP_FOR:
12394 statement = finish_continue_stmt ();
12395 break;
12396 case IN_OMP_BLOCK:
12397 error_at (token->location, "invalid exit from OpenMP structured block");
12398 break;
12399 default:
12400 gcc_unreachable ();
12402 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12403 break;
12405 case RID_RETURN:
12407 tree expr;
12408 bool expr_non_constant_p;
12410 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12412 cp_lexer_set_source_position (parser->lexer);
12413 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12414 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12416 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12417 expr = cp_parser_expression (parser);
12418 else
12419 /* If the next token is a `;', then there is no
12420 expression. */
12421 expr = NULL_TREE;
12422 /* Build the return-statement. */
12423 if (current_function_auto_return_pattern && in_discarded_stmt)
12424 /* Don't deduce from a discarded return statement. */;
12425 else
12426 statement = finish_return_stmt (expr);
12427 /* Look for the final `;'. */
12428 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12430 break;
12432 case RID_GOTO:
12433 if (parser->in_function_body
12434 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12436 error ("%<goto%> in %<constexpr%> function");
12437 cp_function_chain->invalid_constexpr = true;
12440 /* Create the goto-statement. */
12441 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12443 /* Issue a warning about this use of a GNU extension. */
12444 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12445 /* Consume the '*' token. */
12446 cp_lexer_consume_token (parser->lexer);
12447 /* Parse the dependent expression. */
12448 finish_goto_stmt (cp_parser_expression (parser));
12450 else
12451 finish_goto_stmt (cp_parser_identifier (parser));
12452 /* Look for the final `;'. */
12453 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12454 break;
12456 default:
12457 cp_parser_error (parser, "expected jump-statement");
12458 break;
12461 return statement;
12464 /* Parse a declaration-statement.
12466 declaration-statement:
12467 block-declaration */
12469 static void
12470 cp_parser_declaration_statement (cp_parser* parser)
12472 void *p;
12474 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12475 p = obstack_alloc (&declarator_obstack, 0);
12477 /* Parse the block-declaration. */
12478 cp_parser_block_declaration (parser, /*statement_p=*/true);
12480 /* Free any declarators allocated. */
12481 obstack_free (&declarator_obstack, p);
12484 /* Some dependent statements (like `if (cond) statement'), are
12485 implicitly in their own scope. In other words, if the statement is
12486 a single statement (as opposed to a compound-statement), it is
12487 none-the-less treated as if it were enclosed in braces. Any
12488 declarations appearing in the dependent statement are out of scope
12489 after control passes that point. This function parses a statement,
12490 but ensures that is in its own scope, even if it is not a
12491 compound-statement.
12493 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12494 is a (possibly labeled) if statement which is not enclosed in
12495 braces and has an else clause. This is used to implement
12496 -Wparentheses.
12498 CHAIN is a vector of if-else-if conditions. This is used to implement
12499 -Wduplicated-cond.
12501 Returns the new statement. */
12503 static tree
12504 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12505 const token_indent_info &guard_tinfo,
12506 vec<tree> *chain)
12508 tree statement;
12509 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12510 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12511 token_indent_info body_tinfo
12512 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12514 if (if_p != NULL)
12515 *if_p = false;
12517 /* Mark if () ; with a special NOP_EXPR. */
12518 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12520 cp_lexer_consume_token (parser->lexer);
12521 statement = add_stmt (build_empty_stmt (body_loc));
12523 if (guard_tinfo.keyword == RID_IF
12524 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12525 warning_at (body_loc, OPT_Wempty_body,
12526 "suggest braces around empty body in an %<if%> statement");
12527 else if (guard_tinfo.keyword == RID_ELSE)
12528 warning_at (body_loc, OPT_Wempty_body,
12529 "suggest braces around empty body in an %<else%> statement");
12531 /* if a compound is opened, we simply parse the statement directly. */
12532 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12533 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12534 /* If the token is not a `{', then we must take special action. */
12535 else
12537 /* Create a compound-statement. */
12538 statement = begin_compound_stmt (0);
12539 /* Parse the dependent-statement. */
12540 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12541 &body_loc_after_labels);
12542 /* Finish the dummy compound-statement. */
12543 finish_compound_stmt (statement);
12546 token_indent_info next_tinfo
12547 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12548 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12550 if (body_loc_after_labels != UNKNOWN_LOCATION
12551 && next_tinfo.type != CPP_SEMICOLON)
12552 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12553 guard_tinfo.location, guard_tinfo.keyword);
12555 /* Return the statement. */
12556 return statement;
12559 /* For some dependent statements (like `while (cond) statement'), we
12560 have already created a scope. Therefore, even if the dependent
12561 statement is a compound-statement, we do not want to create another
12562 scope. */
12564 static void
12565 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12566 const token_indent_info &guard_tinfo)
12568 /* If the token is a `{', then we must take special action. */
12569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12571 token_indent_info body_tinfo
12572 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12573 location_t loc_after_labels = UNKNOWN_LOCATION;
12575 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12576 &loc_after_labels);
12577 token_indent_info next_tinfo
12578 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12579 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12581 if (loc_after_labels != UNKNOWN_LOCATION
12582 && next_tinfo.type != CPP_SEMICOLON)
12583 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12584 guard_tinfo.location,
12585 guard_tinfo.keyword);
12587 else
12589 /* Avoid calling cp_parser_compound_statement, so that we
12590 don't create a new scope. Do everything else by hand. */
12591 matching_braces braces;
12592 braces.require_open (parser);
12593 /* If the next keyword is `__label__' we have a label declaration. */
12594 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12595 cp_parser_label_declaration (parser);
12596 /* Parse an (optional) statement-seq. */
12597 cp_parser_statement_seq_opt (parser, NULL_TREE);
12598 braces.require_close (parser);
12602 /* Declarations [gram.dcl.dcl] */
12604 /* Parse an optional declaration-sequence.
12606 declaration-seq:
12607 declaration
12608 declaration-seq declaration */
12610 static void
12611 cp_parser_declaration_seq_opt (cp_parser* parser)
12613 while (true)
12615 cp_token *token;
12617 token = cp_lexer_peek_token (parser->lexer);
12619 if (token->type == CPP_CLOSE_BRACE
12620 || token->type == CPP_EOF
12621 || token->type == CPP_PRAGMA_EOL)
12622 break;
12624 if (token->type == CPP_SEMICOLON)
12626 /* A declaration consisting of a single semicolon is
12627 invalid. Allow it unless we're being pedantic. */
12628 cp_lexer_consume_token (parser->lexer);
12629 if (!in_system_header_at (input_location))
12630 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12631 continue;
12634 /* If we're entering or exiting a region that's implicitly
12635 extern "C", modify the lang context appropriately. */
12636 if (!parser->implicit_extern_c && token->implicit_extern_c)
12638 push_lang_context (lang_name_c);
12639 parser->implicit_extern_c = true;
12641 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12643 pop_lang_context ();
12644 parser->implicit_extern_c = false;
12647 if (token->type == CPP_PRAGMA)
12649 /* A top-level declaration can consist solely of a #pragma.
12650 A nested declaration cannot, so this is done here and not
12651 in cp_parser_declaration. (A #pragma at block scope is
12652 handled in cp_parser_statement.) */
12653 cp_parser_pragma (parser, pragma_external, NULL);
12654 continue;
12657 /* Parse the declaration itself. */
12658 cp_parser_declaration (parser);
12662 /* Parse a declaration.
12664 declaration:
12665 block-declaration
12666 function-definition
12667 template-declaration
12668 explicit-instantiation
12669 explicit-specialization
12670 linkage-specification
12671 namespace-definition
12673 C++17:
12674 deduction-guide
12676 GNU extension:
12678 declaration:
12679 __extension__ declaration */
12681 static void
12682 cp_parser_declaration (cp_parser* parser)
12684 cp_token token1;
12685 cp_token token2;
12686 int saved_pedantic;
12687 void *p;
12688 tree attributes = NULL_TREE;
12690 /* Check for the `__extension__' keyword. */
12691 if (cp_parser_extension_opt (parser, &saved_pedantic))
12693 /* Parse the qualified declaration. */
12694 cp_parser_declaration (parser);
12695 /* Restore the PEDANTIC flag. */
12696 pedantic = saved_pedantic;
12698 return;
12701 /* Try to figure out what kind of declaration is present. */
12702 token1 = *cp_lexer_peek_token (parser->lexer);
12704 if (token1.type != CPP_EOF)
12705 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12706 else
12708 token2.type = CPP_EOF;
12709 token2.keyword = RID_MAX;
12712 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12713 p = obstack_alloc (&declarator_obstack, 0);
12715 /* If the next token is `extern' and the following token is a string
12716 literal, then we have a linkage specification. */
12717 if (token1.keyword == RID_EXTERN
12718 && cp_parser_is_pure_string_literal (&token2))
12719 cp_parser_linkage_specification (parser);
12720 /* If the next token is `template', then we have either a template
12721 declaration, an explicit instantiation, or an explicit
12722 specialization. */
12723 else if (token1.keyword == RID_TEMPLATE)
12725 /* `template <>' indicates a template specialization. */
12726 if (token2.type == CPP_LESS
12727 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12728 cp_parser_explicit_specialization (parser);
12729 /* `template <' indicates a template declaration. */
12730 else if (token2.type == CPP_LESS)
12731 cp_parser_template_declaration (parser, /*member_p=*/false);
12732 /* Anything else must be an explicit instantiation. */
12733 else
12734 cp_parser_explicit_instantiation (parser);
12736 /* If the next token is `export', then we have a template
12737 declaration. */
12738 else if (token1.keyword == RID_EXPORT)
12739 cp_parser_template_declaration (parser, /*member_p=*/false);
12740 /* If the next token is `extern', 'static' or 'inline' and the one
12741 after that is `template', we have a GNU extended explicit
12742 instantiation directive. */
12743 else if (cp_parser_allow_gnu_extensions_p (parser)
12744 && (token1.keyword == RID_EXTERN
12745 || token1.keyword == RID_STATIC
12746 || token1.keyword == RID_INLINE)
12747 && token2.keyword == RID_TEMPLATE)
12748 cp_parser_explicit_instantiation (parser);
12749 /* If the next token is `namespace', check for a named or unnamed
12750 namespace definition. */
12751 else if (token1.keyword == RID_NAMESPACE
12752 && (/* A named namespace definition. */
12753 (token2.type == CPP_NAME
12754 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12755 != CPP_EQ))
12756 || (token2.type == CPP_OPEN_SQUARE
12757 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12758 == CPP_OPEN_SQUARE)
12759 /* An unnamed namespace definition. */
12760 || token2.type == CPP_OPEN_BRACE
12761 || token2.keyword == RID_ATTRIBUTE))
12762 cp_parser_namespace_definition (parser);
12763 /* An inline (associated) namespace definition. */
12764 else if (token1.keyword == RID_INLINE
12765 && token2.keyword == RID_NAMESPACE)
12766 cp_parser_namespace_definition (parser);
12767 /* Objective-C++ declaration/definition. */
12768 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12769 cp_parser_objc_declaration (parser, NULL_TREE);
12770 else if (c_dialect_objc ()
12771 && token1.keyword == RID_ATTRIBUTE
12772 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12773 cp_parser_objc_declaration (parser, attributes);
12774 /* At this point we may have a template declared by a concept
12775 introduction. */
12776 else if (flag_concepts
12777 && cp_parser_template_declaration_after_export (parser,
12778 /*member_p=*/false))
12779 /* We did. */;
12780 else
12781 /* Try to parse a block-declaration, or a function-definition. */
12782 cp_parser_block_declaration (parser, /*statement_p=*/false);
12784 /* Free any declarators allocated. */
12785 obstack_free (&declarator_obstack, p);
12788 /* Parse a block-declaration.
12790 block-declaration:
12791 simple-declaration
12792 asm-definition
12793 namespace-alias-definition
12794 using-declaration
12795 using-directive
12797 GNU Extension:
12799 block-declaration:
12800 __extension__ block-declaration
12802 C++0x Extension:
12804 block-declaration:
12805 static_assert-declaration
12807 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12808 part of a declaration-statement. */
12810 static void
12811 cp_parser_block_declaration (cp_parser *parser,
12812 bool statement_p)
12814 cp_token *token1;
12815 int saved_pedantic;
12817 /* Check for the `__extension__' keyword. */
12818 if (cp_parser_extension_opt (parser, &saved_pedantic))
12820 /* Parse the qualified declaration. */
12821 cp_parser_block_declaration (parser, statement_p);
12822 /* Restore the PEDANTIC flag. */
12823 pedantic = saved_pedantic;
12825 return;
12828 /* Peek at the next token to figure out which kind of declaration is
12829 present. */
12830 token1 = cp_lexer_peek_token (parser->lexer);
12832 /* If the next keyword is `asm', we have an asm-definition. */
12833 if (token1->keyword == RID_ASM)
12835 if (statement_p)
12836 cp_parser_commit_to_tentative_parse (parser);
12837 cp_parser_asm_definition (parser);
12839 /* If the next keyword is `namespace', we have a
12840 namespace-alias-definition. */
12841 else if (token1->keyword == RID_NAMESPACE)
12842 cp_parser_namespace_alias_definition (parser);
12843 /* If the next keyword is `using', we have a
12844 using-declaration, a using-directive, or an alias-declaration. */
12845 else if (token1->keyword == RID_USING)
12847 cp_token *token2;
12849 if (statement_p)
12850 cp_parser_commit_to_tentative_parse (parser);
12851 /* If the token after `using' is `namespace', then we have a
12852 using-directive. */
12853 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12854 if (token2->keyword == RID_NAMESPACE)
12855 cp_parser_using_directive (parser);
12856 /* If the second token after 'using' is '=', then we have an
12857 alias-declaration. */
12858 else if (cxx_dialect >= cxx11
12859 && token2->type == CPP_NAME
12860 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12861 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12862 cp_parser_alias_declaration (parser);
12863 /* Otherwise, it's a using-declaration. */
12864 else
12865 cp_parser_using_declaration (parser,
12866 /*access_declaration_p=*/false);
12868 /* If the next keyword is `__label__' we have a misplaced label
12869 declaration. */
12870 else if (token1->keyword == RID_LABEL)
12872 cp_lexer_consume_token (parser->lexer);
12873 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12874 cp_parser_skip_to_end_of_statement (parser);
12875 /* If the next token is now a `;', consume it. */
12876 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12877 cp_lexer_consume_token (parser->lexer);
12879 /* If the next token is `static_assert' we have a static assertion. */
12880 else if (token1->keyword == RID_STATIC_ASSERT)
12881 cp_parser_static_assert (parser, /*member_p=*/false);
12882 /* Anything else must be a simple-declaration. */
12883 else
12884 cp_parser_simple_declaration (parser, !statement_p,
12885 /*maybe_range_for_decl*/NULL);
12888 /* Parse a simple-declaration.
12890 simple-declaration:
12891 decl-specifier-seq [opt] init-declarator-list [opt] ;
12892 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12893 brace-or-equal-initializer ;
12895 init-declarator-list:
12896 init-declarator
12897 init-declarator-list , init-declarator
12899 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12900 function-definition as a simple-declaration.
12902 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12903 parsed declaration if it is an uninitialized single declarator not followed
12904 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12905 if present, will not be consumed. */
12907 static void
12908 cp_parser_simple_declaration (cp_parser* parser,
12909 bool function_definition_allowed_p,
12910 tree *maybe_range_for_decl)
12912 cp_decl_specifier_seq decl_specifiers;
12913 int declares_class_or_enum;
12914 bool saw_declarator;
12915 location_t comma_loc = UNKNOWN_LOCATION;
12916 location_t init_loc = UNKNOWN_LOCATION;
12918 if (maybe_range_for_decl)
12919 *maybe_range_for_decl = NULL_TREE;
12921 /* Defer access checks until we know what is being declared; the
12922 checks for names appearing in the decl-specifier-seq should be
12923 done as if we were in the scope of the thing being declared. */
12924 push_deferring_access_checks (dk_deferred);
12926 /* Parse the decl-specifier-seq. We have to keep track of whether
12927 or not the decl-specifier-seq declares a named class or
12928 enumeration type, since that is the only case in which the
12929 init-declarator-list is allowed to be empty.
12931 [dcl.dcl]
12933 In a simple-declaration, the optional init-declarator-list can be
12934 omitted only when declaring a class or enumeration, that is when
12935 the decl-specifier-seq contains either a class-specifier, an
12936 elaborated-type-specifier, or an enum-specifier. */
12937 cp_parser_decl_specifier_seq (parser,
12938 CP_PARSER_FLAGS_OPTIONAL,
12939 &decl_specifiers,
12940 &declares_class_or_enum);
12941 /* We no longer need to defer access checks. */
12942 stop_deferring_access_checks ();
12944 /* In a block scope, a valid declaration must always have a
12945 decl-specifier-seq. By not trying to parse declarators, we can
12946 resolve the declaration/expression ambiguity more quickly. */
12947 if (!function_definition_allowed_p
12948 && !decl_specifiers.any_specifiers_p)
12950 cp_parser_error (parser, "expected declaration");
12951 goto done;
12954 /* If the next two tokens are both identifiers, the code is
12955 erroneous. The usual cause of this situation is code like:
12957 T t;
12959 where "T" should name a type -- but does not. */
12960 if (!decl_specifiers.any_type_specifiers_p
12961 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12963 /* If parsing tentatively, we should commit; we really are
12964 looking at a declaration. */
12965 cp_parser_commit_to_tentative_parse (parser);
12966 /* Give up. */
12967 goto done;
12970 /* If we have seen at least one decl-specifier, and the next token
12971 is not a parenthesis, then we must be looking at a declaration.
12972 (After "int (" we might be looking at a functional cast.) */
12973 if (decl_specifiers.any_specifiers_p
12974 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12975 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12976 && !cp_parser_error_occurred (parser))
12977 cp_parser_commit_to_tentative_parse (parser);
12979 /* Look for C++17 decomposition declaration. */
12980 for (size_t n = 1; ; n++)
12981 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12982 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12983 continue;
12984 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12985 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12986 && decl_specifiers.any_specifiers_p)
12988 tree decl
12989 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12990 maybe_range_for_decl,
12991 &init_loc);
12993 /* The next token should be either a `,' or a `;'. */
12994 cp_token *token = cp_lexer_peek_token (parser->lexer);
12995 /* If it's a `;', we are done. */
12996 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12997 goto finish;
12998 /* Anything else is an error. */
12999 else
13001 /* If we have already issued an error message we don't need
13002 to issue another one. */
13003 if ((decl != error_mark_node
13004 && DECL_INITIAL (decl) != error_mark_node)
13005 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13006 cp_parser_error (parser, "expected %<,%> or %<;%>");
13007 /* Skip tokens until we reach the end of the statement. */
13008 cp_parser_skip_to_end_of_statement (parser);
13009 /* If the next token is now a `;', consume it. */
13010 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13011 cp_lexer_consume_token (parser->lexer);
13012 goto done;
13015 else
13016 break;
13018 tree last_type;
13019 bool auto_specifier_p;
13020 /* NULL_TREE if both variable and function declaration are allowed,
13021 error_mark_node if function declaration are not allowed and
13022 a FUNCTION_DECL that should be diagnosed if it is followed by
13023 variable declarations. */
13024 tree auto_function_declaration;
13026 last_type = NULL_TREE;
13027 auto_specifier_p
13028 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13029 auto_function_declaration = NULL_TREE;
13031 /* Keep going until we hit the `;' at the end of the simple
13032 declaration. */
13033 saw_declarator = false;
13034 while (cp_lexer_next_token_is_not (parser->lexer,
13035 CPP_SEMICOLON))
13037 cp_token *token;
13038 bool function_definition_p;
13039 tree decl;
13040 tree auto_result = NULL_TREE;
13042 if (saw_declarator)
13044 /* If we are processing next declarator, comma is expected */
13045 token = cp_lexer_peek_token (parser->lexer);
13046 gcc_assert (token->type == CPP_COMMA);
13047 cp_lexer_consume_token (parser->lexer);
13048 if (maybe_range_for_decl)
13050 *maybe_range_for_decl = error_mark_node;
13051 if (comma_loc == UNKNOWN_LOCATION)
13052 comma_loc = token->location;
13055 else
13056 saw_declarator = true;
13058 /* Parse the init-declarator. */
13059 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13060 /*checks=*/NULL,
13061 function_definition_allowed_p,
13062 /*member_p=*/false,
13063 declares_class_or_enum,
13064 &function_definition_p,
13065 maybe_range_for_decl,
13066 &init_loc,
13067 &auto_result);
13068 /* If an error occurred while parsing tentatively, exit quickly.
13069 (That usually happens when in the body of a function; each
13070 statement is treated as a declaration-statement until proven
13071 otherwise.) */
13072 if (cp_parser_error_occurred (parser))
13073 goto done;
13075 if (auto_specifier_p && cxx_dialect >= cxx14)
13077 /* If the init-declarator-list contains more than one
13078 init-declarator, they shall all form declarations of
13079 variables. */
13080 if (auto_function_declaration == NULL_TREE)
13081 auto_function_declaration
13082 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13083 else if (TREE_CODE (decl) == FUNCTION_DECL
13084 || auto_function_declaration != error_mark_node)
13086 error_at (decl_specifiers.locations[ds_type_spec],
13087 "non-variable %qD in declaration with more than one "
13088 "declarator with placeholder type",
13089 TREE_CODE (decl) == FUNCTION_DECL
13090 ? decl : auto_function_declaration);
13091 auto_function_declaration = error_mark_node;
13095 if (auto_result
13096 && (!processing_template_decl || !type_uses_auto (auto_result)))
13098 if (last_type
13099 && last_type != error_mark_node
13100 && !same_type_p (auto_result, last_type))
13102 /* If the list of declarators contains more than one declarator,
13103 the type of each declared variable is determined as described
13104 above. If the type deduced for the template parameter U is not
13105 the same in each deduction, the program is ill-formed. */
13106 error_at (decl_specifiers.locations[ds_type_spec],
13107 "inconsistent deduction for %qT: %qT and then %qT",
13108 decl_specifiers.type, last_type, auto_result);
13109 last_type = error_mark_node;
13111 else
13112 last_type = auto_result;
13115 /* Handle function definitions specially. */
13116 if (function_definition_p)
13118 /* If the next token is a `,', then we are probably
13119 processing something like:
13121 void f() {}, *p;
13123 which is erroneous. */
13124 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13126 cp_token *token = cp_lexer_peek_token (parser->lexer);
13127 error_at (token->location,
13128 "mixing"
13129 " declarations and function-definitions is forbidden");
13131 /* Otherwise, we're done with the list of declarators. */
13132 else
13134 pop_deferring_access_checks ();
13135 return;
13138 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13139 *maybe_range_for_decl = decl;
13140 /* The next token should be either a `,' or a `;'. */
13141 token = cp_lexer_peek_token (parser->lexer);
13142 /* If it's a `,', there are more declarators to come. */
13143 if (token->type == CPP_COMMA)
13144 /* will be consumed next time around */;
13145 /* If it's a `;', we are done. */
13146 else if (token->type == CPP_SEMICOLON)
13147 break;
13148 else if (maybe_range_for_decl)
13150 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13151 permerror (decl_specifiers.locations[ds_type_spec],
13152 "types may not be defined in a for-range-declaration");
13153 break;
13155 /* Anything else is an error. */
13156 else
13158 /* If we have already issued an error message we don't need
13159 to issue another one. */
13160 if ((decl != error_mark_node
13161 && DECL_INITIAL (decl) != error_mark_node)
13162 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13163 cp_parser_error (parser, "expected %<,%> or %<;%>");
13164 /* Skip tokens until we reach the end of the statement. */
13165 cp_parser_skip_to_end_of_statement (parser);
13166 /* If the next token is now a `;', consume it. */
13167 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13168 cp_lexer_consume_token (parser->lexer);
13169 goto done;
13171 /* After the first time around, a function-definition is not
13172 allowed -- even if it was OK at first. For example:
13174 int i, f() {}
13176 is not valid. */
13177 function_definition_allowed_p = false;
13180 /* Issue an error message if no declarators are present, and the
13181 decl-specifier-seq does not itself declare a class or
13182 enumeration: [dcl.dcl]/3. */
13183 if (!saw_declarator)
13185 if (cp_parser_declares_only_class_p (parser))
13187 if (!declares_class_or_enum
13188 && decl_specifiers.type
13189 && OVERLOAD_TYPE_P (decl_specifiers.type))
13190 /* Ensure an error is issued anyway when finish_decltype_type,
13191 called via cp_parser_decl_specifier_seq, returns a class or
13192 an enumeration (c++/51786). */
13193 decl_specifiers.type = NULL_TREE;
13194 shadow_tag (&decl_specifiers);
13196 /* Perform any deferred access checks. */
13197 perform_deferred_access_checks (tf_warning_or_error);
13200 /* Consume the `;'. */
13201 finish:
13202 if (!maybe_range_for_decl)
13203 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13204 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13206 if (init_loc != UNKNOWN_LOCATION)
13207 error_at (init_loc, "initializer in range-based %<for%> loop");
13208 if (comma_loc != UNKNOWN_LOCATION)
13209 error_at (comma_loc,
13210 "multiple declarations in range-based %<for%> loop");
13213 done:
13214 pop_deferring_access_checks ();
13217 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13218 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13219 initializer ; */
13221 static tree
13222 cp_parser_decomposition_declaration (cp_parser *parser,
13223 cp_decl_specifier_seq *decl_specifiers,
13224 tree *maybe_range_for_decl,
13225 location_t *init_loc)
13227 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13228 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13229 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13231 /* Parse the identifier-list. */
13232 auto_vec<cp_expr, 10> v;
13233 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13234 while (true)
13236 cp_expr e = cp_parser_identifier (parser);
13237 if (e.get_value () == error_mark_node)
13238 break;
13239 v.safe_push (e);
13240 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13241 break;
13242 cp_lexer_consume_token (parser->lexer);
13245 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13246 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13248 end_loc = UNKNOWN_LOCATION;
13249 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13250 false);
13251 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13252 cp_lexer_consume_token (parser->lexer);
13253 else
13255 cp_parser_skip_to_end_of_statement (parser);
13256 return error_mark_node;
13260 if (cxx_dialect < cxx17)
13261 pedwarn (loc, 0, "structured bindings only available with "
13262 "-std=c++17 or -std=gnu++17");
13264 tree pushed_scope;
13265 cp_declarator *declarator = make_declarator (cdk_decomp);
13266 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13267 declarator->id_loc = loc;
13268 if (ref_qual != REF_QUAL_NONE)
13269 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13270 ref_qual == REF_QUAL_RVALUE,
13271 NULL_TREE);
13272 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13273 NULL_TREE, decl_specifiers->attributes,
13274 &pushed_scope);
13275 tree orig_decl = decl;
13277 unsigned int i;
13278 cp_expr e;
13279 cp_decl_specifier_seq decl_specs;
13280 clear_decl_specs (&decl_specs);
13281 decl_specs.type = make_auto ();
13282 tree prev = decl;
13283 FOR_EACH_VEC_ELT (v, i, e)
13285 if (i == 0)
13286 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13287 else
13288 declarator->u.id.unqualified_name = e.get_value ();
13289 declarator->id_loc = e.get_location ();
13290 tree elt_pushed_scope;
13291 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13292 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13293 if (decl2 == error_mark_node)
13294 decl = error_mark_node;
13295 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13297 /* Ensure we've diagnosed redeclaration if we aren't creating
13298 a new VAR_DECL. */
13299 gcc_assert (errorcount);
13300 decl = error_mark_node;
13302 else
13303 prev = decl2;
13304 if (elt_pushed_scope)
13305 pop_scope (elt_pushed_scope);
13308 if (v.is_empty ())
13310 error_at (loc, "empty structured binding declaration");
13311 decl = error_mark_node;
13314 if (maybe_range_for_decl == NULL
13315 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13317 bool non_constant_p = false, is_direct_init = false;
13318 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13319 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13320 &non_constant_p);
13321 if (initializer == NULL_TREE
13322 || (TREE_CODE (initializer) == TREE_LIST
13323 && TREE_CHAIN (initializer))
13324 || (is_direct_init
13325 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13326 && CONSTRUCTOR_NELTS (initializer) != 1))
13328 error_at (loc, "invalid initializer for structured binding "
13329 "declaration");
13330 initializer = error_mark_node;
13333 if (decl != error_mark_node)
13335 cp_maybe_mangle_decomp (decl, prev, v.length ());
13336 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13337 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13338 cp_finish_decomp (decl, prev, v.length ());
13341 else if (decl != error_mark_node)
13343 *maybe_range_for_decl = prev;
13344 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13345 the underlying DECL. */
13346 cp_finish_decomp (decl, prev, v.length ());
13349 if (pushed_scope)
13350 pop_scope (pushed_scope);
13352 if (decl == error_mark_node && DECL_P (orig_decl))
13354 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13355 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13358 return decl;
13361 /* Parse a decl-specifier-seq.
13363 decl-specifier-seq:
13364 decl-specifier-seq [opt] decl-specifier
13365 decl-specifier attribute-specifier-seq [opt] (C++11)
13367 decl-specifier:
13368 storage-class-specifier
13369 type-specifier
13370 function-specifier
13371 friend
13372 typedef
13374 GNU Extension:
13376 decl-specifier:
13377 attributes
13379 Concepts Extension:
13381 decl-specifier:
13382 concept
13384 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13386 The parser flags FLAGS is used to control type-specifier parsing.
13388 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13389 flags:
13391 1: one of the decl-specifiers is an elaborated-type-specifier
13392 (i.e., a type declaration)
13393 2: one of the decl-specifiers is an enum-specifier or a
13394 class-specifier (i.e., a type definition)
13398 static void
13399 cp_parser_decl_specifier_seq (cp_parser* parser,
13400 cp_parser_flags flags,
13401 cp_decl_specifier_seq *decl_specs,
13402 int* declares_class_or_enum)
13404 bool constructor_possible_p = !parser->in_declarator_p;
13405 bool found_decl_spec = false;
13406 cp_token *start_token = NULL;
13407 cp_decl_spec ds;
13409 /* Clear DECL_SPECS. */
13410 clear_decl_specs (decl_specs);
13412 /* Assume no class or enumeration type is declared. */
13413 *declares_class_or_enum = 0;
13415 /* Keep reading specifiers until there are no more to read. */
13416 while (true)
13418 bool constructor_p;
13419 cp_token *token;
13420 ds = ds_last;
13422 /* Peek at the next token. */
13423 token = cp_lexer_peek_token (parser->lexer);
13425 /* Save the first token of the decl spec list for error
13426 reporting. */
13427 if (!start_token)
13428 start_token = token;
13429 /* Handle attributes. */
13430 if (cp_next_tokens_can_be_attribute_p (parser))
13432 /* Parse the attributes. */
13433 tree attrs = cp_parser_attributes_opt (parser);
13435 /* In a sequence of declaration specifiers, c++11 attributes
13436 appertain to the type that precede them. In that case
13437 [dcl.spec]/1 says:
13439 The attribute-specifier-seq affects the type only for
13440 the declaration it appears in, not other declarations
13441 involving the same type.
13443 But for now let's force the user to position the
13444 attribute either at the beginning of the declaration or
13445 after the declarator-id, which would clearly mean that it
13446 applies to the declarator. */
13447 if (cxx11_attribute_p (attrs))
13449 if (!found_decl_spec)
13450 /* The c++11 attribute is at the beginning of the
13451 declaration. It appertains to the entity being
13452 declared. */;
13453 else
13455 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13457 /* This is an attribute following a
13458 class-specifier. */
13459 if (decl_specs->type_definition_p)
13460 warn_misplaced_attr_for_class_type (token->location,
13461 decl_specs->type);
13462 attrs = NULL_TREE;
13464 else
13466 decl_specs->std_attributes
13467 = attr_chainon (decl_specs->std_attributes, attrs);
13468 if (decl_specs->locations[ds_std_attribute] == 0)
13469 decl_specs->locations[ds_std_attribute] = token->location;
13471 continue;
13475 decl_specs->attributes
13476 = attr_chainon (decl_specs->attributes, attrs);
13477 if (decl_specs->locations[ds_attribute] == 0)
13478 decl_specs->locations[ds_attribute] = token->location;
13479 continue;
13481 /* Assume we will find a decl-specifier keyword. */
13482 found_decl_spec = true;
13483 /* If the next token is an appropriate keyword, we can simply
13484 add it to the list. */
13485 switch (token->keyword)
13487 /* decl-specifier:
13488 friend
13489 constexpr */
13490 case RID_FRIEND:
13491 if (!at_class_scope_p ())
13493 gcc_rich_location richloc (token->location);
13494 richloc.add_fixit_remove ();
13495 error_at (&richloc, "%<friend%> used outside of class");
13496 cp_lexer_purge_token (parser->lexer);
13498 else
13500 ds = ds_friend;
13501 /* Consume the token. */
13502 cp_lexer_consume_token (parser->lexer);
13504 break;
13506 case RID_CONSTEXPR:
13507 ds = ds_constexpr;
13508 cp_lexer_consume_token (parser->lexer);
13509 break;
13511 case RID_CONCEPT:
13512 ds = ds_concept;
13513 cp_lexer_consume_token (parser->lexer);
13514 break;
13516 /* function-specifier:
13517 inline
13518 virtual
13519 explicit */
13520 case RID_INLINE:
13521 case RID_VIRTUAL:
13522 case RID_EXPLICIT:
13523 cp_parser_function_specifier_opt (parser, decl_specs);
13524 break;
13526 /* decl-specifier:
13527 typedef */
13528 case RID_TYPEDEF:
13529 ds = ds_typedef;
13530 /* Consume the token. */
13531 cp_lexer_consume_token (parser->lexer);
13532 /* A constructor declarator cannot appear in a typedef. */
13533 constructor_possible_p = false;
13534 /* The "typedef" keyword can only occur in a declaration; we
13535 may as well commit at this point. */
13536 cp_parser_commit_to_tentative_parse (parser);
13538 if (decl_specs->storage_class != sc_none)
13539 decl_specs->conflicting_specifiers_p = true;
13540 break;
13542 /* storage-class-specifier:
13543 auto
13544 register
13545 static
13546 extern
13547 mutable
13549 GNU Extension:
13550 thread */
13551 case RID_AUTO:
13552 if (cxx_dialect == cxx98)
13554 /* Consume the token. */
13555 cp_lexer_consume_token (parser->lexer);
13557 /* Complain about `auto' as a storage specifier, if
13558 we're complaining about C++0x compatibility. */
13559 gcc_rich_location richloc (token->location);
13560 richloc.add_fixit_remove ();
13561 warning_at (&richloc, OPT_Wc__11_compat,
13562 "%<auto%> changes meaning in C++11; "
13563 "please remove it");
13565 /* Set the storage class anyway. */
13566 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13567 token);
13569 else
13570 /* C++0x auto type-specifier. */
13571 found_decl_spec = false;
13572 break;
13574 case RID_REGISTER:
13575 case RID_STATIC:
13576 case RID_EXTERN:
13577 case RID_MUTABLE:
13578 /* Consume the token. */
13579 cp_lexer_consume_token (parser->lexer);
13580 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13581 token);
13582 break;
13583 case RID_THREAD:
13584 /* Consume the token. */
13585 ds = ds_thread;
13586 cp_lexer_consume_token (parser->lexer);
13587 break;
13589 default:
13590 /* We did not yet find a decl-specifier yet. */
13591 found_decl_spec = false;
13592 break;
13595 if (found_decl_spec
13596 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13597 && token->keyword != RID_CONSTEXPR)
13598 error ("decl-specifier invalid in condition");
13600 if (found_decl_spec
13601 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13602 && token->keyword != RID_MUTABLE
13603 && token->keyword != RID_CONSTEXPR)
13604 error_at (token->location, "%qD invalid in lambda",
13605 ridpointers[token->keyword]);
13607 if (ds != ds_last)
13608 set_and_check_decl_spec_loc (decl_specs, ds, token);
13610 /* Constructors are a special case. The `S' in `S()' is not a
13611 decl-specifier; it is the beginning of the declarator. */
13612 constructor_p
13613 = (!found_decl_spec
13614 && constructor_possible_p
13615 && (cp_parser_constructor_declarator_p
13616 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13618 /* If we don't have a DECL_SPEC yet, then we must be looking at
13619 a type-specifier. */
13620 if (!found_decl_spec && !constructor_p)
13622 int decl_spec_declares_class_or_enum;
13623 bool is_cv_qualifier;
13624 tree type_spec;
13626 type_spec
13627 = cp_parser_type_specifier (parser, flags,
13628 decl_specs,
13629 /*is_declaration=*/true,
13630 &decl_spec_declares_class_or_enum,
13631 &is_cv_qualifier);
13632 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13634 /* If this type-specifier referenced a user-defined type
13635 (a typedef, class-name, etc.), then we can't allow any
13636 more such type-specifiers henceforth.
13638 [dcl.spec]
13640 The longest sequence of decl-specifiers that could
13641 possibly be a type name is taken as the
13642 decl-specifier-seq of a declaration. The sequence shall
13643 be self-consistent as described below.
13645 [dcl.type]
13647 As a general rule, at most one type-specifier is allowed
13648 in the complete decl-specifier-seq of a declaration. The
13649 only exceptions are the following:
13651 -- const or volatile can be combined with any other
13652 type-specifier.
13654 -- signed or unsigned can be combined with char, long,
13655 short, or int.
13657 -- ..
13659 Example:
13661 typedef char* Pc;
13662 void g (const int Pc);
13664 Here, Pc is *not* part of the decl-specifier seq; it's
13665 the declarator. Therefore, once we see a type-specifier
13666 (other than a cv-qualifier), we forbid any additional
13667 user-defined types. We *do* still allow things like `int
13668 int' to be considered a decl-specifier-seq, and issue the
13669 error message later. */
13670 if (type_spec && !is_cv_qualifier)
13671 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13672 /* A constructor declarator cannot follow a type-specifier. */
13673 if (type_spec)
13675 constructor_possible_p = false;
13676 found_decl_spec = true;
13677 if (!is_cv_qualifier)
13678 decl_specs->any_type_specifiers_p = true;
13682 /* If we still do not have a DECL_SPEC, then there are no more
13683 decl-specifiers. */
13684 if (!found_decl_spec)
13685 break;
13687 decl_specs->any_specifiers_p = true;
13688 /* After we see one decl-specifier, further decl-specifiers are
13689 always optional. */
13690 flags |= CP_PARSER_FLAGS_OPTIONAL;
13693 /* Don't allow a friend specifier with a class definition. */
13694 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13695 && (*declares_class_or_enum & 2))
13696 error_at (decl_specs->locations[ds_friend],
13697 "class definition may not be declared a friend");
13700 /* Parse an (optional) storage-class-specifier.
13702 storage-class-specifier:
13703 auto
13704 register
13705 static
13706 extern
13707 mutable
13709 GNU Extension:
13711 storage-class-specifier:
13712 thread
13714 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13716 static tree
13717 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13719 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13721 case RID_AUTO:
13722 if (cxx_dialect != cxx98)
13723 return NULL_TREE;
13724 /* Fall through for C++98. */
13725 gcc_fallthrough ();
13727 case RID_REGISTER:
13728 case RID_STATIC:
13729 case RID_EXTERN:
13730 case RID_MUTABLE:
13731 case RID_THREAD:
13732 /* Consume the token. */
13733 return cp_lexer_consume_token (parser->lexer)->u.value;
13735 default:
13736 return NULL_TREE;
13740 /* Parse an (optional) function-specifier.
13742 function-specifier:
13743 inline
13744 virtual
13745 explicit
13747 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13748 Updates DECL_SPECS, if it is non-NULL. */
13750 static tree
13751 cp_parser_function_specifier_opt (cp_parser* parser,
13752 cp_decl_specifier_seq *decl_specs)
13754 cp_token *token = cp_lexer_peek_token (parser->lexer);
13755 switch (token->keyword)
13757 case RID_INLINE:
13758 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13759 break;
13761 case RID_VIRTUAL:
13762 /* 14.5.2.3 [temp.mem]
13764 A member function template shall not be virtual. */
13765 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13766 && current_class_type)
13767 error_at (token->location, "templates may not be %<virtual%>");
13768 else
13769 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13770 break;
13772 case RID_EXPLICIT:
13773 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13774 break;
13776 default:
13777 return NULL_TREE;
13780 /* Consume the token. */
13781 return cp_lexer_consume_token (parser->lexer)->u.value;
13784 /* Parse a linkage-specification.
13786 linkage-specification:
13787 extern string-literal { declaration-seq [opt] }
13788 extern string-literal declaration */
13790 static void
13791 cp_parser_linkage_specification (cp_parser* parser)
13793 tree linkage;
13795 /* Look for the `extern' keyword. */
13796 cp_token *extern_token
13797 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13799 /* Look for the string-literal. */
13800 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13801 linkage = cp_parser_string_literal (parser, false, false);
13803 /* Transform the literal into an identifier. If the literal is a
13804 wide-character string, or contains embedded NULs, then we can't
13805 handle it as the user wants. */
13806 if (strlen (TREE_STRING_POINTER (linkage))
13807 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13809 cp_parser_error (parser, "invalid linkage-specification");
13810 /* Assume C++ linkage. */
13811 linkage = lang_name_cplusplus;
13813 else
13814 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13816 /* We're now using the new linkage. */
13817 push_lang_context (linkage);
13819 /* Preserve the location of the the innermost linkage specification,
13820 tracking the locations of nested specifications via a local. */
13821 location_t saved_location
13822 = parser->innermost_linkage_specification_location;
13823 /* Construct a location ranging from the start of the "extern" to
13824 the end of the string-literal, with the caret at the start, e.g.:
13825 extern "C" {
13826 ^~~~~~~~~~
13828 parser->innermost_linkage_specification_location
13829 = make_location (extern_token->location,
13830 extern_token->location,
13831 get_finish (string_token->location));
13833 /* If the next token is a `{', then we're using the first
13834 production. */
13835 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13837 cp_ensure_no_omp_declare_simd (parser);
13838 cp_ensure_no_oacc_routine (parser);
13840 /* Consume the `{' token. */
13841 matching_braces braces;
13842 braces.consume_open (parser)->location;
13843 /* Parse the declarations. */
13844 cp_parser_declaration_seq_opt (parser);
13845 /* Look for the closing `}'. */
13846 braces.require_close (parser);
13848 /* Otherwise, there's just one declaration. */
13849 else
13851 bool saved_in_unbraced_linkage_specification_p;
13853 saved_in_unbraced_linkage_specification_p
13854 = parser->in_unbraced_linkage_specification_p;
13855 parser->in_unbraced_linkage_specification_p = true;
13856 cp_parser_declaration (parser);
13857 parser->in_unbraced_linkage_specification_p
13858 = saved_in_unbraced_linkage_specification_p;
13861 /* We're done with the linkage-specification. */
13862 pop_lang_context ();
13864 /* Restore location of parent linkage specification, if any. */
13865 parser->innermost_linkage_specification_location = saved_location;
13868 /* Parse a static_assert-declaration.
13870 static_assert-declaration:
13871 static_assert ( constant-expression , string-literal ) ;
13872 static_assert ( constant-expression ) ; (C++17)
13874 If MEMBER_P, this static_assert is a class member. */
13876 static void
13877 cp_parser_static_assert(cp_parser *parser, bool member_p)
13879 cp_expr condition;
13880 location_t token_loc;
13881 tree message;
13882 bool dummy;
13884 /* Peek at the `static_assert' token so we can keep track of exactly
13885 where the static assertion started. */
13886 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13888 /* Look for the `static_assert' keyword. */
13889 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13890 RT_STATIC_ASSERT))
13891 return;
13893 /* We know we are in a static assertion; commit to any tentative
13894 parse. */
13895 if (cp_parser_parsing_tentatively (parser))
13896 cp_parser_commit_to_tentative_parse (parser);
13898 /* Parse the `(' starting the static assertion condition. */
13899 matching_parens parens;
13900 parens.require_open (parser);
13902 /* Parse the constant-expression. Allow a non-constant expression
13903 here in order to give better diagnostics in finish_static_assert. */
13904 condition =
13905 cp_parser_constant_expression (parser,
13906 /*allow_non_constant_p=*/true,
13907 /*non_constant_p=*/&dummy);
13909 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13911 if (cxx_dialect < cxx17)
13912 pedwarn (input_location, OPT_Wpedantic,
13913 "static_assert without a message "
13914 "only available with -std=c++17 or -std=gnu++17");
13915 /* Eat the ')' */
13916 cp_lexer_consume_token (parser->lexer);
13917 message = build_string (1, "");
13918 TREE_TYPE (message) = char_array_type_node;
13919 fix_string_type (message);
13921 else
13923 /* Parse the separating `,'. */
13924 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13926 /* Parse the string-literal message. */
13927 message = cp_parser_string_literal (parser,
13928 /*translate=*/false,
13929 /*wide_ok=*/true);
13931 /* A `)' completes the static assertion. */
13932 if (!parens.require_close (parser))
13933 cp_parser_skip_to_closing_parenthesis (parser,
13934 /*recovering=*/true,
13935 /*or_comma=*/false,
13936 /*consume_paren=*/true);
13939 /* A semicolon terminates the declaration. */
13940 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13942 /* Get the location for the static assertion. Use that of the
13943 condition if available, otherwise, use that of the "static_assert"
13944 token. */
13945 location_t assert_loc = condition.get_location ();
13946 if (assert_loc == UNKNOWN_LOCATION)
13947 assert_loc = token_loc;
13949 /* Complete the static assertion, which may mean either processing
13950 the static assert now or saving it for template instantiation. */
13951 finish_static_assert (condition, message, assert_loc, member_p);
13954 /* Parse the expression in decltype ( expression ). */
13956 static tree
13957 cp_parser_decltype_expr (cp_parser *parser,
13958 bool &id_expression_or_member_access_p)
13960 cp_token *id_expr_start_token;
13961 tree expr;
13963 /* Since we're going to preserve any side-effects from this parse, set up a
13964 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13965 in the expression. */
13966 tentative_firewall firewall (parser);
13968 /* First, try parsing an id-expression. */
13969 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13970 cp_parser_parse_tentatively (parser);
13971 expr = cp_parser_id_expression (parser,
13972 /*template_keyword_p=*/false,
13973 /*check_dependency_p=*/true,
13974 /*template_p=*/NULL,
13975 /*declarator_p=*/false,
13976 /*optional_p=*/false);
13978 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13980 bool non_integral_constant_expression_p = false;
13981 tree id_expression = expr;
13982 cp_id_kind idk;
13983 const char *error_msg;
13985 if (identifier_p (expr))
13986 /* Lookup the name we got back from the id-expression. */
13987 expr = cp_parser_lookup_name_simple (parser, expr,
13988 id_expr_start_token->location);
13990 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13991 /* A template without args is not a complete id-expression. */
13992 expr = error_mark_node;
13994 if (expr
13995 && expr != error_mark_node
13996 && TREE_CODE (expr) != TYPE_DECL
13997 && (TREE_CODE (expr) != BIT_NOT_EXPR
13998 || !TYPE_P (TREE_OPERAND (expr, 0)))
13999 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14001 /* Complete lookup of the id-expression. */
14002 expr = (finish_id_expression
14003 (id_expression, expr, parser->scope, &idk,
14004 /*integral_constant_expression_p=*/false,
14005 /*allow_non_integral_constant_expression_p=*/true,
14006 &non_integral_constant_expression_p,
14007 /*template_p=*/false,
14008 /*done=*/true,
14009 /*address_p=*/false,
14010 /*template_arg_p=*/false,
14011 &error_msg,
14012 id_expr_start_token->location));
14014 if (expr == error_mark_node)
14015 /* We found an id-expression, but it was something that we
14016 should not have found. This is an error, not something
14017 we can recover from, so note that we found an
14018 id-expression and we'll recover as gracefully as
14019 possible. */
14020 id_expression_or_member_access_p = true;
14023 if (expr
14024 && expr != error_mark_node
14025 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14026 /* We have an id-expression. */
14027 id_expression_or_member_access_p = true;
14030 if (!id_expression_or_member_access_p)
14032 /* Abort the id-expression parse. */
14033 cp_parser_abort_tentative_parse (parser);
14035 /* Parsing tentatively, again. */
14036 cp_parser_parse_tentatively (parser);
14038 /* Parse a class member access. */
14039 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14040 /*cast_p=*/false, /*decltype*/true,
14041 /*member_access_only_p=*/true, NULL);
14043 if (expr
14044 && expr != error_mark_node
14045 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14046 /* We have an id-expression. */
14047 id_expression_or_member_access_p = true;
14050 if (id_expression_or_member_access_p)
14051 /* We have parsed the complete id-expression or member access. */
14052 cp_parser_parse_definitely (parser);
14053 else
14055 /* Abort our attempt to parse an id-expression or member access
14056 expression. */
14057 cp_parser_abort_tentative_parse (parser);
14059 /* Commit to the tentative_firewall so we get syntax errors. */
14060 cp_parser_commit_to_tentative_parse (parser);
14062 /* Parse a full expression. */
14063 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14064 /*decltype_p=*/true);
14067 return expr;
14070 /* Parse a `decltype' type. Returns the type.
14072 simple-type-specifier:
14073 decltype ( expression )
14074 C++14 proposal:
14075 decltype ( auto ) */
14077 static tree
14078 cp_parser_decltype (cp_parser *parser)
14080 bool id_expression_or_member_access_p = false;
14081 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14083 if (start_token->type == CPP_DECLTYPE)
14085 /* Already parsed. */
14086 cp_lexer_consume_token (parser->lexer);
14087 return saved_checks_value (start_token->u.tree_check_value);
14090 /* Look for the `decltype' token. */
14091 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14092 return error_mark_node;
14094 /* Parse the opening `('. */
14095 matching_parens parens;
14096 if (!parens.require_open (parser))
14097 return error_mark_node;
14099 push_deferring_access_checks (dk_deferred);
14101 tree expr = NULL_TREE;
14103 if (cxx_dialect >= cxx14
14104 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14105 /* decltype (auto) */
14106 cp_lexer_consume_token (parser->lexer);
14107 else
14109 /* decltype (expression) */
14111 /* Types cannot be defined in a `decltype' expression. Save away the
14112 old message and set the new one. */
14113 const char *saved_message = parser->type_definition_forbidden_message;
14114 parser->type_definition_forbidden_message
14115 = G_("types may not be defined in %<decltype%> expressions");
14117 /* The restrictions on constant-expressions do not apply inside
14118 decltype expressions. */
14119 bool saved_integral_constant_expression_p
14120 = parser->integral_constant_expression_p;
14121 bool saved_non_integral_constant_expression_p
14122 = parser->non_integral_constant_expression_p;
14123 parser->integral_constant_expression_p = false;
14125 /* Within a parenthesized expression, a `>' token is always
14126 the greater-than operator. */
14127 bool saved_greater_than_is_operator_p
14128 = parser->greater_than_is_operator_p;
14129 parser->greater_than_is_operator_p = true;
14131 /* Do not actually evaluate the expression. */
14132 ++cp_unevaluated_operand;
14134 /* Do not warn about problems with the expression. */
14135 ++c_inhibit_evaluation_warnings;
14137 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14139 /* Go back to evaluating expressions. */
14140 --cp_unevaluated_operand;
14141 --c_inhibit_evaluation_warnings;
14143 /* The `>' token might be the end of a template-id or
14144 template-parameter-list now. */
14145 parser->greater_than_is_operator_p
14146 = saved_greater_than_is_operator_p;
14148 /* Restore the old message and the integral constant expression
14149 flags. */
14150 parser->type_definition_forbidden_message = saved_message;
14151 parser->integral_constant_expression_p
14152 = saved_integral_constant_expression_p;
14153 parser->non_integral_constant_expression_p
14154 = saved_non_integral_constant_expression_p;
14157 /* Parse to the closing `)'. */
14158 if (!parens.require_close (parser))
14160 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14161 /*consume_paren=*/true);
14162 pop_deferring_access_checks ();
14163 return error_mark_node;
14166 if (!expr)
14168 /* Build auto. */
14169 expr = make_decltype_auto ();
14170 AUTO_IS_DECLTYPE (expr) = true;
14172 else
14173 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14174 tf_warning_or_error);
14176 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14177 it again. */
14178 start_token->type = CPP_DECLTYPE;
14179 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14180 start_token->u.tree_check_value->value = expr;
14181 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14182 start_token->keyword = RID_MAX;
14183 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14185 pop_to_parent_deferring_access_checks ();
14187 return expr;
14190 /* Special member functions [gram.special] */
14192 /* Parse a conversion-function-id.
14194 conversion-function-id:
14195 operator conversion-type-id
14197 Returns an IDENTIFIER_NODE representing the operator. */
14199 static tree
14200 cp_parser_conversion_function_id (cp_parser* parser)
14202 tree type;
14203 tree saved_scope;
14204 tree saved_qualifying_scope;
14205 tree saved_object_scope;
14206 tree pushed_scope = NULL_TREE;
14208 /* Look for the `operator' token. */
14209 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14210 return error_mark_node;
14211 /* When we parse the conversion-type-id, the current scope will be
14212 reset. However, we need that information in able to look up the
14213 conversion function later, so we save it here. */
14214 saved_scope = parser->scope;
14215 saved_qualifying_scope = parser->qualifying_scope;
14216 saved_object_scope = parser->object_scope;
14217 /* We must enter the scope of the class so that the names of
14218 entities declared within the class are available in the
14219 conversion-type-id. For example, consider:
14221 struct S {
14222 typedef int I;
14223 operator I();
14226 S::operator I() { ... }
14228 In order to see that `I' is a type-name in the definition, we
14229 must be in the scope of `S'. */
14230 if (saved_scope)
14231 pushed_scope = push_scope (saved_scope);
14232 /* Parse the conversion-type-id. */
14233 type = cp_parser_conversion_type_id (parser);
14234 /* Leave the scope of the class, if any. */
14235 if (pushed_scope)
14236 pop_scope (pushed_scope);
14237 /* Restore the saved scope. */
14238 parser->scope = saved_scope;
14239 parser->qualifying_scope = saved_qualifying_scope;
14240 parser->object_scope = saved_object_scope;
14241 /* If the TYPE is invalid, indicate failure. */
14242 if (type == error_mark_node)
14243 return error_mark_node;
14244 return make_conv_op_name (type);
14247 /* Parse a conversion-type-id:
14249 conversion-type-id:
14250 type-specifier-seq conversion-declarator [opt]
14252 Returns the TYPE specified. */
14254 static tree
14255 cp_parser_conversion_type_id (cp_parser* parser)
14257 tree attributes;
14258 cp_decl_specifier_seq type_specifiers;
14259 cp_declarator *declarator;
14260 tree type_specified;
14261 const char *saved_message;
14263 /* Parse the attributes. */
14264 attributes = cp_parser_attributes_opt (parser);
14266 saved_message = parser->type_definition_forbidden_message;
14267 parser->type_definition_forbidden_message
14268 = G_("types may not be defined in a conversion-type-id");
14270 /* Parse the type-specifiers. */
14271 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14272 /*is_trailing_return=*/false,
14273 &type_specifiers);
14275 parser->type_definition_forbidden_message = saved_message;
14277 /* If that didn't work, stop. */
14278 if (type_specifiers.type == error_mark_node)
14279 return error_mark_node;
14280 /* Parse the conversion-declarator. */
14281 declarator = cp_parser_conversion_declarator_opt (parser);
14283 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14284 /*initialized=*/0, &attributes);
14285 if (attributes)
14286 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14288 /* Don't give this error when parsing tentatively. This happens to
14289 work because we always parse this definitively once. */
14290 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14291 && type_uses_auto (type_specified))
14293 if (cxx_dialect < cxx14)
14295 error ("invalid use of %<auto%> in conversion operator");
14296 return error_mark_node;
14298 else if (template_parm_scope_p ())
14299 warning (0, "use of %<auto%> in member template "
14300 "conversion operator can never be deduced");
14303 return type_specified;
14306 /* Parse an (optional) conversion-declarator.
14308 conversion-declarator:
14309 ptr-operator conversion-declarator [opt]
14313 static cp_declarator *
14314 cp_parser_conversion_declarator_opt (cp_parser* parser)
14316 enum tree_code code;
14317 tree class_type, std_attributes = NULL_TREE;
14318 cp_cv_quals cv_quals;
14320 /* We don't know if there's a ptr-operator next, or not. */
14321 cp_parser_parse_tentatively (parser);
14322 /* Try the ptr-operator. */
14323 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14324 &std_attributes);
14325 /* If it worked, look for more conversion-declarators. */
14326 if (cp_parser_parse_definitely (parser))
14328 cp_declarator *declarator;
14330 /* Parse another optional declarator. */
14331 declarator = cp_parser_conversion_declarator_opt (parser);
14333 declarator = cp_parser_make_indirect_declarator
14334 (code, class_type, cv_quals, declarator, std_attributes);
14336 return declarator;
14339 return NULL;
14342 /* Parse an (optional) ctor-initializer.
14344 ctor-initializer:
14345 : mem-initializer-list */
14347 static void
14348 cp_parser_ctor_initializer_opt (cp_parser* parser)
14350 /* If the next token is not a `:', then there is no
14351 ctor-initializer. */
14352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14354 /* Do default initialization of any bases and members. */
14355 if (DECL_CONSTRUCTOR_P (current_function_decl))
14356 finish_mem_initializers (NULL_TREE);
14357 return;
14360 /* Consume the `:' token. */
14361 cp_lexer_consume_token (parser->lexer);
14362 /* And the mem-initializer-list. */
14363 cp_parser_mem_initializer_list (parser);
14366 /* Parse a mem-initializer-list.
14368 mem-initializer-list:
14369 mem-initializer ... [opt]
14370 mem-initializer ... [opt] , mem-initializer-list */
14372 static void
14373 cp_parser_mem_initializer_list (cp_parser* parser)
14375 tree mem_initializer_list = NULL_TREE;
14376 tree target_ctor = error_mark_node;
14377 cp_token *token = cp_lexer_peek_token (parser->lexer);
14379 /* Let the semantic analysis code know that we are starting the
14380 mem-initializer-list. */
14381 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14382 error_at (token->location,
14383 "only constructors take member initializers");
14385 /* Loop through the list. */
14386 while (true)
14388 tree mem_initializer;
14390 token = cp_lexer_peek_token (parser->lexer);
14391 /* Parse the mem-initializer. */
14392 mem_initializer = cp_parser_mem_initializer (parser);
14393 /* If the next token is a `...', we're expanding member initializers. */
14394 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14396 /* Consume the `...'. */
14397 cp_lexer_consume_token (parser->lexer);
14399 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14400 can be expanded but members cannot. */
14401 if (mem_initializer != error_mark_node
14402 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14404 error_at (token->location,
14405 "cannot expand initializer for member %qD",
14406 TREE_PURPOSE (mem_initializer));
14407 mem_initializer = error_mark_node;
14410 /* Construct the pack expansion type. */
14411 if (mem_initializer != error_mark_node)
14412 mem_initializer = make_pack_expansion (mem_initializer);
14414 if (target_ctor != error_mark_node
14415 && mem_initializer != error_mark_node)
14417 error ("mem-initializer for %qD follows constructor delegation",
14418 TREE_PURPOSE (mem_initializer));
14419 mem_initializer = error_mark_node;
14421 /* Look for a target constructor. */
14422 if (mem_initializer != error_mark_node
14423 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14424 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14426 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14427 if (mem_initializer_list)
14429 error ("constructor delegation follows mem-initializer for %qD",
14430 TREE_PURPOSE (mem_initializer_list));
14431 mem_initializer = error_mark_node;
14433 target_ctor = mem_initializer;
14435 /* Add it to the list, unless it was erroneous. */
14436 if (mem_initializer != error_mark_node)
14438 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14439 mem_initializer_list = mem_initializer;
14441 /* If the next token is not a `,', we're done. */
14442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14443 break;
14444 /* Consume the `,' token. */
14445 cp_lexer_consume_token (parser->lexer);
14448 /* Perform semantic analysis. */
14449 if (DECL_CONSTRUCTOR_P (current_function_decl))
14450 finish_mem_initializers (mem_initializer_list);
14453 /* Parse a mem-initializer.
14455 mem-initializer:
14456 mem-initializer-id ( expression-list [opt] )
14457 mem-initializer-id braced-init-list
14459 GNU extension:
14461 mem-initializer:
14462 ( expression-list [opt] )
14464 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14465 class) or FIELD_DECL (for a non-static data member) to initialize;
14466 the TREE_VALUE is the expression-list. An empty initialization
14467 list is represented by void_list_node. */
14469 static tree
14470 cp_parser_mem_initializer (cp_parser* parser)
14472 tree mem_initializer_id;
14473 tree expression_list;
14474 tree member;
14475 cp_token *token = cp_lexer_peek_token (parser->lexer);
14477 /* Find out what is being initialized. */
14478 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14480 permerror (token->location,
14481 "anachronistic old-style base class initializer");
14482 mem_initializer_id = NULL_TREE;
14484 else
14486 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14487 if (mem_initializer_id == error_mark_node)
14488 return mem_initializer_id;
14490 member = expand_member_init (mem_initializer_id);
14491 if (member && !DECL_P (member))
14492 in_base_initializer = 1;
14494 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14496 bool expr_non_constant_p;
14497 cp_lexer_set_source_position (parser->lexer);
14498 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14499 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14500 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14501 expression_list = build_tree_list (NULL_TREE, expression_list);
14503 else
14505 vec<tree, va_gc> *vec;
14506 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14507 /*cast_p=*/false,
14508 /*allow_expansion_p=*/true,
14509 /*non_constant_p=*/NULL);
14510 if (vec == NULL)
14511 return error_mark_node;
14512 expression_list = build_tree_list_vec (vec);
14513 release_tree_vector (vec);
14516 if (expression_list == error_mark_node)
14517 return error_mark_node;
14518 if (!expression_list)
14519 expression_list = void_type_node;
14521 in_base_initializer = 0;
14523 return member ? build_tree_list (member, expression_list) : error_mark_node;
14526 /* Parse a mem-initializer-id.
14528 mem-initializer-id:
14529 :: [opt] nested-name-specifier [opt] class-name
14530 decltype-specifier (C++11)
14531 identifier
14533 Returns a TYPE indicating the class to be initialized for the first
14534 production (and the second in C++11). Returns an IDENTIFIER_NODE
14535 indicating the data member to be initialized for the last production. */
14537 static tree
14538 cp_parser_mem_initializer_id (cp_parser* parser)
14540 bool global_scope_p;
14541 bool nested_name_specifier_p;
14542 bool template_p = false;
14543 tree id;
14545 cp_token *token = cp_lexer_peek_token (parser->lexer);
14547 /* `typename' is not allowed in this context ([temp.res]). */
14548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14550 error_at (token->location,
14551 "keyword %<typename%> not allowed in this context (a qualified "
14552 "member initializer is implicitly a type)");
14553 cp_lexer_consume_token (parser->lexer);
14555 /* Look for the optional `::' operator. */
14556 global_scope_p
14557 = (cp_parser_global_scope_opt (parser,
14558 /*current_scope_valid_p=*/false)
14559 != NULL_TREE);
14560 /* Look for the optional nested-name-specifier. The simplest way to
14561 implement:
14563 [temp.res]
14565 The keyword `typename' is not permitted in a base-specifier or
14566 mem-initializer; in these contexts a qualified name that
14567 depends on a template-parameter is implicitly assumed to be a
14568 type name.
14570 is to assume that we have seen the `typename' keyword at this
14571 point. */
14572 nested_name_specifier_p
14573 = (cp_parser_nested_name_specifier_opt (parser,
14574 /*typename_keyword_p=*/true,
14575 /*check_dependency_p=*/true,
14576 /*type_p=*/true,
14577 /*is_declaration=*/true)
14578 != NULL_TREE);
14579 if (nested_name_specifier_p)
14580 template_p = cp_parser_optional_template_keyword (parser);
14581 /* If there is a `::' operator or a nested-name-specifier, then we
14582 are definitely looking for a class-name. */
14583 if (global_scope_p || nested_name_specifier_p)
14584 return cp_parser_class_name (parser,
14585 /*typename_keyword_p=*/true,
14586 /*template_keyword_p=*/template_p,
14587 typename_type,
14588 /*check_dependency_p=*/true,
14589 /*class_head_p=*/false,
14590 /*is_declaration=*/true);
14591 /* Otherwise, we could also be looking for an ordinary identifier. */
14592 cp_parser_parse_tentatively (parser);
14593 if (cp_lexer_next_token_is_decltype (parser->lexer))
14594 /* Try a decltype-specifier. */
14595 id = cp_parser_decltype (parser);
14596 else
14597 /* Otherwise, try a class-name. */
14598 id = cp_parser_class_name (parser,
14599 /*typename_keyword_p=*/true,
14600 /*template_keyword_p=*/false,
14601 none_type,
14602 /*check_dependency_p=*/true,
14603 /*class_head_p=*/false,
14604 /*is_declaration=*/true);
14605 /* If we found one, we're done. */
14606 if (cp_parser_parse_definitely (parser))
14607 return id;
14608 /* Otherwise, look for an ordinary identifier. */
14609 return cp_parser_identifier (parser);
14612 /* Overloading [gram.over] */
14614 /* Parse an operator-function-id.
14616 operator-function-id:
14617 operator operator
14619 Returns an IDENTIFIER_NODE for the operator which is a
14620 human-readable spelling of the identifier, e.g., `operator +'. */
14622 static cp_expr
14623 cp_parser_operator_function_id (cp_parser* parser)
14625 /* Look for the `operator' keyword. */
14626 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14627 return error_mark_node;
14628 /* And then the name of the operator itself. */
14629 return cp_parser_operator (parser);
14632 /* Return an identifier node for a user-defined literal operator.
14633 The suffix identifier is chained to the operator name identifier. */
14635 tree
14636 cp_literal_operator_id (const char* name)
14638 tree identifier;
14639 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14640 + strlen (name) + 10);
14641 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14642 identifier = get_identifier (buffer);
14644 return identifier;
14647 /* Parse an operator.
14649 operator:
14650 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14651 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14652 || ++ -- , ->* -> () []
14654 GNU Extensions:
14656 operator:
14657 <? >? <?= >?=
14659 Returns an IDENTIFIER_NODE for the operator which is a
14660 human-readable spelling of the identifier, e.g., `operator +'. */
14662 static cp_expr
14663 cp_parser_operator (cp_parser* parser)
14665 tree id = NULL_TREE;
14666 cp_token *token;
14667 bool utf8 = false;
14669 /* Peek at the next token. */
14670 token = cp_lexer_peek_token (parser->lexer);
14672 location_t start_loc = token->location;
14674 /* Figure out which operator we have. */
14675 enum tree_code op = ERROR_MARK;
14676 bool assop = false;
14677 bool consumed = false;
14678 switch (token->type)
14680 case CPP_KEYWORD:
14682 /* The keyword should be either `new' or `delete'. */
14683 if (token->keyword == RID_NEW)
14684 op = NEW_EXPR;
14685 else if (token->keyword == RID_DELETE)
14686 op = DELETE_EXPR;
14687 else
14688 break;
14690 /* Consume the `new' or `delete' token. */
14691 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14693 /* Peek at the next token. */
14694 token = cp_lexer_peek_token (parser->lexer);
14695 /* If it's a `[' token then this is the array variant of the
14696 operator. */
14697 if (token->type == CPP_OPEN_SQUARE)
14699 /* Consume the `[' token. */
14700 cp_lexer_consume_token (parser->lexer);
14701 /* Look for the `]' token. */
14702 if (cp_token *close_token
14703 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14704 end_loc = close_token->location;
14705 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14707 start_loc = make_location (start_loc, start_loc, end_loc);
14708 consumed = true;
14709 break;
14712 case CPP_PLUS:
14713 op = PLUS_EXPR;
14714 break;
14716 case CPP_MINUS:
14717 op = MINUS_EXPR;
14718 break;
14720 case CPP_MULT:
14721 op = MULT_EXPR;
14722 break;
14724 case CPP_DIV:
14725 op = TRUNC_DIV_EXPR;
14726 break;
14728 case CPP_MOD:
14729 op = TRUNC_MOD_EXPR;
14730 break;
14732 case CPP_XOR:
14733 op = BIT_XOR_EXPR;
14734 break;
14736 case CPP_AND:
14737 op = BIT_AND_EXPR;
14738 break;
14740 case CPP_OR:
14741 op = BIT_IOR_EXPR;
14742 break;
14744 case CPP_COMPL:
14745 op = BIT_NOT_EXPR;
14746 break;
14748 case CPP_NOT:
14749 op = TRUTH_NOT_EXPR;
14750 break;
14752 case CPP_EQ:
14753 assop = true;
14754 op = NOP_EXPR;
14755 break;
14757 case CPP_LESS:
14758 op = LT_EXPR;
14759 break;
14761 case CPP_GREATER:
14762 op = GT_EXPR;
14763 break;
14765 case CPP_PLUS_EQ:
14766 assop = true;
14767 op = PLUS_EXPR;
14768 break;
14770 case CPP_MINUS_EQ:
14771 assop = true;
14772 op = MINUS_EXPR;
14773 break;
14775 case CPP_MULT_EQ:
14776 assop = true;
14777 op = MULT_EXPR;
14778 break;
14780 case CPP_DIV_EQ:
14781 assop = true;
14782 op = TRUNC_DIV_EXPR;
14783 break;
14785 case CPP_MOD_EQ:
14786 assop = true;
14787 op = TRUNC_MOD_EXPR;
14788 break;
14790 case CPP_XOR_EQ:
14791 assop = true;
14792 op = BIT_XOR_EXPR;
14793 break;
14795 case CPP_AND_EQ:
14796 assop = true;
14797 op = BIT_AND_EXPR;
14798 break;
14800 case CPP_OR_EQ:
14801 assop = true;
14802 op = BIT_IOR_EXPR;
14803 break;
14805 case CPP_LSHIFT:
14806 op = LSHIFT_EXPR;
14807 break;
14809 case CPP_RSHIFT:
14810 op = RSHIFT_EXPR;
14811 break;
14813 case CPP_LSHIFT_EQ:
14814 assop = true;
14815 op = LSHIFT_EXPR;
14816 break;
14818 case CPP_RSHIFT_EQ:
14819 assop = true;
14820 op = RSHIFT_EXPR;
14821 break;
14823 case CPP_EQ_EQ:
14824 op = EQ_EXPR;
14825 break;
14827 case CPP_NOT_EQ:
14828 op = NE_EXPR;
14829 break;
14831 case CPP_LESS_EQ:
14832 op = LE_EXPR;
14833 break;
14835 case CPP_GREATER_EQ:
14836 op = GE_EXPR;
14837 break;
14839 case CPP_AND_AND:
14840 op = TRUTH_ANDIF_EXPR;
14841 break;
14843 case CPP_OR_OR:
14844 op = TRUTH_ORIF_EXPR;
14845 break;
14847 case CPP_PLUS_PLUS:
14848 op = POSTINCREMENT_EXPR;
14849 break;
14851 case CPP_MINUS_MINUS:
14852 op = PREDECREMENT_EXPR;
14853 break;
14855 case CPP_COMMA:
14856 op = COMPOUND_EXPR;
14857 break;
14859 case CPP_DEREF_STAR:
14860 op = MEMBER_REF;
14861 break;
14863 case CPP_DEREF:
14864 op = COMPONENT_REF;
14865 break;
14867 case CPP_OPEN_PAREN:
14869 /* Consume the `('. */
14870 matching_parens parens;
14871 parens.consume_open (parser);
14872 /* Look for the matching `)'. */
14873 parens.require_close (parser);
14874 op = CALL_EXPR;
14875 consumed = true;
14876 break;
14879 case CPP_OPEN_SQUARE:
14880 /* Consume the `['. */
14881 cp_lexer_consume_token (parser->lexer);
14882 /* Look for the matching `]'. */
14883 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14884 op = ARRAY_REF;
14885 consumed = true;
14886 break;
14888 case CPP_UTF8STRING:
14889 case CPP_UTF8STRING_USERDEF:
14890 utf8 = true;
14891 /* FALLTHRU */
14892 case CPP_STRING:
14893 case CPP_WSTRING:
14894 case CPP_STRING16:
14895 case CPP_STRING32:
14896 case CPP_STRING_USERDEF:
14897 case CPP_WSTRING_USERDEF:
14898 case CPP_STRING16_USERDEF:
14899 case CPP_STRING32_USERDEF:
14901 tree str, string_tree;
14902 int sz, len;
14904 if (cxx_dialect == cxx98)
14905 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14907 /* Consume the string. */
14908 str = cp_parser_string_literal (parser, /*translate=*/true,
14909 /*wide_ok=*/true, /*lookup_udlit=*/false);
14910 if (str == error_mark_node)
14911 return error_mark_node;
14912 else if (TREE_CODE (str) == USERDEF_LITERAL)
14914 string_tree = USERDEF_LITERAL_VALUE (str);
14915 id = USERDEF_LITERAL_SUFFIX_ID (str);
14917 else
14919 string_tree = str;
14920 /* Look for the suffix identifier. */
14921 token = cp_lexer_peek_token (parser->lexer);
14922 if (token->type == CPP_NAME)
14923 id = cp_parser_identifier (parser);
14924 else if (token->type == CPP_KEYWORD)
14926 error ("unexpected keyword;"
14927 " remove space between quotes and suffix identifier");
14928 return error_mark_node;
14930 else
14932 error ("expected suffix identifier");
14933 return error_mark_node;
14936 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14937 (TREE_TYPE (TREE_TYPE (string_tree))));
14938 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14939 if (len != 0)
14941 error ("expected empty string after %<operator%> keyword");
14942 return error_mark_node;
14944 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14945 != char_type_node)
14947 error ("invalid encoding prefix in literal operator");
14948 return error_mark_node;
14950 if (id != error_mark_node)
14952 const char *name = IDENTIFIER_POINTER (id);
14953 id = cp_literal_operator_id (name);
14955 return id;
14958 default:
14959 /* Anything else is an error. */
14960 break;
14963 /* If we have selected an identifier, we need to consume the
14964 operator token. */
14965 if (op != ERROR_MARK)
14967 id = ovl_op_identifier (assop, op);
14968 if (!consumed)
14969 cp_lexer_consume_token (parser->lexer);
14971 /* Otherwise, no valid operator name was present. */
14972 else
14974 cp_parser_error (parser, "expected operator");
14975 id = error_mark_node;
14978 return cp_expr (id, start_loc);
14981 /* Parse a template-declaration.
14983 template-declaration:
14984 export [opt] template < template-parameter-list > declaration
14986 If MEMBER_P is TRUE, this template-declaration occurs within a
14987 class-specifier.
14989 The grammar rule given by the standard isn't correct. What
14990 is really meant is:
14992 template-declaration:
14993 export [opt] template-parameter-list-seq
14994 decl-specifier-seq [opt] init-declarator [opt] ;
14995 export [opt] template-parameter-list-seq
14996 function-definition
14998 template-parameter-list-seq:
14999 template-parameter-list-seq [opt]
15000 template < template-parameter-list >
15002 Concept Extensions:
15004 template-parameter-list-seq:
15005 template < template-parameter-list > requires-clause [opt]
15007 requires-clause:
15008 requires logical-or-expression */
15010 static void
15011 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15013 /* Check for `export'. */
15014 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15016 /* Consume the `export' token. */
15017 cp_lexer_consume_token (parser->lexer);
15018 /* Warn that we do not support `export'. */
15019 warning (0, "keyword %<export%> not implemented, and will be ignored");
15022 cp_parser_template_declaration_after_export (parser, member_p);
15025 /* Parse a template-parameter-list.
15027 template-parameter-list:
15028 template-parameter
15029 template-parameter-list , template-parameter
15031 Returns a TREE_LIST. Each node represents a template parameter.
15032 The nodes are connected via their TREE_CHAINs. */
15034 static tree
15035 cp_parser_template_parameter_list (cp_parser* parser)
15037 tree parameter_list = NULL_TREE;
15039 begin_template_parm_list ();
15041 /* The loop below parses the template parms. We first need to know
15042 the total number of template parms to be able to compute proper
15043 canonical types of each dependent type. So after the loop, when
15044 we know the total number of template parms,
15045 end_template_parm_list computes the proper canonical types and
15046 fixes up the dependent types accordingly. */
15047 while (true)
15049 tree parameter;
15050 bool is_non_type;
15051 bool is_parameter_pack;
15052 location_t parm_loc;
15054 /* Parse the template-parameter. */
15055 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15056 parameter = cp_parser_template_parameter (parser,
15057 &is_non_type,
15058 &is_parameter_pack);
15059 /* Add it to the list. */
15060 if (parameter != error_mark_node)
15061 parameter_list = process_template_parm (parameter_list,
15062 parm_loc,
15063 parameter,
15064 is_non_type,
15065 is_parameter_pack);
15066 else
15068 tree err_parm = build_tree_list (parameter, parameter);
15069 parameter_list = chainon (parameter_list, err_parm);
15072 /* If the next token is not a `,', we're done. */
15073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15074 break;
15075 /* Otherwise, consume the `,' token. */
15076 cp_lexer_consume_token (parser->lexer);
15079 return end_template_parm_list (parameter_list);
15082 /* Parse a introduction-list.
15084 introduction-list:
15085 introduced-parameter
15086 introduction-list , introduced-parameter
15088 introduced-parameter:
15089 ...[opt] identifier
15091 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15092 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15093 WILDCARD_DECL will also have DECL_NAME set and token location in
15094 DECL_SOURCE_LOCATION. */
15096 static tree
15097 cp_parser_introduction_list (cp_parser *parser)
15099 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15101 while (true)
15103 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15104 if (is_pack)
15105 cp_lexer_consume_token (parser->lexer);
15107 /* Build placeholder. */
15108 tree parm = build_nt (WILDCARD_DECL);
15109 DECL_SOURCE_LOCATION (parm)
15110 = cp_lexer_peek_token (parser->lexer)->location;
15111 DECL_NAME (parm) = cp_parser_identifier (parser);
15112 WILDCARD_PACK_P (parm) = is_pack;
15113 vec_safe_push (introduction_vec, parm);
15115 /* If the next token is not a `,', we're done. */
15116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15117 break;
15118 /* Otherwise, consume the `,' token. */
15119 cp_lexer_consume_token (parser->lexer);
15122 /* Convert the vec into a TREE_VEC. */
15123 tree introduction_list = make_tree_vec (introduction_vec->length ());
15124 unsigned int n;
15125 tree parm;
15126 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15127 TREE_VEC_ELT (introduction_list, n) = parm;
15129 release_tree_vector (introduction_vec);
15130 return introduction_list;
15133 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15134 is an abstract declarator. */
15136 static inline cp_declarator*
15137 get_id_declarator (cp_declarator *declarator)
15139 cp_declarator *d = declarator;
15140 while (d && d->kind != cdk_id)
15141 d = d->declarator;
15142 return d;
15145 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15146 is an abstract declarator. */
15148 static inline tree
15149 get_unqualified_id (cp_declarator *declarator)
15151 declarator = get_id_declarator (declarator);
15152 if (declarator)
15153 return declarator->u.id.unqualified_name;
15154 else
15155 return NULL_TREE;
15158 /* Returns true if DECL represents a constrained-parameter. */
15160 static inline bool
15161 is_constrained_parameter (tree decl)
15163 return (decl
15164 && TREE_CODE (decl) == TYPE_DECL
15165 && CONSTRAINED_PARM_CONCEPT (decl)
15166 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15169 /* Returns true if PARM declares a constrained-parameter. */
15171 static inline bool
15172 is_constrained_parameter (cp_parameter_declarator *parm)
15174 return is_constrained_parameter (parm->decl_specifiers.type);
15177 /* Check that the type parameter is only a declarator-id, and that its
15178 type is not cv-qualified. */
15180 bool
15181 cp_parser_check_constrained_type_parm (cp_parser *parser,
15182 cp_parameter_declarator *parm)
15184 if (!parm->declarator)
15185 return true;
15187 if (parm->declarator->kind != cdk_id)
15189 cp_parser_error (parser, "invalid constrained type parameter");
15190 return false;
15193 /* Don't allow cv-qualified type parameters. */
15194 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15195 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15197 cp_parser_error (parser, "cv-qualified type parameter");
15198 return false;
15201 return true;
15204 /* Finish parsing/processing a template type parameter and checking
15205 various restrictions. */
15207 static inline tree
15208 cp_parser_constrained_type_template_parm (cp_parser *parser,
15209 tree id,
15210 cp_parameter_declarator* parmdecl)
15212 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15213 return finish_template_type_parm (class_type_node, id);
15214 else
15215 return error_mark_node;
15218 static tree
15219 finish_constrained_template_template_parm (tree proto, tree id)
15221 /* FIXME: This should probably be copied, and we may need to adjust
15222 the template parameter depths. */
15223 tree saved_parms = current_template_parms;
15224 begin_template_parm_list ();
15225 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15226 end_template_parm_list ();
15228 tree parm = finish_template_template_parm (class_type_node, id);
15229 current_template_parms = saved_parms;
15231 return parm;
15234 /* Finish parsing/processing a template template parameter by borrowing
15235 the template parameter list from the prototype parameter. */
15237 static tree
15238 cp_parser_constrained_template_template_parm (cp_parser *parser,
15239 tree proto,
15240 tree id,
15241 cp_parameter_declarator *parmdecl)
15243 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15244 return error_mark_node;
15245 return finish_constrained_template_template_parm (proto, id);
15248 /* Create a new non-type template parameter from the given PARM
15249 declarator. */
15251 static tree
15252 constrained_non_type_template_parm (bool *is_non_type,
15253 cp_parameter_declarator *parm)
15255 *is_non_type = true;
15256 cp_declarator *decl = parm->declarator;
15257 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15258 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15259 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15262 /* Build a constrained template parameter based on the PARMDECL
15263 declarator. The type of PARMDECL is the constrained type, which
15264 refers to the prototype template parameter that ultimately
15265 specifies the type of the declared parameter. */
15267 static tree
15268 finish_constrained_parameter (cp_parser *parser,
15269 cp_parameter_declarator *parmdecl,
15270 bool *is_non_type,
15271 bool *is_parameter_pack)
15273 tree decl = parmdecl->decl_specifiers.type;
15274 tree id = get_unqualified_id (parmdecl->declarator);
15275 tree def = parmdecl->default_argument;
15276 tree proto = DECL_INITIAL (decl);
15278 /* A template parameter constrained by a variadic concept shall also
15279 be declared as a template parameter pack. */
15280 bool is_variadic = template_parameter_pack_p (proto);
15281 if (is_variadic && !*is_parameter_pack)
15282 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15284 /* Build the parameter. Return an error if the declarator was invalid. */
15285 tree parm;
15286 if (TREE_CODE (proto) == TYPE_DECL)
15287 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15288 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15289 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15290 parmdecl);
15291 else
15292 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15293 if (parm == error_mark_node)
15294 return error_mark_node;
15296 /* Finish the parameter decl and create a node attaching the
15297 default argument and constraint. */
15298 parm = build_tree_list (def, parm);
15299 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15301 return parm;
15304 /* Returns true if the parsed type actually represents the declaration
15305 of a type template-parameter. */
15307 static inline bool
15308 declares_constrained_type_template_parameter (tree type)
15310 return (is_constrained_parameter (type)
15311 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15315 /* Returns true if the parsed type actually represents the declaration of
15316 a template template-parameter. */
15318 static bool
15319 declares_constrained_template_template_parameter (tree type)
15321 return (is_constrained_parameter (type)
15322 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15325 /* Parse a default argument for a type template-parameter.
15326 Note that diagnostics are handled in cp_parser_template_parameter. */
15328 static tree
15329 cp_parser_default_type_template_argument (cp_parser *parser)
15331 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15333 /* Consume the `=' token. */
15334 cp_lexer_consume_token (parser->lexer);
15336 cp_token *token = cp_lexer_peek_token (parser->lexer);
15338 /* Parse the default-argument. */
15339 push_deferring_access_checks (dk_no_deferred);
15340 tree default_argument = cp_parser_type_id (parser);
15341 pop_deferring_access_checks ();
15343 if (flag_concepts && type_uses_auto (default_argument))
15345 error_at (token->location,
15346 "invalid use of %<auto%> in default template argument");
15347 return error_mark_node;
15350 return default_argument;
15353 /* Parse a default argument for a template template-parameter. */
15355 static tree
15356 cp_parser_default_template_template_argument (cp_parser *parser)
15358 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15360 bool is_template;
15362 /* Consume the `='. */
15363 cp_lexer_consume_token (parser->lexer);
15364 /* Parse the id-expression. */
15365 push_deferring_access_checks (dk_no_deferred);
15366 /* save token before parsing the id-expression, for error
15367 reporting */
15368 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15369 tree default_argument
15370 = cp_parser_id_expression (parser,
15371 /*template_keyword_p=*/false,
15372 /*check_dependency_p=*/true,
15373 /*template_p=*/&is_template,
15374 /*declarator_p=*/false,
15375 /*optional_p=*/false);
15376 if (TREE_CODE (default_argument) == TYPE_DECL)
15377 /* If the id-expression was a template-id that refers to
15378 a template-class, we already have the declaration here,
15379 so no further lookup is needed. */
15381 else
15382 /* Look up the name. */
15383 default_argument
15384 = cp_parser_lookup_name (parser, default_argument,
15385 none_type,
15386 /*is_template=*/is_template,
15387 /*is_namespace=*/false,
15388 /*check_dependency=*/true,
15389 /*ambiguous_decls=*/NULL,
15390 token->location);
15391 /* See if the default argument is valid. */
15392 default_argument = check_template_template_default_arg (default_argument);
15393 pop_deferring_access_checks ();
15394 return default_argument;
15397 /* Parse a template-parameter.
15399 template-parameter:
15400 type-parameter
15401 parameter-declaration
15403 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15404 the parameter. The TREE_PURPOSE is the default value, if any.
15405 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15406 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15407 set to true iff this parameter is a parameter pack. */
15409 static tree
15410 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15411 bool *is_parameter_pack)
15413 cp_token *token;
15414 cp_parameter_declarator *parameter_declarator;
15415 tree parm;
15417 /* Assume it is a type parameter or a template parameter. */
15418 *is_non_type = false;
15419 /* Assume it not a parameter pack. */
15420 *is_parameter_pack = false;
15421 /* Peek at the next token. */
15422 token = cp_lexer_peek_token (parser->lexer);
15423 /* If it is `template', we have a type-parameter. */
15424 if (token->keyword == RID_TEMPLATE)
15425 return cp_parser_type_parameter (parser, is_parameter_pack);
15426 /* If it is `class' or `typename' we do not know yet whether it is a
15427 type parameter or a non-type parameter. Consider:
15429 template <typename T, typename T::X X> ...
15433 template <class C, class D*> ...
15435 Here, the first parameter is a type parameter, and the second is
15436 a non-type parameter. We can tell by looking at the token after
15437 the identifier -- if it is a `,', `=', or `>' then we have a type
15438 parameter. */
15439 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15441 /* Peek at the token after `class' or `typename'. */
15442 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15443 /* If it's an ellipsis, we have a template type parameter
15444 pack. */
15445 if (token->type == CPP_ELLIPSIS)
15446 return cp_parser_type_parameter (parser, is_parameter_pack);
15447 /* If it's an identifier, skip it. */
15448 if (token->type == CPP_NAME)
15449 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15450 /* Now, see if the token looks like the end of a template
15451 parameter. */
15452 if (token->type == CPP_COMMA
15453 || token->type == CPP_EQ
15454 || token->type == CPP_GREATER)
15455 return cp_parser_type_parameter (parser, is_parameter_pack);
15458 /* Otherwise, it is a non-type parameter or a constrained parameter.
15460 [temp.param]
15462 When parsing a default template-argument for a non-type
15463 template-parameter, the first non-nested `>' is taken as the end
15464 of the template parameter-list rather than a greater-than
15465 operator. */
15466 parameter_declarator
15467 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15468 /*parenthesized_p=*/NULL);
15470 if (!parameter_declarator)
15471 return error_mark_node;
15473 /* If the parameter declaration is marked as a parameter pack, set
15474 *IS_PARAMETER_PACK to notify the caller. */
15475 if (parameter_declarator->template_parameter_pack_p)
15476 *is_parameter_pack = true;
15478 if (parameter_declarator->default_argument)
15480 /* Can happen in some cases of erroneous input (c++/34892). */
15481 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15482 /* Consume the `...' for better error recovery. */
15483 cp_lexer_consume_token (parser->lexer);
15486 // The parameter may have been constrained.
15487 if (is_constrained_parameter (parameter_declarator))
15488 return finish_constrained_parameter (parser,
15489 parameter_declarator,
15490 is_non_type,
15491 is_parameter_pack);
15493 // Now we're sure that the parameter is a non-type parameter.
15494 *is_non_type = true;
15496 parm = grokdeclarator (parameter_declarator->declarator,
15497 &parameter_declarator->decl_specifiers,
15498 TPARM, /*initialized=*/0,
15499 /*attrlist=*/NULL);
15500 if (parm == error_mark_node)
15501 return error_mark_node;
15503 return build_tree_list (parameter_declarator->default_argument, parm);
15506 /* Parse a type-parameter.
15508 type-parameter:
15509 class identifier [opt]
15510 class identifier [opt] = type-id
15511 typename identifier [opt]
15512 typename identifier [opt] = type-id
15513 template < template-parameter-list > class identifier [opt]
15514 template < template-parameter-list > class identifier [opt]
15515 = id-expression
15517 GNU Extension (variadic templates):
15519 type-parameter:
15520 class ... identifier [opt]
15521 typename ... identifier [opt]
15523 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15524 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15525 the declaration of the parameter.
15527 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15529 static tree
15530 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15532 cp_token *token;
15533 tree parameter;
15535 /* Look for a keyword to tell us what kind of parameter this is. */
15536 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15537 if (!token)
15538 return error_mark_node;
15540 switch (token->keyword)
15542 case RID_CLASS:
15543 case RID_TYPENAME:
15545 tree identifier;
15546 tree default_argument;
15548 /* If the next token is an ellipsis, we have a template
15549 argument pack. */
15550 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15552 /* Consume the `...' token. */
15553 cp_lexer_consume_token (parser->lexer);
15554 maybe_warn_variadic_templates ();
15556 *is_parameter_pack = true;
15559 /* If the next token is an identifier, then it names the
15560 parameter. */
15561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15562 identifier = cp_parser_identifier (parser);
15563 else
15564 identifier = NULL_TREE;
15566 /* Create the parameter. */
15567 parameter = finish_template_type_parm (class_type_node, identifier);
15569 /* If the next token is an `=', we have a default argument. */
15570 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15572 default_argument
15573 = cp_parser_default_type_template_argument (parser);
15575 /* Template parameter packs cannot have default
15576 arguments. */
15577 if (*is_parameter_pack)
15579 if (identifier)
15580 error_at (token->location,
15581 "template parameter pack %qD cannot have a "
15582 "default argument", identifier);
15583 else
15584 error_at (token->location,
15585 "template parameter packs cannot have "
15586 "default arguments");
15587 default_argument = NULL_TREE;
15589 else if (check_for_bare_parameter_packs (default_argument))
15590 default_argument = error_mark_node;
15592 else
15593 default_argument = NULL_TREE;
15595 /* Create the combined representation of the parameter and the
15596 default argument. */
15597 parameter = build_tree_list (default_argument, parameter);
15599 break;
15601 case RID_TEMPLATE:
15603 tree identifier;
15604 tree default_argument;
15606 /* Look for the `<'. */
15607 cp_parser_require (parser, CPP_LESS, RT_LESS);
15608 /* Parse the template-parameter-list. */
15609 cp_parser_template_parameter_list (parser);
15610 /* Look for the `>'. */
15611 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15613 // If template requirements are present, parse them.
15614 if (flag_concepts)
15616 tree reqs = get_shorthand_constraints (current_template_parms);
15617 if (tree r = cp_parser_requires_clause_opt (parser))
15618 reqs = conjoin_constraints (reqs, normalize_expression (r));
15619 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15622 /* Look for the `class' or 'typename' keywords. */
15623 cp_parser_type_parameter_key (parser);
15624 /* If the next token is an ellipsis, we have a template
15625 argument pack. */
15626 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15628 /* Consume the `...' token. */
15629 cp_lexer_consume_token (parser->lexer);
15630 maybe_warn_variadic_templates ();
15632 *is_parameter_pack = true;
15634 /* If the next token is an `=', then there is a
15635 default-argument. If the next token is a `>', we are at
15636 the end of the parameter-list. If the next token is a `,',
15637 then we are at the end of this parameter. */
15638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15639 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15640 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15642 identifier = cp_parser_identifier (parser);
15643 /* Treat invalid names as if the parameter were nameless. */
15644 if (identifier == error_mark_node)
15645 identifier = NULL_TREE;
15647 else
15648 identifier = NULL_TREE;
15650 /* Create the template parameter. */
15651 parameter = finish_template_template_parm (class_type_node,
15652 identifier);
15654 /* If the next token is an `=', then there is a
15655 default-argument. */
15656 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15658 default_argument
15659 = cp_parser_default_template_template_argument (parser);
15661 /* Template parameter packs cannot have default
15662 arguments. */
15663 if (*is_parameter_pack)
15665 if (identifier)
15666 error_at (token->location,
15667 "template parameter pack %qD cannot "
15668 "have a default argument",
15669 identifier);
15670 else
15671 error_at (token->location, "template parameter packs cannot "
15672 "have default arguments");
15673 default_argument = NULL_TREE;
15676 else
15677 default_argument = NULL_TREE;
15679 /* Create the combined representation of the parameter and the
15680 default argument. */
15681 parameter = build_tree_list (default_argument, parameter);
15683 break;
15685 default:
15686 gcc_unreachable ();
15687 break;
15690 return parameter;
15693 /* Parse a template-id.
15695 template-id:
15696 template-name < template-argument-list [opt] >
15698 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15699 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15700 returned. Otherwise, if the template-name names a function, or set
15701 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15702 names a class, returns a TYPE_DECL for the specialization.
15704 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15705 uninstantiated templates. */
15707 static tree
15708 cp_parser_template_id (cp_parser *parser,
15709 bool template_keyword_p,
15710 bool check_dependency_p,
15711 enum tag_types tag_type,
15712 bool is_declaration)
15714 tree templ;
15715 tree arguments;
15716 tree template_id;
15717 cp_token_position start_of_id = 0;
15718 cp_token *next_token = NULL, *next_token_2 = NULL;
15719 bool is_identifier;
15721 /* If the next token corresponds to a template-id, there is no need
15722 to reparse it. */
15723 cp_token *token = cp_lexer_peek_token (parser->lexer);
15724 if (token->type == CPP_TEMPLATE_ID)
15726 cp_lexer_consume_token (parser->lexer);
15727 return saved_checks_value (token->u.tree_check_value);
15730 /* Avoid performing name lookup if there is no possibility of
15731 finding a template-id. */
15732 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15733 || (token->type == CPP_NAME
15734 && !cp_parser_nth_token_starts_template_argument_list_p
15735 (parser, 2)))
15737 cp_parser_error (parser, "expected template-id");
15738 return error_mark_node;
15741 /* Remember where the template-id starts. */
15742 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15743 start_of_id = cp_lexer_token_position (parser->lexer, false);
15745 push_deferring_access_checks (dk_deferred);
15747 /* Parse the template-name. */
15748 is_identifier = false;
15749 templ = cp_parser_template_name (parser, template_keyword_p,
15750 check_dependency_p,
15751 is_declaration,
15752 tag_type,
15753 &is_identifier);
15754 if (templ == error_mark_node || is_identifier)
15756 pop_deferring_access_checks ();
15757 return templ;
15760 /* Since we're going to preserve any side-effects from this parse, set up a
15761 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15762 in the template arguments. */
15763 tentative_firewall firewall (parser);
15765 /* If we find the sequence `[:' after a template-name, it's probably
15766 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15767 parse correctly the argument list. */
15768 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15769 == CPP_OPEN_SQUARE)
15770 && next_token->flags & DIGRAPH
15771 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15772 == CPP_COLON)
15773 && !(next_token_2->flags & PREV_WHITE))
15775 cp_parser_parse_tentatively (parser);
15776 /* Change `:' into `::'. */
15777 next_token_2->type = CPP_SCOPE;
15778 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15779 CPP_LESS. */
15780 cp_lexer_consume_token (parser->lexer);
15782 /* Parse the arguments. */
15783 arguments = cp_parser_enclosed_template_argument_list (parser);
15784 if (!cp_parser_parse_definitely (parser))
15786 /* If we couldn't parse an argument list, then we revert our changes
15787 and return simply an error. Maybe this is not a template-id
15788 after all. */
15789 next_token_2->type = CPP_COLON;
15790 cp_parser_error (parser, "expected %<<%>");
15791 pop_deferring_access_checks ();
15792 return error_mark_node;
15794 /* Otherwise, emit an error about the invalid digraph, but continue
15795 parsing because we got our argument list. */
15796 if (permerror (next_token->location,
15797 "%<<::%> cannot begin a template-argument list"))
15799 static bool hint = false;
15800 inform (next_token->location,
15801 "%<<:%> is an alternate spelling for %<[%>."
15802 " Insert whitespace between %<<%> and %<::%>");
15803 if (!hint && !flag_permissive)
15805 inform (next_token->location, "(if you use %<-fpermissive%> "
15806 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15807 "accept your code)");
15808 hint = true;
15812 else
15814 /* Look for the `<' that starts the template-argument-list. */
15815 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15817 pop_deferring_access_checks ();
15818 return error_mark_node;
15820 /* Parse the arguments. */
15821 arguments = cp_parser_enclosed_template_argument_list (parser);
15824 /* Set the location to be of the form:
15825 template-name < template-argument-list [opt] >
15826 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15827 with caret == start at the start of the template-name,
15828 ranging until the closing '>'. */
15829 location_t finish_loc
15830 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15831 location_t combined_loc
15832 = make_location (token->location, token->location, finish_loc);
15834 /* Build a representation of the specialization. */
15835 if (identifier_p (templ))
15836 template_id = build_min_nt_loc (combined_loc,
15837 TEMPLATE_ID_EXPR,
15838 templ, arguments);
15839 else if (DECL_TYPE_TEMPLATE_P (templ)
15840 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15842 bool entering_scope;
15843 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15844 template (rather than some instantiation thereof) only if
15845 is not nested within some other construct. For example, in
15846 "template <typename T> void f(T) { A<T>::", A<T> is just an
15847 instantiation of A. */
15848 entering_scope = (template_parm_scope_p ()
15849 && cp_lexer_next_token_is (parser->lexer,
15850 CPP_SCOPE));
15851 template_id
15852 = finish_template_type (templ, arguments, entering_scope);
15854 /* A template-like identifier may be a partial concept id. */
15855 else if (flag_concepts
15856 && (template_id = (cp_parser_maybe_partial_concept_id
15857 (parser, templ, arguments))))
15858 return template_id;
15859 else if (variable_template_p (templ))
15861 template_id = lookup_template_variable (templ, arguments);
15862 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15863 SET_EXPR_LOCATION (template_id, combined_loc);
15865 else
15867 /* If it's not a class-template or a template-template, it should be
15868 a function-template. */
15869 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15870 || TREE_CODE (templ) == OVERLOAD
15871 || BASELINK_P (templ)));
15873 template_id = lookup_template_function (templ, arguments);
15874 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15875 SET_EXPR_LOCATION (template_id, combined_loc);
15878 /* If parsing tentatively, replace the sequence of tokens that makes
15879 up the template-id with a CPP_TEMPLATE_ID token. That way,
15880 should we re-parse the token stream, we will not have to repeat
15881 the effort required to do the parse, nor will we issue duplicate
15882 error messages about problems during instantiation of the
15883 template. */
15884 if (start_of_id
15885 /* Don't do this if we had a parse error in a declarator; re-parsing
15886 might succeed if a name changes meaning (60361). */
15887 && !(cp_parser_error_occurred (parser)
15888 && cp_parser_parsing_tentatively (parser)
15889 && parser->in_declarator_p))
15891 /* Reset the contents of the START_OF_ID token. */
15892 token->type = CPP_TEMPLATE_ID;
15893 token->location = combined_loc;
15895 /* We must mark the lookup as kept, so we don't throw it away on
15896 the first parse. */
15897 if (is_overloaded_fn (template_id))
15898 lookup_keep (get_fns (template_id), true);
15900 /* Retrieve any deferred checks. Do not pop this access checks yet
15901 so the memory will not be reclaimed during token replacing below. */
15902 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15903 token->u.tree_check_value->value = template_id;
15904 token->u.tree_check_value->checks = get_deferred_access_checks ();
15905 token->keyword = RID_MAX;
15907 /* Purge all subsequent tokens. */
15908 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15910 /* ??? Can we actually assume that, if template_id ==
15911 error_mark_node, we will have issued a diagnostic to the
15912 user, as opposed to simply marking the tentative parse as
15913 failed? */
15914 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15915 error_at (token->location, "parse error in template argument list");
15918 pop_to_parent_deferring_access_checks ();
15919 return template_id;
15922 /* Parse a template-name.
15924 template-name:
15925 identifier
15927 The standard should actually say:
15929 template-name:
15930 identifier
15931 operator-function-id
15933 A defect report has been filed about this issue.
15935 A conversion-function-id cannot be a template name because they cannot
15936 be part of a template-id. In fact, looking at this code:
15938 a.operator K<int>()
15940 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15941 It is impossible to call a templated conversion-function-id with an
15942 explicit argument list, since the only allowed template parameter is
15943 the type to which it is converting.
15945 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15946 `template' keyword, in a construction like:
15948 T::template f<3>()
15950 In that case `f' is taken to be a template-name, even though there
15951 is no way of knowing for sure.
15953 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15954 name refers to a set of overloaded functions, at least one of which
15955 is a template, or an IDENTIFIER_NODE with the name of the template,
15956 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15957 names are looked up inside uninstantiated templates. */
15959 static tree
15960 cp_parser_template_name (cp_parser* parser,
15961 bool template_keyword_p,
15962 bool check_dependency_p,
15963 bool is_declaration,
15964 enum tag_types tag_type,
15965 bool *is_identifier)
15967 tree identifier;
15968 tree decl;
15969 cp_token *token = cp_lexer_peek_token (parser->lexer);
15971 /* If the next token is `operator', then we have either an
15972 operator-function-id or a conversion-function-id. */
15973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15975 /* We don't know whether we're looking at an
15976 operator-function-id or a conversion-function-id. */
15977 cp_parser_parse_tentatively (parser);
15978 /* Try an operator-function-id. */
15979 identifier = cp_parser_operator_function_id (parser);
15980 /* If that didn't work, try a conversion-function-id. */
15981 if (!cp_parser_parse_definitely (parser))
15983 cp_parser_error (parser, "expected template-name");
15984 return error_mark_node;
15987 /* Look for the identifier. */
15988 else
15989 identifier = cp_parser_identifier (parser);
15991 /* If we didn't find an identifier, we don't have a template-id. */
15992 if (identifier == error_mark_node)
15993 return error_mark_node;
15995 /* If the name immediately followed the `template' keyword, then it
15996 is a template-name. However, if the next token is not `<', then
15997 we do not treat it as a template-name, since it is not being used
15998 as part of a template-id. This enables us to handle constructs
15999 like:
16001 template <typename T> struct S { S(); };
16002 template <typename T> S<T>::S();
16004 correctly. We would treat `S' as a template -- if it were `S<T>'
16005 -- but we do not if there is no `<'. */
16007 if (processing_template_decl
16008 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16010 /* In a declaration, in a dependent context, we pretend that the
16011 "template" keyword was present in order to improve error
16012 recovery. For example, given:
16014 template <typename T> void f(T::X<int>);
16016 we want to treat "X<int>" as a template-id. */
16017 if (is_declaration
16018 && !template_keyword_p
16019 && parser->scope && TYPE_P (parser->scope)
16020 && check_dependency_p
16021 && dependent_scope_p (parser->scope)
16022 /* Do not do this for dtors (or ctors), since they never
16023 need the template keyword before their name. */
16024 && !constructor_name_p (identifier, parser->scope))
16026 cp_token_position start = 0;
16028 /* Explain what went wrong. */
16029 error_at (token->location, "non-template %qD used as template",
16030 identifier);
16031 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16032 parser->scope, identifier);
16033 /* If parsing tentatively, find the location of the "<" token. */
16034 if (cp_parser_simulate_error (parser))
16035 start = cp_lexer_token_position (parser->lexer, true);
16036 /* Parse the template arguments so that we can issue error
16037 messages about them. */
16038 cp_lexer_consume_token (parser->lexer);
16039 cp_parser_enclosed_template_argument_list (parser);
16040 /* Skip tokens until we find a good place from which to
16041 continue parsing. */
16042 cp_parser_skip_to_closing_parenthesis (parser,
16043 /*recovering=*/true,
16044 /*or_comma=*/true,
16045 /*consume_paren=*/false);
16046 /* If parsing tentatively, permanently remove the
16047 template argument list. That will prevent duplicate
16048 error messages from being issued about the missing
16049 "template" keyword. */
16050 if (start)
16051 cp_lexer_purge_tokens_after (parser->lexer, start);
16052 if (is_identifier)
16053 *is_identifier = true;
16054 parser->context->object_type = NULL_TREE;
16055 return identifier;
16058 /* If the "template" keyword is present, then there is generally
16059 no point in doing name-lookup, so we just return IDENTIFIER.
16060 But, if the qualifying scope is non-dependent then we can
16061 (and must) do name-lookup normally. */
16062 if (template_keyword_p)
16064 tree scope = (parser->scope ? parser->scope
16065 : parser->context->object_type);
16066 if (scope && TYPE_P (scope)
16067 && (!CLASS_TYPE_P (scope)
16068 || (check_dependency_p && dependent_type_p (scope))))
16070 /* We're optimizing away the call to cp_parser_lookup_name, but
16071 we still need to do this. */
16072 parser->context->object_type = NULL_TREE;
16073 return identifier;
16078 /* Look up the name. */
16079 decl = cp_parser_lookup_name (parser, identifier,
16080 tag_type,
16081 /*is_template=*/true,
16082 /*is_namespace=*/false,
16083 check_dependency_p,
16084 /*ambiguous_decls=*/NULL,
16085 token->location);
16087 decl = strip_using_decl (decl);
16089 /* If DECL is a template, then the name was a template-name. */
16090 if (TREE_CODE (decl) == TEMPLATE_DECL)
16092 if (TREE_DEPRECATED (decl)
16093 && deprecated_state != DEPRECATED_SUPPRESS)
16094 warn_deprecated_use (decl, NULL_TREE);
16096 else
16098 /* The standard does not explicitly indicate whether a name that
16099 names a set of overloaded declarations, some of which are
16100 templates, is a template-name. However, such a name should
16101 be a template-name; otherwise, there is no way to form a
16102 template-id for the overloaded templates. */
16103 bool found = false;
16105 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16106 !found && iter; ++iter)
16107 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16108 found = true;
16110 if (!found)
16112 /* The name does not name a template. */
16113 cp_parser_error (parser, "expected template-name");
16114 return error_mark_node;
16118 /* If DECL is dependent, and refers to a function, then just return
16119 its name; we will look it up again during template instantiation. */
16120 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16122 tree scope = ovl_scope (decl);
16123 if (TYPE_P (scope) && dependent_type_p (scope))
16124 return identifier;
16127 return decl;
16130 /* Parse a template-argument-list.
16132 template-argument-list:
16133 template-argument ... [opt]
16134 template-argument-list , template-argument ... [opt]
16136 Returns a TREE_VEC containing the arguments. */
16138 static tree
16139 cp_parser_template_argument_list (cp_parser* parser)
16141 tree fixed_args[10];
16142 unsigned n_args = 0;
16143 unsigned alloced = 10;
16144 tree *arg_ary = fixed_args;
16145 tree vec;
16146 bool saved_in_template_argument_list_p;
16147 bool saved_ice_p;
16148 bool saved_non_ice_p;
16150 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16151 parser->in_template_argument_list_p = true;
16152 /* Even if the template-id appears in an integral
16153 constant-expression, the contents of the argument list do
16154 not. */
16155 saved_ice_p = parser->integral_constant_expression_p;
16156 parser->integral_constant_expression_p = false;
16157 saved_non_ice_p = parser->non_integral_constant_expression_p;
16158 parser->non_integral_constant_expression_p = false;
16160 /* Parse the arguments. */
16163 tree argument;
16165 if (n_args)
16166 /* Consume the comma. */
16167 cp_lexer_consume_token (parser->lexer);
16169 /* Parse the template-argument. */
16170 argument = cp_parser_template_argument (parser);
16172 /* If the next token is an ellipsis, we're expanding a template
16173 argument pack. */
16174 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16176 if (argument == error_mark_node)
16178 cp_token *token = cp_lexer_peek_token (parser->lexer);
16179 error_at (token->location,
16180 "expected parameter pack before %<...%>");
16182 /* Consume the `...' token. */
16183 cp_lexer_consume_token (parser->lexer);
16185 /* Make the argument into a TYPE_PACK_EXPANSION or
16186 EXPR_PACK_EXPANSION. */
16187 argument = make_pack_expansion (argument);
16190 if (n_args == alloced)
16192 alloced *= 2;
16194 if (arg_ary == fixed_args)
16196 arg_ary = XNEWVEC (tree, alloced);
16197 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16199 else
16200 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16202 arg_ary[n_args++] = argument;
16204 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16206 vec = make_tree_vec (n_args);
16208 while (n_args--)
16209 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16211 if (arg_ary != fixed_args)
16212 free (arg_ary);
16213 parser->non_integral_constant_expression_p = saved_non_ice_p;
16214 parser->integral_constant_expression_p = saved_ice_p;
16215 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16216 if (CHECKING_P)
16217 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16218 return vec;
16221 /* Parse a template-argument.
16223 template-argument:
16224 assignment-expression
16225 type-id
16226 id-expression
16228 The representation is that of an assignment-expression, type-id, or
16229 id-expression -- except that the qualified id-expression is
16230 evaluated, so that the value returned is either a DECL or an
16231 OVERLOAD.
16233 Although the standard says "assignment-expression", it forbids
16234 throw-expressions or assignments in the template argument.
16235 Therefore, we use "conditional-expression" instead. */
16237 static tree
16238 cp_parser_template_argument (cp_parser* parser)
16240 tree argument;
16241 bool template_p;
16242 bool address_p;
16243 bool maybe_type_id = false;
16244 cp_token *token = NULL, *argument_start_token = NULL;
16245 location_t loc = 0;
16246 cp_id_kind idk;
16248 /* There's really no way to know what we're looking at, so we just
16249 try each alternative in order.
16251 [temp.arg]
16253 In a template-argument, an ambiguity between a type-id and an
16254 expression is resolved to a type-id, regardless of the form of
16255 the corresponding template-parameter.
16257 Therefore, we try a type-id first. */
16258 cp_parser_parse_tentatively (parser);
16259 argument = cp_parser_template_type_arg (parser);
16260 /* If there was no error parsing the type-id but the next token is a
16261 '>>', our behavior depends on which dialect of C++ we're
16262 parsing. In C++98, we probably found a typo for '> >'. But there
16263 are type-id which are also valid expressions. For instance:
16265 struct X { int operator >> (int); };
16266 template <int V> struct Foo {};
16267 Foo<X () >> 5> r;
16269 Here 'X()' is a valid type-id of a function type, but the user just
16270 wanted to write the expression "X() >> 5". Thus, we remember that we
16271 found a valid type-id, but we still try to parse the argument as an
16272 expression to see what happens.
16274 In C++0x, the '>>' will be considered two separate '>'
16275 tokens. */
16276 if (!cp_parser_error_occurred (parser)
16277 && cxx_dialect == cxx98
16278 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16280 maybe_type_id = true;
16281 cp_parser_abort_tentative_parse (parser);
16283 else
16285 /* If the next token isn't a `,' or a `>', then this argument wasn't
16286 really finished. This means that the argument is not a valid
16287 type-id. */
16288 if (!cp_parser_next_token_ends_template_argument_p (parser))
16289 cp_parser_error (parser, "expected template-argument");
16290 /* If that worked, we're done. */
16291 if (cp_parser_parse_definitely (parser))
16292 return argument;
16294 /* We're still not sure what the argument will be. */
16295 cp_parser_parse_tentatively (parser);
16296 /* Try a template. */
16297 argument_start_token = cp_lexer_peek_token (parser->lexer);
16298 argument = cp_parser_id_expression (parser,
16299 /*template_keyword_p=*/false,
16300 /*check_dependency_p=*/true,
16301 &template_p,
16302 /*declarator_p=*/false,
16303 /*optional_p=*/false);
16304 /* If the next token isn't a `,' or a `>', then this argument wasn't
16305 really finished. */
16306 if (!cp_parser_next_token_ends_template_argument_p (parser))
16307 cp_parser_error (parser, "expected template-argument");
16308 if (!cp_parser_error_occurred (parser))
16310 /* Figure out what is being referred to. If the id-expression
16311 was for a class template specialization, then we will have a
16312 TYPE_DECL at this point. There is no need to do name lookup
16313 at this point in that case. */
16314 if (TREE_CODE (argument) != TYPE_DECL)
16315 argument = cp_parser_lookup_name (parser, argument,
16316 none_type,
16317 /*is_template=*/template_p,
16318 /*is_namespace=*/false,
16319 /*check_dependency=*/true,
16320 /*ambiguous_decls=*/NULL,
16321 argument_start_token->location);
16322 /* Handle a constrained-type-specifier for a non-type template
16323 parameter. */
16324 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16325 argument = decl;
16326 else if (TREE_CODE (argument) != TEMPLATE_DECL
16327 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16328 cp_parser_error (parser, "expected template-name");
16330 if (cp_parser_parse_definitely (parser))
16332 if (TREE_DEPRECATED (argument))
16333 warn_deprecated_use (argument, NULL_TREE);
16334 return argument;
16336 /* It must be a non-type argument. In C++17 any constant-expression is
16337 allowed. */
16338 if (cxx_dialect > cxx14)
16339 goto general_expr;
16341 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16343 -- an integral constant-expression of integral or enumeration
16344 type; or
16346 -- the name of a non-type template-parameter; or
16348 -- the name of an object or function with external linkage...
16350 -- the address of an object or function with external linkage...
16352 -- a pointer to member... */
16353 /* Look for a non-type template parameter. */
16354 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16356 cp_parser_parse_tentatively (parser);
16357 argument = cp_parser_primary_expression (parser,
16358 /*address_p=*/false,
16359 /*cast_p=*/false,
16360 /*template_arg_p=*/true,
16361 &idk);
16362 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16363 || !cp_parser_next_token_ends_template_argument_p (parser))
16364 cp_parser_simulate_error (parser);
16365 if (cp_parser_parse_definitely (parser))
16366 return argument;
16369 /* If the next token is "&", the argument must be the address of an
16370 object or function with external linkage. */
16371 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16372 if (address_p)
16374 loc = cp_lexer_peek_token (parser->lexer)->location;
16375 cp_lexer_consume_token (parser->lexer);
16377 /* See if we might have an id-expression. */
16378 token = cp_lexer_peek_token (parser->lexer);
16379 if (token->type == CPP_NAME
16380 || token->keyword == RID_OPERATOR
16381 || token->type == CPP_SCOPE
16382 || token->type == CPP_TEMPLATE_ID
16383 || token->type == CPP_NESTED_NAME_SPECIFIER)
16385 cp_parser_parse_tentatively (parser);
16386 argument = cp_parser_primary_expression (parser,
16387 address_p,
16388 /*cast_p=*/false,
16389 /*template_arg_p=*/true,
16390 &idk);
16391 if (cp_parser_error_occurred (parser)
16392 || !cp_parser_next_token_ends_template_argument_p (parser))
16393 cp_parser_abort_tentative_parse (parser);
16394 else
16396 tree probe;
16398 if (INDIRECT_REF_P (argument))
16400 /* Strip the dereference temporarily. */
16401 gcc_assert (REFERENCE_REF_P (argument));
16402 argument = TREE_OPERAND (argument, 0);
16405 /* If we're in a template, we represent a qualified-id referring
16406 to a static data member as a SCOPE_REF even if the scope isn't
16407 dependent so that we can check access control later. */
16408 probe = argument;
16409 if (TREE_CODE (probe) == SCOPE_REF)
16410 probe = TREE_OPERAND (probe, 1);
16411 if (VAR_P (probe))
16413 /* A variable without external linkage might still be a
16414 valid constant-expression, so no error is issued here
16415 if the external-linkage check fails. */
16416 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16417 cp_parser_simulate_error (parser);
16419 else if (is_overloaded_fn (argument))
16420 /* All overloaded functions are allowed; if the external
16421 linkage test does not pass, an error will be issued
16422 later. */
16424 else if (address_p
16425 && (TREE_CODE (argument) == OFFSET_REF
16426 || TREE_CODE (argument) == SCOPE_REF))
16427 /* A pointer-to-member. */
16429 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16431 else
16432 cp_parser_simulate_error (parser);
16434 if (cp_parser_parse_definitely (parser))
16436 if (address_p)
16437 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16438 tf_warning_or_error);
16439 else
16440 argument = convert_from_reference (argument);
16441 return argument;
16445 /* If the argument started with "&", there are no other valid
16446 alternatives at this point. */
16447 if (address_p)
16449 cp_parser_error (parser, "invalid non-type template argument");
16450 return error_mark_node;
16453 general_expr:
16454 /* If the argument wasn't successfully parsed as a type-id followed
16455 by '>>', the argument can only be a constant expression now.
16456 Otherwise, we try parsing the constant-expression tentatively,
16457 because the argument could really be a type-id. */
16458 if (maybe_type_id)
16459 cp_parser_parse_tentatively (parser);
16461 if (cxx_dialect <= cxx14)
16462 argument = cp_parser_constant_expression (parser);
16463 else
16465 /* With C++17 generalized non-type template arguments we need to handle
16466 lvalue constant expressions, too. */
16467 argument = cp_parser_assignment_expression (parser);
16468 require_potential_constant_expression (argument);
16471 if (!maybe_type_id)
16472 return argument;
16473 if (!cp_parser_next_token_ends_template_argument_p (parser))
16474 cp_parser_error (parser, "expected template-argument");
16475 if (cp_parser_parse_definitely (parser))
16476 return argument;
16477 /* We did our best to parse the argument as a non type-id, but that
16478 was the only alternative that matched (albeit with a '>' after
16479 it). We can assume it's just a typo from the user, and a
16480 diagnostic will then be issued. */
16481 return cp_parser_template_type_arg (parser);
16484 /* Parse an explicit-instantiation.
16486 explicit-instantiation:
16487 template declaration
16489 Although the standard says `declaration', what it really means is:
16491 explicit-instantiation:
16492 template decl-specifier-seq [opt] declarator [opt] ;
16494 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16495 supposed to be allowed. A defect report has been filed about this
16496 issue.
16498 GNU Extension:
16500 explicit-instantiation:
16501 storage-class-specifier template
16502 decl-specifier-seq [opt] declarator [opt] ;
16503 function-specifier template
16504 decl-specifier-seq [opt] declarator [opt] ; */
16506 static void
16507 cp_parser_explicit_instantiation (cp_parser* parser)
16509 int declares_class_or_enum;
16510 cp_decl_specifier_seq decl_specifiers;
16511 tree extension_specifier = NULL_TREE;
16513 timevar_push (TV_TEMPLATE_INST);
16515 /* Look for an (optional) storage-class-specifier or
16516 function-specifier. */
16517 if (cp_parser_allow_gnu_extensions_p (parser))
16519 extension_specifier
16520 = cp_parser_storage_class_specifier_opt (parser);
16521 if (!extension_specifier)
16522 extension_specifier
16523 = cp_parser_function_specifier_opt (parser,
16524 /*decl_specs=*/NULL);
16527 /* Look for the `template' keyword. */
16528 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16529 /* Let the front end know that we are processing an explicit
16530 instantiation. */
16531 begin_explicit_instantiation ();
16532 /* [temp.explicit] says that we are supposed to ignore access
16533 control while processing explicit instantiation directives. */
16534 push_deferring_access_checks (dk_no_check);
16535 /* Parse a decl-specifier-seq. */
16536 cp_parser_decl_specifier_seq (parser,
16537 CP_PARSER_FLAGS_OPTIONAL,
16538 &decl_specifiers,
16539 &declares_class_or_enum);
16540 /* If there was exactly one decl-specifier, and it declared a class,
16541 and there's no declarator, then we have an explicit type
16542 instantiation. */
16543 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16545 tree type;
16547 type = check_tag_decl (&decl_specifiers,
16548 /*explicit_type_instantiation_p=*/true);
16549 /* Turn access control back on for names used during
16550 template instantiation. */
16551 pop_deferring_access_checks ();
16552 if (type)
16553 do_type_instantiation (type, extension_specifier,
16554 /*complain=*/tf_error);
16556 else
16558 cp_declarator *declarator;
16559 tree decl;
16561 /* Parse the declarator. */
16562 declarator
16563 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16564 /*ctor_dtor_or_conv_p=*/NULL,
16565 /*parenthesized_p=*/NULL,
16566 /*member_p=*/false,
16567 /*friend_p=*/false);
16568 if (declares_class_or_enum & 2)
16569 cp_parser_check_for_definition_in_return_type (declarator,
16570 decl_specifiers.type,
16571 decl_specifiers.locations[ds_type_spec]);
16572 if (declarator != cp_error_declarator)
16574 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16575 permerror (decl_specifiers.locations[ds_inline],
16576 "explicit instantiation shall not use"
16577 " %<inline%> specifier");
16578 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16579 permerror (decl_specifiers.locations[ds_constexpr],
16580 "explicit instantiation shall not use"
16581 " %<constexpr%> specifier");
16583 decl = grokdeclarator (declarator, &decl_specifiers,
16584 NORMAL, 0, &decl_specifiers.attributes);
16585 /* Turn access control back on for names used during
16586 template instantiation. */
16587 pop_deferring_access_checks ();
16588 /* Do the explicit instantiation. */
16589 do_decl_instantiation (decl, extension_specifier);
16591 else
16593 pop_deferring_access_checks ();
16594 /* Skip the body of the explicit instantiation. */
16595 cp_parser_skip_to_end_of_statement (parser);
16598 /* We're done with the instantiation. */
16599 end_explicit_instantiation ();
16601 cp_parser_consume_semicolon_at_end_of_statement (parser);
16603 timevar_pop (TV_TEMPLATE_INST);
16606 /* Parse an explicit-specialization.
16608 explicit-specialization:
16609 template < > declaration
16611 Although the standard says `declaration', what it really means is:
16613 explicit-specialization:
16614 template <> decl-specifier [opt] init-declarator [opt] ;
16615 template <> function-definition
16616 template <> explicit-specialization
16617 template <> template-declaration */
16619 static void
16620 cp_parser_explicit_specialization (cp_parser* parser)
16622 bool need_lang_pop;
16623 cp_token *token = cp_lexer_peek_token (parser->lexer);
16625 /* Look for the `template' keyword. */
16626 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16627 /* Look for the `<'. */
16628 cp_parser_require (parser, CPP_LESS, RT_LESS);
16629 /* Look for the `>'. */
16630 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16631 /* We have processed another parameter list. */
16632 ++parser->num_template_parameter_lists;
16633 /* [temp]
16635 A template ... explicit specialization ... shall not have C
16636 linkage. */
16637 if (current_lang_name == lang_name_c)
16639 error_at (token->location, "template specialization with C linkage");
16640 maybe_show_extern_c_location ();
16641 /* Give it C++ linkage to avoid confusing other parts of the
16642 front end. */
16643 push_lang_context (lang_name_cplusplus);
16644 need_lang_pop = true;
16646 else
16647 need_lang_pop = false;
16648 /* Let the front end know that we are beginning a specialization. */
16649 if (!begin_specialization ())
16651 end_specialization ();
16652 return;
16655 /* If the next keyword is `template', we need to figure out whether
16656 or not we're looking a template-declaration. */
16657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16659 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16660 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16661 cp_parser_template_declaration_after_export (parser,
16662 /*member_p=*/false);
16663 else
16664 cp_parser_explicit_specialization (parser);
16666 else
16667 /* Parse the dependent declaration. */
16668 cp_parser_single_declaration (parser,
16669 /*checks=*/NULL,
16670 /*member_p=*/false,
16671 /*explicit_specialization_p=*/true,
16672 /*friend_p=*/NULL);
16673 /* We're done with the specialization. */
16674 end_specialization ();
16675 /* For the erroneous case of a template with C linkage, we pushed an
16676 implicit C++ linkage scope; exit that scope now. */
16677 if (need_lang_pop)
16678 pop_lang_context ();
16679 /* We're done with this parameter list. */
16680 --parser->num_template_parameter_lists;
16683 /* Parse a type-specifier.
16685 type-specifier:
16686 simple-type-specifier
16687 class-specifier
16688 enum-specifier
16689 elaborated-type-specifier
16690 cv-qualifier
16692 GNU Extension:
16694 type-specifier:
16695 __complex__
16697 Returns a representation of the type-specifier. For a
16698 class-specifier, enum-specifier, or elaborated-type-specifier, a
16699 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16701 The parser flags FLAGS is used to control type-specifier parsing.
16703 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16704 in a decl-specifier-seq.
16706 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16707 class-specifier, enum-specifier, or elaborated-type-specifier, then
16708 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16709 if a type is declared; 2 if it is defined. Otherwise, it is set to
16710 zero.
16712 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16713 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16714 is set to FALSE. */
16716 static tree
16717 cp_parser_type_specifier (cp_parser* parser,
16718 cp_parser_flags flags,
16719 cp_decl_specifier_seq *decl_specs,
16720 bool is_declaration,
16721 int* declares_class_or_enum,
16722 bool* is_cv_qualifier)
16724 tree type_spec = NULL_TREE;
16725 cp_token *token;
16726 enum rid keyword;
16727 cp_decl_spec ds = ds_last;
16729 /* Assume this type-specifier does not declare a new type. */
16730 if (declares_class_or_enum)
16731 *declares_class_or_enum = 0;
16732 /* And that it does not specify a cv-qualifier. */
16733 if (is_cv_qualifier)
16734 *is_cv_qualifier = false;
16735 /* Peek at the next token. */
16736 token = cp_lexer_peek_token (parser->lexer);
16738 /* If we're looking at a keyword, we can use that to guide the
16739 production we choose. */
16740 keyword = token->keyword;
16741 switch (keyword)
16743 case RID_ENUM:
16744 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16745 goto elaborated_type_specifier;
16747 /* Look for the enum-specifier. */
16748 type_spec = cp_parser_enum_specifier (parser);
16749 /* If that worked, we're done. */
16750 if (type_spec)
16752 if (declares_class_or_enum)
16753 *declares_class_or_enum = 2;
16754 if (decl_specs)
16755 cp_parser_set_decl_spec_type (decl_specs,
16756 type_spec,
16757 token,
16758 /*type_definition_p=*/true);
16759 return type_spec;
16761 else
16762 goto elaborated_type_specifier;
16764 /* Any of these indicate either a class-specifier, or an
16765 elaborated-type-specifier. */
16766 case RID_CLASS:
16767 case RID_STRUCT:
16768 case RID_UNION:
16769 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16770 goto elaborated_type_specifier;
16772 /* Parse tentatively so that we can back up if we don't find a
16773 class-specifier. */
16774 cp_parser_parse_tentatively (parser);
16775 /* Look for the class-specifier. */
16776 type_spec = cp_parser_class_specifier (parser);
16777 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16778 /* If that worked, we're done. */
16779 if (cp_parser_parse_definitely (parser))
16781 if (declares_class_or_enum)
16782 *declares_class_or_enum = 2;
16783 if (decl_specs)
16784 cp_parser_set_decl_spec_type (decl_specs,
16785 type_spec,
16786 token,
16787 /*type_definition_p=*/true);
16788 return type_spec;
16791 /* Fall through. */
16792 elaborated_type_specifier:
16793 /* We're declaring (not defining) a class or enum. */
16794 if (declares_class_or_enum)
16795 *declares_class_or_enum = 1;
16797 /* Fall through. */
16798 case RID_TYPENAME:
16799 /* Look for an elaborated-type-specifier. */
16800 type_spec
16801 = (cp_parser_elaborated_type_specifier
16802 (parser,
16803 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16804 is_declaration));
16805 if (decl_specs)
16806 cp_parser_set_decl_spec_type (decl_specs,
16807 type_spec,
16808 token,
16809 /*type_definition_p=*/false);
16810 return type_spec;
16812 case RID_CONST:
16813 ds = ds_const;
16814 if (is_cv_qualifier)
16815 *is_cv_qualifier = true;
16816 break;
16818 case RID_VOLATILE:
16819 ds = ds_volatile;
16820 if (is_cv_qualifier)
16821 *is_cv_qualifier = true;
16822 break;
16824 case RID_RESTRICT:
16825 ds = ds_restrict;
16826 if (is_cv_qualifier)
16827 *is_cv_qualifier = true;
16828 break;
16830 case RID_COMPLEX:
16831 /* The `__complex__' keyword is a GNU extension. */
16832 ds = ds_complex;
16833 break;
16835 default:
16836 break;
16839 /* Handle simple keywords. */
16840 if (ds != ds_last)
16842 if (decl_specs)
16844 set_and_check_decl_spec_loc (decl_specs, ds, token);
16845 decl_specs->any_specifiers_p = true;
16847 return cp_lexer_consume_token (parser->lexer)->u.value;
16850 /* If we do not already have a type-specifier, assume we are looking
16851 at a simple-type-specifier. */
16852 type_spec = cp_parser_simple_type_specifier (parser,
16853 decl_specs,
16854 flags);
16856 /* If we didn't find a type-specifier, and a type-specifier was not
16857 optional in this context, issue an error message. */
16858 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16860 cp_parser_error (parser, "expected type specifier");
16861 return error_mark_node;
16864 return type_spec;
16867 /* Parse a simple-type-specifier.
16869 simple-type-specifier:
16870 :: [opt] nested-name-specifier [opt] type-name
16871 :: [opt] nested-name-specifier template template-id
16872 char
16873 wchar_t
16874 bool
16875 short
16877 long
16878 signed
16879 unsigned
16880 float
16881 double
16882 void
16884 C++11 Extension:
16886 simple-type-specifier:
16887 auto
16888 decltype ( expression )
16889 char16_t
16890 char32_t
16891 __underlying_type ( type-id )
16893 C++17 extension:
16895 nested-name-specifier(opt) template-name
16897 GNU Extension:
16899 simple-type-specifier:
16900 __int128
16901 __typeof__ unary-expression
16902 __typeof__ ( type-id )
16903 __typeof__ ( type-id ) { initializer-list , [opt] }
16905 Concepts Extension:
16907 simple-type-specifier:
16908 constrained-type-specifier
16910 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16911 appropriately updated. */
16913 static tree
16914 cp_parser_simple_type_specifier (cp_parser* parser,
16915 cp_decl_specifier_seq *decl_specs,
16916 cp_parser_flags flags)
16918 tree type = NULL_TREE;
16919 cp_token *token;
16920 int idx;
16922 /* Peek at the next token. */
16923 token = cp_lexer_peek_token (parser->lexer);
16925 /* If we're looking at a keyword, things are easy. */
16926 switch (token->keyword)
16928 case RID_CHAR:
16929 if (decl_specs)
16930 decl_specs->explicit_char_p = true;
16931 type = char_type_node;
16932 break;
16933 case RID_CHAR16:
16934 type = char16_type_node;
16935 break;
16936 case RID_CHAR32:
16937 type = char32_type_node;
16938 break;
16939 case RID_WCHAR:
16940 type = wchar_type_node;
16941 break;
16942 case RID_BOOL:
16943 type = boolean_type_node;
16944 break;
16945 case RID_SHORT:
16946 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16947 type = short_integer_type_node;
16948 break;
16949 case RID_INT:
16950 if (decl_specs)
16951 decl_specs->explicit_int_p = true;
16952 type = integer_type_node;
16953 break;
16954 case RID_INT_N_0:
16955 case RID_INT_N_1:
16956 case RID_INT_N_2:
16957 case RID_INT_N_3:
16958 idx = token->keyword - RID_INT_N_0;
16959 if (! int_n_enabled_p [idx])
16960 break;
16961 if (decl_specs)
16963 decl_specs->explicit_intN_p = true;
16964 decl_specs->int_n_idx = idx;
16966 type = int_n_trees [idx].signed_type;
16967 break;
16968 case RID_LONG:
16969 if (decl_specs)
16970 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16971 type = long_integer_type_node;
16972 break;
16973 case RID_SIGNED:
16974 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16975 type = integer_type_node;
16976 break;
16977 case RID_UNSIGNED:
16978 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16979 type = unsigned_type_node;
16980 break;
16981 case RID_FLOAT:
16982 type = float_type_node;
16983 break;
16984 case RID_DOUBLE:
16985 type = double_type_node;
16986 break;
16987 case RID_VOID:
16988 type = void_type_node;
16989 break;
16991 case RID_AUTO:
16992 maybe_warn_cpp0x (CPP0X_AUTO);
16993 if (parser->auto_is_implicit_function_template_parm_p)
16995 /* The 'auto' might be the placeholder return type for a function decl
16996 with trailing return type. */
16997 bool have_trailing_return_fn_decl = false;
16999 cp_parser_parse_tentatively (parser);
17000 cp_lexer_consume_token (parser->lexer);
17001 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17002 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17003 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17004 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17006 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17008 cp_lexer_consume_token (parser->lexer);
17009 cp_parser_skip_to_closing_parenthesis (parser,
17010 /*recovering*/false,
17011 /*or_comma*/false,
17012 /*consume_paren*/true);
17013 continue;
17016 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17018 have_trailing_return_fn_decl = true;
17019 break;
17022 cp_lexer_consume_token (parser->lexer);
17024 cp_parser_abort_tentative_parse (parser);
17026 if (have_trailing_return_fn_decl)
17028 type = make_auto ();
17029 break;
17032 if (cxx_dialect >= cxx14)
17034 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17035 type = TREE_TYPE (type);
17037 else
17038 type = error_mark_node;
17040 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17042 if (cxx_dialect < cxx14)
17043 error_at (token->location,
17044 "use of %<auto%> in lambda parameter declaration "
17045 "only available with "
17046 "-std=c++14 or -std=gnu++14");
17048 else if (cxx_dialect < cxx14)
17049 error_at (token->location,
17050 "use of %<auto%> in parameter declaration "
17051 "only available with "
17052 "-std=c++14 or -std=gnu++14");
17053 else if (!flag_concepts)
17054 pedwarn (token->location, 0,
17055 "use of %<auto%> in parameter declaration "
17056 "only available with -fconcepts");
17058 else
17059 type = make_auto ();
17060 break;
17062 case RID_DECLTYPE:
17063 /* Since DR 743, decltype can either be a simple-type-specifier by
17064 itself or begin a nested-name-specifier. Parsing it will replace
17065 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17066 handling below decide what to do. */
17067 cp_parser_decltype (parser);
17068 cp_lexer_set_token_position (parser->lexer, token);
17069 break;
17071 case RID_TYPEOF:
17072 /* Consume the `typeof' token. */
17073 cp_lexer_consume_token (parser->lexer);
17074 /* Parse the operand to `typeof'. */
17075 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17076 /* If it is not already a TYPE, take its type. */
17077 if (!TYPE_P (type))
17078 type = finish_typeof (type);
17080 if (decl_specs)
17081 cp_parser_set_decl_spec_type (decl_specs, type,
17082 token,
17083 /*type_definition_p=*/false);
17085 return type;
17087 case RID_UNDERLYING_TYPE:
17088 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17089 if (decl_specs)
17090 cp_parser_set_decl_spec_type (decl_specs, type,
17091 token,
17092 /*type_definition_p=*/false);
17094 return type;
17096 case RID_BASES:
17097 case RID_DIRECT_BASES:
17098 type = cp_parser_trait_expr (parser, token->keyword);
17099 if (decl_specs)
17100 cp_parser_set_decl_spec_type (decl_specs, type,
17101 token,
17102 /*type_definition_p=*/false);
17103 return type;
17104 default:
17105 break;
17108 /* If token is an already-parsed decltype not followed by ::,
17109 it's a simple-type-specifier. */
17110 if (token->type == CPP_DECLTYPE
17111 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17113 type = saved_checks_value (token->u.tree_check_value);
17114 if (decl_specs)
17116 cp_parser_set_decl_spec_type (decl_specs, type,
17117 token,
17118 /*type_definition_p=*/false);
17119 /* Remember that we are handling a decltype in order to
17120 implement the resolution of DR 1510 when the argument
17121 isn't instantiation dependent. */
17122 decl_specs->decltype_p = true;
17124 cp_lexer_consume_token (parser->lexer);
17125 return type;
17128 /* If the type-specifier was for a built-in type, we're done. */
17129 if (type)
17131 /* Record the type. */
17132 if (decl_specs
17133 && (token->keyword != RID_SIGNED
17134 && token->keyword != RID_UNSIGNED
17135 && token->keyword != RID_SHORT
17136 && token->keyword != RID_LONG))
17137 cp_parser_set_decl_spec_type (decl_specs,
17138 type,
17139 token,
17140 /*type_definition_p=*/false);
17141 if (decl_specs)
17142 decl_specs->any_specifiers_p = true;
17144 /* Consume the token. */
17145 cp_lexer_consume_token (parser->lexer);
17147 if (type == error_mark_node)
17148 return error_mark_node;
17150 /* There is no valid C++ program where a non-template type is
17151 followed by a "<". That usually indicates that the user thought
17152 that the type was a template. */
17153 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17154 token->location);
17156 return TYPE_NAME (type);
17159 /* The type-specifier must be a user-defined type. */
17160 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17162 bool qualified_p;
17163 bool global_p;
17165 /* Don't gobble tokens or issue error messages if this is an
17166 optional type-specifier. */
17167 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17168 cp_parser_parse_tentatively (parser);
17170 token = cp_lexer_peek_token (parser->lexer);
17172 /* Look for the optional `::' operator. */
17173 global_p
17174 = (cp_parser_global_scope_opt (parser,
17175 /*current_scope_valid_p=*/false)
17176 != NULL_TREE);
17177 /* Look for the nested-name specifier. */
17178 qualified_p
17179 = (cp_parser_nested_name_specifier_opt (parser,
17180 /*typename_keyword_p=*/false,
17181 /*check_dependency_p=*/true,
17182 /*type_p=*/false,
17183 /*is_declaration=*/false)
17184 != NULL_TREE);
17185 /* If we have seen a nested-name-specifier, and the next token
17186 is `template', then we are using the template-id production. */
17187 if (parser->scope
17188 && cp_parser_optional_template_keyword (parser))
17190 /* Look for the template-id. */
17191 type = cp_parser_template_id (parser,
17192 /*template_keyword_p=*/true,
17193 /*check_dependency_p=*/true,
17194 none_type,
17195 /*is_declaration=*/false);
17196 /* If the template-id did not name a type, we are out of
17197 luck. */
17198 if (TREE_CODE (type) != TYPE_DECL)
17200 cp_parser_error (parser, "expected template-id for type");
17201 type = NULL_TREE;
17204 /* Otherwise, look for a type-name. */
17205 else
17206 type = cp_parser_type_name (parser);
17207 /* Keep track of all name-lookups performed in class scopes. */
17208 if (type
17209 && !global_p
17210 && !qualified_p
17211 && TREE_CODE (type) == TYPE_DECL
17212 && identifier_p (DECL_NAME (type)))
17213 maybe_note_name_used_in_class (DECL_NAME (type), type);
17214 /* If it didn't work out, we don't have a TYPE. */
17215 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17216 && !cp_parser_parse_definitely (parser))
17217 type = NULL_TREE;
17218 if (!type && cxx_dialect >= cxx17)
17220 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17221 cp_parser_parse_tentatively (parser);
17223 cp_parser_global_scope_opt (parser,
17224 /*current_scope_valid_p=*/false);
17225 cp_parser_nested_name_specifier_opt (parser,
17226 /*typename_keyword_p=*/false,
17227 /*check_dependency_p=*/true,
17228 /*type_p=*/false,
17229 /*is_declaration=*/false);
17230 tree name = cp_parser_identifier (parser);
17231 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17232 && parser->scope != error_mark_node)
17234 tree tmpl = cp_parser_lookup_name (parser, name,
17235 none_type,
17236 /*is_template=*/false,
17237 /*is_namespace=*/false,
17238 /*check_dependency=*/true,
17239 /*ambiguous_decls=*/NULL,
17240 token->location);
17241 if (tmpl && tmpl != error_mark_node
17242 && (DECL_CLASS_TEMPLATE_P (tmpl)
17243 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17244 type = make_template_placeholder (tmpl);
17245 else
17247 type = error_mark_node;
17248 if (!cp_parser_simulate_error (parser))
17249 cp_parser_name_lookup_error (parser, name, tmpl,
17250 NLE_TYPE, token->location);
17253 else
17254 type = error_mark_node;
17256 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17257 && !cp_parser_parse_definitely (parser))
17258 type = NULL_TREE;
17260 if (type && decl_specs)
17261 cp_parser_set_decl_spec_type (decl_specs, type,
17262 token,
17263 /*type_definition_p=*/false);
17266 /* If we didn't get a type-name, issue an error message. */
17267 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17269 cp_parser_error (parser, "expected type-name");
17270 return error_mark_node;
17273 if (type && type != error_mark_node)
17275 /* See if TYPE is an Objective-C type, and if so, parse and
17276 accept any protocol references following it. Do this before
17277 the cp_parser_check_for_invalid_template_id() call, because
17278 Objective-C types can be followed by '<...>' which would
17279 enclose protocol names rather than template arguments, and so
17280 everything is fine. */
17281 if (c_dialect_objc () && !parser->scope
17282 && (objc_is_id (type) || objc_is_class_name (type)))
17284 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17285 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17287 /* Clobber the "unqualified" type previously entered into
17288 DECL_SPECS with the new, improved protocol-qualified version. */
17289 if (decl_specs)
17290 decl_specs->type = qual_type;
17292 return qual_type;
17295 /* There is no valid C++ program where a non-template type is
17296 followed by a "<". That usually indicates that the user
17297 thought that the type was a template. */
17298 cp_parser_check_for_invalid_template_id (parser, type,
17299 none_type,
17300 token->location);
17303 return type;
17306 /* Parse a type-name.
17308 type-name:
17309 class-name
17310 enum-name
17311 typedef-name
17312 simple-template-id [in c++0x]
17314 enum-name:
17315 identifier
17317 typedef-name:
17318 identifier
17320 Concepts:
17322 type-name:
17323 concept-name
17324 partial-concept-id
17326 concept-name:
17327 identifier
17329 Returns a TYPE_DECL for the type. */
17331 static tree
17332 cp_parser_type_name (cp_parser* parser)
17334 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17337 /* See above. */
17338 static tree
17339 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17341 tree type_decl;
17343 /* We can't know yet whether it is a class-name or not. */
17344 cp_parser_parse_tentatively (parser);
17345 /* Try a class-name. */
17346 type_decl = cp_parser_class_name (parser,
17347 typename_keyword_p,
17348 /*template_keyword_p=*/false,
17349 none_type,
17350 /*check_dependency_p=*/true,
17351 /*class_head_p=*/false,
17352 /*is_declaration=*/false);
17353 /* If it's not a class-name, keep looking. */
17354 if (!cp_parser_parse_definitely (parser))
17356 if (cxx_dialect < cxx11)
17357 /* It must be a typedef-name or an enum-name. */
17358 return cp_parser_nonclass_name (parser);
17360 cp_parser_parse_tentatively (parser);
17361 /* It is either a simple-template-id representing an
17362 instantiation of an alias template... */
17363 type_decl = cp_parser_template_id (parser,
17364 /*template_keyword_p=*/false,
17365 /*check_dependency_p=*/true,
17366 none_type,
17367 /*is_declaration=*/false);
17368 /* Note that this must be an instantiation of an alias template
17369 because [temp.names]/6 says:
17371 A template-id that names an alias template specialization
17372 is a type-name.
17374 Whereas [temp.names]/7 says:
17376 A simple-template-id that names a class template
17377 specialization is a class-name.
17379 With concepts, this could also be a partial-concept-id that
17380 declares a non-type template parameter. */
17381 if (type_decl != NULL_TREE
17382 && TREE_CODE (type_decl) == TYPE_DECL
17383 && TYPE_DECL_ALIAS_P (type_decl))
17384 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17385 else if (is_constrained_parameter (type_decl))
17386 /* Don't do anything. */ ;
17387 else
17388 cp_parser_simulate_error (parser);
17390 if (!cp_parser_parse_definitely (parser))
17391 /* ... Or a typedef-name or an enum-name. */
17392 return cp_parser_nonclass_name (parser);
17395 return type_decl;
17398 /* Check if DECL and ARGS can form a constrained-type-specifier.
17399 If ARGS is non-null, we try to form a concept check of the
17400 form DECL<?, ARGS> where ? is a wildcard that matches any
17401 kind of template argument. If ARGS is NULL, then we try to
17402 form a concept check of the form DECL<?>. */
17404 static tree
17405 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17406 tree decl, tree args)
17408 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17410 /* If we a constrained-type-specifier cannot be deduced. */
17411 if (parser->prevent_constrained_type_specifiers)
17412 return NULL_TREE;
17414 /* A constrained type specifier can only be found in an
17415 overload set or as a reference to a template declaration.
17417 FIXME: This might be masking a bug. It's possible that
17418 that the deduction below is causing template specializations
17419 to be formed with the wildcard as an argument. */
17420 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17421 return NULL_TREE;
17423 /* Try to build a call expression that evaluates the
17424 concept. This can fail if the overload set refers
17425 only to non-templates. */
17426 tree placeholder = build_nt (WILDCARD_DECL);
17427 tree check = build_concept_check (decl, placeholder, args);
17428 if (check == error_mark_node)
17429 return NULL_TREE;
17431 /* Deduce the checked constraint and the prototype parameter.
17433 FIXME: In certain cases, failure to deduce should be a
17434 diagnosable error. */
17435 tree conc;
17436 tree proto;
17437 if (!deduce_constrained_parameter (check, conc, proto))
17438 return NULL_TREE;
17440 /* In template parameter scope, this results in a constrained
17441 parameter. Return a descriptor of that parm. */
17442 if (processing_template_parmlist)
17443 return build_constrained_parameter (conc, proto, args);
17445 /* In a parameter-declaration-clause, constrained-type
17446 specifiers result in invented template parameters. */
17447 if (parser->auto_is_implicit_function_template_parm_p)
17449 tree x = build_constrained_parameter (conc, proto, args);
17450 return synthesize_implicit_template_parm (parser, x);
17452 else
17454 /* Otherwise, we're in a context where the constrained
17455 type name is deduced and the constraint applies
17456 after deduction. */
17457 return make_constrained_auto (conc, args);
17460 return NULL_TREE;
17463 /* If DECL refers to a concept, return a TYPE_DECL representing
17464 the result of using the constrained type specifier in the
17465 current context. DECL refers to a concept if
17467 - it is an overload set containing a function concept taking a single
17468 type argument, or
17470 - it is a variable concept taking a single type argument. */
17472 static tree
17473 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17475 if (flag_concepts
17476 && (TREE_CODE (decl) == OVERLOAD
17477 || BASELINK_P (decl)
17478 || variable_concept_p (decl)))
17479 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17480 else
17481 return NULL_TREE;
17484 /* Check if DECL and ARGS form a partial-concept-id. If so,
17485 assign ID to the resulting constrained placeholder.
17487 Returns true if the partial-concept-id designates a placeholder
17488 and false otherwise. Note that *id is set to NULL_TREE in
17489 this case. */
17491 static tree
17492 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17494 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17497 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17498 or a concept-name.
17500 enum-name:
17501 identifier
17503 typedef-name:
17504 identifier
17506 concept-name:
17507 identifier
17509 Returns a TYPE_DECL for the type. */
17511 static tree
17512 cp_parser_nonclass_name (cp_parser* parser)
17514 tree type_decl;
17515 tree identifier;
17517 cp_token *token = cp_lexer_peek_token (parser->lexer);
17518 identifier = cp_parser_identifier (parser);
17519 if (identifier == error_mark_node)
17520 return error_mark_node;
17522 /* Look up the type-name. */
17523 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17525 type_decl = strip_using_decl (type_decl);
17527 /* If we found an overload set, then it may refer to a concept-name. */
17528 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17529 type_decl = decl;
17531 if (TREE_CODE (type_decl) != TYPE_DECL
17532 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17534 /* See if this is an Objective-C type. */
17535 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17536 tree type = objc_get_protocol_qualified_type (identifier, protos);
17537 if (type)
17538 type_decl = TYPE_NAME (type);
17541 /* Issue an error if we did not find a type-name. */
17542 if (TREE_CODE (type_decl) != TYPE_DECL
17543 /* In Objective-C, we have the complication that class names are
17544 normally type names and start declarations (eg, the
17545 "NSObject" in "NSObject *object;"), but can be used in an
17546 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17547 is an expression. So, a classname followed by a dot is not a
17548 valid type-name. */
17549 || (objc_is_class_name (TREE_TYPE (type_decl))
17550 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17552 if (!cp_parser_simulate_error (parser))
17553 cp_parser_name_lookup_error (parser, identifier, type_decl,
17554 NLE_TYPE, token->location);
17555 return error_mark_node;
17557 /* Remember that the name was used in the definition of the
17558 current class so that we can check later to see if the
17559 meaning would have been different after the class was
17560 entirely defined. */
17561 else if (type_decl != error_mark_node
17562 && !parser->scope)
17563 maybe_note_name_used_in_class (identifier, type_decl);
17565 return type_decl;
17568 /* Parse an elaborated-type-specifier. Note that the grammar given
17569 here incorporates the resolution to DR68.
17571 elaborated-type-specifier:
17572 class-key :: [opt] nested-name-specifier [opt] identifier
17573 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17574 enum-key :: [opt] nested-name-specifier [opt] identifier
17575 typename :: [opt] nested-name-specifier identifier
17576 typename :: [opt] nested-name-specifier template [opt]
17577 template-id
17579 GNU extension:
17581 elaborated-type-specifier:
17582 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17583 class-key attributes :: [opt] nested-name-specifier [opt]
17584 template [opt] template-id
17585 enum attributes :: [opt] nested-name-specifier [opt] identifier
17587 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17588 declared `friend'. If IS_DECLARATION is TRUE, then this
17589 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17590 something is being declared.
17592 Returns the TYPE specified. */
17594 static tree
17595 cp_parser_elaborated_type_specifier (cp_parser* parser,
17596 bool is_friend,
17597 bool is_declaration)
17599 enum tag_types tag_type;
17600 tree identifier;
17601 tree type = NULL_TREE;
17602 tree attributes = NULL_TREE;
17603 tree globalscope;
17604 cp_token *token = NULL;
17606 /* See if we're looking at the `enum' keyword. */
17607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17609 /* Consume the `enum' token. */
17610 cp_lexer_consume_token (parser->lexer);
17611 /* Remember that it's an enumeration type. */
17612 tag_type = enum_type;
17613 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17614 enums) is used here. */
17615 cp_token *token = cp_lexer_peek_token (parser->lexer);
17616 if (cp_parser_is_keyword (token, RID_CLASS)
17617 || cp_parser_is_keyword (token, RID_STRUCT))
17619 gcc_rich_location richloc (token->location);
17620 richloc.add_range (input_location, false);
17621 richloc.add_fixit_remove ();
17622 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17623 "a scoped enum must not use the %qD keyword",
17624 token->u.value);
17625 /* Consume the `struct' or `class' and parse it anyway. */
17626 cp_lexer_consume_token (parser->lexer);
17628 /* Parse the attributes. */
17629 attributes = cp_parser_attributes_opt (parser);
17631 /* Or, it might be `typename'. */
17632 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17633 RID_TYPENAME))
17635 /* Consume the `typename' token. */
17636 cp_lexer_consume_token (parser->lexer);
17637 /* Remember that it's a `typename' type. */
17638 tag_type = typename_type;
17640 /* Otherwise it must be a class-key. */
17641 else
17643 tag_type = cp_parser_class_key (parser);
17644 if (tag_type == none_type)
17645 return error_mark_node;
17646 /* Parse the attributes. */
17647 attributes = cp_parser_attributes_opt (parser);
17650 /* Look for the `::' operator. */
17651 globalscope = cp_parser_global_scope_opt (parser,
17652 /*current_scope_valid_p=*/false);
17653 /* Look for the nested-name-specifier. */
17654 tree nested_name_specifier;
17655 if (tag_type == typename_type && !globalscope)
17657 nested_name_specifier
17658 = cp_parser_nested_name_specifier (parser,
17659 /*typename_keyword_p=*/true,
17660 /*check_dependency_p=*/true,
17661 /*type_p=*/true,
17662 is_declaration);
17663 if (!nested_name_specifier)
17664 return error_mark_node;
17666 else
17667 /* Even though `typename' is not present, the proposed resolution
17668 to Core Issue 180 says that in `class A<T>::B', `B' should be
17669 considered a type-name, even if `A<T>' is dependent. */
17670 nested_name_specifier
17671 = cp_parser_nested_name_specifier_opt (parser,
17672 /*typename_keyword_p=*/true,
17673 /*check_dependency_p=*/true,
17674 /*type_p=*/true,
17675 is_declaration);
17676 /* For everything but enumeration types, consider a template-id.
17677 For an enumeration type, consider only a plain identifier. */
17678 if (tag_type != enum_type)
17680 bool template_p = false;
17681 tree decl;
17683 /* Allow the `template' keyword. */
17684 template_p = cp_parser_optional_template_keyword (parser);
17685 /* If we didn't see `template', we don't know if there's a
17686 template-id or not. */
17687 if (!template_p)
17688 cp_parser_parse_tentatively (parser);
17689 /* Parse the template-id. */
17690 token = cp_lexer_peek_token (parser->lexer);
17691 decl = cp_parser_template_id (parser, template_p,
17692 /*check_dependency_p=*/true,
17693 tag_type,
17694 is_declaration);
17695 /* If we didn't find a template-id, look for an ordinary
17696 identifier. */
17697 if (!template_p && !cp_parser_parse_definitely (parser))
17699 /* We can get here when cp_parser_template_id, called by
17700 cp_parser_class_name with tag_type == none_type, succeeds
17701 and caches a BASELINK. Then, when called again here,
17702 instead of failing and returning an error_mark_node
17703 returns it (see template/typename17.C in C++11).
17704 ??? Could we diagnose this earlier? */
17705 else if (tag_type == typename_type && BASELINK_P (decl))
17707 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17708 type = error_mark_node;
17710 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17711 in effect, then we must assume that, upon instantiation, the
17712 template will correspond to a class. */
17713 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17714 && tag_type == typename_type)
17715 type = make_typename_type (parser->scope, decl,
17716 typename_type,
17717 /*complain=*/tf_error);
17718 /* If the `typename' keyword is in effect and DECL is not a type
17719 decl, then type is non existent. */
17720 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17722 else if (TREE_CODE (decl) == TYPE_DECL)
17724 type = check_elaborated_type_specifier (tag_type, decl,
17725 /*allow_template_p=*/true);
17727 /* If the next token is a semicolon, this must be a specialization,
17728 instantiation, or friend declaration. Check the scope while we
17729 still know whether or not we had a nested-name-specifier. */
17730 if (type != error_mark_node
17731 && !nested_name_specifier && !is_friend
17732 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17733 check_unqualified_spec_or_inst (type, token->location);
17735 else if (decl == error_mark_node)
17736 type = error_mark_node;
17739 if (!type)
17741 token = cp_lexer_peek_token (parser->lexer);
17742 identifier = cp_parser_identifier (parser);
17744 if (identifier == error_mark_node)
17746 parser->scope = NULL_TREE;
17747 return error_mark_node;
17750 /* For a `typename', we needn't call xref_tag. */
17751 if (tag_type == typename_type
17752 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17753 return cp_parser_make_typename_type (parser, identifier,
17754 token->location);
17756 /* Template parameter lists apply only if we are not within a
17757 function parameter list. */
17758 bool template_parm_lists_apply
17759 = parser->num_template_parameter_lists;
17760 if (template_parm_lists_apply)
17761 for (cp_binding_level *s = current_binding_level;
17762 s && s->kind != sk_template_parms;
17763 s = s->level_chain)
17764 if (s->kind == sk_function_parms)
17765 template_parm_lists_apply = false;
17767 /* Look up a qualified name in the usual way. */
17768 if (parser->scope)
17770 tree decl;
17771 tree ambiguous_decls;
17773 decl = cp_parser_lookup_name (parser, identifier,
17774 tag_type,
17775 /*is_template=*/false,
17776 /*is_namespace=*/false,
17777 /*check_dependency=*/true,
17778 &ambiguous_decls,
17779 token->location);
17781 /* If the lookup was ambiguous, an error will already have been
17782 issued. */
17783 if (ambiguous_decls)
17784 return error_mark_node;
17786 /* If we are parsing friend declaration, DECL may be a
17787 TEMPLATE_DECL tree node here. However, we need to check
17788 whether this TEMPLATE_DECL results in valid code. Consider
17789 the following example:
17791 namespace N {
17792 template <class T> class C {};
17794 class X {
17795 template <class T> friend class N::C; // #1, valid code
17797 template <class T> class Y {
17798 friend class N::C; // #2, invalid code
17801 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17802 name lookup of `N::C'. We see that friend declaration must
17803 be template for the code to be valid. Note that
17804 processing_template_decl does not work here since it is
17805 always 1 for the above two cases. */
17807 decl = (cp_parser_maybe_treat_template_as_class
17808 (decl, /*tag_name_p=*/is_friend
17809 && template_parm_lists_apply));
17811 if (TREE_CODE (decl) != TYPE_DECL)
17813 cp_parser_diagnose_invalid_type_name (parser,
17814 identifier,
17815 token->location);
17816 return error_mark_node;
17819 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17821 bool allow_template = (template_parm_lists_apply
17822 || DECL_SELF_REFERENCE_P (decl));
17823 type = check_elaborated_type_specifier (tag_type, decl,
17824 allow_template);
17826 if (type == error_mark_node)
17827 return error_mark_node;
17830 /* Forward declarations of nested types, such as
17832 class C1::C2;
17833 class C1::C2::C3;
17835 are invalid unless all components preceding the final '::'
17836 are complete. If all enclosing types are complete, these
17837 declarations become merely pointless.
17839 Invalid forward declarations of nested types are errors
17840 caught elsewhere in parsing. Those that are pointless arrive
17841 here. */
17843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17844 && !is_friend && !processing_explicit_instantiation)
17845 warning (0, "declaration %qD does not declare anything", decl);
17847 type = TREE_TYPE (decl);
17849 else
17851 /* An elaborated-type-specifier sometimes introduces a new type and
17852 sometimes names an existing type. Normally, the rule is that it
17853 introduces a new type only if there is not an existing type of
17854 the same name already in scope. For example, given:
17856 struct S {};
17857 void f() { struct S s; }
17859 the `struct S' in the body of `f' is the same `struct S' as in
17860 the global scope; the existing definition is used. However, if
17861 there were no global declaration, this would introduce a new
17862 local class named `S'.
17864 An exception to this rule applies to the following code:
17866 namespace N { struct S; }
17868 Here, the elaborated-type-specifier names a new type
17869 unconditionally; even if there is already an `S' in the
17870 containing scope this declaration names a new type.
17871 This exception only applies if the elaborated-type-specifier
17872 forms the complete declaration:
17874 [class.name]
17876 A declaration consisting solely of `class-key identifier ;' is
17877 either a redeclaration of the name in the current scope or a
17878 forward declaration of the identifier as a class name. It
17879 introduces the name into the current scope.
17881 We are in this situation precisely when the next token is a `;'.
17883 An exception to the exception is that a `friend' declaration does
17884 *not* name a new type; i.e., given:
17886 struct S { friend struct T; };
17888 `T' is not a new type in the scope of `S'.
17890 Also, `new struct S' or `sizeof (struct S)' never results in the
17891 definition of a new type; a new type can only be declared in a
17892 declaration context. */
17894 tag_scope ts;
17895 bool template_p;
17897 if (is_friend)
17898 /* Friends have special name lookup rules. */
17899 ts = ts_within_enclosing_non_class;
17900 else if (is_declaration
17901 && cp_lexer_next_token_is (parser->lexer,
17902 CPP_SEMICOLON))
17903 /* This is a `class-key identifier ;' */
17904 ts = ts_current;
17905 else
17906 ts = ts_global;
17908 template_p =
17909 (template_parm_lists_apply
17910 && (cp_parser_next_token_starts_class_definition_p (parser)
17911 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17912 /* An unqualified name was used to reference this type, so
17913 there were no qualifying templates. */
17914 if (template_parm_lists_apply
17915 && !cp_parser_check_template_parameters (parser,
17916 /*num_templates=*/0,
17917 token->location,
17918 /*declarator=*/NULL))
17919 return error_mark_node;
17920 type = xref_tag (tag_type, identifier, ts, template_p);
17924 if (type == error_mark_node)
17925 return error_mark_node;
17927 /* Allow attributes on forward declarations of classes. */
17928 if (attributes)
17930 if (TREE_CODE (type) == TYPENAME_TYPE)
17931 warning (OPT_Wattributes,
17932 "attributes ignored on uninstantiated type");
17933 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17934 && ! processing_explicit_instantiation)
17935 warning (OPT_Wattributes,
17936 "attributes ignored on template instantiation");
17937 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17938 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17939 else
17940 warning (OPT_Wattributes,
17941 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17944 if (tag_type != enum_type)
17946 /* Indicate whether this class was declared as a `class' or as a
17947 `struct'. */
17948 if (CLASS_TYPE_P (type))
17949 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17950 cp_parser_check_class_key (tag_type, type);
17953 /* A "<" cannot follow an elaborated type specifier. If that
17954 happens, the user was probably trying to form a template-id. */
17955 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17956 token->location);
17958 return type;
17961 /* Parse an enum-specifier.
17963 enum-specifier:
17964 enum-head { enumerator-list [opt] }
17965 enum-head { enumerator-list , } [C++0x]
17967 enum-head:
17968 enum-key identifier [opt] enum-base [opt]
17969 enum-key nested-name-specifier identifier enum-base [opt]
17971 enum-key:
17972 enum
17973 enum class [C++0x]
17974 enum struct [C++0x]
17976 enum-base: [C++0x]
17977 : type-specifier-seq
17979 opaque-enum-specifier:
17980 enum-key identifier enum-base [opt] ;
17982 GNU Extensions:
17983 enum-key attributes[opt] identifier [opt] enum-base [opt]
17984 { enumerator-list [opt] }attributes[opt]
17985 enum-key attributes[opt] identifier [opt] enum-base [opt]
17986 { enumerator-list, }attributes[opt] [C++0x]
17988 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17989 if the token stream isn't an enum-specifier after all. */
17991 static tree
17992 cp_parser_enum_specifier (cp_parser* parser)
17994 tree identifier;
17995 tree type = NULL_TREE;
17996 tree prev_scope;
17997 tree nested_name_specifier = NULL_TREE;
17998 tree attributes;
17999 bool scoped_enum_p = false;
18000 bool has_underlying_type = false;
18001 bool nested_being_defined = false;
18002 bool new_value_list = false;
18003 bool is_new_type = false;
18004 bool is_unnamed = false;
18005 tree underlying_type = NULL_TREE;
18006 cp_token *type_start_token = NULL;
18007 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18009 parser->colon_corrects_to_scope_p = false;
18011 /* Parse tentatively so that we can back up if we don't find a
18012 enum-specifier. */
18013 cp_parser_parse_tentatively (parser);
18015 /* Caller guarantees that the current token is 'enum', an identifier
18016 possibly follows, and the token after that is an opening brace.
18017 If we don't have an identifier, fabricate an anonymous name for
18018 the enumeration being defined. */
18019 cp_lexer_consume_token (parser->lexer);
18021 /* Parse the "class" or "struct", which indicates a scoped
18022 enumeration type in C++0x. */
18023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18024 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18026 if (cxx_dialect < cxx11)
18027 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18029 /* Consume the `struct' or `class' token. */
18030 cp_lexer_consume_token (parser->lexer);
18032 scoped_enum_p = true;
18035 attributes = cp_parser_attributes_opt (parser);
18037 /* Clear the qualification. */
18038 parser->scope = NULL_TREE;
18039 parser->qualifying_scope = NULL_TREE;
18040 parser->object_scope = NULL_TREE;
18042 /* Figure out in what scope the declaration is being placed. */
18043 prev_scope = current_scope ();
18045 type_start_token = cp_lexer_peek_token (parser->lexer);
18047 push_deferring_access_checks (dk_no_check);
18048 nested_name_specifier
18049 = cp_parser_nested_name_specifier_opt (parser,
18050 /*typename_keyword_p=*/true,
18051 /*check_dependency_p=*/false,
18052 /*type_p=*/false,
18053 /*is_declaration=*/false);
18055 if (nested_name_specifier)
18057 tree name;
18059 identifier = cp_parser_identifier (parser);
18060 name = cp_parser_lookup_name (parser, identifier,
18061 enum_type,
18062 /*is_template=*/false,
18063 /*is_namespace=*/false,
18064 /*check_dependency=*/true,
18065 /*ambiguous_decls=*/NULL,
18066 input_location);
18067 if (name && name != error_mark_node)
18069 type = TREE_TYPE (name);
18070 if (TREE_CODE (type) == TYPENAME_TYPE)
18072 /* Are template enums allowed in ISO? */
18073 if (template_parm_scope_p ())
18074 pedwarn (type_start_token->location, OPT_Wpedantic,
18075 "%qD is an enumeration template", name);
18076 /* ignore a typename reference, for it will be solved by name
18077 in start_enum. */
18078 type = NULL_TREE;
18081 else if (nested_name_specifier == error_mark_node)
18082 /* We already issued an error. */;
18083 else
18085 error_at (type_start_token->location,
18086 "%qD does not name an enumeration in %qT",
18087 identifier, nested_name_specifier);
18088 nested_name_specifier = error_mark_node;
18091 else
18093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18094 identifier = cp_parser_identifier (parser);
18095 else
18097 identifier = make_anon_name ();
18098 is_unnamed = true;
18099 if (scoped_enum_p)
18100 error_at (type_start_token->location,
18101 "unnamed scoped enum is not allowed");
18104 pop_deferring_access_checks ();
18106 /* Check for the `:' that denotes a specified underlying type in C++0x.
18107 Note that a ':' could also indicate a bitfield width, however. */
18108 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18110 cp_decl_specifier_seq type_specifiers;
18112 /* Consume the `:'. */
18113 cp_lexer_consume_token (parser->lexer);
18115 /* Parse the type-specifier-seq. */
18116 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18117 /*is_trailing_return=*/false,
18118 &type_specifiers);
18120 /* At this point this is surely not elaborated type specifier. */
18121 if (!cp_parser_parse_definitely (parser))
18122 return NULL_TREE;
18124 if (cxx_dialect < cxx11)
18125 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18127 has_underlying_type = true;
18129 /* If that didn't work, stop. */
18130 if (type_specifiers.type != error_mark_node)
18132 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18133 /*initialized=*/0, NULL);
18134 if (underlying_type == error_mark_node
18135 || check_for_bare_parameter_packs (underlying_type))
18136 underlying_type = NULL_TREE;
18140 /* Look for the `{' but don't consume it yet. */
18141 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18143 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18145 cp_parser_error (parser, "expected %<{%>");
18146 if (has_underlying_type)
18148 type = NULL_TREE;
18149 goto out;
18152 /* An opaque-enum-specifier must have a ';' here. */
18153 if ((scoped_enum_p || underlying_type)
18154 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18156 cp_parser_error (parser, "expected %<;%> or %<{%>");
18157 if (has_underlying_type)
18159 type = NULL_TREE;
18160 goto out;
18165 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18166 return NULL_TREE;
18168 if (nested_name_specifier)
18170 if (CLASS_TYPE_P (nested_name_specifier))
18172 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18173 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18174 push_scope (nested_name_specifier);
18176 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18178 push_nested_namespace (nested_name_specifier);
18182 /* Issue an error message if type-definitions are forbidden here. */
18183 if (!cp_parser_check_type_definition (parser))
18184 type = error_mark_node;
18185 else
18186 /* Create the new type. We do this before consuming the opening
18187 brace so the enum will be recorded as being on the line of its
18188 tag (or the 'enum' keyword, if there is no tag). */
18189 type = start_enum (identifier, type, underlying_type,
18190 attributes, scoped_enum_p, &is_new_type);
18192 /* If the next token is not '{' it is an opaque-enum-specifier or an
18193 elaborated-type-specifier. */
18194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18196 timevar_push (TV_PARSE_ENUM);
18197 if (nested_name_specifier
18198 && nested_name_specifier != error_mark_node)
18200 /* The following catches invalid code such as:
18201 enum class S<int>::E { A, B, C }; */
18202 if (!processing_specialization
18203 && CLASS_TYPE_P (nested_name_specifier)
18204 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18205 error_at (type_start_token->location, "cannot add an enumerator "
18206 "list to a template instantiation");
18208 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18210 error_at (type_start_token->location,
18211 "%<%T::%E%> has not been declared",
18212 TYPE_CONTEXT (nested_name_specifier),
18213 nested_name_specifier);
18214 type = error_mark_node;
18216 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18217 && !CLASS_TYPE_P (nested_name_specifier))
18219 error_at (type_start_token->location, "nested name specifier "
18220 "%qT for enum declaration does not name a class "
18221 "or namespace", nested_name_specifier);
18222 type = error_mark_node;
18224 /* If that scope does not contain the scope in which the
18225 class was originally declared, the program is invalid. */
18226 else if (prev_scope && !is_ancestor (prev_scope,
18227 nested_name_specifier))
18229 if (at_namespace_scope_p ())
18230 error_at (type_start_token->location,
18231 "declaration of %qD in namespace %qD which does not "
18232 "enclose %qD",
18233 type, prev_scope, nested_name_specifier);
18234 else
18235 error_at (type_start_token->location,
18236 "declaration of %qD in %qD which does not "
18237 "enclose %qD",
18238 type, prev_scope, nested_name_specifier);
18239 type = error_mark_node;
18241 /* If that scope is the scope where the declaration is being placed
18242 the program is invalid. */
18243 else if (CLASS_TYPE_P (nested_name_specifier)
18244 && CLASS_TYPE_P (prev_scope)
18245 && same_type_p (nested_name_specifier, prev_scope))
18247 permerror (type_start_token->location,
18248 "extra qualification not allowed");
18249 nested_name_specifier = NULL_TREE;
18253 if (scoped_enum_p)
18254 begin_scope (sk_scoped_enum, type);
18256 /* Consume the opening brace. */
18257 matching_braces braces;
18258 braces.consume_open (parser);
18260 if (type == error_mark_node)
18261 ; /* Nothing to add */
18262 else if (OPAQUE_ENUM_P (type)
18263 || (cxx_dialect > cxx98 && processing_specialization))
18265 new_value_list = true;
18266 SET_OPAQUE_ENUM_P (type, false);
18267 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18269 else
18271 error_at (type_start_token->location,
18272 "multiple definition of %q#T", type);
18273 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18274 "previous definition here");
18275 type = error_mark_node;
18278 if (type == error_mark_node)
18279 cp_parser_skip_to_end_of_block_or_statement (parser);
18280 /* If the next token is not '}', then there are some enumerators. */
18281 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18283 if (is_unnamed && !scoped_enum_p)
18284 pedwarn (type_start_token->location, OPT_Wpedantic,
18285 "ISO C++ forbids empty unnamed enum");
18287 else
18288 cp_parser_enumerator_list (parser, type);
18290 /* Consume the final '}'. */
18291 braces.require_close (parser);
18293 if (scoped_enum_p)
18294 finish_scope ();
18295 timevar_pop (TV_PARSE_ENUM);
18297 else
18299 /* If a ';' follows, then it is an opaque-enum-specifier
18300 and additional restrictions apply. */
18301 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18303 if (is_unnamed)
18304 error_at (type_start_token->location,
18305 "opaque-enum-specifier without name");
18306 else if (nested_name_specifier)
18307 error_at (type_start_token->location,
18308 "opaque-enum-specifier must use a simple identifier");
18312 /* Look for trailing attributes to apply to this enumeration, and
18313 apply them if appropriate. */
18314 if (cp_parser_allow_gnu_extensions_p (parser))
18316 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18317 cplus_decl_attributes (&type,
18318 trailing_attr,
18319 (int) ATTR_FLAG_TYPE_IN_PLACE);
18322 /* Finish up the enumeration. */
18323 if (type != error_mark_node)
18325 if (new_value_list)
18326 finish_enum_value_list (type);
18327 if (is_new_type)
18328 finish_enum (type);
18331 if (nested_name_specifier)
18333 if (CLASS_TYPE_P (nested_name_specifier))
18335 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18336 pop_scope (nested_name_specifier);
18338 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18340 pop_nested_namespace (nested_name_specifier);
18343 out:
18344 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18345 return type;
18348 /* Parse an enumerator-list. The enumerators all have the indicated
18349 TYPE.
18351 enumerator-list:
18352 enumerator-definition
18353 enumerator-list , enumerator-definition */
18355 static void
18356 cp_parser_enumerator_list (cp_parser* parser, tree type)
18358 while (true)
18360 /* Parse an enumerator-definition. */
18361 cp_parser_enumerator_definition (parser, type);
18363 /* If the next token is not a ',', we've reached the end of
18364 the list. */
18365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18366 break;
18367 /* Otherwise, consume the `,' and keep going. */
18368 cp_lexer_consume_token (parser->lexer);
18369 /* If the next token is a `}', there is a trailing comma. */
18370 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18372 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18373 pedwarn (input_location, OPT_Wpedantic,
18374 "comma at end of enumerator list");
18375 break;
18380 /* Parse an enumerator-definition. The enumerator has the indicated
18381 TYPE.
18383 enumerator-definition:
18384 enumerator
18385 enumerator = constant-expression
18387 enumerator:
18388 identifier
18390 GNU Extensions:
18392 enumerator-definition:
18393 enumerator attributes [opt]
18394 enumerator attributes [opt] = constant-expression */
18396 static void
18397 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18399 tree identifier;
18400 tree value;
18401 location_t loc;
18403 /* Save the input location because we are interested in the location
18404 of the identifier and not the location of the explicit value. */
18405 loc = cp_lexer_peek_token (parser->lexer)->location;
18407 /* Look for the identifier. */
18408 identifier = cp_parser_identifier (parser);
18409 if (identifier == error_mark_node)
18410 return;
18412 /* Parse any specified attributes. */
18413 tree attrs = cp_parser_attributes_opt (parser);
18415 /* If the next token is an '=', then there is an explicit value. */
18416 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18418 /* Consume the `=' token. */
18419 cp_lexer_consume_token (parser->lexer);
18420 /* Parse the value. */
18421 value = cp_parser_constant_expression (parser);
18423 else
18424 value = NULL_TREE;
18426 /* If we are processing a template, make sure the initializer of the
18427 enumerator doesn't contain any bare template parameter pack. */
18428 if (check_for_bare_parameter_packs (value))
18429 value = error_mark_node;
18431 /* Create the enumerator. */
18432 build_enumerator (identifier, value, type, attrs, loc);
18435 /* Parse a namespace-name.
18437 namespace-name:
18438 original-namespace-name
18439 namespace-alias
18441 Returns the NAMESPACE_DECL for the namespace. */
18443 static tree
18444 cp_parser_namespace_name (cp_parser* parser)
18446 tree identifier;
18447 tree namespace_decl;
18449 cp_token *token = cp_lexer_peek_token (parser->lexer);
18451 /* Get the name of the namespace. */
18452 identifier = cp_parser_identifier (parser);
18453 if (identifier == error_mark_node)
18454 return error_mark_node;
18456 /* Look up the identifier in the currently active scope. Look only
18457 for namespaces, due to:
18459 [basic.lookup.udir]
18461 When looking up a namespace-name in a using-directive or alias
18462 definition, only namespace names are considered.
18464 And:
18466 [basic.lookup.qual]
18468 During the lookup of a name preceding the :: scope resolution
18469 operator, object, function, and enumerator names are ignored.
18471 (Note that cp_parser_qualifying_entity only calls this
18472 function if the token after the name is the scope resolution
18473 operator.) */
18474 namespace_decl = cp_parser_lookup_name (parser, identifier,
18475 none_type,
18476 /*is_template=*/false,
18477 /*is_namespace=*/true,
18478 /*check_dependency=*/true,
18479 /*ambiguous_decls=*/NULL,
18480 token->location);
18481 /* If it's not a namespace, issue an error. */
18482 if (namespace_decl == error_mark_node
18483 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18485 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18487 error_at (token->location, "%qD is not a namespace-name", identifier);
18488 if (namespace_decl == error_mark_node
18489 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18490 suggest_alternative_in_explicit_scope (token->location, identifier,
18491 parser->scope);
18493 cp_parser_error (parser, "expected namespace-name");
18494 namespace_decl = error_mark_node;
18497 return namespace_decl;
18500 /* Parse a namespace-definition.
18502 namespace-definition:
18503 named-namespace-definition
18504 unnamed-namespace-definition
18506 named-namespace-definition:
18507 original-namespace-definition
18508 extension-namespace-definition
18510 original-namespace-definition:
18511 namespace identifier { namespace-body }
18513 extension-namespace-definition:
18514 namespace original-namespace-name { namespace-body }
18516 unnamed-namespace-definition:
18517 namespace { namespace-body } */
18519 static void
18520 cp_parser_namespace_definition (cp_parser* parser)
18522 tree identifier;
18523 int nested_definition_count = 0;
18525 cp_ensure_no_omp_declare_simd (parser);
18526 cp_ensure_no_oacc_routine (parser);
18528 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18530 if (is_inline)
18532 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18533 cp_lexer_consume_token (parser->lexer);
18536 /* Look for the `namespace' keyword. */
18537 cp_token* token
18538 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18540 /* Parse any specified attributes before the identifier. */
18541 tree attribs = cp_parser_attributes_opt (parser);
18543 for (;;)
18545 identifier = NULL_TREE;
18547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18549 identifier = cp_parser_identifier (parser);
18551 /* Parse any attributes specified after the identifier. */
18552 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18555 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18556 break;
18558 if (!nested_definition_count && cxx_dialect < cxx17)
18559 pedwarn (input_location, OPT_Wpedantic,
18560 "nested namespace definitions only available with "
18561 "-std=c++17 or -std=gnu++17");
18563 /* Nested namespace names can create new namespaces (unlike
18564 other qualified-ids). */
18565 if (int count = identifier ? push_namespace (identifier) : 0)
18566 nested_definition_count += count;
18567 else
18568 cp_parser_error (parser, "nested namespace name required");
18569 cp_lexer_consume_token (parser->lexer);
18572 if (nested_definition_count && !identifier)
18573 cp_parser_error (parser, "namespace name required");
18575 if (nested_definition_count && attribs)
18576 error_at (token->location,
18577 "a nested namespace definition cannot have attributes");
18578 if (nested_definition_count && is_inline)
18579 error_at (token->location,
18580 "a nested namespace definition cannot be inline");
18582 /* Start the namespace. */
18583 nested_definition_count += push_namespace (identifier, is_inline);
18585 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18587 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18589 /* Look for the `{' to validate starting the namespace. */
18590 matching_braces braces;
18591 if (braces.require_open (parser))
18593 /* Parse the body of the namespace. */
18594 cp_parser_namespace_body (parser);
18596 /* Look for the final `}'. */
18597 braces.require_close (parser);
18600 if (has_visibility)
18601 pop_visibility (1);
18603 /* Pop the nested namespace definitions. */
18604 while (nested_definition_count--)
18605 pop_namespace ();
18608 /* Parse a namespace-body.
18610 namespace-body:
18611 declaration-seq [opt] */
18613 static void
18614 cp_parser_namespace_body (cp_parser* parser)
18616 cp_parser_declaration_seq_opt (parser);
18619 /* Parse a namespace-alias-definition.
18621 namespace-alias-definition:
18622 namespace identifier = qualified-namespace-specifier ; */
18624 static void
18625 cp_parser_namespace_alias_definition (cp_parser* parser)
18627 tree identifier;
18628 tree namespace_specifier;
18630 cp_token *token = cp_lexer_peek_token (parser->lexer);
18632 /* Look for the `namespace' keyword. */
18633 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18634 /* Look for the identifier. */
18635 identifier = cp_parser_identifier (parser);
18636 if (identifier == error_mark_node)
18637 return;
18638 /* Look for the `=' token. */
18639 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18640 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18642 error_at (token->location, "%<namespace%> definition is not allowed here");
18643 /* Skip the definition. */
18644 cp_lexer_consume_token (parser->lexer);
18645 if (cp_parser_skip_to_closing_brace (parser))
18646 cp_lexer_consume_token (parser->lexer);
18647 return;
18649 cp_parser_require (parser, CPP_EQ, RT_EQ);
18650 /* Look for the qualified-namespace-specifier. */
18651 namespace_specifier
18652 = cp_parser_qualified_namespace_specifier (parser);
18653 /* Look for the `;' token. */
18654 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18656 /* Register the alias in the symbol table. */
18657 do_namespace_alias (identifier, namespace_specifier);
18660 /* Parse a qualified-namespace-specifier.
18662 qualified-namespace-specifier:
18663 :: [opt] nested-name-specifier [opt] namespace-name
18665 Returns a NAMESPACE_DECL corresponding to the specified
18666 namespace. */
18668 static tree
18669 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18671 /* Look for the optional `::'. */
18672 cp_parser_global_scope_opt (parser,
18673 /*current_scope_valid_p=*/false);
18675 /* Look for the optional nested-name-specifier. */
18676 cp_parser_nested_name_specifier_opt (parser,
18677 /*typename_keyword_p=*/false,
18678 /*check_dependency_p=*/true,
18679 /*type_p=*/false,
18680 /*is_declaration=*/true);
18682 return cp_parser_namespace_name (parser);
18685 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18686 access declaration.
18688 using-declaration:
18689 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18690 using :: unqualified-id ;
18692 access-declaration:
18693 qualified-id ;
18697 static bool
18698 cp_parser_using_declaration (cp_parser* parser,
18699 bool access_declaration_p)
18701 cp_token *token;
18702 bool typename_p = false;
18703 bool global_scope_p;
18704 tree decl;
18705 tree identifier;
18706 tree qscope;
18707 int oldcount = errorcount;
18708 cp_token *diag_token = NULL;
18710 if (access_declaration_p)
18712 diag_token = cp_lexer_peek_token (parser->lexer);
18713 cp_parser_parse_tentatively (parser);
18715 else
18717 /* Look for the `using' keyword. */
18718 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18720 again:
18721 /* Peek at the next token. */
18722 token = cp_lexer_peek_token (parser->lexer);
18723 /* See if it's `typename'. */
18724 if (token->keyword == RID_TYPENAME)
18726 /* Remember that we've seen it. */
18727 typename_p = true;
18728 /* Consume the `typename' token. */
18729 cp_lexer_consume_token (parser->lexer);
18733 /* Look for the optional global scope qualification. */
18734 global_scope_p
18735 = (cp_parser_global_scope_opt (parser,
18736 /*current_scope_valid_p=*/false)
18737 != NULL_TREE);
18739 /* If we saw `typename', or didn't see `::', then there must be a
18740 nested-name-specifier present. */
18741 if (typename_p || !global_scope_p)
18743 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18744 /*check_dependency_p=*/true,
18745 /*type_p=*/false,
18746 /*is_declaration=*/true);
18747 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18749 cp_parser_skip_to_end_of_block_or_statement (parser);
18750 return false;
18753 /* Otherwise, we could be in either of the two productions. In that
18754 case, treat the nested-name-specifier as optional. */
18755 else
18756 qscope = cp_parser_nested_name_specifier_opt (parser,
18757 /*typename_keyword_p=*/false,
18758 /*check_dependency_p=*/true,
18759 /*type_p=*/false,
18760 /*is_declaration=*/true);
18761 if (!qscope)
18762 qscope = global_namespace;
18763 else if (UNSCOPED_ENUM_P (qscope))
18764 qscope = CP_TYPE_CONTEXT (qscope);
18766 if (access_declaration_p && cp_parser_error_occurred (parser))
18767 /* Something has already gone wrong; there's no need to parse
18768 further. Since an error has occurred, the return value of
18769 cp_parser_parse_definitely will be false, as required. */
18770 return cp_parser_parse_definitely (parser);
18772 token = cp_lexer_peek_token (parser->lexer);
18773 /* Parse the unqualified-id. */
18774 identifier = cp_parser_unqualified_id (parser,
18775 /*template_keyword_p=*/false,
18776 /*check_dependency_p=*/true,
18777 /*declarator_p=*/true,
18778 /*optional_p=*/false);
18780 if (access_declaration_p)
18782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18783 cp_parser_simulate_error (parser);
18784 if (!cp_parser_parse_definitely (parser))
18785 return false;
18787 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18789 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18790 if (cxx_dialect < cxx17
18791 && !in_system_header_at (ell->location))
18792 pedwarn (ell->location, 0,
18793 "pack expansion in using-declaration only available "
18794 "with -std=c++17 or -std=gnu++17");
18795 qscope = make_pack_expansion (qscope);
18798 /* The function we call to handle a using-declaration is different
18799 depending on what scope we are in. */
18800 if (qscope == error_mark_node || identifier == error_mark_node)
18802 else if (!identifier_p (identifier)
18803 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18804 /* [namespace.udecl]
18806 A using declaration shall not name a template-id. */
18807 error_at (token->location,
18808 "a template-id may not appear in a using-declaration");
18809 else
18811 if (at_class_scope_p ())
18813 /* Create the USING_DECL. */
18814 decl = do_class_using_decl (qscope, identifier);
18816 if (decl && typename_p)
18817 USING_DECL_TYPENAME_P (decl) = 1;
18819 if (check_for_bare_parameter_packs (decl))
18821 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18822 return false;
18824 else
18825 /* Add it to the list of members in this class. */
18826 finish_member_declaration (decl);
18828 else
18830 decl = cp_parser_lookup_name_simple (parser,
18831 identifier,
18832 token->location);
18833 if (decl == error_mark_node)
18834 cp_parser_name_lookup_error (parser, identifier,
18835 decl, NLE_NULL,
18836 token->location);
18837 else if (check_for_bare_parameter_packs (decl))
18839 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18840 return false;
18842 else if (!at_namespace_scope_p ())
18843 finish_local_using_decl (decl, qscope, identifier);
18844 else
18845 finish_namespace_using_decl (decl, qscope, identifier);
18849 if (!access_declaration_p
18850 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18852 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18853 if (cxx_dialect < cxx17)
18854 pedwarn (comma->location, 0,
18855 "comma-separated list in using-declaration only available "
18856 "with -std=c++17 or -std=gnu++17");
18857 goto again;
18860 /* Look for the final `;'. */
18861 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18863 if (access_declaration_p && errorcount == oldcount)
18864 warning_at (diag_token->location, OPT_Wdeprecated,
18865 "access declarations are deprecated "
18866 "in favour of using-declarations; "
18867 "suggestion: add the %<using%> keyword");
18869 return true;
18872 /* Parse an alias-declaration.
18874 alias-declaration:
18875 using identifier attribute-specifier-seq [opt] = type-id */
18877 static tree
18878 cp_parser_alias_declaration (cp_parser* parser)
18880 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18881 location_t id_location;
18882 cp_declarator *declarator;
18883 cp_decl_specifier_seq decl_specs;
18884 bool member_p;
18885 const char *saved_message = NULL;
18887 /* Look for the `using' keyword. */
18888 cp_token *using_token
18889 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18890 if (using_token == NULL)
18891 return error_mark_node;
18893 id_location = cp_lexer_peek_token (parser->lexer)->location;
18894 id = cp_parser_identifier (parser);
18895 if (id == error_mark_node)
18896 return error_mark_node;
18898 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18899 attributes = cp_parser_attributes_opt (parser);
18900 if (attributes == error_mark_node)
18901 return error_mark_node;
18903 cp_parser_require (parser, CPP_EQ, RT_EQ);
18905 if (cp_parser_error_occurred (parser))
18906 return error_mark_node;
18908 cp_parser_commit_to_tentative_parse (parser);
18910 /* Now we are going to parse the type-id of the declaration. */
18913 [dcl.type]/3 says:
18915 "A type-specifier-seq shall not define a class or enumeration
18916 unless it appears in the type-id of an alias-declaration (7.1.3) that
18917 is not the declaration of a template-declaration."
18919 In other words, if we currently are in an alias template, the
18920 type-id should not define a type.
18922 So let's set parser->type_definition_forbidden_message in that
18923 case; cp_parser_check_type_definition (called by
18924 cp_parser_class_specifier) will then emit an error if a type is
18925 defined in the type-id. */
18926 if (parser->num_template_parameter_lists)
18928 saved_message = parser->type_definition_forbidden_message;
18929 parser->type_definition_forbidden_message =
18930 G_("types may not be defined in alias template declarations");
18933 type = cp_parser_type_id (parser);
18935 /* Restore the error message if need be. */
18936 if (parser->num_template_parameter_lists)
18937 parser->type_definition_forbidden_message = saved_message;
18939 if (type == error_mark_node
18940 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18942 cp_parser_skip_to_end_of_block_or_statement (parser);
18943 return error_mark_node;
18946 /* A typedef-name can also be introduced by an alias-declaration. The
18947 identifier following the using keyword becomes a typedef-name. It has
18948 the same semantics as if it were introduced by the typedef
18949 specifier. In particular, it does not define a new type and it shall
18950 not appear in the type-id. */
18952 clear_decl_specs (&decl_specs);
18953 decl_specs.type = type;
18954 if (attributes != NULL_TREE)
18956 decl_specs.attributes = attributes;
18957 set_and_check_decl_spec_loc (&decl_specs,
18958 ds_attribute,
18959 attrs_token);
18961 set_and_check_decl_spec_loc (&decl_specs,
18962 ds_typedef,
18963 using_token);
18964 set_and_check_decl_spec_loc (&decl_specs,
18965 ds_alias,
18966 using_token);
18968 if (parser->num_template_parameter_lists
18969 && !cp_parser_check_template_parameters (parser,
18970 /*num_templates=*/0,
18971 id_location,
18972 /*declarator=*/NULL))
18973 return error_mark_node;
18975 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18976 declarator->id_loc = id_location;
18978 member_p = at_class_scope_p ();
18979 if (member_p)
18980 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18981 NULL_TREE, attributes);
18982 else
18983 decl = start_decl (declarator, &decl_specs, 0,
18984 attributes, NULL_TREE, &pushed_scope);
18985 if (decl == error_mark_node)
18986 return decl;
18988 // Attach constraints to the alias declaration.
18989 if (flag_concepts && current_template_parms)
18991 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18992 tree constr = build_constraints (reqs, NULL_TREE);
18993 set_constraints (decl, constr);
18996 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18998 if (pushed_scope)
18999 pop_scope (pushed_scope);
19001 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19002 added into the symbol table; otherwise, return the TYPE_DECL. */
19003 if (DECL_LANG_SPECIFIC (decl)
19004 && DECL_TEMPLATE_INFO (decl)
19005 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19007 decl = DECL_TI_TEMPLATE (decl);
19008 if (member_p)
19009 check_member_template (decl);
19012 return decl;
19015 /* Parse a using-directive.
19017 using-directive:
19018 using namespace :: [opt] nested-name-specifier [opt]
19019 namespace-name ; */
19021 static void
19022 cp_parser_using_directive (cp_parser* parser)
19024 tree namespace_decl;
19025 tree attribs;
19027 /* Look for the `using' keyword. */
19028 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19029 /* And the `namespace' keyword. */
19030 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19031 /* Look for the optional `::' operator. */
19032 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19033 /* And the optional nested-name-specifier. */
19034 cp_parser_nested_name_specifier_opt (parser,
19035 /*typename_keyword_p=*/false,
19036 /*check_dependency_p=*/true,
19037 /*type_p=*/false,
19038 /*is_declaration=*/true);
19039 /* Get the namespace being used. */
19040 namespace_decl = cp_parser_namespace_name (parser);
19041 /* And any specified attributes. */
19042 attribs = cp_parser_attributes_opt (parser);
19044 /* Update the symbol table. */
19045 if (namespace_bindings_p ())
19046 finish_namespace_using_directive (namespace_decl, attribs);
19047 else
19048 finish_local_using_directive (namespace_decl, attribs);
19050 /* Look for the final `;'. */
19051 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19054 /* Parse an asm-definition.
19056 asm-definition:
19057 asm ( string-literal ) ;
19059 GNU Extension:
19061 asm-definition:
19062 asm volatile [opt] ( string-literal ) ;
19063 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19064 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19065 : asm-operand-list [opt] ) ;
19066 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19067 : asm-operand-list [opt]
19068 : asm-clobber-list [opt] ) ;
19069 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19070 : asm-clobber-list [opt]
19071 : asm-goto-list ) ; */
19073 static void
19074 cp_parser_asm_definition (cp_parser* parser)
19076 tree string;
19077 tree outputs = NULL_TREE;
19078 tree inputs = NULL_TREE;
19079 tree clobbers = NULL_TREE;
19080 tree labels = NULL_TREE;
19081 tree asm_stmt;
19082 bool volatile_p = false;
19083 bool extended_p = false;
19084 bool invalid_inputs_p = false;
19085 bool invalid_outputs_p = false;
19086 bool goto_p = false;
19087 required_token missing = RT_NONE;
19089 /* Look for the `asm' keyword. */
19090 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19092 if (parser->in_function_body
19093 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19095 error ("%<asm%> in %<constexpr%> function");
19096 cp_function_chain->invalid_constexpr = true;
19099 /* See if the next token is `volatile'. */
19100 if (cp_parser_allow_gnu_extensions_p (parser)
19101 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19103 /* Remember that we saw the `volatile' keyword. */
19104 volatile_p = true;
19105 /* Consume the token. */
19106 cp_lexer_consume_token (parser->lexer);
19108 if (cp_parser_allow_gnu_extensions_p (parser)
19109 && parser->in_function_body
19110 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19112 /* Remember that we saw the `goto' keyword. */
19113 goto_p = true;
19114 /* Consume the token. */
19115 cp_lexer_consume_token (parser->lexer);
19117 /* Look for the opening `('. */
19118 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19119 return;
19120 /* Look for the string. */
19121 string = cp_parser_string_literal (parser, false, false);
19122 if (string == error_mark_node)
19124 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19125 /*consume_paren=*/true);
19126 return;
19129 /* If we're allowing GNU extensions, check for the extended assembly
19130 syntax. Unfortunately, the `:' tokens need not be separated by
19131 a space in C, and so, for compatibility, we tolerate that here
19132 too. Doing that means that we have to treat the `::' operator as
19133 two `:' tokens. */
19134 if (cp_parser_allow_gnu_extensions_p (parser)
19135 && parser->in_function_body
19136 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19137 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19139 bool inputs_p = false;
19140 bool clobbers_p = false;
19141 bool labels_p = false;
19143 /* The extended syntax was used. */
19144 extended_p = true;
19146 /* Look for outputs. */
19147 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19149 /* Consume the `:'. */
19150 cp_lexer_consume_token (parser->lexer);
19151 /* Parse the output-operands. */
19152 if (cp_lexer_next_token_is_not (parser->lexer,
19153 CPP_COLON)
19154 && cp_lexer_next_token_is_not (parser->lexer,
19155 CPP_SCOPE)
19156 && cp_lexer_next_token_is_not (parser->lexer,
19157 CPP_CLOSE_PAREN)
19158 && !goto_p)
19160 outputs = cp_parser_asm_operand_list (parser);
19161 if (outputs == error_mark_node)
19162 invalid_outputs_p = true;
19165 /* If the next token is `::', there are no outputs, and the
19166 next token is the beginning of the inputs. */
19167 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19168 /* The inputs are coming next. */
19169 inputs_p = true;
19171 /* Look for inputs. */
19172 if (inputs_p
19173 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19175 /* Consume the `:' or `::'. */
19176 cp_lexer_consume_token (parser->lexer);
19177 /* Parse the output-operands. */
19178 if (cp_lexer_next_token_is_not (parser->lexer,
19179 CPP_COLON)
19180 && cp_lexer_next_token_is_not (parser->lexer,
19181 CPP_SCOPE)
19182 && cp_lexer_next_token_is_not (parser->lexer,
19183 CPP_CLOSE_PAREN))
19185 inputs = cp_parser_asm_operand_list (parser);
19186 if (inputs == error_mark_node)
19187 invalid_inputs_p = true;
19190 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19191 /* The clobbers are coming next. */
19192 clobbers_p = true;
19194 /* Look for clobbers. */
19195 if (clobbers_p
19196 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19198 clobbers_p = true;
19199 /* Consume the `:' or `::'. */
19200 cp_lexer_consume_token (parser->lexer);
19201 /* Parse the clobbers. */
19202 if (cp_lexer_next_token_is_not (parser->lexer,
19203 CPP_COLON)
19204 && cp_lexer_next_token_is_not (parser->lexer,
19205 CPP_CLOSE_PAREN))
19206 clobbers = cp_parser_asm_clobber_list (parser);
19208 else if (goto_p
19209 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19210 /* The labels are coming next. */
19211 labels_p = true;
19213 /* Look for labels. */
19214 if (labels_p
19215 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19217 labels_p = true;
19218 /* Consume the `:' or `::'. */
19219 cp_lexer_consume_token (parser->lexer);
19220 /* Parse the labels. */
19221 labels = cp_parser_asm_label_list (parser);
19224 if (goto_p && !labels_p)
19225 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19227 else if (goto_p)
19228 missing = RT_COLON_SCOPE;
19230 /* Look for the closing `)'. */
19231 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19232 missing ? missing : RT_CLOSE_PAREN))
19233 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19234 /*consume_paren=*/true);
19235 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19237 if (!invalid_inputs_p && !invalid_outputs_p)
19239 /* Create the ASM_EXPR. */
19240 if (parser->in_function_body)
19242 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19243 inputs, clobbers, labels);
19244 /* If the extended syntax was not used, mark the ASM_EXPR. */
19245 if (!extended_p)
19247 tree temp = asm_stmt;
19248 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19249 temp = TREE_OPERAND (temp, 0);
19251 ASM_INPUT_P (temp) = 1;
19254 else
19255 symtab->finalize_toplevel_asm (string);
19259 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19260 type that comes from the decl-specifier-seq. */
19262 static tree
19263 strip_declarator_types (tree type, cp_declarator *declarator)
19265 for (cp_declarator *d = declarator; d;)
19266 switch (d->kind)
19268 case cdk_id:
19269 case cdk_decomp:
19270 case cdk_error:
19271 d = NULL;
19272 break;
19274 default:
19275 if (TYPE_PTRMEMFUNC_P (type))
19276 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19277 type = TREE_TYPE (type);
19278 d = d->declarator;
19279 break;
19282 return type;
19285 /* Declarators [gram.dcl.decl] */
19287 /* Parse an init-declarator.
19289 init-declarator:
19290 declarator initializer [opt]
19292 GNU Extension:
19294 init-declarator:
19295 declarator asm-specification [opt] attributes [opt] initializer [opt]
19297 function-definition:
19298 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19299 function-body
19300 decl-specifier-seq [opt] declarator function-try-block
19302 GNU Extension:
19304 function-definition:
19305 __extension__ function-definition
19307 TM Extension:
19309 function-definition:
19310 decl-specifier-seq [opt] declarator function-transaction-block
19312 The DECL_SPECIFIERS apply to this declarator. Returns a
19313 representation of the entity declared. If MEMBER_P is TRUE, then
19314 this declarator appears in a class scope. The new DECL created by
19315 this declarator is returned.
19317 The CHECKS are access checks that should be performed once we know
19318 what entity is being declared (and, therefore, what classes have
19319 befriended it).
19321 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19322 for a function-definition here as well. If the declarator is a
19323 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19324 be TRUE upon return. By that point, the function-definition will
19325 have been completely parsed.
19327 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19328 is FALSE.
19330 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19331 parsed declaration if it is an uninitialized single declarator not followed
19332 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19333 if present, will not be consumed. If returned, this declarator will be
19334 created with SD_INITIALIZED but will not call cp_finish_decl.
19336 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19337 and there is an initializer, the pointed location_t is set to the
19338 location of the '=' or `(', or '{' in C++11 token introducing the
19339 initializer. */
19341 static tree
19342 cp_parser_init_declarator (cp_parser* parser,
19343 cp_decl_specifier_seq *decl_specifiers,
19344 vec<deferred_access_check, va_gc> *checks,
19345 bool function_definition_allowed_p,
19346 bool member_p,
19347 int declares_class_or_enum,
19348 bool* function_definition_p,
19349 tree* maybe_range_for_decl,
19350 location_t* init_loc,
19351 tree* auto_result)
19353 cp_token *token = NULL, *asm_spec_start_token = NULL,
19354 *attributes_start_token = NULL;
19355 cp_declarator *declarator;
19356 tree prefix_attributes;
19357 tree attributes = NULL;
19358 tree asm_specification;
19359 tree initializer;
19360 tree decl = NULL_TREE;
19361 tree scope;
19362 int is_initialized;
19363 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19364 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19365 "(...)". */
19366 enum cpp_ttype initialization_kind;
19367 bool is_direct_init = false;
19368 bool is_non_constant_init;
19369 int ctor_dtor_or_conv_p;
19370 bool friend_p = cp_parser_friend_p (decl_specifiers);
19371 tree pushed_scope = NULL_TREE;
19372 bool range_for_decl_p = false;
19373 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19374 location_t tmp_init_loc = UNKNOWN_LOCATION;
19376 /* Gather the attributes that were provided with the
19377 decl-specifiers. */
19378 prefix_attributes = decl_specifiers->attributes;
19380 /* Assume that this is not the declarator for a function
19381 definition. */
19382 if (function_definition_p)
19383 *function_definition_p = false;
19385 /* Default arguments are only permitted for function parameters. */
19386 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19387 parser->default_arg_ok_p = false;
19389 /* Defer access checks while parsing the declarator; we cannot know
19390 what names are accessible until we know what is being
19391 declared. */
19392 resume_deferring_access_checks ();
19394 token = cp_lexer_peek_token (parser->lexer);
19396 /* Parse the declarator. */
19397 declarator
19398 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19399 &ctor_dtor_or_conv_p,
19400 /*parenthesized_p=*/NULL,
19401 member_p, friend_p);
19402 /* Gather up the deferred checks. */
19403 stop_deferring_access_checks ();
19405 parser->default_arg_ok_p = saved_default_arg_ok_p;
19407 /* If the DECLARATOR was erroneous, there's no need to go
19408 further. */
19409 if (declarator == cp_error_declarator)
19410 return error_mark_node;
19412 /* Check that the number of template-parameter-lists is OK. */
19413 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19414 token->location))
19415 return error_mark_node;
19417 if (declares_class_or_enum & 2)
19418 cp_parser_check_for_definition_in_return_type (declarator,
19419 decl_specifiers->type,
19420 decl_specifiers->locations[ds_type_spec]);
19422 /* Figure out what scope the entity declared by the DECLARATOR is
19423 located in. `grokdeclarator' sometimes changes the scope, so
19424 we compute it now. */
19425 scope = get_scope_of_declarator (declarator);
19427 /* Perform any lookups in the declared type which were thought to be
19428 dependent, but are not in the scope of the declarator. */
19429 decl_specifiers->type
19430 = maybe_update_decl_type (decl_specifiers->type, scope);
19432 /* If we're allowing GNU extensions, look for an
19433 asm-specification. */
19434 if (cp_parser_allow_gnu_extensions_p (parser))
19436 /* Look for an asm-specification. */
19437 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19438 asm_specification = cp_parser_asm_specification_opt (parser);
19440 else
19441 asm_specification = NULL_TREE;
19443 /* Look for attributes. */
19444 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19445 attributes = cp_parser_attributes_opt (parser);
19447 /* Peek at the next token. */
19448 token = cp_lexer_peek_token (parser->lexer);
19450 bool bogus_implicit_tmpl = false;
19452 if (function_declarator_p (declarator))
19454 /* Handle C++17 deduction guides. */
19455 if (!decl_specifiers->type
19456 && ctor_dtor_or_conv_p <= 0
19457 && cxx_dialect >= cxx17)
19459 cp_declarator *id = get_id_declarator (declarator);
19460 tree name = id->u.id.unqualified_name;
19461 parser->scope = id->u.id.qualifying_scope;
19462 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19463 if (tmpl
19464 && (DECL_CLASS_TEMPLATE_P (tmpl)
19465 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19467 id->u.id.unqualified_name = dguide_name (tmpl);
19468 id->u.id.sfk = sfk_deduction_guide;
19469 ctor_dtor_or_conv_p = 1;
19473 /* Check to see if the token indicates the start of a
19474 function-definition. */
19475 if (cp_parser_token_starts_function_definition_p (token))
19477 if (!function_definition_allowed_p)
19479 /* If a function-definition should not appear here, issue an
19480 error message. */
19481 cp_parser_error (parser,
19482 "a function-definition is not allowed here");
19483 return error_mark_node;
19486 location_t func_brace_location
19487 = cp_lexer_peek_token (parser->lexer)->location;
19489 /* Neither attributes nor an asm-specification are allowed
19490 on a function-definition. */
19491 if (asm_specification)
19492 error_at (asm_spec_start_token->location,
19493 "an asm-specification is not allowed "
19494 "on a function-definition");
19495 if (attributes)
19496 error_at (attributes_start_token->location,
19497 "attributes are not allowed "
19498 "on a function-definition");
19499 /* This is a function-definition. */
19500 *function_definition_p = true;
19502 /* Parse the function definition. */
19503 if (member_p)
19504 decl = cp_parser_save_member_function_body (parser,
19505 decl_specifiers,
19506 declarator,
19507 prefix_attributes);
19508 else
19509 decl =
19510 (cp_parser_function_definition_from_specifiers_and_declarator
19511 (parser, decl_specifiers, prefix_attributes, declarator));
19513 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19515 /* This is where the prologue starts... */
19516 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19517 = func_brace_location;
19520 return decl;
19523 else if (parser->fully_implicit_function_template_p)
19525 /* A non-template declaration involving a function parameter list
19526 containing an implicit template parameter will be made into a
19527 template. If the resulting declaration is not going to be an
19528 actual function then finish the template scope here to prevent it.
19529 An error message will be issued once we have a decl to talk about.
19531 FIXME probably we should do type deduction rather than create an
19532 implicit template, but the standard currently doesn't allow it. */
19533 bogus_implicit_tmpl = true;
19534 finish_fully_implicit_template (parser, NULL_TREE);
19537 /* [dcl.dcl]
19539 Only in function declarations for constructors, destructors, type
19540 conversions, and deduction guides can the decl-specifier-seq be omitted.
19542 We explicitly postpone this check past the point where we handle
19543 function-definitions because we tolerate function-definitions
19544 that are missing their return types in some modes. */
19545 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19547 cp_parser_error (parser,
19548 "expected constructor, destructor, or type conversion");
19549 return error_mark_node;
19552 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19553 if (token->type == CPP_EQ
19554 || token->type == CPP_OPEN_PAREN
19555 || token->type == CPP_OPEN_BRACE)
19557 is_initialized = SD_INITIALIZED;
19558 initialization_kind = token->type;
19559 if (maybe_range_for_decl)
19560 *maybe_range_for_decl = error_mark_node;
19561 tmp_init_loc = token->location;
19562 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19563 *init_loc = tmp_init_loc;
19565 if (token->type == CPP_EQ
19566 && function_declarator_p (declarator))
19568 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19569 if (t2->keyword == RID_DEFAULT)
19570 is_initialized = SD_DEFAULTED;
19571 else if (t2->keyword == RID_DELETE)
19572 is_initialized = SD_DELETED;
19575 else
19577 /* If the init-declarator isn't initialized and isn't followed by a
19578 `,' or `;', it's not a valid init-declarator. */
19579 if (token->type != CPP_COMMA
19580 && token->type != CPP_SEMICOLON)
19582 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19583 range_for_decl_p = true;
19584 else
19586 if (!maybe_range_for_decl)
19587 cp_parser_error (parser, "expected initializer");
19588 return error_mark_node;
19591 is_initialized = SD_UNINITIALIZED;
19592 initialization_kind = CPP_EOF;
19595 /* Because start_decl has side-effects, we should only call it if we
19596 know we're going ahead. By this point, we know that we cannot
19597 possibly be looking at any other construct. */
19598 cp_parser_commit_to_tentative_parse (parser);
19600 /* Enter the newly declared entry in the symbol table. If we're
19601 processing a declaration in a class-specifier, we wait until
19602 after processing the initializer. */
19603 if (!member_p)
19605 if (parser->in_unbraced_linkage_specification_p)
19606 decl_specifiers->storage_class = sc_extern;
19607 decl = start_decl (declarator, decl_specifiers,
19608 range_for_decl_p? SD_INITIALIZED : is_initialized,
19609 attributes, prefix_attributes, &pushed_scope);
19610 cp_finalize_omp_declare_simd (parser, decl);
19611 cp_finalize_oacc_routine (parser, decl, false);
19612 /* Adjust location of decl if declarator->id_loc is more appropriate:
19613 set, and decl wasn't merged with another decl, in which case its
19614 location would be different from input_location, and more accurate. */
19615 if (DECL_P (decl)
19616 && declarator->id_loc != UNKNOWN_LOCATION
19617 && DECL_SOURCE_LOCATION (decl) == input_location)
19618 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19620 else if (scope)
19621 /* Enter the SCOPE. That way unqualified names appearing in the
19622 initializer will be looked up in SCOPE. */
19623 pushed_scope = push_scope (scope);
19625 /* Perform deferred access control checks, now that we know in which
19626 SCOPE the declared entity resides. */
19627 if (!member_p && decl)
19629 tree saved_current_function_decl = NULL_TREE;
19631 /* If the entity being declared is a function, pretend that we
19632 are in its scope. If it is a `friend', it may have access to
19633 things that would not otherwise be accessible. */
19634 if (TREE_CODE (decl) == FUNCTION_DECL)
19636 saved_current_function_decl = current_function_decl;
19637 current_function_decl = decl;
19640 /* Perform access checks for template parameters. */
19641 cp_parser_perform_template_parameter_access_checks (checks);
19643 /* Perform the access control checks for the declarator and the
19644 decl-specifiers. */
19645 perform_deferred_access_checks (tf_warning_or_error);
19647 /* Restore the saved value. */
19648 if (TREE_CODE (decl) == FUNCTION_DECL)
19649 current_function_decl = saved_current_function_decl;
19652 /* Parse the initializer. */
19653 initializer = NULL_TREE;
19654 is_direct_init = false;
19655 is_non_constant_init = true;
19656 if (is_initialized)
19658 if (function_declarator_p (declarator))
19660 if (initialization_kind == CPP_EQ)
19661 initializer = cp_parser_pure_specifier (parser);
19662 else
19664 /* If the declaration was erroneous, we don't really
19665 know what the user intended, so just silently
19666 consume the initializer. */
19667 if (decl != error_mark_node)
19668 error_at (tmp_init_loc, "initializer provided for function");
19669 cp_parser_skip_to_closing_parenthesis (parser,
19670 /*recovering=*/true,
19671 /*or_comma=*/false,
19672 /*consume_paren=*/true);
19675 else
19677 /* We want to record the extra mangling scope for in-class
19678 initializers of class members and initializers of static data
19679 member templates. The former involves deferring
19680 parsing of the initializer until end of class as with default
19681 arguments. So right here we only handle the latter. */
19682 if (!member_p && processing_template_decl && decl != error_mark_node)
19683 start_lambda_scope (decl);
19684 initializer = cp_parser_initializer (parser,
19685 &is_direct_init,
19686 &is_non_constant_init);
19687 if (!member_p && processing_template_decl && decl != error_mark_node)
19688 finish_lambda_scope ();
19689 if (initializer == error_mark_node)
19690 cp_parser_skip_to_end_of_statement (parser);
19694 /* The old parser allows attributes to appear after a parenthesized
19695 initializer. Mark Mitchell proposed removing this functionality
19696 on the GCC mailing lists on 2002-08-13. This parser accepts the
19697 attributes -- but ignores them. Made a permerror in GCC 8. */
19698 if (cp_parser_allow_gnu_extensions_p (parser)
19699 && initialization_kind == CPP_OPEN_PAREN
19700 && cp_parser_attributes_opt (parser)
19701 && permerror (input_location,
19702 "attributes after parenthesized initializer ignored"))
19704 static bool hint;
19705 if (flag_permissive && !hint)
19707 hint = true;
19708 inform (input_location,
19709 "this flexibility is deprecated and will be removed");
19713 /* And now complain about a non-function implicit template. */
19714 if (bogus_implicit_tmpl && decl != error_mark_node)
19715 error_at (DECL_SOURCE_LOCATION (decl),
19716 "non-function %qD declared as implicit template", decl);
19718 /* For an in-class declaration, use `grokfield' to create the
19719 declaration. */
19720 if (member_p)
19722 if (pushed_scope)
19724 pop_scope (pushed_scope);
19725 pushed_scope = NULL_TREE;
19727 decl = grokfield (declarator, decl_specifiers,
19728 initializer, !is_non_constant_init,
19729 /*asmspec=*/NULL_TREE,
19730 attr_chainon (attributes, prefix_attributes));
19731 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19732 cp_parser_save_default_args (parser, decl);
19733 cp_finalize_omp_declare_simd (parser, decl);
19734 cp_finalize_oacc_routine (parser, decl, false);
19737 /* Finish processing the declaration. But, skip member
19738 declarations. */
19739 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19741 cp_finish_decl (decl,
19742 initializer, !is_non_constant_init,
19743 asm_specification,
19744 /* If the initializer is in parentheses, then this is
19745 a direct-initialization, which means that an
19746 `explicit' constructor is OK. Otherwise, an
19747 `explicit' constructor cannot be used. */
19748 ((is_direct_init || !is_initialized)
19749 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19751 else if ((cxx_dialect != cxx98) && friend_p
19752 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19753 /* Core issue #226 (C++0x only): A default template-argument
19754 shall not be specified in a friend class template
19755 declaration. */
19756 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19757 /*is_partial=*/false, /*is_friend_decl=*/1);
19759 if (!friend_p && pushed_scope)
19760 pop_scope (pushed_scope);
19762 if (function_declarator_p (declarator)
19763 && parser->fully_implicit_function_template_p)
19765 if (member_p)
19766 decl = finish_fully_implicit_template (parser, decl);
19767 else
19768 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19771 if (auto_result && is_initialized && decl_specifiers->type
19772 && type_uses_auto (decl_specifiers->type))
19773 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19775 return decl;
19778 /* Parse a declarator.
19780 declarator:
19781 direct-declarator
19782 ptr-operator declarator
19784 abstract-declarator:
19785 ptr-operator abstract-declarator [opt]
19786 direct-abstract-declarator
19788 GNU Extensions:
19790 declarator:
19791 attributes [opt] direct-declarator
19792 attributes [opt] ptr-operator declarator
19794 abstract-declarator:
19795 attributes [opt] ptr-operator abstract-declarator [opt]
19796 attributes [opt] direct-abstract-declarator
19798 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19799 detect constructors, destructors, deduction guides, or conversion operators.
19800 It is set to -1 if the declarator is a name, and +1 if it is a
19801 function. Otherwise it is set to zero. Usually you just want to
19802 test for >0, but internally the negative value is used.
19804 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19805 a decl-specifier-seq unless it declares a constructor, destructor,
19806 or conversion. It might seem that we could check this condition in
19807 semantic analysis, rather than parsing, but that makes it difficult
19808 to handle something like `f()'. We want to notice that there are
19809 no decl-specifiers, and therefore realize that this is an
19810 expression, not a declaration.)
19812 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19813 the declarator is a direct-declarator of the form "(...)".
19815 MEMBER_P is true iff this declarator is a member-declarator.
19817 FRIEND_P is true iff this declarator is a friend. */
19819 static cp_declarator *
19820 cp_parser_declarator (cp_parser* parser,
19821 cp_parser_declarator_kind dcl_kind,
19822 int* ctor_dtor_or_conv_p,
19823 bool* parenthesized_p,
19824 bool member_p, bool friend_p)
19826 cp_declarator *declarator;
19827 enum tree_code code;
19828 cp_cv_quals cv_quals;
19829 tree class_type;
19830 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19832 /* Assume this is not a constructor, destructor, or type-conversion
19833 operator. */
19834 if (ctor_dtor_or_conv_p)
19835 *ctor_dtor_or_conv_p = 0;
19837 if (cp_parser_allow_gnu_extensions_p (parser))
19838 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19840 /* Check for the ptr-operator production. */
19841 cp_parser_parse_tentatively (parser);
19842 /* Parse the ptr-operator. */
19843 code = cp_parser_ptr_operator (parser,
19844 &class_type,
19845 &cv_quals,
19846 &std_attributes);
19848 /* If that worked, then we have a ptr-operator. */
19849 if (cp_parser_parse_definitely (parser))
19851 /* If a ptr-operator was found, then this declarator was not
19852 parenthesized. */
19853 if (parenthesized_p)
19854 *parenthesized_p = true;
19855 /* The dependent declarator is optional if we are parsing an
19856 abstract-declarator. */
19857 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19858 cp_parser_parse_tentatively (parser);
19860 /* Parse the dependent declarator. */
19861 declarator = cp_parser_declarator (parser, dcl_kind,
19862 /*ctor_dtor_or_conv_p=*/NULL,
19863 /*parenthesized_p=*/NULL,
19864 /*member_p=*/false,
19865 friend_p);
19867 /* If we are parsing an abstract-declarator, we must handle the
19868 case where the dependent declarator is absent. */
19869 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19870 && !cp_parser_parse_definitely (parser))
19871 declarator = NULL;
19873 declarator = cp_parser_make_indirect_declarator
19874 (code, class_type, cv_quals, declarator, std_attributes);
19876 /* Everything else is a direct-declarator. */
19877 else
19879 if (parenthesized_p)
19880 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19881 CPP_OPEN_PAREN);
19882 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19883 ctor_dtor_or_conv_p,
19884 member_p, friend_p);
19887 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19888 declarator->attributes = gnu_attributes;
19889 return declarator;
19892 /* Parse a direct-declarator or direct-abstract-declarator.
19894 direct-declarator:
19895 declarator-id
19896 direct-declarator ( parameter-declaration-clause )
19897 cv-qualifier-seq [opt]
19898 ref-qualifier [opt]
19899 exception-specification [opt]
19900 direct-declarator [ constant-expression [opt] ]
19901 ( declarator )
19903 direct-abstract-declarator:
19904 direct-abstract-declarator [opt]
19905 ( parameter-declaration-clause )
19906 cv-qualifier-seq [opt]
19907 ref-qualifier [opt]
19908 exception-specification [opt]
19909 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19910 ( abstract-declarator )
19912 Returns a representation of the declarator. DCL_KIND is
19913 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19914 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19915 we are parsing a direct-declarator. It is
19916 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19917 of ambiguity we prefer an abstract declarator, as per
19918 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19919 as for cp_parser_declarator. */
19921 static cp_declarator *
19922 cp_parser_direct_declarator (cp_parser* parser,
19923 cp_parser_declarator_kind dcl_kind,
19924 int* ctor_dtor_or_conv_p,
19925 bool member_p, bool friend_p)
19927 cp_token *token;
19928 cp_declarator *declarator = NULL;
19929 tree scope = NULL_TREE;
19930 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19931 bool saved_in_declarator_p = parser->in_declarator_p;
19932 bool first = true;
19933 tree pushed_scope = NULL_TREE;
19934 cp_token *open_paren = NULL, *close_paren = NULL;
19936 while (true)
19938 /* Peek at the next token. */
19939 token = cp_lexer_peek_token (parser->lexer);
19940 if (token->type == CPP_OPEN_PAREN)
19942 /* This is either a parameter-declaration-clause, or a
19943 parenthesized declarator. When we know we are parsing a
19944 named declarator, it must be a parenthesized declarator
19945 if FIRST is true. For instance, `(int)' is a
19946 parameter-declaration-clause, with an omitted
19947 direct-abstract-declarator. But `((*))', is a
19948 parenthesized abstract declarator. Finally, when T is a
19949 template parameter `(T)' is a
19950 parameter-declaration-clause, and not a parenthesized
19951 named declarator.
19953 We first try and parse a parameter-declaration-clause,
19954 and then try a nested declarator (if FIRST is true).
19956 It is not an error for it not to be a
19957 parameter-declaration-clause, even when FIRST is
19958 false. Consider,
19960 int i (int);
19961 int i (3);
19963 The first is the declaration of a function while the
19964 second is the definition of a variable, including its
19965 initializer.
19967 Having seen only the parenthesis, we cannot know which of
19968 these two alternatives should be selected. Even more
19969 complex are examples like:
19971 int i (int (a));
19972 int i (int (3));
19974 The former is a function-declaration; the latter is a
19975 variable initialization.
19977 Thus again, we try a parameter-declaration-clause, and if
19978 that fails, we back out and return. */
19980 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19982 tree params;
19983 bool is_declarator = false;
19985 open_paren = NULL;
19987 /* In a member-declarator, the only valid interpretation
19988 of a parenthesis is the start of a
19989 parameter-declaration-clause. (It is invalid to
19990 initialize a static data member with a parenthesized
19991 initializer; only the "=" form of initialization is
19992 permitted.) */
19993 if (!member_p)
19994 cp_parser_parse_tentatively (parser);
19996 /* Consume the `('. */
19997 matching_parens parens;
19998 parens.consume_open (parser);
19999 if (first)
20001 /* If this is going to be an abstract declarator, we're
20002 in a declarator and we can't have default args. */
20003 parser->default_arg_ok_p = false;
20004 parser->in_declarator_p = true;
20007 begin_scope (sk_function_parms, NULL_TREE);
20009 /* Parse the parameter-declaration-clause. */
20010 params = cp_parser_parameter_declaration_clause (parser);
20012 /* Consume the `)'. */
20013 parens.require_close (parser);
20015 /* If all went well, parse the cv-qualifier-seq,
20016 ref-qualifier and the exception-specification. */
20017 if (member_p || cp_parser_parse_definitely (parser))
20019 cp_cv_quals cv_quals;
20020 cp_virt_specifiers virt_specifiers;
20021 cp_ref_qualifier ref_qual;
20022 tree exception_specification;
20023 tree late_return;
20024 tree attrs;
20025 bool memfn = (member_p || (pushed_scope
20026 && CLASS_TYPE_P (pushed_scope)));
20028 is_declarator = true;
20030 if (ctor_dtor_or_conv_p)
20031 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20032 first = false;
20034 /* Parse the cv-qualifier-seq. */
20035 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20036 /* Parse the ref-qualifier. */
20037 ref_qual = cp_parser_ref_qualifier_opt (parser);
20038 /* Parse the tx-qualifier. */
20039 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20040 /* And the exception-specification. */
20041 exception_specification
20042 = cp_parser_exception_specification_opt (parser);
20044 attrs = cp_parser_std_attribute_spec_seq (parser);
20046 /* In here, we handle cases where attribute is used after
20047 the function declaration. For example:
20048 void func (int x) __attribute__((vector(..))); */
20049 tree gnu_attrs = NULL_TREE;
20050 tree requires_clause = NULL_TREE;
20051 late_return = (cp_parser_late_return_type_opt
20052 (parser, declarator, requires_clause,
20053 memfn ? cv_quals : -1));
20055 /* Parse the virt-specifier-seq. */
20056 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20058 /* Create the function-declarator. */
20059 declarator = make_call_declarator (declarator,
20060 params,
20061 cv_quals,
20062 virt_specifiers,
20063 ref_qual,
20064 tx_qual,
20065 exception_specification,
20066 late_return,
20067 requires_clause);
20068 declarator->std_attributes = attrs;
20069 declarator->attributes = gnu_attrs;
20070 /* Any subsequent parameter lists are to do with
20071 return type, so are not those of the declared
20072 function. */
20073 parser->default_arg_ok_p = false;
20076 /* Remove the function parms from scope. */
20077 pop_bindings_and_leave_scope ();
20079 if (is_declarator)
20080 /* Repeat the main loop. */
20081 continue;
20084 /* If this is the first, we can try a parenthesized
20085 declarator. */
20086 if (first)
20088 bool saved_in_type_id_in_expr_p;
20090 parser->default_arg_ok_p = saved_default_arg_ok_p;
20091 parser->in_declarator_p = saved_in_declarator_p;
20093 open_paren = token;
20094 /* Consume the `('. */
20095 matching_parens parens;
20096 parens.consume_open (parser);
20097 /* Parse the nested declarator. */
20098 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20099 parser->in_type_id_in_expr_p = true;
20100 declarator
20101 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20102 /*parenthesized_p=*/NULL,
20103 member_p, friend_p);
20104 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20105 first = false;
20106 /* Expect a `)'. */
20107 close_paren = cp_lexer_peek_token (parser->lexer);
20108 if (!parens.require_close (parser))
20109 declarator = cp_error_declarator;
20110 if (declarator == cp_error_declarator)
20111 break;
20113 goto handle_declarator;
20115 /* Otherwise, we must be done. */
20116 else
20117 break;
20119 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20120 && token->type == CPP_OPEN_SQUARE
20121 && !cp_next_tokens_can_be_attribute_p (parser))
20123 /* Parse an array-declarator. */
20124 tree bounds, attrs;
20126 if (ctor_dtor_or_conv_p)
20127 *ctor_dtor_or_conv_p = 0;
20129 open_paren = NULL;
20130 first = false;
20131 parser->default_arg_ok_p = false;
20132 parser->in_declarator_p = true;
20133 /* Consume the `['. */
20134 cp_lexer_consume_token (parser->lexer);
20135 /* Peek at the next token. */
20136 token = cp_lexer_peek_token (parser->lexer);
20137 /* If the next token is `]', then there is no
20138 constant-expression. */
20139 if (token->type != CPP_CLOSE_SQUARE)
20141 bool non_constant_p;
20142 bounds
20143 = cp_parser_constant_expression (parser,
20144 /*allow_non_constant=*/true,
20145 &non_constant_p);
20146 if (!non_constant_p)
20147 /* OK */;
20148 else if (error_operand_p (bounds))
20149 /* Already gave an error. */;
20150 else if (!parser->in_function_body
20151 || current_binding_level->kind == sk_function_parms)
20153 /* Normally, the array bound must be an integral constant
20154 expression. However, as an extension, we allow VLAs
20155 in function scopes as long as they aren't part of a
20156 parameter declaration. */
20157 cp_parser_error (parser,
20158 "array bound is not an integer constant");
20159 bounds = error_mark_node;
20161 else if (processing_template_decl
20162 && !type_dependent_expression_p (bounds))
20164 /* Remember this wasn't a constant-expression. */
20165 bounds = build_nop (TREE_TYPE (bounds), bounds);
20166 TREE_SIDE_EFFECTS (bounds) = 1;
20169 else
20170 bounds = NULL_TREE;
20171 /* Look for the closing `]'. */
20172 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20174 declarator = cp_error_declarator;
20175 break;
20178 attrs = cp_parser_std_attribute_spec_seq (parser);
20179 declarator = make_array_declarator (declarator, bounds);
20180 declarator->std_attributes = attrs;
20182 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20185 tree qualifying_scope;
20186 tree unqualified_name;
20187 tree attrs;
20188 special_function_kind sfk;
20189 bool abstract_ok;
20190 bool pack_expansion_p = false;
20191 cp_token *declarator_id_start_token;
20193 /* Parse a declarator-id */
20194 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20195 if (abstract_ok)
20197 cp_parser_parse_tentatively (parser);
20199 /* If we see an ellipsis, we should be looking at a
20200 parameter pack. */
20201 if (token->type == CPP_ELLIPSIS)
20203 /* Consume the `...' */
20204 cp_lexer_consume_token (parser->lexer);
20206 pack_expansion_p = true;
20210 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20211 unqualified_name
20212 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20213 qualifying_scope = parser->scope;
20214 if (abstract_ok)
20216 bool okay = false;
20218 if (!unqualified_name && pack_expansion_p)
20220 /* Check whether an error occurred. */
20221 okay = !cp_parser_error_occurred (parser);
20223 /* We already consumed the ellipsis to mark a
20224 parameter pack, but we have no way to report it,
20225 so abort the tentative parse. We will be exiting
20226 immediately anyway. */
20227 cp_parser_abort_tentative_parse (parser);
20229 else
20230 okay = cp_parser_parse_definitely (parser);
20232 if (!okay)
20233 unqualified_name = error_mark_node;
20234 else if (unqualified_name
20235 && (qualifying_scope
20236 || (!identifier_p (unqualified_name))))
20238 cp_parser_error (parser, "expected unqualified-id");
20239 unqualified_name = error_mark_node;
20243 if (!unqualified_name)
20244 return NULL;
20245 if (unqualified_name == error_mark_node)
20247 declarator = cp_error_declarator;
20248 pack_expansion_p = false;
20249 declarator->parameter_pack_p = false;
20250 break;
20253 attrs = cp_parser_std_attribute_spec_seq (parser);
20255 if (qualifying_scope && at_namespace_scope_p ()
20256 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20258 /* In the declaration of a member of a template class
20259 outside of the class itself, the SCOPE will sometimes
20260 be a TYPENAME_TYPE. For example, given:
20262 template <typename T>
20263 int S<T>::R::i = 3;
20265 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20266 this context, we must resolve S<T>::R to an ordinary
20267 type, rather than a typename type.
20269 The reason we normally avoid resolving TYPENAME_TYPEs
20270 is that a specialization of `S' might render
20271 `S<T>::R' not a type. However, if `S' is
20272 specialized, then this `i' will not be used, so there
20273 is no harm in resolving the types here. */
20274 tree type;
20276 /* Resolve the TYPENAME_TYPE. */
20277 type = resolve_typename_type (qualifying_scope,
20278 /*only_current_p=*/false);
20279 /* If that failed, the declarator is invalid. */
20280 if (TREE_CODE (type) == TYPENAME_TYPE)
20282 if (typedef_variant_p (type))
20283 error_at (declarator_id_start_token->location,
20284 "cannot define member of dependent typedef "
20285 "%qT", type);
20286 else
20287 error_at (declarator_id_start_token->location,
20288 "%<%T::%E%> is not a type",
20289 TYPE_CONTEXT (qualifying_scope),
20290 TYPE_IDENTIFIER (qualifying_scope));
20292 qualifying_scope = type;
20295 sfk = sfk_none;
20297 if (unqualified_name)
20299 tree class_type;
20301 if (qualifying_scope
20302 && CLASS_TYPE_P (qualifying_scope))
20303 class_type = qualifying_scope;
20304 else
20305 class_type = current_class_type;
20307 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20309 tree name_type = TREE_TYPE (unqualified_name);
20311 if (!class_type || !same_type_p (name_type, class_type))
20313 /* We do not attempt to print the declarator
20314 here because we do not have enough
20315 information about its original syntactic
20316 form. */
20317 cp_parser_error (parser, "invalid declarator");
20318 declarator = cp_error_declarator;
20319 break;
20321 else if (qualifying_scope
20322 && CLASSTYPE_USE_TEMPLATE (name_type))
20324 error_at (declarator_id_start_token->location,
20325 "invalid use of constructor as a template");
20326 inform (declarator_id_start_token->location,
20327 "use %<%T::%D%> instead of %<%T::%D%> to "
20328 "name the constructor in a qualified name",
20329 class_type,
20330 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20331 class_type, name_type);
20332 declarator = cp_error_declarator;
20333 break;
20335 unqualified_name = constructor_name (class_type);
20338 if (class_type)
20340 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20341 sfk = sfk_destructor;
20342 else if (identifier_p (unqualified_name)
20343 && IDENTIFIER_CONV_OP_P (unqualified_name))
20344 sfk = sfk_conversion;
20345 else if (/* There's no way to declare a constructor
20346 for an unnamed type, even if the type
20347 got a name for linkage purposes. */
20348 !TYPE_WAS_UNNAMED (class_type)
20349 /* Handle correctly (c++/19200):
20351 struct S {
20352 struct T{};
20353 friend void S(T);
20356 and also:
20358 namespace N {
20359 void S();
20362 struct S {
20363 friend void N::S();
20364 }; */
20365 && (!friend_p || class_type == qualifying_scope)
20366 && constructor_name_p (unqualified_name,
20367 class_type))
20368 sfk = sfk_constructor;
20369 else if (is_overloaded_fn (unqualified_name)
20370 && DECL_CONSTRUCTOR_P (get_first_fn
20371 (unqualified_name)))
20372 sfk = sfk_constructor;
20374 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20375 *ctor_dtor_or_conv_p = -1;
20378 declarator = make_id_declarator (qualifying_scope,
20379 unqualified_name,
20380 sfk);
20381 declarator->std_attributes = attrs;
20382 declarator->id_loc = token->location;
20383 declarator->parameter_pack_p = pack_expansion_p;
20385 if (pack_expansion_p)
20386 maybe_warn_variadic_templates ();
20389 handle_declarator:;
20390 scope = get_scope_of_declarator (declarator);
20391 if (scope)
20393 /* Any names that appear after the declarator-id for a
20394 member are looked up in the containing scope. */
20395 if (at_function_scope_p ())
20397 /* But declarations with qualified-ids can't appear in a
20398 function. */
20399 cp_parser_error (parser, "qualified-id in declaration");
20400 declarator = cp_error_declarator;
20401 break;
20403 pushed_scope = push_scope (scope);
20405 parser->in_declarator_p = true;
20406 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20407 || (declarator && declarator->kind == cdk_id))
20408 /* Default args are only allowed on function
20409 declarations. */
20410 parser->default_arg_ok_p = saved_default_arg_ok_p;
20411 else
20412 parser->default_arg_ok_p = false;
20414 first = false;
20416 /* We're done. */
20417 else
20418 break;
20421 /* For an abstract declarator, we might wind up with nothing at this
20422 point. That's an error; the declarator is not optional. */
20423 if (!declarator)
20424 cp_parser_error (parser, "expected declarator");
20425 else if (open_paren)
20427 /* Record overly parenthesized declarator so we can give a
20428 diagnostic about confusing decl/expr disambiguation. */
20429 if (declarator->kind == cdk_array)
20431 /* If the open and close parens are on different lines, this
20432 is probably a formatting thing, so ignore. */
20433 expanded_location open = expand_location (open_paren->location);
20434 expanded_location close = expand_location (close_paren->location);
20435 if (open.line != close.line || open.file != close.file)
20436 open_paren = NULL;
20438 if (open_paren)
20439 declarator->parenthesized = open_paren->location;
20442 /* If we entered a scope, we must exit it now. */
20443 if (pushed_scope)
20444 pop_scope (pushed_scope);
20446 parser->default_arg_ok_p = saved_default_arg_ok_p;
20447 parser->in_declarator_p = saved_in_declarator_p;
20449 return declarator;
20452 /* Parse a ptr-operator.
20454 ptr-operator:
20455 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20456 * cv-qualifier-seq [opt]
20458 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20459 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20461 GNU Extension:
20463 ptr-operator:
20464 & cv-qualifier-seq [opt]
20466 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20467 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20468 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20469 filled in with the TYPE containing the member. *CV_QUALS is
20470 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20471 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20472 Note that the tree codes returned by this function have nothing
20473 to do with the types of trees that will be eventually be created
20474 to represent the pointer or reference type being parsed. They are
20475 just constants with suggestive names. */
20476 static enum tree_code
20477 cp_parser_ptr_operator (cp_parser* parser,
20478 tree* type,
20479 cp_cv_quals *cv_quals,
20480 tree *attributes)
20482 enum tree_code code = ERROR_MARK;
20483 cp_token *token;
20484 tree attrs = NULL_TREE;
20486 /* Assume that it's not a pointer-to-member. */
20487 *type = NULL_TREE;
20488 /* And that there are no cv-qualifiers. */
20489 *cv_quals = TYPE_UNQUALIFIED;
20491 /* Peek at the next token. */
20492 token = cp_lexer_peek_token (parser->lexer);
20494 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20495 if (token->type == CPP_MULT)
20496 code = INDIRECT_REF;
20497 else if (token->type == CPP_AND)
20498 code = ADDR_EXPR;
20499 else if ((cxx_dialect != cxx98) &&
20500 token->type == CPP_AND_AND) /* C++0x only */
20501 code = NON_LVALUE_EXPR;
20503 if (code != ERROR_MARK)
20505 /* Consume the `*', `&' or `&&'. */
20506 cp_lexer_consume_token (parser->lexer);
20508 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20509 `&', if we are allowing GNU extensions. (The only qualifier
20510 that can legally appear after `&' is `restrict', but that is
20511 enforced during semantic analysis. */
20512 if (code == INDIRECT_REF
20513 || cp_parser_allow_gnu_extensions_p (parser))
20514 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20516 attrs = cp_parser_std_attribute_spec_seq (parser);
20517 if (attributes != NULL)
20518 *attributes = attrs;
20520 else
20522 /* Try the pointer-to-member case. */
20523 cp_parser_parse_tentatively (parser);
20524 /* Look for the optional `::' operator. */
20525 cp_parser_global_scope_opt (parser,
20526 /*current_scope_valid_p=*/false);
20527 /* Look for the nested-name specifier. */
20528 token = cp_lexer_peek_token (parser->lexer);
20529 cp_parser_nested_name_specifier (parser,
20530 /*typename_keyword_p=*/false,
20531 /*check_dependency_p=*/true,
20532 /*type_p=*/false,
20533 /*is_declaration=*/false);
20534 /* If we found it, and the next token is a `*', then we are
20535 indeed looking at a pointer-to-member operator. */
20536 if (!cp_parser_error_occurred (parser)
20537 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20539 /* Indicate that the `*' operator was used. */
20540 code = INDIRECT_REF;
20542 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20543 error_at (token->location, "%qD is a namespace", parser->scope);
20544 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20545 error_at (token->location, "cannot form pointer to member of "
20546 "non-class %q#T", parser->scope);
20547 else
20549 /* The type of which the member is a member is given by the
20550 current SCOPE. */
20551 *type = parser->scope;
20552 /* The next name will not be qualified. */
20553 parser->scope = NULL_TREE;
20554 parser->qualifying_scope = NULL_TREE;
20555 parser->object_scope = NULL_TREE;
20556 /* Look for optional c++11 attributes. */
20557 attrs = cp_parser_std_attribute_spec_seq (parser);
20558 if (attributes != NULL)
20559 *attributes = attrs;
20560 /* Look for the optional cv-qualifier-seq. */
20561 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20564 /* If that didn't work we don't have a ptr-operator. */
20565 if (!cp_parser_parse_definitely (parser))
20566 cp_parser_error (parser, "expected ptr-operator");
20569 return code;
20572 /* Parse an (optional) cv-qualifier-seq.
20574 cv-qualifier-seq:
20575 cv-qualifier cv-qualifier-seq [opt]
20577 cv-qualifier:
20578 const
20579 volatile
20581 GNU Extension:
20583 cv-qualifier:
20584 __restrict__
20586 Returns a bitmask representing the cv-qualifiers. */
20588 static cp_cv_quals
20589 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20591 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20593 while (true)
20595 cp_token *token;
20596 cp_cv_quals cv_qualifier;
20598 /* Peek at the next token. */
20599 token = cp_lexer_peek_token (parser->lexer);
20600 /* See if it's a cv-qualifier. */
20601 switch (token->keyword)
20603 case RID_CONST:
20604 cv_qualifier = TYPE_QUAL_CONST;
20605 break;
20607 case RID_VOLATILE:
20608 cv_qualifier = TYPE_QUAL_VOLATILE;
20609 break;
20611 case RID_RESTRICT:
20612 cv_qualifier = TYPE_QUAL_RESTRICT;
20613 break;
20615 default:
20616 cv_qualifier = TYPE_UNQUALIFIED;
20617 break;
20620 if (!cv_qualifier)
20621 break;
20623 if (cv_quals & cv_qualifier)
20625 gcc_rich_location richloc (token->location);
20626 richloc.add_fixit_remove ();
20627 error_at (&richloc, "duplicate cv-qualifier");
20628 cp_lexer_purge_token (parser->lexer);
20630 else
20632 cp_lexer_consume_token (parser->lexer);
20633 cv_quals |= cv_qualifier;
20637 return cv_quals;
20640 /* Parse an (optional) ref-qualifier
20642 ref-qualifier:
20646 Returns cp_ref_qualifier representing ref-qualifier. */
20648 static cp_ref_qualifier
20649 cp_parser_ref_qualifier_opt (cp_parser* parser)
20651 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20653 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20654 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20655 return ref_qual;
20657 while (true)
20659 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20660 cp_token *token = cp_lexer_peek_token (parser->lexer);
20662 switch (token->type)
20664 case CPP_AND:
20665 curr_ref_qual = REF_QUAL_LVALUE;
20666 break;
20668 case CPP_AND_AND:
20669 curr_ref_qual = REF_QUAL_RVALUE;
20670 break;
20672 default:
20673 curr_ref_qual = REF_QUAL_NONE;
20674 break;
20677 if (!curr_ref_qual)
20678 break;
20679 else if (ref_qual)
20681 error_at (token->location, "multiple ref-qualifiers");
20682 cp_lexer_purge_token (parser->lexer);
20684 else
20686 ref_qual = curr_ref_qual;
20687 cp_lexer_consume_token (parser->lexer);
20691 return ref_qual;
20694 /* Parse an optional tx-qualifier.
20696 tx-qualifier:
20697 transaction_safe
20698 transaction_safe_dynamic */
20700 static tree
20701 cp_parser_tx_qualifier_opt (cp_parser *parser)
20703 cp_token *token = cp_lexer_peek_token (parser->lexer);
20704 if (token->type == CPP_NAME)
20706 tree name = token->u.value;
20707 const char *p = IDENTIFIER_POINTER (name);
20708 const int len = strlen ("transaction_safe");
20709 if (!strncmp (p, "transaction_safe", len))
20711 p += len;
20712 if (*p == '\0'
20713 || !strcmp (p, "_dynamic"))
20715 cp_lexer_consume_token (parser->lexer);
20716 if (!flag_tm)
20718 error ("%qE requires %<-fgnu-tm%>", name);
20719 return NULL_TREE;
20721 else
20722 return name;
20726 return NULL_TREE;
20729 /* Parse an (optional) virt-specifier-seq.
20731 virt-specifier-seq:
20732 virt-specifier virt-specifier-seq [opt]
20734 virt-specifier:
20735 override
20736 final
20738 Returns a bitmask representing the virt-specifiers. */
20740 static cp_virt_specifiers
20741 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20743 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20745 while (true)
20747 cp_token *token;
20748 cp_virt_specifiers virt_specifier;
20750 /* Peek at the next token. */
20751 token = cp_lexer_peek_token (parser->lexer);
20752 /* See if it's a virt-specifier-qualifier. */
20753 if (token->type != CPP_NAME)
20754 break;
20755 if (id_equal (token->u.value, "override"))
20757 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20758 virt_specifier = VIRT_SPEC_OVERRIDE;
20760 else if (id_equal (token->u.value, "final"))
20762 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20763 virt_specifier = VIRT_SPEC_FINAL;
20765 else if (id_equal (token->u.value, "__final"))
20767 virt_specifier = VIRT_SPEC_FINAL;
20769 else
20770 break;
20772 if (virt_specifiers & virt_specifier)
20774 gcc_rich_location richloc (token->location);
20775 richloc.add_fixit_remove ();
20776 error_at (&richloc, "duplicate virt-specifier");
20777 cp_lexer_purge_token (parser->lexer);
20779 else
20781 cp_lexer_consume_token (parser->lexer);
20782 virt_specifiers |= virt_specifier;
20785 return virt_specifiers;
20788 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20789 is in scope even though it isn't real. */
20791 void
20792 inject_this_parameter (tree ctype, cp_cv_quals quals)
20794 tree this_parm;
20796 if (current_class_ptr)
20798 /* We don't clear this between NSDMIs. Is it already what we want? */
20799 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20800 if (DECL_P (current_class_ptr)
20801 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20802 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20803 && cp_type_quals (type) == quals)
20804 return;
20807 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20808 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20809 current_class_ptr = NULL_TREE;
20810 current_class_ref
20811 = cp_build_fold_indirect_ref (this_parm);
20812 current_class_ptr = this_parm;
20815 /* Return true iff our current scope is a non-static data member
20816 initializer. */
20818 bool
20819 parsing_nsdmi (void)
20821 /* We recognize NSDMI context by the context-less 'this' pointer set up
20822 by the function above. */
20823 if (current_class_ptr
20824 && TREE_CODE (current_class_ptr) == PARM_DECL
20825 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20826 return true;
20827 return false;
20830 /* Parse a late-specified return type, if any. This is not a separate
20831 non-terminal, but part of a function declarator, which looks like
20833 -> trailing-type-specifier-seq abstract-declarator(opt)
20835 Returns the type indicated by the type-id.
20837 In addition to this, parse any queued up #pragma omp declare simd
20838 clauses, and #pragma acc routine clauses.
20840 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20841 function. */
20843 static tree
20844 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20845 tree& requires_clause, cp_cv_quals quals)
20847 cp_token *token;
20848 tree type = NULL_TREE;
20849 bool declare_simd_p = (parser->omp_declare_simd
20850 && declarator
20851 && declarator->kind == cdk_id);
20853 bool oacc_routine_p = (parser->oacc_routine
20854 && declarator
20855 && declarator->kind == cdk_id);
20857 /* Peek at the next token. */
20858 token = cp_lexer_peek_token (parser->lexer);
20859 /* A late-specified return type is indicated by an initial '->'. */
20860 if (token->type != CPP_DEREF
20861 && token->keyword != RID_REQUIRES
20862 && !(token->type == CPP_NAME
20863 && token->u.value == ridpointers[RID_REQUIRES])
20864 && !(declare_simd_p || oacc_routine_p))
20865 return NULL_TREE;
20867 tree save_ccp = current_class_ptr;
20868 tree save_ccr = current_class_ref;
20869 if (quals >= 0)
20871 /* DR 1207: 'this' is in scope in the trailing return type. */
20872 inject_this_parameter (current_class_type, quals);
20875 if (token->type == CPP_DEREF)
20877 /* Consume the ->. */
20878 cp_lexer_consume_token (parser->lexer);
20880 type = cp_parser_trailing_type_id (parser);
20883 /* Function declarations may be followed by a trailing
20884 requires-clause. */
20885 requires_clause = cp_parser_requires_clause_opt (parser);
20887 if (declare_simd_p)
20888 declarator->attributes
20889 = cp_parser_late_parsing_omp_declare_simd (parser,
20890 declarator->attributes);
20891 if (oacc_routine_p)
20892 declarator->attributes
20893 = cp_parser_late_parsing_oacc_routine (parser,
20894 declarator->attributes);
20896 if (quals >= 0)
20898 current_class_ptr = save_ccp;
20899 current_class_ref = save_ccr;
20902 return type;
20905 /* Parse a declarator-id.
20907 declarator-id:
20908 id-expression
20909 :: [opt] nested-name-specifier [opt] type-name
20911 In the `id-expression' case, the value returned is as for
20912 cp_parser_id_expression if the id-expression was an unqualified-id.
20913 If the id-expression was a qualified-id, then a SCOPE_REF is
20914 returned. The first operand is the scope (either a NAMESPACE_DECL
20915 or TREE_TYPE), but the second is still just a representation of an
20916 unqualified-id. */
20918 static tree
20919 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20921 tree id;
20922 /* The expression must be an id-expression. Assume that qualified
20923 names are the names of types so that:
20925 template <class T>
20926 int S<T>::R::i = 3;
20928 will work; we must treat `S<T>::R' as the name of a type.
20929 Similarly, assume that qualified names are templates, where
20930 required, so that:
20932 template <class T>
20933 int S<T>::R<T>::i = 3;
20935 will work, too. */
20936 id = cp_parser_id_expression (parser,
20937 /*template_keyword_p=*/false,
20938 /*check_dependency_p=*/false,
20939 /*template_p=*/NULL,
20940 /*declarator_p=*/true,
20941 optional_p);
20942 if (id && BASELINK_P (id))
20943 id = BASELINK_FUNCTIONS (id);
20944 return id;
20947 /* Parse a type-id.
20949 type-id:
20950 type-specifier-seq abstract-declarator [opt]
20952 Returns the TYPE specified. */
20954 static tree
20955 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20956 bool is_trailing_return)
20958 cp_decl_specifier_seq type_specifier_seq;
20959 cp_declarator *abstract_declarator;
20961 /* Parse the type-specifier-seq. */
20962 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20963 is_trailing_return,
20964 &type_specifier_seq);
20965 if (is_template_arg && type_specifier_seq.type
20966 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20967 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20968 /* A bare template name as a template argument is a template template
20969 argument, not a placeholder, so fail parsing it as a type argument. */
20971 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20972 cp_parser_simulate_error (parser);
20973 return error_mark_node;
20975 if (type_specifier_seq.type == error_mark_node)
20976 return error_mark_node;
20978 /* There might or might not be an abstract declarator. */
20979 cp_parser_parse_tentatively (parser);
20980 /* Look for the declarator. */
20981 abstract_declarator
20982 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20983 /*parenthesized_p=*/NULL,
20984 /*member_p=*/false,
20985 /*friend_p=*/false);
20986 /* Check to see if there really was a declarator. */
20987 if (!cp_parser_parse_definitely (parser))
20988 abstract_declarator = NULL;
20990 if (type_specifier_seq.type
20991 /* The concepts TS allows 'auto' as a type-id. */
20992 && (!flag_concepts || parser->in_type_id_in_expr_p)
20993 /* None of the valid uses of 'auto' in C++14 involve the type-id
20994 nonterminal, but it is valid in a trailing-return-type. */
20995 && !(cxx_dialect >= cxx14 && is_trailing_return))
20996 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20998 /* A type-id with type 'auto' is only ok if the abstract declarator
20999 is a function declarator with a late-specified return type.
21001 A type-id with 'auto' is also valid in a trailing-return-type
21002 in a compound-requirement. */
21003 if (abstract_declarator
21004 && abstract_declarator->kind == cdk_function
21005 && abstract_declarator->u.function.late_return_type)
21006 /* OK */;
21007 else if (parser->in_result_type_constraint_p)
21008 /* OK */;
21009 else
21011 location_t loc = type_specifier_seq.locations[ds_type_spec];
21012 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21014 error_at (loc, "missing template arguments after %qT",
21015 auto_node);
21016 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21017 tmpl);
21019 else
21020 error_at (loc, "invalid use of %qT", auto_node);
21021 return error_mark_node;
21025 return groktypename (&type_specifier_seq, abstract_declarator,
21026 is_template_arg);
21029 static tree
21030 cp_parser_type_id (cp_parser *parser)
21032 return cp_parser_type_id_1 (parser, false, false);
21035 static tree
21036 cp_parser_template_type_arg (cp_parser *parser)
21038 tree r;
21039 const char *saved_message = parser->type_definition_forbidden_message;
21040 parser->type_definition_forbidden_message
21041 = G_("types may not be defined in template arguments");
21042 r = cp_parser_type_id_1 (parser, true, false);
21043 parser->type_definition_forbidden_message = saved_message;
21044 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21046 error ("invalid use of %<auto%> in template argument");
21047 r = error_mark_node;
21049 return r;
21052 static tree
21053 cp_parser_trailing_type_id (cp_parser *parser)
21055 return cp_parser_type_id_1 (parser, false, true);
21058 /* Parse a type-specifier-seq.
21060 type-specifier-seq:
21061 type-specifier type-specifier-seq [opt]
21063 GNU extension:
21065 type-specifier-seq:
21066 attributes type-specifier-seq [opt]
21068 If IS_DECLARATION is true, we are at the start of a "condition" or
21069 exception-declaration, so we might be followed by a declarator-id.
21071 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21072 i.e. we've just seen "->".
21074 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21076 static void
21077 cp_parser_type_specifier_seq (cp_parser* parser,
21078 bool is_declaration,
21079 bool is_trailing_return,
21080 cp_decl_specifier_seq *type_specifier_seq)
21082 bool seen_type_specifier = false;
21083 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21084 cp_token *start_token = NULL;
21086 /* Clear the TYPE_SPECIFIER_SEQ. */
21087 clear_decl_specs (type_specifier_seq);
21089 /* In the context of a trailing return type, enum E { } is an
21090 elaborated-type-specifier followed by a function-body, not an
21091 enum-specifier. */
21092 if (is_trailing_return)
21093 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21095 /* Parse the type-specifiers and attributes. */
21096 while (true)
21098 tree type_specifier;
21099 bool is_cv_qualifier;
21101 /* Check for attributes first. */
21102 if (cp_next_tokens_can_be_attribute_p (parser))
21104 type_specifier_seq->attributes
21105 = attr_chainon (type_specifier_seq->attributes,
21106 cp_parser_attributes_opt (parser));
21107 continue;
21110 /* record the token of the beginning of the type specifier seq,
21111 for error reporting purposes*/
21112 if (!start_token)
21113 start_token = cp_lexer_peek_token (parser->lexer);
21115 /* Look for the type-specifier. */
21116 type_specifier = cp_parser_type_specifier (parser,
21117 flags,
21118 type_specifier_seq,
21119 /*is_declaration=*/false,
21120 NULL,
21121 &is_cv_qualifier);
21122 if (!type_specifier)
21124 /* If the first type-specifier could not be found, this is not a
21125 type-specifier-seq at all. */
21126 if (!seen_type_specifier)
21128 /* Set in_declarator_p to avoid skipping to the semicolon. */
21129 int in_decl = parser->in_declarator_p;
21130 parser->in_declarator_p = true;
21132 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21133 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21134 cp_parser_error (parser, "expected type-specifier");
21136 parser->in_declarator_p = in_decl;
21138 type_specifier_seq->type = error_mark_node;
21139 return;
21141 /* If subsequent type-specifiers could not be found, the
21142 type-specifier-seq is complete. */
21143 break;
21146 seen_type_specifier = true;
21147 /* The standard says that a condition can be:
21149 type-specifier-seq declarator = assignment-expression
21151 However, given:
21153 struct S {};
21154 if (int S = ...)
21156 we should treat the "S" as a declarator, not as a
21157 type-specifier. The standard doesn't say that explicitly for
21158 type-specifier-seq, but it does say that for
21159 decl-specifier-seq in an ordinary declaration. Perhaps it
21160 would be clearer just to allow a decl-specifier-seq here, and
21161 then add a semantic restriction that if any decl-specifiers
21162 that are not type-specifiers appear, the program is invalid. */
21163 if (is_declaration && !is_cv_qualifier)
21164 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21168 /* Return whether the function currently being declared has an associated
21169 template parameter list. */
21171 static bool
21172 function_being_declared_is_template_p (cp_parser* parser)
21174 if (!current_template_parms || processing_template_parmlist)
21175 return false;
21177 if (parser->implicit_template_scope)
21178 return true;
21180 if (at_class_scope_p ()
21181 && TYPE_BEING_DEFINED (current_class_type))
21182 return parser->num_template_parameter_lists != 0;
21184 return ((int) parser->num_template_parameter_lists > template_class_depth
21185 (current_class_type));
21188 /* Parse a parameter-declaration-clause.
21190 parameter-declaration-clause:
21191 parameter-declaration-list [opt] ... [opt]
21192 parameter-declaration-list , ...
21194 Returns a representation for the parameter declarations. A return
21195 value of NULL indicates a parameter-declaration-clause consisting
21196 only of an ellipsis. */
21198 static tree
21199 cp_parser_parameter_declaration_clause (cp_parser* parser)
21201 tree parameters;
21202 cp_token *token;
21203 bool ellipsis_p;
21204 bool is_error;
21206 temp_override<bool> cleanup
21207 (parser->auto_is_implicit_function_template_parm_p);
21209 if (!processing_specialization
21210 && !processing_template_parmlist
21211 && !processing_explicit_instantiation
21212 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21213 actual function or a random abstract declarator. */
21214 && parser->default_arg_ok_p)
21215 if (!current_function_decl
21216 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21217 parser->auto_is_implicit_function_template_parm_p = true;
21219 /* Peek at the next token. */
21220 token = cp_lexer_peek_token (parser->lexer);
21221 /* Check for trivial parameter-declaration-clauses. */
21222 if (token->type == CPP_ELLIPSIS)
21224 /* Consume the `...' token. */
21225 cp_lexer_consume_token (parser->lexer);
21226 return NULL_TREE;
21228 else if (token->type == CPP_CLOSE_PAREN)
21229 /* There are no parameters. */
21231 #ifndef NO_IMPLICIT_EXTERN_C
21232 if (in_system_header_at (input_location)
21233 && current_class_type == NULL
21234 && current_lang_name == lang_name_c)
21235 return NULL_TREE;
21236 else
21237 #endif
21238 return void_list_node;
21240 /* Check for `(void)', too, which is a special case. */
21241 else if (token->keyword == RID_VOID
21242 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21243 == CPP_CLOSE_PAREN))
21245 /* Consume the `void' token. */
21246 cp_lexer_consume_token (parser->lexer);
21247 /* There are no parameters. */
21248 return void_list_node;
21251 /* Parse the parameter-declaration-list. */
21252 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21253 /* If a parse error occurred while parsing the
21254 parameter-declaration-list, then the entire
21255 parameter-declaration-clause is erroneous. */
21256 if (is_error)
21257 return NULL;
21259 /* Peek at the next token. */
21260 token = cp_lexer_peek_token (parser->lexer);
21261 /* If it's a `,', the clause should terminate with an ellipsis. */
21262 if (token->type == CPP_COMMA)
21264 /* Consume the `,'. */
21265 cp_lexer_consume_token (parser->lexer);
21266 /* Expect an ellipsis. */
21267 ellipsis_p
21268 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21270 /* It might also be `...' if the optional trailing `,' was
21271 omitted. */
21272 else if (token->type == CPP_ELLIPSIS)
21274 /* Consume the `...' token. */
21275 cp_lexer_consume_token (parser->lexer);
21276 /* And remember that we saw it. */
21277 ellipsis_p = true;
21279 else
21280 ellipsis_p = false;
21282 /* Finish the parameter list. */
21283 if (!ellipsis_p)
21284 parameters = chainon (parameters, void_list_node);
21286 return parameters;
21289 /* Parse a parameter-declaration-list.
21291 parameter-declaration-list:
21292 parameter-declaration
21293 parameter-declaration-list , parameter-declaration
21295 Returns a representation of the parameter-declaration-list, as for
21296 cp_parser_parameter_declaration_clause. However, the
21297 `void_list_node' is never appended to the list. Upon return,
21298 *IS_ERROR will be true iff an error occurred. */
21300 static tree
21301 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21303 tree parameters = NULL_TREE;
21304 tree *tail = &parameters;
21305 bool saved_in_unbraced_linkage_specification_p;
21306 int index = 0;
21308 /* Assume all will go well. */
21309 *is_error = false;
21310 /* The special considerations that apply to a function within an
21311 unbraced linkage specifications do not apply to the parameters
21312 to the function. */
21313 saved_in_unbraced_linkage_specification_p
21314 = parser->in_unbraced_linkage_specification_p;
21315 parser->in_unbraced_linkage_specification_p = false;
21317 /* Look for more parameters. */
21318 while (true)
21320 cp_parameter_declarator *parameter;
21321 tree decl = error_mark_node;
21322 bool parenthesized_p = false;
21323 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21324 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21325 (current_template_parms)) : 0);
21327 /* Parse the parameter. */
21328 parameter
21329 = cp_parser_parameter_declaration (parser,
21330 /*template_parm_p=*/false,
21331 &parenthesized_p);
21333 /* We don't know yet if the enclosing context is deprecated, so wait
21334 and warn in grokparms if appropriate. */
21335 deprecated_state = DEPRECATED_SUPPRESS;
21337 if (parameter)
21339 /* If a function parameter pack was specified and an implicit template
21340 parameter was introduced during cp_parser_parameter_declaration,
21341 change any implicit parameters introduced into packs. */
21342 if (parser->implicit_template_parms
21343 && parameter->declarator
21344 && parameter->declarator->parameter_pack_p)
21346 int latest_template_parm_idx = TREE_VEC_LENGTH
21347 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21349 if (latest_template_parm_idx != template_parm_idx)
21350 parameter->decl_specifiers.type = convert_generic_types_to_packs
21351 (parameter->decl_specifiers.type,
21352 template_parm_idx, latest_template_parm_idx);
21355 decl = grokdeclarator (parameter->declarator,
21356 &parameter->decl_specifiers,
21357 PARM,
21358 parameter->default_argument != NULL_TREE,
21359 &parameter->decl_specifiers.attributes);
21360 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21361 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21364 deprecated_state = DEPRECATED_NORMAL;
21366 /* If a parse error occurred parsing the parameter declaration,
21367 then the entire parameter-declaration-list is erroneous. */
21368 if (decl == error_mark_node)
21370 *is_error = true;
21371 parameters = error_mark_node;
21372 break;
21375 if (parameter->decl_specifiers.attributes)
21376 cplus_decl_attributes (&decl,
21377 parameter->decl_specifiers.attributes,
21379 if (DECL_NAME (decl))
21380 decl = pushdecl (decl);
21382 if (decl != error_mark_node)
21384 retrofit_lang_decl (decl);
21385 DECL_PARM_INDEX (decl) = ++index;
21386 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21389 /* Add the new parameter to the list. */
21390 *tail = build_tree_list (parameter->default_argument, decl);
21391 tail = &TREE_CHAIN (*tail);
21393 /* Peek at the next token. */
21394 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21395 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21396 /* These are for Objective-C++ */
21397 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21398 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21399 /* The parameter-declaration-list is complete. */
21400 break;
21401 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21403 cp_token *token;
21405 /* Peek at the next token. */
21406 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21407 /* If it's an ellipsis, then the list is complete. */
21408 if (token->type == CPP_ELLIPSIS)
21409 break;
21410 /* Otherwise, there must be more parameters. Consume the
21411 `,'. */
21412 cp_lexer_consume_token (parser->lexer);
21413 /* When parsing something like:
21415 int i(float f, double d)
21417 we can tell after seeing the declaration for "f" that we
21418 are not looking at an initialization of a variable "i",
21419 but rather at the declaration of a function "i".
21421 Due to the fact that the parsing of template arguments
21422 (as specified to a template-id) requires backtracking we
21423 cannot use this technique when inside a template argument
21424 list. */
21425 if (!parser->in_template_argument_list_p
21426 && !parser->in_type_id_in_expr_p
21427 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21428 /* However, a parameter-declaration of the form
21429 "float(f)" (which is a valid declaration of a
21430 parameter "f") can also be interpreted as an
21431 expression (the conversion of "f" to "float"). */
21432 && !parenthesized_p)
21433 cp_parser_commit_to_tentative_parse (parser);
21435 else
21437 cp_parser_error (parser, "expected %<,%> or %<...%>");
21438 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21439 cp_parser_skip_to_closing_parenthesis (parser,
21440 /*recovering=*/true,
21441 /*or_comma=*/false,
21442 /*consume_paren=*/false);
21443 break;
21447 parser->in_unbraced_linkage_specification_p
21448 = saved_in_unbraced_linkage_specification_p;
21450 /* Reset implicit_template_scope if we are about to leave the function
21451 parameter list that introduced it. Note that for out-of-line member
21452 definitions, there will be one or more class scopes before we get to
21453 the template parameter scope. */
21455 if (cp_binding_level *its = parser->implicit_template_scope)
21456 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21458 while (maybe_its->kind == sk_class)
21459 maybe_its = maybe_its->level_chain;
21460 if (maybe_its == its)
21462 parser->implicit_template_parms = 0;
21463 parser->implicit_template_scope = 0;
21467 return parameters;
21470 /* Parse a parameter declaration.
21472 parameter-declaration:
21473 decl-specifier-seq ... [opt] declarator
21474 decl-specifier-seq declarator = assignment-expression
21475 decl-specifier-seq ... [opt] abstract-declarator [opt]
21476 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21478 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21479 declares a template parameter. (In that case, a non-nested `>'
21480 token encountered during the parsing of the assignment-expression
21481 is not interpreted as a greater-than operator.)
21483 Returns a representation of the parameter, or NULL if an error
21484 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21485 true iff the declarator is of the form "(p)". */
21487 static cp_parameter_declarator *
21488 cp_parser_parameter_declaration (cp_parser *parser,
21489 bool template_parm_p,
21490 bool *parenthesized_p)
21492 int declares_class_or_enum;
21493 cp_decl_specifier_seq decl_specifiers;
21494 cp_declarator *declarator;
21495 tree default_argument;
21496 cp_token *token = NULL, *declarator_token_start = NULL;
21497 const char *saved_message;
21498 bool template_parameter_pack_p = false;
21500 /* In a template parameter, `>' is not an operator.
21502 [temp.param]
21504 When parsing a default template-argument for a non-type
21505 template-parameter, the first non-nested `>' is taken as the end
21506 of the template parameter-list rather than a greater-than
21507 operator. */
21509 /* Type definitions may not appear in parameter types. */
21510 saved_message = parser->type_definition_forbidden_message;
21511 parser->type_definition_forbidden_message
21512 = G_("types may not be defined in parameter types");
21514 /* Parse the declaration-specifiers. */
21515 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21516 cp_parser_decl_specifier_seq (parser,
21517 CP_PARSER_FLAGS_NONE,
21518 &decl_specifiers,
21519 &declares_class_or_enum);
21521 /* Complain about missing 'typename' or other invalid type names. */
21522 if (!decl_specifiers.any_type_specifiers_p
21523 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21524 decl_specifiers.type = error_mark_node;
21526 /* If an error occurred, there's no reason to attempt to parse the
21527 rest of the declaration. */
21528 if (cp_parser_error_occurred (parser))
21530 parser->type_definition_forbidden_message = saved_message;
21531 return NULL;
21534 /* Peek at the next token. */
21535 token = cp_lexer_peek_token (parser->lexer);
21537 /* If the next token is a `)', `,', `=', `>', or `...', then there
21538 is no declarator. However, when variadic templates are enabled,
21539 there may be a declarator following `...'. */
21540 if (token->type == CPP_CLOSE_PAREN
21541 || token->type == CPP_COMMA
21542 || token->type == CPP_EQ
21543 || token->type == CPP_GREATER)
21545 declarator = NULL;
21546 if (parenthesized_p)
21547 *parenthesized_p = false;
21549 /* Otherwise, there should be a declarator. */
21550 else
21552 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21553 parser->default_arg_ok_p = false;
21555 /* After seeing a decl-specifier-seq, if the next token is not a
21556 "(", there is no possibility that the code is a valid
21557 expression. Therefore, if parsing tentatively, we commit at
21558 this point. */
21559 if (!parser->in_template_argument_list_p
21560 /* In an expression context, having seen:
21562 (int((char ...
21564 we cannot be sure whether we are looking at a
21565 function-type (taking a "char" as a parameter) or a cast
21566 of some object of type "char" to "int". */
21567 && !parser->in_type_id_in_expr_p
21568 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21569 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21570 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21571 cp_parser_commit_to_tentative_parse (parser);
21572 /* Parse the declarator. */
21573 declarator_token_start = token;
21574 declarator = cp_parser_declarator (parser,
21575 CP_PARSER_DECLARATOR_EITHER,
21576 /*ctor_dtor_or_conv_p=*/NULL,
21577 parenthesized_p,
21578 /*member_p=*/false,
21579 /*friend_p=*/false);
21580 parser->default_arg_ok_p = saved_default_arg_ok_p;
21581 /* After the declarator, allow more attributes. */
21582 decl_specifiers.attributes
21583 = attr_chainon (decl_specifiers.attributes,
21584 cp_parser_attributes_opt (parser));
21586 /* If the declarator is a template parameter pack, remember that and
21587 clear the flag in the declarator itself so we don't get errors
21588 from grokdeclarator. */
21589 if (template_parm_p && declarator && declarator->parameter_pack_p)
21591 declarator->parameter_pack_p = false;
21592 template_parameter_pack_p = true;
21596 /* If the next token is an ellipsis, and we have not seen a declarator
21597 name, and if either the type of the declarator contains parameter
21598 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21599 for, eg, abbreviated integral type names), then we actually have a
21600 parameter pack expansion expression. Otherwise, leave the ellipsis
21601 for a C-style variadic function. */
21602 token = cp_lexer_peek_token (parser->lexer);
21603 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21605 tree type = decl_specifiers.type;
21607 if (type && DECL_P (type))
21608 type = TREE_TYPE (type);
21610 if (((type
21611 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21612 && (template_parm_p || uses_parameter_packs (type)))
21613 || (!type && template_parm_p))
21614 && declarator_can_be_parameter_pack (declarator))
21616 /* Consume the `...'. */
21617 cp_lexer_consume_token (parser->lexer);
21618 maybe_warn_variadic_templates ();
21620 /* Build a pack expansion type */
21621 if (template_parm_p)
21622 template_parameter_pack_p = true;
21623 else if (declarator)
21624 declarator->parameter_pack_p = true;
21625 else
21626 decl_specifiers.type = make_pack_expansion (type);
21630 /* The restriction on defining new types applies only to the type
21631 of the parameter, not to the default argument. */
21632 parser->type_definition_forbidden_message = saved_message;
21634 /* If the next token is `=', then process a default argument. */
21635 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21637 tree type = decl_specifiers.type;
21638 token = cp_lexer_peek_token (parser->lexer);
21639 /* If we are defining a class, then the tokens that make up the
21640 default argument must be saved and processed later. */
21641 if (!template_parm_p && at_class_scope_p ()
21642 && TYPE_BEING_DEFINED (current_class_type)
21643 && !LAMBDA_TYPE_P (current_class_type))
21644 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21646 // A constrained-type-specifier may declare a type template-parameter.
21647 else if (declares_constrained_type_template_parameter (type))
21648 default_argument
21649 = cp_parser_default_type_template_argument (parser);
21651 // A constrained-type-specifier may declare a template-template-parameter.
21652 else if (declares_constrained_template_template_parameter (type))
21653 default_argument
21654 = cp_parser_default_template_template_argument (parser);
21656 /* Outside of a class definition, we can just parse the
21657 assignment-expression. */
21658 else
21659 default_argument
21660 = cp_parser_default_argument (parser, template_parm_p);
21662 if (!parser->default_arg_ok_p)
21664 permerror (token->location,
21665 "default arguments are only "
21666 "permitted for function parameters");
21668 else if ((declarator && declarator->parameter_pack_p)
21669 || template_parameter_pack_p
21670 || (decl_specifiers.type
21671 && PACK_EXPANSION_P (decl_specifiers.type)))
21673 /* Find the name of the parameter pack. */
21674 cp_declarator *id_declarator = declarator;
21675 while (id_declarator && id_declarator->kind != cdk_id)
21676 id_declarator = id_declarator->declarator;
21678 if (id_declarator && id_declarator->kind == cdk_id)
21679 error_at (declarator_token_start->location,
21680 template_parm_p
21681 ? G_("template parameter pack %qD "
21682 "cannot have a default argument")
21683 : G_("parameter pack %qD cannot have "
21684 "a default argument"),
21685 id_declarator->u.id.unqualified_name);
21686 else
21687 error_at (declarator_token_start->location,
21688 template_parm_p
21689 ? G_("template parameter pack cannot have "
21690 "a default argument")
21691 : G_("parameter pack cannot have a "
21692 "default argument"));
21694 default_argument = NULL_TREE;
21697 else
21698 default_argument = NULL_TREE;
21700 /* Generate a location for the parameter, ranging from the start of the
21701 initial token to the end of the final token (using input_location for
21702 the latter, set up by cp_lexer_set_source_position_from_token when
21703 consuming tokens).
21705 If we have a identifier, then use it for the caret location, e.g.
21707 extern int callee (int one, int (*two)(int, int), float three);
21708 ~~~~~~^~~~~~~~~~~~~~
21710 otherwise, reuse the start location for the caret location e.g.:
21712 extern int callee (int one, int (*)(int, int), float three);
21713 ^~~~~~~~~~~~~~~~~
21716 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21717 ? declarator->id_loc
21718 : decl_spec_token_start->location);
21719 location_t param_loc = make_location (caret_loc,
21720 decl_spec_token_start->location,
21721 input_location);
21723 return make_parameter_declarator (&decl_specifiers,
21724 declarator,
21725 default_argument,
21726 param_loc,
21727 template_parameter_pack_p);
21730 /* Parse a default argument and return it.
21732 TEMPLATE_PARM_P is true if this is a default argument for a
21733 non-type template parameter. */
21734 static tree
21735 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21737 tree default_argument = NULL_TREE;
21738 bool saved_greater_than_is_operator_p;
21739 bool saved_local_variables_forbidden_p;
21740 bool non_constant_p, is_direct_init;
21742 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21743 set correctly. */
21744 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21745 parser->greater_than_is_operator_p = !template_parm_p;
21746 /* Local variable names (and the `this' keyword) may not
21747 appear in a default argument. */
21748 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21749 parser->local_variables_forbidden_p = true;
21750 /* Parse the assignment-expression. */
21751 if (template_parm_p)
21752 push_deferring_access_checks (dk_no_deferred);
21753 tree saved_class_ptr = NULL_TREE;
21754 tree saved_class_ref = NULL_TREE;
21755 /* The "this" pointer is not valid in a default argument. */
21756 if (cfun)
21758 saved_class_ptr = current_class_ptr;
21759 cp_function_chain->x_current_class_ptr = NULL_TREE;
21760 saved_class_ref = current_class_ref;
21761 cp_function_chain->x_current_class_ref = NULL_TREE;
21763 default_argument
21764 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21765 /* Restore the "this" pointer. */
21766 if (cfun)
21768 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21769 cp_function_chain->x_current_class_ref = saved_class_ref;
21771 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21772 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21773 if (template_parm_p)
21774 pop_deferring_access_checks ();
21775 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21776 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21778 return default_argument;
21781 /* Parse a function-body.
21783 function-body:
21784 compound_statement */
21786 static void
21787 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21789 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21790 ? BCS_TRY_BLOCK : BCS_NORMAL),
21791 true);
21794 /* Parse a ctor-initializer-opt followed by a function-body. Return
21795 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21796 is true we are parsing a function-try-block. */
21798 static void
21799 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21800 bool in_function_try_block)
21802 tree body, list;
21803 const bool check_body_p =
21804 DECL_CONSTRUCTOR_P (current_function_decl)
21805 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21806 tree last = NULL;
21808 /* Begin the function body. */
21809 body = begin_function_body ();
21810 /* Parse the optional ctor-initializer. */
21811 cp_parser_ctor_initializer_opt (parser);
21813 /* If we're parsing a constexpr constructor definition, we need
21814 to check that the constructor body is indeed empty. However,
21815 before we get to cp_parser_function_body lot of junk has been
21816 generated, so we can't just check that we have an empty block.
21817 Rather we take a snapshot of the outermost block, and check whether
21818 cp_parser_function_body changed its state. */
21819 if (check_body_p)
21821 list = cur_stmt_list;
21822 if (STATEMENT_LIST_TAIL (list))
21823 last = STATEMENT_LIST_TAIL (list)->stmt;
21825 /* Parse the function-body. */
21826 cp_parser_function_body (parser, in_function_try_block);
21827 if (check_body_p)
21828 check_constexpr_ctor_body (last, list, /*complain=*/true);
21829 /* Finish the function body. */
21830 finish_function_body (body);
21833 /* Parse an initializer.
21835 initializer:
21836 = initializer-clause
21837 ( expression-list )
21839 Returns an expression representing the initializer. If no
21840 initializer is present, NULL_TREE is returned.
21842 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21843 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21844 set to TRUE if there is no initializer present. If there is an
21845 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21846 is set to true; otherwise it is set to false. */
21848 static tree
21849 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21850 bool* non_constant_p)
21852 cp_token *token;
21853 tree init;
21855 /* Peek at the next token. */
21856 token = cp_lexer_peek_token (parser->lexer);
21858 /* Let our caller know whether or not this initializer was
21859 parenthesized. */
21860 *is_direct_init = (token->type != CPP_EQ);
21861 /* Assume that the initializer is constant. */
21862 *non_constant_p = false;
21864 if (token->type == CPP_EQ)
21866 /* Consume the `='. */
21867 cp_lexer_consume_token (parser->lexer);
21868 /* Parse the initializer-clause. */
21869 init = cp_parser_initializer_clause (parser, non_constant_p);
21871 else if (token->type == CPP_OPEN_PAREN)
21873 vec<tree, va_gc> *vec;
21874 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21875 /*cast_p=*/false,
21876 /*allow_expansion_p=*/true,
21877 non_constant_p);
21878 if (vec == NULL)
21879 return error_mark_node;
21880 init = build_tree_list_vec (vec);
21881 release_tree_vector (vec);
21883 else if (token->type == CPP_OPEN_BRACE)
21885 cp_lexer_set_source_position (parser->lexer);
21886 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21887 init = cp_parser_braced_list (parser, non_constant_p);
21888 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21890 else
21892 /* Anything else is an error. */
21893 cp_parser_error (parser, "expected initializer");
21894 init = error_mark_node;
21897 if (check_for_bare_parameter_packs (init))
21898 init = error_mark_node;
21900 return init;
21903 /* Parse an initializer-clause.
21905 initializer-clause:
21906 assignment-expression
21907 braced-init-list
21909 Returns an expression representing the initializer.
21911 If the `assignment-expression' production is used the value
21912 returned is simply a representation for the expression.
21914 Otherwise, calls cp_parser_braced_list. */
21916 static cp_expr
21917 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21919 cp_expr initializer;
21921 /* Assume the expression is constant. */
21922 *non_constant_p = false;
21924 /* If it is not a `{', then we are looking at an
21925 assignment-expression. */
21926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21928 initializer
21929 = cp_parser_constant_expression (parser,
21930 /*allow_non_constant_p=*/true,
21931 non_constant_p);
21933 else
21934 initializer = cp_parser_braced_list (parser, non_constant_p);
21936 return initializer;
21939 /* Parse a brace-enclosed initializer list.
21941 braced-init-list:
21942 { initializer-list , [opt] }
21943 { designated-initializer-list , [opt] }
21946 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21947 the elements of the initializer-list (or NULL, if the last
21948 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21949 NULL_TREE. There is no way to detect whether or not the optional
21950 trailing `,' was provided. NON_CONSTANT_P is as for
21951 cp_parser_initializer. */
21953 static cp_expr
21954 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21956 tree initializer;
21957 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21959 /* Consume the `{' token. */
21960 matching_braces braces;
21961 braces.require_open (parser);
21962 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21963 initializer = make_node (CONSTRUCTOR);
21964 /* If it's not a `}', then there is a non-trivial initializer. */
21965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21967 /* Parse the initializer list. */
21968 CONSTRUCTOR_ELTS (initializer)
21969 = cp_parser_initializer_list (parser, non_constant_p);
21970 /* A trailing `,' token is allowed. */
21971 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21972 cp_lexer_consume_token (parser->lexer);
21974 else
21975 *non_constant_p = false;
21976 /* Now, there should be a trailing `}'. */
21977 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21978 braces.require_close (parser);
21979 TREE_TYPE (initializer) = init_list_type_node;
21981 cp_expr result (initializer);
21982 /* Build a location of the form:
21983 { ... }
21984 ^~~~~~~
21985 with caret==start at the open brace, finish at the close brace. */
21986 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21987 result.set_location (combined_loc);
21988 return result;
21991 /* Consume tokens up to, and including, the next non-nested closing `]'.
21992 Returns true iff we found a closing `]'. */
21994 static bool
21995 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21997 unsigned square_depth = 0;
21999 while (true)
22001 cp_token * token = cp_lexer_peek_token (parser->lexer);
22003 switch (token->type)
22005 case CPP_EOF:
22006 case CPP_PRAGMA_EOL:
22007 /* If we've run out of tokens, then there is no closing `]'. */
22008 return false;
22010 case CPP_OPEN_SQUARE:
22011 ++square_depth;
22012 break;
22014 case CPP_CLOSE_SQUARE:
22015 if (!square_depth--)
22017 cp_lexer_consume_token (parser->lexer);
22018 return true;
22020 break;
22022 default:
22023 break;
22026 /* Consume the token. */
22027 cp_lexer_consume_token (parser->lexer);
22031 /* Return true if we are looking at an array-designator, false otherwise. */
22033 static bool
22034 cp_parser_array_designator_p (cp_parser *parser)
22036 /* Consume the `['. */
22037 cp_lexer_consume_token (parser->lexer);
22039 cp_lexer_save_tokens (parser->lexer);
22041 /* Skip tokens until the next token is a closing square bracket.
22042 If we find the closing `]', and the next token is a `=', then
22043 we are looking at an array designator. */
22044 bool array_designator_p
22045 = (cp_parser_skip_to_closing_square_bracket (parser)
22046 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22048 /* Roll back the tokens we skipped. */
22049 cp_lexer_rollback_tokens (parser->lexer);
22051 return array_designator_p;
22054 /* Parse an initializer-list.
22056 initializer-list:
22057 initializer-clause ... [opt]
22058 initializer-list , initializer-clause ... [opt]
22060 C++2A Extension:
22062 designated-initializer-list:
22063 designated-initializer-clause
22064 designated-initializer-list , designated-initializer-clause
22066 designated-initializer-clause:
22067 designator brace-or-equal-initializer
22069 designator:
22070 . identifier
22072 GNU Extension:
22074 initializer-list:
22075 designation initializer-clause ...[opt]
22076 initializer-list , designation initializer-clause ...[opt]
22078 designation:
22079 . identifier =
22080 identifier :
22081 [ constant-expression ] =
22083 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22084 for the initializer. If the INDEX of the elt is non-NULL, it is the
22085 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22086 as for cp_parser_initializer. */
22088 static vec<constructor_elt, va_gc> *
22089 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22091 vec<constructor_elt, va_gc> *v = NULL;
22092 bool first_p = true;
22093 tree first_designator = NULL_TREE;
22095 /* Assume all of the expressions are constant. */
22096 *non_constant_p = false;
22098 /* Parse the rest of the list. */
22099 while (true)
22101 cp_token *token;
22102 tree designator;
22103 tree initializer;
22104 bool clause_non_constant_p;
22105 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22107 /* Handle the C++2A syntax, '. id ='. */
22108 if ((cxx_dialect >= cxx2a
22109 || cp_parser_allow_gnu_extensions_p (parser))
22110 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22111 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22112 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22113 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22114 == CPP_OPEN_BRACE)))
22116 if (cxx_dialect < cxx2a)
22117 pedwarn (loc, OPT_Wpedantic,
22118 "C++ designated initializers only available with "
22119 "-std=c++2a or -std=gnu++2a");
22120 /* Consume the `.'. */
22121 cp_lexer_consume_token (parser->lexer);
22122 /* Consume the identifier. */
22123 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22124 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22125 /* Consume the `='. */
22126 cp_lexer_consume_token (parser->lexer);
22128 /* Also, if the next token is an identifier and the following one is a
22129 colon, we are looking at the GNU designated-initializer
22130 syntax. */
22131 else if (cp_parser_allow_gnu_extensions_p (parser)
22132 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22133 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22134 == CPP_COLON))
22136 /* Warn the user that they are using an extension. */
22137 pedwarn (loc, OPT_Wpedantic,
22138 "ISO C++ does not allow GNU designated initializers");
22139 /* Consume the identifier. */
22140 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22141 /* Consume the `:'. */
22142 cp_lexer_consume_token (parser->lexer);
22144 /* Also handle C99 array designators, '[ const ] ='. */
22145 else if (cp_parser_allow_gnu_extensions_p (parser)
22146 && !c_dialect_objc ()
22147 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22149 /* In C++11, [ could start a lambda-introducer. */
22150 bool non_const = false;
22152 cp_parser_parse_tentatively (parser);
22154 if (!cp_parser_array_designator_p (parser))
22156 cp_parser_simulate_error (parser);
22157 designator = NULL_TREE;
22159 else
22161 designator = cp_parser_constant_expression (parser, true,
22162 &non_const);
22163 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22164 cp_parser_require (parser, CPP_EQ, RT_EQ);
22167 if (!cp_parser_parse_definitely (parser))
22168 designator = NULL_TREE;
22169 else if (non_const
22170 && (!require_potential_rvalue_constant_expression
22171 (designator)))
22172 designator = NULL_TREE;
22173 if (designator)
22174 /* Warn the user that they are using an extension. */
22175 pedwarn (loc, OPT_Wpedantic,
22176 "ISO C++ does not allow C99 designated initializers");
22178 else
22179 designator = NULL_TREE;
22181 if (first_p)
22183 first_designator = designator;
22184 first_p = false;
22186 else if (cxx_dialect >= cxx2a
22187 && first_designator != error_mark_node
22188 && (!first_designator != !designator))
22190 error_at (loc, "either all initializer clauses should be designated "
22191 "or none of them should be");
22192 first_designator = error_mark_node;
22194 else if (cxx_dialect < cxx2a && !first_designator)
22195 first_designator = designator;
22197 /* Parse the initializer. */
22198 initializer = cp_parser_initializer_clause (parser,
22199 &clause_non_constant_p);
22200 /* If any clause is non-constant, so is the entire initializer. */
22201 if (clause_non_constant_p)
22202 *non_constant_p = true;
22204 /* If we have an ellipsis, this is an initializer pack
22205 expansion. */
22206 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22208 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22210 /* Consume the `...'. */
22211 cp_lexer_consume_token (parser->lexer);
22213 if (designator && cxx_dialect >= cxx2a)
22214 error_at (loc,
22215 "%<...%> not allowed in designated initializer list");
22217 /* Turn the initializer into an initializer expansion. */
22218 initializer = make_pack_expansion (initializer);
22221 /* Add it to the vector. */
22222 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22224 /* If the next token is not a comma, we have reached the end of
22225 the list. */
22226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22227 break;
22229 /* Peek at the next token. */
22230 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22231 /* If the next token is a `}', then we're still done. An
22232 initializer-clause can have a trailing `,' after the
22233 initializer-list and before the closing `}'. */
22234 if (token->type == CPP_CLOSE_BRACE)
22235 break;
22237 /* Consume the `,' token. */
22238 cp_lexer_consume_token (parser->lexer);
22241 /* The same identifier shall not appear in multiple designators
22242 of a designated-initializer-list. */
22243 if (first_designator)
22245 unsigned int i;
22246 tree designator, val;
22247 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22248 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22250 if (IDENTIFIER_MARKED (designator))
22252 error_at (EXPR_LOC_OR_LOC (val, input_location),
22253 "%<.%s%> designator used multiple times in "
22254 "the same initializer list",
22255 IDENTIFIER_POINTER (designator));
22256 (*v)[i].index = NULL_TREE;
22258 else
22259 IDENTIFIER_MARKED (designator) = 1;
22261 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22262 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22263 IDENTIFIER_MARKED (designator) = 0;
22266 return v;
22269 /* Classes [gram.class] */
22271 /* Parse a class-name.
22273 class-name:
22274 identifier
22275 template-id
22277 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22278 to indicate that names looked up in dependent types should be
22279 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22280 keyword has been used to indicate that the name that appears next
22281 is a template. TAG_TYPE indicates the explicit tag given before
22282 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22283 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22284 is the class being defined in a class-head. If ENUM_OK is TRUE,
22285 enum-names are also accepted.
22287 Returns the TYPE_DECL representing the class. */
22289 static tree
22290 cp_parser_class_name (cp_parser *parser,
22291 bool typename_keyword_p,
22292 bool template_keyword_p,
22293 enum tag_types tag_type,
22294 bool check_dependency_p,
22295 bool class_head_p,
22296 bool is_declaration,
22297 bool enum_ok)
22299 tree decl;
22300 tree scope;
22301 bool typename_p;
22302 cp_token *token;
22303 tree identifier = NULL_TREE;
22305 /* All class-names start with an identifier. */
22306 token = cp_lexer_peek_token (parser->lexer);
22307 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22309 cp_parser_error (parser, "expected class-name");
22310 return error_mark_node;
22313 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22314 to a template-id, so we save it here. */
22315 scope = parser->scope;
22316 if (scope == error_mark_node)
22317 return error_mark_node;
22319 /* Any name names a type if we're following the `typename' keyword
22320 in a qualified name where the enclosing scope is type-dependent. */
22321 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22322 && dependent_type_p (scope));
22323 /* Handle the common case (an identifier, but not a template-id)
22324 efficiently. */
22325 if (token->type == CPP_NAME
22326 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22328 cp_token *identifier_token;
22329 bool ambiguous_p;
22331 /* Look for the identifier. */
22332 identifier_token = cp_lexer_peek_token (parser->lexer);
22333 ambiguous_p = identifier_token->error_reported;
22334 identifier = cp_parser_identifier (parser);
22335 /* If the next token isn't an identifier, we are certainly not
22336 looking at a class-name. */
22337 if (identifier == error_mark_node)
22338 decl = error_mark_node;
22339 /* If we know this is a type-name, there's no need to look it
22340 up. */
22341 else if (typename_p)
22342 decl = identifier;
22343 else
22345 tree ambiguous_decls;
22346 /* If we already know that this lookup is ambiguous, then
22347 we've already issued an error message; there's no reason
22348 to check again. */
22349 if (ambiguous_p)
22351 cp_parser_simulate_error (parser);
22352 return error_mark_node;
22354 /* If the next token is a `::', then the name must be a type
22355 name.
22357 [basic.lookup.qual]
22359 During the lookup for a name preceding the :: scope
22360 resolution operator, object, function, and enumerator
22361 names are ignored. */
22362 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22363 tag_type = scope_type;
22364 /* Look up the name. */
22365 decl = cp_parser_lookup_name (parser, identifier,
22366 tag_type,
22367 /*is_template=*/false,
22368 /*is_namespace=*/false,
22369 check_dependency_p,
22370 &ambiguous_decls,
22371 identifier_token->location);
22372 if (ambiguous_decls)
22374 if (cp_parser_parsing_tentatively (parser))
22375 cp_parser_simulate_error (parser);
22376 return error_mark_node;
22380 else
22382 /* Try a template-id. */
22383 decl = cp_parser_template_id (parser, template_keyword_p,
22384 check_dependency_p,
22385 tag_type,
22386 is_declaration);
22387 if (decl == error_mark_node)
22388 return error_mark_node;
22391 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22393 /* If this is a typename, create a TYPENAME_TYPE. */
22394 if (typename_p && decl != error_mark_node)
22396 decl = make_typename_type (scope, decl, typename_type,
22397 /*complain=*/tf_error);
22398 if (decl != error_mark_node)
22399 decl = TYPE_NAME (decl);
22402 decl = strip_using_decl (decl);
22404 /* Check to see that it is really the name of a class. */
22405 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22406 && identifier_p (TREE_OPERAND (decl, 0))
22407 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22408 /* Situations like this:
22410 template <typename T> struct A {
22411 typename T::template X<int>::I i;
22414 are problematic. Is `T::template X<int>' a class-name? The
22415 standard does not seem to be definitive, but there is no other
22416 valid interpretation of the following `::'. Therefore, those
22417 names are considered class-names. */
22419 decl = make_typename_type (scope, decl, tag_type, tf_error);
22420 if (decl != error_mark_node)
22421 decl = TYPE_NAME (decl);
22423 else if (TREE_CODE (decl) != TYPE_DECL
22424 || TREE_TYPE (decl) == error_mark_node
22425 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22426 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22427 /* In Objective-C 2.0, a classname followed by '.' starts a
22428 dot-syntax expression, and it's not a type-name. */
22429 || (c_dialect_objc ()
22430 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22431 && objc_is_class_name (decl)))
22432 decl = error_mark_node;
22434 if (decl == error_mark_node)
22435 cp_parser_error (parser, "expected class-name");
22436 else if (identifier && !parser->scope)
22437 maybe_note_name_used_in_class (identifier, decl);
22439 return decl;
22442 /* Parse a class-specifier.
22444 class-specifier:
22445 class-head { member-specification [opt] }
22447 Returns the TREE_TYPE representing the class. */
22449 static tree
22450 cp_parser_class_specifier_1 (cp_parser* parser)
22452 tree type;
22453 tree attributes = NULL_TREE;
22454 bool nested_name_specifier_p;
22455 unsigned saved_num_template_parameter_lists;
22456 bool saved_in_function_body;
22457 unsigned char in_statement;
22458 bool in_switch_statement_p;
22459 bool saved_in_unbraced_linkage_specification_p;
22460 tree old_scope = NULL_TREE;
22461 tree scope = NULL_TREE;
22462 cp_token *closing_brace;
22464 push_deferring_access_checks (dk_no_deferred);
22466 /* Parse the class-head. */
22467 type = cp_parser_class_head (parser,
22468 &nested_name_specifier_p);
22469 /* If the class-head was a semantic disaster, skip the entire body
22470 of the class. */
22471 if (!type)
22473 cp_parser_skip_to_end_of_block_or_statement (parser);
22474 pop_deferring_access_checks ();
22475 return error_mark_node;
22478 /* Look for the `{'. */
22479 matching_braces braces;
22480 if (!braces.require_open (parser))
22482 pop_deferring_access_checks ();
22483 return error_mark_node;
22486 cp_ensure_no_omp_declare_simd (parser);
22487 cp_ensure_no_oacc_routine (parser);
22489 /* Issue an error message if type-definitions are forbidden here. */
22490 cp_parser_check_type_definition (parser);
22491 /* Remember that we are defining one more class. */
22492 ++parser->num_classes_being_defined;
22493 /* Inside the class, surrounding template-parameter-lists do not
22494 apply. */
22495 saved_num_template_parameter_lists
22496 = parser->num_template_parameter_lists;
22497 parser->num_template_parameter_lists = 0;
22498 /* We are not in a function body. */
22499 saved_in_function_body = parser->in_function_body;
22500 parser->in_function_body = false;
22501 /* Or in a loop. */
22502 in_statement = parser->in_statement;
22503 parser->in_statement = 0;
22504 /* Or in a switch. */
22505 in_switch_statement_p = parser->in_switch_statement_p;
22506 parser->in_switch_statement_p = false;
22507 /* We are not immediately inside an extern "lang" block. */
22508 saved_in_unbraced_linkage_specification_p
22509 = parser->in_unbraced_linkage_specification_p;
22510 parser->in_unbraced_linkage_specification_p = false;
22512 // Associate constraints with the type.
22513 if (flag_concepts)
22514 type = associate_classtype_constraints (type);
22516 /* Start the class. */
22517 if (nested_name_specifier_p)
22519 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22520 old_scope = push_inner_scope (scope);
22522 type = begin_class_definition (type);
22524 if (type == error_mark_node)
22525 /* If the type is erroneous, skip the entire body of the class. */
22526 cp_parser_skip_to_closing_brace (parser);
22527 else
22528 /* Parse the member-specification. */
22529 cp_parser_member_specification_opt (parser);
22531 /* Look for the trailing `}'. */
22532 closing_brace = braces.require_close (parser);
22533 /* Look for trailing attributes to apply to this class. */
22534 if (cp_parser_allow_gnu_extensions_p (parser))
22535 attributes = cp_parser_gnu_attributes_opt (parser);
22536 if (type != error_mark_node)
22537 type = finish_struct (type, attributes);
22538 if (nested_name_specifier_p)
22539 pop_inner_scope (old_scope, scope);
22541 /* We've finished a type definition. Check for the common syntax
22542 error of forgetting a semicolon after the definition. We need to
22543 be careful, as we can't just check for not-a-semicolon and be done
22544 with it; the user might have typed:
22546 class X { } c = ...;
22547 class X { } *p = ...;
22549 and so forth. Instead, enumerate all the possible tokens that
22550 might follow this production; if we don't see one of them, then
22551 complain and silently insert the semicolon. */
22553 cp_token *token = cp_lexer_peek_token (parser->lexer);
22554 bool want_semicolon = true;
22556 if (cp_next_tokens_can_be_std_attribute_p (parser))
22557 /* Don't try to parse c++11 attributes here. As per the
22558 grammar, that should be a task for
22559 cp_parser_decl_specifier_seq. */
22560 want_semicolon = false;
22562 switch (token->type)
22564 case CPP_NAME:
22565 case CPP_SEMICOLON:
22566 case CPP_MULT:
22567 case CPP_AND:
22568 case CPP_OPEN_PAREN:
22569 case CPP_CLOSE_PAREN:
22570 case CPP_COMMA:
22571 want_semicolon = false;
22572 break;
22574 /* While it's legal for type qualifiers and storage class
22575 specifiers to follow type definitions in the grammar, only
22576 compiler testsuites contain code like that. Assume that if
22577 we see such code, then what we're really seeing is a case
22578 like:
22580 class X { }
22581 const <type> var = ...;
22585 class Y { }
22586 static <type> func (...) ...
22588 i.e. the qualifier or specifier applies to the next
22589 declaration. To do so, however, we need to look ahead one
22590 more token to see if *that* token is a type specifier.
22592 This code could be improved to handle:
22594 class Z { }
22595 static const <type> var = ...; */
22596 case CPP_KEYWORD:
22597 if (keyword_is_decl_specifier (token->keyword))
22599 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22601 /* Handling user-defined types here would be nice, but very
22602 tricky. */
22603 want_semicolon
22604 = (lookahead->type == CPP_KEYWORD
22605 && keyword_begins_type_specifier (lookahead->keyword));
22607 break;
22608 default:
22609 break;
22612 /* If we don't have a type, then something is very wrong and we
22613 shouldn't try to do anything clever. Likewise for not seeing the
22614 closing brace. */
22615 if (closing_brace && TYPE_P (type) && want_semicolon)
22617 /* Locate the closing brace. */
22618 cp_token_position prev
22619 = cp_lexer_previous_token_position (parser->lexer);
22620 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22621 location_t loc = prev_token->location;
22623 /* We want to suggest insertion of a ';' immediately *after* the
22624 closing brace, so, if we can, offset the location by 1 column. */
22625 location_t next_loc = loc;
22626 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22627 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22629 rich_location richloc (line_table, next_loc);
22631 /* If we successfully offset the location, suggest the fix-it. */
22632 if (next_loc != loc)
22633 richloc.add_fixit_insert_before (next_loc, ";");
22635 if (CLASSTYPE_DECLARED_CLASS (type))
22636 error_at (&richloc,
22637 "expected %<;%> after class definition");
22638 else if (TREE_CODE (type) == RECORD_TYPE)
22639 error_at (&richloc,
22640 "expected %<;%> after struct definition");
22641 else if (TREE_CODE (type) == UNION_TYPE)
22642 error_at (&richloc,
22643 "expected %<;%> after union definition");
22644 else
22645 gcc_unreachable ();
22647 /* Unget one token and smash it to look as though we encountered
22648 a semicolon in the input stream. */
22649 cp_lexer_set_token_position (parser->lexer, prev);
22650 token = cp_lexer_peek_token (parser->lexer);
22651 token->type = CPP_SEMICOLON;
22652 token->keyword = RID_MAX;
22656 /* If this class is not itself within the scope of another class,
22657 then we need to parse the bodies of all of the queued function
22658 definitions. Note that the queued functions defined in a class
22659 are not always processed immediately following the
22660 class-specifier for that class. Consider:
22662 struct A {
22663 struct B { void f() { sizeof (A); } };
22666 If `f' were processed before the processing of `A' were
22667 completed, there would be no way to compute the size of `A'.
22668 Note that the nesting we are interested in here is lexical --
22669 not the semantic nesting given by TYPE_CONTEXT. In particular,
22670 for:
22672 struct A { struct B; };
22673 struct A::B { void f() { } };
22675 there is no need to delay the parsing of `A::B::f'. */
22676 if (--parser->num_classes_being_defined == 0)
22678 tree decl;
22679 tree class_type = NULL_TREE;
22680 tree pushed_scope = NULL_TREE;
22681 unsigned ix;
22682 cp_default_arg_entry *e;
22683 tree save_ccp, save_ccr;
22685 if (any_erroneous_template_args_p (type))
22687 /* Skip default arguments, NSDMIs, etc, in order to improve
22688 error recovery (c++/71169, c++/71832). */
22689 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22690 vec_safe_truncate (unparsed_nsdmis, 0);
22691 vec_safe_truncate (unparsed_classes, 0);
22692 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22695 /* In a first pass, parse default arguments to the functions.
22696 Then, in a second pass, parse the bodies of the functions.
22697 This two-phased approach handles cases like:
22699 struct S {
22700 void f() { g(); }
22701 void g(int i = 3);
22705 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22707 decl = e->decl;
22708 /* If there are default arguments that have not yet been processed,
22709 take care of them now. */
22710 if (class_type != e->class_type)
22712 if (pushed_scope)
22713 pop_scope (pushed_scope);
22714 class_type = e->class_type;
22715 pushed_scope = push_scope (class_type);
22717 /* Make sure that any template parameters are in scope. */
22718 maybe_begin_member_template_processing (decl);
22719 /* Parse the default argument expressions. */
22720 cp_parser_late_parsing_default_args (parser, decl);
22721 /* Remove any template parameters from the symbol table. */
22722 maybe_end_member_template_processing ();
22724 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22725 /* Now parse any NSDMIs. */
22726 save_ccp = current_class_ptr;
22727 save_ccr = current_class_ref;
22728 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22730 if (class_type != DECL_CONTEXT (decl))
22732 if (pushed_scope)
22733 pop_scope (pushed_scope);
22734 class_type = DECL_CONTEXT (decl);
22735 pushed_scope = push_scope (class_type);
22737 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22738 cp_parser_late_parsing_nsdmi (parser, decl);
22740 vec_safe_truncate (unparsed_nsdmis, 0);
22741 current_class_ptr = save_ccp;
22742 current_class_ref = save_ccr;
22743 if (pushed_scope)
22744 pop_scope (pushed_scope);
22746 /* Now do some post-NSDMI bookkeeping. */
22747 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22748 after_nsdmi_defaulted_late_checks (class_type);
22749 vec_safe_truncate (unparsed_classes, 0);
22750 after_nsdmi_defaulted_late_checks (type);
22752 /* Now parse the body of the functions. */
22753 if (flag_openmp)
22755 /* OpenMP UDRs need to be parsed before all other functions. */
22756 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22757 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22758 cp_parser_late_parsing_for_member (parser, decl);
22759 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22760 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22761 cp_parser_late_parsing_for_member (parser, decl);
22763 else
22764 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22765 cp_parser_late_parsing_for_member (parser, decl);
22766 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22768 else
22769 vec_safe_push (unparsed_classes, type);
22771 /* Put back any saved access checks. */
22772 pop_deferring_access_checks ();
22774 /* Restore saved state. */
22775 parser->in_switch_statement_p = in_switch_statement_p;
22776 parser->in_statement = in_statement;
22777 parser->in_function_body = saved_in_function_body;
22778 parser->num_template_parameter_lists
22779 = saved_num_template_parameter_lists;
22780 parser->in_unbraced_linkage_specification_p
22781 = saved_in_unbraced_linkage_specification_p;
22783 return type;
22786 static tree
22787 cp_parser_class_specifier (cp_parser* parser)
22789 tree ret;
22790 timevar_push (TV_PARSE_STRUCT);
22791 ret = cp_parser_class_specifier_1 (parser);
22792 timevar_pop (TV_PARSE_STRUCT);
22793 return ret;
22796 /* Parse a class-head.
22798 class-head:
22799 class-key identifier [opt] base-clause [opt]
22800 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22801 class-key nested-name-specifier [opt] template-id
22802 base-clause [opt]
22804 class-virt-specifier:
22805 final
22807 GNU Extensions:
22808 class-key attributes identifier [opt] base-clause [opt]
22809 class-key attributes nested-name-specifier identifier base-clause [opt]
22810 class-key attributes nested-name-specifier [opt] template-id
22811 base-clause [opt]
22813 Upon return BASES is initialized to the list of base classes (or
22814 NULL, if there are none) in the same form returned by
22815 cp_parser_base_clause.
22817 Returns the TYPE of the indicated class. Sets
22818 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22819 involving a nested-name-specifier was used, and FALSE otherwise.
22821 Returns error_mark_node if this is not a class-head.
22823 Returns NULL_TREE if the class-head is syntactically valid, but
22824 semantically invalid in a way that means we should skip the entire
22825 body of the class. */
22827 static tree
22828 cp_parser_class_head (cp_parser* parser,
22829 bool* nested_name_specifier_p)
22831 tree nested_name_specifier;
22832 enum tag_types class_key;
22833 tree id = NULL_TREE;
22834 tree type = NULL_TREE;
22835 tree attributes;
22836 tree bases;
22837 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22838 bool template_id_p = false;
22839 bool qualified_p = false;
22840 bool invalid_nested_name_p = false;
22841 bool invalid_explicit_specialization_p = false;
22842 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22843 tree pushed_scope = NULL_TREE;
22844 unsigned num_templates;
22845 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22846 /* Assume no nested-name-specifier will be present. */
22847 *nested_name_specifier_p = false;
22848 /* Assume no template parameter lists will be used in defining the
22849 type. */
22850 num_templates = 0;
22851 parser->colon_corrects_to_scope_p = false;
22853 /* Look for the class-key. */
22854 class_key = cp_parser_class_key (parser);
22855 if (class_key == none_type)
22856 return error_mark_node;
22858 location_t class_head_start_location = input_location;
22860 /* Parse the attributes. */
22861 attributes = cp_parser_attributes_opt (parser);
22863 /* If the next token is `::', that is invalid -- but sometimes
22864 people do try to write:
22866 struct ::S {};
22868 Handle this gracefully by accepting the extra qualifier, and then
22869 issuing an error about it later if this really is a
22870 class-head. If it turns out just to be an elaborated type
22871 specifier, remain silent. */
22872 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22873 qualified_p = true;
22875 push_deferring_access_checks (dk_no_check);
22877 /* Determine the name of the class. Begin by looking for an
22878 optional nested-name-specifier. */
22879 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22880 nested_name_specifier
22881 = cp_parser_nested_name_specifier_opt (parser,
22882 /*typename_keyword_p=*/false,
22883 /*check_dependency_p=*/false,
22884 /*type_p=*/true,
22885 /*is_declaration=*/false);
22886 /* If there was a nested-name-specifier, then there *must* be an
22887 identifier. */
22889 cp_token *bad_template_keyword = NULL;
22891 if (nested_name_specifier)
22893 type_start_token = cp_lexer_peek_token (parser->lexer);
22894 /* Although the grammar says `identifier', it really means
22895 `class-name' or `template-name'. You are only allowed to
22896 define a class that has already been declared with this
22897 syntax.
22899 The proposed resolution for Core Issue 180 says that wherever
22900 you see `class T::X' you should treat `X' as a type-name.
22902 It is OK to define an inaccessible class; for example:
22904 class A { class B; };
22905 class A::B {};
22907 We do not know if we will see a class-name, or a
22908 template-name. We look for a class-name first, in case the
22909 class-name is a template-id; if we looked for the
22910 template-name first we would stop after the template-name. */
22911 cp_parser_parse_tentatively (parser);
22912 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22913 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22914 type = cp_parser_class_name (parser,
22915 /*typename_keyword_p=*/false,
22916 /*template_keyword_p=*/false,
22917 class_type,
22918 /*check_dependency_p=*/false,
22919 /*class_head_p=*/true,
22920 /*is_declaration=*/false);
22921 /* If that didn't work, ignore the nested-name-specifier. */
22922 if (!cp_parser_parse_definitely (parser))
22924 invalid_nested_name_p = true;
22925 type_start_token = cp_lexer_peek_token (parser->lexer);
22926 id = cp_parser_identifier (parser);
22927 if (id == error_mark_node)
22928 id = NULL_TREE;
22930 /* If we could not find a corresponding TYPE, treat this
22931 declaration like an unqualified declaration. */
22932 if (type == error_mark_node)
22933 nested_name_specifier = NULL_TREE;
22934 /* Otherwise, count the number of templates used in TYPE and its
22935 containing scopes. */
22936 else
22938 tree scope;
22940 for (scope = TREE_TYPE (type);
22941 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22942 scope = get_containing_scope (scope))
22943 if (TYPE_P (scope)
22944 && CLASS_TYPE_P (scope)
22945 && CLASSTYPE_TEMPLATE_INFO (scope)
22946 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22947 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22948 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22949 ++num_templates;
22952 /* Otherwise, the identifier is optional. */
22953 else
22955 /* We don't know whether what comes next is a template-id,
22956 an identifier, or nothing at all. */
22957 cp_parser_parse_tentatively (parser);
22958 /* Check for a template-id. */
22959 type_start_token = cp_lexer_peek_token (parser->lexer);
22960 id = cp_parser_template_id (parser,
22961 /*template_keyword_p=*/false,
22962 /*check_dependency_p=*/true,
22963 class_key,
22964 /*is_declaration=*/true);
22965 /* If that didn't work, it could still be an identifier. */
22966 if (!cp_parser_parse_definitely (parser))
22968 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22970 type_start_token = cp_lexer_peek_token (parser->lexer);
22971 id = cp_parser_identifier (parser);
22973 else
22974 id = NULL_TREE;
22976 else
22978 template_id_p = true;
22979 ++num_templates;
22983 pop_deferring_access_checks ();
22985 if (id)
22987 cp_parser_check_for_invalid_template_id (parser, id,
22988 class_key,
22989 type_start_token->location);
22991 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22993 /* If it's not a `:' or a `{' then we can't really be looking at a
22994 class-head, since a class-head only appears as part of a
22995 class-specifier. We have to detect this situation before calling
22996 xref_tag, since that has irreversible side-effects. */
22997 if (!cp_parser_next_token_starts_class_definition_p (parser))
22999 cp_parser_error (parser, "expected %<{%> or %<:%>");
23000 type = error_mark_node;
23001 goto out;
23004 /* At this point, we're going ahead with the class-specifier, even
23005 if some other problem occurs. */
23006 cp_parser_commit_to_tentative_parse (parser);
23007 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23009 cp_parser_error (parser,
23010 "cannot specify %<override%> for a class");
23011 type = error_mark_node;
23012 goto out;
23014 /* Issue the error about the overly-qualified name now. */
23015 if (qualified_p)
23017 cp_parser_error (parser,
23018 "global qualification of class name is invalid");
23019 type = error_mark_node;
23020 goto out;
23022 else if (invalid_nested_name_p)
23024 cp_parser_error (parser,
23025 "qualified name does not name a class");
23026 type = error_mark_node;
23027 goto out;
23029 else if (nested_name_specifier)
23031 tree scope;
23033 if (bad_template_keyword)
23034 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23035 keyword template shall not appear at the top level. */
23036 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23037 "keyword %<template%> not allowed in class-head-name");
23039 /* Reject typedef-names in class heads. */
23040 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23042 error_at (type_start_token->location,
23043 "invalid class name in declaration of %qD",
23044 type);
23045 type = NULL_TREE;
23046 goto done;
23049 /* Figure out in what scope the declaration is being placed. */
23050 scope = current_scope ();
23051 /* If that scope does not contain the scope in which the
23052 class was originally declared, the program is invalid. */
23053 if (scope && !is_ancestor (scope, nested_name_specifier))
23055 if (at_namespace_scope_p ())
23056 error_at (type_start_token->location,
23057 "declaration of %qD in namespace %qD which does not "
23058 "enclose %qD",
23059 type, scope, nested_name_specifier);
23060 else
23061 error_at (type_start_token->location,
23062 "declaration of %qD in %qD which does not enclose %qD",
23063 type, scope, nested_name_specifier);
23064 type = NULL_TREE;
23065 goto done;
23067 /* [dcl.meaning]
23069 A declarator-id shall not be qualified except for the
23070 definition of a ... nested class outside of its class
23071 ... [or] the definition or explicit instantiation of a
23072 class member of a namespace outside of its namespace. */
23073 if (scope == nested_name_specifier)
23075 permerror (nested_name_specifier_token_start->location,
23076 "extra qualification not allowed");
23077 nested_name_specifier = NULL_TREE;
23078 num_templates = 0;
23081 /* An explicit-specialization must be preceded by "template <>". If
23082 it is not, try to recover gracefully. */
23083 if (at_namespace_scope_p ()
23084 && parser->num_template_parameter_lists == 0
23085 && !processing_template_parmlist
23086 && template_id_p)
23088 /* Build a location of this form:
23089 struct typename <ARGS>
23090 ^~~~~~~~~~~~~~~~~~~~~~
23091 with caret==start at the start token, and
23092 finishing at the end of the type. */
23093 location_t reported_loc
23094 = make_location (class_head_start_location,
23095 class_head_start_location,
23096 get_finish (type_start_token->location));
23097 rich_location richloc (line_table, reported_loc);
23098 richloc.add_fixit_insert_before (class_head_start_location,
23099 "template <> ");
23100 error_at (&richloc,
23101 "an explicit specialization must be preceded by"
23102 " %<template <>%>");
23103 invalid_explicit_specialization_p = true;
23104 /* Take the same action that would have been taken by
23105 cp_parser_explicit_specialization. */
23106 ++parser->num_template_parameter_lists;
23107 begin_specialization ();
23109 /* There must be no "return" statements between this point and the
23110 end of this function; set "type "to the correct return value and
23111 use "goto done;" to return. */
23112 /* Make sure that the right number of template parameters were
23113 present. */
23114 if (!cp_parser_check_template_parameters (parser, num_templates,
23115 type_start_token->location,
23116 /*declarator=*/NULL))
23118 /* If something went wrong, there is no point in even trying to
23119 process the class-definition. */
23120 type = NULL_TREE;
23121 goto done;
23124 /* Look up the type. */
23125 if (template_id_p)
23127 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23128 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23129 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23131 error_at (type_start_token->location,
23132 "function template %qD redeclared as a class template", id);
23133 type = error_mark_node;
23135 else
23137 type = TREE_TYPE (id);
23138 type = maybe_process_partial_specialization (type);
23140 /* Check the scope while we still know whether or not we had a
23141 nested-name-specifier. */
23142 if (type != error_mark_node)
23143 check_unqualified_spec_or_inst (type, type_start_token->location);
23145 if (nested_name_specifier)
23146 pushed_scope = push_scope (nested_name_specifier);
23148 else if (nested_name_specifier)
23150 tree class_type;
23152 /* Given:
23154 template <typename T> struct S { struct T };
23155 template <typename T> struct S<T>::T { };
23157 we will get a TYPENAME_TYPE when processing the definition of
23158 `S::T'. We need to resolve it to the actual type before we
23159 try to define it. */
23160 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23162 class_type = resolve_typename_type (TREE_TYPE (type),
23163 /*only_current_p=*/false);
23164 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23165 type = TYPE_NAME (class_type);
23166 else
23168 cp_parser_error (parser, "could not resolve typename type");
23169 type = error_mark_node;
23173 if (maybe_process_partial_specialization (TREE_TYPE (type))
23174 == error_mark_node)
23176 type = NULL_TREE;
23177 goto done;
23180 class_type = current_class_type;
23181 /* Enter the scope indicated by the nested-name-specifier. */
23182 pushed_scope = push_scope (nested_name_specifier);
23183 /* Get the canonical version of this type. */
23184 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23185 /* Call push_template_decl if it seems like we should be defining a
23186 template either from the template headers or the type we're
23187 defining, so that we diagnose both extra and missing headers. */
23188 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23189 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23190 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23192 type = push_template_decl (type);
23193 if (type == error_mark_node)
23195 type = NULL_TREE;
23196 goto done;
23200 type = TREE_TYPE (type);
23201 *nested_name_specifier_p = true;
23203 else /* The name is not a nested name. */
23205 /* If the class was unnamed, create a dummy name. */
23206 if (!id)
23207 id = make_anon_name ();
23208 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23209 ? ts_within_enclosing_non_class
23210 : ts_current);
23211 type = xref_tag (class_key, id, tag_scope,
23212 parser->num_template_parameter_lists);
23215 /* Indicate whether this class was declared as a `class' or as a
23216 `struct'. */
23217 if (TREE_CODE (type) == RECORD_TYPE)
23218 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23219 cp_parser_check_class_key (class_key, type);
23221 /* If this type was already complete, and we see another definition,
23222 that's an error. */
23223 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23225 error_at (type_start_token->location, "redefinition of %q#T",
23226 type);
23227 inform (location_of (type), "previous definition of %q#T",
23228 type);
23229 type = NULL_TREE;
23230 goto done;
23232 else if (type == error_mark_node)
23233 type = NULL_TREE;
23235 if (type)
23237 /* Apply attributes now, before any use of the class as a template
23238 argument in its base list. */
23239 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23240 fixup_attribute_variants (type);
23243 /* We will have entered the scope containing the class; the names of
23244 base classes should be looked up in that context. For example:
23246 struct A { struct B {}; struct C; };
23247 struct A::C : B {};
23249 is valid. */
23251 /* Get the list of base-classes, if there is one. */
23252 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23254 /* PR59482: enter the class scope so that base-specifiers are looked
23255 up correctly. */
23256 if (type)
23257 pushclass (type);
23258 bases = cp_parser_base_clause (parser);
23259 /* PR59482: get out of the previously pushed class scope so that the
23260 subsequent pops pop the right thing. */
23261 if (type)
23262 popclass ();
23264 else
23265 bases = NULL_TREE;
23267 /* If we're really defining a class, process the base classes.
23268 If they're invalid, fail. */
23269 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23270 xref_basetypes (type, bases);
23272 done:
23273 /* Leave the scope given by the nested-name-specifier. We will
23274 enter the class scope itself while processing the members. */
23275 if (pushed_scope)
23276 pop_scope (pushed_scope);
23278 if (invalid_explicit_specialization_p)
23280 end_specialization ();
23281 --parser->num_template_parameter_lists;
23284 if (type)
23285 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23286 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23287 CLASSTYPE_FINAL (type) = 1;
23288 out:
23289 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23290 return type;
23293 /* Parse a class-key.
23295 class-key:
23296 class
23297 struct
23298 union
23300 Returns the kind of class-key specified, or none_type to indicate
23301 error. */
23303 static enum tag_types
23304 cp_parser_class_key (cp_parser* parser)
23306 cp_token *token;
23307 enum tag_types tag_type;
23309 /* Look for the class-key. */
23310 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23311 if (!token)
23312 return none_type;
23314 /* Check to see if the TOKEN is a class-key. */
23315 tag_type = cp_parser_token_is_class_key (token);
23316 if (!tag_type)
23317 cp_parser_error (parser, "expected class-key");
23318 return tag_type;
23321 /* Parse a type-parameter-key.
23323 type-parameter-key:
23324 class
23325 typename
23328 static void
23329 cp_parser_type_parameter_key (cp_parser* parser)
23331 /* Look for the type-parameter-key. */
23332 enum tag_types tag_type = none_type;
23333 cp_token *token = cp_lexer_peek_token (parser->lexer);
23334 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23336 cp_lexer_consume_token (parser->lexer);
23337 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23338 /* typename is not allowed in a template template parameter
23339 by the standard until C++17. */
23340 pedwarn (token->location, OPT_Wpedantic,
23341 "ISO C++ forbids typename key in template template parameter;"
23342 " use -std=c++17 or -std=gnu++17");
23344 else
23345 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23347 return;
23350 /* Parse an (optional) member-specification.
23352 member-specification:
23353 member-declaration member-specification [opt]
23354 access-specifier : member-specification [opt] */
23356 static void
23357 cp_parser_member_specification_opt (cp_parser* parser)
23359 while (true)
23361 cp_token *token;
23362 enum rid keyword;
23364 /* Peek at the next token. */
23365 token = cp_lexer_peek_token (parser->lexer);
23366 /* If it's a `}', or EOF then we've seen all the members. */
23367 if (token->type == CPP_CLOSE_BRACE
23368 || token->type == CPP_EOF
23369 || token->type == CPP_PRAGMA_EOL)
23370 break;
23372 /* See if this token is a keyword. */
23373 keyword = token->keyword;
23374 switch (keyword)
23376 case RID_PUBLIC:
23377 case RID_PROTECTED:
23378 case RID_PRIVATE:
23379 /* Consume the access-specifier. */
23380 cp_lexer_consume_token (parser->lexer);
23381 /* Remember which access-specifier is active. */
23382 current_access_specifier = token->u.value;
23383 /* Look for the `:'. */
23384 cp_parser_require (parser, CPP_COLON, RT_COLON);
23385 break;
23387 default:
23388 /* Accept #pragmas at class scope. */
23389 if (token->type == CPP_PRAGMA)
23391 cp_parser_pragma (parser, pragma_member, NULL);
23392 break;
23395 /* Otherwise, the next construction must be a
23396 member-declaration. */
23397 cp_parser_member_declaration (parser);
23402 /* Parse a member-declaration.
23404 member-declaration:
23405 decl-specifier-seq [opt] member-declarator-list [opt] ;
23406 function-definition ; [opt]
23407 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23408 using-declaration
23409 template-declaration
23410 alias-declaration
23412 member-declarator-list:
23413 member-declarator
23414 member-declarator-list , member-declarator
23416 member-declarator:
23417 declarator pure-specifier [opt]
23418 declarator constant-initializer [opt]
23419 identifier [opt] : constant-expression
23421 GNU Extensions:
23423 member-declaration:
23424 __extension__ member-declaration
23426 member-declarator:
23427 declarator attributes [opt] pure-specifier [opt]
23428 declarator attributes [opt] constant-initializer [opt]
23429 identifier [opt] attributes [opt] : constant-expression
23431 C++0x Extensions:
23433 member-declaration:
23434 static_assert-declaration */
23436 static void
23437 cp_parser_member_declaration (cp_parser* parser)
23439 cp_decl_specifier_seq decl_specifiers;
23440 tree prefix_attributes;
23441 tree decl;
23442 int declares_class_or_enum;
23443 bool friend_p;
23444 cp_token *token = NULL;
23445 cp_token *decl_spec_token_start = NULL;
23446 cp_token *initializer_token_start = NULL;
23447 int saved_pedantic;
23448 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23450 /* Check for the `__extension__' keyword. */
23451 if (cp_parser_extension_opt (parser, &saved_pedantic))
23453 /* Recurse. */
23454 cp_parser_member_declaration (parser);
23455 /* Restore the old value of the PEDANTIC flag. */
23456 pedantic = saved_pedantic;
23458 return;
23461 /* Check for a template-declaration. */
23462 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23464 /* An explicit specialization here is an error condition, and we
23465 expect the specialization handler to detect and report this. */
23466 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23467 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23468 cp_parser_explicit_specialization (parser);
23469 else
23470 cp_parser_template_declaration (parser, /*member_p=*/true);
23472 return;
23474 /* Check for a template introduction. */
23475 else if (cp_parser_template_declaration_after_export (parser, true))
23476 return;
23478 /* Check for a using-declaration. */
23479 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23481 if (cxx_dialect < cxx11)
23483 /* Parse the using-declaration. */
23484 cp_parser_using_declaration (parser,
23485 /*access_declaration_p=*/false);
23486 return;
23488 else
23490 tree decl;
23491 bool alias_decl_expected;
23492 cp_parser_parse_tentatively (parser);
23493 decl = cp_parser_alias_declaration (parser);
23494 /* Note that if we actually see the '=' token after the
23495 identifier, cp_parser_alias_declaration commits the
23496 tentative parse. In that case, we really expect an
23497 alias-declaration. Otherwise, we expect a using
23498 declaration. */
23499 alias_decl_expected =
23500 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23501 cp_parser_parse_definitely (parser);
23503 if (alias_decl_expected)
23504 finish_member_declaration (decl);
23505 else
23506 cp_parser_using_declaration (parser,
23507 /*access_declaration_p=*/false);
23508 return;
23512 /* Check for @defs. */
23513 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23515 tree ivar, member;
23516 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23517 ivar = ivar_chains;
23518 while (ivar)
23520 member = ivar;
23521 ivar = TREE_CHAIN (member);
23522 TREE_CHAIN (member) = NULL_TREE;
23523 finish_member_declaration (member);
23525 return;
23528 /* If the next token is `static_assert' we have a static assertion. */
23529 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23531 cp_parser_static_assert (parser, /*member_p=*/true);
23532 return;
23535 parser->colon_corrects_to_scope_p = false;
23537 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23538 goto out;
23540 /* Parse the decl-specifier-seq. */
23541 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23542 cp_parser_decl_specifier_seq (parser,
23543 CP_PARSER_FLAGS_OPTIONAL,
23544 &decl_specifiers,
23545 &declares_class_or_enum);
23546 /* Check for an invalid type-name. */
23547 if (!decl_specifiers.any_type_specifiers_p
23548 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23549 goto out;
23550 /* If there is no declarator, then the decl-specifier-seq should
23551 specify a type. */
23552 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23554 /* If there was no decl-specifier-seq, and the next token is a
23555 `;', then we have something like:
23557 struct S { ; };
23559 [class.mem]
23561 Each member-declaration shall declare at least one member
23562 name of the class. */
23563 if (!decl_specifiers.any_specifiers_p)
23565 cp_token *token = cp_lexer_peek_token (parser->lexer);
23566 if (!in_system_header_at (token->location))
23568 gcc_rich_location richloc (token->location);
23569 richloc.add_fixit_remove ();
23570 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23573 else
23575 tree type;
23577 /* See if this declaration is a friend. */
23578 friend_p = cp_parser_friend_p (&decl_specifiers);
23579 /* If there were decl-specifiers, check to see if there was
23580 a class-declaration. */
23581 type = check_tag_decl (&decl_specifiers,
23582 /*explicit_type_instantiation_p=*/false);
23583 /* Nested classes have already been added to the class, but
23584 a `friend' needs to be explicitly registered. */
23585 if (friend_p)
23587 /* If the `friend' keyword was present, the friend must
23588 be introduced with a class-key. */
23589 if (!declares_class_or_enum && cxx_dialect < cxx11)
23590 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23591 "in C++03 a class-key must be used "
23592 "when declaring a friend");
23593 /* In this case:
23595 template <typename T> struct A {
23596 friend struct A<T>::B;
23599 A<T>::B will be represented by a TYPENAME_TYPE, and
23600 therefore not recognized by check_tag_decl. */
23601 if (!type)
23603 type = decl_specifiers.type;
23604 if (type && TREE_CODE (type) == TYPE_DECL)
23605 type = TREE_TYPE (type);
23607 if (!type || !TYPE_P (type))
23608 error_at (decl_spec_token_start->location,
23609 "friend declaration does not name a class or "
23610 "function");
23611 else
23612 make_friend_class (current_class_type, type,
23613 /*complain=*/true);
23615 /* If there is no TYPE, an error message will already have
23616 been issued. */
23617 else if (!type || type == error_mark_node)
23619 /* An anonymous aggregate has to be handled specially; such
23620 a declaration really declares a data member (with a
23621 particular type), as opposed to a nested class. */
23622 else if (ANON_AGGR_TYPE_P (type))
23624 /* C++11 9.5/6. */
23625 if (decl_specifiers.storage_class != sc_none)
23626 error_at (decl_spec_token_start->location,
23627 "a storage class on an anonymous aggregate "
23628 "in class scope is not allowed");
23630 /* Remove constructors and such from TYPE, now that we
23631 know it is an anonymous aggregate. */
23632 fixup_anonymous_aggr (type);
23633 /* And make the corresponding data member. */
23634 decl = build_decl (decl_spec_token_start->location,
23635 FIELD_DECL, NULL_TREE, type);
23636 /* Add it to the class. */
23637 finish_member_declaration (decl);
23639 else
23640 cp_parser_check_access_in_redeclaration
23641 (TYPE_NAME (type),
23642 decl_spec_token_start->location);
23645 else
23647 bool assume_semicolon = false;
23649 /* Clear attributes from the decl_specifiers but keep them
23650 around as prefix attributes that apply them to the entity
23651 being declared. */
23652 prefix_attributes = decl_specifiers.attributes;
23653 decl_specifiers.attributes = NULL_TREE;
23655 /* See if these declarations will be friends. */
23656 friend_p = cp_parser_friend_p (&decl_specifiers);
23658 /* Keep going until we hit the `;' at the end of the
23659 declaration. */
23660 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23662 tree attributes = NULL_TREE;
23663 tree first_attribute;
23664 tree initializer;
23665 bool named_bitfld = false;
23667 /* Peek at the next token. */
23668 token = cp_lexer_peek_token (parser->lexer);
23670 /* The following code wants to know early if it is a bit-field
23671 or some other declaration. Attributes can appear before
23672 the `:' token. Skip over them without consuming any tokens
23673 to peek if they are followed by `:'. */
23674 if (cp_next_tokens_can_be_attribute_p (parser)
23675 || (token->type == CPP_NAME
23676 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23677 && (named_bitfld = true)))
23679 size_t n
23680 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23681 token = cp_lexer_peek_nth_token (parser->lexer, n);
23684 /* Check for a bitfield declaration. */
23685 if (token->type == CPP_COLON
23686 || (token->type == CPP_NAME
23687 && token == cp_lexer_peek_token (parser->lexer)
23688 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23689 && (named_bitfld = true)))
23691 tree identifier;
23692 tree width;
23693 tree late_attributes = NULL_TREE;
23695 if (named_bitfld)
23696 identifier = cp_parser_identifier (parser);
23697 else
23698 identifier = NULL_TREE;
23700 /* Look for attributes that apply to the bitfield. */
23701 attributes = cp_parser_attributes_opt (parser);
23703 /* Consume the `:' token. */
23704 cp_lexer_consume_token (parser->lexer);
23706 /* Get the width of the bitfield. */
23707 width = cp_parser_constant_expression (parser, false, NULL,
23708 cxx_dialect >= cxx11);
23710 /* In C++2A and as extension for C++11 and above we allow
23711 default member initializers for bit-fields. */
23712 initializer = NULL_TREE;
23713 if (cxx_dialect >= cxx11
23714 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23715 || cp_lexer_next_token_is (parser->lexer,
23716 CPP_OPEN_BRACE)))
23718 location_t loc
23719 = cp_lexer_peek_token (parser->lexer)->location;
23720 if (cxx_dialect < cxx2a
23721 && !in_system_header_at (loc)
23722 && identifier != NULL_TREE)
23723 pedwarn (loc, 0,
23724 "default member initializers for bit-fields "
23725 "only available with -std=c++2a or "
23726 "-std=gnu++2a");
23728 initializer = cp_parser_save_nsdmi (parser);
23729 if (identifier == NULL_TREE)
23731 error_at (loc, "default member initializer for "
23732 "unnamed bit-field");
23733 initializer = NULL_TREE;
23736 else
23738 /* Look for attributes that apply to the bitfield after
23739 the `:' token and width. This is where GCC used to
23740 parse attributes in the past, pedwarn if there is
23741 a std attribute. */
23742 if (cp_next_tokens_can_be_std_attribute_p (parser))
23743 pedwarn (input_location, OPT_Wpedantic,
23744 "ISO C++ allows bit-field attributes only "
23745 "before the %<:%> token");
23747 late_attributes = cp_parser_attributes_opt (parser);
23750 attributes = attr_chainon (attributes, late_attributes);
23752 /* Remember which attributes are prefix attributes and
23753 which are not. */
23754 first_attribute = attributes;
23755 /* Combine the attributes. */
23756 attributes = attr_chainon (prefix_attributes, attributes);
23758 /* Create the bitfield declaration. */
23759 decl = grokbitfield (identifier
23760 ? make_id_declarator (NULL_TREE,
23761 identifier,
23762 sfk_none)
23763 : NULL,
23764 &decl_specifiers,
23765 width, initializer,
23766 attributes);
23768 else
23770 cp_declarator *declarator;
23771 tree asm_specification;
23772 int ctor_dtor_or_conv_p;
23774 /* Parse the declarator. */
23775 declarator
23776 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23777 &ctor_dtor_or_conv_p,
23778 /*parenthesized_p=*/NULL,
23779 /*member_p=*/true,
23780 friend_p);
23782 /* If something went wrong parsing the declarator, make sure
23783 that we at least consume some tokens. */
23784 if (declarator == cp_error_declarator)
23786 /* Skip to the end of the statement. */
23787 cp_parser_skip_to_end_of_statement (parser);
23788 /* If the next token is not a semicolon, that is
23789 probably because we just skipped over the body of
23790 a function. So, we consume a semicolon if
23791 present, but do not issue an error message if it
23792 is not present. */
23793 if (cp_lexer_next_token_is (parser->lexer,
23794 CPP_SEMICOLON))
23795 cp_lexer_consume_token (parser->lexer);
23796 goto out;
23799 if (declares_class_or_enum & 2)
23800 cp_parser_check_for_definition_in_return_type
23801 (declarator, decl_specifiers.type,
23802 decl_specifiers.locations[ds_type_spec]);
23804 /* Look for an asm-specification. */
23805 asm_specification = cp_parser_asm_specification_opt (parser);
23806 /* Look for attributes that apply to the declaration. */
23807 attributes = cp_parser_attributes_opt (parser);
23808 /* Remember which attributes are prefix attributes and
23809 which are not. */
23810 first_attribute = attributes;
23811 /* Combine the attributes. */
23812 attributes = attr_chainon (prefix_attributes, attributes);
23814 /* If it's an `=', then we have a constant-initializer or a
23815 pure-specifier. It is not correct to parse the
23816 initializer before registering the member declaration
23817 since the member declaration should be in scope while
23818 its initializer is processed. However, the rest of the
23819 front end does not yet provide an interface that allows
23820 us to handle this correctly. */
23821 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23823 /* In [class.mem]:
23825 A pure-specifier shall be used only in the declaration of
23826 a virtual function.
23828 A member-declarator can contain a constant-initializer
23829 only if it declares a static member of integral or
23830 enumeration type.
23832 Therefore, if the DECLARATOR is for a function, we look
23833 for a pure-specifier; otherwise, we look for a
23834 constant-initializer. When we call `grokfield', it will
23835 perform more stringent semantics checks. */
23836 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23837 if (function_declarator_p (declarator)
23838 || (decl_specifiers.type
23839 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23840 && declarator->kind == cdk_id
23841 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23842 == FUNCTION_TYPE)))
23843 initializer = cp_parser_pure_specifier (parser);
23844 else if (decl_specifiers.storage_class != sc_static)
23845 initializer = cp_parser_save_nsdmi (parser);
23846 else if (cxx_dialect >= cxx11)
23848 bool nonconst;
23849 /* Don't require a constant rvalue in C++11, since we
23850 might want a reference constant. We'll enforce
23851 constancy later. */
23852 cp_lexer_consume_token (parser->lexer);
23853 /* Parse the initializer. */
23854 initializer = cp_parser_initializer_clause (parser,
23855 &nonconst);
23857 else
23858 /* Parse the initializer. */
23859 initializer = cp_parser_constant_initializer (parser);
23861 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23862 && !function_declarator_p (declarator))
23864 bool x;
23865 if (decl_specifiers.storage_class != sc_static)
23866 initializer = cp_parser_save_nsdmi (parser);
23867 else
23868 initializer = cp_parser_initializer (parser, &x, &x);
23870 /* Otherwise, there is no initializer. */
23871 else
23872 initializer = NULL_TREE;
23874 /* See if we are probably looking at a function
23875 definition. We are certainly not looking at a
23876 member-declarator. Calling `grokfield' has
23877 side-effects, so we must not do it unless we are sure
23878 that we are looking at a member-declarator. */
23879 if (cp_parser_token_starts_function_definition_p
23880 (cp_lexer_peek_token (parser->lexer)))
23882 /* The grammar does not allow a pure-specifier to be
23883 used when a member function is defined. (It is
23884 possible that this fact is an oversight in the
23885 standard, since a pure function may be defined
23886 outside of the class-specifier. */
23887 if (initializer && initializer_token_start)
23888 error_at (initializer_token_start->location,
23889 "pure-specifier on function-definition");
23890 decl = cp_parser_save_member_function_body (parser,
23891 &decl_specifiers,
23892 declarator,
23893 attributes);
23894 if (parser->fully_implicit_function_template_p)
23895 decl = finish_fully_implicit_template (parser, decl);
23896 /* If the member was not a friend, declare it here. */
23897 if (!friend_p)
23898 finish_member_declaration (decl);
23899 /* Peek at the next token. */
23900 token = cp_lexer_peek_token (parser->lexer);
23901 /* If the next token is a semicolon, consume it. */
23902 if (token->type == CPP_SEMICOLON)
23904 location_t semicolon_loc
23905 = cp_lexer_consume_token (parser->lexer)->location;
23906 gcc_rich_location richloc (semicolon_loc);
23907 richloc.add_fixit_remove ();
23908 warning_at (&richloc, OPT_Wextra_semi,
23909 "extra %<;%> after in-class "
23910 "function definition");
23912 goto out;
23914 else
23915 if (declarator->kind == cdk_function)
23916 declarator->id_loc = token->location;
23917 /* Create the declaration. */
23918 decl = grokfield (declarator, &decl_specifiers,
23919 initializer, /*init_const_expr_p=*/true,
23920 asm_specification, attributes);
23921 if (parser->fully_implicit_function_template_p)
23923 if (friend_p)
23924 finish_fully_implicit_template (parser, 0);
23925 else
23926 decl = finish_fully_implicit_template (parser, decl);
23930 cp_finalize_omp_declare_simd (parser, decl);
23931 cp_finalize_oacc_routine (parser, decl, false);
23933 /* Reset PREFIX_ATTRIBUTES. */
23934 if (attributes != error_mark_node)
23936 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23937 attributes = TREE_CHAIN (attributes);
23938 if (attributes)
23939 TREE_CHAIN (attributes) = NULL_TREE;
23942 /* If there is any qualification still in effect, clear it
23943 now; we will be starting fresh with the next declarator. */
23944 parser->scope = NULL_TREE;
23945 parser->qualifying_scope = NULL_TREE;
23946 parser->object_scope = NULL_TREE;
23947 /* If it's a `,', then there are more declarators. */
23948 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23950 cp_lexer_consume_token (parser->lexer);
23951 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23953 cp_token *token = cp_lexer_previous_token (parser->lexer);
23954 gcc_rich_location richloc (token->location);
23955 richloc.add_fixit_remove ();
23956 error_at (&richloc, "stray %<,%> at end of "
23957 "member declaration");
23960 /* If the next token isn't a `;', then we have a parse error. */
23961 else if (cp_lexer_next_token_is_not (parser->lexer,
23962 CPP_SEMICOLON))
23964 /* The next token might be a ways away from where the
23965 actual semicolon is missing. Find the previous token
23966 and use that for our error position. */
23967 cp_token *token = cp_lexer_previous_token (parser->lexer);
23968 gcc_rich_location richloc (token->location);
23969 richloc.add_fixit_insert_after (";");
23970 error_at (&richloc, "expected %<;%> at end of "
23971 "member declaration");
23973 /* Assume that the user meant to provide a semicolon. If
23974 we were to cp_parser_skip_to_end_of_statement, we might
23975 skip to a semicolon inside a member function definition
23976 and issue nonsensical error messages. */
23977 assume_semicolon = true;
23980 if (decl)
23982 /* Add DECL to the list of members. */
23983 if (!friend_p
23984 /* Explicitly include, eg, NSDMIs, for better error
23985 recovery (c++/58650). */
23986 || !DECL_DECLARES_FUNCTION_P (decl))
23987 finish_member_declaration (decl);
23989 if (TREE_CODE (decl) == FUNCTION_DECL)
23990 cp_parser_save_default_args (parser, decl);
23991 else if (TREE_CODE (decl) == FIELD_DECL
23992 && DECL_INITIAL (decl))
23993 /* Add DECL to the queue of NSDMI to be parsed later. */
23994 vec_safe_push (unparsed_nsdmis, decl);
23997 if (assume_semicolon)
23998 goto out;
24002 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24003 out:
24004 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24007 /* Parse a pure-specifier.
24009 pure-specifier:
24012 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24013 Otherwise, ERROR_MARK_NODE is returned. */
24015 static tree
24016 cp_parser_pure_specifier (cp_parser* parser)
24018 cp_token *token;
24020 /* Look for the `=' token. */
24021 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24022 return error_mark_node;
24023 /* Look for the `0' token. */
24024 token = cp_lexer_peek_token (parser->lexer);
24026 if (token->type == CPP_EOF
24027 || token->type == CPP_PRAGMA_EOL)
24028 return error_mark_node;
24030 cp_lexer_consume_token (parser->lexer);
24032 /* Accept = default or = delete in c++0x mode. */
24033 if (token->keyword == RID_DEFAULT
24034 || token->keyword == RID_DELETE)
24036 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24037 return token->u.value;
24040 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24041 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24043 cp_parser_error (parser,
24044 "invalid pure specifier (only %<= 0%> is allowed)");
24045 cp_parser_skip_to_end_of_statement (parser);
24046 return error_mark_node;
24048 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24050 error_at (token->location, "templates may not be %<virtual%>");
24051 return error_mark_node;
24054 return integer_zero_node;
24057 /* Parse a constant-initializer.
24059 constant-initializer:
24060 = constant-expression
24062 Returns a representation of the constant-expression. */
24064 static tree
24065 cp_parser_constant_initializer (cp_parser* parser)
24067 /* Look for the `=' token. */
24068 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24069 return error_mark_node;
24071 /* It is invalid to write:
24073 struct S { static const int i = { 7 }; };
24076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24078 cp_parser_error (parser,
24079 "a brace-enclosed initializer is not allowed here");
24080 /* Consume the opening brace. */
24081 matching_braces braces;
24082 braces.consume_open (parser);
24083 /* Skip the initializer. */
24084 cp_parser_skip_to_closing_brace (parser);
24085 /* Look for the trailing `}'. */
24086 braces.require_close (parser);
24088 return error_mark_node;
24091 return cp_parser_constant_expression (parser);
24094 /* Derived classes [gram.class.derived] */
24096 /* Parse a base-clause.
24098 base-clause:
24099 : base-specifier-list
24101 base-specifier-list:
24102 base-specifier ... [opt]
24103 base-specifier-list , base-specifier ... [opt]
24105 Returns a TREE_LIST representing the base-classes, in the order in
24106 which they were declared. The representation of each node is as
24107 described by cp_parser_base_specifier.
24109 In the case that no bases are specified, this function will return
24110 NULL_TREE, not ERROR_MARK_NODE. */
24112 static tree
24113 cp_parser_base_clause (cp_parser* parser)
24115 tree bases = NULL_TREE;
24117 /* Look for the `:' that begins the list. */
24118 cp_parser_require (parser, CPP_COLON, RT_COLON);
24120 /* Scan the base-specifier-list. */
24121 while (true)
24123 cp_token *token;
24124 tree base;
24125 bool pack_expansion_p = false;
24127 /* Look for the base-specifier. */
24128 base = cp_parser_base_specifier (parser);
24129 /* Look for the (optional) ellipsis. */
24130 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24132 /* Consume the `...'. */
24133 cp_lexer_consume_token (parser->lexer);
24135 pack_expansion_p = true;
24138 /* Add BASE to the front of the list. */
24139 if (base && base != error_mark_node)
24141 if (pack_expansion_p)
24142 /* Make this a pack expansion type. */
24143 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24145 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24147 TREE_CHAIN (base) = bases;
24148 bases = base;
24151 /* Peek at the next token. */
24152 token = cp_lexer_peek_token (parser->lexer);
24153 /* If it's not a comma, then the list is complete. */
24154 if (token->type != CPP_COMMA)
24155 break;
24156 /* Consume the `,'. */
24157 cp_lexer_consume_token (parser->lexer);
24160 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24161 base class had a qualified name. However, the next name that
24162 appears is certainly not qualified. */
24163 parser->scope = NULL_TREE;
24164 parser->qualifying_scope = NULL_TREE;
24165 parser->object_scope = NULL_TREE;
24167 return nreverse (bases);
24170 /* Parse a base-specifier.
24172 base-specifier:
24173 :: [opt] nested-name-specifier [opt] class-name
24174 virtual access-specifier [opt] :: [opt] nested-name-specifier
24175 [opt] class-name
24176 access-specifier virtual [opt] :: [opt] nested-name-specifier
24177 [opt] class-name
24179 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24180 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24181 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24182 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24184 static tree
24185 cp_parser_base_specifier (cp_parser* parser)
24187 cp_token *token;
24188 bool done = false;
24189 bool virtual_p = false;
24190 bool duplicate_virtual_error_issued_p = false;
24191 bool duplicate_access_error_issued_p = false;
24192 bool class_scope_p, template_p;
24193 tree access = access_default_node;
24194 tree type;
24196 /* Process the optional `virtual' and `access-specifier'. */
24197 while (!done)
24199 /* Peek at the next token. */
24200 token = cp_lexer_peek_token (parser->lexer);
24201 /* Process `virtual'. */
24202 switch (token->keyword)
24204 case RID_VIRTUAL:
24205 /* If `virtual' appears more than once, issue an error. */
24206 if (virtual_p && !duplicate_virtual_error_issued_p)
24208 cp_parser_error (parser,
24209 "%<virtual%> specified more than once in base-specifier");
24210 duplicate_virtual_error_issued_p = true;
24213 virtual_p = true;
24215 /* Consume the `virtual' token. */
24216 cp_lexer_consume_token (parser->lexer);
24218 break;
24220 case RID_PUBLIC:
24221 case RID_PROTECTED:
24222 case RID_PRIVATE:
24223 /* If more than one access specifier appears, issue an
24224 error. */
24225 if (access != access_default_node
24226 && !duplicate_access_error_issued_p)
24228 cp_parser_error (parser,
24229 "more than one access specifier in base-specifier");
24230 duplicate_access_error_issued_p = true;
24233 access = ridpointers[(int) token->keyword];
24235 /* Consume the access-specifier. */
24236 cp_lexer_consume_token (parser->lexer);
24238 break;
24240 default:
24241 done = true;
24242 break;
24245 /* It is not uncommon to see programs mechanically, erroneously, use
24246 the 'typename' keyword to denote (dependent) qualified types
24247 as base classes. */
24248 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24250 token = cp_lexer_peek_token (parser->lexer);
24251 if (!processing_template_decl)
24252 error_at (token->location,
24253 "keyword %<typename%> not allowed outside of templates");
24254 else
24255 error_at (token->location,
24256 "keyword %<typename%> not allowed in this context "
24257 "(the base class is implicitly a type)");
24258 cp_lexer_consume_token (parser->lexer);
24261 /* Look for the optional `::' operator. */
24262 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24263 /* Look for the nested-name-specifier. The simplest way to
24264 implement:
24266 [temp.res]
24268 The keyword `typename' is not permitted in a base-specifier or
24269 mem-initializer; in these contexts a qualified name that
24270 depends on a template-parameter is implicitly assumed to be a
24271 type name.
24273 is to pretend that we have seen the `typename' keyword at this
24274 point. */
24275 cp_parser_nested_name_specifier_opt (parser,
24276 /*typename_keyword_p=*/true,
24277 /*check_dependency_p=*/true,
24278 /*type_p=*/true,
24279 /*is_declaration=*/true);
24280 /* If the base class is given by a qualified name, assume that names
24281 we see are type names or templates, as appropriate. */
24282 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24283 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24285 if (!parser->scope
24286 && cp_lexer_next_token_is_decltype (parser->lexer))
24287 /* DR 950 allows decltype as a base-specifier. */
24288 type = cp_parser_decltype (parser);
24289 else
24291 /* Otherwise, look for the class-name. */
24292 type = cp_parser_class_name (parser,
24293 class_scope_p,
24294 template_p,
24295 typename_type,
24296 /*check_dependency_p=*/true,
24297 /*class_head_p=*/false,
24298 /*is_declaration=*/true);
24299 type = TREE_TYPE (type);
24302 if (type == error_mark_node)
24303 return error_mark_node;
24305 return finish_base_specifier (type, access, virtual_p);
24308 /* Exception handling [gram.exception] */
24310 /* Parse an (optional) noexcept-specification.
24312 noexcept-specification:
24313 noexcept ( constant-expression ) [opt]
24315 If no noexcept-specification is present, returns NULL_TREE.
24316 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24317 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24318 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24319 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24320 in which case a boolean condition is returned instead. */
24322 static tree
24323 cp_parser_noexcept_specification_opt (cp_parser* parser,
24324 bool require_constexpr,
24325 bool* consumed_expr,
24326 bool return_cond)
24328 cp_token *token;
24329 const char *saved_message;
24331 /* Peek at the next token. */
24332 token = cp_lexer_peek_token (parser->lexer);
24334 /* Is it a noexcept-specification? */
24335 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24337 tree expr;
24338 cp_lexer_consume_token (parser->lexer);
24340 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24342 matching_parens parens;
24343 parens.consume_open (parser);
24345 if (require_constexpr)
24347 /* Types may not be defined in an exception-specification. */
24348 saved_message = parser->type_definition_forbidden_message;
24349 parser->type_definition_forbidden_message
24350 = G_("types may not be defined in an exception-specification");
24352 expr = cp_parser_constant_expression (parser);
24354 /* Restore the saved message. */
24355 parser->type_definition_forbidden_message = saved_message;
24357 else
24359 expr = cp_parser_expression (parser);
24360 *consumed_expr = true;
24363 parens.require_close (parser);
24365 else
24367 expr = boolean_true_node;
24368 if (!require_constexpr)
24369 *consumed_expr = false;
24372 /* We cannot build a noexcept-spec right away because this will check
24373 that expr is a constexpr. */
24374 if (!return_cond)
24375 return build_noexcept_spec (expr, tf_warning_or_error);
24376 else
24377 return expr;
24379 else
24380 return NULL_TREE;
24383 /* Parse an (optional) exception-specification.
24385 exception-specification:
24386 throw ( type-id-list [opt] )
24388 Returns a TREE_LIST representing the exception-specification. The
24389 TREE_VALUE of each node is a type. */
24391 static tree
24392 cp_parser_exception_specification_opt (cp_parser* parser)
24394 cp_token *token;
24395 tree type_id_list;
24396 const char *saved_message;
24398 /* Peek at the next token. */
24399 token = cp_lexer_peek_token (parser->lexer);
24401 /* Is it a noexcept-specification? */
24402 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24403 false);
24404 if (type_id_list != NULL_TREE)
24405 return type_id_list;
24407 /* If it's not `throw', then there's no exception-specification. */
24408 if (!cp_parser_is_keyword (token, RID_THROW))
24409 return NULL_TREE;
24411 location_t loc = token->location;
24413 /* Consume the `throw'. */
24414 cp_lexer_consume_token (parser->lexer);
24416 /* Look for the `('. */
24417 matching_parens parens;
24418 parens.require_open (parser);
24420 /* Peek at the next token. */
24421 token = cp_lexer_peek_token (parser->lexer);
24422 /* If it's not a `)', then there is a type-id-list. */
24423 if (token->type != CPP_CLOSE_PAREN)
24425 /* Types may not be defined in an exception-specification. */
24426 saved_message = parser->type_definition_forbidden_message;
24427 parser->type_definition_forbidden_message
24428 = G_("types may not be defined in an exception-specification");
24429 /* Parse the type-id-list. */
24430 type_id_list = cp_parser_type_id_list (parser);
24431 /* Restore the saved message. */
24432 parser->type_definition_forbidden_message = saved_message;
24434 if (cxx_dialect >= cxx17)
24436 error_at (loc, "ISO C++17 does not allow dynamic exception "
24437 "specifications");
24438 type_id_list = NULL_TREE;
24440 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24441 warning_at (loc, OPT_Wdeprecated,
24442 "dynamic exception specifications are deprecated in "
24443 "C++11");
24445 /* In C++17, throw() is equivalent to noexcept (true). throw()
24446 is deprecated in C++11 and above as well, but is still widely used,
24447 so don't warn about it yet. */
24448 else if (cxx_dialect >= cxx17)
24449 type_id_list = noexcept_true_spec;
24450 else
24451 type_id_list = empty_except_spec;
24453 /* Look for the `)'. */
24454 parens.require_close (parser);
24456 return type_id_list;
24459 /* Parse an (optional) type-id-list.
24461 type-id-list:
24462 type-id ... [opt]
24463 type-id-list , type-id ... [opt]
24465 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24466 in the order that the types were presented. */
24468 static tree
24469 cp_parser_type_id_list (cp_parser* parser)
24471 tree types = NULL_TREE;
24473 while (true)
24475 cp_token *token;
24476 tree type;
24478 token = cp_lexer_peek_token (parser->lexer);
24480 /* Get the next type-id. */
24481 type = cp_parser_type_id (parser);
24482 /* Check for invalid 'auto'. */
24483 if (flag_concepts && type_uses_auto (type))
24485 error_at (token->location,
24486 "invalid use of %<auto%> in exception-specification");
24487 type = error_mark_node;
24489 /* Parse the optional ellipsis. */
24490 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24492 /* Consume the `...'. */
24493 cp_lexer_consume_token (parser->lexer);
24495 /* Turn the type into a pack expansion expression. */
24496 type = make_pack_expansion (type);
24498 /* Add it to the list. */
24499 types = add_exception_specifier (types, type, /*complain=*/1);
24500 /* Peek at the next token. */
24501 token = cp_lexer_peek_token (parser->lexer);
24502 /* If it is not a `,', we are done. */
24503 if (token->type != CPP_COMMA)
24504 break;
24505 /* Consume the `,'. */
24506 cp_lexer_consume_token (parser->lexer);
24509 return nreverse (types);
24512 /* Parse a try-block.
24514 try-block:
24515 try compound-statement handler-seq */
24517 static tree
24518 cp_parser_try_block (cp_parser* parser)
24520 tree try_block;
24522 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24523 if (parser->in_function_body
24524 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24525 error ("%<try%> in %<constexpr%> function");
24527 try_block = begin_try_block ();
24528 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24529 finish_try_block (try_block);
24530 cp_parser_handler_seq (parser);
24531 finish_handler_sequence (try_block);
24533 return try_block;
24536 /* Parse a function-try-block.
24538 function-try-block:
24539 try ctor-initializer [opt] function-body handler-seq */
24541 static void
24542 cp_parser_function_try_block (cp_parser* parser)
24544 tree compound_stmt;
24545 tree try_block;
24547 /* Look for the `try' keyword. */
24548 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24549 return;
24550 /* Let the rest of the front end know where we are. */
24551 try_block = begin_function_try_block (&compound_stmt);
24552 /* Parse the function-body. */
24553 cp_parser_ctor_initializer_opt_and_function_body
24554 (parser, /*in_function_try_block=*/true);
24555 /* We're done with the `try' part. */
24556 finish_function_try_block (try_block);
24557 /* Parse the handlers. */
24558 cp_parser_handler_seq (parser);
24559 /* We're done with the handlers. */
24560 finish_function_handler_sequence (try_block, compound_stmt);
24563 /* Parse a handler-seq.
24565 handler-seq:
24566 handler handler-seq [opt] */
24568 static void
24569 cp_parser_handler_seq (cp_parser* parser)
24571 while (true)
24573 cp_token *token;
24575 /* Parse the handler. */
24576 cp_parser_handler (parser);
24577 /* Peek at the next token. */
24578 token = cp_lexer_peek_token (parser->lexer);
24579 /* If it's not `catch' then there are no more handlers. */
24580 if (!cp_parser_is_keyword (token, RID_CATCH))
24581 break;
24585 /* Parse a handler.
24587 handler:
24588 catch ( exception-declaration ) compound-statement */
24590 static void
24591 cp_parser_handler (cp_parser* parser)
24593 tree handler;
24594 tree declaration;
24596 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24597 handler = begin_handler ();
24598 matching_parens parens;
24599 parens.require_open (parser);
24600 declaration = cp_parser_exception_declaration (parser);
24601 finish_handler_parms (declaration, handler);
24602 parens.require_close (parser);
24603 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24604 finish_handler (handler);
24607 /* Parse an exception-declaration.
24609 exception-declaration:
24610 type-specifier-seq declarator
24611 type-specifier-seq abstract-declarator
24612 type-specifier-seq
24615 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24616 ellipsis variant is used. */
24618 static tree
24619 cp_parser_exception_declaration (cp_parser* parser)
24621 cp_decl_specifier_seq type_specifiers;
24622 cp_declarator *declarator;
24623 const char *saved_message;
24625 /* If it's an ellipsis, it's easy to handle. */
24626 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24628 /* Consume the `...' token. */
24629 cp_lexer_consume_token (parser->lexer);
24630 return NULL_TREE;
24633 /* Types may not be defined in exception-declarations. */
24634 saved_message = parser->type_definition_forbidden_message;
24635 parser->type_definition_forbidden_message
24636 = G_("types may not be defined in exception-declarations");
24638 /* Parse the type-specifier-seq. */
24639 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24640 /*is_trailing_return=*/false,
24641 &type_specifiers);
24642 /* If it's a `)', then there is no declarator. */
24643 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24644 declarator = NULL;
24645 else
24646 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24647 /*ctor_dtor_or_conv_p=*/NULL,
24648 /*parenthesized_p=*/NULL,
24649 /*member_p=*/false,
24650 /*friend_p=*/false);
24652 /* Restore the saved message. */
24653 parser->type_definition_forbidden_message = saved_message;
24655 if (!type_specifiers.any_specifiers_p)
24656 return error_mark_node;
24658 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24661 /* Parse a throw-expression.
24663 throw-expression:
24664 throw assignment-expression [opt]
24666 Returns a THROW_EXPR representing the throw-expression. */
24668 static tree
24669 cp_parser_throw_expression (cp_parser* parser)
24671 tree expression;
24672 cp_token* token;
24674 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24675 token = cp_lexer_peek_token (parser->lexer);
24676 /* Figure out whether or not there is an assignment-expression
24677 following the "throw" keyword. */
24678 if (token->type == CPP_COMMA
24679 || token->type == CPP_SEMICOLON
24680 || token->type == CPP_CLOSE_PAREN
24681 || token->type == CPP_CLOSE_SQUARE
24682 || token->type == CPP_CLOSE_BRACE
24683 || token->type == CPP_COLON)
24684 expression = NULL_TREE;
24685 else
24686 expression = cp_parser_assignment_expression (parser);
24688 return build_throw (expression);
24691 /* GNU Extensions */
24693 /* Parse an (optional) asm-specification.
24695 asm-specification:
24696 asm ( string-literal )
24698 If the asm-specification is present, returns a STRING_CST
24699 corresponding to the string-literal. Otherwise, returns
24700 NULL_TREE. */
24702 static tree
24703 cp_parser_asm_specification_opt (cp_parser* parser)
24705 cp_token *token;
24706 tree asm_specification;
24708 /* Peek at the next token. */
24709 token = cp_lexer_peek_token (parser->lexer);
24710 /* If the next token isn't the `asm' keyword, then there's no
24711 asm-specification. */
24712 if (!cp_parser_is_keyword (token, RID_ASM))
24713 return NULL_TREE;
24715 /* Consume the `asm' token. */
24716 cp_lexer_consume_token (parser->lexer);
24717 /* Look for the `('. */
24718 matching_parens parens;
24719 parens.require_open (parser);
24721 /* Look for the string-literal. */
24722 asm_specification = cp_parser_string_literal (parser, false, false);
24724 /* Look for the `)'. */
24725 parens.require_close (parser);
24727 return asm_specification;
24730 /* Parse an asm-operand-list.
24732 asm-operand-list:
24733 asm-operand
24734 asm-operand-list , asm-operand
24736 asm-operand:
24737 string-literal ( expression )
24738 [ string-literal ] string-literal ( expression )
24740 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24741 each node is the expression. The TREE_PURPOSE is itself a
24742 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24743 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24744 is a STRING_CST for the string literal before the parenthesis. Returns
24745 ERROR_MARK_NODE if any of the operands are invalid. */
24747 static tree
24748 cp_parser_asm_operand_list (cp_parser* parser)
24750 tree asm_operands = NULL_TREE;
24751 bool invalid_operands = false;
24753 while (true)
24755 tree string_literal;
24756 tree expression;
24757 tree name;
24759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24761 /* Consume the `[' token. */
24762 cp_lexer_consume_token (parser->lexer);
24763 /* Read the operand name. */
24764 name = cp_parser_identifier (parser);
24765 if (name != error_mark_node)
24766 name = build_string (IDENTIFIER_LENGTH (name),
24767 IDENTIFIER_POINTER (name));
24768 /* Look for the closing `]'. */
24769 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24771 else
24772 name = NULL_TREE;
24773 /* Look for the string-literal. */
24774 string_literal = cp_parser_string_literal (parser, false, false);
24776 /* Look for the `('. */
24777 matching_parens parens;
24778 parens.require_open (parser);
24779 /* Parse the expression. */
24780 expression = cp_parser_expression (parser);
24781 /* Look for the `)'. */
24782 parens.require_close (parser);
24784 if (name == error_mark_node
24785 || string_literal == error_mark_node
24786 || expression == error_mark_node)
24787 invalid_operands = true;
24789 /* Add this operand to the list. */
24790 asm_operands = tree_cons (build_tree_list (name, string_literal),
24791 expression,
24792 asm_operands);
24793 /* If the next token is not a `,', there are no more
24794 operands. */
24795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24796 break;
24797 /* Consume the `,'. */
24798 cp_lexer_consume_token (parser->lexer);
24801 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24804 /* Parse an asm-clobber-list.
24806 asm-clobber-list:
24807 string-literal
24808 asm-clobber-list , string-literal
24810 Returns a TREE_LIST, indicating the clobbers in the order that they
24811 appeared. The TREE_VALUE of each node is a STRING_CST. */
24813 static tree
24814 cp_parser_asm_clobber_list (cp_parser* parser)
24816 tree clobbers = NULL_TREE;
24818 while (true)
24820 tree string_literal;
24822 /* Look for the string literal. */
24823 string_literal = cp_parser_string_literal (parser, false, false);
24824 /* Add it to the list. */
24825 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24826 /* If the next token is not a `,', then the list is
24827 complete. */
24828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24829 break;
24830 /* Consume the `,' token. */
24831 cp_lexer_consume_token (parser->lexer);
24834 return clobbers;
24837 /* Parse an asm-label-list.
24839 asm-label-list:
24840 identifier
24841 asm-label-list , identifier
24843 Returns a TREE_LIST, indicating the labels in the order that they
24844 appeared. The TREE_VALUE of each node is a label. */
24846 static tree
24847 cp_parser_asm_label_list (cp_parser* parser)
24849 tree labels = NULL_TREE;
24851 while (true)
24853 tree identifier, label, name;
24855 /* Look for the identifier. */
24856 identifier = cp_parser_identifier (parser);
24857 if (!error_operand_p (identifier))
24859 label = lookup_label (identifier);
24860 if (TREE_CODE (label) == LABEL_DECL)
24862 TREE_USED (label) = 1;
24863 check_goto (label);
24864 name = build_string (IDENTIFIER_LENGTH (identifier),
24865 IDENTIFIER_POINTER (identifier));
24866 labels = tree_cons (name, label, labels);
24869 /* If the next token is not a `,', then the list is
24870 complete. */
24871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24872 break;
24873 /* Consume the `,' token. */
24874 cp_lexer_consume_token (parser->lexer);
24877 return nreverse (labels);
24880 /* Return TRUE iff the next tokens in the stream are possibly the
24881 beginning of a GNU extension attribute. */
24883 static bool
24884 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24886 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24889 /* Return TRUE iff the next tokens in the stream are possibly the
24890 beginning of a standard C++-11 attribute specifier. */
24892 static bool
24893 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24895 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24898 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24899 beginning of a standard C++-11 attribute specifier. */
24901 static bool
24902 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24904 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24906 return (cxx_dialect >= cxx11
24907 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24908 || (token->type == CPP_OPEN_SQUARE
24909 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24910 && token->type == CPP_OPEN_SQUARE)));
24913 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24914 beginning of a GNU extension attribute. */
24916 static bool
24917 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24919 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24921 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24924 /* Return true iff the next tokens can be the beginning of either a
24925 GNU attribute list, or a standard C++11 attribute sequence. */
24927 static bool
24928 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24930 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24931 || cp_next_tokens_can_be_std_attribute_p (parser));
24934 /* Return true iff the next Nth tokens can be the beginning of either
24935 a GNU attribute list, or a standard C++11 attribute sequence. */
24937 static bool
24938 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24940 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24941 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24944 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24945 of GNU attributes, or return NULL. */
24947 static tree
24948 cp_parser_attributes_opt (cp_parser *parser)
24950 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24951 return cp_parser_gnu_attributes_opt (parser);
24952 return cp_parser_std_attribute_spec_seq (parser);
24955 /* Parse an (optional) series of attributes.
24957 attributes:
24958 attributes attribute
24960 attribute:
24961 __attribute__ (( attribute-list [opt] ))
24963 The return value is as for cp_parser_gnu_attribute_list. */
24965 static tree
24966 cp_parser_gnu_attributes_opt (cp_parser* parser)
24968 tree attributes = NULL_TREE;
24970 temp_override<bool> cleanup
24971 (parser->auto_is_implicit_function_template_parm_p, false);
24973 while (true)
24975 cp_token *token;
24976 tree attribute_list;
24977 bool ok = true;
24979 /* Peek at the next token. */
24980 token = cp_lexer_peek_token (parser->lexer);
24981 /* If it's not `__attribute__', then we're done. */
24982 if (token->keyword != RID_ATTRIBUTE)
24983 break;
24985 /* Consume the `__attribute__' keyword. */
24986 cp_lexer_consume_token (parser->lexer);
24987 /* Look for the two `(' tokens. */
24988 matching_parens outer_parens;
24989 outer_parens.require_open (parser);
24990 matching_parens inner_parens;
24991 inner_parens.require_open (parser);
24993 /* Peek at the next token. */
24994 token = cp_lexer_peek_token (parser->lexer);
24995 if (token->type != CPP_CLOSE_PAREN)
24996 /* Parse the attribute-list. */
24997 attribute_list = cp_parser_gnu_attribute_list (parser);
24998 else
24999 /* If the next token is a `)', then there is no attribute
25000 list. */
25001 attribute_list = NULL;
25003 /* Look for the two `)' tokens. */
25004 if (!inner_parens.require_close (parser))
25005 ok = false;
25006 if (!outer_parens.require_close (parser))
25007 ok = false;
25008 if (!ok)
25009 cp_parser_skip_to_end_of_statement (parser);
25011 /* Add these new attributes to the list. */
25012 attributes = attr_chainon (attributes, attribute_list);
25015 return attributes;
25018 /* Parse a GNU attribute-list.
25020 attribute-list:
25021 attribute
25022 attribute-list , attribute
25024 attribute:
25025 identifier
25026 identifier ( identifier )
25027 identifier ( identifier , expression-list )
25028 identifier ( expression-list )
25030 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25031 to an attribute. The TREE_PURPOSE of each node is the identifier
25032 indicating which attribute is in use. The TREE_VALUE represents
25033 the arguments, if any. */
25035 static tree
25036 cp_parser_gnu_attribute_list (cp_parser* parser)
25038 tree attribute_list = NULL_TREE;
25039 bool save_translate_strings_p = parser->translate_strings_p;
25041 parser->translate_strings_p = false;
25042 while (true)
25044 cp_token *token;
25045 tree identifier;
25046 tree attribute;
25048 /* Look for the identifier. We also allow keywords here; for
25049 example `__attribute__ ((const))' is legal. */
25050 token = cp_lexer_peek_token (parser->lexer);
25051 if (token->type == CPP_NAME
25052 || token->type == CPP_KEYWORD)
25054 tree arguments = NULL_TREE;
25056 /* Consume the token, but save it since we need it for the
25057 SIMD enabled function parsing. */
25058 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25060 /* Save away the identifier that indicates which attribute
25061 this is. */
25062 identifier = (token->type == CPP_KEYWORD)
25063 /* For keywords, use the canonical spelling, not the
25064 parsed identifier. */
25065 ? ridpointers[(int) token->keyword]
25066 : id_token->u.value;
25068 identifier = canonicalize_attr_name (identifier);
25069 attribute = build_tree_list (identifier, NULL_TREE);
25071 /* Peek at the next token. */
25072 token = cp_lexer_peek_token (parser->lexer);
25073 /* If it's an `(', then parse the attribute arguments. */
25074 if (token->type == CPP_OPEN_PAREN)
25076 vec<tree, va_gc> *vec;
25077 int attr_flag = (attribute_takes_identifier_p (identifier)
25078 ? id_attr : normal_attr);
25079 vec = cp_parser_parenthesized_expression_list
25080 (parser, attr_flag, /*cast_p=*/false,
25081 /*allow_expansion_p=*/false,
25082 /*non_constant_p=*/NULL);
25083 if (vec == NULL)
25084 arguments = error_mark_node;
25085 else
25087 arguments = build_tree_list_vec (vec);
25088 release_tree_vector (vec);
25090 /* Save the arguments away. */
25091 TREE_VALUE (attribute) = arguments;
25094 if (arguments != error_mark_node)
25096 /* Add this attribute to the list. */
25097 TREE_CHAIN (attribute) = attribute_list;
25098 attribute_list = attribute;
25101 token = cp_lexer_peek_token (parser->lexer);
25103 /* Now, look for more attributes. If the next token isn't a
25104 `,', we're done. */
25105 if (token->type != CPP_COMMA)
25106 break;
25108 /* Consume the comma and keep going. */
25109 cp_lexer_consume_token (parser->lexer);
25111 parser->translate_strings_p = save_translate_strings_p;
25113 /* We built up the list in reverse order. */
25114 return nreverse (attribute_list);
25117 /* Parse a standard C++11 attribute.
25119 The returned representation is a TREE_LIST which TREE_PURPOSE is
25120 the scoped name of the attribute, and the TREE_VALUE is its
25121 arguments list.
25123 Note that the scoped name of the attribute is itself a TREE_LIST
25124 which TREE_PURPOSE is the namespace of the attribute, and
25125 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25126 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25127 and which TREE_PURPOSE is directly the attribute name.
25129 Clients of the attribute code should use get_attribute_namespace
25130 and get_attribute_name to get the actual namespace and name of
25131 attributes, regardless of their being GNU or C++11 attributes.
25133 attribute:
25134 attribute-token attribute-argument-clause [opt]
25136 attribute-token:
25137 identifier
25138 attribute-scoped-token
25140 attribute-scoped-token:
25141 attribute-namespace :: identifier
25143 attribute-namespace:
25144 identifier
25146 attribute-argument-clause:
25147 ( balanced-token-seq )
25149 balanced-token-seq:
25150 balanced-token [opt]
25151 balanced-token-seq balanced-token
25153 balanced-token:
25154 ( balanced-token-seq )
25155 [ balanced-token-seq ]
25156 { balanced-token-seq }. */
25158 static tree
25159 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25161 tree attribute, attr_id = NULL_TREE, arguments;
25162 cp_token *token;
25164 temp_override<bool> cleanup
25165 (parser->auto_is_implicit_function_template_parm_p, false);
25167 /* First, parse name of the attribute, a.k.a attribute-token. */
25169 token = cp_lexer_peek_token (parser->lexer);
25170 if (token->type == CPP_NAME)
25171 attr_id = token->u.value;
25172 else if (token->type == CPP_KEYWORD)
25173 attr_id = ridpointers[(int) token->keyword];
25174 else if (token->flags & NAMED_OP)
25175 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25177 if (attr_id == NULL_TREE)
25178 return NULL_TREE;
25180 cp_lexer_consume_token (parser->lexer);
25182 token = cp_lexer_peek_token (parser->lexer);
25183 if (token->type == CPP_SCOPE)
25185 /* We are seeing a scoped attribute token. */
25187 cp_lexer_consume_token (parser->lexer);
25188 if (attr_ns)
25189 error_at (token->location, "attribute using prefix used together "
25190 "with scoped attribute token");
25191 attr_ns = attr_id;
25193 token = cp_lexer_consume_token (parser->lexer);
25194 if (token->type == CPP_NAME)
25195 attr_id = token->u.value;
25196 else if (token->type == CPP_KEYWORD)
25197 attr_id = ridpointers[(int) token->keyword];
25198 else if (token->flags & NAMED_OP)
25199 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25200 else
25202 error_at (token->location,
25203 "expected an identifier for the attribute name");
25204 return error_mark_node;
25207 attr_id = canonicalize_attr_name (attr_id);
25208 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25209 NULL_TREE);
25210 token = cp_lexer_peek_token (parser->lexer);
25212 else if (attr_ns)
25213 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25214 NULL_TREE);
25215 else
25217 attr_id = canonicalize_attr_name (attr_id);
25218 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25219 NULL_TREE);
25220 /* C++11 noreturn attribute is equivalent to GNU's. */
25221 if (is_attribute_p ("noreturn", attr_id))
25222 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25223 /* C++14 deprecated attribute is equivalent to GNU's. */
25224 else if (is_attribute_p ("deprecated", attr_id))
25225 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25226 /* C++17 fallthrough attribute is equivalent to GNU's. */
25227 else if (is_attribute_p ("fallthrough", attr_id))
25228 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25229 /* Transactional Memory TS optimize_for_synchronized attribute is
25230 equivalent to GNU transaction_callable. */
25231 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25232 TREE_PURPOSE (attribute)
25233 = get_identifier ("transaction_callable");
25234 /* Transactional Memory attributes are GNU attributes. */
25235 else if (tm_attr_to_mask (attr_id))
25236 TREE_PURPOSE (attribute) = attr_id;
25239 /* Now parse the optional argument clause of the attribute. */
25241 if (token->type != CPP_OPEN_PAREN)
25242 return attribute;
25245 vec<tree, va_gc> *vec;
25246 int attr_flag = normal_attr;
25248 if (attr_ns == get_identifier ("gnu")
25249 && attribute_takes_identifier_p (attr_id))
25250 /* A GNU attribute that takes an identifier in parameter. */
25251 attr_flag = id_attr;
25253 vec = cp_parser_parenthesized_expression_list
25254 (parser, attr_flag, /*cast_p=*/false,
25255 /*allow_expansion_p=*/true,
25256 /*non_constant_p=*/NULL);
25257 if (vec == NULL)
25258 arguments = error_mark_node;
25259 else
25261 arguments = build_tree_list_vec (vec);
25262 release_tree_vector (vec);
25265 if (arguments == error_mark_node)
25266 attribute = error_mark_node;
25267 else
25268 TREE_VALUE (attribute) = arguments;
25271 return attribute;
25274 /* Check that the attribute ATTRIBUTE appears at most once in the
25275 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25276 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25277 isn't implemented yet in GCC. */
25279 static void
25280 cp_parser_check_std_attribute (tree attributes, tree attribute)
25282 if (attributes)
25284 tree name = get_attribute_name (attribute);
25285 if (is_attribute_p ("noreturn", name)
25286 && lookup_attribute ("noreturn", attributes))
25287 error ("attribute %<noreturn%> can appear at most once "
25288 "in an attribute-list");
25289 else if (is_attribute_p ("deprecated", name)
25290 && lookup_attribute ("deprecated", attributes))
25291 error ("attribute %<deprecated%> can appear at most once "
25292 "in an attribute-list");
25296 /* Parse a list of standard C++-11 attributes.
25298 attribute-list:
25299 attribute [opt]
25300 attribute-list , attribute[opt]
25301 attribute ...
25302 attribute-list , attribute ...
25305 static tree
25306 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25308 tree attributes = NULL_TREE, attribute = NULL_TREE;
25309 cp_token *token = NULL;
25311 while (true)
25313 attribute = cp_parser_std_attribute (parser, attr_ns);
25314 if (attribute == error_mark_node)
25315 break;
25316 if (attribute != NULL_TREE)
25318 cp_parser_check_std_attribute (attributes, attribute);
25319 TREE_CHAIN (attribute) = attributes;
25320 attributes = attribute;
25322 token = cp_lexer_peek_token (parser->lexer);
25323 if (token->type == CPP_ELLIPSIS)
25325 cp_lexer_consume_token (parser->lexer);
25326 if (attribute == NULL_TREE)
25327 error_at (token->location,
25328 "expected attribute before %<...%>");
25329 else
25331 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25332 if (pack == error_mark_node)
25333 return error_mark_node;
25334 TREE_VALUE (attribute) = pack;
25336 token = cp_lexer_peek_token (parser->lexer);
25338 if (token->type != CPP_COMMA)
25339 break;
25340 cp_lexer_consume_token (parser->lexer);
25342 attributes = nreverse (attributes);
25343 return attributes;
25346 /* Parse a standard C++-11 attribute specifier.
25348 attribute-specifier:
25349 [ [ attribute-using-prefix [opt] attribute-list ] ]
25350 alignment-specifier
25352 attribute-using-prefix:
25353 using attribute-namespace :
25355 alignment-specifier:
25356 alignas ( type-id ... [opt] )
25357 alignas ( alignment-expression ... [opt] ). */
25359 static tree
25360 cp_parser_std_attribute_spec (cp_parser *parser)
25362 tree attributes = NULL_TREE;
25363 cp_token *token = cp_lexer_peek_token (parser->lexer);
25365 if (token->type == CPP_OPEN_SQUARE
25366 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25368 tree attr_ns = NULL_TREE;
25370 cp_lexer_consume_token (parser->lexer);
25371 cp_lexer_consume_token (parser->lexer);
25373 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25375 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25376 if (token->type == CPP_NAME)
25377 attr_ns = token->u.value;
25378 else if (token->type == CPP_KEYWORD)
25379 attr_ns = ridpointers[(int) token->keyword];
25380 else if (token->flags & NAMED_OP)
25381 attr_ns = get_identifier (cpp_type2name (token->type,
25382 token->flags));
25383 if (attr_ns
25384 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25386 if (cxx_dialect < cxx17
25387 && !in_system_header_at (input_location))
25388 pedwarn (input_location, 0,
25389 "attribute using prefix only available "
25390 "with -std=c++17 or -std=gnu++17");
25392 cp_lexer_consume_token (parser->lexer);
25393 cp_lexer_consume_token (parser->lexer);
25394 cp_lexer_consume_token (parser->lexer);
25396 else
25397 attr_ns = NULL_TREE;
25400 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25402 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25403 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25404 cp_parser_skip_to_end_of_statement (parser);
25405 else
25406 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25407 when we are sure that we have actually parsed them. */
25408 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25410 else
25412 tree alignas_expr;
25414 /* Look for an alignment-specifier. */
25416 token = cp_lexer_peek_token (parser->lexer);
25418 if (token->type != CPP_KEYWORD
25419 || token->keyword != RID_ALIGNAS)
25420 return NULL_TREE;
25422 cp_lexer_consume_token (parser->lexer);
25423 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25425 matching_parens parens;
25426 if (!parens.require_open (parser))
25427 return error_mark_node;
25429 cp_parser_parse_tentatively (parser);
25430 alignas_expr = cp_parser_type_id (parser);
25432 if (!cp_parser_parse_definitely (parser))
25434 alignas_expr = cp_parser_assignment_expression (parser);
25435 if (alignas_expr == error_mark_node)
25436 cp_parser_skip_to_end_of_statement (parser);
25437 if (alignas_expr == NULL_TREE
25438 || alignas_expr == error_mark_node)
25439 return alignas_expr;
25442 alignas_expr = cxx_alignas_expr (alignas_expr);
25443 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25445 /* Handle alignas (pack...). */
25446 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25448 cp_lexer_consume_token (parser->lexer);
25449 alignas_expr = make_pack_expansion (alignas_expr);
25452 /* Something went wrong, so don't build the attribute. */
25453 if (alignas_expr == error_mark_node)
25454 return error_mark_node;
25456 if (!parens.require_close (parser))
25457 return error_mark_node;
25459 /* Build the C++-11 representation of an 'aligned'
25460 attribute. */
25461 attributes =
25462 build_tree_list (build_tree_list (get_identifier ("gnu"),
25463 get_identifier ("aligned")),
25464 alignas_expr);
25467 return attributes;
25470 /* Parse a standard C++-11 attribute-specifier-seq.
25472 attribute-specifier-seq:
25473 attribute-specifier-seq [opt] attribute-specifier
25476 static tree
25477 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25479 tree attr_specs = NULL_TREE;
25480 tree attr_last = NULL_TREE;
25482 while (true)
25484 tree attr_spec = cp_parser_std_attribute_spec (parser);
25485 if (attr_spec == NULL_TREE)
25486 break;
25487 if (attr_spec == error_mark_node)
25488 return error_mark_node;
25490 if (attr_last)
25491 TREE_CHAIN (attr_last) = attr_spec;
25492 else
25493 attr_specs = attr_last = attr_spec;
25494 attr_last = tree_last (attr_last);
25497 return attr_specs;
25500 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25501 return index of the first token after balanced-token, or N on failure. */
25503 static size_t
25504 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25506 size_t orig_n = n;
25507 int nparens = 0, nbraces = 0, nsquares = 0;
25509 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25511 case CPP_EOF:
25512 case CPP_PRAGMA_EOL:
25513 /* Ran out of tokens. */
25514 return orig_n;
25515 case CPP_OPEN_PAREN:
25516 ++nparens;
25517 break;
25518 case CPP_OPEN_BRACE:
25519 ++nbraces;
25520 break;
25521 case CPP_OPEN_SQUARE:
25522 ++nsquares;
25523 break;
25524 case CPP_CLOSE_PAREN:
25525 --nparens;
25526 break;
25527 case CPP_CLOSE_BRACE:
25528 --nbraces;
25529 break;
25530 case CPP_CLOSE_SQUARE:
25531 --nsquares;
25532 break;
25533 default:
25534 break;
25536 while (nparens || nbraces || nsquares);
25537 return n;
25540 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25541 return index of the first token after the GNU attribute tokens, or N on
25542 failure. */
25544 static size_t
25545 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25547 while (true)
25549 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25550 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25551 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25552 break;
25554 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25555 if (n2 == n + 2)
25556 break;
25557 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25558 break;
25559 n = n2 + 1;
25561 return n;
25564 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25565 next token), return index of the first token after the standard C++11
25566 attribute tokens, or N on failure. */
25568 static size_t
25569 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25571 while (true)
25573 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25574 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25576 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25577 if (n2 == n + 1)
25578 break;
25579 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25580 break;
25581 n = n2 + 1;
25583 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25584 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25586 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25587 if (n2 == n + 1)
25588 break;
25589 n = n2;
25591 else
25592 break;
25594 return n;
25597 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25598 as the next token), return index of the first token after the attribute
25599 tokens, or N on failure. */
25601 static size_t
25602 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25604 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25605 return cp_parser_skip_gnu_attributes_opt (parser, n);
25606 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25609 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25610 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25611 current value of the PEDANTIC flag, regardless of whether or not
25612 the `__extension__' keyword is present. The caller is responsible
25613 for restoring the value of the PEDANTIC flag. */
25615 static bool
25616 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25618 /* Save the old value of the PEDANTIC flag. */
25619 *saved_pedantic = pedantic;
25621 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25623 /* Consume the `__extension__' token. */
25624 cp_lexer_consume_token (parser->lexer);
25625 /* We're not being pedantic while the `__extension__' keyword is
25626 in effect. */
25627 pedantic = 0;
25629 return true;
25632 return false;
25635 /* Parse a label declaration.
25637 label-declaration:
25638 __label__ label-declarator-seq ;
25640 label-declarator-seq:
25641 identifier , label-declarator-seq
25642 identifier */
25644 static void
25645 cp_parser_label_declaration (cp_parser* parser)
25647 /* Look for the `__label__' keyword. */
25648 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25650 while (true)
25652 tree identifier;
25654 /* Look for an identifier. */
25655 identifier = cp_parser_identifier (parser);
25656 /* If we failed, stop. */
25657 if (identifier == error_mark_node)
25658 break;
25659 /* Declare it as a label. */
25660 finish_label_decl (identifier);
25661 /* If the next token is a `;', stop. */
25662 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25663 break;
25664 /* Look for the `,' separating the label declarations. */
25665 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25668 /* Look for the final `;'. */
25669 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25672 // -------------------------------------------------------------------------- //
25673 // Requires Clause
25675 // Parse a requires clause.
25677 // requires-clause:
25678 // 'requires' logical-or-expression
25680 // The required logical-or-expression must be a constant expression. Note
25681 // that we don't check that the expression is constepxr here. We defer until
25682 // we analyze constraints and then, we only check atomic constraints.
25683 static tree
25684 cp_parser_requires_clause (cp_parser *parser)
25686 // Parse the requires clause so that it is not automatically folded.
25687 ++processing_template_decl;
25688 tree expr = cp_parser_binary_expression (parser, false, false,
25689 PREC_NOT_OPERATOR, NULL);
25690 if (check_for_bare_parameter_packs (expr))
25691 expr = error_mark_node;
25692 --processing_template_decl;
25693 return expr;
25696 // Optionally parse a requires clause:
25697 static tree
25698 cp_parser_requires_clause_opt (cp_parser *parser)
25700 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25701 if (tok->keyword != RID_REQUIRES)
25703 if (!flag_concepts && tok->type == CPP_NAME
25704 && tok->u.value == ridpointers[RID_REQUIRES])
25706 error_at (cp_lexer_peek_token (parser->lexer)->location,
25707 "%<requires%> only available with -fconcepts");
25708 /* Parse and discard the requires-clause. */
25709 cp_lexer_consume_token (parser->lexer);
25710 cp_parser_requires_clause (parser);
25712 return NULL_TREE;
25714 cp_lexer_consume_token (parser->lexer);
25715 return cp_parser_requires_clause (parser);
25719 /*---------------------------------------------------------------------------
25720 Requires expressions
25721 ---------------------------------------------------------------------------*/
25723 /* Parse a requires expression
25725 requirement-expression:
25726 'requires' requirement-parameter-list [opt] requirement-body */
25727 static tree
25728 cp_parser_requires_expression (cp_parser *parser)
25730 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25731 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25733 /* A requires-expression shall appear only within a concept
25734 definition or a requires-clause.
25736 TODO: Implement this diagnostic correctly. */
25737 if (!processing_template_decl)
25739 error_at (loc, "a requires expression cannot appear outside a template");
25740 cp_parser_skip_to_end_of_statement (parser);
25741 return error_mark_node;
25744 tree parms, reqs;
25746 /* Local parameters are delared as variables within the scope
25747 of the expression. They are not visible past the end of
25748 the expression. Expressions within the requires-expression
25749 are unevaluated. */
25750 struct scope_sentinel
25752 scope_sentinel ()
25754 ++cp_unevaluated_operand;
25755 begin_scope (sk_block, NULL_TREE);
25758 ~scope_sentinel ()
25760 pop_bindings_and_leave_scope ();
25761 --cp_unevaluated_operand;
25763 } s;
25765 /* Parse the optional parameter list. */
25766 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25768 parms = cp_parser_requirement_parameter_list (parser);
25769 if (parms == error_mark_node)
25770 return error_mark_node;
25772 else
25773 parms = NULL_TREE;
25775 /* Parse the requirement body. */
25776 reqs = cp_parser_requirement_body (parser);
25777 if (reqs == error_mark_node)
25778 return error_mark_node;
25781 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25782 the parm chain. */
25783 grokparms (parms, &parms);
25784 return finish_requires_expr (parms, reqs);
25787 /* Parse a parameterized requirement.
25789 requirement-parameter-list:
25790 '(' parameter-declaration-clause ')' */
25791 static tree
25792 cp_parser_requirement_parameter_list (cp_parser *parser)
25794 matching_parens parens;
25795 if (!parens.require_open (parser))
25796 return error_mark_node;
25798 tree parms = cp_parser_parameter_declaration_clause (parser);
25800 if (!parens.require_close (parser))
25801 return error_mark_node;
25803 return parms;
25806 /* Parse the body of a requirement.
25808 requirement-body:
25809 '{' requirement-list '}' */
25810 static tree
25811 cp_parser_requirement_body (cp_parser *parser)
25813 matching_braces braces;
25814 if (!braces.require_open (parser))
25815 return error_mark_node;
25817 tree reqs = cp_parser_requirement_list (parser);
25819 if (!braces.require_close (parser))
25820 return error_mark_node;
25822 return reqs;
25825 /* Parse a list of requirements.
25827 requirement-list:
25828 requirement
25829 requirement-list ';' requirement[opt] */
25830 static tree
25831 cp_parser_requirement_list (cp_parser *parser)
25833 tree result = NULL_TREE;
25834 while (true)
25836 tree req = cp_parser_requirement (parser);
25837 if (req == error_mark_node)
25838 return error_mark_node;
25840 result = tree_cons (NULL_TREE, req, result);
25842 /* If we see a semi-colon, consume it. */
25843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25844 cp_lexer_consume_token (parser->lexer);
25846 /* Stop processing at the end of the list. */
25847 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25848 break;
25851 /* Reverse the order of requirements so they are analyzed in
25852 declaration order. */
25853 return nreverse (result);
25856 /* Parse a syntactic requirement or type requirement.
25858 requirement:
25859 simple-requirement
25860 compound-requirement
25861 type-requirement
25862 nested-requirement */
25863 static tree
25864 cp_parser_requirement (cp_parser *parser)
25866 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25867 return cp_parser_compound_requirement (parser);
25868 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25869 return cp_parser_type_requirement (parser);
25870 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25871 return cp_parser_nested_requirement (parser);
25872 else
25873 return cp_parser_simple_requirement (parser);
25876 /* Parse a simple requirement.
25878 simple-requirement:
25879 expression ';' */
25880 static tree
25881 cp_parser_simple_requirement (cp_parser *parser)
25883 tree expr = cp_parser_expression (parser, NULL, false, false);
25884 if (!expr || expr == error_mark_node)
25885 return error_mark_node;
25887 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25888 return error_mark_node;
25890 return finish_simple_requirement (expr);
25893 /* Parse a type requirement
25895 type-requirement
25896 nested-name-specifier [opt] required-type-name ';'
25898 required-type-name:
25899 type-name
25900 'template' [opt] simple-template-id */
25901 static tree
25902 cp_parser_type_requirement (cp_parser *parser)
25904 cp_lexer_consume_token (parser->lexer);
25906 // Save the scope before parsing name specifiers.
25907 tree saved_scope = parser->scope;
25908 tree saved_object_scope = parser->object_scope;
25909 tree saved_qualifying_scope = parser->qualifying_scope;
25910 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25911 cp_parser_nested_name_specifier_opt (parser,
25912 /*typename_keyword_p=*/true,
25913 /*check_dependency_p=*/false,
25914 /*type_p=*/true,
25915 /*is_declaration=*/false);
25917 tree type;
25918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25920 cp_lexer_consume_token (parser->lexer);
25921 type = cp_parser_template_id (parser,
25922 /*template_keyword_p=*/true,
25923 /*check_dependency=*/false,
25924 /*tag_type=*/none_type,
25925 /*is_declaration=*/false);
25926 type = make_typename_type (parser->scope, type, typename_type,
25927 /*complain=*/tf_error);
25929 else
25930 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25932 if (TREE_CODE (type) == TYPE_DECL)
25933 type = TREE_TYPE (type);
25935 parser->scope = saved_scope;
25936 parser->object_scope = saved_object_scope;
25937 parser->qualifying_scope = saved_qualifying_scope;
25939 if (type == error_mark_node)
25940 cp_parser_skip_to_end_of_statement (parser);
25942 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25943 return error_mark_node;
25944 if (type == error_mark_node)
25945 return error_mark_node;
25947 return finish_type_requirement (type);
25950 /* Parse a compound requirement
25952 compound-requirement:
25953 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25954 static tree
25955 cp_parser_compound_requirement (cp_parser *parser)
25957 /* Parse an expression enclosed in '{ }'s. */
25958 matching_braces braces;
25959 if (!braces.require_open (parser))
25960 return error_mark_node;
25962 tree expr = cp_parser_expression (parser, NULL, false, false);
25963 if (!expr || expr == error_mark_node)
25964 return error_mark_node;
25966 if (!braces.require_close (parser))
25967 return error_mark_node;
25969 /* Parse the optional noexcept. */
25970 bool noexcept_p = false;
25971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25973 cp_lexer_consume_token (parser->lexer);
25974 noexcept_p = true;
25977 /* Parse the optional trailing return type. */
25978 tree type = NULL_TREE;
25979 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25981 cp_lexer_consume_token (parser->lexer);
25982 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25983 parser->in_result_type_constraint_p = true;
25984 type = cp_parser_trailing_type_id (parser);
25985 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25986 if (type == error_mark_node)
25987 return error_mark_node;
25990 return finish_compound_requirement (expr, type, noexcept_p);
25993 /* Parse a nested requirement. This is the same as a requires clause.
25995 nested-requirement:
25996 requires-clause */
25997 static tree
25998 cp_parser_nested_requirement (cp_parser *parser)
26000 cp_lexer_consume_token (parser->lexer);
26001 tree req = cp_parser_requires_clause (parser);
26002 if (req == error_mark_node)
26003 return error_mark_node;
26004 return finish_nested_requirement (req);
26007 /* Support Functions */
26009 /* Return the appropriate prefer_type argument for lookup_name_real based on
26010 tag_type and template_mem_access. */
26012 static inline int
26013 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26015 /* DR 141: When looking in the current enclosing context for a template-name
26016 after -> or ., only consider class templates. */
26017 if (template_mem_access)
26018 return 2;
26019 switch (tag_type)
26021 case none_type: return 0; // No preference.
26022 case scope_type: return 1; // Type or namespace.
26023 default: return 2; // Type only.
26027 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26028 NAME should have one of the representations used for an
26029 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26030 is returned. If PARSER->SCOPE is a dependent type, then a
26031 SCOPE_REF is returned.
26033 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26034 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26035 was formed. Abstractly, such entities should not be passed to this
26036 function, because they do not need to be looked up, but it is
26037 simpler to check for this special case here, rather than at the
26038 call-sites.
26040 In cases not explicitly covered above, this function returns a
26041 DECL, OVERLOAD, or baselink representing the result of the lookup.
26042 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26043 is returned.
26045 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26046 (e.g., "struct") that was used. In that case bindings that do not
26047 refer to types are ignored.
26049 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26050 ignored.
26052 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26053 are ignored.
26055 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26056 types.
26058 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26059 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26060 NULL_TREE otherwise. */
26062 static cp_expr
26063 cp_parser_lookup_name (cp_parser *parser, tree name,
26064 enum tag_types tag_type,
26065 bool is_template,
26066 bool is_namespace,
26067 bool check_dependency,
26068 tree *ambiguous_decls,
26069 location_t name_location)
26071 tree decl;
26072 tree object_type = parser->context->object_type;
26074 /* Assume that the lookup will be unambiguous. */
26075 if (ambiguous_decls)
26076 *ambiguous_decls = NULL_TREE;
26078 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26079 no longer valid. Note that if we are parsing tentatively, and
26080 the parse fails, OBJECT_TYPE will be automatically restored. */
26081 parser->context->object_type = NULL_TREE;
26083 if (name == error_mark_node)
26084 return error_mark_node;
26086 /* A template-id has already been resolved; there is no lookup to
26087 do. */
26088 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26089 return name;
26090 if (BASELINK_P (name))
26092 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26093 == TEMPLATE_ID_EXPR);
26094 return name;
26097 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26098 it should already have been checked to make sure that the name
26099 used matches the type being destroyed. */
26100 if (TREE_CODE (name) == BIT_NOT_EXPR)
26102 tree type;
26104 /* Figure out to which type this destructor applies. */
26105 if (parser->scope)
26106 type = parser->scope;
26107 else if (object_type)
26108 type = object_type;
26109 else
26110 type = current_class_type;
26111 /* If that's not a class type, there is no destructor. */
26112 if (!type || !CLASS_TYPE_P (type))
26113 return error_mark_node;
26115 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26116 lazily_declare_fn (sfk_destructor, type);
26118 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26119 return dtor;
26121 return error_mark_node;
26124 /* By this point, the NAME should be an ordinary identifier. If
26125 the id-expression was a qualified name, the qualifying scope is
26126 stored in PARSER->SCOPE at this point. */
26127 gcc_assert (identifier_p (name));
26129 /* Perform the lookup. */
26130 if (parser->scope)
26132 bool dependent_p;
26134 if (parser->scope == error_mark_node)
26135 return error_mark_node;
26137 /* If the SCOPE is dependent, the lookup must be deferred until
26138 the template is instantiated -- unless we are explicitly
26139 looking up names in uninstantiated templates. Even then, we
26140 cannot look up the name if the scope is not a class type; it
26141 might, for example, be a template type parameter. */
26142 dependent_p = (TYPE_P (parser->scope)
26143 && dependent_scope_p (parser->scope));
26144 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26145 && dependent_p)
26146 /* Defer lookup. */
26147 decl = error_mark_node;
26148 else
26150 tree pushed_scope = NULL_TREE;
26152 /* If PARSER->SCOPE is a dependent type, then it must be a
26153 class type, and we must not be checking dependencies;
26154 otherwise, we would have processed this lookup above. So
26155 that PARSER->SCOPE is not considered a dependent base by
26156 lookup_member, we must enter the scope here. */
26157 if (dependent_p)
26158 pushed_scope = push_scope (parser->scope);
26160 /* If the PARSER->SCOPE is a template specialization, it
26161 may be instantiated during name lookup. In that case,
26162 errors may be issued. Even if we rollback the current
26163 tentative parse, those errors are valid. */
26164 decl = lookup_qualified_name (parser->scope, name,
26165 prefer_type_arg (tag_type),
26166 /*complain=*/true);
26168 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26169 lookup result and the nested-name-specifier nominates a class C:
26170 * if the name specified after the nested-name-specifier, when
26171 looked up in C, is the injected-class-name of C (Clause 9), or
26172 * if the name specified after the nested-name-specifier is the
26173 same as the identifier or the simple-template-id's template-
26174 name in the last component of the nested-name-specifier,
26175 the name is instead considered to name the constructor of
26176 class C. [ Note: for example, the constructor is not an
26177 acceptable lookup result in an elaborated-type-specifier so
26178 the constructor would not be used in place of the
26179 injected-class-name. --end note ] Such a constructor name
26180 shall be used only in the declarator-id of a declaration that
26181 names a constructor or in a using-declaration. */
26182 if (tag_type == none_type
26183 && DECL_SELF_REFERENCE_P (decl)
26184 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26185 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26186 prefer_type_arg (tag_type),
26187 /*complain=*/true);
26189 /* If we have a single function from a using decl, pull it out. */
26190 if (TREE_CODE (decl) == OVERLOAD
26191 && !really_overloaded_fn (decl))
26192 decl = OVL_FUNCTION (decl);
26194 if (pushed_scope)
26195 pop_scope (pushed_scope);
26198 /* If the scope is a dependent type and either we deferred lookup or
26199 we did lookup but didn't find the name, rememeber the name. */
26200 if (decl == error_mark_node && TYPE_P (parser->scope)
26201 && dependent_type_p (parser->scope))
26203 if (tag_type)
26205 tree type;
26207 /* The resolution to Core Issue 180 says that `struct
26208 A::B' should be considered a type-name, even if `A'
26209 is dependent. */
26210 type = make_typename_type (parser->scope, name, tag_type,
26211 /*complain=*/tf_error);
26212 if (type != error_mark_node)
26213 decl = TYPE_NAME (type);
26215 else if (is_template
26216 && (cp_parser_next_token_ends_template_argument_p (parser)
26217 || cp_lexer_next_token_is (parser->lexer,
26218 CPP_CLOSE_PAREN)))
26219 decl = make_unbound_class_template (parser->scope,
26220 name, NULL_TREE,
26221 /*complain=*/tf_error);
26222 else
26223 decl = build_qualified_name (/*type=*/NULL_TREE,
26224 parser->scope, name,
26225 is_template);
26227 parser->qualifying_scope = parser->scope;
26228 parser->object_scope = NULL_TREE;
26230 else if (object_type)
26232 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26233 OBJECT_TYPE is not a class. */
26234 if (CLASS_TYPE_P (object_type))
26235 /* If the OBJECT_TYPE is a template specialization, it may
26236 be instantiated during name lookup. In that case, errors
26237 may be issued. Even if we rollback the current tentative
26238 parse, those errors are valid. */
26239 decl = lookup_member (object_type,
26240 name,
26241 /*protect=*/0,
26242 prefer_type_arg (tag_type),
26243 tf_warning_or_error);
26244 else
26245 decl = NULL_TREE;
26247 if (!decl)
26248 /* Look it up in the enclosing context. DR 141: When looking for a
26249 template-name after -> or ., only consider class templates. */
26250 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26251 /*nonclass=*/0,
26252 /*block_p=*/true, is_namespace, 0);
26253 if (object_type == unknown_type_node)
26254 /* The object is type-dependent, so we can't look anything up; we used
26255 this to get the DR 141 behavior. */
26256 object_type = NULL_TREE;
26257 parser->object_scope = object_type;
26258 parser->qualifying_scope = NULL_TREE;
26260 else
26262 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26263 /*nonclass=*/0,
26264 /*block_p=*/true, is_namespace, 0);
26265 parser->qualifying_scope = NULL_TREE;
26266 parser->object_scope = NULL_TREE;
26269 /* If the lookup failed, let our caller know. */
26270 if (!decl || decl == error_mark_node)
26271 return error_mark_node;
26273 /* Pull out the template from an injected-class-name (or multiple). */
26274 if (is_template)
26275 decl = maybe_get_template_decl_from_type_decl (decl);
26277 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26278 if (TREE_CODE (decl) == TREE_LIST)
26280 if (ambiguous_decls)
26281 *ambiguous_decls = decl;
26282 /* The error message we have to print is too complicated for
26283 cp_parser_error, so we incorporate its actions directly. */
26284 if (!cp_parser_simulate_error (parser))
26286 error_at (name_location, "reference to %qD is ambiguous",
26287 name);
26288 print_candidates (decl);
26290 return error_mark_node;
26293 gcc_assert (DECL_P (decl)
26294 || TREE_CODE (decl) == OVERLOAD
26295 || TREE_CODE (decl) == SCOPE_REF
26296 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26297 || BASELINK_P (decl));
26299 /* If we have resolved the name of a member declaration, check to
26300 see if the declaration is accessible. When the name resolves to
26301 set of overloaded functions, accessibility is checked when
26302 overload resolution is done.
26304 During an explicit instantiation, access is not checked at all,
26305 as per [temp.explicit]. */
26306 if (DECL_P (decl))
26307 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26309 maybe_record_typedef_use (decl);
26311 return cp_expr (decl, name_location);
26314 /* Like cp_parser_lookup_name, but for use in the typical case where
26315 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26316 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26318 static tree
26319 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26321 return cp_parser_lookup_name (parser, name,
26322 none_type,
26323 /*is_template=*/false,
26324 /*is_namespace=*/false,
26325 /*check_dependency=*/true,
26326 /*ambiguous_decls=*/NULL,
26327 location);
26330 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26331 the current context, return the TYPE_DECL. If TAG_NAME_P is
26332 true, the DECL indicates the class being defined in a class-head,
26333 or declared in an elaborated-type-specifier.
26335 Otherwise, return DECL. */
26337 static tree
26338 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26340 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26341 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26343 struct A {
26344 template <typename T> struct B;
26347 template <typename T> struct A::B {};
26349 Similarly, in an elaborated-type-specifier:
26351 namespace N { struct X{}; }
26353 struct A {
26354 template <typename T> friend struct N::X;
26357 However, if the DECL refers to a class type, and we are in
26358 the scope of the class, then the name lookup automatically
26359 finds the TYPE_DECL created by build_self_reference rather
26360 than a TEMPLATE_DECL. For example, in:
26362 template <class T> struct S {
26363 S s;
26366 there is no need to handle such case. */
26368 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26369 return DECL_TEMPLATE_RESULT (decl);
26371 return decl;
26374 /* If too many, or too few, template-parameter lists apply to the
26375 declarator, issue an error message. Returns TRUE if all went well,
26376 and FALSE otherwise. */
26378 static bool
26379 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26380 cp_declarator *declarator,
26381 location_t declarator_location)
26383 switch (declarator->kind)
26385 case cdk_id:
26387 unsigned num_templates = 0;
26388 tree scope = declarator->u.id.qualifying_scope;
26390 if (scope)
26391 num_templates = num_template_headers_for_class (scope);
26392 else if (TREE_CODE (declarator->u.id.unqualified_name)
26393 == TEMPLATE_ID_EXPR)
26394 /* If the DECLARATOR has the form `X<y>' then it uses one
26395 additional level of template parameters. */
26396 ++num_templates;
26398 return cp_parser_check_template_parameters
26399 (parser, num_templates, declarator_location, declarator);
26402 case cdk_function:
26403 case cdk_array:
26404 case cdk_pointer:
26405 case cdk_reference:
26406 case cdk_ptrmem:
26407 return (cp_parser_check_declarator_template_parameters
26408 (parser, declarator->declarator, declarator_location));
26410 case cdk_decomp:
26411 case cdk_error:
26412 return true;
26414 default:
26415 gcc_unreachable ();
26417 return false;
26420 /* NUM_TEMPLATES were used in the current declaration. If that is
26421 invalid, return FALSE and issue an error messages. Otherwise,
26422 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26423 declarator and we can print more accurate diagnostics. */
26425 static bool
26426 cp_parser_check_template_parameters (cp_parser* parser,
26427 unsigned num_templates,
26428 location_t location,
26429 cp_declarator *declarator)
26431 /* If there are the same number of template classes and parameter
26432 lists, that's OK. */
26433 if (parser->num_template_parameter_lists == num_templates)
26434 return true;
26435 /* If there are more, but only one more, then we are referring to a
26436 member template. That's OK too. */
26437 if (parser->num_template_parameter_lists == num_templates + 1)
26438 return true;
26439 /* If there are more template classes than parameter lists, we have
26440 something like:
26442 template <class T> void S<T>::R<T>::f (); */
26443 if (parser->num_template_parameter_lists < num_templates)
26445 if (declarator && !current_function_decl)
26446 error_at (location, "specializing member %<%T::%E%> "
26447 "requires %<template<>%> syntax",
26448 declarator->u.id.qualifying_scope,
26449 declarator->u.id.unqualified_name);
26450 else if (declarator)
26451 error_at (location, "invalid declaration of %<%T::%E%>",
26452 declarator->u.id.qualifying_scope,
26453 declarator->u.id.unqualified_name);
26454 else
26455 error_at (location, "too few template-parameter-lists");
26456 return false;
26458 /* Otherwise, there are too many template parameter lists. We have
26459 something like:
26461 template <class T> template <class U> void S::f(); */
26462 error_at (location, "too many template-parameter-lists");
26463 return false;
26466 /* Parse an optional `::' token indicating that the following name is
26467 from the global namespace. If so, PARSER->SCOPE is set to the
26468 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26469 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26470 Returns the new value of PARSER->SCOPE, if the `::' token is
26471 present, and NULL_TREE otherwise. */
26473 static tree
26474 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26476 cp_token *token;
26478 /* Peek at the next token. */
26479 token = cp_lexer_peek_token (parser->lexer);
26480 /* If we're looking at a `::' token then we're starting from the
26481 global namespace, not our current location. */
26482 if (token->type == CPP_SCOPE)
26484 /* Consume the `::' token. */
26485 cp_lexer_consume_token (parser->lexer);
26486 /* Set the SCOPE so that we know where to start the lookup. */
26487 parser->scope = global_namespace;
26488 parser->qualifying_scope = global_namespace;
26489 parser->object_scope = NULL_TREE;
26491 return parser->scope;
26493 else if (!current_scope_valid_p)
26495 parser->scope = NULL_TREE;
26496 parser->qualifying_scope = NULL_TREE;
26497 parser->object_scope = NULL_TREE;
26500 return NULL_TREE;
26503 /* Returns TRUE if the upcoming token sequence is the start of a
26504 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26505 declarator is preceded by the `friend' specifier. */
26507 static bool
26508 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26510 bool constructor_p;
26511 bool outside_class_specifier_p;
26512 tree nested_name_specifier;
26513 cp_token *next_token;
26515 /* The common case is that this is not a constructor declarator, so
26516 try to avoid doing lots of work if at all possible. It's not
26517 valid declare a constructor at function scope. */
26518 if (parser->in_function_body)
26519 return false;
26520 /* And only certain tokens can begin a constructor declarator. */
26521 next_token = cp_lexer_peek_token (parser->lexer);
26522 if (next_token->type != CPP_NAME
26523 && next_token->type != CPP_SCOPE
26524 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26525 && next_token->type != CPP_TEMPLATE_ID)
26526 return false;
26528 /* Parse tentatively; we are going to roll back all of the tokens
26529 consumed here. */
26530 cp_parser_parse_tentatively (parser);
26531 /* Assume that we are looking at a constructor declarator. */
26532 constructor_p = true;
26534 /* Look for the optional `::' operator. */
26535 cp_parser_global_scope_opt (parser,
26536 /*current_scope_valid_p=*/false);
26537 /* Look for the nested-name-specifier. */
26538 nested_name_specifier
26539 = (cp_parser_nested_name_specifier_opt (parser,
26540 /*typename_keyword_p=*/false,
26541 /*check_dependency_p=*/false,
26542 /*type_p=*/false,
26543 /*is_declaration=*/false));
26545 outside_class_specifier_p = (!at_class_scope_p ()
26546 || !TYPE_BEING_DEFINED (current_class_type)
26547 || friend_p);
26549 /* Outside of a class-specifier, there must be a
26550 nested-name-specifier. Except in C++17 mode, where we
26551 might be declaring a guiding declaration. */
26552 if (!nested_name_specifier && outside_class_specifier_p
26553 && cxx_dialect < cxx17)
26554 constructor_p = false;
26555 else if (nested_name_specifier == error_mark_node)
26556 constructor_p = false;
26558 /* If we have a class scope, this is easy; DR 147 says that S::S always
26559 names the constructor, and no other qualified name could. */
26560 if (constructor_p && nested_name_specifier
26561 && CLASS_TYPE_P (nested_name_specifier))
26563 tree id = cp_parser_unqualified_id (parser,
26564 /*template_keyword_p=*/false,
26565 /*check_dependency_p=*/false,
26566 /*declarator_p=*/true,
26567 /*optional_p=*/false);
26568 if (is_overloaded_fn (id))
26569 id = DECL_NAME (get_first_fn (id));
26570 if (!constructor_name_p (id, nested_name_specifier))
26571 constructor_p = false;
26573 /* If we still think that this might be a constructor-declarator,
26574 look for a class-name. */
26575 else if (constructor_p)
26577 /* If we have:
26579 template <typename T> struct S {
26580 S();
26583 we must recognize that the nested `S' names a class. */
26584 if (cxx_dialect >= cxx17)
26585 cp_parser_parse_tentatively (parser);
26587 tree type_decl;
26588 type_decl = cp_parser_class_name (parser,
26589 /*typename_keyword_p=*/false,
26590 /*template_keyword_p=*/false,
26591 none_type,
26592 /*check_dependency_p=*/false,
26593 /*class_head_p=*/false,
26594 /*is_declaration=*/false);
26596 if (cxx_dialect >= cxx17
26597 && !cp_parser_parse_definitely (parser))
26599 type_decl = NULL_TREE;
26600 tree tmpl = cp_parser_template_name (parser,
26601 /*template_keyword*/false,
26602 /*check_dependency_p*/false,
26603 /*is_declaration*/false,
26604 none_type,
26605 /*is_identifier*/NULL);
26606 if (DECL_CLASS_TEMPLATE_P (tmpl)
26607 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26608 /* It's a deduction guide, return true. */;
26609 else
26610 cp_parser_simulate_error (parser);
26613 /* If there was no class-name, then this is not a constructor.
26614 Otherwise, if we are in a class-specifier and we aren't
26615 handling a friend declaration, check that its type matches
26616 current_class_type (c++/38313). Note: error_mark_node
26617 is left alone for error recovery purposes. */
26618 constructor_p = (!cp_parser_error_occurred (parser)
26619 && (outside_class_specifier_p
26620 || type_decl == NULL_TREE
26621 || type_decl == error_mark_node
26622 || same_type_p (current_class_type,
26623 TREE_TYPE (type_decl))));
26625 /* If we're still considering a constructor, we have to see a `(',
26626 to begin the parameter-declaration-clause, followed by either a
26627 `)', an `...', or a decl-specifier. We need to check for a
26628 type-specifier to avoid being fooled into thinking that:
26630 S (f) (int);
26632 is a constructor. (It is actually a function named `f' that
26633 takes one parameter (of type `int') and returns a value of type
26634 `S'. */
26635 if (constructor_p
26636 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26637 constructor_p = false;
26639 if (constructor_p
26640 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26641 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26642 /* A parameter declaration begins with a decl-specifier,
26643 which is either the "attribute" keyword, a storage class
26644 specifier, or (usually) a type-specifier. */
26645 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26647 tree type;
26648 tree pushed_scope = NULL_TREE;
26649 unsigned saved_num_template_parameter_lists;
26651 /* Names appearing in the type-specifier should be looked up
26652 in the scope of the class. */
26653 if (current_class_type)
26654 type = NULL_TREE;
26655 else if (type_decl)
26657 type = TREE_TYPE (type_decl);
26658 if (TREE_CODE (type) == TYPENAME_TYPE)
26660 type = resolve_typename_type (type,
26661 /*only_current_p=*/false);
26662 if (TREE_CODE (type) == TYPENAME_TYPE)
26664 cp_parser_abort_tentative_parse (parser);
26665 return false;
26668 pushed_scope = push_scope (type);
26671 /* Inside the constructor parameter list, surrounding
26672 template-parameter-lists do not apply. */
26673 saved_num_template_parameter_lists
26674 = parser->num_template_parameter_lists;
26675 parser->num_template_parameter_lists = 0;
26677 /* Look for the type-specifier. */
26678 cp_parser_type_specifier (parser,
26679 CP_PARSER_FLAGS_NONE,
26680 /*decl_specs=*/NULL,
26681 /*is_declarator=*/true,
26682 /*declares_class_or_enum=*/NULL,
26683 /*is_cv_qualifier=*/NULL);
26685 parser->num_template_parameter_lists
26686 = saved_num_template_parameter_lists;
26688 /* Leave the scope of the class. */
26689 if (pushed_scope)
26690 pop_scope (pushed_scope);
26692 constructor_p = !cp_parser_error_occurred (parser);
26696 /* We did not really want to consume any tokens. */
26697 cp_parser_abort_tentative_parse (parser);
26699 return constructor_p;
26702 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26703 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26704 they must be performed once we are in the scope of the function.
26706 Returns the function defined. */
26708 static tree
26709 cp_parser_function_definition_from_specifiers_and_declarator
26710 (cp_parser* parser,
26711 cp_decl_specifier_seq *decl_specifiers,
26712 tree attributes,
26713 const cp_declarator *declarator)
26715 tree fn;
26716 bool success_p;
26718 /* Begin the function-definition. */
26719 success_p = start_function (decl_specifiers, declarator, attributes);
26721 /* The things we're about to see are not directly qualified by any
26722 template headers we've seen thus far. */
26723 reset_specialization ();
26725 /* If there were names looked up in the decl-specifier-seq that we
26726 did not check, check them now. We must wait until we are in the
26727 scope of the function to perform the checks, since the function
26728 might be a friend. */
26729 perform_deferred_access_checks (tf_warning_or_error);
26731 if (success_p)
26733 cp_finalize_omp_declare_simd (parser, current_function_decl);
26734 parser->omp_declare_simd = NULL;
26735 cp_finalize_oacc_routine (parser, current_function_decl, true);
26736 parser->oacc_routine = NULL;
26739 if (!success_p)
26741 /* Skip the entire function. */
26742 cp_parser_skip_to_end_of_block_or_statement (parser);
26743 fn = error_mark_node;
26745 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26747 /* Seen already, skip it. An error message has already been output. */
26748 cp_parser_skip_to_end_of_block_or_statement (parser);
26749 fn = current_function_decl;
26750 current_function_decl = NULL_TREE;
26751 /* If this is a function from a class, pop the nested class. */
26752 if (current_class_name)
26753 pop_nested_class ();
26755 else
26757 timevar_id_t tv;
26758 if (DECL_DECLARED_INLINE_P (current_function_decl))
26759 tv = TV_PARSE_INLINE;
26760 else
26761 tv = TV_PARSE_FUNC;
26762 timevar_push (tv);
26763 fn = cp_parser_function_definition_after_declarator (parser,
26764 /*inline_p=*/false);
26765 timevar_pop (tv);
26768 return fn;
26771 /* Parse the part of a function-definition that follows the
26772 declarator. INLINE_P is TRUE iff this function is an inline
26773 function defined within a class-specifier.
26775 Returns the function defined. */
26777 static tree
26778 cp_parser_function_definition_after_declarator (cp_parser* parser,
26779 bool inline_p)
26781 tree fn;
26782 bool saved_in_unbraced_linkage_specification_p;
26783 bool saved_in_function_body;
26784 unsigned saved_num_template_parameter_lists;
26785 cp_token *token;
26786 bool fully_implicit_function_template_p
26787 = parser->fully_implicit_function_template_p;
26788 parser->fully_implicit_function_template_p = false;
26789 tree implicit_template_parms
26790 = parser->implicit_template_parms;
26791 parser->implicit_template_parms = 0;
26792 cp_binding_level* implicit_template_scope
26793 = parser->implicit_template_scope;
26794 parser->implicit_template_scope = 0;
26796 saved_in_function_body = parser->in_function_body;
26797 parser->in_function_body = true;
26798 /* If the next token is `return', then the code may be trying to
26799 make use of the "named return value" extension that G++ used to
26800 support. */
26801 token = cp_lexer_peek_token (parser->lexer);
26802 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26804 /* Consume the `return' keyword. */
26805 cp_lexer_consume_token (parser->lexer);
26806 /* Look for the identifier that indicates what value is to be
26807 returned. */
26808 cp_parser_identifier (parser);
26809 /* Issue an error message. */
26810 error_at (token->location,
26811 "named return values are no longer supported");
26812 /* Skip tokens until we reach the start of the function body. */
26813 while (true)
26815 cp_token *token = cp_lexer_peek_token (parser->lexer);
26816 if (token->type == CPP_OPEN_BRACE
26817 || token->type == CPP_EOF
26818 || token->type == CPP_PRAGMA_EOL)
26819 break;
26820 cp_lexer_consume_token (parser->lexer);
26823 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26824 anything declared inside `f'. */
26825 saved_in_unbraced_linkage_specification_p
26826 = parser->in_unbraced_linkage_specification_p;
26827 parser->in_unbraced_linkage_specification_p = false;
26828 /* Inside the function, surrounding template-parameter-lists do not
26829 apply. */
26830 saved_num_template_parameter_lists
26831 = parser->num_template_parameter_lists;
26832 parser->num_template_parameter_lists = 0;
26834 /* If the next token is `try', `__transaction_atomic', or
26835 `__transaction_relaxed`, then we are looking at either function-try-block
26836 or function-transaction-block. Note that all of these include the
26837 function-body. */
26838 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26839 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26840 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26841 RID_TRANSACTION_RELAXED))
26842 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26843 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26844 cp_parser_function_try_block (parser);
26845 else
26846 cp_parser_ctor_initializer_opt_and_function_body
26847 (parser, /*in_function_try_block=*/false);
26849 /* Finish the function. */
26850 fn = finish_function (inline_p);
26851 /* Generate code for it, if necessary. */
26852 expand_or_defer_fn (fn);
26853 /* Restore the saved values. */
26854 parser->in_unbraced_linkage_specification_p
26855 = saved_in_unbraced_linkage_specification_p;
26856 parser->num_template_parameter_lists
26857 = saved_num_template_parameter_lists;
26858 parser->in_function_body = saved_in_function_body;
26860 parser->fully_implicit_function_template_p
26861 = fully_implicit_function_template_p;
26862 parser->implicit_template_parms
26863 = implicit_template_parms;
26864 parser->implicit_template_scope
26865 = implicit_template_scope;
26867 if (parser->fully_implicit_function_template_p)
26868 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26870 return fn;
26873 /* Parse a template-declaration body (following argument list). */
26875 static void
26876 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26877 tree parameter_list,
26878 bool member_p)
26880 tree decl = NULL_TREE;
26881 bool friend_p = false;
26883 /* We just processed one more parameter list. */
26884 ++parser->num_template_parameter_lists;
26886 /* Get the deferred access checks from the parameter list. These
26887 will be checked once we know what is being declared, as for a
26888 member template the checks must be performed in the scope of the
26889 class containing the member. */
26890 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26892 /* Tentatively parse for a new template parameter list, which can either be
26893 the template keyword or a template introduction. */
26894 if (cp_parser_template_declaration_after_export (parser, member_p))
26895 /* OK */;
26896 else if (cxx_dialect >= cxx11
26897 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26898 decl = cp_parser_alias_declaration (parser);
26899 else
26901 /* There are no access checks when parsing a template, as we do not
26902 know if a specialization will be a friend. */
26903 push_deferring_access_checks (dk_no_check);
26904 cp_token *token = cp_lexer_peek_token (parser->lexer);
26905 decl = cp_parser_single_declaration (parser,
26906 checks,
26907 member_p,
26908 /*explicit_specialization_p=*/false,
26909 &friend_p);
26910 pop_deferring_access_checks ();
26912 /* If this is a member template declaration, let the front
26913 end know. */
26914 if (member_p && !friend_p && decl)
26916 if (TREE_CODE (decl) == TYPE_DECL)
26917 cp_parser_check_access_in_redeclaration (decl, token->location);
26919 decl = finish_member_template_decl (decl);
26921 else if (friend_p && decl
26922 && DECL_DECLARES_TYPE_P (decl))
26923 make_friend_class (current_class_type, TREE_TYPE (decl),
26924 /*complain=*/true);
26926 /* We are done with the current parameter list. */
26927 --parser->num_template_parameter_lists;
26929 pop_deferring_access_checks ();
26931 /* Finish up. */
26932 finish_template_decl (parameter_list);
26934 /* Check the template arguments for a literal operator template. */
26935 if (decl
26936 && DECL_DECLARES_FUNCTION_P (decl)
26937 && UDLIT_OPER_P (DECL_NAME (decl)))
26939 bool ok = true;
26940 if (parameter_list == NULL_TREE)
26941 ok = false;
26942 else
26944 int num_parms = TREE_VEC_LENGTH (parameter_list);
26945 if (num_parms == 1)
26947 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26948 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26949 if (TREE_TYPE (parm) != char_type_node
26950 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26951 ok = false;
26953 else if (num_parms == 2 && cxx_dialect >= cxx14)
26955 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26956 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26957 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26958 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26959 if (parm == error_mark_node
26960 || TREE_TYPE (parm) != TREE_TYPE (type)
26961 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26962 ok = false;
26964 else
26965 ok = false;
26967 if (!ok)
26969 if (cxx_dialect >= cxx14)
26970 error ("literal operator template %qD has invalid parameter list."
26971 " Expected non-type template argument pack <char...>"
26972 " or <typename CharT, CharT...>",
26973 decl);
26974 else
26975 error ("literal operator template %qD has invalid parameter list."
26976 " Expected non-type template argument pack <char...>",
26977 decl);
26981 /* Register member declarations. */
26982 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26983 finish_member_declaration (decl);
26984 /* If DECL is a function template, we must return to parse it later.
26985 (Even though there is no definition, there might be default
26986 arguments that need handling.) */
26987 if (member_p && decl
26988 && DECL_DECLARES_FUNCTION_P (decl))
26989 vec_safe_push (unparsed_funs_with_definitions, decl);
26992 /* Parse a template introduction header for a template-declaration. Returns
26993 false if tentative parse fails. */
26995 static bool
26996 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26998 cp_parser_parse_tentatively (parser);
27000 tree saved_scope = parser->scope;
27001 tree saved_object_scope = parser->object_scope;
27002 tree saved_qualifying_scope = parser->qualifying_scope;
27004 /* Look for the optional `::' operator. */
27005 cp_parser_global_scope_opt (parser,
27006 /*current_scope_valid_p=*/false);
27007 /* Look for the nested-name-specifier. */
27008 cp_parser_nested_name_specifier_opt (parser,
27009 /*typename_keyword_p=*/false,
27010 /*check_dependency_p=*/true,
27011 /*type_p=*/false,
27012 /*is_declaration=*/false);
27014 cp_token *token = cp_lexer_peek_token (parser->lexer);
27015 tree concept_name = cp_parser_identifier (parser);
27017 /* Look up the concept for which we will be matching
27018 template parameters. */
27019 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27020 token->location);
27021 parser->scope = saved_scope;
27022 parser->object_scope = saved_object_scope;
27023 parser->qualifying_scope = saved_qualifying_scope;
27025 if (concept_name == error_mark_node)
27026 cp_parser_simulate_error (parser);
27028 /* Look for opening brace for introduction. */
27029 matching_braces braces;
27030 braces.require_open (parser);
27032 if (!cp_parser_parse_definitely (parser))
27033 return false;
27035 push_deferring_access_checks (dk_deferred);
27037 /* Build vector of placeholder parameters and grab
27038 matching identifiers. */
27039 tree introduction_list = cp_parser_introduction_list (parser);
27041 /* The introduction-list shall not be empty. */
27042 int nargs = TREE_VEC_LENGTH (introduction_list);
27043 if (nargs == 0)
27045 error ("empty introduction-list");
27046 return true;
27049 /* Look for closing brace for introduction. */
27050 if (!braces.require_close (parser))
27051 return true;
27053 if (tmpl_decl == error_mark_node)
27055 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27056 token->location);
27057 return true;
27060 /* Build and associate the constraint. */
27061 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27062 if (parms && parms != error_mark_node)
27064 cp_parser_template_declaration_after_parameters (parser, parms,
27065 member_p);
27066 return true;
27069 error_at (token->location, "no matching concept for template-introduction");
27070 return true;
27073 /* Parse a normal template-declaration following the template keyword. */
27075 static void
27076 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27078 tree parameter_list;
27079 bool need_lang_pop;
27080 location_t location = input_location;
27082 /* Look for the `<' token. */
27083 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27084 return;
27085 if (at_class_scope_p () && current_function_decl)
27087 /* 14.5.2.2 [temp.mem]
27089 A local class shall not have member templates. */
27090 error_at (location,
27091 "invalid declaration of member template in local class");
27092 cp_parser_skip_to_end_of_block_or_statement (parser);
27093 return;
27095 /* [temp]
27097 A template ... shall not have C linkage. */
27098 if (current_lang_name == lang_name_c)
27100 error_at (location, "template with C linkage");
27101 maybe_show_extern_c_location ();
27102 /* Give it C++ linkage to avoid confusing other parts of the
27103 front end. */
27104 push_lang_context (lang_name_cplusplus);
27105 need_lang_pop = true;
27107 else
27108 need_lang_pop = false;
27110 /* We cannot perform access checks on the template parameter
27111 declarations until we know what is being declared, just as we
27112 cannot check the decl-specifier list. */
27113 push_deferring_access_checks (dk_deferred);
27115 /* If the next token is `>', then we have an invalid
27116 specialization. Rather than complain about an invalid template
27117 parameter, issue an error message here. */
27118 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27120 cp_parser_error (parser, "invalid explicit specialization");
27121 begin_specialization ();
27122 parameter_list = NULL_TREE;
27124 else
27126 /* Parse the template parameters. */
27127 parameter_list = cp_parser_template_parameter_list (parser);
27130 /* Look for the `>'. */
27131 cp_parser_skip_to_end_of_template_parameter_list (parser);
27133 /* Manage template requirements */
27134 if (flag_concepts)
27136 tree reqs = get_shorthand_constraints (current_template_parms);
27137 if (tree r = cp_parser_requires_clause_opt (parser))
27138 reqs = conjoin_constraints (reqs, normalize_expression (r));
27139 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27142 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27143 member_p);
27145 /* For the erroneous case of a template with C linkage, we pushed an
27146 implicit C++ linkage scope; exit that scope now. */
27147 if (need_lang_pop)
27148 pop_lang_context ();
27151 /* Parse a template-declaration, assuming that the `export' (and
27152 `extern') keywords, if present, has already been scanned. MEMBER_P
27153 is as for cp_parser_template_declaration. */
27155 static bool
27156 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27158 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27160 cp_lexer_consume_token (parser->lexer);
27161 cp_parser_explicit_template_declaration (parser, member_p);
27162 return true;
27164 else if (flag_concepts)
27165 return cp_parser_template_introduction (parser, member_p);
27167 return false;
27170 /* Perform the deferred access checks from a template-parameter-list.
27171 CHECKS is a TREE_LIST of access checks, as returned by
27172 get_deferred_access_checks. */
27174 static void
27175 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27177 ++processing_template_parmlist;
27178 perform_access_checks (checks, tf_warning_or_error);
27179 --processing_template_parmlist;
27182 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27183 `function-definition' sequence that follows a template header.
27184 If MEMBER_P is true, this declaration appears in a class scope.
27186 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27187 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27189 static tree
27190 cp_parser_single_declaration (cp_parser* parser,
27191 vec<deferred_access_check, va_gc> *checks,
27192 bool member_p,
27193 bool explicit_specialization_p,
27194 bool* friend_p)
27196 int declares_class_or_enum;
27197 tree decl = NULL_TREE;
27198 cp_decl_specifier_seq decl_specifiers;
27199 bool function_definition_p = false;
27200 cp_token *decl_spec_token_start;
27202 /* This function is only used when processing a template
27203 declaration. */
27204 gcc_assert (innermost_scope_kind () == sk_template_parms
27205 || innermost_scope_kind () == sk_template_spec);
27207 /* Defer access checks until we know what is being declared. */
27208 push_deferring_access_checks (dk_deferred);
27210 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27211 alternative. */
27212 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27213 cp_parser_decl_specifier_seq (parser,
27214 CP_PARSER_FLAGS_OPTIONAL,
27215 &decl_specifiers,
27216 &declares_class_or_enum);
27217 if (friend_p)
27218 *friend_p = cp_parser_friend_p (&decl_specifiers);
27220 /* There are no template typedefs. */
27221 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27223 error_at (decl_spec_token_start->location,
27224 "template declaration of %<typedef%>");
27225 decl = error_mark_node;
27228 /* Gather up the access checks that occurred the
27229 decl-specifier-seq. */
27230 stop_deferring_access_checks ();
27232 /* Check for the declaration of a template class. */
27233 if (declares_class_or_enum)
27235 if (cp_parser_declares_only_class_p (parser)
27236 || (declares_class_or_enum & 2))
27238 // If this is a declaration, but not a definition, associate
27239 // any constraints with the type declaration. Constraints
27240 // are associated with definitions in cp_parser_class_specifier.
27241 if (declares_class_or_enum == 1)
27242 associate_classtype_constraints (decl_specifiers.type);
27244 decl = shadow_tag (&decl_specifiers);
27246 /* In this case:
27248 struct C {
27249 friend template <typename T> struct A<T>::B;
27252 A<T>::B will be represented by a TYPENAME_TYPE, and
27253 therefore not recognized by shadow_tag. */
27254 if (friend_p && *friend_p
27255 && !decl
27256 && decl_specifiers.type
27257 && TYPE_P (decl_specifiers.type))
27258 decl = decl_specifiers.type;
27260 if (decl && decl != error_mark_node)
27261 decl = TYPE_NAME (decl);
27262 else
27263 decl = error_mark_node;
27265 /* Perform access checks for template parameters. */
27266 cp_parser_perform_template_parameter_access_checks (checks);
27268 /* Give a helpful diagnostic for
27269 template <class T> struct A { } a;
27270 if we aren't already recovering from an error. */
27271 if (!cp_parser_declares_only_class_p (parser)
27272 && !seen_error ())
27274 error_at (cp_lexer_peek_token (parser->lexer)->location,
27275 "a class template declaration must not declare "
27276 "anything else");
27277 cp_parser_skip_to_end_of_block_or_statement (parser);
27278 goto out;
27283 /* Complain about missing 'typename' or other invalid type names. */
27284 if (!decl_specifiers.any_type_specifiers_p
27285 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27287 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27288 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27289 the rest of this declaration. */
27290 decl = error_mark_node;
27291 goto out;
27294 /* If it's not a template class, try for a template function. If
27295 the next token is a `;', then this declaration does not declare
27296 anything. But, if there were errors in the decl-specifiers, then
27297 the error might well have come from an attempted class-specifier.
27298 In that case, there's no need to warn about a missing declarator. */
27299 if (!decl
27300 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27301 || decl_specifiers.type != error_mark_node))
27303 decl = cp_parser_init_declarator (parser,
27304 &decl_specifiers,
27305 checks,
27306 /*function_definition_allowed_p=*/true,
27307 member_p,
27308 declares_class_or_enum,
27309 &function_definition_p,
27310 NULL, NULL, NULL);
27312 /* 7.1.1-1 [dcl.stc]
27314 A storage-class-specifier shall not be specified in an explicit
27315 specialization... */
27316 if (decl
27317 && explicit_specialization_p
27318 && decl_specifiers.storage_class != sc_none)
27320 error_at (decl_spec_token_start->location,
27321 "explicit template specialization cannot have a storage class");
27322 decl = error_mark_node;
27325 if (decl && VAR_P (decl))
27326 check_template_variable (decl);
27329 /* Look for a trailing `;' after the declaration. */
27330 if (!function_definition_p
27331 && (decl == error_mark_node
27332 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27333 cp_parser_skip_to_end_of_block_or_statement (parser);
27335 out:
27336 pop_deferring_access_checks ();
27338 /* Clear any current qualification; whatever comes next is the start
27339 of something new. */
27340 parser->scope = NULL_TREE;
27341 parser->qualifying_scope = NULL_TREE;
27342 parser->object_scope = NULL_TREE;
27344 return decl;
27347 /* Parse a cast-expression that is not the operand of a unary "&". */
27349 static cp_expr
27350 cp_parser_simple_cast_expression (cp_parser *parser)
27352 return cp_parser_cast_expression (parser, /*address_p=*/false,
27353 /*cast_p=*/false, /*decltype*/false, NULL);
27356 /* Parse a functional cast to TYPE. Returns an expression
27357 representing the cast. */
27359 static cp_expr
27360 cp_parser_functional_cast (cp_parser* parser, tree type)
27362 vec<tree, va_gc> *vec;
27363 tree expression_list;
27364 cp_expr cast;
27365 bool nonconst_p;
27367 location_t start_loc = input_location;
27369 if (!type)
27370 type = error_mark_node;
27372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27374 cp_lexer_set_source_position (parser->lexer);
27375 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27376 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27377 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27378 if (TREE_CODE (type) == TYPE_DECL)
27379 type = TREE_TYPE (type);
27381 cast = finish_compound_literal (type, expression_list,
27382 tf_warning_or_error, fcl_functional);
27383 /* Create a location of the form:
27384 type_name{i, f}
27385 ^~~~~~~~~~~~~~~
27386 with caret == start at the start of the type name,
27387 finishing at the closing brace. */
27388 location_t finish_loc
27389 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27390 location_t combined_loc = make_location (start_loc, start_loc,
27391 finish_loc);
27392 cast.set_location (combined_loc);
27393 return cast;
27397 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27398 /*cast_p=*/true,
27399 /*allow_expansion_p=*/true,
27400 /*non_constant_p=*/NULL);
27401 if (vec == NULL)
27402 expression_list = error_mark_node;
27403 else
27405 expression_list = build_tree_list_vec (vec);
27406 release_tree_vector (vec);
27409 cast = build_functional_cast (type, expression_list,
27410 tf_warning_or_error);
27411 /* [expr.const]/1: In an integral constant expression "only type
27412 conversions to integral or enumeration type can be used". */
27413 if (TREE_CODE (type) == TYPE_DECL)
27414 type = TREE_TYPE (type);
27415 if (cast != error_mark_node
27416 && !cast_valid_in_integral_constant_expression_p (type)
27417 && cp_parser_non_integral_constant_expression (parser,
27418 NIC_CONSTRUCTOR))
27419 return error_mark_node;
27421 /* Create a location of the form:
27422 float(i)
27423 ^~~~~~~~
27424 with caret == start at the start of the type name,
27425 finishing at the closing paren. */
27426 location_t finish_loc
27427 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27428 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27429 cast.set_location (combined_loc);
27430 return cast;
27433 /* Save the tokens that make up the body of a member function defined
27434 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27435 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27436 specifiers applied to the declaration. Returns the FUNCTION_DECL
27437 for the member function. */
27439 static tree
27440 cp_parser_save_member_function_body (cp_parser* parser,
27441 cp_decl_specifier_seq *decl_specifiers,
27442 cp_declarator *declarator,
27443 tree attributes)
27445 cp_token *first;
27446 cp_token *last;
27447 tree fn;
27448 bool function_try_block = false;
27450 /* Create the FUNCTION_DECL. */
27451 fn = grokmethod (decl_specifiers, declarator, attributes);
27452 cp_finalize_omp_declare_simd (parser, fn);
27453 cp_finalize_oacc_routine (parser, fn, true);
27454 /* If something went badly wrong, bail out now. */
27455 if (fn == error_mark_node)
27457 /* If there's a function-body, skip it. */
27458 if (cp_parser_token_starts_function_definition_p
27459 (cp_lexer_peek_token (parser->lexer)))
27460 cp_parser_skip_to_end_of_block_or_statement (parser);
27461 return error_mark_node;
27464 /* Remember it, if there default args to post process. */
27465 cp_parser_save_default_args (parser, fn);
27467 /* Save away the tokens that make up the body of the
27468 function. */
27469 first = parser->lexer->next_token;
27471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27472 cp_lexer_consume_token (parser->lexer);
27473 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27474 RID_TRANSACTION_ATOMIC))
27476 cp_lexer_consume_token (parser->lexer);
27477 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27478 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27479 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27480 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27481 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27482 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27483 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27485 cp_lexer_consume_token (parser->lexer);
27486 cp_lexer_consume_token (parser->lexer);
27487 cp_lexer_consume_token (parser->lexer);
27488 cp_lexer_consume_token (parser->lexer);
27489 cp_lexer_consume_token (parser->lexer);
27491 else
27492 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27493 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27495 cp_lexer_consume_token (parser->lexer);
27496 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27497 break;
27501 /* Handle function try blocks. */
27502 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27504 cp_lexer_consume_token (parser->lexer);
27505 function_try_block = true;
27507 /* We can have braced-init-list mem-initializers before the fn body. */
27508 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27510 cp_lexer_consume_token (parser->lexer);
27511 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27513 /* cache_group will stop after an un-nested { } pair, too. */
27514 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27515 break;
27517 /* variadic mem-inits have ... after the ')'. */
27518 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27519 cp_lexer_consume_token (parser->lexer);
27522 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27523 /* Handle function try blocks. */
27524 if (function_try_block)
27525 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27526 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27527 last = parser->lexer->next_token;
27529 /* Save away the inline definition; we will process it when the
27530 class is complete. */
27531 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27532 DECL_PENDING_INLINE_P (fn) = 1;
27534 /* We need to know that this was defined in the class, so that
27535 friend templates are handled correctly. */
27536 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27538 /* Add FN to the queue of functions to be parsed later. */
27539 vec_safe_push (unparsed_funs_with_definitions, fn);
27541 return fn;
27544 /* Save the tokens that make up the in-class initializer for a non-static
27545 data member. Returns a DEFAULT_ARG. */
27547 static tree
27548 cp_parser_save_nsdmi (cp_parser* parser)
27550 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27553 /* Parse a template-argument-list, as well as the trailing ">" (but
27554 not the opening "<"). See cp_parser_template_argument_list for the
27555 return value. */
27557 static tree
27558 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27560 tree arguments;
27561 tree saved_scope;
27562 tree saved_qualifying_scope;
27563 tree saved_object_scope;
27564 bool saved_greater_than_is_operator_p;
27565 int saved_unevaluated_operand;
27566 int saved_inhibit_evaluation_warnings;
27568 /* [temp.names]
27570 When parsing a template-id, the first non-nested `>' is taken as
27571 the end of the template-argument-list rather than a greater-than
27572 operator. */
27573 saved_greater_than_is_operator_p
27574 = parser->greater_than_is_operator_p;
27575 parser->greater_than_is_operator_p = false;
27576 /* Parsing the argument list may modify SCOPE, so we save it
27577 here. */
27578 saved_scope = parser->scope;
27579 saved_qualifying_scope = parser->qualifying_scope;
27580 saved_object_scope = parser->object_scope;
27581 /* We need to evaluate the template arguments, even though this
27582 template-id may be nested within a "sizeof". */
27583 saved_unevaluated_operand = cp_unevaluated_operand;
27584 cp_unevaluated_operand = 0;
27585 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27586 c_inhibit_evaluation_warnings = 0;
27587 /* Parse the template-argument-list itself. */
27588 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27589 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27590 arguments = NULL_TREE;
27591 else
27592 arguments = cp_parser_template_argument_list (parser);
27593 /* Look for the `>' that ends the template-argument-list. If we find
27594 a '>>' instead, it's probably just a typo. */
27595 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27597 if (cxx_dialect != cxx98)
27599 /* In C++0x, a `>>' in a template argument list or cast
27600 expression is considered to be two separate `>'
27601 tokens. So, change the current token to a `>', but don't
27602 consume it: it will be consumed later when the outer
27603 template argument list (or cast expression) is parsed.
27604 Note that this replacement of `>' for `>>' is necessary
27605 even if we are parsing tentatively: in the tentative
27606 case, after calling
27607 cp_parser_enclosed_template_argument_list we will always
27608 throw away all of the template arguments and the first
27609 closing `>', either because the template argument list
27610 was erroneous or because we are replacing those tokens
27611 with a CPP_TEMPLATE_ID token. The second `>' (which will
27612 not have been thrown away) is needed either to close an
27613 outer template argument list or to complete a new-style
27614 cast. */
27615 cp_token *token = cp_lexer_peek_token (parser->lexer);
27616 token->type = CPP_GREATER;
27618 else if (!saved_greater_than_is_operator_p)
27620 /* If we're in a nested template argument list, the '>>' has
27621 to be a typo for '> >'. We emit the error message, but we
27622 continue parsing and we push a '>' as next token, so that
27623 the argument list will be parsed correctly. Note that the
27624 global source location is still on the token before the
27625 '>>', so we need to say explicitly where we want it. */
27626 cp_token *token = cp_lexer_peek_token (parser->lexer);
27627 gcc_rich_location richloc (token->location);
27628 richloc.add_fixit_replace ("> >");
27629 error_at (&richloc, "%<>>%> should be %<> >%> "
27630 "within a nested template argument list");
27632 token->type = CPP_GREATER;
27634 else
27636 /* If this is not a nested template argument list, the '>>'
27637 is a typo for '>'. Emit an error message and continue.
27638 Same deal about the token location, but here we can get it
27639 right by consuming the '>>' before issuing the diagnostic. */
27640 cp_token *token = cp_lexer_consume_token (parser->lexer);
27641 error_at (token->location,
27642 "spurious %<>>%>, use %<>%> to terminate "
27643 "a template argument list");
27646 else
27647 cp_parser_skip_to_end_of_template_parameter_list (parser);
27648 /* The `>' token might be a greater-than operator again now. */
27649 parser->greater_than_is_operator_p
27650 = saved_greater_than_is_operator_p;
27651 /* Restore the SAVED_SCOPE. */
27652 parser->scope = saved_scope;
27653 parser->qualifying_scope = saved_qualifying_scope;
27654 parser->object_scope = saved_object_scope;
27655 cp_unevaluated_operand = saved_unevaluated_operand;
27656 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27658 return arguments;
27661 /* MEMBER_FUNCTION is a member function, or a friend. If default
27662 arguments, or the body of the function have not yet been parsed,
27663 parse them now. */
27665 static void
27666 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27668 timevar_push (TV_PARSE_INMETH);
27669 /* If this member is a template, get the underlying
27670 FUNCTION_DECL. */
27671 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27672 member_function = DECL_TEMPLATE_RESULT (member_function);
27674 /* There should not be any class definitions in progress at this
27675 point; the bodies of members are only parsed outside of all class
27676 definitions. */
27677 gcc_assert (parser->num_classes_being_defined == 0);
27678 /* While we're parsing the member functions we might encounter more
27679 classes. We want to handle them right away, but we don't want
27680 them getting mixed up with functions that are currently in the
27681 queue. */
27682 push_unparsed_function_queues (parser);
27684 /* Make sure that any template parameters are in scope. */
27685 maybe_begin_member_template_processing (member_function);
27687 /* If the body of the function has not yet been parsed, parse it
27688 now. */
27689 if (DECL_PENDING_INLINE_P (member_function))
27691 tree function_scope;
27692 cp_token_cache *tokens;
27694 /* The function is no longer pending; we are processing it. */
27695 tokens = DECL_PENDING_INLINE_INFO (member_function);
27696 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27697 DECL_PENDING_INLINE_P (member_function) = 0;
27699 /* If this is a local class, enter the scope of the containing
27700 function. */
27701 function_scope = current_function_decl;
27702 if (function_scope)
27703 push_function_context ();
27705 /* Push the body of the function onto the lexer stack. */
27706 cp_parser_push_lexer_for_tokens (parser, tokens);
27708 /* Let the front end know that we going to be defining this
27709 function. */
27710 start_preparsed_function (member_function, NULL_TREE,
27711 SF_PRE_PARSED | SF_INCLASS_INLINE);
27713 /* Don't do access checking if it is a templated function. */
27714 if (processing_template_decl)
27715 push_deferring_access_checks (dk_no_check);
27717 /* #pragma omp declare reduction needs special parsing. */
27718 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27720 parser->lexer->in_pragma = true;
27721 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27722 finish_function (/*inline_p=*/true);
27723 cp_check_omp_declare_reduction (member_function);
27725 else
27726 /* Now, parse the body of the function. */
27727 cp_parser_function_definition_after_declarator (parser,
27728 /*inline_p=*/true);
27730 if (processing_template_decl)
27731 pop_deferring_access_checks ();
27733 /* Leave the scope of the containing function. */
27734 if (function_scope)
27735 pop_function_context ();
27736 cp_parser_pop_lexer (parser);
27739 /* Remove any template parameters from the symbol table. */
27740 maybe_end_member_template_processing ();
27742 /* Restore the queue. */
27743 pop_unparsed_function_queues (parser);
27744 timevar_pop (TV_PARSE_INMETH);
27747 /* If DECL contains any default args, remember it on the unparsed
27748 functions queue. */
27750 static void
27751 cp_parser_save_default_args (cp_parser* parser, tree decl)
27753 tree probe;
27755 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27756 probe;
27757 probe = TREE_CHAIN (probe))
27758 if (TREE_PURPOSE (probe))
27760 cp_default_arg_entry entry = {current_class_type, decl};
27761 vec_safe_push (unparsed_funs_with_default_args, entry);
27762 break;
27766 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27767 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27768 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27769 from the parameter-type-list. */
27771 static tree
27772 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27773 tree default_arg, tree parmtype)
27775 cp_token_cache *tokens;
27776 tree parsed_arg;
27777 bool dummy;
27779 if (default_arg == error_mark_node)
27780 return error_mark_node;
27782 /* Push the saved tokens for the default argument onto the parser's
27783 lexer stack. */
27784 tokens = DEFARG_TOKENS (default_arg);
27785 cp_parser_push_lexer_for_tokens (parser, tokens);
27787 start_lambda_scope (decl);
27789 /* Parse the default argument. */
27790 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27791 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27792 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27794 finish_lambda_scope ();
27796 if (parsed_arg == error_mark_node)
27797 cp_parser_skip_to_end_of_statement (parser);
27799 if (!processing_template_decl)
27801 /* In a non-template class, check conversions now. In a template,
27802 we'll wait and instantiate these as needed. */
27803 if (TREE_CODE (decl) == PARM_DECL)
27804 parsed_arg = check_default_argument (parmtype, parsed_arg,
27805 tf_warning_or_error);
27806 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27807 parsed_arg = error_mark_node;
27808 else
27809 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27812 /* If the token stream has not been completely used up, then
27813 there was extra junk after the end of the default
27814 argument. */
27815 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27817 if (TREE_CODE (decl) == PARM_DECL)
27818 cp_parser_error (parser, "expected %<,%>");
27819 else
27820 cp_parser_error (parser, "expected %<;%>");
27823 /* Revert to the main lexer. */
27824 cp_parser_pop_lexer (parser);
27826 return parsed_arg;
27829 /* FIELD is a non-static data member with an initializer which we saved for
27830 later; parse it now. */
27832 static void
27833 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27835 tree def;
27837 maybe_begin_member_template_processing (field);
27839 push_unparsed_function_queues (parser);
27840 def = cp_parser_late_parse_one_default_arg (parser, field,
27841 DECL_INITIAL (field),
27842 NULL_TREE);
27843 pop_unparsed_function_queues (parser);
27845 maybe_end_member_template_processing ();
27847 DECL_INITIAL (field) = def;
27850 /* FN is a FUNCTION_DECL which may contains a parameter with an
27851 unparsed DEFAULT_ARG. Parse the default args now. This function
27852 assumes that the current scope is the scope in which the default
27853 argument should be processed. */
27855 static void
27856 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27858 bool saved_local_variables_forbidden_p;
27859 tree parm, parmdecl;
27861 /* While we're parsing the default args, we might (due to the
27862 statement expression extension) encounter more classes. We want
27863 to handle them right away, but we don't want them getting mixed
27864 up with default args that are currently in the queue. */
27865 push_unparsed_function_queues (parser);
27867 /* Local variable names (and the `this' keyword) may not appear
27868 in a default argument. */
27869 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27870 parser->local_variables_forbidden_p = true;
27872 push_defarg_context (fn);
27874 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27875 parmdecl = DECL_ARGUMENTS (fn);
27876 parm && parm != void_list_node;
27877 parm = TREE_CHAIN (parm),
27878 parmdecl = DECL_CHAIN (parmdecl))
27880 tree default_arg = TREE_PURPOSE (parm);
27881 tree parsed_arg;
27882 vec<tree, va_gc> *insts;
27883 tree copy;
27884 unsigned ix;
27886 if (!default_arg)
27887 continue;
27889 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27890 /* This can happen for a friend declaration for a function
27891 already declared with default arguments. */
27892 continue;
27894 parsed_arg
27895 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27896 default_arg,
27897 TREE_VALUE (parm));
27898 TREE_PURPOSE (parm) = parsed_arg;
27900 /* Update any instantiations we've already created. */
27901 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27902 vec_safe_iterate (insts, ix, &copy); ix++)
27903 TREE_PURPOSE (copy) = parsed_arg;
27906 pop_defarg_context ();
27908 /* Make sure no default arg is missing. */
27909 check_default_args (fn);
27911 /* Restore the state of local_variables_forbidden_p. */
27912 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27914 /* Restore the queue. */
27915 pop_unparsed_function_queues (parser);
27918 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27920 sizeof ... ( identifier )
27922 where the 'sizeof' token has already been consumed. */
27924 static tree
27925 cp_parser_sizeof_pack (cp_parser *parser)
27927 /* Consume the `...'. */
27928 cp_lexer_consume_token (parser->lexer);
27929 maybe_warn_variadic_templates ();
27931 matching_parens parens;
27932 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27933 if (paren)
27934 parens.consume_open (parser);
27935 else
27936 permerror (cp_lexer_peek_token (parser->lexer)->location,
27937 "%<sizeof...%> argument must be surrounded by parentheses");
27939 cp_token *token = cp_lexer_peek_token (parser->lexer);
27940 tree name = cp_parser_identifier (parser);
27941 if (name == error_mark_node)
27942 return error_mark_node;
27943 /* The name is not qualified. */
27944 parser->scope = NULL_TREE;
27945 parser->qualifying_scope = NULL_TREE;
27946 parser->object_scope = NULL_TREE;
27947 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27948 if (expr == error_mark_node)
27949 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27950 token->location);
27951 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27952 expr = TREE_TYPE (expr);
27953 else if (TREE_CODE (expr) == CONST_DECL)
27954 expr = DECL_INITIAL (expr);
27955 expr = make_pack_expansion (expr);
27956 PACK_EXPANSION_SIZEOF_P (expr) = true;
27958 if (paren)
27959 parens.require_close (parser);
27961 return expr;
27964 /* Parse the operand of `sizeof' (or a similar operator). Returns
27965 either a TYPE or an expression, depending on the form of the
27966 input. The KEYWORD indicates which kind of expression we have
27967 encountered. */
27969 static tree
27970 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27972 tree expr = NULL_TREE;
27973 const char *saved_message;
27974 char *tmp;
27975 bool saved_integral_constant_expression_p;
27976 bool saved_non_integral_constant_expression_p;
27978 /* If it's a `...', then we are computing the length of a parameter
27979 pack. */
27980 if (keyword == RID_SIZEOF
27981 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27982 return cp_parser_sizeof_pack (parser);
27984 /* Types cannot be defined in a `sizeof' expression. Save away the
27985 old message. */
27986 saved_message = parser->type_definition_forbidden_message;
27987 /* And create the new one. */
27988 tmp = concat ("types may not be defined in %<",
27989 IDENTIFIER_POINTER (ridpointers[keyword]),
27990 "%> expressions", NULL);
27991 parser->type_definition_forbidden_message = tmp;
27993 /* The restrictions on constant-expressions do not apply inside
27994 sizeof expressions. */
27995 saved_integral_constant_expression_p
27996 = parser->integral_constant_expression_p;
27997 saved_non_integral_constant_expression_p
27998 = parser->non_integral_constant_expression_p;
27999 parser->integral_constant_expression_p = false;
28001 /* Do not actually evaluate the expression. */
28002 ++cp_unevaluated_operand;
28003 ++c_inhibit_evaluation_warnings;
28004 /* If it's a `(', then we might be looking at the type-id
28005 construction. */
28006 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28008 tree type = NULL_TREE;
28010 /* We can't be sure yet whether we're looking at a type-id or an
28011 expression. */
28012 cp_parser_parse_tentatively (parser);
28014 matching_parens parens;
28015 parens.consume_open (parser);
28017 /* Note: as a GNU Extension, compound literals are considered
28018 postfix-expressions as they are in C99, so they are valid
28019 arguments to sizeof. See comment in cp_parser_cast_expression
28020 for details. */
28021 if (cp_parser_compound_literal_p (parser))
28022 cp_parser_simulate_error (parser);
28023 else
28025 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28026 parser->in_type_id_in_expr_p = true;
28027 /* Look for the type-id. */
28028 type = cp_parser_type_id (parser);
28029 /* Look for the closing `)'. */
28030 parens.require_close (parser);
28031 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28034 /* If all went well, then we're done. */
28035 if (cp_parser_parse_definitely (parser))
28037 cp_decl_specifier_seq decl_specs;
28039 /* Build a trivial decl-specifier-seq. */
28040 clear_decl_specs (&decl_specs);
28041 decl_specs.type = type;
28043 /* Call grokdeclarator to figure out what type this is. */
28044 expr = grokdeclarator (NULL,
28045 &decl_specs,
28046 TYPENAME,
28047 /*initialized=*/0,
28048 /*attrlist=*/NULL);
28052 /* If the type-id production did not work out, then we must be
28053 looking at the unary-expression production. */
28054 if (!expr)
28055 expr = cp_parser_unary_expression (parser);
28057 /* Go back to evaluating expressions. */
28058 --cp_unevaluated_operand;
28059 --c_inhibit_evaluation_warnings;
28061 /* Free the message we created. */
28062 free (tmp);
28063 /* And restore the old one. */
28064 parser->type_definition_forbidden_message = saved_message;
28065 parser->integral_constant_expression_p
28066 = saved_integral_constant_expression_p;
28067 parser->non_integral_constant_expression_p
28068 = saved_non_integral_constant_expression_p;
28070 return expr;
28073 /* If the current declaration has no declarator, return true. */
28075 static bool
28076 cp_parser_declares_only_class_p (cp_parser *parser)
28078 /* If the next token is a `;' or a `,' then there is no
28079 declarator. */
28080 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28081 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28084 /* Update the DECL_SPECS to reflect the storage class indicated by
28085 KEYWORD. */
28087 static void
28088 cp_parser_set_storage_class (cp_parser *parser,
28089 cp_decl_specifier_seq *decl_specs,
28090 enum rid keyword,
28091 cp_token *token)
28093 cp_storage_class storage_class;
28095 if (parser->in_unbraced_linkage_specification_p)
28097 error_at (token->location, "invalid use of %qD in linkage specification",
28098 ridpointers[keyword]);
28099 return;
28101 else if (decl_specs->storage_class != sc_none)
28103 decl_specs->conflicting_specifiers_p = true;
28104 return;
28107 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28108 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28109 && decl_specs->gnu_thread_keyword_p)
28111 pedwarn (decl_specs->locations[ds_thread], 0,
28112 "%<__thread%> before %qD", ridpointers[keyword]);
28115 switch (keyword)
28117 case RID_AUTO:
28118 storage_class = sc_auto;
28119 break;
28120 case RID_REGISTER:
28121 storage_class = sc_register;
28122 break;
28123 case RID_STATIC:
28124 storage_class = sc_static;
28125 break;
28126 case RID_EXTERN:
28127 storage_class = sc_extern;
28128 break;
28129 case RID_MUTABLE:
28130 storage_class = sc_mutable;
28131 break;
28132 default:
28133 gcc_unreachable ();
28135 decl_specs->storage_class = storage_class;
28136 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28138 /* A storage class specifier cannot be applied alongside a typedef
28139 specifier. If there is a typedef specifier present then set
28140 conflicting_specifiers_p which will trigger an error later
28141 on in grokdeclarator. */
28142 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28143 decl_specs->conflicting_specifiers_p = true;
28146 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28147 is true, the type is a class or enum definition. */
28149 static void
28150 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28151 tree type_spec,
28152 cp_token *token,
28153 bool type_definition_p)
28155 decl_specs->any_specifiers_p = true;
28157 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28158 (with, for example, in "typedef int wchar_t;") we remember that
28159 this is what happened. In system headers, we ignore these
28160 declarations so that G++ can work with system headers that are not
28161 C++-safe. */
28162 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28163 && !type_definition_p
28164 && (type_spec == boolean_type_node
28165 || type_spec == char16_type_node
28166 || type_spec == char32_type_node
28167 || type_spec == wchar_type_node)
28168 && (decl_specs->type
28169 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28170 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28171 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28172 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28174 decl_specs->redefined_builtin_type = type_spec;
28175 set_and_check_decl_spec_loc (decl_specs,
28176 ds_redefined_builtin_type_spec,
28177 token);
28178 if (!decl_specs->type)
28180 decl_specs->type = type_spec;
28181 decl_specs->type_definition_p = false;
28182 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28185 else if (decl_specs->type)
28186 decl_specs->multiple_types_p = true;
28187 else
28189 decl_specs->type = type_spec;
28190 decl_specs->type_definition_p = type_definition_p;
28191 decl_specs->redefined_builtin_type = NULL_TREE;
28192 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28196 /* True iff TOKEN is the GNU keyword __thread. */
28198 static bool
28199 token_is__thread (cp_token *token)
28201 gcc_assert (token->keyword == RID_THREAD);
28202 return id_equal (token->u.value, "__thread");
28205 /* Set the location for a declarator specifier and check if it is
28206 duplicated.
28208 DECL_SPECS is the sequence of declarator specifiers onto which to
28209 set the location.
28211 DS is the single declarator specifier to set which location is to
28212 be set onto the existing sequence of declarators.
28214 LOCATION is the location for the declarator specifier to
28215 consider. */
28217 static void
28218 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28219 cp_decl_spec ds, cp_token *token)
28221 gcc_assert (ds < ds_last);
28223 if (decl_specs == NULL)
28224 return;
28226 source_location location = token->location;
28228 if (decl_specs->locations[ds] == 0)
28230 decl_specs->locations[ds] = location;
28231 if (ds == ds_thread)
28232 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28234 else
28236 if (ds == ds_long)
28238 if (decl_specs->locations[ds_long_long] != 0)
28239 error_at (location,
28240 "%<long long long%> is too long for GCC");
28241 else
28243 decl_specs->locations[ds_long_long] = location;
28244 pedwarn_cxx98 (location,
28245 OPT_Wlong_long,
28246 "ISO C++ 1998 does not support %<long long%>");
28249 else if (ds == ds_thread)
28251 bool gnu = token_is__thread (token);
28252 if (gnu != decl_specs->gnu_thread_keyword_p)
28253 error_at (location,
28254 "both %<__thread%> and %<thread_local%> specified");
28255 else
28257 gcc_rich_location richloc (location);
28258 richloc.add_fixit_remove ();
28259 error_at (&richloc, "duplicate %qD", token->u.value);
28262 else
28264 static const char *const decl_spec_names[] = {
28265 "signed",
28266 "unsigned",
28267 "short",
28268 "long",
28269 "const",
28270 "volatile",
28271 "restrict",
28272 "inline",
28273 "virtual",
28274 "explicit",
28275 "friend",
28276 "typedef",
28277 "using",
28278 "constexpr",
28279 "__complex"
28281 gcc_rich_location richloc (location);
28282 richloc.add_fixit_remove ();
28283 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28288 /* Return true iff the declarator specifier DS is present in the
28289 sequence of declarator specifiers DECL_SPECS. */
28291 bool
28292 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28293 cp_decl_spec ds)
28295 gcc_assert (ds < ds_last);
28297 if (decl_specs == NULL)
28298 return false;
28300 return decl_specs->locations[ds] != 0;
28303 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28304 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28306 static bool
28307 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28309 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28312 /* Issue an error message indicating that TOKEN_DESC was expected.
28313 If KEYWORD is true, it indicated this function is called by
28314 cp_parser_require_keword and the required token can only be
28315 a indicated keyword.
28317 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28318 within any error as the location of an "opening" token matching
28319 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28320 RT_CLOSE_PAREN). */
28322 static void
28323 cp_parser_required_error (cp_parser *parser,
28324 required_token token_desc,
28325 bool keyword,
28326 location_t matching_location)
28328 if (cp_parser_simulate_error (parser))
28329 return;
28331 const char *gmsgid = NULL;
28332 switch (token_desc)
28334 case RT_NEW:
28335 gmsgid = G_("expected %<new%>");
28336 break;
28337 case RT_DELETE:
28338 gmsgid = G_("expected %<delete%>");
28339 break;
28340 case RT_RETURN:
28341 gmsgid = G_("expected %<return%>");
28342 break;
28343 case RT_WHILE:
28344 gmsgid = G_("expected %<while%>");
28345 break;
28346 case RT_EXTERN:
28347 gmsgid = G_("expected %<extern%>");
28348 break;
28349 case RT_STATIC_ASSERT:
28350 gmsgid = G_("expected %<static_assert%>");
28351 break;
28352 case RT_DECLTYPE:
28353 gmsgid = G_("expected %<decltype%>");
28354 break;
28355 case RT_OPERATOR:
28356 gmsgid = G_("expected %<operator%>");
28357 break;
28358 case RT_CLASS:
28359 gmsgid = G_("expected %<class%>");
28360 break;
28361 case RT_TEMPLATE:
28362 gmsgid = G_("expected %<template%>");
28363 break;
28364 case RT_NAMESPACE:
28365 gmsgid = G_("expected %<namespace%>");
28366 break;
28367 case RT_USING:
28368 gmsgid = G_("expected %<using%>");
28369 break;
28370 case RT_ASM:
28371 gmsgid = G_("expected %<asm%>");
28372 break;
28373 case RT_TRY:
28374 gmsgid = G_("expected %<try%>");
28375 break;
28376 case RT_CATCH:
28377 gmsgid = G_("expected %<catch%>");
28378 break;
28379 case RT_THROW:
28380 gmsgid = G_("expected %<throw%>");
28381 break;
28382 case RT_LABEL:
28383 gmsgid = G_("expected %<__label__%>");
28384 break;
28385 case RT_AT_TRY:
28386 gmsgid = G_("expected %<@try%>");
28387 break;
28388 case RT_AT_SYNCHRONIZED:
28389 gmsgid = G_("expected %<@synchronized%>");
28390 break;
28391 case RT_AT_THROW:
28392 gmsgid = G_("expected %<@throw%>");
28393 break;
28394 case RT_TRANSACTION_ATOMIC:
28395 gmsgid = G_("expected %<__transaction_atomic%>");
28396 break;
28397 case RT_TRANSACTION_RELAXED:
28398 gmsgid = G_("expected %<__transaction_relaxed%>");
28399 break;
28400 default:
28401 break;
28404 if (!gmsgid && !keyword)
28406 switch (token_desc)
28408 case RT_SEMICOLON:
28409 gmsgid = G_("expected %<;%>");
28410 break;
28411 case RT_OPEN_PAREN:
28412 gmsgid = G_("expected %<(%>");
28413 break;
28414 case RT_CLOSE_BRACE:
28415 gmsgid = G_("expected %<}%>");
28416 break;
28417 case RT_OPEN_BRACE:
28418 gmsgid = G_("expected %<{%>");
28419 break;
28420 case RT_CLOSE_SQUARE:
28421 gmsgid = G_("expected %<]%>");
28422 break;
28423 case RT_OPEN_SQUARE:
28424 gmsgid = G_("expected %<[%>");
28425 break;
28426 case RT_COMMA:
28427 gmsgid = G_("expected %<,%>");
28428 break;
28429 case RT_SCOPE:
28430 gmsgid = G_("expected %<::%>");
28431 break;
28432 case RT_LESS:
28433 gmsgid = G_("expected %<<%>");
28434 break;
28435 case RT_GREATER:
28436 gmsgid = G_("expected %<>%>");
28437 break;
28438 case RT_EQ:
28439 gmsgid = G_("expected %<=%>");
28440 break;
28441 case RT_ELLIPSIS:
28442 gmsgid = G_("expected %<...%>");
28443 break;
28444 case RT_MULT:
28445 gmsgid = G_("expected %<*%>");
28446 break;
28447 case RT_COMPL:
28448 gmsgid = G_("expected %<~%>");
28449 break;
28450 case RT_COLON:
28451 gmsgid = G_("expected %<:%>");
28452 break;
28453 case RT_COLON_SCOPE:
28454 gmsgid = G_("expected %<:%> or %<::%>");
28455 break;
28456 case RT_CLOSE_PAREN:
28457 gmsgid = G_("expected %<)%>");
28458 break;
28459 case RT_COMMA_CLOSE_PAREN:
28460 gmsgid = G_("expected %<,%> or %<)%>");
28461 break;
28462 case RT_PRAGMA_EOL:
28463 gmsgid = G_("expected end of line");
28464 break;
28465 case RT_NAME:
28466 gmsgid = G_("expected identifier");
28467 break;
28468 case RT_SELECT:
28469 gmsgid = G_("expected selection-statement");
28470 break;
28471 case RT_ITERATION:
28472 gmsgid = G_("expected iteration-statement");
28473 break;
28474 case RT_JUMP:
28475 gmsgid = G_("expected jump-statement");
28476 break;
28477 case RT_CLASS_KEY:
28478 gmsgid = G_("expected class-key");
28479 break;
28480 case RT_CLASS_TYPENAME_TEMPLATE:
28481 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28482 break;
28483 default:
28484 gcc_unreachable ();
28488 if (gmsgid)
28489 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28493 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28494 issue an error message indicating that TOKEN_DESC was expected.
28496 Returns the token consumed, if the token had the appropriate type.
28497 Otherwise, returns NULL.
28499 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28500 within any error as the location of an "opening" token matching
28501 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28502 RT_CLOSE_PAREN). */
28504 static cp_token *
28505 cp_parser_require (cp_parser* parser,
28506 enum cpp_ttype type,
28507 required_token token_desc,
28508 location_t matching_location)
28510 if (cp_lexer_next_token_is (parser->lexer, type))
28511 return cp_lexer_consume_token (parser->lexer);
28512 else
28514 /* Output the MESSAGE -- unless we're parsing tentatively. */
28515 if (!cp_parser_simulate_error (parser))
28516 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28517 matching_location);
28518 return NULL;
28522 /* An error message is produced if the next token is not '>'.
28523 All further tokens are skipped until the desired token is
28524 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28526 static void
28527 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28529 /* Current level of '< ... >'. */
28530 unsigned level = 0;
28531 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28532 unsigned nesting_depth = 0;
28534 /* Are we ready, yet? If not, issue error message. */
28535 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28536 return;
28538 /* Skip tokens until the desired token is found. */
28539 while (true)
28541 /* Peek at the next token. */
28542 switch (cp_lexer_peek_token (parser->lexer)->type)
28544 case CPP_LESS:
28545 if (!nesting_depth)
28546 ++level;
28547 break;
28549 case CPP_RSHIFT:
28550 if (cxx_dialect == cxx98)
28551 /* C++0x views the `>>' operator as two `>' tokens, but
28552 C++98 does not. */
28553 break;
28554 else if (!nesting_depth && level-- == 0)
28556 /* We've hit a `>>' where the first `>' closes the
28557 template argument list, and the second `>' is
28558 spurious. Just consume the `>>' and stop; we've
28559 already produced at least one error. */
28560 cp_lexer_consume_token (parser->lexer);
28561 return;
28563 /* Fall through for C++0x, so we handle the second `>' in
28564 the `>>'. */
28565 gcc_fallthrough ();
28567 case CPP_GREATER:
28568 if (!nesting_depth && level-- == 0)
28570 /* We've reached the token we want, consume it and stop. */
28571 cp_lexer_consume_token (parser->lexer);
28572 return;
28574 break;
28576 case CPP_OPEN_PAREN:
28577 case CPP_OPEN_SQUARE:
28578 ++nesting_depth;
28579 break;
28581 case CPP_CLOSE_PAREN:
28582 case CPP_CLOSE_SQUARE:
28583 if (nesting_depth-- == 0)
28584 return;
28585 break;
28587 case CPP_EOF:
28588 case CPP_PRAGMA_EOL:
28589 case CPP_SEMICOLON:
28590 case CPP_OPEN_BRACE:
28591 case CPP_CLOSE_BRACE:
28592 /* The '>' was probably forgotten, don't look further. */
28593 return;
28595 default:
28596 break;
28599 /* Consume this token. */
28600 cp_lexer_consume_token (parser->lexer);
28604 /* If the next token is the indicated keyword, consume it. Otherwise,
28605 issue an error message indicating that TOKEN_DESC was expected.
28607 Returns the token consumed, if the token had the appropriate type.
28608 Otherwise, returns NULL. */
28610 static cp_token *
28611 cp_parser_require_keyword (cp_parser* parser,
28612 enum rid keyword,
28613 required_token token_desc)
28615 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28617 if (token && token->keyword != keyword)
28619 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28620 UNKNOWN_LOCATION);
28621 return NULL;
28624 return token;
28627 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28628 function-definition. */
28630 static bool
28631 cp_parser_token_starts_function_definition_p (cp_token* token)
28633 return (/* An ordinary function-body begins with an `{'. */
28634 token->type == CPP_OPEN_BRACE
28635 /* A ctor-initializer begins with a `:'. */
28636 || token->type == CPP_COLON
28637 /* A function-try-block begins with `try'. */
28638 || token->keyword == RID_TRY
28639 /* A function-transaction-block begins with `__transaction_atomic'
28640 or `__transaction_relaxed'. */
28641 || token->keyword == RID_TRANSACTION_ATOMIC
28642 || token->keyword == RID_TRANSACTION_RELAXED
28643 /* The named return value extension begins with `return'. */
28644 || token->keyword == RID_RETURN);
28647 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28648 definition. */
28650 static bool
28651 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28653 cp_token *token;
28655 token = cp_lexer_peek_token (parser->lexer);
28656 return (token->type == CPP_OPEN_BRACE
28657 || (token->type == CPP_COLON
28658 && !parser->colon_doesnt_start_class_def_p));
28661 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28662 C++0x) ending a template-argument. */
28664 static bool
28665 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28667 cp_token *token;
28669 token = cp_lexer_peek_token (parser->lexer);
28670 return (token->type == CPP_COMMA
28671 || token->type == CPP_GREATER
28672 || token->type == CPP_ELLIPSIS
28673 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28676 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28677 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28679 static bool
28680 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28681 size_t n)
28683 cp_token *token;
28685 token = cp_lexer_peek_nth_token (parser->lexer, n);
28686 if (token->type == CPP_LESS)
28687 return true;
28688 /* Check for the sequence `<::' in the original code. It would be lexed as
28689 `[:', where `[' is a digraph, and there is no whitespace before
28690 `:'. */
28691 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28693 cp_token *token2;
28694 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28695 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28696 return true;
28698 return false;
28701 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28702 or none_type otherwise. */
28704 static enum tag_types
28705 cp_parser_token_is_class_key (cp_token* token)
28707 switch (token->keyword)
28709 case RID_CLASS:
28710 return class_type;
28711 case RID_STRUCT:
28712 return record_type;
28713 case RID_UNION:
28714 return union_type;
28716 default:
28717 return none_type;
28721 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28722 or none_type otherwise or if the token is null. */
28724 static enum tag_types
28725 cp_parser_token_is_type_parameter_key (cp_token* token)
28727 if (!token)
28728 return none_type;
28730 switch (token->keyword)
28732 case RID_CLASS:
28733 return class_type;
28734 case RID_TYPENAME:
28735 return typename_type;
28737 default:
28738 return none_type;
28742 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28744 static void
28745 cp_parser_check_class_key (enum tag_types class_key, tree type)
28747 if (type == error_mark_node)
28748 return;
28749 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28751 if (permerror (input_location, "%qs tag used in naming %q#T",
28752 class_key == union_type ? "union"
28753 : class_key == record_type ? "struct" : "class",
28754 type))
28755 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28756 "%q#T was previously declared here", type);
28760 /* Issue an error message if DECL is redeclared with different
28761 access than its original declaration [class.access.spec/3].
28762 This applies to nested classes, nested class templates and
28763 enumerations [class.mem/1]. */
28765 static void
28766 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28768 if (!decl
28769 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28770 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28771 return;
28773 if ((TREE_PRIVATE (decl)
28774 != (current_access_specifier == access_private_node))
28775 || (TREE_PROTECTED (decl)
28776 != (current_access_specifier == access_protected_node)))
28777 error_at (location, "%qD redeclared with different access", decl);
28780 /* Look for the `template' keyword, as a syntactic disambiguator.
28781 Return TRUE iff it is present, in which case it will be
28782 consumed. */
28784 static bool
28785 cp_parser_optional_template_keyword (cp_parser *parser)
28787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28789 /* In C++98 the `template' keyword can only be used within templates;
28790 outside templates the parser can always figure out what is a
28791 template and what is not. In C++11, per the resolution of DR 468,
28792 `template' is allowed in cases where it is not strictly necessary. */
28793 if (!processing_template_decl
28794 && pedantic && cxx_dialect == cxx98)
28796 cp_token *token = cp_lexer_peek_token (parser->lexer);
28797 pedwarn (token->location, OPT_Wpedantic,
28798 "in C++98 %<template%> (as a disambiguator) is only "
28799 "allowed within templates");
28800 /* If this part of the token stream is rescanned, the same
28801 error message would be generated. So, we purge the token
28802 from the stream. */
28803 cp_lexer_purge_token (parser->lexer);
28804 return false;
28806 else
28808 /* Consume the `template' keyword. */
28809 cp_lexer_consume_token (parser->lexer);
28810 return true;
28813 return false;
28816 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28817 set PARSER->SCOPE, and perform other related actions. */
28819 static void
28820 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28822 struct tree_check *check_value;
28824 /* Get the stored value. */
28825 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28826 /* Set the scope from the stored value. */
28827 parser->scope = saved_checks_value (check_value);
28828 parser->qualifying_scope = check_value->qualifying_scope;
28829 parser->object_scope = NULL_TREE;
28832 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28833 encounter the end of a block before what we were looking for. */
28835 static bool
28836 cp_parser_cache_group (cp_parser *parser,
28837 enum cpp_ttype end,
28838 unsigned depth)
28840 while (true)
28842 cp_token *token = cp_lexer_peek_token (parser->lexer);
28844 /* Abort a parenthesized expression if we encounter a semicolon. */
28845 if ((end == CPP_CLOSE_PAREN || depth == 0)
28846 && token->type == CPP_SEMICOLON)
28847 return true;
28848 /* If we've reached the end of the file, stop. */
28849 if (token->type == CPP_EOF
28850 || (end != CPP_PRAGMA_EOL
28851 && token->type == CPP_PRAGMA_EOL))
28852 return true;
28853 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28854 /* We've hit the end of an enclosing block, so there's been some
28855 kind of syntax error. */
28856 return true;
28858 /* Consume the token. */
28859 cp_lexer_consume_token (parser->lexer);
28860 /* See if it starts a new group. */
28861 if (token->type == CPP_OPEN_BRACE)
28863 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28864 /* In theory this should probably check end == '}', but
28865 cp_parser_save_member_function_body needs it to exit
28866 after either '}' or ')' when called with ')'. */
28867 if (depth == 0)
28868 return false;
28870 else if (token->type == CPP_OPEN_PAREN)
28872 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28873 if (depth == 0 && end == CPP_CLOSE_PAREN)
28874 return false;
28876 else if (token->type == CPP_PRAGMA)
28877 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28878 else if (token->type == end)
28879 return false;
28883 /* Like above, for caching a default argument or NSDMI. Both of these are
28884 terminated by a non-nested comma, but it can be unclear whether or not a
28885 comma is nested in a template argument list unless we do more parsing.
28886 In order to handle this ambiguity, when we encounter a ',' after a '<'
28887 we try to parse what follows as a parameter-declaration-list (in the
28888 case of a default argument) or a member-declarator (in the case of an
28889 NSDMI). If that succeeds, then we stop caching. */
28891 static tree
28892 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28894 unsigned depth = 0;
28895 int maybe_template_id = 0;
28896 cp_token *first_token;
28897 cp_token *token;
28898 tree default_argument;
28900 /* Add tokens until we have processed the entire default
28901 argument. We add the range [first_token, token). */
28902 first_token = cp_lexer_peek_token (parser->lexer);
28903 if (first_token->type == CPP_OPEN_BRACE)
28905 /* For list-initialization, this is straightforward. */
28906 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28907 token = cp_lexer_peek_token (parser->lexer);
28909 else while (true)
28911 bool done = false;
28913 /* Peek at the next token. */
28914 token = cp_lexer_peek_token (parser->lexer);
28915 /* What we do depends on what token we have. */
28916 switch (token->type)
28918 /* In valid code, a default argument must be
28919 immediately followed by a `,' `)', or `...'. */
28920 case CPP_COMMA:
28921 if (depth == 0 && maybe_template_id)
28923 /* If we've seen a '<', we might be in a
28924 template-argument-list. Until Core issue 325 is
28925 resolved, we don't know how this situation ought
28926 to be handled, so try to DTRT. We check whether
28927 what comes after the comma is a valid parameter
28928 declaration list. If it is, then the comma ends
28929 the default argument; otherwise the default
28930 argument continues. */
28931 bool error = false;
28932 cp_token *peek;
28934 /* Set ITALP so cp_parser_parameter_declaration_list
28935 doesn't decide to commit to this parse. */
28936 bool saved_italp = parser->in_template_argument_list_p;
28937 parser->in_template_argument_list_p = true;
28939 cp_parser_parse_tentatively (parser);
28941 if (nsdmi)
28943 /* Parse declarators until we reach a non-comma or
28944 somthing that cannot be an initializer.
28945 Just checking whether we're looking at a single
28946 declarator is insufficient. Consider:
28947 int var = tuple<T,U>::x;
28948 The template parameter 'U' looks exactly like a
28949 declarator. */
28952 int ctor_dtor_or_conv_p;
28953 cp_lexer_consume_token (parser->lexer);
28954 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28955 &ctor_dtor_or_conv_p,
28956 /*parenthesized_p=*/NULL,
28957 /*member_p=*/true,
28958 /*friend_p=*/false);
28959 peek = cp_lexer_peek_token (parser->lexer);
28960 if (cp_parser_error_occurred (parser))
28961 break;
28963 while (peek->type == CPP_COMMA);
28964 /* If we met an '=' or ';' then the original comma
28965 was the end of the NSDMI. Otherwise assume
28966 we're still in the NSDMI. */
28967 error = (peek->type != CPP_EQ
28968 && peek->type != CPP_SEMICOLON);
28970 else
28972 cp_lexer_consume_token (parser->lexer);
28973 begin_scope (sk_function_parms, NULL_TREE);
28974 cp_parser_parameter_declaration_list (parser, &error);
28975 pop_bindings_and_leave_scope ();
28977 if (!cp_parser_error_occurred (parser) && !error)
28978 done = true;
28979 cp_parser_abort_tentative_parse (parser);
28981 parser->in_template_argument_list_p = saved_italp;
28982 break;
28984 /* FALLTHRU */
28985 case CPP_CLOSE_PAREN:
28986 case CPP_ELLIPSIS:
28987 /* If we run into a non-nested `;', `}', or `]',
28988 then the code is invalid -- but the default
28989 argument is certainly over. */
28990 case CPP_SEMICOLON:
28991 case CPP_CLOSE_BRACE:
28992 case CPP_CLOSE_SQUARE:
28993 if (depth == 0
28994 /* Handle correctly int n = sizeof ... ( p ); */
28995 && token->type != CPP_ELLIPSIS)
28996 done = true;
28997 /* Update DEPTH, if necessary. */
28998 else if (token->type == CPP_CLOSE_PAREN
28999 || token->type == CPP_CLOSE_BRACE
29000 || token->type == CPP_CLOSE_SQUARE)
29001 --depth;
29002 break;
29004 case CPP_OPEN_PAREN:
29005 case CPP_OPEN_SQUARE:
29006 case CPP_OPEN_BRACE:
29007 ++depth;
29008 break;
29010 case CPP_LESS:
29011 if (depth == 0)
29012 /* This might be the comparison operator, or it might
29013 start a template argument list. */
29014 ++maybe_template_id;
29015 break;
29017 case CPP_RSHIFT:
29018 if (cxx_dialect == cxx98)
29019 break;
29020 /* Fall through for C++0x, which treats the `>>'
29021 operator like two `>' tokens in certain
29022 cases. */
29023 gcc_fallthrough ();
29025 case CPP_GREATER:
29026 if (depth == 0)
29028 /* This might be an operator, or it might close a
29029 template argument list. But if a previous '<'
29030 started a template argument list, this will have
29031 closed it, so we can't be in one anymore. */
29032 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29033 if (maybe_template_id < 0)
29034 maybe_template_id = 0;
29036 break;
29038 /* If we run out of tokens, issue an error message. */
29039 case CPP_EOF:
29040 case CPP_PRAGMA_EOL:
29041 error_at (token->location, "file ends in default argument");
29042 return error_mark_node;
29044 case CPP_NAME:
29045 case CPP_SCOPE:
29046 /* In these cases, we should look for template-ids.
29047 For example, if the default argument is
29048 `X<int, double>()', we need to do name lookup to
29049 figure out whether or not `X' is a template; if
29050 so, the `,' does not end the default argument.
29052 That is not yet done. */
29053 break;
29055 default:
29056 break;
29059 /* If we've reached the end, stop. */
29060 if (done)
29061 break;
29063 /* Add the token to the token block. */
29064 token = cp_lexer_consume_token (parser->lexer);
29067 /* Create a DEFAULT_ARG to represent the unparsed default
29068 argument. */
29069 default_argument = make_node (DEFAULT_ARG);
29070 DEFARG_TOKENS (default_argument)
29071 = cp_token_cache_new (first_token, token);
29072 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29074 return default_argument;
29077 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29079 location_t
29080 defarg_location (tree default_argument)
29082 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29083 location_t start = tokens->first->location;
29084 location_t end = tokens->last->location;
29085 return make_location (start, start, end);
29088 /* Begin parsing tentatively. We always save tokens while parsing
29089 tentatively so that if the tentative parsing fails we can restore the
29090 tokens. */
29092 static void
29093 cp_parser_parse_tentatively (cp_parser* parser)
29095 /* Enter a new parsing context. */
29096 parser->context = cp_parser_context_new (parser->context);
29097 /* Begin saving tokens. */
29098 cp_lexer_save_tokens (parser->lexer);
29099 /* In order to avoid repetitive access control error messages,
29100 access checks are queued up until we are no longer parsing
29101 tentatively. */
29102 push_deferring_access_checks (dk_deferred);
29105 /* Commit to the currently active tentative parse. */
29107 static void
29108 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29110 cp_parser_context *context;
29111 cp_lexer *lexer;
29113 /* Mark all of the levels as committed. */
29114 lexer = parser->lexer;
29115 for (context = parser->context; context->next; context = context->next)
29117 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29118 break;
29119 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29120 while (!cp_lexer_saving_tokens (lexer))
29121 lexer = lexer->next;
29122 cp_lexer_commit_tokens (lexer);
29126 /* Commit to the topmost currently active tentative parse.
29128 Note that this function shouldn't be called when there are
29129 irreversible side-effects while in a tentative state. For
29130 example, we shouldn't create a permanent entry in the symbol
29131 table, or issue an error message that might not apply if the
29132 tentative parse is aborted. */
29134 static void
29135 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29137 cp_parser_context *context = parser->context;
29138 cp_lexer *lexer = parser->lexer;
29140 if (context)
29142 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29143 return;
29144 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29146 while (!cp_lexer_saving_tokens (lexer))
29147 lexer = lexer->next;
29148 cp_lexer_commit_tokens (lexer);
29152 /* Abort the currently active tentative parse. All consumed tokens
29153 will be rolled back, and no diagnostics will be issued. */
29155 static void
29156 cp_parser_abort_tentative_parse (cp_parser* parser)
29158 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29159 || errorcount > 0);
29160 cp_parser_simulate_error (parser);
29161 /* Now, pretend that we want to see if the construct was
29162 successfully parsed. */
29163 cp_parser_parse_definitely (parser);
29166 /* Stop parsing tentatively. If a parse error has occurred, restore the
29167 token stream. Otherwise, commit to the tokens we have consumed.
29168 Returns true if no error occurred; false otherwise. */
29170 static bool
29171 cp_parser_parse_definitely (cp_parser* parser)
29173 bool error_occurred;
29174 cp_parser_context *context;
29176 /* Remember whether or not an error occurred, since we are about to
29177 destroy that information. */
29178 error_occurred = cp_parser_error_occurred (parser);
29179 /* Remove the topmost context from the stack. */
29180 context = parser->context;
29181 parser->context = context->next;
29182 /* If no parse errors occurred, commit to the tentative parse. */
29183 if (!error_occurred)
29185 /* Commit to the tokens read tentatively, unless that was
29186 already done. */
29187 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29188 cp_lexer_commit_tokens (parser->lexer);
29190 pop_to_parent_deferring_access_checks ();
29192 /* Otherwise, if errors occurred, roll back our state so that things
29193 are just as they were before we began the tentative parse. */
29194 else
29196 cp_lexer_rollback_tokens (parser->lexer);
29197 pop_deferring_access_checks ();
29199 /* Add the context to the front of the free list. */
29200 context->next = cp_parser_context_free_list;
29201 cp_parser_context_free_list = context;
29203 return !error_occurred;
29206 /* Returns true if we are parsing tentatively and are not committed to
29207 this tentative parse. */
29209 static bool
29210 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29212 return (cp_parser_parsing_tentatively (parser)
29213 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29216 /* Returns nonzero iff an error has occurred during the most recent
29217 tentative parse. */
29219 static bool
29220 cp_parser_error_occurred (cp_parser* parser)
29222 return (cp_parser_parsing_tentatively (parser)
29223 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29226 /* Returns nonzero if GNU extensions are allowed. */
29228 static bool
29229 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29231 return parser->allow_gnu_extensions_p;
29234 /* Objective-C++ Productions */
29237 /* Parse an Objective-C expression, which feeds into a primary-expression
29238 above.
29240 objc-expression:
29241 objc-message-expression
29242 objc-string-literal
29243 objc-encode-expression
29244 objc-protocol-expression
29245 objc-selector-expression
29247 Returns a tree representation of the expression. */
29249 static cp_expr
29250 cp_parser_objc_expression (cp_parser* parser)
29252 /* Try to figure out what kind of declaration is present. */
29253 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29255 switch (kwd->type)
29257 case CPP_OPEN_SQUARE:
29258 return cp_parser_objc_message_expression (parser);
29260 case CPP_OBJC_STRING:
29261 kwd = cp_lexer_consume_token (parser->lexer);
29262 return objc_build_string_object (kwd->u.value);
29264 case CPP_KEYWORD:
29265 switch (kwd->keyword)
29267 case RID_AT_ENCODE:
29268 return cp_parser_objc_encode_expression (parser);
29270 case RID_AT_PROTOCOL:
29271 return cp_parser_objc_protocol_expression (parser);
29273 case RID_AT_SELECTOR:
29274 return cp_parser_objc_selector_expression (parser);
29276 default:
29277 break;
29279 /* FALLTHRU */
29280 default:
29281 error_at (kwd->location,
29282 "misplaced %<@%D%> Objective-C++ construct",
29283 kwd->u.value);
29284 cp_parser_skip_to_end_of_block_or_statement (parser);
29287 return error_mark_node;
29290 /* Parse an Objective-C message expression.
29292 objc-message-expression:
29293 [ objc-message-receiver objc-message-args ]
29295 Returns a representation of an Objective-C message. */
29297 static tree
29298 cp_parser_objc_message_expression (cp_parser* parser)
29300 tree receiver, messageargs;
29302 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29303 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29304 receiver = cp_parser_objc_message_receiver (parser);
29305 messageargs = cp_parser_objc_message_args (parser);
29306 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29307 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29309 tree result = objc_build_message_expr (receiver, messageargs);
29311 /* Construct a location e.g.
29312 [self func1:5]
29313 ^~~~~~~~~~~~~~
29314 ranging from the '[' to the ']', with the caret at the start. */
29315 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29316 protected_set_expr_location (result, combined_loc);
29318 return result;
29321 /* Parse an objc-message-receiver.
29323 objc-message-receiver:
29324 expression
29325 simple-type-specifier
29327 Returns a representation of the type or expression. */
29329 static tree
29330 cp_parser_objc_message_receiver (cp_parser* parser)
29332 tree rcv;
29334 /* An Objective-C message receiver may be either (1) a type
29335 or (2) an expression. */
29336 cp_parser_parse_tentatively (parser);
29337 rcv = cp_parser_expression (parser);
29339 /* If that worked out, fine. */
29340 if (cp_parser_parse_definitely (parser))
29341 return rcv;
29343 cp_parser_parse_tentatively (parser);
29344 rcv = cp_parser_simple_type_specifier (parser,
29345 /*decl_specs=*/NULL,
29346 CP_PARSER_FLAGS_NONE);
29348 if (cp_parser_parse_definitely (parser))
29349 return objc_get_class_reference (rcv);
29351 cp_parser_error (parser, "objective-c++ message receiver expected");
29352 return error_mark_node;
29355 /* Parse the arguments and selectors comprising an Objective-C message.
29357 objc-message-args:
29358 objc-selector
29359 objc-selector-args
29360 objc-selector-args , objc-comma-args
29362 objc-selector-args:
29363 objc-selector [opt] : assignment-expression
29364 objc-selector-args objc-selector [opt] : assignment-expression
29366 objc-comma-args:
29367 assignment-expression
29368 objc-comma-args , assignment-expression
29370 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29371 selector arguments and TREE_VALUE containing a list of comma
29372 arguments. */
29374 static tree
29375 cp_parser_objc_message_args (cp_parser* parser)
29377 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29378 bool maybe_unary_selector_p = true;
29379 cp_token *token = cp_lexer_peek_token (parser->lexer);
29381 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29383 tree selector = NULL_TREE, arg;
29385 if (token->type != CPP_COLON)
29386 selector = cp_parser_objc_selector (parser);
29388 /* Detect if we have a unary selector. */
29389 if (maybe_unary_selector_p
29390 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29391 return build_tree_list (selector, NULL_TREE);
29393 maybe_unary_selector_p = false;
29394 cp_parser_require (parser, CPP_COLON, RT_COLON);
29395 arg = cp_parser_assignment_expression (parser);
29397 sel_args
29398 = chainon (sel_args,
29399 build_tree_list (selector, arg));
29401 token = cp_lexer_peek_token (parser->lexer);
29404 /* Handle non-selector arguments, if any. */
29405 while (token->type == CPP_COMMA)
29407 tree arg;
29409 cp_lexer_consume_token (parser->lexer);
29410 arg = cp_parser_assignment_expression (parser);
29412 addl_args
29413 = chainon (addl_args,
29414 build_tree_list (NULL_TREE, arg));
29416 token = cp_lexer_peek_token (parser->lexer);
29419 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29421 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29422 return build_tree_list (error_mark_node, error_mark_node);
29425 return build_tree_list (sel_args, addl_args);
29428 /* Parse an Objective-C encode expression.
29430 objc-encode-expression:
29431 @encode objc-typename
29433 Returns an encoded representation of the type argument. */
29435 static cp_expr
29436 cp_parser_objc_encode_expression (cp_parser* parser)
29438 tree type;
29439 cp_token *token;
29440 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29442 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29443 matching_parens parens;
29444 parens.require_open (parser);
29445 token = cp_lexer_peek_token (parser->lexer);
29446 type = complete_type (cp_parser_type_id (parser));
29447 parens.require_close (parser);
29449 if (!type)
29451 error_at (token->location,
29452 "%<@encode%> must specify a type as an argument");
29453 return error_mark_node;
29456 /* This happens if we find @encode(T) (where T is a template
29457 typename or something dependent on a template typename) when
29458 parsing a template. In that case, we can't compile it
29459 immediately, but we rather create an AT_ENCODE_EXPR which will
29460 need to be instantiated when the template is used.
29462 if (dependent_type_p (type))
29464 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29465 TREE_READONLY (value) = 1;
29466 return value;
29470 /* Build a location of the form:
29471 @encode(int)
29472 ^~~~~~~~~~~~
29473 with caret==start at the @ token, finishing at the close paren. */
29474 location_t combined_loc
29475 = make_location (start_loc, start_loc,
29476 cp_lexer_previous_token (parser->lexer)->location);
29478 return cp_expr (objc_build_encode_expr (type), combined_loc);
29481 /* Parse an Objective-C @defs expression. */
29483 static tree
29484 cp_parser_objc_defs_expression (cp_parser *parser)
29486 tree name;
29488 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29489 matching_parens parens;
29490 parens.require_open (parser);
29491 name = cp_parser_identifier (parser);
29492 parens.require_close (parser);
29494 return objc_get_class_ivars (name);
29497 /* Parse an Objective-C protocol expression.
29499 objc-protocol-expression:
29500 @protocol ( identifier )
29502 Returns a representation of the protocol expression. */
29504 static tree
29505 cp_parser_objc_protocol_expression (cp_parser* parser)
29507 tree proto;
29508 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29510 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29511 matching_parens parens;
29512 parens.require_open (parser);
29513 proto = cp_parser_identifier (parser);
29514 parens.require_close (parser);
29516 /* Build a location of the form:
29517 @protocol(prot)
29518 ^~~~~~~~~~~~~~~
29519 with caret==start at the @ token, finishing at the close paren. */
29520 location_t combined_loc
29521 = make_location (start_loc, start_loc,
29522 cp_lexer_previous_token (parser->lexer)->location);
29523 tree result = objc_build_protocol_expr (proto);
29524 protected_set_expr_location (result, combined_loc);
29525 return result;
29528 /* Parse an Objective-C selector expression.
29530 objc-selector-expression:
29531 @selector ( objc-method-signature )
29533 objc-method-signature:
29534 objc-selector
29535 objc-selector-seq
29537 objc-selector-seq:
29538 objc-selector :
29539 objc-selector-seq objc-selector :
29541 Returns a representation of the method selector. */
29543 static tree
29544 cp_parser_objc_selector_expression (cp_parser* parser)
29546 tree sel_seq = NULL_TREE;
29547 bool maybe_unary_selector_p = true;
29548 cp_token *token;
29549 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29551 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29552 matching_parens parens;
29553 parens.require_open (parser);
29554 token = cp_lexer_peek_token (parser->lexer);
29556 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29557 || token->type == CPP_SCOPE)
29559 tree selector = NULL_TREE;
29561 if (token->type != CPP_COLON
29562 || token->type == CPP_SCOPE)
29563 selector = cp_parser_objc_selector (parser);
29565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29566 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29568 /* Detect if we have a unary selector. */
29569 if (maybe_unary_selector_p)
29571 sel_seq = selector;
29572 goto finish_selector;
29574 else
29576 cp_parser_error (parser, "expected %<:%>");
29579 maybe_unary_selector_p = false;
29580 token = cp_lexer_consume_token (parser->lexer);
29582 if (token->type == CPP_SCOPE)
29584 sel_seq
29585 = chainon (sel_seq,
29586 build_tree_list (selector, NULL_TREE));
29587 sel_seq
29588 = chainon (sel_seq,
29589 build_tree_list (NULL_TREE, NULL_TREE));
29591 else
29592 sel_seq
29593 = chainon (sel_seq,
29594 build_tree_list (selector, NULL_TREE));
29596 token = cp_lexer_peek_token (parser->lexer);
29599 finish_selector:
29600 parens.require_close (parser);
29603 /* Build a location of the form:
29604 @selector(func)
29605 ^~~~~~~~~~~~~~~
29606 with caret==start at the @ token, finishing at the close paren. */
29607 location_t combined_loc
29608 = make_location (loc, loc,
29609 cp_lexer_previous_token (parser->lexer)->location);
29610 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29611 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29612 protected_set_expr_location (result, combined_loc);
29613 return result;
29616 /* Parse a list of identifiers.
29618 objc-identifier-list:
29619 identifier
29620 objc-identifier-list , identifier
29622 Returns a TREE_LIST of identifier nodes. */
29624 static tree
29625 cp_parser_objc_identifier_list (cp_parser* parser)
29627 tree identifier;
29628 tree list;
29629 cp_token *sep;
29631 identifier = cp_parser_identifier (parser);
29632 if (identifier == error_mark_node)
29633 return error_mark_node;
29635 list = build_tree_list (NULL_TREE, identifier);
29636 sep = cp_lexer_peek_token (parser->lexer);
29638 while (sep->type == CPP_COMMA)
29640 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29641 identifier = cp_parser_identifier (parser);
29642 if (identifier == error_mark_node)
29643 return list;
29645 list = chainon (list, build_tree_list (NULL_TREE,
29646 identifier));
29647 sep = cp_lexer_peek_token (parser->lexer);
29650 return list;
29653 /* Parse an Objective-C alias declaration.
29655 objc-alias-declaration:
29656 @compatibility_alias identifier identifier ;
29658 This function registers the alias mapping with the Objective-C front end.
29659 It returns nothing. */
29661 static void
29662 cp_parser_objc_alias_declaration (cp_parser* parser)
29664 tree alias, orig;
29666 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29667 alias = cp_parser_identifier (parser);
29668 orig = cp_parser_identifier (parser);
29669 objc_declare_alias (alias, orig);
29670 cp_parser_consume_semicolon_at_end_of_statement (parser);
29673 /* Parse an Objective-C class forward-declaration.
29675 objc-class-declaration:
29676 @class objc-identifier-list ;
29678 The function registers the forward declarations with the Objective-C
29679 front end. It returns nothing. */
29681 static void
29682 cp_parser_objc_class_declaration (cp_parser* parser)
29684 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29685 while (true)
29687 tree id;
29689 id = cp_parser_identifier (parser);
29690 if (id == error_mark_node)
29691 break;
29693 objc_declare_class (id);
29695 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29696 cp_lexer_consume_token (parser->lexer);
29697 else
29698 break;
29700 cp_parser_consume_semicolon_at_end_of_statement (parser);
29703 /* Parse a list of Objective-C protocol references.
29705 objc-protocol-refs-opt:
29706 objc-protocol-refs [opt]
29708 objc-protocol-refs:
29709 < objc-identifier-list >
29711 Returns a TREE_LIST of identifiers, if any. */
29713 static tree
29714 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29716 tree protorefs = NULL_TREE;
29718 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29720 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29721 protorefs = cp_parser_objc_identifier_list (parser);
29722 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29725 return protorefs;
29728 /* Parse a Objective-C visibility specification. */
29730 static void
29731 cp_parser_objc_visibility_spec (cp_parser* parser)
29733 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29735 switch (vis->keyword)
29737 case RID_AT_PRIVATE:
29738 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29739 break;
29740 case RID_AT_PROTECTED:
29741 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29742 break;
29743 case RID_AT_PUBLIC:
29744 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29745 break;
29746 case RID_AT_PACKAGE:
29747 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29748 break;
29749 default:
29750 return;
29753 /* Eat '@private'/'@protected'/'@public'. */
29754 cp_lexer_consume_token (parser->lexer);
29757 /* Parse an Objective-C method type. Return 'true' if it is a class
29758 (+) method, and 'false' if it is an instance (-) method. */
29760 static inline bool
29761 cp_parser_objc_method_type (cp_parser* parser)
29763 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29764 return true;
29765 else
29766 return false;
29769 /* Parse an Objective-C protocol qualifier. */
29771 static tree
29772 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29774 tree quals = NULL_TREE, node;
29775 cp_token *token = cp_lexer_peek_token (parser->lexer);
29777 node = token->u.value;
29779 while (node && identifier_p (node)
29780 && (node == ridpointers [(int) RID_IN]
29781 || node == ridpointers [(int) RID_OUT]
29782 || node == ridpointers [(int) RID_INOUT]
29783 || node == ridpointers [(int) RID_BYCOPY]
29784 || node == ridpointers [(int) RID_BYREF]
29785 || node == ridpointers [(int) RID_ONEWAY]))
29787 quals = tree_cons (NULL_TREE, node, quals);
29788 cp_lexer_consume_token (parser->lexer);
29789 token = cp_lexer_peek_token (parser->lexer);
29790 node = token->u.value;
29793 return quals;
29796 /* Parse an Objective-C typename. */
29798 static tree
29799 cp_parser_objc_typename (cp_parser* parser)
29801 tree type_name = NULL_TREE;
29803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29805 tree proto_quals, cp_type = NULL_TREE;
29807 matching_parens parens;
29808 parens.consume_open (parser); /* Eat '('. */
29809 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29811 /* An ObjC type name may consist of just protocol qualifiers, in which
29812 case the type shall default to 'id'. */
29813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29815 cp_type = cp_parser_type_id (parser);
29817 /* If the type could not be parsed, an error has already
29818 been produced. For error recovery, behave as if it had
29819 not been specified, which will use the default type
29820 'id'. */
29821 if (cp_type == error_mark_node)
29823 cp_type = NULL_TREE;
29824 /* We need to skip to the closing parenthesis as
29825 cp_parser_type_id() does not seem to do it for
29826 us. */
29827 cp_parser_skip_to_closing_parenthesis (parser,
29828 /*recovering=*/true,
29829 /*or_comma=*/false,
29830 /*consume_paren=*/false);
29834 parens.require_close (parser);
29835 type_name = build_tree_list (proto_quals, cp_type);
29838 return type_name;
29841 /* Check to see if TYPE refers to an Objective-C selector name. */
29843 static bool
29844 cp_parser_objc_selector_p (enum cpp_ttype type)
29846 return (type == CPP_NAME || type == CPP_KEYWORD
29847 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29848 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29849 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29850 || type == CPP_XOR || type == CPP_XOR_EQ);
29853 /* Parse an Objective-C selector. */
29855 static tree
29856 cp_parser_objc_selector (cp_parser* parser)
29858 cp_token *token = cp_lexer_consume_token (parser->lexer);
29860 if (!cp_parser_objc_selector_p (token->type))
29862 error_at (token->location, "invalid Objective-C++ selector name");
29863 return error_mark_node;
29866 /* C++ operator names are allowed to appear in ObjC selectors. */
29867 switch (token->type)
29869 case CPP_AND_AND: return get_identifier ("and");
29870 case CPP_AND_EQ: return get_identifier ("and_eq");
29871 case CPP_AND: return get_identifier ("bitand");
29872 case CPP_OR: return get_identifier ("bitor");
29873 case CPP_COMPL: return get_identifier ("compl");
29874 case CPP_NOT: return get_identifier ("not");
29875 case CPP_NOT_EQ: return get_identifier ("not_eq");
29876 case CPP_OR_OR: return get_identifier ("or");
29877 case CPP_OR_EQ: return get_identifier ("or_eq");
29878 case CPP_XOR: return get_identifier ("xor");
29879 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29880 default: return token->u.value;
29884 /* Parse an Objective-C params list. */
29886 static tree
29887 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29889 tree params = NULL_TREE;
29890 bool maybe_unary_selector_p = true;
29891 cp_token *token = cp_lexer_peek_token (parser->lexer);
29893 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29895 tree selector = NULL_TREE, type_name, identifier;
29896 tree parm_attr = NULL_TREE;
29898 if (token->keyword == RID_ATTRIBUTE)
29899 break;
29901 if (token->type != CPP_COLON)
29902 selector = cp_parser_objc_selector (parser);
29904 /* Detect if we have a unary selector. */
29905 if (maybe_unary_selector_p
29906 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29908 params = selector; /* Might be followed by attributes. */
29909 break;
29912 maybe_unary_selector_p = false;
29913 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29915 /* Something went quite wrong. There should be a colon
29916 here, but there is not. Stop parsing parameters. */
29917 break;
29919 type_name = cp_parser_objc_typename (parser);
29920 /* New ObjC allows attributes on parameters too. */
29921 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29922 parm_attr = cp_parser_attributes_opt (parser);
29923 identifier = cp_parser_identifier (parser);
29925 params
29926 = chainon (params,
29927 objc_build_keyword_decl (selector,
29928 type_name,
29929 identifier,
29930 parm_attr));
29932 token = cp_lexer_peek_token (parser->lexer);
29935 if (params == NULL_TREE)
29937 cp_parser_error (parser, "objective-c++ method declaration is expected");
29938 return error_mark_node;
29941 /* We allow tail attributes for the method. */
29942 if (token->keyword == RID_ATTRIBUTE)
29944 *attributes = cp_parser_attributes_opt (parser);
29945 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29946 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29947 return params;
29948 cp_parser_error (parser,
29949 "method attributes must be specified at the end");
29950 return error_mark_node;
29953 if (params == NULL_TREE)
29955 cp_parser_error (parser, "objective-c++ method declaration is expected");
29956 return error_mark_node;
29958 return params;
29961 /* Parse the non-keyword Objective-C params. */
29963 static tree
29964 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29965 tree* attributes)
29967 tree params = make_node (TREE_LIST);
29968 cp_token *token = cp_lexer_peek_token (parser->lexer);
29969 *ellipsisp = false; /* Initially, assume no ellipsis. */
29971 while (token->type == CPP_COMMA)
29973 cp_parameter_declarator *parmdecl;
29974 tree parm;
29976 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29977 token = cp_lexer_peek_token (parser->lexer);
29979 if (token->type == CPP_ELLIPSIS)
29981 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29982 *ellipsisp = true;
29983 token = cp_lexer_peek_token (parser->lexer);
29984 break;
29987 /* TODO: parse attributes for tail parameters. */
29988 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29989 parm = grokdeclarator (parmdecl->declarator,
29990 &parmdecl->decl_specifiers,
29991 PARM, /*initialized=*/0,
29992 /*attrlist=*/NULL);
29994 chainon (params, build_tree_list (NULL_TREE, parm));
29995 token = cp_lexer_peek_token (parser->lexer);
29998 /* We allow tail attributes for the method. */
29999 if (token->keyword == RID_ATTRIBUTE)
30001 if (*attributes == NULL_TREE)
30003 *attributes = cp_parser_attributes_opt (parser);
30004 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30005 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30006 return params;
30008 else
30009 /* We have an error, but parse the attributes, so that we can
30010 carry on. */
30011 *attributes = cp_parser_attributes_opt (parser);
30013 cp_parser_error (parser,
30014 "method attributes must be specified at the end");
30015 return error_mark_node;
30018 return params;
30021 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30023 static void
30024 cp_parser_objc_interstitial_code (cp_parser* parser)
30026 cp_token *token = cp_lexer_peek_token (parser->lexer);
30028 /* If the next token is `extern' and the following token is a string
30029 literal, then we have a linkage specification. */
30030 if (token->keyword == RID_EXTERN
30031 && cp_parser_is_pure_string_literal
30032 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30033 cp_parser_linkage_specification (parser);
30034 /* Handle #pragma, if any. */
30035 else if (token->type == CPP_PRAGMA)
30036 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30037 /* Allow stray semicolons. */
30038 else if (token->type == CPP_SEMICOLON)
30039 cp_lexer_consume_token (parser->lexer);
30040 /* Mark methods as optional or required, when building protocols. */
30041 else if (token->keyword == RID_AT_OPTIONAL)
30043 cp_lexer_consume_token (parser->lexer);
30044 objc_set_method_opt (true);
30046 else if (token->keyword == RID_AT_REQUIRED)
30048 cp_lexer_consume_token (parser->lexer);
30049 objc_set_method_opt (false);
30051 else if (token->keyword == RID_NAMESPACE)
30052 cp_parser_namespace_definition (parser);
30053 /* Other stray characters must generate errors. */
30054 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30056 cp_lexer_consume_token (parser->lexer);
30057 error ("stray %qs between Objective-C++ methods",
30058 token->type == CPP_OPEN_BRACE ? "{" : "}");
30060 /* Finally, try to parse a block-declaration, or a function-definition. */
30061 else
30062 cp_parser_block_declaration (parser, /*statement_p=*/false);
30065 /* Parse a method signature. */
30067 static tree
30068 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30070 tree rettype, kwdparms, optparms;
30071 bool ellipsis = false;
30072 bool is_class_method;
30074 is_class_method = cp_parser_objc_method_type (parser);
30075 rettype = cp_parser_objc_typename (parser);
30076 *attributes = NULL_TREE;
30077 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30078 if (kwdparms == error_mark_node)
30079 return error_mark_node;
30080 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30081 if (optparms == error_mark_node)
30082 return error_mark_node;
30084 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30087 static bool
30088 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30090 tree tattr;
30091 cp_lexer_save_tokens (parser->lexer);
30092 tattr = cp_parser_attributes_opt (parser);
30093 gcc_assert (tattr) ;
30095 /* If the attributes are followed by a method introducer, this is not allowed.
30096 Dump the attributes and flag the situation. */
30097 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30098 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30099 return true;
30101 /* Otherwise, the attributes introduce some interstitial code, possibly so
30102 rewind to allow that check. */
30103 cp_lexer_rollback_tokens (parser->lexer);
30104 return false;
30107 /* Parse an Objective-C method prototype list. */
30109 static void
30110 cp_parser_objc_method_prototype_list (cp_parser* parser)
30112 cp_token *token = cp_lexer_peek_token (parser->lexer);
30114 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30116 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30118 tree attributes, sig;
30119 bool is_class_method;
30120 if (token->type == CPP_PLUS)
30121 is_class_method = true;
30122 else
30123 is_class_method = false;
30124 sig = cp_parser_objc_method_signature (parser, &attributes);
30125 if (sig == error_mark_node)
30127 cp_parser_skip_to_end_of_block_or_statement (parser);
30128 token = cp_lexer_peek_token (parser->lexer);
30129 continue;
30131 objc_add_method_declaration (is_class_method, sig, attributes);
30132 cp_parser_consume_semicolon_at_end_of_statement (parser);
30134 else if (token->keyword == RID_AT_PROPERTY)
30135 cp_parser_objc_at_property_declaration (parser);
30136 else if (token->keyword == RID_ATTRIBUTE
30137 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30138 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30139 OPT_Wattributes,
30140 "prefix attributes are ignored for methods");
30141 else
30142 /* Allow for interspersed non-ObjC++ code. */
30143 cp_parser_objc_interstitial_code (parser);
30145 token = cp_lexer_peek_token (parser->lexer);
30148 if (token->type != CPP_EOF)
30149 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30150 else
30151 cp_parser_error (parser, "expected %<@end%>");
30153 objc_finish_interface ();
30156 /* Parse an Objective-C method definition list. */
30158 static void
30159 cp_parser_objc_method_definition_list (cp_parser* parser)
30161 cp_token *token = cp_lexer_peek_token (parser->lexer);
30163 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30165 tree meth;
30167 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30169 cp_token *ptk;
30170 tree sig, attribute;
30171 bool is_class_method;
30172 if (token->type == CPP_PLUS)
30173 is_class_method = true;
30174 else
30175 is_class_method = false;
30176 push_deferring_access_checks (dk_deferred);
30177 sig = cp_parser_objc_method_signature (parser, &attribute);
30178 if (sig == error_mark_node)
30180 cp_parser_skip_to_end_of_block_or_statement (parser);
30181 token = cp_lexer_peek_token (parser->lexer);
30182 continue;
30184 objc_start_method_definition (is_class_method, sig, attribute,
30185 NULL_TREE);
30187 /* For historical reasons, we accept an optional semicolon. */
30188 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30189 cp_lexer_consume_token (parser->lexer);
30191 ptk = cp_lexer_peek_token (parser->lexer);
30192 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30193 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30195 perform_deferred_access_checks (tf_warning_or_error);
30196 stop_deferring_access_checks ();
30197 meth = cp_parser_function_definition_after_declarator (parser,
30198 false);
30199 pop_deferring_access_checks ();
30200 objc_finish_method_definition (meth);
30203 /* The following case will be removed once @synthesize is
30204 completely implemented. */
30205 else if (token->keyword == RID_AT_PROPERTY)
30206 cp_parser_objc_at_property_declaration (parser);
30207 else if (token->keyword == RID_AT_SYNTHESIZE)
30208 cp_parser_objc_at_synthesize_declaration (parser);
30209 else if (token->keyword == RID_AT_DYNAMIC)
30210 cp_parser_objc_at_dynamic_declaration (parser);
30211 else if (token->keyword == RID_ATTRIBUTE
30212 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30213 warning_at (token->location, OPT_Wattributes,
30214 "prefix attributes are ignored for methods");
30215 else
30216 /* Allow for interspersed non-ObjC++ code. */
30217 cp_parser_objc_interstitial_code (parser);
30219 token = cp_lexer_peek_token (parser->lexer);
30222 if (token->type != CPP_EOF)
30223 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30224 else
30225 cp_parser_error (parser, "expected %<@end%>");
30227 objc_finish_implementation ();
30230 /* Parse Objective-C ivars. */
30232 static void
30233 cp_parser_objc_class_ivars (cp_parser* parser)
30235 cp_token *token = cp_lexer_peek_token (parser->lexer);
30237 if (token->type != CPP_OPEN_BRACE)
30238 return; /* No ivars specified. */
30240 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30241 token = cp_lexer_peek_token (parser->lexer);
30243 while (token->type != CPP_CLOSE_BRACE
30244 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30246 cp_decl_specifier_seq declspecs;
30247 int decl_class_or_enum_p;
30248 tree prefix_attributes;
30250 cp_parser_objc_visibility_spec (parser);
30252 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30253 break;
30255 cp_parser_decl_specifier_seq (parser,
30256 CP_PARSER_FLAGS_OPTIONAL,
30257 &declspecs,
30258 &decl_class_or_enum_p);
30260 /* auto, register, static, extern, mutable. */
30261 if (declspecs.storage_class != sc_none)
30263 cp_parser_error (parser, "invalid type for instance variable");
30264 declspecs.storage_class = sc_none;
30267 /* thread_local. */
30268 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30270 cp_parser_error (parser, "invalid type for instance variable");
30271 declspecs.locations[ds_thread] = 0;
30274 /* typedef. */
30275 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30277 cp_parser_error (parser, "invalid type for instance variable");
30278 declspecs.locations[ds_typedef] = 0;
30281 prefix_attributes = declspecs.attributes;
30282 declspecs.attributes = NULL_TREE;
30284 /* Keep going until we hit the `;' at the end of the
30285 declaration. */
30286 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30288 tree width = NULL_TREE, attributes, first_attribute, decl;
30289 cp_declarator *declarator = NULL;
30290 int ctor_dtor_or_conv_p;
30292 /* Check for a (possibly unnamed) bitfield declaration. */
30293 token = cp_lexer_peek_token (parser->lexer);
30294 if (token->type == CPP_COLON)
30295 goto eat_colon;
30297 if (token->type == CPP_NAME
30298 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30299 == CPP_COLON))
30301 /* Get the name of the bitfield. */
30302 declarator = make_id_declarator (NULL_TREE,
30303 cp_parser_identifier (parser),
30304 sfk_none);
30306 eat_colon:
30307 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30308 /* Get the width of the bitfield. */
30309 width
30310 = cp_parser_constant_expression (parser);
30312 else
30314 /* Parse the declarator. */
30315 declarator
30316 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30317 &ctor_dtor_or_conv_p,
30318 /*parenthesized_p=*/NULL,
30319 /*member_p=*/false,
30320 /*friend_p=*/false);
30323 /* Look for attributes that apply to the ivar. */
30324 attributes = cp_parser_attributes_opt (parser);
30325 /* Remember which attributes are prefix attributes and
30326 which are not. */
30327 first_attribute = attributes;
30328 /* Combine the attributes. */
30329 attributes = attr_chainon (prefix_attributes, attributes);
30331 if (width)
30332 /* Create the bitfield declaration. */
30333 decl = grokbitfield (declarator, &declspecs,
30334 width, NULL_TREE, attributes);
30335 else
30336 decl = grokfield (declarator, &declspecs,
30337 NULL_TREE, /*init_const_expr_p=*/false,
30338 NULL_TREE, attributes);
30340 /* Add the instance variable. */
30341 if (decl != error_mark_node && decl != NULL_TREE)
30342 objc_add_instance_variable (decl);
30344 /* Reset PREFIX_ATTRIBUTES. */
30345 if (attributes != error_mark_node)
30347 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30348 attributes = TREE_CHAIN (attributes);
30349 if (attributes)
30350 TREE_CHAIN (attributes) = NULL_TREE;
30353 token = cp_lexer_peek_token (parser->lexer);
30355 if (token->type == CPP_COMMA)
30357 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30358 continue;
30360 break;
30363 cp_parser_consume_semicolon_at_end_of_statement (parser);
30364 token = cp_lexer_peek_token (parser->lexer);
30367 if (token->keyword == RID_AT_END)
30368 cp_parser_error (parser, "expected %<}%>");
30370 /* Do not consume the RID_AT_END, so it will be read again as terminating
30371 the @interface of @implementation. */
30372 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30373 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30375 /* For historical reasons, we accept an optional semicolon. */
30376 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30377 cp_lexer_consume_token (parser->lexer);
30380 /* Parse an Objective-C protocol declaration. */
30382 static void
30383 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30385 tree proto, protorefs;
30386 cp_token *tok;
30388 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30391 tok = cp_lexer_peek_token (parser->lexer);
30392 error_at (tok->location, "identifier expected after %<@protocol%>");
30393 cp_parser_consume_semicolon_at_end_of_statement (parser);
30394 return;
30397 /* See if we have a forward declaration or a definition. */
30398 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30400 /* Try a forward declaration first. */
30401 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30403 while (true)
30405 tree id;
30407 id = cp_parser_identifier (parser);
30408 if (id == error_mark_node)
30409 break;
30411 objc_declare_protocol (id, attributes);
30413 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30414 cp_lexer_consume_token (parser->lexer);
30415 else
30416 break;
30418 cp_parser_consume_semicolon_at_end_of_statement (parser);
30421 /* Ok, we got a full-fledged definition (or at least should). */
30422 else
30424 proto = cp_parser_identifier (parser);
30425 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30426 objc_start_protocol (proto, protorefs, attributes);
30427 cp_parser_objc_method_prototype_list (parser);
30431 /* Parse an Objective-C superclass or category. */
30433 static void
30434 cp_parser_objc_superclass_or_category (cp_parser *parser,
30435 bool iface_p,
30436 tree *super,
30437 tree *categ, bool *is_class_extension)
30439 cp_token *next = cp_lexer_peek_token (parser->lexer);
30441 *super = *categ = NULL_TREE;
30442 *is_class_extension = false;
30443 if (next->type == CPP_COLON)
30445 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30446 *super = cp_parser_identifier (parser);
30448 else if (next->type == CPP_OPEN_PAREN)
30450 matching_parens parens;
30451 parens.consume_open (parser); /* Eat '('. */
30453 /* If there is no category name, and this is an @interface, we
30454 have a class extension. */
30455 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30457 *categ = NULL_TREE;
30458 *is_class_extension = true;
30460 else
30461 *categ = cp_parser_identifier (parser);
30463 parens.require_close (parser);
30467 /* Parse an Objective-C class interface. */
30469 static void
30470 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30472 tree name, super, categ, protos;
30473 bool is_class_extension;
30475 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30476 name = cp_parser_identifier (parser);
30477 if (name == error_mark_node)
30479 /* It's hard to recover because even if valid @interface stuff
30480 is to follow, we can't compile it (or validate it) if we
30481 don't even know which class it refers to. Let's assume this
30482 was a stray '@interface' token in the stream and skip it.
30484 return;
30486 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30487 &is_class_extension);
30488 protos = cp_parser_objc_protocol_refs_opt (parser);
30490 /* We have either a class or a category on our hands. */
30491 if (categ || is_class_extension)
30492 objc_start_category_interface (name, categ, protos, attributes);
30493 else
30495 objc_start_class_interface (name, super, protos, attributes);
30496 /* Handle instance variable declarations, if any. */
30497 cp_parser_objc_class_ivars (parser);
30498 objc_continue_interface ();
30501 cp_parser_objc_method_prototype_list (parser);
30504 /* Parse an Objective-C class implementation. */
30506 static void
30507 cp_parser_objc_class_implementation (cp_parser* parser)
30509 tree name, super, categ;
30510 bool is_class_extension;
30512 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30513 name = cp_parser_identifier (parser);
30514 if (name == error_mark_node)
30516 /* It's hard to recover because even if valid @implementation
30517 stuff is to follow, we can't compile it (or validate it) if
30518 we don't even know which class it refers to. Let's assume
30519 this was a stray '@implementation' token in the stream and
30520 skip it.
30522 return;
30524 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30525 &is_class_extension);
30527 /* We have either a class or a category on our hands. */
30528 if (categ)
30529 objc_start_category_implementation (name, categ);
30530 else
30532 objc_start_class_implementation (name, super);
30533 /* Handle instance variable declarations, if any. */
30534 cp_parser_objc_class_ivars (parser);
30535 objc_continue_implementation ();
30538 cp_parser_objc_method_definition_list (parser);
30541 /* Consume the @end token and finish off the implementation. */
30543 static void
30544 cp_parser_objc_end_implementation (cp_parser* parser)
30546 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30547 objc_finish_implementation ();
30550 /* Parse an Objective-C declaration. */
30552 static void
30553 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30555 /* Try to figure out what kind of declaration is present. */
30556 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30558 if (attributes)
30559 switch (kwd->keyword)
30561 case RID_AT_ALIAS:
30562 case RID_AT_CLASS:
30563 case RID_AT_END:
30564 error_at (kwd->location, "attributes may not be specified before"
30565 " the %<@%D%> Objective-C++ keyword",
30566 kwd->u.value);
30567 attributes = NULL;
30568 break;
30569 case RID_AT_IMPLEMENTATION:
30570 warning_at (kwd->location, OPT_Wattributes,
30571 "prefix attributes are ignored before %<@%D%>",
30572 kwd->u.value);
30573 attributes = NULL;
30574 default:
30575 break;
30578 switch (kwd->keyword)
30580 case RID_AT_ALIAS:
30581 cp_parser_objc_alias_declaration (parser);
30582 break;
30583 case RID_AT_CLASS:
30584 cp_parser_objc_class_declaration (parser);
30585 break;
30586 case RID_AT_PROTOCOL:
30587 cp_parser_objc_protocol_declaration (parser, attributes);
30588 break;
30589 case RID_AT_INTERFACE:
30590 cp_parser_objc_class_interface (parser, attributes);
30591 break;
30592 case RID_AT_IMPLEMENTATION:
30593 cp_parser_objc_class_implementation (parser);
30594 break;
30595 case RID_AT_END:
30596 cp_parser_objc_end_implementation (parser);
30597 break;
30598 default:
30599 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30600 kwd->u.value);
30601 cp_parser_skip_to_end_of_block_or_statement (parser);
30605 /* Parse an Objective-C try-catch-finally statement.
30607 objc-try-catch-finally-stmt:
30608 @try compound-statement objc-catch-clause-seq [opt]
30609 objc-finally-clause [opt]
30611 objc-catch-clause-seq:
30612 objc-catch-clause objc-catch-clause-seq [opt]
30614 objc-catch-clause:
30615 @catch ( objc-exception-declaration ) compound-statement
30617 objc-finally-clause:
30618 @finally compound-statement
30620 objc-exception-declaration:
30621 parameter-declaration
30622 '...'
30624 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30626 Returns NULL_TREE.
30628 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30629 for C. Keep them in sync. */
30631 static tree
30632 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30634 location_t location;
30635 tree stmt;
30637 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30638 location = cp_lexer_peek_token (parser->lexer)->location;
30639 objc_maybe_warn_exceptions (location);
30640 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30641 node, lest it get absorbed into the surrounding block. */
30642 stmt = push_stmt_list ();
30643 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30644 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30646 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30648 cp_parameter_declarator *parm;
30649 tree parameter_declaration = error_mark_node;
30650 bool seen_open_paren = false;
30651 matching_parens parens;
30653 cp_lexer_consume_token (parser->lexer);
30654 if (parens.require_open (parser))
30655 seen_open_paren = true;
30656 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30658 /* We have "@catch (...)" (where the '...' are literally
30659 what is in the code). Skip the '...'.
30660 parameter_declaration is set to NULL_TREE, and
30661 objc_being_catch_clauses() knows that that means
30662 '...'. */
30663 cp_lexer_consume_token (parser->lexer);
30664 parameter_declaration = NULL_TREE;
30666 else
30668 /* We have "@catch (NSException *exception)" or something
30669 like that. Parse the parameter declaration. */
30670 parm = cp_parser_parameter_declaration (parser, false, NULL);
30671 if (parm == NULL)
30672 parameter_declaration = error_mark_node;
30673 else
30674 parameter_declaration = grokdeclarator (parm->declarator,
30675 &parm->decl_specifiers,
30676 PARM, /*initialized=*/0,
30677 /*attrlist=*/NULL);
30679 if (seen_open_paren)
30680 parens.require_close (parser);
30681 else
30683 /* If there was no open parenthesis, we are recovering from
30684 an error, and we are trying to figure out what mistake
30685 the user has made. */
30687 /* If there is an immediate closing parenthesis, the user
30688 probably forgot the opening one (ie, they typed "@catch
30689 NSException *e)". Parse the closing parenthesis and keep
30690 going. */
30691 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30692 cp_lexer_consume_token (parser->lexer);
30694 /* If these is no immediate closing parenthesis, the user
30695 probably doesn't know that parenthesis are required at
30696 all (ie, they typed "@catch NSException *e"). So, just
30697 forget about the closing parenthesis and keep going. */
30699 objc_begin_catch_clause (parameter_declaration);
30700 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30701 objc_finish_catch_clause ();
30703 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30705 cp_lexer_consume_token (parser->lexer);
30706 location = cp_lexer_peek_token (parser->lexer)->location;
30707 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30708 node, lest it get absorbed into the surrounding block. */
30709 stmt = push_stmt_list ();
30710 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30711 objc_build_finally_clause (location, pop_stmt_list (stmt));
30714 return objc_finish_try_stmt ();
30717 /* Parse an Objective-C synchronized statement.
30719 objc-synchronized-stmt:
30720 @synchronized ( expression ) compound-statement
30722 Returns NULL_TREE. */
30724 static tree
30725 cp_parser_objc_synchronized_statement (cp_parser *parser)
30727 location_t location;
30728 tree lock, stmt;
30730 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30732 location = cp_lexer_peek_token (parser->lexer)->location;
30733 objc_maybe_warn_exceptions (location);
30734 matching_parens parens;
30735 parens.require_open (parser);
30736 lock = cp_parser_expression (parser);
30737 parens.require_close (parser);
30739 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30740 node, lest it get absorbed into the surrounding block. */
30741 stmt = push_stmt_list ();
30742 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30744 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30747 /* Parse an Objective-C throw statement.
30749 objc-throw-stmt:
30750 @throw assignment-expression [opt] ;
30752 Returns a constructed '@throw' statement. */
30754 static tree
30755 cp_parser_objc_throw_statement (cp_parser *parser)
30757 tree expr = NULL_TREE;
30758 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30760 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30763 expr = cp_parser_expression (parser);
30765 cp_parser_consume_semicolon_at_end_of_statement (parser);
30767 return objc_build_throw_stmt (loc, expr);
30770 /* Parse an Objective-C statement. */
30772 static tree
30773 cp_parser_objc_statement (cp_parser * parser)
30775 /* Try to figure out what kind of declaration is present. */
30776 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30778 switch (kwd->keyword)
30780 case RID_AT_TRY:
30781 return cp_parser_objc_try_catch_finally_statement (parser);
30782 case RID_AT_SYNCHRONIZED:
30783 return cp_parser_objc_synchronized_statement (parser);
30784 case RID_AT_THROW:
30785 return cp_parser_objc_throw_statement (parser);
30786 default:
30787 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30788 kwd->u.value);
30789 cp_parser_skip_to_end_of_block_or_statement (parser);
30792 return error_mark_node;
30795 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30796 look ahead to see if an objc keyword follows the attributes. This
30797 is to detect the use of prefix attributes on ObjC @interface and
30798 @protocol. */
30800 static bool
30801 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30803 cp_lexer_save_tokens (parser->lexer);
30804 *attrib = cp_parser_attributes_opt (parser);
30805 gcc_assert (*attrib);
30806 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30808 cp_lexer_commit_tokens (parser->lexer);
30809 return true;
30811 cp_lexer_rollback_tokens (parser->lexer);
30812 return false;
30815 /* This routine is a minimal replacement for
30816 c_parser_struct_declaration () used when parsing the list of
30817 types/names or ObjC++ properties. For example, when parsing the
30818 code
30820 @property (readonly) int a, b, c;
30822 this function is responsible for parsing "int a, int b, int c" and
30823 returning the declarations as CHAIN of DECLs.
30825 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30826 similar parsing. */
30827 static tree
30828 cp_parser_objc_struct_declaration (cp_parser *parser)
30830 tree decls = NULL_TREE;
30831 cp_decl_specifier_seq declspecs;
30832 int decl_class_or_enum_p;
30833 tree prefix_attributes;
30835 cp_parser_decl_specifier_seq (parser,
30836 CP_PARSER_FLAGS_NONE,
30837 &declspecs,
30838 &decl_class_or_enum_p);
30840 if (declspecs.type == error_mark_node)
30841 return error_mark_node;
30843 /* auto, register, static, extern, mutable. */
30844 if (declspecs.storage_class != sc_none)
30846 cp_parser_error (parser, "invalid type for property");
30847 declspecs.storage_class = sc_none;
30850 /* thread_local. */
30851 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30853 cp_parser_error (parser, "invalid type for property");
30854 declspecs.locations[ds_thread] = 0;
30857 /* typedef. */
30858 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30860 cp_parser_error (parser, "invalid type for property");
30861 declspecs.locations[ds_typedef] = 0;
30864 prefix_attributes = declspecs.attributes;
30865 declspecs.attributes = NULL_TREE;
30867 /* Keep going until we hit the `;' at the end of the declaration. */
30868 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30870 tree attributes, first_attribute, decl;
30871 cp_declarator *declarator;
30872 cp_token *token;
30874 /* Parse the declarator. */
30875 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30876 NULL, NULL, false, false);
30878 /* Look for attributes that apply to the ivar. */
30879 attributes = cp_parser_attributes_opt (parser);
30880 /* Remember which attributes are prefix attributes and
30881 which are not. */
30882 first_attribute = attributes;
30883 /* Combine the attributes. */
30884 attributes = attr_chainon (prefix_attributes, attributes);
30886 decl = grokfield (declarator, &declspecs,
30887 NULL_TREE, /*init_const_expr_p=*/false,
30888 NULL_TREE, attributes);
30890 if (decl == error_mark_node || decl == NULL_TREE)
30891 return error_mark_node;
30893 /* Reset PREFIX_ATTRIBUTES. */
30894 if (attributes != error_mark_node)
30896 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30897 attributes = TREE_CHAIN (attributes);
30898 if (attributes)
30899 TREE_CHAIN (attributes) = NULL_TREE;
30902 DECL_CHAIN (decl) = decls;
30903 decls = decl;
30905 token = cp_lexer_peek_token (parser->lexer);
30906 if (token->type == CPP_COMMA)
30908 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30909 continue;
30911 else
30912 break;
30914 return decls;
30917 /* Parse an Objective-C @property declaration. The syntax is:
30919 objc-property-declaration:
30920 '@property' objc-property-attributes[opt] struct-declaration ;
30922 objc-property-attributes:
30923 '(' objc-property-attribute-list ')'
30925 objc-property-attribute-list:
30926 objc-property-attribute
30927 objc-property-attribute-list, objc-property-attribute
30929 objc-property-attribute
30930 'getter' = identifier
30931 'setter' = identifier
30932 'readonly'
30933 'readwrite'
30934 'assign'
30935 'retain'
30936 'copy'
30937 'nonatomic'
30939 For example:
30940 @property NSString *name;
30941 @property (readonly) id object;
30942 @property (retain, nonatomic, getter=getTheName) id name;
30943 @property int a, b, c;
30945 PS: This function is identical to
30946 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30947 static void
30948 cp_parser_objc_at_property_declaration (cp_parser *parser)
30950 /* The following variables hold the attributes of the properties as
30951 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30952 seen. When we see an attribute, we set them to 'true' (if they
30953 are boolean properties) or to the identifier (if they have an
30954 argument, ie, for getter and setter). Note that here we only
30955 parse the list of attributes, check the syntax and accumulate the
30956 attributes that we find. objc_add_property_declaration() will
30957 then process the information. */
30958 bool property_assign = false;
30959 bool property_copy = false;
30960 tree property_getter_ident = NULL_TREE;
30961 bool property_nonatomic = false;
30962 bool property_readonly = false;
30963 bool property_readwrite = false;
30964 bool property_retain = false;
30965 tree property_setter_ident = NULL_TREE;
30967 /* 'properties' is the list of properties that we read. Usually a
30968 single one, but maybe more (eg, in "@property int a, b, c;" there
30969 are three). */
30970 tree properties;
30971 location_t loc;
30973 loc = cp_lexer_peek_token (parser->lexer)->location;
30975 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30977 /* Parse the optional attribute list... */
30978 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30980 /* Eat the '('. */
30981 matching_parens parens;
30982 parens.consume_open (parser);
30984 while (true)
30986 bool syntax_error = false;
30987 cp_token *token = cp_lexer_peek_token (parser->lexer);
30988 enum rid keyword;
30990 if (token->type != CPP_NAME)
30992 cp_parser_error (parser, "expected identifier");
30993 break;
30995 keyword = C_RID_CODE (token->u.value);
30996 cp_lexer_consume_token (parser->lexer);
30997 switch (keyword)
30999 case RID_ASSIGN: property_assign = true; break;
31000 case RID_COPY: property_copy = true; break;
31001 case RID_NONATOMIC: property_nonatomic = true; break;
31002 case RID_READONLY: property_readonly = true; break;
31003 case RID_READWRITE: property_readwrite = true; break;
31004 case RID_RETAIN: property_retain = true; break;
31006 case RID_GETTER:
31007 case RID_SETTER:
31008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31010 if (keyword == RID_GETTER)
31011 cp_parser_error (parser,
31012 "missing %<=%> (after %<getter%> attribute)");
31013 else
31014 cp_parser_error (parser,
31015 "missing %<=%> (after %<setter%> attribute)");
31016 syntax_error = true;
31017 break;
31019 cp_lexer_consume_token (parser->lexer); /* eat the = */
31020 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31022 cp_parser_error (parser, "expected identifier");
31023 syntax_error = true;
31024 break;
31026 if (keyword == RID_SETTER)
31028 if (property_setter_ident != NULL_TREE)
31030 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31031 cp_lexer_consume_token (parser->lexer);
31033 else
31034 property_setter_ident = cp_parser_objc_selector (parser);
31035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31036 cp_parser_error (parser, "setter name must terminate with %<:%>");
31037 else
31038 cp_lexer_consume_token (parser->lexer);
31040 else
31042 if (property_getter_ident != NULL_TREE)
31044 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31045 cp_lexer_consume_token (parser->lexer);
31047 else
31048 property_getter_ident = cp_parser_objc_selector (parser);
31050 break;
31051 default:
31052 cp_parser_error (parser, "unknown property attribute");
31053 syntax_error = true;
31054 break;
31057 if (syntax_error)
31058 break;
31060 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31061 cp_lexer_consume_token (parser->lexer);
31062 else
31063 break;
31066 /* FIXME: "@property (setter, assign);" will generate a spurious
31067 "error: expected ‘)’ before ‘,’ token". This is because
31068 cp_parser_require, unlike the C counterpart, will produce an
31069 error even if we are in error recovery. */
31070 if (!parens.require_close (parser))
31072 cp_parser_skip_to_closing_parenthesis (parser,
31073 /*recovering=*/true,
31074 /*or_comma=*/false,
31075 /*consume_paren=*/true);
31079 /* ... and the property declaration(s). */
31080 properties = cp_parser_objc_struct_declaration (parser);
31082 if (properties == error_mark_node)
31084 cp_parser_skip_to_end_of_statement (parser);
31085 /* If the next token is now a `;', consume it. */
31086 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31087 cp_lexer_consume_token (parser->lexer);
31088 return;
31091 if (properties == NULL_TREE)
31092 cp_parser_error (parser, "expected identifier");
31093 else
31095 /* Comma-separated properties are chained together in
31096 reverse order; add them one by one. */
31097 properties = nreverse (properties);
31099 for (; properties; properties = TREE_CHAIN (properties))
31100 objc_add_property_declaration (loc, copy_node (properties),
31101 property_readonly, property_readwrite,
31102 property_assign, property_retain,
31103 property_copy, property_nonatomic,
31104 property_getter_ident, property_setter_ident);
31107 cp_parser_consume_semicolon_at_end_of_statement (parser);
31110 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31112 objc-synthesize-declaration:
31113 @synthesize objc-synthesize-identifier-list ;
31115 objc-synthesize-identifier-list:
31116 objc-synthesize-identifier
31117 objc-synthesize-identifier-list, objc-synthesize-identifier
31119 objc-synthesize-identifier
31120 identifier
31121 identifier = identifier
31123 For example:
31124 @synthesize MyProperty;
31125 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31127 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31128 for C. Keep them in sync.
31130 static void
31131 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31133 tree list = NULL_TREE;
31134 location_t loc;
31135 loc = cp_lexer_peek_token (parser->lexer)->location;
31137 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31138 while (true)
31140 tree property, ivar;
31141 property = cp_parser_identifier (parser);
31142 if (property == error_mark_node)
31144 cp_parser_consume_semicolon_at_end_of_statement (parser);
31145 return;
31147 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31149 cp_lexer_consume_token (parser->lexer);
31150 ivar = cp_parser_identifier (parser);
31151 if (ivar == error_mark_node)
31153 cp_parser_consume_semicolon_at_end_of_statement (parser);
31154 return;
31157 else
31158 ivar = NULL_TREE;
31159 list = chainon (list, build_tree_list (ivar, property));
31160 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31161 cp_lexer_consume_token (parser->lexer);
31162 else
31163 break;
31165 cp_parser_consume_semicolon_at_end_of_statement (parser);
31166 objc_add_synthesize_declaration (loc, list);
31169 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31171 objc-dynamic-declaration:
31172 @dynamic identifier-list ;
31174 For example:
31175 @dynamic MyProperty;
31176 @dynamic MyProperty, AnotherProperty;
31178 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31179 for C. Keep them in sync.
31181 static void
31182 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31184 tree list = NULL_TREE;
31185 location_t loc;
31186 loc = cp_lexer_peek_token (parser->lexer)->location;
31188 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31189 while (true)
31191 tree property;
31192 property = cp_parser_identifier (parser);
31193 if (property == error_mark_node)
31195 cp_parser_consume_semicolon_at_end_of_statement (parser);
31196 return;
31198 list = chainon (list, build_tree_list (NULL, property));
31199 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31200 cp_lexer_consume_token (parser->lexer);
31201 else
31202 break;
31204 cp_parser_consume_semicolon_at_end_of_statement (parser);
31205 objc_add_dynamic_declaration (loc, list);
31209 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31211 /* Returns name of the next clause.
31212 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31213 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31214 returned and the token is consumed. */
31216 static pragma_omp_clause
31217 cp_parser_omp_clause_name (cp_parser *parser)
31219 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31222 result = PRAGMA_OACC_CLAUSE_AUTO;
31223 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31224 result = PRAGMA_OMP_CLAUSE_IF;
31225 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31226 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31227 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31228 result = PRAGMA_OACC_CLAUSE_DELETE;
31229 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31230 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31231 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31232 result = PRAGMA_OMP_CLAUSE_FOR;
31233 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31235 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31236 const char *p = IDENTIFIER_POINTER (id);
31238 switch (p[0])
31240 case 'a':
31241 if (!strcmp ("aligned", p))
31242 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31243 else if (!strcmp ("async", p))
31244 result = PRAGMA_OACC_CLAUSE_ASYNC;
31245 break;
31246 case 'c':
31247 if (!strcmp ("collapse", p))
31248 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31249 else if (!strcmp ("copy", p))
31250 result = PRAGMA_OACC_CLAUSE_COPY;
31251 else if (!strcmp ("copyin", p))
31252 result = PRAGMA_OMP_CLAUSE_COPYIN;
31253 else if (!strcmp ("copyout", p))
31254 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31255 else if (!strcmp ("copyprivate", p))
31256 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31257 else if (!strcmp ("create", p))
31258 result = PRAGMA_OACC_CLAUSE_CREATE;
31259 break;
31260 case 'd':
31261 if (!strcmp ("defaultmap", p))
31262 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31263 else if (!strcmp ("depend", p))
31264 result = PRAGMA_OMP_CLAUSE_DEPEND;
31265 else if (!strcmp ("device", p))
31266 result = PRAGMA_OMP_CLAUSE_DEVICE;
31267 else if (!strcmp ("deviceptr", p))
31268 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31269 else if (!strcmp ("device_resident", p))
31270 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31271 else if (!strcmp ("dist_schedule", p))
31272 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31273 break;
31274 case 'f':
31275 if (!strcmp ("final", p))
31276 result = PRAGMA_OMP_CLAUSE_FINAL;
31277 else if (!strcmp ("firstprivate", p))
31278 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31279 else if (!strcmp ("from", p))
31280 result = PRAGMA_OMP_CLAUSE_FROM;
31281 break;
31282 case 'g':
31283 if (!strcmp ("gang", p))
31284 result = PRAGMA_OACC_CLAUSE_GANG;
31285 else if (!strcmp ("grainsize", p))
31286 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31287 break;
31288 case 'h':
31289 if (!strcmp ("hint", p))
31290 result = PRAGMA_OMP_CLAUSE_HINT;
31291 else if (!strcmp ("host", p))
31292 result = PRAGMA_OACC_CLAUSE_HOST;
31293 break;
31294 case 'i':
31295 if (!strcmp ("inbranch", p))
31296 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31297 else if (!strcmp ("independent", p))
31298 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31299 else if (!strcmp ("is_device_ptr", p))
31300 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31301 break;
31302 case 'l':
31303 if (!strcmp ("lastprivate", p))
31304 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31305 else if (!strcmp ("linear", p))
31306 result = PRAGMA_OMP_CLAUSE_LINEAR;
31307 else if (!strcmp ("link", p))
31308 result = PRAGMA_OMP_CLAUSE_LINK;
31309 break;
31310 case 'm':
31311 if (!strcmp ("map", p))
31312 result = PRAGMA_OMP_CLAUSE_MAP;
31313 else if (!strcmp ("mergeable", p))
31314 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31315 break;
31316 case 'n':
31317 if (!strcmp ("nogroup", p))
31318 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31319 else if (!strcmp ("notinbranch", p))
31320 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31321 else if (!strcmp ("nowait", p))
31322 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31323 else if (!strcmp ("num_gangs", p))
31324 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31325 else if (!strcmp ("num_tasks", p))
31326 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31327 else if (!strcmp ("num_teams", p))
31328 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31329 else if (!strcmp ("num_threads", p))
31330 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31331 else if (!strcmp ("num_workers", p))
31332 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31333 break;
31334 case 'o':
31335 if (!strcmp ("ordered", p))
31336 result = PRAGMA_OMP_CLAUSE_ORDERED;
31337 break;
31338 case 'p':
31339 if (!strcmp ("parallel", p))
31340 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31341 else if (!strcmp ("present", p))
31342 result = PRAGMA_OACC_CLAUSE_PRESENT;
31343 else if (!strcmp ("present_or_copy", p)
31344 || !strcmp ("pcopy", p))
31345 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31346 else if (!strcmp ("present_or_copyin", p)
31347 || !strcmp ("pcopyin", p))
31348 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31349 else if (!strcmp ("present_or_copyout", p)
31350 || !strcmp ("pcopyout", p))
31351 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31352 else if (!strcmp ("present_or_create", p)
31353 || !strcmp ("pcreate", p))
31354 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31355 else if (!strcmp ("priority", p))
31356 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31357 else if (!strcmp ("proc_bind", p))
31358 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31359 break;
31360 case 'r':
31361 if (!strcmp ("reduction", p))
31362 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31363 break;
31364 case 's':
31365 if (!strcmp ("safelen", p))
31366 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31367 else if (!strcmp ("schedule", p))
31368 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31369 else if (!strcmp ("sections", p))
31370 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31371 else if (!strcmp ("self", p))
31372 result = PRAGMA_OACC_CLAUSE_SELF;
31373 else if (!strcmp ("seq", p))
31374 result = PRAGMA_OACC_CLAUSE_SEQ;
31375 else if (!strcmp ("shared", p))
31376 result = PRAGMA_OMP_CLAUSE_SHARED;
31377 else if (!strcmp ("simd", p))
31378 result = PRAGMA_OMP_CLAUSE_SIMD;
31379 else if (!strcmp ("simdlen", p))
31380 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31381 break;
31382 case 't':
31383 if (!strcmp ("taskgroup", p))
31384 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31385 else if (!strcmp ("thread_limit", p))
31386 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31387 else if (!strcmp ("threads", p))
31388 result = PRAGMA_OMP_CLAUSE_THREADS;
31389 else if (!strcmp ("tile", p))
31390 result = PRAGMA_OACC_CLAUSE_TILE;
31391 else if (!strcmp ("to", p))
31392 result = PRAGMA_OMP_CLAUSE_TO;
31393 break;
31394 case 'u':
31395 if (!strcmp ("uniform", p))
31396 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31397 else if (!strcmp ("untied", p))
31398 result = PRAGMA_OMP_CLAUSE_UNTIED;
31399 else if (!strcmp ("use_device", p))
31400 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31401 else if (!strcmp ("use_device_ptr", p))
31402 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31403 break;
31404 case 'v':
31405 if (!strcmp ("vector", p))
31406 result = PRAGMA_OACC_CLAUSE_VECTOR;
31407 else if (!strcmp ("vector_length", p))
31408 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31409 break;
31410 case 'w':
31411 if (!strcmp ("wait", p))
31412 result = PRAGMA_OACC_CLAUSE_WAIT;
31413 else if (!strcmp ("worker", p))
31414 result = PRAGMA_OACC_CLAUSE_WORKER;
31415 break;
31419 if (result != PRAGMA_OMP_CLAUSE_NONE)
31420 cp_lexer_consume_token (parser->lexer);
31422 return result;
31425 /* Validate that a clause of the given type does not already exist. */
31427 static void
31428 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31429 const char *name, location_t location)
31431 tree c;
31433 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31434 if (OMP_CLAUSE_CODE (c) == code)
31436 error_at (location, "too many %qs clauses", name);
31437 break;
31441 /* OpenMP 2.5:
31442 variable-list:
31443 identifier
31444 variable-list , identifier
31446 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31447 colon). An opening parenthesis will have been consumed by the caller.
31449 If KIND is nonzero, create the appropriate node and install the decl
31450 in OMP_CLAUSE_DECL and add the node to the head of the list.
31452 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31453 return the list created.
31455 COLON can be NULL if only closing parenthesis should end the list,
31456 or pointer to bool which will receive false if the list is terminated
31457 by closing parenthesis or true if the list is terminated by colon. */
31459 static tree
31460 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31461 tree list, bool *colon)
31463 cp_token *token;
31464 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31465 if (colon)
31467 parser->colon_corrects_to_scope_p = false;
31468 *colon = false;
31470 while (1)
31472 tree name, decl;
31474 token = cp_lexer_peek_token (parser->lexer);
31475 if (kind != 0
31476 && current_class_ptr
31477 && cp_parser_is_keyword (token, RID_THIS))
31479 decl = finish_this_expr ();
31480 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31481 || CONVERT_EXPR_P (decl))
31482 decl = TREE_OPERAND (decl, 0);
31483 cp_lexer_consume_token (parser->lexer);
31485 else
31487 name = cp_parser_id_expression (parser, /*template_p=*/false,
31488 /*check_dependency_p=*/true,
31489 /*template_p=*/NULL,
31490 /*declarator_p=*/false,
31491 /*optional_p=*/false);
31492 if (name == error_mark_node)
31493 goto skip_comma;
31495 if (identifier_p (name))
31496 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31497 else
31498 decl = name;
31499 if (decl == error_mark_node)
31500 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31501 token->location);
31503 if (decl == error_mark_node)
31505 else if (kind != 0)
31507 switch (kind)
31509 case OMP_CLAUSE__CACHE_:
31510 /* The OpenACC cache directive explicitly only allows "array
31511 elements or subarrays". */
31512 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31514 error_at (token->location, "expected %<[%>");
31515 decl = error_mark_node;
31516 break;
31518 /* FALLTHROUGH. */
31519 case OMP_CLAUSE_MAP:
31520 case OMP_CLAUSE_FROM:
31521 case OMP_CLAUSE_TO:
31522 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31524 location_t loc
31525 = cp_lexer_peek_token (parser->lexer)->location;
31526 cp_id_kind idk = CP_ID_KIND_NONE;
31527 cp_lexer_consume_token (parser->lexer);
31528 decl = convert_from_reference (decl);
31529 decl
31530 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31531 decl, false,
31532 &idk, loc);
31534 /* FALLTHROUGH. */
31535 case OMP_CLAUSE_DEPEND:
31536 case OMP_CLAUSE_REDUCTION:
31537 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31539 tree low_bound = NULL_TREE, length = NULL_TREE;
31541 parser->colon_corrects_to_scope_p = false;
31542 cp_lexer_consume_token (parser->lexer);
31543 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31544 low_bound = cp_parser_expression (parser);
31545 if (!colon)
31546 parser->colon_corrects_to_scope_p
31547 = saved_colon_corrects_to_scope_p;
31548 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31549 length = integer_one_node;
31550 else
31552 /* Look for `:'. */
31553 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31554 goto skip_comma;
31555 if (!cp_lexer_next_token_is (parser->lexer,
31556 CPP_CLOSE_SQUARE))
31557 length = cp_parser_expression (parser);
31559 /* Look for the closing `]'. */
31560 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31561 RT_CLOSE_SQUARE))
31562 goto skip_comma;
31564 decl = tree_cons (low_bound, length, decl);
31566 break;
31567 default:
31568 break;
31571 tree u = build_omp_clause (token->location, kind);
31572 OMP_CLAUSE_DECL (u) = decl;
31573 OMP_CLAUSE_CHAIN (u) = list;
31574 list = u;
31576 else
31577 list = tree_cons (decl, NULL_TREE, list);
31579 get_comma:
31580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31581 break;
31582 cp_lexer_consume_token (parser->lexer);
31585 if (colon)
31586 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31588 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31590 *colon = true;
31591 cp_parser_require (parser, CPP_COLON, RT_COLON);
31592 return list;
31595 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31597 int ending;
31599 /* Try to resync to an unnested comma. Copied from
31600 cp_parser_parenthesized_expression_list. */
31601 skip_comma:
31602 if (colon)
31603 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31604 ending = cp_parser_skip_to_closing_parenthesis (parser,
31605 /*recovering=*/true,
31606 /*or_comma=*/true,
31607 /*consume_paren=*/true);
31608 if (ending < 0)
31609 goto get_comma;
31612 return list;
31615 /* Similarly, but expect leading and trailing parenthesis. This is a very
31616 common case for omp clauses. */
31618 static tree
31619 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31621 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31622 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31623 return list;
31626 /* OpenACC 2.0:
31627 copy ( variable-list )
31628 copyin ( variable-list )
31629 copyout ( variable-list )
31630 create ( variable-list )
31631 delete ( variable-list )
31632 present ( variable-list )
31633 present_or_copy ( variable-list )
31634 pcopy ( variable-list )
31635 present_or_copyin ( variable-list )
31636 pcopyin ( variable-list )
31637 present_or_copyout ( variable-list )
31638 pcopyout ( variable-list )
31639 present_or_create ( variable-list )
31640 pcreate ( variable-list ) */
31642 static tree
31643 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31644 tree list)
31646 enum gomp_map_kind kind;
31647 switch (c_kind)
31649 case PRAGMA_OACC_CLAUSE_COPY:
31650 kind = GOMP_MAP_FORCE_TOFROM;
31651 break;
31652 case PRAGMA_OACC_CLAUSE_COPYIN:
31653 kind = GOMP_MAP_FORCE_TO;
31654 break;
31655 case PRAGMA_OACC_CLAUSE_COPYOUT:
31656 kind = GOMP_MAP_FORCE_FROM;
31657 break;
31658 case PRAGMA_OACC_CLAUSE_CREATE:
31659 kind = GOMP_MAP_FORCE_ALLOC;
31660 break;
31661 case PRAGMA_OACC_CLAUSE_DELETE:
31662 kind = GOMP_MAP_DELETE;
31663 break;
31664 case PRAGMA_OACC_CLAUSE_DEVICE:
31665 kind = GOMP_MAP_FORCE_TO;
31666 break;
31667 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31668 kind = GOMP_MAP_DEVICE_RESIDENT;
31669 break;
31670 case PRAGMA_OACC_CLAUSE_HOST:
31671 case PRAGMA_OACC_CLAUSE_SELF:
31672 kind = GOMP_MAP_FORCE_FROM;
31673 break;
31674 case PRAGMA_OACC_CLAUSE_LINK:
31675 kind = GOMP_MAP_LINK;
31676 break;
31677 case PRAGMA_OACC_CLAUSE_PRESENT:
31678 kind = GOMP_MAP_FORCE_PRESENT;
31679 break;
31680 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31681 kind = GOMP_MAP_TOFROM;
31682 break;
31683 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31684 kind = GOMP_MAP_TO;
31685 break;
31686 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31687 kind = GOMP_MAP_FROM;
31688 break;
31689 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31690 kind = GOMP_MAP_ALLOC;
31691 break;
31692 default:
31693 gcc_unreachable ();
31695 tree nl, c;
31696 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31698 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31699 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31701 return nl;
31704 /* OpenACC 2.0:
31705 deviceptr ( variable-list ) */
31707 static tree
31708 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31711 tree vars, t;
31713 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31714 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31715 variable-list must only allow for pointer variables. */
31716 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31717 for (t = vars; t; t = TREE_CHAIN (t))
31719 tree v = TREE_PURPOSE (t);
31720 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31721 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31722 OMP_CLAUSE_DECL (u) = v;
31723 OMP_CLAUSE_CHAIN (u) = list;
31724 list = u;
31727 return list;
31730 /* OpenACC 2.0:
31731 auto
31732 independent
31733 nohost
31734 seq */
31736 static tree
31737 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31738 enum omp_clause_code code,
31739 tree list, location_t location)
31741 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31742 tree c = build_omp_clause (location, code);
31743 OMP_CLAUSE_CHAIN (c) = list;
31744 return c;
31747 /* OpenACC:
31748 num_gangs ( expression )
31749 num_workers ( expression )
31750 vector_length ( expression ) */
31752 static tree
31753 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31754 const char *str, tree list)
31756 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31758 matching_parens parens;
31759 if (!parens.require_open (parser))
31760 return list;
31762 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31764 if (t == error_mark_node
31765 || !parens.require_close (parser))
31767 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31768 /*or_comma=*/false,
31769 /*consume_paren=*/true);
31770 return list;
31773 check_no_duplicate_clause (list, code, str, loc);
31775 tree c = build_omp_clause (loc, code);
31776 OMP_CLAUSE_OPERAND (c, 0) = t;
31777 OMP_CLAUSE_CHAIN (c) = list;
31778 return c;
31781 /* OpenACC:
31783 gang [( gang-arg-list )]
31784 worker [( [num:] int-expr )]
31785 vector [( [length:] int-expr )]
31787 where gang-arg is one of:
31789 [num:] int-expr
31790 static: size-expr
31792 and size-expr may be:
31795 int-expr
31798 static tree
31799 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31800 const char *str, tree list)
31802 const char *id = "num";
31803 cp_lexer *lexer = parser->lexer;
31804 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31805 location_t loc = cp_lexer_peek_token (lexer)->location;
31807 if (kind == OMP_CLAUSE_VECTOR)
31808 id = "length";
31810 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31812 matching_parens parens;
31813 parens.consume_open (parser);
31817 cp_token *next = cp_lexer_peek_token (lexer);
31818 int idx = 0;
31820 /* Gang static argument. */
31821 if (kind == OMP_CLAUSE_GANG
31822 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31824 cp_lexer_consume_token (lexer);
31826 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31827 goto cleanup_error;
31829 idx = 1;
31830 if (ops[idx] != NULL)
31832 cp_parser_error (parser, "too many %<static%> arguments");
31833 goto cleanup_error;
31836 /* Check for the '*' argument. */
31837 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31838 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31839 || cp_lexer_nth_token_is (parser->lexer, 2,
31840 CPP_CLOSE_PAREN)))
31842 cp_lexer_consume_token (lexer);
31843 ops[idx] = integer_minus_one_node;
31845 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31847 cp_lexer_consume_token (lexer);
31848 continue;
31850 else break;
31853 /* Worker num: argument and vector length: arguments. */
31854 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31855 && id_equal (next->u.value, id)
31856 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31858 cp_lexer_consume_token (lexer); /* id */
31859 cp_lexer_consume_token (lexer); /* ':' */
31862 /* Now collect the actual argument. */
31863 if (ops[idx] != NULL_TREE)
31865 cp_parser_error (parser, "unexpected argument");
31866 goto cleanup_error;
31869 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31870 false);
31871 if (expr == error_mark_node)
31872 goto cleanup_error;
31874 mark_exp_read (expr);
31875 ops[idx] = expr;
31877 if (kind == OMP_CLAUSE_GANG
31878 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31880 cp_lexer_consume_token (lexer);
31881 continue;
31883 break;
31885 while (1);
31887 if (!parens.require_close (parser))
31888 goto cleanup_error;
31891 check_no_duplicate_clause (list, kind, str, loc);
31893 c = build_omp_clause (loc, kind);
31895 if (ops[1])
31896 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31898 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31899 OMP_CLAUSE_CHAIN (c) = list;
31901 return c;
31903 cleanup_error:
31904 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31905 return list;
31908 /* OpenACC 2.0:
31909 tile ( size-expr-list ) */
31911 static tree
31912 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31914 tree c, expr = error_mark_node;
31915 tree tile = NULL_TREE;
31917 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31918 so, but the spec authors never considered such a case and have
31919 differing opinions on what it might mean, including 'not
31920 allowed'.) */
31921 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31922 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31923 clause_loc);
31925 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31926 return list;
31930 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31931 return list;
31933 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31934 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31935 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31937 cp_lexer_consume_token (parser->lexer);
31938 expr = integer_zero_node;
31940 else
31941 expr = cp_parser_constant_expression (parser);
31943 tile = tree_cons (NULL_TREE, expr, tile);
31945 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31947 /* Consume the trailing ')'. */
31948 cp_lexer_consume_token (parser->lexer);
31950 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31951 tile = nreverse (tile);
31952 OMP_CLAUSE_TILE_LIST (c) = tile;
31953 OMP_CLAUSE_CHAIN (c) = list;
31954 return c;
31957 /* OpenACC 2.0
31958 Parse wait clause or directive parameters. */
31960 static tree
31961 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31963 vec<tree, va_gc> *args;
31964 tree t, args_tree;
31966 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31967 /*cast_p=*/false,
31968 /*allow_expansion_p=*/true,
31969 /*non_constant_p=*/NULL);
31971 if (args == NULL || args->length () == 0)
31973 cp_parser_error (parser, "expected integer expression before ')'");
31974 if (args != NULL)
31975 release_tree_vector (args);
31976 return list;
31979 args_tree = build_tree_list_vec (args);
31981 release_tree_vector (args);
31983 for (t = args_tree; t; t = TREE_CHAIN (t))
31985 tree targ = TREE_VALUE (t);
31987 if (targ != error_mark_node)
31989 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31990 error ("%<wait%> expression must be integral");
31991 else
31993 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31995 targ = mark_rvalue_use (targ);
31996 OMP_CLAUSE_DECL (c) = targ;
31997 OMP_CLAUSE_CHAIN (c) = list;
31998 list = c;
32003 return list;
32006 /* OpenACC:
32007 wait ( int-expr-list ) */
32009 static tree
32010 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32012 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32014 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32015 return list;
32017 list = cp_parser_oacc_wait_list (parser, location, list);
32019 return list;
32022 /* OpenMP 3.0:
32023 collapse ( constant-expression ) */
32025 static tree
32026 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32028 tree c, num;
32029 location_t loc;
32030 HOST_WIDE_INT n;
32032 loc = cp_lexer_peek_token (parser->lexer)->location;
32033 matching_parens parens;
32034 if (!parens.require_open (parser))
32035 return list;
32037 num = cp_parser_constant_expression (parser);
32039 if (!parens.require_close (parser))
32040 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32041 /*or_comma=*/false,
32042 /*consume_paren=*/true);
32044 if (num == error_mark_node)
32045 return list;
32046 num = fold_non_dependent_expr (num);
32047 if (!tree_fits_shwi_p (num)
32048 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32049 || (n = tree_to_shwi (num)) <= 0
32050 || (int) n != n)
32052 error_at (loc, "collapse argument needs positive constant integer expression");
32053 return list;
32056 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32057 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32058 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32059 OMP_CLAUSE_CHAIN (c) = list;
32060 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32062 return c;
32065 /* OpenMP 2.5:
32066 default ( none | shared )
32068 OpenACC:
32069 default ( none | present ) */
32071 static tree
32072 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32073 location_t location, bool is_oacc)
32075 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32076 tree c;
32078 matching_parens parens;
32079 if (!parens.require_open (parser))
32080 return list;
32081 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32083 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32084 const char *p = IDENTIFIER_POINTER (id);
32086 switch (p[0])
32088 case 'n':
32089 if (strcmp ("none", p) != 0)
32090 goto invalid_kind;
32091 kind = OMP_CLAUSE_DEFAULT_NONE;
32092 break;
32094 case 'p':
32095 if (strcmp ("present", p) != 0 || !is_oacc)
32096 goto invalid_kind;
32097 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32098 break;
32100 case 's':
32101 if (strcmp ("shared", p) != 0 || is_oacc)
32102 goto invalid_kind;
32103 kind = OMP_CLAUSE_DEFAULT_SHARED;
32104 break;
32106 default:
32107 goto invalid_kind;
32110 cp_lexer_consume_token (parser->lexer);
32112 else
32114 invalid_kind:
32115 if (is_oacc)
32116 cp_parser_error (parser, "expected %<none%> or %<present%>");
32117 else
32118 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32121 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32122 || !parens.require_close (parser))
32123 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32124 /*or_comma=*/false,
32125 /*consume_paren=*/true);
32127 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32128 return list;
32130 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32131 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32132 OMP_CLAUSE_CHAIN (c) = list;
32133 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32135 return c;
32138 /* OpenMP 3.1:
32139 final ( expression ) */
32141 static tree
32142 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32144 tree t, c;
32146 matching_parens parens;
32147 if (!parens.require_open (parser))
32148 return list;
32150 t = cp_parser_condition (parser);
32152 if (t == error_mark_node
32153 || !parens.require_close (parser))
32154 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32155 /*or_comma=*/false,
32156 /*consume_paren=*/true);
32158 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32160 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32161 OMP_CLAUSE_FINAL_EXPR (c) = t;
32162 OMP_CLAUSE_CHAIN (c) = list;
32164 return c;
32167 /* OpenMP 2.5:
32168 if ( expression )
32170 OpenMP 4.5:
32171 if ( directive-name-modifier : expression )
32173 directive-name-modifier:
32174 parallel | task | taskloop | target data | target | target update
32175 | target enter data | target exit data */
32177 static tree
32178 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32179 bool is_omp)
32181 tree t, c;
32182 enum tree_code if_modifier = ERROR_MARK;
32184 matching_parens parens;
32185 if (!parens.require_open (parser))
32186 return list;
32188 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32190 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32191 const char *p = IDENTIFIER_POINTER (id);
32192 int n = 2;
32194 if (strcmp ("parallel", p) == 0)
32195 if_modifier = OMP_PARALLEL;
32196 else if (strcmp ("task", p) == 0)
32197 if_modifier = OMP_TASK;
32198 else if (strcmp ("taskloop", p) == 0)
32199 if_modifier = OMP_TASKLOOP;
32200 else if (strcmp ("target", p) == 0)
32202 if_modifier = OMP_TARGET;
32203 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32205 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32206 p = IDENTIFIER_POINTER (id);
32207 if (strcmp ("data", p) == 0)
32208 if_modifier = OMP_TARGET_DATA;
32209 else if (strcmp ("update", p) == 0)
32210 if_modifier = OMP_TARGET_UPDATE;
32211 else if (strcmp ("enter", p) == 0)
32212 if_modifier = OMP_TARGET_ENTER_DATA;
32213 else if (strcmp ("exit", p) == 0)
32214 if_modifier = OMP_TARGET_EXIT_DATA;
32215 if (if_modifier != OMP_TARGET)
32216 n = 3;
32217 else
32219 location_t loc
32220 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32221 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32222 "or %<exit%>");
32223 if_modifier = ERROR_MARK;
32225 if (if_modifier == OMP_TARGET_ENTER_DATA
32226 || if_modifier == OMP_TARGET_EXIT_DATA)
32228 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32230 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32231 p = IDENTIFIER_POINTER (id);
32232 if (strcmp ("data", p) == 0)
32233 n = 4;
32235 if (n != 4)
32237 location_t loc
32238 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32239 error_at (loc, "expected %<data%>");
32240 if_modifier = ERROR_MARK;
32245 if (if_modifier != ERROR_MARK)
32247 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32249 while (n-- > 0)
32250 cp_lexer_consume_token (parser->lexer);
32252 else
32254 if (n > 2)
32256 location_t loc
32257 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32258 error_at (loc, "expected %<:%>");
32260 if_modifier = ERROR_MARK;
32265 t = cp_parser_condition (parser);
32267 if (t == error_mark_node
32268 || !parens.require_close (parser))
32269 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32270 /*or_comma=*/false,
32271 /*consume_paren=*/true);
32273 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32274 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32276 if (if_modifier != ERROR_MARK
32277 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32279 const char *p = NULL;
32280 switch (if_modifier)
32282 case OMP_PARALLEL: p = "parallel"; break;
32283 case OMP_TASK: p = "task"; break;
32284 case OMP_TASKLOOP: p = "taskloop"; break;
32285 case OMP_TARGET_DATA: p = "target data"; break;
32286 case OMP_TARGET: p = "target"; break;
32287 case OMP_TARGET_UPDATE: p = "target update"; break;
32288 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32289 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32290 default: gcc_unreachable ();
32292 error_at (location, "too many %<if%> clauses with %qs modifier",
32294 return list;
32296 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32298 if (!is_omp)
32299 error_at (location, "too many %<if%> clauses");
32300 else
32301 error_at (location, "too many %<if%> clauses without modifier");
32302 return list;
32304 else if (if_modifier == ERROR_MARK
32305 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32307 error_at (location, "if any %<if%> clause has modifier, then all "
32308 "%<if%> clauses have to use modifier");
32309 return list;
32313 c = build_omp_clause (location, OMP_CLAUSE_IF);
32314 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32315 OMP_CLAUSE_IF_EXPR (c) = t;
32316 OMP_CLAUSE_CHAIN (c) = list;
32318 return c;
32321 /* OpenMP 3.1:
32322 mergeable */
32324 static tree
32325 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32326 tree list, location_t location)
32328 tree c;
32330 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32331 location);
32333 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32334 OMP_CLAUSE_CHAIN (c) = list;
32335 return c;
32338 /* OpenMP 2.5:
32339 nowait */
32341 static tree
32342 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32343 tree list, location_t location)
32345 tree c;
32347 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32349 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32350 OMP_CLAUSE_CHAIN (c) = list;
32351 return c;
32354 /* OpenMP 2.5:
32355 num_threads ( expression ) */
32357 static tree
32358 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32359 location_t location)
32361 tree t, c;
32363 matching_parens parens;
32364 if (!parens.require_open (parser))
32365 return list;
32367 t = cp_parser_expression (parser);
32369 if (t == error_mark_node
32370 || !parens.require_close (parser))
32371 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32372 /*or_comma=*/false,
32373 /*consume_paren=*/true);
32375 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32376 "num_threads", location);
32378 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32379 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32380 OMP_CLAUSE_CHAIN (c) = list;
32382 return c;
32385 /* OpenMP 4.5:
32386 num_tasks ( expression ) */
32388 static tree
32389 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32390 location_t location)
32392 tree t, c;
32394 matching_parens parens;
32395 if (!parens.require_open (parser))
32396 return list;
32398 t = cp_parser_expression (parser);
32400 if (t == error_mark_node
32401 || !parens.require_close (parser))
32402 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32403 /*or_comma=*/false,
32404 /*consume_paren=*/true);
32406 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32407 "num_tasks", location);
32409 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32410 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32411 OMP_CLAUSE_CHAIN (c) = list;
32413 return c;
32416 /* OpenMP 4.5:
32417 grainsize ( expression ) */
32419 static tree
32420 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32421 location_t location)
32423 tree t, c;
32425 matching_parens parens;
32426 if (!parens.require_open (parser))
32427 return list;
32429 t = cp_parser_expression (parser);
32431 if (t == error_mark_node
32432 || !parens.require_close (parser))
32433 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32434 /*or_comma=*/false,
32435 /*consume_paren=*/true);
32437 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32438 "grainsize", location);
32440 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32441 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32442 OMP_CLAUSE_CHAIN (c) = list;
32444 return c;
32447 /* OpenMP 4.5:
32448 priority ( expression ) */
32450 static tree
32451 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32452 location_t location)
32454 tree t, c;
32456 matching_parens parens;
32457 if (!parens.require_open (parser))
32458 return list;
32460 t = cp_parser_expression (parser);
32462 if (t == error_mark_node
32463 || !parens.require_close (parser))
32464 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32465 /*or_comma=*/false,
32466 /*consume_paren=*/true);
32468 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32469 "priority", location);
32471 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32472 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32473 OMP_CLAUSE_CHAIN (c) = list;
32475 return c;
32478 /* OpenMP 4.5:
32479 hint ( expression ) */
32481 static tree
32482 cp_parser_omp_clause_hint (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_HINT, "hint", location);
32501 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32502 OMP_CLAUSE_HINT_EXPR (c) = t;
32503 OMP_CLAUSE_CHAIN (c) = list;
32505 return c;
32508 /* OpenMP 4.5:
32509 defaultmap ( tofrom : scalar ) */
32511 static tree
32512 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32513 location_t location)
32515 tree c, id;
32516 const char *p;
32518 matching_parens parens;
32519 if (!parens.require_open (parser))
32520 return list;
32522 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32524 cp_parser_error (parser, "expected %<tofrom%>");
32525 goto out_err;
32527 id = cp_lexer_peek_token (parser->lexer)->u.value;
32528 p = IDENTIFIER_POINTER (id);
32529 if (strcmp (p, "tofrom") != 0)
32531 cp_parser_error (parser, "expected %<tofrom%>");
32532 goto out_err;
32534 cp_lexer_consume_token (parser->lexer);
32535 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32536 goto out_err;
32538 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32540 cp_parser_error (parser, "expected %<scalar%>");
32541 goto out_err;
32543 id = cp_lexer_peek_token (parser->lexer)->u.value;
32544 p = IDENTIFIER_POINTER (id);
32545 if (strcmp (p, "scalar") != 0)
32547 cp_parser_error (parser, "expected %<scalar%>");
32548 goto out_err;
32550 cp_lexer_consume_token (parser->lexer);
32551 if (!parens.require_close (parser))
32552 goto out_err;
32554 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32555 location);
32557 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32558 OMP_CLAUSE_CHAIN (c) = list;
32559 return c;
32561 out_err:
32562 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32563 /*or_comma=*/false,
32564 /*consume_paren=*/true);
32565 return list;
32568 /* OpenMP 2.5:
32569 ordered
32571 OpenMP 4.5:
32572 ordered ( constant-expression ) */
32574 static tree
32575 cp_parser_omp_clause_ordered (cp_parser *parser,
32576 tree list, location_t location)
32578 tree c, num = NULL_TREE;
32579 HOST_WIDE_INT n;
32581 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32582 "ordered", location);
32584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32586 matching_parens parens;
32587 parens.consume_open (parser);
32589 num = cp_parser_constant_expression (parser);
32591 if (!parens.require_close (parser))
32592 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32593 /*or_comma=*/false,
32594 /*consume_paren=*/true);
32596 if (num == error_mark_node)
32597 return list;
32598 num = fold_non_dependent_expr (num);
32599 if (!tree_fits_shwi_p (num)
32600 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32601 || (n = tree_to_shwi (num)) <= 0
32602 || (int) n != n)
32604 error_at (location,
32605 "ordered argument needs positive constant integer "
32606 "expression");
32607 return list;
32611 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32612 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32613 OMP_CLAUSE_CHAIN (c) = list;
32614 return c;
32617 /* OpenMP 2.5:
32618 reduction ( reduction-operator : variable-list )
32620 reduction-operator:
32621 One of: + * - & ^ | && ||
32623 OpenMP 3.1:
32625 reduction-operator:
32626 One of: + * - & ^ | && || min max
32628 OpenMP 4.0:
32630 reduction-operator:
32631 One of: + * - & ^ | && ||
32632 id-expression */
32634 static tree
32635 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32637 enum tree_code code = ERROR_MARK;
32638 tree nlist, c, id = NULL_TREE;
32640 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32641 return list;
32643 switch (cp_lexer_peek_token (parser->lexer)->type)
32645 case CPP_PLUS: code = PLUS_EXPR; break;
32646 case CPP_MULT: code = MULT_EXPR; break;
32647 case CPP_MINUS: code = MINUS_EXPR; break;
32648 case CPP_AND: code = BIT_AND_EXPR; break;
32649 case CPP_XOR: code = BIT_XOR_EXPR; break;
32650 case CPP_OR: code = BIT_IOR_EXPR; break;
32651 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32652 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32653 default: break;
32656 if (code != ERROR_MARK)
32657 cp_lexer_consume_token (parser->lexer);
32658 else
32660 bool saved_colon_corrects_to_scope_p;
32661 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32662 parser->colon_corrects_to_scope_p = false;
32663 id = cp_parser_id_expression (parser, /*template_p=*/false,
32664 /*check_dependency_p=*/true,
32665 /*template_p=*/NULL,
32666 /*declarator_p=*/false,
32667 /*optional_p=*/false);
32668 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32669 if (identifier_p (id))
32671 const char *p = IDENTIFIER_POINTER (id);
32673 if (strcmp (p, "min") == 0)
32674 code = MIN_EXPR;
32675 else if (strcmp (p, "max") == 0)
32676 code = MAX_EXPR;
32677 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32678 code = PLUS_EXPR;
32679 else if (id == ovl_op_identifier (false, MULT_EXPR))
32680 code = MULT_EXPR;
32681 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32682 code = MINUS_EXPR;
32683 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32684 code = BIT_AND_EXPR;
32685 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32686 code = BIT_IOR_EXPR;
32687 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32688 code = BIT_XOR_EXPR;
32689 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32690 code = TRUTH_ANDIF_EXPR;
32691 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32692 code = TRUTH_ORIF_EXPR;
32693 id = omp_reduction_id (code, id, NULL_TREE);
32694 tree scope = parser->scope;
32695 if (scope)
32696 id = build_qualified_name (NULL_TREE, scope, id, false);
32697 parser->scope = NULL_TREE;
32698 parser->qualifying_scope = NULL_TREE;
32699 parser->object_scope = NULL_TREE;
32701 else
32703 error ("invalid reduction-identifier");
32704 resync_fail:
32705 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32706 /*or_comma=*/false,
32707 /*consume_paren=*/true);
32708 return list;
32712 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32713 goto resync_fail;
32715 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32716 NULL);
32717 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32719 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32720 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32723 return nlist;
32726 /* OpenMP 2.5:
32727 schedule ( schedule-kind )
32728 schedule ( schedule-kind , expression )
32730 schedule-kind:
32731 static | dynamic | guided | runtime | auto
32733 OpenMP 4.5:
32734 schedule ( schedule-modifier : schedule-kind )
32735 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32737 schedule-modifier:
32738 simd
32739 monotonic
32740 nonmonotonic */
32742 static tree
32743 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32745 tree c, t;
32746 int modifiers = 0, nmodifiers = 0;
32748 matching_parens parens;
32749 if (!parens.require_open (parser))
32750 return list;
32752 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32754 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32756 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32757 const char *p = IDENTIFIER_POINTER (id);
32758 if (strcmp ("simd", p) == 0)
32759 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32760 else if (strcmp ("monotonic", p) == 0)
32761 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32762 else if (strcmp ("nonmonotonic", p) == 0)
32763 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32764 else
32765 break;
32766 cp_lexer_consume_token (parser->lexer);
32767 if (nmodifiers++ == 0
32768 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32769 cp_lexer_consume_token (parser->lexer);
32770 else
32772 cp_parser_require (parser, CPP_COLON, RT_COLON);
32773 break;
32777 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32779 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32780 const char *p = IDENTIFIER_POINTER (id);
32782 switch (p[0])
32784 case 'd':
32785 if (strcmp ("dynamic", p) != 0)
32786 goto invalid_kind;
32787 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32788 break;
32790 case 'g':
32791 if (strcmp ("guided", p) != 0)
32792 goto invalid_kind;
32793 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32794 break;
32796 case 'r':
32797 if (strcmp ("runtime", p) != 0)
32798 goto invalid_kind;
32799 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32800 break;
32802 default:
32803 goto invalid_kind;
32806 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32807 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32808 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32809 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32810 else
32811 goto invalid_kind;
32812 cp_lexer_consume_token (parser->lexer);
32814 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32815 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32816 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32817 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32819 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32820 "specified");
32821 modifiers = 0;
32824 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32826 cp_token *token;
32827 cp_lexer_consume_token (parser->lexer);
32829 token = cp_lexer_peek_token (parser->lexer);
32830 t = cp_parser_assignment_expression (parser);
32832 if (t == error_mark_node)
32833 goto resync_fail;
32834 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32835 error_at (token->location, "schedule %<runtime%> does not take "
32836 "a %<chunk_size%> parameter");
32837 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32838 error_at (token->location, "schedule %<auto%> does not take "
32839 "a %<chunk_size%> parameter");
32840 else
32841 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32843 if (!parens.require_close (parser))
32844 goto resync_fail;
32846 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32847 goto resync_fail;
32849 OMP_CLAUSE_SCHEDULE_KIND (c)
32850 = (enum omp_clause_schedule_kind)
32851 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32853 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32854 OMP_CLAUSE_CHAIN (c) = list;
32855 return c;
32857 invalid_kind:
32858 cp_parser_error (parser, "invalid schedule kind");
32859 resync_fail:
32860 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32861 /*or_comma=*/false,
32862 /*consume_paren=*/true);
32863 return list;
32866 /* OpenMP 3.0:
32867 untied */
32869 static tree
32870 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32871 tree list, location_t location)
32873 tree c;
32875 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32877 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32878 OMP_CLAUSE_CHAIN (c) = list;
32879 return c;
32882 /* OpenMP 4.0:
32883 inbranch
32884 notinbranch */
32886 static tree
32887 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32888 tree list, location_t location)
32890 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32891 tree c = build_omp_clause (location, code);
32892 OMP_CLAUSE_CHAIN (c) = list;
32893 return c;
32896 /* OpenMP 4.0:
32897 parallel
32899 sections
32900 taskgroup */
32902 static tree
32903 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32904 enum omp_clause_code code,
32905 tree list, location_t location)
32907 tree c = build_omp_clause (location, code);
32908 OMP_CLAUSE_CHAIN (c) = list;
32909 return c;
32912 /* OpenMP 4.5:
32913 nogroup */
32915 static tree
32916 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32917 tree list, location_t location)
32919 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32920 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32921 OMP_CLAUSE_CHAIN (c) = list;
32922 return c;
32925 /* OpenMP 4.5:
32926 simd
32927 threads */
32929 static tree
32930 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32931 enum omp_clause_code code,
32932 tree list, location_t location)
32934 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32935 tree c = build_omp_clause (location, code);
32936 OMP_CLAUSE_CHAIN (c) = list;
32937 return c;
32940 /* OpenMP 4.0:
32941 num_teams ( expression ) */
32943 static tree
32944 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32945 location_t location)
32947 tree t, c;
32949 matching_parens parens;
32950 if (!parens.require_open (parser))
32951 return list;
32953 t = cp_parser_expression (parser);
32955 if (t == error_mark_node
32956 || !parens.require_close (parser))
32957 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32958 /*or_comma=*/false,
32959 /*consume_paren=*/true);
32961 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32962 "num_teams", location);
32964 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32965 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32966 OMP_CLAUSE_CHAIN (c) = list;
32968 return c;
32971 /* OpenMP 4.0:
32972 thread_limit ( expression ) */
32974 static tree
32975 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32976 location_t location)
32978 tree t, c;
32980 matching_parens parens;
32981 if (!parens.require_open (parser))
32982 return list;
32984 t = cp_parser_expression (parser);
32986 if (t == error_mark_node
32987 || !parens.require_close (parser))
32988 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32989 /*or_comma=*/false,
32990 /*consume_paren=*/true);
32992 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32993 "thread_limit", location);
32995 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32996 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32997 OMP_CLAUSE_CHAIN (c) = list;
32999 return c;
33002 /* OpenMP 4.0:
33003 aligned ( variable-list )
33004 aligned ( variable-list : constant-expression ) */
33006 static tree
33007 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33009 tree nlist, c, alignment = NULL_TREE;
33010 bool colon;
33012 matching_parens parens;
33013 if (!parens.require_open (parser))
33014 return list;
33016 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33017 &colon);
33019 if (colon)
33021 alignment = cp_parser_constant_expression (parser);
33023 if (!parens.require_close (parser))
33024 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33025 /*or_comma=*/false,
33026 /*consume_paren=*/true);
33028 if (alignment == error_mark_node)
33029 alignment = NULL_TREE;
33032 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33033 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33035 return nlist;
33038 /* OpenMP 4.0:
33039 linear ( variable-list )
33040 linear ( variable-list : expression )
33042 OpenMP 4.5:
33043 linear ( modifier ( variable-list ) )
33044 linear ( modifier ( variable-list ) : expression ) */
33046 static tree
33047 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33048 bool declare_simd)
33050 tree nlist, c, step = integer_one_node;
33051 bool colon;
33052 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33054 matching_parens parens;
33055 if (!parens.require_open (parser))
33056 return list;
33058 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33060 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33061 const char *p = IDENTIFIER_POINTER (id);
33063 if (strcmp ("ref", p) == 0)
33064 kind = OMP_CLAUSE_LINEAR_REF;
33065 else if (strcmp ("val", p) == 0)
33066 kind = OMP_CLAUSE_LINEAR_VAL;
33067 else if (strcmp ("uval", p) == 0)
33068 kind = OMP_CLAUSE_LINEAR_UVAL;
33069 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33070 cp_lexer_consume_token (parser->lexer);
33071 else
33072 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33075 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33076 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33077 &colon);
33078 else
33080 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33081 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33082 if (colon)
33083 cp_parser_require (parser, CPP_COLON, RT_COLON);
33084 else if (!parens.require_close (parser))
33085 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33086 /*or_comma=*/false,
33087 /*consume_paren=*/true);
33090 if (colon)
33092 step = NULL_TREE;
33093 if (declare_simd
33094 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33095 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33097 cp_token *token = cp_lexer_peek_token (parser->lexer);
33098 cp_parser_parse_tentatively (parser);
33099 step = cp_parser_id_expression (parser, /*template_p=*/false,
33100 /*check_dependency_p=*/true,
33101 /*template_p=*/NULL,
33102 /*declarator_p=*/false,
33103 /*optional_p=*/false);
33104 if (step != error_mark_node)
33105 step = cp_parser_lookup_name_simple (parser, step, token->location);
33106 if (step == error_mark_node)
33108 step = NULL_TREE;
33109 cp_parser_abort_tentative_parse (parser);
33111 else if (!cp_parser_parse_definitely (parser))
33112 step = NULL_TREE;
33114 if (!step)
33115 step = cp_parser_expression (parser);
33117 if (!parens.require_close (parser))
33118 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33119 /*or_comma=*/false,
33120 /*consume_paren=*/true);
33122 if (step == error_mark_node)
33123 return list;
33126 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33128 OMP_CLAUSE_LINEAR_STEP (c) = step;
33129 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33132 return nlist;
33135 /* OpenMP 4.0:
33136 safelen ( constant-expression ) */
33138 static tree
33139 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33140 location_t location)
33142 tree t, c;
33144 matching_parens parens;
33145 if (!parens.require_open (parser))
33146 return list;
33148 t = cp_parser_constant_expression (parser);
33150 if (t == error_mark_node
33151 || !parens.require_close (parser))
33152 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33153 /*or_comma=*/false,
33154 /*consume_paren=*/true);
33156 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33158 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33159 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33160 OMP_CLAUSE_CHAIN (c) = list;
33162 return c;
33165 /* OpenMP 4.0:
33166 simdlen ( constant-expression ) */
33168 static tree
33169 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33170 location_t location)
33172 tree t, c;
33174 matching_parens parens;
33175 if (!parens.require_open (parser))
33176 return list;
33178 t = cp_parser_constant_expression (parser);
33180 if (t == error_mark_node
33181 || !parens.require_close (parser))
33182 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33183 /*or_comma=*/false,
33184 /*consume_paren=*/true);
33186 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33188 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33189 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33190 OMP_CLAUSE_CHAIN (c) = list;
33192 return c;
33195 /* OpenMP 4.5:
33196 vec:
33197 identifier [+/- integer]
33198 vec , identifier [+/- integer]
33201 static tree
33202 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33203 tree list)
33205 tree vec = NULL;
33207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33209 cp_parser_error (parser, "expected identifier");
33210 return list;
33213 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33215 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33216 tree t, identifier = cp_parser_identifier (parser);
33217 tree addend = NULL;
33219 if (identifier == error_mark_node)
33220 t = error_mark_node;
33221 else
33223 t = cp_parser_lookup_name_simple
33224 (parser, identifier,
33225 cp_lexer_peek_token (parser->lexer)->location);
33226 if (t == error_mark_node)
33227 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33228 id_loc);
33231 bool neg = false;
33232 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33233 neg = true;
33234 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33236 addend = integer_zero_node;
33237 goto add_to_vector;
33239 cp_lexer_consume_token (parser->lexer);
33241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33243 cp_parser_error (parser, "expected integer");
33244 return list;
33247 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33248 if (TREE_CODE (addend) != INTEGER_CST)
33250 cp_parser_error (parser, "expected integer");
33251 return list;
33253 cp_lexer_consume_token (parser->lexer);
33255 add_to_vector:
33256 if (t != error_mark_node)
33258 vec = tree_cons (addend, t, vec);
33259 if (neg)
33260 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33264 break;
33266 cp_lexer_consume_token (parser->lexer);
33269 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33271 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33272 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33273 OMP_CLAUSE_DECL (u) = nreverse (vec);
33274 OMP_CLAUSE_CHAIN (u) = list;
33275 return u;
33277 return list;
33280 /* OpenMP 4.0:
33281 depend ( depend-kind : variable-list )
33283 depend-kind:
33284 in | out | inout
33286 OpenMP 4.5:
33287 depend ( source )
33289 depend ( sink : vec ) */
33291 static tree
33292 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33294 tree nlist, c;
33295 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33297 matching_parens parens;
33298 if (!parens.require_open (parser))
33299 return list;
33301 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33303 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33304 const char *p = IDENTIFIER_POINTER (id);
33306 if (strcmp ("in", p) == 0)
33307 kind = OMP_CLAUSE_DEPEND_IN;
33308 else if (strcmp ("inout", p) == 0)
33309 kind = OMP_CLAUSE_DEPEND_INOUT;
33310 else if (strcmp ("out", p) == 0)
33311 kind = OMP_CLAUSE_DEPEND_OUT;
33312 else if (strcmp ("source", p) == 0)
33313 kind = OMP_CLAUSE_DEPEND_SOURCE;
33314 else if (strcmp ("sink", p) == 0)
33315 kind = OMP_CLAUSE_DEPEND_SINK;
33316 else
33317 goto invalid_kind;
33319 else
33320 goto invalid_kind;
33322 cp_lexer_consume_token (parser->lexer);
33324 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33326 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33327 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33328 OMP_CLAUSE_DECL (c) = NULL_TREE;
33329 OMP_CLAUSE_CHAIN (c) = list;
33330 if (!parens.require_close (parser))
33331 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33332 /*or_comma=*/false,
33333 /*consume_paren=*/true);
33334 return c;
33337 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33338 goto resync_fail;
33340 if (kind == OMP_CLAUSE_DEPEND_SINK)
33341 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33342 else
33344 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33345 list, NULL);
33347 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33348 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33350 return nlist;
33352 invalid_kind:
33353 cp_parser_error (parser, "invalid depend kind");
33354 resync_fail:
33355 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33356 /*or_comma=*/false,
33357 /*consume_paren=*/true);
33358 return list;
33361 /* OpenMP 4.0:
33362 map ( map-kind : variable-list )
33363 map ( variable-list )
33365 map-kind:
33366 alloc | to | from | tofrom
33368 OpenMP 4.5:
33369 map-kind:
33370 alloc | to | from | tofrom | release | delete
33372 map ( always [,] map-kind: variable-list ) */
33374 static tree
33375 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33377 tree nlist, c;
33378 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33379 bool always = false;
33381 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33382 return list;
33384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33386 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33387 const char *p = IDENTIFIER_POINTER (id);
33389 if (strcmp ("always", p) == 0)
33391 int nth = 2;
33392 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33393 nth++;
33394 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33395 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33396 == RID_DELETE))
33397 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33398 == CPP_COLON))
33400 always = true;
33401 cp_lexer_consume_token (parser->lexer);
33402 if (nth == 3)
33403 cp_lexer_consume_token (parser->lexer);
33408 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33409 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33411 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33412 const char *p = IDENTIFIER_POINTER (id);
33414 if (strcmp ("alloc", p) == 0)
33415 kind = GOMP_MAP_ALLOC;
33416 else if (strcmp ("to", p) == 0)
33417 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33418 else if (strcmp ("from", p) == 0)
33419 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33420 else if (strcmp ("tofrom", p) == 0)
33421 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33422 else if (strcmp ("release", p) == 0)
33423 kind = GOMP_MAP_RELEASE;
33424 else
33426 cp_parser_error (parser, "invalid map kind");
33427 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33428 /*or_comma=*/false,
33429 /*consume_paren=*/true);
33430 return list;
33432 cp_lexer_consume_token (parser->lexer);
33433 cp_lexer_consume_token (parser->lexer);
33435 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33436 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33438 kind = GOMP_MAP_DELETE;
33439 cp_lexer_consume_token (parser->lexer);
33440 cp_lexer_consume_token (parser->lexer);
33443 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33444 NULL);
33446 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33447 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33449 return nlist;
33452 /* OpenMP 4.0:
33453 device ( expression ) */
33455 static tree
33456 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33457 location_t location)
33459 tree t, c;
33461 matching_parens parens;
33462 if (!parens.require_open (parser))
33463 return list;
33465 t = cp_parser_expression (parser);
33467 if (t == error_mark_node
33468 || !parens.require_close (parser))
33469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33470 /*or_comma=*/false,
33471 /*consume_paren=*/true);
33473 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33474 "device", location);
33476 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33477 OMP_CLAUSE_DEVICE_ID (c) = t;
33478 OMP_CLAUSE_CHAIN (c) = list;
33480 return c;
33483 /* OpenMP 4.0:
33484 dist_schedule ( static )
33485 dist_schedule ( static , expression ) */
33487 static tree
33488 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33489 location_t location)
33491 tree c, t;
33493 matching_parens parens;
33494 if (!parens.require_open (parser))
33495 return list;
33497 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33499 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33500 goto invalid_kind;
33501 cp_lexer_consume_token (parser->lexer);
33503 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33505 cp_lexer_consume_token (parser->lexer);
33507 t = cp_parser_assignment_expression (parser);
33509 if (t == error_mark_node)
33510 goto resync_fail;
33511 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33513 if (!parens.require_close (parser))
33514 goto resync_fail;
33516 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33517 goto resync_fail;
33519 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33520 location);
33521 OMP_CLAUSE_CHAIN (c) = list;
33522 return c;
33524 invalid_kind:
33525 cp_parser_error (parser, "invalid dist_schedule kind");
33526 resync_fail:
33527 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33528 /*or_comma=*/false,
33529 /*consume_paren=*/true);
33530 return list;
33533 /* OpenMP 4.0:
33534 proc_bind ( proc-bind-kind )
33536 proc-bind-kind:
33537 master | close | spread */
33539 static tree
33540 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33541 location_t location)
33543 tree c;
33544 enum omp_clause_proc_bind_kind kind;
33546 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33547 return list;
33549 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33551 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33552 const char *p = IDENTIFIER_POINTER (id);
33554 if (strcmp ("master", p) == 0)
33555 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33556 else if (strcmp ("close", p) == 0)
33557 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33558 else if (strcmp ("spread", p) == 0)
33559 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33560 else
33561 goto invalid_kind;
33563 else
33564 goto invalid_kind;
33566 cp_lexer_consume_token (parser->lexer);
33567 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33568 goto resync_fail;
33570 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33571 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33572 location);
33573 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33574 OMP_CLAUSE_CHAIN (c) = list;
33575 return c;
33577 invalid_kind:
33578 cp_parser_error (parser, "invalid depend kind");
33579 resync_fail:
33580 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33581 /*or_comma=*/false,
33582 /*consume_paren=*/true);
33583 return list;
33586 /* OpenACC:
33587 async [( int-expr )] */
33589 static tree
33590 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33592 tree c, t;
33593 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33595 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33597 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33599 matching_parens parens;
33600 parens.consume_open (parser);
33602 t = cp_parser_expression (parser);
33603 if (t == error_mark_node
33604 || !parens.require_close (parser))
33605 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33606 /*or_comma=*/false,
33607 /*consume_paren=*/true);
33610 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33612 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33613 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33614 OMP_CLAUSE_CHAIN (c) = list;
33615 list = c;
33617 return list;
33620 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33621 is a bitmask in MASK. Return the list of clauses found. */
33623 static tree
33624 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33625 const char *where, cp_token *pragma_tok,
33626 bool finish_p = true)
33628 tree clauses = NULL;
33629 bool first = true;
33631 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33633 location_t here;
33634 pragma_omp_clause c_kind;
33635 omp_clause_code code;
33636 const char *c_name;
33637 tree prev = clauses;
33639 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33640 cp_lexer_consume_token (parser->lexer);
33642 here = cp_lexer_peek_token (parser->lexer)->location;
33643 c_kind = cp_parser_omp_clause_name (parser);
33645 switch (c_kind)
33647 case PRAGMA_OACC_CLAUSE_ASYNC:
33648 clauses = cp_parser_oacc_clause_async (parser, clauses);
33649 c_name = "async";
33650 break;
33651 case PRAGMA_OACC_CLAUSE_AUTO:
33652 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33653 clauses, here);
33654 c_name = "auto";
33655 break;
33656 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33657 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33658 c_name = "collapse";
33659 break;
33660 case PRAGMA_OACC_CLAUSE_COPY:
33661 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33662 c_name = "copy";
33663 break;
33664 case PRAGMA_OACC_CLAUSE_COPYIN:
33665 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33666 c_name = "copyin";
33667 break;
33668 case PRAGMA_OACC_CLAUSE_COPYOUT:
33669 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33670 c_name = "copyout";
33671 break;
33672 case PRAGMA_OACC_CLAUSE_CREATE:
33673 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33674 c_name = "create";
33675 break;
33676 case PRAGMA_OACC_CLAUSE_DELETE:
33677 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33678 c_name = "delete";
33679 break;
33680 case PRAGMA_OMP_CLAUSE_DEFAULT:
33681 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33682 c_name = "default";
33683 break;
33684 case PRAGMA_OACC_CLAUSE_DEVICE:
33685 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33686 c_name = "device";
33687 break;
33688 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33689 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33690 c_name = "deviceptr";
33691 break;
33692 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33693 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33694 c_name = "device_resident";
33695 break;
33696 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33697 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33698 clauses);
33699 c_name = "firstprivate";
33700 break;
33701 case PRAGMA_OACC_CLAUSE_GANG:
33702 c_name = "gang";
33703 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33704 c_name, clauses);
33705 break;
33706 case PRAGMA_OACC_CLAUSE_HOST:
33707 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33708 c_name = "host";
33709 break;
33710 case PRAGMA_OACC_CLAUSE_IF:
33711 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33712 c_name = "if";
33713 break;
33714 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33715 clauses = cp_parser_oacc_simple_clause (parser,
33716 OMP_CLAUSE_INDEPENDENT,
33717 clauses, here);
33718 c_name = "independent";
33719 break;
33720 case PRAGMA_OACC_CLAUSE_LINK:
33721 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33722 c_name = "link";
33723 break;
33724 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33725 code = OMP_CLAUSE_NUM_GANGS;
33726 c_name = "num_gangs";
33727 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33728 clauses);
33729 break;
33730 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33731 c_name = "num_workers";
33732 code = OMP_CLAUSE_NUM_WORKERS;
33733 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33734 clauses);
33735 break;
33736 case PRAGMA_OACC_CLAUSE_PRESENT:
33737 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33738 c_name = "present";
33739 break;
33740 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33741 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33742 c_name = "present_or_copy";
33743 break;
33744 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33745 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33746 c_name = "present_or_copyin";
33747 break;
33748 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33749 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33750 c_name = "present_or_copyout";
33751 break;
33752 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33753 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33754 c_name = "present_or_create";
33755 break;
33756 case PRAGMA_OACC_CLAUSE_PRIVATE:
33757 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33758 clauses);
33759 c_name = "private";
33760 break;
33761 case PRAGMA_OACC_CLAUSE_REDUCTION:
33762 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33763 c_name = "reduction";
33764 break;
33765 case PRAGMA_OACC_CLAUSE_SELF:
33766 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33767 c_name = "self";
33768 break;
33769 case PRAGMA_OACC_CLAUSE_SEQ:
33770 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33771 clauses, here);
33772 c_name = "seq";
33773 break;
33774 case PRAGMA_OACC_CLAUSE_TILE:
33775 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33776 c_name = "tile";
33777 break;
33778 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33779 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33780 clauses);
33781 c_name = "use_device";
33782 break;
33783 case PRAGMA_OACC_CLAUSE_VECTOR:
33784 c_name = "vector";
33785 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33786 c_name, clauses);
33787 break;
33788 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33789 c_name = "vector_length";
33790 code = OMP_CLAUSE_VECTOR_LENGTH;
33791 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33792 clauses);
33793 break;
33794 case PRAGMA_OACC_CLAUSE_WAIT:
33795 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33796 c_name = "wait";
33797 break;
33798 case PRAGMA_OACC_CLAUSE_WORKER:
33799 c_name = "worker";
33800 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33801 c_name, clauses);
33802 break;
33803 default:
33804 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33805 goto saw_error;
33808 first = false;
33810 if (((mask >> c_kind) & 1) == 0)
33812 /* Remove the invalid clause(s) from the list to avoid
33813 confusing the rest of the compiler. */
33814 clauses = prev;
33815 error_at (here, "%qs is not valid for %qs", c_name, where);
33819 saw_error:
33820 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33822 if (finish_p)
33823 return finish_omp_clauses (clauses, C_ORT_ACC);
33825 return clauses;
33828 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33829 is a bitmask in MASK. Return the list of clauses found; the result
33830 of clause default goes in *pdefault. */
33832 static tree
33833 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33834 const char *where, cp_token *pragma_tok,
33835 bool finish_p = true)
33837 tree clauses = NULL;
33838 bool first = true;
33839 cp_token *token = NULL;
33841 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33843 pragma_omp_clause c_kind;
33844 const char *c_name;
33845 tree prev = clauses;
33847 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33848 cp_lexer_consume_token (parser->lexer);
33850 token = cp_lexer_peek_token (parser->lexer);
33851 c_kind = cp_parser_omp_clause_name (parser);
33853 switch (c_kind)
33855 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33856 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33857 token->location);
33858 c_name = "collapse";
33859 break;
33860 case PRAGMA_OMP_CLAUSE_COPYIN:
33861 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33862 c_name = "copyin";
33863 break;
33864 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33865 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33866 clauses);
33867 c_name = "copyprivate";
33868 break;
33869 case PRAGMA_OMP_CLAUSE_DEFAULT:
33870 clauses = cp_parser_omp_clause_default (parser, clauses,
33871 token->location, false);
33872 c_name = "default";
33873 break;
33874 case PRAGMA_OMP_CLAUSE_FINAL:
33875 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33876 c_name = "final";
33877 break;
33878 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33879 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33880 clauses);
33881 c_name = "firstprivate";
33882 break;
33883 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33884 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33885 token->location);
33886 c_name = "grainsize";
33887 break;
33888 case PRAGMA_OMP_CLAUSE_HINT:
33889 clauses = cp_parser_omp_clause_hint (parser, clauses,
33890 token->location);
33891 c_name = "hint";
33892 break;
33893 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33894 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33895 token->location);
33896 c_name = "defaultmap";
33897 break;
33898 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33899 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33900 clauses);
33901 c_name = "use_device_ptr";
33902 break;
33903 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33904 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33905 clauses);
33906 c_name = "is_device_ptr";
33907 break;
33908 case PRAGMA_OMP_CLAUSE_IF:
33909 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33910 true);
33911 c_name = "if";
33912 break;
33913 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33914 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33915 clauses);
33916 c_name = "lastprivate";
33917 break;
33918 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33919 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33920 token->location);
33921 c_name = "mergeable";
33922 break;
33923 case PRAGMA_OMP_CLAUSE_NOWAIT:
33924 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33925 c_name = "nowait";
33926 break;
33927 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33928 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33929 token->location);
33930 c_name = "num_tasks";
33931 break;
33932 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33933 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33934 token->location);
33935 c_name = "num_threads";
33936 break;
33937 case PRAGMA_OMP_CLAUSE_ORDERED:
33938 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33939 token->location);
33940 c_name = "ordered";
33941 break;
33942 case PRAGMA_OMP_CLAUSE_PRIORITY:
33943 clauses = cp_parser_omp_clause_priority (parser, clauses,
33944 token->location);
33945 c_name = "priority";
33946 break;
33947 case PRAGMA_OMP_CLAUSE_PRIVATE:
33948 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33949 clauses);
33950 c_name = "private";
33951 break;
33952 case PRAGMA_OMP_CLAUSE_REDUCTION:
33953 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33954 c_name = "reduction";
33955 break;
33956 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33957 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33958 token->location);
33959 c_name = "schedule";
33960 break;
33961 case PRAGMA_OMP_CLAUSE_SHARED:
33962 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33963 clauses);
33964 c_name = "shared";
33965 break;
33966 case PRAGMA_OMP_CLAUSE_UNTIED:
33967 clauses = cp_parser_omp_clause_untied (parser, clauses,
33968 token->location);
33969 c_name = "untied";
33970 break;
33971 case PRAGMA_OMP_CLAUSE_INBRANCH:
33972 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33973 clauses, token->location);
33974 c_name = "inbranch";
33975 break;
33976 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33977 clauses = cp_parser_omp_clause_branch (parser,
33978 OMP_CLAUSE_NOTINBRANCH,
33979 clauses, token->location);
33980 c_name = "notinbranch";
33981 break;
33982 case PRAGMA_OMP_CLAUSE_PARALLEL:
33983 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33984 clauses, token->location);
33985 c_name = "parallel";
33986 if (!first)
33988 clause_not_first:
33989 error_at (token->location, "%qs must be the first clause of %qs",
33990 c_name, where);
33991 clauses = prev;
33993 break;
33994 case PRAGMA_OMP_CLAUSE_FOR:
33995 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33996 clauses, token->location);
33997 c_name = "for";
33998 if (!first)
33999 goto clause_not_first;
34000 break;
34001 case PRAGMA_OMP_CLAUSE_SECTIONS:
34002 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34003 clauses, token->location);
34004 c_name = "sections";
34005 if (!first)
34006 goto clause_not_first;
34007 break;
34008 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34009 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34010 clauses, token->location);
34011 c_name = "taskgroup";
34012 if (!first)
34013 goto clause_not_first;
34014 break;
34015 case PRAGMA_OMP_CLAUSE_LINK:
34016 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34017 c_name = "to";
34018 break;
34019 case PRAGMA_OMP_CLAUSE_TO:
34020 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34021 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34022 clauses);
34023 else
34024 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34025 c_name = "to";
34026 break;
34027 case PRAGMA_OMP_CLAUSE_FROM:
34028 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34029 c_name = "from";
34030 break;
34031 case PRAGMA_OMP_CLAUSE_UNIFORM:
34032 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34033 clauses);
34034 c_name = "uniform";
34035 break;
34036 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34037 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34038 token->location);
34039 c_name = "num_teams";
34040 break;
34041 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34042 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34043 token->location);
34044 c_name = "thread_limit";
34045 break;
34046 case PRAGMA_OMP_CLAUSE_ALIGNED:
34047 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34048 c_name = "aligned";
34049 break;
34050 case PRAGMA_OMP_CLAUSE_LINEAR:
34052 bool declare_simd = false;
34053 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34054 declare_simd = true;
34055 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34057 c_name = "linear";
34058 break;
34059 case PRAGMA_OMP_CLAUSE_DEPEND:
34060 clauses = cp_parser_omp_clause_depend (parser, clauses,
34061 token->location);
34062 c_name = "depend";
34063 break;
34064 case PRAGMA_OMP_CLAUSE_MAP:
34065 clauses = cp_parser_omp_clause_map (parser, clauses);
34066 c_name = "map";
34067 break;
34068 case PRAGMA_OMP_CLAUSE_DEVICE:
34069 clauses = cp_parser_omp_clause_device (parser, clauses,
34070 token->location);
34071 c_name = "device";
34072 break;
34073 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34074 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34075 token->location);
34076 c_name = "dist_schedule";
34077 break;
34078 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34079 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34080 token->location);
34081 c_name = "proc_bind";
34082 break;
34083 case PRAGMA_OMP_CLAUSE_SAFELEN:
34084 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34085 token->location);
34086 c_name = "safelen";
34087 break;
34088 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34089 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34090 token->location);
34091 c_name = "simdlen";
34092 break;
34093 case PRAGMA_OMP_CLAUSE_NOGROUP:
34094 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34095 token->location);
34096 c_name = "nogroup";
34097 break;
34098 case PRAGMA_OMP_CLAUSE_THREADS:
34099 clauses
34100 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34101 clauses, token->location);
34102 c_name = "threads";
34103 break;
34104 case PRAGMA_OMP_CLAUSE_SIMD:
34105 clauses
34106 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34107 clauses, token->location);
34108 c_name = "simd";
34109 break;
34110 default:
34111 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34112 goto saw_error;
34115 first = false;
34117 if (((mask >> c_kind) & 1) == 0)
34119 /* Remove the invalid clause(s) from the list to avoid
34120 confusing the rest of the compiler. */
34121 clauses = prev;
34122 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34125 saw_error:
34126 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34127 if (finish_p)
34129 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34130 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34131 else
34132 return finish_omp_clauses (clauses, C_ORT_OMP);
34134 return clauses;
34137 /* OpenMP 2.5:
34138 structured-block:
34139 statement
34141 In practice, we're also interested in adding the statement to an
34142 outer node. So it is convenient if we work around the fact that
34143 cp_parser_statement calls add_stmt. */
34145 static unsigned
34146 cp_parser_begin_omp_structured_block (cp_parser *parser)
34148 unsigned save = parser->in_statement;
34150 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34151 This preserves the "not within loop or switch" style error messages
34152 for nonsense cases like
34153 void foo() {
34154 #pragma omp single
34155 break;
34158 if (parser->in_statement)
34159 parser->in_statement = IN_OMP_BLOCK;
34161 return save;
34164 static void
34165 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34167 parser->in_statement = save;
34170 static tree
34171 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34173 tree stmt = begin_omp_structured_block ();
34174 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34176 cp_parser_statement (parser, NULL_TREE, false, if_p);
34178 cp_parser_end_omp_structured_block (parser, save);
34179 return finish_omp_structured_block (stmt);
34182 /* OpenMP 2.5:
34183 # pragma omp atomic new-line
34184 expression-stmt
34186 expression-stmt:
34187 x binop= expr | x++ | ++x | x-- | --x
34188 binop:
34189 +, *, -, /, &, ^, |, <<, >>
34191 where x is an lvalue expression with scalar type.
34193 OpenMP 3.1:
34194 # pragma omp atomic new-line
34195 update-stmt
34197 # pragma omp atomic read new-line
34198 read-stmt
34200 # pragma omp atomic write new-line
34201 write-stmt
34203 # pragma omp atomic update new-line
34204 update-stmt
34206 # pragma omp atomic capture new-line
34207 capture-stmt
34209 # pragma omp atomic capture new-line
34210 capture-block
34212 read-stmt:
34213 v = x
34214 write-stmt:
34215 x = expr
34216 update-stmt:
34217 expression-stmt | x = x binop expr
34218 capture-stmt:
34219 v = expression-stmt
34220 capture-block:
34221 { v = x; update-stmt; } | { update-stmt; v = x; }
34223 OpenMP 4.0:
34224 update-stmt:
34225 expression-stmt | x = x binop expr | x = expr binop x
34226 capture-stmt:
34227 v = update-stmt
34228 capture-block:
34229 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34231 where x and v are lvalue expressions with scalar type. */
34233 static void
34234 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34236 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34237 tree rhs1 = NULL_TREE, orig_lhs;
34238 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34239 bool structured_block = false;
34240 bool seq_cst = false;
34242 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34244 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34245 const char *p = IDENTIFIER_POINTER (id);
34247 if (!strcmp (p, "seq_cst"))
34249 seq_cst = true;
34250 cp_lexer_consume_token (parser->lexer);
34251 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34252 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34253 cp_lexer_consume_token (parser->lexer);
34256 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34258 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34259 const char *p = IDENTIFIER_POINTER (id);
34261 if (!strcmp (p, "read"))
34262 code = OMP_ATOMIC_READ;
34263 else if (!strcmp (p, "write"))
34264 code = NOP_EXPR;
34265 else if (!strcmp (p, "update"))
34266 code = OMP_ATOMIC;
34267 else if (!strcmp (p, "capture"))
34268 code = OMP_ATOMIC_CAPTURE_NEW;
34269 else
34270 p = NULL;
34271 if (p)
34272 cp_lexer_consume_token (parser->lexer);
34274 if (!seq_cst)
34276 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34277 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34278 cp_lexer_consume_token (parser->lexer);
34280 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34282 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34283 const char *p = IDENTIFIER_POINTER (id);
34285 if (!strcmp (p, "seq_cst"))
34287 seq_cst = true;
34288 cp_lexer_consume_token (parser->lexer);
34292 cp_parser_require_pragma_eol (parser, pragma_tok);
34294 switch (code)
34296 case OMP_ATOMIC_READ:
34297 case NOP_EXPR: /* atomic write */
34298 v = cp_parser_unary_expression (parser);
34299 if (v == error_mark_node)
34300 goto saw_error;
34301 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34302 goto saw_error;
34303 if (code == NOP_EXPR)
34304 lhs = cp_parser_expression (parser);
34305 else
34306 lhs = cp_parser_unary_expression (parser);
34307 if (lhs == error_mark_node)
34308 goto saw_error;
34309 if (code == NOP_EXPR)
34311 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34312 opcode. */
34313 code = OMP_ATOMIC;
34314 rhs = lhs;
34315 lhs = v;
34316 v = NULL_TREE;
34318 goto done;
34319 case OMP_ATOMIC_CAPTURE_NEW:
34320 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34322 cp_lexer_consume_token (parser->lexer);
34323 structured_block = true;
34325 else
34327 v = cp_parser_unary_expression (parser);
34328 if (v == error_mark_node)
34329 goto saw_error;
34330 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34331 goto saw_error;
34333 default:
34334 break;
34337 restart:
34338 lhs = cp_parser_unary_expression (parser);
34339 orig_lhs = lhs;
34340 switch (TREE_CODE (lhs))
34342 case ERROR_MARK:
34343 goto saw_error;
34345 case POSTINCREMENT_EXPR:
34346 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34347 code = OMP_ATOMIC_CAPTURE_OLD;
34348 /* FALLTHROUGH */
34349 case PREINCREMENT_EXPR:
34350 lhs = TREE_OPERAND (lhs, 0);
34351 opcode = PLUS_EXPR;
34352 rhs = integer_one_node;
34353 break;
34355 case POSTDECREMENT_EXPR:
34356 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34357 code = OMP_ATOMIC_CAPTURE_OLD;
34358 /* FALLTHROUGH */
34359 case PREDECREMENT_EXPR:
34360 lhs = TREE_OPERAND (lhs, 0);
34361 opcode = MINUS_EXPR;
34362 rhs = integer_one_node;
34363 break;
34365 case COMPOUND_EXPR:
34366 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34367 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34368 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34369 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34370 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34371 (TREE_OPERAND (lhs, 1), 0), 0)))
34372 == BOOLEAN_TYPE)
34373 /* Undo effects of boolean_increment for post {in,de}crement. */
34374 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34375 /* FALLTHRU */
34376 case MODIFY_EXPR:
34377 if (TREE_CODE (lhs) == MODIFY_EXPR
34378 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34380 /* Undo effects of boolean_increment. */
34381 if (integer_onep (TREE_OPERAND (lhs, 1)))
34383 /* This is pre or post increment. */
34384 rhs = TREE_OPERAND (lhs, 1);
34385 lhs = TREE_OPERAND (lhs, 0);
34386 opcode = NOP_EXPR;
34387 if (code == OMP_ATOMIC_CAPTURE_NEW
34388 && !structured_block
34389 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34390 code = OMP_ATOMIC_CAPTURE_OLD;
34391 break;
34394 /* FALLTHRU */
34395 default:
34396 switch (cp_lexer_peek_token (parser->lexer)->type)
34398 case CPP_MULT_EQ:
34399 opcode = MULT_EXPR;
34400 break;
34401 case CPP_DIV_EQ:
34402 opcode = TRUNC_DIV_EXPR;
34403 break;
34404 case CPP_PLUS_EQ:
34405 opcode = PLUS_EXPR;
34406 break;
34407 case CPP_MINUS_EQ:
34408 opcode = MINUS_EXPR;
34409 break;
34410 case CPP_LSHIFT_EQ:
34411 opcode = LSHIFT_EXPR;
34412 break;
34413 case CPP_RSHIFT_EQ:
34414 opcode = RSHIFT_EXPR;
34415 break;
34416 case CPP_AND_EQ:
34417 opcode = BIT_AND_EXPR;
34418 break;
34419 case CPP_OR_EQ:
34420 opcode = BIT_IOR_EXPR;
34421 break;
34422 case CPP_XOR_EQ:
34423 opcode = BIT_XOR_EXPR;
34424 break;
34425 case CPP_EQ:
34426 enum cp_parser_prec oprec;
34427 cp_token *token;
34428 cp_lexer_consume_token (parser->lexer);
34429 cp_parser_parse_tentatively (parser);
34430 rhs1 = cp_parser_simple_cast_expression (parser);
34431 if (rhs1 == error_mark_node)
34433 cp_parser_abort_tentative_parse (parser);
34434 cp_parser_simple_cast_expression (parser);
34435 goto saw_error;
34437 token = cp_lexer_peek_token (parser->lexer);
34438 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34440 cp_parser_abort_tentative_parse (parser);
34441 cp_parser_parse_tentatively (parser);
34442 rhs = cp_parser_binary_expression (parser, false, true,
34443 PREC_NOT_OPERATOR, NULL);
34444 if (rhs == error_mark_node)
34446 cp_parser_abort_tentative_parse (parser);
34447 cp_parser_binary_expression (parser, false, true,
34448 PREC_NOT_OPERATOR, NULL);
34449 goto saw_error;
34451 switch (TREE_CODE (rhs))
34453 case MULT_EXPR:
34454 case TRUNC_DIV_EXPR:
34455 case RDIV_EXPR:
34456 case PLUS_EXPR:
34457 case MINUS_EXPR:
34458 case LSHIFT_EXPR:
34459 case RSHIFT_EXPR:
34460 case BIT_AND_EXPR:
34461 case BIT_IOR_EXPR:
34462 case BIT_XOR_EXPR:
34463 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34465 if (cp_parser_parse_definitely (parser))
34467 opcode = TREE_CODE (rhs);
34468 rhs1 = TREE_OPERAND (rhs, 0);
34469 rhs = TREE_OPERAND (rhs, 1);
34470 goto stmt_done;
34472 else
34473 goto saw_error;
34475 break;
34476 default:
34477 break;
34479 cp_parser_abort_tentative_parse (parser);
34480 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34482 rhs = cp_parser_expression (parser);
34483 if (rhs == error_mark_node)
34484 goto saw_error;
34485 opcode = NOP_EXPR;
34486 rhs1 = NULL_TREE;
34487 goto stmt_done;
34489 cp_parser_error (parser,
34490 "invalid form of %<#pragma omp atomic%>");
34491 goto saw_error;
34493 if (!cp_parser_parse_definitely (parser))
34494 goto saw_error;
34495 switch (token->type)
34497 case CPP_SEMICOLON:
34498 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34500 code = OMP_ATOMIC_CAPTURE_OLD;
34501 v = lhs;
34502 lhs = NULL_TREE;
34503 lhs1 = rhs1;
34504 rhs1 = NULL_TREE;
34505 cp_lexer_consume_token (parser->lexer);
34506 goto restart;
34508 else if (structured_block)
34510 opcode = NOP_EXPR;
34511 rhs = rhs1;
34512 rhs1 = NULL_TREE;
34513 goto stmt_done;
34515 cp_parser_error (parser,
34516 "invalid form of %<#pragma omp atomic%>");
34517 goto saw_error;
34518 case CPP_MULT:
34519 opcode = MULT_EXPR;
34520 break;
34521 case CPP_DIV:
34522 opcode = TRUNC_DIV_EXPR;
34523 break;
34524 case CPP_PLUS:
34525 opcode = PLUS_EXPR;
34526 break;
34527 case CPP_MINUS:
34528 opcode = MINUS_EXPR;
34529 break;
34530 case CPP_LSHIFT:
34531 opcode = LSHIFT_EXPR;
34532 break;
34533 case CPP_RSHIFT:
34534 opcode = RSHIFT_EXPR;
34535 break;
34536 case CPP_AND:
34537 opcode = BIT_AND_EXPR;
34538 break;
34539 case CPP_OR:
34540 opcode = BIT_IOR_EXPR;
34541 break;
34542 case CPP_XOR:
34543 opcode = BIT_XOR_EXPR;
34544 break;
34545 default:
34546 cp_parser_error (parser,
34547 "invalid operator for %<#pragma omp atomic%>");
34548 goto saw_error;
34550 oprec = TOKEN_PRECEDENCE (token);
34551 gcc_assert (oprec != PREC_NOT_OPERATOR);
34552 if (commutative_tree_code (opcode))
34553 oprec = (enum cp_parser_prec) (oprec - 1);
34554 cp_lexer_consume_token (parser->lexer);
34555 rhs = cp_parser_binary_expression (parser, false, false,
34556 oprec, NULL);
34557 if (rhs == error_mark_node)
34558 goto saw_error;
34559 goto stmt_done;
34560 /* FALLTHROUGH */
34561 default:
34562 cp_parser_error (parser,
34563 "invalid operator for %<#pragma omp atomic%>");
34564 goto saw_error;
34566 cp_lexer_consume_token (parser->lexer);
34568 rhs = cp_parser_expression (parser);
34569 if (rhs == error_mark_node)
34570 goto saw_error;
34571 break;
34573 stmt_done:
34574 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34576 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34577 goto saw_error;
34578 v = cp_parser_unary_expression (parser);
34579 if (v == error_mark_node)
34580 goto saw_error;
34581 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34582 goto saw_error;
34583 lhs1 = cp_parser_unary_expression (parser);
34584 if (lhs1 == error_mark_node)
34585 goto saw_error;
34587 if (structured_block)
34589 cp_parser_consume_semicolon_at_end_of_statement (parser);
34590 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34592 done:
34593 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34594 if (!structured_block)
34595 cp_parser_consume_semicolon_at_end_of_statement (parser);
34596 return;
34598 saw_error:
34599 cp_parser_skip_to_end_of_block_or_statement (parser);
34600 if (structured_block)
34602 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34603 cp_lexer_consume_token (parser->lexer);
34604 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34606 cp_parser_skip_to_end_of_block_or_statement (parser);
34607 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34608 cp_lexer_consume_token (parser->lexer);
34614 /* OpenMP 2.5:
34615 # pragma omp barrier new-line */
34617 static void
34618 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34620 cp_parser_require_pragma_eol (parser, pragma_tok);
34621 finish_omp_barrier ();
34624 /* OpenMP 2.5:
34625 # pragma omp critical [(name)] new-line
34626 structured-block
34628 OpenMP 4.5:
34629 # pragma omp critical [(name) [hint(expression)]] new-line
34630 structured-block */
34632 #define OMP_CRITICAL_CLAUSE_MASK \
34633 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34635 static tree
34636 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34638 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34642 matching_parens parens;
34643 parens.consume_open (parser);
34645 name = cp_parser_identifier (parser);
34647 if (name == error_mark_node
34648 || !parens.require_close (parser))
34649 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34650 /*or_comma=*/false,
34651 /*consume_paren=*/true);
34652 if (name == error_mark_node)
34653 name = NULL;
34655 clauses = cp_parser_omp_all_clauses (parser,
34656 OMP_CRITICAL_CLAUSE_MASK,
34657 "#pragma omp critical", pragma_tok);
34659 else
34660 cp_parser_require_pragma_eol (parser, pragma_tok);
34662 stmt = cp_parser_omp_structured_block (parser, if_p);
34663 return c_finish_omp_critical (input_location, stmt, name, clauses);
34666 /* OpenMP 2.5:
34667 # pragma omp flush flush-vars[opt] new-line
34669 flush-vars:
34670 ( variable-list ) */
34672 static void
34673 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34676 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34677 cp_parser_require_pragma_eol (parser, pragma_tok);
34679 finish_omp_flush ();
34682 /* Helper function, to parse omp for increment expression. */
34684 static tree
34685 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34687 tree cond = cp_parser_binary_expression (parser, false, true,
34688 PREC_NOT_OPERATOR, NULL);
34689 if (cond == error_mark_node
34690 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34692 cp_parser_skip_to_end_of_statement (parser);
34693 return error_mark_node;
34696 switch (TREE_CODE (cond))
34698 case GT_EXPR:
34699 case GE_EXPR:
34700 case LT_EXPR:
34701 case LE_EXPR:
34702 break;
34703 case NE_EXPR:
34704 /* Fall through: OpenMP disallows NE_EXPR. */
34705 gcc_fallthrough ();
34706 default:
34707 return error_mark_node;
34710 /* If decl is an iterator, preserve LHS and RHS of the relational
34711 expr until finish_omp_for. */
34712 if (decl
34713 && (type_dependent_expression_p (decl)
34714 || CLASS_TYPE_P (TREE_TYPE (decl))))
34715 return cond;
34717 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34718 TREE_CODE (cond),
34719 TREE_OPERAND (cond, 0), ERROR_MARK,
34720 TREE_OPERAND (cond, 1), ERROR_MARK,
34721 /*overload=*/NULL, tf_warning_or_error);
34724 /* Helper function, to parse omp for increment expression. */
34726 static tree
34727 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34729 cp_token *token = cp_lexer_peek_token (parser->lexer);
34730 enum tree_code op;
34731 tree lhs, rhs;
34732 cp_id_kind idk;
34733 bool decl_first;
34735 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34737 op = (token->type == CPP_PLUS_PLUS
34738 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34739 cp_lexer_consume_token (parser->lexer);
34740 lhs = cp_parser_simple_cast_expression (parser);
34741 if (lhs != decl
34742 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34743 return error_mark_node;
34744 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34747 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34748 if (lhs != decl
34749 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34750 return error_mark_node;
34752 token = cp_lexer_peek_token (parser->lexer);
34753 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34755 op = (token->type == CPP_PLUS_PLUS
34756 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34757 cp_lexer_consume_token (parser->lexer);
34758 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34761 op = cp_parser_assignment_operator_opt (parser);
34762 if (op == ERROR_MARK)
34763 return error_mark_node;
34765 if (op != NOP_EXPR)
34767 rhs = cp_parser_assignment_expression (parser);
34768 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34769 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34772 lhs = cp_parser_binary_expression (parser, false, false,
34773 PREC_ADDITIVE_EXPRESSION, NULL);
34774 token = cp_lexer_peek_token (parser->lexer);
34775 decl_first = (lhs == decl
34776 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34777 if (decl_first)
34778 lhs = NULL_TREE;
34779 if (token->type != CPP_PLUS
34780 && token->type != CPP_MINUS)
34781 return error_mark_node;
34785 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34786 cp_lexer_consume_token (parser->lexer);
34787 rhs = cp_parser_binary_expression (parser, false, false,
34788 PREC_ADDITIVE_EXPRESSION, NULL);
34789 token = cp_lexer_peek_token (parser->lexer);
34790 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34792 if (lhs == NULL_TREE)
34794 if (op == PLUS_EXPR)
34795 lhs = rhs;
34796 else
34797 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34798 tf_warning_or_error);
34800 else
34801 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34802 ERROR_MARK, NULL, tf_warning_or_error);
34805 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34807 if (!decl_first)
34809 if ((rhs != decl
34810 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34811 || op == MINUS_EXPR)
34812 return error_mark_node;
34813 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34815 else
34816 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34818 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34821 /* Parse the initialization statement of an OpenMP for loop.
34823 Return true if the resulting construct should have an
34824 OMP_CLAUSE_PRIVATE added to it. */
34826 static tree
34827 cp_parser_omp_for_loop_init (cp_parser *parser,
34828 tree &this_pre_body,
34829 vec<tree, va_gc> *for_block,
34830 tree &init,
34831 tree &orig_init,
34832 tree &decl,
34833 tree &real_decl)
34835 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34836 return NULL_TREE;
34838 tree add_private_clause = NULL_TREE;
34840 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34842 init-expr:
34843 var = lb
34844 integer-type var = lb
34845 random-access-iterator-type var = lb
34846 pointer-type var = lb
34848 cp_decl_specifier_seq type_specifiers;
34850 /* First, try to parse as an initialized declaration. See
34851 cp_parser_condition, from whence the bulk of this is copied. */
34853 cp_parser_parse_tentatively (parser);
34854 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34855 /*is_trailing_return=*/false,
34856 &type_specifiers);
34857 if (cp_parser_parse_definitely (parser))
34859 /* If parsing a type specifier seq succeeded, then this
34860 MUST be a initialized declaration. */
34861 tree asm_specification, attributes;
34862 cp_declarator *declarator;
34864 declarator = cp_parser_declarator (parser,
34865 CP_PARSER_DECLARATOR_NAMED,
34866 /*ctor_dtor_or_conv_p=*/NULL,
34867 /*parenthesized_p=*/NULL,
34868 /*member_p=*/false,
34869 /*friend_p=*/false);
34870 attributes = cp_parser_attributes_opt (parser);
34871 asm_specification = cp_parser_asm_specification_opt (parser);
34873 if (declarator == cp_error_declarator)
34874 cp_parser_skip_to_end_of_statement (parser);
34876 else
34878 tree pushed_scope, auto_node;
34880 decl = start_decl (declarator, &type_specifiers,
34881 SD_INITIALIZED, attributes,
34882 /*prefix_attributes=*/NULL_TREE,
34883 &pushed_scope);
34885 auto_node = type_uses_auto (TREE_TYPE (decl));
34886 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34888 if (cp_lexer_next_token_is (parser->lexer,
34889 CPP_OPEN_PAREN))
34890 error ("parenthesized initialization is not allowed in "
34891 "OpenMP %<for%> loop");
34892 else
34893 /* Trigger an error. */
34894 cp_parser_require (parser, CPP_EQ, RT_EQ);
34896 init = error_mark_node;
34897 cp_parser_skip_to_end_of_statement (parser);
34899 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34900 || type_dependent_expression_p (decl)
34901 || auto_node)
34903 bool is_direct_init, is_non_constant_init;
34905 init = cp_parser_initializer (parser,
34906 &is_direct_init,
34907 &is_non_constant_init);
34909 if (auto_node)
34911 TREE_TYPE (decl)
34912 = do_auto_deduction (TREE_TYPE (decl), init,
34913 auto_node);
34915 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34916 && !type_dependent_expression_p (decl))
34917 goto non_class;
34920 cp_finish_decl (decl, init, !is_non_constant_init,
34921 asm_specification,
34922 LOOKUP_ONLYCONVERTING);
34923 orig_init = init;
34924 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34926 vec_safe_push (for_block, this_pre_body);
34927 init = NULL_TREE;
34929 else
34931 init = pop_stmt_list (this_pre_body);
34932 if (init && TREE_CODE (init) == STATEMENT_LIST)
34934 tree_stmt_iterator i = tsi_start (init);
34935 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34936 while (!tsi_end_p (i))
34938 tree t = tsi_stmt (i);
34939 if (TREE_CODE (t) == DECL_EXPR
34940 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34942 tsi_delink (&i);
34943 vec_safe_push (for_block, t);
34944 continue;
34946 break;
34948 if (tsi_one_before_end_p (i))
34950 tree t = tsi_stmt (i);
34951 tsi_delink (&i);
34952 free_stmt_list (init);
34953 init = t;
34957 this_pre_body = NULL_TREE;
34959 else
34961 /* Consume '='. */
34962 cp_lexer_consume_token (parser->lexer);
34963 init = cp_parser_assignment_expression (parser);
34965 non_class:
34966 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34967 init = error_mark_node;
34968 else
34969 cp_finish_decl (decl, NULL_TREE,
34970 /*init_const_expr_p=*/false,
34971 asm_specification,
34972 LOOKUP_ONLYCONVERTING);
34975 if (pushed_scope)
34976 pop_scope (pushed_scope);
34979 else
34981 cp_id_kind idk;
34982 /* If parsing a type specifier sequence failed, then
34983 this MUST be a simple expression. */
34984 cp_parser_parse_tentatively (parser);
34985 decl = cp_parser_primary_expression (parser, false, false,
34986 false, &idk);
34987 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34988 if (!cp_parser_error_occurred (parser)
34989 && decl
34990 && (TREE_CODE (decl) == COMPONENT_REF
34991 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34993 cp_parser_abort_tentative_parse (parser);
34994 cp_parser_parse_tentatively (parser);
34995 cp_token *token = cp_lexer_peek_token (parser->lexer);
34996 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34997 /*check_dependency_p=*/true,
34998 /*template_p=*/NULL,
34999 /*declarator_p=*/false,
35000 /*optional_p=*/false);
35001 if (name != error_mark_node
35002 && last_tok == cp_lexer_peek_token (parser->lexer))
35004 decl = cp_parser_lookup_name_simple (parser, name,
35005 token->location);
35006 if (TREE_CODE (decl) == FIELD_DECL)
35007 add_private_clause = omp_privatize_field (decl, false);
35009 cp_parser_abort_tentative_parse (parser);
35010 cp_parser_parse_tentatively (parser);
35011 decl = cp_parser_primary_expression (parser, false, false,
35012 false, &idk);
35014 if (!cp_parser_error_occurred (parser)
35015 && decl
35016 && DECL_P (decl)
35017 && CLASS_TYPE_P (TREE_TYPE (decl)))
35019 tree rhs;
35021 cp_parser_parse_definitely (parser);
35022 cp_parser_require (parser, CPP_EQ, RT_EQ);
35023 rhs = cp_parser_assignment_expression (parser);
35024 orig_init = rhs;
35025 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35026 decl, NOP_EXPR,
35027 rhs,
35028 tf_warning_or_error));
35029 if (!add_private_clause)
35030 add_private_clause = decl;
35032 else
35034 decl = NULL;
35035 cp_parser_abort_tentative_parse (parser);
35036 init = cp_parser_expression (parser);
35037 if (init)
35039 if (TREE_CODE (init) == MODIFY_EXPR
35040 || TREE_CODE (init) == MODOP_EXPR)
35041 real_decl = TREE_OPERAND (init, 0);
35045 return add_private_clause;
35048 /* Parse the restricted form of the for statement allowed by OpenMP. */
35050 static tree
35051 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35052 tree *cclauses, bool *if_p)
35054 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35055 tree real_decl, initv, condv, incrv, declv;
35056 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35057 location_t loc_first;
35058 bool collapse_err = false;
35059 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35060 vec<tree, va_gc> *for_block = make_tree_vector ();
35061 auto_vec<tree, 4> orig_inits;
35062 bool tiling = false;
35064 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35065 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35066 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35067 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35069 tiling = true;
35070 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35072 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35073 && OMP_CLAUSE_ORDERED_EXPR (cl))
35075 ordered_cl = cl;
35076 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35079 if (ordered && ordered < collapse)
35081 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35082 "%<ordered%> clause parameter is less than %<collapse%>");
35083 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35084 = build_int_cst (NULL_TREE, collapse);
35085 ordered = collapse;
35087 if (ordered)
35089 for (tree *pc = &clauses; *pc; )
35090 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35092 error_at (OMP_CLAUSE_LOCATION (*pc),
35093 "%<linear%> clause may not be specified together "
35094 "with %<ordered%> clause with a parameter");
35095 *pc = OMP_CLAUSE_CHAIN (*pc);
35097 else
35098 pc = &OMP_CLAUSE_CHAIN (*pc);
35101 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35102 count = ordered ? ordered : collapse;
35104 declv = make_tree_vec (count);
35105 initv = make_tree_vec (count);
35106 condv = make_tree_vec (count);
35107 incrv = make_tree_vec (count);
35109 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35111 for (i = 0; i < count; i++)
35113 int bracecount = 0;
35114 tree add_private_clause = NULL_TREE;
35115 location_t loc;
35117 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35119 if (!collapse_err)
35120 cp_parser_error (parser, "for statement expected");
35121 return NULL;
35123 loc = cp_lexer_consume_token (parser->lexer)->location;
35125 matching_parens parens;
35126 if (!parens.require_open (parser))
35127 return NULL;
35129 init = orig_init = decl = real_decl = NULL;
35130 this_pre_body = push_stmt_list ();
35132 add_private_clause
35133 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35134 init, orig_init, decl, real_decl);
35136 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35137 if (this_pre_body)
35139 this_pre_body = pop_stmt_list (this_pre_body);
35140 if (pre_body)
35142 tree t = pre_body;
35143 pre_body = push_stmt_list ();
35144 add_stmt (t);
35145 add_stmt (this_pre_body);
35146 pre_body = pop_stmt_list (pre_body);
35148 else
35149 pre_body = this_pre_body;
35152 if (decl)
35153 real_decl = decl;
35154 if (cclauses != NULL
35155 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35156 && real_decl != NULL_TREE)
35158 tree *c;
35159 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35160 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35161 && OMP_CLAUSE_DECL (*c) == real_decl)
35163 error_at (loc, "iteration variable %qD"
35164 " should not be firstprivate", real_decl);
35165 *c = OMP_CLAUSE_CHAIN (*c);
35167 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35168 && OMP_CLAUSE_DECL (*c) == real_decl)
35170 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35171 tree l = *c;
35172 *c = OMP_CLAUSE_CHAIN (*c);
35173 if (code == OMP_SIMD)
35175 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35176 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35178 else
35180 OMP_CLAUSE_CHAIN (l) = clauses;
35181 clauses = l;
35183 add_private_clause = NULL_TREE;
35185 else
35187 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35188 && OMP_CLAUSE_DECL (*c) == real_decl)
35189 add_private_clause = NULL_TREE;
35190 c = &OMP_CLAUSE_CHAIN (*c);
35194 if (add_private_clause)
35196 tree c;
35197 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35199 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35200 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35201 && OMP_CLAUSE_DECL (c) == decl)
35202 break;
35203 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35204 && OMP_CLAUSE_DECL (c) == decl)
35205 error_at (loc, "iteration variable %qD "
35206 "should not be firstprivate",
35207 decl);
35208 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35209 && OMP_CLAUSE_DECL (c) == decl)
35210 error_at (loc, "iteration variable %qD should not be reduction",
35211 decl);
35213 if (c == NULL)
35215 if (code != OMP_SIMD)
35216 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35217 else if (collapse == 1)
35218 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35219 else
35220 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35221 OMP_CLAUSE_DECL (c) = add_private_clause;
35222 c = finish_omp_clauses (c, C_ORT_OMP);
35223 if (c)
35225 OMP_CLAUSE_CHAIN (c) = clauses;
35226 clauses = c;
35227 /* For linear, signal that we need to fill up
35228 the so far unknown linear step. */
35229 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35230 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35235 cond = NULL;
35236 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35237 cond = cp_parser_omp_for_cond (parser, decl);
35238 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35240 incr = NULL;
35241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35243 /* If decl is an iterator, preserve the operator on decl
35244 until finish_omp_for. */
35245 if (real_decl
35246 && ((processing_template_decl
35247 && (TREE_TYPE (real_decl) == NULL_TREE
35248 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35249 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35250 incr = cp_parser_omp_for_incr (parser, real_decl);
35251 else
35252 incr = cp_parser_expression (parser);
35253 if (!EXPR_HAS_LOCATION (incr))
35254 protected_set_expr_location (incr, input_location);
35257 if (!parens.require_close (parser))
35258 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35259 /*or_comma=*/false,
35260 /*consume_paren=*/true);
35262 TREE_VEC_ELT (declv, i) = decl;
35263 TREE_VEC_ELT (initv, i) = init;
35264 TREE_VEC_ELT (condv, i) = cond;
35265 TREE_VEC_ELT (incrv, i) = incr;
35266 if (orig_init)
35268 orig_inits.safe_grow_cleared (i + 1);
35269 orig_inits[i] = orig_init;
35272 if (i == count - 1)
35273 break;
35275 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35276 in between the collapsed for loops to be still considered perfectly
35277 nested. Hopefully the final version clarifies this.
35278 For now handle (multiple) {'s and empty statements. */
35279 cp_parser_parse_tentatively (parser);
35280 for (;;)
35282 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35283 break;
35284 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35286 cp_lexer_consume_token (parser->lexer);
35287 bracecount++;
35289 else if (bracecount
35290 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35291 cp_lexer_consume_token (parser->lexer);
35292 else
35294 loc = cp_lexer_peek_token (parser->lexer)->location;
35295 error_at (loc, "not enough for loops to collapse");
35296 collapse_err = true;
35297 cp_parser_abort_tentative_parse (parser);
35298 declv = NULL_TREE;
35299 break;
35303 if (declv)
35305 cp_parser_parse_definitely (parser);
35306 nbraces += bracecount;
35310 if (nbraces)
35311 if_p = NULL;
35313 /* Note that we saved the original contents of this flag when we entered
35314 the structured block, and so we don't need to re-save it here. */
35315 parser->in_statement = IN_OMP_FOR;
35317 /* Note that the grammar doesn't call for a structured block here,
35318 though the loop as a whole is a structured block. */
35319 body = push_stmt_list ();
35320 cp_parser_statement (parser, NULL_TREE, false, if_p);
35321 body = pop_stmt_list (body);
35323 if (declv == NULL_TREE)
35324 ret = NULL_TREE;
35325 else
35326 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35327 body, pre_body, &orig_inits, clauses);
35329 while (nbraces)
35331 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35333 cp_lexer_consume_token (parser->lexer);
35334 nbraces--;
35336 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35337 cp_lexer_consume_token (parser->lexer);
35338 else
35340 if (!collapse_err)
35342 error_at (cp_lexer_peek_token (parser->lexer)->location,
35343 "collapsed loops not perfectly nested");
35345 collapse_err = true;
35346 cp_parser_statement_seq_opt (parser, NULL);
35347 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35348 break;
35352 while (!for_block->is_empty ())
35354 tree t = for_block->pop ();
35355 if (TREE_CODE (t) == STATEMENT_LIST)
35356 add_stmt (pop_stmt_list (t));
35357 else
35358 add_stmt (t);
35360 release_tree_vector (for_block);
35362 return ret;
35365 /* Helper function for OpenMP parsing, split clauses and call
35366 finish_omp_clauses on each of the set of clauses afterwards. */
35368 static void
35369 cp_omp_split_clauses (location_t loc, enum tree_code code,
35370 omp_clause_mask mask, tree clauses, tree *cclauses)
35372 int i;
35373 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35374 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35375 if (cclauses[i])
35376 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35379 /* OpenMP 4.0:
35380 #pragma omp simd simd-clause[optseq] new-line
35381 for-loop */
35383 #define OMP_SIMD_CLAUSE_MASK \
35384 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35393 static tree
35394 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35395 char *p_name, omp_clause_mask mask, tree *cclauses,
35396 bool *if_p)
35398 tree clauses, sb, ret;
35399 unsigned int save;
35400 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35402 strcat (p_name, " simd");
35403 mask |= OMP_SIMD_CLAUSE_MASK;
35405 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35406 cclauses == NULL);
35407 if (cclauses)
35409 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35410 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35411 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35412 OMP_CLAUSE_ORDERED);
35413 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35415 error_at (OMP_CLAUSE_LOCATION (c),
35416 "%<ordered%> clause with parameter may not be specified "
35417 "on %qs construct", p_name);
35418 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35422 sb = begin_omp_structured_block ();
35423 save = cp_parser_begin_omp_structured_block (parser);
35425 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35427 cp_parser_end_omp_structured_block (parser, save);
35428 add_stmt (finish_omp_structured_block (sb));
35430 return ret;
35433 /* OpenMP 2.5:
35434 #pragma omp for for-clause[optseq] new-line
35435 for-loop
35437 OpenMP 4.0:
35438 #pragma omp for simd for-simd-clause[optseq] new-line
35439 for-loop */
35441 #define OMP_FOR_CLAUSE_MASK \
35442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35452 static tree
35453 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35454 char *p_name, omp_clause_mask mask, tree *cclauses,
35455 bool *if_p)
35457 tree clauses, sb, ret;
35458 unsigned int save;
35459 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35461 strcat (p_name, " for");
35462 mask |= OMP_FOR_CLAUSE_MASK;
35463 /* parallel for{, simd} disallows nowait clause, but for
35464 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35465 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35466 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35467 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35468 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35469 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35473 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35474 const char *p = IDENTIFIER_POINTER (id);
35476 if (strcmp (p, "simd") == 0)
35478 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35479 if (cclauses == NULL)
35480 cclauses = cclauses_buf;
35482 cp_lexer_consume_token (parser->lexer);
35483 if (!flag_openmp) /* flag_openmp_simd */
35484 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35485 cclauses, if_p);
35486 sb = begin_omp_structured_block ();
35487 save = cp_parser_begin_omp_structured_block (parser);
35488 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35489 cclauses, if_p);
35490 cp_parser_end_omp_structured_block (parser, save);
35491 tree body = finish_omp_structured_block (sb);
35492 if (ret == NULL)
35493 return ret;
35494 ret = make_node (OMP_FOR);
35495 TREE_TYPE (ret) = void_type_node;
35496 OMP_FOR_BODY (ret) = body;
35497 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35498 SET_EXPR_LOCATION (ret, loc);
35499 add_stmt (ret);
35500 return ret;
35503 if (!flag_openmp) /* flag_openmp_simd */
35505 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35506 return NULL_TREE;
35509 /* Composite distribute parallel for disallows linear clause. */
35510 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35511 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35513 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35514 cclauses == NULL);
35515 if (cclauses)
35517 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35518 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35521 sb = begin_omp_structured_block ();
35522 save = cp_parser_begin_omp_structured_block (parser);
35524 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35526 cp_parser_end_omp_structured_block (parser, save);
35527 add_stmt (finish_omp_structured_block (sb));
35529 return ret;
35532 /* OpenMP 2.5:
35533 # pragma omp master new-line
35534 structured-block */
35536 static tree
35537 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35539 cp_parser_require_pragma_eol (parser, pragma_tok);
35540 return c_finish_omp_master (input_location,
35541 cp_parser_omp_structured_block (parser, if_p));
35544 /* OpenMP 2.5:
35545 # pragma omp ordered new-line
35546 structured-block
35548 OpenMP 4.5:
35549 # pragma omp ordered ordered-clauses new-line
35550 structured-block */
35552 #define OMP_ORDERED_CLAUSE_MASK \
35553 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35556 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35557 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35559 static bool
35560 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35561 enum pragma_context context, bool *if_p)
35563 location_t loc = pragma_tok->location;
35565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35567 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35568 const char *p = IDENTIFIER_POINTER (id);
35570 if (strcmp (p, "depend") == 0)
35572 if (!flag_openmp) /* flag_openmp_simd */
35574 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35575 return false;
35577 if (context == pragma_stmt)
35579 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35580 "%<depend%> clause may only be used in compound "
35581 "statements");
35582 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35583 return false;
35585 tree clauses
35586 = cp_parser_omp_all_clauses (parser,
35587 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35588 "#pragma omp ordered", pragma_tok);
35589 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35590 return false;
35594 tree clauses
35595 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35596 "#pragma omp ordered", pragma_tok);
35598 if (!flag_openmp /* flag_openmp_simd */
35599 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35600 return false;
35602 c_finish_omp_ordered (loc, clauses,
35603 cp_parser_omp_structured_block (parser, if_p));
35604 return true;
35607 /* OpenMP 2.5:
35609 section-scope:
35610 { section-sequence }
35612 section-sequence:
35613 section-directive[opt] structured-block
35614 section-sequence section-directive structured-block */
35616 static tree
35617 cp_parser_omp_sections_scope (cp_parser *parser)
35619 tree stmt, substmt;
35620 bool error_suppress = false;
35621 cp_token *tok;
35623 matching_braces braces;
35624 if (!braces.require_open (parser))
35625 return NULL_TREE;
35627 stmt = push_stmt_list ();
35629 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35630 != PRAGMA_OMP_SECTION)
35632 substmt = cp_parser_omp_structured_block (parser, NULL);
35633 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35634 add_stmt (substmt);
35637 while (1)
35639 tok = cp_lexer_peek_token (parser->lexer);
35640 if (tok->type == CPP_CLOSE_BRACE)
35641 break;
35642 if (tok->type == CPP_EOF)
35643 break;
35645 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35647 cp_lexer_consume_token (parser->lexer);
35648 cp_parser_require_pragma_eol (parser, tok);
35649 error_suppress = false;
35651 else if (!error_suppress)
35653 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35654 error_suppress = true;
35657 substmt = cp_parser_omp_structured_block (parser, NULL);
35658 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35659 add_stmt (substmt);
35661 braces.require_close (parser);
35663 substmt = pop_stmt_list (stmt);
35665 stmt = make_node (OMP_SECTIONS);
35666 TREE_TYPE (stmt) = void_type_node;
35667 OMP_SECTIONS_BODY (stmt) = substmt;
35669 add_stmt (stmt);
35670 return stmt;
35673 /* OpenMP 2.5:
35674 # pragma omp sections sections-clause[optseq] newline
35675 sections-scope */
35677 #define OMP_SECTIONS_CLAUSE_MASK \
35678 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35684 static tree
35685 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35686 char *p_name, omp_clause_mask mask, tree *cclauses)
35688 tree clauses, ret;
35689 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35691 strcat (p_name, " sections");
35692 mask |= OMP_SECTIONS_CLAUSE_MASK;
35693 if (cclauses)
35694 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35696 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35697 cclauses == NULL);
35698 if (cclauses)
35700 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35701 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35704 ret = cp_parser_omp_sections_scope (parser);
35705 if (ret)
35706 OMP_SECTIONS_CLAUSES (ret) = clauses;
35708 return ret;
35711 /* OpenMP 2.5:
35712 # pragma omp parallel parallel-clause[optseq] new-line
35713 structured-block
35714 # pragma omp parallel for parallel-for-clause[optseq] new-line
35715 structured-block
35716 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35717 structured-block
35719 OpenMP 4.0:
35720 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35721 structured-block */
35723 #define OMP_PARALLEL_CLAUSE_MASK \
35724 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35734 static tree
35735 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35736 char *p_name, omp_clause_mask mask, tree *cclauses,
35737 bool *if_p)
35739 tree stmt, clauses, block;
35740 unsigned int save;
35741 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35743 strcat (p_name, " parallel");
35744 mask |= OMP_PARALLEL_CLAUSE_MASK;
35745 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35746 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35747 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35748 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35750 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35752 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35753 if (cclauses == NULL)
35754 cclauses = cclauses_buf;
35756 cp_lexer_consume_token (parser->lexer);
35757 if (!flag_openmp) /* flag_openmp_simd */
35758 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35759 if_p);
35760 block = begin_omp_parallel ();
35761 save = cp_parser_begin_omp_structured_block (parser);
35762 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35763 if_p);
35764 cp_parser_end_omp_structured_block (parser, save);
35765 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35766 block);
35767 if (ret == NULL_TREE)
35768 return ret;
35769 OMP_PARALLEL_COMBINED (stmt) = 1;
35770 return stmt;
35772 /* When combined with distribute, parallel has to be followed by for.
35773 #pragma omp target parallel is allowed though. */
35774 else if (cclauses
35775 && (mask & (OMP_CLAUSE_MASK_1
35776 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35778 error_at (loc, "expected %<for%> after %qs", p_name);
35779 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35780 return NULL_TREE;
35782 else if (!flag_openmp) /* flag_openmp_simd */
35784 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35785 return NULL_TREE;
35787 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35789 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35790 const char *p = IDENTIFIER_POINTER (id);
35791 if (strcmp (p, "sections") == 0)
35793 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35794 cclauses = cclauses_buf;
35796 cp_lexer_consume_token (parser->lexer);
35797 block = begin_omp_parallel ();
35798 save = cp_parser_begin_omp_structured_block (parser);
35799 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35800 cp_parser_end_omp_structured_block (parser, save);
35801 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35802 block);
35803 OMP_PARALLEL_COMBINED (stmt) = 1;
35804 return stmt;
35808 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35809 cclauses == NULL);
35810 if (cclauses)
35812 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35813 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35816 block = begin_omp_parallel ();
35817 save = cp_parser_begin_omp_structured_block (parser);
35818 cp_parser_statement (parser, NULL_TREE, false, if_p);
35819 cp_parser_end_omp_structured_block (parser, save);
35820 stmt = finish_omp_parallel (clauses, block);
35821 return stmt;
35824 /* OpenMP 2.5:
35825 # pragma omp single single-clause[optseq] new-line
35826 structured-block */
35828 #define OMP_SINGLE_CLAUSE_MASK \
35829 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35834 static tree
35835 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35837 tree stmt = make_node (OMP_SINGLE);
35838 TREE_TYPE (stmt) = void_type_node;
35840 OMP_SINGLE_CLAUSES (stmt)
35841 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35842 "#pragma omp single", pragma_tok);
35843 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35845 return add_stmt (stmt);
35848 /* OpenMP 3.0:
35849 # pragma omp task task-clause[optseq] new-line
35850 structured-block */
35852 #define OMP_TASK_CLAUSE_MASK \
35853 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35864 static tree
35865 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35867 tree clauses, block;
35868 unsigned int save;
35870 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35871 "#pragma omp task", pragma_tok);
35872 block = begin_omp_task ();
35873 save = cp_parser_begin_omp_structured_block (parser);
35874 cp_parser_statement (parser, NULL_TREE, false, if_p);
35875 cp_parser_end_omp_structured_block (parser, save);
35876 return finish_omp_task (clauses, block);
35879 /* OpenMP 3.0:
35880 # pragma omp taskwait new-line */
35882 static void
35883 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35885 cp_parser_require_pragma_eol (parser, pragma_tok);
35886 finish_omp_taskwait ();
35889 /* OpenMP 3.1:
35890 # pragma omp taskyield new-line */
35892 static void
35893 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35895 cp_parser_require_pragma_eol (parser, pragma_tok);
35896 finish_omp_taskyield ();
35899 /* OpenMP 4.0:
35900 # pragma omp taskgroup new-line
35901 structured-block */
35903 static tree
35904 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35906 cp_parser_require_pragma_eol (parser, pragma_tok);
35907 return c_finish_omp_taskgroup (input_location,
35908 cp_parser_omp_structured_block (parser,
35909 if_p));
35913 /* OpenMP 2.5:
35914 # pragma omp threadprivate (variable-list) */
35916 static void
35917 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35919 tree vars;
35921 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35922 cp_parser_require_pragma_eol (parser, pragma_tok);
35924 finish_omp_threadprivate (vars);
35927 /* OpenMP 4.0:
35928 # pragma omp cancel cancel-clause[optseq] new-line */
35930 #define OMP_CANCEL_CLAUSE_MASK \
35931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35937 static void
35938 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35940 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35941 "#pragma omp cancel", pragma_tok);
35942 finish_omp_cancel (clauses);
35945 /* OpenMP 4.0:
35946 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35948 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35949 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35954 static void
35955 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35956 enum pragma_context context)
35958 tree clauses;
35959 bool point_seen = false;
35961 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35963 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35964 const char *p = IDENTIFIER_POINTER (id);
35966 if (strcmp (p, "point") == 0)
35968 cp_lexer_consume_token (parser->lexer);
35969 point_seen = true;
35972 if (!point_seen)
35974 cp_parser_error (parser, "expected %<point%>");
35975 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35976 return;
35979 if (context != pragma_compound)
35981 if (context == pragma_stmt)
35982 error_at (pragma_tok->location,
35983 "%<#pragma %s%> may only be used in compound statements",
35984 "omp cancellation point");
35985 else
35986 cp_parser_error (parser, "expected declaration specifiers");
35987 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35988 return;
35991 clauses = cp_parser_omp_all_clauses (parser,
35992 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35993 "#pragma omp cancellation point",
35994 pragma_tok);
35995 finish_omp_cancellation_point (clauses);
35998 /* OpenMP 4.0:
35999 #pragma omp distribute distribute-clause[optseq] new-line
36000 for-loop */
36002 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36003 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36009 static tree
36010 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36011 char *p_name, omp_clause_mask mask, tree *cclauses,
36012 bool *if_p)
36014 tree clauses, sb, ret;
36015 unsigned int save;
36016 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36018 strcat (p_name, " distribute");
36019 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36021 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36023 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36024 const char *p = IDENTIFIER_POINTER (id);
36025 bool simd = false;
36026 bool parallel = false;
36028 if (strcmp (p, "simd") == 0)
36029 simd = true;
36030 else
36031 parallel = strcmp (p, "parallel") == 0;
36032 if (parallel || simd)
36034 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36035 if (cclauses == NULL)
36036 cclauses = cclauses_buf;
36037 cp_lexer_consume_token (parser->lexer);
36038 if (!flag_openmp) /* flag_openmp_simd */
36040 if (simd)
36041 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36042 cclauses, if_p);
36043 else
36044 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36045 cclauses, if_p);
36047 sb = begin_omp_structured_block ();
36048 save = cp_parser_begin_omp_structured_block (parser);
36049 if (simd)
36050 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36051 cclauses, if_p);
36052 else
36053 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36054 cclauses, if_p);
36055 cp_parser_end_omp_structured_block (parser, save);
36056 tree body = finish_omp_structured_block (sb);
36057 if (ret == NULL)
36058 return ret;
36059 ret = make_node (OMP_DISTRIBUTE);
36060 TREE_TYPE (ret) = void_type_node;
36061 OMP_FOR_BODY (ret) = body;
36062 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36063 SET_EXPR_LOCATION (ret, loc);
36064 add_stmt (ret);
36065 return ret;
36068 if (!flag_openmp) /* flag_openmp_simd */
36070 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36071 return NULL_TREE;
36074 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36075 cclauses == NULL);
36076 if (cclauses)
36078 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36079 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36082 sb = begin_omp_structured_block ();
36083 save = cp_parser_begin_omp_structured_block (parser);
36085 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36087 cp_parser_end_omp_structured_block (parser, save);
36088 add_stmt (finish_omp_structured_block (sb));
36090 return ret;
36093 /* OpenMP 4.0:
36094 # pragma omp teams teams-clause[optseq] new-line
36095 structured-block */
36097 #define OMP_TEAMS_CLAUSE_MASK \
36098 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36106 static tree
36107 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36108 char *p_name, omp_clause_mask mask, tree *cclauses,
36109 bool *if_p)
36111 tree clauses, sb, ret;
36112 unsigned int save;
36113 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36115 strcat (p_name, " teams");
36116 mask |= OMP_TEAMS_CLAUSE_MASK;
36118 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36120 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36121 const char *p = IDENTIFIER_POINTER (id);
36122 if (strcmp (p, "distribute") == 0)
36124 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36125 if (cclauses == NULL)
36126 cclauses = cclauses_buf;
36128 cp_lexer_consume_token (parser->lexer);
36129 if (!flag_openmp) /* flag_openmp_simd */
36130 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36131 cclauses, if_p);
36132 sb = begin_omp_structured_block ();
36133 save = cp_parser_begin_omp_structured_block (parser);
36134 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36135 cclauses, if_p);
36136 cp_parser_end_omp_structured_block (parser, save);
36137 tree body = finish_omp_structured_block (sb);
36138 if (ret == NULL)
36139 return ret;
36140 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36141 ret = make_node (OMP_TEAMS);
36142 TREE_TYPE (ret) = void_type_node;
36143 OMP_TEAMS_CLAUSES (ret) = clauses;
36144 OMP_TEAMS_BODY (ret) = body;
36145 OMP_TEAMS_COMBINED (ret) = 1;
36146 SET_EXPR_LOCATION (ret, loc);
36147 return add_stmt (ret);
36150 if (!flag_openmp) /* flag_openmp_simd */
36152 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36153 return NULL_TREE;
36156 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36157 cclauses == NULL);
36158 if (cclauses)
36160 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36161 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36164 tree stmt = make_node (OMP_TEAMS);
36165 TREE_TYPE (stmt) = void_type_node;
36166 OMP_TEAMS_CLAUSES (stmt) = clauses;
36167 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36168 SET_EXPR_LOCATION (stmt, loc);
36170 return add_stmt (stmt);
36173 /* OpenMP 4.0:
36174 # pragma omp target data target-data-clause[optseq] new-line
36175 structured-block */
36177 #define OMP_TARGET_DATA_CLAUSE_MASK \
36178 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36183 static tree
36184 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36186 tree clauses
36187 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36188 "#pragma omp target data", pragma_tok);
36189 int map_seen = 0;
36190 for (tree *pc = &clauses; *pc;)
36192 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36193 switch (OMP_CLAUSE_MAP_KIND (*pc))
36195 case GOMP_MAP_TO:
36196 case GOMP_MAP_ALWAYS_TO:
36197 case GOMP_MAP_FROM:
36198 case GOMP_MAP_ALWAYS_FROM:
36199 case GOMP_MAP_TOFROM:
36200 case GOMP_MAP_ALWAYS_TOFROM:
36201 case GOMP_MAP_ALLOC:
36202 map_seen = 3;
36203 break;
36204 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36205 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36206 case GOMP_MAP_ALWAYS_POINTER:
36207 break;
36208 default:
36209 map_seen |= 1;
36210 error_at (OMP_CLAUSE_LOCATION (*pc),
36211 "%<#pragma omp target data%> with map-type other "
36212 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36213 "on %<map%> clause");
36214 *pc = OMP_CLAUSE_CHAIN (*pc);
36215 continue;
36217 pc = &OMP_CLAUSE_CHAIN (*pc);
36220 if (map_seen != 3)
36222 if (map_seen == 0)
36223 error_at (pragma_tok->location,
36224 "%<#pragma omp target data%> must contain at least "
36225 "one %<map%> clause");
36226 return NULL_TREE;
36229 tree stmt = make_node (OMP_TARGET_DATA);
36230 TREE_TYPE (stmt) = void_type_node;
36231 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36233 keep_next_level (true);
36234 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36236 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36237 return add_stmt (stmt);
36240 /* OpenMP 4.5:
36241 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36242 structured-block */
36244 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36245 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36251 static tree
36252 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36253 enum pragma_context context)
36255 bool data_seen = false;
36256 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36258 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36259 const char *p = IDENTIFIER_POINTER (id);
36261 if (strcmp (p, "data") == 0)
36263 cp_lexer_consume_token (parser->lexer);
36264 data_seen = true;
36267 if (!data_seen)
36269 cp_parser_error (parser, "expected %<data%>");
36270 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36271 return NULL_TREE;
36274 if (context == pragma_stmt)
36276 error_at (pragma_tok->location,
36277 "%<#pragma %s%> may only be used in compound statements",
36278 "omp target enter data");
36279 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36280 return NULL_TREE;
36283 tree clauses
36284 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36285 "#pragma omp target enter data", pragma_tok);
36286 int map_seen = 0;
36287 for (tree *pc = &clauses; *pc;)
36289 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36290 switch (OMP_CLAUSE_MAP_KIND (*pc))
36292 case GOMP_MAP_TO:
36293 case GOMP_MAP_ALWAYS_TO:
36294 case GOMP_MAP_ALLOC:
36295 map_seen = 3;
36296 break;
36297 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36298 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36299 case GOMP_MAP_ALWAYS_POINTER:
36300 break;
36301 default:
36302 map_seen |= 1;
36303 error_at (OMP_CLAUSE_LOCATION (*pc),
36304 "%<#pragma omp target enter data%> with map-type other "
36305 "than %<to%> or %<alloc%> on %<map%> clause");
36306 *pc = OMP_CLAUSE_CHAIN (*pc);
36307 continue;
36309 pc = &OMP_CLAUSE_CHAIN (*pc);
36312 if (map_seen != 3)
36314 if (map_seen == 0)
36315 error_at (pragma_tok->location,
36316 "%<#pragma omp target enter data%> must contain at least "
36317 "one %<map%> clause");
36318 return NULL_TREE;
36321 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36322 TREE_TYPE (stmt) = void_type_node;
36323 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36324 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36325 return add_stmt (stmt);
36328 /* OpenMP 4.5:
36329 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36330 structured-block */
36332 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36333 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36339 static tree
36340 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36341 enum pragma_context context)
36343 bool data_seen = false;
36344 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36346 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36347 const char *p = IDENTIFIER_POINTER (id);
36349 if (strcmp (p, "data") == 0)
36351 cp_lexer_consume_token (parser->lexer);
36352 data_seen = true;
36355 if (!data_seen)
36357 cp_parser_error (parser, "expected %<data%>");
36358 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36359 return NULL_TREE;
36362 if (context == pragma_stmt)
36364 error_at (pragma_tok->location,
36365 "%<#pragma %s%> may only be used in compound statements",
36366 "omp target exit data");
36367 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36368 return NULL_TREE;
36371 tree clauses
36372 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36373 "#pragma omp target exit data", pragma_tok);
36374 int map_seen = 0;
36375 for (tree *pc = &clauses; *pc;)
36377 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36378 switch (OMP_CLAUSE_MAP_KIND (*pc))
36380 case GOMP_MAP_FROM:
36381 case GOMP_MAP_ALWAYS_FROM:
36382 case GOMP_MAP_RELEASE:
36383 case GOMP_MAP_DELETE:
36384 map_seen = 3;
36385 break;
36386 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36387 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36388 case GOMP_MAP_ALWAYS_POINTER:
36389 break;
36390 default:
36391 map_seen |= 1;
36392 error_at (OMP_CLAUSE_LOCATION (*pc),
36393 "%<#pragma omp target exit data%> with map-type other "
36394 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36395 " clause");
36396 *pc = OMP_CLAUSE_CHAIN (*pc);
36397 continue;
36399 pc = &OMP_CLAUSE_CHAIN (*pc);
36402 if (map_seen != 3)
36404 if (map_seen == 0)
36405 error_at (pragma_tok->location,
36406 "%<#pragma omp target exit data%> must contain at least "
36407 "one %<map%> clause");
36408 return NULL_TREE;
36411 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36412 TREE_TYPE (stmt) = void_type_node;
36413 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36414 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36415 return add_stmt (stmt);
36418 /* OpenMP 4.0:
36419 # pragma omp target update target-update-clause[optseq] new-line */
36421 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36422 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36429 static bool
36430 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36431 enum pragma_context context)
36433 if (context == pragma_stmt)
36435 error_at (pragma_tok->location,
36436 "%<#pragma %s%> may only be used in compound statements",
36437 "omp target update");
36438 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36439 return false;
36442 tree clauses
36443 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36444 "#pragma omp target update", pragma_tok);
36445 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36446 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36448 error_at (pragma_tok->location,
36449 "%<#pragma omp target update%> must contain at least one "
36450 "%<from%> or %<to%> clauses");
36451 return false;
36454 tree stmt = make_node (OMP_TARGET_UPDATE);
36455 TREE_TYPE (stmt) = void_type_node;
36456 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36457 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36458 add_stmt (stmt);
36459 return false;
36462 /* OpenMP 4.0:
36463 # pragma omp target target-clause[optseq] new-line
36464 structured-block */
36466 #define OMP_TARGET_CLAUSE_MASK \
36467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36477 static bool
36478 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36479 enum pragma_context context, bool *if_p)
36481 tree *pc = NULL, stmt;
36483 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36485 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36486 const char *p = IDENTIFIER_POINTER (id);
36487 enum tree_code ccode = ERROR_MARK;
36489 if (strcmp (p, "teams") == 0)
36490 ccode = OMP_TEAMS;
36491 else if (strcmp (p, "parallel") == 0)
36492 ccode = OMP_PARALLEL;
36493 else if (strcmp (p, "simd") == 0)
36494 ccode = OMP_SIMD;
36495 if (ccode != ERROR_MARK)
36497 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36498 char p_name[sizeof ("#pragma omp target teams distribute "
36499 "parallel for simd")];
36501 cp_lexer_consume_token (parser->lexer);
36502 strcpy (p_name, "#pragma omp target");
36503 if (!flag_openmp) /* flag_openmp_simd */
36505 tree stmt;
36506 switch (ccode)
36508 case OMP_TEAMS:
36509 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36510 OMP_TARGET_CLAUSE_MASK,
36511 cclauses, if_p);
36512 break;
36513 case OMP_PARALLEL:
36514 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36515 OMP_TARGET_CLAUSE_MASK,
36516 cclauses, if_p);
36517 break;
36518 case OMP_SIMD:
36519 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36520 OMP_TARGET_CLAUSE_MASK,
36521 cclauses, if_p);
36522 break;
36523 default:
36524 gcc_unreachable ();
36526 return stmt != NULL_TREE;
36528 keep_next_level (true);
36529 tree sb = begin_omp_structured_block (), ret;
36530 unsigned save = cp_parser_begin_omp_structured_block (parser);
36531 switch (ccode)
36533 case OMP_TEAMS:
36534 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36535 OMP_TARGET_CLAUSE_MASK, cclauses,
36536 if_p);
36537 break;
36538 case OMP_PARALLEL:
36539 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36540 OMP_TARGET_CLAUSE_MASK, cclauses,
36541 if_p);
36542 break;
36543 case OMP_SIMD:
36544 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36545 OMP_TARGET_CLAUSE_MASK, cclauses,
36546 if_p);
36547 break;
36548 default:
36549 gcc_unreachable ();
36551 cp_parser_end_omp_structured_block (parser, save);
36552 tree body = finish_omp_structured_block (sb);
36553 if (ret == NULL_TREE)
36554 return false;
36555 if (ccode == OMP_TEAMS && !processing_template_decl)
36557 /* For combined target teams, ensure the num_teams and
36558 thread_limit clause expressions are evaluated on the host,
36559 before entering the target construct. */
36560 tree c;
36561 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36562 c; c = OMP_CLAUSE_CHAIN (c))
36563 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36564 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36565 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36567 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36568 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36569 if (expr == error_mark_node)
36570 continue;
36571 tree tmp = TARGET_EXPR_SLOT (expr);
36572 add_stmt (expr);
36573 OMP_CLAUSE_OPERAND (c, 0) = expr;
36574 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36575 OMP_CLAUSE_FIRSTPRIVATE);
36576 OMP_CLAUSE_DECL (tc) = tmp;
36577 OMP_CLAUSE_CHAIN (tc)
36578 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36579 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36582 tree stmt = make_node (OMP_TARGET);
36583 TREE_TYPE (stmt) = void_type_node;
36584 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36585 OMP_TARGET_BODY (stmt) = body;
36586 OMP_TARGET_COMBINED (stmt) = 1;
36587 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36588 add_stmt (stmt);
36589 pc = &OMP_TARGET_CLAUSES (stmt);
36590 goto check_clauses;
36592 else if (!flag_openmp) /* flag_openmp_simd */
36594 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36595 return false;
36597 else if (strcmp (p, "data") == 0)
36599 cp_lexer_consume_token (parser->lexer);
36600 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36601 return true;
36603 else if (strcmp (p, "enter") == 0)
36605 cp_lexer_consume_token (parser->lexer);
36606 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36607 return false;
36609 else if (strcmp (p, "exit") == 0)
36611 cp_lexer_consume_token (parser->lexer);
36612 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36613 return false;
36615 else if (strcmp (p, "update") == 0)
36617 cp_lexer_consume_token (parser->lexer);
36618 return cp_parser_omp_target_update (parser, pragma_tok, context);
36621 if (!flag_openmp) /* flag_openmp_simd */
36623 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36624 return false;
36627 stmt = make_node (OMP_TARGET);
36628 TREE_TYPE (stmt) = void_type_node;
36630 OMP_TARGET_CLAUSES (stmt)
36631 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36632 "#pragma omp target", pragma_tok);
36633 pc = &OMP_TARGET_CLAUSES (stmt);
36634 keep_next_level (true);
36635 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36637 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36638 add_stmt (stmt);
36640 check_clauses:
36641 while (*pc)
36643 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36644 switch (OMP_CLAUSE_MAP_KIND (*pc))
36646 case GOMP_MAP_TO:
36647 case GOMP_MAP_ALWAYS_TO:
36648 case GOMP_MAP_FROM:
36649 case GOMP_MAP_ALWAYS_FROM:
36650 case GOMP_MAP_TOFROM:
36651 case GOMP_MAP_ALWAYS_TOFROM:
36652 case GOMP_MAP_ALLOC:
36653 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36654 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36655 case GOMP_MAP_ALWAYS_POINTER:
36656 break;
36657 default:
36658 error_at (OMP_CLAUSE_LOCATION (*pc),
36659 "%<#pragma omp target%> with map-type other "
36660 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36661 "on %<map%> clause");
36662 *pc = OMP_CLAUSE_CHAIN (*pc);
36663 continue;
36665 pc = &OMP_CLAUSE_CHAIN (*pc);
36667 return true;
36670 /* OpenACC 2.0:
36671 # pragma acc cache (variable-list) new-line
36674 static tree
36675 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36677 tree stmt, clauses;
36679 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36680 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36682 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36684 stmt = make_node (OACC_CACHE);
36685 TREE_TYPE (stmt) = void_type_node;
36686 OACC_CACHE_CLAUSES (stmt) = clauses;
36687 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36688 add_stmt (stmt);
36690 return stmt;
36693 /* OpenACC 2.0:
36694 # pragma acc data oacc-data-clause[optseq] new-line
36695 structured-block */
36697 #define OACC_DATA_CLAUSE_MASK \
36698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36710 static tree
36711 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36713 tree stmt, clauses, block;
36714 unsigned int save;
36716 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36717 "#pragma acc data", pragma_tok);
36719 block = begin_omp_parallel ();
36720 save = cp_parser_begin_omp_structured_block (parser);
36721 cp_parser_statement (parser, NULL_TREE, false, if_p);
36722 cp_parser_end_omp_structured_block (parser, save);
36723 stmt = finish_oacc_data (clauses, block);
36724 return stmt;
36727 /* OpenACC 2.0:
36728 # pragma acc host_data <clauses> new-line
36729 structured-block */
36731 #define OACC_HOST_DATA_CLAUSE_MASK \
36732 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36734 static tree
36735 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36737 tree stmt, clauses, block;
36738 unsigned int save;
36740 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36741 "#pragma acc host_data", pragma_tok);
36743 block = begin_omp_parallel ();
36744 save = cp_parser_begin_omp_structured_block (parser);
36745 cp_parser_statement (parser, NULL_TREE, false, if_p);
36746 cp_parser_end_omp_structured_block (parser, save);
36747 stmt = finish_oacc_host_data (clauses, block);
36748 return stmt;
36751 /* OpenACC 2.0:
36752 # pragma acc declare oacc-data-clause[optseq] new-line
36755 #define OACC_DECLARE_CLAUSE_MASK \
36756 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36769 static tree
36770 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36772 tree clauses, stmt;
36773 bool error = false;
36775 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36776 "#pragma acc declare", pragma_tok, true);
36779 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36781 error_at (pragma_tok->location,
36782 "no valid clauses specified in %<#pragma acc declare%>");
36783 return NULL_TREE;
36786 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36788 location_t loc = OMP_CLAUSE_LOCATION (t);
36789 tree decl = OMP_CLAUSE_DECL (t);
36790 if (!DECL_P (decl))
36792 error_at (loc, "array section in %<#pragma acc declare%>");
36793 error = true;
36794 continue;
36796 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36797 switch (OMP_CLAUSE_MAP_KIND (t))
36799 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36800 case GOMP_MAP_FORCE_ALLOC:
36801 case GOMP_MAP_FORCE_TO:
36802 case GOMP_MAP_FORCE_DEVICEPTR:
36803 case GOMP_MAP_DEVICE_RESIDENT:
36804 break;
36806 case GOMP_MAP_LINK:
36807 if (!global_bindings_p ()
36808 && (TREE_STATIC (decl)
36809 || !DECL_EXTERNAL (decl)))
36811 error_at (loc,
36812 "%qD must be a global variable in "
36813 "%<#pragma acc declare link%>",
36814 decl);
36815 error = true;
36816 continue;
36818 break;
36820 default:
36821 if (global_bindings_p ())
36823 error_at (loc, "invalid OpenACC clause at file scope");
36824 error = true;
36825 continue;
36827 if (DECL_EXTERNAL (decl))
36829 error_at (loc,
36830 "invalid use of %<extern%> variable %qD "
36831 "in %<#pragma acc declare%>", decl);
36832 error = true;
36833 continue;
36835 else if (TREE_PUBLIC (decl))
36837 error_at (loc,
36838 "invalid use of %<global%> variable %qD "
36839 "in %<#pragma acc declare%>", decl);
36840 error = true;
36841 continue;
36843 break;
36846 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36847 || lookup_attribute ("omp declare target link",
36848 DECL_ATTRIBUTES (decl)))
36850 error_at (loc, "variable %qD used more than once with "
36851 "%<#pragma acc declare%>", decl);
36852 error = true;
36853 continue;
36856 if (!error)
36858 tree id;
36860 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36861 id = get_identifier ("omp declare target link");
36862 else
36863 id = get_identifier ("omp declare target");
36865 DECL_ATTRIBUTES (decl)
36866 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36867 if (global_bindings_p ())
36869 symtab_node *node = symtab_node::get (decl);
36870 if (node != NULL)
36872 node->offloadable = 1;
36873 if (ENABLE_OFFLOADING)
36875 g->have_offload = true;
36876 if (is_a <varpool_node *> (node))
36877 vec_safe_push (offload_vars, decl);
36884 if (error || global_bindings_p ())
36885 return NULL_TREE;
36887 stmt = make_node (OACC_DECLARE);
36888 TREE_TYPE (stmt) = void_type_node;
36889 OACC_DECLARE_CLAUSES (stmt) = clauses;
36890 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36892 add_stmt (stmt);
36894 return NULL_TREE;
36897 /* OpenACC 2.0:
36898 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36902 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36904 LOC is the location of the #pragma token.
36907 #define OACC_ENTER_DATA_CLAUSE_MASK \
36908 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36916 #define OACC_EXIT_DATA_CLAUSE_MASK \
36917 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36923 static tree
36924 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36925 bool enter)
36927 location_t loc = pragma_tok->location;
36928 tree stmt, clauses;
36929 const char *p = "";
36931 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36932 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36934 if (strcmp (p, "data") != 0)
36936 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36937 enter ? "enter" : "exit");
36938 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36939 return NULL_TREE;
36942 cp_lexer_consume_token (parser->lexer);
36944 if (enter)
36945 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36946 "#pragma acc enter data", pragma_tok);
36947 else
36948 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36949 "#pragma acc exit data", pragma_tok);
36951 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36953 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36954 enter ? "enter" : "exit");
36955 return NULL_TREE;
36958 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36959 TREE_TYPE (stmt) = void_type_node;
36960 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36961 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36962 add_stmt (stmt);
36963 return stmt;
36966 /* OpenACC 2.0:
36967 # pragma acc loop oacc-loop-clause[optseq] new-line
36968 structured-block */
36970 #define OACC_LOOP_CLAUSE_MASK \
36971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36982 static tree
36983 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36984 omp_clause_mask mask, tree *cclauses, bool *if_p)
36986 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36988 strcat (p_name, " loop");
36989 mask |= OACC_LOOP_CLAUSE_MASK;
36991 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36992 cclauses == NULL);
36993 if (cclauses)
36995 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36996 if (*cclauses)
36997 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36998 if (clauses)
36999 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37002 tree block = begin_omp_structured_block ();
37003 int save = cp_parser_begin_omp_structured_block (parser);
37004 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37005 cp_parser_end_omp_structured_block (parser, save);
37006 add_stmt (finish_omp_structured_block (block));
37008 return stmt;
37011 /* OpenACC 2.0:
37012 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37013 structured-block
37017 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37018 structured-block
37021 #define OACC_KERNELS_CLAUSE_MASK \
37022 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37040 #define OACC_PARALLEL_CLAUSE_MASK \
37041 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37062 static tree
37063 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37064 char *p_name, bool *if_p)
37066 omp_clause_mask mask;
37067 enum tree_code code;
37068 switch (cp_parser_pragma_kind (pragma_tok))
37070 case PRAGMA_OACC_KERNELS:
37071 strcat (p_name, " kernels");
37072 mask = OACC_KERNELS_CLAUSE_MASK;
37073 code = OACC_KERNELS;
37074 break;
37075 case PRAGMA_OACC_PARALLEL:
37076 strcat (p_name, " parallel");
37077 mask = OACC_PARALLEL_CLAUSE_MASK;
37078 code = OACC_PARALLEL;
37079 break;
37080 default:
37081 gcc_unreachable ();
37084 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37086 const char *p
37087 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37088 if (strcmp (p, "loop") == 0)
37090 cp_lexer_consume_token (parser->lexer);
37091 tree block = begin_omp_parallel ();
37092 tree clauses;
37093 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37094 if_p);
37095 return finish_omp_construct (code, block, clauses);
37099 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37101 tree block = begin_omp_parallel ();
37102 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37103 cp_parser_statement (parser, NULL_TREE, false, if_p);
37104 cp_parser_end_omp_structured_block (parser, save);
37105 return finish_omp_construct (code, block, clauses);
37108 /* OpenACC 2.0:
37109 # pragma acc update oacc-update-clause[optseq] new-line
37112 #define OACC_UPDATE_CLAUSE_MASK \
37113 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37120 static tree
37121 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37123 tree stmt, clauses;
37125 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37126 "#pragma acc update", pragma_tok);
37128 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37130 error_at (pragma_tok->location,
37131 "%<#pragma acc update%> must contain at least one "
37132 "%<device%> or %<host%> or %<self%> clause");
37133 return NULL_TREE;
37136 stmt = make_node (OACC_UPDATE);
37137 TREE_TYPE (stmt) = void_type_node;
37138 OACC_UPDATE_CLAUSES (stmt) = clauses;
37139 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37140 add_stmt (stmt);
37141 return stmt;
37144 /* OpenACC 2.0:
37145 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37147 LOC is the location of the #pragma token.
37150 #define OACC_WAIT_CLAUSE_MASK \
37151 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37153 static tree
37154 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37156 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37157 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37159 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37160 list = cp_parser_oacc_wait_list (parser, loc, list);
37162 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37163 "#pragma acc wait", pragma_tok);
37165 stmt = c_finish_oacc_wait (loc, list, clauses);
37166 stmt = finish_expr_stmt (stmt);
37168 return stmt;
37171 /* OpenMP 4.0:
37172 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37174 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37175 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37182 static void
37183 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37184 enum pragma_context context)
37186 bool first_p = parser->omp_declare_simd == NULL;
37187 cp_omp_declare_simd_data data;
37188 if (first_p)
37190 data.error_seen = false;
37191 data.fndecl_seen = false;
37192 data.tokens = vNULL;
37193 data.clauses = NULL_TREE;
37194 /* It is safe to take the address of a local variable; it will only be
37195 used while this scope is live. */
37196 parser->omp_declare_simd = &data;
37199 /* Store away all pragma tokens. */
37200 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37201 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37202 cp_lexer_consume_token (parser->lexer);
37203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37204 parser->omp_declare_simd->error_seen = true;
37205 cp_parser_require_pragma_eol (parser, pragma_tok);
37206 struct cp_token_cache *cp
37207 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37208 parser->omp_declare_simd->tokens.safe_push (cp);
37210 if (first_p)
37212 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37213 cp_parser_pragma (parser, context, NULL);
37214 switch (context)
37216 case pragma_external:
37217 cp_parser_declaration (parser);
37218 break;
37219 case pragma_member:
37220 cp_parser_member_declaration (parser);
37221 break;
37222 case pragma_objc_icode:
37223 cp_parser_block_declaration (parser, /*statement_p=*/false);
37224 break;
37225 default:
37226 cp_parser_declaration_statement (parser);
37227 break;
37229 if (parser->omp_declare_simd
37230 && !parser->omp_declare_simd->error_seen
37231 && !parser->omp_declare_simd->fndecl_seen)
37232 error_at (pragma_tok->location,
37233 "%<#pragma omp declare simd%> not immediately followed by "
37234 "function declaration or definition");
37235 data.tokens.release ();
37236 parser->omp_declare_simd = NULL;
37240 /* Finalize #pragma omp declare simd clauses after direct declarator has
37241 been parsed, and put that into "omp declare simd" attribute. */
37243 static tree
37244 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37246 struct cp_token_cache *ce;
37247 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37248 int i;
37250 if (!data->error_seen && data->fndecl_seen)
37252 error ("%<#pragma omp declare simd%> not immediately followed by "
37253 "a single function declaration or definition");
37254 data->error_seen = true;
37256 if (data->error_seen)
37257 return attrs;
37259 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37261 tree c, cl;
37263 cp_parser_push_lexer_for_tokens (parser, ce);
37264 parser->lexer->in_pragma = true;
37265 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37266 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37267 cp_lexer_consume_token (parser->lexer);
37268 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37269 "#pragma omp declare simd", pragma_tok);
37270 cp_parser_pop_lexer (parser);
37271 if (cl)
37272 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37273 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37274 TREE_CHAIN (c) = attrs;
37275 if (processing_template_decl)
37276 ATTR_IS_DEPENDENT (c) = 1;
37277 attrs = c;
37280 data->fndecl_seen = true;
37281 return attrs;
37285 /* OpenMP 4.0:
37286 # pragma omp declare target new-line
37287 declarations and definitions
37288 # pragma omp end declare target new-line
37290 OpenMP 4.5:
37291 # pragma omp declare target ( extended-list ) new-line
37293 # pragma omp declare target declare-target-clauses[seq] new-line */
37295 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37299 static void
37300 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37302 tree clauses = NULL_TREE;
37303 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37304 clauses
37305 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37306 "#pragma omp declare target", pragma_tok);
37307 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37309 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37310 clauses);
37311 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37312 cp_parser_require_pragma_eol (parser, pragma_tok);
37314 else
37316 cp_parser_require_pragma_eol (parser, pragma_tok);
37317 scope_chain->omp_declare_target_attribute++;
37318 return;
37320 if (scope_chain->omp_declare_target_attribute)
37321 error_at (pragma_tok->location,
37322 "%<#pragma omp declare target%> with clauses in between "
37323 "%<#pragma omp declare target%> without clauses and "
37324 "%<#pragma omp end declare target%>");
37325 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37327 tree t = OMP_CLAUSE_DECL (c), id;
37328 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37329 tree at2 = lookup_attribute ("omp declare target link",
37330 DECL_ATTRIBUTES (t));
37331 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37333 id = get_identifier ("omp declare target link");
37334 std::swap (at1, at2);
37336 else
37337 id = get_identifier ("omp declare target");
37338 if (at2)
37340 error_at (OMP_CLAUSE_LOCATION (c),
37341 "%qD specified both in declare target %<link%> and %<to%>"
37342 " clauses", t);
37343 continue;
37345 if (!at1)
37347 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37348 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37349 continue;
37351 symtab_node *node = symtab_node::get (t);
37352 if (node != NULL)
37354 node->offloadable = 1;
37355 if (ENABLE_OFFLOADING)
37357 g->have_offload = true;
37358 if (is_a <varpool_node *> (node))
37359 vec_safe_push (offload_vars, t);
37366 static void
37367 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37369 const char *p = "";
37370 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37372 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37373 p = IDENTIFIER_POINTER (id);
37375 if (strcmp (p, "declare") == 0)
37377 cp_lexer_consume_token (parser->lexer);
37378 p = "";
37379 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37381 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37382 p = IDENTIFIER_POINTER (id);
37384 if (strcmp (p, "target") == 0)
37385 cp_lexer_consume_token (parser->lexer);
37386 else
37388 cp_parser_error (parser, "expected %<target%>");
37389 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37390 return;
37393 else
37395 cp_parser_error (parser, "expected %<declare%>");
37396 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37397 return;
37399 cp_parser_require_pragma_eol (parser, pragma_tok);
37400 if (!scope_chain->omp_declare_target_attribute)
37401 error_at (pragma_tok->location,
37402 "%<#pragma omp end declare target%> without corresponding "
37403 "%<#pragma omp declare target%>");
37404 else
37405 scope_chain->omp_declare_target_attribute--;
37408 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37409 expression and optional initializer clause of
37410 #pragma omp declare reduction. We store the expression(s) as
37411 either 3, 6 or 7 special statements inside of the artificial function's
37412 body. The first two statements are DECL_EXPRs for the artificial
37413 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37414 expression that uses those variables.
37415 If there was any INITIALIZER clause, this is followed by further statements,
37416 the fourth and fifth statements are DECL_EXPRs for the artificial
37417 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37418 constructor variant (first token after open paren is not omp_priv),
37419 then the sixth statement is a statement with the function call expression
37420 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37421 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37422 to initialize the OMP_PRIV artificial variable and there is seventh
37423 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37425 static bool
37426 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37428 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37429 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37430 type = TREE_TYPE (type);
37431 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37432 DECL_ARTIFICIAL (omp_out) = 1;
37433 pushdecl (omp_out);
37434 add_decl_expr (omp_out);
37435 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37436 DECL_ARTIFICIAL (omp_in) = 1;
37437 pushdecl (omp_in);
37438 add_decl_expr (omp_in);
37439 tree combiner;
37440 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37442 keep_next_level (true);
37443 tree block = begin_omp_structured_block ();
37444 combiner = cp_parser_expression (parser);
37445 finish_expr_stmt (combiner);
37446 block = finish_omp_structured_block (block);
37447 add_stmt (block);
37449 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37450 return false;
37452 const char *p = "";
37453 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37455 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37456 p = IDENTIFIER_POINTER (id);
37459 if (strcmp (p, "initializer") == 0)
37461 cp_lexer_consume_token (parser->lexer);
37462 matching_parens parens;
37463 if (!parens.require_open (parser))
37464 return false;
37466 p = "";
37467 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37469 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37470 p = IDENTIFIER_POINTER (id);
37473 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37474 DECL_ARTIFICIAL (omp_priv) = 1;
37475 pushdecl (omp_priv);
37476 add_decl_expr (omp_priv);
37477 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37478 DECL_ARTIFICIAL (omp_orig) = 1;
37479 pushdecl (omp_orig);
37480 add_decl_expr (omp_orig);
37482 keep_next_level (true);
37483 block = begin_omp_structured_block ();
37485 bool ctor = false;
37486 if (strcmp (p, "omp_priv") == 0)
37488 bool is_direct_init, is_non_constant_init;
37489 ctor = true;
37490 cp_lexer_consume_token (parser->lexer);
37491 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37492 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37493 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37494 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37495 == CPP_CLOSE_PAREN
37496 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37497 == CPP_CLOSE_PAREN))
37499 finish_omp_structured_block (block);
37500 error ("invalid initializer clause");
37501 return false;
37503 initializer = cp_parser_initializer (parser, &is_direct_init,
37504 &is_non_constant_init);
37505 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37506 NULL_TREE, LOOKUP_ONLYCONVERTING);
37508 else
37510 cp_parser_parse_tentatively (parser);
37511 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37512 /*check_dependency_p=*/true,
37513 /*template_p=*/NULL,
37514 /*declarator_p=*/false,
37515 /*optional_p=*/false);
37516 vec<tree, va_gc> *args;
37517 if (fn_name == error_mark_node
37518 || cp_parser_error_occurred (parser)
37519 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37520 || ((args = cp_parser_parenthesized_expression_list
37521 (parser, non_attr, /*cast_p=*/false,
37522 /*allow_expansion_p=*/true,
37523 /*non_constant_p=*/NULL)),
37524 cp_parser_error_occurred (parser)))
37526 finish_omp_structured_block (block);
37527 cp_parser_abort_tentative_parse (parser);
37528 cp_parser_error (parser, "expected id-expression (arguments)");
37529 return false;
37531 unsigned int i;
37532 tree arg;
37533 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37534 if (arg == omp_priv
37535 || (TREE_CODE (arg) == ADDR_EXPR
37536 && TREE_OPERAND (arg, 0) == omp_priv))
37537 break;
37538 cp_parser_abort_tentative_parse (parser);
37539 if (arg == NULL_TREE)
37540 error ("one of the initializer call arguments should be %<omp_priv%>"
37541 " or %<&omp_priv%>");
37542 initializer = cp_parser_postfix_expression (parser, false, false, false,
37543 false, NULL);
37544 finish_expr_stmt (initializer);
37547 block = finish_omp_structured_block (block);
37548 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37549 add_stmt (block);
37551 if (ctor)
37552 add_decl_expr (omp_orig);
37554 if (!parens.require_close (parser))
37555 return false;
37558 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37559 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37560 UNKNOWN_LOCATION);
37562 return true;
37565 /* OpenMP 4.0
37566 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37567 initializer-clause[opt] new-line
37569 initializer-clause:
37570 initializer (omp_priv initializer)
37571 initializer (function-name (argument-list)) */
37573 static void
37574 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37575 enum pragma_context)
37577 auto_vec<tree> types;
37578 enum tree_code reduc_code = ERROR_MARK;
37579 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37580 unsigned int i;
37581 cp_token *first_token;
37582 cp_token_cache *cp;
37583 int errs;
37584 void *p;
37586 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37587 p = obstack_alloc (&declarator_obstack, 0);
37589 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37590 goto fail;
37592 switch (cp_lexer_peek_token (parser->lexer)->type)
37594 case CPP_PLUS:
37595 reduc_code = PLUS_EXPR;
37596 break;
37597 case CPP_MULT:
37598 reduc_code = MULT_EXPR;
37599 break;
37600 case CPP_MINUS:
37601 reduc_code = MINUS_EXPR;
37602 break;
37603 case CPP_AND:
37604 reduc_code = BIT_AND_EXPR;
37605 break;
37606 case CPP_XOR:
37607 reduc_code = BIT_XOR_EXPR;
37608 break;
37609 case CPP_OR:
37610 reduc_code = BIT_IOR_EXPR;
37611 break;
37612 case CPP_AND_AND:
37613 reduc_code = TRUTH_ANDIF_EXPR;
37614 break;
37615 case CPP_OR_OR:
37616 reduc_code = TRUTH_ORIF_EXPR;
37617 break;
37618 case CPP_NAME:
37619 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37620 break;
37621 default:
37622 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37623 "%<|%>, %<&&%>, %<||%> or identifier");
37624 goto fail;
37627 if (reduc_code != ERROR_MARK)
37628 cp_lexer_consume_token (parser->lexer);
37630 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37631 if (reduc_id == error_mark_node)
37632 goto fail;
37634 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37635 goto fail;
37637 /* Types may not be defined in declare reduction type list. */
37638 const char *saved_message;
37639 saved_message = parser->type_definition_forbidden_message;
37640 parser->type_definition_forbidden_message
37641 = G_("types may not be defined in declare reduction type list");
37642 bool saved_colon_corrects_to_scope_p;
37643 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37644 parser->colon_corrects_to_scope_p = false;
37645 bool saved_colon_doesnt_start_class_def_p;
37646 saved_colon_doesnt_start_class_def_p
37647 = parser->colon_doesnt_start_class_def_p;
37648 parser->colon_doesnt_start_class_def_p = true;
37650 while (true)
37652 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37653 type = cp_parser_type_id (parser);
37654 if (type == error_mark_node)
37656 else if (ARITHMETIC_TYPE_P (type)
37657 && (orig_reduc_id == NULL_TREE
37658 || (TREE_CODE (type) != COMPLEX_TYPE
37659 && (id_equal (orig_reduc_id, "min")
37660 || id_equal (orig_reduc_id, "max")))))
37661 error_at (loc, "predeclared arithmetic type %qT in "
37662 "%<#pragma omp declare reduction%>", type);
37663 else if (TREE_CODE (type) == FUNCTION_TYPE
37664 || TREE_CODE (type) == METHOD_TYPE
37665 || TREE_CODE (type) == ARRAY_TYPE)
37666 error_at (loc, "function or array type %qT in "
37667 "%<#pragma omp declare reduction%>", type);
37668 else if (TREE_CODE (type) == REFERENCE_TYPE)
37669 error_at (loc, "reference type %qT in "
37670 "%<#pragma omp declare reduction%>", type);
37671 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37672 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37673 "%<#pragma omp declare reduction%>", type);
37674 else
37675 types.safe_push (type);
37677 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37678 cp_lexer_consume_token (parser->lexer);
37679 else
37680 break;
37683 /* Restore the saved message. */
37684 parser->type_definition_forbidden_message = saved_message;
37685 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37686 parser->colon_doesnt_start_class_def_p
37687 = saved_colon_doesnt_start_class_def_p;
37689 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37690 || types.is_empty ())
37692 fail:
37693 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37694 goto done;
37697 first_token = cp_lexer_peek_token (parser->lexer);
37698 cp = NULL;
37699 errs = errorcount;
37700 FOR_EACH_VEC_ELT (types, i, type)
37702 tree fntype
37703 = build_function_type_list (void_type_node,
37704 cp_build_reference_type (type, false),
37705 NULL_TREE);
37706 tree this_reduc_id = reduc_id;
37707 if (!dependent_type_p (type))
37708 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37709 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37710 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37711 DECL_ARTIFICIAL (fndecl) = 1;
37712 DECL_EXTERNAL (fndecl) = 1;
37713 DECL_DECLARED_INLINE_P (fndecl) = 1;
37714 DECL_IGNORED_P (fndecl) = 1;
37715 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37716 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37717 DECL_ATTRIBUTES (fndecl)
37718 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37719 DECL_ATTRIBUTES (fndecl));
37720 if (processing_template_decl)
37721 fndecl = push_template_decl (fndecl);
37722 bool block_scope = false;
37723 tree block = NULL_TREE;
37724 if (current_function_decl)
37726 block_scope = true;
37727 DECL_CONTEXT (fndecl) = global_namespace;
37728 if (!processing_template_decl)
37729 pushdecl (fndecl);
37731 else if (current_class_type)
37733 if (cp == NULL)
37735 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37736 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37737 cp_lexer_consume_token (parser->lexer);
37738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37739 goto fail;
37740 cp = cp_token_cache_new (first_token,
37741 cp_lexer_peek_nth_token (parser->lexer,
37742 2));
37744 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37745 finish_member_declaration (fndecl);
37746 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37747 DECL_PENDING_INLINE_P (fndecl) = 1;
37748 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37749 continue;
37751 else
37753 DECL_CONTEXT (fndecl) = current_namespace;
37754 pushdecl (fndecl);
37756 if (!block_scope)
37757 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37758 else
37759 block = begin_omp_structured_block ();
37760 if (cp)
37762 cp_parser_push_lexer_for_tokens (parser, cp);
37763 parser->lexer->in_pragma = true;
37765 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37767 if (!block_scope)
37768 finish_function (/*inline_p=*/false);
37769 else
37770 DECL_CONTEXT (fndecl) = current_function_decl;
37771 if (cp)
37772 cp_parser_pop_lexer (parser);
37773 goto fail;
37775 if (cp)
37776 cp_parser_pop_lexer (parser);
37777 if (!block_scope)
37778 finish_function (/*inline_p=*/false);
37779 else
37781 DECL_CONTEXT (fndecl) = current_function_decl;
37782 block = finish_omp_structured_block (block);
37783 if (TREE_CODE (block) == BIND_EXPR)
37784 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37785 else if (TREE_CODE (block) == STATEMENT_LIST)
37786 DECL_SAVED_TREE (fndecl) = block;
37787 if (processing_template_decl)
37788 add_decl_expr (fndecl);
37790 cp_check_omp_declare_reduction (fndecl);
37791 if (cp == NULL && types.length () > 1)
37792 cp = cp_token_cache_new (first_token,
37793 cp_lexer_peek_nth_token (parser->lexer, 2));
37794 if (errs != errorcount)
37795 break;
37798 cp_parser_require_pragma_eol (parser, pragma_tok);
37800 done:
37801 /* Free any declarators allocated. */
37802 obstack_free (&declarator_obstack, p);
37805 /* OpenMP 4.0
37806 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37807 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37808 initializer-clause[opt] new-line
37809 #pragma omp declare target new-line */
37811 static bool
37812 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37813 enum pragma_context context)
37815 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37817 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37818 const char *p = IDENTIFIER_POINTER (id);
37820 if (strcmp (p, "simd") == 0)
37822 cp_lexer_consume_token (parser->lexer);
37823 cp_parser_omp_declare_simd (parser, pragma_tok,
37824 context);
37825 return true;
37827 cp_ensure_no_omp_declare_simd (parser);
37828 if (strcmp (p, "reduction") == 0)
37830 cp_lexer_consume_token (parser->lexer);
37831 cp_parser_omp_declare_reduction (parser, pragma_tok,
37832 context);
37833 return false;
37835 if (!flag_openmp) /* flag_openmp_simd */
37837 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37838 return false;
37840 if (strcmp (p, "target") == 0)
37842 cp_lexer_consume_token (parser->lexer);
37843 cp_parser_omp_declare_target (parser, pragma_tok);
37844 return false;
37847 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37848 "or %<target%>");
37849 cp_parser_require_pragma_eol (parser, pragma_tok);
37850 return false;
37853 /* OpenMP 4.5:
37854 #pragma omp taskloop taskloop-clause[optseq] new-line
37855 for-loop
37857 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37858 for-loop */
37860 #define OMP_TASKLOOP_CLAUSE_MASK \
37861 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37876 static tree
37877 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37878 char *p_name, omp_clause_mask mask, tree *cclauses,
37879 bool *if_p)
37881 tree clauses, sb, ret;
37882 unsigned int save;
37883 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37885 strcat (p_name, " taskloop");
37886 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37890 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37891 const char *p = IDENTIFIER_POINTER (id);
37893 if (strcmp (p, "simd") == 0)
37895 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37896 if (cclauses == NULL)
37897 cclauses = cclauses_buf;
37899 cp_lexer_consume_token (parser->lexer);
37900 if (!flag_openmp) /* flag_openmp_simd */
37901 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37902 cclauses, if_p);
37903 sb = begin_omp_structured_block ();
37904 save = cp_parser_begin_omp_structured_block (parser);
37905 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37906 cclauses, if_p);
37907 cp_parser_end_omp_structured_block (parser, save);
37908 tree body = finish_omp_structured_block (sb);
37909 if (ret == NULL)
37910 return ret;
37911 ret = make_node (OMP_TASKLOOP);
37912 TREE_TYPE (ret) = void_type_node;
37913 OMP_FOR_BODY (ret) = body;
37914 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37915 SET_EXPR_LOCATION (ret, loc);
37916 add_stmt (ret);
37917 return ret;
37920 if (!flag_openmp) /* flag_openmp_simd */
37922 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37923 return NULL_TREE;
37926 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37927 cclauses == NULL);
37928 if (cclauses)
37930 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37931 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37934 sb = begin_omp_structured_block ();
37935 save = cp_parser_begin_omp_structured_block (parser);
37937 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37938 if_p);
37940 cp_parser_end_omp_structured_block (parser, save);
37941 add_stmt (finish_omp_structured_block (sb));
37943 return ret;
37947 /* OpenACC 2.0:
37948 # pragma acc routine oacc-routine-clause[optseq] new-line
37949 function-definition
37951 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37954 #define OACC_ROUTINE_CLAUSE_MASK \
37955 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37961 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37962 component, which must resolve to a declared namespace-scope
37963 function. The clauses are either processed directly (for a named
37964 function), or defered until the immediatley following declaration
37965 is parsed. */
37967 static void
37968 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37969 enum pragma_context context)
37971 gcc_checking_assert (context == pragma_external);
37972 /* The checking for "another pragma following this one" in the "no optional
37973 '( name )'" case makes sure that we dont re-enter. */
37974 gcc_checking_assert (parser->oacc_routine == NULL);
37976 cp_oacc_routine_data data;
37977 data.error_seen = false;
37978 data.fndecl_seen = false;
37979 data.tokens = vNULL;
37980 data.clauses = NULL_TREE;
37981 data.loc = pragma_tok->location;
37982 /* It is safe to take the address of a local variable; it will only be
37983 used while this scope is live. */
37984 parser->oacc_routine = &data;
37986 /* Look for optional '( name )'. */
37987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37989 matching_parens parens;
37990 parens.consume_open (parser); /* '(' */
37992 /* We parse the name as an id-expression. If it resolves to
37993 anything other than a non-overloaded function at namespace
37994 scope, it's an error. */
37995 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37996 tree name = cp_parser_id_expression (parser,
37997 /*template_keyword_p=*/false,
37998 /*check_dependency_p=*/false,
37999 /*template_p=*/NULL,
38000 /*declarator_p=*/false,
38001 /*optional_p=*/false);
38002 tree decl = (identifier_p (name)
38003 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38004 : name);
38005 if (name != error_mark_node && decl == error_mark_node)
38006 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38008 if (decl == error_mark_node
38009 || !parens.require_close (parser))
38011 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38012 parser->oacc_routine = NULL;
38013 return;
38016 data.clauses
38017 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38018 "#pragma acc routine",
38019 cp_lexer_peek_token (parser->lexer));
38021 if (decl && is_overloaded_fn (decl)
38022 && (TREE_CODE (decl) != FUNCTION_DECL
38023 || DECL_FUNCTION_TEMPLATE_P (decl)))
38025 error_at (name_loc,
38026 "%<#pragma acc routine%> names a set of overloads");
38027 parser->oacc_routine = NULL;
38028 return;
38031 /* Perhaps we should use the same rule as declarations in different
38032 namespaces? */
38033 if (!DECL_NAMESPACE_SCOPE_P (decl))
38035 error_at (name_loc,
38036 "%qD does not refer to a namespace scope function", decl);
38037 parser->oacc_routine = NULL;
38038 return;
38041 if (TREE_CODE (decl) != FUNCTION_DECL)
38043 error_at (name_loc, "%qD does not refer to a function", decl);
38044 parser->oacc_routine = NULL;
38045 return;
38048 cp_finalize_oacc_routine (parser, decl, false);
38049 parser->oacc_routine = NULL;
38051 else /* No optional '( name )'. */
38053 /* Store away all pragma tokens. */
38054 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38055 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38056 cp_lexer_consume_token (parser->lexer);
38057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38058 parser->oacc_routine->error_seen = true;
38059 cp_parser_require_pragma_eol (parser, pragma_tok);
38060 struct cp_token_cache *cp
38061 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38062 parser->oacc_routine->tokens.safe_push (cp);
38064 /* Emit a helpful diagnostic if there's another pragma following this
38065 one. */
38066 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38068 cp_ensure_no_oacc_routine (parser);
38069 data.tokens.release ();
38070 /* ..., and then just keep going. */
38071 return;
38074 /* We only have to consider the pragma_external case here. */
38075 cp_parser_declaration (parser);
38076 if (parser->oacc_routine
38077 && !parser->oacc_routine->fndecl_seen)
38078 cp_ensure_no_oacc_routine (parser);
38079 else
38080 parser->oacc_routine = NULL;
38081 data.tokens.release ();
38085 /* Finalize #pragma acc routine clauses after direct declarator has
38086 been parsed. */
38088 static tree
38089 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38091 struct cp_token_cache *ce;
38092 cp_oacc_routine_data *data = parser->oacc_routine;
38094 if (!data->error_seen && data->fndecl_seen)
38096 error_at (data->loc,
38097 "%<#pragma acc routine%> not immediately followed by "
38098 "a single function declaration or definition");
38099 data->error_seen = true;
38101 if (data->error_seen)
38102 return attrs;
38104 gcc_checking_assert (data->tokens.length () == 1);
38105 ce = data->tokens[0];
38107 cp_parser_push_lexer_for_tokens (parser, ce);
38108 parser->lexer->in_pragma = true;
38109 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38111 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38112 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38113 parser->oacc_routine->clauses
38114 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38115 "#pragma acc routine", pragma_tok);
38116 cp_parser_pop_lexer (parser);
38117 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38118 fndecl_seen. */
38120 return attrs;
38123 /* Apply any saved OpenACC routine clauses to a just-parsed
38124 declaration. */
38126 static void
38127 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38129 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38131 /* Keep going if we're in error reporting mode. */
38132 if (parser->oacc_routine->error_seen
38133 || fndecl == error_mark_node)
38134 return;
38136 if (parser->oacc_routine->fndecl_seen)
38138 error_at (parser->oacc_routine->loc,
38139 "%<#pragma acc routine%> not immediately followed by"
38140 " a single function declaration or definition");
38141 parser->oacc_routine = NULL;
38142 return;
38144 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38146 cp_ensure_no_oacc_routine (parser);
38147 return;
38150 if (oacc_get_fn_attrib (fndecl))
38152 error_at (parser->oacc_routine->loc,
38153 "%<#pragma acc routine%> already applied to %qD", fndecl);
38154 parser->oacc_routine = NULL;
38155 return;
38158 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38160 error_at (parser->oacc_routine->loc,
38161 TREE_USED (fndecl)
38162 ? G_("%<#pragma acc routine%> must be applied before use")
38163 : G_("%<#pragma acc routine%> must be applied before "
38164 "definition"));
38165 parser->oacc_routine = NULL;
38166 return;
38169 /* Process the routine's dimension clauses. */
38170 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38171 oacc_replace_fn_attrib (fndecl, dims);
38173 /* Add an "omp declare target" attribute. */
38174 DECL_ATTRIBUTES (fndecl)
38175 = tree_cons (get_identifier ("omp declare target"),
38176 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38178 /* Don't unset parser->oacc_routine here: we may still need it to
38179 diagnose wrong usage. But, remember that we've used this "#pragma acc
38180 routine". */
38181 parser->oacc_routine->fndecl_seen = true;
38185 /* Main entry point to OpenMP statement pragmas. */
38187 static void
38188 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38190 tree stmt;
38191 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38192 omp_clause_mask mask (0);
38194 switch (cp_parser_pragma_kind (pragma_tok))
38196 case PRAGMA_OACC_ATOMIC:
38197 cp_parser_omp_atomic (parser, pragma_tok);
38198 return;
38199 case PRAGMA_OACC_CACHE:
38200 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38201 break;
38202 case PRAGMA_OACC_DATA:
38203 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38204 break;
38205 case PRAGMA_OACC_ENTER_DATA:
38206 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38207 break;
38208 case PRAGMA_OACC_EXIT_DATA:
38209 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38210 break;
38211 case PRAGMA_OACC_HOST_DATA:
38212 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38213 break;
38214 case PRAGMA_OACC_KERNELS:
38215 case PRAGMA_OACC_PARALLEL:
38216 strcpy (p_name, "#pragma acc");
38217 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38218 if_p);
38219 break;
38220 case PRAGMA_OACC_LOOP:
38221 strcpy (p_name, "#pragma acc");
38222 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38223 if_p);
38224 break;
38225 case PRAGMA_OACC_UPDATE:
38226 stmt = cp_parser_oacc_update (parser, pragma_tok);
38227 break;
38228 case PRAGMA_OACC_WAIT:
38229 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38230 break;
38231 case PRAGMA_OMP_ATOMIC:
38232 cp_parser_omp_atomic (parser, pragma_tok);
38233 return;
38234 case PRAGMA_OMP_CRITICAL:
38235 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38236 break;
38237 case PRAGMA_OMP_DISTRIBUTE:
38238 strcpy (p_name, "#pragma omp");
38239 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38240 if_p);
38241 break;
38242 case PRAGMA_OMP_FOR:
38243 strcpy (p_name, "#pragma omp");
38244 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38245 if_p);
38246 break;
38247 case PRAGMA_OMP_MASTER:
38248 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38249 break;
38250 case PRAGMA_OMP_PARALLEL:
38251 strcpy (p_name, "#pragma omp");
38252 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38253 if_p);
38254 break;
38255 case PRAGMA_OMP_SECTIONS:
38256 strcpy (p_name, "#pragma omp");
38257 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38258 break;
38259 case PRAGMA_OMP_SIMD:
38260 strcpy (p_name, "#pragma omp");
38261 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38262 if_p);
38263 break;
38264 case PRAGMA_OMP_SINGLE:
38265 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38266 break;
38267 case PRAGMA_OMP_TASK:
38268 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38269 break;
38270 case PRAGMA_OMP_TASKGROUP:
38271 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38272 break;
38273 case PRAGMA_OMP_TASKLOOP:
38274 strcpy (p_name, "#pragma omp");
38275 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38276 if_p);
38277 break;
38278 case PRAGMA_OMP_TEAMS:
38279 strcpy (p_name, "#pragma omp");
38280 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38281 if_p);
38282 break;
38283 default:
38284 gcc_unreachable ();
38287 protected_set_expr_location (stmt, pragma_tok->location);
38290 /* Transactional Memory parsing routines. */
38292 /* Parse a transaction attribute.
38294 txn-attribute:
38295 attribute
38296 [ [ identifier ] ]
38298 We use this instead of cp_parser_attributes_opt for transactions to avoid
38299 the pedwarn in C++98 mode. */
38301 static tree
38302 cp_parser_txn_attribute_opt (cp_parser *parser)
38304 cp_token *token;
38305 tree attr_name, attr = NULL;
38307 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38308 return cp_parser_attributes_opt (parser);
38310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38311 return NULL_TREE;
38312 cp_lexer_consume_token (parser->lexer);
38313 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38314 goto error1;
38316 token = cp_lexer_peek_token (parser->lexer);
38317 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38319 token = cp_lexer_consume_token (parser->lexer);
38321 attr_name = (token->type == CPP_KEYWORD
38322 /* For keywords, use the canonical spelling,
38323 not the parsed identifier. */
38324 ? ridpointers[(int) token->keyword]
38325 : token->u.value);
38326 attr = build_tree_list (attr_name, NULL_TREE);
38328 else
38329 cp_parser_error (parser, "expected identifier");
38331 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38332 error1:
38333 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38334 return attr;
38337 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38339 transaction-statement:
38340 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38341 compound-statement
38342 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38345 static tree
38346 cp_parser_transaction (cp_parser *parser, cp_token *token)
38348 unsigned char old_in = parser->in_transaction;
38349 unsigned char this_in = 1, new_in;
38350 enum rid keyword = token->keyword;
38351 tree stmt, attrs, noex;
38353 cp_lexer_consume_token (parser->lexer);
38355 if (keyword == RID_TRANSACTION_RELAXED
38356 || keyword == RID_SYNCHRONIZED)
38357 this_in |= TM_STMT_ATTR_RELAXED;
38358 else
38360 attrs = cp_parser_txn_attribute_opt (parser);
38361 if (attrs)
38362 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38365 /* Parse a noexcept specification. */
38366 if (keyword == RID_ATOMIC_NOEXCEPT)
38367 noex = boolean_true_node;
38368 else if (keyword == RID_ATOMIC_CANCEL)
38370 /* cancel-and-throw is unimplemented. */
38371 sorry ("atomic_cancel");
38372 noex = NULL_TREE;
38374 else
38375 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38377 /* Keep track if we're in the lexical scope of an outer transaction. */
38378 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38380 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38382 parser->in_transaction = new_in;
38383 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38384 parser->in_transaction = old_in;
38386 finish_transaction_stmt (stmt, NULL, this_in, noex);
38388 return stmt;
38391 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38393 transaction-expression:
38394 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38395 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38398 static tree
38399 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38401 unsigned char old_in = parser->in_transaction;
38402 unsigned char this_in = 1;
38403 cp_token *token;
38404 tree expr, noex;
38405 bool noex_expr;
38406 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38408 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38409 || keyword == RID_TRANSACTION_RELAXED);
38411 if (!flag_tm)
38412 error_at (loc,
38413 keyword == RID_TRANSACTION_RELAXED
38414 ? G_("%<__transaction_relaxed%> without transactional memory "
38415 "support enabled")
38416 : G_("%<__transaction_atomic%> without transactional memory "
38417 "support enabled"));
38419 token = cp_parser_require_keyword (parser, keyword,
38420 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38421 : RT_TRANSACTION_RELAXED));
38422 gcc_assert (token != NULL);
38424 if (keyword == RID_TRANSACTION_RELAXED)
38425 this_in |= TM_STMT_ATTR_RELAXED;
38427 /* Set this early. This might mean that we allow transaction_cancel in
38428 an expression that we find out later actually has to be a constexpr.
38429 However, we expect that cxx_constant_value will be able to deal with
38430 this; also, if the noexcept has no constexpr, then what we parse next
38431 really is a transaction's body. */
38432 parser->in_transaction = this_in;
38434 /* Parse a noexcept specification. */
38435 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38436 true);
38438 if (!noex || !noex_expr
38439 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38441 matching_parens parens;
38442 parens.require_open (parser);
38444 expr = cp_parser_expression (parser);
38445 expr = finish_parenthesized_expr (expr);
38447 parens.require_close (parser);
38449 else
38451 /* The only expression that is available got parsed for the noexcept
38452 already. noexcept is true then. */
38453 expr = noex;
38454 noex = boolean_true_node;
38457 expr = build_transaction_expr (token->location, expr, this_in, noex);
38458 parser->in_transaction = old_in;
38460 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38461 return error_mark_node;
38463 return (flag_tm ? expr : error_mark_node);
38466 /* Parse a function-transaction-block.
38468 function-transaction-block:
38469 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38470 function-body
38471 __transaction_atomic txn-attribute[opt] function-try-block
38472 __transaction_relaxed ctor-initializer[opt] function-body
38473 __transaction_relaxed function-try-block
38476 static void
38477 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38479 unsigned char old_in = parser->in_transaction;
38480 unsigned char new_in = 1;
38481 tree compound_stmt, stmt, attrs;
38482 cp_token *token;
38484 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38485 || keyword == RID_TRANSACTION_RELAXED);
38486 token = cp_parser_require_keyword (parser, keyword,
38487 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38488 : RT_TRANSACTION_RELAXED));
38489 gcc_assert (token != NULL);
38491 if (keyword == RID_TRANSACTION_RELAXED)
38492 new_in |= TM_STMT_ATTR_RELAXED;
38493 else
38495 attrs = cp_parser_txn_attribute_opt (parser);
38496 if (attrs)
38497 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38500 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38502 parser->in_transaction = new_in;
38504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38505 cp_parser_function_try_block (parser);
38506 else
38507 cp_parser_ctor_initializer_opt_and_function_body
38508 (parser, /*in_function_try_block=*/false);
38510 parser->in_transaction = old_in;
38512 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38515 /* Parse a __transaction_cancel statement.
38517 cancel-statement:
38518 __transaction_cancel txn-attribute[opt] ;
38519 __transaction_cancel txn-attribute[opt] throw-expression ;
38521 ??? Cancel and throw is not yet implemented. */
38523 static tree
38524 cp_parser_transaction_cancel (cp_parser *parser)
38526 cp_token *token;
38527 bool is_outer = false;
38528 tree stmt, attrs;
38530 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38531 RT_TRANSACTION_CANCEL);
38532 gcc_assert (token != NULL);
38534 attrs = cp_parser_txn_attribute_opt (parser);
38535 if (attrs)
38536 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38538 /* ??? Parse cancel-and-throw here. */
38540 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38542 if (!flag_tm)
38544 error_at (token->location, "%<__transaction_cancel%> without "
38545 "transactional memory support enabled");
38546 return error_mark_node;
38548 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38550 error_at (token->location, "%<__transaction_cancel%> within a "
38551 "%<__transaction_relaxed%>");
38552 return error_mark_node;
38554 else if (is_outer)
38556 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38557 && !is_tm_may_cancel_outer (current_function_decl))
38559 error_at (token->location, "outer %<__transaction_cancel%> not "
38560 "within outer %<__transaction_atomic%>");
38561 error_at (token->location,
38562 " or a %<transaction_may_cancel_outer%> function");
38563 return error_mark_node;
38566 else if (parser->in_transaction == 0)
38568 error_at (token->location, "%<__transaction_cancel%> not within "
38569 "%<__transaction_atomic%>");
38570 return error_mark_node;
38573 stmt = build_tm_abort_call (token->location, is_outer);
38574 add_stmt (stmt);
38576 return stmt;
38579 /* The parser. */
38581 static GTY (()) cp_parser *the_parser;
38584 /* Special handling for the first token or line in the file. The first
38585 thing in the file might be #pragma GCC pch_preprocess, which loads a
38586 PCH file, which is a GC collection point. So we need to handle this
38587 first pragma without benefit of an existing lexer structure.
38589 Always returns one token to the caller in *FIRST_TOKEN. This is
38590 either the true first token of the file, or the first token after
38591 the initial pragma. */
38593 static void
38594 cp_parser_initial_pragma (cp_token *first_token)
38596 tree name = NULL;
38598 cp_lexer_get_preprocessor_token (NULL, first_token);
38599 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38600 return;
38602 cp_lexer_get_preprocessor_token (NULL, first_token);
38603 if (first_token->type == CPP_STRING)
38605 name = first_token->u.value;
38607 cp_lexer_get_preprocessor_token (NULL, first_token);
38608 if (first_token->type != CPP_PRAGMA_EOL)
38609 error_at (first_token->location,
38610 "junk at end of %<#pragma GCC pch_preprocess%>");
38612 else
38613 error_at (first_token->location, "expected string literal");
38615 /* Skip to the end of the pragma. */
38616 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38617 cp_lexer_get_preprocessor_token (NULL, first_token);
38619 /* Now actually load the PCH file. */
38620 if (name)
38621 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38623 /* Read one more token to return to our caller. We have to do this
38624 after reading the PCH file in, since its pointers have to be
38625 live. */
38626 cp_lexer_get_preprocessor_token (NULL, first_token);
38629 /* Parse a pragma GCC ivdep. */
38631 static bool
38632 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38634 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38635 return true;
38638 /* Parse a pragma GCC unroll. */
38640 static unsigned short
38641 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38643 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38644 tree expr = cp_parser_constant_expression (parser);
38645 unsigned short unroll;
38646 expr = maybe_constant_value (expr);
38647 HOST_WIDE_INT lunroll = 0;
38648 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38649 || TREE_CODE (expr) != INTEGER_CST
38650 || (lunroll = tree_to_shwi (expr)) < 0
38651 || lunroll >= USHRT_MAX)
38653 error_at (location, "%<#pragma GCC unroll%> requires an"
38654 " assignment-expression that evaluates to a non-negative"
38655 " integral constant less than %u", USHRT_MAX);
38656 unroll = 0;
38658 else
38660 unroll = (unsigned short)lunroll;
38661 if (unroll == 0)
38662 unroll = 1;
38664 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38665 return unroll;
38668 /* Normal parsing of a pragma token. Here we can (and must) use the
38669 regular lexer. */
38671 static bool
38672 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38674 cp_token *pragma_tok;
38675 unsigned int id;
38676 tree stmt;
38677 bool ret;
38679 pragma_tok = cp_lexer_consume_token (parser->lexer);
38680 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38681 parser->lexer->in_pragma = true;
38683 id = cp_parser_pragma_kind (pragma_tok);
38684 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38685 cp_ensure_no_omp_declare_simd (parser);
38686 switch (id)
38688 case PRAGMA_GCC_PCH_PREPROCESS:
38689 error_at (pragma_tok->location,
38690 "%<#pragma GCC pch_preprocess%> must be first");
38691 break;
38693 case PRAGMA_OMP_BARRIER:
38694 switch (context)
38696 case pragma_compound:
38697 cp_parser_omp_barrier (parser, pragma_tok);
38698 return false;
38699 case pragma_stmt:
38700 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38701 "used in compound statements", "omp barrier");
38702 break;
38703 default:
38704 goto bad_stmt;
38706 break;
38708 case PRAGMA_OMP_FLUSH:
38709 switch (context)
38711 case pragma_compound:
38712 cp_parser_omp_flush (parser, pragma_tok);
38713 return false;
38714 case pragma_stmt:
38715 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38716 "used in compound statements", "omp flush");
38717 break;
38718 default:
38719 goto bad_stmt;
38721 break;
38723 case PRAGMA_OMP_TASKWAIT:
38724 switch (context)
38726 case pragma_compound:
38727 cp_parser_omp_taskwait (parser, pragma_tok);
38728 return false;
38729 case pragma_stmt:
38730 error_at (pragma_tok->location,
38731 "%<#pragma %s%> may only be used in compound statements",
38732 "omp taskwait");
38733 break;
38734 default:
38735 goto bad_stmt;
38737 break;
38739 case PRAGMA_OMP_TASKYIELD:
38740 switch (context)
38742 case pragma_compound:
38743 cp_parser_omp_taskyield (parser, pragma_tok);
38744 return false;
38745 case pragma_stmt:
38746 error_at (pragma_tok->location,
38747 "%<#pragma %s%> may only be used in compound statements",
38748 "omp taskyield");
38749 break;
38750 default:
38751 goto bad_stmt;
38753 break;
38755 case PRAGMA_OMP_CANCEL:
38756 switch (context)
38758 case pragma_compound:
38759 cp_parser_omp_cancel (parser, pragma_tok);
38760 return false;
38761 case pragma_stmt:
38762 error_at (pragma_tok->location,
38763 "%<#pragma %s%> may only be used in compound statements",
38764 "omp cancel");
38765 break;
38766 default:
38767 goto bad_stmt;
38769 break;
38771 case PRAGMA_OMP_CANCELLATION_POINT:
38772 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38773 return false;
38775 case PRAGMA_OMP_THREADPRIVATE:
38776 cp_parser_omp_threadprivate (parser, pragma_tok);
38777 return false;
38779 case PRAGMA_OMP_DECLARE:
38780 return cp_parser_omp_declare (parser, pragma_tok, context);
38782 case PRAGMA_OACC_DECLARE:
38783 cp_parser_oacc_declare (parser, pragma_tok);
38784 return false;
38786 case PRAGMA_OACC_ENTER_DATA:
38787 if (context == pragma_stmt)
38789 error_at (pragma_tok->location,
38790 "%<#pragma %s%> may only be used in compound statements",
38791 "acc enter data");
38792 break;
38794 else if (context != pragma_compound)
38795 goto bad_stmt;
38796 cp_parser_omp_construct (parser, pragma_tok, if_p);
38797 return true;
38799 case PRAGMA_OACC_EXIT_DATA:
38800 if (context == pragma_stmt)
38802 error_at (pragma_tok->location,
38803 "%<#pragma %s%> may only be used in compound statements",
38804 "acc exit data");
38805 break;
38807 else if (context != pragma_compound)
38808 goto bad_stmt;
38809 cp_parser_omp_construct (parser, pragma_tok, if_p);
38810 return true;
38812 case PRAGMA_OACC_ROUTINE:
38813 if (context != pragma_external)
38815 error_at (pragma_tok->location,
38816 "%<#pragma acc routine%> must be at file scope");
38817 break;
38819 cp_parser_oacc_routine (parser, pragma_tok, context);
38820 return false;
38822 case PRAGMA_OACC_UPDATE:
38823 if (context == pragma_stmt)
38825 error_at (pragma_tok->location,
38826 "%<#pragma %s%> may only be used in compound statements",
38827 "acc update");
38828 break;
38830 else if (context != pragma_compound)
38831 goto bad_stmt;
38832 cp_parser_omp_construct (parser, pragma_tok, if_p);
38833 return true;
38835 case PRAGMA_OACC_WAIT:
38836 if (context == pragma_stmt)
38838 error_at (pragma_tok->location,
38839 "%<#pragma %s%> may only be used in compound statements",
38840 "acc wait");
38841 break;
38843 else if (context != pragma_compound)
38844 goto bad_stmt;
38845 cp_parser_omp_construct (parser, pragma_tok, if_p);
38846 return true;
38848 case PRAGMA_OACC_ATOMIC:
38849 case PRAGMA_OACC_CACHE:
38850 case PRAGMA_OACC_DATA:
38851 case PRAGMA_OACC_HOST_DATA:
38852 case PRAGMA_OACC_KERNELS:
38853 case PRAGMA_OACC_PARALLEL:
38854 case PRAGMA_OACC_LOOP:
38855 case PRAGMA_OMP_ATOMIC:
38856 case PRAGMA_OMP_CRITICAL:
38857 case PRAGMA_OMP_DISTRIBUTE:
38858 case PRAGMA_OMP_FOR:
38859 case PRAGMA_OMP_MASTER:
38860 case PRAGMA_OMP_PARALLEL:
38861 case PRAGMA_OMP_SECTIONS:
38862 case PRAGMA_OMP_SIMD:
38863 case PRAGMA_OMP_SINGLE:
38864 case PRAGMA_OMP_TASK:
38865 case PRAGMA_OMP_TASKGROUP:
38866 case PRAGMA_OMP_TASKLOOP:
38867 case PRAGMA_OMP_TEAMS:
38868 if (context != pragma_stmt && context != pragma_compound)
38869 goto bad_stmt;
38870 stmt = push_omp_privatization_clauses (false);
38871 cp_parser_omp_construct (parser, pragma_tok, if_p);
38872 pop_omp_privatization_clauses (stmt);
38873 return true;
38875 case PRAGMA_OMP_ORDERED:
38876 if (context != pragma_stmt && context != pragma_compound)
38877 goto bad_stmt;
38878 stmt = push_omp_privatization_clauses (false);
38879 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38880 pop_omp_privatization_clauses (stmt);
38881 return ret;
38883 case PRAGMA_OMP_TARGET:
38884 if (context != pragma_stmt && context != pragma_compound)
38885 goto bad_stmt;
38886 stmt = push_omp_privatization_clauses (false);
38887 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38888 pop_omp_privatization_clauses (stmt);
38889 return ret;
38891 case PRAGMA_OMP_END_DECLARE_TARGET:
38892 cp_parser_omp_end_declare_target (parser, pragma_tok);
38893 return false;
38895 case PRAGMA_OMP_SECTION:
38896 error_at (pragma_tok->location,
38897 "%<#pragma omp section%> may only be used in "
38898 "%<#pragma omp sections%> construct");
38899 break;
38901 case PRAGMA_IVDEP:
38903 if (context == pragma_external)
38905 error_at (pragma_tok->location,
38906 "%<#pragma GCC ivdep%> must be inside a function");
38907 break;
38909 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38910 unsigned short unroll;
38911 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38912 if (tok->type == CPP_PRAGMA
38913 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38915 tok = cp_lexer_consume_token (parser->lexer);
38916 unroll = cp_parser_pragma_unroll (parser, tok);
38917 tok = cp_lexer_peek_token (the_parser->lexer);
38919 else
38920 unroll = 0;
38921 if (tok->type != CPP_KEYWORD
38922 || (tok->keyword != RID_FOR
38923 && tok->keyword != RID_WHILE
38924 && tok->keyword != RID_DO))
38926 cp_parser_error (parser, "for, while or do statement expected");
38927 return false;
38929 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38930 return true;
38933 case PRAGMA_UNROLL:
38935 if (context == pragma_external)
38937 error_at (pragma_tok->location,
38938 "%<#pragma GCC unroll%> must be inside a function");
38939 break;
38941 const unsigned short unroll
38942 = cp_parser_pragma_unroll (parser, pragma_tok);
38943 bool ivdep;
38944 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38945 if (tok->type == CPP_PRAGMA
38946 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38948 tok = cp_lexer_consume_token (parser->lexer);
38949 ivdep = cp_parser_pragma_ivdep (parser, tok);
38950 tok = cp_lexer_peek_token (the_parser->lexer);
38952 else
38953 ivdep = false;
38954 if (tok->type != CPP_KEYWORD
38955 || (tok->keyword != RID_FOR
38956 && tok->keyword != RID_WHILE
38957 && tok->keyword != RID_DO))
38959 cp_parser_error (parser, "for, while or do statement expected");
38960 return false;
38962 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38963 return true;
38966 default:
38967 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38968 c_invoke_pragma_handler (id);
38969 break;
38971 bad_stmt:
38972 cp_parser_error (parser, "expected declaration specifiers");
38973 break;
38976 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38977 return false;
38980 /* The interface the pragma parsers have to the lexer. */
38982 enum cpp_ttype
38983 pragma_lex (tree *value, location_t *loc)
38985 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38986 enum cpp_ttype ret = tok->type;
38988 *value = tok->u.value;
38989 if (loc)
38990 *loc = tok->location;
38992 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38993 ret = CPP_EOF;
38994 else if (ret == CPP_STRING)
38995 *value = cp_parser_string_literal (the_parser, false, false);
38996 else
38998 if (ret == CPP_KEYWORD)
38999 ret = CPP_NAME;
39000 cp_lexer_consume_token (the_parser->lexer);
39003 return ret;
39007 /* External interface. */
39009 /* Parse one entire translation unit. */
39011 void
39012 c_parse_file (void)
39014 static bool already_called = false;
39016 if (already_called)
39017 fatal_error (input_location,
39018 "inter-module optimizations not implemented for C++");
39019 already_called = true;
39021 the_parser = cp_parser_new ();
39022 push_deferring_access_checks (flag_access_control
39023 ? dk_no_deferred : dk_no_check);
39024 cp_parser_translation_unit (the_parser);
39025 the_parser = NULL;
39028 /* Create an identifier for a generic parameter type (a synthesized
39029 template parameter implied by `auto' or a concept identifier). */
39031 static GTY(()) int generic_parm_count;
39032 static tree
39033 make_generic_type_name ()
39035 char buf[32];
39036 sprintf (buf, "auto:%d", ++generic_parm_count);
39037 return get_identifier (buf);
39040 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39041 (creating a new template parameter list if necessary). Returns the newly
39042 created template type parm. */
39044 static tree
39045 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39047 gcc_assert (current_binding_level->kind == sk_function_parms);
39049 /* Before committing to modifying any scope, if we're in an
39050 implicit template scope, and we're trying to synthesize a
39051 constrained parameter, try to find a previous parameter with
39052 the same name. This is the same-type rule for abbreviated
39053 function templates.
39055 NOTE: We can generate implicit parameters when tentatively
39056 parsing a nested name specifier, only to reject that parse
39057 later. However, matching the same template-id as part of a
39058 direct-declarator should generate an identical template
39059 parameter, so this rule will merge them. */
39060 if (parser->implicit_template_scope && constr)
39062 tree t = parser->implicit_template_parms;
39063 while (t)
39065 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39067 tree d = TREE_VALUE (t);
39068 if (TREE_CODE (d) == PARM_DECL)
39069 /* Return the TEMPLATE_PARM_INDEX. */
39070 d = DECL_INITIAL (d);
39071 return d;
39073 t = TREE_CHAIN (t);
39077 /* We are either continuing a function template that already contains implicit
39078 template parameters, creating a new fully-implicit function template, or
39079 extending an existing explicit function template with implicit template
39080 parameters. */
39082 cp_binding_level *const entry_scope = current_binding_level;
39084 bool become_template = false;
39085 cp_binding_level *parent_scope = 0;
39087 if (parser->implicit_template_scope)
39089 gcc_assert (parser->implicit_template_parms);
39091 current_binding_level = parser->implicit_template_scope;
39093 else
39095 /* Roll back to the existing template parameter scope (in the case of
39096 extending an explicit function template) or introduce a new template
39097 parameter scope ahead of the function parameter scope (or class scope
39098 in the case of out-of-line member definitions). The function scope is
39099 added back after template parameter synthesis below. */
39101 cp_binding_level *scope = entry_scope;
39103 while (scope->kind == sk_function_parms)
39105 parent_scope = scope;
39106 scope = scope->level_chain;
39108 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39110 /* If not defining a class, then any class scope is a scope level in
39111 an out-of-line member definition. In this case simply wind back
39112 beyond the first such scope to inject the template parameter list.
39113 Otherwise wind back to the class being defined. The latter can
39114 occur in class member friend declarations such as:
39116 class A {
39117 void foo (auto);
39119 class B {
39120 friend void A::foo (auto);
39123 The template parameter list synthesized for the friend declaration
39124 must be injected in the scope of 'B'. This can also occur in
39125 erroneous cases such as:
39127 struct A {
39128 struct B {
39129 void foo (auto);
39131 void B::foo (auto) {}
39134 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39135 but, nevertheless, the template parameter list synthesized for the
39136 declarator should be injected into the scope of 'A' as if the
39137 ill-formed template was specified explicitly. */
39139 while (scope->kind == sk_class && !scope->defining_class_p)
39141 parent_scope = scope;
39142 scope = scope->level_chain;
39146 current_binding_level = scope;
39148 if (scope->kind != sk_template_parms
39149 || !function_being_declared_is_template_p (parser))
39151 /* Introduce a new template parameter list for implicit template
39152 parameters. */
39154 become_template = true;
39156 parser->implicit_template_scope
39157 = begin_scope (sk_template_parms, NULL);
39159 ++processing_template_decl;
39161 parser->fully_implicit_function_template_p = true;
39162 ++parser->num_template_parameter_lists;
39164 else
39166 /* Synthesize implicit template parameters at the end of the explicit
39167 template parameter list. */
39169 gcc_assert (current_template_parms);
39171 parser->implicit_template_scope = scope;
39173 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39174 parser->implicit_template_parms
39175 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39179 /* Synthesize a new template parameter and track the current template
39180 parameter chain with implicit_template_parms. */
39182 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39183 tree synth_id = make_generic_type_name ();
39184 tree synth_tmpl_parm;
39185 bool non_type = false;
39187 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39188 synth_tmpl_parm
39189 = finish_template_type_parm (class_type_node, synth_id);
39190 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39191 synth_tmpl_parm
39192 = finish_constrained_template_template_parm (proto, synth_id);
39193 else
39195 synth_tmpl_parm = copy_decl (proto);
39196 DECL_NAME (synth_tmpl_parm) = synth_id;
39197 non_type = true;
39200 // Attach the constraint to the parm before processing.
39201 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39202 TREE_TYPE (node) = constr;
39203 tree new_parm
39204 = process_template_parm (parser->implicit_template_parms,
39205 input_location,
39206 node,
39207 /*non_type=*/non_type,
39208 /*param_pack=*/false);
39210 // Chain the new parameter to the list of implicit parameters.
39211 if (parser->implicit_template_parms)
39212 parser->implicit_template_parms
39213 = TREE_CHAIN (parser->implicit_template_parms);
39214 else
39215 parser->implicit_template_parms = new_parm;
39217 tree new_decl = get_local_decls ();
39218 if (non_type)
39219 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39220 new_decl = DECL_INITIAL (new_decl);
39222 /* If creating a fully implicit function template, start the new implicit
39223 template parameter list with this synthesized type, otherwise grow the
39224 current template parameter list. */
39226 if (become_template)
39228 parent_scope->level_chain = current_binding_level;
39230 tree new_parms = make_tree_vec (1);
39231 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39232 current_template_parms = tree_cons (size_int (processing_template_decl),
39233 new_parms, current_template_parms);
39235 else
39237 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39238 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39239 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39240 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39243 // If the new parameter was constrained, we need to add that to the
39244 // constraints in the template parameter list.
39245 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39247 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39248 reqs = conjoin_constraints (reqs, req);
39249 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39252 current_binding_level = entry_scope;
39254 return new_decl;
39257 /* Finish the declaration of a fully implicit function template. Such a
39258 template has no explicit template parameter list so has not been through the
39259 normal template head and tail processing. synthesize_implicit_template_parm
39260 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39261 provided if the declaration is a class member such that its template
39262 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39263 form is returned. Otherwise NULL_TREE is returned. */
39265 static tree
39266 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39268 gcc_assert (parser->fully_implicit_function_template_p);
39270 if (member_decl_opt && member_decl_opt != error_mark_node
39271 && DECL_VIRTUAL_P (member_decl_opt))
39273 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39274 "implicit templates may not be %<virtual%>");
39275 DECL_VIRTUAL_P (member_decl_opt) = false;
39278 if (member_decl_opt)
39279 member_decl_opt = finish_member_template_decl (member_decl_opt);
39280 end_template_decl ();
39282 parser->fully_implicit_function_template_p = false;
39283 parser->implicit_template_parms = 0;
39284 parser->implicit_template_scope = 0;
39285 --parser->num_template_parameter_lists;
39287 return member_decl_opt;
39290 /* Like finish_fully_implicit_template, but to be used in error
39291 recovery, rearranging scopes so that we restore the state we had
39292 before synthesize_implicit_template_parm inserted the implement
39293 template parms scope. */
39295 static void
39296 abort_fully_implicit_template (cp_parser *parser)
39298 cp_binding_level *return_to_scope = current_binding_level;
39300 if (parser->implicit_template_scope
39301 && return_to_scope != parser->implicit_template_scope)
39303 cp_binding_level *child = return_to_scope;
39304 for (cp_binding_level *scope = child->level_chain;
39305 scope != parser->implicit_template_scope;
39306 scope = child->level_chain)
39307 child = scope;
39308 child->level_chain = parser->implicit_template_scope->level_chain;
39309 parser->implicit_template_scope->level_chain = return_to_scope;
39310 current_binding_level = parser->implicit_template_scope;
39312 else
39313 return_to_scope = return_to_scope->level_chain;
39315 finish_fully_implicit_template (parser, NULL);
39317 gcc_assert (current_binding_level == return_to_scope);
39320 /* Helper function for diagnostics that have complained about things
39321 being used with 'extern "C"' linkage.
39323 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39325 void
39326 maybe_show_extern_c_location (void)
39328 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39329 inform (the_parser->innermost_linkage_specification_location,
39330 "%<extern \"C\"%> linkage started here");
39333 #include "gt-cp-parser.h"